pinctrl: pinconf-generic: Allow driver to specify DT params
[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 #include "pinctrl-utils.h"
28
29 #ifdef CONFIG_DEBUG_FS
30 static const struct pin_config_item conf_items[] = {
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),
36         PCONFDUMP(PIN_CONFIG_BIAS_PULL_PIN_DEFAULT,
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),
50 };
51
52 static 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)
57 {
58         int i;
59
60         for (i = 0; i < nitems; i++) {
61                 unsigned long config;
62                 int ret;
63
64                 /* We want to check out this parameter */
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);
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, " ");
80                 seq_puts(s, items[i].display);
81                 /* Print unit if available */
82                 if (items[i].has_arg) {
83                         seq_printf(s, " (%u",
84                                    pinconf_to_config_argument(config));
85                         if (items[i].format)
86                                 seq_printf(s, " %s)", items[i].format);
87                         else
88                                 seq_puts(s, ")");
89                 }
90         }
91 }
92
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  */
104 void pinconf_generic_dump_pins(struct pinctrl_dev *pctldev, struct seq_file *s,
105                                const char *gname, unsigned pin)
106 {
107         const struct pinconf_ops *ops = pctldev->desc->confops;
108
109         if (!ops->is_generic)
110                 return;
111
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);
120 }
121
122 void pinconf_generic_dump_config(struct pinctrl_dev *pctldev,
123                                  struct seq_file *s, unsigned long config)
124 {
125         int i;
126
127         for (i = 0; i < ARRAY_SIZE(conf_items); i++) {
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         }
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         }
143 }
144 EXPORT_SYMBOL_GPL(pinconf_generic_dump_config);
145 #endif
146
147 #ifdef CONFIG_OF
148 static const 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-enable", PIN_CONFIG_INPUT_ENABLE, 1 },
160         { "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 },
161         { "input-schmitt-enable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 1 },
162         { "input-schmitt-disable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 0 },
163         { "input-debounce", PIN_CONFIG_INPUT_DEBOUNCE, 0 },
164         { "power-source", PIN_CONFIG_POWER_SOURCE, 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         { "slew-rate", PIN_CONFIG_SLEW_RATE, 0},
170 };
171
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  */
185 static 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
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  */
220 int pinconf_generic_parse_dt_config(struct device_node *np,
221                                     struct pinctrl_dev *pctldev,
222                                     unsigned long **configs,
223                                     unsigned int *nconfigs)
224 {
225         unsigned long *cfg;
226         unsigned int max_cfg, ncfg = 0;
227         int ret;
228
229         if (!np)
230                 return -EINVAL;
231
232         /* allocate a temporary array big enough to hold one of each option */
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);
237         if (!cfg)
238                 return -ENOMEM;
239
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);
244
245         ret = 0;
246
247         /* no configs found at all */
248         if (ncfg == 0) {
249                 *configs = NULL;
250                 *nconfigs = 0;
251                 goto out;
252         }
253
254         /*
255          * Now limit the number of configs to the real number of
256          * found properties.
257          */
258         *configs = kmemdup(cfg, ncfg * sizeof(unsigned long), GFP_KERNEL);
259         if (!*configs) {
260                 ret = -ENOMEM;
261                 goto out;
262         }
263
264         *nconfigs = ncfg;
265
266 out:
267         kfree(cfg);
268         return ret;
269 }
270
271 int pinconf_generic_dt_subnode_to_map(struct pinctrl_dev *pctldev,
272                 struct device_node *np, struct pinctrl_map **map,
273                 unsigned *reserved_maps, unsigned *num_maps,
274                 enum pinctrl_map_type type)
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;
284         const char *subnode_target_type = "pins";
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)
290                         dev_err(dev, "could not parse property function\n");
291                 function = NULL;
292         }
293
294         ret = pinconf_generic_parse_dt_config(np, pctldev, &configs,
295                                               &num_configs);
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++;
306
307         ret = of_property_count_strings(np, "pins");
308         if (ret < 0) {
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;
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
328         of_property_for_each_string(np, subnode_target_type, prop, group) {
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,
340                                         num_configs, type);
341                         if (ret < 0)
342                                 goto exit;
343                 }
344         }
345         ret = 0;
346
347 exit:
348         kfree(configs);
349         return ret;
350 }
351 EXPORT_SYMBOL_GPL(pinconf_generic_dt_subnode_to_map);
352
353 int pinconf_generic_dt_node_to_map(struct pinctrl_dev *pctldev,
354                 struct device_node *np_config, struct pinctrl_map **map,
355                 unsigned *num_maps, enum pinctrl_map_type type)
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,
367                                         &reserved_maps, num_maps, type);
368                 if (ret < 0) {
369                         pinctrl_utils_dt_free_map(pctldev, *map, *num_maps);
370                         return ret;
371                 }
372         }
373         return 0;
374 }
375 EXPORT_SYMBOL_GPL(pinconf_generic_dt_node_to_map);
376
377 #endif