pinconf-generic.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Core driver for the generic pin config portions of the pin control subsystem
  4. *
  5. * Copyright (C) 2011 ST-Ericsson SA
  6. * Written on behalf of Linaro for ST-Ericsson
  7. *
  8. * Author: Linus Walleij <linus.walleij@linaro.org>
  9. */
  10. #define pr_fmt(fmt) "generic pinconfig core: " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/device.h>
  15. #include <linux/slab.h>
  16. #include <linux/debugfs.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/pinctrl/pinctrl.h>
  19. #include <linux/pinctrl/pinconf.h>
  20. #include <linux/pinctrl/pinconf-generic.h>
  21. #include <linux/of.h>
  22. #include "core.h"
  23. #include "pinconf.h"
  24. #include "pinctrl-utils.h"
  25. #ifdef CONFIG_DEBUG_FS
  26. static const struct pin_config_item conf_items[] = {
  27. PCONFDUMP(PIN_CONFIG_BIAS_BUS_HOLD, "input bias bus hold", NULL, false),
  28. PCONFDUMP(PIN_CONFIG_BIAS_DISABLE, "input bias disabled", NULL, false),
  29. PCONFDUMP(PIN_CONFIG_BIAS_HIGH_IMPEDANCE, "input bias high impedance", NULL, false),
  30. PCONFDUMP(PIN_CONFIG_BIAS_PULL_DOWN, "input bias pull down", "ohms", true),
  31. PCONFDUMP(PIN_CONFIG_BIAS_PULL_PIN_DEFAULT,
  32. "input bias pull to pin specific state", "ohms", true),
  33. PCONFDUMP(PIN_CONFIG_BIAS_PULL_UP, "input bias pull up", "ohms", true),
  34. PCONFDUMP(PIN_CONFIG_DRIVE_OPEN_DRAIN, "output drive open drain", NULL, false),
  35. PCONFDUMP(PIN_CONFIG_DRIVE_OPEN_SOURCE, "output drive open source", NULL, false),
  36. PCONFDUMP(PIN_CONFIG_DRIVE_PUSH_PULL, "output drive push pull", NULL, false),
  37. PCONFDUMP(PIN_CONFIG_DRIVE_STRENGTH, "output drive strength", "mA", true),
  38. PCONFDUMP(PIN_CONFIG_DRIVE_STRENGTH_UA, "output drive strength", "uA", true),
  39. PCONFDUMP(PIN_CONFIG_INPUT_DEBOUNCE, "input debounce", "usec", true),
  40. PCONFDUMP(PIN_CONFIG_INPUT_ENABLE, "input enabled", NULL, false),
  41. PCONFDUMP(PIN_CONFIG_INPUT_SCHMITT, "input schmitt trigger", NULL, false),
  42. PCONFDUMP(PIN_CONFIG_INPUT_SCHMITT_ENABLE, "input schmitt enabled", NULL, false),
  43. PCONFDUMP(PIN_CONFIG_LOW_POWER_MODE, "pin low power", "mode", true),
  44. PCONFDUMP(PIN_CONFIG_OUTPUT_ENABLE, "output enabled", NULL, false),
  45. PCONFDUMP(PIN_CONFIG_OUTPUT, "pin output", "level", true),
  46. PCONFDUMP(PIN_CONFIG_POWER_SOURCE, "pin power source", "selector", true),
  47. PCONFDUMP(PIN_CONFIG_SLEEP_HARDWARE_STATE, "sleep hardware state", NULL, false),
  48. PCONFDUMP(PIN_CONFIG_SLEW_RATE, "slew rate", NULL, true),
  49. PCONFDUMP(PIN_CONFIG_SKEW_DELAY, "skew delay", NULL, true),
  50. };
  51. static void pinconf_generic_dump_one(struct pinctrl_dev *pctldev,
  52. struct seq_file *s, const char *gname,
  53. unsigned pin,
  54. const struct pin_config_item *items,
  55. int nitems, int *print_sep)
  56. {
  57. int i;
  58. for (i = 0; i < nitems; i++) {
  59. unsigned long config;
  60. int ret;
  61. /* We want to check out this parameter */
  62. config = pinconf_to_config_packed(items[i].param, 0);
  63. if (gname)
  64. ret = pin_config_group_get(dev_name(pctldev->dev),
  65. gname, &config);
  66. else
  67. ret = pin_config_get_for_pin(pctldev, pin, &config);
  68. /* These are legal errors */
  69. if (ret == -EINVAL || ret == -ENOTSUPP)
  70. continue;
  71. if (ret) {
  72. seq_printf(s, "ERROR READING CONFIG SETTING %d ", i);
  73. continue;
  74. }
  75. /* comma between multiple configs */
  76. if (*print_sep)
  77. seq_puts(s, ", ");
  78. *print_sep = 1;
  79. seq_puts(s, items[i].display);
  80. /* Print unit if available */
  81. if (items[i].has_arg) {
  82. seq_printf(s, " (%u",
  83. pinconf_to_config_argument(config));
  84. if (items[i].format)
  85. seq_printf(s, " %s)", items[i].format);
  86. else
  87. seq_puts(s, ")");
  88. }
  89. }
  90. }
  91. /**
  92. * pinconf_generic_dump_pins - Print information about pin or group of pins
  93. * @pctldev: Pincontrol device
  94. * @s: File to print to
  95. * @gname: Group name specifying pins
  96. * @pin: Pin number specyfying pin
  97. *
  98. * Print the pinconf configuration for the requested pin(s) to @s. Pins can be
  99. * specified either by pin using @pin or by group using @gname. Only one needs
  100. * to be specified the other can be NULL/0.
  101. */
  102. void pinconf_generic_dump_pins(struct pinctrl_dev *pctldev, struct seq_file *s,
  103. const char *gname, unsigned pin)
  104. {
  105. const struct pinconf_ops *ops = pctldev->desc->confops;
  106. int print_sep = 0;
  107. if (!ops->is_generic)
  108. return;
  109. /* generic parameters */
  110. pinconf_generic_dump_one(pctldev, s, gname, pin, conf_items,
  111. ARRAY_SIZE(conf_items), &print_sep);
  112. /* driver-specific parameters */
  113. if (pctldev->desc->num_custom_params &&
  114. pctldev->desc->custom_conf_items)
  115. pinconf_generic_dump_one(pctldev, s, gname, pin,
  116. pctldev->desc->custom_conf_items,
  117. pctldev->desc->num_custom_params,
  118. &print_sep);
  119. }
  120. void pinconf_generic_dump_config(struct pinctrl_dev *pctldev,
  121. struct seq_file *s, unsigned long config)
  122. {
  123. int i;
  124. for (i = 0; i < ARRAY_SIZE(conf_items); i++) {
  125. if (pinconf_to_config_param(config) != conf_items[i].param)
  126. continue;
  127. seq_printf(s, "%s: 0x%x", conf_items[i].display,
  128. pinconf_to_config_argument(config));
  129. }
  130. if (!pctldev->desc->num_custom_params ||
  131. !pctldev->desc->custom_conf_items)
  132. return;
  133. for (i = 0; i < pctldev->desc->num_custom_params; i++) {
  134. if (pinconf_to_config_param(config) !=
  135. pctldev->desc->custom_conf_items[i].param)
  136. continue;
  137. seq_printf(s, "%s: 0x%x",
  138. pctldev->desc->custom_conf_items[i].display,
  139. pinconf_to_config_argument(config));
  140. }
  141. }
  142. EXPORT_SYMBOL_GPL(pinconf_generic_dump_config);
  143. #endif
  144. #ifdef CONFIG_OF
  145. static const struct pinconf_generic_params dt_params[] = {
  146. { "bias-bus-hold", PIN_CONFIG_BIAS_BUS_HOLD, 0 },
  147. { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
  148. { "bias-high-impedance", PIN_CONFIG_BIAS_HIGH_IMPEDANCE, 0 },
  149. { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
  150. { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 },
  151. { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
  152. { "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 },
  153. { "drive-open-source", PIN_CONFIG_DRIVE_OPEN_SOURCE, 0 },
  154. { "drive-push-pull", PIN_CONFIG_DRIVE_PUSH_PULL, 0 },
  155. { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
  156. { "drive-strength-microamp", PIN_CONFIG_DRIVE_STRENGTH_UA, 0 },
  157. { "input-debounce", PIN_CONFIG_INPUT_DEBOUNCE, 0 },
  158. { "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 },
  159. { "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 },
  160. { "input-schmitt", PIN_CONFIG_INPUT_SCHMITT, 0 },
  161. { "input-schmitt-disable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 0 },
  162. { "input-schmitt-enable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 1 },
  163. { "low-power-disable", PIN_CONFIG_LOW_POWER_MODE, 0 },
  164. { "low-power-enable", PIN_CONFIG_LOW_POWER_MODE, 1 },
  165. { "output-disable", PIN_CONFIG_OUTPUT_ENABLE, 0 },
  166. { "output-enable", PIN_CONFIG_OUTPUT_ENABLE, 1 },
  167. { "output-high", PIN_CONFIG_OUTPUT, 1, },
  168. { "output-low", PIN_CONFIG_OUTPUT, 0, },
  169. { "power-source", PIN_CONFIG_POWER_SOURCE, 0 },
  170. { "sleep-hardware-state", PIN_CONFIG_SLEEP_HARDWARE_STATE, 0 },
  171. { "slew-rate", PIN_CONFIG_SLEW_RATE, 0 },
  172. { "skew-delay", PIN_CONFIG_SKEW_DELAY, 0 },
  173. };
  174. /**
  175. * parse_dt_cfg() - Parse DT pinconf parameters
  176. * @np: DT node
  177. * @params: Array of describing generic parameters
  178. * @count: Number of entries in @params
  179. * @cfg: Array of parsed config options
  180. * @ncfg: Number of entries in @cfg
  181. *
  182. * Parse the config options described in @params from @np and puts the result
  183. * in @cfg. @cfg does not need to be empty, entries are added beginning at
  184. * @ncfg. @ncfg is updated to reflect the number of entries after parsing. @cfg
  185. * needs to have enough memory allocated to hold all possible entries.
  186. */
  187. static void parse_dt_cfg(struct device_node *np,
  188. const struct pinconf_generic_params *params,
  189. unsigned int count, unsigned long *cfg,
  190. unsigned int *ncfg)
  191. {
  192. int i;
  193. for (i = 0; i < count; i++) {
  194. u32 val;
  195. int ret;
  196. const struct pinconf_generic_params *par = &params[i];
  197. ret = of_property_read_u32(np, par->property, &val);
  198. /* property not found */
  199. if (ret == -EINVAL)
  200. continue;
  201. /* use default value, when no value is specified */
  202. if (ret)
  203. val = par->default_value;
  204. pr_debug("found %s with value %u\n", par->property, val);
  205. cfg[*ncfg] = pinconf_to_config_packed(par->param, val);
  206. (*ncfg)++;
  207. }
  208. }
  209. /**
  210. * pinconf_generic_parse_dt_config()
  211. * parse the config properties into generic pinconfig values.
  212. * @np: node containing the pinconfig properties
  213. * @pctldev: pincontrol device
  214. * @configs: array with nconfigs entries containing the generic pinconf values
  215. * must be freed when no longer necessary.
  216. * @nconfigs: number of configurations
  217. */
  218. int pinconf_generic_parse_dt_config(struct device_node *np,
  219. struct pinctrl_dev *pctldev,
  220. unsigned long **configs,
  221. unsigned int *nconfigs)
  222. {
  223. unsigned long *cfg;
  224. unsigned int max_cfg, ncfg = 0;
  225. int ret;
  226. if (!np)
  227. return -EINVAL;
  228. /* allocate a temporary array big enough to hold one of each option */
  229. max_cfg = ARRAY_SIZE(dt_params);
  230. if (pctldev)
  231. max_cfg += pctldev->desc->num_custom_params;
  232. cfg = kcalloc(max_cfg, sizeof(*cfg), GFP_KERNEL);
  233. if (!cfg)
  234. return -ENOMEM;
  235. parse_dt_cfg(np, dt_params, ARRAY_SIZE(dt_params), cfg, &ncfg);
  236. if (pctldev && pctldev->desc->num_custom_params &&
  237. pctldev->desc->custom_params)
  238. parse_dt_cfg(np, pctldev->desc->custom_params,
  239. pctldev->desc->num_custom_params, cfg, &ncfg);
  240. ret = 0;
  241. /* no configs found at all */
  242. if (ncfg == 0) {
  243. *configs = NULL;
  244. *nconfigs = 0;
  245. goto out;
  246. }
  247. /*
  248. * Now limit the number of configs to the real number of
  249. * found properties.
  250. */
  251. *configs = kmemdup(cfg, ncfg * sizeof(unsigned long), GFP_KERNEL);
  252. if (!*configs) {
  253. ret = -ENOMEM;
  254. goto out;
  255. }
  256. *nconfigs = ncfg;
  257. out:
  258. kfree(cfg);
  259. return ret;
  260. }
  261. EXPORT_SYMBOL_GPL(pinconf_generic_parse_dt_config);
  262. int pinconf_generic_dt_subnode_to_map(struct pinctrl_dev *pctldev,
  263. struct device_node *np, struct pinctrl_map **map,
  264. unsigned *reserved_maps, unsigned *num_maps,
  265. enum pinctrl_map_type type)
  266. {
  267. int ret;
  268. const char *function;
  269. struct device *dev = pctldev->dev;
  270. unsigned long *configs = NULL;
  271. unsigned num_configs = 0;
  272. unsigned reserve, strings_count;
  273. struct property *prop;
  274. const char *group;
  275. const char *subnode_target_type = "pins";
  276. ret = of_property_count_strings(np, "pins");
  277. if (ret < 0) {
  278. ret = of_property_count_strings(np, "groups");
  279. if (ret < 0)
  280. /* skip this node; may contain config child nodes */
  281. return 0;
  282. if (type == PIN_MAP_TYPE_INVALID)
  283. type = PIN_MAP_TYPE_CONFIGS_GROUP;
  284. subnode_target_type = "groups";
  285. } else {
  286. if (type == PIN_MAP_TYPE_INVALID)
  287. type = PIN_MAP_TYPE_CONFIGS_PIN;
  288. }
  289. strings_count = ret;
  290. ret = of_property_read_string(np, "function", &function);
  291. if (ret < 0) {
  292. /* EINVAL=missing, which is fine since it's optional */
  293. if (ret != -EINVAL)
  294. dev_err(dev, "%pOF: could not parse property function\n",
  295. np);
  296. function = NULL;
  297. }
  298. ret = pinconf_generic_parse_dt_config(np, pctldev, &configs,
  299. &num_configs);
  300. if (ret < 0) {
  301. dev_err(dev, "%pOF: could not parse node property\n", np);
  302. return ret;
  303. }
  304. reserve = 0;
  305. if (function != NULL)
  306. reserve++;
  307. if (num_configs)
  308. reserve++;
  309. reserve *= strings_count;
  310. ret = pinctrl_utils_reserve_map(pctldev, map, reserved_maps,
  311. num_maps, reserve);
  312. if (ret < 0)
  313. goto exit;
  314. of_property_for_each_string(np, subnode_target_type, prop, group) {
  315. if (function) {
  316. ret = pinctrl_utils_add_map_mux(pctldev, map,
  317. reserved_maps, num_maps, group,
  318. function);
  319. if (ret < 0)
  320. goto exit;
  321. }
  322. if (num_configs) {
  323. ret = pinctrl_utils_add_map_configs(pctldev, map,
  324. reserved_maps, num_maps, group, configs,
  325. num_configs, type);
  326. if (ret < 0)
  327. goto exit;
  328. }
  329. }
  330. ret = 0;
  331. exit:
  332. kfree(configs);
  333. return ret;
  334. }
  335. EXPORT_SYMBOL_GPL(pinconf_generic_dt_subnode_to_map);
  336. int pinconf_generic_dt_node_to_map(struct pinctrl_dev *pctldev,
  337. struct device_node *np_config, struct pinctrl_map **map,
  338. unsigned *num_maps, enum pinctrl_map_type type)
  339. {
  340. unsigned reserved_maps;
  341. struct device_node *np;
  342. int ret;
  343. reserved_maps = 0;
  344. *map = NULL;
  345. *num_maps = 0;
  346. ret = pinconf_generic_dt_subnode_to_map(pctldev, np_config, map,
  347. &reserved_maps, num_maps, type);
  348. if (ret < 0)
  349. goto exit;
  350. for_each_available_child_of_node(np_config, np) {
  351. ret = pinconf_generic_dt_subnode_to_map(pctldev, np, map,
  352. &reserved_maps, num_maps, type);
  353. if (ret < 0)
  354. goto exit;
  355. }
  356. return 0;
  357. exit:
  358. pinctrl_utils_free_map(pctldev, *map, *num_maps);
  359. return ret;
  360. }
  361. EXPORT_SYMBOL_GPL(pinconf_generic_dt_node_to_map);
  362. void pinconf_generic_dt_free_map(struct pinctrl_dev *pctldev,
  363. struct pinctrl_map *map,
  364. unsigned num_maps)
  365. {
  366. pinctrl_utils_free_map(pctldev, map, num_maps);
  367. }
  368. EXPORT_SYMBOL_GPL(pinconf_generic_dt_free_map);
  369. #endif