1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
8 #include <linux/compat.h>
9 #include <dm/pinctrl.h>
11 DECLARE_GLOBAL_DATA_PTR;
14 * pinctrl_pin_name_to_selector() - return the pin selector for a pin
16 * @dev: pin controller device
17 * @pin: the pin name to look up
18 * @return: pin selector, or negative error code on failure
20 static int pinctrl_pin_name_to_selector(struct udevice *dev, const char *pin)
22 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
23 unsigned npins, selector;
25 if (!ops->get_pins_count || !ops->get_pin_name) {
26 dev_dbg(dev, "get_pins_count or get_pin_name missing\n");
30 npins = ops->get_pins_count(dev);
32 /* See if this pctldev has this pin */
33 for (selector = 0; selector < npins; selector++) {
34 const char *pname = ops->get_pin_name(dev, selector);
36 if (!strcmp(pin, pname))
44 * pinctrl_group_name_to_selector() - return the group selector for a group
46 * @dev: pin controller device
47 * @group: the pin group name to look up
48 * @return: pin group selector, or negative error code on failure
50 static int pinctrl_group_name_to_selector(struct udevice *dev,
53 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
54 unsigned ngroups, selector;
56 if (!ops->get_groups_count || !ops->get_group_name) {
57 dev_dbg(dev, "get_groups_count or get_group_name missing\n");
61 ngroups = ops->get_groups_count(dev);
63 /* See if this pctldev has this group */
64 for (selector = 0; selector < ngroups; selector++) {
65 const char *gname = ops->get_group_name(dev, selector);
67 if (!strcmp(group, gname))
74 #if CONFIG_IS_ENABLED(PINMUX)
76 * pinmux_func_name_to_selector() - return the function selector for a function
78 * @dev: pin controller device
79 * @function: the function name to look up
80 * @return: function selector, or negative error code on failure
82 static int pinmux_func_name_to_selector(struct udevice *dev,
85 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
86 unsigned nfuncs, selector = 0;
88 if (!ops->get_functions_count || !ops->get_function_name) {
90 "get_functions_count or get_function_name missing\n");
94 nfuncs = ops->get_functions_count(dev);
96 /* See if this pctldev has this function */
97 for (selector = 0; selector < nfuncs; selector++) {
98 const char *fname = ops->get_function_name(dev, selector);
100 if (!strcmp(function, fname))
108 * pinmux_enable_setting() - enable pin-mux setting for a certain pin/group
110 * @dev: pin controller device
111 * @is_group: target of operation (true: pin group, false: pin)
112 * @selector: pin selector or group selector, depending on @is_group
113 * @func_selector: function selector
114 * @return: 0 on success, or negative error code on failure
116 static int pinmux_enable_setting(struct udevice *dev, bool is_group,
117 unsigned selector, unsigned func_selector)
119 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
122 if (!ops->pinmux_group_set) {
123 dev_dbg(dev, "pinmux_group_set op missing\n");
127 return ops->pinmux_group_set(dev, selector, func_selector);
129 if (!ops->pinmux_set) {
130 dev_dbg(dev, "pinmux_set op missing\n");
133 return ops->pinmux_set(dev, selector, func_selector);
137 static int pinmux_func_name_to_selector(struct udevice *dev,
138 const char *function)
143 static int pinmux_enable_setting(struct udevice *dev, bool is_group,
144 unsigned selector, unsigned func_selector)
150 #if CONFIG_IS_ENABLED(PINCONF)
152 * pinconf_prop_name_to_param() - return parameter ID for a property name
154 * @dev: pin controller device
155 * @property: property name in DTS, such as "bias-pull-up", "slew-rate", etc.
156 * @default_value: return default value in case no value is specified in DTS
157 * @return: return pamater ID, or negative error code on failure
159 static int pinconf_prop_name_to_param(struct udevice *dev,
160 const char *property, u32 *default_value)
162 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
163 const struct pinconf_param *p, *end;
165 if (!ops->pinconf_num_params || !ops->pinconf_params) {
166 dev_dbg(dev, "pinconf_num_params or pinconf_params missing\n");
170 p = ops->pinconf_params;
171 end = p + ops->pinconf_num_params;
173 /* See if this pctldev supports this parameter */
174 for (; p < end; p++) {
175 if (!strcmp(property, p->property)) {
176 *default_value = p->default_value;
185 * pinconf_enable_setting() - apply pin configuration for a certain pin/group
187 * @dev: pin controller device
188 * @is_group: target of operation (true: pin group, false: pin)
189 * @selector: pin selector or group selector, depending on @is_group
190 * @param: configuration paramter
191 * @argument: argument taken by some configuration parameters
192 * @return: 0 on success, or negative error code on failure
194 static int pinconf_enable_setting(struct udevice *dev, bool is_group,
195 unsigned selector, unsigned param,
198 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
201 if (!ops->pinconf_group_set) {
202 dev_dbg(dev, "pinconf_group_set op missing\n");
206 return ops->pinconf_group_set(dev, selector, param,
209 if (!ops->pinconf_set) {
210 dev_dbg(dev, "pinconf_set op missing\n");
213 return ops->pinconf_set(dev, selector, param, argument);
217 static int pinconf_prop_name_to_param(struct udevice *dev,
218 const char *property, u32 *default_value)
223 static int pinconf_enable_setting(struct udevice *dev, bool is_group,
224 unsigned selector, unsigned param,
232 * pinctrl_generic_set_state_one() - set state for a certain pin/group
233 * Apply all pin multiplexing and pin configurations specified by @config
234 * for a given pin or pin group.
236 * @dev: pin controller device
237 * @config: pseudo device pointing to config node
238 * @is_group: target of operation (true: pin group, false: pin)
239 * @selector: pin selector or group selector, depending on @is_group
240 * @return: 0 on success, or negative error code on failure
242 static int pinctrl_generic_set_state_one(struct udevice *dev,
243 struct udevice *config,
244 bool is_group, unsigned selector)
246 const void *fdt = gd->fdt_blob;
247 int node_offset = dev_of_offset(config);
248 const char *propname;
250 int prop_offset, len, func_selector, param, ret;
251 u32 arg, default_val;
253 for (prop_offset = fdt_first_property_offset(fdt, node_offset);
255 prop_offset = fdt_next_property_offset(fdt, prop_offset)) {
256 value = fdt_getprop_by_offset(fdt, prop_offset,
261 if (!strcmp(propname, "function")) {
262 func_selector = pinmux_func_name_to_selector(dev,
264 if (func_selector < 0)
265 return func_selector;
266 ret = pinmux_enable_setting(dev, is_group,
270 param = pinconf_prop_name_to_param(dev, propname,
273 continue; /* just skip unknown properties */
275 if (len >= sizeof(fdt32_t))
276 arg = fdt32_to_cpu(*(fdt32_t *)value);
280 ret = pinconf_enable_setting(dev, is_group,
281 selector, param, arg);
292 * pinctrl_generic_set_state_subnode() - apply all settings in config node
294 * @dev: pin controller device
295 * @config: pseudo device pointing to config node
296 * @return: 0 on success, or negative error code on failure
298 static int pinctrl_generic_set_state_subnode(struct udevice *dev,
299 struct udevice *config)
301 const void *fdt = gd->fdt_blob;
302 int node = dev_of_offset(config);
303 const char *subnode_target_type = "pins";
304 bool is_group = false;
306 int strings_count, selector, i, ret;
308 strings_count = fdt_stringlist_count(fdt, node, subnode_target_type);
309 if (strings_count < 0) {
310 subnode_target_type = "groups";
312 strings_count = fdt_stringlist_count(fdt, node,
313 subnode_target_type);
314 if (strings_count < 0) {
315 /* skip this node; may contain config child nodes */
320 for (i = 0; i < strings_count; i++) {
321 name = fdt_stringlist_get(fdt, node, subnode_target_type, i,
327 selector = pinctrl_group_name_to_selector(dev, name);
329 selector = pinctrl_pin_name_to_selector(dev, name);
333 ret = pinctrl_generic_set_state_one(dev, config,
342 int pinctrl_generic_set_state(struct udevice *dev, struct udevice *config)
344 struct udevice *child;
347 ret = pinctrl_generic_set_state_subnode(dev, config);
351 for (device_find_first_child(config, &child);
353 device_find_next_child(&child)) {
354 ret = pinctrl_generic_set_state_subnode(dev, child);