gpio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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, MA 02110-1301, USA.
  20. */
  21. #include <linux/init.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/io.h>
  24. #include <linux/irq.h>
  25. #include <linux/gpio.h>
  26. #include <mach/hardware.h>
  27. #include <asm-generic/bug.h>
  28. static struct mxc_gpio_port *mxc_gpio_ports;
  29. static int gpio_table_size;
  30. #define cpu_is_mx1_mx2() (cpu_is_mx1() || cpu_is_mx2())
  31. #define GPIO_DR (cpu_is_mx1_mx2() ? 0x1c : 0x00)
  32. #define GPIO_GDIR (cpu_is_mx1_mx2() ? 0x00 : 0x04)
  33. #define GPIO_PSR (cpu_is_mx1_mx2() ? 0x24 : 0x08)
  34. #define GPIO_ICR1 (cpu_is_mx1_mx2() ? 0x28 : 0x0C)
  35. #define GPIO_ICR2 (cpu_is_mx1_mx2() ? 0x2C : 0x10)
  36. #define GPIO_IMR (cpu_is_mx1_mx2() ? 0x30 : 0x14)
  37. #define GPIO_ISR (cpu_is_mx1_mx2() ? 0x34 : 0x18)
  38. #define GPIO_INT_LOW_LEV (cpu_is_mx1_mx2() ? 0x3 : 0x0)
  39. #define GPIO_INT_HIGH_LEV (cpu_is_mx1_mx2() ? 0x2 : 0x1)
  40. #define GPIO_INT_RISE_EDGE (cpu_is_mx1_mx2() ? 0x0 : 0x2)
  41. #define GPIO_INT_FALL_EDGE (cpu_is_mx1_mx2() ? 0x1 : 0x3)
  42. #define GPIO_INT_NONE 0x4
  43. /* Note: This driver assumes 32 GPIOs are handled in one register */
  44. static void _clear_gpio_irqstatus(struct mxc_gpio_port *port, u32 index)
  45. {
  46. __raw_writel(1 << index, port->base + GPIO_ISR);
  47. }
  48. static void _set_gpio_irqenable(struct mxc_gpio_port *port, u32 index,
  49. int enable)
  50. {
  51. u32 l;
  52. l = __raw_readl(port->base + GPIO_IMR);
  53. l = (l & (~(1 << index))) | (!!enable << index);
  54. __raw_writel(l, port->base + GPIO_IMR);
  55. }
  56. static void gpio_ack_irq(struct irq_data *d)
  57. {
  58. u32 gpio = irq_to_gpio(d->irq);
  59. _clear_gpio_irqstatus(&mxc_gpio_ports[gpio / 32], gpio & 0x1f);
  60. }
  61. static void gpio_mask_irq(struct irq_data *d)
  62. {
  63. u32 gpio = irq_to_gpio(d->irq);
  64. _set_gpio_irqenable(&mxc_gpio_ports[gpio / 32], gpio & 0x1f, 0);
  65. }
  66. static void gpio_unmask_irq(struct irq_data *d)
  67. {
  68. u32 gpio = irq_to_gpio(d->irq);
  69. _set_gpio_irqenable(&mxc_gpio_ports[gpio / 32], gpio & 0x1f, 1);
  70. }
  71. static int mxc_gpio_get(struct gpio_chip *chip, unsigned offset);
  72. static int gpio_set_irq_type(struct irq_data *d, u32 type)
  73. {
  74. u32 gpio = irq_to_gpio(d->irq);
  75. struct mxc_gpio_port *port = &mxc_gpio_ports[gpio / 32];
  76. u32 bit, val;
  77. int edge;
  78. void __iomem *reg = port->base;
  79. port->both_edges &= ~(1 << (gpio & 31));
  80. switch (type) {
  81. case IRQ_TYPE_EDGE_RISING:
  82. edge = GPIO_INT_RISE_EDGE;
  83. break;
  84. case IRQ_TYPE_EDGE_FALLING:
  85. edge = GPIO_INT_FALL_EDGE;
  86. break;
  87. case IRQ_TYPE_EDGE_BOTH:
  88. val = mxc_gpio_get(&port->chip, gpio & 31);
  89. if (val) {
  90. edge = GPIO_INT_LOW_LEV;
  91. pr_debug("mxc: set GPIO %d to low trigger\n", gpio);
  92. } else {
  93. edge = GPIO_INT_HIGH_LEV;
  94. pr_debug("mxc: set GPIO %d to high trigger\n", gpio);
  95. }
  96. port->both_edges |= 1 << (gpio & 31);
  97. break;
  98. case IRQ_TYPE_LEVEL_LOW:
  99. edge = GPIO_INT_LOW_LEV;
  100. break;
  101. case IRQ_TYPE_LEVEL_HIGH:
  102. edge = GPIO_INT_HIGH_LEV;
  103. break;
  104. default:
  105. return -EINVAL;
  106. }
  107. reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
  108. bit = gpio & 0xf;
  109. val = __raw_readl(reg) & ~(0x3 << (bit << 1));
  110. __raw_writel(val | (edge << (bit << 1)), reg);
  111. _clear_gpio_irqstatus(port, gpio & 0x1f);
  112. return 0;
  113. }
  114. static void mxc_flip_edge(struct mxc_gpio_port *port, u32 gpio)
  115. {
  116. void __iomem *reg = port->base;
  117. u32 bit, val;
  118. int edge;
  119. reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
  120. bit = gpio & 0xf;
  121. val = __raw_readl(reg);
  122. edge = (val >> (bit << 1)) & 3;
  123. val &= ~(0x3 << (bit << 1));
  124. if (edge == GPIO_INT_HIGH_LEV) {
  125. edge = GPIO_INT_LOW_LEV;
  126. pr_debug("mxc: switch GPIO %d to low trigger\n", gpio);
  127. } else if (edge == GPIO_INT_LOW_LEV) {
  128. edge = GPIO_INT_HIGH_LEV;
  129. pr_debug("mxc: switch GPIO %d to high trigger\n", gpio);
  130. } else {
  131. pr_err("mxc: invalid configuration for GPIO %d: %x\n",
  132. gpio, edge);
  133. return;
  134. }
  135. __raw_writel(val | (edge << (bit << 1)), reg);
  136. }
  137. /* handle 32 interrupts in one status register */
  138. static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat)
  139. {
  140. u32 gpio_irq_no_base = port->virtual_irq_start;
  141. while (irq_stat != 0) {
  142. int irqoffset = fls(irq_stat) - 1;
  143. if (port->both_edges & (1 << irqoffset))
  144. mxc_flip_edge(port, irqoffset);
  145. generic_handle_irq(gpio_irq_no_base + irqoffset);
  146. irq_stat &= ~(1 << irqoffset);
  147. }
  148. }
  149. /* MX1 and MX3 has one interrupt *per* gpio port */
  150. static void mx3_gpio_irq_handler(u32 irq, struct irq_desc *desc)
  151. {
  152. u32 irq_stat;
  153. struct mxc_gpio_port *port = get_irq_data(irq);
  154. irq_stat = __raw_readl(port->base + GPIO_ISR) &
  155. __raw_readl(port->base + GPIO_IMR);
  156. mxc_gpio_irq_handler(port, irq_stat);
  157. }
  158. /* MX2 has one interrupt *for all* gpio ports */
  159. static void mx2_gpio_irq_handler(u32 irq, struct irq_desc *desc)
  160. {
  161. int i;
  162. u32 irq_msk, irq_stat;
  163. struct mxc_gpio_port *port = get_irq_data(irq);
  164. /* walk through all interrupt status registers */
  165. for (i = 0; i < gpio_table_size; i++) {
  166. irq_msk = __raw_readl(port[i].base + GPIO_IMR);
  167. if (!irq_msk)
  168. continue;
  169. irq_stat = __raw_readl(port[i].base + GPIO_ISR) & irq_msk;
  170. if (irq_stat)
  171. mxc_gpio_irq_handler(&port[i], irq_stat);
  172. }
  173. }
  174. /*
  175. * Set interrupt number "irq" in the GPIO as a wake-up source.
  176. * While system is running, all registered GPIO interrupts need to have
  177. * wake-up enabled. When system is suspended, only selected GPIO interrupts
  178. * need to have wake-up enabled.
  179. * @param irq interrupt source number
  180. * @param enable enable as wake-up if equal to non-zero
  181. * @return This function returns 0 on success.
  182. */
  183. static int gpio_set_wake_irq(struct irq_data *d, u32 enable)
  184. {
  185. u32 gpio = irq_to_gpio(d->irq);
  186. u32 gpio_idx = gpio & 0x1F;
  187. struct mxc_gpio_port *port = &mxc_gpio_ports[gpio / 32];
  188. if (enable) {
  189. if (port->irq_high && (gpio_idx >= 16))
  190. enable_irq_wake(port->irq_high);
  191. else
  192. enable_irq_wake(port->irq);
  193. } else {
  194. if (port->irq_high && (gpio_idx >= 16))
  195. disable_irq_wake(port->irq_high);
  196. else
  197. disable_irq_wake(port->irq);
  198. }
  199. return 0;
  200. }
  201. static struct irq_chip gpio_irq_chip = {
  202. .irq_ack = gpio_ack_irq,
  203. .irq_mask = gpio_mask_irq,
  204. .irq_unmask = gpio_unmask_irq,
  205. .irq_set_type = gpio_set_irq_type,
  206. .irq_set_wake = gpio_set_wake_irq,
  207. };
  208. static void _set_gpio_direction(struct gpio_chip *chip, unsigned offset,
  209. int dir)
  210. {
  211. struct mxc_gpio_port *port =
  212. container_of(chip, struct mxc_gpio_port, chip);
  213. u32 l;
  214. unsigned long flags;
  215. spin_lock_irqsave(&port->lock, flags);
  216. l = __raw_readl(port->base + GPIO_GDIR);
  217. if (dir)
  218. l |= 1 << offset;
  219. else
  220. l &= ~(1 << offset);
  221. __raw_writel(l, port->base + GPIO_GDIR);
  222. spin_unlock_irqrestore(&port->lock, flags);
  223. }
  224. static void mxc_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  225. {
  226. struct mxc_gpio_port *port =
  227. container_of(chip, struct mxc_gpio_port, chip);
  228. void __iomem *reg = port->base + GPIO_DR;
  229. u32 l;
  230. unsigned long flags;
  231. spin_lock_irqsave(&port->lock, flags);
  232. l = (__raw_readl(reg) & (~(1 << offset))) | (!!value << offset);
  233. __raw_writel(l, reg);
  234. spin_unlock_irqrestore(&port->lock, flags);
  235. }
  236. static int mxc_gpio_get(struct gpio_chip *chip, unsigned offset)
  237. {
  238. struct mxc_gpio_port *port =
  239. container_of(chip, struct mxc_gpio_port, chip);
  240. return (__raw_readl(port->base + GPIO_PSR) >> offset) & 1;
  241. }
  242. static int mxc_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  243. {
  244. _set_gpio_direction(chip, offset, 0);
  245. return 0;
  246. }
  247. static int mxc_gpio_direction_output(struct gpio_chip *chip,
  248. unsigned offset, int value)
  249. {
  250. mxc_gpio_set(chip, offset, value);
  251. _set_gpio_direction(chip, offset, 1);
  252. return 0;
  253. }
  254. int __init mxc_gpio_init(struct mxc_gpio_port *port, int cnt)
  255. {
  256. int i, j;
  257. /* save for local usage */
  258. mxc_gpio_ports = port;
  259. gpio_table_size = cnt;
  260. printk(KERN_INFO "MXC GPIO hardware\n");
  261. for (i = 0; i < cnt; i++) {
  262. /* disable the interrupt and clear the status */
  263. __raw_writel(0, port[i].base + GPIO_IMR);
  264. __raw_writel(~0, port[i].base + GPIO_ISR);
  265. for (j = port[i].virtual_irq_start;
  266. j < port[i].virtual_irq_start + 32; j++) {
  267. set_irq_chip(j, &gpio_irq_chip);
  268. set_irq_handler(j, handle_level_irq);
  269. set_irq_flags(j, IRQF_VALID);
  270. }
  271. /* register gpio chip */
  272. port[i].chip.direction_input = mxc_gpio_direction_input;
  273. port[i].chip.direction_output = mxc_gpio_direction_output;
  274. port[i].chip.get = mxc_gpio_get;
  275. port[i].chip.set = mxc_gpio_set;
  276. port[i].chip.base = i * 32;
  277. port[i].chip.ngpio = 32;
  278. spin_lock_init(&port[i].lock);
  279. /* its a serious configuration bug when it fails */
  280. BUG_ON( gpiochip_add(&port[i].chip) < 0 );
  281. if (cpu_is_mx1() || cpu_is_mx3() || cpu_is_mx25() || cpu_is_mx51()) {
  282. /* setup one handler for each entry */
  283. set_irq_chained_handler(port[i].irq, mx3_gpio_irq_handler);
  284. set_irq_data(port[i].irq, &port[i]);
  285. if (port[i].irq_high) {
  286. /* setup handler for GPIO 16 to 31 */
  287. set_irq_chained_handler(port[i].irq_high,
  288. mx3_gpio_irq_handler);
  289. set_irq_data(port[i].irq_high, &port[i]);
  290. }
  291. }
  292. }
  293. if (cpu_is_mx2()) {
  294. /* setup one handler for all GPIO interrupts */
  295. set_irq_chained_handler(port[0].irq, mx2_gpio_irq_handler);
  296. set_irq_data(port[0].irq, port);
  297. }
  298. return 0;
  299. }
  300. #define DEFINE_IMX_GPIO_PORT_IRQ_HIGH(soc, _id, _hwid, _irq, _irq_high) \
  301. { \
  302. .chip.label = "gpio-" #_id, \
  303. .irq = _irq, \
  304. .irq_high = _irq_high, \
  305. .base = soc ## _IO_ADDRESS( \
  306. soc ## _GPIO ## _hwid ## _BASE_ADDR), \
  307. .virtual_irq_start = MXC_GPIO_IRQ_START + (_id) * 32, \
  308. }
  309. #define DEFINE_IMX_GPIO_PORT_IRQ(soc, _id, _hwid, _irq) \
  310. DEFINE_IMX_GPIO_PORT_IRQ_HIGH(soc, _id, _hwid, _irq, 0)
  311. #define DEFINE_IMX_GPIO_PORT(soc, _id, _hwid) \
  312. DEFINE_IMX_GPIO_PORT_IRQ(soc, _id, _hwid, 0)
  313. #define DEFINE_REGISTER_FUNCTION(prefix) \
  314. int __init prefix ## _register_gpios(void) \
  315. { \
  316. return mxc_gpio_init(prefix ## _gpio_ports, \
  317. ARRAY_SIZE(prefix ## _gpio_ports)); \
  318. }
  319. #if defined(CONFIG_SOC_IMX1)
  320. static struct mxc_gpio_port imx1_gpio_ports[] = {
  321. DEFINE_IMX_GPIO_PORT_IRQ(MX1, 0, 1, MX1_GPIO_INT_PORTA),
  322. DEFINE_IMX_GPIO_PORT_IRQ(MX1, 1, 2, MX1_GPIO_INT_PORTB),
  323. DEFINE_IMX_GPIO_PORT_IRQ(MX1, 2, 3, MX1_GPIO_INT_PORTC),
  324. DEFINE_IMX_GPIO_PORT_IRQ(MX1, 3, 4, MX1_GPIO_INT_PORTD),
  325. };
  326. DEFINE_REGISTER_FUNCTION(imx1)
  327. #endif /* if defined(CONFIG_SOC_IMX1) */
  328. #if defined(CONFIG_SOC_IMX21)
  329. static struct mxc_gpio_port imx21_gpio_ports[] = {
  330. DEFINE_IMX_GPIO_PORT_IRQ(MX21, 0, 1, MX21_INT_GPIO),
  331. DEFINE_IMX_GPIO_PORT(MX21, 1, 2),
  332. DEFINE_IMX_GPIO_PORT(MX21, 2, 3),
  333. DEFINE_IMX_GPIO_PORT(MX21, 3, 4),
  334. DEFINE_IMX_GPIO_PORT(MX21, 4, 5),
  335. DEFINE_IMX_GPIO_PORT(MX21, 5, 6),
  336. };
  337. DEFINE_REGISTER_FUNCTION(imx21)
  338. #endif /* if defined(CONFIG_SOC_IMX21) */
  339. #if defined(CONFIG_SOC_IMX25)
  340. static struct mxc_gpio_port imx25_gpio_ports[] = {
  341. DEFINE_IMX_GPIO_PORT_IRQ(MX25, 0, 1, MX25_INT_GPIO1),
  342. DEFINE_IMX_GPIO_PORT_IRQ(MX25, 1, 2, MX25_INT_GPIO2),
  343. DEFINE_IMX_GPIO_PORT_IRQ(MX25, 2, 3, MX25_INT_GPIO3),
  344. DEFINE_IMX_GPIO_PORT_IRQ(MX25, 3, 4, MX25_INT_GPIO4),
  345. };
  346. DEFINE_REGISTER_FUNCTION(imx25)
  347. #endif /* if defined(CONFIG_SOC_IMX25) */
  348. #if defined(CONFIG_SOC_IMX27)
  349. static struct mxc_gpio_port imx27_gpio_ports[] = {
  350. DEFINE_IMX_GPIO_PORT_IRQ(MX27, 0, 1, MX27_INT_GPIO),
  351. DEFINE_IMX_GPIO_PORT(MX27, 1, 2),
  352. DEFINE_IMX_GPIO_PORT(MX27, 2, 3),
  353. DEFINE_IMX_GPIO_PORT(MX27, 3, 4),
  354. DEFINE_IMX_GPIO_PORT(MX27, 4, 5),
  355. DEFINE_IMX_GPIO_PORT(MX27, 5, 6),
  356. };
  357. DEFINE_REGISTER_FUNCTION(imx27)
  358. #endif /* if defined(CONFIG_SOC_IMX27) */
  359. #if defined(CONFIG_SOC_IMX31)
  360. static struct mxc_gpio_port imx31_gpio_ports[] = {
  361. DEFINE_IMX_GPIO_PORT_IRQ(MX31, 0, 1, MX31_INT_GPIO1),
  362. DEFINE_IMX_GPIO_PORT_IRQ(MX31, 1, 2, MX31_INT_GPIO2),
  363. DEFINE_IMX_GPIO_PORT_IRQ(MX31, 2, 3, MX31_INT_GPIO3),
  364. };
  365. DEFINE_REGISTER_FUNCTION(imx31)
  366. #endif /* if defined(CONFIG_SOC_IMX31) */
  367. #if defined(CONFIG_SOC_IMX35)
  368. static struct mxc_gpio_port imx35_gpio_ports[] = {
  369. DEFINE_IMX_GPIO_PORT_IRQ(MX35, 0, 1, MX35_INT_GPIO1),
  370. DEFINE_IMX_GPIO_PORT_IRQ(MX35, 1, 2, MX35_INT_GPIO2),
  371. DEFINE_IMX_GPIO_PORT_IRQ(MX35, 2, 3, MX35_INT_GPIO3),
  372. };
  373. DEFINE_REGISTER_FUNCTION(imx35)
  374. #endif /* if defined(CONFIG_SOC_IMX35) */
  375. #if defined(CONFIG_SOC_IMX50)
  376. static struct mxc_gpio_port imx50_gpio_ports[] = {
  377. DEFINE_IMX_GPIO_PORT_IRQ_HIGH(MX50, 0, 1, MX50_INT_GPIO1_LOW, MX50_INT_GPIO1_HIGH),
  378. DEFINE_IMX_GPIO_PORT_IRQ_HIGH(MX50, 1, 2, MX50_INT_GPIO2_LOW, MX50_INT_GPIO2_HIGH),
  379. DEFINE_IMX_GPIO_PORT_IRQ_HIGH(MX50, 2, 3, MX50_INT_GPIO3_LOW, MX50_INT_GPIO3_HIGH),
  380. DEFINE_IMX_GPIO_PORT_IRQ_HIGH(MX50, 3, 4, MX50_INT_GPIO3_LOW, MX50_INT_GPIO3_HIGH),
  381. DEFINE_IMX_GPIO_PORT_IRQ_HIGH(MX50, 4, 5, MX50_INT_GPIO3_LOW, MX50_INT_GPIO3_HIGH),
  382. DEFINE_IMX_GPIO_PORT_IRQ_HIGH(MX50, 5, 6, MX50_INT_GPIO3_LOW, MX50_INT_GPIO3_HIGH),
  383. };
  384. DEFINE_REGISTER_FUNCTION(imx50)
  385. #endif /* if defined(CONFIG_SOC_IMX50) */