pcf8591_envctrl.c revision 1.9 1 /* $NetBSD: pcf8591_envctrl.c,v 1.9 2018/06/26 06:03:57 thorpej Exp $ */
2 /* $OpenBSD: pcf8591_envctrl.c,v 1.6 2007/10/25 21:17:20 kettenis Exp $ */
3
4 /*
5 * Copyright (c) 2006 Damien Miller <djm (at) openbsd.org>
6 * Copyright (c) 2007 Mark Kettenis <kettenis (at) openbsd.org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20
21 #include <sys/cdefs.h>
22 __KERNEL_RCSID(0, "$NetBSD: pcf8591_envctrl.c,v 1.9 2018/06/26 06:03:57 thorpej Exp $");
23
24 #include <sys/param.h>
25 #include <sys/systm.h>
26 #include <sys/device.h>
27
28 #include <dev/sysmon/sysmonvar.h>
29
30 #include <dev/ofw/openfirm.h>
31 #include <dev/i2c/i2cvar.h>
32
33 #define PCF8591_CHANNELS 4
34
35 #define PCF8591_CTRL_CH0 0x00
36 #define PCF8591_CTRL_CH1 0x01
37 #define PCF8591_CTRL_CH2 0x02
38 #define PCF8591_CTRL_CH3 0x03
39 #define PCF8591_CTRL_AUTOINC 0x04
40 #define PCF8591_CTRL_OSCILLATOR 0x40
41
42 struct ecadc_channel {
43 u_int chan_num;
44 envsys_data_t chan_sensor;
45 u_char *chan_xlate;
46 int64_t chan_factor;
47 int64_t chan_min;
48 int64_t chan_warn;
49 int64_t chan_crit;
50 };
51
52 struct ecadc_softc {
53 device_t sc_dev;
54 i2c_tag_t sc_tag;
55 i2c_addr_t sc_addr;
56 u_char sc_ps_xlate[256];
57 u_char sc_cpu_xlate[256];
58 u_int sc_nchan;
59 struct ecadc_channel sc_channels[PCF8591_CHANNELS];
60 struct sysmon_envsys *sc_sme;
61 };
62
63 static int ecadc_match(device_t, cfdata_t, void *);
64 static void ecadc_attach(device_t, device_t, void *);
65 static void ecadc_refresh(struct sysmon_envsys *, envsys_data_t *);
66 static void ecadc_get_limits(struct sysmon_envsys *, envsys_data_t *,
67 sysmon_envsys_lim_t *, uint32_t *);
68
69 CFATTACH_DECL_NEW(ecadc, sizeof(struct ecadc_softc),
70 ecadc_match, ecadc_attach, NULL, NULL);
71
72 static const struct device_compatible_entry compat_data[] = {
73 { "ecadc", 0 },
74 { NULL, 0 }
75 };
76
77 static int
78 ecadc_match(device_t parent, cfdata_t cf, void *aux)
79 {
80 struct i2c_attach_args *ia = aux;
81 int match_result;
82
83 if (iic_use_direct_match(ia, cf, compat_data, &match_result))
84 return match_result;
85
86 /* This driver is direct-config only. */
87
88 return 0;
89 }
90
91 static void
92 ecadc_attach(device_t parent, device_t self, void *aux)
93 {
94 struct i2c_attach_args *ia = aux;
95 struct ecadc_softc *sc = device_private(self);
96 u_char term[256];
97 u_char *cp, *desc;
98 int64_t minv, warnv, crit, num, den;
99 u_int8_t junk[PCF8591_CHANNELS + 1];
100 envsys_data_t *sensor;
101 int len, error, addr, chan, node = (int)ia->ia_cookie;
102 u_int i;
103
104 sc->sc_dev = self;
105 if ((len = OF_getprop(node, "thermisters", term,
106 sizeof(term))) < 0) {
107 aprint_error(": couldn't find \"thermisters\" property\n");
108 return;
109 }
110
111 if (OF_getprop(node, "cpu-temp-factors", &sc->sc_cpu_xlate[2],
112 sizeof(sc->sc_cpu_xlate) - 2) < 0) {
113 aprint_error(": couldn't find \"cpu-temp-factors\" property\n");
114 return;
115 }
116 sc->sc_cpu_xlate[0] = sc->sc_cpu_xlate[1] = sc->sc_cpu_xlate[2];
117
118 /* Only the Sun Enterprise 450 has these. */
119 OF_getprop(node, "ps-temp-factors", &sc->sc_ps_xlate[2],
120 sizeof(sc->sc_ps_xlate) - 2);
121 sc->sc_ps_xlate[0] = sc->sc_ps_xlate[1] = sc->sc_ps_xlate[2];
122
123 cp = term;
124 while (cp < term + len) {
125 addr = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
126 chan = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
127 minv = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
128 warnv = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
129 crit = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
130 num = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
131 den = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
132 desc = cp;
133 while (cp < term + len && *cp++);
134
135 if (addr != (ia->ia_addr << 1))
136 continue;
137
138 if (num == 0 || den == 0)
139 num = den = 1;
140
141 sc->sc_channels[sc->sc_nchan].chan_num = chan;
142
143 sensor = &sc->sc_channels[sc->sc_nchan].chan_sensor;
144 sensor->units = ENVSYS_STEMP;
145 sensor->flags |= ENVSYS_FMONLIMITS;
146 sensor->state = ENVSYS_SINVALID;
147 strlcpy(sensor->desc, desc, sizeof(sensor->desc));
148
149 if (strncmp(desc, "CPU", 3) == 0)
150 sc->sc_channels[sc->sc_nchan].chan_xlate =
151 sc->sc_cpu_xlate;
152 else if (strncmp(desc, "PS", 2) == 0)
153 sc->sc_channels[sc->sc_nchan].chan_xlate =
154 sc->sc_ps_xlate;
155 else
156 sc->sc_channels[sc->sc_nchan].chan_factor =
157 (1000000 * num) / den;
158 sc->sc_channels[sc->sc_nchan].chan_min =
159 273150000 + 1000000 * minv;
160 sc->sc_channels[sc->sc_nchan].chan_warn =
161 273150000 + 1000000 * warnv;
162 sc->sc_channels[sc->sc_nchan].chan_crit =
163 273150000 + 1000000 * crit;
164 sc->sc_nchan++;
165 }
166
167 sc->sc_tag = ia->ia_tag;
168 sc->sc_addr = ia->ia_addr;
169
170 iic_acquire_bus(sc->sc_tag, 0);
171
172 /* Try a read now, so we can fail if it doesn't work */
173 if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
174 NULL, 0, junk, sc->sc_nchan + 1, 0)) {
175 aprint_error(": read failed\n");
176 iic_release_bus(sc->sc_tag, 0);
177 return;
178 }
179
180 iic_release_bus(sc->sc_tag, 0);
181
182 /* Hook us into the sysmon_envsys subsystem */
183 sc->sc_sme = sysmon_envsys_create();
184 sc->sc_sme->sme_name = device_xname(self);
185 sc->sc_sme->sme_cookie = sc;
186 sc->sc_sme->sme_refresh = ecadc_refresh;
187 sc->sc_sme->sme_get_limits = ecadc_get_limits;
188
189 /* Initialize sensor data. */
190 for (i = 0; i < sc->sc_nchan; i++)
191 sysmon_envsys_sensor_attach(sc->sc_sme,
192 &sc->sc_channels[i].chan_sensor);
193
194 error = sysmon_envsys_register(sc->sc_sme);
195 if (error) {
196 aprint_error_dev(self, "error %d registering with sysmon\n",
197 error);
198 sysmon_envsys_destroy(sc->sc_sme);
199 return;
200 }
201
202 aprint_naive(": Temp Sensors\n");
203 aprint_normal(": %s Temp Sensors\n", ia->ia_name);
204 }
205
206 static void
207 ecadc_refresh(struct sysmon_envsys *sme, envsys_data_t *sensor)
208 {
209 struct ecadc_softc *sc = sme->sme_cookie;
210 u_int i;
211 u_int8_t data[PCF8591_CHANNELS + 1];
212 u_int8_t ctrl = PCF8591_CTRL_CH0 | PCF8591_CTRL_AUTOINC |
213 PCF8591_CTRL_OSCILLATOR;
214
215 iic_acquire_bus(sc->sc_tag, 0);
216 if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
217 &ctrl, 1, NULL, 0, 0)) {
218 iic_release_bus(sc->sc_tag, 0);
219 return;
220 }
221 /* NB: first byte out is stale, so read num_channels + 1 */
222 if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
223 NULL, 0, data, PCF8591_CHANNELS + 1, 0)) {
224 iic_release_bus(sc->sc_tag, 0);
225 return;
226 }
227 iic_release_bus(sc->sc_tag, 0);
228
229 /* We only support temperature channels. */
230 for (i = 0; i < sc->sc_nchan; i++) {
231 struct ecadc_channel *chp = &sc->sc_channels[i];
232
233 if (chp->chan_xlate)
234 chp->chan_sensor.value_cur = 273150000 + 1000000 *
235 chp->chan_xlate[data[1 + chp->chan_num]];
236 else
237 chp->chan_sensor.value_cur = 273150000 +
238 chp->chan_factor * data[1 + chp->chan_num];
239
240 chp->chan_sensor.state = ENVSYS_SVALID;
241 chp->chan_sensor.flags &= ~ENVSYS_FNEED_REFRESH;
242 chp->chan_sensor.flags |= ENVSYS_FMONLIMITS;
243 }
244 }
245
246 static void
247 ecadc_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
248 sysmon_envsys_lim_t *limits, uint32_t *props)
249 {
250 struct ecadc_softc *sc = sme->sme_cookie;
251 int i;
252
253 for (i = 0; i < sc->sc_nchan; i++) {
254 if (edata != &sc->sc_channels[i].chan_sensor)
255 continue;
256 *props |= PROP_WARNMIN|PROP_WARNMAX|PROP_CRITMAX;
257 limits->sel_warnmin = sc->sc_channels[i].chan_min;
258 limits->sel_warnmax = sc->sc_channels[i].chan_warn;
259 limits->sel_critmax = sc->sc_channels[i].chan_crit;
260 return;
261 }
262 }
263