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