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