gpio.c 10 KB

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