gpio-tegra.c 13 KB

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