gpio-mxc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. #define irq_to_gpio(irq) ((irq) - MXC_GPIO_IRQ_START)
  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. int virtual_irq_start;
  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. u32 gpio = irq_to_gpio(d->irq);
  133. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  134. struct mxc_gpio_port *port = gc->private;
  135. u32 bit, val;
  136. int edge;
  137. void __iomem *reg = port->base;
  138. port->both_edges &= ~(1 << (gpio & 31));
  139. switch (type) {
  140. case IRQ_TYPE_EDGE_RISING:
  141. edge = GPIO_INT_RISE_EDGE;
  142. break;
  143. case IRQ_TYPE_EDGE_FALLING:
  144. edge = GPIO_INT_FALL_EDGE;
  145. break;
  146. case IRQ_TYPE_EDGE_BOTH:
  147. val = gpio_get_value(gpio);
  148. if (val) {
  149. edge = GPIO_INT_LOW_LEV;
  150. pr_debug("mxc: set GPIO %d to low trigger\n", gpio);
  151. } else {
  152. edge = GPIO_INT_HIGH_LEV;
  153. pr_debug("mxc: set GPIO %d to high trigger\n", gpio);
  154. }
  155. port->both_edges |= 1 << (gpio & 31);
  156. break;
  157. case IRQ_TYPE_LEVEL_LOW:
  158. edge = GPIO_INT_LOW_LEV;
  159. break;
  160. case IRQ_TYPE_LEVEL_HIGH:
  161. edge = GPIO_INT_HIGH_LEV;
  162. break;
  163. default:
  164. return -EINVAL;
  165. }
  166. reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
  167. bit = gpio & 0xf;
  168. val = readl(reg) & ~(0x3 << (bit << 1));
  169. writel(val | (edge << (bit << 1)), reg);
  170. writel(1 << (gpio & 0x1f), port->base + GPIO_ISR);
  171. return 0;
  172. }
  173. static void mxc_flip_edge(struct mxc_gpio_port *port, u32 gpio)
  174. {
  175. void __iomem *reg = port->base;
  176. u32 bit, val;
  177. int edge;
  178. reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
  179. bit = gpio & 0xf;
  180. val = readl(reg);
  181. edge = (val >> (bit << 1)) & 3;
  182. val &= ~(0x3 << (bit << 1));
  183. if (edge == GPIO_INT_HIGH_LEV) {
  184. edge = GPIO_INT_LOW_LEV;
  185. pr_debug("mxc: switch GPIO %d to low trigger\n", gpio);
  186. } else if (edge == GPIO_INT_LOW_LEV) {
  187. edge = GPIO_INT_HIGH_LEV;
  188. pr_debug("mxc: switch GPIO %d to high trigger\n", gpio);
  189. } else {
  190. pr_err("mxc: invalid configuration for GPIO %d: %x\n",
  191. gpio, edge);
  192. return;
  193. }
  194. writel(val | (edge << (bit << 1)), reg);
  195. }
  196. /* handle 32 interrupts in one status register */
  197. static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat)
  198. {
  199. u32 gpio_irq_no_base = port->virtual_irq_start;
  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(gpio_irq_no_base + 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. u32 gpio = irq_to_gpio(d->irq);
  246. u32 gpio_idx = gpio & 0x1F;
  247. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  248. struct mxc_gpio_port *port = gc->private;
  249. if (enable) {
  250. if (port->irq_high && (gpio_idx >= 16))
  251. enable_irq_wake(port->irq_high);
  252. else
  253. enable_irq_wake(port->irq);
  254. } else {
  255. if (port->irq_high && (gpio_idx >= 16))
  256. disable_irq_wake(port->irq_high);
  257. else
  258. disable_irq_wake(port->irq);
  259. }
  260. return 0;
  261. }
  262. static void __init mxc_gpio_init_gc(struct mxc_gpio_port *port)
  263. {
  264. struct irq_chip_generic *gc;
  265. struct irq_chip_type *ct;
  266. gc = irq_alloc_generic_chip("gpio-mxc", 1, port->virtual_irq_start,
  267. port->base, handle_level_irq);
  268. gc->private = port;
  269. ct = gc->chip_types;
  270. ct->chip.irq_ack = irq_gc_ack_set_bit;
  271. ct->chip.irq_mask = irq_gc_mask_clr_bit;
  272. ct->chip.irq_unmask = irq_gc_mask_set_bit;
  273. ct->chip.irq_set_type = gpio_set_irq_type;
  274. ct->chip.irq_set_wake = gpio_set_wake_irq;
  275. ct->regs.ack = GPIO_ISR;
  276. ct->regs.mask = GPIO_IMR;
  277. irq_setup_generic_chip(gc, IRQ_MSK(32), IRQ_GC_INIT_NESTED_LOCK,
  278. IRQ_NOREQUEST, 0);
  279. }
  280. static void __devinit mxc_gpio_get_hw(struct platform_device *pdev)
  281. {
  282. const struct of_device_id *of_id =
  283. of_match_device(mxc_gpio_dt_ids, &pdev->dev);
  284. enum mxc_gpio_hwtype hwtype;
  285. if (of_id)
  286. pdev->id_entry = of_id->data;
  287. hwtype = pdev->id_entry->driver_data;
  288. if (mxc_gpio_hwtype) {
  289. /*
  290. * The driver works with a reasonable presupposition,
  291. * that is all gpio ports must be the same type when
  292. * running on one soc.
  293. */
  294. BUG_ON(mxc_gpio_hwtype != hwtype);
  295. return;
  296. }
  297. if (hwtype == IMX31_GPIO)
  298. mxc_gpio_hwdata = &imx31_gpio_hwdata;
  299. else
  300. mxc_gpio_hwdata = &imx1_imx21_gpio_hwdata;
  301. mxc_gpio_hwtype = hwtype;
  302. }
  303. static int mxc_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  304. {
  305. struct bgpio_chip *bgc = to_bgpio_chip(gc);
  306. struct mxc_gpio_port *port =
  307. container_of(bgc, struct mxc_gpio_port, bgc);
  308. return port->virtual_irq_start + offset;
  309. }
  310. static int __devinit mxc_gpio_probe(struct platform_device *pdev)
  311. {
  312. struct device_node *np = pdev->dev.of_node;
  313. struct mxc_gpio_port *port;
  314. struct resource *iores;
  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. /* setup one handler for all GPIO interrupts */
  346. if (pdev->id == 0)
  347. irq_set_chained_handler(port->irq,
  348. mx2_gpio_irq_handler);
  349. } else {
  350. /* setup one handler for each entry */
  351. irq_set_chained_handler(port->irq, mx3_gpio_irq_handler);
  352. irq_set_handler_data(port->irq, port);
  353. if (port->irq_high > 0) {
  354. /* setup handler for GPIO 16 to 31 */
  355. irq_set_chained_handler(port->irq_high,
  356. mx3_gpio_irq_handler);
  357. irq_set_handler_data(port->irq_high, port);
  358. }
  359. }
  360. err = bgpio_init(&port->bgc, &pdev->dev, 4,
  361. port->base + GPIO_PSR,
  362. port->base + GPIO_DR, NULL,
  363. port->base + GPIO_GDIR, NULL, false);
  364. if (err)
  365. goto out_iounmap;
  366. port->bgc.gc.to_irq = mxc_gpio_to_irq;
  367. port->bgc.gc.base = pdev->id * 32;
  368. port->bgc.dir = port->bgc.read_reg(port->bgc.reg_dir);
  369. port->bgc.data = port->bgc.read_reg(port->bgc.reg_set);
  370. err = gpiochip_add(&port->bgc.gc);
  371. if (err)
  372. goto out_bgpio_remove;
  373. /*
  374. * In dt case, we use gpio number range dynamically
  375. * allocated by gpio core.
  376. */
  377. port->virtual_irq_start = MXC_GPIO_IRQ_START + (np ? port->bgc.gc.base :
  378. pdev->id * 32);
  379. /* gpio-mxc can be a generic irq chip */
  380. mxc_gpio_init_gc(port);
  381. list_add_tail(&port->node, &mxc_gpio_ports);
  382. return 0;
  383. out_bgpio_remove:
  384. bgpio_remove(&port->bgc);
  385. out_iounmap:
  386. iounmap(port->base);
  387. out_release_mem:
  388. release_mem_region(iores->start, resource_size(iores));
  389. out_kfree:
  390. kfree(port);
  391. dev_info(&pdev->dev, "%s failed with errno %d\n", __func__, err);
  392. return err;
  393. }
  394. static struct platform_driver mxc_gpio_driver = {
  395. .driver = {
  396. .name = "gpio-mxc",
  397. .owner = THIS_MODULE,
  398. .of_match_table = mxc_gpio_dt_ids,
  399. },
  400. .probe = mxc_gpio_probe,
  401. .id_table = mxc_gpio_devtype,
  402. };
  403. static int __init gpio_mxc_init(void)
  404. {
  405. return platform_driver_register(&mxc_gpio_driver);
  406. }
  407. postcore_initcall(gpio_mxc_init);
  408. MODULE_AUTHOR("Freescale Semiconductor, "
  409. "Daniel Mack <danielncaiaq.de>, "
  410. "Juergen Beisert <kernel@pengutronix.de>");
  411. MODULE_DESCRIPTION("Freescale MXC GPIO");
  412. MODULE_LICENSE("GPL");