Home | History | Annotate | Line # | Download | only in i2c
lm75.c revision 1.8
      1 /*	$NetBSD: lm75.c,v 1.8 2006/05/05 18:04:42 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2003 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed for the NetBSD Project by
     20  *      Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/device.h>
     41 #include <sys/kernel.h>
     42 
     43 #include <dev/sysmon/sysmonvar.h>
     44 
     45 #include <dev/i2c/i2cvar.h>
     46 #include <dev/i2c/lm75reg.h>
     47 
     48 struct lmtemp_softc {
     49 	struct device sc_dev;
     50 	i2c_tag_t sc_tag;
     51 	int sc_address;
     52 	int sc_is_ds75;
     53 
     54 	struct envsys_tre_data sc_sensor[1];
     55 	struct envsys_basic_info sc_info[1];
     56 
     57 	struct sysmon_envsys sc_sysmon;
     58 };
     59 
     60 static int  lmtemp_match(struct device *, struct cfdata *, void *);
     61 static void lmtemp_attach(struct device *, struct device *, void *);
     62 
     63 CFATTACH_DECL(lmtemp, sizeof(struct lmtemp_softc),
     64 	lmtemp_match, lmtemp_attach, NULL, NULL);
     65 
     66 static int	lmtemp_gtredata(struct sysmon_envsys *,
     67 				struct envsys_tre_data *);
     68 static int	lmtemp_streinfo(struct sysmon_envsys *,
     69 				struct envsys_basic_info *);
     70 
     71 static const struct envsys_range lmtemp_ranges[] = {
     72 	{ 0, 1,		ENVSYS_STEMP },
     73 	{ 1, 0,		-1 },
     74 };
     75 
     76 static int lmtemp_config_write(struct lmtemp_softc *, uint8_t);
     77 static uint32_t lmtemp_decode_lm75(const uint8_t *);
     78 static uint32_t lmtemp_decode_ds75(const uint8_t *);
     79 
     80 static int
     81 lmtemp_match(struct device *parent, struct cfdata *cf, void *aux)
     82 {
     83 	struct i2c_attach_args *ia = aux;
     84 
     85 	if ((ia->ia_addr & LM75_ADDRMASK) == LM75_ADDR)
     86 		return (1);
     87 
     88 	return (0);
     89 }
     90 
     91 static void
     92 lmtemp_attach(struct device *parent, struct device *self, void *aux)
     93 {
     94 	struct lmtemp_softc *sc = device_private(self);
     95 	struct i2c_attach_args *ia = aux;
     96 	prop_string_t desc;
     97 
     98 	sc->sc_tag = ia->ia_tag;
     99 	sc->sc_address = ia->ia_addr;
    100 	sc->sc_is_ds75 = device_cfdata(&sc->sc_dev)->cf_flags & 1;
    101 
    102 	aprint_naive(": Temperature Sensor\n");
    103 	aprint_normal(": %s Temperature Sensor\n",
    104 	    (sc->sc_is_ds75) ? "DS75" : "LM75");
    105 
    106 	/* Set the configuration of the LM75 to defaults. */
    107 	iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
    108 	if (lmtemp_config_write(sc, 0) != 0) {
    109 		aprint_error("%s: unable to write config register\n",
    110 		    sc->sc_dev.dv_xname);
    111 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
    112 		return;
    113 	}
    114 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    115 
    116 	/* Initialize sensor data. */
    117 	sc->sc_sensor[0].sensor = sc->sc_info[0].sensor = 0;
    118 	sc->sc_sensor[0].validflags = ENVSYS_FVALID;
    119 	sc->sc_info[0].validflags = ENVSYS_FVALID;
    120 	sc->sc_sensor[0].warnflags = ENVSYS_WARN_OK;
    121 
    122 	sc->sc_sensor[0].units = sc->sc_info[0].units = ENVSYS_STEMP;
    123 	desc = prop_dictionary_get(device_properties(&sc->sc_dev),
    124 				   "description");
    125 	if (desc != NULL &&
    126 	    prop_object_type(desc) == PROP_TYPE_STRING &&
    127 	    prop_string_size(desc) > 0)
    128 	    	strcpy(sc->sc_info[0].desc, prop_string_cstring_nocopy(desc));
    129 	else
    130 		strcpy(sc->sc_info[0].desc, sc->sc_dev.dv_xname);
    131 
    132 	/* Hook into system monitor. */
    133 	sc->sc_sysmon.sme_ranges = lmtemp_ranges;
    134 	sc->sc_sysmon.sme_sensor_info = sc->sc_info;
    135 	sc->sc_sysmon.sme_sensor_data = sc->sc_sensor;
    136 	sc->sc_sysmon.sme_cookie = sc;
    137 
    138 	sc->sc_sysmon.sme_gtredata = lmtemp_gtredata;
    139 	sc->sc_sysmon.sme_streinfo = lmtemp_streinfo;
    140 
    141 	sc->sc_sysmon.sme_nsensors = 1;
    142 	sc->sc_sysmon.sme_envsys_version = 1000;
    143 
    144 	if (sysmon_envsys_register(&sc->sc_sysmon))
    145 		aprint_error("%s: unable to register with sysmon\n",
    146 		    sc->sc_dev.dv_xname);
    147 }
    148 
    149 static int
    150 lmtemp_config_write(struct lmtemp_softc *sc, uint8_t val)
    151 {
    152 	uint8_t cmdbuf[2];
    153 
    154 	cmdbuf[0] = LM75_REG_CONFIG;
    155 	cmdbuf[1] = val;
    156 
    157 	return (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    158 	    sc->sc_address, cmdbuf, 1, &cmdbuf[1], 1, I2C_F_POLL));
    159 }
    160 
    161 static int
    162 lmtemp_temp_read(struct lmtemp_softc *sc, uint8_t which, uint32_t *valp)
    163 {
    164 	int error;
    165 	uint8_t cmdbuf[1];
    166 	uint8_t buf[LM75_TEMP_LEN];
    167 
    168 	cmdbuf[0] = which;
    169 
    170 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    171 	    sc->sc_address, cmdbuf, 1, buf, LM75_TEMP_LEN, 0);
    172 	if (error)
    173 		return (error);
    174 
    175 	if (sc->sc_is_ds75)
    176 		*valp = lmtemp_decode_ds75(buf);
    177 	else
    178 		*valp = lmtemp_decode_lm75(buf);
    179 
    180 	return (0);
    181 }
    182 
    183 static void
    184 lmtemp_refresh_sensor_data(struct lmtemp_softc *sc)
    185 {
    186 	uint32_t val;
    187 	int error;
    188 
    189 	error = lmtemp_temp_read(sc, LM75_REG_TEMP, &val);
    190 	if (error) {
    191 #if 0
    192 		printf("%s: unable to read temperature, error = %d\n",
    193 		    sc->sc_dev.dv_xname, error);
    194 #endif
    195 		sc->sc_sensor[0].validflags &= ~ENVSYS_FCURVALID;
    196 		return;
    197 	}
    198 
    199 	sc->sc_sensor[0].cur.data_us = val;
    200 	sc->sc_sensor[0].validflags |= ENVSYS_FCURVALID;
    201 }
    202 
    203 static int
    204 lmtemp_gtredata(struct sysmon_envsys *sme, struct envsys_tre_data *tred)
    205 {
    206 	struct lmtemp_softc *sc = sme->sme_cookie;
    207 
    208 	iic_acquire_bus(sc->sc_tag, 0);	/* also locks our instance */
    209 
    210 	lmtemp_refresh_sensor_data(sc);
    211 	*tred = sc->sc_sensor[tred->sensor];
    212 
    213 	iic_release_bus(sc->sc_tag, 0);	/* also unlocks our instance */
    214 
    215 	return (0);
    216 }
    217 
    218 static int
    219 lmtemp_streinfo(struct sysmon_envsys *sme, struct envsys_basic_info *binfo)
    220 {
    221 	struct lmtemp_softc *sc = sme->sme_cookie;
    222 
    223 	iic_acquire_bus(sc->sc_tag, 0);	/* also locks our instance */
    224 
    225 	memcpy(sc->sc_info[binfo->sensor].desc, binfo->desc,
    226 	    sizeof(sc->sc_info[binfo->sensor].desc));
    227 	sc->sc_info[binfo->sensor].desc[
    228 	    sizeof(sc->sc_info[binfo->sensor].desc) - 1] = '\0';
    229 
    230 	iic_release_bus(sc->sc_tag, 0);	/* also unlocks our instance */
    231 
    232 	binfo->validflags = ENVSYS_FVALID;
    233 
    234 	return (0);
    235 }
    236 
    237 static uint32_t
    238 lmtemp_decode_lm75(const uint8_t *buf)
    239 {
    240 	int neg, temp;
    241 	uint32_t val;
    242 
    243 	if (buf[0] & 1) {
    244 		/* Below 0C */
    245 		temp = ~buf[1] + 1;
    246 		neg = 1;
    247 	} else {
    248 		temp = buf[1];
    249 		neg = 0;
    250 	}
    251 
    252 	/* Temp is given in 1/2 deg. C, we convert to uK. */
    253 	val = ((neg ? -temp : temp) / 2) * 1000000 + 273150000;
    254 	if (temp & 1) {
    255 		if (neg)
    256 			val -= 500000;
    257 		else
    258 			val += 500000;
    259 	}
    260 
    261 	return (val);
    262 }
    263 
    264 static uint32_t
    265 lmtemp_decode_ds75(const uint8_t *buf)
    266 {
    267 	int temp;
    268 
    269 	/*
    270 	 * Sign-extend the MSB byte, and add in the fractions of a
    271 	 * degree contained in the LSB (precision 1/16th DegC).
    272 	 */
    273 	temp = (int8_t)buf[0];
    274 	temp = (temp << 4) | ((buf[1] >> 4) & 0xf);
    275 
    276 	/*
    277 	 * Conversion to uK is simple.
    278 	 */
    279 	return (temp * 62500 + 273150000);
    280 }
    281 
    282