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