1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
8 #include <dm/device_compat.h>
9 #include <linux/compat.h>
10 #include <dm/pinctrl.h>
13 * pinctrl_pin_name_to_selector() - return the pin selector for a pin
15 * @dev: pin controller device
16 * @pin: the pin name to look up
17 * @return: pin selector, or negative error code on failure
19 static int pinctrl_pin_name_to_selector(struct udevice *dev, const char *pin)
21 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
22 unsigned npins, selector;
24 if (!ops->get_pins_count || !ops->get_pin_name) {
25 dev_dbg(dev, "get_pins_count or get_pin_name missing\n");
29 npins = ops->get_pins_count(dev);
31 /* See if this pctldev has this pin */
32 for (selector = 0; selector < npins; selector++) {
33 const char *pname = ops->get_pin_name(dev, selector);
35 if (!strcmp(pin, pname))
43 * pinctrl_group_name_to_selector() - return the group selector for a group
45 * @dev: pin controller device
46 * @group: the pin group name to look up
47 * @return: pin group selector, or negative error code on failure
49 static int pinctrl_group_name_to_selector(struct udevice *dev,
52 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
53 unsigned ngroups, selector;
55 if (!ops->get_groups_count || !ops->get_group_name) {
56 dev_dbg(dev, "get_groups_count or get_group_name missing\n");
60 ngroups = ops->get_groups_count(dev);
62 /* See if this pctldev has this group */
63 for (selector = 0; selector < ngroups; selector++) {
64 const char *gname = ops->get_group_name(dev, selector);
66 if (!strcmp(group, gname))
73 #if CONFIG_IS_ENABLED(PINMUX)
75 * pinmux_func_name_to_selector() - return the function selector for a function
77 * @dev: pin controller device
78 * @function: the function name to look up
79 * @return: function selector, or negative error code on failure
81 static int pinmux_func_name_to_selector(struct udevice *dev,
84 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
85 unsigned nfuncs, selector = 0;
87 if (!ops->get_functions_count || !ops->get_function_name) {
89 "get_functions_count or get_function_name missing\n");
93 nfuncs = ops->get_functions_count(dev);
95 /* See if this pctldev has this function */
96 for (selector = 0; selector < nfuncs; selector++) {
97 const char *fname = ops->get_function_name(dev, selector);
99 if (!strcmp(function, fname))
107 * pinmux_enable_setting() - enable pin-mux setting for a certain pin/group
109 * @dev: pin controller device
110 * @is_group: target of operation (true: pin group, false: pin)
111 * @selector: pin selector or group selector, depending on @is_group
112 * @func_selector: function selector
113 * @return: 0 on success, or negative error code on failure
115 static int pinmux_enable_setting(struct udevice *dev, bool is_group,
116 unsigned selector, unsigned func_selector)
118 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
121 if (!ops->pinmux_group_set) {
122 dev_dbg(dev, "pinmux_group_set op missing\n");
126 return ops->pinmux_group_set(dev, selector, func_selector);
128 if (!ops->pinmux_set) {
129 dev_dbg(dev, "pinmux_set op missing\n");
132 return ops->pinmux_set(dev, selector, func_selector);
136 static int pinmux_func_name_to_selector(struct udevice *dev,
137 const char *function)
142 static int pinmux_enable_setting(struct udevice *dev, bool is_group,
143 unsigned selector, unsigned func_selector)
149 #if CONFIG_IS_ENABLED(PINCONF)
151 * pinconf_prop_name_to_param() - return parameter ID for a property name
153 * @dev: pin controller device
154 * @property: property name in DTS, such as "bias-pull-up", "slew-rate", etc.
155 * @default_value: return default value in case no value is specified in DTS
156 * @return: return pamater ID, or negative error code on failure
158 static int pinconf_prop_name_to_param(struct udevice *dev,
159 const char *property, u32 *default_value)
161 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
162 const struct pinconf_param *p, *end;
164 if (!ops->pinconf_num_params || !ops->pinconf_params) {
165 dev_dbg(dev, "pinconf_num_params or pinconf_params missing\n");
169 p = ops->pinconf_params;
170 end = p + ops->pinconf_num_params;
172 /* See if this pctldev supports this parameter */
173 for (; p < end; p++) {
174 if (!strcmp(property, p->property)) {
175 *default_value = p->default_value;
184 * pinconf_enable_setting() - apply pin configuration for a certain pin/group
186 * @dev: pin controller device
187 * @is_group: target of operation (true: pin group, false: pin)
188 * @selector: pin selector or group selector, depending on @is_group
189 * @param: configuration paramter
190 * @argument: argument taken by some configuration parameters
191 * @return: 0 on success, or negative error code on failure
193 static int pinconf_enable_setting(struct udevice *dev, bool is_group,
194 unsigned selector, unsigned param,
197 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
200 if (!ops->pinconf_group_set) {
201 dev_dbg(dev, "pinconf_group_set op missing\n");
205 return ops->pinconf_group_set(dev, selector, param,
208 if (!ops->pinconf_set) {
209 dev_dbg(dev, "pinconf_set op missing\n");
212 return ops->pinconf_set(dev, selector, param, argument);
216 static int pinconf_prop_name_to_param(struct udevice *dev,
217 const char *property, u32 *default_value)
222 static int pinconf_enable_setting(struct udevice *dev, bool is_group,
223 unsigned selector, unsigned param,
230 enum pinmux_subnode_type {
237 static const char *alloc_name_with_prefix(const char *name, const char *prefix)
240 char *name_with_prefix = malloc(strlen(prefix) + strlen(name) + 1);
241 if (name_with_prefix)
242 sprintf(name_with_prefix, "%s%s", prefix, name);
243 return name_with_prefix;
249 static void free_name_with_prefix(const char *name_with_prefix, const char *prefix)
252 free((char *)name_with_prefix);
256 * pinctrl_generic_set_state_one() - set state for a certain pin/group
257 * Apply all pin multiplexing and pin configurations specified by @config
258 * for a given pin or pin group.
260 * @dev: pin controller device
261 * @config: pseudo device pointing to config node
262 * @subnode_type: target of operation (pin, group, or pin specified by a pinmux
264 * @selector: pin selector or group selector, depending on @subnode_type
265 * @return: 0 on success, or negative error code on failure
267 static int pinctrl_generic_set_state_one(struct udevice *dev,
268 struct udevice *config,
270 enum pinmux_subnode_type subnode_type,
273 const char *function_propname;
274 const char *propname;
276 struct ofprop property;
277 int len, func_selector, param, ret;
278 u32 arg, default_val;
280 assert(subnode_type != PST_NONE);
282 function_propname = alloc_name_with_prefix("function", prefix);
283 if (!function_propname)
286 dev_for_each_property(property, config) {
287 value = dev_read_prop_by_prop(&property, &propname, &len);
289 free_name_with_prefix(function_propname, prefix);
293 /* pinmux subnodes already have their muxing set */
294 if (subnode_type != PST_PINMUX &&
295 !strcmp(propname, function_propname)) {
296 func_selector = pinmux_func_name_to_selector(dev,
298 if (func_selector < 0) {
299 free_name_with_prefix(function_propname, prefix);
300 return func_selector;
302 ret = pinmux_enable_setting(dev,
303 subnode_type == PST_GROUP,
307 param = pinconf_prop_name_to_param(dev, propname,
310 continue; /* just skip unknown properties */
312 if (len >= sizeof(fdt32_t))
313 arg = fdt32_to_cpu(*(fdt32_t *)value);
317 ret = pinconf_enable_setting(dev,
318 subnode_type == PST_GROUP,
319 selector, param, arg);
323 free_name_with_prefix(function_propname, prefix);
328 free_name_with_prefix(function_propname, prefix);
333 * pinctrl_generic_get_subnode_type() - determine whether there is a valid
334 * pins, groups, or pinmux property in the config node
336 * @dev: pin controller device
337 * @config: pseudo device pointing to config node
338 * @count: number of specifiers contained within the property
339 * @return: the type of the subnode, or PST_NONE
341 static enum pinmux_subnode_type pinctrl_generic_get_subnode_type(struct udevice *dev,
342 struct udevice *config,
346 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
347 const char *propname;
349 propname = alloc_name_with_prefix("pins", prefix);
352 *count = dev_read_string_count(config, propname);
353 free_name_with_prefix(propname, prefix);
357 propname = alloc_name_with_prefix("groups", prefix);
360 *count = dev_read_string_count(config, propname);
361 free_name_with_prefix(propname, prefix);
365 if (ops->pinmux_property_set) {
366 propname = alloc_name_with_prefix("pinmux", prefix);
369 *count = dev_read_size(config, propname);
370 free_name_with_prefix(propname, prefix);
371 if (*count >= 0 && !(*count % sizeof(u32))) {
372 *count /= sizeof(u32);
382 * pinctrl_generic_set_state_subnode() - apply all settings in config node
384 * @dev: pin controller device
385 * @config: pseudo device pointing to config node
386 * @prefix: device tree property prefix (e.g. vendor specific)
387 * @return: 0 on success, or negative error code on failure
389 static int pinctrl_generic_set_state_subnode(struct udevice *dev,
390 struct udevice *config,
393 enum pinmux_subnode_type subnode_type;
394 const char *propname;
396 int count, selector, i, ret, scratch;
397 const u32 *pinmux_groups = NULL; /* prevent use-uninitialized warning */
399 subnode_type = pinctrl_generic_get_subnode_type(dev, config, prefix, &count);
401 debug("%s(%s, %s): count=%d\n", __func__, dev->name, config->name,
404 if (subnode_type == PST_PINMUX) {
405 propname = alloc_name_with_prefix("pinmux", prefix);
408 pinmux_groups = dev_read_prop(config, propname, &scratch);
409 free_name_with_prefix(propname, prefix);
414 for (i = 0; i < count; i++) {
415 switch (subnode_type) {
417 propname = alloc_name_with_prefix("pins", prefix);
420 ret = dev_read_string_index(config, propname, i, &name);
421 free_name_with_prefix(propname, prefix);
424 selector = pinctrl_pin_name_to_selector(dev, name);
427 propname = alloc_name_with_prefix("groups", prefix);
430 ret = dev_read_string_index(config, propname, i, &name);
431 free_name_with_prefix(propname, prefix);
434 selector = pinctrl_group_name_to_selector(dev, name);
437 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
438 u32 pinmux_group = fdt32_to_cpu(pinmux_groups[i]);
440 /* Checked for in pinctrl_generic_get_subnode_type */
441 selector = ops->pinmux_property_set(dev, pinmux_group);
446 /* skip this node; may contain config child nodes */
453 ret = pinctrl_generic_set_state_one(dev, config, prefix,
454 subnode_type, selector);
462 int pinctrl_generic_set_state_prefix(struct udevice *dev, struct udevice *config,
465 struct udevice *child;
468 ret = pinctrl_generic_set_state_subnode(dev, config, prefix);
472 for (device_find_first_child(config, &child);
474 device_find_next_child(&child)) {
475 ret = pinctrl_generic_set_state_subnode(dev, child, prefix);
483 int pinctrl_generic_set_state(struct udevice *dev, struct udevice *config)
485 return pinctrl_generic_set_state_prefix(dev, config, NULL);