xref: /linux/drivers/base/pinctrl.c (revision 93df8a1ed6231727c5db94a80b1a6bd5ee67cec3)
1 /*
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  */
22 int 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 
45 	ret = pinctrl_select_state(dev->pins->p, dev->pins->default_state);
46 	if (ret) {
47 		dev_dbg(dev, "failed to activate default pinctrl state\n");
48 		goto cleanup_get;
49 	}
50 
51 #ifdef CONFIG_PM
52 	/*
53 	 * If power management is enabled, we also look for the optional
54 	 * sleep and idle pin states, with semantics as defined in
55 	 * <linux/pinctrl/pinctrl-state.h>
56 	 */
57 	dev->pins->sleep_state = pinctrl_lookup_state(dev->pins->p,
58 					PINCTRL_STATE_SLEEP);
59 	if (IS_ERR(dev->pins->sleep_state))
60 		/* Not supplying this state is perfectly legal */
61 		dev_dbg(dev, "no sleep pinctrl state\n");
62 
63 	dev->pins->idle_state = pinctrl_lookup_state(dev->pins->p,
64 					PINCTRL_STATE_IDLE);
65 	if (IS_ERR(dev->pins->idle_state))
66 		/* Not supplying this state is perfectly legal */
67 		dev_dbg(dev, "no idle pinctrl state\n");
68 #endif
69 
70 	return 0;
71 
72 	/*
73 	 * If no pinctrl handle or default state was found for this device,
74 	 * let's explicitly free the pin container in the device, there is
75 	 * no point in keeping it around.
76 	 */
77 cleanup_get:
78 	devm_pinctrl_put(dev->pins->p);
79 cleanup_alloc:
80 	devm_kfree(dev, dev->pins);
81 	dev->pins = NULL;
82 
83 	/* Only return deferrals */
84 	if (ret != -EPROBE_DEFER)
85 		ret = 0;
86 
87 	return ret;
88 }
89