gpio-mxs.c 8.4 KB

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