gpio.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * (C) Copyright 2007-2012
  4. * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
  5. * Tom Cubie <tangliang@allwinnertech.com>
  6. */
  7. #ifndef _SUNXI_GPIO_H
  8. #define _SUNXI_GPIO_H
  9. #include <linux/types.h>
  10. #include <asm/arch/cpu.h>
  11. /*
  12. * sunxi has 9 banks of gpio, they are:
  13. * PA0 - PA17 | PB0 - PB23 | PC0 - PC24
  14. * PD0 - PD27 | PE0 - PE31 | PF0 - PF5
  15. * PG0 - PG9 | PH0 - PH27 | PI0 - PI12
  16. */
  17. #define SUNXI_GPIO_A 0
  18. #define SUNXI_GPIO_B 1
  19. #define SUNXI_GPIO_C 2
  20. #define SUNXI_GPIO_D 3
  21. #define SUNXI_GPIO_E 4
  22. #define SUNXI_GPIO_F 5
  23. #define SUNXI_GPIO_G 6
  24. #define SUNXI_GPIO_H 7
  25. #define SUNXI_GPIO_I 8
  26. /*
  27. * This defines the number of GPIO banks for the _main_ GPIO controller.
  28. * You should fix up the padding in struct sunxi_gpio_reg below if you
  29. * change this.
  30. */
  31. #define SUNXI_GPIO_BANKS 9
  32. /*
  33. * sun6i/sun8i and later SoCs have an additional GPIO controller (R_PIO)
  34. * at a different register offset.
  35. *
  36. * sun6i has 2 banks:
  37. * PL0 - PL8 | PM0 - PM7
  38. *
  39. * sun8i has 1 bank:
  40. * PL0 - PL11
  41. *
  42. * sun9i has 3 banks:
  43. * PL0 - PL9 | PM0 - PM15 | PN0 - PN1
  44. */
  45. #define SUNXI_GPIO_L 11
  46. #define SUNXI_GPIO_M 12
  47. #define SUNXI_GPIO_N 13
  48. struct sunxi_gpio {
  49. u32 cfg[4];
  50. u32 dat;
  51. u32 drv[2];
  52. u32 pull[2];
  53. };
  54. /* gpio interrupt control */
  55. struct sunxi_gpio_int {
  56. u32 cfg[3];
  57. u32 ctl;
  58. u32 sta;
  59. u32 deb; /* interrupt debounce */
  60. };
  61. struct sunxi_gpio_reg {
  62. struct sunxi_gpio gpio_bank[SUNXI_GPIO_BANKS];
  63. u8 res[0xbc];
  64. struct sunxi_gpio_int gpio_int;
  65. };
  66. #define BANK_TO_GPIO(bank) (((bank) < SUNXI_GPIO_L) ? \
  67. &((struct sunxi_gpio_reg *)SUNXI_PIO_BASE)->gpio_bank[bank] : \
  68. &((struct sunxi_gpio_reg *)SUNXI_R_PIO_BASE)->gpio_bank[(bank) - SUNXI_GPIO_L])
  69. #define GPIO_BANK(pin) ((pin) >> 5)
  70. #define GPIO_NUM(pin) ((pin) & 0x1f)
  71. #define GPIO_CFG_INDEX(pin) (((pin) & 0x1f) >> 3)
  72. #define GPIO_CFG_OFFSET(pin) ((((pin) & 0x1f) & 0x7) << 2)
  73. #define GPIO_DRV_INDEX(pin) (((pin) & 0x1f) >> 4)
  74. #define GPIO_DRV_OFFSET(pin) ((((pin) & 0x1f) & 0xf) << 1)
  75. #define GPIO_PULL_INDEX(pin) (((pin) & 0x1f) >> 4)
  76. #define GPIO_PULL_OFFSET(pin) ((((pin) & 0x1f) & 0xf) << 1)
  77. /* GPIO bank sizes */
  78. #define SUNXI_GPIO_A_NR 32
  79. #define SUNXI_GPIO_B_NR 32
  80. #define SUNXI_GPIO_C_NR 32
  81. #define SUNXI_GPIO_D_NR 32
  82. #define SUNXI_GPIO_E_NR 32
  83. #define SUNXI_GPIO_F_NR 32
  84. #define SUNXI_GPIO_G_NR 32
  85. #define SUNXI_GPIO_H_NR 32
  86. #define SUNXI_GPIO_I_NR 32
  87. #define SUNXI_GPIO_L_NR 32
  88. #define SUNXI_GPIO_M_NR 32
  89. #define SUNXI_GPIO_NEXT(__gpio) \
  90. ((__gpio##_START) + (__gpio##_NR) + 0)
  91. enum sunxi_gpio_number {
  92. SUNXI_GPIO_A_START = 0,
  93. SUNXI_GPIO_B_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_A),
  94. SUNXI_GPIO_C_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_B),
  95. SUNXI_GPIO_D_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_C),
  96. SUNXI_GPIO_E_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_D),
  97. SUNXI_GPIO_F_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_E),
  98. SUNXI_GPIO_G_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_F),
  99. SUNXI_GPIO_H_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_G),
  100. SUNXI_GPIO_I_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_H),
  101. SUNXI_GPIO_L_START = 352,
  102. SUNXI_GPIO_M_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_L),
  103. SUNXI_GPIO_N_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_M),
  104. SUNXI_GPIO_AXP0_START = 1024,
  105. };
  106. /* SUNXI GPIO number definitions */
  107. #define SUNXI_GPA(_nr) (SUNXI_GPIO_A_START + (_nr))
  108. #define SUNXI_GPB(_nr) (SUNXI_GPIO_B_START + (_nr))
  109. #define SUNXI_GPC(_nr) (SUNXI_GPIO_C_START + (_nr))
  110. #define SUNXI_GPD(_nr) (SUNXI_GPIO_D_START + (_nr))
  111. #define SUNXI_GPE(_nr) (SUNXI_GPIO_E_START + (_nr))
  112. #define SUNXI_GPF(_nr) (SUNXI_GPIO_F_START + (_nr))
  113. #define SUNXI_GPG(_nr) (SUNXI_GPIO_G_START + (_nr))
  114. #define SUNXI_GPH(_nr) (SUNXI_GPIO_H_START + (_nr))
  115. #define SUNXI_GPI(_nr) (SUNXI_GPIO_I_START + (_nr))
  116. #define SUNXI_GPL(_nr) (SUNXI_GPIO_L_START + (_nr))
  117. #define SUNXI_GPM(_nr) (SUNXI_GPIO_M_START + (_nr))
  118. #define SUNXI_GPN(_nr) (SUNXI_GPIO_N_START + (_nr))
  119. #define SUNXI_GPAXP0(_nr) (SUNXI_GPIO_AXP0_START + (_nr))
  120. /* GPIO pin function config */
  121. #define SUNXI_GPIO_INPUT 0
  122. #define SUNXI_GPIO_OUTPUT 1
  123. #define SUNXI_GPIO_DISABLE 7
  124. #define SUNXI_GPA_EMAC 2
  125. #define SUN6I_GPA_GMAC 2
  126. #define SUN7I_GPA_GMAC 5
  127. #define SUN6I_GPA_SDC2 5
  128. #define SUN6I_GPA_SDC3 4
  129. #define SUN8I_H3_GPA_UART0 2
  130. #define SUN4I_GPB_PWM 2
  131. #define SUN4I_GPB_TWI0 2
  132. #define SUN4I_GPB_TWI1 2
  133. #define SUN5I_GPB_TWI1 2
  134. #define SUN4I_GPB_TWI2 2
  135. #define SUN5I_GPB_TWI2 2
  136. #define SUN4I_GPB_UART0 2
  137. #define SUN5I_GPB_UART0 2
  138. #define SUN8I_GPB_UART2 2
  139. #define SUN8I_A33_GPB_UART0 3
  140. #define SUN8I_A83T_GPB_UART0 2
  141. #define SUN8I_V3S_GPB_UART0 3
  142. #define SUN50I_GPB_UART0 4
  143. #define SUNXI_GPC_NAND 2
  144. #define SUNXI_GPC_SPI0 3
  145. #define SUNXI_GPC_SDC2 3
  146. #define SUN6I_GPC_SDC3 4
  147. #define SUN50I_GPC_SPI0 4
  148. #define SUN8I_GPD_SDC1 3
  149. #define SUNXI_GPD_LCD0 2
  150. #define SUNXI_GPD_LVDS0 3
  151. #define SUNXI_GPD_PWM 2
  152. #define SUN5I_GPE_SDC2 3
  153. #define SUN8I_GPE_TWI2 3
  154. #define SUNXI_GPF_SDC0 2
  155. #define SUNXI_GPF_UART0 4
  156. #define SUN8I_GPF_UART0 3
  157. #define SUN4I_GPG_SDC1 4
  158. #define SUN5I_GPG_SDC1 2
  159. #define SUN6I_GPG_SDC1 2
  160. #define SUN8I_GPG_SDC1 2
  161. #define SUN6I_GPG_TWI3 2
  162. #define SUN5I_GPG_UART1 4
  163. #define SUN6I_GPH_PWM 2
  164. #define SUN8I_GPH_PWM 2
  165. #define SUN4I_GPH_SDC1 5
  166. #define SUN6I_GPH_TWI0 2
  167. #define SUN8I_GPH_TWI0 2
  168. #define SUN6I_GPH_TWI1 2
  169. #define SUN8I_GPH_TWI1 2
  170. #define SUN6I_GPH_TWI2 2
  171. #define SUN6I_GPH_UART0 2
  172. #define SUN9I_GPH_UART0 2
  173. #define SUN50I_H6_GPH_UART0 2
  174. #define SUNXI_GPI_SDC3 2
  175. #define SUN7I_GPI_TWI3 3
  176. #define SUN7I_GPI_TWI4 3
  177. #define SUN6I_GPL0_R_P2WI_SCK 3
  178. #define SUN6I_GPL1_R_P2WI_SDA 3
  179. #define SUN8I_GPL_R_RSB 2
  180. #define SUN8I_H3_GPL_R_TWI 2
  181. #define SUN8I_A23_GPL_R_TWI 3
  182. #define SUN8I_GPL_R_UART 2
  183. #define SUN50I_GPL_R_TWI 2
  184. #define SUN9I_GPN_R_RSB 3
  185. /* GPIO pin pull-up/down config */
  186. #define SUNXI_GPIO_PULL_DISABLE 0
  187. #define SUNXI_GPIO_PULL_UP 1
  188. #define SUNXI_GPIO_PULL_DOWN 2
  189. /* Virtual AXP0 GPIOs */
  190. #define SUNXI_GPIO_AXP0_PREFIX "AXP0-"
  191. #define SUNXI_GPIO_AXP0_VBUS_DETECT 4
  192. #define SUNXI_GPIO_AXP0_VBUS_ENABLE 5
  193. #define SUNXI_GPIO_AXP0_GPIO_COUNT 6
  194. void sunxi_gpio_set_cfgbank(struct sunxi_gpio *pio, int bank_offset, u32 val);
  195. void sunxi_gpio_set_cfgpin(u32 pin, u32 val);
  196. int sunxi_gpio_get_cfgbank(struct sunxi_gpio *pio, int bank_offset);
  197. int sunxi_gpio_get_cfgpin(u32 pin);
  198. int sunxi_gpio_set_drv(u32 pin, u32 val);
  199. int sunxi_gpio_set_pull(u32 pin, u32 val);
  200. int sunxi_name_to_gpio_bank(const char *name);
  201. int sunxi_name_to_gpio(const char *name);
  202. #define name_to_gpio(name) sunxi_name_to_gpio(name)
  203. #if !defined CONFIG_SPL_BUILD && defined CONFIG_AXP_GPIO
  204. int axp_gpio_init(void);
  205. #else
  206. static inline int axp_gpio_init(void) { return 0; }
  207. #endif
  208. #endif /* _SUNXI_GPIO_H */