1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/interrupt.h>
5 #include <linux/spinlock.h>
6 #include <linux/list.h>
7 #include <linux/device.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 #include <linux/idr.h>
20 #include <linux/cdev.h>
22 #include <linux/uaccess.h>
23 #include <uapi/linux/gpio.h>
27 #define CREATE_TRACE_POINTS
28 #include <trace/events/gpio.h>
30 /* Implementation infrastructure for GPIO interfaces.
32 * The GPIO programming interface allows for inlining speed-critical
33 * get/set operations for common cases, so that access to SOC-integrated
34 * GPIOs can sometimes cost only an instruction or two per bit.
38 /* When debugging, extend minimal trust to callers and platform code.
39 * Also emit diagnostic messages that may help initial bringup, when
40 * board setup or driver bugs are most common.
42 * Otherwise, minimize overhead in what may be bitbanging codepaths.
45 #define extra_checks 1
47 #define extra_checks 0
50 /* Device and char device-related information */
51 static DEFINE_IDA(gpio_ida
);
52 static dev_t gpio_devt
;
53 #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
54 static struct bus_type gpio_bus_type
= {
58 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
59 * While any GPIO is requested, its gpio_chip is not removable;
60 * each GPIO's "requested" flag serves as a lock and refcount.
62 DEFINE_SPINLOCK(gpio_lock
);
64 static DEFINE_MUTEX(gpio_lookup_lock
);
65 static LIST_HEAD(gpio_lookup_list
);
66 LIST_HEAD(gpio_devices
);
68 static void gpiochip_free_hogs(struct gpio_chip
*chip
);
69 static void gpiochip_irqchip_remove(struct gpio_chip
*gpiochip
);
72 static inline void desc_set_label(struct gpio_desc
*d
, const char *label
)
78 * Convert a GPIO number to its descriptor
80 struct gpio_desc
*gpio_to_desc(unsigned gpio
)
82 struct gpio_device
*gdev
;
85 spin_lock_irqsave(&gpio_lock
, flags
);
87 list_for_each_entry(gdev
, &gpio_devices
, list
) {
88 if (gdev
->base
<= gpio
&&
89 gdev
->base
+ gdev
->ngpio
> gpio
) {
90 spin_unlock_irqrestore(&gpio_lock
, flags
);
91 return &gdev
->descs
[gpio
- gdev
->base
];
95 spin_unlock_irqrestore(&gpio_lock
, flags
);
97 if (!gpio_is_valid(gpio
))
98 WARN(1, "invalid GPIO %d\n", gpio
);
102 EXPORT_SYMBOL_GPL(gpio_to_desc
);
105 * Get the GPIO descriptor corresponding to the given hw number for this chip.
107 struct gpio_desc
*gpiochip_get_desc(struct gpio_chip
*chip
,
110 struct gpio_device
*gdev
= chip
->gpiodev
;
112 if (hwnum
>= gdev
->ngpio
)
113 return ERR_PTR(-EINVAL
);
115 return &gdev
->descs
[hwnum
];
119 * Convert a GPIO descriptor to the integer namespace.
120 * This should disappear in the future but is needed since we still
121 * use GPIO numbers for error messages and sysfs nodes
123 int desc_to_gpio(const struct gpio_desc
*desc
)
125 return desc
->gdev
->base
+ (desc
- &desc
->gdev
->descs
[0]);
127 EXPORT_SYMBOL_GPL(desc_to_gpio
);
131 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
132 * @desc: descriptor to return the chip of
134 struct gpio_chip
*gpiod_to_chip(const struct gpio_desc
*desc
)
136 if (!desc
|| !desc
->gdev
|| !desc
->gdev
->chip
)
138 return desc
->gdev
->chip
;
140 EXPORT_SYMBOL_GPL(gpiod_to_chip
);
142 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
143 static int gpiochip_find_base(int ngpio
)
145 struct gpio_device
*gdev
;
146 int base
= ARCH_NR_GPIOS
- ngpio
;
148 list_for_each_entry_reverse(gdev
, &gpio_devices
, list
) {
149 /* found a free space? */
150 if (gdev
->base
+ gdev
->ngpio
<= base
)
153 /* nope, check the space right before the chip */
154 base
= gdev
->base
- ngpio
;
157 if (gpio_is_valid(base
)) {
158 pr_debug("%s: found new base at %d\n", __func__
, base
);
161 pr_err("%s: cannot find free range\n", __func__
);
167 * gpiod_get_direction - return the current direction of a GPIO
168 * @desc: GPIO to get the direction of
170 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
172 * This function may sleep if gpiod_cansleep() is true.
174 int gpiod_get_direction(struct gpio_desc
*desc
)
176 struct gpio_chip
*chip
;
178 int status
= -EINVAL
;
180 chip
= gpiod_to_chip(desc
);
181 offset
= gpio_chip_hwgpio(desc
);
183 if (!chip
->get_direction
)
186 status
= chip
->get_direction(chip
, offset
);
188 /* GPIOF_DIR_IN, or other positive */
190 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
194 set_bit(FLAG_IS_OUT
, &desc
->flags
);
198 EXPORT_SYMBOL_GPL(gpiod_get_direction
);
201 * Add a new chip to the global chips list, keeping the list of chips sorted
202 * by range(means [base, base + ngpio - 1]) order.
204 * Return -EBUSY if the new chip overlaps with some other chip's integer
207 static int gpiodev_add_to_list(struct gpio_device
*gdev
)
209 struct gpio_device
*iterator
;
210 struct gpio_device
*previous
= NULL
;
215 if (list_empty(&gpio_devices
)) {
216 list_add_tail(&gdev
->list
, &gpio_devices
);
220 list_for_each_entry(iterator
, &gpio_devices
, list
) {
221 if (iterator
->base
>= gdev
->base
+ gdev
->ngpio
) {
223 * Iterator is the first GPIO chip so there is no
230 * We found a valid range(means
231 * [base, base + ngpio - 1]) between previous
234 if (previous
->base
+ previous
->ngpio
243 * We are beyond the last chip in the list and iterator now
244 * points to the head.
245 * Let iterator point to the last chip in the list.
248 iterator
= list_last_entry(&gpio_devices
, struct gpio_device
, list
);
249 if (iterator
->base
+ iterator
->ngpio
<= gdev
->base
) {
250 list_add(&gdev
->list
, &iterator
->list
);
255 "GPIO integer space overlap, cannot add chip\n");
259 list_add_tail(&gdev
->list
, &iterator
->list
);
264 * Convert a GPIO name to its descriptor
266 static struct gpio_desc
*gpio_name_to_desc(const char * const name
)
268 struct gpio_device
*gdev
;
271 spin_lock_irqsave(&gpio_lock
, flags
);
273 list_for_each_entry(gdev
, &gpio_devices
, list
) {
276 for (i
= 0; i
!= gdev
->ngpio
; ++i
) {
277 struct gpio_desc
*desc
= &gdev
->descs
[i
];
279 if (!desc
->name
|| !name
)
282 if (!strcmp(desc
->name
, name
)) {
283 spin_unlock_irqrestore(&gpio_lock
, flags
);
289 spin_unlock_irqrestore(&gpio_lock
, flags
);
295 * Takes the names from gc->names and checks if they are all unique. If they
296 * are, they are assigned to their gpio descriptors.
298 * Warning if one of the names is already used for a different GPIO.
300 static int gpiochip_set_desc_names(struct gpio_chip
*gc
)
302 struct gpio_device
*gdev
= gc
->gpiodev
;
308 /* First check all names if they are unique */
309 for (i
= 0; i
!= gc
->ngpio
; ++i
) {
310 struct gpio_desc
*gpio
;
312 gpio
= gpio_name_to_desc(gc
->names
[i
]);
315 "Detected name collision for GPIO name '%s'\n",
319 /* Then add all names to the GPIO descriptors */
320 for (i
= 0; i
!= gc
->ngpio
; ++i
)
321 gdev
->descs
[i
].name
= gc
->names
[i
];
327 * gpio_ioctl() - ioctl handler for the GPIO chardev
329 static long gpio_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
331 struct gpio_device
*gdev
= filp
->private_data
;
332 struct gpio_chip
*chip
= gdev
->chip
;
333 int __user
*ip
= (int __user
*)arg
;
335 /* We fail any subsequent ioctl():s when the chip is gone */
339 /* Fill in the struct and pass to userspace */
340 if (cmd
== GPIO_GET_CHIPINFO_IOCTL
) {
341 struct gpiochip_info chipinfo
;
343 strncpy(chipinfo
.name
, dev_name(&gdev
->dev
),
344 sizeof(chipinfo
.name
));
345 chipinfo
.name
[sizeof(chipinfo
.name
)-1] = '\0';
346 strncpy(chipinfo
.label
, gdev
->label
,
347 sizeof(chipinfo
.label
));
348 chipinfo
.label
[sizeof(chipinfo
.label
)-1] = '\0';
349 chipinfo
.lines
= gdev
->ngpio
;
350 if (copy_to_user(ip
, &chipinfo
, sizeof(chipinfo
)))
353 } else if (cmd
== GPIO_GET_LINEINFO_IOCTL
) {
354 struct gpioline_info lineinfo
;
355 struct gpio_desc
*desc
;
357 if (copy_from_user(&lineinfo
, ip
, sizeof(lineinfo
)))
359 if (lineinfo
.line_offset
> gdev
->ngpio
)
362 desc
= &gdev
->descs
[lineinfo
.line_offset
];
364 strncpy(lineinfo
.name
, desc
->name
,
365 sizeof(lineinfo
.name
));
366 lineinfo
.name
[sizeof(lineinfo
.name
)-1] = '\0';
368 lineinfo
.name
[0] = '\0';
371 strncpy(lineinfo
.label
, desc
->label
,
372 sizeof(lineinfo
.label
));
373 lineinfo
.label
[sizeof(lineinfo
.label
)-1] = '\0';
375 lineinfo
.label
[0] = '\0';
379 * Userspace only need to know that the kernel is using
380 * this GPIO so it can't use it.
383 if (desc
->flags
& (FLAG_REQUESTED
| FLAG_IS_HOGGED
|
384 FLAG_USED_AS_IRQ
| FLAG_EXPORT
|
386 lineinfo
.flags
|= GPIOLINE_FLAG_KERNEL
;
387 if (desc
->flags
& FLAG_IS_OUT
)
388 lineinfo
.flags
|= GPIOLINE_FLAG_IS_OUT
;
389 if (desc
->flags
& FLAG_ACTIVE_LOW
)
390 lineinfo
.flags
|= GPIOLINE_FLAG_ACTIVE_LOW
;
391 if (desc
->flags
& FLAG_OPEN_DRAIN
)
392 lineinfo
.flags
|= GPIOLINE_FLAG_OPEN_DRAIN
;
393 if (desc
->flags
& FLAG_OPEN_SOURCE
)
394 lineinfo
.flags
|= GPIOLINE_FLAG_OPEN_SOURCE
;
396 if (copy_to_user(ip
, &lineinfo
, sizeof(lineinfo
)))
404 * gpio_chrdev_open() - open the chardev for ioctl operations
405 * @inode: inode for this chardev
406 * @filp: file struct for storing private data
407 * Returns 0 on success
409 static int gpio_chrdev_open(struct inode
*inode
, struct file
*filp
)
411 struct gpio_device
*gdev
= container_of(inode
->i_cdev
,
412 struct gpio_device
, chrdev
);
414 /* Fail on open if the backing gpiochip is gone */
415 if (!gdev
|| !gdev
->chip
)
417 get_device(&gdev
->dev
);
418 filp
->private_data
= gdev
;
423 * gpio_chrdev_release() - close chardev after ioctl operations
424 * @inode: inode for this chardev
425 * @filp: file struct for storing private data
426 * Returns 0 on success
428 static int gpio_chrdev_release(struct inode
*inode
, struct file
*filp
)
430 struct gpio_device
*gdev
= container_of(inode
->i_cdev
,
431 struct gpio_device
, chrdev
);
435 put_device(&gdev
->dev
);
440 static const struct file_operations gpio_fileops
= {
441 .release
= gpio_chrdev_release
,
442 .open
= gpio_chrdev_open
,
443 .owner
= THIS_MODULE
,
444 .llseek
= noop_llseek
,
445 .unlocked_ioctl
= gpio_ioctl
,
446 .compat_ioctl
= gpio_ioctl
,
449 static void gpiodevice_release(struct device
*dev
)
451 struct gpio_device
*gdev
= dev_get_drvdata(dev
);
453 cdev_del(&gdev
->chrdev
);
454 list_del(&gdev
->list
);
455 ida_simple_remove(&gpio_ida
, gdev
->id
);
460 * gpiochip_add_data() - register a gpio_chip
461 * @chip: the chip to register, with chip->base initialized
462 * Context: potentially before irqs will work
464 * Returns a negative errno if the chip can't be registered, such as
465 * because the chip->base is invalid or already associated with a
466 * different chip. Otherwise it returns zero as a success code.
468 * When gpiochip_add_data() is called very early during boot, so that GPIOs
469 * can be freely used, the chip->parent device must be registered before
470 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
471 * for GPIOs will fail rudely.
473 * If chip->base is negative, this requests dynamic assignment of
474 * a range of valid GPIOs.
476 int gpiochip_add_data(struct gpio_chip
*chip
, void *data
)
481 int base
= chip
->base
;
482 struct gpio_device
*gdev
;
485 * First: allocate and populate the internal stat container, and
486 * set up the struct device.
488 gdev
= kzalloc(sizeof(*gdev
), GFP_KERNEL
);
491 gdev
->dev
.bus
= &gpio_bus_type
;
493 chip
->gpiodev
= gdev
;
495 gdev
->dev
.parent
= chip
->parent
;
496 gdev
->dev
.of_node
= chip
->parent
->of_node
;
498 #ifdef CONFIG_OF_GPIO
499 /* If the gpiochip has an assigned OF node this takes precedence */
501 gdev
->dev
.of_node
= chip
->of_node
;
504 gdev
->id
= ida_simple_get(&gpio_ida
, 0, 0, GFP_KERNEL
);
509 dev_set_name(&gdev
->dev
, "gpiochip%d", gdev
->id
);
510 device_initialize(&gdev
->dev
);
511 dev_set_drvdata(&gdev
->dev
, gdev
);
512 if (chip
->parent
&& chip
->parent
->driver
)
513 gdev
->owner
= chip
->parent
->driver
->owner
;
514 else if (chip
->owner
)
515 /* TODO: remove chip->owner */
516 gdev
->owner
= chip
->owner
;
518 gdev
->owner
= THIS_MODULE
;
520 gdev
->descs
= devm_kcalloc(&gdev
->dev
, chip
->ngpio
,
521 sizeof(gdev
->descs
[0]), GFP_KERNEL
);
527 if (chip
->ngpio
== 0) {
528 chip_err(chip
, "tried to insert a GPIO chip with zero lines\n");
534 gdev
->label
= devm_kstrdup(&gdev
->dev
, chip
->label
, GFP_KERNEL
);
536 gdev
->label
= devm_kstrdup(&gdev
->dev
, "unknown", GFP_KERNEL
);
542 gdev
->ngpio
= chip
->ngpio
;
545 spin_lock_irqsave(&gpio_lock
, flags
);
548 * TODO: this allocates a Linux GPIO number base in the global
549 * GPIO numberspace for this chip. In the long run we want to
550 * get *rid* of this numberspace and use only descriptors, but
551 * it may be a pipe dream. It will not happen before we get rid
552 * of the sysfs interface anyways.
555 base
= gpiochip_find_base(chip
->ngpio
);
558 spin_unlock_irqrestore(&gpio_lock
, flags
);
562 * TODO: it should not be necessary to reflect the assigned
563 * base outside of the GPIO subsystem. Go over drivers and
564 * see if anyone makes use of this, else drop this and assign
571 status
= gpiodev_add_to_list(gdev
);
573 spin_unlock_irqrestore(&gpio_lock
, flags
);
577 for (i
= 0; i
< chip
->ngpio
; i
++) {
578 struct gpio_desc
*desc
= &gdev
->descs
[i
];
582 /* REVISIT: most hardware initializes GPIOs as inputs (often
583 * with pullups enabled) so power usage is minimized. Linux
584 * code should set the gpio direction first thing; but until
585 * it does, and in case chip->get_direction is not set, we may
586 * expose the wrong direction in sysfs.
588 desc
->flags
= !chip
->direction_input
? (1 << FLAG_IS_OUT
) : 0;
591 spin_unlock_irqrestore(&gpio_lock
, flags
);
593 #ifdef CONFIG_PINCTRL
594 INIT_LIST_HEAD(&gdev
->pin_ranges
);
597 status
= gpiochip_set_desc_names(chip
);
599 goto err_remove_from_list
;
601 status
= of_gpiochip_add(chip
);
603 goto err_remove_chip
;
605 acpi_gpiochip_add(chip
);
608 * By first adding the chardev, and then adding the device,
609 * we get a device node entry in sysfs under
610 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
611 * coldplug of device nodes and other udev business.
613 cdev_init(&gdev
->chrdev
, &gpio_fileops
);
614 gdev
->chrdev
.owner
= THIS_MODULE
;
615 gdev
->chrdev
.kobj
.parent
= &gdev
->dev
.kobj
;
616 gdev
->dev
.devt
= MKDEV(MAJOR(gpio_devt
), gdev
->id
);
617 status
= cdev_add(&gdev
->chrdev
, gdev
->dev
.devt
, 1);
619 chip_warn(chip
, "failed to add char device %d:%d\n",
620 MAJOR(gpio_devt
), gdev
->id
);
622 chip_dbg(chip
, "added GPIO chardev (%d:%d)\n",
623 MAJOR(gpio_devt
), gdev
->id
);
624 status
= device_add(&gdev
->dev
);
626 goto err_remove_chardev
;
628 status
= gpiochip_sysfs_register(gdev
);
630 goto err_remove_device
;
632 /* From this point, the .release() function cleans up gpio_device */
633 gdev
->dev
.release
= gpiodevice_release
;
634 get_device(&gdev
->dev
);
635 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
636 __func__
, gdev
->base
, gdev
->base
+ gdev
->ngpio
- 1,
637 dev_name(&gdev
->dev
), chip
->label
? : "generic");
642 device_del(&gdev
->dev
);
644 cdev_del(&gdev
->chrdev
);
646 acpi_gpiochip_remove(chip
);
647 gpiochip_free_hogs(chip
);
648 of_gpiochip_remove(chip
);
649 err_remove_from_list
:
650 spin_lock_irqsave(&gpio_lock
, flags
);
651 list_del(&gdev
->list
);
652 spin_unlock_irqrestore(&gpio_lock
, flags
);
654 ida_simple_remove(&gpio_ida
, gdev
->id
);
655 /* failures here can mean systems won't boot... */
656 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__
,
657 gdev
->base
, gdev
->base
+ gdev
->ngpio
- 1,
658 chip
->label
? : "generic");
662 EXPORT_SYMBOL_GPL(gpiochip_add_data
);
665 * gpiochip_get_data() - get per-subdriver data for the chip
667 void *gpiochip_get_data(struct gpio_chip
*chip
)
669 return chip
->gpiodev
->data
;
671 EXPORT_SYMBOL_GPL(gpiochip_get_data
);
674 * gpiochip_remove() - unregister a gpio_chip
675 * @chip: the chip to unregister
677 * A gpio_chip with any GPIOs still requested may not be removed.
679 void gpiochip_remove(struct gpio_chip
*chip
)
681 struct gpio_device
*gdev
= chip
->gpiodev
;
682 struct gpio_desc
*desc
;
685 bool requested
= false;
687 /* Numb the device, cancelling all outstanding operations */
690 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
691 gpiochip_sysfs_unregister(gdev
);
692 gpiochip_irqchip_remove(chip
);
693 acpi_gpiochip_remove(chip
);
694 gpiochip_remove_pin_ranges(chip
);
695 gpiochip_free_hogs(chip
);
696 of_gpiochip_remove(chip
);
698 * We accept no more calls into the driver from this point, so
699 * NULL the driver data pointer
703 spin_lock_irqsave(&gpio_lock
, flags
);
704 for (i
= 0; i
< gdev
->ngpio
; i
++) {
705 desc
= &gdev
->descs
[i
];
706 if (test_bit(FLAG_REQUESTED
, &desc
->flags
))
709 spin_unlock_irqrestore(&gpio_lock
, flags
);
713 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
716 * The gpiochip side puts its use of the device to rest here:
717 * if there are no userspace clients, the chardev and device will
718 * be removed, else it will be dangling until the last user is
721 put_device(&gdev
->dev
);
723 EXPORT_SYMBOL_GPL(gpiochip_remove
);
726 * gpiochip_find() - iterator for locating a specific gpio_chip
727 * @data: data to pass to match function
728 * @callback: Callback function to check gpio_chip
730 * Similar to bus_find_device. It returns a reference to a gpio_chip as
731 * determined by a user supplied @match callback. The callback should return
732 * 0 if the device doesn't match and non-zero if it does. If the callback is
733 * non-zero, this function will return to the caller and not iterate over any
736 struct gpio_chip
*gpiochip_find(void *data
,
737 int (*match
)(struct gpio_chip
*chip
,
740 struct gpio_device
*gdev
;
741 struct gpio_chip
*chip
;
744 spin_lock_irqsave(&gpio_lock
, flags
);
745 list_for_each_entry(gdev
, &gpio_devices
, list
)
746 if (match(gdev
->chip
, data
))
750 if (&gdev
->list
== &gpio_devices
)
755 spin_unlock_irqrestore(&gpio_lock
, flags
);
759 EXPORT_SYMBOL_GPL(gpiochip_find
);
761 static int gpiochip_match_name(struct gpio_chip
*chip
, void *data
)
763 const char *name
= data
;
765 return !strcmp(chip
->label
, name
);
768 static struct gpio_chip
*find_chip_by_name(const char *name
)
770 return gpiochip_find((void *)name
, gpiochip_match_name
);
773 #ifdef CONFIG_GPIOLIB_IRQCHIP
776 * The following is irqchip helper code for gpiochips.
780 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
781 * @gpiochip: the gpiochip to set the irqchip chain to
782 * @irqchip: the irqchip to chain to the gpiochip
783 * @parent_irq: the irq number corresponding to the parent IRQ for this
785 * @parent_handler: the parent interrupt handler for the accumulated IRQ
786 * coming out of the gpiochip. If the interrupt is nested rather than
787 * cascaded, pass NULL in this handler argument
789 void gpiochip_set_chained_irqchip(struct gpio_chip
*gpiochip
,
790 struct irq_chip
*irqchip
,
792 irq_flow_handler_t parent_handler
)
796 if (!gpiochip
->irqdomain
) {
797 chip_err(gpiochip
, "called %s before setting up irqchip\n",
802 if (parent_handler
) {
803 if (gpiochip
->can_sleep
) {
805 "you cannot have chained interrupts on a "
806 "chip that may sleep\n");
810 * The parent irqchip is already using the chip_data for this
811 * irqchip, so our callbacks simply use the handler_data.
813 irq_set_chained_handler_and_data(parent_irq
, parent_handler
,
816 gpiochip
->irq_parent
= parent_irq
;
819 /* Set the parent IRQ for all affected IRQs */
820 for (offset
= 0; offset
< gpiochip
->ngpio
; offset
++)
821 irq_set_parent(irq_find_mapping(gpiochip
->irqdomain
, offset
),
824 EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip
);
827 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
828 * @d: the irqdomain used by this irqchip
829 * @irq: the global irq number used by this GPIO irqchip irq
830 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
832 * This function will set up the mapping for a certain IRQ line on a
833 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
834 * stored inside the gpiochip.
836 static int gpiochip_irq_map(struct irq_domain
*d
, unsigned int irq
,
837 irq_hw_number_t hwirq
)
839 struct gpio_chip
*chip
= d
->host_data
;
841 irq_set_chip_data(irq
, chip
);
843 * This lock class tells lockdep that GPIO irqs are in a different
844 * category than their parents, so it won't report false recursion.
846 irq_set_lockdep_class(irq
, chip
->lock_key
);
847 irq_set_chip_and_handler(irq
, chip
->irqchip
, chip
->irq_handler
);
848 /* Chips that can sleep need nested thread handlers */
849 if (chip
->can_sleep
&& !chip
->irq_not_threaded
)
850 irq_set_nested_thread(irq
, 1);
851 irq_set_noprobe(irq
);
854 * No set-up of the hardware will happen if IRQ_TYPE_NONE
855 * is passed as default type.
857 if (chip
->irq_default_type
!= IRQ_TYPE_NONE
)
858 irq_set_irq_type(irq
, chip
->irq_default_type
);
863 static void gpiochip_irq_unmap(struct irq_domain
*d
, unsigned int irq
)
865 struct gpio_chip
*chip
= d
->host_data
;
868 irq_set_nested_thread(irq
, 0);
869 irq_set_chip_and_handler(irq
, NULL
, NULL
);
870 irq_set_chip_data(irq
, NULL
);
873 static const struct irq_domain_ops gpiochip_domain_ops
= {
874 .map
= gpiochip_irq_map
,
875 .unmap
= gpiochip_irq_unmap
,
876 /* Virtually all GPIO irqchips are twocell:ed */
877 .xlate
= irq_domain_xlate_twocell
,
880 static int gpiochip_irq_reqres(struct irq_data
*d
)
882 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(d
);
884 if (!try_module_get(chip
->gpiodev
->owner
))
887 if (gpiochip_lock_as_irq(chip
, d
->hwirq
)) {
889 "unable to lock HW IRQ %lu for IRQ\n",
891 module_put(chip
->gpiodev
->owner
);
897 static void gpiochip_irq_relres(struct irq_data
*d
)
899 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(d
);
901 gpiochip_unlock_as_irq(chip
, d
->hwirq
);
902 module_put(chip
->gpiodev
->owner
);
905 static int gpiochip_to_irq(struct gpio_chip
*chip
, unsigned offset
)
907 return irq_find_mapping(chip
->irqdomain
, offset
);
911 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
912 * @gpiochip: the gpiochip to remove the irqchip from
914 * This is called only from gpiochip_remove()
916 static void gpiochip_irqchip_remove(struct gpio_chip
*gpiochip
)
920 acpi_gpiochip_free_interrupts(gpiochip
);
922 if (gpiochip
->irq_parent
) {
923 irq_set_chained_handler(gpiochip
->irq_parent
, NULL
);
924 irq_set_handler_data(gpiochip
->irq_parent
, NULL
);
927 /* Remove all IRQ mappings and delete the domain */
928 if (gpiochip
->irqdomain
) {
929 for (offset
= 0; offset
< gpiochip
->ngpio
; offset
++)
931 irq_find_mapping(gpiochip
->irqdomain
, offset
));
932 irq_domain_remove(gpiochip
->irqdomain
);
935 if (gpiochip
->irqchip
) {
936 gpiochip
->irqchip
->irq_request_resources
= NULL
;
937 gpiochip
->irqchip
->irq_release_resources
= NULL
;
938 gpiochip
->irqchip
= NULL
;
943 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
944 * @gpiochip: the gpiochip to add the irqchip to
945 * @irqchip: the irqchip to add to the gpiochip
946 * @first_irq: if not dynamically assigned, the base (first) IRQ to
947 * allocate gpiochip irqs from
948 * @handler: the irq handler to use (often a predefined irq core function)
949 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
950 * to have the core avoid setting up any default type in the hardware.
951 * @lock_key: lockdep class
953 * This function closely associates a certain irqchip with a certain
954 * gpiochip, providing an irq domain to translate the local IRQs to
955 * global irqs in the gpiolib core, and making sure that the gpiochip
956 * is passed as chip data to all related functions. Driver callbacks
957 * need to use gpiochip_get_data() to get their local state containers back
958 * from the gpiochip passed as chip data. An irqdomain will be stored
959 * in the gpiochip that shall be used by the driver to handle IRQ number
960 * translation. The gpiochip will need to be initialized and registered
961 * before calling this function.
963 * This function will handle two cell:ed simple IRQs and assumes all
964 * the pins on the gpiochip can generate a unique IRQ. Everything else
965 * need to be open coded.
967 int _gpiochip_irqchip_add(struct gpio_chip
*gpiochip
,
968 struct irq_chip
*irqchip
,
969 unsigned int first_irq
,
970 irq_flow_handler_t handler
,
972 struct lock_class_key
*lock_key
)
974 struct device_node
*of_node
;
976 unsigned irq_base
= 0;
978 if (!gpiochip
|| !irqchip
)
981 if (!gpiochip
->parent
) {
982 pr_err("missing gpiochip .dev parent pointer\n");
985 of_node
= gpiochip
->parent
->of_node
;
986 #ifdef CONFIG_OF_GPIO
988 * If the gpiochip has an assigned OF node this takes precedence
989 * FIXME: get rid of this and use gpiochip->parent->of_node
992 if (gpiochip
->of_node
)
993 of_node
= gpiochip
->of_node
;
995 gpiochip
->irqchip
= irqchip
;
996 gpiochip
->irq_handler
= handler
;
997 gpiochip
->irq_default_type
= type
;
998 gpiochip
->to_irq
= gpiochip_to_irq
;
999 gpiochip
->lock_key
= lock_key
;
1000 gpiochip
->irqdomain
= irq_domain_add_simple(of_node
,
1001 gpiochip
->ngpio
, first_irq
,
1002 &gpiochip_domain_ops
, gpiochip
);
1003 if (!gpiochip
->irqdomain
) {
1004 gpiochip
->irqchip
= NULL
;
1009 * It is possible for a driver to override this, but only if the
1010 * alternative functions are both implemented.
1012 if (!irqchip
->irq_request_resources
&&
1013 !irqchip
->irq_release_resources
) {
1014 irqchip
->irq_request_resources
= gpiochip_irq_reqres
;
1015 irqchip
->irq_release_resources
= gpiochip_irq_relres
;
1019 * Prepare the mapping since the irqchip shall be orthogonal to
1020 * any gpiochip calls. If the first_irq was zero, this is
1021 * necessary to allocate descriptors for all IRQs.
1023 for (offset
= 0; offset
< gpiochip
->ngpio
; offset
++) {
1024 irq_base
= irq_create_mapping(gpiochip
->irqdomain
, offset
);
1027 * Store the base into the gpiochip to be used when
1028 * unmapping the irqs.
1030 gpiochip
->irq_base
= irq_base
;
1033 acpi_gpiochip_request_interrupts(gpiochip
);
1037 EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add
);
1039 #else /* CONFIG_GPIOLIB_IRQCHIP */
1041 static void gpiochip_irqchip_remove(struct gpio_chip
*gpiochip
) {}
1043 #endif /* CONFIG_GPIOLIB_IRQCHIP */
1046 * gpiochip_generic_request() - request the gpio function for a pin
1047 * @chip: the gpiochip owning the GPIO
1048 * @offset: the offset of the GPIO to request for GPIO function
1050 int gpiochip_generic_request(struct gpio_chip
*chip
, unsigned offset
)
1052 return pinctrl_request_gpio(chip
->gpiodev
->base
+ offset
);
1054 EXPORT_SYMBOL_GPL(gpiochip_generic_request
);
1057 * gpiochip_generic_free() - free the gpio function from a pin
1058 * @chip: the gpiochip to request the gpio function for
1059 * @offset: the offset of the GPIO to free from GPIO function
1061 void gpiochip_generic_free(struct gpio_chip
*chip
, unsigned offset
)
1063 pinctrl_free_gpio(chip
->gpiodev
->base
+ offset
);
1065 EXPORT_SYMBOL_GPL(gpiochip_generic_free
);
1067 #ifdef CONFIG_PINCTRL
1070 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1071 * @chip: the gpiochip to add the range for
1072 * @pctldev: the pin controller to map to
1073 * @gpio_offset: the start offset in the current gpio_chip number space
1074 * @pin_group: name of the pin group inside the pin controller
1076 int gpiochip_add_pingroup_range(struct gpio_chip
*chip
,
1077 struct pinctrl_dev
*pctldev
,
1078 unsigned int gpio_offset
, const char *pin_group
)
1080 struct gpio_pin_range
*pin_range
;
1081 struct gpio_device
*gdev
= chip
->gpiodev
;
1084 pin_range
= kzalloc(sizeof(*pin_range
), GFP_KERNEL
);
1086 chip_err(chip
, "failed to allocate pin ranges\n");
1090 /* Use local offset as range ID */
1091 pin_range
->range
.id
= gpio_offset
;
1092 pin_range
->range
.gc
= chip
;
1093 pin_range
->range
.name
= chip
->label
;
1094 pin_range
->range
.base
= gdev
->base
+ gpio_offset
;
1095 pin_range
->pctldev
= pctldev
;
1097 ret
= pinctrl_get_group_pins(pctldev
, pin_group
,
1098 &pin_range
->range
.pins
,
1099 &pin_range
->range
.npins
);
1105 pinctrl_add_gpio_range(pctldev
, &pin_range
->range
);
1107 chip_dbg(chip
, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1108 gpio_offset
, gpio_offset
+ pin_range
->range
.npins
- 1,
1109 pinctrl_dev_get_devname(pctldev
), pin_group
);
1111 list_add_tail(&pin_range
->node
, &gdev
->pin_ranges
);
1115 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range
);
1118 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1119 * @chip: the gpiochip to add the range for
1120 * @pinctrl_name: the dev_name() of the pin controller to map to
1121 * @gpio_offset: the start offset in the current gpio_chip number space
1122 * @pin_offset: the start offset in the pin controller number space
1123 * @npins: the number of pins from the offset of each pin space (GPIO and
1124 * pin controller) to accumulate in this range
1126 int gpiochip_add_pin_range(struct gpio_chip
*chip
, const char *pinctl_name
,
1127 unsigned int gpio_offset
, unsigned int pin_offset
,
1130 struct gpio_pin_range
*pin_range
;
1131 struct gpio_device
*gdev
= chip
->gpiodev
;
1134 pin_range
= kzalloc(sizeof(*pin_range
), GFP_KERNEL
);
1136 chip_err(chip
, "failed to allocate pin ranges\n");
1140 /* Use local offset as range ID */
1141 pin_range
->range
.id
= gpio_offset
;
1142 pin_range
->range
.gc
= chip
;
1143 pin_range
->range
.name
= chip
->label
;
1144 pin_range
->range
.base
= gdev
->base
+ gpio_offset
;
1145 pin_range
->range
.pin_base
= pin_offset
;
1146 pin_range
->range
.npins
= npins
;
1147 pin_range
->pctldev
= pinctrl_find_and_add_gpio_range(pinctl_name
,
1149 if (IS_ERR(pin_range
->pctldev
)) {
1150 ret
= PTR_ERR(pin_range
->pctldev
);
1151 chip_err(chip
, "could not create pin range\n");
1155 chip_dbg(chip
, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1156 gpio_offset
, gpio_offset
+ npins
- 1,
1158 pin_offset
, pin_offset
+ npins
- 1);
1160 list_add_tail(&pin_range
->node
, &gdev
->pin_ranges
);
1164 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range
);
1167 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1168 * @chip: the chip to remove all the mappings for
1170 void gpiochip_remove_pin_ranges(struct gpio_chip
*chip
)
1172 struct gpio_pin_range
*pin_range
, *tmp
;
1173 struct gpio_device
*gdev
= chip
->gpiodev
;
1175 list_for_each_entry_safe(pin_range
, tmp
, &gdev
->pin_ranges
, node
) {
1176 list_del(&pin_range
->node
);
1177 pinctrl_remove_gpio_range(pin_range
->pctldev
,
1182 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges
);
1184 #endif /* CONFIG_PINCTRL */
1186 /* These "optional" allocation calls help prevent drivers from stomping
1187 * on each other, and help provide better diagnostics in debugfs.
1188 * They're called even less than the "set direction" calls.
1190 static int __gpiod_request(struct gpio_desc
*desc
, const char *label
)
1192 struct gpio_chip
*chip
= desc
->gdev
->chip
;
1194 unsigned long flags
;
1196 spin_lock_irqsave(&gpio_lock
, flags
);
1198 /* NOTE: gpio_request() can be called in early boot,
1199 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1202 if (test_and_set_bit(FLAG_REQUESTED
, &desc
->flags
) == 0) {
1203 desc_set_label(desc
, label
? : "?");
1210 if (chip
->request
) {
1211 /* chip->request may sleep */
1212 spin_unlock_irqrestore(&gpio_lock
, flags
);
1213 status
= chip
->request(chip
, gpio_chip_hwgpio(desc
));
1214 spin_lock_irqsave(&gpio_lock
, flags
);
1217 desc_set_label(desc
, NULL
);
1218 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
1222 if (chip
->get_direction
) {
1223 /* chip->get_direction may sleep */
1224 spin_unlock_irqrestore(&gpio_lock
, flags
);
1225 gpiod_get_direction(desc
);
1226 spin_lock_irqsave(&gpio_lock
, flags
);
1230 /* Clear flags that might have been set by the caller before
1231 * requesting the GPIO.
1233 clear_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
1234 clear_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
1235 clear_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
1237 spin_unlock_irqrestore(&gpio_lock
, flags
);
1242 * This descriptor validation needs to be inserted verbatim into each
1243 * function taking a descriptor, so we need to use a preprocessor
1244 * macro to avoid endless duplication.
1246 #define VALIDATE_DESC(desc) do { \
1247 if (!desc || !desc->gdev) { \
1248 pr_warn("%s: invalid GPIO\n", __func__); \
1251 if ( !desc->gdev->chip ) { \
1252 dev_warn(&desc->gdev->dev, \
1253 "%s: backing chip is gone\n", __func__); \
1257 #define VALIDATE_DESC_VOID(desc) do { \
1258 if (!desc || !desc->gdev) { \
1259 pr_warn("%s: invalid GPIO\n", __func__); \
1262 if (!desc->gdev->chip) { \
1263 dev_warn(&desc->gdev->dev, \
1264 "%s: backing chip is gone\n", __func__); \
1269 int gpiod_request(struct gpio_desc
*desc
, const char *label
)
1271 int status
= -EPROBE_DEFER
;
1272 struct gpio_device
*gdev
;
1274 VALIDATE_DESC(desc
);
1277 if (try_module_get(gdev
->owner
)) {
1278 status
= __gpiod_request(desc
, label
);
1280 module_put(gdev
->owner
);
1282 get_device(&gdev
->dev
);
1286 gpiod_dbg(desc
, "%s: status %d\n", __func__
, status
);
1291 static bool __gpiod_free(struct gpio_desc
*desc
)
1294 unsigned long flags
;
1295 struct gpio_chip
*chip
;
1299 gpiod_unexport(desc
);
1301 spin_lock_irqsave(&gpio_lock
, flags
);
1303 chip
= desc
->gdev
->chip
;
1304 if (chip
&& test_bit(FLAG_REQUESTED
, &desc
->flags
)) {
1306 spin_unlock_irqrestore(&gpio_lock
, flags
);
1307 might_sleep_if(chip
->can_sleep
);
1308 chip
->free(chip
, gpio_chip_hwgpio(desc
));
1309 spin_lock_irqsave(&gpio_lock
, flags
);
1311 desc_set_label(desc
, NULL
);
1312 clear_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
1313 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
1314 clear_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
1315 clear_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
1316 clear_bit(FLAG_IS_HOGGED
, &desc
->flags
);
1320 spin_unlock_irqrestore(&gpio_lock
, flags
);
1324 void gpiod_free(struct gpio_desc
*desc
)
1326 if (desc
&& desc
->gdev
&& __gpiod_free(desc
)) {
1327 module_put(desc
->gdev
->owner
);
1328 put_device(&desc
->gdev
->dev
);
1330 WARN_ON(extra_checks
);
1335 * gpiochip_is_requested - return string iff signal was requested
1336 * @chip: controller managing the signal
1337 * @offset: of signal within controller's 0..(ngpio - 1) range
1339 * Returns NULL if the GPIO is not currently requested, else a string.
1340 * The string returned is the label passed to gpio_request(); if none has been
1341 * passed it is a meaningless, non-NULL constant.
1343 * This function is for use by GPIO controller drivers. The label can
1344 * help with diagnostics, and knowing that the signal is used as a GPIO
1345 * can help avoid accidentally multiplexing it to another controller.
1347 const char *gpiochip_is_requested(struct gpio_chip
*chip
, unsigned offset
)
1349 struct gpio_desc
*desc
;
1351 if (offset
>= chip
->ngpio
)
1354 desc
= &chip
->gpiodev
->descs
[offset
];
1356 if (test_bit(FLAG_REQUESTED
, &desc
->flags
) == 0)
1360 EXPORT_SYMBOL_GPL(gpiochip_is_requested
);
1363 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1364 * @desc: GPIO descriptor to request
1365 * @label: label for the GPIO
1367 * Function allows GPIO chip drivers to request and use their own GPIO
1368 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1369 * function will not increase reference count of the GPIO chip module. This
1370 * allows the GPIO chip module to be unloaded as needed (we assume that the
1371 * GPIO chip driver handles freeing the GPIOs it has requested).
1373 struct gpio_desc
*gpiochip_request_own_desc(struct gpio_chip
*chip
, u16 hwnum
,
1376 struct gpio_desc
*desc
= gpiochip_get_desc(chip
, hwnum
);
1380 chip_err(chip
, "failed to get GPIO descriptor\n");
1384 err
= __gpiod_request(desc
, label
);
1386 return ERR_PTR(err
);
1390 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc
);
1393 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1394 * @desc: GPIO descriptor to free
1396 * Function frees the given GPIO requested previously with
1397 * gpiochip_request_own_desc().
1399 void gpiochip_free_own_desc(struct gpio_desc
*desc
)
1404 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc
);
1407 * Drivers MUST set GPIO direction before making get/set calls. In
1408 * some cases this is done in early boot, before IRQs are enabled.
1410 * As a rule these aren't called more than once (except for drivers
1411 * using the open-drain emulation idiom) so these are natural places
1412 * to accumulate extra debugging checks. Note that we can't (yet)
1413 * rely on gpio_request() having been called beforehand.
1417 * gpiod_direction_input - set the GPIO direction to input
1418 * @desc: GPIO to set to input
1420 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1421 * be called safely on it.
1423 * Return 0 in case of success, else an error code.
1425 int gpiod_direction_input(struct gpio_desc
*desc
)
1427 struct gpio_chip
*chip
;
1428 int status
= -EINVAL
;
1430 VALIDATE_DESC(desc
);
1431 chip
= desc
->gdev
->chip
;
1433 if (!chip
->get
|| !chip
->direction_input
) {
1435 "%s: missing get() or direction_input() operations\n",
1440 status
= chip
->direction_input(chip
, gpio_chip_hwgpio(desc
));
1442 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1444 trace_gpio_direction(desc_to_gpio(desc
), 1, status
);
1448 EXPORT_SYMBOL_GPL(gpiod_direction_input
);
1450 static int _gpiod_direction_output_raw(struct gpio_desc
*desc
, int value
)
1452 struct gpio_chip
*chip
;
1453 int status
= -EINVAL
;
1455 /* GPIOs used for IRQs shall not be set as output */
1456 if (test_bit(FLAG_USED_AS_IRQ
, &desc
->flags
)) {
1458 "%s: tried to set a GPIO tied to an IRQ as output\n",
1463 /* Open drain pin should not be driven to 1 */
1464 if (value
&& test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
))
1465 return gpiod_direction_input(desc
);
1467 /* Open source pin should not be driven to 0 */
1468 if (!value
&& test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
))
1469 return gpiod_direction_input(desc
);
1471 chip
= desc
->gdev
->chip
;
1472 if (!chip
->set
|| !chip
->direction_output
) {
1474 "%s: missing set() or direction_output() operations\n",
1479 status
= chip
->direction_output(chip
, gpio_chip_hwgpio(desc
), value
);
1481 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1482 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
1483 trace_gpio_direction(desc_to_gpio(desc
), 0, status
);
1488 * gpiod_direction_output_raw - set the GPIO direction to output
1489 * @desc: GPIO to set to output
1490 * @value: initial output value of the GPIO
1492 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1493 * be called safely on it. The initial value of the output must be specified
1494 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1496 * Return 0 in case of success, else an error code.
1498 int gpiod_direction_output_raw(struct gpio_desc
*desc
, int value
)
1500 VALIDATE_DESC(desc
);
1501 return _gpiod_direction_output_raw(desc
, value
);
1503 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw
);
1506 * gpiod_direction_output - set the GPIO direction to output
1507 * @desc: GPIO to set to output
1508 * @value: initial output value of the GPIO
1510 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1511 * be called safely on it. The initial value of the output must be specified
1512 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1515 * Return 0 in case of success, else an error code.
1517 int gpiod_direction_output(struct gpio_desc
*desc
, int value
)
1519 VALIDATE_DESC(desc
);
1520 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1522 return _gpiod_direction_output_raw(desc
, value
);
1524 EXPORT_SYMBOL_GPL(gpiod_direction_output
);
1527 * gpiod_set_debounce - sets @debounce time for a @gpio
1528 * @gpio: the gpio to set debounce time
1529 * @debounce: debounce time is microseconds
1531 * returns -ENOTSUPP if the controller does not support setting
1534 int gpiod_set_debounce(struct gpio_desc
*desc
, unsigned debounce
)
1536 struct gpio_chip
*chip
;
1538 VALIDATE_DESC(desc
);
1539 chip
= desc
->gdev
->chip
;
1540 if (!chip
->set
|| !chip
->set_debounce
) {
1542 "%s: missing set() or set_debounce() operations\n",
1547 return chip
->set_debounce(chip
, gpio_chip_hwgpio(desc
), debounce
);
1549 EXPORT_SYMBOL_GPL(gpiod_set_debounce
);
1552 * gpiod_is_active_low - test whether a GPIO is active-low or not
1553 * @desc: the gpio descriptor to test
1555 * Returns 1 if the GPIO is active-low, 0 otherwise.
1557 int gpiod_is_active_low(const struct gpio_desc
*desc
)
1559 VALIDATE_DESC(desc
);
1560 return test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
1562 EXPORT_SYMBOL_GPL(gpiod_is_active_low
);
1564 /* I/O calls are only valid after configuration completed; the relevant
1565 * "is this a valid GPIO" error checks should already have been done.
1567 * "Get" operations are often inlinable as reading a pin value register,
1568 * and masking the relevant bit in that register.
1570 * When "set" operations are inlinable, they involve writing that mask to
1571 * one register to set a low value, or a different register to set it high.
1572 * Otherwise locking is needed, so there may be little value to inlining.
1574 *------------------------------------------------------------------------
1576 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1577 * have requested the GPIO. That can include implicit requesting by
1578 * a direction setting call. Marking a gpio as requested locks its chip
1579 * in memory, guaranteeing that these table lookups need no more locking
1580 * and that gpiochip_remove() will fail.
1582 * REVISIT when debugging, consider adding some instrumentation to ensure
1583 * that the GPIO was actually requested.
1586 static int _gpiod_get_raw_value(const struct gpio_desc
*desc
)
1588 struct gpio_chip
*chip
;
1592 chip
= desc
->gdev
->chip
;
1593 offset
= gpio_chip_hwgpio(desc
);
1594 value
= chip
->get
? chip
->get(chip
, offset
) : -EIO
;
1595 value
= value
< 0 ? value
: !!value
;
1596 trace_gpio_value(desc_to_gpio(desc
), 1, value
);
1601 * gpiod_get_raw_value() - return a gpio's raw value
1602 * @desc: gpio whose value will be returned
1604 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1605 * its ACTIVE_LOW status, or negative errno on failure.
1607 * This function should be called from contexts where we cannot sleep, and will
1608 * complain if the GPIO chip functions potentially sleep.
1610 int gpiod_get_raw_value(const struct gpio_desc
*desc
)
1612 VALIDATE_DESC(desc
);
1613 /* Should be using gpio_get_value_cansleep() */
1614 WARN_ON(desc
->gdev
->chip
->can_sleep
);
1615 return _gpiod_get_raw_value(desc
);
1617 EXPORT_SYMBOL_GPL(gpiod_get_raw_value
);
1620 * gpiod_get_value() - return a gpio's value
1621 * @desc: gpio whose value will be returned
1623 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1624 * account, or negative errno on failure.
1626 * This function should be called from contexts where we cannot sleep, and will
1627 * complain if the GPIO chip functions potentially sleep.
1629 int gpiod_get_value(const struct gpio_desc
*desc
)
1633 VALIDATE_DESC(desc
);
1634 /* Should be using gpio_get_value_cansleep() */
1635 WARN_ON(desc
->gdev
->chip
->can_sleep
);
1637 value
= _gpiod_get_raw_value(desc
);
1641 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1646 EXPORT_SYMBOL_GPL(gpiod_get_value
);
1649 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1650 * @desc: gpio descriptor whose state need to be set.
1651 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
1653 static void _gpio_set_open_drain_value(struct gpio_desc
*desc
, bool value
)
1656 struct gpio_chip
*chip
= desc
->gdev
->chip
;
1657 int offset
= gpio_chip_hwgpio(desc
);
1660 err
= chip
->direction_input(chip
, offset
);
1662 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1664 err
= chip
->direction_output(chip
, offset
, 0);
1666 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1668 trace_gpio_direction(desc_to_gpio(desc
), value
, err
);
1671 "%s: Error in set_value for open drain err %d\n",
1676 * _gpio_set_open_source_value() - Set the open source gpio's value.
1677 * @desc: gpio descriptor whose state need to be set.
1678 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
1680 static void _gpio_set_open_source_value(struct gpio_desc
*desc
, bool value
)
1683 struct gpio_chip
*chip
= desc
->gdev
->chip
;
1684 int offset
= gpio_chip_hwgpio(desc
);
1687 err
= chip
->direction_output(chip
, offset
, 1);
1689 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1691 err
= chip
->direction_input(chip
, offset
);
1693 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1695 trace_gpio_direction(desc_to_gpio(desc
), !value
, err
);
1698 "%s: Error in set_value for open source err %d\n",
1702 static void _gpiod_set_raw_value(struct gpio_desc
*desc
, bool value
)
1704 struct gpio_chip
*chip
;
1706 chip
= desc
->gdev
->chip
;
1707 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
1708 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
))
1709 _gpio_set_open_drain_value(desc
, value
);
1710 else if (test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
))
1711 _gpio_set_open_source_value(desc
, value
);
1713 chip
->set(chip
, gpio_chip_hwgpio(desc
), value
);
1717 * set multiple outputs on the same chip;
1718 * use the chip's set_multiple function if available;
1719 * otherwise set the outputs sequentially;
1720 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1721 * defines which outputs are to be changed
1722 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1723 * defines the values the outputs specified by mask are to be set to
1725 static void gpio_chip_set_multiple(struct gpio_chip
*chip
,
1726 unsigned long *mask
, unsigned long *bits
)
1728 if (chip
->set_multiple
) {
1729 chip
->set_multiple(chip
, mask
, bits
);
1732 for (i
= 0; i
< chip
->ngpio
; i
++) {
1733 if (mask
[BIT_WORD(i
)] == 0) {
1734 /* no more set bits in this mask word;
1735 * skip ahead to the next word */
1736 i
= (BIT_WORD(i
) + 1) * BITS_PER_LONG
- 1;
1739 /* set outputs if the corresponding mask bit is set */
1740 if (__test_and_clear_bit(i
, mask
))
1741 chip
->set(chip
, i
, test_bit(i
, bits
));
1746 static void gpiod_set_array_value_priv(bool raw
, bool can_sleep
,
1747 unsigned int array_size
,
1748 struct gpio_desc
**desc_array
,
1753 while (i
< array_size
) {
1754 struct gpio_chip
*chip
= desc_array
[i
]->gdev
->chip
;
1755 unsigned long mask
[BITS_TO_LONGS(chip
->ngpio
)];
1756 unsigned long bits
[BITS_TO_LONGS(chip
->ngpio
)];
1760 WARN_ON(chip
->can_sleep
);
1762 memset(mask
, 0, sizeof(mask
));
1764 struct gpio_desc
*desc
= desc_array
[i
];
1765 int hwgpio
= gpio_chip_hwgpio(desc
);
1766 int value
= value_array
[i
];
1768 if (!raw
&& test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1770 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
1772 * collect all normal outputs belonging to the same chip
1773 * open drain and open source outputs are set individually
1775 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
)) {
1776 _gpio_set_open_drain_value(desc
, value
);
1777 } else if (test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
)) {
1778 _gpio_set_open_source_value(desc
, value
);
1780 __set_bit(hwgpio
, mask
);
1782 __set_bit(hwgpio
, bits
);
1784 __clear_bit(hwgpio
, bits
);
1788 } while ((i
< array_size
) &&
1789 (desc_array
[i
]->gdev
->chip
== chip
));
1790 /* push collected bits to outputs */
1792 gpio_chip_set_multiple(chip
, mask
, bits
);
1797 * gpiod_set_raw_value() - assign a gpio's raw value
1798 * @desc: gpio whose value will be assigned
1799 * @value: value to assign
1801 * Set the raw value of the GPIO, i.e. the value of its physical line without
1802 * regard for its ACTIVE_LOW status.
1804 * This function should be called from contexts where we cannot sleep, and will
1805 * complain if the GPIO chip functions potentially sleep.
1807 void gpiod_set_raw_value(struct gpio_desc
*desc
, int value
)
1809 VALIDATE_DESC_VOID(desc
);
1810 /* Should be using gpio_set_value_cansleep() */
1811 WARN_ON(desc
->gdev
->chip
->can_sleep
);
1812 _gpiod_set_raw_value(desc
, value
);
1814 EXPORT_SYMBOL_GPL(gpiod_set_raw_value
);
1817 * gpiod_set_value() - assign a gpio's value
1818 * @desc: gpio whose value will be assigned
1819 * @value: value to assign
1821 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1824 * This function should be called from contexts where we cannot sleep, and will
1825 * complain if the GPIO chip functions potentially sleep.
1827 void gpiod_set_value(struct gpio_desc
*desc
, int value
)
1829 VALIDATE_DESC_VOID(desc
);
1830 /* Should be using gpio_set_value_cansleep() */
1831 WARN_ON(desc
->gdev
->chip
->can_sleep
);
1832 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1834 _gpiod_set_raw_value(desc
, value
);
1836 EXPORT_SYMBOL_GPL(gpiod_set_value
);
1839 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
1840 * @array_size: number of elements in the descriptor / value arrays
1841 * @desc_array: array of GPIO descriptors whose values will be assigned
1842 * @value_array: array of values to assign
1844 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1845 * without regard for their ACTIVE_LOW status.
1847 * This function should be called from contexts where we cannot sleep, and will
1848 * complain if the GPIO chip functions potentially sleep.
1850 void gpiod_set_raw_array_value(unsigned int array_size
,
1851 struct gpio_desc
**desc_array
, int *value_array
)
1855 gpiod_set_array_value_priv(true, false, array_size
, desc_array
,
1858 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value
);
1861 * gpiod_set_array_value() - assign values to an array of GPIOs
1862 * @array_size: number of elements in the descriptor / value arrays
1863 * @desc_array: array of GPIO descriptors whose values will be assigned
1864 * @value_array: array of values to assign
1866 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1869 * This function should be called from contexts where we cannot sleep, and will
1870 * complain if the GPIO chip functions potentially sleep.
1872 void gpiod_set_array_value(unsigned int array_size
,
1873 struct gpio_desc
**desc_array
, int *value_array
)
1877 gpiod_set_array_value_priv(false, false, array_size
, desc_array
,
1880 EXPORT_SYMBOL_GPL(gpiod_set_array_value
);
1883 * gpiod_cansleep() - report whether gpio value access may sleep
1884 * @desc: gpio to check
1887 int gpiod_cansleep(const struct gpio_desc
*desc
)
1889 VALIDATE_DESC(desc
);
1890 return desc
->gdev
->chip
->can_sleep
;
1892 EXPORT_SYMBOL_GPL(gpiod_cansleep
);
1895 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1896 * @desc: gpio whose IRQ will be returned (already requested)
1898 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1901 int gpiod_to_irq(const struct gpio_desc
*desc
)
1903 struct gpio_chip
*chip
;
1906 VALIDATE_DESC(desc
);
1907 chip
= desc
->gdev
->chip
;
1908 offset
= gpio_chip_hwgpio(desc
);
1909 return chip
->to_irq
? chip
->to_irq(chip
, offset
) : -ENXIO
;
1911 EXPORT_SYMBOL_GPL(gpiod_to_irq
);
1914 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
1915 * @chip: the chip the GPIO to lock belongs to
1916 * @offset: the offset of the GPIO to lock as IRQ
1918 * This is used directly by GPIO drivers that want to lock down
1919 * a certain GPIO line to be used for IRQs.
1921 int gpiochip_lock_as_irq(struct gpio_chip
*chip
, unsigned int offset
)
1923 if (offset
>= chip
->ngpio
)
1926 if (test_bit(FLAG_IS_OUT
, &chip
->gpiodev
->descs
[offset
].flags
)) {
1928 "%s: tried to flag a GPIO set as output for IRQ\n",
1933 set_bit(FLAG_USED_AS_IRQ
, &chip
->gpiodev
->descs
[offset
].flags
);
1936 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq
);
1939 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
1940 * @chip: the chip the GPIO to lock belongs to
1941 * @offset: the offset of the GPIO to lock as IRQ
1943 * This is used directly by GPIO drivers that want to indicate
1944 * that a certain GPIO is no longer used exclusively for IRQ.
1946 void gpiochip_unlock_as_irq(struct gpio_chip
*chip
, unsigned int offset
)
1948 if (offset
>= chip
->ngpio
)
1951 clear_bit(FLAG_USED_AS_IRQ
, &chip
->gpiodev
->descs
[offset
].flags
);
1953 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq
);
1955 bool gpiochip_line_is_irq(struct gpio_chip
*chip
, unsigned int offset
)
1957 if (offset
>= chip
->ngpio
)
1960 return test_bit(FLAG_USED_AS_IRQ
, &chip
->gpiodev
->descs
[offset
].flags
);
1962 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq
);
1964 bool gpiochip_line_is_open_drain(struct gpio_chip
*chip
, unsigned int offset
)
1966 if (offset
>= chip
->ngpio
)
1969 return test_bit(FLAG_OPEN_DRAIN
, &chip
->gpiodev
->descs
[offset
].flags
);
1971 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain
);
1973 bool gpiochip_line_is_open_source(struct gpio_chip
*chip
, unsigned int offset
)
1975 if (offset
>= chip
->ngpio
)
1978 return test_bit(FLAG_OPEN_SOURCE
, &chip
->gpiodev
->descs
[offset
].flags
);
1980 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source
);
1983 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1984 * @desc: gpio whose value will be returned
1986 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1987 * its ACTIVE_LOW status, or negative errno on failure.
1989 * This function is to be called from contexts that can sleep.
1991 int gpiod_get_raw_value_cansleep(const struct gpio_desc
*desc
)
1993 might_sleep_if(extra_checks
);
1994 VALIDATE_DESC(desc
);
1995 return _gpiod_get_raw_value(desc
);
1997 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep
);
2000 * gpiod_get_value_cansleep() - return a gpio's value
2001 * @desc: gpio whose value will be returned
2003 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2004 * account, or negative errno on failure.
2006 * This function is to be called from contexts that can sleep.
2008 int gpiod_get_value_cansleep(const struct gpio_desc
*desc
)
2012 might_sleep_if(extra_checks
);
2013 VALIDATE_DESC(desc
);
2014 value
= _gpiod_get_raw_value(desc
);
2018 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
2023 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep
);
2026 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2027 * @desc: gpio whose value will be assigned
2028 * @value: value to assign
2030 * Set the raw value of the GPIO, i.e. the value of its physical line without
2031 * regard for its ACTIVE_LOW status.
2033 * This function is to be called from contexts that can sleep.
2035 void gpiod_set_raw_value_cansleep(struct gpio_desc
*desc
, int value
)
2037 might_sleep_if(extra_checks
);
2038 VALIDATE_DESC_VOID(desc
);
2039 _gpiod_set_raw_value(desc
, value
);
2041 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep
);
2044 * gpiod_set_value_cansleep() - assign a gpio's value
2045 * @desc: gpio whose value will be assigned
2046 * @value: value to assign
2048 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2051 * This function is to be called from contexts that can sleep.
2053 void gpiod_set_value_cansleep(struct gpio_desc
*desc
, int value
)
2055 might_sleep_if(extra_checks
);
2056 VALIDATE_DESC_VOID(desc
);
2057 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
2059 _gpiod_set_raw_value(desc
, value
);
2061 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep
);
2064 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
2065 * @array_size: number of elements in the descriptor / value arrays
2066 * @desc_array: array of GPIO descriptors whose values will be assigned
2067 * @value_array: array of values to assign
2069 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2070 * without regard for their ACTIVE_LOW status.
2072 * This function is to be called from contexts that can sleep.
2074 void gpiod_set_raw_array_value_cansleep(unsigned int array_size
,
2075 struct gpio_desc
**desc_array
,
2078 might_sleep_if(extra_checks
);
2081 gpiod_set_array_value_priv(true, true, array_size
, desc_array
,
2084 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep
);
2087 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
2088 * @array_size: number of elements in the descriptor / value arrays
2089 * @desc_array: array of GPIO descriptors whose values will be assigned
2090 * @value_array: array of values to assign
2092 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2095 * This function is to be called from contexts that can sleep.
2097 void gpiod_set_array_value_cansleep(unsigned int array_size
,
2098 struct gpio_desc
**desc_array
,
2101 might_sleep_if(extra_checks
);
2104 gpiod_set_array_value_priv(false, true, array_size
, desc_array
,
2107 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep
);
2110 * gpiod_add_lookup_table() - register GPIO device consumers
2111 * @table: table of consumers to register
2113 void gpiod_add_lookup_table(struct gpiod_lookup_table
*table
)
2115 mutex_lock(&gpio_lookup_lock
);
2117 list_add_tail(&table
->list
, &gpio_lookup_list
);
2119 mutex_unlock(&gpio_lookup_lock
);
2123 * gpiod_remove_lookup_table() - unregister GPIO device consumers
2124 * @table: table of consumers to unregister
2126 void gpiod_remove_lookup_table(struct gpiod_lookup_table
*table
)
2128 mutex_lock(&gpio_lookup_lock
);
2130 list_del(&table
->list
);
2132 mutex_unlock(&gpio_lookup_lock
);
2135 static struct gpio_desc
*of_find_gpio(struct device
*dev
, const char *con_id
,
2137 enum gpio_lookup_flags
*flags
)
2139 char prop_name
[32]; /* 32 is max size of property name */
2140 enum of_gpio_flags of_flags
;
2141 struct gpio_desc
*desc
;
2144 for (i
= 0; i
< ARRAY_SIZE(gpio_suffixes
); i
++) {
2146 snprintf(prop_name
, sizeof(prop_name
), "%s-%s", con_id
,
2149 snprintf(prop_name
, sizeof(prop_name
), "%s",
2152 desc
= of_get_named_gpiod_flags(dev
->of_node
, prop_name
, idx
,
2154 if (!IS_ERR(desc
) || (PTR_ERR(desc
) == -EPROBE_DEFER
))
2161 if (of_flags
& OF_GPIO_ACTIVE_LOW
)
2162 *flags
|= GPIO_ACTIVE_LOW
;
2164 if (of_flags
& OF_GPIO_SINGLE_ENDED
) {
2165 if (of_flags
& OF_GPIO_ACTIVE_LOW
)
2166 *flags
|= GPIO_OPEN_DRAIN
;
2168 *flags
|= GPIO_OPEN_SOURCE
;
2174 static struct gpio_desc
*acpi_find_gpio(struct device
*dev
, const char *con_id
,
2176 enum gpio_lookup_flags
*flags
)
2178 struct acpi_device
*adev
= ACPI_COMPANION(dev
);
2179 struct acpi_gpio_info info
;
2180 struct gpio_desc
*desc
;
2184 /* Try first from _DSD */
2185 for (i
= 0; i
< ARRAY_SIZE(gpio_suffixes
); i
++) {
2186 if (con_id
&& strcmp(con_id
, "gpios")) {
2187 snprintf(propname
, sizeof(propname
), "%s-%s",
2188 con_id
, gpio_suffixes
[i
]);
2190 snprintf(propname
, sizeof(propname
), "%s",
2194 desc
= acpi_get_gpiod_by_index(adev
, propname
, idx
, &info
);
2195 if (!IS_ERR(desc
) || (PTR_ERR(desc
) == -EPROBE_DEFER
))
2199 /* Then from plain _CRS GPIOs */
2201 if (!acpi_can_fallback_to_crs(adev
, con_id
))
2202 return ERR_PTR(-ENOENT
);
2204 desc
= acpi_get_gpiod_by_index(adev
, NULL
, idx
, &info
);
2209 if (info
.polarity
== GPIO_ACTIVE_LOW
)
2210 *flags
|= GPIO_ACTIVE_LOW
;
2215 static struct gpiod_lookup_table
*gpiod_find_lookup_table(struct device
*dev
)
2217 const char *dev_id
= dev
? dev_name(dev
) : NULL
;
2218 struct gpiod_lookup_table
*table
;
2220 mutex_lock(&gpio_lookup_lock
);
2222 list_for_each_entry(table
, &gpio_lookup_list
, list
) {
2223 if (table
->dev_id
&& dev_id
) {
2225 * Valid strings on both ends, must be identical to have
2228 if (!strcmp(table
->dev_id
, dev_id
))
2232 * One of the pointers is NULL, so both must be to have
2235 if (dev_id
== table
->dev_id
)
2242 mutex_unlock(&gpio_lookup_lock
);
2246 static struct gpio_desc
*gpiod_find(struct device
*dev
, const char *con_id
,
2248 enum gpio_lookup_flags
*flags
)
2250 struct gpio_desc
*desc
= ERR_PTR(-ENOENT
);
2251 struct gpiod_lookup_table
*table
;
2252 struct gpiod_lookup
*p
;
2254 table
= gpiod_find_lookup_table(dev
);
2258 for (p
= &table
->table
[0]; p
->chip_label
; p
++) {
2259 struct gpio_chip
*chip
;
2261 /* idx must always match exactly */
2265 /* If the lookup entry has a con_id, require exact match */
2266 if (p
->con_id
&& (!con_id
|| strcmp(p
->con_id
, con_id
)))
2269 chip
= find_chip_by_name(p
->chip_label
);
2272 dev_err(dev
, "cannot find GPIO chip %s\n",
2274 return ERR_PTR(-ENODEV
);
2277 if (chip
->ngpio
<= p
->chip_hwnum
) {
2279 "requested GPIO %d is out of range [0..%d] for chip %s\n",
2280 idx
, chip
->ngpio
, chip
->label
);
2281 return ERR_PTR(-EINVAL
);
2284 desc
= gpiochip_get_desc(chip
, p
->chip_hwnum
);
2293 static int dt_gpio_count(struct device
*dev
, const char *con_id
)
2299 for (i
= 0; i
< ARRAY_SIZE(gpio_suffixes
); i
++) {
2301 snprintf(propname
, sizeof(propname
), "%s-%s",
2302 con_id
, gpio_suffixes
[i
]);
2304 snprintf(propname
, sizeof(propname
), "%s",
2307 ret
= of_gpio_named_count(dev
->of_node
, propname
);
2314 static int platform_gpio_count(struct device
*dev
, const char *con_id
)
2316 struct gpiod_lookup_table
*table
;
2317 struct gpiod_lookup
*p
;
2318 unsigned int count
= 0;
2320 table
= gpiod_find_lookup_table(dev
);
2324 for (p
= &table
->table
[0]; p
->chip_label
; p
++) {
2325 if ((con_id
&& p
->con_id
&& !strcmp(con_id
, p
->con_id
)) ||
2326 (!con_id
&& !p
->con_id
))
2336 * gpiod_count - return the number of GPIOs associated with a device / function
2337 * or -ENOENT if no GPIO has been assigned to the requested function
2338 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2339 * @con_id: function within the GPIO consumer
2341 int gpiod_count(struct device
*dev
, const char *con_id
)
2343 int count
= -ENOENT
;
2345 if (IS_ENABLED(CONFIG_OF
) && dev
&& dev
->of_node
)
2346 count
= dt_gpio_count(dev
, con_id
);
2347 else if (IS_ENABLED(CONFIG_ACPI
) && dev
&& ACPI_HANDLE(dev
))
2348 count
= acpi_gpio_count(dev
, con_id
);
2351 count
= platform_gpio_count(dev
, con_id
);
2355 EXPORT_SYMBOL_GPL(gpiod_count
);
2358 * gpiod_get - obtain a GPIO for a given GPIO function
2359 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2360 * @con_id: function within the GPIO consumer
2361 * @flags: optional GPIO initialization flags
2363 * Return the GPIO descriptor corresponding to the function con_id of device
2364 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
2365 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
2367 struct gpio_desc
*__must_check
gpiod_get(struct device
*dev
, const char *con_id
,
2368 enum gpiod_flags flags
)
2370 return gpiod_get_index(dev
, con_id
, 0, flags
);
2372 EXPORT_SYMBOL_GPL(gpiod_get
);
2375 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
2376 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2377 * @con_id: function within the GPIO consumer
2378 * @flags: optional GPIO initialization flags
2380 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
2381 * the requested function it will return NULL. This is convenient for drivers
2382 * that need to handle optional GPIOs.
2384 struct gpio_desc
*__must_check
gpiod_get_optional(struct device
*dev
,
2386 enum gpiod_flags flags
)
2388 return gpiod_get_index_optional(dev
, con_id
, 0, flags
);
2390 EXPORT_SYMBOL_GPL(gpiod_get_optional
);
2393 * gpiod_parse_flags - helper function to parse GPIO lookup flags
2394 * @desc: gpio to be setup
2395 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2398 * Set the GPIO descriptor flags based on the given GPIO lookup flags.
2400 static void gpiod_parse_flags(struct gpio_desc
*desc
, unsigned long lflags
)
2402 if (lflags
& GPIO_ACTIVE_LOW
)
2403 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
2404 if (lflags
& GPIO_OPEN_DRAIN
)
2405 set_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
2406 if (lflags
& GPIO_OPEN_SOURCE
)
2407 set_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
2411 * gpiod_configure_flags - helper function to configure a given GPIO
2412 * @desc: gpio whose value will be assigned
2413 * @con_id: function within the GPIO consumer
2414 * @dflags: gpiod_flags - optional GPIO initialization flags
2416 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
2417 * requested function and/or index, or another IS_ERR() code if an error
2418 * occurred while trying to acquire the GPIO.
2420 static int gpiod_configure_flags(struct gpio_desc
*desc
, const char *con_id
,
2421 enum gpiod_flags dflags
)
2425 /* No particular flag request, return here... */
2426 if (!(dflags
& GPIOD_FLAGS_BIT_DIR_SET
)) {
2427 pr_debug("no flags found for %s\n", con_id
);
2432 if (dflags
& GPIOD_FLAGS_BIT_DIR_OUT
)
2433 status
= gpiod_direction_output(desc
,
2434 dflags
& GPIOD_FLAGS_BIT_DIR_VAL
);
2436 status
= gpiod_direction_input(desc
);
2442 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
2443 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2444 * @con_id: function within the GPIO consumer
2445 * @idx: index of the GPIO to obtain in the consumer
2446 * @flags: optional GPIO initialization flags
2448 * This variant of gpiod_get() allows to access GPIOs other than the first
2449 * defined one for functions that define several GPIOs.
2451 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2452 * requested function and/or index, or another IS_ERR() code if an error
2453 * occurred while trying to acquire the GPIO.
2455 struct gpio_desc
*__must_check
gpiod_get_index(struct device
*dev
,
2458 enum gpiod_flags flags
)
2460 struct gpio_desc
*desc
= NULL
;
2462 enum gpio_lookup_flags lookupflags
= 0;
2464 dev_dbg(dev
, "GPIO lookup for consumer %s\n", con_id
);
2467 /* Using device tree? */
2468 if (IS_ENABLED(CONFIG_OF
) && dev
->of_node
) {
2469 dev_dbg(dev
, "using device tree for GPIO lookup\n");
2470 desc
= of_find_gpio(dev
, con_id
, idx
, &lookupflags
);
2471 } else if (ACPI_COMPANION(dev
)) {
2472 dev_dbg(dev
, "using ACPI for GPIO lookup\n");
2473 desc
= acpi_find_gpio(dev
, con_id
, idx
, &lookupflags
);
2478 * Either we are not using DT or ACPI, or their lookup did not return
2479 * a result. In that case, use platform lookup as a fallback.
2481 if (!desc
|| desc
== ERR_PTR(-ENOENT
)) {
2482 dev_dbg(dev
, "using lookup tables for GPIO lookup\n");
2483 desc
= gpiod_find(dev
, con_id
, idx
, &lookupflags
);
2487 dev_dbg(dev
, "lookup for GPIO %s failed\n", con_id
);
2491 gpiod_parse_flags(desc
, lookupflags
);
2493 status
= gpiod_request(desc
, con_id
);
2495 return ERR_PTR(status
);
2497 status
= gpiod_configure_flags(desc
, con_id
, flags
);
2499 dev_dbg(dev
, "setup of GPIO %s failed\n", con_id
);
2501 return ERR_PTR(status
);
2506 EXPORT_SYMBOL_GPL(gpiod_get_index
);
2509 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2510 * @fwnode: handle of the firmware node
2511 * @propname: name of the firmware property representing the GPIO
2513 * This function can be used for drivers that get their configuration
2516 * Function properly finds the corresponding GPIO using whatever is the
2517 * underlying firmware interface and then makes sure that the GPIO
2518 * descriptor is requested before it is returned to the caller.
2520 * In case of error an ERR_PTR() is returned.
2522 struct gpio_desc
*fwnode_get_named_gpiod(struct fwnode_handle
*fwnode
,
2523 const char *propname
)
2525 struct gpio_desc
*desc
= ERR_PTR(-ENODEV
);
2526 bool active_low
= false;
2527 bool single_ended
= false;
2531 return ERR_PTR(-EINVAL
);
2533 if (is_of_node(fwnode
)) {
2534 enum of_gpio_flags flags
;
2536 desc
= of_get_named_gpiod_flags(to_of_node(fwnode
), propname
, 0,
2538 if (!IS_ERR(desc
)) {
2539 active_low
= flags
& OF_GPIO_ACTIVE_LOW
;
2540 single_ended
= flags
& OF_GPIO_SINGLE_ENDED
;
2542 } else if (is_acpi_node(fwnode
)) {
2543 struct acpi_gpio_info info
;
2545 desc
= acpi_node_get_gpiod(fwnode
, propname
, 0, &info
);
2547 active_low
= info
.polarity
== GPIO_ACTIVE_LOW
;
2554 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
2558 set_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
2560 set_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
2563 ret
= gpiod_request(desc
, NULL
);
2565 return ERR_PTR(ret
);
2569 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod
);
2572 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2574 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2575 * @con_id: function within the GPIO consumer
2576 * @index: index of the GPIO to obtain in the consumer
2577 * @flags: optional GPIO initialization flags
2579 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2580 * specified index was assigned to the requested function it will return NULL.
2581 * This is convenient for drivers that need to handle optional GPIOs.
2583 struct gpio_desc
*__must_check
gpiod_get_index_optional(struct device
*dev
,
2586 enum gpiod_flags flags
)
2588 struct gpio_desc
*desc
;
2590 desc
= gpiod_get_index(dev
, con_id
, index
, flags
);
2592 if (PTR_ERR(desc
) == -ENOENT
)
2598 EXPORT_SYMBOL_GPL(gpiod_get_index_optional
);
2601 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2602 * @desc: gpio whose value will be assigned
2603 * @name: gpio line name
2604 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2606 * @dflags: gpiod_flags - optional GPIO initialization flags
2608 int gpiod_hog(struct gpio_desc
*desc
, const char *name
,
2609 unsigned long lflags
, enum gpiod_flags dflags
)
2611 struct gpio_chip
*chip
;
2612 struct gpio_desc
*local_desc
;
2616 chip
= gpiod_to_chip(desc
);
2617 hwnum
= gpio_chip_hwgpio(desc
);
2619 gpiod_parse_flags(desc
, lflags
);
2621 local_desc
= gpiochip_request_own_desc(chip
, hwnum
, name
);
2622 if (IS_ERR(local_desc
)) {
2623 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
2624 name
, chip
->label
, hwnum
);
2625 return PTR_ERR(local_desc
);
2628 status
= gpiod_configure_flags(desc
, name
, dflags
);
2630 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
2631 name
, chip
->label
, hwnum
);
2632 gpiochip_free_own_desc(desc
);
2636 /* Mark GPIO as hogged so it can be identified and removed later */
2637 set_bit(FLAG_IS_HOGGED
, &desc
->flags
);
2639 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2640 desc_to_gpio(desc
), name
,
2641 (dflags
&GPIOD_FLAGS_BIT_DIR_OUT
) ? "output" : "input",
2642 (dflags
&GPIOD_FLAGS_BIT_DIR_OUT
) ?
2643 (dflags
&GPIOD_FLAGS_BIT_DIR_VAL
) ? "/high" : "/low":"");
2649 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2650 * @chip: gpio chip to act on
2652 * This is only used by of_gpiochip_remove to free hogged gpios
2654 static void gpiochip_free_hogs(struct gpio_chip
*chip
)
2658 for (id
= 0; id
< chip
->ngpio
; id
++) {
2659 if (test_bit(FLAG_IS_HOGGED
, &chip
->gpiodev
->descs
[id
].flags
))
2660 gpiochip_free_own_desc(&chip
->gpiodev
->descs
[id
]);
2665 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2666 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2667 * @con_id: function within the GPIO consumer
2668 * @flags: optional GPIO initialization flags
2670 * This function acquires all the GPIOs defined under a given function.
2672 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2673 * no GPIO has been assigned to the requested function, or another IS_ERR()
2674 * code if an error occurred while trying to acquire the GPIOs.
2676 struct gpio_descs
*__must_check
gpiod_get_array(struct device
*dev
,
2678 enum gpiod_flags flags
)
2680 struct gpio_desc
*desc
;
2681 struct gpio_descs
*descs
;
2684 count
= gpiod_count(dev
, con_id
);
2686 return ERR_PTR(count
);
2688 descs
= kzalloc(sizeof(*descs
) + sizeof(descs
->desc
[0]) * count
,
2691 return ERR_PTR(-ENOMEM
);
2693 for (descs
->ndescs
= 0; descs
->ndescs
< count
; ) {
2694 desc
= gpiod_get_index(dev
, con_id
, descs
->ndescs
, flags
);
2696 gpiod_put_array(descs
);
2697 return ERR_CAST(desc
);
2699 descs
->desc
[descs
->ndescs
] = desc
;
2704 EXPORT_SYMBOL_GPL(gpiod_get_array
);
2707 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2709 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2710 * @con_id: function within the GPIO consumer
2711 * @flags: optional GPIO initialization flags
2713 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2714 * assigned to the requested function it will return NULL.
2716 struct gpio_descs
*__must_check
gpiod_get_array_optional(struct device
*dev
,
2718 enum gpiod_flags flags
)
2720 struct gpio_descs
*descs
;
2722 descs
= gpiod_get_array(dev
, con_id
, flags
);
2723 if (IS_ERR(descs
) && (PTR_ERR(descs
) == -ENOENT
))
2728 EXPORT_SYMBOL_GPL(gpiod_get_array_optional
);
2731 * gpiod_put - dispose of a GPIO descriptor
2732 * @desc: GPIO descriptor to dispose of
2734 * No descriptor can be used after gpiod_put() has been called on it.
2736 void gpiod_put(struct gpio_desc
*desc
)
2740 EXPORT_SYMBOL_GPL(gpiod_put
);
2743 * gpiod_put_array - dispose of multiple GPIO descriptors
2744 * @descs: struct gpio_descs containing an array of descriptors
2746 void gpiod_put_array(struct gpio_descs
*descs
)
2750 for (i
= 0; i
< descs
->ndescs
; i
++)
2751 gpiod_put(descs
->desc
[i
]);
2755 EXPORT_SYMBOL_GPL(gpiod_put_array
);
2757 static int __init
gpiolib_dev_init(void)
2761 /* Register GPIO sysfs bus */
2762 ret
= bus_register(&gpio_bus_type
);
2764 pr_err("gpiolib: could not register GPIO bus type\n");
2768 ret
= alloc_chrdev_region(&gpio_devt
, 0, GPIO_DEV_MAX
, "gpiochip");
2770 pr_err("gpiolib: failed to allocate char dev region\n");
2771 bus_unregister(&gpio_bus_type
);
2775 core_initcall(gpiolib_dev_init
);
2777 #ifdef CONFIG_DEBUG_FS
2779 static void gpiolib_dbg_show(struct seq_file
*s
, struct gpio_device
*gdev
)
2782 struct gpio_chip
*chip
= gdev
->chip
;
2783 unsigned gpio
= gdev
->base
;
2784 struct gpio_desc
*gdesc
= &gdev
->descs
[0];
2788 for (i
= 0; i
< gdev
->ngpio
; i
++, gpio
++, gdesc
++) {
2789 if (!test_bit(FLAG_REQUESTED
, &gdesc
->flags
)) {
2791 seq_printf(s
, " gpio-%-3d (%-20.20s)\n",
2797 gpiod_get_direction(gdesc
);
2798 is_out
= test_bit(FLAG_IS_OUT
, &gdesc
->flags
);
2799 is_irq
= test_bit(FLAG_USED_AS_IRQ
, &gdesc
->flags
);
2800 seq_printf(s
, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
2801 gpio
, gdesc
->name
? gdesc
->name
: "", gdesc
->label
,
2802 is_out
? "out" : "in ",
2804 ? (chip
->get(chip
, i
) ? "hi" : "lo")
2806 is_irq
? "IRQ" : " ");
2807 seq_printf(s
, "\n");
2811 static void *gpiolib_seq_start(struct seq_file
*s
, loff_t
*pos
)
2813 unsigned long flags
;
2814 struct gpio_device
*gdev
= NULL
;
2815 loff_t index
= *pos
;
2819 spin_lock_irqsave(&gpio_lock
, flags
);
2820 list_for_each_entry(gdev
, &gpio_devices
, list
)
2822 spin_unlock_irqrestore(&gpio_lock
, flags
);
2825 spin_unlock_irqrestore(&gpio_lock
, flags
);
2830 static void *gpiolib_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
2832 unsigned long flags
;
2833 struct gpio_device
*gdev
= v
;
2836 spin_lock_irqsave(&gpio_lock
, flags
);
2837 if (list_is_last(&gdev
->list
, &gpio_devices
))
2840 ret
= list_entry(gdev
->list
.next
, struct gpio_device
, list
);
2841 spin_unlock_irqrestore(&gpio_lock
, flags
);
2849 static void gpiolib_seq_stop(struct seq_file
*s
, void *v
)
2853 static int gpiolib_seq_show(struct seq_file
*s
, void *v
)
2855 struct gpio_device
*gdev
= v
;
2856 struct gpio_chip
*chip
= gdev
->chip
;
2857 struct device
*parent
;
2860 seq_printf(s
, "%s%s: (dangling chip)", (char *)s
->private,
2861 dev_name(&gdev
->dev
));
2865 seq_printf(s
, "%s%s: GPIOs %d-%d", (char *)s
->private,
2866 dev_name(&gdev
->dev
),
2867 gdev
->base
, gdev
->base
+ gdev
->ngpio
- 1);
2868 parent
= chip
->parent
;
2870 seq_printf(s
, ", parent: %s/%s",
2871 parent
->bus
? parent
->bus
->name
: "no-bus",
2874 seq_printf(s
, ", %s", chip
->label
);
2875 if (chip
->can_sleep
)
2876 seq_printf(s
, ", can sleep");
2877 seq_printf(s
, ":\n");
2880 chip
->dbg_show(s
, chip
);
2882 gpiolib_dbg_show(s
, gdev
);
2887 static const struct seq_operations gpiolib_seq_ops
= {
2888 .start
= gpiolib_seq_start
,
2889 .next
= gpiolib_seq_next
,
2890 .stop
= gpiolib_seq_stop
,
2891 .show
= gpiolib_seq_show
,
2894 static int gpiolib_open(struct inode
*inode
, struct file
*file
)
2896 return seq_open(file
, &gpiolib_seq_ops
);
2899 static const struct file_operations gpiolib_operations
= {
2900 .owner
= THIS_MODULE
,
2901 .open
= gpiolib_open
,
2903 .llseek
= seq_lseek
,
2904 .release
= seq_release
,
2907 static int __init
gpiolib_debugfs_init(void)
2909 /* /sys/kernel/debug/gpio */
2910 (void) debugfs_create_file("gpio", S_IFREG
| S_IRUGO
,
2911 NULL
, NULL
, &gpiolib_operations
);
2914 subsys_initcall(gpiolib_debugfs_init
);
2916 #endif /* DEBUG_FS */