gpio-mxc.c 13 KB

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