gpio.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. static struct gpio_bank gpio_bank_34xx[6] = {
  43. { (void *)OMAP34XX_GPIO1_BASE, METHOD_GPIO_24XX },
  44. { (void *)OMAP34XX_GPIO2_BASE, METHOD_GPIO_24XX },
  45. { (void *)OMAP34XX_GPIO3_BASE, METHOD_GPIO_24XX },
  46. { (void *)OMAP34XX_GPIO4_BASE, METHOD_GPIO_24XX },
  47. { (void *)OMAP34XX_GPIO5_BASE, METHOD_GPIO_24XX },
  48. { (void *)OMAP34XX_GPIO6_BASE, METHOD_GPIO_24XX },
  49. };
  50. static struct gpio_bank *gpio_bank = &gpio_bank_34xx[0];
  51. static inline struct gpio_bank *get_gpio_bank(int gpio)
  52. {
  53. return &gpio_bank[gpio >> 5];
  54. }
  55. static inline int get_gpio_index(int gpio)
  56. {
  57. return gpio & 0x1f;
  58. }
  59. static inline int gpio_valid(int gpio)
  60. {
  61. if (gpio < 0)
  62. return -1;
  63. if (gpio < 192)
  64. return 0;
  65. return -1;
  66. }
  67. static int check_gpio(int gpio)
  68. {
  69. if (gpio_valid(gpio) < 0) {
  70. printf("ERROR : check_gpio: invalid GPIO %d\n", gpio);
  71. return -1;
  72. }
  73. return 0;
  74. }
  75. static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
  76. {
  77. void *reg = bank->base;
  78. u32 l;
  79. switch (bank->method) {
  80. case METHOD_GPIO_24XX:
  81. reg += OMAP24XX_GPIO_OE;
  82. break;
  83. default:
  84. return;
  85. }
  86. l = __raw_readl(reg);
  87. if (is_input)
  88. l |= 1 << gpio;
  89. else
  90. l &= ~(1 << gpio);
  91. __raw_writel(l, reg);
  92. }
  93. void omap_set_gpio_direction(int gpio, int is_input)
  94. {
  95. struct gpio_bank *bank;
  96. if (check_gpio(gpio) < 0)
  97. return;
  98. bank = get_gpio_bank(gpio);
  99. _set_gpio_direction(bank, get_gpio_index(gpio), is_input);
  100. }
  101. static void _set_gpio_dataout(struct gpio_bank *bank, int gpio, int enable)
  102. {
  103. void *reg = bank->base;
  104. u32 l = 0;
  105. switch (bank->method) {
  106. case METHOD_GPIO_24XX:
  107. if (enable)
  108. reg += OMAP24XX_GPIO_SETDATAOUT;
  109. else
  110. reg += OMAP24XX_GPIO_CLEARDATAOUT;
  111. l = 1 << gpio;
  112. break;
  113. default:
  114. printf("omap3-gpio unknown bank method %s %d\n",
  115. __FILE__, __LINE__);
  116. return;
  117. }
  118. __raw_writel(l, reg);
  119. }
  120. void omap_set_gpio_dataout(int gpio, int enable)
  121. {
  122. struct gpio_bank *bank;
  123. if (check_gpio(gpio) < 0)
  124. return;
  125. bank = get_gpio_bank(gpio);
  126. _set_gpio_dataout(bank, get_gpio_index(gpio), enable);
  127. }
  128. int omap_get_gpio_datain(int gpio)
  129. {
  130. struct gpio_bank *bank;
  131. void *reg;
  132. if (check_gpio(gpio) < 0)
  133. return -EINVAL;
  134. bank = get_gpio_bank(gpio);
  135. reg = bank->base;
  136. switch (bank->method) {
  137. case METHOD_GPIO_24XX:
  138. reg += OMAP24XX_GPIO_DATAIN;
  139. break;
  140. default:
  141. return -EINVAL;
  142. }
  143. return (__raw_readl(reg)
  144. & (1 << get_gpio_index(gpio))) != 0;
  145. }
  146. static void _reset_gpio(struct gpio_bank *bank, int gpio)
  147. {
  148. _set_gpio_direction(bank, get_gpio_index(gpio), 1);
  149. }
  150. int omap_request_gpio(int gpio)
  151. {
  152. if (check_gpio(gpio) < 0)
  153. return -EINVAL;
  154. return 0;
  155. }
  156. void omap_free_gpio(int gpio)
  157. {
  158. struct gpio_bank *bank;
  159. if (check_gpio(gpio) < 0)
  160. return;
  161. bank = get_gpio_bank(gpio);
  162. _reset_gpio(bank, gpio);
  163. }