gpio.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 <linux/irq.h>
  17. #include <asm/gpio.h>
  18. #include <asm/hardware.h>
  19. #include <asm/io.h>
  20. #include <asm/arch/pxa-regs.h>
  21. #include "generic.h"
  22. struct pxa_gpio_chip {
  23. struct gpio_chip chip;
  24. void __iomem *regbase;
  25. };
  26. int pxa_last_gpio;
  27. /*
  28. * Configure pins for GPIO or other functions
  29. */
  30. int pxa_gpio_mode(int gpio_mode)
  31. {
  32. unsigned long flags;
  33. int gpio = gpio_mode & GPIO_MD_MASK_NR;
  34. int fn = (gpio_mode & GPIO_MD_MASK_FN) >> 8;
  35. int gafr;
  36. if (gpio > pxa_last_gpio)
  37. return -EINVAL;
  38. local_irq_save(flags);
  39. if (gpio_mode & GPIO_DFLT_LOW)
  40. GPCR(gpio) = GPIO_bit(gpio);
  41. else if (gpio_mode & GPIO_DFLT_HIGH)
  42. GPSR(gpio) = GPIO_bit(gpio);
  43. if (gpio_mode & GPIO_MD_MASK_DIR)
  44. GPDR(gpio) |= GPIO_bit(gpio);
  45. else
  46. GPDR(gpio) &= ~GPIO_bit(gpio);
  47. gafr = GAFR(gpio) & ~(0x3 << (((gpio) & 0xf)*2));
  48. GAFR(gpio) = gafr | (fn << (((gpio) & 0xf)*2));
  49. local_irq_restore(flags);
  50. return 0;
  51. }
  52. EXPORT_SYMBOL(pxa_gpio_mode);
  53. static int pxa_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  54. {
  55. unsigned long flags;
  56. u32 mask = 1 << offset;
  57. u32 value;
  58. struct pxa_gpio_chip *pxa;
  59. void __iomem *gpdr;
  60. pxa = container_of(chip, struct pxa_gpio_chip, chip);
  61. gpdr = pxa->regbase + GPDR_OFFSET;
  62. local_irq_save(flags);
  63. value = __raw_readl(gpdr);
  64. value &= ~mask;
  65. __raw_writel(value, gpdr);
  66. local_irq_restore(flags);
  67. return 0;
  68. }
  69. static int pxa_gpio_direction_output(struct gpio_chip *chip,
  70. unsigned offset, int value)
  71. {
  72. unsigned long flags;
  73. u32 mask = 1 << offset;
  74. u32 tmp;
  75. struct pxa_gpio_chip *pxa;
  76. void __iomem *gpdr;
  77. pxa = container_of(chip, struct pxa_gpio_chip, chip);
  78. __raw_writel(mask,
  79. pxa->regbase + (value ? GPSR_OFFSET : GPCR_OFFSET));
  80. gpdr = pxa->regbase + GPDR_OFFSET;
  81. local_irq_save(flags);
  82. tmp = __raw_readl(gpdr);
  83. tmp |= mask;
  84. __raw_writel(tmp, gpdr);
  85. local_irq_restore(flags);
  86. return 0;
  87. }
  88. /*
  89. * Return GPIO level
  90. */
  91. static int pxa_gpio_get(struct gpio_chip *chip, unsigned offset)
  92. {
  93. u32 mask = 1 << offset;
  94. struct pxa_gpio_chip *pxa;
  95. pxa = container_of(chip, struct pxa_gpio_chip, chip);
  96. return __raw_readl(pxa->regbase + GPLR_OFFSET) & mask;
  97. }
  98. /*
  99. * Set output GPIO level
  100. */
  101. static void pxa_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  102. {
  103. u32 mask = 1 << offset;
  104. struct pxa_gpio_chip *pxa;
  105. pxa = container_of(chip, struct pxa_gpio_chip, chip);
  106. if (value)
  107. __raw_writel(mask, pxa->regbase + GPSR_OFFSET);
  108. else
  109. __raw_writel(mask, pxa->regbase + GPCR_OFFSET);
  110. }
  111. #define GPIO_CHIP(_n) \
  112. [_n] = { \
  113. .regbase = GPIO##_n##_BASE, \
  114. .chip = { \
  115. .label = "gpio-" #_n, \
  116. .direction_input = pxa_gpio_direction_input, \
  117. .direction_output = pxa_gpio_direction_output, \
  118. .get = pxa_gpio_get, \
  119. .set = pxa_gpio_set, \
  120. .base = (_n) * 32, \
  121. .ngpio = 32, \
  122. }, \
  123. }
  124. static struct pxa_gpio_chip pxa_gpio_chip[] = {
  125. GPIO_CHIP(0),
  126. GPIO_CHIP(1),
  127. GPIO_CHIP(2),
  128. #if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
  129. GPIO_CHIP(3),
  130. #endif
  131. };
  132. /*
  133. * PXA GPIO edge detection for IRQs:
  134. * IRQs are generated on Falling-Edge, Rising-Edge, or both.
  135. * Use this instead of directly setting GRER/GFER.
  136. */
  137. static long GPIO_IRQ_rising_edge[4];
  138. static long GPIO_IRQ_falling_edge[4];
  139. static long GPIO_IRQ_mask[4];
  140. static int pxa_gpio_irq_type(unsigned int irq, unsigned int type)
  141. {
  142. int gpio, idx;
  143. gpio = IRQ_TO_GPIO(irq);
  144. idx = gpio >> 5;
  145. if (type == IRQ_TYPE_PROBE) {
  146. /* Don't mess with enabled GPIOs using preconfigured edges or
  147. * GPIOs set to alternate function or to output during probe
  148. */
  149. if ((GPIO_IRQ_rising_edge[idx] |
  150. GPIO_IRQ_falling_edge[idx] |
  151. GPDR(gpio)) & GPIO_bit(gpio))
  152. return 0;
  153. if (GAFR(gpio) & (0x3 << (((gpio) & 0xf)*2)))
  154. return 0;
  155. type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
  156. }
  157. pxa_gpio_mode(gpio | GPIO_IN);
  158. if (type & IRQ_TYPE_EDGE_RISING)
  159. __set_bit(gpio, GPIO_IRQ_rising_edge);
  160. else
  161. __clear_bit(gpio, GPIO_IRQ_rising_edge);
  162. if (type & IRQ_TYPE_EDGE_FALLING)
  163. __set_bit(gpio, GPIO_IRQ_falling_edge);
  164. else
  165. __clear_bit(gpio, GPIO_IRQ_falling_edge);
  166. GRER(gpio) = GPIO_IRQ_rising_edge[idx] & GPIO_IRQ_mask[idx];
  167. GFER(gpio) = GPIO_IRQ_falling_edge[idx] & GPIO_IRQ_mask[idx];
  168. pr_debug("%s: IRQ%d (GPIO%d) - edge%s%s\n", __func__, irq, gpio,
  169. ((type & IRQ_TYPE_EDGE_RISING) ? " rising" : ""),
  170. ((type & IRQ_TYPE_EDGE_FALLING) ? " falling" : ""));
  171. return 0;
  172. }
  173. /*
  174. * GPIO IRQs must be acknowledged. This is for GPIO 0 and 1.
  175. */
  176. static void pxa_ack_low_gpio(unsigned int irq)
  177. {
  178. GEDR0 = (1 << (irq - IRQ_GPIO0));
  179. }
  180. static void pxa_mask_low_gpio(unsigned int irq)
  181. {
  182. ICMR &= ~(1 << (irq - PXA_IRQ(0)));
  183. }
  184. static void pxa_unmask_low_gpio(unsigned int irq)
  185. {
  186. ICMR |= 1 << (irq - PXA_IRQ(0));
  187. }
  188. static struct irq_chip pxa_low_gpio_chip = {
  189. .name = "GPIO-l",
  190. .ack = pxa_ack_low_gpio,
  191. .mask = pxa_mask_low_gpio,
  192. .unmask = pxa_unmask_low_gpio,
  193. .set_type = pxa_gpio_irq_type,
  194. };
  195. /*
  196. * Demux handler for GPIO>=2 edge detect interrupts
  197. */
  198. #define GEDR_BITS (sizeof(gedr) * BITS_PER_BYTE)
  199. static void pxa_gpio_demux_handler(unsigned int irq, struct irq_desc *desc)
  200. {
  201. int loop, bit, n;
  202. unsigned long gedr[4];
  203. do {
  204. gedr[0] = GEDR0 & GPIO_IRQ_mask[0] & ~3;
  205. gedr[1] = GEDR1 & GPIO_IRQ_mask[1];
  206. gedr[2] = GEDR2 & GPIO_IRQ_mask[2];
  207. gedr[3] = GEDR3 & GPIO_IRQ_mask[3];
  208. GEDR0 = gedr[0]; GEDR1 = gedr[1];
  209. GEDR2 = gedr[2]; GEDR3 = gedr[3];
  210. loop = 0;
  211. bit = find_first_bit(gedr, GEDR_BITS);
  212. while (bit < GEDR_BITS) {
  213. loop = 1;
  214. n = PXA_GPIO_IRQ_BASE + bit;
  215. desc_handle_irq(n, irq_desc + n);
  216. bit = find_next_bit(gedr, GEDR_BITS, bit + 1);
  217. }
  218. } while (loop);
  219. }
  220. static void pxa_ack_muxed_gpio(unsigned int irq)
  221. {
  222. int gpio = irq - IRQ_GPIO(2) + 2;
  223. GEDR(gpio) = GPIO_bit(gpio);
  224. }
  225. static void pxa_mask_muxed_gpio(unsigned int irq)
  226. {
  227. int gpio = irq - IRQ_GPIO(2) + 2;
  228. __clear_bit(gpio, GPIO_IRQ_mask);
  229. GRER(gpio) &= ~GPIO_bit(gpio);
  230. GFER(gpio) &= ~GPIO_bit(gpio);
  231. }
  232. static void pxa_unmask_muxed_gpio(unsigned int irq)
  233. {
  234. int gpio = irq - IRQ_GPIO(2) + 2;
  235. int idx = gpio >> 5;
  236. __set_bit(gpio, GPIO_IRQ_mask);
  237. GRER(gpio) = GPIO_IRQ_rising_edge[idx] & GPIO_IRQ_mask[idx];
  238. GFER(gpio) = GPIO_IRQ_falling_edge[idx] & GPIO_IRQ_mask[idx];
  239. }
  240. static struct irq_chip pxa_muxed_gpio_chip = {
  241. .name = "GPIO",
  242. .ack = pxa_ack_muxed_gpio,
  243. .mask = pxa_mask_muxed_gpio,
  244. .unmask = pxa_unmask_muxed_gpio,
  245. .set_type = pxa_gpio_irq_type,
  246. };
  247. void __init pxa_init_gpio(int gpio_nr, set_wake_t fn)
  248. {
  249. int irq, i, gpio;
  250. pxa_last_gpio = gpio_nr - 1;
  251. /* clear all GPIO edge detects */
  252. for (i = 0; i < gpio_nr; i += 32) {
  253. GFER(i) = 0;
  254. GRER(i) = 0;
  255. GEDR(i) = GEDR(i);
  256. }
  257. /* GPIO 0 and 1 must have their mask bit always set */
  258. GPIO_IRQ_mask[0] = 3;
  259. for (irq = IRQ_GPIO0; irq <= IRQ_GPIO1; irq++) {
  260. set_irq_chip(irq, &pxa_low_gpio_chip);
  261. set_irq_handler(irq, handle_edge_irq);
  262. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  263. }
  264. for (irq = IRQ_GPIO(2); irq < IRQ_GPIO(gpio_nr); irq++) {
  265. set_irq_chip(irq, &pxa_muxed_gpio_chip);
  266. set_irq_handler(irq, handle_edge_irq);
  267. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  268. }
  269. /* Install handler for GPIO>=2 edge detect interrupts */
  270. set_irq_chained_handler(IRQ_GPIO_2_x, pxa_gpio_demux_handler);
  271. pxa_low_gpio_chip.set_wake = fn;
  272. pxa_muxed_gpio_chip.set_wake = fn;
  273. /* add a GPIO chip for each register bank.
  274. * the last PXA25x register only contains 21 GPIOs
  275. */
  276. for (gpio = 0, i = 0; gpio < gpio_nr; gpio += 32, i++) {
  277. if (gpio + 32 > gpio_nr)
  278. pxa_gpio_chip[i].chip.ngpio = gpio_nr - gpio;
  279. gpiochip_add(&pxa_gpio_chip[i].chip);
  280. }
  281. }