2 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
4 * SPDX-License-Identifier: GPL-2.0+
10 #include <linux/list.h>
11 #include <dm/device.h>
13 #include <dm/pinctrl.h>
14 #include <dm/uclass.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 const void *fdt = gd->fdt_blob;
68 int node = dev_of_offset(dev);
69 char propname[32]; /* long enough */
73 struct udevice *config;
74 int state, size, i, ret;
76 state = fdt_stringlist_search(fdt, node, "pinctrl-names", statename);
80 * If statename is not found in "pinctrl-names",
81 * assume statename is just the integer state ID.
83 state = simple_strtoul(statename, &end, 10);
88 snprintf(propname, sizeof(propname), "pinctrl-%d", state);
89 list = fdt_getprop(fdt, node, propname, &size);
93 size /= sizeof(*list);
94 for (i = 0; i < size; i++) {
95 phandle = fdt32_to_cpu(*list++);
97 config_node = fdt_node_offset_by_phandle(fdt, phandle);
98 if (config_node < 0) {
99 dev_err(dev, "prop %s index %d invalid phandle\n",
103 ret = uclass_get_device_by_of_offset(UCLASS_PINCONFIG,
104 config_node, &config);
108 ret = pinctrl_config_one(config);
117 * pinconfig_post_bind() - post binding for PINCONFIG uclass
118 * Recursively bind its children as pinconfig devices.
120 * @dev: pinconfig device
121 * @return: 0 on success, or negative error code on failure
123 static int pinconfig_post_bind(struct udevice *dev)
125 const void *fdt = gd->fdt_blob;
126 int offset = dev_of_offset(dev);
127 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
131 for (offset = fdt_first_subnode(fdt, offset);
133 offset = fdt_next_subnode(fdt, offset)) {
134 if (pre_reloc_only &&
135 !dm_fdt_pre_reloc(fdt, offset))
138 * If this node has "compatible" property, this is not
139 * a pin configuration node, but a normal device. skip.
141 fdt_get_property(fdt, offset, "compatible", &ret);
145 if (ret != -FDT_ERR_NOTFOUND)
148 name = fdt_get_name(fdt, offset, NULL);
151 ret = device_bind_driver_to_node(dev, "pinconfig", name,
160 UCLASS_DRIVER(pinconfig) = {
161 .id = UCLASS_PINCONFIG,
162 .post_bind = pinconfig_post_bind,
166 U_BOOT_DRIVER(pinconfig_generic) = {
168 .id = UCLASS_PINCONFIG,
172 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
177 static int pinconfig_post_bind(struct udevice *dev)
184 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
186 * @dev: peripheral device
187 * @return: 0 on success, or negative error code on failure
189 static int pinctrl_select_state_simple(struct udevice *dev)
191 struct udevice *pctldev;
192 struct pinctrl_ops *ops;
196 * For simplicity, assume the first device of PINCTRL uclass
197 * is the correct one. This is most likely OK as there is
198 * usually only one pinctrl device on the system.
200 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
204 ops = pinctrl_get_ops(pctldev);
205 if (!ops->set_state_simple) {
206 dev_dbg(dev, "set_state_simple op missing\n");
210 return ops->set_state_simple(pctldev, dev);
213 int pinctrl_select_state(struct udevice *dev, const char *statename)
216 * Try full-implemented pinctrl first.
217 * If it fails or is not implemented, try simple one.
219 if (pinctrl_select_state_full(dev, statename))
220 return pinctrl_select_state_simple(dev);
225 int pinctrl_request(struct udevice *dev, int func, int flags)
227 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
232 return ops->request(dev, func, flags);
235 int pinctrl_request_noflags(struct udevice *dev, int func)
237 return pinctrl_request(dev, func, 0);
240 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
242 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
244 if (!ops->get_periph_id)
247 return ops->get_periph_id(dev, periph);
250 int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
252 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
254 if (!ops->get_gpio_mux)
257 return ops->get_gpio_mux(dev, banknum, index);
261 * pinconfig_post_bind() - post binding for PINCTRL uclass
262 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
264 * @dev: pinctrl device
265 * @return: 0 on success, or negative error code on failure
267 static int pinctrl_post_bind(struct udevice *dev)
269 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
272 dev_dbg(dev, "ops is not set. Do not bind.\n");
277 * If set_state callback is set, we assume this pinctrl driver is the
278 * full implementation. In this case, its child nodes should be bound
279 * so that peripheral devices can easily search in parent devices
280 * during later DT-parsing.
283 return pinconfig_post_bind(dev);
288 UCLASS_DRIVER(pinctrl) = {
289 .id = UCLASS_PINCTRL,
290 .post_bind = pinctrl_post_bind,
291 .flags = DM_UC_FLAG_SEQ_ALIAS,