pps-gpio.c 7.0 KB

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