gpio.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * linux/arch/arm/mach-pxa/gpio.c
  3. *
  4. * Generic PXA GPIO handling
  5. *
  6. * Author: Nicolas Pitre
  7. * Created: Jun 15, 2001
  8. * Copyright: MontaVista Software Inc.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <asm/gpio.h>
  17. #include <asm/hardware.h>
  18. #include <asm/io.h>
  19. #include <asm/arch/pxa-regs.h>
  20. #include "generic.h"
  21. struct pxa_gpio_chip {
  22. struct gpio_chip chip;
  23. void __iomem *regbase;
  24. };
  25. int pxa_last_gpio;
  26. /*
  27. * Configure pins for GPIO or other functions
  28. */
  29. int pxa_gpio_mode(int gpio_mode)
  30. {
  31. unsigned long flags;
  32. int gpio = gpio_mode & GPIO_MD_MASK_NR;
  33. int fn = (gpio_mode & GPIO_MD_MASK_FN) >> 8;
  34. int gafr;
  35. if (gpio > pxa_last_gpio)
  36. return -EINVAL;
  37. local_irq_save(flags);
  38. if (gpio_mode & GPIO_DFLT_LOW)
  39. GPCR(gpio) = GPIO_bit(gpio);
  40. else if (gpio_mode & GPIO_DFLT_HIGH)
  41. GPSR(gpio) = GPIO_bit(gpio);
  42. if (gpio_mode & GPIO_MD_MASK_DIR)
  43. GPDR(gpio) |= GPIO_bit(gpio);
  44. else
  45. GPDR(gpio) &= ~GPIO_bit(gpio);
  46. gafr = GAFR(gpio) & ~(0x3 << (((gpio) & 0xf)*2));
  47. GAFR(gpio) = gafr | (fn << (((gpio) & 0xf)*2));
  48. local_irq_restore(flags);
  49. return 0;
  50. }
  51. EXPORT_SYMBOL(pxa_gpio_mode);
  52. static int pxa_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  53. {
  54. unsigned long flags;
  55. u32 mask = 1 << offset;
  56. u32 value;
  57. struct pxa_gpio_chip *pxa;
  58. void __iomem *gpdr;
  59. pxa = container_of(chip, struct pxa_gpio_chip, chip);
  60. gpdr = pxa->regbase + GPDR_OFFSET;
  61. local_irq_save(flags);
  62. value = __raw_readl(gpdr);
  63. value &= ~mask;
  64. __raw_writel(value, gpdr);
  65. local_irq_restore(flags);
  66. return 0;
  67. }
  68. static int pxa_gpio_direction_output(struct gpio_chip *chip,
  69. unsigned offset, int value)
  70. {
  71. unsigned long flags;
  72. u32 mask = 1 << offset;
  73. u32 tmp;
  74. struct pxa_gpio_chip *pxa;
  75. void __iomem *gpdr;
  76. pxa = container_of(chip, struct pxa_gpio_chip, chip);
  77. __raw_writel(mask,
  78. pxa->regbase + (value ? GPSR_OFFSET : GPCR_OFFSET));
  79. gpdr = pxa->regbase + GPDR_OFFSET;
  80. local_irq_save(flags);
  81. tmp = __raw_readl(gpdr);
  82. tmp |= mask;
  83. __raw_writel(tmp, gpdr);
  84. local_irq_restore(flags);
  85. return 0;
  86. }
  87. /*
  88. * Return GPIO level
  89. */
  90. static int pxa_gpio_get(struct gpio_chip *chip, unsigned offset)
  91. {
  92. u32 mask = 1 << offset;
  93. struct pxa_gpio_chip *pxa;
  94. pxa = container_of(chip, struct pxa_gpio_chip, chip);
  95. return __raw_readl(pxa->regbase + GPLR_OFFSET) & mask;
  96. }
  97. /*
  98. * Set output GPIO level
  99. */
  100. static void pxa_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  101. {
  102. u32 mask = 1 << offset;
  103. struct pxa_gpio_chip *pxa;
  104. pxa = container_of(chip, struct pxa_gpio_chip, chip);
  105. if (value)
  106. __raw_writel(mask, pxa->regbase + GPSR_OFFSET);
  107. else
  108. __raw_writel(mask, pxa->regbase + GPCR_OFFSET);
  109. }
  110. static struct pxa_gpio_chip pxa_gpio_chip[] = {
  111. [0] = {
  112. .regbase = GPIO0_BASE,
  113. .chip = {
  114. .label = "gpio-0",
  115. .direction_input = pxa_gpio_direction_input,
  116. .direction_output = pxa_gpio_direction_output,
  117. .get = pxa_gpio_get,
  118. .set = pxa_gpio_set,
  119. .base = 0,
  120. .ngpio = 32,
  121. },
  122. },
  123. [1] = {
  124. .regbase = GPIO1_BASE,
  125. .chip = {
  126. .label = "gpio-1",
  127. .direction_input = pxa_gpio_direction_input,
  128. .direction_output = pxa_gpio_direction_output,
  129. .get = pxa_gpio_get,
  130. .set = pxa_gpio_set,
  131. .base = 32,
  132. .ngpio = 32,
  133. },
  134. },
  135. [2] = {
  136. .regbase = GPIO2_BASE,
  137. .chip = {
  138. .label = "gpio-2",
  139. .direction_input = pxa_gpio_direction_input,
  140. .direction_output = pxa_gpio_direction_output,
  141. .get = pxa_gpio_get,
  142. .set = pxa_gpio_set,
  143. .base = 64,
  144. .ngpio = 32, /* 21 for PXA25x */
  145. },
  146. },
  147. #if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
  148. [3] = {
  149. .regbase = GPIO3_BASE,
  150. .chip = {
  151. .label = "gpio-3",
  152. .direction_input = pxa_gpio_direction_input,
  153. .direction_output = pxa_gpio_direction_output,
  154. .get = pxa_gpio_get,
  155. .set = pxa_gpio_set,
  156. .base = 96,
  157. .ngpio = 32,
  158. },
  159. },
  160. #endif
  161. };
  162. void __init pxa_init_gpio(int gpio_nr)
  163. {
  164. int i;
  165. /* add a GPIO chip for each register bank.
  166. * the last PXA25x register only contains 21 GPIOs
  167. */
  168. for (i = 0; i < gpio_nr; i += 32) {
  169. if (i+32 > gpio_nr)
  170. pxa_gpio_chip[i/32].chip.ngpio = gpio_nr - i;
  171. gpiochip_add(&pxa_gpio_chip[i/32].chip);
  172. }
  173. }