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