gpio-tegra.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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(tgi, x) (GPIO_BANK(x) * tgi->soc->bank_stride + \
  36. GPIO_PORT(x) * 4)
  37. #define GPIO_CNF(t, x) (GPIO_REG(t, x) + 0x00)
  38. #define GPIO_OE(t, x) (GPIO_REG(t, x) + 0x10)
  39. #define GPIO_OUT(t, x) (GPIO_REG(t, x) + 0X20)
  40. #define GPIO_IN(t, x) (GPIO_REG(t, x) + 0x30)
  41. #define GPIO_INT_STA(t, x) (GPIO_REG(t, x) + 0x40)
  42. #define GPIO_INT_ENB(t, x) (GPIO_REG(t, x) + 0x50)
  43. #define GPIO_INT_LVL(t, x) (GPIO_REG(t, x) + 0x60)
  44. #define GPIO_INT_CLR(t, x) (GPIO_REG(t, x) + 0x70)
  45. #define GPIO_DBC_CNT(t, x) (GPIO_REG(t, x) + 0xF0)
  46. #define GPIO_MSK_CNF(t, x) (GPIO_REG(t, x) + t->soc->upper_offset + 0x00)
  47. #define GPIO_MSK_OE(t, x) (GPIO_REG(t, x) + t->soc->upper_offset + 0x10)
  48. #define GPIO_MSK_OUT(t, x) (GPIO_REG(t, x) + t->soc->upper_offset + 0X20)
  49. #define GPIO_MSK_DBC_EN(t, x) (GPIO_REG(t, x) + t->soc->upper_offset + 0x30)
  50. #define GPIO_MSK_INT_STA(t, x) (GPIO_REG(t, x) + t->soc->upper_offset + 0x40)
  51. #define GPIO_MSK_INT_ENB(t, x) (GPIO_REG(t, x) + t->soc->upper_offset + 0x50)
  52. #define GPIO_MSK_INT_LVL(t, x) (GPIO_REG(t, x) + t->soc->upper_offset + 0x60)
  53. #define GPIO_INT_LVL_MASK 0x010101
  54. #define GPIO_INT_LVL_EDGE_RISING 0x000101
  55. #define GPIO_INT_LVL_EDGE_FALLING 0x000100
  56. #define GPIO_INT_LVL_EDGE_BOTH 0x010100
  57. #define GPIO_INT_LVL_LEVEL_HIGH 0x000001
  58. #define GPIO_INT_LVL_LEVEL_LOW 0x000000
  59. struct tegra_gpio_info;
  60. struct tegra_gpio_bank {
  61. int bank;
  62. int irq;
  63. spinlock_t lvl_lock[4];
  64. spinlock_t dbc_lock[4]; /* Lock for updating debounce count register */
  65. #ifdef CONFIG_PM_SLEEP
  66. u32 cnf[4];
  67. u32 out[4];
  68. u32 oe[4];
  69. u32 int_enb[4];
  70. u32 int_lvl[4];
  71. u32 wake_enb[4];
  72. u32 dbc_enb[4];
  73. #endif
  74. u32 dbc_cnt[4];
  75. struct tegra_gpio_info *tgi;
  76. };
  77. struct tegra_gpio_soc_config {
  78. bool debounce_supported;
  79. u32 bank_stride;
  80. u32 upper_offset;
  81. };
  82. struct tegra_gpio_info {
  83. struct device *dev;
  84. void __iomem *regs;
  85. struct irq_domain *irq_domain;
  86. struct tegra_gpio_bank *bank_info;
  87. const struct tegra_gpio_soc_config *soc;
  88. struct gpio_chip gc;
  89. struct irq_chip ic;
  90. u32 bank_count;
  91. };
  92. static inline void tegra_gpio_writel(struct tegra_gpio_info *tgi,
  93. u32 val, u32 reg)
  94. {
  95. __raw_writel(val, tgi->regs + reg);
  96. }
  97. static inline u32 tegra_gpio_readl(struct tegra_gpio_info *tgi, u32 reg)
  98. {
  99. return __raw_readl(tgi->regs + reg);
  100. }
  101. static int tegra_gpio_compose(int bank, int port, int bit)
  102. {
  103. return (bank << 5) | ((port & 0x3) << 3) | (bit & 0x7);
  104. }
  105. static void tegra_gpio_mask_write(struct tegra_gpio_info *tgi, u32 reg,
  106. int gpio, int value)
  107. {
  108. u32 val;
  109. val = 0x100 << GPIO_BIT(gpio);
  110. if (value)
  111. val |= 1 << GPIO_BIT(gpio);
  112. tegra_gpio_writel(tgi, val, reg);
  113. }
  114. static void tegra_gpio_enable(struct tegra_gpio_info *tgi, int gpio)
  115. {
  116. tegra_gpio_mask_write(tgi, GPIO_MSK_CNF(tgi, gpio), gpio, 1);
  117. }
  118. static void tegra_gpio_disable(struct tegra_gpio_info *tgi, int gpio)
  119. {
  120. tegra_gpio_mask_write(tgi, GPIO_MSK_CNF(tgi, gpio), gpio, 0);
  121. }
  122. static int tegra_gpio_request(struct gpio_chip *chip, unsigned offset)
  123. {
  124. return pinctrl_request_gpio(offset);
  125. }
  126. static void tegra_gpio_free(struct gpio_chip *chip, unsigned offset)
  127. {
  128. struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
  129. pinctrl_free_gpio(offset);
  130. tegra_gpio_disable(tgi, offset);
  131. }
  132. static void tegra_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  133. {
  134. struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
  135. tegra_gpio_mask_write(tgi, GPIO_MSK_OUT(tgi, offset), offset, value);
  136. }
  137. static int tegra_gpio_get(struct gpio_chip *chip, unsigned offset)
  138. {
  139. struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
  140. int bval = BIT(GPIO_BIT(offset));
  141. /* If gpio is in output mode then read from the out value */
  142. if (tegra_gpio_readl(tgi, GPIO_OE(tgi, offset)) & bval)
  143. return !!(tegra_gpio_readl(tgi, GPIO_OUT(tgi, offset)) & bval);
  144. return !!(tegra_gpio_readl(tgi, GPIO_IN(tgi, offset)) & bval);
  145. }
  146. static int tegra_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  147. {
  148. struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
  149. tegra_gpio_mask_write(tgi, GPIO_MSK_OE(tgi, offset), offset, 0);
  150. tegra_gpio_enable(tgi, offset);
  151. return 0;
  152. }
  153. static int tegra_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  154. int value)
  155. {
  156. struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
  157. tegra_gpio_set(chip, offset, value);
  158. tegra_gpio_mask_write(tgi, GPIO_MSK_OE(tgi, offset), offset, 1);
  159. tegra_gpio_enable(tgi, offset);
  160. return 0;
  161. }
  162. static int tegra_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  163. {
  164. struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
  165. u32 pin_mask = BIT(GPIO_BIT(offset));
  166. u32 cnf, oe;
  167. cnf = tegra_gpio_readl(tgi, GPIO_CNF(tgi, offset));
  168. if (!(cnf & pin_mask))
  169. return -EINVAL;
  170. oe = tegra_gpio_readl(tgi, GPIO_OE(tgi, offset));
  171. return (oe & pin_mask) ? GPIOF_DIR_OUT : GPIOF_DIR_IN;
  172. }
  173. static int tegra_gpio_set_debounce(struct gpio_chip *chip, unsigned int offset,
  174. unsigned int debounce)
  175. {
  176. struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
  177. struct tegra_gpio_bank *bank = &tgi->bank_info[GPIO_BANK(offset)];
  178. unsigned int debounce_ms = DIV_ROUND_UP(debounce, 1000);
  179. unsigned long flags;
  180. int port;
  181. if (!debounce_ms) {
  182. tegra_gpio_mask_write(tgi, GPIO_MSK_DBC_EN(tgi, offset),
  183. offset, 0);
  184. return 0;
  185. }
  186. debounce_ms = min(debounce_ms, 255U);
  187. port = GPIO_PORT(offset);
  188. /* There is only one debounce count register per port and hence
  189. * set the maximum of current and requested debounce time.
  190. */
  191. spin_lock_irqsave(&bank->dbc_lock[port], flags);
  192. if (bank->dbc_cnt[port] < debounce_ms) {
  193. tegra_gpio_writel(tgi, debounce_ms, GPIO_DBC_CNT(tgi, offset));
  194. bank->dbc_cnt[port] = debounce_ms;
  195. }
  196. spin_unlock_irqrestore(&bank->dbc_lock[port], flags);
  197. tegra_gpio_mask_write(tgi, GPIO_MSK_DBC_EN(tgi, offset), offset, 1);
  198. return 0;
  199. }
  200. static int tegra_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  201. {
  202. struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
  203. return irq_find_mapping(tgi->irq_domain, offset);
  204. }
  205. static void tegra_gpio_irq_ack(struct irq_data *d)
  206. {
  207. struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
  208. struct tegra_gpio_info *tgi = bank->tgi;
  209. int gpio = d->hwirq;
  210. tegra_gpio_writel(tgi, 1 << GPIO_BIT(gpio), GPIO_INT_CLR(tgi, gpio));
  211. }
  212. static void tegra_gpio_irq_mask(struct irq_data *d)
  213. {
  214. struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
  215. struct tegra_gpio_info *tgi = bank->tgi;
  216. int gpio = d->hwirq;
  217. tegra_gpio_mask_write(tgi, GPIO_MSK_INT_ENB(tgi, gpio), gpio, 0);
  218. }
  219. static void tegra_gpio_irq_unmask(struct irq_data *d)
  220. {
  221. struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
  222. struct tegra_gpio_info *tgi = bank->tgi;
  223. int gpio = d->hwirq;
  224. tegra_gpio_mask_write(tgi, GPIO_MSK_INT_ENB(tgi, gpio), gpio, 1);
  225. }
  226. static int tegra_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  227. {
  228. int gpio = d->hwirq;
  229. struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
  230. struct tegra_gpio_info *tgi = bank->tgi;
  231. int port = GPIO_PORT(gpio);
  232. int lvl_type;
  233. int val;
  234. unsigned long flags;
  235. int ret;
  236. switch (type & IRQ_TYPE_SENSE_MASK) {
  237. case IRQ_TYPE_EDGE_RISING:
  238. lvl_type = GPIO_INT_LVL_EDGE_RISING;
  239. break;
  240. case IRQ_TYPE_EDGE_FALLING:
  241. lvl_type = GPIO_INT_LVL_EDGE_FALLING;
  242. break;
  243. case IRQ_TYPE_EDGE_BOTH:
  244. lvl_type = GPIO_INT_LVL_EDGE_BOTH;
  245. break;
  246. case IRQ_TYPE_LEVEL_HIGH:
  247. lvl_type = GPIO_INT_LVL_LEVEL_HIGH;
  248. break;
  249. case IRQ_TYPE_LEVEL_LOW:
  250. lvl_type = GPIO_INT_LVL_LEVEL_LOW;
  251. break;
  252. default:
  253. return -EINVAL;
  254. }
  255. ret = gpiochip_lock_as_irq(&tgi->gc, gpio);
  256. if (ret) {
  257. dev_err(tgi->dev,
  258. "unable to lock Tegra GPIO %d as IRQ\n", gpio);
  259. return ret;
  260. }
  261. spin_lock_irqsave(&bank->lvl_lock[port], flags);
  262. val = tegra_gpio_readl(tgi, GPIO_INT_LVL(tgi, gpio));
  263. val &= ~(GPIO_INT_LVL_MASK << GPIO_BIT(gpio));
  264. val |= lvl_type << GPIO_BIT(gpio);
  265. tegra_gpio_writel(tgi, val, GPIO_INT_LVL(tgi, gpio));
  266. spin_unlock_irqrestore(&bank->lvl_lock[port], flags);
  267. tegra_gpio_mask_write(tgi, GPIO_MSK_OE(tgi, gpio), gpio, 0);
  268. tegra_gpio_enable(tgi, gpio);
  269. if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  270. irq_set_handler_locked(d, handle_level_irq);
  271. else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  272. irq_set_handler_locked(d, handle_edge_irq);
  273. return 0;
  274. }
  275. static void tegra_gpio_irq_shutdown(struct irq_data *d)
  276. {
  277. struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
  278. struct tegra_gpio_info *tgi = bank->tgi;
  279. int gpio = d->hwirq;
  280. gpiochip_unlock_as_irq(&tgi->gc, gpio);
  281. }
  282. static void tegra_gpio_irq_handler(struct irq_desc *desc)
  283. {
  284. int port;
  285. int pin;
  286. int unmasked = 0;
  287. int gpio;
  288. u32 lvl;
  289. unsigned long sta;
  290. struct irq_chip *chip = irq_desc_get_chip(desc);
  291. struct tegra_gpio_bank *bank = irq_desc_get_handler_data(desc);
  292. struct tegra_gpio_info *tgi = bank->tgi;
  293. chained_irq_enter(chip, desc);
  294. for (port = 0; port < 4; port++) {
  295. gpio = tegra_gpio_compose(bank->bank, port, 0);
  296. sta = tegra_gpio_readl(tgi, GPIO_INT_STA(tgi, gpio)) &
  297. tegra_gpio_readl(tgi, GPIO_INT_ENB(tgi, gpio));
  298. lvl = tegra_gpio_readl(tgi, GPIO_INT_LVL(tgi, gpio));
  299. for_each_set_bit(pin, &sta, 8) {
  300. tegra_gpio_writel(tgi, 1 << pin,
  301. GPIO_INT_CLR(tgi, gpio));
  302. /* if gpio is edge triggered, clear condition
  303. * before executing the handler so that we don't
  304. * miss edges
  305. */
  306. if (lvl & (0x100 << pin)) {
  307. unmasked = 1;
  308. chained_irq_exit(chip, desc);
  309. }
  310. generic_handle_irq(gpio_to_irq(gpio + pin));
  311. }
  312. }
  313. if (!unmasked)
  314. chained_irq_exit(chip, desc);
  315. }
  316. #ifdef CONFIG_PM_SLEEP
  317. static int tegra_gpio_resume(struct device *dev)
  318. {
  319. struct platform_device *pdev = to_platform_device(dev);
  320. struct tegra_gpio_info *tgi = platform_get_drvdata(pdev);
  321. unsigned long flags;
  322. int b;
  323. int p;
  324. local_irq_save(flags);
  325. for (b = 0; b < tgi->bank_count; b++) {
  326. struct tegra_gpio_bank *bank = &tgi->bank_info[b];
  327. for (p = 0; p < ARRAY_SIZE(bank->oe); p++) {
  328. unsigned int gpio = (b<<5) | (p<<3);
  329. tegra_gpio_writel(tgi, bank->cnf[p],
  330. GPIO_CNF(tgi, gpio));
  331. if (tgi->soc->debounce_supported) {
  332. tegra_gpio_writel(tgi, bank->dbc_cnt[p],
  333. GPIO_DBC_CNT(tgi, gpio));
  334. tegra_gpio_writel(tgi, bank->dbc_enb[p],
  335. GPIO_MSK_DBC_EN(tgi, gpio));
  336. }
  337. tegra_gpio_writel(tgi, bank->out[p],
  338. GPIO_OUT(tgi, gpio));
  339. tegra_gpio_writel(tgi, bank->oe[p],
  340. GPIO_OE(tgi, gpio));
  341. tegra_gpio_writel(tgi, bank->int_lvl[p],
  342. GPIO_INT_LVL(tgi, gpio));
  343. tegra_gpio_writel(tgi, bank->int_enb[p],
  344. GPIO_INT_ENB(tgi, gpio));
  345. }
  346. }
  347. local_irq_restore(flags);
  348. return 0;
  349. }
  350. static int tegra_gpio_suspend(struct device *dev)
  351. {
  352. struct platform_device *pdev = to_platform_device(dev);
  353. struct tegra_gpio_info *tgi = platform_get_drvdata(pdev);
  354. unsigned long flags;
  355. int b;
  356. int p;
  357. local_irq_save(flags);
  358. for (b = 0; b < tgi->bank_count; b++) {
  359. struct tegra_gpio_bank *bank = &tgi->bank_info[b];
  360. for (p = 0; p < ARRAY_SIZE(bank->oe); p++) {
  361. unsigned int gpio = (b<<5) | (p<<3);
  362. bank->cnf[p] = tegra_gpio_readl(tgi,
  363. GPIO_CNF(tgi, gpio));
  364. bank->out[p] = tegra_gpio_readl(tgi,
  365. GPIO_OUT(tgi, gpio));
  366. bank->oe[p] = tegra_gpio_readl(tgi,
  367. GPIO_OE(tgi, gpio));
  368. if (tgi->soc->debounce_supported) {
  369. bank->dbc_enb[p] = tegra_gpio_readl(tgi,
  370. GPIO_MSK_DBC_EN(tgi, gpio));
  371. bank->dbc_enb[p] = (bank->dbc_enb[p] << 8) |
  372. bank->dbc_enb[p];
  373. }
  374. bank->int_enb[p] = tegra_gpio_readl(tgi,
  375. GPIO_INT_ENB(tgi, gpio));
  376. bank->int_lvl[p] = tegra_gpio_readl(tgi,
  377. GPIO_INT_LVL(tgi, gpio));
  378. /* Enable gpio irq for wake up source */
  379. tegra_gpio_writel(tgi, bank->wake_enb[p],
  380. GPIO_INT_ENB(tgi, gpio));
  381. }
  382. }
  383. local_irq_restore(flags);
  384. return 0;
  385. }
  386. static int tegra_gpio_irq_set_wake(struct irq_data *d, unsigned int enable)
  387. {
  388. struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
  389. int gpio = d->hwirq;
  390. u32 port, bit, mask;
  391. port = GPIO_PORT(gpio);
  392. bit = GPIO_BIT(gpio);
  393. mask = BIT(bit);
  394. if (enable)
  395. bank->wake_enb[port] |= mask;
  396. else
  397. bank->wake_enb[port] &= ~mask;
  398. return irq_set_irq_wake(bank->irq, enable);
  399. }
  400. #endif
  401. #ifdef CONFIG_DEBUG_FS
  402. #include <linux/debugfs.h>
  403. #include <linux/seq_file.h>
  404. static int dbg_gpio_show(struct seq_file *s, void *unused)
  405. {
  406. struct tegra_gpio_info *tgi = s->private;
  407. int i;
  408. int j;
  409. for (i = 0; i < tgi->bank_count; i++) {
  410. for (j = 0; j < 4; j++) {
  411. int gpio = tegra_gpio_compose(i, j, 0);
  412. seq_printf(s,
  413. "%d:%d %02x %02x %02x %02x %02x %02x %06x\n",
  414. i, j,
  415. tegra_gpio_readl(tgi, GPIO_CNF(tgi, gpio)),
  416. tegra_gpio_readl(tgi, GPIO_OE(tgi, gpio)),
  417. tegra_gpio_readl(tgi, GPIO_OUT(tgi, gpio)),
  418. tegra_gpio_readl(tgi, GPIO_IN(tgi, gpio)),
  419. tegra_gpio_readl(tgi, GPIO_INT_STA(tgi, gpio)),
  420. tegra_gpio_readl(tgi, GPIO_INT_ENB(tgi, gpio)),
  421. tegra_gpio_readl(tgi, GPIO_INT_LVL(tgi, gpio)));
  422. }
  423. }
  424. return 0;
  425. }
  426. static int dbg_gpio_open(struct inode *inode, struct file *file)
  427. {
  428. return single_open(file, dbg_gpio_show, inode->i_private);
  429. }
  430. static const struct file_operations debug_fops = {
  431. .open = dbg_gpio_open,
  432. .read = seq_read,
  433. .llseek = seq_lseek,
  434. .release = single_release,
  435. };
  436. static void tegra_gpio_debuginit(struct tegra_gpio_info *tgi)
  437. {
  438. (void) debugfs_create_file("tegra_gpio", S_IRUGO,
  439. NULL, tgi, &debug_fops);
  440. }
  441. #else
  442. static inline void tegra_gpio_debuginit(struct tegra_gpio_info *tgi)
  443. {
  444. }
  445. #endif
  446. static const struct dev_pm_ops tegra_gpio_pm_ops = {
  447. SET_SYSTEM_SLEEP_PM_OPS(tegra_gpio_suspend, tegra_gpio_resume)
  448. };
  449. /*
  450. * This lock class tells lockdep that GPIO irqs are in a different category
  451. * than their parents, so it won't report false recursion.
  452. */
  453. static struct lock_class_key gpio_lock_class;
  454. static int tegra_gpio_probe(struct platform_device *pdev)
  455. {
  456. const struct tegra_gpio_soc_config *config;
  457. struct tegra_gpio_info *tgi;
  458. struct resource *res;
  459. struct tegra_gpio_bank *bank;
  460. int ret;
  461. int gpio;
  462. int i;
  463. int j;
  464. config = of_device_get_match_data(&pdev->dev);
  465. if (!config) {
  466. dev_err(&pdev->dev, "Error: No device match found\n");
  467. return -ENODEV;
  468. }
  469. tgi = devm_kzalloc(&pdev->dev, sizeof(*tgi), GFP_KERNEL);
  470. if (!tgi)
  471. return -ENODEV;
  472. tgi->soc = config;
  473. tgi->dev = &pdev->dev;
  474. for (;;) {
  475. res = platform_get_resource(pdev, IORESOURCE_IRQ,
  476. tgi->bank_count);
  477. if (!res)
  478. break;
  479. tgi->bank_count++;
  480. }
  481. if (!tgi->bank_count) {
  482. dev_err(&pdev->dev, "Missing IRQ resource\n");
  483. return -ENODEV;
  484. }
  485. tgi->gc.label = "tegra-gpio";
  486. tgi->gc.request = tegra_gpio_request;
  487. tgi->gc.free = tegra_gpio_free;
  488. tgi->gc.direction_input = tegra_gpio_direction_input;
  489. tgi->gc.get = tegra_gpio_get;
  490. tgi->gc.direction_output = tegra_gpio_direction_output;
  491. tgi->gc.set = tegra_gpio_set;
  492. tgi->gc.get_direction = tegra_gpio_get_direction;
  493. tgi->gc.to_irq = tegra_gpio_to_irq;
  494. tgi->gc.base = 0;
  495. tgi->gc.ngpio = tgi->bank_count * 32;
  496. tgi->gc.parent = &pdev->dev;
  497. tgi->gc.of_node = pdev->dev.of_node;
  498. tgi->ic.name = "GPIO";
  499. tgi->ic.irq_ack = tegra_gpio_irq_ack;
  500. tgi->ic.irq_mask = tegra_gpio_irq_mask;
  501. tgi->ic.irq_unmask = tegra_gpio_irq_unmask;
  502. tgi->ic.irq_set_type = tegra_gpio_irq_set_type;
  503. tgi->ic.irq_shutdown = tegra_gpio_irq_shutdown;
  504. #ifdef CONFIG_PM_SLEEP
  505. tgi->ic.irq_set_wake = tegra_gpio_irq_set_wake;
  506. #endif
  507. platform_set_drvdata(pdev, tgi);
  508. if (config->debounce_supported)
  509. tgi->gc.set_debounce = tegra_gpio_set_debounce;
  510. tgi->bank_info = devm_kzalloc(&pdev->dev, tgi->bank_count *
  511. sizeof(*tgi->bank_info), GFP_KERNEL);
  512. if (!tgi->bank_info)
  513. return -ENODEV;
  514. tgi->irq_domain = irq_domain_add_linear(pdev->dev.of_node,
  515. tgi->gc.ngpio,
  516. &irq_domain_simple_ops, NULL);
  517. if (!tgi->irq_domain)
  518. return -ENODEV;
  519. for (i = 0; i < tgi->bank_count; i++) {
  520. res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
  521. if (!res) {
  522. dev_err(&pdev->dev, "Missing IRQ resource\n");
  523. return -ENODEV;
  524. }
  525. bank = &tgi->bank_info[i];
  526. bank->bank = i;
  527. bank->irq = res->start;
  528. bank->tgi = tgi;
  529. }
  530. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  531. tgi->regs = devm_ioremap_resource(&pdev->dev, res);
  532. if (IS_ERR(tgi->regs))
  533. return PTR_ERR(tgi->regs);
  534. for (i = 0; i < tgi->bank_count; i++) {
  535. for (j = 0; j < 4; j++) {
  536. int gpio = tegra_gpio_compose(i, j, 0);
  537. tegra_gpio_writel(tgi, 0x00, GPIO_INT_ENB(tgi, gpio));
  538. }
  539. }
  540. ret = devm_gpiochip_add_data(&pdev->dev, &tgi->gc, tgi);
  541. if (ret < 0) {
  542. irq_domain_remove(tgi->irq_domain);
  543. return ret;
  544. }
  545. for (gpio = 0; gpio < tgi->gc.ngpio; gpio++) {
  546. int irq = irq_create_mapping(tgi->irq_domain, gpio);
  547. /* No validity check; all Tegra GPIOs are valid IRQs */
  548. bank = &tgi->bank_info[GPIO_BANK(gpio)];
  549. irq_set_lockdep_class(irq, &gpio_lock_class);
  550. irq_set_chip_data(irq, bank);
  551. irq_set_chip_and_handler(irq, &tgi->ic, handle_simple_irq);
  552. }
  553. for (i = 0; i < tgi->bank_count; i++) {
  554. bank = &tgi->bank_info[i];
  555. irq_set_chained_handler_and_data(bank->irq,
  556. tegra_gpio_irq_handler, bank);
  557. for (j = 0; j < 4; j++) {
  558. spin_lock_init(&bank->lvl_lock[j]);
  559. spin_lock_init(&bank->dbc_lock[j]);
  560. }
  561. }
  562. tegra_gpio_debuginit(tgi);
  563. return 0;
  564. }
  565. static const struct tegra_gpio_soc_config tegra20_gpio_config = {
  566. .bank_stride = 0x80,
  567. .upper_offset = 0x800,
  568. };
  569. static const struct tegra_gpio_soc_config tegra30_gpio_config = {
  570. .bank_stride = 0x100,
  571. .upper_offset = 0x80,
  572. };
  573. static const struct tegra_gpio_soc_config tegra210_gpio_config = {
  574. .debounce_supported = true,
  575. .bank_stride = 0x100,
  576. .upper_offset = 0x80,
  577. };
  578. static const struct of_device_id tegra_gpio_of_match[] = {
  579. { .compatible = "nvidia,tegra210-gpio", .data = &tegra210_gpio_config },
  580. { .compatible = "nvidia,tegra30-gpio", .data = &tegra30_gpio_config },
  581. { .compatible = "nvidia,tegra20-gpio", .data = &tegra20_gpio_config },
  582. { },
  583. };
  584. static struct platform_driver tegra_gpio_driver = {
  585. .driver = {
  586. .name = "tegra-gpio",
  587. .pm = &tegra_gpio_pm_ops,
  588. .of_match_table = tegra_gpio_of_match,
  589. },
  590. .probe = tegra_gpio_probe,
  591. };
  592. static int __init tegra_gpio_init(void)
  593. {
  594. return platform_driver_register(&tegra_gpio_driver);
  595. }
  596. postcore_initcall(tegra_gpio_init);