owtemp.c revision 1.20 1 /* $NetBSD: owtemp.c,v 1.20 2023/08/27 13:20:09 kardel Exp $ */
2 /* $OpenBSD: owtemp.c,v 1.1 2006/03/04 16:27:03 grange Exp $ */
3
4 /*-
5 * Copyright (c) 2019 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Andrew Doran.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * 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 copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 2006 Alexander Yurchenko <grange (at) openbsd.org>
35 *
36 * Permission to use, copy, modify, and distribute this software for any
37 * purpose with or without fee is hereby granted, provided that the above
38 * copyright notice and this permission notice appear in all copies.
39 *
40 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
41 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
42 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
43 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
44 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
45 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
46 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
47 */
48
49 /*
50 * 1-Wire temperature family type device driver.
51 */
52
53 #include <sys/cdefs.h>
54 __KERNEL_RCSID(0, "$NetBSD: owtemp.c,v 1.20 2023/08/27 13:20:09 kardel Exp $");
55
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/device.h>
59 #include <sys/kernel.h>
60 #include <sys/proc.h>
61 #include <sys/module.h>
62
63 #include <dev/sysmon/sysmonvar.h>
64
65 #include <dev/onewire/onewiredevs.h>
66 #include <dev/onewire/onewirereg.h>
67 #include <dev/onewire/onewirevar.h>
68
69 #define DS_CMD_CONVERT 0x44
70 #define DS_CMD_READ_SCRATCHPAD 0xbe
71
72 struct owtemp_softc {
73 device_t sc_dv;
74 void *sc_onewire;
75 u_int64_t sc_rom;
76 const char *sc_chipname;
77
78 envsys_data_t sc_sensor;
79 struct sysmon_envsys *sc_sme;
80
81 uint32_t (*sc_owtemp_decode)(const uint8_t *);
82
83 int sc_dying;
84
85 struct evcnt sc_ev_update;
86 struct evcnt sc_ev_rsterr;
87 struct evcnt sc_ev_crcerr;
88 };
89
90 static int owtemp_match(device_t, cfdata_t, void *);
91 static void owtemp_attach(device_t, device_t, void *);
92 static int owtemp_detach(device_t, int);
93 static int owtemp_activate(device_t, enum devact);
94 static bool owtemp_update(struct owtemp_softc *sc, uint32_t *temp);
95 static void owtemp_refresh(struct sysmon_envsys *, envsys_data_t *);
96 static uint32_t owtemp_decode_ds18b20(const uint8_t *);
97 static uint32_t owtemp_decode_ds1920(const uint8_t *);
98
99 CFATTACH_DECL_NEW(owtemp, sizeof(struct owtemp_softc),
100 owtemp_match, owtemp_attach, owtemp_detach, owtemp_activate);
101
102 extern struct cfdriver owtemp_cd;
103
104 static const struct onewire_matchfam owtemp_fams[] = {
105 { ONEWIRE_FAMILY_DS1920 }, /* also DS1820 */
106 { ONEWIRE_FAMILY_DS18B20 },
107 { ONEWIRE_FAMILY_DS1822 },
108 };
109
110 int owtemp_retries = 3;
111
112 static int
113 owtemp_match(device_t parent, cfdata_t match, void *aux)
114 {
115 return (onewire_matchbyfam(aux, owtemp_fams,
116 __arraycount(owtemp_fams)));
117 }
118
119 static void
120 owtemp_attach(device_t parent, device_t self, void *aux)
121 {
122 struct owtemp_softc *sc = device_private(self);
123 struct onewire_attach_args *oa = aux;
124
125 aprint_naive("\n");
126
127 sc->sc_dv = self;
128 sc->sc_onewire = oa->oa_onewire;
129 sc->sc_rom = oa->oa_rom;
130
131 switch(ONEWIRE_ROM_FAMILY_TYPE(sc->sc_rom)) {
132 case ONEWIRE_FAMILY_DS18B20:
133 sc->sc_chipname = "DS18B20";
134 sc->sc_owtemp_decode = owtemp_decode_ds18b20;
135 break;
136 case ONEWIRE_FAMILY_DS1822:
137 sc->sc_chipname = "DS1822";
138 sc->sc_owtemp_decode = owtemp_decode_ds18b20;
139 break;
140 case ONEWIRE_FAMILY_DS1920:
141 sc->sc_chipname = "DS1920";
142 sc->sc_owtemp_decode = owtemp_decode_ds1920;
143 break;
144 }
145
146 evcnt_attach_dynamic(&sc->sc_ev_update, EVCNT_TYPE_MISC, NULL,
147 device_xname(self), "update");
148 evcnt_attach_dynamic(&sc->sc_ev_rsterr, EVCNT_TYPE_MISC, NULL,
149 device_xname(self), "reset error");
150 evcnt_attach_dynamic(&sc->sc_ev_crcerr, EVCNT_TYPE_MISC, NULL,
151 device_xname(self), "crc error");
152
153 sc->sc_sme = sysmon_envsys_create();
154
155 /* Initialize sensor */
156 sc->sc_sensor.units = ENVSYS_STEMP;
157 sc->sc_sensor.state = ENVSYS_SINVALID;
158 (void)strlcpy(sc->sc_sensor.desc,
159 device_xname(self), sizeof(sc->sc_sensor.desc));
160 (void)snprintf(sc->sc_sensor.desc, sizeof(sc->sc_sensor.desc),
161 "%s S/N %012" PRIx64, sc->sc_chipname, ONEWIRE_ROM_SN(sc->sc_rom));
162 if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) {
163 sysmon_envsys_destroy(sc->sc_sme);
164 return;
165 }
166
167 /* Hook into system monitor. */
168 sc->sc_sme->sme_name = device_xname(self);
169 sc->sc_sme->sme_cookie = sc;
170 sc->sc_sme->sme_refresh = owtemp_refresh;
171
172 if (sysmon_envsys_register(sc->sc_sme)) {
173 aprint_error_dev(self, "unable to register with sysmon\n");
174 sysmon_envsys_destroy(sc->sc_sme);
175 return;
176 }
177
178 aprint_normal("\n");
179 }
180
181 static int
182 owtemp_detach(device_t self, int flags)
183 {
184 struct owtemp_softc *sc = device_private(self);
185
186 sysmon_envsys_unregister(sc->sc_sme);
187 evcnt_detach(&sc->sc_ev_rsterr);
188 evcnt_detach(&sc->sc_ev_update);
189 evcnt_detach(&sc->sc_ev_crcerr);
190
191 return 0;
192 }
193
194 static int
195 owtemp_activate(device_t self, enum devact act)
196 {
197 struct owtemp_softc *sc = device_private(self);
198
199 switch (act) {
200 case DVACT_DEACTIVATE:
201 sc->sc_dying = 1;
202 return 0;
203 default:
204 return EOPNOTSUPP;
205 }
206 }
207
208 static bool
209 owtemp_update(struct owtemp_softc *sc, uint32_t *temp)
210 {
211 u_int8_t data[9];
212
213 sc->sc_ev_update.ev_count++;
214
215 /*
216 * Start temperature conversion. The conversion takes up to 750ms.
217 * After sending the command, the data line must be held high for
218 * at least 750ms to provide power during the conversion process.
219 * As such, no other activity may take place on the 1-Wire bus for
220 * at least this period. Keep the parent bus locked while waiting.
221 */
222 if (onewire_reset(sc->sc_onewire) != 0) {
223 sc->sc_ev_rsterr.ev_count++;
224 return false;
225 }
226 onewire_matchrom(sc->sc_onewire, sc->sc_rom);
227 onewire_write_byte(sc->sc_onewire, DS_CMD_CONVERT);
228 (void)kpause("owtemp", false, mstohz(750 + 10), NULL);
229
230 /*
231 * The result of the temperature measurement is placed in the
232 * first two bytes of the scratchpad. Perform the caculation
233 * only if the CRC is correct.
234 */
235 if (onewire_reset(sc->sc_onewire) != 0) {
236 sc->sc_ev_rsterr.ev_count++;
237 return false;
238 }
239 onewire_matchrom(sc->sc_onewire, sc->sc_rom);
240 onewire_write_byte(sc->sc_onewire, DS_CMD_READ_SCRATCHPAD);
241 onewire_read_block(sc->sc_onewire, data, 9);
242 if (onewire_crc(data, 8) != data[8]) {
243 sc->sc_ev_crcerr.ev_count++;
244 return false;
245 }
246 *temp = sc->sc_owtemp_decode(data);
247 return true;
248 }
249
250 static void
251 owtemp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
252 {
253 struct owtemp_softc *sc = sme->sme_cookie;
254 uint32_t reading;
255 int retry;
256
257 onewire_lock(sc->sc_onewire);
258 for (retry = 0; retry < owtemp_retries; retry++) {
259 if (owtemp_update(sc, &reading)) {
260 onewire_unlock(sc->sc_onewire);
261 sc->sc_sensor.value_cur = reading;
262 sc->sc_sensor.state = ENVSYS_SVALID;
263 return;
264 }
265 }
266 onewire_unlock(sc->sc_onewire);
267 aprint_error_dev(sc->sc_dv,
268 "update failed - use vmstat(8) to check event counters\n");
269 sc->sc_sensor.state = ENVSYS_SINVALID;
270 }
271
272 static uint32_t
273 owtemp_decode_ds18b20(const uint8_t *buf)
274 {
275 int temp;
276
277 /*
278 * Sign-extend the MSB byte, and add in the fractions of a
279 * degree contained in the LSB (precision 1/16th DegC).
280 */
281 temp = (int8_t)buf[1];
282 temp = (temp << 8) | buf[0];
283
284 /*
285 * Conversion to uK is simple.
286 */
287 return (temp * 62500 + 273150000);
288 }
289
290 static uint32_t
291 owtemp_decode_ds1920(const uint8_t *buf)
292 {
293 int temp;
294
295 /*
296 * Sign-extend the MSB byte, and add in the fractions of a
297 * degree contained in the LSB (precision 1/2 DegC).
298 */
299 temp = (int8_t)buf[1];
300 temp = (temp << 8) | buf[0];
301
302 if (buf[7] != 0) {
303 /*
304 * interpolate for higher precision using the count registers
305 *
306 * buf[7]: COUNT_PER_C(elsius)
307 * buf[6]: COUNT_REMAIN
308 *
309 * T = TEMP - 0.25 + (COUNT_PER_C - COUNT_REMAIN) / COUNT_PER_C
310 */
311 temp &= ~1;
312 temp += 500000 * temp + (500000 * (buf[7] - buf[6])) / buf[7] - 250000;
313 } else {
314 temp *= 500000;
315 }
316
317 /* convert to uK */
318 return (temp + 273150000);
319 }
320
321 MODULE(MODULE_CLASS_DRIVER, owtemp, NULL);
322
323 #ifdef _MODULE
324 #include "ioconf.c"
325 #endif
326
327 static int
328 owtemp_modcmd(modcmd_t cmd, void *opaque)
329 {
330 int error;
331
332 error = 0;
333 switch (cmd) {
334 case MODULE_CMD_INIT:
335 #ifdef _MODULE
336 error = config_init_component(cfdriver_ioconf_owtemp,
337 cfattach_ioconf_owtemp, cfdata_ioconf_owtemp);
338 if (error)
339 aprint_error("%s: unable to init component\n",
340 owtemp_cd.cd_name);
341 #endif
342 break;
343 case MODULE_CMD_FINI:
344 #ifdef _MODULE
345 config_fini_component(cfdriver_ioconf_owtemp,
346 cfattach_ioconf_owtemp, cfdata_ioconf_owtemp);
347 #endif
348 break;
349 default:
350 error = ENOTTY;
351 }
352 return error;
353 }
354