pcf8591_envctrl.c revision 1.8 1 /* $NetBSD: pcf8591_envctrl.c,v 1.8 2018/06/18 17:07:07 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.8 2018/06/18 17:07:07 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 char * ecadc_compats[] = {
73 "ecadc",
74 NULL
75 };
76
77 static const struct device_compatible_entry ecadc_compat_data[] = {
78 DEVICE_COMPAT_ENTRY(ecadc_compats),
79 DEVICE_COMPAT_TERMINATOR
80 };
81
82 static int
83 ecadc_match(device_t parent, cfdata_t cf, void *aux)
84 {
85 struct i2c_attach_args *ia = aux;
86 int match_result;
87
88 if (iic_use_direct_match(ia, cf, ecadc_compat_data, &match_result))
89 return match_result;
90
91 /* This driver is direct-config only. */
92
93 return 0;
94 }
95
96 static void
97 ecadc_attach(device_t parent, device_t self, void *aux)
98 {
99 struct i2c_attach_args *ia = aux;
100 struct ecadc_softc *sc = device_private(self);
101 u_char term[256];
102 u_char *cp, *desc;
103 int64_t minv, warnv, crit, num, den;
104 u_int8_t junk[PCF8591_CHANNELS + 1];
105 envsys_data_t *sensor;
106 int len, error, addr, chan, node = (int)ia->ia_cookie;
107 u_int i;
108
109 sc->sc_dev = self;
110 if ((len = OF_getprop(node, "thermisters", term,
111 sizeof(term))) < 0) {
112 aprint_error(": couldn't find \"thermisters\" property\n");
113 return;
114 }
115
116 if (OF_getprop(node, "cpu-temp-factors", &sc->sc_cpu_xlate[2],
117 sizeof(sc->sc_cpu_xlate) - 2) < 0) {
118 aprint_error(": couldn't find \"cpu-temp-factors\" property\n");
119 return;
120 }
121 sc->sc_cpu_xlate[0] = sc->sc_cpu_xlate[1] = sc->sc_cpu_xlate[2];
122
123 /* Only the Sun Enterprise 450 has these. */
124 OF_getprop(node, "ps-temp-factors", &sc->sc_ps_xlate[2],
125 sizeof(sc->sc_ps_xlate) - 2);
126 sc->sc_ps_xlate[0] = sc->sc_ps_xlate[1] = sc->sc_ps_xlate[2];
127
128 cp = term;
129 while (cp < term + len) {
130 addr = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
131 chan = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
132 minv = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
133 warnv = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
134 crit = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
135 num = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
136 den = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
137 desc = cp;
138 while (cp < term + len && *cp++);
139
140 if (addr != (ia->ia_addr << 1))
141 continue;
142
143 if (num == 0 || den == 0)
144 num = den = 1;
145
146 sc->sc_channels[sc->sc_nchan].chan_num = chan;
147
148 sensor = &sc->sc_channels[sc->sc_nchan].chan_sensor;
149 sensor->units = ENVSYS_STEMP;
150 sensor->flags |= ENVSYS_FMONLIMITS;
151 sensor->state = ENVSYS_SINVALID;
152 strlcpy(sensor->desc, desc, sizeof(sensor->desc));
153
154 if (strncmp(desc, "CPU", 3) == 0)
155 sc->sc_channels[sc->sc_nchan].chan_xlate =
156 sc->sc_cpu_xlate;
157 else if (strncmp(desc, "PS", 2) == 0)
158 sc->sc_channels[sc->sc_nchan].chan_xlate =
159 sc->sc_ps_xlate;
160 else
161 sc->sc_channels[sc->sc_nchan].chan_factor =
162 (1000000 * num) / den;
163 sc->sc_channels[sc->sc_nchan].chan_min =
164 273150000 + 1000000 * minv;
165 sc->sc_channels[sc->sc_nchan].chan_warn =
166 273150000 + 1000000 * warnv;
167 sc->sc_channels[sc->sc_nchan].chan_crit =
168 273150000 + 1000000 * crit;
169 sc->sc_nchan++;
170 }
171
172 sc->sc_tag = ia->ia_tag;
173 sc->sc_addr = ia->ia_addr;
174
175 iic_acquire_bus(sc->sc_tag, 0);
176
177 /* Try a read now, so we can fail if it doesn't work */
178 if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
179 NULL, 0, junk, sc->sc_nchan + 1, 0)) {
180 aprint_error(": read failed\n");
181 iic_release_bus(sc->sc_tag, 0);
182 return;
183 }
184
185 iic_release_bus(sc->sc_tag, 0);
186
187 /* Hook us into the sysmon_envsys subsystem */
188 sc->sc_sme = sysmon_envsys_create();
189 sc->sc_sme->sme_name = device_xname(self);
190 sc->sc_sme->sme_cookie = sc;
191 sc->sc_sme->sme_refresh = ecadc_refresh;
192 sc->sc_sme->sme_get_limits = ecadc_get_limits;
193
194 /* Initialize sensor data. */
195 for (i = 0; i < sc->sc_nchan; i++)
196 sysmon_envsys_sensor_attach(sc->sc_sme,
197 &sc->sc_channels[i].chan_sensor);
198
199 error = sysmon_envsys_register(sc->sc_sme);
200 if (error) {
201 aprint_error_dev(self, "error %d registering with sysmon\n",
202 error);
203 sysmon_envsys_destroy(sc->sc_sme);
204 return;
205 }
206
207 aprint_naive(": Temp Sensors\n");
208 aprint_normal(": %s Temp Sensors\n", ia->ia_name);
209 }
210
211 static void
212 ecadc_refresh(struct sysmon_envsys *sme, envsys_data_t *sensor)
213 {
214 struct ecadc_softc *sc = sme->sme_cookie;
215 u_int i;
216 u_int8_t data[PCF8591_CHANNELS + 1];
217 u_int8_t ctrl = PCF8591_CTRL_CH0 | PCF8591_CTRL_AUTOINC |
218 PCF8591_CTRL_OSCILLATOR;
219
220 iic_acquire_bus(sc->sc_tag, 0);
221 if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
222 &ctrl, 1, NULL, 0, 0)) {
223 iic_release_bus(sc->sc_tag, 0);
224 return;
225 }
226 /* NB: first byte out is stale, so read num_channels + 1 */
227 if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
228 NULL, 0, data, PCF8591_CHANNELS + 1, 0)) {
229 iic_release_bus(sc->sc_tag, 0);
230 return;
231 }
232 iic_release_bus(sc->sc_tag, 0);
233
234 /* We only support temperature channels. */
235 for (i = 0; i < sc->sc_nchan; i++) {
236 struct ecadc_channel *chp = &sc->sc_channels[i];
237
238 if (chp->chan_xlate)
239 chp->chan_sensor.value_cur = 273150000 + 1000000 *
240 chp->chan_xlate[data[1 + chp->chan_num]];
241 else
242 chp->chan_sensor.value_cur = 273150000 +
243 chp->chan_factor * data[1 + chp->chan_num];
244
245 chp->chan_sensor.state = ENVSYS_SVALID;
246 chp->chan_sensor.flags &= ~ENVSYS_FNEED_REFRESH;
247 chp->chan_sensor.flags |= ENVSYS_FMONLIMITS;
248 }
249 }
250
251 static void
252 ecadc_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
253 sysmon_envsys_lim_t *limits, uint32_t *props)
254 {
255 struct ecadc_softc *sc = sme->sme_cookie;
256 int i;
257
258 for (i = 0; i < sc->sc_nchan; i++) {
259 if (edata != &sc->sc_channels[i].chan_sensor)
260 continue;
261 *props |= PROP_WARNMIN|PROP_WARNMAX|PROP_CRITMAX;
262 limits->sel_warnmin = sc->sc_channels[i].chan_min;
263 limits->sel_warnmax = sc->sc_channels[i].chan_warn;
264 limits->sel_critmax = sc->sc_channels[i].chan_crit;
265 return;
266 }
267 }
268