hte-tegra194-test.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2021-2022 NVIDIA Corporation
  4. *
  5. * Author: Dipen Patel <[email protected]>
  6. */
  7. #include <linux/err.h>
  8. #include <linux/mod_devicetable.h>
  9. #include <linux/module.h>
  10. #include <linux/moduleparam.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/gpio.h>
  13. #include <linux/timer.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/workqueue.h>
  16. #include <linux/hte.h>
  17. /*
  18. * This sample HTE GPIO test driver demonstrates HTE API usage by enabling
  19. * hardware timestamp on gpio_in and specified LIC IRQ lines.
  20. *
  21. * Note: gpio_out and gpio_in need to be shorted externally in order for this
  22. * test driver to work for the GPIO monitoring. The test driver has been
  23. * tested on Jetson AGX Xavier platform by shorting pin 32 and 16 on 40 pin
  24. * header.
  25. *
  26. * Device tree snippet to activate this driver:
  27. * tegra_hte_test {
  28. * compatible = "nvidia,tegra194-hte-test";
  29. * in-gpio = <&gpio_aon TEGRA194_AON_GPIO(BB, 1)>;
  30. * out-gpio = <&gpio_aon TEGRA194_AON_GPIO(BB, 0)>;
  31. * timestamps = <&tegra_hte_aon TEGRA194_AON_GPIO(BB, 1)>,
  32. * <&tegra_hte_lic 0x19>;
  33. * timestamp-names = "hte-gpio", "hte-i2c-irq";
  34. * status = "okay";
  35. * };
  36. *
  37. * How to run test driver:
  38. * - Load test driver.
  39. * - For the GPIO, at regular interval gpio_out pin toggles triggering
  40. * HTE for rising edge on gpio_in pin.
  41. *
  42. * - For the LIC IRQ line, it uses 0x19 interrupt which is i2c controller 1.
  43. * - Run i2cdetect -y 1 1>/dev/null, this command will generate i2c bus
  44. * transactions which creates timestamp data.
  45. * - It prints below message for both the lines.
  46. * HW timestamp(<line id>:<ts seq number>): <timestamp>, edge: <edge>.
  47. * - Unloading the driver disables and deallocate the HTE.
  48. */
  49. static struct tegra_hte_test {
  50. int gpio_in_irq;
  51. struct device *pdev;
  52. struct gpio_desc *gpio_in;
  53. struct gpio_desc *gpio_out;
  54. struct hte_ts_desc *desc;
  55. struct timer_list timer;
  56. struct kobject *kobj;
  57. } hte;
  58. static enum hte_return process_hw_ts(struct hte_ts_data *ts, void *p)
  59. {
  60. char *edge;
  61. struct hte_ts_desc *desc = p;
  62. if (!ts || !p)
  63. return HTE_CB_HANDLED;
  64. if (ts->raw_level < 0)
  65. edge = "Unknown";
  66. pr_info("HW timestamp(%u: %llu): %llu, edge: %s\n",
  67. desc->attr.line_id, ts->seq, ts->tsc,
  68. (ts->raw_level >= 0) ? ((ts->raw_level == 0) ?
  69. "falling" : "rising") : edge);
  70. return HTE_CB_HANDLED;
  71. }
  72. static void gpio_timer_cb(struct timer_list *t)
  73. {
  74. (void)t;
  75. gpiod_set_value(hte.gpio_out, !gpiod_get_value(hte.gpio_out));
  76. mod_timer(&hte.timer, jiffies + msecs_to_jiffies(8000));
  77. }
  78. static irqreturn_t tegra_hte_test_gpio_isr(int irq, void *data)
  79. {
  80. (void)irq;
  81. (void)data;
  82. return IRQ_HANDLED;
  83. }
  84. static const struct of_device_id tegra_hte_test_of_match[] = {
  85. { .compatible = "nvidia,tegra194-hte-test"},
  86. { }
  87. };
  88. MODULE_DEVICE_TABLE(of, tegra_hte_test_of_match);
  89. static int tegra_hte_test_probe(struct platform_device *pdev)
  90. {
  91. int ret = 0;
  92. int i, cnt;
  93. dev_set_drvdata(&pdev->dev, &hte);
  94. hte.pdev = &pdev->dev;
  95. hte.gpio_out = gpiod_get(&pdev->dev, "out", 0);
  96. if (IS_ERR(hte.gpio_out)) {
  97. dev_err(&pdev->dev, "failed to get gpio out\n");
  98. ret = -EINVAL;
  99. goto out;
  100. }
  101. hte.gpio_in = gpiod_get(&pdev->dev, "in", 0);
  102. if (IS_ERR(hte.gpio_in)) {
  103. dev_err(&pdev->dev, "failed to get gpio in\n");
  104. ret = -EINVAL;
  105. goto free_gpio_out;
  106. }
  107. ret = gpiod_direction_output(hte.gpio_out, 0);
  108. if (ret) {
  109. dev_err(&pdev->dev, "failed to set output\n");
  110. ret = -EINVAL;
  111. goto free_gpio_in;
  112. }
  113. ret = gpiod_direction_input(hte.gpio_in);
  114. if (ret) {
  115. dev_err(&pdev->dev, "failed to set input\n");
  116. ret = -EINVAL;
  117. goto free_gpio_in;
  118. }
  119. ret = gpiod_to_irq(hte.gpio_in);
  120. if (ret < 0) {
  121. dev_err(&pdev->dev, "failed to map GPIO to IRQ: %d\n", ret);
  122. ret = -ENXIO;
  123. goto free_gpio_in;
  124. }
  125. hte.gpio_in_irq = ret;
  126. ret = request_irq(ret, tegra_hte_test_gpio_isr,
  127. IRQF_TRIGGER_RISING,
  128. "tegra_hte_gpio_test_isr", &hte);
  129. if (ret) {
  130. dev_err(&pdev->dev, "failed to acquire IRQ\n");
  131. ret = -ENXIO;
  132. goto free_irq;
  133. }
  134. cnt = of_hte_req_count(hte.pdev);
  135. if (cnt < 0) {
  136. ret = cnt;
  137. goto free_irq;
  138. }
  139. dev_info(&pdev->dev, "Total requested lines:%d\n", cnt);
  140. hte.desc = devm_kzalloc(hte.pdev, sizeof(*hte.desc) * cnt, GFP_KERNEL);
  141. if (!hte.desc) {
  142. ret = -ENOMEM;
  143. goto free_irq;
  144. }
  145. for (i = 0; i < cnt; i++) {
  146. if (i == 0)
  147. /*
  148. * GPIO hte init, line_id and name will be parsed from
  149. * the device tree node. The edge_flag is implicitly
  150. * set by request_irq call. Only line_data is needed to be
  151. * set.
  152. */
  153. hte_init_line_attr(&hte.desc[i], 0, 0, NULL,
  154. hte.gpio_in);
  155. else
  156. /*
  157. * same comment as above except that IRQ does not need
  158. * line data.
  159. */
  160. hte_init_line_attr(&hte.desc[i], 0, 0, NULL, NULL);
  161. ret = hte_ts_get(hte.pdev, &hte.desc[i], i);
  162. if (ret)
  163. goto ts_put;
  164. ret = devm_hte_request_ts_ns(hte.pdev, &hte.desc[i],
  165. process_hw_ts, NULL,
  166. &hte.desc[i]);
  167. if (ret) /* no need to ts_put, request API takes care */
  168. goto free_irq;
  169. }
  170. timer_setup(&hte.timer, gpio_timer_cb, 0);
  171. mod_timer(&hte.timer, jiffies + msecs_to_jiffies(5000));
  172. return 0;
  173. ts_put:
  174. cnt = i;
  175. for (i = 0; i < cnt; i++)
  176. hte_ts_put(&hte.desc[i]);
  177. free_irq:
  178. free_irq(hte.gpio_in_irq, &hte);
  179. free_gpio_in:
  180. gpiod_put(hte.gpio_in);
  181. free_gpio_out:
  182. gpiod_put(hte.gpio_out);
  183. out:
  184. return ret;
  185. }
  186. static int tegra_hte_test_remove(struct platform_device *pdev)
  187. {
  188. (void)pdev;
  189. free_irq(hte.gpio_in_irq, &hte);
  190. gpiod_put(hte.gpio_in);
  191. gpiod_put(hte.gpio_out);
  192. del_timer_sync(&hte.timer);
  193. return 0;
  194. }
  195. static struct platform_driver tegra_hte_test_driver = {
  196. .probe = tegra_hte_test_probe,
  197. .remove = tegra_hte_test_remove,
  198. .driver = {
  199. .name = "tegra_hte_test",
  200. .of_match_table = tegra_hte_test_of_match,
  201. },
  202. };
  203. module_platform_driver(tegra_hte_test_driver);
  204. MODULE_AUTHOR("Dipen Patel <[email protected]>");
  205. MODULE_LICENSE("GPL");