gpiolib: allow user-selection
[linux-2.6.git] / include / asm-generic / gpio.h
bloba3034d20ebd5ef2f7d264b62311afa06a7ac7327
1 #ifndef _ASM_GENERIC_GPIO_H
2 #define _ASM_GENERIC_GPIO_H
4 #include <linux/types.h>
6 #ifdef CONFIG_GPIOLIB
8 #include <linux/compiler.h>
10 /* Platforms may implement their GPIO interface with library code,
11 * at a small performance cost for non-inlined operations and some
12 * extra memory (for code and for per-GPIO table entries).
14 * While the GPIO programming interface defines valid GPIO numbers
15 * to be in the range 0..MAX_INT, this library restricts them to the
16 * smaller range 0..ARCH_NR_GPIOS.
19 #ifndef ARCH_NR_GPIOS
20 #define ARCH_NR_GPIOS 256
21 #endif
23 static inline int gpio_is_valid(int number)
25 /* only some non-negative numbers are valid */
26 return ((unsigned)number) < ARCH_NR_GPIOS;
29 struct seq_file;
30 struct module;
32 /**
33 * struct gpio_chip - abstract a GPIO controller
34 * @label: for diagnostics
35 * @dev: optional device providing the GPIOs
36 * @owner: helps prevent removal of modules exporting active GPIOs
37 * @direction_input: configures signal "offset" as input, or returns error
38 * @get: returns value for signal "offset"; for output signals this
39 * returns either the value actually sensed, or zero
40 * @direction_output: configures signal "offset" as output, or returns error
41 * @set: assigns output value for signal "offset"
42 * @dbg_show: optional routine to show contents in debugfs; default code
43 * will be used when this is omitted, but custom code can show extra
44 * state (such as pullup/pulldown configuration).
45 * @base: identifies the first GPIO number handled by this chip; or, if
46 * negative during registration, requests dynamic ID allocation.
47 * @ngpio: the number of GPIOs handled by this controller; the last GPIO
48 * handled is (base + ngpio - 1).
49 * @can_sleep: flag must be set iff get()/set() methods sleep, as they
50 * must while accessing GPIO expander chips over I2C or SPI
52 * A gpio_chip can help platforms abstract various sources of GPIOs so
53 * they can all be accessed through a common programing interface.
54 * Example sources would be SOC controllers, FPGAs, multifunction
55 * chips, dedicated GPIO expanders, and so on.
57 * Each chip controls a number of signals, identified in method calls
58 * by "offset" values in the range 0..(@ngpio - 1). When those signals
59 * are referenced through calls like gpio_get_value(gpio), the offset
60 * is calculated by subtracting @base from the gpio number.
62 struct gpio_chip {
63 char *label;
64 struct device *dev;
65 struct module *owner;
67 int (*direction_input)(struct gpio_chip *chip,
68 unsigned offset);
69 int (*get)(struct gpio_chip *chip,
70 unsigned offset);
71 int (*direction_output)(struct gpio_chip *chip,
72 unsigned offset, int value);
73 void (*set)(struct gpio_chip *chip,
74 unsigned offset, int value);
75 void (*dbg_show)(struct seq_file *s,
76 struct gpio_chip *chip);
77 int base;
78 u16 ngpio;
79 unsigned can_sleep:1;
80 unsigned exported:1;
83 extern const char *gpiochip_is_requested(struct gpio_chip *chip,
84 unsigned offset);
85 extern int __must_check gpiochip_reserve(int start, int ngpio);
87 /* add/remove chips */
88 extern int gpiochip_add(struct gpio_chip *chip);
89 extern int __must_check gpiochip_remove(struct gpio_chip *chip);
92 /* Always use the library code for GPIO management calls,
93 * or when sleeping may be involved.
95 extern int gpio_request(unsigned gpio, const char *label);
96 extern void gpio_free(unsigned gpio);
98 extern int gpio_direction_input(unsigned gpio);
99 extern int gpio_direction_output(unsigned gpio, int value);
101 extern int gpio_get_value_cansleep(unsigned gpio);
102 extern void gpio_set_value_cansleep(unsigned gpio, int value);
105 /* A platform's <asm/gpio.h> code may want to inline the I/O calls when
106 * the GPIO is constant and refers to some always-present controller,
107 * giving direct access to chip registers and tight bitbanging loops.
109 extern int __gpio_get_value(unsigned gpio);
110 extern void __gpio_set_value(unsigned gpio, int value);
112 extern int __gpio_cansleep(unsigned gpio);
115 #ifdef CONFIG_GPIO_SYSFS
118 * A sysfs interface can be exported by individual drivers if they want,
119 * but more typically is configured entirely from userspace.
121 extern int gpio_export(unsigned gpio, bool direction_may_change);
122 extern void gpio_unexport(unsigned gpio);
124 #endif /* CONFIG_GPIO_SYSFS */
126 #else /* !CONFIG_HAVE_GPIO_LIB */
128 static inline int gpio_is_valid(int number)
130 /* only non-negative numbers are valid */
131 return number >= 0;
134 /* platforms that don't directly support access to GPIOs through I2C, SPI,
135 * or other blocking infrastructure can use these wrappers.
138 static inline int gpio_cansleep(unsigned gpio)
140 return 0;
143 static inline int gpio_get_value_cansleep(unsigned gpio)
145 might_sleep();
146 return gpio_get_value(gpio);
149 static inline void gpio_set_value_cansleep(unsigned gpio, int value)
151 might_sleep();
152 gpio_set_value(gpio, value);
155 #endif /* !CONFIG_HAVE_GPIO_LIB */
157 #ifndef CONFIG_GPIO_SYSFS
159 /* sysfs support is only available with gpiolib, where it's optional */
161 static inline int gpio_export(unsigned gpio, bool direction_may_change)
163 return -ENOSYS;
166 static inline void gpio_unexport(unsigned gpio)
169 #endif /* CONFIG_GPIO_SYSFS */
171 #endif /* _ASM_GENERIC_GPIO_H */