gpio.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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/interrupt.h>
  23. #include <linux/io.h>
  24. #include <linux/irq.h>
  25. #include <linux/gpio.h>
  26. #include <mach/hardware.h>
  27. #include <asm-generic/bug.h>
  28. static struct mxc_gpio_port *mxc_gpio_ports;
  29. static int gpio_table_size;
  30. #define cpu_is_mx1_mx2() (cpu_is_mx1() || cpu_is_mx2())
  31. #define GPIO_DR (cpu_is_mx1_mx2() ? 0x1c : 0x00)
  32. #define GPIO_GDIR (cpu_is_mx1_mx2() ? 0x00 : 0x04)
  33. #define GPIO_PSR (cpu_is_mx1_mx2() ? 0x24 : 0x08)
  34. #define GPIO_ICR1 (cpu_is_mx1_mx2() ? 0x28 : 0x0C)
  35. #define GPIO_ICR2 (cpu_is_mx1_mx2() ? 0x2C : 0x10)
  36. #define GPIO_IMR (cpu_is_mx1_mx2() ? 0x30 : 0x14)
  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(struct irq_data *d)
  57. {
  58. u32 gpio = irq_to_gpio(d->irq);
  59. _clear_gpio_irqstatus(&mxc_gpio_ports[gpio / 32], gpio & 0x1f);
  60. }
  61. static void gpio_mask_irq(struct irq_data *d)
  62. {
  63. u32 gpio = irq_to_gpio(d->irq);
  64. _set_gpio_irqenable(&mxc_gpio_ports[gpio / 32], gpio & 0x1f, 0);
  65. }
  66. static void gpio_unmask_irq(struct irq_data *d)
  67. {
  68. u32 gpio = irq_to_gpio(d->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(struct irq_data *d, u32 type)
  73. {
  74. u32 gpio = irq_to_gpio(d->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. if (edge == GPIO_INT_HIGH_LEV) {
  125. edge = GPIO_INT_LOW_LEV;
  126. pr_debug("mxc: switch GPIO %d to low trigger\n", gpio);
  127. } else if (edge == GPIO_INT_LOW_LEV) {
  128. edge = GPIO_INT_HIGH_LEV;
  129. pr_debug("mxc: switch GPIO %d to high trigger\n", gpio);
  130. } else {
  131. pr_err("mxc: invalid configuration for GPIO %d: %x\n",
  132. gpio, edge);
  133. return;
  134. }
  135. __raw_writel(val | (edge << (bit << 1)), reg);
  136. }
  137. /* handle 32 interrupts in one status register */
  138. static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat)
  139. {
  140. u32 gpio_irq_no_base = port->virtual_irq_start;
  141. while (irq_stat != 0) {
  142. int irqoffset = fls(irq_stat) - 1;
  143. if (port->both_edges & (1 << irqoffset))
  144. mxc_flip_edge(port, irqoffset);
  145. generic_handle_irq(gpio_irq_no_base + irqoffset);
  146. irq_stat &= ~(1 << irqoffset);
  147. }
  148. }
  149. /* MX1 and MX3 has one interrupt *per* gpio port */
  150. static void mx3_gpio_irq_handler(u32 irq, struct irq_desc *desc)
  151. {
  152. u32 irq_stat;
  153. struct mxc_gpio_port *port = irq_get_handler_data(irq);
  154. irq_stat = __raw_readl(port->base + GPIO_ISR) &
  155. __raw_readl(port->base + GPIO_IMR);
  156. mxc_gpio_irq_handler(port, irq_stat);
  157. }
  158. /* MX2 has one interrupt *for all* gpio ports */
  159. static void mx2_gpio_irq_handler(u32 irq, struct irq_desc *desc)
  160. {
  161. int i;
  162. u32 irq_msk, irq_stat;
  163. struct mxc_gpio_port *port = irq_get_handler_data(irq);
  164. /* walk through all interrupt status registers */
  165. for (i = 0; i < gpio_table_size; i++) {
  166. irq_msk = __raw_readl(port[i].base + GPIO_IMR);
  167. if (!irq_msk)
  168. continue;
  169. irq_stat = __raw_readl(port[i].base + GPIO_ISR) & irq_msk;
  170. if (irq_stat)
  171. mxc_gpio_irq_handler(&port[i], irq_stat);
  172. }
  173. }
  174. /*
  175. * Set interrupt number "irq" in the GPIO as a wake-up source.
  176. * While system is running, all registered GPIO interrupts need to have
  177. * wake-up enabled. When system is suspended, only selected GPIO interrupts
  178. * need to have wake-up enabled.
  179. * @param irq interrupt source number
  180. * @param enable enable as wake-up if equal to non-zero
  181. * @return This function returns 0 on success.
  182. */
  183. static int gpio_set_wake_irq(struct irq_data *d, u32 enable)
  184. {
  185. u32 gpio = irq_to_gpio(d->irq);
  186. u32 gpio_idx = gpio & 0x1F;
  187. struct mxc_gpio_port *port = &mxc_gpio_ports[gpio / 32];
  188. if (enable) {
  189. if (port->irq_high && (gpio_idx >= 16))
  190. enable_irq_wake(port->irq_high);
  191. else
  192. enable_irq_wake(port->irq);
  193. } else {
  194. if (port->irq_high && (gpio_idx >= 16))
  195. disable_irq_wake(port->irq_high);
  196. else
  197. disable_irq_wake(port->irq);
  198. }
  199. return 0;
  200. }
  201. static struct irq_chip gpio_irq_chip = {
  202. .name = "GPIO",
  203. .irq_ack = gpio_ack_irq,
  204. .irq_mask = gpio_mask_irq,
  205. .irq_unmask = gpio_unmask_irq,
  206. .irq_set_type = gpio_set_irq_type,
  207. .irq_set_wake = gpio_set_wake_irq,
  208. };
  209. static void _set_gpio_direction(struct gpio_chip *chip, unsigned offset,
  210. int dir)
  211. {
  212. struct mxc_gpio_port *port =
  213. container_of(chip, struct mxc_gpio_port, chip);
  214. u32 l;
  215. unsigned long flags;
  216. spin_lock_irqsave(&port->lock, flags);
  217. l = __raw_readl(port->base + GPIO_GDIR);
  218. if (dir)
  219. l |= 1 << offset;
  220. else
  221. l &= ~(1 << offset);
  222. __raw_writel(l, port->base + GPIO_GDIR);
  223. spin_unlock_irqrestore(&port->lock, flags);
  224. }
  225. static void mxc_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  226. {
  227. struct mxc_gpio_port *port =
  228. container_of(chip, struct mxc_gpio_port, chip);
  229. void __iomem *reg = port->base + GPIO_DR;
  230. u32 l;
  231. unsigned long flags;
  232. spin_lock_irqsave(&port->lock, flags);
  233. l = (__raw_readl(reg) & (~(1 << offset))) | (!!value << offset);
  234. __raw_writel(l, reg);
  235. spin_unlock_irqrestore(&port->lock, flags);
  236. }
  237. static int mxc_gpio_get(struct gpio_chip *chip, unsigned offset)
  238. {
  239. struct mxc_gpio_port *port =
  240. container_of(chip, struct mxc_gpio_port, chip);
  241. return (__raw_readl(port->base + GPIO_PSR) >> offset) & 1;
  242. }
  243. static int mxc_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  244. {
  245. _set_gpio_direction(chip, offset, 0);
  246. return 0;
  247. }
  248. static int mxc_gpio_direction_output(struct gpio_chip *chip,
  249. unsigned offset, int value)
  250. {
  251. mxc_gpio_set(chip, offset, value);
  252. _set_gpio_direction(chip, offset, 1);
  253. return 0;
  254. }
  255. /*
  256. * This lock class tells lockdep that GPIO irqs are in a different
  257. * category than their parents, so it won't report false recursion.
  258. */
  259. static struct lock_class_key gpio_lock_class;
  260. int __init mxc_gpio_init(struct mxc_gpio_port *port, int cnt)
  261. {
  262. int i, j;
  263. /* save for local usage */
  264. mxc_gpio_ports = port;
  265. gpio_table_size = cnt;
  266. printk(KERN_INFO "MXC GPIO hardware\n");
  267. for (i = 0; i < cnt; i++) {
  268. /* disable the interrupt and clear the status */
  269. __raw_writel(0, port[i].base + GPIO_IMR);
  270. __raw_writel(~0, port[i].base + GPIO_ISR);
  271. for (j = port[i].virtual_irq_start;
  272. j < port[i].virtual_irq_start + 32; j++) {
  273. irq_set_lockdep_class(j, &gpio_lock_class);
  274. irq_set_chip_and_handler(j, &gpio_irq_chip,
  275. handle_level_irq);
  276. set_irq_flags(j, IRQF_VALID);
  277. }
  278. /* register gpio chip */
  279. port[i].chip.direction_input = mxc_gpio_direction_input;
  280. port[i].chip.direction_output = mxc_gpio_direction_output;
  281. port[i].chip.get = mxc_gpio_get;
  282. port[i].chip.set = mxc_gpio_set;
  283. port[i].chip.base = i * 32;
  284. port[i].chip.ngpio = 32;
  285. spin_lock_init(&port[i].lock);
  286. /* its a serious configuration bug when it fails */
  287. BUG_ON( gpiochip_add(&port[i].chip) < 0 );
  288. if (cpu_is_mx1() || cpu_is_mx3() || cpu_is_mx25() || cpu_is_mx51()) {
  289. /* setup one handler for each entry */
  290. irq_set_chained_handler(port[i].irq,
  291. mx3_gpio_irq_handler);
  292. irq_set_handler_data(port[i].irq, &port[i]);
  293. if (port[i].irq_high) {
  294. /* setup handler for GPIO 16 to 31 */
  295. irq_set_chained_handler(port[i].irq_high,
  296. mx3_gpio_irq_handler);
  297. irq_set_handler_data(port[i].irq_high,
  298. &port[i]);
  299. }
  300. }
  301. }
  302. if (cpu_is_mx2()) {
  303. /* setup one handler for all GPIO interrupts */
  304. irq_set_chained_handler(port[0].irq, mx2_gpio_irq_handler);
  305. irq_set_handler_data(port[0].irq, port);
  306. }
  307. return 0;
  308. }