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