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