1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/bitmap.h>
3 #include <linux/kernel.h>
4 #include <linux/module.h>
5 #include <linux/interrupt.h>
7 #include <linux/spinlock.h>
8 #include <linux/list.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/debugfs.h>
12 #include <linux/seq_file.h>
13 #include <linux/gpio.h>
14 #include <linux/idr.h>
15 #include <linux/slab.h>
16 #include <linux/acpi.h>
17 #include <linux/gpio/driver.h>
18 #include <linux/gpio/machine.h>
19 #include <linux/pinctrl/consumer.h>
20 #include <linux/cdev.h>
22 #include <linux/uaccess.h>
23 #include <linux/compat.h>
24 #include <linux/anon_inodes.h>
25 #include <linux/file.h>
26 #include <linux/kfifo.h>
27 #include <linux/poll.h>
28 #include <linux/timekeeping.h>
29 #include <uapi/linux/gpio.h>
32 #include "gpiolib-of.h"
33 #include "gpiolib-acpi.h"
35 #define CREATE_TRACE_POINTS
36 #include <trace/events/gpio.h>
38 /* Implementation infrastructure for GPIO interfaces.
40 * The GPIO programming interface allows for inlining speed-critical
41 * get/set operations for common cases, so that access to SOC-integrated
42 * GPIOs can sometimes cost only an instruction or two per bit.
46 /* When debugging, extend minimal trust to callers and platform code.
47 * Also emit diagnostic messages that may help initial bringup, when
48 * board setup or driver bugs are most common.
50 * Otherwise, minimize overhead in what may be bitbanging codepaths.
53 #define extra_checks 1
55 #define extra_checks 0
58 /* Device and char device-related information */
59 static DEFINE_IDA(gpio_ida);
60 static dev_t gpio_devt;
61 #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
62 static struct bus_type gpio_bus_type = {
67 * Number of GPIOs to use for the fast path in set array
69 #define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT
71 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
72 * While any GPIO is requested, its gpio_chip is not removable;
73 * each GPIO's "requested" flag serves as a lock and refcount.
75 DEFINE_SPINLOCK(gpio_lock);
77 static DEFINE_MUTEX(gpio_lookup_lock);
78 static LIST_HEAD(gpio_lookup_list);
79 LIST_HEAD(gpio_devices);
81 static DEFINE_MUTEX(gpio_machine_hogs_mutex);
82 static LIST_HEAD(gpio_machine_hogs);
84 static void gpiochip_free_hogs(struct gpio_chip *chip);
85 static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
86 struct lock_class_key *lock_key,
87 struct lock_class_key *request_key);
88 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
89 static int gpiochip_irqchip_init_hw(struct gpio_chip *gpiochip);
90 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
91 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
93 static bool gpiolib_initialized;
95 static inline void desc_set_label(struct gpio_desc *d, const char *label)
101 * gpio_to_desc - Convert a GPIO number to its descriptor
102 * @gpio: global GPIO number
105 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
106 * with the given number exists in the system.
108 struct gpio_desc *gpio_to_desc(unsigned gpio)
110 struct gpio_device *gdev;
113 spin_lock_irqsave(&gpio_lock, flags);
115 list_for_each_entry(gdev, &gpio_devices, list) {
116 if (gdev->base <= gpio &&
117 gdev->base + gdev->ngpio > gpio) {
118 spin_unlock_irqrestore(&gpio_lock, flags);
119 return &gdev->descs[gpio - gdev->base];
123 spin_unlock_irqrestore(&gpio_lock, flags);
125 if (!gpio_is_valid(gpio))
126 WARN(1, "invalid GPIO %d\n", gpio);
130 EXPORT_SYMBOL_GPL(gpio_to_desc);
133 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
134 * hardware number for this chip
136 * @hwnum: hardware number of the GPIO for this chip
139 * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists
140 * in the given chip for the specified hardware number.
142 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
145 struct gpio_device *gdev = chip->gpiodev;
147 if (hwnum >= gdev->ngpio)
148 return ERR_PTR(-EINVAL);
150 return &gdev->descs[hwnum];
154 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
155 * @desc: GPIO descriptor
157 * This should disappear in the future but is needed since we still
158 * use GPIO numbers for error messages and sysfs nodes.
161 * The global GPIO number for the GPIO specified by its descriptor.
163 int desc_to_gpio(const struct gpio_desc *desc)
165 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
167 EXPORT_SYMBOL_GPL(desc_to_gpio);
171 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
172 * @desc: descriptor to return the chip of
174 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
176 if (!desc || !desc->gdev)
178 return desc->gdev->chip;
180 EXPORT_SYMBOL_GPL(gpiod_to_chip);
182 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
183 static int gpiochip_find_base(int ngpio)
185 struct gpio_device *gdev;
186 int base = ARCH_NR_GPIOS - ngpio;
188 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
189 /* found a free space? */
190 if (gdev->base + gdev->ngpio <= base)
193 /* nope, check the space right before the chip */
194 base = gdev->base - ngpio;
197 if (gpio_is_valid(base)) {
198 pr_debug("%s: found new base at %d\n", __func__, base);
201 pr_err("%s: cannot find free range\n", __func__);
207 * gpiod_get_direction - return the current direction of a GPIO
208 * @desc: GPIO to get the direction of
210 * Returns 0 for output, 1 for input, or an error code in case of error.
212 * This function may sleep if gpiod_cansleep() is true.
214 int gpiod_get_direction(struct gpio_desc *desc)
216 struct gpio_chip *chip;
220 chip = gpiod_to_chip(desc);
221 offset = gpio_chip_hwgpio(desc);
223 if (!chip->get_direction)
226 ret = chip->get_direction(chip, offset);
228 /* GPIOF_DIR_IN, or other positive */
230 clear_bit(FLAG_IS_OUT, &desc->flags);
234 set_bit(FLAG_IS_OUT, &desc->flags);
238 EXPORT_SYMBOL_GPL(gpiod_get_direction);
241 * Add a new chip to the global chips list, keeping the list of chips sorted
242 * by range(means [base, base + ngpio - 1]) order.
244 * Return -EBUSY if the new chip overlaps with some other chip's integer
247 static int gpiodev_add_to_list(struct gpio_device *gdev)
249 struct gpio_device *prev, *next;
251 if (list_empty(&gpio_devices)) {
252 /* initial entry in list */
253 list_add_tail(&gdev->list, &gpio_devices);
257 next = list_entry(gpio_devices.next, struct gpio_device, list);
258 if (gdev->base + gdev->ngpio <= next->base) {
259 /* add before first entry */
260 list_add(&gdev->list, &gpio_devices);
264 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
265 if (prev->base + prev->ngpio <= gdev->base) {
266 /* add behind last entry */
267 list_add_tail(&gdev->list, &gpio_devices);
271 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
272 /* at the end of the list */
273 if (&next->list == &gpio_devices)
276 /* add between prev and next */
277 if (prev->base + prev->ngpio <= gdev->base
278 && gdev->base + gdev->ngpio <= next->base) {
279 list_add(&gdev->list, &prev->list);
284 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
289 * Convert a GPIO name to its descriptor
291 static struct gpio_desc *gpio_name_to_desc(const char * const name)
293 struct gpio_device *gdev;
296 spin_lock_irqsave(&gpio_lock, flags);
298 list_for_each_entry(gdev, &gpio_devices, list) {
301 for (i = 0; i != gdev->ngpio; ++i) {
302 struct gpio_desc *desc = &gdev->descs[i];
304 if (!desc->name || !name)
307 if (!strcmp(desc->name, name)) {
308 spin_unlock_irqrestore(&gpio_lock, flags);
314 spin_unlock_irqrestore(&gpio_lock, flags);
320 * Takes the names from gc->names and checks if they are all unique. If they
321 * are, they are assigned to their gpio descriptors.
323 * Warning if one of the names is already used for a different GPIO.
325 static int gpiochip_set_desc_names(struct gpio_chip *gc)
327 struct gpio_device *gdev = gc->gpiodev;
333 /* First check all names if they are unique */
334 for (i = 0; i != gc->ngpio; ++i) {
335 struct gpio_desc *gpio;
337 gpio = gpio_name_to_desc(gc->names[i]);
340 "Detected name collision for GPIO name '%s'\n",
344 /* Then add all names to the GPIO descriptors */
345 for (i = 0; i != gc->ngpio; ++i)
346 gdev->descs[i].name = gc->names[i];
351 static unsigned long *gpiochip_allocate_mask(struct gpio_chip *chip)
355 p = bitmap_alloc(chip->ngpio, GFP_KERNEL);
359 /* Assume by default all GPIOs are valid */
360 bitmap_fill(p, chip->ngpio);
365 static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
367 if (!(of_gpio_need_valid_mask(gc) || gc->init_valid_mask))
370 gc->valid_mask = gpiochip_allocate_mask(gc);
377 static int gpiochip_init_valid_mask(struct gpio_chip *gc)
379 if (gc->init_valid_mask)
380 return gc->init_valid_mask(gc,
387 static void gpiochip_free_valid_mask(struct gpio_chip *gpiochip)
389 bitmap_free(gpiochip->valid_mask);
390 gpiochip->valid_mask = NULL;
393 bool gpiochip_line_is_valid(const struct gpio_chip *gpiochip,
396 /* No mask means all valid */
397 if (likely(!gpiochip->valid_mask))
399 return test_bit(offset, gpiochip->valid_mask);
401 EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
404 * GPIO line handle management
408 * struct linehandle_state - contains the state of a userspace handle
409 * @gdev: the GPIO device the handle pertains to
410 * @label: consumer label used to tag descriptors
411 * @descs: the GPIO descriptors held by this handle
412 * @numdescs: the number of descriptors held in the descs array
414 struct linehandle_state {
415 struct gpio_device *gdev;
417 struct gpio_desc *descs[GPIOHANDLES_MAX];
421 #define GPIOHANDLE_REQUEST_VALID_FLAGS \
422 (GPIOHANDLE_REQUEST_INPUT | \
423 GPIOHANDLE_REQUEST_OUTPUT | \
424 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
425 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
426 GPIOHANDLE_REQUEST_OPEN_SOURCE)
428 static long linehandle_ioctl(struct file *filep, unsigned int cmd,
431 struct linehandle_state *lh = filep->private_data;
432 void __user *ip = (void __user *)arg;
433 struct gpiohandle_data ghd;
434 DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
437 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
438 /* NOTE: It's ok to read values of output lines. */
439 int ret = gpiod_get_array_value_complex(false,
448 memset(&ghd, 0, sizeof(ghd));
449 for (i = 0; i < lh->numdescs; i++)
450 ghd.values[i] = test_bit(i, vals);
452 if (copy_to_user(ip, &ghd, sizeof(ghd)))
456 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
458 * All line descriptors were created at once with the same
459 * flags so just check if the first one is really output.
461 if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
464 if (copy_from_user(&ghd, ip, sizeof(ghd)))
467 /* Clamp all values to [0,1] */
468 for (i = 0; i < lh->numdescs; i++)
469 __assign_bit(i, vals, ghd.values[i]);
471 /* Reuse the array setting function */
472 return gpiod_set_array_value_complex(false,
483 static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
486 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
490 static int linehandle_release(struct inode *inode, struct file *filep)
492 struct linehandle_state *lh = filep->private_data;
493 struct gpio_device *gdev = lh->gdev;
496 for (i = 0; i < lh->numdescs; i++)
497 gpiod_free(lh->descs[i]);
500 put_device(&gdev->dev);
504 static const struct file_operations linehandle_fileops = {
505 .release = linehandle_release,
506 .owner = THIS_MODULE,
507 .llseek = noop_llseek,
508 .unlocked_ioctl = linehandle_ioctl,
510 .compat_ioctl = linehandle_ioctl_compat,
514 static int linehandle_create(struct gpio_device *gdev, void __user *ip)
516 struct gpiohandle_request handlereq;
517 struct linehandle_state *lh;
519 int fd, i, count = 0, ret;
522 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
524 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
527 lflags = handlereq.flags;
529 /* Return an error if an unknown flag is set */
530 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
534 * Do not allow both INPUT & OUTPUT flags to be set as they are
537 if ((lflags & GPIOHANDLE_REQUEST_INPUT) &&
538 (lflags & GPIOHANDLE_REQUEST_OUTPUT))
542 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
543 * the hardware actually supports enabling both at the same time the
544 * electrical result would be disastrous.
546 if ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
547 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
550 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
551 if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) &&
552 ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
553 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
556 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
560 get_device(&gdev->dev);
562 /* Make sure this is terminated */
563 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
564 if (strlen(handlereq.consumer_label)) {
565 lh->label = kstrdup(handlereq.consumer_label,
573 /* Request each GPIO */
574 for (i = 0; i < handlereq.lines; i++) {
575 u32 offset = handlereq.lineoffsets[i];
576 struct gpio_desc *desc;
578 if (offset >= gdev->ngpio) {
583 desc = &gdev->descs[offset];
584 ret = gpiod_request(desc, lh->label);
590 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
591 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
592 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
593 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
594 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
595 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
597 ret = gpiod_set_transitory(desc, false);
602 * Lines have to be requested explicitly for input
603 * or output, else the line will be treated "as is".
605 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
606 int val = !!handlereq.default_values[i];
608 ret = gpiod_direction_output(desc, val);
611 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
612 ret = gpiod_direction_input(desc);
616 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
619 /* Let i point at the last handle */
621 lh->numdescs = handlereq.lines;
623 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
629 file = anon_inode_getfile("gpio-linehandle",
632 O_RDONLY | O_CLOEXEC);
635 goto out_put_unused_fd;
639 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
641 * fput() will trigger the release() callback, so do not go onto
642 * the regular error cleanup path here.
649 fd_install(fd, file);
651 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
659 for (i = 0; i < count; i++)
660 gpiod_free(lh->descs[i]);
664 put_device(&gdev->dev);
669 * GPIO line event management
673 * struct lineevent_state - contains the state of a userspace event
674 * @gdev: the GPIO device the event pertains to
675 * @label: consumer label used to tag descriptors
676 * @desc: the GPIO descriptor held by this event
677 * @eflags: the event flags this line was requested with
678 * @irq: the interrupt that trigger in response to events on this GPIO
679 * @wait: wait queue that handles blocking reads of events
680 * @events: KFIFO for the GPIO events
681 * @read_lock: mutex lock to protect reads from colliding with adding
682 * new events to the FIFO
683 * @timestamp: cache for the timestamp storing it between hardirq
684 * and IRQ thread, used to bring the timestamp close to the actual
687 struct lineevent_state {
688 struct gpio_device *gdev;
690 struct gpio_desc *desc;
693 wait_queue_head_t wait;
694 DECLARE_KFIFO(events, struct gpioevent_data, 16);
695 struct mutex read_lock;
699 #define GPIOEVENT_REQUEST_VALID_FLAGS \
700 (GPIOEVENT_REQUEST_RISING_EDGE | \
701 GPIOEVENT_REQUEST_FALLING_EDGE)
703 static __poll_t lineevent_poll(struct file *filep,
704 struct poll_table_struct *wait)
706 struct lineevent_state *le = filep->private_data;
709 poll_wait(filep, &le->wait, wait);
711 if (!kfifo_is_empty(&le->events))
712 events = EPOLLIN | EPOLLRDNORM;
718 static ssize_t lineevent_read(struct file *filep,
723 struct lineevent_state *le = filep->private_data;
727 if (count < sizeof(struct gpioevent_data))
731 if (kfifo_is_empty(&le->events)) {
732 if (filep->f_flags & O_NONBLOCK)
735 ret = wait_event_interruptible(le->wait,
736 !kfifo_is_empty(&le->events));
741 if (mutex_lock_interruptible(&le->read_lock))
743 ret = kfifo_to_user(&le->events, buf, count, &copied);
744 mutex_unlock(&le->read_lock);
750 * If we couldn't read anything from the fifo (a different
751 * thread might have been faster) we either return -EAGAIN if
752 * the file descriptor is non-blocking, otherwise we go back to
753 * sleep and wait for more data to arrive.
755 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
758 } while (copied == 0);
763 static int lineevent_release(struct inode *inode, struct file *filep)
765 struct lineevent_state *le = filep->private_data;
766 struct gpio_device *gdev = le->gdev;
768 free_irq(le->irq, le);
769 gpiod_free(le->desc);
772 put_device(&gdev->dev);
776 static long lineevent_ioctl(struct file *filep, unsigned int cmd,
779 struct lineevent_state *le = filep->private_data;
780 void __user *ip = (void __user *)arg;
781 struct gpiohandle_data ghd;
784 * We can get the value for an event line but not set it,
785 * because it is input by definition.
787 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
790 memset(&ghd, 0, sizeof(ghd));
792 val = gpiod_get_value_cansleep(le->desc);
797 if (copy_to_user(ip, &ghd, sizeof(ghd)))
806 static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
809 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
813 static const struct file_operations lineevent_fileops = {
814 .release = lineevent_release,
815 .read = lineevent_read,
816 .poll = lineevent_poll,
817 .owner = THIS_MODULE,
818 .llseek = noop_llseek,
819 .unlocked_ioctl = lineevent_ioctl,
821 .compat_ioctl = lineevent_ioctl_compat,
825 static irqreturn_t lineevent_irq_thread(int irq, void *p)
827 struct lineevent_state *le = p;
828 struct gpioevent_data ge;
831 /* Do not leak kernel stack to userspace */
832 memset(&ge, 0, sizeof(ge));
835 * We may be running from a nested threaded interrupt in which case
836 * we didn't get the timestamp from lineevent_irq_handler().
839 ge.timestamp = ktime_get_real_ns();
841 ge.timestamp = le->timestamp;
843 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
844 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
845 int level = gpiod_get_value_cansleep(le->desc);
847 /* Emit low-to-high event */
848 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
850 /* Emit high-to-low event */
851 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
852 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
853 /* Emit low-to-high event */
854 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
855 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
856 /* Emit high-to-low event */
857 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
862 ret = kfifo_put(&le->events, ge);
864 wake_up_poll(&le->wait, EPOLLIN);
869 static irqreturn_t lineevent_irq_handler(int irq, void *p)
871 struct lineevent_state *le = p;
874 * Just store the timestamp in hardirq context so we get it as
875 * close in time as possible to the actual event.
877 le->timestamp = ktime_get_real_ns();
879 return IRQ_WAKE_THREAD;
882 static int lineevent_create(struct gpio_device *gdev, void __user *ip)
884 struct gpioevent_request eventreq;
885 struct lineevent_state *le;
886 struct gpio_desc *desc;
895 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
898 le = kzalloc(sizeof(*le), GFP_KERNEL);
902 get_device(&gdev->dev);
904 /* Make sure this is terminated */
905 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
906 if (strlen(eventreq.consumer_label)) {
907 le->label = kstrdup(eventreq.consumer_label,
915 offset = eventreq.lineoffset;
916 lflags = eventreq.handleflags;
917 eflags = eventreq.eventflags;
919 if (offset >= gdev->ngpio) {
924 /* Return an error if a unknown flag is set */
925 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
926 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
931 /* This is just wrong: we don't look for events on output lines */
932 if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
933 (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
934 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) {
939 desc = &gdev->descs[offset];
940 ret = gpiod_request(desc, le->label);
946 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
947 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
949 ret = gpiod_direction_input(desc);
953 le->irq = gpiod_to_irq(desc);
959 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
960 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
961 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
962 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
963 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
964 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
965 irqflags |= IRQF_ONESHOT;
967 INIT_KFIFO(le->events);
968 init_waitqueue_head(&le->wait);
969 mutex_init(&le->read_lock);
971 /* Request a thread to read the events */
972 ret = request_threaded_irq(le->irq,
973 lineevent_irq_handler,
974 lineevent_irq_thread,
981 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
987 file = anon_inode_getfile("gpio-event",
990 O_RDONLY | O_CLOEXEC);
993 goto out_put_unused_fd;
997 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
999 * fput() will trigger the release() callback, so do not go onto
1000 * the regular error cleanup path here.
1007 fd_install(fd, file);
1014 free_irq(le->irq, le);
1016 gpiod_free(le->desc);
1021 put_device(&gdev->dev);
1026 * gpio_ioctl() - ioctl handler for the GPIO chardev
1028 static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1030 struct gpio_device *gdev = filp->private_data;
1031 struct gpio_chip *chip = gdev->chip;
1032 void __user *ip = (void __user *)arg;
1034 /* We fail any subsequent ioctl():s when the chip is gone */
1038 /* Fill in the struct and pass to userspace */
1039 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
1040 struct gpiochip_info chipinfo;
1042 memset(&chipinfo, 0, sizeof(chipinfo));
1044 strncpy(chipinfo.name, dev_name(&gdev->dev),
1045 sizeof(chipinfo.name));
1046 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
1047 strncpy(chipinfo.label, gdev->label,
1048 sizeof(chipinfo.label));
1049 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
1050 chipinfo.lines = gdev->ngpio;
1051 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
1054 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
1055 struct gpioline_info lineinfo;
1056 struct gpio_desc *desc;
1058 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1060 if (lineinfo.line_offset >= gdev->ngpio)
1063 desc = &gdev->descs[lineinfo.line_offset];
1065 strncpy(lineinfo.name, desc->name,
1066 sizeof(lineinfo.name));
1067 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
1069 lineinfo.name[0] = '\0';
1072 strncpy(lineinfo.consumer, desc->label,
1073 sizeof(lineinfo.consumer));
1074 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
1076 lineinfo.consumer[0] = '\0';
1080 * Userspace only need to know that the kernel is using
1081 * this GPIO so it can't use it.
1084 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
1085 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
1086 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
1087 test_bit(FLAG_EXPORT, &desc->flags) ||
1088 test_bit(FLAG_SYSFS, &desc->flags) ||
1089 !pinctrl_gpio_can_use_line(chip->base + lineinfo.line_offset))
1090 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
1091 if (test_bit(FLAG_IS_OUT, &desc->flags))
1092 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
1093 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1094 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
1095 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1096 lineinfo.flags |= (GPIOLINE_FLAG_OPEN_DRAIN |
1097 GPIOLINE_FLAG_IS_OUT);
1098 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1099 lineinfo.flags |= (GPIOLINE_FLAG_OPEN_SOURCE |
1100 GPIOLINE_FLAG_IS_OUT);
1102 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
1105 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
1106 return linehandle_create(gdev, ip);
1107 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
1108 return lineevent_create(gdev, ip);
1113 #ifdef CONFIG_COMPAT
1114 static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
1117 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
1122 * gpio_chrdev_open() - open the chardev for ioctl operations
1123 * @inode: inode for this chardev
1124 * @filp: file struct for storing private data
1125 * Returns 0 on success
1127 static int gpio_chrdev_open(struct inode *inode, struct file *filp)
1129 struct gpio_device *gdev = container_of(inode->i_cdev,
1130 struct gpio_device, chrdev);
1132 /* Fail on open if the backing gpiochip is gone */
1135 get_device(&gdev->dev);
1136 filp->private_data = gdev;
1138 return nonseekable_open(inode, filp);
1142 * gpio_chrdev_release() - close chardev after ioctl operations
1143 * @inode: inode for this chardev
1144 * @filp: file struct for storing private data
1145 * Returns 0 on success
1147 static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1149 struct gpio_device *gdev = container_of(inode->i_cdev,
1150 struct gpio_device, chrdev);
1152 put_device(&gdev->dev);
1157 static const struct file_operations gpio_fileops = {
1158 .release = gpio_chrdev_release,
1159 .open = gpio_chrdev_open,
1160 .owner = THIS_MODULE,
1161 .llseek = no_llseek,
1162 .unlocked_ioctl = gpio_ioctl,
1163 #ifdef CONFIG_COMPAT
1164 .compat_ioctl = gpio_ioctl_compat,
1168 static void gpiodevice_release(struct device *dev)
1170 struct gpio_device *gdev = dev_get_drvdata(dev);
1172 list_del(&gdev->list);
1173 ida_simple_remove(&gpio_ida, gdev->id);
1174 kfree_const(gdev->label);
1179 static int gpiochip_setup_dev(struct gpio_device *gdev)
1183 cdev_init(&gdev->chrdev, &gpio_fileops);
1184 gdev->chrdev.owner = THIS_MODULE;
1185 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
1187 ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
1191 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1192 MAJOR(gpio_devt), gdev->id);
1194 ret = gpiochip_sysfs_register(gdev);
1196 goto err_remove_device;
1198 /* From this point, the .release() function cleans up gpio_device */
1199 gdev->dev.release = gpiodevice_release;
1200 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1201 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1202 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1207 cdev_device_del(&gdev->chrdev, &gdev->dev);
1211 static void gpiochip_machine_hog(struct gpio_chip *chip, struct gpiod_hog *hog)
1213 struct gpio_desc *desc;
1216 desc = gpiochip_get_desc(chip, hog->chip_hwnum);
1218 pr_err("%s: unable to get GPIO desc: %ld\n",
1219 __func__, PTR_ERR(desc));
1223 if (test_bit(FLAG_IS_HOGGED, &desc->flags))
1226 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
1228 pr_err("%s: unable to hog GPIO line (%s:%u): %d\n",
1229 __func__, chip->label, hog->chip_hwnum, rv);
1232 static void machine_gpiochip_add(struct gpio_chip *chip)
1234 struct gpiod_hog *hog;
1236 mutex_lock(&gpio_machine_hogs_mutex);
1238 list_for_each_entry(hog, &gpio_machine_hogs, list) {
1239 if (!strcmp(chip->label, hog->chip_label))
1240 gpiochip_machine_hog(chip, hog);
1243 mutex_unlock(&gpio_machine_hogs_mutex);
1246 static void gpiochip_setup_devs(void)
1248 struct gpio_device *gdev;
1251 list_for_each_entry(gdev, &gpio_devices, list) {
1252 ret = gpiochip_setup_dev(gdev);
1254 pr_err("%s: Failed to initialize gpio device (%d)\n",
1255 dev_name(&gdev->dev), ret);
1259 int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
1260 struct lock_class_key *lock_key,
1261 struct lock_class_key *request_key)
1263 unsigned long flags;
1266 int base = chip->base;
1267 struct gpio_device *gdev;
1270 * First: allocate and populate the internal stat container, and
1271 * set up the struct device.
1273 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
1276 gdev->dev.bus = &gpio_bus_type;
1278 chip->gpiodev = gdev;
1280 gdev->dev.parent = chip->parent;
1281 gdev->dev.of_node = chip->parent->of_node;
1284 #ifdef CONFIG_OF_GPIO
1285 /* If the gpiochip has an assigned OF node this takes precedence */
1287 gdev->dev.of_node = chip->of_node;
1289 chip->of_node = gdev->dev.of_node;
1292 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1297 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1298 device_initialize(&gdev->dev);
1299 dev_set_drvdata(&gdev->dev, gdev);
1300 if (chip->parent && chip->parent->driver)
1301 gdev->owner = chip->parent->driver->owner;
1302 else if (chip->owner)
1303 /* TODO: remove chip->owner */
1304 gdev->owner = chip->owner;
1306 gdev->owner = THIS_MODULE;
1308 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
1314 if (chip->ngpio == 0) {
1315 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
1317 goto err_free_descs;
1320 if (chip->ngpio > FASTPATH_NGPIO)
1321 chip_warn(chip, "line cnt %u is greater than fast path cnt %u\n",
1322 chip->ngpio, FASTPATH_NGPIO);
1324 gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);
1327 goto err_free_descs;
1330 gdev->ngpio = chip->ngpio;
1333 spin_lock_irqsave(&gpio_lock, flags);
1336 * TODO: this allocates a Linux GPIO number base in the global
1337 * GPIO numberspace for this chip. In the long run we want to
1338 * get *rid* of this numberspace and use only descriptors, but
1339 * it may be a pipe dream. It will not happen before we get rid
1340 * of the sysfs interface anyways.
1343 base = gpiochip_find_base(chip->ngpio);
1346 spin_unlock_irqrestore(&gpio_lock, flags);
1347 goto err_free_label;
1350 * TODO: it should not be necessary to reflect the assigned
1351 * base outside of the GPIO subsystem. Go over drivers and
1352 * see if anyone makes use of this, else drop this and assign
1359 ret = gpiodev_add_to_list(gdev);
1361 spin_unlock_irqrestore(&gpio_lock, flags);
1362 goto err_free_label;
1365 spin_unlock_irqrestore(&gpio_lock, flags);
1367 for (i = 0; i < chip->ngpio; i++)
1368 gdev->descs[i].gdev = gdev;
1370 #ifdef CONFIG_PINCTRL
1371 INIT_LIST_HEAD(&gdev->pin_ranges);
1374 ret = gpiochip_set_desc_names(chip);
1376 goto err_remove_from_list;
1378 ret = gpiochip_alloc_valid_mask(chip);
1380 goto err_remove_from_list;
1382 ret = of_gpiochip_add(chip);
1384 goto err_free_gpiochip_mask;
1386 ret = gpiochip_init_valid_mask(chip);
1388 goto err_remove_of_chip;
1390 for (i = 0; i < chip->ngpio; i++) {
1391 struct gpio_desc *desc = &gdev->descs[i];
1393 if (chip->get_direction && gpiochip_line_is_valid(chip, i)) {
1394 if (!chip->get_direction(chip, i))
1395 set_bit(FLAG_IS_OUT, &desc->flags);
1397 clear_bit(FLAG_IS_OUT, &desc->flags);
1399 if (!chip->direction_input)
1400 set_bit(FLAG_IS_OUT, &desc->flags);
1402 clear_bit(FLAG_IS_OUT, &desc->flags);
1406 acpi_gpiochip_add(chip);
1408 machine_gpiochip_add(chip);
1410 ret = gpiochip_irqchip_init_hw(chip);
1412 goto err_remove_acpi_chip;
1414 ret = gpiochip_irqchip_init_valid_mask(chip);
1416 goto err_remove_acpi_chip;
1418 ret = gpiochip_add_irqchip(chip, lock_key, request_key);
1420 goto err_remove_irqchip_mask;
1423 * By first adding the chardev, and then adding the device,
1424 * we get a device node entry in sysfs under
1425 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1426 * coldplug of device nodes and other udev business.
1427 * We can do this only if gpiolib has been initialized.
1428 * Otherwise, defer until later.
1430 if (gpiolib_initialized) {
1431 ret = gpiochip_setup_dev(gdev);
1433 goto err_remove_irqchip;
1438 gpiochip_irqchip_remove(chip);
1439 err_remove_irqchip_mask:
1440 gpiochip_irqchip_free_valid_mask(chip);
1441 err_remove_acpi_chip:
1442 acpi_gpiochip_remove(chip);
1444 gpiochip_free_hogs(chip);
1445 of_gpiochip_remove(chip);
1446 err_free_gpiochip_mask:
1447 gpiochip_free_valid_mask(chip);
1448 err_remove_from_list:
1449 spin_lock_irqsave(&gpio_lock, flags);
1450 list_del(&gdev->list);
1451 spin_unlock_irqrestore(&gpio_lock, flags);
1453 kfree_const(gdev->label);
1457 ida_simple_remove(&gpio_ida, gdev->id);
1459 /* failures here can mean systems won't boot... */
1460 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
1461 gdev->base, gdev->base + gdev->ngpio - 1,
1462 chip->label ? : "generic", ret);
1466 EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
1469 * gpiochip_get_data() - get per-subdriver data for the chip
1473 * The per-subdriver data for the chip.
1475 void *gpiochip_get_data(struct gpio_chip *chip)
1477 return chip->gpiodev->data;
1479 EXPORT_SYMBOL_GPL(gpiochip_get_data);
1482 * gpiochip_remove() - unregister a gpio_chip
1483 * @chip: the chip to unregister
1485 * A gpio_chip with any GPIOs still requested may not be removed.
1487 void gpiochip_remove(struct gpio_chip *chip)
1489 struct gpio_device *gdev = chip->gpiodev;
1490 struct gpio_desc *desc;
1491 unsigned long flags;
1493 bool requested = false;
1495 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
1496 gpiochip_sysfs_unregister(gdev);
1497 gpiochip_free_hogs(chip);
1498 /* Numb the device, cancelling all outstanding operations */
1500 gpiochip_irqchip_remove(chip);
1501 acpi_gpiochip_remove(chip);
1502 gpiochip_remove_pin_ranges(chip);
1503 of_gpiochip_remove(chip);
1504 gpiochip_free_valid_mask(chip);
1506 * We accept no more calls into the driver from this point, so
1507 * NULL the driver data pointer
1511 spin_lock_irqsave(&gpio_lock, flags);
1512 for (i = 0; i < gdev->ngpio; i++) {
1513 desc = &gdev->descs[i];
1514 if (test_bit(FLAG_REQUESTED, &desc->flags))
1517 spin_unlock_irqrestore(&gpio_lock, flags);
1520 dev_crit(&gdev->dev,
1521 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
1524 * The gpiochip side puts its use of the device to rest here:
1525 * if there are no userspace clients, the chardev and device will
1526 * be removed, else it will be dangling until the last user is
1529 cdev_device_del(&gdev->chrdev, &gdev->dev);
1530 put_device(&gdev->dev);
1532 EXPORT_SYMBOL_GPL(gpiochip_remove);
1534 static void devm_gpio_chip_release(struct device *dev, void *res)
1536 struct gpio_chip *chip = *(struct gpio_chip **)res;
1538 gpiochip_remove(chip);
1542 * devm_gpiochip_add_data() - Resource manager gpiochip_add_data()
1543 * @dev: pointer to the device that gpio_chip belongs to.
1544 * @chip: the chip to register, with chip->base initialized
1545 * @data: driver-private data associated with this chip
1547 * Context: potentially before irqs will work
1549 * The gpio chip automatically be released when the device is unbound.
1552 * A negative errno if the chip can't be registered, such as because the
1553 * chip->base is invalid or already associated with a different chip.
1554 * Otherwise it returns zero as a success code.
1556 int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1559 struct gpio_chip **ptr;
1562 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1567 ret = gpiochip_add_data(chip, data);
1574 devres_add(dev, ptr);
1578 EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1581 * gpiochip_find() - iterator for locating a specific gpio_chip
1582 * @data: data to pass to match function
1583 * @match: Callback function to check gpio_chip
1585 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1586 * determined by a user supplied @match callback. The callback should return
1587 * 0 if the device doesn't match and non-zero if it does. If the callback is
1588 * non-zero, this function will return to the caller and not iterate over any
1591 struct gpio_chip *gpiochip_find(void *data,
1592 int (*match)(struct gpio_chip *chip,
1595 struct gpio_device *gdev;
1596 struct gpio_chip *chip = NULL;
1597 unsigned long flags;
1599 spin_lock_irqsave(&gpio_lock, flags);
1600 list_for_each_entry(gdev, &gpio_devices, list)
1601 if (gdev->chip && match(gdev->chip, data)) {
1606 spin_unlock_irqrestore(&gpio_lock, flags);
1610 EXPORT_SYMBOL_GPL(gpiochip_find);
1612 static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1614 const char *name = data;
1616 return !strcmp(chip->label, name);
1619 static struct gpio_chip *find_chip_by_name(const char *name)
1621 return gpiochip_find((void *)name, gpiochip_match_name);
1624 #ifdef CONFIG_GPIOLIB_IRQCHIP
1627 * The following is irqchip helper code for gpiochips.
1630 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1632 struct gpio_irq_chip *girq = &gc->irq;
1637 return girq->init_hw(gc);
1640 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
1642 struct gpio_irq_chip *girq = &gc->irq;
1644 if (!girq->init_valid_mask)
1647 girq->valid_mask = gpiochip_allocate_mask(gc);
1648 if (!girq->valid_mask)
1651 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
1656 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1658 bitmap_free(gpiochip->irq.valid_mask);
1659 gpiochip->irq.valid_mask = NULL;
1662 bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1663 unsigned int offset)
1665 if (!gpiochip_line_is_valid(gpiochip, offset))
1667 /* No mask means all valid */
1668 if (likely(!gpiochip->irq.valid_mask))
1670 return test_bit(offset, gpiochip->irq.valid_mask);
1672 EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
1675 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
1676 * @gc: the gpiochip to set the irqchip chain to
1677 * @parent_irq: the irq number corresponding to the parent IRQ for this
1679 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1680 * coming out of the gpiochip. If the interrupt is nested rather than
1681 * cascaded, pass NULL in this handler argument
1683 static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gc,
1684 unsigned int parent_irq,
1685 irq_flow_handler_t parent_handler)
1687 struct gpio_irq_chip *girq = &gc->irq;
1688 struct device *dev = &gc->gpiodev->dev;
1690 if (!girq->domain) {
1691 chip_err(gc, "called %s before setting up irqchip\n",
1696 if (parent_handler) {
1697 if (gc->can_sleep) {
1699 "you cannot have chained interrupts on a chip that may sleep\n");
1702 girq->parents = devm_kcalloc(dev, 1,
1703 sizeof(*girq->parents),
1705 if (!girq->parents) {
1706 chip_err(gc, "out of memory allocating parent IRQ\n");
1709 girq->parents[0] = parent_irq;
1710 girq->num_parents = 1;
1712 * The parent irqchip is already using the chip_data for this
1713 * irqchip, so our callbacks simply use the handler_data.
1715 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1721 * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
1722 * @gpiochip: the gpiochip to set the irqchip chain to
1723 * @irqchip: the irqchip to chain to the gpiochip
1724 * @parent_irq: the irq number corresponding to the parent IRQ for this
1726 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1727 * coming out of the gpiochip.
1729 void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1730 struct irq_chip *irqchip,
1731 unsigned int parent_irq,
1732 irq_flow_handler_t parent_handler)
1734 if (gpiochip->irq.threaded) {
1735 chip_err(gpiochip, "tried to chain a threaded gpiochip\n");
1739 gpiochip_set_cascaded_irqchip(gpiochip, parent_irq, parent_handler);
1741 EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1744 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1745 * @gpiochip: the gpiochip to set the irqchip nested handler to
1746 * @irqchip: the irqchip to nest to the gpiochip
1747 * @parent_irq: the irq number corresponding to the parent IRQ for this
1750 void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1751 struct irq_chip *irqchip,
1752 unsigned int parent_irq)
1754 gpiochip_set_cascaded_irqchip(gpiochip, parent_irq, NULL);
1756 EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1758 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1761 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
1763 * @gc: the gpiochip to set the irqchip hierarchical handler to
1764 * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
1765 * will then percolate up to the parent
1767 static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
1768 struct irq_chip *irqchip)
1770 /* DT will deal with mapping each IRQ as we go along */
1771 if (is_of_node(gc->irq.fwnode))
1775 * This is for legacy and boardfile "irqchip" fwnodes: allocate
1776 * irqs upfront instead of dynamically since we don't have the
1777 * dynamic type of allocation that hardware description languages
1778 * provide. Once all GPIO drivers using board files are gone from
1779 * the kernel we can delete this code, but for a transitional period
1780 * it is necessary to keep this around.
1782 if (is_fwnode_irqchip(gc->irq.fwnode)) {
1786 for (i = 0; i < gc->ngpio; i++) {
1787 struct irq_fwspec fwspec;
1788 unsigned int parent_hwirq;
1789 unsigned int parent_type;
1790 struct gpio_irq_chip *girq = &gc->irq;
1793 * We call the child to parent translation function
1794 * only to check if the child IRQ is valid or not.
1795 * Just pick the rising edge type here as that is what
1796 * we likely need to support.
1798 ret = girq->child_to_parent_hwirq(gc, i,
1799 IRQ_TYPE_EDGE_RISING,
1803 chip_err(gc, "skip set-up on hwirq %d\n",
1808 fwspec.fwnode = gc->irq.fwnode;
1809 /* This is the hwirq for the GPIO line side of things */
1810 fwspec.param[0] = girq->child_offset_to_irq(gc, i);
1811 /* Just pick something */
1812 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
1813 fwspec.param_count = 2;
1814 ret = __irq_domain_alloc_irqs(gc->irq.domain,
1815 /* just pick something */
1824 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
1831 chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
1836 static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
1837 struct irq_fwspec *fwspec,
1838 unsigned long *hwirq,
1841 /* We support standard DT translation */
1842 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
1843 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
1846 /* This is for board files and others not using DT */
1847 if (is_fwnode_irqchip(fwspec->fwnode)) {
1850 ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
1853 WARN_ON(*type == IRQ_TYPE_NONE);
1859 static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
1861 unsigned int nr_irqs,
1864 struct gpio_chip *gc = d->host_data;
1865 irq_hw_number_t hwirq;
1866 unsigned int type = IRQ_TYPE_NONE;
1867 struct irq_fwspec *fwspec = data;
1868 struct irq_fwspec parent_fwspec;
1869 unsigned int parent_hwirq;
1870 unsigned int parent_type;
1871 struct gpio_irq_chip *girq = &gc->irq;
1875 * The nr_irqs parameter is always one except for PCI multi-MSI
1876 * so this should not happen.
1878 WARN_ON(nr_irqs != 1);
1880 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
1884 chip_info(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq);
1886 ret = girq->child_to_parent_hwirq(gc, hwirq, type,
1887 &parent_hwirq, &parent_type);
1889 chip_err(gc, "can't look up hwirq %lu\n", hwirq);
1892 chip_info(gc, "found parent hwirq %u\n", parent_hwirq);
1895 * We set handle_bad_irq because the .set_type() should
1896 * always be invoked and set the right type of handler.
1898 irq_domain_set_info(d,
1908 * Create a IRQ fwspec to send up to the parent irqdomain:
1909 * specify the hwirq we address on the parent and tie it
1910 * all together up the chain.
1912 parent_fwspec.fwnode = d->parent->fwnode;
1913 /* This parent only handles asserted level IRQs */
1914 girq->populate_parent_fwspec(gc, &parent_fwspec, parent_hwirq,
1916 chip_info(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
1918 ret = irq_domain_alloc_irqs_parent(d, irq, 1, &parent_fwspec);
1921 "failed to allocate parent hwirq %d for hwirq %lu\n",
1922 parent_hwirq, hwirq);
1927 static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *chip,
1928 unsigned int offset)
1933 static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
1935 ops->activate = gpiochip_irq_domain_activate;
1936 ops->deactivate = gpiochip_irq_domain_deactivate;
1937 ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
1938 ops->free = irq_domain_free_irqs_common;
1941 * We only allow overriding the translate() function for
1942 * hierarchical chips, and this should only be done if the user
1943 * really need something other than 1:1 translation.
1945 if (!ops->translate)
1946 ops->translate = gpiochip_hierarchy_irq_domain_translate;
1949 static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1951 if (!gc->irq.child_to_parent_hwirq ||
1953 chip_err(gc, "missing irqdomain vital data\n");
1957 if (!gc->irq.child_offset_to_irq)
1958 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
1960 if (!gc->irq.populate_parent_fwspec)
1961 gc->irq.populate_parent_fwspec =
1962 gpiochip_populate_parent_fwspec_twocell;
1964 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
1966 gc->irq.domain = irq_domain_create_hierarchy(
1967 gc->irq.parent_domain,
1971 &gc->irq.child_irq_domain_ops,
1974 if (!gc->irq.domain)
1977 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
1982 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1984 return !!gc->irq.parent_domain;
1987 void gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *chip,
1988 struct irq_fwspec *fwspec,
1989 unsigned int parent_hwirq,
1990 unsigned int parent_type)
1992 fwspec->param_count = 2;
1993 fwspec->param[0] = parent_hwirq;
1994 fwspec->param[1] = parent_type;
1996 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
1998 void gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *chip,
1999 struct irq_fwspec *fwspec,
2000 unsigned int parent_hwirq,
2001 unsigned int parent_type)
2003 fwspec->param_count = 4;
2004 fwspec->param[0] = 0;
2005 fwspec->param[1] = parent_hwirq;
2006 fwspec->param[2] = 0;
2007 fwspec->param[3] = parent_type;
2009 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
2013 static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
2018 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
2023 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
2026 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
2027 * @d: the irqdomain used by this irqchip
2028 * @irq: the global irq number used by this GPIO irqchip irq
2029 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
2031 * This function will set up the mapping for a certain IRQ line on a
2032 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
2033 * stored inside the gpiochip.
2035 int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
2036 irq_hw_number_t hwirq)
2038 struct gpio_chip *chip = d->host_data;
2041 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
2044 irq_set_chip_data(irq, chip);
2046 * This lock class tells lockdep that GPIO irqs are in a different
2047 * category than their parents, so it won't report false recursion.
2049 irq_set_lockdep_class(irq, chip->irq.lock_key, chip->irq.request_key);
2050 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
2051 /* Chips that use nested thread handlers have them marked */
2052 if (chip->irq.threaded)
2053 irq_set_nested_thread(irq, 1);
2054 irq_set_noprobe(irq);
2056 if (chip->irq.num_parents == 1)
2057 ret = irq_set_parent(irq, chip->irq.parents[0]);
2058 else if (chip->irq.map)
2059 ret = irq_set_parent(irq, chip->irq.map[hwirq]);
2065 * No set-up of the hardware will happen if IRQ_TYPE_NONE
2066 * is passed as default type.
2068 if (chip->irq.default_type != IRQ_TYPE_NONE)
2069 irq_set_irq_type(irq, chip->irq.default_type);
2073 EXPORT_SYMBOL_GPL(gpiochip_irq_map);
2075 void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
2077 struct gpio_chip *chip = d->host_data;
2079 if (chip->irq.threaded)
2080 irq_set_nested_thread(irq, 0);
2081 irq_set_chip_and_handler(irq, NULL, NULL);
2082 irq_set_chip_data(irq, NULL);
2084 EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
2086 static const struct irq_domain_ops gpiochip_domain_ops = {
2087 .map = gpiochip_irq_map,
2088 .unmap = gpiochip_irq_unmap,
2089 /* Virtually all GPIO irqchips are twocell:ed */
2090 .xlate = irq_domain_xlate_twocell,
2094 * TODO: move these activate/deactivate in under the hierarchicial
2095 * irqchip implementation as static once SPMI and SSBI (all external
2096 * users) are phased over.
2099 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
2100 * @domain: The IRQ domain used by this IRQ chip
2101 * @data: Outermost irq_data associated with the IRQ
2102 * @reserve: If set, only reserve an interrupt vector instead of assigning one
2104 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
2105 * used as the activate function for the &struct irq_domain_ops. The host_data
2106 * for the IRQ domain must be the &struct gpio_chip.
2108 int gpiochip_irq_domain_activate(struct irq_domain *domain,
2109 struct irq_data *data, bool reserve)
2111 struct gpio_chip *chip = domain->host_data;
2113 return gpiochip_lock_as_irq(chip, data->hwirq);
2115 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate);
2118 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
2119 * @domain: The IRQ domain used by this IRQ chip
2120 * @data: Outermost irq_data associated with the IRQ
2122 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
2123 * be used as the deactivate function for the &struct irq_domain_ops. The
2124 * host_data for the IRQ domain must be the &struct gpio_chip.
2126 void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
2127 struct irq_data *data)
2129 struct gpio_chip *chip = domain->host_data;
2131 return gpiochip_unlock_as_irq(chip, data->hwirq);
2133 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate);
2135 static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
2137 struct irq_domain *domain = chip->irq.domain;
2139 if (!gpiochip_irqchip_irq_valid(chip, offset))
2142 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2143 if (irq_domain_is_hierarchy(domain)) {
2144 struct irq_fwspec spec;
2146 spec.fwnode = domain->fwnode;
2147 spec.param_count = 2;
2148 spec.param[0] = chip->irq.child_offset_to_irq(chip, offset);
2149 spec.param[1] = IRQ_TYPE_NONE;
2151 return irq_create_fwspec_mapping(&spec);
2155 return irq_create_mapping(domain, offset);
2158 static int gpiochip_irq_reqres(struct irq_data *d)
2160 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2162 return gpiochip_reqres_irq(chip, d->hwirq);
2165 static void gpiochip_irq_relres(struct irq_data *d)
2167 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2169 gpiochip_relres_irq(chip, d->hwirq);
2172 static void gpiochip_irq_enable(struct irq_data *d)
2174 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2176 gpiochip_enable_irq(chip, d->hwirq);
2177 if (chip->irq.irq_enable)
2178 chip->irq.irq_enable(d);
2180 chip->irq.chip->irq_unmask(d);
2183 static void gpiochip_irq_disable(struct irq_data *d)
2185 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2187 if (chip->irq.irq_disable)
2188 chip->irq.irq_disable(d);
2190 chip->irq.chip->irq_mask(d);
2191 gpiochip_disable_irq(chip, d->hwirq);
2194 static void gpiochip_set_irq_hooks(struct gpio_chip *gpiochip)
2196 struct irq_chip *irqchip = gpiochip->irq.chip;
2198 if (!irqchip->irq_request_resources &&
2199 !irqchip->irq_release_resources) {
2200 irqchip->irq_request_resources = gpiochip_irq_reqres;
2201 irqchip->irq_release_resources = gpiochip_irq_relres;
2203 if (WARN_ON(gpiochip->irq.irq_enable))
2205 /* Check if the irqchip already has this hook... */
2206 if (irqchip->irq_enable == gpiochip_irq_enable) {
2208 * ...and if so, give a gentle warning that this is bad
2212 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
2215 gpiochip->irq.irq_enable = irqchip->irq_enable;
2216 gpiochip->irq.irq_disable = irqchip->irq_disable;
2217 irqchip->irq_enable = gpiochip_irq_enable;
2218 irqchip->irq_disable = gpiochip_irq_disable;
2222 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
2223 * @gpiochip: the GPIO chip to add the IRQ chip to
2224 * @lock_key: lockdep class for IRQ lock
2225 * @request_key: lockdep class for IRQ request
2227 static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
2228 struct lock_class_key *lock_key,
2229 struct lock_class_key *request_key)
2231 struct irq_chip *irqchip = gpiochip->irq.chip;
2232 const struct irq_domain_ops *ops = NULL;
2233 struct device_node *np;
2240 if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
2241 chip_err(gpiochip, "you cannot have chained interrupts on a chip that may sleep\n");
2245 np = gpiochip->gpiodev->dev.of_node;
2246 type = gpiochip->irq.default_type;
2249 * Specifying a default trigger is a terrible idea if DT or ACPI is
2250 * used to configure the interrupts, as you may end up with
2251 * conflicting triggers. Tell the user, and reset to NONE.
2253 if (WARN(np && type != IRQ_TYPE_NONE,
2254 "%s: Ignoring %u default trigger\n", np->full_name, type))
2255 type = IRQ_TYPE_NONE;
2257 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
2258 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
2259 "Ignoring %u default trigger\n", type);
2260 type = IRQ_TYPE_NONE;
2263 gpiochip->to_irq = gpiochip_to_irq;
2264 gpiochip->irq.default_type = type;
2265 gpiochip->irq.lock_key = lock_key;
2266 gpiochip->irq.request_key = request_key;
2268 /* If a parent irqdomain is provided, let's build a hierarchy */
2269 if (gpiochip_hierarchy_is_hierarchical(gpiochip)) {
2270 int ret = gpiochip_hierarchy_add_domain(gpiochip);
2274 /* Some drivers provide custom irqdomain ops */
2275 if (gpiochip->irq.domain_ops)
2276 ops = gpiochip->irq.domain_ops;
2279 ops = &gpiochip_domain_ops;
2280 gpiochip->irq.domain = irq_domain_add_simple(np,
2282 gpiochip->irq.first,
2284 if (!gpiochip->irq.domain)
2288 if (gpiochip->irq.parent_handler) {
2289 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
2291 for (i = 0; i < gpiochip->irq.num_parents; i++) {
2293 * The parent IRQ chip is already using the chip_data
2294 * for this IRQ chip, so our callbacks simply use the
2297 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
2298 gpiochip->irq.parent_handler,
2303 gpiochip_set_irq_hooks(gpiochip);
2305 acpi_gpiochip_request_interrupts(gpiochip);
2311 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
2312 * @gpiochip: the gpiochip to remove the irqchip from
2314 * This is called only from gpiochip_remove()
2316 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
2318 struct irq_chip *irqchip = gpiochip->irq.chip;
2319 unsigned int offset;
2321 acpi_gpiochip_free_interrupts(gpiochip);
2323 if (irqchip && gpiochip->irq.parent_handler) {
2324 struct gpio_irq_chip *irq = &gpiochip->irq;
2327 for (i = 0; i < irq->num_parents; i++)
2328 irq_set_chained_handler_and_data(irq->parents[i],
2332 /* Remove all IRQ mappings and delete the domain */
2333 if (gpiochip->irq.domain) {
2336 for (offset = 0; offset < gpiochip->ngpio; offset++) {
2337 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
2340 irq = irq_find_mapping(gpiochip->irq.domain, offset);
2341 irq_dispose_mapping(irq);
2344 irq_domain_remove(gpiochip->irq.domain);
2348 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
2349 irqchip->irq_request_resources = NULL;
2350 irqchip->irq_release_resources = NULL;
2352 if (irqchip->irq_enable == gpiochip_irq_enable) {
2353 irqchip->irq_enable = gpiochip->irq.irq_enable;
2354 irqchip->irq_disable = gpiochip->irq.irq_disable;
2357 gpiochip->irq.irq_enable = NULL;
2358 gpiochip->irq.irq_disable = NULL;
2359 gpiochip->irq.chip = NULL;
2361 gpiochip_irqchip_free_valid_mask(gpiochip);
2365 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
2366 * @gpiochip: the gpiochip to add the irqchip to
2367 * @irqchip: the irqchip to add to the gpiochip
2368 * @first_irq: if not dynamically assigned, the base (first) IRQ to
2369 * allocate gpiochip irqs from
2370 * @handler: the irq handler to use (often a predefined irq core function)
2371 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
2372 * to have the core avoid setting up any default type in the hardware.
2373 * @threaded: whether this irqchip uses a nested thread handler
2374 * @lock_key: lockdep class for IRQ lock
2375 * @request_key: lockdep class for IRQ request
2377 * This function closely associates a certain irqchip with a certain
2378 * gpiochip, providing an irq domain to translate the local IRQs to
2379 * global irqs in the gpiolib core, and making sure that the gpiochip
2380 * is passed as chip data to all related functions. Driver callbacks
2381 * need to use gpiochip_get_data() to get their local state containers back
2382 * from the gpiochip passed as chip data. An irqdomain will be stored
2383 * in the gpiochip that shall be used by the driver to handle IRQ number
2384 * translation. The gpiochip will need to be initialized and registered
2385 * before calling this function.
2387 * This function will handle two cell:ed simple IRQs and assumes all
2388 * the pins on the gpiochip can generate a unique IRQ. Everything else
2389 * need to be open coded.
2391 int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
2392 struct irq_chip *irqchip,
2393 unsigned int first_irq,
2394 irq_flow_handler_t handler,
2397 struct lock_class_key *lock_key,
2398 struct lock_class_key *request_key)
2400 struct device_node *of_node;
2402 if (!gpiochip || !irqchip)
2405 if (!gpiochip->parent) {
2406 pr_err("missing gpiochip .dev parent pointer\n");
2409 gpiochip->irq.threaded = threaded;
2410 of_node = gpiochip->parent->of_node;
2411 #ifdef CONFIG_OF_GPIO
2413 * If the gpiochip has an assigned OF node this takes precedence
2414 * FIXME: get rid of this and use gpiochip->parent->of_node
2417 if (gpiochip->of_node)
2418 of_node = gpiochip->of_node;
2421 * Specifying a default trigger is a terrible idea if DT or ACPI is
2422 * used to configure the interrupts, as you may end-up with
2423 * conflicting triggers. Tell the user, and reset to NONE.
2425 if (WARN(of_node && type != IRQ_TYPE_NONE,
2426 "%pOF: Ignoring %d default trigger\n", of_node, type))
2427 type = IRQ_TYPE_NONE;
2428 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
2429 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
2430 "Ignoring %d default trigger\n", type);
2431 type = IRQ_TYPE_NONE;
2434 gpiochip->irq.chip = irqchip;
2435 gpiochip->irq.handler = handler;
2436 gpiochip->irq.default_type = type;
2437 gpiochip->to_irq = gpiochip_to_irq;
2438 gpiochip->irq.lock_key = lock_key;
2439 gpiochip->irq.request_key = request_key;
2440 gpiochip->irq.domain = irq_domain_add_simple(of_node,
2441 gpiochip->ngpio, first_irq,
2442 &gpiochip_domain_ops, gpiochip);
2443 if (!gpiochip->irq.domain) {
2444 gpiochip->irq.chip = NULL;
2448 gpiochip_set_irq_hooks(gpiochip);
2450 acpi_gpiochip_request_interrupts(gpiochip);
2454 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
2456 #else /* CONFIG_GPIOLIB_IRQCHIP */
2458 static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
2459 struct lock_class_key *lock_key,
2460 struct lock_class_key *request_key)
2464 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
2466 static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gpiochip)
2471 static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
2475 static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
2478 #endif /* CONFIG_GPIOLIB_IRQCHIP */
2481 * gpiochip_generic_request() - request the gpio function for a pin
2482 * @chip: the gpiochip owning the GPIO
2483 * @offset: the offset of the GPIO to request for GPIO function
2485 int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
2487 return pinctrl_gpio_request(chip->gpiodev->base + offset);
2489 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
2492 * gpiochip_generic_free() - free the gpio function from a pin
2493 * @chip: the gpiochip to request the gpio function for
2494 * @offset: the offset of the GPIO to free from GPIO function
2496 void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
2498 pinctrl_gpio_free(chip->gpiodev->base + offset);
2500 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
2503 * gpiochip_generic_config() - apply configuration for a pin
2504 * @chip: the gpiochip owning the GPIO
2505 * @offset: the offset of the GPIO to apply the configuration
2506 * @config: the configuration to be applied
2508 int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
2509 unsigned long config)
2511 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
2513 EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2515 #ifdef CONFIG_PINCTRL
2518 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2519 * @chip: the gpiochip to add the range for
2520 * @pctldev: the pin controller to map to
2521 * @gpio_offset: the start offset in the current gpio_chip number space
2522 * @pin_group: name of the pin group inside the pin controller
2524 * Calling this function directly from a DeviceTree-supported
2525 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2526 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2527 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
2529 int gpiochip_add_pingroup_range(struct gpio_chip *chip,
2530 struct pinctrl_dev *pctldev,
2531 unsigned int gpio_offset, const char *pin_group)
2533 struct gpio_pin_range *pin_range;
2534 struct gpio_device *gdev = chip->gpiodev;
2537 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2539 chip_err(chip, "failed to allocate pin ranges\n");
2543 /* Use local offset as range ID */
2544 pin_range->range.id = gpio_offset;
2545 pin_range->range.gc = chip;
2546 pin_range->range.name = chip->label;
2547 pin_range->range.base = gdev->base + gpio_offset;
2548 pin_range->pctldev = pctldev;
2550 ret = pinctrl_get_group_pins(pctldev, pin_group,
2551 &pin_range->range.pins,
2552 &pin_range->range.npins);
2558 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2560 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2561 gpio_offset, gpio_offset + pin_range->range.npins - 1,
2562 pinctrl_dev_get_devname(pctldev), pin_group);
2564 list_add_tail(&pin_range->node, &gdev->pin_ranges);
2568 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2571 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2572 * @chip: the gpiochip to add the range for
2573 * @pinctl_name: the dev_name() of the pin controller to map to
2574 * @gpio_offset: the start offset in the current gpio_chip number space
2575 * @pin_offset: the start offset in the pin controller number space
2576 * @npins: the number of pins from the offset of each pin space (GPIO and
2577 * pin controller) to accumulate in this range
2580 * 0 on success, or a negative error-code on failure.
2582 * Calling this function directly from a DeviceTree-supported
2583 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2584 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2585 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
2587 int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
2588 unsigned int gpio_offset, unsigned int pin_offset,
2591 struct gpio_pin_range *pin_range;
2592 struct gpio_device *gdev = chip->gpiodev;
2595 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2597 chip_err(chip, "failed to allocate pin ranges\n");
2601 /* Use local offset as range ID */
2602 pin_range->range.id = gpio_offset;
2603 pin_range->range.gc = chip;
2604 pin_range->range.name = chip->label;
2605 pin_range->range.base = gdev->base + gpio_offset;
2606 pin_range->range.pin_base = pin_offset;
2607 pin_range->range.npins = npins;
2608 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
2610 if (IS_ERR(pin_range->pctldev)) {
2611 ret = PTR_ERR(pin_range->pctldev);
2612 chip_err(chip, "could not create pin range\n");
2616 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2617 gpio_offset, gpio_offset + npins - 1,
2619 pin_offset, pin_offset + npins - 1);
2621 list_add_tail(&pin_range->node, &gdev->pin_ranges);
2625 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
2628 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2629 * @chip: the chip to remove all the mappings for
2631 void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
2633 struct gpio_pin_range *pin_range, *tmp;
2634 struct gpio_device *gdev = chip->gpiodev;
2636 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
2637 list_del(&pin_range->node);
2638 pinctrl_remove_gpio_range(pin_range->pctldev,
2643 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2645 #endif /* CONFIG_PINCTRL */
2647 /* These "optional" allocation calls help prevent drivers from stomping
2648 * on each other, and help provide better diagnostics in debugfs.
2649 * They're called even less than the "set direction" calls.
2651 static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
2653 struct gpio_chip *chip = desc->gdev->chip;
2655 unsigned long flags;
2659 label = kstrdup_const(label, GFP_KERNEL);
2664 spin_lock_irqsave(&gpio_lock, flags);
2666 /* NOTE: gpio_request() can be called in early boot,
2667 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
2670 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2671 desc_set_label(desc, label ? : "?");
2679 if (chip->request) {
2680 /* chip->request may sleep */
2681 spin_unlock_irqrestore(&gpio_lock, flags);
2682 offset = gpio_chip_hwgpio(desc);
2683 if (gpiochip_line_is_valid(chip, offset))
2684 ret = chip->request(chip, offset);
2687 spin_lock_irqsave(&gpio_lock, flags);
2690 desc_set_label(desc, NULL);
2692 clear_bit(FLAG_REQUESTED, &desc->flags);
2696 if (chip->get_direction) {
2697 /* chip->get_direction may sleep */
2698 spin_unlock_irqrestore(&gpio_lock, flags);
2699 gpiod_get_direction(desc);
2700 spin_lock_irqsave(&gpio_lock, flags);
2703 spin_unlock_irqrestore(&gpio_lock, flags);
2708 * This descriptor validation needs to be inserted verbatim into each
2709 * function taking a descriptor, so we need to use a preprocessor
2710 * macro to avoid endless duplication. If the desc is NULL it is an
2711 * optional GPIO and calls should just bail out.
2713 static int validate_desc(const struct gpio_desc *desc, const char *func)
2718 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2719 return PTR_ERR(desc);
2722 pr_warn("%s: invalid GPIO (no device)\n", func);
2725 if (!desc->gdev->chip) {
2726 dev_warn(&desc->gdev->dev,
2727 "%s: backing chip is gone\n", func);
2733 #define VALIDATE_DESC(desc) do { \
2734 int __valid = validate_desc(desc, __func__); \
2739 #define VALIDATE_DESC_VOID(desc) do { \
2740 int __valid = validate_desc(desc, __func__); \
2745 int gpiod_request(struct gpio_desc *desc, const char *label)
2747 int ret = -EPROBE_DEFER;
2748 struct gpio_device *gdev;
2750 VALIDATE_DESC(desc);
2753 if (try_module_get(gdev->owner)) {
2754 ret = gpiod_request_commit(desc, label);
2756 module_put(gdev->owner);
2758 get_device(&gdev->dev);
2762 gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
2767 static bool gpiod_free_commit(struct gpio_desc *desc)
2770 unsigned long flags;
2771 struct gpio_chip *chip;
2775 gpiod_unexport(desc);
2777 spin_lock_irqsave(&gpio_lock, flags);
2779 chip = desc->gdev->chip;
2780 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2782 spin_unlock_irqrestore(&gpio_lock, flags);
2783 might_sleep_if(chip->can_sleep);
2784 chip->free(chip, gpio_chip_hwgpio(desc));
2785 spin_lock_irqsave(&gpio_lock, flags);
2787 kfree_const(desc->label);
2788 desc_set_label(desc, NULL);
2789 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
2790 clear_bit(FLAG_REQUESTED, &desc->flags);
2791 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
2792 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
2793 clear_bit(FLAG_IS_HOGGED, &desc->flags);
2797 spin_unlock_irqrestore(&gpio_lock, flags);
2801 void gpiod_free(struct gpio_desc *desc)
2803 if (desc && desc->gdev && gpiod_free_commit(desc)) {
2804 module_put(desc->gdev->owner);
2805 put_device(&desc->gdev->dev);
2807 WARN_ON(extra_checks);
2812 * gpiochip_is_requested - return string iff signal was requested
2813 * @chip: controller managing the signal
2814 * @offset: of signal within controller's 0..(ngpio - 1) range
2816 * Returns NULL if the GPIO is not currently requested, else a string.
2817 * The string returned is the label passed to gpio_request(); if none has been
2818 * passed it is a meaningless, non-NULL constant.
2820 * This function is for use by GPIO controller drivers. The label can
2821 * help with diagnostics, and knowing that the signal is used as a GPIO
2822 * can help avoid accidentally multiplexing it to another controller.
2824 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2826 struct gpio_desc *desc;
2828 if (offset >= chip->ngpio)
2831 desc = &chip->gpiodev->descs[offset];
2833 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
2837 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2840 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2842 * @hwnum: hardware number of the GPIO for which to request the descriptor
2843 * @label: label for the GPIO
2844 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2845 * specify things like line inversion semantics with the machine flags
2846 * such as GPIO_OUT_LOW
2847 * @dflags: descriptor request flags for this GPIO or 0 if default, this
2848 * can be used to specify consumer semantics such as open drain
2850 * Function allows GPIO chip drivers to request and use their own GPIO
2851 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2852 * function will not increase reference count of the GPIO chip module. This
2853 * allows the GPIO chip module to be unloaded as needed (we assume that the
2854 * GPIO chip driver handles freeing the GPIOs it has requested).
2857 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2860 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2862 enum gpio_lookup_flags lflags,
2863 enum gpiod_flags dflags)
2865 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2869 chip_err(chip, "failed to get GPIO descriptor\n");
2873 ret = gpiod_request_commit(desc, label);
2875 return ERR_PTR(ret);
2877 ret = gpiod_configure_flags(desc, label, lflags, dflags);
2879 chip_err(chip, "setup of own GPIO %s failed\n", label);
2880 gpiod_free_commit(desc);
2881 return ERR_PTR(ret);
2886 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
2889 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2890 * @desc: GPIO descriptor to free
2892 * Function frees the given GPIO requested previously with
2893 * gpiochip_request_own_desc().
2895 void gpiochip_free_own_desc(struct gpio_desc *desc)
2898 gpiod_free_commit(desc);
2900 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
2903 * Drivers MUST set GPIO direction before making get/set calls. In
2904 * some cases this is done in early boot, before IRQs are enabled.
2906 * As a rule these aren't called more than once (except for drivers
2907 * using the open-drain emulation idiom) so these are natural places
2908 * to accumulate extra debugging checks. Note that we can't (yet)
2909 * rely on gpio_request() having been called beforehand.
2912 static int gpio_set_config(struct gpio_chip *gc, unsigned offset,
2913 enum pin_config_param mode)
2915 unsigned long config;
2919 case PIN_CONFIG_BIAS_PULL_DOWN:
2920 case PIN_CONFIG_BIAS_PULL_UP:
2928 config = PIN_CONF_PACKED(mode, arg);
2929 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2933 * gpiod_direction_input - set the GPIO direction to input
2934 * @desc: GPIO to set to input
2936 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2937 * be called safely on it.
2939 * Return 0 in case of success, else an error code.
2941 int gpiod_direction_input(struct gpio_desc *desc)
2943 struct gpio_chip *chip;
2946 VALIDATE_DESC(desc);
2947 chip = desc->gdev->chip;
2950 * It is legal to have no .get() and .direction_input() specified if
2951 * the chip is output-only, but you can't specify .direction_input()
2952 * and not support the .get() operation, that doesn't make sense.
2954 if (!chip->get && chip->direction_input) {
2956 "%s: missing get() but have direction_input()\n",
2962 * If we have a .direction_input() callback, things are simple,
2963 * just call it. Else we are some input-only chip so try to check the
2964 * direction (if .get_direction() is supported) else we silently
2965 * assume we are in input mode after this.
2967 if (chip->direction_input) {
2968 ret = chip->direction_input(chip, gpio_chip_hwgpio(desc));
2969 } else if (chip->get_direction &&
2970 (chip->get_direction(chip, gpio_chip_hwgpio(desc)) != 1)) {
2972 "%s: missing direction_input() operation and line is output\n",
2977 clear_bit(FLAG_IS_OUT, &desc->flags);
2979 if (test_bit(FLAG_PULL_UP, &desc->flags))
2980 gpio_set_config(chip, gpio_chip_hwgpio(desc),
2981 PIN_CONFIG_BIAS_PULL_UP);
2982 else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2983 gpio_set_config(chip, gpio_chip_hwgpio(desc),
2984 PIN_CONFIG_BIAS_PULL_DOWN);
2986 trace_gpio_direction(desc_to_gpio(desc), 1, ret);
2990 EXPORT_SYMBOL_GPL(gpiod_direction_input);
2992 static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
2994 struct gpio_chip *gc = desc->gdev->chip;
2999 * It's OK not to specify .direction_output() if the gpiochip is
3000 * output-only, but if there is then not even a .set() operation it
3001 * is pretty tricky to drive the output line.
3003 if (!gc->set && !gc->direction_output) {
3005 "%s: missing set() and direction_output() operations\n",
3010 if (gc->direction_output) {
3011 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
3013 /* Check that we are in output mode if we can */
3014 if (gc->get_direction &&
3015 gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
3017 "%s: missing direction_output() operation\n",
3022 * If we can't actively set the direction, we are some
3023 * output-only chip, so just drive the output as desired.
3025 gc->set(gc, gpio_chip_hwgpio(desc), val);
3029 set_bit(FLAG_IS_OUT, &desc->flags);
3030 trace_gpio_value(desc_to_gpio(desc), 0, val);
3031 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
3036 * gpiod_direction_output_raw - set the GPIO direction to output
3037 * @desc: GPIO to set to output
3038 * @value: initial output value of the GPIO
3040 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
3041 * be called safely on it. The initial value of the output must be specified
3042 * as raw value on the physical line without regard for the ACTIVE_LOW status.
3044 * Return 0 in case of success, else an error code.
3046 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
3048 VALIDATE_DESC(desc);
3049 return gpiod_direction_output_raw_commit(desc, value);
3051 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
3054 * gpiod_direction_output - set the GPIO direction to output
3055 * @desc: GPIO to set to output
3056 * @value: initial output value of the GPIO
3058 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
3059 * be called safely on it. The initial value of the output must be specified
3060 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3063 * Return 0 in case of success, else an error code.
3065 int gpiod_direction_output(struct gpio_desc *desc, int value)
3067 struct gpio_chip *gc;
3070 VALIDATE_DESC(desc);
3071 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3076 /* GPIOs used for enabled IRQs shall not be set as output */
3077 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
3078 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
3080 "%s: tried to set a GPIO tied to an IRQ as output\n",
3085 gc = desc->gdev->chip;
3086 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
3087 /* First see if we can enable open drain in hardware */
3088 ret = gpio_set_config(gc, gpio_chip_hwgpio(desc),
3089 PIN_CONFIG_DRIVE_OPEN_DRAIN);
3091 goto set_output_value;
3092 /* Emulate open drain by not actively driving the line high */
3094 ret = gpiod_direction_input(desc);
3095 goto set_output_flag;
3098 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
3099 ret = gpio_set_config(gc, gpio_chip_hwgpio(desc),
3100 PIN_CONFIG_DRIVE_OPEN_SOURCE);
3102 goto set_output_value;
3103 /* Emulate open source by not actively driving the line low */
3105 ret = gpiod_direction_input(desc);
3106 goto set_output_flag;
3109 gpio_set_config(gc, gpio_chip_hwgpio(desc),
3110 PIN_CONFIG_DRIVE_PUSH_PULL);
3114 return gpiod_direction_output_raw_commit(desc, value);
3118 * When emulating open-source or open-drain functionalities by not
3119 * actively driving the line (setting mode to input) we still need to
3120 * set the IS_OUT flag or otherwise we won't be able to set the line
3124 set_bit(FLAG_IS_OUT, &desc->flags);
3127 EXPORT_SYMBOL_GPL(gpiod_direction_output);
3130 * gpiod_set_debounce - sets @debounce time for a GPIO
3131 * @desc: descriptor of the GPIO for which to set debounce time
3132 * @debounce: debounce time in microseconds
3135 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
3138 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
3140 struct gpio_chip *chip;
3141 unsigned long config;
3143 VALIDATE_DESC(desc);
3144 chip = desc->gdev->chip;
3145 if (!chip->set || !chip->set_config) {
3147 "%s: missing set() or set_config() operations\n",
3152 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
3153 return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
3155 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
3158 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
3159 * @desc: descriptor of the GPIO for which to configure persistence
3160 * @transitory: True to lose state on suspend or reset, false for persistence
3163 * 0 on success, otherwise a negative error code.
3165 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
3167 struct gpio_chip *chip;
3168 unsigned long packed;
3172 VALIDATE_DESC(desc);
3174 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
3175 * persistence state.
3178 set_bit(FLAG_TRANSITORY, &desc->flags);
3180 clear_bit(FLAG_TRANSITORY, &desc->flags);
3182 /* If the driver supports it, set the persistence state now */
3183 chip = desc->gdev->chip;
3184 if (!chip->set_config)
3187 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
3189 gpio = gpio_chip_hwgpio(desc);
3190 rc = chip->set_config(chip, gpio, packed);
3191 if (rc == -ENOTSUPP) {
3192 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
3199 EXPORT_SYMBOL_GPL(gpiod_set_transitory);
3202 * gpiod_is_active_low - test whether a GPIO is active-low or not
3203 * @desc: the gpio descriptor to test
3205 * Returns 1 if the GPIO is active-low, 0 otherwise.
3207 int gpiod_is_active_low(const struct gpio_desc *desc)
3209 VALIDATE_DESC(desc);
3210 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
3212 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
3214 /* I/O calls are only valid after configuration completed; the relevant
3215 * "is this a valid GPIO" error checks should already have been done.
3217 * "Get" operations are often inlinable as reading a pin value register,
3218 * and masking the relevant bit in that register.
3220 * When "set" operations are inlinable, they involve writing that mask to
3221 * one register to set a low value, or a different register to set it high.
3222 * Otherwise locking is needed, so there may be little value to inlining.
3224 *------------------------------------------------------------------------
3226 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
3227 * have requested the GPIO. That can include implicit requesting by
3228 * a direction setting call. Marking a gpio as requested locks its chip
3229 * in memory, guaranteeing that these table lookups need no more locking
3230 * and that gpiochip_remove() will fail.
3232 * REVISIT when debugging, consider adding some instrumentation to ensure
3233 * that the GPIO was actually requested.
3236 static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
3238 struct gpio_chip *chip;
3242 chip = desc->gdev->chip;
3243 offset = gpio_chip_hwgpio(desc);
3244 value = chip->get ? chip->get(chip, offset) : -EIO;
3245 value = value < 0 ? value : !!value;
3246 trace_gpio_value(desc_to_gpio(desc), 1, value);
3250 static int gpio_chip_get_multiple(struct gpio_chip *chip,
3251 unsigned long *mask, unsigned long *bits)
3253 if (chip->get_multiple) {
3254 return chip->get_multiple(chip, mask, bits);
3255 } else if (chip->get) {
3258 for_each_set_bit(i, mask, chip->ngpio) {
3259 value = chip->get(chip, i);
3262 __assign_bit(i, bits, value);
3269 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
3270 unsigned int array_size,
3271 struct gpio_desc **desc_array,
3272 struct gpio_array *array_info,
3273 unsigned long *value_bitmap)
3278 * Validate array_info against desc_array and its size.
3279 * It should immediately follow desc_array if both
3280 * have been obtained from the same gpiod_get_array() call.
3282 if (array_info && array_info->desc == desc_array &&
3283 array_size <= array_info->size &&
3284 (void *)array_info == desc_array + array_info->size) {
3286 WARN_ON(array_info->chip->can_sleep);
3288 ret = gpio_chip_get_multiple(array_info->chip,
3289 array_info->get_mask,
3294 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3295 bitmap_xor(value_bitmap, value_bitmap,
3296 array_info->invert_mask, array_size);
3298 if (bitmap_full(array_info->get_mask, array_size))
3301 i = find_first_zero_bit(array_info->get_mask, array_size);
3306 while (i < array_size) {
3307 struct gpio_chip *chip = desc_array[i]->gdev->chip;
3308 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
3309 unsigned long *mask, *bits;
3312 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
3315 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
3317 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
3322 bits = mask + BITS_TO_LONGS(chip->ngpio);
3323 bitmap_zero(mask, chip->ngpio);
3326 WARN_ON(chip->can_sleep);
3328 /* collect all inputs belonging to the same chip */
3331 const struct gpio_desc *desc = desc_array[i];
3332 int hwgpio = gpio_chip_hwgpio(desc);
3334 __set_bit(hwgpio, mask);
3338 i = find_next_zero_bit(array_info->get_mask,
3340 } while ((i < array_size) &&
3341 (desc_array[i]->gdev->chip == chip));
3343 ret = gpio_chip_get_multiple(chip, mask, bits);
3345 if (mask != fastpath)
3350 for (j = first; j < i; ) {
3351 const struct gpio_desc *desc = desc_array[j];
3352 int hwgpio = gpio_chip_hwgpio(desc);
3353 int value = test_bit(hwgpio, bits);
3355 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3357 __assign_bit(j, value_bitmap, value);
3358 trace_gpio_value(desc_to_gpio(desc), 1, value);
3362 j = find_next_zero_bit(array_info->get_mask, i,
3366 if (mask != fastpath)
3373 * gpiod_get_raw_value() - return a gpio's raw value
3374 * @desc: gpio whose value will be returned
3376 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
3377 * its ACTIVE_LOW status, or negative errno on failure.
3379 * This function can be called from contexts where we cannot sleep, and will
3380 * complain if the GPIO chip functions potentially sleep.
3382 int gpiod_get_raw_value(const struct gpio_desc *desc)
3384 VALIDATE_DESC(desc);
3385 /* Should be using gpiod_get_raw_value_cansleep() */
3386 WARN_ON(desc->gdev->chip->can_sleep);
3387 return gpiod_get_raw_value_commit(desc);
3389 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
3392 * gpiod_get_value() - return a gpio's value
3393 * @desc: gpio whose value will be returned
3395 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3396 * account, or negative errno on failure.
3398 * This function can be called from contexts where we cannot sleep, and will
3399 * complain if the GPIO chip functions potentially sleep.
3401 int gpiod_get_value(const struct gpio_desc *desc)
3405 VALIDATE_DESC(desc);
3406 /* Should be using gpiod_get_value_cansleep() */
3407 WARN_ON(desc->gdev->chip->can_sleep);
3409 value = gpiod_get_raw_value_commit(desc);
3413 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3418 EXPORT_SYMBOL_GPL(gpiod_get_value);
3421 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
3422 * @array_size: number of elements in the descriptor array / value bitmap
3423 * @desc_array: array of GPIO descriptors whose values will be read
3424 * @array_info: information on applicability of fast bitmap processing path
3425 * @value_bitmap: bitmap to store the read values
3427 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3428 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3429 * else an error code.
3431 * This function can be called from contexts where we cannot sleep,
3432 * and it will complain if the GPIO chip functions potentially sleep.
3434 int gpiod_get_raw_array_value(unsigned int array_size,
3435 struct gpio_desc **desc_array,
3436 struct gpio_array *array_info,
3437 unsigned long *value_bitmap)
3441 return gpiod_get_array_value_complex(true, false, array_size,
3442 desc_array, array_info,
3445 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
3448 * gpiod_get_array_value() - read values from an array of GPIOs
3449 * @array_size: number of elements in the descriptor array / value bitmap
3450 * @desc_array: array of GPIO descriptors whose values will be read
3451 * @array_info: information on applicability of fast bitmap processing path
3452 * @value_bitmap: bitmap to store the read values
3454 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3455 * into account. Return 0 in case of success, else an error code.
3457 * This function can be called from contexts where we cannot sleep,
3458 * and it will complain if the GPIO chip functions potentially sleep.
3460 int gpiod_get_array_value(unsigned int array_size,
3461 struct gpio_desc **desc_array,
3462 struct gpio_array *array_info,
3463 unsigned long *value_bitmap)
3467 return gpiod_get_array_value_complex(false, false, array_size,
3468 desc_array, array_info,
3471 EXPORT_SYMBOL_GPL(gpiod_get_array_value);
3474 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
3475 * @desc: gpio descriptor whose state need to be set.
3476 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3478 static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
3481 struct gpio_chip *chip = desc->gdev->chip;
3482 int offset = gpio_chip_hwgpio(desc);
3485 ret = chip->direction_input(chip, offset);
3487 ret = chip->direction_output(chip, offset, 0);
3489 set_bit(FLAG_IS_OUT, &desc->flags);
3491 trace_gpio_direction(desc_to_gpio(desc), value, ret);
3494 "%s: Error in set_value for open drain err %d\n",
3499 * _gpio_set_open_source_value() - Set the open source gpio's value.
3500 * @desc: gpio descriptor whose state need to be set.
3501 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3503 static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
3506 struct gpio_chip *chip = desc->gdev->chip;
3507 int offset = gpio_chip_hwgpio(desc);
3510 ret = chip->direction_output(chip, offset, 1);
3512 set_bit(FLAG_IS_OUT, &desc->flags);
3514 ret = chip->direction_input(chip, offset);
3516 trace_gpio_direction(desc_to_gpio(desc), !value, ret);
3519 "%s: Error in set_value for open source err %d\n",
3523 static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
3525 struct gpio_chip *chip;
3527 chip = desc->gdev->chip;
3528 trace_gpio_value(desc_to_gpio(desc), 0, value);
3529 chip->set(chip, gpio_chip_hwgpio(desc), value);
3533 * set multiple outputs on the same chip;
3534 * use the chip's set_multiple function if available;
3535 * otherwise set the outputs sequentially;
3536 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
3537 * defines which outputs are to be changed
3538 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
3539 * defines the values the outputs specified by mask are to be set to
3541 static void gpio_chip_set_multiple(struct gpio_chip *chip,
3542 unsigned long *mask, unsigned long *bits)
3544 if (chip->set_multiple) {
3545 chip->set_multiple(chip, mask, bits);
3549 /* set outputs if the corresponding mask bit is set */
3550 for_each_set_bit(i, mask, chip->ngpio)
3551 chip->set(chip, i, test_bit(i, bits));
3555 int gpiod_set_array_value_complex(bool raw, bool can_sleep,
3556 unsigned int array_size,
3557 struct gpio_desc **desc_array,
3558 struct gpio_array *array_info,
3559 unsigned long *value_bitmap)
3564 * Validate array_info against desc_array and its size.
3565 * It should immediately follow desc_array if both
3566 * have been obtained from the same gpiod_get_array() call.
3568 if (array_info && array_info->desc == desc_array &&
3569 array_size <= array_info->size &&
3570 (void *)array_info == desc_array + array_info->size) {
3572 WARN_ON(array_info->chip->can_sleep);
3574 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3575 bitmap_xor(value_bitmap, value_bitmap,
3576 array_info->invert_mask, array_size);
3578 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
3581 if (bitmap_full(array_info->set_mask, array_size))
3584 i = find_first_zero_bit(array_info->set_mask, array_size);
3589 while (i < array_size) {
3590 struct gpio_chip *chip = desc_array[i]->gdev->chip;
3591 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
3592 unsigned long *mask, *bits;
3595 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
3598 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
3600 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
3605 bits = mask + BITS_TO_LONGS(chip->ngpio);
3606 bitmap_zero(mask, chip->ngpio);
3609 WARN_ON(chip->can_sleep);
3612 struct gpio_desc *desc = desc_array[i];
3613 int hwgpio = gpio_chip_hwgpio(desc);
3614 int value = test_bit(i, value_bitmap);
3617 * Pins applicable for fast input but not for
3618 * fast output processing may have been already
3619 * inverted inside the fast path, skip them.
3621 if (!raw && !(array_info &&
3622 test_bit(i, array_info->invert_mask)) &&
3623 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3625 trace_gpio_value(desc_to_gpio(desc), 0, value);
3627 * collect all normal outputs belonging to the same chip
3628 * open drain and open source outputs are set individually
3630 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
3631 gpio_set_open_drain_value_commit(desc, value);
3632 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
3633 gpio_set_open_source_value_commit(desc, value);
3635 __set_bit(hwgpio, mask);
3637 __set_bit(hwgpio, bits);
3639 __clear_bit(hwgpio, bits);
3645 i = find_next_zero_bit(array_info->set_mask,
3647 } while ((i < array_size) &&
3648 (desc_array[i]->gdev->chip == chip));
3649 /* push collected bits to outputs */
3651 gpio_chip_set_multiple(chip, mask, bits);
3653 if (mask != fastpath)
3660 * gpiod_set_raw_value() - assign a gpio's raw value
3661 * @desc: gpio whose value will be assigned
3662 * @value: value to assign
3664 * Set the raw value of the GPIO, i.e. the value of its physical line without
3665 * regard for its ACTIVE_LOW status.
3667 * This function can be called from contexts where we cannot sleep, and will
3668 * complain if the GPIO chip functions potentially sleep.
3670 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
3672 VALIDATE_DESC_VOID(desc);
3673 /* Should be using gpiod_set_raw_value_cansleep() */
3674 WARN_ON(desc->gdev->chip->can_sleep);
3675 gpiod_set_raw_value_commit(desc, value);
3677 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
3680 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3681 * @desc: the descriptor to set the value on
3682 * @value: value to set
3684 * This sets the value of a GPIO line backing a descriptor, applying
3685 * different semantic quirks like active low and open drain/source
3688 static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3690 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3692 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3693 gpio_set_open_drain_value_commit(desc, value);
3694 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3695 gpio_set_open_source_value_commit(desc, value);
3697 gpiod_set_raw_value_commit(desc, value);
3701 * gpiod_set_value() - assign a gpio's value
3702 * @desc: gpio whose value will be assigned
3703 * @value: value to assign
3705 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3706 * OPEN_DRAIN and OPEN_SOURCE flags into account.
3708 * This function can be called from contexts where we cannot sleep, and will
3709 * complain if the GPIO chip functions potentially sleep.
3711 void gpiod_set_value(struct gpio_desc *desc, int value)
3713 VALIDATE_DESC_VOID(desc);
3714 /* Should be using gpiod_set_value_cansleep() */
3715 WARN_ON(desc->gdev->chip->can_sleep);
3716 gpiod_set_value_nocheck(desc, value);
3718 EXPORT_SYMBOL_GPL(gpiod_set_value);
3721 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
3722 * @array_size: number of elements in the descriptor array / value bitmap
3723 * @desc_array: array of GPIO descriptors whose values will be assigned
3724 * @array_info: information on applicability of fast bitmap processing path
3725 * @value_bitmap: bitmap of values to assign
3727 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3728 * without regard for their ACTIVE_LOW status.
3730 * This function can be called from contexts where we cannot sleep, and will
3731 * complain if the GPIO chip functions potentially sleep.
3733 int gpiod_set_raw_array_value(unsigned int array_size,
3734 struct gpio_desc **desc_array,
3735 struct gpio_array *array_info,
3736 unsigned long *value_bitmap)
3740 return gpiod_set_array_value_complex(true, false, array_size,
3741 desc_array, array_info, value_bitmap);
3743 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
3746 * gpiod_set_array_value() - assign values to an array of GPIOs
3747 * @array_size: number of elements in the descriptor array / value bitmap
3748 * @desc_array: array of GPIO descriptors whose values will be assigned
3749 * @array_info: information on applicability of fast bitmap processing path
3750 * @value_bitmap: bitmap of values to assign
3752 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3755 * This function can be called from contexts where we cannot sleep, and will
3756 * complain if the GPIO chip functions potentially sleep.
3758 int gpiod_set_array_value(unsigned int array_size,
3759 struct gpio_desc **desc_array,
3760 struct gpio_array *array_info,
3761 unsigned long *value_bitmap)
3765 return gpiod_set_array_value_complex(false, false, array_size,
3766 desc_array, array_info,
3769 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
3772 * gpiod_cansleep() - report whether gpio value access may sleep
3773 * @desc: gpio to check
3776 int gpiod_cansleep(const struct gpio_desc *desc)
3778 VALIDATE_DESC(desc);
3779 return desc->gdev->chip->can_sleep;
3781 EXPORT_SYMBOL_GPL(gpiod_cansleep);
3784 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3785 * @desc: gpio to set the consumer name on
3786 * @name: the new consumer name
3788 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
3790 VALIDATE_DESC(desc);
3792 name = kstrdup_const(name, GFP_KERNEL);
3797 kfree_const(desc->label);
3798 desc_set_label(desc, name);
3802 EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3805 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3806 * @desc: gpio whose IRQ will be returned (already requested)
3808 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3811 int gpiod_to_irq(const struct gpio_desc *desc)
3813 struct gpio_chip *chip;
3817 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3818 * requires this function to not return zero on an invalid descriptor
3819 * but rather a negative error number.
3821 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
3824 chip = desc->gdev->chip;
3825 offset = gpio_chip_hwgpio(desc);
3827 int retirq = chip->to_irq(chip, offset);
3829 /* Zero means NO_IRQ */
3837 EXPORT_SYMBOL_GPL(gpiod_to_irq);
3840 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3841 * @chip: the chip the GPIO to lock belongs to
3842 * @offset: the offset of the GPIO to lock as IRQ
3844 * This is used directly by GPIO drivers that want to lock down
3845 * a certain GPIO line to be used for IRQs.
3847 int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
3849 struct gpio_desc *desc;
3851 desc = gpiochip_get_desc(chip, offset);
3853 return PTR_ERR(desc);
3856 * If it's fast: flush the direction setting if something changed
3859 if (!chip->can_sleep && chip->get_direction) {
3860 int dir = gpiod_get_direction(desc);
3863 chip_err(chip, "%s: cannot get GPIO direction\n",
3869 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
3871 "%s: tried to flag a GPIO set as output for IRQ\n",
3876 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
3877 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3880 * If the consumer has not set up a label (such as when the
3881 * IRQ is referenced from .to_irq()) we set up a label here
3882 * so it is clear this is used as an interrupt.
3885 desc_set_label(desc, "interrupt");
3889 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
3892 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3893 * @chip: the chip the GPIO to lock belongs to
3894 * @offset: the offset of the GPIO to lock as IRQ
3896 * This is used directly by GPIO drivers that want to indicate
3897 * that a certain GPIO is no longer used exclusively for IRQ.
3899 void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
3901 struct gpio_desc *desc;
3903 desc = gpiochip_get_desc(chip, offset);
3907 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3908 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3910 /* If we only had this marking, erase it */
3911 if (desc->label && !strcmp(desc->label, "interrupt"))
3912 desc_set_label(desc, NULL);
3914 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
3916 void gpiochip_disable_irq(struct gpio_chip *chip, unsigned int offset)
3918 struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
3920 if (!IS_ERR(desc) &&
3921 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
3922 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3924 EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
3926 void gpiochip_enable_irq(struct gpio_chip *chip, unsigned int offset)
3928 struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
3930 if (!IS_ERR(desc) &&
3931 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
3932 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags));
3933 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3936 EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
3938 bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
3940 if (offset >= chip->ngpio)
3943 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
3945 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3947 int gpiochip_reqres_irq(struct gpio_chip *chip, unsigned int offset)
3951 if (!try_module_get(chip->gpiodev->owner))
3954 ret = gpiochip_lock_as_irq(chip, offset);
3956 chip_err(chip, "unable to lock HW IRQ %u for IRQ\n", offset);
3957 module_put(chip->gpiodev->owner);
3962 EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
3964 void gpiochip_relres_irq(struct gpio_chip *chip, unsigned int offset)
3966 gpiochip_unlock_as_irq(chip, offset);
3967 module_put(chip->gpiodev->owner);
3969 EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
3971 bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
3973 if (offset >= chip->ngpio)
3976 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
3978 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3980 bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
3982 if (offset >= chip->ngpio)
3985 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
3987 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3989 bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
3991 if (offset >= chip->ngpio)
3994 return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
3996 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3999 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
4000 * @desc: gpio whose value will be returned
4002 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
4003 * its ACTIVE_LOW status, or negative errno on failure.
4005 * This function is to be called from contexts that can sleep.
4007 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
4009 might_sleep_if(extra_checks);
4010 VALIDATE_DESC(desc);
4011 return gpiod_get_raw_value_commit(desc);
4013 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
4016 * gpiod_get_value_cansleep() - return a gpio's value
4017 * @desc: gpio whose value will be returned
4019 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
4020 * account, or negative errno on failure.
4022 * This function is to be called from contexts that can sleep.
4024 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
4028 might_sleep_if(extra_checks);
4029 VALIDATE_DESC(desc);
4030 value = gpiod_get_raw_value_commit(desc);
4034 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
4039 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
4042 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
4043 * @array_size: number of elements in the descriptor array / value bitmap
4044 * @desc_array: array of GPIO descriptors whose values will be read
4045 * @array_info: information on applicability of fast bitmap processing path
4046 * @value_bitmap: bitmap to store the read values
4048 * Read the raw values of the GPIOs, i.e. the values of the physical lines
4049 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
4050 * else an error code.
4052 * This function is to be called from contexts that can sleep.
4054 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
4055 struct gpio_desc **desc_array,
4056 struct gpio_array *array_info,
4057 unsigned long *value_bitmap)
4059 might_sleep_if(extra_checks);
4062 return gpiod_get_array_value_complex(true, true, array_size,
4063 desc_array, array_info,
4066 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
4069 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
4070 * @array_size: number of elements in the descriptor array / value bitmap
4071 * @desc_array: array of GPIO descriptors whose values will be read
4072 * @array_info: information on applicability of fast bitmap processing path
4073 * @value_bitmap: bitmap to store the read values
4075 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
4076 * into account. Return 0 in case of success, else an error code.
4078 * This function is to be called from contexts that can sleep.
4080 int gpiod_get_array_value_cansleep(unsigned int array_size,
4081 struct gpio_desc **desc_array,
4082 struct gpio_array *array_info,
4083 unsigned long *value_bitmap)
4085 might_sleep_if(extra_checks);
4088 return gpiod_get_array_value_complex(false, true, array_size,
4089 desc_array, array_info,
4092 EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
4095 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
4096 * @desc: gpio whose value will be assigned
4097 * @value: value to assign
4099 * Set the raw value of the GPIO, i.e. the value of its physical line without
4100 * regard for its ACTIVE_LOW status.
4102 * This function is to be called from contexts that can sleep.
4104 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
4106 might_sleep_if(extra_checks);
4107 VALIDATE_DESC_VOID(desc);
4108 gpiod_set_raw_value_commit(desc, value);
4110 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
4113 * gpiod_set_value_cansleep() - assign a gpio's value
4114 * @desc: gpio whose value will be assigned
4115 * @value: value to assign
4117 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
4120 * This function is to be called from contexts that can sleep.
4122 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
4124 might_sleep_if(extra_checks);
4125 VALIDATE_DESC_VOID(desc);
4126 gpiod_set_value_nocheck(desc, value);
4128 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
4131 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
4132 * @array_size: number of elements in the descriptor array / value bitmap
4133 * @desc_array: array of GPIO descriptors whose values will be assigned
4134 * @array_info: information on applicability of fast bitmap processing path
4135 * @value_bitmap: bitmap of values to assign
4137 * Set the raw values of the GPIOs, i.e. the values of the physical lines
4138 * without regard for their ACTIVE_LOW status.
4140 * This function is to be called from contexts that can sleep.
4142 int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
4143 struct gpio_desc **desc_array,
4144 struct gpio_array *array_info,
4145 unsigned long *value_bitmap)
4147 might_sleep_if(extra_checks);
4150 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
4151 array_info, value_bitmap);
4153 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
4156 * gpiod_add_lookup_tables() - register GPIO device consumers
4157 * @tables: list of tables of consumers to register
4158 * @n: number of tables in the list
4160 void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
4164 mutex_lock(&gpio_lookup_lock);
4166 for (i = 0; i < n; i++)
4167 list_add_tail(&tables[i]->list, &gpio_lookup_list);
4169 mutex_unlock(&gpio_lookup_lock);
4173 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
4174 * @array_size: number of elements in the descriptor array / value bitmap
4175 * @desc_array: array of GPIO descriptors whose values will be assigned
4176 * @array_info: information on applicability of fast bitmap processing path
4177 * @value_bitmap: bitmap of values to assign
4179 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
4182 * This function is to be called from contexts that can sleep.
4184 int gpiod_set_array_value_cansleep(unsigned int array_size,
4185 struct gpio_desc **desc_array,
4186 struct gpio_array *array_info,
4187 unsigned long *value_bitmap)
4189 might_sleep_if(extra_checks);
4192 return gpiod_set_array_value_complex(false, true, array_size,
4193 desc_array, array_info,
4196 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
4199 * gpiod_add_lookup_table() - register GPIO device consumers
4200 * @table: table of consumers to register
4202 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
4204 mutex_lock(&gpio_lookup_lock);
4206 list_add_tail(&table->list, &gpio_lookup_list);
4208 mutex_unlock(&gpio_lookup_lock);
4210 EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
4213 * gpiod_remove_lookup_table() - unregister GPIO device consumers
4214 * @table: table of consumers to unregister
4216 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
4218 mutex_lock(&gpio_lookup_lock);
4220 list_del(&table->list);
4222 mutex_unlock(&gpio_lookup_lock);
4224 EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
4227 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
4228 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
4230 void gpiod_add_hogs(struct gpiod_hog *hogs)
4232 struct gpio_chip *chip;
4233 struct gpiod_hog *hog;
4235 mutex_lock(&gpio_machine_hogs_mutex);
4237 for (hog = &hogs[0]; hog->chip_label; hog++) {
4238 list_add_tail(&hog->list, &gpio_machine_hogs);
4241 * The chip may have been registered earlier, so check if it
4242 * exists and, if so, try to hog the line now.
4244 chip = find_chip_by_name(hog->chip_label);
4246 gpiochip_machine_hog(chip, hog);
4249 mutex_unlock(&gpio_machine_hogs_mutex);
4251 EXPORT_SYMBOL_GPL(gpiod_add_hogs);
4253 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
4255 const char *dev_id = dev ? dev_name(dev) : NULL;
4256 struct gpiod_lookup_table *table;
4258 mutex_lock(&gpio_lookup_lock);
4260 list_for_each_entry(table, &gpio_lookup_list, list) {
4261 if (table->dev_id && dev_id) {
4263 * Valid strings on both ends, must be identical to have
4266 if (!strcmp(table->dev_id, dev_id))
4270 * One of the pointers is NULL, so both must be to have
4273 if (dev_id == table->dev_id)
4280 mutex_unlock(&gpio_lookup_lock);
4284 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
4285 unsigned int idx, unsigned long *flags)
4287 struct gpio_desc *desc = ERR_PTR(-ENOENT);
4288 struct gpiod_lookup_table *table;
4289 struct gpiod_lookup *p;
4291 table = gpiod_find_lookup_table(dev);
4295 for (p = &table->table[0]; p->chip_label; p++) {
4296 struct gpio_chip *chip;
4298 /* idx must always match exactly */
4302 /* If the lookup entry has a con_id, require exact match */
4303 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
4306 chip = find_chip_by_name(p->chip_label);
4310 * As the lookup table indicates a chip with
4311 * p->chip_label should exist, assume it may
4312 * still appear later and let the interested
4313 * consumer be probed again or let the Deferred
4314 * Probe infrastructure handle the error.
4316 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
4318 return ERR_PTR(-EPROBE_DEFER);
4321 if (chip->ngpio <= p->chip_hwnum) {
4323 "requested GPIO %d is out of range [0..%d] for chip %s\n",
4324 idx, chip->ngpio, chip->label);
4325 return ERR_PTR(-EINVAL);
4328 desc = gpiochip_get_desc(chip, p->chip_hwnum);
4337 static int platform_gpio_count(struct device *dev, const char *con_id)
4339 struct gpiod_lookup_table *table;
4340 struct gpiod_lookup *p;
4341 unsigned int count = 0;
4343 table = gpiod_find_lookup_table(dev);
4347 for (p = &table->table[0]; p->chip_label; p++) {
4348 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
4349 (!con_id && !p->con_id))
4359 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
4360 * @fwnode: handle of the firmware node
4361 * @con_id: function within the GPIO consumer
4362 * @index: index of the GPIO to obtain for the consumer
4363 * @flags: GPIO initialization flags
4364 * @label: label to attach to the requested GPIO
4366 * This function can be used for drivers that get their configuration
4367 * from opaque firmware.
4369 * The function properly finds the corresponding GPIO using whatever is the
4370 * underlying firmware interface and then makes sure that the GPIO
4371 * descriptor is requested before it is returned to the caller.
4374 * On successful request the GPIO pin is configured in accordance with
4377 * In case of error an ERR_PTR() is returned.
4379 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
4380 const char *con_id, int index,
4381 enum gpiod_flags flags,
4384 struct gpio_desc *desc;
4385 char prop_name[32]; /* 32 is max size of property name */
4388 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
4390 snprintf(prop_name, sizeof(prop_name), "%s-%s",
4391 con_id, gpio_suffixes[i]);
4393 snprintf(prop_name, sizeof(prop_name), "%s",
4396 desc = fwnode_get_named_gpiod(fwnode, prop_name, index, flags,
4398 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
4404 EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
4407 * gpiod_count - return the number of GPIOs associated with a device / function
4408 * or -ENOENT if no GPIO has been assigned to the requested function
4409 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4410 * @con_id: function within the GPIO consumer
4412 int gpiod_count(struct device *dev, const char *con_id)
4414 int count = -ENOENT;
4416 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
4417 count = of_gpio_get_count(dev, con_id);
4418 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
4419 count = acpi_gpio_count(dev, con_id);
4422 count = platform_gpio_count(dev, con_id);
4426 EXPORT_SYMBOL_GPL(gpiod_count);
4429 * gpiod_get - obtain a GPIO for a given GPIO function
4430 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4431 * @con_id: function within the GPIO consumer
4432 * @flags: optional GPIO initialization flags
4434 * Return the GPIO descriptor corresponding to the function con_id of device
4435 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
4436 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
4438 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
4439 enum gpiod_flags flags)
4441 return gpiod_get_index(dev, con_id, 0, flags);
4443 EXPORT_SYMBOL_GPL(gpiod_get);
4446 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
4447 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4448 * @con_id: function within the GPIO consumer
4449 * @flags: optional GPIO initialization flags
4451 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
4452 * the requested function it will return NULL. This is convenient for drivers
4453 * that need to handle optional GPIOs.
4455 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
4457 enum gpiod_flags flags)
4459 return gpiod_get_index_optional(dev, con_id, 0, flags);
4461 EXPORT_SYMBOL_GPL(gpiod_get_optional);
4465 * gpiod_configure_flags - helper function to configure a given GPIO
4466 * @desc: gpio whose value will be assigned
4467 * @con_id: function within the GPIO consumer
4468 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4469 * of_find_gpio() or of_get_gpio_hog()
4470 * @dflags: gpiod_flags - optional GPIO initialization flags
4472 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
4473 * requested function and/or index, or another IS_ERR() code if an error
4474 * occurred while trying to acquire the GPIO.
4476 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
4477 unsigned long lflags, enum gpiod_flags dflags)
4481 if (lflags & GPIO_ACTIVE_LOW)
4482 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
4484 if (lflags & GPIO_OPEN_DRAIN)
4485 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4486 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
4488 * This enforces open drain mode from the consumer side.
4489 * This is necessary for some busses like I2C, but the lookup
4490 * should *REALLY* have specified them as open drain in the
4491 * first place, so print a little warning here.
4493 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4495 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
4498 if (lflags & GPIO_OPEN_SOURCE)
4499 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
4501 if ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) {
4503 "both pull-up and pull-down enabled, invalid configuration\n");
4507 if (lflags & GPIO_PULL_UP)
4508 set_bit(FLAG_PULL_UP, &desc->flags);
4509 else if (lflags & GPIO_PULL_DOWN)
4510 set_bit(FLAG_PULL_DOWN, &desc->flags);
4512 ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
4516 /* No particular flag request, return here... */
4517 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
4518 pr_debug("no flags found for %s\n", con_id);
4523 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
4524 ret = gpiod_direction_output(desc,
4525 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
4527 ret = gpiod_direction_input(desc);
4533 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
4534 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4535 * @con_id: function within the GPIO consumer
4536 * @idx: index of the GPIO to obtain in the consumer
4537 * @flags: optional GPIO initialization flags
4539 * This variant of gpiod_get() allows to access GPIOs other than the first
4540 * defined one for functions that define several GPIOs.
4542 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
4543 * requested function and/or index, or another IS_ERR() code if an error
4544 * occurred while trying to acquire the GPIO.
4546 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
4549 enum gpiod_flags flags)
4551 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
4552 struct gpio_desc *desc = NULL;
4554 /* Maybe we have a device name, maybe not */
4555 const char *devname = dev ? dev_name(dev) : "?";
4557 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
4560 /* Using device tree? */
4561 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
4562 dev_dbg(dev, "using device tree for GPIO lookup\n");
4563 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
4564 } else if (ACPI_COMPANION(dev)) {
4565 dev_dbg(dev, "using ACPI for GPIO lookup\n");
4566 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
4571 * Either we are not using DT or ACPI, or their lookup did not return
4572 * a result. In that case, use platform lookup as a fallback.
4574 if (!desc || desc == ERR_PTR(-ENOENT)) {
4575 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
4576 desc = gpiod_find(dev, con_id, idx, &lookupflags);
4580 dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
4585 * If a connection label was passed use that, else attempt to use
4586 * the device name as label
4588 ret = gpiod_request(desc, con_id ? con_id : devname);
4590 if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
4592 * This happens when there are several consumers for
4593 * the same GPIO line: we just return here without
4594 * further initialization. It is a bit if a hack.
4595 * This is necessary to support fixed regulators.
4597 * FIXME: Make this more sane and safe.
4599 dev_info(dev, "nonexclusive access to GPIO for %s\n",
4600 con_id ? con_id : devname);
4603 return ERR_PTR(ret);
4607 ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
4609 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
4611 return ERR_PTR(ret);
4616 EXPORT_SYMBOL_GPL(gpiod_get_index);
4619 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
4620 * @fwnode: handle of the firmware node
4621 * @propname: name of the firmware property representing the GPIO
4622 * @index: index of the GPIO to obtain for the consumer
4623 * @dflags: GPIO initialization flags
4624 * @label: label to attach to the requested GPIO
4626 * This function can be used for drivers that get their configuration
4627 * from opaque firmware.
4629 * The function properly finds the corresponding GPIO using whatever is the
4630 * underlying firmware interface and then makes sure that the GPIO
4631 * descriptor is requested before it is returned to the caller.
4634 * On successful request the GPIO pin is configured in accordance with
4637 * In case of error an ERR_PTR() is returned.
4639 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
4640 const char *propname, int index,
4641 enum gpiod_flags dflags,
4644 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
4645 struct gpio_desc *desc = ERR_PTR(-ENODEV);
4649 return ERR_PTR(-EINVAL);
4651 if (is_of_node(fwnode)) {
4652 desc = gpiod_get_from_of_node(to_of_node(fwnode),
4657 } else if (is_acpi_node(fwnode)) {
4658 struct acpi_gpio_info info;
4660 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
4664 acpi_gpio_update_gpiod_flags(&dflags, &info);
4665 acpi_gpio_update_gpiod_lookup_flags(&lflags, &info);
4668 /* Currently only ACPI takes this path */
4669 ret = gpiod_request(desc, label);
4671 return ERR_PTR(ret);
4673 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
4676 return ERR_PTR(ret);
4681 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
4684 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
4686 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4687 * @con_id: function within the GPIO consumer
4688 * @index: index of the GPIO to obtain in the consumer
4689 * @flags: optional GPIO initialization flags
4691 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
4692 * specified index was assigned to the requested function it will return NULL.
4693 * This is convenient for drivers that need to handle optional GPIOs.
4695 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
4698 enum gpiod_flags flags)
4700 struct gpio_desc *desc;
4702 desc = gpiod_get_index(dev, con_id, index, flags);
4704 if (PTR_ERR(desc) == -ENOENT)
4710 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
4713 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4714 * @desc: gpio whose value will be assigned
4715 * @name: gpio line name
4716 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4717 * of_find_gpio() or of_get_gpio_hog()
4718 * @dflags: gpiod_flags - optional GPIO initialization flags
4720 int gpiod_hog(struct gpio_desc *desc, const char *name,
4721 unsigned long lflags, enum gpiod_flags dflags)
4723 struct gpio_chip *chip;
4724 struct gpio_desc *local_desc;
4728 chip = gpiod_to_chip(desc);
4729 hwnum = gpio_chip_hwgpio(desc);
4731 local_desc = gpiochip_request_own_desc(chip, hwnum, name,
4733 if (IS_ERR(local_desc)) {
4734 ret = PTR_ERR(local_desc);
4735 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
4736 name, chip->label, hwnum, ret);
4740 /* Mark GPIO as hogged so it can be identified and removed later */
4741 set_bit(FLAG_IS_HOGGED, &desc->flags);
4743 pr_info("GPIO line %d (%s) hogged as %s%s\n",
4744 desc_to_gpio(desc), name,
4745 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4746 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
4747 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
4753 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4754 * @chip: gpio chip to act on
4756 static void gpiochip_free_hogs(struct gpio_chip *chip)
4760 for (id = 0; id < chip->ngpio; id++) {
4761 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
4762 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
4767 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4768 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4769 * @con_id: function within the GPIO consumer
4770 * @flags: optional GPIO initialization flags
4772 * This function acquires all the GPIOs defined under a given function.
4774 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4775 * no GPIO has been assigned to the requested function, or another IS_ERR()
4776 * code if an error occurred while trying to acquire the GPIOs.
4778 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4780 enum gpiod_flags flags)
4782 struct gpio_desc *desc;
4783 struct gpio_descs *descs;
4784 struct gpio_array *array_info = NULL;
4785 struct gpio_chip *chip;
4786 int count, bitmap_size;
4788 count = gpiod_count(dev, con_id);
4790 return ERR_PTR(count);
4792 descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
4794 return ERR_PTR(-ENOMEM);
4796 for (descs->ndescs = 0; descs->ndescs < count; ) {
4797 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4799 gpiod_put_array(descs);
4800 return ERR_CAST(desc);
4803 descs->desc[descs->ndescs] = desc;
4805 chip = gpiod_to_chip(desc);
4807 * If pin hardware number of array member 0 is also 0, select
4808 * its chip as a candidate for fast bitmap processing path.
4810 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
4811 struct gpio_descs *array;
4813 bitmap_size = BITS_TO_LONGS(chip->ngpio > count ?
4814 chip->ngpio : count);
4816 array = kzalloc(struct_size(descs, desc, count) +
4817 struct_size(array_info, invert_mask,
4818 3 * bitmap_size), GFP_KERNEL);
4820 gpiod_put_array(descs);
4821 return ERR_PTR(-ENOMEM);
4824 memcpy(array, descs,
4825 struct_size(descs, desc, descs->ndescs + 1));
4829 array_info = (void *)(descs->desc + count);
4830 array_info->get_mask = array_info->invert_mask +
4832 array_info->set_mask = array_info->get_mask +
4835 array_info->desc = descs->desc;
4836 array_info->size = count;
4837 array_info->chip = chip;
4838 bitmap_set(array_info->get_mask, descs->ndescs,
4839 count - descs->ndescs);
4840 bitmap_set(array_info->set_mask, descs->ndescs,
4841 count - descs->ndescs);
4842 descs->info = array_info;
4844 /* Unmark array members which don't belong to the 'fast' chip */
4845 if (array_info && array_info->chip != chip) {
4846 __clear_bit(descs->ndescs, array_info->get_mask);
4847 __clear_bit(descs->ndescs, array_info->set_mask);
4850 * Detect array members which belong to the 'fast' chip
4851 * but their pins are not in hardware order.
4853 else if (array_info &&
4854 gpio_chip_hwgpio(desc) != descs->ndescs) {
4856 * Don't use fast path if all array members processed so
4857 * far belong to the same chip as this one but its pin
4858 * hardware number is different from its array index.
4860 if (bitmap_full(array_info->get_mask, descs->ndescs)) {
4863 __clear_bit(descs->ndescs,
4864 array_info->get_mask);
4865 __clear_bit(descs->ndescs,
4866 array_info->set_mask);
4868 } else if (array_info) {
4869 /* Exclude open drain or open source from fast output */
4870 if (gpiochip_line_is_open_drain(chip, descs->ndescs) ||
4871 gpiochip_line_is_open_source(chip, descs->ndescs))
4872 __clear_bit(descs->ndescs,
4873 array_info->set_mask);
4874 /* Identify 'fast' pins which require invertion */
4875 if (gpiod_is_active_low(desc))
4876 __set_bit(descs->ndescs,
4877 array_info->invert_mask);
4884 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4885 array_info->chip->label, array_info->size,
4886 *array_info->get_mask, *array_info->set_mask,
4887 *array_info->invert_mask);
4890 EXPORT_SYMBOL_GPL(gpiod_get_array);
4893 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4895 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4896 * @con_id: function within the GPIO consumer
4897 * @flags: optional GPIO initialization flags
4899 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4900 * assigned to the requested function it will return NULL.
4902 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4904 enum gpiod_flags flags)
4906 struct gpio_descs *descs;
4908 descs = gpiod_get_array(dev, con_id, flags);
4909 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
4914 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4917 * gpiod_put - dispose of a GPIO descriptor
4918 * @desc: GPIO descriptor to dispose of
4920 * No descriptor can be used after gpiod_put() has been called on it.
4922 void gpiod_put(struct gpio_desc *desc)
4927 EXPORT_SYMBOL_GPL(gpiod_put);
4930 * gpiod_put_array - dispose of multiple GPIO descriptors
4931 * @descs: struct gpio_descs containing an array of descriptors
4933 void gpiod_put_array(struct gpio_descs *descs)
4937 for (i = 0; i < descs->ndescs; i++)
4938 gpiod_put(descs->desc[i]);
4942 EXPORT_SYMBOL_GPL(gpiod_put_array);
4944 static int __init gpiolib_dev_init(void)
4948 /* Register GPIO sysfs bus */
4949 ret = bus_register(&gpio_bus_type);
4951 pr_err("gpiolib: could not register GPIO bus type\n");
4955 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
4957 pr_err("gpiolib: failed to allocate char dev region\n");
4958 bus_unregister(&gpio_bus_type);
4960 gpiolib_initialized = true;
4961 gpiochip_setup_devs();
4965 core_initcall(gpiolib_dev_init);
4967 #ifdef CONFIG_DEBUG_FS
4969 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
4972 struct gpio_chip *chip = gdev->chip;
4973 unsigned gpio = gdev->base;
4974 struct gpio_desc *gdesc = &gdev->descs[0];
4979 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
4980 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
4982 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
4988 gpiod_get_direction(gdesc);
4989 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
4990 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
4991 active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags);
4992 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s",
4993 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
4994 is_out ? "out" : "in ",
4995 chip->get ? (chip->get(chip, i) ? "hi" : "lo") : "? ",
4996 is_irq ? "IRQ " : "",
4997 active_low ? "ACTIVE LOW" : "");
4998 seq_printf(s, "\n");
5002 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
5004 unsigned long flags;
5005 struct gpio_device *gdev = NULL;
5006 loff_t index = *pos;
5010 spin_lock_irqsave(&gpio_lock, flags);
5011 list_for_each_entry(gdev, &gpio_devices, list)
5013 spin_unlock_irqrestore(&gpio_lock, flags);
5016 spin_unlock_irqrestore(&gpio_lock, flags);
5021 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
5023 unsigned long flags;
5024 struct gpio_device *gdev = v;
5027 spin_lock_irqsave(&gpio_lock, flags);
5028 if (list_is_last(&gdev->list, &gpio_devices))
5031 ret = list_entry(gdev->list.next, struct gpio_device, list);
5032 spin_unlock_irqrestore(&gpio_lock, flags);
5040 static void gpiolib_seq_stop(struct seq_file *s, void *v)
5044 static int gpiolib_seq_show(struct seq_file *s, void *v)
5046 struct gpio_device *gdev = v;
5047 struct gpio_chip *chip = gdev->chip;
5048 struct device *parent;
5051 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
5052 dev_name(&gdev->dev));
5056 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
5057 dev_name(&gdev->dev),
5058 gdev->base, gdev->base + gdev->ngpio - 1);
5059 parent = chip->parent;
5061 seq_printf(s, ", parent: %s/%s",
5062 parent->bus ? parent->bus->name : "no-bus",
5065 seq_printf(s, ", %s", chip->label);
5066 if (chip->can_sleep)
5067 seq_printf(s, ", can sleep");
5068 seq_printf(s, ":\n");
5071 chip->dbg_show(s, chip);
5073 gpiolib_dbg_show(s, gdev);
5078 static const struct seq_operations gpiolib_seq_ops = {
5079 .start = gpiolib_seq_start,
5080 .next = gpiolib_seq_next,
5081 .stop = gpiolib_seq_stop,
5082 .show = gpiolib_seq_show,
5085 static int gpiolib_open(struct inode *inode, struct file *file)
5087 return seq_open(file, &gpiolib_seq_ops);
5090 static const struct file_operations gpiolib_operations = {
5091 .owner = THIS_MODULE,
5092 .open = gpiolib_open,
5094 .llseek = seq_lseek,
5095 .release = seq_release,
5098 static int __init gpiolib_debugfs_init(void)
5100 /* /sys/kernel/debug/gpio */
5101 debugfs_create_file("gpio", S_IFREG | S_IRUGO, NULL, NULL,
5102 &gpiolib_operations);
5105 subsys_initcall(gpiolib_debugfs_init);
5107 #endif /* DEBUG_FS */