blob: 5917b4b5fb99f3dce1f264c884dfadc26643ec00 [file] [log] [blame]
Linus Walleijab780292013-01-22 10:56:14 -07001/*
2 * Driver core interface to the pinctrl subsystem.
3 *
4 * Copyright (C) 2012 ST-Ericsson SA
5 * Written on behalf of Linaro for ST-Ericsson
6 * Based on bits of regulator core, gpio core and clk core
7 *
8 * Author: Linus Walleij <linus.walleij@linaro.org>
9 *
10 * License terms: GNU General Public License (GPL) version 2
11 */
12
13#include <linux/device.h>
14#include <linux/pinctrl/devinfo.h>
15#include <linux/pinctrl/consumer.h>
16#include <linux/slab.h>
17
18/**
19 * pinctrl_bind_pins() - called by the device core before probe
20 * @dev: the device that is just about to probe
21 */
22int pinctrl_bind_pins(struct device *dev)
23{
24 int ret;
25
26 dev->pins = devm_kzalloc(dev, sizeof(*(dev->pins)), GFP_KERNEL);
27 if (!dev->pins)
28 return -ENOMEM;
29
30 dev->pins->p = devm_pinctrl_get(dev);
31 if (IS_ERR(dev->pins->p)) {
32 dev_dbg(dev, "no pinctrl handle\n");
33 ret = PTR_ERR(dev->pins->p);
34 goto cleanup_alloc;
35 }
36
37 dev->pins->default_state = pinctrl_lookup_state(dev->pins->p,
38 PINCTRL_STATE_DEFAULT);
39 if (IS_ERR(dev->pins->default_state)) {
40 dev_dbg(dev, "no default pinctrl state\n");
41 ret = 0;
42 goto cleanup_get;
43 }
44
Douglas Andersonef0eebc2015-10-20 21:15:06 -070045 dev->pins->init_state = pinctrl_lookup_state(dev->pins->p,
46 PINCTRL_STATE_INIT);
47 if (IS_ERR(dev->pins->init_state)) {
48 /* Not supplying this state is perfectly legal */
49 dev_dbg(dev, "no init pinctrl state\n");
50
51 ret = pinctrl_select_state(dev->pins->p,
52 dev->pins->default_state);
53 } else {
54 ret = pinctrl_select_state(dev->pins->p, dev->pins->init_state);
55 }
56
Linus Walleijab780292013-01-22 10:56:14 -070057 if (ret) {
Douglas Andersonef0eebc2015-10-20 21:15:06 -070058 dev_dbg(dev, "failed to activate initial pinctrl state\n");
Linus Walleijab780292013-01-22 10:56:14 -070059 goto cleanup_get;
60 }
61
Linus Walleij14005ee2013-06-05 15:30:33 +020062#ifdef CONFIG_PM
63 /*
64 * If power management is enabled, we also look for the optional
65 * sleep and idle pin states, with semantics as defined in
66 * <linux/pinctrl/pinctrl-state.h>
67 */
68 dev->pins->sleep_state = pinctrl_lookup_state(dev->pins->p,
69 PINCTRL_STATE_SLEEP);
70 if (IS_ERR(dev->pins->sleep_state))
71 /* Not supplying this state is perfectly legal */
72 dev_dbg(dev, "no sleep pinctrl state\n");
73
74 dev->pins->idle_state = pinctrl_lookup_state(dev->pins->p,
75 PINCTRL_STATE_IDLE);
76 if (IS_ERR(dev->pins->idle_state))
77 /* Not supplying this state is perfectly legal */
78 dev_dbg(dev, "no idle pinctrl state\n");
79#endif
80
Linus Walleijab780292013-01-22 10:56:14 -070081 return 0;
82
83 /*
84 * If no pinctrl handle or default state was found for this device,
85 * let's explicitly free the pin container in the device, there is
86 * no point in keeping it around.
87 */
88cleanup_get:
89 devm_pinctrl_put(dev->pins->p);
90cleanup_alloc:
91 devm_kfree(dev, dev->pins);
92 dev->pins = NULL;
93
Deepakeb4ec682016-09-13 12:43:04 +053094 /* Return deferrals */
95 if (ret == -EPROBE_DEFER)
96 return ret;
97 /* Return serious errors */
98 if (ret == -EINVAL)
99 return ret;
100 /* We ignore errors like -ENOENT meaning no pinctrl state */
Linus Walleijab780292013-01-22 10:56:14 -0700101
Deepakeb4ec682016-09-13 12:43:04 +0530102 return 0;
Linus Walleijab780292013-01-22 10:56:14 -0700103}