blob: 3d9a999fb6997f76028af553271708e86b4969b8 [file] [log] [blame]
Linus Walleij394349f2011-11-24 18:27:15 +01001/*
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>
Haojian Zhuang9cfd1722013-02-17 19:42:53 +080015#include <linux/module.h>
Linus Walleij394349f2011-11-24 18:27:15 +010016#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>
Heiko Stübner7db9af42013-06-10 21:40:29 +020024#include <linux/of.h>
Linus Walleij394349f2011-11-24 18:27:15 +010025#include "core.h"
26#include "pinconf.h"
Laxman Dewangane81c8f12013-08-06 18:42:34 +053027#include "pinctrl-utils.h"
Linus Walleij394349f2011-11-24 18:27:15 +010028
29#ifdef CONFIG_DEBUG_FS
30
31struct pin_config_item {
32 const enum pin_config_param param;
33 const char * const display;
34 const char * const format;
35};
36
37#define PCONFDUMP(a, b, c) { .param = a, .display = b, .format = c }
38
Sachin Kamat1ef465c2013-03-15 10:23:15 +053039static struct pin_config_item conf_items[] = {
Linus Walleij394349f2011-11-24 18:27:15 +010040 PCONFDUMP(PIN_CONFIG_BIAS_DISABLE, "input bias disabled", NULL),
41 PCONFDUMP(PIN_CONFIG_BIAS_HIGH_IMPEDANCE, "input bias high impedance", NULL),
James Hogana2df4262013-05-24 17:21:12 +010042 PCONFDUMP(PIN_CONFIG_BIAS_BUS_HOLD, "input bias bus hold", NULL),
Linus Walleij394349f2011-11-24 18:27:15 +010043 PCONFDUMP(PIN_CONFIG_BIAS_PULL_UP, "input bias pull up", NULL),
44 PCONFDUMP(PIN_CONFIG_BIAS_PULL_DOWN, "input bias pull down", NULL),
Heiko Stübner7970cb72013-06-06 16:44:25 +020045 PCONFDUMP(PIN_CONFIG_BIAS_PULL_PIN_DEFAULT,
46 "input bias pull to pin specific state", NULL),
Linus Walleij394349f2011-11-24 18:27:15 +010047 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),
James Hogan73ae3682013-05-24 17:21:11 +010050 PCONFDUMP(PIN_CONFIG_DRIVE_STRENGTH, "output drive strength", "mA"),
Sherman Yin8ba3f4d2013-12-11 10:37:17 -080051 PCONFDUMP(PIN_CONFIG_INPUT_ENABLE, "input enabled", NULL),
Haojian Zhuangea27c392013-02-12 01:10:57 +080052 PCONFDUMP(PIN_CONFIG_INPUT_SCHMITT_ENABLE, "input schmitt enabled", NULL),
Linus Walleij394349f2011-11-24 18:27:15 +010053 PCONFDUMP(PIN_CONFIG_INPUT_SCHMITT, "input schmitt trigger", NULL),
Heiko Stübner256aeb62013-06-25 14:56:11 +020054 PCONFDUMP(PIN_CONFIG_INPUT_DEBOUNCE, "input debounce", "usec"),
Linus Walleij394349f2011-11-24 18:27:15 +010055 PCONFDUMP(PIN_CONFIG_POWER_SOURCE, "pin power source", "selector"),
Haojian Zhuang684697c2013-01-18 15:31:15 +080056 PCONFDUMP(PIN_CONFIG_SLEW_RATE, "slew rate", NULL),
Linus Walleij394349f2011-11-24 18:27:15 +010057 PCONFDUMP(PIN_CONFIG_LOW_POWER_MODE, "pin low power", "mode"),
Linus Walleij483f33f2013-01-04 17:57:40 +010058 PCONFDUMP(PIN_CONFIG_OUTPUT, "pin output", "level"),
Linus Walleij394349f2011-11-24 18:27:15 +010059};
60
61void pinconf_generic_dump_pin(struct pinctrl_dev *pctldev,
62 struct seq_file *s, unsigned pin)
63{
64 const struct pinconf_ops *ops = pctldev->desc->confops;
65 int i;
66
67 if (!ops->is_generic)
68 return;
69
Sachin Kamatb6465422013-03-15 10:23:16 +053070 for (i = 0; i < ARRAY_SIZE(conf_items); i++) {
Linus Walleij394349f2011-11-24 18:27:15 +010071 unsigned long config;
72 int ret;
73
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)
79 continue;
80 if (ret) {
81 seq_printf(s, "ERROR READING CONFIG SETTING %d ", i);
82 continue;
83 }
84 /* Space between multiple configs */
85 seq_puts(s, " ");
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);
93 }
94}
95
96void pinconf_generic_dump_group(struct pinctrl_dev *pctldev,
97 struct seq_file *s, const char *gname)
98{
99 const struct pinconf_ops *ops = pctldev->desc->confops;
100 int i;
101
102 if (!ops->is_generic)
103 return;
104
Sachin Kamatb6465422013-03-15 10:23:16 +0530105 for (i = 0; i < ARRAY_SIZE(conf_items); i++) {
Linus Walleij394349f2011-11-24 18:27:15 +0100106 unsigned long config;
107 int ret;
108
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,
112 &config);
113 /* These are legal errors */
114 if (ret == -EINVAL || ret == -ENOTSUPP)
115 continue;
116 if (ret) {
117 seq_printf(s, "ERROR READING CONFIG SETTING %d ", i);
118 continue;
119 }
120 /* Space between multiple configs */
121 seq_puts(s, " ");
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);
128 }
129}
130
Haojian Zhuang9cfd1722013-02-17 19:42:53 +0800131void pinconf_generic_dump_config(struct pinctrl_dev *pctldev,
132 struct seq_file *s, unsigned long config)
133{
134 int i;
135
Sachin Kamatb6465422013-03-15 10:23:16 +0530136 for (i = 0; i < ARRAY_SIZE(conf_items); i++) {
Haojian Zhuang9cfd1722013-02-17 19:42:53 +0800137 if (pinconf_to_config_param(config) != conf_items[i].param)
138 continue;
139 seq_printf(s, "%s: 0x%x", conf_items[i].display,
140 pinconf_to_config_argument(config));
141 }
142}
143EXPORT_SYMBOL_GPL(pinconf_generic_dump_config);
Linus Walleij394349f2011-11-24 18:27:15 +0100144#endif
Heiko Stübner7db9af42013-06-10 21:40:29 +0200145
146#ifdef CONFIG_OF
147struct pinconf_generic_dt_params {
148 const char * const property;
149 enum pin_config_param param;
150 u32 default_value;
151};
152
153static 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 },
Heiko Stübner9ee1f7d2013-06-14 17:42:49 +0200157 { "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 },
Heiko Stübner7db9af42013-06-10 21:40:29 +0200160 { "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 },
Sherman Yin8ba3f4d2013-12-11 10:37:17 -0800164 { "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 },
165 { "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 },
Heiko Stübner7db9af42013-06-10 21:40:29 +0200166 { "input-schmitt-enable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 1 },
167 { "input-schmitt-disable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 0 },
Heiko Stübner7db9af42013-06-10 21:40:29 +0200168 { "input-debounce", PIN_CONFIG_INPUT_DEBOUNCE, 0 },
Heiko Stübner9ee1f7d2013-06-14 17:42:49 +0200169 { "low-power-enable", PIN_CONFIG_LOW_POWER_MODE, 1 },
170 { "low-power-disable", PIN_CONFIG_LOW_POWER_MODE, 0 },
Heiko Stübner7db9af42013-06-10 21:40:29 +0200171 { "output-low", PIN_CONFIG_OUTPUT, 0, },
172 { "output-high", PIN_CONFIG_OUTPUT, 1, },
Sherman Yin8ba3f4d2013-12-11 10:37:17 -0800173 { "slew-rate", PIN_CONFIG_SLEW_RATE, 0},
Heiko Stübner7db9af42013-06-10 21:40:29 +0200174};
175
176/**
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
182 */
183int pinconf_generic_parse_dt_config(struct device_node *np,
184 unsigned long **configs,
185 unsigned int *nconfigs)
186{
Heiko Stübner6abab2d2013-06-14 17:43:55 +0200187 unsigned long *cfg;
Heiko Stübner7db9af42013-06-10 21:40:29 +0200188 unsigned int ncfg = 0;
189 int ret;
190 int i;
191 u32 val;
192
193 if (!np)
194 return -EINVAL;
195
Heiko Stübner6abab2d2013-06-14 17:43:55 +0200196 /* allocate a temporary array big enough to hold one of each option */
197 cfg = kzalloc(sizeof(*cfg) * ARRAY_SIZE(dt_params), GFP_KERNEL);
198 if (!cfg)
199 return -ENOMEM;
200
Heiko Stübner7db9af42013-06-10 21:40:29 +0200201 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);
204
205 /* property not found */
206 if (ret == -EINVAL)
207 continue;
208
209 /* use default value, when no value is specified */
210 if (ret)
211 val = par->default_value;
212
213 pr_debug("found %s with value %u\n", par->property, val);
214 cfg[ncfg] = pinconf_to_config_packed(par->param, val);
215 ncfg++;
216 }
217
Heiko Stübner6abab2d2013-06-14 17:43:55 +0200218 ret = 0;
219
Heiko Stübnere4a88442013-06-14 17:43:21 +0200220 /* no configs found at all */
221 if (ncfg == 0) {
222 *configs = NULL;
223 *nconfigs = 0;
Heiko Stübner6abab2d2013-06-14 17:43:55 +0200224 goto out;
Heiko Stübnere4a88442013-06-14 17:43:21 +0200225 }
226
Heiko Stübner7db9af42013-06-10 21:40:29 +0200227 /*
228 * Now limit the number of configs to the real number of
229 * found properties.
230 */
231 *configs = kzalloc(ncfg * sizeof(unsigned long), GFP_KERNEL);
Heiko Stübner6abab2d2013-06-14 17:43:55 +0200232 if (!*configs) {
233 ret = -ENOMEM;
234 goto out;
235 }
Heiko Stübner7db9af42013-06-10 21:40:29 +0200236
Heiko Stübner6abab2d2013-06-14 17:43:55 +0200237 memcpy(*configs, cfg, ncfg * sizeof(unsigned long));
Heiko Stübner7db9af42013-06-10 21:40:29 +0200238 *nconfigs = ncfg;
Heiko Stübner6abab2d2013-06-14 17:43:55 +0200239
240out:
241 kfree(cfg);
242 return ret;
Heiko Stübner7db9af42013-06-10 21:40:29 +0200243}
Laxman Dewangane81c8f12013-08-06 18:42:34 +0530244
245int pinconf_generic_dt_subnode_to_map(struct pinctrl_dev *pctldev,
246 struct device_node *np, struct pinctrl_map **map,
Laxman Dewangan3287c242013-08-21 16:53:37 +0530247 unsigned *reserved_maps, unsigned *num_maps,
248 enum pinctrl_map_type type)
Laxman Dewangane81c8f12013-08-06 18:42:34 +0530249{
250 int ret;
251 const char *function;
252 struct device *dev = pctldev->dev;
253 unsigned long *configs = NULL;
254 unsigned num_configs = 0;
255 unsigned reserve;
256 struct property *prop;
257 const char *group;
258
259 ret = of_property_read_string(np, "function", &function);
260 if (ret < 0) {
261 /* EINVAL=missing, which is fine since it's optional */
262 if (ret != -EINVAL)
Axel Linacf564a2013-08-29 10:05:06 +0800263 dev_err(dev, "could not parse property function\n");
Laxman Dewangane81c8f12013-08-06 18:42:34 +0530264 function = NULL;
265 }
266
267 ret = pinconf_generic_parse_dt_config(np, &configs, &num_configs);
268 if (ret < 0) {
269 dev_err(dev, "could not parse node property\n");
270 return ret;
271 }
272
273 reserve = 0;
274 if (function != NULL)
275 reserve++;
276 if (num_configs)
277 reserve++;
278 ret = of_property_count_strings(np, "pins");
279 if (ret < 0) {
Axel Linacf564a2013-08-29 10:05:06 +0800280 dev_err(dev, "could not parse property pins\n");
Laxman Dewangane81c8f12013-08-06 18:42:34 +0530281 goto exit;
282 }
283 reserve *= ret;
284
285 ret = pinctrl_utils_reserve_map(pctldev, map, reserved_maps,
286 num_maps, reserve);
287 if (ret < 0)
288 goto exit;
289
290 of_property_for_each_string(np, "pins", prop, group) {
291 if (function) {
292 ret = pinctrl_utils_add_map_mux(pctldev, map,
293 reserved_maps, num_maps, group,
294 function);
295 if (ret < 0)
296 goto exit;
297 }
298
299 if (num_configs) {
300 ret = pinctrl_utils_add_map_configs(pctldev, map,
301 reserved_maps, num_maps, group, configs,
Laxman Dewangan3287c242013-08-21 16:53:37 +0530302 num_configs, type);
Laxman Dewangane81c8f12013-08-06 18:42:34 +0530303 if (ret < 0)
304 goto exit;
305 }
306 }
307 ret = 0;
308
309exit:
310 kfree(configs);
311 return ret;
312}
313EXPORT_SYMBOL_GPL(pinconf_generic_dt_subnode_to_map);
314
315int pinconf_generic_dt_node_to_map(struct pinctrl_dev *pctldev,
316 struct device_node *np_config, struct pinctrl_map **map,
Laxman Dewangan3287c242013-08-21 16:53:37 +0530317 unsigned *num_maps, enum pinctrl_map_type type)
Laxman Dewangane81c8f12013-08-06 18:42:34 +0530318{
319 unsigned reserved_maps;
320 struct device_node *np;
321 int ret;
322
323 reserved_maps = 0;
324 *map = NULL;
325 *num_maps = 0;
326
327 for_each_child_of_node(np_config, np) {
328 ret = pinconf_generic_dt_subnode_to_map(pctldev, np, map,
Laxman Dewangan3287c242013-08-21 16:53:37 +0530329 &reserved_maps, num_maps, type);
Laxman Dewangane81c8f12013-08-06 18:42:34 +0530330 if (ret < 0) {
331 pinctrl_utils_dt_free_map(pctldev, *map, *num_maps);
332 return ret;
333 }
334 }
335 return 0;
336}
337EXPORT_SYMBOL_GPL(pinconf_generic_dt_node_to_map);
338
Heiko Stübner7db9af42013-06-10 21:40:29 +0200339#endif