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 53155a5759SHarald Geyer /* 54155a5759SHarald Geyer * Data transmission timing: 55155a5759SHarald Geyer * Data bits are encoded as pulse length (high time) on the data line. 56155a5759SHarald Geyer * 0-bit: 22-30uS -- typically 26uS (AM2302) 57155a5759SHarald Geyer * 1-bit: 68-75uS -- typically 70uS (AM2302) 58155a5759SHarald Geyer * The acutal timings also depend on the properties of the cable, with 59155a5759SHarald Geyer * longer cables typically making pulses shorter. 60155a5759SHarald Geyer * 61155a5759SHarald Geyer * Our decoding depends on the time resolution of the system: 62155a5759SHarald Geyer * timeres > 34uS ... don't know what a 1-tick pulse is 63155a5759SHarald Geyer * 34uS > timeres > 30uS ... no problem (30kHz and 32kHz clocks) 64155a5759SHarald Geyer * 30uS > timeres > 23uS ... don't know what a 2-tick pulse is 65155a5759SHarald Geyer * timeres < 23uS ... no problem 66155a5759SHarald Geyer * 67155a5759SHarald Geyer * Luckily clocks in the 33-44kHz range are quite uncommon, so we can 68155a5759SHarald Geyer * support most systems if the threshold for decoding a pulse as 1-bit 69155a5759SHarald Geyer * is chosen carefully. If somebody really wants to support clocks around 70155a5759SHarald Geyer * 40kHz, where this driver is most unreliable, there are two options. 71155a5759SHarald Geyer * a) select an implementation using busy loop polling on those systems 72155a5759SHarald Geyer * b) use the checksum to do some probabilistic decoding 73155a5759SHarald Geyer */ 745c113b5eSJohn Brooks #define DHT11_START_TRANSMISSION_MIN 18000 /* us */ 755c113b5eSJohn Brooks #define DHT11_START_TRANSMISSION_MAX 20000 /* us */ 76155a5759SHarald Geyer #define DHT11_MIN_TIMERES 34000 /* ns */ 77155a5759SHarald Geyer #define DHT11_THRESHOLD 49000 /* ns */ 78155a5759SHarald Geyer #define DHT11_AMBIG_LOW 23000 /* ns */ 79155a5759SHarald Geyer #define DHT11_AMBIG_HIGH 30000 /* ns */ 80091a121bSHarald Geyer 81091a121bSHarald Geyer struct dht11 { 82091a121bSHarald Geyer struct device *dev; 83091a121bSHarald Geyer 84091a121bSHarald Geyer int gpio; 85091a121bSHarald Geyer int irq; 86091a121bSHarald Geyer 87091a121bSHarald Geyer struct completion completion; 88a7126003SHarald Geyer /* The iio sysfs interface doesn't prevent concurrent reads: */ 89004bc530SRichard Weinberger struct mutex lock; 90091a121bSHarald Geyer 91091a121bSHarald Geyer s64 timestamp; 92091a121bSHarald Geyer int temperature; 93091a121bSHarald Geyer int humidity; 94091a121bSHarald Geyer 95091a121bSHarald Geyer /* num_edges: -1 means "no transmission in progress" */ 96091a121bSHarald Geyer int num_edges; 97091a121bSHarald Geyer struct {s64 ts; int value; } edges[DHT11_EDGES_PER_READ]; 98091a121bSHarald Geyer }; 99091a121bSHarald Geyer 100ab4b6496SHarald Geyer #ifdef CONFIG_DYNAMIC_DEBUG 101ab4b6496SHarald Geyer /* 102ab4b6496SHarald Geyer * dht11_edges_print: show the data as actually received by the 103ab4b6496SHarald Geyer * driver. 104ab4b6496SHarald Geyer */ 105ab4b6496SHarald Geyer static void dht11_edges_print(struct dht11 *dht11) 106ab4b6496SHarald Geyer { 107ab4b6496SHarald Geyer int i; 108ab4b6496SHarald Geyer 109ab4b6496SHarald Geyer dev_dbg(dht11->dev, "%d edges detected:\n", dht11->num_edges); 110ab4b6496SHarald Geyer for (i = 1; i < dht11->num_edges; ++i) { 111ab4b6496SHarald Geyer dev_dbg(dht11->dev, "%d: %lld ns %s\n", i, 112ab4b6496SHarald Geyer dht11->edges[i].ts - dht11->edges[i - 1].ts, 113ab4b6496SHarald Geyer dht11->edges[i - 1].value ? "high" : "low"); 114ab4b6496SHarald Geyer } 115ab4b6496SHarald Geyer } 116ab4b6496SHarald Geyer #endif /* CONFIG_DYNAMIC_DEBUG */ 117ab4b6496SHarald Geyer 118155a5759SHarald Geyer static unsigned char dht11_decode_byte(char *bits) 119091a121bSHarald Geyer { 120091a121bSHarald Geyer unsigned char ret = 0; 121091a121bSHarald Geyer int i; 122091a121bSHarald Geyer 123091a121bSHarald Geyer for (i = 0; i < 8; ++i) { 124091a121bSHarald Geyer ret <<= 1; 125155a5759SHarald Geyer if (bits[i]) 126091a121bSHarald Geyer ++ret; 127091a121bSHarald Geyer } 128091a121bSHarald Geyer 129091a121bSHarald Geyer return ret; 130091a121bSHarald Geyer } 131091a121bSHarald Geyer 132155a5759SHarald Geyer static int dht11_decode(struct dht11 *dht11, int offset) 133091a121bSHarald Geyer { 134155a5759SHarald Geyer int i, t; 135155a5759SHarald Geyer char bits[DHT11_BITS_PER_READ]; 136091a121bSHarald Geyer unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum; 137091a121bSHarald Geyer 138091a121bSHarald Geyer for (i = 0; i < DHT11_BITS_PER_READ; ++i) { 139091a121bSHarald Geyer t = dht11->edges[offset + 2 * i + 2].ts - 140091a121bSHarald Geyer dht11->edges[offset + 2 * i + 1].ts; 141ab4b6496SHarald Geyer if (!dht11->edges[offset + 2 * i + 1].value) { 142ab4b6496SHarald Geyer dev_dbg(dht11->dev, 143ab4b6496SHarald Geyer "lost synchronisation at edge %d\n", 144ab4b6496SHarald Geyer offset + 2 * i + 1); 145ab4b6496SHarald Geyer return -EIO; 146ab4b6496SHarald Geyer } 147155a5759SHarald Geyer bits[i] = t > DHT11_THRESHOLD; 148091a121bSHarald Geyer } 149091a121bSHarald Geyer 150155a5759SHarald Geyer hum_int = dht11_decode_byte(bits); 151155a5759SHarald Geyer hum_dec = dht11_decode_byte(&bits[8]); 152155a5759SHarald Geyer temp_int = dht11_decode_byte(&bits[16]); 153155a5759SHarald Geyer temp_dec = dht11_decode_byte(&bits[24]); 154155a5759SHarald Geyer checksum = dht11_decode_byte(&bits[32]); 155091a121bSHarald Geyer 156ab4b6496SHarald Geyer if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum) { 157ab4b6496SHarald Geyer dev_dbg(dht11->dev, "invalid checksum\n"); 158091a121bSHarald Geyer return -EIO; 159ab4b6496SHarald Geyer } 160091a121bSHarald Geyer 16117a2f46cSAbhilash Jindal dht11->timestamp = ktime_get_boot_ns(); 162a6bffb69SHarald Geyer if (hum_int < 4) { /* DHT22: 100000 = (3*256+232)*100 */ 163091a121bSHarald Geyer dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) * 164091a121bSHarald Geyer ((temp_int & 0x80) ? -100 : 100); 165091a121bSHarald Geyer dht11->humidity = ((hum_int << 8) + hum_dec) * 100; 166091a121bSHarald Geyer } else if (temp_dec == 0 && hum_dec == 0) { /* DHT11 */ 167091a121bSHarald Geyer dht11->temperature = temp_int * 1000; 168091a121bSHarald Geyer dht11->humidity = hum_int * 1000; 169091a121bSHarald Geyer } else { 170091a121bSHarald Geyer dev_err(dht11->dev, 171091a121bSHarald Geyer "Don't know how to decode data: %d %d %d %d\n", 172091a121bSHarald Geyer hum_int, hum_dec, temp_int, temp_dec); 173091a121bSHarald Geyer return -EIO; 174091a121bSHarald Geyer } 175091a121bSHarald Geyer 176091a121bSHarald Geyer return 0; 177091a121bSHarald Geyer } 178091a121bSHarald Geyer 17994e65519SRichard Weinberger /* 18094e65519SRichard Weinberger * IRQ handler called on GPIO edges 18194e65519SRichard Weinberger */ 18294e65519SRichard Weinberger static irqreturn_t dht11_handle_irq(int irq, void *data) 18394e65519SRichard Weinberger { 18494e65519SRichard Weinberger struct iio_dev *iio = data; 18594e65519SRichard Weinberger struct dht11 *dht11 = iio_priv(iio); 18694e65519SRichard Weinberger 18794e65519SRichard Weinberger /* TODO: Consider making the handler safe for IRQ sharing */ 18894e65519SRichard Weinberger if (dht11->num_edges < DHT11_EDGES_PER_READ && dht11->num_edges >= 0) { 18917a2f46cSAbhilash Jindal dht11->edges[dht11->num_edges].ts = ktime_get_boot_ns(); 19094e65519SRichard Weinberger dht11->edges[dht11->num_edges++].value = 19194e65519SRichard Weinberger gpio_get_value(dht11->gpio); 19294e65519SRichard Weinberger 19394e65519SRichard Weinberger if (dht11->num_edges >= DHT11_EDGES_PER_READ) 19494e65519SRichard Weinberger complete(&dht11->completion); 19594e65519SRichard Weinberger } 19694e65519SRichard Weinberger 19794e65519SRichard Weinberger return IRQ_HANDLED; 19894e65519SRichard Weinberger } 19994e65519SRichard Weinberger 200091a121bSHarald Geyer static int dht11_read_raw(struct iio_dev *iio_dev, 201091a121bSHarald Geyer const struct iio_chan_spec *chan, 202091a121bSHarald Geyer int *val, int *val2, long m) 203091a121bSHarald Geyer { 204091a121bSHarald Geyer struct dht11 *dht11 = iio_priv(iio_dev); 20522acc120SHarald Geyer int ret, timeres, offset; 206091a121bSHarald Geyer 207004bc530SRichard Weinberger mutex_lock(&dht11->lock); 20817a2f46cSAbhilash Jindal if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boot_ns()) { 209081d9740SHarald Geyer timeres = ktime_get_resolution_ns(); 210ab4b6496SHarald Geyer dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres); 211155a5759SHarald Geyer if (timeres > DHT11_MIN_TIMERES) { 212081d9740SHarald Geyer dev_err(dht11->dev, "timeresolution %dns too low\n", 213081d9740SHarald Geyer timeres); 214081d9740SHarald Geyer /* In theory a better clock could become available 215081d9740SHarald Geyer * at some point ... and there is no error code 216081d9740SHarald Geyer * that really fits better. 217081d9740SHarald Geyer */ 218081d9740SHarald Geyer ret = -EAGAIN; 219081d9740SHarald Geyer goto err; 220081d9740SHarald Geyer } 221155a5759SHarald Geyer if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH) 222155a5759SHarald Geyer dev_warn(dht11->dev, 223155a5759SHarald Geyer "timeresolution: %dns - decoding ambiguous\n", 224155a5759SHarald Geyer timeres); 225081d9740SHarald Geyer 226091a121bSHarald Geyer reinit_completion(&dht11->completion); 227091a121bSHarald Geyer 228091a121bSHarald Geyer dht11->num_edges = 0; 229091a121bSHarald Geyer ret = gpio_direction_output(dht11->gpio, 0); 230091a121bSHarald Geyer if (ret) 231091a121bSHarald Geyer goto err; 2325c113b5eSJohn Brooks usleep_range(DHT11_START_TRANSMISSION_MIN, 2335c113b5eSJohn Brooks DHT11_START_TRANSMISSION_MAX); 234091a121bSHarald Geyer ret = gpio_direction_input(dht11->gpio); 235091a121bSHarald Geyer if (ret) 236091a121bSHarald Geyer goto err; 237091a121bSHarald Geyer 23894e65519SRichard Weinberger ret = request_irq(dht11->irq, dht11_handle_irq, 23994e65519SRichard Weinberger IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, 24094e65519SRichard Weinberger iio_dev->name, iio_dev); 24194e65519SRichard Weinberger if (ret) 24294e65519SRichard Weinberger goto err; 24394e65519SRichard Weinberger 244091a121bSHarald Geyer ret = wait_for_completion_killable_timeout(&dht11->completion, 245091a121bSHarald Geyer HZ); 24694e65519SRichard Weinberger 24794e65519SRichard Weinberger free_irq(dht11->irq, iio_dev); 24894e65519SRichard Weinberger 249ab4b6496SHarald Geyer #ifdef CONFIG_DYNAMIC_DEBUG 250ab4b6496SHarald Geyer dht11_edges_print(dht11); 251ab4b6496SHarald Geyer #endif 252ab4b6496SHarald Geyer 253091a121bSHarald Geyer if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) { 254ab4b6496SHarald Geyer dev_err(dht11->dev, "Only %d signal edges detected\n", 255091a121bSHarald Geyer dht11->num_edges); 256091a121bSHarald Geyer ret = -ETIMEDOUT; 257091a121bSHarald Geyer } 258091a121bSHarald Geyer if (ret < 0) 259091a121bSHarald Geyer goto err; 260091a121bSHarald Geyer 26122acc120SHarald Geyer offset = DHT11_EDGES_PREAMBLE + 26222acc120SHarald Geyer dht11->num_edges - DHT11_EDGES_PER_READ; 26322acc120SHarald Geyer for (; offset >= 0; --offset) { 264155a5759SHarald Geyer ret = dht11_decode(dht11, offset); 26522acc120SHarald Geyer if (!ret) 26622acc120SHarald Geyer break; 26722acc120SHarald Geyer } 26822acc120SHarald Geyer 269091a121bSHarald Geyer if (ret) 270091a121bSHarald Geyer goto err; 271091a121bSHarald Geyer } 272091a121bSHarald Geyer 273091a121bSHarald Geyer ret = IIO_VAL_INT; 274091a121bSHarald Geyer if (chan->type == IIO_TEMP) 275091a121bSHarald Geyer *val = dht11->temperature; 276091a121bSHarald Geyer else if (chan->type == IIO_HUMIDITYRELATIVE) 277091a121bSHarald Geyer *val = dht11->humidity; 278091a121bSHarald Geyer else 279091a121bSHarald Geyer ret = -EINVAL; 280091a121bSHarald Geyer err: 281091a121bSHarald Geyer dht11->num_edges = -1; 282004bc530SRichard Weinberger mutex_unlock(&dht11->lock); 283091a121bSHarald Geyer return ret; 284091a121bSHarald Geyer } 285091a121bSHarald Geyer 286091a121bSHarald Geyer static const struct iio_info dht11_iio_info = { 287091a121bSHarald Geyer .read_raw = dht11_read_raw, 288091a121bSHarald Geyer }; 289091a121bSHarald Geyer 290091a121bSHarald Geyer static const struct iio_chan_spec dht11_chan_spec[] = { 291091a121bSHarald Geyer { .type = IIO_TEMP, 292091a121bSHarald Geyer .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), }, 293091a121bSHarald Geyer { .type = IIO_HUMIDITYRELATIVE, 294091a121bSHarald Geyer .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), } 295091a121bSHarald Geyer }; 296091a121bSHarald Geyer 297091a121bSHarald Geyer static const struct of_device_id dht11_dt_ids[] = { 298091a121bSHarald Geyer { .compatible = "dht11", }, 299091a121bSHarald Geyer { } 300091a121bSHarald Geyer }; 301091a121bSHarald Geyer MODULE_DEVICE_TABLE(of, dht11_dt_ids); 302091a121bSHarald Geyer 303091a121bSHarald Geyer static int dht11_probe(struct platform_device *pdev) 304091a121bSHarald Geyer { 305091a121bSHarald Geyer struct device *dev = &pdev->dev; 306091a121bSHarald Geyer struct device_node *node = dev->of_node; 307091a121bSHarald Geyer struct dht11 *dht11; 308091a121bSHarald Geyer struct iio_dev *iio; 309091a121bSHarald Geyer int ret; 310091a121bSHarald Geyer 311091a121bSHarald Geyer iio = devm_iio_device_alloc(dev, sizeof(*dht11)); 312091a121bSHarald Geyer if (!iio) { 313091a121bSHarald Geyer dev_err(dev, "Failed to allocate IIO device\n"); 314091a121bSHarald Geyer return -ENOMEM; 315091a121bSHarald Geyer } 316091a121bSHarald Geyer 317091a121bSHarald Geyer dht11 = iio_priv(iio); 318091a121bSHarald Geyer dht11->dev = dev; 319091a121bSHarald Geyer 3205fbb0bc4SHarald Geyer ret = of_get_gpio(node, 0); 321091a121bSHarald Geyer if (ret < 0) 322091a121bSHarald Geyer return ret; 3235fbb0bc4SHarald Geyer dht11->gpio = ret; 324091a121bSHarald Geyer ret = devm_gpio_request_one(dev, dht11->gpio, GPIOF_IN, pdev->name); 325091a121bSHarald Geyer if (ret) 326091a121bSHarald Geyer return ret; 327091a121bSHarald Geyer 328091a121bSHarald Geyer dht11->irq = gpio_to_irq(dht11->gpio); 329091a121bSHarald Geyer if (dht11->irq < 0) { 330091a121bSHarald Geyer dev_err(dev, "GPIO %d has no interrupt\n", dht11->gpio); 331091a121bSHarald Geyer return -EINVAL; 332091a121bSHarald Geyer } 333091a121bSHarald Geyer 33417a2f46cSAbhilash Jindal dht11->timestamp = ktime_get_boot_ns() - DHT11_DATA_VALID_TIME - 1; 335091a121bSHarald Geyer dht11->num_edges = -1; 336091a121bSHarald Geyer 337091a121bSHarald Geyer platform_set_drvdata(pdev, iio); 338091a121bSHarald Geyer 339091a121bSHarald Geyer init_completion(&dht11->completion); 340004bc530SRichard Weinberger mutex_init(&dht11->lock); 341091a121bSHarald Geyer iio->name = pdev->name; 342091a121bSHarald Geyer iio->dev.parent = &pdev->dev; 343091a121bSHarald Geyer iio->info = &dht11_iio_info; 344091a121bSHarald Geyer iio->modes = INDIO_DIRECT_MODE; 345091a121bSHarald Geyer iio->channels = dht11_chan_spec; 346091a121bSHarald Geyer iio->num_channels = ARRAY_SIZE(dht11_chan_spec); 347091a121bSHarald Geyer 348091a121bSHarald Geyer return devm_iio_device_register(dev, iio); 349091a121bSHarald Geyer } 350091a121bSHarald Geyer 351091a121bSHarald Geyer static struct platform_driver dht11_driver = { 352091a121bSHarald Geyer .driver = { 353091a121bSHarald Geyer .name = DRIVER_NAME, 354091a121bSHarald Geyer .of_match_table = dht11_dt_ids, 355091a121bSHarald Geyer }, 356091a121bSHarald Geyer .probe = dht11_probe, 357091a121bSHarald Geyer }; 358091a121bSHarald Geyer 359091a121bSHarald Geyer module_platform_driver(dht11_driver); 360091a121bSHarald Geyer 361091a121bSHarald Geyer MODULE_AUTHOR("Harald Geyer <harald@ccbib.org>"); 362091a121bSHarald Geyer MODULE_DESCRIPTION("DHT11 humidity/temperature sensor driver"); 363091a121bSHarald Geyer MODULE_LICENSE("GPL v2"); 364