owtemp.c revision 1.18 1 /* $NetBSD: owtemp.c,v 1.18 2019/10/25 16:25:14 martin Exp $ */
2 /* $OpenBSD: owtemp.c,v 1.1 2006/03/04 16:27:03 grange Exp $ */
3
4 /*
5 * Copyright (c) 2006 Alexander Yurchenko <grange (at) openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 /*
21 * 1-Wire temperature family type device driver.
22 */
23
24 #include <sys/cdefs.h>
25 __KERNEL_RCSID(0, "$NetBSD: owtemp.c,v 1.18 2019/10/25 16:25:14 martin Exp $");
26
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/device.h>
30 #include <sys/kernel.h>
31 #include <sys/proc.h>
32
33 #include <dev/sysmon/sysmonvar.h>
34
35 #include <dev/onewire/onewiredevs.h>
36 #include <dev/onewire/onewirereg.h>
37 #include <dev/onewire/onewirevar.h>
38
39 #define DS_CMD_CONVERT 0x44
40 #define DS_CMD_READ_SCRATCHPAD 0xbe
41
42 struct owtemp_softc {
43 device_t sc_dv;
44 void *sc_onewire;
45 u_int64_t sc_rom;
46 const char *sc_chipname;
47
48 envsys_data_t sc_sensor;
49 struct sysmon_envsys *sc_sme;
50
51 uint32_t (*sc_owtemp_decode)(const uint8_t *);
52
53 int sc_dying;
54 };
55
56 static int owtemp_match(device_t, cfdata_t, void *);
57 static void owtemp_attach(device_t, device_t, void *);
58 static int owtemp_detach(device_t, int);
59 static int owtemp_activate(device_t, enum devact);
60
61 static void owtemp_update(void *);
62
63 CFATTACH_DECL_NEW(owtemp, sizeof(struct owtemp_softc),
64 owtemp_match, owtemp_attach, owtemp_detach, owtemp_activate);
65
66 extern struct cfdriver owtemp_cd;
67
68 static const struct onewire_matchfam owtemp_fams[] = {
69 { ONEWIRE_FAMILY_DS1920 }, /* also DS1820 */
70 { ONEWIRE_FAMILY_DS18B20 },
71 { ONEWIRE_FAMILY_DS1822 },
72 };
73
74 static void owtemp_refresh(struct sysmon_envsys *, envsys_data_t *);
75
76 static uint32_t owtemp_decode_ds18b20(const uint8_t *);
77 static uint32_t owtemp_decode_ds1920(const uint8_t *);
78
79 static int
80 owtemp_match(device_t parent, cfdata_t match, void *aux)
81 {
82 return (onewire_matchbyfam(aux, owtemp_fams,
83 __arraycount(owtemp_fams)));
84 }
85
86 static void
87 owtemp_attach(device_t parent, device_t self, void *aux)
88 {
89 struct owtemp_softc *sc = device_private(self);
90 struct onewire_attach_args *oa = aux;
91
92 aprint_naive("\n");
93
94 sc->sc_dv = self;
95 sc->sc_onewire = oa->oa_onewire;
96 sc->sc_rom = oa->oa_rom;
97
98 switch(ONEWIRE_ROM_FAMILY_TYPE(sc->sc_rom)) {
99 case ONEWIRE_FAMILY_DS18B20:
100 sc->sc_chipname = "DS18B20";
101 sc->sc_owtemp_decode = owtemp_decode_ds18b20;
102 break;
103 case ONEWIRE_FAMILY_DS1822:
104 sc->sc_chipname = "DS1822";
105 sc->sc_owtemp_decode = owtemp_decode_ds18b20;
106 break;
107 case ONEWIRE_FAMILY_DS1920:
108 sc->sc_chipname = "DS1920";
109 sc->sc_owtemp_decode = owtemp_decode_ds1920;
110 break;
111 }
112
113 sc->sc_sme = sysmon_envsys_create();
114
115 /* Initialize sensor */
116 sc->sc_sensor.units = ENVSYS_STEMP;
117 sc->sc_sensor.state = ENVSYS_SINVALID;
118 (void)strlcpy(sc->sc_sensor.desc,
119 device_xname(self), sizeof(sc->sc_sensor.desc));
120 (void)snprintf(sc->sc_sensor.desc, sizeof(sc->sc_sensor.desc),
121 "%s S/N %012" PRIx64, sc->sc_chipname, ONEWIRE_ROM_SN(sc->sc_rom));
122 if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) {
123 sysmon_envsys_destroy(sc->sc_sme);
124 return;
125 }
126
127 /* Hook into system monitor. */
128 sc->sc_sme->sme_name = device_xname(self);
129 sc->sc_sme->sme_cookie = sc;
130 sc->sc_sme->sme_refresh = owtemp_refresh;
131
132 if (sysmon_envsys_register(sc->sc_sme)) {
133 aprint_error_dev(self, "unable to register with sysmon\n");
134 sysmon_envsys_destroy(sc->sc_sme);
135 return;
136 }
137
138 aprint_normal("\n");
139 }
140
141 static int
142 owtemp_detach(device_t self, int flags)
143 {
144 struct owtemp_softc *sc = device_private(self);
145
146 sysmon_envsys_unregister(sc->sc_sme);
147
148 return 0;
149 }
150
151 static int
152 owtemp_activate(device_t self, enum devact act)
153 {
154 struct owtemp_softc *sc = device_private(self);
155
156 switch (act) {
157 case DVACT_DEACTIVATE:
158 sc->sc_dying = 1;
159 return 0;
160 default:
161 return EOPNOTSUPP;
162 }
163 }
164
165 static void
166 owtemp_update(void *arg)
167 {
168 struct owtemp_softc *sc = arg;
169 u_int8_t data[9];
170
171 onewire_lock(sc->sc_onewire);
172 if (onewire_reset(sc->sc_onewire) != 0) {
173 aprint_error_dev(sc->sc_dv, "owtemp_update: 1st reset failed\n");
174 goto done;
175 }
176 onewire_matchrom(sc->sc_onewire, sc->sc_rom);
177
178 /*
179 * Start temperature conversion. The conversion takes up to 750ms.
180 * After sending the command, the data line must be held high for
181 * at least 750ms to provide power during the conversion process.
182 * As such, no other activity may take place on the 1-Wire bus for
183 * at least this period. Keep the parent bus locked while waiting.
184 */
185 onewire_write_byte(sc->sc_onewire, DS_CMD_CONVERT);
186 kpause("owtemp", false, mstohz(750 + 10), NULL);
187
188 if (onewire_reset(sc->sc_onewire) != 0) {
189 aprint_error_dev(sc->sc_dv, "owtemp_update: 2nd reset failed\n");
190 goto done;
191 }
192 onewire_matchrom(sc->sc_onewire, sc->sc_rom);
193
194 /*
195 * The result of the temperature measurement is placed in the
196 * first two bytes of the scratchpad.
197 */
198 onewire_write_byte(sc->sc_onewire, DS_CMD_READ_SCRATCHPAD);
199 onewire_read_block(sc->sc_onewire, data, 9);
200 #if 0
201 if (onewire_crc(data, 8) == data[8]) {
202 sc->sc_sensor.value = 273150000 +
203 (int)((u_int16_t)data[1] << 8 | data[0]) * 500000;
204 }
205 #endif
206
207 sc->sc_sensor.value_cur = sc->sc_owtemp_decode(data);
208 sc->sc_sensor.state = ENVSYS_SVALID;
209
210 done:
211 onewire_unlock(sc->sc_onewire);
212 }
213
214 static void
215 owtemp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
216 {
217 struct owtemp_softc *sc = sme->sme_cookie;
218
219 owtemp_update(sc);
220 }
221
222 static uint32_t
223 owtemp_decode_ds18b20(const uint8_t *buf)
224 {
225 int temp;
226
227 /*
228 * Sign-extend the MSB byte, and add in the fractions of a
229 * degree contained in the LSB (precision 1/16th DegC).
230 */
231 temp = (int8_t)buf[1];
232 temp = (temp << 8) | buf[0];
233
234 /*
235 * Conversion to uK is simple.
236 */
237 return (temp * 62500 + 273150000);
238 }
239
240 static uint32_t
241 owtemp_decode_ds1920(const uint8_t *buf)
242 {
243 int temp;
244
245 /*
246 * Sign-extend the MSB byte, and add in the fractions of a
247 * degree contained in the LSB (precision 1/2 DegC).
248 */
249 temp = (int8_t)buf[1];
250 temp = (temp << 8) | buf[0];
251
252 if (buf[7] != 0) {
253 /*
254 * interpolate for higher precision using the count registers
255 *
256 * buf[7]: COUNT_PER_C(elsius)
257 * buf[6]: COUNT_REMAIN
258 *
259 * T = TEMP - 0.25 + (COUNT_PER_C - COUNT_REMAIN) / COUNT_PER_C
260 */
261 temp &= ~1;
262 temp += 500000 * temp + (500000 * (buf[7] - buf[6])) / buf[7] - 250000;
263 } else {
264 temp *= 500000;
265 }
266
267 /* convert to uK */
268 return (temp + 273150000);
269 }
270