3d2235790613079c4317491f62d52464911ed724
[profile/mobile/platform/kernel/u-boot-tm1.git] / include / asm-generic / gpio.h
1 #ifndef _ASM_GENERIC_GPIO_H
2 #define _ASM_GENERIC_GPIO_H
3
4 #include <linux/types.h>
5 #include <ubi_uboot.h>
6 #include <asm/arch/gpio.h>
7
8
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).
12  *
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.
16  *
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.
20  */
21
22 /*
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.
26  *
27  * Invalid GPIO numbers are useful for indicating no-such-GPIO in
28  * platform data and other tables.
29  */
30
31 static bool gpio_is_valid(int number)
32 {
33         return number >= 0 && number < ARCH_NR_GPIOS;
34 }
35
36 struct gpio;
37
38 /**
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
68  *      number of the gpio.
69  *
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.
74  *
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.
79  */
80 struct gpio_chip {
81         const char              *label;
82
83         int                     (*request)(struct gpio_chip *chip,
84                                                 unsigned offset);
85         void                    (*free)(struct gpio_chip *chip,
86                                                 unsigned offset);
87
88         int                     (*direction_input)(struct gpio_chip *chip,
89                                                 unsigned offset);
90         int                     (*get)(struct gpio_chip *chip,
91                                                 unsigned offset);
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);
96
97         void                    (*set)(struct gpio_chip *chip,
98                                                 unsigned offset, int value);
99
100         int                     (*to_irq)(struct gpio_chip *chip,
101                                                 unsigned offset);
102
103         void                    (*dbg_show)(struct seq_file *s,
104                                                 struct gpio_chip *chip);
105         int                     base;
106         u16                     ngpio;
107         const char              *const *names;
108         unsigned                can_sleep:1;
109         unsigned                exported:1;
110 };
111
112 extern const char *gpiochip_is_requested(struct gpio_chip *chip,
113                         unsigned offset);
114 extern struct gpio_chip *gpio_to_chip(unsigned gpio);
115 extern int gpiochip_reserve(int start, int ngpio);
116
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,
122                                                      const void *data));
123
124
125 /* Always use the library code for GPIO management calls,
126  * or when sleeping may be involved.
127  */
128 extern int gpio_request(unsigned gpio, const char *label);
129 extern void gpio_free(unsigned gpio);
130
131 extern int gpio_direction_input(unsigned gpio);
132 extern int gpio_direction_output(unsigned gpio, int value);
133
134 extern int gpio_set_debounce(unsigned gpio, unsigned debounce);
135
136 extern int gpio_get_value_cansleep(unsigned gpio);
137 extern void gpio_set_value_cansleep(unsigned gpio, int value);
138
139
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.
143  */
144 extern int __gpio_get_value(unsigned gpio);
145 extern void __gpio_set_value(unsigned gpio, int value);
146
147 extern int __gpio_cansleep(unsigned gpio);
148
149 extern int __gpio_to_irq(unsigned gpio);
150
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);
154
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);
158
159 #endif /* _ASM_GENERIC_GPIO_H */