pinctrl-tegra.c 19 KB

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