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