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