pinctrl-tegra.c 18 KB

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