gpio-mxc.c 13 KB

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