gpio.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 (C) 2004-2010 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. #define cpu_is_mx1_mx2() (cpu_is_mx1() || cpu_is_mx2())
  30. #define GPIO_DR (cpu_is_mx1_mx2() ? 0x1c : 0x00)
  31. #define GPIO_GDIR (cpu_is_mx1_mx2() ? 0x00 : 0x04)
  32. #define GPIO_PSR (cpu_is_mx1_mx2() ? 0x24 : 0x08)
  33. #define GPIO_ICR1 (cpu_is_mx1_mx2() ? 0x28 : 0x0C)
  34. #define GPIO_ICR2 (cpu_is_mx1_mx2() ? 0x2C : 0x10)
  35. #define GPIO_IMR (cpu_is_mx1_mx2() ? 0x30 : 0x14)
  36. #define GPIO_ISR (cpu_is_mx1_mx2() ? 0x34 : 0x18)
  37. #define GPIO_INT_LOW_LEV (cpu_is_mx1_mx2() ? 0x3 : 0x0)
  38. #define GPIO_INT_HIGH_LEV (cpu_is_mx1_mx2() ? 0x2 : 0x1)
  39. #define GPIO_INT_RISE_EDGE (cpu_is_mx1_mx2() ? 0x0 : 0x2)
  40. #define GPIO_INT_FALL_EDGE (cpu_is_mx1_mx2() ? 0x1 : 0x3)
  41. #define GPIO_INT_NONE 0x4
  42. /* Note: This driver assumes 32 GPIOs are handled in one register */
  43. static void _clear_gpio_irqstatus(struct mxc_gpio_port *port, u32 index)
  44. {
  45. __raw_writel(1 << index, port->base + GPIO_ISR);
  46. }
  47. static void _set_gpio_irqenable(struct mxc_gpio_port *port, u32 index,
  48. int enable)
  49. {
  50. u32 l;
  51. l = __raw_readl(port->base + GPIO_IMR);
  52. l = (l & (~(1 << index))) | (!!enable << index);
  53. __raw_writel(l, port->base + GPIO_IMR);
  54. }
  55. static void gpio_ack_irq(u32 irq)
  56. {
  57. u32 gpio = irq_to_gpio(irq);
  58. _clear_gpio_irqstatus(&mxc_gpio_ports[gpio / 32], gpio & 0x1f);
  59. }
  60. static void gpio_mask_irq(u32 irq)
  61. {
  62. u32 gpio = irq_to_gpio(irq);
  63. _set_gpio_irqenable(&mxc_gpio_ports[gpio / 32], gpio & 0x1f, 0);
  64. }
  65. static void gpio_unmask_irq(u32 irq)
  66. {
  67. u32 gpio = irq_to_gpio(irq);
  68. _set_gpio_irqenable(&mxc_gpio_ports[gpio / 32], gpio & 0x1f, 1);
  69. }
  70. static int mxc_gpio_get(struct gpio_chip *chip, unsigned offset);
  71. static int gpio_set_irq_type(u32 irq, u32 type)
  72. {
  73. u32 gpio = irq_to_gpio(irq);
  74. struct mxc_gpio_port *port = &mxc_gpio_ports[gpio / 32];
  75. u32 bit, val;
  76. int edge;
  77. void __iomem *reg = port->base;
  78. port->both_edges &= ~(1 << (gpio & 31));
  79. switch (type) {
  80. case IRQ_TYPE_EDGE_RISING:
  81. edge = GPIO_INT_RISE_EDGE;
  82. break;
  83. case IRQ_TYPE_EDGE_FALLING:
  84. edge = GPIO_INT_FALL_EDGE;
  85. break;
  86. case IRQ_TYPE_EDGE_BOTH:
  87. val = mxc_gpio_get(&port->chip, gpio & 31);
  88. if (val) {
  89. edge = GPIO_INT_LOW_LEV;
  90. pr_debug("mxc: set GPIO %d to low trigger\n", gpio);
  91. } else {
  92. edge = GPIO_INT_HIGH_LEV;
  93. pr_debug("mxc: set GPIO %d to high trigger\n", gpio);
  94. }
  95. port->both_edges |= 1 << (gpio & 31);
  96. break;
  97. case IRQ_TYPE_LEVEL_LOW:
  98. edge = GPIO_INT_LOW_LEV;
  99. break;
  100. case IRQ_TYPE_LEVEL_HIGH:
  101. edge = GPIO_INT_HIGH_LEV;
  102. break;
  103. default:
  104. return -EINVAL;
  105. }
  106. reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
  107. bit = gpio & 0xf;
  108. val = __raw_readl(reg) & ~(0x3 << (bit << 1));
  109. __raw_writel(val | (edge << (bit << 1)), reg);
  110. _clear_gpio_irqstatus(port, gpio & 0x1f);
  111. return 0;
  112. }
  113. static void mxc_flip_edge(struct mxc_gpio_port *port, u32 gpio)
  114. {
  115. void __iomem *reg = port->base;
  116. u32 bit, val;
  117. int edge;
  118. reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
  119. bit = gpio & 0xf;
  120. val = __raw_readl(reg);
  121. edge = (val >> (bit << 1)) & 3;
  122. val &= ~(0x3 << (bit << 1));
  123. if (edge == GPIO_INT_HIGH_LEV) {
  124. edge = GPIO_INT_LOW_LEV;
  125. pr_debug("mxc: switch GPIO %d to low trigger\n", gpio);
  126. } else if (edge == GPIO_INT_LOW_LEV) {
  127. edge = GPIO_INT_HIGH_LEV;
  128. pr_debug("mxc: switch GPIO %d to high trigger\n", gpio);
  129. } else {
  130. pr_err("mxc: invalid configuration for GPIO %d: %x\n",
  131. gpio, edge);
  132. return;
  133. }
  134. __raw_writel(val | (edge << (bit << 1)), reg);
  135. }
  136. /* handle 32 interrupts in one status register */
  137. static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat)
  138. {
  139. u32 gpio_irq_no_base = port->virtual_irq_start;
  140. while (irq_stat != 0) {
  141. int irqoffset = fls(irq_stat) - 1;
  142. if (port->both_edges & (1 << irqoffset))
  143. mxc_flip_edge(port, irqoffset);
  144. generic_handle_irq(gpio_irq_no_base + irqoffset);
  145. irq_stat &= ~(1 << irqoffset);
  146. }
  147. }
  148. /* MX1 and MX3 has one interrupt *per* gpio port */
  149. static void mx3_gpio_irq_handler(u32 irq, struct irq_desc *desc)
  150. {
  151. u32 irq_stat;
  152. struct mxc_gpio_port *port = (struct mxc_gpio_port *)get_irq_data(irq);
  153. irq_stat = __raw_readl(port->base + GPIO_ISR) &
  154. __raw_readl(port->base + GPIO_IMR);
  155. mxc_gpio_irq_handler(port, irq_stat);
  156. }
  157. /* MX2 has one interrupt *for all* gpio ports */
  158. static void mx2_gpio_irq_handler(u32 irq, struct irq_desc *desc)
  159. {
  160. int i;
  161. u32 irq_msk, irq_stat;
  162. struct mxc_gpio_port *port = (struct mxc_gpio_port *)get_irq_data(irq);
  163. /* walk through all interrupt status registers */
  164. for (i = 0; i < gpio_table_size; i++) {
  165. irq_msk = __raw_readl(port[i].base + GPIO_IMR);
  166. if (!irq_msk)
  167. continue;
  168. irq_stat = __raw_readl(port[i].base + GPIO_ISR) & irq_msk;
  169. if (irq_stat)
  170. mxc_gpio_irq_handler(&port[i], irq_stat);
  171. }
  172. }
  173. static struct irq_chip gpio_irq_chip = {
  174. .ack = gpio_ack_irq,
  175. .mask = gpio_mask_irq,
  176. .unmask = gpio_unmask_irq,
  177. .set_type = gpio_set_irq_type,
  178. };
  179. static void _set_gpio_direction(struct gpio_chip *chip, unsigned offset,
  180. int dir)
  181. {
  182. struct mxc_gpio_port *port =
  183. container_of(chip, struct mxc_gpio_port, chip);
  184. u32 l;
  185. unsigned long flags;
  186. spin_lock_irqsave(&port->lock, flags);
  187. l = __raw_readl(port->base + GPIO_GDIR);
  188. if (dir)
  189. l |= 1 << offset;
  190. else
  191. l &= ~(1 << offset);
  192. __raw_writel(l, port->base + GPIO_GDIR);
  193. spin_unlock_irqrestore(&port->lock, flags);
  194. }
  195. static void mxc_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  196. {
  197. struct mxc_gpio_port *port =
  198. container_of(chip, struct mxc_gpio_port, chip);
  199. void __iomem *reg = port->base + GPIO_DR;
  200. u32 l;
  201. unsigned long flags;
  202. spin_lock_irqsave(&port->lock, flags);
  203. l = (__raw_readl(reg) & (~(1 << offset))) | (value << offset);
  204. __raw_writel(l, reg);
  205. spin_unlock_irqrestore(&port->lock, flags);
  206. }
  207. static int mxc_gpio_get(struct gpio_chip *chip, unsigned offset)
  208. {
  209. struct mxc_gpio_port *port =
  210. container_of(chip, struct mxc_gpio_port, chip);
  211. return (__raw_readl(port->base + GPIO_PSR) >> offset) & 1;
  212. }
  213. static int mxc_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  214. {
  215. _set_gpio_direction(chip, offset, 0);
  216. return 0;
  217. }
  218. static int mxc_gpio_direction_output(struct gpio_chip *chip,
  219. unsigned offset, int value)
  220. {
  221. mxc_gpio_set(chip, offset, value);
  222. _set_gpio_direction(chip, offset, 1);
  223. return 0;
  224. }
  225. int __init mxc_gpio_init(struct mxc_gpio_port *port, int cnt)
  226. {
  227. int i, j;
  228. /* save for local usage */
  229. mxc_gpio_ports = port;
  230. gpio_table_size = cnt;
  231. printk(KERN_INFO "MXC GPIO hardware\n");
  232. for (i = 0; i < cnt; i++) {
  233. /* disable the interrupt and clear the status */
  234. __raw_writel(0, port[i].base + GPIO_IMR);
  235. __raw_writel(~0, port[i].base + GPIO_ISR);
  236. for (j = port[i].virtual_irq_start;
  237. j < port[i].virtual_irq_start + 32; j++) {
  238. set_irq_chip(j, &gpio_irq_chip);
  239. set_irq_handler(j, handle_level_irq);
  240. set_irq_flags(j, IRQF_VALID);
  241. }
  242. /* register gpio chip */
  243. port[i].chip.direction_input = mxc_gpio_direction_input;
  244. port[i].chip.direction_output = mxc_gpio_direction_output;
  245. port[i].chip.get = mxc_gpio_get;
  246. port[i].chip.set = mxc_gpio_set;
  247. port[i].chip.base = i * 32;
  248. port[i].chip.ngpio = 32;
  249. spin_lock_init(&port[i].lock);
  250. /* its a serious configuration bug when it fails */
  251. BUG_ON( gpiochip_add(&port[i].chip) < 0 );
  252. if (cpu_is_mx1() || cpu_is_mx3() || cpu_is_mx25() || cpu_is_mx51()) {
  253. /* setup one handler for each entry */
  254. set_irq_chained_handler(port[i].irq, mx3_gpio_irq_handler);
  255. set_irq_data(port[i].irq, &port[i]);
  256. if (port[i].irq_high) {
  257. /* setup handler for GPIO 16 to 31 */
  258. set_irq_chained_handler(port[i].irq_high,
  259. mx3_gpio_irq_handler);
  260. set_irq_data(port[i].irq_high, &port[i]);
  261. }
  262. }
  263. }
  264. if (cpu_is_mx2()) {
  265. /* setup one handler for all GPIO interrupts */
  266. set_irq_chained_handler(port[0].irq, mx2_gpio_irq_handler);
  267. set_irq_data(port[0].irq, port);
  268. }
  269. return 0;
  270. }