gpio-tegra.c 14 KB

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