gpio-tegra.c 12 KB

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