gpio.c 8.4 KB

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