1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Consumer interface the pin control 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 #ifndef __LINUX_PINCTRL_CONSUMER_H
12 #define __LINUX_PINCTRL_CONSUMER_H
13 
14 #include <linux/err.h>
15 #include <linux/types.h>
16 
17 #include <linux/pinctrl/pinctrl-state.h>
18 
19 struct device;
20 
21 /* This struct is private to the core and should be regarded as a cookie */
22 struct pinctrl;
23 struct pinctrl_state;
24 
25 #ifdef CONFIG_PINCTRL
26 
27 /* External interface to pin control */
28 extern bool pinctrl_gpio_can_use_line(unsigned gpio);
29 extern int pinctrl_gpio_request(unsigned gpio);
30 extern void pinctrl_gpio_free(unsigned gpio);
31 extern int pinctrl_gpio_direction_input(unsigned gpio);
32 extern int pinctrl_gpio_direction_output(unsigned gpio);
33 extern int pinctrl_gpio_set_config(unsigned gpio, unsigned long config);
34 
35 extern struct pinctrl * __must_check pinctrl_get(struct device *dev);
36 extern void pinctrl_put(struct pinctrl *p);
37 extern struct pinctrl_state * __must_check pinctrl_lookup_state(struct pinctrl *p,
38 								const char *name);
39 extern int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *s);
40 
41 extern struct pinctrl * __must_check devm_pinctrl_get(struct device *dev);
42 extern void devm_pinctrl_put(struct pinctrl *p);
43 extern int pinctrl_select_default_state(struct device *dev);
44 
45 #ifdef CONFIG_PM
46 extern int pinctrl_pm_select_default_state(struct device *dev);
47 extern int pinctrl_pm_select_sleep_state(struct device *dev);
48 extern int pinctrl_pm_select_idle_state(struct device *dev);
49 #else
pinctrl_pm_select_default_state(struct device * dev)50 static inline int pinctrl_pm_select_default_state(struct device *dev)
51 {
52 	return 0;
53 }
pinctrl_pm_select_sleep_state(struct device * dev)54 static inline int pinctrl_pm_select_sleep_state(struct device *dev)
55 {
56 	return 0;
57 }
pinctrl_pm_select_idle_state(struct device * dev)58 static inline int pinctrl_pm_select_idle_state(struct device *dev)
59 {
60 	return 0;
61 }
62 #endif
63 
64 #else /* !CONFIG_PINCTRL */
65 
pinctrl_gpio_can_use_line(unsigned gpio)66 static inline bool pinctrl_gpio_can_use_line(unsigned gpio)
67 {
68 	return true;
69 }
70 
pinctrl_gpio_request(unsigned gpio)71 static inline int pinctrl_gpio_request(unsigned gpio)
72 {
73 	return 0;
74 }
75 
pinctrl_gpio_free(unsigned gpio)76 static inline void pinctrl_gpio_free(unsigned gpio)
77 {
78 }
79 
pinctrl_gpio_direction_input(unsigned gpio)80 static inline int pinctrl_gpio_direction_input(unsigned gpio)
81 {
82 	return 0;
83 }
84 
pinctrl_gpio_direction_output(unsigned gpio)85 static inline int pinctrl_gpio_direction_output(unsigned gpio)
86 {
87 	return 0;
88 }
89 
pinctrl_gpio_set_config(unsigned gpio,unsigned long config)90 static inline int pinctrl_gpio_set_config(unsigned gpio, unsigned long config)
91 {
92 	return 0;
93 }
94 
pinctrl_get(struct device * dev)95 static inline struct pinctrl * __must_check pinctrl_get(struct device *dev)
96 {
97 	return NULL;
98 }
99 
pinctrl_put(struct pinctrl * p)100 static inline void pinctrl_put(struct pinctrl *p)
101 {
102 }
103 
pinctrl_lookup_state(struct pinctrl * p,const char * name)104 static inline struct pinctrl_state * __must_check pinctrl_lookup_state(struct pinctrl *p,
105 								       const char *name)
106 {
107 	return NULL;
108 }
109 
pinctrl_select_state(struct pinctrl * p,struct pinctrl_state * s)110 static inline int pinctrl_select_state(struct pinctrl *p,
111 				       struct pinctrl_state *s)
112 {
113 	return 0;
114 }
115 
devm_pinctrl_get(struct device * dev)116 static inline struct pinctrl * __must_check devm_pinctrl_get(struct device *dev)
117 {
118 	return NULL;
119 }
120 
devm_pinctrl_put(struct pinctrl * p)121 static inline void devm_pinctrl_put(struct pinctrl *p)
122 {
123 }
124 
pinctrl_select_default_state(struct device * dev)125 static inline int pinctrl_select_default_state(struct device *dev)
126 {
127 	return 0;
128 }
129 
pinctrl_pm_select_default_state(struct device * dev)130 static inline int pinctrl_pm_select_default_state(struct device *dev)
131 {
132 	return 0;
133 }
134 
pinctrl_pm_select_sleep_state(struct device * dev)135 static inline int pinctrl_pm_select_sleep_state(struct device *dev)
136 {
137 	return 0;
138 }
139 
pinctrl_pm_select_idle_state(struct device * dev)140 static inline int pinctrl_pm_select_idle_state(struct device *dev)
141 {
142 	return 0;
143 }
144 
145 #endif /* CONFIG_PINCTRL */
146 
pinctrl_get_select(struct device * dev,const char * name)147 static inline struct pinctrl * __must_check pinctrl_get_select(struct device *dev,
148 							       const char *name)
149 {
150 	struct pinctrl *p;
151 	struct pinctrl_state *s;
152 	int ret;
153 
154 	p = pinctrl_get(dev);
155 	if (IS_ERR(p))
156 		return p;
157 
158 	s = pinctrl_lookup_state(p, name);
159 	if (IS_ERR(s)) {
160 		pinctrl_put(p);
161 		return ERR_CAST(s);
162 	}
163 
164 	ret = pinctrl_select_state(p, s);
165 	if (ret < 0) {
166 		pinctrl_put(p);
167 		return ERR_PTR(ret);
168 	}
169 
170 	return p;
171 }
172 
pinctrl_get_select_default(struct device * dev)173 static inline struct pinctrl * __must_check pinctrl_get_select_default(struct device *dev)
174 {
175 	return pinctrl_get_select(dev, PINCTRL_STATE_DEFAULT);
176 }
177 
devm_pinctrl_get_select(struct device * dev,const char * name)178 static inline struct pinctrl * __must_check devm_pinctrl_get_select(struct device *dev,
179 								    const char *name)
180 {
181 	struct pinctrl *p;
182 	struct pinctrl_state *s;
183 	int ret;
184 
185 	p = devm_pinctrl_get(dev);
186 	if (IS_ERR(p))
187 		return p;
188 
189 	s = pinctrl_lookup_state(p, name);
190 	if (IS_ERR(s)) {
191 		devm_pinctrl_put(p);
192 		return ERR_CAST(s);
193 	}
194 
195 	ret = pinctrl_select_state(p, s);
196 	if (ret < 0) {
197 		devm_pinctrl_put(p);
198 		return ERR_PTR(ret);
199 	}
200 
201 	return p;
202 }
203 
devm_pinctrl_get_select_default(struct device * dev)204 static inline struct pinctrl * __must_check devm_pinctrl_get_select_default(struct device *dev)
205 {
206 	return devm_pinctrl_get_select(dev, PINCTRL_STATE_DEFAULT);
207 }
208 
209 #endif /* __LINUX_PINCTRL_CONSUMER_H */
210