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