blob: eb929dd6ef1e2bf117fe15f52c16a009d9b00be6 [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
Johan Hovolded032c12017-06-06 17:59:01 +020026 if (dev->of_node_reused)
27 return 0;
28
Linus Walleijab780292013-01-22 10:56:14 -070029 dev->pins = devm_kzalloc(dev, sizeof(*(dev->pins)), GFP_KERNEL);
30 if (!dev->pins)
31 return -ENOMEM;
32
33 dev->pins->p = devm_pinctrl_get(dev);
34 if (IS_ERR(dev->pins->p)) {
35 dev_dbg(dev, "no pinctrl handle\n");
36 ret = PTR_ERR(dev->pins->p);
37 goto cleanup_alloc;
38 }
39
40 dev->pins->default_state = pinctrl_lookup_state(dev->pins->p,
41 PINCTRL_STATE_DEFAULT);
42 if (IS_ERR(dev->pins->default_state)) {
43 dev_dbg(dev, "no default pinctrl state\n");
44 ret = 0;
45 goto cleanup_get;
46 }
47
Douglas Andersonef0eebc2015-10-20 21:15:06 -070048 dev->pins->init_state = pinctrl_lookup_state(dev->pins->p,
49 PINCTRL_STATE_INIT);
50 if (IS_ERR(dev->pins->init_state)) {
51 /* Not supplying this state is perfectly legal */
52 dev_dbg(dev, "no init pinctrl state\n");
53
54 ret = pinctrl_select_state(dev->pins->p,
55 dev->pins->default_state);
56 } else {
57 ret = pinctrl_select_state(dev->pins->p, dev->pins->init_state);
58 }
59
Linus Walleijab780292013-01-22 10:56:14 -070060 if (ret) {
Douglas Andersonef0eebc2015-10-20 21:15:06 -070061 dev_dbg(dev, "failed to activate initial pinctrl state\n");
Linus Walleijab780292013-01-22 10:56:14 -070062 goto cleanup_get;
63 }
64
Linus Walleij14005ee2013-06-05 15:30:33 +020065#ifdef CONFIG_PM
66 /*
67 * If power management is enabled, we also look for the optional
68 * sleep and idle pin states, with semantics as defined in
69 * <linux/pinctrl/pinctrl-state.h>
70 */
71 dev->pins->sleep_state = pinctrl_lookup_state(dev->pins->p,
72 PINCTRL_STATE_SLEEP);
73 if (IS_ERR(dev->pins->sleep_state))
74 /* Not supplying this state is perfectly legal */
75 dev_dbg(dev, "no sleep pinctrl state\n");
76
77 dev->pins->idle_state = pinctrl_lookup_state(dev->pins->p,
78 PINCTRL_STATE_IDLE);
79 if (IS_ERR(dev->pins->idle_state))
80 /* Not supplying this state is perfectly legal */
81 dev_dbg(dev, "no idle pinctrl state\n");
82#endif
83
Linus Walleijab780292013-01-22 10:56:14 -070084 return 0;
85
86 /*
87 * If no pinctrl handle or default state was found for this device,
88 * let's explicitly free the pin container in the device, there is
89 * no point in keeping it around.
90 */
91cleanup_get:
92 devm_pinctrl_put(dev->pins->p);
93cleanup_alloc:
94 devm_kfree(dev, dev->pins);
95 dev->pins = NULL;
96
Deepakeb4ec682016-09-13 12:43:04 +053097 /* Return deferrals */
98 if (ret == -EPROBE_DEFER)
99 return ret;
100 /* Return serious errors */
101 if (ret == -EINVAL)
102 return ret;
103 /* We ignore errors like -ENOENT meaning no pinctrl state */
Linus Walleijab780292013-01-22 10:56:14 -0700104
Deepakeb4ec682016-09-13 12:43:04 +0530105 return 0;
Linus Walleijab780292013-01-22 10:56:14 -0700106}