Home | History | Annotate | Line # | Download | only in onewire
owtemp.c revision 1.1
      1 /* $NetBSD */
      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");
     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 	struct envsys_tre_data		sc_sensor[1];
     49 	struct envsys_basic_info	sc_info[1];
     50 
     51 	struct sysmon_envsys		sc_sysmon;
     52 
     53 	uint32_t			(*sc_owtemp_decode)(const uint8_t *);
     54 
     55 	int				sc_dying;
     56 };
     57 
     58 int	owtemp_match(struct device *, struct cfdata *, void *);
     59 void	owtemp_attach(struct device *, struct device *, void *);
     60 int	owtemp_detach(struct device *, int);
     61 int	owtemp_activate(struct device *, enum devact);
     62 
     63 void	owtemp_update(void *);
     64 
     65 CFATTACH_DECL(owtemp, sizeof(struct owtemp_softc),
     66 	owtemp_match, owtemp_attach, owtemp_detach, owtemp_activate);
     67 
     68 extern struct cfdriver owtemp_cd;
     69 
     70 static const struct onewire_matchfam owtemp_fams[] = {
     71 	{ ONEWIRE_FAMILY_DS1920 },
     72 	{ ONEWIRE_FAMILY_DS18B20 },
     73 	{ ONEWIRE_FAMILY_DS1822 },
     74 };
     75 
     76 static const struct envsys_range owtemp_ranges[] = {
     77 	{ 0, 1,		ENVSYS_STEMP },
     78 	{ 1, 0,		-1 },
     79 };
     80 
     81 static int	owtemp_gtredata(struct sysmon_envsys *,
     82 				struct envsys_tre_data *);
     83 static int	owtemp_streinfo(struct sysmon_envsys *,
     84 				struct envsys_basic_info *);
     85 
     86 static uint32_t	owtemp_decode_ds18b20(const uint8_t *);
     87 static uint32_t	owtemp_decode_ds1920(const uint8_t *);
     88 
     89 int
     90 owtemp_match(struct device *parent, struct cfdata *cf, void *aux)
     91 {
     92 	return (onewire_matchbyfam(aux, owtemp_fams,
     93 	    sizeof(owtemp_fams) /sizeof(owtemp_fams[0])));
     94 }
     95 
     96 void
     97 owtemp_attach(struct device *parent, struct device *self, void *aux)
     98 {
     99 	struct owtemp_softc *sc = device_private(self);
    100 	struct onewire_attach_args *oa = aux;
    101 	int ptype;
    102 
    103 	sc->sc_onewire = oa->oa_onewire;
    104 	sc->sc_rom = oa->oa_rom;
    105 
    106 	switch(ONEWIRE_ROM_FAMILY_TYPE(sc->sc_rom)) {
    107 	case ONEWIRE_FAMILY_DS18B20:
    108 	case ONEWIRE_FAMILY_DS1822:
    109 		sc->sc_owtemp_decode = owtemp_decode_ds18b20;
    110 		break;
    111 	case ONEWIRE_FAMILY_DS1920:
    112 		sc->sc_owtemp_decode = owtemp_decode_ds1920;
    113 		break;
    114 	}
    115 
    116 	/* Initialize sensor */
    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 	if (devprop_get(&sc->sc_dev, "description",
    124 		sc->sc_info[0].desc, sizeof(sc->sc_info[0].desc),
    125 		&ptype) < 1 ||
    126 	    ptype != PROP_STRING)
    127 		strcpy(sc->sc_info[0].desc, sc->sc_dev.dv_xname);
    128 
    129 	/* Hook into system monitor. */
    130 	sc->sc_sysmon.sme_ranges = owtemp_ranges;
    131 	sc->sc_sysmon.sme_sensor_info = sc->sc_info;
    132 	sc->sc_sysmon.sme_sensor_data = sc->sc_sensor;
    133 	sc->sc_sysmon.sme_cookie = sc;
    134 
    135 	sc->sc_sysmon.sme_gtredata = owtemp_gtredata;
    136 	sc->sc_sysmon.sme_streinfo = owtemp_streinfo;
    137 
    138 	sc->sc_sysmon.sme_nsensors = 1;
    139 	sc->sc_sysmon.sme_envsys_version = 1000;
    140 
    141 	if (sysmon_envsys_register(&sc->sc_sysmon))
    142 		aprint_error("%s: unable to register with sysmon\n",
    143 		    sc->sc_dev.dv_xname);
    144 
    145 #if 0  /* Old OpenBSD code */
    146 	strlcpy(sc->sc_sensor.device, sc->sc_dev.dv_xname,
    147 	    sizeof(sc->sc_sensor.device));
    148 	sc->sc_sensor.type = SENSOR_TEMP;
    149 	strlcpy(sc->sc_sensor.desc, "Temp", sizeof(sc->sc_sensor.desc));
    150 
    151 	if (sensor_task_register(sc, owtemp_update, 5)) {
    152 		printf(": unable to register update task\n");
    153 		return;
    154 	}
    155 	sensor_add(&sc->sc_sensor);
    156 #endif
    157 
    158 	printf("\n");
    159 }
    160 
    161 int
    162 owtemp_detach(struct device *self, int flags)
    163 {
    164 	struct owtemp_softc *sc = device_private(self);
    165 
    166 	sysmon_envsys_unregister(&sc->sc_sysmon);
    167 
    168 	return 0;
    169 }
    170 
    171 int
    172 owtemp_activate(struct device *self, enum devact act)
    173 {
    174 	struct owtemp_softc *sc = device_private(self);
    175 
    176 	switch (act) {
    177 	case DVACT_ACTIVATE:
    178 		return (EOPNOTSUPP);
    179 	case DVACT_DEACTIVATE:
    180 		sc->sc_dying = 1;
    181 		break;
    182 	}
    183 
    184 	return (0);
    185 }
    186 
    187 void
    188 owtemp_update(void *arg)
    189 {
    190 	struct owtemp_softc *sc = arg;
    191 	u_int8_t data[9];
    192 
    193 	onewire_lock(sc->sc_onewire, 0);
    194 	if (onewire_reset(sc->sc_onewire) != 0)
    195 		goto done;
    196 	onewire_matchrom(sc->sc_onewire, sc->sc_rom);
    197 
    198 	/*
    199 	 * Start temperature conversion. The conversion takes up to 750ms.
    200 	 * After sending the command, the data line must be held high for
    201 	 * at least 750ms to provide power during the conversion process.
    202 	 * As such, no other activity may take place on the 1-Wire bus for
    203 	 * at least this period.
    204 	 */
    205 	onewire_write_byte(sc->sc_onewire, DS_CMD_CONVERT);
    206 	tsleep(sc, PRIBIO, "owtemp", hz);
    207 
    208 	if (onewire_reset(sc->sc_onewire) != 0)
    209 		goto done;
    210 	onewire_matchrom(sc->sc_onewire, sc->sc_rom);
    211 
    212 	/*
    213 	 * The result of the temperature measurement is placed in the
    214 	 * first two bytes of the scratchpad.
    215 	 */
    216 	onewire_write_byte(sc->sc_onewire, DS_CMD_READ_SCRATCHPAD);
    217 	onewire_read_block(sc->sc_onewire, data, 9);
    218 #if 0
    219 	if (onewire_crc(data, 8) == data[8]) {
    220 		sc->sc_sensor.value = 273150000 +
    221 		    (int)((u_int16_t)data[1] << 8 | data[0]) * 500000;
    222 	}
    223 #endif
    224 
    225 	sc->sc_sensor[0].cur.data_us = sc->sc_owtemp_decode(data);
    226 	sc->sc_sensor[0].validflags |= ENVSYS_FCURVALID;
    227 
    228 done:
    229 	onewire_unlock(sc->sc_onewire);
    230 }
    231 
    232 static int
    233 owtemp_gtredata(struct sysmon_envsys *sme, struct envsys_tre_data *tred)
    234 {
    235 	struct owtemp_softc *sc = sme->sme_cookie;
    236 
    237 	owtemp_update(sc);
    238 	*tred = sc->sc_sensor[tred->sensor];
    239 
    240 	return 0;
    241 }
    242 
    243 static int
    244 owtemp_streinfo(struct sysmon_envsys *sme, struct envsys_basic_info *binfo)
    245 {
    246 	struct owtemp_softc *sc = sme->sme_cookie;
    247 
    248 	onewire_lock(sc->sc_onewire,0);	/* Also locks our instance */
    249 
    250 	memcpy(sc->sc_info[binfo->sensor].desc, binfo->desc,
    251 	    sizeof(sc->sc_info[binfo->sensor].desc));
    252 	sc->sc_info[binfo->sensor].desc[
    253 	    sizeof(sc->sc_info[binfo->sensor].desc) - 1] = '\0';
    254 
    255 	onewire_unlock(sc->sc_onewire);
    256 
    257 	binfo->validflags = ENVSYS_FVALID;
    258 
    259 	return 0;
    260 }
    261 
    262 static uint32_t
    263 owtemp_decode_ds18b20(const uint8_t *buf)
    264 {
    265 	int temp;
    266 
    267 	/*
    268 	 * Sign-extend the MSB byte, and add in the fractions of a
    269 	 * degree contained in the LSB (precision 1/16th DegC).
    270 	 */
    271 	temp = (int8_t)buf[1];
    272 	temp = (temp << 8) | buf[0];
    273 
    274 	/*
    275 	 * Conversion to uK is simple.
    276 	 */
    277 	return (temp * 62500 + 273150000);
    278 }
    279 
    280 static uint32_t
    281 owtemp_decode_ds1920(const uint8_t *buf)
    282 {
    283 	int temp;
    284 
    285 	/*
    286 	 * Sign-extend the MSB byte, and add in the fractions of a
    287 	 * degree contained in the LSB (precision 1/2 DegC).
    288 	 */
    289 	temp = (int8_t)buf[1];
    290 	temp = (temp << 8) | buf[0];
    291 
    292 	/* Convert to uK */
    293 	return (temp * 500000 + 273150000);
    294 }
    295