1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
8 #include <asm/global_data.h>
9 #include <dm/device_compat.h>
10 #include <linux/libfdt.h>
11 #include <linux/err.h>
12 #include <linux/list.h>
15 #include <dm/pinctrl.h>
17 #include <dm/of_access.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 #if CONFIG_IS_ENABLED(PINCTRL_FULL)
23 * pinctrl_config_one() - apply pinctrl settings for a single node
25 * @config: pin configuration node
26 * @return: 0 on success, or negative error code on failure
28 static int pinctrl_config_one(struct udevice *config)
30 struct udevice *pctldev;
31 const struct pinctrl_ops *ops;
35 pctldev = dev_get_parent(pctldev);
37 dev_err(config, "could not find pctldev\n");
40 if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
44 ops = pinctrl_get_ops(pctldev);
45 return ops->set_state(pctldev, config);
49 * pinctrl_select_state_full() - full implementation of pinctrl_select_state
51 * @dev: peripheral device
52 * @statename: state name, like "default"
53 * @return: 0 on success, or negative error code on failure
55 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
57 char propname[32]; /* long enough */
60 struct udevice *config;
61 int state, size, i, ret;
63 state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
67 * If statename is not found in "pinctrl-names",
68 * assume statename is just the integer state ID.
70 state = simple_strtoul(statename, &end, 10);
75 snprintf(propname, sizeof(propname), "pinctrl-%d", state);
76 list = dev_read_prop(dev, propname, &size);
80 size /= sizeof(*list);
81 for (i = 0; i < size; i++) {
82 phandle = fdt32_to_cpu(*list++);
83 ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
86 dev_warn(dev, "%s: uclass_get_device_by_phandle_id: err=%d\n",
91 ret = pinctrl_config_one(config);
93 dev_warn(dev, "%s: pinctrl_config_one: err=%d\n",
103 * pinconfig_post_bind() - post binding for PINCONFIG uclass
104 * Recursively bind its children as pinconfig devices.
106 * @dev: pinconfig device
107 * @return: 0 on success, or negative error code on failure
109 static int pinconfig_post_bind(struct udevice *dev)
111 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
116 if (!dev_has_ofnode(dev))
119 dev_for_each_subnode(node, dev) {
120 if (pre_reloc_only &&
121 !ofnode_pre_reloc(node))
124 * If this node has "compatible" property, this is not
125 * a pin configuration node, but a normal device. skip.
127 ofnode_get_property(node, "compatible", &ret);
130 /* If this node has "gpio-controller" property, skip */
131 if (ofnode_read_bool(node, "gpio-controller"))
134 if (ret != -FDT_ERR_NOTFOUND)
137 name = ofnode_get_name(node);
140 ret = device_bind_driver_to_node(dev, "pinconfig", name,
149 UCLASS_DRIVER(pinconfig) = {
150 .id = UCLASS_PINCONFIG,
151 #if CONFIG_IS_ENABLED(PINCONF_RECURSIVE)
152 .post_bind = pinconfig_post_bind,
157 U_BOOT_DRIVER(pinconfig_generic) = {
159 .id = UCLASS_PINCONFIG,
163 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
168 static int pinconfig_post_bind(struct udevice *dev)
175 pinctrl_gpio_get_pinctrl_and_offset(struct udevice *dev, unsigned offset,
176 struct udevice **pctldev,
177 unsigned int *pin_selector)
179 struct ofnode_phandle_args args;
180 unsigned gpio_offset, pfc_base, pfc_pins;
183 ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
186 dev_dbg(dev, "%s: dev_read_phandle_with_args: err=%d\n",
191 ret = uclass_get_device_by_ofnode(UCLASS_PINCTRL,
195 "%s: uclass_get_device_by_of_offset failed: err=%d\n",
200 gpio_offset = args.args[0];
201 pfc_base = args.args[1];
202 pfc_pins = args.args[2];
204 if (offset < gpio_offset || offset > gpio_offset + pfc_pins) {
206 "%s: GPIO can not be mapped to pincontrol pin\n",
211 offset -= gpio_offset;
213 *pin_selector = offset;
219 * pinctrl_gpio_request() - request a single pin to be used as GPIO
221 * @dev: GPIO peripheral device
222 * @offset: the GPIO pin offset from the GPIO controller
223 * @return: 0 on success, or negative error code on failure
225 int pinctrl_gpio_request(struct udevice *dev, unsigned offset)
227 const struct pinctrl_ops *ops;
228 struct udevice *pctldev;
229 unsigned int pin_selector;
232 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
233 &pctldev, &pin_selector);
237 ops = pinctrl_get_ops(pctldev);
238 if (!ops || !ops->gpio_request_enable)
241 return ops->gpio_request_enable(pctldev, pin_selector);
245 * pinctrl_gpio_free() - free a single pin used as GPIO
247 * @dev: GPIO peripheral device
248 * @offset: the GPIO pin offset from the GPIO controller
249 * @return: 0 on success, or negative error code on failure
251 int pinctrl_gpio_free(struct udevice *dev, unsigned offset)
253 const struct pinctrl_ops *ops;
254 struct udevice *pctldev;
255 unsigned int pin_selector;
258 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
259 &pctldev, &pin_selector);
263 ops = pinctrl_get_ops(pctldev);
264 if (!ops || !ops->gpio_disable_free)
267 return ops->gpio_disable_free(pctldev, pin_selector);
271 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
273 * @dev: peripheral device
274 * @return: 0 on success, or negative error code on failure
276 static int pinctrl_select_state_simple(struct udevice *dev)
278 struct udevice *pctldev;
279 struct pinctrl_ops *ops;
283 * For most system, there is only one pincontroller device. But in
284 * case of multiple pincontroller devices, probe the one with sequence
285 * number 0 (defined by alias) to avoid race condition.
287 ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev);
289 /* if not found, get the first one */
290 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
294 ops = pinctrl_get_ops(pctldev);
295 if (!ops->set_state_simple) {
296 dev_dbg(dev, "set_state_simple op missing\n");
300 return ops->set_state_simple(pctldev, dev);
303 int pinctrl_select_state(struct udevice *dev, const char *statename)
306 * Some device which is logical like mmc.blk, do not have
309 if (!dev_has_ofnode(dev))
312 * Try full-implemented pinctrl first.
313 * If it fails or is not implemented, try simple one.
315 if (pinctrl_select_state_full(dev, statename))
316 return pinctrl_select_state_simple(dev);
321 int pinctrl_request(struct udevice *dev, int func, int flags)
323 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
328 return ops->request(dev, func, flags);
331 int pinctrl_request_noflags(struct udevice *dev, int func)
333 return pinctrl_request(dev, func, 0);
336 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
338 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
340 if (!ops->get_periph_id)
343 return ops->get_periph_id(dev, periph);
346 int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
348 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
350 if (!ops->get_gpio_mux)
353 return ops->get_gpio_mux(dev, banknum, index);
356 int pinctrl_get_pins_count(struct udevice *dev)
358 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
360 if (!ops->get_pins_count)
363 return ops->get_pins_count(dev);
366 int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
369 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
371 if (!ops->get_pin_name)
374 snprintf(buf, size, ops->get_pin_name(dev, selector));
379 int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf,
382 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
384 if (!ops->get_pin_muxing)
387 return ops->get_pin_muxing(dev, selector, buf, size);
391 * pinconfig_post_bind() - post binding for PINCTRL uclass
392 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
394 * @dev: pinctrl device
395 * @return: 0 on success, or negative error code on failure
397 static int __maybe_unused pinctrl_post_bind(struct udevice *dev)
399 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
402 dev_dbg(dev, "ops is not set. Do not bind.\n");
407 * If set_state callback is set, we assume this pinctrl driver is the
408 * full implementation. In this case, its child nodes should be bound
409 * so that peripheral devices can easily search in parent devices
410 * during later DT-parsing.
413 return pinconfig_post_bind(dev);
418 UCLASS_DRIVER(pinctrl) = {
419 .id = UCLASS_PINCTRL,
420 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
421 .post_bind = pinctrl_post_bind,
423 .flags = DM_UC_FLAG_SEQ_ALIAS,