2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2021 Andriy Gapon
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_platform.h"
34 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/sysctl.h>
39 #include <sys/systm.h>
41 #include <machine/bus.h>
43 #include <dev/iicbus/iicbus.h>
44 #include <dev/iicbus/iiconf.h>
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
52 * Driver for HTU21D and compatible temperature and humidity sensors.
53 * Reference documents:
54 * - Measurement Specialties HTU21D datasheet,
55 * - Sensirion SHT21 datasheet,
56 * - Silicon Labs Si7021 datasheet,
57 * - HTU2X Serial Number Reading application note,
58 * - Sensirion Electronic Identification Code (How to read-out the serial number
59 * of SHT2x) application note.
61 #define HTU21_ADDR 0x40
63 #define HTU21_GET_TEMP 0xe3
64 #define HTU21_GET_HUM 0xe5
65 #define HTU21_GET_TEMP_NH 0xf3
66 #define HTU21_GET_HUM_NH 0xf5
67 #define HTU21_WRITE_CFG 0xe6
68 #define HTU21_READ_CFG 0xe7
69 #define HTU21_RESET 0xfe
71 #define HTU2x_SERIAL0_0 0xfa
72 #define HTU2x_SERIAL0_1 0x0f
73 #define HTU2x_SERIAL1_0 0xfc
74 #define HTU2x_SERIAL1_1 0xc9
85 static struct ofw_compat_data compat_data[] = {
86 { "meas,htu21", true },
92 calc_crc(uint16_t data)
94 static const uint16_t polynomial = 0x3100;
97 for (i = 0; i < 16; i++) {
98 int msb_neq = data & 0x8000;
108 check_crc_16(const uint8_t *data, uint8_t expected)
112 crc = calc_crc(((uint16_t)data[0] << 8) | data[1]);
113 return (crc == expected);
117 check_crc_8(const uint8_t data, uint8_t expected)
121 crc = calc_crc(data);
122 return (crc == expected);
126 htu21_get_measurement(device_t dev, uint8_t cmd, uint8_t *data, int count)
129 struct iic_msg msgs[2];
130 struct htu21_softc *sc;
133 sc = device_get_softc(dev);
134 msgs[0].slave = sc->sc_addr;
135 msgs[0].flags = IIC_M_WR | IIC_M_NOSTOP;
139 msgs[1].slave = sc->sc_addr;
140 msgs[1].flags = IIC_M_RD;
144 error = iicbus_transfer_excl(dev, msgs, nitems(msgs), IIC_INTRWAIT);
149 htu21_get_measurement_nohold(device_t dev, uint8_t cmd,
150 uint8_t *data, int count)
152 struct iic_msg msgs[2];
153 struct htu21_softc *sc;
157 sc = device_get_softc(dev);
159 msgs[0].slave = sc->sc_addr;
160 msgs[0].flags = IIC_M_WR;
164 msgs[1].slave = sc->sc_addr;
165 msgs[1].flags = IIC_M_RD;
169 error = iicbus_transfer_excl(dev, &msgs[0], 1, IIC_INTRWAIT);
173 for (i = 0; i < hz; i++) {
174 error = iicbus_transfer_excl(dev, &msgs[1], 1, IIC_INTRWAIT);
177 if (error != IIC_ENOACK)
185 htu21_temp_sysctl(SYSCTL_HANDLER_ARGS)
187 struct htu21_softc *sc;
193 sc = device_get_softc(dev);
195 if (req->oldptr != NULL) {
197 error = htu21_get_measurement(dev, HTU21_GET_TEMP,
198 raw_data, nitems(raw_data));
200 error = htu21_get_measurement_nohold(dev,
201 HTU21_GET_TEMP_NH, raw_data, nitems(raw_data));
205 } else if (!check_crc_16(raw_data, raw_data[2])) {
209 temp = (((uint16_t)raw_data[0]) << 8) |
210 (raw_data[1] & 0xfc);
211 temp = ((temp * 17572) >> 16 ) + 27315 - 4685;
215 error = sysctl_handle_int(oidp, &temp, 0, req);
220 htu21_rh_sysctl(SYSCTL_HANDLER_ARGS)
222 struct htu21_softc *sc;
228 sc = device_get_softc(dev);
230 if (req->oldptr != NULL) {
232 error = htu21_get_measurement(dev, HTU21_GET_HUM,
233 raw_data, nitems(raw_data));
235 error = htu21_get_measurement_nohold(dev,
236 HTU21_GET_HUM_NH, raw_data, nitems(raw_data));
240 } else if (!check_crc_16(raw_data, raw_data[2])) {
244 rh = (((uint16_t)raw_data[0]) << 8) |
245 (raw_data[1] & 0xfc);
246 rh = ((rh * 12500) >> 16 ) - 600;
250 error = sysctl_handle_int(oidp, &rh, 0, req);
255 htu21_get_cfg(device_t dev, uint8_t *cfg)
258 struct iic_msg msgs[2];
259 struct htu21_softc *sc;
263 sc = device_get_softc(dev);
264 cmd = HTU21_READ_CFG;
265 msgs[0].slave = sc->sc_addr;
266 msgs[0].flags = IIC_M_WR | IIC_M_NOSTOP;
270 msgs[1].slave = sc->sc_addr;
271 msgs[1].flags = IIC_M_RD;
275 error = iicbus_transfer_excl(dev, msgs, nitems(msgs), IIC_INTRWAIT);
280 htu21_set_cfg(device_t dev, uint8_t cfg)
284 struct htu21_softc *sc;
288 sc = device_get_softc(dev);
289 buf[0] = HTU21_WRITE_CFG;
291 msg.slave = sc->sc_addr;
292 msg.flags = IIC_M_WR;
296 error = iicbus_transfer_excl(dev, &msg, 1, IIC_INTRWAIT);
301 htu21_heater_sysctl(SYSCTL_HANDLER_ARGS)
303 struct htu21_softc *sc;
309 sc = device_get_softc(dev);
311 if (req->oldptr != NULL) {
312 error = htu21_get_cfg(dev, &cfg);
315 heater = (cfg & 0x04) != 0;
317 error = sysctl_handle_int(oidp, &heater, 0, req);
318 if (error != 0 || req->newptr == NULL)
322 cfg |= (heater > 0) << 2;
323 error = htu21_set_cfg(dev, cfg);
324 return (error != 0 ? EIO : 0);
328 htu21_power_sysctl(SYSCTL_HANDLER_ARGS)
330 struct htu21_softc *sc;
336 sc = device_get_softc(dev);
338 if (req->oldptr != NULL) {
339 error = htu21_get_cfg(dev, &cfg);
342 power = (cfg & 0x40) == 0;
344 error = sysctl_handle_int(oidp, &power, 0, req);
349 * May be incompatible with some chips like SHT21 and Si7021.
352 htu21_get_serial(device_t dev)
355 struct iic_msg msgs[2];
356 struct htu21_softc *sc;
359 int error, cksum_err;
362 sc = device_get_softc(dev);
363 cmd[0] = HTU2x_SERIAL0_0;
364 cmd[1] = HTU2x_SERIAL0_1;
365 msgs[0].slave = sc->sc_addr;
366 msgs[0].flags = IIC_M_WR | IIC_M_NOSTOP;
367 msgs[0].len = nitems(cmd);
370 msgs[1].slave = sc->sc_addr;
371 msgs[1].flags = IIC_M_RD;
372 msgs[1].len = nitems(data);
375 error = iicbus_transfer_excl(dev, msgs, nitems(msgs), IIC_INTRWAIT);
380 for (i = 0; i < nitems(data); i += 2) {
381 if (!check_crc_8(data[i], data[i + 1]))
383 sc->sc_serial[2 + i / 2] = data[i];
386 cmd[0] = HTU2x_SERIAL1_0;
387 cmd[1] = HTU2x_SERIAL1_1;
388 msgs[0].slave = sc->sc_addr;
389 msgs[0].flags = IIC_M_WR | IIC_M_NOSTOP;
390 msgs[0].len = nitems(cmd);
393 msgs[1].slave = sc->sc_addr;
394 msgs[1].flags = IIC_M_RD;
398 error = iicbus_transfer_excl(dev, msgs, nitems(msgs), IIC_INTRWAIT);
402 if (!check_crc_16(&data[0], data[2]))
404 sc->sc_serial[6] = data[0];
405 sc->sc_serial[7] = data[1];
407 if (!check_crc_16(&data[3], data[5]))
409 sc->sc_serial[0] = data[3];
410 sc->sc_serial[1] = data[4];
416 htu21_start(void *arg)
419 struct htu21_softc *sc;
420 struct sysctl_ctx_list *ctx;
421 struct sysctl_oid *tree_node;
422 struct sysctl_oid_list *tree;
428 for (int i = 0; i < 5; i++) {
429 error = htu21_get_serial(dev);
434 device_printf(dev, "serial number: %8D (checksum %scorrect)\n",
435 sc->sc_serial, ":", error == 0 ? "" : "in");
437 device_printf(dev, "failed to get serial number, err = %d\n",
441 ctx = device_get_sysctl_ctx(dev);
442 tree_node = device_get_sysctl_tree(dev);
443 tree = SYSCTL_CHILDREN(tree_node);
445 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "temperature",
446 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, 0,
447 htu21_temp_sysctl, "IK2", "Current temperature");
448 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "humidity",
449 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, 0,
450 htu21_rh_sysctl, "I", "Relative humidity in 0.01%% units");
451 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "heater",
452 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, dev, 0,
453 htu21_heater_sysctl, "IU", "Enable built-in heater");
454 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "power",
455 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, 0,
456 htu21_power_sysctl, "IU", "If sensor's power is good");
457 SYSCTL_ADD_BOOL(ctx, tree, OID_AUTO, "hold_bus",
458 CTLFLAG_RW, &sc->sc_hold, 0,
459 "Whether device should hold I2C bus while measuring");
460 SYSCTL_ADD_INT(ctx, tree, OID_AUTO, "crc_errors",
461 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, &sc->sc_errcount, 0,
462 "Number of checksum errors");
466 htu21_probe(device_t dev)
472 if (!ofw_bus_status_okay(dev))
474 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data)
475 rc = BUS_PROBE_GENERIC;
478 rc = BUS_PROBE_NOWILDCARD;
480 addr = iicbus_get_addr(dev);
481 if (addr != (HTU21_ADDR << 1)) {
482 device_printf(dev, "non-standard slave address 0x%02x\n",
486 device_set_desc(dev, "HTU21 temperature and humidity sensor");
491 htu21_attach(device_t dev)
493 struct htu21_softc *sc;
495 sc = device_get_softc(dev);
497 sc->sc_addr = iicbus_get_addr(dev);
500 * We have to wait until interrupts are enabled. Usually I2C read
501 * and write only works when the interrupts are available.
503 config_intrhook_oneshot(htu21_start, sc);
508 htu21_detach(device_t dev)
513 static device_method_t htu21_methods[] = {
514 /* Device interface */
515 DEVMETHOD(device_probe, htu21_probe),
516 DEVMETHOD(device_attach, htu21_attach),
517 DEVMETHOD(device_detach, htu21_detach),
522 static driver_t htu21_driver = {
525 sizeof(struct htu21_softc)
528 static devclass_t htu21_devclass;
530 DRIVER_MODULE(htu21, iicbus, htu21_driver, htu21_devclass, 0, 0);
531 MODULE_DEPEND(htu21, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
532 MODULE_VERSION(htu21, 1);
533 IICBUS_FDT_PNP_INFO(compat_data);