KVM: arm64: Slightly optimize flush_context()
[platform/kernel/linux-starfive.git] / include / asm-generic / gpio.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_GENERIC_GPIO_H
3 #define _ASM_GENERIC_GPIO_H
4
5 #include <linux/types.h>
6 #include <linux/errno.h>
7
8 #ifdef CONFIG_GPIOLIB
9
10 #include <linux/compiler.h>
11 #include <linux/gpio/driver.h>
12 #include <linux/gpio/consumer.h>
13
14 /*
15  * Platforms may implement their GPIO interface with library code,
16  * at a small performance cost for non-inlined operations and some
17  * extra memory (for code and for per-GPIO table entries).
18  */
19
20 /*
21  * At the end we want all GPIOs to be dynamically allocated from 0.
22  * However, some legacy drivers still perform fixed allocation.
23  * Until they are all fixed, leave 0-512 space for them.
24  */
25 #define GPIO_DYNAMIC_BASE       512
26
27 struct device;
28 struct gpio;
29 struct seq_file;
30 struct module;
31 struct device_node;
32 struct gpio_desc;
33
34 /* Always use the library code for GPIO management calls,
35  * or when sleeping may be involved.
36  */
37 extern int gpio_request(unsigned gpio, const char *label);
38 extern void gpio_free(unsigned gpio);
39
40 static inline int gpio_direction_input(unsigned gpio)
41 {
42         return gpiod_direction_input(gpio_to_desc(gpio));
43 }
44 static inline int gpio_direction_output(unsigned gpio, int value)
45 {
46         return gpiod_direction_output_raw(gpio_to_desc(gpio), value);
47 }
48
49 static inline int gpio_set_debounce(unsigned gpio, unsigned debounce)
50 {
51         return gpiod_set_debounce(gpio_to_desc(gpio), debounce);
52 }
53
54 static inline int gpio_get_value_cansleep(unsigned gpio)
55 {
56         return gpiod_get_raw_value_cansleep(gpio_to_desc(gpio));
57 }
58 static inline void gpio_set_value_cansleep(unsigned gpio, int value)
59 {
60         return gpiod_set_raw_value_cansleep(gpio_to_desc(gpio), value);
61 }
62
63
64 /* A platform's <asm/gpio.h> code may want to inline the I/O calls when
65  * the GPIO is constant and refers to some always-present controller,
66  * giving direct access to chip registers and tight bitbanging loops.
67  */
68 static inline int __gpio_get_value(unsigned gpio)
69 {
70         return gpiod_get_raw_value(gpio_to_desc(gpio));
71 }
72 static inline void __gpio_set_value(unsigned gpio, int value)
73 {
74         return gpiod_set_raw_value(gpio_to_desc(gpio), value);
75 }
76
77 static inline int __gpio_cansleep(unsigned gpio)
78 {
79         return gpiod_cansleep(gpio_to_desc(gpio));
80 }
81
82 static inline int __gpio_to_irq(unsigned gpio)
83 {
84         return gpiod_to_irq(gpio_to_desc(gpio));
85 }
86
87 extern int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);
88 extern int gpio_request_array(const struct gpio *array, size_t num);
89 extern void gpio_free_array(const struct gpio *array, size_t num);
90
91 /*
92  * A sysfs interface can be exported by individual drivers if they want,
93  * but more typically is configured entirely from userspace.
94  */
95 static inline int gpio_export(unsigned gpio, bool direction_may_change)
96 {
97         return gpiod_export(gpio_to_desc(gpio), direction_may_change);
98 }
99
100 static inline void gpio_unexport(unsigned gpio)
101 {
102         gpiod_unexport(gpio_to_desc(gpio));
103 }
104
105 #else   /* !CONFIG_GPIOLIB */
106
107 #include <linux/kernel.h>
108
109 /* platforms that don't directly support access to GPIOs through I2C, SPI,
110  * or other blocking infrastructure can use these wrappers.
111  */
112
113 static inline int gpio_cansleep(unsigned gpio)
114 {
115         return 0;
116 }
117
118 static inline int gpio_get_value_cansleep(unsigned gpio)
119 {
120         might_sleep();
121         return __gpio_get_value(gpio);
122 }
123
124 static inline void gpio_set_value_cansleep(unsigned gpio, int value)
125 {
126         might_sleep();
127         __gpio_set_value(gpio, value);
128 }
129
130 #endif /* !CONFIG_GPIOLIB */
131
132 /*
133  * "valid" GPIO numbers are nonnegative and may be passed to
134  * setup routines like gpio_request().  only some valid numbers
135  * can successfully be requested and used.
136  *
137  * Invalid GPIO numbers are useful for indicating no-such-GPIO in
138  * platform data and other tables.
139  */
140
141 static inline bool gpio_is_valid(int number)
142 {
143         /* only non-negative numbers are valid */
144         return number >= 0;
145 }
146
147 #endif /* _ASM_GENERIC_GPIO_H */