File Index
Files and directories
Sign in
FILE

pinconf-generic.c

File preview with syntax highlighting.

linux-6.19.14/drivers/pinctrl/pinconf-generic.c C
  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
 31static 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
 61static 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 */
123void 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
144void 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}
169EXPORT_SYMBOL_GPL(pinconf_generic_dump_config);
170#endif
171
172#ifdef CONFIG_OF
173static 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 */
220static 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	int i;
226
227	for (i = 0; i < count; i++) {
228		u32 val;
229		int ret;
230		const struct pinconf_generic_params *par = &params[i];
231
232		if (par->values && par->num_values) {
233			ret = fwnode_property_match_property_string(of_fwnode_handle(np),
234								    par->property,
235								    par->values, par->num_values);
236			if (ret == -ENOENT)
237				return ret;
238			if (ret >= 0) {
239				val = ret;
240				ret = 0;
241			}
242		} else {
243			ret = of_property_read_u32(np, par->property, &val);
244		}
245
246		/* property not found */
247		if (ret == -EINVAL)
248			continue;
249
250		/* use default value, when no value is specified */
251		if (ret)
252			val = par->default_value;
253
254		pr_debug("found %s with value %u\n", par->property, val);
255		cfg[*ncfg] = pinconf_to_config_packed(par->param, val);
256		(*ncfg)++;
257	}
258
259	return 0;
260}
261
262/**
263 * pinconf_generic_parse_dt_pinmux()
264 * parse the pinmux properties into generic pin mux values.
265 * @np: node containing the pinmux properties
266 * @dev: pincontrol core device
267 * @pid: array with pin identity entries
268 * @pmux: array with pin mux value entries
269 * @npins: number of pins
270 *
271 * pinmux property: mux value [0,7]bits and pin identity [8,31]bits.
272 */
273int pinconf_generic_parse_dt_pinmux(struct device_node *np, struct device *dev,
274				    unsigned int **pid, unsigned int **pmux,
275				    unsigned int *npins)
276{
277	unsigned int *pid_t;
278	unsigned int *pmux_t;
279	struct property *prop;
280	unsigned int npins_t, i;
281	u32 value;
282	int ret;
283
284	prop = of_find_property(np, "pinmux", NULL);
285	if (!prop) {
286		dev_info(dev, "Missing pinmux property\n");
287		return -ENOENT;
288	}
289
290	if (!pid || !pmux || !npins) {
291		dev_err(dev, "parameters error\n");
292		return -EINVAL;
293	}
294
295	npins_t = prop->length / sizeof(u32);
296	pid_t = devm_kcalloc(dev, npins_t, sizeof(*pid_t), GFP_KERNEL);
297	pmux_t = devm_kcalloc(dev, npins_t, sizeof(*pmux_t), GFP_KERNEL);
298	if (!pid_t || !pmux_t) {
299		dev_err(dev, "kalloc memory fail\n");
300		return -ENOMEM;
301	}
302	for (i = 0; i < npins_t; i++) {
303		ret = of_property_read_u32_index(np, "pinmux", i, &value);
304		if (ret) {
305			dev_err(dev, "get pinmux value fail\n");
306			goto exit;
307		}
308		pmux_t[i] = value & 0xff;
309		pid_t[i] = (value >> 8) & 0xffffff;
310	}
311	*pid = pid_t;
312	*pmux = pmux_t;
313	*npins = npins_t;
314
315	return 0;
316exit:
317	devm_kfree(dev, pid_t);
318	devm_kfree(dev, pmux_t);
319	return ret;
320}
321EXPORT_SYMBOL_GPL(pinconf_generic_parse_dt_pinmux);
322
323/**
324 * pinconf_generic_parse_dt_config()
325 * parse the config properties into generic pinconfig values.
326 * @np: node containing the pinconfig properties
327 * @pctldev: pincontrol device
328 * @configs: array with nconfigs entries containing the generic pinconf values
329 *           must be freed when no longer necessary.
330 * @nconfigs: number of configurations
331 */
332int pinconf_generic_parse_dt_config(struct device_node *np,
333				    struct pinctrl_dev *pctldev,
334				    unsigned long **configs,
335				    unsigned int *nconfigs)
336{
337	unsigned long *cfg;
338	unsigned int max_cfg, ncfg = 0;
339	int ret;
340
341	if (!np)
342		return -EINVAL;
343
344	/* allocate a temporary array big enough to hold one of each option */
345	max_cfg = ARRAY_SIZE(dt_params);
346	if (pctldev)
347		max_cfg += pctldev->desc->num_custom_params;
348	cfg = kcalloc(max_cfg, sizeof(*cfg), GFP_KERNEL);
349	if (!cfg)
350		return -ENOMEM;
351
352	ret = parse_dt_cfg(np, dt_params, ARRAY_SIZE(dt_params), cfg, &ncfg);
353	if (ret)
354		goto out;
355	if (pctldev && pctldev->desc->num_custom_params &&
356		pctldev->desc->custom_params) {
357		ret = parse_dt_cfg(np, pctldev->desc->custom_params,
358				   pctldev->desc->num_custom_params, cfg, &ncfg);
359		if (ret)
360			goto out;
361	}
362
363	/* no configs found at all */
364	if (ncfg == 0) {
365		*configs = NULL;
366		*nconfigs = 0;
367		goto out;
368	}
369
370	/*
371	 * Now limit the number of configs to the real number of
372	 * found properties.
373	 */
374	*configs = kmemdup(cfg, ncfg * sizeof(unsigned long), GFP_KERNEL);
375	if (!*configs) {
376		ret = -ENOMEM;
377		goto out;
378	}
379
380	*nconfigs = ncfg;
381
382out:
383	kfree(cfg);
384	return ret;
385}
386EXPORT_SYMBOL_GPL(pinconf_generic_parse_dt_config);
387
388int pinconf_generic_dt_subnode_to_map(struct pinctrl_dev *pctldev,
389		struct device_node *np, struct pinctrl_map **map,
390		unsigned int *reserved_maps, unsigned int *num_maps,
391		enum pinctrl_map_type type)
392{
393	int ret;
394	const char *function;
395	struct device *dev = pctldev->dev;
396	unsigned long *configs = NULL;
397	unsigned int num_configs = 0;
398	unsigned int reserve, strings_count;
399	struct property *prop;
400	const char *group;
401	const char *subnode_target_type = "pins";
402
403	ret = of_property_count_strings(np, "pins");
404	if (ret < 0) {
405		ret = of_property_count_strings(np, "groups");
406		if (ret < 0)
407			/* skip this node; may contain config child nodes */
408			return 0;
409		if (type == PIN_MAP_TYPE_INVALID)
410			type = PIN_MAP_TYPE_CONFIGS_GROUP;
411		subnode_target_type = "groups";
412	} else {
413		if (type == PIN_MAP_TYPE_INVALID)
414			type = PIN_MAP_TYPE_CONFIGS_PIN;
415	}
416	strings_count = ret;
417
418	ret = of_property_read_string(np, "function", &function);
419	if (ret < 0) {
420		/* EINVAL=missing, which is fine since it's optional */
421		if (ret != -EINVAL)
422			dev_err(dev, "%pOF: could not parse property function\n",
423				np);
424		function = NULL;
425	}
426
427	ret = pinconf_generic_parse_dt_config(np, pctldev, &configs,
428					      &num_configs);
429	if (ret < 0) {
430		dev_err(dev, "%pOF: could not parse node property\n", np);
431		return ret;
432	}
433
434	reserve = 0;
435	if (function != NULL)
436		reserve++;
437	if (num_configs)
438		reserve++;
439
440	reserve *= strings_count;
441
442	ret = pinctrl_utils_reserve_map(pctldev, map, reserved_maps,
443			num_maps, reserve);
444	if (ret < 0)
445		goto exit;
446
447	of_property_for_each_string(np, subnode_target_type, prop, group) {
448		if (function) {
449			ret = pinctrl_utils_add_map_mux(pctldev, map,
450					reserved_maps, num_maps, group,
451					function);
452			if (ret < 0)
453				goto exit;
454		}
455
456		if (num_configs) {
457			ret = pinctrl_utils_add_map_configs(pctldev, map,
458					reserved_maps, num_maps, group, configs,
459					num_configs, type);
460			if (ret < 0)
461				goto exit;
462		}
463	}
464	ret = 0;
465
466exit:
467	kfree(configs);
468	return ret;
469}
470EXPORT_SYMBOL_GPL(pinconf_generic_dt_subnode_to_map);
471
472int pinconf_generic_dt_node_to_map(struct pinctrl_dev *pctldev,
473		struct device_node *np_config, struct pinctrl_map **map,
474		unsigned int *num_maps, enum pinctrl_map_type type)
475{
476	unsigned int reserved_maps;
477	int ret;
478
479	reserved_maps = 0;
480	*map = NULL;
481	*num_maps = 0;
482
483	ret = pinconf_generic_dt_subnode_to_map(pctldev, np_config, map,
484						&reserved_maps, num_maps, type);
485	if (ret < 0)
486		goto exit;
487
488	for_each_available_child_of_node_scoped(np_config, np) {
489		ret = pinconf_generic_dt_subnode_to_map(pctldev, np, map,
490					&reserved_maps, num_maps, type);
491		if (ret < 0)
492			goto exit;
493	}
494	return 0;
495
496exit:
497	pinctrl_utils_free_map(pctldev, *map, *num_maps);
498	return ret;
499}
500EXPORT_SYMBOL_GPL(pinconf_generic_dt_node_to_map);
501
502void pinconf_generic_dt_free_map(struct pinctrl_dev *pctldev,
503				 struct pinctrl_map *map,
504				 unsigned int num_maps)
505{
506	pinctrl_utils_free_map(pctldev, map, num_maps);
507}
508EXPORT_SYMBOL_GPL(pinconf_generic_dt_free_map);
509
510#endif
~/ Read-only file preview.