berlin.c 8.0 KB

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