Commit 6c0b4e6c85d085bd92966bc2b8da73e2d7f35929
Committed by
Grant Likely
1 parent
372e722ea4
Exists in
smarc-l5.0.0_1.0.0-ga
and in
5 other branches
gpiolib: let gpio_chip reference its descriptors
Add a pointer to the gpio_chip structure that references the array of GPIO descriptors belonging to the chip, and update gpiolib code to use this pointer instead of the global gpio_desc[] array. This is another step towards the removal of the gpio_desc[] global array. Signed-off-by: Alexandre Courbot <acourbot@nvidia.com> Reviewed-by: Linus Walleij <linus.walleij@linaro.orh> Signed-off-by: Grant Likely <grant.likely@secretlab.ca>
Showing 2 changed files with 26 additions and 16 deletions Inline Diff
drivers/gpio/gpiolib.c
1 | #include <linux/kernel.h> | 1 | #include <linux/kernel.h> |
2 | #include <linux/module.h> | 2 | #include <linux/module.h> |
3 | #include <linux/interrupt.h> | 3 | #include <linux/interrupt.h> |
4 | #include <linux/irq.h> | 4 | #include <linux/irq.h> |
5 | #include <linux/spinlock.h> | 5 | #include <linux/spinlock.h> |
6 | #include <linux/list.h> | 6 | #include <linux/list.h> |
7 | #include <linux/device.h> | 7 | #include <linux/device.h> |
8 | #include <linux/err.h> | 8 | #include <linux/err.h> |
9 | #include <linux/debugfs.h> | 9 | #include <linux/debugfs.h> |
10 | #include <linux/seq_file.h> | 10 | #include <linux/seq_file.h> |
11 | #include <linux/gpio.h> | 11 | #include <linux/gpio.h> |
12 | #include <linux/of_gpio.h> | 12 | #include <linux/of_gpio.h> |
13 | #include <linux/idr.h> | 13 | #include <linux/idr.h> |
14 | #include <linux/slab.h> | 14 | #include <linux/slab.h> |
15 | 15 | ||
16 | #define CREATE_TRACE_POINTS | 16 | #define CREATE_TRACE_POINTS |
17 | #include <trace/events/gpio.h> | 17 | #include <trace/events/gpio.h> |
18 | 18 | ||
19 | /* Optional implementation infrastructure for GPIO interfaces. | 19 | /* Optional implementation infrastructure for GPIO interfaces. |
20 | * | 20 | * |
21 | * Platforms may want to use this if they tend to use very many GPIOs | 21 | * Platforms may want to use this if they tend to use very many GPIOs |
22 | * that aren't part of a System-On-Chip core; or across I2C/SPI/etc. | 22 | * that aren't part of a System-On-Chip core; or across I2C/SPI/etc. |
23 | * | 23 | * |
24 | * When kernel footprint or instruction count is an issue, simpler | 24 | * When kernel footprint or instruction count is an issue, simpler |
25 | * implementations may be preferred. The GPIO programming interface | 25 | * implementations may be preferred. The GPIO programming interface |
26 | * allows for inlining speed-critical get/set operations for common | 26 | * allows for inlining speed-critical get/set operations for common |
27 | * cases, so that access to SOC-integrated GPIOs can sometimes cost | 27 | * cases, so that access to SOC-integrated GPIOs can sometimes cost |
28 | * only an instruction or two per bit. | 28 | * only an instruction or two per bit. |
29 | */ | 29 | */ |
30 | 30 | ||
31 | 31 | ||
32 | /* When debugging, extend minimal trust to callers and platform code. | 32 | /* When debugging, extend minimal trust to callers and platform code. |
33 | * Also emit diagnostic messages that may help initial bringup, when | 33 | * Also emit diagnostic messages that may help initial bringup, when |
34 | * board setup or driver bugs are most common. | 34 | * board setup or driver bugs are most common. |
35 | * | 35 | * |
36 | * Otherwise, minimize overhead in what may be bitbanging codepaths. | 36 | * Otherwise, minimize overhead in what may be bitbanging codepaths. |
37 | */ | 37 | */ |
38 | #ifdef DEBUG | 38 | #ifdef DEBUG |
39 | #define extra_checks 1 | 39 | #define extra_checks 1 |
40 | #else | 40 | #else |
41 | #define extra_checks 0 | 41 | #define extra_checks 0 |
42 | #endif | 42 | #endif |
43 | 43 | ||
44 | /* gpio_lock prevents conflicts during gpio_desc[] table updates. | 44 | /* gpio_lock prevents conflicts during gpio_desc[] table updates. |
45 | * While any GPIO is requested, its gpio_chip is not removable; | 45 | * While any GPIO is requested, its gpio_chip is not removable; |
46 | * each GPIO's "requested" flag serves as a lock and refcount. | 46 | * each GPIO's "requested" flag serves as a lock and refcount. |
47 | */ | 47 | */ |
48 | static DEFINE_SPINLOCK(gpio_lock); | 48 | static DEFINE_SPINLOCK(gpio_lock); |
49 | 49 | ||
50 | struct gpio_desc { | 50 | struct gpio_desc { |
51 | struct gpio_chip *chip; | 51 | struct gpio_chip *chip; |
52 | unsigned long flags; | 52 | unsigned long flags; |
53 | /* flag symbols are bit numbers */ | 53 | /* flag symbols are bit numbers */ |
54 | #define FLAG_REQUESTED 0 | 54 | #define FLAG_REQUESTED 0 |
55 | #define FLAG_IS_OUT 1 | 55 | #define FLAG_IS_OUT 1 |
56 | #define FLAG_EXPORT 2 /* protected by sysfs_lock */ | 56 | #define FLAG_EXPORT 2 /* protected by sysfs_lock */ |
57 | #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */ | 57 | #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */ |
58 | #define FLAG_TRIG_FALL 4 /* trigger on falling edge */ | 58 | #define FLAG_TRIG_FALL 4 /* trigger on falling edge */ |
59 | #define FLAG_TRIG_RISE 5 /* trigger on rising edge */ | 59 | #define FLAG_TRIG_RISE 5 /* trigger on rising edge */ |
60 | #define FLAG_ACTIVE_LOW 6 /* sysfs value has active low */ | 60 | #define FLAG_ACTIVE_LOW 6 /* sysfs value has active low */ |
61 | #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ | 61 | #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ |
62 | #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ | 62 | #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ |
63 | 63 | ||
64 | #define ID_SHIFT 16 /* add new flags before this one */ | 64 | #define ID_SHIFT 16 /* add new flags before this one */ |
65 | 65 | ||
66 | #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1) | 66 | #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1) |
67 | #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE)) | 67 | #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE)) |
68 | 68 | ||
69 | #ifdef CONFIG_DEBUG_FS | 69 | #ifdef CONFIG_DEBUG_FS |
70 | const char *label; | 70 | const char *label; |
71 | #endif | 71 | #endif |
72 | }; | 72 | }; |
73 | static struct gpio_desc gpio_desc[ARCH_NR_GPIOS]; | 73 | static struct gpio_desc gpio_desc[ARCH_NR_GPIOS]; |
74 | 74 | ||
75 | #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio) | ||
76 | |||
75 | static LIST_HEAD(gpio_chips); | 77 | static LIST_HEAD(gpio_chips); |
76 | 78 | ||
77 | #ifdef CONFIG_GPIO_SYSFS | 79 | #ifdef CONFIG_GPIO_SYSFS |
78 | static DEFINE_IDR(dirent_idr); | 80 | static DEFINE_IDR(dirent_idr); |
79 | #endif | 81 | #endif |
80 | 82 | ||
81 | /* | 83 | /* |
82 | * Internal gpiod_* API using descriptors instead of the integer namespace. | 84 | * Internal gpiod_* API using descriptors instead of the integer namespace. |
83 | * Most of this should eventually go public. | 85 | * Most of this should eventually go public. |
84 | */ | 86 | */ |
85 | static int gpiod_request(struct gpio_desc *desc, const char *label); | 87 | static int gpiod_request(struct gpio_desc *desc, const char *label); |
86 | static void gpiod_free(struct gpio_desc *desc); | 88 | static void gpiod_free(struct gpio_desc *desc); |
87 | static int gpiod_direction_input(struct gpio_desc *desc); | 89 | static int gpiod_direction_input(struct gpio_desc *desc); |
88 | static int gpiod_direction_output(struct gpio_desc *desc, int value); | 90 | static int gpiod_direction_output(struct gpio_desc *desc, int value); |
89 | static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce); | 91 | static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce); |
90 | static int gpiod_get_value_cansleep(struct gpio_desc *desc); | 92 | static int gpiod_get_value_cansleep(struct gpio_desc *desc); |
91 | static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value); | 93 | static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value); |
92 | static int gpiod_get_value(struct gpio_desc *desc); | 94 | static int gpiod_get_value(struct gpio_desc *desc); |
93 | static void gpiod_set_value(struct gpio_desc *desc, int value); | 95 | static void gpiod_set_value(struct gpio_desc *desc, int value); |
94 | static int gpiod_cansleep(struct gpio_desc *desc); | 96 | static int gpiod_cansleep(struct gpio_desc *desc); |
95 | static int gpiod_to_irq(struct gpio_desc *desc); | 97 | static int gpiod_to_irq(struct gpio_desc *desc); |
96 | static int gpiod_export(struct gpio_desc *desc, bool direction_may_change); | 98 | static int gpiod_export(struct gpio_desc *desc, bool direction_may_change); |
97 | static int gpiod_export_link(struct device *dev, const char *name, | 99 | static int gpiod_export_link(struct device *dev, const char *name, |
98 | struct gpio_desc *desc); | 100 | struct gpio_desc *desc); |
99 | static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value); | 101 | static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value); |
100 | static void gpiod_unexport(struct gpio_desc *desc); | 102 | static void gpiod_unexport(struct gpio_desc *desc); |
101 | 103 | ||
102 | 104 | ||
103 | static inline void desc_set_label(struct gpio_desc *d, const char *label) | 105 | static inline void desc_set_label(struct gpio_desc *d, const char *label) |
104 | { | 106 | { |
105 | #ifdef CONFIG_DEBUG_FS | 107 | #ifdef CONFIG_DEBUG_FS |
106 | d->label = label; | 108 | d->label = label; |
107 | #endif | 109 | #endif |
108 | } | 110 | } |
109 | 111 | ||
110 | /* | 112 | /* |
111 | * Return the GPIO number of the passed descriptor relative to its chip | 113 | * Return the GPIO number of the passed descriptor relative to its chip |
112 | */ | 114 | */ |
113 | static int gpio_chip_hwgpio(const struct gpio_desc *desc) | 115 | static int gpio_chip_hwgpio(const struct gpio_desc *desc) |
114 | { | 116 | { |
115 | return (desc - &gpio_desc[0]) - desc->chip->base; | 117 | return desc - &desc->chip->desc[0]; |
116 | } | 118 | } |
117 | 119 | ||
118 | /** | 120 | /** |
119 | * Convert a GPIO number to its descriptor | 121 | * Convert a GPIO number to its descriptor |
120 | */ | 122 | */ |
121 | static struct gpio_desc *gpio_to_desc(unsigned gpio) | 123 | static struct gpio_desc *gpio_to_desc(unsigned gpio) |
122 | { | 124 | { |
123 | if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio)) | 125 | if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio)) |
124 | return NULL; | 126 | return NULL; |
125 | else | 127 | else |
126 | return &gpio_desc[gpio]; | 128 | return &gpio_desc[gpio]; |
127 | } | 129 | } |
128 | 130 | ||
129 | /** | 131 | /** |
130 | * Convert a GPIO descriptor to the integer namespace. | 132 | * Convert a GPIO descriptor to the integer namespace. |
131 | * This should disappear in the future but is needed since we still | 133 | * This should disappear in the future but is needed since we still |
132 | * use GPIO numbers for error messages and sysfs nodes | 134 | * use GPIO numbers for error messages and sysfs nodes |
133 | */ | 135 | */ |
134 | static int desc_to_gpio(const struct gpio_desc *desc) | 136 | static int desc_to_gpio(const struct gpio_desc *desc) |
135 | { | 137 | { |
136 | return desc - &gpio_desc[0]; | 138 | return desc->chip->base + gpio_chip_hwgpio(desc); |
137 | } | 139 | } |
138 | 140 | ||
139 | 141 | ||
140 | /* Warn when drivers omit gpio_request() calls -- legal but ill-advised | 142 | /* Warn when drivers omit gpio_request() calls -- legal but ill-advised |
141 | * when setting direction, and otherwise illegal. Until board setup code | 143 | * when setting direction, and otherwise illegal. Until board setup code |
142 | * and drivers use explicit requests everywhere (which won't happen when | 144 | * and drivers use explicit requests everywhere (which won't happen when |
143 | * those calls have no teeth) we can't avoid autorequesting. This nag | 145 | * those calls have no teeth) we can't avoid autorequesting. This nag |
144 | * message should motivate switching to explicit requests... so should | 146 | * message should motivate switching to explicit requests... so should |
145 | * the weaker cleanup after faults, compared to gpio_request(). | 147 | * the weaker cleanup after faults, compared to gpio_request(). |
146 | * | 148 | * |
147 | * NOTE: the autorequest mechanism is going away; at this point it's | 149 | * NOTE: the autorequest mechanism is going away; at this point it's |
148 | * only "legal" in the sense that (old) code using it won't break yet, | 150 | * only "legal" in the sense that (old) code using it won't break yet, |
149 | * but instead only triggers a WARN() stack dump. | 151 | * but instead only triggers a WARN() stack dump. |
150 | */ | 152 | */ |
151 | static int gpio_ensure_requested(struct gpio_desc *desc) | 153 | static int gpio_ensure_requested(struct gpio_desc *desc) |
152 | { | 154 | { |
153 | const struct gpio_chip *chip = desc->chip; | 155 | const struct gpio_chip *chip = desc->chip; |
154 | const int gpio = desc_to_gpio(desc); | 156 | const int gpio = desc_to_gpio(desc); |
155 | 157 | ||
156 | if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0, | 158 | if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0, |
157 | "autorequest GPIO-%d\n", gpio)) { | 159 | "autorequest GPIO-%d\n", gpio)) { |
158 | if (!try_module_get(chip->owner)) { | 160 | if (!try_module_get(chip->owner)) { |
159 | pr_err("GPIO-%d: module can't be gotten \n", gpio); | 161 | pr_err("GPIO-%d: module can't be gotten \n", gpio); |
160 | clear_bit(FLAG_REQUESTED, &desc->flags); | 162 | clear_bit(FLAG_REQUESTED, &desc->flags); |
161 | /* lose */ | 163 | /* lose */ |
162 | return -EIO; | 164 | return -EIO; |
163 | } | 165 | } |
164 | desc_set_label(desc, "[auto]"); | 166 | desc_set_label(desc, "[auto]"); |
165 | /* caller must chip->request() w/o spinlock */ | 167 | /* caller must chip->request() w/o spinlock */ |
166 | if (chip->request) | 168 | if (chip->request) |
167 | return 1; | 169 | return 1; |
168 | } | 170 | } |
169 | return 0; | 171 | return 0; |
170 | } | 172 | } |
171 | 173 | ||
172 | /* caller holds gpio_lock *OR* gpio is marked as requested */ | 174 | /* caller holds gpio_lock *OR* gpio is marked as requested */ |
173 | static struct gpio_chip *gpiod_to_chip(struct gpio_desc *desc) | 175 | static struct gpio_chip *gpiod_to_chip(struct gpio_desc *desc) |
174 | { | 176 | { |
175 | return desc->chip; | 177 | return desc->chip; |
176 | } | 178 | } |
177 | 179 | ||
178 | struct gpio_chip *gpio_to_chip(unsigned gpio) | 180 | struct gpio_chip *gpio_to_chip(unsigned gpio) |
179 | { | 181 | { |
180 | return gpiod_to_chip(gpio_to_desc(gpio)); | 182 | return gpiod_to_chip(gpio_to_desc(gpio)); |
181 | } | 183 | } |
182 | 184 | ||
183 | /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ | 185 | /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ |
184 | static int gpiochip_find_base(int ngpio) | 186 | static int gpiochip_find_base(int ngpio) |
185 | { | 187 | { |
186 | struct gpio_chip *chip; | 188 | struct gpio_chip *chip; |
187 | int base = ARCH_NR_GPIOS - ngpio; | 189 | int base = ARCH_NR_GPIOS - ngpio; |
188 | 190 | ||
189 | list_for_each_entry_reverse(chip, &gpio_chips, list) { | 191 | list_for_each_entry_reverse(chip, &gpio_chips, list) { |
190 | /* found a free space? */ | 192 | /* found a free space? */ |
191 | if (chip->base + chip->ngpio <= base) | 193 | if (chip->base + chip->ngpio <= base) |
192 | break; | 194 | break; |
193 | else | 195 | else |
194 | /* nope, check the space right before the chip */ | 196 | /* nope, check the space right before the chip */ |
195 | base = chip->base - ngpio; | 197 | base = chip->base - ngpio; |
196 | } | 198 | } |
197 | 199 | ||
198 | if (gpio_is_valid(base)) { | 200 | if (gpio_is_valid(base)) { |
199 | pr_debug("%s: found new base at %d\n", __func__, base); | 201 | pr_debug("%s: found new base at %d\n", __func__, base); |
200 | return base; | 202 | return base; |
201 | } else { | 203 | } else { |
202 | pr_err("%s: cannot find free range\n", __func__); | 204 | pr_err("%s: cannot find free range\n", __func__); |
203 | return -ENOSPC; | 205 | return -ENOSPC; |
204 | } | 206 | } |
205 | } | 207 | } |
206 | 208 | ||
207 | /* caller ensures gpio is valid and requested, chip->get_direction may sleep */ | 209 | /* caller ensures gpio is valid and requested, chip->get_direction may sleep */ |
208 | static int gpiod_get_direction(struct gpio_desc *desc) | 210 | static int gpiod_get_direction(struct gpio_desc *desc) |
209 | { | 211 | { |
210 | struct gpio_chip *chip; | 212 | struct gpio_chip *chip; |
211 | unsigned offset; | 213 | unsigned offset; |
212 | int status = -EINVAL; | 214 | int status = -EINVAL; |
213 | 215 | ||
214 | chip = gpiod_to_chip(desc); | 216 | chip = gpiod_to_chip(desc); |
215 | offset = gpio_chip_hwgpio(desc); | 217 | offset = gpio_chip_hwgpio(desc); |
216 | 218 | ||
217 | if (!chip->get_direction) | 219 | if (!chip->get_direction) |
218 | return status; | 220 | return status; |
219 | 221 | ||
220 | status = chip->get_direction(chip, offset); | 222 | status = chip->get_direction(chip, offset); |
221 | if (status > 0) { | 223 | if (status > 0) { |
222 | /* GPIOF_DIR_IN, or other positive */ | 224 | /* GPIOF_DIR_IN, or other positive */ |
223 | status = 1; | 225 | status = 1; |
224 | clear_bit(FLAG_IS_OUT, &desc->flags); | 226 | clear_bit(FLAG_IS_OUT, &desc->flags); |
225 | } | 227 | } |
226 | if (status == 0) { | 228 | if (status == 0) { |
227 | /* GPIOF_DIR_OUT */ | 229 | /* GPIOF_DIR_OUT */ |
228 | set_bit(FLAG_IS_OUT, &desc->flags); | 230 | set_bit(FLAG_IS_OUT, &desc->flags); |
229 | } | 231 | } |
230 | return status; | 232 | return status; |
231 | } | 233 | } |
232 | 234 | ||
233 | #ifdef CONFIG_GPIO_SYSFS | 235 | #ifdef CONFIG_GPIO_SYSFS |
234 | 236 | ||
235 | /* lock protects against unexport_gpio() being called while | 237 | /* lock protects against unexport_gpio() being called while |
236 | * sysfs files are active. | 238 | * sysfs files are active. |
237 | */ | 239 | */ |
238 | static DEFINE_MUTEX(sysfs_lock); | 240 | static DEFINE_MUTEX(sysfs_lock); |
239 | 241 | ||
240 | /* | 242 | /* |
241 | * /sys/class/gpio/gpioN... only for GPIOs that are exported | 243 | * /sys/class/gpio/gpioN... only for GPIOs that are exported |
242 | * /direction | 244 | * /direction |
243 | * * MAY BE OMITTED if kernel won't allow direction changes | 245 | * * MAY BE OMITTED if kernel won't allow direction changes |
244 | * * is read/write as "in" or "out" | 246 | * * is read/write as "in" or "out" |
245 | * * may also be written as "high" or "low", initializing | 247 | * * may also be written as "high" or "low", initializing |
246 | * output value as specified ("out" implies "low") | 248 | * output value as specified ("out" implies "low") |
247 | * /value | 249 | * /value |
248 | * * always readable, subject to hardware behavior | 250 | * * always readable, subject to hardware behavior |
249 | * * may be writable, as zero/nonzero | 251 | * * may be writable, as zero/nonzero |
250 | * /edge | 252 | * /edge |
251 | * * configures behavior of poll(2) on /value | 253 | * * configures behavior of poll(2) on /value |
252 | * * available only if pin can generate IRQs on input | 254 | * * available only if pin can generate IRQs on input |
253 | * * is read/write as "none", "falling", "rising", or "both" | 255 | * * is read/write as "none", "falling", "rising", or "both" |
254 | * /active_low | 256 | * /active_low |
255 | * * configures polarity of /value | 257 | * * configures polarity of /value |
256 | * * is read/write as zero/nonzero | 258 | * * is read/write as zero/nonzero |
257 | * * also affects existing and subsequent "falling" and "rising" | 259 | * * also affects existing and subsequent "falling" and "rising" |
258 | * /edge configuration | 260 | * /edge configuration |
259 | */ | 261 | */ |
260 | 262 | ||
261 | static ssize_t gpio_direction_show(struct device *dev, | 263 | static ssize_t gpio_direction_show(struct device *dev, |
262 | struct device_attribute *attr, char *buf) | 264 | struct device_attribute *attr, char *buf) |
263 | { | 265 | { |
264 | struct gpio_desc *desc = dev_get_drvdata(dev); | 266 | struct gpio_desc *desc = dev_get_drvdata(dev); |
265 | ssize_t status; | 267 | ssize_t status; |
266 | 268 | ||
267 | mutex_lock(&sysfs_lock); | 269 | mutex_lock(&sysfs_lock); |
268 | 270 | ||
269 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { | 271 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { |
270 | status = -EIO; | 272 | status = -EIO; |
271 | } else { | 273 | } else { |
272 | gpiod_get_direction(desc); | 274 | gpiod_get_direction(desc); |
273 | status = sprintf(buf, "%s\n", | 275 | status = sprintf(buf, "%s\n", |
274 | test_bit(FLAG_IS_OUT, &desc->flags) | 276 | test_bit(FLAG_IS_OUT, &desc->flags) |
275 | ? "out" : "in"); | 277 | ? "out" : "in"); |
276 | } | 278 | } |
277 | 279 | ||
278 | mutex_unlock(&sysfs_lock); | 280 | mutex_unlock(&sysfs_lock); |
279 | return status; | 281 | return status; |
280 | } | 282 | } |
281 | 283 | ||
282 | static ssize_t gpio_direction_store(struct device *dev, | 284 | static ssize_t gpio_direction_store(struct device *dev, |
283 | struct device_attribute *attr, const char *buf, size_t size) | 285 | struct device_attribute *attr, const char *buf, size_t size) |
284 | { | 286 | { |
285 | struct gpio_desc *desc = dev_get_drvdata(dev); | 287 | struct gpio_desc *desc = dev_get_drvdata(dev); |
286 | ssize_t status; | 288 | ssize_t status; |
287 | 289 | ||
288 | mutex_lock(&sysfs_lock); | 290 | mutex_lock(&sysfs_lock); |
289 | 291 | ||
290 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | 292 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
291 | status = -EIO; | 293 | status = -EIO; |
292 | else if (sysfs_streq(buf, "high")) | 294 | else if (sysfs_streq(buf, "high")) |
293 | status = gpiod_direction_output(desc, 1); | 295 | status = gpiod_direction_output(desc, 1); |
294 | else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low")) | 296 | else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low")) |
295 | status = gpiod_direction_output(desc, 0); | 297 | status = gpiod_direction_output(desc, 0); |
296 | else if (sysfs_streq(buf, "in")) | 298 | else if (sysfs_streq(buf, "in")) |
297 | status = gpiod_direction_input(desc); | 299 | status = gpiod_direction_input(desc); |
298 | else | 300 | else |
299 | status = -EINVAL; | 301 | status = -EINVAL; |
300 | 302 | ||
301 | mutex_unlock(&sysfs_lock); | 303 | mutex_unlock(&sysfs_lock); |
302 | return status ? : size; | 304 | return status ? : size; |
303 | } | 305 | } |
304 | 306 | ||
305 | static /* const */ DEVICE_ATTR(direction, 0644, | 307 | static /* const */ DEVICE_ATTR(direction, 0644, |
306 | gpio_direction_show, gpio_direction_store); | 308 | gpio_direction_show, gpio_direction_store); |
307 | 309 | ||
308 | static ssize_t gpio_value_show(struct device *dev, | 310 | static ssize_t gpio_value_show(struct device *dev, |
309 | struct device_attribute *attr, char *buf) | 311 | struct device_attribute *attr, char *buf) |
310 | { | 312 | { |
311 | struct gpio_desc *desc = dev_get_drvdata(dev); | 313 | struct gpio_desc *desc = dev_get_drvdata(dev); |
312 | ssize_t status; | 314 | ssize_t status; |
313 | 315 | ||
314 | mutex_lock(&sysfs_lock); | 316 | mutex_lock(&sysfs_lock); |
315 | 317 | ||
316 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { | 318 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { |
317 | status = -EIO; | 319 | status = -EIO; |
318 | } else { | 320 | } else { |
319 | int value; | 321 | int value; |
320 | 322 | ||
321 | value = !!gpiod_get_value_cansleep(desc); | 323 | value = !!gpiod_get_value_cansleep(desc); |
322 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | 324 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
323 | value = !value; | 325 | value = !value; |
324 | 326 | ||
325 | status = sprintf(buf, "%d\n", value); | 327 | status = sprintf(buf, "%d\n", value); |
326 | } | 328 | } |
327 | 329 | ||
328 | mutex_unlock(&sysfs_lock); | 330 | mutex_unlock(&sysfs_lock); |
329 | return status; | 331 | return status; |
330 | } | 332 | } |
331 | 333 | ||
332 | static ssize_t gpio_value_store(struct device *dev, | 334 | static ssize_t gpio_value_store(struct device *dev, |
333 | struct device_attribute *attr, const char *buf, size_t size) | 335 | struct device_attribute *attr, const char *buf, size_t size) |
334 | { | 336 | { |
335 | struct gpio_desc *desc = dev_get_drvdata(dev); | 337 | struct gpio_desc *desc = dev_get_drvdata(dev); |
336 | ssize_t status; | 338 | ssize_t status; |
337 | 339 | ||
338 | mutex_lock(&sysfs_lock); | 340 | mutex_lock(&sysfs_lock); |
339 | 341 | ||
340 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | 342 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
341 | status = -EIO; | 343 | status = -EIO; |
342 | else if (!test_bit(FLAG_IS_OUT, &desc->flags)) | 344 | else if (!test_bit(FLAG_IS_OUT, &desc->flags)) |
343 | status = -EPERM; | 345 | status = -EPERM; |
344 | else { | 346 | else { |
345 | long value; | 347 | long value; |
346 | 348 | ||
347 | status = strict_strtol(buf, 0, &value); | 349 | status = strict_strtol(buf, 0, &value); |
348 | if (status == 0) { | 350 | if (status == 0) { |
349 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | 351 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
350 | value = !value; | 352 | value = !value; |
351 | gpiod_set_value_cansleep(desc, value != 0); | 353 | gpiod_set_value_cansleep(desc, value != 0); |
352 | status = size; | 354 | status = size; |
353 | } | 355 | } |
354 | } | 356 | } |
355 | 357 | ||
356 | mutex_unlock(&sysfs_lock); | 358 | mutex_unlock(&sysfs_lock); |
357 | return status; | 359 | return status; |
358 | } | 360 | } |
359 | 361 | ||
360 | static const DEVICE_ATTR(value, 0644, | 362 | static const DEVICE_ATTR(value, 0644, |
361 | gpio_value_show, gpio_value_store); | 363 | gpio_value_show, gpio_value_store); |
362 | 364 | ||
363 | static irqreturn_t gpio_sysfs_irq(int irq, void *priv) | 365 | static irqreturn_t gpio_sysfs_irq(int irq, void *priv) |
364 | { | 366 | { |
365 | struct sysfs_dirent *value_sd = priv; | 367 | struct sysfs_dirent *value_sd = priv; |
366 | 368 | ||
367 | sysfs_notify_dirent(value_sd); | 369 | sysfs_notify_dirent(value_sd); |
368 | return IRQ_HANDLED; | 370 | return IRQ_HANDLED; |
369 | } | 371 | } |
370 | 372 | ||
371 | static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev, | 373 | static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev, |
372 | unsigned long gpio_flags) | 374 | unsigned long gpio_flags) |
373 | { | 375 | { |
374 | struct sysfs_dirent *value_sd; | 376 | struct sysfs_dirent *value_sd; |
375 | unsigned long irq_flags; | 377 | unsigned long irq_flags; |
376 | int ret, irq, id; | 378 | int ret, irq, id; |
377 | 379 | ||
378 | if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags) | 380 | if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags) |
379 | return 0; | 381 | return 0; |
380 | 382 | ||
381 | irq = gpiod_to_irq(desc); | 383 | irq = gpiod_to_irq(desc); |
382 | if (irq < 0) | 384 | if (irq < 0) |
383 | return -EIO; | 385 | return -EIO; |
384 | 386 | ||
385 | id = desc->flags >> ID_SHIFT; | 387 | id = desc->flags >> ID_SHIFT; |
386 | value_sd = idr_find(&dirent_idr, id); | 388 | value_sd = idr_find(&dirent_idr, id); |
387 | if (value_sd) | 389 | if (value_sd) |
388 | free_irq(irq, value_sd); | 390 | free_irq(irq, value_sd); |
389 | 391 | ||
390 | desc->flags &= ~GPIO_TRIGGER_MASK; | 392 | desc->flags &= ~GPIO_TRIGGER_MASK; |
391 | 393 | ||
392 | if (!gpio_flags) { | 394 | if (!gpio_flags) { |
393 | ret = 0; | 395 | ret = 0; |
394 | goto free_id; | 396 | goto free_id; |
395 | } | 397 | } |
396 | 398 | ||
397 | irq_flags = IRQF_SHARED; | 399 | irq_flags = IRQF_SHARED; |
398 | if (test_bit(FLAG_TRIG_FALL, &gpio_flags)) | 400 | if (test_bit(FLAG_TRIG_FALL, &gpio_flags)) |
399 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? | 401 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
400 | IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; | 402 | IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; |
401 | if (test_bit(FLAG_TRIG_RISE, &gpio_flags)) | 403 | if (test_bit(FLAG_TRIG_RISE, &gpio_flags)) |
402 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? | 404 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
403 | IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; | 405 | IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; |
404 | 406 | ||
405 | if (!value_sd) { | 407 | if (!value_sd) { |
406 | value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value"); | 408 | value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value"); |
407 | if (!value_sd) { | 409 | if (!value_sd) { |
408 | ret = -ENODEV; | 410 | ret = -ENODEV; |
409 | goto err_out; | 411 | goto err_out; |
410 | } | 412 | } |
411 | 413 | ||
412 | do { | 414 | do { |
413 | ret = -ENOMEM; | 415 | ret = -ENOMEM; |
414 | if (idr_pre_get(&dirent_idr, GFP_KERNEL)) | 416 | if (idr_pre_get(&dirent_idr, GFP_KERNEL)) |
415 | ret = idr_get_new_above(&dirent_idr, value_sd, | 417 | ret = idr_get_new_above(&dirent_idr, value_sd, |
416 | 1, &id); | 418 | 1, &id); |
417 | } while (ret == -EAGAIN); | 419 | } while (ret == -EAGAIN); |
418 | 420 | ||
419 | if (ret) | 421 | if (ret) |
420 | goto free_sd; | 422 | goto free_sd; |
421 | 423 | ||
422 | desc->flags &= GPIO_FLAGS_MASK; | 424 | desc->flags &= GPIO_FLAGS_MASK; |
423 | desc->flags |= (unsigned long)id << ID_SHIFT; | 425 | desc->flags |= (unsigned long)id << ID_SHIFT; |
424 | 426 | ||
425 | if (desc->flags >> ID_SHIFT != id) { | 427 | if (desc->flags >> ID_SHIFT != id) { |
426 | ret = -ERANGE; | 428 | ret = -ERANGE; |
427 | goto free_id; | 429 | goto free_id; |
428 | } | 430 | } |
429 | } | 431 | } |
430 | 432 | ||
431 | ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags, | 433 | ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags, |
432 | "gpiolib", value_sd); | 434 | "gpiolib", value_sd); |
433 | if (ret < 0) | 435 | if (ret < 0) |
434 | goto free_id; | 436 | goto free_id; |
435 | 437 | ||
436 | desc->flags |= gpio_flags; | 438 | desc->flags |= gpio_flags; |
437 | return 0; | 439 | return 0; |
438 | 440 | ||
439 | free_id: | 441 | free_id: |
440 | idr_remove(&dirent_idr, id); | 442 | idr_remove(&dirent_idr, id); |
441 | desc->flags &= GPIO_FLAGS_MASK; | 443 | desc->flags &= GPIO_FLAGS_MASK; |
442 | free_sd: | 444 | free_sd: |
443 | if (value_sd) | 445 | if (value_sd) |
444 | sysfs_put(value_sd); | 446 | sysfs_put(value_sd); |
445 | err_out: | 447 | err_out: |
446 | return ret; | 448 | return ret; |
447 | } | 449 | } |
448 | 450 | ||
449 | static const struct { | 451 | static const struct { |
450 | const char *name; | 452 | const char *name; |
451 | unsigned long flags; | 453 | unsigned long flags; |
452 | } trigger_types[] = { | 454 | } trigger_types[] = { |
453 | { "none", 0 }, | 455 | { "none", 0 }, |
454 | { "falling", BIT(FLAG_TRIG_FALL) }, | 456 | { "falling", BIT(FLAG_TRIG_FALL) }, |
455 | { "rising", BIT(FLAG_TRIG_RISE) }, | 457 | { "rising", BIT(FLAG_TRIG_RISE) }, |
456 | { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) }, | 458 | { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) }, |
457 | }; | 459 | }; |
458 | 460 | ||
459 | static ssize_t gpio_edge_show(struct device *dev, | 461 | static ssize_t gpio_edge_show(struct device *dev, |
460 | struct device_attribute *attr, char *buf) | 462 | struct device_attribute *attr, char *buf) |
461 | { | 463 | { |
462 | const struct gpio_desc *desc = dev_get_drvdata(dev); | 464 | const struct gpio_desc *desc = dev_get_drvdata(dev); |
463 | ssize_t status; | 465 | ssize_t status; |
464 | 466 | ||
465 | mutex_lock(&sysfs_lock); | 467 | mutex_lock(&sysfs_lock); |
466 | 468 | ||
467 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | 469 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
468 | status = -EIO; | 470 | status = -EIO; |
469 | else { | 471 | else { |
470 | int i; | 472 | int i; |
471 | 473 | ||
472 | status = 0; | 474 | status = 0; |
473 | for (i = 0; i < ARRAY_SIZE(trigger_types); i++) | 475 | for (i = 0; i < ARRAY_SIZE(trigger_types); i++) |
474 | if ((desc->flags & GPIO_TRIGGER_MASK) | 476 | if ((desc->flags & GPIO_TRIGGER_MASK) |
475 | == trigger_types[i].flags) { | 477 | == trigger_types[i].flags) { |
476 | status = sprintf(buf, "%s\n", | 478 | status = sprintf(buf, "%s\n", |
477 | trigger_types[i].name); | 479 | trigger_types[i].name); |
478 | break; | 480 | break; |
479 | } | 481 | } |
480 | } | 482 | } |
481 | 483 | ||
482 | mutex_unlock(&sysfs_lock); | 484 | mutex_unlock(&sysfs_lock); |
483 | return status; | 485 | return status; |
484 | } | 486 | } |
485 | 487 | ||
486 | static ssize_t gpio_edge_store(struct device *dev, | 488 | static ssize_t gpio_edge_store(struct device *dev, |
487 | struct device_attribute *attr, const char *buf, size_t size) | 489 | struct device_attribute *attr, const char *buf, size_t size) |
488 | { | 490 | { |
489 | struct gpio_desc *desc = dev_get_drvdata(dev); | 491 | struct gpio_desc *desc = dev_get_drvdata(dev); |
490 | ssize_t status; | 492 | ssize_t status; |
491 | int i; | 493 | int i; |
492 | 494 | ||
493 | for (i = 0; i < ARRAY_SIZE(trigger_types); i++) | 495 | for (i = 0; i < ARRAY_SIZE(trigger_types); i++) |
494 | if (sysfs_streq(trigger_types[i].name, buf)) | 496 | if (sysfs_streq(trigger_types[i].name, buf)) |
495 | goto found; | 497 | goto found; |
496 | return -EINVAL; | 498 | return -EINVAL; |
497 | 499 | ||
498 | found: | 500 | found: |
499 | mutex_lock(&sysfs_lock); | 501 | mutex_lock(&sysfs_lock); |
500 | 502 | ||
501 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | 503 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
502 | status = -EIO; | 504 | status = -EIO; |
503 | else { | 505 | else { |
504 | status = gpio_setup_irq(desc, dev, trigger_types[i].flags); | 506 | status = gpio_setup_irq(desc, dev, trigger_types[i].flags); |
505 | if (!status) | 507 | if (!status) |
506 | status = size; | 508 | status = size; |
507 | } | 509 | } |
508 | 510 | ||
509 | mutex_unlock(&sysfs_lock); | 511 | mutex_unlock(&sysfs_lock); |
510 | 512 | ||
511 | return status; | 513 | return status; |
512 | } | 514 | } |
513 | 515 | ||
514 | static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store); | 516 | static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store); |
515 | 517 | ||
516 | static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev, | 518 | static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev, |
517 | int value) | 519 | int value) |
518 | { | 520 | { |
519 | int status = 0; | 521 | int status = 0; |
520 | 522 | ||
521 | if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value) | 523 | if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value) |
522 | return 0; | 524 | return 0; |
523 | 525 | ||
524 | if (value) | 526 | if (value) |
525 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); | 527 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); |
526 | else | 528 | else |
527 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); | 529 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); |
528 | 530 | ||
529 | /* reconfigure poll(2) support if enabled on one edge only */ | 531 | /* reconfigure poll(2) support if enabled on one edge only */ |
530 | if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^ | 532 | if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^ |
531 | !!test_bit(FLAG_TRIG_FALL, &desc->flags))) { | 533 | !!test_bit(FLAG_TRIG_FALL, &desc->flags))) { |
532 | unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK; | 534 | unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK; |
533 | 535 | ||
534 | gpio_setup_irq(desc, dev, 0); | 536 | gpio_setup_irq(desc, dev, 0); |
535 | status = gpio_setup_irq(desc, dev, trigger_flags); | 537 | status = gpio_setup_irq(desc, dev, trigger_flags); |
536 | } | 538 | } |
537 | 539 | ||
538 | return status; | 540 | return status; |
539 | } | 541 | } |
540 | 542 | ||
541 | static ssize_t gpio_active_low_show(struct device *dev, | 543 | static ssize_t gpio_active_low_show(struct device *dev, |
542 | struct device_attribute *attr, char *buf) | 544 | struct device_attribute *attr, char *buf) |
543 | { | 545 | { |
544 | const struct gpio_desc *desc = dev_get_drvdata(dev); | 546 | const struct gpio_desc *desc = dev_get_drvdata(dev); |
545 | ssize_t status; | 547 | ssize_t status; |
546 | 548 | ||
547 | mutex_lock(&sysfs_lock); | 549 | mutex_lock(&sysfs_lock); |
548 | 550 | ||
549 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | 551 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
550 | status = -EIO; | 552 | status = -EIO; |
551 | else | 553 | else |
552 | status = sprintf(buf, "%d\n", | 554 | status = sprintf(buf, "%d\n", |
553 | !!test_bit(FLAG_ACTIVE_LOW, &desc->flags)); | 555 | !!test_bit(FLAG_ACTIVE_LOW, &desc->flags)); |
554 | 556 | ||
555 | mutex_unlock(&sysfs_lock); | 557 | mutex_unlock(&sysfs_lock); |
556 | 558 | ||
557 | return status; | 559 | return status; |
558 | } | 560 | } |
559 | 561 | ||
560 | static ssize_t gpio_active_low_store(struct device *dev, | 562 | static ssize_t gpio_active_low_store(struct device *dev, |
561 | struct device_attribute *attr, const char *buf, size_t size) | 563 | struct device_attribute *attr, const char *buf, size_t size) |
562 | { | 564 | { |
563 | struct gpio_desc *desc = dev_get_drvdata(dev); | 565 | struct gpio_desc *desc = dev_get_drvdata(dev); |
564 | ssize_t status; | 566 | ssize_t status; |
565 | 567 | ||
566 | mutex_lock(&sysfs_lock); | 568 | mutex_lock(&sysfs_lock); |
567 | 569 | ||
568 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { | 570 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { |
569 | status = -EIO; | 571 | status = -EIO; |
570 | } else { | 572 | } else { |
571 | long value; | 573 | long value; |
572 | 574 | ||
573 | status = strict_strtol(buf, 0, &value); | 575 | status = strict_strtol(buf, 0, &value); |
574 | if (status == 0) | 576 | if (status == 0) |
575 | status = sysfs_set_active_low(desc, dev, value != 0); | 577 | status = sysfs_set_active_low(desc, dev, value != 0); |
576 | } | 578 | } |
577 | 579 | ||
578 | mutex_unlock(&sysfs_lock); | 580 | mutex_unlock(&sysfs_lock); |
579 | 581 | ||
580 | return status ? : size; | 582 | return status ? : size; |
581 | } | 583 | } |
582 | 584 | ||
583 | static const DEVICE_ATTR(active_low, 0644, | 585 | static const DEVICE_ATTR(active_low, 0644, |
584 | gpio_active_low_show, gpio_active_low_store); | 586 | gpio_active_low_show, gpio_active_low_store); |
585 | 587 | ||
586 | static const struct attribute *gpio_attrs[] = { | 588 | static const struct attribute *gpio_attrs[] = { |
587 | &dev_attr_value.attr, | 589 | &dev_attr_value.attr, |
588 | &dev_attr_active_low.attr, | 590 | &dev_attr_active_low.attr, |
589 | NULL, | 591 | NULL, |
590 | }; | 592 | }; |
591 | 593 | ||
592 | static const struct attribute_group gpio_attr_group = { | 594 | static const struct attribute_group gpio_attr_group = { |
593 | .attrs = (struct attribute **) gpio_attrs, | 595 | .attrs = (struct attribute **) gpio_attrs, |
594 | }; | 596 | }; |
595 | 597 | ||
596 | /* | 598 | /* |
597 | * /sys/class/gpio/gpiochipN/ | 599 | * /sys/class/gpio/gpiochipN/ |
598 | * /base ... matching gpio_chip.base (N) | 600 | * /base ... matching gpio_chip.base (N) |
599 | * /label ... matching gpio_chip.label | 601 | * /label ... matching gpio_chip.label |
600 | * /ngpio ... matching gpio_chip.ngpio | 602 | * /ngpio ... matching gpio_chip.ngpio |
601 | */ | 603 | */ |
602 | 604 | ||
603 | static ssize_t chip_base_show(struct device *dev, | 605 | static ssize_t chip_base_show(struct device *dev, |
604 | struct device_attribute *attr, char *buf) | 606 | struct device_attribute *attr, char *buf) |
605 | { | 607 | { |
606 | const struct gpio_chip *chip = dev_get_drvdata(dev); | 608 | const struct gpio_chip *chip = dev_get_drvdata(dev); |
607 | 609 | ||
608 | return sprintf(buf, "%d\n", chip->base); | 610 | return sprintf(buf, "%d\n", chip->base); |
609 | } | 611 | } |
610 | static DEVICE_ATTR(base, 0444, chip_base_show, NULL); | 612 | static DEVICE_ATTR(base, 0444, chip_base_show, NULL); |
611 | 613 | ||
612 | static ssize_t chip_label_show(struct device *dev, | 614 | static ssize_t chip_label_show(struct device *dev, |
613 | struct device_attribute *attr, char *buf) | 615 | struct device_attribute *attr, char *buf) |
614 | { | 616 | { |
615 | const struct gpio_chip *chip = dev_get_drvdata(dev); | 617 | const struct gpio_chip *chip = dev_get_drvdata(dev); |
616 | 618 | ||
617 | return sprintf(buf, "%s\n", chip->label ? : ""); | 619 | return sprintf(buf, "%s\n", chip->label ? : ""); |
618 | } | 620 | } |
619 | static DEVICE_ATTR(label, 0444, chip_label_show, NULL); | 621 | static DEVICE_ATTR(label, 0444, chip_label_show, NULL); |
620 | 622 | ||
621 | static ssize_t chip_ngpio_show(struct device *dev, | 623 | static ssize_t chip_ngpio_show(struct device *dev, |
622 | struct device_attribute *attr, char *buf) | 624 | struct device_attribute *attr, char *buf) |
623 | { | 625 | { |
624 | const struct gpio_chip *chip = dev_get_drvdata(dev); | 626 | const struct gpio_chip *chip = dev_get_drvdata(dev); |
625 | 627 | ||
626 | return sprintf(buf, "%u\n", chip->ngpio); | 628 | return sprintf(buf, "%u\n", chip->ngpio); |
627 | } | 629 | } |
628 | static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL); | 630 | static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL); |
629 | 631 | ||
630 | static const struct attribute *gpiochip_attrs[] = { | 632 | static const struct attribute *gpiochip_attrs[] = { |
631 | &dev_attr_base.attr, | 633 | &dev_attr_base.attr, |
632 | &dev_attr_label.attr, | 634 | &dev_attr_label.attr, |
633 | &dev_attr_ngpio.attr, | 635 | &dev_attr_ngpio.attr, |
634 | NULL, | 636 | NULL, |
635 | }; | 637 | }; |
636 | 638 | ||
637 | static const struct attribute_group gpiochip_attr_group = { | 639 | static const struct attribute_group gpiochip_attr_group = { |
638 | .attrs = (struct attribute **) gpiochip_attrs, | 640 | .attrs = (struct attribute **) gpiochip_attrs, |
639 | }; | 641 | }; |
640 | 642 | ||
641 | /* | 643 | /* |
642 | * /sys/class/gpio/export ... write-only | 644 | * /sys/class/gpio/export ... write-only |
643 | * integer N ... number of GPIO to export (full access) | 645 | * integer N ... number of GPIO to export (full access) |
644 | * /sys/class/gpio/unexport ... write-only | 646 | * /sys/class/gpio/unexport ... write-only |
645 | * integer N ... number of GPIO to unexport | 647 | * integer N ... number of GPIO to unexport |
646 | */ | 648 | */ |
647 | static ssize_t export_store(struct class *class, | 649 | static ssize_t export_store(struct class *class, |
648 | struct class_attribute *attr, | 650 | struct class_attribute *attr, |
649 | const char *buf, size_t len) | 651 | const char *buf, size_t len) |
650 | { | 652 | { |
651 | long gpio; | 653 | long gpio; |
652 | struct gpio_desc *desc; | 654 | struct gpio_desc *desc; |
653 | int status; | 655 | int status; |
654 | 656 | ||
655 | status = strict_strtol(buf, 0, &gpio); | 657 | status = strict_strtol(buf, 0, &gpio); |
656 | if (status < 0) | 658 | if (status < 0) |
657 | goto done; | 659 | goto done; |
658 | 660 | ||
659 | desc = gpio_to_desc(gpio); | 661 | desc = gpio_to_desc(gpio); |
660 | 662 | ||
661 | /* No extra locking here; FLAG_SYSFS just signifies that the | 663 | /* No extra locking here; FLAG_SYSFS just signifies that the |
662 | * request and export were done by on behalf of userspace, so | 664 | * request and export were done by on behalf of userspace, so |
663 | * they may be undone on its behalf too. | 665 | * they may be undone on its behalf too. |
664 | */ | 666 | */ |
665 | 667 | ||
666 | status = gpiod_request(desc, "sysfs"); | 668 | status = gpiod_request(desc, "sysfs"); |
667 | if (status < 0) { | 669 | if (status < 0) { |
668 | if (status == -EPROBE_DEFER) | 670 | if (status == -EPROBE_DEFER) |
669 | status = -ENODEV; | 671 | status = -ENODEV; |
670 | goto done; | 672 | goto done; |
671 | } | 673 | } |
672 | status = gpiod_export(desc, true); | 674 | status = gpiod_export(desc, true); |
673 | if (status < 0) | 675 | if (status < 0) |
674 | gpiod_free(desc); | 676 | gpiod_free(desc); |
675 | else | 677 | else |
676 | set_bit(FLAG_SYSFS, &desc->flags); | 678 | set_bit(FLAG_SYSFS, &desc->flags); |
677 | 679 | ||
678 | done: | 680 | done: |
679 | if (status) | 681 | if (status) |
680 | pr_debug("%s: status %d\n", __func__, status); | 682 | pr_debug("%s: status %d\n", __func__, status); |
681 | return status ? : len; | 683 | return status ? : len; |
682 | } | 684 | } |
683 | 685 | ||
684 | static ssize_t unexport_store(struct class *class, | 686 | static ssize_t unexport_store(struct class *class, |
685 | struct class_attribute *attr, | 687 | struct class_attribute *attr, |
686 | const char *buf, size_t len) | 688 | const char *buf, size_t len) |
687 | { | 689 | { |
688 | long gpio; | 690 | long gpio; |
689 | struct gpio_desc *desc; | 691 | struct gpio_desc *desc; |
690 | int status; | 692 | int status; |
691 | 693 | ||
692 | status = strict_strtol(buf, 0, &gpio); | 694 | status = strict_strtol(buf, 0, &gpio); |
693 | if (status < 0) | 695 | if (status < 0) |
694 | goto done; | 696 | goto done; |
695 | 697 | ||
696 | status = -EINVAL; | 698 | status = -EINVAL; |
697 | 699 | ||
698 | desc = gpio_to_desc(gpio); | 700 | desc = gpio_to_desc(gpio); |
699 | /* reject bogus commands (gpio_unexport ignores them) */ | 701 | /* reject bogus commands (gpio_unexport ignores them) */ |
700 | if (!desc) | 702 | if (!desc) |
701 | goto done; | 703 | goto done; |
702 | 704 | ||
703 | /* No extra locking here; FLAG_SYSFS just signifies that the | 705 | /* No extra locking here; FLAG_SYSFS just signifies that the |
704 | * request and export were done by on behalf of userspace, so | 706 | * request and export were done by on behalf of userspace, so |
705 | * they may be undone on its behalf too. | 707 | * they may be undone on its behalf too. |
706 | */ | 708 | */ |
707 | if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) { | 709 | if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) { |
708 | status = 0; | 710 | status = 0; |
709 | gpiod_free(desc); | 711 | gpiod_free(desc); |
710 | } | 712 | } |
711 | done: | 713 | done: |
712 | if (status) | 714 | if (status) |
713 | pr_debug("%s: status %d\n", __func__, status); | 715 | pr_debug("%s: status %d\n", __func__, status); |
714 | return status ? : len; | 716 | return status ? : len; |
715 | } | 717 | } |
716 | 718 | ||
717 | static struct class_attribute gpio_class_attrs[] = { | 719 | static struct class_attribute gpio_class_attrs[] = { |
718 | __ATTR(export, 0200, NULL, export_store), | 720 | __ATTR(export, 0200, NULL, export_store), |
719 | __ATTR(unexport, 0200, NULL, unexport_store), | 721 | __ATTR(unexport, 0200, NULL, unexport_store), |
720 | __ATTR_NULL, | 722 | __ATTR_NULL, |
721 | }; | 723 | }; |
722 | 724 | ||
723 | static struct class gpio_class = { | 725 | static struct class gpio_class = { |
724 | .name = "gpio", | 726 | .name = "gpio", |
725 | .owner = THIS_MODULE, | 727 | .owner = THIS_MODULE, |
726 | 728 | ||
727 | .class_attrs = gpio_class_attrs, | 729 | .class_attrs = gpio_class_attrs, |
728 | }; | 730 | }; |
729 | 731 | ||
730 | 732 | ||
731 | /** | 733 | /** |
732 | * gpio_export - export a GPIO through sysfs | 734 | * gpio_export - export a GPIO through sysfs |
733 | * @gpio: gpio to make available, already requested | 735 | * @gpio: gpio to make available, already requested |
734 | * @direction_may_change: true if userspace may change gpio direction | 736 | * @direction_may_change: true if userspace may change gpio direction |
735 | * Context: arch_initcall or later | 737 | * Context: arch_initcall or later |
736 | * | 738 | * |
737 | * When drivers want to make a GPIO accessible to userspace after they | 739 | * When drivers want to make a GPIO accessible to userspace after they |
738 | * have requested it -- perhaps while debugging, or as part of their | 740 | * have requested it -- perhaps while debugging, or as part of their |
739 | * public interface -- they may use this routine. If the GPIO can | 741 | * public interface -- they may use this routine. If the GPIO can |
740 | * change direction (some can't) and the caller allows it, userspace | 742 | * change direction (some can't) and the caller allows it, userspace |
741 | * will see "direction" sysfs attribute which may be used to change | 743 | * will see "direction" sysfs attribute which may be used to change |
742 | * the gpio's direction. A "value" attribute will always be provided. | 744 | * the gpio's direction. A "value" attribute will always be provided. |
743 | * | 745 | * |
744 | * Returns zero on success, else an error. | 746 | * Returns zero on success, else an error. |
745 | */ | 747 | */ |
746 | static int gpiod_export(struct gpio_desc *desc, bool direction_may_change) | 748 | static int gpiod_export(struct gpio_desc *desc, bool direction_may_change) |
747 | { | 749 | { |
748 | unsigned long flags; | 750 | unsigned long flags; |
749 | int status; | 751 | int status; |
750 | const char *ioname = NULL; | 752 | const char *ioname = NULL; |
751 | struct device *dev; | 753 | struct device *dev; |
752 | int offset; | 754 | int offset; |
753 | 755 | ||
754 | /* can't export until sysfs is available ... */ | 756 | /* can't export until sysfs is available ... */ |
755 | if (!gpio_class.p) { | 757 | if (!gpio_class.p) { |
756 | pr_debug("%s: called too early!\n", __func__); | 758 | pr_debug("%s: called too early!\n", __func__); |
757 | return -ENOENT; | 759 | return -ENOENT; |
758 | } | 760 | } |
759 | 761 | ||
760 | if (!desc) { | 762 | if (!desc) { |
761 | pr_debug("%s: invalid gpio descriptor\n", __func__); | 763 | pr_debug("%s: invalid gpio descriptor\n", __func__); |
762 | return -EINVAL; | 764 | return -EINVAL; |
763 | } | 765 | } |
764 | 766 | ||
765 | mutex_lock(&sysfs_lock); | 767 | mutex_lock(&sysfs_lock); |
766 | 768 | ||
767 | spin_lock_irqsave(&gpio_lock, flags); | 769 | spin_lock_irqsave(&gpio_lock, flags); |
768 | if (!test_bit(FLAG_REQUESTED, &desc->flags) || | 770 | if (!test_bit(FLAG_REQUESTED, &desc->flags) || |
769 | test_bit(FLAG_EXPORT, &desc->flags)) { | 771 | test_bit(FLAG_EXPORT, &desc->flags)) { |
770 | spin_unlock_irqrestore(&gpio_lock, flags); | 772 | spin_unlock_irqrestore(&gpio_lock, flags); |
771 | pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n", | 773 | pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n", |
772 | __func__, desc_to_gpio(desc), | 774 | __func__, desc_to_gpio(desc), |
773 | test_bit(FLAG_REQUESTED, &desc->flags), | 775 | test_bit(FLAG_REQUESTED, &desc->flags), |
774 | test_bit(FLAG_EXPORT, &desc->flags)); | 776 | test_bit(FLAG_EXPORT, &desc->flags)); |
775 | status = -EPERM; | 777 | status = -EPERM; |
776 | goto fail_unlock; | 778 | goto fail_unlock; |
777 | } | 779 | } |
778 | 780 | ||
779 | if (!desc->chip->direction_input || !desc->chip->direction_output) | 781 | if (!desc->chip->direction_input || !desc->chip->direction_output) |
780 | direction_may_change = false; | 782 | direction_may_change = false; |
781 | spin_unlock_irqrestore(&gpio_lock, flags); | 783 | spin_unlock_irqrestore(&gpio_lock, flags); |
782 | 784 | ||
783 | offset = gpio_chip_hwgpio(desc); | 785 | offset = gpio_chip_hwgpio(desc); |
784 | if (desc->chip->names && desc->chip->names[offset]) | 786 | if (desc->chip->names && desc->chip->names[offset]) |
785 | ioname = desc->chip->names[offset]; | 787 | ioname = desc->chip->names[offset]; |
786 | 788 | ||
787 | dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0), | 789 | dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0), |
788 | desc, ioname ? ioname : "gpio%u", | 790 | desc, ioname ? ioname : "gpio%u", |
789 | desc_to_gpio(desc)); | 791 | desc_to_gpio(desc)); |
790 | if (IS_ERR(dev)) { | 792 | if (IS_ERR(dev)) { |
791 | status = PTR_ERR(dev); | 793 | status = PTR_ERR(dev); |
792 | goto fail_unlock; | 794 | goto fail_unlock; |
793 | } | 795 | } |
794 | 796 | ||
795 | status = sysfs_create_group(&dev->kobj, &gpio_attr_group); | 797 | status = sysfs_create_group(&dev->kobj, &gpio_attr_group); |
796 | if (status) | 798 | if (status) |
797 | goto fail_unregister_device; | 799 | goto fail_unregister_device; |
798 | 800 | ||
799 | if (direction_may_change) { | 801 | if (direction_may_change) { |
800 | status = device_create_file(dev, &dev_attr_direction); | 802 | status = device_create_file(dev, &dev_attr_direction); |
801 | if (status) | 803 | if (status) |
802 | goto fail_unregister_device; | 804 | goto fail_unregister_device; |
803 | } | 805 | } |
804 | 806 | ||
805 | if (gpiod_to_irq(desc) >= 0 && (direction_may_change || | 807 | if (gpiod_to_irq(desc) >= 0 && (direction_may_change || |
806 | !test_bit(FLAG_IS_OUT, &desc->flags))) { | 808 | !test_bit(FLAG_IS_OUT, &desc->flags))) { |
807 | status = device_create_file(dev, &dev_attr_edge); | 809 | status = device_create_file(dev, &dev_attr_edge); |
808 | if (status) | 810 | if (status) |
809 | goto fail_unregister_device; | 811 | goto fail_unregister_device; |
810 | } | 812 | } |
811 | 813 | ||
812 | set_bit(FLAG_EXPORT, &desc->flags); | 814 | set_bit(FLAG_EXPORT, &desc->flags); |
813 | mutex_unlock(&sysfs_lock); | 815 | mutex_unlock(&sysfs_lock); |
814 | return 0; | 816 | return 0; |
815 | 817 | ||
816 | fail_unregister_device: | 818 | fail_unregister_device: |
817 | device_unregister(dev); | 819 | device_unregister(dev); |
818 | fail_unlock: | 820 | fail_unlock: |
819 | mutex_unlock(&sysfs_lock); | 821 | mutex_unlock(&sysfs_lock); |
820 | pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), | 822 | pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), |
821 | status); | 823 | status); |
822 | return status; | 824 | return status; |
823 | } | 825 | } |
824 | 826 | ||
825 | int gpio_export(unsigned gpio, bool direction_may_change) | 827 | int gpio_export(unsigned gpio, bool direction_may_change) |
826 | { | 828 | { |
827 | return gpiod_export(gpio_to_desc(gpio), direction_may_change); | 829 | return gpiod_export(gpio_to_desc(gpio), direction_may_change); |
828 | } | 830 | } |
829 | EXPORT_SYMBOL_GPL(gpio_export); | 831 | EXPORT_SYMBOL_GPL(gpio_export); |
830 | 832 | ||
831 | static int match_export(struct device *dev, void *data) | 833 | static int match_export(struct device *dev, void *data) |
832 | { | 834 | { |
833 | return dev_get_drvdata(dev) == data; | 835 | return dev_get_drvdata(dev) == data; |
834 | } | 836 | } |
835 | 837 | ||
836 | /** | 838 | /** |
837 | * gpio_export_link - create a sysfs link to an exported GPIO node | 839 | * gpio_export_link - create a sysfs link to an exported GPIO node |
838 | * @dev: device under which to create symlink | 840 | * @dev: device under which to create symlink |
839 | * @name: name of the symlink | 841 | * @name: name of the symlink |
840 | * @gpio: gpio to create symlink to, already exported | 842 | * @gpio: gpio to create symlink to, already exported |
841 | * | 843 | * |
842 | * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN | 844 | * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN |
843 | * node. Caller is responsible for unlinking. | 845 | * node. Caller is responsible for unlinking. |
844 | * | 846 | * |
845 | * Returns zero on success, else an error. | 847 | * Returns zero on success, else an error. |
846 | */ | 848 | */ |
847 | static int gpiod_export_link(struct device *dev, const char *name, | 849 | static int gpiod_export_link(struct device *dev, const char *name, |
848 | struct gpio_desc *desc) | 850 | struct gpio_desc *desc) |
849 | { | 851 | { |
850 | int status = -EINVAL; | 852 | int status = -EINVAL; |
851 | 853 | ||
852 | if (!desc) | 854 | if (!desc) |
853 | goto done; | 855 | goto done; |
854 | 856 | ||
855 | mutex_lock(&sysfs_lock); | 857 | mutex_lock(&sysfs_lock); |
856 | 858 | ||
857 | if (test_bit(FLAG_EXPORT, &desc->flags)) { | 859 | if (test_bit(FLAG_EXPORT, &desc->flags)) { |
858 | struct device *tdev; | 860 | struct device *tdev; |
859 | 861 | ||
860 | tdev = class_find_device(&gpio_class, NULL, desc, match_export); | 862 | tdev = class_find_device(&gpio_class, NULL, desc, match_export); |
861 | if (tdev != NULL) { | 863 | if (tdev != NULL) { |
862 | status = sysfs_create_link(&dev->kobj, &tdev->kobj, | 864 | status = sysfs_create_link(&dev->kobj, &tdev->kobj, |
863 | name); | 865 | name); |
864 | } else { | 866 | } else { |
865 | status = -ENODEV; | 867 | status = -ENODEV; |
866 | } | 868 | } |
867 | } | 869 | } |
868 | 870 | ||
869 | mutex_unlock(&sysfs_lock); | 871 | mutex_unlock(&sysfs_lock); |
870 | 872 | ||
871 | done: | 873 | done: |
872 | if (status) | 874 | if (status) |
873 | pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), | 875 | pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), |
874 | status); | 876 | status); |
875 | 877 | ||
876 | return status; | 878 | return status; |
877 | } | 879 | } |
878 | 880 | ||
879 | int gpio_export_link(struct device *dev, const char *name, unsigned gpio) | 881 | int gpio_export_link(struct device *dev, const char *name, unsigned gpio) |
880 | { | 882 | { |
881 | return gpiod_export_link(dev, name, gpio_to_desc(gpio)); | 883 | return gpiod_export_link(dev, name, gpio_to_desc(gpio)); |
882 | } | 884 | } |
883 | EXPORT_SYMBOL_GPL(gpio_export_link); | 885 | EXPORT_SYMBOL_GPL(gpio_export_link); |
884 | 886 | ||
885 | /** | 887 | /** |
886 | * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value | 888 | * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value |
887 | * @gpio: gpio to change | 889 | * @gpio: gpio to change |
888 | * @value: non-zero to use active low, i.e. inverted values | 890 | * @value: non-zero to use active low, i.e. inverted values |
889 | * | 891 | * |
890 | * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute. | 892 | * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute. |
891 | * The GPIO does not have to be exported yet. If poll(2) support has | 893 | * The GPIO does not have to be exported yet. If poll(2) support has |
892 | * been enabled for either rising or falling edge, it will be | 894 | * been enabled for either rising or falling edge, it will be |
893 | * reconfigured to follow the new polarity. | 895 | * reconfigured to follow the new polarity. |
894 | * | 896 | * |
895 | * Returns zero on success, else an error. | 897 | * Returns zero on success, else an error. |
896 | */ | 898 | */ |
897 | static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) | 899 | static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) |
898 | { | 900 | { |
899 | struct device *dev = NULL; | 901 | struct device *dev = NULL; |
900 | int status = -EINVAL; | 902 | int status = -EINVAL; |
901 | 903 | ||
902 | if (!desc) | 904 | if (!desc) |
903 | goto done; | 905 | goto done; |
904 | 906 | ||
905 | mutex_lock(&sysfs_lock); | 907 | mutex_lock(&sysfs_lock); |
906 | 908 | ||
907 | if (test_bit(FLAG_EXPORT, &desc->flags)) { | 909 | if (test_bit(FLAG_EXPORT, &desc->flags)) { |
908 | dev = class_find_device(&gpio_class, NULL, desc, match_export); | 910 | dev = class_find_device(&gpio_class, NULL, desc, match_export); |
909 | if (dev == NULL) { | 911 | if (dev == NULL) { |
910 | status = -ENODEV; | 912 | status = -ENODEV; |
911 | goto unlock; | 913 | goto unlock; |
912 | } | 914 | } |
913 | } | 915 | } |
914 | 916 | ||
915 | status = sysfs_set_active_low(desc, dev, value); | 917 | status = sysfs_set_active_low(desc, dev, value); |
916 | 918 | ||
917 | unlock: | 919 | unlock: |
918 | mutex_unlock(&sysfs_lock); | 920 | mutex_unlock(&sysfs_lock); |
919 | 921 | ||
920 | done: | 922 | done: |
921 | if (status) | 923 | if (status) |
922 | pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), | 924 | pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), |
923 | status); | 925 | status); |
924 | 926 | ||
925 | return status; | 927 | return status; |
926 | } | 928 | } |
927 | 929 | ||
928 | int gpio_sysfs_set_active_low(unsigned gpio, int value) | 930 | int gpio_sysfs_set_active_low(unsigned gpio, int value) |
929 | { | 931 | { |
930 | return gpiod_sysfs_set_active_low(gpio_to_desc(gpio), value); | 932 | return gpiod_sysfs_set_active_low(gpio_to_desc(gpio), value); |
931 | } | 933 | } |
932 | EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low); | 934 | EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low); |
933 | 935 | ||
934 | /** | 936 | /** |
935 | * gpio_unexport - reverse effect of gpio_export() | 937 | * gpio_unexport - reverse effect of gpio_export() |
936 | * @gpio: gpio to make unavailable | 938 | * @gpio: gpio to make unavailable |
937 | * | 939 | * |
938 | * This is implicit on gpio_free(). | 940 | * This is implicit on gpio_free(). |
939 | */ | 941 | */ |
940 | static void gpiod_unexport(struct gpio_desc *desc) | 942 | static void gpiod_unexport(struct gpio_desc *desc) |
941 | { | 943 | { |
942 | int status = 0; | 944 | int status = 0; |
943 | struct device *dev = NULL; | 945 | struct device *dev = NULL; |
944 | 946 | ||
945 | if (!desc) { | 947 | if (!desc) { |
946 | status = -EINVAL; | 948 | status = -EINVAL; |
947 | goto done; | 949 | goto done; |
948 | } | 950 | } |
949 | 951 | ||
950 | mutex_lock(&sysfs_lock); | 952 | mutex_lock(&sysfs_lock); |
951 | 953 | ||
952 | if (test_bit(FLAG_EXPORT, &desc->flags)) { | 954 | if (test_bit(FLAG_EXPORT, &desc->flags)) { |
953 | 955 | ||
954 | dev = class_find_device(&gpio_class, NULL, desc, match_export); | 956 | dev = class_find_device(&gpio_class, NULL, desc, match_export); |
955 | if (dev) { | 957 | if (dev) { |
956 | gpio_setup_irq(desc, dev, 0); | 958 | gpio_setup_irq(desc, dev, 0); |
957 | clear_bit(FLAG_EXPORT, &desc->flags); | 959 | clear_bit(FLAG_EXPORT, &desc->flags); |
958 | } else | 960 | } else |
959 | status = -ENODEV; | 961 | status = -ENODEV; |
960 | } | 962 | } |
961 | 963 | ||
962 | mutex_unlock(&sysfs_lock); | 964 | mutex_unlock(&sysfs_lock); |
963 | 965 | ||
964 | if (dev) { | 966 | if (dev) { |
965 | device_unregister(dev); | 967 | device_unregister(dev); |
966 | put_device(dev); | 968 | put_device(dev); |
967 | } | 969 | } |
968 | done: | 970 | done: |
969 | if (status) | 971 | if (status) |
970 | pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), | 972 | pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), |
971 | status); | 973 | status); |
972 | } | 974 | } |
973 | 975 | ||
974 | void gpio_unexport(unsigned gpio) | 976 | void gpio_unexport(unsigned gpio) |
975 | { | 977 | { |
976 | gpiod_unexport(gpio_to_desc(gpio)); | 978 | gpiod_unexport(gpio_to_desc(gpio)); |
977 | } | 979 | } |
978 | EXPORT_SYMBOL_GPL(gpio_unexport); | 980 | EXPORT_SYMBOL_GPL(gpio_unexport); |
979 | 981 | ||
980 | static int gpiochip_export(struct gpio_chip *chip) | 982 | static int gpiochip_export(struct gpio_chip *chip) |
981 | { | 983 | { |
982 | int status; | 984 | int status; |
983 | struct device *dev; | 985 | struct device *dev; |
984 | 986 | ||
985 | /* Many systems register gpio chips for SOC support very early, | 987 | /* Many systems register gpio chips for SOC support very early, |
986 | * before driver model support is available. In those cases we | 988 | * before driver model support is available. In those cases we |
987 | * export this later, in gpiolib_sysfs_init() ... here we just | 989 | * export this later, in gpiolib_sysfs_init() ... here we just |
988 | * verify that _some_ field of gpio_class got initialized. | 990 | * verify that _some_ field of gpio_class got initialized. |
989 | */ | 991 | */ |
990 | if (!gpio_class.p) | 992 | if (!gpio_class.p) |
991 | return 0; | 993 | return 0; |
992 | 994 | ||
993 | /* use chip->base for the ID; it's already known to be unique */ | 995 | /* use chip->base for the ID; it's already known to be unique */ |
994 | mutex_lock(&sysfs_lock); | 996 | mutex_lock(&sysfs_lock); |
995 | dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip, | 997 | dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip, |
996 | "gpiochip%d", chip->base); | 998 | "gpiochip%d", chip->base); |
997 | if (!IS_ERR(dev)) { | 999 | if (!IS_ERR(dev)) { |
998 | status = sysfs_create_group(&dev->kobj, | 1000 | status = sysfs_create_group(&dev->kobj, |
999 | &gpiochip_attr_group); | 1001 | &gpiochip_attr_group); |
1000 | } else | 1002 | } else |
1001 | status = PTR_ERR(dev); | 1003 | status = PTR_ERR(dev); |
1002 | chip->exported = (status == 0); | 1004 | chip->exported = (status == 0); |
1003 | mutex_unlock(&sysfs_lock); | 1005 | mutex_unlock(&sysfs_lock); |
1004 | 1006 | ||
1005 | if (status) { | 1007 | if (status) { |
1006 | unsigned long flags; | 1008 | unsigned long flags; |
1007 | unsigned gpio; | 1009 | unsigned gpio; |
1008 | 1010 | ||
1009 | spin_lock_irqsave(&gpio_lock, flags); | 1011 | spin_lock_irqsave(&gpio_lock, flags); |
1010 | gpio = chip->base; | 1012 | gpio = 0; |
1011 | while (gpio_desc[gpio].chip == chip) | 1013 | while (gpio < chip->ngpio) |
1012 | gpio_desc[gpio++].chip = NULL; | 1014 | chip->desc[gpio++].chip = NULL; |
1013 | spin_unlock_irqrestore(&gpio_lock, flags); | 1015 | spin_unlock_irqrestore(&gpio_lock, flags); |
1014 | 1016 | ||
1015 | pr_debug("%s: chip %s status %d\n", __func__, | 1017 | pr_debug("%s: chip %s status %d\n", __func__, |
1016 | chip->label, status); | 1018 | chip->label, status); |
1017 | } | 1019 | } |
1018 | 1020 | ||
1019 | return status; | 1021 | return status; |
1020 | } | 1022 | } |
1021 | 1023 | ||
1022 | static void gpiochip_unexport(struct gpio_chip *chip) | 1024 | static void gpiochip_unexport(struct gpio_chip *chip) |
1023 | { | 1025 | { |
1024 | int status; | 1026 | int status; |
1025 | struct device *dev; | 1027 | struct device *dev; |
1026 | 1028 | ||
1027 | mutex_lock(&sysfs_lock); | 1029 | mutex_lock(&sysfs_lock); |
1028 | dev = class_find_device(&gpio_class, NULL, chip, match_export); | 1030 | dev = class_find_device(&gpio_class, NULL, chip, match_export); |
1029 | if (dev) { | 1031 | if (dev) { |
1030 | put_device(dev); | 1032 | put_device(dev); |
1031 | device_unregister(dev); | 1033 | device_unregister(dev); |
1032 | chip->exported = 0; | 1034 | chip->exported = 0; |
1033 | status = 0; | 1035 | status = 0; |
1034 | } else | 1036 | } else |
1035 | status = -ENODEV; | 1037 | status = -ENODEV; |
1036 | mutex_unlock(&sysfs_lock); | 1038 | mutex_unlock(&sysfs_lock); |
1037 | 1039 | ||
1038 | if (status) | 1040 | if (status) |
1039 | pr_debug("%s: chip %s status %d\n", __func__, | 1041 | pr_debug("%s: chip %s status %d\n", __func__, |
1040 | chip->label, status); | 1042 | chip->label, status); |
1041 | } | 1043 | } |
1042 | 1044 | ||
1043 | static int __init gpiolib_sysfs_init(void) | 1045 | static int __init gpiolib_sysfs_init(void) |
1044 | { | 1046 | { |
1045 | int status; | 1047 | int status; |
1046 | unsigned long flags; | 1048 | unsigned long flags; |
1047 | struct gpio_chip *chip; | 1049 | struct gpio_chip *chip; |
1048 | 1050 | ||
1049 | status = class_register(&gpio_class); | 1051 | status = class_register(&gpio_class); |
1050 | if (status < 0) | 1052 | if (status < 0) |
1051 | return status; | 1053 | return status; |
1052 | 1054 | ||
1053 | /* Scan and register the gpio_chips which registered very | 1055 | /* Scan and register the gpio_chips which registered very |
1054 | * early (e.g. before the class_register above was called). | 1056 | * early (e.g. before the class_register above was called). |
1055 | * | 1057 | * |
1056 | * We run before arch_initcall() so chip->dev nodes can have | 1058 | * We run before arch_initcall() so chip->dev nodes can have |
1057 | * registered, and so arch_initcall() can always gpio_export(). | 1059 | * registered, and so arch_initcall() can always gpio_export(). |
1058 | */ | 1060 | */ |
1059 | spin_lock_irqsave(&gpio_lock, flags); | 1061 | spin_lock_irqsave(&gpio_lock, flags); |
1060 | list_for_each_entry(chip, &gpio_chips, list) { | 1062 | list_for_each_entry(chip, &gpio_chips, list) { |
1061 | if (!chip || chip->exported) | 1063 | if (!chip || chip->exported) |
1062 | continue; | 1064 | continue; |
1063 | 1065 | ||
1064 | spin_unlock_irqrestore(&gpio_lock, flags); | 1066 | spin_unlock_irqrestore(&gpio_lock, flags); |
1065 | status = gpiochip_export(chip); | 1067 | status = gpiochip_export(chip); |
1066 | spin_lock_irqsave(&gpio_lock, flags); | 1068 | spin_lock_irqsave(&gpio_lock, flags); |
1067 | } | 1069 | } |
1068 | spin_unlock_irqrestore(&gpio_lock, flags); | 1070 | spin_unlock_irqrestore(&gpio_lock, flags); |
1069 | 1071 | ||
1070 | 1072 | ||
1071 | return status; | 1073 | return status; |
1072 | } | 1074 | } |
1073 | postcore_initcall(gpiolib_sysfs_init); | 1075 | postcore_initcall(gpiolib_sysfs_init); |
1074 | 1076 | ||
1075 | #else | 1077 | #else |
1076 | static inline int gpiochip_export(struct gpio_chip *chip) | 1078 | static inline int gpiochip_export(struct gpio_chip *chip) |
1077 | { | 1079 | { |
1078 | return 0; | 1080 | return 0; |
1079 | } | 1081 | } |
1080 | 1082 | ||
1081 | static inline void gpiochip_unexport(struct gpio_chip *chip) | 1083 | static inline void gpiochip_unexport(struct gpio_chip *chip) |
1082 | { | 1084 | { |
1083 | } | 1085 | } |
1084 | 1086 | ||
1085 | static inline int gpiod_export(struct gpio_desc *desc, | 1087 | static inline int gpiod_export(struct gpio_desc *desc, |
1086 | bool direction_may_change) | 1088 | bool direction_may_change) |
1087 | { | 1089 | { |
1088 | return -ENOSYS; | 1090 | return -ENOSYS; |
1089 | } | 1091 | } |
1090 | 1092 | ||
1091 | static inline int gpiod_export_link(struct device *dev, const char *name, | 1093 | static inline int gpiod_export_link(struct device *dev, const char *name, |
1092 | struct gpio_desc *desc) | 1094 | struct gpio_desc *desc) |
1093 | { | 1095 | { |
1094 | return -ENOSYS; | 1096 | return -ENOSYS; |
1095 | } | 1097 | } |
1096 | 1098 | ||
1097 | static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) | 1099 | static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) |
1098 | { | 1100 | { |
1099 | return -ENOSYS; | 1101 | return -ENOSYS; |
1100 | } | 1102 | } |
1101 | 1103 | ||
1102 | static inline void gpiod_unexport(struct gpio_desc *desc) | 1104 | static inline void gpiod_unexport(struct gpio_desc *desc) |
1103 | { | 1105 | { |
1104 | } | 1106 | } |
1105 | 1107 | ||
1106 | #endif /* CONFIG_GPIO_SYSFS */ | 1108 | #endif /* CONFIG_GPIO_SYSFS */ |
1107 | 1109 | ||
1108 | /* | 1110 | /* |
1109 | * Add a new chip to the global chips list, keeping the list of chips sorted | 1111 | * Add a new chip to the global chips list, keeping the list of chips sorted |
1110 | * by base order. | 1112 | * by base order. |
1111 | * | 1113 | * |
1112 | * Return -EBUSY if the new chip overlaps with some other chip's integer | 1114 | * Return -EBUSY if the new chip overlaps with some other chip's integer |
1113 | * space. | 1115 | * space. |
1114 | */ | 1116 | */ |
1115 | static int gpiochip_add_to_list(struct gpio_chip *chip) | 1117 | static int gpiochip_add_to_list(struct gpio_chip *chip) |
1116 | { | 1118 | { |
1117 | struct list_head *pos = &gpio_chips; | 1119 | struct list_head *pos = &gpio_chips; |
1118 | struct gpio_chip *_chip; | 1120 | struct gpio_chip *_chip; |
1119 | int err = 0; | 1121 | int err = 0; |
1120 | 1122 | ||
1121 | /* find where to insert our chip */ | 1123 | /* find where to insert our chip */ |
1122 | list_for_each(pos, &gpio_chips) { | 1124 | list_for_each(pos, &gpio_chips) { |
1123 | _chip = list_entry(pos, struct gpio_chip, list); | 1125 | _chip = list_entry(pos, struct gpio_chip, list); |
1124 | /* shall we insert before _chip? */ | 1126 | /* shall we insert before _chip? */ |
1125 | if (_chip->base >= chip->base + chip->ngpio) | 1127 | if (_chip->base >= chip->base + chip->ngpio) |
1126 | break; | 1128 | break; |
1127 | } | 1129 | } |
1128 | 1130 | ||
1129 | /* are we stepping on the chip right before? */ | 1131 | /* are we stepping on the chip right before? */ |
1130 | if (pos != &gpio_chips && pos->prev != &gpio_chips) { | 1132 | if (pos != &gpio_chips && pos->prev != &gpio_chips) { |
1131 | _chip = list_entry(pos->prev, struct gpio_chip, list); | 1133 | _chip = list_entry(pos->prev, struct gpio_chip, list); |
1132 | if (_chip->base + _chip->ngpio > chip->base) { | 1134 | if (_chip->base + _chip->ngpio > chip->base) { |
1133 | dev_err(chip->dev, | 1135 | dev_err(chip->dev, |
1134 | "GPIO integer space overlap, cannot add chip\n"); | 1136 | "GPIO integer space overlap, cannot add chip\n"); |
1135 | err = -EBUSY; | 1137 | err = -EBUSY; |
1136 | } | 1138 | } |
1137 | } | 1139 | } |
1138 | 1140 | ||
1139 | if (!err) | 1141 | if (!err) |
1140 | list_add_tail(&chip->list, pos); | 1142 | list_add_tail(&chip->list, pos); |
1141 | 1143 | ||
1142 | return err; | 1144 | return err; |
1143 | } | 1145 | } |
1144 | 1146 | ||
1145 | /** | 1147 | /** |
1146 | * gpiochip_add() - register a gpio_chip | 1148 | * gpiochip_add() - register a gpio_chip |
1147 | * @chip: the chip to register, with chip->base initialized | 1149 | * @chip: the chip to register, with chip->base initialized |
1148 | * Context: potentially before irqs or kmalloc will work | 1150 | * Context: potentially before irqs or kmalloc will work |
1149 | * | 1151 | * |
1150 | * Returns a negative errno if the chip can't be registered, such as | 1152 | * Returns a negative errno if the chip can't be registered, such as |
1151 | * because the chip->base is invalid or already associated with a | 1153 | * because the chip->base is invalid or already associated with a |
1152 | * different chip. Otherwise it returns zero as a success code. | 1154 | * different chip. Otherwise it returns zero as a success code. |
1153 | * | 1155 | * |
1154 | * When gpiochip_add() is called very early during boot, so that GPIOs | 1156 | * When gpiochip_add() is called very early during boot, so that GPIOs |
1155 | * can be freely used, the chip->dev device must be registered before | 1157 | * can be freely used, the chip->dev device must be registered before |
1156 | * the gpio framework's arch_initcall(). Otherwise sysfs initialization | 1158 | * the gpio framework's arch_initcall(). Otherwise sysfs initialization |
1157 | * for GPIOs will fail rudely. | 1159 | * for GPIOs will fail rudely. |
1158 | * | 1160 | * |
1159 | * If chip->base is negative, this requests dynamic assignment of | 1161 | * If chip->base is negative, this requests dynamic assignment of |
1160 | * a range of valid GPIOs. | 1162 | * a range of valid GPIOs. |
1161 | */ | 1163 | */ |
1162 | int gpiochip_add(struct gpio_chip *chip) | 1164 | int gpiochip_add(struct gpio_chip *chip) |
1163 | { | 1165 | { |
1164 | unsigned long flags; | 1166 | unsigned long flags; |
1165 | int status = 0; | 1167 | int status = 0; |
1166 | unsigned id; | 1168 | unsigned id; |
1167 | int base = chip->base; | 1169 | int base = chip->base; |
1168 | 1170 | ||
1169 | if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1)) | 1171 | if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1)) |
1170 | && base >= 0) { | 1172 | && base >= 0) { |
1171 | status = -EINVAL; | 1173 | status = -EINVAL; |
1172 | goto fail; | 1174 | goto fail; |
1173 | } | 1175 | } |
1174 | 1176 | ||
1175 | spin_lock_irqsave(&gpio_lock, flags); | 1177 | spin_lock_irqsave(&gpio_lock, flags); |
1176 | 1178 | ||
1177 | if (base < 0) { | 1179 | if (base < 0) { |
1178 | base = gpiochip_find_base(chip->ngpio); | 1180 | base = gpiochip_find_base(chip->ngpio); |
1179 | if (base < 0) { | 1181 | if (base < 0) { |
1180 | status = base; | 1182 | status = base; |
1181 | goto unlock; | 1183 | goto unlock; |
1182 | } | 1184 | } |
1183 | chip->base = base; | 1185 | chip->base = base; |
1184 | } | 1186 | } |
1185 | 1187 | ||
1186 | status = gpiochip_add_to_list(chip); | 1188 | status = gpiochip_add_to_list(chip); |
1187 | 1189 | ||
1188 | if (status == 0) { | 1190 | if (status == 0) { |
1189 | for (id = base; id < base + chip->ngpio; id++) { | 1191 | chip->desc = &gpio_desc[chip->base]; |
1190 | gpio_desc[id].chip = chip; | ||
1191 | 1192 | ||
1193 | for (id = 0; id < chip->ngpio; id++) { | ||
1194 | struct gpio_desc *desc = &chip->desc[id]; | ||
1195 | desc->chip = chip; | ||
1196 | |||
1192 | /* REVISIT: most hardware initializes GPIOs as | 1197 | /* REVISIT: most hardware initializes GPIOs as |
1193 | * inputs (often with pullups enabled) so power | 1198 | * inputs (often with pullups enabled) so power |
1194 | * usage is minimized. Linux code should set the | 1199 | * usage is minimized. Linux code should set the |
1195 | * gpio direction first thing; but until it does, | 1200 | * gpio direction first thing; but until it does, |
1196 | * and in case chip->get_direction is not set, | 1201 | * and in case chip->get_direction is not set, |
1197 | * we may expose the wrong direction in sysfs. | 1202 | * we may expose the wrong direction in sysfs. |
1198 | */ | 1203 | */ |
1199 | gpio_desc[id].flags = !chip->direction_input | 1204 | desc->flags = !chip->direction_input |
1200 | ? (1 << FLAG_IS_OUT) | 1205 | ? (1 << FLAG_IS_OUT) |
1201 | : 0; | 1206 | : 0; |
1202 | } | 1207 | } |
1203 | } | 1208 | } |
1204 | 1209 | ||
1205 | #ifdef CONFIG_PINCTRL | 1210 | #ifdef CONFIG_PINCTRL |
1206 | INIT_LIST_HEAD(&chip->pin_ranges); | 1211 | INIT_LIST_HEAD(&chip->pin_ranges); |
1207 | #endif | 1212 | #endif |
1208 | 1213 | ||
1209 | of_gpiochip_add(chip); | 1214 | of_gpiochip_add(chip); |
1210 | 1215 | ||
1211 | unlock: | 1216 | unlock: |
1212 | spin_unlock_irqrestore(&gpio_lock, flags); | 1217 | spin_unlock_irqrestore(&gpio_lock, flags); |
1213 | 1218 | ||
1214 | if (status) | 1219 | if (status) |
1215 | goto fail; | 1220 | goto fail; |
1216 | 1221 | ||
1217 | status = gpiochip_export(chip); | 1222 | status = gpiochip_export(chip); |
1218 | if (status) | 1223 | if (status) |
1219 | goto fail; | 1224 | goto fail; |
1220 | 1225 | ||
1221 | pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n", | 1226 | pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n", |
1222 | chip->base, chip->base + chip->ngpio - 1, | 1227 | chip->base, chip->base + chip->ngpio - 1, |
1223 | chip->label ? : "generic"); | 1228 | chip->label ? : "generic"); |
1224 | 1229 | ||
1225 | return 0; | 1230 | return 0; |
1226 | fail: | 1231 | fail: |
1227 | /* failures here can mean systems won't boot... */ | 1232 | /* failures here can mean systems won't boot... */ |
1228 | pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n", | 1233 | pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n", |
1229 | chip->base, chip->base + chip->ngpio - 1, | 1234 | chip->base, chip->base + chip->ngpio - 1, |
1230 | chip->label ? : "generic"); | 1235 | chip->label ? : "generic"); |
1231 | return status; | 1236 | return status; |
1232 | } | 1237 | } |
1233 | EXPORT_SYMBOL_GPL(gpiochip_add); | 1238 | EXPORT_SYMBOL_GPL(gpiochip_add); |
1234 | 1239 | ||
1235 | /** | 1240 | /** |
1236 | * gpiochip_remove() - unregister a gpio_chip | 1241 | * gpiochip_remove() - unregister a gpio_chip |
1237 | * @chip: the chip to unregister | 1242 | * @chip: the chip to unregister |
1238 | * | 1243 | * |
1239 | * A gpio_chip with any GPIOs still requested may not be removed. | 1244 | * A gpio_chip with any GPIOs still requested may not be removed. |
1240 | */ | 1245 | */ |
1241 | int gpiochip_remove(struct gpio_chip *chip) | 1246 | int gpiochip_remove(struct gpio_chip *chip) |
1242 | { | 1247 | { |
1243 | unsigned long flags; | 1248 | unsigned long flags; |
1244 | int status = 0; | 1249 | int status = 0; |
1245 | unsigned id; | 1250 | unsigned id; |
1246 | 1251 | ||
1247 | spin_lock_irqsave(&gpio_lock, flags); | 1252 | spin_lock_irqsave(&gpio_lock, flags); |
1248 | 1253 | ||
1249 | gpiochip_remove_pin_ranges(chip); | 1254 | gpiochip_remove_pin_ranges(chip); |
1250 | of_gpiochip_remove(chip); | 1255 | of_gpiochip_remove(chip); |
1251 | 1256 | ||
1252 | for (id = chip->base; id < chip->base + chip->ngpio; id++) { | 1257 | for (id = 0; id < chip->ngpio; id++) { |
1253 | if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) { | 1258 | if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) { |
1254 | status = -EBUSY; | 1259 | status = -EBUSY; |
1255 | break; | 1260 | break; |
1256 | } | 1261 | } |
1257 | } | 1262 | } |
1258 | if (status == 0) { | 1263 | if (status == 0) { |
1259 | for (id = chip->base; id < chip->base + chip->ngpio; id++) | 1264 | for (id = 0; id < chip->ngpio; id++) |
1260 | gpio_desc[id].chip = NULL; | 1265 | chip->desc[id].chip = NULL; |
1261 | 1266 | ||
1262 | list_del(&chip->list); | 1267 | list_del(&chip->list); |
1263 | } | 1268 | } |
1264 | 1269 | ||
1265 | spin_unlock_irqrestore(&gpio_lock, flags); | 1270 | spin_unlock_irqrestore(&gpio_lock, flags); |
1266 | 1271 | ||
1267 | if (status == 0) | 1272 | if (status == 0) |
1268 | gpiochip_unexport(chip); | 1273 | gpiochip_unexport(chip); |
1269 | 1274 | ||
1270 | return status; | 1275 | return status; |
1271 | } | 1276 | } |
1272 | EXPORT_SYMBOL_GPL(gpiochip_remove); | 1277 | EXPORT_SYMBOL_GPL(gpiochip_remove); |
1273 | 1278 | ||
1274 | /** | 1279 | /** |
1275 | * gpiochip_find() - iterator for locating a specific gpio_chip | 1280 | * gpiochip_find() - iterator for locating a specific gpio_chip |
1276 | * @data: data to pass to match function | 1281 | * @data: data to pass to match function |
1277 | * @callback: Callback function to check gpio_chip | 1282 | * @callback: Callback function to check gpio_chip |
1278 | * | 1283 | * |
1279 | * Similar to bus_find_device. It returns a reference to a gpio_chip as | 1284 | * Similar to bus_find_device. It returns a reference to a gpio_chip as |
1280 | * determined by a user supplied @match callback. The callback should return | 1285 | * determined by a user supplied @match callback. The callback should return |
1281 | * 0 if the device doesn't match and non-zero if it does. If the callback is | 1286 | * 0 if the device doesn't match and non-zero if it does. If the callback is |
1282 | * non-zero, this function will return to the caller and not iterate over any | 1287 | * non-zero, this function will return to the caller and not iterate over any |
1283 | * more gpio_chips. | 1288 | * more gpio_chips. |
1284 | */ | 1289 | */ |
1285 | struct gpio_chip *gpiochip_find(void *data, | 1290 | struct gpio_chip *gpiochip_find(void *data, |
1286 | int (*match)(struct gpio_chip *chip, | 1291 | int (*match)(struct gpio_chip *chip, |
1287 | void *data)) | 1292 | void *data)) |
1288 | { | 1293 | { |
1289 | struct gpio_chip *chip; | 1294 | struct gpio_chip *chip; |
1290 | unsigned long flags; | 1295 | unsigned long flags; |
1291 | 1296 | ||
1292 | spin_lock_irqsave(&gpio_lock, flags); | 1297 | spin_lock_irqsave(&gpio_lock, flags); |
1293 | list_for_each_entry(chip, &gpio_chips, list) | 1298 | list_for_each_entry(chip, &gpio_chips, list) |
1294 | if (match(chip, data)) | 1299 | if (match(chip, data)) |
1295 | break; | 1300 | break; |
1296 | 1301 | ||
1297 | /* No match? */ | 1302 | /* No match? */ |
1298 | if (&chip->list == &gpio_chips) | 1303 | if (&chip->list == &gpio_chips) |
1299 | chip = NULL; | 1304 | chip = NULL; |
1300 | spin_unlock_irqrestore(&gpio_lock, flags); | 1305 | spin_unlock_irqrestore(&gpio_lock, flags); |
1301 | 1306 | ||
1302 | return chip; | 1307 | return chip; |
1303 | } | 1308 | } |
1304 | EXPORT_SYMBOL_GPL(gpiochip_find); | 1309 | EXPORT_SYMBOL_GPL(gpiochip_find); |
1305 | 1310 | ||
1306 | #ifdef CONFIG_PINCTRL | 1311 | #ifdef CONFIG_PINCTRL |
1307 | 1312 | ||
1308 | /** | 1313 | /** |
1309 | * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping | 1314 | * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping |
1310 | * @chip: the gpiochip to add the range for | 1315 | * @chip: the gpiochip to add the range for |
1311 | * @pinctrl_name: the dev_name() of the pin controller to map to | 1316 | * @pinctrl_name: the dev_name() of the pin controller to map to |
1312 | * @gpio_offset: the start offset in the current gpio_chip number space | 1317 | * @gpio_offset: the start offset in the current gpio_chip number space |
1313 | * @pin_offset: the start offset in the pin controller number space | 1318 | * @pin_offset: the start offset in the pin controller number space |
1314 | * @npins: the number of pins from the offset of each pin space (GPIO and | 1319 | * @npins: the number of pins from the offset of each pin space (GPIO and |
1315 | * pin controller) to accumulate in this range | 1320 | * pin controller) to accumulate in this range |
1316 | */ | 1321 | */ |
1317 | int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name, | 1322 | int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name, |
1318 | unsigned int gpio_offset, unsigned int pin_offset, | 1323 | unsigned int gpio_offset, unsigned int pin_offset, |
1319 | unsigned int npins) | 1324 | unsigned int npins) |
1320 | { | 1325 | { |
1321 | struct gpio_pin_range *pin_range; | 1326 | struct gpio_pin_range *pin_range; |
1322 | int ret; | 1327 | int ret; |
1323 | 1328 | ||
1324 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); | 1329 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); |
1325 | if (!pin_range) { | 1330 | if (!pin_range) { |
1326 | pr_err("%s: GPIO chip: failed to allocate pin ranges\n", | 1331 | pr_err("%s: GPIO chip: failed to allocate pin ranges\n", |
1327 | chip->label); | 1332 | chip->label); |
1328 | return -ENOMEM; | 1333 | return -ENOMEM; |
1329 | } | 1334 | } |
1330 | 1335 | ||
1331 | /* Use local offset as range ID */ | 1336 | /* Use local offset as range ID */ |
1332 | pin_range->range.id = gpio_offset; | 1337 | pin_range->range.id = gpio_offset; |
1333 | pin_range->range.gc = chip; | 1338 | pin_range->range.gc = chip; |
1334 | pin_range->range.name = chip->label; | 1339 | pin_range->range.name = chip->label; |
1335 | pin_range->range.base = chip->base + gpio_offset; | 1340 | pin_range->range.base = chip->base + gpio_offset; |
1336 | pin_range->range.pin_base = pin_offset; | 1341 | pin_range->range.pin_base = pin_offset; |
1337 | pin_range->range.npins = npins; | 1342 | pin_range->range.npins = npins; |
1338 | pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name, | 1343 | pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name, |
1339 | &pin_range->range); | 1344 | &pin_range->range); |
1340 | if (IS_ERR(pin_range->pctldev)) { | 1345 | if (IS_ERR(pin_range->pctldev)) { |
1341 | ret = PTR_ERR(pin_range->pctldev); | 1346 | ret = PTR_ERR(pin_range->pctldev); |
1342 | pr_err("%s: GPIO chip: could not create pin range\n", | 1347 | pr_err("%s: GPIO chip: could not create pin range\n", |
1343 | chip->label); | 1348 | chip->label); |
1344 | kfree(pin_range); | 1349 | kfree(pin_range); |
1345 | return ret; | 1350 | return ret; |
1346 | } | 1351 | } |
1347 | pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n", | 1352 | pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n", |
1348 | chip->label, gpio_offset, gpio_offset + npins - 1, | 1353 | chip->label, gpio_offset, gpio_offset + npins - 1, |
1349 | pinctl_name, | 1354 | pinctl_name, |
1350 | pin_offset, pin_offset + npins - 1); | 1355 | pin_offset, pin_offset + npins - 1); |
1351 | 1356 | ||
1352 | list_add_tail(&pin_range->node, &chip->pin_ranges); | 1357 | list_add_tail(&pin_range->node, &chip->pin_ranges); |
1353 | 1358 | ||
1354 | return 0; | 1359 | return 0; |
1355 | } | 1360 | } |
1356 | EXPORT_SYMBOL_GPL(gpiochip_add_pin_range); | 1361 | EXPORT_SYMBOL_GPL(gpiochip_add_pin_range); |
1357 | 1362 | ||
1358 | /** | 1363 | /** |
1359 | * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings | 1364 | * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings |
1360 | * @chip: the chip to remove all the mappings for | 1365 | * @chip: the chip to remove all the mappings for |
1361 | */ | 1366 | */ |
1362 | void gpiochip_remove_pin_ranges(struct gpio_chip *chip) | 1367 | void gpiochip_remove_pin_ranges(struct gpio_chip *chip) |
1363 | { | 1368 | { |
1364 | struct gpio_pin_range *pin_range, *tmp; | 1369 | struct gpio_pin_range *pin_range, *tmp; |
1365 | 1370 | ||
1366 | list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) { | 1371 | list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) { |
1367 | list_del(&pin_range->node); | 1372 | list_del(&pin_range->node); |
1368 | pinctrl_remove_gpio_range(pin_range->pctldev, | 1373 | pinctrl_remove_gpio_range(pin_range->pctldev, |
1369 | &pin_range->range); | 1374 | &pin_range->range); |
1370 | kfree(pin_range); | 1375 | kfree(pin_range); |
1371 | } | 1376 | } |
1372 | } | 1377 | } |
1373 | EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges); | 1378 | EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges); |
1374 | 1379 | ||
1375 | #endif /* CONFIG_PINCTRL */ | 1380 | #endif /* CONFIG_PINCTRL */ |
1376 | 1381 | ||
1377 | /* These "optional" allocation calls help prevent drivers from stomping | 1382 | /* These "optional" allocation calls help prevent drivers from stomping |
1378 | * on each other, and help provide better diagnostics in debugfs. | 1383 | * on each other, and help provide better diagnostics in debugfs. |
1379 | * They're called even less than the "set direction" calls. | 1384 | * They're called even less than the "set direction" calls. |
1380 | */ | 1385 | */ |
1381 | static int gpiod_request(struct gpio_desc *desc, const char *label) | 1386 | static int gpiod_request(struct gpio_desc *desc, const char *label) |
1382 | { | 1387 | { |
1383 | struct gpio_chip *chip; | 1388 | struct gpio_chip *chip; |
1384 | int status = -EPROBE_DEFER; | 1389 | int status = -EPROBE_DEFER; |
1385 | unsigned long flags; | 1390 | unsigned long flags; |
1386 | 1391 | ||
1387 | spin_lock_irqsave(&gpio_lock, flags); | 1392 | spin_lock_irqsave(&gpio_lock, flags); |
1388 | 1393 | ||
1389 | if (!desc) { | 1394 | if (!desc) { |
1390 | status = -EINVAL; | 1395 | status = -EINVAL; |
1391 | goto done; | 1396 | goto done; |
1392 | } | 1397 | } |
1393 | chip = desc->chip; | 1398 | chip = desc->chip; |
1394 | if (chip == NULL) | 1399 | if (chip == NULL) |
1395 | goto done; | 1400 | goto done; |
1396 | 1401 | ||
1397 | if (!try_module_get(chip->owner)) | 1402 | if (!try_module_get(chip->owner)) |
1398 | goto done; | 1403 | goto done; |
1399 | 1404 | ||
1400 | /* NOTE: gpio_request() can be called in early boot, | 1405 | /* NOTE: gpio_request() can be called in early boot, |
1401 | * before IRQs are enabled, for non-sleeping (SOC) GPIOs. | 1406 | * before IRQs are enabled, for non-sleeping (SOC) GPIOs. |
1402 | */ | 1407 | */ |
1403 | 1408 | ||
1404 | if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { | 1409 | if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { |
1405 | desc_set_label(desc, label ? : "?"); | 1410 | desc_set_label(desc, label ? : "?"); |
1406 | status = 0; | 1411 | status = 0; |
1407 | } else { | 1412 | } else { |
1408 | status = -EBUSY; | 1413 | status = -EBUSY; |
1409 | module_put(chip->owner); | 1414 | module_put(chip->owner); |
1410 | goto done; | 1415 | goto done; |
1411 | } | 1416 | } |
1412 | 1417 | ||
1413 | if (chip->request) { | 1418 | if (chip->request) { |
1414 | /* chip->request may sleep */ | 1419 | /* chip->request may sleep */ |
1415 | spin_unlock_irqrestore(&gpio_lock, flags); | 1420 | spin_unlock_irqrestore(&gpio_lock, flags); |
1416 | status = chip->request(chip, gpio_chip_hwgpio(desc)); | 1421 | status = chip->request(chip, gpio_chip_hwgpio(desc)); |
1417 | spin_lock_irqsave(&gpio_lock, flags); | 1422 | spin_lock_irqsave(&gpio_lock, flags); |
1418 | 1423 | ||
1419 | if (status < 0) { | 1424 | if (status < 0) { |
1420 | desc_set_label(desc, NULL); | 1425 | desc_set_label(desc, NULL); |
1421 | module_put(chip->owner); | 1426 | module_put(chip->owner); |
1422 | clear_bit(FLAG_REQUESTED, &desc->flags); | 1427 | clear_bit(FLAG_REQUESTED, &desc->flags); |
1423 | goto done; | 1428 | goto done; |
1424 | } | 1429 | } |
1425 | } | 1430 | } |
1426 | if (chip->get_direction) { | 1431 | if (chip->get_direction) { |
1427 | /* chip->get_direction may sleep */ | 1432 | /* chip->get_direction may sleep */ |
1428 | spin_unlock_irqrestore(&gpio_lock, flags); | 1433 | spin_unlock_irqrestore(&gpio_lock, flags); |
1429 | gpiod_get_direction(desc); | 1434 | gpiod_get_direction(desc); |
1430 | spin_lock_irqsave(&gpio_lock, flags); | 1435 | spin_lock_irqsave(&gpio_lock, flags); |
1431 | } | 1436 | } |
1432 | done: | 1437 | done: |
1433 | if (status) | 1438 | if (status) |
1434 | pr_debug("_gpio_request: gpio-%d (%s) status %d\n", | 1439 | pr_debug("_gpio_request: gpio-%d (%s) status %d\n", |
1435 | desc ? desc_to_gpio(desc) : -1, | 1440 | desc ? desc_to_gpio(desc) : -1, |
1436 | label ? : "?", status); | 1441 | label ? : "?", status); |
1437 | spin_unlock_irqrestore(&gpio_lock, flags); | 1442 | spin_unlock_irqrestore(&gpio_lock, flags); |
1438 | return status; | 1443 | return status; |
1439 | } | 1444 | } |
1440 | 1445 | ||
1441 | int gpio_request(unsigned gpio, const char *label) | 1446 | int gpio_request(unsigned gpio, const char *label) |
1442 | { | 1447 | { |
1443 | return gpiod_request(gpio_to_desc(gpio), label); | 1448 | return gpiod_request(gpio_to_desc(gpio), label); |
1444 | } | 1449 | } |
1445 | EXPORT_SYMBOL_GPL(gpio_request); | 1450 | EXPORT_SYMBOL_GPL(gpio_request); |
1446 | 1451 | ||
1447 | static void gpiod_free(struct gpio_desc *desc) | 1452 | static void gpiod_free(struct gpio_desc *desc) |
1448 | { | 1453 | { |
1449 | unsigned long flags; | 1454 | unsigned long flags; |
1450 | struct gpio_chip *chip; | 1455 | struct gpio_chip *chip; |
1451 | 1456 | ||
1452 | might_sleep(); | 1457 | might_sleep(); |
1453 | 1458 | ||
1454 | if (!desc) { | 1459 | if (!desc) { |
1455 | WARN_ON(extra_checks); | 1460 | WARN_ON(extra_checks); |
1456 | return; | 1461 | return; |
1457 | } | 1462 | } |
1458 | 1463 | ||
1459 | gpiod_unexport(desc); | 1464 | gpiod_unexport(desc); |
1460 | 1465 | ||
1461 | spin_lock_irqsave(&gpio_lock, flags); | 1466 | spin_lock_irqsave(&gpio_lock, flags); |
1462 | 1467 | ||
1463 | chip = desc->chip; | 1468 | chip = desc->chip; |
1464 | if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) { | 1469 | if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) { |
1465 | if (chip->free) { | 1470 | if (chip->free) { |
1466 | spin_unlock_irqrestore(&gpio_lock, flags); | 1471 | spin_unlock_irqrestore(&gpio_lock, flags); |
1467 | might_sleep_if(chip->can_sleep); | 1472 | might_sleep_if(chip->can_sleep); |
1468 | chip->free(chip, gpio_chip_hwgpio(desc)); | 1473 | chip->free(chip, gpio_chip_hwgpio(desc)); |
1469 | spin_lock_irqsave(&gpio_lock, flags); | 1474 | spin_lock_irqsave(&gpio_lock, flags); |
1470 | } | 1475 | } |
1471 | desc_set_label(desc, NULL); | 1476 | desc_set_label(desc, NULL); |
1472 | module_put(desc->chip->owner); | 1477 | module_put(desc->chip->owner); |
1473 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); | 1478 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); |
1474 | clear_bit(FLAG_REQUESTED, &desc->flags); | 1479 | clear_bit(FLAG_REQUESTED, &desc->flags); |
1475 | clear_bit(FLAG_OPEN_DRAIN, &desc->flags); | 1480 | clear_bit(FLAG_OPEN_DRAIN, &desc->flags); |
1476 | clear_bit(FLAG_OPEN_SOURCE, &desc->flags); | 1481 | clear_bit(FLAG_OPEN_SOURCE, &desc->flags); |
1477 | } else | 1482 | } else |
1478 | WARN_ON(extra_checks); | 1483 | WARN_ON(extra_checks); |
1479 | 1484 | ||
1480 | spin_unlock_irqrestore(&gpio_lock, flags); | 1485 | spin_unlock_irqrestore(&gpio_lock, flags); |
1481 | } | 1486 | } |
1482 | 1487 | ||
1483 | void gpio_free(unsigned gpio) | 1488 | void gpio_free(unsigned gpio) |
1484 | { | 1489 | { |
1485 | gpiod_free(gpio_to_desc(gpio)); | 1490 | gpiod_free(gpio_to_desc(gpio)); |
1486 | } | 1491 | } |
1487 | EXPORT_SYMBOL_GPL(gpio_free); | 1492 | EXPORT_SYMBOL_GPL(gpio_free); |
1488 | 1493 | ||
1489 | /** | 1494 | /** |
1490 | * gpio_request_one - request a single GPIO with initial configuration | 1495 | * gpio_request_one - request a single GPIO with initial configuration |
1491 | * @gpio: the GPIO number | 1496 | * @gpio: the GPIO number |
1492 | * @flags: GPIO configuration as specified by GPIOF_* | 1497 | * @flags: GPIO configuration as specified by GPIOF_* |
1493 | * @label: a literal description string of this GPIO | 1498 | * @label: a literal description string of this GPIO |
1494 | */ | 1499 | */ |
1495 | int gpio_request_one(unsigned gpio, unsigned long flags, const char *label) | 1500 | int gpio_request_one(unsigned gpio, unsigned long flags, const char *label) |
1496 | { | 1501 | { |
1497 | struct gpio_desc *desc; | 1502 | struct gpio_desc *desc; |
1498 | int err; | 1503 | int err; |
1499 | 1504 | ||
1500 | desc = gpio_to_desc(gpio); | 1505 | desc = gpio_to_desc(gpio); |
1501 | 1506 | ||
1502 | err = gpiod_request(desc, label); | 1507 | err = gpiod_request(desc, label); |
1503 | if (err) | 1508 | if (err) |
1504 | return err; | 1509 | return err; |
1505 | 1510 | ||
1506 | if (flags & GPIOF_OPEN_DRAIN) | 1511 | if (flags & GPIOF_OPEN_DRAIN) |
1507 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); | 1512 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); |
1508 | 1513 | ||
1509 | if (flags & GPIOF_OPEN_SOURCE) | 1514 | if (flags & GPIOF_OPEN_SOURCE) |
1510 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); | 1515 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); |
1511 | 1516 | ||
1512 | if (flags & GPIOF_DIR_IN) | 1517 | if (flags & GPIOF_DIR_IN) |
1513 | err = gpiod_direction_input(desc); | 1518 | err = gpiod_direction_input(desc); |
1514 | else | 1519 | else |
1515 | err = gpiod_direction_output(desc, | 1520 | err = gpiod_direction_output(desc, |
1516 | (flags & GPIOF_INIT_HIGH) ? 1 : 0); | 1521 | (flags & GPIOF_INIT_HIGH) ? 1 : 0); |
1517 | 1522 | ||
1518 | if (err) | 1523 | if (err) |
1519 | goto free_gpio; | 1524 | goto free_gpio; |
1520 | 1525 | ||
1521 | if (flags & GPIOF_EXPORT) { | 1526 | if (flags & GPIOF_EXPORT) { |
1522 | err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE); | 1527 | err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE); |
1523 | if (err) | 1528 | if (err) |
1524 | goto free_gpio; | 1529 | goto free_gpio; |
1525 | } | 1530 | } |
1526 | 1531 | ||
1527 | return 0; | 1532 | return 0; |
1528 | 1533 | ||
1529 | free_gpio: | 1534 | free_gpio: |
1530 | gpiod_free(desc); | 1535 | gpiod_free(desc); |
1531 | return err; | 1536 | return err; |
1532 | } | 1537 | } |
1533 | EXPORT_SYMBOL_GPL(gpio_request_one); | 1538 | EXPORT_SYMBOL_GPL(gpio_request_one); |
1534 | 1539 | ||
1535 | /** | 1540 | /** |
1536 | * gpio_request_array - request multiple GPIOs in a single call | 1541 | * gpio_request_array - request multiple GPIOs in a single call |
1537 | * @array: array of the 'struct gpio' | 1542 | * @array: array of the 'struct gpio' |
1538 | * @num: how many GPIOs in the array | 1543 | * @num: how many GPIOs in the array |
1539 | */ | 1544 | */ |
1540 | int gpio_request_array(const struct gpio *array, size_t num) | 1545 | int gpio_request_array(const struct gpio *array, size_t num) |
1541 | { | 1546 | { |
1542 | int i, err; | 1547 | int i, err; |
1543 | 1548 | ||
1544 | for (i = 0; i < num; i++, array++) { | 1549 | for (i = 0; i < num; i++, array++) { |
1545 | err = gpio_request_one(array->gpio, array->flags, array->label); | 1550 | err = gpio_request_one(array->gpio, array->flags, array->label); |
1546 | if (err) | 1551 | if (err) |
1547 | goto err_free; | 1552 | goto err_free; |
1548 | } | 1553 | } |
1549 | return 0; | 1554 | return 0; |
1550 | 1555 | ||
1551 | err_free: | 1556 | err_free: |
1552 | while (i--) | 1557 | while (i--) |
1553 | gpio_free((--array)->gpio); | 1558 | gpio_free((--array)->gpio); |
1554 | return err; | 1559 | return err; |
1555 | } | 1560 | } |
1556 | EXPORT_SYMBOL_GPL(gpio_request_array); | 1561 | EXPORT_SYMBOL_GPL(gpio_request_array); |
1557 | 1562 | ||
1558 | /** | 1563 | /** |
1559 | * gpio_free_array - release multiple GPIOs in a single call | 1564 | * gpio_free_array - release multiple GPIOs in a single call |
1560 | * @array: array of the 'struct gpio' | 1565 | * @array: array of the 'struct gpio' |
1561 | * @num: how many GPIOs in the array | 1566 | * @num: how many GPIOs in the array |
1562 | */ | 1567 | */ |
1563 | void gpio_free_array(const struct gpio *array, size_t num) | 1568 | void gpio_free_array(const struct gpio *array, size_t num) |
1564 | { | 1569 | { |
1565 | while (num--) | 1570 | while (num--) |
1566 | gpio_free((array++)->gpio); | 1571 | gpio_free((array++)->gpio); |
1567 | } | 1572 | } |
1568 | EXPORT_SYMBOL_GPL(gpio_free_array); | 1573 | EXPORT_SYMBOL_GPL(gpio_free_array); |
1569 | 1574 | ||
1570 | /** | 1575 | /** |
1571 | * gpiochip_is_requested - return string iff signal was requested | 1576 | * gpiochip_is_requested - return string iff signal was requested |
1572 | * @chip: controller managing the signal | 1577 | * @chip: controller managing the signal |
1573 | * @offset: of signal within controller's 0..(ngpio - 1) range | 1578 | * @offset: of signal within controller's 0..(ngpio - 1) range |
1574 | * | 1579 | * |
1575 | * Returns NULL if the GPIO is not currently requested, else a string. | 1580 | * Returns NULL if the GPIO is not currently requested, else a string. |
1576 | * If debugfs support is enabled, the string returned is the label passed | 1581 | * If debugfs support is enabled, the string returned is the label passed |
1577 | * to gpio_request(); otherwise it is a meaningless constant. | 1582 | * to gpio_request(); otherwise it is a meaningless constant. |
1578 | * | 1583 | * |
1579 | * This function is for use by GPIO controller drivers. The label can | 1584 | * This function is for use by GPIO controller drivers. The label can |
1580 | * help with diagnostics, and knowing that the signal is used as a GPIO | 1585 | * help with diagnostics, and knowing that the signal is used as a GPIO |
1581 | * can help avoid accidentally multiplexing it to another controller. | 1586 | * can help avoid accidentally multiplexing it to another controller. |
1582 | */ | 1587 | */ |
1583 | const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset) | 1588 | const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset) |
1584 | { | 1589 | { |
1585 | unsigned gpio = chip->base + offset; | 1590 | struct gpio_desc *desc; |
1586 | struct gpio_desc *desc = &gpio_desc[gpio]; | ||
1587 | 1591 | ||
1588 | if (!gpio_is_valid(gpio) || desc->chip != chip) | 1592 | if (!GPIO_OFFSET_VALID(chip, offset)) |
1589 | return NULL; | 1593 | return NULL; |
1594 | |||
1595 | desc = &chip->desc[offset]; | ||
1596 | |||
1590 | if (test_bit(FLAG_REQUESTED, &desc->flags) == 0) | 1597 | if (test_bit(FLAG_REQUESTED, &desc->flags) == 0) |
1591 | return NULL; | 1598 | return NULL; |
1592 | #ifdef CONFIG_DEBUG_FS | 1599 | #ifdef CONFIG_DEBUG_FS |
1593 | return desc->label; | 1600 | return desc->label; |
1594 | #else | 1601 | #else |
1595 | return "?"; | 1602 | return "?"; |
1596 | #endif | 1603 | #endif |
1597 | } | 1604 | } |
1598 | EXPORT_SYMBOL_GPL(gpiochip_is_requested); | 1605 | EXPORT_SYMBOL_GPL(gpiochip_is_requested); |
1599 | 1606 | ||
1600 | 1607 | ||
1601 | /* Drivers MUST set GPIO direction before making get/set calls. In | 1608 | /* Drivers MUST set GPIO direction before making get/set calls. In |
1602 | * some cases this is done in early boot, before IRQs are enabled. | 1609 | * some cases this is done in early boot, before IRQs are enabled. |
1603 | * | 1610 | * |
1604 | * As a rule these aren't called more than once (except for drivers | 1611 | * As a rule these aren't called more than once (except for drivers |
1605 | * using the open-drain emulation idiom) so these are natural places | 1612 | * using the open-drain emulation idiom) so these are natural places |
1606 | * to accumulate extra debugging checks. Note that we can't (yet) | 1613 | * to accumulate extra debugging checks. Note that we can't (yet) |
1607 | * rely on gpio_request() having been called beforehand. | 1614 | * rely on gpio_request() having been called beforehand. |
1608 | */ | 1615 | */ |
1609 | 1616 | ||
1610 | static int gpiod_direction_input(struct gpio_desc *desc) | 1617 | static int gpiod_direction_input(struct gpio_desc *desc) |
1611 | { | 1618 | { |
1612 | unsigned long flags; | 1619 | unsigned long flags; |
1613 | struct gpio_chip *chip; | 1620 | struct gpio_chip *chip; |
1614 | int status = -EINVAL; | 1621 | int status = -EINVAL; |
1615 | int offset; | 1622 | int offset; |
1616 | 1623 | ||
1617 | spin_lock_irqsave(&gpio_lock, flags); | 1624 | spin_lock_irqsave(&gpio_lock, flags); |
1618 | 1625 | ||
1619 | if (!desc) | 1626 | if (!desc) |
1620 | goto fail; | 1627 | goto fail; |
1621 | chip = desc->chip; | 1628 | chip = desc->chip; |
1622 | if (!chip || !chip->get || !chip->direction_input) | 1629 | if (!chip || !chip->get || !chip->direction_input) |
1623 | goto fail; | 1630 | goto fail; |
1624 | status = gpio_ensure_requested(desc); | 1631 | status = gpio_ensure_requested(desc); |
1625 | if (status < 0) | 1632 | if (status < 0) |
1626 | goto fail; | 1633 | goto fail; |
1627 | 1634 | ||
1628 | /* now we know the gpio is valid and chip won't vanish */ | 1635 | /* now we know the gpio is valid and chip won't vanish */ |
1629 | 1636 | ||
1630 | spin_unlock_irqrestore(&gpio_lock, flags); | 1637 | spin_unlock_irqrestore(&gpio_lock, flags); |
1631 | 1638 | ||
1632 | might_sleep_if(chip->can_sleep); | 1639 | might_sleep_if(chip->can_sleep); |
1633 | 1640 | ||
1634 | offset = gpio_chip_hwgpio(desc); | 1641 | offset = gpio_chip_hwgpio(desc); |
1635 | if (status) { | 1642 | if (status) { |
1636 | status = chip->request(chip, offset); | 1643 | status = chip->request(chip, offset); |
1637 | if (status < 0) { | 1644 | if (status < 0) { |
1638 | pr_debug("GPIO-%d: chip request fail, %d\n", | 1645 | pr_debug("GPIO-%d: chip request fail, %d\n", |
1639 | desc_to_gpio(desc), status); | 1646 | desc_to_gpio(desc), status); |
1640 | /* and it's not available to anyone else ... | 1647 | /* and it's not available to anyone else ... |
1641 | * gpio_request() is the fully clean solution. | 1648 | * gpio_request() is the fully clean solution. |
1642 | */ | 1649 | */ |
1643 | goto lose; | 1650 | goto lose; |
1644 | } | 1651 | } |
1645 | } | 1652 | } |
1646 | 1653 | ||
1647 | status = chip->direction_input(chip, offset); | 1654 | status = chip->direction_input(chip, offset); |
1648 | if (status == 0) | 1655 | if (status == 0) |
1649 | clear_bit(FLAG_IS_OUT, &desc->flags); | 1656 | clear_bit(FLAG_IS_OUT, &desc->flags); |
1650 | 1657 | ||
1651 | trace_gpio_direction(desc_to_gpio(desc), 1, status); | 1658 | trace_gpio_direction(desc_to_gpio(desc), 1, status); |
1652 | lose: | 1659 | lose: |
1653 | return status; | 1660 | return status; |
1654 | fail: | 1661 | fail: |
1655 | spin_unlock_irqrestore(&gpio_lock, flags); | 1662 | spin_unlock_irqrestore(&gpio_lock, flags); |
1656 | if (status) { | 1663 | if (status) { |
1657 | int gpio = -1; | 1664 | int gpio = -1; |
1658 | if (desc) | 1665 | if (desc) |
1659 | gpio = desc_to_gpio(desc); | 1666 | gpio = desc_to_gpio(desc); |
1660 | pr_debug("%s: gpio-%d status %d\n", | 1667 | pr_debug("%s: gpio-%d status %d\n", |
1661 | __func__, gpio, status); | 1668 | __func__, gpio, status); |
1662 | } | 1669 | } |
1663 | return status; | 1670 | return status; |
1664 | } | 1671 | } |
1665 | 1672 | ||
1666 | int gpio_direction_input(unsigned gpio) | 1673 | int gpio_direction_input(unsigned gpio) |
1667 | { | 1674 | { |
1668 | return gpiod_direction_input(gpio_to_desc(gpio)); | 1675 | return gpiod_direction_input(gpio_to_desc(gpio)); |
1669 | } | 1676 | } |
1670 | EXPORT_SYMBOL_GPL(gpio_direction_input); | 1677 | EXPORT_SYMBOL_GPL(gpio_direction_input); |
1671 | 1678 | ||
1672 | static int gpiod_direction_output(struct gpio_desc *desc, int value) | 1679 | static int gpiod_direction_output(struct gpio_desc *desc, int value) |
1673 | { | 1680 | { |
1674 | unsigned long flags; | 1681 | unsigned long flags; |
1675 | struct gpio_chip *chip; | 1682 | struct gpio_chip *chip; |
1676 | int status = -EINVAL; | 1683 | int status = -EINVAL; |
1677 | int offset; | 1684 | int offset; |
1678 | 1685 | ||
1679 | /* Open drain pin should not be driven to 1 */ | 1686 | /* Open drain pin should not be driven to 1 */ |
1680 | if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags)) | 1687 | if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags)) |
1681 | return gpiod_direction_input(desc); | 1688 | return gpiod_direction_input(desc); |
1682 | 1689 | ||
1683 | /* Open source pin should not be driven to 0 */ | 1690 | /* Open source pin should not be driven to 0 */ |
1684 | if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags)) | 1691 | if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags)) |
1685 | return gpiod_direction_input(desc); | 1692 | return gpiod_direction_input(desc); |
1686 | 1693 | ||
1687 | spin_lock_irqsave(&gpio_lock, flags); | 1694 | spin_lock_irqsave(&gpio_lock, flags); |
1688 | 1695 | ||
1689 | if (!desc) | 1696 | if (!desc) |
1690 | goto fail; | 1697 | goto fail; |
1691 | chip = desc->chip; | 1698 | chip = desc->chip; |
1692 | if (!chip || !chip->set || !chip->direction_output) | 1699 | if (!chip || !chip->set || !chip->direction_output) |
1693 | goto fail; | 1700 | goto fail; |
1694 | status = gpio_ensure_requested(desc); | 1701 | status = gpio_ensure_requested(desc); |
1695 | if (status < 0) | 1702 | if (status < 0) |
1696 | goto fail; | 1703 | goto fail; |
1697 | 1704 | ||
1698 | /* now we know the gpio is valid and chip won't vanish */ | 1705 | /* now we know the gpio is valid and chip won't vanish */ |
1699 | 1706 | ||
1700 | spin_unlock_irqrestore(&gpio_lock, flags); | 1707 | spin_unlock_irqrestore(&gpio_lock, flags); |
1701 | 1708 | ||
1702 | might_sleep_if(chip->can_sleep); | 1709 | might_sleep_if(chip->can_sleep); |
1703 | 1710 | ||
1704 | offset = gpio_chip_hwgpio(desc); | 1711 | offset = gpio_chip_hwgpio(desc); |
1705 | if (status) { | 1712 | if (status) { |
1706 | status = chip->request(chip, offset); | 1713 | status = chip->request(chip, offset); |
1707 | if (status < 0) { | 1714 | if (status < 0) { |
1708 | pr_debug("GPIO-%d: chip request fail, %d\n", | 1715 | pr_debug("GPIO-%d: chip request fail, %d\n", |
1709 | desc_to_gpio(desc), status); | 1716 | desc_to_gpio(desc), status); |
1710 | /* and it's not available to anyone else ... | 1717 | /* and it's not available to anyone else ... |
1711 | * gpio_request() is the fully clean solution. | 1718 | * gpio_request() is the fully clean solution. |
1712 | */ | 1719 | */ |
1713 | goto lose; | 1720 | goto lose; |
1714 | } | 1721 | } |
1715 | } | 1722 | } |
1716 | 1723 | ||
1717 | status = chip->direction_output(chip, offset, value); | 1724 | status = chip->direction_output(chip, offset, value); |
1718 | if (status == 0) | 1725 | if (status == 0) |
1719 | set_bit(FLAG_IS_OUT, &desc->flags); | 1726 | set_bit(FLAG_IS_OUT, &desc->flags); |
1720 | trace_gpio_value(desc_to_gpio(desc), 0, value); | 1727 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
1721 | trace_gpio_direction(desc_to_gpio(desc), 0, status); | 1728 | trace_gpio_direction(desc_to_gpio(desc), 0, status); |
1722 | lose: | 1729 | lose: |
1723 | return status; | 1730 | return status; |
1724 | fail: | 1731 | fail: |
1725 | spin_unlock_irqrestore(&gpio_lock, flags); | 1732 | spin_unlock_irqrestore(&gpio_lock, flags); |
1726 | if (status) { | 1733 | if (status) { |
1727 | int gpio = -1; | 1734 | int gpio = -1; |
1728 | if (desc) | 1735 | if (desc) |
1729 | gpio = desc_to_gpio(desc); | 1736 | gpio = desc_to_gpio(desc); |
1730 | pr_debug("%s: gpio-%d status %d\n", | 1737 | pr_debug("%s: gpio-%d status %d\n", |
1731 | __func__, gpio, status); | 1738 | __func__, gpio, status); |
1732 | } | 1739 | } |
1733 | return status; | 1740 | return status; |
1734 | } | 1741 | } |
1735 | 1742 | ||
1736 | int gpio_direction_output(unsigned gpio, int value) | 1743 | int gpio_direction_output(unsigned gpio, int value) |
1737 | { | 1744 | { |
1738 | return gpiod_direction_output(gpio_to_desc(gpio), value); | 1745 | return gpiod_direction_output(gpio_to_desc(gpio), value); |
1739 | } | 1746 | } |
1740 | EXPORT_SYMBOL_GPL(gpio_direction_output); | 1747 | EXPORT_SYMBOL_GPL(gpio_direction_output); |
1741 | 1748 | ||
1742 | /** | 1749 | /** |
1743 | * gpio_set_debounce - sets @debounce time for a @gpio | 1750 | * gpio_set_debounce - sets @debounce time for a @gpio |
1744 | * @gpio: the gpio to set debounce time | 1751 | * @gpio: the gpio to set debounce time |
1745 | * @debounce: debounce time is microseconds | 1752 | * @debounce: debounce time is microseconds |
1746 | */ | 1753 | */ |
1747 | static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) | 1754 | static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) |
1748 | { | 1755 | { |
1749 | unsigned long flags; | 1756 | unsigned long flags; |
1750 | struct gpio_chip *chip; | 1757 | struct gpio_chip *chip; |
1751 | int status = -EINVAL; | 1758 | int status = -EINVAL; |
1752 | int offset; | 1759 | int offset; |
1753 | 1760 | ||
1754 | spin_lock_irqsave(&gpio_lock, flags); | 1761 | spin_lock_irqsave(&gpio_lock, flags); |
1755 | 1762 | ||
1756 | if (!desc) | 1763 | if (!desc) |
1757 | goto fail; | 1764 | goto fail; |
1758 | chip = desc->chip; | 1765 | chip = desc->chip; |
1759 | if (!chip || !chip->set || !chip->set_debounce) | 1766 | if (!chip || !chip->set || !chip->set_debounce) |
1760 | goto fail; | 1767 | goto fail; |
1761 | 1768 | ||
1762 | status = gpio_ensure_requested(desc); | 1769 | status = gpio_ensure_requested(desc); |
1763 | if (status < 0) | 1770 | if (status < 0) |
1764 | goto fail; | 1771 | goto fail; |
1765 | 1772 | ||
1766 | /* now we know the gpio is valid and chip won't vanish */ | 1773 | /* now we know the gpio is valid and chip won't vanish */ |
1767 | 1774 | ||
1768 | spin_unlock_irqrestore(&gpio_lock, flags); | 1775 | spin_unlock_irqrestore(&gpio_lock, flags); |
1769 | 1776 | ||
1770 | might_sleep_if(chip->can_sleep); | 1777 | might_sleep_if(chip->can_sleep); |
1771 | 1778 | ||
1772 | offset = gpio_chip_hwgpio(desc); | 1779 | offset = gpio_chip_hwgpio(desc); |
1773 | return chip->set_debounce(chip, offset, debounce); | 1780 | return chip->set_debounce(chip, offset, debounce); |
1774 | 1781 | ||
1775 | fail: | 1782 | fail: |
1776 | spin_unlock_irqrestore(&gpio_lock, flags); | 1783 | spin_unlock_irqrestore(&gpio_lock, flags); |
1777 | if (status) { | 1784 | if (status) { |
1778 | int gpio = -1; | 1785 | int gpio = -1; |
1779 | if (desc) | 1786 | if (desc) |
1780 | gpio = desc_to_gpio(desc); | 1787 | gpio = desc_to_gpio(desc); |
1781 | pr_debug("%s: gpio-%d status %d\n", | 1788 | pr_debug("%s: gpio-%d status %d\n", |
1782 | __func__, gpio, status); | 1789 | __func__, gpio, status); |
1783 | } | 1790 | } |
1784 | 1791 | ||
1785 | return status; | 1792 | return status; |
1786 | } | 1793 | } |
1787 | 1794 | ||
1788 | int gpio_set_debounce(unsigned gpio, unsigned debounce) | 1795 | int gpio_set_debounce(unsigned gpio, unsigned debounce) |
1789 | { | 1796 | { |
1790 | return gpiod_set_debounce(gpio_to_desc(gpio), debounce); | 1797 | return gpiod_set_debounce(gpio_to_desc(gpio), debounce); |
1791 | } | 1798 | } |
1792 | EXPORT_SYMBOL_GPL(gpio_set_debounce); | 1799 | EXPORT_SYMBOL_GPL(gpio_set_debounce); |
1793 | 1800 | ||
1794 | /* I/O calls are only valid after configuration completed; the relevant | 1801 | /* I/O calls are only valid after configuration completed; the relevant |
1795 | * "is this a valid GPIO" error checks should already have been done. | 1802 | * "is this a valid GPIO" error checks should already have been done. |
1796 | * | 1803 | * |
1797 | * "Get" operations are often inlinable as reading a pin value register, | 1804 | * "Get" operations are often inlinable as reading a pin value register, |
1798 | * and masking the relevant bit in that register. | 1805 | * and masking the relevant bit in that register. |
1799 | * | 1806 | * |
1800 | * When "set" operations are inlinable, they involve writing that mask to | 1807 | * When "set" operations are inlinable, they involve writing that mask to |
1801 | * one register to set a low value, or a different register to set it high. | 1808 | * one register to set a low value, or a different register to set it high. |
1802 | * Otherwise locking is needed, so there may be little value to inlining. | 1809 | * Otherwise locking is needed, so there may be little value to inlining. |
1803 | * | 1810 | * |
1804 | *------------------------------------------------------------------------ | 1811 | *------------------------------------------------------------------------ |
1805 | * | 1812 | * |
1806 | * IMPORTANT!!! The hot paths -- get/set value -- assume that callers | 1813 | * IMPORTANT!!! The hot paths -- get/set value -- assume that callers |
1807 | * have requested the GPIO. That can include implicit requesting by | 1814 | * have requested the GPIO. That can include implicit requesting by |
1808 | * a direction setting call. Marking a gpio as requested locks its chip | 1815 | * a direction setting call. Marking a gpio as requested locks its chip |
1809 | * in memory, guaranteeing that these table lookups need no more locking | 1816 | * in memory, guaranteeing that these table lookups need no more locking |
1810 | * and that gpiochip_remove() will fail. | 1817 | * and that gpiochip_remove() will fail. |
1811 | * | 1818 | * |
1812 | * REVISIT when debugging, consider adding some instrumentation to ensure | 1819 | * REVISIT when debugging, consider adding some instrumentation to ensure |
1813 | * that the GPIO was actually requested. | 1820 | * that the GPIO was actually requested. |
1814 | */ | 1821 | */ |
1815 | 1822 | ||
1816 | /** | 1823 | /** |
1817 | * __gpio_get_value() - return a gpio's value | 1824 | * __gpio_get_value() - return a gpio's value |
1818 | * @gpio: gpio whose value will be returned | 1825 | * @gpio: gpio whose value will be returned |
1819 | * Context: any | 1826 | * Context: any |
1820 | * | 1827 | * |
1821 | * This is used directly or indirectly to implement gpio_get_value(). | 1828 | * This is used directly or indirectly to implement gpio_get_value(). |
1822 | * It returns the zero or nonzero value provided by the associated | 1829 | * It returns the zero or nonzero value provided by the associated |
1823 | * gpio_chip.get() method; or zero if no such method is provided. | 1830 | * gpio_chip.get() method; or zero if no such method is provided. |
1824 | */ | 1831 | */ |
1825 | static int gpiod_get_value(struct gpio_desc *desc) | 1832 | static int gpiod_get_value(struct gpio_desc *desc) |
1826 | { | 1833 | { |
1827 | struct gpio_chip *chip; | 1834 | struct gpio_chip *chip; |
1828 | int value; | 1835 | int value; |
1829 | int offset; | 1836 | int offset; |
1830 | 1837 | ||
1831 | chip = desc->chip; | 1838 | chip = desc->chip; |
1832 | offset = gpio_chip_hwgpio(desc); | 1839 | offset = gpio_chip_hwgpio(desc); |
1833 | /* Should be using gpio_get_value_cansleep() */ | 1840 | /* Should be using gpio_get_value_cansleep() */ |
1834 | WARN_ON(chip->can_sleep); | 1841 | WARN_ON(chip->can_sleep); |
1835 | value = chip->get ? chip->get(chip, offset) : 0; | 1842 | value = chip->get ? chip->get(chip, offset) : 0; |
1836 | trace_gpio_value(desc_to_gpio(desc), 1, value); | 1843 | trace_gpio_value(desc_to_gpio(desc), 1, value); |
1837 | return value; | 1844 | return value; |
1838 | } | 1845 | } |
1839 | 1846 | ||
1840 | int __gpio_get_value(unsigned gpio) | 1847 | int __gpio_get_value(unsigned gpio) |
1841 | { | 1848 | { |
1842 | return gpiod_get_value(gpio_to_desc(gpio)); | 1849 | return gpiod_get_value(gpio_to_desc(gpio)); |
1843 | } | 1850 | } |
1844 | EXPORT_SYMBOL_GPL(__gpio_get_value); | 1851 | EXPORT_SYMBOL_GPL(__gpio_get_value); |
1845 | 1852 | ||
1846 | /* | 1853 | /* |
1847 | * _gpio_set_open_drain_value() - Set the open drain gpio's value. | 1854 | * _gpio_set_open_drain_value() - Set the open drain gpio's value. |
1848 | * @gpio: Gpio whose state need to be set. | 1855 | * @gpio: Gpio whose state need to be set. |
1849 | * @chip: Gpio chip. | 1856 | * @chip: Gpio chip. |
1850 | * @value: Non-zero for setting it HIGH otherise it will set to LOW. | 1857 | * @value: Non-zero for setting it HIGH otherise it will set to LOW. |
1851 | */ | 1858 | */ |
1852 | static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value) | 1859 | static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value) |
1853 | { | 1860 | { |
1854 | int err = 0; | 1861 | int err = 0; |
1855 | struct gpio_chip *chip = desc->chip; | 1862 | struct gpio_chip *chip = desc->chip; |
1856 | int offset = gpio_chip_hwgpio(desc); | 1863 | int offset = gpio_chip_hwgpio(desc); |
1857 | 1864 | ||
1858 | if (value) { | 1865 | if (value) { |
1859 | err = chip->direction_input(chip, offset); | 1866 | err = chip->direction_input(chip, offset); |
1860 | if (!err) | 1867 | if (!err) |
1861 | clear_bit(FLAG_IS_OUT, &desc->flags); | 1868 | clear_bit(FLAG_IS_OUT, &desc->flags); |
1862 | } else { | 1869 | } else { |
1863 | err = chip->direction_output(chip, offset, 0); | 1870 | err = chip->direction_output(chip, offset, 0); |
1864 | if (!err) | 1871 | if (!err) |
1865 | set_bit(FLAG_IS_OUT, &desc->flags); | 1872 | set_bit(FLAG_IS_OUT, &desc->flags); |
1866 | } | 1873 | } |
1867 | trace_gpio_direction(desc_to_gpio(desc), value, err); | 1874 | trace_gpio_direction(desc_to_gpio(desc), value, err); |
1868 | if (err < 0) | 1875 | if (err < 0) |
1869 | pr_err("%s: Error in set_value for open drain gpio%d err %d\n", | 1876 | pr_err("%s: Error in set_value for open drain gpio%d err %d\n", |
1870 | __func__, desc_to_gpio(desc), err); | 1877 | __func__, desc_to_gpio(desc), err); |
1871 | } | 1878 | } |
1872 | 1879 | ||
1873 | /* | 1880 | /* |
1874 | * _gpio_set_open_source() - Set the open source gpio's value. | 1881 | * _gpio_set_open_source() - Set the open source gpio's value. |
1875 | * @gpio: Gpio whose state need to be set. | 1882 | * @gpio: Gpio whose state need to be set. |
1876 | * @chip: Gpio chip. | 1883 | * @chip: Gpio chip. |
1877 | * @value: Non-zero for setting it HIGH otherise it will set to LOW. | 1884 | * @value: Non-zero for setting it HIGH otherise it will set to LOW. |
1878 | */ | 1885 | */ |
1879 | static void _gpio_set_open_source_value(struct gpio_desc *desc, int value) | 1886 | static void _gpio_set_open_source_value(struct gpio_desc *desc, int value) |
1880 | { | 1887 | { |
1881 | int err = 0; | 1888 | int err = 0; |
1882 | struct gpio_chip *chip = desc->chip; | 1889 | struct gpio_chip *chip = desc->chip; |
1883 | int offset = gpio_chip_hwgpio(desc); | 1890 | int offset = gpio_chip_hwgpio(desc); |
1884 | 1891 | ||
1885 | if (value) { | 1892 | if (value) { |
1886 | err = chip->direction_output(chip, offset, 1); | 1893 | err = chip->direction_output(chip, offset, 1); |
1887 | if (!err) | 1894 | if (!err) |
1888 | set_bit(FLAG_IS_OUT, &desc->flags); | 1895 | set_bit(FLAG_IS_OUT, &desc->flags); |
1889 | } else { | 1896 | } else { |
1890 | err = chip->direction_input(chip, offset); | 1897 | err = chip->direction_input(chip, offset); |
1891 | if (!err) | 1898 | if (!err) |
1892 | clear_bit(FLAG_IS_OUT, &desc->flags); | 1899 | clear_bit(FLAG_IS_OUT, &desc->flags); |
1893 | } | 1900 | } |
1894 | trace_gpio_direction(desc_to_gpio(desc), !value, err); | 1901 | trace_gpio_direction(desc_to_gpio(desc), !value, err); |
1895 | if (err < 0) | 1902 | if (err < 0) |
1896 | pr_err("%s: Error in set_value for open source gpio%d err %d\n", | 1903 | pr_err("%s: Error in set_value for open source gpio%d err %d\n", |
1897 | __func__, desc_to_gpio(desc), err); | 1904 | __func__, desc_to_gpio(desc), err); |
1898 | } | 1905 | } |
1899 | 1906 | ||
1900 | /** | 1907 | /** |
1901 | * __gpio_set_value() - assign a gpio's value | 1908 | * __gpio_set_value() - assign a gpio's value |
1902 | * @gpio: gpio whose value will be assigned | 1909 | * @gpio: gpio whose value will be assigned |
1903 | * @value: value to assign | 1910 | * @value: value to assign |
1904 | * Context: any | 1911 | * Context: any |
1905 | * | 1912 | * |
1906 | * This is used directly or indirectly to implement gpio_set_value(). | 1913 | * This is used directly or indirectly to implement gpio_set_value(). |
1907 | * It invokes the associated gpio_chip.set() method. | 1914 | * It invokes the associated gpio_chip.set() method. |
1908 | */ | 1915 | */ |
1909 | static void gpiod_set_value(struct gpio_desc *desc, int value) | 1916 | static void gpiod_set_value(struct gpio_desc *desc, int value) |
1910 | { | 1917 | { |
1911 | struct gpio_chip *chip; | 1918 | struct gpio_chip *chip; |
1912 | 1919 | ||
1913 | chip = desc->chip; | 1920 | chip = desc->chip; |
1914 | /* Should be using gpio_set_value_cansleep() */ | 1921 | /* Should be using gpio_set_value_cansleep() */ |
1915 | WARN_ON(chip->can_sleep); | 1922 | WARN_ON(chip->can_sleep); |
1916 | trace_gpio_value(desc_to_gpio(desc), 0, value); | 1923 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
1917 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) | 1924 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) |
1918 | _gpio_set_open_drain_value(desc, value); | 1925 | _gpio_set_open_drain_value(desc, value); |
1919 | else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) | 1926 | else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) |
1920 | _gpio_set_open_source_value(desc, value); | 1927 | _gpio_set_open_source_value(desc, value); |
1921 | else | 1928 | else |
1922 | chip->set(chip, gpio_chip_hwgpio(desc), value); | 1929 | chip->set(chip, gpio_chip_hwgpio(desc), value); |
1923 | } | 1930 | } |
1924 | 1931 | ||
1925 | void __gpio_set_value(unsigned gpio, int value) | 1932 | void __gpio_set_value(unsigned gpio, int value) |
1926 | { | 1933 | { |
1927 | return gpiod_set_value(gpio_to_desc(gpio), value); | 1934 | return gpiod_set_value(gpio_to_desc(gpio), value); |
1928 | } | 1935 | } |
1929 | EXPORT_SYMBOL_GPL(__gpio_set_value); | 1936 | EXPORT_SYMBOL_GPL(__gpio_set_value); |
1930 | 1937 | ||
1931 | /** | 1938 | /** |
1932 | * __gpio_cansleep() - report whether gpio value access will sleep | 1939 | * __gpio_cansleep() - report whether gpio value access will sleep |
1933 | * @gpio: gpio in question | 1940 | * @gpio: gpio in question |
1934 | * Context: any | 1941 | * Context: any |
1935 | * | 1942 | * |
1936 | * This is used directly or indirectly to implement gpio_cansleep(). It | 1943 | * This is used directly or indirectly to implement gpio_cansleep(). It |
1937 | * returns nonzero if access reading or writing the GPIO value can sleep. | 1944 | * returns nonzero if access reading or writing the GPIO value can sleep. |
1938 | */ | 1945 | */ |
1939 | static int gpiod_cansleep(struct gpio_desc *desc) | 1946 | static int gpiod_cansleep(struct gpio_desc *desc) |
1940 | { | 1947 | { |
1941 | /* only call this on GPIOs that are valid! */ | 1948 | /* only call this on GPIOs that are valid! */ |
1942 | return desc->chip->can_sleep; | 1949 | return desc->chip->can_sleep; |
1943 | } | 1950 | } |
1944 | 1951 | ||
1945 | int __gpio_cansleep(unsigned gpio) | 1952 | int __gpio_cansleep(unsigned gpio) |
1946 | { | 1953 | { |
1947 | return gpiod_cansleep(gpio_to_desc(gpio)); | 1954 | return gpiod_cansleep(gpio_to_desc(gpio)); |
1948 | } | 1955 | } |
1949 | EXPORT_SYMBOL_GPL(__gpio_cansleep); | 1956 | EXPORT_SYMBOL_GPL(__gpio_cansleep); |
1950 | 1957 | ||
1951 | /** | 1958 | /** |
1952 | * __gpio_to_irq() - return the IRQ corresponding to a GPIO | 1959 | * __gpio_to_irq() - return the IRQ corresponding to a GPIO |
1953 | * @gpio: gpio whose IRQ will be returned (already requested) | 1960 | * @gpio: gpio whose IRQ will be returned (already requested) |
1954 | * Context: any | 1961 | * Context: any |
1955 | * | 1962 | * |
1956 | * This is used directly or indirectly to implement gpio_to_irq(). | 1963 | * This is used directly or indirectly to implement gpio_to_irq(). |
1957 | * It returns the number of the IRQ signaled by this (input) GPIO, | 1964 | * It returns the number of the IRQ signaled by this (input) GPIO, |
1958 | * or a negative errno. | 1965 | * or a negative errno. |
1959 | */ | 1966 | */ |
1960 | static int gpiod_to_irq(struct gpio_desc *desc) | 1967 | static int gpiod_to_irq(struct gpio_desc *desc) |
1961 | { | 1968 | { |
1962 | struct gpio_chip *chip; | 1969 | struct gpio_chip *chip; |
1963 | int offset; | 1970 | int offset; |
1964 | 1971 | ||
1965 | chip = desc->chip; | 1972 | chip = desc->chip; |
1966 | offset = gpio_chip_hwgpio(desc); | 1973 | offset = gpio_chip_hwgpio(desc); |
1967 | return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO; | 1974 | return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO; |
1968 | } | 1975 | } |
1969 | 1976 | ||
1970 | int __gpio_to_irq(unsigned gpio) | 1977 | int __gpio_to_irq(unsigned gpio) |
1971 | { | 1978 | { |
1972 | return gpiod_to_irq(gpio_to_desc(gpio)); | 1979 | return gpiod_to_irq(gpio_to_desc(gpio)); |
1973 | } | 1980 | } |
1974 | EXPORT_SYMBOL_GPL(__gpio_to_irq); | 1981 | EXPORT_SYMBOL_GPL(__gpio_to_irq); |
1975 | 1982 | ||
1976 | 1983 | ||
1977 | /* There's no value in making it easy to inline GPIO calls that may sleep. | 1984 | /* There's no value in making it easy to inline GPIO calls that may sleep. |
1978 | * Common examples include ones connected to I2C or SPI chips. | 1985 | * Common examples include ones connected to I2C or SPI chips. |
1979 | */ | 1986 | */ |
1980 | 1987 | ||
1981 | static int gpiod_get_value_cansleep(struct gpio_desc *desc) | 1988 | static int gpiod_get_value_cansleep(struct gpio_desc *desc) |
1982 | { | 1989 | { |
1983 | struct gpio_chip *chip; | 1990 | struct gpio_chip *chip; |
1984 | int value; | 1991 | int value; |
1985 | int offset; | 1992 | int offset; |
1986 | 1993 | ||
1987 | might_sleep_if(extra_checks); | 1994 | might_sleep_if(extra_checks); |
1988 | chip = desc->chip; | 1995 | chip = desc->chip; |
1989 | offset = gpio_chip_hwgpio(desc); | 1996 | offset = gpio_chip_hwgpio(desc); |
1990 | value = chip->get ? chip->get(chip, offset) : 0; | 1997 | value = chip->get ? chip->get(chip, offset) : 0; |
1991 | trace_gpio_value(desc_to_gpio(desc), 1, value); | 1998 | trace_gpio_value(desc_to_gpio(desc), 1, value); |
1992 | return value; | 1999 | return value; |
1993 | } | 2000 | } |
1994 | 2001 | ||
1995 | int gpio_get_value_cansleep(unsigned gpio) | 2002 | int gpio_get_value_cansleep(unsigned gpio) |
1996 | { | 2003 | { |
1997 | return gpiod_get_value_cansleep(gpio_to_desc(gpio)); | 2004 | return gpiod_get_value_cansleep(gpio_to_desc(gpio)); |
1998 | } | 2005 | } |
1999 | EXPORT_SYMBOL_GPL(gpio_get_value_cansleep); | 2006 | EXPORT_SYMBOL_GPL(gpio_get_value_cansleep); |
2000 | 2007 | ||
2001 | static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) | 2008 | static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) |
2002 | { | 2009 | { |
2003 | struct gpio_chip *chip; | 2010 | struct gpio_chip *chip; |
2004 | 2011 | ||
2005 | might_sleep_if(extra_checks); | 2012 | might_sleep_if(extra_checks); |
2006 | chip = desc->chip; | 2013 | chip = desc->chip; |
2007 | trace_gpio_value(desc_to_gpio(desc), 0, value); | 2014 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
2008 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) | 2015 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) |
2009 | _gpio_set_open_drain_value(desc, value); | 2016 | _gpio_set_open_drain_value(desc, value); |
2010 | else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) | 2017 | else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) |
2011 | _gpio_set_open_source_value(desc, value); | 2018 | _gpio_set_open_source_value(desc, value); |
2012 | else | 2019 | else |
2013 | chip->set(chip, gpio_chip_hwgpio(desc), value); | 2020 | chip->set(chip, gpio_chip_hwgpio(desc), value); |
2014 | } | 2021 | } |
2015 | 2022 | ||
2016 | void gpio_set_value_cansleep(unsigned gpio, int value) | 2023 | void gpio_set_value_cansleep(unsigned gpio, int value) |
2017 | { | 2024 | { |
2018 | return gpiod_set_value_cansleep(gpio_to_desc(gpio), value); | 2025 | return gpiod_set_value_cansleep(gpio_to_desc(gpio), value); |
2019 | } | 2026 | } |
2020 | EXPORT_SYMBOL_GPL(gpio_set_value_cansleep); | 2027 | EXPORT_SYMBOL_GPL(gpio_set_value_cansleep); |
2021 | 2028 | ||
2022 | #ifdef CONFIG_DEBUG_FS | 2029 | #ifdef CONFIG_DEBUG_FS |
2023 | 2030 | ||
2024 | static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip) | 2031 | static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
2025 | { | 2032 | { |
2026 | unsigned i; | 2033 | unsigned i; |
2027 | unsigned gpio = chip->base; | 2034 | unsigned gpio = chip->base; |
2028 | struct gpio_desc *gdesc = &gpio_desc[gpio]; | 2035 | struct gpio_desc *gdesc = &chip->desc[0]; |
2029 | int is_out; | 2036 | int is_out; |
2030 | 2037 | ||
2031 | for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) { | 2038 | for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) { |
2032 | if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) | 2039 | if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) |
2033 | continue; | 2040 | continue; |
2034 | 2041 | ||
2035 | gpiod_get_direction(gdesc); | 2042 | gpiod_get_direction(gdesc); |
2036 | is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); | 2043 | is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); |
2037 | seq_printf(s, " gpio-%-3d (%-20.20s) %s %s", | 2044 | seq_printf(s, " gpio-%-3d (%-20.20s) %s %s", |
2038 | gpio, gdesc->label, | 2045 | gpio, gdesc->label, |
2039 | is_out ? "out" : "in ", | 2046 | is_out ? "out" : "in ", |
2040 | chip->get | 2047 | chip->get |
2041 | ? (chip->get(chip, i) ? "hi" : "lo") | 2048 | ? (chip->get(chip, i) ? "hi" : "lo") |
2042 | : "? "); | 2049 | : "? "); |
2043 | seq_printf(s, "\n"); | 2050 | seq_printf(s, "\n"); |
2044 | } | 2051 | } |
2045 | } | 2052 | } |
2046 | 2053 | ||
2047 | static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos) | 2054 | static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos) |
2048 | { | 2055 | { |
2049 | struct gpio_chip *chip = NULL; | 2056 | struct gpio_chip *chip = NULL; |
2050 | loff_t index = *pos; | 2057 | loff_t index = *pos; |
2051 | 2058 | ||
2052 | /* REVISIT this isn't locked against gpio_chip removal ... */ | 2059 | /* REVISIT this isn't locked against gpio_chip removal ... */ |
2053 | 2060 | ||
2054 | s->private = ""; | 2061 | s->private = ""; |
2055 | 2062 | ||
2056 | list_for_each_entry(chip, &gpio_chips, list) | 2063 | list_for_each_entry(chip, &gpio_chips, list) |
2057 | if (index-- == 0) | 2064 | if (index-- == 0) |
2058 | return chip; | 2065 | return chip; |
2059 | 2066 | ||
2060 | return NULL; | 2067 | return NULL; |
2061 | } | 2068 | } |
2062 | 2069 | ||
2063 | static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos) | 2070 | static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos) |
2064 | { | 2071 | { |
2065 | struct gpio_chip *chip = v; | 2072 | struct gpio_chip *chip = v; |
2066 | void *ret = NULL; | 2073 | void *ret = NULL; |
2067 | 2074 | ||
2068 | if (list_is_last(&chip->list, &gpio_chips)) | 2075 | if (list_is_last(&chip->list, &gpio_chips)) |
2069 | ret = NULL; | 2076 | ret = NULL; |
2070 | else | 2077 | else |
2071 | ret = list_entry(chip->list.next, struct gpio_chip, list); | 2078 | ret = list_entry(chip->list.next, struct gpio_chip, list); |
2072 | 2079 | ||
2073 | s->private = "\n"; | 2080 | s->private = "\n"; |
2074 | ++*pos; | 2081 | ++*pos; |
2075 | 2082 | ||
2076 | return ret; | 2083 | return ret; |
2077 | } | 2084 | } |
2078 | 2085 | ||
2079 | static void gpiolib_seq_stop(struct seq_file *s, void *v) | 2086 | static void gpiolib_seq_stop(struct seq_file *s, void *v) |
2080 | { | 2087 | { |
2081 | } | 2088 | } |
2082 | 2089 | ||
2083 | static int gpiolib_seq_show(struct seq_file *s, void *v) | 2090 | static int gpiolib_seq_show(struct seq_file *s, void *v) |
2084 | { | 2091 | { |
2085 | struct gpio_chip *chip = v; | 2092 | struct gpio_chip *chip = v; |
2086 | struct device *dev; | 2093 | struct device *dev; |
2087 | 2094 | ||
2088 | seq_printf(s, "%sGPIOs %d-%d", (char *)s->private, | 2095 | seq_printf(s, "%sGPIOs %d-%d", (char *)s->private, |
2089 | chip->base, chip->base + chip->ngpio - 1); | 2096 | chip->base, chip->base + chip->ngpio - 1); |
2090 | dev = chip->dev; | 2097 | dev = chip->dev; |
2091 | if (dev) | 2098 | if (dev) |
2092 | seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus", | 2099 | seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus", |
2093 | dev_name(dev)); | 2100 | dev_name(dev)); |
2094 | if (chip->label) | 2101 | if (chip->label) |
2095 | seq_printf(s, ", %s", chip->label); | 2102 | seq_printf(s, ", %s", chip->label); |
2096 | if (chip->can_sleep) | 2103 | if (chip->can_sleep) |
2097 | seq_printf(s, ", can sleep"); | 2104 | seq_printf(s, ", can sleep"); |
2098 | seq_printf(s, ":\n"); | 2105 | seq_printf(s, ":\n"); |
2099 | 2106 | ||
2100 | if (chip->dbg_show) | 2107 | if (chip->dbg_show) |
2101 | chip->dbg_show(s, chip); | 2108 | chip->dbg_show(s, chip); |
2102 | else | 2109 | else |
2103 | gpiolib_dbg_show(s, chip); | 2110 | gpiolib_dbg_show(s, chip); |
2104 | 2111 | ||
2105 | return 0; | 2112 | return 0; |
2106 | } | 2113 | } |
2107 | 2114 | ||
2108 | static const struct seq_operations gpiolib_seq_ops = { | 2115 | static const struct seq_operations gpiolib_seq_ops = { |
2109 | .start = gpiolib_seq_start, | 2116 | .start = gpiolib_seq_start, |
2110 | .next = gpiolib_seq_next, | 2117 | .next = gpiolib_seq_next, |
2111 | .stop = gpiolib_seq_stop, | 2118 | .stop = gpiolib_seq_stop, |
2112 | .show = gpiolib_seq_show, | 2119 | .show = gpiolib_seq_show, |
2113 | }; | 2120 | }; |
2114 | 2121 | ||
2115 | static int gpiolib_open(struct inode *inode, struct file *file) | 2122 | static int gpiolib_open(struct inode *inode, struct file *file) |
2116 | { | 2123 | { |
2117 | return seq_open(file, &gpiolib_seq_ops); | 2124 | return seq_open(file, &gpiolib_seq_ops); |
2118 | } | 2125 | } |
2119 | 2126 | ||
2120 | static const struct file_operations gpiolib_operations = { | 2127 | static const struct file_operations gpiolib_operations = { |
2121 | .owner = THIS_MODULE, | 2128 | .owner = THIS_MODULE, |
2122 | .open = gpiolib_open, | 2129 | .open = gpiolib_open, |
2123 | .read = seq_read, | 2130 | .read = seq_read, |
2124 | .llseek = seq_lseek, | 2131 | .llseek = seq_lseek, |
2125 | .release = seq_release, | 2132 | .release = seq_release, |
2126 | }; | 2133 | }; |
2127 | 2134 | ||
2128 | static int __init gpiolib_debugfs_init(void) | 2135 | static int __init gpiolib_debugfs_init(void) |
2129 | { | 2136 | { |
2130 | /* /sys/kernel/debug/gpio */ | 2137 | /* /sys/kernel/debug/gpio */ |
2131 | (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO, | 2138 | (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO, |
2132 | NULL, NULL, &gpiolib_operations); | 2139 | NULL, NULL, &gpiolib_operations); |
2133 | return 0; | 2140 | return 0; |
2134 | } | 2141 | } |
2135 | subsys_initcall(gpiolib_debugfs_init); | 2142 | subsys_initcall(gpiolib_debugfs_init); |
2136 | 2143 |
include/asm-generic/gpio.h
1 | #ifndef _ASM_GENERIC_GPIO_H | 1 | #ifndef _ASM_GENERIC_GPIO_H |
2 | #define _ASM_GENERIC_GPIO_H | 2 | #define _ASM_GENERIC_GPIO_H |
3 | 3 | ||
4 | #include <linux/kernel.h> | 4 | #include <linux/kernel.h> |
5 | #include <linux/types.h> | 5 | #include <linux/types.h> |
6 | #include <linux/errno.h> | 6 | #include <linux/errno.h> |
7 | #include <linux/of.h> | 7 | #include <linux/of.h> |
8 | #include <linux/pinctrl/pinctrl.h> | 8 | #include <linux/pinctrl/pinctrl.h> |
9 | 9 | ||
10 | #ifdef CONFIG_GPIOLIB | 10 | #ifdef CONFIG_GPIOLIB |
11 | 11 | ||
12 | #include <linux/compiler.h> | 12 | #include <linux/compiler.h> |
13 | 13 | ||
14 | /* Platforms may implement their GPIO interface with library code, | 14 | /* Platforms may implement their GPIO interface with library code, |
15 | * at a small performance cost for non-inlined operations and some | 15 | * at a small performance cost for non-inlined operations and some |
16 | * extra memory (for code and for per-GPIO table entries). | 16 | * extra memory (for code and for per-GPIO table entries). |
17 | * | 17 | * |
18 | * While the GPIO programming interface defines valid GPIO numbers | 18 | * While the GPIO programming interface defines valid GPIO numbers |
19 | * to be in the range 0..MAX_INT, this library restricts them to the | 19 | * to be in the range 0..MAX_INT, this library restricts them to the |
20 | * smaller range 0..ARCH_NR_GPIOS-1. | 20 | * smaller range 0..ARCH_NR_GPIOS-1. |
21 | * | 21 | * |
22 | * ARCH_NR_GPIOS is somewhat arbitrary; it usually reflects the sum of | 22 | * ARCH_NR_GPIOS is somewhat arbitrary; it usually reflects the sum of |
23 | * builtin/SoC GPIOs plus a number of GPIOs on expanders; the latter is | 23 | * builtin/SoC GPIOs plus a number of GPIOs on expanders; the latter is |
24 | * actually an estimate of a board-specific value. | 24 | * actually an estimate of a board-specific value. |
25 | */ | 25 | */ |
26 | 26 | ||
27 | #ifndef ARCH_NR_GPIOS | 27 | #ifndef ARCH_NR_GPIOS |
28 | #define ARCH_NR_GPIOS 256 | 28 | #define ARCH_NR_GPIOS 256 |
29 | #endif | 29 | #endif |
30 | 30 | ||
31 | /* | 31 | /* |
32 | * "valid" GPIO numbers are nonnegative and may be passed to | 32 | * "valid" GPIO numbers are nonnegative and may be passed to |
33 | * setup routines like gpio_request(). only some valid numbers | 33 | * setup routines like gpio_request(). only some valid numbers |
34 | * can successfully be requested and used. | 34 | * can successfully be requested and used. |
35 | * | 35 | * |
36 | * Invalid GPIO numbers are useful for indicating no-such-GPIO in | 36 | * Invalid GPIO numbers are useful for indicating no-such-GPIO in |
37 | * platform data and other tables. | 37 | * platform data and other tables. |
38 | */ | 38 | */ |
39 | 39 | ||
40 | static inline bool gpio_is_valid(int number) | 40 | static inline bool gpio_is_valid(int number) |
41 | { | 41 | { |
42 | return number >= 0 && number < ARCH_NR_GPIOS; | 42 | return number >= 0 && number < ARCH_NR_GPIOS; |
43 | } | 43 | } |
44 | 44 | ||
45 | struct device; | 45 | struct device; |
46 | struct gpio; | 46 | struct gpio; |
47 | struct seq_file; | 47 | struct seq_file; |
48 | struct module; | 48 | struct module; |
49 | struct device_node; | 49 | struct device_node; |
50 | struct gpio_desc; | ||
50 | 51 | ||
51 | /** | 52 | /** |
52 | * struct gpio_chip - abstract a GPIO controller | 53 | * struct gpio_chip - abstract a GPIO controller |
53 | * @label: for diagnostics | 54 | * @label: for diagnostics |
54 | * @dev: optional device providing the GPIOs | 55 | * @dev: optional device providing the GPIOs |
55 | * @owner: helps prevent removal of modules exporting active GPIOs | 56 | * @owner: helps prevent removal of modules exporting active GPIOs |
56 | * @list: links gpio_chips together for traversal | 57 | * @list: links gpio_chips together for traversal |
57 | * @request: optional hook for chip-specific activation, such as | 58 | * @request: optional hook for chip-specific activation, such as |
58 | * enabling module power and clock; may sleep | 59 | * enabling module power and clock; may sleep |
59 | * @free: optional hook for chip-specific deactivation, such as | 60 | * @free: optional hook for chip-specific deactivation, such as |
60 | * disabling module power and clock; may sleep | 61 | * disabling module power and clock; may sleep |
61 | * @get_direction: returns direction for signal "offset", 0=out, 1=in, | 62 | * @get_direction: returns direction for signal "offset", 0=out, 1=in, |
62 | * (same as GPIOF_DIR_XXX), or negative error | 63 | * (same as GPIOF_DIR_XXX), or negative error |
63 | * @direction_input: configures signal "offset" as input, or returns error | 64 | * @direction_input: configures signal "offset" as input, or returns error |
64 | * @get: returns value for signal "offset"; for output signals this | 65 | * @get: returns value for signal "offset"; for output signals this |
65 | * returns either the value actually sensed, or zero | 66 | * returns either the value actually sensed, or zero |
66 | * @direction_output: configures signal "offset" as output, or returns error | 67 | * @direction_output: configures signal "offset" as output, or returns error |
67 | * @set_debounce: optional hook for setting debounce time for specified gpio in | 68 | * @set_debounce: optional hook for setting debounce time for specified gpio in |
68 | * interrupt triggered gpio chips | 69 | * interrupt triggered gpio chips |
69 | * @set: assigns output value for signal "offset" | 70 | * @set: assigns output value for signal "offset" |
70 | * @to_irq: optional hook supporting non-static gpio_to_irq() mappings; | 71 | * @to_irq: optional hook supporting non-static gpio_to_irq() mappings; |
71 | * implementation may not sleep | 72 | * implementation may not sleep |
72 | * @dbg_show: optional routine to show contents in debugfs; default code | 73 | * @dbg_show: optional routine to show contents in debugfs; default code |
73 | * will be used when this is omitted, but custom code can show extra | 74 | * will be used when this is omitted, but custom code can show extra |
74 | * state (such as pullup/pulldown configuration). | 75 | * state (such as pullup/pulldown configuration). |
75 | * @base: identifies the first GPIO number handled by this chip; or, if | 76 | * @base: identifies the first GPIO number handled by this chip; or, if |
76 | * negative during registration, requests dynamic ID allocation. | 77 | * negative during registration, requests dynamic ID allocation. |
77 | * @ngpio: the number of GPIOs handled by this controller; the last GPIO | 78 | * @ngpio: the number of GPIOs handled by this controller; the last GPIO |
78 | * handled is (base + ngpio - 1). | 79 | * handled is (base + ngpio - 1). |
80 | * @desc: array of ngpio descriptors. Private. | ||
79 | * @can_sleep: flag must be set iff get()/set() methods sleep, as they | 81 | * @can_sleep: flag must be set iff get()/set() methods sleep, as they |
80 | * must while accessing GPIO expander chips over I2C or SPI | 82 | * must while accessing GPIO expander chips over I2C or SPI |
81 | * @names: if set, must be an array of strings to use as alternative | 83 | * @names: if set, must be an array of strings to use as alternative |
82 | * names for the GPIOs in this chip. Any entry in the array | 84 | * names for the GPIOs in this chip. Any entry in the array |
83 | * may be NULL if there is no alias for the GPIO, however the | 85 | * may be NULL if there is no alias for the GPIO, however the |
84 | * array must be @ngpio entries long. A name can include a single printk | 86 | * array must be @ngpio entries long. A name can include a single printk |
85 | * format specifier for an unsigned int. It is substituted by the actual | 87 | * format specifier for an unsigned int. It is substituted by the actual |
86 | * number of the gpio. | 88 | * number of the gpio. |
87 | * | 89 | * |
88 | * A gpio_chip can help platforms abstract various sources of GPIOs so | 90 | * A gpio_chip can help platforms abstract various sources of GPIOs so |
89 | * they can all be accessed through a common programing interface. | 91 | * they can all be accessed through a common programing interface. |
90 | * Example sources would be SOC controllers, FPGAs, multifunction | 92 | * Example sources would be SOC controllers, FPGAs, multifunction |
91 | * chips, dedicated GPIO expanders, and so on. | 93 | * chips, dedicated GPIO expanders, and so on. |
92 | * | 94 | * |
93 | * Each chip controls a number of signals, identified in method calls | 95 | * Each chip controls a number of signals, identified in method calls |
94 | * by "offset" values in the range 0..(@ngpio - 1). When those signals | 96 | * by "offset" values in the range 0..(@ngpio - 1). When those signals |
95 | * are referenced through calls like gpio_get_value(gpio), the offset | 97 | * are referenced through calls like gpio_get_value(gpio), the offset |
96 | * is calculated by subtracting @base from the gpio number. | 98 | * is calculated by subtracting @base from the gpio number. |
97 | */ | 99 | */ |
98 | struct gpio_chip { | 100 | struct gpio_chip { |
99 | const char *label; | 101 | const char *label; |
100 | struct device *dev; | 102 | struct device *dev; |
101 | struct module *owner; | 103 | struct module *owner; |
102 | struct list_head list; | 104 | struct list_head list; |
103 | 105 | ||
104 | int (*request)(struct gpio_chip *chip, | 106 | int (*request)(struct gpio_chip *chip, |
105 | unsigned offset); | 107 | unsigned offset); |
106 | void (*free)(struct gpio_chip *chip, | 108 | void (*free)(struct gpio_chip *chip, |
107 | unsigned offset); | 109 | unsigned offset); |
108 | int (*get_direction)(struct gpio_chip *chip, | 110 | int (*get_direction)(struct gpio_chip *chip, |
109 | unsigned offset); | 111 | unsigned offset); |
110 | int (*direction_input)(struct gpio_chip *chip, | 112 | int (*direction_input)(struct gpio_chip *chip, |
111 | unsigned offset); | 113 | unsigned offset); |
112 | int (*get)(struct gpio_chip *chip, | 114 | int (*get)(struct gpio_chip *chip, |
113 | unsigned offset); | 115 | unsigned offset); |
114 | int (*direction_output)(struct gpio_chip *chip, | 116 | int (*direction_output)(struct gpio_chip *chip, |
115 | unsigned offset, int value); | 117 | unsigned offset, int value); |
116 | int (*set_debounce)(struct gpio_chip *chip, | 118 | int (*set_debounce)(struct gpio_chip *chip, |
117 | unsigned offset, unsigned debounce); | 119 | unsigned offset, unsigned debounce); |
118 | 120 | ||
119 | void (*set)(struct gpio_chip *chip, | 121 | void (*set)(struct gpio_chip *chip, |
120 | unsigned offset, int value); | 122 | unsigned offset, int value); |
121 | 123 | ||
122 | int (*to_irq)(struct gpio_chip *chip, | 124 | int (*to_irq)(struct gpio_chip *chip, |
123 | unsigned offset); | 125 | unsigned offset); |
124 | 126 | ||
125 | void (*dbg_show)(struct seq_file *s, | 127 | void (*dbg_show)(struct seq_file *s, |
126 | struct gpio_chip *chip); | 128 | struct gpio_chip *chip); |
127 | int base; | 129 | int base; |
128 | u16 ngpio; | 130 | u16 ngpio; |
131 | struct gpio_desc *desc; | ||
129 | const char *const *names; | 132 | const char *const *names; |
130 | unsigned can_sleep:1; | 133 | unsigned can_sleep:1; |
131 | unsigned exported:1; | 134 | unsigned exported:1; |
132 | 135 | ||
133 | #if defined(CONFIG_OF_GPIO) | 136 | #if defined(CONFIG_OF_GPIO) |
134 | /* | 137 | /* |
135 | * If CONFIG_OF is enabled, then all GPIO controllers described in the | 138 | * If CONFIG_OF is enabled, then all GPIO controllers described in the |
136 | * device tree automatically may have an OF translation | 139 | * device tree automatically may have an OF translation |
137 | */ | 140 | */ |
138 | struct device_node *of_node; | 141 | struct device_node *of_node; |
139 | int of_gpio_n_cells; | 142 | int of_gpio_n_cells; |
140 | int (*of_xlate)(struct gpio_chip *gc, | 143 | int (*of_xlate)(struct gpio_chip *gc, |
141 | const struct of_phandle_args *gpiospec, u32 *flags); | 144 | const struct of_phandle_args *gpiospec, u32 *flags); |
142 | #endif | 145 | #endif |
143 | #ifdef CONFIG_PINCTRL | 146 | #ifdef CONFIG_PINCTRL |
144 | /* | 147 | /* |
145 | * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally | 148 | * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally |
146 | * describe the actual pin range which they serve in an SoC. This | 149 | * describe the actual pin range which they serve in an SoC. This |
147 | * information would be used by pinctrl subsystem to configure | 150 | * information would be used by pinctrl subsystem to configure |
148 | * corresponding pins for gpio usage. | 151 | * corresponding pins for gpio usage. |
149 | */ | 152 | */ |
150 | struct list_head pin_ranges; | 153 | struct list_head pin_ranges; |
151 | #endif | 154 | #endif |
152 | }; | 155 | }; |
153 | 156 | ||
154 | extern const char *gpiochip_is_requested(struct gpio_chip *chip, | 157 | extern const char *gpiochip_is_requested(struct gpio_chip *chip, |
155 | unsigned offset); | 158 | unsigned offset); |
156 | extern struct gpio_chip *gpio_to_chip(unsigned gpio); | 159 | extern struct gpio_chip *gpio_to_chip(unsigned gpio); |
157 | 160 | ||
158 | /* add/remove chips */ | 161 | /* add/remove chips */ |
159 | extern int gpiochip_add(struct gpio_chip *chip); | 162 | extern int gpiochip_add(struct gpio_chip *chip); |
160 | extern int __must_check gpiochip_remove(struct gpio_chip *chip); | 163 | extern int __must_check gpiochip_remove(struct gpio_chip *chip); |
161 | extern struct gpio_chip *gpiochip_find(void *data, | 164 | extern struct gpio_chip *gpiochip_find(void *data, |
162 | int (*match)(struct gpio_chip *chip, | 165 | int (*match)(struct gpio_chip *chip, |
163 | void *data)); | 166 | void *data)); |
164 | 167 | ||
165 | 168 | ||
166 | /* Always use the library code for GPIO management calls, | 169 | /* Always use the library code for GPIO management calls, |
167 | * or when sleeping may be involved. | 170 | * or when sleeping may be involved. |
168 | */ | 171 | */ |
169 | extern int gpio_request(unsigned gpio, const char *label); | 172 | extern int gpio_request(unsigned gpio, const char *label); |
170 | extern void gpio_free(unsigned gpio); | 173 | extern void gpio_free(unsigned gpio); |
171 | 174 | ||
172 | extern int gpio_direction_input(unsigned gpio); | 175 | extern int gpio_direction_input(unsigned gpio); |
173 | extern int gpio_direction_output(unsigned gpio, int value); | 176 | extern int gpio_direction_output(unsigned gpio, int value); |
174 | 177 | ||
175 | extern int gpio_set_debounce(unsigned gpio, unsigned debounce); | 178 | extern int gpio_set_debounce(unsigned gpio, unsigned debounce); |
176 | 179 | ||
177 | extern int gpio_get_value_cansleep(unsigned gpio); | 180 | extern int gpio_get_value_cansleep(unsigned gpio); |
178 | extern void gpio_set_value_cansleep(unsigned gpio, int value); | 181 | extern void gpio_set_value_cansleep(unsigned gpio, int value); |
179 | 182 | ||
180 | 183 | ||
181 | /* A platform's <asm/gpio.h> code may want to inline the I/O calls when | 184 | /* A platform's <asm/gpio.h> code may want to inline the I/O calls when |
182 | * the GPIO is constant and refers to some always-present controller, | 185 | * the GPIO is constant and refers to some always-present controller, |
183 | * giving direct access to chip registers and tight bitbanging loops. | 186 | * giving direct access to chip registers and tight bitbanging loops. |
184 | */ | 187 | */ |
185 | extern int __gpio_get_value(unsigned gpio); | 188 | extern int __gpio_get_value(unsigned gpio); |
186 | extern void __gpio_set_value(unsigned gpio, int value); | 189 | extern void __gpio_set_value(unsigned gpio, int value); |
187 | 190 | ||
188 | extern int __gpio_cansleep(unsigned gpio); | 191 | extern int __gpio_cansleep(unsigned gpio); |
189 | 192 | ||
190 | extern int __gpio_to_irq(unsigned gpio); | 193 | extern int __gpio_to_irq(unsigned gpio); |
191 | 194 | ||
192 | extern int gpio_request_one(unsigned gpio, unsigned long flags, const char *label); | 195 | extern int gpio_request_one(unsigned gpio, unsigned long flags, const char *label); |
193 | extern int gpio_request_array(const struct gpio *array, size_t num); | 196 | extern int gpio_request_array(const struct gpio *array, size_t num); |
194 | extern void gpio_free_array(const struct gpio *array, size_t num); | 197 | extern void gpio_free_array(const struct gpio *array, size_t num); |
195 | 198 | ||
196 | #ifdef CONFIG_GPIO_SYSFS | 199 | #ifdef CONFIG_GPIO_SYSFS |
197 | 200 | ||
198 | /* | 201 | /* |
199 | * A sysfs interface can be exported by individual drivers if they want, | 202 | * A sysfs interface can be exported by individual drivers if they want, |
200 | * but more typically is configured entirely from userspace. | 203 | * but more typically is configured entirely from userspace. |
201 | */ | 204 | */ |
202 | extern int gpio_export(unsigned gpio, bool direction_may_change); | 205 | extern int gpio_export(unsigned gpio, bool direction_may_change); |
203 | extern int gpio_export_link(struct device *dev, const char *name, | 206 | extern int gpio_export_link(struct device *dev, const char *name, |
204 | unsigned gpio); | 207 | unsigned gpio); |
205 | extern int gpio_sysfs_set_active_low(unsigned gpio, int value); | 208 | extern int gpio_sysfs_set_active_low(unsigned gpio, int value); |
206 | extern void gpio_unexport(unsigned gpio); | 209 | extern void gpio_unexport(unsigned gpio); |
207 | 210 | ||
208 | #endif /* CONFIG_GPIO_SYSFS */ | 211 | #endif /* CONFIG_GPIO_SYSFS */ |
209 | 212 | ||
210 | #ifdef CONFIG_PINCTRL | 213 | #ifdef CONFIG_PINCTRL |
211 | 214 | ||
212 | /** | 215 | /** |
213 | * struct gpio_pin_range - pin range controlled by a gpio chip | 216 | * struct gpio_pin_range - pin range controlled by a gpio chip |
214 | * @head: list for maintaining set of pin ranges, used internally | 217 | * @head: list for maintaining set of pin ranges, used internally |
215 | * @pctldev: pinctrl device which handles corresponding pins | 218 | * @pctldev: pinctrl device which handles corresponding pins |
216 | * @range: actual range of pins controlled by a gpio controller | 219 | * @range: actual range of pins controlled by a gpio controller |
217 | */ | 220 | */ |
218 | 221 | ||
219 | struct gpio_pin_range { | 222 | struct gpio_pin_range { |
220 | struct list_head node; | 223 | struct list_head node; |
221 | struct pinctrl_dev *pctldev; | 224 | struct pinctrl_dev *pctldev; |
222 | struct pinctrl_gpio_range range; | 225 | struct pinctrl_gpio_range range; |
223 | }; | 226 | }; |
224 | 227 | ||
225 | int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name, | 228 | int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name, |
226 | unsigned int gpio_offset, unsigned int pin_offset, | 229 | unsigned int gpio_offset, unsigned int pin_offset, |
227 | unsigned int npins); | 230 | unsigned int npins); |
228 | void gpiochip_remove_pin_ranges(struct gpio_chip *chip); | 231 | void gpiochip_remove_pin_ranges(struct gpio_chip *chip); |
229 | 232 | ||
230 | #else | 233 | #else |
231 | 234 | ||
232 | static inline int | 235 | static inline int |
233 | gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name, | 236 | gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name, |
234 | unsigned int gpio_offset, unsigned int pin_offset, | 237 | unsigned int gpio_offset, unsigned int pin_offset, |
235 | unsigned int npins) | 238 | unsigned int npins) |
236 | { | 239 | { |
237 | return 0; | 240 | return 0; |
238 | } | 241 | } |
239 | 242 | ||
240 | static inline void | 243 | static inline void |
241 | gpiochip_remove_pin_ranges(struct gpio_chip *chip) | 244 | gpiochip_remove_pin_ranges(struct gpio_chip *chip) |
242 | { | 245 | { |
243 | } | 246 | } |
244 | 247 | ||
245 | #endif /* CONFIG_PINCTRL */ | 248 | #endif /* CONFIG_PINCTRL */ |
246 | 249 | ||
247 | #else /* !CONFIG_GPIOLIB */ | 250 | #else /* !CONFIG_GPIOLIB */ |
248 | 251 | ||
249 | static inline bool gpio_is_valid(int number) | 252 | static inline bool gpio_is_valid(int number) |
250 | { | 253 | { |
251 | /* only non-negative numbers are valid */ | 254 | /* only non-negative numbers are valid */ |
252 | return number >= 0; | 255 | return number >= 0; |
253 | } | 256 | } |
254 | 257 | ||
255 | /* platforms that don't directly support access to GPIOs through I2C, SPI, | 258 | /* platforms that don't directly support access to GPIOs through I2C, SPI, |
256 | * or other blocking infrastructure can use these wrappers. | 259 | * or other blocking infrastructure can use these wrappers. |
257 | */ | 260 | */ |
258 | 261 | ||
259 | static inline int gpio_cansleep(unsigned gpio) | 262 | static inline int gpio_cansleep(unsigned gpio) |
260 | { | 263 | { |
261 | return 0; | 264 | return 0; |
262 | } | 265 | } |
263 | 266 | ||
264 | static inline int gpio_get_value_cansleep(unsigned gpio) | 267 | static inline int gpio_get_value_cansleep(unsigned gpio) |
265 | { | 268 | { |
266 | might_sleep(); | 269 | might_sleep(); |
267 | return __gpio_get_value(gpio); | 270 | return __gpio_get_value(gpio); |
268 | } | 271 | } |
269 | 272 | ||
270 | static inline void gpio_set_value_cansleep(unsigned gpio, int value) | 273 | static inline void gpio_set_value_cansleep(unsigned gpio, int value) |
271 | { | 274 | { |
272 | might_sleep(); | 275 | might_sleep(); |
273 | __gpio_set_value(gpio, value); | 276 | __gpio_set_value(gpio, value); |
274 | } | 277 | } |
275 | 278 | ||
276 | #endif /* !CONFIG_GPIOLIB */ | 279 | #endif /* !CONFIG_GPIOLIB */ |
277 | 280 | ||
278 | #ifndef CONFIG_GPIO_SYSFS | 281 | #ifndef CONFIG_GPIO_SYSFS |
279 | 282 | ||
280 | struct device; | 283 | struct device; |
281 | 284 | ||
282 | /* sysfs support is only available with gpiolib, where it's optional */ | 285 | /* sysfs support is only available with gpiolib, where it's optional */ |
283 | 286 | ||
284 | static inline int gpio_export(unsigned gpio, bool direction_may_change) | 287 | static inline int gpio_export(unsigned gpio, bool direction_may_change) |
285 | { | 288 | { |
286 | return -ENOSYS; | 289 | return -ENOSYS; |
287 | } | 290 | } |
288 | 291 | ||
289 | static inline int gpio_export_link(struct device *dev, const char *name, | 292 | static inline int gpio_export_link(struct device *dev, const char *name, |
290 | unsigned gpio) | 293 | unsigned gpio) |
291 | { | 294 | { |
292 | return -ENOSYS; | 295 | return -ENOSYS; |
293 | } | 296 | } |
294 | 297 | ||
295 | static inline int gpio_sysfs_set_active_low(unsigned gpio, int value) | 298 | static inline int gpio_sysfs_set_active_low(unsigned gpio, int value) |
296 | { | 299 | { |
297 | return -ENOSYS; | 300 | return -ENOSYS; |
298 | } | 301 | } |
299 | 302 | ||
300 | static inline void gpio_unexport(unsigned gpio) | 303 | static inline void gpio_unexport(unsigned gpio) |
301 | { | 304 | { |
302 | } | 305 | } |
303 | #endif /* CONFIG_GPIO_SYSFS */ | 306 | #endif /* CONFIG_GPIO_SYSFS */ |
304 | 307 | ||
305 | #endif /* _ASM_GENERIC_GPIO_H */ | 308 | #endif /* _ASM_GENERIC_GPIO_H */ |
306 | 309 |