gpio.c 3.8 KB

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