1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
6 #define LOG_CATEGORY UCLASS_PINCTRL
10 #include <asm/global_data.h>
11 #include <dm/device_compat.h>
12 #include <linux/libfdt.h>
13 #include <linux/err.h>
14 #include <linux/list.h>
17 #include <dm/pinctrl.h>
19 #include <dm/of_access.h>
21 DECLARE_GLOBAL_DATA_PTR;
23 #if CONFIG_IS_ENABLED(PINCTRL_FULL)
25 * pinctrl_config_one() - apply pinctrl settings for a single node
27 * @config: pin configuration node
28 * @return: 0 on success, or negative error code on failure
30 static int pinctrl_config_one(struct udevice *config)
32 struct udevice *pctldev;
33 const struct pinctrl_ops *ops;
37 pctldev = dev_get_parent(pctldev);
39 dev_err(config, "could not find pctldev\n");
42 if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
46 ops = pinctrl_get_ops(pctldev);
47 return ops->set_state(pctldev, config);
51 * pinctrl_select_state_full() - full implementation of pinctrl_select_state
53 * @dev: peripheral device
54 * @statename: state name, like "default"
55 * @return: 0 on success, or negative error code on failure
57 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
59 char propname[32]; /* long enough */
62 struct udevice *config;
63 int state, size, i, ret;
65 state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
69 * If statename is not found in "pinctrl-names",
70 * assume statename is just the integer state ID.
72 state = dectoul(statename, &end);
77 snprintf(propname, sizeof(propname), "pinctrl-%d", state);
78 list = dev_read_prop(dev, propname, &size);
82 size /= sizeof(*list);
83 for (i = 0; i < size; i++) {
84 phandle = fdt32_to_cpu(*list++);
85 ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
88 dev_warn(dev, "%s: uclass_get_device_by_phandle_id: err=%d\n",
93 ret = pinctrl_config_one(config);
95 dev_warn(dev, "%s: pinctrl_config_one: err=%d\n",
105 * pinconfig_post_bind() - post binding for PINCONFIG uclass
106 * Recursively bind its children as pinconfig devices.
108 * @dev: pinconfig device
109 * @return: 0 on success, or negative error code on failure
111 static int pinconfig_post_bind(struct udevice *dev)
113 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
118 if (!dev_has_ofnode(dev))
121 dev_for_each_subnode(node, dev) {
122 if (pre_reloc_only &&
123 !ofnode_pre_reloc(node))
126 * If this node has "compatible" property, this is not
127 * a pin configuration node, but a normal device. skip.
129 ofnode_get_property(node, "compatible", &ret);
132 /* If this node has "gpio-controller" property, skip */
133 if (ofnode_read_bool(node, "gpio-controller"))
136 if (ret != -FDT_ERR_NOTFOUND)
139 name = ofnode_get_name(node);
142 ret = device_bind_driver_to_node(dev, "pinconfig", name,
151 UCLASS_DRIVER(pinconfig) = {
152 .id = UCLASS_PINCONFIG,
153 #if CONFIG_IS_ENABLED(PINCONF_RECURSIVE)
154 .post_bind = pinconfig_post_bind,
159 U_BOOT_DRIVER(pinconfig_generic) = {
161 .id = UCLASS_PINCONFIG,
165 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
170 static int pinconfig_post_bind(struct udevice *dev)
177 pinctrl_gpio_get_pinctrl_and_offset(struct udevice *dev, unsigned offset,
178 struct udevice **pctldev,
179 unsigned int *pin_selector)
181 struct ofnode_phandle_args args;
182 unsigned gpio_offset, pfc_base, pfc_pins;
185 ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
188 dev_dbg(dev, "%s: dev_read_phandle_with_args: err=%d\n",
193 ret = uclass_get_device_by_ofnode(UCLASS_PINCTRL,
197 "%s: uclass_get_device_by_of_offset failed: err=%d\n",
202 gpio_offset = args.args[0];
203 pfc_base = args.args[1];
204 pfc_pins = args.args[2];
206 if (offset < gpio_offset || offset > gpio_offset + pfc_pins) {
208 "%s: GPIO can not be mapped to pincontrol pin\n",
213 offset -= gpio_offset;
215 *pin_selector = offset;
221 * pinctrl_gpio_request() - request a single pin to be used as GPIO
223 * @dev: GPIO peripheral device
224 * @offset: the GPIO pin offset from the GPIO controller
225 * @return: 0 on success, or negative error code on failure
227 int pinctrl_gpio_request(struct udevice *dev, unsigned offset)
229 const struct pinctrl_ops *ops;
230 struct udevice *pctldev;
231 unsigned int pin_selector;
234 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
235 &pctldev, &pin_selector);
239 ops = pinctrl_get_ops(pctldev);
241 if (!ops->gpio_request_enable)
244 return ops->gpio_request_enable(pctldev, pin_selector);
248 * pinctrl_gpio_free() - free a single pin used as GPIO
250 * @dev: GPIO peripheral device
251 * @offset: the GPIO pin offset from the GPIO controller
252 * @return: 0 on success, or negative error code on failure
254 int pinctrl_gpio_free(struct udevice *dev, unsigned offset)
256 const struct pinctrl_ops *ops;
257 struct udevice *pctldev;
258 unsigned int pin_selector;
261 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
262 &pctldev, &pin_selector);
266 ops = pinctrl_get_ops(pctldev);
268 if (!ops->gpio_disable_free)
271 return ops->gpio_disable_free(pctldev, pin_selector);
275 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
277 * @dev: peripheral device
278 * @return: 0 on success, or negative error code on failure
280 static int pinctrl_select_state_simple(struct udevice *dev)
282 struct udevice *pctldev;
283 struct pinctrl_ops *ops;
287 * For most system, there is only one pincontroller device. But in
288 * case of multiple pincontroller devices, probe the one with sequence
289 * number 0 (defined by alias) to avoid race condition.
291 ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev);
293 /* if not found, get the first one */
294 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
298 ops = pinctrl_get_ops(pctldev);
299 if (!ops->set_state_simple) {
300 dev_dbg(dev, "set_state_simple op missing\n");
304 return ops->set_state_simple(pctldev, dev);
307 int pinctrl_select_state(struct udevice *dev, const char *statename)
310 * Some device which is logical like mmc.blk, do not have
313 if (!dev_has_ofnode(dev))
316 * Try full-implemented pinctrl first.
317 * If it fails or is not implemented, try simple one.
319 if (pinctrl_select_state_full(dev, statename))
320 return pinctrl_select_state_simple(dev);
325 int pinctrl_request(struct udevice *dev, int func, int flags)
327 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
332 return ops->request(dev, func, flags);
335 int pinctrl_request_noflags(struct udevice *dev, int func)
337 return pinctrl_request(dev, func, 0);
340 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
342 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
344 if (!ops->get_periph_id)
347 return ops->get_periph_id(dev, periph);
350 int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
352 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
354 if (!ops->get_gpio_mux)
357 return ops->get_gpio_mux(dev, banknum, index);
360 int pinctrl_get_pins_count(struct udevice *dev)
362 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
364 if (!ops->get_pins_count)
367 return ops->get_pins_count(dev);
370 int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
373 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
375 if (!ops->get_pin_name)
378 snprintf(buf, size, ops->get_pin_name(dev, selector));
383 int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf,
386 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
388 if (!ops->get_pin_muxing)
391 return ops->get_pin_muxing(dev, selector, buf, size);
395 * pinconfig_post_bind() - post binding for PINCTRL uclass
396 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
398 * @dev: pinctrl device
399 * @return: 0 on success, or negative error code on failure
401 static int __maybe_unused pinctrl_post_bind(struct udevice *dev)
403 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
406 dev_dbg(dev, "ops is not set. Do not bind.\n");
411 * If set_state callback is set, we assume this pinctrl driver is the
412 * full implementation. In this case, its child nodes should be bound
413 * so that peripheral devices can easily search in parent devices
414 * during later DT-parsing.
417 return pinconfig_post_bind(dev);
422 UCLASS_DRIVER(pinctrl) = {
423 .id = UCLASS_PINCTRL,
424 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
425 .post_bind = pinctrl_post_bind,
427 .flags = DM_UC_FLAG_SEQ_ALIAS,