gpio-mxc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 <linux/of.h>
  30. #include <linux/of_device.h>
  31. #include <linux/module.h>
  32. #include <asm-generic/bug.h>
  33. #include <asm/mach/irq.h>
  34. enum mxc_gpio_hwtype {
  35. IMX1_GPIO, /* runs on i.mx1 */
  36. IMX21_GPIO, /* runs on i.mx21 and i.mx27 */
  37. IMX31_GPIO, /* runs on all other i.mx */
  38. };
  39. /* device type dependent stuff */
  40. struct mxc_gpio_hwdata {
  41. unsigned dr_reg;
  42. unsigned gdir_reg;
  43. unsigned psr_reg;
  44. unsigned icr1_reg;
  45. unsigned icr2_reg;
  46. unsigned imr_reg;
  47. unsigned isr_reg;
  48. unsigned low_level;
  49. unsigned high_level;
  50. unsigned rise_edge;
  51. unsigned fall_edge;
  52. };
  53. struct mxc_gpio_port {
  54. struct list_head node;
  55. void __iomem *base;
  56. int irq;
  57. int irq_high;
  58. int virtual_irq_start;
  59. struct bgpio_chip bgc;
  60. u32 both_edges;
  61. };
  62. static struct mxc_gpio_hwdata imx1_imx21_gpio_hwdata = {
  63. .dr_reg = 0x1c,
  64. .gdir_reg = 0x00,
  65. .psr_reg = 0x24,
  66. .icr1_reg = 0x28,
  67. .icr2_reg = 0x2c,
  68. .imr_reg = 0x30,
  69. .isr_reg = 0x34,
  70. .low_level = 0x03,
  71. .high_level = 0x02,
  72. .rise_edge = 0x00,
  73. .fall_edge = 0x01,
  74. };
  75. static struct mxc_gpio_hwdata imx31_gpio_hwdata = {
  76. .dr_reg = 0x00,
  77. .gdir_reg = 0x04,
  78. .psr_reg = 0x08,
  79. .icr1_reg = 0x0c,
  80. .icr2_reg = 0x10,
  81. .imr_reg = 0x14,
  82. .isr_reg = 0x18,
  83. .low_level = 0x00,
  84. .high_level = 0x01,
  85. .rise_edge = 0x02,
  86. .fall_edge = 0x03,
  87. };
  88. static enum mxc_gpio_hwtype mxc_gpio_hwtype;
  89. static struct mxc_gpio_hwdata *mxc_gpio_hwdata;
  90. #define GPIO_DR (mxc_gpio_hwdata->dr_reg)
  91. #define GPIO_GDIR (mxc_gpio_hwdata->gdir_reg)
  92. #define GPIO_PSR (mxc_gpio_hwdata->psr_reg)
  93. #define GPIO_ICR1 (mxc_gpio_hwdata->icr1_reg)
  94. #define GPIO_ICR2 (mxc_gpio_hwdata->icr2_reg)
  95. #define GPIO_IMR (mxc_gpio_hwdata->imr_reg)
  96. #define GPIO_ISR (mxc_gpio_hwdata->isr_reg)
  97. #define GPIO_INT_LOW_LEV (mxc_gpio_hwdata->low_level)
  98. #define GPIO_INT_HIGH_LEV (mxc_gpio_hwdata->high_level)
  99. #define GPIO_INT_RISE_EDGE (mxc_gpio_hwdata->rise_edge)
  100. #define GPIO_INT_FALL_EDGE (mxc_gpio_hwdata->fall_edge)
  101. #define GPIO_INT_NONE 0x4
  102. static struct platform_device_id mxc_gpio_devtype[] = {
  103. {
  104. .name = "imx1-gpio",
  105. .driver_data = IMX1_GPIO,
  106. }, {
  107. .name = "imx21-gpio",
  108. .driver_data = IMX21_GPIO,
  109. }, {
  110. .name = "imx31-gpio",
  111. .driver_data = IMX31_GPIO,
  112. }, {
  113. /* sentinel */
  114. }
  115. };
  116. static const struct of_device_id mxc_gpio_dt_ids[] = {
  117. { .compatible = "fsl,imx1-gpio", .data = &mxc_gpio_devtype[IMX1_GPIO], },
  118. { .compatible = "fsl,imx21-gpio", .data = &mxc_gpio_devtype[IMX21_GPIO], },
  119. { .compatible = "fsl,imx31-gpio", .data = &mxc_gpio_devtype[IMX31_GPIO], },
  120. { /* sentinel */ }
  121. };
  122. /*
  123. * MX2 has one interrupt *for all* gpio ports. The list is used
  124. * to save the references to all ports, so that mx2_gpio_irq_handler
  125. * can walk through all interrupt status registers.
  126. */
  127. static LIST_HEAD(mxc_gpio_ports);
  128. /* Note: This driver assumes 32 GPIOs are handled in one register */
  129. static int gpio_set_irq_type(struct irq_data *d, u32 type)
  130. {
  131. u32 gpio = irq_to_gpio(d->irq);
  132. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  133. struct mxc_gpio_port *port = gc->private;
  134. u32 bit, val;
  135. int edge;
  136. void __iomem *reg = port->base;
  137. port->both_edges &= ~(1 << (gpio & 31));
  138. switch (type) {
  139. case IRQ_TYPE_EDGE_RISING:
  140. edge = GPIO_INT_RISE_EDGE;
  141. break;
  142. case IRQ_TYPE_EDGE_FALLING:
  143. edge = GPIO_INT_FALL_EDGE;
  144. break;
  145. case IRQ_TYPE_EDGE_BOTH:
  146. val = gpio_get_value(gpio);
  147. if (val) {
  148. edge = GPIO_INT_LOW_LEV;
  149. pr_debug("mxc: set GPIO %d to low trigger\n", gpio);
  150. } else {
  151. edge = GPIO_INT_HIGH_LEV;
  152. pr_debug("mxc: set GPIO %d to high trigger\n", gpio);
  153. }
  154. port->both_edges |= 1 << (gpio & 31);
  155. break;
  156. case IRQ_TYPE_LEVEL_LOW:
  157. edge = GPIO_INT_LOW_LEV;
  158. break;
  159. case IRQ_TYPE_LEVEL_HIGH:
  160. edge = GPIO_INT_HIGH_LEV;
  161. break;
  162. default:
  163. return -EINVAL;
  164. }
  165. reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
  166. bit = gpio & 0xf;
  167. val = readl(reg) & ~(0x3 << (bit << 1));
  168. writel(val | (edge << (bit << 1)), reg);
  169. writel(1 << (gpio & 0x1f), port->base + GPIO_ISR);
  170. return 0;
  171. }
  172. static void mxc_flip_edge(struct mxc_gpio_port *port, u32 gpio)
  173. {
  174. void __iomem *reg = port->base;
  175. u32 bit, val;
  176. int edge;
  177. reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
  178. bit = gpio & 0xf;
  179. val = readl(reg);
  180. edge = (val >> (bit << 1)) & 3;
  181. val &= ~(0x3 << (bit << 1));
  182. if (edge == GPIO_INT_HIGH_LEV) {
  183. edge = GPIO_INT_LOW_LEV;
  184. pr_debug("mxc: switch GPIO %d to low trigger\n", gpio);
  185. } else if (edge == GPIO_INT_LOW_LEV) {
  186. edge = GPIO_INT_HIGH_LEV;
  187. pr_debug("mxc: switch GPIO %d to high trigger\n", gpio);
  188. } else {
  189. pr_err("mxc: invalid configuration for GPIO %d: %x\n",
  190. gpio, edge);
  191. return;
  192. }
  193. writel(val | (edge << (bit << 1)), reg);
  194. }
  195. /* handle 32 interrupts in one status register */
  196. static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat)
  197. {
  198. u32 gpio_irq_no_base = port->virtual_irq_start;
  199. while (irq_stat != 0) {
  200. int irqoffset = fls(irq_stat) - 1;
  201. if (port->both_edges & (1 << irqoffset))
  202. mxc_flip_edge(port, irqoffset);
  203. generic_handle_irq(gpio_irq_no_base + irqoffset);
  204. irq_stat &= ~(1 << irqoffset);
  205. }
  206. }
  207. /* MX1 and MX3 has one interrupt *per* gpio port */
  208. static void mx3_gpio_irq_handler(u32 irq, struct irq_desc *desc)
  209. {
  210. u32 irq_stat;
  211. struct mxc_gpio_port *port = irq_get_handler_data(irq);
  212. struct irq_chip *chip = irq_get_chip(irq);
  213. chained_irq_enter(chip, desc);
  214. irq_stat = readl(port->base + GPIO_ISR) & readl(port->base + GPIO_IMR);
  215. mxc_gpio_irq_handler(port, irq_stat);
  216. chained_irq_exit(chip, desc);
  217. }
  218. /* MX2 has one interrupt *for all* gpio ports */
  219. static void mx2_gpio_irq_handler(u32 irq, struct irq_desc *desc)
  220. {
  221. u32 irq_msk, irq_stat;
  222. struct mxc_gpio_port *port;
  223. /* walk through all interrupt status registers */
  224. list_for_each_entry(port, &mxc_gpio_ports, node) {
  225. irq_msk = readl(port->base + GPIO_IMR);
  226. if (!irq_msk)
  227. continue;
  228. irq_stat = readl(port->base + GPIO_ISR) & irq_msk;
  229. if (irq_stat)
  230. mxc_gpio_irq_handler(port, irq_stat);
  231. }
  232. }
  233. /*
  234. * Set interrupt number "irq" in the GPIO as a wake-up source.
  235. * While system is running, all registered GPIO interrupts need to have
  236. * wake-up enabled. When system is suspended, only selected GPIO interrupts
  237. * need to have wake-up enabled.
  238. * @param irq interrupt source number
  239. * @param enable enable as wake-up if equal to non-zero
  240. * @return This function returns 0 on success.
  241. */
  242. static int gpio_set_wake_irq(struct irq_data *d, u32 enable)
  243. {
  244. u32 gpio = irq_to_gpio(d->irq);
  245. u32 gpio_idx = gpio & 0x1F;
  246. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  247. struct mxc_gpio_port *port = gc->private;
  248. if (enable) {
  249. if (port->irq_high && (gpio_idx >= 16))
  250. enable_irq_wake(port->irq_high);
  251. else
  252. enable_irq_wake(port->irq);
  253. } else {
  254. if (port->irq_high && (gpio_idx >= 16))
  255. disable_irq_wake(port->irq_high);
  256. else
  257. disable_irq_wake(port->irq);
  258. }
  259. return 0;
  260. }
  261. static void __init mxc_gpio_init_gc(struct mxc_gpio_port *port)
  262. {
  263. struct irq_chip_generic *gc;
  264. struct irq_chip_type *ct;
  265. gc = irq_alloc_generic_chip("gpio-mxc", 1, port->virtual_irq_start,
  266. port->base, handle_level_irq);
  267. gc->private = port;
  268. ct = gc->chip_types;
  269. ct->chip.irq_ack = irq_gc_ack_set_bit;
  270. ct->chip.irq_mask = irq_gc_mask_clr_bit;
  271. ct->chip.irq_unmask = irq_gc_mask_set_bit;
  272. ct->chip.irq_set_type = gpio_set_irq_type;
  273. ct->chip.irq_set_wake = gpio_set_wake_irq;
  274. ct->regs.ack = GPIO_ISR;
  275. ct->regs.mask = GPIO_IMR;
  276. irq_setup_generic_chip(gc, IRQ_MSK(32), IRQ_GC_INIT_NESTED_LOCK,
  277. IRQ_NOREQUEST, 0);
  278. }
  279. static void __devinit mxc_gpio_get_hw(struct platform_device *pdev)
  280. {
  281. const struct of_device_id *of_id =
  282. of_match_device(mxc_gpio_dt_ids, &pdev->dev);
  283. enum mxc_gpio_hwtype hwtype;
  284. if (of_id)
  285. pdev->id_entry = of_id->data;
  286. hwtype = pdev->id_entry->driver_data;
  287. if (mxc_gpio_hwtype) {
  288. /*
  289. * The driver works with a reasonable presupposition,
  290. * that is all gpio ports must be the same type when
  291. * running on one soc.
  292. */
  293. BUG_ON(mxc_gpio_hwtype != hwtype);
  294. return;
  295. }
  296. if (hwtype == IMX31_GPIO)
  297. mxc_gpio_hwdata = &imx31_gpio_hwdata;
  298. else
  299. mxc_gpio_hwdata = &imx1_imx21_gpio_hwdata;
  300. mxc_gpio_hwtype = hwtype;
  301. }
  302. static int __devinit mxc_gpio_probe(struct platform_device *pdev)
  303. {
  304. struct device_node *np = pdev->dev.of_node;
  305. struct mxc_gpio_port *port;
  306. struct resource *iores;
  307. int err;
  308. mxc_gpio_get_hw(pdev);
  309. port = kzalloc(sizeof(struct mxc_gpio_port), GFP_KERNEL);
  310. if (!port)
  311. return -ENOMEM;
  312. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  313. if (!iores) {
  314. err = -ENODEV;
  315. goto out_kfree;
  316. }
  317. if (!request_mem_region(iores->start, resource_size(iores),
  318. pdev->name)) {
  319. err = -EBUSY;
  320. goto out_kfree;
  321. }
  322. port->base = ioremap(iores->start, resource_size(iores));
  323. if (!port->base) {
  324. err = -ENOMEM;
  325. goto out_release_mem;
  326. }
  327. port->irq_high = platform_get_irq(pdev, 1);
  328. port->irq = platform_get_irq(pdev, 0);
  329. if (port->irq < 0) {
  330. err = -EINVAL;
  331. goto out_iounmap;
  332. }
  333. /* disable the interrupt and clear the status */
  334. writel(0, port->base + GPIO_IMR);
  335. writel(~0, port->base + GPIO_ISR);
  336. if (mxc_gpio_hwtype == IMX21_GPIO) {
  337. /* setup one handler for all GPIO interrupts */
  338. if (pdev->id == 0)
  339. irq_set_chained_handler(port->irq,
  340. mx2_gpio_irq_handler);
  341. } else {
  342. /* setup one handler for each entry */
  343. irq_set_chained_handler(port->irq, mx3_gpio_irq_handler);
  344. irq_set_handler_data(port->irq, port);
  345. if (port->irq_high > 0) {
  346. /* setup handler for GPIO 16 to 31 */
  347. irq_set_chained_handler(port->irq_high,
  348. mx3_gpio_irq_handler);
  349. irq_set_handler_data(port->irq_high, port);
  350. }
  351. }
  352. err = bgpio_init(&port->bgc, &pdev->dev, 4,
  353. port->base + GPIO_PSR,
  354. port->base + GPIO_DR, NULL,
  355. port->base + GPIO_GDIR, NULL, false);
  356. if (err)
  357. goto out_iounmap;
  358. port->bgc.gc.base = pdev->id * 32;
  359. port->bgc.dir = port->bgc.read_reg(port->bgc.reg_dir);
  360. port->bgc.data = port->bgc.read_reg(port->bgc.reg_set);
  361. err = gpiochip_add(&port->bgc.gc);
  362. if (err)
  363. goto out_bgpio_remove;
  364. /*
  365. * In dt case, we use gpio number range dynamically
  366. * allocated by gpio core.
  367. */
  368. port->virtual_irq_start = MXC_GPIO_IRQ_START + (np ? port->bgc.gc.base :
  369. pdev->id * 32);
  370. /* gpio-mxc can be a generic irq chip */
  371. mxc_gpio_init_gc(port);
  372. list_add_tail(&port->node, &mxc_gpio_ports);
  373. return 0;
  374. out_bgpio_remove:
  375. bgpio_remove(&port->bgc);
  376. out_iounmap:
  377. iounmap(port->base);
  378. out_release_mem:
  379. release_mem_region(iores->start, resource_size(iores));
  380. out_kfree:
  381. kfree(port);
  382. dev_info(&pdev->dev, "%s failed with errno %d\n", __func__, err);
  383. return err;
  384. }
  385. static struct platform_driver mxc_gpio_driver = {
  386. .driver = {
  387. .name = "gpio-mxc",
  388. .owner = THIS_MODULE,
  389. .of_match_table = mxc_gpio_dt_ids,
  390. },
  391. .probe = mxc_gpio_probe,
  392. .id_table = mxc_gpio_devtype,
  393. };
  394. static int __init gpio_mxc_init(void)
  395. {
  396. return platform_driver_register(&mxc_gpio_driver);
  397. }
  398. postcore_initcall(gpio_mxc_init);
  399. MODULE_AUTHOR("Freescale Semiconductor, "
  400. "Daniel Mack <danielncaiaq.de>, "
  401. "Juergen Beisert <kernel@pengutronix.de>");
  402. MODULE_DESCRIPTION("Freescale MXC GPIO");
  403. MODULE_LICENSE("GPL");