pinctrl-tegra.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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 void tegra_pinctrl_disable(struct pinctrl_dev *pctldev,
  250. unsigned function, unsigned group)
  251. {
  252. struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  253. const struct tegra_pingroup *g;
  254. g = &pmx->soc->groups[group];
  255. if (WARN_ON(g->mux_reg < 0))
  256. return;
  257. }
  258. static const struct pinmux_ops tegra_pinmux_ops = {
  259. .get_functions_count = tegra_pinctrl_get_funcs_count,
  260. .get_function_name = tegra_pinctrl_get_func_name,
  261. .get_function_groups = tegra_pinctrl_get_func_groups,
  262. .enable = tegra_pinctrl_enable,
  263. .disable = tegra_pinctrl_disable,
  264. };
  265. static int tegra_pinconf_reg(struct tegra_pmx *pmx,
  266. const struct tegra_pingroup *g,
  267. enum tegra_pinconf_param param,
  268. bool report_err,
  269. s8 *bank, s16 *reg, s8 *bit, s8 *width)
  270. {
  271. switch (param) {
  272. case TEGRA_PINCONF_PARAM_PULL:
  273. *bank = g->pupd_bank;
  274. *reg = g->pupd_reg;
  275. *bit = g->pupd_bit;
  276. *width = 2;
  277. break;
  278. case TEGRA_PINCONF_PARAM_TRISTATE:
  279. *bank = g->tri_bank;
  280. *reg = g->tri_reg;
  281. *bit = g->tri_bit;
  282. *width = 1;
  283. break;
  284. case TEGRA_PINCONF_PARAM_ENABLE_INPUT:
  285. *bank = g->mux_bank;
  286. *reg = g->mux_reg;
  287. *bit = g->einput_bit;
  288. *width = 1;
  289. break;
  290. case TEGRA_PINCONF_PARAM_OPEN_DRAIN:
  291. *bank = g->mux_bank;
  292. *reg = g->mux_reg;
  293. *bit = g->odrain_bit;
  294. *width = 1;
  295. break;
  296. case TEGRA_PINCONF_PARAM_LOCK:
  297. *bank = g->mux_bank;
  298. *reg = g->mux_reg;
  299. *bit = g->lock_bit;
  300. *width = 1;
  301. break;
  302. case TEGRA_PINCONF_PARAM_IORESET:
  303. *bank = g->mux_bank;
  304. *reg = g->mux_reg;
  305. *bit = g->ioreset_bit;
  306. *width = 1;
  307. break;
  308. case TEGRA_PINCONF_PARAM_RCV_SEL:
  309. *bank = g->mux_bank;
  310. *reg = g->mux_reg;
  311. *bit = g->rcv_sel_bit;
  312. *width = 1;
  313. break;
  314. case TEGRA_PINCONF_PARAM_HIGH_SPEED_MODE:
  315. *bank = g->drv_bank;
  316. *reg = g->drv_reg;
  317. *bit = g->hsm_bit;
  318. *width = 1;
  319. break;
  320. case TEGRA_PINCONF_PARAM_SCHMITT:
  321. *bank = g->drv_bank;
  322. *reg = g->drv_reg;
  323. *bit = g->schmitt_bit;
  324. *width = 1;
  325. break;
  326. case TEGRA_PINCONF_PARAM_LOW_POWER_MODE:
  327. *bank = g->drv_bank;
  328. *reg = g->drv_reg;
  329. *bit = g->lpmd_bit;
  330. *width = 2;
  331. break;
  332. case TEGRA_PINCONF_PARAM_DRIVE_DOWN_STRENGTH:
  333. *bank = g->drv_bank;
  334. *reg = g->drv_reg;
  335. *bit = g->drvdn_bit;
  336. *width = g->drvdn_width;
  337. break;
  338. case TEGRA_PINCONF_PARAM_DRIVE_UP_STRENGTH:
  339. *bank = g->drv_bank;
  340. *reg = g->drv_reg;
  341. *bit = g->drvup_bit;
  342. *width = g->drvup_width;
  343. break;
  344. case TEGRA_PINCONF_PARAM_SLEW_RATE_FALLING:
  345. *bank = g->drv_bank;
  346. *reg = g->drv_reg;
  347. *bit = g->slwf_bit;
  348. *width = g->slwf_width;
  349. break;
  350. case TEGRA_PINCONF_PARAM_SLEW_RATE_RISING:
  351. *bank = g->drv_bank;
  352. *reg = g->drv_reg;
  353. *bit = g->slwr_bit;
  354. *width = g->slwr_width;
  355. break;
  356. case TEGRA_PINCONF_PARAM_DRIVE_TYPE:
  357. *bank = g->drv_bank;
  358. *reg = g->drv_reg;
  359. *bit = g->drvtype_bit;
  360. *width = 2;
  361. break;
  362. default:
  363. dev_err(pmx->dev, "Invalid config param %04x\n", param);
  364. return -ENOTSUPP;
  365. }
  366. if (*reg < 0 || *bit > 31) {
  367. if (report_err) {
  368. const char *prop = "unknown";
  369. int i;
  370. for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
  371. if (cfg_params[i].param == param) {
  372. prop = cfg_params[i].property;
  373. break;
  374. }
  375. }
  376. dev_err(pmx->dev,
  377. "Config param %04x (%s) not supported on group %s\n",
  378. param, prop, g->name);
  379. }
  380. return -ENOTSUPP;
  381. }
  382. return 0;
  383. }
  384. static int tegra_pinconf_get(struct pinctrl_dev *pctldev,
  385. unsigned pin, unsigned long *config)
  386. {
  387. dev_err(pctldev->dev, "pin_config_get op not supported\n");
  388. return -ENOTSUPP;
  389. }
  390. static int tegra_pinconf_set(struct pinctrl_dev *pctldev,
  391. unsigned pin, unsigned long *configs,
  392. unsigned num_configs)
  393. {
  394. dev_err(pctldev->dev, "pin_config_set op not supported\n");
  395. return -ENOTSUPP;
  396. }
  397. static int tegra_pinconf_group_get(struct pinctrl_dev *pctldev,
  398. unsigned group, unsigned long *config)
  399. {
  400. struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  401. enum tegra_pinconf_param param = TEGRA_PINCONF_UNPACK_PARAM(*config);
  402. u16 arg;
  403. const struct tegra_pingroup *g;
  404. int ret;
  405. s8 bank, bit, width;
  406. s16 reg;
  407. u32 val, mask;
  408. g = &pmx->soc->groups[group];
  409. ret = tegra_pinconf_reg(pmx, g, param, true, &bank, &reg, &bit,
  410. &width);
  411. if (ret < 0)
  412. return ret;
  413. val = pmx_readl(pmx, bank, reg);
  414. mask = (1 << width) - 1;
  415. arg = (val >> bit) & mask;
  416. *config = TEGRA_PINCONF_PACK(param, arg);
  417. return 0;
  418. }
  419. static int tegra_pinconf_group_set(struct pinctrl_dev *pctldev,
  420. unsigned group, unsigned long *configs,
  421. unsigned num_configs)
  422. {
  423. struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  424. enum tegra_pinconf_param param;
  425. u16 arg;
  426. const struct tegra_pingroup *g;
  427. int ret, i;
  428. s8 bank, bit, width;
  429. s16 reg;
  430. u32 val, mask;
  431. g = &pmx->soc->groups[group];
  432. for (i = 0; i < num_configs; i++) {
  433. param = TEGRA_PINCONF_UNPACK_PARAM(configs[i]);
  434. arg = TEGRA_PINCONF_UNPACK_ARG(configs[i]);
  435. ret = tegra_pinconf_reg(pmx, g, param, true, &bank, &reg, &bit,
  436. &width);
  437. if (ret < 0)
  438. return ret;
  439. val = pmx_readl(pmx, bank, reg);
  440. /* LOCK can't be cleared */
  441. if (param == TEGRA_PINCONF_PARAM_LOCK) {
  442. if ((val & BIT(bit)) && !arg) {
  443. dev_err(pctldev->dev, "LOCK bit cannot be cleared\n");
  444. return -EINVAL;
  445. }
  446. }
  447. /* Special-case Boolean values; allow any non-zero as true */
  448. if (width == 1)
  449. arg = !!arg;
  450. /* Range-check user-supplied value */
  451. mask = (1 << width) - 1;
  452. if (arg & ~mask) {
  453. dev_err(pctldev->dev,
  454. "config %lx: %x too big for %d bit register\n",
  455. configs[i], arg, width);
  456. return -EINVAL;
  457. }
  458. /* Update register */
  459. val &= ~(mask << bit);
  460. val |= arg << bit;
  461. pmx_writel(pmx, val, bank, reg);
  462. } /* for each config */
  463. return 0;
  464. }
  465. #ifdef CONFIG_DEBUG_FS
  466. static void tegra_pinconf_dbg_show(struct pinctrl_dev *pctldev,
  467. struct seq_file *s, unsigned offset)
  468. {
  469. }
  470. static const char *strip_prefix(const char *s)
  471. {
  472. const char *comma = strchr(s, ',');
  473. if (!comma)
  474. return s;
  475. return comma + 1;
  476. }
  477. static void tegra_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
  478. struct seq_file *s, unsigned group)
  479. {
  480. struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  481. const struct tegra_pingroup *g;
  482. int i, ret;
  483. s8 bank, bit, width;
  484. s16 reg;
  485. u32 val;
  486. g = &pmx->soc->groups[group];
  487. for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
  488. ret = tegra_pinconf_reg(pmx, g, cfg_params[i].param, false,
  489. &bank, &reg, &bit, &width);
  490. if (ret < 0)
  491. continue;
  492. val = pmx_readl(pmx, bank, reg);
  493. val >>= bit;
  494. val &= (1 << width) - 1;
  495. seq_printf(s, "\n\t%s=%u",
  496. strip_prefix(cfg_params[i].property), val);
  497. }
  498. }
  499. static void tegra_pinconf_config_dbg_show(struct pinctrl_dev *pctldev,
  500. struct seq_file *s,
  501. unsigned long config)
  502. {
  503. enum tegra_pinconf_param param = TEGRA_PINCONF_UNPACK_PARAM(config);
  504. u16 arg = TEGRA_PINCONF_UNPACK_ARG(config);
  505. const char *pname = "unknown";
  506. int i;
  507. for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
  508. if (cfg_params[i].param == param) {
  509. pname = cfg_params[i].property;
  510. break;
  511. }
  512. }
  513. seq_printf(s, "%s=%d", strip_prefix(pname), arg);
  514. }
  515. #endif
  516. static const struct pinconf_ops tegra_pinconf_ops = {
  517. .pin_config_get = tegra_pinconf_get,
  518. .pin_config_set = tegra_pinconf_set,
  519. .pin_config_group_get = tegra_pinconf_group_get,
  520. .pin_config_group_set = tegra_pinconf_group_set,
  521. #ifdef CONFIG_DEBUG_FS
  522. .pin_config_dbg_show = tegra_pinconf_dbg_show,
  523. .pin_config_group_dbg_show = tegra_pinconf_group_dbg_show,
  524. .pin_config_config_dbg_show = tegra_pinconf_config_dbg_show,
  525. #endif
  526. };
  527. static struct pinctrl_gpio_range tegra_pinctrl_gpio_range = {
  528. .name = "Tegra GPIOs",
  529. .id = 0,
  530. .base = 0,
  531. };
  532. static struct pinctrl_desc tegra_pinctrl_desc = {
  533. .pctlops = &tegra_pinctrl_ops,
  534. .pmxops = &tegra_pinmux_ops,
  535. .confops = &tegra_pinconf_ops,
  536. .owner = THIS_MODULE,
  537. };
  538. int tegra_pinctrl_probe(struct platform_device *pdev,
  539. const struct tegra_pinctrl_soc_data *soc_data)
  540. {
  541. struct tegra_pmx *pmx;
  542. struct resource *res;
  543. int i;
  544. const char **group_pins;
  545. int fn, gn, gfn;
  546. pmx = devm_kzalloc(&pdev->dev, sizeof(*pmx), GFP_KERNEL);
  547. if (!pmx) {
  548. dev_err(&pdev->dev, "Can't alloc tegra_pmx\n");
  549. return -ENOMEM;
  550. }
  551. pmx->dev = &pdev->dev;
  552. pmx->soc = soc_data;
  553. /*
  554. * Each mux group will appear in 4 functions' list of groups.
  555. * This over-allocates slightly, since not all groups are mux groups.
  556. */
  557. pmx->group_pins = devm_kzalloc(&pdev->dev,
  558. soc_data->ngroups * 4 * sizeof(*pmx->group_pins),
  559. GFP_KERNEL);
  560. if (!pmx->group_pins)
  561. return -ENOMEM;
  562. group_pins = pmx->group_pins;
  563. for (fn = 0; fn < soc_data->nfunctions; fn++) {
  564. struct tegra_function *func = &soc_data->functions[fn];
  565. func->groups = group_pins;
  566. for (gn = 0; gn < soc_data->ngroups; gn++) {
  567. const struct tegra_pingroup *g = &soc_data->groups[gn];
  568. if (g->mux_reg == -1)
  569. continue;
  570. for (gfn = 0; gfn < 4; gfn++)
  571. if (g->funcs[gfn] == fn)
  572. break;
  573. if (gfn == 4)
  574. continue;
  575. BUG_ON(group_pins - pmx->group_pins >=
  576. soc_data->ngroups * 4);
  577. *group_pins++ = g->name;
  578. func->ngroups++;
  579. }
  580. }
  581. tegra_pinctrl_gpio_range.npins = pmx->soc->ngpios;
  582. tegra_pinctrl_desc.name = dev_name(&pdev->dev);
  583. tegra_pinctrl_desc.pins = pmx->soc->pins;
  584. tegra_pinctrl_desc.npins = pmx->soc->npins;
  585. for (i = 0; ; i++) {
  586. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  587. if (!res)
  588. break;
  589. }
  590. pmx->nbanks = i;
  591. pmx->regs = devm_kzalloc(&pdev->dev, pmx->nbanks * sizeof(*pmx->regs),
  592. GFP_KERNEL);
  593. if (!pmx->regs) {
  594. dev_err(&pdev->dev, "Can't alloc regs pointer\n");
  595. return -ENOMEM;
  596. }
  597. for (i = 0; i < pmx->nbanks; i++) {
  598. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  599. pmx->regs[i] = devm_ioremap_resource(&pdev->dev, res);
  600. if (IS_ERR(pmx->regs[i]))
  601. return PTR_ERR(pmx->regs[i]);
  602. }
  603. pmx->pctl = pinctrl_register(&tegra_pinctrl_desc, &pdev->dev, pmx);
  604. if (!pmx->pctl) {
  605. dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
  606. return -ENODEV;
  607. }
  608. pinctrl_add_gpio_range(pmx->pctl, &tegra_pinctrl_gpio_range);
  609. platform_set_drvdata(pdev, pmx);
  610. dev_dbg(&pdev->dev, "Probed Tegra pinctrl driver\n");
  611. return 0;
  612. }
  613. EXPORT_SYMBOL_GPL(tegra_pinctrl_probe);
  614. int tegra_pinctrl_remove(struct platform_device *pdev)
  615. {
  616. struct tegra_pmx *pmx = platform_get_drvdata(pdev);
  617. pinctrl_unregister(pmx->pctl);
  618. return 0;
  619. }
  620. EXPORT_SYMBOL_GPL(tegra_pinctrl_remove);