gpio.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (C) 2007-2009, OpenWrt.org, Florian Fainelli <florian@openwrt.org>
  3. * Architecture specific GPIO support
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  11. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  13. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  14. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  15. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  16. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  17. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  18. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  19. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 675 Mass Ave, Cambridge, MA 02139, USA.
  24. *
  25. * Notes :
  26. * au1000 SoC have only one GPIO line : GPIO1
  27. * others have a second one : GPIO2
  28. */
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/types.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/gpio.h>
  34. #include <asm/mach-au1x00/au1000.h>
  35. #include <asm/gpio.h>
  36. struct au1000_gpio_chip {
  37. struct gpio_chip chip;
  38. void __iomem *regbase;
  39. };
  40. #if !defined(CONFIG_SOC_AU1000)
  41. static int au1000_gpio2_get(struct gpio_chip *chip, unsigned offset)
  42. {
  43. u32 mask = 1 << offset;
  44. struct au1000_gpio_chip *gpch;
  45. gpch = container_of(chip, struct au1000_gpio_chip, chip);
  46. return readl(gpch->regbase + AU1000_GPIO2_ST) & mask;
  47. }
  48. static void au1000_gpio2_set(struct gpio_chip *chip,
  49. unsigned offset, int value)
  50. {
  51. u32 mask = ((GPIO2_OUT_EN_MASK << offset) | (!!value << offset));
  52. struct au1000_gpio_chip *gpch;
  53. unsigned long flags;
  54. gpch = container_of(chip, struct au1000_gpio_chip, chip);
  55. local_irq_save(flags);
  56. writel(mask, gpch->regbase + AU1000_GPIO2_OUT);
  57. local_irq_restore(flags);
  58. }
  59. static int au1000_gpio2_direction_input(struct gpio_chip *chip, unsigned offset)
  60. {
  61. u32 mask = 1 << offset;
  62. u32 tmp;
  63. struct au1000_gpio_chip *gpch;
  64. unsigned long flags;
  65. gpch = container_of(chip, struct au1000_gpio_chip, chip);
  66. local_irq_save(flags);
  67. tmp = readl(gpch->regbase + AU1000_GPIO2_DIR);
  68. tmp &= ~mask;
  69. writel(tmp, gpch->regbase + AU1000_GPIO2_DIR);
  70. local_irq_restore(flags);
  71. return 0;
  72. }
  73. static int au1000_gpio2_direction_output(struct gpio_chip *chip,
  74. unsigned offset, int value)
  75. {
  76. u32 mask = 1 << offset;
  77. u32 out_mask = ((GPIO2_OUT_EN_MASK << offset) | (!!value << offset));
  78. u32 tmp;
  79. struct au1000_gpio_chip *gpch;
  80. unsigned long flags;
  81. gpch = container_of(chip, struct au1000_gpio_chip, chip);
  82. local_irq_save(flags);
  83. tmp = readl(gpch->regbase + AU1000_GPIO2_DIR);
  84. tmp |= mask;
  85. writel(tmp, gpch->regbase + AU1000_GPIO2_DIR);
  86. writel(out_mask, gpch->regbase + AU1000_GPIO2_OUT);
  87. local_irq_restore(flags);
  88. return 0;
  89. }
  90. #endif /* !defined(CONFIG_SOC_AU1000) */
  91. static int au1000_gpio1_get(struct gpio_chip *chip, unsigned offset)
  92. {
  93. u32 mask = 1 << offset;
  94. struct au1000_gpio_chip *gpch;
  95. gpch = container_of(chip, struct au1000_gpio_chip, chip);
  96. return readl(gpch->regbase + AU1000_GPIO1_ST) & mask;
  97. }
  98. static void au1000_gpio1_set(struct gpio_chip *chip,
  99. unsigned offset, int value)
  100. {
  101. u32 mask = 1 << offset;
  102. u32 reg_offset;
  103. struct au1000_gpio_chip *gpch;
  104. unsigned long flags;
  105. gpch = container_of(chip, struct au1000_gpio_chip, chip);
  106. if (value)
  107. reg_offset = AU1000_GPIO1_OUT;
  108. else
  109. reg_offset = AU1000_GPIO1_CLR;
  110. local_irq_save(flags);
  111. writel(mask, gpch->regbase + reg_offset);
  112. local_irq_restore(flags);
  113. }
  114. static int au1000_gpio1_direction_input(struct gpio_chip *chip, unsigned offset)
  115. {
  116. u32 mask = 1 << offset;
  117. struct au1000_gpio_chip *gpch;
  118. gpch = container_of(chip, struct au1000_gpio_chip, chip);
  119. writel(mask, gpch->regbase + AU1000_GPIO1_ST);
  120. return 0;
  121. }
  122. static int au1000_gpio1_direction_output(struct gpio_chip *chip,
  123. unsigned offset, int value)
  124. {
  125. u32 mask = 1 << offset;
  126. struct au1000_gpio_chip *gpch;
  127. gpch = container_of(chip, struct au1000_gpio_chip, chip);
  128. writel(mask, gpch->regbase + AU1000_GPIO1_TRI_OUT);
  129. au1000_gpio1_set(chip, offset, value);
  130. return 0;
  131. }
  132. struct au1000_gpio_chip au1000_gpio_chip[] = {
  133. [0] = {
  134. .regbase = (void __iomem *)SYS_BASE,
  135. .chip = {
  136. .label = "au1000-gpio1",
  137. .direction_input = au1000_gpio1_direction_input,
  138. .direction_output = au1000_gpio1_direction_output,
  139. .get = au1000_gpio1_get,
  140. .set = au1000_gpio1_set,
  141. .base = 0,
  142. .ngpio = 32,
  143. },
  144. },
  145. #if !defined(CONFIG_SOC_AU1000)
  146. [1] = {
  147. .regbase = (void __iomem *)GPIO2_BASE,
  148. .chip = {
  149. .label = "au1000-gpio2",
  150. .direction_input = au1000_gpio2_direction_input,
  151. .direction_output = au1000_gpio2_direction_output,
  152. .get = au1000_gpio2_get,
  153. .set = au1000_gpio2_set,
  154. .base = AU1XXX_GPIO_BASE,
  155. .ngpio = 32,
  156. },
  157. },
  158. #endif
  159. };
  160. static int __init au1000_gpio_init(void)
  161. {
  162. gpiochip_add(&au1000_gpio_chip[0].chip);
  163. #if !defined(CONFIG_SOC_AU1000)
  164. gpiochip_add(&au1000_gpio_chip[1].chip);
  165. #endif
  166. return 0;
  167. }
  168. arch_initcall(au1000_gpio_init);