Home | History | Annotate | Line # | Download | only in onewire
owtemp.c revision 1.3
      1 /*	$NetBSD: owtemp.c,v 1.3 2006/05/05 18:04:42 thorpej 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.3 2006/05/05 18:04:42 thorpej 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 	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 	prop_string_t desc;
    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 	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 = owtemp_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 = owtemp_gtredata;
    139 	sc->sc_sysmon.sme_streinfo = owtemp_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 #if 0  /* Old OpenBSD code */
    149 	strlcpy(sc->sc_sensor.device, sc->sc_dev.dv_xname,
    150 	    sizeof(sc->sc_sensor.device));
    151 	sc->sc_sensor.type = SENSOR_TEMP;
    152 	strlcpy(sc->sc_sensor.desc, "Temp", sizeof(sc->sc_sensor.desc));
    153 
    154 	if (sensor_task_register(sc, owtemp_update, 5)) {
    155 		printf(": unable to register update task\n");
    156 		return;
    157 	}
    158 	sensor_add(&sc->sc_sensor);
    159 #endif
    160 
    161 	printf("\n");
    162 }
    163 
    164 int
    165 owtemp_detach(struct device *self, int flags)
    166 {
    167 	struct owtemp_softc *sc = device_private(self);
    168 
    169 	sysmon_envsys_unregister(&sc->sc_sysmon);
    170 
    171 	return 0;
    172 }
    173 
    174 int
    175 owtemp_activate(struct device *self, enum devact act)
    176 {
    177 	struct owtemp_softc *sc = device_private(self);
    178 
    179 	switch (act) {
    180 	case DVACT_ACTIVATE:
    181 		return (EOPNOTSUPP);
    182 	case DVACT_DEACTIVATE:
    183 		sc->sc_dying = 1;
    184 		break;
    185 	}
    186 
    187 	return (0);
    188 }
    189 
    190 void
    191 owtemp_update(void *arg)
    192 {
    193 	struct owtemp_softc *sc = arg;
    194 	u_int8_t data[9];
    195 
    196 	onewire_lock(sc->sc_onewire, 0);
    197 	if (onewire_reset(sc->sc_onewire) != 0)
    198 		goto done;
    199 	onewire_matchrom(sc->sc_onewire, sc->sc_rom);
    200 
    201 	/*
    202 	 * Start temperature conversion. The conversion takes up to 750ms.
    203 	 * After sending the command, the data line must be held high for
    204 	 * at least 750ms to provide power during the conversion process.
    205 	 * As such, no other activity may take place on the 1-Wire bus for
    206 	 * at least this period.
    207 	 */
    208 	onewire_write_byte(sc->sc_onewire, DS_CMD_CONVERT);
    209 	tsleep(sc, PRIBIO, "owtemp", hz);
    210 
    211 	if (onewire_reset(sc->sc_onewire) != 0)
    212 		goto done;
    213 	onewire_matchrom(sc->sc_onewire, sc->sc_rom);
    214 
    215 	/*
    216 	 * The result of the temperature measurement is placed in the
    217 	 * first two bytes of the scratchpad.
    218 	 */
    219 	onewire_write_byte(sc->sc_onewire, DS_CMD_READ_SCRATCHPAD);
    220 	onewire_read_block(sc->sc_onewire, data, 9);
    221 #if 0
    222 	if (onewire_crc(data, 8) == data[8]) {
    223 		sc->sc_sensor.value = 273150000 +
    224 		    (int)((u_int16_t)data[1] << 8 | data[0]) * 500000;
    225 	}
    226 #endif
    227 
    228 	sc->sc_sensor[0].cur.data_us = sc->sc_owtemp_decode(data);
    229 	sc->sc_sensor[0].validflags |= ENVSYS_FCURVALID;
    230 
    231 done:
    232 	onewire_unlock(sc->sc_onewire);
    233 }
    234 
    235 static int
    236 owtemp_gtredata(struct sysmon_envsys *sme, struct envsys_tre_data *tred)
    237 {
    238 	struct owtemp_softc *sc = sme->sme_cookie;
    239 
    240 	owtemp_update(sc);
    241 	*tred = sc->sc_sensor[tred->sensor];
    242 
    243 	return 0;
    244 }
    245 
    246 static int
    247 owtemp_streinfo(struct sysmon_envsys *sme, struct envsys_basic_info *binfo)
    248 {
    249 	struct owtemp_softc *sc = sme->sme_cookie;
    250 
    251 	onewire_lock(sc->sc_onewire,0);	/* Also locks our instance */
    252 
    253 	memcpy(sc->sc_info[binfo->sensor].desc, binfo->desc,
    254 	    sizeof(sc->sc_info[binfo->sensor].desc));
    255 	sc->sc_info[binfo->sensor].desc[
    256 	    sizeof(sc->sc_info[binfo->sensor].desc) - 1] = '\0';
    257 
    258 	onewire_unlock(sc->sc_onewire);
    259 
    260 	binfo->validflags = ENVSYS_FVALID;
    261 
    262 	return 0;
    263 }
    264 
    265 static uint32_t
    266 owtemp_decode_ds18b20(const uint8_t *buf)
    267 {
    268 	int temp;
    269 
    270 	/*
    271 	 * Sign-extend the MSB byte, and add in the fractions of a
    272 	 * degree contained in the LSB (precision 1/16th DegC).
    273 	 */
    274 	temp = (int8_t)buf[1];
    275 	temp = (temp << 8) | buf[0];
    276 
    277 	/*
    278 	 * Conversion to uK is simple.
    279 	 */
    280 	return (temp * 62500 + 273150000);
    281 }
    282 
    283 static uint32_t
    284 owtemp_decode_ds1920(const uint8_t *buf)
    285 {
    286 	int temp;
    287 
    288 	/*
    289 	 * Sign-extend the MSB byte, and add in the fractions of a
    290 	 * degree contained in the LSB (precision 1/2 DegC).
    291 	 */
    292 	temp = (int8_t)buf[1];
    293 	temp = (temp << 8) | buf[0];
    294 
    295 	/* Convert to uK */
    296 	return (temp * 500000 + 273150000);
    297 }
    298