dht11.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. #include <linux/err.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/device.h>
  19. #include <linux/kernel.h>
  20. #include <linux/printk.h>
  21. #include <linux/slab.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/sysfs.h>
  25. #include <linux/io.h>
  26. #include <linux/module.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/wait.h>
  29. #include <linux/bitops.h>
  30. #include <linux/completion.h>
  31. #include <linux/mutex.h>
  32. #include <linux/delay.h>
  33. #include <linux/gpio.h>
  34. #include <linux/of_gpio.h>
  35. #include <linux/timekeeping.h>
  36. #include <linux/iio/iio.h>
  37. #define DRIVER_NAME "dht11"
  38. #define DHT11_DATA_VALID_TIME 2000000000 /* 2s in ns */
  39. #define DHT11_EDGES_PREAMBLE 2
  40. #define DHT11_BITS_PER_READ 40
  41. /*
  42. * Note that when reading the sensor actually 84 edges are detected, but
  43. * since the last edge is not significant, we only store 83:
  44. */
  45. #define DHT11_EDGES_PER_READ (2 * DHT11_BITS_PER_READ + \
  46. DHT11_EDGES_PREAMBLE + 1)
  47. /* Data transmission timing (nano seconds) */
  48. #define DHT11_START_TRANSMISSION 18 /* ms */
  49. #define DHT11_SENSOR_RESPONSE 80000
  50. #define DHT11_START_BIT 50000
  51. #define DHT11_DATA_BIT_LOW 27000
  52. #define DHT11_DATA_BIT_HIGH 70000
  53. struct dht11 {
  54. struct device *dev;
  55. int gpio;
  56. int irq;
  57. struct completion completion;
  58. /* The iio sysfs interface doesn't prevent concurrent reads: */
  59. struct mutex lock;
  60. s64 timestamp;
  61. int temperature;
  62. int humidity;
  63. /* num_edges: -1 means "no transmission in progress" */
  64. int num_edges;
  65. struct {s64 ts; int value; } edges[DHT11_EDGES_PER_READ];
  66. };
  67. static unsigned char dht11_decode_byte(int *timing, int threshold)
  68. {
  69. unsigned char ret = 0;
  70. int i;
  71. for (i = 0; i < 8; ++i) {
  72. ret <<= 1;
  73. if (timing[i] >= threshold)
  74. ++ret;
  75. }
  76. return ret;
  77. }
  78. static int dht11_decode(struct dht11 *dht11, int offset, int timeres)
  79. {
  80. int i, t, timing[DHT11_BITS_PER_READ], threshold;
  81. unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum;
  82. threshold = DHT11_DATA_BIT_HIGH / timeres;
  83. if (DHT11_DATA_BIT_LOW / timeres + 1 >= threshold)
  84. pr_err("dht11: WARNING: decoding ambiguous\n");
  85. /* scale down with timeres and check validity */
  86. for (i = 0; i < DHT11_BITS_PER_READ; ++i) {
  87. t = dht11->edges[offset + 2 * i + 2].ts -
  88. dht11->edges[offset + 2 * i + 1].ts;
  89. if (!dht11->edges[offset + 2 * i + 1].value)
  90. return -EIO; /* lost synchronisation */
  91. timing[i] = t / timeres;
  92. }
  93. hum_int = dht11_decode_byte(timing, threshold);
  94. hum_dec = dht11_decode_byte(&timing[8], threshold);
  95. temp_int = dht11_decode_byte(&timing[16], threshold);
  96. temp_dec = dht11_decode_byte(&timing[24], threshold);
  97. checksum = dht11_decode_byte(&timing[32], threshold);
  98. if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum)
  99. return -EIO;
  100. dht11->timestamp = ktime_get_real_ns();
  101. if (hum_int < 20) { /* DHT22 */
  102. dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
  103. ((temp_int & 0x80) ? -100 : 100);
  104. dht11->humidity = ((hum_int << 8) + hum_dec) * 100;
  105. } else if (temp_dec == 0 && hum_dec == 0) { /* DHT11 */
  106. dht11->temperature = temp_int * 1000;
  107. dht11->humidity = hum_int * 1000;
  108. } else {
  109. dev_err(dht11->dev,
  110. "Don't know how to decode data: %d %d %d %d\n",
  111. hum_int, hum_dec, temp_int, temp_dec);
  112. return -EIO;
  113. }
  114. return 0;
  115. }
  116. /*
  117. * IRQ handler called on GPIO edges
  118. */
  119. static irqreturn_t dht11_handle_irq(int irq, void *data)
  120. {
  121. struct iio_dev *iio = data;
  122. struct dht11 *dht11 = iio_priv(iio);
  123. /* TODO: Consider making the handler safe for IRQ sharing */
  124. if (dht11->num_edges < DHT11_EDGES_PER_READ && dht11->num_edges >= 0) {
  125. dht11->edges[dht11->num_edges].ts = ktime_get_real_ns();
  126. dht11->edges[dht11->num_edges++].value =
  127. gpio_get_value(dht11->gpio);
  128. if (dht11->num_edges >= DHT11_EDGES_PER_READ)
  129. complete(&dht11->completion);
  130. }
  131. return IRQ_HANDLED;
  132. }
  133. static int dht11_read_raw(struct iio_dev *iio_dev,
  134. const struct iio_chan_spec *chan,
  135. int *val, int *val2, long m)
  136. {
  137. struct dht11 *dht11 = iio_priv(iio_dev);
  138. int ret, timeres;
  139. mutex_lock(&dht11->lock);
  140. if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_real_ns()) {
  141. timeres = ktime_get_resolution_ns();
  142. if (DHT11_DATA_BIT_HIGH < 2 * timeres) {
  143. dev_err(dht11->dev, "timeresolution %dns too low\n",
  144. timeres);
  145. /* In theory a better clock could become available
  146. * at some point ... and there is no error code
  147. * that really fits better.
  148. */
  149. ret = -EAGAIN;
  150. goto err;
  151. }
  152. reinit_completion(&dht11->completion);
  153. dht11->num_edges = 0;
  154. ret = gpio_direction_output(dht11->gpio, 0);
  155. if (ret)
  156. goto err;
  157. msleep(DHT11_START_TRANSMISSION);
  158. ret = gpio_direction_input(dht11->gpio);
  159. if (ret)
  160. goto err;
  161. ret = request_irq(dht11->irq, dht11_handle_irq,
  162. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  163. iio_dev->name, iio_dev);
  164. if (ret)
  165. goto err;
  166. ret = wait_for_completion_killable_timeout(&dht11->completion,
  167. HZ);
  168. free_irq(dht11->irq, iio_dev);
  169. if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
  170. dev_err(&iio_dev->dev,
  171. "Only %d signal edges detected\n",
  172. dht11->num_edges);
  173. ret = -ETIMEDOUT;
  174. }
  175. if (ret < 0)
  176. goto err;
  177. ret = dht11_decode(dht11,
  178. dht11->num_edges == DHT11_EDGES_PER_READ ?
  179. DHT11_EDGES_PREAMBLE :
  180. DHT11_EDGES_PREAMBLE - 2,
  181. timeres);
  182. if (ret)
  183. goto err;
  184. }
  185. ret = IIO_VAL_INT;
  186. if (chan->type == IIO_TEMP)
  187. *val = dht11->temperature;
  188. else if (chan->type == IIO_HUMIDITYRELATIVE)
  189. *val = dht11->humidity;
  190. else
  191. ret = -EINVAL;
  192. err:
  193. dht11->num_edges = -1;
  194. mutex_unlock(&dht11->lock);
  195. return ret;
  196. }
  197. static const struct iio_info dht11_iio_info = {
  198. .driver_module = THIS_MODULE,
  199. .read_raw = dht11_read_raw,
  200. };
  201. static const struct iio_chan_spec dht11_chan_spec[] = {
  202. { .type = IIO_TEMP,
  203. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), },
  204. { .type = IIO_HUMIDITYRELATIVE,
  205. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), }
  206. };
  207. static const struct of_device_id dht11_dt_ids[] = {
  208. { .compatible = "dht11", },
  209. { }
  210. };
  211. MODULE_DEVICE_TABLE(of, dht11_dt_ids);
  212. static int dht11_probe(struct platform_device *pdev)
  213. {
  214. struct device *dev = &pdev->dev;
  215. struct device_node *node = dev->of_node;
  216. struct dht11 *dht11;
  217. struct iio_dev *iio;
  218. int ret;
  219. iio = devm_iio_device_alloc(dev, sizeof(*dht11));
  220. if (!iio) {
  221. dev_err(dev, "Failed to allocate IIO device\n");
  222. return -ENOMEM;
  223. }
  224. dht11 = iio_priv(iio);
  225. dht11->dev = dev;
  226. ret = of_get_gpio(node, 0);
  227. if (ret < 0)
  228. return ret;
  229. dht11->gpio = ret;
  230. ret = devm_gpio_request_one(dev, dht11->gpio, GPIOF_IN, pdev->name);
  231. if (ret)
  232. return ret;
  233. dht11->irq = gpio_to_irq(dht11->gpio);
  234. if (dht11->irq < 0) {
  235. dev_err(dev, "GPIO %d has no interrupt\n", dht11->gpio);
  236. return -EINVAL;
  237. }
  238. dht11->timestamp = ktime_get_real_ns() - DHT11_DATA_VALID_TIME - 1;
  239. dht11->num_edges = -1;
  240. platform_set_drvdata(pdev, iio);
  241. init_completion(&dht11->completion);
  242. mutex_init(&dht11->lock);
  243. iio->name = pdev->name;
  244. iio->dev.parent = &pdev->dev;
  245. iio->info = &dht11_iio_info;
  246. iio->modes = INDIO_DIRECT_MODE;
  247. iio->channels = dht11_chan_spec;
  248. iio->num_channels = ARRAY_SIZE(dht11_chan_spec);
  249. return devm_iio_device_register(dev, iio);
  250. }
  251. static struct platform_driver dht11_driver = {
  252. .driver = {
  253. .name = DRIVER_NAME,
  254. .of_match_table = dht11_dt_ids,
  255. },
  256. .probe = dht11_probe,
  257. };
  258. module_platform_driver(dht11_driver);
  259. MODULE_AUTHOR("Harald Geyer <harald@ccbib.org>");
  260. MODULE_DESCRIPTION("DHT11 humidity/temperature sensor driver");
  261. MODULE_LICENSE("GPL v2");