omap_gpio.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (c) 2009 Wind River Systems, Inc.
  3. * Tom Rix <Tom.Rix@windriver.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  18. * MA 02111-1307 USA
  19. *
  20. * This work is derived from the linux 2.6.27 kernel source
  21. * To fetch, use the kernel repository
  22. * git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
  23. * Use the v2.6.27 tag.
  24. *
  25. * Below is the original's header including its copyright
  26. *
  27. * linux/arch/arm/plat-omap/gpio.c
  28. *
  29. * Support functions for OMAP GPIO
  30. *
  31. * Copyright (C) 2003-2005 Nokia Corporation
  32. * Written by Juha Yrjölä <juha.yrjola@nokia.com>
  33. *
  34. * This program is free software; you can redistribute it and/or modify
  35. * it under the terms of the GNU General Public License version 2 as
  36. * published by the Free Software Foundation.
  37. */
  38. #include <common.h>
  39. #include <asm/gpio.h>
  40. #include <asm/io.h>
  41. #include <asm/errno.h>
  42. #define OMAP_GPIO_DIR_OUT 0
  43. #define OMAP_GPIO_DIR_IN 1
  44. static inline const struct gpio_bank *get_gpio_bank(int gpio)
  45. {
  46. return &omap_gpio_bank[gpio >> 5];
  47. }
  48. static inline int get_gpio_index(int gpio)
  49. {
  50. return gpio & 0x1f;
  51. }
  52. static inline int gpio_valid(int gpio)
  53. {
  54. if (gpio < 0)
  55. return -1;
  56. if (gpio < 192)
  57. return 0;
  58. return -1;
  59. }
  60. static int check_gpio(int gpio)
  61. {
  62. if (gpio_valid(gpio) < 0) {
  63. printf("ERROR : check_gpio: invalid GPIO %d\n", gpio);
  64. return -1;
  65. }
  66. return 0;
  67. }
  68. static void _set_gpio_direction(const struct gpio_bank *bank, int gpio,
  69. int is_input)
  70. {
  71. void *reg = bank->base;
  72. u32 l;
  73. switch (bank->method) {
  74. case METHOD_GPIO_24XX:
  75. reg += OMAP_GPIO_OE;
  76. break;
  77. default:
  78. return;
  79. }
  80. l = __raw_readl(reg);
  81. if (is_input)
  82. l |= 1 << gpio;
  83. else
  84. l &= ~(1 << gpio);
  85. __raw_writel(l, reg);
  86. }
  87. /**
  88. * Get the direction of the GPIO by reading the GPIO_OE register
  89. * corresponding to the specified bank.
  90. */
  91. static int _get_gpio_direction(const struct gpio_bank *bank, int gpio)
  92. {
  93. void *reg = bank->base;
  94. u32 v;
  95. switch (bank->method) {
  96. case METHOD_GPIO_24XX:
  97. reg += OMAP_GPIO_OE;
  98. break;
  99. default:
  100. return -1;
  101. }
  102. v = __raw_readl(reg);
  103. if (v & (1 << gpio))
  104. return OMAP_GPIO_DIR_IN;
  105. else
  106. return OMAP_GPIO_DIR_OUT;
  107. }
  108. static void _set_gpio_dataout(const struct gpio_bank *bank, int gpio,
  109. int enable)
  110. {
  111. void *reg = bank->base;
  112. u32 l = 0;
  113. switch (bank->method) {
  114. case METHOD_GPIO_24XX:
  115. if (enable)
  116. reg += OMAP_GPIO_SETDATAOUT;
  117. else
  118. reg += OMAP_GPIO_CLEARDATAOUT;
  119. l = 1 << gpio;
  120. break;
  121. default:
  122. printf("omap3-gpio unknown bank method %s %d\n",
  123. __FILE__, __LINE__);
  124. return;
  125. }
  126. __raw_writel(l, reg);
  127. }
  128. /**
  129. * Set value of the specified gpio
  130. */
  131. int gpio_set_value(unsigned gpio, int value)
  132. {
  133. const struct gpio_bank *bank;
  134. if (check_gpio(gpio) < 0)
  135. return -1;
  136. bank = get_gpio_bank(gpio);
  137. _set_gpio_dataout(bank, get_gpio_index(gpio), value);
  138. return 0;
  139. }
  140. /**
  141. * Get value of the specified gpio
  142. */
  143. int gpio_get_value(unsigned gpio)
  144. {
  145. const struct gpio_bank *bank;
  146. void *reg;
  147. int input;
  148. if (check_gpio(gpio) < 0)
  149. return -1;
  150. bank = get_gpio_bank(gpio);
  151. reg = bank->base;
  152. switch (bank->method) {
  153. case METHOD_GPIO_24XX:
  154. input = _get_gpio_direction(bank, get_gpio_index(gpio));
  155. switch (input) {
  156. case OMAP_GPIO_DIR_IN:
  157. reg += OMAP_GPIO_DATAIN;
  158. break;
  159. case OMAP_GPIO_DIR_OUT:
  160. reg += OMAP_GPIO_DATAOUT;
  161. break;
  162. default:
  163. return -1;
  164. }
  165. break;
  166. default:
  167. return -1;
  168. }
  169. return (__raw_readl(reg)
  170. & (1 << get_gpio_index(gpio))) != 0;
  171. }
  172. /**
  173. * Set gpio direction as input
  174. */
  175. int gpio_direction_input(unsigned gpio)
  176. {
  177. const struct gpio_bank *bank;
  178. if (check_gpio(gpio) < 0)
  179. return -1;
  180. bank = get_gpio_bank(gpio);
  181. _set_gpio_direction(bank, get_gpio_index(gpio), 1);
  182. return 0;
  183. }
  184. /**
  185. * Set gpio direction as output
  186. */
  187. int gpio_direction_output(unsigned gpio, int value)
  188. {
  189. const struct gpio_bank *bank;
  190. if (check_gpio(gpio) < 0)
  191. return -1;
  192. bank = get_gpio_bank(gpio);
  193. _set_gpio_dataout(bank, get_gpio_index(gpio), value);
  194. _set_gpio_direction(bank, get_gpio_index(gpio), 0);
  195. return 0;
  196. }
  197. /**
  198. * Request a gpio before using it.
  199. *
  200. * NOTE: Argument 'label' is unused.
  201. */
  202. int gpio_request(unsigned gpio, const char *label)
  203. {
  204. if (check_gpio(gpio) < 0)
  205. return -1;
  206. return 0;
  207. }
  208. /**
  209. * Reset and free the gpio after using it.
  210. */
  211. int gpio_free(unsigned gpio)
  212. {
  213. return 0;
  214. }