2 * Core driver for the generic pin config portions of the pin control subsystem
4 * Copyright (C) 2011 ST-Ericsson SA
5 * Written on behalf of Linaro for ST-Ericsson
7 * Author: Linus Walleij <linus.walleij@linaro.org>
9 * License terms: GNU General Public License (GPL) version 2
12 #define pr_fmt(fmt) "generic pinconfig core: " fmt
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/device.h>
18 #include <linux/slab.h>
19 #include <linux/debugfs.h>
20 #include <linux/seq_file.h>
21 #include <linux/pinctrl/pinctrl.h>
22 #include <linux/pinctrl/pinconf.h>
23 #include <linux/pinctrl/pinconf-generic.h>
27 #include "pinctrl-utils.h"
29 #ifdef CONFIG_DEBUG_FS
31 struct pin_config_item {
32 const enum pin_config_param param;
33 const char * const display;
34 const char * const format;
37 #define PCONFDUMP(a, b, c) { .param = a, .display = b, .format = c }
39 static struct pin_config_item conf_items[] = {
40 PCONFDUMP(PIN_CONFIG_BIAS_DISABLE, "input bias disabled", NULL),
41 PCONFDUMP(PIN_CONFIG_BIAS_HIGH_IMPEDANCE, "input bias high impedance", NULL),
42 PCONFDUMP(PIN_CONFIG_BIAS_BUS_HOLD, "input bias bus hold", NULL),
43 PCONFDUMP(PIN_CONFIG_BIAS_PULL_UP, "input bias pull up", NULL),
44 PCONFDUMP(PIN_CONFIG_BIAS_PULL_DOWN, "input bias pull down", NULL),
45 PCONFDUMP(PIN_CONFIG_BIAS_PULL_PIN_DEFAULT,
46 "input bias pull to pin specific state", NULL),
47 PCONFDUMP(PIN_CONFIG_DRIVE_PUSH_PULL, "output drive push pull", NULL),
48 PCONFDUMP(PIN_CONFIG_DRIVE_OPEN_DRAIN, "output drive open drain", NULL),
49 PCONFDUMP(PIN_CONFIG_DRIVE_OPEN_SOURCE, "output drive open source", NULL),
50 PCONFDUMP(PIN_CONFIG_DRIVE_STRENGTH, "output drive strength", "mA"),
51 PCONFDUMP(PIN_CONFIG_INPUT_ENABLE, "input enabled", NULL),
52 PCONFDUMP(PIN_CONFIG_INPUT_SCHMITT_ENABLE, "input schmitt enabled", NULL),
53 PCONFDUMP(PIN_CONFIG_INPUT_SCHMITT, "input schmitt trigger", NULL),
54 PCONFDUMP(PIN_CONFIG_INPUT_DEBOUNCE, "input debounce", "usec"),
55 PCONFDUMP(PIN_CONFIG_POWER_SOURCE, "pin power source", "selector"),
56 PCONFDUMP(PIN_CONFIG_SLEW_RATE, "slew rate", NULL),
57 PCONFDUMP(PIN_CONFIG_LOW_POWER_MODE, "pin low power", "mode"),
58 PCONFDUMP(PIN_CONFIG_OUTPUT, "pin output", "level"),
61 void pinconf_generic_dump_pin(struct pinctrl_dev *pctldev,
62 struct seq_file *s, unsigned pin)
64 const struct pinconf_ops *ops = pctldev->desc->confops;
70 for (i = 0; i < ARRAY_SIZE(conf_items); i++) {
74 /* We want to check out this parameter */
75 config = pinconf_to_config_packed(conf_items[i].param, 0);
76 ret = pin_config_get_for_pin(pctldev, pin, &config);
77 /* These are legal errors */
78 if (ret == -EINVAL || ret == -ENOTSUPP)
81 seq_printf(s, "ERROR READING CONFIG SETTING %d ", i);
84 /* Space between multiple configs */
86 seq_puts(s, conf_items[i].display);
87 /* Print unit if available */
88 if (conf_items[i].format &&
89 pinconf_to_config_argument(config) != 0)
90 seq_printf(s, " (%u %s)",
91 pinconf_to_config_argument(config),
92 conf_items[i].format);
96 void pinconf_generic_dump_group(struct pinctrl_dev *pctldev,
97 struct seq_file *s, const char *gname)
99 const struct pinconf_ops *ops = pctldev->desc->confops;
102 if (!ops->is_generic)
105 for (i = 0; i < ARRAY_SIZE(conf_items); i++) {
106 unsigned long config;
109 /* We want to check out this parameter */
110 config = pinconf_to_config_packed(conf_items[i].param, 0);
111 ret = pin_config_group_get(dev_name(pctldev->dev), gname,
113 /* These are legal errors */
114 if (ret == -EINVAL || ret == -ENOTSUPP)
117 seq_printf(s, "ERROR READING CONFIG SETTING %d ", i);
120 /* Space between multiple configs */
122 seq_puts(s, conf_items[i].display);
123 /* Print unit if available */
124 if (conf_items[i].format && config != 0)
125 seq_printf(s, " (%u %s)",
126 pinconf_to_config_argument(config),
127 conf_items[i].format);
131 void pinconf_generic_dump_config(struct pinctrl_dev *pctldev,
132 struct seq_file *s, unsigned long config)
136 for (i = 0; i < ARRAY_SIZE(conf_items); i++) {
137 if (pinconf_to_config_param(config) != conf_items[i].param)
139 seq_printf(s, "%s: 0x%x", conf_items[i].display,
140 pinconf_to_config_argument(config));
143 EXPORT_SYMBOL_GPL(pinconf_generic_dump_config);
147 struct pinconf_generic_dt_params {
148 const char * const property;
149 enum pin_config_param param;
153 static struct pinconf_generic_dt_params dt_params[] = {
154 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
155 { "bias-high-impedance", PIN_CONFIG_BIAS_HIGH_IMPEDANCE, 0 },
156 { "bias-bus-hold", PIN_CONFIG_BIAS_BUS_HOLD, 0 },
157 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
158 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
159 { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 },
160 { "drive-push-pull", PIN_CONFIG_DRIVE_PUSH_PULL, 0 },
161 { "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 },
162 { "drive-open-source", PIN_CONFIG_DRIVE_OPEN_SOURCE, 0 },
163 { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
164 { "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 },
165 { "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 },
166 { "input-schmitt-enable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 1 },
167 { "input-schmitt-disable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 0 },
168 { "input-debounce", PIN_CONFIG_INPUT_DEBOUNCE, 0 },
169 { "low-power-enable", PIN_CONFIG_LOW_POWER_MODE, 1 },
170 { "low-power-disable", PIN_CONFIG_LOW_POWER_MODE, 0 },
171 { "output-low", PIN_CONFIG_OUTPUT, 0, },
172 { "output-high", PIN_CONFIG_OUTPUT, 1, },
173 { "slew-rate", PIN_CONFIG_SLEW_RATE, 0},
177 * pinconf_generic_parse_dt_config()
178 * parse the config properties into generic pinconfig values.
179 * @np: node containing the pinconfig properties
180 * @configs: array with nconfigs entries containing the generic pinconf values
181 * @nconfigs: umber of configurations
183 int pinconf_generic_parse_dt_config(struct device_node *np,
184 unsigned long **configs,
185 unsigned int *nconfigs)
188 unsigned int ncfg = 0;
196 /* allocate a temporary array big enough to hold one of each option */
197 cfg = kzalloc(sizeof(*cfg) * ARRAY_SIZE(dt_params), GFP_KERNEL);
201 for (i = 0; i < ARRAY_SIZE(dt_params); i++) {
202 struct pinconf_generic_dt_params *par = &dt_params[i];
203 ret = of_property_read_u32(np, par->property, &val);
205 /* property not found */
209 /* use default value, when no value is specified */
211 val = par->default_value;
213 pr_debug("found %s with value %u\n", par->property, val);
214 cfg[ncfg] = pinconf_to_config_packed(par->param, val);
220 /* no configs found at all */
228 * Now limit the number of configs to the real number of
231 *configs = kzalloc(ncfg * sizeof(unsigned long), GFP_KERNEL);
237 memcpy(*configs, cfg, ncfg * sizeof(unsigned long));
245 int pinconf_generic_dt_subnode_to_map(struct pinctrl_dev *pctldev,
246 struct device_node *np, struct pinctrl_map **map,
247 unsigned *reserved_maps, unsigned *num_maps,
248 enum pinctrl_map_type type)
251 const char *function;
252 struct device *dev = pctldev->dev;
253 unsigned long *configs = NULL;
254 unsigned num_configs = 0;
256 struct property *prop;
259 ret = of_property_read_string(np, "function", &function);
261 /* EINVAL=missing, which is fine since it's optional */
263 dev_err(dev, "could not parse property function\n");
267 ret = pinconf_generic_parse_dt_config(np, &configs, &num_configs);
269 dev_err(dev, "could not parse node property\n");
274 if (function != NULL)
278 ret = of_property_count_strings(np, "pins");
280 dev_err(dev, "could not parse property pins\n");
285 ret = pinctrl_utils_reserve_map(pctldev, map, reserved_maps,
290 of_property_for_each_string(np, "pins", prop, group) {
292 ret = pinctrl_utils_add_map_mux(pctldev, map,
293 reserved_maps, num_maps, group,
300 ret = pinctrl_utils_add_map_configs(pctldev, map,
301 reserved_maps, num_maps, group, configs,
313 EXPORT_SYMBOL_GPL(pinconf_generic_dt_subnode_to_map);
315 int pinconf_generic_dt_node_to_map(struct pinctrl_dev *pctldev,
316 struct device_node *np_config, struct pinctrl_map **map,
317 unsigned *num_maps, enum pinctrl_map_type type)
319 unsigned reserved_maps;
320 struct device_node *np;
327 for_each_child_of_node(np_config, np) {
328 ret = pinconf_generic_dt_subnode_to_map(pctldev, np, map,
329 &reserved_maps, num_maps, type);
331 pinctrl_utils_dt_free_map(pctldev, *map, *num_maps);
337 EXPORT_SYMBOL_GPL(pinconf_generic_dt_node_to_map);