1 #ifndef _ASM_GENERIC_GPIO_H
2 #define _ASM_GENERIC_GPIO_H
4 #include <linux/types.h>
6 #include <asm/arch/gpio.h>
9 /* Platforms may implement their GPIO interface with library code,
10 * at a small performance cost for non-inlined operations and some
11 * extra memory (for code and for per-GPIO table entries).
13 * While the GPIO programming interface defines valid GPIO numbers
14 * to be in the range 0..MAX_INT, this library restricts them to the
15 * smaller range 0..ARCH_NR_GPIOS-1.
17 * ARCH_NR_GPIOS is somewhat arbitrary; it usually reflects the sum of
18 * builtin/SoC GPIOs plus a number of GPIOs on expanders; the latter is
19 * actually an estimate of a board-specific value.
23 * "valid" GPIO numbers are nonnegative and may be passed to
24 * setup routines like gpio_request(). only some valid numbers
25 * can successfully be requested and used.
27 * Invalid GPIO numbers are useful for indicating no-such-GPIO in
28 * platform data and other tables.
31 static bool gpio_is_valid(int number)
33 return number >= 0 && number < ARCH_NR_GPIOS;
39 * struct gpio_chip - abstract a GPIO controller
40 * @label: for diagnostics
41 * @dev: optional device providing the GPIOs
42 * @owner: helps prevent removal of modules exporting active GPIOs
43 * @request: optional hook for chip-specific activation, such as
44 * enabling module power and clock; may sleep
45 * @free: optional hook for chip-specific deactivation, such as
46 * disabling module power and clock; may sleep
47 * @direction_input: configures signal "offset" as input, or returns error
48 * @get: returns value for signal "offset"; for output signals this
49 * returns either the value actually sensed, or zero
50 * @direction_output: configures signal "offset" as output, or returns error
51 * @set: assigns output value for signal "offset"
52 * @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
53 * implementation may not sleep
54 * @dbg_show: optional routine to show contents in debugfs; default code
55 * will be used when this is omitted, but custom code can show extra
56 * state (such as pullup/pulldown configuration).
57 * @base: identifies the first GPIO number handled by this chip; or, if
58 * negative during registration, requests dynamic ID allocation.
59 * @ngpio: the number of GPIOs handled by this controller; the last GPIO
60 * handled is (base + ngpio - 1).
61 * @can_sleep: flag must be set iff get()/set() methods sleep, as they
62 * must while accessing GPIO expander chips over I2C or SPI
63 * @names: if set, must be an array of strings to use as alternative
64 * names for the GPIOs in this chip. Any entry in the array
65 * may be NULL if there is no alias for the GPIO, however the
66 * array must be @ngpio entries long. A name can include a single printk
67 * format specifier for an unsigned int. It is substituted by the actual
70 * A gpio_chip can help platforms abstract various sources of GPIOs so
71 * they can all be accessed through a common programing interface.
72 * Example sources would be SOC controllers, FPGAs, multifunction
73 * chips, dedicated GPIO expanders, and so on.
75 * Each chip controls a number of signals, identified in method calls
76 * by "offset" values in the range 0..(@ngpio - 1). When those signals
77 * are referenced through calls like gpio_get_value(gpio), the offset
78 * is calculated by subtracting @base from the gpio number.
83 int (*request)(struct gpio_chip *chip,
85 void (*free)(struct gpio_chip *chip,
88 int (*direction_input)(struct gpio_chip *chip,
90 int (*get)(struct gpio_chip *chip,
92 int (*direction_output)(struct gpio_chip *chip,
93 unsigned offset, int value);
94 int (*set_debounce)(struct gpio_chip *chip,
95 unsigned offset, unsigned debounce);
97 void (*set)(struct gpio_chip *chip,
98 unsigned offset, int value);
100 int (*to_irq)(struct gpio_chip *chip,
103 void (*dbg_show)(struct seq_file *s,
104 struct gpio_chip *chip);
107 const char *const *names;
108 unsigned can_sleep:1;
112 extern const char *gpiochip_is_requested(struct gpio_chip *chip,
114 extern struct gpio_chip *gpio_to_chip(unsigned gpio);
115 extern int gpiochip_reserve(int start, int ngpio);
117 /* add/remove chips */
118 extern int gpiochip_add(struct gpio_chip *chip);
119 extern int gpiochip_remove(struct gpio_chip *chip);
120 extern struct gpio_chip *gpiochip_find(const void *data,
121 int (*match)(struct gpio_chip *chip,
125 /* Always use the library code for GPIO management calls,
126 * or when sleeping may be involved.
128 extern int gpio_request(unsigned gpio, const char *label);
129 extern void gpio_free(unsigned gpio);
131 extern int gpio_direction_input(unsigned gpio);
132 extern int gpio_direction_output(unsigned gpio, int value);
134 extern int gpio_set_debounce(unsigned gpio, unsigned debounce);
136 extern int gpio_get_value_cansleep(unsigned gpio);
137 extern void gpio_set_value_cansleep(unsigned gpio, int value);
140 /* A platform's <asm/gpio.h> code may want to inline the I/O calls when
141 * the GPIO is constant and refers to some always-present controller,
142 * giving direct access to chip registers and tight bitbanging loops.
144 extern int __gpio_get_value(unsigned gpio);
145 extern void __gpio_set_value(unsigned gpio, int value);
147 extern int __gpio_cansleep(unsigned gpio);
149 extern int __gpio_to_irq(unsigned gpio);
151 extern int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);
152 extern int gpio_request_array(const struct gpio *array, size_t num);
153 extern void gpio_free_array(const struct gpio *array, size_t num);
155 /* bindings for managed devices that want to request gpios */
156 int devm_gpio_request(struct device *dev, unsigned gpio, const char *label);
157 void devm_gpio_free(struct device *dev, unsigned int gpio);
159 #endif /* _ASM_GENERIC_GPIO_H */