pps-gpio.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * pps-gpio.c -- PPS client driver using GPIO
  4. *
  5. * Copyright (C) 2010 Ricardo Martins <rasm@fe.up.pt>
  6. * Copyright (C) 2011 James Nuss <jamesnuss@nanometrics.ca>
  7. */
  8. #define PPS_GPIO_NAME "pps-gpio"
  9. #define pr_fmt(fmt) PPS_GPIO_NAME ": " fmt
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/slab.h>
  16. #include <linux/pps_kernel.h>
  17. #include <linux/pps-gpio.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <linux/list.h>
  20. #include <linux/of_device.h>
  21. #include <linux/of_gpio.h>
  22. #include <linux/timer.h>
  23. #include <linux/jiffies.h>
  24. /* Info for each registered platform device */
  25. struct pps_gpio_device_data {
  26. int irq; /* IRQ used as PPS source */
  27. struct pps_device *pps; /* PPS source device */
  28. struct pps_source_info info; /* PPS source information */
  29. struct gpio_desc *gpio_pin; /* GPIO port descriptors */
  30. struct gpio_desc *echo_pin;
  31. struct timer_list echo_timer; /* timer to reset echo active state */
  32. bool assert_falling_edge;
  33. bool capture_clear;
  34. unsigned int echo_active_ms; /* PPS echo active duration */
  35. unsigned long echo_timeout; /* timer timeout value in jiffies */
  36. };
  37. /*
  38. * Report the PPS event
  39. */
  40. static irqreturn_t pps_gpio_irq_handler(int irq, void *data)
  41. {
  42. const struct pps_gpio_device_data *info;
  43. struct pps_event_time ts;
  44. int rising_edge;
  45. /* Get the time stamp first */
  46. pps_get_ts(&ts);
  47. info = data;
  48. rising_edge = gpiod_get_value(info->gpio_pin);
  49. if ((rising_edge && !info->assert_falling_edge) ||
  50. (!rising_edge && info->assert_falling_edge))
  51. pps_event(info->pps, &ts, PPS_CAPTUREASSERT, data);
  52. else if (info->capture_clear &&
  53. ((rising_edge && info->assert_falling_edge) ||
  54. (!rising_edge && !info->assert_falling_edge)))
  55. pps_event(info->pps, &ts, PPS_CAPTURECLEAR, data);
  56. return IRQ_HANDLED;
  57. }
  58. /* This function will only be called when an ECHO GPIO is defined */
  59. static void pps_gpio_echo(struct pps_device *pps, int event, void *data)
  60. {
  61. /* add_timer() needs to write into info->echo_timer */
  62. struct pps_gpio_device_data *info = data;
  63. switch (event) {
  64. case PPS_CAPTUREASSERT:
  65. if (pps->params.mode & PPS_ECHOASSERT)
  66. gpiod_set_value(info->echo_pin, 1);
  67. break;
  68. case PPS_CAPTURECLEAR:
  69. if (pps->params.mode & PPS_ECHOCLEAR)
  70. gpiod_set_value(info->echo_pin, 1);
  71. break;
  72. }
  73. /* fire the timer */
  74. if (info->pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) {
  75. info->echo_timer.expires = jiffies + info->echo_timeout;
  76. add_timer(&info->echo_timer);
  77. }
  78. }
  79. /* Timer callback to reset the echo pin to the inactive state */
  80. static void pps_gpio_echo_timer_callback(struct timer_list *t)
  81. {
  82. const struct pps_gpio_device_data *info;
  83. info = from_timer(info, t, echo_timer);
  84. gpiod_set_value(info->echo_pin, 0);
  85. }
  86. static int pps_gpio_setup(struct platform_device *pdev)
  87. {
  88. struct pps_gpio_device_data *data = platform_get_drvdata(pdev);
  89. struct device_node *np = pdev->dev.of_node;
  90. int ret;
  91. u32 value;
  92. data->gpio_pin = devm_gpiod_get(&pdev->dev,
  93. NULL, /* request "gpios" */
  94. GPIOD_IN);
  95. if (IS_ERR(data->gpio_pin)) {
  96. dev_err(&pdev->dev,
  97. "failed to request PPS GPIO\n");
  98. return PTR_ERR(data->gpio_pin);
  99. }
  100. data->echo_pin = devm_gpiod_get_optional(&pdev->dev,
  101. "echo",
  102. GPIOD_OUT_LOW);
  103. if (data->echo_pin) {
  104. if (IS_ERR(data->echo_pin)) {
  105. dev_err(&pdev->dev, "failed to request ECHO GPIO\n");
  106. return PTR_ERR(data->echo_pin);
  107. }
  108. ret = of_property_read_u32(np,
  109. "echo-active-ms",
  110. &value);
  111. if (ret) {
  112. dev_err(&pdev->dev,
  113. "failed to get echo-active-ms from OF\n");
  114. return ret;
  115. }
  116. data->echo_active_ms = value;
  117. /* sanity check on echo_active_ms */
  118. if (!data->echo_active_ms || data->echo_active_ms > 999) {
  119. dev_err(&pdev->dev,
  120. "echo-active-ms: %u - bad value from OF\n",
  121. data->echo_active_ms);
  122. return -EINVAL;
  123. }
  124. }
  125. if (of_property_read_bool(np, "assert-falling-edge"))
  126. data->assert_falling_edge = true;
  127. return 0;
  128. }
  129. static unsigned long
  130. get_irqf_trigger_flags(const struct pps_gpio_device_data *data)
  131. {
  132. unsigned long flags = data->assert_falling_edge ?
  133. IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
  134. if (data->capture_clear) {
  135. flags |= ((flags & IRQF_TRIGGER_RISING) ?
  136. IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING);
  137. }
  138. return flags;
  139. }
  140. static int pps_gpio_probe(struct platform_device *pdev)
  141. {
  142. struct pps_gpio_device_data *data;
  143. int ret;
  144. int pps_default_params;
  145. const struct pps_gpio_platform_data *pdata = pdev->dev.platform_data;
  146. /* allocate space for device info */
  147. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  148. if (!data)
  149. return -ENOMEM;
  150. platform_set_drvdata(pdev, data);
  151. /* GPIO setup */
  152. if (pdata) {
  153. data->gpio_pin = pdata->gpio_pin;
  154. data->echo_pin = pdata->echo_pin;
  155. data->assert_falling_edge = pdata->assert_falling_edge;
  156. data->capture_clear = pdata->capture_clear;
  157. data->echo_active_ms = pdata->echo_active_ms;
  158. } else {
  159. ret = pps_gpio_setup(pdev);
  160. if (ret)
  161. return -EINVAL;
  162. }
  163. /* IRQ setup */
  164. ret = gpiod_to_irq(data->gpio_pin);
  165. if (ret < 0) {
  166. dev_err(&pdev->dev, "failed to map GPIO to IRQ: %d\n", ret);
  167. return -EINVAL;
  168. }
  169. data->irq = ret;
  170. /* initialize PPS specific parts of the bookkeeping data structure. */
  171. data->info.mode = PPS_CAPTUREASSERT | PPS_OFFSETASSERT |
  172. PPS_ECHOASSERT | PPS_CANWAIT | PPS_TSFMT_TSPEC;
  173. if (data->capture_clear)
  174. data->info.mode |= PPS_CAPTURECLEAR | PPS_OFFSETCLEAR |
  175. PPS_ECHOCLEAR;
  176. data->info.owner = THIS_MODULE;
  177. snprintf(data->info.name, PPS_MAX_NAME_LEN - 1, "%s.%d",
  178. pdev->name, pdev->id);
  179. if (data->echo_pin) {
  180. data->info.echo = pps_gpio_echo;
  181. data->echo_timeout = msecs_to_jiffies(data->echo_active_ms);
  182. timer_setup(&data->echo_timer, pps_gpio_echo_timer_callback, 0);
  183. }
  184. /* register PPS source */
  185. pps_default_params = PPS_CAPTUREASSERT | PPS_OFFSETASSERT;
  186. if (data->capture_clear)
  187. pps_default_params |= PPS_CAPTURECLEAR | PPS_OFFSETCLEAR;
  188. data->pps = pps_register_source(&data->info, pps_default_params);
  189. if (IS_ERR(data->pps)) {
  190. dev_err(&pdev->dev, "failed to register IRQ %d as PPS source\n",
  191. data->irq);
  192. return PTR_ERR(data->pps);
  193. }
  194. /* register IRQ interrupt handler */
  195. ret = devm_request_irq(&pdev->dev, data->irq, pps_gpio_irq_handler,
  196. get_irqf_trigger_flags(data), data->info.name, data);
  197. if (ret) {
  198. pps_unregister_source(data->pps);
  199. dev_err(&pdev->dev, "failed to acquire IRQ %d\n", data->irq);
  200. return -EINVAL;
  201. }
  202. dev_info(data->pps->dev, "Registered IRQ %d as PPS source\n",
  203. data->irq);
  204. return 0;
  205. }
  206. static int pps_gpio_remove(struct platform_device *pdev)
  207. {
  208. struct pps_gpio_device_data *data = platform_get_drvdata(pdev);
  209. pps_unregister_source(data->pps);
  210. if (data->echo_pin) {
  211. del_timer_sync(&data->echo_timer);
  212. /* reset echo pin in any case */
  213. gpiod_set_value(data->echo_pin, 0);
  214. }
  215. dev_info(&pdev->dev, "removed IRQ %d as PPS source\n", data->irq);
  216. return 0;
  217. }
  218. static const struct of_device_id pps_gpio_dt_ids[] = {
  219. { .compatible = "pps-gpio", },
  220. { /* sentinel */ }
  221. };
  222. MODULE_DEVICE_TABLE(of, pps_gpio_dt_ids);
  223. static struct platform_driver pps_gpio_driver = {
  224. .probe = pps_gpio_probe,
  225. .remove = pps_gpio_remove,
  226. .driver = {
  227. .name = PPS_GPIO_NAME,
  228. .of_match_table = pps_gpio_dt_ids,
  229. },
  230. };
  231. module_platform_driver(pps_gpio_driver);
  232. MODULE_AUTHOR("Ricardo Martins <rasm@fe.up.pt>");
  233. MODULE_AUTHOR("James Nuss <jamesnuss@nanometrics.ca>");
  234. MODULE_DESCRIPTION("Use GPIO pin as PPS source");
  235. MODULE_LICENSE("GPL");
  236. MODULE_VERSION("1.2.0");