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