blob: 66d6106a2067fefc1f4154e500d93ea889f94cd6 [file] [log] [blame]
David Brownell4c203862007-02-12 00:53:11 -08001#ifndef _ASM_GENERIC_GPIO_H
2#define _ASM_GENERIC_GPIO_H
3
Mike Frysingerb3db4a82009-10-01 15:43:56 -07004#include <linux/kernel.h>
David Brownell6ea02052008-05-23 13:04:58 -07005#include <linux/types.h>
Atsushi Nemoto25947d52008-07-28 15:46:38 -07006#include <linux/errno.h>
David Brownell6ea02052008-05-23 13:04:58 -07007
Michael Buesch7444a722008-07-25 01:46:11 -07008#ifdef CONFIG_GPIOLIB
David Brownelld2876d02008-02-04 22:28:20 -08009
David Brownell6ea02052008-05-23 13:04:58 -070010#include <linux/compiler.h>
11
David Brownelld2876d02008-02-04 22:28:20 -080012/* Platforms may implement their GPIO interface with library code,
13 * at a small performance cost for non-inlined operations and some
14 * extra memory (for code and for per-GPIO table entries).
15 *
16 * While the GPIO programming interface defines valid GPIO numbers
17 * to be in the range 0..MAX_INT, this library restricts them to the
Michael Buesch6a9436d2008-07-26 15:22:26 -070018 * smaller range 0..ARCH_NR_GPIOS-1.
David Brownelld2876d02008-02-04 22:28:20 -080019 */
20
21#ifndef ARCH_NR_GPIOS
22#define ARCH_NR_GPIOS 256
23#endif
24
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -070025static inline int gpio_is_valid(int number)
26{
27 /* only some non-negative numbers are valid */
28 return ((unsigned)number) < ARCH_NR_GPIOS;
29}
30
David Brownelld2876d02008-02-04 22:28:20 -080031struct seq_file;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -070032struct module;
David Brownelld2876d02008-02-04 22:28:20 -080033
34/**
35 * struct gpio_chip - abstract a GPIO controller
36 * @label: for diagnostics
David Brownelld8f388d2008-07-25 01:46:07 -070037 * @dev: optional device providing the GPIOs
38 * @owner: helps prevent removal of modules exporting active GPIOs
David Brownell35e8bb52008-10-15 22:03:16 -070039 * @request: optional hook for chip-specific activation, such as
40 * enabling module power and clock; may sleep
41 * @free: optional hook for chip-specific deactivation, such as
42 * disabling module power and clock; may sleep
David Brownelld2876d02008-02-04 22:28:20 -080043 * @direction_input: configures signal "offset" as input, or returns error
44 * @get: returns value for signal "offset"; for output signals this
45 * returns either the value actually sensed, or zero
46 * @direction_output: configures signal "offset" as output, or returns error
47 * @set: assigns output value for signal "offset"
David Brownell0f6d5042008-10-15 22:03:14 -070048 * @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
49 * implementation may not sleep
David Brownelld2876d02008-02-04 22:28:20 -080050 * @dbg_show: optional routine to show contents in debugfs; default code
51 * will be used when this is omitted, but custom code can show extra
52 * state (such as pullup/pulldown configuration).
53 * @base: identifies the first GPIO number handled by this chip; or, if
54 * negative during registration, requests dynamic ID allocation.
55 * @ngpio: the number of GPIOs handled by this controller; the last GPIO
56 * handled is (base + ngpio - 1).
57 * @can_sleep: flag must be set iff get()/set() methods sleep, as they
58 * must while accessing GPIO expander chips over I2C or SPI
Daniel Silverstone926b6632009-04-02 16:57:05 -070059 * @names: if set, must be an array of strings to use as alternative
60 * names for the GPIOs in this chip. Any entry in the array
61 * may be NULL if there is no alias for the GPIO, however the
62 * array must be @ngpio entries long.
David Brownelld2876d02008-02-04 22:28:20 -080063 *
64 * A gpio_chip can help platforms abstract various sources of GPIOs so
65 * they can all be accessed through a common programing interface.
66 * Example sources would be SOC controllers, FPGAs, multifunction
67 * chips, dedicated GPIO expanders, and so on.
68 *
69 * Each chip controls a number of signals, identified in method calls
70 * by "offset" values in the range 0..(@ngpio - 1). When those signals
71 * are referenced through calls like gpio_get_value(gpio), the offset
72 * is calculated by subtracting @base from the gpio number.
73 */
74struct gpio_chip {
Dmitry Baryshkov4fd54632008-10-15 22:03:10 -070075 const char *label;
David Brownelld8f388d2008-07-25 01:46:07 -070076 struct device *dev;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -070077 struct module *owner;
David Brownelld2876d02008-02-04 22:28:20 -080078
David Brownell35e8bb52008-10-15 22:03:16 -070079 int (*request)(struct gpio_chip *chip,
80 unsigned offset);
81 void (*free)(struct gpio_chip *chip,
82 unsigned offset);
83
David Brownelld2876d02008-02-04 22:28:20 -080084 int (*direction_input)(struct gpio_chip *chip,
85 unsigned offset);
86 int (*get)(struct gpio_chip *chip,
87 unsigned offset);
88 int (*direction_output)(struct gpio_chip *chip,
89 unsigned offset, int value);
90 void (*set)(struct gpio_chip *chip,
91 unsigned offset, int value);
David Brownell0f6d5042008-10-15 22:03:14 -070092
93 int (*to_irq)(struct gpio_chip *chip,
94 unsigned offset);
95
David Brownelld2876d02008-02-04 22:28:20 -080096 void (*dbg_show)(struct seq_file *s,
97 struct gpio_chip *chip);
98 int base;
99 u16 ngpio;
Daniel Silverstone926b6632009-04-02 16:57:05 -0700100 char **names;
David Brownelld2876d02008-02-04 22:28:20 -0800101 unsigned can_sleep:1;
David Brownelld8f388d2008-07-25 01:46:07 -0700102 unsigned exported:1;
David Brownelld2876d02008-02-04 22:28:20 -0800103};
104
105extern const char *gpiochip_is_requested(struct gpio_chip *chip,
106 unsigned offset);
David Brownell6ea02052008-05-23 13:04:58 -0700107extern int __must_check gpiochip_reserve(int start, int ngpio);
David Brownelld2876d02008-02-04 22:28:20 -0800108
109/* add/remove chips */
110extern int gpiochip_add(struct gpio_chip *chip);
111extern int __must_check gpiochip_remove(struct gpio_chip *chip);
112
113
114/* Always use the library code for GPIO management calls,
115 * or when sleeping may be involved.
116 */
117extern int gpio_request(unsigned gpio, const char *label);
118extern void gpio_free(unsigned gpio);
119
120extern int gpio_direction_input(unsigned gpio);
121extern int gpio_direction_output(unsigned gpio, int value);
122
123extern int gpio_get_value_cansleep(unsigned gpio);
124extern void gpio_set_value_cansleep(unsigned gpio, int value);
125
126
127/* A platform's <asm/gpio.h> code may want to inline the I/O calls when
128 * the GPIO is constant and refers to some always-present controller,
129 * giving direct access to chip registers and tight bitbanging loops.
130 */
131extern int __gpio_get_value(unsigned gpio);
132extern void __gpio_set_value(unsigned gpio, int value);
133
134extern int __gpio_cansleep(unsigned gpio);
135
David Brownell0f6d5042008-10-15 22:03:14 -0700136extern int __gpio_to_irq(unsigned gpio);
David Brownelld2876d02008-02-04 22:28:20 -0800137
David Brownelld8f388d2008-07-25 01:46:07 -0700138#ifdef CONFIG_GPIO_SYSFS
139
140/*
141 * A sysfs interface can be exported by individual drivers if they want,
142 * but more typically is configured entirely from userspace.
143 */
144extern int gpio_export(unsigned gpio, bool direction_may_change);
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700145extern int gpio_export_link(struct device *dev, const char *name,
146 unsigned gpio);
David Brownelld8f388d2008-07-25 01:46:07 -0700147extern void gpio_unexport(unsigned gpio);
148
149#endif /* CONFIG_GPIO_SYSFS */
150
151#else /* !CONFIG_HAVE_GPIO_LIB */
David Brownelld2876d02008-02-04 22:28:20 -0800152
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -0700153static inline int gpio_is_valid(int number)
154{
155 /* only non-negative numbers are valid */
156 return number >= 0;
157}
158
David Brownell4c203862007-02-12 00:53:11 -0800159/* platforms that don't directly support access to GPIOs through I2C, SPI,
160 * or other blocking infrastructure can use these wrappers.
161 */
162
163static inline int gpio_cansleep(unsigned gpio)
164{
165 return 0;
166}
167
168static inline int gpio_get_value_cansleep(unsigned gpio)
169{
170 might_sleep();
171 return gpio_get_value(gpio);
172}
173
174static inline void gpio_set_value_cansleep(unsigned gpio, int value)
175{
176 might_sleep();
177 gpio_set_value(gpio, value);
178}
179
David Brownelld8f388d2008-07-25 01:46:07 -0700180#endif /* !CONFIG_HAVE_GPIO_LIB */
181
182#ifndef CONFIG_GPIO_SYSFS
183
184/* sysfs support is only available with gpiolib, where it's optional */
185
186static inline int gpio_export(unsigned gpio, bool direction_may_change)
187{
188 return -ENOSYS;
189}
190
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700191static inline int gpio_export_link(struct device *dev, const char *name,
192 unsigned gpio)
193{
194 return -ENOSYS;
195}
196
David Brownelld8f388d2008-07-25 01:46:07 -0700197static inline void gpio_unexport(unsigned gpio)
198{
199}
200#endif /* CONFIG_GPIO_SYSFS */
David Brownelld2876d02008-02-04 22:28:20 -0800201
David Brownell4c203862007-02-12 00:53:11 -0800202#endif /* _ASM_GENERIC_GPIO_H */