xref: /openbmc/linux/drivers/iio/humidity/dht11.c (revision 17a2f46c)
1091a121bSHarald Geyer /*
2091a121bSHarald Geyer  * DHT11/DHT22 bit banging GPIO driver
3091a121bSHarald Geyer  *
4091a121bSHarald Geyer  * Copyright (c) Harald Geyer <harald@ccbib.org>
5091a121bSHarald Geyer  *
6091a121bSHarald Geyer  * This program is free software; you can redistribute it and/or modify
7091a121bSHarald Geyer  * it under the terms of the GNU General Public License as published by
8091a121bSHarald Geyer  * the Free Software Foundation; either version 2 of the License, or
9091a121bSHarald Geyer  * (at your option) any later version.
10091a121bSHarald Geyer  *
11091a121bSHarald Geyer  * This program is distributed in the hope that it will be useful,
12091a121bSHarald Geyer  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13091a121bSHarald Geyer  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14091a121bSHarald Geyer  * GNU General Public License for more details.
15091a121bSHarald Geyer  */
16091a121bSHarald Geyer 
17091a121bSHarald Geyer #include <linux/err.h>
18091a121bSHarald Geyer #include <linux/interrupt.h>
19091a121bSHarald Geyer #include <linux/device.h>
20091a121bSHarald Geyer #include <linux/kernel.h>
21091a121bSHarald Geyer #include <linux/printk.h>
22091a121bSHarald Geyer #include <linux/slab.h>
23091a121bSHarald Geyer #include <linux/of.h>
24091a121bSHarald Geyer #include <linux/of_device.h>
25091a121bSHarald Geyer #include <linux/sysfs.h>
26091a121bSHarald Geyer #include <linux/io.h>
27091a121bSHarald Geyer #include <linux/module.h>
28091a121bSHarald Geyer #include <linux/platform_device.h>
29091a121bSHarald Geyer #include <linux/wait.h>
30091a121bSHarald Geyer #include <linux/bitops.h>
31091a121bSHarald Geyer #include <linux/completion.h>
32004bc530SRichard Weinberger #include <linux/mutex.h>
33091a121bSHarald Geyer #include <linux/delay.h>
34091a121bSHarald Geyer #include <linux/gpio.h>
35091a121bSHarald Geyer #include <linux/of_gpio.h>
36081d9740SHarald Geyer #include <linux/timekeeping.h>
37091a121bSHarald Geyer 
38091a121bSHarald Geyer #include <linux/iio/iio.h>
39091a121bSHarald Geyer 
40091a121bSHarald Geyer #define DRIVER_NAME	"dht11"
41091a121bSHarald Geyer 
42091a121bSHarald Geyer #define DHT11_DATA_VALID_TIME	2000000000  /* 2s in ns */
43091a121bSHarald Geyer 
4494e65519SRichard Weinberger #define DHT11_EDGES_PREAMBLE 2
45091a121bSHarald Geyer #define DHT11_BITS_PER_READ 40
4694e65519SRichard Weinberger /*
4794e65519SRichard Weinberger  * Note that when reading the sensor actually 84 edges are detected, but
4894e65519SRichard Weinberger  * since the last edge is not significant, we only store 83:
4994e65519SRichard Weinberger  */
50889c5e9bSHarald Geyer #define DHT11_EDGES_PER_READ (2 * DHT11_BITS_PER_READ + \
51889c5e9bSHarald Geyer 			      DHT11_EDGES_PREAMBLE + 1)
52091a121bSHarald Geyer 
53091a121bSHarald Geyer /* Data transmission timing (nano seconds) */
54091a121bSHarald Geyer #define DHT11_START_TRANSMISSION	18  /* ms */
55091a121bSHarald Geyer #define DHT11_SENSOR_RESPONSE	80000
56091a121bSHarald Geyer #define DHT11_START_BIT		50000
57091a121bSHarald Geyer #define DHT11_DATA_BIT_LOW	27000
58091a121bSHarald Geyer #define DHT11_DATA_BIT_HIGH	70000
59091a121bSHarald Geyer 
60091a121bSHarald Geyer struct dht11 {
61091a121bSHarald Geyer 	struct device			*dev;
62091a121bSHarald Geyer 
63091a121bSHarald Geyer 	int				gpio;
64091a121bSHarald Geyer 	int				irq;
65091a121bSHarald Geyer 
66091a121bSHarald Geyer 	struct completion		completion;
67a7126003SHarald Geyer 	/* The iio sysfs interface doesn't prevent concurrent reads: */
68004bc530SRichard Weinberger 	struct mutex			lock;
69091a121bSHarald Geyer 
70091a121bSHarald Geyer 	s64				timestamp;
71091a121bSHarald Geyer 	int				temperature;
72091a121bSHarald Geyer 	int				humidity;
73091a121bSHarald Geyer 
74091a121bSHarald Geyer 	/* num_edges: -1 means "no transmission in progress" */
75091a121bSHarald Geyer 	int				num_edges;
76091a121bSHarald Geyer 	struct {s64 ts; int value; }	edges[DHT11_EDGES_PER_READ];
77091a121bSHarald Geyer };
78091a121bSHarald Geyer 
79091a121bSHarald Geyer static unsigned char dht11_decode_byte(int *timing, int threshold)
80091a121bSHarald Geyer {
81091a121bSHarald Geyer 	unsigned char ret = 0;
82091a121bSHarald Geyer 	int i;
83091a121bSHarald Geyer 
84091a121bSHarald Geyer 	for (i = 0; i < 8; ++i) {
85091a121bSHarald Geyer 		ret <<= 1;
86091a121bSHarald Geyer 		if (timing[i] >= threshold)
87091a121bSHarald Geyer 			++ret;
88091a121bSHarald Geyer 	}
89091a121bSHarald Geyer 
90091a121bSHarald Geyer 	return ret;
91091a121bSHarald Geyer }
92091a121bSHarald Geyer 
93081d9740SHarald Geyer static int dht11_decode(struct dht11 *dht11, int offset, int timeres)
94091a121bSHarald Geyer {
95081d9740SHarald Geyer 	int i, t, timing[DHT11_BITS_PER_READ], threshold;
96091a121bSHarald Geyer 	unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum;
97091a121bSHarald Geyer 
98091a121bSHarald Geyer 	threshold = DHT11_DATA_BIT_HIGH / timeres;
99091a121bSHarald Geyer 	if (DHT11_DATA_BIT_LOW / timeres + 1 >= threshold)
100091a121bSHarald Geyer 		pr_err("dht11: WARNING: decoding ambiguous\n");
101091a121bSHarald Geyer 
102091a121bSHarald Geyer 	/* scale down with timeres and check validity */
103091a121bSHarald Geyer 	for (i = 0; i < DHT11_BITS_PER_READ; ++i) {
104091a121bSHarald Geyer 		t = dht11->edges[offset + 2 * i + 2].ts -
105091a121bSHarald Geyer 			dht11->edges[offset + 2 * i + 1].ts;
106091a121bSHarald Geyer 		if (!dht11->edges[offset + 2 * i + 1].value)
107091a121bSHarald Geyer 			return -EIO;  /* lost synchronisation */
108091a121bSHarald Geyer 		timing[i] = t / timeres;
109091a121bSHarald Geyer 	}
110091a121bSHarald Geyer 
111091a121bSHarald Geyer 	hum_int = dht11_decode_byte(timing, threshold);
112091a121bSHarald Geyer 	hum_dec = dht11_decode_byte(&timing[8], threshold);
113091a121bSHarald Geyer 	temp_int = dht11_decode_byte(&timing[16], threshold);
114091a121bSHarald Geyer 	temp_dec = dht11_decode_byte(&timing[24], threshold);
115091a121bSHarald Geyer 	checksum = dht11_decode_byte(&timing[32], threshold);
116091a121bSHarald Geyer 
117091a121bSHarald Geyer 	if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum)
118091a121bSHarald Geyer 		return -EIO;
119091a121bSHarald Geyer 
12017a2f46cSAbhilash Jindal 	dht11->timestamp = ktime_get_boot_ns();
121091a121bSHarald Geyer 	if (hum_int < 20) {  /* DHT22 */
122091a121bSHarald Geyer 		dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
123091a121bSHarald Geyer 					((temp_int & 0x80) ? -100 : 100);
124091a121bSHarald Geyer 		dht11->humidity = ((hum_int << 8) + hum_dec) * 100;
125091a121bSHarald Geyer 	} else if (temp_dec == 0 && hum_dec == 0) {  /* DHT11 */
126091a121bSHarald Geyer 		dht11->temperature = temp_int * 1000;
127091a121bSHarald Geyer 		dht11->humidity = hum_int * 1000;
128091a121bSHarald Geyer 	} else {
129091a121bSHarald Geyer 		dev_err(dht11->dev,
130091a121bSHarald Geyer 			"Don't know how to decode data: %d %d %d %d\n",
131091a121bSHarald Geyer 			hum_int, hum_dec, temp_int, temp_dec);
132091a121bSHarald Geyer 		return -EIO;
133091a121bSHarald Geyer 	}
134091a121bSHarald Geyer 
135091a121bSHarald Geyer 	return 0;
136091a121bSHarald Geyer }
137091a121bSHarald Geyer 
13894e65519SRichard Weinberger /*
13994e65519SRichard Weinberger  * IRQ handler called on GPIO edges
14094e65519SRichard Weinberger  */
14194e65519SRichard Weinberger static irqreturn_t dht11_handle_irq(int irq, void *data)
14294e65519SRichard Weinberger {
14394e65519SRichard Weinberger 	struct iio_dev *iio = data;
14494e65519SRichard Weinberger 	struct dht11 *dht11 = iio_priv(iio);
14594e65519SRichard Weinberger 
14694e65519SRichard Weinberger 	/* TODO: Consider making the handler safe for IRQ sharing */
14794e65519SRichard Weinberger 	if (dht11->num_edges < DHT11_EDGES_PER_READ && dht11->num_edges >= 0) {
14817a2f46cSAbhilash Jindal 		dht11->edges[dht11->num_edges].ts = ktime_get_boot_ns();
14994e65519SRichard Weinberger 		dht11->edges[dht11->num_edges++].value =
15094e65519SRichard Weinberger 						gpio_get_value(dht11->gpio);
15194e65519SRichard Weinberger 
15294e65519SRichard Weinberger 		if (dht11->num_edges >= DHT11_EDGES_PER_READ)
15394e65519SRichard Weinberger 			complete(&dht11->completion);
15494e65519SRichard Weinberger 	}
15594e65519SRichard Weinberger 
15694e65519SRichard Weinberger 	return IRQ_HANDLED;
15794e65519SRichard Weinberger }
15894e65519SRichard Weinberger 
159091a121bSHarald Geyer static int dht11_read_raw(struct iio_dev *iio_dev,
160091a121bSHarald Geyer 			  const struct iio_chan_spec *chan,
161091a121bSHarald Geyer 			int *val, int *val2, long m)
162091a121bSHarald Geyer {
163091a121bSHarald Geyer 	struct dht11 *dht11 = iio_priv(iio_dev);
164081d9740SHarald Geyer 	int ret, timeres;
165091a121bSHarald Geyer 
166004bc530SRichard Weinberger 	mutex_lock(&dht11->lock);
16717a2f46cSAbhilash Jindal 	if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boot_ns()) {
168081d9740SHarald Geyer 		timeres = ktime_get_resolution_ns();
169081d9740SHarald Geyer 		if (DHT11_DATA_BIT_HIGH < 2 * timeres) {
170081d9740SHarald Geyer 			dev_err(dht11->dev, "timeresolution %dns too low\n",
171081d9740SHarald Geyer 				timeres);
172081d9740SHarald Geyer 			/* In theory a better clock could become available
173081d9740SHarald Geyer 			 * at some point ... and there is no error code
174081d9740SHarald Geyer 			 * that really fits better.
175081d9740SHarald Geyer 			 */
176081d9740SHarald Geyer 			ret = -EAGAIN;
177081d9740SHarald Geyer 			goto err;
178081d9740SHarald Geyer 		}
179081d9740SHarald Geyer 
180091a121bSHarald Geyer 		reinit_completion(&dht11->completion);
181091a121bSHarald Geyer 
182091a121bSHarald Geyer 		dht11->num_edges = 0;
183091a121bSHarald Geyer 		ret = gpio_direction_output(dht11->gpio, 0);
184091a121bSHarald Geyer 		if (ret)
185091a121bSHarald Geyer 			goto err;
186091a121bSHarald Geyer 		msleep(DHT11_START_TRANSMISSION);
187091a121bSHarald Geyer 		ret = gpio_direction_input(dht11->gpio);
188091a121bSHarald Geyer 		if (ret)
189091a121bSHarald Geyer 			goto err;
190091a121bSHarald Geyer 
19194e65519SRichard Weinberger 		ret = request_irq(dht11->irq, dht11_handle_irq,
19294e65519SRichard Weinberger 				  IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
19394e65519SRichard Weinberger 				  iio_dev->name, iio_dev);
19494e65519SRichard Weinberger 		if (ret)
19594e65519SRichard Weinberger 			goto err;
19694e65519SRichard Weinberger 
197091a121bSHarald Geyer 		ret = wait_for_completion_killable_timeout(&dht11->completion,
198091a121bSHarald Geyer 							   HZ);
19994e65519SRichard Weinberger 
20094e65519SRichard Weinberger 		free_irq(dht11->irq, iio_dev);
20194e65519SRichard Weinberger 
202091a121bSHarald Geyer 		if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
203091a121bSHarald Geyer 			dev_err(&iio_dev->dev,
204091a121bSHarald Geyer 				"Only %d signal edges detected\n",
205091a121bSHarald Geyer 					dht11->num_edges);
206091a121bSHarald Geyer 			ret = -ETIMEDOUT;
207091a121bSHarald Geyer 		}
208091a121bSHarald Geyer 		if (ret < 0)
209091a121bSHarald Geyer 			goto err;
210091a121bSHarald Geyer 
211091a121bSHarald Geyer 		ret = dht11_decode(dht11,
212091a121bSHarald Geyer 				   dht11->num_edges == DHT11_EDGES_PER_READ ?
213091a121bSHarald Geyer 					DHT11_EDGES_PREAMBLE :
214081d9740SHarald Geyer 					DHT11_EDGES_PREAMBLE - 2,
215081d9740SHarald Geyer 				timeres);
216091a121bSHarald Geyer 		if (ret)
217091a121bSHarald Geyer 			goto err;
218091a121bSHarald Geyer 	}
219091a121bSHarald Geyer 
220091a121bSHarald Geyer 	ret = IIO_VAL_INT;
221091a121bSHarald Geyer 	if (chan->type == IIO_TEMP)
222091a121bSHarald Geyer 		*val = dht11->temperature;
223091a121bSHarald Geyer 	else if (chan->type == IIO_HUMIDITYRELATIVE)
224091a121bSHarald Geyer 		*val = dht11->humidity;
225091a121bSHarald Geyer 	else
226091a121bSHarald Geyer 		ret = -EINVAL;
227091a121bSHarald Geyer err:
228091a121bSHarald Geyer 	dht11->num_edges = -1;
229004bc530SRichard Weinberger 	mutex_unlock(&dht11->lock);
230091a121bSHarald Geyer 	return ret;
231091a121bSHarald Geyer }
232091a121bSHarald Geyer 
233091a121bSHarald Geyer static const struct iio_info dht11_iio_info = {
234091a121bSHarald Geyer 	.driver_module		= THIS_MODULE,
235091a121bSHarald Geyer 	.read_raw		= dht11_read_raw,
236091a121bSHarald Geyer };
237091a121bSHarald Geyer 
238091a121bSHarald Geyer static const struct iio_chan_spec dht11_chan_spec[] = {
239091a121bSHarald Geyer 	{ .type = IIO_TEMP,
240091a121bSHarald Geyer 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), },
241091a121bSHarald Geyer 	{ .type = IIO_HUMIDITYRELATIVE,
242091a121bSHarald Geyer 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), }
243091a121bSHarald Geyer };
244091a121bSHarald Geyer 
245091a121bSHarald Geyer static const struct of_device_id dht11_dt_ids[] = {
246091a121bSHarald Geyer 	{ .compatible = "dht11", },
247091a121bSHarald Geyer 	{ }
248091a121bSHarald Geyer };
249091a121bSHarald Geyer MODULE_DEVICE_TABLE(of, dht11_dt_ids);
250091a121bSHarald Geyer 
251091a121bSHarald Geyer static int dht11_probe(struct platform_device *pdev)
252091a121bSHarald Geyer {
253091a121bSHarald Geyer 	struct device *dev = &pdev->dev;
254091a121bSHarald Geyer 	struct device_node *node = dev->of_node;
255091a121bSHarald Geyer 	struct dht11 *dht11;
256091a121bSHarald Geyer 	struct iio_dev *iio;
257091a121bSHarald Geyer 	int ret;
258091a121bSHarald Geyer 
259091a121bSHarald Geyer 	iio = devm_iio_device_alloc(dev, sizeof(*dht11));
260091a121bSHarald Geyer 	if (!iio) {
261091a121bSHarald Geyer 		dev_err(dev, "Failed to allocate IIO device\n");
262091a121bSHarald Geyer 		return -ENOMEM;
263091a121bSHarald Geyer 	}
264091a121bSHarald Geyer 
265091a121bSHarald Geyer 	dht11 = iio_priv(iio);
266091a121bSHarald Geyer 	dht11->dev = dev;
267091a121bSHarald Geyer 
2685fbb0bc4SHarald Geyer 	ret = of_get_gpio(node, 0);
269091a121bSHarald Geyer 	if (ret < 0)
270091a121bSHarald Geyer 		return ret;
2715fbb0bc4SHarald Geyer 	dht11->gpio = ret;
272091a121bSHarald Geyer 	ret = devm_gpio_request_one(dev, dht11->gpio, GPIOF_IN, pdev->name);
273091a121bSHarald Geyer 	if (ret)
274091a121bSHarald Geyer 		return ret;
275091a121bSHarald Geyer 
276091a121bSHarald Geyer 	dht11->irq = gpio_to_irq(dht11->gpio);
277091a121bSHarald Geyer 	if (dht11->irq < 0) {
278091a121bSHarald Geyer 		dev_err(dev, "GPIO %d has no interrupt\n", dht11->gpio);
279091a121bSHarald Geyer 		return -EINVAL;
280091a121bSHarald Geyer 	}
281091a121bSHarald Geyer 
28217a2f46cSAbhilash Jindal 	dht11->timestamp = ktime_get_boot_ns() - DHT11_DATA_VALID_TIME - 1;
283091a121bSHarald Geyer 	dht11->num_edges = -1;
284091a121bSHarald Geyer 
285091a121bSHarald Geyer 	platform_set_drvdata(pdev, iio);
286091a121bSHarald Geyer 
287091a121bSHarald Geyer 	init_completion(&dht11->completion);
288004bc530SRichard Weinberger 	mutex_init(&dht11->lock);
289091a121bSHarald Geyer 	iio->name = pdev->name;
290091a121bSHarald Geyer 	iio->dev.parent = &pdev->dev;
291091a121bSHarald Geyer 	iio->info = &dht11_iio_info;
292091a121bSHarald Geyer 	iio->modes = INDIO_DIRECT_MODE;
293091a121bSHarald Geyer 	iio->channels = dht11_chan_spec;
294091a121bSHarald Geyer 	iio->num_channels = ARRAY_SIZE(dht11_chan_spec);
295091a121bSHarald Geyer 
296091a121bSHarald Geyer 	return devm_iio_device_register(dev, iio);
297091a121bSHarald Geyer }
298091a121bSHarald Geyer 
299091a121bSHarald Geyer static struct platform_driver dht11_driver = {
300091a121bSHarald Geyer 	.driver = {
301091a121bSHarald Geyer 		.name	= DRIVER_NAME,
302091a121bSHarald Geyer 		.of_match_table = dht11_dt_ids,
303091a121bSHarald Geyer 	},
304091a121bSHarald Geyer 	.probe  = dht11_probe,
305091a121bSHarald Geyer };
306091a121bSHarald Geyer 
307091a121bSHarald Geyer module_platform_driver(dht11_driver);
308091a121bSHarald Geyer 
309091a121bSHarald Geyer MODULE_AUTHOR("Harald Geyer <harald@ccbib.org>");
310091a121bSHarald Geyer MODULE_DESCRIPTION("DHT11 humidity/temperature sensor driver");
311091a121bSHarald Geyer MODULE_LICENSE("GPL v2");
312