blob: fec06a1a0e15d64e04c556b583207d89327456e1 [file] [log] [blame]
Greg Kroah-Hartman989d42e2017-11-07 17:30:07 +01001// SPDX-License-Identifier: GPL-2.0
Linus Walleijab780292013-01-22 10:56:14 -07002/*
3 * Driver core interface to the pinctrl subsystem.
4 *
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
8 *
9 * Author: Linus Walleij <linus.walleij@linaro.org>
10 *
11 * License terms: GNU General Public License (GPL) version 2
12 */
13
14#include <linux/device.h>
15#include <linux/pinctrl/devinfo.h>
16#include <linux/pinctrl/consumer.h>
17#include <linux/slab.h>
18
19/**
20 * pinctrl_bind_pins() - called by the device core before probe
21 * @dev: the device that is just about to probe
22 */
23int pinctrl_bind_pins(struct device *dev)
24{
25 int ret;
26
Johan Hovolded032c12017-06-06 17:59:01 +020027 if (dev->of_node_reused)
28 return 0;
29
Linus Walleijab780292013-01-22 10:56:14 -070030 dev->pins = devm_kzalloc(dev, sizeof(*(dev->pins)), GFP_KERNEL);
31 if (!dev->pins)
32 return -ENOMEM;
33
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);
38 goto cleanup_alloc;
39 }
40
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");
45 ret = 0;
46 goto cleanup_get;
47 }
48
Douglas Andersonef0eebc2015-10-20 21:15:06 -070049 dev->pins->init_state = pinctrl_lookup_state(dev->pins->p,
50 PINCTRL_STATE_INIT);
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");
54
55 ret = pinctrl_select_state(dev->pins->p,
56 dev->pins->default_state);
57 } else {
58 ret = pinctrl_select_state(dev->pins->p, dev->pins->init_state);
59 }
60
Linus Walleijab780292013-01-22 10:56:14 -070061 if (ret) {
Douglas Andersonef0eebc2015-10-20 21:15:06 -070062 dev_dbg(dev, "failed to activate initial pinctrl state\n");
Linus Walleijab780292013-01-22 10:56:14 -070063 goto cleanup_get;
64 }
65
Linus Walleij14005ee2013-06-05 15:30:33 +020066#ifdef CONFIG_PM
67 /*
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>
71 */
72 dev->pins->sleep_state = pinctrl_lookup_state(dev->pins->p,
73 PINCTRL_STATE_SLEEP);
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");
77
78 dev->pins->idle_state = pinctrl_lookup_state(dev->pins->p,
79 PINCTRL_STATE_IDLE);
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");
83#endif
84
Linus Walleijab780292013-01-22 10:56:14 -070085 return 0;
86
87 /*
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.
91 */
92cleanup_get:
93 devm_pinctrl_put(dev->pins->p);
94cleanup_alloc:
95 devm_kfree(dev, dev->pins);
96 dev->pins = NULL;
97
Deepakeb4ec682016-09-13 12:43:04 +053098 /* Return deferrals */
99 if (ret == -EPROBE_DEFER)
100 return ret;
101 /* Return serious errors */
102 if (ret == -EINVAL)
103 return ret;
104 /* We ignore errors like -ENOENT meaning no pinctrl state */
Linus Walleijab780292013-01-22 10:56:14 -0700105
Deepakeb4ec682016-09-13 12:43:04 +0530106 return 0;
Linus Walleijab780292013-01-22 10:56:14 -0700107}