pinctrl-tegra.c 18 KB

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