1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * PING: ultrasonic sensor for distance measuring by using only one GPIOs
5 * Copyright (c) 2019 Andreas Klinger <ak@it-klinger.de>
7 * For details about the devices see:
8 * http://parallax.com/sites/default/files/downloads/28041-LaserPING-2m-Rangefinder-Guide.pdf
9 * http://parallax.com/sites/default/files/downloads/28015-PING-Documentation-v1.6.pdf
11 * the measurement cycle as timing diagram looks like:
13 * GPIO ___ ________________________
14 * ping: __/ \____________/ \________________
16 * |<->| interrupt interrupt
17 * udelay(5) (ts_rising) (ts_falling)
18 * |<---------------------->|
19 * . pulse time measured .
20 * . --> one round trip of ultra sonic waves
23 * burst: _________/ \_/ \_/ \_________________________________________
27 * echo: __________________________________/ \_/ \_/ \________________
29 #include <linux/err.h>
30 #include <linux/gpio/consumer.h>
31 #include <linux/kernel.h>
32 #include <linux/mod_devicetable.h>
33 #include <linux/module.h>
34 #include <linux/platform_device.h>
35 #include <linux/property.h>
36 #include <linux/sched.h>
37 #include <linux/interrupt.h>
38 #include <linux/delay.h>
39 #include <linux/iio/iio.h>
40 #include <linux/iio/sysfs.h>
43 unsigned long trigger_pulse_us
; /* length of trigger pulse */
44 int laserping_error
; /* support error code in */
45 /* pulse width of laser */
47 s64 timeout_ns
; /* timeout in ns */
52 struct gpio_desc
*gpiod_ping
;
57 struct completion rising
;
58 struct completion falling
;
59 const struct ping_cfg
*cfg
;
62 static const struct ping_cfg pa_ping_cfg
= {
63 .trigger_pulse_us
= 5,
65 .timeout_ns
= 18500000, /* 3 meters */
68 static const struct ping_cfg pa_laser_ping_cfg
= {
69 .trigger_pulse_us
= 5,
71 .timeout_ns
= 15500000, /* 2 meters plus error codes */
74 static irqreturn_t
ping_handle_irq(int irq
, void *dev_id
)
76 struct iio_dev
*indio_dev
= dev_id
;
77 struct ping_data
*data
= iio_priv(indio_dev
);
78 ktime_t now
= ktime_get();
80 if (gpiod_get_value(data
->gpiod_ping
)) {
81 data
->ts_rising
= now
;
82 complete(&data
->rising
);
84 data
->ts_falling
= now
;
85 complete(&data
->falling
);
91 static int ping_read(struct iio_dev
*indio_dev
)
93 struct ping_data
*data
= iio_priv(indio_dev
);
97 u32 time_ns
, distance_mm
;
98 struct platform_device
*pdev
= to_platform_device(data
->dev
);
101 * just one read-echo-cycle can take place at a time
102 * ==> lock against concurrent reading calls
104 mutex_lock(&data
->lock
);
106 reinit_completion(&data
->rising
);
107 reinit_completion(&data
->falling
);
109 gpiod_set_value(data
->gpiod_ping
, 1);
110 udelay(data
->cfg
->trigger_pulse_us
);
111 gpiod_set_value(data
->gpiod_ping
, 0);
113 ret
= gpiod_direction_input(data
->gpiod_ping
);
115 mutex_unlock(&data
->lock
);
119 data
->irqnr
= gpiod_to_irq(data
->gpiod_ping
);
120 if (data
->irqnr
< 0) {
121 dev_err(data
->dev
, "gpiod_to_irq: %d\n", data
->irqnr
);
122 mutex_unlock(&data
->lock
);
126 ret
= request_irq(data
->irqnr
, ping_handle_irq
,
127 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
128 pdev
->name
, indio_dev
);
130 dev_err(data
->dev
, "request_irq: %d\n", ret
);
131 mutex_unlock(&data
->lock
);
135 /* it should not take more than 20 ms until echo is rising */
136 ret
= wait_for_completion_killable_timeout(&data
->rising
, HZ
/50);
138 goto err_reset_direction
;
141 goto err_reset_direction
;
144 /* it cannot take more than 50 ms until echo is falling */
145 ret
= wait_for_completion_killable_timeout(&data
->falling
, HZ
/20);
147 goto err_reset_direction
;
150 goto err_reset_direction
;
153 ktime_dt
= ktime_sub(data
->ts_falling
, data
->ts_rising
);
155 free_irq(data
->irqnr
, indio_dev
);
157 ret
= gpiod_direction_output(data
->gpiod_ping
, GPIOD_OUT_LOW
);
159 mutex_unlock(&data
->lock
);
163 mutex_unlock(&data
->lock
);
165 dt_ns
= ktime_to_ns(ktime_dt
);
166 if (dt_ns
> data
->cfg
->timeout_ns
) {
167 dev_dbg(data
->dev
, "distance out of range: dt=%lldns\n",
175 * read error code of laser ping sensor and give users chance to
176 * figure out error by using dynamic debugging
178 if (data
->cfg
->laserping_error
) {
179 if ((time_ns
> 12500000) && (time_ns
<= 13500000)) {
180 dev_dbg(data
->dev
, "target too close or to far\n");
183 if ((time_ns
> 13500000) && (time_ns
<= 14500000)) {
184 dev_dbg(data
->dev
, "internal sensor error\n");
187 if ((time_ns
> 14500000) && (time_ns
<= 15500000)) {
188 dev_dbg(data
->dev
, "internal sensor timeout\n");
194 * the speed as function of the temperature is approximately:
196 * speed = 331,5 + 0,6 * Temp
200 * use 343,5 m/s as ultrasonic speed at 20 °C here in absence of the
204 * time 343,5 time * 232
205 * distance = ------ * ------- = ------------
208 * and distance in mm (one way)
210 * because we limit to 3 meters the multiplication with 232 just
213 distance_mm
= time_ns
* 232 / 1350800;
218 free_irq(data
->irqnr
, indio_dev
);
219 mutex_unlock(&data
->lock
);
221 if (gpiod_direction_output(data
->gpiod_ping
, GPIOD_OUT_LOW
))
222 dev_dbg(data
->dev
, "error in gpiod_direction_output\n");
226 static int ping_read_raw(struct iio_dev
*indio_dev
,
227 struct iio_chan_spec
const *channel
, int *val
,
228 int *val2
, long info
)
232 if (channel
->type
!= IIO_DISTANCE
)
236 case IIO_CHAN_INFO_RAW
:
237 ret
= ping_read(indio_dev
);
242 case IIO_CHAN_INFO_SCALE
:
244 * maximum resolution in datasheet is 1 mm
249 return IIO_VAL_INT_PLUS_MICRO
;
255 static const struct iio_info ping_iio_info
= {
256 .read_raw
= ping_read_raw
,
259 static const struct iio_chan_spec ping_chan_spec
[] = {
261 .type
= IIO_DISTANCE
,
262 .info_mask_separate
=
263 BIT(IIO_CHAN_INFO_RAW
) |
264 BIT(IIO_CHAN_INFO_SCALE
),
268 static const struct of_device_id of_ping_match
[] = {
269 { .compatible
= "parallax,ping", .data
= &pa_ping_cfg
},
270 { .compatible
= "parallax,laserping", .data
= &pa_laser_ping_cfg
},
274 MODULE_DEVICE_TABLE(of
, of_ping_match
);
276 static int ping_probe(struct platform_device
*pdev
)
278 struct device
*dev
= &pdev
->dev
;
279 struct ping_data
*data
;
280 struct iio_dev
*indio_dev
;
282 indio_dev
= devm_iio_device_alloc(dev
, sizeof(struct ping_data
));
284 dev_err(dev
, "failed to allocate IIO device\n");
288 data
= iio_priv(indio_dev
);
290 data
->cfg
= device_get_match_data(dev
);
292 mutex_init(&data
->lock
);
293 init_completion(&data
->rising
);
294 init_completion(&data
->falling
);
296 data
->gpiod_ping
= devm_gpiod_get(dev
, "ping", GPIOD_OUT_LOW
);
297 if (IS_ERR(data
->gpiod_ping
)) {
298 dev_err(dev
, "failed to get ping-gpios: err=%ld\n",
299 PTR_ERR(data
->gpiod_ping
));
300 return PTR_ERR(data
->gpiod_ping
);
303 if (gpiod_cansleep(data
->gpiod_ping
)) {
304 dev_err(data
->dev
, "cansleep-GPIOs not supported\n");
308 platform_set_drvdata(pdev
, indio_dev
);
310 indio_dev
->name
= "ping";
311 indio_dev
->info
= &ping_iio_info
;
312 indio_dev
->modes
= INDIO_DIRECT_MODE
;
313 indio_dev
->channels
= ping_chan_spec
;
314 indio_dev
->num_channels
= ARRAY_SIZE(ping_chan_spec
);
316 return devm_iio_device_register(dev
, indio_dev
);
319 static struct platform_driver ping_driver
= {
323 .of_match_table
= of_ping_match
,
327 module_platform_driver(ping_driver
);
329 MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
330 MODULE_DESCRIPTION("PING sensors for distance measuring using one GPIOs");
331 MODULE_LICENSE("GPL");
332 MODULE_ALIAS("platform:ping");