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