David Brownell | 4c20386 | 2007-02-12 00:53:11 -0800 | [diff] [blame] | 1 | #ifndef _ASM_GENERIC_GPIO_H |
| 2 | #define _ASM_GENERIC_GPIO_H |
| 3 | |
David Brownell | 6ea0205 | 2008-05-23 13:04:58 -0700 | [diff] [blame] | 4 | #include <linux/types.h> |
Atsushi Nemoto | 25947d5 | 2008-07-28 15:46:38 -0700 | [diff] [blame] | 5 | #include <linux/errno.h> |
David Brownell | 6ea0205 | 2008-05-23 13:04:58 -0700 | [diff] [blame] | 6 | |
Michael Buesch | 7444a72 | 2008-07-25 01:46:11 -0700 | [diff] [blame] | 7 | #ifdef CONFIG_GPIOLIB |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 8 | |
David Brownell | 6ea0205 | 2008-05-23 13:04:58 -0700 | [diff] [blame] | 9 | #include <linux/compiler.h> |
| 10 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 11 | /* 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 Buesch | 6a9436d | 2008-07-26 15:22:26 -0700 | [diff] [blame] | 17 | * smaller range 0..ARCH_NR_GPIOS-1. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #ifndef ARCH_NR_GPIOS |
| 21 | #define ARCH_NR_GPIOS 256 |
| 22 | #endif |
| 23 | |
Guennadi Liakhovetski | e6de180 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 24 | static 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 Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 30 | struct seq_file; |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 31 | struct module; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * struct gpio_chip - abstract a GPIO controller |
| 35 | * @label: for diagnostics |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 36 | * @dev: optional device providing the GPIOs |
| 37 | * @owner: helps prevent removal of modules exporting active GPIOs |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 38 | * @request: optional hook for chip-specific activation, such as |
| 39 | * enabling module power and clock; may sleep |
| 40 | * @free: optional hook for chip-specific deactivation, such as |
| 41 | * disabling module power and clock; may sleep |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 42 | * @direction_input: configures signal "offset" as input, or returns error |
| 43 | * @get: returns value for signal "offset"; for output signals this |
| 44 | * returns either the value actually sensed, or zero |
| 45 | * @direction_output: configures signal "offset" as output, or returns error |
| 46 | * @set: assigns output value for signal "offset" |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 47 | * @to_irq: optional hook supporting non-static gpio_to_irq() mappings; |
| 48 | * implementation may not sleep |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 49 | * @dbg_show: optional routine to show contents in debugfs; default code |
| 50 | * will be used when this is omitted, but custom code can show extra |
| 51 | * state (such as pullup/pulldown configuration). |
| 52 | * @base: identifies the first GPIO number handled by this chip; or, if |
| 53 | * negative during registration, requests dynamic ID allocation. |
| 54 | * @ngpio: the number of GPIOs handled by this controller; the last GPIO |
| 55 | * handled is (base + ngpio - 1). |
| 56 | * @can_sleep: flag must be set iff get()/set() methods sleep, as they |
| 57 | * must while accessing GPIO expander chips over I2C or SPI |
Daniel Silverstone | 926b663 | 2009-04-02 16:57:05 -0700 | [diff] [blame] | 58 | * @names: if set, must be an array of strings to use as alternative |
| 59 | * names for the GPIOs in this chip. Any entry in the array |
| 60 | * may be NULL if there is no alias for the GPIO, however the |
| 61 | * array must be @ngpio entries long. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 62 | * |
| 63 | * A gpio_chip can help platforms abstract various sources of GPIOs so |
| 64 | * they can all be accessed through a common programing interface. |
| 65 | * Example sources would be SOC controllers, FPGAs, multifunction |
| 66 | * chips, dedicated GPIO expanders, and so on. |
| 67 | * |
| 68 | * Each chip controls a number of signals, identified in method calls |
| 69 | * by "offset" values in the range 0..(@ngpio - 1). When those signals |
| 70 | * are referenced through calls like gpio_get_value(gpio), the offset |
| 71 | * is calculated by subtracting @base from the gpio number. |
| 72 | */ |
| 73 | struct gpio_chip { |
Dmitry Baryshkov | 4fd5463 | 2008-10-15 22:03:10 -0700 | [diff] [blame] | 74 | const char *label; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 75 | struct device *dev; |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 76 | struct module *owner; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 77 | |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 78 | int (*request)(struct gpio_chip *chip, |
| 79 | unsigned offset); |
| 80 | void (*free)(struct gpio_chip *chip, |
| 81 | unsigned offset); |
| 82 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 83 | int (*direction_input)(struct gpio_chip *chip, |
| 84 | unsigned offset); |
| 85 | int (*get)(struct gpio_chip *chip, |
| 86 | unsigned offset); |
| 87 | int (*direction_output)(struct gpio_chip *chip, |
| 88 | unsigned offset, int value); |
| 89 | void (*set)(struct gpio_chip *chip, |
| 90 | unsigned offset, int value); |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 91 | |
| 92 | int (*to_irq)(struct gpio_chip *chip, |
| 93 | unsigned offset); |
| 94 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 95 | void (*dbg_show)(struct seq_file *s, |
| 96 | struct gpio_chip *chip); |
| 97 | int base; |
| 98 | u16 ngpio; |
Daniel Silverstone | 926b663 | 2009-04-02 16:57:05 -0700 | [diff] [blame] | 99 | char **names; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 100 | unsigned can_sleep:1; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 101 | unsigned exported:1; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | extern const char *gpiochip_is_requested(struct gpio_chip *chip, |
| 105 | unsigned offset); |
David Brownell | 6ea0205 | 2008-05-23 13:04:58 -0700 | [diff] [blame] | 106 | extern int __must_check gpiochip_reserve(int start, int ngpio); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 107 | |
| 108 | /* add/remove chips */ |
| 109 | extern int gpiochip_add(struct gpio_chip *chip); |
| 110 | extern int __must_check gpiochip_remove(struct gpio_chip *chip); |
| 111 | |
| 112 | |
| 113 | /* Always use the library code for GPIO management calls, |
| 114 | * or when sleeping may be involved. |
| 115 | */ |
| 116 | extern int gpio_request(unsigned gpio, const char *label); |
| 117 | extern void gpio_free(unsigned gpio); |
| 118 | |
| 119 | extern int gpio_direction_input(unsigned gpio); |
| 120 | extern int gpio_direction_output(unsigned gpio, int value); |
| 121 | |
| 122 | extern int gpio_get_value_cansleep(unsigned gpio); |
| 123 | extern void gpio_set_value_cansleep(unsigned gpio, int value); |
| 124 | |
| 125 | |
| 126 | /* A platform's <asm/gpio.h> code may want to inline the I/O calls when |
| 127 | * the GPIO is constant and refers to some always-present controller, |
| 128 | * giving direct access to chip registers and tight bitbanging loops. |
| 129 | */ |
| 130 | extern int __gpio_get_value(unsigned gpio); |
| 131 | extern void __gpio_set_value(unsigned gpio, int value); |
| 132 | |
| 133 | extern int __gpio_cansleep(unsigned gpio); |
| 134 | |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 135 | extern int __gpio_to_irq(unsigned gpio); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 136 | |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 137 | #ifdef CONFIG_GPIO_SYSFS |
| 138 | |
| 139 | /* |
| 140 | * A sysfs interface can be exported by individual drivers if they want, |
| 141 | * but more typically is configured entirely from userspace. |
| 142 | */ |
| 143 | extern int gpio_export(unsigned gpio, bool direction_may_change); |
Jani Nikula | a4177ee | 2009-09-22 16:46:33 -0700 | [diff] [blame] | 144 | extern int gpio_export_link(struct device *dev, const char *name, |
| 145 | unsigned gpio); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 146 | extern void gpio_unexport(unsigned gpio); |
| 147 | |
| 148 | #endif /* CONFIG_GPIO_SYSFS */ |
| 149 | |
| 150 | #else /* !CONFIG_HAVE_GPIO_LIB */ |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 151 | |
Guennadi Liakhovetski | e6de180 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 152 | static inline int gpio_is_valid(int number) |
| 153 | { |
| 154 | /* only non-negative numbers are valid */ |
| 155 | return number >= 0; |
| 156 | } |
| 157 | |
David Brownell | 4c20386 | 2007-02-12 00:53:11 -0800 | [diff] [blame] | 158 | /* platforms that don't directly support access to GPIOs through I2C, SPI, |
| 159 | * or other blocking infrastructure can use these wrappers. |
| 160 | */ |
| 161 | |
| 162 | static inline int gpio_cansleep(unsigned gpio) |
| 163 | { |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static inline int gpio_get_value_cansleep(unsigned gpio) |
| 168 | { |
| 169 | might_sleep(); |
| 170 | return gpio_get_value(gpio); |
| 171 | } |
| 172 | |
| 173 | static inline void gpio_set_value_cansleep(unsigned gpio, int value) |
| 174 | { |
| 175 | might_sleep(); |
| 176 | gpio_set_value(gpio, value); |
| 177 | } |
| 178 | |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 179 | #endif /* !CONFIG_HAVE_GPIO_LIB */ |
| 180 | |
| 181 | #ifndef CONFIG_GPIO_SYSFS |
| 182 | |
| 183 | /* sysfs support is only available with gpiolib, where it's optional */ |
| 184 | |
| 185 | static inline int gpio_export(unsigned gpio, bool direction_may_change) |
| 186 | { |
| 187 | return -ENOSYS; |
| 188 | } |
| 189 | |
Jani Nikula | a4177ee | 2009-09-22 16:46:33 -0700 | [diff] [blame] | 190 | static inline int gpio_export_link(struct device *dev, const char *name, |
| 191 | unsigned gpio) |
| 192 | { |
| 193 | return -ENOSYS; |
| 194 | } |
| 195 | |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 196 | static inline void gpio_unexport(unsigned gpio) |
| 197 | { |
| 198 | } |
| 199 | #endif /* CONFIG_GPIO_SYSFS */ |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 200 | |
David Brownell | 4c20386 | 2007-02-12 00:53:11 -0800 | [diff] [blame] | 201 | #endif /* _ASM_GENERIC_GPIO_H */ |