gpio.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * GPIO support for RDC SoC R3210/R8610
  3. *
  4. * Copyright (C) 2007, Florian Fainelli <florian@openwrt.org>
  5. * Copyright (C) 2008, Volker Weiss <dev@tintuc.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. */
  22. #include <linux/spinlock.h>
  23. #include <linux/io.h>
  24. #include <linux/types.h>
  25. #include <linux/module.h>
  26. #include <asm/gpio.h>
  27. #include <asm/mach-rdc321x/rdc321x_defs.h>
  28. /* spin lock to protect our private copy of GPIO data register plus
  29. the access to PCI conf registers. */
  30. static DEFINE_SPINLOCK(gpio_lock);
  31. /* copy of GPIO data registers */
  32. static u32 gpio_data_reg1;
  33. static u32 gpio_data_reg2;
  34. static u32 gpio_request_data[2];
  35. static inline void rdc321x_conf_write(unsigned addr, u32 value)
  36. {
  37. outl((1 << 31) | (7 << 11) | addr, RDC3210_CFGREG_ADDR);
  38. outl(value, RDC3210_CFGREG_DATA);
  39. }
  40. static inline void rdc321x_conf_or(unsigned addr, u32 value)
  41. {
  42. outl((1 << 31) | (7 << 11) | addr, RDC3210_CFGREG_ADDR);
  43. value |= inl(RDC3210_CFGREG_DATA);
  44. outl(value, RDC3210_CFGREG_DATA);
  45. }
  46. static inline u32 rdc321x_conf_read(unsigned addr)
  47. {
  48. outl((1 << 31) | (7 << 11) | addr, RDC3210_CFGREG_ADDR);
  49. return inl(RDC3210_CFGREG_DATA);
  50. }
  51. /* configure pin as GPIO */
  52. static void rdc321x_configure_gpio(unsigned gpio)
  53. {
  54. unsigned long flags;
  55. spin_lock_irqsave(&gpio_lock, flags);
  56. rdc321x_conf_or(gpio < 32
  57. ? RDC321X_GPIO_CTRL_REG1 : RDC321X_GPIO_CTRL_REG2,
  58. 1 << (gpio & 0x1f));
  59. spin_unlock_irqrestore(&gpio_lock, flags);
  60. }
  61. /* initially setup the 2 copies of the gpio data registers.
  62. This function must be called by the platform setup code. */
  63. void __init rdc321x_gpio_setup()
  64. {
  65. /* this might not be, what others (BIOS, bootloader, etc.)
  66. wrote to these registers before, but it's a good guess. Still
  67. better than just using 0xffffffff. */
  68. gpio_data_reg1 = rdc321x_conf_read(RDC321X_GPIO_DATA_REG1);
  69. gpio_data_reg2 = rdc321x_conf_read(RDC321X_GPIO_DATA_REG2);
  70. }
  71. /* determine, if gpio number is valid */
  72. static inline int rdc321x_is_gpio(unsigned gpio)
  73. {
  74. return gpio <= RDC321X_MAX_GPIO;
  75. }
  76. /* request GPIO */
  77. int rdc_gpio_request(unsigned gpio, const char *label)
  78. {
  79. unsigned long flags;
  80. if (!rdc321x_is_gpio(gpio))
  81. return -EINVAL;
  82. spin_lock_irqsave(&gpio_lock, flags);
  83. if (gpio_request_data[(gpio & 0x20) ? 1 : 0] & (1 << (gpio & 0x1f)))
  84. goto inuse;
  85. gpio_request_data[(gpio & 0x20) ? 1 : 0] |= (1 << (gpio & 0x1f));
  86. spin_unlock_irqrestore(&gpio_lock, flags);
  87. return 0;
  88. inuse:
  89. spin_unlock_irqrestore(&gpio_lock, flags);
  90. return -EINVAL;
  91. }
  92. EXPORT_SYMBOL(rdc_gpio_request);
  93. /* release previously-claimed GPIO */
  94. void rdc_gpio_free(unsigned gpio)
  95. {
  96. unsigned long flags;
  97. if (!rdc321x_is_gpio(gpio))
  98. return;
  99. spin_lock_irqsave(&gpio_lock, flags);
  100. gpio_request_data[(gpio & 0x20) ? 1 : 0] &= ~(1 << (gpio & 0x1f));
  101. spin_unlock_irqrestore(&gpio_lock, flags);
  102. }
  103. EXPORT_SYMBOL(rdc_gpio_free);
  104. /* read GPIO pin */
  105. int rdc_gpio_get_value(unsigned gpio)
  106. {
  107. u32 reg;
  108. unsigned long flags;
  109. spin_lock_irqsave(&gpio_lock, flags);
  110. reg = rdc321x_conf_read(gpio < 32
  111. ? RDC321X_GPIO_DATA_REG1 : RDC321X_GPIO_DATA_REG2);
  112. spin_unlock_irqrestore(&gpio_lock, flags);
  113. return (1 << (gpio & 0x1f)) & reg ? 1 : 0;
  114. }
  115. EXPORT_SYMBOL(rdc_gpio_get_value);
  116. /* set GPIO pin to value */
  117. void rdc_gpio_set_value(unsigned gpio, int value)
  118. {
  119. unsigned long flags;
  120. u32 reg;
  121. reg = 1 << (gpio & 0x1f);
  122. if (gpio < 32) {
  123. spin_lock_irqsave(&gpio_lock, flags);
  124. if (value)
  125. gpio_data_reg1 |= reg;
  126. else
  127. gpio_data_reg1 &= ~reg;
  128. rdc321x_conf_write(RDC321X_GPIO_DATA_REG1, gpio_data_reg1);
  129. spin_unlock_irqrestore(&gpio_lock, flags);
  130. } else {
  131. spin_lock_irqsave(&gpio_lock, flags);
  132. if (value)
  133. gpio_data_reg2 |= reg;
  134. else
  135. gpio_data_reg2 &= ~reg;
  136. rdc321x_conf_write(RDC321X_GPIO_DATA_REG2, gpio_data_reg2);
  137. spin_unlock_irqrestore(&gpio_lock, flags);
  138. }
  139. }
  140. EXPORT_SYMBOL(rdc_gpio_set_value);
  141. /* configure GPIO pin as input */
  142. int rdc_gpio_direction_input(unsigned gpio)
  143. {
  144. if (!rdc321x_is_gpio(gpio))
  145. return -EINVAL;
  146. rdc321x_configure_gpio(gpio);
  147. return 0;
  148. }
  149. EXPORT_SYMBOL(rdc_gpio_direction_input);
  150. /* configure GPIO pin as output and set value */
  151. int rdc_gpio_direction_output(unsigned gpio, int value)
  152. {
  153. if (!rdc321x_is_gpio(gpio))
  154. return -EINVAL;
  155. gpio_set_value(gpio, value);
  156. rdc321x_configure_gpio(gpio);
  157. return 0;
  158. }
  159. EXPORT_SYMBOL(rdc_gpio_direction_output);