pinctrl-tegra.c 18 KB

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