pinctrl-generic.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. static const char *alloc_name_with_prefix(const char *name, const char *prefix)
  204. {
  205. if (prefix) {
  206. char *name_with_prefix = malloc(strlen(prefix) + strlen(name) + 1);
  207. if (name_with_prefix)
  208. sprintf(name_with_prefix, "%s%s", prefix, name);
  209. return name_with_prefix;
  210. } else {
  211. return name;
  212. }
  213. }
  214. static void free_name_with_prefix(const char *name_with_prefix, const char *prefix)
  215. {
  216. if (prefix)
  217. free((char *)name_with_prefix);
  218. }
  219. /**
  220. * pinctrl_generic_set_state_one() - set state for a certain pin/group
  221. * Apply all pin multiplexing and pin configurations specified by @config
  222. * for a given pin or pin group.
  223. *
  224. * @dev: pin controller device
  225. * @config: pseudo device pointing to config node
  226. * @subnode_type: target of operation (pin, group, or pin specified by a pinmux
  227. * group)
  228. * @selector: pin selector or group selector, depending on @subnode_type
  229. * @return: 0 on success, or negative error code on failure
  230. */
  231. static int pinctrl_generic_set_state_one(struct udevice *dev,
  232. struct udevice *config,
  233. const char *prefix,
  234. enum pinmux_subnode_type subnode_type,
  235. unsigned selector)
  236. {
  237. const char *function_propname;
  238. const char *propname;
  239. const void *value;
  240. struct ofprop property;
  241. int len, func_selector, param, ret;
  242. u32 arg, default_val;
  243. assert(subnode_type != PST_NONE);
  244. function_propname = alloc_name_with_prefix("function", prefix);
  245. if (!function_propname)
  246. return -ENOMEM;
  247. dev_for_each_property(property, config) {
  248. value = dev_read_prop_by_prop(&property, &propname, &len);
  249. if (!value) {
  250. free_name_with_prefix(function_propname, prefix);
  251. return -EINVAL;
  252. }
  253. /* pinmux subnodes already have their muxing set */
  254. if (subnode_type != PST_PINMUX &&
  255. !strcmp(propname, function_propname)) {
  256. func_selector = pinmux_func_name_to_selector(dev,
  257. value);
  258. if (func_selector < 0) {
  259. free_name_with_prefix(function_propname, prefix);
  260. return func_selector;
  261. }
  262. ret = pinmux_enable_setting(dev,
  263. subnode_type == PST_GROUP,
  264. selector,
  265. func_selector);
  266. } else {
  267. param = pinconf_prop_name_to_param(dev, propname,
  268. &default_val);
  269. if (param < 0)
  270. continue; /* just skip unknown properties */
  271. if (len >= sizeof(fdt32_t))
  272. arg = fdt32_to_cpu(*(fdt32_t *)value);
  273. else
  274. arg = default_val;
  275. ret = pinconf_enable_setting(dev,
  276. subnode_type == PST_GROUP,
  277. selector, param, arg);
  278. }
  279. if (ret) {
  280. free_name_with_prefix(function_propname, prefix);
  281. return ret;
  282. }
  283. }
  284. free_name_with_prefix(function_propname, prefix);
  285. return 0;
  286. }
  287. /**
  288. * pinctrl_generic_get_subnode_type() - determine whether there is a valid
  289. * pins, groups, or pinmux property in the config node
  290. *
  291. * @dev: pin controller device
  292. * @config: pseudo device pointing to config node
  293. * @count: number of specifiers contained within the property
  294. * @return: the type of the subnode, or PST_NONE
  295. */
  296. static enum pinmux_subnode_type pinctrl_generic_get_subnode_type(struct udevice *dev,
  297. struct udevice *config,
  298. const char *prefix,
  299. int *count)
  300. {
  301. const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
  302. const char *propname;
  303. propname = alloc_name_with_prefix("pins", prefix);
  304. if (!propname)
  305. return -ENOMEM;
  306. *count = dev_read_string_count(config, propname);
  307. free_name_with_prefix(propname, prefix);
  308. if (*count >= 0)
  309. return PST_PIN;
  310. propname = alloc_name_with_prefix("groups", prefix);
  311. if (!propname)
  312. return -ENOMEM;
  313. *count = dev_read_string_count(config, propname);
  314. free_name_with_prefix(propname, prefix);
  315. if (*count >= 0)
  316. return PST_GROUP;
  317. if (ops->pinmux_property_set) {
  318. propname = alloc_name_with_prefix("pinmux", prefix);
  319. if (!propname)
  320. return -ENOMEM;
  321. *count = dev_read_size(config, propname);
  322. free_name_with_prefix(propname, prefix);
  323. if (*count >= 0 && !(*count % sizeof(u32))) {
  324. *count /= sizeof(u32);
  325. return PST_PINMUX;
  326. }
  327. }
  328. *count = 0;
  329. return PST_NONE;
  330. }
  331. /**
  332. * pinctrl_generic_set_state_subnode() - apply all settings in config node
  333. *
  334. * @dev: pin controller device
  335. * @config: pseudo device pointing to config node
  336. * @prefix: device tree property prefix (e.g. vendor specific)
  337. * @return: 0 on success, or negative error code on failure
  338. */
  339. static int pinctrl_generic_set_state_subnode(struct udevice *dev,
  340. struct udevice *config,
  341. const char *prefix)
  342. {
  343. enum pinmux_subnode_type subnode_type;
  344. const char *propname;
  345. const char *name;
  346. int count, selector, i, ret, scratch;
  347. const u32 *pinmux_groups = NULL; /* prevent use-uninitialized warning */
  348. subnode_type = pinctrl_generic_get_subnode_type(dev, config, prefix, &count);
  349. debug("%s(%s, %s): count=%d\n", __func__, dev->name, config->name,
  350. count);
  351. if (subnode_type == PST_PINMUX) {
  352. propname = alloc_name_with_prefix("pinmux", prefix);
  353. if (!propname)
  354. return -ENOMEM;
  355. pinmux_groups = dev_read_prop(config, propname, &scratch);
  356. free_name_with_prefix(propname, prefix);
  357. if (!pinmux_groups)
  358. return -EINVAL;
  359. }
  360. for (i = 0; i < count; i++) {
  361. switch (subnode_type) {
  362. case PST_PIN:
  363. propname = alloc_name_with_prefix("pins", prefix);
  364. if (!propname)
  365. return -ENOMEM;
  366. ret = dev_read_string_index(config, propname, i, &name);
  367. free_name_with_prefix(propname, prefix);
  368. if (ret)
  369. return ret;
  370. selector = pinctrl_pin_name_to_selector(dev, name);
  371. break;
  372. case PST_GROUP:
  373. propname = alloc_name_with_prefix("groups", prefix);
  374. if (!propname)
  375. return -ENOMEM;
  376. ret = dev_read_string_index(config, propname, i, &name);
  377. free_name_with_prefix(propname, prefix);
  378. if (ret)
  379. return ret;
  380. selector = pinctrl_group_name_to_selector(dev, name);
  381. break;
  382. case PST_PINMUX: {
  383. const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
  384. u32 pinmux_group = fdt32_to_cpu(pinmux_groups[i]);
  385. /* Checked for in pinctrl_generic_get_subnode_type */
  386. selector = ops->pinmux_property_set(dev, pinmux_group);
  387. break;
  388. }
  389. case PST_NONE:
  390. default:
  391. /* skip this node; may contain config child nodes */
  392. return 0;
  393. }
  394. if (selector < 0)
  395. return selector;
  396. ret = pinctrl_generic_set_state_one(dev, config, prefix,
  397. subnode_type, selector);
  398. if (ret)
  399. return ret;
  400. }
  401. return 0;
  402. }
  403. int pinctrl_generic_set_state_prefix(struct udevice *dev, struct udevice *config,
  404. const char *prefix)
  405. {
  406. struct udevice *child;
  407. int ret;
  408. ret = pinctrl_generic_set_state_subnode(dev, config, prefix);
  409. if (ret)
  410. return ret;
  411. for (device_find_first_child(config, &child);
  412. child;
  413. device_find_next_child(&child)) {
  414. ret = pinctrl_generic_set_state_subnode(dev, child, prefix);
  415. if (ret)
  416. return ret;
  417. }
  418. return 0;
  419. }
  420. int pinctrl_generic_set_state(struct udevice *dev, struct udevice *config)
  421. {
  422. return pinctrl_generic_set_state_prefix(dev, config, NULL);
  423. }