1 /*
2  * (C) Copyright 2007-2012
3  * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
4  * Tom Cubie <tangliang@allwinnertech.com>
5  *
6  * SPDX-License-Identifier:	GPL-2.0+
7  */
8 
9 #ifndef _SUNXI_GPIO_H
10 #define _SUNXI_GPIO_H
11 
12 #include <linux/types.h>
13 
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
30 #define SUNXI_GPIO_BANKS 9
31 
32 struct sunxi_gpio {
33 	u32 cfg[4];
34 	u32 dat;
35 	u32 drv[2];
36 	u32 pull[2];
37 };
38 
39 /* gpio interrupt control */
40 struct sunxi_gpio_int {
41 	u32 cfg[3];
42 	u32 ctl;
43 	u32 sta;
44 	u32 deb;		/* interrupt debounce */
45 };
46 
47 struct sunxi_gpio_reg {
48 	struct sunxi_gpio gpio_bank[SUNXI_GPIO_BANKS];
49 	u8 res[0xbc];
50 	struct sunxi_gpio_int gpio_int;
51 };
52 
53 #define BANK_TO_GPIO(bank) \
54 	&((struct sunxi_gpio_reg *)SUNXI_PIO_BASE)->gpio_bank[bank]
55 
56 #define GPIO_BANK(pin)		((pin) >> 5)
57 #define GPIO_NUM(pin)		((pin) & 0x1f)
58 
59 #define GPIO_CFG_INDEX(pin)	(((pin) & 0x1f) >> 3)
60 #define GPIO_CFG_OFFSET(pin)	((((pin) & 0x1f) & 0x7) << 2)
61 
62 #define GPIO_DRV_INDEX(pin)   (((pin) & 0x1f) >> 4)
63 #define GPIO_DRV_OFFSET(pin)	((((pin) & 0x1f) & 0xf) << 1)
64 
65 #define GPIO_PULL_INDEX(pin)	(((pin) & 0x1f) >> 4)
66 #define GPIO_PULL_OFFSET(pin)	((((pin) & 0x1f) & 0xf) << 1)
67 
68 /* GPIO bank sizes */
69 #define SUNXI_GPIO_A_NR		32
70 #define SUNXI_GPIO_B_NR		32
71 #define SUNXI_GPIO_C_NR		32
72 #define SUNXI_GPIO_D_NR		32
73 #define SUNXI_GPIO_E_NR		32
74 #define SUNXI_GPIO_F_NR		32
75 #define SUNXI_GPIO_G_NR		32
76 #define SUNXI_GPIO_H_NR		32
77 #define SUNXI_GPIO_I_NR		32
78 
79 #define SUNXI_GPIO_NEXT(__gpio) \
80 	((__gpio##_START) + (__gpio##_NR) + 0)
81 
82 enum sunxi_gpio_number {
83 	SUNXI_GPIO_A_START = 0,
84 	SUNXI_GPIO_B_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_A),
85 	SUNXI_GPIO_C_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_B),
86 	SUNXI_GPIO_D_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_C),
87 	SUNXI_GPIO_E_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_D),
88 	SUNXI_GPIO_F_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_E),
89 	SUNXI_GPIO_G_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_F),
90 	SUNXI_GPIO_H_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_G),
91 	SUNXI_GPIO_I_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_H),
92 };
93 
94 /* SUNXI GPIO number definitions */
95 #define SUNXI_GPA(_nr)	(SUNXI_GPIO_A_START + (_nr))
96 #define SUNXI_GPB(_nr)	(SUNXI_GPIO_B_START + (_nr))
97 #define SUNXI_GPC(_nr)	(SUNXI_GPIO_C_START + (_nr))
98 #define SUNXI_GPD(_nr)	(SUNXI_GPIO_D_START + (_nr))
99 #define SUNXI_GPE(_nr)	(SUNXI_GPIO_E_START + (_nr))
100 #define SUNXI_GPF(_nr)	(SUNXI_GPIO_F_START + (_nr))
101 #define SUNXI_GPG(_nr)	(SUNXI_GPIO_G_START + (_nr))
102 #define SUNXI_GPH(_nr)	(SUNXI_GPIO_H_START + (_nr))
103 #define SUNXI_GPI(_nr)	(SUNXI_GPIO_I_START + (_nr))
104 
105 /* GPIO pin function config */
106 #define SUNXI_GPIO_INPUT	0
107 #define SUNXI_GPIO_OUTPUT	1
108 
109 #define SUNXI_GPA0_EMAC		2
110 #define SUN7I_GPA0_GMAC		5
111 
112 #define SUNXI_GPB0_TWI0		2
113 
114 #define SUN4I_GPB22_UART0_TX	2
115 #define SUN4I_GPB23_UART0_RX	2
116 
117 #define SUN5I_GPB19_UART0_TX	2
118 #define SUN5I_GPB20_UART0_RX	2
119 
120 #define SUN5I_GPG3_UART1_TX	4
121 #define SUN5I_GPG4_UART1_RX	4
122 
123 #define SUNXI_GPC6_SDC2		3
124 
125 #define SUNXI_GPF0_SDC0		2
126 
127 #define SUNXI_GPF2_SDC0		2
128 #define SUNXI_GPF2_UART0_TX	4
129 #define SUNXI_GPF4_UART0_RX	4
130 
131 #define SUN4I_GPG0_SDC1		4
132 
133 #define SUN4I_GPH22_SDC1	5
134 
135 #define SUN4I_GPI4_SDC3		2
136 
137 /* GPIO pin pull-up/down config */
138 #define SUNXI_GPIO_PULL_DISABLE	0
139 #define SUNXI_GPIO_PULL_UP	1
140 #define SUNXI_GPIO_PULL_DOWN	2
141 
142 int sunxi_gpio_set_cfgpin(u32 pin, u32 val);
143 int sunxi_gpio_get_cfgpin(u32 pin);
144 int sunxi_gpio_set_drv(u32 pin, u32 val);
145 int sunxi_gpio_set_pull(u32 pin, u32 val);
146 
147 #endif /* _SUNXI_GPIO_H */
148