gpio.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 mxc_gpio_get(struct gpio_chip *chip, unsigned offset);
  58. static int gpio_set_irq_type(u32 irq, u32 type)
  59. {
  60. u32 gpio = irq_to_gpio(irq);
  61. struct mxc_gpio_port *port = &mxc_gpio_ports[gpio / 32];
  62. u32 bit, val;
  63. int edge;
  64. void __iomem *reg = port->base;
  65. port->both_edges &= ~(1 << (gpio & 31));
  66. switch (type) {
  67. case IRQ_TYPE_EDGE_RISING:
  68. edge = GPIO_INT_RISE_EDGE;
  69. break;
  70. case IRQ_TYPE_EDGE_FALLING:
  71. edge = GPIO_INT_FALL_EDGE;
  72. break;
  73. case IRQ_TYPE_EDGE_BOTH:
  74. val = mxc_gpio_get(&port->chip, gpio & 31);
  75. if (val) {
  76. edge = GPIO_INT_LOW_LEV;
  77. pr_debug("mxc: set GPIO %d to low trigger\n", gpio);
  78. } else {
  79. edge = GPIO_INT_HIGH_LEV;
  80. pr_debug("mxc: set GPIO %d to high trigger\n", gpio);
  81. }
  82. port->both_edges |= 1 << (gpio & 31);
  83. break;
  84. case IRQ_TYPE_LEVEL_LOW:
  85. edge = GPIO_INT_LOW_LEV;
  86. break;
  87. case IRQ_TYPE_LEVEL_HIGH:
  88. edge = GPIO_INT_HIGH_LEV;
  89. break;
  90. default:
  91. return -EINVAL;
  92. }
  93. reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
  94. bit = gpio & 0xf;
  95. val = __raw_readl(reg) & ~(0x3 << (bit << 1));
  96. __raw_writel(val | (edge << (bit << 1)), reg);
  97. _clear_gpio_irqstatus(port, gpio & 0x1f);
  98. return 0;
  99. }
  100. static void mxc_flip_edge(struct mxc_gpio_port *port, u32 gpio)
  101. {
  102. void __iomem *reg = port->base;
  103. u32 bit, val;
  104. int edge;
  105. reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
  106. bit = gpio & 0xf;
  107. val = __raw_readl(reg);
  108. edge = (val >> (bit << 1)) & 3;
  109. val &= ~(0x3 << (bit << 1));
  110. switch (edge) {
  111. case GPIO_INT_HIGH_LEV:
  112. edge = GPIO_INT_LOW_LEV;
  113. pr_debug("mxc: switch GPIO %d to low trigger\n", gpio);
  114. break;
  115. case GPIO_INT_LOW_LEV:
  116. edge = GPIO_INT_HIGH_LEV;
  117. pr_debug("mxc: switch GPIO %d to high trigger\n", gpio);
  118. break;
  119. default:
  120. pr_err("mxc: invalid configuration for GPIO %d: %x\n",
  121. gpio, edge);
  122. return;
  123. }
  124. __raw_writel(val | (edge << (bit << 1)), reg);
  125. }
  126. /* handle n interrupts in one status register */
  127. static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat)
  128. {
  129. u32 gpio_irq_no;
  130. gpio_irq_no = port->virtual_irq_start;
  131. for (; irq_stat != 0; irq_stat >>= 1, gpio_irq_no++) {
  132. u32 gpio = irq_to_gpio(gpio_irq_no);
  133. if ((irq_stat & 1) == 0)
  134. continue;
  135. BUG_ON(!(irq_desc[gpio_irq_no].handle_irq));
  136. if (port->both_edges & (1 << (gpio & 31)))
  137. mxc_flip_edge(port, gpio);
  138. irq_desc[gpio_irq_no].handle_irq(gpio_irq_no,
  139. &irq_desc[gpio_irq_no]);
  140. }
  141. }
  142. #if defined(CONFIG_ARCH_MX3) || defined(CONFIG_ARCH_MX1)
  143. /* MX1 and MX3 has one interrupt *per* gpio port */
  144. static void mx3_gpio_irq_handler(u32 irq, struct irq_desc *desc)
  145. {
  146. u32 irq_stat;
  147. struct mxc_gpio_port *port = (struct mxc_gpio_port *)get_irq_data(irq);
  148. irq_stat = __raw_readl(port->base + GPIO_ISR) &
  149. __raw_readl(port->base + GPIO_IMR);
  150. mxc_gpio_irq_handler(port, irq_stat);
  151. }
  152. #endif
  153. #ifdef CONFIG_ARCH_MX2
  154. /* MX2 has one interrupt *for all* gpio ports */
  155. static void mx2_gpio_irq_handler(u32 irq, struct irq_desc *desc)
  156. {
  157. int i;
  158. u32 irq_msk, irq_stat;
  159. struct mxc_gpio_port *port = (struct mxc_gpio_port *)get_irq_data(irq);
  160. /* walk through all interrupt status registers */
  161. for (i = 0; i < gpio_table_size; i++) {
  162. irq_msk = __raw_readl(port[i].base + GPIO_IMR);
  163. if (!irq_msk)
  164. continue;
  165. irq_stat = __raw_readl(port[i].base + GPIO_ISR) & irq_msk;
  166. if (irq_stat)
  167. mxc_gpio_irq_handler(&port[i], irq_stat);
  168. }
  169. }
  170. #endif
  171. static struct irq_chip gpio_irq_chip = {
  172. .ack = gpio_ack_irq,
  173. .mask = gpio_mask_irq,
  174. .unmask = gpio_unmask_irq,
  175. .set_type = gpio_set_irq_type,
  176. };
  177. static void _set_gpio_direction(struct gpio_chip *chip, unsigned offset,
  178. int dir)
  179. {
  180. struct mxc_gpio_port *port =
  181. container_of(chip, struct mxc_gpio_port, chip);
  182. u32 l;
  183. l = __raw_readl(port->base + GPIO_GDIR);
  184. if (dir)
  185. l |= 1 << offset;
  186. else
  187. l &= ~(1 << offset);
  188. __raw_writel(l, port->base + GPIO_GDIR);
  189. }
  190. static void mxc_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  191. {
  192. struct mxc_gpio_port *port =
  193. container_of(chip, struct mxc_gpio_port, chip);
  194. void __iomem *reg = port->base + GPIO_DR;
  195. u32 l;
  196. l = (__raw_readl(reg) & (~(1 << offset))) | (value << offset);
  197. __raw_writel(l, reg);
  198. }
  199. static int mxc_gpio_get(struct gpio_chip *chip, unsigned offset)
  200. {
  201. struct mxc_gpio_port *port =
  202. container_of(chip, struct mxc_gpio_port, chip);
  203. return (__raw_readl(port->base + GPIO_PSR) >> offset) & 1;
  204. }
  205. static int mxc_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  206. {
  207. _set_gpio_direction(chip, offset, 0);
  208. return 0;
  209. }
  210. static int mxc_gpio_direction_output(struct gpio_chip *chip,
  211. unsigned offset, int value)
  212. {
  213. mxc_gpio_set(chip, offset, value);
  214. _set_gpio_direction(chip, offset, 1);
  215. return 0;
  216. }
  217. int __init mxc_gpio_init(struct mxc_gpio_port *port, int cnt)
  218. {
  219. int i, j;
  220. /* save for local usage */
  221. mxc_gpio_ports = port;
  222. gpio_table_size = cnt;
  223. printk(KERN_INFO "MXC GPIO hardware\n");
  224. for (i = 0; i < cnt; i++) {
  225. /* disable the interrupt and clear the status */
  226. __raw_writel(0, port[i].base + GPIO_IMR);
  227. __raw_writel(~0, port[i].base + GPIO_ISR);
  228. for (j = port[i].virtual_irq_start;
  229. j < port[i].virtual_irq_start + 32; j++) {
  230. set_irq_chip(j, &gpio_irq_chip);
  231. set_irq_handler(j, handle_edge_irq);
  232. set_irq_flags(j, IRQF_VALID);
  233. }
  234. /* register gpio chip */
  235. port[i].chip.direction_input = mxc_gpio_direction_input;
  236. port[i].chip.direction_output = mxc_gpio_direction_output;
  237. port[i].chip.get = mxc_gpio_get;
  238. port[i].chip.set = mxc_gpio_set;
  239. port[i].chip.base = i * 32;
  240. port[i].chip.ngpio = 32;
  241. /* its a serious configuration bug when it fails */
  242. BUG_ON( gpiochip_add(&port[i].chip) < 0 );
  243. #if defined(CONFIG_ARCH_MX3) || defined(CONFIG_ARCH_MX1)
  244. /* setup one handler for each entry */
  245. set_irq_chained_handler(port[i].irq, mx3_gpio_irq_handler);
  246. set_irq_data(port[i].irq, &port[i]);
  247. #endif
  248. }
  249. #ifdef CONFIG_ARCH_MX2
  250. /* setup one handler for all GPIO interrupts */
  251. set_irq_chained_handler(port[0].irq, mx2_gpio_irq_handler);
  252. set_irq_data(port[0].irq, port);
  253. #endif
  254. return 0;
  255. }