PageRenderTime 81ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/pinctrl/freescale/pinctrl-mxs.c

https://github.com/tklauser/linux-nios2
C | 554 lines | 429 code | 90 blank | 35 comment | 55 complexity | 66acebec59846d195be541aef640e907 MD5 | raw file
  1. /*
  2. * Copyright 2012 Freescale Semiconductor, Inc.
  3. *
  4. * The code contained herein is licensed under the GNU General Public
  5. * License. You may obtain a copy of the GNU General Public License
  6. * Version 2 or later at the following locations:
  7. *
  8. * http://www.opensource.org/licenses/gpl-license.html
  9. * http://www.gnu.org/copyleft/gpl.html
  10. */
  11. #include <linux/err.h>
  12. #include <linux/init.h>
  13. #include <linux/io.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/pinctrl/machine.h>
  17. #include <linux/pinctrl/pinconf.h>
  18. #include <linux/pinctrl/pinctrl.h>
  19. #include <linux/pinctrl/pinmux.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/slab.h>
  22. #include "../core.h"
  23. #include "pinctrl-mxs.h"
  24. #define SUFFIX_LEN 4
  25. struct mxs_pinctrl_data {
  26. struct device *dev;
  27. struct pinctrl_dev *pctl;
  28. void __iomem *base;
  29. struct mxs_pinctrl_soc_data *soc;
  30. };
  31. static int mxs_get_groups_count(struct pinctrl_dev *pctldev)
  32. {
  33. struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
  34. return d->soc->ngroups;
  35. }
  36. static const char *mxs_get_group_name(struct pinctrl_dev *pctldev,
  37. unsigned group)
  38. {
  39. struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
  40. return d->soc->groups[group].name;
  41. }
  42. static int mxs_get_group_pins(struct pinctrl_dev *pctldev, unsigned group,
  43. const unsigned **pins, unsigned *num_pins)
  44. {
  45. struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
  46. *pins = d->soc->groups[group].pins;
  47. *num_pins = d->soc->groups[group].npins;
  48. return 0;
  49. }
  50. static void mxs_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
  51. unsigned offset)
  52. {
  53. seq_printf(s, " %s", dev_name(pctldev->dev));
  54. }
  55. static int mxs_dt_node_to_map(struct pinctrl_dev *pctldev,
  56. struct device_node *np,
  57. struct pinctrl_map **map, unsigned *num_maps)
  58. {
  59. struct pinctrl_map *new_map;
  60. char *group = NULL;
  61. unsigned new_num = 1;
  62. unsigned long config = 0;
  63. unsigned long *pconfig;
  64. int length = strlen(np->name) + SUFFIX_LEN;
  65. bool purecfg = false;
  66. u32 val, reg;
  67. int ret, i = 0;
  68. /* Check for pin config node which has no 'reg' property */
  69. if (of_property_read_u32(np, "reg", &reg))
  70. purecfg = true;
  71. ret = of_property_read_u32(np, "fsl,drive-strength", &val);
  72. if (!ret)
  73. config = val | MA_PRESENT;
  74. ret = of_property_read_u32(np, "fsl,voltage", &val);
  75. if (!ret)
  76. config |= val << VOL_SHIFT | VOL_PRESENT;
  77. ret = of_property_read_u32(np, "fsl,pull-up", &val);
  78. if (!ret)
  79. config |= val << PULL_SHIFT | PULL_PRESENT;
  80. /* Check for group node which has both mux and config settings */
  81. if (!purecfg && config)
  82. new_num = 2;
  83. new_map = kzalloc(sizeof(*new_map) * new_num, GFP_KERNEL);
  84. if (!new_map)
  85. return -ENOMEM;
  86. if (!purecfg) {
  87. new_map[i].type = PIN_MAP_TYPE_MUX_GROUP;
  88. new_map[i].data.mux.function = np->name;
  89. /* Compose group name */
  90. group = kzalloc(length, GFP_KERNEL);
  91. if (!group) {
  92. ret = -ENOMEM;
  93. goto free;
  94. }
  95. snprintf(group, length, "%s.%d", np->name, reg);
  96. new_map[i].data.mux.group = group;
  97. i++;
  98. }
  99. if (config) {
  100. pconfig = kmemdup(&config, sizeof(config), GFP_KERNEL);
  101. if (!pconfig) {
  102. ret = -ENOMEM;
  103. goto free_group;
  104. }
  105. new_map[i].type = PIN_MAP_TYPE_CONFIGS_GROUP;
  106. new_map[i].data.configs.group_or_pin = purecfg ? np->name :
  107. group;
  108. new_map[i].data.configs.configs = pconfig;
  109. new_map[i].data.configs.num_configs = 1;
  110. }
  111. *map = new_map;
  112. *num_maps = new_num;
  113. return 0;
  114. free_group:
  115. if (!purecfg)
  116. kfree(group);
  117. free:
  118. kfree(new_map);
  119. return ret;
  120. }
  121. static void mxs_dt_free_map(struct pinctrl_dev *pctldev,
  122. struct pinctrl_map *map, unsigned num_maps)
  123. {
  124. u32 i;
  125. for (i = 0; i < num_maps; i++) {
  126. if (map[i].type == PIN_MAP_TYPE_MUX_GROUP)
  127. kfree(map[i].data.mux.group);
  128. if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP)
  129. kfree(map[i].data.configs.configs);
  130. }
  131. kfree(map);
  132. }
  133. static const struct pinctrl_ops mxs_pinctrl_ops = {
  134. .get_groups_count = mxs_get_groups_count,
  135. .get_group_name = mxs_get_group_name,
  136. .get_group_pins = mxs_get_group_pins,
  137. .pin_dbg_show = mxs_pin_dbg_show,
  138. .dt_node_to_map = mxs_dt_node_to_map,
  139. .dt_free_map = mxs_dt_free_map,
  140. };
  141. static int mxs_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev)
  142. {
  143. struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
  144. return d->soc->nfunctions;
  145. }
  146. static const char *mxs_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
  147. unsigned function)
  148. {
  149. struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
  150. return d->soc->functions[function].name;
  151. }
  152. static int mxs_pinctrl_get_func_groups(struct pinctrl_dev *pctldev,
  153. unsigned group,
  154. const char * const **groups,
  155. unsigned * const num_groups)
  156. {
  157. struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
  158. *groups = d->soc->functions[group].groups;
  159. *num_groups = d->soc->functions[group].ngroups;
  160. return 0;
  161. }
  162. static int mxs_pinctrl_set_mux(struct pinctrl_dev *pctldev, unsigned selector,
  163. unsigned group)
  164. {
  165. struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
  166. struct mxs_group *g = &d->soc->groups[group];
  167. void __iomem *reg;
  168. u8 bank, shift;
  169. u16 pin;
  170. u32 i;
  171. for (i = 0; i < g->npins; i++) {
  172. bank = PINID_TO_BANK(g->pins[i]);
  173. pin = PINID_TO_PIN(g->pins[i]);
  174. reg = d->base + d->soc->regs->muxsel;
  175. reg += bank * 0x20 + pin / 16 * 0x10;
  176. shift = pin % 16 * 2;
  177. writel(0x3 << shift, reg + CLR);
  178. writel(g->muxsel[i] << shift, reg + SET);
  179. }
  180. return 0;
  181. }
  182. static const struct pinmux_ops mxs_pinmux_ops = {
  183. .get_functions_count = mxs_pinctrl_get_funcs_count,
  184. .get_function_name = mxs_pinctrl_get_func_name,
  185. .get_function_groups = mxs_pinctrl_get_func_groups,
  186. .set_mux = mxs_pinctrl_set_mux,
  187. };
  188. static int mxs_pinconf_get(struct pinctrl_dev *pctldev,
  189. unsigned pin, unsigned long *config)
  190. {
  191. return -ENOTSUPP;
  192. }
  193. static int mxs_pinconf_set(struct pinctrl_dev *pctldev,
  194. unsigned pin, unsigned long *configs,
  195. unsigned num_configs)
  196. {
  197. return -ENOTSUPP;
  198. }
  199. static int mxs_pinconf_group_get(struct pinctrl_dev *pctldev,
  200. unsigned group, unsigned long *config)
  201. {
  202. struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
  203. *config = d->soc->groups[group].config;
  204. return 0;
  205. }
  206. static int mxs_pinconf_group_set(struct pinctrl_dev *pctldev,
  207. unsigned group, unsigned long *configs,
  208. unsigned num_configs)
  209. {
  210. struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
  211. struct mxs_group *g = &d->soc->groups[group];
  212. void __iomem *reg;
  213. u8 ma, vol, pull, bank, shift;
  214. u16 pin;
  215. u32 i;
  216. int n;
  217. unsigned long config;
  218. for (n = 0; n < num_configs; n++) {
  219. config = configs[n];
  220. ma = CONFIG_TO_MA(config);
  221. vol = CONFIG_TO_VOL(config);
  222. pull = CONFIG_TO_PULL(config);
  223. for (i = 0; i < g->npins; i++) {
  224. bank = PINID_TO_BANK(g->pins[i]);
  225. pin = PINID_TO_PIN(g->pins[i]);
  226. /* drive */
  227. reg = d->base + d->soc->regs->drive;
  228. reg += bank * 0x40 + pin / 8 * 0x10;
  229. /* mA */
  230. if (config & MA_PRESENT) {
  231. shift = pin % 8 * 4;
  232. writel(0x3 << shift, reg + CLR);
  233. writel(ma << shift, reg + SET);
  234. }
  235. /* vol */
  236. if (config & VOL_PRESENT) {
  237. shift = pin % 8 * 4 + 2;
  238. if (vol)
  239. writel(1 << shift, reg + SET);
  240. else
  241. writel(1 << shift, reg + CLR);
  242. }
  243. /* pull */
  244. if (config & PULL_PRESENT) {
  245. reg = d->base + d->soc->regs->pull;
  246. reg += bank * 0x10;
  247. shift = pin;
  248. if (pull)
  249. writel(1 << shift, reg + SET);
  250. else
  251. writel(1 << shift, reg + CLR);
  252. }
  253. }
  254. /* cache the config value for mxs_pinconf_group_get() */
  255. g->config = config;
  256. } /* for each config */
  257. return 0;
  258. }
  259. static void mxs_pinconf_dbg_show(struct pinctrl_dev *pctldev,
  260. struct seq_file *s, unsigned pin)
  261. {
  262. /* Not support */
  263. }
  264. static void mxs_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
  265. struct seq_file *s, unsigned group)
  266. {
  267. unsigned long config;
  268. if (!mxs_pinconf_group_get(pctldev, group, &config))
  269. seq_printf(s, "0x%lx", config);
  270. }
  271. static const struct pinconf_ops mxs_pinconf_ops = {
  272. .pin_config_get = mxs_pinconf_get,
  273. .pin_config_set = mxs_pinconf_set,
  274. .pin_config_group_get = mxs_pinconf_group_get,
  275. .pin_config_group_set = mxs_pinconf_group_set,
  276. .pin_config_dbg_show = mxs_pinconf_dbg_show,
  277. .pin_config_group_dbg_show = mxs_pinconf_group_dbg_show,
  278. };
  279. static struct pinctrl_desc mxs_pinctrl_desc = {
  280. .pctlops = &mxs_pinctrl_ops,
  281. .pmxops = &mxs_pinmux_ops,
  282. .confops = &mxs_pinconf_ops,
  283. .owner = THIS_MODULE,
  284. };
  285. static int mxs_pinctrl_parse_group(struct platform_device *pdev,
  286. struct device_node *np, int idx,
  287. const char **out_name)
  288. {
  289. struct mxs_pinctrl_data *d = platform_get_drvdata(pdev);
  290. struct mxs_group *g = &d->soc->groups[idx];
  291. struct property *prop;
  292. const char *propname = "fsl,pinmux-ids";
  293. char *group;
  294. int length = strlen(np->name) + SUFFIX_LEN;
  295. u32 val, i;
  296. group = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
  297. if (!group)
  298. return -ENOMEM;
  299. if (of_property_read_u32(np, "reg", &val))
  300. snprintf(group, length, "%s", np->name);
  301. else
  302. snprintf(group, length, "%s.%d", np->name, val);
  303. g->name = group;
  304. prop = of_find_property(np, propname, &length);
  305. if (!prop)
  306. return -EINVAL;
  307. g->npins = length / sizeof(u32);
  308. g->pins = devm_kzalloc(&pdev->dev, g->npins * sizeof(*g->pins),
  309. GFP_KERNEL);
  310. if (!g->pins)
  311. return -ENOMEM;
  312. g->muxsel = devm_kzalloc(&pdev->dev, g->npins * sizeof(*g->muxsel),
  313. GFP_KERNEL);
  314. if (!g->muxsel)
  315. return -ENOMEM;
  316. of_property_read_u32_array(np, propname, g->pins, g->npins);
  317. for (i = 0; i < g->npins; i++) {
  318. g->muxsel[i] = MUXID_TO_MUXSEL(g->pins[i]);
  319. g->pins[i] = MUXID_TO_PINID(g->pins[i]);
  320. }
  321. if (out_name)
  322. *out_name = g->name;
  323. return 0;
  324. }
  325. static int mxs_pinctrl_probe_dt(struct platform_device *pdev,
  326. struct mxs_pinctrl_data *d)
  327. {
  328. struct mxs_pinctrl_soc_data *soc = d->soc;
  329. struct device_node *np = pdev->dev.of_node;
  330. struct device_node *child;
  331. struct mxs_function *f;
  332. const char *gpio_compat = "fsl,mxs-gpio";
  333. const char *fn, *fnull = "";
  334. int i = 0, idxf = 0, idxg = 0;
  335. int ret;
  336. u32 val;
  337. child = of_get_next_child(np, NULL);
  338. if (!child) {
  339. dev_err(&pdev->dev, "no group is defined\n");
  340. return -ENOENT;
  341. }
  342. /* Count total functions and groups */
  343. fn = fnull;
  344. for_each_child_of_node(np, child) {
  345. if (of_device_is_compatible(child, gpio_compat))
  346. continue;
  347. soc->ngroups++;
  348. /* Skip pure pinconf node */
  349. if (of_property_read_u32(child, "reg", &val))
  350. continue;
  351. if (strcmp(fn, child->name)) {
  352. fn = child->name;
  353. soc->nfunctions++;
  354. }
  355. }
  356. soc->functions = devm_kzalloc(&pdev->dev, soc->nfunctions *
  357. sizeof(*soc->functions), GFP_KERNEL);
  358. if (!soc->functions)
  359. return -ENOMEM;
  360. soc->groups = devm_kzalloc(&pdev->dev, soc->ngroups *
  361. sizeof(*soc->groups), GFP_KERNEL);
  362. if (!soc->groups)
  363. return -ENOMEM;
  364. /* Count groups for each function */
  365. fn = fnull;
  366. f = &soc->functions[idxf];
  367. for_each_child_of_node(np, child) {
  368. if (of_device_is_compatible(child, gpio_compat))
  369. continue;
  370. if (of_property_read_u32(child, "reg", &val))
  371. continue;
  372. if (strcmp(fn, child->name)) {
  373. struct device_node *child2;
  374. /*
  375. * This reference is dropped by
  376. * of_get_next_child(np, * child)
  377. */
  378. of_node_get(child);
  379. /*
  380. * The logic parsing the functions from dt currently
  381. * doesn't handle if functions with the same name are
  382. * not grouped together. Only the first contiguous
  383. * cluster is usable for each function name. This is a
  384. * bug that is not trivial to fix, but at least warn
  385. * about it.
  386. */
  387. for (child2 = of_get_next_child(np, child);
  388. child2 != NULL;
  389. child2 = of_get_next_child(np, child2)) {
  390. if (!strcmp(child2->name, fn))
  391. dev_warn(&pdev->dev,
  392. "function nodes must be grouped by name (failed for: %s)",
  393. fn);
  394. }
  395. f = &soc->functions[idxf++];
  396. f->name = fn = child->name;
  397. }
  398. f->ngroups++;
  399. }
  400. /* Get groups for each function */
  401. idxf = 0;
  402. fn = fnull;
  403. for_each_child_of_node(np, child) {
  404. if (of_device_is_compatible(child, gpio_compat))
  405. continue;
  406. if (of_property_read_u32(child, "reg", &val)) {
  407. ret = mxs_pinctrl_parse_group(pdev, child,
  408. idxg++, NULL);
  409. if (ret)
  410. return ret;
  411. continue;
  412. }
  413. if (strcmp(fn, child->name)) {
  414. f = &soc->functions[idxf++];
  415. f->groups = devm_kzalloc(&pdev->dev, f->ngroups *
  416. sizeof(*f->groups),
  417. GFP_KERNEL);
  418. if (!f->groups)
  419. return -ENOMEM;
  420. fn = child->name;
  421. i = 0;
  422. }
  423. ret = mxs_pinctrl_parse_group(pdev, child, idxg++,
  424. &f->groups[i++]);
  425. if (ret)
  426. return ret;
  427. }
  428. return 0;
  429. }
  430. int mxs_pinctrl_probe(struct platform_device *pdev,
  431. struct mxs_pinctrl_soc_data *soc)
  432. {
  433. struct device_node *np = pdev->dev.of_node;
  434. struct mxs_pinctrl_data *d;
  435. int ret;
  436. d = devm_kzalloc(&pdev->dev, sizeof(*d), GFP_KERNEL);
  437. if (!d)
  438. return -ENOMEM;
  439. d->dev = &pdev->dev;
  440. d->soc = soc;
  441. d->base = of_iomap(np, 0);
  442. if (!d->base)
  443. return -EADDRNOTAVAIL;
  444. mxs_pinctrl_desc.pins = d->soc->pins;
  445. mxs_pinctrl_desc.npins = d->soc->npins;
  446. mxs_pinctrl_desc.name = dev_name(&pdev->dev);
  447. platform_set_drvdata(pdev, d);
  448. ret = mxs_pinctrl_probe_dt(pdev, d);
  449. if (ret) {
  450. dev_err(&pdev->dev, "dt probe failed: %d\n", ret);
  451. goto err;
  452. }
  453. d->pctl = pinctrl_register(&mxs_pinctrl_desc, &pdev->dev, d);
  454. if (IS_ERR(d->pctl)) {
  455. dev_err(&pdev->dev, "Couldn't register MXS pinctrl driver\n");
  456. ret = PTR_ERR(d->pctl);
  457. goto err;
  458. }
  459. return 0;
  460. err:
  461. iounmap(d->base);
  462. return ret;
  463. }
  464. EXPORT_SYMBOL_GPL(mxs_pinctrl_probe);