gpio-mxs.c 7.9 KB

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