xref: /openbmc/linux/drivers/iio/humidity/dht11.c (revision c942fddf)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2091a121bSHarald Geyer /*
3091a121bSHarald Geyer  * DHT11/DHT22 bit banging GPIO driver
4091a121bSHarald Geyer  *
5091a121bSHarald Geyer  * Copyright (c) Harald Geyer <harald@ccbib.org>
6091a121bSHarald Geyer  */
7091a121bSHarald Geyer 
8091a121bSHarald Geyer #include <linux/err.h>
9091a121bSHarald Geyer #include <linux/interrupt.h>
10091a121bSHarald Geyer #include <linux/device.h>
11091a121bSHarald Geyer #include <linux/kernel.h>
12091a121bSHarald Geyer #include <linux/printk.h>
13091a121bSHarald Geyer #include <linux/slab.h>
14091a121bSHarald Geyer #include <linux/of.h>
15091a121bSHarald Geyer #include <linux/of_device.h>
16091a121bSHarald Geyer #include <linux/sysfs.h>
17091a121bSHarald Geyer #include <linux/io.h>
18091a121bSHarald Geyer #include <linux/module.h>
19091a121bSHarald Geyer #include <linux/platform_device.h>
20091a121bSHarald Geyer #include <linux/wait.h>
21091a121bSHarald Geyer #include <linux/bitops.h>
22091a121bSHarald Geyer #include <linux/completion.h>
23004bc530SRichard Weinberger #include <linux/mutex.h>
24091a121bSHarald Geyer #include <linux/delay.h>
25091a121bSHarald Geyer #include <linux/gpio.h>
26091a121bSHarald Geyer #include <linux/of_gpio.h>
27081d9740SHarald Geyer #include <linux/timekeeping.h>
28091a121bSHarald Geyer 
29091a121bSHarald Geyer #include <linux/iio/iio.h>
30091a121bSHarald Geyer 
31091a121bSHarald Geyer #define DRIVER_NAME	"dht11"
32091a121bSHarald Geyer 
33091a121bSHarald Geyer #define DHT11_DATA_VALID_TIME	2000000000  /* 2s in ns */
34091a121bSHarald Geyer 
3594e65519SRichard Weinberger #define DHT11_EDGES_PREAMBLE 2
36091a121bSHarald Geyer #define DHT11_BITS_PER_READ 40
3794e65519SRichard Weinberger /*
3894e65519SRichard Weinberger  * Note that when reading the sensor actually 84 edges are detected, but
3994e65519SRichard Weinberger  * since the last edge is not significant, we only store 83:
4094e65519SRichard Weinberger  */
41889c5e9bSHarald Geyer #define DHT11_EDGES_PER_READ (2 * DHT11_BITS_PER_READ + \
42889c5e9bSHarald Geyer 			      DHT11_EDGES_PREAMBLE + 1)
43091a121bSHarald Geyer 
44155a5759SHarald Geyer /*
45155a5759SHarald Geyer  * Data transmission timing:
46155a5759SHarald Geyer  * Data bits are encoded as pulse length (high time) on the data line.
47155a5759SHarald Geyer  * 0-bit: 22-30uS -- typically 26uS (AM2302)
48155a5759SHarald Geyer  * 1-bit: 68-75uS -- typically 70uS (AM2302)
49155a5759SHarald Geyer  * The acutal timings also depend on the properties of the cable, with
50155a5759SHarald Geyer  * longer cables typically making pulses shorter.
51155a5759SHarald Geyer  *
52155a5759SHarald Geyer  * Our decoding depends on the time resolution of the system:
53155a5759SHarald Geyer  * timeres > 34uS ... don't know what a 1-tick pulse is
54155a5759SHarald Geyer  * 34uS > timeres > 30uS ... no problem (30kHz and 32kHz clocks)
55155a5759SHarald Geyer  * 30uS > timeres > 23uS ... don't know what a 2-tick pulse is
56155a5759SHarald Geyer  * timeres < 23uS ... no problem
57155a5759SHarald Geyer  *
58155a5759SHarald Geyer  * Luckily clocks in the 33-44kHz range are quite uncommon, so we can
59155a5759SHarald Geyer  * support most systems if the threshold for decoding a pulse as 1-bit
60155a5759SHarald Geyer  * is chosen carefully. If somebody really wants to support clocks around
61155a5759SHarald Geyer  * 40kHz, where this driver is most unreliable, there are two options.
62155a5759SHarald Geyer  * a) select an implementation using busy loop polling on those systems
63155a5759SHarald Geyer  * b) use the checksum to do some probabilistic decoding
64155a5759SHarald Geyer  */
655c113b5eSJohn Brooks #define DHT11_START_TRANSMISSION_MIN	18000  /* us */
665c113b5eSJohn Brooks #define DHT11_START_TRANSMISSION_MAX	20000  /* us */
67155a5759SHarald Geyer #define DHT11_MIN_TIMERES	34000  /* ns */
68155a5759SHarald Geyer #define DHT11_THRESHOLD		49000  /* ns */
69155a5759SHarald Geyer #define DHT11_AMBIG_LOW		23000  /* ns */
70155a5759SHarald Geyer #define DHT11_AMBIG_HIGH	30000  /* ns */
71091a121bSHarald Geyer 
72091a121bSHarald Geyer struct dht11 {
73091a121bSHarald Geyer 	struct device			*dev;
74091a121bSHarald Geyer 
75091a121bSHarald Geyer 	int				gpio;
76091a121bSHarald Geyer 	int				irq;
77091a121bSHarald Geyer 
78091a121bSHarald Geyer 	struct completion		completion;
79a7126003SHarald Geyer 	/* The iio sysfs interface doesn't prevent concurrent reads: */
80004bc530SRichard Weinberger 	struct mutex			lock;
81091a121bSHarald Geyer 
82091a121bSHarald Geyer 	s64				timestamp;
83091a121bSHarald Geyer 	int				temperature;
84091a121bSHarald Geyer 	int				humidity;
85091a121bSHarald Geyer 
86091a121bSHarald Geyer 	/* num_edges: -1 means "no transmission in progress" */
87091a121bSHarald Geyer 	int				num_edges;
88091a121bSHarald Geyer 	struct {s64 ts; int value; }	edges[DHT11_EDGES_PER_READ];
89091a121bSHarald Geyer };
90091a121bSHarald Geyer 
91ab4b6496SHarald Geyer #ifdef CONFIG_DYNAMIC_DEBUG
92ab4b6496SHarald Geyer /*
93ab4b6496SHarald Geyer  * dht11_edges_print: show the data as actually received by the
94ab4b6496SHarald Geyer  *                    driver.
95ab4b6496SHarald Geyer  */
96ab4b6496SHarald Geyer static void dht11_edges_print(struct dht11 *dht11)
97ab4b6496SHarald Geyer {
98ab4b6496SHarald Geyer 	int i;
99ab4b6496SHarald Geyer 
100ab4b6496SHarald Geyer 	dev_dbg(dht11->dev, "%d edges detected:\n", dht11->num_edges);
101ab4b6496SHarald Geyer 	for (i = 1; i < dht11->num_edges; ++i) {
102ab4b6496SHarald Geyer 		dev_dbg(dht11->dev, "%d: %lld ns %s\n", i,
103ab4b6496SHarald Geyer 			dht11->edges[i].ts - dht11->edges[i - 1].ts,
104ab4b6496SHarald Geyer 			dht11->edges[i - 1].value ? "high" : "low");
105ab4b6496SHarald Geyer 	}
106ab4b6496SHarald Geyer }
107ab4b6496SHarald Geyer #endif /* CONFIG_DYNAMIC_DEBUG */
108ab4b6496SHarald Geyer 
109155a5759SHarald Geyer static unsigned char dht11_decode_byte(char *bits)
110091a121bSHarald Geyer {
111091a121bSHarald Geyer 	unsigned char ret = 0;
112091a121bSHarald Geyer 	int i;
113091a121bSHarald Geyer 
114091a121bSHarald Geyer 	for (i = 0; i < 8; ++i) {
115091a121bSHarald Geyer 		ret <<= 1;
116155a5759SHarald Geyer 		if (bits[i])
117091a121bSHarald Geyer 			++ret;
118091a121bSHarald Geyer 	}
119091a121bSHarald Geyer 
120091a121bSHarald Geyer 	return ret;
121091a121bSHarald Geyer }
122091a121bSHarald Geyer 
123155a5759SHarald Geyer static int dht11_decode(struct dht11 *dht11, int offset)
124091a121bSHarald Geyer {
125155a5759SHarald Geyer 	int i, t;
126155a5759SHarald Geyer 	char bits[DHT11_BITS_PER_READ];
127091a121bSHarald Geyer 	unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum;
128091a121bSHarald Geyer 
129091a121bSHarald Geyer 	for (i = 0; i < DHT11_BITS_PER_READ; ++i) {
130091a121bSHarald Geyer 		t = dht11->edges[offset + 2 * i + 2].ts -
131091a121bSHarald Geyer 			dht11->edges[offset + 2 * i + 1].ts;
132ab4b6496SHarald Geyer 		if (!dht11->edges[offset + 2 * i + 1].value) {
133ab4b6496SHarald Geyer 			dev_dbg(dht11->dev,
134ab4b6496SHarald Geyer 				"lost synchronisation at edge %d\n",
135ab4b6496SHarald Geyer 				offset + 2 * i + 1);
136ab4b6496SHarald Geyer 			return -EIO;
137ab4b6496SHarald Geyer 		}
138155a5759SHarald Geyer 		bits[i] = t > DHT11_THRESHOLD;
139091a121bSHarald Geyer 	}
140091a121bSHarald Geyer 
141155a5759SHarald Geyer 	hum_int = dht11_decode_byte(bits);
142155a5759SHarald Geyer 	hum_dec = dht11_decode_byte(&bits[8]);
143155a5759SHarald Geyer 	temp_int = dht11_decode_byte(&bits[16]);
144155a5759SHarald Geyer 	temp_dec = dht11_decode_byte(&bits[24]);
145155a5759SHarald Geyer 	checksum = dht11_decode_byte(&bits[32]);
146091a121bSHarald Geyer 
147ab4b6496SHarald Geyer 	if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum) {
148ab4b6496SHarald Geyer 		dev_dbg(dht11->dev, "invalid checksum\n");
149091a121bSHarald Geyer 		return -EIO;
150ab4b6496SHarald Geyer 	}
151091a121bSHarald Geyer 
15217a2f46cSAbhilash Jindal 	dht11->timestamp = ktime_get_boot_ns();
153a6bffb69SHarald Geyer 	if (hum_int < 4) {  /* DHT22: 100000 = (3*256+232)*100 */
154091a121bSHarald Geyer 		dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
155091a121bSHarald Geyer 					((temp_int & 0x80) ? -100 : 100);
156091a121bSHarald Geyer 		dht11->humidity = ((hum_int << 8) + hum_dec) * 100;
157091a121bSHarald Geyer 	} else if (temp_dec == 0 && hum_dec == 0) {  /* DHT11 */
158091a121bSHarald Geyer 		dht11->temperature = temp_int * 1000;
159091a121bSHarald Geyer 		dht11->humidity = hum_int * 1000;
160091a121bSHarald Geyer 	} else {
161091a121bSHarald Geyer 		dev_err(dht11->dev,
162091a121bSHarald Geyer 			"Don't know how to decode data: %d %d %d %d\n",
163091a121bSHarald Geyer 			hum_int, hum_dec, temp_int, temp_dec);
164091a121bSHarald Geyer 		return -EIO;
165091a121bSHarald Geyer 	}
166091a121bSHarald Geyer 
167091a121bSHarald Geyer 	return 0;
168091a121bSHarald Geyer }
169091a121bSHarald Geyer 
17094e65519SRichard Weinberger /*
17194e65519SRichard Weinberger  * IRQ handler called on GPIO edges
17294e65519SRichard Weinberger  */
17394e65519SRichard Weinberger static irqreturn_t dht11_handle_irq(int irq, void *data)
17494e65519SRichard Weinberger {
17594e65519SRichard Weinberger 	struct iio_dev *iio = data;
17694e65519SRichard Weinberger 	struct dht11 *dht11 = iio_priv(iio);
17794e65519SRichard Weinberger 
17894e65519SRichard Weinberger 	/* TODO: Consider making the handler safe for IRQ sharing */
17994e65519SRichard Weinberger 	if (dht11->num_edges < DHT11_EDGES_PER_READ && dht11->num_edges >= 0) {
18017a2f46cSAbhilash Jindal 		dht11->edges[dht11->num_edges].ts = ktime_get_boot_ns();
18194e65519SRichard Weinberger 		dht11->edges[dht11->num_edges++].value =
18294e65519SRichard Weinberger 						gpio_get_value(dht11->gpio);
18394e65519SRichard Weinberger 
18494e65519SRichard Weinberger 		if (dht11->num_edges >= DHT11_EDGES_PER_READ)
18594e65519SRichard Weinberger 			complete(&dht11->completion);
18694e65519SRichard Weinberger 	}
18794e65519SRichard Weinberger 
18894e65519SRichard Weinberger 	return IRQ_HANDLED;
18994e65519SRichard Weinberger }
19094e65519SRichard Weinberger 
191091a121bSHarald Geyer static int dht11_read_raw(struct iio_dev *iio_dev,
192091a121bSHarald Geyer 			  const struct iio_chan_spec *chan,
193091a121bSHarald Geyer 			int *val, int *val2, long m)
194091a121bSHarald Geyer {
195091a121bSHarald Geyer 	struct dht11 *dht11 = iio_priv(iio_dev);
19622acc120SHarald Geyer 	int ret, timeres, offset;
197091a121bSHarald Geyer 
198004bc530SRichard Weinberger 	mutex_lock(&dht11->lock);
19917a2f46cSAbhilash Jindal 	if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boot_ns()) {
200081d9740SHarald Geyer 		timeres = ktime_get_resolution_ns();
201ab4b6496SHarald Geyer 		dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
202155a5759SHarald Geyer 		if (timeres > DHT11_MIN_TIMERES) {
203081d9740SHarald Geyer 			dev_err(dht11->dev, "timeresolution %dns too low\n",
204081d9740SHarald Geyer 				timeres);
205081d9740SHarald Geyer 			/* In theory a better clock could become available
206081d9740SHarald Geyer 			 * at some point ... and there is no error code
207081d9740SHarald Geyer 			 * that really fits better.
208081d9740SHarald Geyer 			 */
209081d9740SHarald Geyer 			ret = -EAGAIN;
210081d9740SHarald Geyer 			goto err;
211081d9740SHarald Geyer 		}
212155a5759SHarald Geyer 		if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH)
213155a5759SHarald Geyer 			dev_warn(dht11->dev,
214155a5759SHarald Geyer 				 "timeresolution: %dns - decoding ambiguous\n",
215155a5759SHarald Geyer 				 timeres);
216081d9740SHarald Geyer 
217091a121bSHarald Geyer 		reinit_completion(&dht11->completion);
218091a121bSHarald Geyer 
219091a121bSHarald Geyer 		dht11->num_edges = 0;
220091a121bSHarald Geyer 		ret = gpio_direction_output(dht11->gpio, 0);
221091a121bSHarald Geyer 		if (ret)
222091a121bSHarald Geyer 			goto err;
2235c113b5eSJohn Brooks 		usleep_range(DHT11_START_TRANSMISSION_MIN,
2245c113b5eSJohn Brooks 			     DHT11_START_TRANSMISSION_MAX);
225091a121bSHarald Geyer 		ret = gpio_direction_input(dht11->gpio);
226091a121bSHarald Geyer 		if (ret)
227091a121bSHarald Geyer 			goto err;
228091a121bSHarald Geyer 
22994e65519SRichard Weinberger 		ret = request_irq(dht11->irq, dht11_handle_irq,
23094e65519SRichard Weinberger 				  IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
23194e65519SRichard Weinberger 				  iio_dev->name, iio_dev);
23294e65519SRichard Weinberger 		if (ret)
23394e65519SRichard Weinberger 			goto err;
23494e65519SRichard Weinberger 
235091a121bSHarald Geyer 		ret = wait_for_completion_killable_timeout(&dht11->completion,
236091a121bSHarald Geyer 							   HZ);
23794e65519SRichard Weinberger 
23894e65519SRichard Weinberger 		free_irq(dht11->irq, iio_dev);
23994e65519SRichard Weinberger 
240ab4b6496SHarald Geyer #ifdef CONFIG_DYNAMIC_DEBUG
241ab4b6496SHarald Geyer 		dht11_edges_print(dht11);
242ab4b6496SHarald Geyer #endif
243ab4b6496SHarald Geyer 
244091a121bSHarald Geyer 		if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
245ab4b6496SHarald Geyer 			dev_err(dht11->dev, "Only %d signal edges detected\n",
246091a121bSHarald Geyer 				dht11->num_edges);
247091a121bSHarald Geyer 			ret = -ETIMEDOUT;
248091a121bSHarald Geyer 		}
249091a121bSHarald Geyer 		if (ret < 0)
250091a121bSHarald Geyer 			goto err;
251091a121bSHarald Geyer 
25222acc120SHarald Geyer 		offset = DHT11_EDGES_PREAMBLE +
25322acc120SHarald Geyer 				dht11->num_edges - DHT11_EDGES_PER_READ;
25422acc120SHarald Geyer 		for (; offset >= 0; --offset) {
255155a5759SHarald Geyer 			ret = dht11_decode(dht11, offset);
25622acc120SHarald Geyer 			if (!ret)
25722acc120SHarald Geyer 				break;
25822acc120SHarald Geyer 		}
25922acc120SHarald Geyer 
260091a121bSHarald Geyer 		if (ret)
261091a121bSHarald Geyer 			goto err;
262091a121bSHarald Geyer 	}
263091a121bSHarald Geyer 
264091a121bSHarald Geyer 	ret = IIO_VAL_INT;
265091a121bSHarald Geyer 	if (chan->type == IIO_TEMP)
266091a121bSHarald Geyer 		*val = dht11->temperature;
267091a121bSHarald Geyer 	else if (chan->type == IIO_HUMIDITYRELATIVE)
268091a121bSHarald Geyer 		*val = dht11->humidity;
269091a121bSHarald Geyer 	else
270091a121bSHarald Geyer 		ret = -EINVAL;
271091a121bSHarald Geyer err:
272091a121bSHarald Geyer 	dht11->num_edges = -1;
273004bc530SRichard Weinberger 	mutex_unlock(&dht11->lock);
274091a121bSHarald Geyer 	return ret;
275091a121bSHarald Geyer }
276091a121bSHarald Geyer 
277091a121bSHarald Geyer static const struct iio_info dht11_iio_info = {
278091a121bSHarald Geyer 	.read_raw		= dht11_read_raw,
279091a121bSHarald Geyer };
280091a121bSHarald Geyer 
281091a121bSHarald Geyer static const struct iio_chan_spec dht11_chan_spec[] = {
282091a121bSHarald Geyer 	{ .type = IIO_TEMP,
283091a121bSHarald Geyer 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), },
284091a121bSHarald Geyer 	{ .type = IIO_HUMIDITYRELATIVE,
285091a121bSHarald Geyer 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), }
286091a121bSHarald Geyer };
287091a121bSHarald Geyer 
288091a121bSHarald Geyer static const struct of_device_id dht11_dt_ids[] = {
289091a121bSHarald Geyer 	{ .compatible = "dht11", },
290091a121bSHarald Geyer 	{ }
291091a121bSHarald Geyer };
292091a121bSHarald Geyer MODULE_DEVICE_TABLE(of, dht11_dt_ids);
293091a121bSHarald Geyer 
294091a121bSHarald Geyer static int dht11_probe(struct platform_device *pdev)
295091a121bSHarald Geyer {
296091a121bSHarald Geyer 	struct device *dev = &pdev->dev;
297091a121bSHarald Geyer 	struct device_node *node = dev->of_node;
298091a121bSHarald Geyer 	struct dht11 *dht11;
299091a121bSHarald Geyer 	struct iio_dev *iio;
300091a121bSHarald Geyer 	int ret;
301091a121bSHarald Geyer 
302091a121bSHarald Geyer 	iio = devm_iio_device_alloc(dev, sizeof(*dht11));
303091a121bSHarald Geyer 	if (!iio) {
304091a121bSHarald Geyer 		dev_err(dev, "Failed to allocate IIO device\n");
305091a121bSHarald Geyer 		return -ENOMEM;
306091a121bSHarald Geyer 	}
307091a121bSHarald Geyer 
308091a121bSHarald Geyer 	dht11 = iio_priv(iio);
309091a121bSHarald Geyer 	dht11->dev = dev;
310091a121bSHarald Geyer 
3115fbb0bc4SHarald Geyer 	ret = of_get_gpio(node, 0);
312091a121bSHarald Geyer 	if (ret < 0)
313091a121bSHarald Geyer 		return ret;
3145fbb0bc4SHarald Geyer 	dht11->gpio = ret;
315091a121bSHarald Geyer 	ret = devm_gpio_request_one(dev, dht11->gpio, GPIOF_IN, pdev->name);
316091a121bSHarald Geyer 	if (ret)
317091a121bSHarald Geyer 		return ret;
318091a121bSHarald Geyer 
319091a121bSHarald Geyer 	dht11->irq = gpio_to_irq(dht11->gpio);
320091a121bSHarald Geyer 	if (dht11->irq < 0) {
321091a121bSHarald Geyer 		dev_err(dev, "GPIO %d has no interrupt\n", dht11->gpio);
322091a121bSHarald Geyer 		return -EINVAL;
323091a121bSHarald Geyer 	}
324091a121bSHarald Geyer 
32517a2f46cSAbhilash Jindal 	dht11->timestamp = ktime_get_boot_ns() - DHT11_DATA_VALID_TIME - 1;
326091a121bSHarald Geyer 	dht11->num_edges = -1;
327091a121bSHarald Geyer 
328091a121bSHarald Geyer 	platform_set_drvdata(pdev, iio);
329091a121bSHarald Geyer 
330091a121bSHarald Geyer 	init_completion(&dht11->completion);
331004bc530SRichard Weinberger 	mutex_init(&dht11->lock);
332091a121bSHarald Geyer 	iio->name = pdev->name;
333091a121bSHarald Geyer 	iio->dev.parent = &pdev->dev;
334091a121bSHarald Geyer 	iio->info = &dht11_iio_info;
335091a121bSHarald Geyer 	iio->modes = INDIO_DIRECT_MODE;
336091a121bSHarald Geyer 	iio->channels = dht11_chan_spec;
337091a121bSHarald Geyer 	iio->num_channels = ARRAY_SIZE(dht11_chan_spec);
338091a121bSHarald Geyer 
339091a121bSHarald Geyer 	return devm_iio_device_register(dev, iio);
340091a121bSHarald Geyer }
341091a121bSHarald Geyer 
342091a121bSHarald Geyer static struct platform_driver dht11_driver = {
343091a121bSHarald Geyer 	.driver = {
344091a121bSHarald Geyer 		.name	= DRIVER_NAME,
345091a121bSHarald Geyer 		.of_match_table = dht11_dt_ids,
346091a121bSHarald Geyer 	},
347091a121bSHarald Geyer 	.probe  = dht11_probe,
348091a121bSHarald Geyer };
349091a121bSHarald Geyer 
350091a121bSHarald Geyer module_platform_driver(dht11_driver);
351091a121bSHarald Geyer 
352091a121bSHarald Geyer MODULE_AUTHOR("Harald Geyer <harald@ccbib.org>");
353091a121bSHarald Geyer MODULE_DESCRIPTION("DHT11 humidity/temperature sensor driver");
354091a121bSHarald Geyer MODULE_LICENSE("GPL v2");
355