Home | History | Annotate | Line # | Download | only in onewire
owtemp.c revision 1.9
      1 /*	$NetBSD: owtemp.c,v 1.9 2007/07/04 19:12:42 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.9 2007/07/04 19:12:42 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 
    110 	/* Hook into system monitor. */
    111 	sc->sc_sysmon.sme_name = sc->sc_dev.dv_xname;
    112 	sc->sc_sysmon.sme_sensor_data = sc->sc_sensor;
    113 	sc->sc_sysmon.sme_cookie = sc;
    114 	sc->sc_sysmon.sme_gtredata = owtemp_gtredata;
    115 	sc->sc_sysmon.sme_nsensors = 1;
    116 
    117 	if (sysmon_envsys_register(&sc->sc_sysmon))
    118 		aprint_error("%s: unable to register with sysmon\n",
    119 		    sc->sc_dev.dv_xname);
    120 
    121 	printf("\n");
    122 }
    123 
    124 int
    125 owtemp_detach(struct device *self, int flags)
    126 {
    127 	struct owtemp_softc *sc = device_private(self);
    128 
    129 	sysmon_envsys_unregister(&sc->sc_sysmon);
    130 
    131 	return 0;
    132 }
    133 
    134 int
    135 owtemp_activate(struct device *self, enum devact act)
    136 {
    137 	struct owtemp_softc *sc = device_private(self);
    138 
    139 	switch (act) {
    140 	case DVACT_ACTIVATE:
    141 		return (EOPNOTSUPP);
    142 	case DVACT_DEACTIVATE:
    143 		sc->sc_dying = 1;
    144 		break;
    145 	}
    146 
    147 	return (0);
    148 }
    149 
    150 void
    151 owtemp_update(void *arg)
    152 {
    153 	struct owtemp_softc *sc = arg;
    154 	u_int8_t data[9];
    155 
    156 	onewire_lock(sc->sc_onewire, 0);
    157 	if (onewire_reset(sc->sc_onewire) != 0)
    158 		goto done;
    159 	onewire_matchrom(sc->sc_onewire, sc->sc_rom);
    160 
    161 	/*
    162 	 * Start temperature conversion. The conversion takes up to 750ms.
    163 	 * After sending the command, the data line must be held high for
    164 	 * at least 750ms to provide power during the conversion process.
    165 	 * As such, no other activity may take place on the 1-Wire bus for
    166 	 * at least this period.
    167 	 */
    168 	onewire_write_byte(sc->sc_onewire, DS_CMD_CONVERT);
    169 	tsleep(sc, PRIBIO, "owtemp", hz);
    170 
    171 	if (onewire_reset(sc->sc_onewire) != 0)
    172 		goto done;
    173 	onewire_matchrom(sc->sc_onewire, sc->sc_rom);
    174 
    175 	/*
    176 	 * The result of the temperature measurement is placed in the
    177 	 * first two bytes of the scratchpad.
    178 	 */
    179 	onewire_write_byte(sc->sc_onewire, DS_CMD_READ_SCRATCHPAD);
    180 	onewire_read_block(sc->sc_onewire, data, 9);
    181 #if 0
    182 	if (onewire_crc(data, 8) == data[8]) {
    183 		sc->sc_sensor.value = 273150000 +
    184 		    (int)((u_int16_t)data[1] << 8 | data[0]) * 500000;
    185 	}
    186 #endif
    187 
    188 	sc->sc_sensor[0].value_cur = sc->sc_owtemp_decode(data);
    189 	sc->sc_sensor[0].state = ENVSYS_SVALID;
    190 
    191 done:
    192 	onewire_unlock(sc->sc_onewire);
    193 }
    194 
    195 static int
    196 owtemp_gtredata(struct sysmon_envsys *sme, envsys_data_t *edata)
    197 {
    198 	struct owtemp_softc *sc = sme->sme_cookie;
    199 
    200 	owtemp_update(sc);
    201 	return 0;
    202 }
    203 
    204 static uint32_t
    205 owtemp_decode_ds18b20(const uint8_t *buf)
    206 {
    207 	int temp;
    208 
    209 	/*
    210 	 * Sign-extend the MSB byte, and add in the fractions of a
    211 	 * degree contained in the LSB (precision 1/16th DegC).
    212 	 */
    213 	temp = (int8_t)buf[1];
    214 	temp = (temp << 8) | buf[0];
    215 
    216 	/*
    217 	 * Conversion to uK is simple.
    218 	 */
    219 	return (temp * 62500 + 273150000);
    220 }
    221 
    222 static uint32_t
    223 owtemp_decode_ds1920(const uint8_t *buf)
    224 {
    225 	int temp;
    226 
    227 	/*
    228 	 * Sign-extend the MSB byte, and add in the fractions of a
    229 	 * degree contained in the LSB (precision 1/2 DegC).
    230 	 */
    231 	temp = (int8_t)buf[1];
    232 	temp = (temp << 8) | buf[0];
    233 
    234 	/* Convert to uK */
    235 	return (temp * 500000 + 273150000);
    236 }
    237