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