gpio-mxc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // MXC GPIO support. (c) 2008 Daniel Mack <[email protected]>
  4. // Copyright 2008 Juergen Beisert, [email protected]
  5. //
  6. // Based on code from Freescale Semiconductor,
  7. // Authors: Daniel Mack, Juergen Beisert.
  8. // Copyright (C) 2004-2010 Freescale Semiconductor, Inc. All Rights Reserved.
  9. #include <linux/clk.h>
  10. #include <linux/err.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/io.h>
  14. #include <linux/irq.h>
  15. #include <linux/irqdomain.h>
  16. #include <linux/irqchip/chained_irq.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/slab.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/syscore_ops.h>
  22. #include <linux/gpio/driver.h>
  23. #include <linux/of.h>
  24. #include <linux/of_device.h>
  25. #include <linux/bug.h>
  26. /* device type dependent stuff */
  27. struct mxc_gpio_hwdata {
  28. unsigned dr_reg;
  29. unsigned gdir_reg;
  30. unsigned psr_reg;
  31. unsigned icr1_reg;
  32. unsigned icr2_reg;
  33. unsigned imr_reg;
  34. unsigned isr_reg;
  35. int edge_sel_reg;
  36. unsigned low_level;
  37. unsigned high_level;
  38. unsigned rise_edge;
  39. unsigned fall_edge;
  40. };
  41. struct mxc_gpio_reg_saved {
  42. u32 icr1;
  43. u32 icr2;
  44. u32 imr;
  45. u32 gdir;
  46. u32 edge_sel;
  47. u32 dr;
  48. };
  49. struct mxc_gpio_port {
  50. struct list_head node;
  51. void __iomem *base;
  52. struct clk *clk;
  53. int irq;
  54. int irq_high;
  55. struct irq_domain *domain;
  56. struct gpio_chip gc;
  57. struct device *dev;
  58. u32 both_edges;
  59. struct mxc_gpio_reg_saved gpio_saved_reg;
  60. bool power_off;
  61. const struct mxc_gpio_hwdata *hwdata;
  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. .edge_sel_reg = -EINVAL,
  72. .low_level = 0x03,
  73. .high_level = 0x02,
  74. .rise_edge = 0x00,
  75. .fall_edge = 0x01,
  76. };
  77. static struct mxc_gpio_hwdata imx31_gpio_hwdata = {
  78. .dr_reg = 0x00,
  79. .gdir_reg = 0x04,
  80. .psr_reg = 0x08,
  81. .icr1_reg = 0x0c,
  82. .icr2_reg = 0x10,
  83. .imr_reg = 0x14,
  84. .isr_reg = 0x18,
  85. .edge_sel_reg = -EINVAL,
  86. .low_level = 0x00,
  87. .high_level = 0x01,
  88. .rise_edge = 0x02,
  89. .fall_edge = 0x03,
  90. };
  91. static struct mxc_gpio_hwdata imx35_gpio_hwdata = {
  92. .dr_reg = 0x00,
  93. .gdir_reg = 0x04,
  94. .psr_reg = 0x08,
  95. .icr1_reg = 0x0c,
  96. .icr2_reg = 0x10,
  97. .imr_reg = 0x14,
  98. .isr_reg = 0x18,
  99. .edge_sel_reg = 0x1c,
  100. .low_level = 0x00,
  101. .high_level = 0x01,
  102. .rise_edge = 0x02,
  103. .fall_edge = 0x03,
  104. };
  105. #define GPIO_DR (port->hwdata->dr_reg)
  106. #define GPIO_GDIR (port->hwdata->gdir_reg)
  107. #define GPIO_PSR (port->hwdata->psr_reg)
  108. #define GPIO_ICR1 (port->hwdata->icr1_reg)
  109. #define GPIO_ICR2 (port->hwdata->icr2_reg)
  110. #define GPIO_IMR (port->hwdata->imr_reg)
  111. #define GPIO_ISR (port->hwdata->isr_reg)
  112. #define GPIO_EDGE_SEL (port->hwdata->edge_sel_reg)
  113. #define GPIO_INT_LOW_LEV (port->hwdata->low_level)
  114. #define GPIO_INT_HIGH_LEV (port->hwdata->high_level)
  115. #define GPIO_INT_RISE_EDGE (port->hwdata->rise_edge)
  116. #define GPIO_INT_FALL_EDGE (port->hwdata->fall_edge)
  117. #define GPIO_INT_BOTH_EDGES 0x4
  118. static const struct of_device_id mxc_gpio_dt_ids[] = {
  119. { .compatible = "fsl,imx1-gpio", .data = &imx1_imx21_gpio_hwdata },
  120. { .compatible = "fsl,imx21-gpio", .data = &imx1_imx21_gpio_hwdata },
  121. { .compatible = "fsl,imx31-gpio", .data = &imx31_gpio_hwdata },
  122. { .compatible = "fsl,imx35-gpio", .data = &imx35_gpio_hwdata },
  123. { .compatible = "fsl,imx7d-gpio", .data = &imx35_gpio_hwdata },
  124. { /* sentinel */ }
  125. };
  126. MODULE_DEVICE_TABLE(of, mxc_gpio_dt_ids);
  127. /*
  128. * MX2 has one interrupt *for all* gpio ports. The list is used
  129. * to save the references to all ports, so that mx2_gpio_irq_handler
  130. * can walk through all interrupt status registers.
  131. */
  132. static LIST_HEAD(mxc_gpio_ports);
  133. /* Note: This driver assumes 32 GPIOs are handled in one register */
  134. static int gpio_set_irq_type(struct irq_data *d, u32 type)
  135. {
  136. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  137. struct mxc_gpio_port *port = gc->private;
  138. unsigned long flags;
  139. u32 bit, val;
  140. u32 gpio_idx = d->hwirq;
  141. int edge;
  142. void __iomem *reg = port->base;
  143. port->both_edges &= ~(1 << gpio_idx);
  144. switch (type) {
  145. case IRQ_TYPE_EDGE_RISING:
  146. edge = GPIO_INT_RISE_EDGE;
  147. break;
  148. case IRQ_TYPE_EDGE_FALLING:
  149. edge = GPIO_INT_FALL_EDGE;
  150. break;
  151. case IRQ_TYPE_EDGE_BOTH:
  152. if (GPIO_EDGE_SEL >= 0) {
  153. edge = GPIO_INT_BOTH_EDGES;
  154. } else {
  155. val = port->gc.get(&port->gc, gpio_idx);
  156. if (val) {
  157. edge = GPIO_INT_LOW_LEV;
  158. pr_debug("mxc: set GPIO %d to low trigger\n", gpio_idx);
  159. } else {
  160. edge = GPIO_INT_HIGH_LEV;
  161. pr_debug("mxc: set GPIO %d to high trigger\n", gpio_idx);
  162. }
  163. port->both_edges |= 1 << gpio_idx;
  164. }
  165. break;
  166. case IRQ_TYPE_LEVEL_LOW:
  167. edge = GPIO_INT_LOW_LEV;
  168. break;
  169. case IRQ_TYPE_LEVEL_HIGH:
  170. edge = GPIO_INT_HIGH_LEV;
  171. break;
  172. default:
  173. return -EINVAL;
  174. }
  175. raw_spin_lock_irqsave(&port->gc.bgpio_lock, flags);
  176. if (GPIO_EDGE_SEL >= 0) {
  177. val = readl(port->base + GPIO_EDGE_SEL);
  178. if (edge == GPIO_INT_BOTH_EDGES)
  179. writel(val | (1 << gpio_idx),
  180. port->base + GPIO_EDGE_SEL);
  181. else
  182. writel(val & ~(1 << gpio_idx),
  183. port->base + GPIO_EDGE_SEL);
  184. }
  185. if (edge != GPIO_INT_BOTH_EDGES) {
  186. reg += GPIO_ICR1 + ((gpio_idx & 0x10) >> 2); /* lower or upper register */
  187. bit = gpio_idx & 0xf;
  188. val = readl(reg) & ~(0x3 << (bit << 1));
  189. writel(val | (edge << (bit << 1)), reg);
  190. }
  191. writel(1 << gpio_idx, port->base + GPIO_ISR);
  192. raw_spin_unlock_irqrestore(&port->gc.bgpio_lock, flags);
  193. return port->gc.direction_input(&port->gc, gpio_idx);
  194. }
  195. static void mxc_flip_edge(struct mxc_gpio_port *port, u32 gpio)
  196. {
  197. void __iomem *reg = port->base;
  198. unsigned long flags;
  199. u32 bit, val;
  200. int edge;
  201. raw_spin_lock_irqsave(&port->gc.bgpio_lock, flags);
  202. reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
  203. bit = gpio & 0xf;
  204. val = readl(reg);
  205. edge = (val >> (bit << 1)) & 3;
  206. val &= ~(0x3 << (bit << 1));
  207. if (edge == GPIO_INT_HIGH_LEV) {
  208. edge = GPIO_INT_LOW_LEV;
  209. pr_debug("mxc: switch GPIO %d to low trigger\n", gpio);
  210. } else if (edge == GPIO_INT_LOW_LEV) {
  211. edge = GPIO_INT_HIGH_LEV;
  212. pr_debug("mxc: switch GPIO %d to high trigger\n", gpio);
  213. } else {
  214. pr_err("mxc: invalid configuration for GPIO %d: %x\n",
  215. gpio, edge);
  216. goto unlock;
  217. }
  218. writel(val | (edge << (bit << 1)), reg);
  219. unlock:
  220. raw_spin_unlock_irqrestore(&port->gc.bgpio_lock, flags);
  221. }
  222. /* handle 32 interrupts in one status register */
  223. static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat)
  224. {
  225. while (irq_stat != 0) {
  226. int irqoffset = fls(irq_stat) - 1;
  227. if (port->both_edges & (1 << irqoffset))
  228. mxc_flip_edge(port, irqoffset);
  229. generic_handle_domain_irq(port->domain, irqoffset);
  230. irq_stat &= ~(1 << irqoffset);
  231. }
  232. }
  233. /* MX1 and MX3 has one interrupt *per* gpio port */
  234. static void mx3_gpio_irq_handler(struct irq_desc *desc)
  235. {
  236. u32 irq_stat;
  237. struct mxc_gpio_port *port = irq_desc_get_handler_data(desc);
  238. struct irq_chip *chip = irq_desc_get_chip(desc);
  239. chained_irq_enter(chip, desc);
  240. irq_stat = readl(port->base + GPIO_ISR) & readl(port->base + GPIO_IMR);
  241. mxc_gpio_irq_handler(port, irq_stat);
  242. chained_irq_exit(chip, desc);
  243. }
  244. /* MX2 has one interrupt *for all* gpio ports */
  245. static void mx2_gpio_irq_handler(struct irq_desc *desc)
  246. {
  247. u32 irq_msk, irq_stat;
  248. struct mxc_gpio_port *port;
  249. struct irq_chip *chip = irq_desc_get_chip(desc);
  250. chained_irq_enter(chip, desc);
  251. /* walk through all interrupt status registers */
  252. list_for_each_entry(port, &mxc_gpio_ports, node) {
  253. irq_msk = readl(port->base + GPIO_IMR);
  254. if (!irq_msk)
  255. continue;
  256. irq_stat = readl(port->base + GPIO_ISR) & irq_msk;
  257. if (irq_stat)
  258. mxc_gpio_irq_handler(port, irq_stat);
  259. }
  260. chained_irq_exit(chip, desc);
  261. }
  262. /*
  263. * Set interrupt number "irq" in the GPIO as a wake-up source.
  264. * While system is running, all registered GPIO interrupts need to have
  265. * wake-up enabled. When system is suspended, only selected GPIO interrupts
  266. * need to have wake-up enabled.
  267. * @param irq interrupt source number
  268. * @param enable enable as wake-up if equal to non-zero
  269. * @return This function returns 0 on success.
  270. */
  271. static int gpio_set_wake_irq(struct irq_data *d, u32 enable)
  272. {
  273. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  274. struct mxc_gpio_port *port = gc->private;
  275. u32 gpio_idx = d->hwirq;
  276. int ret;
  277. if (enable) {
  278. if (port->irq_high && (gpio_idx >= 16))
  279. ret = enable_irq_wake(port->irq_high);
  280. else
  281. ret = enable_irq_wake(port->irq);
  282. } else {
  283. if (port->irq_high && (gpio_idx >= 16))
  284. ret = disable_irq_wake(port->irq_high);
  285. else
  286. ret = disable_irq_wake(port->irq);
  287. }
  288. return ret;
  289. }
  290. static int mxc_gpio_init_gc(struct mxc_gpio_port *port, int irq_base)
  291. {
  292. struct irq_chip_generic *gc;
  293. struct irq_chip_type *ct;
  294. int rv;
  295. gc = devm_irq_alloc_generic_chip(port->dev, "gpio-mxc", 1, irq_base,
  296. port->base, handle_level_irq);
  297. if (!gc)
  298. return -ENOMEM;
  299. gc->private = port;
  300. ct = gc->chip_types;
  301. ct->chip.irq_ack = irq_gc_ack_set_bit;
  302. ct->chip.irq_mask = irq_gc_mask_clr_bit;
  303. ct->chip.irq_unmask = irq_gc_mask_set_bit;
  304. ct->chip.irq_set_type = gpio_set_irq_type;
  305. ct->chip.irq_set_wake = gpio_set_wake_irq;
  306. ct->chip.flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND;
  307. ct->regs.ack = GPIO_ISR;
  308. ct->regs.mask = GPIO_IMR;
  309. rv = devm_irq_setup_generic_chip(port->dev, gc, IRQ_MSK(32),
  310. IRQ_GC_INIT_NESTED_LOCK,
  311. IRQ_NOREQUEST, 0);
  312. return rv;
  313. }
  314. static int mxc_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  315. {
  316. struct mxc_gpio_port *port = gpiochip_get_data(gc);
  317. return irq_find_mapping(port->domain, offset);
  318. }
  319. static int mxc_gpio_probe(struct platform_device *pdev)
  320. {
  321. struct device_node *np = pdev->dev.of_node;
  322. struct mxc_gpio_port *port;
  323. int irq_count;
  324. int irq_base;
  325. int err;
  326. port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL);
  327. if (!port)
  328. return -ENOMEM;
  329. port->dev = &pdev->dev;
  330. port->hwdata = device_get_match_data(&pdev->dev);
  331. port->base = devm_platform_ioremap_resource(pdev, 0);
  332. if (IS_ERR(port->base))
  333. return PTR_ERR(port->base);
  334. irq_count = platform_irq_count(pdev);
  335. if (irq_count < 0)
  336. return irq_count;
  337. if (irq_count > 1) {
  338. port->irq_high = platform_get_irq(pdev, 1);
  339. if (port->irq_high < 0)
  340. port->irq_high = 0;
  341. }
  342. port->irq = platform_get_irq(pdev, 0);
  343. if (port->irq < 0)
  344. return port->irq;
  345. /* the controller clock is optional */
  346. port->clk = devm_clk_get_optional(&pdev->dev, NULL);
  347. if (IS_ERR(port->clk))
  348. return PTR_ERR(port->clk);
  349. err = clk_prepare_enable(port->clk);
  350. if (err) {
  351. dev_err(&pdev->dev, "Unable to enable clock.\n");
  352. return err;
  353. }
  354. if (of_device_is_compatible(np, "fsl,imx7d-gpio"))
  355. port->power_off = true;
  356. /* disable the interrupt and clear the status */
  357. writel(0, port->base + GPIO_IMR);
  358. writel(~0, port->base + GPIO_ISR);
  359. if (of_device_is_compatible(np, "fsl,imx21-gpio")) {
  360. /*
  361. * Setup one handler for all GPIO interrupts. Actually setting
  362. * the handler is needed only once, but doing it for every port
  363. * is more robust and easier.
  364. */
  365. irq_set_chained_handler(port->irq, mx2_gpio_irq_handler);
  366. } else {
  367. /* setup one handler for each entry */
  368. irq_set_chained_handler_and_data(port->irq,
  369. mx3_gpio_irq_handler, port);
  370. if (port->irq_high > 0)
  371. /* setup handler for GPIO 16 to 31 */
  372. irq_set_chained_handler_and_data(port->irq_high,
  373. mx3_gpio_irq_handler,
  374. port);
  375. }
  376. err = bgpio_init(&port->gc, &pdev->dev, 4,
  377. port->base + GPIO_PSR,
  378. port->base + GPIO_DR, NULL,
  379. port->base + GPIO_GDIR, NULL,
  380. BGPIOF_READ_OUTPUT_REG_SET);
  381. if (err)
  382. goto out_bgio;
  383. port->gc.request = gpiochip_generic_request;
  384. port->gc.free = gpiochip_generic_free;
  385. port->gc.to_irq = mxc_gpio_to_irq;
  386. port->gc.base = (pdev->id < 0) ? of_alias_get_id(np, "gpio") * 32 :
  387. pdev->id * 32;
  388. err = devm_gpiochip_add_data(&pdev->dev, &port->gc, port);
  389. if (err)
  390. goto out_bgio;
  391. irq_base = devm_irq_alloc_descs(&pdev->dev, -1, 0, 32, numa_node_id());
  392. if (irq_base < 0) {
  393. err = irq_base;
  394. goto out_bgio;
  395. }
  396. port->domain = irq_domain_add_legacy(np, 32, irq_base, 0,
  397. &irq_domain_simple_ops, NULL);
  398. if (!port->domain) {
  399. err = -ENODEV;
  400. goto out_bgio;
  401. }
  402. /* gpio-mxc can be a generic irq chip */
  403. err = mxc_gpio_init_gc(port, irq_base);
  404. if (err < 0)
  405. goto out_irqdomain_remove;
  406. list_add_tail(&port->node, &mxc_gpio_ports);
  407. platform_set_drvdata(pdev, port);
  408. return 0;
  409. out_irqdomain_remove:
  410. irq_domain_remove(port->domain);
  411. out_bgio:
  412. clk_disable_unprepare(port->clk);
  413. dev_info(&pdev->dev, "%s failed with errno %d\n", __func__, err);
  414. return err;
  415. }
  416. static void mxc_gpio_save_regs(struct mxc_gpio_port *port)
  417. {
  418. if (!port->power_off)
  419. return;
  420. port->gpio_saved_reg.icr1 = readl(port->base + GPIO_ICR1);
  421. port->gpio_saved_reg.icr2 = readl(port->base + GPIO_ICR2);
  422. port->gpio_saved_reg.imr = readl(port->base + GPIO_IMR);
  423. port->gpio_saved_reg.gdir = readl(port->base + GPIO_GDIR);
  424. port->gpio_saved_reg.edge_sel = readl(port->base + GPIO_EDGE_SEL);
  425. port->gpio_saved_reg.dr = readl(port->base + GPIO_DR);
  426. }
  427. static void mxc_gpio_restore_regs(struct mxc_gpio_port *port)
  428. {
  429. if (!port->power_off)
  430. return;
  431. writel(port->gpio_saved_reg.icr1, port->base + GPIO_ICR1);
  432. writel(port->gpio_saved_reg.icr2, port->base + GPIO_ICR2);
  433. writel(port->gpio_saved_reg.imr, port->base + GPIO_IMR);
  434. writel(port->gpio_saved_reg.gdir, port->base + GPIO_GDIR);
  435. writel(port->gpio_saved_reg.edge_sel, port->base + GPIO_EDGE_SEL);
  436. writel(port->gpio_saved_reg.dr, port->base + GPIO_DR);
  437. }
  438. static int mxc_gpio_syscore_suspend(void)
  439. {
  440. struct mxc_gpio_port *port;
  441. /* walk through all ports */
  442. list_for_each_entry(port, &mxc_gpio_ports, node) {
  443. mxc_gpio_save_regs(port);
  444. clk_disable_unprepare(port->clk);
  445. }
  446. return 0;
  447. }
  448. static void mxc_gpio_syscore_resume(void)
  449. {
  450. struct mxc_gpio_port *port;
  451. int ret;
  452. /* walk through all ports */
  453. list_for_each_entry(port, &mxc_gpio_ports, node) {
  454. ret = clk_prepare_enable(port->clk);
  455. if (ret) {
  456. pr_err("mxc: failed to enable gpio clock %d\n", ret);
  457. return;
  458. }
  459. mxc_gpio_restore_regs(port);
  460. }
  461. }
  462. static struct syscore_ops mxc_gpio_syscore_ops = {
  463. .suspend = mxc_gpio_syscore_suspend,
  464. .resume = mxc_gpio_syscore_resume,
  465. };
  466. static struct platform_driver mxc_gpio_driver = {
  467. .driver = {
  468. .name = "gpio-mxc",
  469. .of_match_table = mxc_gpio_dt_ids,
  470. .suppress_bind_attrs = true,
  471. },
  472. .probe = mxc_gpio_probe,
  473. };
  474. static int __init gpio_mxc_init(void)
  475. {
  476. register_syscore_ops(&mxc_gpio_syscore_ops);
  477. return platform_driver_register(&mxc_gpio_driver);
  478. }
  479. subsys_initcall(gpio_mxc_init);
  480. MODULE_AUTHOR("Shawn Guo <[email protected]>");
  481. MODULE_DESCRIPTION("i.MX GPIO Driver");
  482. MODULE_LICENSE("GPL");