patches-5.15.92-rt57
[platform/kernel/linux-rpi.git] / drivers / gpio / gpiolib.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include <linux/bitmap.h>
4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/interrupt.h>
7 #include <linux/irq.h>
8 #include <linux/spinlock.h>
9 #include <linux/list.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/debugfs.h>
13 #include <linux/seq_file.h>
14 #include <linux/gpio.h>
15 #include <linux/idr.h>
16 #include <linux/slab.h>
17 #include <linux/acpi.h>
18 #include <linux/gpio/driver.h>
19 #include <linux/gpio/machine.h>
20 #include <linux/pinctrl/consumer.h>
21 #include <linux/fs.h>
22 #include <linux/compat.h>
23 #include <linux/file.h>
24 #include <uapi/linux/gpio.h>
25
26 #include "gpiolib.h"
27 #include "gpiolib-of.h"
28 #include "gpiolib-acpi.h"
29 #include "gpiolib-cdev.h"
30 #include "gpiolib-sysfs.h"
31
32 #define CREATE_TRACE_POINTS
33 #include <trace/events/gpio.h>
34
35 /* Implementation infrastructure for GPIO interfaces.
36  *
37  * The GPIO programming interface allows for inlining speed-critical
38  * get/set operations for common cases, so that access to SOC-integrated
39  * GPIOs can sometimes cost only an instruction or two per bit.
40  */
41
42
43 /* When debugging, extend minimal trust to callers and platform code.
44  * Also emit diagnostic messages that may help initial bringup, when
45  * board setup or driver bugs are most common.
46  *
47  * Otherwise, minimize overhead in what may be bitbanging codepaths.
48  */
49 #ifdef  DEBUG
50 #define extra_checks    1
51 #else
52 #define extra_checks    0
53 #endif
54
55 #define dont_test_bit(b,d) (0)
56
57 /* Device and char device-related information */
58 static DEFINE_IDA(gpio_ida);
59 static dev_t gpio_devt;
60 #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
61 static int gpio_bus_match(struct device *dev, struct device_driver *drv);
62 static struct bus_type gpio_bus_type = {
63         .name = "gpio",
64         .match = gpio_bus_match,
65 };
66
67 /*
68  * Number of GPIOs to use for the fast path in set array
69  */
70 #define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT
71
72 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
73  * While any GPIO is requested, its gpio_chip is not removable;
74  * each GPIO's "requested" flag serves as a lock and refcount.
75  */
76 DEFINE_SPINLOCK(gpio_lock);
77
78 static DEFINE_MUTEX(gpio_lookup_lock);
79 static LIST_HEAD(gpio_lookup_list);
80 LIST_HEAD(gpio_devices);
81
82 static DEFINE_MUTEX(gpio_machine_hogs_mutex);
83 static LIST_HEAD(gpio_machine_hogs);
84
85 static void gpiochip_free_hogs(struct gpio_chip *gc);
86 static int gpiochip_add_irqchip(struct gpio_chip *gc,
87                                 struct lock_class_key *lock_key,
88                                 struct lock_class_key *request_key);
89 static void gpiochip_irqchip_remove(struct gpio_chip *gc);
90 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc);
91 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc);
92 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc);
93
94 static bool gpiolib_initialized;
95
96 static inline void desc_set_label(struct gpio_desc *d, const char *label)
97 {
98         d->label = label;
99 }
100
101 /**
102  * gpio_to_desc - Convert a GPIO number to its descriptor
103  * @gpio: global GPIO number
104  *
105  * Returns:
106  * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
107  * with the given number exists in the system.
108  */
109 struct gpio_desc *gpio_to_desc(unsigned gpio)
110 {
111         struct gpio_device *gdev;
112         unsigned long flags;
113
114         spin_lock_irqsave(&gpio_lock, flags);
115
116         list_for_each_entry(gdev, &gpio_devices, list) {
117                 if (gdev->base <= gpio &&
118                     gdev->base + gdev->ngpio > gpio) {
119                         spin_unlock_irqrestore(&gpio_lock, flags);
120                         return &gdev->descs[gpio - gdev->base];
121                 }
122         }
123
124         spin_unlock_irqrestore(&gpio_lock, flags);
125
126         if (!gpio_is_valid(gpio))
127                 pr_warn("invalid GPIO %d\n", gpio);
128
129         return NULL;
130 }
131 EXPORT_SYMBOL_GPL(gpio_to_desc);
132
133 /**
134  * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
135  *                     hardware number for this chip
136  * @gc: GPIO chip
137  * @hwnum: hardware number of the GPIO for this chip
138  *
139  * Returns:
140  * A pointer to the GPIO descriptor or ``ERR_PTR(-EINVAL)`` if no GPIO exists
141  * in the given chip for the specified hardware number.
142  */
143 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc,
144                                     unsigned int hwnum)
145 {
146         struct gpio_device *gdev = gc->gpiodev;
147
148         if (hwnum >= gdev->ngpio)
149                 return ERR_PTR(-EINVAL);
150
151         return &gdev->descs[hwnum];
152 }
153 EXPORT_SYMBOL_GPL(gpiochip_get_desc);
154
155 /**
156  * desc_to_gpio - convert a GPIO descriptor to the integer namespace
157  * @desc: GPIO descriptor
158  *
159  * This should disappear in the future but is needed since we still
160  * use GPIO numbers for error messages and sysfs nodes.
161  *
162  * Returns:
163  * The global GPIO number for the GPIO specified by its descriptor.
164  */
165 int desc_to_gpio(const struct gpio_desc *desc)
166 {
167         return desc->gdev->base + (desc - &desc->gdev->descs[0]);
168 }
169 EXPORT_SYMBOL_GPL(desc_to_gpio);
170
171
172 /**
173  * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
174  * @desc:       descriptor to return the chip of
175  */
176 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
177 {
178         if (!desc || !desc->gdev)
179                 return NULL;
180         return desc->gdev->chip;
181 }
182 EXPORT_SYMBOL_GPL(gpiod_to_chip);
183
184 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
185 static int gpiochip_find_base(int ngpio)
186 {
187         struct gpio_device *gdev;
188         int base = ARCH_NR_GPIOS - ngpio;
189
190         list_for_each_entry_reverse(gdev, &gpio_devices, list) {
191                 /* found a free space? */
192                 if (gdev->base + gdev->ngpio <= base)
193                         break;
194                 /* nope, check the space right before the chip */
195                 base = gdev->base - ngpio;
196         }
197
198         if (gpio_is_valid(base)) {
199                 pr_debug("%s: found new base at %d\n", __func__, base);
200                 return base;
201         } else {
202                 pr_err("%s: cannot find free range\n", __func__);
203                 return -ENOSPC;
204         }
205 }
206
207 /**
208  * gpiod_get_direction - return the current direction of a GPIO
209  * @desc:       GPIO to get the direction of
210  *
211  * Returns 0 for output, 1 for input, or an error code in case of error.
212  *
213  * This function may sleep if gpiod_cansleep() is true.
214  */
215 int gpiod_get_direction(struct gpio_desc *desc)
216 {
217         struct gpio_chip *gc;
218         unsigned int offset;
219         int ret;
220
221         gc = gpiod_to_chip(desc);
222         offset = gpio_chip_hwgpio(desc);
223
224         /*
225          * Open drain emulation using input mode may incorrectly report
226          * input here, fix that up.
227          */
228         if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) &&
229             test_bit(FLAG_IS_OUT, &desc->flags))
230                 return 0;
231
232         if (!gc->get_direction)
233                 return -ENOTSUPP;
234
235         ret = gc->get_direction(gc, offset);
236         if (ret < 0)
237                 return ret;
238
239         /* GPIOF_DIR_IN or other positive, otherwise GPIOF_DIR_OUT */
240         if (ret > 0)
241                 ret = 1;
242
243         assign_bit(FLAG_IS_OUT, &desc->flags, !ret);
244
245         return ret;
246 }
247 EXPORT_SYMBOL_GPL(gpiod_get_direction);
248
249 /*
250  * Add a new chip to the global chips list, keeping the list of chips sorted
251  * by range(means [base, base + ngpio - 1]) order.
252  *
253  * Return -EBUSY if the new chip overlaps with some other chip's integer
254  * space.
255  */
256 static int gpiodev_add_to_list(struct gpio_device *gdev)
257 {
258         struct gpio_device *prev, *next;
259
260         if (list_empty(&gpio_devices)) {
261                 /* initial entry in list */
262                 list_add_tail(&gdev->list, &gpio_devices);
263                 return 0;
264         }
265
266         next = list_entry(gpio_devices.next, struct gpio_device, list);
267         if (gdev->base + gdev->ngpio <= next->base) {
268                 /* add before first entry */
269                 list_add(&gdev->list, &gpio_devices);
270                 return 0;
271         }
272
273         prev = list_entry(gpio_devices.prev, struct gpio_device, list);
274         if (prev->base + prev->ngpio <= gdev->base) {
275                 /* add behind last entry */
276                 list_add_tail(&gdev->list, &gpio_devices);
277                 return 0;
278         }
279
280         list_for_each_entry_safe(prev, next, &gpio_devices, list) {
281                 /* at the end of the list */
282                 if (&next->list == &gpio_devices)
283                         break;
284
285                 /* add between prev and next */
286                 if (prev->base + prev->ngpio <= gdev->base
287                                 && gdev->base + gdev->ngpio <= next->base) {
288                         list_add(&gdev->list, &prev->list);
289                         return 0;
290                 }
291         }
292
293         dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
294         return -EBUSY;
295 }
296
297 /*
298  * Convert a GPIO name to its descriptor
299  * Note that there is no guarantee that GPIO names are globally unique!
300  * Hence this function will return, if it exists, a reference to the first GPIO
301  * line found that matches the given name.
302  */
303 static struct gpio_desc *gpio_name_to_desc(const char * const name)
304 {
305         struct gpio_device *gdev;
306         unsigned long flags;
307
308         if (!name)
309                 return NULL;
310
311         spin_lock_irqsave(&gpio_lock, flags);
312
313         list_for_each_entry(gdev, &gpio_devices, list) {
314                 int i;
315
316                 for (i = 0; i != gdev->ngpio; ++i) {
317                         struct gpio_desc *desc = &gdev->descs[i];
318
319                         if (!desc->name)
320                                 continue;
321
322                         if (!strcmp(desc->name, name)) {
323                                 spin_unlock_irqrestore(&gpio_lock, flags);
324                                 return desc;
325                         }
326                 }
327         }
328
329         spin_unlock_irqrestore(&gpio_lock, flags);
330
331         return NULL;
332 }
333
334 /*
335  * Take the names from gc->names and assign them to their GPIO descriptors.
336  * Warn if a name is already used for a GPIO line on a different GPIO chip.
337  *
338  * Note that:
339  *   1. Non-unique names are still accepted,
340  *   2. Name collisions within the same GPIO chip are not reported.
341  */
342 static int gpiochip_set_desc_names(struct gpio_chip *gc)
343 {
344         struct gpio_device *gdev = gc->gpiodev;
345         int i;
346
347         /* First check all names if they are unique */
348         for (i = 0; i != gc->ngpio; ++i) {
349                 struct gpio_desc *gpio;
350
351                 gpio = gpio_name_to_desc(gc->names[i]);
352                 if (gpio)
353                         dev_warn(&gdev->dev,
354                                  "Detected name collision for GPIO name '%s'\n",
355                                  gc->names[i]);
356         }
357
358         /* Then add all names to the GPIO descriptors */
359         for (i = 0; i != gc->ngpio; ++i)
360                 gdev->descs[i].name = gc->names[i];
361
362         return 0;
363 }
364
365 /*
366  * devprop_gpiochip_set_names - Set GPIO line names using device properties
367  * @chip: GPIO chip whose lines should be named, if possible
368  *
369  * Looks for device property "gpio-line-names" and if it exists assigns
370  * GPIO line names for the chip. The memory allocated for the assigned
371  * names belong to the underlying firmware node and should not be released
372  * by the caller.
373  */
374 static int devprop_gpiochip_set_names(struct gpio_chip *chip)
375 {
376         struct gpio_device *gdev = chip->gpiodev;
377         struct fwnode_handle *fwnode = dev_fwnode(&gdev->dev);
378         const char **names;
379         int ret, i;
380         int count;
381
382         count = fwnode_property_string_array_count(fwnode, "gpio-line-names");
383         if (count < 0)
384                 return 0;
385
386         /*
387          * When offset is set in the driver side we assume the driver internally
388          * is using more than one gpiochip per the same device. We have to stop
389          * setting friendly names if the specified ones with 'gpio-line-names'
390          * are less than the offset in the device itself. This means all the
391          * lines are not present for every single pin within all the internal
392          * gpiochips.
393          */
394         if (count <= chip->offset) {
395                 dev_warn(&gdev->dev, "gpio-line-names too short (length %d), cannot map names for the gpiochip at offset %u\n",
396                          count, chip->offset);
397                 return 0;
398         }
399
400         names = kcalloc(count, sizeof(*names), GFP_KERNEL);
401         if (!names)
402                 return -ENOMEM;
403
404         ret = fwnode_property_read_string_array(fwnode, "gpio-line-names",
405                                                 names, count);
406         if (ret < 0) {
407                 dev_warn(&gdev->dev, "failed to read GPIO line names\n");
408                 kfree(names);
409                 return ret;
410         }
411
412         /*
413          * When more that one gpiochip per device is used, 'count' can
414          * contain at most number gpiochips x chip->ngpio. We have to
415          * correctly distribute all defined lines taking into account
416          * chip->offset as starting point from where we will assign
417          * the names to pins from the 'names' array. Since property
418          * 'gpio-line-names' cannot contains gaps, we have to be sure
419          * we only assign those pins that really exists since chip->ngpio
420          * can be different of the chip->offset.
421          */
422         count = (count > chip->offset) ? count - chip->offset : count;
423         if (count > chip->ngpio)
424                 count = chip->ngpio;
425
426         for (i = 0; i < count; i++)
427                 gdev->descs[i].name = names[chip->offset + i];
428
429         kfree(names);
430
431         return 0;
432 }
433
434 static unsigned long *gpiochip_allocate_mask(struct gpio_chip *gc)
435 {
436         unsigned long *p;
437
438         p = bitmap_alloc(gc->ngpio, GFP_KERNEL);
439         if (!p)
440                 return NULL;
441
442         /* Assume by default all GPIOs are valid */
443         bitmap_fill(p, gc->ngpio);
444
445         return p;
446 }
447
448 static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
449 {
450         if (!(of_gpio_need_valid_mask(gc) || gc->init_valid_mask))
451                 return 0;
452
453         gc->valid_mask = gpiochip_allocate_mask(gc);
454         if (!gc->valid_mask)
455                 return -ENOMEM;
456
457         return 0;
458 }
459
460 static int gpiochip_init_valid_mask(struct gpio_chip *gc)
461 {
462         if (gc->init_valid_mask)
463                 return gc->init_valid_mask(gc,
464                                            gc->valid_mask,
465                                            gc->ngpio);
466
467         return 0;
468 }
469
470 static void gpiochip_free_valid_mask(struct gpio_chip *gc)
471 {
472         bitmap_free(gc->valid_mask);
473         gc->valid_mask = NULL;
474 }
475
476 static int gpiochip_add_pin_ranges(struct gpio_chip *gc)
477 {
478         if (gc->add_pin_ranges)
479                 return gc->add_pin_ranges(gc);
480
481         return 0;
482 }
483
484 bool gpiochip_line_is_valid(const struct gpio_chip *gc,
485                                 unsigned int offset)
486 {
487         /* No mask means all valid */
488         if (likely(!gc->valid_mask))
489                 return true;
490         return test_bit(offset, gc->valid_mask);
491 }
492 EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
493
494 static void gpiodevice_release(struct device *dev)
495 {
496         struct gpio_device *gdev = container_of(dev, struct gpio_device, dev);
497         unsigned long flags;
498
499         spin_lock_irqsave(&gpio_lock, flags);
500         list_del(&gdev->list);
501         spin_unlock_irqrestore(&gpio_lock, flags);
502
503         ida_free(&gpio_ida, gdev->id);
504         kfree_const(gdev->label);
505         kfree(gdev->descs);
506         kfree(gdev);
507 }
508
509 #ifdef CONFIG_GPIO_CDEV
510 #define gcdev_register(gdev, devt)      gpiolib_cdev_register((gdev), (devt))
511 #define gcdev_unregister(gdev)          gpiolib_cdev_unregister((gdev))
512 #else
513 /*
514  * gpiolib_cdev_register() indirectly calls device_add(), which is still
515  * required even when cdev is not selected.
516  */
517 #define gcdev_register(gdev, devt)      device_add(&(gdev)->dev)
518 #define gcdev_unregister(gdev)          device_del(&(gdev)->dev)
519 #endif
520
521 static int gpiochip_setup_dev(struct gpio_device *gdev)
522 {
523         int ret;
524
525         ret = gcdev_register(gdev, gpio_devt);
526         if (ret)
527                 return ret;
528
529         /* From this point, the .release() function cleans up gpio_device */
530         gdev->dev.release = gpiodevice_release;
531
532         ret = gpiochip_sysfs_register(gdev);
533         if (ret)
534                 goto err_remove_device;
535
536         dev_dbg(&gdev->dev, "registered GPIOs %d to %d on %s\n", gdev->base,
537                 gdev->base + gdev->ngpio - 1, gdev->chip->label ? : "generic");
538
539         return 0;
540
541 err_remove_device:
542         gcdev_unregister(gdev);
543         return ret;
544 }
545
546 static void gpiochip_machine_hog(struct gpio_chip *gc, struct gpiod_hog *hog)
547 {
548         struct gpio_desc *desc;
549         int rv;
550
551         desc = gpiochip_get_desc(gc, hog->chip_hwnum);
552         if (IS_ERR(desc)) {
553                 chip_err(gc, "%s: unable to get GPIO desc: %ld\n", __func__,
554                          PTR_ERR(desc));
555                 return;
556         }
557
558         if (test_bit(FLAG_IS_HOGGED, &desc->flags))
559                 return;
560
561         rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
562         if (rv)
563                 gpiod_err(desc, "%s: unable to hog GPIO line (%s:%u): %d\n",
564                           __func__, gc->label, hog->chip_hwnum, rv);
565 }
566
567 static void machine_gpiochip_add(struct gpio_chip *gc)
568 {
569         struct gpiod_hog *hog;
570
571         mutex_lock(&gpio_machine_hogs_mutex);
572
573         list_for_each_entry(hog, &gpio_machine_hogs, list) {
574                 if (!strcmp(gc->label, hog->chip_label))
575                         gpiochip_machine_hog(gc, hog);
576         }
577
578         mutex_unlock(&gpio_machine_hogs_mutex);
579 }
580
581 static void gpiochip_setup_devs(void)
582 {
583         struct gpio_device *gdev;
584         int ret;
585
586         list_for_each_entry(gdev, &gpio_devices, list) {
587                 ret = gpiochip_setup_dev(gdev);
588                 if (ret)
589                         dev_err(&gdev->dev,
590                                 "Failed to initialize gpio device (%d)\n", ret);
591         }
592 }
593
594 int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
595                                struct lock_class_key *lock_key,
596                                struct lock_class_key *request_key)
597 {
598         struct fwnode_handle *fwnode = gc->parent ? dev_fwnode(gc->parent) : NULL;
599         struct gpio_device *gdev;
600         unsigned long flags;
601         unsigned int i;
602         u32 ngpios = 0;
603         int base = 0;
604         int ret = 0;
605
606         /*
607          * First: allocate and populate the internal stat container, and
608          * set up the struct device.
609          */
610         gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
611         if (!gdev)
612                 return -ENOMEM;
613         gdev->dev.bus = &gpio_bus_type;
614         gdev->dev.parent = gc->parent;
615         gdev->chip = gc;
616         gc->gpiodev = gdev;
617
618         of_gpio_dev_init(gc, gdev);
619         acpi_gpio_dev_init(gc, gdev);
620
621         /*
622          * Assign fwnode depending on the result of the previous calls,
623          * if none of them succeed, assign it to the parent's one.
624          */
625         gdev->dev.fwnode = dev_fwnode(&gdev->dev) ?: fwnode;
626
627         gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL);
628         if (gdev->id < 0) {
629                 ret = gdev->id;
630                 goto err_free_gdev;
631         }
632
633         ret = dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id);
634         if (ret)
635                 goto err_free_ida;
636
637         device_initialize(&gdev->dev);
638         if (gc->parent && gc->parent->driver)
639                 gdev->owner = gc->parent->driver->owner;
640         else if (gc->owner)
641                 /* TODO: remove chip->owner */
642                 gdev->owner = gc->owner;
643         else
644                 gdev->owner = THIS_MODULE;
645
646         /*
647          * Try the device properties if the driver didn't supply the number
648          * of GPIO lines.
649          */
650         ngpios = gc->ngpio;
651         if (ngpios == 0) {
652                 ret = device_property_read_u32(&gdev->dev, "ngpios", &ngpios);
653                 if (ret == -ENODATA)
654                         /*
655                          * -ENODATA means that there is no property found and
656                          * we want to issue the error message to the user.
657                          * Besides that, we want to return different error code
658                          * to state that supplied value is not valid.
659                          */
660                         ngpios = 0;
661                 else if (ret)
662                         goto err_free_dev_name;
663
664                 gc->ngpio = ngpios;
665         }
666
667         if (gc->ngpio == 0) {
668                 chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
669                 ret = -EINVAL;
670                 goto err_free_dev_name;
671         }
672
673         if (gc->ngpio > FASTPATH_NGPIO)
674                 chip_warn(gc, "line cnt %u is greater than fast path cnt %u\n",
675                           gc->ngpio, FASTPATH_NGPIO);
676
677         gdev->descs = kcalloc(gc->ngpio, sizeof(*gdev->descs), GFP_KERNEL);
678         if (!gdev->descs) {
679                 ret = -ENOMEM;
680                 goto err_free_dev_name;
681         }
682
683         gdev->label = kstrdup_const(gc->label ?: "unknown", GFP_KERNEL);
684         if (!gdev->label) {
685                 ret = -ENOMEM;
686                 goto err_free_descs;
687         }
688
689         gdev->ngpio = gc->ngpio;
690         gdev->data = data;
691
692         spin_lock_irqsave(&gpio_lock, flags);
693
694         /*
695          * TODO: this allocates a Linux GPIO number base in the global
696          * GPIO numberspace for this chip. In the long run we want to
697          * get *rid* of this numberspace and use only descriptors, but
698          * it may be a pipe dream. It will not happen before we get rid
699          * of the sysfs interface anyways.
700          */
701         base = gc->base;
702         if (base < 0) {
703                 base = gpiochip_find_base(gc->ngpio);
704                 if (base < 0) {
705                         spin_unlock_irqrestore(&gpio_lock, flags);
706                         ret = base;
707                         base = 0;
708                         goto err_free_label;
709                 }
710                 /*
711                  * TODO: it should not be necessary to reflect the assigned
712                  * base outside of the GPIO subsystem. Go over drivers and
713                  * see if anyone makes use of this, else drop this and assign
714                  * a poison instead.
715                  */
716                 gc->base = base;
717         }
718         gdev->base = base;
719
720         ret = gpiodev_add_to_list(gdev);
721         if (ret) {
722                 spin_unlock_irqrestore(&gpio_lock, flags);
723                 goto err_free_label;
724         }
725
726         for (i = 0; i < gc->ngpio; i++)
727                 gdev->descs[i].gdev = gdev;
728
729         spin_unlock_irqrestore(&gpio_lock, flags);
730
731         BLOCKING_INIT_NOTIFIER_HEAD(&gdev->notifier);
732         init_rwsem(&gdev->sem);
733
734 #ifdef CONFIG_PINCTRL
735         INIT_LIST_HEAD(&gdev->pin_ranges);
736 #endif
737
738         if (gc->names)
739                 ret = gpiochip_set_desc_names(gc);
740         else
741                 ret = devprop_gpiochip_set_names(gc);
742         if (ret)
743                 goto err_remove_from_list;
744
745         ret = gpiochip_alloc_valid_mask(gc);
746         if (ret)
747                 goto err_remove_from_list;
748
749         ret = of_gpiochip_add(gc);
750         if (ret)
751                 goto err_free_gpiochip_mask;
752
753         ret = gpiochip_init_valid_mask(gc);
754         if (ret)
755                 goto err_remove_of_chip;
756
757         for (i = 0; i < gc->ngpio; i++) {
758                 struct gpio_desc *desc = &gdev->descs[i];
759
760                 if (gc->get_direction && gpiochip_line_is_valid(gc, i)) {
761                         assign_bit(FLAG_IS_OUT,
762                                    &desc->flags, !gc->get_direction(gc, i));
763                 } else {
764                         assign_bit(FLAG_IS_OUT,
765                                    &desc->flags, !gc->direction_input);
766                 }
767         }
768
769         ret = gpiochip_add_pin_ranges(gc);
770         if (ret)
771                 goto err_remove_of_chip;
772
773         acpi_gpiochip_add(gc);
774
775         machine_gpiochip_add(gc);
776
777         ret = gpiochip_irqchip_init_valid_mask(gc);
778         if (ret)
779                 goto err_remove_acpi_chip;
780
781         ret = gpiochip_irqchip_init_hw(gc);
782         if (ret)
783                 goto err_remove_acpi_chip;
784
785         ret = gpiochip_add_irqchip(gc, lock_key, request_key);
786         if (ret)
787                 goto err_remove_irqchip_mask;
788
789         /*
790          * By first adding the chardev, and then adding the device,
791          * we get a device node entry in sysfs under
792          * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
793          * coldplug of device nodes and other udev business.
794          * We can do this only if gpiolib has been initialized.
795          * Otherwise, defer until later.
796          */
797         if (gpiolib_initialized) {
798                 ret = gpiochip_setup_dev(gdev);
799                 if (ret)
800                         goto err_remove_irqchip;
801         }
802         return 0;
803
804 err_remove_irqchip:
805         gpiochip_irqchip_remove(gc);
806 err_remove_irqchip_mask:
807         gpiochip_irqchip_free_valid_mask(gc);
808 err_remove_acpi_chip:
809         acpi_gpiochip_remove(gc);
810 err_remove_of_chip:
811         gpiochip_free_hogs(gc);
812         of_gpiochip_remove(gc);
813 err_free_gpiochip_mask:
814         gpiochip_remove_pin_ranges(gc);
815         gpiochip_free_valid_mask(gc);
816         if (gdev->dev.release) {
817                 /* release() has been registered by gpiochip_setup_dev() */
818                 put_device(&gdev->dev);
819                 goto err_print_message;
820         }
821 err_remove_from_list:
822         spin_lock_irqsave(&gpio_lock, flags);
823         list_del(&gdev->list);
824         spin_unlock_irqrestore(&gpio_lock, flags);
825 err_free_label:
826         kfree_const(gdev->label);
827 err_free_descs:
828         kfree(gdev->descs);
829 err_free_dev_name:
830         kfree(dev_name(&gdev->dev));
831 err_free_ida:
832         ida_free(&gpio_ida, gdev->id);
833 err_free_gdev:
834         kfree(gdev);
835 err_print_message:
836         /* failures here can mean systems won't boot... */
837         if (ret != -EPROBE_DEFER) {
838                 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
839                        base, base + (int)ngpios - 1,
840                        gc->label ? : "generic", ret);
841         }
842         return ret;
843 }
844 EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
845
846 /**
847  * gpiochip_get_data() - get per-subdriver data for the chip
848  * @gc: GPIO chip
849  *
850  * Returns:
851  * The per-subdriver data for the chip.
852  */
853 void *gpiochip_get_data(struct gpio_chip *gc)
854 {
855         return gc->gpiodev->data;
856 }
857 EXPORT_SYMBOL_GPL(gpiochip_get_data);
858
859 /**
860  * gpiochip_remove() - unregister a gpio_chip
861  * @gc: the chip to unregister
862  *
863  * A gpio_chip with any GPIOs still requested may not be removed.
864  */
865 void gpiochip_remove(struct gpio_chip *gc)
866 {
867         struct gpio_device *gdev = gc->gpiodev;
868         unsigned long   flags;
869         unsigned int    i;
870
871         down_write(&gdev->sem);
872
873         /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
874         gpiochip_sysfs_unregister(gdev);
875         gpiochip_free_hogs(gc);
876         /* Numb the device, cancelling all outstanding operations */
877         gdev->chip = NULL;
878         gpiochip_irqchip_remove(gc);
879         acpi_gpiochip_remove(gc);
880         of_gpiochip_remove(gc);
881         gpiochip_remove_pin_ranges(gc);
882         gpiochip_free_valid_mask(gc);
883         /*
884          * We accept no more calls into the driver from this point, so
885          * NULL the driver data pointer
886          */
887         gdev->data = NULL;
888
889         spin_lock_irqsave(&gpio_lock, flags);
890         for (i = 0; i < gdev->ngpio; i++) {
891                 if (gpiochip_is_requested(gc, i))
892                         break;
893         }
894         spin_unlock_irqrestore(&gpio_lock, flags);
895
896         if (i != gdev->ngpio)
897                 dev_crit(&gdev->dev,
898                          "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
899
900         /*
901          * The gpiochip side puts its use of the device to rest here:
902          * if there are no userspace clients, the chardev and device will
903          * be removed, else it will be dangling until the last user is
904          * gone.
905          */
906         gcdev_unregister(gdev);
907         up_write(&gdev->sem);
908         put_device(&gdev->dev);
909 }
910 EXPORT_SYMBOL_GPL(gpiochip_remove);
911
912 /**
913  * gpiochip_find() - iterator for locating a specific gpio_chip
914  * @data: data to pass to match function
915  * @match: Callback function to check gpio_chip
916  *
917  * Similar to bus_find_device.  It returns a reference to a gpio_chip as
918  * determined by a user supplied @match callback.  The callback should return
919  * 0 if the device doesn't match and non-zero if it does.  If the callback is
920  * non-zero, this function will return to the caller and not iterate over any
921  * more gpio_chips.
922  */
923 struct gpio_chip *gpiochip_find(void *data,
924                                 int (*match)(struct gpio_chip *gc,
925                                              void *data))
926 {
927         struct gpio_device *gdev;
928         struct gpio_chip *gc = NULL;
929         unsigned long flags;
930
931         spin_lock_irqsave(&gpio_lock, flags);
932         list_for_each_entry(gdev, &gpio_devices, list)
933                 if (gdev->chip && match(gdev->chip, data)) {
934                         gc = gdev->chip;
935                         break;
936                 }
937
938         spin_unlock_irqrestore(&gpio_lock, flags);
939
940         return gc;
941 }
942 EXPORT_SYMBOL_GPL(gpiochip_find);
943
944 static int gpiochip_match_name(struct gpio_chip *gc, void *data)
945 {
946         const char *name = data;
947
948         return !strcmp(gc->label, name);
949 }
950
951 static struct gpio_chip *find_chip_by_name(const char *name)
952 {
953         return gpiochip_find((void *)name, gpiochip_match_name);
954 }
955
956 #ifdef CONFIG_GPIOLIB_IRQCHIP
957
958 /*
959  * The following is irqchip helper code for gpiochips.
960  */
961
962 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
963 {
964         struct gpio_irq_chip *girq = &gc->irq;
965
966         if (!girq->init_hw)
967                 return 0;
968
969         return girq->init_hw(gc);
970 }
971
972 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
973 {
974         struct gpio_irq_chip *girq = &gc->irq;
975
976         if (!girq->init_valid_mask)
977                 return 0;
978
979         girq->valid_mask = gpiochip_allocate_mask(gc);
980         if (!girq->valid_mask)
981                 return -ENOMEM;
982
983         girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
984
985         return 0;
986 }
987
988 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
989 {
990         bitmap_free(gc->irq.valid_mask);
991         gc->irq.valid_mask = NULL;
992 }
993
994 bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gc,
995                                 unsigned int offset)
996 {
997         if (!gpiochip_line_is_valid(gc, offset))
998                 return false;
999         /* No mask means all valid */
1000         if (likely(!gc->irq.valid_mask))
1001                 return true;
1002         return test_bit(offset, gc->irq.valid_mask);
1003 }
1004 EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
1005
1006 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1007
1008 /**
1009  * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
1010  * to a gpiochip
1011  * @gc: the gpiochip to set the irqchip hierarchical handler to
1012  * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
1013  * will then percolate up to the parent
1014  */
1015 static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
1016                                               struct irq_chip *irqchip)
1017 {
1018         /* DT will deal with mapping each IRQ as we go along */
1019         if (is_of_node(gc->irq.fwnode))
1020                 return;
1021
1022         /*
1023          * This is for legacy and boardfile "irqchip" fwnodes: allocate
1024          * irqs upfront instead of dynamically since we don't have the
1025          * dynamic type of allocation that hardware description languages
1026          * provide. Once all GPIO drivers using board files are gone from
1027          * the kernel we can delete this code, but for a transitional period
1028          * it is necessary to keep this around.
1029          */
1030         if (is_fwnode_irqchip(gc->irq.fwnode)) {
1031                 int i;
1032                 int ret;
1033
1034                 for (i = 0; i < gc->ngpio; i++) {
1035                         struct irq_fwspec fwspec;
1036                         unsigned int parent_hwirq;
1037                         unsigned int parent_type;
1038                         struct gpio_irq_chip *girq = &gc->irq;
1039
1040                         /*
1041                          * We call the child to parent translation function
1042                          * only to check if the child IRQ is valid or not.
1043                          * Just pick the rising edge type here as that is what
1044                          * we likely need to support.
1045                          */
1046                         ret = girq->child_to_parent_hwirq(gc, i,
1047                                                           IRQ_TYPE_EDGE_RISING,
1048                                                           &parent_hwirq,
1049                                                           &parent_type);
1050                         if (ret) {
1051                                 chip_err(gc, "skip set-up on hwirq %d\n",
1052                                          i);
1053                                 continue;
1054                         }
1055
1056                         fwspec.fwnode = gc->irq.fwnode;
1057                         /* This is the hwirq for the GPIO line side of things */
1058                         fwspec.param[0] = girq->child_offset_to_irq(gc, i);
1059                         /* Just pick something */
1060                         fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
1061                         fwspec.param_count = 2;
1062                         ret = __irq_domain_alloc_irqs(gc->irq.domain,
1063                                                       /* just pick something */
1064                                                       -1,
1065                                                       1,
1066                                                       NUMA_NO_NODE,
1067                                                       &fwspec,
1068                                                       false,
1069                                                       NULL);
1070                         if (ret < 0) {
1071                                 chip_err(gc,
1072                                          "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
1073                                          i, parent_hwirq,
1074                                          ret);
1075                         }
1076                 }
1077         }
1078
1079         chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
1080
1081         return;
1082 }
1083
1084 static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
1085                                                    struct irq_fwspec *fwspec,
1086                                                    unsigned long *hwirq,
1087                                                    unsigned int *type)
1088 {
1089         /* We support standard DT translation */
1090         if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
1091                 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
1092         }
1093
1094         /* This is for board files and others not using DT */
1095         if (is_fwnode_irqchip(fwspec->fwnode)) {
1096                 int ret;
1097
1098                 ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
1099                 if (ret)
1100                         return ret;
1101                 WARN_ON(*type == IRQ_TYPE_NONE);
1102                 return 0;
1103         }
1104         return -EINVAL;
1105 }
1106
1107 static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
1108                                                unsigned int irq,
1109                                                unsigned int nr_irqs,
1110                                                void *data)
1111 {
1112         struct gpio_chip *gc = d->host_data;
1113         irq_hw_number_t hwirq;
1114         unsigned int type = IRQ_TYPE_NONE;
1115         struct irq_fwspec *fwspec = data;
1116         void *parent_arg;
1117         unsigned int parent_hwirq;
1118         unsigned int parent_type;
1119         struct gpio_irq_chip *girq = &gc->irq;
1120         int ret;
1121
1122         /*
1123          * The nr_irqs parameter is always one except for PCI multi-MSI
1124          * so this should not happen.
1125          */
1126         WARN_ON(nr_irqs != 1);
1127
1128         ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
1129         if (ret)
1130                 return ret;
1131
1132         chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq,  hwirq);
1133
1134         ret = girq->child_to_parent_hwirq(gc, hwirq, type,
1135                                           &parent_hwirq, &parent_type);
1136         if (ret) {
1137                 chip_err(gc, "can't look up hwirq %lu\n", hwirq);
1138                 return ret;
1139         }
1140         chip_dbg(gc, "found parent hwirq %u\n", parent_hwirq);
1141
1142         /*
1143          * We set handle_bad_irq because the .set_type() should
1144          * always be invoked and set the right type of handler.
1145          */
1146         irq_domain_set_info(d,
1147                             irq,
1148                             hwirq,
1149                             gc->irq.chip,
1150                             gc,
1151                             girq->handler,
1152                             NULL, NULL);
1153         irq_set_probe(irq);
1154
1155         /* This parent only handles asserted level IRQs */
1156         parent_arg = girq->populate_parent_alloc_arg(gc, parent_hwirq, parent_type);
1157         if (!parent_arg)
1158                 return -ENOMEM;
1159
1160         chip_dbg(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
1161                   irq, parent_hwirq);
1162         irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1163         ret = irq_domain_alloc_irqs_parent(d, irq, 1, parent_arg);
1164         /*
1165          * If the parent irqdomain is msi, the interrupts have already
1166          * been allocated, so the EEXIST is good.
1167          */
1168         if (irq_domain_is_msi(d->parent) && (ret == -EEXIST))
1169                 ret = 0;
1170         if (ret)
1171                 chip_err(gc,
1172                          "failed to allocate parent hwirq %d for hwirq %lu\n",
1173                          parent_hwirq, hwirq);
1174
1175         kfree(parent_arg);
1176         return ret;
1177 }
1178
1179 static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *gc,
1180                                                       unsigned int offset)
1181 {
1182         return offset;
1183 }
1184
1185 static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
1186 {
1187         ops->activate = gpiochip_irq_domain_activate;
1188         ops->deactivate = gpiochip_irq_domain_deactivate;
1189         ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
1190         ops->free = irq_domain_free_irqs_common;
1191
1192         /*
1193          * We only allow overriding the translate() function for
1194          * hierarchical chips, and this should only be done if the user
1195          * really need something other than 1:1 translation.
1196          */
1197         if (!ops->translate)
1198                 ops->translate = gpiochip_hierarchy_irq_domain_translate;
1199 }
1200
1201 static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1202 {
1203         if (!gc->irq.child_to_parent_hwirq ||
1204             !gc->irq.fwnode) {
1205                 chip_err(gc, "missing irqdomain vital data\n");
1206                 return -EINVAL;
1207         }
1208
1209         if (!gc->irq.child_offset_to_irq)
1210                 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
1211
1212         if (!gc->irq.populate_parent_alloc_arg)
1213                 gc->irq.populate_parent_alloc_arg =
1214                         gpiochip_populate_parent_fwspec_twocell;
1215
1216         gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
1217
1218         gc->irq.domain = irq_domain_create_hierarchy(
1219                 gc->irq.parent_domain,
1220                 0,
1221                 gc->ngpio,
1222                 gc->irq.fwnode,
1223                 &gc->irq.child_irq_domain_ops,
1224                 gc);
1225
1226         if (!gc->irq.domain)
1227                 return -ENOMEM;
1228
1229         gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
1230
1231         return 0;
1232 }
1233
1234 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1235 {
1236         return !!gc->irq.parent_domain;
1237 }
1238
1239 void *gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc,
1240                                              unsigned int parent_hwirq,
1241                                              unsigned int parent_type)
1242 {
1243         struct irq_fwspec *fwspec;
1244
1245         fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
1246         if (!fwspec)
1247                 return NULL;
1248
1249         fwspec->fwnode = gc->irq.parent_domain->fwnode;
1250         fwspec->param_count = 2;
1251         fwspec->param[0] = parent_hwirq;
1252         fwspec->param[1] = parent_type;
1253
1254         return fwspec;
1255 }
1256 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
1257
1258 void *gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc,
1259                                               unsigned int parent_hwirq,
1260                                               unsigned int parent_type)
1261 {
1262         struct irq_fwspec *fwspec;
1263
1264         fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
1265         if (!fwspec)
1266                 return NULL;
1267
1268         fwspec->fwnode = gc->irq.parent_domain->fwnode;
1269         fwspec->param_count = 4;
1270         fwspec->param[0] = 0;
1271         fwspec->param[1] = parent_hwirq;
1272         fwspec->param[2] = 0;
1273         fwspec->param[3] = parent_type;
1274
1275         return fwspec;
1276 }
1277 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
1278
1279 #else
1280
1281 static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1282 {
1283         return -EINVAL;
1284 }
1285
1286 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1287 {
1288         return false;
1289 }
1290
1291 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1292
1293 /**
1294  * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1295  * @d: the irqdomain used by this irqchip
1296  * @irq: the global irq number used by this GPIO irqchip irq
1297  * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1298  *
1299  * This function will set up the mapping for a certain IRQ line on a
1300  * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1301  * stored inside the gpiochip.
1302  */
1303 int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1304                      irq_hw_number_t hwirq)
1305 {
1306         struct gpio_chip *gc = d->host_data;
1307         int ret = 0;
1308
1309         if (!gpiochip_irqchip_irq_valid(gc, hwirq))
1310                 return -ENXIO;
1311
1312         irq_set_chip_data(irq, gc);
1313         /*
1314          * This lock class tells lockdep that GPIO irqs are in a different
1315          * category than their parents, so it won't report false recursion.
1316          */
1317         irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1318         irq_set_chip_and_handler(irq, gc->irq.chip, gc->irq.handler);
1319         /* Chips that use nested thread handlers have them marked */
1320         if (gc->irq.threaded)
1321                 irq_set_nested_thread(irq, 1);
1322         irq_set_noprobe(irq);
1323
1324         if (gc->irq.num_parents == 1)
1325                 ret = irq_set_parent(irq, gc->irq.parents[0]);
1326         else if (gc->irq.map)
1327                 ret = irq_set_parent(irq, gc->irq.map[hwirq]);
1328
1329         if (ret < 0)
1330                 return ret;
1331
1332         /*
1333          * No set-up of the hardware will happen if IRQ_TYPE_NONE
1334          * is passed as default type.
1335          */
1336         if (gc->irq.default_type != IRQ_TYPE_NONE)
1337                 irq_set_irq_type(irq, gc->irq.default_type);
1338
1339         return 0;
1340 }
1341 EXPORT_SYMBOL_GPL(gpiochip_irq_map);
1342
1343 void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1344 {
1345         struct gpio_chip *gc = d->host_data;
1346
1347         if (gc->irq.threaded)
1348                 irq_set_nested_thread(irq, 0);
1349         irq_set_chip_and_handler(irq, NULL, NULL);
1350         irq_set_chip_data(irq, NULL);
1351 }
1352 EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
1353
1354 static const struct irq_domain_ops gpiochip_domain_ops = {
1355         .map    = gpiochip_irq_map,
1356         .unmap  = gpiochip_irq_unmap,
1357         /* Virtually all GPIO irqchips are twocell:ed */
1358         .xlate  = irq_domain_xlate_twocell,
1359 };
1360
1361 /*
1362  * TODO: move these activate/deactivate in under the hierarchicial
1363  * irqchip implementation as static once SPMI and SSBI (all external
1364  * users) are phased over.
1365  */
1366 /**
1367  * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
1368  * @domain: The IRQ domain used by this IRQ chip
1369  * @data: Outermost irq_data associated with the IRQ
1370  * @reserve: If set, only reserve an interrupt vector instead of assigning one
1371  *
1372  * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
1373  * used as the activate function for the &struct irq_domain_ops. The host_data
1374  * for the IRQ domain must be the &struct gpio_chip.
1375  */
1376 int gpiochip_irq_domain_activate(struct irq_domain *domain,
1377                                  struct irq_data *data, bool reserve)
1378 {
1379         struct gpio_chip *gc = domain->host_data;
1380
1381         return gpiochip_lock_as_irq(gc, data->hwirq);
1382 }
1383 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate);
1384
1385 /**
1386  * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
1387  * @domain: The IRQ domain used by this IRQ chip
1388  * @data: Outermost irq_data associated with the IRQ
1389  *
1390  * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
1391  * be used as the deactivate function for the &struct irq_domain_ops. The
1392  * host_data for the IRQ domain must be the &struct gpio_chip.
1393  */
1394 void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
1395                                     struct irq_data *data)
1396 {
1397         struct gpio_chip *gc = domain->host_data;
1398
1399         return gpiochip_unlock_as_irq(gc, data->hwirq);
1400 }
1401 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate);
1402
1403 static int gpiochip_to_irq(struct gpio_chip *gc, unsigned int offset)
1404 {
1405         struct irq_domain *domain = gc->irq.domain;
1406
1407 #ifdef CONFIG_GPIOLIB_IRQCHIP
1408         /*
1409          * Avoid race condition with other code, which tries to lookup
1410          * an IRQ before the irqchip has been properly registered,
1411          * i.e. while gpiochip is still being brought up.
1412          */
1413         if (!gc->irq.initialized)
1414                 return -EPROBE_DEFER;
1415 #endif
1416
1417         if (!gpiochip_irqchip_irq_valid(gc, offset))
1418                 return -ENXIO;
1419
1420 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1421         if (irq_domain_is_hierarchy(domain)) {
1422                 struct irq_fwspec spec;
1423
1424                 spec.fwnode = domain->fwnode;
1425                 spec.param_count = 2;
1426                 spec.param[0] = gc->irq.child_offset_to_irq(gc, offset);
1427                 spec.param[1] = IRQ_TYPE_NONE;
1428
1429                 return irq_create_fwspec_mapping(&spec);
1430         }
1431 #endif
1432
1433         return irq_create_mapping(domain, offset);
1434 }
1435
1436 static int gpiochip_irq_reqres(struct irq_data *d)
1437 {
1438         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1439
1440         return gpiochip_reqres_irq(gc, d->hwirq);
1441 }
1442
1443 static void gpiochip_irq_relres(struct irq_data *d)
1444 {
1445         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1446
1447         gpiochip_relres_irq(gc, d->hwirq);
1448 }
1449
1450 static void gpiochip_irq_mask(struct irq_data *d)
1451 {
1452         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1453
1454         if (gc->irq.irq_mask)
1455                 gc->irq.irq_mask(d);
1456         gpiochip_disable_irq(gc, d->hwirq);
1457 }
1458
1459 static void gpiochip_irq_unmask(struct irq_data *d)
1460 {
1461         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1462
1463         gpiochip_enable_irq(gc, d->hwirq);
1464         if (gc->irq.irq_unmask)
1465                 gc->irq.irq_unmask(d);
1466 }
1467
1468 static void gpiochip_irq_enable(struct irq_data *d)
1469 {
1470         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1471
1472         gpiochip_enable_irq(gc, d->hwirq);
1473         gc->irq.irq_enable(d);
1474 }
1475
1476 static void gpiochip_irq_disable(struct irq_data *d)
1477 {
1478         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1479
1480         gc->irq.irq_disable(d);
1481         gpiochip_disable_irq(gc, d->hwirq);
1482 }
1483
1484 static void gpiochip_set_irq_hooks(struct gpio_chip *gc)
1485 {
1486         struct irq_chip *irqchip = gc->irq.chip;
1487
1488         if (!irqchip->irq_request_resources &&
1489             !irqchip->irq_release_resources) {
1490                 irqchip->irq_request_resources = gpiochip_irq_reqres;
1491                 irqchip->irq_release_resources = gpiochip_irq_relres;
1492         }
1493         if (WARN_ON(gc->irq.irq_enable))
1494                 return;
1495         /* Check if the irqchip already has this hook... */
1496         if (irqchip->irq_enable == gpiochip_irq_enable ||
1497                 irqchip->irq_mask == gpiochip_irq_mask) {
1498                 /*
1499                  * ...and if so, give a gentle warning that this is bad
1500                  * practice.
1501                  */
1502                 chip_info(gc,
1503                           "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
1504                 return;
1505         }
1506
1507         if (irqchip->irq_disable) {
1508                 gc->irq.irq_disable = irqchip->irq_disable;
1509                 irqchip->irq_disable = gpiochip_irq_disable;
1510         } else {
1511                 gc->irq.irq_mask = irqchip->irq_mask;
1512                 irqchip->irq_mask = gpiochip_irq_mask;
1513         }
1514
1515         if (irqchip->irq_enable) {
1516                 gc->irq.irq_enable = irqchip->irq_enable;
1517                 irqchip->irq_enable = gpiochip_irq_enable;
1518         } else {
1519                 gc->irq.irq_unmask = irqchip->irq_unmask;
1520                 irqchip->irq_unmask = gpiochip_irq_unmask;
1521         }
1522 }
1523
1524 /**
1525  * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1526  * @gc: the GPIO chip to add the IRQ chip to
1527  * @lock_key: lockdep class for IRQ lock
1528  * @request_key: lockdep class for IRQ request
1529  */
1530 static int gpiochip_add_irqchip(struct gpio_chip *gc,
1531                                 struct lock_class_key *lock_key,
1532                                 struct lock_class_key *request_key)
1533 {
1534         struct fwnode_handle *fwnode = dev_fwnode(&gc->gpiodev->dev);
1535         struct irq_chip *irqchip = gc->irq.chip;
1536         unsigned int type;
1537         unsigned int i;
1538
1539         if (!irqchip)
1540                 return 0;
1541
1542         if (gc->irq.parent_handler && gc->can_sleep) {
1543                 chip_err(gc, "you cannot have chained interrupts on a chip that may sleep\n");
1544                 return -EINVAL;
1545         }
1546
1547         type = gc->irq.default_type;
1548
1549         /*
1550          * Specifying a default trigger is a terrible idea if DT or ACPI is
1551          * used to configure the interrupts, as you may end up with
1552          * conflicting triggers. Tell the user, and reset to NONE.
1553          */
1554         if (WARN(fwnode && type != IRQ_TYPE_NONE,
1555                  "%pfw: Ignoring %u default trigger\n", fwnode, type))
1556                 type = IRQ_TYPE_NONE;
1557
1558         if (gc->to_irq)
1559                 chip_warn(gc, "to_irq is redefined in %s and you shouldn't rely on it\n", __func__);
1560
1561         gc->to_irq = gpiochip_to_irq;
1562         gc->irq.default_type = type;
1563         gc->irq.lock_key = lock_key;
1564         gc->irq.request_key = request_key;
1565
1566         /* If a parent irqdomain is provided, let's build a hierarchy */
1567         if (gpiochip_hierarchy_is_hierarchical(gc)) {
1568                 int ret = gpiochip_hierarchy_add_domain(gc);
1569                 if (ret)
1570                         return ret;
1571         } else {
1572                 /* Some drivers provide custom irqdomain ops */
1573                 gc->irq.domain = irq_domain_create_simple(fwnode,
1574                         gc->ngpio,
1575                         gc->irq.first,
1576                         gc->irq.domain_ops ?: &gpiochip_domain_ops,
1577                         gc);
1578                 if (!gc->irq.domain)
1579                         return -EINVAL;
1580         }
1581
1582         if (gc->irq.parent_handler) {
1583                 void *data = gc->irq.parent_handler_data ?: gc;
1584
1585                 for (i = 0; i < gc->irq.num_parents; i++) {
1586                         /*
1587                          * The parent IRQ chip is already using the chip_data
1588                          * for this IRQ chip, so our callbacks simply use the
1589                          * handler_data.
1590                          */
1591                         irq_set_chained_handler_and_data(gc->irq.parents[i],
1592                                                          gc->irq.parent_handler,
1593                                                          data);
1594                 }
1595         }
1596
1597         gpiochip_set_irq_hooks(gc);
1598
1599         /*
1600          * Using barrier() here to prevent compiler from reordering
1601          * gc->irq.initialized before initialization of above
1602          * GPIO chip irq members.
1603          */
1604         barrier();
1605
1606         gc->irq.initialized = true;
1607
1608         acpi_gpiochip_request_interrupts(gc);
1609
1610         return 0;
1611 }
1612
1613 /**
1614  * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1615  * @gc: the gpiochip to remove the irqchip from
1616  *
1617  * This is called only from gpiochip_remove()
1618  */
1619 static void gpiochip_irqchip_remove(struct gpio_chip *gc)
1620 {
1621         struct irq_chip *irqchip = gc->irq.chip;
1622         unsigned int offset;
1623
1624         acpi_gpiochip_free_interrupts(gc);
1625
1626         if (irqchip && gc->irq.parent_handler) {
1627                 struct gpio_irq_chip *irq = &gc->irq;
1628                 unsigned int i;
1629
1630                 for (i = 0; i < irq->num_parents; i++)
1631                         irq_set_chained_handler_and_data(irq->parents[i],
1632                                                          NULL, NULL);
1633         }
1634
1635         /* Remove all IRQ mappings and delete the domain */
1636         if (gc->irq.domain) {
1637                 unsigned int irq;
1638
1639                 for (offset = 0; offset < gc->ngpio; offset++) {
1640                         if (!gpiochip_irqchip_irq_valid(gc, offset))
1641                                 continue;
1642
1643                         irq = irq_find_mapping(gc->irq.domain, offset);
1644                         irq_dispose_mapping(irq);
1645                 }
1646
1647                 irq_domain_remove(gc->irq.domain);
1648         }
1649
1650         if (irqchip) {
1651                 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
1652                         irqchip->irq_request_resources = NULL;
1653                         irqchip->irq_release_resources = NULL;
1654                 }
1655                 if (irqchip->irq_enable == gpiochip_irq_enable) {
1656                         irqchip->irq_enable = gc->irq.irq_enable;
1657                         irqchip->irq_disable = gc->irq.irq_disable;
1658                 }
1659         }
1660         gc->irq.irq_enable = NULL;
1661         gc->irq.irq_disable = NULL;
1662         gc->irq.chip = NULL;
1663
1664         gpiochip_irqchip_free_valid_mask(gc);
1665 }
1666
1667 /**
1668  * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip
1669  * @gc: the gpiochip to add the irqchip to
1670  * @domain: the irqdomain to add to the gpiochip
1671  *
1672  * This function adds an IRQ domain to the gpiochip.
1673  */
1674 int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
1675                                 struct irq_domain *domain)
1676 {
1677         if (!domain)
1678                 return -EINVAL;
1679
1680         gc->to_irq = gpiochip_to_irq;
1681         gc->irq.domain = domain;
1682
1683         return 0;
1684 }
1685 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_domain);
1686
1687 #else /* CONFIG_GPIOLIB_IRQCHIP */
1688
1689 static inline int gpiochip_add_irqchip(struct gpio_chip *gc,
1690                                        struct lock_class_key *lock_key,
1691                                        struct lock_class_key *request_key)
1692 {
1693         return 0;
1694 }
1695 static void gpiochip_irqchip_remove(struct gpio_chip *gc) {}
1696
1697 static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1698 {
1699         return 0;
1700 }
1701
1702 static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
1703 {
1704         return 0;
1705 }
1706 static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
1707 { }
1708
1709 #endif /* CONFIG_GPIOLIB_IRQCHIP */
1710
1711 /**
1712  * gpiochip_generic_request() - request the gpio function for a pin
1713  * @gc: the gpiochip owning the GPIO
1714  * @offset: the offset of the GPIO to request for GPIO function
1715  */
1716 int gpiochip_generic_request(struct gpio_chip *gc, unsigned int offset)
1717 {
1718 #ifdef CONFIG_PINCTRL
1719         if (list_empty(&gc->gpiodev->pin_ranges))
1720                 return 0;
1721 #endif
1722
1723         return pinctrl_gpio_request(gc->gpiodev->base + offset);
1724 }
1725 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1726
1727 /**
1728  * gpiochip_generic_free() - free the gpio function from a pin
1729  * @gc: the gpiochip to request the gpio function for
1730  * @offset: the offset of the GPIO to free from GPIO function
1731  */
1732 void gpiochip_generic_free(struct gpio_chip *gc, unsigned int offset)
1733 {
1734 #ifdef CONFIG_PINCTRL
1735         if (list_empty(&gc->gpiodev->pin_ranges))
1736                 return;
1737 #endif
1738
1739         pinctrl_gpio_free(gc->gpiodev->base + offset);
1740 }
1741 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1742
1743 /**
1744  * gpiochip_generic_config() - apply configuration for a pin
1745  * @gc: the gpiochip owning the GPIO
1746  * @offset: the offset of the GPIO to apply the configuration
1747  * @config: the configuration to be applied
1748  */
1749 int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset,
1750                             unsigned long config)
1751 {
1752         return pinctrl_gpio_set_config(gc->gpiodev->base + offset, config);
1753 }
1754 EXPORT_SYMBOL_GPL(gpiochip_generic_config);
1755
1756 #ifdef CONFIG_PINCTRL
1757
1758 /**
1759  * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1760  * @gc: the gpiochip to add the range for
1761  * @pctldev: the pin controller to map to
1762  * @gpio_offset: the start offset in the current gpio_chip number space
1763  * @pin_group: name of the pin group inside the pin controller
1764  *
1765  * Calling this function directly from a DeviceTree-supported
1766  * pinctrl driver is DEPRECATED. Please see Section 2.1 of
1767  * Documentation/devicetree/bindings/gpio/gpio.txt on how to
1768  * bind pinctrl and gpio drivers via the "gpio-ranges" property.
1769  */
1770 int gpiochip_add_pingroup_range(struct gpio_chip *gc,
1771                         struct pinctrl_dev *pctldev,
1772                         unsigned int gpio_offset, const char *pin_group)
1773 {
1774         struct gpio_pin_range *pin_range;
1775         struct gpio_device *gdev = gc->gpiodev;
1776         int ret;
1777
1778         pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1779         if (!pin_range) {
1780                 chip_err(gc, "failed to allocate pin ranges\n");
1781                 return -ENOMEM;
1782         }
1783
1784         /* Use local offset as range ID */
1785         pin_range->range.id = gpio_offset;
1786         pin_range->range.gc = gc;
1787         pin_range->range.name = gc->label;
1788         pin_range->range.base = gdev->base + gpio_offset;
1789         pin_range->pctldev = pctldev;
1790
1791         ret = pinctrl_get_group_pins(pctldev, pin_group,
1792                                         &pin_range->range.pins,
1793                                         &pin_range->range.npins);
1794         if (ret < 0) {
1795                 kfree(pin_range);
1796                 return ret;
1797         }
1798
1799         pinctrl_add_gpio_range(pctldev, &pin_range->range);
1800
1801         chip_dbg(gc, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1802                  gpio_offset, gpio_offset + pin_range->range.npins - 1,
1803                  pinctrl_dev_get_devname(pctldev), pin_group);
1804
1805         list_add_tail(&pin_range->node, &gdev->pin_ranges);
1806
1807         return 0;
1808 }
1809 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1810
1811 /**
1812  * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1813  * @gc: the gpiochip to add the range for
1814  * @pinctl_name: the dev_name() of the pin controller to map to
1815  * @gpio_offset: the start offset in the current gpio_chip number space
1816  * @pin_offset: the start offset in the pin controller number space
1817  * @npins: the number of pins from the offset of each pin space (GPIO and
1818  *      pin controller) to accumulate in this range
1819  *
1820  * Returns:
1821  * 0 on success, or a negative error-code on failure.
1822  *
1823  * Calling this function directly from a DeviceTree-supported
1824  * pinctrl driver is DEPRECATED. Please see Section 2.1 of
1825  * Documentation/devicetree/bindings/gpio/gpio.txt on how to
1826  * bind pinctrl and gpio drivers via the "gpio-ranges" property.
1827  */
1828 int gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
1829                            unsigned int gpio_offset, unsigned int pin_offset,
1830                            unsigned int npins)
1831 {
1832         struct gpio_pin_range *pin_range;
1833         struct gpio_device *gdev = gc->gpiodev;
1834         int ret;
1835
1836         pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1837         if (!pin_range) {
1838                 chip_err(gc, "failed to allocate pin ranges\n");
1839                 return -ENOMEM;
1840         }
1841
1842         /* Use local offset as range ID */
1843         pin_range->range.id = gpio_offset;
1844         pin_range->range.gc = gc;
1845         pin_range->range.name = gc->label;
1846         pin_range->range.base = gdev->base + gpio_offset;
1847         pin_range->range.pin_base = pin_offset;
1848         pin_range->range.npins = npins;
1849         pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
1850                         &pin_range->range);
1851         if (IS_ERR(pin_range->pctldev)) {
1852                 ret = PTR_ERR(pin_range->pctldev);
1853                 chip_err(gc, "could not create pin range\n");
1854                 kfree(pin_range);
1855                 return ret;
1856         }
1857         chip_dbg(gc, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1858                  gpio_offset, gpio_offset + npins - 1,
1859                  pinctl_name,
1860                  pin_offset, pin_offset + npins - 1);
1861
1862         list_add_tail(&pin_range->node, &gdev->pin_ranges);
1863
1864         return 0;
1865 }
1866 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
1867
1868 /**
1869  * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1870  * @gc: the chip to remove all the mappings for
1871  */
1872 void gpiochip_remove_pin_ranges(struct gpio_chip *gc)
1873 {
1874         struct gpio_pin_range *pin_range, *tmp;
1875         struct gpio_device *gdev = gc->gpiodev;
1876
1877         list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
1878                 list_del(&pin_range->node);
1879                 pinctrl_remove_gpio_range(pin_range->pctldev,
1880                                 &pin_range->range);
1881                 kfree(pin_range);
1882         }
1883 }
1884 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1885
1886 #endif /* CONFIG_PINCTRL */
1887
1888 /* These "optional" allocation calls help prevent drivers from stomping
1889  * on each other, and help provide better diagnostics in debugfs.
1890  * They're called even less than the "set direction" calls.
1891  */
1892 static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
1893 {
1894         struct gpio_chip        *gc = desc->gdev->chip;
1895         int                     ret;
1896         unsigned long           flags;
1897         unsigned                offset;
1898
1899         if (label) {
1900                 label = kstrdup_const(label, GFP_KERNEL);
1901                 if (!label)
1902                         return -ENOMEM;
1903         }
1904
1905         spin_lock_irqsave(&gpio_lock, flags);
1906
1907         /* NOTE:  gpio_request() can be called in early boot,
1908          * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1909          */
1910
1911         if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1912                 desc_set_label(desc, label ? : "?");
1913         } else {
1914                 ret = -EBUSY;
1915                 goto out_free_unlock;
1916         }
1917
1918         if (gc->request) {
1919                 /* gc->request may sleep */
1920                 spin_unlock_irqrestore(&gpio_lock, flags);
1921                 offset = gpio_chip_hwgpio(desc);
1922                 if (gpiochip_line_is_valid(gc, offset))
1923                         ret = gc->request(gc, offset);
1924                 else
1925                         ret = -EINVAL;
1926                 spin_lock_irqsave(&gpio_lock, flags);
1927
1928                 if (ret) {
1929                         desc_set_label(desc, NULL);
1930                         clear_bit(FLAG_REQUESTED, &desc->flags);
1931                         goto out_free_unlock;
1932                 }
1933         }
1934         if (gc->get_direction) {
1935                 /* gc->get_direction may sleep */
1936                 spin_unlock_irqrestore(&gpio_lock, flags);
1937                 gpiod_get_direction(desc);
1938                 spin_lock_irqsave(&gpio_lock, flags);
1939         }
1940         spin_unlock_irqrestore(&gpio_lock, flags);
1941         return 0;
1942
1943 out_free_unlock:
1944         spin_unlock_irqrestore(&gpio_lock, flags);
1945         kfree_const(label);
1946         return ret;
1947 }
1948
1949 /*
1950  * This descriptor validation needs to be inserted verbatim into each
1951  * function taking a descriptor, so we need to use a preprocessor
1952  * macro to avoid endless duplication. If the desc is NULL it is an
1953  * optional GPIO and calls should just bail out.
1954  */
1955 static int validate_desc(const struct gpio_desc *desc, const char *func)
1956 {
1957         if (!desc)
1958                 return 0;
1959         if (IS_ERR(desc)) {
1960                 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
1961                 return PTR_ERR(desc);
1962         }
1963         if (!desc->gdev) {
1964                 pr_warn("%s: invalid GPIO (no device)\n", func);
1965                 return -EINVAL;
1966         }
1967         if (!desc->gdev->chip) {
1968                 dev_warn(&desc->gdev->dev,
1969                          "%s: backing chip is gone\n", func);
1970                 return 0;
1971         }
1972         return 1;
1973 }
1974
1975 #define VALIDATE_DESC(desc) do { \
1976         int __valid = validate_desc(desc, __func__); \
1977         if (__valid <= 0) \
1978                 return __valid; \
1979         } while (0)
1980
1981 #define VALIDATE_DESC_VOID(desc) do { \
1982         int __valid = validate_desc(desc, __func__); \
1983         if (__valid <= 0) \
1984                 return; \
1985         } while (0)
1986
1987 int gpiod_request(struct gpio_desc *desc, const char *label)
1988 {
1989         int ret = -EPROBE_DEFER;
1990         struct gpio_device *gdev;
1991
1992         VALIDATE_DESC(desc);
1993         gdev = desc->gdev;
1994
1995         if (try_module_get(gdev->owner)) {
1996                 ret = gpiod_request_commit(desc, label);
1997                 if (ret)
1998                         module_put(gdev->owner);
1999                 else
2000                         get_device(&gdev->dev);
2001         }
2002
2003         if (ret)
2004                 gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
2005
2006         return ret;
2007 }
2008
2009 static bool gpiod_free_commit(struct gpio_desc *desc)
2010 {
2011         bool                    ret = false;
2012         unsigned long           flags;
2013         struct gpio_chip        *gc;
2014
2015         might_sleep();
2016
2017         gpiod_unexport(desc);
2018
2019         spin_lock_irqsave(&gpio_lock, flags);
2020
2021         gc = desc->gdev->chip;
2022         if (gc && test_bit(FLAG_REQUESTED, &desc->flags)) {
2023                 if (gc->free) {
2024                         spin_unlock_irqrestore(&gpio_lock, flags);
2025                         might_sleep_if(gc->can_sleep);
2026                         gc->free(gc, gpio_chip_hwgpio(desc));
2027                         spin_lock_irqsave(&gpio_lock, flags);
2028                 }
2029                 kfree_const(desc->label);
2030                 desc_set_label(desc, NULL);
2031                 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
2032                 clear_bit(FLAG_REQUESTED, &desc->flags);
2033                 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
2034                 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
2035                 clear_bit(FLAG_PULL_UP, &desc->flags);
2036                 clear_bit(FLAG_PULL_DOWN, &desc->flags);
2037                 clear_bit(FLAG_BIAS_DISABLE, &desc->flags);
2038                 clear_bit(FLAG_EDGE_RISING, &desc->flags);
2039                 clear_bit(FLAG_EDGE_FALLING, &desc->flags);
2040                 clear_bit(FLAG_IS_HOGGED, &desc->flags);
2041 #ifdef CONFIG_OF_DYNAMIC
2042                 desc->hog = NULL;
2043 #endif
2044 #ifdef CONFIG_GPIO_CDEV
2045                 WRITE_ONCE(desc->debounce_period_us, 0);
2046 #endif
2047                 ret = true;
2048         }
2049
2050         spin_unlock_irqrestore(&gpio_lock, flags);
2051         blocking_notifier_call_chain(&desc->gdev->notifier,
2052                                      GPIOLINE_CHANGED_RELEASED, desc);
2053
2054         return ret;
2055 }
2056
2057 void gpiod_free(struct gpio_desc *desc)
2058 {
2059         if (desc && desc->gdev && gpiod_free_commit(desc)) {
2060                 module_put(desc->gdev->owner);
2061                 put_device(&desc->gdev->dev);
2062         } else {
2063                 WARN_ON(extra_checks);
2064         }
2065 }
2066
2067 /**
2068  * gpiochip_is_requested - return string iff signal was requested
2069  * @gc: controller managing the signal
2070  * @offset: of signal within controller's 0..(ngpio - 1) range
2071  *
2072  * Returns NULL if the GPIO is not currently requested, else a string.
2073  * The string returned is the label passed to gpio_request(); if none has been
2074  * passed it is a meaningless, non-NULL constant.
2075  *
2076  * This function is for use by GPIO controller drivers.  The label can
2077  * help with diagnostics, and knowing that the signal is used as a GPIO
2078  * can help avoid accidentally multiplexing it to another controller.
2079  */
2080 const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned int offset)
2081 {
2082         struct gpio_desc *desc;
2083
2084         desc = gpiochip_get_desc(gc, offset);
2085         if (IS_ERR(desc))
2086                 return NULL;
2087
2088         if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
2089                 return NULL;
2090         return desc->label;
2091 }
2092 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2093
2094 /**
2095  * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2096  * @gc: GPIO chip
2097  * @hwnum: hardware number of the GPIO for which to request the descriptor
2098  * @label: label for the GPIO
2099  * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2100  * specify things like line inversion semantics with the machine flags
2101  * such as GPIO_OUT_LOW
2102  * @dflags: descriptor request flags for this GPIO or 0 if default, this
2103  * can be used to specify consumer semantics such as open drain
2104  *
2105  * Function allows GPIO chip drivers to request and use their own GPIO
2106  * descriptors via gpiolib API. Difference to gpiod_request() is that this
2107  * function will not increase reference count of the GPIO chip module. This
2108  * allows the GPIO chip module to be unloaded as needed (we assume that the
2109  * GPIO chip driver handles freeing the GPIOs it has requested).
2110  *
2111  * Returns:
2112  * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2113  * code on failure.
2114  */
2115 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
2116                                             unsigned int hwnum,
2117                                             const char *label,
2118                                             enum gpio_lookup_flags lflags,
2119                                             enum gpiod_flags dflags)
2120 {
2121         struct gpio_desc *desc = gpiochip_get_desc(gc, hwnum);
2122         int ret;
2123
2124         if (IS_ERR(desc)) {
2125                 chip_err(gc, "failed to get GPIO descriptor\n");
2126                 return desc;
2127         }
2128
2129         ret = gpiod_request_commit(desc, label);
2130         if (ret < 0)
2131                 return ERR_PTR(ret);
2132
2133         ret = gpiod_configure_flags(desc, label, lflags, dflags);
2134         if (ret) {
2135                 chip_err(gc, "setup of own GPIO %s failed\n", label);
2136                 gpiod_free_commit(desc);
2137                 return ERR_PTR(ret);
2138         }
2139
2140         return desc;
2141 }
2142 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
2143
2144 /**
2145  * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2146  * @desc: GPIO descriptor to free
2147  *
2148  * Function frees the given GPIO requested previously with
2149  * gpiochip_request_own_desc().
2150  */
2151 void gpiochip_free_own_desc(struct gpio_desc *desc)
2152 {
2153         if (desc)
2154                 gpiod_free_commit(desc);
2155 }
2156 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
2157
2158 /*
2159  * Drivers MUST set GPIO direction before making get/set calls.  In
2160  * some cases this is done in early boot, before IRQs are enabled.
2161  *
2162  * As a rule these aren't called more than once (except for drivers
2163  * using the open-drain emulation idiom) so these are natural places
2164  * to accumulate extra debugging checks.  Note that we can't (yet)
2165  * rely on gpio_request() having been called beforehand.
2166  */
2167
2168 static int gpio_do_set_config(struct gpio_chip *gc, unsigned int offset,
2169                               unsigned long config)
2170 {
2171         if (!gc->set_config)
2172                 return -ENOTSUPP;
2173
2174         return gc->set_config(gc, offset, config);
2175 }
2176
2177 static int gpio_set_config_with_argument(struct gpio_desc *desc,
2178                                          enum pin_config_param mode,
2179                                          u32 argument)
2180 {
2181         struct gpio_chip *gc = desc->gdev->chip;
2182         unsigned long config;
2183
2184         config = pinconf_to_config_packed(mode, argument);
2185         return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2186 }
2187
2188 static int gpio_set_config_with_argument_optional(struct gpio_desc *desc,
2189                                                   enum pin_config_param mode,
2190                                                   u32 argument)
2191 {
2192         struct device *dev = &desc->gdev->dev;
2193         int gpio = gpio_chip_hwgpio(desc);
2194         int ret;
2195
2196         ret = gpio_set_config_with_argument(desc, mode, argument);
2197         if (ret != -ENOTSUPP)
2198                 return ret;
2199
2200         switch (mode) {
2201         case PIN_CONFIG_PERSIST_STATE:
2202                 dev_dbg(dev, "Persistence not supported for GPIO %d\n", gpio);
2203                 break;
2204         default:
2205                 break;
2206         }
2207
2208         return 0;
2209 }
2210
2211 static int gpio_set_config(struct gpio_desc *desc, enum pin_config_param mode)
2212 {
2213         return gpio_set_config_with_argument(desc, mode, 0);
2214 }
2215
2216 static int gpio_set_bias(struct gpio_desc *desc)
2217 {
2218         enum pin_config_param bias;
2219         unsigned int arg;
2220
2221         if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2222                 bias = PIN_CONFIG_BIAS_DISABLE;
2223         else if (test_bit(FLAG_PULL_UP, &desc->flags))
2224                 bias = PIN_CONFIG_BIAS_PULL_UP;
2225         else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2226                 bias = PIN_CONFIG_BIAS_PULL_DOWN;
2227         else
2228                 return 0;
2229
2230         switch (bias) {
2231         case PIN_CONFIG_BIAS_PULL_DOWN:
2232         case PIN_CONFIG_BIAS_PULL_UP:
2233                 arg = 1;
2234                 break;
2235
2236         default:
2237                 arg = 0;
2238                 break;
2239         }
2240
2241         return gpio_set_config_with_argument_optional(desc, bias, arg);
2242 }
2243
2244 /**
2245  * gpio_set_debounce_timeout() - Set debounce timeout
2246  * @desc:       GPIO descriptor to set the debounce timeout
2247  * @debounce:   Debounce timeout in microseconds
2248  *
2249  * The function calls the certain GPIO driver to set debounce timeout
2250  * in the hardware.
2251  *
2252  * Returns 0 on success, or negative error code otherwise.
2253  */
2254 int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce)
2255 {
2256         return gpio_set_config_with_argument_optional(desc,
2257                                                       PIN_CONFIG_INPUT_DEBOUNCE,
2258                                                       debounce);
2259 }
2260
2261 /**
2262  * gpiod_direction_input - set the GPIO direction to input
2263  * @desc:       GPIO to set to input
2264  *
2265  * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2266  * be called safely on it.
2267  *
2268  * Return 0 in case of success, else an error code.
2269  */
2270 int gpiod_direction_input(struct gpio_desc *desc)
2271 {
2272         struct gpio_chip        *gc;
2273         int                     ret = 0;
2274
2275         VALIDATE_DESC(desc);
2276         gc = desc->gdev->chip;
2277
2278         /*
2279          * It is legal to have no .get() and .direction_input() specified if
2280          * the chip is output-only, but you can't specify .direction_input()
2281          * and not support the .get() operation, that doesn't make sense.
2282          */
2283         if (!gc->get && gc->direction_input) {
2284                 gpiod_warn(desc,
2285                            "%s: missing get() but have direction_input()\n",
2286                            __func__);
2287                 return -EIO;
2288         }
2289
2290         /*
2291          * If we have a .direction_input() callback, things are simple,
2292          * just call it. Else we are some input-only chip so try to check the
2293          * direction (if .get_direction() is supported) else we silently
2294          * assume we are in input mode after this.
2295          */
2296         if (gc->direction_input) {
2297                 ret = gc->direction_input(gc, gpio_chip_hwgpio(desc));
2298         } else if (gc->get_direction &&
2299                   (gc->get_direction(gc, gpio_chip_hwgpio(desc)) != 1)) {
2300                 gpiod_warn(desc,
2301                            "%s: missing direction_input() operation and line is output\n",
2302                            __func__);
2303                 return -EIO;
2304         }
2305         if (ret == 0) {
2306                 clear_bit(FLAG_IS_OUT, &desc->flags);
2307                 ret = gpio_set_bias(desc);
2308         }
2309
2310         trace_gpio_direction(desc_to_gpio(desc), 1, ret);
2311
2312         return ret;
2313 }
2314 EXPORT_SYMBOL_GPL(gpiod_direction_input);
2315
2316 static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
2317 {
2318         struct gpio_chip *gc = desc->gdev->chip;
2319         int val = !!value;
2320         int ret = 0;
2321
2322         /*
2323          * It's OK not to specify .direction_output() if the gpiochip is
2324          * output-only, but if there is then not even a .set() operation it
2325          * is pretty tricky to drive the output line.
2326          */
2327         if (!gc->set && !gc->direction_output) {
2328                 gpiod_warn(desc,
2329                            "%s: missing set() and direction_output() operations\n",
2330                            __func__);
2331                 return -EIO;
2332         }
2333
2334         if (gc->direction_output) {
2335                 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
2336         } else {
2337                 /* Check that we are in output mode if we can */
2338                 if (gc->get_direction &&
2339                     gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
2340                         gpiod_warn(desc,
2341                                 "%s: missing direction_output() operation\n",
2342                                 __func__);
2343                         return -EIO;
2344                 }
2345                 /*
2346                  * If we can't actively set the direction, we are some
2347                  * output-only chip, so just drive the output as desired.
2348                  */
2349                 gc->set(gc, gpio_chip_hwgpio(desc), val);
2350         }
2351
2352         if (!ret)
2353                 set_bit(FLAG_IS_OUT, &desc->flags);
2354         trace_gpio_value(desc_to_gpio(desc), 0, val);
2355         trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2356         return ret;
2357 }
2358
2359 /**
2360  * gpiod_direction_output_raw - set the GPIO direction to output
2361  * @desc:       GPIO to set to output
2362  * @value:      initial output value of the GPIO
2363  *
2364  * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2365  * be called safely on it. The initial value of the output must be specified
2366  * as raw value on the physical line without regard for the ACTIVE_LOW status.
2367  *
2368  * Return 0 in case of success, else an error code.
2369  */
2370 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2371 {
2372         VALIDATE_DESC(desc);
2373         return gpiod_direction_output_raw_commit(desc, value);
2374 }
2375 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2376
2377 /**
2378  * gpiod_direction_output - set the GPIO direction to output
2379  * @desc:       GPIO to set to output
2380  * @value:      initial output value of the GPIO
2381  *
2382  * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2383  * be called safely on it. The initial value of the output must be specified
2384  * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2385  * account.
2386  *
2387  * Return 0 in case of success, else an error code.
2388  */
2389 int gpiod_direction_output(struct gpio_desc *desc, int value)
2390 {
2391         int ret;
2392
2393         VALIDATE_DESC(desc);
2394         if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2395                 value = !value;
2396         else
2397                 value = !!value;
2398
2399         /* GPIOs used for enabled IRQs shall not be set as output */
2400         if (dont_test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
2401             dont_test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
2402                 gpiod_err(desc,
2403                           "%s: tried to set a GPIO tied to an IRQ as output\n",
2404                           __func__);
2405                 return -EIO;
2406         }
2407
2408         if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2409                 /* First see if we can enable open drain in hardware */
2410                 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_DRAIN);
2411                 if (!ret)
2412                         goto set_output_value;
2413                 /* Emulate open drain by not actively driving the line high */
2414                 if (value) {
2415                         ret = gpiod_direction_input(desc);
2416                         goto set_output_flag;
2417                 }
2418         } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2419                 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE);
2420                 if (!ret)
2421                         goto set_output_value;
2422                 /* Emulate open source by not actively driving the line low */
2423                 if (!value) {
2424                         ret = gpiod_direction_input(desc);
2425                         goto set_output_flag;
2426                 }
2427         } else {
2428                 gpio_set_config(desc, PIN_CONFIG_DRIVE_PUSH_PULL);
2429         }
2430
2431 set_output_value:
2432         ret = gpio_set_bias(desc);
2433         if (ret)
2434                 return ret;
2435         return gpiod_direction_output_raw_commit(desc, value);
2436
2437 set_output_flag:
2438         /*
2439          * When emulating open-source or open-drain functionalities by not
2440          * actively driving the line (setting mode to input) we still need to
2441          * set the IS_OUT flag or otherwise we won't be able to set the line
2442          * value anymore.
2443          */
2444         if (ret == 0)
2445                 set_bit(FLAG_IS_OUT, &desc->flags);
2446         return ret;
2447 }
2448 EXPORT_SYMBOL_GPL(gpiod_direction_output);
2449
2450 /**
2451  * gpiod_set_config - sets @config for a GPIO
2452  * @desc: descriptor of the GPIO for which to set the configuration
2453  * @config: Same packed config format as generic pinconf
2454  *
2455  * Returns:
2456  * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2457  * configuration.
2458  */
2459 int gpiod_set_config(struct gpio_desc *desc, unsigned long config)
2460 {
2461         struct gpio_chip *gc;
2462
2463         VALIDATE_DESC(desc);
2464         gc = desc->gdev->chip;
2465
2466         return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2467 }
2468 EXPORT_SYMBOL_GPL(gpiod_set_config);
2469
2470 /**
2471  * gpiod_set_debounce - sets @debounce time for a GPIO
2472  * @desc: descriptor of the GPIO for which to set debounce time
2473  * @debounce: debounce time in microseconds
2474  *
2475  * Returns:
2476  * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2477  * debounce time.
2478  */
2479 int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce)
2480 {
2481         unsigned long config;
2482
2483         config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2484         return gpiod_set_config(desc, config);
2485 }
2486 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
2487
2488 /**
2489  * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2490  * @desc: descriptor of the GPIO for which to configure persistence
2491  * @transitory: True to lose state on suspend or reset, false for persistence
2492  *
2493  * Returns:
2494  * 0 on success, otherwise a negative error code.
2495  */
2496 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2497 {
2498         VALIDATE_DESC(desc);
2499         /*
2500          * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2501          * persistence state.
2502          */
2503         assign_bit(FLAG_TRANSITORY, &desc->flags, transitory);
2504
2505         /* If the driver supports it, set the persistence state now */
2506         return gpio_set_config_with_argument_optional(desc,
2507                                                       PIN_CONFIG_PERSIST_STATE,
2508                                                       !transitory);
2509 }
2510 EXPORT_SYMBOL_GPL(gpiod_set_transitory);
2511
2512 /**
2513  * gpiod_is_active_low - test whether a GPIO is active-low or not
2514  * @desc: the gpio descriptor to test
2515  *
2516  * Returns 1 if the GPIO is active-low, 0 otherwise.
2517  */
2518 int gpiod_is_active_low(const struct gpio_desc *desc)
2519 {
2520         VALIDATE_DESC(desc);
2521         return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
2522 }
2523 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
2524
2525 /**
2526  * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
2527  * @desc: the gpio descriptor to change
2528  */
2529 void gpiod_toggle_active_low(struct gpio_desc *desc)
2530 {
2531         VALIDATE_DESC_VOID(desc);
2532         change_bit(FLAG_ACTIVE_LOW, &desc->flags);
2533 }
2534 EXPORT_SYMBOL_GPL(gpiod_toggle_active_low);
2535
2536 /* I/O calls are only valid after configuration completed; the relevant
2537  * "is this a valid GPIO" error checks should already have been done.
2538  *
2539  * "Get" operations are often inlinable as reading a pin value register,
2540  * and masking the relevant bit in that register.
2541  *
2542  * When "set" operations are inlinable, they involve writing that mask to
2543  * one register to set a low value, or a different register to set it high.
2544  * Otherwise locking is needed, so there may be little value to inlining.
2545  *
2546  *------------------------------------------------------------------------
2547  *
2548  * IMPORTANT!!!  The hot paths -- get/set value -- assume that callers
2549  * have requested the GPIO.  That can include implicit requesting by
2550  * a direction setting call.  Marking a gpio as requested locks its chip
2551  * in memory, guaranteeing that these table lookups need no more locking
2552  * and that gpiochip_remove() will fail.
2553  *
2554  * REVISIT when debugging, consider adding some instrumentation to ensure
2555  * that the GPIO was actually requested.
2556  */
2557
2558 static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
2559 {
2560         struct gpio_chip        *gc;
2561         int offset;
2562         int value;
2563
2564         gc = desc->gdev->chip;
2565         offset = gpio_chip_hwgpio(desc);
2566         value = gc->get ? gc->get(gc, offset) : -EIO;
2567         value = value < 0 ? value : !!value;
2568         trace_gpio_value(desc_to_gpio(desc), 1, value);
2569         return value;
2570 }
2571
2572 static int gpio_chip_get_multiple(struct gpio_chip *gc,
2573                                   unsigned long *mask, unsigned long *bits)
2574 {
2575         if (gc->get_multiple)
2576                 return gc->get_multiple(gc, mask, bits);
2577         if (gc->get) {
2578                 int i, value;
2579
2580                 for_each_set_bit(i, mask, gc->ngpio) {
2581                         value = gc->get(gc, i);
2582                         if (value < 0)
2583                                 return value;
2584                         __assign_bit(i, bits, value);
2585                 }
2586                 return 0;
2587         }
2588         return -EIO;
2589 }
2590
2591 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2592                                   unsigned int array_size,
2593                                   struct gpio_desc **desc_array,
2594                                   struct gpio_array *array_info,
2595                                   unsigned long *value_bitmap)
2596 {
2597         int ret, i = 0;
2598
2599         /*
2600          * Validate array_info against desc_array and its size.
2601          * It should immediately follow desc_array if both
2602          * have been obtained from the same gpiod_get_array() call.
2603          */
2604         if (array_info && array_info->desc == desc_array &&
2605             array_size <= array_info->size &&
2606             (void *)array_info == desc_array + array_info->size) {
2607                 if (!can_sleep)
2608                         WARN_ON(array_info->chip->can_sleep);
2609
2610                 ret = gpio_chip_get_multiple(array_info->chip,
2611                                              array_info->get_mask,
2612                                              value_bitmap);
2613                 if (ret)
2614                         return ret;
2615
2616                 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2617                         bitmap_xor(value_bitmap, value_bitmap,
2618                                    array_info->invert_mask, array_size);
2619
2620                 i = find_first_zero_bit(array_info->get_mask, array_size);
2621                 if (i == array_size)
2622                         return 0;
2623         } else {
2624                 array_info = NULL;
2625         }
2626
2627         while (i < array_size) {
2628                 struct gpio_chip *gc = desc_array[i]->gdev->chip;
2629                 DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO);
2630                 DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO);
2631                 unsigned long *mask, *bits;
2632                 int first, j;
2633
2634                 if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
2635                         mask = fastpath_mask;
2636                         bits = fastpath_bits;
2637                 } else {
2638                         gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC;
2639
2640                         mask = bitmap_alloc(gc->ngpio, flags);
2641                         if (!mask)
2642                                 return -ENOMEM;
2643
2644                         bits = bitmap_alloc(gc->ngpio, flags);
2645                         if (!bits) {
2646                                 bitmap_free(mask);
2647                                 return -ENOMEM;
2648                         }
2649                 }
2650
2651                 bitmap_zero(mask, gc->ngpio);
2652
2653                 if (!can_sleep)
2654                         WARN_ON(gc->can_sleep);
2655
2656                 /* collect all inputs belonging to the same chip */
2657                 first = i;
2658                 do {
2659                         const struct gpio_desc *desc = desc_array[i];
2660                         int hwgpio = gpio_chip_hwgpio(desc);
2661
2662                         __set_bit(hwgpio, mask);
2663                         i++;
2664
2665                         if (array_info)
2666                                 i = find_next_zero_bit(array_info->get_mask,
2667                                                        array_size, i);
2668                 } while ((i < array_size) &&
2669                          (desc_array[i]->gdev->chip == gc));
2670
2671                 ret = gpio_chip_get_multiple(gc, mask, bits);
2672                 if (ret) {
2673                         if (mask != fastpath_mask)
2674                                 bitmap_free(mask);
2675                         if (bits != fastpath_bits)
2676                                 bitmap_free(bits);
2677                         return ret;
2678                 }
2679
2680                 for (j = first; j < i; ) {
2681                         const struct gpio_desc *desc = desc_array[j];
2682                         int hwgpio = gpio_chip_hwgpio(desc);
2683                         int value = test_bit(hwgpio, bits);
2684
2685                         if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2686                                 value = !value;
2687                         __assign_bit(j, value_bitmap, value);
2688                         trace_gpio_value(desc_to_gpio(desc), 1, value);
2689                         j++;
2690
2691                         if (array_info)
2692                                 j = find_next_zero_bit(array_info->get_mask, i,
2693                                                        j);
2694                 }
2695
2696                 if (mask != fastpath_mask)
2697                         bitmap_free(mask);
2698                 if (bits != fastpath_bits)
2699                         bitmap_free(bits);
2700         }
2701         return 0;
2702 }
2703
2704 /**
2705  * gpiod_get_raw_value() - return a gpio's raw value
2706  * @desc: gpio whose value will be returned
2707  *
2708  * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2709  * its ACTIVE_LOW status, or negative errno on failure.
2710  *
2711  * This function can be called from contexts where we cannot sleep, and will
2712  * complain if the GPIO chip functions potentially sleep.
2713  */
2714 int gpiod_get_raw_value(const struct gpio_desc *desc)
2715 {
2716         VALIDATE_DESC(desc);
2717         /* Should be using gpiod_get_raw_value_cansleep() */
2718         WARN_ON(desc->gdev->chip->can_sleep);
2719         return gpiod_get_raw_value_commit(desc);
2720 }
2721 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
2722
2723 /**
2724  * gpiod_get_value() - return a gpio's value
2725  * @desc: gpio whose value will be returned
2726  *
2727  * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2728  * account, or negative errno on failure.
2729  *
2730  * This function can be called from contexts where we cannot sleep, and will
2731  * complain if the GPIO chip functions potentially sleep.
2732  */
2733 int gpiod_get_value(const struct gpio_desc *desc)
2734 {
2735         int value;
2736
2737         VALIDATE_DESC(desc);
2738         /* Should be using gpiod_get_value_cansleep() */
2739         WARN_ON(desc->gdev->chip->can_sleep);
2740
2741         value = gpiod_get_raw_value_commit(desc);
2742         if (value < 0)
2743                 return value;
2744
2745         if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2746                 value = !value;
2747
2748         return value;
2749 }
2750 EXPORT_SYMBOL_GPL(gpiod_get_value);
2751
2752 /**
2753  * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2754  * @array_size: number of elements in the descriptor array / value bitmap
2755  * @desc_array: array of GPIO descriptors whose values will be read
2756  * @array_info: information on applicability of fast bitmap processing path
2757  * @value_bitmap: bitmap to store the read values
2758  *
2759  * Read the raw values of the GPIOs, i.e. the values of the physical lines
2760  * without regard for their ACTIVE_LOW status.  Return 0 in case of success,
2761  * else an error code.
2762  *
2763  * This function can be called from contexts where we cannot sleep,
2764  * and it will complain if the GPIO chip functions potentially sleep.
2765  */
2766 int gpiod_get_raw_array_value(unsigned int array_size,
2767                               struct gpio_desc **desc_array,
2768                               struct gpio_array *array_info,
2769                               unsigned long *value_bitmap)
2770 {
2771         if (!desc_array)
2772                 return -EINVAL;
2773         return gpiod_get_array_value_complex(true, false, array_size,
2774                                              desc_array, array_info,
2775                                              value_bitmap);
2776 }
2777 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2778
2779 /**
2780  * gpiod_get_array_value() - read values from an array of GPIOs
2781  * @array_size: number of elements in the descriptor array / value bitmap
2782  * @desc_array: array of GPIO descriptors whose values will be read
2783  * @array_info: information on applicability of fast bitmap processing path
2784  * @value_bitmap: bitmap to store the read values
2785  *
2786  * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2787  * into account.  Return 0 in case of success, else an error code.
2788  *
2789  * This function can be called from contexts where we cannot sleep,
2790  * and it will complain if the GPIO chip functions potentially sleep.
2791  */
2792 int gpiod_get_array_value(unsigned int array_size,
2793                           struct gpio_desc **desc_array,
2794                           struct gpio_array *array_info,
2795                           unsigned long *value_bitmap)
2796 {
2797         if (!desc_array)
2798                 return -EINVAL;
2799         return gpiod_get_array_value_complex(false, false, array_size,
2800                                              desc_array, array_info,
2801                                              value_bitmap);
2802 }
2803 EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2804
2805 /*
2806  *  gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
2807  * @desc: gpio descriptor whose state need to be set.
2808  * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2809  */
2810 static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
2811 {
2812         int ret = 0;
2813         struct gpio_chip *gc = desc->gdev->chip;
2814         int offset = gpio_chip_hwgpio(desc);
2815
2816         if (value) {
2817                 ret = gc->direction_input(gc, offset);
2818         } else {
2819                 ret = gc->direction_output(gc, offset, 0);
2820                 if (!ret)
2821                         set_bit(FLAG_IS_OUT, &desc->flags);
2822         }
2823         trace_gpio_direction(desc_to_gpio(desc), value, ret);
2824         if (ret < 0)
2825                 gpiod_err(desc,
2826                           "%s: Error in set_value for open drain err %d\n",
2827                           __func__, ret);
2828 }
2829
2830 /*
2831  *  _gpio_set_open_source_value() - Set the open source gpio's value.
2832  * @desc: gpio descriptor whose state need to be set.
2833  * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2834  */
2835 static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
2836 {
2837         int ret = 0;
2838         struct gpio_chip *gc = desc->gdev->chip;
2839         int offset = gpio_chip_hwgpio(desc);
2840
2841         if (value) {
2842                 ret = gc->direction_output(gc, offset, 1);
2843                 if (!ret)
2844                         set_bit(FLAG_IS_OUT, &desc->flags);
2845         } else {
2846                 ret = gc->direction_input(gc, offset);
2847         }
2848         trace_gpio_direction(desc_to_gpio(desc), !value, ret);
2849         if (ret < 0)
2850                 gpiod_err(desc,
2851                           "%s: Error in set_value for open source err %d\n",
2852                           __func__, ret);
2853 }
2854
2855 static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
2856 {
2857         struct gpio_chip        *gc;
2858
2859         gc = desc->gdev->chip;
2860         trace_gpio_value(desc_to_gpio(desc), 0, value);
2861         gc->set(gc, gpio_chip_hwgpio(desc), value);
2862 }
2863
2864 /*
2865  * set multiple outputs on the same chip;
2866  * use the chip's set_multiple function if available;
2867  * otherwise set the outputs sequentially;
2868  * @chip: the GPIO chip we operate on
2869  * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2870  *        defines which outputs are to be changed
2871  * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2872  *        defines the values the outputs specified by mask are to be set to
2873  */
2874 static void gpio_chip_set_multiple(struct gpio_chip *gc,
2875                                    unsigned long *mask, unsigned long *bits)
2876 {
2877         if (gc->set_multiple) {
2878                 gc->set_multiple(gc, mask, bits);
2879         } else {
2880                 unsigned int i;
2881
2882                 /* set outputs if the corresponding mask bit is set */
2883                 for_each_set_bit(i, mask, gc->ngpio)
2884                         gc->set(gc, i, test_bit(i, bits));
2885         }
2886 }
2887
2888 int gpiod_set_array_value_complex(bool raw, bool can_sleep,
2889                                   unsigned int array_size,
2890                                   struct gpio_desc **desc_array,
2891                                   struct gpio_array *array_info,
2892                                   unsigned long *value_bitmap)
2893 {
2894         int i = 0;
2895
2896         /*
2897          * Validate array_info against desc_array and its size.
2898          * It should immediately follow desc_array if both
2899          * have been obtained from the same gpiod_get_array() call.
2900          */
2901         if (array_info && array_info->desc == desc_array &&
2902             array_size <= array_info->size &&
2903             (void *)array_info == desc_array + array_info->size) {
2904                 if (!can_sleep)
2905                         WARN_ON(array_info->chip->can_sleep);
2906
2907                 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2908                         bitmap_xor(value_bitmap, value_bitmap,
2909                                    array_info->invert_mask, array_size);
2910
2911                 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
2912                                        value_bitmap);
2913
2914                 i = find_first_zero_bit(array_info->set_mask, array_size);
2915                 if (i == array_size)
2916                         return 0;
2917         } else {
2918                 array_info = NULL;
2919         }
2920
2921         while (i < array_size) {
2922                 struct gpio_chip *gc = desc_array[i]->gdev->chip;
2923                 DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO);
2924                 DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO);
2925                 unsigned long *mask, *bits;
2926                 int count = 0;
2927
2928                 if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
2929                         mask = fastpath_mask;
2930                         bits = fastpath_bits;
2931                 } else {
2932                         gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC;
2933
2934                         mask = bitmap_alloc(gc->ngpio, flags);
2935                         if (!mask)
2936                                 return -ENOMEM;
2937
2938                         bits = bitmap_alloc(gc->ngpio, flags);
2939                         if (!bits) {
2940                                 bitmap_free(mask);
2941                                 return -ENOMEM;
2942                         }
2943                 }
2944
2945                 bitmap_zero(mask, gc->ngpio);
2946
2947                 if (!can_sleep)
2948                         WARN_ON(gc->can_sleep);
2949
2950                 do {
2951                         struct gpio_desc *desc = desc_array[i];
2952                         int hwgpio = gpio_chip_hwgpio(desc);
2953                         int value = test_bit(i, value_bitmap);
2954
2955                         /*
2956                          * Pins applicable for fast input but not for
2957                          * fast output processing may have been already
2958                          * inverted inside the fast path, skip them.
2959                          */
2960                         if (!raw && !(array_info &&
2961                             test_bit(i, array_info->invert_mask)) &&
2962                             test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2963                                 value = !value;
2964                         trace_gpio_value(desc_to_gpio(desc), 0, value);
2965                         /*
2966                          * collect all normal outputs belonging to the same chip
2967                          * open drain and open source outputs are set individually
2968                          */
2969                         if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
2970                                 gpio_set_open_drain_value_commit(desc, value);
2971                         } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
2972                                 gpio_set_open_source_value_commit(desc, value);
2973                         } else {
2974                                 __set_bit(hwgpio, mask);
2975                                 __assign_bit(hwgpio, bits, value);
2976                                 count++;
2977                         }
2978                         i++;
2979
2980                         if (array_info)
2981                                 i = find_next_zero_bit(array_info->set_mask,
2982                                                        array_size, i);
2983                 } while ((i < array_size) &&
2984                          (desc_array[i]->gdev->chip == gc));
2985                 /* push collected bits to outputs */
2986                 if (count != 0)
2987                         gpio_chip_set_multiple(gc, mask, bits);
2988
2989                 if (mask != fastpath_mask)
2990                         bitmap_free(mask);
2991                 if (bits != fastpath_bits)
2992                         bitmap_free(bits);
2993         }
2994         return 0;
2995 }
2996
2997 /**
2998  * gpiod_set_raw_value() - assign a gpio's raw value
2999  * @desc: gpio whose value will be assigned
3000  * @value: value to assign
3001  *
3002  * Set the raw value of the GPIO, i.e. the value of its physical line without
3003  * regard for its ACTIVE_LOW status.
3004  *
3005  * This function can be called from contexts where we cannot sleep, and will
3006  * complain if the GPIO chip functions potentially sleep.
3007  */
3008 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
3009 {
3010         VALIDATE_DESC_VOID(desc);
3011         /* Should be using gpiod_set_raw_value_cansleep() */
3012         WARN_ON(desc->gdev->chip->can_sleep);
3013         gpiod_set_raw_value_commit(desc, value);
3014 }
3015 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
3016
3017 /**
3018  * gpiod_set_value_nocheck() - set a GPIO line value without checking
3019  * @desc: the descriptor to set the value on
3020  * @value: value to set
3021  *
3022  * This sets the value of a GPIO line backing a descriptor, applying
3023  * different semantic quirks like active low and open drain/source
3024  * handling.
3025  */
3026 static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3027 {
3028         if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3029                 value = !value;
3030         if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3031                 gpio_set_open_drain_value_commit(desc, value);
3032         else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3033                 gpio_set_open_source_value_commit(desc, value);
3034         else
3035                 gpiod_set_raw_value_commit(desc, value);
3036 }
3037
3038 /**
3039  * gpiod_set_value() - assign a gpio's value
3040  * @desc: gpio whose value will be assigned
3041  * @value: value to assign
3042  *
3043  * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3044  * OPEN_DRAIN and OPEN_SOURCE flags into account.
3045  *
3046  * This function can be called from contexts where we cannot sleep, and will
3047  * complain if the GPIO chip functions potentially sleep.
3048  */
3049 void gpiod_set_value(struct gpio_desc *desc, int value)
3050 {
3051         VALIDATE_DESC_VOID(desc);
3052         /* Should be using gpiod_set_value_cansleep() */
3053         WARN_ON(desc->gdev->chip->can_sleep);
3054         gpiod_set_value_nocheck(desc, value);
3055 }
3056 EXPORT_SYMBOL_GPL(gpiod_set_value);
3057
3058 /**
3059  * gpiod_set_raw_array_value() - assign values to an array of GPIOs
3060  * @array_size: number of elements in the descriptor array / value bitmap
3061  * @desc_array: array of GPIO descriptors whose values will be assigned
3062  * @array_info: information on applicability of fast bitmap processing path
3063  * @value_bitmap: bitmap of values to assign
3064  *
3065  * Set the raw values of the GPIOs, i.e. the values of the physical lines
3066  * without regard for their ACTIVE_LOW status.
3067  *
3068  * This function can be called from contexts where we cannot sleep, and will
3069  * complain if the GPIO chip functions potentially sleep.
3070  */
3071 int gpiod_set_raw_array_value(unsigned int array_size,
3072                               struct gpio_desc **desc_array,
3073                               struct gpio_array *array_info,
3074                               unsigned long *value_bitmap)
3075 {
3076         if (!desc_array)
3077                 return -EINVAL;
3078         return gpiod_set_array_value_complex(true, false, array_size,
3079                                         desc_array, array_info, value_bitmap);
3080 }
3081 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
3082
3083 /**
3084  * gpiod_set_array_value() - assign values to an array of GPIOs
3085  * @array_size: number of elements in the descriptor array / value bitmap
3086  * @desc_array: array of GPIO descriptors whose values will be assigned
3087  * @array_info: information on applicability of fast bitmap processing path
3088  * @value_bitmap: bitmap of values to assign
3089  *
3090  * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3091  * into account.
3092  *
3093  * This function can be called from contexts where we cannot sleep, and will
3094  * complain if the GPIO chip functions potentially sleep.
3095  */
3096 int gpiod_set_array_value(unsigned int array_size,
3097                           struct gpio_desc **desc_array,
3098                           struct gpio_array *array_info,
3099                           unsigned long *value_bitmap)
3100 {
3101         if (!desc_array)
3102                 return -EINVAL;
3103         return gpiod_set_array_value_complex(false, false, array_size,
3104                                              desc_array, array_info,
3105                                              value_bitmap);
3106 }
3107 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
3108
3109 /**
3110  * gpiod_cansleep() - report whether gpio value access may sleep
3111  * @desc: gpio to check
3112  *
3113  */
3114 int gpiod_cansleep(const struct gpio_desc *desc)
3115 {
3116         VALIDATE_DESC(desc);
3117         return desc->gdev->chip->can_sleep;
3118 }
3119 EXPORT_SYMBOL_GPL(gpiod_cansleep);
3120
3121 /**
3122  * gpiod_set_consumer_name() - set the consumer name for the descriptor
3123  * @desc: gpio to set the consumer name on
3124  * @name: the new consumer name
3125  */
3126 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
3127 {
3128         VALIDATE_DESC(desc);
3129         if (name) {
3130                 name = kstrdup_const(name, GFP_KERNEL);
3131                 if (!name)
3132                         return -ENOMEM;
3133         }
3134
3135         kfree_const(desc->label);
3136         desc_set_label(desc, name);
3137
3138         return 0;
3139 }
3140 EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3141
3142 /**
3143  * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3144  * @desc: gpio whose IRQ will be returned (already requested)
3145  *
3146  * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3147  * error.
3148  */
3149 int gpiod_to_irq(const struct gpio_desc *desc)
3150 {
3151         struct gpio_chip *gc;
3152         int offset;
3153
3154         /*
3155          * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3156          * requires this function to not return zero on an invalid descriptor
3157          * but rather a negative error number.
3158          */
3159         if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
3160                 return -EINVAL;
3161
3162         gc = desc->gdev->chip;
3163         offset = gpio_chip_hwgpio(desc);
3164         if (gc->to_irq) {
3165                 int retirq = gc->to_irq(gc, offset);
3166
3167                 /* Zero means NO_IRQ */
3168                 if (!retirq)
3169                         return -ENXIO;
3170
3171                 return retirq;
3172         }
3173 #ifdef CONFIG_GPIOLIB_IRQCHIP
3174         if (gc->irq.chip) {
3175                 /*
3176                  * Avoid race condition with other code, which tries to lookup
3177                  * an IRQ before the irqchip has been properly registered,
3178                  * i.e. while gpiochip is still being brought up.
3179                  */
3180                 return -EPROBE_DEFER;
3181         }
3182 #endif
3183         return -ENXIO;
3184 }
3185 EXPORT_SYMBOL_GPL(gpiod_to_irq);
3186
3187 /**
3188  * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3189  * @gc: the chip the GPIO to lock belongs to
3190  * @offset: the offset of the GPIO to lock as IRQ
3191  *
3192  * This is used directly by GPIO drivers that want to lock down
3193  * a certain GPIO line to be used for IRQs.
3194  */
3195 int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset)
3196 {
3197         struct gpio_desc *desc;
3198
3199         desc = gpiochip_get_desc(gc, offset);
3200         if (IS_ERR(desc))
3201                 return PTR_ERR(desc);
3202
3203         /*
3204          * If it's fast: flush the direction setting if something changed
3205          * behind our back
3206          */
3207         if (!gc->can_sleep && gc->get_direction) {
3208                 int dir = gpiod_get_direction(desc);
3209
3210                 if (dir < 0) {
3211                         chip_err(gc, "%s: cannot get GPIO direction\n",
3212                                  __func__);
3213                         return dir;
3214                 }
3215         }
3216
3217         /* To be valid for IRQ the line needs to be input or open drain */
3218         if (dont_test_bit(FLAG_IS_OUT, &desc->flags) &&
3219             !dont_test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
3220                 chip_err(gc,
3221                          "%s: tried to flag a GPIO set as output for IRQ\n",
3222                          __func__);
3223                 return -EIO;
3224         }
3225
3226         set_bit(FLAG_USED_AS_IRQ, &desc->flags);
3227         set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3228
3229         /*
3230          * If the consumer has not set up a label (such as when the
3231          * IRQ is referenced from .to_irq()) we set up a label here
3232          * so it is clear this is used as an interrupt.
3233          */
3234         if (!desc->label)
3235                 desc_set_label(desc, "interrupt");
3236
3237         return 0;
3238 }
3239 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
3240
3241 /**
3242  * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3243  * @gc: the chip the GPIO to lock belongs to
3244  * @offset: the offset of the GPIO to lock as IRQ
3245  *
3246  * This is used directly by GPIO drivers that want to indicate
3247  * that a certain GPIO is no longer used exclusively for IRQ.
3248  */
3249 void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset)
3250 {
3251         struct gpio_desc *desc;
3252
3253         desc = gpiochip_get_desc(gc, offset);
3254         if (IS_ERR(desc))
3255                 return;
3256
3257         clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3258         clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3259
3260         /* If we only had this marking, erase it */
3261         if (desc->label && !strcmp(desc->label, "interrupt"))
3262                 desc_set_label(desc, NULL);
3263 }
3264 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
3265
3266 void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset)
3267 {
3268         struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3269
3270         if (!IS_ERR(desc) &&
3271             !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
3272                 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3273 }
3274 EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
3275
3276 void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset)
3277 {
3278         struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3279
3280         if (!IS_ERR(desc) &&
3281             !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
3282                 /*
3283                  * We must not be output when using IRQ UNLESS we are
3284                  * open drain.
3285                  */
3286                 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) &&
3287                         !test_bit(FLAG_OPEN_DRAIN, &desc->flags));
3288                 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3289         }
3290 }
3291 EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
3292
3293 bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset)
3294 {
3295         if (offset >= gc->ngpio)
3296                 return false;
3297
3298         return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags);
3299 }
3300 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3301
3302 int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset)
3303 {
3304         int ret;
3305
3306         if (!try_module_get(gc->gpiodev->owner))
3307                 return -ENODEV;
3308
3309         ret = gpiochip_lock_as_irq(gc, offset);
3310         if (ret) {
3311                 chip_err(gc, "unable to lock HW IRQ %u for IRQ\n", offset);
3312                 module_put(gc->gpiodev->owner);
3313                 return ret;
3314         }
3315         return 0;
3316 }
3317 EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
3318
3319 void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset)
3320 {
3321         gpiochip_unlock_as_irq(gc, offset);
3322         module_put(gc->gpiodev->owner);
3323 }
3324 EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
3325
3326 bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset)
3327 {
3328         if (offset >= gc->ngpio)
3329                 return false;
3330
3331         return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags);
3332 }
3333 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3334
3335 bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset)
3336 {
3337         if (offset >= gc->ngpio)
3338                 return false;
3339
3340         return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags);
3341 }
3342 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3343
3344 bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset)
3345 {
3346         if (offset >= gc->ngpio)
3347                 return false;
3348
3349         return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags);
3350 }
3351 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3352
3353 /**
3354  * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3355  * @desc: gpio whose value will be returned
3356  *
3357  * Return the GPIO's raw value, i.e. the value of the physical line disregarding
3358  * its ACTIVE_LOW status, or negative errno on failure.
3359  *
3360  * This function is to be called from contexts that can sleep.
3361  */
3362 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
3363 {
3364         might_sleep_if(extra_checks);
3365         VALIDATE_DESC(desc);
3366         return gpiod_get_raw_value_commit(desc);
3367 }
3368 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
3369
3370 /**
3371  * gpiod_get_value_cansleep() - return a gpio's value
3372  * @desc: gpio whose value will be returned
3373  *
3374  * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3375  * account, or negative errno on failure.
3376  *
3377  * This function is to be called from contexts that can sleep.
3378  */
3379 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
3380 {
3381         int value;
3382
3383         might_sleep_if(extra_checks);
3384         VALIDATE_DESC(desc);
3385         value = gpiod_get_raw_value_commit(desc);
3386         if (value < 0)
3387                 return value;
3388
3389         if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3390                 value = !value;
3391
3392         return value;
3393 }
3394 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
3395
3396 /**
3397  * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3398  * @array_size: number of elements in the descriptor array / value bitmap
3399  * @desc_array: array of GPIO descriptors whose values will be read
3400  * @array_info: information on applicability of fast bitmap processing path
3401  * @value_bitmap: bitmap to store the read values
3402  *
3403  * Read the raw values of the GPIOs, i.e. the values of the physical lines
3404  * without regard for their ACTIVE_LOW status.  Return 0 in case of success,
3405  * else an error code.
3406  *
3407  * This function is to be called from contexts that can sleep.
3408  */
3409 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3410                                        struct gpio_desc **desc_array,
3411                                        struct gpio_array *array_info,
3412                                        unsigned long *value_bitmap)
3413 {
3414         might_sleep_if(extra_checks);
3415         if (!desc_array)
3416                 return -EINVAL;
3417         return gpiod_get_array_value_complex(true, true, array_size,
3418                                              desc_array, array_info,
3419                                              value_bitmap);
3420 }
3421 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3422
3423 /**
3424  * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3425  * @array_size: number of elements in the descriptor array / value bitmap
3426  * @desc_array: array of GPIO descriptors whose values will be read
3427  * @array_info: information on applicability of fast bitmap processing path
3428  * @value_bitmap: bitmap to store the read values
3429  *
3430  * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3431  * into account.  Return 0 in case of success, else an error code.
3432  *
3433  * This function is to be called from contexts that can sleep.
3434  */
3435 int gpiod_get_array_value_cansleep(unsigned int array_size,
3436                                    struct gpio_desc **desc_array,
3437                                    struct gpio_array *array_info,
3438                                    unsigned long *value_bitmap)
3439 {
3440         might_sleep_if(extra_checks);
3441         if (!desc_array)
3442                 return -EINVAL;
3443         return gpiod_get_array_value_complex(false, true, array_size,
3444                                              desc_array, array_info,
3445                                              value_bitmap);
3446 }
3447 EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3448
3449 /**
3450  * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3451  * @desc: gpio whose value will be assigned
3452  * @value: value to assign
3453  *
3454  * Set the raw value of the GPIO, i.e. the value of its physical line without
3455  * regard for its ACTIVE_LOW status.
3456  *
3457  * This function is to be called from contexts that can sleep.
3458  */
3459 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
3460 {
3461         might_sleep_if(extra_checks);
3462         VALIDATE_DESC_VOID(desc);
3463         gpiod_set_raw_value_commit(desc, value);
3464 }
3465 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
3466
3467 /**
3468  * gpiod_set_value_cansleep() - assign a gpio's value
3469  * @desc: gpio whose value will be assigned
3470  * @value: value to assign
3471  *
3472  * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3473  * account
3474  *
3475  * This function is to be called from contexts that can sleep.
3476  */
3477 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
3478 {
3479         might_sleep_if(extra_checks);
3480         VALIDATE_DESC_VOID(desc);
3481         gpiod_set_value_nocheck(desc, value);
3482 }
3483 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
3484
3485 /**
3486  * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
3487  * @array_size: number of elements in the descriptor array / value bitmap
3488  * @desc_array: array of GPIO descriptors whose values will be assigned
3489  * @array_info: information on applicability of fast bitmap processing path
3490  * @value_bitmap: bitmap of values to assign
3491  *
3492  * Set the raw values of the GPIOs, i.e. the values of the physical lines
3493  * without regard for their ACTIVE_LOW status.
3494  *
3495  * This function is to be called from contexts that can sleep.
3496  */
3497 int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3498                                        struct gpio_desc **desc_array,
3499                                        struct gpio_array *array_info,
3500                                        unsigned long *value_bitmap)
3501 {
3502         might_sleep_if(extra_checks);
3503         if (!desc_array)
3504                 return -EINVAL;
3505         return gpiod_set_array_value_complex(true, true, array_size, desc_array,
3506                                       array_info, value_bitmap);
3507 }
3508 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
3509
3510 /**
3511  * gpiod_add_lookup_tables() - register GPIO device consumers
3512  * @tables: list of tables of consumers to register
3513  * @n: number of tables in the list
3514  */
3515 void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3516 {
3517         unsigned int i;
3518
3519         mutex_lock(&gpio_lookup_lock);
3520
3521         for (i = 0; i < n; i++)
3522                 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3523
3524         mutex_unlock(&gpio_lookup_lock);
3525 }
3526
3527 /**
3528  * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
3529  * @array_size: number of elements in the descriptor array / value bitmap
3530  * @desc_array: array of GPIO descriptors whose values will be assigned
3531  * @array_info: information on applicability of fast bitmap processing path
3532  * @value_bitmap: bitmap of values to assign
3533  *
3534  * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3535  * into account.
3536  *
3537  * This function is to be called from contexts that can sleep.
3538  */
3539 int gpiod_set_array_value_cansleep(unsigned int array_size,
3540                                    struct gpio_desc **desc_array,
3541                                    struct gpio_array *array_info,
3542                                    unsigned long *value_bitmap)
3543 {
3544         might_sleep_if(extra_checks);
3545         if (!desc_array)
3546                 return -EINVAL;
3547         return gpiod_set_array_value_complex(false, true, array_size,
3548                                              desc_array, array_info,
3549                                              value_bitmap);
3550 }
3551 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
3552
3553 /**
3554  * gpiod_add_lookup_table() - register GPIO device consumers
3555  * @table: table of consumers to register
3556  */
3557 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
3558 {
3559         mutex_lock(&gpio_lookup_lock);
3560
3561         list_add_tail(&table->list, &gpio_lookup_list);
3562
3563         mutex_unlock(&gpio_lookup_lock);
3564 }
3565 EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
3566
3567 /**
3568  * gpiod_remove_lookup_table() - unregister GPIO device consumers
3569  * @table: table of consumers to unregister
3570  */
3571 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3572 {
3573         /* Nothing to remove */
3574         if (!table)
3575                 return;
3576
3577         mutex_lock(&gpio_lookup_lock);
3578
3579         list_del(&table->list);
3580
3581         mutex_unlock(&gpio_lookup_lock);
3582 }
3583 EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
3584
3585 /**
3586  * gpiod_add_hogs() - register a set of GPIO hogs from machine code
3587  * @hogs: table of gpio hog entries with a zeroed sentinel at the end
3588  */
3589 void gpiod_add_hogs(struct gpiod_hog *hogs)
3590 {
3591         struct gpio_chip *gc;
3592         struct gpiod_hog *hog;
3593
3594         mutex_lock(&gpio_machine_hogs_mutex);
3595
3596         for (hog = &hogs[0]; hog->chip_label; hog++) {
3597                 list_add_tail(&hog->list, &gpio_machine_hogs);
3598
3599                 /*
3600                  * The chip may have been registered earlier, so check if it
3601                  * exists and, if so, try to hog the line now.
3602                  */
3603                 gc = find_chip_by_name(hog->chip_label);
3604                 if (gc)
3605                         gpiochip_machine_hog(gc, hog);
3606         }
3607
3608         mutex_unlock(&gpio_machine_hogs_mutex);
3609 }
3610 EXPORT_SYMBOL_GPL(gpiod_add_hogs);
3611
3612 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3613 {
3614         const char *dev_id = dev ? dev_name(dev) : NULL;
3615         struct gpiod_lookup_table *table;
3616
3617         mutex_lock(&gpio_lookup_lock);
3618
3619         list_for_each_entry(table, &gpio_lookup_list, list) {
3620                 if (table->dev_id && dev_id) {
3621                         /*
3622                          * Valid strings on both ends, must be identical to have
3623                          * a match
3624                          */
3625                         if (!strcmp(table->dev_id, dev_id))
3626                                 goto found;
3627                 } else {
3628                         /*
3629                          * One of the pointers is NULL, so both must be to have
3630                          * a match
3631                          */
3632                         if (dev_id == table->dev_id)
3633                                 goto found;
3634                 }
3635         }
3636         table = NULL;
3637
3638 found:
3639         mutex_unlock(&gpio_lookup_lock);
3640         return table;
3641 }
3642
3643 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
3644                                     unsigned int idx, unsigned long *flags)
3645 {
3646         struct gpio_desc *desc = ERR_PTR(-ENOENT);
3647         struct gpiod_lookup_table *table;
3648         struct gpiod_lookup *p;
3649
3650         table = gpiod_find_lookup_table(dev);
3651         if (!table)
3652                 return desc;
3653
3654         for (p = &table->table[0]; p->key; p++) {
3655                 struct gpio_chip *gc;
3656
3657                 /* idx must always match exactly */
3658                 if (p->idx != idx)
3659                         continue;
3660
3661                 /* If the lookup entry has a con_id, require exact match */
3662                 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3663                         continue;
3664
3665                 if (p->chip_hwnum == U16_MAX) {
3666                         desc = gpio_name_to_desc(p->key);
3667                         if (desc) {
3668                                 *flags = p->flags;
3669                                 return desc;
3670                         }
3671
3672                         dev_warn(dev, "cannot find GPIO line %s, deferring\n",
3673                                  p->key);
3674                         return ERR_PTR(-EPROBE_DEFER);
3675                 }
3676
3677                 gc = find_chip_by_name(p->key);
3678
3679                 if (!gc) {
3680                         /*
3681                          * As the lookup table indicates a chip with
3682                          * p->key should exist, assume it may
3683                          * still appear later and let the interested
3684                          * consumer be probed again or let the Deferred
3685                          * Probe infrastructure handle the error.
3686                          */
3687                         dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
3688                                  p->key);
3689                         return ERR_PTR(-EPROBE_DEFER);
3690                 }
3691
3692                 if (gc->ngpio <= p->chip_hwnum) {
3693                         dev_err(dev,
3694                                 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
3695                                 idx, p->chip_hwnum, gc->ngpio - 1,
3696                                 gc->label);
3697                         return ERR_PTR(-EINVAL);
3698                 }
3699
3700                 desc = gpiochip_get_desc(gc, p->chip_hwnum);
3701                 *flags = p->flags;
3702
3703                 return desc;
3704         }
3705
3706         return desc;
3707 }
3708
3709 static int platform_gpio_count(struct device *dev, const char *con_id)
3710 {
3711         struct gpiod_lookup_table *table;
3712         struct gpiod_lookup *p;
3713         unsigned int count = 0;
3714
3715         table = gpiod_find_lookup_table(dev);
3716         if (!table)
3717                 return -ENOENT;
3718
3719         for (p = &table->table[0]; p->key; p++) {
3720                 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3721                     (!con_id && !p->con_id))
3722                         count++;
3723         }
3724         if (!count)
3725                 return -ENOENT;
3726
3727         return count;
3728 }
3729
3730 /**
3731  * fwnode_gpiod_get_index - obtain a GPIO from firmware node
3732  * @fwnode:     handle of the firmware node
3733  * @con_id:     function within the GPIO consumer
3734  * @index:      index of the GPIO to obtain for the consumer
3735  * @flags:      GPIO initialization flags
3736  * @label:      label to attach to the requested GPIO
3737  *
3738  * This function can be used for drivers that get their configuration
3739  * from opaque firmware.
3740  *
3741  * The function properly finds the corresponding GPIO using whatever is the
3742  * underlying firmware interface and then makes sure that the GPIO
3743  * descriptor is requested before it is returned to the caller.
3744  *
3745  * Returns:
3746  * On successful request the GPIO pin is configured in accordance with
3747  * provided @flags.
3748  *
3749  * In case of error an ERR_PTR() is returned.
3750  */
3751 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
3752                                          const char *con_id, int index,
3753                                          enum gpiod_flags flags,
3754                                          const char *label)
3755 {
3756         struct gpio_desc *desc;
3757         char prop_name[32]; /* 32 is max size of property name */
3758         unsigned int i;
3759
3760         for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3761                 if (con_id)
3762                         snprintf(prop_name, sizeof(prop_name), "%s-%s",
3763                                             con_id, gpio_suffixes[i]);
3764                 else
3765                         snprintf(prop_name, sizeof(prop_name), "%s",
3766                                             gpio_suffixes[i]);
3767
3768                 desc = fwnode_get_named_gpiod(fwnode, prop_name, index, flags,
3769                                               label);
3770                 if (!gpiod_not_found(desc))
3771                         break;
3772         }
3773
3774         return desc;
3775 }
3776 EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
3777
3778 /**
3779  * gpiod_count - return the number of GPIOs associated with a device / function
3780  *              or -ENOENT if no GPIO has been assigned to the requested function
3781  * @dev:        GPIO consumer, can be NULL for system-global GPIOs
3782  * @con_id:     function within the GPIO consumer
3783  */
3784 int gpiod_count(struct device *dev, const char *con_id)
3785 {
3786         const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
3787         int count = -ENOENT;
3788
3789         if (is_of_node(fwnode))
3790                 count = of_gpio_get_count(dev, con_id);
3791         else if (is_acpi_node(fwnode))
3792                 count = acpi_gpio_count(dev, con_id);
3793
3794         if (count < 0)
3795                 count = platform_gpio_count(dev, con_id);
3796
3797         return count;
3798 }
3799 EXPORT_SYMBOL_GPL(gpiod_count);
3800
3801 /**
3802  * gpiod_get - obtain a GPIO for a given GPIO function
3803  * @dev:        GPIO consumer, can be NULL for system-global GPIOs
3804  * @con_id:     function within the GPIO consumer
3805  * @flags:      optional GPIO initialization flags
3806  *
3807  * Return the GPIO descriptor corresponding to the function con_id of device
3808  * dev, -ENOENT if no GPIO has been assigned to the requested function, or
3809  * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
3810  */
3811 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
3812                                          enum gpiod_flags flags)
3813 {
3814         return gpiod_get_index(dev, con_id, 0, flags);
3815 }
3816 EXPORT_SYMBOL_GPL(gpiod_get);
3817
3818 /**
3819  * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3820  * @dev: GPIO consumer, can be NULL for system-global GPIOs
3821  * @con_id: function within the GPIO consumer
3822  * @flags: optional GPIO initialization flags
3823  *
3824  * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3825  * the requested function it will return NULL. This is convenient for drivers
3826  * that need to handle optional GPIOs.
3827  */
3828 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
3829                                                   const char *con_id,
3830                                                   enum gpiod_flags flags)
3831 {
3832         return gpiod_get_index_optional(dev, con_id, 0, flags);
3833 }
3834 EXPORT_SYMBOL_GPL(gpiod_get_optional);
3835
3836
3837 /**
3838  * gpiod_configure_flags - helper function to configure a given GPIO
3839  * @desc:       gpio whose value will be assigned
3840  * @con_id:     function within the GPIO consumer
3841  * @lflags:     bitmask of gpio_lookup_flags GPIO_* values - returned from
3842  *              of_find_gpio() or of_get_gpio_hog()
3843  * @dflags:     gpiod_flags - optional GPIO initialization flags
3844  *
3845  * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3846  * requested function and/or index, or another IS_ERR() code if an error
3847  * occurred while trying to acquire the GPIO.
3848  */
3849 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
3850                 unsigned long lflags, enum gpiod_flags dflags)
3851 {
3852         int ret;
3853
3854         if (lflags & GPIO_ACTIVE_LOW)
3855                 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3856
3857         if (lflags & GPIO_OPEN_DRAIN)
3858                 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3859         else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
3860                 /*
3861                  * This enforces open drain mode from the consumer side.
3862                  * This is necessary for some busses like I2C, but the lookup
3863                  * should *REALLY* have specified them as open drain in the
3864                  * first place, so print a little warning here.
3865                  */
3866                 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3867                 gpiod_warn(desc,
3868                            "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
3869         }
3870
3871         if (lflags & GPIO_OPEN_SOURCE)
3872                 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3873
3874         if ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) {
3875                 gpiod_err(desc,
3876                           "both pull-up and pull-down enabled, invalid configuration\n");
3877                 return -EINVAL;
3878         }
3879
3880         if (lflags & GPIO_PULL_UP)
3881                 set_bit(FLAG_PULL_UP, &desc->flags);
3882         else if (lflags & GPIO_PULL_DOWN)
3883                 set_bit(FLAG_PULL_DOWN, &desc->flags);
3884
3885         ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
3886         if (ret < 0)
3887                 return ret;
3888
3889         /* No particular flag request, return here... */
3890         if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3891                 gpiod_dbg(desc, "no flags found for %s\n", con_id);
3892                 return 0;
3893         }
3894
3895         /* Process flags */
3896         if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3897                 ret = gpiod_direction_output(desc,
3898                                 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
3899         else
3900                 ret = gpiod_direction_input(desc);
3901
3902         return ret;
3903 }
3904
3905 /**
3906  * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
3907  * @dev:        GPIO consumer, can be NULL for system-global GPIOs
3908  * @con_id:     function within the GPIO consumer
3909  * @idx:        index of the GPIO to obtain in the consumer
3910  * @flags:      optional GPIO initialization flags
3911  *
3912  * This variant of gpiod_get() allows to access GPIOs other than the first
3913  * defined one for functions that define several GPIOs.
3914  *
3915  * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3916  * requested function and/or index, or another IS_ERR() code if an error
3917  * occurred while trying to acquire the GPIO.
3918  */
3919 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
3920                                                const char *con_id,
3921                                                unsigned int idx,
3922                                                enum gpiod_flags flags)
3923 {
3924         unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
3925         struct gpio_desc *desc = NULL;
3926         int ret;
3927         /* Maybe we have a device name, maybe not */
3928         const char *devname = dev ? dev_name(dev) : "?";
3929         const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
3930
3931         dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3932
3933         /* Using device tree? */
3934         if (is_of_node(fwnode)) {
3935                 dev_dbg(dev, "using device tree for GPIO lookup\n");
3936                 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3937         } else if (is_acpi_node(fwnode)) {
3938                 dev_dbg(dev, "using ACPI for GPIO lookup\n");
3939                 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
3940         }
3941
3942         /*
3943          * Either we are not using DT or ACPI, or their lookup did not return
3944          * a result. In that case, use platform lookup as a fallback.
3945          */
3946         if (!desc || gpiod_not_found(desc)) {
3947                 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
3948                 desc = gpiod_find(dev, con_id, idx, &lookupflags);
3949         }
3950
3951         if (IS_ERR(desc)) {
3952                 dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
3953                 return desc;
3954         }
3955
3956         /*
3957          * If a connection label was passed use that, else attempt to use
3958          * the device name as label
3959          */
3960         ret = gpiod_request(desc, con_id ? con_id : devname);
3961         if (ret) {
3962                 if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
3963                         /*
3964                          * This happens when there are several consumers for
3965                          * the same GPIO line: we just return here without
3966                          * further initialization. It is a bit if a hack.
3967                          * This is necessary to support fixed regulators.
3968                          *
3969                          * FIXME: Make this more sane and safe.
3970                          */
3971                         dev_info(dev, "nonexclusive access to GPIO for %s\n",
3972                                  con_id ? con_id : devname);
3973                         return desc;
3974                 } else {
3975                         return ERR_PTR(ret);
3976                 }
3977         }
3978
3979         ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
3980         if (ret < 0) {
3981                 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3982                 gpiod_put(desc);
3983                 return ERR_PTR(ret);
3984         }
3985
3986         blocking_notifier_call_chain(&desc->gdev->notifier,
3987                                      GPIOLINE_CHANGED_REQUESTED, desc);
3988
3989         return desc;
3990 }
3991 EXPORT_SYMBOL_GPL(gpiod_get_index);
3992
3993 /**
3994  * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3995  * @fwnode:     handle of the firmware node
3996  * @propname:   name of the firmware property representing the GPIO
3997  * @index:      index of the GPIO to obtain for the consumer
3998  * @dflags:     GPIO initialization flags
3999  * @label:      label to attach to the requested GPIO
4000  *
4001  * This function can be used for drivers that get their configuration
4002  * from opaque firmware.
4003  *
4004  * The function properly finds the corresponding GPIO using whatever is the
4005  * underlying firmware interface and then makes sure that the GPIO
4006  * descriptor is requested before it is returned to the caller.
4007  *
4008  * Returns:
4009  * On successful request the GPIO pin is configured in accordance with
4010  * provided @dflags.
4011  *
4012  * In case of error an ERR_PTR() is returned.
4013  */
4014 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
4015                                          const char *propname, int index,
4016                                          enum gpiod_flags dflags,
4017                                          const char *label)
4018 {
4019         unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
4020         struct gpio_desc *desc = ERR_PTR(-ENODEV);
4021         int ret;
4022
4023         if (is_of_node(fwnode)) {
4024                 desc = gpiod_get_from_of_node(to_of_node(fwnode),
4025                                               propname, index,
4026                                               dflags,
4027                                               label);
4028                 return desc;
4029         } else if (is_acpi_node(fwnode)) {
4030                 struct acpi_gpio_info info;
4031
4032                 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
4033                 if (IS_ERR(desc))
4034                         return desc;
4035
4036                 acpi_gpio_update_gpiod_flags(&dflags, &info);
4037                 acpi_gpio_update_gpiod_lookup_flags(&lflags, &info);
4038         } else
4039                 return ERR_PTR(-EINVAL);
4040
4041         /* Currently only ACPI takes this path */
4042         ret = gpiod_request(desc, label);
4043         if (ret)
4044                 return ERR_PTR(ret);
4045
4046         ret = gpiod_configure_flags(desc, propname, lflags, dflags);
4047         if (ret < 0) {
4048                 gpiod_put(desc);
4049                 return ERR_PTR(ret);
4050         }
4051
4052         blocking_notifier_call_chain(&desc->gdev->notifier,
4053                                      GPIOLINE_CHANGED_REQUESTED, desc);
4054
4055         return desc;
4056 }
4057 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
4058
4059 /**
4060  * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
4061  *                            function
4062  * @dev: GPIO consumer, can be NULL for system-global GPIOs
4063  * @con_id: function within the GPIO consumer
4064  * @index: index of the GPIO to obtain in the consumer
4065  * @flags: optional GPIO initialization flags
4066  *
4067  * This is equivalent to gpiod_get_index(), except that when no GPIO with the
4068  * specified index was assigned to the requested function it will return NULL.
4069  * This is convenient for drivers that need to handle optional GPIOs.
4070  */
4071 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
4072                                                         const char *con_id,
4073                                                         unsigned int index,
4074                                                         enum gpiod_flags flags)
4075 {
4076         struct gpio_desc *desc;
4077
4078         desc = gpiod_get_index(dev, con_id, index, flags);
4079         if (gpiod_not_found(desc))
4080                 return NULL;
4081
4082         return desc;
4083 }
4084 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
4085
4086 /**
4087  * gpiod_hog - Hog the specified GPIO desc given the provided flags
4088  * @desc:       gpio whose value will be assigned
4089  * @name:       gpio line name
4090  * @lflags:     bitmask of gpio_lookup_flags GPIO_* values - returned from
4091  *              of_find_gpio() or of_get_gpio_hog()
4092  * @dflags:     gpiod_flags - optional GPIO initialization flags
4093  */
4094 int gpiod_hog(struct gpio_desc *desc, const char *name,
4095               unsigned long lflags, enum gpiod_flags dflags)
4096 {
4097         struct gpio_chip *gc;
4098         struct gpio_desc *local_desc;
4099         int hwnum;
4100         int ret;
4101
4102         gc = gpiod_to_chip(desc);
4103         hwnum = gpio_chip_hwgpio(desc);
4104
4105         local_desc = gpiochip_request_own_desc(gc, hwnum, name,
4106                                                lflags, dflags);
4107         if (IS_ERR(local_desc)) {
4108                 ret = PTR_ERR(local_desc);
4109                 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
4110                        name, gc->label, hwnum, ret);
4111                 return ret;
4112         }
4113
4114         /* Mark GPIO as hogged so it can be identified and removed later */
4115         set_bit(FLAG_IS_HOGGED, &desc->flags);
4116
4117         gpiod_info(desc, "hogged as %s%s\n",
4118                 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4119                 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ?
4120                   (dflags & GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low" : "");
4121
4122         return 0;
4123 }
4124
4125 /**
4126  * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4127  * @gc: gpio chip to act on
4128  */
4129 static void gpiochip_free_hogs(struct gpio_chip *gc)
4130 {
4131         int id;
4132
4133         for (id = 0; id < gc->ngpio; id++) {
4134                 if (test_bit(FLAG_IS_HOGGED, &gc->gpiodev->descs[id].flags))
4135                         gpiochip_free_own_desc(&gc->gpiodev->descs[id]);
4136         }
4137 }
4138
4139 /**
4140  * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4141  * @dev:        GPIO consumer, can be NULL for system-global GPIOs
4142  * @con_id:     function within the GPIO consumer
4143  * @flags:      optional GPIO initialization flags
4144  *
4145  * This function acquires all the GPIOs defined under a given function.
4146  *
4147  * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4148  * no GPIO has been assigned to the requested function, or another IS_ERR()
4149  * code if an error occurred while trying to acquire the GPIOs.
4150  */
4151 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4152                                                 const char *con_id,
4153                                                 enum gpiod_flags flags)
4154 {
4155         struct gpio_desc *desc;
4156         struct gpio_descs *descs;
4157         struct gpio_array *array_info = NULL;
4158         struct gpio_chip *gc;
4159         int count, bitmap_size;
4160
4161         count = gpiod_count(dev, con_id);
4162         if (count < 0)
4163                 return ERR_PTR(count);
4164
4165         descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
4166         if (!descs)
4167                 return ERR_PTR(-ENOMEM);
4168
4169         for (descs->ndescs = 0; descs->ndescs < count; ) {
4170                 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4171                 if (IS_ERR(desc)) {
4172                         gpiod_put_array(descs);
4173                         return ERR_CAST(desc);
4174                 }
4175
4176                 descs->desc[descs->ndescs] = desc;
4177
4178                 gc = gpiod_to_chip(desc);
4179                 /*
4180                  * If pin hardware number of array member 0 is also 0, select
4181                  * its chip as a candidate for fast bitmap processing path.
4182                  */
4183                 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
4184                         struct gpio_descs *array;
4185
4186                         bitmap_size = BITS_TO_LONGS(gc->ngpio > count ?
4187                                                     gc->ngpio : count);
4188
4189                         array = kzalloc(struct_size(descs, desc, count) +
4190                                         struct_size(array_info, invert_mask,
4191                                         3 * bitmap_size), GFP_KERNEL);
4192                         if (!array) {
4193                                 gpiod_put_array(descs);
4194                                 return ERR_PTR(-ENOMEM);
4195                         }
4196
4197                         memcpy(array, descs,
4198                                struct_size(descs, desc, descs->ndescs + 1));
4199                         kfree(descs);
4200
4201                         descs = array;
4202                         array_info = (void *)(descs->desc + count);
4203                         array_info->get_mask = array_info->invert_mask +
4204                                                   bitmap_size;
4205                         array_info->set_mask = array_info->get_mask +
4206                                                   bitmap_size;
4207
4208                         array_info->desc = descs->desc;
4209                         array_info->size = count;
4210                         array_info->chip = gc;
4211                         bitmap_set(array_info->get_mask, descs->ndescs,
4212                                    count - descs->ndescs);
4213                         bitmap_set(array_info->set_mask, descs->ndescs,
4214                                    count - descs->ndescs);
4215                         descs->info = array_info;
4216                 }
4217                 /* Unmark array members which don't belong to the 'fast' chip */
4218                 if (array_info && array_info->chip != gc) {
4219                         __clear_bit(descs->ndescs, array_info->get_mask);
4220                         __clear_bit(descs->ndescs, array_info->set_mask);
4221                 }
4222                 /*
4223                  * Detect array members which belong to the 'fast' chip
4224                  * but their pins are not in hardware order.
4225                  */
4226                 else if (array_info &&
4227                            gpio_chip_hwgpio(desc) != descs->ndescs) {
4228                         /*
4229                          * Don't use fast path if all array members processed so
4230                          * far belong to the same chip as this one but its pin
4231                          * hardware number is different from its array index.
4232                          */
4233                         if (bitmap_full(array_info->get_mask, descs->ndescs)) {
4234                                 array_info = NULL;
4235                         } else {
4236                                 __clear_bit(descs->ndescs,
4237                                             array_info->get_mask);
4238                                 __clear_bit(descs->ndescs,
4239                                             array_info->set_mask);
4240                         }
4241                 } else if (array_info) {
4242                         /* Exclude open drain or open source from fast output */
4243                         if (gpiochip_line_is_open_drain(gc, descs->ndescs) ||
4244                             gpiochip_line_is_open_source(gc, descs->ndescs))
4245                                 __clear_bit(descs->ndescs,
4246                                             array_info->set_mask);
4247                         /* Identify 'fast' pins which require invertion */
4248                         if (gpiod_is_active_low(desc))
4249                                 __set_bit(descs->ndescs,
4250                                           array_info->invert_mask);
4251                 }
4252
4253                 descs->ndescs++;
4254         }
4255         if (array_info)
4256                 dev_dbg(dev,
4257                         "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4258                         array_info->chip->label, array_info->size,
4259                         *array_info->get_mask, *array_info->set_mask,
4260                         *array_info->invert_mask);
4261         return descs;
4262 }
4263 EXPORT_SYMBOL_GPL(gpiod_get_array);
4264
4265 /**
4266  * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4267  *                            function
4268  * @dev:        GPIO consumer, can be NULL for system-global GPIOs
4269  * @con_id:     function within the GPIO consumer
4270  * @flags:      optional GPIO initialization flags
4271  *
4272  * This is equivalent to gpiod_get_array(), except that when no GPIO was
4273  * assigned to the requested function it will return NULL.
4274  */
4275 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4276                                                         const char *con_id,
4277                                                         enum gpiod_flags flags)
4278 {
4279         struct gpio_descs *descs;
4280
4281         descs = gpiod_get_array(dev, con_id, flags);
4282         if (gpiod_not_found(descs))
4283                 return NULL;
4284
4285         return descs;
4286 }
4287 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4288
4289 /**
4290  * gpiod_put - dispose of a GPIO descriptor
4291  * @desc:       GPIO descriptor to dispose of
4292  *
4293  * No descriptor can be used after gpiod_put() has been called on it.
4294  */
4295 void gpiod_put(struct gpio_desc *desc)
4296 {
4297         if (desc)
4298                 gpiod_free(desc);
4299 }
4300 EXPORT_SYMBOL_GPL(gpiod_put);
4301
4302 /**
4303  * gpiod_put_array - dispose of multiple GPIO descriptors
4304  * @descs:      struct gpio_descs containing an array of descriptors
4305  */
4306 void gpiod_put_array(struct gpio_descs *descs)
4307 {
4308         unsigned int i;
4309
4310         for (i = 0; i < descs->ndescs; i++)
4311                 gpiod_put(descs->desc[i]);
4312
4313         kfree(descs);
4314 }
4315 EXPORT_SYMBOL_GPL(gpiod_put_array);
4316
4317
4318 static int gpio_bus_match(struct device *dev, struct device_driver *drv)
4319 {
4320         struct fwnode_handle *fwnode = dev_fwnode(dev);
4321
4322         /*
4323          * Only match if the fwnode doesn't already have a proper struct device
4324          * created for it.
4325          */
4326         if (fwnode && fwnode->dev != dev)
4327                 return 0;
4328         return 1;
4329 }
4330
4331 static int gpio_stub_drv_probe(struct device *dev)
4332 {
4333         /*
4334          * The DT node of some GPIO chips have a "compatible" property, but
4335          * never have a struct device added and probed by a driver to register
4336          * the GPIO chip with gpiolib. In such cases, fw_devlink=on will cause
4337          * the consumers of the GPIO chip to get probe deferred forever because
4338          * they will be waiting for a device associated with the GPIO chip
4339          * firmware node to get added and bound to a driver.
4340          *
4341          * To allow these consumers to probe, we associate the struct
4342          * gpio_device of the GPIO chip with the firmware node and then simply
4343          * bind it to this stub driver.
4344          */
4345         return 0;
4346 }
4347
4348 static struct device_driver gpio_stub_drv = {
4349         .name = "gpio_stub_drv",
4350         .bus = &gpio_bus_type,
4351         .probe = gpio_stub_drv_probe,
4352 };
4353
4354 static int __init gpiolib_dev_init(void)
4355 {
4356         int ret;
4357
4358         /* Register GPIO sysfs bus */
4359         ret = bus_register(&gpio_bus_type);
4360         if (ret < 0) {
4361                 pr_err("gpiolib: could not register GPIO bus type\n");
4362                 return ret;
4363         }
4364
4365         ret = driver_register(&gpio_stub_drv);
4366         if (ret < 0) {
4367                 pr_err("gpiolib: could not register GPIO stub driver\n");
4368                 bus_unregister(&gpio_bus_type);
4369                 return ret;
4370         }
4371
4372         ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME);
4373         if (ret < 0) {
4374                 pr_err("gpiolib: failed to allocate char dev region\n");
4375                 driver_unregister(&gpio_stub_drv);
4376                 bus_unregister(&gpio_bus_type);
4377                 return ret;
4378         }
4379
4380         gpiolib_initialized = true;
4381         gpiochip_setup_devs();
4382
4383 #if IS_ENABLED(CONFIG_OF_DYNAMIC) && IS_ENABLED(CONFIG_OF_GPIO)
4384         WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier));
4385 #endif /* CONFIG_OF_DYNAMIC && CONFIG_OF_GPIO */
4386
4387         return ret;
4388 }
4389 core_initcall(gpiolib_dev_init);
4390
4391 #ifdef CONFIG_DEBUG_FS
4392
4393 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
4394 {
4395         unsigned                i;
4396         struct gpio_chip        *gc = gdev->chip;
4397         unsigned                gpio = gdev->base;
4398         struct gpio_desc        *gdesc = &gdev->descs[0];
4399         bool                    is_out;
4400         bool                    is_irq;
4401         bool                    active_low;
4402
4403         for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
4404                 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
4405                         if (gdesc->name) {
4406                                 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
4407                                            gpio, gdesc->name);
4408                         }
4409                         continue;
4410                 }
4411
4412                 gpiod_get_direction(gdesc);
4413                 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
4414                 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
4415                 active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags);
4416                 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s",
4417                         gpio, gdesc->name ? gdesc->name : "", gdesc->label,
4418                         is_out ? "out" : "in ",
4419                         gc->get ? (gc->get(gc, i) ? "hi" : "lo") : "?  ",
4420                         is_irq ? "IRQ " : "",
4421                         active_low ? "ACTIVE LOW" : "");
4422                 seq_printf(s, "\n");
4423         }
4424 }
4425
4426 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
4427 {
4428         unsigned long flags;
4429         struct gpio_device *gdev = NULL;
4430         loff_t index = *pos;
4431
4432         s->private = "";
4433
4434         spin_lock_irqsave(&gpio_lock, flags);
4435         list_for_each_entry(gdev, &gpio_devices, list)
4436                 if (index-- == 0) {
4437                         spin_unlock_irqrestore(&gpio_lock, flags);
4438                         return gdev;
4439                 }
4440         spin_unlock_irqrestore(&gpio_lock, flags);
4441
4442         return NULL;
4443 }
4444
4445 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4446 {
4447         unsigned long flags;
4448         struct gpio_device *gdev = v;
4449         void *ret = NULL;
4450
4451         spin_lock_irqsave(&gpio_lock, flags);
4452         if (list_is_last(&gdev->list, &gpio_devices))
4453                 ret = NULL;
4454         else
4455                 ret = list_entry(gdev->list.next, struct gpio_device, list);
4456         spin_unlock_irqrestore(&gpio_lock, flags);
4457
4458         s->private = "\n";
4459         ++*pos;
4460
4461         return ret;
4462 }
4463
4464 static void gpiolib_seq_stop(struct seq_file *s, void *v)
4465 {
4466 }
4467
4468 static int gpiolib_seq_show(struct seq_file *s, void *v)
4469 {
4470         struct gpio_device *gdev = v;
4471         struct gpio_chip *gc = gdev->chip;
4472         struct device *parent;
4473
4474         if (!gc) {
4475                 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4476                            dev_name(&gdev->dev));
4477                 return 0;
4478         }
4479
4480         seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4481                    dev_name(&gdev->dev),
4482                    gdev->base, gdev->base + gdev->ngpio - 1);
4483         parent = gc->parent;
4484         if (parent)
4485                 seq_printf(s, ", parent: %s/%s",
4486                            parent->bus ? parent->bus->name : "no-bus",
4487                            dev_name(parent));
4488         if (gc->label)
4489                 seq_printf(s, ", %s", gc->label);
4490         if (gc->can_sleep)
4491                 seq_printf(s, ", can sleep");
4492         seq_printf(s, ":\n");
4493
4494         if (gc->dbg_show)
4495                 gc->dbg_show(s, gc);
4496         else
4497                 gpiolib_dbg_show(s, gdev);
4498
4499         return 0;
4500 }
4501
4502 static const struct seq_operations gpiolib_sops = {
4503         .start = gpiolib_seq_start,
4504         .next = gpiolib_seq_next,
4505         .stop = gpiolib_seq_stop,
4506         .show = gpiolib_seq_show,
4507 };
4508 DEFINE_SEQ_ATTRIBUTE(gpiolib);
4509
4510 static int __init gpiolib_debugfs_init(void)
4511 {
4512         /* /sys/kernel/debug/gpio */
4513         debugfs_create_file("gpio", 0444, NULL, NULL, &gpiolib_fops);
4514         return 0;
4515 }
4516 subsys_initcall(gpiolib_debugfs_init);
4517
4518 #endif  /* DEBUG_FS */