gpio-mxs.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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,
  20. * MA 02110-1301, USA.
  21. */
  22. #include <linux/init.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/io.h>
  25. #include <linux/irq.h>
  26. #include <linux/gpio.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/slab.h>
  29. #include <linux/basic_mmio_gpio.h>
  30. #include <mach/mxs.h>
  31. #define MXS_SET 0x4
  32. #define MXS_CLR 0x8
  33. #define PINCTRL_DOUT(n) ((cpu_is_mx23() ? 0x0500 : 0x0700) + (n) * 0x10)
  34. #define PINCTRL_DIN(n) ((cpu_is_mx23() ? 0x0600 : 0x0900) + (n) * 0x10)
  35. #define PINCTRL_DOE(n) ((cpu_is_mx23() ? 0x0700 : 0x0b00) + (n) * 0x10)
  36. #define PINCTRL_PIN2IRQ(n) ((cpu_is_mx23() ? 0x0800 : 0x1000) + (n) * 0x10)
  37. #define PINCTRL_IRQEN(n) ((cpu_is_mx23() ? 0x0900 : 0x1100) + (n) * 0x10)
  38. #define PINCTRL_IRQLEV(n) ((cpu_is_mx23() ? 0x0a00 : 0x1200) + (n) * 0x10)
  39. #define PINCTRL_IRQPOL(n) ((cpu_is_mx23() ? 0x0b00 : 0x1300) + (n) * 0x10)
  40. #define PINCTRL_IRQSTAT(n) ((cpu_is_mx23() ? 0x0c00 : 0x1400) + (n) * 0x10)
  41. #define GPIO_INT_FALL_EDGE 0x0
  42. #define GPIO_INT_LOW_LEV 0x1
  43. #define GPIO_INT_RISE_EDGE 0x2
  44. #define GPIO_INT_HIGH_LEV 0x3
  45. #define GPIO_INT_LEV_MASK (1 << 0)
  46. #define GPIO_INT_POL_MASK (1 << 1)
  47. #define irq_to_gpio(irq) ((irq) - MXS_GPIO_IRQ_START)
  48. struct mxs_gpio_port {
  49. void __iomem *base;
  50. int id;
  51. int irq;
  52. int virtual_irq_start;
  53. struct bgpio_chip bgc;
  54. };
  55. /* Note: This driver assumes 32 GPIOs are handled in one register */
  56. static int mxs_gpio_set_irq_type(struct irq_data *d, unsigned int type)
  57. {
  58. u32 gpio = irq_to_gpio(d->irq);
  59. u32 pin_mask = 1 << (gpio & 31);
  60. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  61. struct mxs_gpio_port *port = gc->private;
  62. void __iomem *pin_addr;
  63. int edge;
  64. switch (type) {
  65. case IRQ_TYPE_EDGE_RISING:
  66. edge = GPIO_INT_RISE_EDGE;
  67. break;
  68. case IRQ_TYPE_EDGE_FALLING:
  69. edge = GPIO_INT_FALL_EDGE;
  70. break;
  71. case IRQ_TYPE_LEVEL_LOW:
  72. edge = GPIO_INT_LOW_LEV;
  73. break;
  74. case IRQ_TYPE_LEVEL_HIGH:
  75. edge = GPIO_INT_HIGH_LEV;
  76. break;
  77. default:
  78. return -EINVAL;
  79. }
  80. /* set level or edge */
  81. pin_addr = port->base + PINCTRL_IRQLEV(port->id);
  82. if (edge & GPIO_INT_LEV_MASK)
  83. writel(pin_mask, pin_addr + MXS_SET);
  84. else
  85. writel(pin_mask, pin_addr + MXS_CLR);
  86. /* set polarity */
  87. pin_addr = port->base + PINCTRL_IRQPOL(port->id);
  88. if (edge & GPIO_INT_POL_MASK)
  89. writel(pin_mask, pin_addr + MXS_SET);
  90. else
  91. writel(pin_mask, pin_addr + MXS_CLR);
  92. writel(1 << (gpio & 0x1f),
  93. port->base + PINCTRL_IRQSTAT(port->id) + MXS_CLR);
  94. return 0;
  95. }
  96. /* MXS has one interrupt *per* gpio port */
  97. static void mxs_gpio_irq_handler(u32 irq, struct irq_desc *desc)
  98. {
  99. u32 irq_stat;
  100. struct mxs_gpio_port *port = irq_get_handler_data(irq);
  101. u32 gpio_irq_no_base = port->virtual_irq_start;
  102. desc->irq_data.chip->irq_ack(&desc->irq_data);
  103. irq_stat = readl(port->base + PINCTRL_IRQSTAT(port->id)) &
  104. readl(port->base + PINCTRL_IRQEN(port->id));
  105. while (irq_stat != 0) {
  106. int irqoffset = fls(irq_stat) - 1;
  107. generic_handle_irq(gpio_irq_no_base + irqoffset);
  108. irq_stat &= ~(1 << irqoffset);
  109. }
  110. }
  111. /*
  112. * Set interrupt number "irq" in the GPIO as a wake-up source.
  113. * While system is running, all registered GPIO interrupts need to have
  114. * wake-up enabled. When system is suspended, only selected GPIO interrupts
  115. * need to have wake-up enabled.
  116. * @param irq interrupt source number
  117. * @param enable enable as wake-up if equal to non-zero
  118. * @return This function returns 0 on success.
  119. */
  120. static int mxs_gpio_set_wake_irq(struct irq_data *d, unsigned int enable)
  121. {
  122. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  123. struct mxs_gpio_port *port = gc->private;
  124. if (enable)
  125. enable_irq_wake(port->irq);
  126. else
  127. disable_irq_wake(port->irq);
  128. return 0;
  129. }
  130. static void __init mxs_gpio_init_gc(struct mxs_gpio_port *port)
  131. {
  132. struct irq_chip_generic *gc;
  133. struct irq_chip_type *ct;
  134. gc = irq_alloc_generic_chip("gpio-mxs", 1, port->virtual_irq_start,
  135. port->base, handle_level_irq);
  136. gc->private = port;
  137. ct = gc->chip_types;
  138. ct->chip.irq_ack = irq_gc_ack_set_bit;
  139. ct->chip.irq_mask = irq_gc_mask_clr_bit;
  140. ct->chip.irq_unmask = irq_gc_mask_set_bit;
  141. ct->chip.irq_set_type = mxs_gpio_set_irq_type;
  142. ct->chip.irq_set_wake = mxs_gpio_set_wake_irq;
  143. ct->regs.ack = PINCTRL_IRQSTAT(port->id) + MXS_CLR;
  144. ct->regs.mask = PINCTRL_IRQEN(port->id);
  145. irq_setup_generic_chip(gc, IRQ_MSK(32), 0, IRQ_NOREQUEST, 0);
  146. }
  147. static int mxs_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  148. {
  149. struct bgpio_chip *bgc = to_bgpio_chip(gc);
  150. struct mxs_gpio_port *port =
  151. container_of(bgc, struct mxs_gpio_port, bgc);
  152. return port->virtual_irq_start + offset;
  153. }
  154. static int __devinit mxs_gpio_probe(struct platform_device *pdev)
  155. {
  156. static void __iomem *base;
  157. struct mxs_gpio_port *port;
  158. struct resource *iores = NULL;
  159. int err;
  160. port = kzalloc(sizeof(struct mxs_gpio_port), GFP_KERNEL);
  161. if (!port)
  162. return -ENOMEM;
  163. port->id = pdev->id;
  164. port->virtual_irq_start = MXS_GPIO_IRQ_START + port->id * 32;
  165. /*
  166. * map memory region only once, as all the gpio ports
  167. * share the same one
  168. */
  169. if (!base) {
  170. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  171. if (!iores) {
  172. err = -ENODEV;
  173. goto out_kfree;
  174. }
  175. if (!request_mem_region(iores->start, resource_size(iores),
  176. pdev->name)) {
  177. err = -EBUSY;
  178. goto out_kfree;
  179. }
  180. base = ioremap(iores->start, resource_size(iores));
  181. if (!base) {
  182. err = -ENOMEM;
  183. goto out_release_mem;
  184. }
  185. }
  186. port->base = base;
  187. port->irq = platform_get_irq(pdev, 0);
  188. if (port->irq < 0) {
  189. err = -EINVAL;
  190. goto out_iounmap;
  191. }
  192. /*
  193. * select the pin interrupt functionality but initially
  194. * disable the interrupts
  195. */
  196. writel(~0U, port->base + PINCTRL_PIN2IRQ(port->id));
  197. writel(0, port->base + PINCTRL_IRQEN(port->id));
  198. /* clear address has to be used to clear IRQSTAT bits */
  199. writel(~0U, port->base + PINCTRL_IRQSTAT(port->id) + MXS_CLR);
  200. /* gpio-mxs can be a generic irq chip */
  201. mxs_gpio_init_gc(port);
  202. /* setup one handler for each entry */
  203. irq_set_chained_handler(port->irq, mxs_gpio_irq_handler);
  204. irq_set_handler_data(port->irq, port);
  205. err = bgpio_init(&port->bgc, &pdev->dev, 4,
  206. port->base + PINCTRL_DIN(port->id),
  207. port->base + PINCTRL_DOUT(port->id), NULL,
  208. port->base + PINCTRL_DOE(port->id), NULL, false);
  209. if (err)
  210. goto out_iounmap;
  211. port->bgc.gc.to_irq = mxs_gpio_to_irq;
  212. port->bgc.gc.base = port->id * 32;
  213. err = gpiochip_add(&port->bgc.gc);
  214. if (err)
  215. goto out_bgpio_remove;
  216. return 0;
  217. out_bgpio_remove:
  218. bgpio_remove(&port->bgc);
  219. out_iounmap:
  220. if (iores)
  221. iounmap(port->base);
  222. out_release_mem:
  223. if (iores)
  224. release_mem_region(iores->start, resource_size(iores));
  225. out_kfree:
  226. kfree(port);
  227. dev_info(&pdev->dev, "%s failed with errno %d\n", __func__, err);
  228. return err;
  229. }
  230. static struct platform_driver mxs_gpio_driver = {
  231. .driver = {
  232. .name = "gpio-mxs",
  233. .owner = THIS_MODULE,
  234. },
  235. .probe = mxs_gpio_probe,
  236. };
  237. static int __init mxs_gpio_init(void)
  238. {
  239. return platform_driver_register(&mxs_gpio_driver);
  240. }
  241. postcore_initcall(mxs_gpio_init);
  242. MODULE_AUTHOR("Freescale Semiconductor, "
  243. "Daniel Mack <danielncaiaq.de>, "
  244. "Juergen Beisert <kernel@pengutronix.de>");
  245. MODULE_DESCRIPTION("Freescale MXS GPIO");
  246. MODULE_LICENSE("GPL");