pinconf-generic.c 15 KB

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