Home | History | Annotate | Line # | Download | only in dev
pcf8591_envctrl.c revision 1.19.4.1
      1  1.19.4.1   thorpej /*	$NetBSD: pcf8591_envctrl.c,v 1.19.4.1 2021/05/15 03:22:17 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.19.4.1   thorpej __KERNEL_RCSID(0, "$NetBSD: pcf8591_envctrl.c,v 1.19.4.1 2021/05/15 03:22:17 thorpej Exp $");
     23       1.6       mrg 
     24       1.1    martin #include <sys/param.h>
     25       1.1    martin #include <sys/systm.h>
     26      1.10       jdc #include <sys/kernel.h>
     27       1.1    martin #include <sys/device.h>
     28       1.1    martin 
     29       1.1    martin #include <dev/sysmon/sysmonvar.h>
     30      1.10       jdc #include <dev/sysmon/sysmon_taskq.h>
     31      1.10       jdc 
     32      1.10       jdc #include <machine/autoconf.h>
     33       1.1    martin 
     34       1.1    martin #include <dev/ofw/openfirm.h>
     35       1.1    martin #include <dev/i2c/i2cvar.h>
     36       1.1    martin 
     37      1.14       jdc #ifdef ECADC_DEBUG
     38      1.14       jdc #define DPRINTF printf
     39      1.14       jdc #else
     40      1.14       jdc #define DPRINTF if (0) printf
     41      1.14       jdc #endif
     42      1.14       jdc 
     43      1.10       jdc /* Translation tables contain 254 entries */
     44      1.10       jdc #define XLATE_SIZE		256
     45      1.10       jdc #define XLATE_MAX		(XLATE_SIZE - 2)
     46      1.10       jdc 
     47       1.1    martin #define PCF8591_CHANNELS	4
     48       1.1    martin 
     49       1.1    martin #define PCF8591_CTRL_CH0	0x00
     50       1.1    martin #define PCF8591_CTRL_CH1	0x01
     51       1.1    martin #define PCF8591_CTRL_CH2	0x02
     52       1.1    martin #define PCF8591_CTRL_CH3	0x03
     53       1.1    martin #define PCF8591_CTRL_AUTOINC	0x04
     54       1.1    martin #define PCF8591_CTRL_OSCILLATOR	0x40
     55       1.1    martin 
     56      1.10       jdc #define PCF8591_TEMP_SENS	0x00
     57      1.16       jdc #define PCF8591_SYS_FAN_CTRL	0x01
     58      1.10       jdc 
     59       1.1    martin struct ecadc_channel {
     60       1.1    martin 	u_int		chan_num;
     61      1.10       jdc 	u_int		chan_type;
     62       1.1    martin 	envsys_data_t	chan_sensor;
     63       1.1    martin 	u_char		*chan_xlate;
     64       1.1    martin 	int64_t		chan_factor;
     65       1.1    martin 	int64_t		chan_min;
     66       1.1    martin 	int64_t		chan_warn;
     67       1.1    martin 	int64_t		chan_crit;
     68      1.10       jdc 	u_int8_t	chan_speed;
     69       1.1    martin };
     70       1.1    martin 
     71       1.1    martin struct ecadc_softc {
     72       1.1    martin 	device_t		sc_dev;
     73       1.1    martin 	i2c_tag_t		sc_tag;
     74       1.1    martin 	i2c_addr_t		sc_addr;
     75      1.10       jdc 	u_char			sc_ps_xlate[XLATE_SIZE];
     76      1.10       jdc 	u_char			sc_cpu_xlate[XLATE_SIZE];
     77      1.10       jdc 	u_char			sc_cpu_fan_spd[XLATE_SIZE];
     78       1.1    martin 	u_int			sc_nchan;
     79       1.1    martin 	struct ecadc_channel	sc_channels[PCF8591_CHANNELS];
     80       1.1    martin 	struct sysmon_envsys	*sc_sme;
     81      1.10       jdc 	int			sc_hastimer;
     82      1.10       jdc 	callout_t		sc_timer;
     83       1.1    martin };
     84       1.1    martin 
     85       1.1    martin static int	ecadc_match(device_t, cfdata_t, void *);
     86       1.1    martin static void	ecadc_attach(device_t, device_t, void *);
     87      1.10       jdc static int	ecadc_detach(device_t, int);
     88       1.1    martin static void	ecadc_refresh(struct sysmon_envsys *, envsys_data_t *);
     89       1.1    martin static void	ecadc_get_limits(struct sysmon_envsys *, envsys_data_t *,
     90      1.10       jdc 			sysmon_envsys_lim_t *, u_int32_t *);
     91      1.15       jdc static int	ecadc_set_fan_speed(struct ecadc_softc *, u_int8_t, u_int8_t);
     92      1.10       jdc static void	ecadc_timeout(void *);
     93      1.10       jdc static void	ecadc_fan_adjust(void *);
     94      1.10       jdc 
     95      1.10       jdc CFATTACH_DECL3_NEW(ecadc, sizeof(struct ecadc_softc),
     96      1.10       jdc 	ecadc_match, ecadc_attach, ecadc_detach, NULL, NULL, NULL,
     97      1.10       jdc 	DVF_DETACH_SHUTDOWN);
     98       1.1    martin 
     99       1.9   thorpej static const struct device_compatible_entry compat_data[] = {
    100      1.17   thorpej 	{ .compat = "ecadc" },
    101      1.19   thorpej 	DEVICE_COMPAT_EOL
    102       1.8   thorpej };
    103       1.8   thorpej 
    104       1.1    martin static int
    105       1.1    martin ecadc_match(device_t parent, cfdata_t cf, void *aux)
    106       1.1    martin {
    107       1.1    martin 	struct i2c_attach_args *ia = aux;
    108       1.7   thorpej 	int match_result;
    109       1.1    martin 
    110      1.13       jdc 	if (iic_use_direct_match(ia, cf, compat_data, &match_result))
    111      1.13       jdc 		return match_result;
    112       1.7   thorpej 
    113       1.7   thorpej 	/* This driver is direct-config only. */
    114       1.1    martin 
    115       1.1    martin 	return 0;
    116       1.1    martin }
    117       1.1    martin 
    118       1.1    martin static void
    119       1.1    martin ecadc_attach(device_t parent, device_t self, void *aux)
    120       1.1    martin {
    121       1.1    martin 	struct i2c_attach_args *ia = aux;
    122       1.1    martin 	struct ecadc_softc *sc = device_private(self);
    123       1.1    martin 	u_char term[256];
    124       1.1    martin 	u_char *cp, *desc;
    125       1.1    martin 	int64_t minv, warnv, crit, num, den;
    126       1.1    martin 	u_int8_t junk[PCF8591_CHANNELS + 1];
    127       1.1    martin 	envsys_data_t *sensor;
    128  1.19.4.1   thorpej 	int len, error, addr, chan, node;
    129       1.1    martin 	u_int i;
    130       1.1    martin 
    131  1.19.4.1   thorpej 	node = devhandle_to_of(device_handle(self));
    132  1.19.4.1   thorpej 
    133       1.1    martin 	sc->sc_dev = self;
    134      1.10       jdc 	sc->sc_nchan = 0;
    135      1.10       jdc 	sc->sc_hastimer = 0;
    136      1.10       jdc 
    137      1.14       jdc 	DPRINTF("\n");
    138       1.1    martin 	if ((len = OF_getprop(node, "thermisters", term,
    139       1.1    martin 	    sizeof(term))) < 0) {
    140       1.1    martin 		aprint_error(": couldn't find \"thermisters\" property\n");
    141       1.1    martin 		return;
    142       1.1    martin 	}
    143       1.1    martin 
    144       1.1    martin 	if (OF_getprop(node, "cpu-temp-factors", &sc->sc_cpu_xlate[2],
    145      1.10       jdc 	    XLATE_MAX) < 0) {
    146       1.1    martin 		aprint_error(": couldn't find \"cpu-temp-factors\" property\n");
    147       1.1    martin 		return;
    148       1.1    martin 	}
    149       1.1    martin 	sc->sc_cpu_xlate[0] = sc->sc_cpu_xlate[1] = sc->sc_cpu_xlate[2];
    150       1.1    martin 
    151       1.1    martin 	/* Only the Sun Enterprise 450 has these. */
    152      1.10       jdc 	OF_getprop(node, "ps-temp-factors", &sc->sc_ps_xlate[2], XLATE_MAX);
    153       1.1    martin 	sc->sc_ps_xlate[0] = sc->sc_ps_xlate[1] = sc->sc_ps_xlate[2];
    154       1.1    martin 
    155       1.1    martin 	cp = term;
    156       1.1    martin 	while (cp < term + len) {
    157       1.1    martin 		addr = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
    158       1.1    martin 		chan = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
    159       1.1    martin 		minv = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
    160       1.1    martin 		warnv = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
    161       1.1    martin 		crit = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
    162       1.1    martin 		num = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
    163       1.1    martin 		den = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
    164       1.1    martin 		desc = cp;
    165       1.1    martin 		while (cp < term + len && *cp++);
    166       1.1    martin 
    167       1.1    martin 		if (addr != (ia->ia_addr << 1))
    168       1.1    martin 			continue;
    169       1.1    martin 
    170       1.1    martin 		if (num == 0 || den == 0)
    171       1.1    martin 			num = den = 1;
    172       1.1    martin 
    173       1.1    martin 		sc->sc_channels[sc->sc_nchan].chan_num = chan;
    174      1.10       jdc 		sc->sc_channels[sc->sc_nchan].chan_type = PCF8591_TEMP_SENS;
    175       1.1    martin 
    176       1.1    martin 		sensor = &sc->sc_channels[sc->sc_nchan].chan_sensor;
    177       1.1    martin 		sensor->units = ENVSYS_STEMP;
    178       1.4       jdc 		sensor->flags |= ENVSYS_FMONLIMITS;
    179       1.5  pgoyette 		sensor->state = ENVSYS_SINVALID;
    180       1.1    martin 		strlcpy(sensor->desc, desc, sizeof(sensor->desc));
    181       1.1    martin 
    182      1.14       jdc 		if (strncmp(desc, "CPU", 3) == 0) {
    183       1.1    martin 			sc->sc_channels[sc->sc_nchan].chan_xlate =
    184       1.1    martin 			    sc->sc_cpu_xlate;
    185      1.14       jdc 			DPRINTF("%s: "
    186      1.14       jdc 			    "added %s sensor (chan %d) with cpu_xlate\n",
    187      1.14       jdc 			    device_xname(sc->sc_dev), desc, chan);
    188      1.14       jdc 		} else if (strncmp(desc, "PS", 2) == 0) {
    189       1.1    martin 			sc->sc_channels[sc->sc_nchan].chan_xlate =
    190       1.1    martin 			    sc->sc_ps_xlate;
    191      1.14       jdc 			DPRINTF("%s: "
    192      1.14       jdc 			    "added %s sensor (chan %d) with ps_xlate\n",
    193      1.14       jdc 			    device_xname(sc->sc_dev), desc, chan);
    194      1.14       jdc 		} else {
    195       1.1    martin 			sc->sc_channels[sc->sc_nchan].chan_factor =
    196       1.1    martin 			    (1000000 * num) / den;
    197      1.14       jdc 			DPRINTF("%s: "
    198      1.14       jdc 			    "added %s sensor (chan %d) without xlate\n",
    199      1.14       jdc 			    device_xname(sc->sc_dev), desc, chan);
    200      1.14       jdc 		}
    201       1.1    martin 		sc->sc_channels[sc->sc_nchan].chan_min =
    202       1.1    martin 		    273150000 + 1000000 * minv;
    203       1.1    martin 		sc->sc_channels[sc->sc_nchan].chan_warn =
    204       1.1    martin 		    273150000 + 1000000 * warnv;
    205       1.1    martin 		sc->sc_channels[sc->sc_nchan].chan_crit =
    206       1.1    martin 		    273150000 + 1000000 * crit;
    207       1.1    martin 		sc->sc_nchan++;
    208       1.1    martin 	}
    209       1.1    martin 
    210      1.15       jdc 	sc->sc_tag = ia->ia_tag;
    211      1.15       jdc 	sc->sc_addr = ia->ia_addr;
    212      1.15       jdc 
    213      1.15       jdc 	iic_acquire_bus(sc->sc_tag, 0);
    214      1.15       jdc 
    215      1.15       jdc 	/* Try a read now, so we can fail if this component isn't present */
    216      1.15       jdc 	if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
    217      1.15       jdc 	    NULL, 0, junk, sc->sc_nchan + 1, 0)) {
    218      1.15       jdc 		aprint_normal(": read failed\n");
    219      1.15       jdc 		iic_release_bus(sc->sc_tag, 0);
    220      1.15       jdc 		return;
    221      1.15       jdc 	}
    222      1.15       jdc 
    223      1.15       jdc 	iic_release_bus(sc->sc_tag, 0);
    224      1.15       jdc 
    225      1.10       jdc 	/*
    226      1.10       jdc 	 * Fan speed changing information is missing from OFW
    227      1.10       jdc 	 * The E250 CPU fan is connected to the sensor at addr 0x4a, channel 1
    228      1.10       jdc 	 */
    229      1.10       jdc 	if (ia->ia_addr == 0x4a && !strcmp(machine_model, "SUNW,Ultra-250") &&
    230      1.10       jdc 	    OF_getprop(node, "cpu-fan-speeds", &sc->sc_cpu_fan_spd,
    231      1.10       jdc 	    XLATE_MAX) > 0) {
    232      1.10       jdc 		sc->sc_channels[sc->sc_nchan].chan_num = 1;
    233      1.16       jdc 		sc->sc_channels[sc->sc_nchan].chan_type = PCF8591_SYS_FAN_CTRL;
    234      1.10       jdc 		sensor = &sc->sc_channels[sc->sc_nchan].chan_sensor;
    235      1.10       jdc 		sensor->units = ENVSYS_INTEGER;
    236      1.10       jdc 		sensor->flags = ENVSYS_FMONNOTSUPP;
    237      1.10       jdc 		sensor->state = ENVSYS_SINVALID;
    238      1.16       jdc 		strlcpy(sensor->desc, "SYSFAN", sizeof(sensor->desc));
    239      1.10       jdc 		sc->sc_channels[sc->sc_nchan].chan_xlate = sc->sc_cpu_fan_spd;
    240      1.14       jdc 		DPRINTF("%s: "
    241      1.14       jdc 		    "added CPUFAN sensor (chan %d) with cpu-fan xlate\n",
    242      1.14       jdc 		    device_xname(sc->sc_dev),
    243      1.14       jdc 		    sc->sc_channels[sc->sc_nchan].chan_num);
    244      1.15       jdc 
    245      1.15       jdc 		/* Set the fan to medium speed */
    246      1.15       jdc 		sc->sc_channels[sc->sc_nchan].chan_speed =
    247      1.15       jdc 		    (sc->sc_cpu_fan_spd[0]+sc->sc_cpu_fan_spd[XLATE_MAX])/2;
    248      1.15       jdc 		ecadc_set_fan_speed(sc, sc->sc_channels[sc->sc_nchan].chan_num,
    249      1.15       jdc 		    sc->sc_channels[sc->sc_nchan].chan_speed);
    250      1.15       jdc 
    251      1.10       jdc 		sc->sc_nchan++;
    252      1.10       jdc 		sc->sc_hastimer = 1;
    253      1.10       jdc 	}
    254      1.10       jdc 
    255       1.1    martin 	/* Hook us into the sysmon_envsys subsystem */
    256       1.1    martin 	sc->sc_sme = sysmon_envsys_create();
    257       1.1    martin 	sc->sc_sme->sme_name = device_xname(self);
    258       1.1    martin 	sc->sc_sme->sme_cookie = sc;
    259       1.1    martin 	sc->sc_sme->sme_refresh = ecadc_refresh;
    260       1.1    martin 	sc->sc_sme->sme_get_limits = ecadc_get_limits;
    261       1.1    martin 
    262       1.1    martin 	/* Initialize sensor data. */
    263       1.1    martin 	for (i = 0; i < sc->sc_nchan; i++)
    264       1.1    martin 		sysmon_envsys_sensor_attach(sc->sc_sme,
    265       1.1    martin 		    &sc->sc_channels[i].chan_sensor);
    266       1.1    martin 
    267       1.1    martin 	error = sysmon_envsys_register(sc->sc_sme);
    268       1.1    martin 	if (error) {
    269       1.1    martin 		aprint_error_dev(self, "error %d registering with sysmon\n",
    270       1.1    martin 			error);
    271       1.1    martin 		sysmon_envsys_destroy(sc->sc_sme);
    272      1.13       jdc 		sc->sc_sme = NULL;
    273       1.1    martin 		return;
    274       1.1    martin 	}
    275      1.10       jdc 
    276      1.10       jdc 	if (sc->sc_hastimer) {
    277      1.10       jdc 		callout_init(&sc->sc_timer, CALLOUT_MPSAFE);
    278      1.10       jdc 		callout_reset(&sc->sc_timer, hz*20, ecadc_timeout, sc);
    279      1.10       jdc 	}
    280       1.1    martin 
    281       1.1    martin 	aprint_naive(": Temp Sensors\n");
    282      1.10       jdc 	aprint_normal(": %s Temp Sensors (%d channels)\n", ia->ia_name,
    283      1.10       jdc 	    sc->sc_nchan);
    284      1.10       jdc }
    285      1.10       jdc 
    286      1.10       jdc static int
    287      1.10       jdc ecadc_detach(device_t self, int flags)
    288      1.10       jdc {
    289      1.10       jdc 	struct ecadc_softc *sc = device_private(self);
    290      1.15       jdc 	int c, i;
    291      1.15       jdc 
    292      1.10       jdc 	if (sc->sc_hastimer) {
    293      1.10       jdc 		callout_halt(&sc->sc_timer, NULL);
    294      1.10       jdc 		callout_destroy(&sc->sc_timer);
    295      1.10       jdc 	}
    296      1.10       jdc 
    297      1.10       jdc 	if (sc->sc_sme != NULL)
    298      1.11       jdc 		sysmon_envsys_unregister(sc->sc_sme);
    299      1.10       jdc 
    300      1.15       jdc 	for (i = 0; i < sc->sc_nchan; i++) {
    301      1.15       jdc 		struct ecadc_channel *chp = &sc->sc_channels[i];
    302      1.15       jdc 
    303      1.16       jdc 		if (chp->chan_type == PCF8591_SYS_FAN_CTRL) {
    304      1.15       jdc 			/* Loop in case the bus is busy */
    305      1.15       jdc 			for (c = 0; c < 5; c++) {
    306      1.15       jdc 				chp->chan_speed = sc->sc_cpu_fan_spd[0];
    307      1.15       jdc 				if (!ecadc_set_fan_speed(sc, chp->chan_num,
    308      1.15       jdc 				    chp->chan_speed))
    309      1.15       jdc 					return 0;
    310      1.15       jdc 				delay(10000);
    311      1.15       jdc 			}
    312      1.16       jdc 			printf("%s: cannot set fan speed (chan %d)\n",
    313      1.16       jdc 			    device_xname(sc->sc_dev), chp->chan_num);
    314      1.15       jdc 		}
    315      1.15       jdc 	}
    316      1.15       jdc 
    317      1.10       jdc 	return 0;
    318       1.1    martin }
    319       1.1    martin 
    320       1.1    martin static void
    321       1.1    martin ecadc_refresh(struct sysmon_envsys *sme, envsys_data_t *sensor)
    322       1.1    martin {
    323       1.1    martin 	struct ecadc_softc *sc = sme->sme_cookie;
    324       1.1    martin 	u_int i;
    325       1.1    martin 	u_int8_t data[PCF8591_CHANNELS + 1];
    326       1.1    martin 	u_int8_t ctrl = PCF8591_CTRL_CH0 | PCF8591_CTRL_AUTOINC |
    327       1.1    martin 	    PCF8591_CTRL_OSCILLATOR;
    328       1.1    martin 
    329      1.13       jdc 	if (iic_acquire_bus(sc->sc_tag, 0))
    330      1.13       jdc 		return;
    331       1.1    martin 	if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
    332       1.1    martin 	    &ctrl, 1, NULL, 0, 0)) {
    333       1.1    martin 		iic_release_bus(sc->sc_tag, 0);
    334       1.1    martin 		return;
    335       1.1    martin 	}
    336      1.10       jdc 	/*
    337      1.10       jdc 	 * Each data byte that we read is the result of the previous request,
    338      1.10       jdc 	 * so read num_channels + 1 and update envsys values from chan + 1.
    339      1.10       jdc 	 */
    340       1.1    martin 	if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
    341       1.1    martin 	    NULL, 0, data, PCF8591_CHANNELS + 1, 0)) {
    342       1.1    martin 		iic_release_bus(sc->sc_tag, 0);
    343       1.1    martin 		return;
    344       1.1    martin 	}
    345       1.1    martin 	iic_release_bus(sc->sc_tag, 0);
    346       1.1    martin 
    347      1.10       jdc 	/* Temperature with/without translation or relative (ADC value) */
    348       1.1    martin 	for (i = 0; i < sc->sc_nchan; i++) {
    349       1.1    martin 		struct ecadc_channel *chp = &sc->sc_channels[i];
    350       1.1    martin 
    351      1.10       jdc 		if (chp->chan_type == PCF8591_TEMP_SENS) {
    352      1.10       jdc 
    353      1.10       jdc 			/* Encode the raw value to use for the fan control */
    354      1.10       jdc 			if (chp->chan_xlate) {
    355      1.10       jdc 				int32_t temp;
    356      1.10       jdc 
    357      1.10       jdc 				temp = 273150000 + 1000000 *
    358      1.10       jdc 				    chp->chan_xlate[data[1 + chp->chan_num]];
    359      1.10       jdc 				temp &= ~0xff;
    360      1.10       jdc 				temp += data[1 + chp->chan_num];
    361      1.10       jdc 				chp->chan_sensor.value_cur = temp;
    362      1.14       jdc 				DPRINTF("%s: xlate %s sensor = %d"
    363      1.14       jdc 				    " (0x%x > 0x%x)\n",
    364      1.14       jdc 				    device_xname(sc->sc_dev),
    365      1.14       jdc 				    chp->chan_sensor.desc, temp,
    366      1.14       jdc 				    data[1 + chp->chan_num],
    367      1.14       jdc 				    chp->chan_xlate[data[1 + chp->chan_num]]);
    368      1.14       jdc 			} else {
    369      1.10       jdc 				chp->chan_sensor.value_cur = 273150000 +
    370      1.10       jdc 				    chp->chan_factor * data[1 + chp->chan_num];
    371      1.14       jdc 				DPRINTF("%s: read %s sensor = %d (0x%x)\n",
    372      1.14       jdc 				    device_xname(sc->sc_dev),
    373      1.14       jdc 				    chp->chan_sensor.desc,
    374      1.14       jdc 				    chp->chan_sensor.value_cur,
    375      1.14       jdc 				    data[1 + chp->chan_num]);
    376      1.14       jdc 			}
    377      1.10       jdc 			chp->chan_sensor.flags |= ENVSYS_FMONLIMITS;
    378      1.10       jdc 		}
    379      1.16       jdc 		if (chp->chan_type == PCF8591_SYS_FAN_CTRL)
    380      1.10       jdc 			chp->chan_sensor.value_cur = data[1 + chp->chan_num];
    381       1.1    martin 
    382       1.1    martin 		chp->chan_sensor.state = ENVSYS_SVALID;
    383       1.1    martin 	}
    384       1.1    martin }
    385       1.1    martin 
    386       1.1    martin static void
    387       1.1    martin ecadc_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
    388      1.10       jdc 		sysmon_envsys_lim_t *limits, u_int32_t *props)
    389       1.1    martin {
    390       1.1    martin 	struct ecadc_softc *sc = sme->sme_cookie;
    391       1.1    martin 	int i;
    392       1.1    martin 
    393       1.1    martin 	for (i = 0; i < sc->sc_nchan; i++) {
    394       1.1    martin 		if (edata != &sc->sc_channels[i].chan_sensor)
    395       1.1    martin 			continue;
    396      1.10       jdc 		if (sc->sc_channels[i].chan_type == PCF8591_TEMP_SENS) {
    397      1.10       jdc 			*props |= PROP_WARNMIN|PROP_WARNMAX|PROP_CRITMAX;
    398      1.10       jdc 			limits->sel_warnmin = sc->sc_channels[i].chan_min;
    399      1.10       jdc 			limits->sel_warnmax = sc->sc_channels[i].chan_warn;
    400      1.10       jdc 			limits->sel_critmax = sc->sc_channels[i].chan_crit;
    401      1.10       jdc 		}
    402       1.1    martin 		return;
    403       1.1    martin 	}
    404       1.1    martin }
    405      1.10       jdc 
    406      1.10       jdc static void
    407      1.10       jdc ecadc_timeout(void *v)
    408      1.10       jdc {
    409      1.10       jdc 	struct ecadc_softc *sc = v;
    410      1.10       jdc 
    411      1.10       jdc 	sysmon_task_queue_sched(0, ecadc_fan_adjust, sc);
    412      1.10       jdc 	callout_reset(&sc->sc_timer, hz*60, ecadc_timeout, sc);
    413      1.10       jdc }
    414      1.10       jdc 
    415      1.10       jdc static bool
    416      1.10       jdc is_cpu_temp(const envsys_data_t *edata)
    417      1.10       jdc {
    418      1.10       jdc 	if (edata->units != ENVSYS_STEMP)
    419      1.10       jdc 		return false;
    420      1.10       jdc 	return strncmp(edata->desc, "CPU", 3) == 0;
    421      1.10       jdc }
    422      1.10       jdc 
    423      1.16       jdc static bool
    424      1.16       jdc is_high_temp(const envsys_data_t *edata)
    425      1.16       jdc {
    426      1.16       jdc 	if (edata->units != ENVSYS_INDICATOR)
    427      1.16       jdc 		return false;
    428      1.16       jdc 	return strcmp(edata->desc, "high_temp") == 0;
    429      1.16       jdc }
    430      1.16       jdc 
    431      1.16       jdc static bool
    432      1.16       jdc is_fan_fail(const envsys_data_t *edata)
    433      1.16       jdc {
    434      1.16       jdc 	if (edata->units != ENVSYS_INDICATOR)
    435      1.16       jdc 		return false;
    436      1.16       jdc 	return strcmp(edata->desc, "fan_fail") == 0;
    437      1.16       jdc }
    438      1.16       jdc 
    439      1.10       jdc static int
    440      1.10       jdc ecadc_set_fan_speed(struct ecadc_softc *sc, u_int8_t chan, u_int8_t val)
    441      1.10       jdc {
    442      1.10       jdc 	u_int8_t ctrl = PCF8591_CTRL_AUTOINC | PCF8591_CTRL_OSCILLATOR;
    443      1.10       jdc 	int ret;
    444      1.10       jdc 
    445      1.10       jdc 	ctrl |= chan;
    446      1.13       jdc 	ret = iic_acquire_bus(sc->sc_tag, 0);
    447      1.13       jdc 	if (ret) {
    448      1.16       jdc 		printf("%s: error acquiring i2c bus (ch %d)\n",
    449      1.16       jdc 		    device_xname(sc->sc_dev), chan);
    450      1.13       jdc 		return ret;
    451      1.13       jdc 	}
    452      1.10       jdc 	ret = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
    453      1.10       jdc 	    &ctrl, 1, &val, 1, 0);
    454      1.10       jdc 	if (ret)
    455      1.16       jdc 		printf("%s: error changing fan speed (ch %d)\n",
    456      1.16       jdc 		    device_xname(sc->sc_dev), chan);
    457      1.10       jdc 	else
    458      1.14       jdc 		DPRINTF("%s changed fan speed (ch %d) to 0x%x\n",
    459      1.14       jdc 		    device_xname(sc->sc_dev), chan, val);
    460      1.10       jdc 	iic_release_bus(sc->sc_tag, 0);
    461      1.10       jdc 	return ret;
    462      1.10       jdc }
    463      1.10       jdc 
    464      1.10       jdc static void
    465      1.10       jdc ecadc_fan_adjust(void *v)
    466      1.10       jdc {
    467      1.10       jdc 	struct ecadc_softc *sc = v;
    468      1.16       jdc 	struct ecadc_channel *chp;
    469      1.10       jdc 	int i;
    470      1.10       jdc 	u_int8_t temp, speed;
    471      1.16       jdc 	u_int32_t htemp, ffail;
    472      1.10       jdc 
    473      1.10       jdc 	for (i = 0; i < sc->sc_nchan; i++) {
    474      1.16       jdc 		chp = &sc->sc_channels[i];
    475      1.16       jdc 		if (chp->chan_type != PCF8591_SYS_FAN_CTRL)
    476      1.16       jdc 			continue;
    477      1.10       jdc 
    478      1.16       jdc 		/* Check for high temperature or fan failure */
    479      1.16       jdc 		htemp = sysmon_envsys_get_max_value(is_high_temp, true);
    480      1.16       jdc 		ffail = sysmon_envsys_get_max_value(is_fan_fail, true);
    481      1.16       jdc 		if (htemp) {
    482      1.16       jdc 			printf("%s: High temperature detected\n",
    483      1.16       jdc 			    device_xname(sc->sc_dev));
    484      1.16       jdc 			/* Set fans to maximum speed */
    485      1.16       jdc 			speed = sc->sc_cpu_fan_spd[0];
    486      1.16       jdc 		} else if (ffail) {
    487      1.16       jdc 			printf("%s: Fan failure detected\n",
    488      1.16       jdc 			    device_xname(sc->sc_dev));
    489      1.16       jdc 			/* Set fans to maximum speed */
    490      1.16       jdc 			speed = sc->sc_cpu_fan_spd[0];
    491      1.16       jdc 		} else {
    492      1.10       jdc 			/* Extract the raw value from the max CPU temp */
    493      1.10       jdc 			temp = sysmon_envsys_get_max_value(is_cpu_temp, true)
    494      1.16       jdc 			    & 0xff;
    495      1.10       jdc 			if (!temp) {
    496      1.16       jdc 				printf("%s: skipping temp adjustment"
    497      1.16       jdc 				    " - no sensor values\n",
    498      1.16       jdc 				    device_xname(sc->sc_dev));
    499      1.10       jdc 				return;
    500      1.10       jdc 			}
    501      1.10       jdc 			if (temp > XLATE_MAX)
    502      1.10       jdc 				temp = XLATE_MAX;
    503      1.10       jdc 			speed = chp->chan_xlate[temp];
    504      1.16       jdc 		}
    505      1.16       jdc 		if (speed != chp->chan_speed) {
    506      1.16       jdc 			if (!ecadc_set_fan_speed(sc, chp->chan_num,
    507      1.16       jdc 			    speed))
    508      1.16       jdc 				chp->chan_speed = speed;
    509      1.10       jdc 		}
    510      1.10       jdc 	}
    511      1.10       jdc }
    512