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