pinctrl-tegra.c 19 KB

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