1 // SPDX-License-Identifier: GPL-2.0+
8 #include <dm/device_compat.h>
9 #include <linux/libfdt.h>
10 #include <linux/err.h>
11 #include <linux/list.h>
14 #include <dm/pinctrl.h>
16 #include <dm/of_access.h>
18 DECLARE_GLOBAL_DATA_PTR;
20 #if CONFIG_IS_ENABLED(PINCTRL_FULL)
22 * pinctrl_config_one() - apply pinctrl settings for a single node
24 * @config: pin configuration node
25 * @return: 0 on success, or negative error code on failure
27 static int pinctrl_config_one(struct udevice *config)
29 struct udevice *pctldev;
30 const struct pinctrl_ops *ops;
34 pctldev = dev_get_parent(pctldev);
36 dev_err(config, "could not find pctldev\n");
39 if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
43 ops = pinctrl_get_ops(pctldev);
44 return ops->set_state(pctldev, config);
48 * pinctrl_select_state_full() - full implementation of pinctrl_select_state
50 * @dev: peripheral device
51 * @statename: state name, like "default"
52 * @return: 0 on success, or negative error code on failure
54 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
56 char propname[32]; /* long enough */
59 struct udevice *config;
60 int state, size, i, ret;
62 state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
66 * If statename is not found in "pinctrl-names",
67 * assume statename is just the integer state ID.
69 state = simple_strtoul(statename, &end, 10);
74 snprintf(propname, sizeof(propname), "pinctrl-%d", state);
75 list = dev_read_prop(dev, propname, &size);
79 size /= sizeof(*list);
80 for (i = 0; i < size; i++) {
81 phandle = fdt32_to_cpu(*list++);
82 ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
85 dev_warn(dev, "%s: uclass_get_device_by_phandle_id: err=%d\n",
90 ret = pinctrl_config_one(config);
92 dev_warn(dev, "%s: pinctrl_config_one: err=%d\n",
102 * pinconfig_post_bind() - post binding for PINCONFIG uclass
103 * Recursively bind its children as pinconfig devices.
105 * @dev: pinconfig device
106 * @return: 0 on success, or negative error code on failure
108 static int pinconfig_post_bind(struct udevice *dev)
110 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
115 if (!dev_of_valid(dev))
118 dev_for_each_subnode(node, dev) {
119 if (pre_reloc_only &&
120 !ofnode_pre_reloc(node))
123 * If this node has "compatible" property, this is not
124 * a pin configuration node, but a normal device. skip.
126 ofnode_get_property(node, "compatible", &ret);
129 /* If this node has "gpio-controller" property, skip */
130 if (ofnode_read_bool(node, "gpio-controller"))
133 if (ret != -FDT_ERR_NOTFOUND)
136 name = ofnode_get_name(node);
139 ret = device_bind_driver_to_node(dev, "pinconfig", name,
148 UCLASS_DRIVER(pinconfig) = {
149 .id = UCLASS_PINCONFIG,
150 #if CONFIG_IS_ENABLED(PINCONF_RECURSIVE)
151 .post_bind = pinconfig_post_bind,
156 U_BOOT_DRIVER(pinconfig_generic) = {
158 .id = UCLASS_PINCONFIG,
162 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
167 static int pinconfig_post_bind(struct udevice *dev)
174 pinctrl_gpio_get_pinctrl_and_offset(struct udevice *dev, unsigned offset,
175 struct udevice **pctldev,
176 unsigned int *pin_selector)
178 struct ofnode_phandle_args args;
179 unsigned gpio_offset, pfc_base, pfc_pins;
182 ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
185 dev_dbg(dev, "%s: dev_read_phandle_with_args: err=%d\n",
190 ret = uclass_get_device_by_ofnode(UCLASS_PINCTRL,
194 "%s: uclass_get_device_by_of_offset failed: err=%d\n",
199 gpio_offset = args.args[0];
200 pfc_base = args.args[1];
201 pfc_pins = args.args[2];
203 if (offset < gpio_offset || offset > gpio_offset + pfc_pins) {
205 "%s: GPIO can not be mapped to pincontrol pin\n",
210 offset -= gpio_offset;
212 *pin_selector = offset;
218 * pinctrl_gpio_request() - request a single pin to be used as GPIO
220 * @dev: GPIO peripheral device
221 * @offset: the GPIO pin offset from the GPIO controller
222 * @return: 0 on success, or negative error code on failure
224 int pinctrl_gpio_request(struct udevice *dev, unsigned offset)
226 const struct pinctrl_ops *ops;
227 struct udevice *pctldev;
228 unsigned int pin_selector;
231 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
232 &pctldev, &pin_selector);
236 ops = pinctrl_get_ops(pctldev);
237 if (!ops || !ops->gpio_request_enable)
240 return ops->gpio_request_enable(pctldev, pin_selector);
244 * pinctrl_gpio_free() - free a single pin used as GPIO
246 * @dev: GPIO peripheral device
247 * @offset: the GPIO pin offset from the GPIO controller
248 * @return: 0 on success, or negative error code on failure
250 int pinctrl_gpio_free(struct udevice *dev, unsigned offset)
252 const struct pinctrl_ops *ops;
253 struct udevice *pctldev;
254 unsigned int pin_selector;
257 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
258 &pctldev, &pin_selector);
262 ops = pinctrl_get_ops(pctldev);
263 if (!ops || !ops->gpio_disable_free)
266 return ops->gpio_disable_free(pctldev, pin_selector);
270 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
272 * @dev: peripheral device
273 * @return: 0 on success, or negative error code on failure
275 static int pinctrl_select_state_simple(struct udevice *dev)
277 struct udevice *pctldev;
278 struct pinctrl_ops *ops;
282 * For most system, there is only one pincontroller device. But in
283 * case of multiple pincontroller devices, probe the one with sequence
284 * number 0 (defined by alias) to avoid race condition.
286 ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev);
288 /* if not found, get the first one */
289 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
293 ops = pinctrl_get_ops(pctldev);
294 if (!ops->set_state_simple) {
295 dev_dbg(dev, "set_state_simple op missing\n");
299 return ops->set_state_simple(pctldev, dev);
302 int pinctrl_select_state(struct udevice *dev, const char *statename)
305 * Some device which is logical like mmc.blk, do not have
308 if (!ofnode_valid(dev->node))
311 * Try full-implemented pinctrl first.
312 * If it fails or is not implemented, try simple one.
314 if (pinctrl_select_state_full(dev, statename))
315 return pinctrl_select_state_simple(dev);
320 int pinctrl_request(struct udevice *dev, int func, int flags)
322 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
327 return ops->request(dev, func, flags);
330 int pinctrl_request_noflags(struct udevice *dev, int func)
332 return pinctrl_request(dev, func, 0);
335 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
337 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
339 if (!ops->get_periph_id)
342 return ops->get_periph_id(dev, periph);
345 int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
347 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
349 if (!ops->get_gpio_mux)
352 return ops->get_gpio_mux(dev, banknum, index);
355 int pinctrl_get_pins_count(struct udevice *dev)
357 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
359 if (!ops->get_pins_count)
362 return ops->get_pins_count(dev);
365 int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
368 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
370 if (!ops->get_pin_name)
373 snprintf(buf, size, ops->get_pin_name(dev, selector));
378 int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf,
381 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
383 if (!ops->get_pin_muxing)
386 return ops->get_pin_muxing(dev, selector, buf, size);
390 * pinconfig_post_bind() - post binding for PINCTRL uclass
391 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
393 * @dev: pinctrl device
394 * @return: 0 on success, or negative error code on failure
396 static int __maybe_unused pinctrl_post_bind(struct udevice *dev)
398 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
401 dev_dbg(dev, "ops is not set. Do not bind.\n");
406 * If set_state callback is set, we assume this pinctrl driver is the
407 * full implementation. In this case, its child nodes should be bound
408 * so that peripheral devices can easily search in parent devices
409 * during later DT-parsing.
412 return pinconfig_post_bind(dev);
417 UCLASS_DRIVER(pinctrl) = {
418 .id = UCLASS_PINCTRL,
419 .post_bind = pinctrl_post_bind,
420 .flags = DM_UC_FLAG_SEQ_ALIAS,