diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 2006-01-10 23:02:33 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 2006-01-10 23:02:33 +0000 |
commit | d361e8768ae1f4dc915e9c8f576922a4c471742c (patch) | |
tree | 1a984d39631c7be52ba4d02a62a24fbb70de4b08 /sys/dev | |
parent | 8840d56425d35fc01612b080cc8bf2a3bbf055fe (diff) |
rewritten lm75 driver. try to cope with the lm75 and lm77 better, but
something is still odd or wrong with a ds1775 i have (which is returning
absolute garbage); tested by kettenis grange
Diffstat (limited to 'sys/dev')
-rw-r--r-- | sys/dev/i2c/lm75.c | 181 | ||||
-rw-r--r-- | sys/dev/i2c/lm75reg.h | 180 |
2 files changed, 103 insertions, 258 deletions
diff --git a/sys/dev/i2c/lm75.c b/sys/dev/i2c/lm75.c index 55915b9f1db..9a9d054d720 100644 --- a/sys/dev/i2c/lm75.c +++ b/sys/dev/i2c/lm75.c @@ -1,6 +1,7 @@ -/* $OpenBSD: lm75.c,v 1.9 2006/01/09 23:30:38 deraadt Exp $ */ +/* $OpenBSD: lm75.c,v 1.10 2006/01/10 23:02:32 deraadt Exp $ */ /* $NetBSD: lm75.c,v 1.1 2003/09/30 00:35:31 thorpej Exp $ */ /* + * Copyright (c) 2006 Theo de Raadt <deraadt@openbsd.org> * Copyright (c) 2004 Alexander Yurchenko <grange@openbsd.org> * * Permission to use, copy, modify, and distribute this software for any @@ -17,41 +18,6 @@ */ /* - * Copyright (c) 2003 Wasabi Systems, Inc. - * All rights reserved. - * - * Written by Jason R. Thorpe for Wasabi Systems, Inc. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed for the NetBSD Project by - * Wasabi Systems, Inc. - * 4. The name of Wasabi Systems, Inc. may not be used to endorse - * or promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC - * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -/* * National Semiconductor LM75/LM77 temperature sensor. */ @@ -62,13 +28,37 @@ #include <sys/sensors.h> #include <dev/i2c/i2cvar.h> -#include <dev/i2c/lm75reg.h> + +#define LM_MODEL_LM75 1 +#define LM_MODEL_LM77 2 +#define LM_MODEL_DS1775 3 + +#define LM_POLLTIME 3 /* 3s */ + +#define LM75_REG_TEMP 0x00 +#define LM75_REG_CONFIG 0x01 +#define LM75_CONFIG_SHUTDOWN 0x01 +#define LM75_CONFIG_CMPINT 0x02 +#define LM75_CONFIG_OSPOLARITY 0x04 +#define LM75_CONFIG_FAULT_QUEUE_MASK 0x18 +#define LM75_CONFIG_FAULT_QUEUE_1 (0 << 3) +#define LM75_CONFIG_FAULT_QUEUE_2 (1 << 3) +#define LM75_CONFIG_FAULT_QUEUE_4 (2 << 3) +#define LM75_CONFIG_FAULT_QUEUE_6 (3 << 3) +#define LM77_CONFIG_INTPOLARITY 0x08 +#define LM77_CONFIG_FAULT_QUEUE_4 0x10 +#define DS1755_CONFIG_RESOLUTION(i) (9 + (((i) >> 5) & 3)) +#define LM75_REG_THYST_SET_POINT 0x02 +#define LM75_REG_TOS_SET_POINT 0x03 +#define LM77_REG_TLOW 0x04 +#define LM77_REG_THIGH 0x05 struct lmtemp_softc { struct device sc_dev; i2c_tag_t sc_tag; - int sc_address; - int sc_model; + int sc_addr; + int sc_model; + int sc_bits; struct sensor sc_sensor; }; @@ -86,7 +76,43 @@ struct cfdriver lmtemp_cd = { NULL, "lmtemp", DV_DULL }; -int lmtemp_config_write(struct lmtemp_softc *, uint8_t); +/* + * Temperature on the LM75 is represented by a 9-bit two's complement + * integer in steps of 0.5C. The following examples are taken from + * the LM75 data sheet: + * + * +125C 0 1111 1010 0x0fa + * +25C 0 0011 0010 0x032 + * +0.5C 0 0000 0001 0x001 + * 0C 0 0000 0000 0x000 + * -0.5C 1 1111 1111 0x1ff + * -25C 1 1100 1110 0x1ce + * -55C 1 1001 0010 0x192 + * + * Temperature on the LM77 is represented by a 10-bit two's complement + * integer in steps of 0.5C: + * + * +130C 01 0000 0100 0x104 + * +125C 00 1111 1010 0x0fa + * +25C 00 0011 0010 0x032 + * +0.5C 00 0000 0001 0x001 + * 0C 00 0000 0000 0x000 + * -0.5C 11 1111 1111 0x3ff + * -25C 11 1100 1110 0x3ce + * -55C 11 1001 0010 0x392 + * + * LM75 temperature word: + * + * MSB Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0 X X X X X X X + * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 + * + * + * LM77 temperature word: + * + * Sign Sign Sign Sign MSB Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0 Status bits + * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 + */ + int lmtemp_temp_read(struct lmtemp_softc *, uint8_t, int *); void lmtemp_refresh_sensor_data(void *); @@ -107,26 +133,46 @@ lmtemp_attach(struct device *parent, struct device *self, void *aux) { struct lmtemp_softc *sc = (struct lmtemp_softc *)self; struct i2c_attach_args *ia = aux; + u_int8_t cmd, data; sc->sc_tag = ia->ia_tag; - sc->sc_address = ia->ia_addr; + sc->sc_addr = ia->ia_addr; - sc->sc_model = LM_MODEL_LM75; - if (strcmp(ia->ia_name, "lm77") == 0) - sc->sc_model = LM_MODEL_LM77; - - printf(": %s\n", ia->ia_name); + printf(": %s", ia->ia_name); - /* Set the configuration to defaults */ + /* If in SHUTDOWN mode, wake it up */ iic_acquire_bus(sc->sc_tag, 0); - if (lmtemp_config_write(sc, 0) != 0) { - printf("%s: unable to write config register\n", - sc->sc_dev.dv_xname); + cmd = LM75_REG_CONFIG; + if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, + sc->sc_addr, &cmd, 1, &data, 1, 0)) { iic_release_bus(sc->sc_tag, 0); return; } + if (data & LM75_CONFIG_SHUTDOWN) { + data &= ~LM75_CONFIG_SHUTDOWN; + if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, + sc->sc_addr, &cmd, 1, &data, 1, 0)) { + printf(", cannot wake up\n"); + iic_release_bus(sc->sc_tag, 0); + return; + } + printf(", woken up"); + } iic_release_bus(sc->sc_tag, 0); + sc->sc_model = LM_MODEL_LM75; + sc->sc_bits = 9; + if (strcmp(ia->ia_name, "lm77") == 0) { + sc->sc_model = LM_MODEL_LM77; + sc->sc_bits = 13; + } else if (strcmp(ia->ia_name, "ds1775") == 0) { + sc->sc_model = LM_MODEL_DS1775; + sc->sc_bits = 9; + //sc->sc_bits = DS1755_CONFIG_RESOLUTION(data); + } + + printf("\n"); + /* Initialize sensor data */ strlcpy(sc->sc_sensor.device, sc->sc_dev.dv_xname, sizeof(sc->sc_sensor.device)); @@ -140,44 +186,23 @@ lmtemp_attach(struct device *parent, struct device *self, void *aux) } int -lmtemp_config_write(struct lmtemp_softc *sc, uint8_t val) -{ - uint8_t cmdbuf[2]; - - cmdbuf[0] = LM75_REG_CONFIG; - cmdbuf[1] = val; - - return (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, - sc->sc_address, cmdbuf, 1, &cmdbuf[1], 1, 0)); -} - -int lmtemp_temp_read(struct lmtemp_softc *sc, uint8_t which, int *valp) { - u_int8_t cmdbuf[1]; - u_int8_t buf[LM75_TEMP_LEN]; + u_int8_t cmd, buf[2]; int error; - cmdbuf[0] = which; - + cmd = which; error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, - sc->sc_address, cmdbuf, 1, buf, LM75_TEMP_LEN, 0); + sc->sc_addr, &cmd, 1, buf, 2, 0); if (error) return (error); - switch (sc->sc_model) { - case LM_MODEL_LM75: - *valp = lm75_wordtotemp((buf[0] << 8) | buf[1]); - break; - case LM_MODEL_LM77: - *valp = lm77_wordtotemp((buf[0] << 8) | buf[1]); - break; - default: - printf("%s: unknown model (%d)\n", - sc->sc_dev.dv_xname, sc->sc_model); + /* Some chips return transient 0's.. we try next time */ + if (buf[0] == 0x00 && buf[1] == 0x00) return (1); - } + /* convert to half-degrees C */ + *valp = ((buf[0] << 8) | buf[1]) / (1 << (16 - sc->sc_bits)); return (0); } diff --git a/sys/dev/i2c/lm75reg.h b/sys/dev/i2c/lm75reg.h deleted file mode 100644 index 3eee0f079d7..00000000000 --- a/sys/dev/i2c/lm75reg.h +++ /dev/null @@ -1,180 +0,0 @@ -/* $OpenBSD: lm75reg.h,v 1.2 2005/11/11 16:14:14 kettenis Exp $ */ -/* $NetBSD: lm75reg.h,v 1.1 2003/09/30 00:35:31 thorpej Exp $ */ -/* - * Copyright (c) 2004 Alexander Yurchenko <grange@openbsd.org> - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -/* - * Copyright (c) 2003 Wasabi Systems, Inc. - * All rights reserved. - * - * Written by Jason R. Thorpe for Wasabi Systems, Inc. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed for the NetBSD Project by - * Wasabi Systems, Inc. - * 4. The name of Wasabi Systems, Inc. may not be used to endorse - * or promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC - * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef _DEV_I2C_LM75REG_H_ -#define _DEV_I2C_LM75REG_H_ - -#define LM_MODEL_LM75 1 -#define LM_MODEL_LM77 2 - -#define LM_POLLTIME 2 /* 2s */ - -/* - * LM75 temperature sensor I2C address: - * - * 100 1xxx - */ -#define LM75_ADDRMASK 0x78 -#define LM75_ADDR 0x48 - -/* - * LM77 temperature sensor I2C address: - * - * 100 10xx - */ -#define LM77_ADDRMASK 0x7c -#define LM77_ADDR 0x48 - -/* - * Temperature on the LM75 is represented by a 9-bit two's complement - * integer in steps of 0.5C. The following examples are taken from - * the LM75 data sheet: - * - * +125C 0 1111 1010 0x0fa - * +25C 0 0011 0010 0x032 - * +0.5C 0 0000 0001 0x001 - * 0C 0 0000 0000 0x000 - * -0.5C 1 1111 1111 0x1ff - * -25C 1 1100 1110 0x1ce - * -55C 1 1001 0010 0x192 - * - * Temperature on the LM77 is represented by a 10-bit two's complement - * integer in steps of 0.5C: - * - * +130C 01 0000 0100 0x104 - * +125C 00 1111 1010 0x0fa - * +25C 00 0011 0010 0x032 - * +0.5C 00 0000 0001 0x001 - * 0C 00 0000 0000 0x000 - * -0.5C 11 1111 1111 0x3ff - * -25C 11 1100 1110 0x3ce - * -55C 11 1001 0010 0x392 - */ - -#define LM75_REG_TEMP 0x00 -#define LM75_REG_CONFIG 0x01 -#define LM75_REG_THYST_SET_POINT 0x02 -#define LM75_REG_TOS_SET_POINT 0x03 - -#define LM77_REG_TLOW 0x04 -#define LM77_REG_THIGH 0x05 - -#define LM77_TEMP_ST_LOW 0x01 -#define LM77_TEMP_ST_HIGH 0x02 -#define LM77_TEMP_ST_CRIT 0x04 - -#define LM75_TEMP_LEN 2 /* 2 data bytes */ - -#define LM75_CONFIG_SHUTDOWN 0x01 -#define LM75_CONFIG_CMPINT 0x02 -#define LM75_CONFIG_OSPOLARITY 0x04 -#define LM75_CONFIG_FAULT_QUEUE_1 (0 << 3) -#define LM75_CONFIG_FAULT_QUEUE_2 (1 << 3) -#define LM75_CONFIG_FAULT_QUEUE_4 (2 << 3) -#define LM75_CONFIG_FAULT_QUEUE_6 (3 << 3) - -#define LM77_CONFIG_INTPOLARITY 0x08 -#define LM77_CONFIG_FAULT_QUEUE_4 0x10 - -/* - * LM75 temperature word: - * - * MSB Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0 X X X X X X X - * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 - * - * - * LM77 temperature word: - * - * Sign Sign Sign Sign MSB Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0 Status bits - * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 - */ - -static __inline int -lm75_wordtotemp(u_int16_t word) -{ - return ((int16_t)word / 128); -} - -static __inline u_int16_t -lm75_temptoword(int temp) -{ - int sign = 0; - - if (temp < 0) { - sign = 1; - temp = -temp; - }; - - return ((sign << 15 ) | ((temp & 0xff) << 7)); -} - -static __inline int -lm77_wordtotemp(u_int16_t word) -{ - return ((int16_t)word / 8); -} - -static __inline u_int16_t -lm77_temptoword(int temp) -{ - int sign = 0; - - if (temp < 0) { - sign = 0xf; - temp = -temp; - }; - - return ((sign << 12 ) | ((temp & 0x1ff) << 3)); -} - -#endif /* _DEV_I2C_LM75REG_H_ */ |