am2315.c revision 1.3 1 /* $NetBSD: am2315.c,v 1.3 2018/06/16 21:22:13 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2017 Brad Spencer <brad (at) anduin.eldar.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <sys/cdefs.h>
20 __KERNEL_RCSID(0, "$NetBSD: am2315.c,v 1.3 2018/06/16 21:22:13 thorpej Exp $");
21
22 /*
23 * Driver for the Aosong AM2315
24 */
25
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/kernel.h>
29 #include <sys/device.h>
30 #include <sys/module.h>
31 #include <sys/sysctl.h>
32 #include <sys/condvar.h>
33 #include <sys/mutex.h>
34 #include <sys/time.h>
35
36 #include <dev/sysmon/sysmonvar.h>
37 #include <dev/i2c/i2cvar.h>
38 #include <dev/i2c/am2315reg.h>
39 #include <dev/i2c/am2315var.h>
40
41 static uint16_t am2315_crc(uint8_t *, size_t);
42 static int am2315_poke(struct am2315_sc *);
43 static int am2315_poke_m(i2c_tag_t, i2c_addr_t, const char *, bool);
44 static int am2315_match(device_t, cfdata_t, void *);
45 static void am2315_attach(device_t, device_t, void *);
46 static int am2315_detach(device_t, int);
47 static void am2315_refresh(struct sysmon_envsys *, envsys_data_t *);
48 static int am2315_verify_sysctl(SYSCTLFN_ARGS);
49
50 #define AM2315_DEBUG
51 #ifdef AM2315_DEBUG
52 #define DPRINTF(s, l, x) \
53 do { \
54 if (l <= s->sc_am2315debug) \
55 printf x; \
56 } while (/*CONSTCOND*/0)
57 #else
58 #define DPRINTF(s, l, x)
59 #endif
60
61 CFATTACH_DECL_NEW(am2315temp, sizeof(struct am2315_sc),
62 am2315_match, am2315_attach, am2315_detach, NULL);
63
64 static struct am2315_sensor am2315_sensors[] = {
65 {
66 .desc = "humidity",
67 .type = ENVSYS_SRELHUMIDITY,
68 },
69 {
70 .desc = "temperature",
71 .type = ENVSYS_STEMP,
72 }
73 };
74
75 static uint16_t
76 am2315_crc(uint8_t *data, size_t len)
77 {
78 uint16_t crc = 0xffff;
79
80 for (size_t j = 0; j < len; j++) {
81 crc ^= data[j];
82 for (size_t i = 0; i < 8; i++) {
83 if (crc & 0x01) {
84 crc >>= 1;
85 crc ^= 0xA001;
86 } else {
87 crc >>= 1;
88 }
89 }
90 }
91
92 return crc;
93 }
94
95 int
96 am2315_verify_sysctl(SYSCTLFN_ARGS)
97 {
98 int error, t;
99 struct sysctlnode node;
100
101 node = *rnode;
102 t = *(int *)rnode->sysctl_data;
103 node.sysctl_data = &t;
104 error = sysctl_lookup(SYSCTLFN_CALL(&node));
105 if (error || newp == NULL)
106 return error;
107
108 if (t < 0)
109 return EINVAL;
110
111 *(int *) rnode->sysctl_data = t;
112
113 return 0;
114 }
115
116 static int
117 am2315_cmd(i2c_tag_t tag, i2c_addr_t addr, uint8_t dir, uint8_t cmd,
118 uint8_t clen, uint8_t *buf, size_t blen)
119 {
120 uint8_t command[] = { dir, cmd, clen };
121 if (buf)
122 memset(buf, 0xff, blen);
123 uint8_t reg = dir == AM2315_READ_REGISTERS ?
124 I2C_OP_READ_WITH_STOP : I2C_OP_WRITE_WITH_STOP;
125
126 return iic_exec(tag, reg, addr, command,
127 __arraycount(command), buf, blen, 0);
128 }
129
130 static int
131 am2315_read_regs(struct am2315_sc *sc, uint8_t cmd, uint8_t clen, uint8_t *buf,
132 size_t blen)
133 {
134 return am2315_cmd(sc->sc_tag, sc->sc_addr, AM2315_READ_REGISTERS,
135 cmd, clen, buf, blen);
136 }
137
138 static int
139 am2315_poke(struct am2315_sc *sc)
140 {
141 return am2315_poke_m(sc->sc_tag, sc->sc_addr, device_xname(sc->sc_dev),
142 sc->sc_am2315debug >= 2);
143 }
144
145 static int
146 am2315_poke_m(i2c_tag_t tag, i2c_addr_t addr, const char *name, bool debug)
147 {
148 uint8_t buf[5];
149 int error;
150
151 error = am2315_cmd(tag, addr, AM2315_WRITE_REGISTERS,
152 AM2315_REGISTER_HIGH_USER1, 1, NULL, 0);
153 if (debug)
154 printf("%s: poke 1: %d\n", name, error);
155
156 if (error)
157 delay(2800);
158
159 error = am2315_cmd(tag, addr, AM2315_READ_REGISTERS,
160 AM2315_REGISTER_STATUS, 1, buf, __arraycount(buf));
161 if (debug)
162 printf("%s: poke 2: %d %02x %02x %02x %02x%02x\n", name, error,
163 buf[0], buf[1], buf[2], buf[3], buf[4]);
164
165 if (error)
166 delay(2800);
167 return error;
168 }
169
170 static int
171 am2315_match(device_t parent, cfdata_t match, void *aux)
172 {
173 struct i2c_attach_args *ia = aux;
174 int rv;
175 const bool matchdebug = false;
176 int match_result;
177
178 if (iic_use_direct_match(ia, match, NULL, &match_result))
179 return match_result;
180
181 /* indirect config - check for standard address */
182 if (ia->ia_addr == AM2315_TYPICAL_ADDR)
183 return I2C_MATCH_ADDRESS_ONLY;
184
185 return 0;
186 }
187
188 static void
189 am2315_attach(device_t parent, device_t self, void *aux)
190 {
191 struct am2315_sc *sc = device_private(self);
192 struct i2c_attach_args *ia = aux;
193 uint8_t buf[11];
194 int error;
195 uint16_t crc, readcrc, model;
196 uint8_t chipver;
197 uint32_t id;
198 bool modelgood, chipvergood, idgood;
199
200 sc->sc_dev = self;
201 sc->sc_tag = ia->ia_tag;
202 sc->sc_addr = ia->ia_addr;
203 sc->sc_am2315debug = 0;
204 sc->sc_readcount = 2;
205 sc->sc_readticks = 100;
206 sc->sc_sme = NULL;
207
208 aprint_normal("\n");
209
210 mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE);
211 mutex_init(&sc->sc_waitmutex, MUTEX_DEFAULT, IPL_NONE);
212 cv_init(&sc->sc_condwait, "am2315wait");
213
214 sc->sc_numsensors = __arraycount(am2315_sensors);
215
216 if ((sc->sc_sme = sysmon_envsys_create()) == NULL) {
217 aprint_error_dev(self, "unable to create sysmon structure\n");
218 return;
219 }
220
221 /* XXX: sysctl's not destroyed on failure */
222 const struct sysctlnode *cnode;
223 int sysctlroot_num;
224 if ((error = sysctl_createv(&sc->sc_am2315log, 0, NULL, &cnode, 0,
225 CTLTYPE_NODE, device_xname(self),
226 SYSCTL_DESCR("am2315 controls"), NULL, 0, NULL, 0, CTL_HW,
227 CTL_CREATE, CTL_EOL)) != 0)
228 goto badsysctl;
229 sysctlroot_num = cnode->sysctl_num;
230
231 #ifdef AM2315_DEBUG
232 if ((error = sysctl_createv(&sc->sc_am2315log, 0, NULL, &cnode,
233 CTLFLAG_READWRITE, CTLTYPE_INT, "debug",
234 SYSCTL_DESCR("Debug level"), am2315_verify_sysctl, 0,
235 &sc->sc_am2315debug, 0, CTL_HW, sysctlroot_num, CTL_CREATE,
236 CTL_EOL)) != 0)
237 goto badsysctl;
238
239 #endif
240
241 if ((error = sysctl_createv(&sc->sc_am2315log, 0, NULL, &cnode,
242 CTLFLAG_READWRITE, CTLTYPE_INT, "readcount",
243 SYSCTL_DESCR("Number of times to read the sensor"),
244 am2315_verify_sysctl, 0, &sc->sc_readcount, 0, CTL_HW,
245 sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
246 goto badsysctl;
247
248 if ((error = sysctl_createv(&sc->sc_am2315log, 0, NULL, &cnode,
249 CTLFLAG_READWRITE, CTLTYPE_INT, "readticks",
250 SYSCTL_DESCR("Number of ticks between reads"),
251 am2315_verify_sysctl, 0, &sc->sc_readticks, 0, CTL_HW,
252 sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
253 goto badsysctl;
254
255 if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
256 aprint_error_dev(self,
257 "Could not acquire iic bus: %d\n", error);
258 return;
259 }
260 am2315_poke(sc);
261
262 #define DUMP(a) \
263 DPRINTF(sc, 2, ("%s: read cmd+len+%s+crcl+crch values: %02x %02x " \
264 "%02x%02x %02x%02x -- %02x%02x%02x%02x%02x -- %04x %04x\n", a, \
265 device_xname(self), buf[0], buf[1], buf[2], buf[3], \
266 buf[4], buf[5], buf[6], buf[7], buf[8], buf[9], \
267 buf[10], crc, readcrc))
268
269 error = am2315_read_regs(sc, AM2315_REGISTER_HIGH_MODEL, 2, buf, 6);
270 if (error)
271 aprint_error_dev(sc->sc_dev, "read model: %d\n", error);
272 readcrc = buf[5] << 8 | buf[4];
273 crc = am2315_crc(buf, 4);
274 DUMP("modh+modl");
275 model = buf[2] << 8 | buf[3];
276 modelgood = buf[0] == AM2315_READ_REGISTERS && buf[1] == 2
277 && crc == readcrc;
278
279 error = am2315_read_regs(sc, AM2315_REGISTER_VERSION, 1, buf, 5);
280 if (error)
281 aprint_error_dev(self, "read chipver: %d\n", error);
282 readcrc = buf[4] << 8 | buf[3];
283 crc = am2315_crc(buf, 3);
284 DUMP("ver");
285 chipver = buf[2];
286 chipvergood = buf[0] == AM2315_READ_REGISTERS && buf[1] == 1
287 && crc == readcrc;
288
289 error = am2315_read_regs(sc, AM2315_REGISTER_ID_PT_24_31, 2, buf, 6);
290 if (error)
291 aprint_error_dev(self, "read id 1: %d\n", error);
292 readcrc = buf[5] << 8 | buf[4];
293 crc = am2315_crc(buf, 4);
294 DUMP("id1+id2");
295 id = buf[2] << 8 | buf[3];
296 idgood = buf[0] == AM2315_READ_REGISTERS && buf[1] == 2
297 && crc == readcrc;
298
299 error = am2315_read_regs(sc, AM2315_REGISTER_ID_PT_8_15, 2, buf, 6);
300 if (error)
301 aprint_error_dev(self, "read id 2: %d\n", error);
302 readcrc = buf[5] << 8 | buf[4];
303 crc = am2315_crc(buf, 4);
304 DUMP("id3+id4");
305 id = id << 8 | buf[2];
306 id = id << 8 | buf[3];
307 idgood = buf[0] == AM2315_READ_REGISTERS && buf[1] == 2
308 && crc == readcrc && idgood;
309
310 iic_release_bus(sc->sc_tag, 0);
311
312 for (int i = 0; i < sc->sc_numsensors; i++) {
313 strlcpy(sc->sc_sensors[i].desc, am2315_sensors[i].desc,
314 sizeof(sc->sc_sensors[i].desc));
315
316 sc->sc_sensors[i].units = am2315_sensors[i].type;
317 sc->sc_sensors[i].state = ENVSYS_SINVALID;
318
319 DPRINTF(sc, 2, ("am2315_attach: registering sensor %d (%s)\n",
320 i, sc->sc_sensors[i].desc));
321
322 error = sysmon_envsys_sensor_attach(sc->sc_sme,
323 &sc->sc_sensors[i]);
324 if (error) {
325 aprint_error_dev(self, "unable to attach sensor %d\n",
326 error);
327 goto badregister;
328 }
329 }
330
331 sc->sc_sme->sme_name = device_xname(sc->sc_dev);
332 sc->sc_sme->sme_cookie = sc;
333 sc->sc_sme->sme_refresh = am2315_refresh;
334
335 DPRINTF(sc, 2, ("am2315_attach: registering with envsys\n"));
336
337 error = sysmon_envsys_register(sc->sc_sme);
338 if (error) {
339 aprint_error_dev(self, "unable to register with sysmon %d\n",
340 error);
341 goto badregister;
342 }
343 aprint_normal_dev(self, "Aosong AM2315, Model: %04x%s Version: %02x%s"
344 " ID: %08x%s",
345 model, (modelgood ? "," : "(inaccurate),"),
346 chipver, (chipvergood ? "," : "(inaccurate),"),
347 id, (idgood ? "\n" : "(inaccurate)\n"));
348 return;
349
350 badsysctl:
351 aprint_error_dev(self, ": can't setup sysctl tree (%d)\n", error);
352 return;
353 badregister:
354 sysmon_envsys_destroy(sc->sc_sme);
355 sc->sc_sme = NULL;
356 }
357
358 static void
359 am2315_refresh(struct sysmon_envsys * sme, envsys_data_t * edata)
360 {
361 struct am2315_sc *sc;
362 uint8_t buf[11], thecommand;
363 uint16_t crc, readcrc;
364 int error, rv;
365 uint32_t val32;
366 bool istempneg = false;
367
368 sc = sme->sme_cookie;
369 edata->state = ENVSYS_SINVALID;
370
371 mutex_enter(&sc->sc_mutex);
372 error = iic_acquire_bus(sc->sc_tag, 0);
373 if (error) {
374 DPRINTF(sc, 2, ("%s: Could not acquire i2c bus: %d\n",
375 device_xname(sc->sc_dev), error));
376 goto out;
377 }
378
379 switch (edata->sensor) {
380 case AM2315_HUMIDITY_SENSOR:
381 thecommand = AM2315_REGISTER_HIGH_RH;
382 break;
383 case AM2315_TEMP_SENSOR:
384 thecommand = AM2315_REGISTER_HIGH_TEMP;
385 break;
386 default:
387 DPRINTF(sc, 2, ("%s: bad sensor %d\n",
388 device_xname(sc->sc_dev), edata->sensor));
389 goto out;
390 }
391
392 for (int count = 0; ;) {
393 am2315_poke(sc);
394
395 if ((error = am2315_read_regs(sc, thecommand, 2, buf, 6)) != 0)
396 DPRINTF(sc, 2, ("%s: Read sensor %d error: %d\n",
397 device_xname(sc->sc_dev),edata->sensor, error));
398
399 readcrc = buf[5] << 8 | buf[4];
400 crc = am2315_crc(buf, 4);
401
402 DPRINTF(sc, 2, ("%s: read cmd+len+dh+dl+crch+crcl values: %02x"
403 " %02x %02x%02x %02x%02x -- %04x %04x -- %d\n",
404 device_xname(sc->sc_dev), buf[0], buf[1], buf[2],
405 buf[3], buf[4], buf[5], crc, readcrc, count));
406 if (++count == sc->sc_readcount)
407 break;
408 mutex_enter(&sc->sc_waitmutex);
409 rv = cv_timedwait(&sc->sc_condwait, &sc->sc_waitmutex,
410 sc->sc_readticks);
411 DPRINTF(sc, 2, ("%s: wait rv: %d\n", device_xname(sc->sc_dev),
412 rv));
413 mutex_exit(&sc->sc_waitmutex);
414 }
415
416 iic_release_bus(sc->sc_tag, 0);
417
418 if (buf[0] != AM2315_READ_REGISTERS || buf[1] != 2 ||
419 crc != readcrc) {
420 DPRINTF(sc, 2, ("%s: Invalid sensor data for %d\n",
421 device_xname(sc->sc_dev), edata->sensor));
422 goto out;
423 }
424
425 switch (edata->sensor) {
426 case AM2315_HUMIDITY_SENSOR:
427 val32 = buf[2] << 8 | buf[3];
428 val32 = val32 * 100000;
429 DPRINTF(sc, 2, ("%s: read translated values RH: %x\n",
430 device_xname(sc->sc_dev), val32));
431 edata->value_cur = val32;
432 edata->state = ENVSYS_SVALID;
433 break;
434 case AM2315_TEMP_SENSOR:
435 istempneg = (buf[2] & AM2315_TEMP_NEGATIVE);
436 buf[2] = buf[2] & (~AM2315_TEMP_NEGATIVE);
437 val32 = buf[2] << 8 | buf[3];
438 if (istempneg) {
439 val32 = 273150000 - (val32 * 100000);
440 } else {
441 val32 = (val32 * 100000) + 273150000;
442 }
443 DPRINTF(sc, 2, ("%s: read translated values TEMP: %x\n",
444 device_xname(sc->sc_dev), val32));
445 edata->value_cur = val32;
446 edata->state = ENVSYS_SVALID;
447 break;
448 default:
449 panic("bad sensor %d\n", edata->sensor);
450 }
451 out:
452 mutex_exit(&sc->sc_mutex);
453 }
454
455 static int
456 am2315_detach(device_t self, int flags)
457 {
458 struct am2315_sc *sc = device_private(self);
459
460 mutex_enter(&sc->sc_mutex);
461
462 /* Remove the sensors */
463 if (sc->sc_sme != NULL) {
464 sysmon_envsys_unregister(sc->sc_sme);
465 sc->sc_sme = NULL;
466 }
467 mutex_exit(&sc->sc_mutex);
468
469 /* Destroy the wait cond */
470 cv_destroy(&sc->sc_condwait);
471
472 /* Remove the sysctl tree */
473 sysctl_teardown(&sc->sc_am2315log);
474
475 /* Remove the mutex */
476 mutex_destroy(&sc->sc_waitmutex);
477 mutex_destroy(&sc->sc_mutex);
478
479 return 0;
480 }
481
482 MODULE(MODULE_CLASS_DRIVER, am2315temp, "i2cexec,sysmon_envsys");
483
484 #ifdef _MODULE
485 #include "ioconf.c"
486 #endif
487
488 static int
489 am2315temp_modcmd(modcmd_t cmd, void *opaque)
490 {
491 switch (cmd) {
492 case MODULE_CMD_INIT:
493 #ifdef _MODULE
494 return config_init_component(cfdriver_ioconf_am2315temp,
495 cfattach_ioconf_am2315temp, cfdata_ioconf_am2315temp);
496 #else
497 return 0;
498 #endif
499 case MODULE_CMD_FINI:
500 #ifdef _MODULE
501 return config_fini_component(cfdriver_ioconf_am2315temp,
502 cfattach_ioconf_am2315temp, cfdata_ioconf_am2315temp);
503 #else
504 return 0;
505 #endif
506 default:
507 return ENOTTY;
508 }
509 }
510