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