gpio.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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/arch/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 -EINVAL;
  56. if (gpio < 192)
  57. return 0;
  58. return -EINVAL;
  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 -EINVAL;
  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 -EINVAL;
  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. void gpio_set_value(int gpio, int value)
  132. {
  133. const struct gpio_bank *bank;
  134. if (check_gpio(gpio) < 0)
  135. return;
  136. bank = get_gpio_bank(gpio);
  137. _set_gpio_dataout(bank, get_gpio_index(gpio), value);
  138. }
  139. /**
  140. * Get value of the specified gpio
  141. */
  142. int gpio_get_value(int gpio)
  143. {
  144. const struct gpio_bank *bank;
  145. void *reg;
  146. int input;
  147. if (check_gpio(gpio) < 0)
  148. return -EINVAL;
  149. bank = get_gpio_bank(gpio);
  150. reg = bank->base;
  151. switch (bank->method) {
  152. case METHOD_GPIO_24XX:
  153. input = _get_gpio_direction(bank, get_gpio_index(gpio));
  154. switch (input) {
  155. case OMAP_GPIO_DIR_IN:
  156. reg += OMAP_GPIO_DATAIN;
  157. break;
  158. case OMAP_GPIO_DIR_OUT:
  159. reg += OMAP_GPIO_DATAOUT;
  160. break;
  161. default:
  162. return -EINVAL;
  163. }
  164. break;
  165. default:
  166. return -EINVAL;
  167. }
  168. return (__raw_readl(reg)
  169. & (1 << get_gpio_index(gpio))) != 0;
  170. }
  171. /**
  172. * Set gpio direction as input
  173. */
  174. int gpio_direction_input(unsigned gpio)
  175. {
  176. const struct gpio_bank *bank;
  177. if (check_gpio(gpio) < 0)
  178. return -EINVAL;
  179. bank = get_gpio_bank(gpio);
  180. _set_gpio_direction(bank, get_gpio_index(gpio), 1);
  181. return 0;
  182. }
  183. /**
  184. * Set gpio direction as output
  185. */
  186. int gpio_direction_output(unsigned gpio, int value)
  187. {
  188. const struct gpio_bank *bank;
  189. if (check_gpio(gpio) < 0)
  190. return -EINVAL;
  191. bank = get_gpio_bank(gpio);
  192. _set_gpio_dataout(bank, get_gpio_index(gpio), value);
  193. _set_gpio_direction(bank, get_gpio_index(gpio), 0);
  194. return 0;
  195. }
  196. /**
  197. * Request a gpio before using it.
  198. *
  199. * NOTE: Argument 'label' is unused.
  200. */
  201. int gpio_request(int gpio, const char *label)
  202. {
  203. if (check_gpio(gpio) < 0)
  204. return -EINVAL;
  205. return 0;
  206. }
  207. /**
  208. * Reset and free the gpio after using it.
  209. */
  210. void gpio_free(unsigned gpio)
  211. {
  212. }