pinctrl-mxs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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 void mxs_pinctrl_rmwl(u32 value, u32 mask, u8 shift, void __iomem *reg)
  163. {
  164. u32 tmp;
  165. tmp = readl(reg);
  166. tmp &= ~(mask << shift);
  167. tmp |= value << shift;
  168. writel(tmp, reg);
  169. }
  170. static int mxs_pinctrl_set_mux(struct pinctrl_dev *pctldev, unsigned selector,
  171. unsigned group)
  172. {
  173. struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
  174. struct mxs_group *g = &d->soc->groups[group];
  175. void __iomem *reg;
  176. u8 bank, shift;
  177. u16 pin;
  178. u32 i;
  179. for (i = 0; i < g->npins; i++) {
  180. bank = PINID_TO_BANK(g->pins[i]);
  181. pin = PINID_TO_PIN(g->pins[i]);
  182. reg = d->base + d->soc->regs->muxsel;
  183. reg += bank * 0x20 + pin / 16 * 0x10;
  184. shift = pin % 16 * 2;
  185. mxs_pinctrl_rmwl(g->muxsel[i], 0x3, shift, reg);
  186. }
  187. return 0;
  188. }
  189. static const struct pinmux_ops mxs_pinmux_ops = {
  190. .get_functions_count = mxs_pinctrl_get_funcs_count,
  191. .get_function_name = mxs_pinctrl_get_func_name,
  192. .get_function_groups = mxs_pinctrl_get_func_groups,
  193. .set_mux = mxs_pinctrl_set_mux,
  194. };
  195. static int mxs_pinconf_get(struct pinctrl_dev *pctldev,
  196. unsigned pin, unsigned long *config)
  197. {
  198. return -ENOTSUPP;
  199. }
  200. static int mxs_pinconf_set(struct pinctrl_dev *pctldev,
  201. unsigned pin, unsigned long *configs,
  202. unsigned num_configs)
  203. {
  204. return -ENOTSUPP;
  205. }
  206. static int mxs_pinconf_group_get(struct pinctrl_dev *pctldev,
  207. unsigned group, unsigned long *config)
  208. {
  209. struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
  210. *config = d->soc->groups[group].config;
  211. return 0;
  212. }
  213. static int mxs_pinconf_group_set(struct pinctrl_dev *pctldev,
  214. unsigned group, unsigned long *configs,
  215. unsigned num_configs)
  216. {
  217. struct mxs_pinctrl_data *d = pinctrl_dev_get_drvdata(pctldev);
  218. struct mxs_group *g = &d->soc->groups[group];
  219. void __iomem *reg;
  220. u8 ma, vol, pull, bank, shift;
  221. u16 pin;
  222. u32 i;
  223. int n;
  224. unsigned long config;
  225. for (n = 0; n < num_configs; n++) {
  226. config = configs[n];
  227. ma = CONFIG_TO_MA(config);
  228. vol = CONFIG_TO_VOL(config);
  229. pull = CONFIG_TO_PULL(config);
  230. for (i = 0; i < g->npins; i++) {
  231. bank = PINID_TO_BANK(g->pins[i]);
  232. pin = PINID_TO_PIN(g->pins[i]);
  233. /* drive */
  234. reg = d->base + d->soc->regs->drive;
  235. reg += bank * 0x40 + pin / 8 * 0x10;
  236. /* mA */
  237. if (config & MA_PRESENT) {
  238. shift = pin % 8 * 4;
  239. mxs_pinctrl_rmwl(ma, 0x3, shift, reg);
  240. }
  241. /* vol */
  242. if (config & VOL_PRESENT) {
  243. shift = pin % 8 * 4 + 2;
  244. if (vol)
  245. writel(1 << shift, reg + SET);
  246. else
  247. writel(1 << shift, reg + CLR);
  248. }
  249. /* pull */
  250. if (config & PULL_PRESENT) {
  251. reg = d->base + d->soc->regs->pull;
  252. reg += bank * 0x10;
  253. shift = pin;
  254. if (pull)
  255. writel(1 << shift, reg + SET);
  256. else
  257. writel(1 << shift, reg + CLR);
  258. }
  259. }
  260. /* cache the config value for mxs_pinconf_group_get() */
  261. g->config = config;
  262. } /* for each config */
  263. return 0;
  264. }
  265. static void mxs_pinconf_dbg_show(struct pinctrl_dev *pctldev,
  266. struct seq_file *s, unsigned pin)
  267. {
  268. /* Not support */
  269. }
  270. static void mxs_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
  271. struct seq_file *s, unsigned group)
  272. {
  273. unsigned long config;
  274. if (!mxs_pinconf_group_get(pctldev, group, &config))
  275. seq_printf(s, "0x%lx", config);
  276. }
  277. static const struct pinconf_ops mxs_pinconf_ops = {
  278. .pin_config_get = mxs_pinconf_get,
  279. .pin_config_set = mxs_pinconf_set,
  280. .pin_config_group_get = mxs_pinconf_group_get,
  281. .pin_config_group_set = mxs_pinconf_group_set,
  282. .pin_config_dbg_show = mxs_pinconf_dbg_show,
  283. .pin_config_group_dbg_show = mxs_pinconf_group_dbg_show,
  284. };
  285. static struct pinctrl_desc mxs_pinctrl_desc = {
  286. .pctlops = &mxs_pinctrl_ops,
  287. .pmxops = &mxs_pinmux_ops,
  288. .confops = &mxs_pinconf_ops,
  289. .owner = THIS_MODULE,
  290. };
  291. static int mxs_pinctrl_parse_group(struct platform_device *pdev,
  292. struct device_node *np, int idx,
  293. const char **out_name)
  294. {
  295. struct mxs_pinctrl_data *d = platform_get_drvdata(pdev);
  296. struct mxs_group *g = &d->soc->groups[idx];
  297. struct property *prop;
  298. const char *propname = "fsl,pinmux-ids";
  299. char *group;
  300. int length = strlen(np->name) + SUFFIX_LEN;
  301. u32 val, i;
  302. group = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
  303. if (!group)
  304. return -ENOMEM;
  305. if (of_property_read_u32(np, "reg", &val))
  306. snprintf(group, length, "%s", np->name);
  307. else
  308. snprintf(group, length, "%s.%d", np->name, val);
  309. g->name = group;
  310. prop = of_find_property(np, propname, &length);
  311. if (!prop)
  312. return -EINVAL;
  313. g->npins = length / sizeof(u32);
  314. g->pins = devm_kzalloc(&pdev->dev, g->npins * sizeof(*g->pins),
  315. GFP_KERNEL);
  316. if (!g->pins)
  317. return -ENOMEM;
  318. g->muxsel = devm_kzalloc(&pdev->dev, g->npins * sizeof(*g->muxsel),
  319. GFP_KERNEL);
  320. if (!g->muxsel)
  321. return -ENOMEM;
  322. of_property_read_u32_array(np, propname, g->pins, g->npins);
  323. for (i = 0; i < g->npins; i++) {
  324. g->muxsel[i] = MUXID_TO_MUXSEL(g->pins[i]);
  325. g->pins[i] = MUXID_TO_PINID(g->pins[i]);
  326. }
  327. if (out_name)
  328. *out_name = g->name;
  329. return 0;
  330. }
  331. static int mxs_pinctrl_probe_dt(struct platform_device *pdev,
  332. struct mxs_pinctrl_data *d)
  333. {
  334. struct mxs_pinctrl_soc_data *soc = d->soc;
  335. struct device_node *np = pdev->dev.of_node;
  336. struct device_node *child;
  337. struct mxs_function *f;
  338. const char *gpio_compat = "fsl,mxs-gpio";
  339. const char *fn, *fnull = "";
  340. int i = 0, idxf = 0, idxg = 0;
  341. int ret;
  342. u32 val;
  343. child = of_get_next_child(np, NULL);
  344. if (!child) {
  345. dev_err(&pdev->dev, "no group is defined\n");
  346. return -ENOENT;
  347. }
  348. /* Count total functions and groups */
  349. fn = fnull;
  350. for_each_child_of_node(np, child) {
  351. if (of_device_is_compatible(child, gpio_compat))
  352. continue;
  353. soc->ngroups++;
  354. /* Skip pure pinconf node */
  355. if (of_property_read_u32(child, "reg", &val))
  356. continue;
  357. if (strcmp(fn, child->name)) {
  358. fn = child->name;
  359. soc->nfunctions++;
  360. }
  361. }
  362. soc->functions = devm_kzalloc(&pdev->dev, soc->nfunctions *
  363. sizeof(*soc->functions), GFP_KERNEL);
  364. if (!soc->functions)
  365. return -ENOMEM;
  366. soc->groups = devm_kzalloc(&pdev->dev, soc->ngroups *
  367. sizeof(*soc->groups), GFP_KERNEL);
  368. if (!soc->groups)
  369. return -ENOMEM;
  370. /* Count groups for each function */
  371. fn = fnull;
  372. f = &soc->functions[idxf];
  373. for_each_child_of_node(np, child) {
  374. if (of_device_is_compatible(child, gpio_compat))
  375. continue;
  376. if (of_property_read_u32(child, "reg", &val))
  377. continue;
  378. if (strcmp(fn, child->name)) {
  379. struct device_node *child2;
  380. /*
  381. * This reference is dropped by
  382. * of_get_next_child(np, * child)
  383. */
  384. of_node_get(child);
  385. /*
  386. * The logic parsing the functions from dt currently
  387. * doesn't handle if functions with the same name are
  388. * not grouped together. Only the first contiguous
  389. * cluster is usable for each function name. This is a
  390. * bug that is not trivial to fix, but at least warn
  391. * about it.
  392. */
  393. for (child2 = of_get_next_child(np, child);
  394. child2 != NULL;
  395. child2 = of_get_next_child(np, child2)) {
  396. if (!strcmp(child2->name, fn))
  397. dev_warn(&pdev->dev,
  398. "function nodes must be grouped by name (failed for: %s)",
  399. fn);
  400. }
  401. f = &soc->functions[idxf++];
  402. f->name = fn = child->name;
  403. }
  404. f->ngroups++;
  405. }
  406. /* Get groups for each function */
  407. idxf = 0;
  408. fn = fnull;
  409. for_each_child_of_node(np, child) {
  410. if (of_device_is_compatible(child, gpio_compat))
  411. continue;
  412. if (of_property_read_u32(child, "reg", &val)) {
  413. ret = mxs_pinctrl_parse_group(pdev, child,
  414. idxg++, NULL);
  415. if (ret)
  416. return ret;
  417. continue;
  418. }
  419. if (strcmp(fn, child->name)) {
  420. f = &soc->functions[idxf++];
  421. f->groups = devm_kzalloc(&pdev->dev, f->ngroups *
  422. sizeof(*f->groups),
  423. GFP_KERNEL);
  424. if (!f->groups)
  425. return -ENOMEM;
  426. fn = child->name;
  427. i = 0;
  428. }
  429. ret = mxs_pinctrl_parse_group(pdev, child, idxg++,
  430. &f->groups[i++]);
  431. if (ret)
  432. return ret;
  433. }
  434. return 0;
  435. }
  436. int mxs_pinctrl_probe(struct platform_device *pdev,
  437. struct mxs_pinctrl_soc_data *soc)
  438. {
  439. struct device_node *np = pdev->dev.of_node;
  440. struct mxs_pinctrl_data *d;
  441. int ret;
  442. d = devm_kzalloc(&pdev->dev, sizeof(*d), GFP_KERNEL);
  443. if (!d)
  444. return -ENOMEM;
  445. d->dev = &pdev->dev;
  446. d->soc = soc;
  447. d->base = of_iomap(np, 0);
  448. if (!d->base)
  449. return -EADDRNOTAVAIL;
  450. mxs_pinctrl_desc.pins = d->soc->pins;
  451. mxs_pinctrl_desc.npins = d->soc->npins;
  452. mxs_pinctrl_desc.name = dev_name(&pdev->dev);
  453. platform_set_drvdata(pdev, d);
  454. ret = mxs_pinctrl_probe_dt(pdev, d);
  455. if (ret) {
  456. dev_err(&pdev->dev, "dt probe failed: %d\n", ret);
  457. goto err;
  458. }
  459. d->pctl = pinctrl_register(&mxs_pinctrl_desc, &pdev->dev, d);
  460. if (IS_ERR(d->pctl)) {
  461. dev_err(&pdev->dev, "Couldn't register MXS pinctrl driver\n");
  462. ret = PTR_ERR(d->pctl);
  463. goto err;
  464. }
  465. return 0;
  466. err:
  467. iounmap(d->base);
  468. return ret;
  469. }
  470. EXPORT_SYMBOL_GPL(mxs_pinctrl_probe);