1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2021-2022 NVIDIA Corporation
5 * Author: Dipen Patel <dipenp@nvidia.com>
9 #include <linux/gpio/consumer.h>
10 #include <linux/hte.h>
11 #include <linux/interrupt.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/timer.h>
16 #include <linux/workqueue.h>
19 * This sample HTE test driver demonstrates HTE API usage by enabling
20 * hardware timestamp on gpio_in and specified LIC IRQ lines.
22 * Note: gpio_out and gpio_in need to be shorted externally in order for this
23 * test driver to work for the GPIO monitoring. The test driver has been
24 * tested on Jetson AGX Xavier platform by shorting pin 32 and 16 on 40 pin
27 * Device tree snippet to activate this driver:
29 * compatible = "nvidia,tegra194-hte-test";
30 * in-gpio = <&gpio_aon TEGRA194_AON_GPIO(BB, 1)>;
31 * out-gpio = <&gpio_aon TEGRA194_AON_GPIO(BB, 0)>;
32 * timestamps = <&tegra_hte_aon TEGRA194_AON_GPIO(BB, 1)>,
33 * <&tegra_hte_lic 0x19>;
34 * timestamp-names = "hte-gpio", "hte-i2c-irq";
38 * How to run test driver:
40 * - For the GPIO, at regular interval gpio_out pin toggles triggering
41 * HTE for rising edge on gpio_in pin.
43 * - For the LIC IRQ line, it uses 0x19 interrupt which is i2c controller 1.
44 * - Run i2cdetect -y 1 1>/dev/null, this command will generate i2c bus
45 * transactions which creates timestamp data.
46 * - It prints below message for both the lines.
47 * HW timestamp(<line id>:<ts seq number>): <timestamp>, edge: <edge>.
48 * - Unloading the driver disables and deallocate the HTE.
51 static struct tegra_hte_test {
54 struct gpio_desc *gpio_in;
55 struct gpio_desc *gpio_out;
56 struct hte_ts_desc *desc;
57 struct timer_list timer;
61 static enum hte_return process_hw_ts(struct hte_ts_data *ts, void *p)
64 struct hte_ts_desc *desc = p;
67 return HTE_CB_HANDLED;
69 if (ts->raw_level < 0)
72 pr_info("HW timestamp(%u: %llu): %llu, edge: %s\n",
73 desc->attr.line_id, ts->seq, ts->tsc,
74 (ts->raw_level >= 0) ? ((ts->raw_level == 0) ?
75 "falling" : "rising") : edge);
77 return HTE_CB_HANDLED;
80 static void gpio_timer_cb(struct timer_list *t)
84 gpiod_set_value(hte.gpio_out, !gpiod_get_value(hte.gpio_out));
85 mod_timer(&hte.timer, jiffies + msecs_to_jiffies(8000));
88 static irqreturn_t tegra_hte_test_gpio_isr(int irq, void *data)
96 static const struct of_device_id tegra_hte_test_of_match[] = {
97 { .compatible = "nvidia,tegra194-hte-test"},
100 MODULE_DEVICE_TABLE(of, tegra_hte_test_of_match);
102 static int tegra_hte_test_probe(struct platform_device *pdev)
107 dev_set_drvdata(&pdev->dev, &hte);
108 hte.pdev = &pdev->dev;
110 hte.gpio_out = gpiod_get(&pdev->dev, "out", 0);
111 if (IS_ERR(hte.gpio_out)) {
112 dev_err(&pdev->dev, "failed to get gpio out\n");
117 hte.gpio_in = gpiod_get(&pdev->dev, "in", 0);
118 if (IS_ERR(hte.gpio_in)) {
119 dev_err(&pdev->dev, "failed to get gpio in\n");
124 ret = gpiod_direction_output(hte.gpio_out, 0);
126 dev_err(&pdev->dev, "failed to set output\n");
131 ret = gpiod_direction_input(hte.gpio_in);
133 dev_err(&pdev->dev, "failed to set input\n");
138 ret = gpiod_to_irq(hte.gpio_in);
140 dev_err(&pdev->dev, "failed to map GPIO to IRQ: %d\n", ret);
145 hte.gpio_in_irq = ret;
146 ret = request_irq(ret, tegra_hte_test_gpio_isr,
148 "tegra_hte_gpio_test_isr", &hte);
150 dev_err(&pdev->dev, "failed to acquire IRQ\n");
155 cnt = of_hte_req_count(hte.pdev);
159 dev_info(&pdev->dev, "Total requested lines:%d\n", cnt);
161 hte.desc = devm_kzalloc(hte.pdev, sizeof(*hte.desc) * cnt, GFP_KERNEL);
167 for (i = 0; i < cnt; i++) {
170 * GPIO hte init, line_id and name will be parsed from
171 * the device tree node. The edge_flag is implicitly
172 * set by request_irq call. Only line_data is needed to be
175 hte_init_line_attr(&hte.desc[i], 0, 0, NULL,
179 * same comment as above except that IRQ does not need
182 hte_init_line_attr(&hte.desc[i], 0, 0, NULL, NULL);
184 ret = hte_ts_get(hte.pdev, &hte.desc[i], i);
188 ret = devm_hte_request_ts_ns(hte.pdev, &hte.desc[i],
191 if (ret) /* no need to ts_put, request API takes care */
195 timer_setup(&hte.timer, gpio_timer_cb, 0);
196 mod_timer(&hte.timer, jiffies + msecs_to_jiffies(5000));
202 for (i = 0; i < cnt; i++)
203 hte_ts_put(&hte.desc[i]);
205 free_irq(hte.gpio_in_irq, &hte);
207 gpiod_put(hte.gpio_in);
209 gpiod_put(hte.gpio_out);
215 static int tegra_hte_test_remove(struct platform_device *pdev)
219 free_irq(hte.gpio_in_irq, &hte);
220 gpiod_put(hte.gpio_in);
221 gpiod_put(hte.gpio_out);
222 del_timer_sync(&hte.timer);
227 static struct platform_driver tegra_hte_test_driver = {
228 .probe = tegra_hte_test_probe,
229 .remove = tegra_hte_test_remove,
231 .name = "tegra_hte_test",
232 .of_match_table = tegra_hte_test_of_match,
235 module_platform_driver(tegra_hte_test_driver);
237 MODULE_AUTHOR("Dipen Patel <dipenp@nvidia.com>");
238 MODULE_LICENSE("GPL");