gpio.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 <mach/mx23.h>
  28. #include <mach/mx28.h>
  29. #include <asm-generic/bug.h>
  30. #include "gpio.h"
  31. static struct mxs_gpio_port *mxs_gpio_ports;
  32. static int gpio_table_size;
  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. /* Note: This driver assumes 32 GPIOs are handled in one register */
  48. static void clear_gpio_irqstatus(struct mxs_gpio_port *port, u32 index)
  49. {
  50. __mxs_clrl(1 << index, port->base + PINCTRL_IRQSTAT(port->id));
  51. }
  52. static void set_gpio_irqenable(struct mxs_gpio_port *port, u32 index,
  53. int enable)
  54. {
  55. if (enable) {
  56. __mxs_setl(1 << index, port->base + PINCTRL_IRQEN(port->id));
  57. __mxs_setl(1 << index, port->base + PINCTRL_PIN2IRQ(port->id));
  58. } else {
  59. __mxs_clrl(1 << index, port->base + PINCTRL_IRQEN(port->id));
  60. }
  61. }
  62. static void mxs_gpio_ack_irq(struct irq_data *d)
  63. {
  64. u32 gpio = irq_to_gpio(d->irq);
  65. clear_gpio_irqstatus(&mxs_gpio_ports[gpio / 32], gpio & 0x1f);
  66. }
  67. static void mxs_gpio_mask_irq(struct irq_data *d)
  68. {
  69. u32 gpio = irq_to_gpio(d->irq);
  70. set_gpio_irqenable(&mxs_gpio_ports[gpio / 32], gpio & 0x1f, 0);
  71. }
  72. static void mxs_gpio_unmask_irq(struct irq_data *d)
  73. {
  74. u32 gpio = irq_to_gpio(d->irq);
  75. set_gpio_irqenable(&mxs_gpio_ports[gpio / 32], gpio & 0x1f, 1);
  76. }
  77. static int mxs_gpio_get(struct gpio_chip *chip, unsigned offset);
  78. static int mxs_gpio_set_irq_type(struct irq_data *d, unsigned int type)
  79. {
  80. u32 gpio = irq_to_gpio(d->irq);
  81. u32 pin_mask = 1 << (gpio & 31);
  82. struct mxs_gpio_port *port = &mxs_gpio_ports[gpio / 32];
  83. void __iomem *pin_addr;
  84. int edge;
  85. switch (type) {
  86. case IRQ_TYPE_EDGE_RISING:
  87. edge = GPIO_INT_RISE_EDGE;
  88. break;
  89. case IRQ_TYPE_EDGE_FALLING:
  90. edge = GPIO_INT_FALL_EDGE;
  91. break;
  92. case IRQ_TYPE_LEVEL_LOW:
  93. edge = GPIO_INT_LOW_LEV;
  94. break;
  95. case IRQ_TYPE_LEVEL_HIGH:
  96. edge = GPIO_INT_HIGH_LEV;
  97. break;
  98. default:
  99. return -EINVAL;
  100. }
  101. /* set level or edge */
  102. pin_addr = port->base + PINCTRL_IRQLEV(port->id);
  103. if (edge & GPIO_INT_LEV_MASK)
  104. __mxs_setl(pin_mask, pin_addr);
  105. else
  106. __mxs_clrl(pin_mask, pin_addr);
  107. /* set polarity */
  108. pin_addr = port->base + PINCTRL_IRQPOL(port->id);
  109. if (edge & GPIO_INT_POL_MASK)
  110. __mxs_setl(pin_mask, pin_addr);
  111. else
  112. __mxs_clrl(pin_mask, pin_addr);
  113. clear_gpio_irqstatus(port, gpio & 0x1f);
  114. return 0;
  115. }
  116. /* MXS has one interrupt *per* gpio port */
  117. static void mxs_gpio_irq_handler(u32 irq, struct irq_desc *desc)
  118. {
  119. u32 irq_stat;
  120. struct mxs_gpio_port *port = (struct mxs_gpio_port *)irq_get_handler_data(irq);
  121. u32 gpio_irq_no_base = port->virtual_irq_start;
  122. desc->irq_data.chip->irq_ack(&desc->irq_data);
  123. irq_stat = __raw_readl(port->base + PINCTRL_IRQSTAT(port->id)) &
  124. __raw_readl(port->base + PINCTRL_IRQEN(port->id));
  125. while (irq_stat != 0) {
  126. int irqoffset = fls(irq_stat) - 1;
  127. generic_handle_irq(gpio_irq_no_base + irqoffset);
  128. irq_stat &= ~(1 << irqoffset);
  129. }
  130. }
  131. /*
  132. * Set interrupt number "irq" in the GPIO as a wake-up source.
  133. * While system is running, all registered GPIO interrupts need to have
  134. * wake-up enabled. When system is suspended, only selected GPIO interrupts
  135. * need to have wake-up enabled.
  136. * @param irq interrupt source number
  137. * @param enable enable as wake-up if equal to non-zero
  138. * @return This function returns 0 on success.
  139. */
  140. static int mxs_gpio_set_wake_irq(struct irq_data *d, unsigned int enable)
  141. {
  142. u32 gpio = irq_to_gpio(d->irq);
  143. u32 gpio_idx = gpio & 0x1f;
  144. struct mxs_gpio_port *port = &mxs_gpio_ports[gpio / 32];
  145. if (enable) {
  146. if (port->irq_high && (gpio_idx >= 16))
  147. enable_irq_wake(port->irq_high);
  148. else
  149. enable_irq_wake(port->irq);
  150. } else {
  151. if (port->irq_high && (gpio_idx >= 16))
  152. disable_irq_wake(port->irq_high);
  153. else
  154. disable_irq_wake(port->irq);
  155. }
  156. return 0;
  157. }
  158. static struct irq_chip gpio_irq_chip = {
  159. .name = "mxs gpio",
  160. .irq_ack = mxs_gpio_ack_irq,
  161. .irq_mask = mxs_gpio_mask_irq,
  162. .irq_unmask = mxs_gpio_unmask_irq,
  163. .irq_set_type = mxs_gpio_set_irq_type,
  164. .irq_set_wake = mxs_gpio_set_wake_irq,
  165. };
  166. static void mxs_set_gpio_direction(struct gpio_chip *chip, unsigned offset,
  167. int dir)
  168. {
  169. struct mxs_gpio_port *port =
  170. container_of(chip, struct mxs_gpio_port, chip);
  171. void __iomem *pin_addr = port->base + PINCTRL_DOE(port->id);
  172. if (dir)
  173. __mxs_setl(1 << offset, pin_addr);
  174. else
  175. __mxs_clrl(1 << offset, pin_addr);
  176. }
  177. static int mxs_gpio_get(struct gpio_chip *chip, unsigned offset)
  178. {
  179. struct mxs_gpio_port *port =
  180. container_of(chip, struct mxs_gpio_port, chip);
  181. return (__raw_readl(port->base + PINCTRL_DIN(port->id)) >> offset) & 1;
  182. }
  183. static void mxs_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  184. {
  185. struct mxs_gpio_port *port =
  186. container_of(chip, struct mxs_gpio_port, chip);
  187. void __iomem *pin_addr = port->base + PINCTRL_DOUT(port->id);
  188. if (value)
  189. __mxs_setl(1 << offset, pin_addr);
  190. else
  191. __mxs_clrl(1 << offset, pin_addr);
  192. }
  193. static int mxs_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  194. {
  195. struct mxs_gpio_port *port =
  196. container_of(chip, struct mxs_gpio_port, chip);
  197. return port->virtual_irq_start + offset;
  198. }
  199. static int mxs_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  200. {
  201. mxs_set_gpio_direction(chip, offset, 0);
  202. return 0;
  203. }
  204. static int mxs_gpio_direction_output(struct gpio_chip *chip,
  205. unsigned offset, int value)
  206. {
  207. mxs_gpio_set(chip, offset, value);
  208. mxs_set_gpio_direction(chip, offset, 1);
  209. return 0;
  210. }
  211. int __init mxs_gpio_init(struct mxs_gpio_port *port, int cnt)
  212. {
  213. int i, j;
  214. /* save for local usage */
  215. mxs_gpio_ports = port;
  216. gpio_table_size = cnt;
  217. pr_info("MXS GPIO hardware\n");
  218. for (i = 0; i < cnt; i++) {
  219. /* disable the interrupt and clear the status */
  220. __raw_writel(0, port[i].base + PINCTRL_PIN2IRQ(i));
  221. __raw_writel(0, port[i].base + PINCTRL_IRQEN(i));
  222. /* clear address has to be used to clear IRQSTAT bits */
  223. __mxs_clrl(~0U, port[i].base + PINCTRL_IRQSTAT(i));
  224. for (j = port[i].virtual_irq_start;
  225. j < port[i].virtual_irq_start + 32; j++) {
  226. irq_set_chip_and_handler(j, &gpio_irq_chip,
  227. handle_level_irq);
  228. set_irq_flags(j, IRQF_VALID);
  229. }
  230. /* setup one handler for each entry */
  231. irq_set_chained_handler(port[i].irq, mxs_gpio_irq_handler);
  232. irq_set_handler_data(port[i].irq, &port[i]);
  233. /* register gpio chip */
  234. port[i].chip.direction_input = mxs_gpio_direction_input;
  235. port[i].chip.direction_output = mxs_gpio_direction_output;
  236. port[i].chip.get = mxs_gpio_get;
  237. port[i].chip.set = mxs_gpio_set;
  238. port[i].chip.to_irq = mxs_gpio_to_irq;
  239. port[i].chip.base = i * 32;
  240. port[i].chip.ngpio = 32;
  241. /* its a serious configuration bug when it fails */
  242. BUG_ON(gpiochip_add(&port[i].chip) < 0);
  243. }
  244. return 0;
  245. }
  246. #define MX23_GPIO_BASE MX23_IO_ADDRESS(MX23_PINCTRL_BASE_ADDR)
  247. #define MX28_GPIO_BASE MX28_IO_ADDRESS(MX28_PINCTRL_BASE_ADDR)
  248. #define DEFINE_MXS_GPIO_PORT(_base, _irq, _id) \
  249. { \
  250. .chip.label = "gpio-" #_id, \
  251. .id = _id, \
  252. .irq = _irq, \
  253. .base = _base, \
  254. .virtual_irq_start = MXS_GPIO_IRQ_START + (_id) * 32, \
  255. }
  256. #ifdef CONFIG_SOC_IMX23
  257. static struct mxs_gpio_port mx23_gpio_ports[] = {
  258. DEFINE_MXS_GPIO_PORT(MX23_GPIO_BASE, MX23_INT_GPIO0, 0),
  259. DEFINE_MXS_GPIO_PORT(MX23_GPIO_BASE, MX23_INT_GPIO1, 1),
  260. DEFINE_MXS_GPIO_PORT(MX23_GPIO_BASE, MX23_INT_GPIO2, 2),
  261. };
  262. int __init mx23_register_gpios(void)
  263. {
  264. return mxs_gpio_init(mx23_gpio_ports, ARRAY_SIZE(mx23_gpio_ports));
  265. }
  266. #endif
  267. #ifdef CONFIG_SOC_IMX28
  268. static struct mxs_gpio_port mx28_gpio_ports[] = {
  269. DEFINE_MXS_GPIO_PORT(MX28_GPIO_BASE, MX28_INT_GPIO0, 0),
  270. DEFINE_MXS_GPIO_PORT(MX28_GPIO_BASE, MX28_INT_GPIO1, 1),
  271. DEFINE_MXS_GPIO_PORT(MX28_GPIO_BASE, MX28_INT_GPIO2, 2),
  272. DEFINE_MXS_GPIO_PORT(MX28_GPIO_BASE, MX28_INT_GPIO3, 3),
  273. DEFINE_MXS_GPIO_PORT(MX28_GPIO_BASE, MX28_INT_GPIO4, 4),
  274. };
  275. int __init mx28_register_gpios(void)
  276. {
  277. return mxs_gpio_init(mx28_gpio_ports, ARRAY_SIZE(mx28_gpio_ports));
  278. }
  279. #endif