Home | History | Annotate | Line # | Download | only in i2c
dstemp.c revision 1.12.4.2
      1  1.12.4.2   thorpej /* $NetBSD: dstemp.c,v 1.12.4.2 2021/08/01 22:42:23 thorpej Exp $ */
      2       1.1  macallan 
      3       1.1  macallan /*-
      4       1.1  macallan  * Copyright (c) 2018 Michael Lorenz
      5       1.1  macallan  * All rights reserved.
      6       1.1  macallan  *
      7       1.1  macallan  * Redistribution and use in source and binary forms, with or without
      8       1.1  macallan  * modification, are permitted provided that the following conditions
      9       1.1  macallan  * are met:
     10       1.1  macallan  * 1. Redistributions of source code must retain the above copyright
     11       1.1  macallan  *    notice, this list of conditions and the following disclaimer.
     12       1.1  macallan  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1  macallan  *    notice, this list of conditions and the following disclaimer in the
     14       1.1  macallan  *    documentation and/or other materials provided with the distribution.
     15       1.1  macallan  *
     16       1.1  macallan  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17       1.1  macallan  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18       1.1  macallan  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19       1.1  macallan  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20       1.1  macallan  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21       1.1  macallan  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22       1.1  macallan  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23       1.1  macallan  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24       1.1  macallan  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25       1.1  macallan  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26       1.1  macallan  * POSSIBILITY OF SUCH DAMAGE.
     27       1.1  macallan  */
     28       1.1  macallan 
     29       1.1  macallan #include <sys/cdefs.h>
     30  1.12.4.2   thorpej __KERNEL_RCSID(0, "$NetBSD: dstemp.c,v 1.12.4.2 2021/08/01 22:42:23 thorpej Exp $");
     31       1.1  macallan 
     32       1.1  macallan #include <sys/param.h>
     33       1.1  macallan #include <sys/systm.h>
     34       1.1  macallan #include <sys/device.h>
     35       1.1  macallan #include <sys/conf.h>
     36       1.1  macallan #include <sys/bus.h>
     37       1.1  macallan 
     38       1.1  macallan #include <dev/i2c/i2cvar.h>
     39       1.1  macallan 
     40       1.1  macallan #include <dev/sysmon/sysmonvar.h>
     41       1.1  macallan 
     42       1.1  macallan #define DSTEMP_CMD_START	0x51	/* command, no data */
     43       1.1  macallan #define DSTEMP_CMD_POR		0x54	/* command, no data */
     44       1.1  macallan #define DSTEMP_CMD_STOP		0x22	/* command, no data */
     45       1.1  macallan #define DSTEMP_TCURRENT		0xaa	/* current temperature, 2 bytes */
     46       1.1  macallan #define DSTEMP_THIGH		0xa1	/* high threshold, 2 bytes */
     47       1.1  macallan #define DSTEMP_TLOW		0xa2	/* low threshold, 2 bytes */
     48       1.1  macallan #define DSTEMP_CONFIG		0xac	/* 1 byte */
     49       1.1  macallan 
     50       1.5  macallan #define DSTEMP_1SHOT		0x01
     51       1.5  macallan #define DSTEMP_POL		0x02	/* Tout polarity, 1 - active high */
     52       1.5  macallan #define DSTEMP_8BIT		0x00
     53       1.5  macallan #define DSTEMP_10BIT		0x04
     54       1.5  macallan #define DSTEMP_11BIT		0x08
     55       1.5  macallan #define DSTEMP_12BIT		0x0c
     56       1.5  macallan #define DSTEMP_RES_MASK		0x0c
     57       1.5  macallan #define DSTEMP_NVB		0x10	/* EEPROM busy */
     58       1.5  macallan #define DSTEMP_TLF		0x20	/* temperature low flag */
     59       1.5  macallan #define DSTEMP_THF		0x40	/* temperature high flag */
     60       1.5  macallan #define DSTEMP_DONE		0x80
     61       1.5  macallan 
     62       1.1  macallan struct dstemp_softc {
     63       1.6  macallan 	device_t		sc_dev;
     64       1.6  macallan 	i2c_tag_t		sc_i2c;
     65       1.6  macallan 	i2c_addr_t		sc_addr;
     66       1.6  macallan 	prop_dictionary_t 	sc_prop;
     67       1.6  macallan 	struct sysmon_envsys 	*sc_sme;
     68       1.6  macallan 	envsys_data_t		sc_sensor_temp;
     69       1.1  macallan };
     70       1.1  macallan 
     71       1.1  macallan static int	dstemp_match(device_t, cfdata_t, void *);
     72       1.1  macallan static void	dstemp_attach(device_t, device_t, void *);
     73       1.1  macallan 
     74       1.1  macallan static void	dstemp_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
     75       1.5  macallan static void	dstemp_init(struct dstemp_softc *);
     76       1.1  macallan 
     77       1.1  macallan CFATTACH_DECL_NEW(dstemp, sizeof(struct dstemp_softc),
     78       1.1  macallan     dstemp_match, dstemp_attach, NULL, NULL);
     79       1.1  macallan 
     80       1.4   thorpej static const struct device_compatible_entry compat_data[] = {
     81      1.11   thorpej 	{ .compat = "dallas,ds1631" },
     82       1.7   thorpej 	{ .compat = "ds1631" },
     83       1.9   thorpej 	DEVICE_COMPAT_EOL
     84       1.3   thorpej };
     85       1.3   thorpej 
     86       1.1  macallan static int
     87       1.1  macallan dstemp_match(device_t parent, cfdata_t match, void *aux)
     88       1.1  macallan {
     89       1.1  macallan 	struct i2c_attach_args *ia = aux;
     90       1.2   thorpej 	int match_result;
     91       1.1  macallan 
     92       1.4   thorpej 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
     93       1.2   thorpej 		return match_result;
     94       1.2   thorpej 
     95       1.2   thorpej 	if ((ia->ia_addr & 0xf8) == 0x48)
     96       1.2   thorpej 		return I2C_MATCH_ADDRESS_ONLY;
     97       1.2   thorpej 
     98       1.2   thorpej 	return 0;
     99       1.1  macallan }
    100       1.1  macallan 
    101       1.1  macallan static void
    102       1.1  macallan dstemp_attach(device_t parent, device_t self, void *aux)
    103       1.1  macallan {
    104       1.1  macallan 	struct dstemp_softc *sc = device_private(self);
    105       1.1  macallan 	struct i2c_attach_args *ia = aux;
    106       1.1  macallan 	char name[64] = "temperature";
    107       1.6  macallan 	const char *desc;
    108       1.1  macallan 
    109       1.1  macallan 	sc->sc_dev = self;
    110       1.1  macallan 	sc->sc_i2c = ia->ia_tag;
    111       1.1  macallan 	sc->sc_addr = ia->ia_addr;
    112       1.6  macallan 	sc->sc_prop = ia->ia_prop;
    113      1.10   thorpej 	prop_object_retain(sc->sc_prop);
    114       1.1  macallan 
    115       1.1  macallan 	aprint_naive("\n");
    116       1.1  macallan 	aprint_normal(": DS1361\n");
    117       1.1  macallan 
    118       1.5  macallan 	dstemp_init(sc);
    119       1.5  macallan 
    120       1.1  macallan 	sc->sc_sme = sysmon_envsys_create();
    121       1.1  macallan 	sc->sc_sme->sme_name = device_xname(self);
    122       1.1  macallan 	sc->sc_sme->sme_cookie = sc;
    123       1.1  macallan 	sc->sc_sme->sme_refresh = dstemp_sensors_refresh;
    124       1.1  macallan 
    125       1.1  macallan 	sc->sc_sensor_temp.units = ENVSYS_STEMP;
    126       1.1  macallan 	sc->sc_sensor_temp.state = ENVSYS_SINVALID;
    127      1.12       rin 	sc->sc_sensor_temp.flags = ENVSYS_FHAS_ENTROPY;
    128       1.6  macallan 
    129  1.12.4.2   thorpej 	if (prop_dictionary_get_string(sc->sc_prop, "s00", &desc)) {
    130       1.6  macallan 		strncpy(name, desc, 64);
    131  1.12.4.2   thorpej 	} else if (prop_dictionary_get_string(sc->sc_prop, "saa", &desc)) {
    132       1.6  macallan 		strncpy(name, desc, 64);
    133       1.1  macallan 	}
    134       1.6  macallan 
    135       1.1  macallan 	strncpy(sc->sc_sensor_temp.desc, name, sizeof(sc->sc_sensor_temp.desc));
    136       1.1  macallan 
    137       1.1  macallan 	sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor_temp);
    138       1.1  macallan 
    139       1.1  macallan 	sysmon_envsys_register(sc->sc_sme);
    140       1.1  macallan }
    141       1.1  macallan 
    142       1.1  macallan static void
    143       1.5  macallan dstemp_init(struct dstemp_softc *sc)
    144       1.5  macallan {
    145       1.5  macallan 	int error;
    146       1.5  macallan 	uint8_t cmd[2], data;
    147       1.5  macallan 
    148  1.12.4.1   thorpej 	if (iic_acquire_bus(sc->sc_i2c, 0))
    149  1.12.4.1   thorpej 		return;
    150       1.5  macallan 	cmd[0] = DSTEMP_CONFIG;
    151       1.5  macallan 	data = 0;
    152       1.5  macallan 	error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
    153       1.5  macallan 	    sc->sc_addr, cmd, 1, &data, 1, 0);
    154       1.5  macallan 	/* we don't want to change the POL bit, so preserve it */
    155       1.5  macallan 	cmd[1] = (data & DSTEMP_POL) | DSTEMP_12BIT;
    156       1.5  macallan 	error |= iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
    157       1.5  macallan 	    sc->sc_addr, cmd, 2, NULL, 0, 0);
    158       1.5  macallan 	/* ... and start converting */
    159       1.5  macallan 	cmd[0] = DSTEMP_CMD_START;
    160       1.5  macallan 	error |= iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
    161       1.5  macallan 	    sc->sc_addr, cmd, 1, NULL, 0, 0);
    162       1.5  macallan 	if (error) aprint_error_dev(sc->sc_dev, "chip initialization failed\n");
    163       1.5  macallan 	iic_release_bus(sc->sc_i2c, 0);
    164       1.5  macallan }
    165       1.5  macallan 
    166       1.5  macallan static void
    167       1.1  macallan dstemp_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    168       1.1  macallan {
    169       1.1  macallan 	struct dstemp_softc *sc = sme->sme_cookie;
    170       1.1  macallan 	uint8_t cmd = DSTEMP_TCURRENT;
    171       1.1  macallan 	uint16_t data = 0;
    172       1.1  macallan 	int error;
    173       1.1  macallan 
    174       1.1  macallan 
    175  1.12.4.1   thorpej 	error = iic_acquire_bus(sc->sc_i2c, 0);
    176  1.12.4.1   thorpej 	if (error == 0) {
    177  1.12.4.1   thorpej 		error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
    178  1.12.4.1   thorpej 		    sc->sc_addr, &cmd, 1, &data, 2, 0);
    179  1.12.4.1   thorpej 		iic_release_bus(sc->sc_i2c, 0);
    180  1.12.4.1   thorpej 	}
    181       1.1  macallan 
    182       1.1  macallan 	if (error) {
    183       1.1  macallan 		edata->state = ENVSYS_SINVALID;
    184       1.1  macallan 	} else {
    185       1.1  macallan 		edata->value_cur =
    186       1.5  macallan 		    ((uint64_t)(be16toh(data) >> 4) * 62500) +
    187       1.1  macallan 		    + 273150000;
    188       1.5  macallan 		if (edata->value_cur > (273150000 + 120000000)) {
    189       1.5  macallan 			edata->state = ENVSYS_SINVALID;
    190       1.5  macallan 		} else
    191       1.5  macallan 			edata->state = ENVSYS_SVALID;
    192       1.1  macallan 	}
    193       1.1  macallan }
    194