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