gpio-mxc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 <linux/platform_device.h>
  27. #include <linux/slab.h>
  28. #include <linux/basic_mmio_gpio.h>
  29. #include <mach/hardware.h>
  30. #include <asm-generic/bug.h>
  31. struct mxc_gpio_port {
  32. struct list_head node;
  33. void __iomem *base;
  34. int irq;
  35. int irq_high;
  36. int virtual_irq_start;
  37. struct bgpio_chip bgc;
  38. u32 both_edges;
  39. };
  40. /*
  41. * MX2 has one interrupt *for all* gpio ports. The list is used
  42. * to save the references to all ports, so that mx2_gpio_irq_handler
  43. * can walk through all interrupt status registers.
  44. */
  45. static LIST_HEAD(mxc_gpio_ports);
  46. #define cpu_is_mx1_mx2() (cpu_is_mx1() || cpu_is_mx2())
  47. #define GPIO_DR (cpu_is_mx1_mx2() ? 0x1c : 0x00)
  48. #define GPIO_GDIR (cpu_is_mx1_mx2() ? 0x00 : 0x04)
  49. #define GPIO_PSR (cpu_is_mx1_mx2() ? 0x24 : 0x08)
  50. #define GPIO_ICR1 (cpu_is_mx1_mx2() ? 0x28 : 0x0C)
  51. #define GPIO_ICR2 (cpu_is_mx1_mx2() ? 0x2C : 0x10)
  52. #define GPIO_IMR (cpu_is_mx1_mx2() ? 0x30 : 0x14)
  53. #define GPIO_ISR (cpu_is_mx1_mx2() ? 0x34 : 0x18)
  54. #define GPIO_INT_LOW_LEV (cpu_is_mx1_mx2() ? 0x3 : 0x0)
  55. #define GPIO_INT_HIGH_LEV (cpu_is_mx1_mx2() ? 0x2 : 0x1)
  56. #define GPIO_INT_RISE_EDGE (cpu_is_mx1_mx2() ? 0x0 : 0x2)
  57. #define GPIO_INT_FALL_EDGE (cpu_is_mx1_mx2() ? 0x1 : 0x3)
  58. #define GPIO_INT_NONE 0x4
  59. /* Note: This driver assumes 32 GPIOs are handled in one register */
  60. static void _clear_gpio_irqstatus(struct mxc_gpio_port *port, u32 index)
  61. {
  62. writel(1 << index, port->base + GPIO_ISR);
  63. }
  64. static void _set_gpio_irqenable(struct mxc_gpio_port *port, u32 index,
  65. int enable)
  66. {
  67. u32 l;
  68. l = readl(port->base + GPIO_IMR);
  69. l = (l & (~(1 << index))) | (!!enable << index);
  70. writel(l, port->base + GPIO_IMR);
  71. }
  72. static void gpio_ack_irq(struct irq_data *d)
  73. {
  74. struct mxc_gpio_port *port = irq_data_get_irq_chip_data(d);
  75. u32 gpio = irq_to_gpio(d->irq);
  76. _clear_gpio_irqstatus(port, gpio & 0x1f);
  77. }
  78. static void gpio_mask_irq(struct irq_data *d)
  79. {
  80. struct mxc_gpio_port *port = irq_data_get_irq_chip_data(d);
  81. u32 gpio = irq_to_gpio(d->irq);
  82. _set_gpio_irqenable(port, gpio & 0x1f, 0);
  83. }
  84. static void gpio_unmask_irq(struct irq_data *d)
  85. {
  86. struct mxc_gpio_port *port = irq_data_get_irq_chip_data(d);
  87. u32 gpio = irq_to_gpio(d->irq);
  88. _set_gpio_irqenable(port, gpio & 0x1f, 1);
  89. }
  90. static int gpio_set_irq_type(struct irq_data *d, u32 type)
  91. {
  92. u32 gpio = irq_to_gpio(d->irq);
  93. struct mxc_gpio_port *port = irq_data_get_irq_chip_data(d);
  94. u32 bit, val;
  95. int edge;
  96. void __iomem *reg = port->base;
  97. port->both_edges &= ~(1 << (gpio & 31));
  98. switch (type) {
  99. case IRQ_TYPE_EDGE_RISING:
  100. edge = GPIO_INT_RISE_EDGE;
  101. break;
  102. case IRQ_TYPE_EDGE_FALLING:
  103. edge = GPIO_INT_FALL_EDGE;
  104. break;
  105. case IRQ_TYPE_EDGE_BOTH:
  106. val = gpio_get_value(gpio & 31);
  107. if (val) {
  108. edge = GPIO_INT_LOW_LEV;
  109. pr_debug("mxc: set GPIO %d to low trigger\n", gpio);
  110. } else {
  111. edge = GPIO_INT_HIGH_LEV;
  112. pr_debug("mxc: set GPIO %d to high trigger\n", gpio);
  113. }
  114. port->both_edges |= 1 << (gpio & 31);
  115. break;
  116. case IRQ_TYPE_LEVEL_LOW:
  117. edge = GPIO_INT_LOW_LEV;
  118. break;
  119. case IRQ_TYPE_LEVEL_HIGH:
  120. edge = GPIO_INT_HIGH_LEV;
  121. break;
  122. default:
  123. return -EINVAL;
  124. }
  125. reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
  126. bit = gpio & 0xf;
  127. val = readl(reg) & ~(0x3 << (bit << 1));
  128. writel(val | (edge << (bit << 1)), reg);
  129. _clear_gpio_irqstatus(port, gpio & 0x1f);
  130. return 0;
  131. }
  132. static void mxc_flip_edge(struct mxc_gpio_port *port, u32 gpio)
  133. {
  134. void __iomem *reg = port->base;
  135. u32 bit, val;
  136. int edge;
  137. reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
  138. bit = gpio & 0xf;
  139. val = readl(reg);
  140. edge = (val >> (bit << 1)) & 3;
  141. val &= ~(0x3 << (bit << 1));
  142. if (edge == GPIO_INT_HIGH_LEV) {
  143. edge = GPIO_INT_LOW_LEV;
  144. pr_debug("mxc: switch GPIO %d to low trigger\n", gpio);
  145. } else if (edge == GPIO_INT_LOW_LEV) {
  146. edge = GPIO_INT_HIGH_LEV;
  147. pr_debug("mxc: switch GPIO %d to high trigger\n", gpio);
  148. } else {
  149. pr_err("mxc: invalid configuration for GPIO %d: %x\n",
  150. gpio, edge);
  151. return;
  152. }
  153. writel(val | (edge << (bit << 1)), reg);
  154. }
  155. /* handle 32 interrupts in one status register */
  156. static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat)
  157. {
  158. u32 gpio_irq_no_base = port->virtual_irq_start;
  159. while (irq_stat != 0) {
  160. int irqoffset = fls(irq_stat) - 1;
  161. if (port->both_edges & (1 << irqoffset))
  162. mxc_flip_edge(port, irqoffset);
  163. generic_handle_irq(gpio_irq_no_base + irqoffset);
  164. irq_stat &= ~(1 << irqoffset);
  165. }
  166. }
  167. /* MX1 and MX3 has one interrupt *per* gpio port */
  168. static void mx3_gpio_irq_handler(u32 irq, struct irq_desc *desc)
  169. {
  170. u32 irq_stat;
  171. struct mxc_gpio_port *port = irq_get_handler_data(irq);
  172. irq_stat = readl(port->base + GPIO_ISR) & readl(port->base + GPIO_IMR);
  173. mxc_gpio_irq_handler(port, irq_stat);
  174. }
  175. /* MX2 has one interrupt *for all* gpio ports */
  176. static void mx2_gpio_irq_handler(u32 irq, struct irq_desc *desc)
  177. {
  178. u32 irq_msk, irq_stat;
  179. struct mxc_gpio_port *port;
  180. /* walk through all interrupt status registers */
  181. list_for_each_entry(port, &mxc_gpio_ports, node) {
  182. irq_msk = readl(port->base + GPIO_IMR);
  183. if (!irq_msk)
  184. continue;
  185. irq_stat = readl(port->base + GPIO_ISR) & irq_msk;
  186. if (irq_stat)
  187. mxc_gpio_irq_handler(port, irq_stat);
  188. }
  189. }
  190. /*
  191. * Set interrupt number "irq" in the GPIO as a wake-up source.
  192. * While system is running, all registered GPIO interrupts need to have
  193. * wake-up enabled. When system is suspended, only selected GPIO interrupts
  194. * need to have wake-up enabled.
  195. * @param irq interrupt source number
  196. * @param enable enable as wake-up if equal to non-zero
  197. * @return This function returns 0 on success.
  198. */
  199. static int gpio_set_wake_irq(struct irq_data *d, u32 enable)
  200. {
  201. u32 gpio = irq_to_gpio(d->irq);
  202. u32 gpio_idx = gpio & 0x1F;
  203. struct mxc_gpio_port *port = irq_data_get_irq_chip_data(d);
  204. if (enable) {
  205. if (port->irq_high && (gpio_idx >= 16))
  206. enable_irq_wake(port->irq_high);
  207. else
  208. enable_irq_wake(port->irq);
  209. } else {
  210. if (port->irq_high && (gpio_idx >= 16))
  211. disable_irq_wake(port->irq_high);
  212. else
  213. disable_irq_wake(port->irq);
  214. }
  215. return 0;
  216. }
  217. static struct irq_chip gpio_irq_chip = {
  218. .name = "GPIO",
  219. .irq_ack = gpio_ack_irq,
  220. .irq_mask = gpio_mask_irq,
  221. .irq_unmask = gpio_unmask_irq,
  222. .irq_set_type = gpio_set_irq_type,
  223. .irq_set_wake = gpio_set_wake_irq,
  224. };
  225. /*
  226. * This lock class tells lockdep that GPIO irqs are in a different
  227. * category than their parents, so it won't report false recursion.
  228. */
  229. static struct lock_class_key gpio_lock_class;
  230. static int __devinit mxc_gpio_probe(struct platform_device *pdev)
  231. {
  232. struct mxc_gpio_port *port;
  233. struct resource *iores;
  234. int err, i;
  235. port = kzalloc(sizeof(struct mxc_gpio_port), GFP_KERNEL);
  236. if (!port)
  237. return -ENOMEM;
  238. port->virtual_irq_start = MXC_GPIO_IRQ_START + pdev->id * 32;
  239. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  240. if (!iores) {
  241. err = -ENODEV;
  242. goto out_kfree;
  243. }
  244. if (!request_mem_region(iores->start, resource_size(iores),
  245. pdev->name)) {
  246. err = -EBUSY;
  247. goto out_kfree;
  248. }
  249. port->base = ioremap(iores->start, resource_size(iores));
  250. if (!port->base) {
  251. err = -ENOMEM;
  252. goto out_release_mem;
  253. }
  254. port->irq_high = platform_get_irq(pdev, 1);
  255. port->irq = platform_get_irq(pdev, 0);
  256. if (port->irq < 0) {
  257. err = -EINVAL;
  258. goto out_iounmap;
  259. }
  260. /* disable the interrupt and clear the status */
  261. writel(0, port->base + GPIO_IMR);
  262. writel(~0, port->base + GPIO_ISR);
  263. for (i = port->virtual_irq_start;
  264. i < port->virtual_irq_start + 32; i++) {
  265. irq_set_lockdep_class(i, &gpio_lock_class);
  266. irq_set_chip_and_handler(i, &gpio_irq_chip, handle_level_irq);
  267. set_irq_flags(i, IRQF_VALID);
  268. irq_set_chip_data(i, port);
  269. }
  270. if (cpu_is_mx2()) {
  271. /* setup one handler for all GPIO interrupts */
  272. if (pdev->id == 0)
  273. irq_set_chained_handler(port->irq,
  274. mx2_gpio_irq_handler);
  275. } else {
  276. /* setup one handler for each entry */
  277. irq_set_chained_handler(port->irq, mx3_gpio_irq_handler);
  278. irq_set_handler_data(port->irq, port);
  279. if (port->irq_high > 0) {
  280. /* setup handler for GPIO 16 to 31 */
  281. irq_set_chained_handler(port->irq_high,
  282. mx3_gpio_irq_handler);
  283. irq_set_handler_data(port->irq_high, port);
  284. }
  285. }
  286. err = bgpio_init(&port->bgc, &pdev->dev, 4,
  287. port->base + GPIO_PSR,
  288. port->base + GPIO_DR, NULL,
  289. port->base + GPIO_GDIR, NULL, false);
  290. if (err)
  291. goto out_iounmap;
  292. port->bgc.gc.base = pdev->id * 32;
  293. err = gpiochip_add(&port->bgc.gc);
  294. if (err)
  295. goto out_bgpio_remove;
  296. list_add_tail(&port->node, &mxc_gpio_ports);
  297. return 0;
  298. out_bgpio_remove:
  299. bgpio_remove(&port->bgc);
  300. out_iounmap:
  301. iounmap(port->base);
  302. out_release_mem:
  303. release_mem_region(iores->start, resource_size(iores));
  304. out_kfree:
  305. kfree(port);
  306. dev_info(&pdev->dev, "%s failed with errno %d\n", __func__, err);
  307. return err;
  308. }
  309. static struct platform_driver mxc_gpio_driver = {
  310. .driver = {
  311. .name = "gpio-mxc",
  312. .owner = THIS_MODULE,
  313. },
  314. .probe = mxc_gpio_probe,
  315. };
  316. static int __init gpio_mxc_init(void)
  317. {
  318. return platform_driver_register(&mxc_gpio_driver);
  319. }
  320. postcore_initcall(gpio_mxc_init);
  321. MODULE_AUTHOR("Freescale Semiconductor, "
  322. "Daniel Mack <danielncaiaq.de>, "
  323. "Juergen Beisert <kernel@pengutronix.de>");
  324. MODULE_DESCRIPTION("Freescale MXC GPIO");
  325. MODULE_LICENSE("GPL");