1*989d42e8SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2ab78029eSLinus Walleij /*
3ab78029eSLinus Walleij * Driver core interface to the pinctrl subsystem.
4ab78029eSLinus Walleij *
5ab78029eSLinus Walleij * Copyright (C) 2012 ST-Ericsson SA
6ab78029eSLinus Walleij * Written on behalf of Linaro for ST-Ericsson
7ab78029eSLinus Walleij * Based on bits of regulator core, gpio core and clk core
8ab78029eSLinus Walleij *
9ab78029eSLinus Walleij * Author: Linus Walleij <linus.walleij@linaro.org>
10ab78029eSLinus Walleij */
11ab78029eSLinus Walleij
12ab78029eSLinus Walleij #include <linux/device.h>
13ab78029eSLinus Walleij #include <linux/pinctrl/devinfo.h>
14ab78029eSLinus Walleij #include <linux/pinctrl/consumer.h>
15ab78029eSLinus Walleij #include <linux/slab.h>
16ab78029eSLinus Walleij
17ab78029eSLinus Walleij /**
18ab78029eSLinus Walleij * pinctrl_bind_pins() - called by the device core before probe
19ab78029eSLinus Walleij * @dev: the device that is just about to probe
20ab78029eSLinus Walleij */
pinctrl_bind_pins(struct device * dev)21ab78029eSLinus Walleij int pinctrl_bind_pins(struct device *dev)
22ab78029eSLinus Walleij {
23ab78029eSLinus Walleij int ret;
24ab78029eSLinus Walleij
25ed032c1bSJohan Hovold if (dev->of_node_reused)
26ed032c1bSJohan Hovold return 0;
27ed032c1bSJohan Hovold
28ab78029eSLinus Walleij dev->pins = devm_kzalloc(dev, sizeof(*(dev->pins)), GFP_KERNEL);
29ab78029eSLinus Walleij if (!dev->pins)
30ab78029eSLinus Walleij return -ENOMEM;
31ab78029eSLinus Walleij
32ab78029eSLinus Walleij dev->pins->p = devm_pinctrl_get(dev);
33ab78029eSLinus Walleij if (IS_ERR(dev->pins->p)) {
34ab78029eSLinus Walleij dev_dbg(dev, "no pinctrl handle\n");
35ab78029eSLinus Walleij ret = PTR_ERR(dev->pins->p);
36ab78029eSLinus Walleij goto cleanup_alloc;
37ab78029eSLinus Walleij }
38ab78029eSLinus Walleij
39ab78029eSLinus Walleij dev->pins->default_state = pinctrl_lookup_state(dev->pins->p,
40ab78029eSLinus Walleij PINCTRL_STATE_DEFAULT);
41ab78029eSLinus Walleij if (IS_ERR(dev->pins->default_state)) {
42ab78029eSLinus Walleij dev_dbg(dev, "no default pinctrl state\n");
43ab78029eSLinus Walleij ret = 0;
44ab78029eSLinus Walleij goto cleanup_get;
45ab78029eSLinus Walleij }
46ab78029eSLinus Walleij
47ef0eebc0SDouglas Anderson dev->pins->init_state = pinctrl_lookup_state(dev->pins->p,
48ef0eebc0SDouglas Anderson PINCTRL_STATE_INIT);
49ef0eebc0SDouglas Anderson if (IS_ERR(dev->pins->init_state)) {
50ef0eebc0SDouglas Anderson /* Not supplying this state is perfectly legal */
51ef0eebc0SDouglas Anderson dev_dbg(dev, "no init pinctrl state\n");
52ef0eebc0SDouglas Anderson
53ef0eebc0SDouglas Anderson ret = pinctrl_select_state(dev->pins->p,
54ef0eebc0SDouglas Anderson dev->pins->default_state);
55ef0eebc0SDouglas Anderson } else {
56ef0eebc0SDouglas Anderson ret = pinctrl_select_state(dev->pins->p, dev->pins->init_state);
57ef0eebc0SDouglas Anderson }
58ef0eebc0SDouglas Anderson
59ab78029eSLinus Walleij if (ret) {
60ef0eebc0SDouglas Anderson dev_dbg(dev, "failed to activate initial pinctrl state\n");
61ab78029eSLinus Walleij goto cleanup_get;
62ab78029eSLinus Walleij }
63ab78029eSLinus Walleij
6414005ee2SLinus Walleij #ifdef CONFIG_PM
6514005ee2SLinus Walleij /*
6614005ee2SLinus Walleij * If power management is enabled, we also look for the optional
6714005ee2SLinus Walleij * sleep and idle pin states, with semantics as defined in
6814005ee2SLinus Walleij * <linux/pinctrl/pinctrl-state.h>
6914005ee2SLinus Walleij */
7014005ee2SLinus Walleij dev->pins->sleep_state = pinctrl_lookup_state(dev->pins->p,
7114005ee2SLinus Walleij PINCTRL_STATE_SLEEP);
7214005ee2SLinus Walleij if (IS_ERR(dev->pins->sleep_state))
7314005ee2SLinus Walleij /* Not supplying this state is perfectly legal */
7414005ee2SLinus Walleij dev_dbg(dev, "no sleep pinctrl state\n");
7514005ee2SLinus Walleij
7614005ee2SLinus Walleij dev->pins->idle_state = pinctrl_lookup_state(dev->pins->p,
7714005ee2SLinus Walleij PINCTRL_STATE_IDLE);
7814005ee2SLinus Walleij if (IS_ERR(dev->pins->idle_state))
7914005ee2SLinus Walleij /* Not supplying this state is perfectly legal */
8014005ee2SLinus Walleij dev_dbg(dev, "no idle pinctrl state\n");
8114005ee2SLinus Walleij #endif
8214005ee2SLinus Walleij
83ab78029eSLinus Walleij return 0;
84ab78029eSLinus Walleij
85ab78029eSLinus Walleij /*
86ab78029eSLinus Walleij * If no pinctrl handle or default state was found for this device,
87ab78029eSLinus Walleij * let's explicitly free the pin container in the device, there is
88ab78029eSLinus Walleij * no point in keeping it around.
89ab78029eSLinus Walleij */
90ab78029eSLinus Walleij cleanup_get:
91ab78029eSLinus Walleij devm_pinctrl_put(dev->pins->p);
92ab78029eSLinus Walleij cleanup_alloc:
93ab78029eSLinus Walleij devm_kfree(dev, dev->pins);
94ab78029eSLinus Walleij dev->pins = NULL;
95ab78029eSLinus Walleij
96eb4ec68aSDeepak /* Return deferrals */
97eb4ec68aSDeepak if (ret == -EPROBE_DEFER)
98ab78029eSLinus Walleij return ret;
99eb4ec68aSDeepak /* Return serious errors */
100eb4ec68aSDeepak if (ret == -EINVAL)
101eb4ec68aSDeepak return ret;
102eb4ec68aSDeepak /* We ignore errors like -ENOENT meaning no pinctrl state */
103eb4ec68aSDeepak
104eb4ec68aSDeepak return 0;
105ab78029eSLinus Walleij }
106