gpio.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * arch/arm/mach-tegra/gpio.c
  3. *
  4. * Copyright (c) 2010 Google, Inc
  5. *
  6. * Author:
  7. * Erik Gilling <konkers@google.com>
  8. *
  9. * This software is licensed under the terms of the GNU General Public
  10. * License version 2, as published by the Free Software Foundation, and
  11. * may be copied, distributed, and modified under those terms.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. */
  19. #include <linux/init.h>
  20. #include <linux/irq.h>
  21. #include <linux/io.h>
  22. #include <linux/gpio.h>
  23. #include <mach/iomap.h>
  24. #define GPIO_BANK(x) ((x) >> 5)
  25. #define GPIO_PORT(x) (((x) >> 3) & 0x3)
  26. #define GPIO_BIT(x) ((x) & 0x7)
  27. #define GPIO_REG(x) (IO_TO_VIRT(TEGRA_GPIO_BASE) + \
  28. GPIO_BANK(x) * 0x80 + \
  29. GPIO_PORT(x) * 4)
  30. #define GPIO_CNF(x) (GPIO_REG(x) + 0x00)
  31. #define GPIO_OE(x) (GPIO_REG(x) + 0x10)
  32. #define GPIO_OUT(x) (GPIO_REG(x) + 0X20)
  33. #define GPIO_IN(x) (GPIO_REG(x) + 0x30)
  34. #define GPIO_INT_STA(x) (GPIO_REG(x) + 0x40)
  35. #define GPIO_INT_ENB(x) (GPIO_REG(x) + 0x50)
  36. #define GPIO_INT_LVL(x) (GPIO_REG(x) + 0x60)
  37. #define GPIO_INT_CLR(x) (GPIO_REG(x) + 0x70)
  38. #define GPIO_MSK_CNF(x) (GPIO_REG(x) + 0x800)
  39. #define GPIO_MSK_OE(x) (GPIO_REG(x) + 0x810)
  40. #define GPIO_MSK_OUT(x) (GPIO_REG(x) + 0X820)
  41. #define GPIO_MSK_INT_STA(x) (GPIO_REG(x) + 0x840)
  42. #define GPIO_MSK_INT_ENB(x) (GPIO_REG(x) + 0x850)
  43. #define GPIO_MSK_INT_LVL(x) (GPIO_REG(x) + 0x860)
  44. #define GPIO_INT_LVL_MASK 0x010101
  45. #define GPIO_INT_LVL_EDGE_RISING 0x000101
  46. #define GPIO_INT_LVL_EDGE_FALLING 0x000100
  47. #define GPIO_INT_LVL_EDGE_BOTH 0x010100
  48. #define GPIO_INT_LVL_LEVEL_HIGH 0x000001
  49. #define GPIO_INT_LVL_LEVEL_LOW 0x000000
  50. struct tegra_gpio_bank {
  51. int bank;
  52. int irq;
  53. spinlock_t lvl_lock[4];
  54. };
  55. static struct tegra_gpio_bank tegra_gpio_banks[] = {
  56. {.bank = 0, .irq = INT_GPIO1},
  57. {.bank = 1, .irq = INT_GPIO2},
  58. {.bank = 2, .irq = INT_GPIO3},
  59. {.bank = 3, .irq = INT_GPIO4},
  60. {.bank = 4, .irq = INT_GPIO5},
  61. {.bank = 5, .irq = INT_GPIO6},
  62. {.bank = 6, .irq = INT_GPIO7},
  63. };
  64. static int tegra_gpio_compose(int bank, int port, int bit)
  65. {
  66. return (bank << 5) | ((port & 0x3) << 3) | (bit & 0x7);
  67. }
  68. static void tegra_gpio_mask_write(u32 reg, int gpio, int value)
  69. {
  70. u32 val;
  71. val = 0x100 << GPIO_BIT(gpio);
  72. if (value)
  73. val |= 1 << GPIO_BIT(gpio);
  74. __raw_writel(val, reg);
  75. }
  76. void tegra_gpio_enable(int gpio)
  77. {
  78. tegra_gpio_mask_write(GPIO_MSK_CNF(gpio), gpio, 1);
  79. }
  80. void tegra_gpio_disable(int gpio)
  81. {
  82. tegra_gpio_mask_write(GPIO_MSK_CNF(gpio), gpio, 0);
  83. }
  84. static void tegra_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  85. {
  86. tegra_gpio_mask_write(GPIO_MSK_OUT(offset), offset, value);
  87. }
  88. static int tegra_gpio_get(struct gpio_chip *chip, unsigned offset)
  89. {
  90. return (__raw_readl(GPIO_IN(offset)) >> GPIO_BIT(offset)) & 0x1;
  91. }
  92. static int tegra_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  93. {
  94. tegra_gpio_mask_write(GPIO_MSK_OE(offset), offset, 0);
  95. return 0;
  96. }
  97. static int tegra_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  98. int value)
  99. {
  100. tegra_gpio_set(chip, offset, value);
  101. tegra_gpio_mask_write(GPIO_MSK_OE(offset), offset, 1);
  102. return 0;
  103. }
  104. static struct gpio_chip tegra_gpio_chip = {
  105. .label = "tegra-gpio",
  106. .direction_input = tegra_gpio_direction_input,
  107. .get = tegra_gpio_get,
  108. .direction_output = tegra_gpio_direction_output,
  109. .set = tegra_gpio_set,
  110. .base = 0,
  111. .ngpio = ARCH_NR_GPIOS,
  112. };
  113. static void tegra_gpio_irq_ack(unsigned int irq)
  114. {
  115. int gpio = irq - INT_GPIO_BASE;
  116. __raw_writel(1 << GPIO_BIT(gpio), GPIO_INT_CLR(gpio));
  117. }
  118. static void tegra_gpio_irq_mask(unsigned int irq)
  119. {
  120. int gpio = irq - INT_GPIO_BASE;
  121. tegra_gpio_mask_write(GPIO_MSK_INT_ENB(gpio), gpio, 0);
  122. }
  123. static void tegra_gpio_irq_unmask(unsigned int irq)
  124. {
  125. int gpio = irq - INT_GPIO_BASE;
  126. tegra_gpio_mask_write(GPIO_MSK_INT_ENB(gpio), gpio, 1);
  127. }
  128. static int tegra_gpio_irq_set_type(unsigned int irq, unsigned int type)
  129. {
  130. int gpio = irq - INT_GPIO_BASE;
  131. struct tegra_gpio_bank *bank = get_irq_chip_data(irq);
  132. int port = GPIO_PORT(gpio);
  133. int lvl_type;
  134. int val;
  135. unsigned long flags;
  136. switch (type & IRQ_TYPE_SENSE_MASK) {
  137. case IRQ_TYPE_EDGE_RISING:
  138. lvl_type = GPIO_INT_LVL_EDGE_RISING;
  139. break;
  140. case IRQ_TYPE_EDGE_FALLING:
  141. lvl_type = GPIO_INT_LVL_EDGE_FALLING;
  142. break;
  143. case IRQ_TYPE_EDGE_BOTH:
  144. lvl_type = GPIO_INT_LVL_EDGE_BOTH;
  145. break;
  146. case IRQ_TYPE_LEVEL_HIGH:
  147. lvl_type = GPIO_INT_LVL_LEVEL_HIGH;
  148. break;
  149. case IRQ_TYPE_LEVEL_LOW:
  150. lvl_type = GPIO_INT_LVL_LEVEL_LOW;
  151. break;
  152. default:
  153. return -EINVAL;
  154. }
  155. spin_lock_irqsave(&bank->lvl_lock[port], flags);
  156. val = __raw_readl(GPIO_INT_LVL(gpio));
  157. val &= ~(GPIO_INT_LVL_MASK << GPIO_BIT(gpio));
  158. val |= lvl_type << GPIO_BIT(gpio);
  159. __raw_writel(val, GPIO_INT_LVL(gpio));
  160. spin_unlock_irqrestore(&bank->lvl_lock[port], flags);
  161. if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  162. __set_irq_handler_unlocked(irq, handle_level_irq);
  163. else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  164. __set_irq_handler_unlocked(irq, handle_edge_irq);
  165. return 0;
  166. }
  167. static void tegra_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  168. {
  169. struct tegra_gpio_bank *bank;
  170. int port;
  171. int pin;
  172. int unmasked = 0;
  173. desc->chip->ack(irq);
  174. bank = get_irq_data(irq);
  175. for (port = 0; port < 4; port++) {
  176. int gpio = tegra_gpio_compose(bank->bank, port, 0);
  177. unsigned long sta = __raw_readl(GPIO_INT_STA(gpio)) &
  178. __raw_readl(GPIO_INT_ENB(gpio));
  179. u32 lvl = __raw_readl(GPIO_INT_LVL(gpio));
  180. for_each_set_bit(pin, &sta, 8) {
  181. __raw_writel(1 << pin, GPIO_INT_CLR(gpio));
  182. /* if gpio is edge triggered, clear condition
  183. * before executing the hander so that we don't
  184. * miss edges
  185. */
  186. if (lvl & (0x100 << pin)) {
  187. unmasked = 1;
  188. desc->chip->unmask(irq);
  189. }
  190. generic_handle_irq(gpio_to_irq(gpio + pin));
  191. }
  192. }
  193. if (!unmasked)
  194. desc->chip->unmask(irq);
  195. }
  196. static struct irq_chip tegra_gpio_irq_chip = {
  197. .name = "GPIO",
  198. .ack = tegra_gpio_irq_ack,
  199. .mask = tegra_gpio_irq_mask,
  200. .unmask = tegra_gpio_irq_unmask,
  201. .set_type = tegra_gpio_irq_set_type,
  202. };
  203. /* This lock class tells lockdep that GPIO irqs are in a different
  204. * category than their parents, so it won't report false recursion.
  205. */
  206. static struct lock_class_key gpio_lock_class;
  207. static int __init tegra_gpio_init(void)
  208. {
  209. struct tegra_gpio_bank *bank;
  210. int i;
  211. int j;
  212. for (i = 0; i < 7; i++) {
  213. for (j = 0; j < 4; j++) {
  214. int gpio = tegra_gpio_compose(i, j, 0);
  215. __raw_writel(0x00, GPIO_INT_ENB(gpio));
  216. }
  217. }
  218. gpiochip_add(&tegra_gpio_chip);
  219. for (i = INT_GPIO_BASE; i < (INT_GPIO_BASE + ARCH_NR_GPIOS); i++) {
  220. bank = &tegra_gpio_banks[GPIO_BANK(irq_to_gpio(i))];
  221. lockdep_set_class(&irq_desc[i].lock, &gpio_lock_class);
  222. set_irq_chip_data(i, bank);
  223. set_irq_chip(i, &tegra_gpio_irq_chip);
  224. set_irq_handler(i, handle_simple_irq);
  225. set_irq_flags(i, IRQF_VALID);
  226. }
  227. for (i = 0; i < ARRAY_SIZE(tegra_gpio_banks); i++) {
  228. bank = &tegra_gpio_banks[i];
  229. set_irq_chained_handler(bank->irq, tegra_gpio_irq_handler);
  230. set_irq_data(bank->irq, bank);
  231. for (j = 0; j < 4; j++)
  232. spin_lock_init(&bank->lvl_lock[j]);
  233. }
  234. return 0;
  235. }
  236. postcore_initcall(tegra_gpio_init);
  237. #ifdef CONFIG_DEBUG_FS
  238. #include <linux/debugfs.h>
  239. #include <linux/seq_file.h>
  240. static int dbg_gpio_show(struct seq_file *s, void *unused)
  241. {
  242. int i;
  243. int j;
  244. for (i = 0; i < 7; i++) {
  245. for (j = 0; j < 4; j++) {
  246. int gpio = tegra_gpio_compose(i, j, 0);
  247. seq_printf(s, "%d:%d %02x %02x %02x %02x %02x %02x %06x\n",
  248. i, j,
  249. __raw_readl(GPIO_CNF(gpio)),
  250. __raw_readl(GPIO_OE(gpio)),
  251. __raw_readl(GPIO_OUT(gpio)),
  252. __raw_readl(GPIO_IN(gpio)),
  253. __raw_readl(GPIO_INT_STA(gpio)),
  254. __raw_readl(GPIO_INT_ENB(gpio)),
  255. __raw_readl(GPIO_INT_LVL(gpio)));
  256. }
  257. }
  258. return 0;
  259. }
  260. static int dbg_gpio_open(struct inode *inode, struct file *file)
  261. {
  262. return single_open(file, dbg_gpio_show, &inode->i_private);
  263. }
  264. static const struct file_operations debug_fops = {
  265. .open = dbg_gpio_open,
  266. .read = seq_read,
  267. .llseek = seq_lseek,
  268. .release = single_release,
  269. };
  270. static int __init tegra_gpio_debuginit(void)
  271. {
  272. (void) debugfs_create_file("tegra_gpio", S_IRUGO,
  273. NULL, NULL, &debug_fops);
  274. return 0;
  275. }
  276. late_initcall(tegra_gpio_debuginit);
  277. #endif