srf04.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * SRF04: ultrasonic sensor for distance measuring by using GPIOs
  4. *
  5. * Copyright (c) 2017 Andreas Klinger <ak@it-klinger.de>
  6. *
  7. * For details about the device see:
  8. * https://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. * |<->|
  17. * udelay(trigger_pulse_us)
  18. *
  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/mod_devicetable.h>
  41. #include <linux/module.h>
  42. #include <linux/platform_device.h>
  43. #include <linux/property.h>
  44. #include <linux/sched.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/delay.h>
  47. #include <linux/pm_runtime.h>
  48. #include <linux/iio/iio.h>
  49. #include <linux/iio/sysfs.h>
  50. struct srf04_cfg {
  51. unsigned long trigger_pulse_us;
  52. };
  53. struct srf04_data {
  54. struct device *dev;
  55. struct gpio_desc *gpiod_trig;
  56. struct gpio_desc *gpiod_echo;
  57. struct gpio_desc *gpiod_power;
  58. struct mutex lock;
  59. int irqnr;
  60. ktime_t ts_rising;
  61. ktime_t ts_falling;
  62. struct completion rising;
  63. struct completion falling;
  64. const struct srf04_cfg *cfg;
  65. int startup_time_ms;
  66. };
  67. static const struct srf04_cfg srf04_cfg = {
  68. .trigger_pulse_us = 10,
  69. };
  70. static const struct srf04_cfg mb_lv_cfg = {
  71. .trigger_pulse_us = 20,
  72. };
  73. static irqreturn_t srf04_handle_irq(int irq, void *dev_id)
  74. {
  75. struct iio_dev *indio_dev = dev_id;
  76. struct srf04_data *data = iio_priv(indio_dev);
  77. ktime_t now = ktime_get();
  78. if (gpiod_get_value(data->gpiod_echo)) {
  79. data->ts_rising = now;
  80. complete(&data->rising);
  81. } else {
  82. data->ts_falling = now;
  83. complete(&data->falling);
  84. }
  85. return IRQ_HANDLED;
  86. }
  87. static int srf04_read(struct srf04_data *data)
  88. {
  89. int ret;
  90. ktime_t ktime_dt;
  91. u64 dt_ns;
  92. u32 time_ns, distance_mm;
  93. if (data->gpiod_power) {
  94. ret = pm_runtime_resume_and_get(data->dev);
  95. if (ret < 0)
  96. return ret;
  97. }
  98. /*
  99. * just one read-echo-cycle can take place at a time
  100. * ==> lock against concurrent reading calls
  101. */
  102. mutex_lock(&data->lock);
  103. reinit_completion(&data->rising);
  104. reinit_completion(&data->falling);
  105. gpiod_set_value(data->gpiod_trig, 1);
  106. udelay(data->cfg->trigger_pulse_us);
  107. gpiod_set_value(data->gpiod_trig, 0);
  108. if (data->gpiod_power)
  109. pm_runtime_put_autosuspend(data->dev);
  110. /* it should not take more than 20 ms until echo is rising */
  111. ret = wait_for_completion_killable_timeout(&data->rising, HZ/50);
  112. if (ret < 0) {
  113. mutex_unlock(&data->lock);
  114. return ret;
  115. } else if (ret == 0) {
  116. mutex_unlock(&data->lock);
  117. return -ETIMEDOUT;
  118. }
  119. /* it cannot take more than 50 ms until echo is falling */
  120. ret = wait_for_completion_killable_timeout(&data->falling, HZ/20);
  121. if (ret < 0) {
  122. mutex_unlock(&data->lock);
  123. return ret;
  124. } else if (ret == 0) {
  125. mutex_unlock(&data->lock);
  126. return -ETIMEDOUT;
  127. }
  128. ktime_dt = ktime_sub(data->ts_falling, data->ts_rising);
  129. mutex_unlock(&data->lock);
  130. dt_ns = ktime_to_ns(ktime_dt);
  131. /*
  132. * measuring more than 6,45 meters is beyond the capabilities of
  133. * the supported sensors
  134. * ==> filter out invalid results for not measuring echos of
  135. * another us sensor
  136. *
  137. * formula:
  138. * distance 6,45 * 2 m
  139. * time = ---------- = ------------ = 40438871 ns
  140. * speed 319 m/s
  141. *
  142. * using a minimum speed at -20 °C of 319 m/s
  143. */
  144. if (dt_ns > 40438871)
  145. return -EIO;
  146. time_ns = dt_ns;
  147. /*
  148. * the speed as function of the temperature is approximately:
  149. *
  150. * speed = 331,5 + 0,6 * Temp
  151. * with Temp in °C
  152. * and speed in m/s
  153. *
  154. * use 343,5 m/s as ultrasonic speed at 20 °C here in absence of the
  155. * temperature
  156. *
  157. * therefore:
  158. * time 343,5 time * 106
  159. * distance = ------ * ------- = ------------
  160. * 10^6 2 617176
  161. * with time in ns
  162. * and distance in mm (one way)
  163. *
  164. * because we limit to 6,45 meters the multiplication with 106 just
  165. * fits into 32 bit
  166. */
  167. distance_mm = time_ns * 106 / 617176;
  168. return distance_mm;
  169. }
  170. static int srf04_read_raw(struct iio_dev *indio_dev,
  171. struct iio_chan_spec const *channel, int *val,
  172. int *val2, long info)
  173. {
  174. struct srf04_data *data = iio_priv(indio_dev);
  175. int ret;
  176. if (channel->type != IIO_DISTANCE)
  177. return -EINVAL;
  178. switch (info) {
  179. case IIO_CHAN_INFO_RAW:
  180. ret = srf04_read(data);
  181. if (ret < 0)
  182. return ret;
  183. *val = ret;
  184. return IIO_VAL_INT;
  185. case IIO_CHAN_INFO_SCALE:
  186. /*
  187. * theoretical maximum resolution is 3 mm
  188. * 1 LSB is 1 mm
  189. */
  190. *val = 0;
  191. *val2 = 1000;
  192. return IIO_VAL_INT_PLUS_MICRO;
  193. default:
  194. return -EINVAL;
  195. }
  196. }
  197. static const struct iio_info srf04_iio_info = {
  198. .read_raw = srf04_read_raw,
  199. };
  200. static const struct iio_chan_spec srf04_chan_spec[] = {
  201. {
  202. .type = IIO_DISTANCE,
  203. .info_mask_separate =
  204. BIT(IIO_CHAN_INFO_RAW) |
  205. BIT(IIO_CHAN_INFO_SCALE),
  206. },
  207. };
  208. static const struct of_device_id of_srf04_match[] = {
  209. { .compatible = "devantech,srf04", .data = &srf04_cfg },
  210. { .compatible = "maxbotix,mb1000", .data = &mb_lv_cfg },
  211. { .compatible = "maxbotix,mb1010", .data = &mb_lv_cfg },
  212. { .compatible = "maxbotix,mb1020", .data = &mb_lv_cfg },
  213. { .compatible = "maxbotix,mb1030", .data = &mb_lv_cfg },
  214. { .compatible = "maxbotix,mb1040", .data = &mb_lv_cfg },
  215. { }
  216. };
  217. MODULE_DEVICE_TABLE(of, of_srf04_match);
  218. static int srf04_probe(struct platform_device *pdev)
  219. {
  220. struct device *dev = &pdev->dev;
  221. struct srf04_data *data;
  222. struct iio_dev *indio_dev;
  223. int ret;
  224. indio_dev = devm_iio_device_alloc(dev, sizeof(struct srf04_data));
  225. if (!indio_dev)
  226. return -ENOMEM;
  227. data = iio_priv(indio_dev);
  228. data->dev = dev;
  229. data->cfg = device_get_match_data(dev);
  230. mutex_init(&data->lock);
  231. init_completion(&data->rising);
  232. init_completion(&data->falling);
  233. data->gpiod_trig = devm_gpiod_get(dev, "trig", GPIOD_OUT_LOW);
  234. if (IS_ERR(data->gpiod_trig)) {
  235. dev_err(dev, "failed to get trig-gpios: err=%ld\n",
  236. PTR_ERR(data->gpiod_trig));
  237. return PTR_ERR(data->gpiod_trig);
  238. }
  239. data->gpiod_echo = devm_gpiod_get(dev, "echo", GPIOD_IN);
  240. if (IS_ERR(data->gpiod_echo)) {
  241. dev_err(dev, "failed to get echo-gpios: err=%ld\n",
  242. PTR_ERR(data->gpiod_echo));
  243. return PTR_ERR(data->gpiod_echo);
  244. }
  245. data->gpiod_power = devm_gpiod_get_optional(dev, "power",
  246. GPIOD_OUT_LOW);
  247. if (IS_ERR(data->gpiod_power)) {
  248. dev_err(dev, "failed to get power-gpios: err=%ld\n",
  249. PTR_ERR(data->gpiod_power));
  250. return PTR_ERR(data->gpiod_power);
  251. }
  252. if (data->gpiod_power) {
  253. data->startup_time_ms = 100;
  254. device_property_read_u32(dev, "startup-time-ms", &data->startup_time_ms);
  255. dev_dbg(dev, "using power gpio: startup-time-ms=%d\n",
  256. data->startup_time_ms);
  257. }
  258. if (gpiod_cansleep(data->gpiod_echo)) {
  259. dev_err(data->dev, "cansleep-GPIOs not supported\n");
  260. return -ENODEV;
  261. }
  262. data->irqnr = gpiod_to_irq(data->gpiod_echo);
  263. if (data->irqnr < 0) {
  264. dev_err(data->dev, "gpiod_to_irq: %d\n", data->irqnr);
  265. return data->irqnr;
  266. }
  267. ret = devm_request_irq(dev, data->irqnr, srf04_handle_irq,
  268. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  269. pdev->name, indio_dev);
  270. if (ret < 0) {
  271. dev_err(data->dev, "request_irq: %d\n", ret);
  272. return ret;
  273. }
  274. platform_set_drvdata(pdev, indio_dev);
  275. indio_dev->name = "srf04";
  276. indio_dev->info = &srf04_iio_info;
  277. indio_dev->modes = INDIO_DIRECT_MODE;
  278. indio_dev->channels = srf04_chan_spec;
  279. indio_dev->num_channels = ARRAY_SIZE(srf04_chan_spec);
  280. ret = iio_device_register(indio_dev);
  281. if (ret < 0) {
  282. dev_err(data->dev, "iio_device_register: %d\n", ret);
  283. return ret;
  284. }
  285. if (data->gpiod_power) {
  286. pm_runtime_set_autosuspend_delay(data->dev, 1000);
  287. pm_runtime_use_autosuspend(data->dev);
  288. ret = pm_runtime_set_active(data->dev);
  289. if (ret) {
  290. dev_err(data->dev, "pm_runtime_set_active: %d\n", ret);
  291. iio_device_unregister(indio_dev);
  292. }
  293. pm_runtime_enable(data->dev);
  294. pm_runtime_idle(data->dev);
  295. }
  296. return ret;
  297. }
  298. static void srf04_remove(struct platform_device *pdev)
  299. {
  300. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  301. struct srf04_data *data = iio_priv(indio_dev);
  302. iio_device_unregister(indio_dev);
  303. if (data->gpiod_power) {
  304. pm_runtime_disable(data->dev);
  305. pm_runtime_set_suspended(data->dev);
  306. }
  307. }
  308. static int srf04_pm_runtime_suspend(struct device *dev)
  309. {
  310. struct platform_device *pdev = container_of(dev,
  311. struct platform_device, dev);
  312. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  313. struct srf04_data *data = iio_priv(indio_dev);
  314. gpiod_set_value(data->gpiod_power, 0);
  315. return 0;
  316. }
  317. static int srf04_pm_runtime_resume(struct device *dev)
  318. {
  319. struct platform_device *pdev = container_of(dev,
  320. struct platform_device, dev);
  321. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  322. struct srf04_data *data = iio_priv(indio_dev);
  323. gpiod_set_value(data->gpiod_power, 1);
  324. msleep(data->startup_time_ms);
  325. return 0;
  326. }
  327. static const struct dev_pm_ops srf04_pm_ops = {
  328. RUNTIME_PM_OPS(srf04_pm_runtime_suspend,
  329. srf04_pm_runtime_resume, NULL)
  330. };
  331. static struct platform_driver srf04_driver = {
  332. .probe = srf04_probe,
  333. .remove = srf04_remove,
  334. .driver = {
  335. .name = "srf04-gpio",
  336. .of_match_table = of_srf04_match,
  337. .pm = pm_ptr(&srf04_pm_ops),
  338. },
  339. };
  340. module_platform_driver(srf04_driver);
  341. MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
  342. MODULE_DESCRIPTION("SRF04 ultrasonic sensor for distance measuring using GPIOs");
  343. MODULE_LICENSE("GPL");
  344. MODULE_ALIAS("platform:srf04");