gpio-sa1100.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * linux/arch/arm/mach-sa1100/gpio.c
  3. *
  4. * Generic SA-1100 GPIO handling
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/gpio.h>
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/io.h>
  14. #include <linux/syscore_ops.h>
  15. #include <soc/sa1100/pwer.h>
  16. #include <mach/hardware.h>
  17. #include <mach/irqs.h>
  18. struct sa1100_gpio_chip {
  19. struct gpio_chip chip;
  20. void __iomem *membase;
  21. int irqbase;
  22. u32 irqmask;
  23. u32 irqrising;
  24. u32 irqfalling;
  25. u32 irqwake;
  26. };
  27. #define sa1100_gpio_chip(x) container_of(x, struct sa1100_gpio_chip, chip)
  28. enum {
  29. R_GPLR = 0x00,
  30. R_GPDR = 0x04,
  31. R_GPSR = 0x08,
  32. R_GPCR = 0x0c,
  33. R_GRER = 0x10,
  34. R_GFER = 0x14,
  35. R_GEDR = 0x18,
  36. R_GAFR = 0x1c,
  37. };
  38. static int sa1100_gpio_get(struct gpio_chip *chip, unsigned offset)
  39. {
  40. return readl_relaxed(sa1100_gpio_chip(chip)->membase + R_GPLR) &
  41. BIT(offset);
  42. }
  43. static void sa1100_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  44. {
  45. int reg = value ? R_GPSR : R_GPCR;
  46. writel_relaxed(BIT(offset), sa1100_gpio_chip(chip)->membase + reg);
  47. }
  48. static int sa1100_get_direction(struct gpio_chip *chip, unsigned offset)
  49. {
  50. void __iomem *gpdr = sa1100_gpio_chip(chip)->membase + R_GPDR;
  51. return !(readl_relaxed(gpdr) & BIT(offset));
  52. }
  53. static int sa1100_direction_input(struct gpio_chip *chip, unsigned offset)
  54. {
  55. void __iomem *gpdr = sa1100_gpio_chip(chip)->membase + R_GPDR;
  56. unsigned long flags;
  57. local_irq_save(flags);
  58. writel_relaxed(readl_relaxed(gpdr) & ~BIT(offset), gpdr);
  59. local_irq_restore(flags);
  60. return 0;
  61. }
  62. static int sa1100_direction_output(struct gpio_chip *chip, unsigned offset, int value)
  63. {
  64. void __iomem *gpdr = sa1100_gpio_chip(chip)->membase + R_GPDR;
  65. unsigned long flags;
  66. local_irq_save(flags);
  67. sa1100_gpio_set(chip, offset, value);
  68. writel_relaxed(readl_relaxed(gpdr) | BIT(offset), gpdr);
  69. local_irq_restore(flags);
  70. return 0;
  71. }
  72. static int sa1100_to_irq(struct gpio_chip *chip, unsigned offset)
  73. {
  74. return sa1100_gpio_chip(chip)->irqbase + offset;
  75. }
  76. static struct sa1100_gpio_chip sa1100_gpio_chip = {
  77. .chip = {
  78. .label = "gpio",
  79. .get_direction = sa1100_get_direction,
  80. .direction_input = sa1100_direction_input,
  81. .direction_output = sa1100_direction_output,
  82. .set = sa1100_gpio_set,
  83. .get = sa1100_gpio_get,
  84. .to_irq = sa1100_to_irq,
  85. .base = 0,
  86. .ngpio = GPIO_MAX + 1,
  87. },
  88. .membase = (void *)&GPLR,
  89. .irqbase = IRQ_GPIO0,
  90. };
  91. /*
  92. * SA1100 GPIO edge detection for IRQs:
  93. * IRQs are generated on Falling-Edge, Rising-Edge, or both.
  94. * Use this instead of directly setting GRER/GFER.
  95. */
  96. static void sa1100_update_edge_regs(struct sa1100_gpio_chip *sgc)
  97. {
  98. void *base = sgc->membase;
  99. u32 grer, gfer;
  100. grer = sgc->irqrising & sgc->irqmask;
  101. gfer = sgc->irqfalling & sgc->irqmask;
  102. writel_relaxed(grer, base + R_GRER);
  103. writel_relaxed(gfer, base + R_GFER);
  104. }
  105. static int sa1100_gpio_type(struct irq_data *d, unsigned int type)
  106. {
  107. struct sa1100_gpio_chip *sgc = irq_data_get_irq_chip_data(d);
  108. unsigned int mask = BIT(d->hwirq);
  109. if (type == IRQ_TYPE_PROBE) {
  110. if ((sgc->irqrising | sgc->irqfalling) & mask)
  111. return 0;
  112. type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
  113. }
  114. if (type & IRQ_TYPE_EDGE_RISING)
  115. sgc->irqrising |= mask;
  116. else
  117. sgc->irqrising &= ~mask;
  118. if (type & IRQ_TYPE_EDGE_FALLING)
  119. sgc->irqfalling |= mask;
  120. else
  121. sgc->irqfalling &= ~mask;
  122. sa1100_update_edge_regs(sgc);
  123. return 0;
  124. }
  125. /*
  126. * GPIO IRQs must be acknowledged.
  127. */
  128. static void sa1100_gpio_ack(struct irq_data *d)
  129. {
  130. struct sa1100_gpio_chip *sgc = irq_data_get_irq_chip_data(d);
  131. writel_relaxed(BIT(d->hwirq), sgc->membase + R_GEDR);
  132. }
  133. static void sa1100_gpio_mask(struct irq_data *d)
  134. {
  135. struct sa1100_gpio_chip *sgc = irq_data_get_irq_chip_data(d);
  136. unsigned int mask = BIT(d->hwirq);
  137. sgc->irqmask &= ~mask;
  138. sa1100_update_edge_regs(sgc);
  139. }
  140. static void sa1100_gpio_unmask(struct irq_data *d)
  141. {
  142. struct sa1100_gpio_chip *sgc = irq_data_get_irq_chip_data(d);
  143. unsigned int mask = BIT(d->hwirq);
  144. sgc->irqmask |= mask;
  145. sa1100_update_edge_regs(sgc);
  146. }
  147. static int sa1100_gpio_wake(struct irq_data *d, unsigned int on)
  148. {
  149. struct sa1100_gpio_chip *sgc = irq_data_get_irq_chip_data(d);
  150. int ret = sa11x0_gpio_set_wake(d->hwirq, on);
  151. if (!ret) {
  152. if (on)
  153. sgc->irqwake |= BIT(d->hwirq);
  154. else
  155. sgc->irqwake &= ~BIT(d->hwirq);
  156. }
  157. return ret;
  158. }
  159. /*
  160. * This is for GPIO IRQs
  161. */
  162. static struct irq_chip sa1100_gpio_irq_chip = {
  163. .name = "GPIO",
  164. .irq_ack = sa1100_gpio_ack,
  165. .irq_mask = sa1100_gpio_mask,
  166. .irq_unmask = sa1100_gpio_unmask,
  167. .irq_set_type = sa1100_gpio_type,
  168. .irq_set_wake = sa1100_gpio_wake,
  169. };
  170. static int sa1100_gpio_irqdomain_map(struct irq_domain *d,
  171. unsigned int irq, irq_hw_number_t hwirq)
  172. {
  173. struct sa1100_gpio_chip *sgc = d->host_data;
  174. irq_set_chip_data(irq, sgc);
  175. irq_set_chip_and_handler(irq, &sa1100_gpio_irq_chip, handle_edge_irq);
  176. irq_set_probe(irq);
  177. return 0;
  178. }
  179. static const struct irq_domain_ops sa1100_gpio_irqdomain_ops = {
  180. .map = sa1100_gpio_irqdomain_map,
  181. .xlate = irq_domain_xlate_onetwocell,
  182. };
  183. static struct irq_domain *sa1100_gpio_irqdomain;
  184. /*
  185. * IRQ 0-11 (GPIO) handler. We enter here with the
  186. * irq_controller_lock held, and IRQs disabled. Decode the IRQ
  187. * and call the handler.
  188. */
  189. static void sa1100_gpio_handler(struct irq_desc *desc)
  190. {
  191. struct sa1100_gpio_chip *sgc = irq_desc_get_handler_data(desc);
  192. unsigned int irq, mask;
  193. void __iomem *gedr = sgc->membase + R_GEDR;
  194. mask = readl_relaxed(gedr);
  195. do {
  196. /*
  197. * clear down all currently active IRQ sources.
  198. * We will be processing them all.
  199. */
  200. writel_relaxed(mask, gedr);
  201. irq = sgc->irqbase;
  202. do {
  203. if (mask & 1)
  204. generic_handle_irq(irq);
  205. mask >>= 1;
  206. irq++;
  207. } while (mask);
  208. mask = readl_relaxed(gedr);
  209. } while (mask);
  210. }
  211. static int sa1100_gpio_suspend(void)
  212. {
  213. struct sa1100_gpio_chip *sgc = &sa1100_gpio_chip;
  214. /*
  215. * Set the appropriate edges for wakeup.
  216. */
  217. writel_relaxed(sgc->irqwake & sgc->irqrising, sgc->membase + R_GRER);
  218. writel_relaxed(sgc->irqwake & sgc->irqfalling, sgc->membase + R_GFER);
  219. /*
  220. * Clear any pending GPIO interrupts.
  221. */
  222. writel_relaxed(readl_relaxed(sgc->membase + R_GEDR),
  223. sgc->membase + R_GEDR);
  224. return 0;
  225. }
  226. static void sa1100_gpio_resume(void)
  227. {
  228. sa1100_update_edge_regs(&sa1100_gpio_chip);
  229. }
  230. static struct syscore_ops sa1100_gpio_syscore_ops = {
  231. .suspend = sa1100_gpio_suspend,
  232. .resume = sa1100_gpio_resume,
  233. };
  234. static int __init sa1100_gpio_init_devicefs(void)
  235. {
  236. register_syscore_ops(&sa1100_gpio_syscore_ops);
  237. return 0;
  238. }
  239. device_initcall(sa1100_gpio_init_devicefs);
  240. static const int sa1100_gpio_irqs[] __initconst = {
  241. /* Install handlers for GPIO 0-10 edge detect interrupts */
  242. IRQ_GPIO0_SC,
  243. IRQ_GPIO1_SC,
  244. IRQ_GPIO2_SC,
  245. IRQ_GPIO3_SC,
  246. IRQ_GPIO4_SC,
  247. IRQ_GPIO5_SC,
  248. IRQ_GPIO6_SC,
  249. IRQ_GPIO7_SC,
  250. IRQ_GPIO8_SC,
  251. IRQ_GPIO9_SC,
  252. IRQ_GPIO10_SC,
  253. /* Install handler for GPIO 11-27 edge detect interrupts */
  254. IRQ_GPIO11_27,
  255. };
  256. void __init sa1100_init_gpio(void)
  257. {
  258. struct sa1100_gpio_chip *sgc = &sa1100_gpio_chip;
  259. int i;
  260. /* clear all GPIO edge detects */
  261. writel_relaxed(0, sgc->membase + R_GFER);
  262. writel_relaxed(0, sgc->membase + R_GRER);
  263. writel_relaxed(-1, sgc->membase + R_GEDR);
  264. gpiochip_add_data(&sa1100_gpio_chip.chip, NULL);
  265. sa1100_gpio_irqdomain = irq_domain_add_simple(NULL,
  266. 28, IRQ_GPIO0,
  267. &sa1100_gpio_irqdomain_ops, sgc);
  268. for (i = 0; i < ARRAY_SIZE(sa1100_gpio_irqs); i++)
  269. irq_set_chained_handler_and_data(sa1100_gpio_irqs[i],
  270. sa1100_gpio_handler, sgc);
  271. }