Home | History | Annotate | Line # | Download | only in i2c
lm75.c revision 1.12
      1  1.12   xtraeme /*	$NetBSD: lm75.c,v 1.12 2007/07/01 07:37:16 xtraeme 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.1   thorpej #include <sys/param.h>
     39   1.1   thorpej #include <sys/systm.h>
     40   1.1   thorpej #include <sys/device.h>
     41   1.1   thorpej #include <sys/kernel.h>
     42   1.1   thorpej 
     43   1.1   thorpej #include <dev/sysmon/sysmonvar.h>
     44   1.1   thorpej 
     45   1.1   thorpej #include <dev/i2c/i2cvar.h>
     46   1.1   thorpej #include <dev/i2c/lm75reg.h>
     47   1.1   thorpej 
     48   1.1   thorpej struct lmtemp_softc {
     49   1.1   thorpej 	struct device sc_dev;
     50   1.1   thorpej 	i2c_tag_t sc_tag;
     51   1.1   thorpej 	int sc_address;
     52   1.1   thorpej 
     53  1.12   xtraeme 	envsys_data_t sc_sensor[1];
     54   1.1   thorpej 	struct sysmon_envsys sc_sysmon;
     55   1.9  kiyohara 
     56   1.9  kiyohara 	uint32_t (*sc_lmtemp_decode)(const uint8_t *);
     57   1.1   thorpej };
     58   1.1   thorpej 
     59   1.1   thorpej static int  lmtemp_match(struct device *, struct cfdata *, void *);
     60   1.1   thorpej static void lmtemp_attach(struct device *, struct device *, void *);
     61   1.1   thorpej 
     62   1.1   thorpej CFATTACH_DECL(lmtemp, sizeof(struct lmtemp_softc),
     63   1.1   thorpej 	lmtemp_match, lmtemp_attach, NULL, NULL);
     64   1.1   thorpej 
     65  1.12   xtraeme static int	lmtemp_gtredata(struct sysmon_envsys *, envsys_data_t *);
     66   1.1   thorpej 
     67   1.1   thorpej static int lmtemp_config_write(struct lmtemp_softc *, uint8_t);
     68   1.2       scw static uint32_t lmtemp_decode_lm75(const uint8_t *);
     69   1.2       scw static uint32_t lmtemp_decode_ds75(const uint8_t *);
     70   1.9  kiyohara static uint32_t lmtemp_decode_lm77(const uint8_t *);
     71   1.9  kiyohara 
     72   1.9  kiyohara enum {
     73   1.9  kiyohara 	lmtemp_lm75 = 0,
     74   1.9  kiyohara 	lmtemp_ds75,
     75   1.9  kiyohara 	lmtemp_lm77,
     76   1.9  kiyohara };
     77   1.9  kiyohara static const struct {
     78   1.9  kiyohara 	int lmtemp_type;
     79   1.9  kiyohara 	const char *lmtemp_name;
     80   1.9  kiyohara 	int lmtemp_addrmask;
     81   1.9  kiyohara 	int lmtemp_addr;
     82   1.9  kiyohara 	uint32_t (*lmtemp_decode)(const uint8_t *);
     83   1.9  kiyohara } lmtemptbl[] = {
     84   1.9  kiyohara 	{ lmtemp_lm75,	"LM75",
     85   1.9  kiyohara 	    LM75_ADDRMASK,	LM75_ADDR,	lmtemp_decode_lm75 },
     86   1.9  kiyohara 	{ lmtemp_ds75,	"DS75",
     87   1.9  kiyohara 	    LM75_ADDRMASK,	LM75_ADDR,	lmtemp_decode_ds75 },
     88   1.9  kiyohara 	{ lmtemp_lm77,	"LM77",
     89   1.9  kiyohara 	    LM77_ADDRMASK,	LM77_ADDR,	lmtemp_decode_lm77 },
     90   1.9  kiyohara 
     91   1.9  kiyohara 	{ -1,		NULL,
     92   1.9  kiyohara 	    0,			0,		NULL }
     93   1.9  kiyohara };
     94   1.1   thorpej 
     95   1.1   thorpej static int
     96   1.1   thorpej lmtemp_match(struct device *parent, struct cfdata *cf, void *aux)
     97   1.1   thorpej {
     98   1.1   thorpej 	struct i2c_attach_args *ia = aux;
     99   1.9  kiyohara 	int i;
    100   1.9  kiyohara 
    101   1.9  kiyohara 	for (i = 0; lmtemptbl[i].lmtemp_type != -1 ; i++)
    102   1.9  kiyohara 		if (lmtemptbl[i].lmtemp_type == cf->cf_flags)
    103   1.9  kiyohara 			break;
    104   1.9  kiyohara 	if (lmtemptbl[i].lmtemp_type == -1)
    105   1.9  kiyohara 		return (0);
    106   1.1   thorpej 
    107   1.9  kiyohara 	if ((ia->ia_addr & lmtemptbl[i].lmtemp_addrmask) ==
    108   1.9  kiyohara 	    lmtemptbl[i].lmtemp_addr)
    109   1.1   thorpej 		return (1);
    110   1.1   thorpej 
    111   1.1   thorpej 	return (0);
    112   1.1   thorpej }
    113   1.1   thorpej 
    114   1.1   thorpej static void
    115   1.1   thorpej lmtemp_attach(struct device *parent, struct device *self, void *aux)
    116   1.1   thorpej {
    117   1.6   thorpej 	struct lmtemp_softc *sc = device_private(self);
    118   1.1   thorpej 	struct i2c_attach_args *ia = aux;
    119   1.8   thorpej 	prop_string_t desc;
    120   1.9  kiyohara 	int i;
    121   1.9  kiyohara 
    122   1.9  kiyohara 	for (i = 0; lmtemptbl[i].lmtemp_type != -1 ; i++)
    123   1.9  kiyohara 		if (lmtemptbl[i].lmtemp_type ==
    124   1.9  kiyohara 		    device_cfdata(&sc->sc_dev)->cf_flags)
    125   1.9  kiyohara 			break;
    126   1.1   thorpej 
    127   1.1   thorpej 	sc->sc_tag = ia->ia_tag;
    128   1.1   thorpej 	sc->sc_address = ia->ia_addr;
    129   1.1   thorpej 
    130   1.1   thorpej 	aprint_naive(": Temperature Sensor\n");
    131   1.9  kiyohara 	aprint_normal(": %s Temperature Sensor\n", lmtemptbl[i].lmtemp_name);
    132   1.1   thorpej 
    133   1.1   thorpej 	/* Set the configuration of the LM75 to defaults. */
    134   1.1   thorpej 	iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
    135   1.1   thorpej 	if (lmtemp_config_write(sc, 0) != 0) {
    136   1.1   thorpej 		aprint_error("%s: unable to write config register\n",
    137   1.1   thorpej 		    sc->sc_dev.dv_xname);
    138   1.1   thorpej 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
    139   1.1   thorpej 		return;
    140   1.1   thorpej 	}
    141   1.1   thorpej 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    142   1.1   thorpej 
    143   1.1   thorpej 	/* Initialize sensor data. */
    144  1.12   xtraeme 	sc->sc_sensor[0].sensor = 0;
    145  1.12   xtraeme 	sc->sc_sensor[0].state = ENVSYS_FVALID;
    146  1.12   xtraeme 	sc->sc_sensor[0].units =  ENVSYS_STEMP;
    147   1.8   thorpej 	desc = prop_dictionary_get(device_properties(&sc->sc_dev),
    148  1.11   thorpej 				   "envsys-description");
    149   1.8   thorpej 	if (desc != NULL &&
    150   1.8   thorpej 	    prop_object_type(desc) == PROP_TYPE_STRING &&
    151   1.8   thorpej 	    prop_string_size(desc) > 0)
    152  1.12   xtraeme 	    	strcpy(sc->sc_sensor[0].desc, prop_string_cstring_nocopy(desc));
    153   1.8   thorpej 	else
    154  1.12   xtraeme 		strcpy(sc->sc_sensor[0].desc, sc->sc_dev.dv_xname);
    155   1.1   thorpej 
    156   1.9  kiyohara 	sc->sc_lmtemp_decode = lmtemptbl[i].lmtemp_decode;
    157   1.9  kiyohara 
    158   1.7       riz 	/* Hook into system monitor. */
    159  1.12   xtraeme 	sc->sc_sysmon.sme_name = sc->sc_dev.dv_xname;
    160   1.1   thorpej 	sc->sc_sysmon.sme_sensor_data = sc->sc_sensor;
    161   1.1   thorpej 	sc->sc_sysmon.sme_cookie = sc;
    162   1.1   thorpej 	sc->sc_sysmon.sme_gtredata = lmtemp_gtredata;
    163   1.1   thorpej 	sc->sc_sysmon.sme_nsensors = 1;
    164   1.1   thorpej 
    165   1.1   thorpej 	if (sysmon_envsys_register(&sc->sc_sysmon))
    166   1.1   thorpej 		aprint_error("%s: unable to register with sysmon\n",
    167   1.1   thorpej 		    sc->sc_dev.dv_xname);
    168   1.1   thorpej }
    169   1.1   thorpej 
    170   1.1   thorpej static int
    171   1.1   thorpej lmtemp_config_write(struct lmtemp_softc *sc, uint8_t val)
    172   1.1   thorpej {
    173   1.1   thorpej 	uint8_t cmdbuf[2];
    174   1.1   thorpej 
    175   1.1   thorpej 	cmdbuf[0] = LM75_REG_CONFIG;
    176   1.1   thorpej 	cmdbuf[1] = val;
    177   1.1   thorpej 
    178   1.1   thorpej 	return (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    179   1.1   thorpej 	    sc->sc_address, cmdbuf, 1, &cmdbuf[1], 1, I2C_F_POLL));
    180   1.1   thorpej }
    181   1.1   thorpej 
    182   1.1   thorpej static int
    183   1.1   thorpej lmtemp_temp_read(struct lmtemp_softc *sc, uint8_t which, uint32_t *valp)
    184   1.1   thorpej {
    185   1.2       scw 	int error;
    186   1.1   thorpej 	uint8_t cmdbuf[1];
    187   1.1   thorpej 	uint8_t buf[LM75_TEMP_LEN];
    188   1.1   thorpej 
    189   1.1   thorpej 	cmdbuf[0] = which;
    190   1.1   thorpej 
    191   1.1   thorpej 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    192   1.1   thorpej 	    sc->sc_address, cmdbuf, 1, buf, LM75_TEMP_LEN, 0);
    193   1.1   thorpej 	if (error)
    194   1.1   thorpej 		return (error);
    195   1.1   thorpej 
    196   1.9  kiyohara 	*valp = sc->sc_lmtemp_decode(buf);
    197   1.1   thorpej 	return (0);
    198   1.1   thorpej }
    199   1.1   thorpej 
    200   1.1   thorpej static void
    201   1.1   thorpej lmtemp_refresh_sensor_data(struct lmtemp_softc *sc)
    202   1.1   thorpej {
    203   1.1   thorpej 	uint32_t val;
    204   1.1   thorpej 	int error;
    205   1.1   thorpej 
    206   1.1   thorpej 	error = lmtemp_temp_read(sc, LM75_REG_TEMP, &val);
    207   1.1   thorpej 	if (error) {
    208   1.1   thorpej #if 0
    209   1.1   thorpej 		printf("%s: unable to read temperature, error = %d\n",
    210   1.1   thorpej 		    sc->sc_dev.dv_xname, error);
    211   1.1   thorpej #endif
    212  1.12   xtraeme 		sc->sc_sensor[0].state = ENVSYS_SINVALID;
    213   1.1   thorpej 		return;
    214   1.1   thorpej 	}
    215   1.1   thorpej 
    216  1.12   xtraeme 	sc->sc_sensor[0].value_cur = val;
    217  1.12   xtraeme 	sc->sc_sensor[0].state = ENVSYS_SVALID;
    218   1.1   thorpej }
    219   1.1   thorpej 
    220   1.1   thorpej static int
    221  1.12   xtraeme lmtemp_gtredata(struct sysmon_envsys *sme, envsys_data_t *edata)
    222   1.1   thorpej {
    223   1.1   thorpej 	struct lmtemp_softc *sc = sme->sme_cookie;
    224   1.1   thorpej 
    225   1.1   thorpej 	iic_acquire_bus(sc->sc_tag, 0);	/* also locks our instance */
    226   1.1   thorpej 	lmtemp_refresh_sensor_data(sc);
    227   1.1   thorpej 	iic_release_bus(sc->sc_tag, 0);	/* also unlocks our instance */
    228   1.1   thorpej 
    229   1.1   thorpej 	return (0);
    230   1.1   thorpej }
    231   1.2       scw 
    232   1.2       scw static uint32_t
    233   1.2       scw lmtemp_decode_lm75(const uint8_t *buf)
    234   1.2       scw {
    235   1.2       scw 	int neg, temp;
    236   1.2       scw 	uint32_t val;
    237   1.2       scw 
    238   1.2       scw 	if (buf[0] & 1) {
    239   1.2       scw 		/* Below 0C */
    240   1.2       scw 		temp = ~buf[1] + 1;
    241   1.2       scw 		neg = 1;
    242   1.2       scw 	} else {
    243   1.2       scw 		temp = buf[1];
    244   1.2       scw 		neg = 0;
    245   1.2       scw 	}
    246   1.2       scw 
    247   1.2       scw 	/* Temp is given in 1/2 deg. C, we convert to uK. */
    248   1.2       scw 	val = ((neg ? -temp : temp) / 2) * 1000000 + 273150000;
    249   1.2       scw 	if (temp & 1) {
    250   1.2       scw 		if (neg)
    251   1.2       scw 			val -= 500000;
    252   1.2       scw 		else
    253   1.2       scw 			val += 500000;
    254   1.2       scw 	}
    255   1.2       scw 
    256   1.2       scw 	return (val);
    257   1.2       scw }
    258   1.2       scw 
    259   1.2       scw static uint32_t
    260   1.2       scw lmtemp_decode_ds75(const uint8_t *buf)
    261   1.2       scw {
    262   1.2       scw 	int temp;
    263   1.2       scw 
    264   1.2       scw 	/*
    265   1.2       scw 	 * Sign-extend the MSB byte, and add in the fractions of a
    266   1.7       riz 	 * degree contained in the LSB (precision 1/16th DegC).
    267   1.2       scw 	 */
    268   1.2       scw 	temp = (int8_t)buf[0];
    269   1.2       scw 	temp = (temp << 4) | ((buf[1] >> 4) & 0xf);
    270   1.2       scw 
    271   1.2       scw 	/*
    272   1.2       scw 	 * Conversion to uK is simple.
    273   1.2       scw 	 */
    274   1.2       scw 	return (temp * 62500 + 273150000);
    275   1.2       scw }
    276   1.2       scw 
    277   1.9  kiyohara static uint32_t
    278   1.9  kiyohara lmtemp_decode_lm77(const uint8_t *buf)
    279   1.9  kiyohara {
    280   1.9  kiyohara 	int temp;
    281   1.9  kiyohara 	uint32_t val;
    282   1.9  kiyohara 
    283   1.9  kiyohara 	/*
    284   1.9  kiyohara 	 * Describe each bits of temperature registers on LM77.
    285   1.9  kiyohara 	 *   D15 - D12:	Sign
    286   1.9  kiyohara 	 *   D11 - D3 :	Bit8(MSB) - Bit0
    287   1.9  kiyohara 	 */
    288   1.9  kiyohara 	temp = (int8_t)buf[0];
    289  1.10  kiyohara 	temp = (temp << 5) | ((buf[1] >> 3) & 0x1f);
    290   1.9  kiyohara 
    291   1.9  kiyohara 	/* Temp is given in 1/2 deg. C, we convert to uK. */
    292   1.9  kiyohara 	val = temp * 500000 + 273150000;
    293   1.9  kiyohara 
    294   1.9  kiyohara 	return (val);
    295   1.9  kiyohara }
    296   1.9  kiyohara 
    297