gpio.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. Linux gpio.C
  3. */
  4. #include <linux/module.h>
  5. #include <mach/am_regs.h>
  6. #include <mach/gpio.h>
  7. struct gpio_addr {
  8. unsigned long mode_addr;
  9. unsigned long out_addr;
  10. unsigned long in_addr;
  11. };
  12. static struct gpio_addr gpio_addrs[] = {
  13. [PREG_EGPIO] = {PREG_EGPIO_EN_N, PREG_EGPIO_O, PREG_EGPIO_I},
  14. [PREG_FGPIO] = {PREG_FGPIO_EN_N, PREG_FGPIO_O, PREG_FGPIO_I},
  15. [PREG_GGPIO] = {PREG_GGPIO_EN_N, PREG_GGPIO_O, PREG_GGPIO_I},
  16. [PREG_HGPIO] = {PREG_HGPIO_EN_N, PREG_HGPIO_O, PREG_HGPIO_I},
  17. [PREG_JTAG_GPIO] =
  18. {PREG_JTAG_GPIO_ADDR, PREG_JTAG_GPIO_ADDR, PREG_JTAG_GPIO_ADDR},
  19. };
  20. char jtag_bits_map[][3] = {
  21. [0] = {0, 4, 8},
  22. [1] = {1, 5, 9},
  23. [2] = {2, 6, 10},
  24. [3] = {3, 7, 11},
  25. [16] = {16, 19, 24},
  26. };
  27. static inline int gpio_bits(int type, gpio_bank_t bank, int bit)
  28. {
  29. if ((bank == PREG_JTAG_GPIO) && (type < 3)) {
  30. return jtag_bits_map[bit][type];
  31. } else {
  32. return bit;
  33. }
  34. }
  35. int set_gpio_mode(gpio_bank_t bank, int bit, gpio_mode_t mode)
  36. {
  37. #ifdef CONFIG_EXGPIO
  38. if (bank >= EXGPIO_BANK0) {
  39. set_exgpio_mode(bank - EXGPIO_BANK0, bit, mode);
  40. return 0;
  41. }
  42. #endif
  43. unsigned long addr = gpio_addrs[bank].mode_addr;
  44. WRITE_CBUS_REG_BITS(addr, mode, gpio_bits(0, bank, bit), 1);
  45. return 0;
  46. }
  47. gpio_mode_t get_gpio_mode(gpio_bank_t bank, int bit)
  48. {
  49. #ifdef CONFIG_EXGPIO
  50. if (bank >= EXGPIO_BANK0) {
  51. return get_exgpio_mode(bank - EXGPIO_BANK0, bit);
  52. }
  53. #endif
  54. unsigned long addr = gpio_addrs[bank].mode_addr;
  55. return (READ_CBUS_REG_BITS(addr, gpio_bits(0, bank, bit), 1) >
  56. 0) ? (GPIO_INPUT_MODE) : (GPIO_OUTPUT_MODE);
  57. }
  58. int set_gpio_val(gpio_bank_t bank, int bit, unsigned long val)
  59. {
  60. #ifdef CONFIG_EXGPIO
  61. if (bank >= EXGPIO_BANK0) {
  62. set_exgpio_val(bank - EXGPIO_BANK0, bit, val);
  63. return 0;
  64. }
  65. #endif
  66. unsigned long addr = gpio_addrs[bank].out_addr;
  67. WRITE_CBUS_REG_BITS(addr, val ? 1 : 0, gpio_bits(1, bank, bit), 1);
  68. return 0;
  69. }
  70. unsigned long get_gpio_val(gpio_bank_t bank, int bit)
  71. {
  72. #ifdef CONFIG_EXGPIO
  73. if (bank >= EXGPIO_BANK0) {
  74. return get_exgpio_val(bank - EXGPIO_BANK0, bit);
  75. }
  76. #endif
  77. unsigned long addr = gpio_addrs[bank].in_addr;
  78. return READ_CBUS_REG_BITS(addr, gpio_bits(2, bank, bit), 1);
  79. }
  80. int gpio_to_idx(unsigned gpio)
  81. {
  82. gpio_bank_t bank = (gpio_bank_t) (gpio >> 16);
  83. int bit = gpio & 0xFFFF;
  84. int idx = -1;
  85. if (bank == PREG_EGPIO) {
  86. if (bit < 4)
  87. idx = GPIOA_23_IDX - bit;
  88. else if (bit < 19)
  89. idx = GPIOA_IDX + bit - 4;
  90. else
  91. idx = GPIOB_IDX + bit - 19;
  92. } else if (bank == PREG_FGPIO)
  93. idx = GPIOC_IDX + bit;
  94. else if (bank == PREG_GGPIO)
  95. idx = GPIOD_IDX + bit + 2;
  96. else if (bank == PREG_HGPIO)
  97. idx = GPIOE_IDX + bit;
  98. return idx;
  99. }
  100. /**
  101. * enable gpio edge interrupt
  102. *
  103. * @param [in] pin index number of the chip, start with 0 up to 255
  104. * @param [in] flag rising(0) or falling(1) edge
  105. * @param [in] group this interrupt belong to which interrupt group from 0 to 7
  106. */
  107. void gpio_enable_edge_int(int pin, int flag, int group)
  108. {
  109. group &= 7;
  110. WRITE_CBUS_REG_BITS(GPIO_INTR_GPIO_SEL0 + (group >> 2), pin,
  111. (group & 3) * 8, 8);
  112. WRITE_CBUS_REG_BITS(GPIO_INTR_EDGE_POL, 1, group, 1);
  113. WRITE_CBUS_REG_BITS(GPIO_INTR_EDGE_POL, flag, group + 16, 1);
  114. }
  115. /**
  116. * enable gpio level interrupt
  117. *
  118. * @param [in] pin index number of the chip, start with 0 up to 255
  119. * @param [in] flag high(0) or low(1) level
  120. * @param [in] group this interrupt belong to which interrupt group from 0 to 7
  121. */
  122. void gpio_enable_level_int(int pin, int flag, int group)
  123. {
  124. group &= 7;
  125. WRITE_CBUS_REG_BITS(GPIO_INTR_GPIO_SEL0 + (group >> 2), pin,
  126. (group & 3) * 8, 8);
  127. WRITE_CBUS_REG_BITS(GPIO_INTR_EDGE_POL, 0, group, 1);
  128. WRITE_CBUS_REG_BITS(GPIO_INTR_EDGE_POL, flag, group + 16, 1);
  129. }
  130. /**
  131. * enable gpio interrupt filter
  132. *
  133. * @param [in] filter from 0~7(*222ns)
  134. * @param [in] group this interrupt belong to which interrupt group from 0 to 7
  135. */
  136. void gpio_enable_int_filter(int filter, int group)
  137. {
  138. group &= 7;
  139. filter &= 7;
  140. WRITE_CBUS_REG_BITS(GPIO_INTR_FILTER_SEL0, filter, group * 4, 3);
  141. }
  142. int gpio_is_valid(int number)
  143. {
  144. return 1;
  145. }
  146. int gpio_request(unsigned gpio, const char *label)
  147. {
  148. return 0;
  149. }
  150. void gpio_free(unsigned gpio)
  151. {
  152. }
  153. int gpio_direction_input(unsigned gpio)
  154. {
  155. gpio_bank_t bank = (gpio_bank_t) (gpio >> 16);
  156. int bit = gpio & 0xFFFF;
  157. set_gpio_mode(bank, bit, GPIO_INPUT_MODE);
  158. printk("set gpio%d.%d input\n", bank, bit);
  159. return 0;
  160. }
  161. int gpio_direction_output(unsigned gpio, int value)
  162. {
  163. gpio_bank_t bank = (gpio_bank_t) (gpio >> 16);
  164. int bit = gpio & 0xFFFF;
  165. set_gpio_val(bank, bit, value ? 1 : 0);
  166. set_gpio_mode(bank, bit, GPIO_OUTPUT_MODE);
  167. printk("set gpio%d.%d output\n", bank, bit);
  168. return 0;
  169. }
  170. void gpio_set_value(unsigned gpio, int value)
  171. {
  172. gpio_bank_t bank = (gpio_bank_t) (gpio >> 16);
  173. int bit = gpio & 0xFFFF;
  174. set_gpio_val(bank, bit, value ? 1 : 0);
  175. }
  176. int gpio_get_value(unsigned gpio)
  177. {
  178. gpio_bank_t bank = (gpio_bank_t) (gpio >> 16);
  179. int bit = gpio & 0xFFFF;
  180. return (get_gpio_val(bank, bit));
  181. }