Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 1 | /* |
| 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 Weinberger | 004bc53 | 2015-01-07 13:18:23 +0100 | [diff] [blame] | 32 | #include <linux/mutex.h> |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 33 | #include <linux/delay.h> |
| 34 | #include <linux/gpio.h> |
| 35 | #include <linux/of_gpio.h> |
Harald Geyer | 081d974 | 2015-07-07 13:39:31 +0000 | [diff] [blame] | 36 | #include <linux/timekeeping.h> |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 37 | |
| 38 | #include <linux/iio/iio.h> |
| 39 | |
| 40 | #define DRIVER_NAME "dht11" |
| 41 | |
| 42 | #define DHT11_DATA_VALID_TIME 2000000000 /* 2s in ns */ |
| 43 | |
Richard Weinberger | 94e6551 | 2015-01-07 13:22:49 +0100 | [diff] [blame] | 44 | #define DHT11_EDGES_PREAMBLE 2 |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 45 | #define DHT11_BITS_PER_READ 40 |
Richard Weinberger | 94e6551 | 2015-01-07 13:22:49 +0100 | [diff] [blame] | 46 | /* |
| 47 | * Note that when reading the sensor actually 84 edges are detected, but |
| 48 | * since the last edge is not significant, we only store 83: |
| 49 | */ |
Harald Geyer | 889c5e9 | 2015-07-07 13:39:28 +0000 | [diff] [blame] | 50 | #define DHT11_EDGES_PER_READ (2 * DHT11_BITS_PER_READ + \ |
| 51 | DHT11_EDGES_PREAMBLE + 1) |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 52 | |
Harald Geyer | 155a575 | 2016-01-17 16:13:30 +0000 | [diff] [blame^] | 53 | /* |
| 54 | * Data transmission timing: |
| 55 | * Data bits are encoded as pulse length (high time) on the data line. |
| 56 | * 0-bit: 22-30uS -- typically 26uS (AM2302) |
| 57 | * 1-bit: 68-75uS -- typically 70uS (AM2302) |
| 58 | * The acutal timings also depend on the properties of the cable, with |
| 59 | * longer cables typically making pulses shorter. |
| 60 | * |
| 61 | * Our decoding depends on the time resolution of the system: |
| 62 | * timeres > 34uS ... don't know what a 1-tick pulse is |
| 63 | * 34uS > timeres > 30uS ... no problem (30kHz and 32kHz clocks) |
| 64 | * 30uS > timeres > 23uS ... don't know what a 2-tick pulse is |
| 65 | * timeres < 23uS ... no problem |
| 66 | * |
| 67 | * Luckily clocks in the 33-44kHz range are quite uncommon, so we can |
| 68 | * support most systems if the threshold for decoding a pulse as 1-bit |
| 69 | * is chosen carefully. If somebody really wants to support clocks around |
| 70 | * 40kHz, where this driver is most unreliable, there are two options. |
| 71 | * a) select an implementation using busy loop polling on those systems |
| 72 | * b) use the checksum to do some probabilistic decoding |
| 73 | */ |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 74 | #define DHT11_START_TRANSMISSION 18 /* ms */ |
Harald Geyer | 155a575 | 2016-01-17 16:13:30 +0000 | [diff] [blame^] | 75 | #define DHT11_MIN_TIMERES 34000 /* ns */ |
| 76 | #define DHT11_THRESHOLD 49000 /* ns */ |
| 77 | #define DHT11_AMBIG_LOW 23000 /* ns */ |
| 78 | #define DHT11_AMBIG_HIGH 30000 /* ns */ |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 79 | |
| 80 | struct dht11 { |
| 81 | struct device *dev; |
| 82 | |
| 83 | int gpio; |
| 84 | int irq; |
| 85 | |
| 86 | struct completion completion; |
Harald Geyer | a712600 | 2015-07-07 13:39:29 +0000 | [diff] [blame] | 87 | /* The iio sysfs interface doesn't prevent concurrent reads: */ |
Richard Weinberger | 004bc53 | 2015-01-07 13:18:23 +0100 | [diff] [blame] | 88 | struct mutex lock; |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 89 | |
| 90 | s64 timestamp; |
| 91 | int temperature; |
| 92 | int humidity; |
| 93 | |
| 94 | /* num_edges: -1 means "no transmission in progress" */ |
| 95 | int num_edges; |
| 96 | struct {s64 ts; int value; } edges[DHT11_EDGES_PER_READ]; |
| 97 | }; |
| 98 | |
Harald Geyer | 155a575 | 2016-01-17 16:13:30 +0000 | [diff] [blame^] | 99 | static unsigned char dht11_decode_byte(char *bits) |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 100 | { |
| 101 | unsigned char ret = 0; |
| 102 | int i; |
| 103 | |
| 104 | for (i = 0; i < 8; ++i) { |
| 105 | ret <<= 1; |
Harald Geyer | 155a575 | 2016-01-17 16:13:30 +0000 | [diff] [blame^] | 106 | if (bits[i]) |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 107 | ++ret; |
| 108 | } |
| 109 | |
| 110 | return ret; |
| 111 | } |
| 112 | |
Harald Geyer | 155a575 | 2016-01-17 16:13:30 +0000 | [diff] [blame^] | 113 | static int dht11_decode(struct dht11 *dht11, int offset) |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 114 | { |
Harald Geyer | 155a575 | 2016-01-17 16:13:30 +0000 | [diff] [blame^] | 115 | int i, t; |
| 116 | char bits[DHT11_BITS_PER_READ]; |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 117 | unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum; |
| 118 | |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 119 | for (i = 0; i < DHT11_BITS_PER_READ; ++i) { |
Harald Geyer | 889c5e9 | 2015-07-07 13:39:28 +0000 | [diff] [blame] | 120 | t = dht11->edges[offset + 2 * i + 2].ts - |
| 121 | dht11->edges[offset + 2 * i + 1].ts; |
| 122 | if (!dht11->edges[offset + 2 * i + 1].value) |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 123 | return -EIO; /* lost synchronisation */ |
Harald Geyer | 155a575 | 2016-01-17 16:13:30 +0000 | [diff] [blame^] | 124 | bits[i] = t > DHT11_THRESHOLD; |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Harald Geyer | 155a575 | 2016-01-17 16:13:30 +0000 | [diff] [blame^] | 127 | hum_int = dht11_decode_byte(bits); |
| 128 | hum_dec = dht11_decode_byte(&bits[8]); |
| 129 | temp_int = dht11_decode_byte(&bits[16]); |
| 130 | temp_dec = dht11_decode_byte(&bits[24]); |
| 131 | checksum = dht11_decode_byte(&bits[32]); |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 132 | |
| 133 | if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum) |
| 134 | return -EIO; |
| 135 | |
Harald Geyer | 081d974 | 2015-07-07 13:39:31 +0000 | [diff] [blame] | 136 | dht11->timestamp = ktime_get_real_ns(); |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 137 | if (hum_int < 20) { /* DHT22 */ |
| 138 | dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) * |
| 139 | ((temp_int & 0x80) ? -100 : 100); |
| 140 | dht11->humidity = ((hum_int << 8) + hum_dec) * 100; |
| 141 | } else if (temp_dec == 0 && hum_dec == 0) { /* DHT11 */ |
| 142 | dht11->temperature = temp_int * 1000; |
| 143 | dht11->humidity = hum_int * 1000; |
| 144 | } else { |
| 145 | dev_err(dht11->dev, |
| 146 | "Don't know how to decode data: %d %d %d %d\n", |
| 147 | hum_int, hum_dec, temp_int, temp_dec); |
| 148 | return -EIO; |
| 149 | } |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
Richard Weinberger | 94e6551 | 2015-01-07 13:22:49 +0100 | [diff] [blame] | 154 | /* |
| 155 | * IRQ handler called on GPIO edges |
| 156 | */ |
| 157 | static irqreturn_t dht11_handle_irq(int irq, void *data) |
| 158 | { |
| 159 | struct iio_dev *iio = data; |
| 160 | struct dht11 *dht11 = iio_priv(iio); |
| 161 | |
| 162 | /* TODO: Consider making the handler safe for IRQ sharing */ |
| 163 | if (dht11->num_edges < DHT11_EDGES_PER_READ && dht11->num_edges >= 0) { |
Harald Geyer | 081d974 | 2015-07-07 13:39:31 +0000 | [diff] [blame] | 164 | dht11->edges[dht11->num_edges].ts = ktime_get_real_ns(); |
Richard Weinberger | 94e6551 | 2015-01-07 13:22:49 +0100 | [diff] [blame] | 165 | dht11->edges[dht11->num_edges++].value = |
| 166 | gpio_get_value(dht11->gpio); |
| 167 | |
| 168 | if (dht11->num_edges >= DHT11_EDGES_PER_READ) |
| 169 | complete(&dht11->completion); |
| 170 | } |
| 171 | |
| 172 | return IRQ_HANDLED; |
| 173 | } |
| 174 | |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 175 | static int dht11_read_raw(struct iio_dev *iio_dev, |
Harald Geyer | 889c5e9 | 2015-07-07 13:39:28 +0000 | [diff] [blame] | 176 | const struct iio_chan_spec *chan, |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 177 | int *val, int *val2, long m) |
| 178 | { |
| 179 | struct dht11 *dht11 = iio_priv(iio_dev); |
Harald Geyer | 22acc12 | 2016-01-17 16:13:29 +0000 | [diff] [blame] | 180 | int ret, timeres, offset; |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 181 | |
Richard Weinberger | 004bc53 | 2015-01-07 13:18:23 +0100 | [diff] [blame] | 182 | mutex_lock(&dht11->lock); |
Harald Geyer | 081d974 | 2015-07-07 13:39:31 +0000 | [diff] [blame] | 183 | if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_real_ns()) { |
| 184 | timeres = ktime_get_resolution_ns(); |
Harald Geyer | 155a575 | 2016-01-17 16:13:30 +0000 | [diff] [blame^] | 185 | if (timeres > DHT11_MIN_TIMERES) { |
Harald Geyer | 081d974 | 2015-07-07 13:39:31 +0000 | [diff] [blame] | 186 | dev_err(dht11->dev, "timeresolution %dns too low\n", |
| 187 | timeres); |
| 188 | /* In theory a better clock could become available |
| 189 | * at some point ... and there is no error code |
| 190 | * that really fits better. |
| 191 | */ |
| 192 | ret = -EAGAIN; |
| 193 | goto err; |
| 194 | } |
Harald Geyer | 155a575 | 2016-01-17 16:13:30 +0000 | [diff] [blame^] | 195 | if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH) |
| 196 | dev_warn(dht11->dev, |
| 197 | "timeresolution: %dns - decoding ambiguous\n", |
| 198 | timeres); |
Harald Geyer | 081d974 | 2015-07-07 13:39:31 +0000 | [diff] [blame] | 199 | |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 200 | reinit_completion(&dht11->completion); |
| 201 | |
| 202 | dht11->num_edges = 0; |
| 203 | ret = gpio_direction_output(dht11->gpio, 0); |
| 204 | if (ret) |
| 205 | goto err; |
| 206 | msleep(DHT11_START_TRANSMISSION); |
| 207 | ret = gpio_direction_input(dht11->gpio); |
| 208 | if (ret) |
| 209 | goto err; |
| 210 | |
Richard Weinberger | 94e6551 | 2015-01-07 13:22:49 +0100 | [diff] [blame] | 211 | ret = request_irq(dht11->irq, dht11_handle_irq, |
| 212 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 213 | iio_dev->name, iio_dev); |
| 214 | if (ret) |
| 215 | goto err; |
| 216 | |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 217 | ret = wait_for_completion_killable_timeout(&dht11->completion, |
Harald Geyer | 889c5e9 | 2015-07-07 13:39:28 +0000 | [diff] [blame] | 218 | HZ); |
Richard Weinberger | 94e6551 | 2015-01-07 13:22:49 +0100 | [diff] [blame] | 219 | |
| 220 | free_irq(dht11->irq, iio_dev); |
| 221 | |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 222 | if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) { |
| 223 | dev_err(&iio_dev->dev, |
Harald Geyer | 889c5e9 | 2015-07-07 13:39:28 +0000 | [diff] [blame] | 224 | "Only %d signal edges detected\n", |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 225 | dht11->num_edges); |
| 226 | ret = -ETIMEDOUT; |
| 227 | } |
| 228 | if (ret < 0) |
| 229 | goto err; |
| 230 | |
Harald Geyer | 22acc12 | 2016-01-17 16:13:29 +0000 | [diff] [blame] | 231 | offset = DHT11_EDGES_PREAMBLE + |
| 232 | dht11->num_edges - DHT11_EDGES_PER_READ; |
| 233 | for (; offset >= 0; --offset) { |
Harald Geyer | 155a575 | 2016-01-17 16:13:30 +0000 | [diff] [blame^] | 234 | ret = dht11_decode(dht11, offset); |
Harald Geyer | 22acc12 | 2016-01-17 16:13:29 +0000 | [diff] [blame] | 235 | if (!ret) |
| 236 | break; |
| 237 | } |
| 238 | |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 239 | if (ret) |
| 240 | goto err; |
| 241 | } |
| 242 | |
| 243 | ret = IIO_VAL_INT; |
| 244 | if (chan->type == IIO_TEMP) |
| 245 | *val = dht11->temperature; |
| 246 | else if (chan->type == IIO_HUMIDITYRELATIVE) |
| 247 | *val = dht11->humidity; |
| 248 | else |
| 249 | ret = -EINVAL; |
| 250 | err: |
| 251 | dht11->num_edges = -1; |
Richard Weinberger | 004bc53 | 2015-01-07 13:18:23 +0100 | [diff] [blame] | 252 | mutex_unlock(&dht11->lock); |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 253 | return ret; |
| 254 | } |
| 255 | |
| 256 | static const struct iio_info dht11_iio_info = { |
| 257 | .driver_module = THIS_MODULE, |
| 258 | .read_raw = dht11_read_raw, |
| 259 | }; |
| 260 | |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 261 | static const struct iio_chan_spec dht11_chan_spec[] = { |
| 262 | { .type = IIO_TEMP, |
| 263 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), }, |
| 264 | { .type = IIO_HUMIDITYRELATIVE, |
| 265 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), } |
| 266 | }; |
| 267 | |
| 268 | static const struct of_device_id dht11_dt_ids[] = { |
| 269 | { .compatible = "dht11", }, |
| 270 | { } |
| 271 | }; |
| 272 | MODULE_DEVICE_TABLE(of, dht11_dt_ids); |
| 273 | |
| 274 | static int dht11_probe(struct platform_device *pdev) |
| 275 | { |
| 276 | struct device *dev = &pdev->dev; |
| 277 | struct device_node *node = dev->of_node; |
| 278 | struct dht11 *dht11; |
| 279 | struct iio_dev *iio; |
| 280 | int ret; |
| 281 | |
| 282 | iio = devm_iio_device_alloc(dev, sizeof(*dht11)); |
| 283 | if (!iio) { |
| 284 | dev_err(dev, "Failed to allocate IIO device\n"); |
| 285 | return -ENOMEM; |
| 286 | } |
| 287 | |
| 288 | dht11 = iio_priv(iio); |
| 289 | dht11->dev = dev; |
| 290 | |
Harald Geyer | 5fbb0bc | 2015-07-07 13:39:30 +0000 | [diff] [blame] | 291 | ret = of_get_gpio(node, 0); |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 292 | if (ret < 0) |
| 293 | return ret; |
Harald Geyer | 5fbb0bc | 2015-07-07 13:39:30 +0000 | [diff] [blame] | 294 | dht11->gpio = ret; |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 295 | ret = devm_gpio_request_one(dev, dht11->gpio, GPIOF_IN, pdev->name); |
| 296 | if (ret) |
| 297 | return ret; |
| 298 | |
| 299 | dht11->irq = gpio_to_irq(dht11->gpio); |
| 300 | if (dht11->irq < 0) { |
| 301 | dev_err(dev, "GPIO %d has no interrupt\n", dht11->gpio); |
| 302 | return -EINVAL; |
| 303 | } |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 304 | |
Harald Geyer | 081d974 | 2015-07-07 13:39:31 +0000 | [diff] [blame] | 305 | dht11->timestamp = ktime_get_real_ns() - DHT11_DATA_VALID_TIME - 1; |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 306 | dht11->num_edges = -1; |
| 307 | |
| 308 | platform_set_drvdata(pdev, iio); |
| 309 | |
| 310 | init_completion(&dht11->completion); |
Richard Weinberger | 004bc53 | 2015-01-07 13:18:23 +0100 | [diff] [blame] | 311 | mutex_init(&dht11->lock); |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 312 | iio->name = pdev->name; |
| 313 | iio->dev.parent = &pdev->dev; |
| 314 | iio->info = &dht11_iio_info; |
| 315 | iio->modes = INDIO_DIRECT_MODE; |
| 316 | iio->channels = dht11_chan_spec; |
| 317 | iio->num_channels = ARRAY_SIZE(dht11_chan_spec); |
| 318 | |
| 319 | return devm_iio_device_register(dev, iio); |
| 320 | } |
| 321 | |
| 322 | static struct platform_driver dht11_driver = { |
| 323 | .driver = { |
| 324 | .name = DRIVER_NAME, |
Harald Geyer | 091a121 | 2013-12-01 15:04:00 +0000 | [diff] [blame] | 325 | .of_match_table = dht11_dt_ids, |
| 326 | }, |
| 327 | .probe = dht11_probe, |
| 328 | }; |
| 329 | |
| 330 | module_platform_driver(dht11_driver); |
| 331 | |
| 332 | MODULE_AUTHOR("Harald Geyer <harald@ccbib.org>"); |
| 333 | MODULE_DESCRIPTION("DHT11 humidity/temperature sensor driver"); |
| 334 | MODULE_LICENSE("GPL v2"); |