Home | History | Annotate | Line # | Download | only in onewire
owtemp.c revision 1.11
      1 /*	$NetBSD: owtemp.c,v 1.11 2007/09/05 15:24:07 xtraeme Exp $ */
      2 /*	$OpenBSD: owtemp.c,v 1.1 2006/03/04 16:27:03 grange Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 2006 Alexander Yurchenko <grange (at) openbsd.org>
      6  *
      7  * Permission to use, copy, modify, and distribute this software for any
      8  * purpose with or without fee is hereby granted, provided that the above
      9  * copyright notice and this permission notice appear in all copies.
     10  *
     11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  */
     19 
     20 /*
     21  * 1-Wire temperature family type device driver.
     22  */
     23 
     24 #include <sys/cdefs.h>
     25 __KERNEL_RCSID(0, "$NetBSD: owtemp.c,v 1.11 2007/09/05 15:24:07 xtraeme Exp $");
     26 
     27 #include <sys/param.h>
     28 #include <sys/systm.h>
     29 #include <sys/device.h>
     30 #include <sys/kernel.h>
     31 #include <sys/proc.h>
     32 
     33 #include <dev/sysmon/sysmonvar.h>
     34 
     35 #include <dev/onewire/onewiredevs.h>
     36 #include <dev/onewire/onewirereg.h>
     37 #include <dev/onewire/onewirevar.h>
     38 
     39 #define DS_CMD_CONVERT		0x44
     40 #define DS_CMD_READ_SCRATCHPAD	0xbe
     41 
     42 struct owtemp_softc {
     43 	struct device			sc_dev;
     44 
     45 	void *				sc_onewire;
     46 	u_int64_t			sc_rom;
     47 
     48 	envsys_data_t			sc_sensor[1];
     49 	struct sysmon_envsys		sc_sysmon;
     50 
     51 	uint32_t			(*sc_owtemp_decode)(const uint8_t *);
     52 
     53 	int				sc_dying;
     54 };
     55 
     56 int	owtemp_match(struct device *, struct cfdata *, void *);
     57 void	owtemp_attach(struct device *, struct device *, void *);
     58 int	owtemp_detach(struct device *, int);
     59 int	owtemp_activate(struct device *, enum devact);
     60 
     61 void	owtemp_update(void *);
     62 
     63 CFATTACH_DECL(owtemp, sizeof(struct owtemp_softc),
     64 	owtemp_match, owtemp_attach, owtemp_detach, owtemp_activate);
     65 
     66 extern struct cfdriver owtemp_cd;
     67 
     68 static const struct onewire_matchfam owtemp_fams[] = {
     69 	{ ONEWIRE_FAMILY_DS1920 },
     70 	{ ONEWIRE_FAMILY_DS18B20 },
     71 	{ ONEWIRE_FAMILY_DS1822 },
     72 };
     73 
     74 static int	owtemp_gtredata(struct sysmon_envsys *, envsys_data_t *);
     75 
     76 static uint32_t	owtemp_decode_ds18b20(const uint8_t *);
     77 static uint32_t	owtemp_decode_ds1920(const uint8_t *);
     78 
     79 int
     80 owtemp_match(struct device *parent, struct cfdata *cf, void *aux)
     81 {
     82 	return (onewire_matchbyfam(aux, owtemp_fams,
     83 	    __arraycount(owtemp_fams)));
     84 }
     85 
     86 void
     87 owtemp_attach(struct device *parent, struct device *self, void *aux)
     88 {
     89 	struct owtemp_softc *sc = device_private(self);
     90 	struct onewire_attach_args *oa = aux;
     91 
     92 	sc->sc_onewire = oa->oa_onewire;
     93 	sc->sc_rom = oa->oa_rom;
     94 
     95 	switch(ONEWIRE_ROM_FAMILY_TYPE(sc->sc_rom)) {
     96 	case ONEWIRE_FAMILY_DS18B20:
     97 	case ONEWIRE_FAMILY_DS1822:
     98 		sc->sc_owtemp_decode = owtemp_decode_ds18b20;
     99 		break;
    100 	case ONEWIRE_FAMILY_DS1920:
    101 		sc->sc_owtemp_decode = owtemp_decode_ds1920;
    102 		break;
    103 	}
    104 
    105 	/* Initialize sensor */
    106 	sc->sc_sensor[0].sensor = 0;
    107 	sc->sc_sensor[0].state = ENVSYS_SVALID;
    108 	sc->sc_sensor[0].units = ENVSYS_STEMP;
    109 	(void)strlcpy(sc->sc_sensor[0].desc,
    110 	    sc->sc_dev.dv_xname, sizeof(sc->sc_sensor[0].desc));
    111 
    112 	/* Hook into system monitor. */
    113 	sc->sc_sysmon.sme_name = sc->sc_dev.dv_xname;
    114 	sc->sc_sysmon.sme_sensor_data = sc->sc_sensor;
    115 	sc->sc_sysmon.sme_cookie = sc;
    116 	sc->sc_sysmon.sme_gtredata = owtemp_gtredata;
    117 	sc->sc_sysmon.sme_nsensors = 1;
    118 
    119 	if (sysmon_envsys_register(&sc->sc_sysmon))
    120 		aprint_error("%s: unable to register with sysmon\n",
    121 		    sc->sc_dev.dv_xname);
    122 
    123 	printf("\n");
    124 }
    125 
    126 int
    127 owtemp_detach(struct device *self, int flags)
    128 {
    129 	struct owtemp_softc *sc = device_private(self);
    130 
    131 	sysmon_envsys_unregister(&sc->sc_sysmon);
    132 
    133 	return 0;
    134 }
    135 
    136 int
    137 owtemp_activate(struct device *self, enum devact act)
    138 {
    139 	struct owtemp_softc *sc = device_private(self);
    140 
    141 	switch (act) {
    142 	case DVACT_ACTIVATE:
    143 		return (EOPNOTSUPP);
    144 	case DVACT_DEACTIVATE:
    145 		sc->sc_dying = 1;
    146 		break;
    147 	}
    148 
    149 	return (0);
    150 }
    151 
    152 void
    153 owtemp_update(void *arg)
    154 {
    155 	struct owtemp_softc *sc = arg;
    156 	u_int8_t data[9];
    157 
    158 	onewire_lock(sc->sc_onewire);
    159 	if (onewire_reset(sc->sc_onewire) != 0)
    160 		goto done;
    161 	onewire_matchrom(sc->sc_onewire, sc->sc_rom);
    162 
    163 	/*
    164 	 * Start temperature conversion. The conversion takes up to 750ms.
    165 	 * After sending the command, the data line must be held high for
    166 	 * at least 750ms to provide power during the conversion process.
    167 	 * As such, no other activity may take place on the 1-Wire bus for
    168 	 * at least this period.
    169 	 */
    170 	onewire_write_byte(sc->sc_onewire, DS_CMD_CONVERT);
    171 	tsleep(sc, PRIBIO, "owtemp", hz);
    172 
    173 	if (onewire_reset(sc->sc_onewire) != 0)
    174 		goto done;
    175 	onewire_matchrom(sc->sc_onewire, sc->sc_rom);
    176 
    177 	/*
    178 	 * The result of the temperature measurement is placed in the
    179 	 * first two bytes of the scratchpad.
    180 	 */
    181 	onewire_write_byte(sc->sc_onewire, DS_CMD_READ_SCRATCHPAD);
    182 	onewire_read_block(sc->sc_onewire, data, 9);
    183 #if 0
    184 	if (onewire_crc(data, 8) == data[8]) {
    185 		sc->sc_sensor.value = 273150000 +
    186 		    (int)((u_int16_t)data[1] << 8 | data[0]) * 500000;
    187 	}
    188 #endif
    189 
    190 	sc->sc_sensor[0].value_cur = sc->sc_owtemp_decode(data);
    191 	sc->sc_sensor[0].state = ENVSYS_SVALID;
    192 
    193 done:
    194 	onewire_unlock(sc->sc_onewire);
    195 }
    196 
    197 static int
    198 owtemp_gtredata(struct sysmon_envsys *sme, envsys_data_t *edata)
    199 {
    200 	struct owtemp_softc *sc = sme->sme_cookie;
    201 
    202 	owtemp_update(sc);
    203 	return 0;
    204 }
    205 
    206 static uint32_t
    207 owtemp_decode_ds18b20(const uint8_t *buf)
    208 {
    209 	int temp;
    210 
    211 	/*
    212 	 * Sign-extend the MSB byte, and add in the fractions of a
    213 	 * degree contained in the LSB (precision 1/16th DegC).
    214 	 */
    215 	temp = (int8_t)buf[1];
    216 	temp = (temp << 8) | buf[0];
    217 
    218 	/*
    219 	 * Conversion to uK is simple.
    220 	 */
    221 	return (temp * 62500 + 273150000);
    222 }
    223 
    224 static uint32_t
    225 owtemp_decode_ds1920(const uint8_t *buf)
    226 {
    227 	int temp;
    228 
    229 	/*
    230 	 * Sign-extend the MSB byte, and add in the fractions of a
    231 	 * degree contained in the LSB (precision 1/2 DegC).
    232 	 */
    233 	temp = (int8_t)buf[1];
    234 	temp = (temp << 8) | buf[0];
    235 
    236 	/* Convert to uK */
    237 	return (temp * 500000 + 273150000);
    238 }
    239