2 * SRF04: ultrasonic sensor for distance measuring by using GPIOs
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * For details about the device see:
17 * http://www.robot-electronics.co.uk/htm/srf04tech.htm
19 * the measurement cycle as timing diagram looks like:
23 * trig: --+ +------------------------------------------------------
30 * burst: ---------+ +-+ +-+ +-----------------------------------------
34 * echo: ----------------------------------+ +-+ +-+ +----------------
36 * +------------------------+
38 * echo: -------------------+ +---------------
41 * (ts_rising) (ts_falling)
42 * |<---------------------->|
44 * --> one round trip of ultra sonic waves
46 #include <linux/err.h>
47 #include <linux/gpio/consumer.h>
48 #include <linux/kernel.h>
49 #include <linux/module.h>
51 #include <linux/platform_device.h>
52 #include <linux/property.h>
53 #include <linux/sched.h>
54 #include <linux/interrupt.h>
55 #include <linux/delay.h>
56 #include <linux/iio/iio.h>
57 #include <linux/iio/sysfs.h>
61 struct gpio_desc *gpiod_trig;
62 struct gpio_desc *gpiod_echo;
67 struct completion rising;
68 struct completion falling;
71 static irqreturn_t srf04_handle_irq(int irq, void *dev_id)
73 struct iio_dev *indio_dev = dev_id;
74 struct srf04_data *data = iio_priv(indio_dev);
75 ktime_t now = ktime_get();
77 if (gpiod_get_value(data->gpiod_echo)) {
78 data->ts_rising = now;
79 complete(&data->rising);
81 data->ts_falling = now;
82 complete(&data->falling);
88 static int srf04_read(struct srf04_data *data)
93 u32 time_ns, distance_mm;
96 * just one read-echo-cycle can take place at a time
97 * ==> lock against concurrent reading calls
99 mutex_lock(&data->lock);
101 reinit_completion(&data->rising);
102 reinit_completion(&data->falling);
104 gpiod_set_value(data->gpiod_trig, 1);
106 gpiod_set_value(data->gpiod_trig, 0);
108 /* it cannot take more than 20 ms */
109 ret = wait_for_completion_killable_timeout(&data->rising, HZ/50);
111 mutex_unlock(&data->lock);
113 } else if (ret == 0) {
114 mutex_unlock(&data->lock);
118 ret = wait_for_completion_killable_timeout(&data->falling, HZ/50);
120 mutex_unlock(&data->lock);
122 } else if (ret == 0) {
123 mutex_unlock(&data->lock);
127 ktime_dt = ktime_sub(data->ts_falling, data->ts_rising);
129 mutex_unlock(&data->lock);
131 dt_ns = ktime_to_ns(ktime_dt);
133 * measuring more than 3 meters is beyond the capabilities of
135 * ==> filter out invalid results for not measuring echos of
140 * time = ---------- = --------- = 9404389 ns
143 * using a minimum speed at -20 °C of 319 m/s
151 * the speed as function of the temperature is approximately:
153 * speed = 331,5 + 0,6 * Temp
157 * use 343 m/s as ultrasonic speed at 20 °C here in absence of the
162 * distance = ------ * -----
165 * and distance in mm (one way)
167 * because we limit to 3 meters the multiplication with 343 just
170 distance_mm = time_ns * 343 / 2000000;
175 static int srf04_read_raw(struct iio_dev *indio_dev,
176 struct iio_chan_spec const *channel, int *val,
177 int *val2, long info)
179 struct srf04_data *data = iio_priv(indio_dev);
182 if (channel->type != IIO_DISTANCE)
186 case IIO_CHAN_INFO_RAW:
187 ret = srf04_read(data);
192 case IIO_CHAN_INFO_SCALE:
194 * theoretical maximum resolution is 3 mm
199 return IIO_VAL_INT_PLUS_MICRO;
205 static const struct iio_info srf04_iio_info = {
206 .read_raw = srf04_read_raw,
209 static const struct iio_chan_spec srf04_chan_spec[] = {
211 .type = IIO_DISTANCE,
212 .info_mask_separate =
213 BIT(IIO_CHAN_INFO_RAW) |
214 BIT(IIO_CHAN_INFO_SCALE),
218 static int srf04_probe(struct platform_device *pdev)
220 struct device *dev = &pdev->dev;
221 struct srf04_data *data;
222 struct iio_dev *indio_dev;
225 indio_dev = devm_iio_device_alloc(dev, sizeof(struct srf04_data));
227 dev_err(dev, "failed to allocate IIO device\n");
231 data = iio_priv(indio_dev);
234 mutex_init(&data->lock);
235 init_completion(&data->rising);
236 init_completion(&data->falling);
238 data->gpiod_trig = devm_gpiod_get(dev, "trig", GPIOD_OUT_LOW);
239 if (IS_ERR(data->gpiod_trig)) {
240 dev_err(dev, "failed to get trig-gpios: err=%ld\n",
241 PTR_ERR(data->gpiod_trig));
242 return PTR_ERR(data->gpiod_trig);
245 data->gpiod_echo = devm_gpiod_get(dev, "echo", GPIOD_IN);
246 if (IS_ERR(data->gpiod_echo)) {
247 dev_err(dev, "failed to get echo-gpios: err=%ld\n",
248 PTR_ERR(data->gpiod_echo));
249 return PTR_ERR(data->gpiod_echo);
252 if (gpiod_cansleep(data->gpiod_echo)) {
253 dev_err(data->dev, "cansleep-GPIOs not supported\n");
257 data->irqnr = gpiod_to_irq(data->gpiod_echo);
258 if (data->irqnr < 0) {
259 dev_err(data->dev, "gpiod_to_irq: %d\n", data->irqnr);
263 ret = devm_request_irq(dev, data->irqnr, srf04_handle_irq,
264 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
265 pdev->name, indio_dev);
267 dev_err(data->dev, "request_irq: %d\n", ret);
271 platform_set_drvdata(pdev, indio_dev);
273 indio_dev->name = "srf04";
274 indio_dev->dev.parent = &pdev->dev;
275 indio_dev->info = &srf04_iio_info;
276 indio_dev->modes = INDIO_DIRECT_MODE;
277 indio_dev->channels = srf04_chan_spec;
278 indio_dev->num_channels = ARRAY_SIZE(srf04_chan_spec);
280 return devm_iio_device_register(dev, indio_dev);
283 static const struct of_device_id of_srf04_match[] = {
284 { .compatible = "devantech,srf04", },
288 MODULE_DEVICE_TABLE(of, of_srf04_match);
290 static struct platform_driver srf04_driver = {
291 .probe = srf04_probe,
293 .name = "srf04-gpio",
294 .of_match_table = of_srf04_match,
298 module_platform_driver(srf04_driver);
301 MODULE_DESCRIPTION("SRF04 ultrasonic sensor for distance measuring using GPIOs");
302 MODULE_LICENSE("GPL");
303 MODULE_ALIAS("platform:srf04");