gpio.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (C) 2007, 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/autoconf.h>
  30. #include <linux/init.h>
  31. #include <linux/io.h>
  32. #include <linux/types.h>
  33. #include <linux/module.h>
  34. #include <asm/addrspace.h>
  35. #include <asm/mach-au1x00/au1000.h>
  36. #include <asm/gpio.h>
  37. #define gpio1 sys
  38. #if !defined(CONFIG_SOC_AU1000)
  39. static struct au1x00_gpio2 *const gpio2 = (struct au1x00_gpio2 *) GPIO2_BASE;
  40. #define GPIO2_OUTPUT_ENABLE_MASK 0x00010000
  41. static int au1xxx_gpio2_read(unsigned gpio)
  42. {
  43. gpio -= AU1XXX_GPIO_BASE;
  44. return ((gpio2->pinstate >> gpio) & 0x01);
  45. }
  46. static void au1xxx_gpio2_write(unsigned gpio, int value)
  47. {
  48. gpio -= AU1XXX_GPIO_BASE;
  49. gpio2->output = (GPIO2_OUTPUT_ENABLE_MASK << gpio) | (value << gpio);
  50. }
  51. static int au1xxx_gpio2_direction_input(unsigned gpio)
  52. {
  53. gpio -= AU1XXX_GPIO_BASE;
  54. gpio2->dir &= ~(0x01 << gpio);
  55. return 0;
  56. }
  57. static int au1xxx_gpio2_direction_output(unsigned gpio, int value)
  58. {
  59. gpio -= AU1XXX_GPIO_BASE;
  60. gpio2->dir = (0x01 << gpio) | (value << gpio);
  61. return 0;
  62. }
  63. #endif /* !defined(CONFIG_SOC_AU1000) */
  64. static int au1xxx_gpio1_read(unsigned gpio)
  65. {
  66. return ((gpio1->pinstaterd >> gpio) & 0x01);
  67. }
  68. static void au1xxx_gpio1_write(unsigned gpio, int value)
  69. {
  70. if (value)
  71. gpio1->outputset = (0x01 << gpio);
  72. else
  73. /* Output a zero */
  74. gpio1->outputclr = (0x01 << gpio);
  75. }
  76. static int au1xxx_gpio1_direction_input(unsigned gpio)
  77. {
  78. gpio1->pininputen = (0x01 << gpio);
  79. return 0;
  80. }
  81. static int au1xxx_gpio1_direction_output(unsigned gpio, int value)
  82. {
  83. gpio1->trioutclr = (0x01 & gpio);
  84. return 0;
  85. }
  86. int au1xxx_gpio_get_value(unsigned gpio)
  87. {
  88. if (gpio >= AU1XXX_GPIO_BASE)
  89. #if defined(CONFIG_SOC_AU1000)
  90. return 0;
  91. #else
  92. return au1xxx_gpio2_read(gpio);
  93. #endif
  94. else
  95. return au1xxx_gpio1_read(gpio);
  96. }
  97. EXPORT_SYMBOL(au1xxx_gpio_get_value);
  98. void au1xxx_gpio_set_value(unsigned gpio, int value)
  99. {
  100. if (gpio >= AU1XXX_GPIO_BASE)
  101. #if defined(CONFIG_SOC_AU1000)
  102. ;
  103. #else
  104. au1xxx_gpio2_write(gpio, value);
  105. #endif
  106. else
  107. au1xxx_gpio1_write(gpio, value);
  108. }
  109. EXPORT_SYMBOL(au1xxx_gpio_set_value);
  110. int au1xxx_gpio_direction_input(unsigned gpio)
  111. {
  112. if (gpio >= AU1XXX_GPIO_BASE)
  113. #if defined(CONFIG_SOC_AU1000)
  114. return -ENODEV;
  115. #else
  116. return au1xxx_gpio2_direction_input(gpio);
  117. #endif
  118. return au1xxx_gpio1_direction_input(gpio);
  119. }
  120. EXPORT_SYMBOL(au1xxx_gpio_direction_input);
  121. int au1xxx_gpio_direction_output(unsigned gpio, int value)
  122. {
  123. if (gpio >= AU1XXX_GPIO_BASE)
  124. #if defined(CONFIG_SOC_AU1000)
  125. return -ENODEV;
  126. #else
  127. return au1xxx_gpio2_direction_output(gpio, value);
  128. #endif
  129. return au1xxx_gpio1_direction_output(gpio, value);
  130. }
  131. EXPORT_SYMBOL(au1xxx_gpio_direction_output);