pps-gpio.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 <[email protected]>
  6. * Copyright (C) 2011 James Nuss <[email protected]>
  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. rising_edge = gpiod_get_value(info->gpio_pin);
  48. if ((rising_edge && !info->assert_falling_edge) ||
  49. (!rising_edge && info->assert_falling_edge))
  50. pps_event(info->pps, &ts, PPS_CAPTUREASSERT, data);
  51. else if (info->capture_clear &&
  52. ((rising_edge && info->assert_falling_edge) ||
  53. (!rising_edge && !info->assert_falling_edge)))
  54. pps_event(info->pps, &ts, PPS_CAPTURECLEAR, data);
  55. return IRQ_HANDLED;
  56. }
  57. /* This function will only be called when an ECHO GPIO is defined */
  58. static void pps_gpio_echo(struct pps_device *pps, int event, void *data)
  59. {
  60. /* add_timer() needs to write into info->echo_timer */
  61. struct pps_gpio_device_data *info = data;
  62. switch (event) {
  63. case PPS_CAPTUREASSERT:
  64. if (pps->params.mode & PPS_ECHOASSERT)
  65. gpiod_set_value(info->echo_pin, 1);
  66. break;
  67. case PPS_CAPTURECLEAR:
  68. if (pps->params.mode & PPS_ECHOCLEAR)
  69. gpiod_set_value(info->echo_pin, 1);
  70. break;
  71. }
  72. /* fire the timer */
  73. if (info->pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) {
  74. info->echo_timer.expires = jiffies + info->echo_timeout;
  75. add_timer(&info->echo_timer);
  76. }
  77. }
  78. /* Timer callback to reset the echo pin to the inactive state */
  79. static void pps_gpio_echo_timer_callback(struct timer_list *t)
  80. {
  81. const struct pps_gpio_device_data *info;
  82. info = from_timer(info, t, echo_timer);
  83. gpiod_set_value(info->echo_pin, 0);
  84. }
  85. static int pps_gpio_setup(struct device *dev)
  86. {
  87. struct pps_gpio_device_data *data = dev_get_drvdata(dev);
  88. int ret;
  89. u32 value;
  90. data->gpio_pin = devm_gpiod_get(dev, NULL, GPIOD_IN);
  91. if (IS_ERR(data->gpio_pin))
  92. return dev_err_probe(dev, PTR_ERR(data->gpio_pin),
  93. "failed to request PPS GPIO\n");
  94. data->assert_falling_edge =
  95. device_property_read_bool(dev, "assert-falling-edge");
  96. data->echo_pin = devm_gpiod_get_optional(dev, "echo", GPIOD_OUT_LOW);
  97. if (IS_ERR(data->echo_pin))
  98. return dev_err_probe(dev, PTR_ERR(data->echo_pin),
  99. "failed to request ECHO GPIO\n");
  100. if (!data->echo_pin)
  101. return 0;
  102. ret = device_property_read_u32(dev, "echo-active-ms", &value);
  103. if (ret) {
  104. dev_err(dev, "failed to get echo-active-ms from FW\n");
  105. return ret;
  106. }
  107. /* sanity check on echo_active_ms */
  108. if (!value || value > 999) {
  109. dev_err(dev, "echo-active-ms: %u - bad value from FW\n", value);
  110. return -EINVAL;
  111. }
  112. data->echo_active_ms = value;
  113. return 0;
  114. }
  115. static unsigned long
  116. get_irqf_trigger_flags(const struct pps_gpio_device_data *data)
  117. {
  118. unsigned long flags = data->assert_falling_edge ?
  119. IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
  120. if (data->capture_clear) {
  121. flags |= ((flags & IRQF_TRIGGER_RISING) ?
  122. IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING);
  123. }
  124. return flags;
  125. }
  126. static int pps_gpio_probe(struct platform_device *pdev)
  127. {
  128. struct pps_gpio_device_data *data;
  129. struct device *dev = &pdev->dev;
  130. int ret;
  131. int pps_default_params;
  132. /* allocate space for device info */
  133. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  134. if (!data)
  135. return -ENOMEM;
  136. dev_set_drvdata(dev, data);
  137. /* GPIO setup */
  138. ret = pps_gpio_setup(dev);
  139. if (ret)
  140. return ret;
  141. /* IRQ setup */
  142. ret = gpiod_to_irq(data->gpio_pin);
  143. if (ret < 0) {
  144. dev_err(dev, "failed to map GPIO to IRQ: %d\n", ret);
  145. return -EINVAL;
  146. }
  147. data->irq = ret;
  148. /* initialize PPS specific parts of the bookkeeping data structure. */
  149. data->info.mode = PPS_CAPTUREASSERT | PPS_OFFSETASSERT |
  150. PPS_ECHOASSERT | PPS_CANWAIT | PPS_TSFMT_TSPEC;
  151. if (data->capture_clear)
  152. data->info.mode |= PPS_CAPTURECLEAR | PPS_OFFSETCLEAR |
  153. PPS_ECHOCLEAR;
  154. data->info.owner = THIS_MODULE;
  155. snprintf(data->info.name, PPS_MAX_NAME_LEN - 1, "%s.%d",
  156. pdev->name, pdev->id);
  157. if (data->echo_pin) {
  158. data->info.echo = pps_gpio_echo;
  159. data->echo_timeout = msecs_to_jiffies(data->echo_active_ms);
  160. timer_setup(&data->echo_timer, pps_gpio_echo_timer_callback, 0);
  161. }
  162. /* register PPS source */
  163. pps_default_params = PPS_CAPTUREASSERT | PPS_OFFSETASSERT;
  164. if (data->capture_clear)
  165. pps_default_params |= PPS_CAPTURECLEAR | PPS_OFFSETCLEAR;
  166. data->pps = pps_register_source(&data->info, pps_default_params);
  167. if (IS_ERR(data->pps)) {
  168. dev_err(dev, "failed to register IRQ %d as PPS source\n",
  169. data->irq);
  170. return PTR_ERR(data->pps);
  171. }
  172. /* register IRQ interrupt handler */
  173. ret = devm_request_irq(dev, data->irq, pps_gpio_irq_handler,
  174. get_irqf_trigger_flags(data), data->info.name, data);
  175. if (ret) {
  176. pps_unregister_source(data->pps);
  177. dev_err(dev, "failed to acquire IRQ %d\n", data->irq);
  178. return -EINVAL;
  179. }
  180. dev_info(data->pps->dev, "Registered IRQ %d as PPS source\n",
  181. data->irq);
  182. return 0;
  183. }
  184. static int pps_gpio_remove(struct platform_device *pdev)
  185. {
  186. struct pps_gpio_device_data *data = platform_get_drvdata(pdev);
  187. pps_unregister_source(data->pps);
  188. del_timer_sync(&data->echo_timer);
  189. /* reset echo pin in any case */
  190. gpiod_set_value(data->echo_pin, 0);
  191. dev_info(&pdev->dev, "removed IRQ %d as PPS source\n", data->irq);
  192. return 0;
  193. }
  194. static const struct of_device_id pps_gpio_dt_ids[] = {
  195. { .compatible = "pps-gpio", },
  196. { /* sentinel */ }
  197. };
  198. MODULE_DEVICE_TABLE(of, pps_gpio_dt_ids);
  199. static struct platform_driver pps_gpio_driver = {
  200. .probe = pps_gpio_probe,
  201. .remove = pps_gpio_remove,
  202. .driver = {
  203. .name = PPS_GPIO_NAME,
  204. .of_match_table = pps_gpio_dt_ids,
  205. },
  206. };
  207. module_platform_driver(pps_gpio_driver);
  208. MODULE_AUTHOR("Ricardo Martins <[email protected]>");
  209. MODULE_AUTHOR("James Nuss <[email protected]>");
  210. MODULE_DESCRIPTION("Use GPIO pin as PPS source");
  211. MODULE_LICENSE("GPL");
  212. MODULE_VERSION("1.2.0");