blob: a3034d20ebd5ef2f7d264b62311afa06a7ac7327 [file] [log] [blame]
David Brownell4c203862007-02-12 00:53:11 -08001#ifndef _ASM_GENERIC_GPIO_H
2#define _ASM_GENERIC_GPIO_H
3
David Brownell6ea02052008-05-23 13:04:58 -07004#include <linux/types.h>
5
Michael Buesch7444a722008-07-25 01:46:11 -07006#ifdef CONFIG_GPIOLIB
David Brownelld2876d02008-02-04 22:28:20 -08007
David Brownell6ea02052008-05-23 13:04:58 -07008#include <linux/compiler.h>
9
David Brownelld2876d02008-02-04 22:28:20 -080010/* 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).
13 *
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.
17 */
18
19#ifndef ARCH_NR_GPIOS
20#define ARCH_NR_GPIOS 256
21#endif
22
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -070023static inline int gpio_is_valid(int number)
24{
25 /* only some non-negative numbers are valid */
26 return ((unsigned)number) < ARCH_NR_GPIOS;
27}
28
David Brownelld2876d02008-02-04 22:28:20 -080029struct seq_file;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -070030struct module;
David Brownelld2876d02008-02-04 22:28:20 -080031
32/**
33 * struct gpio_chip - abstract a GPIO controller
34 * @label: for diagnostics
David Brownelld8f388d82008-07-25 01:46:07 -070035 * @dev: optional device providing the GPIOs
36 * @owner: helps prevent removal of modules exporting active GPIOs
David Brownelld2876d02008-02-04 22:28:20 -080037 * @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
51 *
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.
56 *
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.
61 */
62struct gpio_chip {
63 char *label;
David Brownelld8f388d82008-07-25 01:46:07 -070064 struct device *dev;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -070065 struct module *owner;
David Brownelld2876d02008-02-04 22:28:20 -080066
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;
David Brownelld8f388d82008-07-25 01:46:07 -070080 unsigned exported:1;
David Brownelld2876d02008-02-04 22:28:20 -080081};
82
83extern const char *gpiochip_is_requested(struct gpio_chip *chip,
84 unsigned offset);
David Brownell6ea02052008-05-23 13:04:58 -070085extern int __must_check gpiochip_reserve(int start, int ngpio);
David Brownelld2876d02008-02-04 22:28:20 -080086
87/* add/remove chips */
88extern int gpiochip_add(struct gpio_chip *chip);
89extern int __must_check gpiochip_remove(struct gpio_chip *chip);
90
91
92/* Always use the library code for GPIO management calls,
93 * or when sleeping may be involved.
94 */
95extern int gpio_request(unsigned gpio, const char *label);
96extern void gpio_free(unsigned gpio);
97
98extern int gpio_direction_input(unsigned gpio);
99extern int gpio_direction_output(unsigned gpio, int value);
100
101extern int gpio_get_value_cansleep(unsigned gpio);
102extern void gpio_set_value_cansleep(unsigned gpio, int value);
103
104
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.
108 */
109extern int __gpio_get_value(unsigned gpio);
110extern void __gpio_set_value(unsigned gpio, int value);
111
112extern int __gpio_cansleep(unsigned gpio);
113
114
David Brownelld8f388d82008-07-25 01:46:07 -0700115#ifdef CONFIG_GPIO_SYSFS
116
117/*
118 * A sysfs interface can be exported by individual drivers if they want,
119 * but more typically is configured entirely from userspace.
120 */
121extern int gpio_export(unsigned gpio, bool direction_may_change);
122extern void gpio_unexport(unsigned gpio);
123
124#endif /* CONFIG_GPIO_SYSFS */
125
126#else /* !CONFIG_HAVE_GPIO_LIB */
David Brownelld2876d02008-02-04 22:28:20 -0800127
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -0700128static inline int gpio_is_valid(int number)
129{
130 /* only non-negative numbers are valid */
131 return number >= 0;
132}
133
David Brownell4c203862007-02-12 00:53:11 -0800134/* platforms that don't directly support access to GPIOs through I2C, SPI,
135 * or other blocking infrastructure can use these wrappers.
136 */
137
138static inline int gpio_cansleep(unsigned gpio)
139{
140 return 0;
141}
142
143static inline int gpio_get_value_cansleep(unsigned gpio)
144{
145 might_sleep();
146 return gpio_get_value(gpio);
147}
148
149static inline void gpio_set_value_cansleep(unsigned gpio, int value)
150{
151 might_sleep();
152 gpio_set_value(gpio, value);
153}
154
David Brownelld8f388d82008-07-25 01:46:07 -0700155#endif /* !CONFIG_HAVE_GPIO_LIB */
156
157#ifndef CONFIG_GPIO_SYSFS
158
159/* sysfs support is only available with gpiolib, where it's optional */
160
161static inline int gpio_export(unsigned gpio, bool direction_may_change)
162{
163 return -ENOSYS;
164}
165
166static inline void gpio_unexport(unsigned gpio)
167{
168}
169#endif /* CONFIG_GPIO_SYSFS */
David Brownelld2876d02008-02-04 22:28:20 -0800170
David Brownell4c203862007-02-12 00:53:11 -0800171#endif /* _ASM_GENERIC_GPIO_H */