Blame view

include/asm-generic/gpio.h 7.98 KB
4c20386c8   David Brownell   [PATCH] GPIO core
1
2
  #ifndef _ASM_GENERIC_GPIO_H
  #define _ASM_GENERIC_GPIO_H
b3db4a8ad   Mike Frysinger   asm-generic/gpio....
3
  #include <linux/kernel.h>
6ea0205b5   David Brownell   gpio: build fixes
4
  #include <linux/types.h>
25947d5ac   Atsushi Nemoto   gpio: fix build o...
5
  #include <linux/errno.h>
15c9a0acc   Grant Likely   of: create of_pha...
6
  #include <linux/of.h>
6ea0205b5   David Brownell   gpio: build fixes
7

7444a72ef   Michael Buesch   gpiolib: allow us...
8
  #ifdef CONFIG_GPIOLIB
d2876d08d   David Brownell   gpiolib: add gpio...
9

6ea0205b5   David Brownell   gpio: build fixes
10
  #include <linux/compiler.h>
d2876d08d   David Brownell   gpiolib: add gpio...
11
12
13
14
15
16
  /* Platforms may implement their GPIO interface with library code,
   * at a small performance cost for non-inlined operations and some
   * extra memory (for code and for per-GPIO table entries).
   *
   * While the GPIO programming interface defines valid GPIO numbers
   * to be in the range 0..MAX_INT, this library restricts them to the
6a9436d0c   Michael Buesch   gpiolib: fix typo...
17
   * smaller range 0..ARCH_NR_GPIOS-1.
c956126c1   David Brownell   gpio: doc updates
18
19
20
21
   *
   * ARCH_NR_GPIOS is somewhat arbitrary; it usually reflects the sum of
   * builtin/SoC GPIOs plus a number of GPIOs on expanders; the latter is
   * actually an estimate of a board-specific value.
d2876d08d   David Brownell   gpiolib: add gpio...
22
23
24
25
26
   */
  
  #ifndef ARCH_NR_GPIOS
  #define ARCH_NR_GPIOS		256
  #endif
c956126c1   David Brownell   gpio: doc updates
27
28
29
30
31
32
33
34
  /*
   * "valid" GPIO numbers are nonnegative and may be passed to
   * setup routines like gpio_request().  only some valid numbers
   * can successfully be requested and used.
   *
   * Invalid GPIO numbers are useful for indicating no-such-GPIO in
   * platform data and other tables.
   */
3474cb3cc   Joe Perches   gpio: Convert gpi...
35
  static inline bool gpio_is_valid(int number)
e6de1808f   Guennadi Liakhovetski   gpio: define gpio...
36
  {
3474cb3cc   Joe Perches   gpio: Convert gpi...
37
  	return number >= 0 && number < ARCH_NR_GPIOS;
e6de1808f   Guennadi Liakhovetski   gpio: define gpio...
38
  }
1f018c8d0   Mike Frysinger   asm-generic/gpio....
39
  struct device;
feb836992   Mark Brown   gpiolib: Ensure s...
40
  struct gpio;
d2876d08d   David Brownell   gpiolib: add gpio...
41
  struct seq_file;
438d8908b   Guennadi Liakhovetski   gpiolib: better r...
42
  struct module;
a19e3da5b   Anton Vorontsov   of/gpio: Kill of_...
43
  struct device_node;
d2876d08d   David Brownell   gpiolib: add gpio...
44
45
46
47
  
  /**
   * struct gpio_chip - abstract a GPIO controller
   * @label: for diagnostics
d8f388d8d   David Brownell   gpio: sysfs inter...
48
49
   * @dev: optional device providing the GPIOs
   * @owner: helps prevent removal of modules exporting active GPIOs
35e8bb517   David Brownell   gpiolib: request/...
50
51
52
53
   * @request: optional hook for chip-specific activation, such as
   *	enabling module power and clock; may sleep
   * @free: optional hook for chip-specific deactivation, such as
   *	disabling module power and clock; may sleep
d2876d08d   David Brownell   gpiolib: add gpio...
54
55
56
57
58
   * @direction_input: configures signal "offset" as input, or returns error
   * @get: returns value for signal "offset"; for output signals this
   *	returns either the value actually sensed, or zero
   * @direction_output: configures signal "offset" as output, or returns error
   * @set: assigns output value for signal "offset"
0f6d504e7   David Brownell   gpiolib: gpio_to_...
59
60
   * @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
   *	implementation may not sleep
d2876d08d   David Brownell   gpiolib: add gpio...
61
62
63
64
65
66
67
68
69
   * @dbg_show: optional routine to show contents in debugfs; default code
   *	will be used when this is omitted, but custom code can show extra
   *	state (such as pullup/pulldown configuration).
   * @base: identifies the first GPIO number handled by this chip; or, if
   *	negative during registration, requests dynamic ID allocation.
   * @ngpio: the number of GPIOs handled by this controller; the last GPIO
   *	handled is (base + ngpio - 1).
   * @can_sleep: flag must be set iff get()/set() methods sleep, as they
   *	must while accessing GPIO expander chips over I2C or SPI
926b663ce   Daniel Silverstone   gpiolib: allow GP...
70
71
72
   * @names: if set, must be an array of strings to use as alternative
   *      names for the GPIOs in this chip. Any entry in the array
   *      may be NULL if there is no alias for the GPIO, however the
7839ec782   Uwe Kleine-König   gpiolib: document...
73
74
75
   *      array must be @ngpio entries long.  A name can include a single printk
   *      format specifier for an unsigned int.  It is substituted by the actual
   *      number of the gpio.
d2876d08d   David Brownell   gpiolib: add gpio...
76
77
78
79
80
81
82
83
84
85
86
87
   *
   * A gpio_chip can help platforms abstract various sources of GPIOs so
   * they can all be accessed through a common programing interface.
   * Example sources would be SOC controllers, FPGAs, multifunction
   * chips, dedicated GPIO expanders, and so on.
   *
   * Each chip controls a number of signals, identified in method calls
   * by "offset" values in the range 0..(@ngpio - 1).  When those signals
   * are referenced through calls like gpio_get_value(gpio), the offset
   * is calculated by subtracting @base from the gpio number.
   */
  struct gpio_chip {
4fd5463c4   Dmitry Eremin-Solenikov   gpio: make gpioch...
88
  	const char		*label;
d8f388d8d   David Brownell   gpio: sysfs inter...
89
  	struct device		*dev;
438d8908b   Guennadi Liakhovetski   gpiolib: better r...
90
  	struct module		*owner;
d2876d08d   David Brownell   gpiolib: add gpio...
91

35e8bb517   David Brownell   gpiolib: request/...
92
93
94
95
  	int			(*request)(struct gpio_chip *chip,
  						unsigned offset);
  	void			(*free)(struct gpio_chip *chip,
  						unsigned offset);
d2876d08d   David Brownell   gpiolib: add gpio...
96
97
98
99
100
101
  	int			(*direction_input)(struct gpio_chip *chip,
  						unsigned offset);
  	int			(*get)(struct gpio_chip *chip,
  						unsigned offset);
  	int			(*direction_output)(struct gpio_chip *chip,
  						unsigned offset, int value);
c4b5be98f   Felipe Balbi   gpiolib: introduc...
102
103
  	int			(*set_debounce)(struct gpio_chip *chip,
  						unsigned offset, unsigned debounce);
d2876d08d   David Brownell   gpiolib: add gpio...
104
105
  	void			(*set)(struct gpio_chip *chip,
  						unsigned offset, int value);
0f6d504e7   David Brownell   gpiolib: gpio_to_...
106
107
108
  
  	int			(*to_irq)(struct gpio_chip *chip,
  						unsigned offset);
d2876d08d   David Brownell   gpiolib: add gpio...
109
110
111
112
  	void			(*dbg_show)(struct seq_file *s,
  						struct gpio_chip *chip);
  	int			base;
  	u16			ngpio;
62154991a   Uwe Kleine-König   gpiolib: make nam...
113
  	const char		*const *names;
d2876d08d   David Brownell   gpiolib: add gpio...
114
  	unsigned		can_sleep:1;
d8f388d8d   David Brownell   gpio: sysfs inter...
115
  	unsigned		exported:1;
a19e3da5b   Anton Vorontsov   of/gpio: Kill of_...
116
117
118
119
120
121
122
123
  
  #if defined(CONFIG_OF_GPIO)
  	/*
  	 * If CONFIG_OF is enabled, then all GPIO controllers described in the
  	 * device tree automatically may have an OF translation
  	 */
  	struct device_node *of_node;
  	int of_gpio_n_cells;
15c9a0acc   Grant Likely   of: create of_pha...
124
125
  	int (*of_xlate)(struct gpio_chip *gc,
  		        const struct of_phandle_args *gpiospec, u32 *flags);
a19e3da5b   Anton Vorontsov   of/gpio: Kill of_...
126
  #endif
d2876d08d   David Brownell   gpiolib: add gpio...
127
128
129
130
  };
  
  extern const char *gpiochip_is_requested(struct gpio_chip *chip,
  			unsigned offset);
1a2d397a6   Grant Likely   gpio/powerpc: Eli...
131
  extern struct gpio_chip *gpio_to_chip(unsigned gpio);
6ea0205b5   David Brownell   gpio: build fixes
132
  extern int __must_check gpiochip_reserve(int start, int ngpio);
d2876d08d   David Brownell   gpiolib: add gpio...
133
134
135
136
  
  /* add/remove chips */
  extern int gpiochip_add(struct gpio_chip *chip);
  extern int __must_check gpiochip_remove(struct gpio_chip *chip);
594fa265e   Grant Likely   of/gpio: stop usi...
137
138
139
  extern struct gpio_chip *gpiochip_find(void *data,
  					int (*match)(struct gpio_chip *chip,
  						     void *data));
d2876d08d   David Brownell   gpiolib: add gpio...
140
141
142
143
144
  
  
  /* Always use the library code for GPIO management calls,
   * or when sleeping may be involved.
   */
d8a3515e2   Linus Torvalds   Revert "gpiolib: ...
145
  extern int gpio_request(unsigned gpio, const char *label);
d2876d08d   David Brownell   gpiolib: add gpio...
146
  extern void gpio_free(unsigned gpio);
d8a3515e2   Linus Torvalds   Revert "gpiolib: ...
147
148
  extern int gpio_direction_input(unsigned gpio);
  extern int gpio_direction_output(unsigned gpio, int value);
d2876d08d   David Brownell   gpiolib: add gpio...
149

c4b5be98f   Felipe Balbi   gpiolib: introduc...
150
  extern int gpio_set_debounce(unsigned gpio, unsigned debounce);
d2876d08d   David Brownell   gpiolib: add gpio...
151
152
153
154
155
156
157
158
159
160
161
162
  extern int gpio_get_value_cansleep(unsigned gpio);
  extern void gpio_set_value_cansleep(unsigned gpio, int value);
  
  
  /* A platform's <asm/gpio.h> code may want to inline the I/O calls when
   * the GPIO is constant and refers to some always-present controller,
   * giving direct access to chip registers and tight bitbanging loops.
   */
  extern int __gpio_get_value(unsigned gpio);
  extern void __gpio_set_value(unsigned gpio, int value);
  
  extern int __gpio_cansleep(unsigned gpio);
0f6d504e7   David Brownell   gpiolib: gpio_to_...
163
  extern int __gpio_to_irq(unsigned gpio);
d2876d08d   David Brownell   gpiolib: add gpio...
164

d8a3515e2   Linus Torvalds   Revert "gpiolib: ...
165
  extern int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);
7c295975a   Lars-Peter Clausen   gpio: make gpio_{...
166
167
  extern int gpio_request_array(const struct gpio *array, size_t num);
  extern void gpio_free_array(const struct gpio *array, size_t num);
3e45f1d11   Eric Miao   gpio: introduce g...
168

1a0703ede   John Crispin   GPIO: add binding...
169
170
171
  /* bindings for managed devices that want to request gpios */
  int devm_gpio_request(struct device *dev, unsigned gpio, const char *label);
  void devm_gpio_free(struct device *dev, unsigned int gpio);
d8f388d8d   David Brownell   gpio: sysfs inter...
172
173
174
175
176
177
178
  #ifdef CONFIG_GPIO_SYSFS
  
  /*
   * A sysfs interface can be exported by individual drivers if they want,
   * but more typically is configured entirely from userspace.
   */
  extern int gpio_export(unsigned gpio, bool direction_may_change);
a4177ee7f   Jani Nikula   gpiolib: allow ex...
179
180
  extern int gpio_export_link(struct device *dev, const char *name,
  			unsigned gpio);
076974618   Jani Nikula   gpiolib: add supp...
181
  extern int gpio_sysfs_set_active_low(unsigned gpio, int value);
d8f388d8d   David Brownell   gpio: sysfs inter...
182
183
184
  extern void gpio_unexport(unsigned gpio);
  
  #endif	/* CONFIG_GPIO_SYSFS */
09cd9527d   Anton Vorontsov   gpiolib: fix HAVE...
185
  #else	/* !CONFIG_GPIOLIB */
d2876d08d   David Brownell   gpiolib: add gpio...
186

3474cb3cc   Joe Perches   gpio: Convert gpi...
187
  static inline bool gpio_is_valid(int number)
e6de1808f   Guennadi Liakhovetski   gpio: define gpio...
188
189
190
191
  {
  	/* only non-negative numbers are valid */
  	return number >= 0;
  }
4c20386c8   David Brownell   [PATCH] GPIO core
192
193
194
195
196
197
198
199
200
201
202
203
  /* platforms that don't directly support access to GPIOs through I2C, SPI,
   * or other blocking infrastructure can use these wrappers.
   */
  
  static inline int gpio_cansleep(unsigned gpio)
  {
  	return 0;
  }
  
  static inline int gpio_get_value_cansleep(unsigned gpio)
  {
  	might_sleep();
eb9ae7f2a   Hamo   gpio: fix build e...
204
  	return __gpio_get_value(gpio);
4c20386c8   David Brownell   [PATCH] GPIO core
205
206
207
208
209
  }
  
  static inline void gpio_set_value_cansleep(unsigned gpio, int value)
  {
  	might_sleep();
eb9ae7f2a   Hamo   gpio: fix build e...
210
  	__gpio_set_value(gpio, value);
4c20386c8   David Brownell   [PATCH] GPIO core
211
  }
09cd9527d   Anton Vorontsov   gpiolib: fix HAVE...
212
  #endif /* !CONFIG_GPIOLIB */
d8f388d8d   David Brownell   gpio: sysfs inter...
213
214
  
  #ifndef CONFIG_GPIO_SYSFS
1f018c8d0   Mike Frysinger   asm-generic/gpio....
215
  struct device;
d8f388d8d   David Brownell   gpio: sysfs inter...
216
217
218
219
220
221
  /* sysfs support is only available with gpiolib, where it's optional */
  
  static inline int gpio_export(unsigned gpio, bool direction_may_change)
  {
  	return -ENOSYS;
  }
a4177ee7f   Jani Nikula   gpiolib: allow ex...
222
223
224
225
226
  static inline int gpio_export_link(struct device *dev, const char *name,
  				unsigned gpio)
  {
  	return -ENOSYS;
  }
076974618   Jani Nikula   gpiolib: add supp...
227
228
229
230
  static inline int gpio_sysfs_set_active_low(unsigned gpio, int value)
  {
  	return -ENOSYS;
  }
d8f388d8d   David Brownell   gpio: sysfs inter...
231
232
233
234
  static inline void gpio_unexport(unsigned gpio)
  {
  }
  #endif	/* CONFIG_GPIO_SYSFS */
d2876d08d   David Brownell   gpiolib: add gpio...
235

4c20386c8   David Brownell   [PATCH] GPIO core
236
  #endif /* _ASM_GENERIC_GPIO_H */