gpio.c 8.5 KB

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