pinctrl-tegra.c 17 KB

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