blob: 01eb8cc63076f32a18b4a19061252e33796e0d0a [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * SRF04: ultrasonic sensor for distance measuring by using GPIOs
4 *
5 * Copyright (c) 2017 Andreas Klinger <ak@it-klinger.de>
6 *
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007 * For details about the device see:
8 * http://www.robot-electronics.co.uk/htm/srf04tech.htm
9 *
10 * the measurement cycle as timing diagram looks like:
11 *
12 * +---+
13 * GPIO | |
14 * trig: --+ +------------------------------------------------------
15 * ^ ^
16 * |<->|
David Brazdil0f672f62019-12-10 10:32:29 +000017 * udelay(trigger_pulse_us)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000018 *
19 * ultra +-+ +-+ +-+
20 * sonic | | | | | |
21 * burst: ---------+ +-+ +-+ +-----------------------------------------
22 * .
23 * ultra . +-+ +-+ +-+
24 * sonic . | | | | | |
25 * echo: ----------------------------------+ +-+ +-+ +----------------
26 * . .
27 * +------------------------+
28 * GPIO | |
29 * echo: -------------------+ +---------------
30 * ^ ^
31 * interrupt interrupt
32 * (ts_rising) (ts_falling)
33 * |<---------------------->|
34 * pulse time measured
35 * --> one round trip of ultra sonic waves
36 */
37#include <linux/err.h>
38#include <linux/gpio/consumer.h>
39#include <linux/kernel.h>
40#include <linux/module.h>
41#include <linux/of.h>
David Brazdil0f672f62019-12-10 10:32:29 +000042#include <linux/of_device.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000043#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/iio/iio.h>
49#include <linux/iio/sysfs.h>
50
David Brazdil0f672f62019-12-10 10:32:29 +000051struct srf04_cfg {
52 unsigned long trigger_pulse_us;
53};
54
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000055struct srf04_data {
56 struct device *dev;
57 struct gpio_desc *gpiod_trig;
58 struct gpio_desc *gpiod_echo;
59 struct mutex lock;
60 int irqnr;
61 ktime_t ts_rising;
62 ktime_t ts_falling;
63 struct completion rising;
64 struct completion falling;
David Brazdil0f672f62019-12-10 10:32:29 +000065 const struct srf04_cfg *cfg;
66};
67
68static const struct srf04_cfg srf04_cfg = {
69 .trigger_pulse_us = 10,
70};
71
72static const struct srf04_cfg mb_lv_cfg = {
73 .trigger_pulse_us = 20,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000074};
75
76static irqreturn_t srf04_handle_irq(int irq, void *dev_id)
77{
78 struct iio_dev *indio_dev = dev_id;
79 struct srf04_data *data = iio_priv(indio_dev);
80 ktime_t now = ktime_get();
81
82 if (gpiod_get_value(data->gpiod_echo)) {
83 data->ts_rising = now;
84 complete(&data->rising);
85 } else {
86 data->ts_falling = now;
87 complete(&data->falling);
88 }
89
90 return IRQ_HANDLED;
91}
92
93static int srf04_read(struct srf04_data *data)
94{
95 int ret;
96 ktime_t ktime_dt;
97 u64 dt_ns;
98 u32 time_ns, distance_mm;
99
100 /*
101 * just one read-echo-cycle can take place at a time
102 * ==> lock against concurrent reading calls
103 */
104 mutex_lock(&data->lock);
105
106 reinit_completion(&data->rising);
107 reinit_completion(&data->falling);
108
109 gpiod_set_value(data->gpiod_trig, 1);
David Brazdil0f672f62019-12-10 10:32:29 +0000110 udelay(data->cfg->trigger_pulse_us);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000111 gpiod_set_value(data->gpiod_trig, 0);
112
David Brazdil0f672f62019-12-10 10:32:29 +0000113 /* it should not take more than 20 ms until echo is rising */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000114 ret = wait_for_completion_killable_timeout(&data->rising, HZ/50);
115 if (ret < 0) {
116 mutex_unlock(&data->lock);
117 return ret;
118 } else if (ret == 0) {
119 mutex_unlock(&data->lock);
120 return -ETIMEDOUT;
121 }
122
David Brazdil0f672f62019-12-10 10:32:29 +0000123 /* it cannot take more than 50 ms until echo is falling */
124 ret = wait_for_completion_killable_timeout(&data->falling, HZ/20);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000125 if (ret < 0) {
126 mutex_unlock(&data->lock);
127 return ret;
128 } else if (ret == 0) {
129 mutex_unlock(&data->lock);
130 return -ETIMEDOUT;
131 }
132
133 ktime_dt = ktime_sub(data->ts_falling, data->ts_rising);
134
135 mutex_unlock(&data->lock);
136
137 dt_ns = ktime_to_ns(ktime_dt);
138 /*
David Brazdil0f672f62019-12-10 10:32:29 +0000139 * measuring more than 6,45 meters is beyond the capabilities of
140 * the supported sensors
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000141 * ==> filter out invalid results for not measuring echos of
142 * another us sensor
143 *
144 * formula:
David Brazdil0f672f62019-12-10 10:32:29 +0000145 * distance 6,45 * 2 m
146 * time = ---------- = ------------ = 40438871 ns
147 * speed 319 m/s
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000148 *
149 * using a minimum speed at -20 °C of 319 m/s
150 */
David Brazdil0f672f62019-12-10 10:32:29 +0000151 if (dt_ns > 40438871)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000152 return -EIO;
153
154 time_ns = dt_ns;
155
156 /*
157 * the speed as function of the temperature is approximately:
158 *
159 * speed = 331,5 + 0,6 * Temp
160 * with Temp in °C
161 * and speed in m/s
162 *
David Brazdil0f672f62019-12-10 10:32:29 +0000163 * use 343,5 m/s as ultrasonic speed at 20 °C here in absence of the
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000164 * temperature
165 *
166 * therefore:
David Brazdil0f672f62019-12-10 10:32:29 +0000167 * time 343,5 time * 106
168 * distance = ------ * ------- = ------------
169 * 10^6 2 617176
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000170 * with time in ns
171 * and distance in mm (one way)
172 *
David Brazdil0f672f62019-12-10 10:32:29 +0000173 * because we limit to 6,45 meters the multiplication with 106 just
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000174 * fits into 32 bit
175 */
David Brazdil0f672f62019-12-10 10:32:29 +0000176 distance_mm = time_ns * 106 / 617176;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000177
178 return distance_mm;
179}
180
181static int srf04_read_raw(struct iio_dev *indio_dev,
182 struct iio_chan_spec const *channel, int *val,
183 int *val2, long info)
184{
185 struct srf04_data *data = iio_priv(indio_dev);
186 int ret;
187
188 if (channel->type != IIO_DISTANCE)
189 return -EINVAL;
190
191 switch (info) {
192 case IIO_CHAN_INFO_RAW:
193 ret = srf04_read(data);
194 if (ret < 0)
195 return ret;
196 *val = ret;
197 return IIO_VAL_INT;
198 case IIO_CHAN_INFO_SCALE:
199 /*
200 * theoretical maximum resolution is 3 mm
201 * 1 LSB is 1 mm
202 */
203 *val = 0;
204 *val2 = 1000;
205 return IIO_VAL_INT_PLUS_MICRO;
206 default:
207 return -EINVAL;
208 }
209}
210
211static const struct iio_info srf04_iio_info = {
212 .read_raw = srf04_read_raw,
213};
214
215static const struct iio_chan_spec srf04_chan_spec[] = {
216 {
217 .type = IIO_DISTANCE,
218 .info_mask_separate =
219 BIT(IIO_CHAN_INFO_RAW) |
220 BIT(IIO_CHAN_INFO_SCALE),
221 },
222};
223
David Brazdil0f672f62019-12-10 10:32:29 +0000224static const struct of_device_id of_srf04_match[] = {
225 { .compatible = "devantech,srf04", .data = &srf04_cfg},
226 { .compatible = "maxbotix,mb1000", .data = &mb_lv_cfg},
227 { .compatible = "maxbotix,mb1010", .data = &mb_lv_cfg},
228 { .compatible = "maxbotix,mb1020", .data = &mb_lv_cfg},
229 { .compatible = "maxbotix,mb1030", .data = &mb_lv_cfg},
230 { .compatible = "maxbotix,mb1040", .data = &mb_lv_cfg},
231 {},
232};
233
234MODULE_DEVICE_TABLE(of, of_srf04_match);
235
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000236static int srf04_probe(struct platform_device *pdev)
237{
238 struct device *dev = &pdev->dev;
239 struct srf04_data *data;
240 struct iio_dev *indio_dev;
241 int ret;
242
243 indio_dev = devm_iio_device_alloc(dev, sizeof(struct srf04_data));
244 if (!indio_dev) {
245 dev_err(dev, "failed to allocate IIO device\n");
246 return -ENOMEM;
247 }
248
249 data = iio_priv(indio_dev);
250 data->dev = dev;
David Brazdil0f672f62019-12-10 10:32:29 +0000251 data->cfg = of_match_device(of_srf04_match, dev)->data;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000252
253 mutex_init(&data->lock);
254 init_completion(&data->rising);
255 init_completion(&data->falling);
256
257 data->gpiod_trig = devm_gpiod_get(dev, "trig", GPIOD_OUT_LOW);
258 if (IS_ERR(data->gpiod_trig)) {
259 dev_err(dev, "failed to get trig-gpios: err=%ld\n",
260 PTR_ERR(data->gpiod_trig));
261 return PTR_ERR(data->gpiod_trig);
262 }
263
264 data->gpiod_echo = devm_gpiod_get(dev, "echo", GPIOD_IN);
265 if (IS_ERR(data->gpiod_echo)) {
266 dev_err(dev, "failed to get echo-gpios: err=%ld\n",
267 PTR_ERR(data->gpiod_echo));
268 return PTR_ERR(data->gpiod_echo);
269 }
270
271 if (gpiod_cansleep(data->gpiod_echo)) {
272 dev_err(data->dev, "cansleep-GPIOs not supported\n");
273 return -ENODEV;
274 }
275
276 data->irqnr = gpiod_to_irq(data->gpiod_echo);
277 if (data->irqnr < 0) {
278 dev_err(data->dev, "gpiod_to_irq: %d\n", data->irqnr);
279 return data->irqnr;
280 }
281
282 ret = devm_request_irq(dev, data->irqnr, srf04_handle_irq,
283 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
284 pdev->name, indio_dev);
285 if (ret < 0) {
286 dev_err(data->dev, "request_irq: %d\n", ret);
287 return ret;
288 }
289
290 platform_set_drvdata(pdev, indio_dev);
291
292 indio_dev->name = "srf04";
293 indio_dev->dev.parent = &pdev->dev;
294 indio_dev->info = &srf04_iio_info;
295 indio_dev->modes = INDIO_DIRECT_MODE;
296 indio_dev->channels = srf04_chan_spec;
297 indio_dev->num_channels = ARRAY_SIZE(srf04_chan_spec);
298
299 return devm_iio_device_register(dev, indio_dev);
300}
301
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000302static struct platform_driver srf04_driver = {
303 .probe = srf04_probe,
304 .driver = {
305 .name = "srf04-gpio",
306 .of_match_table = of_srf04_match,
307 },
308};
309
310module_platform_driver(srf04_driver);
311
312MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
313MODULE_DESCRIPTION("SRF04 ultrasonic sensor for distance measuring using GPIOs");
314MODULE_LICENSE("GPL");
315MODULE_ALIAS("platform:srf04");