xref: /linux-6.15/include/linux/gpio/consumer.h (revision e756bc56)
1 #ifndef __LINUX_GPIO_CONSUMER_H
2 #define __LINUX_GPIO_CONSUMER_H
3 
4 #include <linux/err.h>
5 #include <linux/kernel.h>
6 
7 #ifdef CONFIG_GPIOLIB
8 
9 struct device;
10 struct gpio_chip;
11 
12 /**
13  * Opaque descriptor for a GPIO. These are obtained using gpiod_get() and are
14  * preferable to the old integer-based handles.
15  *
16  * Contrary to integers, a pointer to a gpio_desc is guaranteed to be valid
17  * until the GPIO is released.
18  */
19 struct gpio_desc;
20 
21 /* Acquire and dispose GPIOs */
22 struct gpio_desc *__must_check gpiod_get(struct device *dev,
23 					 const char *con_id);
24 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
25 					       const char *con_id,
26 					       unsigned int idx);
27 void gpiod_put(struct gpio_desc *desc);
28 
29 struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
30 					      const char *con_id);
31 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
32 						    const char *con_id,
33 						    unsigned int idx);
34 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
35 
36 int gpiod_get_direction(const struct gpio_desc *desc);
37 int gpiod_direction_input(struct gpio_desc *desc);
38 int gpiod_direction_output(struct gpio_desc *desc, int value);
39 
40 /* Value get/set from non-sleeping context */
41 int gpiod_get_value(const struct gpio_desc *desc);
42 void gpiod_set_value(struct gpio_desc *desc, int value);
43 int gpiod_get_raw_value(const struct gpio_desc *desc);
44 void gpiod_set_raw_value(struct gpio_desc *desc, int value);
45 
46 /* Value get/set from sleeping context */
47 int gpiod_get_value_cansleep(const struct gpio_desc *desc);
48 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
49 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
50 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
51 
52 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
53 
54 int gpiod_is_active_low(const struct gpio_desc *desc);
55 int gpiod_cansleep(const struct gpio_desc *desc);
56 
57 int gpiod_to_irq(const struct gpio_desc *desc);
58 
59 /* Convert between the old gpio_ and new gpiod_ interfaces */
60 struct gpio_desc *gpio_to_desc(unsigned gpio);
61 int desc_to_gpio(const struct gpio_desc *desc);
62 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc);
63 
64 #else /* CONFIG_GPIOLIB */
65 
66 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
67 						       const char *con_id)
68 {
69 	return ERR_PTR(-ENOSYS);
70 }
71 static inline struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
72 							     const char *con_id,
73 							     unsigned int idx)
74 {
75 	return ERR_PTR(-ENOSYS);
76 }
77 static inline void gpiod_put(struct gpio_desc *desc)
78 {
79 	might_sleep();
80 
81 	/* GPIO can never have been requested */
82 	WARN_ON(1);
83 }
84 
85 static inline struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
86 							    const char *con_id)
87 {
88 	return ERR_PTR(-ENOSYS);
89 }
90 static inline
91 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
92 						    const char *con_id,
93 						    unsigned int idx)
94 {
95 	return ERR_PTR(-ENOSYS);
96 }
97 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
98 {
99 	might_sleep();
100 
101 	/* GPIO can never have been requested */
102 	WARN_ON(1);
103 }
104 
105 
106 static inline int gpiod_get_direction(const struct gpio_desc *desc)
107 {
108 	/* GPIO can never have been requested */
109 	WARN_ON(1);
110 	return -ENOSYS;
111 }
112 static inline int gpiod_direction_input(struct gpio_desc *desc)
113 {
114 	/* GPIO can never have been requested */
115 	WARN_ON(1);
116 	return -ENOSYS;
117 }
118 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
119 {
120 	/* GPIO can never have been requested */
121 	WARN_ON(1);
122 	return -ENOSYS;
123 }
124 
125 
126 static inline int gpiod_get_value(const struct gpio_desc *desc)
127 {
128 	/* GPIO can never have been requested */
129 	WARN_ON(1);
130 	return 0;
131 }
132 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
133 {
134 	/* GPIO can never have been requested */
135 	WARN_ON(1);
136 }
137 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
138 {
139 	/* GPIO can never have been requested */
140 	WARN_ON(1);
141 	return 0;
142 }
143 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
144 {
145 	/* GPIO can never have been requested */
146 	WARN_ON(1);
147 }
148 
149 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
150 {
151 	/* GPIO can never have been requested */
152 	WARN_ON(1);
153 	return 0;
154 }
155 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
156 {
157 	/* GPIO can never have been requested */
158 	WARN_ON(1);
159 }
160 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
161 {
162 	/* GPIO can never have been requested */
163 	WARN_ON(1);
164 	return 0;
165 }
166 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
167 						int value)
168 {
169 	/* GPIO can never have been requested */
170 	WARN_ON(1);
171 }
172 
173 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
174 {
175 	/* GPIO can never have been requested */
176 	WARN_ON(1);
177 	return -ENOSYS;
178 }
179 
180 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
181 {
182 	/* GPIO can never have been requested */
183 	WARN_ON(1);
184 	return 0;
185 }
186 static inline int gpiod_cansleep(const struct gpio_desc *desc)
187 {
188 	/* GPIO can never have been requested */
189 	WARN_ON(1);
190 	return 0;
191 }
192 
193 static inline int gpiod_to_irq(const struct gpio_desc *desc)
194 {
195 	/* GPIO can never have been requested */
196 	WARN_ON(1);
197 	return -EINVAL;
198 }
199 
200 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
201 {
202 	return ERR_PTR(-EINVAL);
203 }
204 static inline int desc_to_gpio(const struct gpio_desc *desc)
205 {
206 	/* GPIO can never have been requested */
207 	WARN_ON(1);
208 	return -EINVAL;
209 }
210 static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
211 {
212 	/* GPIO can never have been requested */
213 	WARN_ON(1);
214 	return ERR_PTR(-ENODEV);
215 }
216 
217 
218 #endif /* CONFIG_GPIOLIB */
219 
220 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
221 
222 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
223 int gpiod_export_link(struct device *dev, const char *name,
224 		      struct gpio_desc *desc);
225 int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
226 void gpiod_unexport(struct gpio_desc *desc);
227 
228 #else  /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
229 
230 static inline int gpiod_export(struct gpio_desc *desc,
231 			       bool direction_may_change)
232 {
233 	return -ENOSYS;
234 }
235 
236 static inline int gpiod_export_link(struct device *dev, const char *name,
237 				    struct gpio_desc *desc)
238 {
239 	return -ENOSYS;
240 }
241 
242 static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
243 {
244 	return -ENOSYS;
245 }
246 
247 static inline void gpiod_unexport(struct gpio_desc *desc)
248 {
249 }
250 
251 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
252 
253 #endif
254