1fe1b4db0SIan Campbell /* 2fe1b4db0SIan Campbell * (C) Copyright 2007-2012 3fe1b4db0SIan Campbell * Allwinner Technology Co., Ltd. <www.allwinnertech.com> 4fe1b4db0SIan Campbell * Tom Cubie <tangliang@allwinnertech.com> 5fe1b4db0SIan Campbell * 6fe1b4db0SIan Campbell * SPDX-License-Identifier: GPL-2.0+ 7fe1b4db0SIan Campbell */ 8fe1b4db0SIan Campbell 9fe1b4db0SIan Campbell #ifndef _SUNXI_GPIO_H 10fe1b4db0SIan Campbell #define _SUNXI_GPIO_H 11fe1b4db0SIan Campbell 12fe1b4db0SIan Campbell #include <linux/types.h> 13e373aad3SHans de Goede #include <asm/arch/cpu.h> 14fe1b4db0SIan Campbell 15fe1b4db0SIan Campbell /* 16fe1b4db0SIan Campbell * sunxi has 9 banks of gpio, they are: 17fe1b4db0SIan Campbell * PA0 - PA17 | PB0 - PB23 | PC0 - PC24 18fe1b4db0SIan Campbell * PD0 - PD27 | PE0 - PE31 | PF0 - PF5 19fe1b4db0SIan Campbell * PG0 - PG9 | PH0 - PH27 | PI0 - PI12 20fe1b4db0SIan Campbell */ 21fe1b4db0SIan Campbell 22fe1b4db0SIan Campbell #define SUNXI_GPIO_A 0 23fe1b4db0SIan Campbell #define SUNXI_GPIO_B 1 24fe1b4db0SIan Campbell #define SUNXI_GPIO_C 2 25fe1b4db0SIan Campbell #define SUNXI_GPIO_D 3 26fe1b4db0SIan Campbell #define SUNXI_GPIO_E 4 27fe1b4db0SIan Campbell #define SUNXI_GPIO_F 5 28fe1b4db0SIan Campbell #define SUNXI_GPIO_G 6 29fe1b4db0SIan Campbell #define SUNXI_GPIO_H 7 30fe1b4db0SIan Campbell #define SUNXI_GPIO_I 8 31e373aad3SHans de Goede 32e373aad3SHans de Goede /* 33e373aad3SHans de Goede * This defines the number of GPIO banks for the _main_ GPIO controller. 34e373aad3SHans de Goede * You should fix up the padding in struct sunxi_gpio_reg below if you 35e373aad3SHans de Goede * change this. 36e373aad3SHans de Goede */ 37fe1b4db0SIan Campbell #define SUNXI_GPIO_BANKS 9 38fe1b4db0SIan Campbell 39e373aad3SHans de Goede /* 40e373aad3SHans de Goede * sun6i/sun8i and later SoCs have an additional GPIO controller (R_PIO) 41e373aad3SHans de Goede * at a different register offset. 42e373aad3SHans de Goede * 43e373aad3SHans de Goede * sun6i has 2 banks: 44e373aad3SHans de Goede * PL0 - PL8 | PM0 - PM7 45e373aad3SHans de Goede * 46e373aad3SHans de Goede * sun8i has 1 bank: 47e373aad3SHans de Goede * PL0 - PL11 48e373aad3SHans de Goede */ 49e373aad3SHans de Goede #define SUNXI_GPIO_L 11 50e373aad3SHans de Goede #define SUNXI_GPIO_M 12 51e373aad3SHans de Goede 52fe1b4db0SIan Campbell struct sunxi_gpio { 53fe1b4db0SIan Campbell u32 cfg[4]; 54fe1b4db0SIan Campbell u32 dat; 55fe1b4db0SIan Campbell u32 drv[2]; 56fe1b4db0SIan Campbell u32 pull[2]; 57fe1b4db0SIan Campbell }; 58fe1b4db0SIan Campbell 59fe1b4db0SIan Campbell /* gpio interrupt control */ 60fe1b4db0SIan Campbell struct sunxi_gpio_int { 61fe1b4db0SIan Campbell u32 cfg[3]; 62fe1b4db0SIan Campbell u32 ctl; 63fe1b4db0SIan Campbell u32 sta; 64fe1b4db0SIan Campbell u32 deb; /* interrupt debounce */ 65fe1b4db0SIan Campbell }; 66fe1b4db0SIan Campbell 67fe1b4db0SIan Campbell struct sunxi_gpio_reg { 68fe1b4db0SIan Campbell struct sunxi_gpio gpio_bank[SUNXI_GPIO_BANKS]; 69fe1b4db0SIan Campbell u8 res[0xbc]; 70fe1b4db0SIan Campbell struct sunxi_gpio_int gpio_int; 71fe1b4db0SIan Campbell }; 72fe1b4db0SIan Campbell 73e373aad3SHans de Goede #define BANK_TO_GPIO(bank) (((bank) < SUNXI_GPIO_L) ? \ 74e373aad3SHans de Goede &((struct sunxi_gpio_reg *)SUNXI_PIO_BASE)->gpio_bank[bank] : \ 75e373aad3SHans de Goede &((struct sunxi_gpio_reg *)SUNXI_R_PIO_BASE)->gpio_bank[(bank) - SUNXI_GPIO_L]) 76fe1b4db0SIan Campbell 77fe1b4db0SIan Campbell #define GPIO_BANK(pin) ((pin) >> 5) 78fe1b4db0SIan Campbell #define GPIO_NUM(pin) ((pin) & 0x1f) 79fe1b4db0SIan Campbell 80fe1b4db0SIan Campbell #define GPIO_CFG_INDEX(pin) (((pin) & 0x1f) >> 3) 81fe1b4db0SIan Campbell #define GPIO_CFG_OFFSET(pin) ((((pin) & 0x1f) & 0x7) << 2) 82fe1b4db0SIan Campbell 83fe1b4db0SIan Campbell #define GPIO_DRV_INDEX(pin) (((pin) & 0x1f) >> 4) 84fe1b4db0SIan Campbell #define GPIO_DRV_OFFSET(pin) ((((pin) & 0x1f) & 0xf) << 1) 85fe1b4db0SIan Campbell 86fe1b4db0SIan Campbell #define GPIO_PULL_INDEX(pin) (((pin) & 0x1f) >> 4) 87fe1b4db0SIan Campbell #define GPIO_PULL_OFFSET(pin) ((((pin) & 0x1f) & 0xf) << 1) 88fe1b4db0SIan Campbell 89fe1b4db0SIan Campbell /* GPIO bank sizes */ 90fe1b4db0SIan Campbell #define SUNXI_GPIO_A_NR 32 91fe1b4db0SIan Campbell #define SUNXI_GPIO_B_NR 32 92fe1b4db0SIan Campbell #define SUNXI_GPIO_C_NR 32 93fe1b4db0SIan Campbell #define SUNXI_GPIO_D_NR 32 94fe1b4db0SIan Campbell #define SUNXI_GPIO_E_NR 32 95fe1b4db0SIan Campbell #define SUNXI_GPIO_F_NR 32 96fe1b4db0SIan Campbell #define SUNXI_GPIO_G_NR 32 97fe1b4db0SIan Campbell #define SUNXI_GPIO_H_NR 32 98fe1b4db0SIan Campbell #define SUNXI_GPIO_I_NR 32 99e373aad3SHans de Goede #define SUNXI_GPIO_L_NR 32 100e373aad3SHans de Goede #define SUNXI_GPIO_M_NR 32 101fe1b4db0SIan Campbell 102fe1b4db0SIan Campbell #define SUNXI_GPIO_NEXT(__gpio) \ 103fe1b4db0SIan Campbell ((__gpio##_START) + (__gpio##_NR) + 0) 104fe1b4db0SIan Campbell 105fe1b4db0SIan Campbell enum sunxi_gpio_number { 106fe1b4db0SIan Campbell SUNXI_GPIO_A_START = 0, 107fe1b4db0SIan Campbell SUNXI_GPIO_B_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_A), 108fe1b4db0SIan Campbell SUNXI_GPIO_C_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_B), 109fe1b4db0SIan Campbell SUNXI_GPIO_D_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_C), 110fe1b4db0SIan Campbell SUNXI_GPIO_E_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_D), 111fe1b4db0SIan Campbell SUNXI_GPIO_F_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_E), 112fe1b4db0SIan Campbell SUNXI_GPIO_G_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_F), 113fe1b4db0SIan Campbell SUNXI_GPIO_H_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_G), 114fe1b4db0SIan Campbell SUNXI_GPIO_I_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_H), 115e373aad3SHans de Goede SUNXI_GPIO_L_START = 352, 116e373aad3SHans de Goede SUNXI_GPIO_M_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_L), 117fe1b4db0SIan Campbell }; 118fe1b4db0SIan Campbell 119fe1b4db0SIan Campbell /* SUNXI GPIO number definitions */ 120fe1b4db0SIan Campbell #define SUNXI_GPA(_nr) (SUNXI_GPIO_A_START + (_nr)) 121fe1b4db0SIan Campbell #define SUNXI_GPB(_nr) (SUNXI_GPIO_B_START + (_nr)) 122fe1b4db0SIan Campbell #define SUNXI_GPC(_nr) (SUNXI_GPIO_C_START + (_nr)) 123fe1b4db0SIan Campbell #define SUNXI_GPD(_nr) (SUNXI_GPIO_D_START + (_nr)) 124fe1b4db0SIan Campbell #define SUNXI_GPE(_nr) (SUNXI_GPIO_E_START + (_nr)) 125fe1b4db0SIan Campbell #define SUNXI_GPF(_nr) (SUNXI_GPIO_F_START + (_nr)) 126fe1b4db0SIan Campbell #define SUNXI_GPG(_nr) (SUNXI_GPIO_G_START + (_nr)) 127fe1b4db0SIan Campbell #define SUNXI_GPH(_nr) (SUNXI_GPIO_H_START + (_nr)) 128fe1b4db0SIan Campbell #define SUNXI_GPI(_nr) (SUNXI_GPIO_I_START + (_nr)) 129e373aad3SHans de Goede #define SUNXI_GPL(_nr) (SUNXI_GPIO_L_START + (_nr)) 130e373aad3SHans de Goede #define SUNXI_GPM(_nr) (SUNXI_GPIO_M_START + (_nr)) 131fe1b4db0SIan Campbell 132fe1b4db0SIan Campbell /* GPIO pin function config */ 133fe1b4db0SIan Campbell #define SUNXI_GPIO_INPUT 0 134fe1b4db0SIan Campbell #define SUNXI_GPIO_OUTPUT 1 135fe1b4db0SIan Campbell 136fe1b4db0SIan Campbell #define SUNXI_GPA0_EMAC 2 137fe1b4db0SIan Campbell #define SUN7I_GPA0_GMAC 5 138fe1b4db0SIan Campbell 139fe1b4db0SIan Campbell #define SUNXI_GPB0_TWI0 2 140fe1b4db0SIan Campbell 141fe1b4db0SIan Campbell #define SUN4I_GPB22_UART0_TX 2 142fe1b4db0SIan Campbell #define SUN4I_GPB23_UART0_RX 2 143fe1b4db0SIan Campbell 144fe1b4db0SIan Campbell #define SUN5I_GPB19_UART0_TX 2 145fe1b4db0SIan Campbell #define SUN5I_GPB20_UART0_RX 2 146fe1b4db0SIan Campbell 147bbff84b3SHans de Goede #define SUN5I_GPG3_SDC1 2 148bbff84b3SHans de Goede 149fe1b4db0SIan Campbell #define SUN5I_GPG3_UART1_TX 4 150fe1b4db0SIan Campbell #define SUN5I_GPG4_UART1_RX 4 151fe1b4db0SIan Campbell 152fe1b4db0SIan Campbell #define SUNXI_GPC6_SDC2 3 153fe1b4db0SIan Campbell 154fe1b4db0SIan Campbell #define SUNXI_GPF0_SDC0 2 155fe1b4db0SIan Campbell 156fe1b4db0SIan Campbell #define SUNXI_GPF2_SDC0 2 1577f87ad35SChen-Yu Tsai 158ed41e62fSIan Campbell #ifdef CONFIG_MACH_SUN8I 1597f87ad35SChen-Yu Tsai #define SUNXI_GPF2_UART0_TX 3 1607f87ad35SChen-Yu Tsai #define SUNXI_GPF4_UART0_RX 3 1617f87ad35SChen-Yu Tsai #else 162fe1b4db0SIan Campbell #define SUNXI_GPF2_UART0_TX 4 163fe1b4db0SIan Campbell #define SUNXI_GPF4_UART0_RX 4 1647f87ad35SChen-Yu Tsai #endif 165fe1b4db0SIan Campbell 166fe1b4db0SIan Campbell #define SUN4I_GPG0_SDC1 4 167fe1b4db0SIan Campbell 168fe1b4db0SIan Campbell #define SUN4I_GPH22_SDC1 5 169fe1b4db0SIan Campbell 170ba1e40fdSChen-Yu Tsai #define SUN6I_GPH20_UART0_TX 2 171ba1e40fdSChen-Yu Tsai #define SUN6I_GPH21_UART0_RX 2 172ba1e40fdSChen-Yu Tsai 173fe1b4db0SIan Campbell #define SUN4I_GPI4_SDC3 2 174fe1b4db0SIan Campbell 175*3b10e6ebSOliver Schinagl #define SUNXI_GPL0_R_P2WI_SCK 3 176*3b10e6ebSOliver Schinagl #define SUNXI_GPL1_R_P2WI_SDA 3 177*3b10e6ebSOliver Schinagl 178c757a50bSChen-Yu Tsai #define SUN8I_GPL2_R_UART_TX 2 179c757a50bSChen-Yu Tsai #define SUN8I_GPL3_R_UART_RX 2 180c757a50bSChen-Yu Tsai 181fe1b4db0SIan Campbell /* GPIO pin pull-up/down config */ 182fe1b4db0SIan Campbell #define SUNXI_GPIO_PULL_DISABLE 0 183fe1b4db0SIan Campbell #define SUNXI_GPIO_PULL_UP 1 184fe1b4db0SIan Campbell #define SUNXI_GPIO_PULL_DOWN 2 185fe1b4db0SIan Campbell 186bf38891aSSimon Glass void sunxi_gpio_set_cfgbank(struct sunxi_gpio *pio, int bank_offset, u32 val); 187bf38891aSSimon Glass void sunxi_gpio_set_cfgpin(u32 pin, u32 val); 188bf38891aSSimon Glass int sunxi_gpio_get_cfgbank(struct sunxi_gpio *pio, int bank_offset); 189fe1b4db0SIan Campbell int sunxi_gpio_get_cfgpin(u32 pin); 190fe1b4db0SIan Campbell int sunxi_gpio_set_drv(u32 pin, u32 val); 191fe1b4db0SIan Campbell int sunxi_gpio_set_pull(u32 pin, u32 val); 192abce2c62SIan Campbell int sunxi_name_to_gpio(const char *name); 193abce2c62SIan Campbell #define name_to_gpio(name) sunxi_name_to_gpio(name) 194fe1b4db0SIan Campbell 195fe1b4db0SIan Campbell #endif /* _SUNXI_GPIO_H */ 196