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