1 // SPDX-License-Identifier: GPL-2.0+
7 #include <dm/device_compat.h>
8 #include <linux/compat.h>
9 #include <dm/pinctrl.h>
12 * pinctrl_pin_name_to_selector() - return the pin selector for a pin
14 * @dev: pin controller device
15 * @pin: the pin name to look up
16 * @return: pin selector, or negative error code on failure
18 static int pinctrl_pin_name_to_selector(struct udevice *dev, const char *pin)
20 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
21 unsigned npins, selector;
23 if (!ops->get_pins_count || !ops->get_pin_name) {
24 dev_dbg(dev, "get_pins_count or get_pin_name missing\n");
28 npins = ops->get_pins_count(dev);
30 /* See if this pctldev has this pin */
31 for (selector = 0; selector < npins; selector++) {
32 const char *pname = ops->get_pin_name(dev, selector);
34 if (!strcmp(pin, pname))
42 * pinctrl_group_name_to_selector() - return the group selector for a group
44 * @dev: pin controller device
45 * @group: the pin group name to look up
46 * @return: pin group selector, or negative error code on failure
48 static int pinctrl_group_name_to_selector(struct udevice *dev,
51 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
52 unsigned ngroups, selector;
54 if (!ops->get_groups_count || !ops->get_group_name) {
55 dev_dbg(dev, "get_groups_count or get_group_name missing\n");
59 ngroups = ops->get_groups_count(dev);
61 /* See if this pctldev has this group */
62 for (selector = 0; selector < ngroups; selector++) {
63 const char *gname = ops->get_group_name(dev, selector);
65 if (!strcmp(group, gname))
72 #if CONFIG_IS_ENABLED(PINMUX)
74 * pinmux_func_name_to_selector() - return the function selector for a function
76 * @dev: pin controller device
77 * @function: the function name to look up
78 * @return: function selector, or negative error code on failure
80 static int pinmux_func_name_to_selector(struct udevice *dev,
83 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
84 unsigned nfuncs, selector = 0;
86 if (!ops->get_functions_count || !ops->get_function_name) {
88 "get_functions_count or get_function_name missing\n");
92 nfuncs = ops->get_functions_count(dev);
94 /* See if this pctldev has this function */
95 for (selector = 0; selector < nfuncs; selector++) {
96 const char *fname = ops->get_function_name(dev, selector);
98 if (!strcmp(function, fname))
106 * pinmux_enable_setting() - enable pin-mux setting for a certain pin/group
108 * @dev: pin controller device
109 * @is_group: target of operation (true: pin group, false: pin)
110 * @selector: pin selector or group selector, depending on @is_group
111 * @func_selector: function selector
112 * @return: 0 on success, or negative error code on failure
114 static int pinmux_enable_setting(struct udevice *dev, bool is_group,
115 unsigned selector, unsigned func_selector)
117 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
120 if (!ops->pinmux_group_set) {
121 dev_dbg(dev, "pinmux_group_set op missing\n");
125 return ops->pinmux_group_set(dev, selector, func_selector);
127 if (!ops->pinmux_set) {
128 dev_dbg(dev, "pinmux_set op missing\n");
131 return ops->pinmux_set(dev, selector, func_selector);
135 static int pinmux_func_name_to_selector(struct udevice *dev,
136 const char *function)
141 static int pinmux_enable_setting(struct udevice *dev, bool is_group,
142 unsigned selector, unsigned func_selector)
148 #if CONFIG_IS_ENABLED(PINCONF)
150 * pinconf_prop_name_to_param() - return parameter ID for a property name
152 * @dev: pin controller device
153 * @property: property name in DTS, such as "bias-pull-up", "slew-rate", etc.
154 * @default_value: return default value in case no value is specified in DTS
155 * @return: return pamater ID, or negative error code on failure
157 static int pinconf_prop_name_to_param(struct udevice *dev,
158 const char *property, u32 *default_value)
160 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
161 const struct pinconf_param *p, *end;
163 if (!ops->pinconf_num_params || !ops->pinconf_params) {
164 dev_dbg(dev, "pinconf_num_params or pinconf_params missing\n");
168 p = ops->pinconf_params;
169 end = p + ops->pinconf_num_params;
171 /* See if this pctldev supports this parameter */
172 for (; p < end; p++) {
173 if (!strcmp(property, p->property)) {
174 *default_value = p->default_value;
183 * pinconf_enable_setting() - apply pin configuration for a certain pin/group
185 * @dev: pin controller device
186 * @is_group: target of operation (true: pin group, false: pin)
187 * @selector: pin selector or group selector, depending on @is_group
188 * @param: configuration paramter
189 * @argument: argument taken by some configuration parameters
190 * @return: 0 on success, or negative error code on failure
192 static int pinconf_enable_setting(struct udevice *dev, bool is_group,
193 unsigned selector, unsigned param,
196 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
199 if (!ops->pinconf_group_set) {
200 dev_dbg(dev, "pinconf_group_set op missing\n");
204 return ops->pinconf_group_set(dev, selector, param,
207 if (!ops->pinconf_set) {
208 dev_dbg(dev, "pinconf_set op missing\n");
211 return ops->pinconf_set(dev, selector, param, argument);
215 static int pinconf_prop_name_to_param(struct udevice *dev,
216 const char *property, u32 *default_value)
221 static int pinconf_enable_setting(struct udevice *dev, bool is_group,
222 unsigned selector, unsigned param,
229 enum pinmux_subnode_type {
236 static const char *alloc_name_with_prefix(const char *name, const char *prefix)
239 char *name_with_prefix = malloc(strlen(prefix) + strlen(name) + 1);
240 if (name_with_prefix)
241 sprintf(name_with_prefix, "%s%s", prefix, name);
242 return name_with_prefix;
248 static void free_name_with_prefix(const char *name_with_prefix, const char *prefix)
251 free((char *)name_with_prefix);
255 * pinctrl_generic_set_state_one() - set state for a certain pin/group
256 * Apply all pin multiplexing and pin configurations specified by @config
257 * for a given pin or pin group.
259 * @dev: pin controller device
260 * @config: pseudo device pointing to config node
261 * @subnode_type: target of operation (pin, group, or pin specified by a pinmux
263 * @selector: pin selector or group selector, depending on @subnode_type
264 * @return: 0 on success, or negative error code on failure
266 static int pinctrl_generic_set_state_one(struct udevice *dev,
267 struct udevice *config,
269 enum pinmux_subnode_type subnode_type,
272 const char *function_propname;
273 const char *propname;
275 struct ofprop property;
276 int len, func_selector, param, ret;
277 u32 arg, default_val;
279 assert(subnode_type != PST_NONE);
281 function_propname = alloc_name_with_prefix("function", prefix);
282 if (!function_propname)
285 dev_for_each_property(property, config) {
286 value = dev_read_prop_by_prop(&property, &propname, &len);
288 free_name_with_prefix(function_propname, prefix);
292 /* pinmux subnodes already have their muxing set */
293 if (subnode_type != PST_PINMUX &&
294 !strcmp(propname, function_propname)) {
295 func_selector = pinmux_func_name_to_selector(dev,
297 if (func_selector < 0) {
298 free_name_with_prefix(function_propname, prefix);
299 return func_selector;
301 ret = pinmux_enable_setting(dev,
302 subnode_type == PST_GROUP,
306 param = pinconf_prop_name_to_param(dev, propname,
309 continue; /* just skip unknown properties */
311 if (len >= sizeof(fdt32_t))
312 arg = fdt32_to_cpu(*(fdt32_t *)value);
316 ret = pinconf_enable_setting(dev,
317 subnode_type == PST_GROUP,
318 selector, param, arg);
322 free_name_with_prefix(function_propname, prefix);
327 free_name_with_prefix(function_propname, prefix);
332 * pinctrl_generic_get_subnode_type() - determine whether there is a valid
333 * pins, groups, or pinmux property in the config node
335 * @dev: pin controller device
336 * @config: pseudo device pointing to config node
337 * @count: number of specifiers contained within the property
338 * @return: the type of the subnode, or PST_NONE
340 static enum pinmux_subnode_type pinctrl_generic_get_subnode_type(struct udevice *dev,
341 struct udevice *config,
345 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
346 const char *propname;
348 propname = alloc_name_with_prefix("pins", prefix);
351 *count = dev_read_string_count(config, propname);
352 free_name_with_prefix(propname, prefix);
356 propname = alloc_name_with_prefix("groups", prefix);
359 *count = dev_read_string_count(config, propname);
360 free_name_with_prefix(propname, prefix);
364 if (ops->pinmux_property_set) {
365 propname = alloc_name_with_prefix("pinmux", prefix);
368 *count = dev_read_size(config, propname);
369 free_name_with_prefix(propname, prefix);
370 if (*count >= 0 && !(*count % sizeof(u32))) {
371 *count /= sizeof(u32);
381 * pinctrl_generic_set_state_subnode() - apply all settings in config node
383 * @dev: pin controller device
384 * @config: pseudo device pointing to config node
385 * @prefix: device tree property prefix (e.g. vendor specific)
386 * @return: 0 on success, or negative error code on failure
388 static int pinctrl_generic_set_state_subnode(struct udevice *dev,
389 struct udevice *config,
392 enum pinmux_subnode_type subnode_type;
393 const char *propname;
395 int count, selector, i, ret, scratch;
396 const u32 *pinmux_groups = NULL; /* prevent use-uninitialized warning */
398 subnode_type = pinctrl_generic_get_subnode_type(dev, config, prefix, &count);
400 debug("%s(%s, %s): count=%d\n", __func__, dev->name, config->name,
403 if (subnode_type == PST_PINMUX) {
404 propname = alloc_name_with_prefix("pinmux", prefix);
407 pinmux_groups = dev_read_prop(config, propname, &scratch);
408 free_name_with_prefix(propname, prefix);
413 for (i = 0; i < count; i++) {
414 switch (subnode_type) {
416 propname = alloc_name_with_prefix("pins", prefix);
419 ret = dev_read_string_index(config, propname, i, &name);
420 free_name_with_prefix(propname, prefix);
423 selector = pinctrl_pin_name_to_selector(dev, name);
426 propname = alloc_name_with_prefix("groups", prefix);
429 ret = dev_read_string_index(config, propname, i, &name);
430 free_name_with_prefix(propname, prefix);
433 selector = pinctrl_group_name_to_selector(dev, name);
436 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
437 u32 pinmux_group = fdt32_to_cpu(pinmux_groups[i]);
439 /* Checked for in pinctrl_generic_get_subnode_type */
440 selector = ops->pinmux_property_set(dev, pinmux_group);
445 /* skip this node; may contain config child nodes */
452 ret = pinctrl_generic_set_state_one(dev, config, prefix,
453 subnode_type, selector);
461 int pinctrl_generic_set_state_prefix(struct udevice *dev, struct udevice *config,
464 struct udevice *child;
467 ret = pinctrl_generic_set_state_subnode(dev, config, prefix);
471 for (device_find_first_child(config, &child);
473 device_find_next_child(&child)) {
474 ret = pinctrl_generic_set_state_subnode(dev, child, prefix);
482 int pinctrl_generic_set_state(struct udevice *dev, struct udevice *config)
484 return pinctrl_generic_set_state_prefix(dev, config, NULL);