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;
31 * TODO: this function is temporary for v2019.01.
32 * It should be renamed to pinctrl_decode_pin_config(),
33 * the original pinctrl_decode_pin_config() function should
34 * be removed and all callers of the original function should
35 * be migrated to use the new one.
37 int pinctrl_decode_pin_config_dm(struct udevice *dev)
41 if (dev->uclass->uc_drv->id != UCLASS_PINCONFIG)
44 if (dev_read_bool(dev, "bias-pull-up"))
45 pinconfig |= 1 << PIN_CONFIG_BIAS_PULL_UP;
46 else if (dev_read_bool(dev, "bias-pull-down"))
47 pinconfig |= 1 << PIN_CONFIG_BIAS_PULL_DOWN;
52 #if CONFIG_IS_ENABLED(PINCTRL_FULL)
54 * pinctrl_config_one() - apply pinctrl settings for a single node
56 * @config: pin configuration node
57 * @return: 0 on success, or negative error code on failure
59 static int pinctrl_config_one(struct udevice *config)
61 struct udevice *pctldev;
62 const struct pinctrl_ops *ops;
66 pctldev = dev_get_parent(pctldev);
68 dev_err(config, "could not find pctldev\n");
71 if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
75 ops = pinctrl_get_ops(pctldev);
76 return ops->set_state(pctldev, config);
80 * pinctrl_select_state_full() - full implementation of pinctrl_select_state
82 * @dev: peripheral device
83 * @statename: state name, like "default"
84 * @return: 0 on success, or negative error code on failure
86 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
88 char propname[32]; /* long enough */
91 struct udevice *config;
92 int state, size, i, ret;
94 state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
98 * If statename is not found in "pinctrl-names",
99 * assume statename is just the integer state ID.
101 state = simple_strtoul(statename, &end, 10);
106 snprintf(propname, sizeof(propname), "pinctrl-%d", state);
107 list = dev_read_prop(dev, propname, &size);
111 size /= sizeof(*list);
112 for (i = 0; i < size; i++) {
113 phandle = fdt32_to_cpu(*list++);
114 ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
119 ret = pinctrl_config_one(config);
128 * pinconfig_post_bind() - post binding for PINCONFIG uclass
129 * Recursively bind its children as pinconfig devices.
131 * @dev: pinconfig device
132 * @return: 0 on success, or negative error code on failure
134 static int pinconfig_post_bind(struct udevice *dev)
136 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
141 dev_for_each_subnode(node, dev) {
142 if (pre_reloc_only &&
143 !ofnode_pre_reloc(node))
146 * If this node has "compatible" property, this is not
147 * a pin configuration node, but a normal device. skip.
149 ofnode_get_property(node, "compatible", &ret);
153 if (ret != -FDT_ERR_NOTFOUND)
156 name = ofnode_get_name(node);
159 ret = device_bind_driver_to_node(dev, "pinconfig", name,
168 UCLASS_DRIVER(pinconfig) = {
169 .id = UCLASS_PINCONFIG,
170 .post_bind = pinconfig_post_bind,
174 U_BOOT_DRIVER(pinconfig_generic) = {
176 .id = UCLASS_PINCONFIG,
180 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
185 static int pinconfig_post_bind(struct udevice *dev)
192 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
194 * @dev: peripheral device
195 * @return: 0 on success, or negative error code on failure
197 static int pinctrl_select_state_simple(struct udevice *dev)
199 struct udevice *pctldev;
200 struct pinctrl_ops *ops;
204 * For simplicity, assume the first device of PINCTRL uclass
205 * is the correct one. This is most likely OK as there is
206 * usually only one pinctrl device on the system.
208 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
212 ops = pinctrl_get_ops(pctldev);
213 if (!ops->set_state_simple) {
214 dev_dbg(dev, "set_state_simple op missing\n");
218 return ops->set_state_simple(pctldev, dev);
221 int pinctrl_select_state(struct udevice *dev, const char *statename)
224 * Some device which is logical like mmc.blk, do not have
227 if (!ofnode_valid(dev->node))
230 * Try full-implemented pinctrl first.
231 * If it fails or is not implemented, try simple one.
233 if (pinctrl_select_state_full(dev, statename))
234 return pinctrl_select_state_simple(dev);
239 int pinctrl_request(struct udevice *dev, int func, int flags)
241 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
246 return ops->request(dev, func, flags);
249 int pinctrl_request_noflags(struct udevice *dev, int func)
251 return pinctrl_request(dev, func, 0);
254 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
256 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
258 if (!ops->get_periph_id)
261 return ops->get_periph_id(dev, periph);
264 int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
266 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
268 if (!ops->get_gpio_mux)
271 return ops->get_gpio_mux(dev, banknum, index);
274 int pinctrl_get_pins_count(struct udevice *dev)
276 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
278 if (!ops->get_pins_count)
281 return ops->get_pins_count(dev);
284 int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
287 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
289 if (!ops->get_pin_name)
292 snprintf(buf, size, ops->get_pin_name(dev, selector));
297 int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf,
300 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
302 if (!ops->get_pin_muxing)
305 return ops->get_pin_muxing(dev, selector, buf, size);
309 * pinconfig_post_bind() - post binding for PINCTRL uclass
310 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
312 * @dev: pinctrl device
313 * @return: 0 on success, or negative error code on failure
315 static int pinctrl_post_bind(struct udevice *dev)
317 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
320 dev_dbg(dev, "ops is not set. Do not bind.\n");
325 * If set_state callback is set, we assume this pinctrl driver is the
326 * full implementation. In this case, its child nodes should be bound
327 * so that peripheral devices can easily search in parent devices
328 * during later DT-parsing.
331 return pinconfig_post_bind(dev);
336 UCLASS_DRIVER(pinctrl) = {
337 .id = UCLASS_PINCTRL,
338 .post_bind = pinctrl_post_bind,
339 .flags = DM_UC_FLAG_SEQ_ALIAS,