xref: /openbmc/linux/drivers/hte/hte-tegra194.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
109574ccaSDipen Patel // SPDX-License-Identifier: GPL-2.0
209574ccaSDipen Patel /*
309574ccaSDipen Patel  * Copyright (c) 2021-2022 NVIDIA Corporation
409574ccaSDipen Patel  *
509574ccaSDipen Patel  * Author: Dipen Patel <dipenp@nvidia.com>
609574ccaSDipen Patel  */
709574ccaSDipen Patel 
809574ccaSDipen Patel #include <linux/err.h>
909574ccaSDipen Patel #include <linux/io.h>
1009574ccaSDipen Patel #include <linux/module.h>
1109574ccaSDipen Patel #include <linux/slab.h>
1209574ccaSDipen Patel #include <linux/stat.h>
1309574ccaSDipen Patel #include <linux/interrupt.h>
1409574ccaSDipen Patel #include <linux/of.h>
1509574ccaSDipen Patel #include <linux/platform_device.h>
1609574ccaSDipen Patel #include <linux/hte.h>
1709574ccaSDipen Patel #include <linux/uaccess.h>
1809574ccaSDipen Patel #include <linux/gpio/driver.h>
1909574ccaSDipen Patel #include <linux/gpio/consumer.h>
2009574ccaSDipen Patel 
2109574ccaSDipen Patel #define HTE_SUSPEND	0
2209574ccaSDipen Patel 
2309574ccaSDipen Patel /* HTE source clock TSC is 31.25MHz */
2409574ccaSDipen Patel #define HTE_TS_CLK_RATE_HZ	31250000ULL
2509574ccaSDipen Patel #define HTE_CLK_RATE_NS		32
2609574ccaSDipen Patel #define HTE_TS_NS_SHIFT	__builtin_ctz(HTE_CLK_RATE_NS)
2709574ccaSDipen Patel 
2809574ccaSDipen Patel #define NV_AON_SLICE_INVALID	-1
2909574ccaSDipen Patel #define NV_LINES_IN_SLICE	32
3009574ccaSDipen Patel 
3109574ccaSDipen Patel /* AON HTE line map For slice 1 */
3209574ccaSDipen Patel #define NV_AON_HTE_SLICE1_IRQ_GPIO_28	12
3309574ccaSDipen Patel #define NV_AON_HTE_SLICE1_IRQ_GPIO_29	13
3409574ccaSDipen Patel 
3509574ccaSDipen Patel /* AON HTE line map For slice 2 */
3609574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_0	0
3709574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_1	1
3809574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_2	2
3909574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_3	3
4009574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_4	4
4109574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_5	5
4209574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_6	6
4309574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_7	7
4409574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_8	8
4509574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_9	9
4609574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_10	10
4709574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_11	11
4809574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_12	12
4909574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_13	13
5009574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_14	14
5109574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_15	15
5209574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_16	16
5309574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_17	17
5409574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_18	18
5509574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_19	19
5609574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_20	20
5709574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_21	21
5809574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_22	22
5909574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_23	23
6009574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_24	24
6109574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_25	25
6209574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_26	26
6309574ccaSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_27	27
64b003fb5cSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_28	28
65b003fb5cSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_29	29
66b003fb5cSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_30	30
67b003fb5cSDipen Patel #define NV_AON_HTE_SLICE2_IRQ_GPIO_31	31
6809574ccaSDipen Patel 
6909574ccaSDipen Patel #define HTE_TECTRL		0x0
7009574ccaSDipen Patel #define HTE_TETSCH		0x4
7109574ccaSDipen Patel #define HTE_TETSCL		0x8
7209574ccaSDipen Patel #define HTE_TESRC		0xC
7309574ccaSDipen Patel #define HTE_TECCV		0x10
7409574ccaSDipen Patel #define HTE_TEPCV		0x14
7509574ccaSDipen Patel #define HTE_TECMD		0x1C
7609574ccaSDipen Patel #define HTE_TESTATUS		0x20
7709574ccaSDipen Patel #define HTE_SLICE0_TETEN	0x40
7809574ccaSDipen Patel #define HTE_SLICE1_TETEN	0x60
7909574ccaSDipen Patel 
8009574ccaSDipen Patel #define HTE_SLICE_SIZE		(HTE_SLICE1_TETEN - HTE_SLICE0_TETEN)
8109574ccaSDipen Patel 
8209574ccaSDipen Patel #define HTE_TECTRL_ENABLE_ENABLE	0x1
8309574ccaSDipen Patel 
8409574ccaSDipen Patel #define HTE_TECTRL_OCCU_SHIFT		0x8
8509574ccaSDipen Patel #define HTE_TECTRL_INTR_SHIFT		0x1
8609574ccaSDipen Patel #define HTE_TECTRL_INTR_ENABLE		0x1
8709574ccaSDipen Patel 
8809574ccaSDipen Patel #define HTE_TESRC_SLICE_SHIFT		16
8909574ccaSDipen Patel #define HTE_TESRC_SLICE_DEFAULT_MASK	0xFF
9009574ccaSDipen Patel 
9109574ccaSDipen Patel #define HTE_TECMD_CMD_POP		0x1
9209574ccaSDipen Patel 
9309574ccaSDipen Patel #define HTE_TESTATUS_OCCUPANCY_SHIFT	8
9409574ccaSDipen Patel #define HTE_TESTATUS_OCCUPANCY_MASK	0xFF
9509574ccaSDipen Patel 
9609574ccaSDipen Patel enum tegra_hte_type {
9709574ccaSDipen Patel 	HTE_TEGRA_TYPE_GPIO = 1U << 0,
9809574ccaSDipen Patel 	HTE_TEGRA_TYPE_LIC = 1U << 1,
9909574ccaSDipen Patel };
10009574ccaSDipen Patel 
10109574ccaSDipen Patel struct hte_slices {
10209574ccaSDipen Patel 	u32 r_val;
10309574ccaSDipen Patel 	unsigned long flags;
10409574ccaSDipen Patel 	/* to prevent lines mapped to same slice updating its register */
10509574ccaSDipen Patel 	spinlock_t s_lock;
10609574ccaSDipen Patel };
10709574ccaSDipen Patel 
10809574ccaSDipen Patel struct tegra_hte_line_mapped {
10909574ccaSDipen Patel 	int slice;
11009574ccaSDipen Patel 	u32 bit_index;
11109574ccaSDipen Patel };
11209574ccaSDipen Patel 
11309574ccaSDipen Patel struct tegra_hte_line_data {
11409574ccaSDipen Patel 	unsigned long flags;
11509574ccaSDipen Patel 	void *data;
11609574ccaSDipen Patel };
11709574ccaSDipen Patel 
11809574ccaSDipen Patel struct tegra_hte_data {
11909574ccaSDipen Patel 	enum tegra_hte_type type;
1200ebc475fSDipen Patel 	u32 slices;
12109574ccaSDipen Patel 	u32 map_sz;
12209574ccaSDipen Patel 	u32 sec_map_sz;
12309574ccaSDipen Patel 	const struct tegra_hte_line_mapped *map;
12409574ccaSDipen Patel 	const struct tegra_hte_line_mapped *sec_map;
12509574ccaSDipen Patel };
12609574ccaSDipen Patel 
12709574ccaSDipen Patel struct tegra_hte_soc {
12809574ccaSDipen Patel 	int hte_irq;
12909574ccaSDipen Patel 	u32 itr_thrshld;
13009574ccaSDipen Patel 	u32 conf_rval;
13109574ccaSDipen Patel 	struct hte_slices *sl;
13209574ccaSDipen Patel 	const struct tegra_hte_data *prov_data;
13309574ccaSDipen Patel 	struct tegra_hte_line_data *line_data;
13409574ccaSDipen Patel 	struct hte_chip *chip;
13509574ccaSDipen Patel 	struct gpio_chip *c;
13609574ccaSDipen Patel 	void __iomem *regs;
13709574ccaSDipen Patel };
13809574ccaSDipen Patel 
13909574ccaSDipen Patel static const struct tegra_hte_line_mapped tegra194_aon_gpio_map[] = {
14009574ccaSDipen Patel 	/* gpio, slice, bit_index */
14109574ccaSDipen Patel 	/* AA port */
14209574ccaSDipen Patel 	[0]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_11},
14309574ccaSDipen Patel 	[1]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_10},
14409574ccaSDipen Patel 	[2]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_9},
14509574ccaSDipen Patel 	[3]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_8},
14609574ccaSDipen Patel 	[4]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_7},
14709574ccaSDipen Patel 	[5]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_6},
14809574ccaSDipen Patel 	[6]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_5},
14909574ccaSDipen Patel 	[7]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_4},
15009574ccaSDipen Patel 	/* BB port */
15109574ccaSDipen Patel 	[8]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_3},
15209574ccaSDipen Patel 	[9]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_2},
15309574ccaSDipen Patel 	[10] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_1},
15409574ccaSDipen Patel 	[11] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_0},
15509574ccaSDipen Patel 	/* CC port */
15609574ccaSDipen Patel 	[12] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_22},
15709574ccaSDipen Patel 	[13] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_21},
15809574ccaSDipen Patel 	[14] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_20},
15909574ccaSDipen Patel 	[15] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_19},
16009574ccaSDipen Patel 	[16] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_18},
16109574ccaSDipen Patel 	[17] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_17},
16209574ccaSDipen Patel 	[18] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_16},
16309574ccaSDipen Patel 	[19] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_15},
16409574ccaSDipen Patel 	/* DD port */
16509574ccaSDipen Patel 	[20] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_14},
16609574ccaSDipen Patel 	[21] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_13},
16709574ccaSDipen Patel 	[22] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_12},
16809574ccaSDipen Patel 	/* EE port */
16909574ccaSDipen Patel 	[23] = {1, NV_AON_HTE_SLICE1_IRQ_GPIO_29},
17009574ccaSDipen Patel 	[24] = {1, NV_AON_HTE_SLICE1_IRQ_GPIO_28},
17109574ccaSDipen Patel 	[25] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_27},
17209574ccaSDipen Patel 	[26] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_26},
17309574ccaSDipen Patel 	[27] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_25},
17409574ccaSDipen Patel 	[28] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_24},
17509574ccaSDipen Patel 	[29] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_23},
17609574ccaSDipen Patel };
17709574ccaSDipen Patel 
17809574ccaSDipen Patel static const struct tegra_hte_line_mapped tegra194_aon_gpio_sec_map[] = {
17909574ccaSDipen Patel 	/* gpio, slice, bit_index */
18009574ccaSDipen Patel 	/* AA port */
18109574ccaSDipen Patel 	[0]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_11},
18209574ccaSDipen Patel 	[1]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_10},
18309574ccaSDipen Patel 	[2]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_9},
18409574ccaSDipen Patel 	[3]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_8},
18509574ccaSDipen Patel 	[4]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_7},
18609574ccaSDipen Patel 	[5]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_6},
18709574ccaSDipen Patel 	[6]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_5},
18809574ccaSDipen Patel 	[7]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_4},
18909574ccaSDipen Patel 	/* BB port */
19009574ccaSDipen Patel 	[8]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_3},
19109574ccaSDipen Patel 	[9]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_2},
19209574ccaSDipen Patel 	[10] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_1},
19309574ccaSDipen Patel 	[11] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_0},
19409574ccaSDipen Patel 	[12]  = {NV_AON_SLICE_INVALID, 0},
19509574ccaSDipen Patel 	[13]  = {NV_AON_SLICE_INVALID, 0},
19609574ccaSDipen Patel 	[14] = {NV_AON_SLICE_INVALID, 0},
19709574ccaSDipen Patel 	[15] = {NV_AON_SLICE_INVALID, 0},
19809574ccaSDipen Patel 	/* CC port */
19909574ccaSDipen Patel 	[16] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_22},
20009574ccaSDipen Patel 	[17] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_21},
20109574ccaSDipen Patel 	[18] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_20},
20209574ccaSDipen Patel 	[19] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_19},
20309574ccaSDipen Patel 	[20] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_18},
20409574ccaSDipen Patel 	[21] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_17},
20509574ccaSDipen Patel 	[22] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_16},
20609574ccaSDipen Patel 	[23] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_15},
20709574ccaSDipen Patel 	/* DD port */
20809574ccaSDipen Patel 	[24] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_14},
20909574ccaSDipen Patel 	[25] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_13},
21009574ccaSDipen Patel 	[26] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_12},
21109574ccaSDipen Patel 	[27] = {NV_AON_SLICE_INVALID, 0},
21209574ccaSDipen Patel 	[28] = {NV_AON_SLICE_INVALID, 0},
21309574ccaSDipen Patel 	[29] = {NV_AON_SLICE_INVALID, 0},
21409574ccaSDipen Patel 	[30] = {NV_AON_SLICE_INVALID, 0},
21509574ccaSDipen Patel 	[31] = {NV_AON_SLICE_INVALID, 0},
21609574ccaSDipen Patel 	/* EE port */
21709574ccaSDipen Patel 	[32] = {1, NV_AON_HTE_SLICE1_IRQ_GPIO_29},
21809574ccaSDipen Patel 	[33] = {1, NV_AON_HTE_SLICE1_IRQ_GPIO_28},
21909574ccaSDipen Patel 	[34] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_27},
22009574ccaSDipen Patel 	[35] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_26},
22109574ccaSDipen Patel 	[36] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_25},
22209574ccaSDipen Patel 	[37] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_24},
22309574ccaSDipen Patel 	[38] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_23},
22409574ccaSDipen Patel 	[39] = {NV_AON_SLICE_INVALID, 0},
22509574ccaSDipen Patel };
22609574ccaSDipen Patel 
227b003fb5cSDipen Patel static const struct tegra_hte_line_mapped tegra234_aon_gpio_map[] = {
228b003fb5cSDipen Patel 	/* gpio, slice, bit_index */
229b003fb5cSDipen Patel 	/* AA port */
230b003fb5cSDipen Patel 	[0]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_11},
231b003fb5cSDipen Patel 	[1]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_10},
232b003fb5cSDipen Patel 	[2]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_9},
233b003fb5cSDipen Patel 	[3]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_8},
234b003fb5cSDipen Patel 	[4]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_7},
235b003fb5cSDipen Patel 	[5]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_6},
236b003fb5cSDipen Patel 	[6]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_5},
237b003fb5cSDipen Patel 	[7]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_4},
238b003fb5cSDipen Patel 	/* BB port */
239b003fb5cSDipen Patel 	[8]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_3},
240b003fb5cSDipen Patel 	[9]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_2},
241b003fb5cSDipen Patel 	[10] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_1},
242b003fb5cSDipen Patel 	[11] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_0},
243b003fb5cSDipen Patel 	/* CC port */
244b003fb5cSDipen Patel 	[12] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_22},
245b003fb5cSDipen Patel 	[13] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_21},
246b003fb5cSDipen Patel 	[14] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_20},
247b003fb5cSDipen Patel 	[15] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_19},
248b003fb5cSDipen Patel 	[16] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_18},
249b003fb5cSDipen Patel 	[17] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_17},
250b003fb5cSDipen Patel 	[18] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_16},
251b003fb5cSDipen Patel 	[19] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_15},
252b003fb5cSDipen Patel 	/* DD port */
253b003fb5cSDipen Patel 	[20] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_14},
254b003fb5cSDipen Patel 	[21] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_13},
255b003fb5cSDipen Patel 	[22] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_12},
256b003fb5cSDipen Patel 	/* EE port */
257b003fb5cSDipen Patel 	[23] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_31},
258b003fb5cSDipen Patel 	[24] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_30},
259b003fb5cSDipen Patel 	[25] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_29},
260b003fb5cSDipen Patel 	[26] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_28},
261b003fb5cSDipen Patel 	[27] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_27},
262b003fb5cSDipen Patel 	[28] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_26},
263b003fb5cSDipen Patel 	[29] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_25},
264b003fb5cSDipen Patel 	[30] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_24},
265b003fb5cSDipen Patel 	/* GG port */
266b003fb5cSDipen Patel 	[31] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_23},
267b003fb5cSDipen Patel };
268b003fb5cSDipen Patel 
269b003fb5cSDipen Patel static const struct tegra_hte_line_mapped tegra234_aon_gpio_sec_map[] = {
270b003fb5cSDipen Patel 	/* gpio, slice, bit_index */
271b003fb5cSDipen Patel 	/* AA port */
272b003fb5cSDipen Patel 	[0]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_11},
273b003fb5cSDipen Patel 	[1]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_10},
274b003fb5cSDipen Patel 	[2]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_9},
275b003fb5cSDipen Patel 	[3]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_8},
276b003fb5cSDipen Patel 	[4]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_7},
277b003fb5cSDipen Patel 	[5]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_6},
278b003fb5cSDipen Patel 	[6]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_5},
279b003fb5cSDipen Patel 	[7]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_4},
280b003fb5cSDipen Patel 	/* BB port */
281b003fb5cSDipen Patel 	[8]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_3},
282b003fb5cSDipen Patel 	[9]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_2},
283b003fb5cSDipen Patel 	[10] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_1},
284b003fb5cSDipen Patel 	[11] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_0},
285b003fb5cSDipen Patel 	[12] = {NV_AON_SLICE_INVALID, 0},
286b003fb5cSDipen Patel 	[13] = {NV_AON_SLICE_INVALID, 0},
287b003fb5cSDipen Patel 	[14] = {NV_AON_SLICE_INVALID, 0},
288b003fb5cSDipen Patel 	[15] = {NV_AON_SLICE_INVALID, 0},
289b003fb5cSDipen Patel 	/* CC port */
290b003fb5cSDipen Patel 	[16] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_22},
291b003fb5cSDipen Patel 	[17] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_21},
292b003fb5cSDipen Patel 	[18] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_20},
293b003fb5cSDipen Patel 	[19] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_19},
294b003fb5cSDipen Patel 	[20] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_18},
295b003fb5cSDipen Patel 	[21] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_17},
296b003fb5cSDipen Patel 	[22] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_16},
297b003fb5cSDipen Patel 	[23] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_15},
298b003fb5cSDipen Patel 	/* DD port */
299b003fb5cSDipen Patel 	[24] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_14},
300b003fb5cSDipen Patel 	[25] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_13},
301b003fb5cSDipen Patel 	[26] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_12},
302b003fb5cSDipen Patel 	[27] = {NV_AON_SLICE_INVALID, 0},
303b003fb5cSDipen Patel 	[28] = {NV_AON_SLICE_INVALID, 0},
304b003fb5cSDipen Patel 	[29] = {NV_AON_SLICE_INVALID, 0},
305b003fb5cSDipen Patel 	[30] = {NV_AON_SLICE_INVALID, 0},
306b003fb5cSDipen Patel 	[31] = {NV_AON_SLICE_INVALID, 0},
307b003fb5cSDipen Patel 	/* EE port */
308b003fb5cSDipen Patel 	[32] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_31},
309b003fb5cSDipen Patel 	[33] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_30},
310b003fb5cSDipen Patel 	[34] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_29},
311b003fb5cSDipen Patel 	[35] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_28},
312b003fb5cSDipen Patel 	[36] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_27},
313b003fb5cSDipen Patel 	[37] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_26},
314b003fb5cSDipen Patel 	[38] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_25},
315b003fb5cSDipen Patel 	[39] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_24},
316b003fb5cSDipen Patel 	/* GG port */
317b003fb5cSDipen Patel 	[40] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_23},
318b003fb5cSDipen Patel };
319b003fb5cSDipen Patel 
320b003fb5cSDipen Patel static const struct tegra_hte_data t194_aon_hte = {
32109574ccaSDipen Patel 	.map_sz = ARRAY_SIZE(tegra194_aon_gpio_map),
32209574ccaSDipen Patel 	.map = tegra194_aon_gpio_map,
32309574ccaSDipen Patel 	.sec_map_sz = ARRAY_SIZE(tegra194_aon_gpio_sec_map),
32409574ccaSDipen Patel 	.sec_map = tegra194_aon_gpio_sec_map,
32509574ccaSDipen Patel 	.type = HTE_TEGRA_TYPE_GPIO,
3260ebc475fSDipen Patel 	.slices = 3,
32709574ccaSDipen Patel };
32809574ccaSDipen Patel 
329b003fb5cSDipen Patel static const struct tegra_hte_data t234_aon_hte = {
330b003fb5cSDipen Patel 	.map_sz = ARRAY_SIZE(tegra234_aon_gpio_map),
331b003fb5cSDipen Patel 	.map = tegra234_aon_gpio_map,
332b003fb5cSDipen Patel 	.sec_map_sz = ARRAY_SIZE(tegra234_aon_gpio_sec_map),
333b003fb5cSDipen Patel 	.sec_map = tegra234_aon_gpio_sec_map,
334b003fb5cSDipen Patel 	.type = HTE_TEGRA_TYPE_GPIO,
3350ebc475fSDipen Patel 	.slices = 3,
336b003fb5cSDipen Patel };
337b003fb5cSDipen Patel 
3380ebc475fSDipen Patel static const struct tegra_hte_data t194_lic_hte = {
33909574ccaSDipen Patel 	.map_sz = 0,
34009574ccaSDipen Patel 	.map = NULL,
34109574ccaSDipen Patel 	.type = HTE_TEGRA_TYPE_LIC,
3420ebc475fSDipen Patel 	.slices = 11,
3430ebc475fSDipen Patel };
3440ebc475fSDipen Patel 
3450ebc475fSDipen Patel static const struct tegra_hte_data t234_lic_hte = {
3460ebc475fSDipen Patel 	.map_sz = 0,
3470ebc475fSDipen Patel 	.map = NULL,
3480ebc475fSDipen Patel 	.type = HTE_TEGRA_TYPE_LIC,
3490ebc475fSDipen Patel 	.slices = 17,
35009574ccaSDipen Patel };
35109574ccaSDipen Patel 
tegra_hte_readl(struct tegra_hte_soc * hte,u32 reg)35209574ccaSDipen Patel static inline u32 tegra_hte_readl(struct tegra_hte_soc *hte, u32 reg)
35309574ccaSDipen Patel {
35409574ccaSDipen Patel 	return readl(hte->regs + reg);
35509574ccaSDipen Patel }
35609574ccaSDipen Patel 
tegra_hte_writel(struct tegra_hte_soc * hte,u32 reg,u32 val)35709574ccaSDipen Patel static inline void tegra_hte_writel(struct tegra_hte_soc *hte, u32 reg,
35809574ccaSDipen Patel 				    u32 val)
35909574ccaSDipen Patel {
36009574ccaSDipen Patel 	writel(val, hte->regs + reg);
36109574ccaSDipen Patel }
36209574ccaSDipen Patel 
tegra_hte_map_to_line_id(u32 eid,const struct tegra_hte_line_mapped * m,u32 map_sz,u32 * mapped)36309574ccaSDipen Patel static int tegra_hte_map_to_line_id(u32 eid,
36409574ccaSDipen Patel 				    const struct tegra_hte_line_mapped *m,
36509574ccaSDipen Patel 				    u32 map_sz, u32 *mapped)
36609574ccaSDipen Patel {
36709574ccaSDipen Patel 
36809574ccaSDipen Patel 	if (m) {
369*e078180dSDan Carpenter 		if (eid >= map_sz)
37009574ccaSDipen Patel 			return -EINVAL;
37109574ccaSDipen Patel 		if (m[eid].slice == NV_AON_SLICE_INVALID)
37209574ccaSDipen Patel 			return -EINVAL;
37309574ccaSDipen Patel 
37409574ccaSDipen Patel 		*mapped = (m[eid].slice << 5) + m[eid].bit_index;
37509574ccaSDipen Patel 	} else {
37609574ccaSDipen Patel 		*mapped = eid;
37709574ccaSDipen Patel 	}
37809574ccaSDipen Patel 
37909574ccaSDipen Patel 	return 0;
38009574ccaSDipen Patel }
38109574ccaSDipen Patel 
tegra_hte_line_xlate(struct hte_chip * gc,const struct of_phandle_args * args,struct hte_ts_desc * desc,u32 * xlated_id)38209574ccaSDipen Patel static int tegra_hte_line_xlate(struct hte_chip *gc,
38309574ccaSDipen Patel 				const struct of_phandle_args *args,
38409574ccaSDipen Patel 				struct hte_ts_desc *desc, u32 *xlated_id)
38509574ccaSDipen Patel {
38609574ccaSDipen Patel 	int ret = 0;
38709574ccaSDipen Patel 	u32 line_id;
38809574ccaSDipen Patel 	struct tegra_hte_soc *gs;
38909574ccaSDipen Patel 	const struct tegra_hte_line_mapped *map = NULL;
39009574ccaSDipen Patel 	u32 map_sz = 0;
39109574ccaSDipen Patel 
39209574ccaSDipen Patel 	if (!gc || !desc || !xlated_id)
39309574ccaSDipen Patel 		return -EINVAL;
39409574ccaSDipen Patel 
39509574ccaSDipen Patel 	if (args) {
39609574ccaSDipen Patel 		if (gc->of_hte_n_cells < 1)
39709574ccaSDipen Patel 			return -EINVAL;
39809574ccaSDipen Patel 
39909574ccaSDipen Patel 		if (args->args_count != gc->of_hte_n_cells)
40009574ccaSDipen Patel 			return -EINVAL;
40109574ccaSDipen Patel 
40209574ccaSDipen Patel 		desc->attr.line_id = args->args[0];
40309574ccaSDipen Patel 	}
40409574ccaSDipen Patel 
40509574ccaSDipen Patel 	gs = gc->data;
40609574ccaSDipen Patel 	if (!gs || !gs->prov_data)
40709574ccaSDipen Patel 		return -EINVAL;
40809574ccaSDipen Patel 
40909574ccaSDipen Patel 	/*
41009574ccaSDipen Patel 	 *
41109574ccaSDipen Patel 	 * There are two paths GPIO consumers can take as follows:
41209574ccaSDipen Patel 	 * 1) The consumer (gpiolib-cdev for example) which uses GPIO global
41309574ccaSDipen Patel 	 * number which gets assigned run time.
41409574ccaSDipen Patel 	 * 2) The consumer passing GPIO from the DT which is assigned
41509574ccaSDipen Patel 	 * statically for example by using TEGRA194_AON_GPIO gpio DT binding.
41609574ccaSDipen Patel 	 *
41709574ccaSDipen Patel 	 * The code below addresses both the consumer use cases and maps into
41809574ccaSDipen Patel 	 * HTE/GTE namespace.
41909574ccaSDipen Patel 	 */
42009574ccaSDipen Patel 	if (gs->prov_data->type == HTE_TEGRA_TYPE_GPIO && !args) {
42109574ccaSDipen Patel 		line_id = desc->attr.line_id - gs->c->base;
42209574ccaSDipen Patel 		map = gs->prov_data->map;
42309574ccaSDipen Patel 		map_sz = gs->prov_data->map_sz;
42409574ccaSDipen Patel 	} else if (gs->prov_data->type == HTE_TEGRA_TYPE_GPIO && args) {
42509574ccaSDipen Patel 		line_id = desc->attr.line_id;
42609574ccaSDipen Patel 		map = gs->prov_data->sec_map;
42709574ccaSDipen Patel 		map_sz = gs->prov_data->sec_map_sz;
42809574ccaSDipen Patel 	} else {
42909574ccaSDipen Patel 		line_id = desc->attr.line_id;
43009574ccaSDipen Patel 	}
43109574ccaSDipen Patel 
43209574ccaSDipen Patel 	ret = tegra_hte_map_to_line_id(line_id, map, map_sz, xlated_id);
43309574ccaSDipen Patel 	if (ret < 0) {
43409574ccaSDipen Patel 		dev_err(gc->dev, "line_id:%u mapping failed\n",
43509574ccaSDipen Patel 			desc->attr.line_id);
43609574ccaSDipen Patel 		return ret;
43709574ccaSDipen Patel 	}
43809574ccaSDipen Patel 
43909574ccaSDipen Patel 	if (*xlated_id > gc->nlines)
44009574ccaSDipen Patel 		return -EINVAL;
44109574ccaSDipen Patel 
44209574ccaSDipen Patel 	dev_dbg(gc->dev, "requested id:%u, xlated id:%u\n",
44309574ccaSDipen Patel 		desc->attr.line_id, *xlated_id);
44409574ccaSDipen Patel 
44509574ccaSDipen Patel 	return 0;
44609574ccaSDipen Patel }
44709574ccaSDipen Patel 
tegra_hte_line_xlate_plat(struct hte_chip * gc,struct hte_ts_desc * desc,u32 * xlated_id)44809574ccaSDipen Patel static int tegra_hte_line_xlate_plat(struct hte_chip *gc,
44909574ccaSDipen Patel 				     struct hte_ts_desc *desc, u32 *xlated_id)
45009574ccaSDipen Patel {
45109574ccaSDipen Patel 	return tegra_hte_line_xlate(gc, NULL, desc, xlated_id);
45209574ccaSDipen Patel }
45309574ccaSDipen Patel 
tegra_hte_en_dis_common(struct hte_chip * chip,u32 line_id,bool en)45409574ccaSDipen Patel static int tegra_hte_en_dis_common(struct hte_chip *chip, u32 line_id, bool en)
45509574ccaSDipen Patel {
45609574ccaSDipen Patel 	u32 slice, sl_bit_shift, line_bit, val, reg;
45709574ccaSDipen Patel 	struct tegra_hte_soc *gs;
45809574ccaSDipen Patel 
45909574ccaSDipen Patel 	sl_bit_shift = __builtin_ctz(HTE_SLICE_SIZE);
46009574ccaSDipen Patel 
46109574ccaSDipen Patel 	if (!chip)
46209574ccaSDipen Patel 		return -EINVAL;
46309574ccaSDipen Patel 
46409574ccaSDipen Patel 	gs = chip->data;
46509574ccaSDipen Patel 
46609574ccaSDipen Patel 	if (line_id > chip->nlines) {
46709574ccaSDipen Patel 		dev_err(chip->dev,
46809574ccaSDipen Patel 			"line id: %u is not supported by this controller\n",
46909574ccaSDipen Patel 			line_id);
47009574ccaSDipen Patel 		return -EINVAL;
47109574ccaSDipen Patel 	}
47209574ccaSDipen Patel 
47309574ccaSDipen Patel 	slice = line_id >> sl_bit_shift;
47409574ccaSDipen Patel 	line_bit = line_id & (HTE_SLICE_SIZE - 1);
47509574ccaSDipen Patel 	reg = (slice << sl_bit_shift) + HTE_SLICE0_TETEN;
47609574ccaSDipen Patel 
47709574ccaSDipen Patel 	spin_lock(&gs->sl[slice].s_lock);
47809574ccaSDipen Patel 
47909574ccaSDipen Patel 	if (test_bit(HTE_SUSPEND, &gs->sl[slice].flags)) {
48009574ccaSDipen Patel 		spin_unlock(&gs->sl[slice].s_lock);
48109574ccaSDipen Patel 		dev_dbg(chip->dev, "device suspended");
48209574ccaSDipen Patel 		return -EBUSY;
48309574ccaSDipen Patel 	}
48409574ccaSDipen Patel 
48509574ccaSDipen Patel 	val = tegra_hte_readl(gs, reg);
48609574ccaSDipen Patel 	if (en)
48709574ccaSDipen Patel 		val = val | (1 << line_bit);
48809574ccaSDipen Patel 	else
48909574ccaSDipen Patel 		val = val & (~(1 << line_bit));
49009574ccaSDipen Patel 	tegra_hte_writel(gs, reg, val);
49109574ccaSDipen Patel 
49209574ccaSDipen Patel 	spin_unlock(&gs->sl[slice].s_lock);
49309574ccaSDipen Patel 
49409574ccaSDipen Patel 	dev_dbg(chip->dev, "line: %u, slice %u, line_bit %u, reg:0x%x\n",
49509574ccaSDipen Patel 		line_id, slice, line_bit, reg);
49609574ccaSDipen Patel 
49709574ccaSDipen Patel 	return 0;
49809574ccaSDipen Patel }
49909574ccaSDipen Patel 
tegra_hte_enable(struct hte_chip * chip,u32 line_id)50009574ccaSDipen Patel static int tegra_hte_enable(struct hte_chip *chip, u32 line_id)
50109574ccaSDipen Patel {
50209574ccaSDipen Patel 	if (!chip)
50309574ccaSDipen Patel 		return -EINVAL;
50409574ccaSDipen Patel 
50509574ccaSDipen Patel 	return tegra_hte_en_dis_common(chip, line_id, true);
50609574ccaSDipen Patel }
50709574ccaSDipen Patel 
tegra_hte_disable(struct hte_chip * chip,u32 line_id)50809574ccaSDipen Patel static int tegra_hte_disable(struct hte_chip *chip, u32 line_id)
50909574ccaSDipen Patel {
51009574ccaSDipen Patel 	if (!chip)
51109574ccaSDipen Patel 		return -EINVAL;
51209574ccaSDipen Patel 
51309574ccaSDipen Patel 	return tegra_hte_en_dis_common(chip, line_id, false);
51409574ccaSDipen Patel }
51509574ccaSDipen Patel 
tegra_hte_request(struct hte_chip * chip,struct hte_ts_desc * desc,u32 line_id)51609574ccaSDipen Patel static int tegra_hte_request(struct hte_chip *chip, struct hte_ts_desc *desc,
51709574ccaSDipen Patel 			     u32 line_id)
51809574ccaSDipen Patel {
51909574ccaSDipen Patel 	int ret;
52009574ccaSDipen Patel 	struct tegra_hte_soc *gs;
52109574ccaSDipen Patel 	struct hte_line_attr *attr;
52209574ccaSDipen Patel 
52309574ccaSDipen Patel 	if (!chip || !chip->data || !desc)
52409574ccaSDipen Patel 		return -EINVAL;
52509574ccaSDipen Patel 
52609574ccaSDipen Patel 	gs = chip->data;
52709574ccaSDipen Patel 	attr = &desc->attr;
52809574ccaSDipen Patel 
52909574ccaSDipen Patel 	if (gs->prov_data->type == HTE_TEGRA_TYPE_GPIO) {
53009574ccaSDipen Patel 		if (!attr->line_data)
53109574ccaSDipen Patel 			return -EINVAL;
53209574ccaSDipen Patel 
53309574ccaSDipen Patel 		ret = gpiod_enable_hw_timestamp_ns(attr->line_data,
53409574ccaSDipen Patel 						   attr->edge_flags);
53509574ccaSDipen Patel 		if (ret)
53609574ccaSDipen Patel 			return ret;
53709574ccaSDipen Patel 
53809574ccaSDipen Patel 		gs->line_data[line_id].data = attr->line_data;
53909574ccaSDipen Patel 		gs->line_data[line_id].flags = attr->edge_flags;
54009574ccaSDipen Patel 	}
54109574ccaSDipen Patel 
54209574ccaSDipen Patel 	return tegra_hte_en_dis_common(chip, line_id, true);
54309574ccaSDipen Patel }
54409574ccaSDipen Patel 
tegra_hte_release(struct hte_chip * chip,struct hte_ts_desc * desc,u32 line_id)54509574ccaSDipen Patel static int tegra_hte_release(struct hte_chip *chip, struct hte_ts_desc *desc,
54609574ccaSDipen Patel 			     u32 line_id)
54709574ccaSDipen Patel {
54809574ccaSDipen Patel 	struct tegra_hte_soc *gs;
54909574ccaSDipen Patel 	struct hte_line_attr *attr;
55009574ccaSDipen Patel 	int ret;
55109574ccaSDipen Patel 
55209574ccaSDipen Patel 	if (!chip || !chip->data || !desc)
55309574ccaSDipen Patel 		return -EINVAL;
55409574ccaSDipen Patel 
55509574ccaSDipen Patel 	gs = chip->data;
55609574ccaSDipen Patel 	attr = &desc->attr;
55709574ccaSDipen Patel 
55809574ccaSDipen Patel 	if (gs->prov_data->type == HTE_TEGRA_TYPE_GPIO) {
55909574ccaSDipen Patel 		ret = gpiod_disable_hw_timestamp_ns(attr->line_data,
56009574ccaSDipen Patel 						    gs->line_data[line_id].flags);
56109574ccaSDipen Patel 		if (ret)
56209574ccaSDipen Patel 			return ret;
56309574ccaSDipen Patel 
56409574ccaSDipen Patel 		gs->line_data[line_id].data = NULL;
56509574ccaSDipen Patel 		gs->line_data[line_id].flags = 0;
56609574ccaSDipen Patel 	}
56709574ccaSDipen Patel 
56809574ccaSDipen Patel 	return tegra_hte_en_dis_common(chip, line_id, false);
56909574ccaSDipen Patel }
57009574ccaSDipen Patel 
tegra_hte_clk_src_info(struct hte_chip * chip,struct hte_clk_info * ci)57109574ccaSDipen Patel static int tegra_hte_clk_src_info(struct hte_chip *chip,
57209574ccaSDipen Patel 				  struct hte_clk_info *ci)
57309574ccaSDipen Patel {
57409574ccaSDipen Patel 	(void)chip;
57509574ccaSDipen Patel 
57609574ccaSDipen Patel 	if (!ci)
57709574ccaSDipen Patel 		return -EINVAL;
57809574ccaSDipen Patel 
57909574ccaSDipen Patel 	ci->hz = HTE_TS_CLK_RATE_HZ;
58009574ccaSDipen Patel 	ci->type = CLOCK_MONOTONIC;
58109574ccaSDipen Patel 
58209574ccaSDipen Patel 	return 0;
58309574ccaSDipen Patel }
58409574ccaSDipen Patel 
tegra_hte_get_level(struct tegra_hte_soc * gs,u32 line_id)58509574ccaSDipen Patel static int tegra_hte_get_level(struct tegra_hte_soc *gs, u32 line_id)
58609574ccaSDipen Patel {
58709574ccaSDipen Patel 	struct gpio_desc *desc;
58809574ccaSDipen Patel 
58909574ccaSDipen Patel 	if (gs->prov_data->type == HTE_TEGRA_TYPE_GPIO) {
59009574ccaSDipen Patel 		desc = gs->line_data[line_id].data;
59109574ccaSDipen Patel 		if (desc)
59209574ccaSDipen Patel 			return gpiod_get_raw_value(desc);
59309574ccaSDipen Patel 	}
59409574ccaSDipen Patel 
59509574ccaSDipen Patel 	return -1;
59609574ccaSDipen Patel }
59709574ccaSDipen Patel 
tegra_hte_read_fifo(struct tegra_hte_soc * gs)59809574ccaSDipen Patel static void tegra_hte_read_fifo(struct tegra_hte_soc *gs)
59909574ccaSDipen Patel {
60009574ccaSDipen Patel 	u32 tsh, tsl, src, pv, cv, acv, slice, bit_index, line_id;
60109574ccaSDipen Patel 	u64 tsc;
60209574ccaSDipen Patel 	struct hte_ts_data el;
60309574ccaSDipen Patel 
60409574ccaSDipen Patel 	while ((tegra_hte_readl(gs, HTE_TESTATUS) >>
60509574ccaSDipen Patel 		HTE_TESTATUS_OCCUPANCY_SHIFT) &
60609574ccaSDipen Patel 		HTE_TESTATUS_OCCUPANCY_MASK) {
60709574ccaSDipen Patel 		tsh = tegra_hte_readl(gs, HTE_TETSCH);
60809574ccaSDipen Patel 		tsl = tegra_hte_readl(gs, HTE_TETSCL);
60909574ccaSDipen Patel 		tsc = (((u64)tsh << 32) | tsl);
61009574ccaSDipen Patel 
61109574ccaSDipen Patel 		src = tegra_hte_readl(gs, HTE_TESRC);
61209574ccaSDipen Patel 		slice = (src >> HTE_TESRC_SLICE_SHIFT) &
61309574ccaSDipen Patel 			    HTE_TESRC_SLICE_DEFAULT_MASK;
61409574ccaSDipen Patel 
61509574ccaSDipen Patel 		pv = tegra_hte_readl(gs, HTE_TEPCV);
61609574ccaSDipen Patel 		cv = tegra_hte_readl(gs, HTE_TECCV);
61709574ccaSDipen Patel 		acv = pv ^ cv;
61809574ccaSDipen Patel 		while (acv) {
61909574ccaSDipen Patel 			bit_index = __builtin_ctz(acv);
62009574ccaSDipen Patel 			line_id = bit_index + (slice << 5);
62109574ccaSDipen Patel 			el.tsc = tsc << HTE_TS_NS_SHIFT;
62209574ccaSDipen Patel 			el.raw_level = tegra_hte_get_level(gs, line_id);
62309574ccaSDipen Patel 			hte_push_ts_ns(gs->chip, line_id, &el);
62409574ccaSDipen Patel 			acv &= ~BIT(bit_index);
62509574ccaSDipen Patel 		}
62609574ccaSDipen Patel 		tegra_hte_writel(gs, HTE_TECMD, HTE_TECMD_CMD_POP);
62709574ccaSDipen Patel 	}
62809574ccaSDipen Patel }
62909574ccaSDipen Patel 
tegra_hte_isr(int irq,void * dev_id)63009574ccaSDipen Patel static irqreturn_t tegra_hte_isr(int irq, void *dev_id)
63109574ccaSDipen Patel {
63209574ccaSDipen Patel 	struct tegra_hte_soc *gs = dev_id;
63309574ccaSDipen Patel 	(void)irq;
63409574ccaSDipen Patel 
63509574ccaSDipen Patel 	tegra_hte_read_fifo(gs);
63609574ccaSDipen Patel 
63709574ccaSDipen Patel 	return IRQ_HANDLED;
63809574ccaSDipen Patel }
63909574ccaSDipen Patel 
tegra_hte_match_from_linedata(const struct hte_chip * chip,const struct hte_ts_desc * hdesc)64009574ccaSDipen Patel static bool tegra_hte_match_from_linedata(const struct hte_chip *chip,
64109574ccaSDipen Patel 					  const struct hte_ts_desc *hdesc)
64209574ccaSDipen Patel {
64309574ccaSDipen Patel 	struct tegra_hte_soc *hte_dev = chip->data;
64409574ccaSDipen Patel 
64509574ccaSDipen Patel 	if (!hte_dev || (hte_dev->prov_data->type != HTE_TEGRA_TYPE_GPIO))
64609574ccaSDipen Patel 		return false;
64709574ccaSDipen Patel 
64809574ccaSDipen Patel 	return hte_dev->c == gpiod_to_chip(hdesc->attr.line_data);
64909574ccaSDipen Patel }
65009574ccaSDipen Patel 
65109574ccaSDipen Patel static const struct of_device_id tegra_hte_of_match[] = {
6520ebc475fSDipen Patel 	{ .compatible = "nvidia,tegra194-gte-lic", .data = &t194_lic_hte},
653b003fb5cSDipen Patel 	{ .compatible = "nvidia,tegra194-gte-aon", .data = &t194_aon_hte},
6540ebc475fSDipen Patel 	{ .compatible = "nvidia,tegra234-gte-lic", .data = &t234_lic_hte},
655b003fb5cSDipen Patel 	{ .compatible = "nvidia,tegra234-gte-aon", .data = &t234_aon_hte},
65609574ccaSDipen Patel 	{ }
65709574ccaSDipen Patel };
65809574ccaSDipen Patel MODULE_DEVICE_TABLE(of, tegra_hte_of_match);
65909574ccaSDipen Patel 
66009574ccaSDipen Patel static const struct hte_ops g_ops = {
66109574ccaSDipen Patel 	.request = tegra_hte_request,
66209574ccaSDipen Patel 	.release = tegra_hte_release,
66309574ccaSDipen Patel 	.enable = tegra_hte_enable,
66409574ccaSDipen Patel 	.disable = tegra_hte_disable,
66509574ccaSDipen Patel 	.get_clk_src_info = tegra_hte_clk_src_info,
66609574ccaSDipen Patel };
66709574ccaSDipen Patel 
tegra_gte_disable(void * data)66809574ccaSDipen Patel static void tegra_gte_disable(void *data)
66909574ccaSDipen Patel {
67009574ccaSDipen Patel 	struct platform_device *pdev = data;
67109574ccaSDipen Patel 	struct tegra_hte_soc *gs = dev_get_drvdata(&pdev->dev);
67209574ccaSDipen Patel 
67309574ccaSDipen Patel 	tegra_hte_writel(gs, HTE_TECTRL, 0);
67409574ccaSDipen Patel }
67509574ccaSDipen Patel 
tegra_get_gpiochip_from_name(struct gpio_chip * chip,void * data)67609574ccaSDipen Patel static int tegra_get_gpiochip_from_name(struct gpio_chip *chip, void *data)
67709574ccaSDipen Patel {
67809574ccaSDipen Patel 	return !strcmp(chip->label, data);
67909574ccaSDipen Patel }
68009574ccaSDipen Patel 
tegra_gpiochip_match(struct gpio_chip * chip,void * data)681d02b1cabSDipen Patel static int tegra_gpiochip_match(struct gpio_chip *chip, void *data)
682d02b1cabSDipen Patel {
683d02b1cabSDipen Patel 	return chip->fwnode == of_node_to_fwnode(data);
684d02b1cabSDipen Patel }
685d02b1cabSDipen Patel 
tegra_hte_probe(struct platform_device * pdev)68609574ccaSDipen Patel static int tegra_hte_probe(struct platform_device *pdev)
68709574ccaSDipen Patel {
68809574ccaSDipen Patel 	int ret;
68909574ccaSDipen Patel 	u32 i, slices, val = 0;
69009574ccaSDipen Patel 	u32 nlines;
69109574ccaSDipen Patel 	struct device *dev;
69209574ccaSDipen Patel 	struct tegra_hte_soc *hte_dev;
69309574ccaSDipen Patel 	struct hte_chip *gc;
694d02b1cabSDipen Patel 	struct device_node *gpio_ctrl;
69509574ccaSDipen Patel 
69609574ccaSDipen Patel 	dev = &pdev->dev;
69709574ccaSDipen Patel 
69809574ccaSDipen Patel 	hte_dev = devm_kzalloc(dev, sizeof(*hte_dev), GFP_KERNEL);
69909574ccaSDipen Patel 	if (!hte_dev)
70009574ccaSDipen Patel 		return -ENOMEM;
70109574ccaSDipen Patel 
70209574ccaSDipen Patel 	gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL);
70309574ccaSDipen Patel 	if (!gc)
70409574ccaSDipen Patel 		return -ENOMEM;
70509574ccaSDipen Patel 
70609574ccaSDipen Patel 	dev_set_drvdata(&pdev->dev, hte_dev);
70709574ccaSDipen Patel 	hte_dev->prov_data = of_device_get_match_data(&pdev->dev);
70809574ccaSDipen Patel 
7090ebc475fSDipen Patel 	ret = of_property_read_u32(dev->of_node, "nvidia,slices", &slices);
7100ebc475fSDipen Patel 	if (ret != 0)
7110ebc475fSDipen Patel 		slices = hte_dev->prov_data->slices;
7120ebc475fSDipen Patel 
7130ebc475fSDipen Patel 	dev_dbg(dev, "slices:%d\n", slices);
7140ebc475fSDipen Patel 	nlines = slices << 5;
7150ebc475fSDipen Patel 
71609574ccaSDipen Patel 	hte_dev->regs = devm_platform_ioremap_resource(pdev, 0);
71709574ccaSDipen Patel 	if (IS_ERR(hte_dev->regs))
71809574ccaSDipen Patel 		return PTR_ERR(hte_dev->regs);
71909574ccaSDipen Patel 
72009574ccaSDipen Patel 	ret = of_property_read_u32(dev->of_node, "nvidia,int-threshold",
72109574ccaSDipen Patel 				   &hte_dev->itr_thrshld);
72209574ccaSDipen Patel 	if (ret != 0)
72309574ccaSDipen Patel 		hte_dev->itr_thrshld = 1;
72409574ccaSDipen Patel 
72509574ccaSDipen Patel 	hte_dev->sl = devm_kcalloc(dev, slices, sizeof(*hte_dev->sl),
72609574ccaSDipen Patel 				   GFP_KERNEL);
72709574ccaSDipen Patel 	if (!hte_dev->sl)
72809574ccaSDipen Patel 		return -ENOMEM;
72909574ccaSDipen Patel 
73009574ccaSDipen Patel 	ret = platform_get_irq(pdev, 0);
73109574ccaSDipen Patel 	if (ret < 0) {
73209574ccaSDipen Patel 		dev_err_probe(dev, ret, "failed to get irq\n");
73309574ccaSDipen Patel 		return ret;
73409574ccaSDipen Patel 	}
73509574ccaSDipen Patel 	hte_dev->hte_irq = ret;
73609574ccaSDipen Patel 	ret = devm_request_irq(dev, hte_dev->hte_irq, tegra_hte_isr, 0,
73709574ccaSDipen Patel 			       dev_name(dev), hte_dev);
73809574ccaSDipen Patel 	if (ret < 0) {
73909574ccaSDipen Patel 		dev_err(dev, "request irq failed.\n");
74009574ccaSDipen Patel 		return ret;
74109574ccaSDipen Patel 	}
74209574ccaSDipen Patel 
74309574ccaSDipen Patel 	gc->nlines = nlines;
74409574ccaSDipen Patel 	gc->ops = &g_ops;
74509574ccaSDipen Patel 	gc->dev = dev;
74609574ccaSDipen Patel 	gc->data = hte_dev;
74709574ccaSDipen Patel 	gc->xlate_of = tegra_hte_line_xlate;
74809574ccaSDipen Patel 	gc->xlate_plat = tegra_hte_line_xlate_plat;
74909574ccaSDipen Patel 	gc->of_hte_n_cells = 1;
75009574ccaSDipen Patel 
75109574ccaSDipen Patel 	if (hte_dev->prov_data &&
75209574ccaSDipen Patel 	    hte_dev->prov_data->type == HTE_TEGRA_TYPE_GPIO) {
75309574ccaSDipen Patel 		hte_dev->line_data = devm_kcalloc(dev, nlines,
75409574ccaSDipen Patel 						  sizeof(*hte_dev->line_data),
75509574ccaSDipen Patel 						  GFP_KERNEL);
75609574ccaSDipen Patel 		if (!hte_dev->line_data)
75709574ccaSDipen Patel 			return -ENOMEM;
75809574ccaSDipen Patel 
75909574ccaSDipen Patel 		gc->match_from_linedata = tegra_hte_match_from_linedata;
76009574ccaSDipen Patel 
761b003fb5cSDipen Patel 		if (of_device_is_compatible(dev->of_node,
762d02b1cabSDipen Patel 					    "nvidia,tegra194-gte-aon")) {
76309574ccaSDipen Patel 			hte_dev->c = gpiochip_find("tegra194-gpio-aon",
76409574ccaSDipen Patel 						tegra_get_gpiochip_from_name);
765d02b1cabSDipen Patel 		} else {
766d02b1cabSDipen Patel 			gpio_ctrl = of_parse_phandle(dev->of_node,
767d02b1cabSDipen Patel 						     "nvidia,gpio-controller",
768d02b1cabSDipen Patel 						     0);
769d02b1cabSDipen Patel 			if (!gpio_ctrl) {
770d02b1cabSDipen Patel 				dev_err(dev,
771d02b1cabSDipen Patel 					"gpio controller node not found\n");
772b003fb5cSDipen Patel 				return -ENODEV;
773d02b1cabSDipen Patel 			}
774d02b1cabSDipen Patel 
775d02b1cabSDipen Patel 			hte_dev->c = gpiochip_find(gpio_ctrl,
776d02b1cabSDipen Patel 						   tegra_gpiochip_match);
777d02b1cabSDipen Patel 			of_node_put(gpio_ctrl);
778d02b1cabSDipen Patel 		}
779b003fb5cSDipen Patel 
78009574ccaSDipen Patel 		if (!hte_dev->c)
78109574ccaSDipen Patel 			return dev_err_probe(dev, -EPROBE_DEFER,
78209574ccaSDipen Patel 					     "wait for gpio controller\n");
78309574ccaSDipen Patel 	}
78409574ccaSDipen Patel 
78509574ccaSDipen Patel 	hte_dev->chip = gc;
78609574ccaSDipen Patel 
78709574ccaSDipen Patel 	ret = devm_hte_register_chip(hte_dev->chip);
78809574ccaSDipen Patel 	if (ret) {
78909574ccaSDipen Patel 		dev_err(gc->dev, "hte chip register failed");
79009574ccaSDipen Patel 		return ret;
79109574ccaSDipen Patel 	}
79209574ccaSDipen Patel 
79309574ccaSDipen Patel 	for (i = 0; i < slices; i++) {
79409574ccaSDipen Patel 		hte_dev->sl[i].flags = 0;
79509574ccaSDipen Patel 		spin_lock_init(&hte_dev->sl[i].s_lock);
79609574ccaSDipen Patel 	}
79709574ccaSDipen Patel 
79809574ccaSDipen Patel 	val = HTE_TECTRL_ENABLE_ENABLE |
79909574ccaSDipen Patel 	      (HTE_TECTRL_INTR_ENABLE << HTE_TECTRL_INTR_SHIFT) |
80009574ccaSDipen Patel 	      (hte_dev->itr_thrshld << HTE_TECTRL_OCCU_SHIFT);
80109574ccaSDipen Patel 	tegra_hte_writel(hte_dev, HTE_TECTRL, val);
80209574ccaSDipen Patel 
80309574ccaSDipen Patel 	ret = devm_add_action_or_reset(&pdev->dev, tegra_gte_disable, pdev);
80409574ccaSDipen Patel 	if (ret)
80509574ccaSDipen Patel 		return ret;
80609574ccaSDipen Patel 
80709574ccaSDipen Patel 	dev_dbg(gc->dev, "lines: %d, slices:%d", gc->nlines, slices);
80809574ccaSDipen Patel 
80909574ccaSDipen Patel 	return 0;
81009574ccaSDipen Patel }
81109574ccaSDipen Patel 
tegra_hte_resume_early(struct device * dev)81209574ccaSDipen Patel static int __maybe_unused tegra_hte_resume_early(struct device *dev)
81309574ccaSDipen Patel {
81409574ccaSDipen Patel 	u32 i;
81509574ccaSDipen Patel 	struct tegra_hte_soc *gs = dev_get_drvdata(dev);
81609574ccaSDipen Patel 	u32 slices = gs->chip->nlines / NV_LINES_IN_SLICE;
81709574ccaSDipen Patel 	u32 sl_bit_shift = __builtin_ctz(HTE_SLICE_SIZE);
81809574ccaSDipen Patel 
81909574ccaSDipen Patel 	tegra_hte_writel(gs, HTE_TECTRL, gs->conf_rval);
82009574ccaSDipen Patel 
82109574ccaSDipen Patel 	for (i = 0; i < slices; i++) {
82209574ccaSDipen Patel 		spin_lock(&gs->sl[i].s_lock);
82309574ccaSDipen Patel 		tegra_hte_writel(gs,
82409574ccaSDipen Patel 				 ((i << sl_bit_shift) + HTE_SLICE0_TETEN),
82509574ccaSDipen Patel 				 gs->sl[i].r_val);
82609574ccaSDipen Patel 		clear_bit(HTE_SUSPEND, &gs->sl[i].flags);
82709574ccaSDipen Patel 		spin_unlock(&gs->sl[i].s_lock);
82809574ccaSDipen Patel 	}
82909574ccaSDipen Patel 
83009574ccaSDipen Patel 	return 0;
83109574ccaSDipen Patel }
83209574ccaSDipen Patel 
tegra_hte_suspend_late(struct device * dev)83309574ccaSDipen Patel static int __maybe_unused tegra_hte_suspend_late(struct device *dev)
83409574ccaSDipen Patel {
83509574ccaSDipen Patel 	u32 i;
83609574ccaSDipen Patel 	struct tegra_hte_soc *gs = dev_get_drvdata(dev);
83709574ccaSDipen Patel 	u32 slices = gs->chip->nlines / NV_LINES_IN_SLICE;
83809574ccaSDipen Patel 	u32 sl_bit_shift = __builtin_ctz(HTE_SLICE_SIZE);
83909574ccaSDipen Patel 
84009574ccaSDipen Patel 	gs->conf_rval = tegra_hte_readl(gs, HTE_TECTRL);
84109574ccaSDipen Patel 	for (i = 0; i < slices; i++) {
84209574ccaSDipen Patel 		spin_lock(&gs->sl[i].s_lock);
84309574ccaSDipen Patel 		gs->sl[i].r_val = tegra_hte_readl(gs,
84409574ccaSDipen Patel 				((i << sl_bit_shift) + HTE_SLICE0_TETEN));
84509574ccaSDipen Patel 		set_bit(HTE_SUSPEND, &gs->sl[i].flags);
84609574ccaSDipen Patel 		spin_unlock(&gs->sl[i].s_lock);
84709574ccaSDipen Patel 	}
84809574ccaSDipen Patel 
84909574ccaSDipen Patel 	return 0;
85009574ccaSDipen Patel }
85109574ccaSDipen Patel 
85209574ccaSDipen Patel static const struct dev_pm_ops tegra_hte_pm = {
85309574ccaSDipen Patel 	SET_LATE_SYSTEM_SLEEP_PM_OPS(tegra_hte_suspend_late,
85409574ccaSDipen Patel 				     tegra_hte_resume_early)
85509574ccaSDipen Patel };
85609574ccaSDipen Patel 
85709574ccaSDipen Patel static struct platform_driver tegra_hte_driver = {
85809574ccaSDipen Patel 	.probe = tegra_hte_probe,
85909574ccaSDipen Patel 	.driver = {
86009574ccaSDipen Patel 		.name = "tegra_hte",
86109574ccaSDipen Patel 		.pm = &tegra_hte_pm,
86209574ccaSDipen Patel 		.of_match_table = tegra_hte_of_match,
86309574ccaSDipen Patel 	},
86409574ccaSDipen Patel };
86509574ccaSDipen Patel 
86609574ccaSDipen Patel module_platform_driver(tegra_hte_driver);
86709574ccaSDipen Patel 
86809574ccaSDipen Patel MODULE_AUTHOR("Dipen Patel <dipenp@nvidia.com>");
86909574ccaSDipen Patel MODULE_DESCRIPTION("NVIDIA Tegra HTE (Hardware Timestamping Engine) driver");
87009574ccaSDipen Patel MODULE_LICENSE("GPL");
871