xref: /rk3399_rockchip-uboot/drivers/pinctrl/pinctrl-uclass.c (revision a033e9375e181f1f8613471f5f5e4cff6d041b71)
1 /*
2  * Copyright (C) 2015  Masahiro Yamada <yamada.masahiro@socionext.com>
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <linux/libfdt.h>
9 #include <linux/err.h>
10 #include <linux/list.h>
11 #include <dm.h>
12 #include <dm/lists.h>
13 #include <dm/pinctrl.h>
14 #include <dm/util.h>
15 #include <dm/of_access.h>
16 
17 DECLARE_GLOBAL_DATA_PTR;
18 
19 int pinctrl_decode_pin_config(const void *blob, int node)
20 {
21 	int flags = 0;
22 
23 	if (fdtdec_get_bool(blob, node, "bias-pull-up"))
24 		flags |= 1 << PIN_CONFIG_BIAS_PULL_UP;
25 	else if (fdtdec_get_bool(blob, node, "bias-pull-down"))
26 		flags |= 1 << PIN_CONFIG_BIAS_PULL_DOWN;
27 
28 	return flags;
29 }
30 
31 #if CONFIG_IS_ENABLED(PINCTRL_FULL)
32 /**
33  * pinctrl_config_one() - apply pinctrl settings for a single node
34  *
35  * @config: pin configuration node
36  * @return: 0 on success, or negative error code on failure
37  */
38 static int pinctrl_config_one(struct udevice *config)
39 {
40 	struct udevice *pctldev;
41 	const struct pinctrl_ops *ops;
42 
43 	pctldev = config;
44 	for (;;) {
45 		pctldev = dev_get_parent(pctldev);
46 		if (!pctldev) {
47 			dev_err(config, "could not find pctldev\n");
48 			return -EINVAL;
49 		}
50 		if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
51 			break;
52 	}
53 
54 	ops = pinctrl_get_ops(pctldev);
55 	return ops->set_state(pctldev, config);
56 }
57 
58 /**
59  * pinctrl_select_state_full() - full implementation of pinctrl_select_state
60  *
61  * @dev: peripheral device
62  * @statename: state name, like "default"
63  * @return: 0 on success, or negative error code on failure
64  */
65 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
66 {
67 	char propname[32]; /* long enough */
68 	const fdt32_t *list;
69 	uint32_t phandle;
70 	struct udevice *config;
71 	int state, size, i, ret;
72 
73 	state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
74 	if (state < 0) {
75 		char *end;
76 		/*
77 		 * If statename is not found in "pinctrl-names",
78 		 * assume statename is just the integer state ID.
79 		 */
80 		state = simple_strtoul(statename, &end, 10);
81 		if (*end)
82 			return -ENOSYS;
83 	}
84 
85 	snprintf(propname, sizeof(propname), "pinctrl-%d", state);
86 	list = dev_read_prop(dev, propname, &size);
87 	if (!list)
88 		return -ENOSYS;
89 
90 	size /= sizeof(*list);
91 	for (i = 0; i < size; i++) {
92 		phandle = fdt32_to_cpu(*list++);
93 		ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
94 						      &config);
95 		if (ret) {
96 			dev_warn(dev, "%s: uclass_get_device_by_phandle_id: err=%d\n",
97 				__func__, ret);
98 			continue;
99 		}
100 
101 		ret = pinctrl_config_one(config);
102 		if (ret) {
103 			dev_warn(dev, "%s: pinctrl_config_one: err=%d\n",
104 				__func__, ret);
105 			continue;
106 		}
107 	}
108 
109 	return 0;
110 }
111 
112 /**
113  * pinconfig_post_bind() - post binding for PINCONFIG uclass
114  * Recursively bind its children as pinconfig devices.
115  *
116  * @dev: pinconfig device
117  * @return: 0 on success, or negative error code on failure
118  */
119 static int pinconfig_post_bind(struct udevice *dev)
120 {
121 	bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
122 	const char *name;
123 	ofnode node;
124 	int ret;
125 
126 	if (!dev_of_valid(dev))
127 		return 0;
128 
129 	dev_for_each_subnode(node, dev) {
130 		if (pre_reloc_only &&
131 		    !ofnode_pre_reloc(node))
132 			continue;
133 		/*
134 		 * If this node has "compatible" property, this is not
135 		 * a pin configuration node, but a normal device. skip.
136 		 */
137 		ofnode_get_property(node, "compatible", &ret);
138 		if (ret >= 0)
139 			continue;
140 
141 		if (ret != -FDT_ERR_NOTFOUND)
142 			return ret;
143 
144 		name = ofnode_get_name(node);
145 		if (!name)
146 			return -EINVAL;
147 		ret = device_bind_driver_to_node(dev, "pinconfig", name,
148 						 node, NULL);
149 		if (ret)
150 			return ret;
151 	}
152 
153 	return 0;
154 }
155 
156 UCLASS_DRIVER(pinconfig) = {
157 	.id = UCLASS_PINCONFIG,
158 	.post_bind = pinconfig_post_bind,
159 	.name = "pinconfig",
160 };
161 
162 U_BOOT_DRIVER(pinconfig_generic) = {
163 	.name = "pinconfig",
164 	.id = UCLASS_PINCONFIG,
165 };
166 
167 #else
168 static int pinconfig_post_bind(struct udevice *dev)
169 {
170 	return 0;
171 }
172 
173 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
174 {
175 	return 0;
176 }
177 #endif
178 
179 static int
180 pinctrl_gpio_get_pinctrl_and_offset(struct udevice *dev, unsigned offset,
181 				    struct udevice **pctldev,
182 				    unsigned int *pin_selector)
183 {
184 	struct ofnode_phandle_args args;
185 	unsigned gpio_offset, pfc_base, pfc_pins;
186 	int ret;
187 
188 	ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
189 					 0, &args);
190 	if (ret) {
191 		dev_dbg(dev, "%s: dev_read_phandle_with_args: err=%d\n",
192 			__func__, ret);
193 		return ret;
194 	}
195 
196 	ret = uclass_get_device_by_ofnode(UCLASS_PINCTRL,
197 					  args.node, pctldev);
198 	if (ret) {
199 		dev_dbg(dev,
200 			"%s: uclass_get_device_by_of_offset failed: err=%d\n",
201 			__func__, ret);
202 		return ret;
203 	}
204 
205 	gpio_offset = args.args[0];
206 	pfc_base = args.args[1];
207 	pfc_pins = args.args[2];
208 
209 	if (offset < gpio_offset || offset > gpio_offset + pfc_pins) {
210 		dev_dbg(dev,
211 			"%s: GPIO can not be mapped to pincontrol pin\n",
212 			__func__);
213 		return -EINVAL;
214 	}
215 
216 	offset -= gpio_offset;
217 	offset += pfc_base;
218 	*pin_selector = offset;
219 
220 	return 0;
221 }
222 
223 /**
224  * pinctrl_gpio_request() - request a single pin to be used as GPIO
225  *
226  * @dev: GPIO peripheral device
227  * @offset: the GPIO pin offset from the GPIO controller
228  * @return: 0 on success, or negative error code on failure
229  */
230 int pinctrl_gpio_request(struct udevice *dev, unsigned offset)
231 {
232 	const struct pinctrl_ops *ops;
233 	struct udevice *pctldev;
234 	unsigned int pin_selector;
235 	int ret;
236 
237 	ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
238 						  &pctldev, &pin_selector);
239 	if (ret)
240 		return ret;
241 
242 	ops = pinctrl_get_ops(pctldev);
243 	if (!ops || !ops->gpio_request_enable)
244 		return -ENOTSUPP;
245 
246 	return ops->gpio_request_enable(pctldev, pin_selector);
247 }
248 
249 /**
250  * pinctrl_gpio_free() - free a single pin used as GPIO
251  *
252  * @dev: GPIO peripheral device
253  * @offset: the GPIO pin offset from the GPIO controller
254  * @return: 0 on success, or negative error code on failure
255  */
256 int pinctrl_gpio_free(struct udevice *dev, unsigned offset)
257 {
258 	const struct pinctrl_ops *ops;
259 	struct udevice *pctldev;
260 	unsigned int pin_selector;
261 	int ret;
262 
263 	ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
264 						  &pctldev, &pin_selector);
265 	if (ret)
266 		return ret;
267 
268 	ops = pinctrl_get_ops(pctldev);
269 	if (!ops || !ops->gpio_disable_free)
270 		return -ENOTSUPP;
271 
272 	return ops->gpio_disable_free(pctldev, pin_selector);
273 }
274 
275 /**
276  * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
277  *
278  * @dev: peripheral device
279  * @return: 0 on success, or negative error code on failure
280  */
281 static int pinctrl_select_state_simple(struct udevice *dev)
282 {
283 	struct udevice *pctldev;
284 	struct pinctrl_ops *ops;
285 	int ret;
286 
287 	/*
288 	 * For most system, there is only one pincontroller device. But in
289 	 * case of multiple pincontroller devices, probe the one with sequence
290 	 * number 0 (defined by alias) to avoid race condition.
291 	 */
292 	ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev);
293 	if (ret)
294 		/* if not found, get the first one */
295 		ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
296 	if (ret)
297 		return ret;
298 
299 	ops = pinctrl_get_ops(pctldev);
300 	if (!ops->set_state_simple) {
301 		dev_dbg(dev, "set_state_simple op missing\n");
302 		return -ENOSYS;
303 	}
304 
305 	return ops->set_state_simple(pctldev, dev);
306 }
307 
308 int pinctrl_select_state(struct udevice *dev, const char *statename)
309 {
310 	/*
311 	 * Some device which is logical like mmc.blk, do not have
312 	 * a valid ofnode.
313 	 */
314 	if (!ofnode_valid(dev->node))
315 		return 0;
316 	/*
317 	 * Try full-implemented pinctrl first.
318 	 * If it fails or is not implemented, try simple one.
319 	 */
320 	if (CONFIG_IS_ENABLED(PINCTRL_FULL))
321 		return pinctrl_select_state_full(dev, statename);
322 
323 	return pinctrl_select_state_simple(dev);
324 }
325 
326 int pinctrl_request(struct udevice *dev, int func, int flags)
327 {
328 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
329 
330 	if (!ops->request)
331 		return -ENOSYS;
332 
333 	return ops->request(dev, func, flags);
334 }
335 
336 int pinctrl_request_noflags(struct udevice *dev, int func)
337 {
338 	return pinctrl_request(dev, func, 0);
339 }
340 
341 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
342 {
343 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
344 
345 	if (!ops->get_periph_id)
346 		return -ENOSYS;
347 
348 	return ops->get_periph_id(dev, periph);
349 }
350 
351 int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
352 {
353 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
354 
355 	if (!ops->get_gpio_mux)
356 		return -ENOSYS;
357 
358 	return ops->get_gpio_mux(dev, banknum, index);
359 }
360 
361 int pinctrl_get_pins_count(struct udevice *dev)
362 {
363 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
364 
365 	if (!ops->get_pins_count)
366 		return -ENOSYS;
367 
368 	return ops->get_pins_count(dev);
369 }
370 
371 int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
372 			 int size)
373 {
374 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
375 
376 	if (!ops->get_pin_name)
377 		return -ENOSYS;
378 
379 	snprintf(buf, size, ops->get_pin_name(dev, selector));
380 
381 	return 0;
382 }
383 
384 int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf,
385 			   int size)
386 {
387 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
388 
389 	if (!ops->get_pin_muxing)
390 		return -ENOSYS;
391 
392 	return ops->get_pin_muxing(dev, selector, buf, size);
393 }
394 
395 /**
396  * pinconfig_post_bind() - post binding for PINCTRL uclass
397  * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
398  *
399  * @dev: pinctrl device
400  * @return: 0 on success, or negative error code on failure
401  */
402 static int pinctrl_post_bind(struct udevice *dev)
403 {
404 	const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
405 
406 	if (!ops) {
407 		dev_dbg(dev, "ops is not set.  Do not bind.\n");
408 		return -EINVAL;
409 	}
410 
411 	/*
412 	 * If set_state callback is set, we assume this pinctrl driver is the
413 	 * full implementation.  In this case, its child nodes should be bound
414 	 * so that peripheral devices can easily search in parent devices
415 	 * during later DT-parsing.
416 	 */
417 	if (ops->set_state)
418 		return pinconfig_post_bind(dev);
419 
420 	return 0;
421 }
422 
423 UCLASS_DRIVER(pinctrl) = {
424 	.id = UCLASS_PINCTRL,
425 	.post_bind = pinctrl_post_bind,
426 	.flags = DM_UC_FLAG_SEQ_ALIAS,
427 	.name = "pinctrl",
428 };
429