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