Lines Matching defs:dht11
29 #define DRIVER_NAME "dht11"
70 struct dht11 {
94 static void dht11_edges_print(struct dht11 *dht11)
98 dev_dbg(dht11->dev, "%d edges detected:\n", dht11->num_edges);
99 for (i = 1; i < dht11->num_edges; ++i) {
100 dev_dbg(dht11->dev, "%d: %lld ns %s\n", i,
101 dht11->edges[i].ts - dht11->edges[i - 1].ts,
102 dht11->edges[i - 1].value ? "high" : "low");
121 static int dht11_decode(struct dht11 *dht11, int offset)
128 t = dht11->edges[offset + 2 * i + 2].ts -
129 dht11->edges[offset + 2 * i + 1].ts;
130 if (!dht11->edges[offset + 2 * i + 1].value) {
131 dev_dbg(dht11->dev,
146 dev_dbg(dht11->dev, "invalid checksum\n");
150 dht11->timestamp = ktime_get_boottime_ns();
152 dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
154 dht11->humidity = ((hum_int << 8) + hum_dec) * 100;
156 dht11->temperature = temp_int * 1000;
157 dht11->humidity = hum_int * 1000;
159 dev_err(dht11->dev,
174 struct dht11 *dht11 = iio_priv(iio);
176 if (dht11->num_edges < DHT11_EDGES_PER_READ && dht11->num_edges >= 0) {
177 dht11->edges[dht11->num_edges].ts = ktime_get_boottime_ns();
178 dht11->edges[dht11->num_edges++].value =
179 gpiod_get_value(dht11->gpiod);
181 if (dht11->num_edges >= DHT11_EDGES_PER_READ)
182 complete(&dht11->completion);
192 struct dht11 *dht11 = iio_priv(iio_dev);
195 mutex_lock(&dht11->lock);
196 if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boottime_ns()) {
198 dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
200 dev_err(dht11->dev, "timeresolution %dns too low\n",
210 dev_warn(dht11->dev,
214 reinit_completion(&dht11->completion);
216 dht11->num_edges = 0;
217 ret = gpiod_direction_output(dht11->gpiod, 0);
222 ret = gpiod_direction_input(dht11->gpiod);
226 ret = request_irq(dht11->irq, dht11_handle_irq,
232 ret = wait_for_completion_killable_timeout(&dht11->completion,
235 free_irq(dht11->irq, iio_dev);
238 dht11_edges_print(dht11);
241 if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
242 dev_err(dht11->dev, "Only %d signal edges detected\n",
243 dht11->num_edges);
250 dht11->num_edges - DHT11_EDGES_PER_READ;
252 ret = dht11_decode(dht11, offset);
263 *val = dht11->temperature;
265 *val = dht11->humidity;
269 dht11->num_edges = -1;
270 mutex_unlock(&dht11->lock);
286 { .compatible = "dht11", },
294 struct dht11 *dht11;
297 iio = devm_iio_device_alloc(dev, sizeof(*dht11));
303 dht11 = iio_priv(iio);
304 dht11->dev = dev;
305 dht11->gpiod = devm_gpiod_get(dev, NULL, GPIOD_IN);
306 if (IS_ERR(dht11->gpiod))
307 return PTR_ERR(dht11->gpiod);
309 dht11->irq = gpiod_to_irq(dht11->gpiod);
310 if (dht11->irq < 0) {
311 dev_err(dev, "GPIO %d has no interrupt\n", desc_to_gpio(dht11->gpiod));
315 dht11->timestamp = ktime_get_boottime_ns() - DHT11_DATA_VALID_TIME - 1;
316 dht11->num_edges = -1;
320 init_completion(&dht11->completion);
321 mutex_init(&dht11->lock);