gpio-tegra.c 14 KB

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