pinconf-generic.c 12 KB

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