Skip to content

Commit 9a75a7c

Browse files
pateldipen1984-nvthierryreding
authored andcommitted
hte: Add Tegra HTE test driver
The test driver uses IRQ and GPIO lines to timestamp using HTE subsystem. The patch also adds compilation support in Kconfig and Makefile. Signed-off-by: Dipen Patel <[email protected]> Signed-off-by: Thierry Reding <[email protected]>
1 parent ed94eb2 commit 9a75a7c

File tree

3 files changed

+247
-1
lines changed

3 files changed

+247
-1
lines changed

drivers/hte/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,11 @@ config HTE_TEGRA194
2323
systems-on-chip. The driver supports 352 LIC IRQs and 39 AON GPIOs
2424
lines for timestamping in realtime.
2525

26+
config HTE_TEGRA194_TEST
27+
tristate "NVIDIA Tegra194 HTE Test"
28+
depends on HTE_TEGRA194
29+
help
30+
The NVIDIA Tegra194 GTE test driver demonstrates how to use HTE
31+
framework to timestamp GPIO and LIC IRQ lines.
32+
2633
endif

drivers/hte/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
obj-$(CONFIG_HTE) += hte.o
22
obj-$(CONFIG_HTE_TEGRA194) += hte-tegra194.o
3-
3+
obj-$(CONFIG_HTE_TEGRA194_TEST) += hte-tegra194-test.o

drivers/hte/hte-tegra194-test.c

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

0 commit comments

Comments
 (0)