Home | History | Annotate | Line # | Download | only in dev
pcf8591_envctrl.c revision 1.10
      1 /*	$NetBSD: pcf8591_envctrl.c,v 1.10 2020/10/24 15:16:39 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.10 2020/10/24 15:16:39 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 it doesn't work */
    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_error(": 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 		return;
    245 	}
    246 
    247 	if (sc->sc_hastimer) {
    248 		callout_init(&sc->sc_timer, CALLOUT_MPSAFE);
    249 		callout_reset(&sc->sc_timer, hz*20, ecadc_timeout, sc);
    250 	}
    251 
    252 	aprint_naive(": Temp Sensors\n");
    253 	aprint_normal(": %s Temp Sensors (%d channels)\n", ia->ia_name,
    254 	    sc->sc_nchan);
    255 }
    256 
    257 static int
    258 ecadc_detach(device_t self, int flags)
    259 {
    260 	struct ecadc_softc *sc = device_private(self);
    261 	if (sc->sc_hastimer) {
    262 		callout_halt(&sc->sc_timer, NULL);
    263 		callout_destroy(&sc->sc_timer);
    264 	}
    265 
    266 	if (sc->sc_sme != NULL)
    267 		sysmon_envsys_destroy(sc->sc_sme);
    268 
    269 	return 0;
    270 }
    271 
    272 static void
    273 ecadc_refresh(struct sysmon_envsys *sme, envsys_data_t *sensor)
    274 {
    275 	struct ecadc_softc *sc = sme->sme_cookie;
    276 	u_int i;
    277 	u_int8_t data[PCF8591_CHANNELS + 1];
    278 	u_int8_t ctrl = PCF8591_CTRL_CH0 | PCF8591_CTRL_AUTOINC |
    279 	    PCF8591_CTRL_OSCILLATOR;
    280 
    281 	iic_acquire_bus(sc->sc_tag, 0);
    282 	if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
    283 	    &ctrl, 1, NULL, 0, 0)) {
    284 		iic_release_bus(sc->sc_tag, 0);
    285 		return;
    286 	}
    287 	/*
    288 	 * Each data byte that we read is the result of the previous request,
    289 	 * so read num_channels + 1 and update envsys values from chan + 1.
    290 	 */
    291 	if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
    292 	    NULL, 0, data, PCF8591_CHANNELS + 1, 0)) {
    293 		iic_release_bus(sc->sc_tag, 0);
    294 		return;
    295 	}
    296 	iic_release_bus(sc->sc_tag, 0);
    297 
    298 	/* Temperature with/without translation or relative (ADC value) */
    299 	for (i = 0; i < sc->sc_nchan; i++) {
    300 		struct ecadc_channel *chp = &sc->sc_channels[i];
    301 
    302 		if (chp->chan_type == PCF8591_TEMP_SENS) {
    303 
    304 			/* Encode the raw value to use for the fan control */
    305 			if (chp->chan_xlate) {
    306 				int32_t temp;
    307 
    308 				temp = 273150000 + 1000000 *
    309 				    chp->chan_xlate[data[1 + chp->chan_num]];
    310 				temp &= ~0xff;
    311 				temp += data[1 + chp->chan_num];
    312 				chp->chan_sensor.value_cur = temp;
    313 			} else
    314 				chp->chan_sensor.value_cur = 273150000 +
    315 				    chp->chan_factor * data[1 + chp->chan_num];
    316 			chp->chan_sensor.flags |= ENVSYS_FMONLIMITS;
    317 		}
    318 		if (chp->chan_type == PCF8591_CPU_FAN_CTRL ||
    319 		    chp->chan_type == PCF8591_PS_FAN_CTRL)
    320 			chp->chan_sensor.value_cur = data[1 + chp->chan_num];
    321 
    322 		chp->chan_sensor.state = ENVSYS_SVALID;
    323 		chp->chan_sensor.flags &= ~ENVSYS_FNEED_REFRESH;
    324 	}
    325 }
    326 
    327 static void
    328 ecadc_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
    329 		sysmon_envsys_lim_t *limits, u_int32_t *props)
    330 {
    331 	struct ecadc_softc *sc = sme->sme_cookie;
    332 	int i;
    333 
    334 	for (i = 0; i < sc->sc_nchan; i++) {
    335 		if (edata != &sc->sc_channels[i].chan_sensor)
    336 			continue;
    337 		if (sc->sc_channels[i].chan_type == PCF8591_TEMP_SENS) {
    338 			*props |= PROP_WARNMIN|PROP_WARNMAX|PROP_CRITMAX;
    339 			limits->sel_warnmin = sc->sc_channels[i].chan_min;
    340 			limits->sel_warnmax = sc->sc_channels[i].chan_warn;
    341 			limits->sel_critmax = sc->sc_channels[i].chan_crit;
    342 		}
    343 		return;
    344 	}
    345 }
    346 
    347 static void
    348 ecadc_timeout(void *v)
    349 {
    350 	struct ecadc_softc *sc = v;
    351 
    352 	sysmon_task_queue_sched(0, ecadc_fan_adjust, sc);
    353 	callout_reset(&sc->sc_timer, hz*60, ecadc_timeout, sc);
    354 }
    355 
    356 static bool
    357 is_cpu_temp(const envsys_data_t *edata)
    358 {
    359 	if (edata->units != ENVSYS_STEMP)
    360 		return false;
    361 	return strncmp(edata->desc, "CPU", 3) == 0;
    362 }
    363 
    364 static int
    365 ecadc_set_fan_speed(struct ecadc_softc *sc, u_int8_t chan, u_int8_t val)
    366 {
    367 	u_int8_t ctrl = PCF8591_CTRL_AUTOINC | PCF8591_CTRL_OSCILLATOR;
    368 	int ret;
    369 
    370 	ctrl |= chan;
    371 	iic_acquire_bus(sc->sc_tag, 0);
    372 	ret = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
    373 	    &ctrl, 1, &val, 1, 0);
    374 	if (ret)
    375 		aprint_error_dev(sc->sc_dev,
    376 		    "error changing fan speed (ch %d)\n", chan);
    377 	else
    378 		aprint_debug_dev(sc->sc_dev,
    379 		    "changed fan speed (ch %d) to 0x%x\n", chan, val);
    380 	iic_release_bus(sc->sc_tag, 0);
    381 	return ret;
    382 }
    383 
    384 static void
    385 ecadc_fan_adjust(void *v)
    386 {
    387 	struct ecadc_softc *sc = v;
    388 	int i;
    389 	u_int8_t temp, speed;
    390 
    391 	for (i = 0; i < sc->sc_nchan; i++) {
    392 		struct ecadc_channel *chp = &sc->sc_channels[i];
    393 
    394 		if (chp->chan_type == PCF8591_CPU_FAN_CTRL) {
    395 			/* Extract the raw value from the max CPU temp */
    396 			temp = sysmon_envsys_get_max_value(is_cpu_temp, true)
    397 				& 0xff;
    398 			if (!temp) {
    399 				aprint_error_dev(sc->sc_dev,
    400 				    "skipping temp adjustment"
    401 				    " - no sensor values\n");
    402 				return;
    403 			}
    404 			if (temp > XLATE_MAX)
    405 				temp = XLATE_MAX;
    406 			speed = chp->chan_xlate[temp];
    407 			if (speed != chp->chan_speed) {
    408 				if (!ecadc_set_fan_speed(sc, chp->chan_num,
    409 				    speed))
    410 					chp->chan_speed = speed;
    411 			}
    412 		}
    413 	}
    414 }
    415