gpio.c 9.5 KB

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