1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
6 #define LOG_CATEGORY UCLASS_PINCTRL
10 #include <asm/global_data.h>
11 #include <dm/device_compat.h>
12 #include <linux/libfdt.h>
13 #include <linux/err.h>
14 #include <linux/list.h>
17 #include <dm/pinctrl.h>
19 #include <dm/of_access.h>
21 DECLARE_GLOBAL_DATA_PTR;
24 * pinctrl_config_one() - apply pinctrl settings for a single node
26 * @config: pin configuration node
27 * @return: 0 on success, or negative error code on failure
29 static int pinctrl_config_one(struct udevice *config)
31 struct udevice *pctldev;
32 const struct pinctrl_ops *ops;
36 pctldev = dev_get_parent(pctldev);
38 dev_err(config, "could not find pctldev\n");
41 if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
45 ops = pinctrl_get_ops(pctldev);
46 return ops->set_state(pctldev, config);
50 * pinctrl_select_state_full() - full implementation of pinctrl_select_state
52 * @dev: peripheral device
53 * @statename: state name, like "default"
54 * @return: 0 on success, or negative error code on failure
56 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
58 char propname[32]; /* long enough */
61 struct udevice *config;
62 int state, size, i, ret;
64 state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
68 * If statename is not found in "pinctrl-names",
69 * assume statename is just the integer state ID.
71 state = dectoul(statename, &end);
76 snprintf(propname, sizeof(propname), "pinctrl-%d", state);
77 list = dev_read_prop(dev, propname, &size);
81 size /= sizeof(*list);
82 for (i = 0; i < size; i++) {
83 phandle = fdt32_to_cpu(*list++);
84 ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
87 dev_warn(dev, "%s: uclass_get_device_by_phandle_id: err=%d\n",
92 ret = pinctrl_config_one(config);
94 dev_warn(dev, "%s: pinctrl_config_one: err=%d\n",
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 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
117 if (!dev_has_ofnode(dev))
120 dev_for_each_subnode(node, dev) {
121 if (pre_reloc_only &&
122 !ofnode_pre_reloc(node))
125 * If this node has "compatible" property, this is not
126 * a pin configuration node, but a normal device. skip.
128 ofnode_get_property(node, "compatible", &ret);
131 /* If this node has "gpio-controller" property, skip */
132 if (ofnode_read_bool(node, "gpio-controller"))
135 if (ret != -FDT_ERR_NOTFOUND)
138 name = ofnode_get_name(node);
141 ret = device_bind_driver_to_node(dev, "pinconfig", name,
150 #if CONFIG_IS_ENABLED(PINCTRL_FULL)
151 UCLASS_DRIVER(pinconfig) = {
152 .id = UCLASS_PINCONFIG,
153 #if CONFIG_IS_ENABLED(PINCONF_RECURSIVE)
154 .post_bind = pinconfig_post_bind,
159 U_BOOT_DRIVER(pinconfig_generic) = {
161 .id = UCLASS_PINCONFIG,
166 pinctrl_gpio_get_pinctrl_and_offset(struct udevice *dev, unsigned offset,
167 struct udevice **pctldev,
168 unsigned int *pin_selector)
170 struct ofnode_phandle_args args;
171 unsigned gpio_offset, pfc_base, pfc_pins;
174 ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
177 dev_dbg(dev, "%s: dev_read_phandle_with_args: err=%d\n",
182 ret = uclass_get_device_by_ofnode(UCLASS_PINCTRL,
186 "%s: uclass_get_device_by_of_offset failed: err=%d\n",
191 gpio_offset = args.args[0];
192 pfc_base = args.args[1];
193 pfc_pins = args.args[2];
195 if (offset < gpio_offset || offset > gpio_offset + pfc_pins) {
197 "%s: GPIO can not be mapped to pincontrol pin\n",
202 offset -= gpio_offset;
204 *pin_selector = offset;
210 * pinctrl_gpio_request() - request a single pin to be used as GPIO
212 * @dev: GPIO peripheral device
213 * @offset: the GPIO pin offset from the GPIO controller
214 * @label: the GPIO pin label
215 * @return: 0 on success, or negative error code on failure
217 int pinctrl_gpio_request(struct udevice *dev, unsigned offset, const char *label)
219 const struct pinctrl_ops *ops;
220 struct udevice *pctldev;
221 unsigned int pin_selector;
224 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
225 &pctldev, &pin_selector);
229 ops = pinctrl_get_ops(pctldev);
231 if (!ops->gpio_request_enable)
234 return ops->gpio_request_enable(pctldev, pin_selector);
238 * pinctrl_gpio_free() - free a single pin used as GPIO
240 * @dev: GPIO peripheral device
241 * @offset: the GPIO pin offset from the GPIO controller
242 * @return: 0 on success, or negative error code on failure
244 int pinctrl_gpio_free(struct udevice *dev, unsigned offset)
246 const struct pinctrl_ops *ops;
247 struct udevice *pctldev;
248 unsigned int pin_selector;
251 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
252 &pctldev, &pin_selector);
256 ops = pinctrl_get_ops(pctldev);
258 if (!ops->gpio_disable_free)
261 return ops->gpio_disable_free(pctldev, pin_selector);
265 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
267 * @dev: peripheral device
268 * @return: 0 on success, or negative error code on failure
270 static int pinctrl_select_state_simple(struct udevice *dev)
272 struct udevice *pctldev;
273 struct pinctrl_ops *ops;
277 * For most system, there is only one pincontroller device. But in
278 * case of multiple pincontroller devices, probe the one with sequence
279 * number 0 (defined by alias) to avoid race condition.
281 ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev);
283 /* if not found, get the first one */
284 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
288 ops = pinctrl_get_ops(pctldev);
289 if (!ops->set_state_simple) {
290 dev_dbg(dev, "set_state_simple op missing\n");
294 return ops->set_state_simple(pctldev, dev);
297 int pinctrl_select_state(struct udevice *dev, const char *statename)
300 * Some device which is logical like mmc.blk, do not have
303 if (!dev_has_ofnode(dev))
306 * Try full-implemented pinctrl first.
307 * If it fails or is not implemented, try simple one.
309 if (CONFIG_IS_ENABLED(PINCTRL_FULL))
310 return pinctrl_select_state_full(dev, statename);
312 return pinctrl_select_state_simple(dev);
315 int pinctrl_request(struct udevice *dev, int func, int flags)
317 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
322 return ops->request(dev, func, flags);
325 int pinctrl_request_noflags(struct udevice *dev, int func)
327 return pinctrl_request(dev, func, 0);
330 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
332 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
334 if (!ops->get_periph_id)
337 return ops->get_periph_id(dev, periph);
340 int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
342 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
344 if (!ops->get_gpio_mux)
347 return ops->get_gpio_mux(dev, banknum, index);
350 int pinctrl_get_pins_count(struct udevice *dev)
352 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
354 if (!ops->get_pins_count)
357 return ops->get_pins_count(dev);
360 int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
363 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
365 if (!ops->get_pin_name)
368 snprintf(buf, size, ops->get_pin_name(dev, selector));
373 int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf,
376 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
378 if (!ops->get_pin_muxing)
381 return ops->get_pin_muxing(dev, selector, buf, size);
385 * pinctrl_post_bind() - post binding for PINCTRL uclass
386 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
388 * @dev: pinctrl device
389 * @return: 0 on success, or negative error code on failure
391 static int __maybe_unused pinctrl_post_bind(struct udevice *dev)
393 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
396 dev_dbg(dev, "ops is not set. Do not bind.\n");
401 * If the pinctrl driver has the full implementation, its child nodes
402 * should be bound so that peripheral devices can easily search in
403 * parent devices during later DT-parsing.
405 if (CONFIG_IS_ENABLED(PINCTRL_FULL))
406 return pinconfig_post_bind(dev);
411 UCLASS_DRIVER(pinctrl) = {
412 .id = UCLASS_PINCTRL,
413 #if CONFIG_IS_ENABLED(OF_REAL)
414 .post_bind = pinctrl_post_bind,
416 .flags = DM_UC_FLAG_SEQ_ALIAS,