srf04.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 <[email protected]>
  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_mark_last_busy(data->dev);
  110. pm_runtime_put_autosuspend(data->dev);
  111. }
  112. /* it should not take more than 20 ms until echo is rising */
  113. ret = wait_for_completion_killable_timeout(&data->rising, HZ/50);
  114. if (ret < 0) {
  115. mutex_unlock(&data->lock);
  116. return ret;
  117. } else if (ret == 0) {
  118. mutex_unlock(&data->lock);
  119. return -ETIMEDOUT;
  120. }
  121. /* it cannot take more than 50 ms until echo is falling */
  122. ret = wait_for_completion_killable_timeout(&data->falling, HZ/20);
  123. if (ret < 0) {
  124. mutex_unlock(&data->lock);
  125. return ret;
  126. } else if (ret == 0) {
  127. mutex_unlock(&data->lock);
  128. return -ETIMEDOUT;
  129. }
  130. ktime_dt = ktime_sub(data->ts_falling, data->ts_rising);
  131. mutex_unlock(&data->lock);
  132. dt_ns = ktime_to_ns(ktime_dt);
  133. /*
  134. * measuring more than 6,45 meters is beyond the capabilities of
  135. * the supported sensors
  136. * ==> filter out invalid results for not measuring echos of
  137. * another us sensor
  138. *
  139. * formula:
  140. * distance 6,45 * 2 m
  141. * time = ---------- = ------------ = 40438871 ns
  142. * speed 319 m/s
  143. *
  144. * using a minimum speed at -20 °C of 319 m/s
  145. */
  146. if (dt_ns > 40438871)
  147. return -EIO;
  148. time_ns = dt_ns;
  149. /*
  150. * the speed as function of the temperature is approximately:
  151. *
  152. * speed = 331,5 + 0,6 * Temp
  153. * with Temp in °C
  154. * and speed in m/s
  155. *
  156. * use 343,5 m/s as ultrasonic speed at 20 °C here in absence of the
  157. * temperature
  158. *
  159. * therefore:
  160. * time 343,5 time * 106
  161. * distance = ------ * ------- = ------------
  162. * 10^6 2 617176
  163. * with time in ns
  164. * and distance in mm (one way)
  165. *
  166. * because we limit to 6,45 meters the multiplication with 106 just
  167. * fits into 32 bit
  168. */
  169. distance_mm = time_ns * 106 / 617176;
  170. return distance_mm;
  171. }
  172. static int srf04_read_raw(struct iio_dev *indio_dev,
  173. struct iio_chan_spec const *channel, int *val,
  174. int *val2, long info)
  175. {
  176. struct srf04_data *data = iio_priv(indio_dev);
  177. int ret;
  178. if (channel->type != IIO_DISTANCE)
  179. return -EINVAL;
  180. switch (info) {
  181. case IIO_CHAN_INFO_RAW:
  182. ret = srf04_read(data);
  183. if (ret < 0)
  184. return ret;
  185. *val = ret;
  186. return IIO_VAL_INT;
  187. case IIO_CHAN_INFO_SCALE:
  188. /*
  189. * theoretical maximum resolution is 3 mm
  190. * 1 LSB is 1 mm
  191. */
  192. *val = 0;
  193. *val2 = 1000;
  194. return IIO_VAL_INT_PLUS_MICRO;
  195. default:
  196. return -EINVAL;
  197. }
  198. }
  199. static const struct iio_info srf04_iio_info = {
  200. .read_raw = srf04_read_raw,
  201. };
  202. static const struct iio_chan_spec srf04_chan_spec[] = {
  203. {
  204. .type = IIO_DISTANCE,
  205. .info_mask_separate =
  206. BIT(IIO_CHAN_INFO_RAW) |
  207. BIT(IIO_CHAN_INFO_SCALE),
  208. },
  209. };
  210. static const struct of_device_id of_srf04_match[] = {
  211. { .compatible = "devantech,srf04", .data = &srf04_cfg },
  212. { .compatible = "maxbotix,mb1000", .data = &mb_lv_cfg },
  213. { .compatible = "maxbotix,mb1010", .data = &mb_lv_cfg },
  214. { .compatible = "maxbotix,mb1020", .data = &mb_lv_cfg },
  215. { .compatible = "maxbotix,mb1030", .data = &mb_lv_cfg },
  216. { .compatible = "maxbotix,mb1040", .data = &mb_lv_cfg },
  217. {},
  218. };
  219. MODULE_DEVICE_TABLE(of, of_srf04_match);
  220. static int srf04_probe(struct platform_device *pdev)
  221. {
  222. struct device *dev = &pdev->dev;
  223. struct srf04_data *data;
  224. struct iio_dev *indio_dev;
  225. int ret;
  226. indio_dev = devm_iio_device_alloc(dev, sizeof(struct srf04_data));
  227. if (!indio_dev) {
  228. dev_err(dev, "failed to allocate IIO device\n");
  229. return -ENOMEM;
  230. }
  231. data = iio_priv(indio_dev);
  232. data->dev = dev;
  233. data->cfg = device_get_match_data(dev);
  234. mutex_init(&data->lock);
  235. init_completion(&data->rising);
  236. init_completion(&data->falling);
  237. data->gpiod_trig = devm_gpiod_get(dev, "trig", GPIOD_OUT_LOW);
  238. if (IS_ERR(data->gpiod_trig)) {
  239. dev_err(dev, "failed to get trig-gpios: err=%ld\n",
  240. PTR_ERR(data->gpiod_trig));
  241. return PTR_ERR(data->gpiod_trig);
  242. }
  243. data->gpiod_echo = devm_gpiod_get(dev, "echo", GPIOD_IN);
  244. if (IS_ERR(data->gpiod_echo)) {
  245. dev_err(dev, "failed to get echo-gpios: err=%ld\n",
  246. PTR_ERR(data->gpiod_echo));
  247. return PTR_ERR(data->gpiod_echo);
  248. }
  249. data->gpiod_power = devm_gpiod_get_optional(dev, "power",
  250. GPIOD_OUT_LOW);
  251. if (IS_ERR(data->gpiod_power)) {
  252. dev_err(dev, "failed to get power-gpios: err=%ld\n",
  253. PTR_ERR(data->gpiod_power));
  254. return PTR_ERR(data->gpiod_power);
  255. }
  256. if (data->gpiod_power) {
  257. data->startup_time_ms = 100;
  258. device_property_read_u32(dev, "startup-time-ms", &data->startup_time_ms);
  259. dev_dbg(dev, "using power gpio: startup-time-ms=%d\n",
  260. data->startup_time_ms);
  261. }
  262. if (gpiod_cansleep(data->gpiod_echo)) {
  263. dev_err(data->dev, "cansleep-GPIOs not supported\n");
  264. return -ENODEV;
  265. }
  266. data->irqnr = gpiod_to_irq(data->gpiod_echo);
  267. if (data->irqnr < 0) {
  268. dev_err(data->dev, "gpiod_to_irq: %d\n", data->irqnr);
  269. return data->irqnr;
  270. }
  271. ret = devm_request_irq(dev, data->irqnr, srf04_handle_irq,
  272. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  273. pdev->name, indio_dev);
  274. if (ret < 0) {
  275. dev_err(data->dev, "request_irq: %d\n", ret);
  276. return ret;
  277. }
  278. platform_set_drvdata(pdev, indio_dev);
  279. indio_dev->name = "srf04";
  280. indio_dev->info = &srf04_iio_info;
  281. indio_dev->modes = INDIO_DIRECT_MODE;
  282. indio_dev->channels = srf04_chan_spec;
  283. indio_dev->num_channels = ARRAY_SIZE(srf04_chan_spec);
  284. ret = iio_device_register(indio_dev);
  285. if (ret < 0) {
  286. dev_err(data->dev, "iio_device_register: %d\n", ret);
  287. return ret;
  288. }
  289. if (data->gpiod_power) {
  290. pm_runtime_set_autosuspend_delay(data->dev, 1000);
  291. pm_runtime_use_autosuspend(data->dev);
  292. ret = pm_runtime_set_active(data->dev);
  293. if (ret) {
  294. dev_err(data->dev, "pm_runtime_set_active: %d\n", ret);
  295. iio_device_unregister(indio_dev);
  296. }
  297. pm_runtime_enable(data->dev);
  298. pm_runtime_idle(data->dev);
  299. }
  300. return ret;
  301. }
  302. static int srf04_remove(struct platform_device *pdev)
  303. {
  304. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  305. struct srf04_data *data = iio_priv(indio_dev);
  306. iio_device_unregister(indio_dev);
  307. if (data->gpiod_power) {
  308. pm_runtime_disable(data->dev);
  309. pm_runtime_set_suspended(data->dev);
  310. }
  311. return 0;
  312. }
  313. static int srf04_pm_runtime_suspend(struct device *dev)
  314. {
  315. struct platform_device *pdev = container_of(dev,
  316. struct platform_device, dev);
  317. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  318. struct srf04_data *data = iio_priv(indio_dev);
  319. gpiod_set_value(data->gpiod_power, 0);
  320. return 0;
  321. }
  322. static int srf04_pm_runtime_resume(struct device *dev)
  323. {
  324. struct platform_device *pdev = container_of(dev,
  325. struct platform_device, dev);
  326. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  327. struct srf04_data *data = iio_priv(indio_dev);
  328. gpiod_set_value(data->gpiod_power, 1);
  329. msleep(data->startup_time_ms);
  330. return 0;
  331. }
  332. static const struct dev_pm_ops srf04_pm_ops = {
  333. RUNTIME_PM_OPS(srf04_pm_runtime_suspend,
  334. srf04_pm_runtime_resume, NULL)
  335. };
  336. static struct platform_driver srf04_driver = {
  337. .probe = srf04_probe,
  338. .remove = srf04_remove,
  339. .driver = {
  340. .name = "srf04-gpio",
  341. .of_match_table = of_srf04_match,
  342. .pm = pm_ptr(&srf04_pm_ops),
  343. },
  344. };
  345. module_platform_driver(srf04_driver);
  346. MODULE_AUTHOR("Andreas Klinger <[email protected]>");
  347. MODULE_DESCRIPTION("SRF04 ultrasonic sensor for distance measuring using GPIOs");
  348. MODULE_LICENSE("GPL");
  349. MODULE_ALIAS("platform:srf04");