Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com> |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 4 | */ |
| 5 | |
Simon Glass | 11c89f3 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 6 | #include <dm.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 7 | #include <dm/device_compat.h> |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 8 | #include <linux/compat.h> |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 9 | #include <dm/pinctrl.h> |
| 10 | |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 11 | /** |
| 12 | * pinctrl_pin_name_to_selector() - return the pin selector for a pin |
| 13 | * |
| 14 | * @dev: pin controller device |
| 15 | * @pin: the pin name to look up |
| 16 | * @return: pin selector, or negative error code on failure |
| 17 | */ |
| 18 | static int pinctrl_pin_name_to_selector(struct udevice *dev, const char *pin) |
| 19 | { |
| 20 | const struct pinctrl_ops *ops = pinctrl_get_ops(dev); |
| 21 | unsigned npins, selector; |
| 22 | |
| 23 | if (!ops->get_pins_count || !ops->get_pin_name) { |
| 24 | dev_dbg(dev, "get_pins_count or get_pin_name missing\n"); |
Sean Anderson | 9a82aca | 2024-08-10 09:59:37 -0400 | [diff] [blame] | 25 | return -ENOENT; |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | npins = ops->get_pins_count(dev); |
| 29 | |
| 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); |
| 33 | |
| 34 | if (!strcmp(pin, pname)) |
| 35 | return selector; |
| 36 | } |
| 37 | |
Sean Anderson | 9a82aca | 2024-08-10 09:59:37 -0400 | [diff] [blame] | 38 | return -ENOENT; |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | /** |
| 42 | * pinctrl_group_name_to_selector() - return the group selector for a group |
| 43 | * |
| 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 |
| 47 | */ |
| 48 | static int pinctrl_group_name_to_selector(struct udevice *dev, |
| 49 | const char *group) |
| 50 | { |
| 51 | const struct pinctrl_ops *ops = pinctrl_get_ops(dev); |
| 52 | unsigned ngroups, selector; |
| 53 | |
| 54 | if (!ops->get_groups_count || !ops->get_group_name) { |
| 55 | dev_dbg(dev, "get_groups_count or get_group_name missing\n"); |
Sean Anderson | 9a82aca | 2024-08-10 09:59:37 -0400 | [diff] [blame] | 56 | return -ENOENT; |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | ngroups = ops->get_groups_count(dev); |
| 60 | |
| 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); |
| 64 | |
| 65 | if (!strcmp(group, gname)) |
| 66 | return selector; |
| 67 | } |
| 68 | |
Sean Anderson | 9a82aca | 2024-08-10 09:59:37 -0400 | [diff] [blame] | 69 | return -ENOENT; |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | #if CONFIG_IS_ENABLED(PINMUX) |
| 73 | /** |
| 74 | * pinmux_func_name_to_selector() - return the function selector for a function |
| 75 | * |
| 76 | * @dev: pin controller device |
| 77 | * @function: the function name to look up |
| 78 | * @return: function selector, or negative error code on failure |
| 79 | */ |
| 80 | static int pinmux_func_name_to_selector(struct udevice *dev, |
| 81 | const char *function) |
| 82 | { |
| 83 | const struct pinctrl_ops *ops = pinctrl_get_ops(dev); |
| 84 | unsigned nfuncs, selector = 0; |
| 85 | |
| 86 | if (!ops->get_functions_count || !ops->get_function_name) { |
| 87 | dev_dbg(dev, |
| 88 | "get_functions_count or get_function_name missing\n"); |
Sean Anderson | 9a82aca | 2024-08-10 09:59:37 -0400 | [diff] [blame] | 89 | return -ENOENT; |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | nfuncs = ops->get_functions_count(dev); |
| 93 | |
| 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); |
| 97 | |
| 98 | if (!strcmp(function, fname)) |
| 99 | return selector; |
| 100 | } |
| 101 | |
Sean Anderson | 9a82aca | 2024-08-10 09:59:37 -0400 | [diff] [blame] | 102 | return -ENOENT; |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * pinmux_enable_setting() - enable pin-mux setting for a certain pin/group |
| 107 | * |
| 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 |
| 113 | */ |
| 114 | static int pinmux_enable_setting(struct udevice *dev, bool is_group, |
| 115 | unsigned selector, unsigned func_selector) |
| 116 | { |
| 117 | const struct pinctrl_ops *ops = pinctrl_get_ops(dev); |
| 118 | |
| 119 | if (is_group) { |
| 120 | if (!ops->pinmux_group_set) { |
| 121 | dev_dbg(dev, "pinmux_group_set op missing\n"); |
Sean Anderson | 9a82aca | 2024-08-10 09:59:37 -0400 | [diff] [blame] | 122 | return -ENOENT; |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | return ops->pinmux_group_set(dev, selector, func_selector); |
| 126 | } else { |
| 127 | if (!ops->pinmux_set) { |
| 128 | dev_dbg(dev, "pinmux_set op missing\n"); |
Sean Anderson | 9a82aca | 2024-08-10 09:59:37 -0400 | [diff] [blame] | 129 | return -ENOENT; |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 130 | } |
| 131 | return ops->pinmux_set(dev, selector, func_selector); |
| 132 | } |
| 133 | } |
| 134 | #else |
| 135 | static int pinmux_func_name_to_selector(struct udevice *dev, |
| 136 | const char *function) |
| 137 | { |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static int pinmux_enable_setting(struct udevice *dev, bool is_group, |
| 142 | unsigned selector, unsigned func_selector) |
| 143 | { |
| 144 | return 0; |
| 145 | } |
| 146 | #endif |
| 147 | |
| 148 | #if CONFIG_IS_ENABLED(PINCONF) |
| 149 | /** |
| 150 | * pinconf_prop_name_to_param() - return parameter ID for a property name |
| 151 | * |
| 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 |
| 156 | */ |
| 157 | static int pinconf_prop_name_to_param(struct udevice *dev, |
| 158 | const char *property, u32 *default_value) |
| 159 | { |
| 160 | const struct pinctrl_ops *ops = pinctrl_get_ops(dev); |
| 161 | const struct pinconf_param *p, *end; |
| 162 | |
| 163 | if (!ops->pinconf_num_params || !ops->pinconf_params) { |
| 164 | dev_dbg(dev, "pinconf_num_params or pinconf_params missing\n"); |
Sean Anderson | 9a82aca | 2024-08-10 09:59:37 -0400 | [diff] [blame] | 165 | return -ENOENT; |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | p = ops->pinconf_params; |
| 169 | end = p + ops->pinconf_num_params; |
| 170 | |
| 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; |
| 175 | return p->param; |
| 176 | } |
| 177 | } |
| 178 | |
Sean Anderson | 9a82aca | 2024-08-10 09:59:37 -0400 | [diff] [blame] | 179 | return -ENOENT; |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /** |
| 183 | * pinconf_enable_setting() - apply pin configuration for a certain pin/group |
| 184 | * |
| 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 |
| 191 | */ |
| 192 | static int pinconf_enable_setting(struct udevice *dev, bool is_group, |
| 193 | unsigned selector, unsigned param, |
| 194 | u32 argument) |
| 195 | { |
| 196 | const struct pinctrl_ops *ops = pinctrl_get_ops(dev); |
| 197 | |
| 198 | if (is_group) { |
| 199 | if (!ops->pinconf_group_set) { |
| 200 | dev_dbg(dev, "pinconf_group_set op missing\n"); |
Sean Anderson | 9a82aca | 2024-08-10 09:59:37 -0400 | [diff] [blame] | 201 | return -ENOENT; |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | return ops->pinconf_group_set(dev, selector, param, |
| 205 | argument); |
| 206 | } else { |
| 207 | if (!ops->pinconf_set) { |
| 208 | dev_dbg(dev, "pinconf_set op missing\n"); |
Sean Anderson | 9a82aca | 2024-08-10 09:59:37 -0400 | [diff] [blame] | 209 | return -ENOENT; |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 210 | } |
| 211 | return ops->pinconf_set(dev, selector, param, argument); |
| 212 | } |
| 213 | } |
| 214 | #else |
| 215 | static int pinconf_prop_name_to_param(struct udevice *dev, |
| 216 | const char *property, u32 *default_value) |
| 217 | { |
Sean Anderson | 9a82aca | 2024-08-10 09:59:37 -0400 | [diff] [blame] | 218 | return -ENOENT; |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | static int pinconf_enable_setting(struct udevice *dev, bool is_group, |
| 222 | unsigned selector, unsigned param, |
| 223 | u32 argument) |
| 224 | { |
| 225 | return 0; |
| 226 | } |
| 227 | #endif |
| 228 | |
Sean Anderson | 319ca70 | 2020-09-14 11:01:55 -0400 | [diff] [blame] | 229 | enum pinmux_subnode_type { |
| 230 | PST_NONE = 0, |
| 231 | PST_PIN, |
| 232 | PST_GROUP, |
| 233 | PST_PINMUX, |
| 234 | }; |
| 235 | |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 236 | static const char *alloc_name_with_prefix(const char *name, const char *prefix) |
| 237 | { |
| 238 | if (prefix) { |
John Keeping | 8573ec4 | 2022-09-07 12:11:42 +0100 | [diff] [blame] | 239 | char *name_with_prefix = malloc(strlen(prefix) + strlen(name) + 1); |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 240 | if (name_with_prefix) |
| 241 | sprintf(name_with_prefix, "%s%s", prefix, name); |
| 242 | return name_with_prefix; |
| 243 | } else { |
| 244 | return name; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | static void free_name_with_prefix(const char *name_with_prefix, const char *prefix) |
| 249 | { |
| 250 | if (prefix) |
| 251 | free((char *)name_with_prefix); |
| 252 | } |
| 253 | |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 254 | /** |
| 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. |
| 258 | * |
| 259 | * @dev: pin controller device |
| 260 | * @config: pseudo device pointing to config node |
Sean Anderson | 319ca70 | 2020-09-14 11:01:55 -0400 | [diff] [blame] | 261 | * @subnode_type: target of operation (pin, group, or pin specified by a pinmux |
| 262 | * group) |
| 263 | * @selector: pin selector or group selector, depending on @subnode_type |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 264 | * @return: 0 on success, or negative error code on failure |
| 265 | */ |
| 266 | static int pinctrl_generic_set_state_one(struct udevice *dev, |
| 267 | struct udevice *config, |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 268 | const char *prefix, |
Sean Anderson | 319ca70 | 2020-09-14 11:01:55 -0400 | [diff] [blame] | 269 | enum pinmux_subnode_type subnode_type, |
| 270 | unsigned selector) |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 271 | { |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 272 | const char *function_propname; |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 273 | const char *propname; |
| 274 | const void *value; |
Patrick Delaunay | 73237dc | 2020-01-13 11:34:57 +0100 | [diff] [blame] | 275 | struct ofprop property; |
| 276 | int len, func_selector, param, ret; |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 277 | u32 arg, default_val; |
| 278 | |
Sean Anderson | 319ca70 | 2020-09-14 11:01:55 -0400 | [diff] [blame] | 279 | assert(subnode_type != PST_NONE); |
| 280 | |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 281 | function_propname = alloc_name_with_prefix("function", prefix); |
| 282 | if (!function_propname) |
| 283 | return -ENOMEM; |
| 284 | |
Patrick Delaunay | 73237dc | 2020-01-13 11:34:57 +0100 | [diff] [blame] | 285 | dev_for_each_property(property, config) { |
| 286 | value = dev_read_prop_by_prop(&property, &propname, &len); |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 287 | if (!value) { |
| 288 | free_name_with_prefix(function_propname, prefix); |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 289 | return -EINVAL; |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 290 | } |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 291 | |
Sean Anderson | 319ca70 | 2020-09-14 11:01:55 -0400 | [diff] [blame] | 292 | /* pinmux subnodes already have their muxing set */ |
| 293 | if (subnode_type != PST_PINMUX && |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 294 | !strcmp(propname, function_propname)) { |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 295 | func_selector = pinmux_func_name_to_selector(dev, |
| 296 | value); |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 297 | if (func_selector < 0) { |
| 298 | free_name_with_prefix(function_propname, prefix); |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 299 | return func_selector; |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 300 | } |
Sean Anderson | 319ca70 | 2020-09-14 11:01:55 -0400 | [diff] [blame] | 301 | ret = pinmux_enable_setting(dev, |
| 302 | subnode_type == PST_GROUP, |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 303 | selector, |
| 304 | func_selector); |
| 305 | } else { |
| 306 | param = pinconf_prop_name_to_param(dev, propname, |
| 307 | &default_val); |
| 308 | if (param < 0) |
| 309 | continue; /* just skip unknown properties */ |
| 310 | |
| 311 | if (len >= sizeof(fdt32_t)) |
| 312 | arg = fdt32_to_cpu(*(fdt32_t *)value); |
| 313 | else |
| 314 | arg = default_val; |
| 315 | |
Sean Anderson | 319ca70 | 2020-09-14 11:01:55 -0400 | [diff] [blame] | 316 | ret = pinconf_enable_setting(dev, |
| 317 | subnode_type == PST_GROUP, |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 318 | selector, param, arg); |
| 319 | } |
| 320 | |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 321 | if (ret) { |
| 322 | free_name_with_prefix(function_propname, prefix); |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 323 | return ret; |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 324 | } |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 325 | } |
| 326 | |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 327 | free_name_with_prefix(function_propname, prefix); |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | /** |
Sean Anderson | 319ca70 | 2020-09-14 11:01:55 -0400 | [diff] [blame] | 332 | * pinctrl_generic_get_subnode_type() - determine whether there is a valid |
| 333 | * pins, groups, or pinmux property in the config node |
| 334 | * |
| 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 |
| 339 | */ |
| 340 | static enum pinmux_subnode_type pinctrl_generic_get_subnode_type(struct udevice *dev, |
| 341 | struct udevice *config, |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 342 | const char *prefix, |
Sean Anderson | 319ca70 | 2020-09-14 11:01:55 -0400 | [diff] [blame] | 343 | int *count) |
| 344 | { |
| 345 | const struct pinctrl_ops *ops = pinctrl_get_ops(dev); |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 346 | const char *propname; |
Sean Anderson | 319ca70 | 2020-09-14 11:01:55 -0400 | [diff] [blame] | 347 | |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 348 | propname = alloc_name_with_prefix("pins", prefix); |
| 349 | if (!propname) |
| 350 | return -ENOMEM; |
| 351 | *count = dev_read_string_count(config, propname); |
| 352 | free_name_with_prefix(propname, prefix); |
Sean Anderson | 319ca70 | 2020-09-14 11:01:55 -0400 | [diff] [blame] | 353 | if (*count >= 0) |
| 354 | return PST_PIN; |
| 355 | |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 356 | propname = alloc_name_with_prefix("groups", prefix); |
| 357 | if (!propname) |
| 358 | return -ENOMEM; |
| 359 | *count = dev_read_string_count(config, propname); |
| 360 | free_name_with_prefix(propname, prefix); |
Sean Anderson | 319ca70 | 2020-09-14 11:01:55 -0400 | [diff] [blame] | 361 | if (*count >= 0) |
| 362 | return PST_GROUP; |
| 363 | |
| 364 | if (ops->pinmux_property_set) { |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 365 | propname = alloc_name_with_prefix("pinmux", prefix); |
| 366 | if (!propname) |
| 367 | return -ENOMEM; |
| 368 | *count = dev_read_size(config, propname); |
| 369 | free_name_with_prefix(propname, prefix); |
Sean Anderson | 319ca70 | 2020-09-14 11:01:55 -0400 | [diff] [blame] | 370 | if (*count >= 0 && !(*count % sizeof(u32))) { |
| 371 | *count /= sizeof(u32); |
| 372 | return PST_PINMUX; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | *count = 0; |
| 377 | return PST_NONE; |
| 378 | } |
| 379 | |
| 380 | /** |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 381 | * pinctrl_generic_set_state_subnode() - apply all settings in config node |
| 382 | * |
| 383 | * @dev: pin controller device |
| 384 | * @config: pseudo device pointing to config node |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 385 | * @prefix: device tree property prefix (e.g. vendor specific) |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 386 | * @return: 0 on success, or negative error code on failure |
| 387 | */ |
| 388 | static int pinctrl_generic_set_state_subnode(struct udevice *dev, |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 389 | struct udevice *config, |
| 390 | const char *prefix) |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 391 | { |
Sean Anderson | 319ca70 | 2020-09-14 11:01:55 -0400 | [diff] [blame] | 392 | enum pinmux_subnode_type subnode_type; |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 393 | const char *propname; |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 394 | const char *name; |
Sean Anderson | 319ca70 | 2020-09-14 11:01:55 -0400 | [diff] [blame] | 395 | int count, selector, i, ret, scratch; |
| 396 | const u32 *pinmux_groups = NULL; /* prevent use-uninitialized warning */ |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 397 | |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 398 | subnode_type = pinctrl_generic_get_subnode_type(dev, config, prefix, &count); |
Sean Anderson | 319ca70 | 2020-09-14 11:01:55 -0400 | [diff] [blame] | 399 | |
| 400 | debug("%s(%s, %s): count=%d\n", __func__, dev->name, config->name, |
| 401 | count); |
| 402 | |
| 403 | if (subnode_type == PST_PINMUX) { |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 404 | propname = alloc_name_with_prefix("pinmux", prefix); |
| 405 | if (!propname) |
| 406 | return -ENOMEM; |
| 407 | pinmux_groups = dev_read_prop(config, propname, &scratch); |
| 408 | free_name_with_prefix(propname, prefix); |
Sean Anderson | 319ca70 | 2020-09-14 11:01:55 -0400 | [diff] [blame] | 409 | if (!pinmux_groups) |
| 410 | return -EINVAL; |
| 411 | } |
| 412 | |
| 413 | for (i = 0; i < count; i++) { |
| 414 | switch (subnode_type) { |
| 415 | case PST_PIN: |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 416 | propname = alloc_name_with_prefix("pins", prefix); |
| 417 | if (!propname) |
| 418 | return -ENOMEM; |
| 419 | ret = dev_read_string_index(config, propname, i, &name); |
| 420 | free_name_with_prefix(propname, prefix); |
Sean Anderson | 319ca70 | 2020-09-14 11:01:55 -0400 | [diff] [blame] | 421 | if (ret) |
| 422 | return ret; |
| 423 | selector = pinctrl_pin_name_to_selector(dev, name); |
| 424 | break; |
| 425 | case PST_GROUP: |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 426 | propname = alloc_name_with_prefix("groups", prefix); |
| 427 | if (!propname) |
| 428 | return -ENOMEM; |
| 429 | ret = dev_read_string_index(config, propname, i, &name); |
| 430 | free_name_with_prefix(propname, prefix); |
Sean Anderson | 319ca70 | 2020-09-14 11:01:55 -0400 | [diff] [blame] | 431 | if (ret) |
| 432 | return ret; |
| 433 | selector = pinctrl_group_name_to_selector(dev, name); |
| 434 | break; |
| 435 | case PST_PINMUX: { |
| 436 | const struct pinctrl_ops *ops = pinctrl_get_ops(dev); |
| 437 | u32 pinmux_group = fdt32_to_cpu(pinmux_groups[i]); |
| 438 | |
| 439 | /* Checked for in pinctrl_generic_get_subnode_type */ |
| 440 | selector = ops->pinmux_property_set(dev, pinmux_group); |
| 441 | break; |
| 442 | } |
| 443 | case PST_NONE: |
| 444 | default: |
Beniamino Galvani | cc52ad3 | 2016-08-16 11:49:47 +0200 | [diff] [blame] | 445 | /* skip this node; may contain config child nodes */ |
| 446 | return 0; |
| 447 | } |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 448 | |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 449 | if (selector < 0) |
| 450 | return selector; |
| 451 | |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 452 | ret = pinctrl_generic_set_state_one(dev, config, prefix, |
| 453 | subnode_type, selector); |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 454 | if (ret) |
| 455 | return ret; |
| 456 | } |
| 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 461 | int pinctrl_generic_set_state_prefix(struct udevice *dev, struct udevice *config, |
| 462 | const char *prefix) |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 463 | { |
| 464 | struct udevice *child; |
| 465 | int ret; |
| 466 | |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 467 | ret = pinctrl_generic_set_state_subnode(dev, config, prefix); |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 468 | if (ret) |
| 469 | return ret; |
| 470 | |
| 471 | for (device_find_first_child(config, &child); |
| 472 | child; |
| 473 | device_find_next_child(&child)) { |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 474 | ret = pinctrl_generic_set_state_subnode(dev, child, prefix); |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 475 | if (ret) |
| 476 | return ret; |
| 477 | } |
| 478 | |
| 479 | return 0; |
| 480 | } |
Pali Rohár | d80ed17 | 2022-07-25 13:56:08 +0200 | [diff] [blame] | 481 | |
| 482 | int pinctrl_generic_set_state(struct udevice *dev, struct udevice *config) |
| 483 | { |
| 484 | return pinctrl_generic_set_state_prefix(dev, config, NULL); |
| 485 | } |