gpio-mxc.c 13 KB

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