gpio-mxc.c 12 KB

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