2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2016 Michael Zhilin <mizhka@freebsd.org> All rights reserved.
5 * Copyright (c) 2019 Ian Lepore <ian@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * GPIOTHS - Temp/Humidity sensor over GPIO.
32 * This is driver for Temperature & Humidity sensor which provides digital
33 * output over single-wire protocol from embedded 8-bit microcontroller.
34 * Note that it uses a custom single-wire protocol, it is not 1-wire(tm).
36 * This driver supports the following chips:
37 * DHT11: Temp 0c to 50c +-2.0c, Humidity 20% to 90% +-5%
38 * DHT12: Temp -20c to 60c +-0.5c, Humidity 20% to 95% +-5%
39 * DHT21: Temp -40c to 80c +-0.3c, Humidity 0% to 100% +-3%
40 * DHT22: Temp -40c to 80c +-0.3c, Humidity 0% to 100% +-2%
41 * AM2301: Same as DHT21, but also supports i2c interface.
42 * AM2302: Same as DHT22, but also supports i2c interface.
44 * Temp/Humidity sensor can't be discovered automatically, please specify hints
45 * as part of loader or kernel configuration:
46 * hint.gpioths.0.at="gpiobus0"
47 * hint.gpioths.0.pins=<PIN>
49 * Or configure via FDT data.
52 #include <sys/cdefs.h>
53 __FBSDID("$FreeBSD$");
55 #include <sys/param.h>
56 #include <sys/kernel.h>
59 #include <sys/module.h>
60 #include <sys/errno.h>
61 #include <sys/systm.h>
62 #include <sys/sysctl.h>
63 #include <sys/taskqueue.h>
65 #include <dev/gpio/gpiobusvar.h>
68 #include <dev/ofw/ofw_bus.h>
69 #include <dev/ofw/ofw_bus_subr.h>
71 static struct ofw_compat_data compat_data[] = {
75 OFWBUS_PNP_INFO(compat_data);
76 SIMPLEBUS_PNP_INFO(compat_data);
79 #define PIN_IDX 0 /* Use the first/only configured pin. */
81 #define GPIOTHS_POLLTIME 5 /* in seconds */
83 #define GPIOTHS_DHT_STARTCYCLE 20000 /* 20ms = 20000us */
84 #define GPIOTHS_DHT_TIMEOUT 1000 /* 1ms = 1000us */
85 #define GPIOTHS_DHT_CYCLES 41
86 #define GPIOTHS_DHT_ONEBYTEMASK 0xFF
88 struct gpioths_softc {
94 struct timeout_task task;
99 gpioths_probe(device_t dev)
104 * By default we only bid to attach if specifically added by our parent
105 * (usually via hint.gpioths.#.at=busname). On FDT systems we bid as
106 * the default driver based on being configured in the FDT data.
108 rv = BUS_PROBE_NOWILDCARD;
111 if (ofw_bus_status_okay(dev) &&
112 ofw_bus_search_compatible(dev, compat_data)->ocd_data)
113 rv = BUS_PROBE_DEFAULT;
116 device_set_desc(dev, "DHT11/DHT22 Temperature and Humidity Sensor");
122 gpioths_dht_timeuntil(struct gpioths_softc *sc, bool lev, uint32_t *time)
127 for (i = 0; i < GPIOTHS_DHT_TIMEOUT; i++) {
128 gpio_pin_is_active(sc->pin, &cur_level);
129 if (cur_level == lev) {
142 gpioths_dht_initread(struct gpioths_softc *sc)
146 * According to specifications we need to drive the data line low for at
147 * least 20ms then drive it high, to wake up the chip and signal it to
148 * send a measurement. After sending this start signal, we switch the
149 * pin back to input so the device can begin talking to us.
151 gpio_pin_setflags(sc->pin, GPIO_PIN_OUTPUT);
152 gpio_pin_set_active(sc->pin, false);
153 pause_sbt("gpioths", ustosbt(GPIOTHS_DHT_STARTCYCLE), C_PREL(2), 0);
154 gpio_pin_set_active(sc->pin, true);
155 gpio_pin_setflags(sc->pin, GPIO_PIN_INPUT);
159 gpioths_dht_readbytes(struct gpioths_softc *sc)
161 uint32_t calibrations[GPIOTHS_DHT_CYCLES];
162 uint32_t intervals[GPIOTHS_DHT_CYCLES];
163 uint32_t err, avglen, value;
165 int i, negmul, offset, size, tmphi, tmplo;
167 gpioths_dht_initread(sc);
169 err = gpioths_dht_timeuntil(sc, false, NULL);
171 device_printf(sc->dev, "err(START) = %d\n", err);
175 /* reading - 41 cycles */
176 for (i = 0; i < GPIOTHS_DHT_CYCLES; i++) {
177 err = gpioths_dht_timeuntil(sc, true, &calibrations[i]);
179 device_printf(sc->dev, "err(CAL, %d) = %d\n", i, err);
182 err = gpioths_dht_timeuntil(sc, false, &intervals[i]);
184 device_printf(sc->dev, "err(INTERVAL, %d) = %d\n", i, err);
189 /* Calculate average data calibration cycle length */
191 for (i = 1; i < GPIOTHS_DHT_CYCLES; i++)
192 avglen += calibrations[i];
194 avglen = avglen / (GPIOTHS_DHT_CYCLES - 1);
199 size = sizeof(value) * 8;
200 for (i = offset; i < size + offset; i++) {
202 if (intervals[i] > avglen)
208 offset = sizeof(value) * 8 + 1;
209 size = sizeof(crc) * 8;
210 for (i = offset; i < size + offset; i++) {
212 if (intervals[i] > avglen)
217 for (i = 0; i < sizeof(value); i++)
218 calc += (value >> (8*i)) & GPIOTHS_DHT_ONEBYTEMASK;
222 for (i = 0; i < GPIOTHS_DHT_CYCLES; i++)
223 device_printf(sc->dev, "%d: %d %d\n", i, calibrations[i],
226 device_printf(sc->dev, "len=%d, data=%x, crc=%x/%x\n", avglen, value, crc,
228 #endif /* GPIOTHS_DEBUG */
237 * For DHT11/12, the values are split into 8 bits of integer and 8 bits
238 * of fractional tenths. On DHT11 the fraction bytes are always zero.
239 * On DHT12 the sign bit is in the high bit of the fraction byte.
240 * - DHT11: 0HHHHHHH 00000000 00TTTTTT 00000000
241 * - DHT12: 0HHHHHHH 0000hhhh 00TTTTTT s000tttt
243 * For DHT21/21, the values are are encoded in 16 bits each, with the
244 * temperature sign bit in the high bit. The values are tenths of a
245 * degree C and tenths of a percent RH.
246 * - DHT21: 000000HH HHHHHHHH s00000TT TTTTTTTT
247 * - DHT22: 000000HH HHHHHHHH s00000TT TTTTTTTT
249 * For all devices, some bits are always zero because of the range of
250 * values supported by the device.
252 * We figure out how to decode things based on the high byte of the
253 * humidity. A DHT21/22 cannot report a value greater than 3 in
254 * the upper bits of its 16-bit humidity. A DHT11/12 should not report
255 * a value lower than 20. To allow for the possibility that a device
256 * could report a value slightly out of its sensitivity range, we split
257 * the difference and say if the value is greater than 10 it must be a
258 * DHT11/12 (that would be a humidity over 256% on a DHT21/22).
260 #define DK_OFFSET 2731 /* Offset between K and C, in decikelvins. */
261 if ((value >> 24) > 10) {
263 tmphi = (value >> 8) & 0x3f;
264 tmplo = value & 0x0f;
265 negmul = (value & 0x80) ? -1 : 1;
266 sc->temp = DK_OFFSET + (negmul * (tmphi * 10 + tmplo));
267 sc->hum = (value >> 24) & 0x7f;
270 negmul = (value & 0x8000) ? -1 : 1;
271 sc->temp = DK_OFFSET + (negmul * (value & 0x03ff));
272 sc->hum = ((value >> 16) & 0x03ff) / 10;
279 device_printf(dev, "fails=%d, temp=%d, hum=%d\n", sc->fails,
281 #endif /* GPIOTHS_DEBUG */
290 gpioths_poll(void *arg, int pending __unused)
292 struct gpioths_softc *sc;
294 sc = (struct gpioths_softc *)arg;
296 gpioths_dht_readbytes(sc);
298 taskqueue_enqueue_timeout_sbt(taskqueue_thread, &sc->task,
299 GPIOTHS_POLLTIME * SBT_1S, 0, C_PREL(3));
303 gpioths_attach(device_t dev)
305 struct gpioths_softc *sc;
306 struct sysctl_ctx_list *ctx;
307 struct sysctl_oid *tree;
310 sc = device_get_softc(dev);
311 ctx = device_get_sysctl_ctx(dev);
312 tree = device_get_sysctl_tree(dev);
316 TIMEOUT_TASK_INIT(taskqueue_thread, &sc->task, 0, gpioths_poll, sc);
319 /* Try to configure our pin from fdt data on fdt-based systems. */
320 err = gpio_pin_get_by_ofw_idx(dev, ofw_bus_get_node(dev), PIN_IDX,
326 * If we didn't get configured by fdt data and our parent is gpiobus,
327 * see if we can be configured by the bus (allows hinted attachment even
328 * on fdt-based systems).
331 strcmp("gpiobus", device_get_name(device_get_parent(dev))) == 0)
332 err = gpio_pin_get_by_child_index(dev, PIN_IDX, &sc->pin);
334 /* If we didn't get configured by either method, whine and punt. */
336 device_printf(sc->dev,
337 "cannot acquire gpio pin (config error)\n");
342 * Ensure we have control of our pin, and preset the data line to its
343 * idle condition (high). Leave the line in input mode, relying on the
344 * external pullup to keep the line high while idle.
346 err = gpio_pin_setflags(sc->pin, GPIO_PIN_OUTPUT);
348 device_printf(dev, "gpio_pin_setflags(OUT) = %d\n", err);
351 err = gpio_pin_set_active(sc->pin, true);
353 device_printf(dev, "gpio_pin_set_active(false) = %d\n", err);
356 err = gpio_pin_setflags(sc->pin, GPIO_PIN_INPUT);
358 device_printf(dev, "gpio_pin_setflags(IN) = %d\n", err);
363 * Do an initial read so we have correct values for reporting before
364 * registering the sysctls that can access those values. This also
365 * schedules the periodic polling the driver does every few seconds to
366 * update the sysctl variables.
370 sysctl_add_oid(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "temperature", \
371 CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE,
372 &sc->temp, 0, sysctl_handle_int, "IK", "temperature", NULL);
374 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "humidity",
375 CTLFLAG_RD, &sc->hum, 0, "relative humidity(%)");
377 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "fails",
378 CTLFLAG_RD, &sc->fails, 0,
379 "failures since last successful read");
385 gpioths_detach(device_t dev)
387 struct gpioths_softc *sc;
389 sc = device_get_softc(dev);
390 gpio_pin_release(sc->pin);
391 sc->detaching = true;
392 while (taskqueue_cancel_timeout(taskqueue_thread, &sc->task, NULL) != 0)
393 taskqueue_drain_timeout(taskqueue_thread, &sc->task);
399 static device_method_t gpioths_methods[] = {
400 /* Device interface */
401 DEVMETHOD(device_probe, gpioths_probe),
402 DEVMETHOD(device_attach, gpioths_attach),
403 DEVMETHOD(device_detach, gpioths_detach),
408 static devclass_t gpioths_devclass;
410 DEFINE_CLASS_0(gpioths, gpioths_driver, gpioths_methods, sizeof(struct gpioths_softc));
413 DRIVER_MODULE(gpioths, simplebus, gpioths_driver, gpioths_devclass, 0, 0);
416 DRIVER_MODULE(gpioths, gpiobus, gpioths_driver, gpioths_devclass, 0, 0);
417 MODULE_DEPEND(gpioths, gpiobus, 1, 1, 1);