blob: 81a9327eb3537bce1f89b04625199e9c04572074 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamadaf8efa632015-08-27 12:44:29 +09002/*
3 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamadaf8efa632015-08-27 12:44:29 +09004 */
5
Simon Glass11c89f32017-05-17 17:18:03 -06006#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -07007#include <dm/device_compat.h>
Masahiro Yamadaf8efa632015-08-27 12:44:29 +09008#include <linux/compat.h>
Masahiro Yamadaf8efa632015-08-27 12:44:29 +09009#include <dm/pinctrl.h>
10
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090011/**
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 */
18static 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 Anderson9a82aca2024-08-10 09:59:37 -040025 return -ENOENT;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090026 }
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 Anderson9a82aca2024-08-10 09:59:37 -040038 return -ENOENT;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090039}
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 */
48static 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 Anderson9a82aca2024-08-10 09:59:37 -040056 return -ENOENT;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090057 }
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 Anderson9a82aca2024-08-10 09:59:37 -040069 return -ENOENT;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090070}
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 */
80static 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 Anderson9a82aca2024-08-10 09:59:37 -040089 return -ENOENT;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090090 }
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 Anderson9a82aca2024-08-10 09:59:37 -0400102 return -ENOENT;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900103}
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 */
114static 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 Anderson9a82aca2024-08-10 09:59:37 -0400122 return -ENOENT;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900123 }
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 Anderson9a82aca2024-08-10 09:59:37 -0400129 return -ENOENT;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900130 }
131 return ops->pinmux_set(dev, selector, func_selector);
132 }
133}
134#else
135static int pinmux_func_name_to_selector(struct udevice *dev,
136 const char *function)
137{
138 return 0;
139}
140
141static 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 */
157static 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 Anderson9a82aca2024-08-10 09:59:37 -0400165 return -ENOENT;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900166 }
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 Anderson9a82aca2024-08-10 09:59:37 -0400179 return -ENOENT;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900180}
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 */
192static 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 Anderson9a82aca2024-08-10 09:59:37 -0400201 return -ENOENT;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900202 }
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 Anderson9a82aca2024-08-10 09:59:37 -0400209 return -ENOENT;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900210 }
211 return ops->pinconf_set(dev, selector, param, argument);
212 }
213}
214#else
215static int pinconf_prop_name_to_param(struct udevice *dev,
216 const char *property, u32 *default_value)
217{
Sean Anderson9a82aca2024-08-10 09:59:37 -0400218 return -ENOENT;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900219}
220
221static 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 Anderson319ca702020-09-14 11:01:55 -0400229enum pinmux_subnode_type {
230 PST_NONE = 0,
231 PST_PIN,
232 PST_GROUP,
233 PST_PINMUX,
234};
235
Pali Rohárd80ed172022-07-25 13:56:08 +0200236static const char *alloc_name_with_prefix(const char *name, const char *prefix)
237{
238 if (prefix) {
John Keeping8573ec42022-09-07 12:11:42 +0100239 char *name_with_prefix = malloc(strlen(prefix) + strlen(name) + 1);
Pali Rohárd80ed172022-07-25 13:56:08 +0200240 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
248static 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 Yamadaf8efa632015-08-27 12:44:29 +0900254/**
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 Anderson319ca702020-09-14 11:01:55 -0400261 * @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 Yamadaf8efa632015-08-27 12:44:29 +0900264 * @return: 0 on success, or negative error code on failure
265 */
266static int pinctrl_generic_set_state_one(struct udevice *dev,
267 struct udevice *config,
Pali Rohárd80ed172022-07-25 13:56:08 +0200268 const char *prefix,
Sean Anderson319ca702020-09-14 11:01:55 -0400269 enum pinmux_subnode_type subnode_type,
270 unsigned selector)
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900271{
Pali Rohárd80ed172022-07-25 13:56:08 +0200272 const char *function_propname;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900273 const char *propname;
274 const void *value;
Patrick Delaunay73237dc2020-01-13 11:34:57 +0100275 struct ofprop property;
276 int len, func_selector, param, ret;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900277 u32 arg, default_val;
278
Sean Anderson319ca702020-09-14 11:01:55 -0400279 assert(subnode_type != PST_NONE);
280
Pali Rohárd80ed172022-07-25 13:56:08 +0200281 function_propname = alloc_name_with_prefix("function", prefix);
282 if (!function_propname)
283 return -ENOMEM;
284
Patrick Delaunay73237dc2020-01-13 11:34:57 +0100285 dev_for_each_property(property, config) {
286 value = dev_read_prop_by_prop(&property, &propname, &len);
Pali Rohárd80ed172022-07-25 13:56:08 +0200287 if (!value) {
288 free_name_with_prefix(function_propname, prefix);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900289 return -EINVAL;
Pali Rohárd80ed172022-07-25 13:56:08 +0200290 }
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900291
Sean Anderson319ca702020-09-14 11:01:55 -0400292 /* pinmux subnodes already have their muxing set */
293 if (subnode_type != PST_PINMUX &&
Pali Rohárd80ed172022-07-25 13:56:08 +0200294 !strcmp(propname, function_propname)) {
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900295 func_selector = pinmux_func_name_to_selector(dev,
296 value);
Pali Rohárd80ed172022-07-25 13:56:08 +0200297 if (func_selector < 0) {
298 free_name_with_prefix(function_propname, prefix);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900299 return func_selector;
Pali Rohárd80ed172022-07-25 13:56:08 +0200300 }
Sean Anderson319ca702020-09-14 11:01:55 -0400301 ret = pinmux_enable_setting(dev,
302 subnode_type == PST_GROUP,
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900303 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 Anderson319ca702020-09-14 11:01:55 -0400316 ret = pinconf_enable_setting(dev,
317 subnode_type == PST_GROUP,
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900318 selector, param, arg);
319 }
320
Pali Rohárd80ed172022-07-25 13:56:08 +0200321 if (ret) {
322 free_name_with_prefix(function_propname, prefix);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900323 return ret;
Pali Rohárd80ed172022-07-25 13:56:08 +0200324 }
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900325 }
326
Pali Rohárd80ed172022-07-25 13:56:08 +0200327 free_name_with_prefix(function_propname, prefix);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900328 return 0;
329}
330
331/**
Sean Anderson319ca702020-09-14 11:01:55 -0400332 * 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 */
340static enum pinmux_subnode_type pinctrl_generic_get_subnode_type(struct udevice *dev,
341 struct udevice *config,
Pali Rohárd80ed172022-07-25 13:56:08 +0200342 const char *prefix,
Sean Anderson319ca702020-09-14 11:01:55 -0400343 int *count)
344{
345 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
Pali Rohárd80ed172022-07-25 13:56:08 +0200346 const char *propname;
Sean Anderson319ca702020-09-14 11:01:55 -0400347
Pali Rohárd80ed172022-07-25 13:56:08 +0200348 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 Anderson319ca702020-09-14 11:01:55 -0400353 if (*count >= 0)
354 return PST_PIN;
355
Pali Rohárd80ed172022-07-25 13:56:08 +0200356 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 Anderson319ca702020-09-14 11:01:55 -0400361 if (*count >= 0)
362 return PST_GROUP;
363
364 if (ops->pinmux_property_set) {
Pali Rohárd80ed172022-07-25 13:56:08 +0200365 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 Anderson319ca702020-09-14 11:01:55 -0400370 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 Yamadaf8efa632015-08-27 12:44:29 +0900381 * 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árd80ed172022-07-25 13:56:08 +0200385 * @prefix: device tree property prefix (e.g. vendor specific)
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900386 * @return: 0 on success, or negative error code on failure
387 */
388static int pinctrl_generic_set_state_subnode(struct udevice *dev,
Pali Rohárd80ed172022-07-25 13:56:08 +0200389 struct udevice *config,
390 const char *prefix)
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900391{
Sean Anderson319ca702020-09-14 11:01:55 -0400392 enum pinmux_subnode_type subnode_type;
Pali Rohárd80ed172022-07-25 13:56:08 +0200393 const char *propname;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900394 const char *name;
Sean Anderson319ca702020-09-14 11:01:55 -0400395 int count, selector, i, ret, scratch;
396 const u32 *pinmux_groups = NULL; /* prevent use-uninitialized warning */
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900397
Pali Rohárd80ed172022-07-25 13:56:08 +0200398 subnode_type = pinctrl_generic_get_subnode_type(dev, config, prefix, &count);
Sean Anderson319ca702020-09-14 11:01:55 -0400399
400 debug("%s(%s, %s): count=%d\n", __func__, dev->name, config->name,
401 count);
402
403 if (subnode_type == PST_PINMUX) {
Pali Rohárd80ed172022-07-25 13:56:08 +0200404 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 Anderson319ca702020-09-14 11:01:55 -0400409 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árd80ed172022-07-25 13:56:08 +0200416 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 Anderson319ca702020-09-14 11:01:55 -0400421 if (ret)
422 return ret;
423 selector = pinctrl_pin_name_to_selector(dev, name);
424 break;
425 case PST_GROUP:
Pali Rohárd80ed172022-07-25 13:56:08 +0200426 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 Anderson319ca702020-09-14 11:01:55 -0400431 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 Galvanicc52ad32016-08-16 11:49:47 +0200445 /* skip this node; may contain config child nodes */
446 return 0;
447 }
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900448
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900449 if (selector < 0)
450 return selector;
451
Pali Rohárd80ed172022-07-25 13:56:08 +0200452 ret = pinctrl_generic_set_state_one(dev, config, prefix,
453 subnode_type, selector);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900454 if (ret)
455 return ret;
456 }
457
458 return 0;
459}
460
Pali Rohárd80ed172022-07-25 13:56:08 +0200461int pinctrl_generic_set_state_prefix(struct udevice *dev, struct udevice *config,
462 const char *prefix)
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900463{
464 struct udevice *child;
465 int ret;
466
Pali Rohárd80ed172022-07-25 13:56:08 +0200467 ret = pinctrl_generic_set_state_subnode(dev, config, prefix);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900468 if (ret)
469 return ret;
470
471 for (device_find_first_child(config, &child);
472 child;
473 device_find_next_child(&child)) {
Pali Rohárd80ed172022-07-25 13:56:08 +0200474 ret = pinctrl_generic_set_state_subnode(dev, child, prefix);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900475 if (ret)
476 return ret;
477 }
478
479 return 0;
480}
Pali Rohárd80ed172022-07-25 13:56:08 +0200481
482int pinctrl_generic_set_state(struct udevice *dev, struct udevice *config)
483{
484 return pinctrl_generic_set_state_prefix(dev, config, NULL);
485}