gpio-pxa.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. static DEFINE_SPINLOCK(gpio_lock);
  37. static struct pxa_gpio_chip *pxa_gpio_chips;
  38. #define for_each_gpio_chip(i, c) \
  39. for (i = 0, c = &pxa_gpio_chips[0]; i <= pxa_last_gpio; i += 32, c++)
  40. static inline void __iomem *gpio_chip_base(struct gpio_chip *c)
  41. {
  42. return container_of(c, struct pxa_gpio_chip, chip)->regbase;
  43. }
  44. static inline struct pxa_gpio_chip *gpio_to_pxachip(unsigned gpio)
  45. {
  46. return &pxa_gpio_chips[gpio_to_bank(gpio)];
  47. }
  48. static int pxa_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  49. {
  50. void __iomem *base = gpio_chip_base(chip);
  51. uint32_t value, mask = 1 << offset;
  52. unsigned long flags;
  53. spin_lock_irqsave(&gpio_lock, flags);
  54. value = __raw_readl(base + GPDR_OFFSET);
  55. if (__gpio_is_inverted(chip->base + offset))
  56. value |= mask;
  57. else
  58. value &= ~mask;
  59. __raw_writel(value, base + GPDR_OFFSET);
  60. spin_unlock_irqrestore(&gpio_lock, flags);
  61. return 0;
  62. }
  63. static int pxa_gpio_direction_output(struct gpio_chip *chip,
  64. unsigned offset, int value)
  65. {
  66. void __iomem *base = gpio_chip_base(chip);
  67. uint32_t tmp, mask = 1 << offset;
  68. unsigned long flags;
  69. __raw_writel(mask, base + (value ? GPSR_OFFSET : GPCR_OFFSET));
  70. spin_lock_irqsave(&gpio_lock, flags);
  71. tmp = __raw_readl(base + GPDR_OFFSET);
  72. if (__gpio_is_inverted(chip->base + offset))
  73. tmp &= ~mask;
  74. else
  75. tmp |= mask;
  76. __raw_writel(tmp, base + GPDR_OFFSET);
  77. spin_unlock_irqrestore(&gpio_lock, flags);
  78. return 0;
  79. }
  80. static int pxa_gpio_get(struct gpio_chip *chip, unsigned offset)
  81. {
  82. return __raw_readl(gpio_chip_base(chip) + GPLR_OFFSET) & (1 << offset);
  83. }
  84. static void pxa_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  85. {
  86. __raw_writel(1 << offset, gpio_chip_base(chip) +
  87. (value ? GPSR_OFFSET : GPCR_OFFSET));
  88. }
  89. static int __init pxa_init_gpio_chip(int gpio_end)
  90. {
  91. int i, gpio, nbanks = gpio_to_bank(gpio_end) + 1;
  92. struct pxa_gpio_chip *chips;
  93. chips = kzalloc(nbanks * sizeof(struct pxa_gpio_chip), GFP_KERNEL);
  94. if (chips == NULL) {
  95. pr_err("%s: failed to allocate GPIO chips\n", __func__);
  96. return -ENOMEM;
  97. }
  98. for (i = 0, gpio = 0; i < nbanks; i++, gpio += 32) {
  99. struct gpio_chip *c = &chips[i].chip;
  100. sprintf(chips[i].label, "gpio-%d", i);
  101. chips[i].regbase = GPIO_BANK(i);
  102. c->base = gpio;
  103. c->label = chips[i].label;
  104. c->direction_input = pxa_gpio_direction_input;
  105. c->direction_output = pxa_gpio_direction_output;
  106. c->get = pxa_gpio_get;
  107. c->set = pxa_gpio_set;
  108. /* number of GPIOs on last bank may be less than 32 */
  109. c->ngpio = (gpio + 31 > gpio_end) ? (gpio_end - gpio + 1) : 32;
  110. gpiochip_add(c);
  111. }
  112. pxa_gpio_chips = chips;
  113. return 0;
  114. }
  115. /* Update only those GRERx and GFERx edge detection register bits if those
  116. * bits are set in c->irq_mask
  117. */
  118. static inline void update_edge_detect(struct pxa_gpio_chip *c)
  119. {
  120. uint32_t grer, gfer;
  121. grer = __raw_readl(c->regbase + GRER_OFFSET) & ~c->irq_mask;
  122. gfer = __raw_readl(c->regbase + GFER_OFFSET) & ~c->irq_mask;
  123. grer |= c->irq_edge_rise & c->irq_mask;
  124. gfer |= c->irq_edge_fall & c->irq_mask;
  125. __raw_writel(grer, c->regbase + GRER_OFFSET);
  126. __raw_writel(gfer, c->regbase + GFER_OFFSET);
  127. }
  128. static int pxa_gpio_irq_type(struct irq_data *d, unsigned int type)
  129. {
  130. struct pxa_gpio_chip *c;
  131. int gpio = irq_to_gpio(d->irq);
  132. unsigned long gpdr, mask = GPIO_bit(gpio);
  133. c = gpio_to_pxachip(gpio);
  134. if (type == IRQ_TYPE_PROBE) {
  135. /* Don't mess with enabled GPIOs using preconfigured edges or
  136. * GPIOs set to alternate function or to output during probe
  137. */
  138. if ((c->irq_edge_rise | c->irq_edge_fall) & GPIO_bit(gpio))
  139. return 0;
  140. if (__gpio_is_occupied(gpio))
  141. return 0;
  142. type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
  143. }
  144. gpdr = __raw_readl(c->regbase + GPDR_OFFSET);
  145. if (__gpio_is_inverted(gpio))
  146. __raw_writel(gpdr | mask, c->regbase + GPDR_OFFSET);
  147. else
  148. __raw_writel(gpdr & ~mask, c->regbase + GPDR_OFFSET);
  149. if (type & IRQ_TYPE_EDGE_RISING)
  150. c->irq_edge_rise |= mask;
  151. else
  152. c->irq_edge_rise &= ~mask;
  153. if (type & IRQ_TYPE_EDGE_FALLING)
  154. c->irq_edge_fall |= mask;
  155. else
  156. c->irq_edge_fall &= ~mask;
  157. update_edge_detect(c);
  158. pr_debug("%s: IRQ%d (GPIO%d) - edge%s%s\n", __func__, d->irq, gpio,
  159. ((type & IRQ_TYPE_EDGE_RISING) ? " rising" : ""),
  160. ((type & IRQ_TYPE_EDGE_FALLING) ? " falling" : ""));
  161. return 0;
  162. }
  163. static void pxa_gpio_demux_handler(unsigned int irq, struct irq_desc *desc)
  164. {
  165. struct pxa_gpio_chip *c;
  166. int loop, gpio, gpio_base, n;
  167. unsigned long gedr;
  168. do {
  169. loop = 0;
  170. for_each_gpio_chip(gpio, c) {
  171. gpio_base = c->chip.base;
  172. gedr = __raw_readl(c->regbase + GEDR_OFFSET);
  173. gedr = gedr & c->irq_mask;
  174. __raw_writel(gedr, c->regbase + GEDR_OFFSET);
  175. n = find_first_bit(&gedr, BITS_PER_LONG);
  176. while (n < BITS_PER_LONG) {
  177. loop = 1;
  178. generic_handle_irq(gpio_to_irq(gpio_base + n));
  179. n = find_next_bit(&gedr, BITS_PER_LONG, n + 1);
  180. }
  181. }
  182. } while (loop);
  183. }
  184. static void pxa_ack_muxed_gpio(struct irq_data *d)
  185. {
  186. int gpio = irq_to_gpio(d->irq);
  187. struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
  188. __raw_writel(GPIO_bit(gpio), c->regbase + GEDR_OFFSET);
  189. }
  190. static void pxa_mask_muxed_gpio(struct irq_data *d)
  191. {
  192. int gpio = irq_to_gpio(d->irq);
  193. struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
  194. uint32_t grer, gfer;
  195. c->irq_mask &= ~GPIO_bit(gpio);
  196. grer = __raw_readl(c->regbase + GRER_OFFSET) & ~GPIO_bit(gpio);
  197. gfer = __raw_readl(c->regbase + GFER_OFFSET) & ~GPIO_bit(gpio);
  198. __raw_writel(grer, c->regbase + GRER_OFFSET);
  199. __raw_writel(gfer, c->regbase + GFER_OFFSET);
  200. }
  201. static void pxa_unmask_muxed_gpio(struct irq_data *d)
  202. {
  203. int gpio = irq_to_gpio(d->irq);
  204. struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
  205. c->irq_mask |= GPIO_bit(gpio);
  206. update_edge_detect(c);
  207. }
  208. static struct irq_chip pxa_muxed_gpio_chip = {
  209. .name = "GPIO",
  210. .irq_ack = pxa_ack_muxed_gpio,
  211. .irq_mask = pxa_mask_muxed_gpio,
  212. .irq_unmask = pxa_unmask_muxed_gpio,
  213. .irq_set_type = pxa_gpio_irq_type,
  214. };
  215. void __init pxa_init_gpio(int mux_irq, int start, int end, set_wake_t fn)
  216. {
  217. struct pxa_gpio_chip *c;
  218. int gpio, irq;
  219. pxa_last_gpio = end;
  220. /* Initialize GPIO chips */
  221. pxa_init_gpio_chip(end);
  222. /* clear all GPIO edge detects */
  223. for_each_gpio_chip(gpio, c) {
  224. __raw_writel(0, c->regbase + GFER_OFFSET);
  225. __raw_writel(0, c->regbase + GRER_OFFSET);
  226. __raw_writel(~0,c->regbase + GEDR_OFFSET);
  227. }
  228. for (irq = gpio_to_irq(start); irq <= gpio_to_irq(end); irq++) {
  229. irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
  230. handle_edge_irq);
  231. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  232. }
  233. /* Install handler for GPIO>=2 edge detect interrupts */
  234. irq_set_chained_handler(mux_irq, pxa_gpio_demux_handler);
  235. pxa_muxed_gpio_chip.irq_set_wake = fn;
  236. }
  237. #ifdef CONFIG_PM
  238. static int pxa_gpio_suspend(void)
  239. {
  240. struct pxa_gpio_chip *c;
  241. int gpio;
  242. for_each_gpio_chip(gpio, c) {
  243. c->saved_gplr = __raw_readl(c->regbase + GPLR_OFFSET);
  244. c->saved_gpdr = __raw_readl(c->regbase + GPDR_OFFSET);
  245. c->saved_grer = __raw_readl(c->regbase + GRER_OFFSET);
  246. c->saved_gfer = __raw_readl(c->regbase + GFER_OFFSET);
  247. /* Clear GPIO transition detect bits */
  248. __raw_writel(0xffffffff, c->regbase + GEDR_OFFSET);
  249. }
  250. return 0;
  251. }
  252. static void pxa_gpio_resume(void)
  253. {
  254. struct pxa_gpio_chip *c;
  255. int gpio;
  256. for_each_gpio_chip(gpio, c) {
  257. /* restore level with set/clear */
  258. __raw_writel( c->saved_gplr, c->regbase + GPSR_OFFSET);
  259. __raw_writel(~c->saved_gplr, c->regbase + GPCR_OFFSET);
  260. __raw_writel(c->saved_grer, c->regbase + GRER_OFFSET);
  261. __raw_writel(c->saved_gfer, c->regbase + GFER_OFFSET);
  262. __raw_writel(c->saved_gpdr, c->regbase + GPDR_OFFSET);
  263. }
  264. }
  265. #else
  266. #define pxa_gpio_suspend NULL
  267. #define pxa_gpio_resume NULL
  268. #endif
  269. struct syscore_ops pxa_gpio_syscore_ops = {
  270. .suspend = pxa_gpio_suspend,
  271. .resume = pxa_gpio_resume,
  272. };