Lines Matching +full:ld +full:- +full:pulse +full:- +full:delay +full:- +full:us
1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (c) 2017 Andreas Klinger <ak@it-klinger.de>
8 * https://www.robot-electronics.co.uk/htm/srf04tech.htm
12 * +---+
14 * trig: --+ +------------------------------------------------------
16 * |<->|
19 * ultra +-+ +-+ +-+
21 * burst: ---------+ +-+ +-+ +-----------------------------------------
23 * ultra . +-+ +-+ +-+
25 * echo: ----------------------------------+ +-+ +-+ +----------------
27 * +------------------------+
29 * echo: -------------------+ +---------------
33 * |<---------------------->|
34 * pulse time measured
35 * --> one round trip of ultra sonic waves
47 #include <linux/delay.h>
85 if (gpiod_get_value(data->gpiod_echo)) { in srf04_handle_irq()
86 data->ts_rising = now; in srf04_handle_irq()
87 complete(&data->rising); in srf04_handle_irq()
89 data->ts_falling = now; in srf04_handle_irq()
90 complete(&data->falling); in srf04_handle_irq()
103 if (data->gpiod_power) in srf04_read()
104 pm_runtime_get_sync(data->dev); in srf04_read()
107 * just one read-echo-cycle can take place at a time in srf04_read()
110 mutex_lock(&data->lock); in srf04_read()
112 reinit_completion(&data->rising); in srf04_read()
113 reinit_completion(&data->falling); in srf04_read()
115 gpiod_set_value(data->gpiod_trig, 1); in srf04_read()
116 udelay(data->cfg->trigger_pulse_us); in srf04_read()
117 gpiod_set_value(data->gpiod_trig, 0); in srf04_read()
119 if (data->gpiod_power) { in srf04_read()
120 pm_runtime_mark_last_busy(data->dev); in srf04_read()
121 pm_runtime_put_autosuspend(data->dev); in srf04_read()
125 ret = wait_for_completion_killable_timeout(&data->rising, HZ/50); in srf04_read()
127 mutex_unlock(&data->lock); in srf04_read()
130 mutex_unlock(&data->lock); in srf04_read()
131 return -ETIMEDOUT; in srf04_read()
135 ret = wait_for_completion_killable_timeout(&data->falling, HZ/20); in srf04_read()
137 mutex_unlock(&data->lock); in srf04_read()
140 mutex_unlock(&data->lock); in srf04_read()
141 return -ETIMEDOUT; in srf04_read()
144 ktime_dt = ktime_sub(data->ts_falling, data->ts_rising); in srf04_read()
146 mutex_unlock(&data->lock); in srf04_read()
153 * another us sensor in srf04_read()
157 * time = ---------- = ------------ = 40438871 ns in srf04_read()
160 * using a minimum speed at -20 °C of 319 m/s in srf04_read()
163 return -EIO; in srf04_read()
179 * distance = ------ * ------- = ------------ in srf04_read()
199 if (channel->type != IIO_DISTANCE) in srf04_read_raw()
200 return -EINVAL; in srf04_read_raw()
218 return -EINVAL; in srf04_read_raw()
249 struct device *dev = &pdev->dev; in srf04_probe()
257 return -ENOMEM; in srf04_probe()
261 data->dev = dev; in srf04_probe()
262 data->cfg = of_match_device(of_srf04_match, dev)->data; in srf04_probe()
264 mutex_init(&data->lock); in srf04_probe()
265 init_completion(&data->rising); in srf04_probe()
266 init_completion(&data->falling); in srf04_probe()
268 data->gpiod_trig = devm_gpiod_get(dev, "trig", GPIOD_OUT_LOW); in srf04_probe()
269 if (IS_ERR(data->gpiod_trig)) { in srf04_probe()
270 dev_err(dev, "failed to get trig-gpios: err=%ld\n", in srf04_probe()
271 PTR_ERR(data->gpiod_trig)); in srf04_probe()
272 return PTR_ERR(data->gpiod_trig); in srf04_probe()
275 data->gpiod_echo = devm_gpiod_get(dev, "echo", GPIOD_IN); in srf04_probe()
276 if (IS_ERR(data->gpiod_echo)) { in srf04_probe()
277 dev_err(dev, "failed to get echo-gpios: err=%ld\n", in srf04_probe()
278 PTR_ERR(data->gpiod_echo)); in srf04_probe()
279 return PTR_ERR(data->gpiod_echo); in srf04_probe()
282 data->gpiod_power = devm_gpiod_get_optional(dev, "power", in srf04_probe()
284 if (IS_ERR(data->gpiod_power)) { in srf04_probe()
285 dev_err(dev, "failed to get power-gpios: err=%ld\n", in srf04_probe()
286 PTR_ERR(data->gpiod_power)); in srf04_probe()
287 return PTR_ERR(data->gpiod_power); in srf04_probe()
289 if (data->gpiod_power) { in srf04_probe()
291 if (of_property_read_u32(dev->of_node, "startup-time-ms", in srf04_probe()
292 &data->startup_time_ms)) in srf04_probe()
293 data->startup_time_ms = 100; in srf04_probe()
294 dev_dbg(dev, "using power gpio: startup-time-ms=%d\n", in srf04_probe()
295 data->startup_time_ms); in srf04_probe()
298 if (gpiod_cansleep(data->gpiod_echo)) { in srf04_probe()
299 dev_err(data->dev, "cansleep-GPIOs not supported\n"); in srf04_probe()
300 return -ENODEV; in srf04_probe()
303 data->irqnr = gpiod_to_irq(data->gpiod_echo); in srf04_probe()
304 if (data->irqnr < 0) { in srf04_probe()
305 dev_err(data->dev, "gpiod_to_irq: %d\n", data->irqnr); in srf04_probe()
306 return data->irqnr; in srf04_probe()
309 ret = devm_request_irq(dev, data->irqnr, srf04_handle_irq, in srf04_probe()
311 pdev->name, indio_dev); in srf04_probe()
313 dev_err(data->dev, "request_irq: %d\n", ret); in srf04_probe()
319 indio_dev->name = "srf04"; in srf04_probe()
320 indio_dev->info = &srf04_iio_info; in srf04_probe()
321 indio_dev->modes = INDIO_DIRECT_MODE; in srf04_probe()
322 indio_dev->channels = srf04_chan_spec; in srf04_probe()
323 indio_dev->num_channels = ARRAY_SIZE(srf04_chan_spec); in srf04_probe()
327 dev_err(data->dev, "iio_device_register: %d\n", ret); in srf04_probe()
331 if (data->gpiod_power) { in srf04_probe()
332 pm_runtime_set_autosuspend_delay(data->dev, 1000); in srf04_probe()
333 pm_runtime_use_autosuspend(data->dev); in srf04_probe()
335 ret = pm_runtime_set_active(data->dev); in srf04_probe()
337 dev_err(data->dev, "pm_runtime_set_active: %d\n", ret); in srf04_probe()
341 pm_runtime_enable(data->dev); in srf04_probe()
342 pm_runtime_idle(data->dev); in srf04_probe()
355 if (data->gpiod_power) { in srf04_remove()
356 pm_runtime_disable(data->dev); in srf04_remove()
357 pm_runtime_set_suspended(data->dev); in srf04_remove()
370 gpiod_set_value(data->gpiod_power, 0); in srf04_pm_runtime_suspend()
382 gpiod_set_value(data->gpiod_power, 1); in srf04_pm_runtime_resume()
383 msleep(data->startup_time_ms); in srf04_pm_runtime_resume()
397 .name = "srf04-gpio",
405 MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");