pinconf-generic.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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/init.h>
  14. #include <linux/device.h>
  15. #include <linux/slab.h>
  16. #include <linux/debugfs.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/pinctrl/pinctrl.h>
  19. #include <linux/pinctrl/pinconf.h>
  20. #include <linux/pinctrl/pinconf-generic.h>
  21. #include <linux/of.h>
  22. #include "core.h"
  23. #include "pinconf.h"
  24. #ifdef CONFIG_DEBUG_FS
  25. struct pin_config_item {
  26. const enum pin_config_param param;
  27. const char * const display;
  28. const char * const format;
  29. };
  30. #define PCONFDUMP(a, b, c) { .param = a, .display = b, .format = c }
  31. struct pin_config_item conf_items[] = {
  32. PCONFDUMP(PIN_CONFIG_BIAS_DISABLE, "input bias disabled", NULL),
  33. PCONFDUMP(PIN_CONFIG_BIAS_HIGH_IMPEDANCE, "input bias high impedance", NULL),
  34. PCONFDUMP(PIN_CONFIG_BIAS_BUS_HOLD, "input bias bus hold", NULL),
  35. PCONFDUMP(PIN_CONFIG_BIAS_PULL_UP, "input bias pull up", NULL),
  36. PCONFDUMP(PIN_CONFIG_BIAS_PULL_DOWN, "input bias pull down", NULL),
  37. PCONFDUMP(PIN_CONFIG_BIAS_PULL_PIN_DEFAULT,
  38. "input bias pull to pin specific state", NULL),
  39. PCONFDUMP(PIN_CONFIG_DRIVE_PUSH_PULL, "output drive push pull", NULL),
  40. PCONFDUMP(PIN_CONFIG_DRIVE_OPEN_DRAIN, "output drive open drain", NULL),
  41. PCONFDUMP(PIN_CONFIG_DRIVE_OPEN_SOURCE, "output drive open source", NULL),
  42. PCONFDUMP(PIN_CONFIG_DRIVE_STRENGTH, "output drive strength", "mA"),
  43. PCONFDUMP(PIN_CONFIG_INPUT_SCHMITT_ENABLE, "input schmitt enabled", NULL),
  44. PCONFDUMP(PIN_CONFIG_INPUT_SCHMITT, "input schmitt trigger", NULL),
  45. PCONFDUMP(PIN_CONFIG_INPUT_DEBOUNCE, "input debounce", "time units"),
  46. PCONFDUMP(PIN_CONFIG_POWER_SOURCE, "pin power source", "selector"),
  47. PCONFDUMP(PIN_CONFIG_SLEW_RATE, "slew rate", NULL),
  48. PCONFDUMP(PIN_CONFIG_LOW_POWER_MODE, "pin low power", "mode"),
  49. PCONFDUMP(PIN_CONFIG_OUTPUT, "pin output", "level"),
  50. };
  51. void pinconf_generic_dump_pin(struct pinctrl_dev *pctldev,
  52. struct seq_file *s, unsigned pin)
  53. {
  54. const struct pinconf_ops *ops = pctldev->desc->confops;
  55. int i;
  56. if (!ops->is_generic)
  57. return;
  58. for(i = 0; i < ARRAY_SIZE(conf_items); i++) {
  59. unsigned long config;
  60. int ret;
  61. /* We want to check out this parameter */
  62. config = pinconf_to_config_packed(conf_items[i].param, 0);
  63. ret = pin_config_get_for_pin(pctldev, pin, &config);
  64. /* These are legal errors */
  65. if (ret == -EINVAL || ret == -ENOTSUPP)
  66. continue;
  67. if (ret) {
  68. seq_printf(s, "ERROR READING CONFIG SETTING %d ", i);
  69. continue;
  70. }
  71. /* Space between multiple configs */
  72. seq_puts(s, " ");
  73. seq_puts(s, conf_items[i].display);
  74. /* Print unit if available */
  75. if (conf_items[i].format &&
  76. pinconf_to_config_argument(config) != 0)
  77. seq_printf(s, " (%u %s)",
  78. pinconf_to_config_argument(config),
  79. conf_items[i].format);
  80. }
  81. }
  82. void pinconf_generic_dump_group(struct pinctrl_dev *pctldev,
  83. struct seq_file *s, const char *gname)
  84. {
  85. const struct pinconf_ops *ops = pctldev->desc->confops;
  86. int i;
  87. if (!ops->is_generic)
  88. return;
  89. for(i = 0; i < ARRAY_SIZE(conf_items); i++) {
  90. unsigned long config;
  91. int ret;
  92. /* We want to check out this parameter */
  93. config = pinconf_to_config_packed(conf_items[i].param, 0);
  94. ret = pin_config_group_get(dev_name(pctldev->dev), gname,
  95. &config);
  96. /* These are legal errors */
  97. if (ret == -EINVAL || ret == -ENOTSUPP)
  98. continue;
  99. if (ret) {
  100. seq_printf(s, "ERROR READING CONFIG SETTING %d ", i);
  101. continue;
  102. }
  103. /* Space between multiple configs */
  104. seq_puts(s, " ");
  105. seq_puts(s, conf_items[i].display);
  106. /* Print unit if available */
  107. if (conf_items[i].format && config != 0)
  108. seq_printf(s, " (%u %s)",
  109. pinconf_to_config_argument(config),
  110. conf_items[i].format);
  111. }
  112. }
  113. #endif
  114. #ifdef CONFIG_OF
  115. struct pinconf_generic_dt_params {
  116. const char * const property;
  117. enum pin_config_param param;
  118. u32 default_value;
  119. };
  120. static struct pinconf_generic_dt_params dt_params[] = {
  121. { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
  122. { "bias-high-impedance", PIN_CONFIG_BIAS_HIGH_IMPEDANCE, 0 },
  123. { "bias-bus-hold", PIN_CONFIG_BIAS_BUS_HOLD, 0 },
  124. { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
  125. { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
  126. { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 },
  127. { "drive-push-pull", PIN_CONFIG_DRIVE_PUSH_PULL, 0 },
  128. { "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 },
  129. { "drive-open-source", PIN_CONFIG_DRIVE_OPEN_SOURCE, 0 },
  130. { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
  131. { "input-schmitt-enable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 1 },
  132. { "input-schmitt-disable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 0 },
  133. { "input-schmitt", PIN_CONFIG_INPUT_SCHMITT, 0 },
  134. { "input-debounce", PIN_CONFIG_INPUT_DEBOUNCE, 0 },
  135. { "power-source", PIN_CONFIG_POWER_SOURCE, 0 },
  136. { "slew-rate", PIN_CONFIG_SLEW_RATE, 0 },
  137. { "low-power-enable", PIN_CONFIG_LOW_POWER_MODE, 1 },
  138. { "low-power-disable", PIN_CONFIG_LOW_POWER_MODE, 0 },
  139. { "output-low", PIN_CONFIG_OUTPUT, 0, },
  140. { "output-high", PIN_CONFIG_OUTPUT, 1, },
  141. };
  142. /**
  143. * pinconf_generic_parse_dt_config()
  144. * parse the config properties into generic pinconfig values.
  145. * @np: node containing the pinconfig properties
  146. * @configs: array with nconfigs entries containing the generic pinconf values
  147. * @nconfigs: umber of configurations
  148. */
  149. int pinconf_generic_parse_dt_config(struct device_node *np,
  150. unsigned long **configs,
  151. unsigned int *nconfigs)
  152. {
  153. unsigned long *cfg;
  154. unsigned int ncfg = 0;
  155. int ret;
  156. int i;
  157. u32 val;
  158. if (!np)
  159. return -EINVAL;
  160. /* allocate a temporary array big enough to hold one of each option */
  161. cfg = kzalloc(sizeof(*cfg) * ARRAY_SIZE(dt_params), GFP_KERNEL);
  162. if (!cfg)
  163. return -ENOMEM;
  164. for (i = 0; i < ARRAY_SIZE(dt_params); i++) {
  165. struct pinconf_generic_dt_params *par = &dt_params[i];
  166. ret = of_property_read_u32(np, par->property, &val);
  167. /* property not found */
  168. if (ret == -EINVAL)
  169. continue;
  170. /* use default value, when no value is specified */
  171. if (ret)
  172. val = par->default_value;
  173. pr_debug("found %s with value %u\n", par->property, val);
  174. cfg[ncfg] = pinconf_to_config_packed(par->param, val);
  175. ncfg++;
  176. }
  177. ret = 0;
  178. /* no configs found at all */
  179. if (ncfg == 0) {
  180. *configs = NULL;
  181. *nconfigs = 0;
  182. goto out;
  183. }
  184. /*
  185. * Now limit the number of configs to the real number of
  186. * found properties.
  187. */
  188. *configs = kzalloc(ncfg * sizeof(unsigned long), GFP_KERNEL);
  189. if (!*configs) {
  190. ret = -ENOMEM;
  191. goto out;
  192. }
  193. memcpy(*configs, cfg, ncfg * sizeof(unsigned long));
  194. *nconfigs = ncfg;
  195. out:
  196. kfree(cfg);
  197. return ret;
  198. }
  199. #endif