Home | History | Annotate | Line # | Download | only in dev
pcf8591_envctrl.c revision 1.10
      1  1.10       jdc /*	$NetBSD: pcf8591_envctrl.c,v 1.10 2020/10/24 15:16:39 jdc 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.10       jdc __KERNEL_RCSID(0, "$NetBSD: pcf8591_envctrl.c,v 1.10 2020/10/24 15:16:39 jdc 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.10       jdc /* Translation tables contain 254 entries */
     38  1.10       jdc #define XLATE_SIZE		256
     39  1.10       jdc #define XLATE_MAX		(XLATE_SIZE - 2)
     40  1.10       jdc 
     41   1.1    martin #define PCF8591_CHANNELS	4
     42   1.1    martin 
     43   1.1    martin #define PCF8591_CTRL_CH0	0x00
     44   1.1    martin #define PCF8591_CTRL_CH1	0x01
     45   1.1    martin #define PCF8591_CTRL_CH2	0x02
     46   1.1    martin #define PCF8591_CTRL_CH3	0x03
     47   1.1    martin #define PCF8591_CTRL_AUTOINC	0x04
     48   1.1    martin #define PCF8591_CTRL_OSCILLATOR	0x40
     49   1.1    martin 
     50  1.10       jdc #define PCF8591_TEMP_SENS	0x00
     51  1.10       jdc #define PCF8591_CPU_FAN_CTRL	0x01
     52  1.10       jdc #define PCF8591_PS_FAN_CTRL	0x02
     53  1.10       jdc 
     54   1.1    martin struct ecadc_channel {
     55   1.1    martin 	u_int		chan_num;
     56  1.10       jdc 	u_int		chan_type;
     57   1.1    martin 	envsys_data_t	chan_sensor;
     58   1.1    martin 	u_char		*chan_xlate;
     59   1.1    martin 	int64_t		chan_factor;
     60   1.1    martin 	int64_t		chan_min;
     61   1.1    martin 	int64_t		chan_warn;
     62   1.1    martin 	int64_t		chan_crit;
     63  1.10       jdc 	u_int8_t	chan_speed;
     64   1.1    martin };
     65   1.1    martin 
     66   1.1    martin struct ecadc_softc {
     67   1.1    martin 	device_t		sc_dev;
     68   1.1    martin 	i2c_tag_t		sc_tag;
     69   1.1    martin 	i2c_addr_t		sc_addr;
     70  1.10       jdc 	u_char			sc_ps_xlate[XLATE_SIZE];
     71  1.10       jdc 	u_char			sc_cpu_xlate[XLATE_SIZE];
     72  1.10       jdc 	u_char			sc_cpu_fan_spd[XLATE_SIZE];
     73   1.1    martin 	u_int			sc_nchan;
     74   1.1    martin 	struct ecadc_channel	sc_channels[PCF8591_CHANNELS];
     75   1.1    martin 	struct sysmon_envsys	*sc_sme;
     76  1.10       jdc 	int			sc_hastimer;
     77  1.10       jdc 	callout_t		sc_timer;
     78   1.1    martin };
     79   1.1    martin 
     80   1.1    martin static int	ecadc_match(device_t, cfdata_t, void *);
     81   1.1    martin static void	ecadc_attach(device_t, device_t, void *);
     82  1.10       jdc static int	ecadc_detach(device_t, int);
     83   1.1    martin static void	ecadc_refresh(struct sysmon_envsys *, envsys_data_t *);
     84   1.1    martin static void	ecadc_get_limits(struct sysmon_envsys *, envsys_data_t *,
     85  1.10       jdc 			sysmon_envsys_lim_t *, u_int32_t *);
     86  1.10       jdc static void	ecadc_timeout(void *);
     87  1.10       jdc static void	ecadc_fan_adjust(void *);
     88  1.10       jdc 
     89  1.10       jdc CFATTACH_DECL3_NEW(ecadc, sizeof(struct ecadc_softc),
     90  1.10       jdc 	ecadc_match, ecadc_attach, ecadc_detach, NULL, NULL, NULL,
     91  1.10       jdc 	DVF_DETACH_SHUTDOWN);
     92   1.1    martin 
     93   1.9   thorpej static const struct device_compatible_entry compat_data[] = {
     94   1.9   thorpej 	{ "ecadc",		0 },
     95   1.9   thorpej 	{ NULL,			0 }
     96   1.8   thorpej };
     97   1.8   thorpej 
     98   1.1    martin static int
     99   1.1    martin ecadc_match(device_t parent, cfdata_t cf, void *aux)
    100   1.1    martin {
    101   1.1    martin 	struct i2c_attach_args *ia = aux;
    102   1.7   thorpej 	int match_result;
    103   1.1    martin 
    104   1.9   thorpej 	if (iic_use_direct_match(ia, cf, compat_data, &match_result))
    105   1.7   thorpej 		return match_result;
    106   1.7   thorpej 
    107   1.7   thorpej 	/* This driver is direct-config only. */
    108   1.1    martin 
    109   1.1    martin 	return 0;
    110   1.1    martin }
    111   1.1    martin 
    112   1.1    martin static void
    113   1.1    martin ecadc_attach(device_t parent, device_t self, void *aux)
    114   1.1    martin {
    115   1.1    martin 	struct i2c_attach_args *ia = aux;
    116   1.1    martin 	struct ecadc_softc *sc = device_private(self);
    117   1.1    martin 	u_char term[256];
    118   1.1    martin 	u_char *cp, *desc;
    119   1.1    martin 	int64_t minv, warnv, crit, num, den;
    120   1.1    martin 	u_int8_t junk[PCF8591_CHANNELS + 1];
    121   1.1    martin 	envsys_data_t *sensor;
    122   1.1    martin 	int len, error, addr, chan, node = (int)ia->ia_cookie;
    123   1.1    martin 	u_int i;
    124   1.1    martin 
    125   1.1    martin 	sc->sc_dev = self;
    126  1.10       jdc 	sc->sc_nchan = 0;
    127  1.10       jdc 	sc->sc_hastimer = 0;
    128  1.10       jdc 
    129   1.1    martin 	if ((len = OF_getprop(node, "thermisters", term,
    130   1.1    martin 	    sizeof(term))) < 0) {
    131   1.1    martin 		aprint_error(": couldn't find \"thermisters\" property\n");
    132   1.1    martin 		return;
    133   1.1    martin 	}
    134   1.1    martin 
    135   1.1    martin 	if (OF_getprop(node, "cpu-temp-factors", &sc->sc_cpu_xlate[2],
    136  1.10       jdc 	    XLATE_MAX) < 0) {
    137   1.1    martin 		aprint_error(": couldn't find \"cpu-temp-factors\" property\n");
    138   1.1    martin 		return;
    139   1.1    martin 	}
    140   1.1    martin 	sc->sc_cpu_xlate[0] = sc->sc_cpu_xlate[1] = sc->sc_cpu_xlate[2];
    141   1.1    martin 
    142   1.1    martin 	/* Only the Sun Enterprise 450 has these. */
    143  1.10       jdc 	OF_getprop(node, "ps-temp-factors", &sc->sc_ps_xlate[2], XLATE_MAX);
    144   1.1    martin 	sc->sc_ps_xlate[0] = sc->sc_ps_xlate[1] = sc->sc_ps_xlate[2];
    145   1.1    martin 
    146   1.1    martin 	cp = term;
    147   1.1    martin 	while (cp < term + len) {
    148   1.1    martin 		addr = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
    149   1.1    martin 		chan = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
    150   1.1    martin 		minv = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
    151   1.1    martin 		warnv = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
    152   1.1    martin 		crit = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
    153   1.1    martin 		num = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
    154   1.1    martin 		den = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
    155   1.1    martin 		desc = cp;
    156   1.1    martin 		while (cp < term + len && *cp++);
    157   1.1    martin 
    158   1.1    martin 		if (addr != (ia->ia_addr << 1))
    159   1.1    martin 			continue;
    160   1.1    martin 
    161   1.1    martin 		if (num == 0 || den == 0)
    162   1.1    martin 			num = den = 1;
    163   1.1    martin 
    164   1.1    martin 		sc->sc_channels[sc->sc_nchan].chan_num = chan;
    165  1.10       jdc 		sc->sc_channels[sc->sc_nchan].chan_type = PCF8591_TEMP_SENS;
    166   1.1    martin 
    167   1.1    martin 		sensor = &sc->sc_channels[sc->sc_nchan].chan_sensor;
    168   1.1    martin 		sensor->units = ENVSYS_STEMP;
    169   1.4       jdc 		sensor->flags |= ENVSYS_FMONLIMITS;
    170   1.5  pgoyette 		sensor->state = ENVSYS_SINVALID;
    171   1.1    martin 		strlcpy(sensor->desc, desc, sizeof(sensor->desc));
    172   1.1    martin 
    173   1.1    martin 		if (strncmp(desc, "CPU", 3) == 0)
    174   1.1    martin 			sc->sc_channels[sc->sc_nchan].chan_xlate =
    175   1.1    martin 			    sc->sc_cpu_xlate;
    176   1.1    martin 		else if (strncmp(desc, "PS", 2) == 0)
    177   1.1    martin 			sc->sc_channels[sc->sc_nchan].chan_xlate =
    178   1.1    martin 			    sc->sc_ps_xlate;
    179   1.1    martin 		else
    180   1.1    martin 			sc->sc_channels[sc->sc_nchan].chan_factor =
    181   1.1    martin 			    (1000000 * num) / den;
    182   1.1    martin 		sc->sc_channels[sc->sc_nchan].chan_min =
    183   1.1    martin 		    273150000 + 1000000 * minv;
    184   1.1    martin 		sc->sc_channels[sc->sc_nchan].chan_warn =
    185   1.1    martin 		    273150000 + 1000000 * warnv;
    186   1.1    martin 		sc->sc_channels[sc->sc_nchan].chan_crit =
    187   1.1    martin 		    273150000 + 1000000 * crit;
    188   1.1    martin 		sc->sc_nchan++;
    189   1.1    martin 	}
    190   1.1    martin 
    191  1.10       jdc 	/*
    192  1.10       jdc 	 * Fan speed changing information is missing from OFW
    193  1.10       jdc 	 * The E250 CPU fan is connected to the sensor at addr 0x4a, channel 1
    194  1.10       jdc 	 */
    195  1.10       jdc 	if (ia->ia_addr == 0x4a && !strcmp(machine_model, "SUNW,Ultra-250") &&
    196  1.10       jdc 	    OF_getprop(node, "cpu-fan-speeds", &sc->sc_cpu_fan_spd,
    197  1.10       jdc 	    XLATE_MAX) > 0) {
    198  1.10       jdc 		sc->sc_channels[sc->sc_nchan].chan_num = 1;
    199  1.10       jdc 		sc->sc_channels[sc->sc_nchan].chan_type = PCF8591_CPU_FAN_CTRL;
    200  1.10       jdc 		sc->sc_channels[sc->sc_nchan].chan_speed = 0;
    201  1.10       jdc 		sensor = &sc->sc_channels[sc->sc_nchan].chan_sensor;
    202  1.10       jdc 		sensor->units = ENVSYS_INTEGER;
    203  1.10       jdc 		sensor->flags = ENVSYS_FMONNOTSUPP;
    204  1.10       jdc 		sensor->state = ENVSYS_SINVALID;
    205  1.10       jdc 		strlcpy(sensor->desc, "CPUFAN", sizeof(sensor->desc));
    206  1.10       jdc 		sc->sc_channels[sc->sc_nchan].chan_xlate = sc->sc_cpu_fan_spd;
    207  1.10       jdc 		sc->sc_nchan++;
    208  1.10       jdc 
    209  1.10       jdc 		sc->sc_hastimer = 1;
    210  1.10       jdc 	}
    211  1.10       jdc 
    212   1.1    martin 	sc->sc_tag = ia->ia_tag;
    213   1.1    martin 	sc->sc_addr = ia->ia_addr;
    214   1.1    martin 
    215   1.1    martin 	iic_acquire_bus(sc->sc_tag, 0);
    216   1.1    martin 
    217   1.1    martin 	/* Try a read now, so we can fail if it doesn't work */
    218   1.1    martin 	if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
    219   1.1    martin 	    NULL, 0, junk, sc->sc_nchan + 1, 0)) {
    220   1.1    martin 		aprint_error(": read failed\n");
    221   1.1    martin 		iic_release_bus(sc->sc_tag, 0);
    222   1.1    martin 		return;
    223   1.1    martin 	}
    224   1.1    martin 
    225   1.1    martin 	iic_release_bus(sc->sc_tag, 0);
    226   1.1    martin 
    227   1.1    martin 	/* Hook us into the sysmon_envsys subsystem */
    228   1.1    martin 	sc->sc_sme = sysmon_envsys_create();
    229   1.1    martin 	sc->sc_sme->sme_name = device_xname(self);
    230   1.1    martin 	sc->sc_sme->sme_cookie = sc;
    231   1.1    martin 	sc->sc_sme->sme_refresh = ecadc_refresh;
    232   1.1    martin 	sc->sc_sme->sme_get_limits = ecadc_get_limits;
    233   1.1    martin 
    234   1.1    martin 	/* Initialize sensor data. */
    235   1.1    martin 	for (i = 0; i < sc->sc_nchan; i++)
    236   1.1    martin 		sysmon_envsys_sensor_attach(sc->sc_sme,
    237   1.1    martin 		    &sc->sc_channels[i].chan_sensor);
    238   1.1    martin 
    239   1.1    martin 	error = sysmon_envsys_register(sc->sc_sme);
    240   1.1    martin 	if (error) {
    241   1.1    martin 		aprint_error_dev(self, "error %d registering with sysmon\n",
    242   1.1    martin 			error);
    243   1.1    martin 		sysmon_envsys_destroy(sc->sc_sme);
    244   1.1    martin 		return;
    245   1.1    martin 	}
    246  1.10       jdc 
    247  1.10       jdc 	if (sc->sc_hastimer) {
    248  1.10       jdc 		callout_init(&sc->sc_timer, CALLOUT_MPSAFE);
    249  1.10       jdc 		callout_reset(&sc->sc_timer, hz*20, ecadc_timeout, sc);
    250  1.10       jdc 	}
    251   1.1    martin 
    252   1.1    martin 	aprint_naive(": Temp Sensors\n");
    253  1.10       jdc 	aprint_normal(": %s Temp Sensors (%d channels)\n", ia->ia_name,
    254  1.10       jdc 	    sc->sc_nchan);
    255  1.10       jdc }
    256  1.10       jdc 
    257  1.10       jdc static int
    258  1.10       jdc ecadc_detach(device_t self, int flags)
    259  1.10       jdc {
    260  1.10       jdc 	struct ecadc_softc *sc = device_private(self);
    261  1.10       jdc 	if (sc->sc_hastimer) {
    262  1.10       jdc 		callout_halt(&sc->sc_timer, NULL);
    263  1.10       jdc 		callout_destroy(&sc->sc_timer);
    264  1.10       jdc 	}
    265  1.10       jdc 
    266  1.10       jdc 	if (sc->sc_sme != NULL)
    267  1.10       jdc 		sysmon_envsys_destroy(sc->sc_sme);
    268  1.10       jdc 
    269  1.10       jdc 	return 0;
    270   1.1    martin }
    271   1.1    martin 
    272   1.1    martin static void
    273   1.1    martin ecadc_refresh(struct sysmon_envsys *sme, envsys_data_t *sensor)
    274   1.1    martin {
    275   1.1    martin 	struct ecadc_softc *sc = sme->sme_cookie;
    276   1.1    martin 	u_int i;
    277   1.1    martin 	u_int8_t data[PCF8591_CHANNELS + 1];
    278   1.1    martin 	u_int8_t ctrl = PCF8591_CTRL_CH0 | PCF8591_CTRL_AUTOINC |
    279   1.1    martin 	    PCF8591_CTRL_OSCILLATOR;
    280   1.1    martin 
    281   1.1    martin 	iic_acquire_bus(sc->sc_tag, 0);
    282   1.1    martin 	if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
    283   1.1    martin 	    &ctrl, 1, NULL, 0, 0)) {
    284   1.1    martin 		iic_release_bus(sc->sc_tag, 0);
    285   1.1    martin 		return;
    286   1.1    martin 	}
    287  1.10       jdc 	/*
    288  1.10       jdc 	 * Each data byte that we read is the result of the previous request,
    289  1.10       jdc 	 * so read num_channels + 1 and update envsys values from chan + 1.
    290  1.10       jdc 	 */
    291   1.1    martin 	if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
    292   1.1    martin 	    NULL, 0, data, PCF8591_CHANNELS + 1, 0)) {
    293   1.1    martin 		iic_release_bus(sc->sc_tag, 0);
    294   1.1    martin 		return;
    295   1.1    martin 	}
    296   1.1    martin 	iic_release_bus(sc->sc_tag, 0);
    297   1.1    martin 
    298  1.10       jdc 	/* Temperature with/without translation or relative (ADC value) */
    299   1.1    martin 	for (i = 0; i < sc->sc_nchan; i++) {
    300   1.1    martin 		struct ecadc_channel *chp = &sc->sc_channels[i];
    301   1.1    martin 
    302  1.10       jdc 		if (chp->chan_type == PCF8591_TEMP_SENS) {
    303  1.10       jdc 
    304  1.10       jdc 			/* Encode the raw value to use for the fan control */
    305  1.10       jdc 			if (chp->chan_xlate) {
    306  1.10       jdc 				int32_t temp;
    307  1.10       jdc 
    308  1.10       jdc 				temp = 273150000 + 1000000 *
    309  1.10       jdc 				    chp->chan_xlate[data[1 + chp->chan_num]];
    310  1.10       jdc 				temp &= ~0xff;
    311  1.10       jdc 				temp += data[1 + chp->chan_num];
    312  1.10       jdc 				chp->chan_sensor.value_cur = temp;
    313  1.10       jdc 			} else
    314  1.10       jdc 				chp->chan_sensor.value_cur = 273150000 +
    315  1.10       jdc 				    chp->chan_factor * data[1 + chp->chan_num];
    316  1.10       jdc 			chp->chan_sensor.flags |= ENVSYS_FMONLIMITS;
    317  1.10       jdc 		}
    318  1.10       jdc 		if (chp->chan_type == PCF8591_CPU_FAN_CTRL ||
    319  1.10       jdc 		    chp->chan_type == PCF8591_PS_FAN_CTRL)
    320  1.10       jdc 			chp->chan_sensor.value_cur = data[1 + chp->chan_num];
    321   1.1    martin 
    322   1.1    martin 		chp->chan_sensor.state = ENVSYS_SVALID;
    323   1.1    martin 		chp->chan_sensor.flags &= ~ENVSYS_FNEED_REFRESH;
    324   1.1    martin 	}
    325   1.1    martin }
    326   1.1    martin 
    327   1.1    martin static void
    328   1.1    martin ecadc_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
    329  1.10       jdc 		sysmon_envsys_lim_t *limits, u_int32_t *props)
    330   1.1    martin {
    331   1.1    martin 	struct ecadc_softc *sc = sme->sme_cookie;
    332   1.1    martin 	int i;
    333   1.1    martin 
    334   1.1    martin 	for (i = 0; i < sc->sc_nchan; i++) {
    335   1.1    martin 		if (edata != &sc->sc_channels[i].chan_sensor)
    336   1.1    martin 			continue;
    337  1.10       jdc 		if (sc->sc_channels[i].chan_type == PCF8591_TEMP_SENS) {
    338  1.10       jdc 			*props |= PROP_WARNMIN|PROP_WARNMAX|PROP_CRITMAX;
    339  1.10       jdc 			limits->sel_warnmin = sc->sc_channels[i].chan_min;
    340  1.10       jdc 			limits->sel_warnmax = sc->sc_channels[i].chan_warn;
    341  1.10       jdc 			limits->sel_critmax = sc->sc_channels[i].chan_crit;
    342  1.10       jdc 		}
    343   1.1    martin 		return;
    344   1.1    martin 	}
    345   1.1    martin }
    346  1.10       jdc 
    347  1.10       jdc static void
    348  1.10       jdc ecadc_timeout(void *v)
    349  1.10       jdc {
    350  1.10       jdc 	struct ecadc_softc *sc = v;
    351  1.10       jdc 
    352  1.10       jdc 	sysmon_task_queue_sched(0, ecadc_fan_adjust, sc);
    353  1.10       jdc 	callout_reset(&sc->sc_timer, hz*60, ecadc_timeout, sc);
    354  1.10       jdc }
    355  1.10       jdc 
    356  1.10       jdc static bool
    357  1.10       jdc is_cpu_temp(const envsys_data_t *edata)
    358  1.10       jdc {
    359  1.10       jdc 	if (edata->units != ENVSYS_STEMP)
    360  1.10       jdc 		return false;
    361  1.10       jdc 	return strncmp(edata->desc, "CPU", 3) == 0;
    362  1.10       jdc }
    363  1.10       jdc 
    364  1.10       jdc static int
    365  1.10       jdc ecadc_set_fan_speed(struct ecadc_softc *sc, u_int8_t chan, u_int8_t val)
    366  1.10       jdc {
    367  1.10       jdc 	u_int8_t ctrl = PCF8591_CTRL_AUTOINC | PCF8591_CTRL_OSCILLATOR;
    368  1.10       jdc 	int ret;
    369  1.10       jdc 
    370  1.10       jdc 	ctrl |= chan;
    371  1.10       jdc 	iic_acquire_bus(sc->sc_tag, 0);
    372  1.10       jdc 	ret = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
    373  1.10       jdc 	    &ctrl, 1, &val, 1, 0);
    374  1.10       jdc 	if (ret)
    375  1.10       jdc 		aprint_error_dev(sc->sc_dev,
    376  1.10       jdc 		    "error changing fan speed (ch %d)\n", chan);
    377  1.10       jdc 	else
    378  1.10       jdc 		aprint_debug_dev(sc->sc_dev,
    379  1.10       jdc 		    "changed fan speed (ch %d) to 0x%x\n", chan, val);
    380  1.10       jdc 	iic_release_bus(sc->sc_tag, 0);
    381  1.10       jdc 	return ret;
    382  1.10       jdc }
    383  1.10       jdc 
    384  1.10       jdc static void
    385  1.10       jdc ecadc_fan_adjust(void *v)
    386  1.10       jdc {
    387  1.10       jdc 	struct ecadc_softc *sc = v;
    388  1.10       jdc 	int i;
    389  1.10       jdc 	u_int8_t temp, speed;
    390  1.10       jdc 
    391  1.10       jdc 	for (i = 0; i < sc->sc_nchan; i++) {
    392  1.10       jdc 		struct ecadc_channel *chp = &sc->sc_channels[i];
    393  1.10       jdc 
    394  1.10       jdc 		if (chp->chan_type == PCF8591_CPU_FAN_CTRL) {
    395  1.10       jdc 			/* Extract the raw value from the max CPU temp */
    396  1.10       jdc 			temp = sysmon_envsys_get_max_value(is_cpu_temp, true)
    397  1.10       jdc 				& 0xff;
    398  1.10       jdc 			if (!temp) {
    399  1.10       jdc 				aprint_error_dev(sc->sc_dev,
    400  1.10       jdc 				    "skipping temp adjustment"
    401  1.10       jdc 				    " - no sensor values\n");
    402  1.10       jdc 				return;
    403  1.10       jdc 			}
    404  1.10       jdc 			if (temp > XLATE_MAX)
    405  1.10       jdc 				temp = XLATE_MAX;
    406  1.10       jdc 			speed = chp->chan_xlate[temp];
    407  1.10       jdc 			if (speed != chp->chan_speed) {
    408  1.10       jdc 				if (!ecadc_set_fan_speed(sc, chp->chan_num,
    409  1.10       jdc 				    speed))
    410  1.10       jdc 					chp->chan_speed = speed;
    411  1.10       jdc 			}
    412  1.10       jdc 		}
    413  1.10       jdc 	}
    414  1.10       jdc }
    415