xref: /aosp_15_r20/external/coreboot/src/soc/nvidia/tegra/gpio.h (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #ifndef __SOC_NVIDIA_TEGRA_GPIO_H__
4 #define __SOC_NVIDIA_TEGRA_GPIO_H__
5 
6 #include <stdint.h>
7 
8 #include "pinmux.h"
9 
10 typedef u32 gpio_t;
11 
12 #define GPIO_PINMUX_SHIFT 16
13 #define GPIO(name) ((gpio_t)(GPIO_##name##_INDEX | \
14 			     (PINMUX_GPIO_##name << GPIO_PINMUX_SHIFT)))
15 
16 /* Functions to modify specific GPIO control values. */
17 
18 enum gpio_mode {
19 	GPIO_MODE_SPIO = 0,
20 	GPIO_MODE_GPIO = 1
21 };
22 void gpio_set_mode(gpio_t gpio, enum gpio_mode);
23 int gpio_get_mode(gpio_t gpio);
24 
25 // Lock a GPIO with extreme caution since they can't be unlocked.
26 void gpio_set_lock(gpio_t gpio);
27 int gpio_get_lock(gpio_t gpio);
28 
29 void gpio_set_out_enable(gpio_t gpio, int enable);
30 int gpio_get_out_enable(gpio_t gpio);
31 
32 int gpio_get_out_value(gpio_t gpio);
33 
34 int gpio_get_int_status(gpio_t gpio);
35 
36 void gpio_set_int_enable(gpio_t gpio, int enable);
37 int gpio_get_int_enable(gpio_t gpio);
38 
39 void gpio_set_int_level(gpio_t gpio, int high_rise, int edge, int delta);
40 void gpio_get_int_level(gpio_t gpio, int *high_rise, int *edge, int *delta);
41 
42 void gpio_set_int_clear(gpio_t gpio);
43 
44 void gpio_output_open_drain(gpio_t gpio, int value);
45 
46 /* Hardware definitions. */
47 
48 enum {
49 	GPIO_GPIOS_PER_PORT = 8,
50 	GPIO_PORTS_PER_BANK = 4,
51 	GPIO_BANKS = 8,
52 
53 	GPIO_GPIOS_PER_BANK = GPIO_GPIOS_PER_PORT * GPIO_PORTS_PER_BANK,
54 	GPIO_GPIOS = GPIO_BANKS * GPIO_GPIOS_PER_BANK
55 };
56 
gpio_index_to_bank(int index)57 static inline int gpio_index_to_bank(int index)
58 {
59 	return index / GPIO_GPIOS_PER_BANK;
60 }
61 
gpio_index_to_port(int index)62 static inline int gpio_index_to_port(int index)
63 {
64 	return (index % GPIO_GPIOS_PER_BANK) / GPIO_GPIOS_PER_PORT;
65 }
66 
gpio_to_bit(int index)67 static inline int gpio_to_bit(int index)
68 {
69 	return index % GPIO_GPIOS_PER_PORT;
70 }
71 
72 struct gpio_bank {
73 	// Values
74 	u32 config[GPIO_PORTS_PER_BANK];
75 	u32 out_enable[GPIO_PORTS_PER_BANK];
76 	u32 out_value[GPIO_PORTS_PER_BANK];
77 	u32 in_value[GPIO_PORTS_PER_BANK];
78 	u32 int_status[GPIO_PORTS_PER_BANK];
79 	u32 int_enable[GPIO_PORTS_PER_BANK];
80 	u32 int_level[GPIO_PORTS_PER_BANK];
81 	u32 int_clear[GPIO_PORTS_PER_BANK];
82 
83 	// Masks
84 	u32 config_mask[GPIO_PORTS_PER_BANK];
85 	u32 out_enable_mask[GPIO_PORTS_PER_BANK];
86 	u32 out_value_mask[GPIO_PORTS_PER_BANK];
87 	u32 in_value_mask[GPIO_PORTS_PER_BANK];
88 	u32 int_status_mask[GPIO_PORTS_PER_BANK];
89 	u32 int_enable_mask[GPIO_PORTS_PER_BANK];
90 	u32 int_level_mask[GPIO_PORTS_PER_BANK];
91 	u32 int_clear_mask[GPIO_PORTS_PER_BANK];
92 };
93 
94 #endif	/* __SOC_NVIDIA_TEGRA_GPIO_H__ */
95