gpio.c 11 KB

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