xref: /netbsd-src/sys/arch/sparc64/dev/pcf8591_envctrl.c (revision ec189949006ea3661186ac8a565a91b130219ef0)
1*ec189949Sthorpej /*	$NetBSD: pcf8591_envctrl.c,v 1.19 2021/01/27 02:20:03 thorpej Exp $	*/
2f0ca32ddSmartin /*	$OpenBSD: pcf8591_envctrl.c,v 1.6 2007/10/25 21:17:20 kettenis Exp $ */
3f0ca32ddSmartin 
4f0ca32ddSmartin /*
5f0ca32ddSmartin  * Copyright (c) 2006 Damien Miller <djm@openbsd.org>
6f0ca32ddSmartin  * Copyright (c) 2007 Mark Kettenis <kettenis@openbsd.org>
7f0ca32ddSmartin  *
8f0ca32ddSmartin  * Permission to use, copy, modify, and distribute this software for any
9f0ca32ddSmartin  * purpose with or without fee is hereby granted, provided that the above
10f0ca32ddSmartin  * copyright notice and this permission notice appear in all copies.
11f0ca32ddSmartin  *
12f0ca32ddSmartin  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13f0ca32ddSmartin  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14f0ca32ddSmartin  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15f0ca32ddSmartin  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16f0ca32ddSmartin  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17f0ca32ddSmartin  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18f0ca32ddSmartin  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19f0ca32ddSmartin  */
20f0ca32ddSmartin 
21f72d1cdaSmrg #include <sys/cdefs.h>
22*ec189949Sthorpej __KERNEL_RCSID(0, "$NetBSD: pcf8591_envctrl.c,v 1.19 2021/01/27 02:20:03 thorpej Exp $");
23f72d1cdaSmrg 
24f0ca32ddSmartin #include <sys/param.h>
25f0ca32ddSmartin #include <sys/systm.h>
26d90581adSjdc #include <sys/kernel.h>
27f0ca32ddSmartin #include <sys/device.h>
28f0ca32ddSmartin 
29f0ca32ddSmartin #include <dev/sysmon/sysmonvar.h>
30d90581adSjdc #include <dev/sysmon/sysmon_taskq.h>
31d90581adSjdc 
32d90581adSjdc #include <machine/autoconf.h>
33f0ca32ddSmartin 
34f0ca32ddSmartin #include <dev/ofw/openfirm.h>
35f0ca32ddSmartin #include <dev/i2c/i2cvar.h>
36f0ca32ddSmartin 
37fd3cc354Sjdc #ifdef ECADC_DEBUG
38fd3cc354Sjdc #define DPRINTF printf
39fd3cc354Sjdc #else
40fd3cc354Sjdc #define DPRINTF if (0) printf
41fd3cc354Sjdc #endif
42fd3cc354Sjdc 
43d90581adSjdc /* Translation tables contain 254 entries */
44d90581adSjdc #define XLATE_SIZE		256
45d90581adSjdc #define XLATE_MAX		(XLATE_SIZE - 2)
46d90581adSjdc 
47f0ca32ddSmartin #define PCF8591_CHANNELS	4
48f0ca32ddSmartin 
49f0ca32ddSmartin #define PCF8591_CTRL_CH0	0x00
50f0ca32ddSmartin #define PCF8591_CTRL_CH1	0x01
51f0ca32ddSmartin #define PCF8591_CTRL_CH2	0x02
52f0ca32ddSmartin #define PCF8591_CTRL_CH3	0x03
53f0ca32ddSmartin #define PCF8591_CTRL_AUTOINC	0x04
54f0ca32ddSmartin #define PCF8591_CTRL_OSCILLATOR	0x40
55f0ca32ddSmartin 
56d90581adSjdc #define PCF8591_TEMP_SENS	0x00
579bb301c6Sjdc #define PCF8591_SYS_FAN_CTRL	0x01
58d90581adSjdc 
59f0ca32ddSmartin struct ecadc_channel {
60f0ca32ddSmartin 	u_int		chan_num;
61d90581adSjdc 	u_int		chan_type;
62f0ca32ddSmartin 	envsys_data_t	chan_sensor;
63f0ca32ddSmartin 	u_char		*chan_xlate;
64f0ca32ddSmartin 	int64_t		chan_factor;
65f0ca32ddSmartin 	int64_t		chan_min;
66f0ca32ddSmartin 	int64_t		chan_warn;
67f0ca32ddSmartin 	int64_t		chan_crit;
68d90581adSjdc 	u_int8_t	chan_speed;
69f0ca32ddSmartin };
70f0ca32ddSmartin 
71f0ca32ddSmartin struct ecadc_softc {
72f0ca32ddSmartin 	device_t		sc_dev;
73f0ca32ddSmartin 	i2c_tag_t		sc_tag;
74f0ca32ddSmartin 	i2c_addr_t		sc_addr;
75d90581adSjdc 	u_char			sc_ps_xlate[XLATE_SIZE];
76d90581adSjdc 	u_char			sc_cpu_xlate[XLATE_SIZE];
77d90581adSjdc 	u_char			sc_cpu_fan_spd[XLATE_SIZE];
78f0ca32ddSmartin 	u_int			sc_nchan;
79f0ca32ddSmartin 	struct ecadc_channel	sc_channels[PCF8591_CHANNELS];
80f0ca32ddSmartin 	struct sysmon_envsys	*sc_sme;
81d90581adSjdc 	int			sc_hastimer;
82d90581adSjdc 	callout_t		sc_timer;
83f0ca32ddSmartin };
84f0ca32ddSmartin 
85f0ca32ddSmartin static int	ecadc_match(device_t, cfdata_t, void *);
86f0ca32ddSmartin static void	ecadc_attach(device_t, device_t, void *);
87d90581adSjdc static int	ecadc_detach(device_t, int);
88f0ca32ddSmartin static void	ecadc_refresh(struct sysmon_envsys *, envsys_data_t *);
89f0ca32ddSmartin static void	ecadc_get_limits(struct sysmon_envsys *, envsys_data_t *,
90d90581adSjdc 			sysmon_envsys_lim_t *, u_int32_t *);
910ec0e425Sjdc static int	ecadc_set_fan_speed(struct ecadc_softc *, u_int8_t, u_int8_t);
92d90581adSjdc static void	ecadc_timeout(void *);
93d90581adSjdc static void	ecadc_fan_adjust(void *);
94f0ca32ddSmartin 
95d90581adSjdc CFATTACH_DECL3_NEW(ecadc, sizeof(struct ecadc_softc),
96d90581adSjdc 	ecadc_match, ecadc_attach, ecadc_detach, NULL, NULL, NULL,
97d90581adSjdc 	DVF_DETACH_SHUTDOWN);
98f0ca32ddSmartin 
99feee3a19Sthorpej static const struct device_compatible_entry compat_data[] = {
10056caee62Sthorpej 	{ .compat = "ecadc" },
101*ec189949Sthorpej 	DEVICE_COMPAT_EOL
1024f9e5a44Sthorpej };
1034f9e5a44Sthorpej 
104f0ca32ddSmartin static int
ecadc_match(device_t parent,cfdata_t cf,void * aux)105f0ca32ddSmartin ecadc_match(device_t parent, cfdata_t cf, void *aux)
106f0ca32ddSmartin {
107f0ca32ddSmartin 	struct i2c_attach_args *ia = aux;
108aa41e992Sthorpej 	int match_result;
109f0ca32ddSmartin 
110ca0f291dSjdc 	if (iic_use_direct_match(ia, cf, compat_data, &match_result))
111aa41e992Sthorpej 		return match_result;
112aa41e992Sthorpej 
113aa41e992Sthorpej 	/* This driver is direct-config only. */
114f0ca32ddSmartin 
115f0ca32ddSmartin 	return 0;
116f0ca32ddSmartin }
117f0ca32ddSmartin 
118f0ca32ddSmartin static void
ecadc_attach(device_t parent,device_t self,void * aux)119f0ca32ddSmartin ecadc_attach(device_t parent, device_t self, void *aux)
120f0ca32ddSmartin {
121f0ca32ddSmartin 	struct i2c_attach_args *ia = aux;
122f0ca32ddSmartin 	struct ecadc_softc *sc = device_private(self);
123f0ca32ddSmartin 	u_char term[256];
124f0ca32ddSmartin 	u_char *cp, *desc;
125f0ca32ddSmartin 	int64_t minv, warnv, crit, num, den;
126f0ca32ddSmartin 	u_int8_t junk[PCF8591_CHANNELS + 1];
127f0ca32ddSmartin 	envsys_data_t *sensor;
128f0ca32ddSmartin 	int len, error, addr, chan, node = (int)ia->ia_cookie;
129f0ca32ddSmartin 	u_int i;
130f0ca32ddSmartin 
131f0ca32ddSmartin 	sc->sc_dev = self;
132d90581adSjdc 	sc->sc_nchan = 0;
133d90581adSjdc 	sc->sc_hastimer = 0;
134d90581adSjdc 
135fd3cc354Sjdc 	DPRINTF("\n");
136f0ca32ddSmartin 	if ((len = OF_getprop(node, "thermisters", term,
137f0ca32ddSmartin 	    sizeof(term))) < 0) {
138f0ca32ddSmartin 		aprint_error(": couldn't find \"thermisters\" property\n");
139f0ca32ddSmartin 		return;
140f0ca32ddSmartin 	}
141f0ca32ddSmartin 
142f0ca32ddSmartin 	if (OF_getprop(node, "cpu-temp-factors", &sc->sc_cpu_xlate[2],
143d90581adSjdc 	    XLATE_MAX) < 0) {
144f0ca32ddSmartin 		aprint_error(": couldn't find \"cpu-temp-factors\" property\n");
145f0ca32ddSmartin 		return;
146f0ca32ddSmartin 	}
147f0ca32ddSmartin 	sc->sc_cpu_xlate[0] = sc->sc_cpu_xlate[1] = sc->sc_cpu_xlate[2];
148f0ca32ddSmartin 
149f0ca32ddSmartin 	/* Only the Sun Enterprise 450 has these. */
150d90581adSjdc 	OF_getprop(node, "ps-temp-factors", &sc->sc_ps_xlate[2], XLATE_MAX);
151f0ca32ddSmartin 	sc->sc_ps_xlate[0] = sc->sc_ps_xlate[1] = sc->sc_ps_xlate[2];
152f0ca32ddSmartin 
153f0ca32ddSmartin 	cp = term;
154f0ca32ddSmartin 	while (cp < term + len) {
155f0ca32ddSmartin 		addr = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
156f0ca32ddSmartin 		chan = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
157f0ca32ddSmartin 		minv = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
158f0ca32ddSmartin 		warnv = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
159f0ca32ddSmartin 		crit = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
160f0ca32ddSmartin 		num = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
161f0ca32ddSmartin 		den = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
162f0ca32ddSmartin 		desc = cp;
163f0ca32ddSmartin 		while (cp < term + len && *cp++);
164f0ca32ddSmartin 
165f0ca32ddSmartin 		if (addr != (ia->ia_addr << 1))
166f0ca32ddSmartin 			continue;
167f0ca32ddSmartin 
168f0ca32ddSmartin 		if (num == 0 || den == 0)
169f0ca32ddSmartin 			num = den = 1;
170f0ca32ddSmartin 
171f0ca32ddSmartin 		sc->sc_channels[sc->sc_nchan].chan_num = chan;
172d90581adSjdc 		sc->sc_channels[sc->sc_nchan].chan_type = PCF8591_TEMP_SENS;
173f0ca32ddSmartin 
174f0ca32ddSmartin 		sensor = &sc->sc_channels[sc->sc_nchan].chan_sensor;
175f0ca32ddSmartin 		sensor->units = ENVSYS_STEMP;
176e549a2a1Sjdc 		sensor->flags |= ENVSYS_FMONLIMITS;
177d9b90a67Spgoyette 		sensor->state = ENVSYS_SINVALID;
178f0ca32ddSmartin 		strlcpy(sensor->desc, desc, sizeof(sensor->desc));
179f0ca32ddSmartin 
180fd3cc354Sjdc 		if (strncmp(desc, "CPU", 3) == 0) {
181f0ca32ddSmartin 			sc->sc_channels[sc->sc_nchan].chan_xlate =
182f0ca32ddSmartin 			    sc->sc_cpu_xlate;
183fd3cc354Sjdc 			DPRINTF("%s: "
184fd3cc354Sjdc 			    "added %s sensor (chan %d) with cpu_xlate\n",
185fd3cc354Sjdc 			    device_xname(sc->sc_dev), desc, chan);
186fd3cc354Sjdc 		} else if (strncmp(desc, "PS", 2) == 0) {
187f0ca32ddSmartin 			sc->sc_channels[sc->sc_nchan].chan_xlate =
188f0ca32ddSmartin 			    sc->sc_ps_xlate;
189fd3cc354Sjdc 			DPRINTF("%s: "
190fd3cc354Sjdc 			    "added %s sensor (chan %d) with ps_xlate\n",
191fd3cc354Sjdc 			    device_xname(sc->sc_dev), desc, chan);
192fd3cc354Sjdc 		} else {
193f0ca32ddSmartin 			sc->sc_channels[sc->sc_nchan].chan_factor =
194f0ca32ddSmartin 			    (1000000 * num) / den;
195fd3cc354Sjdc 			DPRINTF("%s: "
196fd3cc354Sjdc 			    "added %s sensor (chan %d) without xlate\n",
197fd3cc354Sjdc 			    device_xname(sc->sc_dev), desc, chan);
198fd3cc354Sjdc 		}
199f0ca32ddSmartin 		sc->sc_channels[sc->sc_nchan].chan_min =
200f0ca32ddSmartin 		    273150000 + 1000000 * minv;
201f0ca32ddSmartin 		sc->sc_channels[sc->sc_nchan].chan_warn =
202f0ca32ddSmartin 		    273150000 + 1000000 * warnv;
203f0ca32ddSmartin 		sc->sc_channels[sc->sc_nchan].chan_crit =
204f0ca32ddSmartin 		    273150000 + 1000000 * crit;
205f0ca32ddSmartin 		sc->sc_nchan++;
206f0ca32ddSmartin 	}
207f0ca32ddSmartin 
208f0ca32ddSmartin 	sc->sc_tag = ia->ia_tag;
209f0ca32ddSmartin 	sc->sc_addr = ia->ia_addr;
210f0ca32ddSmartin 
211f0ca32ddSmartin 	iic_acquire_bus(sc->sc_tag, 0);
212f0ca32ddSmartin 
213ca0f291dSjdc 	/* Try a read now, so we can fail if this component isn't present */
214f0ca32ddSmartin 	if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
215f0ca32ddSmartin 	    NULL, 0, junk, sc->sc_nchan + 1, 0)) {
216ca0f291dSjdc 		aprint_normal(": read failed\n");
217f0ca32ddSmartin 		iic_release_bus(sc->sc_tag, 0);
218f0ca32ddSmartin 		return;
219f0ca32ddSmartin 	}
220f0ca32ddSmartin 
221f0ca32ddSmartin 	iic_release_bus(sc->sc_tag, 0);
222f0ca32ddSmartin 
2230ec0e425Sjdc 	/*
2240ec0e425Sjdc 	 * Fan speed changing information is missing from OFW
2250ec0e425Sjdc 	 * The E250 CPU fan is connected to the sensor at addr 0x4a, channel 1
2260ec0e425Sjdc 	 */
2270ec0e425Sjdc 	if (ia->ia_addr == 0x4a && !strcmp(machine_model, "SUNW,Ultra-250") &&
2280ec0e425Sjdc 	    OF_getprop(node, "cpu-fan-speeds", &sc->sc_cpu_fan_spd,
2290ec0e425Sjdc 	    XLATE_MAX) > 0) {
2300ec0e425Sjdc 		sc->sc_channels[sc->sc_nchan].chan_num = 1;
2319bb301c6Sjdc 		sc->sc_channels[sc->sc_nchan].chan_type = PCF8591_SYS_FAN_CTRL;
2320ec0e425Sjdc 		sensor = &sc->sc_channels[sc->sc_nchan].chan_sensor;
2330ec0e425Sjdc 		sensor->units = ENVSYS_INTEGER;
2340ec0e425Sjdc 		sensor->flags = ENVSYS_FMONNOTSUPP;
2350ec0e425Sjdc 		sensor->state = ENVSYS_SINVALID;
2369bb301c6Sjdc 		strlcpy(sensor->desc, "SYSFAN", sizeof(sensor->desc));
2370ec0e425Sjdc 		sc->sc_channels[sc->sc_nchan].chan_xlate = sc->sc_cpu_fan_spd;
2380ec0e425Sjdc 		DPRINTF("%s: "
2390ec0e425Sjdc 		    "added CPUFAN sensor (chan %d) with cpu-fan xlate\n",
2400ec0e425Sjdc 		    device_xname(sc->sc_dev),
2410ec0e425Sjdc 		    sc->sc_channels[sc->sc_nchan].chan_num);
2420ec0e425Sjdc 
2430ec0e425Sjdc 		/* Set the fan to medium speed */
2440ec0e425Sjdc 		sc->sc_channels[sc->sc_nchan].chan_speed =
2450ec0e425Sjdc 		    (sc->sc_cpu_fan_spd[0]+sc->sc_cpu_fan_spd[XLATE_MAX])/2;
2460ec0e425Sjdc 		ecadc_set_fan_speed(sc, sc->sc_channels[sc->sc_nchan].chan_num,
2470ec0e425Sjdc 		    sc->sc_channels[sc->sc_nchan].chan_speed);
2480ec0e425Sjdc 
2490ec0e425Sjdc 		sc->sc_nchan++;
2500ec0e425Sjdc 		sc->sc_hastimer = 1;
2510ec0e425Sjdc 	}
2520ec0e425Sjdc 
253f0ca32ddSmartin 	/* Hook us into the sysmon_envsys subsystem */
254f0ca32ddSmartin 	sc->sc_sme = sysmon_envsys_create();
255f0ca32ddSmartin 	sc->sc_sme->sme_name = device_xname(self);
256f0ca32ddSmartin 	sc->sc_sme->sme_cookie = sc;
257f0ca32ddSmartin 	sc->sc_sme->sme_refresh = ecadc_refresh;
258f0ca32ddSmartin 	sc->sc_sme->sme_get_limits = ecadc_get_limits;
259f0ca32ddSmartin 
260f0ca32ddSmartin 	/* Initialize sensor data. */
261f0ca32ddSmartin 	for (i = 0; i < sc->sc_nchan; i++)
262f0ca32ddSmartin 		sysmon_envsys_sensor_attach(sc->sc_sme,
263f0ca32ddSmartin 		    &sc->sc_channels[i].chan_sensor);
264f0ca32ddSmartin 
265f0ca32ddSmartin 	error = sysmon_envsys_register(sc->sc_sme);
266f0ca32ddSmartin 	if (error) {
267f0ca32ddSmartin 		aprint_error_dev(self, "error %d registering with sysmon\n",
268f0ca32ddSmartin 			error);
269f0ca32ddSmartin 		sysmon_envsys_destroy(sc->sc_sme);
270ca0f291dSjdc 		sc->sc_sme = NULL;
271f0ca32ddSmartin 		return;
272f0ca32ddSmartin 	}
273f0ca32ddSmartin 
274d90581adSjdc 	if (sc->sc_hastimer) {
275d90581adSjdc 		callout_init(&sc->sc_timer, CALLOUT_MPSAFE);
276d90581adSjdc 		callout_reset(&sc->sc_timer, hz*20, ecadc_timeout, sc);
277d90581adSjdc 	}
278d90581adSjdc 
279f0ca32ddSmartin 	aprint_naive(": Temp Sensors\n");
280d90581adSjdc 	aprint_normal(": %s Temp Sensors (%d channels)\n", ia->ia_name,
281d90581adSjdc 	    sc->sc_nchan);
282d90581adSjdc }
283d90581adSjdc 
284d90581adSjdc static int
ecadc_detach(device_t self,int flags)285d90581adSjdc ecadc_detach(device_t self, int flags)
286d90581adSjdc {
287d90581adSjdc 	struct ecadc_softc *sc = device_private(self);
2880ec0e425Sjdc 	int c, i;
2890ec0e425Sjdc 
290d90581adSjdc 	if (sc->sc_hastimer) {
291d90581adSjdc 		callout_halt(&sc->sc_timer, NULL);
292d90581adSjdc 		callout_destroy(&sc->sc_timer);
293d90581adSjdc 	}
294d90581adSjdc 
295d90581adSjdc 	if (sc->sc_sme != NULL)
296f74360b9Sjdc 		sysmon_envsys_unregister(sc->sc_sme);
297d90581adSjdc 
2980ec0e425Sjdc 	for (i = 0; i < sc->sc_nchan; i++) {
2990ec0e425Sjdc 		struct ecadc_channel *chp = &sc->sc_channels[i];
3000ec0e425Sjdc 
3019bb301c6Sjdc 		if (chp->chan_type == PCF8591_SYS_FAN_CTRL) {
3020ec0e425Sjdc 			/* Loop in case the bus is busy */
3030ec0e425Sjdc 			for (c = 0; c < 5; c++) {
3040ec0e425Sjdc 				chp->chan_speed = sc->sc_cpu_fan_spd[0];
3050ec0e425Sjdc 				if (!ecadc_set_fan_speed(sc, chp->chan_num,
3060ec0e425Sjdc 				    chp->chan_speed))
3070ec0e425Sjdc 					return 0;
3080ec0e425Sjdc 				delay(10000);
3090ec0e425Sjdc 			}
3109bb301c6Sjdc 			printf("%s: cannot set fan speed (chan %d)\n",
3119bb301c6Sjdc 			    device_xname(sc->sc_dev), chp->chan_num);
3120ec0e425Sjdc 		}
3130ec0e425Sjdc 	}
3140ec0e425Sjdc 
315d90581adSjdc 	return 0;
316f0ca32ddSmartin }
317f0ca32ddSmartin 
318f0ca32ddSmartin static void
ecadc_refresh(struct sysmon_envsys * sme,envsys_data_t * sensor)319f0ca32ddSmartin ecadc_refresh(struct sysmon_envsys *sme, envsys_data_t *sensor)
320f0ca32ddSmartin {
321f0ca32ddSmartin 	struct ecadc_softc *sc = sme->sme_cookie;
322f0ca32ddSmartin 	u_int i;
323f0ca32ddSmartin 	u_int8_t data[PCF8591_CHANNELS + 1];
324f0ca32ddSmartin 	u_int8_t ctrl = PCF8591_CTRL_CH0 | PCF8591_CTRL_AUTOINC |
325f0ca32ddSmartin 	    PCF8591_CTRL_OSCILLATOR;
326f0ca32ddSmartin 
327ca0f291dSjdc 	if (iic_acquire_bus(sc->sc_tag, 0))
328ca0f291dSjdc 		return;
329f0ca32ddSmartin 	if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
330f0ca32ddSmartin 	    &ctrl, 1, NULL, 0, 0)) {
331f0ca32ddSmartin 		iic_release_bus(sc->sc_tag, 0);
332f0ca32ddSmartin 		return;
333f0ca32ddSmartin 	}
334d90581adSjdc 	/*
335d90581adSjdc 	 * Each data byte that we read is the result of the previous request,
336d90581adSjdc 	 * so read num_channels + 1 and update envsys values from chan + 1.
337d90581adSjdc 	 */
338f0ca32ddSmartin 	if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
339f0ca32ddSmartin 	    NULL, 0, data, PCF8591_CHANNELS + 1, 0)) {
340f0ca32ddSmartin 		iic_release_bus(sc->sc_tag, 0);
341f0ca32ddSmartin 		return;
342f0ca32ddSmartin 	}
343f0ca32ddSmartin 	iic_release_bus(sc->sc_tag, 0);
344f0ca32ddSmartin 
345d90581adSjdc 	/* Temperature with/without translation or relative (ADC value) */
346f0ca32ddSmartin 	for (i = 0; i < sc->sc_nchan; i++) {
347f0ca32ddSmartin 		struct ecadc_channel *chp = &sc->sc_channels[i];
348f0ca32ddSmartin 
349d90581adSjdc 		if (chp->chan_type == PCF8591_TEMP_SENS) {
350d90581adSjdc 
351d90581adSjdc 			/* Encode the raw value to use for the fan control */
352d90581adSjdc 			if (chp->chan_xlate) {
353d90581adSjdc 				int32_t temp;
354d90581adSjdc 
355d90581adSjdc 				temp = 273150000 + 1000000 *
356f0ca32ddSmartin 				    chp->chan_xlate[data[1 + chp->chan_num]];
357d90581adSjdc 				temp &= ~0xff;
358d90581adSjdc 				temp += data[1 + chp->chan_num];
359d90581adSjdc 				chp->chan_sensor.value_cur = temp;
360fd3cc354Sjdc 				DPRINTF("%s: xlate %s sensor = %d"
361fd3cc354Sjdc 				    " (0x%x > 0x%x)\n",
362fd3cc354Sjdc 				    device_xname(sc->sc_dev),
363fd3cc354Sjdc 				    chp->chan_sensor.desc, temp,
364fd3cc354Sjdc 				    data[1 + chp->chan_num],
365fd3cc354Sjdc 				    chp->chan_xlate[data[1 + chp->chan_num]]);
366fd3cc354Sjdc 			} else {
367f0ca32ddSmartin 				chp->chan_sensor.value_cur = 273150000 +
368f0ca32ddSmartin 				    chp->chan_factor * data[1 + chp->chan_num];
369fd3cc354Sjdc 				DPRINTF("%s: read %s sensor = %d (0x%x)\n",
370fd3cc354Sjdc 				    device_xname(sc->sc_dev),
371fd3cc354Sjdc 				    chp->chan_sensor.desc,
372fd3cc354Sjdc 				    chp->chan_sensor.value_cur,
373fd3cc354Sjdc 				    data[1 + chp->chan_num]);
374fd3cc354Sjdc 			}
375d90581adSjdc 			chp->chan_sensor.flags |= ENVSYS_FMONLIMITS;
376d90581adSjdc 		}
3779bb301c6Sjdc 		if (chp->chan_type == PCF8591_SYS_FAN_CTRL)
378d90581adSjdc 			chp->chan_sensor.value_cur = data[1 + chp->chan_num];
379f0ca32ddSmartin 
380f0ca32ddSmartin 		chp->chan_sensor.state = ENVSYS_SVALID;
381f0ca32ddSmartin 	}
382f0ca32ddSmartin }
383f0ca32ddSmartin 
384f0ca32ddSmartin static void
ecadc_get_limits(struct sysmon_envsys * sme,envsys_data_t * edata,sysmon_envsys_lim_t * limits,u_int32_t * props)385f0ca32ddSmartin ecadc_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
386d90581adSjdc 		sysmon_envsys_lim_t *limits, u_int32_t *props)
387f0ca32ddSmartin {
388f0ca32ddSmartin 	struct ecadc_softc *sc = sme->sme_cookie;
389f0ca32ddSmartin 	int i;
390f0ca32ddSmartin 
391f0ca32ddSmartin 	for (i = 0; i < sc->sc_nchan; i++) {
392f0ca32ddSmartin 		if (edata != &sc->sc_channels[i].chan_sensor)
393f0ca32ddSmartin 			continue;
394d90581adSjdc 		if (sc->sc_channels[i].chan_type == PCF8591_TEMP_SENS) {
395f0ca32ddSmartin 			*props |= PROP_WARNMIN|PROP_WARNMAX|PROP_CRITMAX;
396f0ca32ddSmartin 			limits->sel_warnmin = sc->sc_channels[i].chan_min;
397f0ca32ddSmartin 			limits->sel_warnmax = sc->sc_channels[i].chan_warn;
398f0ca32ddSmartin 			limits->sel_critmax = sc->sc_channels[i].chan_crit;
399d90581adSjdc 		}
400f0ca32ddSmartin 		return;
401f0ca32ddSmartin 	}
402f0ca32ddSmartin }
403d90581adSjdc 
404d90581adSjdc static void
ecadc_timeout(void * v)405d90581adSjdc ecadc_timeout(void *v)
406d90581adSjdc {
407d90581adSjdc 	struct ecadc_softc *sc = v;
408d90581adSjdc 
409d90581adSjdc 	sysmon_task_queue_sched(0, ecadc_fan_adjust, sc);
410d90581adSjdc 	callout_reset(&sc->sc_timer, hz*60, ecadc_timeout, sc);
411d90581adSjdc }
412d90581adSjdc 
413d90581adSjdc static bool
is_cpu_temp(const envsys_data_t * edata)414d90581adSjdc is_cpu_temp(const envsys_data_t *edata)
415d90581adSjdc {
416d90581adSjdc 	if (edata->units != ENVSYS_STEMP)
417d90581adSjdc 		return false;
418d90581adSjdc 	return strncmp(edata->desc, "CPU", 3) == 0;
419d90581adSjdc }
420d90581adSjdc 
4219bb301c6Sjdc static bool
is_high_temp(const envsys_data_t * edata)4229bb301c6Sjdc is_high_temp(const envsys_data_t *edata)
4239bb301c6Sjdc {
4249bb301c6Sjdc 	if (edata->units != ENVSYS_INDICATOR)
4259bb301c6Sjdc 		return false;
4269bb301c6Sjdc 	return strcmp(edata->desc, "high_temp") == 0;
4279bb301c6Sjdc }
4289bb301c6Sjdc 
4299bb301c6Sjdc static bool
is_fan_fail(const envsys_data_t * edata)4309bb301c6Sjdc is_fan_fail(const envsys_data_t *edata)
4319bb301c6Sjdc {
4329bb301c6Sjdc 	if (edata->units != ENVSYS_INDICATOR)
4339bb301c6Sjdc 		return false;
4349bb301c6Sjdc 	return strcmp(edata->desc, "fan_fail") == 0;
4359bb301c6Sjdc }
4369bb301c6Sjdc 
437d90581adSjdc static int
ecadc_set_fan_speed(struct ecadc_softc * sc,u_int8_t chan,u_int8_t val)438d90581adSjdc ecadc_set_fan_speed(struct ecadc_softc *sc, u_int8_t chan, u_int8_t val)
439d90581adSjdc {
440d90581adSjdc 	u_int8_t ctrl = PCF8591_CTRL_AUTOINC | PCF8591_CTRL_OSCILLATOR;
441d90581adSjdc 	int ret;
442d90581adSjdc 
443d90581adSjdc 	ctrl |= chan;
444ca0f291dSjdc 	ret = iic_acquire_bus(sc->sc_tag, 0);
445ca0f291dSjdc 	if (ret) {
4469bb301c6Sjdc 		printf("%s: error acquiring i2c bus (ch %d)\n",
4479bb301c6Sjdc 		    device_xname(sc->sc_dev), chan);
448ca0f291dSjdc 		return ret;
449ca0f291dSjdc 	}
450d90581adSjdc 	ret = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
451d90581adSjdc 	    &ctrl, 1, &val, 1, 0);
452d90581adSjdc 	if (ret)
4539bb301c6Sjdc 		printf("%s: error changing fan speed (ch %d)\n",
4549bb301c6Sjdc 		    device_xname(sc->sc_dev), chan);
455d90581adSjdc 	else
456fd3cc354Sjdc 		DPRINTF("%s changed fan speed (ch %d) to 0x%x\n",
457fd3cc354Sjdc 		    device_xname(sc->sc_dev), chan, val);
458d90581adSjdc 	iic_release_bus(sc->sc_tag, 0);
459d90581adSjdc 	return ret;
460d90581adSjdc }
461d90581adSjdc 
462d90581adSjdc static void
ecadc_fan_adjust(void * v)463d90581adSjdc ecadc_fan_adjust(void *v)
464d90581adSjdc {
465d90581adSjdc 	struct ecadc_softc *sc = v;
4669bb301c6Sjdc 	struct ecadc_channel *chp;
467d90581adSjdc 	int i;
468d90581adSjdc 	u_int8_t temp, speed;
4699bb301c6Sjdc 	u_int32_t htemp, ffail;
470d90581adSjdc 
471d90581adSjdc 	for (i = 0; i < sc->sc_nchan; i++) {
4729bb301c6Sjdc 		chp = &sc->sc_channels[i];
4739bb301c6Sjdc 		if (chp->chan_type != PCF8591_SYS_FAN_CTRL)
4749bb301c6Sjdc 			continue;
475d90581adSjdc 
4769bb301c6Sjdc 		/* Check for high temperature or fan failure */
4779bb301c6Sjdc 		htemp = sysmon_envsys_get_max_value(is_high_temp, true);
4789bb301c6Sjdc 		ffail = sysmon_envsys_get_max_value(is_fan_fail, true);
4799bb301c6Sjdc 		if (htemp) {
4809bb301c6Sjdc 			printf("%s: High temperature detected\n",
4819bb301c6Sjdc 			    device_xname(sc->sc_dev));
4829bb301c6Sjdc 			/* Set fans to maximum speed */
4839bb301c6Sjdc 			speed = sc->sc_cpu_fan_spd[0];
4849bb301c6Sjdc 		} else if (ffail) {
4859bb301c6Sjdc 			printf("%s: Fan failure detected\n",
4869bb301c6Sjdc 			    device_xname(sc->sc_dev));
4879bb301c6Sjdc 			/* Set fans to maximum speed */
4889bb301c6Sjdc 			speed = sc->sc_cpu_fan_spd[0];
4899bb301c6Sjdc 		} else {
490d90581adSjdc 			/* Extract the raw value from the max CPU temp */
491d90581adSjdc 			temp = sysmon_envsys_get_max_value(is_cpu_temp, true)
492d90581adSjdc 			    & 0xff;
493d90581adSjdc 			if (!temp) {
4949bb301c6Sjdc 				printf("%s: skipping temp adjustment"
4959bb301c6Sjdc 				    " - no sensor values\n",
4969bb301c6Sjdc 				    device_xname(sc->sc_dev));
497d90581adSjdc 				return;
498d90581adSjdc 			}
499d90581adSjdc 			if (temp > XLATE_MAX)
500d90581adSjdc 				temp = XLATE_MAX;
501d90581adSjdc 			speed = chp->chan_xlate[temp];
5029bb301c6Sjdc 		}
503d90581adSjdc 		if (speed != chp->chan_speed) {
504d90581adSjdc 			if (!ecadc_set_fan_speed(sc, chp->chan_num,
505d90581adSjdc 			    speed))
506d90581adSjdc 				chp->chan_speed = speed;
507d90581adSjdc 		}
508d90581adSjdc 	}
509d90581adSjdc }
510