gpio-pxa.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * linux/arch/arm/plat-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/gpio.h>
  15. #include <linux/init.h>
  16. #include <linux/irq.h>
  17. #include <linux/io.h>
  18. #include <linux/syscore_ops.h>
  19. #include <linux/slab.h>
  20. #include <mach/gpio-pxa.h>
  21. int pxa_last_gpio;
  22. struct pxa_gpio_chip {
  23. struct gpio_chip chip;
  24. void __iomem *regbase;
  25. char label[10];
  26. unsigned long irq_mask;
  27. unsigned long irq_edge_rise;
  28. unsigned long irq_edge_fall;
  29. #ifdef CONFIG_PM
  30. unsigned long saved_gplr;
  31. unsigned long saved_gpdr;
  32. unsigned long saved_grer;
  33. unsigned long saved_gfer;
  34. #endif
  35. };
  36. enum {
  37. PXA25X_GPIO = 0,
  38. PXA26X_GPIO,
  39. PXA27X_GPIO,
  40. PXA3XX_GPIO,
  41. PXA93X_GPIO,
  42. MMP_GPIO = 0x10,
  43. MMP2_GPIO,
  44. };
  45. static DEFINE_SPINLOCK(gpio_lock);
  46. static struct pxa_gpio_chip *pxa_gpio_chips;
  47. static int gpio_type;
  48. #define for_each_gpio_chip(i, c) \
  49. for (i = 0, c = &pxa_gpio_chips[0]; i <= pxa_last_gpio; i += 32, c++)
  50. static inline void __iomem *gpio_chip_base(struct gpio_chip *c)
  51. {
  52. return container_of(c, struct pxa_gpio_chip, chip)->regbase;
  53. }
  54. static inline struct pxa_gpio_chip *gpio_to_pxachip(unsigned gpio)
  55. {
  56. return &pxa_gpio_chips[gpio_to_bank(gpio)];
  57. }
  58. static inline int gpio_is_pxa_type(int type)
  59. {
  60. return (type & MMP_GPIO) == 0;
  61. }
  62. static inline int gpio_is_mmp_type(int type)
  63. {
  64. return (type & MMP_GPIO) != 0;
  65. }
  66. #ifdef CONFIG_ARCH_PXA
  67. static inline int __pxa_gpio_to_irq(int gpio)
  68. {
  69. if (gpio_is_pxa_type(gpio_type))
  70. return PXA_GPIO_TO_IRQ(gpio);
  71. return -1;
  72. }
  73. static inline int __pxa_irq_to_gpio(int irq)
  74. {
  75. if (gpio_is_pxa_type(gpio_type))
  76. return irq - PXA_GPIO_TO_IRQ(0);
  77. return -1;
  78. }
  79. #else
  80. static inline int __pxa_gpio_to_irq(int gpio) { return -1; }
  81. static inline int __pxa_irq_to_gpio(int irq) { return -1; }
  82. #endif
  83. #ifdef CONFIG_ARCH_MMP
  84. static inline int __mmp_gpio_to_irq(int gpio)
  85. {
  86. if (gpio_is_mmp_type(gpio_type))
  87. return MMP_GPIO_TO_IRQ(gpio);
  88. return -1;
  89. }
  90. static inline int __mmp_irq_to_gpio(int irq)
  91. {
  92. if (gpio_is_mmp_type(gpio_type))
  93. return irq - MMP_GPIO_TO_IRQ(0);
  94. return -1;
  95. }
  96. #else
  97. static inline int __mmp_gpio_to_irq(int gpio) { return -1; }
  98. static inline int __mmp_irq_to_gpio(int irq) { return -1; }
  99. #endif
  100. static int pxa_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  101. {
  102. int gpio, ret;
  103. gpio = chip->base + offset;
  104. ret = __pxa_gpio_to_irq(gpio);
  105. if (ret >= 0)
  106. return ret;
  107. return __mmp_gpio_to_irq(gpio);
  108. }
  109. int pxa_irq_to_gpio(int irq)
  110. {
  111. int ret;
  112. ret = __pxa_irq_to_gpio(irq);
  113. if (ret >= 0)
  114. return ret;
  115. return __mmp_irq_to_gpio(irq);
  116. }
  117. static int pxa_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  118. {
  119. void __iomem *base = gpio_chip_base(chip);
  120. uint32_t value, mask = 1 << offset;
  121. unsigned long flags;
  122. spin_lock_irqsave(&gpio_lock, flags);
  123. value = __raw_readl(base + GPDR_OFFSET);
  124. if (__gpio_is_inverted(chip->base + offset))
  125. value |= mask;
  126. else
  127. value &= ~mask;
  128. __raw_writel(value, base + GPDR_OFFSET);
  129. spin_unlock_irqrestore(&gpio_lock, flags);
  130. return 0;
  131. }
  132. static int pxa_gpio_direction_output(struct gpio_chip *chip,
  133. unsigned offset, int value)
  134. {
  135. void __iomem *base = gpio_chip_base(chip);
  136. uint32_t tmp, mask = 1 << offset;
  137. unsigned long flags;
  138. __raw_writel(mask, base + (value ? GPSR_OFFSET : GPCR_OFFSET));
  139. spin_lock_irqsave(&gpio_lock, flags);
  140. tmp = __raw_readl(base + GPDR_OFFSET);
  141. if (__gpio_is_inverted(chip->base + offset))
  142. tmp &= ~mask;
  143. else
  144. tmp |= mask;
  145. __raw_writel(tmp, base + GPDR_OFFSET);
  146. spin_unlock_irqrestore(&gpio_lock, flags);
  147. return 0;
  148. }
  149. static int pxa_gpio_get(struct gpio_chip *chip, unsigned offset)
  150. {
  151. return __raw_readl(gpio_chip_base(chip) + GPLR_OFFSET) & (1 << offset);
  152. }
  153. static void pxa_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  154. {
  155. __raw_writel(1 << offset, gpio_chip_base(chip) +
  156. (value ? GPSR_OFFSET : GPCR_OFFSET));
  157. }
  158. static int __init pxa_init_gpio_chip(int gpio_end)
  159. {
  160. int i, gpio, nbanks = gpio_to_bank(gpio_end) + 1;
  161. struct pxa_gpio_chip *chips;
  162. chips = kzalloc(nbanks * sizeof(struct pxa_gpio_chip), GFP_KERNEL);
  163. if (chips == NULL) {
  164. pr_err("%s: failed to allocate GPIO chips\n", __func__);
  165. return -ENOMEM;
  166. }
  167. for (i = 0, gpio = 0; i < nbanks; i++, gpio += 32) {
  168. struct gpio_chip *c = &chips[i].chip;
  169. sprintf(chips[i].label, "gpio-%d", i);
  170. chips[i].regbase = GPIO_BANK(i);
  171. c->base = gpio;
  172. c->label = chips[i].label;
  173. c->direction_input = pxa_gpio_direction_input;
  174. c->direction_output = pxa_gpio_direction_output;
  175. c->get = pxa_gpio_get;
  176. c->set = pxa_gpio_set;
  177. c->to_irq = pxa_gpio_to_irq;
  178. /* number of GPIOs on last bank may be less than 32 */
  179. c->ngpio = (gpio + 31 > gpio_end) ? (gpio_end - gpio + 1) : 32;
  180. gpiochip_add(c);
  181. }
  182. pxa_gpio_chips = chips;
  183. return 0;
  184. }
  185. /* Update only those GRERx and GFERx edge detection register bits if those
  186. * bits are set in c->irq_mask
  187. */
  188. static inline void update_edge_detect(struct pxa_gpio_chip *c)
  189. {
  190. uint32_t grer, gfer;
  191. grer = __raw_readl(c->regbase + GRER_OFFSET) & ~c->irq_mask;
  192. gfer = __raw_readl(c->regbase + GFER_OFFSET) & ~c->irq_mask;
  193. grer |= c->irq_edge_rise & c->irq_mask;
  194. gfer |= c->irq_edge_fall & c->irq_mask;
  195. __raw_writel(grer, c->regbase + GRER_OFFSET);
  196. __raw_writel(gfer, c->regbase + GFER_OFFSET);
  197. }
  198. static int pxa_gpio_irq_type(struct irq_data *d, unsigned int type)
  199. {
  200. struct pxa_gpio_chip *c;
  201. int gpio = pxa_irq_to_gpio(d->irq);
  202. unsigned long gpdr, mask = GPIO_bit(gpio);
  203. c = gpio_to_pxachip(gpio);
  204. if (type == IRQ_TYPE_PROBE) {
  205. /* Don't mess with enabled GPIOs using preconfigured edges or
  206. * GPIOs set to alternate function or to output during probe
  207. */
  208. if ((c->irq_edge_rise | c->irq_edge_fall) & GPIO_bit(gpio))
  209. return 0;
  210. if (__gpio_is_occupied(gpio))
  211. return 0;
  212. type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
  213. }
  214. gpdr = __raw_readl(c->regbase + GPDR_OFFSET);
  215. if (__gpio_is_inverted(gpio))
  216. __raw_writel(gpdr | mask, c->regbase + GPDR_OFFSET);
  217. else
  218. __raw_writel(gpdr & ~mask, c->regbase + GPDR_OFFSET);
  219. if (type & IRQ_TYPE_EDGE_RISING)
  220. c->irq_edge_rise |= mask;
  221. else
  222. c->irq_edge_rise &= ~mask;
  223. if (type & IRQ_TYPE_EDGE_FALLING)
  224. c->irq_edge_fall |= mask;
  225. else
  226. c->irq_edge_fall &= ~mask;
  227. update_edge_detect(c);
  228. pr_debug("%s: IRQ%d (GPIO%d) - edge%s%s\n", __func__, d->irq, gpio,
  229. ((type & IRQ_TYPE_EDGE_RISING) ? " rising" : ""),
  230. ((type & IRQ_TYPE_EDGE_FALLING) ? " falling" : ""));
  231. return 0;
  232. }
  233. static void pxa_gpio_demux_handler(unsigned int irq, struct irq_desc *desc)
  234. {
  235. struct pxa_gpio_chip *c;
  236. int loop, gpio, gpio_base, n;
  237. unsigned long gedr;
  238. do {
  239. loop = 0;
  240. for_each_gpio_chip(gpio, c) {
  241. gpio_base = c->chip.base;
  242. gedr = __raw_readl(c->regbase + GEDR_OFFSET);
  243. gedr = gedr & c->irq_mask;
  244. __raw_writel(gedr, c->regbase + GEDR_OFFSET);
  245. n = find_first_bit(&gedr, BITS_PER_LONG);
  246. while (n < BITS_PER_LONG) {
  247. loop = 1;
  248. generic_handle_irq(gpio_to_irq(gpio_base + n));
  249. n = find_next_bit(&gedr, BITS_PER_LONG, n + 1);
  250. }
  251. }
  252. } while (loop);
  253. }
  254. static void pxa_ack_muxed_gpio(struct irq_data *d)
  255. {
  256. int gpio = pxa_irq_to_gpio(d->irq);
  257. struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
  258. __raw_writel(GPIO_bit(gpio), c->regbase + GEDR_OFFSET);
  259. }
  260. static void pxa_mask_muxed_gpio(struct irq_data *d)
  261. {
  262. int gpio = pxa_irq_to_gpio(d->irq);
  263. struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
  264. uint32_t grer, gfer;
  265. c->irq_mask &= ~GPIO_bit(gpio);
  266. grer = __raw_readl(c->regbase + GRER_OFFSET) & ~GPIO_bit(gpio);
  267. gfer = __raw_readl(c->regbase + GFER_OFFSET) & ~GPIO_bit(gpio);
  268. __raw_writel(grer, c->regbase + GRER_OFFSET);
  269. __raw_writel(gfer, c->regbase + GFER_OFFSET);
  270. }
  271. static void pxa_unmask_muxed_gpio(struct irq_data *d)
  272. {
  273. int gpio = pxa_irq_to_gpio(d->irq);
  274. struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
  275. c->irq_mask |= GPIO_bit(gpio);
  276. update_edge_detect(c);
  277. }
  278. static struct irq_chip pxa_muxed_gpio_chip = {
  279. .name = "GPIO",
  280. .irq_ack = pxa_ack_muxed_gpio,
  281. .irq_mask = pxa_mask_muxed_gpio,
  282. .irq_unmask = pxa_unmask_muxed_gpio,
  283. .irq_set_type = pxa_gpio_irq_type,
  284. };
  285. static int pxa_gpio_nums(void)
  286. {
  287. int count = 0;
  288. #ifdef CONFIG_ARCH_PXA
  289. if (cpu_is_pxa25x()) {
  290. #ifdef CONFIG_CPU_PXA26x
  291. count = 89;
  292. gpio_type = PXA26X_GPIO;
  293. #elif defined(CONFIG_PXA25x)
  294. count = 84;
  295. gpio_type = PXA26X_GPIO;
  296. #endif /* CONFIG_CPU_PXA26x */
  297. } else if (cpu_is_pxa27x()) {
  298. count = 120;
  299. gpio_type = PXA27X_GPIO;
  300. } else if (cpu_is_pxa93x() || cpu_is_pxa95x()) {
  301. count = 191;
  302. gpio_type = PXA93X_GPIO;
  303. } else if (cpu_is_pxa3xx()) {
  304. count = 127;
  305. gpio_type = PXA3XX_GPIO;
  306. }
  307. #endif /* CONFIG_ARCH_PXA */
  308. #ifdef CONFIG_ARCH_MMP
  309. if (cpu_is_pxa168() || cpu_is_pxa910()) {
  310. count = 127;
  311. gpio_type = MMP_GPIO;
  312. } else if (cpu_is_mmp2()) {
  313. count = 191;
  314. gpio_type = MMP2_GPIO;
  315. }
  316. #endif /* CONFIG_ARCH_MMP */
  317. return count;
  318. }
  319. void __init pxa_init_gpio(int mux_irq, int start, int end, set_wake_t fn)
  320. {
  321. struct pxa_gpio_chip *c;
  322. int gpio, irq;
  323. pxa_last_gpio = pxa_gpio_nums();
  324. if (!pxa_last_gpio)
  325. return;
  326. /* Initialize GPIO chips */
  327. pxa_init_gpio_chip(end);
  328. /* clear all GPIO edge detects */
  329. for_each_gpio_chip(gpio, c) {
  330. __raw_writel(0, c->regbase + GFER_OFFSET);
  331. __raw_writel(0, c->regbase + GRER_OFFSET);
  332. __raw_writel(~0,c->regbase + GEDR_OFFSET);
  333. }
  334. #ifdef CONFIG_ARCH_PXA
  335. irq = gpio_to_irq(0);
  336. irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
  337. handle_edge_irq);
  338. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  339. irq_set_chained_handler(IRQ_GPIO0, pxa_gpio_demux_handler);
  340. irq = gpio_to_irq(1);
  341. irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
  342. handle_edge_irq);
  343. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  344. irq_set_chained_handler(IRQ_GPIO1, pxa_gpio_demux_handler);
  345. #endif
  346. for (irq = gpio_to_irq(start); irq <= gpio_to_irq(end); irq++) {
  347. irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
  348. handle_edge_irq);
  349. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  350. }
  351. /* Install handler for GPIO>=2 edge detect interrupts */
  352. irq_set_chained_handler(mux_irq, pxa_gpio_demux_handler);
  353. pxa_muxed_gpio_chip.irq_set_wake = fn;
  354. }
  355. #ifdef CONFIG_PM
  356. static int pxa_gpio_suspend(void)
  357. {
  358. struct pxa_gpio_chip *c;
  359. int gpio;
  360. for_each_gpio_chip(gpio, c) {
  361. c->saved_gplr = __raw_readl(c->regbase + GPLR_OFFSET);
  362. c->saved_gpdr = __raw_readl(c->regbase + GPDR_OFFSET);
  363. c->saved_grer = __raw_readl(c->regbase + GRER_OFFSET);
  364. c->saved_gfer = __raw_readl(c->regbase + GFER_OFFSET);
  365. /* Clear GPIO transition detect bits */
  366. __raw_writel(0xffffffff, c->regbase + GEDR_OFFSET);
  367. }
  368. return 0;
  369. }
  370. static void pxa_gpio_resume(void)
  371. {
  372. struct pxa_gpio_chip *c;
  373. int gpio;
  374. for_each_gpio_chip(gpio, c) {
  375. /* restore level with set/clear */
  376. __raw_writel( c->saved_gplr, c->regbase + GPSR_OFFSET);
  377. __raw_writel(~c->saved_gplr, c->regbase + GPCR_OFFSET);
  378. __raw_writel(c->saved_grer, c->regbase + GRER_OFFSET);
  379. __raw_writel(c->saved_gfer, c->regbase + GFER_OFFSET);
  380. __raw_writel(c->saved_gpdr, c->regbase + GPDR_OFFSET);
  381. }
  382. }
  383. #else
  384. #define pxa_gpio_suspend NULL
  385. #define pxa_gpio_resume NULL
  386. #endif
  387. struct syscore_ops pxa_gpio_syscore_ops = {
  388. .suspend = pxa_gpio_suspend,
  389. .resume = pxa_gpio_resume,
  390. };