gpio.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * TI DaVinci GPIO Support
  3. *
  4. * Copyright (c) 2006 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 <asm/arch/irqs.h>
  22. #include <asm/arch/hardware.h>
  23. #include <asm/arch/gpio.h>
  24. #include <asm/mach/irq.h>
  25. static DEFINE_SPINLOCK(gpio_lock);
  26. static DECLARE_BITMAP(gpio_in_use, DAVINCI_N_GPIO);
  27. int gpio_request(unsigned gpio, const char *tag)
  28. {
  29. if (gpio >= DAVINCI_N_GPIO)
  30. return -EINVAL;
  31. if (test_and_set_bit(gpio, gpio_in_use))
  32. return -EBUSY;
  33. return 0;
  34. }
  35. EXPORT_SYMBOL(gpio_request);
  36. void gpio_free(unsigned gpio)
  37. {
  38. if (gpio >= DAVINCI_N_GPIO)
  39. return;
  40. clear_bit(gpio, gpio_in_use);
  41. }
  42. EXPORT_SYMBOL(gpio_free);
  43. /* create a non-inlined version */
  44. static struct gpio_controller *__iomem gpio2controller(unsigned gpio)
  45. {
  46. return __gpio_to_controller(gpio);
  47. }
  48. /*
  49. * Assuming the pin is muxed as a gpio output, set its output value.
  50. */
  51. void __gpio_set(unsigned gpio, int value)
  52. {
  53. struct gpio_controller *__iomem g = gpio2controller(gpio);
  54. __raw_writel(__gpio_mask(gpio), value ? &g->set_data : &g->clr_data);
  55. }
  56. EXPORT_SYMBOL(__gpio_set);
  57. /*
  58. * Read the pin's value (works even if it's set up as output);
  59. * returns zero/nonzero.
  60. *
  61. * Note that changes are synched to the GPIO clock, so reading values back
  62. * right after you've set them may give old values.
  63. */
  64. int __gpio_get(unsigned gpio)
  65. {
  66. struct gpio_controller *__iomem g = gpio2controller(gpio);
  67. return !!(__gpio_mask(gpio) & __raw_readl(&g->in_data));
  68. }
  69. EXPORT_SYMBOL(__gpio_get);
  70. /*--------------------------------------------------------------------------*/
  71. /*
  72. * board setup code *MUST* set PINMUX0 and PINMUX1 as
  73. * needed, and enable the GPIO clock.
  74. */
  75. int gpio_direction_input(unsigned gpio)
  76. {
  77. struct gpio_controller *__iomem g = gpio2controller(gpio);
  78. u32 temp;
  79. u32 mask;
  80. if (!g)
  81. return -EINVAL;
  82. spin_lock(&gpio_lock);
  83. mask = __gpio_mask(gpio);
  84. temp = __raw_readl(&g->dir);
  85. temp |= mask;
  86. __raw_writel(temp, &g->dir);
  87. spin_unlock(&gpio_lock);
  88. return 0;
  89. }
  90. EXPORT_SYMBOL(gpio_direction_input);
  91. int gpio_direction_output(unsigned gpio, int value)
  92. {
  93. struct gpio_controller *__iomem g = gpio2controller(gpio);
  94. u32 temp;
  95. u32 mask;
  96. if (!g)
  97. return -EINVAL;
  98. spin_lock(&gpio_lock);
  99. mask = __gpio_mask(gpio);
  100. temp = __raw_readl(&g->dir);
  101. temp &= ~mask;
  102. __raw_writel(mask, value ? &g->set_data : &g->clr_data);
  103. __raw_writel(temp, &g->dir);
  104. spin_unlock(&gpio_lock);
  105. return 0;
  106. }
  107. EXPORT_SYMBOL(gpio_direction_output);
  108. /*
  109. * We expect irqs will normally be set up as input pins, but they can also be
  110. * used as output pins ... which is convenient for testing.
  111. *
  112. * NOTE: GPIO0..GPIO7 also have direct INTC hookups, which work in addition
  113. * to their GPIOBNK0 irq (but with a bit less overhead). But we don't have
  114. * a good way to hook those up ...
  115. *
  116. * All those INTC hookups (GPIO0..GPIO7 plus five IRQ banks) can also
  117. * serve as EDMA event triggers.
  118. */
  119. static void gpio_irq_disable(unsigned irq)
  120. {
  121. struct gpio_controller *__iomem g = get_irq_chip_data(irq);
  122. u32 mask = __gpio_mask(irq_to_gpio(irq));
  123. __raw_writel(mask, &g->clr_falling);
  124. __raw_writel(mask, &g->clr_rising);
  125. }
  126. static void gpio_irq_enable(unsigned irq)
  127. {
  128. struct gpio_controller *__iomem g = get_irq_chip_data(irq);
  129. u32 mask = __gpio_mask(irq_to_gpio(irq));
  130. if (irq_desc[irq].status & IRQ_TYPE_EDGE_FALLING)
  131. __raw_writel(mask, &g->set_falling);
  132. if (irq_desc[irq].status & IRQ_TYPE_EDGE_RISING)
  133. __raw_writel(mask, &g->set_rising);
  134. }
  135. static int gpio_irq_type(unsigned irq, unsigned trigger)
  136. {
  137. struct gpio_controller *__iomem g = get_irq_chip_data(irq);
  138. u32 mask = __gpio_mask(irq_to_gpio(irq));
  139. if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  140. return -EINVAL;
  141. irq_desc[irq].status &= ~IRQ_TYPE_SENSE_MASK;
  142. irq_desc[irq].status |= trigger;
  143. __raw_writel(mask, (trigger & IRQ_TYPE_EDGE_FALLING)
  144. ? &g->set_falling : &g->clr_falling);
  145. __raw_writel(mask, (trigger & IRQ_TYPE_EDGE_RISING)
  146. ? &g->set_rising : &g->clr_rising);
  147. return 0;
  148. }
  149. static struct irq_chip gpio_irqchip = {
  150. .name = "GPIO",
  151. .enable = gpio_irq_enable,
  152. .disable = gpio_irq_disable,
  153. .set_type = gpio_irq_type,
  154. };
  155. static void
  156. gpio_irq_handler(unsigned irq, struct irq_desc *desc)
  157. {
  158. struct gpio_controller *__iomem g = get_irq_chip_data(irq);
  159. u32 mask = 0xffff;
  160. /* we only care about one bank */
  161. if (irq & 1)
  162. mask <<= 16;
  163. /* temporarily mask (level sensitive) parent IRQ */
  164. desc->chip->ack(irq);
  165. while (1) {
  166. u32 status;
  167. struct irq_desc *gpio;
  168. int n;
  169. int res;
  170. /* ack any irqs */
  171. status = __raw_readl(&g->intstat) & mask;
  172. if (!status)
  173. break;
  174. __raw_writel(status, &g->intstat);
  175. if (irq & 1)
  176. status >>= 16;
  177. /* now demux them to the right lowlevel handler */
  178. n = (int)get_irq_data(irq);
  179. gpio = &irq_desc[n];
  180. while (status) {
  181. res = ffs(status);
  182. n += res;
  183. gpio += res;
  184. desc_handle_irq(n - 1, gpio - 1);
  185. status >>= res;
  186. }
  187. }
  188. desc->chip->unmask(irq);
  189. /* now it may re-trigger */
  190. }
  191. /*
  192. * NOTE: for suspend/resume, probably best to make a sysdev (and class)
  193. * with its suspend/resume calls hooking into the results of the set_wake()
  194. * calls ... so if no gpios are wakeup events the clock can be disabled,
  195. * with outputs left at previously set levels, and so that VDD3P3V.IOPWDN0
  196. * can be set appropriately for GPIOV33 pins.
  197. */
  198. static int __init davinci_gpio_irq_setup(void)
  199. {
  200. unsigned gpio, irq, bank;
  201. struct clk *clk;
  202. clk = clk_get(NULL, "gpio");
  203. if (IS_ERR(clk)) {
  204. printk(KERN_ERR "Error %ld getting gpio clock?\n",
  205. PTR_ERR(clk));
  206. return 0;
  207. }
  208. clk_enable(clk);
  209. for (gpio = 0, irq = gpio_to_irq(0), bank = IRQ_GPIOBNK0;
  210. gpio < DAVINCI_N_GPIO; bank++) {
  211. struct gpio_controller *__iomem g = gpio2controller(gpio);
  212. unsigned i;
  213. __raw_writel(~0, &g->clr_falling);
  214. __raw_writel(~0, &g->clr_rising);
  215. /* set up all irqs in this bank */
  216. set_irq_chained_handler(bank, gpio_irq_handler);
  217. set_irq_chip_data(bank, g);
  218. set_irq_data(bank, (void *)irq);
  219. for (i = 0; i < 16 && gpio < DAVINCI_N_GPIO;
  220. i++, irq++, gpio++) {
  221. set_irq_chip(irq, &gpio_irqchip);
  222. set_irq_chip_data(irq, g);
  223. set_irq_handler(irq, handle_simple_irq);
  224. set_irq_flags(irq, IRQF_VALID);
  225. }
  226. }
  227. /* BINTEN -- per-bank interrupt enable. genirq would also let these
  228. * bits be set/cleared dynamically.
  229. */
  230. __raw_writel(0x1f, (void *__iomem)
  231. IO_ADDRESS(DAVINCI_GPIO_BASE + 0x08));
  232. printk(KERN_INFO "DaVinci: %d gpio irqs\n", irq - gpio_to_irq(0));
  233. return 0;
  234. }
  235. arch_initcall(davinci_gpio_irq_setup);