gpio.c 9.6 KB

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