1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
7 #include <linux/libfdt.h>
9 #include <linux/list.h>
12 #include <dm/pinctrl.h>
14 #include <dm/of_access.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 #if CONFIG_IS_ENABLED(PINCTRL_FULL)
20 * pinctrl_config_one() - apply pinctrl settings for a single node
22 * @config: pin configuration node
23 * @return: 0 on success, or negative error code on failure
25 static int pinctrl_config_one(struct udevice *config)
27 struct udevice *pctldev;
28 const struct pinctrl_ops *ops;
32 pctldev = dev_get_parent(pctldev);
34 dev_err(config, "could not find pctldev\n");
37 if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
41 ops = pinctrl_get_ops(pctldev);
42 return ops->set_state(pctldev, config);
46 * pinctrl_select_state_full() - full implementation of pinctrl_select_state
48 * @dev: peripheral device
49 * @statename: state name, like "default"
50 * @return: 0 on success, or negative error code on failure
52 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
54 char propname[32]; /* long enough */
57 struct udevice *config;
58 int state, size, i, ret;
60 state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
64 * If statename is not found in "pinctrl-names",
65 * assume statename is just the integer state ID.
67 state = simple_strtoul(statename, &end, 10);
72 snprintf(propname, sizeof(propname), "pinctrl-%d", state);
73 list = dev_read_prop(dev, propname, &size);
77 size /= sizeof(*list);
78 for (i = 0; i < size; i++) {
79 phandle = fdt32_to_cpu(*list++);
80 ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
83 dev_warn(dev, "%s: uclass_get_device_by_phandle_id: err=%d\n",
88 ret = pinctrl_config_one(config);
90 dev_warn(dev, "%s: pinctrl_config_one: err=%d\n",
100 * pinconfig_post_bind() - post binding for PINCONFIG uclass
101 * Recursively bind its children as pinconfig devices.
103 * @dev: pinconfig device
104 * @return: 0 on success, or negative error code on failure
106 static int pinconfig_post_bind(struct udevice *dev)
108 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
113 if (!dev_of_valid(dev))
116 dev_for_each_subnode(node, dev) {
117 if (pre_reloc_only &&
118 !ofnode_pre_reloc(node))
121 * If this node has "compatible" property, this is not
122 * a pin configuration node, but a normal device. skip.
124 ofnode_get_property(node, "compatible", &ret);
127 /* If this node has "gpio-controller" property, skip */
128 if (ofnode_read_bool(node, "gpio-controller"))
131 if (ret != -FDT_ERR_NOTFOUND)
134 name = ofnode_get_name(node);
137 ret = device_bind_driver_to_node(dev, "pinconfig", name,
146 UCLASS_DRIVER(pinconfig) = {
147 .id = UCLASS_PINCONFIG,
148 #if CONFIG_IS_ENABLED(PINCONF_RECURSIVE)
149 .post_bind = pinconfig_post_bind,
154 U_BOOT_DRIVER(pinconfig_generic) = {
156 .id = UCLASS_PINCONFIG,
160 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
165 static int pinconfig_post_bind(struct udevice *dev)
172 pinctrl_gpio_get_pinctrl_and_offset(struct udevice *dev, unsigned offset,
173 struct udevice **pctldev,
174 unsigned int *pin_selector)
176 struct ofnode_phandle_args args;
177 unsigned gpio_offset, pfc_base, pfc_pins;
180 ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
183 dev_dbg(dev, "%s: dev_read_phandle_with_args: err=%d\n",
188 ret = uclass_get_device_by_ofnode(UCLASS_PINCTRL,
192 "%s: uclass_get_device_by_of_offset failed: err=%d\n",
197 gpio_offset = args.args[0];
198 pfc_base = args.args[1];
199 pfc_pins = args.args[2];
201 if (offset < gpio_offset || offset > gpio_offset + pfc_pins) {
203 "%s: GPIO can not be mapped to pincontrol pin\n",
208 offset -= gpio_offset;
210 *pin_selector = offset;
216 * pinctrl_gpio_request() - request a single pin to be used as GPIO
218 * @dev: GPIO peripheral device
219 * @offset: the GPIO pin offset from the GPIO controller
220 * @return: 0 on success, or negative error code on failure
222 int pinctrl_gpio_request(struct udevice *dev, unsigned offset)
224 const struct pinctrl_ops *ops;
225 struct udevice *pctldev;
226 unsigned int pin_selector;
229 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
230 &pctldev, &pin_selector);
234 ops = pinctrl_get_ops(pctldev);
235 if (!ops || !ops->gpio_request_enable)
238 return ops->gpio_request_enable(pctldev, pin_selector);
242 * pinctrl_gpio_free() - free a single pin used as GPIO
244 * @dev: GPIO peripheral device
245 * @offset: the GPIO pin offset from the GPIO controller
246 * @return: 0 on success, or negative error code on failure
248 int pinctrl_gpio_free(struct udevice *dev, unsigned offset)
250 const struct pinctrl_ops *ops;
251 struct udevice *pctldev;
252 unsigned int pin_selector;
255 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
256 &pctldev, &pin_selector);
260 ops = pinctrl_get_ops(pctldev);
261 if (!ops || !ops->gpio_disable_free)
264 return ops->gpio_disable_free(pctldev, pin_selector);
268 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
270 * @dev: peripheral device
271 * @return: 0 on success, or negative error code on failure
273 static int pinctrl_select_state_simple(struct udevice *dev)
275 struct udevice *pctldev;
276 struct pinctrl_ops *ops;
280 * For most system, there is only one pincontroller device. But in
281 * case of multiple pincontroller devices, probe the one with sequence
282 * number 0 (defined by alias) to avoid race condition.
284 ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev);
286 /* if not found, get the first one */
287 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
291 ops = pinctrl_get_ops(pctldev);
292 if (!ops->set_state_simple) {
293 dev_dbg(dev, "set_state_simple op missing\n");
297 return ops->set_state_simple(pctldev, dev);
300 int pinctrl_select_state(struct udevice *dev, const char *statename)
303 * Some device which is logical like mmc.blk, do not have
306 if (!ofnode_valid(dev->node))
309 * Try full-implemented pinctrl first.
310 * If it fails or is not implemented, try simple one.
312 if (pinctrl_select_state_full(dev, statename))
313 return pinctrl_select_state_simple(dev);
318 int pinctrl_request(struct udevice *dev, int func, int flags)
320 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
325 return ops->request(dev, func, flags);
328 int pinctrl_request_noflags(struct udevice *dev, int func)
330 return pinctrl_request(dev, func, 0);
333 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
335 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
337 if (!ops->get_periph_id)
340 return ops->get_periph_id(dev, periph);
343 int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
345 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
347 if (!ops->get_gpio_mux)
350 return ops->get_gpio_mux(dev, banknum, index);
353 int pinctrl_get_pins_count(struct udevice *dev)
355 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
357 if (!ops->get_pins_count)
360 return ops->get_pins_count(dev);
363 int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
366 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
368 if (!ops->get_pin_name)
371 snprintf(buf, size, ops->get_pin_name(dev, selector));
376 int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf,
379 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
381 if (!ops->get_pin_muxing)
384 return ops->get_pin_muxing(dev, selector, buf, size);
388 * pinconfig_post_bind() - post binding for PINCTRL uclass
389 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
391 * @dev: pinctrl device
392 * @return: 0 on success, or negative error code on failure
394 static int __maybe_unused pinctrl_post_bind(struct udevice *dev)
396 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
399 dev_dbg(dev, "ops is not set. Do not bind.\n");
404 * If set_state callback is set, we assume this pinctrl driver is the
405 * full implementation. In this case, its child nodes should be bound
406 * so that peripheral devices can easily search in parent devices
407 * during later DT-parsing.
410 return pinconfig_post_bind(dev);
415 UCLASS_DRIVER(pinctrl) = {
416 .id = UCLASS_PINCTRL,
417 .post_bind = pinctrl_post_bind,
418 .flags = DM_UC_FLAG_SEQ_ALIAS,