gpio-mxs.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 <mach/mxs.h>
  30. #define MXS_SET 0x4
  31. #define MXS_CLR 0x8
  32. #define PINCTRL_DOUT(n) ((cpu_is_mx23() ? 0x0500 : 0x0700) + (n) * 0x10)
  33. #define PINCTRL_DIN(n) ((cpu_is_mx23() ? 0x0600 : 0x0900) + (n) * 0x10)
  34. #define PINCTRL_DOE(n) ((cpu_is_mx23() ? 0x0700 : 0x0b00) + (n) * 0x10)
  35. #define PINCTRL_PIN2IRQ(n) ((cpu_is_mx23() ? 0x0800 : 0x1000) + (n) * 0x10)
  36. #define PINCTRL_IRQEN(n) ((cpu_is_mx23() ? 0x0900 : 0x1100) + (n) * 0x10)
  37. #define PINCTRL_IRQLEV(n) ((cpu_is_mx23() ? 0x0a00 : 0x1200) + (n) * 0x10)
  38. #define PINCTRL_IRQPOL(n) ((cpu_is_mx23() ? 0x0b00 : 0x1300) + (n) * 0x10)
  39. #define PINCTRL_IRQSTAT(n) ((cpu_is_mx23() ? 0x0c00 : 0x1400) + (n) * 0x10)
  40. #define GPIO_INT_FALL_EDGE 0x0
  41. #define GPIO_INT_LOW_LEV 0x1
  42. #define GPIO_INT_RISE_EDGE 0x2
  43. #define GPIO_INT_HIGH_LEV 0x3
  44. #define GPIO_INT_LEV_MASK (1 << 0)
  45. #define GPIO_INT_POL_MASK (1 << 1)
  46. struct mxs_gpio_port {
  47. void __iomem *base;
  48. int id;
  49. int irq;
  50. int irq_high;
  51. int virtual_irq_start;
  52. struct gpio_chip chip;
  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_get(struct gpio_chip *chip, unsigned offset);
  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 void mxs_set_gpio_direction(struct gpio_chip *chip, unsigned offset,
  180. int dir)
  181. {
  182. struct mxs_gpio_port *port =
  183. container_of(chip, struct mxs_gpio_port, chip);
  184. void __iomem *pin_addr = port->base + PINCTRL_DOE(port->id);
  185. if (dir)
  186. writel(1 << offset, pin_addr + MXS_SET);
  187. else
  188. writel(1 << offset, pin_addr + MXS_CLR);
  189. }
  190. static int mxs_gpio_get(struct gpio_chip *chip, unsigned offset)
  191. {
  192. struct mxs_gpio_port *port =
  193. container_of(chip, struct mxs_gpio_port, chip);
  194. return (readl(port->base + PINCTRL_DIN(port->id)) >> offset) & 1;
  195. }
  196. static void mxs_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  197. {
  198. struct mxs_gpio_port *port =
  199. container_of(chip, struct mxs_gpio_port, chip);
  200. void __iomem *pin_addr = port->base + PINCTRL_DOUT(port->id);
  201. if (value)
  202. writel(1 << offset, pin_addr + MXS_SET);
  203. else
  204. writel(1 << offset, pin_addr + MXS_CLR);
  205. }
  206. static int mxs_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  207. {
  208. struct mxs_gpio_port *port =
  209. container_of(chip, struct mxs_gpio_port, chip);
  210. return port->virtual_irq_start + offset;
  211. }
  212. static int mxs_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  213. {
  214. mxs_set_gpio_direction(chip, offset, 0);
  215. return 0;
  216. }
  217. static int mxs_gpio_direction_output(struct gpio_chip *chip,
  218. unsigned offset, int value)
  219. {
  220. mxs_gpio_set(chip, offset, value);
  221. mxs_set_gpio_direction(chip, offset, 1);
  222. return 0;
  223. }
  224. static int __devinit mxs_gpio_probe(struct platform_device *pdev)
  225. {
  226. static void __iomem *base;
  227. struct mxs_gpio_port *port;
  228. struct resource *iores = NULL;
  229. int err, i;
  230. port = kzalloc(sizeof(struct mxs_gpio_port), GFP_KERNEL);
  231. if (!port)
  232. return -ENOMEM;
  233. port->id = pdev->id;
  234. port->virtual_irq_start = MXS_GPIO_IRQ_START + port->id * 32;
  235. /*
  236. * map memory region only once, as all the gpio ports
  237. * share the same one
  238. */
  239. if (!base) {
  240. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  241. if (!iores) {
  242. err = -ENODEV;
  243. goto out_kfree;
  244. }
  245. if (!request_mem_region(iores->start, resource_size(iores),
  246. pdev->name)) {
  247. err = -EBUSY;
  248. goto out_kfree;
  249. }
  250. base = ioremap(iores->start, resource_size(iores));
  251. if (!base) {
  252. err = -ENOMEM;
  253. goto out_release_mem;
  254. }
  255. }
  256. port->base = base;
  257. port->irq = platform_get_irq(pdev, 0);
  258. if (port->irq < 0) {
  259. err = -EINVAL;
  260. goto out_iounmap;
  261. }
  262. /* disable the interrupt and clear the status */
  263. writel(0, port->base + PINCTRL_PIN2IRQ(port->id));
  264. writel(0, port->base + PINCTRL_IRQEN(port->id));
  265. /* clear address has to be used to clear IRQSTAT bits */
  266. writel(~0U, port->base + PINCTRL_IRQSTAT(port->id) + MXS_CLR);
  267. for (i = port->virtual_irq_start;
  268. i < port->virtual_irq_start + 32; i++) {
  269. irq_set_chip_and_handler(i, &gpio_irq_chip,
  270. handle_level_irq);
  271. set_irq_flags(i, IRQF_VALID);
  272. irq_set_chip_data(i, port);
  273. }
  274. /* setup one handler for each entry */
  275. irq_set_chained_handler(port->irq, mxs_gpio_irq_handler);
  276. irq_set_handler_data(port->irq, port);
  277. /* register gpio chip */
  278. port->chip.direction_input = mxs_gpio_direction_input;
  279. port->chip.direction_output = mxs_gpio_direction_output;
  280. port->chip.get = mxs_gpio_get;
  281. port->chip.set = mxs_gpio_set;
  282. port->chip.to_irq = mxs_gpio_to_irq;
  283. port->chip.base = port->id * 32;
  284. port->chip.ngpio = 32;
  285. err = gpiochip_add(&port->chip);
  286. if (err)
  287. goto out_iounmap;
  288. return 0;
  289. out_iounmap:
  290. if (iores)
  291. iounmap(port->base);
  292. out_release_mem:
  293. if (iores)
  294. release_mem_region(iores->start, resource_size(iores));
  295. out_kfree:
  296. kfree(port);
  297. dev_info(&pdev->dev, "%s failed with errno %d\n", __func__, err);
  298. return err;
  299. }
  300. static struct platform_driver mxs_gpio_driver = {
  301. .driver = {
  302. .name = "gpio-mxs",
  303. .owner = THIS_MODULE,
  304. },
  305. .probe = mxs_gpio_probe,
  306. };
  307. static int __init mxs_gpio_init(void)
  308. {
  309. return platform_driver_register(&mxs_gpio_driver);
  310. }
  311. postcore_initcall(mxs_gpio_init);
  312. MODULE_AUTHOR("Freescale Semiconductor, "
  313. "Daniel Mack <danielncaiaq.de>, "
  314. "Juergen Beisert <kernel@pengutronix.de>");
  315. MODULE_DESCRIPTION("Freescale MXS GPIO");
  316. MODULE_LICENSE("GPL");