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