183d290c5STom Rini /* SPDX-License-Identifier: GPL-2.0+ */
2fe1b4db0SIan Campbell /*
3fe1b4db0SIan Campbell * (C) Copyright 2007-2012
4fe1b4db0SIan Campbell * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
5fe1b4db0SIan Campbell * Tom Cubie <tangliang@allwinnertech.com>
6fe1b4db0SIan Campbell */
7fe1b4db0SIan Campbell
8fe1b4db0SIan Campbell #ifndef _SUNXI_GPIO_H
9fe1b4db0SIan Campbell #define _SUNXI_GPIO_H
10fe1b4db0SIan Campbell
11fe1b4db0SIan Campbell #include <linux/types.h>
12e373aad3SHans de Goede #include <asm/arch/cpu.h>
13fe1b4db0SIan Campbell
14fe1b4db0SIan Campbell /*
15fe1b4db0SIan Campbell * sunxi has 9 banks of gpio, they are:
16fe1b4db0SIan Campbell * PA0 - PA17 | PB0 - PB23 | PC0 - PC24
17fe1b4db0SIan Campbell * PD0 - PD27 | PE0 - PE31 | PF0 - PF5
18fe1b4db0SIan Campbell * PG0 - PG9 | PH0 - PH27 | PI0 - PI12
19fe1b4db0SIan Campbell */
20fe1b4db0SIan Campbell
21fe1b4db0SIan Campbell #define SUNXI_GPIO_A 0
22fe1b4db0SIan Campbell #define SUNXI_GPIO_B 1
23fe1b4db0SIan Campbell #define SUNXI_GPIO_C 2
24fe1b4db0SIan Campbell #define SUNXI_GPIO_D 3
25fe1b4db0SIan Campbell #define SUNXI_GPIO_E 4
26fe1b4db0SIan Campbell #define SUNXI_GPIO_F 5
27fe1b4db0SIan Campbell #define SUNXI_GPIO_G 6
28fe1b4db0SIan Campbell #define SUNXI_GPIO_H 7
29fe1b4db0SIan Campbell #define SUNXI_GPIO_I 8
30e373aad3SHans de Goede
31e373aad3SHans de Goede /*
32e373aad3SHans de Goede * This defines the number of GPIO banks for the _main_ GPIO controller.
33e373aad3SHans de Goede * You should fix up the padding in struct sunxi_gpio_reg below if you
34e373aad3SHans de Goede * change this.
35e373aad3SHans de Goede */
36fe1b4db0SIan Campbell #define SUNXI_GPIO_BANKS 9
37fe1b4db0SIan Campbell
38e373aad3SHans de Goede /*
39e373aad3SHans de Goede * sun6i/sun8i and later SoCs have an additional GPIO controller (R_PIO)
40e373aad3SHans de Goede * at a different register offset.
41e373aad3SHans de Goede *
42e373aad3SHans de Goede * sun6i has 2 banks:
43e373aad3SHans de Goede * PL0 - PL8 | PM0 - PM7
44e373aad3SHans de Goede *
45e373aad3SHans de Goede * sun8i has 1 bank:
46e373aad3SHans de Goede * PL0 - PL11
47d35488c7SHans de Goede *
48d35488c7SHans de Goede * sun9i has 3 banks:
49d35488c7SHans de Goede * PL0 - PL9 | PM0 - PM15 | PN0 - PN1
50e373aad3SHans de Goede */
51e373aad3SHans de Goede #define SUNXI_GPIO_L 11
52e373aad3SHans de Goede #define SUNXI_GPIO_M 12
53d35488c7SHans de Goede #define SUNXI_GPIO_N 13
54e373aad3SHans de Goede
55fe1b4db0SIan Campbell struct sunxi_gpio {
56fe1b4db0SIan Campbell u32 cfg[4];
57fe1b4db0SIan Campbell u32 dat;
58fe1b4db0SIan Campbell u32 drv[2];
59fe1b4db0SIan Campbell u32 pull[2];
60fe1b4db0SIan Campbell };
61fe1b4db0SIan Campbell
62fe1b4db0SIan Campbell /* gpio interrupt control */
63fe1b4db0SIan Campbell struct sunxi_gpio_int {
64fe1b4db0SIan Campbell u32 cfg[3];
65fe1b4db0SIan Campbell u32 ctl;
66fe1b4db0SIan Campbell u32 sta;
67fe1b4db0SIan Campbell u32 deb; /* interrupt debounce */
68fe1b4db0SIan Campbell };
69fe1b4db0SIan Campbell
70fe1b4db0SIan Campbell struct sunxi_gpio_reg {
71fe1b4db0SIan Campbell struct sunxi_gpio gpio_bank[SUNXI_GPIO_BANKS];
72fe1b4db0SIan Campbell u8 res[0xbc];
73fe1b4db0SIan Campbell struct sunxi_gpio_int gpio_int;
74fe1b4db0SIan Campbell };
75fe1b4db0SIan Campbell
76e373aad3SHans de Goede #define BANK_TO_GPIO(bank) (((bank) < SUNXI_GPIO_L) ? \
77e373aad3SHans de Goede &((struct sunxi_gpio_reg *)SUNXI_PIO_BASE)->gpio_bank[bank] : \
78e373aad3SHans de Goede &((struct sunxi_gpio_reg *)SUNXI_R_PIO_BASE)->gpio_bank[(bank) - SUNXI_GPIO_L])
79fe1b4db0SIan Campbell
80fe1b4db0SIan Campbell #define GPIO_BANK(pin) ((pin) >> 5)
81fe1b4db0SIan Campbell #define GPIO_NUM(pin) ((pin) & 0x1f)
82fe1b4db0SIan Campbell
83fe1b4db0SIan Campbell #define GPIO_CFG_INDEX(pin) (((pin) & 0x1f) >> 3)
84fe1b4db0SIan Campbell #define GPIO_CFG_OFFSET(pin) ((((pin) & 0x1f) & 0x7) << 2)
85fe1b4db0SIan Campbell
86fe1b4db0SIan Campbell #define GPIO_DRV_INDEX(pin) (((pin) & 0x1f) >> 4)
87fe1b4db0SIan Campbell #define GPIO_DRV_OFFSET(pin) ((((pin) & 0x1f) & 0xf) << 1)
88fe1b4db0SIan Campbell
89fe1b4db0SIan Campbell #define GPIO_PULL_INDEX(pin) (((pin) & 0x1f) >> 4)
90fe1b4db0SIan Campbell #define GPIO_PULL_OFFSET(pin) ((((pin) & 0x1f) & 0xf) << 1)
91fe1b4db0SIan Campbell
92fe1b4db0SIan Campbell /* GPIO bank sizes */
93fe1b4db0SIan Campbell #define SUNXI_GPIO_A_NR 32
94fe1b4db0SIan Campbell #define SUNXI_GPIO_B_NR 32
95fe1b4db0SIan Campbell #define SUNXI_GPIO_C_NR 32
96fe1b4db0SIan Campbell #define SUNXI_GPIO_D_NR 32
97fe1b4db0SIan Campbell #define SUNXI_GPIO_E_NR 32
98fe1b4db0SIan Campbell #define SUNXI_GPIO_F_NR 32
99fe1b4db0SIan Campbell #define SUNXI_GPIO_G_NR 32
100fe1b4db0SIan Campbell #define SUNXI_GPIO_H_NR 32
101fe1b4db0SIan Campbell #define SUNXI_GPIO_I_NR 32
102e373aad3SHans de Goede #define SUNXI_GPIO_L_NR 32
103e373aad3SHans de Goede #define SUNXI_GPIO_M_NR 32
104fe1b4db0SIan Campbell
105fe1b4db0SIan Campbell #define SUNXI_GPIO_NEXT(__gpio) \
106fe1b4db0SIan Campbell ((__gpio##_START) + (__gpio##_NR) + 0)
107fe1b4db0SIan Campbell
108fe1b4db0SIan Campbell enum sunxi_gpio_number {
109fe1b4db0SIan Campbell SUNXI_GPIO_A_START = 0,
110fe1b4db0SIan Campbell SUNXI_GPIO_B_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_A),
111fe1b4db0SIan Campbell SUNXI_GPIO_C_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_B),
112fe1b4db0SIan Campbell SUNXI_GPIO_D_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_C),
113fe1b4db0SIan Campbell SUNXI_GPIO_E_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_D),
114fe1b4db0SIan Campbell SUNXI_GPIO_F_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_E),
115fe1b4db0SIan Campbell SUNXI_GPIO_G_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_F),
116fe1b4db0SIan Campbell SUNXI_GPIO_H_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_G),
117fe1b4db0SIan Campbell SUNXI_GPIO_I_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_H),
118e373aad3SHans de Goede SUNXI_GPIO_L_START = 352,
119e373aad3SHans de Goede SUNXI_GPIO_M_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_L),
120d35488c7SHans de Goede SUNXI_GPIO_N_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_M),
1216c727e09SHans de Goede SUNXI_GPIO_AXP0_START = 1024,
122fe1b4db0SIan Campbell };
123fe1b4db0SIan Campbell
124fe1b4db0SIan Campbell /* SUNXI GPIO number definitions */
125fe1b4db0SIan Campbell #define SUNXI_GPA(_nr) (SUNXI_GPIO_A_START + (_nr))
126fe1b4db0SIan Campbell #define SUNXI_GPB(_nr) (SUNXI_GPIO_B_START + (_nr))
127fe1b4db0SIan Campbell #define SUNXI_GPC(_nr) (SUNXI_GPIO_C_START + (_nr))
128fe1b4db0SIan Campbell #define SUNXI_GPD(_nr) (SUNXI_GPIO_D_START + (_nr))
129fe1b4db0SIan Campbell #define SUNXI_GPE(_nr) (SUNXI_GPIO_E_START + (_nr))
130fe1b4db0SIan Campbell #define SUNXI_GPF(_nr) (SUNXI_GPIO_F_START + (_nr))
131fe1b4db0SIan Campbell #define SUNXI_GPG(_nr) (SUNXI_GPIO_G_START + (_nr))
132fe1b4db0SIan Campbell #define SUNXI_GPH(_nr) (SUNXI_GPIO_H_START + (_nr))
133fe1b4db0SIan Campbell #define SUNXI_GPI(_nr) (SUNXI_GPIO_I_START + (_nr))
134e373aad3SHans de Goede #define SUNXI_GPL(_nr) (SUNXI_GPIO_L_START + (_nr))
135e373aad3SHans de Goede #define SUNXI_GPM(_nr) (SUNXI_GPIO_M_START + (_nr))
136d35488c7SHans de Goede #define SUNXI_GPN(_nr) (SUNXI_GPIO_N_START + (_nr))
137fe1b4db0SIan Campbell
1386c727e09SHans de Goede #define SUNXI_GPAXP0(_nr) (SUNXI_GPIO_AXP0_START + (_nr))
1396c727e09SHans de Goede
140fe1b4db0SIan Campbell /* GPIO pin function config */
141fe1b4db0SIan Campbell #define SUNXI_GPIO_INPUT 0
142fe1b4db0SIan Campbell #define SUNXI_GPIO_OUTPUT 1
14319e99fb4SSiarhei Siamashka #define SUNXI_GPIO_DISABLE 7
144fe1b4db0SIan Campbell
145487b3277SPaul Kocialkowski #define SUNXI_GPA_EMAC 2
146487b3277SPaul Kocialkowski #define SUN6I_GPA_GMAC 2
147487b3277SPaul Kocialkowski #define SUN7I_GPA_GMAC 5
1488deacca9SPaul Kocialkowski #define SUN6I_GPA_SDC2 5
1498deacca9SPaul Kocialkowski #define SUN6I_GPA_SDC3 4
1501c27b7dcSJens Kuske #define SUN8I_H3_GPA_UART0 2
151fe1b4db0SIan Campbell
152421c98d7SHans de Goede #define SUN4I_GPB_PWM 2
1536c739c5dSPaul Kocialkowski #define SUN4I_GPB_TWI0 2
1546c739c5dSPaul Kocialkowski #define SUN4I_GPB_TWI1 2
1556c739c5dSPaul Kocialkowski #define SUN5I_GPB_TWI1 2
1566c739c5dSPaul Kocialkowski #define SUN4I_GPB_TWI2 2
1576c739c5dSPaul Kocialkowski #define SUN5I_GPB_TWI2 2
158487b3277SPaul Kocialkowski #define SUN4I_GPB_UART0 2
159487b3277SPaul Kocialkowski #define SUN5I_GPB_UART0 2
1605cd83b11SLaurent Itti #define SUN8I_GPB_UART2 2
161e506889cSChen-Yu Tsai #define SUN8I_A33_GPB_UART0 3
162d5a3357fSvishnupatekar #define SUN8I_A83T_GPB_UART0 2
163c199489fSIcenowy Zheng #define SUN8I_V3S_GPB_UART0 3
164d96ebc46SSiarhei Siamashka #define SUN50I_GPB_UART0 4
165fe1b4db0SIan Campbell
166ad008299SKarol Gugala #define SUNXI_GPC_NAND 2
16719e99fb4SSiarhei Siamashka #define SUNXI_GPC_SPI0 3
168487b3277SPaul Kocialkowski #define SUNXI_GPC_SDC2 3
1698deacca9SPaul Kocialkowski #define SUN6I_GPC_SDC3 4
17019e99fb4SSiarhei Siamashka #define SUN50I_GPC_SPI0 4
171fe1b4db0SIan Campbell
1728deacca9SPaul Kocialkowski #define SUN8I_GPD_SDC1 3
173487b3277SPaul Kocialkowski #define SUNXI_GPD_LCD0 2
174487b3277SPaul Kocialkowski #define SUNXI_GPD_LVDS0 3
1751c353aeaSVasily Khoruzhick #define SUNXI_GPD_PWM 2
176fe1b4db0SIan Campbell
1778deacca9SPaul Kocialkowski #define SUN5I_GPE_SDC2 3
1786c739c5dSPaul Kocialkowski #define SUN8I_GPE_TWI2 3
179*da1ae590SStefan Mavrodiev #define SUN50I_GPE_TWI2 3
1808deacca9SPaul Kocialkowski
181487b3277SPaul Kocialkowski #define SUNXI_GPF_SDC0 2
182487b3277SPaul Kocialkowski #define SUNXI_GPF_UART0 4
183487b3277SPaul Kocialkowski #define SUN8I_GPF_UART0 3
184fe1b4db0SIan Campbell
1858deacca9SPaul Kocialkowski #define SUN4I_GPG_SDC1 4
186487b3277SPaul Kocialkowski #define SUN5I_GPG_SDC1 2
1878deacca9SPaul Kocialkowski #define SUN6I_GPG_SDC1 2
1888deacca9SPaul Kocialkowski #define SUN8I_GPG_SDC1 2
1896c739c5dSPaul Kocialkowski #define SUN6I_GPG_TWI3 2
190487b3277SPaul Kocialkowski #define SUN5I_GPG_UART1 4
1912dae800fSHans de Goede
192421c98d7SHans de Goede #define SUN6I_GPH_PWM 2
193421c98d7SHans de Goede #define SUN8I_GPH_PWM 2
1948deacca9SPaul Kocialkowski #define SUN4I_GPH_SDC1 5
1956c739c5dSPaul Kocialkowski #define SUN6I_GPH_TWI0 2
1966c739c5dSPaul Kocialkowski #define SUN8I_GPH_TWI0 2
197*da1ae590SStefan Mavrodiev #define SUN50I_GPH_TWI0 2
1986c739c5dSPaul Kocialkowski #define SUN6I_GPH_TWI1 2
1996c739c5dSPaul Kocialkowski #define SUN8I_GPH_TWI1 2
200*da1ae590SStefan Mavrodiev #define SUN50I_GPH_TWI1 2
2016c739c5dSPaul Kocialkowski #define SUN6I_GPH_TWI2 2
202487b3277SPaul Kocialkowski #define SUN6I_GPH_UART0 2
2031871a8caSHans de Goede #define SUN9I_GPH_UART0 2
2047f51a402SIcenowy Zheng #define SUN50I_H6_GPH_UART0 2
205fe1b4db0SIan Campbell
2068deacca9SPaul Kocialkowski #define SUNXI_GPI_SDC3 2
2076c739c5dSPaul Kocialkowski #define SUN7I_GPI_TWI3 3
2086c739c5dSPaul Kocialkowski #define SUN7I_GPI_TWI4 3
209fe1b4db0SIan Campbell
210ce881076SHans de Goede #define SUN6I_GPL0_R_P2WI_SCK 3
211ce881076SHans de Goede #define SUN6I_GPL1_R_P2WI_SDA 3
2123b10e6ebSOliver Schinagl
213487b3277SPaul Kocialkowski #define SUN8I_GPL_R_RSB 2
2149d082687SJelle van der Waa #define SUN8I_H3_GPL_R_TWI 2
2159d082687SJelle van der Waa #define SUN8I_A23_GPL_R_TWI 3
216487b3277SPaul Kocialkowski #define SUN8I_GPL_R_UART 2
21731a4ac4dSVasily Khoruzhick #define SUN50I_GPL_R_TWI 2
218c757a50bSChen-Yu Tsai
219487b3277SPaul Kocialkowski #define SUN9I_GPN_R_RSB 3
220d35488c7SHans de Goede
221fe1b4db0SIan Campbell /* GPIO pin pull-up/down config */
222fe1b4db0SIan Campbell #define SUNXI_GPIO_PULL_DISABLE 0
223fe1b4db0SIan Campbell #define SUNXI_GPIO_PULL_UP 1
224fe1b4db0SIan Campbell #define SUNXI_GPIO_PULL_DOWN 2
225fe1b4db0SIan Campbell
226f7c7ab63SPaul Kocialkowski /* Virtual AXP0 GPIOs */
227f9b7a04bSHans de Goede #define SUNXI_GPIO_AXP0_PREFIX "AXP0-"
228f9b7a04bSHans de Goede #define SUNXI_GPIO_AXP0_VBUS_DETECT 4
229f9b7a04bSHans de Goede #define SUNXI_GPIO_AXP0_VBUS_ENABLE 5
230f9b7a04bSHans de Goede #define SUNXI_GPIO_AXP0_GPIO_COUNT 6
231f7c7ab63SPaul Kocialkowski
232bf38891aSSimon Glass void sunxi_gpio_set_cfgbank(struct sunxi_gpio *pio, int bank_offset, u32 val);
233bf38891aSSimon Glass void sunxi_gpio_set_cfgpin(u32 pin, u32 val);
234bf38891aSSimon Glass int sunxi_gpio_get_cfgbank(struct sunxi_gpio *pio, int bank_offset);
235fe1b4db0SIan Campbell int sunxi_gpio_get_cfgpin(u32 pin);
236fe1b4db0SIan Campbell int sunxi_gpio_set_drv(u32 pin, u32 val);
237fe1b4db0SIan Campbell int sunxi_gpio_set_pull(u32 pin, u32 val);
2388deacca9SPaul Kocialkowski int sunxi_name_to_gpio_bank(const char *name);
239abce2c62SIan Campbell int sunxi_name_to_gpio(const char *name);
240abce2c62SIan Campbell #define name_to_gpio(name) sunxi_name_to_gpio(name)
241fe1b4db0SIan Campbell
2422fcf033dSHans de Goede #if !defined CONFIG_SPL_BUILD && defined CONFIG_AXP_GPIO
2432fcf033dSHans de Goede int axp_gpio_init(void);
2442fcf033dSHans de Goede #else
axp_gpio_init(void)2452fcf033dSHans de Goede static inline int axp_gpio_init(void) { return 0; }
2462fcf033dSHans de Goede #endif
2472fcf033dSHans de Goede
248fe1b4db0SIan Campbell #endif /* _SUNXI_GPIO_H */
249