gpio_lib.h 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * gpio_lib.c
  3. *
  4. * Copyright 2013 Stefan Mavrodiev <support@olimex.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. * MA 02110-1301, USA.
  20. */
  21. /******************************************************************************/
  22. #ifndef _GPIO_LIB_H_
  23. #define _GPIO_LIB_H_
  24. /******************************************************************************/
  25. #define SW_PORTC_IO_BASE 0x01c20800
  26. #define SUNXI_GPIO_A 0
  27. #define SUNXI_GPIO_B 1
  28. #define SUNXI_GPIO_C 2
  29. #define SUNXI_GPIO_D 3
  30. #define SUNXI_GPIO_E 4
  31. #define SUNXI_GPIO_F 5
  32. #define SUNXI_GPIO_G 6
  33. #define SUNXI_GPIO_H 7
  34. #define SUNXI_GPIO_I 8
  35. #define SETUP_OK 0
  36. #define SETUP_DEVMEM_FAIL 1
  37. #define SETUP_MALLOC_FAIL 2
  38. #define SETUP_MMAP_FAIL 3
  39. #define HIGH 1
  40. #define LOW 0
  41. #define INPUT 0
  42. #define OUTPUT 1
  43. #define PER 2
  44. /******************************************************************************/
  45. struct sunxi_gpio
  46. {
  47. unsigned int cfg[4];
  48. unsigned int dat;
  49. unsigned int drv[2];
  50. unsigned int pull[2];
  51. };
  52. /* gpio interrupt control */
  53. struct sunxi_gpio_int
  54. {
  55. unsigned int cfg[3];
  56. unsigned int ctl;
  57. unsigned int sta;
  58. unsigned int deb;
  59. };
  60. struct sunxi_gpio_reg
  61. {
  62. struct sunxi_gpio gpio_bank[9];
  63. unsigned char res[0xbc];
  64. struct sunxi_gpio_int gpio_int;
  65. };
  66. /******************************************************************************/
  67. #define GPIO_BANK(pin) ((pin) >> 5)
  68. #define GPIO_NUM(pin) ((pin) & 0x1F)
  69. #define GPIO_CFG_INDEX(pin) (((pin) & 0x1F) >> 3)
  70. #define GPIO_CFG_OFFSET(pin) ((((pin) & 0x1F) & 0x7) << 2)
  71. /******************************************************************************/
  72. /* GPIO bank sizes */
  73. #define SUNXI_GPIO_A_NR (32)
  74. #define SUNXI_GPIO_B_NR (32)
  75. #define SUNXI_GPIO_C_NR (32)
  76. #define SUNXI_GPIO_D_NR (32)
  77. #define SUNXI_GPIO_E_NR (32)
  78. #define SUNXI_GPIO_F_NR (32)
  79. #define SUNXI_GPIO_G_NR (32)
  80. #define SUNXI_GPIO_H_NR (32)
  81. #define SUNXI_GPIO_I_NR (32)
  82. /******************************************************************************/
  83. #define SUNXI_GPIO_NEXT(__gpio) ((__gpio##_START)+(__gpio##_NR)+0)
  84. /******************************************************************************/
  85. enum sunxi_gpio_number
  86. {
  87. SUNXI_GPIO_A_START = 0,
  88. SUNXI_GPIO_B_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_A), //32
  89. SUNXI_GPIO_C_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_B), //64
  90. SUNXI_GPIO_D_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_C), //96
  91. SUNXI_GPIO_E_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_D), //128
  92. SUNXI_GPIO_F_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_E), //160
  93. SUNXI_GPIO_G_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_F), //192
  94. SUNXI_GPIO_H_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_G), //224
  95. SUNXI_GPIO_I_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_H) //256
  96. };
  97. /******************************************************************************/
  98. /* SUNXI GPIO number definitions */
  99. #define SUNXI_GPA(_nr) (SUNXI_GPIO_A_START + (_nr))
  100. #define SUNXI_GPB(_nr) (SUNXI_GPIO_B_START + (_nr))
  101. #define SUNXI_GPC(_nr) (SUNXI_GPIO_C_START + (_nr))
  102. #define SUNXI_GPD(_nr) (SUNXI_GPIO_D_START + (_nr))
  103. #define SUNXI_GPE(_nr) (SUNXI_GPIO_E_START + (_nr))
  104. #define SUNXI_GPF(_nr) (SUNXI_GPIO_F_START + (_nr))
  105. #define SUNXI_GPG(_nr) (SUNXI_GPIO_G_START + (_nr))
  106. #define SUNXI_GPH(_nr) (SUNXI_GPIO_H_START + (_nr))
  107. #define SUNXI_GPI(_nr) (SUNXI_GPIO_I_START + (_nr))
  108. /******************************************************************************/
  109. /* GPIO pin function config */
  110. #define SUNXI_GPIO_INPUT (0)
  111. #define SUNXI_GPIO_OUTPUT (1)
  112. #define SUNXI_GPIO_PER (2)
  113. #define SUNXI_GPA0_ERXD3 (2)
  114. #define SUNXI_GPA0_SPI1_CS0 (3)
  115. #define SUNXI_GPA0_UART2_RTS (4)
  116. #define SUNXI_GPA1_ERXD2 (2)
  117. #define SUNXI_GPA1_SPI1_CLK (3)
  118. #define SUNXI_GPA1_UART2_CTS (4)
  119. #define SUNXI_GPA2_ERXD1 (2)
  120. #define SUNXI_GPA2_SPI1_MOSI (3)
  121. #define SUNXI_GPA2_UART2_TX (4)
  122. #define SUNXI_GPA10_UART1_TX (4)
  123. #define SUNXI_GPA11_UART1_RX (4)
  124. #define SUN4I_GPB22_UART0_TX (2)
  125. #define SUN4I_GPB23_UART0_RX (2)
  126. #define SUN5I_GPG3_UART0_TX (4)
  127. #define SUN5I_GPG4_UART0_RX (4)
  128. #define SUNXI_GPC2_NCLE (2)
  129. #define SUNXI_GPC2_SPI0_CLK (3)
  130. #define SUNXI_GPC6_NRB0 (2)
  131. #define SUNXI_GPC6_SDC2_CMD (3)
  132. #define SUNXI_GPC7_NRB1 (2)
  133. #define SUNXI_GPC7_SDC2_CLK (3)
  134. #define SUNXI_GPC8_NDQ0 (2)
  135. #define SUNXI_GPC8_SDC2_D0 (3)
  136. #define SUNXI_GPC9_NDQ1 (2)
  137. #define SUNXI_GPC9_SDC2_D1 (3)
  138. #define SUNXI_GPC10_NDQ2 (2)
  139. #define SUNXI_GPC10_SDC2_D2 (3)
  140. #define SUNXI_GPC11_NDQ3 (2)
  141. #define SUNXI_GPC11_SDC2_D3 (3)
  142. #define SUNXI_GPF2_SDC0_CLK (2)
  143. #define SUNXI_GPF2_UART0_TX (4)
  144. #define SUNXI_GPF4_SDC0_D3 (2)
  145. #define SUNXI_GPF4_UART0_RX (4)
  146. /******************************************************************************/
  147. extern int sunxi_gpio_input(unsigned int pin);
  148. extern int sunxi_gpio_init(void);
  149. extern int sunxi_gpio_set_cfgpin(unsigned int pin, unsigned int val);
  150. extern int sunxi_gpio_get_cfgpin(unsigned int pin);
  151. extern int sunxi_gpio_output(unsigned int pin, unsigned int val);
  152. extern int sunxi_gpio_getoutput(unsigned int pin);
  153. extern void sunxi_gpio_cleanup(void);
  154. /******************************************************************************/
  155. extern unsigned int SUNXI_PIO_BASE;
  156. /******************************************************************************/
  157. #endif