pinctrl-tegra.c 18 KB

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