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