blob: 106605adf5e2cdfc84f1e8770053f219844e0d8d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Ian Campbellb2765ec2014-05-05 11:52:24 +01002/*
3 * (C) Copyright 2007-2012
4 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
5 * Tom Cubie <tangliang@allwinnertech.com>
Ian Campbellb2765ec2014-05-05 11:52:24 +01006 */
7
8#ifndef _SUNXI_GPIO_H
9#define _SUNXI_GPIO_H
10
11#include <linux/types.h>
Hans de Goede0ee72682014-10-22 16:47:45 +080012#include <asm/arch/cpu.h>
Ian Campbellb2765ec2014-05-05 11:52:24 +010013
14/*
15 * sunxi has 9 banks of gpio, they are:
16 * PA0 - PA17 | PB0 - PB23 | PC0 - PC24
17 * PD0 - PD27 | PE0 - PE31 | PF0 - PF5
18 * PG0 - PG9 | PH0 - PH27 | PI0 - PI12
19 */
20
21#define SUNXI_GPIO_A 0
22#define SUNXI_GPIO_B 1
23#define SUNXI_GPIO_C 2
24#define SUNXI_GPIO_D 3
25#define SUNXI_GPIO_E 4
26#define SUNXI_GPIO_F 5
27#define SUNXI_GPIO_G 6
28#define SUNXI_GPIO_H 7
29#define SUNXI_GPIO_I 8
Hans de Goede0ee72682014-10-22 16:47:45 +080030
31/*
32 * This defines the number of GPIO banks for the _main_ GPIO controller.
33 * You should fix up the padding in struct sunxi_gpio_reg below if you
34 * change this.
35 */
Ian Campbellb2765ec2014-05-05 11:52:24 +010036#define SUNXI_GPIO_BANKS 9
37
Hans de Goede0ee72682014-10-22 16:47:45 +080038/*
39 * sun6i/sun8i and later SoCs have an additional GPIO controller (R_PIO)
40 * at a different register offset.
41 *
42 * sun6i has 2 banks:
43 * PL0 - PL8 | PM0 - PM7
44 *
45 * sun8i has 1 bank:
46 * PL0 - PL11
Hans de Goede8760c912015-01-26 16:46:43 +010047 *
48 * sun9i has 3 banks:
49 * PL0 - PL9 | PM0 - PM15 | PN0 - PN1
Hans de Goede0ee72682014-10-22 16:47:45 +080050 */
51#define SUNXI_GPIO_L 11
52#define SUNXI_GPIO_M 12
Hans de Goede8760c912015-01-26 16:46:43 +010053#define SUNXI_GPIO_N 13
Hans de Goede0ee72682014-10-22 16:47:45 +080054
Ian Campbellb2765ec2014-05-05 11:52:24 +010055struct sunxi_gpio {
56 u32 cfg[4];
57 u32 dat;
58 u32 drv[2];
59 u32 pull[2];
60};
61
62/* gpio interrupt control */
63struct sunxi_gpio_int {
64 u32 cfg[3];
65 u32 ctl;
66 u32 sta;
67 u32 deb; /* interrupt debounce */
68};
69
70struct sunxi_gpio_reg {
71 struct sunxi_gpio gpio_bank[SUNXI_GPIO_BANKS];
72 u8 res[0xbc];
73 struct sunxi_gpio_int gpio_int;
74};
75
Icenowy Zheng112c8862019-04-24 13:44:12 +080076#define SUN50I_H6_GPIO_POW_MOD_SEL 0x340
77#define SUN50I_H6_GPIO_POW_MOD_VAL 0x348
78
Hans de Goede0ee72682014-10-22 16:47:45 +080079#define BANK_TO_GPIO(bank) (((bank) < SUNXI_GPIO_L) ? \
80 &((struct sunxi_gpio_reg *)SUNXI_PIO_BASE)->gpio_bank[bank] : \
81 &((struct sunxi_gpio_reg *)SUNXI_R_PIO_BASE)->gpio_bank[(bank) - SUNXI_GPIO_L])
Ian Campbellb2765ec2014-05-05 11:52:24 +010082
83#define GPIO_BANK(pin) ((pin) >> 5)
84#define GPIO_NUM(pin) ((pin) & 0x1f)
85
86#define GPIO_CFG_INDEX(pin) (((pin) & 0x1f) >> 3)
87#define GPIO_CFG_OFFSET(pin) ((((pin) & 0x1f) & 0x7) << 2)
88
Paul Kocialkowskic97e1682015-03-22 18:07:08 +010089#define GPIO_DRV_INDEX(pin) (((pin) & 0x1f) >> 4)
Ian Campbellb2765ec2014-05-05 11:52:24 +010090#define GPIO_DRV_OFFSET(pin) ((((pin) & 0x1f) & 0xf) << 1)
91
92#define GPIO_PULL_INDEX(pin) (((pin) & 0x1f) >> 4)
93#define GPIO_PULL_OFFSET(pin) ((((pin) & 0x1f) & 0xf) << 1)
94
95/* GPIO bank sizes */
Samuel Hollande93ea432021-09-11 16:50:49 -050096#define SUNXI_GPIOS_PER_BANK 32
Ian Campbellb2765ec2014-05-05 11:52:24 +010097
98#define SUNXI_GPIO_NEXT(__gpio) \
Samuel Hollande93ea432021-09-11 16:50:49 -050099 ((__gpio##_START) + SUNXI_GPIOS_PER_BANK)
Ian Campbellb2765ec2014-05-05 11:52:24 +0100100
101enum sunxi_gpio_number {
102 SUNXI_GPIO_A_START = 0,
103 SUNXI_GPIO_B_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_A),
104 SUNXI_GPIO_C_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_B),
105 SUNXI_GPIO_D_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_C),
106 SUNXI_GPIO_E_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_D),
107 SUNXI_GPIO_F_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_E),
108 SUNXI_GPIO_G_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_F),
109 SUNXI_GPIO_H_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_G),
110 SUNXI_GPIO_I_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_H),
Hans de Goede0ee72682014-10-22 16:47:45 +0800111 SUNXI_GPIO_L_START = 352,
112 SUNXI_GPIO_M_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_L),
Hans de Goede8760c912015-01-26 16:46:43 +0100113 SUNXI_GPIO_N_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_M),
Hans de Goede1fc9c4a2014-12-24 19:34:38 +0100114 SUNXI_GPIO_AXP0_START = 1024,
Ian Campbellb2765ec2014-05-05 11:52:24 +0100115};
116
117/* SUNXI GPIO number definitions */
118#define SUNXI_GPA(_nr) (SUNXI_GPIO_A_START + (_nr))
119#define SUNXI_GPB(_nr) (SUNXI_GPIO_B_START + (_nr))
120#define SUNXI_GPC(_nr) (SUNXI_GPIO_C_START + (_nr))
121#define SUNXI_GPD(_nr) (SUNXI_GPIO_D_START + (_nr))
122#define SUNXI_GPE(_nr) (SUNXI_GPIO_E_START + (_nr))
123#define SUNXI_GPF(_nr) (SUNXI_GPIO_F_START + (_nr))
124#define SUNXI_GPG(_nr) (SUNXI_GPIO_G_START + (_nr))
125#define SUNXI_GPH(_nr) (SUNXI_GPIO_H_START + (_nr))
126#define SUNXI_GPI(_nr) (SUNXI_GPIO_I_START + (_nr))
Hans de Goede0ee72682014-10-22 16:47:45 +0800127#define SUNXI_GPL(_nr) (SUNXI_GPIO_L_START + (_nr))
128#define SUNXI_GPM(_nr) (SUNXI_GPIO_M_START + (_nr))
Hans de Goede8760c912015-01-26 16:46:43 +0100129#define SUNXI_GPN(_nr) (SUNXI_GPIO_N_START + (_nr))
Ian Campbellb2765ec2014-05-05 11:52:24 +0100130
Hans de Goede1fc9c4a2014-12-24 19:34:38 +0100131#define SUNXI_GPAXP0(_nr) (SUNXI_GPIO_AXP0_START + (_nr))
132
Ian Campbellb2765ec2014-05-05 11:52:24 +0100133/* GPIO pin function config */
134#define SUNXI_GPIO_INPUT 0
135#define SUNXI_GPIO_OUTPUT 1
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300136#define SUNXI_GPIO_DISABLE 7
Ian Campbellb2765ec2014-05-05 11:52:24 +0100137
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100138#define SUNXI_GPA_EMAC 2
139#define SUN6I_GPA_GMAC 2
140#define SUN7I_GPA_GMAC 5
Jens Kuskef9770722015-11-17 15:12:58 +0100141#define SUN8I_H3_GPA_UART0 2
Ian Campbellb2765ec2014-05-05 11:52:24 +0100142
Hans de Goede663ae652016-08-19 15:25:41 +0200143#define SUN4I_GPB_PWM 2
Paul Kocialkowski0a3ec0a2015-04-10 23:09:52 +0200144#define SUN4I_GPB_TWI0 2
145#define SUN4I_GPB_TWI1 2
146#define SUN5I_GPB_TWI1 2
147#define SUN4I_GPB_TWI2 2
148#define SUN5I_GPB_TWI2 2
Icenowy Zheng365951a2020-10-26 22:19:34 +0800149#define SUN8I_V3S_GPB_TWI0 2
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100150#define SUN4I_GPB_UART0 2
151#define SUN5I_GPB_UART0 2
Laurent Itti20dfe002015-05-05 17:02:00 -0700152#define SUN8I_GPB_UART2 2
Chen-Yu Tsai28b71922015-06-23 19:57:25 +0800153#define SUN8I_A33_GPB_UART0 3
vishnupatekar133bfbe2015-11-29 01:07:20 +0800154#define SUN8I_A83T_GPB_UART0 2
Icenowy Zheng52e61882017-04-08 15:30:12 +0800155#define SUN8I_V3S_GPB_UART0 3
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200156#define SUN50I_GPB_UART0 4
Ian Campbellb2765ec2014-05-05 11:52:24 +0100157
Karol Gugala7bea8932015-07-23 14:33:01 +0200158#define SUNXI_GPC_NAND 2
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300159#define SUNXI_GPC_SPI0 3
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100160#define SUNXI_GPC_SDC2 3
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100161#define SUN6I_GPC_SDC3 4
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300162#define SUN50I_GPC_SPI0 4
Ian Campbellb2765ec2014-05-05 11:52:24 +0100163
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100164#define SUNXI_GPD_LCD0 2
165#define SUNXI_GPD_LVDS0 3
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -0700166#define SUNXI_GPD_PWM 2
Ian Campbellb2765ec2014-05-05 11:52:24 +0100167
Paul Kocialkowski0a3ec0a2015-04-10 23:09:52 +0200168#define SUN8I_GPE_TWI2 3
Stefan Mavrodievcabe9922019-01-08 12:04:30 +0200169#define SUN50I_GPE_TWI2 3
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100170
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100171#define SUNXI_GPF_SDC0 2
172#define SUNXI_GPF_UART0 4
173#define SUN8I_GPF_UART0 3
Ian Campbellb2765ec2014-05-05 11:52:24 +0100174
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100175#define SUN4I_GPG_SDC1 4
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100176#define SUN5I_GPG_SDC1 2
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100177#define SUN6I_GPG_SDC1 2
178#define SUN8I_GPG_SDC1 2
Tobias Schramm6892a562021-02-15 00:19:58 +0100179#define SUN8I_GPG_UART1 2
Paul Kocialkowski0a3ec0a2015-04-10 23:09:52 +0200180#define SUN6I_GPG_TWI3 2
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100181#define SUN5I_GPG_UART1 4
Hans de Goede7e68a1b2014-12-21 16:28:32 +0100182
Hans de Goede663ae652016-08-19 15:25:41 +0200183#define SUN6I_GPH_PWM 2
184#define SUN8I_GPH_PWM 2
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100185#define SUN4I_GPH_SDC1 5
Paul Kocialkowski0a3ec0a2015-04-10 23:09:52 +0200186#define SUN6I_GPH_TWI0 2
187#define SUN8I_GPH_TWI0 2
Stefan Mavrodievcabe9922019-01-08 12:04:30 +0200188#define SUN50I_GPH_TWI0 2
Paul Kocialkowski0a3ec0a2015-04-10 23:09:52 +0200189#define SUN6I_GPH_TWI1 2
190#define SUN8I_GPH_TWI1 2
Stefan Mavrodievcabe9922019-01-08 12:04:30 +0200191#define SUN50I_GPH_TWI1 2
Paul Kocialkowski0a3ec0a2015-04-10 23:09:52 +0200192#define SUN6I_GPH_TWI2 2
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100193#define SUN6I_GPH_UART0 2
Hans de Goede7bfe2bb2015-01-13 19:25:06 +0100194#define SUN9I_GPH_UART0 2
Icenowy Zhenga78bb072018-07-21 16:20:28 +0800195#define SUN50I_H6_GPH_UART0 2
Jernej Skrabec30efb9d2021-01-11 21:11:41 +0100196#define SUN50I_H616_GPH_UART0 2
Ian Campbellb2765ec2014-05-05 11:52:24 +0100197
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100198#define SUNXI_GPI_SDC3 2
Paul Kocialkowski0a3ec0a2015-04-10 23:09:52 +0200199#define SUN7I_GPI_TWI3 3
200#define SUN7I_GPI_TWI4 3
Ian Campbellb2765ec2014-05-05 11:52:24 +0100201
Hans de Goede7c590382014-12-13 10:25:14 +0100202#define SUN6I_GPL0_R_P2WI_SCK 3
203#define SUN6I_GPL1_R_P2WI_SDA 3
Oliver Schinagl4f9a0082013-07-25 14:07:42 +0200204
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100205#define SUN8I_GPL_R_RSB 2
Jelle van der Waa8d3d7c12016-01-14 14:06:26 +0100206#define SUN8I_H3_GPL_R_TWI 2
207#define SUN8I_A23_GPL_R_TWI 3
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100208#define SUN8I_GPL_R_UART 2
Vasily Khoruzhick6f4c3442018-11-05 20:24:30 -0800209#define SUN50I_GPL_R_TWI 2
Jernej Skrabec7de8eb02021-01-11 21:11:42 +0100210#define SUN50I_H616_GPL_R_TWI 3
Chen-Yu Tsai6ee63882014-10-22 16:47:47 +0800211
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100212#define SUN9I_GPN_R_RSB 3
Hans de Goede8760c912015-01-26 16:46:43 +0100213
Ian Campbellb2765ec2014-05-05 11:52:24 +0100214/* GPIO pin pull-up/down config */
215#define SUNXI_GPIO_PULL_DISABLE 0
216#define SUNXI_GPIO_PULL_UP 1
217#define SUNXI_GPIO_PULL_DOWN 2
218
Paul Kocialkowski6604a132015-03-22 18:07:09 +0100219/* Virtual AXP0 GPIOs */
Hans de Goede08607d12015-04-22 11:31:22 +0200220#define SUNXI_GPIO_AXP0_PREFIX "AXP0-"
221#define SUNXI_GPIO_AXP0_VBUS_DETECT 4
222#define SUNXI_GPIO_AXP0_VBUS_ENABLE 5
223#define SUNXI_GPIO_AXP0_GPIO_COUNT 6
Paul Kocialkowski6604a132015-03-22 18:07:09 +0100224
Simon Glassd8624532014-10-30 20:25:47 -0600225void sunxi_gpio_set_cfgbank(struct sunxi_gpio *pio, int bank_offset, u32 val);
226void sunxi_gpio_set_cfgpin(u32 pin, u32 val);
227int sunxi_gpio_get_cfgbank(struct sunxi_gpio *pio, int bank_offset);
Ian Campbellb2765ec2014-05-05 11:52:24 +0100228int sunxi_gpio_get_cfgpin(u32 pin);
Samuel Holland41abadc2021-10-20 23:52:54 -0500229void sunxi_gpio_set_drv(u32 pin, u32 val);
Samuel Holland6dde4a22021-10-20 23:52:55 -0500230void sunxi_gpio_set_drv_bank(struct sunxi_gpio *pio, u32 bank_offset, u32 val);
Samuel Holland41abadc2021-10-20 23:52:54 -0500231void sunxi_gpio_set_pull(u32 pin, u32 val);
Samuel Holland6dde4a22021-10-20 23:52:55 -0500232void sunxi_gpio_set_pull_bank(struct sunxi_gpio *pio, int bank_offset, u32 val);
Ian Campbellaf471472014-06-05 19:00:15 +0100233int sunxi_name_to_gpio(const char *name);
Ian Campbellb2765ec2014-05-05 11:52:24 +0100234
Hans de Goede3ae1d132015-04-25 17:25:14 +0200235#if !defined CONFIG_SPL_BUILD && defined CONFIG_AXP_GPIO
236int axp_gpio_init(void);
237#else
238static inline int axp_gpio_init(void) { return 0; }
239#endif
240
Ian Campbellb2765ec2014-05-05 11:52:24 +0100241#endif /* _SUNXI_GPIO_H */