srf04.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 cannot take more than 20 ms */
  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. ret = wait_for_completion_killable_timeout(&data->falling, HZ/50);
  109. if (ret < 0) {
  110. mutex_unlock(&data->lock);
  111. return ret;
  112. } else if (ret == 0) {
  113. mutex_unlock(&data->lock);
  114. return -ETIMEDOUT;
  115. }
  116. ktime_dt = ktime_sub(data->ts_falling, data->ts_rising);
  117. mutex_unlock(&data->lock);
  118. dt_ns = ktime_to_ns(ktime_dt);
  119. /*
  120. * measuring more than 3 meters is beyond the capabilities of
  121. * the sensor
  122. * ==> filter out invalid results for not measuring echos of
  123. * another us sensor
  124. *
  125. * formula:
  126. * distance 3 m
  127. * time = ---------- = --------- = 9404389 ns
  128. * speed 319 m/s
  129. *
  130. * using a minimum speed at -20 °C of 319 m/s
  131. */
  132. if (dt_ns > 9404389)
  133. return -EIO;
  134. time_ns = dt_ns;
  135. /*
  136. * the speed as function of the temperature is approximately:
  137. *
  138. * speed = 331,5 + 0,6 * Temp
  139. * with Temp in °C
  140. * and speed in m/s
  141. *
  142. * use 343 m/s as ultrasonic speed at 20 °C here in absence of the
  143. * temperature
  144. *
  145. * therefore:
  146. * time 343
  147. * distance = ------ * -----
  148. * 10^6 2
  149. * with time in ns
  150. * and distance in mm (one way)
  151. *
  152. * because we limit to 3 meters the multiplication with 343 just
  153. * fits into 32 bit
  154. */
  155. distance_mm = time_ns * 343 / 2000000;
  156. return distance_mm;
  157. }
  158. static int srf04_read_raw(struct iio_dev *indio_dev,
  159. struct iio_chan_spec const *channel, int *val,
  160. int *val2, long info)
  161. {
  162. struct srf04_data *data = iio_priv(indio_dev);
  163. int ret;
  164. if (channel->type != IIO_DISTANCE)
  165. return -EINVAL;
  166. switch (info) {
  167. case IIO_CHAN_INFO_RAW:
  168. ret = srf04_read(data);
  169. if (ret < 0)
  170. return ret;
  171. *val = ret;
  172. return IIO_VAL_INT;
  173. case IIO_CHAN_INFO_SCALE:
  174. /*
  175. * theoretical maximum resolution is 3 mm
  176. * 1 LSB is 1 mm
  177. */
  178. *val = 0;
  179. *val2 = 1000;
  180. return IIO_VAL_INT_PLUS_MICRO;
  181. default:
  182. return -EINVAL;
  183. }
  184. }
  185. static const struct iio_info srf04_iio_info = {
  186. .read_raw = srf04_read_raw,
  187. };
  188. static const struct iio_chan_spec srf04_chan_spec[] = {
  189. {
  190. .type = IIO_DISTANCE,
  191. .info_mask_separate =
  192. BIT(IIO_CHAN_INFO_RAW) |
  193. BIT(IIO_CHAN_INFO_SCALE),
  194. },
  195. };
  196. static int srf04_probe(struct platform_device *pdev)
  197. {
  198. struct device *dev = &pdev->dev;
  199. struct srf04_data *data;
  200. struct iio_dev *indio_dev;
  201. int ret;
  202. indio_dev = devm_iio_device_alloc(dev, sizeof(struct srf04_data));
  203. if (!indio_dev) {
  204. dev_err(dev, "failed to allocate IIO device\n");
  205. return -ENOMEM;
  206. }
  207. data = iio_priv(indio_dev);
  208. data->dev = dev;
  209. mutex_init(&data->lock);
  210. init_completion(&data->rising);
  211. init_completion(&data->falling);
  212. data->gpiod_trig = devm_gpiod_get(dev, "trig", GPIOD_OUT_LOW);
  213. if (IS_ERR(data->gpiod_trig)) {
  214. dev_err(dev, "failed to get trig-gpios: err=%ld\n",
  215. PTR_ERR(data->gpiod_trig));
  216. return PTR_ERR(data->gpiod_trig);
  217. }
  218. data->gpiod_echo = devm_gpiod_get(dev, "echo", GPIOD_IN);
  219. if (IS_ERR(data->gpiod_echo)) {
  220. dev_err(dev, "failed to get echo-gpios: err=%ld\n",
  221. PTR_ERR(data->gpiod_echo));
  222. return PTR_ERR(data->gpiod_echo);
  223. }
  224. if (gpiod_cansleep(data->gpiod_echo)) {
  225. dev_err(data->dev, "cansleep-GPIOs not supported\n");
  226. return -ENODEV;
  227. }
  228. data->irqnr = gpiod_to_irq(data->gpiod_echo);
  229. if (data->irqnr < 0) {
  230. dev_err(data->dev, "gpiod_to_irq: %d\n", data->irqnr);
  231. return data->irqnr;
  232. }
  233. ret = devm_request_irq(dev, data->irqnr, srf04_handle_irq,
  234. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  235. pdev->name, indio_dev);
  236. if (ret < 0) {
  237. dev_err(data->dev, "request_irq: %d\n", ret);
  238. return ret;
  239. }
  240. platform_set_drvdata(pdev, indio_dev);
  241. indio_dev->name = "srf04";
  242. indio_dev->dev.parent = &pdev->dev;
  243. indio_dev->info = &srf04_iio_info;
  244. indio_dev->modes = INDIO_DIRECT_MODE;
  245. indio_dev->channels = srf04_chan_spec;
  246. indio_dev->num_channels = ARRAY_SIZE(srf04_chan_spec);
  247. return devm_iio_device_register(dev, indio_dev);
  248. }
  249. static const struct of_device_id of_srf04_match[] = {
  250. { .compatible = "devantech,srf04", },
  251. {},
  252. };
  253. MODULE_DEVICE_TABLE(of, of_srf04_match);
  254. static struct platform_driver srf04_driver = {
  255. .probe = srf04_probe,
  256. .driver = {
  257. .name = "srf04-gpio",
  258. .of_match_table = of_srf04_match,
  259. },
  260. };
  261. module_platform_driver(srf04_driver);
  262. MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
  263. MODULE_DESCRIPTION("SRF04 ultrasonic sensor for distance measuring using GPIOs");
  264. MODULE_LICENSE("GPL");
  265. MODULE_ALIAS("platform:srf04");