pinctrl-lantiq.c 8.7 KB

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