xref: /netbsd-src/sys/dev/spi/tmp121.c (revision f64c887e97cc4e0fca7184a6ff7742154fef673e)
1*f64c887eSthorpej /* $NetBSD: tmp121.c,v 1.7 2022/01/19 05:21:44 thorpej Exp $ */
25c050c46Sgdamore 
35c050c46Sgdamore /*-
45c050c46Sgdamore  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
55c050c46Sgdamore  * Copyright (c) 2006 Garrett D'Amore.
65c050c46Sgdamore  * All rights reserved.
75c050c46Sgdamore  *
85c050c46Sgdamore  * Portions of this code were written by Garrett D'Amore for the
95c050c46Sgdamore  * Champaign-Urbana Community Wireless Network Project.
105c050c46Sgdamore  *
115c050c46Sgdamore  * Redistribution and use in source and binary forms, with or
125c050c46Sgdamore  * without modification, are permitted provided that the following
135c050c46Sgdamore  * conditions are met:
145c050c46Sgdamore  * 1. Redistributions of source code must retain the above copyright
155c050c46Sgdamore  *    notice, this list of conditions and the following disclaimer.
165c050c46Sgdamore  * 2. Redistributions in binary form must reproduce the above
175c050c46Sgdamore  *    copyright notice, this list of conditions and the following
185c050c46Sgdamore  *    disclaimer in the documentation and/or other materials provided
195c050c46Sgdamore  *    with the distribution.
205c050c46Sgdamore  * 3. All advertising materials mentioning features or use of this
215c050c46Sgdamore  *    software must display the following acknowledgements:
225c050c46Sgdamore  *      This product includes software developed by the Urbana-Champaign
235c050c46Sgdamore  *      Independent Media Center.
245c050c46Sgdamore  *	This product includes software developed by Garrett D'Amore.
255c050c46Sgdamore  * 4. Urbana-Champaign Independent Media Center's name and Garrett
265c050c46Sgdamore  *    D'Amore's name may not be used to endorse or promote products
275c050c46Sgdamore  *    derived from this software without specific prior written permission.
285c050c46Sgdamore  *
295c050c46Sgdamore  * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
305c050c46Sgdamore  * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
315c050c46Sgdamore  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
325c050c46Sgdamore  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
335c050c46Sgdamore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
345c050c46Sgdamore  * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
355c050c46Sgdamore  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
365c050c46Sgdamore  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
375c050c46Sgdamore  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
385c050c46Sgdamore  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
395c050c46Sgdamore  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
405c050c46Sgdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
415c050c46Sgdamore  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
425c050c46Sgdamore  */
435c050c46Sgdamore 
445c050c46Sgdamore #include <sys/cdefs.h>
45*f64c887eSthorpej __KERNEL_RCSID(0, "$NetBSD: tmp121.c,v 1.7 2022/01/19 05:21:44 thorpej Exp $");
465c050c46Sgdamore 
475c050c46Sgdamore #include <sys/param.h>
485c050c46Sgdamore #include <sys/systm.h>
495c050c46Sgdamore #include <sys/device.h>
505c050c46Sgdamore #include <sys/kernel.h>
515c050c46Sgdamore 
525c050c46Sgdamore #include <dev/sysmon/sysmonvar.h>
535c050c46Sgdamore 
545c050c46Sgdamore #include <dev/spi/spivar.h>
555c050c46Sgdamore 
565c050c46Sgdamore struct tmp121temp_softc {
575c050c46Sgdamore 	struct spi_handle *sc_sh;
585c050c46Sgdamore 
5931962fc6Sxtraeme 	struct sysmon_envsys *sc_sme;
6031962fc6Sxtraeme 	envsys_data_t sc_sensor;
615c050c46Sgdamore };
625c050c46Sgdamore 
6363e2f604Sxtraeme static int	tmp121temp_match(device_t, cfdata_t, void *);
6463e2f604Sxtraeme static void	tmp121temp_attach(device_t, device_t, void *);
655c050c46Sgdamore 
6631962fc6Sxtraeme static void	tmp121temp_refresh(struct sysmon_envsys *, envsys_data_t *);
675c050c46Sgdamore 
6863e2f604Sxtraeme CFATTACH_DECL_NEW(tmp121temp, sizeof(struct tmp121temp_softc),
695c050c46Sgdamore     tmp121temp_match, tmp121temp_attach, NULL, NULL);
705c050c46Sgdamore 
715c050c46Sgdamore static int
tmp121temp_match(device_t parent,cfdata_t cf,void * aux)7263e2f604Sxtraeme tmp121temp_match(device_t parent, cfdata_t cf, void *aux)
735c050c46Sgdamore {
745c050c46Sgdamore 	return 1;
755c050c46Sgdamore }
765c050c46Sgdamore 
775c050c46Sgdamore static void
tmp121temp_attach(device_t parent,device_t self,void * aux)7863e2f604Sxtraeme tmp121temp_attach(device_t parent, device_t self, void *aux)
795c050c46Sgdamore {
805c050c46Sgdamore 	struct tmp121temp_softc *sc = device_private(self);
815c050c46Sgdamore 	struct spi_attach_args *sa = aux;
82c8694285Sthorpej 	int error;
835c050c46Sgdamore 
845c050c46Sgdamore 	aprint_naive(": Temperature Sensor\n");
855c050c46Sgdamore 	aprint_normal(": TI TMP121 Temperature Sensor\n");
865c050c46Sgdamore 
87c8694285Sthorpej 	/* configure for 10MHz */
88*f64c887eSthorpej 	error = spi_configure(self, sa->sa_handle, SPI_MODE_0, 1000000);
89c8694285Sthorpej 	if (error) {
90c8694285Sthorpej 		return;
91c8694285Sthorpej 	}
92c8694285Sthorpej 
935c050c46Sgdamore 	sc->sc_sh = sa->sa_handle;
945c050c46Sgdamore 
9531962fc6Sxtraeme 	sc->sc_sme = sysmon_envsys_create();
9631962fc6Sxtraeme 	sc->sc_sensor.units = ENVSYS_STEMP;
97564fdf67Spgoyette 	sc->sc_sensor.state = ENVSYS_SINVALID;
9831962fc6Sxtraeme 	strlcpy(sc->sc_sensor.desc, device_xname(self),
9931962fc6Sxtraeme 	    sizeof(sc->sc_sensor.desc));
10031962fc6Sxtraeme 	if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) {
10131962fc6Sxtraeme 		sysmon_envsys_destroy(sc->sc_sme);
10231962fc6Sxtraeme 		return;
10331962fc6Sxtraeme 	}
1045c050c46Sgdamore 
10531962fc6Sxtraeme 	sc->sc_sme->sme_name = device_xname(self);
10631962fc6Sxtraeme 	sc->sc_sme->sme_refresh = tmp121temp_refresh;
10731962fc6Sxtraeme 	sc->sc_sme->sme_cookie = sc;
1085c050c46Sgdamore 
10931962fc6Sxtraeme 	if (sysmon_envsys_register(sc->sc_sme)) {
11063e2f604Sxtraeme 		aprint_error_dev(self, "unable to register with sysmon\n");
11131962fc6Sxtraeme 		sysmon_envsys_destroy(sc->sc_sme);
11231962fc6Sxtraeme 	}
1135c050c46Sgdamore }
1145c050c46Sgdamore 
1155c050c46Sgdamore static void
tmp121temp_refresh(struct sysmon_envsys * sme,envsys_data_t * edata)11631962fc6Sxtraeme tmp121temp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
1175c050c46Sgdamore {
11831962fc6Sxtraeme 	struct tmp121temp_softc *sc = sme->sme_cookie;
1195c050c46Sgdamore 	uint16_t		reg;
1205c050c46Sgdamore 	int16_t			sreg;
1215c050c46Sgdamore 	int			val;
1225c050c46Sgdamore 
1235c050c46Sgdamore 	if (spi_recv(sc->sc_sh, 2, (uint8_t *)&reg) != 0) {
12431962fc6Sxtraeme 		edata->state = ENVSYS_SINVALID;
1255c050c46Sgdamore 		return;
1265c050c46Sgdamore 	}
1275c050c46Sgdamore 
1285c050c46Sgdamore 	sreg = (int16_t)be16toh(reg);
1295c050c46Sgdamore 
1305c050c46Sgdamore 	/*
1315c050c46Sgdamore 	 * convert to uK:
1325c050c46Sgdamore 	 *
1335c050c46Sgdamore 	 * TMP121 bits:
1345c050c46Sgdamore 	 * D15		: sign bit
1355c050c46Sgdamore 	 * D14-D3	: data (D14 is MSB)
1365c050c46Sgdamore 	 * D2-D0	: zero
1375c050c46Sgdamore 	 *
1385c050c46Sgdamore 	 * The data is represented in units of 0.0625 deg C.
1395c050c46Sgdamore 	 */
1405c050c46Sgdamore 	sreg >>= 3;
1415c050c46Sgdamore 	val = sreg * 62500 + 273150000;
1425c050c46Sgdamore 
14331962fc6Sxtraeme 	edata->value_cur = val;
14431962fc6Sxtraeme 	edata->state = ENVSYS_SVALID;
1455c050c46Sgdamore }
146