Lines Matching defs:dht11
30 #define DRIVER_NAME "dht11"
71 struct dht11 {
95 static void dht11_edges_print(struct dht11 *dht11)
99 dev_dbg(dht11->dev, "%d edges detected:\n", dht11->num_edges);
100 for (i = 1; i < dht11->num_edges; ++i) {
101 dev_dbg(dht11->dev, "%d: %lld ns %s\n", i,
102 dht11->edges[i].ts - dht11->edges[i - 1].ts,
103 dht11->edges[i - 1].value ? "high" : "low");
122 static int dht11_decode(struct dht11 *dht11, int offset)
129 t = dht11->edges[offset + 2 * i + 2].ts -
130 dht11->edges[offset + 2 * i + 1].ts;
131 if (!dht11->edges[offset + 2 * i + 1].value) {
132 dev_dbg(dht11->dev,
147 dev_dbg(dht11->dev, "invalid checksum\n");
151 dht11->timestamp = ktime_get_boottime_ns();
153 dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
155 dht11->humidity = ((hum_int << 8) + hum_dec) * 100;
157 dht11->temperature = temp_int * 1000;
158 dht11->humidity = hum_int * 1000;
160 dev_err(dht11->dev,
175 struct dht11 *dht11 = iio_priv(iio);
177 if (dht11->num_edges < DHT11_EDGES_PER_READ && dht11->num_edges >= 0) {
178 dht11->edges[dht11->num_edges].ts = ktime_get_boottime_ns();
179 dht11->edges[dht11->num_edges++].value =
180 gpiod_get_value(dht11->gpiod);
182 if (dht11->num_edges >= DHT11_EDGES_PER_READ)
183 complete(&dht11->completion);
193 struct dht11 *dht11 = iio_priv(iio_dev);
196 mutex_lock(&dht11->lock);
197 if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boottime_ns()) {
199 dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
201 dev_err(dht11->dev, "timeresolution %dns too low\n",
211 dev_warn(dht11->dev,
215 reinit_completion(&dht11->completion);
217 dht11->num_edges = 0;
218 ret = gpiod_direction_output(dht11->gpiod, 0);
223 ret = gpiod_direction_input(dht11->gpiod);
227 ret = request_irq(dht11->irq, dht11_handle_irq,
233 ret = wait_for_completion_killable_timeout(&dht11->completion,
236 free_irq(dht11->irq, iio_dev);
239 dht11_edges_print(dht11);
242 if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
243 dev_err(dht11->dev, "Only %d signal edges detected\n",
244 dht11->num_edges);
251 dht11->num_edges - DHT11_EDGES_PER_READ;
253 ret = dht11_decode(dht11, offset);
264 *val = dht11->temperature;
266 *val = dht11->humidity;
270 dht11->num_edges = -1;
271 mutex_unlock(&dht11->lock);
287 { .compatible = "dht11", },
295 struct dht11 *dht11;
298 iio = devm_iio_device_alloc(dev, sizeof(*dht11));
304 dht11 = iio_priv(iio);
305 dht11->dev = dev;
306 dht11->gpiod = devm_gpiod_get(dev, NULL, GPIOD_IN);
307 if (IS_ERR(dht11->gpiod))
308 return PTR_ERR(dht11->gpiod);
310 dht11->irq = gpiod_to_irq(dht11->gpiod);
311 if (dht11->irq < 0) {
312 dev_err(dev, "GPIO %d has no interrupt\n", desc_to_gpio(dht11->gpiod));
316 dht11->timestamp = ktime_get_boottime_ns() - DHT11_DATA_VALID_TIME - 1;
317 dht11->num_edges = -1;
321 init_completion(&dht11->completion);
322 mutex_init(&dht11->lock);