1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
6 #define LOG_CATEGORY UCLASS_PINCTRL
9 #include <asm/global_data.h>
10 #include <dm/device_compat.h>
11 #include <linux/libfdt.h>
12 #include <linux/err.h>
13 #include <linux/list.h>
16 #include <dm/pinctrl.h>
18 #include <dm/of_access.h>
20 DECLARE_GLOBAL_DATA_PTR;
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 = dectoul(statename, &end);
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",
102 static bool ofnode_pre_reloc_recursive(ofnode parent)
106 if (ofnode_pre_reloc(parent))
109 if (CONFIG_IS_ENABLED(PINCONF_RECURSIVE)) {
110 ofnode_for_each_subnode(child, parent)
111 if (ofnode_pre_reloc_recursive(child))
119 * pinconfig_post_bind() - post binding for PINCONFIG uclass
120 * Recursively bind its children as pinconfig devices.
122 * @dev: pinconfig device
123 * @return: 0 on success, or negative error code on failure
125 static int pinconfig_post_bind(struct udevice *dev)
127 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
132 if (!dev_has_ofnode(dev))
135 dev_for_each_subnode(node, dev) {
136 if (pre_reloc_only &&
137 !ofnode_pre_reloc_recursive(node))
140 * If this node has "compatible" property, this is not
141 * a pin configuration node, but a normal device. skip.
143 ofnode_get_property(node, "compatible", &ret);
146 /* If this node has "gpio-controller" property, skip */
147 if (ofnode_read_bool(node, "gpio-controller"))
150 if (ret != -FDT_ERR_NOTFOUND)
153 name = ofnode_get_name(node);
156 ret = device_bind_driver_to_node(dev, "pinconfig", name,
165 #if CONFIG_IS_ENABLED(PINCTRL_FULL)
166 UCLASS_DRIVER(pinconfig) = {
167 .id = UCLASS_PINCONFIG,
168 #if CONFIG_IS_ENABLED(PINCONF_RECURSIVE)
169 .post_bind = pinconfig_post_bind,
174 U_BOOT_DRIVER(pinconfig_generic) = {
176 .id = UCLASS_PINCONFIG,
181 pinctrl_gpio_get_pinctrl_and_offset(struct udevice *dev, unsigned offset,
182 struct udevice **pctldev,
183 unsigned int *pin_selector)
185 struct ofnode_phandle_args args;
186 unsigned gpio_offset, pfc_base, pfc_pins;
191 ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
194 dev_dbg(dev, "%s: dev_read_phandle_with_args: err=%d\n",
199 ret = uclass_get_device_by_ofnode(UCLASS_PINCTRL,
203 "%s: uclass_get_device_by_of_offset failed: err=%d\n",
208 gpio_offset = args.args[0];
209 pfc_base = args.args[1];
210 pfc_pins = args.args[2];
212 if (offset >= gpio_offset && offset < gpio_offset + pfc_pins)
216 offset -= gpio_offset;
218 *pin_selector = offset;
224 * pinctrl_gpio_request() - request a single pin to be used as GPIO
226 * @dev: GPIO peripheral device
227 * @offset: the GPIO pin offset from the GPIO controller
228 * @label: the GPIO pin label
229 * @return: 0 on success, or negative error code on failure
231 int pinctrl_gpio_request(struct udevice *dev, unsigned offset, const char *label)
233 const struct pinctrl_ops *ops;
234 struct udevice *pctldev;
235 unsigned int pin_selector;
238 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
239 &pctldev, &pin_selector);
243 ops = pinctrl_get_ops(pctldev);
245 if (!ops->gpio_request_enable)
248 return ops->gpio_request_enable(pctldev, pin_selector);
252 * pinctrl_gpio_free() - free a single pin used as GPIO
254 * @dev: GPIO peripheral device
255 * @offset: the GPIO pin offset from the GPIO controller
256 * @return: 0 on success, or negative error code on failure
258 int pinctrl_gpio_free(struct udevice *dev, unsigned offset)
260 const struct pinctrl_ops *ops;
261 struct udevice *pctldev;
262 unsigned int pin_selector;
265 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
266 &pctldev, &pin_selector);
270 ops = pinctrl_get_ops(pctldev);
272 if (!ops->gpio_disable_free)
275 return ops->gpio_disable_free(pctldev, pin_selector);
279 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
281 * @dev: peripheral device
282 * @return: 0 on success, or negative error code on failure
284 static int pinctrl_select_state_simple(struct udevice *dev)
286 struct udevice *pctldev;
287 struct pinctrl_ops *ops;
291 * For most system, there is only one pincontroller device. But in
292 * case of multiple pincontroller devices, probe the one with sequence
293 * number 0 (defined by alias) to avoid race condition.
295 ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev);
297 /* if not found, get the first one */
298 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
302 ops = pinctrl_get_ops(pctldev);
303 if (!ops->set_state_simple) {
304 dev_dbg(dev, "set_state_simple op missing\n");
308 return ops->set_state_simple(pctldev, dev);
311 int pinctrl_select_state(struct udevice *dev, const char *statename)
314 * Some device which is logical like mmc.blk, do not have
317 if (!dev_has_ofnode(dev))
320 * Try full-implemented pinctrl first.
321 * If it fails or is not implemented, try simple one.
323 if (CONFIG_IS_ENABLED(PINCTRL_FULL))
324 return pinctrl_select_state_full(dev, statename);
326 return pinctrl_select_state_simple(dev);
329 int pinctrl_request(struct udevice *dev, int func, int flags)
331 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
336 return ops->request(dev, func, flags);
339 int pinctrl_request_noflags(struct udevice *dev, int func)
341 return pinctrl_request(dev, func, 0);
344 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
346 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
348 if (!ops->get_periph_id)
351 return ops->get_periph_id(dev, periph);
354 int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
356 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
358 if (!ops->get_gpio_mux)
361 return ops->get_gpio_mux(dev, banknum, index);
364 int pinctrl_get_pins_count(struct udevice *dev)
366 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
368 if (!ops->get_pins_count)
371 return ops->get_pins_count(dev);
374 int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
377 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
379 if (!ops->get_pin_name)
382 snprintf(buf, size, ops->get_pin_name(dev, selector));
387 int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf,
390 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
392 if (!ops->get_pin_muxing)
395 return ops->get_pin_muxing(dev, selector, buf, size);
399 * pinctrl_post_bind() - post binding for PINCTRL uclass
400 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
402 * @dev: pinctrl device
403 * @return: 0 on success, or negative error code on failure
405 static int __maybe_unused pinctrl_post_bind(struct udevice *dev)
407 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
410 dev_dbg(dev, "ops is not set. Do not bind.\n");
415 * If the pinctrl driver has the full implementation, its child nodes
416 * should be bound so that peripheral devices can easily search in
417 * parent devices during later DT-parsing.
419 if (CONFIG_IS_ENABLED(PINCTRL_FULL))
420 return pinconfig_post_bind(dev);
425 UCLASS_DRIVER(pinctrl) = {
426 .id = UCLASS_PINCTRL,
427 #if CONFIG_IS_ENABLED(OF_REAL)
428 .post_bind = pinctrl_post_bind,
430 .flags = DM_UC_FLAG_SEQ_ALIAS,