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/list.h>
16 #include <linux/seq_file.h>
17 #include <linux/pinctrl/pinctrl-state.h>
18 
19 /* This struct is private to the core and should be regarded as a cookie */
20 struct pinctrl;
21 struct pinctrl_state;
22 struct device;
23 
24 #ifdef CONFIG_PINCTRL
25 
26 /* External interface to pin control */
27 extern bool pinctrl_gpio_can_use_line(unsigned gpio);
28 extern int pinctrl_gpio_request(unsigned gpio);
29 extern void pinctrl_gpio_free(unsigned gpio);
30 extern int pinctrl_gpio_direction_input(unsigned gpio);
31 extern int pinctrl_gpio_direction_output(unsigned gpio);
32 extern int pinctrl_gpio_set_config(unsigned gpio, unsigned long config);
33 
34 extern struct pinctrl * __must_check pinctrl_get(struct device *dev);
35 extern void pinctrl_put(struct pinctrl *p);
36 extern struct pinctrl_state * __must_check pinctrl_lookup_state(
37 							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(
105 							struct pinctrl *p,
106 							const char *name)
107 {
108 	return NULL;
109 }
110 
pinctrl_select_state(struct pinctrl * p,struct pinctrl_state * s)111 static inline int pinctrl_select_state(struct pinctrl *p,
112 				       struct pinctrl_state *s)
113 {
114 	return 0;
115 }
116 
devm_pinctrl_get(struct device * dev)117 static inline struct pinctrl * __must_check devm_pinctrl_get(struct device *dev)
118 {
119 	return NULL;
120 }
121 
devm_pinctrl_put(struct pinctrl * p)122 static inline void devm_pinctrl_put(struct pinctrl *p)
123 {
124 }
125 
pinctrl_select_default_state(struct device * dev)126 static inline int pinctrl_select_default_state(struct device *dev)
127 {
128 	return 0;
129 }
130 
pinctrl_pm_select_default_state(struct device * dev)131 static inline int pinctrl_pm_select_default_state(struct device *dev)
132 {
133 	return 0;
134 }
135 
pinctrl_pm_select_sleep_state(struct device * dev)136 static inline int pinctrl_pm_select_sleep_state(struct device *dev)
137 {
138 	return 0;
139 }
140 
pinctrl_pm_select_idle_state(struct device * dev)141 static inline int pinctrl_pm_select_idle_state(struct device *dev)
142 {
143 	return 0;
144 }
145 
146 #endif /* CONFIG_PINCTRL */
147 
pinctrl_get_select(struct device * dev,const char * name)148 static inline struct pinctrl * __must_check pinctrl_get_select(
149 					struct device *dev, const char *name)
150 {
151 	struct pinctrl *p;
152 	struct pinctrl_state *s;
153 	int ret;
154 
155 	p = pinctrl_get(dev);
156 	if (IS_ERR(p))
157 		return p;
158 
159 	s = pinctrl_lookup_state(p, name);
160 	if (IS_ERR(s)) {
161 		pinctrl_put(p);
162 		return ERR_CAST(s);
163 	}
164 
165 	ret = pinctrl_select_state(p, s);
166 	if (ret < 0) {
167 		pinctrl_put(p);
168 		return ERR_PTR(ret);
169 	}
170 
171 	return p;
172 }
173 
pinctrl_get_select_default(struct device * dev)174 static inline struct pinctrl * __must_check pinctrl_get_select_default(
175 					struct device *dev)
176 {
177 	return pinctrl_get_select(dev, PINCTRL_STATE_DEFAULT);
178 }
179 
devm_pinctrl_get_select(struct device * dev,const char * name)180 static inline struct pinctrl * __must_check devm_pinctrl_get_select(
181 					struct device *dev, const char *name)
182 {
183 	struct pinctrl *p;
184 	struct pinctrl_state *s;
185 	int ret;
186 
187 	p = devm_pinctrl_get(dev);
188 	if (IS_ERR(p))
189 		return p;
190 
191 	s = pinctrl_lookup_state(p, name);
192 	if (IS_ERR(s)) {
193 		devm_pinctrl_put(p);
194 		return ERR_CAST(s);
195 	}
196 
197 	ret = pinctrl_select_state(p, s);
198 	if (ret < 0) {
199 		devm_pinctrl_put(p);
200 		return ERR_PTR(ret);
201 	}
202 
203 	return p;
204 }
205 
devm_pinctrl_get_select_default(struct device * dev)206 static inline struct pinctrl * __must_check devm_pinctrl_get_select_default(
207 					struct device *dev)
208 {
209 	return devm_pinctrl_get_select(dev, PINCTRL_STATE_DEFAULT);
210 }
211 
212 #endif /* __LINUX_PINCTRL_CONSUMER_H */
213