gpio.c 8.7 KB

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