usb: added usb-host functionality for MSM devices amd Leo config file
[htc-linux.git] / include / asm-generic / gpio.h
blob66d6106a2067fefc1f4154e500d93ea889f94cd6
1 #ifndef _ASM_GENERIC_GPIO_H
2 #define _ASM_GENERIC_GPIO_H
4 #include <linux/kernel.h>
5 #include <linux/types.h>
6 #include <linux/errno.h>
8 #ifdef CONFIG_GPIOLIB
10 #include <linux/compiler.h>
12 /* Platforms may implement their GPIO interface with library code,
13 * at a small performance cost for non-inlined operations and some
14 * extra memory (for code and for per-GPIO table entries).
16 * While the GPIO programming interface defines valid GPIO numbers
17 * to be in the range 0..MAX_INT, this library restricts them to the
18 * smaller range 0..ARCH_NR_GPIOS-1.
21 #ifndef ARCH_NR_GPIOS
22 #define ARCH_NR_GPIOS 256
23 #endif
25 static inline int gpio_is_valid(int number)
27 /* only some non-negative numbers are valid */
28 return ((unsigned)number) < ARCH_NR_GPIOS;
31 struct seq_file;
32 struct module;
34 /**
35 * struct gpio_chip - abstract a GPIO controller
36 * @label: for diagnostics
37 * @dev: optional device providing the GPIOs
38 * @owner: helps prevent removal of modules exporting active GPIOs
39 * @request: optional hook for chip-specific activation, such as
40 * enabling module power and clock; may sleep
41 * @free: optional hook for chip-specific deactivation, such as
42 * disabling module power and clock; may sleep
43 * @direction_input: configures signal "offset" as input, or returns error
44 * @get: returns value for signal "offset"; for output signals this
45 * returns either the value actually sensed, or zero
46 * @direction_output: configures signal "offset" as output, or returns error
47 * @set: assigns output value for signal "offset"
48 * @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
49 * implementation may not sleep
50 * @dbg_show: optional routine to show contents in debugfs; default code
51 * will be used when this is omitted, but custom code can show extra
52 * state (such as pullup/pulldown configuration).
53 * @base: identifies the first GPIO number handled by this chip; or, if
54 * negative during registration, requests dynamic ID allocation.
55 * @ngpio: the number of GPIOs handled by this controller; the last GPIO
56 * handled is (base + ngpio - 1).
57 * @can_sleep: flag must be set iff get()/set() methods sleep, as they
58 * must while accessing GPIO expander chips over I2C or SPI
59 * @names: if set, must be an array of strings to use as alternative
60 * names for the GPIOs in this chip. Any entry in the array
61 * may be NULL if there is no alias for the GPIO, however the
62 * array must be @ngpio entries long.
64 * A gpio_chip can help platforms abstract various sources of GPIOs so
65 * they can all be accessed through a common programing interface.
66 * Example sources would be SOC controllers, FPGAs, multifunction
67 * chips, dedicated GPIO expanders, and so on.
69 * Each chip controls a number of signals, identified in method calls
70 * by "offset" values in the range 0..(@ngpio - 1). When those signals
71 * are referenced through calls like gpio_get_value(gpio), the offset
72 * is calculated by subtracting @base from the gpio number.
74 struct gpio_chip {
75 const char *label;
76 struct device *dev;
77 struct module *owner;
79 int (*request)(struct gpio_chip *chip,
80 unsigned offset);
81 void (*free)(struct gpio_chip *chip,
82 unsigned offset);
84 int (*direction_input)(struct gpio_chip *chip,
85 unsigned offset);
86 int (*get)(struct gpio_chip *chip,
87 unsigned offset);
88 int (*direction_output)(struct gpio_chip *chip,
89 unsigned offset, int value);
90 void (*set)(struct gpio_chip *chip,
91 unsigned offset, int value);
93 int (*to_irq)(struct gpio_chip *chip,
94 unsigned offset);
96 void (*dbg_show)(struct seq_file *s,
97 struct gpio_chip *chip);
98 int base;
99 u16 ngpio;
100 char **names;
101 unsigned can_sleep:1;
102 unsigned exported:1;
105 extern const char *gpiochip_is_requested(struct gpio_chip *chip,
106 unsigned offset);
107 extern int __must_check gpiochip_reserve(int start, int ngpio);
109 /* add/remove chips */
110 extern int gpiochip_add(struct gpio_chip *chip);
111 extern int __must_check gpiochip_remove(struct gpio_chip *chip);
114 /* Always use the library code for GPIO management calls,
115 * or when sleeping may be involved.
117 extern int gpio_request(unsigned gpio, const char *label);
118 extern void gpio_free(unsigned gpio);
120 extern int gpio_direction_input(unsigned gpio);
121 extern int gpio_direction_output(unsigned gpio, int value);
123 extern int gpio_get_value_cansleep(unsigned gpio);
124 extern void gpio_set_value_cansleep(unsigned gpio, int value);
127 /* A platform's <asm/gpio.h> code may want to inline the I/O calls when
128 * the GPIO is constant and refers to some always-present controller,
129 * giving direct access to chip registers and tight bitbanging loops.
131 extern int __gpio_get_value(unsigned gpio);
132 extern void __gpio_set_value(unsigned gpio, int value);
134 extern int __gpio_cansleep(unsigned gpio);
136 extern int __gpio_to_irq(unsigned gpio);
138 #ifdef CONFIG_GPIO_SYSFS
141 * A sysfs interface can be exported by individual drivers if they want,
142 * but more typically is configured entirely from userspace.
144 extern int gpio_export(unsigned gpio, bool direction_may_change);
145 extern int gpio_export_link(struct device *dev, const char *name,
146 unsigned gpio);
147 extern void gpio_unexport(unsigned gpio);
149 #endif /* CONFIG_GPIO_SYSFS */
151 #else /* !CONFIG_HAVE_GPIO_LIB */
153 static inline int gpio_is_valid(int number)
155 /* only non-negative numbers are valid */
156 return number >= 0;
159 /* platforms that don't directly support access to GPIOs through I2C, SPI,
160 * or other blocking infrastructure can use these wrappers.
163 static inline int gpio_cansleep(unsigned gpio)
165 return 0;
168 static inline int gpio_get_value_cansleep(unsigned gpio)
170 might_sleep();
171 return gpio_get_value(gpio);
174 static inline void gpio_set_value_cansleep(unsigned gpio, int value)
176 might_sleep();
177 gpio_set_value(gpio, value);
180 #endif /* !CONFIG_HAVE_GPIO_LIB */
182 #ifndef CONFIG_GPIO_SYSFS
184 /* sysfs support is only available with gpiolib, where it's optional */
186 static inline int gpio_export(unsigned gpio, bool direction_may_change)
188 return -ENOSYS;
191 static inline int gpio_export_link(struct device *dev, const char *name,
192 unsigned gpio)
194 return -ENOSYS;
197 static inline void gpio_unexport(unsigned gpio)
200 #endif /* CONFIG_GPIO_SYSFS */
202 #endif /* _ASM_GENERIC_GPIO_H */