blob: 04cb1d175df1782274bda4d9098d43937ae6f60c [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>
Atsushi Nemoto25947d52008-07-28 15:46:38 -07005#include <linux/errno.h>
David Brownell6ea02052008-05-23 13:04:58 -07006
Michael Buesch7444a722008-07-25 01:46:11 -07007#ifdef CONFIG_GPIOLIB
David Brownelld2876d02008-02-04 22:28:20 -08008
David Brownell6ea02052008-05-23 13:04:58 -07009#include <linux/compiler.h>
10
David Brownelld2876d02008-02-04 22:28:20 -080011/* Platforms may implement their GPIO interface with library code,
12 * at a small performance cost for non-inlined operations and some
13 * extra memory (for code and for per-GPIO table entries).
14 *
15 * While the GPIO programming interface defines valid GPIO numbers
16 * to be in the range 0..MAX_INT, this library restricts them to the
Michael Buesch6a9436d2008-07-26 15:22:26 -070017 * smaller range 0..ARCH_NR_GPIOS-1.
David Brownelld2876d02008-02-04 22:28:20 -080018 */
19
20#ifndef ARCH_NR_GPIOS
21#define ARCH_NR_GPIOS 256
22#endif
23
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -070024static inline int gpio_is_valid(int number)
25{
26 /* only some non-negative numbers are valid */
27 return ((unsigned)number) < ARCH_NR_GPIOS;
28}
29
David Brownelld2876d02008-02-04 22:28:20 -080030struct seq_file;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -070031struct module;
David Brownelld2876d02008-02-04 22:28:20 -080032
33/**
34 * struct gpio_chip - abstract a GPIO controller
35 * @label: for diagnostics
David Brownelld8f388d2008-07-25 01:46:07 -070036 * @dev: optional device providing the GPIOs
37 * @owner: helps prevent removal of modules exporting active GPIOs
David Brownelld2876d02008-02-04 22:28:20 -080038 * @direction_input: configures signal "offset" as input, or returns error
39 * @get: returns value for signal "offset"; for output signals this
40 * returns either the value actually sensed, or zero
41 * @direction_output: configures signal "offset" as output, or returns error
42 * @set: assigns output value for signal "offset"
David Brownell0f6d5042008-10-15 22:03:14 -070043 * @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
44 * implementation may not sleep
David Brownelld2876d02008-02-04 22:28:20 -080045 * @dbg_show: optional routine to show contents in debugfs; default code
46 * will be used when this is omitted, but custom code can show extra
47 * state (such as pullup/pulldown configuration).
48 * @base: identifies the first GPIO number handled by this chip; or, if
49 * negative during registration, requests dynamic ID allocation.
50 * @ngpio: the number of GPIOs handled by this controller; the last GPIO
51 * handled is (base + ngpio - 1).
52 * @can_sleep: flag must be set iff get()/set() methods sleep, as they
53 * must while accessing GPIO expander chips over I2C or SPI
54 *
55 * A gpio_chip can help platforms abstract various sources of GPIOs so
56 * they can all be accessed through a common programing interface.
57 * Example sources would be SOC controllers, FPGAs, multifunction
58 * chips, dedicated GPIO expanders, and so on.
59 *
60 * Each chip controls a number of signals, identified in method calls
61 * by "offset" values in the range 0..(@ngpio - 1). When those signals
62 * are referenced through calls like gpio_get_value(gpio), the offset
63 * is calculated by subtracting @base from the gpio number.
64 */
65struct gpio_chip {
Dmitry Baryshkov4fd54632008-10-15 22:03:10 -070066 const char *label;
David Brownelld8f388d2008-07-25 01:46:07 -070067 struct device *dev;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -070068 struct module *owner;
David Brownelld2876d02008-02-04 22:28:20 -080069
70 int (*direction_input)(struct gpio_chip *chip,
71 unsigned offset);
72 int (*get)(struct gpio_chip *chip,
73 unsigned offset);
74 int (*direction_output)(struct gpio_chip *chip,
75 unsigned offset, int value);
76 void (*set)(struct gpio_chip *chip,
77 unsigned offset, int value);
David Brownell0f6d5042008-10-15 22:03:14 -070078
79 int (*to_irq)(struct gpio_chip *chip,
80 unsigned offset);
81
David Brownelld2876d02008-02-04 22:28:20 -080082 void (*dbg_show)(struct seq_file *s,
83 struct gpio_chip *chip);
84 int base;
85 u16 ngpio;
86 unsigned can_sleep:1;
David Brownelld8f388d2008-07-25 01:46:07 -070087 unsigned exported:1;
David Brownelld2876d02008-02-04 22:28:20 -080088};
89
90extern const char *gpiochip_is_requested(struct gpio_chip *chip,
91 unsigned offset);
David Brownell6ea02052008-05-23 13:04:58 -070092extern int __must_check gpiochip_reserve(int start, int ngpio);
David Brownelld2876d02008-02-04 22:28:20 -080093
94/* add/remove chips */
95extern int gpiochip_add(struct gpio_chip *chip);
96extern int __must_check gpiochip_remove(struct gpio_chip *chip);
97
98
99/* Always use the library code for GPIO management calls,
100 * or when sleeping may be involved.
101 */
102extern int gpio_request(unsigned gpio, const char *label);
103extern void gpio_free(unsigned gpio);
104
105extern int gpio_direction_input(unsigned gpio);
106extern int gpio_direction_output(unsigned gpio, int value);
107
108extern int gpio_get_value_cansleep(unsigned gpio);
109extern void gpio_set_value_cansleep(unsigned gpio, int value);
110
111
112/* A platform's <asm/gpio.h> code may want to inline the I/O calls when
113 * the GPIO is constant and refers to some always-present controller,
114 * giving direct access to chip registers and tight bitbanging loops.
115 */
116extern int __gpio_get_value(unsigned gpio);
117extern void __gpio_set_value(unsigned gpio, int value);
118
119extern int __gpio_cansleep(unsigned gpio);
120
David Brownell0f6d5042008-10-15 22:03:14 -0700121extern int __gpio_to_irq(unsigned gpio);
David Brownelld2876d02008-02-04 22:28:20 -0800122
David Brownelld8f388d2008-07-25 01:46:07 -0700123#ifdef CONFIG_GPIO_SYSFS
124
125/*
126 * A sysfs interface can be exported by individual drivers if they want,
127 * but more typically is configured entirely from userspace.
128 */
129extern int gpio_export(unsigned gpio, bool direction_may_change);
130extern void gpio_unexport(unsigned gpio);
131
132#endif /* CONFIG_GPIO_SYSFS */
133
134#else /* !CONFIG_HAVE_GPIO_LIB */
David Brownelld2876d02008-02-04 22:28:20 -0800135
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -0700136static inline int gpio_is_valid(int number)
137{
138 /* only non-negative numbers are valid */
139 return number >= 0;
140}
141
David Brownell4c203862007-02-12 00:53:11 -0800142/* platforms that don't directly support access to GPIOs through I2C, SPI,
143 * or other blocking infrastructure can use these wrappers.
144 */
145
146static inline int gpio_cansleep(unsigned gpio)
147{
148 return 0;
149}
150
151static inline int gpio_get_value_cansleep(unsigned gpio)
152{
153 might_sleep();
154 return gpio_get_value(gpio);
155}
156
157static inline void gpio_set_value_cansleep(unsigned gpio, int value)
158{
159 might_sleep();
160 gpio_set_value(gpio, value);
161}
162
David Brownelld8f388d2008-07-25 01:46:07 -0700163#endif /* !CONFIG_HAVE_GPIO_LIB */
164
165#ifndef CONFIG_GPIO_SYSFS
166
167/* sysfs support is only available with gpiolib, where it's optional */
168
169static inline int gpio_export(unsigned gpio, bool direction_may_change)
170{
171 return -ENOSYS;
172}
173
174static inline void gpio_unexport(unsigned gpio)
175{
176}
177#endif /* CONFIG_GPIO_SYSFS */
David Brownelld2876d02008-02-04 22:28:20 -0800178
David Brownell4c203862007-02-12 00:53:11 -0800179#endif /* _ASM_GENERIC_GPIO_H */