gpio.c 8.3 KB

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