gpio.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * TI DaVinci GPIO Support
  3. *
  4. * Copyright (c) 2006-2007 David Brownell
  5. * Copyright (c) 2007, MontaVista Software, Inc. <source@mvista.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/errno.h>
  13. #include <linux/kernel.h>
  14. #include <linux/list.h>
  15. #include <linux/module.h>
  16. #include <linux/clk.h>
  17. #include <linux/err.h>
  18. #include <linux/io.h>
  19. #include <linux/irq.h>
  20. #include <linux/bitops.h>
  21. #include <mach/cputype.h>
  22. #include <mach/irqs.h>
  23. #include <mach/hardware.h>
  24. #include <mach/gpio.h>
  25. #include <asm/mach/irq.h>
  26. static DEFINE_SPINLOCK(gpio_lock);
  27. struct davinci_gpio {
  28. struct gpio_chip chip;
  29. struct gpio_controller *__iomem regs;
  30. };
  31. static struct davinci_gpio chips[DIV_ROUND_UP(DAVINCI_N_GPIO, 32)];
  32. static unsigned __initdata ngpio;
  33. /* create a non-inlined version */
  34. static struct gpio_controller __iomem * __init gpio2controller(unsigned gpio)
  35. {
  36. return __gpio_to_controller(gpio);
  37. }
  38. /*--------------------------------------------------------------------------*/
  39. /*
  40. * board setup code *MUST* set PINMUX0 and PINMUX1 as
  41. * needed, and enable the GPIO clock.
  42. */
  43. static int davinci_direction_in(struct gpio_chip *chip, unsigned offset)
  44. {
  45. struct davinci_gpio *d = container_of(chip, struct davinci_gpio, chip);
  46. struct gpio_controller *__iomem g = d->regs;
  47. u32 temp;
  48. spin_lock(&gpio_lock);
  49. temp = __raw_readl(&g->dir);
  50. temp |= (1 << offset);
  51. __raw_writel(temp, &g->dir);
  52. spin_unlock(&gpio_lock);
  53. return 0;
  54. }
  55. /*
  56. * Read the pin's value (works even if it's set up as output);
  57. * returns zero/nonzero.
  58. *
  59. * Note that changes are synched to the GPIO clock, so reading values back
  60. * right after you've set them may give old values.
  61. */
  62. static int davinci_gpio_get(struct gpio_chip *chip, unsigned offset)
  63. {
  64. struct davinci_gpio *d = container_of(chip, struct davinci_gpio, chip);
  65. struct gpio_controller *__iomem g = d->regs;
  66. return (1 << offset) & __raw_readl(&g->in_data);
  67. }
  68. static int
  69. davinci_direction_out(struct gpio_chip *chip, unsigned offset, int value)
  70. {
  71. struct davinci_gpio *d = container_of(chip, struct davinci_gpio, chip);
  72. struct gpio_controller *__iomem g = d->regs;
  73. u32 temp;
  74. u32 mask = 1 << offset;
  75. spin_lock(&gpio_lock);
  76. temp = __raw_readl(&g->dir);
  77. temp &= ~mask;
  78. __raw_writel(mask, value ? &g->set_data : &g->clr_data);
  79. __raw_writel(temp, &g->dir);
  80. spin_unlock(&gpio_lock);
  81. return 0;
  82. }
  83. /*
  84. * Assuming the pin is muxed as a gpio output, set its output value.
  85. */
  86. static void
  87. davinci_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  88. {
  89. struct davinci_gpio *d = container_of(chip, struct davinci_gpio, chip);
  90. struct gpio_controller *__iomem g = d->regs;
  91. __raw_writel((1 << offset), value ? &g->set_data : &g->clr_data);
  92. }
  93. static int __init davinci_gpio_setup(void)
  94. {
  95. int i, base;
  96. /* The gpio banks conceptually expose a segmented bitmap,
  97. * and "ngpio" is one more than the largest zero-based
  98. * bit index that's valid.
  99. */
  100. if (cpu_is_davinci_dm355()) { /* or dm335() */
  101. ngpio = 104;
  102. } else if (cpu_is_davinci_dm644x()) { /* or dm337() */
  103. ngpio = 71;
  104. } else if (cpu_is_davinci_dm646x()) {
  105. /* NOTE: each bank has several "reserved" bits,
  106. * unusable as GPIOs. Only 33 of the GPIO numbers
  107. * are usable, and we're not rejecting the others.
  108. */
  109. ngpio = 43;
  110. } else {
  111. /* if cpu_is_davinci_dm643x() ngpio = 111 */
  112. pr_err("GPIO setup: how many GPIOs?\n");
  113. return -EINVAL;
  114. }
  115. if (WARN_ON(DAVINCI_N_GPIO < ngpio))
  116. ngpio = DAVINCI_N_GPIO;
  117. for (i = 0, base = 0; base < ngpio; i++, base += 32) {
  118. chips[i].chip.label = "DaVinci";
  119. chips[i].chip.direction_input = davinci_direction_in;
  120. chips[i].chip.get = davinci_gpio_get;
  121. chips[i].chip.direction_output = davinci_direction_out;
  122. chips[i].chip.set = davinci_gpio_set;
  123. chips[i].chip.base = base;
  124. chips[i].chip.ngpio = ngpio - base;
  125. if (chips[i].chip.ngpio > 32)
  126. chips[i].chip.ngpio = 32;
  127. chips[i].regs = gpio2controller(base);
  128. gpiochip_add(&chips[i].chip);
  129. }
  130. return 0;
  131. }
  132. pure_initcall(davinci_gpio_setup);
  133. /*--------------------------------------------------------------------------*/
  134. /*
  135. * We expect irqs will normally be set up as input pins, but they can also be
  136. * used as output pins ... which is convenient for testing.
  137. *
  138. * NOTE: The first few GPIOs also have direct INTC hookups in addition
  139. * to their GPIOBNK0 irq, with a bit less overhead but less flexibility
  140. * on triggering (e.g. no edge options). We don't try to use those.
  141. *
  142. * All those INTC hookups (direct, plus several IRQ banks) can also
  143. * serve as EDMA event triggers.
  144. */
  145. static void gpio_irq_disable(unsigned irq)
  146. {
  147. struct gpio_controller *__iomem g = get_irq_chip_data(irq);
  148. u32 mask = __gpio_mask(irq_to_gpio(irq));
  149. __raw_writel(mask, &g->clr_falling);
  150. __raw_writel(mask, &g->clr_rising);
  151. }
  152. static void gpio_irq_enable(unsigned irq)
  153. {
  154. struct gpio_controller *__iomem g = get_irq_chip_data(irq);
  155. u32 mask = __gpio_mask(irq_to_gpio(irq));
  156. if (irq_desc[irq].status & IRQ_TYPE_EDGE_FALLING)
  157. __raw_writel(mask, &g->set_falling);
  158. if (irq_desc[irq].status & IRQ_TYPE_EDGE_RISING)
  159. __raw_writel(mask, &g->set_rising);
  160. }
  161. static int gpio_irq_type(unsigned irq, unsigned trigger)
  162. {
  163. struct gpio_controller *__iomem g = get_irq_chip_data(irq);
  164. u32 mask = __gpio_mask(irq_to_gpio(irq));
  165. if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  166. return -EINVAL;
  167. irq_desc[irq].status &= ~IRQ_TYPE_SENSE_MASK;
  168. irq_desc[irq].status |= trigger;
  169. __raw_writel(mask, (trigger & IRQ_TYPE_EDGE_FALLING)
  170. ? &g->set_falling : &g->clr_falling);
  171. __raw_writel(mask, (trigger & IRQ_TYPE_EDGE_RISING)
  172. ? &g->set_rising : &g->clr_rising);
  173. return 0;
  174. }
  175. static struct irq_chip gpio_irqchip = {
  176. .name = "GPIO",
  177. .enable = gpio_irq_enable,
  178. .disable = gpio_irq_disable,
  179. .set_type = gpio_irq_type,
  180. };
  181. static void
  182. gpio_irq_handler(unsigned irq, struct irq_desc *desc)
  183. {
  184. struct gpio_controller *__iomem g = get_irq_chip_data(irq);
  185. u32 mask = 0xffff;
  186. /* we only care about one bank */
  187. if (irq & 1)
  188. mask <<= 16;
  189. /* temporarily mask (level sensitive) parent IRQ */
  190. desc->chip->ack(irq);
  191. while (1) {
  192. u32 status;
  193. int n;
  194. int res;
  195. /* ack any irqs */
  196. status = __raw_readl(&g->intstat) & mask;
  197. if (!status)
  198. break;
  199. __raw_writel(status, &g->intstat);
  200. if (irq & 1)
  201. status >>= 16;
  202. /* now demux them to the right lowlevel handler */
  203. n = (int)get_irq_data(irq);
  204. while (status) {
  205. res = ffs(status);
  206. n += res;
  207. generic_handle_irq(n - 1);
  208. status >>= res;
  209. }
  210. }
  211. desc->chip->unmask(irq);
  212. /* now it may re-trigger */
  213. }
  214. /*
  215. * NOTE: for suspend/resume, probably best to make a platform_device with
  216. * suspend_late/resume_resume calls hooking into results of the set_wake()
  217. * calls ... so if no gpios are wakeup events the clock can be disabled,
  218. * with outputs left at previously set levels, and so that VDD3P3V.IOPWDN0
  219. * (dm6446) can be set appropriately for GPIOV33 pins.
  220. */
  221. static int __init davinci_gpio_irq_setup(void)
  222. {
  223. unsigned gpio, irq, bank;
  224. unsigned bank_irq;
  225. struct clk *clk;
  226. u32 binten = 0;
  227. if (cpu_is_davinci_dm355()) { /* or dm335() */
  228. bank_irq = IRQ_DM355_GPIOBNK0;
  229. } else if (cpu_is_davinci_dm644x()) {
  230. bank_irq = IRQ_GPIOBNK0;
  231. } else if (cpu_is_davinci_dm646x()) {
  232. bank_irq = IRQ_DM646X_GPIOBNK0;
  233. } else {
  234. printk(KERN_ERR "Don't know first GPIO bank IRQ.\n");
  235. return -EINVAL;
  236. }
  237. clk = clk_get(NULL, "gpio");
  238. if (IS_ERR(clk)) {
  239. printk(KERN_ERR "Error %ld getting gpio clock?\n",
  240. PTR_ERR(clk));
  241. return PTR_ERR(clk);
  242. }
  243. clk_enable(clk);
  244. for (gpio = 0, irq = gpio_to_irq(0), bank = 0;
  245. gpio < ngpio;
  246. bank++, bank_irq++) {
  247. struct gpio_controller *__iomem g = gpio2controller(gpio);
  248. unsigned i;
  249. __raw_writel(~0, &g->clr_falling);
  250. __raw_writel(~0, &g->clr_rising);
  251. /* set up all irqs in this bank */
  252. set_irq_chained_handler(bank_irq, gpio_irq_handler);
  253. set_irq_chip_data(bank_irq, g);
  254. set_irq_data(bank_irq, (void *)irq);
  255. for (i = 0; i < 16 && gpio < ngpio; i++, irq++, gpio++) {
  256. set_irq_chip(irq, &gpio_irqchip);
  257. set_irq_chip_data(irq, g);
  258. set_irq_handler(irq, handle_simple_irq);
  259. set_irq_flags(irq, IRQF_VALID);
  260. }
  261. binten |= BIT(bank);
  262. }
  263. /* BINTEN -- per-bank interrupt enable. genirq would also let these
  264. * bits be set/cleared dynamically.
  265. */
  266. __raw_writel(binten, (void *__iomem)
  267. IO_ADDRESS(DAVINCI_GPIO_BASE + 0x08));
  268. printk(KERN_INFO "DaVinci: %d gpio irqs\n", irq - gpio_to_irq(0));
  269. return 0;
  270. }
  271. arch_initcall(davinci_gpio_irq_setup);