Home | History | Annotate | Line # | Download | only in i2c
lm75.c revision 1.21
      1  1.21    martin /*	$NetBSD: lm75.c,v 1.21 2010/02/28 11:36:27 martin 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.21    martin __KERNEL_RCSID(0, "$NetBSD: lm75.c,v 1.21 2010/02/28 11:36:27 martin 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.1   thorpej 
     46   1.1   thorpej #include <dev/sysmon/sysmonvar.h>
     47   1.1   thorpej 
     48   1.1   thorpej #include <dev/i2c/i2cvar.h>
     49   1.1   thorpej #include <dev/i2c/lm75reg.h>
     50   1.1   thorpej 
     51   1.1   thorpej struct lmtemp_softc {
     52   1.1   thorpej 	i2c_tag_t sc_tag;
     53   1.1   thorpej 	int sc_address;
     54   1.1   thorpej 
     55  1.18   xtraeme 	struct sysmon_envsys *sc_sme;
     56  1.16   xtraeme 	envsys_data_t sc_sensor;
     57   1.9  kiyohara 
     58   1.9  kiyohara 	uint32_t (*sc_lmtemp_decode)(const uint8_t *);
     59   1.1   thorpej };
     60   1.1   thorpej 
     61  1.18   xtraeme static int  lmtemp_match(device_t, cfdata_t, void *);
     62  1.18   xtraeme static void lmtemp_attach(device_t, device_t, void *);
     63   1.1   thorpej 
     64  1.18   xtraeme CFATTACH_DECL_NEW(lmtemp, sizeof(struct lmtemp_softc),
     65   1.1   thorpej 	lmtemp_match, lmtemp_attach, NULL, NULL);
     66   1.1   thorpej 
     67  1.16   xtraeme static void	lmtemp_refresh(struct sysmon_envsys *, envsys_data_t *);
     68   1.1   thorpej 
     69  1.18   xtraeme static int	lmtemp_config_write(struct lmtemp_softc *, uint8_t);
     70   1.2       scw static uint32_t lmtemp_decode_lm75(const uint8_t *);
     71   1.2       scw static uint32_t lmtemp_decode_ds75(const uint8_t *);
     72   1.9  kiyohara static uint32_t lmtemp_decode_lm77(const uint8_t *);
     73   1.9  kiyohara 
     74  1.21    martin 
     75  1.21    martin static const char * lmtemp_compats[] = {
     76  1.21    martin 	"i2c-lm75",
     77  1.21    martin 	/*
     78  1.21    martin 	 * see XXX in _attach() below: add code once non-lm75 matches are
     79  1.21    martin 	 * added here!
     80  1.21    martin 	 */
     81  1.21    martin 	NULL
     82  1.21    martin };
     83  1.21    martin 
     84   1.9  kiyohara enum {
     85   1.9  kiyohara 	lmtemp_lm75 = 0,
     86   1.9  kiyohara 	lmtemp_ds75,
     87   1.9  kiyohara 	lmtemp_lm77,
     88   1.9  kiyohara };
     89   1.9  kiyohara static const struct {
     90   1.9  kiyohara 	int lmtemp_type;
     91   1.9  kiyohara 	const char *lmtemp_name;
     92   1.9  kiyohara 	int lmtemp_addrmask;
     93   1.9  kiyohara 	int lmtemp_addr;
     94   1.9  kiyohara 	uint32_t (*lmtemp_decode)(const uint8_t *);
     95   1.9  kiyohara } lmtemptbl[] = {
     96   1.9  kiyohara 	{ lmtemp_lm75,	"LM75",
     97   1.9  kiyohara 	    LM75_ADDRMASK,	LM75_ADDR,	lmtemp_decode_lm75 },
     98   1.9  kiyohara 	{ lmtemp_ds75,	"DS75",
     99   1.9  kiyohara 	    LM75_ADDRMASK,	LM75_ADDR,	lmtemp_decode_ds75 },
    100   1.9  kiyohara 	{ lmtemp_lm77,	"LM77",
    101   1.9  kiyohara 	    LM77_ADDRMASK,	LM77_ADDR,	lmtemp_decode_lm77 },
    102   1.9  kiyohara 
    103   1.9  kiyohara 	{ -1,		NULL,
    104   1.9  kiyohara 	    0,			0,		NULL }
    105   1.9  kiyohara };
    106   1.1   thorpej 
    107   1.1   thorpej static int
    108  1.18   xtraeme lmtemp_match(device_t parent, cfdata_t cf, void *aux)
    109   1.1   thorpej {
    110   1.1   thorpej 	struct i2c_attach_args *ia = aux;
    111   1.9  kiyohara 	int i;
    112   1.9  kiyohara 
    113  1.21    martin 	if (ia->ia_name == NULL) {
    114  1.21    martin 		/*
    115  1.21    martin 		 * Indirect config - not much we can do!
    116  1.21    martin 		 */
    117  1.21    martin 		for (i = 0; lmtemptbl[i].lmtemp_type != -1 ; i++)
    118  1.21    martin 			if (lmtemptbl[i].lmtemp_type == cf->cf_flags)
    119  1.21    martin 				break;
    120  1.21    martin 		if (lmtemptbl[i].lmtemp_type == -1)
    121  1.21    martin 			return 0;
    122  1.21    martin 
    123  1.21    martin 		if ((ia->ia_addr & lmtemptbl[i].lmtemp_addrmask) ==
    124  1.21    martin 		    lmtemptbl[i].lmtemp_addr)
    125  1.21    martin 			return 1;
    126  1.21    martin 	} else {
    127  1.21    martin 		/*
    128  1.21    martin 		 * Direct config - match via the list of compatible
    129  1.21    martin 		 * hardware.
    130  1.21    martin 		 */
    131  1.21    martin 		if (iic_compat_match(ia, lmtemp_compats))
    132  1.21    martin 			return 1;
    133  1.21    martin 	}
    134  1.21    martin 
    135   1.1   thorpej 
    136  1.18   xtraeme 	return 0;
    137   1.1   thorpej }
    138   1.1   thorpej 
    139   1.1   thorpej static void
    140  1.18   xtraeme lmtemp_attach(device_t parent, device_t self, void *aux)
    141   1.1   thorpej {
    142   1.6   thorpej 	struct lmtemp_softc *sc = device_private(self);
    143   1.1   thorpej 	struct i2c_attach_args *ia = aux;
    144   1.9  kiyohara 	int i;
    145   1.9  kiyohara 
    146  1.21    martin 	if (ia->ia_name == NULL) {
    147  1.21    martin 		for (i = 0; lmtemptbl[i].lmtemp_type != -1 ; i++)
    148  1.21    martin 			if (lmtemptbl[i].lmtemp_type ==
    149  1.21    martin 			    device_cfdata(self)->cf_flags)
    150  1.21    martin 				break;
    151  1.21    martin 	} else {
    152  1.21    martin 		/* XXX - add code when adding other direct matches! */
    153  1.21    martin 		i = 0;
    154  1.21    martin 	}
    155   1.1   thorpej 
    156   1.1   thorpej 	sc->sc_tag = ia->ia_tag;
    157   1.1   thorpej 	sc->sc_address = ia->ia_addr;
    158   1.1   thorpej 
    159   1.1   thorpej 	aprint_naive(": Temperature Sensor\n");
    160  1.21    martin 	if (ia->ia_name) {
    161  1.21    martin 		aprint_normal(": %s %s Temperature Sensor\n", ia->ia_name,
    162  1.21    martin 			lmtemptbl[i].lmtemp_name);
    163  1.21    martin 	} else {
    164  1.21    martin 		aprint_normal(": %s Temperature Sensor\n",
    165  1.21    martin 			lmtemptbl[i].lmtemp_name);
    166  1.21    martin 	}
    167   1.1   thorpej 
    168   1.1   thorpej 	/* Set the configuration of the LM75 to defaults. */
    169   1.1   thorpej 	iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
    170   1.1   thorpej 	if (lmtemp_config_write(sc, 0) != 0) {
    171  1.18   xtraeme 		aprint_error_dev(self, "unable to write config register\n");
    172   1.1   thorpej 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
    173   1.1   thorpej 		return;
    174   1.1   thorpej 	}
    175   1.1   thorpej 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    176   1.1   thorpej 
    177  1.16   xtraeme 	sc->sc_sme = sysmon_envsys_create();
    178   1.1   thorpej 	/* Initialize sensor data. */
    179  1.16   xtraeme 	sc->sc_sensor.units =  ENVSYS_STEMP;
    180  1.21    martin 	(void)strlcpy(sc->sc_sensor.desc,
    181  1.21    martin 	    ia->ia_name? ia->ia_name : device_xname(self),
    182  1.18   xtraeme 	    sizeof(sc->sc_sensor.desc));
    183  1.16   xtraeme 	if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) {
    184  1.16   xtraeme 		sysmon_envsys_destroy(sc->sc_sme);
    185  1.16   xtraeme 		return;
    186  1.16   xtraeme 	}
    187   1.1   thorpej 
    188   1.9  kiyohara 	sc->sc_lmtemp_decode = lmtemptbl[i].lmtemp_decode;
    189   1.9  kiyohara 
    190   1.7       riz 	/* Hook into system monitor. */
    191  1.18   xtraeme 	sc->sc_sme->sme_name = device_xname(self);
    192  1.16   xtraeme 	sc->sc_sme->sme_cookie = sc;
    193  1.16   xtraeme 	sc->sc_sme->sme_refresh = lmtemp_refresh;
    194   1.1   thorpej 
    195  1.16   xtraeme 	if (sysmon_envsys_register(sc->sc_sme)) {
    196  1.18   xtraeme 		aprint_error_dev(self, "unable to register with sysmon\n");
    197  1.16   xtraeme 		sysmon_envsys_destroy(sc->sc_sme);
    198  1.16   xtraeme 	}
    199   1.1   thorpej }
    200   1.1   thorpej 
    201   1.1   thorpej static int
    202   1.1   thorpej lmtemp_config_write(struct lmtemp_softc *sc, uint8_t val)
    203   1.1   thorpej {
    204   1.1   thorpej 	uint8_t cmdbuf[2];
    205   1.1   thorpej 
    206   1.1   thorpej 	cmdbuf[0] = LM75_REG_CONFIG;
    207   1.1   thorpej 	cmdbuf[1] = val;
    208   1.1   thorpej 
    209  1.18   xtraeme 	return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    210  1.18   xtraeme 	    sc->sc_address, cmdbuf, 1, &cmdbuf[1], 1, I2C_F_POLL);
    211   1.1   thorpej }
    212   1.1   thorpej 
    213   1.1   thorpej static int
    214   1.1   thorpej lmtemp_temp_read(struct lmtemp_softc *sc, uint8_t which, uint32_t *valp)
    215   1.1   thorpej {
    216   1.2       scw 	int error;
    217   1.1   thorpej 	uint8_t cmdbuf[1];
    218   1.1   thorpej 	uint8_t buf[LM75_TEMP_LEN];
    219   1.1   thorpej 
    220   1.1   thorpej 	cmdbuf[0] = which;
    221   1.1   thorpej 
    222   1.1   thorpej 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    223   1.1   thorpej 	    sc->sc_address, cmdbuf, 1, buf, LM75_TEMP_LEN, 0);
    224   1.1   thorpej 	if (error)
    225  1.18   xtraeme 		return error;
    226   1.1   thorpej 
    227   1.9  kiyohara 	*valp = sc->sc_lmtemp_decode(buf);
    228  1.18   xtraeme 	return 0;
    229   1.1   thorpej }
    230   1.1   thorpej 
    231   1.1   thorpej static void
    232   1.1   thorpej lmtemp_refresh_sensor_data(struct lmtemp_softc *sc)
    233   1.1   thorpej {
    234   1.1   thorpej 	uint32_t val;
    235   1.1   thorpej 	int error;
    236   1.1   thorpej 
    237   1.1   thorpej 	error = lmtemp_temp_read(sc, LM75_REG_TEMP, &val);
    238   1.1   thorpej 	if (error) {
    239   1.1   thorpej #if 0
    240  1.19    cegger 		aprint_error_dev(&sc->sc_dev, "unable to read temperature, error = %d\n",
    241  1.19    cegger 		    error);
    242   1.1   thorpej #endif
    243  1.16   xtraeme 		sc->sc_sensor.state = ENVSYS_SINVALID;
    244   1.1   thorpej 		return;
    245   1.1   thorpej 	}
    246   1.1   thorpej 
    247  1.16   xtraeme 	sc->sc_sensor.value_cur = val;
    248  1.16   xtraeme 	sc->sc_sensor.state = ENVSYS_SVALID;
    249   1.1   thorpej }
    250   1.1   thorpej 
    251  1.16   xtraeme static void
    252  1.16   xtraeme lmtemp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    253   1.1   thorpej {
    254   1.1   thorpej 	struct lmtemp_softc *sc = sme->sme_cookie;
    255   1.1   thorpej 
    256   1.1   thorpej 	iic_acquire_bus(sc->sc_tag, 0);	/* also locks our instance */
    257   1.1   thorpej 	lmtemp_refresh_sensor_data(sc);
    258   1.1   thorpej 	iic_release_bus(sc->sc_tag, 0);	/* also unlocks our instance */
    259   1.1   thorpej }
    260   1.2       scw 
    261   1.2       scw static uint32_t
    262   1.2       scw lmtemp_decode_lm75(const uint8_t *buf)
    263   1.2       scw {
    264  1.20    briggs 	int temp;
    265   1.2       scw 	uint32_t val;
    266   1.2       scw 
    267  1.20    briggs 	/*
    268  1.20    briggs 	 * LM75 temps are the most-significant 9 bits of a 16-bit reg.
    269  1.20    briggs 	 * sign-extend the MSB and add in the 0.5 from the LSB
    270  1.20    briggs 	 */
    271  1.20    briggs 	temp = (int8_t) buf[0];
    272  1.20    briggs 	temp = (temp << 1) + ((buf[1] >> 7) & 0x1);
    273   1.2       scw 
    274   1.2       scw 	/* Temp is given in 1/2 deg. C, we convert to uK. */
    275  1.20    briggs 	val = temp * 500000 + 273150000;
    276   1.2       scw 
    277  1.18   xtraeme 	return val;
    278   1.2       scw }
    279   1.2       scw 
    280   1.2       scw static uint32_t
    281   1.2       scw lmtemp_decode_ds75(const uint8_t *buf)
    282   1.2       scw {
    283   1.2       scw 	int temp;
    284   1.2       scw 
    285   1.2       scw 	/*
    286   1.2       scw 	 * Sign-extend the MSB byte, and add in the fractions of a
    287   1.7       riz 	 * degree contained in the LSB (precision 1/16th DegC).
    288   1.2       scw 	 */
    289   1.2       scw 	temp = (int8_t)buf[0];
    290   1.2       scw 	temp = (temp << 4) | ((buf[1] >> 4) & 0xf);
    291   1.2       scw 
    292   1.2       scw 	/*
    293   1.2       scw 	 * Conversion to uK is simple.
    294   1.2       scw 	 */
    295   1.2       scw 	return (temp * 62500 + 273150000);
    296   1.2       scw }
    297   1.2       scw 
    298   1.9  kiyohara static uint32_t
    299   1.9  kiyohara lmtemp_decode_lm77(const uint8_t *buf)
    300   1.9  kiyohara {
    301   1.9  kiyohara 	int temp;
    302   1.9  kiyohara 	uint32_t val;
    303   1.9  kiyohara 
    304   1.9  kiyohara 	/*
    305   1.9  kiyohara 	 * Describe each bits of temperature registers on LM77.
    306   1.9  kiyohara 	 *   D15 - D12:	Sign
    307   1.9  kiyohara 	 *   D11 - D3 :	Bit8(MSB) - Bit0
    308   1.9  kiyohara 	 */
    309   1.9  kiyohara 	temp = (int8_t)buf[0];
    310  1.10  kiyohara 	temp = (temp << 5) | ((buf[1] >> 3) & 0x1f);
    311   1.9  kiyohara 
    312   1.9  kiyohara 	/* Temp is given in 1/2 deg. C, we convert to uK. */
    313   1.9  kiyohara 	val = temp * 500000 + 273150000;
    314   1.9  kiyohara 
    315  1.18   xtraeme 	return val;
    316   1.9  kiyohara }
    317   1.9  kiyohara 
    318