gpio-tegra.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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/interrupt.h>
  22. #include <linux/io.h>
  23. #include <linux/gpio.h>
  24. #include <linux/of.h>
  25. #include <asm/mach/irq.h>
  26. #include <mach/iomap.h>
  27. #include <mach/suspend.h>
  28. #define GPIO_BANK(x) ((x) >> 5)
  29. #define GPIO_PORT(x) (((x) >> 3) & 0x3)
  30. #define GPIO_BIT(x) ((x) & 0x7)
  31. #define GPIO_REG(x) (IO_TO_VIRT(TEGRA_GPIO_BASE) + \
  32. GPIO_BANK(x) * 0x80 + \
  33. GPIO_PORT(x) * 4)
  34. #define GPIO_CNF(x) (GPIO_REG(x) + 0x00)
  35. #define GPIO_OE(x) (GPIO_REG(x) + 0x10)
  36. #define GPIO_OUT(x) (GPIO_REG(x) + 0X20)
  37. #define GPIO_IN(x) (GPIO_REG(x) + 0x30)
  38. #define GPIO_INT_STA(x) (GPIO_REG(x) + 0x40)
  39. #define GPIO_INT_ENB(x) (GPIO_REG(x) + 0x50)
  40. #define GPIO_INT_LVL(x) (GPIO_REG(x) + 0x60)
  41. #define GPIO_INT_CLR(x) (GPIO_REG(x) + 0x70)
  42. #define GPIO_MSK_CNF(x) (GPIO_REG(x) + 0x800)
  43. #define GPIO_MSK_OE(x) (GPIO_REG(x) + 0x810)
  44. #define GPIO_MSK_OUT(x) (GPIO_REG(x) + 0X820)
  45. #define GPIO_MSK_INT_STA(x) (GPIO_REG(x) + 0x840)
  46. #define GPIO_MSK_INT_ENB(x) (GPIO_REG(x) + 0x850)
  47. #define GPIO_MSK_INT_LVL(x) (GPIO_REG(x) + 0x860)
  48. #define GPIO_INT_LVL_MASK 0x010101
  49. #define GPIO_INT_LVL_EDGE_RISING 0x000101
  50. #define GPIO_INT_LVL_EDGE_FALLING 0x000100
  51. #define GPIO_INT_LVL_EDGE_BOTH 0x010100
  52. #define GPIO_INT_LVL_LEVEL_HIGH 0x000001
  53. #define GPIO_INT_LVL_LEVEL_LOW 0x000000
  54. struct tegra_gpio_bank {
  55. int bank;
  56. int irq;
  57. spinlock_t lvl_lock[4];
  58. #ifdef CONFIG_PM
  59. u32 cnf[4];
  60. u32 out[4];
  61. u32 oe[4];
  62. u32 int_enb[4];
  63. u32 int_lvl[4];
  64. #endif
  65. };
  66. static struct tegra_gpio_bank tegra_gpio_banks[] = {
  67. {.bank = 0, .irq = INT_GPIO1},
  68. {.bank = 1, .irq = INT_GPIO2},
  69. {.bank = 2, .irq = INT_GPIO3},
  70. {.bank = 3, .irq = INT_GPIO4},
  71. {.bank = 4, .irq = INT_GPIO5},
  72. {.bank = 5, .irq = INT_GPIO6},
  73. {.bank = 6, .irq = INT_GPIO7},
  74. };
  75. static int tegra_gpio_compose(int bank, int port, int bit)
  76. {
  77. return (bank << 5) | ((port & 0x3) << 3) | (bit & 0x7);
  78. }
  79. static void tegra_gpio_mask_write(u32 reg, int gpio, int value)
  80. {
  81. u32 val;
  82. val = 0x100 << GPIO_BIT(gpio);
  83. if (value)
  84. val |= 1 << GPIO_BIT(gpio);
  85. __raw_writel(val, reg);
  86. }
  87. void tegra_gpio_enable(int gpio)
  88. {
  89. tegra_gpio_mask_write(GPIO_MSK_CNF(gpio), gpio, 1);
  90. }
  91. void tegra_gpio_disable(int gpio)
  92. {
  93. tegra_gpio_mask_write(GPIO_MSK_CNF(gpio), gpio, 0);
  94. }
  95. static void tegra_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  96. {
  97. tegra_gpio_mask_write(GPIO_MSK_OUT(offset), offset, value);
  98. }
  99. static int tegra_gpio_get(struct gpio_chip *chip, unsigned offset)
  100. {
  101. return (__raw_readl(GPIO_IN(offset)) >> GPIO_BIT(offset)) & 0x1;
  102. }
  103. static int tegra_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  104. {
  105. tegra_gpio_mask_write(GPIO_MSK_OE(offset), offset, 0);
  106. return 0;
  107. }
  108. static int tegra_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  109. int value)
  110. {
  111. tegra_gpio_set(chip, offset, value);
  112. tegra_gpio_mask_write(GPIO_MSK_OE(offset), offset, 1);
  113. return 0;
  114. }
  115. static struct gpio_chip tegra_gpio_chip = {
  116. .label = "tegra-gpio",
  117. .direction_input = tegra_gpio_direction_input,
  118. .get = tegra_gpio_get,
  119. .direction_output = tegra_gpio_direction_output,
  120. .set = tegra_gpio_set,
  121. .base = 0,
  122. .ngpio = TEGRA_NR_GPIOS,
  123. };
  124. static void tegra_gpio_irq_ack(struct irq_data *d)
  125. {
  126. int gpio = d->irq - INT_GPIO_BASE;
  127. __raw_writel(1 << GPIO_BIT(gpio), GPIO_INT_CLR(gpio));
  128. }
  129. static void tegra_gpio_irq_mask(struct irq_data *d)
  130. {
  131. int gpio = d->irq - INT_GPIO_BASE;
  132. tegra_gpio_mask_write(GPIO_MSK_INT_ENB(gpio), gpio, 0);
  133. }
  134. static void tegra_gpio_irq_unmask(struct irq_data *d)
  135. {
  136. int gpio = d->irq - INT_GPIO_BASE;
  137. tegra_gpio_mask_write(GPIO_MSK_INT_ENB(gpio), gpio, 1);
  138. }
  139. static int tegra_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  140. {
  141. int gpio = d->irq - INT_GPIO_BASE;
  142. struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
  143. int port = GPIO_PORT(gpio);
  144. int lvl_type;
  145. int val;
  146. unsigned long flags;
  147. switch (type & IRQ_TYPE_SENSE_MASK) {
  148. case IRQ_TYPE_EDGE_RISING:
  149. lvl_type = GPIO_INT_LVL_EDGE_RISING;
  150. break;
  151. case IRQ_TYPE_EDGE_FALLING:
  152. lvl_type = GPIO_INT_LVL_EDGE_FALLING;
  153. break;
  154. case IRQ_TYPE_EDGE_BOTH:
  155. lvl_type = GPIO_INT_LVL_EDGE_BOTH;
  156. break;
  157. case IRQ_TYPE_LEVEL_HIGH:
  158. lvl_type = GPIO_INT_LVL_LEVEL_HIGH;
  159. break;
  160. case IRQ_TYPE_LEVEL_LOW:
  161. lvl_type = GPIO_INT_LVL_LEVEL_LOW;
  162. break;
  163. default:
  164. return -EINVAL;
  165. }
  166. spin_lock_irqsave(&bank->lvl_lock[port], flags);
  167. val = __raw_readl(GPIO_INT_LVL(gpio));
  168. val &= ~(GPIO_INT_LVL_MASK << GPIO_BIT(gpio));
  169. val |= lvl_type << GPIO_BIT(gpio);
  170. __raw_writel(val, GPIO_INT_LVL(gpio));
  171. spin_unlock_irqrestore(&bank->lvl_lock[port], flags);
  172. if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  173. __irq_set_handler_locked(d->irq, handle_level_irq);
  174. else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  175. __irq_set_handler_locked(d->irq, handle_edge_irq);
  176. return 0;
  177. }
  178. static void tegra_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  179. {
  180. struct tegra_gpio_bank *bank;
  181. int port;
  182. int pin;
  183. int unmasked = 0;
  184. struct irq_chip *chip = irq_desc_get_chip(desc);
  185. chained_irq_enter(chip, desc);
  186. bank = irq_get_handler_data(irq);
  187. for (port = 0; port < 4; port++) {
  188. int gpio = tegra_gpio_compose(bank->bank, port, 0);
  189. unsigned long sta = __raw_readl(GPIO_INT_STA(gpio)) &
  190. __raw_readl(GPIO_INT_ENB(gpio));
  191. u32 lvl = __raw_readl(GPIO_INT_LVL(gpio));
  192. for_each_set_bit(pin, &sta, 8) {
  193. __raw_writel(1 << pin, GPIO_INT_CLR(gpio));
  194. /* if gpio is edge triggered, clear condition
  195. * before executing the hander so that we don't
  196. * miss edges
  197. */
  198. if (lvl & (0x100 << pin)) {
  199. unmasked = 1;
  200. chained_irq_exit(chip, desc);
  201. }
  202. generic_handle_irq(gpio_to_irq(gpio + pin));
  203. }
  204. }
  205. if (!unmasked)
  206. chained_irq_exit(chip, desc);
  207. }
  208. #ifdef CONFIG_PM
  209. void tegra_gpio_resume(void)
  210. {
  211. unsigned long flags;
  212. int b;
  213. int p;
  214. local_irq_save(flags);
  215. for (b = 0; b < ARRAY_SIZE(tegra_gpio_banks); b++) {
  216. struct tegra_gpio_bank *bank = &tegra_gpio_banks[b];
  217. for (p = 0; p < ARRAY_SIZE(bank->oe); p++) {
  218. unsigned int gpio = (b<<5) | (p<<3);
  219. __raw_writel(bank->cnf[p], GPIO_CNF(gpio));
  220. __raw_writel(bank->out[p], GPIO_OUT(gpio));
  221. __raw_writel(bank->oe[p], GPIO_OE(gpio));
  222. __raw_writel(bank->int_lvl[p], GPIO_INT_LVL(gpio));
  223. __raw_writel(bank->int_enb[p], GPIO_INT_ENB(gpio));
  224. }
  225. }
  226. local_irq_restore(flags);
  227. }
  228. void tegra_gpio_suspend(void)
  229. {
  230. unsigned long flags;
  231. int b;
  232. int p;
  233. local_irq_save(flags);
  234. for (b = 0; b < ARRAY_SIZE(tegra_gpio_banks); b++) {
  235. struct tegra_gpio_bank *bank = &tegra_gpio_banks[b];
  236. for (p = 0; p < ARRAY_SIZE(bank->oe); p++) {
  237. unsigned int gpio = (b<<5) | (p<<3);
  238. bank->cnf[p] = __raw_readl(GPIO_CNF(gpio));
  239. bank->out[p] = __raw_readl(GPIO_OUT(gpio));
  240. bank->oe[p] = __raw_readl(GPIO_OE(gpio));
  241. bank->int_enb[p] = __raw_readl(GPIO_INT_ENB(gpio));
  242. bank->int_lvl[p] = __raw_readl(GPIO_INT_LVL(gpio));
  243. }
  244. }
  245. local_irq_restore(flags);
  246. }
  247. static int tegra_gpio_wake_enable(struct irq_data *d, unsigned int enable)
  248. {
  249. struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
  250. return irq_set_irq_wake(bank->irq, enable);
  251. }
  252. #endif
  253. static struct irq_chip tegra_gpio_irq_chip = {
  254. .name = "GPIO",
  255. .irq_ack = tegra_gpio_irq_ack,
  256. .irq_mask = tegra_gpio_irq_mask,
  257. .irq_unmask = tegra_gpio_irq_unmask,
  258. .irq_set_type = tegra_gpio_irq_set_type,
  259. #ifdef CONFIG_PM
  260. .irq_set_wake = tegra_gpio_wake_enable,
  261. #endif
  262. };
  263. /* This lock class tells lockdep that GPIO irqs are in a different
  264. * category than their parents, so it won't report false recursion.
  265. */
  266. static struct lock_class_key gpio_lock_class;
  267. static int __init tegra_gpio_init(void)
  268. {
  269. struct tegra_gpio_bank *bank;
  270. int i;
  271. int j;
  272. for (i = 0; i < 7; i++) {
  273. for (j = 0; j < 4; j++) {
  274. int gpio = tegra_gpio_compose(i, j, 0);
  275. __raw_writel(0x00, GPIO_INT_ENB(gpio));
  276. }
  277. }
  278. #ifdef CONFIG_OF_GPIO
  279. /*
  280. * This isn't ideal, but it gets things hooked up until this
  281. * driver is converted into a platform_device
  282. */
  283. tegra_gpio_chip.of_node = of_find_compatible_node(NULL, NULL,
  284. "nvidia,tegra20-gpio");
  285. #endif /* CONFIG_OF_GPIO */
  286. gpiochip_add(&tegra_gpio_chip);
  287. for (i = INT_GPIO_BASE; i < (INT_GPIO_BASE + TEGRA_NR_GPIOS); i++) {
  288. bank = &tegra_gpio_banks[GPIO_BANK(irq_to_gpio(i))];
  289. irq_set_lockdep_class(i, &gpio_lock_class);
  290. irq_set_chip_data(i, bank);
  291. irq_set_chip_and_handler(i, &tegra_gpio_irq_chip,
  292. handle_simple_irq);
  293. set_irq_flags(i, IRQF_VALID);
  294. }
  295. for (i = 0; i < ARRAY_SIZE(tegra_gpio_banks); i++) {
  296. bank = &tegra_gpio_banks[i];
  297. irq_set_chained_handler(bank->irq, tegra_gpio_irq_handler);
  298. irq_set_handler_data(bank->irq, bank);
  299. for (j = 0; j < 4; j++)
  300. spin_lock_init(&bank->lvl_lock[j]);
  301. }
  302. return 0;
  303. }
  304. postcore_initcall(tegra_gpio_init);
  305. void __init tegra_gpio_config(struct tegra_gpio_table *table, int num)
  306. {
  307. int i;
  308. for (i = 0; i < num; i++) {
  309. int gpio = table[i].gpio;
  310. if (table[i].enable)
  311. tegra_gpio_enable(gpio);
  312. else
  313. tegra_gpio_disable(gpio);
  314. }
  315. }
  316. #ifdef CONFIG_DEBUG_FS
  317. #include <linux/debugfs.h>
  318. #include <linux/seq_file.h>
  319. static int dbg_gpio_show(struct seq_file *s, void *unused)
  320. {
  321. int i;
  322. int j;
  323. for (i = 0; i < 7; i++) {
  324. for (j = 0; j < 4; j++) {
  325. int gpio = tegra_gpio_compose(i, j, 0);
  326. seq_printf(s,
  327. "%d:%d %02x %02x %02x %02x %02x %02x %06x\n",
  328. i, j,
  329. __raw_readl(GPIO_CNF(gpio)),
  330. __raw_readl(GPIO_OE(gpio)),
  331. __raw_readl(GPIO_OUT(gpio)),
  332. __raw_readl(GPIO_IN(gpio)),
  333. __raw_readl(GPIO_INT_STA(gpio)),
  334. __raw_readl(GPIO_INT_ENB(gpio)),
  335. __raw_readl(GPIO_INT_LVL(gpio)));
  336. }
  337. }
  338. return 0;
  339. }
  340. static int dbg_gpio_open(struct inode *inode, struct file *file)
  341. {
  342. return single_open(file, dbg_gpio_show, &inode->i_private);
  343. }
  344. static const struct file_operations debug_fops = {
  345. .open = dbg_gpio_open,
  346. .read = seq_read,
  347. .llseek = seq_lseek,
  348. .release = single_release,
  349. };
  350. static int __init tegra_gpio_debuginit(void)
  351. {
  352. (void) debugfs_create_file("tegra_gpio", S_IRUGO,
  353. NULL, NULL, &debug_fops);
  354. return 0;
  355. }
  356. late_initcall(tegra_gpio_debuginit);
  357. #endif