gpio-tegra.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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_set_config(struct gpio_chip *chip, unsigned int offset,
  201. unsigned long config)
  202. {
  203. u32 debounce;
  204. if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
  205. return -ENOTSUPP;
  206. debounce = pinconf_to_config_argument(config);
  207. return tegra_gpio_set_debounce(chip, offset, debounce);
  208. }
  209. static int tegra_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  210. {
  211. struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
  212. return irq_find_mapping(tgi->irq_domain, offset);
  213. }
  214. static void tegra_gpio_irq_ack(struct irq_data *d)
  215. {
  216. struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
  217. struct tegra_gpio_info *tgi = bank->tgi;
  218. int gpio = d->hwirq;
  219. tegra_gpio_writel(tgi, 1 << GPIO_BIT(gpio), GPIO_INT_CLR(tgi, gpio));
  220. }
  221. static void tegra_gpio_irq_mask(struct irq_data *d)
  222. {
  223. struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
  224. struct tegra_gpio_info *tgi = bank->tgi;
  225. int gpio = d->hwirq;
  226. tegra_gpio_mask_write(tgi, GPIO_MSK_INT_ENB(tgi, gpio), gpio, 0);
  227. }
  228. static void tegra_gpio_irq_unmask(struct irq_data *d)
  229. {
  230. struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
  231. struct tegra_gpio_info *tgi = bank->tgi;
  232. int gpio = d->hwirq;
  233. tegra_gpio_mask_write(tgi, GPIO_MSK_INT_ENB(tgi, gpio), gpio, 1);
  234. }
  235. static int tegra_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  236. {
  237. int gpio = d->hwirq;
  238. struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
  239. struct tegra_gpio_info *tgi = bank->tgi;
  240. int port = GPIO_PORT(gpio);
  241. int lvl_type;
  242. int val;
  243. unsigned long flags;
  244. int ret;
  245. switch (type & IRQ_TYPE_SENSE_MASK) {
  246. case IRQ_TYPE_EDGE_RISING:
  247. lvl_type = GPIO_INT_LVL_EDGE_RISING;
  248. break;
  249. case IRQ_TYPE_EDGE_FALLING:
  250. lvl_type = GPIO_INT_LVL_EDGE_FALLING;
  251. break;
  252. case IRQ_TYPE_EDGE_BOTH:
  253. lvl_type = GPIO_INT_LVL_EDGE_BOTH;
  254. break;
  255. case IRQ_TYPE_LEVEL_HIGH:
  256. lvl_type = GPIO_INT_LVL_LEVEL_HIGH;
  257. break;
  258. case IRQ_TYPE_LEVEL_LOW:
  259. lvl_type = GPIO_INT_LVL_LEVEL_LOW;
  260. break;
  261. default:
  262. return -EINVAL;
  263. }
  264. ret = gpiochip_lock_as_irq(&tgi->gc, gpio);
  265. if (ret) {
  266. dev_err(tgi->dev,
  267. "unable to lock Tegra GPIO %d as IRQ\n", gpio);
  268. return ret;
  269. }
  270. spin_lock_irqsave(&bank->lvl_lock[port], flags);
  271. val = tegra_gpio_readl(tgi, GPIO_INT_LVL(tgi, gpio));
  272. val &= ~(GPIO_INT_LVL_MASK << GPIO_BIT(gpio));
  273. val |= lvl_type << GPIO_BIT(gpio);
  274. tegra_gpio_writel(tgi, val, GPIO_INT_LVL(tgi, gpio));
  275. spin_unlock_irqrestore(&bank->lvl_lock[port], flags);
  276. tegra_gpio_mask_write(tgi, GPIO_MSK_OE(tgi, gpio), gpio, 0);
  277. tegra_gpio_enable(tgi, gpio);
  278. if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  279. irq_set_handler_locked(d, handle_level_irq);
  280. else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  281. irq_set_handler_locked(d, handle_edge_irq);
  282. return 0;
  283. }
  284. static void tegra_gpio_irq_shutdown(struct irq_data *d)
  285. {
  286. struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
  287. struct tegra_gpio_info *tgi = bank->tgi;
  288. int gpio = d->hwirq;
  289. gpiochip_unlock_as_irq(&tgi->gc, gpio);
  290. }
  291. static void tegra_gpio_irq_handler(struct irq_desc *desc)
  292. {
  293. int port;
  294. int pin;
  295. int unmasked = 0;
  296. int gpio;
  297. u32 lvl;
  298. unsigned long sta;
  299. struct irq_chip *chip = irq_desc_get_chip(desc);
  300. struct tegra_gpio_bank *bank = irq_desc_get_handler_data(desc);
  301. struct tegra_gpio_info *tgi = bank->tgi;
  302. chained_irq_enter(chip, desc);
  303. for (port = 0; port < 4; port++) {
  304. gpio = tegra_gpio_compose(bank->bank, port, 0);
  305. sta = tegra_gpio_readl(tgi, GPIO_INT_STA(tgi, gpio)) &
  306. tegra_gpio_readl(tgi, GPIO_INT_ENB(tgi, gpio));
  307. lvl = tegra_gpio_readl(tgi, GPIO_INT_LVL(tgi, gpio));
  308. for_each_set_bit(pin, &sta, 8) {
  309. tegra_gpio_writel(tgi, 1 << pin,
  310. GPIO_INT_CLR(tgi, gpio));
  311. /* if gpio is edge triggered, clear condition
  312. * before executing the handler so that we don't
  313. * miss edges
  314. */
  315. if (lvl & (0x100 << pin)) {
  316. unmasked = 1;
  317. chained_irq_exit(chip, desc);
  318. }
  319. generic_handle_irq(gpio_to_irq(gpio + pin));
  320. }
  321. }
  322. if (!unmasked)
  323. chained_irq_exit(chip, desc);
  324. }
  325. #ifdef CONFIG_PM_SLEEP
  326. static int tegra_gpio_resume(struct device *dev)
  327. {
  328. struct platform_device *pdev = to_platform_device(dev);
  329. struct tegra_gpio_info *tgi = platform_get_drvdata(pdev);
  330. unsigned long flags;
  331. int b;
  332. int p;
  333. local_irq_save(flags);
  334. for (b = 0; b < tgi->bank_count; b++) {
  335. struct tegra_gpio_bank *bank = &tgi->bank_info[b];
  336. for (p = 0; p < ARRAY_SIZE(bank->oe); p++) {
  337. unsigned int gpio = (b<<5) | (p<<3);
  338. tegra_gpio_writel(tgi, bank->cnf[p],
  339. GPIO_CNF(tgi, gpio));
  340. if (tgi->soc->debounce_supported) {
  341. tegra_gpio_writel(tgi, bank->dbc_cnt[p],
  342. GPIO_DBC_CNT(tgi, gpio));
  343. tegra_gpio_writel(tgi, bank->dbc_enb[p],
  344. GPIO_MSK_DBC_EN(tgi, gpio));
  345. }
  346. tegra_gpio_writel(tgi, bank->out[p],
  347. GPIO_OUT(tgi, gpio));
  348. tegra_gpio_writel(tgi, bank->oe[p],
  349. GPIO_OE(tgi, gpio));
  350. tegra_gpio_writel(tgi, bank->int_lvl[p],
  351. GPIO_INT_LVL(tgi, gpio));
  352. tegra_gpio_writel(tgi, bank->int_enb[p],
  353. GPIO_INT_ENB(tgi, gpio));
  354. }
  355. }
  356. local_irq_restore(flags);
  357. return 0;
  358. }
  359. static int tegra_gpio_suspend(struct device *dev)
  360. {
  361. struct platform_device *pdev = to_platform_device(dev);
  362. struct tegra_gpio_info *tgi = platform_get_drvdata(pdev);
  363. unsigned long flags;
  364. int b;
  365. int 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. 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. int i;
  417. int j;
  418. for (i = 0; i < tgi->bank_count; i++) {
  419. for (j = 0; j < 4; j++) {
  420. int gpio = tegra_gpio_compose(i, j, 0);
  421. seq_printf(s,
  422. "%d:%d %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. static int dbg_gpio_open(struct inode *inode, struct file *file)
  436. {
  437. return single_open(file, dbg_gpio_show, inode->i_private);
  438. }
  439. static const struct file_operations debug_fops = {
  440. .open = dbg_gpio_open,
  441. .read = seq_read,
  442. .llseek = seq_lseek,
  443. .release = single_release,
  444. };
  445. static void tegra_gpio_debuginit(struct tegra_gpio_info *tgi)
  446. {
  447. (void) debugfs_create_file("tegra_gpio", S_IRUGO,
  448. NULL, tgi, &debug_fops);
  449. }
  450. #else
  451. static inline void tegra_gpio_debuginit(struct tegra_gpio_info *tgi)
  452. {
  453. }
  454. #endif
  455. static const struct dev_pm_ops tegra_gpio_pm_ops = {
  456. SET_SYSTEM_SLEEP_PM_OPS(tegra_gpio_suspend, tegra_gpio_resume)
  457. };
  458. /*
  459. * This lock class tells lockdep that GPIO irqs are in a different category
  460. * than their parents, so it won't report false recursion.
  461. */
  462. static struct lock_class_key gpio_lock_class;
  463. static int tegra_gpio_probe(struct platform_device *pdev)
  464. {
  465. const struct tegra_gpio_soc_config *config;
  466. struct tegra_gpio_info *tgi;
  467. struct resource *res;
  468. struct tegra_gpio_bank *bank;
  469. int ret;
  470. int gpio;
  471. int i;
  472. int j;
  473. config = of_device_get_match_data(&pdev->dev);
  474. if (!config) {
  475. dev_err(&pdev->dev, "Error: No device match found\n");
  476. return -ENODEV;
  477. }
  478. tgi = devm_kzalloc(&pdev->dev, sizeof(*tgi), GFP_KERNEL);
  479. if (!tgi)
  480. return -ENODEV;
  481. tgi->soc = config;
  482. tgi->dev = &pdev->dev;
  483. for (;;) {
  484. res = platform_get_resource(pdev, IORESOURCE_IRQ,
  485. tgi->bank_count);
  486. if (!res)
  487. break;
  488. tgi->bank_count++;
  489. }
  490. if (!tgi->bank_count) {
  491. dev_err(&pdev->dev, "Missing IRQ resource\n");
  492. return -ENODEV;
  493. }
  494. tgi->gc.label = "tegra-gpio";
  495. tgi->gc.request = tegra_gpio_request;
  496. tgi->gc.free = tegra_gpio_free;
  497. tgi->gc.direction_input = tegra_gpio_direction_input;
  498. tgi->gc.get = tegra_gpio_get;
  499. tgi->gc.direction_output = tegra_gpio_direction_output;
  500. tgi->gc.set = tegra_gpio_set;
  501. tgi->gc.get_direction = tegra_gpio_get_direction;
  502. tgi->gc.to_irq = tegra_gpio_to_irq;
  503. tgi->gc.base = 0;
  504. tgi->gc.ngpio = tgi->bank_count * 32;
  505. tgi->gc.parent = &pdev->dev;
  506. tgi->gc.of_node = pdev->dev.of_node;
  507. tgi->ic.name = "GPIO";
  508. tgi->ic.irq_ack = tegra_gpio_irq_ack;
  509. tgi->ic.irq_mask = tegra_gpio_irq_mask;
  510. tgi->ic.irq_unmask = tegra_gpio_irq_unmask;
  511. tgi->ic.irq_set_type = tegra_gpio_irq_set_type;
  512. tgi->ic.irq_shutdown = tegra_gpio_irq_shutdown;
  513. #ifdef CONFIG_PM_SLEEP
  514. tgi->ic.irq_set_wake = tegra_gpio_irq_set_wake;
  515. #endif
  516. platform_set_drvdata(pdev, tgi);
  517. if (config->debounce_supported)
  518. tgi->gc.set_config = tegra_gpio_set_config;
  519. tgi->bank_info = devm_kzalloc(&pdev->dev, tgi->bank_count *
  520. sizeof(*tgi->bank_info), GFP_KERNEL);
  521. if (!tgi->bank_info)
  522. return -ENODEV;
  523. tgi->irq_domain = irq_domain_add_linear(pdev->dev.of_node,
  524. tgi->gc.ngpio,
  525. &irq_domain_simple_ops, NULL);
  526. if (!tgi->irq_domain)
  527. return -ENODEV;
  528. for (i = 0; i < tgi->bank_count; i++) {
  529. res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
  530. if (!res) {
  531. dev_err(&pdev->dev, "Missing IRQ resource\n");
  532. return -ENODEV;
  533. }
  534. bank = &tgi->bank_info[i];
  535. bank->bank = i;
  536. bank->irq = res->start;
  537. bank->tgi = tgi;
  538. }
  539. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  540. tgi->regs = devm_ioremap_resource(&pdev->dev, res);
  541. if (IS_ERR(tgi->regs))
  542. return PTR_ERR(tgi->regs);
  543. for (i = 0; i < tgi->bank_count; i++) {
  544. for (j = 0; j < 4; j++) {
  545. int gpio = tegra_gpio_compose(i, j, 0);
  546. tegra_gpio_writel(tgi, 0x00, GPIO_INT_ENB(tgi, gpio));
  547. }
  548. }
  549. ret = devm_gpiochip_add_data(&pdev->dev, &tgi->gc, tgi);
  550. if (ret < 0) {
  551. irq_domain_remove(tgi->irq_domain);
  552. return ret;
  553. }
  554. for (gpio = 0; gpio < tgi->gc.ngpio; gpio++) {
  555. int irq = irq_create_mapping(tgi->irq_domain, gpio);
  556. /* No validity check; all Tegra GPIOs are valid IRQs */
  557. bank = &tgi->bank_info[GPIO_BANK(gpio)];
  558. irq_set_lockdep_class(irq, &gpio_lock_class);
  559. irq_set_chip_data(irq, bank);
  560. irq_set_chip_and_handler(irq, &tgi->ic, handle_simple_irq);
  561. }
  562. for (i = 0; i < tgi->bank_count; i++) {
  563. bank = &tgi->bank_info[i];
  564. irq_set_chained_handler_and_data(bank->irq,
  565. tegra_gpio_irq_handler, bank);
  566. for (j = 0; j < 4; j++) {
  567. spin_lock_init(&bank->lvl_lock[j]);
  568. spin_lock_init(&bank->dbc_lock[j]);
  569. }
  570. }
  571. tegra_gpio_debuginit(tgi);
  572. return 0;
  573. }
  574. static const struct tegra_gpio_soc_config tegra20_gpio_config = {
  575. .bank_stride = 0x80,
  576. .upper_offset = 0x800,
  577. };
  578. static const struct tegra_gpio_soc_config tegra30_gpio_config = {
  579. .bank_stride = 0x100,
  580. .upper_offset = 0x80,
  581. };
  582. static const struct tegra_gpio_soc_config tegra210_gpio_config = {
  583. .debounce_supported = true,
  584. .bank_stride = 0x100,
  585. .upper_offset = 0x80,
  586. };
  587. static const struct of_device_id tegra_gpio_of_match[] = {
  588. { .compatible = "nvidia,tegra210-gpio", .data = &tegra210_gpio_config },
  589. { .compatible = "nvidia,tegra30-gpio", .data = &tegra30_gpio_config },
  590. { .compatible = "nvidia,tegra20-gpio", .data = &tegra20_gpio_config },
  591. { },
  592. };
  593. static struct platform_driver tegra_gpio_driver = {
  594. .driver = {
  595. .name = "tegra-gpio",
  596. .pm = &tegra_gpio_pm_ops,
  597. .of_match_table = tegra_gpio_of_match,
  598. },
  599. .probe = tegra_gpio_probe,
  600. };
  601. static int __init tegra_gpio_init(void)
  602. {
  603. return platform_driver_register(&tegra_gpio_driver);
  604. }
  605. postcore_initcall(tegra_gpio_init);