pinctrl-tegra.c 19 KB

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