pinctrl: qcom-spmi-gpio: Migrate to pinconf-generic
[linux-2.6-block.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"
e81c8f18 27#include "pinctrl-utils.h"
394349f7
LW
28
29#ifdef CONFIG_DEBUG_FS
2500bcc9 30static const struct pin_config_item conf_items[] = {
eec45071
SB
31 PCONFDUMP(PIN_CONFIG_BIAS_DISABLE, "input bias disabled", NULL, false),
32 PCONFDUMP(PIN_CONFIG_BIAS_HIGH_IMPEDANCE, "input bias high impedance", NULL, false),
33 PCONFDUMP(PIN_CONFIG_BIAS_BUS_HOLD, "input bias bus hold", NULL, false),
34 PCONFDUMP(PIN_CONFIG_BIAS_PULL_UP, "input bias pull up", NULL, false),
35 PCONFDUMP(PIN_CONFIG_BIAS_PULL_DOWN, "input bias pull down", NULL, false),
7970cb77 36 PCONFDUMP(PIN_CONFIG_BIAS_PULL_PIN_DEFAULT,
eec45071
SB
37 "input bias pull to pin specific state", NULL, false),
38 PCONFDUMP(PIN_CONFIG_DRIVE_PUSH_PULL, "output drive push pull", NULL, false),
39 PCONFDUMP(PIN_CONFIG_DRIVE_OPEN_DRAIN, "output drive open drain", NULL, false),
40 PCONFDUMP(PIN_CONFIG_DRIVE_OPEN_SOURCE, "output drive open source", NULL, false),
41 PCONFDUMP(PIN_CONFIG_DRIVE_STRENGTH, "output drive strength", "mA", true),
42 PCONFDUMP(PIN_CONFIG_INPUT_ENABLE, "input enabled", NULL, false),
43 PCONFDUMP(PIN_CONFIG_INPUT_SCHMITT_ENABLE, "input schmitt enabled", NULL, false),
44 PCONFDUMP(PIN_CONFIG_INPUT_SCHMITT, "input schmitt trigger", NULL, false),
45 PCONFDUMP(PIN_CONFIG_INPUT_DEBOUNCE, "input debounce", "usec", true),
46 PCONFDUMP(PIN_CONFIG_POWER_SOURCE, "pin power source", "selector", true),
47 PCONFDUMP(PIN_CONFIG_SLEW_RATE, "slew rate", NULL, true),
48 PCONFDUMP(PIN_CONFIG_LOW_POWER_MODE, "pin low power", "mode", true),
49 PCONFDUMP(PIN_CONFIG_OUTPUT, "pin output", "level", true),
394349f7
LW
50};
51
dd4d01f7
SB
52static void pinconf_generic_dump_one(struct pinctrl_dev *pctldev,
53 struct seq_file *s, const char *gname,
54 unsigned pin,
55 const struct pin_config_item *items,
56 int nitems)
394349f7 57{
394349f7
LW
58 int i;
59
dd4d01f7 60 for (i = 0; i < nitems; i++) {
394349f7
LW
61 unsigned long config;
62 int ret;
63
64 /* We want to check out this parameter */
dd4d01f7
SB
65 config = pinconf_to_config_packed(items[i].param, 0);
66 if (gname)
67 ret = pin_config_group_get(dev_name(pctldev->dev),
68 gname, &config);
69 else
70 ret = pin_config_get_for_pin(pctldev, pin, &config);
394349f7
LW
71 /* These are legal errors */
72 if (ret == -EINVAL || ret == -ENOTSUPP)
73 continue;
74 if (ret) {
75 seq_printf(s, "ERROR READING CONFIG SETTING %d ", i);
76 continue;
77 }
78 /* Space between multiple configs */
79 seq_puts(s, " ");
dd4d01f7 80 seq_puts(s, items[i].display);
394349f7 81 /* Print unit if available */
dd4d01f7 82 if (items[i].has_arg) {
eec45071
SB
83 seq_printf(s, " (%u",
84 pinconf_to_config_argument(config));
dd4d01f7
SB
85 if (items[i].format)
86 seq_printf(s, " %s)", items[i].format);
eec45071
SB
87 else
88 seq_puts(s, ")");
89 }
394349f7
LW
90 }
91}
92
dd4d01f7
SB
93/**
94 * pinconf_generic_dump_pins - Print information about pin or group of pins
95 * @pctldev: Pincontrol device
96 * @s: File to print to
97 * @gname: Group name specifying pins
98 * @pin: Pin number specyfying pin
99 *
100 * Print the pinconf configuration for the requested pin(s) to @s. Pins can be
101 * specified either by pin using @pin or by group using @gname. Only one needs
102 * to be specified the other can be NULL/0.
103 */
104void pinconf_generic_dump_pins(struct pinctrl_dev *pctldev, struct seq_file *s,
105 const char *gname, unsigned pin)
394349f7
LW
106{
107 const struct pinconf_ops *ops = pctldev->desc->confops;
394349f7
LW
108
109 if (!ops->is_generic)
110 return;
111
dd4d01f7
SB
112 /* generic parameters */
113 pinconf_generic_dump_one(pctldev, s, gname, pin, conf_items,
114 ARRAY_SIZE(conf_items));
115 /* driver-specific parameters */
116 if (pctldev->desc->num_dt_params && pctldev->desc->conf_items)
117 pinconf_generic_dump_one(pctldev, s, gname, pin,
118 pctldev->desc->conf_items,
119 pctldev->desc->num_dt_params);
394349f7
LW
120}
121
9cfd1724
HZ
122void pinconf_generic_dump_config(struct pinctrl_dev *pctldev,
123 struct seq_file *s, unsigned long config)
124{
125 int i;
126
b6465424 127 for (i = 0; i < ARRAY_SIZE(conf_items); i++) {
9cfd1724
HZ
128 if (pinconf_to_config_param(config) != conf_items[i].param)
129 continue;
130 seq_printf(s, "%s: 0x%x", conf_items[i].display,
131 pinconf_to_config_argument(config));
132 }
dd4d01f7
SB
133
134 if (!pctldev->desc->num_dt_params || !pctldev->desc->conf_items)
135 return;
136
137 for (i = 0; i < pctldev->desc->num_dt_params; i++) {
138 if (pinconf_to_config_param(config) != pctldev->desc->conf_items[i].param)
139 continue;
140 seq_printf(s, "%s: 0x%x", pctldev->desc->conf_items[i].display,
141 pinconf_to_config_argument(config));
142 }
9cfd1724
HZ
143}
144EXPORT_SYMBOL_GPL(pinconf_generic_dump_config);
394349f7 145#endif
7db9af4b
HS
146
147#ifdef CONFIG_OF
2500bcc9 148static const struct pinconf_generic_dt_params dt_params[] = {
7db9af4b
HS
149 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
150 { "bias-high-impedance", PIN_CONFIG_BIAS_HIGH_IMPEDANCE, 0 },
151 { "bias-bus-hold", PIN_CONFIG_BIAS_BUS_HOLD, 0 },
9ee1f7d2
HS
152 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
153 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
154 { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 },
7db9af4b
HS
155 { "drive-push-pull", PIN_CONFIG_DRIVE_PUSH_PULL, 0 },
156 { "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 },
157 { "drive-open-source", PIN_CONFIG_DRIVE_OPEN_SOURCE, 0 },
158 { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
8ba3f4d0
SY
159 { "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 },
160 { "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 },
7db9af4b
HS
161 { "input-schmitt-enable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 1 },
162 { "input-schmitt-disable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 0 },
7db9af4b 163 { "input-debounce", PIN_CONFIG_INPUT_DEBOUNCE, 0 },
ca6c5518 164 { "power-source", PIN_CONFIG_POWER_SOURCE, 0 },
9ee1f7d2
HS
165 { "low-power-enable", PIN_CONFIG_LOW_POWER_MODE, 1 },
166 { "low-power-disable", PIN_CONFIG_LOW_POWER_MODE, 0 },
7db9af4b
HS
167 { "output-low", PIN_CONFIG_OUTPUT, 0, },
168 { "output-high", PIN_CONFIG_OUTPUT, 1, },
8ba3f4d0 169 { "slew-rate", PIN_CONFIG_SLEW_RATE, 0},
7db9af4b
HS
170};
171
dd4d01f7
SB
172/**
173 * parse_dt_cfg - Parse DT pinconf parameters
174 * @np: DT node
175 * @params: Array of describing DT parameters
176 * @count: Number of entries in @params
177 * @cfg: Array of parsed config options
178 * @ncfg: Number of entries in @cfg
179 *
180 * Parse the config options described in @params from @np and puts the result
181 * in @cfg. @cfg does not need to be empty, entries are added beggining at
182 * @ncfg. @ncfg is updated to reflect the number of entries after parsing. @cfg
183 * needs to have enough memory allocated to hold all possible entries.
184 */
185static void parse_dt_cfg(struct device_node *np,
186 const struct pinconf_generic_dt_params *params,
187 unsigned int count, unsigned long *cfg,
188 unsigned int *ncfg)
189{
190 int i;
191
192 for (i = 0; i < count; i++) {
193 u32 val;
194 int ret;
195 const struct pinconf_generic_dt_params *par = &params[i];
196
197 ret = of_property_read_u32(np, par->property, &val);
198
199 /* property not found */
200 if (ret == -EINVAL)
201 continue;
202
203 /* use default value, when no value is specified */
204 if (ret)
205 val = par->default_value;
206
207 pr_debug("found %s with value %u\n", par->property, val);
208 cfg[*ncfg] = pinconf_to_config_packed(par->param, val);
209 (*ncfg)++;
210 }
211}
212
7db9af4b
HS
213/**
214 * pinconf_generic_parse_dt_config()
215 * parse the config properties into generic pinconfig values.
216 * @np: node containing the pinconfig properties
217 * @configs: array with nconfigs entries containing the generic pinconf values
218 * @nconfigs: umber of configurations
219 */
220int pinconf_generic_parse_dt_config(struct device_node *np,
dd4d01f7 221 struct pinctrl_dev *pctldev,
7db9af4b
HS
222 unsigned long **configs,
223 unsigned int *nconfigs)
224{
6abab2d4 225 unsigned long *cfg;
dd4d01f7 226 unsigned int max_cfg, ncfg = 0;
7db9af4b 227 int ret;
7db9af4b
HS
228
229 if (!np)
230 return -EINVAL;
231
6abab2d4 232 /* allocate a temporary array big enough to hold one of each option */
dd4d01f7
SB
233 max_cfg = ARRAY_SIZE(dt_params);
234 if (pctldev)
235 max_cfg += pctldev->desc->num_dt_params;
236 cfg = kcalloc(max_cfg, sizeof(*cfg), GFP_KERNEL);
6abab2d4
HS
237 if (!cfg)
238 return -ENOMEM;
239
dd4d01f7
SB
240 parse_dt_cfg(np, dt_params, ARRAY_SIZE(dt_params), cfg, &ncfg);
241 if (pctldev && pctldev->desc->num_dt_params && pctldev->desc->params)
242 parse_dt_cfg(np, pctldev->desc->params,
243 pctldev->desc->num_dt_params, cfg, &ncfg);
7db9af4b 244
6abab2d4
HS
245 ret = 0;
246
e4a8844c
HS
247 /* no configs found at all */
248 if (ncfg == 0) {
249 *configs = NULL;
250 *nconfigs = 0;
6abab2d4 251 goto out;
e4a8844c
HS
252 }
253
7db9af4b
HS
254 /*
255 * Now limit the number of configs to the real number of
256 * found properties.
257 */
db388dfb 258 *configs = kmemdup(cfg, ncfg * sizeof(unsigned long), GFP_KERNEL);
6abab2d4
HS
259 if (!*configs) {
260 ret = -ENOMEM;
261 goto out;
262 }
7db9af4b 263
7db9af4b 264 *nconfigs = ncfg;
6abab2d4
HS
265
266out:
267 kfree(cfg);
268 return ret;
7db9af4b 269}
e81c8f18
LD
270
271int pinconf_generic_dt_subnode_to_map(struct pinctrl_dev *pctldev,
272 struct device_node *np, struct pinctrl_map **map,
3287c240
LD
273 unsigned *reserved_maps, unsigned *num_maps,
274 enum pinctrl_map_type type)
e81c8f18
LD
275{
276 int ret;
277 const char *function;
278 struct device *dev = pctldev->dev;
279 unsigned long *configs = NULL;
280 unsigned num_configs = 0;
281 unsigned reserve;
282 struct property *prop;
283 const char *group;
31c89c95 284 const char *subnode_target_type = "pins";
e81c8f18
LD
285
286 ret = of_property_read_string(np, "function", &function);
287 if (ret < 0) {
288 /* EINVAL=missing, which is fine since it's optional */
289 if (ret != -EINVAL)
acf564a8 290 dev_err(dev, "could not parse property function\n");
e81c8f18
LD
291 function = NULL;
292 }
293
dd4d01f7
SB
294 ret = pinconf_generic_parse_dt_config(np, pctldev, &configs,
295 &num_configs);
e81c8f18
LD
296 if (ret < 0) {
297 dev_err(dev, "could not parse node property\n");
298 return ret;
299 }
300
301 reserve = 0;
302 if (function != NULL)
303 reserve++;
304 if (num_configs)
305 reserve++;
31c89c95 306
e81c8f18
LD
307 ret = of_property_count_strings(np, "pins");
308 if (ret < 0) {
31c89c95
SB
309 ret = of_property_count_strings(np, "groups");
310 if (ret < 0) {
311 dev_err(dev, "could not parse property pins/groups\n");
312 goto exit;
313 }
314 if (type == PIN_MAP_TYPE_INVALID)
315 type = PIN_MAP_TYPE_CONFIGS_GROUP;
316 subnode_target_type = "groups";
317 } else {
318 if (type == PIN_MAP_TYPE_INVALID)
319 type = PIN_MAP_TYPE_CONFIGS_PIN;
e81c8f18
LD
320 }
321 reserve *= ret;
322
323 ret = pinctrl_utils_reserve_map(pctldev, map, reserved_maps,
324 num_maps, reserve);
325 if (ret < 0)
326 goto exit;
327
31c89c95 328 of_property_for_each_string(np, subnode_target_type, prop, group) {
e81c8f18
LD
329 if (function) {
330 ret = pinctrl_utils_add_map_mux(pctldev, map,
331 reserved_maps, num_maps, group,
332 function);
333 if (ret < 0)
334 goto exit;
335 }
336
337 if (num_configs) {
338 ret = pinctrl_utils_add_map_configs(pctldev, map,
339 reserved_maps, num_maps, group, configs,
3287c240 340 num_configs, type);
e81c8f18
LD
341 if (ret < 0)
342 goto exit;
343 }
344 }
345 ret = 0;
346
347exit:
348 kfree(configs);
349 return ret;
350}
351EXPORT_SYMBOL_GPL(pinconf_generic_dt_subnode_to_map);
352
353int pinconf_generic_dt_node_to_map(struct pinctrl_dev *pctldev,
354 struct device_node *np_config, struct pinctrl_map **map,
3287c240 355 unsigned *num_maps, enum pinctrl_map_type type)
e81c8f18
LD
356{
357 unsigned reserved_maps;
358 struct device_node *np;
359 int ret;
360
361 reserved_maps = 0;
362 *map = NULL;
363 *num_maps = 0;
364
365 for_each_child_of_node(np_config, np) {
366 ret = pinconf_generic_dt_subnode_to_map(pctldev, np, map,
3287c240 367 &reserved_maps, num_maps, type);
e81c8f18
LD
368 if (ret < 0) {
369 pinctrl_utils_dt_free_map(pctldev, *map, *num_maps);
370 return ret;
371 }
372 }
373 return 0;
374}
375EXPORT_SYMBOL_GPL(pinconf_generic_dt_node_to_map);
376
7db9af4b 377#endif