pinctrl-generic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <dm/device_compat.h>
  8. #include <linux/compat.h>
  9. #include <dm/pinctrl.h>
  10. /**
  11. * pinctrl_pin_name_to_selector() - return the pin selector for a pin
  12. *
  13. * @dev: pin controller device
  14. * @pin: the pin name to look up
  15. * @return: pin selector, or negative error code on failure
  16. */
  17. static int pinctrl_pin_name_to_selector(struct udevice *dev, const char *pin)
  18. {
  19. const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
  20. unsigned npins, selector;
  21. if (!ops->get_pins_count || !ops->get_pin_name) {
  22. dev_dbg(dev, "get_pins_count or get_pin_name missing\n");
  23. return -ENOSYS;
  24. }
  25. npins = ops->get_pins_count(dev);
  26. /* See if this pctldev has this pin */
  27. for (selector = 0; selector < npins; selector++) {
  28. const char *pname = ops->get_pin_name(dev, selector);
  29. if (!strcmp(pin, pname))
  30. return selector;
  31. }
  32. return -ENOSYS;
  33. }
  34. /**
  35. * pinctrl_group_name_to_selector() - return the group selector for a group
  36. *
  37. * @dev: pin controller device
  38. * @group: the pin group name to look up
  39. * @return: pin group selector, or negative error code on failure
  40. */
  41. static int pinctrl_group_name_to_selector(struct udevice *dev,
  42. const char *group)
  43. {
  44. const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
  45. unsigned ngroups, selector;
  46. if (!ops->get_groups_count || !ops->get_group_name) {
  47. dev_dbg(dev, "get_groups_count or get_group_name missing\n");
  48. return -ENOSYS;
  49. }
  50. ngroups = ops->get_groups_count(dev);
  51. /* See if this pctldev has this group */
  52. for (selector = 0; selector < ngroups; selector++) {
  53. const char *gname = ops->get_group_name(dev, selector);
  54. if (!strcmp(group, gname))
  55. return selector;
  56. }
  57. return -ENOSYS;
  58. }
  59. #if CONFIG_IS_ENABLED(PINMUX)
  60. /**
  61. * pinmux_func_name_to_selector() - return the function selector for a function
  62. *
  63. * @dev: pin controller device
  64. * @function: the function name to look up
  65. * @return: function selector, or negative error code on failure
  66. */
  67. static int pinmux_func_name_to_selector(struct udevice *dev,
  68. const char *function)
  69. {
  70. const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
  71. unsigned nfuncs, selector = 0;
  72. if (!ops->get_functions_count || !ops->get_function_name) {
  73. dev_dbg(dev,
  74. "get_functions_count or get_function_name missing\n");
  75. return -ENOSYS;
  76. }
  77. nfuncs = ops->get_functions_count(dev);
  78. /* See if this pctldev has this function */
  79. for (selector = 0; selector < nfuncs; selector++) {
  80. const char *fname = ops->get_function_name(dev, selector);
  81. if (!strcmp(function, fname))
  82. return selector;
  83. }
  84. return -ENOSYS;
  85. }
  86. /**
  87. * pinmux_enable_setting() - enable pin-mux setting for a certain pin/group
  88. *
  89. * @dev: pin controller device
  90. * @is_group: target of operation (true: pin group, false: pin)
  91. * @selector: pin selector or group selector, depending on @is_group
  92. * @func_selector: function selector
  93. * @return: 0 on success, or negative error code on failure
  94. */
  95. static int pinmux_enable_setting(struct udevice *dev, bool is_group,
  96. unsigned selector, unsigned func_selector)
  97. {
  98. const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
  99. if (is_group) {
  100. if (!ops->pinmux_group_set) {
  101. dev_dbg(dev, "pinmux_group_set op missing\n");
  102. return -ENOSYS;
  103. }
  104. return ops->pinmux_group_set(dev, selector, func_selector);
  105. } else {
  106. if (!ops->pinmux_set) {
  107. dev_dbg(dev, "pinmux_set op missing\n");
  108. return -ENOSYS;
  109. }
  110. return ops->pinmux_set(dev, selector, func_selector);
  111. }
  112. }
  113. #else
  114. static int pinmux_func_name_to_selector(struct udevice *dev,
  115. const char *function)
  116. {
  117. return 0;
  118. }
  119. static int pinmux_enable_setting(struct udevice *dev, bool is_group,
  120. unsigned selector, unsigned func_selector)
  121. {
  122. return 0;
  123. }
  124. #endif
  125. #if CONFIG_IS_ENABLED(PINCONF)
  126. /**
  127. * pinconf_prop_name_to_param() - return parameter ID for a property name
  128. *
  129. * @dev: pin controller device
  130. * @property: property name in DTS, such as "bias-pull-up", "slew-rate", etc.
  131. * @default_value: return default value in case no value is specified in DTS
  132. * @return: return pamater ID, or negative error code on failure
  133. */
  134. static int pinconf_prop_name_to_param(struct udevice *dev,
  135. const char *property, u32 *default_value)
  136. {
  137. const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
  138. const struct pinconf_param *p, *end;
  139. if (!ops->pinconf_num_params || !ops->pinconf_params) {
  140. dev_dbg(dev, "pinconf_num_params or pinconf_params missing\n");
  141. return -ENOSYS;
  142. }
  143. p = ops->pinconf_params;
  144. end = p + ops->pinconf_num_params;
  145. /* See if this pctldev supports this parameter */
  146. for (; p < end; p++) {
  147. if (!strcmp(property, p->property)) {
  148. *default_value = p->default_value;
  149. return p->param;
  150. }
  151. }
  152. return -ENOSYS;
  153. }
  154. /**
  155. * pinconf_enable_setting() - apply pin configuration for a certain pin/group
  156. *
  157. * @dev: pin controller device
  158. * @is_group: target of operation (true: pin group, false: pin)
  159. * @selector: pin selector or group selector, depending on @is_group
  160. * @param: configuration paramter
  161. * @argument: argument taken by some configuration parameters
  162. * @return: 0 on success, or negative error code on failure
  163. */
  164. static int pinconf_enable_setting(struct udevice *dev, bool is_group,
  165. unsigned selector, unsigned param,
  166. u32 argument)
  167. {
  168. const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
  169. if (is_group) {
  170. if (!ops->pinconf_group_set) {
  171. dev_dbg(dev, "pinconf_group_set op missing\n");
  172. return -ENOSYS;
  173. }
  174. return ops->pinconf_group_set(dev, selector, param,
  175. argument);
  176. } else {
  177. if (!ops->pinconf_set) {
  178. dev_dbg(dev, "pinconf_set op missing\n");
  179. return -ENOSYS;
  180. }
  181. return ops->pinconf_set(dev, selector, param, argument);
  182. }
  183. }
  184. #else
  185. static int pinconf_prop_name_to_param(struct udevice *dev,
  186. const char *property, u32 *default_value)
  187. {
  188. return -ENOSYS;
  189. }
  190. static int pinconf_enable_setting(struct udevice *dev, bool is_group,
  191. unsigned selector, unsigned param,
  192. u32 argument)
  193. {
  194. return 0;
  195. }
  196. #endif
  197. enum pinmux_subnode_type {
  198. PST_NONE = 0,
  199. PST_PIN,
  200. PST_GROUP,
  201. PST_PINMUX,
  202. };
  203. /**
  204. * pinctrl_generic_set_state_one() - set state for a certain pin/group
  205. * Apply all pin multiplexing and pin configurations specified by @config
  206. * for a given pin or pin group.
  207. *
  208. * @dev: pin controller device
  209. * @config: pseudo device pointing to config node
  210. * @subnode_type: target of operation (pin, group, or pin specified by a pinmux
  211. * group)
  212. * @selector: pin selector or group selector, depending on @subnode_type
  213. * @return: 0 on success, or negative error code on failure
  214. */
  215. static int pinctrl_generic_set_state_one(struct udevice *dev,
  216. struct udevice *config,
  217. enum pinmux_subnode_type subnode_type,
  218. unsigned selector)
  219. {
  220. const char *propname;
  221. const void *value;
  222. struct ofprop property;
  223. int len, func_selector, param, ret;
  224. u32 arg, default_val;
  225. assert(subnode_type != PST_NONE);
  226. dev_for_each_property(property, config) {
  227. value = dev_read_prop_by_prop(&property, &propname, &len);
  228. if (!value)
  229. return -EINVAL;
  230. /* pinmux subnodes already have their muxing set */
  231. if (subnode_type != PST_PINMUX &&
  232. !strcmp(propname, "function")) {
  233. func_selector = pinmux_func_name_to_selector(dev,
  234. value);
  235. if (func_selector < 0)
  236. return func_selector;
  237. ret = pinmux_enable_setting(dev,
  238. subnode_type == PST_GROUP,
  239. selector,
  240. func_selector);
  241. } else {
  242. param = pinconf_prop_name_to_param(dev, propname,
  243. &default_val);
  244. if (param < 0)
  245. continue; /* just skip unknown properties */
  246. if (len >= sizeof(fdt32_t))
  247. arg = fdt32_to_cpu(*(fdt32_t *)value);
  248. else
  249. arg = default_val;
  250. ret = pinconf_enable_setting(dev,
  251. subnode_type == PST_GROUP,
  252. selector, param, arg);
  253. }
  254. if (ret)
  255. return ret;
  256. }
  257. return 0;
  258. }
  259. /**
  260. * pinctrl_generic_get_subnode_type() - determine whether there is a valid
  261. * pins, groups, or pinmux property in the config node
  262. *
  263. * @dev: pin controller device
  264. * @config: pseudo device pointing to config node
  265. * @count: number of specifiers contained within the property
  266. * @return: the type of the subnode, or PST_NONE
  267. */
  268. static enum pinmux_subnode_type pinctrl_generic_get_subnode_type(struct udevice *dev,
  269. struct udevice *config,
  270. int *count)
  271. {
  272. const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
  273. *count = dev_read_string_count(config, "pins");
  274. if (*count >= 0)
  275. return PST_PIN;
  276. *count = dev_read_string_count(config, "groups");
  277. if (*count >= 0)
  278. return PST_GROUP;
  279. if (ops->pinmux_property_set) {
  280. *count = dev_read_size(config, "pinmux");
  281. if (*count >= 0 && !(*count % sizeof(u32))) {
  282. *count /= sizeof(u32);
  283. return PST_PINMUX;
  284. }
  285. }
  286. *count = 0;
  287. return PST_NONE;
  288. }
  289. /**
  290. * pinctrl_generic_set_state_subnode() - apply all settings in config node
  291. *
  292. * @dev: pin controller device
  293. * @config: pseudo device pointing to config node
  294. * @return: 0 on success, or negative error code on failure
  295. */
  296. static int pinctrl_generic_set_state_subnode(struct udevice *dev,
  297. struct udevice *config)
  298. {
  299. enum pinmux_subnode_type subnode_type;
  300. const char *name;
  301. int count, selector, i, ret, scratch;
  302. const u32 *pinmux_groups = NULL; /* prevent use-uninitialized warning */
  303. subnode_type = pinctrl_generic_get_subnode_type(dev, config, &count);
  304. debug("%s(%s, %s): count=%d\n", __func__, dev->name, config->name,
  305. count);
  306. if (subnode_type == PST_PINMUX) {
  307. pinmux_groups = dev_read_prop(config, "pinmux", &scratch);
  308. if (!pinmux_groups)
  309. return -EINVAL;
  310. }
  311. for (i = 0; i < count; i++) {
  312. switch (subnode_type) {
  313. case PST_PIN:
  314. ret = dev_read_string_index(config, "pins", i, &name);
  315. if (ret)
  316. return ret;
  317. selector = pinctrl_pin_name_to_selector(dev, name);
  318. break;
  319. case PST_GROUP:
  320. ret = dev_read_string_index(config, "groups", i, &name);
  321. if (ret)
  322. return ret;
  323. selector = pinctrl_group_name_to_selector(dev, name);
  324. break;
  325. case PST_PINMUX: {
  326. const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
  327. u32 pinmux_group = fdt32_to_cpu(pinmux_groups[i]);
  328. /* Checked for in pinctrl_generic_get_subnode_type */
  329. selector = ops->pinmux_property_set(dev, pinmux_group);
  330. break;
  331. }
  332. case PST_NONE:
  333. default:
  334. /* skip this node; may contain config child nodes */
  335. return 0;
  336. }
  337. if (selector < 0)
  338. return selector;
  339. ret = pinctrl_generic_set_state_one(dev, config, subnode_type,
  340. selector);
  341. if (ret)
  342. return ret;
  343. }
  344. return 0;
  345. }
  346. int pinctrl_generic_set_state(struct udevice *dev, struct udevice *config)
  347. {
  348. struct udevice *child;
  349. int ret;
  350. ret = pinctrl_generic_set_state_subnode(dev, config);
  351. if (ret)
  352. return ret;
  353. for (device_find_first_child(config, &child);
  354. child;
  355. device_find_next_child(&child)) {
  356. ret = pinctrl_generic_set_state_subnode(dev, child);
  357. if (ret)
  358. return ret;
  359. }
  360. return 0;
  361. }