blob: 9c47bc98f3acdea4cb4b56b23a72e582c34a413b [file] [log] [blame]
Harald Geyer091a1212013-12-01 15:04:00 +00001/*
2 * DHT11/DHT22 bit banging GPIO driver
3 *
4 * Copyright (c) Harald Geyer <harald@ccbib.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <linux/err.h>
18#include <linux/interrupt.h>
19#include <linux/device.h>
20#include <linux/kernel.h>
21#include <linux/printk.h>
22#include <linux/slab.h>
23#include <linux/of.h>
24#include <linux/of_device.h>
25#include <linux/sysfs.h>
26#include <linux/io.h>
27#include <linux/module.h>
28#include <linux/platform_device.h>
29#include <linux/wait.h>
30#include <linux/bitops.h>
31#include <linux/completion.h>
Richard Weinberger004bc532015-01-07 13:18:23 +010032#include <linux/mutex.h>
Harald Geyer091a1212013-12-01 15:04:00 +000033#include <linux/delay.h>
34#include <linux/gpio.h>
35#include <linux/of_gpio.h>
Harald Geyer081d9742015-07-07 13:39:31 +000036#include <linux/timekeeping.h>
Harald Geyer091a1212013-12-01 15:04:00 +000037
38#include <linux/iio/iio.h>
39
40#define DRIVER_NAME "dht11"
41
42#define DHT11_DATA_VALID_TIME 2000000000 /* 2s in ns */
43
Richard Weinberger94e65512015-01-07 13:22:49 +010044#define DHT11_EDGES_PREAMBLE 2
Harald Geyer091a1212013-12-01 15:04:00 +000045#define DHT11_BITS_PER_READ 40
Richard Weinberger94e65512015-01-07 13:22:49 +010046/*
47 * Note that when reading the sensor actually 84 edges are detected, but
48 * since the last edge is not significant, we only store 83:
49 */
Harald Geyer889c5e92015-07-07 13:39:28 +000050#define DHT11_EDGES_PER_READ (2 * DHT11_BITS_PER_READ + \
51 DHT11_EDGES_PREAMBLE + 1)
Harald Geyer091a1212013-12-01 15:04:00 +000052
Harald Geyer155a5752016-01-17 16:13:30 +000053/*
54 * Data transmission timing:
55 * Data bits are encoded as pulse length (high time) on the data line.
56 * 0-bit: 22-30uS -- typically 26uS (AM2302)
57 * 1-bit: 68-75uS -- typically 70uS (AM2302)
58 * The acutal timings also depend on the properties of the cable, with
59 * longer cables typically making pulses shorter.
60 *
61 * Our decoding depends on the time resolution of the system:
62 * timeres > 34uS ... don't know what a 1-tick pulse is
63 * 34uS > timeres > 30uS ... no problem (30kHz and 32kHz clocks)
64 * 30uS > timeres > 23uS ... don't know what a 2-tick pulse is
65 * timeres < 23uS ... no problem
66 *
67 * Luckily clocks in the 33-44kHz range are quite uncommon, so we can
68 * support most systems if the threshold for decoding a pulse as 1-bit
69 * is chosen carefully. If somebody really wants to support clocks around
70 * 40kHz, where this driver is most unreliable, there are two options.
71 * a) select an implementation using busy loop polling on those systems
72 * b) use the checksum to do some probabilistic decoding
73 */
Harald Geyer091a1212013-12-01 15:04:00 +000074#define DHT11_START_TRANSMISSION 18 /* ms */
Harald Geyer155a5752016-01-17 16:13:30 +000075#define DHT11_MIN_TIMERES 34000 /* ns */
76#define DHT11_THRESHOLD 49000 /* ns */
77#define DHT11_AMBIG_LOW 23000 /* ns */
78#define DHT11_AMBIG_HIGH 30000 /* ns */
Harald Geyer091a1212013-12-01 15:04:00 +000079
80struct dht11 {
81 struct device *dev;
82
83 int gpio;
84 int irq;
85
86 struct completion completion;
Harald Geyera7126002015-07-07 13:39:29 +000087 /* The iio sysfs interface doesn't prevent concurrent reads: */
Richard Weinberger004bc532015-01-07 13:18:23 +010088 struct mutex lock;
Harald Geyer091a1212013-12-01 15:04:00 +000089
90 s64 timestamp;
91 int temperature;
92 int humidity;
93
94 /* num_edges: -1 means "no transmission in progress" */
95 int num_edges;
96 struct {s64 ts; int value; } edges[DHT11_EDGES_PER_READ];
97};
98
Harald Geyerab4b6492016-01-17 16:13:31 +000099#ifdef CONFIG_DYNAMIC_DEBUG
100/*
101 * dht11_edges_print: show the data as actually received by the
102 * driver.
103 */
104static void dht11_edges_print(struct dht11 *dht11)
105{
106 int i;
107
108 dev_dbg(dht11->dev, "%d edges detected:\n", dht11->num_edges);
109 for (i = 1; i < dht11->num_edges; ++i) {
110 dev_dbg(dht11->dev, "%d: %lld ns %s\n", i,
111 dht11->edges[i].ts - dht11->edges[i - 1].ts,
112 dht11->edges[i - 1].value ? "high" : "low");
113 }
114}
115#endif /* CONFIG_DYNAMIC_DEBUG */
116
Harald Geyer155a5752016-01-17 16:13:30 +0000117static unsigned char dht11_decode_byte(char *bits)
Harald Geyer091a1212013-12-01 15:04:00 +0000118{
119 unsigned char ret = 0;
120 int i;
121
122 for (i = 0; i < 8; ++i) {
123 ret <<= 1;
Harald Geyer155a5752016-01-17 16:13:30 +0000124 if (bits[i])
Harald Geyer091a1212013-12-01 15:04:00 +0000125 ++ret;
126 }
127
128 return ret;
129}
130
Harald Geyer155a5752016-01-17 16:13:30 +0000131static int dht11_decode(struct dht11 *dht11, int offset)
Harald Geyer091a1212013-12-01 15:04:00 +0000132{
Harald Geyer155a5752016-01-17 16:13:30 +0000133 int i, t;
134 char bits[DHT11_BITS_PER_READ];
Harald Geyer091a1212013-12-01 15:04:00 +0000135 unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum;
136
Harald Geyer091a1212013-12-01 15:04:00 +0000137 for (i = 0; i < DHT11_BITS_PER_READ; ++i) {
Harald Geyer889c5e92015-07-07 13:39:28 +0000138 t = dht11->edges[offset + 2 * i + 2].ts -
139 dht11->edges[offset + 2 * i + 1].ts;
Harald Geyerab4b6492016-01-17 16:13:31 +0000140 if (!dht11->edges[offset + 2 * i + 1].value) {
141 dev_dbg(dht11->dev,
142 "lost synchronisation at edge %d\n",
143 offset + 2 * i + 1);
144 return -EIO;
145 }
Harald Geyer155a5752016-01-17 16:13:30 +0000146 bits[i] = t > DHT11_THRESHOLD;
Harald Geyer091a1212013-12-01 15:04:00 +0000147 }
148
Harald Geyer155a5752016-01-17 16:13:30 +0000149 hum_int = dht11_decode_byte(bits);
150 hum_dec = dht11_decode_byte(&bits[8]);
151 temp_int = dht11_decode_byte(&bits[16]);
152 temp_dec = dht11_decode_byte(&bits[24]);
153 checksum = dht11_decode_byte(&bits[32]);
Harald Geyer091a1212013-12-01 15:04:00 +0000154
Harald Geyerab4b6492016-01-17 16:13:31 +0000155 if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum) {
156 dev_dbg(dht11->dev, "invalid checksum\n");
Harald Geyer091a1212013-12-01 15:04:00 +0000157 return -EIO;
Harald Geyerab4b6492016-01-17 16:13:31 +0000158 }
Harald Geyer091a1212013-12-01 15:04:00 +0000159
Abhilash Jindal17a2f462016-01-27 17:46:02 -0500160 dht11->timestamp = ktime_get_boot_ns();
Harald Geyer091a1212013-12-01 15:04:00 +0000161 if (hum_int < 20) { /* DHT22 */
162 dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
163 ((temp_int & 0x80) ? -100 : 100);
164 dht11->humidity = ((hum_int << 8) + hum_dec) * 100;
165 } else if (temp_dec == 0 && hum_dec == 0) { /* DHT11 */
166 dht11->temperature = temp_int * 1000;
167 dht11->humidity = hum_int * 1000;
168 } else {
169 dev_err(dht11->dev,
170 "Don't know how to decode data: %d %d %d %d\n",
171 hum_int, hum_dec, temp_int, temp_dec);
172 return -EIO;
173 }
174
175 return 0;
176}
177
Richard Weinberger94e65512015-01-07 13:22:49 +0100178/*
179 * IRQ handler called on GPIO edges
180 */
181static irqreturn_t dht11_handle_irq(int irq, void *data)
182{
183 struct iio_dev *iio = data;
184 struct dht11 *dht11 = iio_priv(iio);
185
186 /* TODO: Consider making the handler safe for IRQ sharing */
187 if (dht11->num_edges < DHT11_EDGES_PER_READ && dht11->num_edges >= 0) {
Abhilash Jindal17a2f462016-01-27 17:46:02 -0500188 dht11->edges[dht11->num_edges].ts = ktime_get_boot_ns();
Richard Weinberger94e65512015-01-07 13:22:49 +0100189 dht11->edges[dht11->num_edges++].value =
190 gpio_get_value(dht11->gpio);
191
192 if (dht11->num_edges >= DHT11_EDGES_PER_READ)
193 complete(&dht11->completion);
194 }
195
196 return IRQ_HANDLED;
197}
198
Harald Geyer091a1212013-12-01 15:04:00 +0000199static int dht11_read_raw(struct iio_dev *iio_dev,
Harald Geyer889c5e92015-07-07 13:39:28 +0000200 const struct iio_chan_spec *chan,
Harald Geyer091a1212013-12-01 15:04:00 +0000201 int *val, int *val2, long m)
202{
203 struct dht11 *dht11 = iio_priv(iio_dev);
Harald Geyer22acc122016-01-17 16:13:29 +0000204 int ret, timeres, offset;
Harald Geyer091a1212013-12-01 15:04:00 +0000205
Richard Weinberger004bc532015-01-07 13:18:23 +0100206 mutex_lock(&dht11->lock);
Abhilash Jindal17a2f462016-01-27 17:46:02 -0500207 if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boot_ns()) {
Harald Geyer081d9742015-07-07 13:39:31 +0000208 timeres = ktime_get_resolution_ns();
Harald Geyerab4b6492016-01-17 16:13:31 +0000209 dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
Harald Geyer155a5752016-01-17 16:13:30 +0000210 if (timeres > DHT11_MIN_TIMERES) {
Harald Geyer081d9742015-07-07 13:39:31 +0000211 dev_err(dht11->dev, "timeresolution %dns too low\n",
212 timeres);
213 /* In theory a better clock could become available
214 * at some point ... and there is no error code
215 * that really fits better.
216 */
217 ret = -EAGAIN;
218 goto err;
219 }
Harald Geyer155a5752016-01-17 16:13:30 +0000220 if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH)
221 dev_warn(dht11->dev,
222 "timeresolution: %dns - decoding ambiguous\n",
223 timeres);
Harald Geyer081d9742015-07-07 13:39:31 +0000224
Harald Geyer091a1212013-12-01 15:04:00 +0000225 reinit_completion(&dht11->completion);
226
227 dht11->num_edges = 0;
228 ret = gpio_direction_output(dht11->gpio, 0);
229 if (ret)
230 goto err;
231 msleep(DHT11_START_TRANSMISSION);
232 ret = gpio_direction_input(dht11->gpio);
233 if (ret)
234 goto err;
235
Richard Weinberger94e65512015-01-07 13:22:49 +0100236 ret = request_irq(dht11->irq, dht11_handle_irq,
237 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
238 iio_dev->name, iio_dev);
239 if (ret)
240 goto err;
241
Harald Geyer091a1212013-12-01 15:04:00 +0000242 ret = wait_for_completion_killable_timeout(&dht11->completion,
Harald Geyer889c5e92015-07-07 13:39:28 +0000243 HZ);
Richard Weinberger94e65512015-01-07 13:22:49 +0100244
245 free_irq(dht11->irq, iio_dev);
246
Harald Geyerab4b6492016-01-17 16:13:31 +0000247#ifdef CONFIG_DYNAMIC_DEBUG
248 dht11_edges_print(dht11);
249#endif
250
Harald Geyer091a1212013-12-01 15:04:00 +0000251 if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
Harald Geyerab4b6492016-01-17 16:13:31 +0000252 dev_err(dht11->dev, "Only %d signal edges detected\n",
253 dht11->num_edges);
Harald Geyer091a1212013-12-01 15:04:00 +0000254 ret = -ETIMEDOUT;
255 }
256 if (ret < 0)
257 goto err;
258
Harald Geyer22acc122016-01-17 16:13:29 +0000259 offset = DHT11_EDGES_PREAMBLE +
260 dht11->num_edges - DHT11_EDGES_PER_READ;
261 for (; offset >= 0; --offset) {
Harald Geyer155a5752016-01-17 16:13:30 +0000262 ret = dht11_decode(dht11, offset);
Harald Geyer22acc122016-01-17 16:13:29 +0000263 if (!ret)
264 break;
265 }
266
Harald Geyer091a1212013-12-01 15:04:00 +0000267 if (ret)
268 goto err;
269 }
270
271 ret = IIO_VAL_INT;
272 if (chan->type == IIO_TEMP)
273 *val = dht11->temperature;
274 else if (chan->type == IIO_HUMIDITYRELATIVE)
275 *val = dht11->humidity;
276 else
277 ret = -EINVAL;
278err:
279 dht11->num_edges = -1;
Richard Weinberger004bc532015-01-07 13:18:23 +0100280 mutex_unlock(&dht11->lock);
Harald Geyer091a1212013-12-01 15:04:00 +0000281 return ret;
282}
283
284static const struct iio_info dht11_iio_info = {
285 .driver_module = THIS_MODULE,
286 .read_raw = dht11_read_raw,
287};
288
Harald Geyer091a1212013-12-01 15:04:00 +0000289static const struct iio_chan_spec dht11_chan_spec[] = {
290 { .type = IIO_TEMP,
291 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), },
292 { .type = IIO_HUMIDITYRELATIVE,
293 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), }
294};
295
296static const struct of_device_id dht11_dt_ids[] = {
297 { .compatible = "dht11", },
298 { }
299};
300MODULE_DEVICE_TABLE(of, dht11_dt_ids);
301
302static int dht11_probe(struct platform_device *pdev)
303{
304 struct device *dev = &pdev->dev;
305 struct device_node *node = dev->of_node;
306 struct dht11 *dht11;
307 struct iio_dev *iio;
308 int ret;
309
310 iio = devm_iio_device_alloc(dev, sizeof(*dht11));
311 if (!iio) {
312 dev_err(dev, "Failed to allocate IIO device\n");
313 return -ENOMEM;
314 }
315
316 dht11 = iio_priv(iio);
317 dht11->dev = dev;
318
Harald Geyer5fbb0bc2015-07-07 13:39:30 +0000319 ret = of_get_gpio(node, 0);
Harald Geyer091a1212013-12-01 15:04:00 +0000320 if (ret < 0)
321 return ret;
Harald Geyer5fbb0bc2015-07-07 13:39:30 +0000322 dht11->gpio = ret;
Harald Geyer091a1212013-12-01 15:04:00 +0000323 ret = devm_gpio_request_one(dev, dht11->gpio, GPIOF_IN, pdev->name);
324 if (ret)
325 return ret;
326
327 dht11->irq = gpio_to_irq(dht11->gpio);
328 if (dht11->irq < 0) {
329 dev_err(dev, "GPIO %d has no interrupt\n", dht11->gpio);
330 return -EINVAL;
331 }
Harald Geyer091a1212013-12-01 15:04:00 +0000332
Abhilash Jindal17a2f462016-01-27 17:46:02 -0500333 dht11->timestamp = ktime_get_boot_ns() - DHT11_DATA_VALID_TIME - 1;
Harald Geyer091a1212013-12-01 15:04:00 +0000334 dht11->num_edges = -1;
335
336 platform_set_drvdata(pdev, iio);
337
338 init_completion(&dht11->completion);
Richard Weinberger004bc532015-01-07 13:18:23 +0100339 mutex_init(&dht11->lock);
Harald Geyer091a1212013-12-01 15:04:00 +0000340 iio->name = pdev->name;
341 iio->dev.parent = &pdev->dev;
342 iio->info = &dht11_iio_info;
343 iio->modes = INDIO_DIRECT_MODE;
344 iio->channels = dht11_chan_spec;
345 iio->num_channels = ARRAY_SIZE(dht11_chan_spec);
346
347 return devm_iio_device_register(dev, iio);
348}
349
350static struct platform_driver dht11_driver = {
351 .driver = {
352 .name = DRIVER_NAME,
Harald Geyer091a1212013-12-01 15:04:00 +0000353 .of_match_table = dht11_dt_ids,
354 },
355 .probe = dht11_probe,
356};
357
358module_platform_driver(dht11_driver);
359
360MODULE_AUTHOR("Harald Geyer <harald@ccbib.org>");
361MODULE_DESCRIPTION("DHT11 humidity/temperature sensor driver");
362MODULE_LICENSE("GPL v2");