gpio.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (c) 2019-2025 Allwinner Technology Co., Ltd. ALL rights reserved.
  3. *
  4. * Allwinner is a trademark of Allwinner Technology Co.,Ltd., registered in
  5. * the the People's Republic of China and other countries.
  6. * All Allwinner Technology Co.,Ltd. trademarks are used with permission.
  7. *
  8. * DISCLAIMER
  9. * THIRD PARTY LICENCES MAY BE REQUIRED TO IMPLEMENT THE SOLUTION/PRODUCT.
  10. * IF YOU NEED TO INTEGRATE THIRD PARTY’S TECHNOLOGY (SONY, DTS, DOLBY, AVS OR MPEGLA, ETC.)
  11. * IN ALLWINNERS’SDK OR PRODUCTS, YOU SHALL BE SOLELY RESPONSIBLE TO OBTAIN
  12. * ALL APPROPRIATELY REQUIRED THIRD PARTY LICENCES.
  13. * ALLWINNER SHALL HAVE NO WARRANTY, INDEMNITY OR OTHER OBLIGATIONS WITH RESPECT TO MATTERS
  14. * COVERED UNDER ANY REQUIRED THIRD PARTY LICENSE.
  15. * YOU ARE SOLELY RESPONSIBLE FOR YOUR USAGE OF THIRD PARTY’S TECHNOLOGY.
  16. *
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY ALLWINNER"AS IS" AND TO THE MAXIMUM EXTENT
  19. * PERMITTED BY LAW, ALLWINNER EXPRESSLY DISCLAIMS ALL WARRANTIES OF ANY KIND,
  20. * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION REGARDING
  21. * THE TITLE, NON-INFRINGEMENT, ACCURACY, CONDITION, COMPLETENESS, PERFORMANCE
  22. * OR MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  23. * IN NO EVENT SHALL ALLWINNER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  28. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  30. * OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #ifndef __GPIO_I_H__
  33. #define __GPIO_I_H__
  34. #include "hal_interrupt.h"
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. #define PA_BASE 0
  39. #define PB_BASE 32
  40. #define PC_BASE 64
  41. #define PD_BASE 96
  42. #define PE_BASE 128
  43. #define PF_BASE 160
  44. #define PG_BASE 192
  45. #define PH_BASE 224
  46. #define PI_BASE 256
  47. #define PJ_BASE 288
  48. #define PK_BASE 320
  49. #define PL_BASE 352
  50. #define PM_BASE 384
  51. #define PN_BASE 416
  52. #define PO_BASE 448
  53. /* sunxi gpio name space */
  54. #define GPIOA(n) (PA_BASE + (n))
  55. #define GPIOB(n) (PB_BASE + (n))
  56. #define GPIOC(n) (PC_BASE + (n))
  57. #define GPIOD(n) (PD_BASE + (n))
  58. #define GPIOE(n) (PE_BASE + (n))
  59. #define GPIOF(n) (PF_BASE + (n))
  60. #define GPIOG(n) (PG_BASE + (n))
  61. #define GPIOH(n) (PH_BASE + (n))
  62. #define GPIOI(n) (PI_BASE + (n))
  63. #define GPIOJ(n) (PJ_BASE + (n))
  64. #define GPIOK(n) (PK_BASE + (n))
  65. #define GPIOL(n) (PL_BASE + (n))
  66. #define GPIOM(n) (PM_BASE + (n))
  67. #define GPION(n) (PN_BASE + (n))
  68. #define GPIOO(n) (PO_BASE + (n))
  69. #define IRQ_MEM_SIZE 0x20
  70. #define GIC_IRQ_NUM 140
  71. #define GPIO_IRQ_START (GIC_IRQ_NUM + 1)
  72. #if defined(CONFIG_ARCH_SUN8IW20) || defined(CONFIG_SOC_SUN20IW1)
  73. #define BANK_MEM_SIZE 0x30
  74. #define PULL_REGS_OFFSET 0x24
  75. #define DLEVEL_PINS_PER_REG 8
  76. #define DLEVEL_PINS_BITS 4
  77. #define DLEVEL_PINS_MASK 0x0f
  78. #else
  79. #define BANK_MEM_SIZE 0x24
  80. #define PULL_REGS_OFFSET 0x1c
  81. #define DLEVEL_PINS_PER_REG 16
  82. #define DLEVEL_PINS_BITS 2
  83. #define DLEVEL_PINS_MASK 0x03
  84. #endif
  85. #define MUX_REGS_OFFSET 0x0
  86. #define DATA_REGS_OFFSET 0x10
  87. #define DLEVEL_REGS_OFFSET 0x14
  88. #define PINS_PER_BANK 32
  89. #define MUX_PINS_PER_REG 8
  90. #define MUX_PINS_BITS 4
  91. #define MUX_PINS_MASK 0x0f
  92. #define DATA_PINS_PER_REG 32
  93. #define DATA_PINS_BITS 1
  94. #define DATA_PINS_MASK 0x01
  95. #define PULL_PINS_PER_REG 16
  96. #define PULL_PINS_BITS 2
  97. #define PULL_PINS_MASK 0x03
  98. #define IRQ_PER_BANK 32
  99. #define IRQ_CFG_REG 0x200
  100. #define IRQ_CFG_IRQ_PER_REG 8
  101. #define IRQ_CFG_IRQ_BITS 4
  102. #define IRQ_CFG_IRQ_MASK ((1 << IRQ_CFG_IRQ_BITS) - 1)
  103. #define IRQ_CTRL_REG 0x210
  104. #define IRQ_CTRL_IRQ_PER_REG 32
  105. #define IRQ_CTRL_IRQ_BITS 1
  106. #define IRQ_CTRL_IRQ_MASK ((1 << IRQ_CTRL_IRQ_BITS) - 1)
  107. #define IRQ_STATUS_REG 0x214
  108. #define IRQ_STATUS_IRQ_PER_REG 32
  109. #define IRQ_STATUS_IRQ_BITS 1
  110. #define IRQ_STATUS_IRQ_MASK ((1 << IRQ_STATUS_IRQ_BITS) - 1)
  111. #define IRQ_DEBOUNCE_REG 0x218
  112. #define POWER_MODE_SEL 0x0340
  113. #define POWER_MODE_VAL 0x0348
  114. #define POWER_VOL_SEL 0x0350
  115. #define IRQ_MEM_SIZE 0x20
  116. #define GIC_IRQ_NUM 140
  117. #define GPIO_IRQ_START (GIC_IRQ_NUM + 1)
  118. #define IRQ_EDGE_RISING 0x00
  119. #define IRQ_EDGE_FALLING 0x01
  120. #define IRQ_LEVEL_HIGH 0x02
  121. #define IRQ_LEVEL_LOW 0x03
  122. #define IRQ_EDGE_BOTH 0x04
  123. #define SUNXI_PIO_BANK_BASE(pin, irq_bank) \
  124. ((pin-PA_BASE)/PINS_PER_BANK - irq_bank)
  125. #define SUNXI_R_PIO_BANK_BASE(pin, irq_bank) \
  126. ((pin-PL_BASE)/PINS_PER_BANK - irq_bank)
  127. /*
  128. * This looks more complex than it should be. But we need to
  129. * get the type for the ~ right in round_down (it needs to be
  130. * as wide as the result!), and we want to evaluate the macro
  131. * arguments just once each.
  132. */
  133. #define __round_mask(x, y) ((__typeof__(x))((y)-1))
  134. #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
  135. #define round_down(x, y) ((x) & ~__round_mask(x, y))
  136. #ifndef ARRAY_SIZE
  137. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  138. #endif
  139. /*
  140. * gpio configuration (pull up/down and drive strength) type and its value are
  141. * packed together into a 32-bits. The lower 8-bits represent the configuration
  142. * type and the upper 24-bits hold the value of the configuration type.
  143. */
  144. #define GPIO_CFG_PACK(type, value) (((value) << 8) | ((unsigned long) type & 0xFFUL))
  145. #define GPIO_CFG_UNPACK_TYPE(cfg) ((cfg) & 0xFFUL)
  146. #define GPIO_CFG_UNPACK_VALUE(cfg) (((cfg) & 0xFFFFFF00UL) >> 8)
  147. typedef enum
  148. {
  149. GPIO_TYPE_FUNC,
  150. GPIO_TYPE_DAT,
  151. GPIO_TYPE_PUD,
  152. GPIO_TYPE_DRV,
  153. GPIO_TYPE_VOL,
  154. GPIO_CONFIG_END = 0x7F,
  155. GPIO_CONFIG_MAX = 0xFF,
  156. } pin_config_param_t;
  157. typedef enum
  158. {
  159. IRQ_TYPE_NONE = 0x00000000,
  160. IRQ_TYPE_EDGE_RISING = 0x00000001,
  161. IRQ_TYPE_EDGE_FALLING = 0x00000002,
  162. IRQ_TYPE_EDGE_BOTH = (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING),
  163. IRQ_TYPE_LEVEL_HIGH = 0x00000004,
  164. IRQ_TYPE_LEVEL_LOW = 0x00000008,
  165. } gpio_interrupt_mode_t;
  166. struct gpio_irq_desc
  167. {
  168. uint32_t virq;
  169. uint32_t pin;
  170. unsigned long flags;
  171. irq_handler_t handle_irq;
  172. void (*irq_attach)(void*);
  173. void *data;
  174. };
  175. struct gpio_desc
  176. {
  177. const unsigned long membase;
  178. const uint32_t resource_size; /* reg resource size */
  179. const uint32_t irq_arry_size;
  180. const uint32_t *irq;
  181. const uint32_t pin_base;
  182. const uint32_t banks;
  183. const uint32_t *bank_base;
  184. const uint32_t irq_banks;
  185. const uint32_t *irq_bank_base;
  186. const uint32_t virq_offset;
  187. uint32_t irq_desc_size;
  188. struct gpio_irq_desc *irq_desc;
  189. };
  190. struct gpio_pm_reg_cache
  191. {
  192. void *reg_dump[2];
  193. int reg_dump_size[2];
  194. };
  195. /*
  196. * include the platform gpio header file,
  197. * should be after the name space macro.
  198. */
  199. #if defined(CONFIG_ARCH_SUN8IW19)
  200. #include "sun8iw19/platform-gpio.h"
  201. #endif
  202. #if defined(CONFIG_ARCH_SUN8IW18P1)
  203. #include "sun8iw18/platform-gpio.h"
  204. #endif
  205. #if defined(CONFIG_ARCH_SUN8IW20) || defined(CONFIG_SOC_SUN20IW1)
  206. #include "sun8iw20/platform-gpio.h"
  207. #endif
  208. #if defined(CONFIG_ARCH_SUN50IW11)
  209. #include "sun50iw11/platform-gpio.h"
  210. #endif
  211. const struct gpio_desc **gpio_get_platform_desc(void);
  212. int hal_gpio_suspend(void);
  213. int hal_gpio_resume(void);
  214. #ifdef __cplusplus
  215. }
  216. #endif
  217. #endif /* __GPIO_I_H__ */