gpio.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. void __init pxa_init_gpio(int gpio_nr)
  133. {
  134. int i;
  135. /* add a GPIO chip for each register bank.
  136. * the last PXA25x register only contains 21 GPIOs
  137. */
  138. for (i = 0; i < gpio_nr; i += 32) {
  139. if (i+32 > gpio_nr)
  140. pxa_gpio_chip[i/32].chip.ngpio = gpio_nr - i;
  141. gpiochip_add(&pxa_gpio_chip[i/32].chip);
  142. }
  143. }
  144. /*
  145. * PXA GPIO edge detection for IRQs:
  146. * IRQs are generated on Falling-Edge, Rising-Edge, or both.
  147. * Use this instead of directly setting GRER/GFER.
  148. */
  149. static long GPIO_IRQ_rising_edge[4];
  150. static long GPIO_IRQ_falling_edge[4];
  151. static long GPIO_IRQ_mask[4];
  152. static int pxa_gpio_irq_type(unsigned int irq, unsigned int type)
  153. {
  154. int gpio, idx;
  155. gpio = IRQ_TO_GPIO(irq);
  156. idx = gpio >> 5;
  157. if (type == IRQ_TYPE_PROBE) {
  158. /* Don't mess with enabled GPIOs using preconfigured edges or
  159. * GPIOs set to alternate function or to output during probe
  160. */
  161. if ((GPIO_IRQ_rising_edge[idx] |
  162. GPIO_IRQ_falling_edge[idx] |
  163. GPDR(gpio)) & GPIO_bit(gpio))
  164. return 0;
  165. if (GAFR(gpio) & (0x3 << (((gpio) & 0xf)*2)))
  166. return 0;
  167. type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
  168. }
  169. pxa_gpio_mode(gpio | GPIO_IN);
  170. if (type & IRQ_TYPE_EDGE_RISING)
  171. __set_bit(gpio, GPIO_IRQ_rising_edge);
  172. else
  173. __clear_bit(gpio, GPIO_IRQ_rising_edge);
  174. if (type & IRQ_TYPE_EDGE_FALLING)
  175. __set_bit(gpio, GPIO_IRQ_falling_edge);
  176. else
  177. __clear_bit(gpio, GPIO_IRQ_falling_edge);
  178. GRER(gpio) = GPIO_IRQ_rising_edge[idx] & GPIO_IRQ_mask[idx];
  179. GFER(gpio) = GPIO_IRQ_falling_edge[idx] & GPIO_IRQ_mask[idx];
  180. pr_debug("%s: IRQ%d (GPIO%d) - edge%s%s\n", __func__, irq, gpio,
  181. ((type & IRQ_TYPE_EDGE_RISING) ? " rising" : ""),
  182. ((type & IRQ_TYPE_EDGE_FALLING) ? " falling" : ""));
  183. return 0;
  184. }
  185. /*
  186. * GPIO IRQs must be acknowledged. This is for GPIO 0 and 1.
  187. */
  188. static void pxa_ack_low_gpio(unsigned int irq)
  189. {
  190. GEDR0 = (1 << (irq - IRQ_GPIO0));
  191. }
  192. static void pxa_mask_low_gpio(unsigned int irq)
  193. {
  194. ICMR &= ~(1 << (irq - PXA_IRQ(0)));
  195. }
  196. static void pxa_unmask_low_gpio(unsigned int irq)
  197. {
  198. ICMR |= 1 << (irq - PXA_IRQ(0));
  199. }
  200. static struct irq_chip pxa_low_gpio_chip = {
  201. .name = "GPIO-l",
  202. .ack = pxa_ack_low_gpio,
  203. .mask = pxa_mask_low_gpio,
  204. .unmask = pxa_unmask_low_gpio,
  205. .set_type = pxa_gpio_irq_type,
  206. };
  207. /*
  208. * Demux handler for GPIO>=2 edge detect interrupts
  209. */
  210. #define GEDR_BITS (sizeof(gedr) * BITS_PER_BYTE)
  211. static void pxa_gpio_demux_handler(unsigned int irq, struct irq_desc *desc)
  212. {
  213. int loop, bit, n;
  214. unsigned long gedr[4];
  215. do {
  216. gedr[0] = GEDR0 & GPIO_IRQ_mask[0] & ~3;
  217. gedr[1] = GEDR1 & GPIO_IRQ_mask[1];
  218. gedr[2] = GEDR2 & GPIO_IRQ_mask[2];
  219. gedr[3] = GEDR3 & GPIO_IRQ_mask[3];
  220. GEDR0 = gedr[0]; GEDR1 = gedr[1];
  221. GEDR2 = gedr[2]; GEDR3 = gedr[3];
  222. loop = 0;
  223. bit = find_first_bit(gedr, GEDR_BITS);
  224. while (bit < GEDR_BITS) {
  225. loop = 1;
  226. n = PXA_GPIO_IRQ_BASE + bit;
  227. desc_handle_irq(n, irq_desc + n);
  228. bit = find_next_bit(gedr, GEDR_BITS, bit + 1);
  229. }
  230. } while (loop);
  231. }
  232. static void pxa_ack_muxed_gpio(unsigned int irq)
  233. {
  234. int gpio = irq - IRQ_GPIO(2) + 2;
  235. GEDR(gpio) = GPIO_bit(gpio);
  236. }
  237. static void pxa_mask_muxed_gpio(unsigned int irq)
  238. {
  239. int gpio = irq - IRQ_GPIO(2) + 2;
  240. __clear_bit(gpio, GPIO_IRQ_mask);
  241. GRER(gpio) &= ~GPIO_bit(gpio);
  242. GFER(gpio) &= ~GPIO_bit(gpio);
  243. }
  244. static void pxa_unmask_muxed_gpio(unsigned int irq)
  245. {
  246. int gpio = irq - IRQ_GPIO(2) + 2;
  247. int idx = gpio >> 5;
  248. __set_bit(gpio, GPIO_IRQ_mask);
  249. GRER(gpio) = GPIO_IRQ_rising_edge[idx] & GPIO_IRQ_mask[idx];
  250. GFER(gpio) = GPIO_IRQ_falling_edge[idx] & GPIO_IRQ_mask[idx];
  251. }
  252. static struct irq_chip pxa_muxed_gpio_chip = {
  253. .name = "GPIO",
  254. .ack = pxa_ack_muxed_gpio,
  255. .mask = pxa_mask_muxed_gpio,
  256. .unmask = pxa_unmask_muxed_gpio,
  257. .set_type = pxa_gpio_irq_type,
  258. };
  259. void __init pxa_init_irq_gpio(int gpio_nr)
  260. {
  261. int irq, i;
  262. pxa_last_gpio = gpio_nr - 1;
  263. /* clear all GPIO edge detects */
  264. for (i = 0; i < gpio_nr; i += 32) {
  265. GFER(i) = 0;
  266. GRER(i) = 0;
  267. GEDR(i) = GEDR(i);
  268. }
  269. /* GPIO 0 and 1 must have their mask bit always set */
  270. GPIO_IRQ_mask[0] = 3;
  271. for (irq = IRQ_GPIO0; irq <= IRQ_GPIO1; irq++) {
  272. set_irq_chip(irq, &pxa_low_gpio_chip);
  273. set_irq_handler(irq, handle_edge_irq);
  274. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  275. }
  276. for (irq = IRQ_GPIO(2); irq < IRQ_GPIO(gpio_nr); irq++) {
  277. set_irq_chip(irq, &pxa_muxed_gpio_chip);
  278. set_irq_handler(irq, handle_edge_irq);
  279. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  280. }
  281. /* Install handler for GPIO>=2 edge detect interrupts */
  282. set_irq_chained_handler(IRQ_GPIO_2_x, pxa_gpio_demux_handler);
  283. pxa_init_gpio(gpio_nr);
  284. }
  285. void __init pxa_init_gpio_set_wake(int (*set_wake)(unsigned int, unsigned int))
  286. {
  287. pxa_low_gpio_chip.set_wake = set_wake;
  288. pxa_muxed_gpio_chip.set_wake = set_wake;
  289. }