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