srf04.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * SRF04: ultrasonic sensor for distance measuring by using GPIOs
  3. *
  4. * Copyright (c) 2017 Andreas Klinger <ak@it-klinger.de>
  5. *
  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.
  10. *
  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.
  15. *
  16. * For details about the device see:
  17. * http://www.robot-electronics.co.uk/htm/srf04tech.htm
  18. *
  19. * the measurement cycle as timing diagram looks like:
  20. *
  21. * +---+
  22. * GPIO | |
  23. * trig: --+ +------------------------------------------------------
  24. * ^ ^
  25. * |<->|
  26. * udelay(10)
  27. *
  28. * ultra +-+ +-+ +-+
  29. * sonic | | | | | |
  30. * burst: ---------+ +-+ +-+ +-----------------------------------------
  31. * .
  32. * ultra . +-+ +-+ +-+
  33. * sonic . | | | | | |
  34. * echo: ----------------------------------+ +-+ +-+ +----------------
  35. * . .
  36. * +------------------------+
  37. * GPIO | |
  38. * echo: -------------------+ +---------------
  39. * ^ ^
  40. * interrupt interrupt
  41. * (ts_rising) (ts_falling)
  42. * |<---------------------->|
  43. * pulse time measured
  44. * --> one round trip of ultra sonic waves
  45. */
  46. #include <linux/err.h>
  47. #include <linux/gpio/consumer.h>
  48. #include <linux/kernel.h>
  49. #include <linux/module.h>
  50. #include <linux/of.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>
  58. struct srf04_data {
  59. struct device *dev;
  60. struct gpio_desc *gpiod_trig;
  61. struct gpio_desc *gpiod_echo;
  62. struct mutex lock;
  63. int irqnr;
  64. ktime_t ts_rising;
  65. ktime_t ts_falling;
  66. struct completion rising;
  67. struct completion falling;
  68. };
  69. static irqreturn_t srf04_handle_irq(int irq, void *dev_id)
  70. {
  71. struct iio_dev *indio_dev = dev_id;
  72. struct srf04_data *data = iio_priv(indio_dev);
  73. ktime_t now = ktime_get();
  74. if (gpiod_get_value(data->gpiod_echo)) {
  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 srf04_read(struct srf04_data *data)
  84. {
  85. int ret;
  86. ktime_t ktime_dt;
  87. u64 dt_ns;
  88. u32 time_ns, distance_mm;
  89. /*
  90. * just one read-echo-cycle can take place at a time
  91. * ==> lock against concurrent reading calls
  92. */
  93. mutex_lock(&data->lock);
  94. reinit_completion(&data->rising);
  95. reinit_completion(&data->falling);
  96. gpiod_set_value(data->gpiod_trig, 1);
  97. udelay(10);
  98. gpiod_set_value(data->gpiod_trig, 0);
  99. /* it should not take more than 20 ms until echo is rising */
  100. ret = wait_for_completion_killable_timeout(&data->rising, HZ/50);
  101. if (ret < 0) {
  102. mutex_unlock(&data->lock);
  103. return ret;
  104. } else if (ret == 0) {
  105. mutex_unlock(&data->lock);
  106. return -ETIMEDOUT;
  107. }
  108. /* it cannot take more than 50 ms until echo is falling */
  109. ret = wait_for_completion_killable_timeout(&data->falling, HZ/20);
  110. if (ret < 0) {
  111. mutex_unlock(&data->lock);
  112. return ret;
  113. } else if (ret == 0) {
  114. mutex_unlock(&data->lock);
  115. return -ETIMEDOUT;
  116. }
  117. ktime_dt = ktime_sub(data->ts_falling, data->ts_rising);
  118. mutex_unlock(&data->lock);
  119. dt_ns = ktime_to_ns(ktime_dt);
  120. /*
  121. * measuring more than 6,45 meters is beyond the capabilities of
  122. * the supported sensors
  123. * ==> filter out invalid results for not measuring echos of
  124. * another us sensor
  125. *
  126. * formula:
  127. * distance 6,45 * 2 m
  128. * time = ---------- = ------------ = 40438871 ns
  129. * speed 319 m/s
  130. *
  131. * using a minimum speed at -20 °C of 319 m/s
  132. */
  133. if (dt_ns > 40438871)
  134. return -EIO;
  135. time_ns = dt_ns;
  136. /*
  137. * the speed as function of the temperature is approximately:
  138. *
  139. * speed = 331,5 + 0,6 * Temp
  140. * with Temp in °C
  141. * and speed in m/s
  142. *
  143. * use 343,5 m/s as ultrasonic speed at 20 °C here in absence of the
  144. * temperature
  145. *
  146. * therefore:
  147. * time 343,5 time * 106
  148. * distance = ------ * ------- = ------------
  149. * 10^6 2 617176
  150. * with time in ns
  151. * and distance in mm (one way)
  152. *
  153. * because we limit to 6,45 meters the multiplication with 106 just
  154. * fits into 32 bit
  155. */
  156. distance_mm = time_ns * 106 / 617176;
  157. return distance_mm;
  158. }
  159. static int srf04_read_raw(struct iio_dev *indio_dev,
  160. struct iio_chan_spec const *channel, int *val,
  161. int *val2, long info)
  162. {
  163. struct srf04_data *data = iio_priv(indio_dev);
  164. int ret;
  165. if (channel->type != IIO_DISTANCE)
  166. return -EINVAL;
  167. switch (info) {
  168. case IIO_CHAN_INFO_RAW:
  169. ret = srf04_read(data);
  170. if (ret < 0)
  171. return ret;
  172. *val = ret;
  173. return IIO_VAL_INT;
  174. case IIO_CHAN_INFO_SCALE:
  175. /*
  176. * theoretical maximum resolution is 3 mm
  177. * 1 LSB is 1 mm
  178. */
  179. *val = 0;
  180. *val2 = 1000;
  181. return IIO_VAL_INT_PLUS_MICRO;
  182. default:
  183. return -EINVAL;
  184. }
  185. }
  186. static const struct iio_info srf04_iio_info = {
  187. .read_raw = srf04_read_raw,
  188. };
  189. static const struct iio_chan_spec srf04_chan_spec[] = {
  190. {
  191. .type = IIO_DISTANCE,
  192. .info_mask_separate =
  193. BIT(IIO_CHAN_INFO_RAW) |
  194. BIT(IIO_CHAN_INFO_SCALE),
  195. },
  196. };
  197. static int srf04_probe(struct platform_device *pdev)
  198. {
  199. struct device *dev = &pdev->dev;
  200. struct srf04_data *data;
  201. struct iio_dev *indio_dev;
  202. int ret;
  203. indio_dev = devm_iio_device_alloc(dev, sizeof(struct srf04_data));
  204. if (!indio_dev) {
  205. dev_err(dev, "failed to allocate IIO device\n");
  206. return -ENOMEM;
  207. }
  208. data = iio_priv(indio_dev);
  209. data->dev = dev;
  210. mutex_init(&data->lock);
  211. init_completion(&data->rising);
  212. init_completion(&data->falling);
  213. data->gpiod_trig = devm_gpiod_get(dev, "trig", GPIOD_OUT_LOW);
  214. if (IS_ERR(data->gpiod_trig)) {
  215. dev_err(dev, "failed to get trig-gpios: err=%ld\n",
  216. PTR_ERR(data->gpiod_trig));
  217. return PTR_ERR(data->gpiod_trig);
  218. }
  219. data->gpiod_echo = devm_gpiod_get(dev, "echo", GPIOD_IN);
  220. if (IS_ERR(data->gpiod_echo)) {
  221. dev_err(dev, "failed to get echo-gpios: err=%ld\n",
  222. PTR_ERR(data->gpiod_echo));
  223. return PTR_ERR(data->gpiod_echo);
  224. }
  225. if (gpiod_cansleep(data->gpiod_echo)) {
  226. dev_err(data->dev, "cansleep-GPIOs not supported\n");
  227. return -ENODEV;
  228. }
  229. data->irqnr = gpiod_to_irq(data->gpiod_echo);
  230. if (data->irqnr < 0) {
  231. dev_err(data->dev, "gpiod_to_irq: %d\n", data->irqnr);
  232. return data->irqnr;
  233. }
  234. ret = devm_request_irq(dev, data->irqnr, srf04_handle_irq,
  235. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  236. pdev->name, indio_dev);
  237. if (ret < 0) {
  238. dev_err(data->dev, "request_irq: %d\n", ret);
  239. return ret;
  240. }
  241. platform_set_drvdata(pdev, indio_dev);
  242. indio_dev->name = "srf04";
  243. indio_dev->dev.parent = &pdev->dev;
  244. indio_dev->info = &srf04_iio_info;
  245. indio_dev->modes = INDIO_DIRECT_MODE;
  246. indio_dev->channels = srf04_chan_spec;
  247. indio_dev->num_channels = ARRAY_SIZE(srf04_chan_spec);
  248. return devm_iio_device_register(dev, indio_dev);
  249. }
  250. static const struct of_device_id of_srf04_match[] = {
  251. { .compatible = "devantech,srf04", },
  252. {},
  253. };
  254. MODULE_DEVICE_TABLE(of, of_srf04_match);
  255. static struct platform_driver srf04_driver = {
  256. .probe = srf04_probe,
  257. .driver = {
  258. .name = "srf04-gpio",
  259. .of_match_table = of_srf04_match,
  260. },
  261. };
  262. module_platform_driver(srf04_driver);
  263. MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
  264. MODULE_DESCRIPTION("SRF04 ultrasonic sensor for distance measuring using GPIOs");
  265. MODULE_LICENSE("GPL");
  266. MODULE_ALIAS("platform:srf04");