gpio-mxc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. u32 both_edges;
  66. };
  67. static struct mxc_gpio_hwdata imx1_imx21_gpio_hwdata = {
  68. .dr_reg = 0x1c,
  69. .gdir_reg = 0x00,
  70. .psr_reg = 0x24,
  71. .icr1_reg = 0x28,
  72. .icr2_reg = 0x2c,
  73. .imr_reg = 0x30,
  74. .isr_reg = 0x34,
  75. .edge_sel_reg = -EINVAL,
  76. .low_level = 0x03,
  77. .high_level = 0x02,
  78. .rise_edge = 0x00,
  79. .fall_edge = 0x01,
  80. };
  81. static struct mxc_gpio_hwdata imx31_gpio_hwdata = {
  82. .dr_reg = 0x00,
  83. .gdir_reg = 0x04,
  84. .psr_reg = 0x08,
  85. .icr1_reg = 0x0c,
  86. .icr2_reg = 0x10,
  87. .imr_reg = 0x14,
  88. .isr_reg = 0x18,
  89. .edge_sel_reg = -EINVAL,
  90. .low_level = 0x00,
  91. .high_level = 0x01,
  92. .rise_edge = 0x02,
  93. .fall_edge = 0x03,
  94. };
  95. static struct mxc_gpio_hwdata imx35_gpio_hwdata = {
  96. .dr_reg = 0x00,
  97. .gdir_reg = 0x04,
  98. .psr_reg = 0x08,
  99. .icr1_reg = 0x0c,
  100. .icr2_reg = 0x10,
  101. .imr_reg = 0x14,
  102. .isr_reg = 0x18,
  103. .edge_sel_reg = 0x1c,
  104. .low_level = 0x00,
  105. .high_level = 0x01,
  106. .rise_edge = 0x02,
  107. .fall_edge = 0x03,
  108. };
  109. static enum mxc_gpio_hwtype mxc_gpio_hwtype;
  110. static struct mxc_gpio_hwdata *mxc_gpio_hwdata;
  111. #define GPIO_DR (mxc_gpio_hwdata->dr_reg)
  112. #define GPIO_GDIR (mxc_gpio_hwdata->gdir_reg)
  113. #define GPIO_PSR (mxc_gpio_hwdata->psr_reg)
  114. #define GPIO_ICR1 (mxc_gpio_hwdata->icr1_reg)
  115. #define GPIO_ICR2 (mxc_gpio_hwdata->icr2_reg)
  116. #define GPIO_IMR (mxc_gpio_hwdata->imr_reg)
  117. #define GPIO_ISR (mxc_gpio_hwdata->isr_reg)
  118. #define GPIO_EDGE_SEL (mxc_gpio_hwdata->edge_sel_reg)
  119. #define GPIO_INT_LOW_LEV (mxc_gpio_hwdata->low_level)
  120. #define GPIO_INT_HIGH_LEV (mxc_gpio_hwdata->high_level)
  121. #define GPIO_INT_RISE_EDGE (mxc_gpio_hwdata->rise_edge)
  122. #define GPIO_INT_FALL_EDGE (mxc_gpio_hwdata->fall_edge)
  123. #define GPIO_INT_BOTH_EDGES 0x4
  124. static const struct platform_device_id mxc_gpio_devtype[] = {
  125. {
  126. .name = "imx1-gpio",
  127. .driver_data = IMX1_GPIO,
  128. }, {
  129. .name = "imx21-gpio",
  130. .driver_data = IMX21_GPIO,
  131. }, {
  132. .name = "imx31-gpio",
  133. .driver_data = IMX31_GPIO,
  134. }, {
  135. .name = "imx35-gpio",
  136. .driver_data = IMX35_GPIO,
  137. }, {
  138. /* sentinel */
  139. }
  140. };
  141. static const struct of_device_id mxc_gpio_dt_ids[] = {
  142. { .compatible = "fsl,imx1-gpio", .data = &mxc_gpio_devtype[IMX1_GPIO], },
  143. { .compatible = "fsl,imx21-gpio", .data = &mxc_gpio_devtype[IMX21_GPIO], },
  144. { .compatible = "fsl,imx31-gpio", .data = &mxc_gpio_devtype[IMX31_GPIO], },
  145. { .compatible = "fsl,imx35-gpio", .data = &mxc_gpio_devtype[IMX35_GPIO], },
  146. { /* sentinel */ }
  147. };
  148. /*
  149. * MX2 has one interrupt *for all* gpio ports. The list is used
  150. * to save the references to all ports, so that mx2_gpio_irq_handler
  151. * can walk through all interrupt status registers.
  152. */
  153. static LIST_HEAD(mxc_gpio_ports);
  154. /* Note: This driver assumes 32 GPIOs are handled in one register */
  155. static int gpio_set_irq_type(struct irq_data *d, u32 type)
  156. {
  157. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  158. struct mxc_gpio_port *port = gc->private;
  159. u32 bit, val;
  160. u32 gpio_idx = d->hwirq;
  161. u32 gpio = port->gc.base + gpio_idx;
  162. int edge;
  163. void __iomem *reg = port->base;
  164. port->both_edges &= ~(1 << gpio_idx);
  165. switch (type) {
  166. case IRQ_TYPE_EDGE_RISING:
  167. edge = GPIO_INT_RISE_EDGE;
  168. break;
  169. case IRQ_TYPE_EDGE_FALLING:
  170. edge = GPIO_INT_FALL_EDGE;
  171. break;
  172. case IRQ_TYPE_EDGE_BOTH:
  173. if (GPIO_EDGE_SEL >= 0) {
  174. edge = GPIO_INT_BOTH_EDGES;
  175. } else {
  176. val = gpio_get_value(gpio);
  177. if (val) {
  178. edge = GPIO_INT_LOW_LEV;
  179. pr_debug("mxc: set GPIO %d to low trigger\n", gpio);
  180. } else {
  181. edge = GPIO_INT_HIGH_LEV;
  182. pr_debug("mxc: set GPIO %d to high trigger\n", gpio);
  183. }
  184. port->both_edges |= 1 << gpio_idx;
  185. }
  186. break;
  187. case IRQ_TYPE_LEVEL_LOW:
  188. edge = GPIO_INT_LOW_LEV;
  189. break;
  190. case IRQ_TYPE_LEVEL_HIGH:
  191. edge = GPIO_INT_HIGH_LEV;
  192. break;
  193. default:
  194. return -EINVAL;
  195. }
  196. if (GPIO_EDGE_SEL >= 0) {
  197. val = readl(port->base + GPIO_EDGE_SEL);
  198. if (edge == GPIO_INT_BOTH_EDGES)
  199. writel(val | (1 << gpio_idx),
  200. port->base + GPIO_EDGE_SEL);
  201. else
  202. writel(val & ~(1 << gpio_idx),
  203. port->base + GPIO_EDGE_SEL);
  204. }
  205. if (edge != GPIO_INT_BOTH_EDGES) {
  206. reg += GPIO_ICR1 + ((gpio_idx & 0x10) >> 2); /* lower or upper register */
  207. bit = gpio_idx & 0xf;
  208. val = readl(reg) & ~(0x3 << (bit << 1));
  209. writel(val | (edge << (bit << 1)), reg);
  210. }
  211. writel(1 << gpio_idx, port->base + GPIO_ISR);
  212. return 0;
  213. }
  214. static void mxc_flip_edge(struct mxc_gpio_port *port, u32 gpio)
  215. {
  216. void __iomem *reg = port->base;
  217. u32 bit, val;
  218. int edge;
  219. reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
  220. bit = gpio & 0xf;
  221. val = readl(reg);
  222. edge = (val >> (bit << 1)) & 3;
  223. val &= ~(0x3 << (bit << 1));
  224. if (edge == GPIO_INT_HIGH_LEV) {
  225. edge = GPIO_INT_LOW_LEV;
  226. pr_debug("mxc: switch GPIO %d to low trigger\n", gpio);
  227. } else if (edge == GPIO_INT_LOW_LEV) {
  228. edge = GPIO_INT_HIGH_LEV;
  229. pr_debug("mxc: switch GPIO %d to high trigger\n", gpio);
  230. } else {
  231. pr_err("mxc: invalid configuration for GPIO %d: %x\n",
  232. gpio, edge);
  233. return;
  234. }
  235. writel(val | (edge << (bit << 1)), reg);
  236. }
  237. /* handle 32 interrupts in one status register */
  238. static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat)
  239. {
  240. while (irq_stat != 0) {
  241. int irqoffset = fls(irq_stat) - 1;
  242. if (port->both_edges & (1 << irqoffset))
  243. mxc_flip_edge(port, irqoffset);
  244. generic_handle_irq(irq_find_mapping(port->domain, irqoffset));
  245. irq_stat &= ~(1 << irqoffset);
  246. }
  247. }
  248. /* MX1 and MX3 has one interrupt *per* gpio port */
  249. static void mx3_gpio_irq_handler(struct irq_desc *desc)
  250. {
  251. u32 irq_stat;
  252. struct mxc_gpio_port *port = irq_desc_get_handler_data(desc);
  253. struct irq_chip *chip = irq_desc_get_chip(desc);
  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. if (enable) {
  292. if (port->irq_high && (gpio_idx >= 16))
  293. enable_irq_wake(port->irq_high);
  294. else
  295. enable_irq_wake(port->irq);
  296. } else {
  297. if (port->irq_high && (gpio_idx >= 16))
  298. disable_irq_wake(port->irq_high);
  299. else
  300. disable_irq_wake(port->irq);
  301. }
  302. return 0;
  303. }
  304. static int mxc_gpio_init_gc(struct mxc_gpio_port *port, int irq_base)
  305. {
  306. struct irq_chip_generic *gc;
  307. struct irq_chip_type *ct;
  308. gc = irq_alloc_generic_chip("gpio-mxc", 1, irq_base,
  309. port->base, handle_level_irq);
  310. if (!gc)
  311. return -ENOMEM;
  312. gc->private = port;
  313. ct = gc->chip_types;
  314. ct->chip.irq_ack = irq_gc_ack_set_bit;
  315. ct->chip.irq_mask = irq_gc_mask_clr_bit;
  316. ct->chip.irq_unmask = irq_gc_mask_set_bit;
  317. ct->chip.irq_set_type = gpio_set_irq_type;
  318. ct->chip.irq_set_wake = gpio_set_wake_irq;
  319. ct->chip.flags = IRQCHIP_MASK_ON_SUSPEND;
  320. ct->regs.ack = GPIO_ISR;
  321. ct->regs.mask = GPIO_IMR;
  322. irq_setup_generic_chip(gc, IRQ_MSK(32), IRQ_GC_INIT_NESTED_LOCK,
  323. IRQ_NOREQUEST, 0);
  324. return 0;
  325. }
  326. static void mxc_gpio_get_hw(struct platform_device *pdev)
  327. {
  328. const struct of_device_id *of_id =
  329. of_match_device(mxc_gpio_dt_ids, &pdev->dev);
  330. enum mxc_gpio_hwtype hwtype;
  331. if (of_id)
  332. pdev->id_entry = of_id->data;
  333. hwtype = pdev->id_entry->driver_data;
  334. if (mxc_gpio_hwtype) {
  335. /*
  336. * The driver works with a reasonable presupposition,
  337. * that is all gpio ports must be the same type when
  338. * running on one soc.
  339. */
  340. BUG_ON(mxc_gpio_hwtype != hwtype);
  341. return;
  342. }
  343. if (hwtype == IMX35_GPIO)
  344. mxc_gpio_hwdata = &imx35_gpio_hwdata;
  345. else if (hwtype == IMX31_GPIO)
  346. mxc_gpio_hwdata = &imx31_gpio_hwdata;
  347. else
  348. mxc_gpio_hwdata = &imx1_imx21_gpio_hwdata;
  349. mxc_gpio_hwtype = hwtype;
  350. }
  351. static int mxc_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  352. {
  353. struct mxc_gpio_port *port = gpiochip_get_data(gc);
  354. return irq_find_mapping(port->domain, offset);
  355. }
  356. static int mxc_gpio_probe(struct platform_device *pdev)
  357. {
  358. struct device_node *np = pdev->dev.of_node;
  359. struct mxc_gpio_port *port;
  360. struct resource *iores;
  361. int irq_base;
  362. int err;
  363. mxc_gpio_get_hw(pdev);
  364. port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL);
  365. if (!port)
  366. return -ENOMEM;
  367. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  368. port->base = devm_ioremap_resource(&pdev->dev, iores);
  369. if (IS_ERR(port->base))
  370. return PTR_ERR(port->base);
  371. port->irq_high = platform_get_irq(pdev, 1);
  372. port->irq = platform_get_irq(pdev, 0);
  373. if (port->irq < 0)
  374. return port->irq;
  375. /* disable the interrupt and clear the status */
  376. writel(0, port->base + GPIO_IMR);
  377. writel(~0, port->base + GPIO_ISR);
  378. if (mxc_gpio_hwtype == IMX21_GPIO) {
  379. /*
  380. * Setup one handler for all GPIO interrupts. Actually setting
  381. * the handler is needed only once, but doing it for every port
  382. * is more robust and easier.
  383. */
  384. irq_set_chained_handler(port->irq, mx2_gpio_irq_handler);
  385. } else {
  386. /* setup one handler for each entry */
  387. irq_set_chained_handler_and_data(port->irq,
  388. mx3_gpio_irq_handler, port);
  389. if (port->irq_high > 0)
  390. /* setup handler for GPIO 16 to 31 */
  391. irq_set_chained_handler_and_data(port->irq_high,
  392. mx3_gpio_irq_handler,
  393. port);
  394. }
  395. err = bgpio_init(&port->gc, &pdev->dev, 4,
  396. port->base + GPIO_PSR,
  397. port->base + GPIO_DR, NULL,
  398. port->base + GPIO_GDIR, NULL,
  399. BGPIOF_READ_OUTPUT_REG_SET);
  400. if (err)
  401. goto out_bgio;
  402. if (of_property_read_bool(np, "gpio-ranges")) {
  403. port->gc.request = gpiochip_generic_request;
  404. port->gc.free = gpiochip_generic_free;
  405. }
  406. port->gc.to_irq = mxc_gpio_to_irq;
  407. port->gc.base = (pdev->id < 0) ? of_alias_get_id(np, "gpio") * 32 :
  408. pdev->id * 32;
  409. err = devm_gpiochip_add_data(&pdev->dev, &port->gc, port);
  410. if (err)
  411. goto out_bgio;
  412. irq_base = irq_alloc_descs(-1, 0, 32, numa_node_id());
  413. if (irq_base < 0) {
  414. err = irq_base;
  415. goto out_bgio;
  416. }
  417. port->domain = irq_domain_add_legacy(np, 32, irq_base, 0,
  418. &irq_domain_simple_ops, NULL);
  419. if (!port->domain) {
  420. err = -ENODEV;
  421. goto out_irqdesc_free;
  422. }
  423. /* gpio-mxc can be a generic irq chip */
  424. err = mxc_gpio_init_gc(port, irq_base);
  425. if (err < 0)
  426. goto out_irqdomain_remove;
  427. list_add_tail(&port->node, &mxc_gpio_ports);
  428. return 0;
  429. out_irqdomain_remove:
  430. irq_domain_remove(port->domain);
  431. out_irqdesc_free:
  432. irq_free_descs(irq_base, 32);
  433. out_bgio:
  434. dev_info(&pdev->dev, "%s failed with errno %d\n", __func__, err);
  435. return err;
  436. }
  437. static struct platform_driver mxc_gpio_driver = {
  438. .driver = {
  439. .name = "gpio-mxc",
  440. .of_match_table = mxc_gpio_dt_ids,
  441. },
  442. .probe = mxc_gpio_probe,
  443. .id_table = mxc_gpio_devtype,
  444. };
  445. static int __init gpio_mxc_init(void)
  446. {
  447. return platform_driver_register(&mxc_gpio_driver);
  448. }
  449. subsys_initcall(gpio_mxc_init);