Commit | Line | Data |
---|---|---|
d2876d08 DB |
1 | #include <linux/kernel.h> |
2 | #include <linux/module.h> | |
ff77c352 | 3 | #include <linux/interrupt.h> |
d2876d08 DB |
4 | #include <linux/irq.h> |
5 | #include <linux/spinlock.h> | |
1a989d0f | 6 | #include <linux/list.h> |
d8f388d8 DB |
7 | #include <linux/device.h> |
8 | #include <linux/err.h> | |
9 | #include <linux/debugfs.h> | |
10 | #include <linux/seq_file.h> | |
11 | #include <linux/gpio.h> | |
391c970c | 12 | #include <linux/of_gpio.h> |
ff77c352 | 13 | #include <linux/idr.h> |
5a0e3ad6 | 14 | #include <linux/slab.h> |
7b199811 | 15 | #include <linux/acpi.h> |
53e7cac3 | 16 | #include <linux/gpio/driver.h> |
0a6d3158 | 17 | #include <linux/gpio/machine.h> |
c771c2f4 | 18 | #include <linux/pinctrl/consumer.h> |
d2876d08 | 19 | |
664e3e5a MW |
20 | #include "gpiolib.h" |
21 | ||
3f397c21 UKK |
22 | #define CREATE_TRACE_POINTS |
23 | #include <trace/events/gpio.h> | |
d2876d08 | 24 | |
79a9becd | 25 | /* Implementation infrastructure for GPIO interfaces. |
d2876d08 | 26 | * |
79a9becd AC |
27 | * The GPIO programming interface allows for inlining speed-critical |
28 | * get/set operations for common cases, so that access to SOC-integrated | |
29 | * GPIOs can sometimes cost only an instruction or two per bit. | |
d2876d08 DB |
30 | */ |
31 | ||
32 | ||
33 | /* When debugging, extend minimal trust to callers and platform code. | |
34 | * Also emit diagnostic messages that may help initial bringup, when | |
35 | * board setup or driver bugs are most common. | |
36 | * | |
37 | * Otherwise, minimize overhead in what may be bitbanging codepaths. | |
38 | */ | |
39 | #ifdef DEBUG | |
40 | #define extra_checks 1 | |
41 | #else | |
42 | #define extra_checks 0 | |
43 | #endif | |
44 | ||
45 | /* gpio_lock prevents conflicts during gpio_desc[] table updates. | |
46 | * While any GPIO is requested, its gpio_chip is not removable; | |
47 | * each GPIO's "requested" flag serves as a lock and refcount. | |
48 | */ | |
0eb4c6c2 | 49 | DEFINE_SPINLOCK(gpio_lock); |
d2876d08 | 50 | |
bae48da2 AC |
51 | static DEFINE_MUTEX(gpio_lookup_lock); |
52 | static LIST_HEAD(gpio_lookup_list); | |
0eb4c6c2 | 53 | LIST_HEAD(gpio_chips); |
1a2a99c6 | 54 | |
6d86750c JH |
55 | |
56 | static void gpiochip_free_hogs(struct gpio_chip *chip); | |
57 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip); | |
58 | ||
59 | ||
d2876d08 DB |
60 | static inline void desc_set_label(struct gpio_desc *d, const char *label) |
61 | { | |
d2876d08 | 62 | d->label = label; |
d2876d08 DB |
63 | } |
64 | ||
372e722e AC |
65 | /** |
66 | * Convert a GPIO number to its descriptor | |
67 | */ | |
79a9becd | 68 | struct gpio_desc *gpio_to_desc(unsigned gpio) |
372e722e | 69 | { |
14e85c0e AC |
70 | struct gpio_chip *chip; |
71 | unsigned long flags; | |
72 | ||
73 | spin_lock_irqsave(&gpio_lock, flags); | |
74 | ||
75 | list_for_each_entry(chip, &gpio_chips, list) { | |
76 | if (chip->base <= gpio && chip->base + chip->ngpio > gpio) { | |
77 | spin_unlock_irqrestore(&gpio_lock, flags); | |
78 | return &chip->desc[gpio - chip->base]; | |
79 | } | |
80 | } | |
81 | ||
82 | spin_unlock_irqrestore(&gpio_lock, flags); | |
83 | ||
0e9a5edf AC |
84 | if (!gpio_is_valid(gpio)) |
85 | WARN(1, "invalid GPIO %d\n", gpio); | |
86 | ||
14e85c0e | 87 | return NULL; |
372e722e | 88 | } |
79a9becd | 89 | EXPORT_SYMBOL_GPL(gpio_to_desc); |
372e722e | 90 | |
d468bf9e | 91 | /** |
bb1e88cc | 92 | * Get the GPIO descriptor corresponding to the given hw number for this chip. |
d468bf9e | 93 | */ |
bb1e88cc AC |
94 | struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, |
95 | u16 hwnum) | |
d468bf9e | 96 | { |
bb1e88cc | 97 | if (hwnum >= chip->ngpio) |
b7d0a28a | 98 | return ERR_PTR(-EINVAL); |
d468bf9e | 99 | |
bb1e88cc | 100 | return &chip->desc[hwnum]; |
d468bf9e | 101 | } |
372e722e AC |
102 | |
103 | /** | |
104 | * Convert a GPIO descriptor to the integer namespace. | |
105 | * This should disappear in the future but is needed since we still | |
106 | * use GPIO numbers for error messages and sysfs nodes | |
107 | */ | |
79a9becd | 108 | int desc_to_gpio(const struct gpio_desc *desc) |
372e722e | 109 | { |
14e85c0e | 110 | return desc->chip->base + (desc - &desc->chip->desc[0]); |
372e722e | 111 | } |
79a9becd | 112 | EXPORT_SYMBOL_GPL(desc_to_gpio); |
372e722e AC |
113 | |
114 | ||
79a9becd AC |
115 | /** |
116 | * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs | |
117 | * @desc: descriptor to return the chip of | |
118 | */ | |
119 | struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) | |
372e722e | 120 | { |
bcabdef1 | 121 | return desc ? desc->chip : NULL; |
372e722e | 122 | } |
79a9becd | 123 | EXPORT_SYMBOL_GPL(gpiod_to_chip); |
d2876d08 | 124 | |
8d0aab2f AV |
125 | /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ |
126 | static int gpiochip_find_base(int ngpio) | |
127 | { | |
83cabe33 AC |
128 | struct gpio_chip *chip; |
129 | int base = ARCH_NR_GPIOS - ngpio; | |
8d0aab2f | 130 | |
83cabe33 AC |
131 | list_for_each_entry_reverse(chip, &gpio_chips, list) { |
132 | /* found a free space? */ | |
133 | if (chip->base + chip->ngpio <= base) | |
134 | break; | |
135 | else | |
136 | /* nope, check the space right before the chip */ | |
137 | base = chip->base - ngpio; | |
8d0aab2f AV |
138 | } |
139 | ||
83cabe33 | 140 | if (gpio_is_valid(base)) { |
8d0aab2f | 141 | pr_debug("%s: found new base at %d\n", __func__, base); |
83cabe33 AC |
142 | return base; |
143 | } else { | |
144 | pr_err("%s: cannot find free range\n", __func__); | |
145 | return -ENOSPC; | |
169b6a7a | 146 | } |
169b6a7a AV |
147 | } |
148 | ||
79a9becd AC |
149 | /** |
150 | * gpiod_get_direction - return the current direction of a GPIO | |
151 | * @desc: GPIO to get the direction of | |
152 | * | |
153 | * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error. | |
154 | * | |
155 | * This function may sleep if gpiod_cansleep() is true. | |
156 | */ | |
8e53b0f1 | 157 | int gpiod_get_direction(struct gpio_desc *desc) |
80b0a602 MN |
158 | { |
159 | struct gpio_chip *chip; | |
372e722e | 160 | unsigned offset; |
80b0a602 MN |
161 | int status = -EINVAL; |
162 | ||
372e722e AC |
163 | chip = gpiod_to_chip(desc); |
164 | offset = gpio_chip_hwgpio(desc); | |
80b0a602 MN |
165 | |
166 | if (!chip->get_direction) | |
167 | return status; | |
168 | ||
372e722e | 169 | status = chip->get_direction(chip, offset); |
80b0a602 MN |
170 | if (status > 0) { |
171 | /* GPIOF_DIR_IN, or other positive */ | |
172 | status = 1; | |
8e53b0f1 | 173 | clear_bit(FLAG_IS_OUT, &desc->flags); |
80b0a602 MN |
174 | } |
175 | if (status == 0) { | |
176 | /* GPIOF_DIR_OUT */ | |
8e53b0f1 | 177 | set_bit(FLAG_IS_OUT, &desc->flags); |
80b0a602 MN |
178 | } |
179 | return status; | |
180 | } | |
79a9becd | 181 | EXPORT_SYMBOL_GPL(gpiod_get_direction); |
80b0a602 | 182 | |
1a989d0f AC |
183 | /* |
184 | * Add a new chip to the global chips list, keeping the list of chips sorted | |
ef7c7553 | 185 | * by range(means [base, base + ngpio - 1]) order. |
1a989d0f AC |
186 | * |
187 | * Return -EBUSY if the new chip overlaps with some other chip's integer | |
188 | * space. | |
189 | */ | |
190 | static int gpiochip_add_to_list(struct gpio_chip *chip) | |
191 | { | |
d1aceb80 | 192 | struct list_head *pos; |
ef7c7553 BJZ |
193 | struct gpio_chip *iterator; |
194 | struct gpio_chip *previous = NULL; | |
1a989d0f | 195 | |
ef7c7553 BJZ |
196 | if (list_empty(&gpio_chips)) { |
197 | pos = gpio_chips.next; | |
198 | goto found; | |
1a989d0f AC |
199 | } |
200 | ||
ef7c7553 BJZ |
201 | list_for_each(pos, &gpio_chips) { |
202 | iterator = list_entry(pos, struct gpio_chip, list); | |
203 | if (iterator->base >= chip->base + chip->ngpio) { | |
204 | /* | |
205 | * Iterator is the first GPIO chip so there is no | |
206 | * previous one | |
207 | */ | |
208 | if (previous == NULL) { | |
209 | goto found; | |
210 | } else { | |
211 | /* | |
212 | * We found a valid range(means | |
213 | * [base, base + ngpio - 1]) between previous | |
214 | * and iterator chip. | |
215 | */ | |
216 | if (previous->base + previous->ngpio | |
217 | <= chip->base) | |
218 | goto found; | |
219 | } | |
1a989d0f | 220 | } |
ef7c7553 | 221 | previous = iterator; |
1a989d0f AC |
222 | } |
223 | ||
ef7c7553 BJZ |
224 | /* We are beyond the last chip in the list */ |
225 | if (iterator->base + iterator->ngpio <= chip->base) | |
226 | goto found; | |
227 | ||
228 | dev_err(chip->parent, | |
229 | "GPIO integer space overlap, cannot add chip\n"); | |
230 | return -EBUSY; | |
1a989d0f | 231 | |
ef7c7553 BJZ |
232 | found: |
233 | list_add_tail(&chip->list, pos); | |
234 | return 0; | |
1a989d0f AC |
235 | } |
236 | ||
f881bab0 LW |
237 | /** |
238 | * Convert a GPIO name to its descriptor | |
239 | */ | |
240 | static struct gpio_desc *gpio_name_to_desc(const char * const name) | |
241 | { | |
242 | struct gpio_chip *chip; | |
243 | unsigned long flags; | |
244 | ||
245 | spin_lock_irqsave(&gpio_lock, flags); | |
246 | ||
247 | list_for_each_entry(chip, &gpio_chips, list) { | |
248 | int i; | |
249 | ||
250 | for (i = 0; i != chip->ngpio; ++i) { | |
251 | struct gpio_desc *gpio = &chip->desc[i]; | |
252 | ||
253 | if (!gpio->name) | |
254 | continue; | |
255 | ||
256 | if (!strcmp(gpio->name, name)) { | |
257 | spin_unlock_irqrestore(&gpio_lock, flags); | |
258 | return gpio; | |
259 | } | |
260 | } | |
261 | } | |
262 | ||
263 | spin_unlock_irqrestore(&gpio_lock, flags); | |
264 | ||
265 | return NULL; | |
266 | } | |
267 | ||
5f3ca732 MP |
268 | /* |
269 | * Takes the names from gc->names and checks if they are all unique. If they | |
270 | * are, they are assigned to their gpio descriptors. | |
271 | * | |
ed37915c | 272 | * Warning if one of the names is already used for a different GPIO. |
5f3ca732 MP |
273 | */ |
274 | static int gpiochip_set_desc_names(struct gpio_chip *gc) | |
275 | { | |
276 | int i; | |
277 | ||
278 | if (!gc->names) | |
279 | return 0; | |
280 | ||
281 | /* First check all names if they are unique */ | |
282 | for (i = 0; i != gc->ngpio; ++i) { | |
283 | struct gpio_desc *gpio; | |
284 | ||
285 | gpio = gpio_name_to_desc(gc->names[i]); | |
f881bab0 | 286 | if (gpio) |
58383c78 | 287 | dev_warn(gc->parent, "Detected name collision for " |
f881bab0 LW |
288 | "GPIO name '%s'\n", |
289 | gc->names[i]); | |
5f3ca732 MP |
290 | } |
291 | ||
292 | /* Then add all names to the GPIO descriptors */ | |
293 | for (i = 0; i != gc->ngpio; ++i) | |
294 | gc->desc[i].name = gc->names[i]; | |
295 | ||
296 | return 0; | |
297 | } | |
298 | ||
d2876d08 DB |
299 | /** |
300 | * gpiochip_add() - register a gpio_chip | |
301 | * @chip: the chip to register, with chip->base initialized | |
14e85c0e | 302 | * Context: potentially before irqs will work |
d2876d08 DB |
303 | * |
304 | * Returns a negative errno if the chip can't be registered, such as | |
305 | * because the chip->base is invalid or already associated with a | |
306 | * different chip. Otherwise it returns zero as a success code. | |
8d0aab2f | 307 | * |
d8f388d8 DB |
308 | * When gpiochip_add() is called very early during boot, so that GPIOs |
309 | * can be freely used, the chip->dev device must be registered before | |
310 | * the gpio framework's arch_initcall(). Otherwise sysfs initialization | |
311 | * for GPIOs will fail rudely. | |
312 | * | |
8d0aab2f AV |
313 | * If chip->base is negative, this requests dynamic assignment of |
314 | * a range of valid GPIOs. | |
d2876d08 DB |
315 | */ |
316 | int gpiochip_add(struct gpio_chip *chip) | |
317 | { | |
318 | unsigned long flags; | |
319 | int status = 0; | |
320 | unsigned id; | |
8d0aab2f | 321 | int base = chip->base; |
14e85c0e | 322 | struct gpio_desc *descs; |
d2876d08 | 323 | |
14e85c0e AC |
324 | descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL); |
325 | if (!descs) | |
326 | return -ENOMEM; | |
d2876d08 | 327 | |
5ed41cc4 BJZ |
328 | if (chip->ngpio == 0) { |
329 | chip_err(chip, "tried to insert a GPIO chip with zero lines\n"); | |
330 | return -EINVAL; | |
331 | } | |
332 | ||
d2876d08 DB |
333 | spin_lock_irqsave(&gpio_lock, flags); |
334 | ||
8d0aab2f AV |
335 | if (base < 0) { |
336 | base = gpiochip_find_base(chip->ngpio); | |
337 | if (base < 0) { | |
338 | status = base; | |
225fce83 JH |
339 | spin_unlock_irqrestore(&gpio_lock, flags); |
340 | goto err_free_descs; | |
8d0aab2f AV |
341 | } |
342 | chip->base = base; | |
343 | } | |
344 | ||
1a989d0f | 345 | status = gpiochip_add_to_list(chip); |
05aa5203 JH |
346 | if (status) { |
347 | spin_unlock_irqrestore(&gpio_lock, flags); | |
348 | goto err_free_descs; | |
349 | } | |
1a989d0f | 350 | |
05aa5203 JH |
351 | for (id = 0; id < chip->ngpio; id++) { |
352 | struct gpio_desc *desc = &descs[id]; | |
353 | ||
354 | desc->chip = chip; | |
355 | ||
356 | /* REVISIT: most hardware initializes GPIOs as inputs (often | |
357 | * with pullups enabled) so power usage is minimized. Linux | |
358 | * code should set the gpio direction first thing; but until | |
359 | * it does, and in case chip->get_direction is not set, we may | |
360 | * expose the wrong direction in sysfs. | |
361 | */ | |
362 | desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0; | |
d2876d08 DB |
363 | } |
364 | ||
14e85c0e AC |
365 | chip->desc = descs; |
366 | ||
3bae4811 ZG |
367 | spin_unlock_irqrestore(&gpio_lock, flags); |
368 | ||
f23f1516 SH |
369 | #ifdef CONFIG_PINCTRL |
370 | INIT_LIST_HEAD(&chip->pin_ranges); | |
371 | #endif | |
372 | ||
58383c78 LW |
373 | if (!chip->owner && chip->parent && chip->parent->driver) |
374 | chip->owner = chip->parent->driver->owner; | |
3726960e | 375 | |
5f3ca732 MP |
376 | status = gpiochip_set_desc_names(chip); |
377 | if (status) | |
378 | goto err_remove_from_list; | |
379 | ||
28355f81 TV |
380 | status = of_gpiochip_add(chip); |
381 | if (status) | |
382 | goto err_remove_chip; | |
383 | ||
664e3e5a | 384 | acpi_gpiochip_add(chip); |
391c970c | 385 | |
426577bd | 386 | status = gpiochip_sysfs_register(chip); |
225fce83 JH |
387 | if (status) |
388 | goto err_remove_chip; | |
cedb1881 | 389 | |
7589e59f | 390 | pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__, |
64842aad GL |
391 | chip->base, chip->base + chip->ngpio - 1, |
392 | chip->label ? : "generic"); | |
393 | ||
cedb1881 | 394 | return 0; |
3bae4811 | 395 | |
225fce83 JH |
396 | err_remove_chip: |
397 | acpi_gpiochip_remove(chip); | |
6d86750c | 398 | gpiochip_free_hogs(chip); |
225fce83 | 399 | of_gpiochip_remove(chip); |
5f3ca732 | 400 | err_remove_from_list: |
225fce83 JH |
401 | spin_lock_irqsave(&gpio_lock, flags); |
402 | list_del(&chip->list); | |
3bae4811 | 403 | spin_unlock_irqrestore(&gpio_lock, flags); |
05aa5203 | 404 | chip->desc = NULL; |
225fce83 | 405 | err_free_descs: |
14e85c0e | 406 | kfree(descs); |
14e85c0e | 407 | |
d2876d08 | 408 | /* failures here can mean systems won't boot... */ |
7589e59f | 409 | pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__, |
cedb1881 AV |
410 | chip->base, chip->base + chip->ngpio - 1, |
411 | chip->label ? : "generic"); | |
d2876d08 DB |
412 | return status; |
413 | } | |
414 | EXPORT_SYMBOL_GPL(gpiochip_add); | |
415 | ||
416 | /** | |
417 | * gpiochip_remove() - unregister a gpio_chip | |
418 | * @chip: the chip to unregister | |
419 | * | |
420 | * A gpio_chip with any GPIOs still requested may not be removed. | |
421 | */ | |
e1db1706 | 422 | void gpiochip_remove(struct gpio_chip *chip) |
d2876d08 | 423 | { |
fab28b89 | 424 | struct gpio_desc *desc; |
d2876d08 | 425 | unsigned long flags; |
d2876d08 | 426 | unsigned id; |
fab28b89 | 427 | bool requested = false; |
d2876d08 | 428 | |
426577bd | 429 | gpiochip_sysfs_unregister(chip); |
01cca93a | 430 | |
00acc3dc JH |
431 | gpiochip_irqchip_remove(chip); |
432 | ||
6072b9dc | 433 | acpi_gpiochip_remove(chip); |
9ef0d6f7 | 434 | gpiochip_remove_pin_ranges(chip); |
f625d460 | 435 | gpiochip_free_hogs(chip); |
391c970c AV |
436 | of_gpiochip_remove(chip); |
437 | ||
6798acaa | 438 | spin_lock_irqsave(&gpio_lock, flags); |
6c0b4e6c | 439 | for (id = 0; id < chip->ngpio; id++) { |
fab28b89 JH |
440 | desc = &chip->desc[id]; |
441 | desc->chip = NULL; | |
442 | if (test_bit(FLAG_REQUESTED, &desc->flags)) | |
443 | requested = true; | |
d2876d08 | 444 | } |
e1db1706 | 445 | list_del(&chip->list); |
d2876d08 | 446 | spin_unlock_irqrestore(&gpio_lock, flags); |
14e85c0e | 447 | |
fab28b89 | 448 | if (requested) |
58383c78 LW |
449 | dev_crit(chip->parent, |
450 | "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n"); | |
fab28b89 | 451 | |
14e85c0e AC |
452 | kfree(chip->desc); |
453 | chip->desc = NULL; | |
d2876d08 DB |
454 | } |
455 | EXPORT_SYMBOL_GPL(gpiochip_remove); | |
456 | ||
594fa265 GL |
457 | /** |
458 | * gpiochip_find() - iterator for locating a specific gpio_chip | |
459 | * @data: data to pass to match function | |
460 | * @callback: Callback function to check gpio_chip | |
461 | * | |
462 | * Similar to bus_find_device. It returns a reference to a gpio_chip as | |
463 | * determined by a user supplied @match callback. The callback should return | |
464 | * 0 if the device doesn't match and non-zero if it does. If the callback is | |
465 | * non-zero, this function will return to the caller and not iterate over any | |
466 | * more gpio_chips. | |
467 | */ | |
07ce8ec7 | 468 | struct gpio_chip *gpiochip_find(void *data, |
6e2cf651 | 469 | int (*match)(struct gpio_chip *chip, |
3d0f7cf0 | 470 | void *data)) |
594fa265 | 471 | { |
125eef96 | 472 | struct gpio_chip *chip; |
594fa265 | 473 | unsigned long flags; |
594fa265 GL |
474 | |
475 | spin_lock_irqsave(&gpio_lock, flags); | |
125eef96 AC |
476 | list_for_each_entry(chip, &gpio_chips, list) |
477 | if (match(chip, data)) | |
594fa265 | 478 | break; |
125eef96 AC |
479 | |
480 | /* No match? */ | |
481 | if (&chip->list == &gpio_chips) | |
482 | chip = NULL; | |
594fa265 GL |
483 | spin_unlock_irqrestore(&gpio_lock, flags); |
484 | ||
485 | return chip; | |
486 | } | |
8fa0c9bf | 487 | EXPORT_SYMBOL_GPL(gpiochip_find); |
d2876d08 | 488 | |
79697ef9 AC |
489 | static int gpiochip_match_name(struct gpio_chip *chip, void *data) |
490 | { | |
491 | const char *name = data; | |
492 | ||
493 | return !strcmp(chip->label, name); | |
494 | } | |
495 | ||
496 | static struct gpio_chip *find_chip_by_name(const char *name) | |
497 | { | |
498 | return gpiochip_find((void *)name, gpiochip_match_name); | |
499 | } | |
500 | ||
14250520 LW |
501 | #ifdef CONFIG_GPIOLIB_IRQCHIP |
502 | ||
503 | /* | |
504 | * The following is irqchip helper code for gpiochips. | |
505 | */ | |
506 | ||
507 | /** | |
3f97d5fc LW |
508 | * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip |
509 | * @gpiochip: the gpiochip to set the irqchip chain to | |
510 | * @irqchip: the irqchip to chain to the gpiochip | |
14250520 LW |
511 | * @parent_irq: the irq number corresponding to the parent IRQ for this |
512 | * chained irqchip | |
513 | * @parent_handler: the parent interrupt handler for the accumulated IRQ | |
3f97d5fc LW |
514 | * coming out of the gpiochip. If the interrupt is nested rather than |
515 | * cascaded, pass NULL in this handler argument | |
14250520 LW |
516 | */ |
517 | void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip, | |
518 | struct irq_chip *irqchip, | |
519 | int parent_irq, | |
520 | irq_flow_handler_t parent_handler) | |
521 | { | |
83141a77 LW |
522 | unsigned int offset; |
523 | ||
83141a77 LW |
524 | if (!gpiochip->irqdomain) { |
525 | chip_err(gpiochip, "called %s before setting up irqchip\n", | |
526 | __func__); | |
1c8732bb LW |
527 | return; |
528 | } | |
529 | ||
3f97d5fc LW |
530 | if (parent_handler) { |
531 | if (gpiochip->can_sleep) { | |
532 | chip_err(gpiochip, | |
533 | "you cannot have chained interrupts on a " | |
534 | "chip that may sleep\n"); | |
535 | return; | |
536 | } | |
3f97d5fc LW |
537 | /* |
538 | * The parent irqchip is already using the chip_data for this | |
539 | * irqchip, so our callbacks simply use the handler_data. | |
540 | */ | |
f7f87753 TG |
541 | irq_set_chained_handler_and_data(parent_irq, parent_handler, |
542 | gpiochip); | |
25e4fe92 DES |
543 | |
544 | gpiochip->irq_parent = parent_irq; | |
3f97d5fc | 545 | } |
83141a77 LW |
546 | |
547 | /* Set the parent IRQ for all affected IRQs */ | |
548 | for (offset = 0; offset < gpiochip->ngpio; offset++) | |
549 | irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset), | |
550 | parent_irq); | |
14250520 LW |
551 | } |
552 | EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip); | |
553 | ||
554 | /** | |
555 | * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip | |
556 | * @d: the irqdomain used by this irqchip | |
557 | * @irq: the global irq number used by this GPIO irqchip irq | |
558 | * @hwirq: the local IRQ/GPIO line offset on this gpiochip | |
559 | * | |
560 | * This function will set up the mapping for a certain IRQ line on a | |
561 | * gpiochip by assigning the gpiochip as chip data, and using the irqchip | |
562 | * stored inside the gpiochip. | |
563 | */ | |
564 | static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq, | |
565 | irq_hw_number_t hwirq) | |
566 | { | |
567 | struct gpio_chip *chip = d->host_data; | |
568 | ||
14250520 | 569 | irq_set_chip_data(irq, chip); |
a0a8bcf4 GS |
570 | /* |
571 | * This lock class tells lockdep that GPIO irqs are in a different | |
572 | * category than their parents, so it won't report false recursion. | |
573 | */ | |
574 | irq_set_lockdep_class(irq, chip->lock_key); | |
7633fb95 | 575 | irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler); |
1c8732bb | 576 | /* Chips that can sleep need nested thread handlers */ |
295494af | 577 | if (chip->can_sleep && !chip->irq_not_threaded) |
1c8732bb | 578 | irq_set_nested_thread(irq, 1); |
14250520 | 579 | irq_set_noprobe(irq); |
23393d49 | 580 | |
1333b90f LW |
581 | /* |
582 | * No set-up of the hardware will happen if IRQ_TYPE_NONE | |
583 | * is passed as default type. | |
584 | */ | |
585 | if (chip->irq_default_type != IRQ_TYPE_NONE) | |
586 | irq_set_irq_type(irq, chip->irq_default_type); | |
14250520 LW |
587 | |
588 | return 0; | |
589 | } | |
590 | ||
c3626fde LW |
591 | static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq) |
592 | { | |
1c8732bb LW |
593 | struct gpio_chip *chip = d->host_data; |
594 | ||
1c8732bb LW |
595 | if (chip->can_sleep) |
596 | irq_set_nested_thread(irq, 0); | |
c3626fde LW |
597 | irq_set_chip_and_handler(irq, NULL, NULL); |
598 | irq_set_chip_data(irq, NULL); | |
599 | } | |
600 | ||
14250520 LW |
601 | static const struct irq_domain_ops gpiochip_domain_ops = { |
602 | .map = gpiochip_irq_map, | |
c3626fde | 603 | .unmap = gpiochip_irq_unmap, |
14250520 LW |
604 | /* Virtually all GPIO irqchips are twocell:ed */ |
605 | .xlate = irq_domain_xlate_twocell, | |
606 | }; | |
607 | ||
608 | static int gpiochip_irq_reqres(struct irq_data *d) | |
609 | { | |
610 | struct gpio_chip *chip = irq_data_get_irq_chip_data(d); | |
611 | ||
5b76e79c GS |
612 | if (!try_module_get(chip->owner)) |
613 | return -ENODEV; | |
614 | ||
e3a2e878 | 615 | if (gpiochip_lock_as_irq(chip, d->hwirq)) { |
14250520 LW |
616 | chip_err(chip, |
617 | "unable to lock HW IRQ %lu for IRQ\n", | |
618 | d->hwirq); | |
5b76e79c | 619 | module_put(chip->owner); |
14250520 LW |
620 | return -EINVAL; |
621 | } | |
622 | return 0; | |
623 | } | |
624 | ||
625 | static void gpiochip_irq_relres(struct irq_data *d) | |
626 | { | |
627 | struct gpio_chip *chip = irq_data_get_irq_chip_data(d); | |
628 | ||
e3a2e878 | 629 | gpiochip_unlock_as_irq(chip, d->hwirq); |
5b76e79c | 630 | module_put(chip->owner); |
14250520 LW |
631 | } |
632 | ||
633 | static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset) | |
634 | { | |
635 | return irq_find_mapping(chip->irqdomain, offset); | |
636 | } | |
637 | ||
638 | /** | |
639 | * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip | |
640 | * @gpiochip: the gpiochip to remove the irqchip from | |
641 | * | |
642 | * This is called only from gpiochip_remove() | |
643 | */ | |
644 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) | |
645 | { | |
c3626fde LW |
646 | unsigned int offset; |
647 | ||
afa82fab MW |
648 | acpi_gpiochip_free_interrupts(gpiochip); |
649 | ||
25e4fe92 DES |
650 | if (gpiochip->irq_parent) { |
651 | irq_set_chained_handler(gpiochip->irq_parent, NULL); | |
652 | irq_set_handler_data(gpiochip->irq_parent, NULL); | |
653 | } | |
654 | ||
c3626fde LW |
655 | /* Remove all IRQ mappings and delete the domain */ |
656 | if (gpiochip->irqdomain) { | |
657 | for (offset = 0; offset < gpiochip->ngpio; offset++) | |
e3893386 GS |
658 | irq_dispose_mapping( |
659 | irq_find_mapping(gpiochip->irqdomain, offset)); | |
14250520 | 660 | irq_domain_remove(gpiochip->irqdomain); |
c3626fde | 661 | } |
14250520 LW |
662 | |
663 | if (gpiochip->irqchip) { | |
664 | gpiochip->irqchip->irq_request_resources = NULL; | |
665 | gpiochip->irqchip->irq_release_resources = NULL; | |
666 | gpiochip->irqchip = NULL; | |
667 | } | |
668 | } | |
669 | ||
670 | /** | |
671 | * gpiochip_irqchip_add() - adds an irqchip to a gpiochip | |
672 | * @gpiochip: the gpiochip to add the irqchip to | |
673 | * @irqchip: the irqchip to add to the gpiochip | |
674 | * @first_irq: if not dynamically assigned, the base (first) IRQ to | |
675 | * allocate gpiochip irqs from | |
676 | * @handler: the irq handler to use (often a predefined irq core function) | |
1333b90f LW |
677 | * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE |
678 | * to have the core avoid setting up any default type in the hardware. | |
a0a8bcf4 | 679 | * @lock_key: lockdep class |
14250520 LW |
680 | * |
681 | * This function closely associates a certain irqchip with a certain | |
682 | * gpiochip, providing an irq domain to translate the local IRQs to | |
683 | * global irqs in the gpiolib core, and making sure that the gpiochip | |
684 | * is passed as chip data to all related functions. Driver callbacks | |
685 | * need to use container_of() to get their local state containers back | |
686 | * from the gpiochip passed as chip data. An irqdomain will be stored | |
687 | * in the gpiochip that shall be used by the driver to handle IRQ number | |
688 | * translation. The gpiochip will need to be initialized and registered | |
689 | * before calling this function. | |
690 | * | |
c3626fde LW |
691 | * This function will handle two cell:ed simple IRQs and assumes all |
692 | * the pins on the gpiochip can generate a unique IRQ. Everything else | |
14250520 LW |
693 | * need to be open coded. |
694 | */ | |
a0a8bcf4 GS |
695 | int _gpiochip_irqchip_add(struct gpio_chip *gpiochip, |
696 | struct irq_chip *irqchip, | |
697 | unsigned int first_irq, | |
698 | irq_flow_handler_t handler, | |
699 | unsigned int type, | |
700 | struct lock_class_key *lock_key) | |
14250520 LW |
701 | { |
702 | struct device_node *of_node; | |
703 | unsigned int offset; | |
c3626fde | 704 | unsigned irq_base = 0; |
14250520 LW |
705 | |
706 | if (!gpiochip || !irqchip) | |
707 | return -EINVAL; | |
708 | ||
58383c78 | 709 | if (!gpiochip->parent) { |
14250520 LW |
710 | pr_err("missing gpiochip .dev parent pointer\n"); |
711 | return -EINVAL; | |
712 | } | |
58383c78 | 713 | of_node = gpiochip->parent->of_node; |
14250520 LW |
714 | #ifdef CONFIG_OF_GPIO |
715 | /* | |
20a8a968 | 716 | * If the gpiochip has an assigned OF node this takes precedence |
14250520 LW |
717 | * FIXME: get rid of this and use gpiochip->dev->of_node everywhere |
718 | */ | |
719 | if (gpiochip->of_node) | |
720 | of_node = gpiochip->of_node; | |
721 | #endif | |
722 | gpiochip->irqchip = irqchip; | |
723 | gpiochip->irq_handler = handler; | |
724 | gpiochip->irq_default_type = type; | |
725 | gpiochip->to_irq = gpiochip_to_irq; | |
a0a8bcf4 | 726 | gpiochip->lock_key = lock_key; |
14250520 LW |
727 | gpiochip->irqdomain = irq_domain_add_simple(of_node, |
728 | gpiochip->ngpio, first_irq, | |
729 | &gpiochip_domain_ops, gpiochip); | |
730 | if (!gpiochip->irqdomain) { | |
731 | gpiochip->irqchip = NULL; | |
732 | return -EINVAL; | |
733 | } | |
8b67a1f0 RV |
734 | |
735 | /* | |
736 | * It is possible for a driver to override this, but only if the | |
737 | * alternative functions are both implemented. | |
738 | */ | |
739 | if (!irqchip->irq_request_resources && | |
740 | !irqchip->irq_release_resources) { | |
741 | irqchip->irq_request_resources = gpiochip_irq_reqres; | |
742 | irqchip->irq_release_resources = gpiochip_irq_relres; | |
743 | } | |
14250520 LW |
744 | |
745 | /* | |
746 | * Prepare the mapping since the irqchip shall be orthogonal to | |
747 | * any gpiochip calls. If the first_irq was zero, this is | |
748 | * necessary to allocate descriptors for all IRQs. | |
749 | */ | |
c3626fde LW |
750 | for (offset = 0; offset < gpiochip->ngpio; offset++) { |
751 | irq_base = irq_create_mapping(gpiochip->irqdomain, offset); | |
752 | if (offset == 0) | |
753 | /* | |
754 | * Store the base into the gpiochip to be used when | |
755 | * unmapping the irqs. | |
756 | */ | |
757 | gpiochip->irq_base = irq_base; | |
758 | } | |
14250520 | 759 | |
afa82fab MW |
760 | acpi_gpiochip_request_interrupts(gpiochip); |
761 | ||
14250520 LW |
762 | return 0; |
763 | } | |
a0a8bcf4 | 764 | EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add); |
14250520 LW |
765 | |
766 | #else /* CONFIG_GPIOLIB_IRQCHIP */ | |
767 | ||
768 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {} | |
769 | ||
770 | #endif /* CONFIG_GPIOLIB_IRQCHIP */ | |
771 | ||
c771c2f4 JG |
772 | /** |
773 | * gpiochip_generic_request() - request the gpio function for a pin | |
774 | * @chip: the gpiochip owning the GPIO | |
775 | * @offset: the offset of the GPIO to request for GPIO function | |
776 | */ | |
777 | int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset) | |
778 | { | |
779 | return pinctrl_request_gpio(chip->base + offset); | |
780 | } | |
781 | EXPORT_SYMBOL_GPL(gpiochip_generic_request); | |
782 | ||
783 | /** | |
784 | * gpiochip_generic_free() - free the gpio function from a pin | |
785 | * @chip: the gpiochip to request the gpio function for | |
786 | * @offset: the offset of the GPIO to free from GPIO function | |
787 | */ | |
788 | void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset) | |
789 | { | |
790 | pinctrl_free_gpio(chip->base + offset); | |
791 | } | |
792 | EXPORT_SYMBOL_GPL(gpiochip_generic_free); | |
793 | ||
f23f1516 | 794 | #ifdef CONFIG_PINCTRL |
165adc9c | 795 | |
586a87e6 CR |
796 | /** |
797 | * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping | |
798 | * @chip: the gpiochip to add the range for | |
d32651f6 | 799 | * @pctldev: the pin controller to map to |
586a87e6 CR |
800 | * @gpio_offset: the start offset in the current gpio_chip number space |
801 | * @pin_group: name of the pin group inside the pin controller | |
802 | */ | |
803 | int gpiochip_add_pingroup_range(struct gpio_chip *chip, | |
804 | struct pinctrl_dev *pctldev, | |
805 | unsigned int gpio_offset, const char *pin_group) | |
806 | { | |
807 | struct gpio_pin_range *pin_range; | |
808 | int ret; | |
809 | ||
810 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); | |
811 | if (!pin_range) { | |
1a2a99c6 | 812 | chip_err(chip, "failed to allocate pin ranges\n"); |
586a87e6 CR |
813 | return -ENOMEM; |
814 | } | |
815 | ||
816 | /* Use local offset as range ID */ | |
817 | pin_range->range.id = gpio_offset; | |
818 | pin_range->range.gc = chip; | |
819 | pin_range->range.name = chip->label; | |
820 | pin_range->range.base = chip->base + gpio_offset; | |
821 | pin_range->pctldev = pctldev; | |
822 | ||
823 | ret = pinctrl_get_group_pins(pctldev, pin_group, | |
824 | &pin_range->range.pins, | |
825 | &pin_range->range.npins); | |
61c6375d MN |
826 | if (ret < 0) { |
827 | kfree(pin_range); | |
586a87e6 | 828 | return ret; |
61c6375d | 829 | } |
586a87e6 CR |
830 | |
831 | pinctrl_add_gpio_range(pctldev, &pin_range->range); | |
832 | ||
1a2a99c6 AS |
833 | chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n", |
834 | gpio_offset, gpio_offset + pin_range->range.npins - 1, | |
586a87e6 CR |
835 | pinctrl_dev_get_devname(pctldev), pin_group); |
836 | ||
837 | list_add_tail(&pin_range->node, &chip->pin_ranges); | |
838 | ||
839 | return 0; | |
840 | } | |
841 | EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range); | |
842 | ||
3f0f8670 LW |
843 | /** |
844 | * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping | |
845 | * @chip: the gpiochip to add the range for | |
846 | * @pinctrl_name: the dev_name() of the pin controller to map to | |
316511c0 LW |
847 | * @gpio_offset: the start offset in the current gpio_chip number space |
848 | * @pin_offset: the start offset in the pin controller number space | |
3f0f8670 LW |
849 | * @npins: the number of pins from the offset of each pin space (GPIO and |
850 | * pin controller) to accumulate in this range | |
851 | */ | |
1e63d7b9 | 852 | int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name, |
316511c0 | 853 | unsigned int gpio_offset, unsigned int pin_offset, |
3f0f8670 | 854 | unsigned int npins) |
f23f1516 SH |
855 | { |
856 | struct gpio_pin_range *pin_range; | |
b4d4b1f0 | 857 | int ret; |
f23f1516 | 858 | |
3f0f8670 | 859 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); |
f23f1516 | 860 | if (!pin_range) { |
1a2a99c6 | 861 | chip_err(chip, "failed to allocate pin ranges\n"); |
1e63d7b9 | 862 | return -ENOMEM; |
f23f1516 SH |
863 | } |
864 | ||
3f0f8670 | 865 | /* Use local offset as range ID */ |
316511c0 | 866 | pin_range->range.id = gpio_offset; |
3f0f8670 | 867 | pin_range->range.gc = chip; |
f23f1516 | 868 | pin_range->range.name = chip->label; |
316511c0 LW |
869 | pin_range->range.base = chip->base + gpio_offset; |
870 | pin_range->range.pin_base = pin_offset; | |
f23f1516 | 871 | pin_range->range.npins = npins; |
192c369c | 872 | pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name, |
f23f1516 | 873 | &pin_range->range); |
8f23ca1a | 874 | if (IS_ERR(pin_range->pctldev)) { |
b4d4b1f0 | 875 | ret = PTR_ERR(pin_range->pctldev); |
1a2a99c6 | 876 | chip_err(chip, "could not create pin range\n"); |
3f0f8670 | 877 | kfree(pin_range); |
b4d4b1f0 | 878 | return ret; |
3f0f8670 | 879 | } |
1a2a99c6 AS |
880 | chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n", |
881 | gpio_offset, gpio_offset + npins - 1, | |
316511c0 LW |
882 | pinctl_name, |
883 | pin_offset, pin_offset + npins - 1); | |
f23f1516 SH |
884 | |
885 | list_add_tail(&pin_range->node, &chip->pin_ranges); | |
1e63d7b9 LW |
886 | |
887 | return 0; | |
f23f1516 | 888 | } |
165adc9c | 889 | EXPORT_SYMBOL_GPL(gpiochip_add_pin_range); |
f23f1516 | 890 | |
3f0f8670 LW |
891 | /** |
892 | * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings | |
893 | * @chip: the chip to remove all the mappings for | |
894 | */ | |
f23f1516 SH |
895 | void gpiochip_remove_pin_ranges(struct gpio_chip *chip) |
896 | { | |
897 | struct gpio_pin_range *pin_range, *tmp; | |
898 | ||
899 | list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) { | |
900 | list_del(&pin_range->node); | |
901 | pinctrl_remove_gpio_range(pin_range->pctldev, | |
902 | &pin_range->range); | |
3f0f8670 | 903 | kfree(pin_range); |
f23f1516 SH |
904 | } |
905 | } | |
165adc9c LW |
906 | EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges); |
907 | ||
908 | #endif /* CONFIG_PINCTRL */ | |
f23f1516 | 909 | |
d2876d08 DB |
910 | /* These "optional" allocation calls help prevent drivers from stomping |
911 | * on each other, and help provide better diagnostics in debugfs. | |
912 | * They're called even less than the "set direction" calls. | |
913 | */ | |
77c2d792 | 914 | static int __gpiod_request(struct gpio_desc *desc, const char *label) |
d2876d08 | 915 | { |
77c2d792 MW |
916 | struct gpio_chip *chip = desc->chip; |
917 | int status; | |
d2876d08 DB |
918 | unsigned long flags; |
919 | ||
bcabdef1 AC |
920 | spin_lock_irqsave(&gpio_lock, flags); |
921 | ||
d2876d08 | 922 | /* NOTE: gpio_request() can be called in early boot, |
35e8bb51 | 923 | * before IRQs are enabled, for non-sleeping (SOC) GPIOs. |
d2876d08 DB |
924 | */ |
925 | ||
926 | if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { | |
927 | desc_set_label(desc, label ? : "?"); | |
928 | status = 0; | |
438d8908 | 929 | } else { |
d2876d08 | 930 | status = -EBUSY; |
7460db56 | 931 | goto done; |
35e8bb51 DB |
932 | } |
933 | ||
934 | if (chip->request) { | |
935 | /* chip->request may sleep */ | |
936 | spin_unlock_irqrestore(&gpio_lock, flags); | |
372e722e | 937 | status = chip->request(chip, gpio_chip_hwgpio(desc)); |
35e8bb51 DB |
938 | spin_lock_irqsave(&gpio_lock, flags); |
939 | ||
940 | if (status < 0) { | |
941 | desc_set_label(desc, NULL); | |
35e8bb51 | 942 | clear_bit(FLAG_REQUESTED, &desc->flags); |
80b0a602 | 943 | goto done; |
35e8bb51 | 944 | } |
438d8908 | 945 | } |
80b0a602 MN |
946 | if (chip->get_direction) { |
947 | /* chip->get_direction may sleep */ | |
948 | spin_unlock_irqrestore(&gpio_lock, flags); | |
372e722e | 949 | gpiod_get_direction(desc); |
80b0a602 MN |
950 | spin_lock_irqsave(&gpio_lock, flags); |
951 | } | |
77c2d792 | 952 | done: |
923b93e4 LP |
953 | if (status < 0) { |
954 | /* Clear flags that might have been set by the caller before | |
955 | * requesting the GPIO. | |
956 | */ | |
957 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); | |
958 | clear_bit(FLAG_OPEN_DRAIN, &desc->flags); | |
959 | clear_bit(FLAG_OPEN_SOURCE, &desc->flags); | |
960 | } | |
77c2d792 MW |
961 | spin_unlock_irqrestore(&gpio_lock, flags); |
962 | return status; | |
963 | } | |
964 | ||
0eb4c6c2 | 965 | int gpiod_request(struct gpio_desc *desc, const char *label) |
77c2d792 MW |
966 | { |
967 | int status = -EPROBE_DEFER; | |
968 | struct gpio_chip *chip; | |
969 | ||
970 | if (!desc) { | |
971 | pr_warn("%s: invalid GPIO\n", __func__); | |
972 | return -EINVAL; | |
973 | } | |
974 | ||
975 | chip = desc->chip; | |
976 | if (!chip) | |
977 | goto done; | |
978 | ||
979 | if (try_module_get(chip->owner)) { | |
980 | status = __gpiod_request(desc, label); | |
981 | if (status < 0) | |
982 | module_put(chip->owner); | |
983 | } | |
984 | ||
d2876d08 DB |
985 | done: |
986 | if (status) | |
7589e59f | 987 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
77c2d792 | 988 | |
d2876d08 DB |
989 | return status; |
990 | } | |
372e722e | 991 | |
77c2d792 | 992 | static bool __gpiod_free(struct gpio_desc *desc) |
d2876d08 | 993 | { |
77c2d792 | 994 | bool ret = false; |
d2876d08 | 995 | unsigned long flags; |
35e8bb51 | 996 | struct gpio_chip *chip; |
d2876d08 | 997 | |
3d599d1c UKK |
998 | might_sleep(); |
999 | ||
372e722e | 1000 | gpiod_unexport(desc); |
d8f388d8 | 1001 | |
d2876d08 DB |
1002 | spin_lock_irqsave(&gpio_lock, flags); |
1003 | ||
35e8bb51 DB |
1004 | chip = desc->chip; |
1005 | if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) { | |
1006 | if (chip->free) { | |
1007 | spin_unlock_irqrestore(&gpio_lock, flags); | |
9c4ba946 | 1008 | might_sleep_if(chip->can_sleep); |
372e722e | 1009 | chip->free(chip, gpio_chip_hwgpio(desc)); |
35e8bb51 DB |
1010 | spin_lock_irqsave(&gpio_lock, flags); |
1011 | } | |
d2876d08 | 1012 | desc_set_label(desc, NULL); |
07697461 | 1013 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); |
35e8bb51 | 1014 | clear_bit(FLAG_REQUESTED, &desc->flags); |
aca5ce14 | 1015 | clear_bit(FLAG_OPEN_DRAIN, &desc->flags); |
25553ff0 | 1016 | clear_bit(FLAG_OPEN_SOURCE, &desc->flags); |
f625d460 | 1017 | clear_bit(FLAG_IS_HOGGED, &desc->flags); |
77c2d792 MW |
1018 | ret = true; |
1019 | } | |
d2876d08 DB |
1020 | |
1021 | spin_unlock_irqrestore(&gpio_lock, flags); | |
77c2d792 MW |
1022 | return ret; |
1023 | } | |
1024 | ||
0eb4c6c2 | 1025 | void gpiod_free(struct gpio_desc *desc) |
77c2d792 MW |
1026 | { |
1027 | if (desc && __gpiod_free(desc)) | |
1028 | module_put(desc->chip->owner); | |
1029 | else | |
1030 | WARN_ON(extra_checks); | |
d2876d08 | 1031 | } |
372e722e | 1032 | |
d2876d08 DB |
1033 | /** |
1034 | * gpiochip_is_requested - return string iff signal was requested | |
1035 | * @chip: controller managing the signal | |
1036 | * @offset: of signal within controller's 0..(ngpio - 1) range | |
1037 | * | |
1038 | * Returns NULL if the GPIO is not currently requested, else a string. | |
9c8318ff AC |
1039 | * The string returned is the label passed to gpio_request(); if none has been |
1040 | * passed it is a meaningless, non-NULL constant. | |
d2876d08 DB |
1041 | * |
1042 | * This function is for use by GPIO controller drivers. The label can | |
1043 | * help with diagnostics, and knowing that the signal is used as a GPIO | |
1044 | * can help avoid accidentally multiplexing it to another controller. | |
1045 | */ | |
1046 | const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset) | |
1047 | { | |
6c0b4e6c | 1048 | struct gpio_desc *desc; |
d2876d08 | 1049 | |
48b5953e | 1050 | if (offset >= chip->ngpio) |
d2876d08 | 1051 | return NULL; |
6c0b4e6c AC |
1052 | |
1053 | desc = &chip->desc[offset]; | |
1054 | ||
372e722e | 1055 | if (test_bit(FLAG_REQUESTED, &desc->flags) == 0) |
d2876d08 | 1056 | return NULL; |
372e722e | 1057 | return desc->label; |
d2876d08 DB |
1058 | } |
1059 | EXPORT_SYMBOL_GPL(gpiochip_is_requested); | |
1060 | ||
77c2d792 MW |
1061 | /** |
1062 | * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor | |
1063 | * @desc: GPIO descriptor to request | |
1064 | * @label: label for the GPIO | |
1065 | * | |
1066 | * Function allows GPIO chip drivers to request and use their own GPIO | |
1067 | * descriptors via gpiolib API. Difference to gpiod_request() is that this | |
1068 | * function will not increase reference count of the GPIO chip module. This | |
1069 | * allows the GPIO chip module to be unloaded as needed (we assume that the | |
1070 | * GPIO chip driver handles freeing the GPIOs it has requested). | |
1071 | */ | |
abdc08a3 AC |
1072 | struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum, |
1073 | const char *label) | |
77c2d792 | 1074 | { |
abdc08a3 AC |
1075 | struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum); |
1076 | int err; | |
77c2d792 | 1077 | |
abdc08a3 AC |
1078 | if (IS_ERR(desc)) { |
1079 | chip_err(chip, "failed to get GPIO descriptor\n"); | |
1080 | return desc; | |
1081 | } | |
1082 | ||
1083 | err = __gpiod_request(desc, label); | |
1084 | if (err < 0) | |
1085 | return ERR_PTR(err); | |
77c2d792 | 1086 | |
abdc08a3 | 1087 | return desc; |
77c2d792 | 1088 | } |
f7d4ad98 | 1089 | EXPORT_SYMBOL_GPL(gpiochip_request_own_desc); |
77c2d792 MW |
1090 | |
1091 | /** | |
1092 | * gpiochip_free_own_desc - Free GPIO requested by the chip driver | |
1093 | * @desc: GPIO descriptor to free | |
1094 | * | |
1095 | * Function frees the given GPIO requested previously with | |
1096 | * gpiochip_request_own_desc(). | |
1097 | */ | |
1098 | void gpiochip_free_own_desc(struct gpio_desc *desc) | |
1099 | { | |
1100 | if (desc) | |
1101 | __gpiod_free(desc); | |
1102 | } | |
f7d4ad98 | 1103 | EXPORT_SYMBOL_GPL(gpiochip_free_own_desc); |
d2876d08 DB |
1104 | |
1105 | /* Drivers MUST set GPIO direction before making get/set calls. In | |
1106 | * some cases this is done in early boot, before IRQs are enabled. | |
1107 | * | |
1108 | * As a rule these aren't called more than once (except for drivers | |
1109 | * using the open-drain emulation idiom) so these are natural places | |
1110 | * to accumulate extra debugging checks. Note that we can't (yet) | |
1111 | * rely on gpio_request() having been called beforehand. | |
1112 | */ | |
1113 | ||
79a9becd AC |
1114 | /** |
1115 | * gpiod_direction_input - set the GPIO direction to input | |
1116 | * @desc: GPIO to set to input | |
1117 | * | |
1118 | * Set the direction of the passed GPIO to input, such as gpiod_get_value() can | |
1119 | * be called safely on it. | |
1120 | * | |
1121 | * Return 0 in case of success, else an error code. | |
1122 | */ | |
1123 | int gpiod_direction_input(struct gpio_desc *desc) | |
d2876d08 | 1124 | { |
d2876d08 | 1125 | struct gpio_chip *chip; |
d2876d08 DB |
1126 | int status = -EINVAL; |
1127 | ||
be1a4b13 | 1128 | if (!desc || !desc->chip) { |
bcabdef1 AC |
1129 | pr_warn("%s: invalid GPIO\n", __func__); |
1130 | return -EINVAL; | |
1131 | } | |
1132 | ||
be1a4b13 LW |
1133 | chip = desc->chip; |
1134 | if (!chip->get || !chip->direction_input) { | |
6424de5a MB |
1135 | gpiod_warn(desc, |
1136 | "%s: missing get() or direction_input() operations\n", | |
7589e59f | 1137 | __func__); |
be1a4b13 LW |
1138 | return -EIO; |
1139 | } | |
1140 | ||
d82da797 | 1141 | status = chip->direction_input(chip, gpio_chip_hwgpio(desc)); |
d2876d08 DB |
1142 | if (status == 0) |
1143 | clear_bit(FLAG_IS_OUT, &desc->flags); | |
3f397c21 | 1144 | |
372e722e | 1145 | trace_gpio_direction(desc_to_gpio(desc), 1, status); |
d82da797 | 1146 | |
d2876d08 DB |
1147 | return status; |
1148 | } | |
79a9becd | 1149 | EXPORT_SYMBOL_GPL(gpiod_direction_input); |
372e722e | 1150 | |
ef70bbe1 | 1151 | static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value) |
d2876d08 | 1152 | { |
d2876d08 | 1153 | struct gpio_chip *chip; |
d2876d08 DB |
1154 | int status = -EINVAL; |
1155 | ||
d468bf9e LW |
1156 | /* GPIOs used for IRQs shall not be set as output */ |
1157 | if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) { | |
1158 | gpiod_err(desc, | |
1159 | "%s: tried to set a GPIO tied to an IRQ as output\n", | |
1160 | __func__); | |
1161 | return -EIO; | |
1162 | } | |
1163 | ||
aca5ce14 LD |
1164 | /* Open drain pin should not be driven to 1 */ |
1165 | if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags)) | |
372e722e | 1166 | return gpiod_direction_input(desc); |
aca5ce14 | 1167 | |
25553ff0 LD |
1168 | /* Open source pin should not be driven to 0 */ |
1169 | if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags)) | |
372e722e | 1170 | return gpiod_direction_input(desc); |
25553ff0 | 1171 | |
be1a4b13 LW |
1172 | chip = desc->chip; |
1173 | if (!chip->set || !chip->direction_output) { | |
6424de5a MB |
1174 | gpiod_warn(desc, |
1175 | "%s: missing set() or direction_output() operations\n", | |
1176 | __func__); | |
be1a4b13 LW |
1177 | return -EIO; |
1178 | } | |
1179 | ||
d82da797 | 1180 | status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value); |
d2876d08 DB |
1181 | if (status == 0) |
1182 | set_bit(FLAG_IS_OUT, &desc->flags); | |
372e722e AC |
1183 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
1184 | trace_gpio_direction(desc_to_gpio(desc), 0, status); | |
d2876d08 DB |
1185 | return status; |
1186 | } | |
ef70bbe1 PZ |
1187 | |
1188 | /** | |
1189 | * gpiod_direction_output_raw - set the GPIO direction to output | |
1190 | * @desc: GPIO to set to output | |
1191 | * @value: initial output value of the GPIO | |
1192 | * | |
1193 | * Set the direction of the passed GPIO to output, such as gpiod_set_value() can | |
1194 | * be called safely on it. The initial value of the output must be specified | |
1195 | * as raw value on the physical line without regard for the ACTIVE_LOW status. | |
1196 | * | |
1197 | * Return 0 in case of success, else an error code. | |
1198 | */ | |
1199 | int gpiod_direction_output_raw(struct gpio_desc *desc, int value) | |
1200 | { | |
1201 | if (!desc || !desc->chip) { | |
1202 | pr_warn("%s: invalid GPIO\n", __func__); | |
1203 | return -EINVAL; | |
1204 | } | |
1205 | return _gpiod_direction_output_raw(desc, value); | |
1206 | } | |
1207 | EXPORT_SYMBOL_GPL(gpiod_direction_output_raw); | |
1208 | ||
1209 | /** | |
90df4fe0 | 1210 | * gpiod_direction_output - set the GPIO direction to output |
ef70bbe1 PZ |
1211 | * @desc: GPIO to set to output |
1212 | * @value: initial output value of the GPIO | |
1213 | * | |
1214 | * Set the direction of the passed GPIO to output, such as gpiod_set_value() can | |
1215 | * be called safely on it. The initial value of the output must be specified | |
1216 | * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into | |
1217 | * account. | |
1218 | * | |
1219 | * Return 0 in case of success, else an error code. | |
1220 | */ | |
1221 | int gpiod_direction_output(struct gpio_desc *desc, int value) | |
1222 | { | |
1223 | if (!desc || !desc->chip) { | |
1224 | pr_warn("%s: invalid GPIO\n", __func__); | |
1225 | return -EINVAL; | |
1226 | } | |
1227 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | |
1228 | value = !value; | |
1229 | return _gpiod_direction_output_raw(desc, value); | |
1230 | } | |
79a9becd | 1231 | EXPORT_SYMBOL_GPL(gpiod_direction_output); |
d2876d08 | 1232 | |
c4b5be98 | 1233 | /** |
79a9becd | 1234 | * gpiod_set_debounce - sets @debounce time for a @gpio |
c4b5be98 FB |
1235 | * @gpio: the gpio to set debounce time |
1236 | * @debounce: debounce time is microseconds | |
65d87656 LW |
1237 | * |
1238 | * returns -ENOTSUPP if the controller does not support setting | |
1239 | * debounce. | |
c4b5be98 | 1240 | */ |
79a9becd | 1241 | int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) |
c4b5be98 | 1242 | { |
c4b5be98 | 1243 | struct gpio_chip *chip; |
c4b5be98 | 1244 | |
be1a4b13 | 1245 | if (!desc || !desc->chip) { |
bcabdef1 AC |
1246 | pr_warn("%s: invalid GPIO\n", __func__); |
1247 | return -EINVAL; | |
1248 | } | |
1249 | ||
c4b5be98 | 1250 | chip = desc->chip; |
be1a4b13 | 1251 | if (!chip->set || !chip->set_debounce) { |
6424de5a MB |
1252 | gpiod_dbg(desc, |
1253 | "%s: missing set() or set_debounce() operations\n", | |
1254 | __func__); | |
65d87656 | 1255 | return -ENOTSUPP; |
be1a4b13 LW |
1256 | } |
1257 | ||
d82da797 | 1258 | return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce); |
c4b5be98 | 1259 | } |
79a9becd | 1260 | EXPORT_SYMBOL_GPL(gpiod_set_debounce); |
372e722e | 1261 | |
79a9becd AC |
1262 | /** |
1263 | * gpiod_is_active_low - test whether a GPIO is active-low or not | |
1264 | * @desc: the gpio descriptor to test | |
1265 | * | |
1266 | * Returns 1 if the GPIO is active-low, 0 otherwise. | |
1267 | */ | |
1268 | int gpiod_is_active_low(const struct gpio_desc *desc) | |
372e722e | 1269 | { |
79a9becd | 1270 | return test_bit(FLAG_ACTIVE_LOW, &desc->flags); |
372e722e | 1271 | } |
79a9becd | 1272 | EXPORT_SYMBOL_GPL(gpiod_is_active_low); |
d2876d08 DB |
1273 | |
1274 | /* I/O calls are only valid after configuration completed; the relevant | |
1275 | * "is this a valid GPIO" error checks should already have been done. | |
1276 | * | |
1277 | * "Get" operations are often inlinable as reading a pin value register, | |
1278 | * and masking the relevant bit in that register. | |
1279 | * | |
1280 | * When "set" operations are inlinable, they involve writing that mask to | |
1281 | * one register to set a low value, or a different register to set it high. | |
1282 | * Otherwise locking is needed, so there may be little value to inlining. | |
1283 | * | |
1284 | *------------------------------------------------------------------------ | |
1285 | * | |
1286 | * IMPORTANT!!! The hot paths -- get/set value -- assume that callers | |
1287 | * have requested the GPIO. That can include implicit requesting by | |
1288 | * a direction setting call. Marking a gpio as requested locks its chip | |
1289 | * in memory, guaranteeing that these table lookups need no more locking | |
1290 | * and that gpiochip_remove() will fail. | |
1291 | * | |
1292 | * REVISIT when debugging, consider adding some instrumentation to ensure | |
1293 | * that the GPIO was actually requested. | |
1294 | */ | |
1295 | ||
e20538b8 | 1296 | static int _gpiod_get_raw_value(const struct gpio_desc *desc) |
d2876d08 DB |
1297 | { |
1298 | struct gpio_chip *chip; | |
372e722e | 1299 | int offset; |
e20538b8 | 1300 | int value; |
d2876d08 | 1301 | |
372e722e AC |
1302 | chip = desc->chip; |
1303 | offset = gpio_chip_hwgpio(desc); | |
e20538b8 BA |
1304 | value = chip->get ? chip->get(chip, offset) : -EIO; |
1305 | value = value < 0 ? value : !!value; | |
372e722e | 1306 | trace_gpio_value(desc_to_gpio(desc), 1, value); |
3f397c21 | 1307 | return value; |
d2876d08 | 1308 | } |
372e722e | 1309 | |
d2876d08 | 1310 | /** |
79a9becd AC |
1311 | * gpiod_get_raw_value() - return a gpio's raw value |
1312 | * @desc: gpio whose value will be returned | |
d2876d08 | 1313 | * |
79a9becd | 1314 | * Return the GPIO's raw value, i.e. the value of the physical line disregarding |
e20538b8 | 1315 | * its ACTIVE_LOW status, or negative errno on failure. |
79a9becd AC |
1316 | * |
1317 | * This function should be called from contexts where we cannot sleep, and will | |
1318 | * complain if the GPIO chip functions potentially sleep. | |
d2876d08 | 1319 | */ |
79a9becd | 1320 | int gpiod_get_raw_value(const struct gpio_desc *desc) |
d2876d08 | 1321 | { |
bcabdef1 AC |
1322 | if (!desc) |
1323 | return 0; | |
e4e449e8 | 1324 | /* Should be using gpio_get_value_cansleep() */ |
d8e0ac08 | 1325 | WARN_ON(desc->chip->can_sleep); |
79a9becd | 1326 | return _gpiod_get_raw_value(desc); |
d2876d08 | 1327 | } |
79a9becd | 1328 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value); |
372e722e | 1329 | |
79a9becd AC |
1330 | /** |
1331 | * gpiod_get_value() - return a gpio's value | |
1332 | * @desc: gpio whose value will be returned | |
1333 | * | |
1334 | * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into | |
e20538b8 | 1335 | * account, or negative errno on failure. |
79a9becd AC |
1336 | * |
1337 | * This function should be called from contexts where we cannot sleep, and will | |
1338 | * complain if the GPIO chip functions potentially sleep. | |
1339 | */ | |
1340 | int gpiod_get_value(const struct gpio_desc *desc) | |
372e722e | 1341 | { |
79a9becd AC |
1342 | int value; |
1343 | if (!desc) | |
1344 | return 0; | |
1345 | /* Should be using gpio_get_value_cansleep() */ | |
1346 | WARN_ON(desc->chip->can_sleep); | |
1347 | ||
1348 | value = _gpiod_get_raw_value(desc); | |
e20538b8 BA |
1349 | if (value < 0) |
1350 | return value; | |
1351 | ||
79a9becd AC |
1352 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
1353 | value = !value; | |
1354 | ||
1355 | return value; | |
372e722e | 1356 | } |
79a9becd | 1357 | EXPORT_SYMBOL_GPL(gpiod_get_value); |
d2876d08 | 1358 | |
aca5ce14 LD |
1359 | /* |
1360 | * _gpio_set_open_drain_value() - Set the open drain gpio's value. | |
79a9becd | 1361 | * @desc: gpio descriptor whose state need to be set. |
20a8a968 | 1362 | * @value: Non-zero for setting it HIGH otherwise it will set to LOW. |
aca5ce14 | 1363 | */ |
23600969 | 1364 | static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value) |
aca5ce14 LD |
1365 | { |
1366 | int err = 0; | |
372e722e AC |
1367 | struct gpio_chip *chip = desc->chip; |
1368 | int offset = gpio_chip_hwgpio(desc); | |
1369 | ||
aca5ce14 | 1370 | if (value) { |
372e722e | 1371 | err = chip->direction_input(chip, offset); |
aca5ce14 | 1372 | if (!err) |
372e722e | 1373 | clear_bit(FLAG_IS_OUT, &desc->flags); |
aca5ce14 | 1374 | } else { |
372e722e | 1375 | err = chip->direction_output(chip, offset, 0); |
aca5ce14 | 1376 | if (!err) |
372e722e | 1377 | set_bit(FLAG_IS_OUT, &desc->flags); |
aca5ce14 | 1378 | } |
372e722e | 1379 | trace_gpio_direction(desc_to_gpio(desc), value, err); |
aca5ce14 | 1380 | if (err < 0) |
6424de5a MB |
1381 | gpiod_err(desc, |
1382 | "%s: Error in set_value for open drain err %d\n", | |
1383 | __func__, err); | |
aca5ce14 LD |
1384 | } |
1385 | ||
25553ff0 | 1386 | /* |
79a9becd AC |
1387 | * _gpio_set_open_source_value() - Set the open source gpio's value. |
1388 | * @desc: gpio descriptor whose state need to be set. | |
20a8a968 | 1389 | * @value: Non-zero for setting it HIGH otherwise it will set to LOW. |
25553ff0 | 1390 | */ |
23600969 | 1391 | static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value) |
25553ff0 LD |
1392 | { |
1393 | int err = 0; | |
372e722e AC |
1394 | struct gpio_chip *chip = desc->chip; |
1395 | int offset = gpio_chip_hwgpio(desc); | |
1396 | ||
25553ff0 | 1397 | if (value) { |
372e722e | 1398 | err = chip->direction_output(chip, offset, 1); |
25553ff0 | 1399 | if (!err) |
372e722e | 1400 | set_bit(FLAG_IS_OUT, &desc->flags); |
25553ff0 | 1401 | } else { |
372e722e | 1402 | err = chip->direction_input(chip, offset); |
25553ff0 | 1403 | if (!err) |
372e722e | 1404 | clear_bit(FLAG_IS_OUT, &desc->flags); |
25553ff0 | 1405 | } |
372e722e | 1406 | trace_gpio_direction(desc_to_gpio(desc), !value, err); |
25553ff0 | 1407 | if (err < 0) |
6424de5a MB |
1408 | gpiod_err(desc, |
1409 | "%s: Error in set_value for open source err %d\n", | |
1410 | __func__, err); | |
25553ff0 LD |
1411 | } |
1412 | ||
23600969 | 1413 | static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value) |
d2876d08 DB |
1414 | { |
1415 | struct gpio_chip *chip; | |
1416 | ||
372e722e | 1417 | chip = desc->chip; |
372e722e AC |
1418 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
1419 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) | |
1420 | _gpio_set_open_drain_value(desc, value); | |
1421 | else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) | |
1422 | _gpio_set_open_source_value(desc, value); | |
aca5ce14 | 1423 | else |
372e722e AC |
1424 | chip->set(chip, gpio_chip_hwgpio(desc), value); |
1425 | } | |
1426 | ||
5f424243 RI |
1427 | /* |
1428 | * set multiple outputs on the same chip; | |
1429 | * use the chip's set_multiple function if available; | |
1430 | * otherwise set the outputs sequentially; | |
1431 | * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word | |
1432 | * defines which outputs are to be changed | |
1433 | * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word | |
1434 | * defines the values the outputs specified by mask are to be set to | |
1435 | */ | |
1436 | static void gpio_chip_set_multiple(struct gpio_chip *chip, | |
1437 | unsigned long *mask, unsigned long *bits) | |
1438 | { | |
1439 | if (chip->set_multiple) { | |
1440 | chip->set_multiple(chip, mask, bits); | |
1441 | } else { | |
1442 | int i; | |
1443 | for (i = 0; i < chip->ngpio; i++) { | |
1444 | if (mask[BIT_WORD(i)] == 0) { | |
1445 | /* no more set bits in this mask word; | |
1446 | * skip ahead to the next word */ | |
1447 | i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1; | |
1448 | continue; | |
1449 | } | |
1450 | /* set outputs if the corresponding mask bit is set */ | |
38e003f4 | 1451 | if (__test_and_clear_bit(i, mask)) |
5f424243 | 1452 | chip->set(chip, i, test_bit(i, bits)); |
5f424243 RI |
1453 | } |
1454 | } | |
1455 | } | |
1456 | ||
3fff99bc RI |
1457 | static void gpiod_set_array_value_priv(bool raw, bool can_sleep, |
1458 | unsigned int array_size, | |
1459 | struct gpio_desc **desc_array, | |
1460 | int *value_array) | |
5f424243 RI |
1461 | { |
1462 | int i = 0; | |
1463 | ||
1464 | while (i < array_size) { | |
1465 | struct gpio_chip *chip = desc_array[i]->chip; | |
1466 | unsigned long mask[BITS_TO_LONGS(chip->ngpio)]; | |
1467 | unsigned long bits[BITS_TO_LONGS(chip->ngpio)]; | |
1468 | int count = 0; | |
1469 | ||
38e003f4 | 1470 | if (!can_sleep) |
5f424243 | 1471 | WARN_ON(chip->can_sleep); |
38e003f4 | 1472 | |
5f424243 RI |
1473 | memset(mask, 0, sizeof(mask)); |
1474 | do { | |
1475 | struct gpio_desc *desc = desc_array[i]; | |
1476 | int hwgpio = gpio_chip_hwgpio(desc); | |
1477 | int value = value_array[i]; | |
1478 | ||
1479 | if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | |
1480 | value = !value; | |
1481 | trace_gpio_value(desc_to_gpio(desc), 0, value); | |
1482 | /* | |
1483 | * collect all normal outputs belonging to the same chip | |
1484 | * open drain and open source outputs are set individually | |
1485 | */ | |
1486 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { | |
38e003f4 | 1487 | _gpio_set_open_drain_value(desc, value); |
5f424243 RI |
1488 | } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) { |
1489 | _gpio_set_open_source_value(desc, value); | |
1490 | } else { | |
1491 | __set_bit(hwgpio, mask); | |
38e003f4 | 1492 | if (value) |
5f424243 | 1493 | __set_bit(hwgpio, bits); |
38e003f4 | 1494 | else |
5f424243 | 1495 | __clear_bit(hwgpio, bits); |
5f424243 RI |
1496 | count++; |
1497 | } | |
1498 | i++; | |
1499 | } while ((i < array_size) && (desc_array[i]->chip == chip)); | |
1500 | /* push collected bits to outputs */ | |
38e003f4 | 1501 | if (count != 0) |
5f424243 | 1502 | gpio_chip_set_multiple(chip, mask, bits); |
5f424243 RI |
1503 | } |
1504 | } | |
1505 | ||
d2876d08 | 1506 | /** |
79a9becd AC |
1507 | * gpiod_set_raw_value() - assign a gpio's raw value |
1508 | * @desc: gpio whose value will be assigned | |
d2876d08 | 1509 | * @value: value to assign |
d2876d08 | 1510 | * |
79a9becd AC |
1511 | * Set the raw value of the GPIO, i.e. the value of its physical line without |
1512 | * regard for its ACTIVE_LOW status. | |
1513 | * | |
1514 | * This function should be called from contexts where we cannot sleep, and will | |
1515 | * complain if the GPIO chip functions potentially sleep. | |
d2876d08 | 1516 | */ |
79a9becd | 1517 | void gpiod_set_raw_value(struct gpio_desc *desc, int value) |
372e722e | 1518 | { |
bcabdef1 AC |
1519 | if (!desc) |
1520 | return; | |
e4e449e8 | 1521 | /* Should be using gpio_set_value_cansleep() */ |
d8e0ac08 | 1522 | WARN_ON(desc->chip->can_sleep); |
79a9becd | 1523 | _gpiod_set_raw_value(desc, value); |
d2876d08 | 1524 | } |
79a9becd | 1525 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value); |
d2876d08 DB |
1526 | |
1527 | /** | |
79a9becd AC |
1528 | * gpiod_set_value() - assign a gpio's value |
1529 | * @desc: gpio whose value will be assigned | |
1530 | * @value: value to assign | |
1531 | * | |
1532 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into | |
1533 | * account | |
d2876d08 | 1534 | * |
79a9becd AC |
1535 | * This function should be called from contexts where we cannot sleep, and will |
1536 | * complain if the GPIO chip functions potentially sleep. | |
d2876d08 | 1537 | */ |
79a9becd | 1538 | void gpiod_set_value(struct gpio_desc *desc, int value) |
d2876d08 | 1539 | { |
bcabdef1 | 1540 | if (!desc) |
79a9becd AC |
1541 | return; |
1542 | /* Should be using gpio_set_value_cansleep() */ | |
1543 | WARN_ON(desc->chip->can_sleep); | |
1544 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | |
1545 | value = !value; | |
1546 | _gpiod_set_raw_value(desc, value); | |
372e722e | 1547 | } |
79a9becd | 1548 | EXPORT_SYMBOL_GPL(gpiod_set_value); |
d2876d08 | 1549 | |
5f424243 | 1550 | /** |
3fff99bc | 1551 | * gpiod_set_raw_array_value() - assign values to an array of GPIOs |
5f424243 RI |
1552 | * @array_size: number of elements in the descriptor / value arrays |
1553 | * @desc_array: array of GPIO descriptors whose values will be assigned | |
1554 | * @value_array: array of values to assign | |
1555 | * | |
1556 | * Set the raw values of the GPIOs, i.e. the values of the physical lines | |
1557 | * without regard for their ACTIVE_LOW status. | |
1558 | * | |
1559 | * This function should be called from contexts where we cannot sleep, and will | |
1560 | * complain if the GPIO chip functions potentially sleep. | |
1561 | */ | |
3fff99bc | 1562 | void gpiod_set_raw_array_value(unsigned int array_size, |
5f424243 RI |
1563 | struct gpio_desc **desc_array, int *value_array) |
1564 | { | |
1565 | if (!desc_array) | |
1566 | return; | |
3fff99bc RI |
1567 | gpiod_set_array_value_priv(true, false, array_size, desc_array, |
1568 | value_array); | |
5f424243 | 1569 | } |
3fff99bc | 1570 | EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value); |
5f424243 RI |
1571 | |
1572 | /** | |
3fff99bc | 1573 | * gpiod_set_array_value() - assign values to an array of GPIOs |
5f424243 RI |
1574 | * @array_size: number of elements in the descriptor / value arrays |
1575 | * @desc_array: array of GPIO descriptors whose values will be assigned | |
1576 | * @value_array: array of values to assign | |
1577 | * | |
1578 | * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status | |
1579 | * into account. | |
1580 | * | |
1581 | * This function should be called from contexts where we cannot sleep, and will | |
1582 | * complain if the GPIO chip functions potentially sleep. | |
1583 | */ | |
3fff99bc RI |
1584 | void gpiod_set_array_value(unsigned int array_size, |
1585 | struct gpio_desc **desc_array, int *value_array) | |
5f424243 RI |
1586 | { |
1587 | if (!desc_array) | |
1588 | return; | |
3fff99bc RI |
1589 | gpiod_set_array_value_priv(false, false, array_size, desc_array, |
1590 | value_array); | |
5f424243 | 1591 | } |
3fff99bc | 1592 | EXPORT_SYMBOL_GPL(gpiod_set_array_value); |
5f424243 | 1593 | |
d2876d08 | 1594 | /** |
79a9becd AC |
1595 | * gpiod_cansleep() - report whether gpio value access may sleep |
1596 | * @desc: gpio to check | |
d2876d08 | 1597 | * |
d2876d08 | 1598 | */ |
79a9becd | 1599 | int gpiod_cansleep(const struct gpio_desc *desc) |
372e722e | 1600 | { |
bcabdef1 AC |
1601 | if (!desc) |
1602 | return 0; | |
372e722e | 1603 | return desc->chip->can_sleep; |
d2876d08 | 1604 | } |
79a9becd | 1605 | EXPORT_SYMBOL_GPL(gpiod_cansleep); |
d2876d08 | 1606 | |
0f6d504e | 1607 | /** |
79a9becd AC |
1608 | * gpiod_to_irq() - return the IRQ corresponding to a GPIO |
1609 | * @desc: gpio whose IRQ will be returned (already requested) | |
0f6d504e | 1610 | * |
79a9becd AC |
1611 | * Return the IRQ corresponding to the passed GPIO, or an error code in case of |
1612 | * error. | |
0f6d504e | 1613 | */ |
79a9becd | 1614 | int gpiod_to_irq(const struct gpio_desc *desc) |
0f6d504e DB |
1615 | { |
1616 | struct gpio_chip *chip; | |
372e722e | 1617 | int offset; |
0f6d504e | 1618 | |
bcabdef1 AC |
1619 | if (!desc) |
1620 | return -EINVAL; | |
372e722e AC |
1621 | chip = desc->chip; |
1622 | offset = gpio_chip_hwgpio(desc); | |
1623 | return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO; | |
0f6d504e | 1624 | } |
79a9becd | 1625 | EXPORT_SYMBOL_GPL(gpiod_to_irq); |
0f6d504e | 1626 | |
d468bf9e | 1627 | /** |
e3a2e878 | 1628 | * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ |
d74be6df AC |
1629 | * @chip: the chip the GPIO to lock belongs to |
1630 | * @offset: the offset of the GPIO to lock as IRQ | |
d468bf9e LW |
1631 | * |
1632 | * This is used directly by GPIO drivers that want to lock down | |
f438acdf | 1633 | * a certain GPIO line to be used for IRQs. |
d468bf9e | 1634 | */ |
e3a2e878 | 1635 | int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset) |
372e722e | 1636 | { |
d74be6df | 1637 | if (offset >= chip->ngpio) |
d468bf9e LW |
1638 | return -EINVAL; |
1639 | ||
d74be6df AC |
1640 | if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) { |
1641 | chip_err(chip, | |
d468bf9e LW |
1642 | "%s: tried to flag a GPIO set as output for IRQ\n", |
1643 | __func__); | |
1644 | return -EIO; | |
1645 | } | |
1646 | ||
d74be6df | 1647 | set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags); |
d468bf9e | 1648 | return 0; |
372e722e | 1649 | } |
e3a2e878 | 1650 | EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq); |
d2876d08 | 1651 | |
d468bf9e | 1652 | /** |
e3a2e878 | 1653 | * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ |
d74be6df AC |
1654 | * @chip: the chip the GPIO to lock belongs to |
1655 | * @offset: the offset of the GPIO to lock as IRQ | |
d468bf9e LW |
1656 | * |
1657 | * This is used directly by GPIO drivers that want to indicate | |
1658 | * that a certain GPIO is no longer used exclusively for IRQ. | |
d2876d08 | 1659 | */ |
e3a2e878 | 1660 | void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset) |
d468bf9e | 1661 | { |
d74be6df | 1662 | if (offset >= chip->ngpio) |
d468bf9e | 1663 | return; |
d2876d08 | 1664 | |
d74be6df | 1665 | clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags); |
d468bf9e | 1666 | } |
e3a2e878 | 1667 | EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq); |
d468bf9e | 1668 | |
79a9becd AC |
1669 | /** |
1670 | * gpiod_get_raw_value_cansleep() - return a gpio's raw value | |
1671 | * @desc: gpio whose value will be returned | |
1672 | * | |
1673 | * Return the GPIO's raw value, i.e. the value of the physical line disregarding | |
e20538b8 | 1674 | * its ACTIVE_LOW status, or negative errno on failure. |
79a9becd AC |
1675 | * |
1676 | * This function is to be called from contexts that can sleep. | |
d2876d08 | 1677 | */ |
79a9becd | 1678 | int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) |
d2876d08 | 1679 | { |
d2876d08 | 1680 | might_sleep_if(extra_checks); |
bcabdef1 AC |
1681 | if (!desc) |
1682 | return 0; | |
79a9becd | 1683 | return _gpiod_get_raw_value(desc); |
d2876d08 | 1684 | } |
79a9becd | 1685 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep); |
372e722e | 1686 | |
79a9becd AC |
1687 | /** |
1688 | * gpiod_get_value_cansleep() - return a gpio's value | |
1689 | * @desc: gpio whose value will be returned | |
1690 | * | |
1691 | * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into | |
e20538b8 | 1692 | * account, or negative errno on failure. |
79a9becd AC |
1693 | * |
1694 | * This function is to be called from contexts that can sleep. | |
1695 | */ | |
1696 | int gpiod_get_value_cansleep(const struct gpio_desc *desc) | |
d2876d08 | 1697 | { |
3f397c21 | 1698 | int value; |
d2876d08 DB |
1699 | |
1700 | might_sleep_if(extra_checks); | |
bcabdef1 AC |
1701 | if (!desc) |
1702 | return 0; | |
79a9becd AC |
1703 | |
1704 | value = _gpiod_get_raw_value(desc); | |
e20538b8 BA |
1705 | if (value < 0) |
1706 | return value; | |
1707 | ||
79a9becd AC |
1708 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
1709 | value = !value; | |
1710 | ||
3f397c21 | 1711 | return value; |
d2876d08 | 1712 | } |
79a9becd | 1713 | EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep); |
372e722e | 1714 | |
79a9becd AC |
1715 | /** |
1716 | * gpiod_set_raw_value_cansleep() - assign a gpio's raw value | |
1717 | * @desc: gpio whose value will be assigned | |
1718 | * @value: value to assign | |
1719 | * | |
1720 | * Set the raw value of the GPIO, i.e. the value of its physical line without | |
1721 | * regard for its ACTIVE_LOW status. | |
1722 | * | |
1723 | * This function is to be called from contexts that can sleep. | |
1724 | */ | |
1725 | void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value) | |
372e722e | 1726 | { |
d2876d08 | 1727 | might_sleep_if(extra_checks); |
bcabdef1 AC |
1728 | if (!desc) |
1729 | return; | |
79a9becd | 1730 | _gpiod_set_raw_value(desc, value); |
372e722e | 1731 | } |
79a9becd | 1732 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep); |
d2876d08 | 1733 | |
79a9becd AC |
1734 | /** |
1735 | * gpiod_set_value_cansleep() - assign a gpio's value | |
1736 | * @desc: gpio whose value will be assigned | |
1737 | * @value: value to assign | |
1738 | * | |
1739 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into | |
1740 | * account | |
1741 | * | |
1742 | * This function is to be called from contexts that can sleep. | |
1743 | */ | |
1744 | void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) | |
d2876d08 | 1745 | { |
d2876d08 | 1746 | might_sleep_if(extra_checks); |
bcabdef1 AC |
1747 | if (!desc) |
1748 | return; | |
79a9becd AC |
1749 | |
1750 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | |
1751 | value = !value; | |
1752 | _gpiod_set_raw_value(desc, value); | |
372e722e | 1753 | } |
79a9becd | 1754 | EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep); |
d2876d08 | 1755 | |
5f424243 | 1756 | /** |
3fff99bc | 1757 | * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs |
5f424243 RI |
1758 | * @array_size: number of elements in the descriptor / value arrays |
1759 | * @desc_array: array of GPIO descriptors whose values will be assigned | |
1760 | * @value_array: array of values to assign | |
1761 | * | |
1762 | * Set the raw values of the GPIOs, i.e. the values of the physical lines | |
1763 | * without regard for their ACTIVE_LOW status. | |
1764 | * | |
1765 | * This function is to be called from contexts that can sleep. | |
1766 | */ | |
3fff99bc RI |
1767 | void gpiod_set_raw_array_value_cansleep(unsigned int array_size, |
1768 | struct gpio_desc **desc_array, | |
1769 | int *value_array) | |
5f424243 RI |
1770 | { |
1771 | might_sleep_if(extra_checks); | |
1772 | if (!desc_array) | |
1773 | return; | |
3fff99bc RI |
1774 | gpiod_set_array_value_priv(true, true, array_size, desc_array, |
1775 | value_array); | |
5f424243 | 1776 | } |
3fff99bc | 1777 | EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep); |
5f424243 RI |
1778 | |
1779 | /** | |
3fff99bc | 1780 | * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs |
5f424243 RI |
1781 | * @array_size: number of elements in the descriptor / value arrays |
1782 | * @desc_array: array of GPIO descriptors whose values will be assigned | |
1783 | * @value_array: array of values to assign | |
1784 | * | |
1785 | * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status | |
1786 | * into account. | |
1787 | * | |
1788 | * This function is to be called from contexts that can sleep. | |
1789 | */ | |
3fff99bc RI |
1790 | void gpiod_set_array_value_cansleep(unsigned int array_size, |
1791 | struct gpio_desc **desc_array, | |
1792 | int *value_array) | |
5f424243 RI |
1793 | { |
1794 | might_sleep_if(extra_checks); | |
1795 | if (!desc_array) | |
1796 | return; | |
3fff99bc RI |
1797 | gpiod_set_array_value_priv(false, true, array_size, desc_array, |
1798 | value_array); | |
5f424243 | 1799 | } |
3fff99bc | 1800 | EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep); |
5f424243 | 1801 | |
bae48da2 | 1802 | /** |
ad824783 AC |
1803 | * gpiod_add_lookup_table() - register GPIO device consumers |
1804 | * @table: table of consumers to register | |
bae48da2 | 1805 | */ |
ad824783 | 1806 | void gpiod_add_lookup_table(struct gpiod_lookup_table *table) |
bae48da2 AC |
1807 | { |
1808 | mutex_lock(&gpio_lookup_lock); | |
1809 | ||
ad824783 | 1810 | list_add_tail(&table->list, &gpio_lookup_list); |
bae48da2 AC |
1811 | |
1812 | mutex_unlock(&gpio_lookup_lock); | |
1813 | } | |
1814 | ||
be9015ab SK |
1815 | /** |
1816 | * gpiod_remove_lookup_table() - unregister GPIO device consumers | |
1817 | * @table: table of consumers to unregister | |
1818 | */ | |
1819 | void gpiod_remove_lookup_table(struct gpiod_lookup_table *table) | |
1820 | { | |
1821 | mutex_lock(&gpio_lookup_lock); | |
1822 | ||
1823 | list_del(&table->list); | |
1824 | ||
1825 | mutex_unlock(&gpio_lookup_lock); | |
1826 | } | |
1827 | ||
bae48da2 | 1828 | static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id, |
53e7cac3 AC |
1829 | unsigned int idx, |
1830 | enum gpio_lookup_flags *flags) | |
bae48da2 AC |
1831 | { |
1832 | char prop_name[32]; /* 32 is max size of property name */ | |
1833 | enum of_gpio_flags of_flags; | |
1834 | struct gpio_desc *desc; | |
dd34c37a | 1835 | unsigned int i; |
bae48da2 | 1836 | |
7f2e553a | 1837 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { |
dd34c37a | 1838 | if (con_id) |
9e089246 | 1839 | snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id, |
7f2e553a | 1840 | gpio_suffixes[i]); |
dd34c37a | 1841 | else |
9e089246 | 1842 | snprintf(prop_name, sizeof(prop_name), "%s", |
7f2e553a | 1843 | gpio_suffixes[i]); |
bae48da2 | 1844 | |
dd34c37a TR |
1845 | desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx, |
1846 | &of_flags); | |
06fc3b70 | 1847 | if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER)) |
dd34c37a TR |
1848 | break; |
1849 | } | |
bae48da2 AC |
1850 | |
1851 | if (IS_ERR(desc)) | |
1852 | return desc; | |
1853 | ||
1854 | if (of_flags & OF_GPIO_ACTIVE_LOW) | |
53e7cac3 | 1855 | *flags |= GPIO_ACTIVE_LOW; |
bae48da2 | 1856 | |
90b665f6 LP |
1857 | if (of_flags & OF_GPIO_SINGLE_ENDED) { |
1858 | if (of_flags & OF_GPIO_ACTIVE_LOW) | |
1859 | *flags |= GPIO_OPEN_DRAIN; | |
1860 | else | |
1861 | *flags |= GPIO_OPEN_SOURCE; | |
1862 | } | |
1863 | ||
bae48da2 AC |
1864 | return desc; |
1865 | } | |
d2876d08 | 1866 | |
81f59e9d | 1867 | static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, |
53e7cac3 AC |
1868 | unsigned int idx, |
1869 | enum gpio_lookup_flags *flags) | |
372e722e | 1870 | { |
0d9a693c | 1871 | struct acpi_device *adev = ACPI_COMPANION(dev); |
e01f440a MW |
1872 | struct acpi_gpio_info info; |
1873 | struct gpio_desc *desc; | |
0d9a693c MW |
1874 | char propname[32]; |
1875 | int i; | |
e01f440a | 1876 | |
0d9a693c | 1877 | /* Try first from _DSD */ |
7f2e553a | 1878 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { |
0d9a693c MW |
1879 | if (con_id && strcmp(con_id, "gpios")) { |
1880 | snprintf(propname, sizeof(propname), "%s-%s", | |
7f2e553a | 1881 | con_id, gpio_suffixes[i]); |
0d9a693c MW |
1882 | } else { |
1883 | snprintf(propname, sizeof(propname), "%s", | |
7f2e553a | 1884 | gpio_suffixes[i]); |
0d9a693c MW |
1885 | } |
1886 | ||
1887 | desc = acpi_get_gpiod_by_index(adev, propname, idx, &info); | |
1888 | if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER)) | |
1889 | break; | |
1890 | } | |
1891 | ||
1892 | /* Then from plain _CRS GPIOs */ | |
1893 | if (IS_ERR(desc)) { | |
9c3c9bc9 DT |
1894 | if (!acpi_can_fallback_to_crs(adev, con_id)) |
1895 | return ERR_PTR(-ENOENT); | |
1896 | ||
0d9a693c MW |
1897 | desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info); |
1898 | if (IS_ERR(desc)) | |
1899 | return desc; | |
1900 | } | |
e01f440a | 1901 | |
0d9a693c | 1902 | if (info.active_low) |
53e7cac3 | 1903 | *flags |= GPIO_ACTIVE_LOW; |
e01f440a MW |
1904 | |
1905 | return desc; | |
81f59e9d MW |
1906 | } |
1907 | ||
ad824783 | 1908 | static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev) |
bae48da2 AC |
1909 | { |
1910 | const char *dev_id = dev ? dev_name(dev) : NULL; | |
ad824783 | 1911 | struct gpiod_lookup_table *table; |
bae48da2 AC |
1912 | |
1913 | mutex_lock(&gpio_lookup_lock); | |
1914 | ||
ad824783 AC |
1915 | list_for_each_entry(table, &gpio_lookup_list, list) { |
1916 | if (table->dev_id && dev_id) { | |
1917 | /* | |
1918 | * Valid strings on both ends, must be identical to have | |
1919 | * a match | |
1920 | */ | |
1921 | if (!strcmp(table->dev_id, dev_id)) | |
1922 | goto found; | |
1923 | } else { | |
1924 | /* | |
1925 | * One of the pointers is NULL, so both must be to have | |
1926 | * a match | |
1927 | */ | |
1928 | if (dev_id == table->dev_id) | |
1929 | goto found; | |
1930 | } | |
1931 | } | |
1932 | table = NULL; | |
bae48da2 | 1933 | |
ad824783 AC |
1934 | found: |
1935 | mutex_unlock(&gpio_lookup_lock); | |
1936 | return table; | |
1937 | } | |
bae48da2 | 1938 | |
ad824783 AC |
1939 | static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id, |
1940 | unsigned int idx, | |
1941 | enum gpio_lookup_flags *flags) | |
1942 | { | |
2a3cf6a3 | 1943 | struct gpio_desc *desc = ERR_PTR(-ENOENT); |
ad824783 AC |
1944 | struct gpiod_lookup_table *table; |
1945 | struct gpiod_lookup *p; | |
bae48da2 | 1946 | |
ad824783 AC |
1947 | table = gpiod_find_lookup_table(dev); |
1948 | if (!table) | |
1949 | return desc; | |
bae48da2 | 1950 | |
ad824783 AC |
1951 | for (p = &table->table[0]; p->chip_label; p++) { |
1952 | struct gpio_chip *chip; | |
bae48da2 | 1953 | |
ad824783 | 1954 | /* idx must always match exactly */ |
bae48da2 AC |
1955 | if (p->idx != idx) |
1956 | continue; | |
1957 | ||
ad824783 AC |
1958 | /* If the lookup entry has a con_id, require exact match */ |
1959 | if (p->con_id && (!con_id || strcmp(p->con_id, con_id))) | |
1960 | continue; | |
bae48da2 | 1961 | |
ad824783 | 1962 | chip = find_chip_by_name(p->chip_label); |
bae48da2 | 1963 | |
ad824783 | 1964 | if (!chip) { |
2a3cf6a3 AC |
1965 | dev_err(dev, "cannot find GPIO chip %s\n", |
1966 | p->chip_label); | |
1967 | return ERR_PTR(-ENODEV); | |
ad824783 | 1968 | } |
bae48da2 | 1969 | |
ad824783 | 1970 | if (chip->ngpio <= p->chip_hwnum) { |
2a3cf6a3 AC |
1971 | dev_err(dev, |
1972 | "requested GPIO %d is out of range [0..%d] for chip %s\n", | |
1973 | idx, chip->ngpio, chip->label); | |
1974 | return ERR_PTR(-EINVAL); | |
bae48da2 | 1975 | } |
bae48da2 | 1976 | |
bb1e88cc | 1977 | desc = gpiochip_get_desc(chip, p->chip_hwnum); |
ad824783 | 1978 | *flags = p->flags; |
bae48da2 | 1979 | |
2a3cf6a3 | 1980 | return desc; |
bae48da2 AC |
1981 | } |
1982 | ||
bae48da2 AC |
1983 | return desc; |
1984 | } | |
1985 | ||
66858527 RI |
1986 | static int dt_gpio_count(struct device *dev, const char *con_id) |
1987 | { | |
1988 | int ret; | |
1989 | char propname[32]; | |
1990 | unsigned int i; | |
1991 | ||
1992 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { | |
1993 | if (con_id) | |
1994 | snprintf(propname, sizeof(propname), "%s-%s", | |
1995 | con_id, gpio_suffixes[i]); | |
1996 | else | |
1997 | snprintf(propname, sizeof(propname), "%s", | |
1998 | gpio_suffixes[i]); | |
1999 | ||
2000 | ret = of_gpio_named_count(dev->of_node, propname); | |
2001 | if (ret >= 0) | |
2002 | break; | |
2003 | } | |
2004 | return ret; | |
2005 | } | |
2006 | ||
2007 | static int platform_gpio_count(struct device *dev, const char *con_id) | |
2008 | { | |
2009 | struct gpiod_lookup_table *table; | |
2010 | struct gpiod_lookup *p; | |
2011 | unsigned int count = 0; | |
2012 | ||
2013 | table = gpiod_find_lookup_table(dev); | |
2014 | if (!table) | |
2015 | return -ENOENT; | |
2016 | ||
2017 | for (p = &table->table[0]; p->chip_label; p++) { | |
2018 | if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) || | |
2019 | (!con_id && !p->con_id)) | |
2020 | count++; | |
2021 | } | |
2022 | if (!count) | |
2023 | return -ENOENT; | |
2024 | ||
2025 | return count; | |
2026 | } | |
2027 | ||
2028 | /** | |
2029 | * gpiod_count - return the number of GPIOs associated with a device / function | |
2030 | * or -ENOENT if no GPIO has been assigned to the requested function | |
2031 | * @dev: GPIO consumer, can be NULL for system-global GPIOs | |
2032 | * @con_id: function within the GPIO consumer | |
2033 | */ | |
2034 | int gpiod_count(struct device *dev, const char *con_id) | |
2035 | { | |
2036 | int count = -ENOENT; | |
2037 | ||
2038 | if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) | |
2039 | count = dt_gpio_count(dev, con_id); | |
2040 | else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) | |
2041 | count = acpi_gpio_count(dev, con_id); | |
2042 | ||
2043 | if (count < 0) | |
2044 | count = platform_gpio_count(dev, con_id); | |
2045 | ||
2046 | return count; | |
2047 | } | |
2048 | EXPORT_SYMBOL_GPL(gpiod_count); | |
2049 | ||
bae48da2 | 2050 | /** |
0879162f | 2051 | * gpiod_get - obtain a GPIO for a given GPIO function |
ad824783 | 2052 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
bae48da2 | 2053 | * @con_id: function within the GPIO consumer |
39b2bbe3 | 2054 | * @flags: optional GPIO initialization flags |
bae48da2 AC |
2055 | * |
2056 | * Return the GPIO descriptor corresponding to the function con_id of device | |
2a3cf6a3 | 2057 | * dev, -ENOENT if no GPIO has been assigned to the requested function, or |
20a8a968 | 2058 | * another IS_ERR() code if an error occurred while trying to acquire the GPIO. |
bae48da2 | 2059 | */ |
b17d1bf1 | 2060 | struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id, |
39b2bbe3 | 2061 | enum gpiod_flags flags) |
bae48da2 | 2062 | { |
39b2bbe3 | 2063 | return gpiod_get_index(dev, con_id, 0, flags); |
bae48da2 | 2064 | } |
b17d1bf1 | 2065 | EXPORT_SYMBOL_GPL(gpiod_get); |
bae48da2 | 2066 | |
29a1f233 TR |
2067 | /** |
2068 | * gpiod_get_optional - obtain an optional GPIO for a given GPIO function | |
2069 | * @dev: GPIO consumer, can be NULL for system-global GPIOs | |
2070 | * @con_id: function within the GPIO consumer | |
39b2bbe3 | 2071 | * @flags: optional GPIO initialization flags |
29a1f233 TR |
2072 | * |
2073 | * This is equivalent to gpiod_get(), except that when no GPIO was assigned to | |
2074 | * the requested function it will return NULL. This is convenient for drivers | |
2075 | * that need to handle optional GPIOs. | |
2076 | */ | |
b17d1bf1 | 2077 | struct gpio_desc *__must_check gpiod_get_optional(struct device *dev, |
39b2bbe3 AC |
2078 | const char *con_id, |
2079 | enum gpiod_flags flags) | |
29a1f233 | 2080 | { |
39b2bbe3 | 2081 | return gpiod_get_index_optional(dev, con_id, 0, flags); |
29a1f233 | 2082 | } |
b17d1bf1 | 2083 | EXPORT_SYMBOL_GPL(gpiod_get_optional); |
29a1f233 | 2084 | |
923b93e4 LP |
2085 | /** |
2086 | * gpiod_parse_flags - helper function to parse GPIO lookup flags | |
2087 | * @desc: gpio to be setup | |
2088 | * @lflags: gpio_lookup_flags - returned from of_find_gpio() or | |
2089 | * of_get_gpio_hog() | |
2090 | * | |
2091 | * Set the GPIO descriptor flags based on the given GPIO lookup flags. | |
2092 | */ | |
2093 | static void gpiod_parse_flags(struct gpio_desc *desc, unsigned long lflags) | |
2094 | { | |
2095 | if (lflags & GPIO_ACTIVE_LOW) | |
2096 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); | |
2097 | if (lflags & GPIO_OPEN_DRAIN) | |
2098 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); | |
2099 | if (lflags & GPIO_OPEN_SOURCE) | |
2100 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); | |
2101 | } | |
f625d460 BP |
2102 | |
2103 | /** | |
2104 | * gpiod_configure_flags - helper function to configure a given GPIO | |
2105 | * @desc: gpio whose value will be assigned | |
2106 | * @con_id: function within the GPIO consumer | |
f625d460 BP |
2107 | * @dflags: gpiod_flags - optional GPIO initialization flags |
2108 | * | |
2109 | * Return 0 on success, -ENOENT if no GPIO has been assigned to the | |
2110 | * requested function and/or index, or another IS_ERR() code if an error | |
2111 | * occurred while trying to acquire the GPIO. | |
2112 | */ | |
2113 | static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, | |
923b93e4 | 2114 | enum gpiod_flags dflags) |
f625d460 BP |
2115 | { |
2116 | int status; | |
2117 | ||
f625d460 BP |
2118 | /* No particular flag request, return here... */ |
2119 | if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) { | |
2120 | pr_debug("no flags found for %s\n", con_id); | |
2121 | return 0; | |
2122 | } | |
2123 | ||
2124 | /* Process flags */ | |
2125 | if (dflags & GPIOD_FLAGS_BIT_DIR_OUT) | |
2126 | status = gpiod_direction_output(desc, | |
2127 | dflags & GPIOD_FLAGS_BIT_DIR_VAL); | |
2128 | else | |
2129 | status = gpiod_direction_input(desc); | |
2130 | ||
2131 | return status; | |
2132 | } | |
2133 | ||
bae48da2 AC |
2134 | /** |
2135 | * gpiod_get_index - obtain a GPIO from a multi-index GPIO function | |
fdd6a5fe | 2136 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
bae48da2 AC |
2137 | * @con_id: function within the GPIO consumer |
2138 | * @idx: index of the GPIO to obtain in the consumer | |
39b2bbe3 | 2139 | * @flags: optional GPIO initialization flags |
bae48da2 AC |
2140 | * |
2141 | * This variant of gpiod_get() allows to access GPIOs other than the first | |
2142 | * defined one for functions that define several GPIOs. | |
2143 | * | |
2a3cf6a3 AC |
2144 | * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the |
2145 | * requested function and/or index, or another IS_ERR() code if an error | |
20a8a968 | 2146 | * occurred while trying to acquire the GPIO. |
bae48da2 | 2147 | */ |
b17d1bf1 | 2148 | struct gpio_desc *__must_check gpiod_get_index(struct device *dev, |
bae48da2 | 2149 | const char *con_id, |
39b2bbe3 AC |
2150 | unsigned int idx, |
2151 | enum gpiod_flags flags) | |
bae48da2 | 2152 | { |
35c5d7fd | 2153 | struct gpio_desc *desc = NULL; |
bae48da2 | 2154 | int status; |
39b2bbe3 | 2155 | enum gpio_lookup_flags lookupflags = 0; |
bae48da2 AC |
2156 | |
2157 | dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id); | |
2158 | ||
4d8440b9 RW |
2159 | if (dev) { |
2160 | /* Using device tree? */ | |
2161 | if (IS_ENABLED(CONFIG_OF) && dev->of_node) { | |
2162 | dev_dbg(dev, "using device tree for GPIO lookup\n"); | |
2163 | desc = of_find_gpio(dev, con_id, idx, &lookupflags); | |
2164 | } else if (ACPI_COMPANION(dev)) { | |
2165 | dev_dbg(dev, "using ACPI for GPIO lookup\n"); | |
2166 | desc = acpi_find_gpio(dev, con_id, idx, &lookupflags); | |
2167 | } | |
35c5d7fd AC |
2168 | } |
2169 | ||
2170 | /* | |
2171 | * Either we are not using DT or ACPI, or their lookup did not return | |
2172 | * a result. In that case, use platform lookup as a fallback. | |
2173 | */ | |
2a3cf6a3 | 2174 | if (!desc || desc == ERR_PTR(-ENOENT)) { |
43a8785a | 2175 | dev_dbg(dev, "using lookup tables for GPIO lookup\n"); |
39b2bbe3 | 2176 | desc = gpiod_find(dev, con_id, idx, &lookupflags); |
bae48da2 AC |
2177 | } |
2178 | ||
2179 | if (IS_ERR(desc)) { | |
351cfe0f | 2180 | dev_dbg(dev, "lookup for GPIO %s failed\n", con_id); |
bae48da2 AC |
2181 | return desc; |
2182 | } | |
2183 | ||
923b93e4 LP |
2184 | gpiod_parse_flags(desc, lookupflags); |
2185 | ||
bae48da2 | 2186 | status = gpiod_request(desc, con_id); |
bae48da2 AC |
2187 | if (status < 0) |
2188 | return ERR_PTR(status); | |
2189 | ||
923b93e4 | 2190 | status = gpiod_configure_flags(desc, con_id, flags); |
39b2bbe3 AC |
2191 | if (status < 0) { |
2192 | dev_dbg(dev, "setup of GPIO %s failed\n", con_id); | |
2193 | gpiod_put(desc); | |
2194 | return ERR_PTR(status); | |
2195 | } | |
2196 | ||
bae48da2 AC |
2197 | return desc; |
2198 | } | |
b17d1bf1 | 2199 | EXPORT_SYMBOL_GPL(gpiod_get_index); |
bae48da2 | 2200 | |
40b73183 MW |
2201 | /** |
2202 | * fwnode_get_named_gpiod - obtain a GPIO from firmware node | |
2203 | * @fwnode: handle of the firmware node | |
2204 | * @propname: name of the firmware property representing the GPIO | |
2205 | * | |
2206 | * This function can be used for drivers that get their configuration | |
2207 | * from firmware. | |
2208 | * | |
2209 | * Function properly finds the corresponding GPIO using whatever is the | |
2210 | * underlying firmware interface and then makes sure that the GPIO | |
2211 | * descriptor is requested before it is returned to the caller. | |
2212 | * | |
2213 | * In case of error an ERR_PTR() is returned. | |
2214 | */ | |
2215 | struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode, | |
2216 | const char *propname) | |
2217 | { | |
2218 | struct gpio_desc *desc = ERR_PTR(-ENODEV); | |
2219 | bool active_low = false; | |
90b665f6 | 2220 | bool single_ended = false; |
40b73183 MW |
2221 | int ret; |
2222 | ||
2223 | if (!fwnode) | |
2224 | return ERR_PTR(-EINVAL); | |
2225 | ||
2226 | if (is_of_node(fwnode)) { | |
2227 | enum of_gpio_flags flags; | |
2228 | ||
c181fb3e | 2229 | desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0, |
40b73183 | 2230 | &flags); |
90b665f6 | 2231 | if (!IS_ERR(desc)) { |
40b73183 | 2232 | active_low = flags & OF_GPIO_ACTIVE_LOW; |
90b665f6 LP |
2233 | single_ended = flags & OF_GPIO_SINGLE_ENDED; |
2234 | } | |
40b73183 MW |
2235 | } else if (is_acpi_node(fwnode)) { |
2236 | struct acpi_gpio_info info; | |
2237 | ||
504a3374 | 2238 | desc = acpi_node_get_gpiod(fwnode, propname, 0, &info); |
40b73183 MW |
2239 | if (!IS_ERR(desc)) |
2240 | active_low = info.active_low; | |
2241 | } | |
2242 | ||
2243 | if (IS_ERR(desc)) | |
2244 | return desc; | |
2245 | ||
40b73183 MW |
2246 | if (active_low) |
2247 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); | |
2248 | ||
90b665f6 LP |
2249 | if (single_ended) { |
2250 | if (active_low) | |
2251 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); | |
2252 | else | |
2253 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); | |
2254 | } | |
2255 | ||
923b93e4 LP |
2256 | ret = gpiod_request(desc, NULL); |
2257 | if (ret) | |
2258 | return ERR_PTR(ret); | |
2259 | ||
40b73183 MW |
2260 | return desc; |
2261 | } | |
2262 | EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod); | |
2263 | ||
29a1f233 TR |
2264 | /** |
2265 | * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO | |
2266 | * function | |
2267 | * @dev: GPIO consumer, can be NULL for system-global GPIOs | |
2268 | * @con_id: function within the GPIO consumer | |
2269 | * @index: index of the GPIO to obtain in the consumer | |
39b2bbe3 | 2270 | * @flags: optional GPIO initialization flags |
29a1f233 TR |
2271 | * |
2272 | * This is equivalent to gpiod_get_index(), except that when no GPIO with the | |
2273 | * specified index was assigned to the requested function it will return NULL. | |
2274 | * This is convenient for drivers that need to handle optional GPIOs. | |
2275 | */ | |
b17d1bf1 | 2276 | struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev, |
29a1f233 | 2277 | const char *con_id, |
39b2bbe3 AC |
2278 | unsigned int index, |
2279 | enum gpiod_flags flags) | |
29a1f233 TR |
2280 | { |
2281 | struct gpio_desc *desc; | |
2282 | ||
39b2bbe3 | 2283 | desc = gpiod_get_index(dev, con_id, index, flags); |
29a1f233 TR |
2284 | if (IS_ERR(desc)) { |
2285 | if (PTR_ERR(desc) == -ENOENT) | |
2286 | return NULL; | |
2287 | } | |
2288 | ||
2289 | return desc; | |
2290 | } | |
b17d1bf1 | 2291 | EXPORT_SYMBOL_GPL(gpiod_get_index_optional); |
29a1f233 | 2292 | |
f625d460 BP |
2293 | /** |
2294 | * gpiod_hog - Hog the specified GPIO desc given the provided flags | |
2295 | * @desc: gpio whose value will be assigned | |
2296 | * @name: gpio line name | |
2297 | * @lflags: gpio_lookup_flags - returned from of_find_gpio() or | |
2298 | * of_get_gpio_hog() | |
2299 | * @dflags: gpiod_flags - optional GPIO initialization flags | |
2300 | */ | |
2301 | int gpiod_hog(struct gpio_desc *desc, const char *name, | |
2302 | unsigned long lflags, enum gpiod_flags dflags) | |
2303 | { | |
2304 | struct gpio_chip *chip; | |
2305 | struct gpio_desc *local_desc; | |
2306 | int hwnum; | |
2307 | int status; | |
2308 | ||
2309 | chip = gpiod_to_chip(desc); | |
2310 | hwnum = gpio_chip_hwgpio(desc); | |
2311 | ||
923b93e4 LP |
2312 | gpiod_parse_flags(desc, lflags); |
2313 | ||
f625d460 BP |
2314 | local_desc = gpiochip_request_own_desc(chip, hwnum, name); |
2315 | if (IS_ERR(local_desc)) { | |
a713890d LW |
2316 | pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n", |
2317 | name, chip->label, hwnum); | |
f625d460 BP |
2318 | return PTR_ERR(local_desc); |
2319 | } | |
2320 | ||
923b93e4 | 2321 | status = gpiod_configure_flags(desc, name, dflags); |
f625d460 | 2322 | if (status < 0) { |
a713890d LW |
2323 | pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n", |
2324 | name, chip->label, hwnum); | |
f625d460 BP |
2325 | gpiochip_free_own_desc(desc); |
2326 | return status; | |
2327 | } | |
2328 | ||
2329 | /* Mark GPIO as hogged so it can be identified and removed later */ | |
2330 | set_bit(FLAG_IS_HOGGED, &desc->flags); | |
2331 | ||
2332 | pr_info("GPIO line %d (%s) hogged as %s%s\n", | |
2333 | desc_to_gpio(desc), name, | |
2334 | (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input", | |
2335 | (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? | |
2336 | (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":""); | |
2337 | ||
2338 | return 0; | |
2339 | } | |
2340 | ||
2341 | /** | |
2342 | * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog | |
2343 | * @chip: gpio chip to act on | |
2344 | * | |
2345 | * This is only used by of_gpiochip_remove to free hogged gpios | |
2346 | */ | |
2347 | static void gpiochip_free_hogs(struct gpio_chip *chip) | |
2348 | { | |
2349 | int id; | |
2350 | ||
2351 | for (id = 0; id < chip->ngpio; id++) { | |
2352 | if (test_bit(FLAG_IS_HOGGED, &chip->desc[id].flags)) | |
2353 | gpiochip_free_own_desc(&chip->desc[id]); | |
2354 | } | |
2355 | } | |
2356 | ||
66858527 RI |
2357 | /** |
2358 | * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function | |
2359 | * @dev: GPIO consumer, can be NULL for system-global GPIOs | |
2360 | * @con_id: function within the GPIO consumer | |
2361 | * @flags: optional GPIO initialization flags | |
2362 | * | |
2363 | * This function acquires all the GPIOs defined under a given function. | |
2364 | * | |
2365 | * Return a struct gpio_descs containing an array of descriptors, -ENOENT if | |
2366 | * no GPIO has been assigned to the requested function, or another IS_ERR() | |
2367 | * code if an error occurred while trying to acquire the GPIOs. | |
2368 | */ | |
2369 | struct gpio_descs *__must_check gpiod_get_array(struct device *dev, | |
2370 | const char *con_id, | |
2371 | enum gpiod_flags flags) | |
2372 | { | |
2373 | struct gpio_desc *desc; | |
2374 | struct gpio_descs *descs; | |
2375 | int count; | |
2376 | ||
2377 | count = gpiod_count(dev, con_id); | |
2378 | if (count < 0) | |
2379 | return ERR_PTR(count); | |
2380 | ||
2381 | descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count, | |
2382 | GFP_KERNEL); | |
2383 | if (!descs) | |
2384 | return ERR_PTR(-ENOMEM); | |
2385 | ||
2386 | for (descs->ndescs = 0; descs->ndescs < count; ) { | |
2387 | desc = gpiod_get_index(dev, con_id, descs->ndescs, flags); | |
2388 | if (IS_ERR(desc)) { | |
2389 | gpiod_put_array(descs); | |
2390 | return ERR_CAST(desc); | |
2391 | } | |
2392 | descs->desc[descs->ndescs] = desc; | |
2393 | descs->ndescs++; | |
2394 | } | |
2395 | return descs; | |
2396 | } | |
2397 | EXPORT_SYMBOL_GPL(gpiod_get_array); | |
2398 | ||
2399 | /** | |
2400 | * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO | |
2401 | * function | |
2402 | * @dev: GPIO consumer, can be NULL for system-global GPIOs | |
2403 | * @con_id: function within the GPIO consumer | |
2404 | * @flags: optional GPIO initialization flags | |
2405 | * | |
2406 | * This is equivalent to gpiod_get_array(), except that when no GPIO was | |
2407 | * assigned to the requested function it will return NULL. | |
2408 | */ | |
2409 | struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev, | |
2410 | const char *con_id, | |
2411 | enum gpiod_flags flags) | |
2412 | { | |
2413 | struct gpio_descs *descs; | |
2414 | ||
2415 | descs = gpiod_get_array(dev, con_id, flags); | |
2416 | if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT)) | |
2417 | return NULL; | |
2418 | ||
2419 | return descs; | |
2420 | } | |
2421 | EXPORT_SYMBOL_GPL(gpiod_get_array_optional); | |
2422 | ||
bae48da2 AC |
2423 | /** |
2424 | * gpiod_put - dispose of a GPIO descriptor | |
2425 | * @desc: GPIO descriptor to dispose of | |
2426 | * | |
2427 | * No descriptor can be used after gpiod_put() has been called on it. | |
2428 | */ | |
2429 | void gpiod_put(struct gpio_desc *desc) | |
2430 | { | |
2431 | gpiod_free(desc); | |
372e722e | 2432 | } |
bae48da2 | 2433 | EXPORT_SYMBOL_GPL(gpiod_put); |
d2876d08 | 2434 | |
66858527 RI |
2435 | /** |
2436 | * gpiod_put_array - dispose of multiple GPIO descriptors | |
2437 | * @descs: struct gpio_descs containing an array of descriptors | |
2438 | */ | |
2439 | void gpiod_put_array(struct gpio_descs *descs) | |
2440 | { | |
2441 | unsigned int i; | |
2442 | ||
2443 | for (i = 0; i < descs->ndescs; i++) | |
2444 | gpiod_put(descs->desc[i]); | |
2445 | ||
2446 | kfree(descs); | |
2447 | } | |
2448 | EXPORT_SYMBOL_GPL(gpiod_put_array); | |
2449 | ||
d2876d08 DB |
2450 | #ifdef CONFIG_DEBUG_FS |
2451 | ||
d2876d08 DB |
2452 | static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
2453 | { | |
2454 | unsigned i; | |
2455 | unsigned gpio = chip->base; | |
6c0b4e6c | 2456 | struct gpio_desc *gdesc = &chip->desc[0]; |
d2876d08 | 2457 | int is_out; |
d468bf9e | 2458 | int is_irq; |
d2876d08 DB |
2459 | |
2460 | for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) { | |
ced433e2 MP |
2461 | if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) { |
2462 | if (gdesc->name) { | |
2463 | seq_printf(s, " gpio-%-3d (%-20.20s)\n", | |
2464 | gpio, gdesc->name); | |
2465 | } | |
d2876d08 | 2466 | continue; |
ced433e2 | 2467 | } |
d2876d08 | 2468 | |
372e722e | 2469 | gpiod_get_direction(gdesc); |
d2876d08 | 2470 | is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); |
d468bf9e | 2471 | is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags); |
ced433e2 MP |
2472 | seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s", |
2473 | gpio, gdesc->name ? gdesc->name : "", gdesc->label, | |
d2876d08 DB |
2474 | is_out ? "out" : "in ", |
2475 | chip->get | |
2476 | ? (chip->get(chip, i) ? "hi" : "lo") | |
d468bf9e LW |
2477 | : "? ", |
2478 | is_irq ? "IRQ" : " "); | |
d2876d08 DB |
2479 | seq_printf(s, "\n"); |
2480 | } | |
2481 | } | |
2482 | ||
f9c4a31f | 2483 | static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos) |
d2876d08 | 2484 | { |
362432ae | 2485 | unsigned long flags; |
f9c4a31f | 2486 | struct gpio_chip *chip = NULL; |
cb1650d4 | 2487 | loff_t index = *pos; |
d2876d08 | 2488 | |
f9c4a31f | 2489 | s->private = ""; |
d2876d08 | 2490 | |
362432ae | 2491 | spin_lock_irqsave(&gpio_lock, flags); |
cb1650d4 | 2492 | list_for_each_entry(chip, &gpio_chips, list) |
362432ae GL |
2493 | if (index-- == 0) { |
2494 | spin_unlock_irqrestore(&gpio_lock, flags); | |
cb1650d4 | 2495 | return chip; |
f9c4a31f | 2496 | } |
362432ae | 2497 | spin_unlock_irqrestore(&gpio_lock, flags); |
f9c4a31f | 2498 | |
cb1650d4 | 2499 | return NULL; |
f9c4a31f TR |
2500 | } |
2501 | ||
2502 | static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
2503 | { | |
362432ae | 2504 | unsigned long flags; |
f9c4a31f | 2505 | struct gpio_chip *chip = v; |
f9c4a31f TR |
2506 | void *ret = NULL; |
2507 | ||
362432ae | 2508 | spin_lock_irqsave(&gpio_lock, flags); |
cb1650d4 AC |
2509 | if (list_is_last(&chip->list, &gpio_chips)) |
2510 | ret = NULL; | |
2511 | else | |
2512 | ret = list_entry(chip->list.next, struct gpio_chip, list); | |
362432ae | 2513 | spin_unlock_irqrestore(&gpio_lock, flags); |
f9c4a31f TR |
2514 | |
2515 | s->private = "\n"; | |
2516 | ++*pos; | |
2517 | ||
2518 | return ret; | |
2519 | } | |
2520 | ||
2521 | static void gpiolib_seq_stop(struct seq_file *s, void *v) | |
2522 | { | |
2523 | } | |
2524 | ||
2525 | static int gpiolib_seq_show(struct seq_file *s, void *v) | |
2526 | { | |
2527 | struct gpio_chip *chip = v; | |
2528 | struct device *dev; | |
2529 | ||
2530 | seq_printf(s, "%sGPIOs %d-%d", (char *)s->private, | |
2531 | chip->base, chip->base + chip->ngpio - 1); | |
58383c78 | 2532 | dev = chip->parent; |
f9c4a31f TR |
2533 | if (dev) |
2534 | seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus", | |
2535 | dev_name(dev)); | |
2536 | if (chip->label) | |
2537 | seq_printf(s, ", %s", chip->label); | |
2538 | if (chip->can_sleep) | |
2539 | seq_printf(s, ", can sleep"); | |
2540 | seq_printf(s, ":\n"); | |
2541 | ||
2542 | if (chip->dbg_show) | |
2543 | chip->dbg_show(s, chip); | |
2544 | else | |
2545 | gpiolib_dbg_show(s, chip); | |
2546 | ||
d2876d08 DB |
2547 | return 0; |
2548 | } | |
2549 | ||
f9c4a31f TR |
2550 | static const struct seq_operations gpiolib_seq_ops = { |
2551 | .start = gpiolib_seq_start, | |
2552 | .next = gpiolib_seq_next, | |
2553 | .stop = gpiolib_seq_stop, | |
2554 | .show = gpiolib_seq_show, | |
2555 | }; | |
2556 | ||
d2876d08 DB |
2557 | static int gpiolib_open(struct inode *inode, struct file *file) |
2558 | { | |
f9c4a31f | 2559 | return seq_open(file, &gpiolib_seq_ops); |
d2876d08 DB |
2560 | } |
2561 | ||
828c0950 | 2562 | static const struct file_operations gpiolib_operations = { |
f9c4a31f | 2563 | .owner = THIS_MODULE, |
d2876d08 DB |
2564 | .open = gpiolib_open, |
2565 | .read = seq_read, | |
2566 | .llseek = seq_lseek, | |
f9c4a31f | 2567 | .release = seq_release, |
d2876d08 DB |
2568 | }; |
2569 | ||
2570 | static int __init gpiolib_debugfs_init(void) | |
2571 | { | |
2572 | /* /sys/kernel/debug/gpio */ | |
2573 | (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO, | |
2574 | NULL, NULL, &gpiolib_operations); | |
2575 | return 0; | |
2576 | } | |
2577 | subsys_initcall(gpiolib_debugfs_init); | |
2578 | ||
2579 | #endif /* DEBUG_FS */ |