gpio-mxc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // MXC GPIO support. (c) 2008 Daniel Mack <daniel@caiaq.de>
  4. // Copyright 2008 Juergen Beisert, kernel@pengutronix.de
  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/pm_runtime.h>
  20. #include <linux/slab.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/syscore_ops.h>
  23. #include <linux/gpio/driver.h>
  24. #include <linux/of.h>
  25. #include <linux/bug.h>
  26. #define IMX_SCU_WAKEUP_OFF 0
  27. #define IMX_SCU_WAKEUP_LOW_LVL 4
  28. #define IMX_SCU_WAKEUP_FALL_EDGE 5
  29. #define IMX_SCU_WAKEUP_RISE_EDGE 6
  30. #define IMX_SCU_WAKEUP_HIGH_LVL 7
  31. /* device type dependent stuff */
  32. struct mxc_gpio_hwdata {
  33. unsigned dr_reg;
  34. unsigned gdir_reg;
  35. unsigned psr_reg;
  36. unsigned icr1_reg;
  37. unsigned icr2_reg;
  38. unsigned imr_reg;
  39. unsigned isr_reg;
  40. int edge_sel_reg;
  41. unsigned low_level;
  42. unsigned high_level;
  43. unsigned rise_edge;
  44. unsigned fall_edge;
  45. };
  46. struct mxc_gpio_reg_saved {
  47. u32 icr1;
  48. u32 icr2;
  49. u32 imr;
  50. u32 gdir;
  51. u32 edge_sel;
  52. u32 dr;
  53. };
  54. struct mxc_gpio_port {
  55. struct list_head node;
  56. void __iomem *base;
  57. struct clk *clk;
  58. int irq;
  59. int irq_high;
  60. void (*mx_irq_handler)(struct irq_desc *desc);
  61. struct irq_domain *domain;
  62. struct gpio_chip gc;
  63. struct device *dev;
  64. u32 both_edges;
  65. struct mxc_gpio_reg_saved gpio_saved_reg;
  66. bool power_off;
  67. u32 wakeup_pads;
  68. bool is_pad_wakeup;
  69. u32 pad_type[32];
  70. const struct mxc_gpio_hwdata *hwdata;
  71. };
  72. static struct mxc_gpio_hwdata imx1_imx21_gpio_hwdata = {
  73. .dr_reg = 0x1c,
  74. .gdir_reg = 0x00,
  75. .psr_reg = 0x24,
  76. .icr1_reg = 0x28,
  77. .icr2_reg = 0x2c,
  78. .imr_reg = 0x30,
  79. .isr_reg = 0x34,
  80. .edge_sel_reg = -EINVAL,
  81. .low_level = 0x03,
  82. .high_level = 0x02,
  83. .rise_edge = 0x00,
  84. .fall_edge = 0x01,
  85. };
  86. static struct mxc_gpio_hwdata imx31_gpio_hwdata = {
  87. .dr_reg = 0x00,
  88. .gdir_reg = 0x04,
  89. .psr_reg = 0x08,
  90. .icr1_reg = 0x0c,
  91. .icr2_reg = 0x10,
  92. .imr_reg = 0x14,
  93. .isr_reg = 0x18,
  94. .edge_sel_reg = -EINVAL,
  95. .low_level = 0x00,
  96. .high_level = 0x01,
  97. .rise_edge = 0x02,
  98. .fall_edge = 0x03,
  99. };
  100. static struct mxc_gpio_hwdata imx35_gpio_hwdata = {
  101. .dr_reg = 0x00,
  102. .gdir_reg = 0x04,
  103. .psr_reg = 0x08,
  104. .icr1_reg = 0x0c,
  105. .icr2_reg = 0x10,
  106. .imr_reg = 0x14,
  107. .isr_reg = 0x18,
  108. .edge_sel_reg = 0x1c,
  109. .low_level = 0x00,
  110. .high_level = 0x01,
  111. .rise_edge = 0x02,
  112. .fall_edge = 0x03,
  113. };
  114. #define GPIO_DR (port->hwdata->dr_reg)
  115. #define GPIO_GDIR (port->hwdata->gdir_reg)
  116. #define GPIO_PSR (port->hwdata->psr_reg)
  117. #define GPIO_ICR1 (port->hwdata->icr1_reg)
  118. #define GPIO_ICR2 (port->hwdata->icr2_reg)
  119. #define GPIO_IMR (port->hwdata->imr_reg)
  120. #define GPIO_ISR (port->hwdata->isr_reg)
  121. #define GPIO_EDGE_SEL (port->hwdata->edge_sel_reg)
  122. #define GPIO_INT_LOW_LEV (port->hwdata->low_level)
  123. #define GPIO_INT_HIGH_LEV (port->hwdata->high_level)
  124. #define GPIO_INT_RISE_EDGE (port->hwdata->rise_edge)
  125. #define GPIO_INT_FALL_EDGE (port->hwdata->fall_edge)
  126. #define GPIO_INT_BOTH_EDGES 0x4
  127. static const struct of_device_id mxc_gpio_dt_ids[] = {
  128. { .compatible = "fsl,imx1-gpio", .data = &imx1_imx21_gpio_hwdata },
  129. { .compatible = "fsl,imx21-gpio", .data = &imx1_imx21_gpio_hwdata },
  130. { .compatible = "fsl,imx31-gpio", .data = &imx31_gpio_hwdata },
  131. { .compatible = "fsl,imx35-gpio", .data = &imx35_gpio_hwdata },
  132. { .compatible = "fsl,imx7d-gpio", .data = &imx35_gpio_hwdata },
  133. { .compatible = "fsl,imx8dxl-gpio", .data = &imx35_gpio_hwdata },
  134. { .compatible = "fsl,imx8qm-gpio", .data = &imx35_gpio_hwdata },
  135. { .compatible = "fsl,imx8qxp-gpio", .data = &imx35_gpio_hwdata },
  136. { /* sentinel */ }
  137. };
  138. MODULE_DEVICE_TABLE(of, mxc_gpio_dt_ids);
  139. /*
  140. * MX2 has one interrupt *for all* gpio ports. The list is used
  141. * to save the references to all ports, so that mx2_gpio_irq_handler
  142. * can walk through all interrupt status registers.
  143. */
  144. static LIST_HEAD(mxc_gpio_ports);
  145. /* Note: This driver assumes 32 GPIOs are handled in one register */
  146. static int gpio_set_irq_type(struct irq_data *d, u32 type)
  147. {
  148. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  149. struct mxc_gpio_port *port = gc->private;
  150. unsigned long flags;
  151. u32 bit, val;
  152. u32 gpio_idx = d->hwirq;
  153. int edge;
  154. void __iomem *reg = port->base;
  155. port->both_edges &= ~(1 << gpio_idx);
  156. switch (type) {
  157. case IRQ_TYPE_EDGE_RISING:
  158. edge = GPIO_INT_RISE_EDGE;
  159. break;
  160. case IRQ_TYPE_EDGE_FALLING:
  161. edge = GPIO_INT_FALL_EDGE;
  162. break;
  163. case IRQ_TYPE_EDGE_BOTH:
  164. if (GPIO_EDGE_SEL >= 0) {
  165. edge = GPIO_INT_BOTH_EDGES;
  166. } else {
  167. val = port->gc.get(&port->gc, gpio_idx);
  168. if (val) {
  169. edge = GPIO_INT_LOW_LEV;
  170. pr_debug("mxc: set GPIO %d to low trigger\n", gpio_idx);
  171. } else {
  172. edge = GPIO_INT_HIGH_LEV;
  173. pr_debug("mxc: set GPIO %d to high trigger\n", gpio_idx);
  174. }
  175. port->both_edges |= 1 << gpio_idx;
  176. }
  177. break;
  178. case IRQ_TYPE_LEVEL_LOW:
  179. edge = GPIO_INT_LOW_LEV;
  180. break;
  181. case IRQ_TYPE_LEVEL_HIGH:
  182. edge = GPIO_INT_HIGH_LEV;
  183. break;
  184. default:
  185. return -EINVAL;
  186. }
  187. raw_spin_lock_irqsave(&port->gc.bgpio_lock, flags);
  188. if (GPIO_EDGE_SEL >= 0) {
  189. val = readl(port->base + GPIO_EDGE_SEL);
  190. if (edge == GPIO_INT_BOTH_EDGES)
  191. writel(val | (1 << gpio_idx),
  192. port->base + GPIO_EDGE_SEL);
  193. else
  194. writel(val & ~(1 << gpio_idx),
  195. port->base + GPIO_EDGE_SEL);
  196. }
  197. if (edge != GPIO_INT_BOTH_EDGES) {
  198. reg += GPIO_ICR1 + ((gpio_idx & 0x10) >> 2); /* lower or upper register */
  199. bit = gpio_idx & 0xf;
  200. val = readl(reg) & ~(0x3 << (bit << 1));
  201. writel(val | (edge << (bit << 1)), reg);
  202. }
  203. writel(1 << gpio_idx, port->base + GPIO_ISR);
  204. port->pad_type[gpio_idx] = type;
  205. raw_spin_unlock_irqrestore(&port->gc.bgpio_lock, flags);
  206. return port->gc.direction_input(&port->gc, gpio_idx);
  207. }
  208. static void mxc_flip_edge(struct mxc_gpio_port *port, u32 gpio)
  209. {
  210. void __iomem *reg = port->base;
  211. unsigned long flags;
  212. u32 bit, val;
  213. int edge;
  214. raw_spin_lock_irqsave(&port->gc.bgpio_lock, flags);
  215. reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
  216. bit = gpio & 0xf;
  217. val = readl(reg);
  218. edge = (val >> (bit << 1)) & 3;
  219. val &= ~(0x3 << (bit << 1));
  220. if (edge == GPIO_INT_HIGH_LEV) {
  221. edge = GPIO_INT_LOW_LEV;
  222. pr_debug("mxc: switch GPIO %d to low trigger\n", gpio);
  223. } else if (edge == GPIO_INT_LOW_LEV) {
  224. edge = GPIO_INT_HIGH_LEV;
  225. pr_debug("mxc: switch GPIO %d to high trigger\n", gpio);
  226. } else {
  227. pr_err("mxc: invalid configuration for GPIO %d: %x\n",
  228. gpio, edge);
  229. goto unlock;
  230. }
  231. writel(val | (edge << (bit << 1)), reg);
  232. unlock:
  233. raw_spin_unlock_irqrestore(&port->gc.bgpio_lock, flags);
  234. }
  235. /* handle 32 interrupts in one status register */
  236. static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat)
  237. {
  238. while (irq_stat != 0) {
  239. int irqoffset = fls(irq_stat) - 1;
  240. if (port->both_edges & (1 << irqoffset))
  241. mxc_flip_edge(port, irqoffset);
  242. generic_handle_domain_irq(port->domain, irqoffset);
  243. irq_stat &= ~(1 << irqoffset);
  244. }
  245. }
  246. /* MX1 and MX3 has one interrupt *per* gpio port */
  247. static void mx3_gpio_irq_handler(struct irq_desc *desc)
  248. {
  249. u32 irq_stat;
  250. struct mxc_gpio_port *port = irq_desc_get_handler_data(desc);
  251. struct irq_chip *chip = irq_desc_get_chip(desc);
  252. if (port->is_pad_wakeup)
  253. return;
  254. chained_irq_enter(chip, desc);
  255. irq_stat = readl(port->base + GPIO_ISR) & readl(port->base + GPIO_IMR);
  256. mxc_gpio_irq_handler(port, irq_stat);
  257. chained_irq_exit(chip, desc);
  258. }
  259. /* MX2 has one interrupt *for all* gpio ports */
  260. static void mx2_gpio_irq_handler(struct irq_desc *desc)
  261. {
  262. u32 irq_msk, irq_stat;
  263. struct mxc_gpio_port *port;
  264. struct irq_chip *chip = irq_desc_get_chip(desc);
  265. chained_irq_enter(chip, desc);
  266. /* walk through all interrupt status registers */
  267. list_for_each_entry(port, &mxc_gpio_ports, node) {
  268. irq_msk = readl(port->base + GPIO_IMR);
  269. if (!irq_msk)
  270. continue;
  271. irq_stat = readl(port->base + GPIO_ISR) & irq_msk;
  272. if (irq_stat)
  273. mxc_gpio_irq_handler(port, irq_stat);
  274. }
  275. chained_irq_exit(chip, desc);
  276. }
  277. /*
  278. * Set interrupt number "irq" in the GPIO as a wake-up source.
  279. * While system is running, all registered GPIO interrupts need to have
  280. * wake-up enabled. When system is suspended, only selected GPIO interrupts
  281. * need to have wake-up enabled.
  282. * @param irq interrupt source number
  283. * @param enable enable as wake-up if equal to non-zero
  284. * @return This function returns 0 on success.
  285. */
  286. static int gpio_set_wake_irq(struct irq_data *d, u32 enable)
  287. {
  288. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  289. struct mxc_gpio_port *port = gc->private;
  290. u32 gpio_idx = d->hwirq;
  291. int ret;
  292. if (enable) {
  293. if (port->irq_high && (gpio_idx >= 16))
  294. ret = enable_irq_wake(port->irq_high);
  295. else
  296. ret = enable_irq_wake(port->irq);
  297. port->wakeup_pads |= (1 << gpio_idx);
  298. } else {
  299. if (port->irq_high && (gpio_idx >= 16))
  300. ret = disable_irq_wake(port->irq_high);
  301. else
  302. ret = disable_irq_wake(port->irq);
  303. port->wakeup_pads &= ~(1 << gpio_idx);
  304. }
  305. return ret;
  306. }
  307. static int mxc_gpio_init_gc(struct mxc_gpio_port *port, int irq_base)
  308. {
  309. struct irq_chip_generic *gc;
  310. struct irq_chip_type *ct;
  311. int rv;
  312. gc = devm_irq_alloc_generic_chip(port->dev, "gpio-mxc", 1, irq_base,
  313. port->base, handle_level_irq);
  314. if (!gc)
  315. return -ENOMEM;
  316. gc->private = port;
  317. ct = gc->chip_types;
  318. ct->chip.irq_ack = irq_gc_ack_set_bit;
  319. ct->chip.irq_mask = irq_gc_mask_clr_bit;
  320. ct->chip.irq_unmask = irq_gc_mask_set_bit;
  321. ct->chip.irq_set_type = gpio_set_irq_type;
  322. ct->chip.irq_set_wake = gpio_set_wake_irq;
  323. ct->chip.flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND;
  324. ct->regs.ack = GPIO_ISR;
  325. ct->regs.mask = GPIO_IMR;
  326. rv = devm_irq_setup_generic_chip(port->dev, gc, IRQ_MSK(32),
  327. IRQ_GC_INIT_NESTED_LOCK,
  328. IRQ_NOREQUEST, 0);
  329. return rv;
  330. }
  331. static int mxc_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  332. {
  333. struct mxc_gpio_port *port = gpiochip_get_data(gc);
  334. return irq_find_mapping(port->domain, offset);
  335. }
  336. static int mxc_gpio_request(struct gpio_chip *chip, unsigned int offset)
  337. {
  338. int ret;
  339. ret = gpiochip_generic_request(chip, offset);
  340. if (ret)
  341. return ret;
  342. return pm_runtime_resume_and_get(chip->parent);
  343. }
  344. static void mxc_gpio_free(struct gpio_chip *chip, unsigned int offset)
  345. {
  346. gpiochip_generic_free(chip, offset);
  347. pm_runtime_put(chip->parent);
  348. }
  349. static void mxc_update_irq_chained_handler(struct mxc_gpio_port *port, bool enable)
  350. {
  351. if (enable)
  352. irq_set_chained_handler_and_data(port->irq, port->mx_irq_handler, port);
  353. else
  354. irq_set_chained_handler_and_data(port->irq, NULL, NULL);
  355. /* setup handler for GPIO 16 to 31 */
  356. if (port->irq_high > 0) {
  357. if (enable)
  358. irq_set_chained_handler_and_data(port->irq_high,
  359. port->mx_irq_handler,
  360. port);
  361. else
  362. irq_set_chained_handler_and_data(port->irq_high, NULL, NULL);
  363. }
  364. }
  365. static int mxc_gpio_probe(struct platform_device *pdev)
  366. {
  367. struct device_node *np = pdev->dev.of_node;
  368. struct mxc_gpio_port *port;
  369. int irq_count;
  370. int irq_base;
  371. int err;
  372. port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL);
  373. if (!port)
  374. return -ENOMEM;
  375. port->dev = &pdev->dev;
  376. port->hwdata = device_get_match_data(&pdev->dev);
  377. port->base = devm_platform_ioremap_resource(pdev, 0);
  378. if (IS_ERR(port->base))
  379. return PTR_ERR(port->base);
  380. irq_count = platform_irq_count(pdev);
  381. if (irq_count < 0)
  382. return irq_count;
  383. if (irq_count > 1) {
  384. port->irq_high = platform_get_irq(pdev, 1);
  385. if (port->irq_high < 0)
  386. port->irq_high = 0;
  387. }
  388. port->irq = platform_get_irq(pdev, 0);
  389. if (port->irq < 0)
  390. return port->irq;
  391. /* the controller clock is optional */
  392. port->clk = devm_clk_get_optional_enabled(&pdev->dev, NULL);
  393. if (IS_ERR(port->clk))
  394. return PTR_ERR(port->clk);
  395. if (of_device_is_compatible(np, "fsl,imx7d-gpio"))
  396. port->power_off = true;
  397. pm_runtime_get_noresume(&pdev->dev);
  398. pm_runtime_set_active(&pdev->dev);
  399. pm_runtime_enable(&pdev->dev);
  400. /* disable the interrupt and clear the status */
  401. writel(0, port->base + GPIO_IMR);
  402. writel(~0, port->base + GPIO_ISR);
  403. if (of_device_is_compatible(np, "fsl,imx21-gpio")) {
  404. /*
  405. * Setup one handler for all GPIO interrupts. Actually setting
  406. * the handler is needed only once, but doing it for every port
  407. * is more robust and easier.
  408. */
  409. port->irq_high = -1;
  410. port->mx_irq_handler = mx2_gpio_irq_handler;
  411. } else
  412. port->mx_irq_handler = mx3_gpio_irq_handler;
  413. mxc_update_irq_chained_handler(port, true);
  414. err = bgpio_init(&port->gc, &pdev->dev, 4,
  415. port->base + GPIO_PSR,
  416. port->base + GPIO_DR, NULL,
  417. port->base + GPIO_GDIR, NULL,
  418. BGPIOF_READ_OUTPUT_REG_SET);
  419. if (err)
  420. goto out_bgio;
  421. port->gc.request = mxc_gpio_request;
  422. port->gc.free = mxc_gpio_free;
  423. port->gc.to_irq = mxc_gpio_to_irq;
  424. port->gc.base = of_alias_get_id(np, "gpio") * 32;
  425. err = devm_gpiochip_add_data(&pdev->dev, &port->gc, port);
  426. if (err)
  427. goto out_bgio;
  428. irq_base = devm_irq_alloc_descs(&pdev->dev, -1, 0, 32, numa_node_id());
  429. if (irq_base < 0) {
  430. err = irq_base;
  431. goto out_bgio;
  432. }
  433. port->domain = irq_domain_add_legacy(np, 32, irq_base, 0,
  434. &irq_domain_simple_ops, NULL);
  435. if (!port->domain) {
  436. err = -ENODEV;
  437. goto out_bgio;
  438. }
  439. irq_domain_set_pm_device(port->domain, &pdev->dev);
  440. /* gpio-mxc can be a generic irq chip */
  441. err = mxc_gpio_init_gc(port, irq_base);
  442. if (err < 0)
  443. goto out_irqdomain_remove;
  444. list_add_tail(&port->node, &mxc_gpio_ports);
  445. platform_set_drvdata(pdev, port);
  446. pm_runtime_put_autosuspend(&pdev->dev);
  447. return 0;
  448. out_irqdomain_remove:
  449. irq_domain_remove(port->domain);
  450. out_bgio:
  451. pm_runtime_disable(&pdev->dev);
  452. pm_runtime_put_noidle(&pdev->dev);
  453. dev_info(&pdev->dev, "%s failed with errno %d\n", __func__, err);
  454. return err;
  455. }
  456. static void mxc_gpio_save_regs(struct mxc_gpio_port *port)
  457. {
  458. if (!port->power_off)
  459. return;
  460. port->gpio_saved_reg.icr1 = readl(port->base + GPIO_ICR1);
  461. port->gpio_saved_reg.icr2 = readl(port->base + GPIO_ICR2);
  462. port->gpio_saved_reg.imr = readl(port->base + GPIO_IMR);
  463. port->gpio_saved_reg.gdir = readl(port->base + GPIO_GDIR);
  464. port->gpio_saved_reg.edge_sel = readl(port->base + GPIO_EDGE_SEL);
  465. port->gpio_saved_reg.dr = readl(port->base + GPIO_DR);
  466. }
  467. static void mxc_gpio_restore_regs(struct mxc_gpio_port *port)
  468. {
  469. if (!port->power_off)
  470. return;
  471. writel(port->gpio_saved_reg.icr1, port->base + GPIO_ICR1);
  472. writel(port->gpio_saved_reg.icr2, port->base + GPIO_ICR2);
  473. writel(port->gpio_saved_reg.imr, port->base + GPIO_IMR);
  474. writel(port->gpio_saved_reg.gdir, port->base + GPIO_GDIR);
  475. writel(port->gpio_saved_reg.edge_sel, port->base + GPIO_EDGE_SEL);
  476. writel(port->gpio_saved_reg.dr, port->base + GPIO_DR);
  477. }
  478. static bool mxc_gpio_generic_config(struct mxc_gpio_port *port,
  479. unsigned int offset, unsigned long conf)
  480. {
  481. struct device_node *np = port->dev->of_node;
  482. if (of_device_is_compatible(np, "fsl,imx8dxl-gpio") ||
  483. of_device_is_compatible(np, "fsl,imx8qxp-gpio") ||
  484. of_device_is_compatible(np, "fsl,imx8qm-gpio"))
  485. return (gpiochip_generic_config(&port->gc, offset, conf) == 0);
  486. return false;
  487. }
  488. static bool mxc_gpio_set_pad_wakeup(struct mxc_gpio_port *port, bool enable)
  489. {
  490. unsigned long config;
  491. bool ret = false;
  492. int i, type;
  493. static const u32 pad_type_map[] = {
  494. IMX_SCU_WAKEUP_OFF, /* 0 */
  495. IMX_SCU_WAKEUP_RISE_EDGE, /* IRQ_TYPE_EDGE_RISING */
  496. IMX_SCU_WAKEUP_FALL_EDGE, /* IRQ_TYPE_EDGE_FALLING */
  497. IMX_SCU_WAKEUP_FALL_EDGE, /* IRQ_TYPE_EDGE_BOTH */
  498. IMX_SCU_WAKEUP_HIGH_LVL, /* IRQ_TYPE_LEVEL_HIGH */
  499. IMX_SCU_WAKEUP_OFF, /* 5 */
  500. IMX_SCU_WAKEUP_OFF, /* 6 */
  501. IMX_SCU_WAKEUP_OFF, /* 7 */
  502. IMX_SCU_WAKEUP_LOW_LVL, /* IRQ_TYPE_LEVEL_LOW */
  503. };
  504. for (i = 0; i < 32; i++) {
  505. if ((port->wakeup_pads & (1 << i))) {
  506. type = port->pad_type[i];
  507. if (enable)
  508. config = pad_type_map[type];
  509. else
  510. config = IMX_SCU_WAKEUP_OFF;
  511. ret |= mxc_gpio_generic_config(port, i, config);
  512. }
  513. }
  514. return ret;
  515. }
  516. static int mxc_gpio_runtime_suspend(struct device *dev)
  517. {
  518. struct mxc_gpio_port *port = dev_get_drvdata(dev);
  519. mxc_gpio_save_regs(port);
  520. clk_disable_unprepare(port->clk);
  521. mxc_update_irq_chained_handler(port, false);
  522. return 0;
  523. }
  524. static int mxc_gpio_runtime_resume(struct device *dev)
  525. {
  526. struct mxc_gpio_port *port = dev_get_drvdata(dev);
  527. int ret;
  528. mxc_update_irq_chained_handler(port, true);
  529. ret = clk_prepare_enable(port->clk);
  530. if (ret) {
  531. mxc_update_irq_chained_handler(port, false);
  532. return ret;
  533. }
  534. mxc_gpio_restore_regs(port);
  535. return 0;
  536. }
  537. static int mxc_gpio_noirq_suspend(struct device *dev)
  538. {
  539. struct platform_device *pdev = to_platform_device(dev);
  540. struct mxc_gpio_port *port = platform_get_drvdata(pdev);
  541. if (port->wakeup_pads > 0)
  542. port->is_pad_wakeup = mxc_gpio_set_pad_wakeup(port, true);
  543. return 0;
  544. }
  545. static int mxc_gpio_noirq_resume(struct device *dev)
  546. {
  547. struct platform_device *pdev = to_platform_device(dev);
  548. struct mxc_gpio_port *port = platform_get_drvdata(pdev);
  549. if (port->wakeup_pads > 0)
  550. mxc_gpio_set_pad_wakeup(port, false);
  551. port->is_pad_wakeup = false;
  552. return 0;
  553. }
  554. static const struct dev_pm_ops mxc_gpio_dev_pm_ops = {
  555. NOIRQ_SYSTEM_SLEEP_PM_OPS(mxc_gpio_noirq_suspend, mxc_gpio_noirq_resume)
  556. RUNTIME_PM_OPS(mxc_gpio_runtime_suspend, mxc_gpio_runtime_resume, NULL)
  557. };
  558. static int mxc_gpio_syscore_suspend(void)
  559. {
  560. struct mxc_gpio_port *port;
  561. int ret;
  562. /* walk through all ports */
  563. list_for_each_entry(port, &mxc_gpio_ports, node) {
  564. ret = clk_prepare_enable(port->clk);
  565. if (ret)
  566. return ret;
  567. mxc_gpio_save_regs(port);
  568. clk_disable_unprepare(port->clk);
  569. }
  570. return 0;
  571. }
  572. static void mxc_gpio_syscore_resume(void)
  573. {
  574. struct mxc_gpio_port *port;
  575. int ret;
  576. /* walk through all ports */
  577. list_for_each_entry(port, &mxc_gpio_ports, node) {
  578. ret = clk_prepare_enable(port->clk);
  579. if (ret) {
  580. pr_err("mxc: failed to enable gpio clock %d\n", ret);
  581. return;
  582. }
  583. mxc_gpio_restore_regs(port);
  584. clk_disable_unprepare(port->clk);
  585. }
  586. }
  587. static struct syscore_ops mxc_gpio_syscore_ops = {
  588. .suspend = mxc_gpio_syscore_suspend,
  589. .resume = mxc_gpio_syscore_resume,
  590. };
  591. static struct platform_driver mxc_gpio_driver = {
  592. .driver = {
  593. .name = "gpio-mxc",
  594. .of_match_table = mxc_gpio_dt_ids,
  595. .suppress_bind_attrs = true,
  596. .pm = pm_ptr(&mxc_gpio_dev_pm_ops),
  597. },
  598. .probe = mxc_gpio_probe,
  599. };
  600. static int __init gpio_mxc_init(void)
  601. {
  602. register_syscore_ops(&mxc_gpio_syscore_ops);
  603. return platform_driver_register(&mxc_gpio_driver);
  604. }
  605. subsys_initcall(gpio_mxc_init);
  606. MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
  607. MODULE_DESCRIPTION("i.MX GPIO Driver");
  608. MODULE_LICENSE("GPL");