pinctrl-mxs.c 13 KB

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