Home | History | Annotate | Line # | Download | only in dev
pcf8591_envctrl.c revision 1.6.38.1
      1  1.6.38.1  pgoyette /*	$NetBSD: pcf8591_envctrl.c,v 1.6.38.1 2018/06/25 07:25:45 pgoyette Exp $	*/
      2       1.1    martin /*	$OpenBSD: pcf8591_envctrl.c,v 1.6 2007/10/25 21:17:20 kettenis Exp $ */
      3       1.1    martin 
      4       1.1    martin /*
      5       1.1    martin  * Copyright (c) 2006 Damien Miller <djm (at) openbsd.org>
      6       1.1    martin  * Copyright (c) 2007 Mark Kettenis <kettenis (at) openbsd.org>
      7       1.1    martin  *
      8       1.1    martin  * Permission to use, copy, modify, and distribute this software for any
      9       1.1    martin  * purpose with or without fee is hereby granted, provided that the above
     10       1.1    martin  * copyright notice and this permission notice appear in all copies.
     11       1.1    martin  *
     12       1.1    martin  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     13       1.1    martin  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     14       1.1    martin  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     15       1.1    martin  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     16       1.1    martin  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     17       1.1    martin  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     18       1.1    martin  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     19       1.1    martin  */
     20       1.1    martin 
     21       1.6       mrg #include <sys/cdefs.h>
     22  1.6.38.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: pcf8591_envctrl.c,v 1.6.38.1 2018/06/25 07:25:45 pgoyette Exp $");
     23       1.6       mrg 
     24       1.1    martin #include <sys/param.h>
     25       1.1    martin #include <sys/systm.h>
     26       1.1    martin #include <sys/device.h>
     27       1.1    martin 
     28       1.1    martin #include <dev/sysmon/sysmonvar.h>
     29       1.1    martin 
     30       1.1    martin #include <dev/ofw/openfirm.h>
     31       1.1    martin #include <dev/i2c/i2cvar.h>
     32       1.1    martin 
     33       1.1    martin #define PCF8591_CHANNELS	4
     34       1.1    martin 
     35       1.1    martin #define PCF8591_CTRL_CH0	0x00
     36       1.1    martin #define PCF8591_CTRL_CH1	0x01
     37       1.1    martin #define PCF8591_CTRL_CH2	0x02
     38       1.1    martin #define PCF8591_CTRL_CH3	0x03
     39       1.1    martin #define PCF8591_CTRL_AUTOINC	0x04
     40       1.1    martin #define PCF8591_CTRL_OSCILLATOR	0x40
     41       1.1    martin 
     42       1.1    martin struct ecadc_channel {
     43       1.1    martin 	u_int		chan_num;
     44       1.1    martin 	envsys_data_t	chan_sensor;
     45       1.1    martin 	u_char		*chan_xlate;
     46       1.1    martin 	int64_t		chan_factor;
     47       1.1    martin 	int64_t		chan_min;
     48       1.1    martin 	int64_t		chan_warn;
     49       1.1    martin 	int64_t		chan_crit;
     50       1.1    martin };
     51       1.1    martin 
     52       1.1    martin struct ecadc_softc {
     53       1.1    martin 	device_t		sc_dev;
     54       1.1    martin 	i2c_tag_t		sc_tag;
     55       1.1    martin 	i2c_addr_t		sc_addr;
     56       1.1    martin 	u_char			sc_ps_xlate[256];
     57       1.1    martin 	u_char			sc_cpu_xlate[256];
     58       1.1    martin 	u_int			sc_nchan;
     59       1.1    martin 	struct ecadc_channel	sc_channels[PCF8591_CHANNELS];
     60       1.1    martin 	struct sysmon_envsys	*sc_sme;
     61       1.1    martin };
     62       1.1    martin 
     63       1.1    martin static int	ecadc_match(device_t, cfdata_t, void *);
     64       1.1    martin static void	ecadc_attach(device_t, device_t, void *);
     65       1.1    martin static void	ecadc_refresh(struct sysmon_envsys *, envsys_data_t *);
     66       1.1    martin static void	ecadc_get_limits(struct sysmon_envsys *, envsys_data_t *,
     67       1.1    martin 			sysmon_envsys_lim_t *, uint32_t *);
     68       1.1    martin 
     69       1.1    martin CFATTACH_DECL_NEW(ecadc, sizeof(struct ecadc_softc),
     70       1.1    martin 	ecadc_match, ecadc_attach, NULL, NULL);
     71       1.1    martin 
     72       1.1    martin static const char * ecadc_compats[] = {
     73       1.1    martin 	"ecadc",
     74       1.1    martin 	NULL
     75       1.1    martin };
     76       1.1    martin 
     77  1.6.38.1  pgoyette static const struct device_compatible_entry ecadc_compat_data[] = {
     78  1.6.38.1  pgoyette 	DEVICE_COMPAT_ENTRY(ecadc_compats),
     79  1.6.38.1  pgoyette 	DEVICE_COMPAT_TERMINATOR
     80  1.6.38.1  pgoyette };
     81  1.6.38.1  pgoyette 
     82       1.1    martin static int
     83       1.1    martin ecadc_match(device_t parent, cfdata_t cf, void *aux)
     84       1.1    martin {
     85       1.1    martin 	struct i2c_attach_args *ia = aux;
     86  1.6.38.1  pgoyette 	int match_result;
     87  1.6.38.1  pgoyette 
     88  1.6.38.1  pgoyette 	if (iic_use_direct_match(ia, cf, ecadc_compat_data, &match_result))
     89  1.6.38.1  pgoyette 		return match_result;
     90       1.1    martin 
     91  1.6.38.1  pgoyette 	/* This driver is direct-config only. */
     92       1.1    martin 
     93       1.1    martin 	return 0;
     94       1.1    martin }
     95       1.1    martin 
     96       1.1    martin static void
     97       1.1    martin ecadc_attach(device_t parent, device_t self, void *aux)
     98       1.1    martin {
     99       1.1    martin 	struct i2c_attach_args *ia = aux;
    100       1.1    martin 	struct ecadc_softc *sc = device_private(self);
    101       1.1    martin 	u_char term[256];
    102       1.1    martin 	u_char *cp, *desc;
    103       1.1    martin 	int64_t minv, warnv, crit, num, den;
    104       1.1    martin 	u_int8_t junk[PCF8591_CHANNELS + 1];
    105       1.1    martin 	envsys_data_t *sensor;
    106       1.1    martin 	int len, error, addr, chan, node = (int)ia->ia_cookie;
    107       1.1    martin 	u_int i;
    108       1.1    martin 
    109       1.1    martin 	sc->sc_dev = self;
    110       1.1    martin 	if ((len = OF_getprop(node, "thermisters", term,
    111       1.1    martin 	    sizeof(term))) < 0) {
    112       1.1    martin 		aprint_error(": couldn't find \"thermisters\" property\n");
    113       1.1    martin 		return;
    114       1.1    martin 	}
    115       1.1    martin 
    116       1.1    martin 	if (OF_getprop(node, "cpu-temp-factors", &sc->sc_cpu_xlate[2],
    117       1.1    martin 	    sizeof(sc->sc_cpu_xlate) - 2) < 0) {
    118       1.1    martin 		aprint_error(": couldn't find \"cpu-temp-factors\" property\n");
    119       1.1    martin 		return;
    120       1.1    martin 	}
    121       1.1    martin 	sc->sc_cpu_xlate[0] = sc->sc_cpu_xlate[1] = sc->sc_cpu_xlate[2];
    122       1.1    martin 
    123       1.1    martin 	/* Only the Sun Enterprise 450 has these. */
    124       1.1    martin 	OF_getprop(node, "ps-temp-factors", &sc->sc_ps_xlate[2],
    125       1.1    martin 	    sizeof(sc->sc_ps_xlate) - 2);
    126       1.1    martin 	sc->sc_ps_xlate[0] = sc->sc_ps_xlate[1] = sc->sc_ps_xlate[2];
    127       1.1    martin 
    128       1.1    martin 	cp = term;
    129       1.1    martin 	while (cp < term + len) {
    130       1.1    martin 		addr = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
    131       1.1    martin 		chan = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
    132       1.1    martin 		minv = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
    133       1.1    martin 		warnv = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
    134       1.1    martin 		crit = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
    135       1.1    martin 		num = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
    136       1.1    martin 		den = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4;
    137       1.1    martin 		desc = cp;
    138       1.1    martin 		while (cp < term + len && *cp++);
    139       1.1    martin 
    140       1.1    martin 		if (addr != (ia->ia_addr << 1))
    141       1.1    martin 			continue;
    142       1.1    martin 
    143       1.1    martin 		if (num == 0 || den == 0)
    144       1.1    martin 			num = den = 1;
    145       1.1    martin 
    146       1.1    martin 		sc->sc_channels[sc->sc_nchan].chan_num = chan;
    147       1.1    martin 
    148       1.1    martin 		sensor = &sc->sc_channels[sc->sc_nchan].chan_sensor;
    149       1.1    martin 		sensor->units = ENVSYS_STEMP;
    150       1.4       jdc 		sensor->flags |= ENVSYS_FMONLIMITS;
    151       1.5  pgoyette 		sensor->state = ENVSYS_SINVALID;
    152       1.1    martin 		strlcpy(sensor->desc, desc, sizeof(sensor->desc));
    153       1.1    martin 
    154       1.1    martin 		if (strncmp(desc, "CPU", 3) == 0)
    155       1.1    martin 			sc->sc_channels[sc->sc_nchan].chan_xlate =
    156       1.1    martin 			    sc->sc_cpu_xlate;
    157       1.1    martin 		else if (strncmp(desc, "PS", 2) == 0)
    158       1.1    martin 			sc->sc_channels[sc->sc_nchan].chan_xlate =
    159       1.1    martin 			    sc->sc_ps_xlate;
    160       1.1    martin 		else
    161       1.1    martin 			sc->sc_channels[sc->sc_nchan].chan_factor =
    162       1.1    martin 			    (1000000 * num) / den;
    163       1.1    martin 		sc->sc_channels[sc->sc_nchan].chan_min =
    164       1.1    martin 		    273150000 + 1000000 * minv;
    165       1.1    martin 		sc->sc_channels[sc->sc_nchan].chan_warn =
    166       1.1    martin 		    273150000 + 1000000 * warnv;
    167       1.1    martin 		sc->sc_channels[sc->sc_nchan].chan_crit =
    168       1.1    martin 		    273150000 + 1000000 * crit;
    169       1.1    martin 		sc->sc_nchan++;
    170       1.1    martin 	}
    171       1.1    martin 
    172       1.1    martin 	sc->sc_tag = ia->ia_tag;
    173       1.1    martin 	sc->sc_addr = ia->ia_addr;
    174       1.1    martin 
    175       1.1    martin 	iic_acquire_bus(sc->sc_tag, 0);
    176       1.1    martin 
    177       1.1    martin 	/* Try a read now, so we can fail if it doesn't work */
    178       1.1    martin 	if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
    179       1.1    martin 	    NULL, 0, junk, sc->sc_nchan + 1, 0)) {
    180       1.1    martin 		aprint_error(": read failed\n");
    181       1.1    martin 		iic_release_bus(sc->sc_tag, 0);
    182       1.1    martin 		return;
    183       1.1    martin 	}
    184       1.1    martin 
    185       1.1    martin 	iic_release_bus(sc->sc_tag, 0);
    186       1.1    martin 
    187       1.1    martin 	/* Hook us into the sysmon_envsys subsystem */
    188       1.1    martin 	sc->sc_sme = sysmon_envsys_create();
    189       1.1    martin 	sc->sc_sme->sme_name = device_xname(self);
    190       1.1    martin 	sc->sc_sme->sme_cookie = sc;
    191       1.1    martin 	sc->sc_sme->sme_refresh = ecadc_refresh;
    192       1.1    martin 	sc->sc_sme->sme_get_limits = ecadc_get_limits;
    193       1.1    martin 
    194       1.1    martin 	/* Initialize sensor data. */
    195       1.1    martin 	for (i = 0; i < sc->sc_nchan; i++)
    196       1.1    martin 		sysmon_envsys_sensor_attach(sc->sc_sme,
    197       1.1    martin 		    &sc->sc_channels[i].chan_sensor);
    198       1.1    martin 
    199       1.1    martin 	error = sysmon_envsys_register(sc->sc_sme);
    200       1.1    martin 	if (error) {
    201       1.1    martin 		aprint_error_dev(self, "error %d registering with sysmon\n",
    202       1.1    martin 			error);
    203       1.1    martin 		sysmon_envsys_destroy(sc->sc_sme);
    204       1.1    martin 		return;
    205       1.1    martin 	}
    206       1.1    martin 
    207       1.1    martin 	aprint_naive(": Temp Sensors\n");
    208       1.3    martin 	aprint_normal(": %s Temp Sensors\n", ia->ia_name);
    209       1.1    martin }
    210       1.1    martin 
    211       1.1    martin static void
    212       1.1    martin ecadc_refresh(struct sysmon_envsys *sme, envsys_data_t *sensor)
    213       1.1    martin {
    214       1.1    martin 	struct ecadc_softc *sc = sme->sme_cookie;
    215       1.1    martin 	u_int i;
    216       1.1    martin 	u_int8_t data[PCF8591_CHANNELS + 1];
    217       1.1    martin 	u_int8_t ctrl = PCF8591_CTRL_CH0 | PCF8591_CTRL_AUTOINC |
    218       1.1    martin 	    PCF8591_CTRL_OSCILLATOR;
    219       1.1    martin 
    220       1.1    martin 	iic_acquire_bus(sc->sc_tag, 0);
    221       1.1    martin 	if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
    222       1.1    martin 	    &ctrl, 1, NULL, 0, 0)) {
    223       1.1    martin 		iic_release_bus(sc->sc_tag, 0);
    224       1.1    martin 		return;
    225       1.1    martin 	}
    226       1.1    martin 	/* NB: first byte out is stale, so read num_channels + 1 */
    227       1.1    martin 	if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
    228       1.1    martin 	    NULL, 0, data, PCF8591_CHANNELS + 1, 0)) {
    229       1.1    martin 		iic_release_bus(sc->sc_tag, 0);
    230       1.1    martin 		return;
    231       1.1    martin 	}
    232       1.1    martin 	iic_release_bus(sc->sc_tag, 0);
    233       1.1    martin 
    234       1.1    martin 	/* We only support temperature channels. */
    235       1.1    martin 	for (i = 0; i < sc->sc_nchan; i++) {
    236       1.1    martin 		struct ecadc_channel *chp = &sc->sc_channels[i];
    237       1.1    martin 
    238       1.1    martin 		if (chp->chan_xlate)
    239       1.1    martin 			chp->chan_sensor.value_cur = 273150000 + 1000000 *
    240       1.1    martin 			    chp->chan_xlate[data[1 + chp->chan_num]];
    241       1.1    martin 		else
    242       1.1    martin 			chp->chan_sensor.value_cur = 273150000 +
    243       1.1    martin 			    chp->chan_factor * data[1 + chp->chan_num];
    244       1.1    martin 
    245       1.1    martin 		chp->chan_sensor.state = ENVSYS_SVALID;
    246       1.1    martin 		chp->chan_sensor.flags &= ~ENVSYS_FNEED_REFRESH;
    247       1.1    martin 		chp->chan_sensor.flags |= ENVSYS_FMONLIMITS;
    248       1.1    martin 	}
    249       1.1    martin }
    250       1.1    martin 
    251       1.1    martin static void
    252       1.1    martin ecadc_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
    253       1.1    martin 		sysmon_envsys_lim_t *limits, uint32_t *props)
    254       1.1    martin {
    255       1.1    martin 	struct ecadc_softc *sc = sme->sme_cookie;
    256       1.1    martin 	int i;
    257       1.1    martin 
    258       1.1    martin 	for (i = 0; i < sc->sc_nchan; i++) {
    259       1.1    martin 		if (edata != &sc->sc_channels[i].chan_sensor)
    260       1.1    martin 			continue;
    261       1.1    martin 		*props |= PROP_WARNMIN|PROP_WARNMAX|PROP_CRITMAX;
    262       1.1    martin 		limits->sel_warnmin = sc->sc_channels[i].chan_min;
    263       1.1    martin 		limits->sel_warnmax = sc->sc_channels[i].chan_warn;
    264       1.1    martin 		limits->sel_critmax = sc->sc_channels[i].chan_crit;
    265       1.1    martin 		return;
    266       1.1    martin 	}
    267       1.1    martin }
    268