berlin.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Marvell Berlin SoC pinctrl core driver
  3. *
  4. * Copyright (C) 2014 Marvell Technology Group Ltd.
  5. *
  6. * Antoine Ténart <antoine.tenart@free-electrons.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/io.h>
  13. #include <linux/mfd/syscon.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_device.h>
  18. #include <linux/pinctrl/pinctrl.h>
  19. #include <linux/pinctrl/pinmux.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/regmap.h>
  22. #include <linux/slab.h>
  23. #include "../core.h"
  24. #include "../pinctrl-utils.h"
  25. #include "berlin.h"
  26. struct berlin_pinctrl {
  27. struct regmap *regmap;
  28. struct device *dev;
  29. const struct berlin_pinctrl_desc *desc;
  30. struct berlin_pinctrl_function *functions;
  31. unsigned nfunctions;
  32. struct pinctrl_dev *pctrl_dev;
  33. };
  34. static int berlin_pinctrl_get_group_count(struct pinctrl_dev *pctrl_dev)
  35. {
  36. struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
  37. return pctrl->desc->ngroups;
  38. }
  39. static const char *berlin_pinctrl_get_group_name(struct pinctrl_dev *pctrl_dev,
  40. unsigned group)
  41. {
  42. struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
  43. return pctrl->desc->groups[group].name;
  44. }
  45. static int berlin_pinctrl_dt_node_to_map(struct pinctrl_dev *pctrl_dev,
  46. struct device_node *node,
  47. struct pinctrl_map **map,
  48. unsigned *num_maps)
  49. {
  50. struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
  51. struct property *prop;
  52. const char *function_name, *group_name;
  53. unsigned reserved_maps = 0;
  54. int ret, ngroups;
  55. *map = NULL;
  56. *num_maps = 0;
  57. ret = of_property_read_string(node, "function", &function_name);
  58. if (ret) {
  59. dev_err(pctrl->dev,
  60. "missing function property in node %s\n",
  61. node->name);
  62. return -EINVAL;
  63. }
  64. ngroups = of_property_count_strings(node, "groups");
  65. if (ngroups < 0) {
  66. dev_err(pctrl->dev,
  67. "missing groups property in node %s\n",
  68. node->name);
  69. return -EINVAL;
  70. }
  71. ret = pinctrl_utils_reserve_map(pctrl_dev, map, &reserved_maps,
  72. num_maps, ngroups);
  73. if (ret) {
  74. dev_err(pctrl->dev, "can't reserve map: %d\n", ret);
  75. return ret;
  76. }
  77. of_property_for_each_string(node, "groups", prop, group_name) {
  78. ret = pinctrl_utils_add_map_mux(pctrl_dev, map, &reserved_maps,
  79. num_maps, group_name,
  80. function_name);
  81. if (ret) {
  82. dev_err(pctrl->dev, "can't add map: %d\n", ret);
  83. return ret;
  84. }
  85. }
  86. return 0;
  87. }
  88. static const struct pinctrl_ops berlin_pinctrl_ops = {
  89. .get_groups_count = &berlin_pinctrl_get_group_count,
  90. .get_group_name = &berlin_pinctrl_get_group_name,
  91. .dt_node_to_map = &berlin_pinctrl_dt_node_to_map,
  92. .dt_free_map = &pinctrl_utils_dt_free_map,
  93. };
  94. static int berlin_pinmux_get_functions_count(struct pinctrl_dev *pctrl_dev)
  95. {
  96. struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
  97. return pctrl->nfunctions;
  98. }
  99. static const char *berlin_pinmux_get_function_name(struct pinctrl_dev *pctrl_dev,
  100. unsigned function)
  101. {
  102. struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
  103. return pctrl->functions[function].name;
  104. }
  105. static int berlin_pinmux_get_function_groups(struct pinctrl_dev *pctrl_dev,
  106. unsigned function,
  107. const char * const **groups,
  108. unsigned * const num_groups)
  109. {
  110. struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
  111. *groups = pctrl->functions[function].groups;
  112. *num_groups = pctrl->functions[function].ngroups;
  113. return 0;
  114. }
  115. static struct berlin_desc_function *
  116. berlin_pinctrl_find_function_by_name(struct berlin_pinctrl *pctrl,
  117. const struct berlin_desc_group *group,
  118. const char *fname)
  119. {
  120. struct berlin_desc_function *function = group->functions;
  121. while (function->name) {
  122. if (!strcmp(function->name, fname))
  123. return function;
  124. function++;
  125. }
  126. return NULL;
  127. }
  128. static int berlin_pinmux_set(struct pinctrl_dev *pctrl_dev,
  129. unsigned function,
  130. unsigned group)
  131. {
  132. struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
  133. const struct berlin_desc_group *group_desc = pctrl->desc->groups + group;
  134. struct berlin_pinctrl_function *func = pctrl->functions + function;
  135. struct berlin_desc_function *function_desc =
  136. berlin_pinctrl_find_function_by_name(pctrl, group_desc,
  137. func->name);
  138. u32 mask, val;
  139. if (!function_desc)
  140. return -EINVAL;
  141. mask = GENMASK(group_desc->lsb + group_desc->bit_width - 1,
  142. group_desc->lsb);
  143. val = function_desc->muxval << group_desc->lsb;
  144. regmap_update_bits(pctrl->regmap, group_desc->offset, mask, val);
  145. return 0;
  146. }
  147. static const struct pinmux_ops berlin_pinmux_ops = {
  148. .get_functions_count = &berlin_pinmux_get_functions_count,
  149. .get_function_name = &berlin_pinmux_get_function_name,
  150. .get_function_groups = &berlin_pinmux_get_function_groups,
  151. .set_mux = &berlin_pinmux_set,
  152. };
  153. static int berlin_pinctrl_add_function(struct berlin_pinctrl *pctrl,
  154. const char *name)
  155. {
  156. struct berlin_pinctrl_function *function = pctrl->functions;
  157. while (function->name) {
  158. if (!strcmp(function->name, name)) {
  159. function->ngroups++;
  160. return -EEXIST;
  161. }
  162. function++;
  163. }
  164. function->name = name;
  165. function->ngroups = 1;
  166. pctrl->nfunctions++;
  167. return 0;
  168. }
  169. static int berlin_pinctrl_build_state(struct platform_device *pdev)
  170. {
  171. struct berlin_pinctrl *pctrl = platform_get_drvdata(pdev);
  172. struct berlin_desc_group const *desc_group;
  173. struct berlin_desc_function const *desc_function;
  174. int i, max_functions = 0;
  175. pctrl->nfunctions = 0;
  176. for (i = 0; i < pctrl->desc->ngroups; i++) {
  177. desc_group = pctrl->desc->groups + i;
  178. /* compute the maxiumum number of functions a group can have */
  179. max_functions += 1 << (desc_group->bit_width + 1);
  180. }
  181. /* we will reallocate later */
  182. pctrl->functions = devm_kzalloc(&pdev->dev,
  183. max_functions * sizeof(*pctrl->functions),
  184. GFP_KERNEL);
  185. if (!pctrl->functions)
  186. return -ENOMEM;
  187. /* register all functions */
  188. for (i = 0; i < pctrl->desc->ngroups; i++) {
  189. desc_group = pctrl->desc->groups + i;
  190. desc_function = desc_group->functions;
  191. while (desc_function->name) {
  192. berlin_pinctrl_add_function(pctrl, desc_function->name);
  193. desc_function++;
  194. }
  195. }
  196. pctrl->functions = krealloc(pctrl->functions,
  197. pctrl->nfunctions * sizeof(*pctrl->functions),
  198. GFP_KERNEL);
  199. /* map functions to theirs groups */
  200. for (i = 0; i < pctrl->desc->ngroups; i++) {
  201. desc_group = pctrl->desc->groups + i;
  202. desc_function = desc_group->functions;
  203. while (desc_function->name) {
  204. struct berlin_pinctrl_function
  205. *function = pctrl->functions;
  206. const char **groups;
  207. bool found = false;
  208. while (function->name) {
  209. if (!strcmp(desc_function->name, function->name)) {
  210. found = true;
  211. break;
  212. }
  213. function++;
  214. }
  215. if (!found)
  216. return -EINVAL;
  217. if (!function->groups) {
  218. function->groups =
  219. devm_kzalloc(&pdev->dev,
  220. function->ngroups * sizeof(char *),
  221. GFP_KERNEL);
  222. if (!function->groups)
  223. return -ENOMEM;
  224. }
  225. groups = function->groups;
  226. while (*groups)
  227. groups++;
  228. *groups = desc_group->name;
  229. desc_function++;
  230. }
  231. }
  232. return 0;
  233. }
  234. static struct pinctrl_desc berlin_pctrl_desc = {
  235. .name = "berlin-pinctrl",
  236. .pctlops = &berlin_pinctrl_ops,
  237. .pmxops = &berlin_pinmux_ops,
  238. .owner = THIS_MODULE,
  239. };
  240. int berlin_pinctrl_probe_regmap(struct platform_device *pdev,
  241. const struct berlin_pinctrl_desc *desc,
  242. struct regmap *regmap)
  243. {
  244. struct device *dev = &pdev->dev;
  245. struct berlin_pinctrl *pctrl;
  246. int ret;
  247. pctrl = devm_kzalloc(dev, sizeof(*pctrl), GFP_KERNEL);
  248. if (!pctrl)
  249. return -ENOMEM;
  250. platform_set_drvdata(pdev, pctrl);
  251. pctrl->regmap = regmap;
  252. pctrl->dev = &pdev->dev;
  253. pctrl->desc = desc;
  254. ret = berlin_pinctrl_build_state(pdev);
  255. if (ret) {
  256. dev_err(dev, "cannot build driver state: %d\n", ret);
  257. return ret;
  258. }
  259. pctrl->pctrl_dev = pinctrl_register(&berlin_pctrl_desc, dev, pctrl);
  260. if (IS_ERR(pctrl->pctrl_dev)) {
  261. dev_err(dev, "failed to register pinctrl driver\n");
  262. return PTR_ERR(pctrl->pctrl_dev);
  263. }
  264. return 0;
  265. }
  266. int berlin_pinctrl_probe(struct platform_device *pdev,
  267. const struct berlin_pinctrl_desc *desc)
  268. {
  269. struct device *dev = &pdev->dev;
  270. struct device_node *parent_np = of_get_parent(dev->of_node);
  271. struct regmap *regmap = syscon_node_to_regmap(parent_np);
  272. of_node_put(parent_np);
  273. if (IS_ERR(regmap))
  274. return PTR_ERR(regmap);
  275. return berlin_pinctrl_probe_regmap(pdev, desc, regmap);
  276. }