pinconf-generic.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. struct pin_config_item {
  28. const enum pin_config_param param;
  29. const char * const display;
  30. const char * const format;
  31. bool has_arg;
  32. };
  33. #define PCONFDUMP(a, b, c, d) { .param = a, .display = b, .format = c, \
  34. .has_arg = d }
  35. static const struct pin_config_item conf_items[] = {
  36. PCONFDUMP(PIN_CONFIG_BIAS_DISABLE, "input bias disabled", NULL, false),
  37. PCONFDUMP(PIN_CONFIG_BIAS_HIGH_IMPEDANCE, "input bias high impedance", NULL, false),
  38. PCONFDUMP(PIN_CONFIG_BIAS_BUS_HOLD, "input bias bus hold", NULL, false),
  39. PCONFDUMP(PIN_CONFIG_BIAS_PULL_UP, "input bias pull up", NULL, false),
  40. PCONFDUMP(PIN_CONFIG_BIAS_PULL_DOWN, "input bias pull down", NULL, false),
  41. PCONFDUMP(PIN_CONFIG_BIAS_PULL_PIN_DEFAULT,
  42. "input bias pull to pin specific state", NULL, false),
  43. PCONFDUMP(PIN_CONFIG_DRIVE_PUSH_PULL, "output drive push pull", NULL, false),
  44. PCONFDUMP(PIN_CONFIG_DRIVE_OPEN_DRAIN, "output drive open drain", NULL, false),
  45. PCONFDUMP(PIN_CONFIG_DRIVE_OPEN_SOURCE, "output drive open source", NULL, false),
  46. PCONFDUMP(PIN_CONFIG_DRIVE_STRENGTH, "output drive strength", "mA", true),
  47. PCONFDUMP(PIN_CONFIG_INPUT_ENABLE, "input enabled", NULL, false),
  48. PCONFDUMP(PIN_CONFIG_INPUT_SCHMITT_ENABLE, "input schmitt enabled", NULL, false),
  49. PCONFDUMP(PIN_CONFIG_INPUT_SCHMITT, "input schmitt trigger", NULL, false),
  50. PCONFDUMP(PIN_CONFIG_INPUT_DEBOUNCE, "input debounce", "usec", true),
  51. PCONFDUMP(PIN_CONFIG_POWER_SOURCE, "pin power source", "selector", true),
  52. PCONFDUMP(PIN_CONFIG_SLEW_RATE, "slew rate", NULL, true),
  53. PCONFDUMP(PIN_CONFIG_LOW_POWER_MODE, "pin low power", "mode", true),
  54. PCONFDUMP(PIN_CONFIG_OUTPUT, "pin output", "level", true),
  55. };
  56. void pinconf_generic_dump_pin(struct pinctrl_dev *pctldev,
  57. struct seq_file *s, unsigned pin)
  58. {
  59. const struct pinconf_ops *ops = pctldev->desc->confops;
  60. int i;
  61. if (!ops->is_generic)
  62. return;
  63. for (i = 0; i < ARRAY_SIZE(conf_items); i++) {
  64. unsigned long config;
  65. int ret;
  66. /* We want to check out this parameter */
  67. config = pinconf_to_config_packed(conf_items[i].param, 0);
  68. ret = pin_config_get_for_pin(pctldev, pin, &config);
  69. /* These are legal errors */
  70. if (ret == -EINVAL || ret == -ENOTSUPP)
  71. continue;
  72. if (ret) {
  73. seq_printf(s, "ERROR READING CONFIG SETTING %d ", i);
  74. continue;
  75. }
  76. /* Space between multiple configs */
  77. seq_puts(s, " ");
  78. seq_puts(s, conf_items[i].display);
  79. /* Print unit if available */
  80. if (conf_items[i].has_arg) {
  81. seq_printf(s, " (%u",
  82. pinconf_to_config_argument(config));
  83. if (conf_items[i].format)
  84. seq_printf(s, " %s)", conf_items[i].format);
  85. else
  86. seq_puts(s, ")");
  87. }
  88. }
  89. }
  90. void pinconf_generic_dump_group(struct pinctrl_dev *pctldev,
  91. struct seq_file *s, const char *gname)
  92. {
  93. const struct pinconf_ops *ops = pctldev->desc->confops;
  94. int i;
  95. if (!ops->is_generic)
  96. return;
  97. for (i = 0; i < ARRAY_SIZE(conf_items); i++) {
  98. unsigned long config;
  99. int ret;
  100. /* We want to check out this parameter */
  101. config = pinconf_to_config_packed(conf_items[i].param, 0);
  102. ret = pin_config_group_get(dev_name(pctldev->dev), gname,
  103. &config);
  104. /* These are legal errors */
  105. if (ret == -EINVAL || ret == -ENOTSUPP)
  106. continue;
  107. if (ret) {
  108. seq_printf(s, "ERROR READING CONFIG SETTING %d ", i);
  109. continue;
  110. }
  111. /* Space between multiple configs */
  112. seq_puts(s, " ");
  113. seq_puts(s, conf_items[i].display);
  114. /* Print unit if available */
  115. if (conf_items[i].has_arg) {
  116. seq_printf(s, " (%u",
  117. pinconf_to_config_argument(config));
  118. if (conf_items[i].format)
  119. seq_printf(s, " %s)", conf_items[i].format);
  120. else
  121. seq_puts(s, ")");
  122. }
  123. }
  124. }
  125. void pinconf_generic_dump_config(struct pinctrl_dev *pctldev,
  126. struct seq_file *s, unsigned long config)
  127. {
  128. int i;
  129. for (i = 0; i < ARRAY_SIZE(conf_items); i++) {
  130. if (pinconf_to_config_param(config) != conf_items[i].param)
  131. continue;
  132. seq_printf(s, "%s: 0x%x", conf_items[i].display,
  133. pinconf_to_config_argument(config));
  134. }
  135. }
  136. EXPORT_SYMBOL_GPL(pinconf_generic_dump_config);
  137. #endif
  138. #ifdef CONFIG_OF
  139. struct pinconf_generic_dt_params {
  140. const char * const property;
  141. enum pin_config_param param;
  142. u32 default_value;
  143. };
  144. static const struct pinconf_generic_dt_params dt_params[] = {
  145. { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
  146. { "bias-high-impedance", PIN_CONFIG_BIAS_HIGH_IMPEDANCE, 0 },
  147. { "bias-bus-hold", PIN_CONFIG_BIAS_BUS_HOLD, 0 },
  148. { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
  149. { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
  150. { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 },
  151. { "drive-push-pull", PIN_CONFIG_DRIVE_PUSH_PULL, 0 },
  152. { "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 },
  153. { "drive-open-source", PIN_CONFIG_DRIVE_OPEN_SOURCE, 0 },
  154. { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
  155. { "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 },
  156. { "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 },
  157. { "input-schmitt-enable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 1 },
  158. { "input-schmitt-disable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 0 },
  159. { "input-debounce", PIN_CONFIG_INPUT_DEBOUNCE, 0 },
  160. { "power-source", PIN_CONFIG_POWER_SOURCE, 0 },
  161. { "low-power-enable", PIN_CONFIG_LOW_POWER_MODE, 1 },
  162. { "low-power-disable", PIN_CONFIG_LOW_POWER_MODE, 0 },
  163. { "output-low", PIN_CONFIG_OUTPUT, 0, },
  164. { "output-high", PIN_CONFIG_OUTPUT, 1, },
  165. { "slew-rate", PIN_CONFIG_SLEW_RATE, 0},
  166. };
  167. /**
  168. * pinconf_generic_parse_dt_config()
  169. * parse the config properties into generic pinconfig values.
  170. * @np: node containing the pinconfig properties
  171. * @configs: array with nconfigs entries containing the generic pinconf values
  172. * @nconfigs: umber of configurations
  173. */
  174. int pinconf_generic_parse_dt_config(struct device_node *np,
  175. unsigned long **configs,
  176. unsigned int *nconfigs)
  177. {
  178. unsigned long *cfg;
  179. unsigned int ncfg = 0;
  180. int ret;
  181. int i;
  182. u32 val;
  183. if (!np)
  184. return -EINVAL;
  185. /* allocate a temporary array big enough to hold one of each option */
  186. cfg = kzalloc(sizeof(*cfg) * ARRAY_SIZE(dt_params), GFP_KERNEL);
  187. if (!cfg)
  188. return -ENOMEM;
  189. for (i = 0; i < ARRAY_SIZE(dt_params); i++) {
  190. const struct pinconf_generic_dt_params *par = &dt_params[i];
  191. ret = of_property_read_u32(np, par->property, &val);
  192. /* property not found */
  193. if (ret == -EINVAL)
  194. continue;
  195. /* use default value, when no value is specified */
  196. if (ret)
  197. val = par->default_value;
  198. pr_debug("found %s with value %u\n", par->property, val);
  199. cfg[ncfg] = pinconf_to_config_packed(par->param, val);
  200. ncfg++;
  201. }
  202. ret = 0;
  203. /* no configs found at all */
  204. if (ncfg == 0) {
  205. *configs = NULL;
  206. *nconfigs = 0;
  207. goto out;
  208. }
  209. /*
  210. * Now limit the number of configs to the real number of
  211. * found properties.
  212. */
  213. *configs = kmemdup(cfg, ncfg * sizeof(unsigned long), GFP_KERNEL);
  214. if (!*configs) {
  215. ret = -ENOMEM;
  216. goto out;
  217. }
  218. *nconfigs = ncfg;
  219. out:
  220. kfree(cfg);
  221. return ret;
  222. }
  223. int pinconf_generic_dt_subnode_to_map(struct pinctrl_dev *pctldev,
  224. struct device_node *np, struct pinctrl_map **map,
  225. unsigned *reserved_maps, unsigned *num_maps,
  226. enum pinctrl_map_type type)
  227. {
  228. int ret;
  229. const char *function;
  230. struct device *dev = pctldev->dev;
  231. unsigned long *configs = NULL;
  232. unsigned num_configs = 0;
  233. unsigned reserve;
  234. struct property *prop;
  235. const char *group;
  236. ret = of_property_read_string(np, "function", &function);
  237. if (ret < 0) {
  238. /* EINVAL=missing, which is fine since it's optional */
  239. if (ret != -EINVAL)
  240. dev_err(dev, "could not parse property function\n");
  241. function = NULL;
  242. }
  243. ret = pinconf_generic_parse_dt_config(np, &configs, &num_configs);
  244. if (ret < 0) {
  245. dev_err(dev, "could not parse node property\n");
  246. return ret;
  247. }
  248. reserve = 0;
  249. if (function != NULL)
  250. reserve++;
  251. if (num_configs)
  252. reserve++;
  253. ret = of_property_count_strings(np, "pins");
  254. if (ret < 0) {
  255. dev_err(dev, "could not parse property pins\n");
  256. goto exit;
  257. }
  258. reserve *= ret;
  259. ret = pinctrl_utils_reserve_map(pctldev, map, reserved_maps,
  260. num_maps, reserve);
  261. if (ret < 0)
  262. goto exit;
  263. of_property_for_each_string(np, "pins", prop, group) {
  264. if (function) {
  265. ret = pinctrl_utils_add_map_mux(pctldev, map,
  266. reserved_maps, num_maps, group,
  267. function);
  268. if (ret < 0)
  269. goto exit;
  270. }
  271. if (num_configs) {
  272. ret = pinctrl_utils_add_map_configs(pctldev, map,
  273. reserved_maps, num_maps, group, configs,
  274. num_configs, type);
  275. if (ret < 0)
  276. goto exit;
  277. }
  278. }
  279. ret = 0;
  280. exit:
  281. kfree(configs);
  282. return ret;
  283. }
  284. EXPORT_SYMBOL_GPL(pinconf_generic_dt_subnode_to_map);
  285. int pinconf_generic_dt_node_to_map(struct pinctrl_dev *pctldev,
  286. struct device_node *np_config, struct pinctrl_map **map,
  287. unsigned *num_maps, enum pinctrl_map_type type)
  288. {
  289. unsigned reserved_maps;
  290. struct device_node *np;
  291. int ret;
  292. reserved_maps = 0;
  293. *map = NULL;
  294. *num_maps = 0;
  295. for_each_child_of_node(np_config, np) {
  296. ret = pinconf_generic_dt_subnode_to_map(pctldev, np, map,
  297. &reserved_maps, num_maps, type);
  298. if (ret < 0) {
  299. pinctrl_utils_dt_free_map(pctldev, *map, *num_maps);
  300. return ret;
  301. }
  302. }
  303. return 0;
  304. }
  305. EXPORT_SYMBOL_GPL(pinconf_generic_dt_node_to_map);
  306. #endif