pinconf-generic.c 12 KB

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