si70xx.c revision 1.9 1 1.9 brad /* $NetBSD: si70xx.c,v 1.9 2021/11/11 14:16:04 brad Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 2017 Brad Spencer <brad (at) anduin.eldar.org>
5 1.1 christos *
6 1.1 christos * Permission to use, copy, modify, and distribute this software for any
7 1.1 christos * purpose with or without fee is hereby granted, provided that the above
8 1.1 christos * copyright notice and this permission notice appear in all copies.
9 1.1 christos *
10 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 1.1 christos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1.1 christos */
18 1.1 christos
19 1.1 christos #include <sys/cdefs.h>
20 1.9 brad __KERNEL_RCSID(0, "$NetBSD: si70xx.c,v 1.9 2021/11/11 14:16:04 brad Exp $");
21 1.1 christos
22 1.1 christos /*
23 1.9 brad Driver for the Silicon Labs SI7013/SI7020/SI7021, HTU21D and SHT21
24 1.1 christos */
25 1.1 christos
26 1.1 christos #include <sys/param.h>
27 1.1 christos #include <sys/systm.h>
28 1.1 christos #include <sys/kernel.h>
29 1.1 christos #include <sys/device.h>
30 1.1 christos #include <sys/module.h>
31 1.1 christos #include <sys/sysctl.h>
32 1.1 christos #include <sys/mutex.h>
33 1.1 christos
34 1.1 christos #include <dev/sysmon/sysmonvar.h>
35 1.1 christos #include <dev/i2c/i2cvar.h>
36 1.1 christos #include <dev/i2c/si70xxreg.h>
37 1.1 christos #include <dev/i2c/si70xxvar.h>
38 1.1 christos
39 1.1 christos
40 1.1 christos static uint8_t si70xx_crc(uint8_t *, size_t);
41 1.1 christos static int si70xx_poke(i2c_tag_t, i2c_addr_t, bool);
42 1.1 christos static int si70xx_match(device_t, cfdata_t, void *);
43 1.1 christos static void si70xx_attach(device_t, device_t, void *);
44 1.1 christos static int si70xx_detach(device_t, int);
45 1.1 christos static void si70xx_refresh(struct sysmon_envsys *, envsys_data_t *);
46 1.1 christos static int si70xx_update_status(struct si70xx_sc *);
47 1.1 christos static int si70xx_set_heateron(struct si70xx_sc *);
48 1.1 christos static int si70xx_set_resolution(struct si70xx_sc *, size_t);
49 1.1 christos static int si70xx_set_heatervalue(struct si70xx_sc *, size_t);
50 1.1 christos static int si70xx_verify_sysctl(SYSCTLFN_ARGS);
51 1.1 christos static int si70xx_verify_sysctl_resolution(SYSCTLFN_ARGS);
52 1.1 christos static int si70xx_verify_sysctl_heateron(SYSCTLFN_ARGS);
53 1.1 christos static int si70xx_verify_sysctl_heatervalue(SYSCTLFN_ARGS);
54 1.1 christos
55 1.1 christos #define SI70XX_DEBUG
56 1.1 christos #ifdef SI70XX_DEBUG
57 1.1 christos #define DPRINTF(s, l, x) \
58 1.1 christos do { \
59 1.1 christos if (l <= s->sc_si70xxdebug) \
60 1.1 christos printf x; \
61 1.1 christos } while (/*CONSTCOND*/0)
62 1.1 christos #else
63 1.1 christos #define DPRINTF(s, l, x)
64 1.1 christos #endif
65 1.1 christos
66 1.1 christos CFATTACH_DECL_NEW(si70xxtemp, sizeof(struct si70xx_sc),
67 1.1 christos si70xx_match, si70xx_attach, si70xx_detach, NULL);
68 1.1 christos
69 1.1 christos static struct si70xx_sensor si70xx_sensors[] = {
70 1.1 christos {
71 1.1 christos .desc = "humidity",
72 1.1 christos .type = ENVSYS_SRELHUMIDITY,
73 1.1 christos },
74 1.1 christos {
75 1.1 christos .desc = "temperature",
76 1.1 christos .type = ENVSYS_STEMP,
77 1.1 christos }
78 1.1 christos };
79 1.1 christos
80 1.1 christos static struct si70xx_resolution si70xx_resolutions[] = {
81 1.1 christos {
82 1.1 christos .text = "12bit/14bit",
83 1.1 christos .num = 0x00,
84 1.1 christos },
85 1.1 christos {
86 1.1 christos .text = "8bit/12bit",
87 1.1 christos .num = 0x01,
88 1.1 christos },
89 1.1 christos {
90 1.1 christos .text = "10bit/13bit",
91 1.1 christos .num = 0x80,
92 1.1 christos },
93 1.1 christos {
94 1.1 christos .text = "11bit/11bit",
95 1.1 christos .num = 0x81,
96 1.1 christos }
97 1.1 christos };
98 1.1 christos
99 1.1 christos static const char si70xx_resolution_names[] =
100 1.1 christos "12bit/14bit, 8bit/12bit, 10bit/13bit, 11bit/11bit";
101 1.1 christos
102 1.1 christos static const int si70xx_heatervalues[] = {
103 1.1 christos 0xdeadbeef, 0x00, 0x01, 0x02, 0x04, 0x08, 0x0f
104 1.1 christos };
105 1.1 christos
106 1.1 christos int
107 1.1 christos si70xx_verify_sysctl(SYSCTLFN_ARGS)
108 1.1 christos {
109 1.1 christos int error, t;
110 1.1 christos struct sysctlnode node;
111 1.1 christos
112 1.1 christos node = *rnode;
113 1.1 christos t = *(int *)rnode->sysctl_data;
114 1.1 christos node.sysctl_data = &t;
115 1.1 christos error = sysctl_lookup(SYSCTLFN_CALL(&node));
116 1.1 christos if (error || newp == NULL)
117 1.1 christos return error;
118 1.1 christos
119 1.1 christos if (t < 0)
120 1.1 christos return EINVAL;
121 1.1 christos
122 1.1 christos *(int *)rnode->sysctl_data = t;
123 1.1 christos
124 1.1 christos return 0;
125 1.1 christos }
126 1.1 christos
127 1.1 christos int
128 1.1 christos si70xx_verify_sysctl_resolution(SYSCTLFN_ARGS)
129 1.1 christos {
130 1.1 christos char buf[SI70XX_RES_NAME];
131 1.1 christos struct si70xx_sc *sc;
132 1.1 christos struct sysctlnode node;
133 1.1 christos int error = 0;
134 1.1 christos size_t i;
135 1.1 christos
136 1.1 christos node = *rnode;
137 1.1 christos sc = node.sysctl_data;
138 1.1 christos (void) memcpy(buf, sc->sc_resolution, SI70XX_RES_NAME);
139 1.1 christos node.sysctl_data = buf;
140 1.1 christos error = sysctl_lookup(SYSCTLFN_CALL(&node));
141 1.1 christos if (error || newp == NULL)
142 1.1 christos return error;
143 1.1 christos
144 1.1 christos for (i = 0; i < __arraycount(si70xx_resolutions); i++) {
145 1.1 christos if (memcmp(node.sysctl_data, si70xx_resolutions[i].text,
146 1.1 christos SI70XX_RES_NAME) == 0)
147 1.1 christos break;
148 1.1 christos }
149 1.1 christos
150 1.1 christos if (i == __arraycount(si70xx_resolutions))
151 1.1 christos return EINVAL;
152 1.1 christos (void) memcpy(sc->sc_resolution, node.sysctl_data, SI70XX_RES_NAME);
153 1.1 christos
154 1.1 christos error = si70xx_set_resolution(sc, i);
155 1.1 christos
156 1.1 christos return error;
157 1.1 christos }
158 1.1 christos
159 1.1 christos int
160 1.1 christos si70xx_verify_sysctl_heateron(SYSCTLFN_ARGS)
161 1.1 christos {
162 1.1 christos int error;
163 1.1 christos bool t;
164 1.1 christos struct si70xx_sc *sc;
165 1.1 christos struct sysctlnode node;
166 1.1 christos
167 1.1 christos node = *rnode;
168 1.1 christos sc = node.sysctl_data;
169 1.1 christos t = sc->sc_heateron;
170 1.1 christos node.sysctl_data = &t;
171 1.1 christos error = sysctl_lookup(SYSCTLFN_CALL(&node));
172 1.1 christos if (error || newp == NULL)
173 1.1 christos return error;
174 1.1 christos
175 1.1 christos sc->sc_heateron = t;
176 1.1 christos error = si70xx_set_heateron(sc);
177 1.1 christos
178 1.1 christos return error;
179 1.1 christos }
180 1.1 christos
181 1.1 christos int
182 1.1 christos si70xx_verify_sysctl_heatervalue(SYSCTLFN_ARGS)
183 1.1 christos {
184 1.1 christos int error = 0, t;
185 1.1 christos struct si70xx_sc *sc;
186 1.1 christos struct sysctlnode node;
187 1.1 christos
188 1.1 christos node = *rnode;
189 1.1 christos sc = node.sysctl_data;
190 1.1 christos t = sc->sc_heaterval;
191 1.1 christos node.sysctl_data = &t;
192 1.1 christos error = sysctl_lookup(SYSCTLFN_CALL(&node));
193 1.1 christos if (error || newp == NULL)
194 1.1 christos return (error);
195 1.1 christos
196 1.1 christos if (t < 1 || t >= __arraycount(si70xx_heatervalues))
197 1.1 christos return (EINVAL);
198 1.1 christos
199 1.1 christos sc->sc_heaterval = t;
200 1.1 christos error = si70xx_set_heatervalue(sc, t);
201 1.1 christos
202 1.1 christos return error;
203 1.1 christos }
204 1.1 christos
205 1.3 christos static uint8_t
206 1.3 christos si70xx_dir(uint8_t cmd, size_t len)
207 1.1 christos {
208 1.3 christos switch (cmd) {
209 1.1 christos case SI70XX_READ_USER_REG_1:
210 1.1 christos case SI70XX_READ_HEATER_REG:
211 1.1 christos case SI70XX_READ_ID_PT1A:
212 1.1 christos case SI70XX_READ_ID_PT1B:
213 1.1 christos case SI70XX_READ_ID_PT2A:
214 1.1 christos case SI70XX_READ_ID_PT2B:
215 1.1 christos case SI70XX_READ_FW_VERA:
216 1.1 christos case SI70XX_READ_FW_VERB:
217 1.3 christos return I2C_OP_READ_WITH_STOP;
218 1.1 christos case SI70XX_WRITE_USER_REG_1:
219 1.1 christos case SI70XX_WRITE_HEATER_REG:
220 1.1 christos case SI70XX_RESET:
221 1.3 christos return I2C_OP_WRITE_WITH_STOP;
222 1.1 christos case SI70XX_MEASURE_RH_NOHOLD:
223 1.1 christos case SI70XX_MEASURE_TEMP_NOHOLD:
224 1.3 christos return len == 0 ? I2C_OP_READ : I2C_OP_READ_WITH_STOP;
225 1.1 christos default:
226 1.3 christos panic("%s: bad command %#x\n", __func__, cmd);
227 1.3 christos return 0;
228 1.1 christos }
229 1.3 christos }
230 1.3 christos
231 1.3 christos static int
232 1.3 christos si70xx_cmd(i2c_tag_t tag, i2c_addr_t addr, uint8_t *cmd,
233 1.3 christos uint8_t clen, uint8_t *buf, size_t blen)
234 1.3 christos {
235 1.3 christos uint8_t dir;
236 1.3 christos if (clen == 0)
237 1.3 christos dir = blen == 0 ? I2C_OP_READ : I2C_OP_READ_WITH_STOP;
238 1.3 christos else
239 1.3 christos dir = si70xx_dir(cmd[0], blen);
240 1.3 christos
241 1.3 christos if (dir == I2C_OP_READ || dir == I2C_OP_READ_WITH_STOP)
242 1.3 christos memset(buf, 0, blen);
243 1.1 christos
244 1.1 christos return iic_exec(tag, dir, addr, cmd, clen, buf, blen, 0);
245 1.1 christos }
246 1.1 christos
247 1.1 christos static int
248 1.1 christos si70xx_cmd0(struct si70xx_sc *sc, uint8_t *buf, size_t blen)
249 1.1 christos {
250 1.1 christos return si70xx_cmd(sc->sc_tag, sc->sc_addr, NULL, 0, buf, blen);
251 1.1 christos }
252 1.1 christos
253 1.1 christos static int
254 1.1 christos si70xx_cmd1(struct si70xx_sc *sc, uint8_t cmd, uint8_t *buf, size_t blen)
255 1.1 christos {
256 1.1 christos return si70xx_cmd(sc->sc_tag, sc->sc_addr, &cmd, 1, buf, blen);
257 1.1 christos }
258 1.1 christos
259 1.1 christos static int
260 1.1 christos si70xx_cmd2(struct si70xx_sc *sc, uint8_t cmd1, uint8_t cmd2, uint8_t *buf,
261 1.1 christos size_t blen)
262 1.1 christos {
263 1.1 christos uint8_t cmd[] = { cmd1, cmd2 };
264 1.1 christos return si70xx_cmd(sc->sc_tag, sc->sc_addr, cmd, __arraycount(cmd),
265 1.1 christos buf, blen);
266 1.1 christos }
267 1.1 christos
268 1.1 christos static int
269 1.1 christos si70xx_set_heateron(struct si70xx_sc * sc)
270 1.1 christos {
271 1.1 christos int error;
272 1.1 christos uint8_t userregister;
273 1.1 christos
274 1.1 christos error = iic_acquire_bus(sc->sc_tag, 0);
275 1.1 christos if (error) {
276 1.1 christos DPRINTF(sc, 2, ("%s:%s: Failed to acquire bus: %d\n",
277 1.1 christos device_xname(sc->sc_dev), __func__, error));
278 1.1 christos return error;
279 1.1 christos }
280 1.1 christos
281 1.1 christos error = si70xx_cmd1(sc, SI70XX_READ_USER_REG_1, &userregister, 1);
282 1.1 christos if (error) {
283 1.1 christos DPRINTF(sc, 2, ("%s: Failed to read user register 1: %d\n",
284 1.1 christos device_xname(sc->sc_dev), error));
285 1.1 christos goto out;
286 1.1 christos }
287 1.1 christos
288 1.1 christos DPRINTF(sc, 2, ("%s:%s: reg 1 values before: %#x\n",
289 1.1 christos device_xname(sc->sc_dev), __func__, userregister));
290 1.1 christos if (sc->sc_heateron) {
291 1.1 christos userregister |= SI70XX_HTRE_MASK;
292 1.1 christos } else {
293 1.1 christos userregister &= ~SI70XX_HTRE_MASK;
294 1.1 christos }
295 1.1 christos DPRINTF(sc, 2, ("%s:%s: user reg 1 values after: %#x\n",
296 1.1 christos device_xname(sc->sc_dev), __func__, userregister));
297 1.1 christos
298 1.1 christos error = si70xx_cmd1(sc, SI70XX_WRITE_USER_REG_1, &userregister, 1);
299 1.1 christos if (error) {
300 1.1 christos DPRINTF(sc, 2, ("%s: Failed to write user register 1: %d\n",
301 1.1 christos device_xname(sc->sc_dev), error));
302 1.1 christos }
303 1.1 christos out:
304 1.1 christos iic_release_bus(sc->sc_tag, 0);
305 1.1 christos return error;
306 1.1 christos }
307 1.1 christos
308 1.1 christos static int
309 1.1 christos si70xx_set_resolution(struct si70xx_sc * sc, size_t index)
310 1.1 christos {
311 1.1 christos int error;
312 1.1 christos uint8_t userregister;
313 1.1 christos
314 1.1 christos error = iic_acquire_bus(sc->sc_tag, 0);
315 1.1 christos if (error) {
316 1.1 christos DPRINTF(sc, 2, ("%s: Failed to acquire bus: %d\n",
317 1.1 christos device_xname(sc->sc_dev), error));
318 1.1 christos return error;
319 1.1 christos }
320 1.1 christos
321 1.1 christos error = si70xx_cmd1(sc, SI70XX_READ_USER_REG_1, &userregister, 1);
322 1.1 christos if (error) {
323 1.1 christos DPRINTF(sc, 2, ("%s: Failed to read user register 1: %d\n",
324 1.1 christos device_xname(sc->sc_dev), error));
325 1.1 christos goto out;
326 1.1 christos }
327 1.1 christos
328 1.1 christos DPRINTF(sc, 2, ("%s:%s: reg 1 values before: %#x\n",
329 1.1 christos device_xname(sc->sc_dev), __func__, userregister));
330 1.1 christos userregister &= (~SI70XX_RESOLUTION_MASK);
331 1.1 christos userregister |= si70xx_resolutions[index].num;
332 1.1 christos DPRINTF(sc, 2, ("%s:%s: reg 1 values after: %#x\n",
333 1.1 christos device_xname(sc->sc_dev), __func__, userregister));
334 1.1 christos
335 1.1 christos error = si70xx_cmd1(sc, SI70XX_WRITE_USER_REG_1, &userregister, 1);
336 1.1 christos if (error) {
337 1.1 christos DPRINTF(sc, 2, ("%s: Failed to write user register 1: %d\n",
338 1.1 christos device_xname(sc->sc_dev), error));
339 1.1 christos }
340 1.1 christos out:
341 1.1 christos iic_release_bus(sc->sc_tag, 0);
342 1.1 christos return error;
343 1.1 christos }
344 1.1 christos
345 1.1 christos static int
346 1.1 christos si70xx_set_heatervalue(struct si70xx_sc * sc, size_t index)
347 1.1 christos {
348 1.1 christos int error;
349 1.1 christos uint8_t heaterregister;
350 1.1 christos
351 1.1 christos error = iic_acquire_bus(sc->sc_tag, 0);
352 1.1 christos if (error) {
353 1.1 christos DPRINTF(sc, 2, ("%s: Failed to acquire bus: %d\n",
354 1.1 christos device_xname(sc->sc_dev), error));
355 1.1 christos return error;
356 1.1 christos }
357 1.1 christos error = si70xx_cmd1(sc, SI70XX_READ_HEATER_REG, &heaterregister, 1);
358 1.1 christos if (error) {
359 1.1 christos DPRINTF(sc, 2, ("%s: Failed to read heater register: %d\n",
360 1.1 christos device_xname(sc->sc_dev), error));
361 1.1 christos goto out;
362 1.1 christos }
363 1.1 christos
364 1.1 christos DPRINTF(sc, 2, ("%s:%s: heater values before: %#x\n",
365 1.1 christos device_xname(sc->sc_dev), __func__, heaterregister));
366 1.1 christos heaterregister &= ~SI70XX_HEATER_MASK;
367 1.1 christos heaterregister |= si70xx_heatervalues[index];
368 1.1 christos DPRINTF(sc, 2, ("%s:%s: heater values after: %#x\n",
369 1.1 christos device_xname(sc->sc_dev), __func__, heaterregister));
370 1.1 christos
371 1.3 christos error = si70xx_cmd1(sc, SI70XX_WRITE_HEATER_REG, &heaterregister, 1);
372 1.1 christos if (error) {
373 1.1 christos DPRINTF(sc, 2, ("%s: Failed to write heater register: %d\n",
374 1.1 christos device_xname(sc->sc_dev), error));
375 1.1 christos }
376 1.1 christos out:
377 1.1 christos iic_release_bus(sc->sc_tag, 0);
378 1.1 christos return error;
379 1.1 christos }
380 1.1 christos
381 1.1 christos static int
382 1.1 christos si70xx_update_heater(struct si70xx_sc *sc)
383 1.1 christos {
384 1.1 christos size_t i;
385 1.1 christos int error;
386 1.1 christos uint8_t heaterregister;
387 1.1 christos
388 1.1 christos error = si70xx_cmd1(sc, SI70XX_READ_HEATER_REG, &heaterregister, 1);
389 1.1 christos if (error) {
390 1.1 christos DPRINTF(sc, 2, ("%s: Failed to read heater register: %d\n",
391 1.1 christos device_xname(sc->sc_dev), error));
392 1.1 christos return error;
393 1.1 christos }
394 1.1 christos
395 1.1 christos DPRINTF(sc, 2, ("%s: read heater reg values: %02x\n",
396 1.1 christos device_xname(sc->sc_dev), heaterregister));
397 1.1 christos
398 1.1 christos uint8_t heat = heaterregister & SI70XX_HEATER_MASK;
399 1.1 christos for (i = 0; i < __arraycount(si70xx_heatervalues); i++) {
400 1.1 christos if (si70xx_heatervalues[i] == heat)
401 1.1 christos break;
402 1.1 christos }
403 1.1 christos sc->sc_heaterval = i != __arraycount(si70xx_heatervalues) ? i : 0;
404 1.1 christos return 0;
405 1.1 christos }
406 1.1 christos
407 1.1 christos static int
408 1.1 christos si70xx_update_user(struct si70xx_sc *sc)
409 1.1 christos {
410 1.1 christos size_t i;
411 1.1 christos int error;
412 1.1 christos uint8_t userregister;
413 1.1 christos
414 1.1 christos error = si70xx_cmd1(sc, SI70XX_READ_USER_REG_1, &userregister, 1);
415 1.1 christos if (error) {
416 1.1 christos DPRINTF(sc, 2, ("%s: Failed to read user register 1: %d\n",
417 1.1 christos device_xname(sc->sc_dev), error));
418 1.1 christos return error;
419 1.1 christos }
420 1.1 christos DPRINTF(sc, 2, ("%s: read user reg 1 values: %#x\n",
421 1.1 christos device_xname(sc->sc_dev), userregister));
422 1.1 christos
423 1.1 christos uint8_t res = userregister & SI70XX_RESOLUTION_MASK;
424 1.1 christos for (i = 0; i < __arraycount(si70xx_resolutions); i++) {
425 1.1 christos if (si70xx_resolutions[i].num == res)
426 1.1 christos break;
427 1.1 christos }
428 1.1 christos
429 1.1 christos if (i != __arraycount(si70xx_resolutions)) {
430 1.1 christos memcpy(sc->sc_resolution, si70xx_resolutions[i].text,
431 1.1 christos SI70XX_RES_NAME);
432 1.1 christos } else {
433 1.1 christos snprintf(sc->sc_resolution, SI70XX_RES_NAME, "%02x", res);
434 1.1 christos }
435 1.1 christos
436 1.1 christos sc->sc_vddok = (userregister & SI70XX_VDDS_MASK) == 0;
437 1.1 christos sc->sc_heaterval = userregister & SI70XX_HTRE_MASK;
438 1.1 christos return 0;
439 1.1 christos }
440 1.1 christos
441 1.1 christos static int
442 1.1 christos si70xx_update_status(struct si70xx_sc *sc)
443 1.1 christos {
444 1.1 christos int error1 = si70xx_update_user(sc);
445 1.9 brad int error2 = 0;
446 1.9 brad if (! sc->sc_noheater) {
447 1.9 brad error2 = si70xx_update_heater(sc);
448 1.9 brad }
449 1.1 christos return error1 ? error1 : error2;
450 1.1 christos }
451 1.1 christos
452 1.1 christos static uint8_t
453 1.1 christos si70xx_crc(uint8_t * data, size_t size)
454 1.1 christos {
455 1.1 christos uint8_t crc = 0;
456 1.1 christos
457 1.1 christos for (size_t i = 0; i < size; i++) {
458 1.1 christos crc ^= data[i];
459 1.1 christos for (size_t j = 8; j > 0; j--) {
460 1.1 christos if (crc & 0x80)
461 1.1 christos crc = (crc << 1) ^ 0x131;
462 1.1 christos else
463 1.1 christos crc <<= 1;
464 1.1 christos }
465 1.1 christos }
466 1.1 christos return crc;
467 1.1 christos }
468 1.1 christos
469 1.1 christos static int
470 1.1 christos si70xx_poke(i2c_tag_t tag, i2c_addr_t addr, bool matchdebug)
471 1.1 christos {
472 1.1 christos uint8_t reg = SI70XX_READ_USER_REG_1;
473 1.1 christos uint8_t buf;
474 1.1 christos int error;
475 1.1 christos
476 1.1 christos error = si70xx_cmd(tag, addr, ®, 1, &buf, 1);
477 1.1 christos if (matchdebug) {
478 1.1 christos printf("poke X 1: %d\n", error);
479 1.1 christos }
480 1.1 christos return error;
481 1.1 christos }
482 1.1 christos
483 1.1 christos static int
484 1.1 christos si70xx_sysctl_init(struct si70xx_sc *sc)
485 1.1 christos {
486 1.1 christos int error;
487 1.1 christos const struct sysctlnode *cnode;
488 1.1 christos int sysctlroot_num;
489 1.1 christos
490 1.1 christos if ((error = sysctl_createv(&sc->sc_si70xxlog, 0, NULL, &cnode,
491 1.1 christos 0, CTLTYPE_NODE, device_xname(sc->sc_dev),
492 1.1 christos SYSCTL_DESCR("si70xx controls"), NULL, 0, NULL, 0, CTL_HW,
493 1.1 christos CTL_CREATE, CTL_EOL)) != 0)
494 1.1 christos return error;
495 1.1 christos
496 1.1 christos sysctlroot_num = cnode->sysctl_num;
497 1.1 christos
498 1.1 christos #ifdef SI70XX_DEBUG
499 1.1 christos if ((error = sysctl_createv(&sc->sc_si70xxlog, 0, NULL, &cnode,
500 1.1 christos CTLFLAG_READWRITE, CTLTYPE_INT, "debug",
501 1.1 christos SYSCTL_DESCR("Debug level"), si70xx_verify_sysctl, 0,
502 1.1 christos &sc->sc_si70xxdebug, 0, CTL_HW, sysctlroot_num, CTL_CREATE,
503 1.1 christos CTL_EOL)) != 0)
504 1.1 christos return error;
505 1.1 christos
506 1.1 christos #endif
507 1.1 christos
508 1.1 christos #ifdef HAVE_I2C_EXECV
509 1.1 christos if ((error = sysctl_createv(&sc->sc_si70xxlog, 0, NULL, &cnode,
510 1.1 christos CTLFLAG_READWRITE, CTLTYPE_INT, "clockstretch",
511 1.1 christos SYSCTL_DESCR("Clockstretch value"), si70xx_verify_sysctl, 0,
512 1.1 christos &sc->sc_clockstretch, 0, CTL_HW, sysctlroot_num, CTL_CREATE,
513 1.1 christos CTL_EOL)) != 0)
514 1.1 christos return error;
515 1.1 christos #endif
516 1.1 christos
517 1.1 christos
518 1.1 christos if ((error = sysctl_createv(&sc->sc_si70xxlog, 0, NULL, &cnode,
519 1.1 christos CTLFLAG_READWRITE, CTLTYPE_INT, "readattempts",
520 1.1 christos SYSCTL_DESCR("The number of times to attempt to read the values"),
521 1.1 christos si70xx_verify_sysctl, 0, &sc->sc_readattempts, 0, CTL_HW,
522 1.1 christos sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
523 1.1 christos return error;
524 1.1 christos
525 1.1 christos
526 1.1 christos if ((error = sysctl_createv(&sc->sc_si70xxlog, 0, NULL, &cnode,
527 1.1 christos CTLFLAG_READONLY, CTLTYPE_STRING, "resolutions",
528 1.1 christos SYSCTL_DESCR("Valid resolutions"), 0, 0,
529 1.1 christos __UNCONST(si70xx_resolution_names),
530 1.1 christos sizeof(si70xx_resolution_names) + 1,
531 1.1 christos CTL_HW, sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
532 1.1 christos return error;
533 1.1 christos
534 1.1 christos if ((error = sysctl_createv(&sc->sc_si70xxlog, 0, NULL, &cnode,
535 1.1 christos CTLFLAG_READWRITE, CTLTYPE_STRING, "resolution",
536 1.1 christos SYSCTL_DESCR("Resolution of RH and Temp"),
537 1.1 christos si70xx_verify_sysctl_resolution, 0, (void *) sc,
538 1.1 christos SI70XX_RES_NAME, CTL_HW, sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
539 1.1 christos return error;
540 1.1 christos
541 1.1 christos if ((error = sysctl_createv(&sc->sc_si70xxlog, 0, NULL, &cnode,
542 1.1 christos CTLFLAG_READWRITE, CTLTYPE_BOOL, "ignorecrc",
543 1.1 christos SYSCTL_DESCR("Ignore the CRC byte"), NULL, 0, &sc->sc_ignorecrc,
544 1.1 christos 0, CTL_HW, sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
545 1.1 christos return error;
546 1.1 christos
547 1.1 christos if ((error = sysctl_createv(&sc->sc_si70xxlog, 0, NULL, &cnode,
548 1.1 christos CTLFLAG_READONLY, CTLTYPE_BOOL, "vddok",
549 1.1 christos SYSCTL_DESCR("Vdd at least 1.9v"), NULL, 0, &sc->sc_vddok, 0,
550 1.1 christos CTL_HW, sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
551 1.1 christos return error;
552 1.1 christos
553 1.9 brad if (! sc->sc_noheater) {
554 1.9 brad if ((error = sysctl_createv(&sc->sc_si70xxlog, 0, NULL, &cnode,
555 1.9 brad CTLFLAG_READWRITE, CTLTYPE_BOOL, "heateron",
556 1.9 brad SYSCTL_DESCR("Heater on"), si70xx_verify_sysctl_heateron, 0,
557 1.9 brad (void *)sc, 0, CTL_HW, sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
558 1.9 brad return error;
559 1.9 brad
560 1.9 brad if ((error = sysctl_createv(&sc->sc_si70xxlog, 0, NULL, &cnode,
561 1.9 brad CTLFLAG_READWRITE, CTLTYPE_INT, "heaterstrength",
562 1.9 brad SYSCTL_DESCR("Heater strength 1 to 6"),
563 1.9 brad si70xx_verify_sysctl_heatervalue, 0, (void *)sc, 0, CTL_HW,
564 1.9 brad sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
565 1.9 brad return error;
566 1.9 brad }
567 1.1 christos
568 1.9 brad return 0;
569 1.1 christos }
570 1.1 christos
571 1.1 christos static int
572 1.1 christos si70xx_match(device_t parent, cfdata_t match, void *aux)
573 1.1 christos {
574 1.4 thorpej struct i2c_attach_args *ia = aux;
575 1.4 thorpej int error, match_result;
576 1.1 christos const bool matchdebug = false;
577 1.1 christos
578 1.4 thorpej if (iic_use_direct_match(ia, match, NULL, &match_result))
579 1.4 thorpej return match_result;
580 1.1 christos
581 1.4 thorpej /* indirect config - check for configured address */
582 1.4 thorpej if (ia->ia_addr != SI70XX_TYPICAL_ADDR)
583 1.4 thorpej return 0;
584 1.1 christos
585 1.1 christos /*
586 1.1 christos * Check to see if something is really at this i2c address. This will
587 1.1 christos * keep phantom devices from appearing
588 1.1 christos */
589 1.1 christos if (iic_acquire_bus(ia->ia_tag, 0) != 0) {
590 1.1 christos if (matchdebug)
591 1.1 christos printf("in match acquire bus failed\n");
592 1.1 christos return 0;
593 1.1 christos }
594 1.1 christos
595 1.1 christos error = si70xx_poke(ia->ia_tag, ia->ia_addr, matchdebug);
596 1.1 christos iic_release_bus(ia->ia_tag, 0);
597 1.1 christos
598 1.4 thorpej return error == 0 ? I2C_MATCH_ADDRESS_AND_PROBE : 0;
599 1.1 christos }
600 1.1 christos
601 1.1 christos static void
602 1.1 christos si70xx_attach(device_t parent, device_t self, void *aux)
603 1.1 christos {
604 1.1 christos struct si70xx_sc *sc;
605 1.1 christos struct i2c_attach_args *ia;
606 1.1 christos int error, i;
607 1.1 christos int ecount = 0;
608 1.1 christos uint8_t buf[8];
609 1.1 christos uint8_t testcrcpt1[4];
610 1.1 christos uint8_t testcrcpt2[4];
611 1.1 christos uint8_t crc1 = 0, crc2 = 0;
612 1.1 christos uint8_t readcrc1 = 0, readcrc2 = 0;
613 1.9 brad uint8_t fwversion = 0, model, heaterregister;
614 1.1 christos
615 1.1 christos ia = aux;
616 1.1 christos sc = device_private(self);
617 1.1 christos
618 1.1 christos sc->sc_dev = self;
619 1.1 christos sc->sc_tag = ia->ia_tag;
620 1.1 christos sc->sc_addr = ia->ia_addr;
621 1.1 christos sc->sc_si70xxdebug = 0;
622 1.1 christos #ifdef HAVE_I2C_EXECV
623 1.1 christos sc->sc_clockstretch = 2048;
624 1.1 christos #endif
625 1.3 christos sc->sc_readattempts = 25;
626 1.1 christos sc->sc_ignorecrc = false;
627 1.1 christos sc->sc_sme = NULL;
628 1.9 brad sc->sc_noheater = false;
629 1.9 brad sc->sc_nofw = false;
630 1.1 christos
631 1.1 christos aprint_normal("\n");
632 1.1 christos
633 1.1 christos mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE);
634 1.1 christos sc->sc_numsensors = __arraycount(si70xx_sensors);
635 1.1 christos
636 1.1 christos if ((sc->sc_sme = sysmon_envsys_create()) == NULL) {
637 1.1 christos aprint_error_dev(self,
638 1.1 christos "Unable to create sysmon structure\n");
639 1.1 christos sc->sc_sme = NULL;
640 1.1 christos return;
641 1.1 christos }
642 1.1 christos
643 1.1 christos error = iic_acquire_bus(sc->sc_tag, 0);
644 1.1 christos if (error) {
645 1.1 christos aprint_error_dev(self, "Could not acquire iic bus: %d\n",
646 1.1 christos error);
647 1.1 christos goto out;
648 1.1 christos }
649 1.1 christos error = si70xx_cmd1(sc, SI70XX_RESET, NULL, 0);
650 1.1 christos if (error != 0)
651 1.1 christos aprint_error_dev(self, "Reset failed: %d\n", error);
652 1.1 christos
653 1.1 christos delay(15000); /* 15 ms max */
654 1.1 christos
655 1.1 christos error = si70xx_cmd2(sc, SI70XX_READ_ID_PT1A, SI70XX_READ_ID_PT1B,
656 1.1 christos buf, 8);
657 1.1 christos if (error) {
658 1.1 christos aprint_error_dev(self, "Failed to read first part of ID: %d\n",
659 1.1 christos error);
660 1.1 christos ecount++;
661 1.1 christos }
662 1.1 christos testcrcpt1[0] = buf[0];
663 1.1 christos testcrcpt1[1] = buf[2];
664 1.1 christos testcrcpt1[2] = buf[4];
665 1.1 christos testcrcpt1[3] = buf[6];
666 1.1 christos readcrc1 = buf[7];
667 1.1 christos crc1 = si70xx_crc(testcrcpt1, 4);
668 1.1 christos
669 1.1 christos DPRINTF(sc, 2, ("%s: read 1 values: %02x%02x%02x%02x%02x%02x%02x%02x "
670 1.1 christos "- %02x\n", device_xname(sc->sc_dev), buf[0], buf[1],
671 1.1 christos buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
672 1.1 christos crc1));
673 1.1 christos
674 1.1 christos error = si70xx_cmd2(sc, SI70XX_READ_ID_PT2A, SI70XX_READ_ID_PT2B,
675 1.1 christos buf, 8);
676 1.1 christos if (error != 0) {
677 1.1 christos aprint_error_dev(self, "Failed to read second part of ID: %d\n",
678 1.1 christos error);
679 1.1 christos ecount++;
680 1.1 christos }
681 1.1 christos model = testcrcpt2[0] = buf[0];
682 1.1 christos testcrcpt2[1] = buf[1];
683 1.1 christos testcrcpt2[2] = buf[3];
684 1.1 christos testcrcpt2[3] = buf[4];
685 1.1 christos readcrc2 = buf[5];
686 1.1 christos crc2 = si70xx_crc(testcrcpt2, 4);
687 1.1 christos
688 1.1 christos DPRINTF(sc, 2, ("%s: read 2 values: %02x%02x%02x%02x%02x%02x - %02x\n",
689 1.1 christos device_xname(sc->sc_dev), buf[0], buf[1], buf[2],
690 1.1 christos buf[3], buf[4], buf[5], crc2));
691 1.1 christos
692 1.1 christos error = si70xx_cmd2(sc, SI70XX_READ_FW_VERA, SI70XX_READ_FW_VERB,
693 1.1 christos buf, 8);
694 1.1 christos
695 1.1 christos if (error) {
696 1.8 andvar aprint_error_dev(self, "Failed to read firmware version: %d\n",
697 1.1 christos error);
698 1.9 brad sc->sc_nofw = true;
699 1.9 brad }
700 1.9 brad if (! sc->sc_nofw) {
701 1.9 brad fwversion = buf[0];
702 1.9 brad DPRINTF(sc, 2, ("%s: read fw values: %#x\n", device_xname(sc->sc_dev),
703 1.9 brad fwversion));
704 1.9 brad }
705 1.9 brad
706 1.9 brad error = si70xx_cmd1(sc, SI70XX_READ_HEATER_REG, &heaterregister, 1);
707 1.9 brad
708 1.9 brad if (error) {
709 1.9 brad aprint_error_dev(self, "Failed to read heater register: %d\n",
710 1.9 brad error);
711 1.9 brad sc->sc_noheater = true;
712 1.1 christos }
713 1.1 christos
714 1.1 christos error = si70xx_update_status(sc);
715 1.9 brad
716 1.1 christos iic_release_bus(sc->sc_tag, 0);
717 1.9 brad
718 1.9 brad if ((error = si70xx_sysctl_init(sc)) != 0) {
719 1.9 brad aprint_error_dev(self, "Can't setup sysctl tree (%d)\n", error);
720 1.9 brad goto out;
721 1.9 brad }
722 1.9 brad
723 1.1 christos if (error != 0) {
724 1.1 christos aprint_error_dev(self, "Failed to update status: %x\n", error);
725 1.1 christos aprint_error_dev(self, "Unable to setup device\n");
726 1.1 christos goto out;
727 1.1 christos }
728 1.1 christos
729 1.1 christos for (i = 0; i < sc->sc_numsensors; i++) {
730 1.1 christos strlcpy(sc->sc_sensors[i].desc, si70xx_sensors[i].desc,
731 1.1 christos sizeof(sc->sc_sensors[i].desc));
732 1.1 christos
733 1.1 christos sc->sc_sensors[i].units = si70xx_sensors[i].type;
734 1.1 christos sc->sc_sensors[i].state = ENVSYS_SINVALID;
735 1.1 christos
736 1.1 christos DPRINTF(sc, 2, ("%s: registering sensor %d (%s)\n", __func__, i,
737 1.1 christos sc->sc_sensors[i].desc));
738 1.1 christos
739 1.1 christos error = sysmon_envsys_sensor_attach(sc->sc_sme,
740 1.1 christos &sc->sc_sensors[i]);
741 1.1 christos if (error) {
742 1.1 christos aprint_error_dev(self,
743 1.1 christos "Unable to attach sensor %d: %d\n", i, error);
744 1.6 jdc sc->sc_sme = NULL;
745 1.1 christos goto out;
746 1.1 christos }
747 1.1 christos }
748 1.1 christos
749 1.1 christos sc->sc_sme->sme_name = device_xname(sc->sc_dev);
750 1.1 christos sc->sc_sme->sme_cookie = sc;
751 1.1 christos sc->sc_sme->sme_refresh = si70xx_refresh;
752 1.1 christos
753 1.1 christos DPRINTF(sc, 2, ("si70xx_attach: registering with envsys\n"));
754 1.1 christos
755 1.1 christos if (sysmon_envsys_register(sc->sc_sme)) {
756 1.1 christos aprint_error_dev(self,
757 1.1 christos "unable to register with sysmon\n");
758 1.1 christos sysmon_envsys_destroy(sc->sc_sme);
759 1.1 christos sc->sc_sme = NULL;
760 1.1 christos return;
761 1.1 christos }
762 1.1 christos if (ecount != 0) {
763 1.1 christos aprint_normal_dev(self, "Could not read model, "
764 1.1 christos "probably an HTU21D\n");
765 1.1 christos return;
766 1.1 christos }
767 1.1 christos
768 1.1 christos char modelstr[64];
769 1.1 christos switch (model) {
770 1.1 christos case 0:
771 1.1 christos case 0xff:
772 1.1 christos snprintf(modelstr, sizeof(modelstr), "Engineering Sample");
773 1.5 mrg break;
774 1.1 christos case 13:
775 1.1 christos case 20:
776 1.1 christos case 21:
777 1.1 christos snprintf(modelstr, sizeof(modelstr), "SI70%d", model);
778 1.1 christos break;
779 1.1 christos default:
780 1.9 brad snprintf(modelstr, sizeof(modelstr), "Unknown model %d (maybe an HTU21D)", model);
781 1.1 christos break;
782 1.1 christos }
783 1.1 christos
784 1.1 christos const char *fwversionstr;
785 1.1 christos switch (fwversion) {
786 1.1 christos case 0xff:
787 1.1 christos fwversionstr = "1.0";
788 1.1 christos break;
789 1.1 christos case 0x20:
790 1.1 christos fwversionstr = "2.0";
791 1.1 christos break;
792 1.1 christos default:
793 1.1 christos fwversionstr = "unknown";
794 1.1 christos break;
795 1.1 christos }
796 1.1 christos
797 1.1 christos aprint_normal_dev(self, "Silicon Labs Model: %s, "
798 1.1 christos "Firmware version: %s, "
799 1.1 christos "Serial number: %02x%02x%02x%02x%02x%02x%02x%02x%s",
800 1.1 christos modelstr, fwversionstr, testcrcpt1[0], testcrcpt1[1],
801 1.1 christos testcrcpt1[2], testcrcpt1[3], testcrcpt2[0], testcrcpt2[1],
802 1.1 christos testcrcpt2[2], testcrcpt2[3],
803 1.1 christos (crc1 == readcrc1 && crc2 == readcrc2) ? "\n" : " (bad crc)\n");
804 1.1 christos return;
805 1.1 christos out:
806 1.1 christos sysmon_envsys_destroy(sc->sc_sme);
807 1.1 christos sc->sc_sme = NULL;
808 1.1 christos }
809 1.1 christos
810 1.1 christos static int
811 1.1 christos si70xx_exec(struct si70xx_sc *sc, uint8_t cmd, envsys_data_t *edata)
812 1.1 christos {
813 1.1 christos int error;
814 1.1 christos int xdelay;
815 1.1 christos const char *name;
816 1.1 christos int64_t mul, offs;
817 1.1 christos uint8_t buf[3];
818 1.1 christos
819 1.1 christos switch (cmd) {
820 1.1 christos case SI70XX_MEASURE_RH_NOHOLD:
821 1.1 christos /*
822 1.1 christos * The published conversion for RH is: %RH =
823 1.1 christos * ((125 * RHCODE) / 65536) - 6
824 1.1 christos *
825 1.1 christos * The sysmon infrastructure for RH wants %RH *
826 1.1 christos * 10^6 The result will fit in 32 bits, but
827 1.1 christos * the intermediate values will not.
828 1.1 christos */
829 1.1 christos mul = 125000000;
830 1.1 christos offs = -6000000;
831 1.1 christos /*
832 1.1 christos * Conversion times for %RH in ms
833 1.1 christos *
834 1.1 christos * Typical Max
835 1.1 christos * 12-bit 10.0 12.0
836 1.1 christos * 11-bit 5.8 7.0
837 1.1 christos * 10-bit 3.7 4.5
838 1.1 christos * 8-bit 2.6 3.1
839 1.1 christos *
840 1.1 christos * A call to read %RH will also read temperature. The
841 1.1 christos * conversion time will be the amount of time above
842 1.1 christos * plus the amount of time for temperature below
843 1.1 christos */
844 1.1 christos xdelay = 10500;
845 1.1 christos name = "RH";
846 1.1 christos break;
847 1.1 christos case SI70XX_MEASURE_TEMP_NOHOLD:
848 1.1 christos /*
849 1.1 christos * The published conversion for temp is:
850 1.1 christos * degree C = ((175.72 * TEMPCODE) / 65536) -
851 1.1 christos * 46.85
852 1.1 christos *
853 1.1 christos * The sysmon infrastructure for temp wants
854 1.1 christos * microkelvin. This is simple, as degree C
855 1.1 christos * converts directly with K with simple
856 1.1 christos * addition. The result will fit in 32 bits,
857 1.1 christos * but the intermediate values will not.
858 1.1 christos */
859 1.1 christos mul = 175720000;
860 1.1 christos offs = 226300000;
861 1.1 christos /*
862 1.1 christos * Conversion times for temperature in ms
863 1.1 christos *
864 1.1 christos * Typical Max
865 1.1 christos * 14-bit 7.0 10.8
866 1.1 christos * 13-bit 4.0 6.2
867 1.1 christos * 12-bit 2.4 3.8
868 1.1 christos * 11-bit 1.5 2.4
869 1.1 christos */
870 1.1 christos xdelay = 4750;
871 1.1 christos name = "TEMP";
872 1.1 christos break;
873 1.1 christos default:
874 1.1 christos return EINVAL;
875 1.1 christos }
876 1.1 christos
877 1.1 christos #if HAVE_I2C_EXECV
878 1.1 christos memset(buf, 0, sizeof(buf));
879 1.1 christos error = iic_execv(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
880 1.1 christos &cmd, 1, buf, sizeof(buf), 0, I2C_ATTR_CLOCKSTRETCH,
881 1.1 christos sc->sc_clockstretch, I2C_ATTR_EOL);
882 1.1 christos #else
883 1.1 christos /*
884 1.1 christos * The lower level driver must support the ability to
885 1.1 christos * do a zero length read, otherwise this breaks
886 1.1 christos */
887 1.1 christos error = si70xx_cmd1(sc, cmd, buf, 0);
888 1.1 christos if (error) {
889 1.1 christos DPRINTF(sc, 2, ("%s: Failed to read NO HOLD %s %d %d\n",
890 1.1 christos device_xname(sc->sc_dev), name, 1, error));
891 1.1 christos return error;
892 1.1 christos }
893 1.1 christos
894 1.1 christos /*
895 1.1 christos * It will probably be at least this long... we would
896 1.1 christos * not have to do this sort of thing if clock
897 1.1 christos * stretching worked. Even this is a problem for the
898 1.1 christos * RPI without a patch to remove a [apparently] not
899 1.1 christos * needed KASSERT()
900 1.1 christos */
901 1.1 christos delay(xdelay);
902 1.1 christos
903 1.1 christos for (int aint = 0; aint < sc->sc_readattempts; aint++) {
904 1.1 christos error = si70xx_cmd0(sc, buf, sizeof(buf));
905 1.1 christos if (error == 0)
906 1.1 christos break;
907 1.1 christos DPRINTF(sc, 2, ("%s: Failed to read NO HOLD RH"
908 1.1 christos " %d %d\n", device_xname(sc->sc_dev), 2, error));
909 1.3 christos delay(1000);
910 1.1 christos }
911 1.1 christos #endif
912 1.1 christos
913 1.1 christos DPRINTF(sc, 2, ("%s: %s values: %02x%02x%02x - %02x\n",
914 1.1 christos device_xname(sc->sc_dev), name, buf[0], buf[1], buf[2],
915 1.1 christos si70xx_crc(buf, 2)));
916 1.1 christos
917 1.1 christos uint8_t crc;
918 1.1 christos if (sc->sc_ignorecrc) {
919 1.1 christos crc = buf[2];
920 1.1 christos } else {
921 1.1 christos crc = si70xx_crc(buf, 2);
922 1.1 christos }
923 1.1 christos
924 1.1 christos if (crc != buf[2]) {
925 1.1 christos DPRINTF(sc, 2, ("%s: Bad CRC for %s: %#x and %#x\n",
926 1.1 christos device_xname(sc->sc_dev), name, crc, buf[2]));
927 1.1 christos return EINVAL;
928 1.1 christos }
929 1.1 christos
930 1.1 christos uint16_t val16 = (buf[0] << 8) | buf[1];
931 1.1 christos uint64_t val64 = ((mul * val16) >> 16) + offs;
932 1.1 christos DPRINTF(sc, 2, ("%s: %s calculated values: %x %#jx\n",
933 1.1 christos device_xname(sc->sc_dev), name, val16, (uintmax_t)val64));
934 1.1 christos edata->value_cur = (uint32_t) val64;
935 1.1 christos edata->state = ENVSYS_SVALID;
936 1.1 christos return 0;
937 1.1 christos }
938 1.1 christos
939 1.1 christos static void
940 1.1 christos si70xx_refresh(struct sysmon_envsys * sme, envsys_data_t * edata)
941 1.1 christos {
942 1.1 christos struct si70xx_sc *sc;
943 1.1 christos int error;
944 1.1 christos
945 1.1 christos sc = sme->sme_cookie;
946 1.1 christos edata->state = ENVSYS_SINVALID;
947 1.1 christos
948 1.1 christos mutex_enter(&sc->sc_mutex);
949 1.1 christos error = iic_acquire_bus(sc->sc_tag, 0);
950 1.1 christos if (error) {
951 1.1 christos DPRINTF(sc, 2, ("%s: Could not acquire i2c bus: %x\n",
952 1.1 christos device_xname(sc->sc_dev), error));
953 1.1 christos goto out;
954 1.1 christos }
955 1.1 christos error = si70xx_update_status(sc);
956 1.1 christos if (error) {
957 1.1 christos DPRINTF(sc, 2, ("%s: Failed to update status in refresh %d\n",
958 1.1 christos device_xname(sc->sc_dev), error));
959 1.1 christos goto out1;
960 1.1 christos }
961 1.1 christos switch (edata->sensor) {
962 1.1 christos case SI70XX_HUMIDITY_SENSOR:
963 1.3 christos error = si70xx_exec(sc, SI70XX_MEASURE_RH_NOHOLD, edata);
964 1.1 christos break;
965 1.1 christos
966 1.1 christos case SI70XX_TEMP_SENSOR:
967 1.3 christos error = si70xx_exec(sc, SI70XX_MEASURE_TEMP_NOHOLD, edata);
968 1.1 christos break;
969 1.1 christos default:
970 1.1 christos error = EINVAL;
971 1.1 christos break;
972 1.1 christos }
973 1.1 christos
974 1.1 christos if (error) {
975 1.1 christos DPRINTF(sc, 2, ("%s: Failed to get new status in refresh %d\n",
976 1.1 christos device_xname(sc->sc_dev), error));
977 1.1 christos }
978 1.1 christos out1:
979 1.1 christos iic_release_bus(sc->sc_tag, 0);
980 1.1 christos out:
981 1.1 christos mutex_exit(&sc->sc_mutex);
982 1.1 christos }
983 1.1 christos
984 1.1 christos static int
985 1.1 christos si70xx_detach(device_t self, int flags)
986 1.1 christos {
987 1.1 christos struct si70xx_sc *sc;
988 1.1 christos
989 1.1 christos sc = device_private(self);
990 1.1 christos
991 1.1 christos mutex_enter(&sc->sc_mutex);
992 1.1 christos
993 1.1 christos /* Remove the sensors */
994 1.7 mlelstv if (sc->sc_sme != NULL)
995 1.1 christos sysmon_envsys_unregister(sc->sc_sme);
996 1.1 christos mutex_exit(&sc->sc_mutex);
997 1.1 christos
998 1.1 christos /* Remove the sysctl tree */
999 1.1 christos sysctl_teardown(&sc->sc_si70xxlog);
1000 1.1 christos
1001 1.1 christos /* Remove the mutex */
1002 1.1 christos mutex_destroy(&sc->sc_mutex);
1003 1.1 christos
1004 1.1 christos return 0;
1005 1.1 christos }
1006 1.1 christos
1007 1.1 christos MODULE(MODULE_CLASS_DRIVER, si70xxtemp, "i2cexec,sysmon_envsys");
1008 1.1 christos
1009 1.1 christos #ifdef _MODULE
1010 1.1 christos #include "ioconf.c"
1011 1.1 christos #endif
1012 1.1 christos
1013 1.1 christos static int
1014 1.1 christos si70xxtemp_modcmd(modcmd_t cmd, void *opaque)
1015 1.1 christos {
1016 1.1 christos
1017 1.1 christos switch (cmd) {
1018 1.1 christos case MODULE_CMD_INIT:
1019 1.1 christos #ifdef _MODULE
1020 1.1 christos return config_init_component(cfdriver_ioconf_si70xxtemp,
1021 1.1 christos cfattach_ioconf_si70xxtemp, cfdata_ioconf_si70xxtemp);
1022 1.1 christos #else
1023 1.1 christos return 0;
1024 1.1 christos #endif
1025 1.1 christos case MODULE_CMD_FINI:
1026 1.1 christos #ifdef _MODULE
1027 1.2 christos return config_fini_component(cfdriver_ioconf_si70xxtemp,
1028 1.1 christos cfattach_ioconf_si70xxtemp, cfdata_ioconf_si70xxtemp);
1029 1.1 christos #else
1030 1.1 christos return 0;
1031 1.1 christos #endif
1032 1.1 christos default:
1033 1.1 christos return ENOTTY;
1034 1.1 christos }
1035 1.1 christos }
1036