gpio-mxc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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,
  6. * Copyright (C) 2004-2010 Freescale Semiconductor, Inc. All Rights Reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. */
  21. #include <linux/init.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/io.h>
  24. #include <linux/irq.h>
  25. #include <linux/gpio.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/slab.h>
  28. #include <linux/basic_mmio_gpio.h>
  29. #include <asm-generic/bug.h>
  30. enum mxc_gpio_hwtype {
  31. IMX1_GPIO, /* runs on i.mx1 */
  32. IMX21_GPIO, /* runs on i.mx21 and i.mx27 */
  33. IMX31_GPIO, /* runs on all other i.mx */
  34. };
  35. /* device type dependent stuff */
  36. struct mxc_gpio_hwdata {
  37. unsigned dr_reg;
  38. unsigned gdir_reg;
  39. unsigned psr_reg;
  40. unsigned icr1_reg;
  41. unsigned icr2_reg;
  42. unsigned imr_reg;
  43. unsigned isr_reg;
  44. unsigned low_level;
  45. unsigned high_level;
  46. unsigned rise_edge;
  47. unsigned fall_edge;
  48. };
  49. struct mxc_gpio_port {
  50. struct list_head node;
  51. void __iomem *base;
  52. int irq;
  53. int irq_high;
  54. int virtual_irq_start;
  55. struct bgpio_chip bgc;
  56. u32 both_edges;
  57. };
  58. static struct mxc_gpio_hwdata imx1_imx21_gpio_hwdata = {
  59. .dr_reg = 0x1c,
  60. .gdir_reg = 0x00,
  61. .psr_reg = 0x24,
  62. .icr1_reg = 0x28,
  63. .icr2_reg = 0x2c,
  64. .imr_reg = 0x30,
  65. .isr_reg = 0x34,
  66. .low_level = 0x03,
  67. .high_level = 0x02,
  68. .rise_edge = 0x00,
  69. .fall_edge = 0x01,
  70. };
  71. static struct mxc_gpio_hwdata imx31_gpio_hwdata = {
  72. .dr_reg = 0x00,
  73. .gdir_reg = 0x04,
  74. .psr_reg = 0x08,
  75. .icr1_reg = 0x0c,
  76. .icr2_reg = 0x10,
  77. .imr_reg = 0x14,
  78. .isr_reg = 0x18,
  79. .low_level = 0x00,
  80. .high_level = 0x01,
  81. .rise_edge = 0x02,
  82. .fall_edge = 0x03,
  83. };
  84. static enum mxc_gpio_hwtype mxc_gpio_hwtype;
  85. static struct mxc_gpio_hwdata *mxc_gpio_hwdata;
  86. #define GPIO_DR (mxc_gpio_hwdata->dr_reg)
  87. #define GPIO_GDIR (mxc_gpio_hwdata->gdir_reg)
  88. #define GPIO_PSR (mxc_gpio_hwdata->psr_reg)
  89. #define GPIO_ICR1 (mxc_gpio_hwdata->icr1_reg)
  90. #define GPIO_ICR2 (mxc_gpio_hwdata->icr2_reg)
  91. #define GPIO_IMR (mxc_gpio_hwdata->imr_reg)
  92. #define GPIO_ISR (mxc_gpio_hwdata->isr_reg)
  93. #define GPIO_INT_LOW_LEV (mxc_gpio_hwdata->low_level)
  94. #define GPIO_INT_HIGH_LEV (mxc_gpio_hwdata->high_level)
  95. #define GPIO_INT_RISE_EDGE (mxc_gpio_hwdata->rise_edge)
  96. #define GPIO_INT_FALL_EDGE (mxc_gpio_hwdata->fall_edge)
  97. #define GPIO_INT_NONE 0x4
  98. static struct platform_device_id mxc_gpio_devtype[] = {
  99. {
  100. .name = "imx1-gpio",
  101. .driver_data = IMX1_GPIO,
  102. }, {
  103. .name = "imx21-gpio",
  104. .driver_data = IMX21_GPIO,
  105. }, {
  106. .name = "imx31-gpio",
  107. .driver_data = IMX31_GPIO,
  108. }, {
  109. /* sentinel */
  110. }
  111. };
  112. /*
  113. * MX2 has one interrupt *for all* gpio ports. The list is used
  114. * to save the references to all ports, so that mx2_gpio_irq_handler
  115. * can walk through all interrupt status registers.
  116. */
  117. static LIST_HEAD(mxc_gpio_ports);
  118. /* Note: This driver assumes 32 GPIOs are handled in one register */
  119. static int gpio_set_irq_type(struct irq_data *d, u32 type)
  120. {
  121. u32 gpio = irq_to_gpio(d->irq);
  122. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  123. struct mxc_gpio_port *port = gc->private;
  124. u32 bit, val;
  125. int edge;
  126. void __iomem *reg = port->base;
  127. port->both_edges &= ~(1 << (gpio & 31));
  128. switch (type) {
  129. case IRQ_TYPE_EDGE_RISING:
  130. edge = GPIO_INT_RISE_EDGE;
  131. break;
  132. case IRQ_TYPE_EDGE_FALLING:
  133. edge = GPIO_INT_FALL_EDGE;
  134. break;
  135. case IRQ_TYPE_EDGE_BOTH:
  136. val = gpio_get_value(gpio);
  137. if (val) {
  138. edge = GPIO_INT_LOW_LEV;
  139. pr_debug("mxc: set GPIO %d to low trigger\n", gpio);
  140. } else {
  141. edge = GPIO_INT_HIGH_LEV;
  142. pr_debug("mxc: set GPIO %d to high trigger\n", gpio);
  143. }
  144. port->both_edges |= 1 << (gpio & 31);
  145. break;
  146. case IRQ_TYPE_LEVEL_LOW:
  147. edge = GPIO_INT_LOW_LEV;
  148. break;
  149. case IRQ_TYPE_LEVEL_HIGH:
  150. edge = GPIO_INT_HIGH_LEV;
  151. break;
  152. default:
  153. return -EINVAL;
  154. }
  155. reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
  156. bit = gpio & 0xf;
  157. val = readl(reg) & ~(0x3 << (bit << 1));
  158. writel(val | (edge << (bit << 1)), reg);
  159. writel(1 << (gpio & 0x1f), port->base + GPIO_ISR);
  160. return 0;
  161. }
  162. static void mxc_flip_edge(struct mxc_gpio_port *port, u32 gpio)
  163. {
  164. void __iomem *reg = port->base;
  165. u32 bit, val;
  166. int edge;
  167. reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
  168. bit = gpio & 0xf;
  169. val = readl(reg);
  170. edge = (val >> (bit << 1)) & 3;
  171. val &= ~(0x3 << (bit << 1));
  172. if (edge == GPIO_INT_HIGH_LEV) {
  173. edge = GPIO_INT_LOW_LEV;
  174. pr_debug("mxc: switch GPIO %d to low trigger\n", gpio);
  175. } else if (edge == GPIO_INT_LOW_LEV) {
  176. edge = GPIO_INT_HIGH_LEV;
  177. pr_debug("mxc: switch GPIO %d to high trigger\n", gpio);
  178. } else {
  179. pr_err("mxc: invalid configuration for GPIO %d: %x\n",
  180. gpio, edge);
  181. return;
  182. }
  183. writel(val | (edge << (bit << 1)), reg);
  184. }
  185. /* handle 32 interrupts in one status register */
  186. static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat)
  187. {
  188. u32 gpio_irq_no_base = port->virtual_irq_start;
  189. while (irq_stat != 0) {
  190. int irqoffset = fls(irq_stat) - 1;
  191. if (port->both_edges & (1 << irqoffset))
  192. mxc_flip_edge(port, irqoffset);
  193. generic_handle_irq(gpio_irq_no_base + irqoffset);
  194. irq_stat &= ~(1 << irqoffset);
  195. }
  196. }
  197. /* MX1 and MX3 has one interrupt *per* gpio port */
  198. static void mx3_gpio_irq_handler(u32 irq, struct irq_desc *desc)
  199. {
  200. u32 irq_stat;
  201. struct mxc_gpio_port *port = irq_get_handler_data(irq);
  202. irq_stat = readl(port->base + GPIO_ISR) & readl(port->base + GPIO_IMR);
  203. mxc_gpio_irq_handler(port, irq_stat);
  204. }
  205. /* MX2 has one interrupt *for all* gpio ports */
  206. static void mx2_gpio_irq_handler(u32 irq, struct irq_desc *desc)
  207. {
  208. u32 irq_msk, irq_stat;
  209. struct mxc_gpio_port *port;
  210. /* walk through all interrupt status registers */
  211. list_for_each_entry(port, &mxc_gpio_ports, node) {
  212. irq_msk = readl(port->base + GPIO_IMR);
  213. if (!irq_msk)
  214. continue;
  215. irq_stat = readl(port->base + GPIO_ISR) & irq_msk;
  216. if (irq_stat)
  217. mxc_gpio_irq_handler(port, irq_stat);
  218. }
  219. }
  220. /*
  221. * Set interrupt number "irq" in the GPIO as a wake-up source.
  222. * While system is running, all registered GPIO interrupts need to have
  223. * wake-up enabled. When system is suspended, only selected GPIO interrupts
  224. * need to have wake-up enabled.
  225. * @param irq interrupt source number
  226. * @param enable enable as wake-up if equal to non-zero
  227. * @return This function returns 0 on success.
  228. */
  229. static int gpio_set_wake_irq(struct irq_data *d, u32 enable)
  230. {
  231. u32 gpio = irq_to_gpio(d->irq);
  232. u32 gpio_idx = gpio & 0x1F;
  233. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  234. struct mxc_gpio_port *port = gc->private;
  235. if (enable) {
  236. if (port->irq_high && (gpio_idx >= 16))
  237. enable_irq_wake(port->irq_high);
  238. else
  239. enable_irq_wake(port->irq);
  240. } else {
  241. if (port->irq_high && (gpio_idx >= 16))
  242. disable_irq_wake(port->irq_high);
  243. else
  244. disable_irq_wake(port->irq);
  245. }
  246. return 0;
  247. }
  248. static void __init mxc_gpio_init_gc(struct mxc_gpio_port *port)
  249. {
  250. struct irq_chip_generic *gc;
  251. struct irq_chip_type *ct;
  252. gc = irq_alloc_generic_chip("gpio-mxc", 1, port->virtual_irq_start,
  253. port->base, handle_level_irq);
  254. gc->private = port;
  255. ct = gc->chip_types;
  256. ct->chip.irq_ack = irq_gc_ack,
  257. ct->chip.irq_mask = irq_gc_mask_clr_bit;
  258. ct->chip.irq_unmask = irq_gc_mask_set_bit;
  259. ct->chip.irq_set_type = gpio_set_irq_type;
  260. ct->chip.irq_set_wake = gpio_set_wake_irq,
  261. ct->regs.ack = GPIO_ISR;
  262. ct->regs.mask = GPIO_IMR;
  263. irq_setup_generic_chip(gc, IRQ_MSK(32), IRQ_GC_INIT_NESTED_LOCK,
  264. IRQ_NOREQUEST, 0);
  265. }
  266. static void __devinit mxc_gpio_get_hw(struct platform_device *pdev)
  267. {
  268. enum mxc_gpio_hwtype hwtype = pdev->id_entry->driver_data;
  269. if (mxc_gpio_hwtype) {
  270. /*
  271. * The driver works with a reasonable presupposition,
  272. * that is all gpio ports must be the same type when
  273. * running on one soc.
  274. */
  275. BUG_ON(mxc_gpio_hwtype != hwtype);
  276. return;
  277. }
  278. if (hwtype == IMX31_GPIO)
  279. mxc_gpio_hwdata = &imx31_gpio_hwdata;
  280. else
  281. mxc_gpio_hwdata = &imx1_imx21_gpio_hwdata;
  282. mxc_gpio_hwtype = hwtype;
  283. }
  284. static int __devinit mxc_gpio_probe(struct platform_device *pdev)
  285. {
  286. struct mxc_gpio_port *port;
  287. struct resource *iores;
  288. int err;
  289. mxc_gpio_get_hw(pdev);
  290. port = kzalloc(sizeof(struct mxc_gpio_port), GFP_KERNEL);
  291. if (!port)
  292. return -ENOMEM;
  293. port->virtual_irq_start = MXC_GPIO_IRQ_START + pdev->id * 32;
  294. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  295. if (!iores) {
  296. err = -ENODEV;
  297. goto out_kfree;
  298. }
  299. if (!request_mem_region(iores->start, resource_size(iores),
  300. pdev->name)) {
  301. err = -EBUSY;
  302. goto out_kfree;
  303. }
  304. port->base = ioremap(iores->start, resource_size(iores));
  305. if (!port->base) {
  306. err = -ENOMEM;
  307. goto out_release_mem;
  308. }
  309. port->irq_high = platform_get_irq(pdev, 1);
  310. port->irq = platform_get_irq(pdev, 0);
  311. if (port->irq < 0) {
  312. err = -EINVAL;
  313. goto out_iounmap;
  314. }
  315. /* disable the interrupt and clear the status */
  316. writel(0, port->base + GPIO_IMR);
  317. writel(~0, port->base + GPIO_ISR);
  318. /* gpio-mxc can be a generic irq chip */
  319. mxc_gpio_init_gc(port);
  320. if (mxc_gpio_hwtype == IMX21_GPIO) {
  321. /* setup one handler for all GPIO interrupts */
  322. if (pdev->id == 0)
  323. irq_set_chained_handler(port->irq,
  324. mx2_gpio_irq_handler);
  325. } else {
  326. /* setup one handler for each entry */
  327. irq_set_chained_handler(port->irq, mx3_gpio_irq_handler);
  328. irq_set_handler_data(port->irq, port);
  329. if (port->irq_high > 0) {
  330. /* setup handler for GPIO 16 to 31 */
  331. irq_set_chained_handler(port->irq_high,
  332. mx3_gpio_irq_handler);
  333. irq_set_handler_data(port->irq_high, port);
  334. }
  335. }
  336. err = bgpio_init(&port->bgc, &pdev->dev, 4,
  337. port->base + GPIO_PSR,
  338. port->base + GPIO_DR, NULL,
  339. port->base + GPIO_GDIR, NULL, false);
  340. if (err)
  341. goto out_iounmap;
  342. port->bgc.gc.base = pdev->id * 32;
  343. port->bgc.dir = port->bgc.read_reg(port->bgc.reg_dir);
  344. port->bgc.data = port->bgc.read_reg(port->bgc.reg_set);
  345. err = gpiochip_add(&port->bgc.gc);
  346. if (err)
  347. goto out_bgpio_remove;
  348. list_add_tail(&port->node, &mxc_gpio_ports);
  349. return 0;
  350. out_bgpio_remove:
  351. bgpio_remove(&port->bgc);
  352. out_iounmap:
  353. iounmap(port->base);
  354. out_release_mem:
  355. release_mem_region(iores->start, resource_size(iores));
  356. out_kfree:
  357. kfree(port);
  358. dev_info(&pdev->dev, "%s failed with errno %d\n", __func__, err);
  359. return err;
  360. }
  361. static struct platform_driver mxc_gpio_driver = {
  362. .driver = {
  363. .name = "gpio-mxc",
  364. .owner = THIS_MODULE,
  365. },
  366. .probe = mxc_gpio_probe,
  367. .id_table = mxc_gpio_devtype,
  368. };
  369. static int __init gpio_mxc_init(void)
  370. {
  371. return platform_driver_register(&mxc_gpio_driver);
  372. }
  373. postcore_initcall(gpio_mxc_init);
  374. MODULE_AUTHOR("Freescale Semiconductor, "
  375. "Daniel Mack <danielncaiaq.de>, "
  376. "Juergen Beisert <kernel@pengutronix.de>");
  377. MODULE_DESCRIPTION("Freescale MXC GPIO");
  378. MODULE_LICENSE("GPL");