owtemp.c revision 1.19 1 1.18 martin /* $NetBSD: owtemp.c,v 1.19 2019/11/30 23:06:52 ad 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.19 ad __KERNEL_RCSID(0, "$NetBSD: owtemp.c,v 1.19 2019/11/30 23:06:52 ad 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.1 riz
62 1.1 riz #include <dev/sysmon/sysmonvar.h>
63 1.1 riz
64 1.1 riz #include <dev/onewire/onewiredevs.h>
65 1.1 riz #include <dev/onewire/onewirereg.h>
66 1.1 riz #include <dev/onewire/onewirevar.h>
67 1.1 riz
68 1.1 riz #define DS_CMD_CONVERT 0x44
69 1.1 riz #define DS_CMD_READ_SCRATCHPAD 0xbe
70 1.1 riz
71 1.1 riz struct owtemp_softc {
72 1.18 martin device_t sc_dv;
73 1.18 martin void *sc_onewire;
74 1.1 riz u_int64_t sc_rom;
75 1.18 martin const char *sc_chipname;
76 1.1 riz
77 1.12 xtraeme envsys_data_t sc_sensor;
78 1.12 xtraeme struct sysmon_envsys *sc_sme;
79 1.1 riz
80 1.1 riz uint32_t (*sc_owtemp_decode)(const uint8_t *);
81 1.1 riz
82 1.1 riz int sc_dying;
83 1.19 ad
84 1.19 ad struct evcnt sc_ev_update;
85 1.19 ad struct evcnt sc_ev_rsterr;
86 1.19 ad struct evcnt sc_ev_crcerr;
87 1.1 riz };
88 1.1 riz
89 1.14 xtraeme static int owtemp_match(device_t, cfdata_t, void *);
90 1.14 xtraeme static void owtemp_attach(device_t, device_t, void *);
91 1.14 xtraeme static int owtemp_detach(device_t, int);
92 1.14 xtraeme static int owtemp_activate(device_t, enum devact);
93 1.19 ad static bool owtemp_update(struct owtemp_softc *sc, uint32_t *temp);
94 1.19 ad static void owtemp_refresh(struct sysmon_envsys *, envsys_data_t *);
95 1.19 ad static uint32_t owtemp_decode_ds18b20(const uint8_t *);
96 1.19 ad static uint32_t owtemp_decode_ds1920(const uint8_t *);
97 1.1 riz
98 1.14 xtraeme CFATTACH_DECL_NEW(owtemp, sizeof(struct owtemp_softc),
99 1.1 riz owtemp_match, owtemp_attach, owtemp_detach, owtemp_activate);
100 1.1 riz
101 1.1 riz extern struct cfdriver owtemp_cd;
102 1.1 riz
103 1.1 riz static const struct onewire_matchfam owtemp_fams[] = {
104 1.17 kardel { ONEWIRE_FAMILY_DS1920 }, /* also DS1820 */
105 1.1 riz { ONEWIRE_FAMILY_DS18B20 },
106 1.1 riz { ONEWIRE_FAMILY_DS1822 },
107 1.1 riz };
108 1.1 riz
109 1.19 ad int owtemp_retries = 3;
110 1.1 riz
111 1.14 xtraeme static int
112 1.14 xtraeme owtemp_match(device_t parent, cfdata_t match, void *aux)
113 1.1 riz {
114 1.1 riz return (onewire_matchbyfam(aux, owtemp_fams,
115 1.7 xtraeme __arraycount(owtemp_fams)));
116 1.1 riz }
117 1.1 riz
118 1.14 xtraeme static void
119 1.14 xtraeme owtemp_attach(device_t parent, device_t self, void *aux)
120 1.1 riz {
121 1.1 riz struct owtemp_softc *sc = device_private(self);
122 1.1 riz struct onewire_attach_args *oa = aux;
123 1.1 riz
124 1.14 xtraeme aprint_naive("\n");
125 1.14 xtraeme
126 1.18 martin sc->sc_dv = self;
127 1.1 riz sc->sc_onewire = oa->oa_onewire;
128 1.1 riz sc->sc_rom = oa->oa_rom;
129 1.1 riz
130 1.1 riz switch(ONEWIRE_ROM_FAMILY_TYPE(sc->sc_rom)) {
131 1.1 riz case ONEWIRE_FAMILY_DS18B20:
132 1.18 martin sc->sc_chipname = "DS18B20";
133 1.18 martin sc->sc_owtemp_decode = owtemp_decode_ds18b20;
134 1.18 martin break;
135 1.1 riz case ONEWIRE_FAMILY_DS1822:
136 1.18 martin sc->sc_chipname = "DS1822";
137 1.1 riz sc->sc_owtemp_decode = owtemp_decode_ds18b20;
138 1.1 riz break;
139 1.1 riz case ONEWIRE_FAMILY_DS1920:
140 1.18 martin sc->sc_chipname = "DS1920";
141 1.1 riz sc->sc_owtemp_decode = owtemp_decode_ds1920;
142 1.1 riz break;
143 1.1 riz }
144 1.1 riz
145 1.19 ad evcnt_attach_dynamic(&sc->sc_ev_update, EVCNT_TYPE_MISC, NULL,
146 1.19 ad device_xname(self), "update");
147 1.19 ad evcnt_attach_dynamic(&sc->sc_ev_rsterr, EVCNT_TYPE_MISC, NULL,
148 1.19 ad device_xname(self), "reset error");
149 1.19 ad evcnt_attach_dynamic(&sc->sc_ev_crcerr, EVCNT_TYPE_MISC, NULL,
150 1.19 ad device_xname(self), "crc error");
151 1.19 ad
152 1.12 xtraeme sc->sc_sme = sysmon_envsys_create();
153 1.12 xtraeme
154 1.1 riz /* Initialize sensor */
155 1.12 xtraeme sc->sc_sensor.units = ENVSYS_STEMP;
156 1.16 pgoyette sc->sc_sensor.state = ENVSYS_SINVALID;
157 1.12 xtraeme (void)strlcpy(sc->sc_sensor.desc,
158 1.14 xtraeme device_xname(self), sizeof(sc->sc_sensor.desc));
159 1.18 martin (void)snprintf(sc->sc_sensor.desc, sizeof(sc->sc_sensor.desc),
160 1.18 martin "%s S/N %012" PRIx64, sc->sc_chipname, ONEWIRE_ROM_SN(sc->sc_rom));
161 1.12 xtraeme if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) {
162 1.12 xtraeme sysmon_envsys_destroy(sc->sc_sme);
163 1.12 xtraeme return;
164 1.12 xtraeme }
165 1.1 riz
166 1.1 riz /* Hook into system monitor. */
167 1.14 xtraeme sc->sc_sme->sme_name = device_xname(self);
168 1.12 xtraeme sc->sc_sme->sme_cookie = sc;
169 1.12 xtraeme sc->sc_sme->sme_refresh = owtemp_refresh;
170 1.1 riz
171 1.12 xtraeme if (sysmon_envsys_register(sc->sc_sme)) {
172 1.14 xtraeme aprint_error_dev(self, "unable to register with sysmon\n");
173 1.12 xtraeme sysmon_envsys_destroy(sc->sc_sme);
174 1.12 xtraeme return;
175 1.12 xtraeme }
176 1.1 riz
177 1.14 xtraeme aprint_normal("\n");
178 1.1 riz }
179 1.1 riz
180 1.14 xtraeme static int
181 1.14 xtraeme owtemp_detach(device_t self, int flags)
182 1.1 riz {
183 1.1 riz struct owtemp_softc *sc = device_private(self);
184 1.1 riz
185 1.12 xtraeme sysmon_envsys_unregister(sc->sc_sme);
186 1.19 ad evcnt_detach(&sc->sc_ev_rsterr);
187 1.19 ad evcnt_detach(&sc->sc_ev_update);
188 1.19 ad evcnt_detach(&sc->sc_ev_crcerr);
189 1.1 riz
190 1.1 riz return 0;
191 1.1 riz }
192 1.1 riz
193 1.14 xtraeme static int
194 1.14 xtraeme owtemp_activate(device_t self, enum devact act)
195 1.1 riz {
196 1.1 riz struct owtemp_softc *sc = device_private(self);
197 1.1 riz
198 1.1 riz switch (act) {
199 1.1 riz case DVACT_DEACTIVATE:
200 1.1 riz sc->sc_dying = 1;
201 1.15 dyoung return 0;
202 1.15 dyoung default:
203 1.15 dyoung return EOPNOTSUPP;
204 1.1 riz }
205 1.1 riz }
206 1.1 riz
207 1.19 ad static bool
208 1.19 ad owtemp_update(struct owtemp_softc *sc, uint32_t *temp)
209 1.1 riz {
210 1.1 riz u_int8_t data[9];
211 1.1 riz
212 1.19 ad sc->sc_ev_update.ev_count++;
213 1.1 riz
214 1.1 riz /*
215 1.1 riz * Start temperature conversion. The conversion takes up to 750ms.
216 1.1 riz * After sending the command, the data line must be held high for
217 1.1 riz * at least 750ms to provide power during the conversion process.
218 1.1 riz * As such, no other activity may take place on the 1-Wire bus for
219 1.18 martin * at least this period. Keep the parent bus locked while waiting.
220 1.1 riz */
221 1.18 martin if (onewire_reset(sc->sc_onewire) != 0) {
222 1.19 ad sc->sc_ev_rsterr.ev_count++;
223 1.19 ad return false;
224 1.18 martin }
225 1.1 riz onewire_matchrom(sc->sc_onewire, sc->sc_rom);
226 1.19 ad onewire_write_byte(sc->sc_onewire, DS_CMD_CONVERT);
227 1.19 ad (void)kpause("owtemp", false, mstohz(750 + 10), NULL);
228 1.1 riz
229 1.1 riz /*
230 1.1 riz * The result of the temperature measurement is placed in the
231 1.19 ad * first two bytes of the scratchpad. Perform the caculation
232 1.19 ad * only if the CRC is correct.
233 1.1 riz */
234 1.19 ad if (onewire_reset(sc->sc_onewire) != 0) {
235 1.19 ad sc->sc_ev_rsterr.ev_count++;
236 1.19 ad return false;
237 1.19 ad }
238 1.19 ad onewire_matchrom(sc->sc_onewire, sc->sc_rom);
239 1.1 riz onewire_write_byte(sc->sc_onewire, DS_CMD_READ_SCRATCHPAD);
240 1.1 riz onewire_read_block(sc->sc_onewire, data, 9);
241 1.19 ad if (onewire_crc(data, 8) != data[8]) {
242 1.19 ad sc->sc_ev_crcerr.ev_count++;
243 1.19 ad return false;
244 1.1 riz }
245 1.19 ad *temp = sc->sc_owtemp_decode(data);
246 1.19 ad return true;
247 1.1 riz }
248 1.1 riz
249 1.12 xtraeme static void
250 1.12 xtraeme owtemp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
251 1.1 riz {
252 1.1 riz struct owtemp_softc *sc = sme->sme_cookie;
253 1.19 ad uint32_t reading;
254 1.19 ad int retry;
255 1.1 riz
256 1.19 ad onewire_lock(sc->sc_onewire);
257 1.19 ad for (retry = 0; retry < owtemp_retries; retry++) {
258 1.19 ad if (owtemp_update(sc, &reading)) {
259 1.19 ad onewire_unlock(sc->sc_onewire);
260 1.19 ad sc->sc_sensor.value_cur = reading;
261 1.19 ad sc->sc_sensor.state = ENVSYS_SVALID;
262 1.19 ad return;
263 1.19 ad }
264 1.19 ad }
265 1.19 ad onewire_unlock(sc->sc_onewire);
266 1.19 ad aprint_error_dev(sc->sc_dv,
267 1.19 ad "update failed - use vmstat(8) to check event counters\n");
268 1.19 ad sc->sc_sensor.state = ENVSYS_SINVALID;
269 1.1 riz }
270 1.1 riz
271 1.1 riz static uint32_t
272 1.1 riz owtemp_decode_ds18b20(const uint8_t *buf)
273 1.1 riz {
274 1.1 riz int temp;
275 1.1 riz
276 1.1 riz /*
277 1.1 riz * Sign-extend the MSB byte, and add in the fractions of a
278 1.1 riz * degree contained in the LSB (precision 1/16th DegC).
279 1.1 riz */
280 1.1 riz temp = (int8_t)buf[1];
281 1.1 riz temp = (temp << 8) | buf[0];
282 1.1 riz
283 1.1 riz /*
284 1.1 riz * Conversion to uK is simple.
285 1.1 riz */
286 1.1 riz return (temp * 62500 + 273150000);
287 1.1 riz }
288 1.1 riz
289 1.1 riz static uint32_t
290 1.1 riz owtemp_decode_ds1920(const uint8_t *buf)
291 1.1 riz {
292 1.1 riz int temp;
293 1.1 riz
294 1.1 riz /*
295 1.1 riz * Sign-extend the MSB byte, and add in the fractions of a
296 1.1 riz * degree contained in the LSB (precision 1/2 DegC).
297 1.1 riz */
298 1.1 riz temp = (int8_t)buf[1];
299 1.1 riz temp = (temp << 8) | buf[0];
300 1.1 riz
301 1.17 kardel if (buf[7] != 0) {
302 1.17 kardel /*
303 1.17 kardel * interpolate for higher precision using the count registers
304 1.17 kardel *
305 1.17 kardel * buf[7]: COUNT_PER_C(elsius)
306 1.17 kardel * buf[6]: COUNT_REMAIN
307 1.17 kardel *
308 1.17 kardel * T = TEMP - 0.25 + (COUNT_PER_C - COUNT_REMAIN) / COUNT_PER_C
309 1.17 kardel */
310 1.17 kardel temp &= ~1;
311 1.17 kardel temp += 500000 * temp + (500000 * (buf[7] - buf[6])) / buf[7] - 250000;
312 1.17 kardel } else {
313 1.17 kardel temp *= 500000;
314 1.17 kardel }
315 1.17 kardel
316 1.17 kardel /* convert to uK */
317 1.17 kardel return (temp + 273150000);
318 1.1 riz }
319