Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
fork
Configure Feed
Select the types of activity you want to include in your feed.
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Core driver for the generic pin config portions of the pin control subsystem
4 *
5 * Copyright (C) 2011 ST-Ericsson SA
6 * Written on behalf of Linaro for ST-Ericsson
7 *
8 * Author: Linus Walleij <linus.walleij@linaro.org>
9 */
10
11#define pr_fmt(fmt) "generic pinconfig core: " fmt
12
13#include <linux/array_size.h>
14#include <linux/debugfs.h>
15#include <linux/device.h>
16#include <linux/init.h>
17#include <linux/module.h>
18#include <linux/of.h>
19#include <linux/property.h>
20#include <linux/slab.h>
21#include <linux/seq_file.h>
22
23#include <linux/pinctrl/pinconf-generic.h>
24#include <linux/pinctrl/pinconf.h>
25#include <linux/pinctrl/pinctrl.h>
26
27#include "core.h"
28#include "pinconf.h"
29#include "pinctrl-utils.h"
30
31#ifdef CONFIG_DEBUG_FS
32static const struct pin_config_item conf_items[] = {
33 PCONFDUMP(PIN_CONFIG_BIAS_BUS_HOLD, "input bias bus hold", NULL, false),
34 PCONFDUMP(PIN_CONFIG_BIAS_DISABLE, "input bias disabled", NULL, false),
35 PCONFDUMP(PIN_CONFIG_BIAS_HIGH_IMPEDANCE, "input bias high impedance", NULL, false),
36 PCONFDUMP(PIN_CONFIG_BIAS_PULL_DOWN, "input bias pull down", "ohms", true),
37 PCONFDUMP(PIN_CONFIG_BIAS_PULL_PIN_DEFAULT,
38 "input bias pull to pin specific state", "ohms", true),
39 PCONFDUMP(PIN_CONFIG_BIAS_PULL_UP, "input bias pull up", "ohms", true),
40 PCONFDUMP(PIN_CONFIG_DRIVE_OPEN_DRAIN, "output drive open drain", NULL, false),
41 PCONFDUMP(PIN_CONFIG_DRIVE_OPEN_SOURCE, "output drive open source", NULL, false),
42 PCONFDUMP(PIN_CONFIG_DRIVE_PUSH_PULL, "output drive push pull", NULL, false),
43 PCONFDUMP(PIN_CONFIG_DRIVE_STRENGTH, "output drive strength", "mA", true),
44 PCONFDUMP(PIN_CONFIG_DRIVE_STRENGTH_UA, "output drive strength", "uA", true),
45 PCONFDUMP(PIN_CONFIG_INPUT_DEBOUNCE, "input debounce", "usec", true),
46 PCONFDUMP(PIN_CONFIG_INPUT_ENABLE, "input enabled", NULL, false),
47 PCONFDUMP(PIN_CONFIG_INPUT_SCHMITT, "input schmitt trigger", NULL, false),
48 PCONFDUMP(PIN_CONFIG_INPUT_SCHMITT_UV, "input schmitt threshold", "uV", true),
49 PCONFDUMP(PIN_CONFIG_INPUT_SCHMITT_ENABLE, "input schmitt enabled", NULL, false),
50 PCONFDUMP(PIN_CONFIG_MODE_LOW_POWER, "pin low power", "mode", true),
51 PCONFDUMP(PIN_CONFIG_OUTPUT_ENABLE, "output enabled", NULL, false),
52 PCONFDUMP(PIN_CONFIG_LEVEL, "pin output", "level", true),
53 PCONFDUMP(PIN_CONFIG_OUTPUT_IMPEDANCE_OHMS, "output impedance", "ohms", true),
54 PCONFDUMP(PIN_CONFIG_POWER_SOURCE, "pin power source", "selector", true),
55 PCONFDUMP(PIN_CONFIG_SLEEP_HARDWARE_STATE, "sleep hardware state", NULL, false),
56 PCONFDUMP(PIN_CONFIG_SLEW_RATE, "slew rate", NULL, true),
57 PCONFDUMP(PIN_CONFIG_SKEW_DELAY, "skew delay", NULL, true),
58 PCONFDUMP(PIN_CONFIG_SKEW_DELAY_INPUT_PS, "input skew delay", "ps", true),
59 PCONFDUMP(PIN_CONFIG_SKEW_DELAY_OUTPUT_PS, "output skew delay", "ps", true),
60 PCONFDUMP(PIN_CONFIG_INPUT_VOLTAGE_UV, "input voltage in microvolt", "uV", true),
61};
62
63static void pinconf_generic_dump_one(struct pinctrl_dev *pctldev,
64 struct seq_file *s, const char *gname,
65 unsigned int pin,
66 const struct pin_config_item *items,
67 int nitems, int *print_sep)
68{
69 int i;
70
71 for (i = 0; i < nitems; i++) {
72 const struct pin_config_item *item = &items[i];
73 unsigned long config;
74 int ret;
75
76 /* We want to check out this parameter */
77 config = pinconf_to_config_packed(item->param, 0);
78 if (gname)
79 ret = pin_config_group_get(dev_name(pctldev->dev),
80 gname, &config);
81 else
82 ret = pin_config_get_for_pin(pctldev, pin, &config);
83 /* These are legal errors */
84 if (ret == -EINVAL || ret == -ENOTSUPP)
85 continue;
86 if (ret) {
87 seq_printf(s, "ERROR READING CONFIG SETTING %d ", i);
88 continue;
89 }
90 /* comma between multiple configs */
91 if (*print_sep)
92 seq_puts(s, ", ");
93 *print_sep = 1;
94 seq_puts(s, item->display);
95 /* Print unit if available */
96 if (item->has_arg) {
97 u32 val = pinconf_to_config_argument(config);
98
99 if (item->format)
100 seq_printf(s, " (%u %s)", val, item->format);
101 else
102 seq_printf(s, " (0x%x)", val);
103
104 if (item->values && item->num_values) {
105 if (val < item->num_values)
106 seq_printf(s, " \"%s\"", item->values[val]);
107 else
108 seq_puts(s, " \"(unknown)\"");
109 }
110 }
111 }
112}
113
114/**
115 * pinconf_generic_dump_pins - Print information about pin or group of pins
116 * @pctldev: Pincontrol device
117 * @s: File to print to
118 * @gname: Group name specifying pins
119 * @pin: Pin number specifying pin
120 *
121 * Print the pinconf configuration for the requested pin(s) to @s. Pins can be
122 * specified either by pin using @pin or by group using @gname. Only one needs
123 * to be specified the other can be NULL/0.
124 */
125void pinconf_generic_dump_pins(struct pinctrl_dev *pctldev, struct seq_file *s,
126 const char *gname, unsigned int pin)
127{
128 const struct pinconf_ops *ops = pctldev->desc->confops;
129 int print_sep = 0;
130
131 if (!ops->is_generic)
132 return;
133
134 /* generic parameters */
135 pinconf_generic_dump_one(pctldev, s, gname, pin, conf_items,
136 ARRAY_SIZE(conf_items), &print_sep);
137 /* driver-specific parameters */
138 if (pctldev->desc->num_custom_params &&
139 pctldev->desc->custom_conf_items)
140 pinconf_generic_dump_one(pctldev, s, gname, pin,
141 pctldev->desc->custom_conf_items,
142 pctldev->desc->num_custom_params,
143 &print_sep);
144}
145
146void pinconf_generic_dump_config(struct pinctrl_dev *pctldev,
147 struct seq_file *s, unsigned long config)
148{
149 int i;
150
151 for (i = 0; i < ARRAY_SIZE(conf_items); i++) {
152 if (pinconf_to_config_param(config) != conf_items[i].param)
153 continue;
154 seq_printf(s, "%s: 0x%x", conf_items[i].display,
155 pinconf_to_config_argument(config));
156 }
157
158 if (!pctldev->desc->num_custom_params ||
159 !pctldev->desc->custom_conf_items)
160 return;
161
162 for (i = 0; i < pctldev->desc->num_custom_params; i++) {
163 if (pinconf_to_config_param(config) !=
164 pctldev->desc->custom_conf_items[i].param)
165 continue;
166 seq_printf(s, "%s: 0x%x",
167 pctldev->desc->custom_conf_items[i].display,
168 pinconf_to_config_argument(config));
169 }
170}
171EXPORT_SYMBOL_GPL(pinconf_generic_dump_config);
172#endif
173
174#ifdef CONFIG_OF
175static const struct pinconf_generic_params dt_params[] = {
176 { "bias-bus-hold", PIN_CONFIG_BIAS_BUS_HOLD, 0 },
177 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
178 { "bias-high-impedance", PIN_CONFIG_BIAS_HIGH_IMPEDANCE, 0 },
179 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
180 { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 },
181 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
182 { "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 },
183 { "drive-open-source", PIN_CONFIG_DRIVE_OPEN_SOURCE, 0 },
184 { "drive-push-pull", PIN_CONFIG_DRIVE_PUSH_PULL, 0 },
185 { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
186 { "drive-strength-microamp", PIN_CONFIG_DRIVE_STRENGTH_UA, 0 },
187 { "input-debounce", PIN_CONFIG_INPUT_DEBOUNCE, 0 },
188 { "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 },
189 { "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 },
190 { "input-schmitt", PIN_CONFIG_INPUT_SCHMITT, 0 },
191 { "input-schmitt-disable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 0 },
192 { "input-schmitt-enable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 1 },
193 { "input-schmitt-microvolts", PIN_CONFIG_INPUT_SCHMITT_UV, 0 },
194 { "low-power-disable", PIN_CONFIG_MODE_LOW_POWER, 0 },
195 { "low-power-enable", PIN_CONFIG_MODE_LOW_POWER, 1 },
196 { "output-disable", PIN_CONFIG_OUTPUT_ENABLE, 0 },
197 { "output-enable", PIN_CONFIG_OUTPUT_ENABLE, 1 },
198 { "output-high", PIN_CONFIG_LEVEL, 1, },
199 { "output-impedance-ohms", PIN_CONFIG_OUTPUT_IMPEDANCE_OHMS, 0 },
200 { "output-low", PIN_CONFIG_LEVEL, 0, },
201 { "power-source", PIN_CONFIG_POWER_SOURCE, 0 },
202 { "sleep-hardware-state", PIN_CONFIG_SLEEP_HARDWARE_STATE, 0 },
203 { "slew-rate", PIN_CONFIG_SLEW_RATE, 0 },
204 { "skew-delay", PIN_CONFIG_SKEW_DELAY, 0 },
205 { "skew-delay-input-ps", PIN_CONFIG_SKEW_DELAY_INPUT_PS, 0 },
206 { "skew-delay-output-ps", PIN_CONFIG_SKEW_DELAY_OUTPUT_PS, 0 },
207 { "input-threshold-voltage-microvolt", PIN_CONFIG_INPUT_VOLTAGE_UV, 0 },
208};
209
210/**
211 * parse_fw_cfg() - Parse firmware pinconf parameters
212 * @fwnode: firmware node
213 * @params: Array of describing generic parameters
214 * @count: Number of entries in @params
215 * @cfg: Array of parsed config options
216 * @ncfg: Number of entries in @cfg
217 *
218 * Parse the config options described in @params from @fwnode and puts the result
219 * in @cfg. @cfg does not need to be empty, entries are added beginning at
220 * @ncfg. @ncfg is updated to reflect the number of entries after parsing. @cfg
221 * needs to have enough memory allocated to hold all possible entries.
222 */
223static int parse_fw_cfg(struct fwnode_handle *fwnode,
224 const struct pinconf_generic_params *params,
225 unsigned int count, unsigned long *cfg,
226 unsigned int *ncfg)
227{
228 unsigned long *properties;
229 int i, test;
230
231 properties = bitmap_zalloc(count, GFP_KERNEL);
232
233 for (i = 0; i < count; i++) {
234 u32 val;
235 int ret;
236 const struct pinconf_generic_params *par = ¶ms[i];
237
238 if (par->values && par->num_values) {
239 ret = fwnode_property_match_property_string(fwnode,
240 par->property,
241 par->values, par->num_values);
242 if (ret == -ENOENT)
243 return ret;
244 if (ret >= 0) {
245 val = ret;
246 ret = 0;
247 }
248 } else {
249 ret = fwnode_property_read_u32(fwnode, par->property, &val);
250 }
251
252 /* property not found */
253 if (ret == -EINVAL)
254 continue;
255
256 /* use default value, when no value is specified */
257 if (ret)
258 val = par->default_value;
259
260 /* if param is greater than count, these are custom properties */
261 if (par->param <= count) {
262 ret = test_and_set_bit(par->param, properties);
263 if (ret) {
264 pr_err("%pfw: conflicting setting detected for %s\n",
265 fwnode, par->property);
266 bitmap_free(properties);
267 return -EINVAL;
268 }
269 }
270
271 pr_debug("found %s with value %u\n", par->property, val);
272 cfg[*ncfg] = pinconf_to_config_packed(par->param, val);
273 (*ncfg)++;
274 }
275
276 if (test_bit(PIN_CONFIG_DRIVE_STRENGTH, properties) &&
277 test_bit(PIN_CONFIG_DRIVE_STRENGTH_UA, properties))
278 pr_err("%pfw: cannot have multiple drive strength properties\n",
279 fwnode);
280
281 test = test_bit(PIN_CONFIG_BIAS_BUS_HOLD, properties) +
282 test_bit(PIN_CONFIG_BIAS_DISABLE, properties) +
283 test_bit(PIN_CONFIG_BIAS_HIGH_IMPEDANCE, properties) +
284 test_bit(PIN_CONFIG_BIAS_PULL_UP, properties) +
285 test_bit(PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, properties) +
286 test_bit(PIN_CONFIG_BIAS_PULL_DOWN, properties);
287 if (test > 1)
288 pr_err("%pfw: cannot have multiple bias configurations\n",
289 fwnode);
290
291 test = test_bit(PIN_CONFIG_DRIVE_OPEN_DRAIN, properties) +
292 test_bit(PIN_CONFIG_DRIVE_OPEN_SOURCE, properties) +
293 test_bit(PIN_CONFIG_DRIVE_PUSH_PULL, properties);
294 if (test > 1)
295 pr_err("%pfw: cannot have multiple drive configurations\n",
296 fwnode);
297
298 bitmap_free(properties);
299 return 0;
300}
301
302/**
303 * pinconf_generic_parse_dt_pinmux()
304 * parse the pinmux properties into generic pin mux values.
305 * @np: node containing the pinmux properties
306 * @dev: pincontrol core device
307 * @pid: array with pin identity entries
308 * @pmux: array with pin mux value entries
309 * @npins: number of pins
310 *
311 * pinmux property: mux value [0,7]bits and pin identity [8,31]bits.
312 */
313int pinconf_generic_parse_dt_pinmux(struct device_node *np, struct device *dev,
314 unsigned int **pid, unsigned int **pmux,
315 unsigned int *npins)
316{
317 struct fwnode_handle *fwnode = of_fwnode_handle(np);
318 unsigned int *pid_t;
319 unsigned int *pmux_t;
320 unsigned int npins_t, i;
321 int ret;
322
323 ret = fwnode_property_count_u32(fwnode, "pinmux");
324 if (ret < 0) {
325 dev_info(dev, "Missing pinmux property\n");
326 return ret;
327 }
328
329 npins_t = ret;
330 if (npins_t == 0) {
331 dev_info(dev, "pinmux property doesn't have entries\n");
332 return -ENODATA;
333 }
334
335 if (!pid || !pmux || !npins) {
336 dev_err(dev, "parameters error\n");
337 return -EINVAL;
338 }
339
340 pid_t = devm_kcalloc(dev, npins_t, sizeof(*pid_t), GFP_KERNEL);
341 pmux_t = devm_kcalloc(dev, npins_t, sizeof(*pmux_t), GFP_KERNEL);
342 if (!pid_t || !pmux_t) {
343 dev_err(dev, "kalloc memory fail\n");
344 return -ENOMEM;
345 }
346
347 ret = fwnode_property_read_u32_array(fwnode, "pinmux", pmux_t, npins_t);
348 if (ret) {
349 dev_err(dev, "get pinmux value fail\n");
350 goto exit;
351 }
352
353 for (i = 0; i < npins_t; i++) {
354 pid_t[i] = pmux_t[i] >> 8;
355 pmux_t[i] = pmux_t[i] & 0xff;
356 }
357 *pid = pid_t;
358 *pmux = pmux_t;
359 *npins = npins_t;
360
361 return 0;
362exit:
363 devm_kfree(dev, pid_t);
364 devm_kfree(dev, pmux_t);
365 return ret;
366}
367EXPORT_SYMBOL_GPL(pinconf_generic_parse_dt_pinmux);
368
369/**
370 * pinconf_generic_parse_dt_config()
371 * parse the config properties into generic pinconfig values.
372 * @np: node containing the pinconfig properties
373 * @pctldev: pincontrol device
374 * @configs: array with nconfigs entries containing the generic pinconf values
375 * must be freed when no longer necessary.
376 * @nconfigs: number of configurations
377 */
378int pinconf_generic_parse_dt_config(struct device_node *np,
379 struct pinctrl_dev *pctldev,
380 unsigned long **configs,
381 unsigned int *nconfigs)
382{
383 unsigned long *cfg;
384 unsigned int max_cfg, ncfg = 0;
385 struct fwnode_handle *fwnode;
386 int ret;
387
388 fwnode = of_fwnode_handle(np);
389 if (!fwnode)
390 return -EINVAL;
391
392 /* allocate a temporary array big enough to hold one of each option */
393 max_cfg = ARRAY_SIZE(dt_params);
394 if (pctldev)
395 max_cfg += pctldev->desc->num_custom_params;
396 cfg = kcalloc(max_cfg, sizeof(*cfg), GFP_KERNEL);
397 if (!cfg)
398 return -ENOMEM;
399
400 ret = parse_fw_cfg(fwnode, dt_params, ARRAY_SIZE(dt_params), cfg, &ncfg);
401 if (ret)
402 goto out;
403 if (pctldev && pctldev->desc->num_custom_params &&
404 pctldev->desc->custom_params) {
405 ret = parse_fw_cfg(fwnode, pctldev->desc->custom_params,
406 pctldev->desc->num_custom_params, cfg, &ncfg);
407 if (ret)
408 goto out;
409 }
410
411 /* no configs found at all */
412 if (ncfg == 0) {
413 *configs = NULL;
414 *nconfigs = 0;
415 goto out;
416 }
417
418 /*
419 * Now limit the number of configs to the real number of
420 * found properties.
421 */
422 *configs = kmemdup(cfg, ncfg * sizeof(unsigned long), GFP_KERNEL);
423 if (!*configs) {
424 ret = -ENOMEM;
425 goto out;
426 }
427
428 *nconfigs = ncfg;
429
430out:
431 kfree(cfg);
432 return ret;
433}
434EXPORT_SYMBOL_GPL(pinconf_generic_parse_dt_config);
435
436int pinconf_generic_dt_subnode_to_map(struct pinctrl_dev *pctldev,
437 struct device_node *np, struct pinctrl_map **map,
438 unsigned int *reserved_maps, unsigned int *num_maps,
439 enum pinctrl_map_type type)
440{
441 int ret;
442 const char *function;
443 struct device *dev = pctldev->dev;
444 unsigned long *configs = NULL;
445 unsigned int num_configs = 0;
446 unsigned int reserve, strings_count;
447 struct property *prop;
448 const char *group;
449 const char *subnode_target_type = "pins";
450
451 ret = of_property_count_strings(np, "pins");
452 if (ret < 0) {
453 ret = of_property_count_strings(np, "groups");
454 if (ret < 0)
455 /* skip this node; may contain config child nodes */
456 return 0;
457 if (type == PIN_MAP_TYPE_INVALID)
458 type = PIN_MAP_TYPE_CONFIGS_GROUP;
459 subnode_target_type = "groups";
460 } else {
461 if (type == PIN_MAP_TYPE_INVALID)
462 type = PIN_MAP_TYPE_CONFIGS_PIN;
463 }
464 strings_count = ret;
465
466 ret = of_property_read_string(np, "function", &function);
467 if (ret < 0) {
468 /* EINVAL=missing, which is fine since it's optional */
469 if (ret != -EINVAL)
470 dev_err(dev, "%pOF: could not parse property function\n",
471 np);
472 function = NULL;
473 }
474
475 ret = pinconf_generic_parse_dt_config(np, pctldev, &configs,
476 &num_configs);
477 if (ret < 0) {
478 dev_err(dev, "%pOF: could not parse node property\n", np);
479 return ret;
480 }
481
482 reserve = 0;
483 if (function != NULL)
484 reserve++;
485 if (num_configs)
486 reserve++;
487
488 reserve *= strings_count;
489
490 ret = pinctrl_utils_reserve_map(pctldev, map, reserved_maps,
491 num_maps, reserve);
492 if (ret < 0)
493 goto exit;
494
495 of_property_for_each_string(np, subnode_target_type, prop, group) {
496 if (function) {
497 ret = pinctrl_utils_add_map_mux(pctldev, map,
498 reserved_maps, num_maps, group,
499 function);
500 if (ret < 0)
501 goto exit;
502 }
503
504 if (num_configs) {
505 ret = pinctrl_utils_add_map_configs(pctldev, map,
506 reserved_maps, num_maps, group, configs,
507 num_configs, type);
508 if (ret < 0)
509 goto exit;
510 }
511 }
512 ret = 0;
513
514exit:
515 kfree(configs);
516 return ret;
517}
518EXPORT_SYMBOL_GPL(pinconf_generic_dt_subnode_to_map);
519
520int pinconf_generic_dt_node_to_map(struct pinctrl_dev *pctldev,
521 struct device_node *np_config, struct pinctrl_map **map,
522 unsigned int *num_maps, enum pinctrl_map_type type)
523{
524 unsigned int reserved_maps;
525 int ret;
526
527 reserved_maps = 0;
528 *map = NULL;
529 *num_maps = 0;
530
531 ret = pinconf_generic_dt_subnode_to_map(pctldev, np_config, map,
532 &reserved_maps, num_maps, type);
533 if (ret < 0)
534 goto exit;
535
536 for_each_available_child_of_node_scoped(np_config, np) {
537 ret = pinconf_generic_dt_subnode_to_map(pctldev, np, map,
538 &reserved_maps, num_maps, type);
539 if (ret < 0)
540 goto exit;
541 }
542 return 0;
543
544exit:
545 pinctrl_utils_free_map(pctldev, *map, *num_maps);
546 return ret;
547}
548EXPORT_SYMBOL_GPL(pinconf_generic_dt_node_to_map);
549
550void pinconf_generic_dt_free_map(struct pinctrl_dev *pctldev,
551 struct pinctrl_map *map,
552 unsigned int num_maps)
553{
554 pinctrl_utils_free_map(pctldev, map, num_maps);
555}
556EXPORT_SYMBOL_GPL(pinconf_generic_dt_free_map);
557
558#endif