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