gpio.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * MXC GPIO support. (c) 2008 Daniel Mack <daniel@caiaq.de>
  3. * Copyright 2008 Juergen Beisert, kernel@pengutronix.de
  4. *
  5. * Based on code from Freescale,
  6. * Copyright 2004-2006 Freescale Semiconductor, Inc. All Rights Reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. */
  21. #include <linux/init.h>
  22. #include <linux/io.h>
  23. #include <linux/irq.h>
  24. #include <linux/gpio.h>
  25. #include <mach/hardware.h>
  26. #include <asm-generic/bug.h>
  27. static struct mxc_gpio_port *mxc_gpio_ports;
  28. static int gpio_table_size;
  29. /* Note: This driver assumes 32 GPIOs are handled in one register */
  30. static void _clear_gpio_irqstatus(struct mxc_gpio_port *port, u32 index)
  31. {
  32. __raw_writel(1 << index, port->base + GPIO_ISR);
  33. }
  34. static void _set_gpio_irqenable(struct mxc_gpio_port *port, u32 index,
  35. int enable)
  36. {
  37. u32 l;
  38. l = __raw_readl(port->base + GPIO_IMR);
  39. l = (l & (~(1 << index))) | (!!enable << index);
  40. __raw_writel(l, port->base + GPIO_IMR);
  41. }
  42. static void gpio_ack_irq(u32 irq)
  43. {
  44. u32 gpio = irq_to_gpio(irq);
  45. _clear_gpio_irqstatus(&mxc_gpio_ports[gpio / 32], gpio & 0x1f);
  46. }
  47. static void gpio_mask_irq(u32 irq)
  48. {
  49. u32 gpio = irq_to_gpio(irq);
  50. _set_gpio_irqenable(&mxc_gpio_ports[gpio / 32], gpio & 0x1f, 0);
  51. }
  52. static void gpio_unmask_irq(u32 irq)
  53. {
  54. u32 gpio = irq_to_gpio(irq);
  55. _set_gpio_irqenable(&mxc_gpio_ports[gpio / 32], gpio & 0x1f, 1);
  56. }
  57. static int gpio_set_irq_type(u32 irq, u32 type)
  58. {
  59. u32 gpio = irq_to_gpio(irq);
  60. struct mxc_gpio_port *port = &mxc_gpio_ports[gpio / 32];
  61. u32 bit, val;
  62. int edge;
  63. void __iomem *reg = port->base;
  64. switch (type) {
  65. case IRQ_TYPE_EDGE_RISING:
  66. edge = GPIO_INT_RISE_EDGE;
  67. break;
  68. case IRQ_TYPE_EDGE_FALLING:
  69. edge = GPIO_INT_FALL_EDGE;
  70. break;
  71. case IRQ_TYPE_LEVEL_LOW:
  72. edge = GPIO_INT_LOW_LEV;
  73. break;
  74. case IRQ_TYPE_LEVEL_HIGH:
  75. edge = GPIO_INT_HIGH_LEV;
  76. break;
  77. default: /* this includes IRQ_TYPE_EDGE_BOTH */
  78. return -EINVAL;
  79. }
  80. reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
  81. bit = gpio & 0xf;
  82. val = __raw_readl(reg) & ~(0x3 << (bit << 1));
  83. __raw_writel(val | (edge << (bit << 1)), reg);
  84. _clear_gpio_irqstatus(port, gpio & 0x1f);
  85. return 0;
  86. }
  87. /* handle n interrupts in one status register */
  88. static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat)
  89. {
  90. u32 gpio_irq_no;
  91. gpio_irq_no = port->virtual_irq_start;
  92. for (; irq_stat != 0; irq_stat >>= 1, gpio_irq_no++) {
  93. if ((irq_stat & 1) == 0)
  94. continue;
  95. BUG_ON(!(irq_desc[gpio_irq_no].handle_irq));
  96. irq_desc[gpio_irq_no].handle_irq(gpio_irq_no,
  97. &irq_desc[gpio_irq_no]);
  98. }
  99. }
  100. #if defined(CONFIG_ARCH_MX3) || defined(CONFIG_ARCH_MX1)
  101. /* MX1 and MX3 has one interrupt *per* gpio port */
  102. static void mx3_gpio_irq_handler(u32 irq, struct irq_desc *desc)
  103. {
  104. u32 irq_stat;
  105. struct mxc_gpio_port *port = (struct mxc_gpio_port *)get_irq_data(irq);
  106. irq_stat = __raw_readl(port->base + GPIO_ISR) &
  107. __raw_readl(port->base + GPIO_IMR);
  108. mxc_gpio_irq_handler(port, irq_stat);
  109. }
  110. #endif
  111. #ifdef CONFIG_ARCH_MX2
  112. /* MX2 has one interrupt *for all* gpio ports */
  113. static void mx2_gpio_irq_handler(u32 irq, struct irq_desc *desc)
  114. {
  115. int i;
  116. u32 irq_msk, irq_stat;
  117. struct mxc_gpio_port *port = (struct mxc_gpio_port *)get_irq_data(irq);
  118. /* walk through all interrupt status registers */
  119. for (i = 0; i < gpio_table_size; i++) {
  120. irq_msk = __raw_readl(port[i].base + GPIO_IMR);
  121. if (!irq_msk)
  122. continue;
  123. irq_stat = __raw_readl(port[i].base + GPIO_ISR) & irq_msk;
  124. if (irq_stat)
  125. mxc_gpio_irq_handler(&port[i], irq_stat);
  126. }
  127. }
  128. #endif
  129. static struct irq_chip gpio_irq_chip = {
  130. .ack = gpio_ack_irq,
  131. .mask = gpio_mask_irq,
  132. .unmask = gpio_unmask_irq,
  133. .set_type = gpio_set_irq_type,
  134. };
  135. static void _set_gpio_direction(struct gpio_chip *chip, unsigned offset,
  136. int dir)
  137. {
  138. struct mxc_gpio_port *port =
  139. container_of(chip, struct mxc_gpio_port, chip);
  140. u32 l;
  141. l = __raw_readl(port->base + GPIO_GDIR);
  142. if (dir)
  143. l |= 1 << offset;
  144. else
  145. l &= ~(1 << offset);
  146. __raw_writel(l, port->base + GPIO_GDIR);
  147. }
  148. static void mxc_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  149. {
  150. struct mxc_gpio_port *port =
  151. container_of(chip, struct mxc_gpio_port, chip);
  152. void __iomem *reg = port->base + GPIO_DR;
  153. u32 l;
  154. l = (__raw_readl(reg) & (~(1 << offset))) | (value << offset);
  155. __raw_writel(l, reg);
  156. }
  157. static int mxc_gpio_get(struct gpio_chip *chip, unsigned offset)
  158. {
  159. struct mxc_gpio_port *port =
  160. container_of(chip, struct mxc_gpio_port, chip);
  161. return (__raw_readl(port->base + GPIO_PSR) >> offset) & 1;
  162. }
  163. static int mxc_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  164. {
  165. _set_gpio_direction(chip, offset, 0);
  166. return 0;
  167. }
  168. static int mxc_gpio_direction_output(struct gpio_chip *chip,
  169. unsigned offset, int value)
  170. {
  171. mxc_gpio_set(chip, offset, value);
  172. _set_gpio_direction(chip, offset, 1);
  173. return 0;
  174. }
  175. int __init mxc_gpio_init(struct mxc_gpio_port *port, int cnt)
  176. {
  177. int i, j;
  178. /* save for local usage */
  179. mxc_gpio_ports = port;
  180. gpio_table_size = cnt;
  181. printk(KERN_INFO "MXC GPIO hardware\n");
  182. for (i = 0; i < cnt; i++) {
  183. /* disable the interrupt and clear the status */
  184. __raw_writel(0, port[i].base + GPIO_IMR);
  185. __raw_writel(~0, port[i].base + GPIO_ISR);
  186. for (j = port[i].virtual_irq_start;
  187. j < port[i].virtual_irq_start + 32; j++) {
  188. set_irq_chip(j, &gpio_irq_chip);
  189. set_irq_handler(j, handle_edge_irq);
  190. set_irq_flags(j, IRQF_VALID);
  191. }
  192. /* register gpio chip */
  193. port[i].chip.direction_input = mxc_gpio_direction_input;
  194. port[i].chip.direction_output = mxc_gpio_direction_output;
  195. port[i].chip.get = mxc_gpio_get;
  196. port[i].chip.set = mxc_gpio_set;
  197. port[i].chip.base = i * 32;
  198. port[i].chip.ngpio = 32;
  199. /* its a serious configuration bug when it fails */
  200. BUG_ON( gpiochip_add(&port[i].chip) < 0 );
  201. #if defined(CONFIG_ARCH_MX3) || defined(CONFIG_ARCH_MX1)
  202. /* setup one handler for each entry */
  203. set_irq_chained_handler(port[i].irq, mx3_gpio_irq_handler);
  204. set_irq_data(port[i].irq, &port[i]);
  205. #endif
  206. }
  207. #ifdef CONFIG_ARCH_MX2
  208. /* setup one handler for all GPIO interrupts */
  209. set_irq_chained_handler(port[0].irq, mx2_gpio_irq_handler);
  210. set_irq_data(port[0].irq, port);
  211. #endif
  212. return 0;
  213. }