Blame view

include/asm-generic/gpio.h 4.45 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  /* SPDX-License-Identifier: GPL-2.0 */
4c20386c8   David Brownell   [PATCH] GPIO core
2
3
  #ifndef _ASM_GENERIC_GPIO_H
  #define _ASM_GENERIC_GPIO_H
b3db4a8ad   Mike Frysinger   asm-generic/gpio....
4
  #include <linux/kernel.h>
6ea0205b5   David Brownell   gpio: build fixes
5
  #include <linux/types.h>
25947d5ac   Atsushi Nemoto   gpio: fix build o...
6
  #include <linux/errno.h>
15c9a0acc   Grant Likely   of: create of_pha...
7
  #include <linux/of.h>
6ea0205b5   David Brownell   gpio: build fixes
8

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

6ea0205b5   David Brownell   gpio: build fixes
11
  #include <linux/compiler.h>
79a9becda   Alexandre Courbot   gpiolib: export d...
12
13
  #include <linux/gpio/driver.h>
  #include <linux/gpio/consumer.h>
6ea0205b5   David Brownell   gpio: build fixes
14

d2876d08d   David Brownell   gpiolib: add gpio...
15
16
17
18
19
20
  /* 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...
21
   * smaller range 0..ARCH_NR_GPIOS-1.
c956126c1   David Brownell   gpio: doc updates
22
23
24
25
   *
   * 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...
26
27
28
   */
  
  #ifndef ARCH_NR_GPIOS
aa6aedb54   Arnd Bergmann   gpio: allow setti...
29
30
31
  #if defined(CONFIG_ARCH_NR_GPIO) && CONFIG_ARCH_NR_GPIO > 0
  #define ARCH_NR_GPIOS CONFIG_ARCH_NR_GPIO
  #else
7ca267fab   Mika Westerberg   gpio: Increase AR...
32
  #define ARCH_NR_GPIOS		512
d2876d08d   David Brownell   gpiolib: add gpio...
33
  #endif
aa6aedb54   Arnd Bergmann   gpio: allow setti...
34
  #endif
d2876d08d   David Brownell   gpiolib: add gpio...
35

c956126c1   David Brownell   gpio: doc updates
36
37
38
39
40
41
42
43
  /*
   * "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...
44
  static inline bool gpio_is_valid(int number)
e6de1808f   Guennadi Liakhovetski   gpio: define gpio...
45
  {
3474cb3cc   Joe Perches   gpio: Convert gpi...
46
  	return number >= 0 && number < ARCH_NR_GPIOS;
e6de1808f   Guennadi Liakhovetski   gpio: define gpio...
47
  }
1f018c8d0   Mike Frysinger   asm-generic/gpio....
48
  struct device;
feb836992   Mark Brown   gpiolib: Ensure s...
49
  struct gpio;
d2876d08d   David Brownell   gpiolib: add gpio...
50
  struct seq_file;
438d8908b   Guennadi Liakhovetski   gpiolib: better r...
51
  struct module;
a19e3da5b   Anton Vorontsov   of/gpio: Kill of_...
52
  struct device_node;
6c0b4e6c8   Alexandre Courbot   gpiolib: let gpio...
53
  struct gpio_desc;
d2876d08d   David Brownell   gpiolib: add gpio...
54

79a9becda   Alexandre Courbot   gpiolib: export d...
55
56
57
58
59
  /* caller holds gpio_lock *OR* gpio is marked as requested */
  static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
  {
  	return gpiod_to_chip(gpio_to_desc(gpio));
  }
d2876d08d   David Brownell   gpiolib: add gpio...
60
61
62
63
  
  /* Always use the library code for GPIO management calls,
   * or when sleeping may be involved.
   */
d8a3515e2   Linus Torvalds   Revert "gpiolib: ...
64
  extern int gpio_request(unsigned gpio, const char *label);
d2876d08d   David Brownell   gpiolib: add gpio...
65
  extern void gpio_free(unsigned gpio);
c7caf8682   Alexandre Courbot   gpio: remove gpio...
66
67
68
69
70
71
72
73
  static inline int gpio_direction_input(unsigned gpio)
  {
  	return gpiod_direction_input(gpio_to_desc(gpio));
  }
  static inline int gpio_direction_output(unsigned gpio, int value)
  {
  	return gpiod_direction_output_raw(gpio_to_desc(gpio), value);
  }
d2876d08d   David Brownell   gpiolib: add gpio...
74

c7caf8682   Alexandre Courbot   gpio: remove gpio...
75
76
77
78
  static inline int gpio_set_debounce(unsigned gpio, unsigned debounce)
  {
  	return gpiod_set_debounce(gpio_to_desc(gpio), debounce);
  }
c4b5be98f   Felipe Balbi   gpiolib: introduc...
79

79a9becda   Alexandre Courbot   gpiolib: export d...
80
81
82
83
84
85
86
87
  static inline int gpio_get_value_cansleep(unsigned gpio)
  {
  	return gpiod_get_raw_value_cansleep(gpio_to_desc(gpio));
  }
  static inline void gpio_set_value_cansleep(unsigned gpio, int value)
  {
  	return gpiod_set_raw_value_cansleep(gpio_to_desc(gpio), value);
  }
d2876d08d   David Brownell   gpiolib: add gpio...
88
89
90
91
92
93
  
  
  /* 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.
   */
79a9becda   Alexandre Courbot   gpiolib: export d...
94
95
96
97
98
99
100
101
  static inline int __gpio_get_value(unsigned gpio)
  {
  	return gpiod_get_raw_value(gpio_to_desc(gpio));
  }
  static inline void __gpio_set_value(unsigned gpio, int value)
  {
  	return gpiod_set_raw_value(gpio_to_desc(gpio), value);
  }
d2876d08d   David Brownell   gpiolib: add gpio...
102

79a9becda   Alexandre Courbot   gpiolib: export d...
103
104
105
106
  static inline int __gpio_cansleep(unsigned gpio)
  {
  	return gpiod_cansleep(gpio_to_desc(gpio));
  }
d2876d08d   David Brownell   gpiolib: add gpio...
107

79a9becda   Alexandre Courbot   gpiolib: export d...
108
109
110
111
  static inline int __gpio_to_irq(unsigned gpio)
  {
  	return gpiod_to_irq(gpio_to_desc(gpio));
  }
d2876d08d   David Brownell   gpiolib: add gpio...
112

d8a3515e2   Linus Torvalds   Revert "gpiolib: ...
113
  extern int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);
7c295975a   Lars-Peter Clausen   gpio: make gpio_{...
114
115
  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...
116

d8f388d8d   David Brownell   gpio: sysfs inter...
117
118
119
120
  /*
   * A sysfs interface can be exported by individual drivers if they want,
   * but more typically is configured entirely from userspace.
   */
79a9becda   Alexandre Courbot   gpiolib: export d...
121
122
123
124
  static inline int gpio_export(unsigned gpio, bool direction_may_change)
  {
  	return gpiod_export(gpio_to_desc(gpio), direction_may_change);
  }
d8f388d8d   David Brownell   gpio: sysfs inter...
125

79a9becda   Alexandre Courbot   gpiolib: export d...
126
127
128
129
130
  static inline int gpio_export_link(struct device *dev, const char *name,
  				   unsigned gpio)
  {
  	return gpiod_export_link(dev, name, gpio_to_desc(gpio));
  }
79a9becda   Alexandre Courbot   gpiolib: export d...
131
132
133
134
  static inline void gpio_unexport(unsigned gpio)
  {
  	gpiod_unexport(gpio_to_desc(gpio));
  }
d8f388d8d   David Brownell   gpio: sysfs inter...
135

09cd9527d   Anton Vorontsov   gpiolib: fix HAVE...
136
  #else	/* !CONFIG_GPIOLIB */
d2876d08d   David Brownell   gpiolib: add gpio...
137

3474cb3cc   Joe Perches   gpio: Convert gpi...
138
  static inline bool gpio_is_valid(int number)
e6de1808f   Guennadi Liakhovetski   gpio: define gpio...
139
140
141
142
  {
  	/* only non-negative numbers are valid */
  	return number >= 0;
  }
4c20386c8   David Brownell   [PATCH] GPIO core
143
144
145
146
147
148
149
150
151
152
153
154
  /* 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...
155
  	return __gpio_get_value(gpio);
4c20386c8   David Brownell   [PATCH] GPIO core
156
157
158
159
160
  }
  
  static inline void gpio_set_value_cansleep(unsigned gpio, int value)
  {
  	might_sleep();
eb9ae7f2a   Hamo   gpio: fix build e...
161
  	__gpio_set_value(gpio, value);
4c20386c8   David Brownell   [PATCH] GPIO core
162
  }
09cd9527d   Anton Vorontsov   gpiolib: fix HAVE...
163
  #endif /* !CONFIG_GPIOLIB */
d8f388d8d   David Brownell   gpio: sysfs inter...
164

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