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