• Home
  • Raw
  • Download

Lines Matching +full:bit +full:- +full:banging

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * DHT11/DHT22 bit banging GPIO driver
46 * 0-bit: 22-30uS -- typically 26uS (AM2302)
47 * 1-bit: 68-75uS -- typically 70uS (AM2302)
52 * timeres > 34uS ... don't know what a 1-tick pulse is
54 * 30uS > timeres > 23uS ... don't know what a 2-tick pulse is
57 * Luckily clocks in the 33-44kHz range are quite uncommon, so we can
58 * support most systems if the threshold for decoding a pulse as 1-bit
85 /* num_edges: -1 means "no transmission in progress" */
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()
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()
135 return -EIO; in dht11_decode()
147 dev_dbg(dht11->dev, "invalid checksum\n"); in dht11_decode()
148 return -EIO; 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()
154 ((temp_int & 0x80) ? -100 : 100); 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()
163 return -EIO; in dht11_decode()
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()
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()
207 ret = -EAGAIN; in dht11_read_raw()
211 dev_warn(dht11->dev, in dht11_read_raw()
212 "timeresolution: %dns - decoding ambiguous\n", 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()
229 iio_dev->name, iio_dev); 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()
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()
245 ret = -ETIMEDOUT; in dht11_read_raw()
251 dht11->num_edges - DHT11_EDGES_PER_READ; in dht11_read_raw()
252 for (; offset >= 0; --offset) { in dht11_read_raw()
263 if (chan->type == IIO_TEMP) in dht11_read_raw()
264 *val = dht11->temperature; in dht11_read_raw()
265 else if (chan->type == IIO_HUMIDITYRELATIVE) in dht11_read_raw()
266 *val = dht11->humidity; in dht11_read_raw()
268 ret = -EINVAL; in dht11_read_raw()
270 dht11->num_edges = -1; in dht11_read_raw()
271 mutex_unlock(&dht11->lock); in dht11_read_raw()
281 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), },
283 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), }
294 struct device *dev = &pdev->dev; in dht11_probe()
301 return -ENOMEM; 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()
313 return -EINVAL; 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()
323 iio->name = pdev->name; in dht11_probe()
324 iio->info = &dht11_iio_info; in dht11_probe()
325 iio->modes = INDIO_DIRECT_MODE; in dht11_probe()
326 iio->channels = dht11_chan_spec; in dht11_probe()
327 iio->num_channels = ARRAY_SIZE(dht11_chan_spec); in dht11_probe()