gpio-tegra.c 12 KB

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