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