Commit | Line | Data |
---|---|---|
4c20386c DB |
1 | #ifndef _ASM_GENERIC_GPIO_H |
2 | #define _ASM_GENERIC_GPIO_H | |
3 | ||
6ea0205b | 4 | #include <linux/types.h> |
25947d5a | 5 | #include <linux/errno.h> |
6ea0205b | 6 | |
7444a72e | 7 | #ifdef CONFIG_GPIOLIB |
d2876d08 | 8 | |
6ea0205b DB |
9 | #include <linux/compiler.h> |
10 | ||
d2876d08 DB |
11 | /* Platforms may implement their GPIO interface with library code, |
12 | * at a small performance cost for non-inlined operations and some | |
13 | * extra memory (for code and for per-GPIO table entries). | |
14 | * | |
15 | * While the GPIO programming interface defines valid GPIO numbers | |
16 | * to be in the range 0..MAX_INT, this library restricts them to the | |
6a9436d0 | 17 | * smaller range 0..ARCH_NR_GPIOS-1. |
d2876d08 DB |
18 | */ |
19 | ||
20 | #ifndef ARCH_NR_GPIOS | |
21 | #define ARCH_NR_GPIOS 256 | |
22 | #endif | |
23 | ||
e6de1808 GL |
24 | static inline int gpio_is_valid(int number) |
25 | { | |
26 | /* only some non-negative numbers are valid */ | |
27 | return ((unsigned)number) < ARCH_NR_GPIOS; | |
28 | } | |
29 | ||
d2876d08 | 30 | struct seq_file; |
438d8908 | 31 | struct module; |
d2876d08 DB |
32 | |
33 | /** | |
34 | * struct gpio_chip - abstract a GPIO controller | |
35 | * @label: for diagnostics | |
d8f388d8 DB |
36 | * @dev: optional device providing the GPIOs |
37 | * @owner: helps prevent removal of modules exporting active GPIOs | |
35e8bb51 DB |
38 | * @request: optional hook for chip-specific activation, such as |
39 | * enabling module power and clock; may sleep | |
40 | * @free: optional hook for chip-specific deactivation, such as | |
41 | * disabling module power and clock; may sleep | |
d2876d08 DB |
42 | * @direction_input: configures signal "offset" as input, or returns error |
43 | * @get: returns value for signal "offset"; for output signals this | |
44 | * returns either the value actually sensed, or zero | |
45 | * @direction_output: configures signal "offset" as output, or returns error | |
46 | * @set: assigns output value for signal "offset" | |
0f6d504e DB |
47 | * @to_irq: optional hook supporting non-static gpio_to_irq() mappings; |
48 | * implementation may not sleep | |
d2876d08 DB |
49 | * @dbg_show: optional routine to show contents in debugfs; default code |
50 | * will be used when this is omitted, but custom code can show extra | |
51 | * state (such as pullup/pulldown configuration). | |
52 | * @base: identifies the first GPIO number handled by this chip; or, if | |
53 | * negative during registration, requests dynamic ID allocation. | |
54 | * @ngpio: the number of GPIOs handled by this controller; the last GPIO | |
55 | * handled is (base + ngpio - 1). | |
56 | * @can_sleep: flag must be set iff get()/set() methods sleep, as they | |
57 | * must while accessing GPIO expander chips over I2C or SPI | |
58 | * | |
59 | * A gpio_chip can help platforms abstract various sources of GPIOs so | |
60 | * they can all be accessed through a common programing interface. | |
61 | * Example sources would be SOC controllers, FPGAs, multifunction | |
62 | * chips, dedicated GPIO expanders, and so on. | |
63 | * | |
64 | * Each chip controls a number of signals, identified in method calls | |
65 | * by "offset" values in the range 0..(@ngpio - 1). When those signals | |
66 | * are referenced through calls like gpio_get_value(gpio), the offset | |
67 | * is calculated by subtracting @base from the gpio number. | |
68 | */ | |
69 | struct gpio_chip { | |
4fd5463c | 70 | const char *label; |
d8f388d8 | 71 | struct device *dev; |
438d8908 | 72 | struct module *owner; |
d2876d08 | 73 | |
35e8bb51 DB |
74 | int (*request)(struct gpio_chip *chip, |
75 | unsigned offset); | |
76 | void (*free)(struct gpio_chip *chip, | |
77 | unsigned offset); | |
78 | ||
d2876d08 DB |
79 | int (*direction_input)(struct gpio_chip *chip, |
80 | unsigned offset); | |
81 | int (*get)(struct gpio_chip *chip, | |
82 | unsigned offset); | |
83 | int (*direction_output)(struct gpio_chip *chip, | |
84 | unsigned offset, int value); | |
85 | void (*set)(struct gpio_chip *chip, | |
86 | unsigned offset, int value); | |
0f6d504e DB |
87 | |
88 | int (*to_irq)(struct gpio_chip *chip, | |
89 | unsigned offset); | |
90 | ||
d2876d08 DB |
91 | void (*dbg_show)(struct seq_file *s, |
92 | struct gpio_chip *chip); | |
93 | int base; | |
94 | u16 ngpio; | |
95 | unsigned can_sleep:1; | |
d8f388d8 | 96 | unsigned exported:1; |
d2876d08 DB |
97 | }; |
98 | ||
99 | extern const char *gpiochip_is_requested(struct gpio_chip *chip, | |
100 | unsigned offset); | |
6ea0205b | 101 | extern int __must_check gpiochip_reserve(int start, int ngpio); |
d2876d08 DB |
102 | |
103 | /* add/remove chips */ | |
104 | extern int gpiochip_add(struct gpio_chip *chip); | |
105 | extern int __must_check gpiochip_remove(struct gpio_chip *chip); | |
106 | ||
107 | ||
108 | /* Always use the library code for GPIO management calls, | |
109 | * or when sleeping may be involved. | |
110 | */ | |
111 | extern int gpio_request(unsigned gpio, const char *label); | |
112 | extern void gpio_free(unsigned gpio); | |
113 | ||
114 | extern int gpio_direction_input(unsigned gpio); | |
115 | extern int gpio_direction_output(unsigned gpio, int value); | |
116 | ||
117 | extern int gpio_get_value_cansleep(unsigned gpio); | |
118 | extern void gpio_set_value_cansleep(unsigned gpio, int value); | |
119 | ||
120 | ||
121 | /* A platform's <asm/gpio.h> code may want to inline the I/O calls when | |
122 | * the GPIO is constant and refers to some always-present controller, | |
123 | * giving direct access to chip registers and tight bitbanging loops. | |
124 | */ | |
125 | extern int __gpio_get_value(unsigned gpio); | |
126 | extern void __gpio_set_value(unsigned gpio, int value); | |
127 | ||
128 | extern int __gpio_cansleep(unsigned gpio); | |
129 | ||
0f6d504e | 130 | extern int __gpio_to_irq(unsigned gpio); |
d2876d08 | 131 | |
d8f388d8 DB |
132 | #ifdef CONFIG_GPIO_SYSFS |
133 | ||
134 | /* | |
135 | * A sysfs interface can be exported by individual drivers if they want, | |
136 | * but more typically is configured entirely from userspace. | |
137 | */ | |
138 | extern int gpio_export(unsigned gpio, bool direction_may_change); | |
139 | extern void gpio_unexport(unsigned gpio); | |
140 | ||
141 | #endif /* CONFIG_GPIO_SYSFS */ | |
142 | ||
143 | #else /* !CONFIG_HAVE_GPIO_LIB */ | |
d2876d08 | 144 | |
e6de1808 GL |
145 | static inline int gpio_is_valid(int number) |
146 | { | |
147 | /* only non-negative numbers are valid */ | |
148 | return number >= 0; | |
149 | } | |
150 | ||
4c20386c DB |
151 | /* platforms that don't directly support access to GPIOs through I2C, SPI, |
152 | * or other blocking infrastructure can use these wrappers. | |
153 | */ | |
154 | ||
155 | static inline int gpio_cansleep(unsigned gpio) | |
156 | { | |
157 | return 0; | |
158 | } | |
159 | ||
160 | static inline int gpio_get_value_cansleep(unsigned gpio) | |
161 | { | |
162 | might_sleep(); | |
163 | return gpio_get_value(gpio); | |
164 | } | |
165 | ||
166 | static inline void gpio_set_value_cansleep(unsigned gpio, int value) | |
167 | { | |
168 | might_sleep(); | |
169 | gpio_set_value(gpio, value); | |
170 | } | |
171 | ||
d8f388d8 DB |
172 | #endif /* !CONFIG_HAVE_GPIO_LIB */ |
173 | ||
174 | #ifndef CONFIG_GPIO_SYSFS | |
175 | ||
176 | /* sysfs support is only available with gpiolib, where it's optional */ | |
177 | ||
178 | static inline int gpio_export(unsigned gpio, bool direction_may_change) | |
179 | { | |
180 | return -ENOSYS; | |
181 | } | |
182 | ||
183 | static inline void gpio_unexport(unsigned gpio) | |
184 | { | |
185 | } | |
186 | #endif /* CONFIG_GPIO_SYSFS */ | |
d2876d08 | 187 | |
4c20386c | 188 | #endif /* _ASM_GENERIC_GPIO_H */ |