ping.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PING: ultrasonic sensor for distance measuring by using only one GPIOs
  4. *
  5. * Copyright (c) 2019 Andreas Klinger <[email protected]>
  6. *
  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
  10. *
  11. * the measurement cycle as timing diagram looks like:
  12. *
  13. * GPIO ___ ________________________
  14. * ping: __/ \____________/ \________________
  15. * ^ ^ ^ ^
  16. * |<->| interrupt interrupt
  17. * udelay(5) (ts_rising) (ts_falling)
  18. * |<---------------------->|
  19. * . pulse time measured .
  20. * . --> one round trip of ultra sonic waves
  21. * ultra . .
  22. * sonic _ _ _. .
  23. * burst: _________/ \_/ \_/ \_________________________________________
  24. * .
  25. * ultra .
  26. * sonic _ _ _.
  27. * echo: __________________________________/ \_/ \_/ \________________
  28. */
  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>
  41. struct ping_cfg {
  42. unsigned long trigger_pulse_us; /* length of trigger pulse */
  43. int laserping_error; /* support error code in */
  44. /* pulse width of laser */
  45. /* ping sensors */
  46. s64 timeout_ns; /* timeout in ns */
  47. };
  48. struct ping_data {
  49. struct device *dev;
  50. struct gpio_desc *gpiod_ping;
  51. struct mutex lock;
  52. int irqnr;
  53. ktime_t ts_rising;
  54. ktime_t ts_falling;
  55. struct completion rising;
  56. struct completion falling;
  57. const struct ping_cfg *cfg;
  58. };
  59. static const struct ping_cfg pa_ping_cfg = {
  60. .trigger_pulse_us = 5,
  61. .laserping_error = 0,
  62. .timeout_ns = 18500000, /* 3 meters */
  63. };
  64. static const struct ping_cfg pa_laser_ping_cfg = {
  65. .trigger_pulse_us = 5,
  66. .laserping_error = 1,
  67. .timeout_ns = 15500000, /* 2 meters plus error codes */
  68. };
  69. static irqreturn_t ping_handle_irq(int irq, void *dev_id)
  70. {
  71. struct iio_dev *indio_dev = dev_id;
  72. struct ping_data *data = iio_priv(indio_dev);
  73. ktime_t now = ktime_get();
  74. if (gpiod_get_value(data->gpiod_ping)) {
  75. data->ts_rising = now;
  76. complete(&data->rising);
  77. } else {
  78. data->ts_falling = now;
  79. complete(&data->falling);
  80. }
  81. return IRQ_HANDLED;
  82. }
  83. static int ping_read(struct iio_dev *indio_dev)
  84. {
  85. struct ping_data *data = iio_priv(indio_dev);
  86. int ret;
  87. ktime_t ktime_dt;
  88. s64 dt_ns;
  89. u32 time_ns, distance_mm;
  90. struct platform_device *pdev = to_platform_device(data->dev);
  91. /*
  92. * just one read-echo-cycle can take place at a time
  93. * ==> lock against concurrent reading calls
  94. */
  95. mutex_lock(&data->lock);
  96. reinit_completion(&data->rising);
  97. reinit_completion(&data->falling);
  98. gpiod_set_value(data->gpiod_ping, 1);
  99. udelay(data->cfg->trigger_pulse_us);
  100. gpiod_set_value(data->gpiod_ping, 0);
  101. ret = gpiod_direction_input(data->gpiod_ping);
  102. if (ret < 0) {
  103. mutex_unlock(&data->lock);
  104. return ret;
  105. }
  106. data->irqnr = gpiod_to_irq(data->gpiod_ping);
  107. if (data->irqnr < 0) {
  108. dev_err(data->dev, "gpiod_to_irq: %d\n", data->irqnr);
  109. mutex_unlock(&data->lock);
  110. return data->irqnr;
  111. }
  112. ret = request_irq(data->irqnr, ping_handle_irq,
  113. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  114. pdev->name, indio_dev);
  115. if (ret < 0) {
  116. dev_err(data->dev, "request_irq: %d\n", ret);
  117. mutex_unlock(&data->lock);
  118. return ret;
  119. }
  120. /* it should not take more than 20 ms until echo is rising */
  121. ret = wait_for_completion_killable_timeout(&data->rising, HZ/50);
  122. if (ret < 0)
  123. goto err_reset_direction;
  124. else if (ret == 0) {
  125. ret = -ETIMEDOUT;
  126. goto err_reset_direction;
  127. }
  128. /* it cannot take more than 50 ms until echo is falling */
  129. ret = wait_for_completion_killable_timeout(&data->falling, HZ/20);
  130. if (ret < 0)
  131. goto err_reset_direction;
  132. else if (ret == 0) {
  133. ret = -ETIMEDOUT;
  134. goto err_reset_direction;
  135. }
  136. ktime_dt = ktime_sub(data->ts_falling, data->ts_rising);
  137. free_irq(data->irqnr, indio_dev);
  138. ret = gpiod_direction_output(data->gpiod_ping, GPIOD_OUT_LOW);
  139. if (ret < 0) {
  140. mutex_unlock(&data->lock);
  141. return ret;
  142. }
  143. mutex_unlock(&data->lock);
  144. dt_ns = ktime_to_ns(ktime_dt);
  145. if (dt_ns > data->cfg->timeout_ns) {
  146. dev_dbg(data->dev, "distance out of range: dt=%lldns\n",
  147. dt_ns);
  148. return -EIO;
  149. }
  150. time_ns = dt_ns;
  151. /*
  152. * read error code of laser ping sensor and give users chance to
  153. * figure out error by using dynamic debugging
  154. */
  155. if (data->cfg->laserping_error) {
  156. if ((time_ns > 12500000) && (time_ns <= 13500000)) {
  157. dev_dbg(data->dev, "target too close or to far\n");
  158. return -EIO;
  159. }
  160. if ((time_ns > 13500000) && (time_ns <= 14500000)) {
  161. dev_dbg(data->dev, "internal sensor error\n");
  162. return -EIO;
  163. }
  164. if ((time_ns > 14500000) && (time_ns <= 15500000)) {
  165. dev_dbg(data->dev, "internal sensor timeout\n");
  166. return -EIO;
  167. }
  168. }
  169. /*
  170. * the speed as function of the temperature is approximately:
  171. *
  172. * speed = 331,5 + 0,6 * Temp
  173. * with Temp in °C
  174. * and speed in m/s
  175. *
  176. * use 343,5 m/s as ultrasonic speed at 20 °C here in absence of the
  177. * temperature
  178. *
  179. * therefore:
  180. * time 343,5 time * 232
  181. * distance = ------ * ------- = ------------
  182. * 10^6 2 1350800
  183. * with time in ns
  184. * and distance in mm (one way)
  185. *
  186. * because we limit to 3 meters the multiplication with 232 just
  187. * fits into 32 bit
  188. */
  189. distance_mm = time_ns * 232 / 1350800;
  190. return distance_mm;
  191. err_reset_direction:
  192. free_irq(data->irqnr, indio_dev);
  193. mutex_unlock(&data->lock);
  194. if (gpiod_direction_output(data->gpiod_ping, GPIOD_OUT_LOW))
  195. dev_dbg(data->dev, "error in gpiod_direction_output\n");
  196. return ret;
  197. }
  198. static int ping_read_raw(struct iio_dev *indio_dev,
  199. struct iio_chan_spec const *channel, int *val,
  200. int *val2, long info)
  201. {
  202. int ret;
  203. if (channel->type != IIO_DISTANCE)
  204. return -EINVAL;
  205. switch (info) {
  206. case IIO_CHAN_INFO_RAW:
  207. ret = ping_read(indio_dev);
  208. if (ret < 0)
  209. return ret;
  210. *val = ret;
  211. return IIO_VAL_INT;
  212. case IIO_CHAN_INFO_SCALE:
  213. /*
  214. * maximum resolution in datasheet is 1 mm
  215. * 1 LSB is 1 mm
  216. */
  217. *val = 0;
  218. *val2 = 1000;
  219. return IIO_VAL_INT_PLUS_MICRO;
  220. default:
  221. return -EINVAL;
  222. }
  223. }
  224. static const struct iio_info ping_iio_info = {
  225. .read_raw = ping_read_raw,
  226. };
  227. static const struct iio_chan_spec ping_chan_spec[] = {
  228. {
  229. .type = IIO_DISTANCE,
  230. .info_mask_separate =
  231. BIT(IIO_CHAN_INFO_RAW) |
  232. BIT(IIO_CHAN_INFO_SCALE),
  233. },
  234. };
  235. static const struct of_device_id of_ping_match[] = {
  236. { .compatible = "parallax,ping", .data = &pa_ping_cfg },
  237. { .compatible = "parallax,laserping", .data = &pa_laser_ping_cfg },
  238. {},
  239. };
  240. MODULE_DEVICE_TABLE(of, of_ping_match);
  241. static int ping_probe(struct platform_device *pdev)
  242. {
  243. struct device *dev = &pdev->dev;
  244. struct ping_data *data;
  245. struct iio_dev *indio_dev;
  246. indio_dev = devm_iio_device_alloc(dev, sizeof(struct ping_data));
  247. if (!indio_dev) {
  248. dev_err(dev, "failed to allocate IIO device\n");
  249. return -ENOMEM;
  250. }
  251. data = iio_priv(indio_dev);
  252. data->dev = dev;
  253. data->cfg = device_get_match_data(dev);
  254. mutex_init(&data->lock);
  255. init_completion(&data->rising);
  256. init_completion(&data->falling);
  257. data->gpiod_ping = devm_gpiod_get(dev, "ping", GPIOD_OUT_LOW);
  258. if (IS_ERR(data->gpiod_ping)) {
  259. dev_err(dev, "failed to get ping-gpios: err=%ld\n",
  260. PTR_ERR(data->gpiod_ping));
  261. return PTR_ERR(data->gpiod_ping);
  262. }
  263. if (gpiod_cansleep(data->gpiod_ping)) {
  264. dev_err(data->dev, "cansleep-GPIOs not supported\n");
  265. return -ENODEV;
  266. }
  267. platform_set_drvdata(pdev, indio_dev);
  268. indio_dev->name = "ping";
  269. indio_dev->info = &ping_iio_info;
  270. indio_dev->modes = INDIO_DIRECT_MODE;
  271. indio_dev->channels = ping_chan_spec;
  272. indio_dev->num_channels = ARRAY_SIZE(ping_chan_spec);
  273. return devm_iio_device_register(dev, indio_dev);
  274. }
  275. static struct platform_driver ping_driver = {
  276. .probe = ping_probe,
  277. .driver = {
  278. .name = "ping-gpio",
  279. .of_match_table = of_ping_match,
  280. },
  281. };
  282. module_platform_driver(ping_driver);
  283. MODULE_AUTHOR("Andreas Klinger <[email protected]>");
  284. MODULE_DESCRIPTION("PING sensors for distance measuring using one GPIOs");
  285. MODULE_LICENSE("GPL");
  286. MODULE_ALIAS("platform:ping");