Home | History | Annotate | Line # | Download | only in i2c
titemp.c revision 1.6
      1  1.6   thorpej /* $NetBSD: titemp.c,v 1.6 2018/06/18 17:07:07 thorpej Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include <sys/cdefs.h>
     30  1.6   thorpej __KERNEL_RCSID(0, "$NetBSD: titemp.c,v 1.6 2018/06/18 17:07:07 thorpej Exp $");
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <sys/param.h>
     33  1.1  jmcneill #include <sys/systm.h>
     34  1.1  jmcneill #include <sys/device.h>
     35  1.1  jmcneill #include <sys/conf.h>
     36  1.1  jmcneill #include <sys/bus.h>
     37  1.1  jmcneill #include <sys/kmem.h>
     38  1.1  jmcneill 
     39  1.1  jmcneill #include <dev/i2c/i2cvar.h>
     40  1.1  jmcneill 
     41  1.1  jmcneill #include <dev/sysmon/sysmonvar.h>
     42  1.1  jmcneill 
     43  1.1  jmcneill #define TITEMP_LTEMP_HI_REG		0x00
     44  1.1  jmcneill #define TITEMP_RTEMP_HI_REG		0x01
     45  1.1  jmcneill #define TITEMP_STATUS_REG		0x02
     46  1.1  jmcneill #define TITEMP_CONFIG_REG		0x03
     47  1.1  jmcneill #define TITEMP_CONVRATE_REG		0x04
     48  1.1  jmcneill #define TITEMP_LTEMP_HLIMIT_HI_REG	0x05
     49  1.1  jmcneill #define TITEMP_LTEMP_LLIMIT_HI_REG	0x06
     50  1.1  jmcneill #define TITEMP_RTEMP_HLIMIT_HI_REG	0x07
     51  1.1  jmcneill #define TITEMP_RTEMP_LLIMIT_HI_REG	0x08
     52  1.1  jmcneill #define TITEMP_RTEMP_LO_REG		0x10
     53  1.1  jmcneill #define TITEMP_RTEMP_OFF_HI_REG		0x11
     54  1.1  jmcneill #define TITEMP_RTEMP_OFF_LO_REG		0x12
     55  1.1  jmcneill #define TITEMP_RTEMP_HLIMIT_LO_REG	0x13
     56  1.1  jmcneill #define TITEMP_RTEMP_LLIMIT_LO_REG	0x14
     57  1.1  jmcneill #define TITEMP_LTEMP_LO_REG		0x15
     58  1.1  jmcneill #define TITEMP_RTEMP_THERM_LIMIT_REG	0x19
     59  1.1  jmcneill #define TITEMP_LTEMP_THERM_LIMIT_REG	0x20
     60  1.1  jmcneill #define TITEMP_THERM_HYST_REG		0x21
     61  1.1  jmcneill #define TITEMP_CONAL_REG		0x22
     62  1.1  jmcneill #define TITEMP_NC_REG			0x23
     63  1.1  jmcneill #define TITEMP_DF_REG			0x24
     64  1.1  jmcneill #define TITEMP_MFID_REG			0xfe
     65  1.1  jmcneill 
     66  1.1  jmcneill #define TITEMP_MFID_TMP451		0x55
     67  1.1  jmcneill 
     68  1.1  jmcneill struct titemp_softc {
     69  1.1  jmcneill 	device_t	sc_dev;
     70  1.1  jmcneill 	i2c_tag_t	sc_i2c;
     71  1.1  jmcneill 	i2c_addr_t	sc_addr;
     72  1.1  jmcneill 
     73  1.1  jmcneill 	struct sysmon_envsys *sc_sme;
     74  1.1  jmcneill 	envsys_data_t	sc_sensor_ltemp;
     75  1.1  jmcneill 	envsys_data_t	sc_sensor_rtemp;
     76  1.1  jmcneill };
     77  1.1  jmcneill 
     78  1.1  jmcneill static int	titemp_match(device_t, cfdata_t, void *);
     79  1.1  jmcneill static void	titemp_attach(device_t, device_t, void *);
     80  1.1  jmcneill 
     81  1.1  jmcneill static void	titemp_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
     82  1.1  jmcneill static int	titemp_read(struct titemp_softc *, uint8_t, uint8_t *);
     83  1.1  jmcneill 
     84  1.1  jmcneill CFATTACH_DECL_NEW(titemp, sizeof(struct titemp_softc),
     85  1.1  jmcneill     titemp_match, titemp_attach, NULL, NULL);
     86  1.1  jmcneill 
     87  1.2  jmcneill static const char * titemp_compats[] = {
     88  1.2  jmcneill 	"ti,tmp451",
     89  1.2  jmcneill 	NULL
     90  1.2  jmcneill };
     91  1.2  jmcneill 
     92  1.6   thorpej static const struct device_compatible_entry titemp_compat_data[] = {
     93  1.6   thorpej 	DEVICE_COMPAT_ENTRY(titemp_compats),
     94  1.6   thorpej 	DEVICE_COMPAT_TERMINATOR
     95  1.6   thorpej };
     96  1.6   thorpej 
     97  1.1  jmcneill static int
     98  1.1  jmcneill titemp_match(device_t parent, cfdata_t match, void *aux)
     99  1.1  jmcneill {
    100  1.1  jmcneill 	struct i2c_attach_args *ia = aux;
    101  1.1  jmcneill 	uint8_t mfid;
    102  1.5   thorpej 	int error, match_result;
    103  1.1  jmcneill 
    104  1.6   thorpej 	if (iic_use_direct_match(ia, match, titemp_compat_data, &match_result))
    105  1.5   thorpej 		return match_result;
    106  1.5   thorpej 
    107  1.5   thorpej 	if (ia->ia_addr != 0x4c)
    108  1.5   thorpej 		return 0;
    109  1.5   thorpej 
    110  1.5   thorpej 	if (iic_acquire_bus(ia->ia_tag, I2C_F_POLL) != 0)
    111  1.5   thorpej 		return 0;
    112  1.5   thorpej 	error = iic_smbus_read_byte(ia->ia_tag, ia->ia_addr,
    113  1.5   thorpej 	    TITEMP_MFID_REG, &mfid, I2C_F_POLL);
    114  1.5   thorpej 	iic_release_bus(ia->ia_tag, I2C_F_POLL);
    115  1.1  jmcneill 
    116  1.5   thorpej 	if (error || mfid != TITEMP_MFID_TMP451)
    117  1.5   thorpej 		return 0;
    118  1.1  jmcneill 
    119  1.5   thorpej 	return I2C_MATCH_ADDRESS_AND_PROBE;
    120  1.1  jmcneill }
    121  1.1  jmcneill 
    122  1.1  jmcneill static void
    123  1.1  jmcneill titemp_attach(device_t parent, device_t self, void *aux)
    124  1.1  jmcneill {
    125  1.1  jmcneill 	struct titemp_softc *sc = device_private(self);
    126  1.1  jmcneill 	struct i2c_attach_args *ia = aux;
    127  1.1  jmcneill 
    128  1.1  jmcneill 	sc->sc_dev = self;
    129  1.1  jmcneill 	sc->sc_i2c = ia->ia_tag;
    130  1.1  jmcneill 	sc->sc_addr = ia->ia_addr;
    131  1.1  jmcneill 
    132  1.1  jmcneill 	aprint_naive("\n");
    133  1.1  jmcneill 	aprint_normal(": TMP451\n");
    134  1.1  jmcneill 
    135  1.1  jmcneill 	sc->sc_sme = sysmon_envsys_create();
    136  1.1  jmcneill 	sc->sc_sme->sme_name = device_xname(self);
    137  1.1  jmcneill 	sc->sc_sme->sme_cookie = sc;
    138  1.1  jmcneill 	sc->sc_sme->sme_refresh = titemp_sensors_refresh;
    139  1.1  jmcneill 
    140  1.1  jmcneill 	sc->sc_sensor_ltemp.units = ENVSYS_STEMP;
    141  1.1  jmcneill 	sc->sc_sensor_ltemp.state = ENVSYS_SINVALID;
    142  1.1  jmcneill 	snprintf(sc->sc_sensor_ltemp.desc, sizeof(sc->sc_sensor_ltemp.desc),
    143  1.1  jmcneill 	    "local temperature");
    144  1.1  jmcneill 	sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor_ltemp);
    145  1.1  jmcneill 
    146  1.1  jmcneill 	sc->sc_sensor_rtemp.units = ENVSYS_STEMP;
    147  1.1  jmcneill 	sc->sc_sensor_rtemp.state = ENVSYS_SINVALID;
    148  1.1  jmcneill 	snprintf(sc->sc_sensor_rtemp.desc, sizeof(sc->sc_sensor_rtemp.desc),
    149  1.1  jmcneill 	    "remote temperature");
    150  1.1  jmcneill 	sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor_rtemp);
    151  1.1  jmcneill 
    152  1.1  jmcneill 	sysmon_envsys_register(sc->sc_sme);
    153  1.1  jmcneill }
    154  1.1  jmcneill 
    155  1.1  jmcneill static void
    156  1.1  jmcneill titemp_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    157  1.1  jmcneill {
    158  1.1  jmcneill 	struct titemp_softc *sc = sme->sme_cookie;
    159  1.1  jmcneill 	uint8_t reg_hi, reg_lo, temp[2];
    160  1.1  jmcneill 	int error;
    161  1.1  jmcneill 
    162  1.1  jmcneill 	if (edata == &sc->sc_sensor_ltemp) {
    163  1.1  jmcneill 		reg_hi = TITEMP_LTEMP_HI_REG;
    164  1.1  jmcneill 		reg_lo = TITEMP_LTEMP_LO_REG;
    165  1.1  jmcneill 	} else if (edata == &sc->sc_sensor_rtemp) {
    166  1.1  jmcneill 		reg_hi = TITEMP_RTEMP_HI_REG;
    167  1.1  jmcneill 		reg_lo = TITEMP_RTEMP_LO_REG;
    168  1.1  jmcneill 	} else {
    169  1.1  jmcneill 		edata->state = ENVSYS_SINVALID;
    170  1.1  jmcneill 		return;
    171  1.1  jmcneill 	}
    172  1.1  jmcneill 
    173  1.1  jmcneill 	iic_acquire_bus(sc->sc_i2c, 0);
    174  1.1  jmcneill 	if ((error = titemp_read(sc, reg_hi, &temp[0])) != 0)
    175  1.1  jmcneill 		goto done;
    176  1.1  jmcneill 	if ((error = titemp_read(sc, reg_lo, &temp[1])) != 0)
    177  1.1  jmcneill 		goto done;
    178  1.1  jmcneill done:
    179  1.1  jmcneill 	iic_release_bus(sc->sc_i2c, 0);
    180  1.1  jmcneill 
    181  1.1  jmcneill 	if (error) {
    182  1.1  jmcneill 		edata->state = ENVSYS_SINVALID;
    183  1.1  jmcneill 	} else {
    184  1.1  jmcneill 		edata->value_cur =
    185  1.1  jmcneill 		    ((uint64_t)temp[0] * 1000000) +
    186  1.3  jakllsch 		    ((uint64_t)(temp[1]>>4) * 62500) +
    187  1.1  jmcneill 		    + 273150000;
    188  1.1  jmcneill 		edata->state = ENVSYS_SVALID;
    189  1.1  jmcneill 	}
    190  1.1  jmcneill }
    191  1.1  jmcneill 
    192  1.1  jmcneill static int
    193  1.1  jmcneill titemp_read(struct titemp_softc *sc, uint8_t reg, uint8_t *val)
    194  1.1  jmcneill {
    195  1.1  jmcneill 	return iic_smbus_read_byte(sc->sc_i2c, sc->sc_addr, reg, val, 0);
    196  1.1  jmcneill }
    197