xref: /openbmc/linux/drivers/iio/humidity/dht11.c (revision d2fdbccd)
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/sysfs.h>
15091a121bSHarald Geyer #include <linux/io.h>
16*d2fdbccdSAndy Shevchenko #include <linux/mod_devicetable.h>
17091a121bSHarald Geyer #include <linux/module.h>
18091a121bSHarald Geyer #include <linux/platform_device.h>
19091a121bSHarald Geyer #include <linux/wait.h>
20091a121bSHarald Geyer #include <linux/bitops.h>
21091a121bSHarald Geyer #include <linux/completion.h>
22004bc530SRichard Weinberger #include <linux/mutex.h>
23091a121bSHarald Geyer #include <linux/delay.h>
247e8b817eSShobhit Kukreti #include <linux/gpio/consumer.h>
25081d9740SHarald Geyer #include <linux/timekeeping.h>
26091a121bSHarald Geyer 
27091a121bSHarald Geyer #include <linux/iio/iio.h>
28091a121bSHarald Geyer 
29091a121bSHarald Geyer #define DRIVER_NAME	"dht11"
30091a121bSHarald Geyer 
31091a121bSHarald Geyer #define DHT11_DATA_VALID_TIME	2000000000  /* 2s in ns */
32091a121bSHarald Geyer 
3394e65519SRichard Weinberger #define DHT11_EDGES_PREAMBLE 2
34091a121bSHarald Geyer #define DHT11_BITS_PER_READ 40
3594e65519SRichard Weinberger /*
3694e65519SRichard Weinberger  * Note that when reading the sensor actually 84 edges are detected, but
3794e65519SRichard Weinberger  * since the last edge is not significant, we only store 83:
3894e65519SRichard Weinberger  */
39889c5e9bSHarald Geyer #define DHT11_EDGES_PER_READ (2 * DHT11_BITS_PER_READ + \
40889c5e9bSHarald Geyer 			      DHT11_EDGES_PREAMBLE + 1)
41091a121bSHarald Geyer 
42155a5759SHarald Geyer /*
43155a5759SHarald Geyer  * Data transmission timing:
44155a5759SHarald Geyer  * Data bits are encoded as pulse length (high time) on the data line.
45155a5759SHarald Geyer  * 0-bit: 22-30uS -- typically 26uS (AM2302)
46155a5759SHarald Geyer  * 1-bit: 68-75uS -- typically 70uS (AM2302)
47155a5759SHarald Geyer  * The acutal timings also depend on the properties of the cable, with
48155a5759SHarald Geyer  * longer cables typically making pulses shorter.
49155a5759SHarald Geyer  *
50155a5759SHarald Geyer  * Our decoding depends on the time resolution of the system:
51155a5759SHarald Geyer  * timeres > 34uS ... don't know what a 1-tick pulse is
52155a5759SHarald Geyer  * 34uS > timeres > 30uS ... no problem (30kHz and 32kHz clocks)
53155a5759SHarald Geyer  * 30uS > timeres > 23uS ... don't know what a 2-tick pulse is
54155a5759SHarald Geyer  * timeres < 23uS ... no problem
55155a5759SHarald Geyer  *
56155a5759SHarald Geyer  * Luckily clocks in the 33-44kHz range are quite uncommon, so we can
57155a5759SHarald Geyer  * support most systems if the threshold for decoding a pulse as 1-bit
58155a5759SHarald Geyer  * is chosen carefully. If somebody really wants to support clocks around
59155a5759SHarald Geyer  * 40kHz, where this driver is most unreliable, there are two options.
60155a5759SHarald Geyer  * a) select an implementation using busy loop polling on those systems
61155a5759SHarald Geyer  * b) use the checksum to do some probabilistic decoding
62155a5759SHarald Geyer  */
635c113b5eSJohn Brooks #define DHT11_START_TRANSMISSION_MIN	18000  /* us */
645c113b5eSJohn Brooks #define DHT11_START_TRANSMISSION_MAX	20000  /* us */
65155a5759SHarald Geyer #define DHT11_MIN_TIMERES	34000  /* ns */
66155a5759SHarald Geyer #define DHT11_THRESHOLD		49000  /* ns */
67155a5759SHarald Geyer #define DHT11_AMBIG_LOW		23000  /* ns */
68155a5759SHarald Geyer #define DHT11_AMBIG_HIGH	30000  /* ns */
69091a121bSHarald Geyer 
70091a121bSHarald Geyer struct dht11 {
71091a121bSHarald Geyer 	struct device			*dev;
72091a121bSHarald Geyer 
737e8b817eSShobhit Kukreti 	struct gpio_desc		*gpiod;
74091a121bSHarald Geyer 	int				irq;
75091a121bSHarald Geyer 
76091a121bSHarald Geyer 	struct completion		completion;
77a7126003SHarald Geyer 	/* The iio sysfs interface doesn't prevent concurrent reads: */
78004bc530SRichard Weinberger 	struct mutex			lock;
79091a121bSHarald Geyer 
80091a121bSHarald Geyer 	s64				timestamp;
81091a121bSHarald Geyer 	int				temperature;
82091a121bSHarald Geyer 	int				humidity;
83091a121bSHarald Geyer 
84091a121bSHarald Geyer 	/* num_edges: -1 means "no transmission in progress" */
85091a121bSHarald Geyer 	int				num_edges;
86091a121bSHarald Geyer 	struct {s64 ts; int value; }	edges[DHT11_EDGES_PER_READ];
87091a121bSHarald Geyer };
88091a121bSHarald Geyer 
89ab4b6496SHarald Geyer #ifdef CONFIG_DYNAMIC_DEBUG
90ab4b6496SHarald Geyer /*
91ab4b6496SHarald Geyer  * dht11_edges_print: show the data as actually received by the
92ab4b6496SHarald Geyer  *                    driver.
93ab4b6496SHarald Geyer  */
dht11_edges_print(struct dht11 * dht11)94ab4b6496SHarald Geyer static void dht11_edges_print(struct dht11 *dht11)
95ab4b6496SHarald Geyer {
96ab4b6496SHarald Geyer 	int i;
97ab4b6496SHarald Geyer 
98ab4b6496SHarald Geyer 	dev_dbg(dht11->dev, "%d edges detected:\n", dht11->num_edges);
99ab4b6496SHarald Geyer 	for (i = 1; i < dht11->num_edges; ++i) {
100ab4b6496SHarald Geyer 		dev_dbg(dht11->dev, "%d: %lld ns %s\n", i,
101ab4b6496SHarald Geyer 			dht11->edges[i].ts - dht11->edges[i - 1].ts,
102ab4b6496SHarald Geyer 			dht11->edges[i - 1].value ? "high" : "low");
103ab4b6496SHarald Geyer 	}
104ab4b6496SHarald Geyer }
105ab4b6496SHarald Geyer #endif /* CONFIG_DYNAMIC_DEBUG */
106ab4b6496SHarald Geyer 
dht11_decode_byte(char * bits)107155a5759SHarald Geyer static unsigned char dht11_decode_byte(char *bits)
108091a121bSHarald Geyer {
109091a121bSHarald Geyer 	unsigned char ret = 0;
110091a121bSHarald Geyer 	int i;
111091a121bSHarald Geyer 
112091a121bSHarald Geyer 	for (i = 0; i < 8; ++i) {
113091a121bSHarald Geyer 		ret <<= 1;
114155a5759SHarald Geyer 		if (bits[i])
115091a121bSHarald Geyer 			++ret;
116091a121bSHarald Geyer 	}
117091a121bSHarald Geyer 
118091a121bSHarald Geyer 	return ret;
119091a121bSHarald Geyer }
120091a121bSHarald Geyer 
dht11_decode(struct dht11 * dht11,int offset)121155a5759SHarald Geyer static int dht11_decode(struct dht11 *dht11, int offset)
122091a121bSHarald Geyer {
123155a5759SHarald Geyer 	int i, t;
124155a5759SHarald Geyer 	char bits[DHT11_BITS_PER_READ];
125091a121bSHarald Geyer 	unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum;
126091a121bSHarald Geyer 
127091a121bSHarald Geyer 	for (i = 0; i < DHT11_BITS_PER_READ; ++i) {
128091a121bSHarald Geyer 		t = dht11->edges[offset + 2 * i + 2].ts -
129091a121bSHarald Geyer 			dht11->edges[offset + 2 * i + 1].ts;
130ab4b6496SHarald Geyer 		if (!dht11->edges[offset + 2 * i + 1].value) {
131ab4b6496SHarald Geyer 			dev_dbg(dht11->dev,
132ab4b6496SHarald Geyer 				"lost synchronisation at edge %d\n",
133ab4b6496SHarald Geyer 				offset + 2 * i + 1);
134ab4b6496SHarald Geyer 			return -EIO;
135ab4b6496SHarald Geyer 		}
136155a5759SHarald Geyer 		bits[i] = t > DHT11_THRESHOLD;
137091a121bSHarald Geyer 	}
138091a121bSHarald Geyer 
139155a5759SHarald Geyer 	hum_int = dht11_decode_byte(bits);
140155a5759SHarald Geyer 	hum_dec = dht11_decode_byte(&bits[8]);
141155a5759SHarald Geyer 	temp_int = dht11_decode_byte(&bits[16]);
142155a5759SHarald Geyer 	temp_dec = dht11_decode_byte(&bits[24]);
143155a5759SHarald Geyer 	checksum = dht11_decode_byte(&bits[32]);
144091a121bSHarald Geyer 
145ab4b6496SHarald Geyer 	if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum) {
146ab4b6496SHarald Geyer 		dev_dbg(dht11->dev, "invalid checksum\n");
147091a121bSHarald Geyer 		return -EIO;
148ab4b6496SHarald Geyer 	}
149091a121bSHarald Geyer 
1509285ec4cSJason A. Donenfeld 	dht11->timestamp = ktime_get_boottime_ns();
151a6bffb69SHarald Geyer 	if (hum_int < 4) {  /* DHT22: 100000 = (3*256+232)*100 */
152091a121bSHarald Geyer 		dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
153091a121bSHarald Geyer 					((temp_int & 0x80) ? -100 : 100);
154091a121bSHarald Geyer 		dht11->humidity = ((hum_int << 8) + hum_dec) * 100;
155091a121bSHarald Geyer 	} else if (temp_dec == 0 && hum_dec == 0) {  /* DHT11 */
156091a121bSHarald Geyer 		dht11->temperature = temp_int * 1000;
157091a121bSHarald Geyer 		dht11->humidity = hum_int * 1000;
158091a121bSHarald Geyer 	} else {
159091a121bSHarald Geyer 		dev_err(dht11->dev,
160091a121bSHarald Geyer 			"Don't know how to decode data: %d %d %d %d\n",
161091a121bSHarald Geyer 			hum_int, hum_dec, temp_int, temp_dec);
162091a121bSHarald Geyer 		return -EIO;
163091a121bSHarald Geyer 	}
164091a121bSHarald Geyer 
165091a121bSHarald Geyer 	return 0;
166091a121bSHarald Geyer }
167091a121bSHarald Geyer 
16894e65519SRichard Weinberger /*
16994e65519SRichard Weinberger  * IRQ handler called on GPIO edges
17094e65519SRichard Weinberger  */
dht11_handle_irq(int irq,void * data)17194e65519SRichard Weinberger static irqreturn_t dht11_handle_irq(int irq, void *data)
17294e65519SRichard Weinberger {
17394e65519SRichard Weinberger 	struct iio_dev *iio = data;
17494e65519SRichard Weinberger 	struct dht11 *dht11 = iio_priv(iio);
17594e65519SRichard Weinberger 
17694e65519SRichard Weinberger 	if (dht11->num_edges < DHT11_EDGES_PER_READ && dht11->num_edges >= 0) {
1779285ec4cSJason A. Donenfeld 		dht11->edges[dht11->num_edges].ts = ktime_get_boottime_ns();
17894e65519SRichard Weinberger 		dht11->edges[dht11->num_edges++].value =
1797e8b817eSShobhit Kukreti 						gpiod_get_value(dht11->gpiod);
18094e65519SRichard Weinberger 
18194e65519SRichard Weinberger 		if (dht11->num_edges >= DHT11_EDGES_PER_READ)
18294e65519SRichard Weinberger 			complete(&dht11->completion);
18394e65519SRichard Weinberger 	}
18494e65519SRichard Weinberger 
18594e65519SRichard Weinberger 	return IRQ_HANDLED;
18694e65519SRichard Weinberger }
18794e65519SRichard Weinberger 
dht11_read_raw(struct iio_dev * iio_dev,const struct iio_chan_spec * chan,int * val,int * val2,long m)188091a121bSHarald Geyer static int dht11_read_raw(struct iio_dev *iio_dev,
189091a121bSHarald Geyer 			  const struct iio_chan_spec *chan,
190091a121bSHarald Geyer 			int *val, int *val2, long m)
191091a121bSHarald Geyer {
192091a121bSHarald Geyer 	struct dht11 *dht11 = iio_priv(iio_dev);
19322acc120SHarald Geyer 	int ret, timeres, offset;
194091a121bSHarald Geyer 
195004bc530SRichard Weinberger 	mutex_lock(&dht11->lock);
1969285ec4cSJason A. Donenfeld 	if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boottime_ns()) {
197081d9740SHarald Geyer 		timeres = ktime_get_resolution_ns();
198ab4b6496SHarald Geyer 		dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
199155a5759SHarald Geyer 		if (timeres > DHT11_MIN_TIMERES) {
200081d9740SHarald Geyer 			dev_err(dht11->dev, "timeresolution %dns too low\n",
201081d9740SHarald Geyer 				timeres);
202081d9740SHarald Geyer 			/* In theory a better clock could become available
203081d9740SHarald Geyer 			 * at some point ... and there is no error code
204081d9740SHarald Geyer 			 * that really fits better.
205081d9740SHarald Geyer 			 */
206081d9740SHarald Geyer 			ret = -EAGAIN;
207081d9740SHarald Geyer 			goto err;
208081d9740SHarald Geyer 		}
209155a5759SHarald Geyer 		if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH)
210155a5759SHarald Geyer 			dev_warn(dht11->dev,
211155a5759SHarald Geyer 				 "timeresolution: %dns - decoding ambiguous\n",
212155a5759SHarald Geyer 				 timeres);
213081d9740SHarald Geyer 
214091a121bSHarald Geyer 		reinit_completion(&dht11->completion);
215091a121bSHarald Geyer 
216091a121bSHarald Geyer 		dht11->num_edges = 0;
2177e8b817eSShobhit Kukreti 		ret = gpiod_direction_output(dht11->gpiod, 0);
218091a121bSHarald Geyer 		if (ret)
219091a121bSHarald Geyer 			goto err;
2205c113b5eSJohn Brooks 		usleep_range(DHT11_START_TRANSMISSION_MIN,
2215c113b5eSJohn Brooks 			     DHT11_START_TRANSMISSION_MAX);
2227e8b817eSShobhit Kukreti 		ret = gpiod_direction_input(dht11->gpiod);
223091a121bSHarald Geyer 		if (ret)
224091a121bSHarald Geyer 			goto err;
225091a121bSHarald Geyer 
22694e65519SRichard Weinberger 		ret = request_irq(dht11->irq, dht11_handle_irq,
22794e65519SRichard Weinberger 				  IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
22894e65519SRichard Weinberger 				  iio_dev->name, iio_dev);
22994e65519SRichard Weinberger 		if (ret)
23094e65519SRichard Weinberger 			goto err;
23194e65519SRichard Weinberger 
232091a121bSHarald Geyer 		ret = wait_for_completion_killable_timeout(&dht11->completion,
233091a121bSHarald Geyer 							   HZ);
23494e65519SRichard Weinberger 
23594e65519SRichard Weinberger 		free_irq(dht11->irq, iio_dev);
23694e65519SRichard Weinberger 
237ab4b6496SHarald Geyer #ifdef CONFIG_DYNAMIC_DEBUG
238ab4b6496SHarald Geyer 		dht11_edges_print(dht11);
239ab4b6496SHarald Geyer #endif
240ab4b6496SHarald Geyer 
241091a121bSHarald Geyer 		if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
242ab4b6496SHarald Geyer 			dev_err(dht11->dev, "Only %d signal edges detected\n",
243091a121bSHarald Geyer 				dht11->num_edges);
244091a121bSHarald Geyer 			ret = -ETIMEDOUT;
245091a121bSHarald Geyer 		}
246091a121bSHarald Geyer 		if (ret < 0)
247091a121bSHarald Geyer 			goto err;
248091a121bSHarald Geyer 
24922acc120SHarald Geyer 		offset = DHT11_EDGES_PREAMBLE +
25022acc120SHarald Geyer 				dht11->num_edges - DHT11_EDGES_PER_READ;
25122acc120SHarald Geyer 		for (; offset >= 0; --offset) {
252155a5759SHarald Geyer 			ret = dht11_decode(dht11, offset);
25322acc120SHarald Geyer 			if (!ret)
25422acc120SHarald Geyer 				break;
25522acc120SHarald Geyer 		}
25622acc120SHarald Geyer 
257091a121bSHarald Geyer 		if (ret)
258091a121bSHarald Geyer 			goto err;
259091a121bSHarald Geyer 	}
260091a121bSHarald Geyer 
261091a121bSHarald Geyer 	ret = IIO_VAL_INT;
262091a121bSHarald Geyer 	if (chan->type == IIO_TEMP)
263091a121bSHarald Geyer 		*val = dht11->temperature;
264091a121bSHarald Geyer 	else if (chan->type == IIO_HUMIDITYRELATIVE)
265091a121bSHarald Geyer 		*val = dht11->humidity;
266091a121bSHarald Geyer 	else
267091a121bSHarald Geyer 		ret = -EINVAL;
268091a121bSHarald Geyer err:
269091a121bSHarald Geyer 	dht11->num_edges = -1;
270004bc530SRichard Weinberger 	mutex_unlock(&dht11->lock);
271091a121bSHarald Geyer 	return ret;
272091a121bSHarald Geyer }
273091a121bSHarald Geyer 
274091a121bSHarald Geyer static const struct iio_info dht11_iio_info = {
275091a121bSHarald Geyer 	.read_raw		= dht11_read_raw,
276091a121bSHarald Geyer };
277091a121bSHarald Geyer 
278091a121bSHarald Geyer static const struct iio_chan_spec dht11_chan_spec[] = {
279091a121bSHarald Geyer 	{ .type = IIO_TEMP,
280091a121bSHarald Geyer 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), },
281091a121bSHarald Geyer 	{ .type = IIO_HUMIDITYRELATIVE,
282091a121bSHarald Geyer 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), }
283091a121bSHarald Geyer };
284091a121bSHarald Geyer 
285091a121bSHarald Geyer static const struct of_device_id dht11_dt_ids[] = {
286091a121bSHarald Geyer 	{ .compatible = "dht11", },
287091a121bSHarald Geyer 	{ }
288091a121bSHarald Geyer };
289091a121bSHarald Geyer MODULE_DEVICE_TABLE(of, dht11_dt_ids);
290091a121bSHarald Geyer 
dht11_probe(struct platform_device * pdev)291091a121bSHarald Geyer static int dht11_probe(struct platform_device *pdev)
292091a121bSHarald Geyer {
293091a121bSHarald Geyer 	struct device *dev = &pdev->dev;
294091a121bSHarald Geyer 	struct dht11 *dht11;
295091a121bSHarald Geyer 	struct iio_dev *iio;
296091a121bSHarald Geyer 
297091a121bSHarald Geyer 	iio = devm_iio_device_alloc(dev, sizeof(*dht11));
298091a121bSHarald Geyer 	if (!iio) {
299091a121bSHarald Geyer 		dev_err(dev, "Failed to allocate IIO device\n");
300091a121bSHarald Geyer 		return -ENOMEM;
301091a121bSHarald Geyer 	}
302091a121bSHarald Geyer 
303091a121bSHarald Geyer 	dht11 = iio_priv(iio);
304091a121bSHarald Geyer 	dht11->dev = dev;
3057e8b817eSShobhit Kukreti 	dht11->gpiod = devm_gpiod_get(dev, NULL, GPIOD_IN);
3067e8b817eSShobhit Kukreti 	if (IS_ERR(dht11->gpiod))
3077e8b817eSShobhit Kukreti 		return PTR_ERR(dht11->gpiod);
308091a121bSHarald Geyer 
3097e8b817eSShobhit Kukreti 	dht11->irq = gpiod_to_irq(dht11->gpiod);
310091a121bSHarald Geyer 	if (dht11->irq < 0) {
3117e8b817eSShobhit Kukreti 		dev_err(dev, "GPIO %d has no interrupt\n", desc_to_gpio(dht11->gpiod));
312091a121bSHarald Geyer 		return -EINVAL;
313091a121bSHarald Geyer 	}
314091a121bSHarald Geyer 
3159285ec4cSJason A. Donenfeld 	dht11->timestamp = ktime_get_boottime_ns() - DHT11_DATA_VALID_TIME - 1;
316091a121bSHarald Geyer 	dht11->num_edges = -1;
317091a121bSHarald Geyer 
318091a121bSHarald Geyer 	platform_set_drvdata(pdev, iio);
319091a121bSHarald Geyer 
320091a121bSHarald Geyer 	init_completion(&dht11->completion);
321004bc530SRichard Weinberger 	mutex_init(&dht11->lock);
322091a121bSHarald Geyer 	iio->name = pdev->name;
323091a121bSHarald Geyer 	iio->info = &dht11_iio_info;
324091a121bSHarald Geyer 	iio->modes = INDIO_DIRECT_MODE;
325091a121bSHarald Geyer 	iio->channels = dht11_chan_spec;
326091a121bSHarald Geyer 	iio->num_channels = ARRAY_SIZE(dht11_chan_spec);
327091a121bSHarald Geyer 
328091a121bSHarald Geyer 	return devm_iio_device_register(dev, iio);
329091a121bSHarald Geyer }
330091a121bSHarald Geyer 
331091a121bSHarald Geyer static struct platform_driver dht11_driver = {
332091a121bSHarald Geyer 	.driver = {
333091a121bSHarald Geyer 		.name	= DRIVER_NAME,
334091a121bSHarald Geyer 		.of_match_table = dht11_dt_ids,
335091a121bSHarald Geyer 	},
336091a121bSHarald Geyer 	.probe  = dht11_probe,
337091a121bSHarald Geyer };
338091a121bSHarald Geyer 
339091a121bSHarald Geyer module_platform_driver(dht11_driver);
340091a121bSHarald Geyer 
341091a121bSHarald Geyer MODULE_AUTHOR("Harald Geyer <harald@ccbib.org>");
342091a121bSHarald Geyer MODULE_DESCRIPTION("DHT11 humidity/temperature sensor driver");
343091a121bSHarald Geyer MODULE_LICENSE("GPL v2");
344