1 // SPDX-License-Identifier: GPL-2.0
3 * Driver core interface to the pinctrl subsystem.
5 * Copyright (C) 2012 ST-Ericsson SA
6 * Written on behalf of Linaro for ST-Ericsson
7 * Based on bits of regulator core, gpio core and clk core
9 * Author: Linus Walleij <linus.walleij@linaro.org>
11 * License terms: GNU General Public License (GPL) version 2
14 #include <linux/device.h>
15 #include <linux/pinctrl/devinfo.h>
16 #include <linux/pinctrl/consumer.h>
17 #include <linux/slab.h>
20 * pinctrl_bind_pins() - called by the device core before probe
21 * @dev: the device that is just about to probe
23 int pinctrl_bind_pins(struct device *dev)
27 if (dev->of_node_reused)
30 dev->pins = devm_kzalloc(dev, sizeof(*(dev->pins)), GFP_KERNEL);
34 dev->pins->p = devm_pinctrl_get(dev);
35 if (IS_ERR(dev->pins->p)) {
36 dev_dbg(dev, "no pinctrl handle\n");
37 ret = PTR_ERR(dev->pins->p);
41 dev->pins->default_state = pinctrl_lookup_state(dev->pins->p,
42 PINCTRL_STATE_DEFAULT);
43 if (IS_ERR(dev->pins->default_state)) {
44 dev_dbg(dev, "no default pinctrl state\n");
49 dev->pins->init_state = pinctrl_lookup_state(dev->pins->p,
51 if (IS_ERR(dev->pins->init_state)) {
52 /* Not supplying this state is perfectly legal */
53 dev_dbg(dev, "no init pinctrl state\n");
55 ret = pinctrl_select_state(dev->pins->p,
56 dev->pins->default_state);
58 ret = pinctrl_select_state(dev->pins->p, dev->pins->init_state);
62 dev_dbg(dev, "failed to activate initial pinctrl state\n");
68 * If power management is enabled, we also look for the optional
69 * sleep and idle pin states, with semantics as defined in
70 * <linux/pinctrl/pinctrl-state.h>
72 dev->pins->sleep_state = pinctrl_lookup_state(dev->pins->p,
74 if (IS_ERR(dev->pins->sleep_state))
75 /* Not supplying this state is perfectly legal */
76 dev_dbg(dev, "no sleep pinctrl state\n");
78 dev->pins->idle_state = pinctrl_lookup_state(dev->pins->p,
80 if (IS_ERR(dev->pins->idle_state))
81 /* Not supplying this state is perfectly legal */
82 dev_dbg(dev, "no idle pinctrl state\n");
88 * If no pinctrl handle or default state was found for this device,
89 * let's explicitly free the pin container in the device, there is
90 * no point in keeping it around.
93 devm_pinctrl_put(dev->pins->p);
95 devm_kfree(dev, dev->pins);
98 /* Return deferrals */
99 if (ret == -EPROBE_DEFER)
101 /* Return serious errors */
104 /* We ignore errors like -ENOENT meaning no pinctrl state */