1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015  Masahiro Yamada <yamada.masahiro@socionext.com>
4  */
5 
6 #include <dm.h>
7 #include <dm/device_compat.h>
8 #include <linux/compat.h>
9 #include <dm/pinctrl.h>
10 
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  */
pinctrl_pin_name_to_selector(struct udevice * dev,const char * pin)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");
25 		return -ENOENT;
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 
38 	return -ENOENT;
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  */
pinctrl_group_name_to_selector(struct udevice * dev,const char * group)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");
56 		return -ENOENT;
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 
69 	return -ENOENT;
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  */
pinmux_func_name_to_selector(struct udevice * dev,const char * function)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");
89 		return -ENOENT;
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 
102 	return -ENOENT;
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  */
pinmux_enable_setting(struct udevice * dev,bool is_group,unsigned selector,unsigned func_selector)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");
122 			return -ENOENT;
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");
129 			return -ENOENT;
130 		}
131 		return ops->pinmux_set(dev, selector, func_selector);
132 	}
133 }
134 #else
pinmux_func_name_to_selector(struct udevice * dev,const char * function)135 static int pinmux_func_name_to_selector(struct udevice *dev,
136 					const char *function)
137 {
138 	return 0;
139 }
140 
pinmux_enable_setting(struct udevice * dev,bool is_group,unsigned selector,unsigned func_selector)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  */
pinconf_prop_name_to_param(struct udevice * dev,const char * property,u32 * default_value)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");
165 		return -ENOENT;
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 
179 	return -ENOENT;
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  */
pinconf_enable_setting(struct udevice * dev,bool is_group,unsigned selector,unsigned param,u32 argument)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");
201 			return -ENOENT;
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");
209 			return -ENOENT;
210 		}
211 		return ops->pinconf_set(dev, selector, param, argument);
212 	}
213 }
214 #else
pinconf_prop_name_to_param(struct udevice * dev,const char * property,u32 * default_value)215 static int pinconf_prop_name_to_param(struct udevice *dev,
216 				      const char *property, u32 *default_value)
217 {
218 	return -ENOENT;
219 }
220 
pinconf_enable_setting(struct udevice * dev,bool is_group,unsigned selector,unsigned param,u32 argument)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 
229 enum pinmux_subnode_type {
230 	PST_NONE = 0,
231 	PST_PIN,
232 	PST_GROUP,
233 	PST_PINMUX,
234 };
235 
alloc_name_with_prefix(const char * name,const char * prefix)236 static const char *alloc_name_with_prefix(const char *name, const char *prefix)
237 {
238 	if (prefix) {
239 		char *name_with_prefix = malloc(strlen(prefix) + strlen(name) + 1);
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 
free_name_with_prefix(const char * name_with_prefix,const char * prefix)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 
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
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
264  * @return: 0 on success, or negative error code on failure
265  */
pinctrl_generic_set_state_one(struct udevice * dev,struct udevice * config,const char * prefix,enum pinmux_subnode_type subnode_type,unsigned selector)266 static int pinctrl_generic_set_state_one(struct udevice *dev,
267 					 struct udevice *config,
268 					 const char *prefix,
269 					 enum pinmux_subnode_type subnode_type,
270 					 unsigned selector)
271 {
272 	const char *function_propname;
273 	const char *propname;
274 	const void *value;
275 	struct ofprop property;
276 	int len, func_selector, param, ret;
277 	u32 arg, default_val;
278 
279 	assert(subnode_type != PST_NONE);
280 
281 	function_propname = alloc_name_with_prefix("function", prefix);
282 	if (!function_propname)
283 		return -ENOMEM;
284 
285 	dev_for_each_property(property, config) {
286 		value = dev_read_prop_by_prop(&property, &propname, &len);
287 		if (!value) {
288 			free_name_with_prefix(function_propname, prefix);
289 			return -EINVAL;
290 		}
291 
292 		/* pinmux subnodes already have their muxing set */
293 		if (subnode_type != PST_PINMUX &&
294 		    !strcmp(propname, function_propname)) {
295 			func_selector = pinmux_func_name_to_selector(dev,
296 								     value);
297 			if (func_selector < 0) {
298 				free_name_with_prefix(function_propname, prefix);
299 				return func_selector;
300 			}
301 			ret = pinmux_enable_setting(dev,
302 						    subnode_type == PST_GROUP,
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 
316 			ret = pinconf_enable_setting(dev,
317 						     subnode_type == PST_GROUP,
318 						     selector, param, arg);
319 		}
320 
321 		if (ret) {
322 			free_name_with_prefix(function_propname, prefix);
323 			return ret;
324 		}
325 	}
326 
327 	free_name_with_prefix(function_propname, prefix);
328 	return 0;
329 }
330 
331 /**
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  */
pinctrl_generic_get_subnode_type(struct udevice * dev,struct udevice * config,const char * prefix,int * count)340 static enum pinmux_subnode_type pinctrl_generic_get_subnode_type(struct udevice *dev,
341 								 struct udevice *config,
342 								 const char *prefix,
343 								 int *count)
344 {
345 	const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
346 	const char *propname;
347 
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);
353 	if (*count >= 0)
354 		return PST_PIN;
355 
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);
361 	if (*count >= 0)
362 		return PST_GROUP;
363 
364 	if (ops->pinmux_property_set) {
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);
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 /**
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
385  * @prefix: device tree property prefix (e.g. vendor specific)
386  * @return: 0 on success, or negative error code on failure
387  */
pinctrl_generic_set_state_subnode(struct udevice * dev,struct udevice * config,const char * prefix)388 static int pinctrl_generic_set_state_subnode(struct udevice *dev,
389 					     struct udevice *config,
390 					     const char *prefix)
391 {
392 	enum pinmux_subnode_type subnode_type;
393 	const char *propname;
394 	const char *name;
395 	int count, selector, i, ret, scratch;
396 	const u32 *pinmux_groups = NULL; /* prevent use-uninitialized warning */
397 
398 	subnode_type = pinctrl_generic_get_subnode_type(dev, config, prefix, &count);
399 
400 	debug("%s(%s, %s): count=%d\n", __func__, dev->name, config->name,
401 	      count);
402 
403 	if (subnode_type == PST_PINMUX) {
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);
409 		if (!pinmux_groups)
410 			return -EINVAL;
411 	}
412 
413 	for (i = 0; i < count; i++) {
414 		switch (subnode_type) {
415 		case PST_PIN:
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);
421 			if (ret)
422 				return ret;
423 			selector = pinctrl_pin_name_to_selector(dev, name);
424 			break;
425 		case PST_GROUP:
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);
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:
445 			/* skip this node; may contain config child nodes */
446 			return 0;
447 		}
448 
449 		if (selector < 0)
450 			return selector;
451 
452 		ret = pinctrl_generic_set_state_one(dev, config, prefix,
453 						    subnode_type, selector);
454 		if (ret)
455 			return ret;
456 	}
457 
458 	return 0;
459 }
460 
pinctrl_generic_set_state_prefix(struct udevice * dev,struct udevice * config,const char * prefix)461 int pinctrl_generic_set_state_prefix(struct udevice *dev, struct udevice *config,
462 				     const char *prefix)
463 {
464 	struct udevice *child;
465 	int ret;
466 
467 	ret = pinctrl_generic_set_state_subnode(dev, config, prefix);
468 	if (ret)
469 		return ret;
470 
471 	for (device_find_first_child(config, &child);
472 	     child;
473 	     device_find_next_child(&child)) {
474 		ret = pinctrl_generic_set_state_subnode(dev, child, prefix);
475 		if (ret)
476 			return ret;
477 	}
478 
479 	return 0;
480 }
481 
pinctrl_generic_set_state(struct udevice * dev,struct udevice * config)482 int pinctrl_generic_set_state(struct udevice *dev, struct udevice *config)
483 {
484 	return pinctrl_generic_set_state_prefix(dev, config, NULL);
485 }
486