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