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