pinconf-generic.c 13 KB

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