Home | History | Annotate | Line # | Download | only in onewire
owtemp.c revision 1.18
      1  1.18    martin /*	$NetBSD: owtemp.c,v 1.18 2019/10/25 16:25:14 martin 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.18    martin __KERNEL_RCSID(0, "$NetBSD: owtemp.c,v 1.18 2019/10/25 16:25:14 martin 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.18    martin 	device_t			sc_dv;
     44  1.18    martin 	void				*sc_onewire;
     45   1.1       riz 	u_int64_t			sc_rom;
     46  1.18    martin 	const char			*sc_chipname;
     47   1.1       riz 
     48  1.12   xtraeme 	envsys_data_t			sc_sensor;
     49  1.12   xtraeme 	struct sysmon_envsys		*sc_sme;
     50   1.1       riz 
     51   1.1       riz 	uint32_t			(*sc_owtemp_decode)(const uint8_t *);
     52   1.1       riz 
     53   1.1       riz 	int				sc_dying;
     54   1.1       riz };
     55   1.1       riz 
     56  1.14   xtraeme static int	owtemp_match(device_t, cfdata_t, void *);
     57  1.14   xtraeme static void	owtemp_attach(device_t, device_t, void *);
     58  1.14   xtraeme static int	owtemp_detach(device_t, int);
     59  1.14   xtraeme static int	owtemp_activate(device_t, enum devact);
     60   1.1       riz 
     61  1.14   xtraeme static void	owtemp_update(void *);
     62   1.1       riz 
     63  1.14   xtraeme CFATTACH_DECL_NEW(owtemp, sizeof(struct owtemp_softc),
     64   1.1       riz 	owtemp_match, owtemp_attach, owtemp_detach, owtemp_activate);
     65   1.1       riz 
     66   1.1       riz extern struct cfdriver owtemp_cd;
     67   1.1       riz 
     68   1.1       riz static const struct onewire_matchfam owtemp_fams[] = {
     69  1.17    kardel 	{ ONEWIRE_FAMILY_DS1920 }, /* also DS1820 */
     70   1.1       riz 	{ ONEWIRE_FAMILY_DS18B20 },
     71   1.1       riz 	{ ONEWIRE_FAMILY_DS1822 },
     72   1.1       riz };
     73   1.1       riz 
     74  1.12   xtraeme static void	owtemp_refresh(struct sysmon_envsys *, envsys_data_t *);
     75   1.1       riz 
     76   1.1       riz static uint32_t	owtemp_decode_ds18b20(const uint8_t *);
     77   1.1       riz static uint32_t	owtemp_decode_ds1920(const uint8_t *);
     78   1.1       riz 
     79  1.14   xtraeme static int
     80  1.14   xtraeme owtemp_match(device_t parent, cfdata_t match, void *aux)
     81   1.1       riz {
     82   1.1       riz 	return (onewire_matchbyfam(aux, owtemp_fams,
     83   1.7   xtraeme 	    __arraycount(owtemp_fams)));
     84   1.1       riz }
     85   1.1       riz 
     86  1.14   xtraeme static void
     87  1.14   xtraeme owtemp_attach(device_t parent, device_t self, void *aux)
     88   1.1       riz {
     89   1.1       riz 	struct owtemp_softc *sc = device_private(self);
     90   1.1       riz 	struct onewire_attach_args *oa = aux;
     91   1.1       riz 
     92  1.14   xtraeme 	aprint_naive("\n");
     93  1.14   xtraeme 
     94  1.18    martin 	sc->sc_dv = self;
     95   1.1       riz 	sc->sc_onewire = oa->oa_onewire;
     96   1.1       riz 	sc->sc_rom = oa->oa_rom;
     97   1.1       riz 
     98   1.1       riz 	switch(ONEWIRE_ROM_FAMILY_TYPE(sc->sc_rom)) {
     99   1.1       riz 	case ONEWIRE_FAMILY_DS18B20:
    100  1.18    martin 		sc->sc_chipname = "DS18B20";
    101  1.18    martin 		sc->sc_owtemp_decode = owtemp_decode_ds18b20;
    102  1.18    martin 		break;
    103   1.1       riz 	case ONEWIRE_FAMILY_DS1822:
    104  1.18    martin 		sc->sc_chipname = "DS1822";
    105   1.1       riz 		sc->sc_owtemp_decode = owtemp_decode_ds18b20;
    106   1.1       riz 		break;
    107   1.1       riz 	case ONEWIRE_FAMILY_DS1920:
    108  1.18    martin 		sc->sc_chipname = "DS1920";
    109   1.1       riz 		sc->sc_owtemp_decode = owtemp_decode_ds1920;
    110   1.1       riz 		break;
    111   1.1       riz 	}
    112   1.1       riz 
    113  1.12   xtraeme 	sc->sc_sme = sysmon_envsys_create();
    114  1.12   xtraeme 
    115   1.1       riz 	/* Initialize sensor */
    116  1.12   xtraeme 	sc->sc_sensor.units = ENVSYS_STEMP;
    117  1.16  pgoyette 	sc->sc_sensor.state = ENVSYS_SINVALID;
    118  1.12   xtraeme 	(void)strlcpy(sc->sc_sensor.desc,
    119  1.14   xtraeme 	    device_xname(self), sizeof(sc->sc_sensor.desc));
    120  1.18    martin 	(void)snprintf(sc->sc_sensor.desc, sizeof(sc->sc_sensor.desc),
    121  1.18    martin 	    "%s S/N %012" PRIx64, sc->sc_chipname, ONEWIRE_ROM_SN(sc->sc_rom));
    122  1.12   xtraeme 	if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) {
    123  1.12   xtraeme 		sysmon_envsys_destroy(sc->sc_sme);
    124  1.12   xtraeme 		return;
    125  1.12   xtraeme 	}
    126   1.1       riz 
    127   1.1       riz 	/* Hook into system monitor. */
    128  1.14   xtraeme 	sc->sc_sme->sme_name = device_xname(self);
    129  1.12   xtraeme 	sc->sc_sme->sme_cookie = sc;
    130  1.12   xtraeme 	sc->sc_sme->sme_refresh = owtemp_refresh;
    131   1.1       riz 
    132  1.12   xtraeme 	if (sysmon_envsys_register(sc->sc_sme)) {
    133  1.14   xtraeme 		aprint_error_dev(self, "unable to register with sysmon\n");
    134  1.12   xtraeme 		sysmon_envsys_destroy(sc->sc_sme);
    135  1.12   xtraeme 		return;
    136  1.12   xtraeme 	}
    137   1.1       riz 
    138  1.14   xtraeme 	aprint_normal("\n");
    139   1.1       riz }
    140   1.1       riz 
    141  1.14   xtraeme static int
    142  1.14   xtraeme owtemp_detach(device_t self, int flags)
    143   1.1       riz {
    144   1.1       riz 	struct owtemp_softc *sc = device_private(self);
    145   1.1       riz 
    146  1.12   xtraeme 	sysmon_envsys_unregister(sc->sc_sme);
    147   1.1       riz 
    148   1.1       riz 	return 0;
    149   1.1       riz }
    150   1.1       riz 
    151  1.14   xtraeme static int
    152  1.14   xtraeme owtemp_activate(device_t self, enum devact act)
    153   1.1       riz {
    154   1.1       riz 	struct owtemp_softc *sc = device_private(self);
    155   1.1       riz 
    156   1.1       riz 	switch (act) {
    157   1.1       riz 	case DVACT_DEACTIVATE:
    158   1.1       riz 		sc->sc_dying = 1;
    159  1.15    dyoung 		return 0;
    160  1.15    dyoung 	default:
    161  1.15    dyoung 		return EOPNOTSUPP;
    162   1.1       riz 	}
    163   1.1       riz }
    164   1.1       riz 
    165  1.14   xtraeme static void
    166   1.1       riz owtemp_update(void *arg)
    167   1.1       riz {
    168   1.1       riz 	struct owtemp_softc *sc = arg;
    169   1.1       riz 	u_int8_t data[9];
    170   1.1       riz 
    171  1.11   xtraeme 	onewire_lock(sc->sc_onewire);
    172  1.18    martin 	if (onewire_reset(sc->sc_onewire) != 0) {
    173  1.18    martin 		aprint_error_dev(sc->sc_dv, "owtemp_update: 1st reset failed\n");
    174   1.1       riz 		goto done;
    175  1.18    martin 	}
    176   1.1       riz 	onewire_matchrom(sc->sc_onewire, sc->sc_rom);
    177   1.1       riz 
    178   1.1       riz 	/*
    179   1.1       riz 	 * Start temperature conversion. The conversion takes up to 750ms.
    180   1.1       riz 	 * After sending the command, the data line must be held high for
    181   1.1       riz 	 * at least 750ms to provide power during the conversion process.
    182   1.1       riz 	 * As such, no other activity may take place on the 1-Wire bus for
    183  1.18    martin 	 * at least this period.  Keep the parent bus locked while waiting.
    184   1.1       riz 	 */
    185   1.1       riz 	onewire_write_byte(sc->sc_onewire, DS_CMD_CONVERT);
    186  1.18    martin 	kpause("owtemp", false, mstohz(750 + 10), NULL);
    187   1.1       riz 
    188  1.18    martin 	if (onewire_reset(sc->sc_onewire) != 0) {
    189  1.18    martin 		aprint_error_dev(sc->sc_dv, "owtemp_update: 2nd reset failed\n");
    190   1.1       riz 		goto done;
    191  1.18    martin 	}
    192   1.1       riz 	onewire_matchrom(sc->sc_onewire, sc->sc_rom);
    193   1.1       riz 
    194   1.1       riz 	/*
    195   1.1       riz 	 * The result of the temperature measurement is placed in the
    196   1.1       riz 	 * first two bytes of the scratchpad.
    197   1.1       riz 	 */
    198   1.1       riz 	onewire_write_byte(sc->sc_onewire, DS_CMD_READ_SCRATCHPAD);
    199   1.1       riz 	onewire_read_block(sc->sc_onewire, data, 9);
    200   1.1       riz #if 0
    201   1.1       riz 	if (onewire_crc(data, 8) == data[8]) {
    202   1.1       riz 		sc->sc_sensor.value = 273150000 +
    203   1.1       riz 		    (int)((u_int16_t)data[1] << 8 | data[0]) * 500000;
    204   1.1       riz 	}
    205   1.1       riz #endif
    206   1.1       riz 
    207  1.12   xtraeme 	sc->sc_sensor.value_cur = sc->sc_owtemp_decode(data);
    208  1.12   xtraeme 	sc->sc_sensor.state = ENVSYS_SVALID;
    209   1.1       riz 
    210   1.1       riz done:
    211   1.1       riz 	onewire_unlock(sc->sc_onewire);
    212   1.1       riz }
    213   1.1       riz 
    214  1.12   xtraeme static void
    215  1.12   xtraeme owtemp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    216   1.1       riz {
    217   1.1       riz 	struct owtemp_softc *sc = sme->sme_cookie;
    218   1.1       riz 
    219   1.1       riz 	owtemp_update(sc);
    220   1.1       riz }
    221   1.1       riz 
    222   1.1       riz static uint32_t
    223   1.1       riz owtemp_decode_ds18b20(const uint8_t *buf)
    224   1.1       riz {
    225   1.1       riz 	int temp;
    226   1.1       riz 
    227   1.1       riz 	/*
    228   1.1       riz 	 * Sign-extend the MSB byte, and add in the fractions of a
    229   1.1       riz 	 * degree contained in the LSB (precision 1/16th DegC).
    230   1.1       riz 	 */
    231   1.1       riz 	temp = (int8_t)buf[1];
    232   1.1       riz 	temp = (temp << 8) | buf[0];
    233   1.1       riz 
    234   1.1       riz 	/*
    235   1.1       riz 	 * Conversion to uK is simple.
    236   1.1       riz 	 */
    237   1.1       riz 	return (temp * 62500 + 273150000);
    238   1.1       riz }
    239   1.1       riz 
    240   1.1       riz static uint32_t
    241   1.1       riz owtemp_decode_ds1920(const uint8_t *buf)
    242   1.1       riz {
    243   1.1       riz 	int temp;
    244   1.1       riz 
    245   1.1       riz 	/*
    246   1.1       riz 	 * Sign-extend the MSB byte, and add in the fractions of a
    247   1.1       riz 	 * degree contained in the LSB (precision 1/2 DegC).
    248   1.1       riz 	 */
    249   1.1       riz 	temp = (int8_t)buf[1];
    250   1.1       riz 	temp = (temp << 8) | buf[0];
    251   1.1       riz 
    252  1.17    kardel 	if (buf[7] != 0) {
    253  1.17    kardel 		/*
    254  1.17    kardel 	 	 * interpolate for higher precision using the count registers
    255  1.17    kardel 	 	 *
    256  1.17    kardel 	 	 * buf[7]: COUNT_PER_C(elsius)
    257  1.17    kardel 	 	 * buf[6]: COUNT_REMAIN
    258  1.17    kardel 	 	 *
    259  1.17    kardel 	 	 * T = TEMP - 0.25 + (COUNT_PER_C - COUNT_REMAIN) / COUNT_PER_C
    260  1.17    kardel 	 	 */
    261  1.17    kardel 		temp &= ~1;
    262  1.17    kardel         	temp += 500000 * temp + (500000 * (buf[7] - buf[6])) / buf[7] - 250000;
    263  1.17    kardel 	} else {
    264  1.17    kardel 		temp *= 500000;
    265  1.17    kardel 	}
    266  1.17    kardel 
    267  1.17    kardel 	/* convert to uK */
    268  1.17    kardel 	return (temp + 273150000);
    269   1.1       riz }
    270