pinconf-generic.c 6.9 KB

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