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>
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 const void *fdt = gd->fdt_blob;
55 int node = dev->of_offset;
56 char propname[32]; /* long enough */
60 struct udevice *config;
61 int state, size, i, ret;
63 state = fdt_find_string(fdt, node, "pinctrl-names", statename);
67 * If statename is not found in "pinctrl-names",
68 * assume statename is just the integer state ID.
70 state = simple_strtoul(statename, &end, 10);
75 snprintf(propname, sizeof(propname), "pinctrl-%d", state);
76 list = fdt_getprop(fdt, node, propname, &size);
80 size /= sizeof(*list);
81 for (i = 0; i < size; i++) {
82 phandle = fdt32_to_cpu(*list++);
84 config_node = fdt_node_offset_by_phandle(fdt, phandle);
85 if (config_node < 0) {
86 dev_err(dev, "prop %s index %d invalid phandle\n",
90 ret = uclass_get_device_by_of_offset(UCLASS_PINCONFIG,
91 config_node, &config);
95 ret = pinctrl_config_one(config);
104 * pinconfig_post-bind() - post binding for PINCONFIG uclass
105 * Recursively bind its children as pinconfig devices.
107 * @dev: pinconfig device
108 * @return: 0 on success, or negative error code on failure
110 static int pinconfig_post_bind(struct udevice *dev)
112 const void *fdt = gd->fdt_blob;
113 int offset = dev->of_offset;
114 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
118 for (offset = fdt_first_subnode(fdt, offset);
120 offset = fdt_next_subnode(fdt, offset)) {
121 if (pre_reloc_only &&
122 !fdt_getprop(fdt, offset, "u-boot,dm-pre-reloc", NULL))
125 * If this node has "compatible" property, this is not
126 * a pin configuration node, but a normal device. skip.
128 fdt_get_property(fdt, offset, "compatible", &ret);
132 if (ret != -FDT_ERR_NOTFOUND)
135 name = fdt_get_name(fdt, offset, NULL);
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);
238 * pinconfig_post-bind() - post binding for PINCTRL uclass
239 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
241 * @dev: pinctrl device
242 * @return: 0 on success, or negative error code on failure
244 static int pinctrl_post_bind(struct udevice *dev)
246 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
249 dev_dbg(dev, "ops is not set. Do not bind.\n");
254 * If set_state callback is set, we assume this pinctrl driver is the
255 * full implementation. In this case, its child nodes should be bound
256 * so that peripheral devices can easily search in parent devices
257 * during later DT-parsing.
260 return pinconfig_post_bind(dev);
265 UCLASS_DRIVER(pinctrl) = {
266 .id = UCLASS_PINCTRL,
267 .post_bind = pinctrl_post_bind,