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,
97 ret = pinctrl_config_one(config);
106 * pinconfig_post_bind() - post binding for PINCONFIG uclass
107 * Recursively bind its children as pinconfig devices.
109 * @dev: pinconfig device
110 * @return: 0 on success, or negative error code on failure
112 static int pinconfig_post_bind(struct udevice *dev)
114 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
119 if (!dev_of_valid(dev))
122 dev_for_each_subnode(node, dev) {
123 if (pre_reloc_only &&
124 !ofnode_pre_reloc(node))
127 * If this node has "compatible" property, this is not
128 * a pin configuration node, but a normal device. skip.
130 ofnode_get_property(node, "compatible", &ret);
133 /* If this node has "gpio-controller" property, skip */
134 if (ofnode_read_bool(node, "gpio-controller"))
137 if (ret != -FDT_ERR_NOTFOUND)
140 name = ofnode_get_name(node);
143 ret = device_bind_driver_to_node(dev, "pinconfig", name,
152 UCLASS_DRIVER(pinconfig) = {
153 .id = UCLASS_PINCONFIG,
154 .post_bind = pinconfig_post_bind,
158 U_BOOT_DRIVER(pinconfig_generic) = {
160 .id = UCLASS_PINCONFIG,
164 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
169 static int pinconfig_post_bind(struct udevice *dev)
176 pinctrl_gpio_get_pinctrl_and_offset(struct udevice *dev, unsigned offset,
177 struct udevice **pctldev,
178 unsigned int *pin_selector)
180 struct ofnode_phandle_args args;
181 unsigned gpio_offset, pfc_base, pfc_pins;
184 ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
187 dev_dbg(dev, "%s: dev_read_phandle_with_args: err=%d\n",
192 ret = uclass_get_device_by_ofnode(UCLASS_PINCTRL,
196 "%s: uclass_get_device_by_of_offset failed: err=%d\n",
201 gpio_offset = args.args[0];
202 pfc_base = args.args[1];
203 pfc_pins = args.args[2];
205 if (offset < gpio_offset || offset > gpio_offset + pfc_pins) {
207 "%s: GPIO can not be mapped to pincontrol pin\n",
212 offset -= gpio_offset;
214 *pin_selector = offset;
220 * pinctrl_gpio_request() - request a single pin to be used as GPIO
222 * @dev: GPIO peripheral device
223 * @offset: the GPIO pin offset from the GPIO controller
224 * @return: 0 on success, or negative error code on failure
226 int pinctrl_gpio_request(struct udevice *dev, unsigned offset)
228 const struct pinctrl_ops *ops;
229 struct udevice *pctldev;
230 unsigned int pin_selector;
233 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
234 &pctldev, &pin_selector);
238 ops = pinctrl_get_ops(pctldev);
239 if (!ops || !ops->gpio_request_enable)
242 return ops->gpio_request_enable(pctldev, pin_selector);
246 * pinctrl_gpio_free() - free a single pin used as GPIO
248 * @dev: GPIO peripheral device
249 * @offset: the GPIO pin offset from the GPIO controller
250 * @return: 0 on success, or negative error code on failure
252 int pinctrl_gpio_free(struct udevice *dev, unsigned offset)
254 const struct pinctrl_ops *ops;
255 struct udevice *pctldev;
256 unsigned int pin_selector;
259 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
260 &pctldev, &pin_selector);
264 ops = pinctrl_get_ops(pctldev);
265 if (!ops || !ops->gpio_disable_free)
268 return ops->gpio_disable_free(pctldev, pin_selector);
272 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
274 * @dev: peripheral device
275 * @return: 0 on success, or negative error code on failure
277 static int pinctrl_select_state_simple(struct udevice *dev)
279 struct udevice *pctldev;
280 struct pinctrl_ops *ops;
284 * For most system, there is only one pincontroller device. But in
285 * case of multiple pincontroller devices, probe the one with sequence
286 * number 0 (defined by alias) to avoid race condition.
288 ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev);
290 /* if not found, get the first one */
291 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
295 ops = pinctrl_get_ops(pctldev);
296 if (!ops->set_state_simple) {
297 dev_dbg(dev, "set_state_simple op missing\n");
301 return ops->set_state_simple(pctldev, dev);
304 int pinctrl_select_state(struct udevice *dev, const char *statename)
307 * Some device which is logical like mmc.blk, do not have
310 if (!ofnode_valid(dev->node))
313 * Try full-implemented pinctrl first.
314 * If it fails or is not implemented, try simple one.
316 if (pinctrl_select_state_full(dev, statename))
317 return pinctrl_select_state_simple(dev);
322 int pinctrl_request(struct udevice *dev, int func, int flags)
324 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
329 return ops->request(dev, func, flags);
332 int pinctrl_request_noflags(struct udevice *dev, int func)
334 return pinctrl_request(dev, func, 0);
337 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
339 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
341 if (!ops->get_periph_id)
344 return ops->get_periph_id(dev, periph);
347 int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
349 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
351 if (!ops->get_gpio_mux)
354 return ops->get_gpio_mux(dev, banknum, index);
357 int pinctrl_get_pins_count(struct udevice *dev)
359 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
361 if (!ops->get_pins_count)
364 return ops->get_pins_count(dev);
367 int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
370 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
372 if (!ops->get_pin_name)
375 snprintf(buf, size, ops->get_pin_name(dev, selector));
380 int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf,
383 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
385 if (!ops->get_pin_muxing)
388 return ops->get_pin_muxing(dev, selector, buf, size);
392 * pinconfig_post_bind() - post binding for PINCTRL uclass
393 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
395 * @dev: pinctrl device
396 * @return: 0 on success, or negative error code on failure
398 static int pinctrl_post_bind(struct udevice *dev)
400 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
403 dev_dbg(dev, "ops is not set. Do not bind.\n");
408 * If set_state callback is set, we assume this pinctrl driver is the
409 * full implementation. In this case, its child nodes should be bound
410 * so that peripheral devices can easily search in parent devices
411 * during later DT-parsing.
414 return pinconfig_post_bind(dev);
419 UCLASS_DRIVER(pinctrl) = {
420 .id = UCLASS_PINCTRL,
421 .post_bind = pinctrl_post_bind,
422 .flags = DM_UC_FLAG_SEQ_ALIAS,