gpio: build fixes
[linux-2.6/openmoko-kernel/knife-kernel.git] / include / asm-generic / gpio.h
blob6be061d09da9d06fc2dabdb520148c028194621b
1 #ifndef _ASM_GENERIC_GPIO_H
2 #define _ASM_GENERIC_GPIO_H
4 #include <linux/types.h>
6 #ifdef CONFIG_HAVE_GPIO_LIB
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 * @direction_input: configures signal "offset" as input, or returns error
36 * @get: returns value for signal "offset"; for output signals this
37 * returns either the value actually sensed, or zero
38 * @direction_output: configures signal "offset" as output, or returns error
39 * @set: assigns output value for signal "offset"
40 * @dbg_show: optional routine to show contents in debugfs; default code
41 * will be used when this is omitted, but custom code can show extra
42 * state (such as pullup/pulldown configuration).
43 * @base: identifies the first GPIO number handled by this chip; or, if
44 * negative during registration, requests dynamic ID allocation.
45 * @ngpio: the number of GPIOs handled by this controller; the last GPIO
46 * handled is (base + ngpio - 1).
47 * @can_sleep: flag must be set iff get()/set() methods sleep, as they
48 * must while accessing GPIO expander chips over I2C or SPI
50 * A gpio_chip can help platforms abstract various sources of GPIOs so
51 * they can all be accessed through a common programing interface.
52 * Example sources would be SOC controllers, FPGAs, multifunction
53 * chips, dedicated GPIO expanders, and so on.
55 * Each chip controls a number of signals, identified in method calls
56 * by "offset" values in the range 0..(@ngpio - 1). When those signals
57 * are referenced through calls like gpio_get_value(gpio), the offset
58 * is calculated by subtracting @base from the gpio number.
60 struct gpio_chip {
61 char *label;
62 struct module *owner;
64 int (*direction_input)(struct gpio_chip *chip,
65 unsigned offset);
66 int (*get)(struct gpio_chip *chip,
67 unsigned offset);
68 int (*direction_output)(struct gpio_chip *chip,
69 unsigned offset, int value);
70 void (*set)(struct gpio_chip *chip,
71 unsigned offset, int value);
72 void (*dbg_show)(struct seq_file *s,
73 struct gpio_chip *chip);
74 int base;
75 u16 ngpio;
76 unsigned can_sleep:1;
79 extern const char *gpiochip_is_requested(struct gpio_chip *chip,
80 unsigned offset);
81 extern int __must_check gpiochip_reserve(int start, int ngpio);
83 /* add/remove chips */
84 extern int gpiochip_add(struct gpio_chip *chip);
85 extern int __must_check gpiochip_remove(struct gpio_chip *chip);
88 /* Always use the library code for GPIO management calls,
89 * or when sleeping may be involved.
91 extern int gpio_request(unsigned gpio, const char *label);
92 extern void gpio_free(unsigned gpio);
94 extern int gpio_direction_input(unsigned gpio);
95 extern int gpio_direction_output(unsigned gpio, int value);
97 extern int gpio_get_value_cansleep(unsigned gpio);
98 extern void gpio_set_value_cansleep(unsigned gpio, int value);
101 /* A platform's <asm/gpio.h> code may want to inline the I/O calls when
102 * the GPIO is constant and refers to some always-present controller,
103 * giving direct access to chip registers and tight bitbanging loops.
105 extern int __gpio_get_value(unsigned gpio);
106 extern void __gpio_set_value(unsigned gpio, int value);
108 extern int __gpio_cansleep(unsigned gpio);
111 #else
113 static inline int gpio_is_valid(int number)
115 /* only non-negative numbers are valid */
116 return number >= 0;
119 /* platforms that don't directly support access to GPIOs through I2C, SPI,
120 * or other blocking infrastructure can use these wrappers.
123 static inline int gpio_cansleep(unsigned gpio)
125 return 0;
128 static inline int gpio_get_value_cansleep(unsigned gpio)
130 might_sleep();
131 return gpio_get_value(gpio);
134 static inline void gpio_set_value_cansleep(unsigned gpio, int value)
136 might_sleep();
137 gpio_set_value(gpio, value);
140 #endif
142 #endif /* _ASM_GENERIC_GPIO_H */