gpio-tegra.c 12 KB

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