]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpio/gpiolib.c
gpio: make gpiochip_get_desc() gpiolib-private
[mirror_ubuntu-bionic-kernel.git] / drivers / gpio / gpiolib.c
CommitLineData
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>
d2876d08 17
664e3e5a
MW
18#include "gpiolib.h"
19
3f397c21
UKK
20#define CREATE_TRACE_POINTS
21#include <trace/events/gpio.h>
d2876d08 22
79a9becd 23/* Implementation infrastructure for GPIO interfaces.
d2876d08 24 *
79a9becd
AC
25 * The GPIO programming interface allows for inlining speed-critical
26 * get/set operations for common cases, so that access to SOC-integrated
27 * GPIOs can sometimes cost only an instruction or two per bit.
d2876d08
DB
28 */
29
30
31/* When debugging, extend minimal trust to callers and platform code.
32 * Also emit diagnostic messages that may help initial bringup, when
33 * board setup or driver bugs are most common.
34 *
35 * Otherwise, minimize overhead in what may be bitbanging codepaths.
36 */
37#ifdef DEBUG
38#define extra_checks 1
39#else
40#define extra_checks 0
41#endif
42
43/* gpio_lock prevents conflicts during gpio_desc[] table updates.
44 * While any GPIO is requested, its gpio_chip is not removable;
45 * each GPIO's "requested" flag serves as a lock and refcount.
46 */
0eb4c6c2 47DEFINE_SPINLOCK(gpio_lock);
d2876d08 48
d2876d08
DB
49static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
50
6c0b4e6c
AC
51#define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
52
bae48da2
AC
53static DEFINE_MUTEX(gpio_lookup_lock);
54static LIST_HEAD(gpio_lookup_list);
0eb4c6c2 55LIST_HEAD(gpio_chips);
1a2a99c6 56
d2876d08
DB
57static inline void desc_set_label(struct gpio_desc *d, const char *label)
58{
d2876d08 59 d->label = label;
d2876d08
DB
60}
61
372e722e
AC
62/**
63 * Convert a GPIO number to its descriptor
64 */
79a9becd 65struct gpio_desc *gpio_to_desc(unsigned gpio)
372e722e
AC
66{
67 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
68 return NULL;
69 else
70 return &gpio_desc[gpio];
71}
79a9becd 72EXPORT_SYMBOL_GPL(gpio_to_desc);
372e722e 73
d468bf9e 74/**
bb1e88cc 75 * Get the GPIO descriptor corresponding to the given hw number for this chip.
d468bf9e 76 */
bb1e88cc
AC
77struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
78 u16 hwnum)
d468bf9e 79{
bb1e88cc 80 if (hwnum >= chip->ngpio)
b7d0a28a 81 return ERR_PTR(-EINVAL);
d468bf9e 82
bb1e88cc 83 return &chip->desc[hwnum];
d468bf9e 84}
372e722e
AC
85
86/**
87 * Convert a GPIO descriptor to the integer namespace.
88 * This should disappear in the future but is needed since we still
89 * use GPIO numbers for error messages and sysfs nodes
90 */
79a9becd 91int desc_to_gpio(const struct gpio_desc *desc)
372e722e 92{
8c0fca81 93 return desc - &gpio_desc[0];
372e722e 94}
79a9becd 95EXPORT_SYMBOL_GPL(desc_to_gpio);
372e722e
AC
96
97
d2876d08
DB
98/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
99 * when setting direction, and otherwise illegal. Until board setup code
100 * and drivers use explicit requests everywhere (which won't happen when
101 * those calls have no teeth) we can't avoid autorequesting. This nag
35e8bb51
DB
102 * message should motivate switching to explicit requests... so should
103 * the weaker cleanup after faults, compared to gpio_request().
8a0cecff
DB
104 *
105 * NOTE: the autorequest mechanism is going away; at this point it's
106 * only "legal" in the sense that (old) code using it won't break yet,
107 * but instead only triggers a WARN() stack dump.
d2876d08 108 */
372e722e 109static int gpio_ensure_requested(struct gpio_desc *desc)
d2876d08 110{
8a0cecff 111 const struct gpio_chip *chip = desc->chip;
372e722e 112 const int gpio = desc_to_gpio(desc);
35e8bb51 113
8a0cecff
DB
114 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
115 "autorequest GPIO-%d\n", gpio)) {
35e8bb51 116 if (!try_module_get(chip->owner)) {
7589e59f
AS
117 gpiod_err(desc, "%s: module can't be gotten\n",
118 __func__);
35e8bb51
DB
119 clear_bit(FLAG_REQUESTED, &desc->flags);
120 /* lose */
121 return -EIO;
122 }
d2876d08 123 desc_set_label(desc, "[auto]");
35e8bb51
DB
124 /* caller must chip->request() w/o spinlock */
125 if (chip->request)
126 return 1;
d2876d08 127 }
35e8bb51 128 return 0;
d2876d08
DB
129}
130
79a9becd
AC
131/**
132 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
133 * @desc: descriptor to return the chip of
134 */
135struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
372e722e 136{
bcabdef1 137 return desc ? desc->chip : NULL;
372e722e 138}
79a9becd 139EXPORT_SYMBOL_GPL(gpiod_to_chip);
d2876d08 140
8d0aab2f
AV
141/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
142static int gpiochip_find_base(int ngpio)
143{
83cabe33
AC
144 struct gpio_chip *chip;
145 int base = ARCH_NR_GPIOS - ngpio;
8d0aab2f 146
83cabe33
AC
147 list_for_each_entry_reverse(chip, &gpio_chips, list) {
148 /* found a free space? */
149 if (chip->base + chip->ngpio <= base)
150 break;
151 else
152 /* nope, check the space right before the chip */
153 base = chip->base - ngpio;
8d0aab2f
AV
154 }
155
83cabe33 156 if (gpio_is_valid(base)) {
8d0aab2f 157 pr_debug("%s: found new base at %d\n", __func__, base);
83cabe33
AC
158 return base;
159 } else {
160 pr_err("%s: cannot find free range\n", __func__);
161 return -ENOSPC;
169b6a7a 162 }
169b6a7a
AV
163}
164
79a9becd
AC
165/**
166 * gpiod_get_direction - return the current direction of a GPIO
167 * @desc: GPIO to get the direction of
168 *
169 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
170 *
171 * This function may sleep if gpiod_cansleep() is true.
172 */
173int gpiod_get_direction(const struct gpio_desc *desc)
80b0a602
MN
174{
175 struct gpio_chip *chip;
372e722e 176 unsigned offset;
80b0a602
MN
177 int status = -EINVAL;
178
372e722e
AC
179 chip = gpiod_to_chip(desc);
180 offset = gpio_chip_hwgpio(desc);
80b0a602
MN
181
182 if (!chip->get_direction)
183 return status;
184
372e722e 185 status = chip->get_direction(chip, offset);
80b0a602
MN
186 if (status > 0) {
187 /* GPIOF_DIR_IN, or other positive */
188 status = 1;
def63433
AC
189 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
190 * so it does not affect constness per se */
191 clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
80b0a602
MN
192 }
193 if (status == 0) {
194 /* GPIOF_DIR_OUT */
def63433 195 set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
80b0a602
MN
196 }
197 return status;
198}
79a9becd 199EXPORT_SYMBOL_GPL(gpiod_get_direction);
80b0a602 200
1a989d0f
AC
201/*
202 * Add a new chip to the global chips list, keeping the list of chips sorted
203 * by base order.
204 *
205 * Return -EBUSY if the new chip overlaps with some other chip's integer
206 * space.
207 */
208static int gpiochip_add_to_list(struct gpio_chip *chip)
209{
210 struct list_head *pos = &gpio_chips;
211 struct gpio_chip *_chip;
212 int err = 0;
213
214 /* find where to insert our chip */
215 list_for_each(pos, &gpio_chips) {
216 _chip = list_entry(pos, struct gpio_chip, list);
217 /* shall we insert before _chip? */
218 if (_chip->base >= chip->base + chip->ngpio)
219 break;
220 }
221
222 /* are we stepping on the chip right before? */
223 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
224 _chip = list_entry(pos->prev, struct gpio_chip, list);
225 if (_chip->base + _chip->ngpio > chip->base) {
226 dev_err(chip->dev,
227 "GPIO integer space overlap, cannot add chip\n");
228 err = -EBUSY;
229 }
230 }
231
232 if (!err)
233 list_add_tail(&chip->list, pos);
234
235 return err;
236}
237
d2876d08
DB
238/**
239 * gpiochip_add() - register a gpio_chip
240 * @chip: the chip to register, with chip->base initialized
241 * Context: potentially before irqs or kmalloc will work
242 *
243 * Returns a negative errno if the chip can't be registered, such as
244 * because the chip->base is invalid or already associated with a
245 * different chip. Otherwise it returns zero as a success code.
8d0aab2f 246 *
d8f388d8
DB
247 * When gpiochip_add() is called very early during boot, so that GPIOs
248 * can be freely used, the chip->dev device must be registered before
249 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
250 * for GPIOs will fail rudely.
251 *
8d0aab2f
AV
252 * If chip->base is negative, this requests dynamic assignment of
253 * a range of valid GPIOs.
d2876d08
DB
254 */
255int gpiochip_add(struct gpio_chip *chip)
256{
257 unsigned long flags;
258 int status = 0;
259 unsigned id;
8d0aab2f 260 int base = chip->base;
d2876d08 261
bff5fda9 262 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
8d0aab2f 263 && base >= 0) {
d2876d08
DB
264 status = -EINVAL;
265 goto fail;
266 }
267
268 spin_lock_irqsave(&gpio_lock, flags);
269
8d0aab2f
AV
270 if (base < 0) {
271 base = gpiochip_find_base(chip->ngpio);
272 if (base < 0) {
273 status = base;
d8f388d8 274 goto unlock;
8d0aab2f
AV
275 }
276 chip->base = base;
277 }
278
1a989d0f
AC
279 status = gpiochip_add_to_list(chip);
280
d2876d08 281 if (status == 0) {
6c0b4e6c
AC
282 chip->desc = &gpio_desc[chip->base];
283
284 for (id = 0; id < chip->ngpio; id++) {
285 struct gpio_desc *desc = &chip->desc[id];
286 desc->chip = chip;
d8f388d8
DB
287
288 /* REVISIT: most hardware initializes GPIOs as
289 * inputs (often with pullups enabled) so power
290 * usage is minimized. Linux code should set the
291 * gpio direction first thing; but until it does,
80b0a602 292 * and in case chip->get_direction is not set,
d8f388d8
DB
293 * we may expose the wrong direction in sysfs.
294 */
6c0b4e6c 295 desc->flags = !chip->direction_input
d8f388d8
DB
296 ? (1 << FLAG_IS_OUT)
297 : 0;
d2876d08
DB
298 }
299 }
300
3bae4811
ZG
301 spin_unlock_irqrestore(&gpio_lock, flags);
302
f23f1516
SH
303#ifdef CONFIG_PINCTRL
304 INIT_LIST_HEAD(&chip->pin_ranges);
305#endif
306
391c970c 307 of_gpiochip_add(chip);
664e3e5a 308 acpi_gpiochip_add(chip);
391c970c 309
cedb1881
AV
310 if (status)
311 goto fail;
312
313 status = gpiochip_export(chip);
314 if (status)
315 goto fail;
316
7589e59f 317 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
64842aad
GL
318 chip->base, chip->base + chip->ngpio - 1,
319 chip->label ? : "generic");
320
cedb1881 321 return 0;
3bae4811
ZG
322
323unlock:
324 spin_unlock_irqrestore(&gpio_lock, flags);
d2876d08
DB
325fail:
326 /* failures here can mean systems won't boot... */
7589e59f 327 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
cedb1881
AV
328 chip->base, chip->base + chip->ngpio - 1,
329 chip->label ? : "generic");
d2876d08
DB
330 return status;
331}
332EXPORT_SYMBOL_GPL(gpiochip_add);
333
14250520
LW
334/* Forward-declaration */
335static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
336
d2876d08
DB
337/**
338 * gpiochip_remove() - unregister a gpio_chip
339 * @chip: the chip to unregister
340 *
341 * A gpio_chip with any GPIOs still requested may not be removed.
342 */
343int gpiochip_remove(struct gpio_chip *chip)
344{
345 unsigned long flags;
346 int status = 0;
347 unsigned id;
348
6072b9dc
MW
349 acpi_gpiochip_remove(chip);
350
d2876d08
DB
351 spin_lock_irqsave(&gpio_lock, flags);
352
14250520 353 gpiochip_irqchip_remove(chip);
9ef0d6f7 354 gpiochip_remove_pin_ranges(chip);
391c970c
AV
355 of_gpiochip_remove(chip);
356
6c0b4e6c
AC
357 for (id = 0; id < chip->ngpio; id++) {
358 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) {
d2876d08
DB
359 status = -EBUSY;
360 break;
361 }
362 }
363 if (status == 0) {
6c0b4e6c
AC
364 for (id = 0; id < chip->ngpio; id++)
365 chip->desc[id].chip = NULL;
1a989d0f
AC
366
367 list_del(&chip->list);
d2876d08
DB
368 }
369
370 spin_unlock_irqrestore(&gpio_lock, flags);
d8f388d8
DB
371
372 if (status == 0)
373 gpiochip_unexport(chip);
374
d2876d08
DB
375 return status;
376}
377EXPORT_SYMBOL_GPL(gpiochip_remove);
378
594fa265
GL
379/**
380 * gpiochip_find() - iterator for locating a specific gpio_chip
381 * @data: data to pass to match function
382 * @callback: Callback function to check gpio_chip
383 *
384 * Similar to bus_find_device. It returns a reference to a gpio_chip as
385 * determined by a user supplied @match callback. The callback should return
386 * 0 if the device doesn't match and non-zero if it does. If the callback is
387 * non-zero, this function will return to the caller and not iterate over any
388 * more gpio_chips.
389 */
07ce8ec7 390struct gpio_chip *gpiochip_find(void *data,
6e2cf651 391 int (*match)(struct gpio_chip *chip,
3d0f7cf0 392 void *data))
594fa265 393{
125eef96 394 struct gpio_chip *chip;
594fa265 395 unsigned long flags;
594fa265
GL
396
397 spin_lock_irqsave(&gpio_lock, flags);
125eef96
AC
398 list_for_each_entry(chip, &gpio_chips, list)
399 if (match(chip, data))
594fa265 400 break;
125eef96
AC
401
402 /* No match? */
403 if (&chip->list == &gpio_chips)
404 chip = NULL;
594fa265
GL
405 spin_unlock_irqrestore(&gpio_lock, flags);
406
407 return chip;
408}
8fa0c9bf 409EXPORT_SYMBOL_GPL(gpiochip_find);
d2876d08 410
79697ef9
AC
411static int gpiochip_match_name(struct gpio_chip *chip, void *data)
412{
413 const char *name = data;
414
415 return !strcmp(chip->label, name);
416}
417
418static struct gpio_chip *find_chip_by_name(const char *name)
419{
420 return gpiochip_find((void *)name, gpiochip_match_name);
421}
422
14250520
LW
423#ifdef CONFIG_GPIOLIB_IRQCHIP
424
425/*
426 * The following is irqchip helper code for gpiochips.
427 */
428
429/**
430 * gpiochip_add_chained_irqchip() - adds a chained irqchip to a gpiochip
431 * @gpiochip: the gpiochip to add the irqchip to
432 * @irqchip: the irqchip to add to the gpiochip
433 * @parent_irq: the irq number corresponding to the parent IRQ for this
434 * chained irqchip
435 * @parent_handler: the parent interrupt handler for the accumulated IRQ
436 * coming out of the gpiochip
437 */
438void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
439 struct irq_chip *irqchip,
440 int parent_irq,
441 irq_flow_handler_t parent_handler)
442{
1c8732bb
LW
443 if (gpiochip->can_sleep) {
444 chip_err(gpiochip, "you cannot have chained interrupts on a chip that may sleep\n");
445 return;
446 }
447
14250520
LW
448 irq_set_chained_handler(parent_irq, parent_handler);
449 /*
450 * The parent irqchip is already using the chip_data for this
451 * irqchip, so our callbacks simply use the handler_data.
452 */
453 irq_set_handler_data(parent_irq, gpiochip);
454}
455EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
456
e45d1c80
LW
457/*
458 * This lock class tells lockdep that GPIO irqs are in a different
459 * category than their parents, so it won't report false recursion.
460 */
461static struct lock_class_key gpiochip_irq_lock_class;
462
14250520
LW
463/**
464 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
465 * @d: the irqdomain used by this irqchip
466 * @irq: the global irq number used by this GPIO irqchip irq
467 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
468 *
469 * This function will set up the mapping for a certain IRQ line on a
470 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
471 * stored inside the gpiochip.
472 */
473static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
474 irq_hw_number_t hwirq)
475{
476 struct gpio_chip *chip = d->host_data;
477
14250520 478 irq_set_chip_data(irq, chip);
e45d1c80 479 irq_set_lockdep_class(irq, &gpiochip_irq_lock_class);
7633fb95 480 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
1c8732bb
LW
481 /* Chips that can sleep need nested thread handlers */
482 if (chip->can_sleep)
483 irq_set_nested_thread(irq, 1);
14250520
LW
484#ifdef CONFIG_ARM
485 set_irq_flags(irq, IRQF_VALID);
486#else
487 irq_set_noprobe(irq);
488#endif
1333b90f
LW
489 /*
490 * No set-up of the hardware will happen if IRQ_TYPE_NONE
491 * is passed as default type.
492 */
493 if (chip->irq_default_type != IRQ_TYPE_NONE)
494 irq_set_irq_type(irq, chip->irq_default_type);
14250520
LW
495
496 return 0;
497}
498
c3626fde
LW
499static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
500{
1c8732bb
LW
501 struct gpio_chip *chip = d->host_data;
502
c3626fde
LW
503#ifdef CONFIG_ARM
504 set_irq_flags(irq, 0);
505#endif
1c8732bb
LW
506 if (chip->can_sleep)
507 irq_set_nested_thread(irq, 0);
c3626fde
LW
508 irq_set_chip_and_handler(irq, NULL, NULL);
509 irq_set_chip_data(irq, NULL);
510}
511
14250520
LW
512static const struct irq_domain_ops gpiochip_domain_ops = {
513 .map = gpiochip_irq_map,
c3626fde 514 .unmap = gpiochip_irq_unmap,
14250520
LW
515 /* Virtually all GPIO irqchips are twocell:ed */
516 .xlate = irq_domain_xlate_twocell,
517};
518
519static int gpiochip_irq_reqres(struct irq_data *d)
520{
521 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
522
523 if (gpio_lock_as_irq(chip, d->hwirq)) {
524 chip_err(chip,
525 "unable to lock HW IRQ %lu for IRQ\n",
526 d->hwirq);
527 return -EINVAL;
528 }
529 return 0;
530}
531
532static void gpiochip_irq_relres(struct irq_data *d)
533{
534 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
535
536 gpio_unlock_as_irq(chip, d->hwirq);
537}
538
539static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
540{
541 return irq_find_mapping(chip->irqdomain, offset);
542}
543
544/**
545 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
546 * @gpiochip: the gpiochip to remove the irqchip from
547 *
548 * This is called only from gpiochip_remove()
549 */
550static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
551{
c3626fde
LW
552 unsigned int offset;
553
554 /* Remove all IRQ mappings and delete the domain */
555 if (gpiochip->irqdomain) {
556 for (offset = 0; offset < gpiochip->ngpio; offset++)
557 irq_dispose_mapping(gpiochip->irq_base + offset);
14250520 558 irq_domain_remove(gpiochip->irqdomain);
c3626fde 559 }
14250520
LW
560
561 if (gpiochip->irqchip) {
562 gpiochip->irqchip->irq_request_resources = NULL;
563 gpiochip->irqchip->irq_release_resources = NULL;
564 gpiochip->irqchip = NULL;
565 }
566}
567
568/**
569 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
570 * @gpiochip: the gpiochip to add the irqchip to
571 * @irqchip: the irqchip to add to the gpiochip
572 * @first_irq: if not dynamically assigned, the base (first) IRQ to
573 * allocate gpiochip irqs from
574 * @handler: the irq handler to use (often a predefined irq core function)
1333b90f
LW
575 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
576 * to have the core avoid setting up any default type in the hardware.
14250520
LW
577 *
578 * This function closely associates a certain irqchip with a certain
579 * gpiochip, providing an irq domain to translate the local IRQs to
580 * global irqs in the gpiolib core, and making sure that the gpiochip
581 * is passed as chip data to all related functions. Driver callbacks
582 * need to use container_of() to get their local state containers back
583 * from the gpiochip passed as chip data. An irqdomain will be stored
584 * in the gpiochip that shall be used by the driver to handle IRQ number
585 * translation. The gpiochip will need to be initialized and registered
586 * before calling this function.
587 *
c3626fde
LW
588 * This function will handle two cell:ed simple IRQs and assumes all
589 * the pins on the gpiochip can generate a unique IRQ. Everything else
14250520
LW
590 * need to be open coded.
591 */
592int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
593 struct irq_chip *irqchip,
594 unsigned int first_irq,
595 irq_flow_handler_t handler,
596 unsigned int type)
597{
598 struct device_node *of_node;
599 unsigned int offset;
c3626fde 600 unsigned irq_base = 0;
14250520
LW
601
602 if (!gpiochip || !irqchip)
603 return -EINVAL;
604
605 if (!gpiochip->dev) {
606 pr_err("missing gpiochip .dev parent pointer\n");
607 return -EINVAL;
608 }
609 of_node = gpiochip->dev->of_node;
610#ifdef CONFIG_OF_GPIO
611 /*
612 * If the gpiochip has an assigned OF node this takes precendence
613 * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
614 */
615 if (gpiochip->of_node)
616 of_node = gpiochip->of_node;
617#endif
618 gpiochip->irqchip = irqchip;
619 gpiochip->irq_handler = handler;
620 gpiochip->irq_default_type = type;
621 gpiochip->to_irq = gpiochip_to_irq;
622 gpiochip->irqdomain = irq_domain_add_simple(of_node,
623 gpiochip->ngpio, first_irq,
624 &gpiochip_domain_ops, gpiochip);
625 if (!gpiochip->irqdomain) {
626 gpiochip->irqchip = NULL;
627 return -EINVAL;
628 }
629 irqchip->irq_request_resources = gpiochip_irq_reqres;
630 irqchip->irq_release_resources = gpiochip_irq_relres;
631
632 /*
633 * Prepare the mapping since the irqchip shall be orthogonal to
634 * any gpiochip calls. If the first_irq was zero, this is
635 * necessary to allocate descriptors for all IRQs.
636 */
c3626fde
LW
637 for (offset = 0; offset < gpiochip->ngpio; offset++) {
638 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
639 if (offset == 0)
640 /*
641 * Store the base into the gpiochip to be used when
642 * unmapping the irqs.
643 */
644 gpiochip->irq_base = irq_base;
645 }
14250520
LW
646
647 return 0;
648}
649EXPORT_SYMBOL_GPL(gpiochip_irqchip_add);
650
651#else /* CONFIG_GPIOLIB_IRQCHIP */
652
653static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
654
655#endif /* CONFIG_GPIOLIB_IRQCHIP */
656
f23f1516 657#ifdef CONFIG_PINCTRL
165adc9c 658
586a87e6
CR
659/**
660 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
661 * @chip: the gpiochip to add the range for
662 * @pinctrl: the dev_name() of the pin controller to map to
663 * @gpio_offset: the start offset in the current gpio_chip number space
664 * @pin_group: name of the pin group inside the pin controller
665 */
666int gpiochip_add_pingroup_range(struct gpio_chip *chip,
667 struct pinctrl_dev *pctldev,
668 unsigned int gpio_offset, const char *pin_group)
669{
670 struct gpio_pin_range *pin_range;
671 int ret;
672
673 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
674 if (!pin_range) {
1a2a99c6 675 chip_err(chip, "failed to allocate pin ranges\n");
586a87e6
CR
676 return -ENOMEM;
677 }
678
679 /* Use local offset as range ID */
680 pin_range->range.id = gpio_offset;
681 pin_range->range.gc = chip;
682 pin_range->range.name = chip->label;
683 pin_range->range.base = chip->base + gpio_offset;
684 pin_range->pctldev = pctldev;
685
686 ret = pinctrl_get_group_pins(pctldev, pin_group,
687 &pin_range->range.pins,
688 &pin_range->range.npins);
61c6375d
MN
689 if (ret < 0) {
690 kfree(pin_range);
586a87e6 691 return ret;
61c6375d 692 }
586a87e6
CR
693
694 pinctrl_add_gpio_range(pctldev, &pin_range->range);
695
1a2a99c6
AS
696 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
697 gpio_offset, gpio_offset + pin_range->range.npins - 1,
586a87e6
CR
698 pinctrl_dev_get_devname(pctldev), pin_group);
699
700 list_add_tail(&pin_range->node, &chip->pin_ranges);
701
702 return 0;
703}
704EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
705
3f0f8670
LW
706/**
707 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
708 * @chip: the gpiochip to add the range for
709 * @pinctrl_name: the dev_name() of the pin controller to map to
316511c0
LW
710 * @gpio_offset: the start offset in the current gpio_chip number space
711 * @pin_offset: the start offset in the pin controller number space
3f0f8670
LW
712 * @npins: the number of pins from the offset of each pin space (GPIO and
713 * pin controller) to accumulate in this range
714 */
1e63d7b9 715int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
316511c0 716 unsigned int gpio_offset, unsigned int pin_offset,
3f0f8670 717 unsigned int npins)
f23f1516
SH
718{
719 struct gpio_pin_range *pin_range;
b4d4b1f0 720 int ret;
f23f1516 721
3f0f8670 722 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
f23f1516 723 if (!pin_range) {
1a2a99c6 724 chip_err(chip, "failed to allocate pin ranges\n");
1e63d7b9 725 return -ENOMEM;
f23f1516
SH
726 }
727
3f0f8670 728 /* Use local offset as range ID */
316511c0 729 pin_range->range.id = gpio_offset;
3f0f8670 730 pin_range->range.gc = chip;
f23f1516 731 pin_range->range.name = chip->label;
316511c0
LW
732 pin_range->range.base = chip->base + gpio_offset;
733 pin_range->range.pin_base = pin_offset;
f23f1516 734 pin_range->range.npins = npins;
192c369c 735 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
f23f1516 736 &pin_range->range);
8f23ca1a 737 if (IS_ERR(pin_range->pctldev)) {
b4d4b1f0 738 ret = PTR_ERR(pin_range->pctldev);
1a2a99c6 739 chip_err(chip, "could not create pin range\n");
3f0f8670 740 kfree(pin_range);
b4d4b1f0 741 return ret;
3f0f8670 742 }
1a2a99c6
AS
743 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
744 gpio_offset, gpio_offset + npins - 1,
316511c0
LW
745 pinctl_name,
746 pin_offset, pin_offset + npins - 1);
f23f1516
SH
747
748 list_add_tail(&pin_range->node, &chip->pin_ranges);
1e63d7b9
LW
749
750 return 0;
f23f1516 751}
165adc9c 752EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
f23f1516 753
3f0f8670
LW
754/**
755 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
756 * @chip: the chip to remove all the mappings for
757 */
f23f1516
SH
758void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
759{
760 struct gpio_pin_range *pin_range, *tmp;
761
762 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
763 list_del(&pin_range->node);
764 pinctrl_remove_gpio_range(pin_range->pctldev,
765 &pin_range->range);
3f0f8670 766 kfree(pin_range);
f23f1516
SH
767 }
768}
165adc9c
LW
769EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
770
771#endif /* CONFIG_PINCTRL */
f23f1516 772
d2876d08
DB
773/* These "optional" allocation calls help prevent drivers from stomping
774 * on each other, and help provide better diagnostics in debugfs.
775 * They're called even less than the "set direction" calls.
776 */
77c2d792 777static int __gpiod_request(struct gpio_desc *desc, const char *label)
d2876d08 778{
77c2d792
MW
779 struct gpio_chip *chip = desc->chip;
780 int status;
d2876d08
DB
781 unsigned long flags;
782
bcabdef1
AC
783 spin_lock_irqsave(&gpio_lock, flags);
784
d2876d08 785 /* NOTE: gpio_request() can be called in early boot,
35e8bb51 786 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
d2876d08
DB
787 */
788
789 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
790 desc_set_label(desc, label ? : "?");
791 status = 0;
438d8908 792 } else {
d2876d08 793 status = -EBUSY;
7460db56 794 goto done;
35e8bb51
DB
795 }
796
797 if (chip->request) {
798 /* chip->request may sleep */
799 spin_unlock_irqrestore(&gpio_lock, flags);
372e722e 800 status = chip->request(chip, gpio_chip_hwgpio(desc));
35e8bb51
DB
801 spin_lock_irqsave(&gpio_lock, flags);
802
803 if (status < 0) {
804 desc_set_label(desc, NULL);
35e8bb51 805 clear_bit(FLAG_REQUESTED, &desc->flags);
80b0a602 806 goto done;
35e8bb51 807 }
438d8908 808 }
80b0a602
MN
809 if (chip->get_direction) {
810 /* chip->get_direction may sleep */
811 spin_unlock_irqrestore(&gpio_lock, flags);
372e722e 812 gpiod_get_direction(desc);
80b0a602
MN
813 spin_lock_irqsave(&gpio_lock, flags);
814 }
77c2d792
MW
815done:
816 spin_unlock_irqrestore(&gpio_lock, flags);
817 return status;
818}
819
0eb4c6c2 820int gpiod_request(struct gpio_desc *desc, const char *label)
77c2d792
MW
821{
822 int status = -EPROBE_DEFER;
823 struct gpio_chip *chip;
824
825 if (!desc) {
826 pr_warn("%s: invalid GPIO\n", __func__);
827 return -EINVAL;
828 }
829
830 chip = desc->chip;
831 if (!chip)
832 goto done;
833
834 if (try_module_get(chip->owner)) {
835 status = __gpiod_request(desc, label);
836 if (status < 0)
837 module_put(chip->owner);
838 }
839
d2876d08
DB
840done:
841 if (status)
7589e59f 842 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
77c2d792 843
d2876d08
DB
844 return status;
845}
372e722e 846
77c2d792 847static bool __gpiod_free(struct gpio_desc *desc)
d2876d08 848{
77c2d792 849 bool ret = false;
d2876d08 850 unsigned long flags;
35e8bb51 851 struct gpio_chip *chip;
d2876d08 852
3d599d1c
UKK
853 might_sleep();
854
372e722e 855 gpiod_unexport(desc);
d8f388d8 856
d2876d08
DB
857 spin_lock_irqsave(&gpio_lock, flags);
858
35e8bb51
DB
859 chip = desc->chip;
860 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
861 if (chip->free) {
862 spin_unlock_irqrestore(&gpio_lock, flags);
9c4ba946 863 might_sleep_if(chip->can_sleep);
372e722e 864 chip->free(chip, gpio_chip_hwgpio(desc));
35e8bb51
DB
865 spin_lock_irqsave(&gpio_lock, flags);
866 }
d2876d08 867 desc_set_label(desc, NULL);
07697461 868 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
35e8bb51 869 clear_bit(FLAG_REQUESTED, &desc->flags);
aca5ce14 870 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
25553ff0 871 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
77c2d792
MW
872 ret = true;
873 }
d2876d08
DB
874
875 spin_unlock_irqrestore(&gpio_lock, flags);
77c2d792
MW
876 return ret;
877}
878
0eb4c6c2 879void gpiod_free(struct gpio_desc *desc)
77c2d792
MW
880{
881 if (desc && __gpiod_free(desc))
882 module_put(desc->chip->owner);
883 else
884 WARN_ON(extra_checks);
d2876d08 885}
372e722e 886
d2876d08
DB
887/**
888 * gpiochip_is_requested - return string iff signal was requested
889 * @chip: controller managing the signal
890 * @offset: of signal within controller's 0..(ngpio - 1) range
891 *
892 * Returns NULL if the GPIO is not currently requested, else a string.
9c8318ff
AC
893 * The string returned is the label passed to gpio_request(); if none has been
894 * passed it is a meaningless, non-NULL constant.
d2876d08
DB
895 *
896 * This function is for use by GPIO controller drivers. The label can
897 * help with diagnostics, and knowing that the signal is used as a GPIO
898 * can help avoid accidentally multiplexing it to another controller.
899 */
900const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
901{
6c0b4e6c 902 struct gpio_desc *desc;
d2876d08 903
6c0b4e6c 904 if (!GPIO_OFFSET_VALID(chip, offset))
d2876d08 905 return NULL;
6c0b4e6c
AC
906
907 desc = &chip->desc[offset];
908
372e722e 909 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
d2876d08 910 return NULL;
372e722e 911 return desc->label;
d2876d08
DB
912}
913EXPORT_SYMBOL_GPL(gpiochip_is_requested);
914
77c2d792
MW
915/**
916 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
917 * @desc: GPIO descriptor to request
918 * @label: label for the GPIO
919 *
920 * Function allows GPIO chip drivers to request and use their own GPIO
921 * descriptors via gpiolib API. Difference to gpiod_request() is that this
922 * function will not increase reference count of the GPIO chip module. This
923 * allows the GPIO chip module to be unloaded as needed (we assume that the
924 * GPIO chip driver handles freeing the GPIOs it has requested).
925 */
926int gpiochip_request_own_desc(struct gpio_desc *desc, const char *label)
927{
928 if (!desc || !desc->chip)
929 return -EINVAL;
930
931 return __gpiod_request(desc, label);
932}
933
934/**
935 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
936 * @desc: GPIO descriptor to free
937 *
938 * Function frees the given GPIO requested previously with
939 * gpiochip_request_own_desc().
940 */
941void gpiochip_free_own_desc(struct gpio_desc *desc)
942{
943 if (desc)
944 __gpiod_free(desc);
945}
d2876d08
DB
946
947/* Drivers MUST set GPIO direction before making get/set calls. In
948 * some cases this is done in early boot, before IRQs are enabled.
949 *
950 * As a rule these aren't called more than once (except for drivers
951 * using the open-drain emulation idiom) so these are natural places
952 * to accumulate extra debugging checks. Note that we can't (yet)
953 * rely on gpio_request() having been called beforehand.
954 */
955
79a9becd
AC
956/**
957 * gpiod_direction_input - set the GPIO direction to input
958 * @desc: GPIO to set to input
959 *
960 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
961 * be called safely on it.
962 *
963 * Return 0 in case of success, else an error code.
964 */
965int gpiod_direction_input(struct gpio_desc *desc)
d2876d08
DB
966{
967 unsigned long flags;
968 struct gpio_chip *chip;
d2876d08 969 int status = -EINVAL;
372e722e 970 int offset;
d2876d08 971
be1a4b13 972 if (!desc || !desc->chip) {
bcabdef1
AC
973 pr_warn("%s: invalid GPIO\n", __func__);
974 return -EINVAL;
975 }
976
be1a4b13
LW
977 chip = desc->chip;
978 if (!chip->get || !chip->direction_input) {
6424de5a
MB
979 gpiod_warn(desc,
980 "%s: missing get() or direction_input() operations\n",
7589e59f 981 __func__);
be1a4b13
LW
982 return -EIO;
983 }
984
d2876d08
DB
985 spin_lock_irqsave(&gpio_lock, flags);
986
372e722e 987 status = gpio_ensure_requested(desc);
35e8bb51
DB
988 if (status < 0)
989 goto fail;
d2876d08
DB
990
991 /* now we know the gpio is valid and chip won't vanish */
992
993 spin_unlock_irqrestore(&gpio_lock, flags);
994
9c4ba946 995 might_sleep_if(chip->can_sleep);
d2876d08 996
372e722e 997 offset = gpio_chip_hwgpio(desc);
35e8bb51 998 if (status) {
372e722e 999 status = chip->request(chip, offset);
35e8bb51 1000 if (status < 0) {
7589e59f
AS
1001 gpiod_dbg(desc, "%s: chip request fail, %d\n",
1002 __func__, status);
35e8bb51
DB
1003 /* and it's not available to anyone else ...
1004 * gpio_request() is the fully clean solution.
1005 */
1006 goto lose;
1007 }
1008 }
1009
372e722e 1010 status = chip->direction_input(chip, offset);
d2876d08
DB
1011 if (status == 0)
1012 clear_bit(FLAG_IS_OUT, &desc->flags);
3f397c21 1013
372e722e 1014 trace_gpio_direction(desc_to_gpio(desc), 1, status);
35e8bb51 1015lose:
d2876d08
DB
1016 return status;
1017fail:
1018 spin_unlock_irqrestore(&gpio_lock, flags);
bcabdef1 1019 if (status)
7589e59f 1020 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
d2876d08
DB
1021 return status;
1022}
79a9becd 1023EXPORT_SYMBOL_GPL(gpiod_direction_input);
372e722e 1024
ef70bbe1 1025static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
d2876d08
DB
1026{
1027 unsigned long flags;
1028 struct gpio_chip *chip;
d2876d08 1029 int status = -EINVAL;
372e722e 1030 int offset;
d2876d08 1031
d468bf9e
LW
1032 /* GPIOs used for IRQs shall not be set as output */
1033 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1034 gpiod_err(desc,
1035 "%s: tried to set a GPIO tied to an IRQ as output\n",
1036 __func__);
1037 return -EIO;
1038 }
1039
aca5ce14
LD
1040 /* Open drain pin should not be driven to 1 */
1041 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
372e722e 1042 return gpiod_direction_input(desc);
aca5ce14 1043
25553ff0
LD
1044 /* Open source pin should not be driven to 0 */
1045 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
372e722e 1046 return gpiod_direction_input(desc);
25553ff0 1047
be1a4b13
LW
1048 chip = desc->chip;
1049 if (!chip->set || !chip->direction_output) {
6424de5a
MB
1050 gpiod_warn(desc,
1051 "%s: missing set() or direction_output() operations\n",
1052 __func__);
be1a4b13
LW
1053 return -EIO;
1054 }
1055
d2876d08
DB
1056 spin_lock_irqsave(&gpio_lock, flags);
1057
372e722e 1058 status = gpio_ensure_requested(desc);
35e8bb51
DB
1059 if (status < 0)
1060 goto fail;
d2876d08
DB
1061
1062 /* now we know the gpio is valid and chip won't vanish */
1063
1064 spin_unlock_irqrestore(&gpio_lock, flags);
1065
9c4ba946 1066 might_sleep_if(chip->can_sleep);
d2876d08 1067
372e722e 1068 offset = gpio_chip_hwgpio(desc);
35e8bb51 1069 if (status) {
372e722e 1070 status = chip->request(chip, offset);
35e8bb51 1071 if (status < 0) {
7589e59f
AS
1072 gpiod_dbg(desc, "%s: chip request fail, %d\n",
1073 __func__, status);
35e8bb51
DB
1074 /* and it's not available to anyone else ...
1075 * gpio_request() is the fully clean solution.
1076 */
1077 goto lose;
1078 }
1079 }
1080
372e722e 1081 status = chip->direction_output(chip, offset, value);
d2876d08
DB
1082 if (status == 0)
1083 set_bit(FLAG_IS_OUT, &desc->flags);
372e722e
AC
1084 trace_gpio_value(desc_to_gpio(desc), 0, value);
1085 trace_gpio_direction(desc_to_gpio(desc), 0, status);
35e8bb51 1086lose:
d2876d08
DB
1087 return status;
1088fail:
1089 spin_unlock_irqrestore(&gpio_lock, flags);
bcabdef1 1090 if (status)
6424de5a 1091 gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status);
d2876d08
DB
1092 return status;
1093}
ef70bbe1
PZ
1094
1095/**
1096 * gpiod_direction_output_raw - set the GPIO direction to output
1097 * @desc: GPIO to set to output
1098 * @value: initial output value of the GPIO
1099 *
1100 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1101 * be called safely on it. The initial value of the output must be specified
1102 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1103 *
1104 * Return 0 in case of success, else an error code.
1105 */
1106int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1107{
1108 if (!desc || !desc->chip) {
1109 pr_warn("%s: invalid GPIO\n", __func__);
1110 return -EINVAL;
1111 }
1112 return _gpiod_direction_output_raw(desc, value);
1113}
1114EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1115
1116/**
90df4fe0 1117 * gpiod_direction_output - set the GPIO direction to output
ef70bbe1
PZ
1118 * @desc: GPIO to set to output
1119 * @value: initial output value of the GPIO
1120 *
1121 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1122 * be called safely on it. The initial value of the output must be specified
1123 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1124 * account.
1125 *
1126 * Return 0 in case of success, else an error code.
1127 */
1128int gpiod_direction_output(struct gpio_desc *desc, int value)
1129{
1130 if (!desc || !desc->chip) {
1131 pr_warn("%s: invalid GPIO\n", __func__);
1132 return -EINVAL;
1133 }
1134 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1135 value = !value;
1136 return _gpiod_direction_output_raw(desc, value);
1137}
79a9becd 1138EXPORT_SYMBOL_GPL(gpiod_direction_output);
d2876d08 1139
c4b5be98 1140/**
79a9becd 1141 * gpiod_set_debounce - sets @debounce time for a @gpio
c4b5be98
FB
1142 * @gpio: the gpio to set debounce time
1143 * @debounce: debounce time is microseconds
65d87656
LW
1144 *
1145 * returns -ENOTSUPP if the controller does not support setting
1146 * debounce.
c4b5be98 1147 */
79a9becd 1148int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
c4b5be98
FB
1149{
1150 unsigned long flags;
1151 struct gpio_chip *chip;
c4b5be98 1152 int status = -EINVAL;
372e722e 1153 int offset;
c4b5be98 1154
be1a4b13 1155 if (!desc || !desc->chip) {
bcabdef1
AC
1156 pr_warn("%s: invalid GPIO\n", __func__);
1157 return -EINVAL;
1158 }
1159
c4b5be98 1160 chip = desc->chip;
be1a4b13 1161 if (!chip->set || !chip->set_debounce) {
6424de5a
MB
1162 gpiod_dbg(desc,
1163 "%s: missing set() or set_debounce() operations\n",
1164 __func__);
65d87656 1165 return -ENOTSUPP;
be1a4b13
LW
1166 }
1167
1168 spin_lock_irqsave(&gpio_lock, flags);
372e722e
AC
1169
1170 status = gpio_ensure_requested(desc);
c4b5be98
FB
1171 if (status < 0)
1172 goto fail;
1173
1174 /* now we know the gpio is valid and chip won't vanish */
1175
1176 spin_unlock_irqrestore(&gpio_lock, flags);
1177
9c4ba946 1178 might_sleep_if(chip->can_sleep);
c4b5be98 1179
372e722e
AC
1180 offset = gpio_chip_hwgpio(desc);
1181 return chip->set_debounce(chip, offset, debounce);
c4b5be98
FB
1182
1183fail:
1184 spin_unlock_irqrestore(&gpio_lock, flags);
bcabdef1 1185 if (status)
6424de5a 1186 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
c4b5be98
FB
1187
1188 return status;
1189}
79a9becd 1190EXPORT_SYMBOL_GPL(gpiod_set_debounce);
372e722e 1191
79a9becd
AC
1192/**
1193 * gpiod_is_active_low - test whether a GPIO is active-low or not
1194 * @desc: the gpio descriptor to test
1195 *
1196 * Returns 1 if the GPIO is active-low, 0 otherwise.
1197 */
1198int gpiod_is_active_low(const struct gpio_desc *desc)
372e722e 1199{
79a9becd 1200 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
372e722e 1201}
79a9becd 1202EXPORT_SYMBOL_GPL(gpiod_is_active_low);
d2876d08
DB
1203
1204/* I/O calls are only valid after configuration completed; the relevant
1205 * "is this a valid GPIO" error checks should already have been done.
1206 *
1207 * "Get" operations are often inlinable as reading a pin value register,
1208 * and masking the relevant bit in that register.
1209 *
1210 * When "set" operations are inlinable, they involve writing that mask to
1211 * one register to set a low value, or a different register to set it high.
1212 * Otherwise locking is needed, so there may be little value to inlining.
1213 *
1214 *------------------------------------------------------------------------
1215 *
1216 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1217 * have requested the GPIO. That can include implicit requesting by
1218 * a direction setting call. Marking a gpio as requested locks its chip
1219 * in memory, guaranteeing that these table lookups need no more locking
1220 * and that gpiochip_remove() will fail.
1221 *
1222 * REVISIT when debugging, consider adding some instrumentation to ensure
1223 * that the GPIO was actually requested.
1224 */
1225
23600969 1226static bool _gpiod_get_raw_value(const struct gpio_desc *desc)
d2876d08
DB
1227{
1228 struct gpio_chip *chip;
23600969 1229 bool value;
372e722e 1230 int offset;
d2876d08 1231
372e722e
AC
1232 chip = desc->chip;
1233 offset = gpio_chip_hwgpio(desc);
23600969 1234 value = chip->get ? chip->get(chip, offset) : false;
372e722e 1235 trace_gpio_value(desc_to_gpio(desc), 1, value);
3f397c21 1236 return value;
d2876d08 1237}
372e722e 1238
d2876d08 1239/**
79a9becd
AC
1240 * gpiod_get_raw_value() - return a gpio's raw value
1241 * @desc: gpio whose value will be returned
d2876d08 1242 *
79a9becd
AC
1243 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1244 * its ACTIVE_LOW status.
1245 *
1246 * This function should be called from contexts where we cannot sleep, and will
1247 * complain if the GPIO chip functions potentially sleep.
d2876d08 1248 */
79a9becd 1249int gpiod_get_raw_value(const struct gpio_desc *desc)
d2876d08 1250{
bcabdef1
AC
1251 if (!desc)
1252 return 0;
e4e449e8 1253 /* Should be using gpio_get_value_cansleep() */
d8e0ac08 1254 WARN_ON(desc->chip->can_sleep);
79a9becd 1255 return _gpiod_get_raw_value(desc);
d2876d08 1256}
79a9becd 1257EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
372e722e 1258
79a9becd
AC
1259/**
1260 * gpiod_get_value() - return a gpio's value
1261 * @desc: gpio whose value will be returned
1262 *
1263 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1264 * account.
1265 *
1266 * This function should be called from contexts where we cannot sleep, and will
1267 * complain if the GPIO chip functions potentially sleep.
1268 */
1269int gpiod_get_value(const struct gpio_desc *desc)
372e722e 1270{
79a9becd
AC
1271 int value;
1272 if (!desc)
1273 return 0;
1274 /* Should be using gpio_get_value_cansleep() */
1275 WARN_ON(desc->chip->can_sleep);
1276
1277 value = _gpiod_get_raw_value(desc);
1278 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1279 value = !value;
1280
1281 return value;
372e722e 1282}
79a9becd 1283EXPORT_SYMBOL_GPL(gpiod_get_value);
d2876d08 1284
aca5ce14
LD
1285/*
1286 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
79a9becd 1287 * @desc: gpio descriptor whose state need to be set.
aca5ce14
LD
1288 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1289 */
23600969 1290static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
aca5ce14
LD
1291{
1292 int err = 0;
372e722e
AC
1293 struct gpio_chip *chip = desc->chip;
1294 int offset = gpio_chip_hwgpio(desc);
1295
aca5ce14 1296 if (value) {
372e722e 1297 err = chip->direction_input(chip, offset);
aca5ce14 1298 if (!err)
372e722e 1299 clear_bit(FLAG_IS_OUT, &desc->flags);
aca5ce14 1300 } else {
372e722e 1301 err = chip->direction_output(chip, offset, 0);
aca5ce14 1302 if (!err)
372e722e 1303 set_bit(FLAG_IS_OUT, &desc->flags);
aca5ce14 1304 }
372e722e 1305 trace_gpio_direction(desc_to_gpio(desc), value, err);
aca5ce14 1306 if (err < 0)
6424de5a
MB
1307 gpiod_err(desc,
1308 "%s: Error in set_value for open drain err %d\n",
1309 __func__, err);
aca5ce14
LD
1310}
1311
25553ff0 1312/*
79a9becd
AC
1313 * _gpio_set_open_source_value() - Set the open source gpio's value.
1314 * @desc: gpio descriptor whose state need to be set.
25553ff0
LD
1315 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1316 */
23600969 1317static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
25553ff0
LD
1318{
1319 int err = 0;
372e722e
AC
1320 struct gpio_chip *chip = desc->chip;
1321 int offset = gpio_chip_hwgpio(desc);
1322
25553ff0 1323 if (value) {
372e722e 1324 err = chip->direction_output(chip, offset, 1);
25553ff0 1325 if (!err)
372e722e 1326 set_bit(FLAG_IS_OUT, &desc->flags);
25553ff0 1327 } else {
372e722e 1328 err = chip->direction_input(chip, offset);
25553ff0 1329 if (!err)
372e722e 1330 clear_bit(FLAG_IS_OUT, &desc->flags);
25553ff0 1331 }
372e722e 1332 trace_gpio_direction(desc_to_gpio(desc), !value, err);
25553ff0 1333 if (err < 0)
6424de5a
MB
1334 gpiod_err(desc,
1335 "%s: Error in set_value for open source err %d\n",
1336 __func__, err);
25553ff0
LD
1337}
1338
23600969 1339static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
d2876d08
DB
1340{
1341 struct gpio_chip *chip;
1342
372e722e 1343 chip = desc->chip;
372e722e
AC
1344 trace_gpio_value(desc_to_gpio(desc), 0, value);
1345 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1346 _gpio_set_open_drain_value(desc, value);
1347 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1348 _gpio_set_open_source_value(desc, value);
aca5ce14 1349 else
372e722e
AC
1350 chip->set(chip, gpio_chip_hwgpio(desc), value);
1351}
1352
d2876d08 1353/**
79a9becd
AC
1354 * gpiod_set_raw_value() - assign a gpio's raw value
1355 * @desc: gpio whose value will be assigned
d2876d08 1356 * @value: value to assign
d2876d08 1357 *
79a9becd
AC
1358 * Set the raw value of the GPIO, i.e. the value of its physical line without
1359 * regard for its ACTIVE_LOW status.
1360 *
1361 * This function should be called from contexts where we cannot sleep, and will
1362 * complain if the GPIO chip functions potentially sleep.
d2876d08 1363 */
79a9becd 1364void gpiod_set_raw_value(struct gpio_desc *desc, int value)
372e722e 1365{
bcabdef1
AC
1366 if (!desc)
1367 return;
e4e449e8 1368 /* Should be using gpio_set_value_cansleep() */
d8e0ac08 1369 WARN_ON(desc->chip->can_sleep);
79a9becd 1370 _gpiod_set_raw_value(desc, value);
d2876d08 1371}
79a9becd 1372EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
d2876d08
DB
1373
1374/**
79a9becd
AC
1375 * gpiod_set_value() - assign a gpio's value
1376 * @desc: gpio whose value will be assigned
1377 * @value: value to assign
1378 *
1379 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1380 * account
d2876d08 1381 *
79a9becd
AC
1382 * This function should be called from contexts where we cannot sleep, and will
1383 * complain if the GPIO chip functions potentially sleep.
d2876d08 1384 */
79a9becd 1385void gpiod_set_value(struct gpio_desc *desc, int value)
d2876d08 1386{
bcabdef1 1387 if (!desc)
79a9becd
AC
1388 return;
1389 /* Should be using gpio_set_value_cansleep() */
1390 WARN_ON(desc->chip->can_sleep);
1391 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1392 value = !value;
1393 _gpiod_set_raw_value(desc, value);
372e722e 1394}
79a9becd 1395EXPORT_SYMBOL_GPL(gpiod_set_value);
d2876d08 1396
d2876d08 1397/**
79a9becd
AC
1398 * gpiod_cansleep() - report whether gpio value access may sleep
1399 * @desc: gpio to check
d2876d08 1400 *
d2876d08 1401 */
79a9becd 1402int gpiod_cansleep(const struct gpio_desc *desc)
372e722e 1403{
bcabdef1
AC
1404 if (!desc)
1405 return 0;
372e722e 1406 return desc->chip->can_sleep;
d2876d08 1407}
79a9becd 1408EXPORT_SYMBOL_GPL(gpiod_cansleep);
d2876d08 1409
0f6d504e 1410/**
79a9becd
AC
1411 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1412 * @desc: gpio whose IRQ will be returned (already requested)
0f6d504e 1413 *
79a9becd
AC
1414 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1415 * error.
0f6d504e 1416 */
79a9becd 1417int gpiod_to_irq(const struct gpio_desc *desc)
0f6d504e
DB
1418{
1419 struct gpio_chip *chip;
372e722e 1420 int offset;
0f6d504e 1421
bcabdef1
AC
1422 if (!desc)
1423 return -EINVAL;
372e722e
AC
1424 chip = desc->chip;
1425 offset = gpio_chip_hwgpio(desc);
1426 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
0f6d504e 1427}
79a9becd 1428EXPORT_SYMBOL_GPL(gpiod_to_irq);
0f6d504e 1429
d468bf9e
LW
1430/**
1431 * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ
1432 * @gpio: the GPIO line to lock as used for IRQ
1433 *
1434 * This is used directly by GPIO drivers that want to lock down
f438acdf 1435 * a certain GPIO line to be used for IRQs.
d468bf9e 1436 */
79a9becd 1437int gpiod_lock_as_irq(struct gpio_desc *desc)
372e722e 1438{
d468bf9e
LW
1439 if (!desc)
1440 return -EINVAL;
1441
1442 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
1443 gpiod_err(desc,
1444 "%s: tried to flag a GPIO set as output for IRQ\n",
1445 __func__);
1446 return -EIO;
1447 }
1448
1449 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
1450 return 0;
372e722e 1451}
79a9becd 1452EXPORT_SYMBOL_GPL(gpiod_lock_as_irq);
d2876d08 1453
d468bf9e
LW
1454/**
1455 * gpiod_unlock_as_irq() - unlock a GPIO used as IRQ
1456 * @gpio: the GPIO line to unlock from IRQ usage
1457 *
1458 * This is used directly by GPIO drivers that want to indicate
1459 * that a certain GPIO is no longer used exclusively for IRQ.
d2876d08 1460 */
79a9becd 1461void gpiod_unlock_as_irq(struct gpio_desc *desc)
d468bf9e
LW
1462{
1463 if (!desc)
1464 return;
d2876d08 1465
d468bf9e
LW
1466 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
1467}
79a9becd 1468EXPORT_SYMBOL_GPL(gpiod_unlock_as_irq);
d468bf9e 1469
79a9becd
AC
1470/**
1471 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1472 * @desc: gpio whose value will be returned
1473 *
1474 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1475 * its ACTIVE_LOW status.
1476 *
1477 * This function is to be called from contexts that can sleep.
d2876d08 1478 */
79a9becd 1479int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
d2876d08 1480{
d2876d08 1481 might_sleep_if(extra_checks);
bcabdef1
AC
1482 if (!desc)
1483 return 0;
79a9becd 1484 return _gpiod_get_raw_value(desc);
d2876d08 1485}
79a9becd 1486EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
372e722e 1487
79a9becd
AC
1488/**
1489 * gpiod_get_value_cansleep() - return a gpio's value
1490 * @desc: gpio whose value will be returned
1491 *
1492 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1493 * account.
1494 *
1495 * This function is to be called from contexts that can sleep.
1496 */
1497int gpiod_get_value_cansleep(const struct gpio_desc *desc)
d2876d08 1498{
3f397c21 1499 int value;
d2876d08
DB
1500
1501 might_sleep_if(extra_checks);
bcabdef1
AC
1502 if (!desc)
1503 return 0;
79a9becd
AC
1504
1505 value = _gpiod_get_raw_value(desc);
1506 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1507 value = !value;
1508
3f397c21 1509 return value;
d2876d08 1510}
79a9becd 1511EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
372e722e 1512
79a9becd
AC
1513/**
1514 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1515 * @desc: gpio whose value will be assigned
1516 * @value: value to assign
1517 *
1518 * Set the raw value of the GPIO, i.e. the value of its physical line without
1519 * regard for its ACTIVE_LOW status.
1520 *
1521 * This function is to be called from contexts that can sleep.
1522 */
1523void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
372e722e 1524{
d2876d08 1525 might_sleep_if(extra_checks);
bcabdef1
AC
1526 if (!desc)
1527 return;
79a9becd 1528 _gpiod_set_raw_value(desc, value);
372e722e 1529}
79a9becd 1530EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
d2876d08 1531
79a9becd
AC
1532/**
1533 * gpiod_set_value_cansleep() - assign a gpio's value
1534 * @desc: gpio whose value will be assigned
1535 * @value: value to assign
1536 *
1537 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1538 * account
1539 *
1540 * This function is to be called from contexts that can sleep.
1541 */
1542void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
d2876d08 1543{
d2876d08 1544 might_sleep_if(extra_checks);
bcabdef1
AC
1545 if (!desc)
1546 return;
79a9becd
AC
1547
1548 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1549 value = !value;
1550 _gpiod_set_raw_value(desc, value);
372e722e 1551}
79a9becd 1552EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
d2876d08 1553
bae48da2 1554/**
ad824783
AC
1555 * gpiod_add_lookup_table() - register GPIO device consumers
1556 * @table: table of consumers to register
bae48da2 1557 */
ad824783 1558void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
bae48da2
AC
1559{
1560 mutex_lock(&gpio_lookup_lock);
1561
ad824783 1562 list_add_tail(&table->list, &gpio_lookup_list);
bae48da2
AC
1563
1564 mutex_unlock(&gpio_lookup_lock);
1565}
1566
bae48da2 1567static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
53e7cac3
AC
1568 unsigned int idx,
1569 enum gpio_lookup_flags *flags)
bae48da2 1570{
dd34c37a 1571 static const char *suffixes[] = { "gpios", "gpio" };
bae48da2
AC
1572 char prop_name[32]; /* 32 is max size of property name */
1573 enum of_gpio_flags of_flags;
1574 struct gpio_desc *desc;
dd34c37a 1575 unsigned int i;
bae48da2 1576
dd34c37a
TR
1577 for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
1578 if (con_id)
1579 snprintf(prop_name, 32, "%s-%s", con_id, suffixes[i]);
1580 else
1581 snprintf(prop_name, 32, "%s", suffixes[i]);
bae48da2 1582
dd34c37a
TR
1583 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1584 &of_flags);
06fc3b70 1585 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
dd34c37a
TR
1586 break;
1587 }
bae48da2
AC
1588
1589 if (IS_ERR(desc))
1590 return desc;
1591
1592 if (of_flags & OF_GPIO_ACTIVE_LOW)
53e7cac3 1593 *flags |= GPIO_ACTIVE_LOW;
bae48da2
AC
1594
1595 return desc;
1596}
d2876d08 1597
81f59e9d 1598static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
53e7cac3
AC
1599 unsigned int idx,
1600 enum gpio_lookup_flags *flags)
372e722e 1601{
e01f440a
MW
1602 struct acpi_gpio_info info;
1603 struct gpio_desc *desc;
1604
1605 desc = acpi_get_gpiod_by_index(dev, idx, &info);
1606 if (IS_ERR(desc))
1607 return desc;
1608
1609 if (info.gpioint && info.active_low)
53e7cac3 1610 *flags |= GPIO_ACTIVE_LOW;
e01f440a
MW
1611
1612 return desc;
81f59e9d
MW
1613}
1614
ad824783 1615static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
bae48da2
AC
1616{
1617 const char *dev_id = dev ? dev_name(dev) : NULL;
ad824783 1618 struct gpiod_lookup_table *table;
bae48da2
AC
1619
1620 mutex_lock(&gpio_lookup_lock);
1621
ad824783
AC
1622 list_for_each_entry(table, &gpio_lookup_list, list) {
1623 if (table->dev_id && dev_id) {
1624 /*
1625 * Valid strings on both ends, must be identical to have
1626 * a match
1627 */
1628 if (!strcmp(table->dev_id, dev_id))
1629 goto found;
1630 } else {
1631 /*
1632 * One of the pointers is NULL, so both must be to have
1633 * a match
1634 */
1635 if (dev_id == table->dev_id)
1636 goto found;
1637 }
1638 }
1639 table = NULL;
bae48da2 1640
ad824783
AC
1641found:
1642 mutex_unlock(&gpio_lookup_lock);
1643 return table;
1644}
bae48da2 1645
ad824783
AC
1646static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
1647 unsigned int idx,
1648 enum gpio_lookup_flags *flags)
1649{
2a3cf6a3 1650 struct gpio_desc *desc = ERR_PTR(-ENOENT);
ad824783
AC
1651 struct gpiod_lookup_table *table;
1652 struct gpiod_lookup *p;
bae48da2 1653
ad824783
AC
1654 table = gpiod_find_lookup_table(dev);
1655 if (!table)
1656 return desc;
bae48da2 1657
ad824783
AC
1658 for (p = &table->table[0]; p->chip_label; p++) {
1659 struct gpio_chip *chip;
bae48da2 1660
ad824783 1661 /* idx must always match exactly */
bae48da2
AC
1662 if (p->idx != idx)
1663 continue;
1664
ad824783
AC
1665 /* If the lookup entry has a con_id, require exact match */
1666 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1667 continue;
bae48da2 1668
ad824783 1669 chip = find_chip_by_name(p->chip_label);
bae48da2 1670
ad824783 1671 if (!chip) {
2a3cf6a3
AC
1672 dev_err(dev, "cannot find GPIO chip %s\n",
1673 p->chip_label);
1674 return ERR_PTR(-ENODEV);
ad824783 1675 }
bae48da2 1676
ad824783 1677 if (chip->ngpio <= p->chip_hwnum) {
2a3cf6a3
AC
1678 dev_err(dev,
1679 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1680 idx, chip->ngpio, chip->label);
1681 return ERR_PTR(-EINVAL);
bae48da2 1682 }
bae48da2 1683
bb1e88cc 1684 desc = gpiochip_get_desc(chip, p->chip_hwnum);
ad824783 1685 *flags = p->flags;
bae48da2 1686
2a3cf6a3 1687 return desc;
bae48da2
AC
1688 }
1689
bae48da2
AC
1690 return desc;
1691}
1692
1693/**
0879162f 1694 * gpiod_get - obtain a GPIO for a given GPIO function
ad824783 1695 * @dev: GPIO consumer, can be NULL for system-global GPIOs
bae48da2
AC
1696 * @con_id: function within the GPIO consumer
1697 *
1698 * Return the GPIO descriptor corresponding to the function con_id of device
2a3cf6a3
AC
1699 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
1700 * another IS_ERR() code if an error occured while trying to acquire the GPIO.
bae48da2
AC
1701 */
1702struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id)
1703{
1704 return gpiod_get_index(dev, con_id, 0);
1705}
1706EXPORT_SYMBOL_GPL(gpiod_get);
1707
29a1f233
TR
1708/**
1709 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
1710 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1711 * @con_id: function within the GPIO consumer
1712 *
1713 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
1714 * the requested function it will return NULL. This is convenient for drivers
1715 * that need to handle optional GPIOs.
1716 */
1717struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
1718 const char *con_id)
1719{
1720 return gpiod_get_index_optional(dev, con_id, 0);
1721}
1722EXPORT_SYMBOL_GPL(gpiod_get_optional);
1723
bae48da2
AC
1724/**
1725 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
fdd6a5fe 1726 * @dev: GPIO consumer, can be NULL for system-global GPIOs
bae48da2
AC
1727 * @con_id: function within the GPIO consumer
1728 * @idx: index of the GPIO to obtain in the consumer
1729 *
1730 * This variant of gpiod_get() allows to access GPIOs other than the first
1731 * defined one for functions that define several GPIOs.
1732 *
2a3cf6a3
AC
1733 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
1734 * requested function and/or index, or another IS_ERR() code if an error
1735 * occured while trying to acquire the GPIO.
bae48da2
AC
1736 */
1737struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
1738 const char *con_id,
1739 unsigned int idx)
1740{
35c5d7fd 1741 struct gpio_desc *desc = NULL;
bae48da2 1742 int status;
53e7cac3 1743 enum gpio_lookup_flags flags = 0;
bae48da2
AC
1744
1745 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
1746
1747 /* Using device tree? */
1748 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) {
1749 dev_dbg(dev, "using device tree for GPIO lookup\n");
1750 desc = of_find_gpio(dev, con_id, idx, &flags);
81f59e9d
MW
1751 } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) {
1752 dev_dbg(dev, "using ACPI for GPIO lookup\n");
1753 desc = acpi_find_gpio(dev, con_id, idx, &flags);
35c5d7fd
AC
1754 }
1755
1756 /*
1757 * Either we are not using DT or ACPI, or their lookup did not return
1758 * a result. In that case, use platform lookup as a fallback.
1759 */
2a3cf6a3 1760 if (!desc || desc == ERR_PTR(-ENOENT)) {
bae48da2 1761 dev_dbg(dev, "using lookup tables for GPIO lookup");
2a3cf6a3 1762 desc = gpiod_find(dev, con_id, idx, &flags);
bae48da2
AC
1763 }
1764
1765 if (IS_ERR(desc)) {
351cfe0f 1766 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
bae48da2
AC
1767 return desc;
1768 }
1769
1770 status = gpiod_request(desc, con_id);
1771
1772 if (status < 0)
1773 return ERR_PTR(status);
1774
53e7cac3 1775 if (flags & GPIO_ACTIVE_LOW)
bae48da2 1776 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
53e7cac3
AC
1777 if (flags & GPIO_OPEN_DRAIN)
1778 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
1779 if (flags & GPIO_OPEN_SOURCE)
1780 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
bae48da2
AC
1781
1782 return desc;
1783}
1784EXPORT_SYMBOL_GPL(gpiod_get_index);
1785
29a1f233
TR
1786/**
1787 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
1788 * function
1789 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1790 * @con_id: function within the GPIO consumer
1791 * @index: index of the GPIO to obtain in the consumer
1792 *
1793 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
1794 * specified index was assigned to the requested function it will return NULL.
1795 * This is convenient for drivers that need to handle optional GPIOs.
1796 */
1797struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
1798 const char *con_id,
1799 unsigned int index)
1800{
1801 struct gpio_desc *desc;
1802
1803 desc = gpiod_get_index(dev, con_id, index);
1804 if (IS_ERR(desc)) {
1805 if (PTR_ERR(desc) == -ENOENT)
1806 return NULL;
1807 }
1808
1809 return desc;
1810}
1811EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
1812
bae48da2
AC
1813/**
1814 * gpiod_put - dispose of a GPIO descriptor
1815 * @desc: GPIO descriptor to dispose of
1816 *
1817 * No descriptor can be used after gpiod_put() has been called on it.
1818 */
1819void gpiod_put(struct gpio_desc *desc)
1820{
1821 gpiod_free(desc);
372e722e 1822}
bae48da2 1823EXPORT_SYMBOL_GPL(gpiod_put);
d2876d08
DB
1824
1825#ifdef CONFIG_DEBUG_FS
1826
d2876d08
DB
1827static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1828{
1829 unsigned i;
1830 unsigned gpio = chip->base;
6c0b4e6c 1831 struct gpio_desc *gdesc = &chip->desc[0];
d2876d08 1832 int is_out;
d468bf9e 1833 int is_irq;
d2876d08
DB
1834
1835 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1836 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1837 continue;
1838
372e722e 1839 gpiod_get_direction(gdesc);
d2876d08 1840 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
d468bf9e
LW
1841 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
1842 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
d2876d08
DB
1843 gpio, gdesc->label,
1844 is_out ? "out" : "in ",
1845 chip->get
1846 ? (chip->get(chip, i) ? "hi" : "lo")
d468bf9e
LW
1847 : "? ",
1848 is_irq ? "IRQ" : " ");
d2876d08
DB
1849 seq_printf(s, "\n");
1850 }
1851}
1852
f9c4a31f 1853static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
d2876d08 1854{
362432ae 1855 unsigned long flags;
f9c4a31f 1856 struct gpio_chip *chip = NULL;
cb1650d4 1857 loff_t index = *pos;
d2876d08 1858
f9c4a31f 1859 s->private = "";
d2876d08 1860
362432ae 1861 spin_lock_irqsave(&gpio_lock, flags);
cb1650d4 1862 list_for_each_entry(chip, &gpio_chips, list)
362432ae
GL
1863 if (index-- == 0) {
1864 spin_unlock_irqrestore(&gpio_lock, flags);
cb1650d4 1865 return chip;
f9c4a31f 1866 }
362432ae 1867 spin_unlock_irqrestore(&gpio_lock, flags);
f9c4a31f 1868
cb1650d4 1869 return NULL;
f9c4a31f
TR
1870}
1871
1872static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
1873{
362432ae 1874 unsigned long flags;
f9c4a31f 1875 struct gpio_chip *chip = v;
f9c4a31f
TR
1876 void *ret = NULL;
1877
362432ae 1878 spin_lock_irqsave(&gpio_lock, flags);
cb1650d4
AC
1879 if (list_is_last(&chip->list, &gpio_chips))
1880 ret = NULL;
1881 else
1882 ret = list_entry(chip->list.next, struct gpio_chip, list);
362432ae 1883 spin_unlock_irqrestore(&gpio_lock, flags);
f9c4a31f
TR
1884
1885 s->private = "\n";
1886 ++*pos;
1887
1888 return ret;
1889}
1890
1891static void gpiolib_seq_stop(struct seq_file *s, void *v)
1892{
1893}
1894
1895static int gpiolib_seq_show(struct seq_file *s, void *v)
1896{
1897 struct gpio_chip *chip = v;
1898 struct device *dev;
1899
1900 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
1901 chip->base, chip->base + chip->ngpio - 1);
1902 dev = chip->dev;
1903 if (dev)
1904 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
1905 dev_name(dev));
1906 if (chip->label)
1907 seq_printf(s, ", %s", chip->label);
1908 if (chip->can_sleep)
1909 seq_printf(s, ", can sleep");
1910 seq_printf(s, ":\n");
1911
1912 if (chip->dbg_show)
1913 chip->dbg_show(s, chip);
1914 else
1915 gpiolib_dbg_show(s, chip);
1916
d2876d08
DB
1917 return 0;
1918}
1919
f9c4a31f
TR
1920static const struct seq_operations gpiolib_seq_ops = {
1921 .start = gpiolib_seq_start,
1922 .next = gpiolib_seq_next,
1923 .stop = gpiolib_seq_stop,
1924 .show = gpiolib_seq_show,
1925};
1926
d2876d08
DB
1927static int gpiolib_open(struct inode *inode, struct file *file)
1928{
f9c4a31f 1929 return seq_open(file, &gpiolib_seq_ops);
d2876d08
DB
1930}
1931
828c0950 1932static const struct file_operations gpiolib_operations = {
f9c4a31f 1933 .owner = THIS_MODULE,
d2876d08
DB
1934 .open = gpiolib_open,
1935 .read = seq_read,
1936 .llseek = seq_lseek,
f9c4a31f 1937 .release = seq_release,
d2876d08
DB
1938};
1939
1940static int __init gpiolib_debugfs_init(void)
1941{
1942 /* /sys/kernel/debug/gpio */
1943 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1944 NULL, NULL, &gpiolib_operations);
1945 return 0;
1946}
1947subsys_initcall(gpiolib_debugfs_init);
1948
1949#endif /* DEBUG_FS */