pinctrl-lantiq.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * linux/drivers/pinctrl/pinctrl-lantiq.c
  3. * based on linux/drivers/pinctrl/pinctrl-pxa3xx.c
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * publishhed by the Free Software Foundation.
  8. *
  9. * Copyright (C) 2012 John Crispin <john@phrozen.org>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/device.h>
  13. #include <linux/io.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/slab.h>
  16. #include <linux/of.h>
  17. #include "pinctrl-lantiq.h"
  18. static int ltq_get_group_count(struct pinctrl_dev *pctrldev)
  19. {
  20. struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
  21. return info->num_grps;
  22. }
  23. static const char *ltq_get_group_name(struct pinctrl_dev *pctrldev,
  24. unsigned selector)
  25. {
  26. struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
  27. if (selector >= info->num_grps)
  28. return NULL;
  29. return info->grps[selector].name;
  30. }
  31. static int ltq_get_group_pins(struct pinctrl_dev *pctrldev,
  32. unsigned selector,
  33. const unsigned **pins,
  34. unsigned *num_pins)
  35. {
  36. struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
  37. if (selector >= info->num_grps)
  38. return -EINVAL;
  39. *pins = info->grps[selector].pins;
  40. *num_pins = info->grps[selector].npins;
  41. return 0;
  42. }
  43. static void ltq_pinctrl_dt_free_map(struct pinctrl_dev *pctldev,
  44. struct pinctrl_map *map, unsigned num_maps)
  45. {
  46. int i;
  47. for (i = 0; i < num_maps; i++)
  48. if (map[i].type == PIN_MAP_TYPE_CONFIGS_PIN ||
  49. map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP)
  50. kfree(map[i].data.configs.configs);
  51. kfree(map);
  52. }
  53. static void ltq_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
  54. struct seq_file *s,
  55. unsigned offset)
  56. {
  57. seq_printf(s, " %s", dev_name(pctldev->dev));
  58. }
  59. static void ltq_pinctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
  60. struct device_node *np,
  61. struct pinctrl_map **map)
  62. {
  63. struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctldev);
  64. struct property *pins = of_find_property(np, "lantiq,pins", NULL);
  65. struct property *groups = of_find_property(np, "lantiq,groups", NULL);
  66. unsigned long configs[3];
  67. unsigned num_configs = 0;
  68. struct property *prop;
  69. const char *group, *pin;
  70. const char *function;
  71. int ret, i;
  72. if (!pins && !groups) {
  73. dev_err(pctldev->dev, "%s defines neither pins nor groups\n",
  74. np->name);
  75. return;
  76. }
  77. if (pins && groups) {
  78. dev_err(pctldev->dev, "%s defines both pins and groups\n",
  79. np->name);
  80. return;
  81. }
  82. ret = of_property_read_string(np, "lantiq,function", &function);
  83. if (groups && !ret) {
  84. of_property_for_each_string(np, "lantiq,groups", prop, group) {
  85. (*map)->type = PIN_MAP_TYPE_MUX_GROUP;
  86. (*map)->name = function;
  87. (*map)->data.mux.group = group;
  88. (*map)->data.mux.function = function;
  89. (*map)++;
  90. }
  91. }
  92. for (i = 0; i < info->num_params; i++) {
  93. u32 val;
  94. int ret = of_property_read_u32(np,
  95. info->params[i].property, &val);
  96. if (!ret)
  97. configs[num_configs++] =
  98. LTQ_PINCONF_PACK(info->params[i].param,
  99. val);
  100. }
  101. if (!num_configs)
  102. return;
  103. of_property_for_each_string(np, "lantiq,pins", prop, pin) {
  104. (*map)->data.configs.configs = kmemdup(configs,
  105. num_configs * sizeof(unsigned long),
  106. GFP_KERNEL);
  107. (*map)->type = PIN_MAP_TYPE_CONFIGS_PIN;
  108. (*map)->name = pin;
  109. (*map)->data.configs.group_or_pin = pin;
  110. (*map)->data.configs.num_configs = num_configs;
  111. (*map)++;
  112. }
  113. of_property_for_each_string(np, "lantiq,groups", prop, group) {
  114. (*map)->data.configs.configs = kmemdup(configs,
  115. num_configs * sizeof(unsigned long),
  116. GFP_KERNEL);
  117. (*map)->type = PIN_MAP_TYPE_CONFIGS_GROUP;
  118. (*map)->name = group;
  119. (*map)->data.configs.group_or_pin = group;
  120. (*map)->data.configs.num_configs = num_configs;
  121. (*map)++;
  122. }
  123. }
  124. static int ltq_pinctrl_dt_subnode_size(struct device_node *np)
  125. {
  126. int ret;
  127. ret = of_property_count_strings(np, "lantiq,groups");
  128. if (ret < 0)
  129. ret = of_property_count_strings(np, "lantiq,pins");
  130. return ret;
  131. }
  132. static int ltq_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
  133. struct device_node *np_config,
  134. struct pinctrl_map **map,
  135. unsigned *num_maps)
  136. {
  137. struct pinctrl_map *tmp;
  138. struct device_node *np;
  139. int max_maps = 0;
  140. for_each_child_of_node(np_config, np)
  141. max_maps += ltq_pinctrl_dt_subnode_size(np);
  142. *map = kzalloc(max_maps * sizeof(struct pinctrl_map) * 2, GFP_KERNEL);
  143. if (!*map)
  144. return -ENOMEM;
  145. tmp = *map;
  146. for_each_child_of_node(np_config, np)
  147. ltq_pinctrl_dt_subnode_to_map(pctldev, np, &tmp);
  148. *num_maps = ((int)(tmp - *map));
  149. return 0;
  150. }
  151. static const struct pinctrl_ops ltq_pctrl_ops = {
  152. .get_groups_count = ltq_get_group_count,
  153. .get_group_name = ltq_get_group_name,
  154. .get_group_pins = ltq_get_group_pins,
  155. .pin_dbg_show = ltq_pinctrl_pin_dbg_show,
  156. .dt_node_to_map = ltq_pinctrl_dt_node_to_map,
  157. .dt_free_map = ltq_pinctrl_dt_free_map,
  158. };
  159. static int ltq_pmx_func_count(struct pinctrl_dev *pctrldev)
  160. {
  161. struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
  162. return info->num_funcs;
  163. }
  164. static const char *ltq_pmx_func_name(struct pinctrl_dev *pctrldev,
  165. unsigned selector)
  166. {
  167. struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
  168. if (selector >= info->num_funcs)
  169. return NULL;
  170. return info->funcs[selector].name;
  171. }
  172. static int ltq_pmx_get_groups(struct pinctrl_dev *pctrldev,
  173. unsigned func,
  174. const char * const **groups,
  175. unsigned * const num_groups)
  176. {
  177. struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
  178. *groups = info->funcs[func].groups;
  179. *num_groups = info->funcs[func].num_groups;
  180. return 0;
  181. }
  182. /* Return function number. If failure, return negative value. */
  183. static int match_mux(const struct ltq_mfp_pin *mfp, unsigned mux)
  184. {
  185. int i;
  186. for (i = 0; i < LTQ_MAX_MUX; i++) {
  187. if (mfp->func[i] == mux)
  188. break;
  189. }
  190. if (i >= LTQ_MAX_MUX)
  191. return -EINVAL;
  192. return i;
  193. }
  194. /* dont assume .mfp is linearly mapped. find the mfp with the correct .pin */
  195. static int match_mfp(const struct ltq_pinmux_info *info, int pin)
  196. {
  197. int i;
  198. for (i = 0; i < info->num_mfp; i++) {
  199. if (info->mfp[i].pin == pin)
  200. return i;
  201. }
  202. return -1;
  203. }
  204. /* check whether current pin configuration is valid. Negative for failure */
  205. static int match_group_mux(const struct ltq_pin_group *grp,
  206. const struct ltq_pinmux_info *info,
  207. unsigned mux)
  208. {
  209. int i, pin, ret = 0;
  210. for (i = 0; i < grp->npins; i++) {
  211. pin = match_mfp(info, grp->pins[i]);
  212. if (pin < 0) {
  213. dev_err(info->dev, "could not find mfp for pin %d\n",
  214. grp->pins[i]);
  215. return -EINVAL;
  216. }
  217. ret = match_mux(&info->mfp[pin], mux);
  218. if (ret < 0) {
  219. dev_err(info->dev, "Can't find mux %d on pin%d\n",
  220. mux, pin);
  221. break;
  222. }
  223. }
  224. return ret;
  225. }
  226. static int ltq_pmx_set(struct pinctrl_dev *pctrldev,
  227. unsigned func,
  228. unsigned group)
  229. {
  230. struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
  231. const struct ltq_pin_group *pin_grp = &info->grps[group];
  232. int i, pin, pin_func, ret;
  233. if (!pin_grp->npins ||
  234. (match_group_mux(pin_grp, info, pin_grp->mux) < 0)) {
  235. dev_err(info->dev, "Failed to set the pin group: %s\n",
  236. info->grps[group].name);
  237. return -EINVAL;
  238. }
  239. for (i = 0; i < pin_grp->npins; i++) {
  240. pin = match_mfp(info, pin_grp->pins[i]);
  241. if (pin < 0) {
  242. dev_err(info->dev, "could not find mfp for pin %d\n",
  243. pin_grp->pins[i]);
  244. return -EINVAL;
  245. }
  246. pin_func = match_mux(&info->mfp[pin], pin_grp->mux);
  247. ret = info->apply_mux(pctrldev, pin, pin_func);
  248. if (ret) {
  249. dev_err(info->dev,
  250. "failed to apply mux %d for pin %d\n",
  251. pin_func, pin);
  252. return ret;
  253. }
  254. }
  255. return 0;
  256. }
  257. static int ltq_pmx_gpio_request_enable(struct pinctrl_dev *pctrldev,
  258. struct pinctrl_gpio_range *range,
  259. unsigned pin)
  260. {
  261. struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
  262. int mfp = match_mfp(info, pin);
  263. int pin_func;
  264. if (mfp < 0) {
  265. dev_err(info->dev, "could not find mfp for pin %d\n", pin);
  266. return -EINVAL;
  267. }
  268. pin_func = match_mux(&info->mfp[mfp], 0);
  269. if (pin_func < 0) {
  270. dev_err(info->dev, "No GPIO function on pin%d\n", mfp);
  271. return -EINVAL;
  272. }
  273. return info->apply_mux(pctrldev, mfp, pin_func);
  274. }
  275. static const struct pinmux_ops ltq_pmx_ops = {
  276. .get_functions_count = ltq_pmx_func_count,
  277. .get_function_name = ltq_pmx_func_name,
  278. .get_function_groups = ltq_pmx_get_groups,
  279. .set_mux = ltq_pmx_set,
  280. .gpio_request_enable = ltq_pmx_gpio_request_enable,
  281. };
  282. /*
  283. * allow different socs to register with the generic part of the lanti
  284. * pinctrl code
  285. */
  286. int ltq_pinctrl_register(struct platform_device *pdev,
  287. struct ltq_pinmux_info *info)
  288. {
  289. struct pinctrl_desc *desc;
  290. if (!info)
  291. return -EINVAL;
  292. desc = info->desc;
  293. desc->pctlops = &ltq_pctrl_ops;
  294. desc->pmxops = &ltq_pmx_ops;
  295. info->dev = &pdev->dev;
  296. info->pctrl = devm_pinctrl_register(&pdev->dev, desc, info);
  297. if (IS_ERR(info->pctrl)) {
  298. dev_err(&pdev->dev, "failed to register LTQ pinmux driver\n");
  299. return PTR_ERR(info->pctrl);
  300. }
  301. platform_set_drvdata(pdev, info);
  302. return 0;
  303. }