2 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
4 * SPDX-License-Identifier: GPL-2.0+
8 #include <linux/libfdt.h>
10 #include <linux/list.h>
13 #include <dm/pinctrl.h>
15 #include <dm/of_access.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 int pinctrl_decode_pin_config(const void *blob, int node)
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;
31 #if CONFIG_IS_ENABLED(PINCTRL_FULL)
33 * pinctrl_config_one() - apply pinctrl settings for a single node
35 * @config: pin configuration node
36 * @return: 0 on success, or negative error code on failure
38 static int pinctrl_config_one(struct udevice *config)
40 struct udevice *pctldev;
41 const struct pinctrl_ops *ops;
45 pctldev = dev_get_parent(pctldev);
47 dev_err(config, "could not find pctldev\n");
50 if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
54 ops = pinctrl_get_ops(pctldev);
55 return ops->set_state(pctldev, config);
59 * pinctrl_select_state_full() - full implementation of pinctrl_select_state
61 * @dev: peripheral device
62 * @statename: state name, like "default"
63 * @return: 0 on success, or negative error code on failure
65 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
67 char propname[32]; /* long enough */
70 struct udevice *config;
71 int state, size, i, ret;
73 state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
77 * If statename is not found in "pinctrl-names",
78 * assume statename is just the integer state ID.
80 state = simple_strtoul(statename, &end, 10);
85 snprintf(propname, sizeof(propname), "pinctrl-%d", state);
86 list = dev_read_prop(dev, propname, &size);
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,
98 ret = pinctrl_config_one(config);
107 * pinconfig_post_bind() - post binding for PINCONFIG uclass
108 * Recursively bind its children as pinconfig devices.
110 * @dev: pinconfig device
111 * @return: 0 on success, or negative error code on failure
113 static int pinconfig_post_bind(struct udevice *dev)
115 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
120 dev_for_each_subnode(node, dev) {
121 if (pre_reloc_only &&
122 !ofnode_pre_reloc(node))
125 * If this node has "compatible" property, this is not
126 * a pin configuration node, but a normal device. skip.
128 ofnode_get_property(node, "compatible", &ret);
132 if (ret != -FDT_ERR_NOTFOUND)
135 name = ofnode_get_name(node);
138 ret = device_bind_driver_to_node(dev, "pinconfig", name,
147 UCLASS_DRIVER(pinconfig) = {
148 .id = UCLASS_PINCONFIG,
149 .post_bind = pinconfig_post_bind,
153 U_BOOT_DRIVER(pinconfig_generic) = {
155 .id = UCLASS_PINCONFIG,
159 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
164 static int pinconfig_post_bind(struct udevice *dev)
171 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
173 * @dev: peripheral device
174 * @return: 0 on success, or negative error code on failure
176 static int pinctrl_select_state_simple(struct udevice *dev)
178 struct udevice *pctldev;
179 struct pinctrl_ops *ops;
183 * For simplicity, assume the first device of PINCTRL uclass
184 * is the correct one. This is most likely OK as there is
185 * usually only one pinctrl device on the system.
187 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
191 ops = pinctrl_get_ops(pctldev);
192 if (!ops->set_state_simple) {
193 dev_dbg(dev, "set_state_simple op missing\n");
197 return ops->set_state_simple(pctldev, dev);
200 int pinctrl_select_state(struct udevice *dev, const char *statename)
203 * Try full-implemented pinctrl first.
204 * If it fails or is not implemented, try simple one.
206 if (pinctrl_select_state_full(dev, statename))
207 return pinctrl_select_state_simple(dev);
212 int pinctrl_request(struct udevice *dev, int func, int flags)
214 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
219 return ops->request(dev, func, flags);
222 int pinctrl_request_noflags(struct udevice *dev, int func)
224 return pinctrl_request(dev, func, 0);
227 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
229 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
231 if (!ops->get_periph_id)
234 return ops->get_periph_id(dev, periph);
237 int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
239 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
241 if (!ops->get_gpio_mux)
244 return ops->get_gpio_mux(dev, banknum, index);
248 * pinconfig_post_bind() - post binding for PINCTRL uclass
249 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
251 * @dev: pinctrl device
252 * @return: 0 on success, or negative error code on failure
254 static int pinctrl_post_bind(struct udevice *dev)
256 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
259 dev_dbg(dev, "ops is not set. Do not bind.\n");
264 * If set_state callback is set, we assume this pinctrl driver is the
265 * full implementation. In this case, its child nodes should be bound
266 * so that peripheral devices can easily search in parent devices
267 * during later DT-parsing.
270 return pinconfig_post_bind(dev);
275 UCLASS_DRIVER(pinctrl) = {
276 .id = UCLASS_PINCTRL,
277 .post_bind = pinctrl_post_bind,
278 .flags = DM_UC_FLAG_SEQ_ALIAS,