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