omap_gpio.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. int gpio_is_valid(int gpio)
  53. {
  54. return (gpio >= 0) && (gpio < OMAP_MAX_GPIO);
  55. }
  56. static int check_gpio(int gpio)
  57. {
  58. if (!gpio_is_valid(gpio)) {
  59. printf("ERROR : check_gpio: invalid GPIO %d\n", gpio);
  60. return -1;
  61. }
  62. return 0;
  63. }
  64. static void _set_gpio_direction(const struct gpio_bank *bank, int gpio,
  65. int is_input)
  66. {
  67. void *reg = bank->base;
  68. u32 l;
  69. switch (bank->method) {
  70. case METHOD_GPIO_24XX:
  71. reg += OMAP_GPIO_OE;
  72. break;
  73. default:
  74. return;
  75. }
  76. l = __raw_readl(reg);
  77. if (is_input)
  78. l |= 1 << gpio;
  79. else
  80. l &= ~(1 << gpio);
  81. __raw_writel(l, reg);
  82. }
  83. /**
  84. * Get the direction of the GPIO by reading the GPIO_OE register
  85. * corresponding to the specified bank.
  86. */
  87. static int _get_gpio_direction(const struct gpio_bank *bank, int gpio)
  88. {
  89. void *reg = bank->base;
  90. u32 v;
  91. switch (bank->method) {
  92. case METHOD_GPIO_24XX:
  93. reg += OMAP_GPIO_OE;
  94. break;
  95. default:
  96. return -1;
  97. }
  98. v = __raw_readl(reg);
  99. if (v & (1 << gpio))
  100. return OMAP_GPIO_DIR_IN;
  101. else
  102. return OMAP_GPIO_DIR_OUT;
  103. }
  104. static void _set_gpio_dataout(const struct gpio_bank *bank, int gpio,
  105. int enable)
  106. {
  107. void *reg = bank->base;
  108. u32 l = 0;
  109. switch (bank->method) {
  110. case METHOD_GPIO_24XX:
  111. if (enable)
  112. reg += OMAP_GPIO_SETDATAOUT;
  113. else
  114. reg += OMAP_GPIO_CLEARDATAOUT;
  115. l = 1 << gpio;
  116. break;
  117. default:
  118. printf("omap3-gpio unknown bank method %s %d\n",
  119. __FILE__, __LINE__);
  120. return;
  121. }
  122. __raw_writel(l, reg);
  123. }
  124. /**
  125. * Set value of the specified gpio
  126. */
  127. int gpio_set_value(unsigned gpio, int value)
  128. {
  129. const struct gpio_bank *bank;
  130. if (check_gpio(gpio) < 0)
  131. return -1;
  132. bank = get_gpio_bank(gpio);
  133. _set_gpio_dataout(bank, get_gpio_index(gpio), value);
  134. return 0;
  135. }
  136. /**
  137. * Get value of the specified gpio
  138. */
  139. int gpio_get_value(unsigned gpio)
  140. {
  141. const struct gpio_bank *bank;
  142. void *reg;
  143. int input;
  144. if (check_gpio(gpio) < 0)
  145. return -1;
  146. bank = get_gpio_bank(gpio);
  147. reg = bank->base;
  148. switch (bank->method) {
  149. case METHOD_GPIO_24XX:
  150. input = _get_gpio_direction(bank, get_gpio_index(gpio));
  151. switch (input) {
  152. case OMAP_GPIO_DIR_IN:
  153. reg += OMAP_GPIO_DATAIN;
  154. break;
  155. case OMAP_GPIO_DIR_OUT:
  156. reg += OMAP_GPIO_DATAOUT;
  157. break;
  158. default:
  159. return -1;
  160. }
  161. break;
  162. default:
  163. return -1;
  164. }
  165. return (__raw_readl(reg)
  166. & (1 << get_gpio_index(gpio))) != 0;
  167. }
  168. /**
  169. * Set gpio direction as input
  170. */
  171. int gpio_direction_input(unsigned gpio)
  172. {
  173. const struct gpio_bank *bank;
  174. if (check_gpio(gpio) < 0)
  175. return -1;
  176. bank = get_gpio_bank(gpio);
  177. _set_gpio_direction(bank, get_gpio_index(gpio), 1);
  178. return 0;
  179. }
  180. /**
  181. * Set gpio direction as output
  182. */
  183. int gpio_direction_output(unsigned gpio, int value)
  184. {
  185. const struct gpio_bank *bank;
  186. if (check_gpio(gpio) < 0)
  187. return -1;
  188. bank = get_gpio_bank(gpio);
  189. _set_gpio_dataout(bank, get_gpio_index(gpio), value);
  190. _set_gpio_direction(bank, get_gpio_index(gpio), 0);
  191. return 0;
  192. }
  193. /**
  194. * Request a gpio before using it.
  195. *
  196. * NOTE: Argument 'label' is unused.
  197. */
  198. int gpio_request(unsigned gpio, const char *label)
  199. {
  200. if (check_gpio(gpio) < 0)
  201. return -1;
  202. return 0;
  203. }
  204. /**
  205. * Reset and free the gpio after using it.
  206. */
  207. int gpio_free(unsigned gpio)
  208. {
  209. return 0;
  210. }