ping.c 8.6 KB

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