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