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 int pinctrl_decode_pin_config(const void *blob, int node)
22 if (fdtdec_get_bool(blob, node, "bias-pull-up"))
23 flags |= 1 << PIN_CONFIG_BIAS_PULL_UP;
24 else if (fdtdec_get_bool(blob, node, "bias-pull-down"))
25 flags |= 1 << PIN_CONFIG_BIAS_PULL_DOWN;
30 #if CONFIG_IS_ENABLED(PINCTRL_FULL)
32 * pinctrl_config_one() - apply pinctrl settings for a single node
34 * @config: pin configuration node
35 * @return: 0 on success, or negative error code on failure
37 static int pinctrl_config_one(struct udevice *config)
39 struct udevice *pctldev;
40 const struct pinctrl_ops *ops;
44 pctldev = dev_get_parent(pctldev);
46 dev_err(config, "could not find pctldev\n");
49 if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
53 ops = pinctrl_get_ops(pctldev);
54 return ops->set_state(pctldev, config);
58 * pinctrl_select_state_full() - full implementation of pinctrl_select_state
60 * @dev: peripheral device
61 * @statename: state name, like "default"
62 * @return: 0 on success, or negative error code on failure
64 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
66 char propname[32]; /* long enough */
69 struct udevice *config;
70 int state, size, i, ret;
72 state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
76 * If statename is not found in "pinctrl-names",
77 * assume statename is just the integer state ID.
79 state = simple_strtoul(statename, &end, 10);
84 snprintf(propname, sizeof(propname), "pinctrl-%d", state);
85 list = dev_read_prop(dev, propname, &size);
89 size /= sizeof(*list);
90 for (i = 0; i < size; i++) {
91 phandle = fdt32_to_cpu(*list++);
92 ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
97 ret = pinctrl_config_one(config);
106 * pinconfig_post_bind() - post binding for PINCONFIG uclass
107 * Recursively bind its children as pinconfig devices.
109 * @dev: pinconfig device
110 * @return: 0 on success, or negative error code on failure
112 static int pinconfig_post_bind(struct udevice *dev)
114 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
119 dev_for_each_subnode(node, dev) {
120 if (pre_reloc_only &&
121 !ofnode_pre_reloc(node))
124 * If this node has "compatible" property, this is not
125 * a pin configuration node, but a normal device. skip.
127 ofnode_get_property(node, "compatible", &ret);
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 .post_bind = pinconfig_post_bind,
152 U_BOOT_DRIVER(pinconfig_generic) = {
154 .id = UCLASS_PINCONFIG,
158 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
163 static int pinconfig_post_bind(struct udevice *dev)
170 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
172 * @dev: peripheral device
173 * @return: 0 on success, or negative error code on failure
175 static int pinctrl_select_state_simple(struct udevice *dev)
177 struct udevice *pctldev;
178 struct pinctrl_ops *ops;
182 * For simplicity, assume the first device of PINCTRL uclass
183 * is the correct one. This is most likely OK as there is
184 * usually only one pinctrl device on the system.
186 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
190 ops = pinctrl_get_ops(pctldev);
191 if (!ops->set_state_simple) {
192 dev_dbg(dev, "set_state_simple op missing\n");
196 return ops->set_state_simple(pctldev, dev);
199 int pinctrl_select_state(struct udevice *dev, const char *statename)
202 * Some device which is logical like mmc.blk, do not have
205 if (!ofnode_valid(dev->node))
208 * Try full-implemented pinctrl first.
209 * If it fails or is not implemented, try simple one.
211 if (pinctrl_select_state_full(dev, statename))
212 return pinctrl_select_state_simple(dev);
217 int pinctrl_request(struct udevice *dev, int func, int flags)
219 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
224 return ops->request(dev, func, flags);
227 int pinctrl_request_noflags(struct udevice *dev, int func)
229 return pinctrl_request(dev, func, 0);
232 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
234 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
236 if (!ops->get_periph_id)
239 return ops->get_periph_id(dev, periph);
242 int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
244 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
246 if (!ops->get_gpio_mux)
249 return ops->get_gpio_mux(dev, banknum, index);
253 * pinconfig_post_bind() - post binding for PINCTRL uclass
254 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
256 * @dev: pinctrl device
257 * @return: 0 on success, or negative error code on failure
259 static int pinctrl_post_bind(struct udevice *dev)
261 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
264 dev_dbg(dev, "ops is not set. Do not bind.\n");
269 * If set_state callback is set, we assume this pinctrl driver is the
270 * full implementation. In this case, its child nodes should be bound
271 * so that peripheral devices can easily search in parent devices
272 * during later DT-parsing.
275 return pinconfig_post_bind(dev);
280 UCLASS_DRIVER(pinctrl) = {
281 .id = UCLASS_PINCTRL,
282 .post_bind = pinctrl_post_bind,
283 .flags = DM_UC_FLAG_SEQ_ALIAS,