pinconf-generic.c 9.1 KB

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