1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * SRF04: ultrasonic sensor for distance measuring by using GPIOs
5 * Copyright (c) 2017 Andreas Klinger <ak@it-klinger.de>
7 * For details about the device see:
8 * https://www.robot-electronics.co.uk/htm/srf04tech.htm
10 * the measurement cycle as timing diagram looks like:
14 * trig: --+ +------------------------------------------------------
17 * udelay(trigger_pulse_us)
21 * burst: ---------+ +-+ +-+ +-----------------------------------------
25 * echo: ----------------------------------+ +-+ +-+ +----------------
27 * +------------------------+
29 * echo: -------------------+ +---------------
32 * (ts_rising) (ts_falling)
33 * |<---------------------->|
35 * --> one round trip of ultra sonic waves
37 #include <linux/err.h>
38 #include <linux/gpio/consumer.h>
39 #include <linux/kernel.h>
40 #include <linux/module.h>
42 #include <linux/of_device.h>
43 #include <linux/platform_device.h>
44 #include <linux/property.h>
45 #include <linux/sched.h>
46 #include <linux/interrupt.h>
47 #include <linux/delay.h>
48 #include <linux/pm_runtime.h>
49 #include <linux/iio/iio.h>
50 #include <linux/iio/sysfs.h>
53 unsigned long trigger_pulse_us;
58 struct gpio_desc *gpiod_trig;
59 struct gpio_desc *gpiod_echo;
60 struct gpio_desc *gpiod_power;
65 struct completion rising;
66 struct completion falling;
67 const struct srf04_cfg *cfg;
71 static const struct srf04_cfg srf04_cfg = {
72 .trigger_pulse_us = 10,
75 static const struct srf04_cfg mb_lv_cfg = {
76 .trigger_pulse_us = 20,
79 static irqreturn_t srf04_handle_irq(int irq, void *dev_id)
81 struct iio_dev *indio_dev = dev_id;
82 struct srf04_data *data = iio_priv(indio_dev);
83 ktime_t now = ktime_get();
85 if (gpiod_get_value(data->gpiod_echo)) {
86 data->ts_rising = now;
87 complete(&data->rising);
89 data->ts_falling = now;
90 complete(&data->falling);
96 static int srf04_read(struct srf04_data *data)
101 u32 time_ns, distance_mm;
103 if (data->gpiod_power) {
104 ret = pm_runtime_resume_and_get(data->dev);
109 * just one read-echo-cycle can take place at a time
110 * ==> lock against concurrent reading calls
112 mutex_lock(&data->lock);
114 reinit_completion(&data->rising);
115 reinit_completion(&data->falling);
117 gpiod_set_value(data->gpiod_trig, 1);
118 udelay(data->cfg->trigger_pulse_us);
119 gpiod_set_value(data->gpiod_trig, 0);
121 if (data->gpiod_power) {
122 pm_runtime_mark_last_busy(data->dev);
123 pm_runtime_put_autosuspend(data->dev);
126 /* it should not take more than 20 ms until echo is rising */
127 ret = wait_for_completion_killable_timeout(&data->rising, HZ/50);
129 mutex_unlock(&data->lock);
131 } else if (ret == 0) {
132 mutex_unlock(&data->lock);
136 /* it cannot take more than 50 ms until echo is falling */
137 ret = wait_for_completion_killable_timeout(&data->falling, HZ/20);
139 mutex_unlock(&data->lock);
141 } else if (ret == 0) {
142 mutex_unlock(&data->lock);
146 ktime_dt = ktime_sub(data->ts_falling, data->ts_rising);
148 mutex_unlock(&data->lock);
150 dt_ns = ktime_to_ns(ktime_dt);
152 * measuring more than 6,45 meters is beyond the capabilities of
153 * the supported sensors
154 * ==> filter out invalid results for not measuring echos of
158 * distance 6,45 * 2 m
159 * time = ---------- = ------------ = 40438871 ns
162 * using a minimum speed at -20 °C of 319 m/s
164 if (dt_ns > 40438871)
170 * the speed as function of the temperature is approximately:
172 * speed = 331,5 + 0,6 * Temp
176 * use 343,5 m/s as ultrasonic speed at 20 °C here in absence of the
180 * time 343,5 time * 106
181 * distance = ------ * ------- = ------------
184 * and distance in mm (one way)
186 * because we limit to 6,45 meters the multiplication with 106 just
189 distance_mm = time_ns * 106 / 617176;
194 static int srf04_read_raw(struct iio_dev *indio_dev,
195 struct iio_chan_spec const *channel, int *val,
196 int *val2, long info)
198 struct srf04_data *data = iio_priv(indio_dev);
201 if (channel->type != IIO_DISTANCE)
205 case IIO_CHAN_INFO_RAW:
206 ret = srf04_read(data);
211 case IIO_CHAN_INFO_SCALE:
213 * theoretical maximum resolution is 3 mm
218 return IIO_VAL_INT_PLUS_MICRO;
224 static const struct iio_info srf04_iio_info = {
225 .read_raw = srf04_read_raw,
228 static const struct iio_chan_spec srf04_chan_spec[] = {
230 .type = IIO_DISTANCE,
231 .info_mask_separate =
232 BIT(IIO_CHAN_INFO_RAW) |
233 BIT(IIO_CHAN_INFO_SCALE),
237 static const struct of_device_id of_srf04_match[] = {
238 { .compatible = "devantech,srf04", .data = &srf04_cfg},
239 { .compatible = "maxbotix,mb1000", .data = &mb_lv_cfg},
240 { .compatible = "maxbotix,mb1010", .data = &mb_lv_cfg},
241 { .compatible = "maxbotix,mb1020", .data = &mb_lv_cfg},
242 { .compatible = "maxbotix,mb1030", .data = &mb_lv_cfg},
243 { .compatible = "maxbotix,mb1040", .data = &mb_lv_cfg},
247 MODULE_DEVICE_TABLE(of, of_srf04_match);
249 static int srf04_probe(struct platform_device *pdev)
251 struct device *dev = &pdev->dev;
252 struct srf04_data *data;
253 struct iio_dev *indio_dev;
256 indio_dev = devm_iio_device_alloc(dev, sizeof(struct srf04_data));
258 dev_err(dev, "failed to allocate IIO device\n");
262 data = iio_priv(indio_dev);
264 data->cfg = of_match_device(of_srf04_match, dev)->data;
266 mutex_init(&data->lock);
267 init_completion(&data->rising);
268 init_completion(&data->falling);
270 data->gpiod_trig = devm_gpiod_get(dev, "trig", GPIOD_OUT_LOW);
271 if (IS_ERR(data->gpiod_trig)) {
272 dev_err(dev, "failed to get trig-gpios: err=%ld\n",
273 PTR_ERR(data->gpiod_trig));
274 return PTR_ERR(data->gpiod_trig);
277 data->gpiod_echo = devm_gpiod_get(dev, "echo", GPIOD_IN);
278 if (IS_ERR(data->gpiod_echo)) {
279 dev_err(dev, "failed to get echo-gpios: err=%ld\n",
280 PTR_ERR(data->gpiod_echo));
281 return PTR_ERR(data->gpiod_echo);
284 data->gpiod_power = devm_gpiod_get_optional(dev, "power",
286 if (IS_ERR(data->gpiod_power)) {
287 dev_err(dev, "failed to get power-gpios: err=%ld\n",
288 PTR_ERR(data->gpiod_power));
289 return PTR_ERR(data->gpiod_power);
291 if (data->gpiod_power) {
293 if (of_property_read_u32(dev->of_node, "startup-time-ms",
294 &data->startup_time_ms))
295 data->startup_time_ms = 100;
296 dev_dbg(dev, "using power gpio: startup-time-ms=%d\n",
297 data->startup_time_ms);
300 if (gpiod_cansleep(data->gpiod_echo)) {
301 dev_err(data->dev, "cansleep-GPIOs not supported\n");
305 data->irqnr = gpiod_to_irq(data->gpiod_echo);
306 if (data->irqnr < 0) {
307 dev_err(data->dev, "gpiod_to_irq: %d\n", data->irqnr);
311 ret = devm_request_irq(dev, data->irqnr, srf04_handle_irq,
312 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
313 pdev->name, indio_dev);
315 dev_err(data->dev, "request_irq: %d\n", ret);
319 platform_set_drvdata(pdev, indio_dev);
321 indio_dev->name = "srf04";
322 indio_dev->info = &srf04_iio_info;
323 indio_dev->modes = INDIO_DIRECT_MODE;
324 indio_dev->channels = srf04_chan_spec;
325 indio_dev->num_channels = ARRAY_SIZE(srf04_chan_spec);
327 ret = iio_device_register(indio_dev);
329 dev_err(data->dev, "iio_device_register: %d\n", ret);
333 if (data->gpiod_power) {
334 pm_runtime_set_autosuspend_delay(data->dev, 1000);
335 pm_runtime_use_autosuspend(data->dev);
337 ret = pm_runtime_set_active(data->dev);
339 dev_err(data->dev, "pm_runtime_set_active: %d\n", ret);
340 iio_device_unregister(indio_dev);
343 pm_runtime_enable(data->dev);
344 pm_runtime_idle(data->dev);
350 static int srf04_remove(struct platform_device *pdev)
352 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
353 struct srf04_data *data = iio_priv(indio_dev);
355 iio_device_unregister(indio_dev);
357 if (data->gpiod_power) {
358 pm_runtime_disable(data->dev);
359 pm_runtime_set_suspended(data->dev);
365 static int __maybe_unused srf04_pm_runtime_suspend(struct device *dev)
367 struct platform_device *pdev = container_of(dev,
368 struct platform_device, dev);
369 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
370 struct srf04_data *data = iio_priv(indio_dev);
372 gpiod_set_value(data->gpiod_power, 0);
377 static int __maybe_unused srf04_pm_runtime_resume(struct device *dev)
379 struct platform_device *pdev = container_of(dev,
380 struct platform_device, dev);
381 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
382 struct srf04_data *data = iio_priv(indio_dev);
384 gpiod_set_value(data->gpiod_power, 1);
385 msleep(data->startup_time_ms);
390 static const struct dev_pm_ops srf04_pm_ops = {
391 SET_RUNTIME_PM_OPS(srf04_pm_runtime_suspend,
392 srf04_pm_runtime_resume, NULL)
395 static struct platform_driver srf04_driver = {
396 .probe = srf04_probe,
397 .remove = srf04_remove,
399 .name = "srf04-gpio",
400 .of_match_table = of_srf04_match,
405 module_platform_driver(srf04_driver);
407 MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
408 MODULE_DESCRIPTION("SRF04 ultrasonic sensor for distance measuring using GPIOs");
409 MODULE_LICENSE("GPL");
410 MODULE_ALIAS("platform:srf04");