gpio-mxs.c 8.7 KB

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