gpio.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * arch/arm/mach-pxa/include/mach/gpio.h
  3. *
  4. * PXA GPIO wrappers for arch-neutral GPIO calls
  5. *
  6. * Written by Philipp Zabel <philipp.zabel@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #ifndef __ASM_ARCH_PXA_GPIO_H
  24. #define __ASM_ARCH_PXA_GPIO_H
  25. #include <mach/irqs.h>
  26. #include <mach/hardware.h>
  27. #include <asm-generic/gpio.h>
  28. #define GPIO_REGS_VIRT io_p2v(0x40E00000)
  29. #define BANK_OFF(n) (((n) < 3) ? (n) << 2 : 0x100 + (((n) - 3) << 2))
  30. #define GPIO_REG(x) (*(volatile u32 *)(GPIO_REGS_VIRT + (x)))
  31. /* GPIO Pin Level Registers */
  32. #define GPLR0 GPIO_REG(BANK_OFF(0) + 0x00)
  33. #define GPLR1 GPIO_REG(BANK_OFF(1) + 0x00)
  34. #define GPLR2 GPIO_REG(BANK_OFF(2) + 0x00)
  35. #define GPLR3 GPIO_REG(BANK_OFF(3) + 0x00)
  36. /* GPIO Pin Direction Registers */
  37. #define GPDR0 GPIO_REG(BANK_OFF(0) + 0x0c)
  38. #define GPDR1 GPIO_REG(BANK_OFF(1) + 0x0c)
  39. #define GPDR2 GPIO_REG(BANK_OFF(2) + 0x0c)
  40. #define GPDR3 GPIO_REG(BANK_OFF(3) + 0x0c)
  41. /* GPIO Pin Output Set Registers */
  42. #define GPSR0 GPIO_REG(BANK_OFF(0) + 0x18)
  43. #define GPSR1 GPIO_REG(BANK_OFF(1) + 0x18)
  44. #define GPSR2 GPIO_REG(BANK_OFF(2) + 0x18)
  45. #define GPSR3 GPIO_REG(BANK_OFF(3) + 0x18)
  46. /* GPIO Pin Output Clear Registers */
  47. #define GPCR0 GPIO_REG(BANK_OFF(0) + 0x24)
  48. #define GPCR1 GPIO_REG(BANK_OFF(1) + 0x24)
  49. #define GPCR2 GPIO_REG(BANK_OFF(2) + 0x24)
  50. #define GPCR3 GPIO_REG(BANK_OFF(3) + 0x24)
  51. /* GPIO Rising Edge Detect Registers */
  52. #define GRER0 GPIO_REG(BANK_OFF(0) + 0x30)
  53. #define GRER1 GPIO_REG(BANK_OFF(1) + 0x30)
  54. #define GRER2 GPIO_REG(BANK_OFF(2) + 0x30)
  55. #define GRER3 GPIO_REG(BANK_OFF(3) + 0x30)
  56. /* GPIO Falling Edge Detect Registers */
  57. #define GFER0 GPIO_REG(BANK_OFF(0) + 0x3c)
  58. #define GFER1 GPIO_REG(BANK_OFF(1) + 0x3c)
  59. #define GFER2 GPIO_REG(BANK_OFF(2) + 0x3c)
  60. #define GFER3 GPIO_REG(BANK_OFF(3) + 0x3c)
  61. /* GPIO Edge Detect Status Registers */
  62. #define GEDR0 GPIO_REG(BANK_OFF(0) + 0x48)
  63. #define GEDR1 GPIO_REG(BANK_OFF(1) + 0x48)
  64. #define GEDR2 GPIO_REG(BANK_OFF(2) + 0x48)
  65. #define GEDR3 GPIO_REG(BANK_OFF(3) + 0x48)
  66. /* GPIO Alternate Function Select Registers */
  67. #define GAFR0_L GPIO_REG(0x0054)
  68. #define GAFR0_U GPIO_REG(0x0058)
  69. #define GAFR1_L GPIO_REG(0x005C)
  70. #define GAFR1_U GPIO_REG(0x0060)
  71. #define GAFR2_L GPIO_REG(0x0064)
  72. #define GAFR2_U GPIO_REG(0x0068)
  73. #define GAFR3_L GPIO_REG(0x006C)
  74. #define GAFR3_U GPIO_REG(0x0070)
  75. /* More handy macros. The argument is a literal GPIO number. */
  76. #define GPIO_bit(x) (1 << ((x) & 0x1f))
  77. #define GPLR(x) GPIO_REG(BANK_OFF((x) >> 5) + 0x00)
  78. #define GPDR(x) GPIO_REG(BANK_OFF((x) >> 5) + 0x0c)
  79. #define GPSR(x) GPIO_REG(BANK_OFF((x) >> 5) + 0x18)
  80. #define GPCR(x) GPIO_REG(BANK_OFF((x) >> 5) + 0x24)
  81. #define GRER(x) GPIO_REG(BANK_OFF((x) >> 5) + 0x30)
  82. #define GFER(x) GPIO_REG(BANK_OFF((x) >> 5) + 0x3c)
  83. #define GEDR(x) GPIO_REG(BANK_OFF((x) >> 5) + 0x48)
  84. #define GAFR(x) GPIO_REG(0x54 + (((x) & 0x70) >> 2))
  85. #define NR_BUILTIN_GPIO 128
  86. #define gpio_to_bank(gpio) ((gpio) >> 5)
  87. #define gpio_to_irq(gpio) IRQ_GPIO(gpio)
  88. #define irq_to_gpio(irq) IRQ_TO_GPIO(irq)
  89. #ifdef CONFIG_CPU_PXA26x
  90. /* GPIO86/87/88/89 on PXA26x have their direction bits in GPDR2 inverted,
  91. * as well as their Alternate Function value being '1' for GPIO in GAFRx.
  92. */
  93. static inline int __gpio_is_inverted(unsigned gpio)
  94. {
  95. return cpu_is_pxa25x() && gpio > 85;
  96. }
  97. #else
  98. static inline int __gpio_is_inverted(unsigned gpio) { return 0; }
  99. #endif
  100. /*
  101. * On PXA25x and PXA27x, GAFRx and GPDRx together decide the alternate
  102. * function of a GPIO, and GPDRx cannot be altered once configured. It
  103. * is attributed as "occupied" here (I know this terminology isn't
  104. * accurate, you are welcome to propose a better one :-)
  105. */
  106. static inline int __gpio_is_occupied(unsigned gpio)
  107. {
  108. if (cpu_is_pxa27x() || cpu_is_pxa25x()) {
  109. int af = (GAFR(gpio) >> ((gpio & 0xf) * 2)) & 0x3;
  110. int dir = GPDR(gpio) & GPIO_bit(gpio);
  111. if (__gpio_is_inverted(gpio))
  112. return af != 1 || dir == 0;
  113. else
  114. return af != 0 || dir != 0;
  115. } else
  116. return GPDR(gpio) & GPIO_bit(gpio);
  117. }
  118. #include <plat/gpio.h>
  119. #endif