gpiolib: i2c/spi drivers: handle rmmod better
[deliverable/linux.git] / include / asm-generic / gpio.h
CommitLineData
4c20386c
DB
1#ifndef _ASM_GENERIC_GPIO_H
2#define _ASM_GENERIC_GPIO_H
3
d2876d08
DB
4#ifdef CONFIG_HAVE_GPIO_LIB
5
6/* Platforms may implement their GPIO interface with library code,
7 * at a small performance cost for non-inlined operations and some
8 * extra memory (for code and for per-GPIO table entries).
9 *
10 * While the GPIO programming interface defines valid GPIO numbers
11 * to be in the range 0..MAX_INT, this library restricts them to the
12 * smaller range 0..ARCH_NR_GPIOS.
13 */
14
15#ifndef ARCH_NR_GPIOS
16#define ARCH_NR_GPIOS 256
17#endif
18
19struct seq_file;
438d8908 20struct module;
d2876d08
DB
21
22/**
23 * struct gpio_chip - abstract a GPIO controller
24 * @label: for diagnostics
25 * @direction_input: configures signal "offset" as input, or returns error
26 * @get: returns value for signal "offset"; for output signals this
27 * returns either the value actually sensed, or zero
28 * @direction_output: configures signal "offset" as output, or returns error
29 * @set: assigns output value for signal "offset"
30 * @dbg_show: optional routine to show contents in debugfs; default code
31 * will be used when this is omitted, but custom code can show extra
32 * state (such as pullup/pulldown configuration).
33 * @base: identifies the first GPIO number handled by this chip; or, if
34 * negative during registration, requests dynamic ID allocation.
35 * @ngpio: the number of GPIOs handled by this controller; the last GPIO
36 * handled is (base + ngpio - 1).
37 * @can_sleep: flag must be set iff get()/set() methods sleep, as they
38 * must while accessing GPIO expander chips over I2C or SPI
39 *
40 * A gpio_chip can help platforms abstract various sources of GPIOs so
41 * they can all be accessed through a common programing interface.
42 * Example sources would be SOC controllers, FPGAs, multifunction
43 * chips, dedicated GPIO expanders, and so on.
44 *
45 * Each chip controls a number of signals, identified in method calls
46 * by "offset" values in the range 0..(@ngpio - 1). When those signals
47 * are referenced through calls like gpio_get_value(gpio), the offset
48 * is calculated by subtracting @base from the gpio number.
49 */
50struct gpio_chip {
51 char *label;
438d8908 52 struct module *owner;
d2876d08
DB
53
54 int (*direction_input)(struct gpio_chip *chip,
55 unsigned offset);
56 int (*get)(struct gpio_chip *chip,
57 unsigned offset);
58 int (*direction_output)(struct gpio_chip *chip,
59 unsigned offset, int value);
60 void (*set)(struct gpio_chip *chip,
61 unsigned offset, int value);
62 void (*dbg_show)(struct seq_file *s,
63 struct gpio_chip *chip);
64 int base;
65 u16 ngpio;
66 unsigned can_sleep:1;
67};
68
69extern const char *gpiochip_is_requested(struct gpio_chip *chip,
70 unsigned offset);
71
72/* add/remove chips */
73extern int gpiochip_add(struct gpio_chip *chip);
74extern int __must_check gpiochip_remove(struct gpio_chip *chip);
75
76
77/* Always use the library code for GPIO management calls,
78 * or when sleeping may be involved.
79 */
80extern int gpio_request(unsigned gpio, const char *label);
81extern void gpio_free(unsigned gpio);
82
83extern int gpio_direction_input(unsigned gpio);
84extern int gpio_direction_output(unsigned gpio, int value);
85
86extern int gpio_get_value_cansleep(unsigned gpio);
87extern void gpio_set_value_cansleep(unsigned gpio, int value);
88
89
90/* A platform's <asm/gpio.h> code may want to inline the I/O calls when
91 * the GPIO is constant and refers to some always-present controller,
92 * giving direct access to chip registers and tight bitbanging loops.
93 */
94extern int __gpio_get_value(unsigned gpio);
95extern void __gpio_set_value(unsigned gpio, int value);
96
97extern int __gpio_cansleep(unsigned gpio);
98
99
100#else
101
4c20386c
DB
102/* platforms that don't directly support access to GPIOs through I2C, SPI,
103 * or other blocking infrastructure can use these wrappers.
104 */
105
106static inline int gpio_cansleep(unsigned gpio)
107{
108 return 0;
109}
110
111static inline int gpio_get_value_cansleep(unsigned gpio)
112{
113 might_sleep();
114 return gpio_get_value(gpio);
115}
116
117static inline void gpio_set_value_cansleep(unsigned gpio, int value)
118{
119 might_sleep();
120 gpio_set_value(gpio, value);
121}
122
d2876d08
DB
123#endif
124
4c20386c 125#endif /* _ASM_GENERIC_GPIO_H */
This page took 0.147498 seconds and 5 git commands to generate.