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