Home | History | Annotate | Line # | Download | only in spi
tmp121.c revision 1.5.72.1
      1  1.5.72.1   thorpej /* $NetBSD: tmp121.c,v 1.5.72.1 2021/05/19 03:34:11 thorpej Exp $ */
      2       1.1   gdamore 
      3       1.1   gdamore /*-
      4       1.1   gdamore  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
      5       1.1   gdamore  * Copyright (c) 2006 Garrett D'Amore.
      6       1.1   gdamore  * All rights reserved.
      7       1.1   gdamore  *
      8       1.1   gdamore  * Portions of this code were written by Garrett D'Amore for the
      9       1.1   gdamore  * Champaign-Urbana Community Wireless Network Project.
     10       1.1   gdamore  *
     11       1.1   gdamore  * Redistribution and use in source and binary forms, with or
     12       1.1   gdamore  * without modification, are permitted provided that the following
     13       1.1   gdamore  * conditions are met:
     14       1.1   gdamore  * 1. Redistributions of source code must retain the above copyright
     15       1.1   gdamore  *    notice, this list of conditions and the following disclaimer.
     16       1.1   gdamore  * 2. Redistributions in binary form must reproduce the above
     17       1.1   gdamore  *    copyright notice, this list of conditions and the following
     18       1.1   gdamore  *    disclaimer in the documentation and/or other materials provided
     19       1.1   gdamore  *    with the distribution.
     20       1.1   gdamore  * 3. All advertising materials mentioning features or use of this
     21       1.1   gdamore  *    software must display the following acknowledgements:
     22       1.1   gdamore  *      This product includes software developed by the Urbana-Champaign
     23       1.1   gdamore  *      Independent Media Center.
     24       1.1   gdamore  *	This product includes software developed by Garrett D'Amore.
     25       1.1   gdamore  * 4. Urbana-Champaign Independent Media Center's name and Garrett
     26       1.1   gdamore  *    D'Amore's name may not be used to endorse or promote products
     27       1.1   gdamore  *    derived from this software without specific prior written permission.
     28       1.1   gdamore  *
     29       1.1   gdamore  * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
     30       1.1   gdamore  * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
     31       1.1   gdamore  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     32       1.1   gdamore  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     33       1.1   gdamore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
     34       1.1   gdamore  * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
     35       1.1   gdamore  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     36       1.1   gdamore  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     37       1.1   gdamore  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     38       1.1   gdamore  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39       1.1   gdamore  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     40       1.1   gdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     41       1.1   gdamore  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     42       1.1   gdamore  */
     43       1.1   gdamore 
     44       1.1   gdamore #include <sys/cdefs.h>
     45  1.5.72.1   thorpej __KERNEL_RCSID(0, "$NetBSD: tmp121.c,v 1.5.72.1 2021/05/19 03:34:11 thorpej Exp $");
     46       1.1   gdamore 
     47       1.1   gdamore #include <sys/param.h>
     48       1.1   gdamore #include <sys/systm.h>
     49       1.1   gdamore #include <sys/device.h>
     50       1.1   gdamore #include <sys/kernel.h>
     51       1.1   gdamore 
     52       1.1   gdamore #include <dev/sysmon/sysmonvar.h>
     53       1.1   gdamore 
     54       1.1   gdamore #include <dev/spi/spivar.h>
     55       1.1   gdamore 
     56       1.1   gdamore struct tmp121temp_softc {
     57       1.1   gdamore 	struct spi_handle *sc_sh;
     58       1.1   gdamore 
     59       1.3   xtraeme 	struct sysmon_envsys *sc_sme;
     60       1.3   xtraeme 	envsys_data_t sc_sensor;
     61       1.1   gdamore };
     62       1.1   gdamore 
     63       1.4   xtraeme static int	tmp121temp_match(device_t, cfdata_t, void *);
     64       1.4   xtraeme static void	tmp121temp_attach(device_t, device_t, void *);
     65       1.1   gdamore 
     66       1.4   xtraeme static void	tmp121temp_refresh(struct sysmon_envsys *, envsys_data_t *);
     67       1.1   gdamore 
     68       1.4   xtraeme CFATTACH_DECL_NEW(tmp121temp, sizeof(struct tmp121temp_softc),
     69       1.1   gdamore     tmp121temp_match, tmp121temp_attach, NULL, NULL);
     70       1.1   gdamore 
     71  1.5.72.1   thorpej static const struct device_compatible_entry compat_data[] = {
     72  1.5.72.1   thorpej 	{ .compat = "ti,tmp121" },
     73  1.5.72.1   thorpej 	{ .compat = "TMP00121" },
     74  1.5.72.1   thorpej 
     75  1.5.72.1   thorpej #if 0	/* We should also add support for these: */
     76  1.5.72.1   thorpej 	{ .compat = "ti,tmp122" },
     77  1.5.72.1   thorpej 
     78  1.5.72.1   thorpej 	{ .compat = "ti,lm70" },
     79  1.5.72.1   thorpej 	{ .compat = "LM000070" },
     80  1.5.72.1   thorpej 
     81  1.5.72.1   thorpej 	{ .compat = "ti,lm71" },
     82  1.5.72.1   thorpej 	{ .compat = "LM000071" },
     83  1.5.72.1   thorpej 
     84  1.5.72.1   thorpej 	{ .compat = "ti,lm74" },
     85  1.5.72.1   thorpej 	{ .compat = "LM000074" },
     86  1.5.72.1   thorpej #endif
     87  1.5.72.1   thorpej 	DEVICE_COMPAT_EOL
     88  1.5.72.1   thorpej };
     89  1.5.72.1   thorpej 
     90       1.1   gdamore static int
     91       1.4   xtraeme tmp121temp_match(device_t parent, cfdata_t cf, void *aux)
     92       1.1   gdamore {
     93       1.1   gdamore 	struct spi_attach_args *sa = aux;
     94  1.5.72.1   thorpej 	int rv;
     95       1.1   gdamore 
     96  1.5.72.1   thorpej 	rv = spi_compatible_match(sa, cf, compat_data);
     97  1.5.72.1   thorpej 	if (rv != 0) {
     98  1.5.72.1   thorpej 		/* configure for 10MHz */
     99  1.5.72.1   thorpej 		if (spi_configure(sa->sa_handle, SPI_MODE_0, 1000000))
    100  1.5.72.1   thorpej 			return 0;
    101  1.5.72.1   thorpej 	}
    102       1.1   gdamore 
    103  1.5.72.1   thorpej 	return rv;
    104       1.1   gdamore }
    105       1.1   gdamore 
    106       1.1   gdamore static void
    107       1.4   xtraeme tmp121temp_attach(device_t parent, device_t self, void *aux)
    108       1.1   gdamore {
    109       1.1   gdamore 	struct tmp121temp_softc *sc = device_private(self);
    110       1.1   gdamore 	struct spi_attach_args *sa = aux;
    111       1.1   gdamore 
    112       1.1   gdamore 	aprint_naive(": Temperature Sensor\n");
    113       1.1   gdamore 	aprint_normal(": TI TMP121 Temperature Sensor\n");
    114       1.1   gdamore 
    115       1.1   gdamore 	sc->sc_sh = sa->sa_handle;
    116       1.1   gdamore 
    117       1.3   xtraeme 	sc->sc_sme = sysmon_envsys_create();
    118       1.3   xtraeme 	sc->sc_sensor.units = ENVSYS_STEMP;
    119       1.5  pgoyette 	sc->sc_sensor.state = ENVSYS_SINVALID;
    120       1.3   xtraeme 	strlcpy(sc->sc_sensor.desc, device_xname(self),
    121       1.3   xtraeme 	    sizeof(sc->sc_sensor.desc));
    122       1.3   xtraeme 	if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) {
    123       1.3   xtraeme 		sysmon_envsys_destroy(sc->sc_sme);
    124       1.3   xtraeme 		return;
    125       1.3   xtraeme 	}
    126       1.1   gdamore 
    127       1.3   xtraeme 	sc->sc_sme->sme_name = device_xname(self);
    128       1.3   xtraeme 	sc->sc_sme->sme_refresh = tmp121temp_refresh;
    129       1.3   xtraeme 	sc->sc_sme->sme_cookie = sc;
    130       1.1   gdamore 
    131       1.3   xtraeme 	if (sysmon_envsys_register(sc->sc_sme)) {
    132       1.4   xtraeme 		aprint_error_dev(self, "unable to register with sysmon\n");
    133       1.3   xtraeme 		sysmon_envsys_destroy(sc->sc_sme);
    134       1.3   xtraeme 	}
    135       1.1   gdamore }
    136       1.1   gdamore 
    137       1.1   gdamore static void
    138       1.3   xtraeme tmp121temp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    139       1.1   gdamore {
    140       1.3   xtraeme 	struct tmp121temp_softc *sc = sme->sme_cookie;
    141       1.1   gdamore 	uint16_t		reg;
    142       1.1   gdamore 	int16_t			sreg;
    143       1.1   gdamore 	int			val;
    144       1.1   gdamore 
    145       1.1   gdamore 	if (spi_recv(sc->sc_sh, 2, (uint8_t *)&reg) != 0) {
    146       1.3   xtraeme 		edata->state = ENVSYS_SINVALID;
    147       1.1   gdamore 		return;
    148       1.1   gdamore 	}
    149       1.1   gdamore 
    150       1.1   gdamore 	sreg = (int16_t)be16toh(reg);
    151       1.1   gdamore 
    152       1.1   gdamore 	/*
    153       1.1   gdamore 	 * convert to uK:
    154       1.1   gdamore 	 *
    155       1.1   gdamore 	 * TMP121 bits:
    156       1.1   gdamore 	 * D15		: sign bit
    157       1.1   gdamore 	 * D14-D3	: data (D14 is MSB)
    158       1.1   gdamore 	 * D2-D0	: zero
    159       1.1   gdamore 	 *
    160       1.1   gdamore 	 * The data is represented in units of 0.0625 deg C.
    161       1.1   gdamore 	 */
    162       1.1   gdamore 	sreg >>= 3;
    163       1.1   gdamore 	val = sreg * 62500 + 273150000;
    164       1.1   gdamore 
    165       1.3   xtraeme 	edata->value_cur = val;
    166       1.3   xtraeme 	edata->state = ENVSYS_SVALID;
    167       1.1   gdamore }
    168