gpio.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Gemini gpiochip and interrupt routines
  3. *
  4. * Copyright (C) 2008-2009 Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
  5. *
  6. * Based on plat-mxc/gpio.c:
  7. * MXC GPIO support. (c) 2008 Daniel Mack <daniel@caiaq.de>
  8. * Copyright 2008 Juergen Beisert, kernel@pengutronix.de
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/io.h>
  18. #include <linux/irq.h>
  19. #include <linux/gpio/driver.h>
  20. #include <mach/hardware.h>
  21. #include <mach/irqs.h>
  22. #define GPIO_BASE(x) IO_ADDRESS(GEMINI_GPIO_BASE(x))
  23. #define irq_to_gpio(x) ((x) - GPIO_IRQ_BASE)
  24. /* GPIO registers definition */
  25. #define GPIO_DATA_OUT 0x0
  26. #define GPIO_DATA_IN 0x4
  27. #define GPIO_DIR 0x8
  28. #define GPIO_DATA_SET 0x10
  29. #define GPIO_DATA_CLR 0x14
  30. #define GPIO_PULL_EN 0x18
  31. #define GPIO_PULL_TYPE 0x1C
  32. #define GPIO_INT_EN 0x20
  33. #define GPIO_INT_STAT 0x24
  34. #define GPIO_INT_MASK 0x2C
  35. #define GPIO_INT_CLR 0x30
  36. #define GPIO_INT_TYPE 0x34
  37. #define GPIO_INT_BOTH_EDGE 0x38
  38. #define GPIO_INT_LEVEL 0x3C
  39. #define GPIO_DEBOUNCE_EN 0x40
  40. #define GPIO_DEBOUNCE_PRESCALE 0x44
  41. #define GPIO_PORT_NUM 3
  42. static void _set_gpio_irqenable(void __iomem *base, unsigned int index,
  43. int enable)
  44. {
  45. unsigned int reg;
  46. reg = __raw_readl(base + GPIO_INT_EN);
  47. reg = (reg & (~(1 << index))) | (!!enable << index);
  48. __raw_writel(reg, base + GPIO_INT_EN);
  49. }
  50. static void gpio_ack_irq(struct irq_data *d)
  51. {
  52. unsigned int gpio = irq_to_gpio(d->irq);
  53. void __iomem *base = GPIO_BASE(gpio / 32);
  54. __raw_writel(1 << (gpio % 32), base + GPIO_INT_CLR);
  55. }
  56. static void gpio_mask_irq(struct irq_data *d)
  57. {
  58. unsigned int gpio = irq_to_gpio(d->irq);
  59. void __iomem *base = GPIO_BASE(gpio / 32);
  60. _set_gpio_irqenable(base, gpio % 32, 0);
  61. }
  62. static void gpio_unmask_irq(struct irq_data *d)
  63. {
  64. unsigned int gpio = irq_to_gpio(d->irq);
  65. void __iomem *base = GPIO_BASE(gpio / 32);
  66. _set_gpio_irqenable(base, gpio % 32, 1);
  67. }
  68. static int gpio_set_irq_type(struct irq_data *d, unsigned int type)
  69. {
  70. unsigned int gpio = irq_to_gpio(d->irq);
  71. unsigned int gpio_mask = 1 << (gpio % 32);
  72. void __iomem *base = GPIO_BASE(gpio / 32);
  73. unsigned int reg_both, reg_level, reg_type;
  74. reg_type = __raw_readl(base + GPIO_INT_TYPE);
  75. reg_level = __raw_readl(base + GPIO_INT_LEVEL);
  76. reg_both = __raw_readl(base + GPIO_INT_BOTH_EDGE);
  77. switch (type) {
  78. case IRQ_TYPE_EDGE_BOTH:
  79. reg_type &= ~gpio_mask;
  80. reg_both |= gpio_mask;
  81. break;
  82. case IRQ_TYPE_EDGE_RISING:
  83. reg_type &= ~gpio_mask;
  84. reg_both &= ~gpio_mask;
  85. reg_level &= ~gpio_mask;
  86. break;
  87. case IRQ_TYPE_EDGE_FALLING:
  88. reg_type &= ~gpio_mask;
  89. reg_both &= ~gpio_mask;
  90. reg_level |= gpio_mask;
  91. break;
  92. case IRQ_TYPE_LEVEL_HIGH:
  93. reg_type |= gpio_mask;
  94. reg_level &= ~gpio_mask;
  95. break;
  96. case IRQ_TYPE_LEVEL_LOW:
  97. reg_type |= gpio_mask;
  98. reg_level |= gpio_mask;
  99. break;
  100. default:
  101. return -EINVAL;
  102. }
  103. __raw_writel(reg_type, base + GPIO_INT_TYPE);
  104. __raw_writel(reg_level, base + GPIO_INT_LEVEL);
  105. __raw_writel(reg_both, base + GPIO_INT_BOTH_EDGE);
  106. gpio_ack_irq(d);
  107. return 0;
  108. }
  109. static void gpio_irq_handler(struct irq_desc *desc)
  110. {
  111. unsigned int port = (unsigned int)irq_desc_get_handler_data(desc);
  112. unsigned int gpio_irq_no, irq_stat;
  113. irq_stat = __raw_readl(GPIO_BASE(port) + GPIO_INT_STAT);
  114. gpio_irq_no = GPIO_IRQ_BASE + port * 32;
  115. for (; irq_stat != 0; irq_stat >>= 1, gpio_irq_no++) {
  116. if ((irq_stat & 1) == 0)
  117. continue;
  118. generic_handle_irq(gpio_irq_no);
  119. }
  120. }
  121. static struct irq_chip gpio_irq_chip = {
  122. .name = "GPIO",
  123. .irq_ack = gpio_ack_irq,
  124. .irq_mask = gpio_mask_irq,
  125. .irq_unmask = gpio_unmask_irq,
  126. .irq_set_type = gpio_set_irq_type,
  127. };
  128. static void _set_gpio_direction(struct gpio_chip *chip, unsigned offset,
  129. int dir)
  130. {
  131. void __iomem *base = GPIO_BASE(offset / 32);
  132. unsigned int reg;
  133. reg = __raw_readl(base + GPIO_DIR);
  134. if (dir)
  135. reg |= 1 << (offset % 32);
  136. else
  137. reg &= ~(1 << (offset % 32));
  138. __raw_writel(reg, base + GPIO_DIR);
  139. }
  140. static void gemini_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  141. {
  142. void __iomem *base = GPIO_BASE(offset / 32);
  143. if (value)
  144. __raw_writel(1 << (offset % 32), base + GPIO_DATA_SET);
  145. else
  146. __raw_writel(1 << (offset % 32), base + GPIO_DATA_CLR);
  147. }
  148. static int gemini_gpio_get(struct gpio_chip *chip, unsigned offset)
  149. {
  150. void __iomem *base = GPIO_BASE(offset / 32);
  151. return (__raw_readl(base + GPIO_DATA_IN) >> (offset % 32)) & 1;
  152. }
  153. static int gemini_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  154. {
  155. _set_gpio_direction(chip, offset, 0);
  156. return 0;
  157. }
  158. static int gemini_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  159. int value)
  160. {
  161. _set_gpio_direction(chip, offset, 1);
  162. gemini_gpio_set(chip, offset, value);
  163. return 0;
  164. }
  165. static struct gpio_chip gemini_gpio_chip = {
  166. .label = "Gemini",
  167. .direction_input = gemini_gpio_direction_input,
  168. .get = gemini_gpio_get,
  169. .direction_output = gemini_gpio_direction_output,
  170. .set = gemini_gpio_set,
  171. .base = 0,
  172. .ngpio = GPIO_PORT_NUM * 32,
  173. };
  174. void __init gemini_gpio_init(void)
  175. {
  176. int i, j;
  177. for (i = 0; i < GPIO_PORT_NUM; i++) {
  178. /* disable, unmask and clear all interrupts */
  179. __raw_writel(0x0, GPIO_BASE(i) + GPIO_INT_EN);
  180. __raw_writel(0x0, GPIO_BASE(i) + GPIO_INT_MASK);
  181. __raw_writel(~0x0, GPIO_BASE(i) + GPIO_INT_CLR);
  182. for (j = GPIO_IRQ_BASE + i * 32;
  183. j < GPIO_IRQ_BASE + (i + 1) * 32; j++) {
  184. irq_set_chip_and_handler(j, &gpio_irq_chip,
  185. handle_edge_irq);
  186. irq_clear_status_flags(j, IRQ_NOREQUEST);
  187. }
  188. irq_set_chained_handler_and_data(IRQ_GPIO(i), gpio_irq_handler,
  189. (void *)i);
  190. }
  191. BUG_ON(gpiochip_add_data(&gemini_gpio_chip, NULL));
  192. }