pinctrl-mxs.c 12 KB

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