gpio-tegra.c 19 KB

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