gpio-mxs.c 7.8 KB

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