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;
30 #if CONFIG_IS_ENABLED(PINCTRL_FULL)
32 * pinctrl_config_one() - apply pinctrl settings for a single node
34 * @config: pin configuration node
35 * @return: 0 on success, or negative error code on failure
37 static int pinctrl_config_one(struct udevice *config)
39 struct udevice *pctldev;
40 const struct pinctrl_ops *ops;
44 pctldev = dev_get_parent(pctldev);
46 dev_err(config, "could not find pctldev\n");
49 if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
53 ops = pinctrl_get_ops(pctldev);
54 return ops->set_state(pctldev, config);
58 * pinctrl_select_state_full() - full implementation of pinctrl_select_state
60 * @dev: peripheral device
61 * @statename: state name, like "default"
62 * @return: 0 on success, or negative error code on failure
64 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
66 char propname[32]; /* long enough */
69 struct udevice *config;
70 int state, size, i, ret;
72 state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
76 * If statename is not found in "pinctrl-names",
77 * assume statename is just the integer state ID.
79 state = simple_strtoul(statename, &end, 10);
84 snprintf(propname, sizeof(propname), "pinctrl-%d", state);
85 list = dev_read_prop(dev, propname, &size);
89 size /= sizeof(*list);
90 for (i = 0; i < size; i++) {
91 phandle = fdt32_to_cpu(*list++);
92 ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
95 dev_warn(dev, "%s: uclass_get_device_by_phandle_id: err=%d\n",
100 ret = pinctrl_config_one(config);
102 dev_warn(dev, "%s: pinctrl_config_one: err=%d\n",
112 * pinconfig_post_bind() - post binding for PINCONFIG uclass
113 * Recursively bind its children as pinconfig devices.
115 * @dev: pinconfig device
116 * @return: 0 on success, or negative error code on failure
118 static int pinconfig_post_bind(struct udevice *dev)
120 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
125 if (!dev_of_valid(dev))
128 dev_for_each_subnode(node, dev) {
129 if (pre_reloc_only &&
130 !ofnode_pre_reloc(node))
133 * If this node has "compatible" property, this is not
134 * a pin configuration node, but a normal device. skip.
136 ofnode_get_property(node, "compatible", &ret);
139 /* If this node has "gpio-controller" property, skip */
140 if (ofnode_read_bool(node, "gpio-controller"))
143 if (ret != -FDT_ERR_NOTFOUND)
146 name = ofnode_get_name(node);
149 ret = device_bind_driver_to_node(dev, "pinconfig", name,
158 UCLASS_DRIVER(pinconfig) = {
159 .id = UCLASS_PINCONFIG,
160 #if CONFIG_IS_ENABLED(PINCONFIG_RECURSIVE)
161 .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_gpio_get_pinctrl_and_offset(struct udevice *dev, unsigned offset,
185 struct udevice **pctldev,
186 unsigned int *pin_selector)
188 struct ofnode_phandle_args args;
189 unsigned gpio_offset, pfc_base, pfc_pins;
192 ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
195 dev_dbg(dev, "%s: dev_read_phandle_with_args: err=%d\n",
200 ret = uclass_get_device_by_ofnode(UCLASS_PINCTRL,
204 "%s: uclass_get_device_by_of_offset failed: err=%d\n",
209 gpio_offset = args.args[0];
210 pfc_base = args.args[1];
211 pfc_pins = args.args[2];
213 if (offset < gpio_offset || offset > gpio_offset + pfc_pins) {
215 "%s: GPIO can not be mapped to pincontrol pin\n",
220 offset -= gpio_offset;
222 *pin_selector = offset;
228 * pinctrl_gpio_request() - request a single pin to be used as GPIO
230 * @dev: GPIO peripheral device
231 * @offset: the GPIO pin offset from the GPIO controller
232 * @return: 0 on success, or negative error code on failure
234 int pinctrl_gpio_request(struct udevice *dev, unsigned offset)
236 const struct pinctrl_ops *ops;
237 struct udevice *pctldev;
238 unsigned int pin_selector;
241 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
242 &pctldev, &pin_selector);
246 ops = pinctrl_get_ops(pctldev);
247 if (!ops || !ops->gpio_request_enable)
250 return ops->gpio_request_enable(pctldev, pin_selector);
254 * pinctrl_gpio_free() - free a single pin used as GPIO
256 * @dev: GPIO peripheral device
257 * @offset: the GPIO pin offset from the GPIO controller
258 * @return: 0 on success, or negative error code on failure
260 int pinctrl_gpio_free(struct udevice *dev, unsigned offset)
262 const struct pinctrl_ops *ops;
263 struct udevice *pctldev;
264 unsigned int pin_selector;
267 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
268 &pctldev, &pin_selector);
272 ops = pinctrl_get_ops(pctldev);
273 if (!ops || !ops->gpio_disable_free)
276 return ops->gpio_disable_free(pctldev, pin_selector);
280 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
282 * @dev: peripheral device
283 * @return: 0 on success, or negative error code on failure
285 static int pinctrl_select_state_simple(struct udevice *dev)
287 struct udevice *pctldev;
288 struct pinctrl_ops *ops;
292 * For most system, there is only one pincontroller device. But in
293 * case of multiple pincontroller devices, probe the one with sequence
294 * number 0 (defined by alias) to avoid race condition.
296 ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev);
298 /* if not found, get the first one */
299 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
303 ops = pinctrl_get_ops(pctldev);
304 if (!ops->set_state_simple) {
305 dev_dbg(dev, "set_state_simple op missing\n");
309 return ops->set_state_simple(pctldev, dev);
312 int pinctrl_select_state(struct udevice *dev, const char *statename)
315 * Some device which is logical like mmc.blk, do not have
318 if (!ofnode_valid(dev->node))
321 * Try full-implemented pinctrl first.
322 * If it fails or is not implemented, try simple one.
324 if (pinctrl_select_state_full(dev, statename))
325 return pinctrl_select_state_simple(dev);
330 int pinctrl_request(struct udevice *dev, int func, int flags)
332 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
337 return ops->request(dev, func, flags);
340 int pinctrl_request_noflags(struct udevice *dev, int func)
342 return pinctrl_request(dev, func, 0);
345 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
347 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
349 if (!ops->get_periph_id)
352 return ops->get_periph_id(dev, periph);
355 int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
357 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
359 if (!ops->get_gpio_mux)
362 return ops->get_gpio_mux(dev, banknum, index);
365 int pinctrl_get_pins_count(struct udevice *dev)
367 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
369 if (!ops->get_pins_count)
372 return ops->get_pins_count(dev);
375 int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
378 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
380 if (!ops->get_pin_name)
383 snprintf(buf, size, ops->get_pin_name(dev, selector));
388 int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf,
391 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
393 if (!ops->get_pin_muxing)
396 return ops->get_pin_muxing(dev, selector, buf, size);
400 * pinconfig_post_bind() - post binding for PINCTRL uclass
401 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
403 * @dev: pinctrl device
404 * @return: 0 on success, or negative error code on failure
406 static int __maybe_unused pinctrl_post_bind(struct udevice *dev)
408 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
411 dev_dbg(dev, "ops is not set. Do not bind.\n");
416 * If set_state callback is set, we assume this pinctrl driver is the
417 * full implementation. In this case, its child nodes should be bound
418 * so that peripheral devices can easily search in parent devices
419 * during later DT-parsing.
422 return pinconfig_post_bind(dev);
427 UCLASS_DRIVER(pinctrl) = {
428 .id = UCLASS_PINCTRL,
429 #if CONFIG_IS_ENABLED(PINCONF_RECURSIVE)
430 .post_bind = pinctrl_post_bind,
432 .flags = DM_UC_FLAG_SEQ_ALIAS,