summaryrefslogtreecommitdiff
path: root/drivers/pinctrl/pinctrl-generic.c
blob: 8909b57810af05c51f4cb46732e418a529d70442 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2015  Masahiro Yamada <yamada.masahiro@socionext.com>
 */

#include <common.h>
#include <dm.h>
#include <dm/device_compat.h>
#include <linux/compat.h>
#include <dm/pinctrl.h>

/**
 * pinctrl_pin_name_to_selector() - return the pin selector for a pin
 *
 * @dev: pin controller device
 * @pin: the pin name to look up
 * @return: pin selector, or negative error code on failure
 */
static int pinctrl_pin_name_to_selector(struct udevice *dev, const char *pin)
{
	const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
	unsigned npins, selector;

	if (!ops->get_pins_count || !ops->get_pin_name) {
		dev_dbg(dev, "get_pins_count or get_pin_name missing\n");
		return -ENOSYS;
	}

	npins = ops->get_pins_count(dev);

	/* See if this pctldev has this pin */
	for (selector = 0; selector < npins; selector++) {
		const char *pname = ops->get_pin_name(dev, selector);

		if (!strcmp(pin, pname))
			return selector;
	}

	return -ENOSYS;
}

/**
 * pinctrl_group_name_to_selector() - return the group selector for a group
 *
 * @dev: pin controller device
 * @group: the pin group name to look up
 * @return: pin group selector, or negative error code on failure
 */
static int pinctrl_group_name_to_selector(struct udevice *dev,
					  const char *group)
{
	const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
	unsigned ngroups, selector;

	if (!ops->get_groups_count || !ops->get_group_name) {
		dev_dbg(dev, "get_groups_count or get_group_name missing\n");
		return -ENOSYS;
	}

	ngroups = ops->get_groups_count(dev);

	/* See if this pctldev has this group */
	for (selector = 0; selector < ngroups; selector++) {
		const char *gname = ops->get_group_name(dev, selector);

		if (!strcmp(group, gname))
			return selector;
	}

	return -ENOSYS;
}

#if CONFIG_IS_ENABLED(PINMUX)
/**
 * pinmux_func_name_to_selector() - return the function selector for a function
 *
 * @dev: pin controller device
 * @function: the function name to look up
 * @return: function selector, or negative error code on failure
 */
static int pinmux_func_name_to_selector(struct udevice *dev,
					const char *function)
{
	const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
	unsigned nfuncs, selector = 0;

	if (!ops->get_functions_count || !ops->get_function_name) {
		dev_dbg(dev,
			"get_functions_count or get_function_name missing\n");
		return -ENOSYS;
	}

	nfuncs = ops->get_functions_count(dev);

	/* See if this pctldev has this function */
	for (selector = 0; selector < nfuncs; selector++) {
		const char *fname = ops->get_function_name(dev, selector);

		if (!strcmp(function, fname))
			return selector;
	}

	return -ENOSYS;
}

/**
 * pinmux_enable_setting() - enable pin-mux setting for a certain pin/group
 *
 * @dev: pin controller device
 * @is_group: target of operation (true: pin group, false: pin)
 * @selector: pin selector or group selector, depending on @is_group
 * @func_selector: function selector
 * @return: 0 on success, or negative error code on failure
 */
static int pinmux_enable_setting(struct udevice *dev, bool is_group,
				 unsigned selector, unsigned func_selector)
{
	const struct pinctrl_ops *ops = pinctrl_get_ops(dev);

	if (is_group) {
		if (!ops->pinmux_group_set) {
			dev_dbg(dev, "pinmux_group_set op missing\n");
			return -ENOSYS;
		}

		return ops->pinmux_group_set(dev, selector, func_selector);
	} else {
		if (!ops->pinmux_set) {
			dev_dbg(dev, "pinmux_set op missing\n");
			return -ENOSYS;
		}
		return ops->pinmux_set(dev, selector, func_selector);
	}
}
#else
static int pinmux_func_name_to_selector(struct udevice *dev,
					const char *function)
{
	return 0;
}

static int pinmux_enable_setting(struct udevice *dev, bool is_group,
				 unsigned selector, unsigned func_selector)
{
	return 0;
}
#endif

#if CONFIG_IS_ENABLED(PINCONF)
/**
 * pinconf_prop_name_to_param() - return parameter ID for a property name
 *
 * @dev: pin controller device
 * @property: property name in DTS, such as "bias-pull-up", "slew-rate", etc.
 * @default_value: return default value in case no value is specified in DTS
 * @return: return pamater ID, or negative error code on failure
 */
static int pinconf_prop_name_to_param(struct udevice *dev,
				      const char *property, u32 *default_value)
{
	const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
	const struct pinconf_param *p, *end;

	if (!ops->pinconf_num_params || !ops->pinconf_params) {
		dev_dbg(dev, "pinconf_num_params or pinconf_params missing\n");
		return -ENOSYS;
	}

	p = ops->pinconf_params;
	end = p + ops->pinconf_num_params;

	/* See if this pctldev supports this parameter */
	for (; p < end; p++) {
		if (!strcmp(property, p->property)) {
			*default_value = p->default_value;
			return p->param;
		}
	}

	return -ENOSYS;
}

/**
 * pinconf_enable_setting() - apply pin configuration for a certain pin/group
 *
 * @dev: pin controller device
 * @is_group: target of operation (true: pin group, false: pin)
 * @selector: pin selector or group selector, depending on @is_group
 * @param: configuration paramter
 * @argument: argument taken by some configuration parameters
 * @return: 0 on success, or negative error code on failure
 */
static int pinconf_enable_setting(struct udevice *dev, bool is_group,
				  unsigned selector, unsigned param,
				  u32 argument)
{
	const struct pinctrl_ops *ops = pinctrl_get_ops(dev);

	if (is_group) {
		if (!ops->pinconf_group_set) {
			dev_dbg(dev, "pinconf_group_set op missing\n");
			return -ENOSYS;
		}

		return ops->pinconf_group_set(dev, selector, param,
					      argument);
	} else {
		if (!ops->pinconf_set) {
			dev_dbg(dev, "pinconf_set op missing\n");
			return -ENOSYS;
		}
		return ops->pinconf_set(dev, selector, param, argument);
	}
}
#else
static int pinconf_prop_name_to_param(struct udevice *dev,
				      const char *property, u32 *default_value)
{
	return -ENOSYS;
}

static int pinconf_enable_setting(struct udevice *dev, bool is_group,
				  unsigned selector, unsigned param,
				  u32 argument)
{
	return 0;
}
#endif

enum pinmux_subnode_type {
	PST_NONE = 0,
	PST_PIN,
	PST_GROUP,
	PST_PINMUX,
};

static const char *alloc_name_with_prefix(const char *name, const char *prefix)
{
	if (prefix) {
		char *name_with_prefix = malloc(strlen(prefix) + strlen(name) + 1);
		if (name_with_prefix)
			sprintf(name_with_prefix, "%s%s", prefix, name);
		return name_with_prefix;
	} else {
		return name;
	}
}

static void free_name_with_prefix(const char *name_with_prefix, const char *prefix)
{
	if (prefix)
		free((char *)name_with_prefix);
}

/**
 * pinctrl_generic_set_state_one() - set state for a certain pin/group
 * Apply all pin multiplexing and pin configurations specified by @config
 * for a given pin or pin group.
 *
 * @dev: pin controller device
 * @config: pseudo device pointing to config node
 * @subnode_type: target of operation (pin, group, or pin specified by a pinmux
 * group)
 * @selector: pin selector or group selector, depending on @subnode_type
 * @return: 0 on success, or negative error code on failure
 */
static int pinctrl_generic_set_state_one(struct udevice *dev,
					 struct udevice *config,
					 const char *prefix,
					 enum pinmux_subnode_type subnode_type,
					 unsigned selector)
{
	const char *function_propname;
	const char *propname;
	const void *value;
	struct ofprop property;
	int len, func_selector, param, ret;
	u32 arg, default_val;

	assert(subnode_type != PST_NONE);

	function_propname = alloc_name_with_prefix("function", prefix);
	if (!function_propname)
		return -ENOMEM;

	dev_for_each_property(property, config) {
		value = dev_read_prop_by_prop(&property, &propname, &len);
		if (!value) {
			free_name_with_prefix(function_propname, prefix);
			return -EINVAL;
		}

		/* pinmux subnodes already have their muxing set */
		if (subnode_type != PST_PINMUX &&
		    !strcmp(propname, function_propname)) {
			func_selector = pinmux_func_name_to_selector(dev,
								     value);
			if (func_selector < 0) {
				free_name_with_prefix(function_propname, prefix);
				return func_selector;
			}
			ret = pinmux_enable_setting(dev,
						    subnode_type == PST_GROUP,
						    selector,
						    func_selector);
		} else {
			param = pinconf_prop_name_to_param(dev, propname,
							   &default_val);
			if (param < 0)
				continue; /* just skip unknown properties */

			if (len >= sizeof(fdt32_t))
				arg = fdt32_to_cpu(*(fdt32_t *)value);
			else
				arg = default_val;

			ret = pinconf_enable_setting(dev,
						     subnode_type == PST_GROUP,
						     selector, param, arg);
		}

		if (ret) {
			free_name_with_prefix(function_propname, prefix);
			return ret;
		}
	}

	free_name_with_prefix(function_propname, prefix);
	return 0;
}

/**
 * pinctrl_generic_get_subnode_type() - determine whether there is a valid
 * pins, groups, or pinmux property in the config node
 *
 * @dev: pin controller device
 * @config: pseudo device pointing to config node
 * @count: number of specifiers contained within the property
 * @return: the type of the subnode, or PST_NONE
 */
static enum pinmux_subnode_type pinctrl_generic_get_subnode_type(struct udevice *dev,
								 struct udevice *config,
								 const char *prefix,
								 int *count)
{
	const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
	const char *propname;

	propname = alloc_name_with_prefix("pins", prefix);
	if (!propname)
		return -ENOMEM;
	*count = dev_read_string_count(config, propname);
	free_name_with_prefix(propname, prefix);
	if (*count >= 0)
		return PST_PIN;

	propname = alloc_name_with_prefix("groups", prefix);
	if (!propname)
		return -ENOMEM;
	*count = dev_read_string_count(config, propname);
	free_name_with_prefix(propname, prefix);
	if (*count >= 0)
		return PST_GROUP;

	if (ops->pinmux_property_set) {
		propname = alloc_name_with_prefix("pinmux", prefix);
		if (!propname)
			return -ENOMEM;
		*count = dev_read_size(config, propname);
		free_name_with_prefix(propname, prefix);
		if (*count >= 0 && !(*count % sizeof(u32))) {
			*count /= sizeof(u32);
			return PST_PINMUX;
		}
	}

	*count = 0;
	return PST_NONE;
}

/**
 * pinctrl_generic_set_state_subnode() - apply all settings in config node
 *
 * @dev: pin controller device
 * @config: pseudo device pointing to config node
 * @prefix: device tree property prefix (e.g. vendor specific)
 * @return: 0 on success, or negative error code on failure
 */
static int pinctrl_generic_set_state_subnode(struct udevice *dev,
					     struct udevice *config,
					     const char *prefix)
{
	enum pinmux_subnode_type subnode_type;
	const char *propname;
	const char *name;
	int count, selector, i, ret, scratch;
	const u32 *pinmux_groups = NULL; /* prevent use-uninitialized warning */

	subnode_type = pinctrl_generic_get_subnode_type(dev, config, prefix, &count);

	debug("%s(%s, %s): count=%d\n", __func__, dev->name, config->name,
	      count);

	if (subnode_type == PST_PINMUX) {
		propname = alloc_name_with_prefix("pinmux", prefix);
		if (!propname)
			return -ENOMEM;
		pinmux_groups = dev_read_prop(config, propname, &scratch);
		free_name_with_prefix(propname, prefix);
		if (!pinmux_groups)
			return -EINVAL;
	}

	for (i = 0; i < count; i++) {
		switch (subnode_type) {
		case PST_PIN:
			propname = alloc_name_with_prefix("pins", prefix);
			if (!propname)
				return -ENOMEM;
			ret = dev_read_string_index(config, propname, i, &name);
			free_name_with_prefix(propname, prefix);
			if (ret)
				return ret;
			selector = pinctrl_pin_name_to_selector(dev, name);
			break;
		case PST_GROUP:
			propname = alloc_name_with_prefix("groups", prefix);
			if (!propname)
				return -ENOMEM;
			ret = dev_read_string_index(config, propname, i, &name);
			free_name_with_prefix(propname, prefix);
			if (ret)
				return ret;
			selector = pinctrl_group_name_to_selector(dev, name);
			break;
		case PST_PINMUX: {
			const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
			u32 pinmux_group = fdt32_to_cpu(pinmux_groups[i]);

			/* Checked for in pinctrl_generic_get_subnode_type */
			selector = ops->pinmux_property_set(dev, pinmux_group);
			break;
		}
		case PST_NONE:
		default:
			/* skip this node; may contain config child nodes */
			return 0;
		}

		if (selector < 0)
			return selector;

		ret = pinctrl_generic_set_state_one(dev, config, prefix,
						    subnode_type, selector);
		if (ret)
			return ret;
	}

	return 0;
}

int pinctrl_generic_set_state_prefix(struct udevice *dev, struct udevice *config,
				     const char *prefix)
{
	struct udevice *child;
	int ret;

	ret = pinctrl_generic_set_state_subnode(dev, config, prefix);
	if (ret)
		return ret;

	for (device_find_first_child(config, &child);
	     child;
	     device_find_next_child(&child)) {
		ret = pinctrl_generic_set_state_subnode(dev, child, prefix);
		if (ret)
			return ret;
	}

	return 0;
}

int pinctrl_generic_set_state(struct udevice *dev, struct udevice *config)
{
	return pinctrl_generic_set_state_prefix(dev, config, NULL);
}