gpio.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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/slab.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. chips = kzalloc(nbanks * sizeof(struct pxa_gpio_chip), GFP_KERNEL);
  93. if (chips == NULL) {
  94. pr_err("%s: failed to allocate GPIO chips\n", __func__);
  95. return -ENOMEM;
  96. }
  97. for (i = 0, gpio = 0; i < nbanks; i++, gpio += 32) {
  98. struct gpio_chip *c = &chips[i].chip;
  99. sprintf(chips[i].label, "gpio-%d", i);
  100. chips[i].regbase = (void __iomem *)GPIO_BANK(i);
  101. c->base = gpio;
  102. c->label = chips[i].label;
  103. c->direction_input = pxa_gpio_direction_input;
  104. c->direction_output = pxa_gpio_direction_output;
  105. c->get = pxa_gpio_get;
  106. c->set = pxa_gpio_set;
  107. /* number of GPIOs on last bank may be less than 32 */
  108. c->ngpio = (gpio + 31 > gpio_end) ? (gpio_end - gpio + 1) : 32;
  109. gpiochip_add(c);
  110. }
  111. pxa_gpio_chips = chips;
  112. return 0;
  113. }
  114. /* Update only those GRERx and GFERx edge detection register bits if those
  115. * bits are set in c->irq_mask
  116. */
  117. static inline void update_edge_detect(struct pxa_gpio_chip *c)
  118. {
  119. uint32_t grer, gfer;
  120. grer = __raw_readl(c->regbase + GRER_OFFSET) & ~c->irq_mask;
  121. gfer = __raw_readl(c->regbase + GFER_OFFSET) & ~c->irq_mask;
  122. grer |= c->irq_edge_rise & c->irq_mask;
  123. gfer |= c->irq_edge_fall & c->irq_mask;
  124. __raw_writel(grer, c->regbase + GRER_OFFSET);
  125. __raw_writel(gfer, c->regbase + GFER_OFFSET);
  126. }
  127. static int pxa_gpio_irq_type(unsigned int irq, unsigned int type)
  128. {
  129. struct pxa_gpio_chip *c;
  130. int gpio = irq_to_gpio(irq);
  131. unsigned long gpdr, mask = GPIO_bit(gpio);
  132. c = gpio_to_chip(gpio);
  133. if (type == IRQ_TYPE_PROBE) {
  134. /* Don't mess with enabled GPIOs using preconfigured edges or
  135. * GPIOs set to alternate function or to output during probe
  136. */
  137. if ((c->irq_edge_rise | c->irq_edge_fall) & GPIO_bit(gpio))
  138. return 0;
  139. if (__gpio_is_occupied(gpio))
  140. return 0;
  141. type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
  142. }
  143. gpdr = __raw_readl(c->regbase + GPDR_OFFSET);
  144. if (__gpio_is_inverted(gpio))
  145. __raw_writel(gpdr | mask, c->regbase + GPDR_OFFSET);
  146. else
  147. __raw_writel(gpdr & ~mask, c->regbase + GPDR_OFFSET);
  148. if (type & IRQ_TYPE_EDGE_RISING)
  149. c->irq_edge_rise |= mask;
  150. else
  151. c->irq_edge_rise &= ~mask;
  152. if (type & IRQ_TYPE_EDGE_FALLING)
  153. c->irq_edge_fall |= mask;
  154. else
  155. c->irq_edge_fall &= ~mask;
  156. update_edge_detect(c);
  157. pr_debug("%s: IRQ%d (GPIO%d) - edge%s%s\n", __func__, irq, gpio,
  158. ((type & IRQ_TYPE_EDGE_RISING) ? " rising" : ""),
  159. ((type & IRQ_TYPE_EDGE_FALLING) ? " falling" : ""));
  160. return 0;
  161. }
  162. static void pxa_gpio_demux_handler(unsigned int irq, struct irq_desc *desc)
  163. {
  164. struct pxa_gpio_chip *c;
  165. int loop, gpio, gpio_base, n;
  166. unsigned long gedr;
  167. do {
  168. loop = 0;
  169. for_each_gpio_chip(gpio, c) {
  170. gpio_base = c->chip.base;
  171. gedr = __raw_readl(c->regbase + GEDR_OFFSET);
  172. gedr = gedr & c->irq_mask;
  173. __raw_writel(gedr, c->regbase + GEDR_OFFSET);
  174. n = find_first_bit(&gedr, BITS_PER_LONG);
  175. while (n < BITS_PER_LONG) {
  176. loop = 1;
  177. generic_handle_irq(gpio_to_irq(gpio_base + n));
  178. n = find_next_bit(&gedr, BITS_PER_LONG, n + 1);
  179. }
  180. }
  181. } while (loop);
  182. }
  183. static void pxa_ack_muxed_gpio(unsigned int irq)
  184. {
  185. int gpio = irq_to_gpio(irq);
  186. struct pxa_gpio_chip *c = gpio_to_chip(gpio);
  187. __raw_writel(GPIO_bit(gpio), c->regbase + GEDR_OFFSET);
  188. }
  189. static void pxa_mask_muxed_gpio(unsigned int irq)
  190. {
  191. int gpio = irq_to_gpio(irq);
  192. struct pxa_gpio_chip *c = gpio_to_chip(gpio);
  193. uint32_t grer, gfer;
  194. c->irq_mask &= ~GPIO_bit(gpio);
  195. grer = __raw_readl(c->regbase + GRER_OFFSET) & ~GPIO_bit(gpio);
  196. gfer = __raw_readl(c->regbase + GFER_OFFSET) & ~GPIO_bit(gpio);
  197. __raw_writel(grer, c->regbase + GRER_OFFSET);
  198. __raw_writel(gfer, c->regbase + GFER_OFFSET);
  199. }
  200. static void pxa_unmask_muxed_gpio(unsigned int irq)
  201. {
  202. int gpio = irq_to_gpio(irq);
  203. struct pxa_gpio_chip *c = gpio_to_chip(gpio);
  204. c->irq_mask |= GPIO_bit(gpio);
  205. update_edge_detect(c);
  206. }
  207. static struct irq_chip pxa_muxed_gpio_chip = {
  208. .name = "GPIO",
  209. .ack = pxa_ack_muxed_gpio,
  210. .mask = pxa_mask_muxed_gpio,
  211. .unmask = pxa_unmask_muxed_gpio,
  212. .set_type = pxa_gpio_irq_type,
  213. };
  214. void __init pxa_init_gpio(int mux_irq, int start, int end, set_wake_t fn)
  215. {
  216. struct pxa_gpio_chip *c;
  217. int gpio, irq;
  218. pxa_last_gpio = end;
  219. /* Initialize GPIO chips */
  220. pxa_init_gpio_chip(end);
  221. /* clear all GPIO edge detects */
  222. for_each_gpio_chip(gpio, c) {
  223. __raw_writel(0, c->regbase + GFER_OFFSET);
  224. __raw_writel(0, c->regbase + GRER_OFFSET);
  225. __raw_writel(~0,c->regbase + GEDR_OFFSET);
  226. }
  227. for (irq = gpio_to_irq(start); irq <= gpio_to_irq(end); irq++) {
  228. set_irq_chip(irq, &pxa_muxed_gpio_chip);
  229. set_irq_handler(irq, handle_edge_irq);
  230. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  231. }
  232. /* Install handler for GPIO>=2 edge detect interrupts */
  233. set_irq_chained_handler(mux_irq, pxa_gpio_demux_handler);
  234. pxa_muxed_gpio_chip.set_wake = fn;
  235. }
  236. #ifdef CONFIG_PM
  237. static int pxa_gpio_suspend(struct sys_device *dev, pm_message_t state)
  238. {
  239. struct pxa_gpio_chip *c;
  240. int gpio;
  241. for_each_gpio_chip(gpio, c) {
  242. c->saved_gplr = __raw_readl(c->regbase + GPLR_OFFSET);
  243. c->saved_gpdr = __raw_readl(c->regbase + GPDR_OFFSET);
  244. c->saved_grer = __raw_readl(c->regbase + GRER_OFFSET);
  245. c->saved_gfer = __raw_readl(c->regbase + GFER_OFFSET);
  246. /* Clear GPIO transition detect bits */
  247. __raw_writel(0xffffffff, c->regbase + GEDR_OFFSET);
  248. }
  249. return 0;
  250. }
  251. static int pxa_gpio_resume(struct sys_device *dev)
  252. {
  253. struct pxa_gpio_chip *c;
  254. int gpio;
  255. for_each_gpio_chip(gpio, c) {
  256. /* restore level with set/clear */
  257. __raw_writel( c->saved_gplr, c->regbase + GPSR_OFFSET);
  258. __raw_writel(~c->saved_gplr, c->regbase + GPCR_OFFSET);
  259. __raw_writel(c->saved_grer, c->regbase + GRER_OFFSET);
  260. __raw_writel(c->saved_gfer, c->regbase + GFER_OFFSET);
  261. __raw_writel(c->saved_gpdr, c->regbase + GPDR_OFFSET);
  262. }
  263. return 0;
  264. }
  265. #else
  266. #define pxa_gpio_suspend NULL
  267. #define pxa_gpio_resume NULL
  268. #endif
  269. struct sysdev_class pxa_gpio_sysclass = {
  270. .name = "gpio",
  271. .suspend = pxa_gpio_suspend,
  272. .resume = pxa_gpio_resume,
  273. };
  274. static int __init pxa_gpio_init(void)
  275. {
  276. return sysdev_class_register(&pxa_gpio_sysclass);
  277. }
  278. core_initcall(pxa_gpio_init);