gpio-mxc.c 13 KB

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