pinctrl-tegra.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /*
  2. * Driver for the NVIDIA Tegra pinmux
  3. *
  4. * Copyright (c) 2011-2012, 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.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/pinctrl/machine.h>
  27. #include <linux/pinctrl/pinctrl.h>
  28. #include <linux/pinctrl/pinmux.h>
  29. #include <linux/pinctrl/pinconf.h>
  30. #include <linux/slab.h>
  31. #include "../core.h"
  32. #include "../pinctrl-utils.h"
  33. #include "pinctrl-tegra.h"
  34. struct tegra_pmx {
  35. struct device *dev;
  36. struct pinctrl_dev *pctl;
  37. const struct tegra_pinctrl_soc_data *soc;
  38. const char **group_pins;
  39. int nbanks;
  40. void __iomem **regs;
  41. };
  42. static inline u32 pmx_readl(struct tegra_pmx *pmx, u32 bank, u32 reg)
  43. {
  44. return readl(pmx->regs[bank] + reg);
  45. }
  46. static inline void pmx_writel(struct tegra_pmx *pmx, u32 val, u32 bank, u32 reg)
  47. {
  48. writel_relaxed(val, pmx->regs[bank] + reg);
  49. /* make sure pinmux register write completed */
  50. pmx_readl(pmx, bank, reg);
  51. }
  52. static int tegra_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
  53. {
  54. struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  55. return pmx->soc->ngroups;
  56. }
  57. static const char *tegra_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
  58. unsigned group)
  59. {
  60. struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  61. return pmx->soc->groups[group].name;
  62. }
  63. static int tegra_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
  64. unsigned group,
  65. const unsigned **pins,
  66. unsigned *num_pins)
  67. {
  68. struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  69. *pins = pmx->soc->groups[group].pins;
  70. *num_pins = pmx->soc->groups[group].npins;
  71. return 0;
  72. }
  73. #ifdef CONFIG_DEBUG_FS
  74. static void tegra_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
  75. struct seq_file *s,
  76. unsigned offset)
  77. {
  78. seq_printf(s, " %s", dev_name(pctldev->dev));
  79. }
  80. #endif
  81. static const struct cfg_param {
  82. const char *property;
  83. enum tegra_pinconf_param param;
  84. } cfg_params[] = {
  85. {"nvidia,pull", TEGRA_PINCONF_PARAM_PULL},
  86. {"nvidia,tristate", TEGRA_PINCONF_PARAM_TRISTATE},
  87. {"nvidia,enable-input", TEGRA_PINCONF_PARAM_ENABLE_INPUT},
  88. {"nvidia,open-drain", TEGRA_PINCONF_PARAM_OPEN_DRAIN},
  89. {"nvidia,lock", TEGRA_PINCONF_PARAM_LOCK},
  90. {"nvidia,io-reset", TEGRA_PINCONF_PARAM_IORESET},
  91. {"nvidia,rcv-sel", TEGRA_PINCONF_PARAM_RCV_SEL},
  92. {"nvidia,io-hv", TEGRA_PINCONF_PARAM_RCV_SEL},
  93. {"nvidia,high-speed-mode", TEGRA_PINCONF_PARAM_HIGH_SPEED_MODE},
  94. {"nvidia,schmitt", TEGRA_PINCONF_PARAM_SCHMITT},
  95. {"nvidia,low-power-mode", TEGRA_PINCONF_PARAM_LOW_POWER_MODE},
  96. {"nvidia,pull-down-strength", TEGRA_PINCONF_PARAM_DRIVE_DOWN_STRENGTH},
  97. {"nvidia,pull-up-strength", TEGRA_PINCONF_PARAM_DRIVE_UP_STRENGTH},
  98. {"nvidia,slew-rate-falling", TEGRA_PINCONF_PARAM_SLEW_RATE_FALLING},
  99. {"nvidia,slew-rate-rising", TEGRA_PINCONF_PARAM_SLEW_RATE_RISING},
  100. {"nvidia,drive-type", TEGRA_PINCONF_PARAM_DRIVE_TYPE},
  101. };
  102. static int tegra_pinctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
  103. struct device_node *np,
  104. struct pinctrl_map **map,
  105. unsigned *reserved_maps,
  106. unsigned *num_maps)
  107. {
  108. struct device *dev = pctldev->dev;
  109. int ret, i;
  110. const char *function;
  111. u32 val;
  112. unsigned long config;
  113. unsigned long *configs = NULL;
  114. unsigned num_configs = 0;
  115. unsigned reserve;
  116. struct property *prop;
  117. const char *group;
  118. ret = of_property_read_string(np, "nvidia,function", &function);
  119. if (ret < 0) {
  120. /* EINVAL=missing, which is fine since it's optional */
  121. if (ret != -EINVAL)
  122. dev_err(dev,
  123. "could not parse property nvidia,function\n");
  124. function = NULL;
  125. }
  126. for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
  127. ret = of_property_read_u32(np, cfg_params[i].property, &val);
  128. if (!ret) {
  129. config = TEGRA_PINCONF_PACK(cfg_params[i].param, val);
  130. ret = pinctrl_utils_add_config(pctldev, &configs,
  131. &num_configs, config);
  132. if (ret < 0)
  133. goto exit;
  134. /* EINVAL=missing, which is fine since it's optional */
  135. } else if (ret != -EINVAL) {
  136. dev_err(dev, "could not parse property %s\n",
  137. cfg_params[i].property);
  138. }
  139. }
  140. reserve = 0;
  141. if (function != NULL)
  142. reserve++;
  143. if (num_configs)
  144. reserve++;
  145. ret = of_property_count_strings(np, "nvidia,pins");
  146. if (ret < 0) {
  147. dev_err(dev, "could not parse property nvidia,pins\n");
  148. goto exit;
  149. }
  150. reserve *= ret;
  151. ret = pinctrl_utils_reserve_map(pctldev, map, reserved_maps,
  152. num_maps, reserve);
  153. if (ret < 0)
  154. goto exit;
  155. of_property_for_each_string(np, "nvidia,pins", prop, group) {
  156. if (function) {
  157. ret = pinctrl_utils_add_map_mux(pctldev, map,
  158. reserved_maps, num_maps, group,
  159. function);
  160. if (ret < 0)
  161. goto exit;
  162. }
  163. if (num_configs) {
  164. ret = pinctrl_utils_add_map_configs(pctldev, map,
  165. reserved_maps, num_maps, group,
  166. configs, num_configs,
  167. PIN_MAP_TYPE_CONFIGS_GROUP);
  168. if (ret < 0)
  169. goto exit;
  170. }
  171. }
  172. ret = 0;
  173. exit:
  174. kfree(configs);
  175. return ret;
  176. }
  177. static int tegra_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
  178. struct device_node *np_config,
  179. struct pinctrl_map **map,
  180. unsigned *num_maps)
  181. {
  182. unsigned reserved_maps;
  183. struct device_node *np;
  184. int ret;
  185. reserved_maps = 0;
  186. *map = NULL;
  187. *num_maps = 0;
  188. for_each_child_of_node(np_config, np) {
  189. ret = tegra_pinctrl_dt_subnode_to_map(pctldev, np, map,
  190. &reserved_maps, num_maps);
  191. if (ret < 0) {
  192. pinctrl_utils_free_map(pctldev, *map,
  193. *num_maps);
  194. of_node_put(np);
  195. return ret;
  196. }
  197. }
  198. return 0;
  199. }
  200. static const struct pinctrl_ops tegra_pinctrl_ops = {
  201. .get_groups_count = tegra_pinctrl_get_groups_count,
  202. .get_group_name = tegra_pinctrl_get_group_name,
  203. .get_group_pins = tegra_pinctrl_get_group_pins,
  204. #ifdef CONFIG_DEBUG_FS
  205. .pin_dbg_show = tegra_pinctrl_pin_dbg_show,
  206. #endif
  207. .dt_node_to_map = tegra_pinctrl_dt_node_to_map,
  208. .dt_free_map = pinctrl_utils_free_map,
  209. };
  210. static int tegra_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev)
  211. {
  212. struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  213. return pmx->soc->nfunctions;
  214. }
  215. static const char *tegra_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
  216. unsigned function)
  217. {
  218. struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  219. return pmx->soc->functions[function].name;
  220. }
  221. static int tegra_pinctrl_get_func_groups(struct pinctrl_dev *pctldev,
  222. unsigned function,
  223. const char * const **groups,
  224. unsigned * const num_groups)
  225. {
  226. struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  227. *groups = pmx->soc->functions[function].groups;
  228. *num_groups = pmx->soc->functions[function].ngroups;
  229. return 0;
  230. }
  231. static int tegra_pinctrl_set_mux(struct pinctrl_dev *pctldev,
  232. unsigned function,
  233. unsigned group)
  234. {
  235. struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  236. const struct tegra_pingroup *g;
  237. int i;
  238. u32 val;
  239. g = &pmx->soc->groups[group];
  240. if (WARN_ON(g->mux_reg < 0))
  241. return -EINVAL;
  242. for (i = 0; i < ARRAY_SIZE(g->funcs); i++) {
  243. if (g->funcs[i] == function)
  244. break;
  245. }
  246. if (WARN_ON(i == ARRAY_SIZE(g->funcs)))
  247. return -EINVAL;
  248. val = pmx_readl(pmx, g->mux_bank, g->mux_reg);
  249. val &= ~(0x3 << g->mux_bit);
  250. val |= i << g->mux_bit;
  251. pmx_writel(pmx, val, g->mux_bank, g->mux_reg);
  252. return 0;
  253. }
  254. static const struct pinmux_ops tegra_pinmux_ops = {
  255. .get_functions_count = tegra_pinctrl_get_funcs_count,
  256. .get_function_name = tegra_pinctrl_get_func_name,
  257. .get_function_groups = tegra_pinctrl_get_func_groups,
  258. .set_mux = tegra_pinctrl_set_mux,
  259. };
  260. static int tegra_pinconf_reg(struct tegra_pmx *pmx,
  261. const struct tegra_pingroup *g,
  262. enum tegra_pinconf_param param,
  263. bool report_err,
  264. s8 *bank, s16 *reg, s8 *bit, s8 *width)
  265. {
  266. switch (param) {
  267. case TEGRA_PINCONF_PARAM_PULL:
  268. *bank = g->pupd_bank;
  269. *reg = g->pupd_reg;
  270. *bit = g->pupd_bit;
  271. *width = 2;
  272. break;
  273. case TEGRA_PINCONF_PARAM_TRISTATE:
  274. *bank = g->tri_bank;
  275. *reg = g->tri_reg;
  276. *bit = g->tri_bit;
  277. *width = 1;
  278. break;
  279. case TEGRA_PINCONF_PARAM_ENABLE_INPUT:
  280. *bank = g->mux_bank;
  281. *reg = g->mux_reg;
  282. *bit = g->einput_bit;
  283. *width = 1;
  284. break;
  285. case TEGRA_PINCONF_PARAM_OPEN_DRAIN:
  286. *bank = g->mux_bank;
  287. *reg = g->mux_reg;
  288. *bit = g->odrain_bit;
  289. *width = 1;
  290. break;
  291. case TEGRA_PINCONF_PARAM_LOCK:
  292. *bank = g->mux_bank;
  293. *reg = g->mux_reg;
  294. *bit = g->lock_bit;
  295. *width = 1;
  296. break;
  297. case TEGRA_PINCONF_PARAM_IORESET:
  298. *bank = g->mux_bank;
  299. *reg = g->mux_reg;
  300. *bit = g->ioreset_bit;
  301. *width = 1;
  302. break;
  303. case TEGRA_PINCONF_PARAM_RCV_SEL:
  304. *bank = g->mux_bank;
  305. *reg = g->mux_reg;
  306. *bit = g->rcv_sel_bit;
  307. *width = 1;
  308. break;
  309. case TEGRA_PINCONF_PARAM_HIGH_SPEED_MODE:
  310. if (pmx->soc->hsm_in_mux) {
  311. *bank = g->mux_bank;
  312. *reg = g->mux_reg;
  313. } else {
  314. *bank = g->drv_bank;
  315. *reg = g->drv_reg;
  316. }
  317. *bit = g->hsm_bit;
  318. *width = 1;
  319. break;
  320. case TEGRA_PINCONF_PARAM_SCHMITT:
  321. if (pmx->soc->schmitt_in_mux) {
  322. *bank = g->mux_bank;
  323. *reg = g->mux_reg;
  324. } else {
  325. *bank = g->drv_bank;
  326. *reg = g->drv_reg;
  327. }
  328. *bit = g->schmitt_bit;
  329. *width = 1;
  330. break;
  331. case TEGRA_PINCONF_PARAM_LOW_POWER_MODE:
  332. *bank = g->drv_bank;
  333. *reg = g->drv_reg;
  334. *bit = g->lpmd_bit;
  335. *width = 2;
  336. break;
  337. case TEGRA_PINCONF_PARAM_DRIVE_DOWN_STRENGTH:
  338. *bank = g->drv_bank;
  339. *reg = g->drv_reg;
  340. *bit = g->drvdn_bit;
  341. *width = g->drvdn_width;
  342. break;
  343. case TEGRA_PINCONF_PARAM_DRIVE_UP_STRENGTH:
  344. *bank = g->drv_bank;
  345. *reg = g->drv_reg;
  346. *bit = g->drvup_bit;
  347. *width = g->drvup_width;
  348. break;
  349. case TEGRA_PINCONF_PARAM_SLEW_RATE_FALLING:
  350. *bank = g->drv_bank;
  351. *reg = g->drv_reg;
  352. *bit = g->slwf_bit;
  353. *width = g->slwf_width;
  354. break;
  355. case TEGRA_PINCONF_PARAM_SLEW_RATE_RISING:
  356. *bank = g->drv_bank;
  357. *reg = g->drv_reg;
  358. *bit = g->slwr_bit;
  359. *width = g->slwr_width;
  360. break;
  361. case TEGRA_PINCONF_PARAM_DRIVE_TYPE:
  362. if (pmx->soc->drvtype_in_mux) {
  363. *bank = g->mux_bank;
  364. *reg = g->mux_reg;
  365. } else {
  366. *bank = g->drv_bank;
  367. *reg = g->drv_reg;
  368. }
  369. *bit = g->drvtype_bit;
  370. *width = 2;
  371. break;
  372. default:
  373. dev_err(pmx->dev, "Invalid config param %04x\n", param);
  374. return -ENOTSUPP;
  375. }
  376. if (*reg < 0 || *bit < 0) {
  377. if (report_err) {
  378. const char *prop = "unknown";
  379. int i;
  380. for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
  381. if (cfg_params[i].param == param) {
  382. prop = cfg_params[i].property;
  383. break;
  384. }
  385. }
  386. dev_err(pmx->dev,
  387. "Config param %04x (%s) not supported on group %s\n",
  388. param, prop, g->name);
  389. }
  390. return -ENOTSUPP;
  391. }
  392. return 0;
  393. }
  394. static int tegra_pinconf_get(struct pinctrl_dev *pctldev,
  395. unsigned pin, unsigned long *config)
  396. {
  397. dev_err(pctldev->dev, "pin_config_get op not supported\n");
  398. return -ENOTSUPP;
  399. }
  400. static int tegra_pinconf_set(struct pinctrl_dev *pctldev,
  401. unsigned pin, unsigned long *configs,
  402. unsigned num_configs)
  403. {
  404. dev_err(pctldev->dev, "pin_config_set op not supported\n");
  405. return -ENOTSUPP;
  406. }
  407. static int tegra_pinconf_group_get(struct pinctrl_dev *pctldev,
  408. unsigned group, unsigned long *config)
  409. {
  410. struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  411. enum tegra_pinconf_param param = TEGRA_PINCONF_UNPACK_PARAM(*config);
  412. u16 arg;
  413. const struct tegra_pingroup *g;
  414. int ret;
  415. s8 bank, bit, width;
  416. s16 reg;
  417. u32 val, mask;
  418. g = &pmx->soc->groups[group];
  419. ret = tegra_pinconf_reg(pmx, g, param, true, &bank, &reg, &bit,
  420. &width);
  421. if (ret < 0)
  422. return ret;
  423. val = pmx_readl(pmx, bank, reg);
  424. mask = (1 << width) - 1;
  425. arg = (val >> bit) & mask;
  426. *config = TEGRA_PINCONF_PACK(param, arg);
  427. return 0;
  428. }
  429. static int tegra_pinconf_group_set(struct pinctrl_dev *pctldev,
  430. unsigned group, unsigned long *configs,
  431. unsigned num_configs)
  432. {
  433. struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  434. enum tegra_pinconf_param param;
  435. u16 arg;
  436. const struct tegra_pingroup *g;
  437. int ret, i;
  438. s8 bank, bit, width;
  439. s16 reg;
  440. u32 val, mask;
  441. g = &pmx->soc->groups[group];
  442. for (i = 0; i < num_configs; i++) {
  443. param = TEGRA_PINCONF_UNPACK_PARAM(configs[i]);
  444. arg = TEGRA_PINCONF_UNPACK_ARG(configs[i]);
  445. ret = tegra_pinconf_reg(pmx, g, param, true, &bank, &reg, &bit,
  446. &width);
  447. if (ret < 0)
  448. return ret;
  449. val = pmx_readl(pmx, bank, reg);
  450. /* LOCK can't be cleared */
  451. if (param == TEGRA_PINCONF_PARAM_LOCK) {
  452. if ((val & BIT(bit)) && !arg) {
  453. dev_err(pctldev->dev, "LOCK bit cannot be cleared\n");
  454. return -EINVAL;
  455. }
  456. }
  457. /* Special-case Boolean values; allow any non-zero as true */
  458. if (width == 1)
  459. arg = !!arg;
  460. /* Range-check user-supplied value */
  461. mask = (1 << width) - 1;
  462. if (arg & ~mask) {
  463. dev_err(pctldev->dev,
  464. "config %lx: %x too big for %d bit register\n",
  465. configs[i], arg, width);
  466. return -EINVAL;
  467. }
  468. /* Update register */
  469. val &= ~(mask << bit);
  470. val |= arg << bit;
  471. pmx_writel(pmx, val, bank, reg);
  472. } /* for each config */
  473. return 0;
  474. }
  475. #ifdef CONFIG_DEBUG_FS
  476. static void tegra_pinconf_dbg_show(struct pinctrl_dev *pctldev,
  477. struct seq_file *s, unsigned offset)
  478. {
  479. }
  480. static const char *strip_prefix(const char *s)
  481. {
  482. const char *comma = strchr(s, ',');
  483. if (!comma)
  484. return s;
  485. return comma + 1;
  486. }
  487. static void tegra_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
  488. struct seq_file *s, unsigned group)
  489. {
  490. struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  491. const struct tegra_pingroup *g;
  492. int i, ret;
  493. s8 bank, bit, width;
  494. s16 reg;
  495. u32 val;
  496. g = &pmx->soc->groups[group];
  497. for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
  498. ret = tegra_pinconf_reg(pmx, g, cfg_params[i].param, false,
  499. &bank, &reg, &bit, &width);
  500. if (ret < 0)
  501. continue;
  502. val = pmx_readl(pmx, bank, reg);
  503. val >>= bit;
  504. val &= (1 << width) - 1;
  505. seq_printf(s, "\n\t%s=%u",
  506. strip_prefix(cfg_params[i].property), val);
  507. }
  508. }
  509. static void tegra_pinconf_config_dbg_show(struct pinctrl_dev *pctldev,
  510. struct seq_file *s,
  511. unsigned long config)
  512. {
  513. enum tegra_pinconf_param param = TEGRA_PINCONF_UNPACK_PARAM(config);
  514. u16 arg = TEGRA_PINCONF_UNPACK_ARG(config);
  515. const char *pname = "unknown";
  516. int i;
  517. for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
  518. if (cfg_params[i].param == param) {
  519. pname = cfg_params[i].property;
  520. break;
  521. }
  522. }
  523. seq_printf(s, "%s=%d", strip_prefix(pname), arg);
  524. }
  525. #endif
  526. static const struct pinconf_ops tegra_pinconf_ops = {
  527. .pin_config_get = tegra_pinconf_get,
  528. .pin_config_set = tegra_pinconf_set,
  529. .pin_config_group_get = tegra_pinconf_group_get,
  530. .pin_config_group_set = tegra_pinconf_group_set,
  531. #ifdef CONFIG_DEBUG_FS
  532. .pin_config_dbg_show = tegra_pinconf_dbg_show,
  533. .pin_config_group_dbg_show = tegra_pinconf_group_dbg_show,
  534. .pin_config_config_dbg_show = tegra_pinconf_config_dbg_show,
  535. #endif
  536. };
  537. static struct pinctrl_gpio_range tegra_pinctrl_gpio_range = {
  538. .name = "Tegra GPIOs",
  539. .id = 0,
  540. .base = 0,
  541. };
  542. static struct pinctrl_desc tegra_pinctrl_desc = {
  543. .pctlops = &tegra_pinctrl_ops,
  544. .pmxops = &tegra_pinmux_ops,
  545. .confops = &tegra_pinconf_ops,
  546. .owner = THIS_MODULE,
  547. };
  548. static void tegra_pinctrl_clear_parked_bits(struct tegra_pmx *pmx)
  549. {
  550. int i = 0;
  551. const struct tegra_pingroup *g;
  552. u32 val;
  553. for (i = 0; i < pmx->soc->ngroups; ++i) {
  554. g = &pmx->soc->groups[i];
  555. if (g->parked_bit >= 0) {
  556. val = pmx_readl(pmx, g->mux_bank, g->mux_reg);
  557. val &= ~(1 << g->parked_bit);
  558. pmx_writel(pmx, val, g->mux_bank, g->mux_reg);
  559. }
  560. }
  561. }
  562. static bool gpio_node_has_range(void)
  563. {
  564. struct device_node *np;
  565. bool has_prop = false;
  566. np = of_find_compatible_node(NULL, NULL, "nvidia,tegra30-gpio");
  567. if (!np)
  568. return has_prop;
  569. has_prop = of_find_property(np, "gpio-ranges", NULL);
  570. of_node_put(np);
  571. return has_prop;
  572. }
  573. int tegra_pinctrl_probe(struct platform_device *pdev,
  574. const struct tegra_pinctrl_soc_data *soc_data)
  575. {
  576. struct tegra_pmx *pmx;
  577. struct resource *res;
  578. int i;
  579. const char **group_pins;
  580. int fn, gn, gfn;
  581. pmx = devm_kzalloc(&pdev->dev, sizeof(*pmx), GFP_KERNEL);
  582. if (!pmx) {
  583. dev_err(&pdev->dev, "Can't alloc tegra_pmx\n");
  584. return -ENOMEM;
  585. }
  586. pmx->dev = &pdev->dev;
  587. pmx->soc = soc_data;
  588. /*
  589. * Each mux group will appear in 4 functions' list of groups.
  590. * This over-allocates slightly, since not all groups are mux groups.
  591. */
  592. pmx->group_pins = devm_kzalloc(&pdev->dev,
  593. soc_data->ngroups * 4 * sizeof(*pmx->group_pins),
  594. GFP_KERNEL);
  595. if (!pmx->group_pins)
  596. return -ENOMEM;
  597. group_pins = pmx->group_pins;
  598. for (fn = 0; fn < soc_data->nfunctions; fn++) {
  599. struct tegra_function *func = &soc_data->functions[fn];
  600. func->groups = group_pins;
  601. for (gn = 0; gn < soc_data->ngroups; gn++) {
  602. const struct tegra_pingroup *g = &soc_data->groups[gn];
  603. if (g->mux_reg == -1)
  604. continue;
  605. for (gfn = 0; gfn < 4; gfn++)
  606. if (g->funcs[gfn] == fn)
  607. break;
  608. if (gfn == 4)
  609. continue;
  610. BUG_ON(group_pins - pmx->group_pins >=
  611. soc_data->ngroups * 4);
  612. *group_pins++ = g->name;
  613. func->ngroups++;
  614. }
  615. }
  616. tegra_pinctrl_gpio_range.npins = pmx->soc->ngpios;
  617. tegra_pinctrl_desc.name = dev_name(&pdev->dev);
  618. tegra_pinctrl_desc.pins = pmx->soc->pins;
  619. tegra_pinctrl_desc.npins = pmx->soc->npins;
  620. for (i = 0; ; i++) {
  621. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  622. if (!res)
  623. break;
  624. }
  625. pmx->nbanks = i;
  626. pmx->regs = devm_kzalloc(&pdev->dev, pmx->nbanks * sizeof(*pmx->regs),
  627. GFP_KERNEL);
  628. if (!pmx->regs) {
  629. dev_err(&pdev->dev, "Can't alloc regs pointer\n");
  630. return -ENOMEM;
  631. }
  632. for (i = 0; i < pmx->nbanks; i++) {
  633. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  634. pmx->regs[i] = devm_ioremap_resource(&pdev->dev, res);
  635. if (IS_ERR(pmx->regs[i]))
  636. return PTR_ERR(pmx->regs[i]);
  637. }
  638. pmx->pctl = devm_pinctrl_register(&pdev->dev, &tegra_pinctrl_desc, pmx);
  639. if (IS_ERR(pmx->pctl)) {
  640. dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
  641. return PTR_ERR(pmx->pctl);
  642. }
  643. tegra_pinctrl_clear_parked_bits(pmx);
  644. if (!gpio_node_has_range())
  645. pinctrl_add_gpio_range(pmx->pctl, &tegra_pinctrl_gpio_range);
  646. platform_set_drvdata(pdev, pmx);
  647. dev_dbg(&pdev->dev, "Probed Tegra pinctrl driver\n");
  648. return 0;
  649. }
  650. EXPORT_SYMBOL_GPL(tegra_pinctrl_probe);