Home | History | Annotate | Line # | Download | only in i2c
lm75.c revision 1.25.14.2
      1  1.25.14.2     skrll /*	$NetBSD: lm75.c,v 1.25.14.2 2016/03/19 11:30:09 skrll Exp $	*/
      2        1.1   thorpej 
      3        1.1   thorpej /*
      4        1.1   thorpej  * Copyright (c) 2003 Wasabi Systems, Inc.
      5        1.1   thorpej  * All rights reserved.
      6        1.1   thorpej  *
      7        1.1   thorpej  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8        1.1   thorpej  *
      9        1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     10        1.1   thorpej  * modification, are permitted provided that the following conditions
     11        1.1   thorpej  * are met:
     12        1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     13        1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     14        1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     15        1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     16        1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     17        1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     18        1.1   thorpej  *    must display the following acknowledgement:
     19        1.1   thorpej  *      This product includes software developed for the NetBSD Project by
     20        1.1   thorpej  *      Wasabi Systems, Inc.
     21        1.1   thorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22        1.1   thorpej  *    or promote products derived from this software without specific prior
     23        1.1   thorpej  *    written permission.
     24        1.1   thorpej  *
     25        1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26        1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27        1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28        1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29        1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30        1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31        1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32        1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33        1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34        1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35        1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     36        1.1   thorpej  */
     37        1.1   thorpej 
     38       1.17     lukem #include <sys/cdefs.h>
     39  1.25.14.2     skrll __KERNEL_RCSID(0, "$NetBSD: lm75.c,v 1.25.14.2 2016/03/19 11:30:09 skrll Exp $");
     40       1.17     lukem 
     41        1.1   thorpej #include <sys/param.h>
     42        1.1   thorpej #include <sys/systm.h>
     43        1.1   thorpej #include <sys/device.h>
     44        1.1   thorpej #include <sys/kernel.h>
     45       1.23  macallan #include <sys/sysctl.h>
     46        1.1   thorpej 
     47        1.1   thorpej #include <dev/sysmon/sysmonvar.h>
     48        1.1   thorpej 
     49        1.1   thorpej #include <dev/i2c/i2cvar.h>
     50        1.1   thorpej #include <dev/i2c/lm75reg.h>
     51        1.1   thorpej 
     52        1.1   thorpej struct lmtemp_softc {
     53       1.23  macallan 	device_t sc_dev;
     54        1.1   thorpej 	i2c_tag_t sc_tag;
     55        1.1   thorpej 	int sc_address;
     56        1.1   thorpej 
     57       1.18   xtraeme 	struct sysmon_envsys *sc_sme;
     58       1.16   xtraeme 	envsys_data_t sc_sensor;
     59       1.23  macallan 	int sc_tmax;
     60  1.25.14.2     skrll 	uint32_t sc_smax, sc_smin, sc_scrit;
     61        1.9  kiyohara 
     62  1.25.14.2     skrll 	uint32_t (*sc_lmtemp_decode)(const uint8_t *, int);
     63  1.25.14.2     skrll 	void (*sc_lmtemp_encode)(const uint32_t, uint8_t *, int);
     64        1.1   thorpej };
     65        1.1   thorpej 
     66       1.18   xtraeme static int  lmtemp_match(device_t, cfdata_t, void *);
     67       1.18   xtraeme static void lmtemp_attach(device_t, device_t, void *);
     68        1.1   thorpej 
     69       1.18   xtraeme CFATTACH_DECL_NEW(lmtemp, sizeof(struct lmtemp_softc),
     70        1.1   thorpej 	lmtemp_match, lmtemp_attach, NULL, NULL);
     71        1.1   thorpej 
     72       1.16   xtraeme static void	lmtemp_refresh(struct sysmon_envsys *, envsys_data_t *);
     73       1.18   xtraeme static int	lmtemp_config_write(struct lmtemp_softc *, uint8_t);
     74  1.25.14.2     skrll static int	lmtemp_temp_write(struct lmtemp_softc *, uint8_t, uint32_t,
     75  1.25.14.2     skrll 				int);
     76  1.25.14.2     skrll static int	lmtemp_temp_read(struct lmtemp_softc *, uint8_t, uint32_t *,
     77  1.25.14.2     skrll 				int);
     78  1.25.14.2     skrll static uint32_t lmtemp_decode_lm75(const uint8_t *, int);
     79  1.25.14.2     skrll static uint32_t lmtemp_decode_ds75(const uint8_t *, int);
     80  1.25.14.2     skrll static uint32_t lmtemp_decode_lm77(const uint8_t *, int);
     81  1.25.14.2     skrll static void	lmtemp_encode_lm75(const uint32_t, uint8_t *, int);
     82  1.25.14.2     skrll static void	lmtemp_encode_ds75(const uint32_t, uint8_t *, int);
     83  1.25.14.2     skrll static void	lmtemp_encode_lm77(const uint32_t, uint8_t *, int);
     84  1.25.14.2     skrll static void	lmtemp_getlim_lm75(struct sysmon_envsys *, envsys_data_t *,
     85  1.25.14.2     skrll 				sysmon_envsys_lim_t *, uint32_t *);
     86  1.25.14.2     skrll static void	lmtemp_getlim_lm77(struct sysmon_envsys *, envsys_data_t *,
     87  1.25.14.2     skrll 				sysmon_envsys_lim_t *, uint32_t *);
     88  1.25.14.2     skrll static void	lmtemp_setlim_lm75(struct sysmon_envsys *, envsys_data_t *,
     89  1.25.14.2     skrll 				sysmon_envsys_lim_t *, uint32_t *);
     90  1.25.14.2     skrll static void	lmtemp_setlim_lm77(struct sysmon_envsys *, envsys_data_t *,
     91  1.25.14.2     skrll 				sysmon_envsys_lim_t *, uint32_t *);
     92        1.9  kiyohara 
     93       1.23  macallan static void	lmtemp_setup_sysctl(struct lmtemp_softc *);
     94       1.23  macallan static int	sysctl_lm75_temp(SYSCTLFN_ARGS);
     95       1.21    martin 
     96       1.21    martin static const char * lmtemp_compats[] = {
     97       1.21    martin 	"i2c-lm75",
     98       1.21    martin 	/*
     99       1.21    martin 	 * see XXX in _attach() below: add code once non-lm75 matches are
    100       1.21    martin 	 * added here!
    101       1.21    martin 	 */
    102       1.21    martin 	NULL
    103       1.21    martin };
    104       1.21    martin 
    105        1.9  kiyohara enum {
    106        1.9  kiyohara 	lmtemp_lm75 = 0,
    107        1.9  kiyohara 	lmtemp_ds75,
    108        1.9  kiyohara 	lmtemp_lm77,
    109        1.9  kiyohara };
    110        1.9  kiyohara static const struct {
    111        1.9  kiyohara 	int lmtemp_type;
    112        1.9  kiyohara 	const char *lmtemp_name;
    113        1.9  kiyohara 	int lmtemp_addrmask;
    114        1.9  kiyohara 	int lmtemp_addr;
    115  1.25.14.2     skrll 	uint32_t (*lmtemp_decode)(const uint8_t *, int);
    116  1.25.14.2     skrll 	void (*lmtemp_encode)(const uint32_t, uint8_t *, int);
    117  1.25.14.2     skrll 	void (*lmtemp_getlim)(struct sysmon_envsys *, envsys_data_t *,
    118  1.25.14.2     skrll 		sysmon_envsys_lim_t *, uint32_t *);
    119  1.25.14.2     skrll 	void (*lmtemp_setlim)(struct sysmon_envsys *, envsys_data_t *,
    120  1.25.14.2     skrll 		sysmon_envsys_lim_t *, uint32_t *);
    121        1.9  kiyohara } lmtemptbl[] = {
    122  1.25.14.2     skrll 	{ lmtemp_lm75,	"LM75",	LM75_ADDRMASK,	LM75_ADDR,
    123  1.25.14.2     skrll 	    lmtemp_decode_lm75,	lmtemp_encode_lm75,
    124  1.25.14.2     skrll 	    lmtemp_getlim_lm75,	lmtemp_setlim_lm75 },
    125  1.25.14.2     skrll 	{ lmtemp_ds75,	"DS75",	LM75_ADDRMASK,	LM75_ADDR,
    126  1.25.14.2     skrll 	    lmtemp_decode_ds75,	lmtemp_encode_ds75,
    127  1.25.14.2     skrll 	    lmtemp_getlim_lm75,	lmtemp_setlim_lm75 },
    128  1.25.14.2     skrll 	{ lmtemp_lm77,	"LM77",	LM77_ADDRMASK,	LM77_ADDR,
    129  1.25.14.2     skrll 	    lmtemp_decode_lm77, lmtemp_encode_lm77,
    130  1.25.14.2     skrll 	    lmtemp_getlim_lm77,	lmtemp_setlim_lm77 },
    131  1.25.14.2     skrll 	{ -1,		NULL,	 0,		0,
    132  1.25.14.2     skrll 	    NULL,		NULL,
    133  1.25.14.2     skrll 	    NULL,		NULL }
    134        1.9  kiyohara };
    135        1.1   thorpej 
    136        1.1   thorpej static int
    137       1.18   xtraeme lmtemp_match(device_t parent, cfdata_t cf, void *aux)
    138        1.1   thorpej {
    139        1.1   thorpej 	struct i2c_attach_args *ia = aux;
    140        1.9  kiyohara 	int i;
    141        1.9  kiyohara 
    142       1.21    martin 	if (ia->ia_name == NULL) {
    143       1.21    martin 		/*
    144       1.21    martin 		 * Indirect config - not much we can do!
    145       1.21    martin 		 */
    146       1.21    martin 		for (i = 0; lmtemptbl[i].lmtemp_type != -1 ; i++)
    147       1.21    martin 			if (lmtemptbl[i].lmtemp_type == cf->cf_flags)
    148       1.21    martin 				break;
    149       1.21    martin 		if (lmtemptbl[i].lmtemp_type == -1)
    150       1.21    martin 			return 0;
    151       1.21    martin 
    152       1.21    martin 		if ((ia->ia_addr & lmtemptbl[i].lmtemp_addrmask) ==
    153       1.21    martin 		    lmtemptbl[i].lmtemp_addr)
    154       1.21    martin 			return 1;
    155       1.21    martin 	} else {
    156       1.21    martin 		/*
    157       1.21    martin 		 * Direct config - match via the list of compatible
    158  1.25.14.1     skrll 		 * hardware or simply match the device name.
    159       1.21    martin 		 */
    160  1.25.14.1     skrll 		if (ia->ia_ncompat > 0) {
    161  1.25.14.1     skrll 			if (iic_compat_match(ia, lmtemp_compats))
    162  1.25.14.1     skrll 				return 1;
    163  1.25.14.1     skrll 		} else {
    164  1.25.14.1     skrll 			if (strcmp(ia->ia_name, "lmtemp") == 0)
    165  1.25.14.1     skrll 				return 1;
    166  1.25.14.1     skrll 		}
    167       1.21    martin 	}
    168       1.21    martin 
    169        1.1   thorpej 
    170       1.18   xtraeme 	return 0;
    171        1.1   thorpej }
    172        1.1   thorpej 
    173        1.1   thorpej static void
    174       1.18   xtraeme lmtemp_attach(device_t parent, device_t self, void *aux)
    175        1.1   thorpej {
    176        1.6   thorpej 	struct lmtemp_softc *sc = device_private(self);
    177        1.1   thorpej 	struct i2c_attach_args *ia = aux;
    178        1.9  kiyohara 	int i;
    179        1.9  kiyohara 
    180       1.23  macallan 	sc->sc_dev = self;
    181       1.21    martin 	if (ia->ia_name == NULL) {
    182       1.21    martin 		for (i = 0; lmtemptbl[i].lmtemp_type != -1 ; i++)
    183       1.21    martin 			if (lmtemptbl[i].lmtemp_type ==
    184       1.21    martin 			    device_cfdata(self)->cf_flags)
    185       1.21    martin 				break;
    186       1.21    martin 	} else {
    187       1.21    martin 		/* XXX - add code when adding other direct matches! */
    188       1.21    martin 		i = 0;
    189       1.21    martin 	}
    190        1.1   thorpej 
    191        1.1   thorpej 	sc->sc_tag = ia->ia_tag;
    192        1.1   thorpej 	sc->sc_address = ia->ia_addr;
    193        1.1   thorpej 
    194        1.1   thorpej 	aprint_naive(": Temperature Sensor\n");
    195       1.21    martin 	if (ia->ia_name) {
    196       1.21    martin 		aprint_normal(": %s %s Temperature Sensor\n", ia->ia_name,
    197       1.21    martin 			lmtemptbl[i].lmtemp_name);
    198       1.21    martin 	} else {
    199       1.21    martin 		aprint_normal(": %s Temperature Sensor\n",
    200       1.21    martin 			lmtemptbl[i].lmtemp_name);
    201       1.21    martin 	}
    202        1.1   thorpej 
    203  1.25.14.2     skrll 	sc->sc_lmtemp_decode = lmtemptbl[i].lmtemp_decode;
    204  1.25.14.2     skrll 	sc->sc_lmtemp_encode = lmtemptbl[i].lmtemp_encode;
    205  1.25.14.2     skrll 
    206  1.25.14.2     skrll 	iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
    207  1.25.14.2     skrll 
    208  1.25.14.2     skrll 	/* Read temperature limit(s) and remember initial value(s). */
    209  1.25.14.2     skrll 	if (i == lmtemp_lm77) {
    210  1.25.14.2     skrll 		if (lmtemp_temp_read(sc, LM77_REG_TCRIT_SET_POINT,
    211  1.25.14.2     skrll 		    &sc->sc_scrit, 1) != 0) {
    212  1.25.14.2     skrll 			aprint_error_dev(self,
    213  1.25.14.2     skrll 			    "unable to read low register\n");
    214  1.25.14.2     skrll 			iic_release_bus(sc->sc_tag, I2C_F_POLL);
    215  1.25.14.2     skrll 			return;
    216  1.25.14.2     skrll 		}
    217  1.25.14.2     skrll 		if (lmtemp_temp_read(sc, LM77_REG_TLOW_SET_POINT,
    218  1.25.14.2     skrll 		    &sc->sc_smin, 1) != 0) {
    219  1.25.14.2     skrll 			aprint_error_dev(self,
    220  1.25.14.2     skrll 			    "unable to read low register\n");
    221  1.25.14.2     skrll 			iic_release_bus(sc->sc_tag, I2C_F_POLL);
    222  1.25.14.2     skrll 			return;
    223  1.25.14.2     skrll 		}
    224  1.25.14.2     skrll 		if (lmtemp_temp_read(sc, LM77_REG_THIGH_SET_POINT,
    225  1.25.14.2     skrll 		    &sc->sc_smax, 1) != 0) {
    226  1.25.14.2     skrll 			aprint_error_dev(self,
    227  1.25.14.2     skrll 			    "unable to read high register\n");
    228  1.25.14.2     skrll 			iic_release_bus(sc->sc_tag, I2C_F_POLL);
    229  1.25.14.2     skrll 			return;
    230  1.25.14.2     skrll 		}
    231  1.25.14.2     skrll 	} else {	/* LM75 or compatible */
    232  1.25.14.2     skrll 		if (lmtemp_temp_read(sc, LM75_REG_TOS_SET_POINT,
    233  1.25.14.2     skrll 		    &sc->sc_smax, 1) != 0) {
    234  1.25.14.2     skrll 			aprint_error_dev(self, "unable to read Tos register\n");
    235  1.25.14.2     skrll 			iic_release_bus(sc->sc_tag, I2C_F_POLL);
    236  1.25.14.2     skrll 			return;
    237  1.25.14.2     skrll 		}
    238  1.25.14.2     skrll 	}
    239  1.25.14.2     skrll 	sc->sc_tmax = sc->sc_smax;
    240  1.25.14.2     skrll 
    241       1.23  macallan 	if (i == lmtemp_lm75)
    242       1.23  macallan 		lmtemp_setup_sysctl(sc);
    243       1.23  macallan 
    244        1.1   thorpej 	/* Set the configuration of the LM75 to defaults. */
    245       1.23  macallan 	if (lmtemp_config_write(sc, LM75_CONFIG_FAULT_QUEUE_4) != 0) {
    246       1.18   xtraeme 		aprint_error_dev(self, "unable to write config register\n");
    247        1.1   thorpej 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
    248        1.1   thorpej 		return;
    249        1.1   thorpej 	}
    250        1.1   thorpej 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    251        1.1   thorpej 
    252       1.16   xtraeme 	sc->sc_sme = sysmon_envsys_create();
    253        1.1   thorpej 	/* Initialize sensor data. */
    254       1.16   xtraeme 	sc->sc_sensor.units =  ENVSYS_STEMP;
    255       1.22  pgoyette 	sc->sc_sensor.state =  ENVSYS_SINVALID;
    256  1.25.14.2     skrll 	sc->sc_sensor.flags =  ENVSYS_FMONLIMITS;
    257       1.21    martin 	(void)strlcpy(sc->sc_sensor.desc,
    258       1.21    martin 	    ia->ia_name? ia->ia_name : device_xname(self),
    259       1.18   xtraeme 	    sizeof(sc->sc_sensor.desc));
    260       1.16   xtraeme 	if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) {
    261       1.16   xtraeme 		sysmon_envsys_destroy(sc->sc_sme);
    262       1.16   xtraeme 		return;
    263       1.16   xtraeme 	}
    264        1.1   thorpej 
    265        1.7       riz 	/* Hook into system monitor. */
    266       1.18   xtraeme 	sc->sc_sme->sme_name = device_xname(self);
    267       1.16   xtraeme 	sc->sc_sme->sme_cookie = sc;
    268       1.16   xtraeme 	sc->sc_sme->sme_refresh = lmtemp_refresh;
    269  1.25.14.2     skrll 	sc->sc_sme->sme_get_limits = lmtemptbl[i].lmtemp_getlim;
    270  1.25.14.2     skrll 	sc->sc_sme->sme_set_limits = lmtemptbl[i].lmtemp_setlim;
    271        1.1   thorpej 
    272       1.16   xtraeme 	if (sysmon_envsys_register(sc->sc_sme)) {
    273       1.18   xtraeme 		aprint_error_dev(self, "unable to register with sysmon\n");
    274       1.16   xtraeme 		sysmon_envsys_destroy(sc->sc_sme);
    275       1.16   xtraeme 	}
    276        1.1   thorpej }
    277        1.1   thorpej 
    278        1.1   thorpej static int
    279        1.1   thorpej lmtemp_config_write(struct lmtemp_softc *sc, uint8_t val)
    280        1.1   thorpej {
    281        1.1   thorpej 	uint8_t cmdbuf[2];
    282        1.1   thorpej 
    283        1.1   thorpej 	cmdbuf[0] = LM75_REG_CONFIG;
    284        1.1   thorpej 	cmdbuf[1] = val;
    285        1.1   thorpej 
    286       1.18   xtraeme 	return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    287       1.18   xtraeme 	    sc->sc_address, cmdbuf, 1, &cmdbuf[1], 1, I2C_F_POLL);
    288        1.1   thorpej }
    289        1.1   thorpej 
    290        1.1   thorpej static int
    291  1.25.14.2     skrll lmtemp_temp_write(struct lmtemp_softc *sc, uint8_t reg, uint32_t val, int degc)
    292       1.23  macallan {
    293       1.23  macallan 	uint8_t cmdbuf[3];
    294       1.23  macallan 
    295       1.23  macallan 	cmdbuf[0] = reg;
    296  1.25.14.2     skrll 	sc->sc_lmtemp_encode(val, &cmdbuf[1], degc);
    297       1.23  macallan 
    298       1.23  macallan 	return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    299       1.23  macallan 	    sc->sc_address, cmdbuf, 1, &cmdbuf[1], 2, I2C_F_POLL);
    300       1.23  macallan }
    301       1.23  macallan 
    302       1.23  macallan static int
    303  1.25.14.2     skrll lmtemp_temp_read(struct lmtemp_softc *sc, uint8_t which, uint32_t *valp,
    304  1.25.14.2     skrll     int degc)
    305        1.1   thorpej {
    306        1.2       scw 	int error;
    307        1.1   thorpej 	uint8_t cmdbuf[1];
    308        1.1   thorpej 	uint8_t buf[LM75_TEMP_LEN];
    309        1.1   thorpej 
    310        1.1   thorpej 	cmdbuf[0] = which;
    311        1.1   thorpej 
    312        1.1   thorpej 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    313        1.1   thorpej 	    sc->sc_address, cmdbuf, 1, buf, LM75_TEMP_LEN, 0);
    314        1.1   thorpej 	if (error)
    315       1.18   xtraeme 		return error;
    316        1.1   thorpej 
    317  1.25.14.2     skrll 	*valp = sc->sc_lmtemp_decode(buf, degc);
    318       1.18   xtraeme 	return 0;
    319        1.1   thorpej }
    320        1.1   thorpej 
    321        1.1   thorpej static void
    322        1.1   thorpej lmtemp_refresh_sensor_data(struct lmtemp_softc *sc)
    323        1.1   thorpej {
    324        1.1   thorpej 	uint32_t val;
    325        1.1   thorpej 	int error;
    326        1.1   thorpej 
    327  1.25.14.2     skrll 	error = lmtemp_temp_read(sc, LM75_REG_TEMP, &val, 0);
    328        1.1   thorpej 	if (error) {
    329        1.1   thorpej #if 0
    330       1.25       chs 		aprint_error_dev(sc->sc_dev, "unable to read temperature, error = %d\n",
    331       1.19    cegger 		    error);
    332        1.1   thorpej #endif
    333       1.16   xtraeme 		sc->sc_sensor.state = ENVSYS_SINVALID;
    334        1.1   thorpej 		return;
    335        1.1   thorpej 	}
    336        1.1   thorpej 
    337       1.16   xtraeme 	sc->sc_sensor.value_cur = val;
    338       1.16   xtraeme 	sc->sc_sensor.state = ENVSYS_SVALID;
    339        1.1   thorpej }
    340        1.1   thorpej 
    341       1.16   xtraeme static void
    342       1.16   xtraeme lmtemp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    343        1.1   thorpej {
    344        1.1   thorpej 	struct lmtemp_softc *sc = sme->sme_cookie;
    345        1.1   thorpej 
    346        1.1   thorpej 	iic_acquire_bus(sc->sc_tag, 0);	/* also locks our instance */
    347        1.1   thorpej 	lmtemp_refresh_sensor_data(sc);
    348        1.1   thorpej 	iic_release_bus(sc->sc_tag, 0);	/* also unlocks our instance */
    349        1.1   thorpej }
    350        1.2       scw 
    351  1.25.14.2     skrll static void
    352  1.25.14.2     skrll lmtemp_getlim_lm75(struct sysmon_envsys *sme, envsys_data_t *edata,
    353  1.25.14.2     skrll     sysmon_envsys_lim_t *limits, uint32_t *props)
    354  1.25.14.2     skrll {
    355  1.25.14.2     skrll 	struct lmtemp_softc *sc = sme->sme_cookie;
    356  1.25.14.2     skrll 	uint32_t val;
    357  1.25.14.2     skrll 
    358  1.25.14.2     skrll 	*props &= ~(PROP_CRITMAX);
    359  1.25.14.2     skrll 
    360  1.25.14.2     skrll 	iic_acquire_bus(sc->sc_tag, 0);
    361  1.25.14.2     skrll 	if (lmtemp_temp_read(sc, LM75_REG_TOS_SET_POINT, &val, 0) == 0) {
    362  1.25.14.2     skrll 		limits->sel_critmax = val;
    363  1.25.14.2     skrll 		*props |= PROP_CRITMAX;
    364  1.25.14.2     skrll 	}
    365  1.25.14.2     skrll 	iic_release_bus(sc->sc_tag, 0);
    366  1.25.14.2     skrll }
    367  1.25.14.2     skrll 
    368  1.25.14.2     skrll static void
    369  1.25.14.2     skrll lmtemp_getlim_lm77(struct sysmon_envsys *sme, envsys_data_t *edata,
    370  1.25.14.2     skrll     sysmon_envsys_lim_t *limits, uint32_t *props)
    371  1.25.14.2     skrll {
    372  1.25.14.2     skrll 	struct lmtemp_softc *sc = sme->sme_cookie;
    373  1.25.14.2     skrll 	uint32_t val;
    374  1.25.14.2     skrll 
    375  1.25.14.2     skrll 	*props &= ~(PROP_CRITMAX | PROP_WARNMAX | PROP_WARNMIN);
    376  1.25.14.2     skrll 
    377  1.25.14.2     skrll 	iic_acquire_bus(sc->sc_tag, 0);
    378  1.25.14.2     skrll 	if (lmtemp_temp_read(sc, LM77_REG_TCRIT_SET_POINT, &val, 0) == 0) {
    379  1.25.14.2     skrll 		limits->sel_critmax = val;
    380  1.25.14.2     skrll 		*props |= PROP_CRITMAX;
    381  1.25.14.2     skrll 	}
    382  1.25.14.2     skrll 	if (lmtemp_temp_read(sc, LM77_REG_THIGH_SET_POINT, &val, 0) == 0) {
    383  1.25.14.2     skrll 		limits->sel_warnmax = val;
    384  1.25.14.2     skrll 		*props |= PROP_WARNMAX;
    385  1.25.14.2     skrll 	}
    386  1.25.14.2     skrll 	if (lmtemp_temp_read(sc, LM77_REG_TLOW_SET_POINT, &val, 0) == 0) {
    387  1.25.14.2     skrll 		limits->sel_warnmin = val;
    388  1.25.14.2     skrll 		*props |= PROP_WARNMIN;
    389  1.25.14.2     skrll 	}
    390  1.25.14.2     skrll 	iic_release_bus(sc->sc_tag, 0);
    391  1.25.14.2     skrll }
    392  1.25.14.2     skrll 
    393  1.25.14.2     skrll static void
    394  1.25.14.2     skrll lmtemp_setlim_lm75(struct sysmon_envsys *sme, envsys_data_t *edata,
    395  1.25.14.2     skrll     sysmon_envsys_lim_t *limits, uint32_t *props)
    396  1.25.14.2     skrll {
    397  1.25.14.2     skrll 	struct lmtemp_softc *sc = sme->sme_cookie;
    398  1.25.14.2     skrll 	int32_t limit;
    399  1.25.14.2     skrll 
    400  1.25.14.2     skrll 	if (*props & PROP_CRITMAX) {
    401  1.25.14.2     skrll 		if (limits == NULL)	/* Restore defaults */
    402  1.25.14.2     skrll 			limit = sc->sc_smax;
    403  1.25.14.2     skrll 		else
    404  1.25.14.2     skrll 			limit = limits->sel_critmax;
    405  1.25.14.2     skrll 		iic_acquire_bus(sc->sc_tag, 0);
    406  1.25.14.2     skrll 		lmtemp_temp_write(sc, LM75_REG_THYST_SET_POINT,
    407  1.25.14.2     skrll 		    limit - 5000000, 0);
    408  1.25.14.2     skrll 		lmtemp_temp_write(sc, LM75_REG_TOS_SET_POINT, limit, 0);
    409  1.25.14.2     skrll 		iic_release_bus(sc->sc_tag, 0);
    410  1.25.14.2     skrll 
    411  1.25.14.2     skrll 		/* Synchronise sysctl */
    412  1.25.14.2     skrll 		sc->sc_tmax = (limit - 273150000) / 1000000;
    413  1.25.14.2     skrll 	}
    414  1.25.14.2     skrll }
    415  1.25.14.2     skrll 
    416  1.25.14.2     skrll static void
    417  1.25.14.2     skrll lmtemp_setlim_lm77(struct sysmon_envsys *sme, envsys_data_t *edata,
    418  1.25.14.2     skrll     sysmon_envsys_lim_t *limits, uint32_t *props)
    419  1.25.14.2     skrll {
    420  1.25.14.2     skrll 	struct lmtemp_softc *sc = sme->sme_cookie;
    421  1.25.14.2     skrll 	int32_t limit;
    422  1.25.14.2     skrll 
    423  1.25.14.2     skrll 	iic_acquire_bus(sc->sc_tag, 0);
    424  1.25.14.2     skrll 	if (*props & PROP_CRITMAX) {
    425  1.25.14.2     skrll 		if (limits == NULL)	/* Restore defaults */
    426  1.25.14.2     skrll 			limit = sc->sc_scrit;
    427  1.25.14.2     skrll 		else
    428  1.25.14.2     skrll 			limit = limits->sel_critmax;
    429  1.25.14.2     skrll 		lmtemp_temp_write(sc, LM77_REG_TCRIT_SET_POINT, limit, 0);
    430  1.25.14.2     skrll 	}
    431  1.25.14.2     skrll 	if (*props & PROP_WARNMAX) {
    432  1.25.14.2     skrll 		if (limits == NULL)	/* Restore defaults */
    433  1.25.14.2     skrll 			limit = sc->sc_smax;
    434  1.25.14.2     skrll 		else
    435  1.25.14.2     skrll 			limit = limits->sel_warnmax;
    436  1.25.14.2     skrll 		lmtemp_temp_write(sc, LM77_REG_THIGH_SET_POINT, limit, 0);
    437  1.25.14.2     skrll 	}
    438  1.25.14.2     skrll 	if (*props & PROP_WARNMIN) {
    439  1.25.14.2     skrll 		if (limits == NULL)	/* Restore defaults */
    440  1.25.14.2     skrll 			limit = sc->sc_smin;
    441  1.25.14.2     skrll 		else
    442  1.25.14.2     skrll 			limit = limits->sel_warnmin;
    443  1.25.14.2     skrll 		lmtemp_temp_write(sc, LM77_REG_TLOW_SET_POINT, limit, 0);
    444  1.25.14.2     skrll 	}
    445  1.25.14.2     skrll 	iic_release_bus(sc->sc_tag, 0);
    446  1.25.14.2     skrll }
    447  1.25.14.2     skrll 
    448        1.2       scw static uint32_t
    449  1.25.14.2     skrll lmtemp_decode_lm75(const uint8_t *buf, int degc)
    450        1.2       scw {
    451       1.20    briggs 	int temp;
    452        1.2       scw 	uint32_t val;
    453        1.2       scw 
    454       1.20    briggs 	/*
    455       1.20    briggs 	 * LM75 temps are the most-significant 9 bits of a 16-bit reg.
    456       1.20    briggs 	 * sign-extend the MSB and add in the 0.5 from the LSB
    457       1.20    briggs 	 */
    458       1.20    briggs 	temp = (int8_t) buf[0];
    459       1.20    briggs 	temp = (temp << 1) + ((buf[1] >> 7) & 0x1);
    460        1.2       scw 
    461  1.25.14.2     skrll 	/* Temp is given in 1/2 deg. C, we convert to C or uK. */
    462  1.25.14.2     skrll 	if (degc)
    463  1.25.14.2     skrll 		val = temp / 2;
    464  1.25.14.2     skrll 	else
    465  1.25.14.2     skrll 		val = temp * 500000 + 273150000;
    466        1.2       scw 
    467       1.18   xtraeme 	return val;
    468        1.2       scw }
    469        1.2       scw 
    470        1.2       scw static uint32_t
    471  1.25.14.2     skrll lmtemp_decode_ds75(const uint8_t *buf, int degc)
    472        1.2       scw {
    473        1.2       scw 	int temp;
    474        1.2       scw 
    475        1.2       scw 	/*
    476        1.2       scw 	 * Sign-extend the MSB byte, and add in the fractions of a
    477        1.7       riz 	 * degree contained in the LSB (precision 1/16th DegC).
    478        1.2       scw 	 */
    479        1.2       scw 	temp = (int8_t)buf[0];
    480        1.2       scw 	temp = (temp << 4) | ((buf[1] >> 4) & 0xf);
    481        1.2       scw 
    482        1.2       scw 	/*
    483  1.25.14.2     skrll 	 * Conversion to C or uK is simple.
    484        1.2       scw 	 */
    485  1.25.14.2     skrll 	if (degc)
    486  1.25.14.2     skrll 		return temp / 16;
    487  1.25.14.2     skrll 	else
    488  1.25.14.2     skrll 		return (temp * 62500 + 273150000);
    489        1.2       scw }
    490        1.2       scw 
    491        1.9  kiyohara static uint32_t
    492  1.25.14.2     skrll lmtemp_decode_lm77(const uint8_t *buf, int degc)
    493        1.9  kiyohara {
    494        1.9  kiyohara 	int temp;
    495        1.9  kiyohara 	uint32_t val;
    496        1.9  kiyohara 
    497        1.9  kiyohara 	/*
    498        1.9  kiyohara 	 * Describe each bits of temperature registers on LM77.
    499        1.9  kiyohara 	 *   D15 - D12:	Sign
    500        1.9  kiyohara 	 *   D11 - D3 :	Bit8(MSB) - Bit0
    501        1.9  kiyohara 	 */
    502        1.9  kiyohara 	temp = (int8_t)buf[0];
    503       1.10  kiyohara 	temp = (temp << 5) | ((buf[1] >> 3) & 0x1f);
    504        1.9  kiyohara 
    505  1.25.14.2     skrll 	/* Temp is given in 1/2 deg. C, we convert to C or uK. */
    506  1.25.14.2     skrll 	if (degc)
    507  1.25.14.2     skrll 		val = temp / 2;
    508  1.25.14.2     skrll 	else
    509  1.25.14.2     skrll 		val = temp * 500000 + 273150000;
    510        1.9  kiyohara 
    511       1.18   xtraeme 	return val;
    512        1.9  kiyohara }
    513        1.9  kiyohara 
    514  1.25.14.2     skrll static void lmtemp_encode_lm75(const uint32_t val, uint8_t *buf, int degc)
    515  1.25.14.2     skrll {
    516  1.25.14.2     skrll 	int temp;
    517  1.25.14.2     skrll 
    518  1.25.14.2     skrll 	/* Convert from C or uK to register format */
    519  1.25.14.2     skrll 	if (degc)
    520  1.25.14.2     skrll 		temp = val * 2;
    521  1.25.14.2     skrll 	else
    522  1.25.14.2     skrll 		temp = (val - 273150000) / 500000;
    523  1.25.14.2     skrll 	buf[0] = (temp >> 1) & 0xff;
    524  1.25.14.2     skrll 	buf[1] = (temp & 1) << 7;
    525  1.25.14.2     skrll }
    526  1.25.14.2     skrll 
    527  1.25.14.2     skrll static void lmtemp_encode_ds75(const uint32_t val, uint8_t *buf, int degc)
    528  1.25.14.2     skrll {
    529  1.25.14.2     skrll 	int temp;
    530  1.25.14.2     skrll 
    531  1.25.14.2     skrll 	/* Convert from C or uK to register format */
    532  1.25.14.2     skrll 	if (degc)
    533  1.25.14.2     skrll 		temp = val * 16;
    534  1.25.14.2     skrll 	else
    535  1.25.14.2     skrll 		temp = (val - 273150000) / 62500;
    536  1.25.14.2     skrll 	buf[0] = (temp >> 4) & 0xff;
    537  1.25.14.2     skrll 	buf[1] = (temp & 0xf) << 4;
    538  1.25.14.2     skrll }
    539  1.25.14.2     skrll 
    540  1.25.14.2     skrll static void lmtemp_encode_lm77(const uint32_t val, uint8_t *buf, int degc)
    541  1.25.14.2     skrll {
    542  1.25.14.2     skrll 	int temp;
    543  1.25.14.2     skrll 
    544  1.25.14.2     skrll 	/* Convert from C or uK to register format */
    545  1.25.14.2     skrll 	if (degc)
    546  1.25.14.2     skrll 		temp = val * 2;
    547  1.25.14.2     skrll 	else
    548  1.25.14.2     skrll 		temp = (val - 273150000) / 500000;
    549  1.25.14.2     skrll 	buf[0] = (temp >> 5) & 0xff;
    550  1.25.14.2     skrll 	buf[1] = (temp & 0x1f) << 3;
    551  1.25.14.2     skrll }
    552  1.25.14.2     skrll 
    553       1.23  macallan static void
    554       1.23  macallan lmtemp_setup_sysctl(struct lmtemp_softc *sc)
    555       1.23  macallan {
    556       1.23  macallan 	const struct sysctlnode *me = NULL, *node = NULL;
    557       1.23  macallan 
    558       1.23  macallan 	sysctl_createv(NULL, 0, NULL, &me,
    559       1.23  macallan 	    CTLFLAG_READWRITE,
    560       1.23  macallan 	    CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
    561       1.23  macallan 	    NULL, 0, NULL, 0,
    562       1.23  macallan 	    CTL_MACHDEP, CTL_CREATE, CTL_EOL);
    563       1.23  macallan 
    564       1.23  macallan 	sysctl_createv(NULL, 0, NULL, &node,
    565       1.23  macallan 	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
    566       1.23  macallan 	    CTLTYPE_INT, "temp", "Threshold temperature",
    567       1.24       dsl 	    sysctl_lm75_temp, 1, (void *)sc, 0,
    568       1.23  macallan 	    CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL);
    569       1.23  macallan }
    570       1.23  macallan 
    571       1.23  macallan static int
    572       1.23  macallan sysctl_lm75_temp(SYSCTLFN_ARGS)
    573       1.23  macallan {
    574       1.23  macallan 	struct sysctlnode node = *rnode;
    575       1.23  macallan 	struct lmtemp_softc *sc = node.sysctl_data;
    576       1.23  macallan 	int temp;
    577       1.23  macallan 
    578       1.23  macallan 	if (newp) {
    579       1.23  macallan 
    580       1.23  macallan 		/* we're asked to write */
    581       1.23  macallan 		node.sysctl_data = &sc->sc_tmax;
    582       1.23  macallan 		if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
    583       1.23  macallan 
    584       1.23  macallan 			temp = *(int *)node.sysctl_data;
    585       1.23  macallan 			sc->sc_tmax = temp;
    586       1.23  macallan 			iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
    587       1.23  macallan 			lmtemp_temp_write(sc, LM75_REG_THYST_SET_POINT,
    588  1.25.14.2     skrll 			    sc->sc_tmax - 5, 1);
    589       1.23  macallan 			lmtemp_temp_write(sc, LM75_REG_TOS_SET_POINT,
    590  1.25.14.2     skrll 			    sc->sc_tmax, 1);
    591       1.23  macallan 			iic_release_bus(sc->sc_tag, I2C_F_POLL);
    592  1.25.14.2     skrll 
    593  1.25.14.2     skrll 			/* Synchronise envsys - calls lmtemp_getlim_lm75() */
    594  1.25.14.2     skrll 			sysmon_envsys_update_limits(sc->sc_sme, &sc->sc_sensor);
    595       1.23  macallan 			return 0;
    596       1.23  macallan 		}
    597       1.23  macallan 		return EINVAL;
    598       1.23  macallan 	} else {
    599       1.23  macallan 
    600       1.23  macallan 		node.sysctl_data = &sc->sc_tmax;
    601       1.23  macallan 		node.sysctl_size = 4;
    602       1.23  macallan 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    603       1.23  macallan 	}
    604       1.23  macallan 
    605       1.23  macallan 	return 0;
    606       1.23  macallan }
    607       1.23  macallan 
    608       1.23  macallan SYSCTL_SETUP(sysctl_lmtemp_setup, "sysctl lmtemp subtree setup")
    609       1.23  macallan {
    610       1.23  macallan 
    611       1.23  macallan 	sysctl_createv(NULL, 0, NULL, NULL,
    612       1.23  macallan 		       CTLFLAG_PERMANENT,
    613       1.23  macallan 		       CTLTYPE_NODE, "machdep", NULL,
    614       1.23  macallan 		       NULL, 0, NULL, 0,
    615       1.23  macallan 		       CTL_MACHDEP, CTL_EOL);
    616       1.23  macallan }
    617       1.23  macallan 
    618       1.23  macallan 
    619