xref: /openbsd/sys/dev/i2c/tmp451.c (revision 471aeecf)
1*471aeecfSnaddy /*	$OpenBSD: tmp451.c,v 1.2 2022/04/06 18:59:28 naddy Exp $	*/
2c0e91744Skettenis /*
3c0e91744Skettenis  * Copyright (c) 2018 Mark Kettenis <kettenis@openbsd.org>
4c0e91744Skettenis  *
5c0e91744Skettenis  * Permission to use, copy, modify, and distribute this software for any
6c0e91744Skettenis  * purpose with or without fee is hereby granted, provided that the above
7c0e91744Skettenis  * copyright notice and this permission notice appear in all copies.
8c0e91744Skettenis  *
9c0e91744Skettenis  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10c0e91744Skettenis  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11c0e91744Skettenis  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12c0e91744Skettenis  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13c0e91744Skettenis  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14c0e91744Skettenis  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15c0e91744Skettenis  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16c0e91744Skettenis  */
17c0e91744Skettenis 
18c0e91744Skettenis #include <sys/param.h>
19c0e91744Skettenis #include <sys/systm.h>
20c0e91744Skettenis #include <sys/device.h>
21c0e91744Skettenis #include <sys/sensors.h>
22c0e91744Skettenis 
23c0e91744Skettenis #include <dev/i2c/i2cvar.h>
24c0e91744Skettenis 
25c0e91744Skettenis /* TI TMP451 registers */
26c0e91744Skettenis #define TMP451_LT_HI		0x00
27c0e91744Skettenis #define TMP451_RT_HI		0x01
28c0e91744Skettenis #define TMP451_RT_LO		0x10
29c0e91744Skettenis #define TMP451_RTOS_HI		0x11
30c0e91744Skettenis #define TMP451_RTOS_LO		0x12
31c0e91744Skettenis #define TMP451_LT_LO		0x15
32c0e91744Skettenis 
33c0e91744Skettenis /* Sensors */
34c0e91744Skettenis #define TITMP_LT		0
35c0e91744Skettenis #define TITMP_RT		1
36c0e91744Skettenis #define TITMP_NUM_SENSORS	2
37c0e91744Skettenis 
38c0e91744Skettenis struct titmp_softc {
39c0e91744Skettenis 	struct device sc_dev;
40c0e91744Skettenis 	i2c_tag_t sc_tag;
41c0e91744Skettenis 	i2c_addr_t sc_addr;
42c0e91744Skettenis 
43c0e91744Skettenis 	uint64_t sc_offset[TITMP_NUM_SENSORS];
44c0e91744Skettenis 	struct ksensor sc_sensor[TITMP_NUM_SENSORS];
45c0e91744Skettenis 	struct ksensordev sc_sensordev;
46c0e91744Skettenis };
47c0e91744Skettenis 
48c0e91744Skettenis int	titmp_match(struct device *, void *, void *);
49c0e91744Skettenis void	titmp_attach(struct device *, struct device *, void *);
50c0e91744Skettenis 
51*471aeecfSnaddy const struct cfattach titmp_ca = {
52c0e91744Skettenis 	sizeof(struct titmp_softc), titmp_match, titmp_attach
53c0e91744Skettenis };
54c0e91744Skettenis 
55c0e91744Skettenis struct cfdriver titmp_cd = {
56c0e91744Skettenis 	NULL, "titmp", DV_DULL
57c0e91744Skettenis };
58c0e91744Skettenis 
59c0e91744Skettenis void	titmp_read_offsets(struct titmp_softc *);
60c0e91744Skettenis void	titmp_refresh_sensors(void *);
61c0e91744Skettenis 
62c0e91744Skettenis int
titmp_match(struct device * parent,void * match,void * aux)63c0e91744Skettenis titmp_match(struct device *parent, void *match, void *aux)
64c0e91744Skettenis {
65c0e91744Skettenis 	struct i2c_attach_args *ia = aux;
66c0e91744Skettenis 
67c0e91744Skettenis 	return (strcmp(ia->ia_name, "ti,tmp451") == 0);
68c0e91744Skettenis }
69c0e91744Skettenis 
70c0e91744Skettenis void
titmp_attach(struct device * parent,struct device * self,void * aux)71c0e91744Skettenis titmp_attach(struct device *parent, struct device *self, void *aux)
72c0e91744Skettenis {
73c0e91744Skettenis 	struct titmp_softc *sc = (struct titmp_softc *)self;
74c0e91744Skettenis 	struct i2c_attach_args *ia = aux;
75c0e91744Skettenis 	int i;
76c0e91744Skettenis 
77c0e91744Skettenis 	sc->sc_tag = ia->ia_tag;
78c0e91744Skettenis 	sc->sc_addr = ia->ia_addr;
79c0e91744Skettenis 
80c0e91744Skettenis 	printf("\n");
81c0e91744Skettenis 
82c0e91744Skettenis 	titmp_read_offsets(sc);
83c0e91744Skettenis 
84c0e91744Skettenis 	/* Register sensors. */
85c0e91744Skettenis 	strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
86c0e91744Skettenis 	    sizeof(sc->sc_sensordev.xname));
87c0e91744Skettenis 	sc->sc_sensor[TITMP_LT].type = SENSOR_TEMP;
88c0e91744Skettenis 	strlcpy(sc->sc_sensor[TITMP_LT].desc, "Local",
89c0e91744Skettenis 	    sizeof(sc->sc_sensor[TITMP_LT].desc));
90c0e91744Skettenis 	sc->sc_sensor[TITMP_RT].type = SENSOR_TEMP;
91c0e91744Skettenis 	strlcpy(sc->sc_sensor[TITMP_RT].desc, "Remote",
92c0e91744Skettenis 	    sizeof(sc->sc_sensor[TITMP_RT].desc));
93c0e91744Skettenis 	for (i = 0; i < TITMP_NUM_SENSORS; i++)
94c0e91744Skettenis 		sensor_attach(&sc->sc_sensordev, &sc->sc_sensor[i]);
95c0e91744Skettenis 	sensordev_install(&sc->sc_sensordev);
96c0e91744Skettenis 	sensor_task_register(sc, titmp_refresh_sensors, 5);
97c0e91744Skettenis }
98c0e91744Skettenis 
99c0e91744Skettenis void
titmp_read_offsets(struct titmp_softc * sc)100c0e91744Skettenis titmp_read_offsets(struct titmp_softc *sc)
101c0e91744Skettenis {
102c0e91744Skettenis 	uint8_t cmd, hi, lo;
103c0e91744Skettenis 	int error;
104c0e91744Skettenis 
105c0e91744Skettenis 	iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
106c0e91744Skettenis 
107c0e91744Skettenis 	cmd = TMP451_RTOS_HI;
108c0e91744Skettenis 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
109c0e91744Skettenis 	    &cmd, sizeof(cmd), &hi, sizeof(hi), I2C_F_POLL);
110c0e91744Skettenis 	if (error)
111c0e91744Skettenis 		goto fail;
112c0e91744Skettenis 	cmd = TMP451_RTOS_LO;
113c0e91744Skettenis 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
114c0e91744Skettenis 	    &cmd, sizeof(cmd), &lo, sizeof(lo), I2C_F_POLL);
115c0e91744Skettenis 	if (error)
116c0e91744Skettenis 		goto fail;
117c0e91744Skettenis 
118c0e91744Skettenis 	sc->sc_offset[TITMP_RT] = 1000000 * hi + (lo >> 4) * 1000000 / 16;
119c0e91744Skettenis 
120c0e91744Skettenis fail:
121c0e91744Skettenis 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
122c0e91744Skettenis }
123c0e91744Skettenis 
124c0e91744Skettenis void
titmp_read_sensor(struct titmp_softc * sc,uint8_t cmdhi,uint8_t cmdlo,int index)125c0e91744Skettenis titmp_read_sensor(struct titmp_softc *sc, uint8_t cmdhi, uint8_t cmdlo,
126c0e91744Skettenis     int index)
127c0e91744Skettenis {
128c0e91744Skettenis 	uint8_t hi, lo;
129c0e91744Skettenis 
130c0e91744Skettenis 	if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
131c0e91744Skettenis 	    sc->sc_addr, &cmdhi, sizeof(cmdhi), &hi, sizeof(hi), 0))
132c0e91744Skettenis 		goto invalid;
133c0e91744Skettenis 	if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
134c0e91744Skettenis 	    sc->sc_addr, &cmdlo, sizeof(cmdlo), &lo, sizeof(lo), 0))
135c0e91744Skettenis 		goto invalid;
136c0e91744Skettenis 
137c0e91744Skettenis 	sc->sc_sensor[index].value = 273150000 +
138c0e91744Skettenis 	    1000000 * hi + (lo >> 4) * 1000000 / 16;
139c0e91744Skettenis 	sc->sc_sensor[index].value += sc->sc_offset[index];
140c0e91744Skettenis 	return;
141c0e91744Skettenis 
142c0e91744Skettenis invalid:
143c0e91744Skettenis 	sc->sc_sensor[index].flags |= SENSOR_FINVALID;
144c0e91744Skettenis }
145c0e91744Skettenis 
146c0e91744Skettenis void
titmp_refresh_sensors(void * arg)147c0e91744Skettenis titmp_refresh_sensors(void *arg)
148c0e91744Skettenis {
149c0e91744Skettenis 	struct titmp_softc *sc = arg;
150c0e91744Skettenis 
151c0e91744Skettenis 	iic_acquire_bus(sc->sc_tag, 0);
152c0e91744Skettenis 
153c0e91744Skettenis 	titmp_read_sensor(sc, TMP451_LT_HI, TMP451_LT_LO, TITMP_LT);
154c0e91744Skettenis 	titmp_read_sensor(sc, TMP451_RT_HI, TMP451_RT_LO, TITMP_RT);
155c0e91744Skettenis 
156c0e91744Skettenis 	iic_release_bus(sc->sc_tag, 0);
157c0e91744Skettenis }
158