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