pinctrl-mxs.c 13 KB

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