pinconf-generic.c 9.3 KB

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