gpio-tegra.c 14 KB

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