gpio.c 10 KB

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