pinctrl-tegra.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /*
  2. * Driver for the NVIDIA Tegra pinmux
  3. *
  4. * Copyright (c) 2011, NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * Derived from code:
  7. * Copyright (C) 2010 Google, Inc.
  8. * Copyright (C) 2010 NVIDIA Corporation
  9. * Copyright (C) 2009-2011 ST-Ericsson AB
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms and conditions of the GNU General Public License,
  13. * version 2, as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  18. * more details.
  19. */
  20. #include <linux/err.h>
  21. #include <linux/init.h>
  22. #include <linux/io.h>
  23. #include <linux/module.h>
  24. #include <linux/of_device.h>
  25. #include <linux/pinctrl/pinctrl.h>
  26. #include <linux/pinctrl/pinmux.h>
  27. #include <linux/pinctrl/pinconf.h>
  28. #include <mach/pinconf-tegra.h>
  29. #include "pinctrl-tegra.h"
  30. #define DRIVER_NAME "tegra-pinmux-disabled"
  31. struct tegra_pmx {
  32. struct device *dev;
  33. struct pinctrl_dev *pctl;
  34. const struct tegra_pinctrl_soc_data *soc;
  35. int nbanks;
  36. void __iomem **regs;
  37. };
  38. static inline u32 pmx_readl(struct tegra_pmx *pmx, u32 bank, u32 reg)
  39. {
  40. return readl(pmx->regs[bank] + reg);
  41. }
  42. static inline void pmx_writel(struct tegra_pmx *pmx, u32 val, u32 bank, u32 reg)
  43. {
  44. writel(val, pmx->regs[bank] + reg);
  45. }
  46. static int tegra_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
  47. {
  48. struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  49. return pmx->soc->ngroups;
  50. }
  51. static const char *tegra_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
  52. unsigned group)
  53. {
  54. struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  55. return pmx->soc->groups[group].name;
  56. }
  57. static int tegra_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
  58. unsigned group,
  59. const unsigned **pins,
  60. unsigned *num_pins)
  61. {
  62. struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  63. *pins = pmx->soc->groups[group].pins;
  64. *num_pins = pmx->soc->groups[group].npins;
  65. return 0;
  66. }
  67. static void tegra_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
  68. struct seq_file *s,
  69. unsigned offset)
  70. {
  71. seq_printf(s, " " DRIVER_NAME);
  72. }
  73. static struct pinctrl_ops tegra_pinctrl_ops = {
  74. .get_groups_count = tegra_pinctrl_get_groups_count,
  75. .get_group_name = tegra_pinctrl_get_group_name,
  76. .get_group_pins = tegra_pinctrl_get_group_pins,
  77. .pin_dbg_show = tegra_pinctrl_pin_dbg_show,
  78. };
  79. static int tegra_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev)
  80. {
  81. struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  82. return pmx->soc->nfunctions;
  83. }
  84. static const char *tegra_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
  85. unsigned function)
  86. {
  87. struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  88. return pmx->soc->functions[function].name;
  89. }
  90. static int tegra_pinctrl_get_func_groups(struct pinctrl_dev *pctldev,
  91. unsigned function,
  92. const char * const **groups,
  93. unsigned * const num_groups)
  94. {
  95. struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  96. *groups = pmx->soc->functions[function].groups;
  97. *num_groups = pmx->soc->functions[function].ngroups;
  98. return 0;
  99. }
  100. static int tegra_pinctrl_enable(struct pinctrl_dev *pctldev, unsigned function,
  101. unsigned group)
  102. {
  103. struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  104. const struct tegra_pingroup *g;
  105. int i;
  106. u32 val;
  107. g = &pmx->soc->groups[group];
  108. if (g->mux_reg < 0)
  109. return -EINVAL;
  110. for (i = 0; i < ARRAY_SIZE(g->funcs); i++) {
  111. if (g->funcs[i] == function)
  112. break;
  113. }
  114. if (i == ARRAY_SIZE(g->funcs))
  115. return -EINVAL;
  116. val = pmx_readl(pmx, g->mux_bank, g->mux_reg);
  117. val &= ~(0x3 << g->mux_bit);
  118. val |= i << g->mux_bit;
  119. pmx_writel(pmx, val, g->mux_bank, g->mux_reg);
  120. return 0;
  121. }
  122. static void tegra_pinctrl_disable(struct pinctrl_dev *pctldev,
  123. unsigned function, unsigned group)
  124. {
  125. struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  126. const struct tegra_pingroup *g;
  127. u32 val;
  128. g = &pmx->soc->groups[group];
  129. if (g->mux_reg < 0)
  130. return;
  131. val = pmx_readl(pmx, g->mux_bank, g->mux_reg);
  132. val &= ~(0x3 << g->mux_bit);
  133. val |= g->func_safe << g->mux_bit;
  134. pmx_writel(pmx, val, g->mux_bank, g->mux_reg);
  135. }
  136. static struct pinmux_ops tegra_pinmux_ops = {
  137. .get_functions_count = tegra_pinctrl_get_funcs_count,
  138. .get_function_name = tegra_pinctrl_get_func_name,
  139. .get_function_groups = tegra_pinctrl_get_func_groups,
  140. .enable = tegra_pinctrl_enable,
  141. .disable = tegra_pinctrl_disable,
  142. };
  143. static int tegra_pinconf_reg(struct tegra_pmx *pmx,
  144. const struct tegra_pingroup *g,
  145. enum tegra_pinconf_param param,
  146. s8 *bank, s16 *reg, s8 *bit, s8 *width)
  147. {
  148. switch (param) {
  149. case TEGRA_PINCONF_PARAM_PULL:
  150. *bank = g->pupd_bank;
  151. *reg = g->pupd_reg;
  152. *bit = g->pupd_bit;
  153. *width = 2;
  154. break;
  155. case TEGRA_PINCONF_PARAM_TRISTATE:
  156. *bank = g->tri_bank;
  157. *reg = g->tri_reg;
  158. *bit = g->tri_bit;
  159. *width = 1;
  160. break;
  161. case TEGRA_PINCONF_PARAM_ENABLE_INPUT:
  162. *bank = g->einput_bank;
  163. *reg = g->einput_reg;
  164. *bit = g->einput_bit;
  165. *width = 1;
  166. break;
  167. case TEGRA_PINCONF_PARAM_OPEN_DRAIN:
  168. *bank = g->odrain_bank;
  169. *reg = g->odrain_reg;
  170. *bit = g->odrain_bit;
  171. *width = 1;
  172. break;
  173. case TEGRA_PINCONF_PARAM_LOCK:
  174. *bank = g->lock_bank;
  175. *reg = g->lock_reg;
  176. *bit = g->lock_bit;
  177. *width = 1;
  178. break;
  179. case TEGRA_PINCONF_PARAM_IORESET:
  180. *bank = g->ioreset_bank;
  181. *reg = g->ioreset_reg;
  182. *bit = g->ioreset_bit;
  183. *width = 1;
  184. break;
  185. case TEGRA_PINCONF_PARAM_HIGH_SPEED_MODE:
  186. *bank = g->drv_bank;
  187. *reg = g->drv_reg;
  188. *bit = g->hsm_bit;
  189. *width = 1;
  190. break;
  191. case TEGRA_PINCONF_PARAM_SCHMITT:
  192. *bank = g->drv_bank;
  193. *reg = g->drv_reg;
  194. *bit = g->schmitt_bit;
  195. *width = 1;
  196. break;
  197. case TEGRA_PINCONF_PARAM_LOW_POWER_MODE:
  198. *bank = g->drv_bank;
  199. *reg = g->drv_reg;
  200. *bit = g->lpmd_bit;
  201. *width = 2;
  202. break;
  203. case TEGRA_PINCONF_PARAM_DRIVE_DOWN_STRENGTH:
  204. *bank = g->drv_bank;
  205. *reg = g->drv_reg;
  206. *bit = g->drvdn_bit;
  207. *width = g->drvdn_width;
  208. break;
  209. case TEGRA_PINCONF_PARAM_DRIVE_UP_STRENGTH:
  210. *bank = g->drv_bank;
  211. *reg = g->drv_reg;
  212. *bit = g->drvup_bit;
  213. *width = g->drvup_width;
  214. break;
  215. case TEGRA_PINCONF_PARAM_SLEW_RATE_FALLING:
  216. *bank = g->drv_bank;
  217. *reg = g->drv_reg;
  218. *bit = g->slwf_bit;
  219. *width = g->slwf_width;
  220. break;
  221. case TEGRA_PINCONF_PARAM_SLEW_RATE_RISING:
  222. *bank = g->drv_bank;
  223. *reg = g->drv_reg;
  224. *bit = g->slwr_bit;
  225. *width = g->slwr_width;
  226. break;
  227. default:
  228. dev_err(pmx->dev, "Invalid config param %04x\n", param);
  229. return -ENOTSUPP;
  230. }
  231. if (*reg < 0) {
  232. dev_err(pmx->dev,
  233. "Config param %04x not supported on group %s\n",
  234. param, g->name);
  235. return -ENOTSUPP;
  236. }
  237. return 0;
  238. }
  239. static int tegra_pinconf_get(struct pinctrl_dev *pctldev,
  240. unsigned pin, unsigned long *config)
  241. {
  242. return -ENOTSUPP;
  243. }
  244. static int tegra_pinconf_set(struct pinctrl_dev *pctldev,
  245. unsigned pin, unsigned long config)
  246. {
  247. return -ENOTSUPP;
  248. }
  249. static int tegra_pinconf_group_get(struct pinctrl_dev *pctldev,
  250. unsigned group, unsigned long *config)
  251. {
  252. struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  253. enum tegra_pinconf_param param = TEGRA_PINCONF_UNPACK_PARAM(*config);
  254. u16 arg;
  255. const struct tegra_pingroup *g;
  256. int ret;
  257. s8 bank, bit, width;
  258. s16 reg;
  259. u32 val, mask;
  260. g = &pmx->soc->groups[group];
  261. ret = tegra_pinconf_reg(pmx, g, param, &bank, &reg, &bit, &width);
  262. if (ret < 0)
  263. return ret;
  264. val = pmx_readl(pmx, bank, reg);
  265. mask = (1 << width) - 1;
  266. arg = (val >> bit) & mask;
  267. *config = TEGRA_PINCONF_PACK(param, arg);
  268. return 0;
  269. }
  270. static int tegra_pinconf_group_set(struct pinctrl_dev *pctldev,
  271. unsigned group, unsigned long config)
  272. {
  273. struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  274. enum tegra_pinconf_param param = TEGRA_PINCONF_UNPACK_PARAM(config);
  275. u16 arg = TEGRA_PINCONF_UNPACK_ARG(config);
  276. const struct tegra_pingroup *g;
  277. int ret;
  278. s8 bank, bit, width;
  279. s16 reg;
  280. u32 val, mask;
  281. g = &pmx->soc->groups[group];
  282. ret = tegra_pinconf_reg(pmx, g, param, &bank, &reg, &bit, &width);
  283. if (ret < 0)
  284. return ret;
  285. val = pmx_readl(pmx, bank, reg);
  286. /* LOCK can't be cleared */
  287. if (param == TEGRA_PINCONF_PARAM_LOCK) {
  288. if ((val & BIT(bit)) && !arg)
  289. return -EINVAL;
  290. }
  291. /* Special-case Boolean values; allow any non-zero as true */
  292. if (width == 1)
  293. arg = !!arg;
  294. /* Range-check user-supplied value */
  295. mask = (1 << width) - 1;
  296. if (arg & ~mask)
  297. return -EINVAL;
  298. /* Update register */
  299. val &= ~(mask << bit);
  300. val |= arg << bit;
  301. pmx_writel(pmx, val, bank, reg);
  302. return 0;
  303. }
  304. static void tegra_pinconf_dbg_show(struct pinctrl_dev *pctldev,
  305. struct seq_file *s, unsigned offset)
  306. {
  307. }
  308. static void tegra_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
  309. struct seq_file *s, unsigned selector)
  310. {
  311. }
  312. struct pinconf_ops tegra_pinconf_ops = {
  313. .pin_config_get = tegra_pinconf_get,
  314. .pin_config_set = tegra_pinconf_set,
  315. .pin_config_group_get = tegra_pinconf_group_get,
  316. .pin_config_group_set = tegra_pinconf_group_set,
  317. .pin_config_dbg_show = tegra_pinconf_dbg_show,
  318. .pin_config_group_dbg_show = tegra_pinconf_group_dbg_show,
  319. };
  320. static struct pinctrl_gpio_range tegra_pinctrl_gpio_range = {
  321. .name = "Tegra GPIOs",
  322. .id = 0,
  323. .base = 0,
  324. };
  325. static struct pinctrl_desc tegra_pinctrl_desc = {
  326. .name = DRIVER_NAME,
  327. .pctlops = &tegra_pinctrl_ops,
  328. .pmxops = &tegra_pinmux_ops,
  329. .confops = &tegra_pinconf_ops,
  330. .owner = THIS_MODULE,
  331. };
  332. static struct of_device_id tegra_pinctrl_of_match[] __devinitdata = {
  333. #ifdef CONFIG_PINCTRL_TEGRA20
  334. {
  335. .compatible = "nvidia,tegra20-pinmux-disabled",
  336. .data = tegra20_pinctrl_init,
  337. },
  338. #endif
  339. #ifdef CONFIG_PINCTRL_TEGRA30
  340. {
  341. .compatible = "nvidia,tegra30-pinmux-disabled",
  342. .data = tegra30_pinctrl_init,
  343. },
  344. #endif
  345. {},
  346. };
  347. static int __devinit tegra_pinctrl_probe(struct platform_device *pdev)
  348. {
  349. const struct of_device_id *match;
  350. tegra_pinctrl_soc_initf initf = NULL;
  351. struct tegra_pmx *pmx;
  352. struct resource *res;
  353. int i;
  354. match = of_match_device(tegra_pinctrl_of_match, &pdev->dev);
  355. if (match)
  356. initf = (tegra_pinctrl_soc_initf)match->data;
  357. #ifdef CONFIG_PINCTRL_TEGRA20
  358. if (!initf)
  359. initf = tegra20_pinctrl_init;
  360. #endif
  361. if (!initf) {
  362. dev_err(&pdev->dev,
  363. "Could not determine SoC-specific init func\n");
  364. return -EINVAL;
  365. }
  366. pmx = devm_kzalloc(&pdev->dev, sizeof(*pmx), GFP_KERNEL);
  367. if (!pmx) {
  368. dev_err(&pdev->dev, "Can't alloc tegra_pmx\n");
  369. return -ENOMEM;
  370. }
  371. pmx->dev = &pdev->dev;
  372. (*initf)(&pmx->soc);
  373. tegra_pinctrl_gpio_range.npins = pmx->soc->ngpios;
  374. tegra_pinctrl_desc.pins = pmx->soc->pins;
  375. tegra_pinctrl_desc.npins = pmx->soc->npins;
  376. for (i = 0; ; i++) {
  377. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  378. if (!res)
  379. break;
  380. }
  381. pmx->nbanks = i;
  382. pmx->regs = devm_kzalloc(&pdev->dev, pmx->nbanks * sizeof(*pmx->regs),
  383. GFP_KERNEL);
  384. if (!pmx->regs) {
  385. dev_err(&pdev->dev, "Can't alloc regs pointer\n");
  386. return -ENODEV;
  387. }
  388. for (i = 0; i < pmx->nbanks; i++) {
  389. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  390. if (!res) {
  391. dev_err(&pdev->dev, "Missing MEM resource\n");
  392. return -ENODEV;
  393. }
  394. if (!devm_request_mem_region(&pdev->dev, res->start,
  395. resource_size(res),
  396. dev_name(&pdev->dev))) {
  397. dev_err(&pdev->dev,
  398. "Couldn't request MEM resource %d\n", i);
  399. return -ENODEV;
  400. }
  401. pmx->regs[i] = devm_ioremap(&pdev->dev, res->start,
  402. resource_size(res));
  403. if (!pmx->regs[i]) {
  404. dev_err(&pdev->dev, "Couldn't ioremap regs %d\n", i);
  405. return -ENODEV;
  406. }
  407. }
  408. pmx->pctl = pinctrl_register(&tegra_pinctrl_desc, &pdev->dev, pmx);
  409. if (IS_ERR(pmx->pctl)) {
  410. dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
  411. return PTR_ERR(pmx->pctl);
  412. }
  413. pinctrl_add_gpio_range(pmx->pctl, &tegra_pinctrl_gpio_range);
  414. platform_set_drvdata(pdev, pmx);
  415. dev_dbg(&pdev->dev, "Probed Tegra pinctrl driver\n");
  416. return 0;
  417. }
  418. static int __devexit tegra_pinctrl_remove(struct platform_device *pdev)
  419. {
  420. struct tegra_pmx *pmx = platform_get_drvdata(pdev);
  421. pinctrl_unregister(pmx->pctl);
  422. return 0;
  423. }
  424. static struct platform_driver tegra_pinctrl_driver = {
  425. .driver = {
  426. .name = DRIVER_NAME,
  427. .owner = THIS_MODULE,
  428. .of_match_table = tegra_pinctrl_of_match,
  429. },
  430. .probe = tegra_pinctrl_probe,
  431. .remove = __devexit_p(tegra_pinctrl_remove),
  432. };
  433. static int __init tegra_pinctrl_init(void)
  434. {
  435. return platform_driver_register(&tegra_pinctrl_driver);
  436. }
  437. arch_initcall(tegra_pinctrl_init);
  438. static void __exit tegra_pinctrl_exit(void)
  439. {
  440. platform_driver_unregister(&tegra_pinctrl_driver);
  441. }
  442. module_exit(tegra_pinctrl_exit);
  443. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  444. MODULE_DESCRIPTION("NVIDIA Tegra pinctrl driver");
  445. MODULE_LICENSE("GPL v2");
  446. MODULE_DEVICE_TABLE(of, tegra_pinctrl_of_match);