omap_gpio.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (c) 2009 Wind River Systems, Inc.
  3. * Tom Rix <Tom.Rix@windriver.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0
  6. *
  7. * This work is derived from the linux 2.6.27 kernel source
  8. * To fetch, use the kernel repository
  9. * git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
  10. * Use the v2.6.27 tag.
  11. *
  12. * Below is the original's header including its copyright
  13. *
  14. * linux/arch/arm/plat-omap/gpio.c
  15. *
  16. * Support functions for OMAP GPIO
  17. *
  18. * Copyright (C) 2003-2005 Nokia Corporation
  19. * Written by Juha Yrjölä <juha.yrjola@nokia.com>
  20. */
  21. #include <common.h>
  22. #include <asm/gpio.h>
  23. #include <asm/io.h>
  24. #include <asm/errno.h>
  25. #define OMAP_GPIO_DIR_OUT 0
  26. #define OMAP_GPIO_DIR_IN 1
  27. static inline const struct gpio_bank *get_gpio_bank(int gpio)
  28. {
  29. return &omap_gpio_bank[gpio >> 5];
  30. }
  31. static inline int get_gpio_index(int gpio)
  32. {
  33. return gpio & 0x1f;
  34. }
  35. int gpio_is_valid(int gpio)
  36. {
  37. return (gpio >= 0) && (gpio < OMAP_MAX_GPIO);
  38. }
  39. static int check_gpio(int gpio)
  40. {
  41. if (!gpio_is_valid(gpio)) {
  42. printf("ERROR : check_gpio: invalid GPIO %d\n", gpio);
  43. return -1;
  44. }
  45. return 0;
  46. }
  47. static void _set_gpio_direction(const struct gpio_bank *bank, int gpio,
  48. int is_input)
  49. {
  50. void *reg = bank->base;
  51. u32 l;
  52. switch (bank->method) {
  53. case METHOD_GPIO_24XX:
  54. reg += OMAP_GPIO_OE;
  55. break;
  56. default:
  57. return;
  58. }
  59. l = __raw_readl(reg);
  60. if (is_input)
  61. l |= 1 << gpio;
  62. else
  63. l &= ~(1 << gpio);
  64. __raw_writel(l, reg);
  65. }
  66. /**
  67. * Get the direction of the GPIO by reading the GPIO_OE register
  68. * corresponding to the specified bank.
  69. */
  70. static int _get_gpio_direction(const struct gpio_bank *bank, int gpio)
  71. {
  72. void *reg = bank->base;
  73. u32 v;
  74. switch (bank->method) {
  75. case METHOD_GPIO_24XX:
  76. reg += OMAP_GPIO_OE;
  77. break;
  78. default:
  79. return -1;
  80. }
  81. v = __raw_readl(reg);
  82. if (v & (1 << gpio))
  83. return OMAP_GPIO_DIR_IN;
  84. else
  85. return OMAP_GPIO_DIR_OUT;
  86. }
  87. static void _set_gpio_dataout(const struct gpio_bank *bank, int gpio,
  88. int enable)
  89. {
  90. void *reg = bank->base;
  91. u32 l = 0;
  92. switch (bank->method) {
  93. case METHOD_GPIO_24XX:
  94. if (enable)
  95. reg += OMAP_GPIO_SETDATAOUT;
  96. else
  97. reg += OMAP_GPIO_CLEARDATAOUT;
  98. l = 1 << gpio;
  99. break;
  100. default:
  101. printf("omap3-gpio unknown bank method %s %d\n",
  102. __FILE__, __LINE__);
  103. return;
  104. }
  105. __raw_writel(l, reg);
  106. }
  107. /**
  108. * Set value of the specified gpio
  109. */
  110. int gpio_set_value(unsigned gpio, int value)
  111. {
  112. const struct gpio_bank *bank;
  113. if (check_gpio(gpio) < 0)
  114. return -1;
  115. bank = get_gpio_bank(gpio);
  116. _set_gpio_dataout(bank, get_gpio_index(gpio), value);
  117. return 0;
  118. }
  119. /**
  120. * Get value of the specified gpio
  121. */
  122. int gpio_get_value(unsigned gpio)
  123. {
  124. const struct gpio_bank *bank;
  125. void *reg;
  126. int input;
  127. if (check_gpio(gpio) < 0)
  128. return -1;
  129. bank = get_gpio_bank(gpio);
  130. reg = bank->base;
  131. switch (bank->method) {
  132. case METHOD_GPIO_24XX:
  133. input = _get_gpio_direction(bank, get_gpio_index(gpio));
  134. switch (input) {
  135. case OMAP_GPIO_DIR_IN:
  136. reg += OMAP_GPIO_DATAIN;
  137. break;
  138. case OMAP_GPIO_DIR_OUT:
  139. reg += OMAP_GPIO_DATAOUT;
  140. break;
  141. default:
  142. return -1;
  143. }
  144. break;
  145. default:
  146. return -1;
  147. }
  148. return (__raw_readl(reg)
  149. & (1 << get_gpio_index(gpio))) != 0;
  150. }
  151. /**
  152. * Set gpio direction as input
  153. */
  154. int gpio_direction_input(unsigned gpio)
  155. {
  156. const struct gpio_bank *bank;
  157. if (check_gpio(gpio) < 0)
  158. return -1;
  159. bank = get_gpio_bank(gpio);
  160. _set_gpio_direction(bank, get_gpio_index(gpio), 1);
  161. return 0;
  162. }
  163. /**
  164. * Set gpio direction as output
  165. */
  166. int gpio_direction_output(unsigned gpio, int value)
  167. {
  168. const struct gpio_bank *bank;
  169. if (check_gpio(gpio) < 0)
  170. return -1;
  171. bank = get_gpio_bank(gpio);
  172. _set_gpio_dataout(bank, get_gpio_index(gpio), value);
  173. _set_gpio_direction(bank, get_gpio_index(gpio), 0);
  174. return 0;
  175. }
  176. /**
  177. * Request a gpio before using it.
  178. *
  179. * NOTE: Argument 'label' is unused.
  180. */
  181. int gpio_request(unsigned gpio, const char *label)
  182. {
  183. if (check_gpio(gpio) < 0)
  184. return -1;
  185. return 0;
  186. }
  187. /**
  188. * Reset and free the gpio after using it.
  189. */
  190. int gpio_free(unsigned gpio)
  191. {
  192. return 0;
  193. }