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>
15 #include <dm/uclass.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 #if CONFIG_IS_ENABLED(PINCTRL_FULL)
21 * pinctrl_config_one() - apply pinctrl settings for a single node
23 * @config: pin configuration node
24 * @return: 0 on success, or negative error code on failure
26 static int pinctrl_config_one(struct udevice *config)
28 struct udevice *pctldev;
29 const struct pinctrl_ops *ops;
33 pctldev = dev_get_parent(pctldev);
35 dev_err(config, "could not find pctldev\n");
38 if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
42 ops = pinctrl_get_ops(pctldev);
43 return ops->set_state(pctldev, config);
47 * pinctrl_select_state_full() - full implementation of pinctrl_select_state
49 * @dev: peripheral device
50 * @statename: state name, like "default"
51 * @return: 0 on success, or negative error code on failure
53 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
55 const void *fdt = gd->fdt_blob;
56 int node = dev->of_offset;
57 char propname[32]; /* long enough */
61 struct udevice *config;
62 int state, size, i, ret;
64 state = fdt_find_string(fdt, node, "pinctrl-names", statename);
68 * If statename is not found in "pinctrl-names",
69 * assume statename is just the integer state ID.
71 state = simple_strtoul(statename, &end, 10);
76 snprintf(propname, sizeof(propname), "pinctrl-%d", state);
77 list = fdt_getprop(fdt, node, propname, &size);
81 size /= sizeof(*list);
82 for (i = 0; i < size; i++) {
83 phandle = fdt32_to_cpu(*list++);
85 config_node = fdt_node_offset_by_phandle(fdt, phandle);
86 if (config_node < 0) {
87 dev_err(dev, "prop %s index %d invalid phandle\n",
91 ret = uclass_get_device_by_of_offset(UCLASS_PINCONFIG,
92 config_node, &config);
96 ret = pinctrl_config_one(config);
105 * pinconfig_post-bind() - post binding for PINCONFIG uclass
106 * Recursively bind its children as pinconfig devices.
108 * @dev: pinconfig device
109 * @return: 0 on success, or negative error code on failure
111 static int pinconfig_post_bind(struct udevice *dev)
113 const void *fdt = gd->fdt_blob;
114 int offset = dev->of_offset;
118 for (offset = fdt_first_subnode(fdt, offset);
120 offset = fdt_next_subnode(fdt, offset)) {
122 * If this node has "compatible" property, this is not
123 * a pin configuration node, but a normal device. skip.
125 fdt_get_property(fdt, offset, "compatible", &ret);
129 if (ret != -FDT_ERR_NOTFOUND)
132 name = fdt_get_name(fdt, offset, NULL);
135 ret = device_bind_driver_to_node(dev, "pinconfig", name,
144 UCLASS_DRIVER(pinconfig) = {
145 .id = UCLASS_PINCONFIG,
146 .post_bind = pinconfig_post_bind,
150 U_BOOT_DRIVER(pinconfig_generic) = {
152 .id = UCLASS_PINCONFIG,
156 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
161 static int pinconfig_post_bind(struct udevice *dev)
163 /* Scan the bus for devices */
164 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
169 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
171 * @dev: peripheral device
172 * @return: 0 on success, or negative error code on failure
174 static int pinctrl_select_state_simple(struct udevice *dev)
176 struct udevice *pctldev;
177 struct pinctrl_ops *ops;
181 * For simplicity, assume the first device of PINCTRL uclass
182 * is the correct one. This is most likely OK as there is
183 * usually only one pinctrl device on the system.
185 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
189 ops = pinctrl_get_ops(pctldev);
190 if (!ops->set_state_simple) {
191 dev_dbg(dev, "set_state_simple op missing\n");
195 return ops->set_state_simple(pctldev, dev);
198 int pinctrl_select_state(struct udevice *dev, const char *statename)
201 * Try full-implemented pinctrl first.
202 * If it fails or is not implemented, try simple one.
204 if (pinctrl_select_state_full(dev, statename))
205 return pinctrl_select_state_simple(dev);
210 int pinctrl_request(struct udevice *dev, int func, int flags)
212 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
217 return ops->request(dev, func, flags);
220 int pinctrl_request_noflags(struct udevice *dev, int func)
222 return pinctrl_request(dev, func, 0);
225 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
227 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
229 if (!ops->get_periph_id)
232 return ops->get_periph_id(dev, periph);
236 * pinconfig_post-bind() - post binding for PINCTRL uclass
237 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
239 * @dev: pinctrl device
240 * @return: 0 on success, or negative error code on failure
242 static int pinctrl_post_bind(struct udevice *dev)
244 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
247 dev_dbg(dev, "ops is not set. Do not bind.\n");
252 * The pinctrl driver child nodes should be bound so that peripheral
253 * devices can easily search in parent devices during later DT-parsing.
255 return pinconfig_post_bind(dev);
258 UCLASS_DRIVER(pinctrl) = {
259 .id = UCLASS_PINCTRL,
260 .post_bind = pinctrl_post_bind,