gpio-tegra.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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/driver.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_gpio_request(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_gpio_free(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);
  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. spin_lock_irqsave(&bank->lvl_lock[port], flags);
  268. val = tegra_gpio_readl(tgi, GPIO_INT_LVL(tgi, gpio));
  269. val &= ~(GPIO_INT_LVL_MASK << GPIO_BIT(gpio));
  270. val |= lvl_type << GPIO_BIT(gpio);
  271. tegra_gpio_writel(tgi, val, GPIO_INT_LVL(tgi, gpio));
  272. spin_unlock_irqrestore(&bank->lvl_lock[port], flags);
  273. tegra_gpio_mask_write(tgi, GPIO_MSK_OE(tgi, gpio), gpio, 0);
  274. tegra_gpio_enable(tgi, gpio);
  275. ret = gpiochip_lock_as_irq(&tgi->gc, gpio);
  276. if (ret) {
  277. dev_err(tgi->dev,
  278. "unable to lock Tegra GPIO %u as IRQ\n", gpio);
  279. tegra_gpio_disable(tgi, gpio);
  280. return ret;
  281. }
  282. if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  283. irq_set_handler_locked(d, handle_level_irq);
  284. else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  285. irq_set_handler_locked(d, handle_edge_irq);
  286. return 0;
  287. }
  288. static void tegra_gpio_irq_shutdown(struct irq_data *d)
  289. {
  290. struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
  291. struct tegra_gpio_info *tgi = bank->tgi;
  292. unsigned int gpio = d->hwirq;
  293. gpiochip_unlock_as_irq(&tgi->gc, gpio);
  294. }
  295. static void tegra_gpio_irq_handler(struct irq_desc *desc)
  296. {
  297. unsigned int port, pin, gpio;
  298. bool unmasked = false;
  299. u32 lvl;
  300. unsigned long sta;
  301. struct irq_chip *chip = irq_desc_get_chip(desc);
  302. struct tegra_gpio_bank *bank = irq_desc_get_handler_data(desc);
  303. struct tegra_gpio_info *tgi = bank->tgi;
  304. chained_irq_enter(chip, desc);
  305. for (port = 0; port < 4; port++) {
  306. gpio = tegra_gpio_compose(bank->bank, port, 0);
  307. sta = tegra_gpio_readl(tgi, GPIO_INT_STA(tgi, gpio)) &
  308. tegra_gpio_readl(tgi, GPIO_INT_ENB(tgi, gpio));
  309. lvl = tegra_gpio_readl(tgi, GPIO_INT_LVL(tgi, gpio));
  310. for_each_set_bit(pin, &sta, 8) {
  311. tegra_gpio_writel(tgi, 1 << pin,
  312. GPIO_INT_CLR(tgi, gpio));
  313. /* if gpio is edge triggered, clear condition
  314. * before executing the handler so that we don't
  315. * miss edges
  316. */
  317. if (!unmasked && lvl & (0x100 << pin)) {
  318. unmasked = true;
  319. chained_irq_exit(chip, desc);
  320. }
  321. generic_handle_irq(irq_find_mapping(tgi->irq_domain,
  322. gpio + pin));
  323. }
  324. }
  325. if (!unmasked)
  326. chained_irq_exit(chip, desc);
  327. }
  328. #ifdef CONFIG_PM_SLEEP
  329. static int tegra_gpio_resume(struct device *dev)
  330. {
  331. struct platform_device *pdev = to_platform_device(dev);
  332. struct tegra_gpio_info *tgi = platform_get_drvdata(pdev);
  333. unsigned long flags;
  334. unsigned int b, p;
  335. local_irq_save(flags);
  336. for (b = 0; b < tgi->bank_count; b++) {
  337. struct tegra_gpio_bank *bank = &tgi->bank_info[b];
  338. for (p = 0; p < ARRAY_SIZE(bank->oe); p++) {
  339. unsigned int gpio = (b << 5) | (p << 3);
  340. tegra_gpio_writel(tgi, bank->cnf[p],
  341. GPIO_CNF(tgi, gpio));
  342. if (tgi->soc->debounce_supported) {
  343. tegra_gpio_writel(tgi, bank->dbc_cnt[p],
  344. GPIO_DBC_CNT(tgi, gpio));
  345. tegra_gpio_writel(tgi, bank->dbc_enb[p],
  346. GPIO_MSK_DBC_EN(tgi, gpio));
  347. }
  348. tegra_gpio_writel(tgi, bank->out[p],
  349. GPIO_OUT(tgi, gpio));
  350. tegra_gpio_writel(tgi, bank->oe[p],
  351. GPIO_OE(tgi, gpio));
  352. tegra_gpio_writel(tgi, bank->int_lvl[p],
  353. GPIO_INT_LVL(tgi, gpio));
  354. tegra_gpio_writel(tgi, bank->int_enb[p],
  355. GPIO_INT_ENB(tgi, gpio));
  356. }
  357. }
  358. local_irq_restore(flags);
  359. return 0;
  360. }
  361. static int tegra_gpio_suspend(struct device *dev)
  362. {
  363. struct platform_device *pdev = to_platform_device(dev);
  364. struct tegra_gpio_info *tgi = platform_get_drvdata(pdev);
  365. unsigned long flags;
  366. unsigned int b, p;
  367. local_irq_save(flags);
  368. for (b = 0; b < tgi->bank_count; b++) {
  369. struct tegra_gpio_bank *bank = &tgi->bank_info[b];
  370. for (p = 0; p < ARRAY_SIZE(bank->oe); p++) {
  371. unsigned int gpio = (b << 5) | (p << 3);
  372. bank->cnf[p] = tegra_gpio_readl(tgi,
  373. GPIO_CNF(tgi, gpio));
  374. bank->out[p] = tegra_gpio_readl(tgi,
  375. GPIO_OUT(tgi, gpio));
  376. bank->oe[p] = tegra_gpio_readl(tgi,
  377. GPIO_OE(tgi, gpio));
  378. if (tgi->soc->debounce_supported) {
  379. bank->dbc_enb[p] = tegra_gpio_readl(tgi,
  380. GPIO_MSK_DBC_EN(tgi, gpio));
  381. bank->dbc_enb[p] = (bank->dbc_enb[p] << 8) |
  382. bank->dbc_enb[p];
  383. }
  384. bank->int_enb[p] = tegra_gpio_readl(tgi,
  385. GPIO_INT_ENB(tgi, gpio));
  386. bank->int_lvl[p] = tegra_gpio_readl(tgi,
  387. GPIO_INT_LVL(tgi, gpio));
  388. /* Enable gpio irq for wake up source */
  389. tegra_gpio_writel(tgi, bank->wake_enb[p],
  390. GPIO_INT_ENB(tgi, gpio));
  391. }
  392. }
  393. local_irq_restore(flags);
  394. return 0;
  395. }
  396. static int tegra_gpio_irq_set_wake(struct irq_data *d, unsigned int enable)
  397. {
  398. struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
  399. unsigned int gpio = d->hwirq;
  400. u32 port, bit, mask;
  401. port = GPIO_PORT(gpio);
  402. bit = GPIO_BIT(gpio);
  403. mask = BIT(bit);
  404. if (enable)
  405. bank->wake_enb[port] |= mask;
  406. else
  407. bank->wake_enb[port] &= ~mask;
  408. return irq_set_irq_wake(bank->irq, enable);
  409. }
  410. #endif
  411. #ifdef CONFIG_DEBUG_FS
  412. #include <linux/debugfs.h>
  413. #include <linux/seq_file.h>
  414. static int tegra_dbg_gpio_show(struct seq_file *s, void *unused)
  415. {
  416. struct tegra_gpio_info *tgi = s->private;
  417. unsigned int i, j;
  418. for (i = 0; i < tgi->bank_count; i++) {
  419. for (j = 0; j < 4; j++) {
  420. unsigned int gpio = tegra_gpio_compose(i, j, 0);
  421. seq_printf(s,
  422. "%u:%u %02x %02x %02x %02x %02x %02x %06x\n",
  423. i, j,
  424. tegra_gpio_readl(tgi, GPIO_CNF(tgi, gpio)),
  425. tegra_gpio_readl(tgi, GPIO_OE(tgi, gpio)),
  426. tegra_gpio_readl(tgi, GPIO_OUT(tgi, gpio)),
  427. tegra_gpio_readl(tgi, GPIO_IN(tgi, gpio)),
  428. tegra_gpio_readl(tgi, GPIO_INT_STA(tgi, gpio)),
  429. tegra_gpio_readl(tgi, GPIO_INT_ENB(tgi, gpio)),
  430. tegra_gpio_readl(tgi, GPIO_INT_LVL(tgi, gpio)));
  431. }
  432. }
  433. return 0;
  434. }
  435. DEFINE_SHOW_ATTRIBUTE(tegra_dbg_gpio);
  436. static void tegra_gpio_debuginit(struct tegra_gpio_info *tgi)
  437. {
  438. (void) debugfs_create_file("tegra_gpio", 0444,
  439. NULL, tgi, &tegra_dbg_gpio_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. static int tegra_gpio_probe(struct platform_device *pdev)
  450. {
  451. struct tegra_gpio_info *tgi;
  452. struct resource *res;
  453. struct tegra_gpio_bank *bank;
  454. unsigned int gpio, i, j;
  455. int ret;
  456. tgi = devm_kzalloc(&pdev->dev, sizeof(*tgi), GFP_KERNEL);
  457. if (!tgi)
  458. return -ENODEV;
  459. tgi->soc = of_device_get_match_data(&pdev->dev);
  460. tgi->dev = &pdev->dev;
  461. ret = platform_irq_count(pdev);
  462. if (ret < 0)
  463. return ret;
  464. tgi->bank_count = ret;
  465. if (!tgi->bank_count) {
  466. dev_err(&pdev->dev, "Missing IRQ resource\n");
  467. return -ENODEV;
  468. }
  469. tgi->gc.label = "tegra-gpio";
  470. tgi->gc.request = tegra_gpio_request;
  471. tgi->gc.free = tegra_gpio_free;
  472. tgi->gc.direction_input = tegra_gpio_direction_input;
  473. tgi->gc.get = tegra_gpio_get;
  474. tgi->gc.direction_output = tegra_gpio_direction_output;
  475. tgi->gc.set = tegra_gpio_set;
  476. tgi->gc.get_direction = tegra_gpio_get_direction;
  477. tgi->gc.to_irq = tegra_gpio_to_irq;
  478. tgi->gc.base = 0;
  479. tgi->gc.ngpio = tgi->bank_count * 32;
  480. tgi->gc.parent = &pdev->dev;
  481. tgi->gc.of_node = pdev->dev.of_node;
  482. tgi->ic.name = "GPIO";
  483. tgi->ic.irq_ack = tegra_gpio_irq_ack;
  484. tgi->ic.irq_mask = tegra_gpio_irq_mask;
  485. tgi->ic.irq_unmask = tegra_gpio_irq_unmask;
  486. tgi->ic.irq_set_type = tegra_gpio_irq_set_type;
  487. tgi->ic.irq_shutdown = tegra_gpio_irq_shutdown;
  488. #ifdef CONFIG_PM_SLEEP
  489. tgi->ic.irq_set_wake = tegra_gpio_irq_set_wake;
  490. #endif
  491. platform_set_drvdata(pdev, tgi);
  492. if (tgi->soc->debounce_supported)
  493. tgi->gc.set_config = tegra_gpio_set_config;
  494. tgi->bank_info = devm_kcalloc(&pdev->dev, tgi->bank_count,
  495. sizeof(*tgi->bank_info), GFP_KERNEL);
  496. if (!tgi->bank_info)
  497. return -ENOMEM;
  498. tgi->irq_domain = irq_domain_add_linear(pdev->dev.of_node,
  499. tgi->gc.ngpio,
  500. &irq_domain_simple_ops, NULL);
  501. if (!tgi->irq_domain)
  502. return -ENODEV;
  503. for (i = 0; i < tgi->bank_count; i++) {
  504. ret = platform_get_irq(pdev, i);
  505. if (ret < 0) {
  506. dev_err(&pdev->dev, "Missing IRQ resource: %d\n", ret);
  507. return ret;
  508. }
  509. bank = &tgi->bank_info[i];
  510. bank->bank = i;
  511. bank->irq = ret;
  512. bank->tgi = tgi;
  513. }
  514. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  515. tgi->regs = devm_ioremap_resource(&pdev->dev, res);
  516. if (IS_ERR(tgi->regs))
  517. return PTR_ERR(tgi->regs);
  518. for (i = 0; i < tgi->bank_count; i++) {
  519. for (j = 0; j < 4; j++) {
  520. int gpio = tegra_gpio_compose(i, j, 0);
  521. tegra_gpio_writel(tgi, 0x00, GPIO_INT_ENB(tgi, gpio));
  522. }
  523. }
  524. ret = devm_gpiochip_add_data(&pdev->dev, &tgi->gc, tgi);
  525. if (ret < 0) {
  526. irq_domain_remove(tgi->irq_domain);
  527. return ret;
  528. }
  529. for (gpio = 0; gpio < tgi->gc.ngpio; gpio++) {
  530. int irq = irq_create_mapping(tgi->irq_domain, gpio);
  531. /* No validity check; all Tegra GPIOs are valid IRQs */
  532. bank = &tgi->bank_info[GPIO_BANK(gpio)];
  533. irq_set_chip_data(irq, bank);
  534. irq_set_chip_and_handler(irq, &tgi->ic, handle_simple_irq);
  535. }
  536. for (i = 0; i < tgi->bank_count; i++) {
  537. bank = &tgi->bank_info[i];
  538. irq_set_chained_handler_and_data(bank->irq,
  539. tegra_gpio_irq_handler, bank);
  540. for (j = 0; j < 4; j++) {
  541. spin_lock_init(&bank->lvl_lock[j]);
  542. spin_lock_init(&bank->dbc_lock[j]);
  543. }
  544. }
  545. tegra_gpio_debuginit(tgi);
  546. return 0;
  547. }
  548. static const struct tegra_gpio_soc_config tegra20_gpio_config = {
  549. .bank_stride = 0x80,
  550. .upper_offset = 0x800,
  551. };
  552. static const struct tegra_gpio_soc_config tegra30_gpio_config = {
  553. .bank_stride = 0x100,
  554. .upper_offset = 0x80,
  555. };
  556. static const struct tegra_gpio_soc_config tegra210_gpio_config = {
  557. .debounce_supported = true,
  558. .bank_stride = 0x100,
  559. .upper_offset = 0x80,
  560. };
  561. static const struct of_device_id tegra_gpio_of_match[] = {
  562. { .compatible = "nvidia,tegra210-gpio", .data = &tegra210_gpio_config },
  563. { .compatible = "nvidia,tegra30-gpio", .data = &tegra30_gpio_config },
  564. { .compatible = "nvidia,tegra20-gpio", .data = &tegra20_gpio_config },
  565. { },
  566. };
  567. static struct platform_driver tegra_gpio_driver = {
  568. .driver = {
  569. .name = "tegra-gpio",
  570. .pm = &tegra_gpio_pm_ops,
  571. .of_match_table = tegra_gpio_of_match,
  572. },
  573. .probe = tegra_gpio_probe,
  574. };
  575. static int __init tegra_gpio_init(void)
  576. {
  577. return platform_driver_register(&tegra_gpio_driver);
  578. }
  579. subsys_initcall(tegra_gpio_init);