blob: bb5ad57ed2a29feea0b06003c23c7a3e875a35b7 [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>
36
37#include <linux/iio/iio.h>
38
39#define DRIVER_NAME "dht11"
40
41#define DHT11_DATA_VALID_TIME 2000000000 /* 2s in ns */
42
Richard Weinberger94e65512015-01-07 13:22:49 +010043#define DHT11_EDGES_PREAMBLE 2
Harald Geyer091a1212013-12-01 15:04:00 +000044#define DHT11_BITS_PER_READ 40
Richard Weinberger94e65512015-01-07 13:22:49 +010045/*
46 * Note that when reading the sensor actually 84 edges are detected, but
47 * since the last edge is not significant, we only store 83:
48 */
Harald Geyer889c5e92015-07-07 13:39:28 +000049#define DHT11_EDGES_PER_READ (2 * DHT11_BITS_PER_READ + \
50 DHT11_EDGES_PREAMBLE + 1)
Harald Geyer091a1212013-12-01 15:04:00 +000051
52/* Data transmission timing (nano seconds) */
53#define DHT11_START_TRANSMISSION 18 /* ms */
54#define DHT11_SENSOR_RESPONSE 80000
55#define DHT11_START_BIT 50000
56#define DHT11_DATA_BIT_LOW 27000
57#define DHT11_DATA_BIT_HIGH 70000
58
59struct dht11 {
60 struct device *dev;
61
62 int gpio;
63 int irq;
64
65 struct completion completion;
Harald Geyera7126002015-07-07 13:39:29 +000066 /* The iio sysfs interface doesn't prevent concurrent reads: */
Richard Weinberger004bc532015-01-07 13:18:23 +010067 struct mutex lock;
Harald Geyer091a1212013-12-01 15:04:00 +000068
69 s64 timestamp;
70 int temperature;
71 int humidity;
72
73 /* num_edges: -1 means "no transmission in progress" */
74 int num_edges;
75 struct {s64 ts; int value; } edges[DHT11_EDGES_PER_READ];
76};
77
78static unsigned char dht11_decode_byte(int *timing, int threshold)
79{
80 unsigned char ret = 0;
81 int i;
82
83 for (i = 0; i < 8; ++i) {
84 ret <<= 1;
85 if (timing[i] >= threshold)
86 ++ret;
87 }
88
89 return ret;
90}
91
92static int dht11_decode(struct dht11 *dht11, int offset)
93{
94 int i, t, timing[DHT11_BITS_PER_READ], threshold,
95 timeres = DHT11_SENSOR_RESPONSE;
96 unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum;
97
98 /* Calculate timestamp resolution */
Richard Weinbergerddc25bd2015-01-07 13:15:22 +010099 for (i = 1; i < dht11->num_edges; ++i) {
Harald Geyer889c5e92015-07-07 13:39:28 +0000100 t = dht11->edges[i].ts - dht11->edges[i - 1].ts;
Harald Geyer091a1212013-12-01 15:04:00 +0000101 if (t > 0 && t < timeres)
102 timeres = t;
103 }
Harald Geyer889c5e92015-07-07 13:39:28 +0000104 if (2 * timeres > DHT11_DATA_BIT_HIGH) {
Harald Geyer091a1212013-12-01 15:04:00 +0000105 pr_err("dht11: timeresolution %d too bad for decoding\n",
Harald Geyer889c5e92015-07-07 13:39:28 +0000106 timeres);
Harald Geyer091a1212013-12-01 15:04:00 +0000107 return -EIO;
108 }
109 threshold = DHT11_DATA_BIT_HIGH / timeres;
Harald Geyer889c5e92015-07-07 13:39:28 +0000110 if (DHT11_DATA_BIT_LOW / timeres + 1 >= threshold)
Harald Geyer091a1212013-12-01 15:04:00 +0000111 pr_err("dht11: WARNING: decoding ambiguous\n");
112
113 /* scale down with timeres and check validity */
114 for (i = 0; i < DHT11_BITS_PER_READ; ++i) {
Harald Geyer889c5e92015-07-07 13:39:28 +0000115 t = dht11->edges[offset + 2 * i + 2].ts -
116 dht11->edges[offset + 2 * i + 1].ts;
117 if (!dht11->edges[offset + 2 * i + 1].value)
Harald Geyer091a1212013-12-01 15:04:00 +0000118 return -EIO; /* lost synchronisation */
119 timing[i] = t / timeres;
120 }
121
122 hum_int = dht11_decode_byte(timing, threshold);
123 hum_dec = dht11_decode_byte(&timing[8], threshold);
124 temp_int = dht11_decode_byte(&timing[16], threshold);
125 temp_dec = dht11_decode_byte(&timing[24], threshold);
126 checksum = dht11_decode_byte(&timing[32], threshold);
127
128 if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum)
129 return -EIO;
130
131 dht11->timestamp = iio_get_time_ns();
132 if (hum_int < 20) { /* DHT22 */
133 dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
134 ((temp_int & 0x80) ? -100 : 100);
135 dht11->humidity = ((hum_int << 8) + hum_dec) * 100;
136 } else if (temp_dec == 0 && hum_dec == 0) { /* DHT11 */
137 dht11->temperature = temp_int * 1000;
138 dht11->humidity = hum_int * 1000;
139 } else {
140 dev_err(dht11->dev,
141 "Don't know how to decode data: %d %d %d %d\n",
142 hum_int, hum_dec, temp_int, temp_dec);
143 return -EIO;
144 }
145
146 return 0;
147}
148
Richard Weinberger94e65512015-01-07 13:22:49 +0100149/*
150 * IRQ handler called on GPIO edges
151 */
152static irqreturn_t dht11_handle_irq(int irq, void *data)
153{
154 struct iio_dev *iio = data;
155 struct dht11 *dht11 = iio_priv(iio);
156
157 /* TODO: Consider making the handler safe for IRQ sharing */
158 if (dht11->num_edges < DHT11_EDGES_PER_READ && dht11->num_edges >= 0) {
159 dht11->edges[dht11->num_edges].ts = iio_get_time_ns();
160 dht11->edges[dht11->num_edges++].value =
161 gpio_get_value(dht11->gpio);
162
163 if (dht11->num_edges >= DHT11_EDGES_PER_READ)
164 complete(&dht11->completion);
165 }
166
167 return IRQ_HANDLED;
168}
169
Harald Geyer091a1212013-12-01 15:04:00 +0000170static int dht11_read_raw(struct iio_dev *iio_dev,
Harald Geyer889c5e92015-07-07 13:39:28 +0000171 const struct iio_chan_spec *chan,
Harald Geyer091a1212013-12-01 15:04:00 +0000172 int *val, int *val2, long m)
173{
174 struct dht11 *dht11 = iio_priv(iio_dev);
175 int ret;
176
Richard Weinberger004bc532015-01-07 13:18:23 +0100177 mutex_lock(&dht11->lock);
Harald Geyer091a1212013-12-01 15:04:00 +0000178 if (dht11->timestamp + DHT11_DATA_VALID_TIME < iio_get_time_ns()) {
179 reinit_completion(&dht11->completion);
180
181 dht11->num_edges = 0;
182 ret = gpio_direction_output(dht11->gpio, 0);
183 if (ret)
184 goto err;
185 msleep(DHT11_START_TRANSMISSION);
186 ret = gpio_direction_input(dht11->gpio);
187 if (ret)
188 goto err;
189
Richard Weinberger94e65512015-01-07 13:22:49 +0100190 ret = request_irq(dht11->irq, dht11_handle_irq,
191 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
192 iio_dev->name, iio_dev);
193 if (ret)
194 goto err;
195
Harald Geyer091a1212013-12-01 15:04:00 +0000196 ret = wait_for_completion_killable_timeout(&dht11->completion,
Harald Geyer889c5e92015-07-07 13:39:28 +0000197 HZ);
Richard Weinberger94e65512015-01-07 13:22:49 +0100198
199 free_irq(dht11->irq, iio_dev);
200
Harald Geyer091a1212013-12-01 15:04:00 +0000201 if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
202 dev_err(&iio_dev->dev,
Harald Geyer889c5e92015-07-07 13:39:28 +0000203 "Only %d signal edges detected\n",
Harald Geyer091a1212013-12-01 15:04:00 +0000204 dht11->num_edges);
205 ret = -ETIMEDOUT;
206 }
207 if (ret < 0)
208 goto err;
209
210 ret = dht11_decode(dht11,
Harald Geyer889c5e92015-07-07 13:39:28 +0000211 dht11->num_edges == DHT11_EDGES_PER_READ ?
Harald Geyer091a1212013-12-01 15:04:00 +0000212 DHT11_EDGES_PREAMBLE :
213 DHT11_EDGES_PREAMBLE - 2);
214 if (ret)
215 goto err;
216 }
217
218 ret = IIO_VAL_INT;
219 if (chan->type == IIO_TEMP)
220 *val = dht11->temperature;
221 else if (chan->type == IIO_HUMIDITYRELATIVE)
222 *val = dht11->humidity;
223 else
224 ret = -EINVAL;
225err:
226 dht11->num_edges = -1;
Richard Weinberger004bc532015-01-07 13:18:23 +0100227 mutex_unlock(&dht11->lock);
Harald Geyer091a1212013-12-01 15:04:00 +0000228 return ret;
229}
230
231static const struct iio_info dht11_iio_info = {
232 .driver_module = THIS_MODULE,
233 .read_raw = dht11_read_raw,
234};
235
Harald Geyer091a1212013-12-01 15:04:00 +0000236static const struct iio_chan_spec dht11_chan_spec[] = {
237 { .type = IIO_TEMP,
238 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), },
239 { .type = IIO_HUMIDITYRELATIVE,
240 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), }
241};
242
243static const struct of_device_id dht11_dt_ids[] = {
244 { .compatible = "dht11", },
245 { }
246};
247MODULE_DEVICE_TABLE(of, dht11_dt_ids);
248
249static int dht11_probe(struct platform_device *pdev)
250{
251 struct device *dev = &pdev->dev;
252 struct device_node *node = dev->of_node;
253 struct dht11 *dht11;
254 struct iio_dev *iio;
255 int ret;
256
257 iio = devm_iio_device_alloc(dev, sizeof(*dht11));
258 if (!iio) {
259 dev_err(dev, "Failed to allocate IIO device\n");
260 return -ENOMEM;
261 }
262
263 dht11 = iio_priv(iio);
264 dht11->dev = dev;
265
266 dht11->gpio = ret = of_get_gpio(node, 0);
267 if (ret < 0)
268 return ret;
269 ret = devm_gpio_request_one(dev, dht11->gpio, GPIOF_IN, pdev->name);
270 if (ret)
271 return ret;
272
273 dht11->irq = gpio_to_irq(dht11->gpio);
274 if (dht11->irq < 0) {
275 dev_err(dev, "GPIO %d has no interrupt\n", dht11->gpio);
276 return -EINVAL;
277 }
Harald Geyer091a1212013-12-01 15:04:00 +0000278
279 dht11->timestamp = iio_get_time_ns() - DHT11_DATA_VALID_TIME - 1;
280 dht11->num_edges = -1;
281
282 platform_set_drvdata(pdev, iio);
283
284 init_completion(&dht11->completion);
Richard Weinberger004bc532015-01-07 13:18:23 +0100285 mutex_init(&dht11->lock);
Harald Geyer091a1212013-12-01 15:04:00 +0000286 iio->name = pdev->name;
287 iio->dev.parent = &pdev->dev;
288 iio->info = &dht11_iio_info;
289 iio->modes = INDIO_DIRECT_MODE;
290 iio->channels = dht11_chan_spec;
291 iio->num_channels = ARRAY_SIZE(dht11_chan_spec);
292
293 return devm_iio_device_register(dev, iio);
294}
295
296static struct platform_driver dht11_driver = {
297 .driver = {
298 .name = DRIVER_NAME,
Harald Geyer091a1212013-12-01 15:04:00 +0000299 .of_match_table = dht11_dt_ids,
300 },
301 .probe = dht11_probe,
302};
303
304module_platform_driver(dht11_driver);
305
306MODULE_AUTHOR("Harald Geyer <harald@ccbib.org>");
307MODULE_DESCRIPTION("DHT11 humidity/temperature sensor driver");
308MODULE_LICENSE("GPL v2");