pinctrl-mxs.c 12 KB

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