1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_GPIO_CONSUMER_H
3 #define __LINUX_GPIO_CONSUMER_H
4 
5 #include <linux/bits.h>
6 #include <linux/bug.h>
7 #include <linux/compiler_types.h>
8 #include <linux/err.h>
9 
10 struct device;
11 struct gpio_desc;
12 struct gpio_array;
13 
14 /**
15  * struct gpio_descs - Struct containing an array of descriptors that can be
16  *                     obtained using gpiod_get_array()
17  *
18  * @info:	Pointer to the opaque gpio_array structure
19  * @ndescs:	Number of held descriptors
20  * @desc:	Array of pointers to GPIO descriptors
21  */
22 struct gpio_descs {
23 	struct gpio_array *info;
24 	unsigned int ndescs;
25 	struct gpio_desc *desc[];
26 };
27 
28 #define GPIOD_FLAGS_BIT_DIR_SET		BIT(0)
29 #define GPIOD_FLAGS_BIT_DIR_OUT		BIT(1)
30 #define GPIOD_FLAGS_BIT_DIR_VAL		BIT(2)
31 #define GPIOD_FLAGS_BIT_OPEN_DRAIN	BIT(3)
32 #define GPIOD_FLAGS_BIT_NONEXCLUSIVE	BIT(4)
33 
34 /**
35  * enum gpiod_flags - Optional flags that can be passed to one of gpiod_* to
36  *                    configure direction and output value. These values
37  *                    cannot be OR'd.
38  *
39  * @GPIOD_ASIS:			Don't change anything
40  * @GPIOD_IN:			Set lines to input mode
41  * @GPIOD_OUT_LOW:		Set lines to output and drive them low
42  * @GPIOD_OUT_HIGH:		Set lines to output and drive them high
43  * @GPIOD_OUT_LOW_OPEN_DRAIN:	Set lines to open-drain output and drive them low
44  * @GPIOD_OUT_HIGH_OPEN_DRAIN:	Set lines to open-drain output and drive them high
45  */
46 enum gpiod_flags {
47 	GPIOD_ASIS	= 0,
48 	GPIOD_IN	= GPIOD_FLAGS_BIT_DIR_SET,
49 	GPIOD_OUT_LOW	= GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT,
50 	GPIOD_OUT_HIGH	= GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT |
51 			  GPIOD_FLAGS_BIT_DIR_VAL,
52 	GPIOD_OUT_LOW_OPEN_DRAIN = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_OPEN_DRAIN,
53 	GPIOD_OUT_HIGH_OPEN_DRAIN = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_OPEN_DRAIN,
54 };
55 
56 #ifdef CONFIG_GPIOLIB
57 
58 /* Return the number of GPIOs associated with a device / function */
59 int gpiod_count(struct device *dev, const char *con_id);
60 
61 /* Acquire and dispose GPIOs */
62 struct gpio_desc *__must_check gpiod_get(struct device *dev,
63 					 const char *con_id,
64 					 enum gpiod_flags flags);
65 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
66 					       const char *con_id,
67 					       unsigned int idx,
68 					       enum gpiod_flags flags);
69 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
70 						  const char *con_id,
71 						  enum gpiod_flags flags);
72 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
73 							const char *con_id,
74 							unsigned int index,
75 							enum gpiod_flags flags);
76 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
77 						const char *con_id,
78 						enum gpiod_flags flags);
79 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
80 							const char *con_id,
81 							enum gpiod_flags flags);
82 void gpiod_put(struct gpio_desc *desc);
83 void gpiod_put_array(struct gpio_descs *descs);
84 
85 struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
86 					      const char *con_id,
87 					      enum gpiod_flags flags);
88 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
89 						    const char *con_id,
90 						    unsigned int idx,
91 						    enum gpiod_flags flags);
92 struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
93 						       const char *con_id,
94 						       enum gpiod_flags flags);
95 struct gpio_desc *__must_check
96 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
97 			      unsigned int index, enum gpiod_flags flags);
98 struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
99 						     const char *con_id,
100 						     enum gpiod_flags flags);
101 struct gpio_descs *__must_check
102 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
103 			      enum gpiod_flags flags);
104 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
105 void devm_gpiod_unhinge(struct device *dev, struct gpio_desc *desc);
106 void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs);
107 
108 int gpiod_get_direction(struct gpio_desc *desc);
109 int gpiod_direction_input(struct gpio_desc *desc);
110 int gpiod_direction_output(struct gpio_desc *desc, int value);
111 int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
112 int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags);
113 int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags);
114 
115 /* Value get/set from non-sleeping context */
116 int gpiod_get_value(const struct gpio_desc *desc);
117 int gpiod_get_array_value(unsigned int array_size,
118 			  struct gpio_desc **desc_array,
119 			  struct gpio_array *array_info,
120 			  unsigned long *value_bitmap);
121 void gpiod_set_value(struct gpio_desc *desc, int value);
122 int gpiod_set_array_value(unsigned int array_size,
123 			  struct gpio_desc **desc_array,
124 			  struct gpio_array *array_info,
125 			  unsigned long *value_bitmap);
126 int gpiod_get_raw_value(const struct gpio_desc *desc);
127 int gpiod_get_raw_array_value(unsigned int array_size,
128 			      struct gpio_desc **desc_array,
129 			      struct gpio_array *array_info,
130 			      unsigned long *value_bitmap);
131 void gpiod_set_raw_value(struct gpio_desc *desc, int value);
132 int gpiod_set_raw_array_value(unsigned int array_size,
133 			      struct gpio_desc **desc_array,
134 			      struct gpio_array *array_info,
135 			      unsigned long *value_bitmap);
136 
137 /* Value get/set from sleeping context */
138 int gpiod_get_value_cansleep(const struct gpio_desc *desc);
139 int gpiod_get_array_value_cansleep(unsigned int array_size,
140 				   struct gpio_desc **desc_array,
141 				   struct gpio_array *array_info,
142 				   unsigned long *value_bitmap);
143 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
144 int gpiod_set_array_value_cansleep(unsigned int array_size,
145 				   struct gpio_desc **desc_array,
146 				   struct gpio_array *array_info,
147 				   unsigned long *value_bitmap);
148 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
149 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
150 				       struct gpio_desc **desc_array,
151 				       struct gpio_array *array_info,
152 				       unsigned long *value_bitmap);
153 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
154 int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
155 				       struct gpio_desc **desc_array,
156 				       struct gpio_array *array_info,
157 				       unsigned long *value_bitmap);
158 
159 int gpiod_set_config(struct gpio_desc *desc, unsigned long config);
160 int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce);
161 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory);
162 void gpiod_toggle_active_low(struct gpio_desc *desc);
163 
164 int gpiod_is_active_low(const struct gpio_desc *desc);
165 int gpiod_cansleep(const struct gpio_desc *desc);
166 
167 int gpiod_to_irq(const struct gpio_desc *desc);
168 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name);
169 
170 /* Convert between the old gpio_ and new gpiod_ interfaces */
171 struct gpio_desc *gpio_to_desc(unsigned gpio);
172 int desc_to_gpio(const struct gpio_desc *desc);
173 
174 /* Child properties interface */
175 struct fwnode_handle;
176 
177 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
178 					 const char *con_id, int index,
179 					 enum gpiod_flags flags,
180 					 const char *label);
181 struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
182 					      struct fwnode_handle *child,
183 					      const char *con_id, int index,
184 					      enum gpiod_flags flags,
185 					      const char *label);
186 
187 #else /* CONFIG_GPIOLIB */
188 
189 #include <linux/kernel.h>
190 
gpiod_count(struct device * dev,const char * con_id)191 static inline int gpiod_count(struct device *dev, const char *con_id)
192 {
193 	return 0;
194 }
195 
gpiod_get(struct device * dev,const char * con_id,enum gpiod_flags flags)196 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
197 						       const char *con_id,
198 						       enum gpiod_flags flags)
199 {
200 	return ERR_PTR(-ENOSYS);
201 }
202 static inline struct gpio_desc *__must_check
gpiod_get_index(struct device * dev,const char * con_id,unsigned int idx,enum gpiod_flags flags)203 gpiod_get_index(struct device *dev,
204 		const char *con_id,
205 		unsigned int idx,
206 		enum gpiod_flags flags)
207 {
208 	return ERR_PTR(-ENOSYS);
209 }
210 
211 static inline struct gpio_desc *__must_check
gpiod_get_optional(struct device * dev,const char * con_id,enum gpiod_flags flags)212 gpiod_get_optional(struct device *dev, const char *con_id,
213 		   enum gpiod_flags flags)
214 {
215 	return NULL;
216 }
217 
218 static inline struct gpio_desc *__must_check
gpiod_get_index_optional(struct device * dev,const char * con_id,unsigned int index,enum gpiod_flags flags)219 gpiod_get_index_optional(struct device *dev, const char *con_id,
220 			 unsigned int index, enum gpiod_flags flags)
221 {
222 	return NULL;
223 }
224 
225 static inline struct gpio_descs *__must_check
gpiod_get_array(struct device * dev,const char * con_id,enum gpiod_flags flags)226 gpiod_get_array(struct device *dev, const char *con_id,
227 		enum gpiod_flags flags)
228 {
229 	return ERR_PTR(-ENOSYS);
230 }
231 
232 static inline struct gpio_descs *__must_check
gpiod_get_array_optional(struct device * dev,const char * con_id,enum gpiod_flags flags)233 gpiod_get_array_optional(struct device *dev, const char *con_id,
234 			 enum gpiod_flags flags)
235 {
236 	return NULL;
237 }
238 
gpiod_put(struct gpio_desc * desc)239 static inline void gpiod_put(struct gpio_desc *desc)
240 {
241 	might_sleep();
242 
243 	/* GPIO can never have been requested */
244 	WARN_ON(desc);
245 }
246 
devm_gpiod_unhinge(struct device * dev,struct gpio_desc * desc)247 static inline void devm_gpiod_unhinge(struct device *dev,
248 				      struct gpio_desc *desc)
249 {
250 	might_sleep();
251 
252 	/* GPIO can never have been requested */
253 	WARN_ON(desc);
254 }
255 
gpiod_put_array(struct gpio_descs * descs)256 static inline void gpiod_put_array(struct gpio_descs *descs)
257 {
258 	might_sleep();
259 
260 	/* GPIO can never have been requested */
261 	WARN_ON(descs);
262 }
263 
264 static inline struct gpio_desc *__must_check
devm_gpiod_get(struct device * dev,const char * con_id,enum gpiod_flags flags)265 devm_gpiod_get(struct device *dev,
266 		 const char *con_id,
267 		 enum gpiod_flags flags)
268 {
269 	return ERR_PTR(-ENOSYS);
270 }
271 static inline
272 struct gpio_desc *__must_check
devm_gpiod_get_index(struct device * dev,const char * con_id,unsigned int idx,enum gpiod_flags flags)273 devm_gpiod_get_index(struct device *dev,
274 		       const char *con_id,
275 		       unsigned int idx,
276 		       enum gpiod_flags flags)
277 {
278 	return ERR_PTR(-ENOSYS);
279 }
280 
281 static inline struct gpio_desc *__must_check
devm_gpiod_get_optional(struct device * dev,const char * con_id,enum gpiod_flags flags)282 devm_gpiod_get_optional(struct device *dev, const char *con_id,
283 			  enum gpiod_flags flags)
284 {
285 	return NULL;
286 }
287 
288 static inline struct gpio_desc *__must_check
devm_gpiod_get_index_optional(struct device * dev,const char * con_id,unsigned int index,enum gpiod_flags flags)289 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
290 				unsigned int index, enum gpiod_flags flags)
291 {
292 	return NULL;
293 }
294 
295 static inline struct gpio_descs *__must_check
devm_gpiod_get_array(struct device * dev,const char * con_id,enum gpiod_flags flags)296 devm_gpiod_get_array(struct device *dev, const char *con_id,
297 		     enum gpiod_flags flags)
298 {
299 	return ERR_PTR(-ENOSYS);
300 }
301 
302 static inline struct gpio_descs *__must_check
devm_gpiod_get_array_optional(struct device * dev,const char * con_id,enum gpiod_flags flags)303 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
304 			      enum gpiod_flags flags)
305 {
306 	return NULL;
307 }
308 
devm_gpiod_put(struct device * dev,struct gpio_desc * desc)309 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
310 {
311 	might_sleep();
312 
313 	/* GPIO can never have been requested */
314 	WARN_ON(desc);
315 }
316 
devm_gpiod_put_array(struct device * dev,struct gpio_descs * descs)317 static inline void devm_gpiod_put_array(struct device *dev,
318 					struct gpio_descs *descs)
319 {
320 	might_sleep();
321 
322 	/* GPIO can never have been requested */
323 	WARN_ON(descs);
324 }
325 
326 
gpiod_get_direction(const struct gpio_desc * desc)327 static inline int gpiod_get_direction(const struct gpio_desc *desc)
328 {
329 	/* GPIO can never have been requested */
330 	WARN_ON(desc);
331 	return -ENOSYS;
332 }
gpiod_direction_input(struct gpio_desc * desc)333 static inline int gpiod_direction_input(struct gpio_desc *desc)
334 {
335 	/* GPIO can never have been requested */
336 	WARN_ON(desc);
337 	return -ENOSYS;
338 }
gpiod_direction_output(struct gpio_desc * desc,int value)339 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
340 {
341 	/* GPIO can never have been requested */
342 	WARN_ON(desc);
343 	return -ENOSYS;
344 }
gpiod_direction_output_raw(struct gpio_desc * desc,int value)345 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
346 {
347 	/* GPIO can never have been requested */
348 	WARN_ON(desc);
349 	return -ENOSYS;
350 }
gpiod_enable_hw_timestamp_ns(struct gpio_desc * desc,unsigned long flags)351 static inline int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc,
352 					       unsigned long flags)
353 {
354 	WARN_ON(desc);
355 	return -ENOSYS;
356 }
gpiod_disable_hw_timestamp_ns(struct gpio_desc * desc,unsigned long flags)357 static inline int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc,
358 						unsigned long flags)
359 {
360 	WARN_ON(desc);
361 	return -ENOSYS;
362 }
gpiod_get_value(const struct gpio_desc * desc)363 static inline int gpiod_get_value(const struct gpio_desc *desc)
364 {
365 	/* GPIO can never have been requested */
366 	WARN_ON(desc);
367 	return 0;
368 }
gpiod_get_array_value(unsigned int array_size,struct gpio_desc ** desc_array,struct gpio_array * array_info,unsigned long * value_bitmap)369 static inline int gpiod_get_array_value(unsigned int array_size,
370 					struct gpio_desc **desc_array,
371 					struct gpio_array *array_info,
372 					unsigned long *value_bitmap)
373 {
374 	/* GPIO can never have been requested */
375 	WARN_ON(desc_array);
376 	return 0;
377 }
gpiod_set_value(struct gpio_desc * desc,int value)378 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
379 {
380 	/* GPIO can never have been requested */
381 	WARN_ON(desc);
382 }
gpiod_set_array_value(unsigned int array_size,struct gpio_desc ** desc_array,struct gpio_array * array_info,unsigned long * value_bitmap)383 static inline int gpiod_set_array_value(unsigned int array_size,
384 					struct gpio_desc **desc_array,
385 					struct gpio_array *array_info,
386 					unsigned long *value_bitmap)
387 {
388 	/* GPIO can never have been requested */
389 	WARN_ON(desc_array);
390 	return 0;
391 }
gpiod_get_raw_value(const struct gpio_desc * desc)392 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
393 {
394 	/* GPIO can never have been requested */
395 	WARN_ON(desc);
396 	return 0;
397 }
gpiod_get_raw_array_value(unsigned int array_size,struct gpio_desc ** desc_array,struct gpio_array * array_info,unsigned long * value_bitmap)398 static inline int gpiod_get_raw_array_value(unsigned int array_size,
399 					    struct gpio_desc **desc_array,
400 					    struct gpio_array *array_info,
401 					    unsigned long *value_bitmap)
402 {
403 	/* GPIO can never have been requested */
404 	WARN_ON(desc_array);
405 	return 0;
406 }
gpiod_set_raw_value(struct gpio_desc * desc,int value)407 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
408 {
409 	/* GPIO can never have been requested */
410 	WARN_ON(desc);
411 }
gpiod_set_raw_array_value(unsigned int array_size,struct gpio_desc ** desc_array,struct gpio_array * array_info,unsigned long * value_bitmap)412 static inline int gpiod_set_raw_array_value(unsigned int array_size,
413 					    struct gpio_desc **desc_array,
414 					    struct gpio_array *array_info,
415 					    unsigned long *value_bitmap)
416 {
417 	/* GPIO can never have been requested */
418 	WARN_ON(desc_array);
419 	return 0;
420 }
421 
gpiod_get_value_cansleep(const struct gpio_desc * desc)422 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
423 {
424 	/* GPIO can never have been requested */
425 	WARN_ON(desc);
426 	return 0;
427 }
gpiod_get_array_value_cansleep(unsigned int array_size,struct gpio_desc ** desc_array,struct gpio_array * array_info,unsigned long * value_bitmap)428 static inline int gpiod_get_array_value_cansleep(unsigned int array_size,
429 				     struct gpio_desc **desc_array,
430 				     struct gpio_array *array_info,
431 				     unsigned long *value_bitmap)
432 {
433 	/* GPIO can never have been requested */
434 	WARN_ON(desc_array);
435 	return 0;
436 }
gpiod_set_value_cansleep(struct gpio_desc * desc,int value)437 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
438 {
439 	/* GPIO can never have been requested */
440 	WARN_ON(desc);
441 }
gpiod_set_array_value_cansleep(unsigned int array_size,struct gpio_desc ** desc_array,struct gpio_array * array_info,unsigned long * value_bitmap)442 static inline int gpiod_set_array_value_cansleep(unsigned int array_size,
443 					    struct gpio_desc **desc_array,
444 					    struct gpio_array *array_info,
445 					    unsigned long *value_bitmap)
446 {
447 	/* GPIO can never have been requested */
448 	WARN_ON(desc_array);
449 	return 0;
450 }
gpiod_get_raw_value_cansleep(const struct gpio_desc * desc)451 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
452 {
453 	/* GPIO can never have been requested */
454 	WARN_ON(desc);
455 	return 0;
456 }
gpiod_get_raw_array_value_cansleep(unsigned int array_size,struct gpio_desc ** desc_array,struct gpio_array * array_info,unsigned long * value_bitmap)457 static inline int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
458 					       struct gpio_desc **desc_array,
459 					       struct gpio_array *array_info,
460 					       unsigned long *value_bitmap)
461 {
462 	/* GPIO can never have been requested */
463 	WARN_ON(desc_array);
464 	return 0;
465 }
gpiod_set_raw_value_cansleep(struct gpio_desc * desc,int value)466 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
467 						int value)
468 {
469 	/* GPIO can never have been requested */
470 	WARN_ON(desc);
471 }
gpiod_set_raw_array_value_cansleep(unsigned int array_size,struct gpio_desc ** desc_array,struct gpio_array * array_info,unsigned long * value_bitmap)472 static inline int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
473 						struct gpio_desc **desc_array,
474 						struct gpio_array *array_info,
475 						unsigned long *value_bitmap)
476 {
477 	/* GPIO can never have been requested */
478 	WARN_ON(desc_array);
479 	return 0;
480 }
481 
gpiod_set_config(struct gpio_desc * desc,unsigned long config)482 static inline int gpiod_set_config(struct gpio_desc *desc, unsigned long config)
483 {
484 	/* GPIO can never have been requested */
485 	WARN_ON(desc);
486 	return -ENOSYS;
487 }
488 
gpiod_set_debounce(struct gpio_desc * desc,unsigned int debounce)489 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce)
490 {
491 	/* GPIO can never have been requested */
492 	WARN_ON(desc);
493 	return -ENOSYS;
494 }
495 
gpiod_set_transitory(struct gpio_desc * desc,bool transitory)496 static inline int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
497 {
498 	/* GPIO can never have been requested */
499 	WARN_ON(desc);
500 	return -ENOSYS;
501 }
502 
gpiod_toggle_active_low(struct gpio_desc * desc)503 static inline void gpiod_toggle_active_low(struct gpio_desc *desc)
504 {
505 	/* GPIO can never have been requested */
506 	WARN_ON(desc);
507 }
508 
gpiod_is_active_low(const struct gpio_desc * desc)509 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
510 {
511 	/* GPIO can never have been requested */
512 	WARN_ON(desc);
513 	return 0;
514 }
gpiod_cansleep(const struct gpio_desc * desc)515 static inline int gpiod_cansleep(const struct gpio_desc *desc)
516 {
517 	/* GPIO can never have been requested */
518 	WARN_ON(desc);
519 	return 0;
520 }
521 
gpiod_to_irq(const struct gpio_desc * desc)522 static inline int gpiod_to_irq(const struct gpio_desc *desc)
523 {
524 	/* GPIO can never have been requested */
525 	WARN_ON(desc);
526 	return -EINVAL;
527 }
528 
gpiod_set_consumer_name(struct gpio_desc * desc,const char * name)529 static inline int gpiod_set_consumer_name(struct gpio_desc *desc,
530 					  const char *name)
531 {
532 	/* GPIO can never have been requested */
533 	WARN_ON(desc);
534 	return -EINVAL;
535 }
536 
gpio_to_desc(unsigned gpio)537 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
538 {
539 	return NULL;
540 }
541 
desc_to_gpio(const struct gpio_desc * desc)542 static inline int desc_to_gpio(const struct gpio_desc *desc)
543 {
544 	/* GPIO can never have been requested */
545 	WARN_ON(desc);
546 	return -EINVAL;
547 }
548 
549 /* Child properties interface */
550 struct fwnode_handle;
551 
552 static inline
fwnode_gpiod_get_index(struct fwnode_handle * fwnode,const char * con_id,int index,enum gpiod_flags flags,const char * label)553 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
554 					 const char *con_id, int index,
555 					 enum gpiod_flags flags,
556 					 const char *label)
557 {
558 	return ERR_PTR(-ENOSYS);
559 }
560 
561 static inline
devm_fwnode_gpiod_get_index(struct device * dev,struct fwnode_handle * fwnode,const char * con_id,int index,enum gpiod_flags flags,const char * label)562 struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
563 					      struct fwnode_handle *fwnode,
564 					      const char *con_id, int index,
565 					      enum gpiod_flags flags,
566 					      const char *label)
567 {
568 	return ERR_PTR(-ENOSYS);
569 }
570 
571 #endif /* CONFIG_GPIOLIB */
572 
573 static inline
devm_fwnode_gpiod_get(struct device * dev,struct fwnode_handle * fwnode,const char * con_id,enum gpiod_flags flags,const char * label)574 struct gpio_desc *devm_fwnode_gpiod_get(struct device *dev,
575 					struct fwnode_handle *fwnode,
576 					const char *con_id,
577 					enum gpiod_flags flags,
578 					const char *label)
579 {
580 	return devm_fwnode_gpiod_get_index(dev, fwnode, con_id, 0,
581 					   flags, label);
582 }
583 
584 static inline
devm_fwnode_get_index_gpiod_from_child(struct device * dev,const char * con_id,int index,struct fwnode_handle * child,enum gpiod_flags flags,const char * label)585 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
586 						const char *con_id, int index,
587 						struct fwnode_handle *child,
588 						enum gpiod_flags flags,
589 						const char *label)
590 {
591 	return devm_fwnode_gpiod_get_index(dev, child, con_id, index,
592 					   flags, label);
593 }
594 
595 static inline
devm_fwnode_get_gpiod_from_child(struct device * dev,const char * con_id,struct fwnode_handle * child,enum gpiod_flags flags,const char * label)596 struct gpio_desc *devm_fwnode_get_gpiod_from_child(struct device *dev,
597 						   const char *con_id,
598 						   struct fwnode_handle *child,
599 						   enum gpiod_flags flags,
600 						   const char *label)
601 {
602 	return devm_fwnode_gpiod_get_index(dev, child, con_id, 0, flags, label);
603 }
604 
605 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_OF_GPIO)
606 struct device_node;
607 
608 struct gpio_desc *gpiod_get_from_of_node(const struct device_node *node,
609 					 const char *propname, int index,
610 					 enum gpiod_flags dflags,
611 					 const char *label);
612 
613 #else  /* CONFIG_GPIOLIB && CONFIG_OF_GPIO */
614 
615 struct device_node;
616 
617 static inline
gpiod_get_from_of_node(const struct device_node * node,const char * propname,int index,enum gpiod_flags dflags,const char * label)618 struct gpio_desc *gpiod_get_from_of_node(const struct device_node *node,
619 					 const char *propname, int index,
620 					 enum gpiod_flags dflags,
621 					 const char *label)
622 {
623 	return ERR_PTR(-ENOSYS);
624 }
625 
626 #endif /* CONFIG_GPIOLIB && CONFIG_OF_GPIO */
627 
628 #ifdef CONFIG_GPIOLIB
629 struct device_node;
630 
631 struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
632 					      const struct device_node *node,
633 					      const char *propname, int index,
634 					      enum gpiod_flags dflags,
635 					      const char *label);
636 
637 #else  /* CONFIG_GPIOLIB */
638 
639 struct device_node;
640 
641 static inline
devm_gpiod_get_from_of_node(struct device * dev,const struct device_node * node,const char * propname,int index,enum gpiod_flags dflags,const char * label)642 struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
643 					      const struct device_node *node,
644 					      const char *propname, int index,
645 					      enum gpiod_flags dflags,
646 					      const char *label)
647 {
648 	return ERR_PTR(-ENOSYS);
649 }
650 
651 #endif /* CONFIG_GPIOLIB */
652 
653 struct acpi_gpio_params {
654 	unsigned int crs_entry_index;
655 	unsigned int line_index;
656 	bool active_low;
657 };
658 
659 struct acpi_gpio_mapping {
660 	const char *name;
661 	const struct acpi_gpio_params *data;
662 	unsigned int size;
663 
664 /* Ignore IoRestriction field */
665 #define ACPI_GPIO_QUIRK_NO_IO_RESTRICTION	BIT(0)
666 /*
667  * When ACPI GPIO mapping table is in use the index parameter inside it
668  * refers to the GPIO resource in _CRS method. That index has no
669  * distinction of actual type of the resource. When consumer wants to
670  * get GpioIo type explicitly, this quirk may be used.
671  */
672 #define ACPI_GPIO_QUIRK_ONLY_GPIOIO		BIT(1)
673 /* Use given pin as an absolute GPIO number in the system */
674 #define ACPI_GPIO_QUIRK_ABSOLUTE_NUMBER		BIT(2)
675 
676 	unsigned int quirks;
677 };
678 
679 struct acpi_device;
680 
681 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_ACPI)
682 
683 int acpi_dev_add_driver_gpios(struct acpi_device *adev,
684 			      const struct acpi_gpio_mapping *gpios);
685 void acpi_dev_remove_driver_gpios(struct acpi_device *adev);
686 
687 int devm_acpi_dev_add_driver_gpios(struct device *dev,
688 				   const struct acpi_gpio_mapping *gpios);
689 
690 struct gpio_desc *acpi_get_and_request_gpiod(char *path, unsigned int pin, char *label);
691 
692 #else  /* CONFIG_GPIOLIB && CONFIG_ACPI */
693 
acpi_dev_add_driver_gpios(struct acpi_device * adev,const struct acpi_gpio_mapping * gpios)694 static inline int acpi_dev_add_driver_gpios(struct acpi_device *adev,
695 			      const struct acpi_gpio_mapping *gpios)
696 {
697 	return -ENXIO;
698 }
acpi_dev_remove_driver_gpios(struct acpi_device * adev)699 static inline void acpi_dev_remove_driver_gpios(struct acpi_device *adev) {}
700 
devm_acpi_dev_add_driver_gpios(struct device * dev,const struct acpi_gpio_mapping * gpios)701 static inline int devm_acpi_dev_add_driver_gpios(struct device *dev,
702 			      const struct acpi_gpio_mapping *gpios)
703 {
704 	return -ENXIO;
705 }
706 
acpi_get_and_request_gpiod(char * path,unsigned int pin,char * label)707 static inline struct gpio_desc *acpi_get_and_request_gpiod(char *path, unsigned int pin,
708 							   char *label)
709 {
710 	return ERR_PTR(-ENOSYS);
711 }
712 
713 #endif /* CONFIG_GPIOLIB && CONFIG_ACPI */
714 
715 
716 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
717 
718 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
719 int gpiod_export_link(struct device *dev, const char *name,
720 		      struct gpio_desc *desc);
721 void gpiod_unexport(struct gpio_desc *desc);
722 
723 #else  /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
724 
gpiod_export(struct gpio_desc * desc,bool direction_may_change)725 static inline int gpiod_export(struct gpio_desc *desc,
726 			       bool direction_may_change)
727 {
728 	return -ENOSYS;
729 }
730 
gpiod_export_link(struct device * dev,const char * name,struct gpio_desc * desc)731 static inline int gpiod_export_link(struct device *dev, const char *name,
732 				    struct gpio_desc *desc)
733 {
734 	return -ENOSYS;
735 }
736 
gpiod_unexport(struct gpio_desc * desc)737 static inline void gpiod_unexport(struct gpio_desc *desc)
738 {
739 }
740 
741 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
742 
743 #endif
744