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);
224 * Open drain emulation using input mode may incorrectly report
225 * input here, fix that up.
227 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) &&
228 test_bit(FLAG_IS_OUT, &desc->flags))
231 if (!chip->get_direction)
234 ret = chip->get_direction(chip, offset);
236 /* GPIOF_DIR_IN, or other positive */
238 clear_bit(FLAG_IS_OUT, &desc->flags);
242 set_bit(FLAG_IS_OUT, &desc->flags);
246 EXPORT_SYMBOL_GPL(gpiod_get_direction);
249 * Add a new chip to the global chips list, keeping the list of chips sorted
250 * by range(means [base, base + ngpio - 1]) order.
252 * Return -EBUSY if the new chip overlaps with some other chip's integer
255 static int gpiodev_add_to_list(struct gpio_device *gdev)
257 struct gpio_device *prev, *next;
259 if (list_empty(&gpio_devices)) {
260 /* initial entry in list */
261 list_add_tail(&gdev->list, &gpio_devices);
265 next = list_entry(gpio_devices.next, struct gpio_device, list);
266 if (gdev->base + gdev->ngpio <= next->base) {
267 /* add before first entry */
268 list_add(&gdev->list, &gpio_devices);
272 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
273 if (prev->base + prev->ngpio <= gdev->base) {
274 /* add behind last entry */
275 list_add_tail(&gdev->list, &gpio_devices);
279 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
280 /* at the end of the list */
281 if (&next->list == &gpio_devices)
284 /* add between prev and next */
285 if (prev->base + prev->ngpio <= gdev->base
286 && gdev->base + gdev->ngpio <= next->base) {
287 list_add(&gdev->list, &prev->list);
292 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
297 * Convert a GPIO name to its descriptor
299 static struct gpio_desc *gpio_name_to_desc(const char * const name)
301 struct gpio_device *gdev;
304 spin_lock_irqsave(&gpio_lock, flags);
306 list_for_each_entry(gdev, &gpio_devices, list) {
309 for (i = 0; i != gdev->ngpio; ++i) {
310 struct gpio_desc *desc = &gdev->descs[i];
312 if (!desc->name || !name)
315 if (!strcmp(desc->name, name)) {
316 spin_unlock_irqrestore(&gpio_lock, flags);
322 spin_unlock_irqrestore(&gpio_lock, flags);
328 * Takes the names from gc->names and checks if they are all unique. If they
329 * are, they are assigned to their gpio descriptors.
331 * Warning if one of the names is already used for a different GPIO.
333 static int gpiochip_set_desc_names(struct gpio_chip *gc)
335 struct gpio_device *gdev = gc->gpiodev;
341 /* First check all names if they are unique */
342 for (i = 0; i != gc->ngpio; ++i) {
343 struct gpio_desc *gpio;
345 gpio = gpio_name_to_desc(gc->names[i]);
348 "Detected name collision for GPIO name '%s'\n",
352 /* Then add all names to the GPIO descriptors */
353 for (i = 0; i != gc->ngpio; ++i)
354 gdev->descs[i].name = gc->names[i];
359 static unsigned long *gpiochip_allocate_mask(struct gpio_chip *chip)
363 p = bitmap_alloc(chip->ngpio, GFP_KERNEL);
367 /* Assume by default all GPIOs are valid */
368 bitmap_fill(p, chip->ngpio);
373 static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
375 if (!(of_gpio_need_valid_mask(gc) || gc->init_valid_mask))
378 gc->valid_mask = gpiochip_allocate_mask(gc);
385 static int gpiochip_init_valid_mask(struct gpio_chip *gc)
387 if (gc->init_valid_mask)
388 return gc->init_valid_mask(gc,
395 static void gpiochip_free_valid_mask(struct gpio_chip *gpiochip)
397 bitmap_free(gpiochip->valid_mask);
398 gpiochip->valid_mask = NULL;
401 bool gpiochip_line_is_valid(const struct gpio_chip *gpiochip,
404 /* No mask means all valid */
405 if (likely(!gpiochip->valid_mask))
407 return test_bit(offset, gpiochip->valid_mask);
409 EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
412 * GPIO line handle management
416 * struct linehandle_state - contains the state of a userspace handle
417 * @gdev: the GPIO device the handle pertains to
418 * @label: consumer label used to tag descriptors
419 * @descs: the GPIO descriptors held by this handle
420 * @numdescs: the number of descriptors held in the descs array
422 struct linehandle_state {
423 struct gpio_device *gdev;
425 struct gpio_desc *descs[GPIOHANDLES_MAX];
429 #define GPIOHANDLE_REQUEST_VALID_FLAGS \
430 (GPIOHANDLE_REQUEST_INPUT | \
431 GPIOHANDLE_REQUEST_OUTPUT | \
432 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
433 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
434 GPIOHANDLE_REQUEST_OPEN_SOURCE)
436 static long linehandle_ioctl(struct file *filep, unsigned int cmd,
439 struct linehandle_state *lh = filep->private_data;
440 void __user *ip = (void __user *)arg;
441 struct gpiohandle_data ghd;
442 DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
445 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
446 /* NOTE: It's ok to read values of output lines. */
447 int ret = gpiod_get_array_value_complex(false,
456 memset(&ghd, 0, sizeof(ghd));
457 for (i = 0; i < lh->numdescs; i++)
458 ghd.values[i] = test_bit(i, vals);
460 if (copy_to_user(ip, &ghd, sizeof(ghd)))
464 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
466 * All line descriptors were created at once with the same
467 * flags so just check if the first one is really output.
469 if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
472 if (copy_from_user(&ghd, ip, sizeof(ghd)))
475 /* Clamp all values to [0,1] */
476 for (i = 0; i < lh->numdescs; i++)
477 __assign_bit(i, vals, ghd.values[i]);
479 /* Reuse the array setting function */
480 return gpiod_set_array_value_complex(false,
491 static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
494 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
498 static int linehandle_release(struct inode *inode, struct file *filep)
500 struct linehandle_state *lh = filep->private_data;
501 struct gpio_device *gdev = lh->gdev;
504 for (i = 0; i < lh->numdescs; i++)
505 gpiod_free(lh->descs[i]);
508 put_device(&gdev->dev);
512 static const struct file_operations linehandle_fileops = {
513 .release = linehandle_release,
514 .owner = THIS_MODULE,
515 .llseek = noop_llseek,
516 .unlocked_ioctl = linehandle_ioctl,
518 .compat_ioctl = linehandle_ioctl_compat,
522 static int linehandle_create(struct gpio_device *gdev, void __user *ip)
524 struct gpiohandle_request handlereq;
525 struct linehandle_state *lh;
527 int fd, i, count = 0, ret;
530 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
532 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
535 lflags = handlereq.flags;
537 /* Return an error if an unknown flag is set */
538 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
542 * Do not allow both INPUT & OUTPUT flags to be set as they are
545 if ((lflags & GPIOHANDLE_REQUEST_INPUT) &&
546 (lflags & GPIOHANDLE_REQUEST_OUTPUT))
550 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
551 * the hardware actually supports enabling both at the same time the
552 * electrical result would be disastrous.
554 if ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
555 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
558 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
559 if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) &&
560 ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
561 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
564 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
568 get_device(&gdev->dev);
570 /* Make sure this is terminated */
571 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
572 if (strlen(handlereq.consumer_label)) {
573 lh->label = kstrdup(handlereq.consumer_label,
581 /* Request each GPIO */
582 for (i = 0; i < handlereq.lines; i++) {
583 u32 offset = handlereq.lineoffsets[i];
584 struct gpio_desc *desc;
586 if (offset >= gdev->ngpio) {
591 desc = &gdev->descs[offset];
592 ret = gpiod_request(desc, lh->label);
598 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
599 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
600 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
601 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
602 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
603 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
605 ret = gpiod_set_transitory(desc, false);
610 * Lines have to be requested explicitly for input
611 * or output, else the line will be treated "as is".
613 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
614 int val = !!handlereq.default_values[i];
616 ret = gpiod_direction_output(desc, val);
619 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
620 ret = gpiod_direction_input(desc);
624 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
627 /* Let i point at the last handle */
629 lh->numdescs = handlereq.lines;
631 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
637 file = anon_inode_getfile("gpio-linehandle",
640 O_RDONLY | O_CLOEXEC);
643 goto out_put_unused_fd;
647 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
649 * fput() will trigger the release() callback, so do not go onto
650 * the regular error cleanup path here.
657 fd_install(fd, file);
659 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
667 for (i = 0; i < count; i++)
668 gpiod_free(lh->descs[i]);
672 put_device(&gdev->dev);
677 * GPIO line event management
681 * struct lineevent_state - contains the state of a userspace event
682 * @gdev: the GPIO device the event pertains to
683 * @label: consumer label used to tag descriptors
684 * @desc: the GPIO descriptor held by this event
685 * @eflags: the event flags this line was requested with
686 * @irq: the interrupt that trigger in response to events on this GPIO
687 * @wait: wait queue that handles blocking reads of events
688 * @events: KFIFO for the GPIO events
689 * @read_lock: mutex lock to protect reads from colliding with adding
690 * new events to the FIFO
691 * @timestamp: cache for the timestamp storing it between hardirq
692 * and IRQ thread, used to bring the timestamp close to the actual
695 struct lineevent_state {
696 struct gpio_device *gdev;
698 struct gpio_desc *desc;
701 wait_queue_head_t wait;
702 DECLARE_KFIFO(events, struct gpioevent_data, 16);
703 struct mutex read_lock;
707 #define GPIOEVENT_REQUEST_VALID_FLAGS \
708 (GPIOEVENT_REQUEST_RISING_EDGE | \
709 GPIOEVENT_REQUEST_FALLING_EDGE)
711 static __poll_t lineevent_poll(struct file *filep,
712 struct poll_table_struct *wait)
714 struct lineevent_state *le = filep->private_data;
717 poll_wait(filep, &le->wait, wait);
719 if (!kfifo_is_empty(&le->events))
720 events = EPOLLIN | EPOLLRDNORM;
726 static ssize_t lineevent_read(struct file *filep,
731 struct lineevent_state *le = filep->private_data;
735 if (count < sizeof(struct gpioevent_data))
739 if (kfifo_is_empty(&le->events)) {
740 if (filep->f_flags & O_NONBLOCK)
743 ret = wait_event_interruptible(le->wait,
744 !kfifo_is_empty(&le->events));
749 if (mutex_lock_interruptible(&le->read_lock))
751 ret = kfifo_to_user(&le->events, buf, count, &copied);
752 mutex_unlock(&le->read_lock);
758 * If we couldn't read anything from the fifo (a different
759 * thread might have been faster) we either return -EAGAIN if
760 * the file descriptor is non-blocking, otherwise we go back to
761 * sleep and wait for more data to arrive.
763 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
766 } while (copied == 0);
771 static int lineevent_release(struct inode *inode, struct file *filep)
773 struct lineevent_state *le = filep->private_data;
774 struct gpio_device *gdev = le->gdev;
776 free_irq(le->irq, le);
777 gpiod_free(le->desc);
780 put_device(&gdev->dev);
784 static long lineevent_ioctl(struct file *filep, unsigned int cmd,
787 struct lineevent_state *le = filep->private_data;
788 void __user *ip = (void __user *)arg;
789 struct gpiohandle_data ghd;
792 * We can get the value for an event line but not set it,
793 * because it is input by definition.
795 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
798 memset(&ghd, 0, sizeof(ghd));
800 val = gpiod_get_value_cansleep(le->desc);
805 if (copy_to_user(ip, &ghd, sizeof(ghd)))
814 static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
817 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
821 static const struct file_operations lineevent_fileops = {
822 .release = lineevent_release,
823 .read = lineevent_read,
824 .poll = lineevent_poll,
825 .owner = THIS_MODULE,
826 .llseek = noop_llseek,
827 .unlocked_ioctl = lineevent_ioctl,
829 .compat_ioctl = lineevent_ioctl_compat,
833 static irqreturn_t lineevent_irq_thread(int irq, void *p)
835 struct lineevent_state *le = p;
836 struct gpioevent_data ge;
839 /* Do not leak kernel stack to userspace */
840 memset(&ge, 0, sizeof(ge));
843 * We may be running from a nested threaded interrupt in which case
844 * we didn't get the timestamp from lineevent_irq_handler().
847 ge.timestamp = ktime_get_real_ns();
849 ge.timestamp = le->timestamp;
851 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
852 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
853 int level = gpiod_get_value_cansleep(le->desc);
855 /* Emit low-to-high event */
856 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
858 /* Emit high-to-low event */
859 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
860 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
861 /* Emit low-to-high event */
862 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
863 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
864 /* Emit high-to-low event */
865 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
870 ret = kfifo_put(&le->events, ge);
872 wake_up_poll(&le->wait, EPOLLIN);
877 static irqreturn_t lineevent_irq_handler(int irq, void *p)
879 struct lineevent_state *le = p;
882 * Just store the timestamp in hardirq context so we get it as
883 * close in time as possible to the actual event.
885 le->timestamp = ktime_get_real_ns();
887 return IRQ_WAKE_THREAD;
890 static int lineevent_create(struct gpio_device *gdev, void __user *ip)
892 struct gpioevent_request eventreq;
893 struct lineevent_state *le;
894 struct gpio_desc *desc;
903 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
906 le = kzalloc(sizeof(*le), GFP_KERNEL);
910 get_device(&gdev->dev);
912 /* Make sure this is terminated */
913 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
914 if (strlen(eventreq.consumer_label)) {
915 le->label = kstrdup(eventreq.consumer_label,
923 offset = eventreq.lineoffset;
924 lflags = eventreq.handleflags;
925 eflags = eventreq.eventflags;
927 if (offset >= gdev->ngpio) {
932 /* Return an error if a unknown flag is set */
933 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
934 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
939 /* This is just wrong: we don't look for events on output lines */
940 if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
941 (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
942 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) {
947 desc = &gdev->descs[offset];
948 ret = gpiod_request(desc, le->label);
954 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
955 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
957 ret = gpiod_direction_input(desc);
961 le->irq = gpiod_to_irq(desc);
967 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
968 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
969 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
970 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
971 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
972 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
973 irqflags |= IRQF_ONESHOT;
975 INIT_KFIFO(le->events);
976 init_waitqueue_head(&le->wait);
977 mutex_init(&le->read_lock);
979 /* Request a thread to read the events */
980 ret = request_threaded_irq(le->irq,
981 lineevent_irq_handler,
982 lineevent_irq_thread,
989 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
995 file = anon_inode_getfile("gpio-event",
998 O_RDONLY | O_CLOEXEC);
1000 ret = PTR_ERR(file);
1001 goto out_put_unused_fd;
1005 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
1007 * fput() will trigger the release() callback, so do not go onto
1008 * the regular error cleanup path here.
1015 fd_install(fd, file);
1022 free_irq(le->irq, le);
1024 gpiod_free(le->desc);
1029 put_device(&gdev->dev);
1034 * gpio_ioctl() - ioctl handler for the GPIO chardev
1036 static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1038 struct gpio_device *gdev = filp->private_data;
1039 struct gpio_chip *chip = gdev->chip;
1040 void __user *ip = (void __user *)arg;
1042 /* We fail any subsequent ioctl():s when the chip is gone */
1046 /* Fill in the struct and pass to userspace */
1047 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
1048 struct gpiochip_info chipinfo;
1050 memset(&chipinfo, 0, sizeof(chipinfo));
1052 strncpy(chipinfo.name, dev_name(&gdev->dev),
1053 sizeof(chipinfo.name));
1054 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
1055 strncpy(chipinfo.label, gdev->label,
1056 sizeof(chipinfo.label));
1057 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
1058 chipinfo.lines = gdev->ngpio;
1059 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
1062 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
1063 struct gpioline_info lineinfo;
1064 struct gpio_desc *desc;
1066 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1068 if (lineinfo.line_offset >= gdev->ngpio)
1071 desc = &gdev->descs[lineinfo.line_offset];
1073 strncpy(lineinfo.name, desc->name,
1074 sizeof(lineinfo.name));
1075 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
1077 lineinfo.name[0] = '\0';
1080 strncpy(lineinfo.consumer, desc->label,
1081 sizeof(lineinfo.consumer));
1082 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
1084 lineinfo.consumer[0] = '\0';
1088 * Userspace only need to know that the kernel is using
1089 * this GPIO so it can't use it.
1092 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
1093 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
1094 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
1095 test_bit(FLAG_EXPORT, &desc->flags) ||
1096 test_bit(FLAG_SYSFS, &desc->flags) ||
1097 !pinctrl_gpio_can_use_line(chip->base + lineinfo.line_offset))
1098 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
1099 if (test_bit(FLAG_IS_OUT, &desc->flags))
1100 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
1101 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1102 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
1103 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1104 lineinfo.flags |= (GPIOLINE_FLAG_OPEN_DRAIN |
1105 GPIOLINE_FLAG_IS_OUT);
1106 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1107 lineinfo.flags |= (GPIOLINE_FLAG_OPEN_SOURCE |
1108 GPIOLINE_FLAG_IS_OUT);
1110 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
1113 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
1114 return linehandle_create(gdev, ip);
1115 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
1116 return lineevent_create(gdev, ip);
1121 #ifdef CONFIG_COMPAT
1122 static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
1125 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
1130 * gpio_chrdev_open() - open the chardev for ioctl operations
1131 * @inode: inode for this chardev
1132 * @filp: file struct for storing private data
1133 * Returns 0 on success
1135 static int gpio_chrdev_open(struct inode *inode, struct file *filp)
1137 struct gpio_device *gdev = container_of(inode->i_cdev,
1138 struct gpio_device, chrdev);
1140 /* Fail on open if the backing gpiochip is gone */
1143 get_device(&gdev->dev);
1144 filp->private_data = gdev;
1146 return nonseekable_open(inode, filp);
1150 * gpio_chrdev_release() - close chardev after ioctl operations
1151 * @inode: inode for this chardev
1152 * @filp: file struct for storing private data
1153 * Returns 0 on success
1155 static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1157 struct gpio_device *gdev = container_of(inode->i_cdev,
1158 struct gpio_device, chrdev);
1160 put_device(&gdev->dev);
1165 static const struct file_operations gpio_fileops = {
1166 .release = gpio_chrdev_release,
1167 .open = gpio_chrdev_open,
1168 .owner = THIS_MODULE,
1169 .llseek = no_llseek,
1170 .unlocked_ioctl = gpio_ioctl,
1171 #ifdef CONFIG_COMPAT
1172 .compat_ioctl = gpio_ioctl_compat,
1176 static void gpiodevice_release(struct device *dev)
1178 struct gpio_device *gdev = dev_get_drvdata(dev);
1180 list_del(&gdev->list);
1181 ida_simple_remove(&gpio_ida, gdev->id);
1182 kfree_const(gdev->label);
1187 static int gpiochip_setup_dev(struct gpio_device *gdev)
1191 cdev_init(&gdev->chrdev, &gpio_fileops);
1192 gdev->chrdev.owner = THIS_MODULE;
1193 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
1195 ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
1199 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1200 MAJOR(gpio_devt), gdev->id);
1202 ret = gpiochip_sysfs_register(gdev);
1204 goto err_remove_device;
1206 /* From this point, the .release() function cleans up gpio_device */
1207 gdev->dev.release = gpiodevice_release;
1208 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1209 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1210 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1215 cdev_device_del(&gdev->chrdev, &gdev->dev);
1219 static void gpiochip_machine_hog(struct gpio_chip *chip, struct gpiod_hog *hog)
1221 struct gpio_desc *desc;
1224 desc = gpiochip_get_desc(chip, hog->chip_hwnum);
1226 pr_err("%s: unable to get GPIO desc: %ld\n",
1227 __func__, PTR_ERR(desc));
1231 if (test_bit(FLAG_IS_HOGGED, &desc->flags))
1234 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
1236 pr_err("%s: unable to hog GPIO line (%s:%u): %d\n",
1237 __func__, chip->label, hog->chip_hwnum, rv);
1240 static void machine_gpiochip_add(struct gpio_chip *chip)
1242 struct gpiod_hog *hog;
1244 mutex_lock(&gpio_machine_hogs_mutex);
1246 list_for_each_entry(hog, &gpio_machine_hogs, list) {
1247 if (!strcmp(chip->label, hog->chip_label))
1248 gpiochip_machine_hog(chip, hog);
1251 mutex_unlock(&gpio_machine_hogs_mutex);
1254 static void gpiochip_setup_devs(void)
1256 struct gpio_device *gdev;
1259 list_for_each_entry(gdev, &gpio_devices, list) {
1260 ret = gpiochip_setup_dev(gdev);
1262 pr_err("%s: Failed to initialize gpio device (%d)\n",
1263 dev_name(&gdev->dev), ret);
1267 int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
1268 struct lock_class_key *lock_key,
1269 struct lock_class_key *request_key)
1271 unsigned long flags;
1274 int base = chip->base;
1275 struct gpio_device *gdev;
1278 * First: allocate and populate the internal stat container, and
1279 * set up the struct device.
1281 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
1284 gdev->dev.bus = &gpio_bus_type;
1286 chip->gpiodev = gdev;
1288 gdev->dev.parent = chip->parent;
1289 gdev->dev.of_node = chip->parent->of_node;
1292 #ifdef CONFIG_OF_GPIO
1293 /* If the gpiochip has an assigned OF node this takes precedence */
1295 gdev->dev.of_node = chip->of_node;
1297 chip->of_node = gdev->dev.of_node;
1300 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1305 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1306 device_initialize(&gdev->dev);
1307 dev_set_drvdata(&gdev->dev, gdev);
1308 if (chip->parent && chip->parent->driver)
1309 gdev->owner = chip->parent->driver->owner;
1310 else if (chip->owner)
1311 /* TODO: remove chip->owner */
1312 gdev->owner = chip->owner;
1314 gdev->owner = THIS_MODULE;
1316 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
1322 if (chip->ngpio == 0) {
1323 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
1325 goto err_free_descs;
1328 if (chip->ngpio > FASTPATH_NGPIO)
1329 chip_warn(chip, "line cnt %u is greater than fast path cnt %u\n",
1330 chip->ngpio, FASTPATH_NGPIO);
1332 gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);
1335 goto err_free_descs;
1338 gdev->ngpio = chip->ngpio;
1341 spin_lock_irqsave(&gpio_lock, flags);
1344 * TODO: this allocates a Linux GPIO number base in the global
1345 * GPIO numberspace for this chip. In the long run we want to
1346 * get *rid* of this numberspace and use only descriptors, but
1347 * it may be a pipe dream. It will not happen before we get rid
1348 * of the sysfs interface anyways.
1351 base = gpiochip_find_base(chip->ngpio);
1354 spin_unlock_irqrestore(&gpio_lock, flags);
1355 goto err_free_label;
1358 * TODO: it should not be necessary to reflect the assigned
1359 * base outside of the GPIO subsystem. Go over drivers and
1360 * see if anyone makes use of this, else drop this and assign
1367 ret = gpiodev_add_to_list(gdev);
1369 spin_unlock_irqrestore(&gpio_lock, flags);
1370 goto err_free_label;
1373 spin_unlock_irqrestore(&gpio_lock, flags);
1375 for (i = 0; i < chip->ngpio; i++)
1376 gdev->descs[i].gdev = gdev;
1378 #ifdef CONFIG_PINCTRL
1379 INIT_LIST_HEAD(&gdev->pin_ranges);
1382 ret = gpiochip_set_desc_names(chip);
1384 goto err_remove_from_list;
1386 ret = gpiochip_alloc_valid_mask(chip);
1388 goto err_remove_from_list;
1390 ret = of_gpiochip_add(chip);
1392 goto err_free_gpiochip_mask;
1394 ret = gpiochip_init_valid_mask(chip);
1396 goto err_remove_of_chip;
1398 for (i = 0; i < chip->ngpio; i++) {
1399 struct gpio_desc *desc = &gdev->descs[i];
1401 if (chip->get_direction && gpiochip_line_is_valid(chip, i)) {
1402 if (!chip->get_direction(chip, i))
1403 set_bit(FLAG_IS_OUT, &desc->flags);
1405 clear_bit(FLAG_IS_OUT, &desc->flags);
1407 if (!chip->direction_input)
1408 set_bit(FLAG_IS_OUT, &desc->flags);
1410 clear_bit(FLAG_IS_OUT, &desc->flags);
1414 acpi_gpiochip_add(chip);
1416 machine_gpiochip_add(chip);
1418 ret = gpiochip_irqchip_init_hw(chip);
1420 goto err_remove_acpi_chip;
1422 ret = gpiochip_irqchip_init_valid_mask(chip);
1424 goto err_remove_acpi_chip;
1426 ret = gpiochip_add_irqchip(chip, lock_key, request_key);
1428 goto err_remove_irqchip_mask;
1431 * By first adding the chardev, and then adding the device,
1432 * we get a device node entry in sysfs under
1433 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1434 * coldplug of device nodes and other udev business.
1435 * We can do this only if gpiolib has been initialized.
1436 * Otherwise, defer until later.
1438 if (gpiolib_initialized) {
1439 ret = gpiochip_setup_dev(gdev);
1441 goto err_remove_irqchip;
1446 gpiochip_irqchip_remove(chip);
1447 err_remove_irqchip_mask:
1448 gpiochip_irqchip_free_valid_mask(chip);
1449 err_remove_acpi_chip:
1450 acpi_gpiochip_remove(chip);
1452 gpiochip_free_hogs(chip);
1453 of_gpiochip_remove(chip);
1454 err_free_gpiochip_mask:
1455 gpiochip_remove_pin_ranges(chip);
1456 gpiochip_free_valid_mask(chip);
1457 err_remove_from_list:
1458 spin_lock_irqsave(&gpio_lock, flags);
1459 list_del(&gdev->list);
1460 spin_unlock_irqrestore(&gpio_lock, flags);
1462 kfree_const(gdev->label);
1466 ida_simple_remove(&gpio_ida, gdev->id);
1468 /* failures here can mean systems won't boot... */
1469 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
1470 gdev->base, gdev->base + gdev->ngpio - 1,
1471 chip->label ? : "generic", ret);
1475 EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
1478 * gpiochip_get_data() - get per-subdriver data for the chip
1482 * The per-subdriver data for the chip.
1484 void *gpiochip_get_data(struct gpio_chip *chip)
1486 return chip->gpiodev->data;
1488 EXPORT_SYMBOL_GPL(gpiochip_get_data);
1491 * gpiochip_remove() - unregister a gpio_chip
1492 * @chip: the chip to unregister
1494 * A gpio_chip with any GPIOs still requested may not be removed.
1496 void gpiochip_remove(struct gpio_chip *chip)
1498 struct gpio_device *gdev = chip->gpiodev;
1499 struct gpio_desc *desc;
1500 unsigned long flags;
1502 bool requested = false;
1504 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
1505 gpiochip_sysfs_unregister(gdev);
1506 gpiochip_free_hogs(chip);
1507 /* Numb the device, cancelling all outstanding operations */
1509 gpiochip_irqchip_remove(chip);
1510 acpi_gpiochip_remove(chip);
1511 of_gpiochip_remove(chip);
1512 gpiochip_remove_pin_ranges(chip);
1513 gpiochip_free_valid_mask(chip);
1515 * We accept no more calls into the driver from this point, so
1516 * NULL the driver data pointer
1520 spin_lock_irqsave(&gpio_lock, flags);
1521 for (i = 0; i < gdev->ngpio; i++) {
1522 desc = &gdev->descs[i];
1523 if (test_bit(FLAG_REQUESTED, &desc->flags))
1526 spin_unlock_irqrestore(&gpio_lock, flags);
1529 dev_crit(&gdev->dev,
1530 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
1533 * The gpiochip side puts its use of the device to rest here:
1534 * if there are no userspace clients, the chardev and device will
1535 * be removed, else it will be dangling until the last user is
1538 cdev_device_del(&gdev->chrdev, &gdev->dev);
1539 put_device(&gdev->dev);
1541 EXPORT_SYMBOL_GPL(gpiochip_remove);
1543 static void devm_gpio_chip_release(struct device *dev, void *res)
1545 struct gpio_chip *chip = *(struct gpio_chip **)res;
1547 gpiochip_remove(chip);
1551 * devm_gpiochip_add_data() - Resource manager gpiochip_add_data()
1552 * @dev: pointer to the device that gpio_chip belongs to.
1553 * @chip: the chip to register, with chip->base initialized
1554 * @data: driver-private data associated with this chip
1556 * Context: potentially before irqs will work
1558 * The gpio chip automatically be released when the device is unbound.
1561 * A negative errno if the chip can't be registered, such as because the
1562 * chip->base is invalid or already associated with a different chip.
1563 * Otherwise it returns zero as a success code.
1565 int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1568 struct gpio_chip **ptr;
1571 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1576 ret = gpiochip_add_data(chip, data);
1583 devres_add(dev, ptr);
1587 EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1590 * gpiochip_find() - iterator for locating a specific gpio_chip
1591 * @data: data to pass to match function
1592 * @match: Callback function to check gpio_chip
1594 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1595 * determined by a user supplied @match callback. The callback should return
1596 * 0 if the device doesn't match and non-zero if it does. If the callback is
1597 * non-zero, this function will return to the caller and not iterate over any
1600 struct gpio_chip *gpiochip_find(void *data,
1601 int (*match)(struct gpio_chip *chip,
1604 struct gpio_device *gdev;
1605 struct gpio_chip *chip = NULL;
1606 unsigned long flags;
1608 spin_lock_irqsave(&gpio_lock, flags);
1609 list_for_each_entry(gdev, &gpio_devices, list)
1610 if (gdev->chip && match(gdev->chip, data)) {
1615 spin_unlock_irqrestore(&gpio_lock, flags);
1619 EXPORT_SYMBOL_GPL(gpiochip_find);
1621 static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1623 const char *name = data;
1625 return !strcmp(chip->label, name);
1628 static struct gpio_chip *find_chip_by_name(const char *name)
1630 return gpiochip_find((void *)name, gpiochip_match_name);
1633 #ifdef CONFIG_GPIOLIB_IRQCHIP
1636 * The following is irqchip helper code for gpiochips.
1639 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1641 struct gpio_irq_chip *girq = &gc->irq;
1646 return girq->init_hw(gc);
1649 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
1651 struct gpio_irq_chip *girq = &gc->irq;
1653 if (!girq->init_valid_mask)
1656 girq->valid_mask = gpiochip_allocate_mask(gc);
1657 if (!girq->valid_mask)
1660 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
1665 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1667 bitmap_free(gpiochip->irq.valid_mask);
1668 gpiochip->irq.valid_mask = NULL;
1671 bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1672 unsigned int offset)
1674 if (!gpiochip_line_is_valid(gpiochip, offset))
1676 /* No mask means all valid */
1677 if (likely(!gpiochip->irq.valid_mask))
1679 return test_bit(offset, gpiochip->irq.valid_mask);
1681 EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
1684 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
1685 * @gc: the gpiochip to set the irqchip chain to
1686 * @parent_irq: the irq number corresponding to the parent IRQ for this
1688 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1689 * coming out of the gpiochip. If the interrupt is nested rather than
1690 * cascaded, pass NULL in this handler argument
1692 static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gc,
1693 unsigned int parent_irq,
1694 irq_flow_handler_t parent_handler)
1696 struct gpio_irq_chip *girq = &gc->irq;
1697 struct device *dev = &gc->gpiodev->dev;
1699 if (!girq->domain) {
1700 chip_err(gc, "called %s before setting up irqchip\n",
1705 if (parent_handler) {
1706 if (gc->can_sleep) {
1708 "you cannot have chained interrupts on a chip that may sleep\n");
1711 girq->parents = devm_kcalloc(dev, 1,
1712 sizeof(*girq->parents),
1714 if (!girq->parents) {
1715 chip_err(gc, "out of memory allocating parent IRQ\n");
1718 girq->parents[0] = parent_irq;
1719 girq->num_parents = 1;
1721 * The parent irqchip is already using the chip_data for this
1722 * irqchip, so our callbacks simply use the handler_data.
1724 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1730 * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
1731 * @gpiochip: the gpiochip to set the irqchip chain to
1732 * @irqchip: the irqchip to chain to the gpiochip
1733 * @parent_irq: the irq number corresponding to the parent IRQ for this
1735 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1736 * coming out of the gpiochip.
1738 void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1739 struct irq_chip *irqchip,
1740 unsigned int parent_irq,
1741 irq_flow_handler_t parent_handler)
1743 if (gpiochip->irq.threaded) {
1744 chip_err(gpiochip, "tried to chain a threaded gpiochip\n");
1748 gpiochip_set_cascaded_irqchip(gpiochip, parent_irq, parent_handler);
1750 EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1753 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1754 * @gpiochip: the gpiochip to set the irqchip nested handler to
1755 * @irqchip: the irqchip to nest to the gpiochip
1756 * @parent_irq: the irq number corresponding to the parent IRQ for this
1759 void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1760 struct irq_chip *irqchip,
1761 unsigned int parent_irq)
1763 gpiochip_set_cascaded_irqchip(gpiochip, parent_irq, NULL);
1765 EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1767 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1770 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
1772 * @gc: the gpiochip to set the irqchip hierarchical handler to
1773 * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
1774 * will then percolate up to the parent
1776 static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
1777 struct irq_chip *irqchip)
1779 /* DT will deal with mapping each IRQ as we go along */
1780 if (is_of_node(gc->irq.fwnode))
1784 * This is for legacy and boardfile "irqchip" fwnodes: allocate
1785 * irqs upfront instead of dynamically since we don't have the
1786 * dynamic type of allocation that hardware description languages
1787 * provide. Once all GPIO drivers using board files are gone from
1788 * the kernel we can delete this code, but for a transitional period
1789 * it is necessary to keep this around.
1791 if (is_fwnode_irqchip(gc->irq.fwnode)) {
1795 for (i = 0; i < gc->ngpio; i++) {
1796 struct irq_fwspec fwspec;
1797 unsigned int parent_hwirq;
1798 unsigned int parent_type;
1799 struct gpio_irq_chip *girq = &gc->irq;
1802 * We call the child to parent translation function
1803 * only to check if the child IRQ is valid or not.
1804 * Just pick the rising edge type here as that is what
1805 * we likely need to support.
1807 ret = girq->child_to_parent_hwirq(gc, i,
1808 IRQ_TYPE_EDGE_RISING,
1812 chip_err(gc, "skip set-up on hwirq %d\n",
1817 fwspec.fwnode = gc->irq.fwnode;
1818 /* This is the hwirq for the GPIO line side of things */
1819 fwspec.param[0] = girq->child_offset_to_irq(gc, i);
1820 /* Just pick something */
1821 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
1822 fwspec.param_count = 2;
1823 ret = __irq_domain_alloc_irqs(gc->irq.domain,
1824 /* just pick something */
1833 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
1840 chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
1845 static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
1846 struct irq_fwspec *fwspec,
1847 unsigned long *hwirq,
1850 /* We support standard DT translation */
1851 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
1852 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
1855 /* This is for board files and others not using DT */
1856 if (is_fwnode_irqchip(fwspec->fwnode)) {
1859 ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
1862 WARN_ON(*type == IRQ_TYPE_NONE);
1868 static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
1870 unsigned int nr_irqs,
1873 struct gpio_chip *gc = d->host_data;
1874 irq_hw_number_t hwirq;
1875 unsigned int type = IRQ_TYPE_NONE;
1876 struct irq_fwspec *fwspec = data;
1877 struct irq_fwspec parent_fwspec;
1878 unsigned int parent_hwirq;
1879 unsigned int parent_type;
1880 struct gpio_irq_chip *girq = &gc->irq;
1884 * The nr_irqs parameter is always one except for PCI multi-MSI
1885 * so this should not happen.
1887 WARN_ON(nr_irqs != 1);
1889 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
1893 chip_info(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq);
1895 ret = girq->child_to_parent_hwirq(gc, hwirq, type,
1896 &parent_hwirq, &parent_type);
1898 chip_err(gc, "can't look up hwirq %lu\n", hwirq);
1901 chip_info(gc, "found parent hwirq %u\n", parent_hwirq);
1904 * We set handle_bad_irq because the .set_type() should
1905 * always be invoked and set the right type of handler.
1907 irq_domain_set_info(d,
1917 * Create a IRQ fwspec to send up to the parent irqdomain:
1918 * specify the hwirq we address on the parent and tie it
1919 * all together up the chain.
1921 parent_fwspec.fwnode = d->parent->fwnode;
1922 /* This parent only handles asserted level IRQs */
1923 girq->populate_parent_fwspec(gc, &parent_fwspec, parent_hwirq,
1925 chip_info(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
1927 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1928 ret = irq_domain_alloc_irqs_parent(d, irq, 1, &parent_fwspec);
1931 "failed to allocate parent hwirq %d for hwirq %lu\n",
1932 parent_hwirq, hwirq);
1937 static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *chip,
1938 unsigned int offset)
1943 static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
1945 ops->activate = gpiochip_irq_domain_activate;
1946 ops->deactivate = gpiochip_irq_domain_deactivate;
1947 ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
1948 ops->free = irq_domain_free_irqs_common;
1951 * We only allow overriding the translate() function for
1952 * hierarchical chips, and this should only be done if the user
1953 * really need something other than 1:1 translation.
1955 if (!ops->translate)
1956 ops->translate = gpiochip_hierarchy_irq_domain_translate;
1959 static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1961 if (!gc->irq.child_to_parent_hwirq ||
1963 chip_err(gc, "missing irqdomain vital data\n");
1967 if (!gc->irq.child_offset_to_irq)
1968 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
1970 if (!gc->irq.populate_parent_fwspec)
1971 gc->irq.populate_parent_fwspec =
1972 gpiochip_populate_parent_fwspec_twocell;
1974 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
1976 gc->irq.domain = irq_domain_create_hierarchy(
1977 gc->irq.parent_domain,
1981 &gc->irq.child_irq_domain_ops,
1984 if (!gc->irq.domain)
1987 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
1992 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1994 return !!gc->irq.parent_domain;
1997 void gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *chip,
1998 struct irq_fwspec *fwspec,
1999 unsigned int parent_hwirq,
2000 unsigned int parent_type)
2002 fwspec->param_count = 2;
2003 fwspec->param[0] = parent_hwirq;
2004 fwspec->param[1] = parent_type;
2006 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
2008 void gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *chip,
2009 struct irq_fwspec *fwspec,
2010 unsigned int parent_hwirq,
2011 unsigned int parent_type)
2013 fwspec->param_count = 4;
2014 fwspec->param[0] = 0;
2015 fwspec->param[1] = parent_hwirq;
2016 fwspec->param[2] = 0;
2017 fwspec->param[3] = parent_type;
2019 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
2023 static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
2028 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
2033 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
2036 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
2037 * @d: the irqdomain used by this irqchip
2038 * @irq: the global irq number used by this GPIO irqchip irq
2039 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
2041 * This function will set up the mapping for a certain IRQ line on a
2042 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
2043 * stored inside the gpiochip.
2045 int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
2046 irq_hw_number_t hwirq)
2048 struct gpio_chip *chip = d->host_data;
2051 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
2054 irq_set_chip_data(irq, chip);
2056 * This lock class tells lockdep that GPIO irqs are in a different
2057 * category than their parents, so it won't report false recursion.
2059 irq_set_lockdep_class(irq, chip->irq.lock_key, chip->irq.request_key);
2060 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
2061 /* Chips that use nested thread handlers have them marked */
2062 if (chip->irq.threaded)
2063 irq_set_nested_thread(irq, 1);
2064 irq_set_noprobe(irq);
2066 if (chip->irq.num_parents == 1)
2067 ret = irq_set_parent(irq, chip->irq.parents[0]);
2068 else if (chip->irq.map)
2069 ret = irq_set_parent(irq, chip->irq.map[hwirq]);
2075 * No set-up of the hardware will happen if IRQ_TYPE_NONE
2076 * is passed as default type.
2078 if (chip->irq.default_type != IRQ_TYPE_NONE)
2079 irq_set_irq_type(irq, chip->irq.default_type);
2083 EXPORT_SYMBOL_GPL(gpiochip_irq_map);
2085 void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
2087 struct gpio_chip *chip = d->host_data;
2089 if (chip->irq.threaded)
2090 irq_set_nested_thread(irq, 0);
2091 irq_set_chip_and_handler(irq, NULL, NULL);
2092 irq_set_chip_data(irq, NULL);
2094 EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
2096 static const struct irq_domain_ops gpiochip_domain_ops = {
2097 .map = gpiochip_irq_map,
2098 .unmap = gpiochip_irq_unmap,
2099 /* Virtually all GPIO irqchips are twocell:ed */
2100 .xlate = irq_domain_xlate_twocell,
2104 * TODO: move these activate/deactivate in under the hierarchicial
2105 * irqchip implementation as static once SPMI and SSBI (all external
2106 * users) are phased over.
2109 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
2110 * @domain: The IRQ domain used by this IRQ chip
2111 * @data: Outermost irq_data associated with the IRQ
2112 * @reserve: If set, only reserve an interrupt vector instead of assigning one
2114 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
2115 * used as the activate function for the &struct irq_domain_ops. The host_data
2116 * for the IRQ domain must be the &struct gpio_chip.
2118 int gpiochip_irq_domain_activate(struct irq_domain *domain,
2119 struct irq_data *data, bool reserve)
2121 struct gpio_chip *chip = domain->host_data;
2123 return gpiochip_lock_as_irq(chip, data->hwirq);
2125 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate);
2128 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
2129 * @domain: The IRQ domain used by this IRQ chip
2130 * @data: Outermost irq_data associated with the IRQ
2132 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
2133 * be used as the deactivate function for the &struct irq_domain_ops. The
2134 * host_data for the IRQ domain must be the &struct gpio_chip.
2136 void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
2137 struct irq_data *data)
2139 struct gpio_chip *chip = domain->host_data;
2141 return gpiochip_unlock_as_irq(chip, data->hwirq);
2143 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate);
2145 static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
2147 struct irq_domain *domain = chip->irq.domain;
2149 if (!gpiochip_irqchip_irq_valid(chip, offset))
2152 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2153 if (irq_domain_is_hierarchy(domain)) {
2154 struct irq_fwspec spec;
2156 spec.fwnode = domain->fwnode;
2157 spec.param_count = 2;
2158 spec.param[0] = chip->irq.child_offset_to_irq(chip, offset);
2159 spec.param[1] = IRQ_TYPE_NONE;
2161 return irq_create_fwspec_mapping(&spec);
2165 return irq_create_mapping(domain, offset);
2168 static int gpiochip_irq_reqres(struct irq_data *d)
2170 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2172 return gpiochip_reqres_irq(chip, d->hwirq);
2175 static void gpiochip_irq_relres(struct irq_data *d)
2177 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2179 gpiochip_relres_irq(chip, d->hwirq);
2182 static void gpiochip_irq_enable(struct irq_data *d)
2184 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2186 gpiochip_enable_irq(chip, d->hwirq);
2187 if (chip->irq.irq_enable)
2188 chip->irq.irq_enable(d);
2190 chip->irq.chip->irq_unmask(d);
2193 static void gpiochip_irq_disable(struct irq_data *d)
2195 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2198 * Since we override .irq_disable() we need to mimic the
2199 * behaviour of __irq_disable() in irq/chip.c.
2200 * First call .irq_disable() if it exists, else mimic the
2201 * behaviour of mask_irq() which calls .irq_mask() if
2204 if (chip->irq.irq_disable)
2205 chip->irq.irq_disable(d);
2206 else if (chip->irq.chip->irq_mask)
2207 chip->irq.chip->irq_mask(d);
2208 gpiochip_disable_irq(chip, d->hwirq);
2211 static void gpiochip_set_irq_hooks(struct gpio_chip *gpiochip)
2213 struct irq_chip *irqchip = gpiochip->irq.chip;
2215 if (!irqchip->irq_request_resources &&
2216 !irqchip->irq_release_resources) {
2217 irqchip->irq_request_resources = gpiochip_irq_reqres;
2218 irqchip->irq_release_resources = gpiochip_irq_relres;
2220 if (WARN_ON(gpiochip->irq.irq_enable))
2222 /* Check if the irqchip already has this hook... */
2223 if (irqchip->irq_enable == gpiochip_irq_enable) {
2225 * ...and if so, give a gentle warning that this is bad
2229 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
2232 gpiochip->irq.irq_enable = irqchip->irq_enable;
2233 gpiochip->irq.irq_disable = irqchip->irq_disable;
2234 irqchip->irq_enable = gpiochip_irq_enable;
2235 irqchip->irq_disable = gpiochip_irq_disable;
2239 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
2240 * @gpiochip: the GPIO chip to add the IRQ chip to
2241 * @lock_key: lockdep class for IRQ lock
2242 * @request_key: lockdep class for IRQ request
2244 static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
2245 struct lock_class_key *lock_key,
2246 struct lock_class_key *request_key)
2248 struct irq_chip *irqchip = gpiochip->irq.chip;
2249 const struct irq_domain_ops *ops = NULL;
2250 struct device_node *np;
2257 if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
2258 chip_err(gpiochip, "you cannot have chained interrupts on a chip that may sleep\n");
2262 np = gpiochip->gpiodev->dev.of_node;
2263 type = gpiochip->irq.default_type;
2266 * Specifying a default trigger is a terrible idea if DT or ACPI is
2267 * used to configure the interrupts, as you may end up with
2268 * conflicting triggers. Tell the user, and reset to NONE.
2270 if (WARN(np && type != IRQ_TYPE_NONE,
2271 "%s: Ignoring %u default trigger\n", np->full_name, type))
2272 type = IRQ_TYPE_NONE;
2274 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
2275 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
2276 "Ignoring %u default trigger\n", type);
2277 type = IRQ_TYPE_NONE;
2280 gpiochip->to_irq = gpiochip_to_irq;
2281 gpiochip->irq.default_type = type;
2282 gpiochip->irq.lock_key = lock_key;
2283 gpiochip->irq.request_key = request_key;
2285 /* If a parent irqdomain is provided, let's build a hierarchy */
2286 if (gpiochip_hierarchy_is_hierarchical(gpiochip)) {
2287 int ret = gpiochip_hierarchy_add_domain(gpiochip);
2291 /* Some drivers provide custom irqdomain ops */
2292 if (gpiochip->irq.domain_ops)
2293 ops = gpiochip->irq.domain_ops;
2296 ops = &gpiochip_domain_ops;
2297 gpiochip->irq.domain = irq_domain_add_simple(np,
2299 gpiochip->irq.first,
2301 if (!gpiochip->irq.domain)
2305 if (gpiochip->irq.parent_handler) {
2306 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
2308 for (i = 0; i < gpiochip->irq.num_parents; i++) {
2310 * The parent IRQ chip is already using the chip_data
2311 * for this IRQ chip, so our callbacks simply use the
2314 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
2315 gpiochip->irq.parent_handler,
2320 gpiochip_set_irq_hooks(gpiochip);
2322 acpi_gpiochip_request_interrupts(gpiochip);
2328 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
2329 * @gpiochip: the gpiochip to remove the irqchip from
2331 * This is called only from gpiochip_remove()
2333 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
2335 struct irq_chip *irqchip = gpiochip->irq.chip;
2336 unsigned int offset;
2338 acpi_gpiochip_free_interrupts(gpiochip);
2340 if (irqchip && gpiochip->irq.parent_handler) {
2341 struct gpio_irq_chip *irq = &gpiochip->irq;
2344 for (i = 0; i < irq->num_parents; i++)
2345 irq_set_chained_handler_and_data(irq->parents[i],
2349 /* Remove all IRQ mappings and delete the domain */
2350 if (gpiochip->irq.domain) {
2353 for (offset = 0; offset < gpiochip->ngpio; offset++) {
2354 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
2357 irq = irq_find_mapping(gpiochip->irq.domain, offset);
2358 irq_dispose_mapping(irq);
2361 irq_domain_remove(gpiochip->irq.domain);
2365 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
2366 irqchip->irq_request_resources = NULL;
2367 irqchip->irq_release_resources = NULL;
2369 if (irqchip->irq_enable == gpiochip_irq_enable) {
2370 irqchip->irq_enable = gpiochip->irq.irq_enable;
2371 irqchip->irq_disable = gpiochip->irq.irq_disable;
2374 gpiochip->irq.irq_enable = NULL;
2375 gpiochip->irq.irq_disable = NULL;
2376 gpiochip->irq.chip = NULL;
2378 gpiochip_irqchip_free_valid_mask(gpiochip);
2382 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
2383 * @gpiochip: the gpiochip to add the irqchip to
2384 * @irqchip: the irqchip to add to the gpiochip
2385 * @first_irq: if not dynamically assigned, the base (first) IRQ to
2386 * allocate gpiochip irqs from
2387 * @handler: the irq handler to use (often a predefined irq core function)
2388 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
2389 * to have the core avoid setting up any default type in the hardware.
2390 * @threaded: whether this irqchip uses a nested thread handler
2391 * @lock_key: lockdep class for IRQ lock
2392 * @request_key: lockdep class for IRQ request
2394 * This function closely associates a certain irqchip with a certain
2395 * gpiochip, providing an irq domain to translate the local IRQs to
2396 * global irqs in the gpiolib core, and making sure that the gpiochip
2397 * is passed as chip data to all related functions. Driver callbacks
2398 * need to use gpiochip_get_data() to get their local state containers back
2399 * from the gpiochip passed as chip data. An irqdomain will be stored
2400 * in the gpiochip that shall be used by the driver to handle IRQ number
2401 * translation. The gpiochip will need to be initialized and registered
2402 * before calling this function.
2404 * This function will handle two cell:ed simple IRQs and assumes all
2405 * the pins on the gpiochip can generate a unique IRQ. Everything else
2406 * need to be open coded.
2408 int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
2409 struct irq_chip *irqchip,
2410 unsigned int first_irq,
2411 irq_flow_handler_t handler,
2414 struct lock_class_key *lock_key,
2415 struct lock_class_key *request_key)
2417 struct device_node *of_node;
2419 if (!gpiochip || !irqchip)
2422 if (!gpiochip->parent) {
2423 pr_err("missing gpiochip .dev parent pointer\n");
2426 gpiochip->irq.threaded = threaded;
2427 of_node = gpiochip->parent->of_node;
2428 #ifdef CONFIG_OF_GPIO
2430 * If the gpiochip has an assigned OF node this takes precedence
2431 * FIXME: get rid of this and use gpiochip->parent->of_node
2434 if (gpiochip->of_node)
2435 of_node = gpiochip->of_node;
2438 * Specifying a default trigger is a terrible idea if DT or ACPI is
2439 * used to configure the interrupts, as you may end-up with
2440 * conflicting triggers. Tell the user, and reset to NONE.
2442 if (WARN(of_node && type != IRQ_TYPE_NONE,
2443 "%pOF: Ignoring %d default trigger\n", of_node, type))
2444 type = IRQ_TYPE_NONE;
2445 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
2446 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
2447 "Ignoring %d default trigger\n", type);
2448 type = IRQ_TYPE_NONE;
2451 gpiochip->irq.chip = irqchip;
2452 gpiochip->irq.handler = handler;
2453 gpiochip->irq.default_type = type;
2454 gpiochip->to_irq = gpiochip_to_irq;
2455 gpiochip->irq.lock_key = lock_key;
2456 gpiochip->irq.request_key = request_key;
2457 gpiochip->irq.domain = irq_domain_add_simple(of_node,
2458 gpiochip->ngpio, first_irq,
2459 &gpiochip_domain_ops, gpiochip);
2460 if (!gpiochip->irq.domain) {
2461 gpiochip->irq.chip = NULL;
2465 gpiochip_set_irq_hooks(gpiochip);
2467 acpi_gpiochip_request_interrupts(gpiochip);
2471 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
2473 #else /* CONFIG_GPIOLIB_IRQCHIP */
2475 static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
2476 struct lock_class_key *lock_key,
2477 struct lock_class_key *request_key)
2481 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
2483 static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gpiochip)
2488 static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
2492 static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
2495 #endif /* CONFIG_GPIOLIB_IRQCHIP */
2498 * gpiochip_generic_request() - request the gpio function for a pin
2499 * @chip: the gpiochip owning the GPIO
2500 * @offset: the offset of the GPIO to request for GPIO function
2502 int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
2504 return pinctrl_gpio_request(chip->gpiodev->base + offset);
2506 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
2509 * gpiochip_generic_free() - free the gpio function from a pin
2510 * @chip: the gpiochip to request the gpio function for
2511 * @offset: the offset of the GPIO to free from GPIO function
2513 void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
2515 pinctrl_gpio_free(chip->gpiodev->base + offset);
2517 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
2520 * gpiochip_generic_config() - apply configuration for a pin
2521 * @chip: the gpiochip owning the GPIO
2522 * @offset: the offset of the GPIO to apply the configuration
2523 * @config: the configuration to be applied
2525 int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
2526 unsigned long config)
2528 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
2530 EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2532 #ifdef CONFIG_PINCTRL
2535 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2536 * @chip: the gpiochip to add the range for
2537 * @pctldev: the pin controller to map to
2538 * @gpio_offset: the start offset in the current gpio_chip number space
2539 * @pin_group: name of the pin group inside the pin controller
2541 * Calling this function directly from a DeviceTree-supported
2542 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2543 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2544 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
2546 int gpiochip_add_pingroup_range(struct gpio_chip *chip,
2547 struct pinctrl_dev *pctldev,
2548 unsigned int gpio_offset, const char *pin_group)
2550 struct gpio_pin_range *pin_range;
2551 struct gpio_device *gdev = chip->gpiodev;
2554 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2556 chip_err(chip, "failed to allocate pin ranges\n");
2560 /* Use local offset as range ID */
2561 pin_range->range.id = gpio_offset;
2562 pin_range->range.gc = chip;
2563 pin_range->range.name = chip->label;
2564 pin_range->range.base = gdev->base + gpio_offset;
2565 pin_range->pctldev = pctldev;
2567 ret = pinctrl_get_group_pins(pctldev, pin_group,
2568 &pin_range->range.pins,
2569 &pin_range->range.npins);
2575 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2577 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2578 gpio_offset, gpio_offset + pin_range->range.npins - 1,
2579 pinctrl_dev_get_devname(pctldev), pin_group);
2581 list_add_tail(&pin_range->node, &gdev->pin_ranges);
2585 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2588 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2589 * @chip: the gpiochip to add the range for
2590 * @pinctl_name: the dev_name() of the pin controller to map to
2591 * @gpio_offset: the start offset in the current gpio_chip number space
2592 * @pin_offset: the start offset in the pin controller number space
2593 * @npins: the number of pins from the offset of each pin space (GPIO and
2594 * pin controller) to accumulate in this range
2597 * 0 on success, or a negative error-code on failure.
2599 * Calling this function directly from a DeviceTree-supported
2600 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2601 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2602 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
2604 int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
2605 unsigned int gpio_offset, unsigned int pin_offset,
2608 struct gpio_pin_range *pin_range;
2609 struct gpio_device *gdev = chip->gpiodev;
2612 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2614 chip_err(chip, "failed to allocate pin ranges\n");
2618 /* Use local offset as range ID */
2619 pin_range->range.id = gpio_offset;
2620 pin_range->range.gc = chip;
2621 pin_range->range.name = chip->label;
2622 pin_range->range.base = gdev->base + gpio_offset;
2623 pin_range->range.pin_base = pin_offset;
2624 pin_range->range.npins = npins;
2625 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
2627 if (IS_ERR(pin_range->pctldev)) {
2628 ret = PTR_ERR(pin_range->pctldev);
2629 chip_err(chip, "could not create pin range\n");
2633 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2634 gpio_offset, gpio_offset + npins - 1,
2636 pin_offset, pin_offset + npins - 1);
2638 list_add_tail(&pin_range->node, &gdev->pin_ranges);
2642 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
2645 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2646 * @chip: the chip to remove all the mappings for
2648 void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
2650 struct gpio_pin_range *pin_range, *tmp;
2651 struct gpio_device *gdev = chip->gpiodev;
2653 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
2654 list_del(&pin_range->node);
2655 pinctrl_remove_gpio_range(pin_range->pctldev,
2660 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2662 #endif /* CONFIG_PINCTRL */
2664 /* These "optional" allocation calls help prevent drivers from stomping
2665 * on each other, and help provide better diagnostics in debugfs.
2666 * They're called even less than the "set direction" calls.
2668 static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
2670 struct gpio_chip *chip = desc->gdev->chip;
2672 unsigned long flags;
2676 label = kstrdup_const(label, GFP_KERNEL);
2681 spin_lock_irqsave(&gpio_lock, flags);
2683 /* NOTE: gpio_request() can be called in early boot,
2684 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
2687 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2688 desc_set_label(desc, label ? : "?");
2696 if (chip->request) {
2697 /* chip->request may sleep */
2698 spin_unlock_irqrestore(&gpio_lock, flags);
2699 offset = gpio_chip_hwgpio(desc);
2700 if (gpiochip_line_is_valid(chip, offset))
2701 ret = chip->request(chip, offset);
2704 spin_lock_irqsave(&gpio_lock, flags);
2707 desc_set_label(desc, NULL);
2709 clear_bit(FLAG_REQUESTED, &desc->flags);
2713 if (chip->get_direction) {
2714 /* chip->get_direction may sleep */
2715 spin_unlock_irqrestore(&gpio_lock, flags);
2716 gpiod_get_direction(desc);
2717 spin_lock_irqsave(&gpio_lock, flags);
2720 spin_unlock_irqrestore(&gpio_lock, flags);
2725 * This descriptor validation needs to be inserted verbatim into each
2726 * function taking a descriptor, so we need to use a preprocessor
2727 * macro to avoid endless duplication. If the desc is NULL it is an
2728 * optional GPIO and calls should just bail out.
2730 static int validate_desc(const struct gpio_desc *desc, const char *func)
2735 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2736 return PTR_ERR(desc);
2739 pr_warn("%s: invalid GPIO (no device)\n", func);
2742 if (!desc->gdev->chip) {
2743 dev_warn(&desc->gdev->dev,
2744 "%s: backing chip is gone\n", func);
2750 #define VALIDATE_DESC(desc) do { \
2751 int __valid = validate_desc(desc, __func__); \
2756 #define VALIDATE_DESC_VOID(desc) do { \
2757 int __valid = validate_desc(desc, __func__); \
2762 int gpiod_request(struct gpio_desc *desc, const char *label)
2764 int ret = -EPROBE_DEFER;
2765 struct gpio_device *gdev;
2767 VALIDATE_DESC(desc);
2770 if (try_module_get(gdev->owner)) {
2771 ret = gpiod_request_commit(desc, label);
2773 module_put(gdev->owner);
2775 get_device(&gdev->dev);
2779 gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
2784 static bool gpiod_free_commit(struct gpio_desc *desc)
2787 unsigned long flags;
2788 struct gpio_chip *chip;
2792 gpiod_unexport(desc);
2794 spin_lock_irqsave(&gpio_lock, flags);
2796 chip = desc->gdev->chip;
2797 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2799 spin_unlock_irqrestore(&gpio_lock, flags);
2800 might_sleep_if(chip->can_sleep);
2801 chip->free(chip, gpio_chip_hwgpio(desc));
2802 spin_lock_irqsave(&gpio_lock, flags);
2804 kfree_const(desc->label);
2805 desc_set_label(desc, NULL);
2806 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
2807 clear_bit(FLAG_REQUESTED, &desc->flags);
2808 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
2809 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
2810 clear_bit(FLAG_IS_HOGGED, &desc->flags);
2814 spin_unlock_irqrestore(&gpio_lock, flags);
2818 void gpiod_free(struct gpio_desc *desc)
2820 if (desc && desc->gdev && gpiod_free_commit(desc)) {
2821 module_put(desc->gdev->owner);
2822 put_device(&desc->gdev->dev);
2824 WARN_ON(extra_checks);
2829 * gpiochip_is_requested - return string iff signal was requested
2830 * @chip: controller managing the signal
2831 * @offset: of signal within controller's 0..(ngpio - 1) range
2833 * Returns NULL if the GPIO is not currently requested, else a string.
2834 * The string returned is the label passed to gpio_request(); if none has been
2835 * passed it is a meaningless, non-NULL constant.
2837 * This function is for use by GPIO controller drivers. The label can
2838 * help with diagnostics, and knowing that the signal is used as a GPIO
2839 * can help avoid accidentally multiplexing it to another controller.
2841 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2843 struct gpio_desc *desc;
2845 if (offset >= chip->ngpio)
2848 desc = &chip->gpiodev->descs[offset];
2850 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
2854 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2857 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2859 * @hwnum: hardware number of the GPIO for which to request the descriptor
2860 * @label: label for the GPIO
2861 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2862 * specify things like line inversion semantics with the machine flags
2863 * such as GPIO_OUT_LOW
2864 * @dflags: descriptor request flags for this GPIO or 0 if default, this
2865 * can be used to specify consumer semantics such as open drain
2867 * Function allows GPIO chip drivers to request and use their own GPIO
2868 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2869 * function will not increase reference count of the GPIO chip module. This
2870 * allows the GPIO chip module to be unloaded as needed (we assume that the
2871 * GPIO chip driver handles freeing the GPIOs it has requested).
2874 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2877 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2879 enum gpio_lookup_flags lflags,
2880 enum gpiod_flags dflags)
2882 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2886 chip_err(chip, "failed to get GPIO descriptor\n");
2890 ret = gpiod_request_commit(desc, label);
2892 return ERR_PTR(ret);
2894 ret = gpiod_configure_flags(desc, label, lflags, dflags);
2896 chip_err(chip, "setup of own GPIO %s failed\n", label);
2897 gpiod_free_commit(desc);
2898 return ERR_PTR(ret);
2903 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
2906 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2907 * @desc: GPIO descriptor to free
2909 * Function frees the given GPIO requested previously with
2910 * gpiochip_request_own_desc().
2912 void gpiochip_free_own_desc(struct gpio_desc *desc)
2915 gpiod_free_commit(desc);
2917 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
2920 * Drivers MUST set GPIO direction before making get/set calls. In
2921 * some cases this is done in early boot, before IRQs are enabled.
2923 * As a rule these aren't called more than once (except for drivers
2924 * using the open-drain emulation idiom) so these are natural places
2925 * to accumulate extra debugging checks. Note that we can't (yet)
2926 * rely on gpio_request() having been called beforehand.
2929 static int gpio_set_config(struct gpio_chip *gc, unsigned offset,
2930 enum pin_config_param mode)
2932 unsigned long config;
2936 case PIN_CONFIG_BIAS_PULL_DOWN:
2937 case PIN_CONFIG_BIAS_PULL_UP:
2945 config = PIN_CONF_PACKED(mode, arg);
2946 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2950 * gpiod_direction_input - set the GPIO direction to input
2951 * @desc: GPIO to set to input
2953 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2954 * be called safely on it.
2956 * Return 0 in case of success, else an error code.
2958 int gpiod_direction_input(struct gpio_desc *desc)
2960 struct gpio_chip *chip;
2963 VALIDATE_DESC(desc);
2964 chip = desc->gdev->chip;
2967 * It is legal to have no .get() and .direction_input() specified if
2968 * the chip is output-only, but you can't specify .direction_input()
2969 * and not support the .get() operation, that doesn't make sense.
2971 if (!chip->get && chip->direction_input) {
2973 "%s: missing get() but have direction_input()\n",
2979 * If we have a .direction_input() callback, things are simple,
2980 * just call it. Else we are some input-only chip so try to check the
2981 * direction (if .get_direction() is supported) else we silently
2982 * assume we are in input mode after this.
2984 if (chip->direction_input) {
2985 ret = chip->direction_input(chip, gpio_chip_hwgpio(desc));
2986 } else if (chip->get_direction &&
2987 (chip->get_direction(chip, gpio_chip_hwgpio(desc)) != 1)) {
2989 "%s: missing direction_input() operation and line is output\n",
2994 clear_bit(FLAG_IS_OUT, &desc->flags);
2996 if (test_bit(FLAG_PULL_UP, &desc->flags))
2997 gpio_set_config(chip, gpio_chip_hwgpio(desc),
2998 PIN_CONFIG_BIAS_PULL_UP);
2999 else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
3000 gpio_set_config(chip, gpio_chip_hwgpio(desc),
3001 PIN_CONFIG_BIAS_PULL_DOWN);
3003 trace_gpio_direction(desc_to_gpio(desc), 1, ret);
3007 EXPORT_SYMBOL_GPL(gpiod_direction_input);
3009 static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
3011 struct gpio_chip *gc = desc->gdev->chip;
3016 * It's OK not to specify .direction_output() if the gpiochip is
3017 * output-only, but if there is then not even a .set() operation it
3018 * is pretty tricky to drive the output line.
3020 if (!gc->set && !gc->direction_output) {
3022 "%s: missing set() and direction_output() operations\n",
3027 if (gc->direction_output) {
3028 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
3030 /* Check that we are in output mode if we can */
3031 if (gc->get_direction &&
3032 gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
3034 "%s: missing direction_output() operation\n",
3039 * If we can't actively set the direction, we are some
3040 * output-only chip, so just drive the output as desired.
3042 gc->set(gc, gpio_chip_hwgpio(desc), val);
3046 set_bit(FLAG_IS_OUT, &desc->flags);
3047 trace_gpio_value(desc_to_gpio(desc), 0, val);
3048 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
3053 * gpiod_direction_output_raw - set the GPIO direction to output
3054 * @desc: GPIO to set to output
3055 * @value: initial output value of the GPIO
3057 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
3058 * be called safely on it. The initial value of the output must be specified
3059 * as raw value on the physical line without regard for the ACTIVE_LOW status.
3061 * Return 0 in case of success, else an error code.
3063 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
3065 VALIDATE_DESC(desc);
3066 return gpiod_direction_output_raw_commit(desc, value);
3068 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
3071 * gpiod_direction_output - set the GPIO direction to output
3072 * @desc: GPIO to set to output
3073 * @value: initial output value of the GPIO
3075 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
3076 * be called safely on it. The initial value of the output must be specified
3077 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3080 * Return 0 in case of success, else an error code.
3082 int gpiod_direction_output(struct gpio_desc *desc, int value)
3084 struct gpio_chip *gc;
3087 VALIDATE_DESC(desc);
3088 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3093 /* GPIOs used for enabled IRQs shall not be set as output */
3094 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
3095 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
3097 "%s: tried to set a GPIO tied to an IRQ as output\n",
3102 gc = desc->gdev->chip;
3103 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
3104 /* First see if we can enable open drain in hardware */
3105 ret = gpio_set_config(gc, gpio_chip_hwgpio(desc),
3106 PIN_CONFIG_DRIVE_OPEN_DRAIN);
3108 goto set_output_value;
3109 /* Emulate open drain by not actively driving the line high */
3111 ret = gpiod_direction_input(desc);
3112 goto set_output_flag;
3115 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
3116 ret = gpio_set_config(gc, gpio_chip_hwgpio(desc),
3117 PIN_CONFIG_DRIVE_OPEN_SOURCE);
3119 goto set_output_value;
3120 /* Emulate open source by not actively driving the line low */
3122 ret = gpiod_direction_input(desc);
3123 goto set_output_flag;
3126 gpio_set_config(gc, gpio_chip_hwgpio(desc),
3127 PIN_CONFIG_DRIVE_PUSH_PULL);
3131 return gpiod_direction_output_raw_commit(desc, value);
3135 * When emulating open-source or open-drain functionalities by not
3136 * actively driving the line (setting mode to input) we still need to
3137 * set the IS_OUT flag or otherwise we won't be able to set the line
3141 set_bit(FLAG_IS_OUT, &desc->flags);
3144 EXPORT_SYMBOL_GPL(gpiod_direction_output);
3147 * gpiod_set_debounce - sets @debounce time for a GPIO
3148 * @desc: descriptor of the GPIO for which to set debounce time
3149 * @debounce: debounce time in microseconds
3152 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
3155 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
3157 struct gpio_chip *chip;
3158 unsigned long config;
3160 VALIDATE_DESC(desc);
3161 chip = desc->gdev->chip;
3162 if (!chip->set || !chip->set_config) {
3164 "%s: missing set() or set_config() operations\n",
3169 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
3170 return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
3172 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
3175 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
3176 * @desc: descriptor of the GPIO for which to configure persistence
3177 * @transitory: True to lose state on suspend or reset, false for persistence
3180 * 0 on success, otherwise a negative error code.
3182 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
3184 struct gpio_chip *chip;
3185 unsigned long packed;
3189 VALIDATE_DESC(desc);
3191 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
3192 * persistence state.
3195 set_bit(FLAG_TRANSITORY, &desc->flags);
3197 clear_bit(FLAG_TRANSITORY, &desc->flags);
3199 /* If the driver supports it, set the persistence state now */
3200 chip = desc->gdev->chip;
3201 if (!chip->set_config)
3204 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
3206 gpio = gpio_chip_hwgpio(desc);
3207 rc = chip->set_config(chip, gpio, packed);
3208 if (rc == -ENOTSUPP) {
3209 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
3216 EXPORT_SYMBOL_GPL(gpiod_set_transitory);
3219 * gpiod_is_active_low - test whether a GPIO is active-low or not
3220 * @desc: the gpio descriptor to test
3222 * Returns 1 if the GPIO is active-low, 0 otherwise.
3224 int gpiod_is_active_low(const struct gpio_desc *desc)
3226 VALIDATE_DESC(desc);
3227 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
3229 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
3232 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
3233 * @desc: the gpio descriptor to change
3235 void gpiod_toggle_active_low(struct gpio_desc *desc)
3237 VALIDATE_DESC_VOID(desc);
3238 change_bit(FLAG_ACTIVE_LOW, &desc->flags);
3240 EXPORT_SYMBOL_GPL(gpiod_toggle_active_low);
3242 /* I/O calls are only valid after configuration completed; the relevant
3243 * "is this a valid GPIO" error checks should already have been done.
3245 * "Get" operations are often inlinable as reading a pin value register,
3246 * and masking the relevant bit in that register.
3248 * When "set" operations are inlinable, they involve writing that mask to
3249 * one register to set a low value, or a different register to set it high.
3250 * Otherwise locking is needed, so there may be little value to inlining.
3252 *------------------------------------------------------------------------
3254 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
3255 * have requested the GPIO. That can include implicit requesting by
3256 * a direction setting call. Marking a gpio as requested locks its chip
3257 * in memory, guaranteeing that these table lookups need no more locking
3258 * and that gpiochip_remove() will fail.
3260 * REVISIT when debugging, consider adding some instrumentation to ensure
3261 * that the GPIO was actually requested.
3264 static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
3266 struct gpio_chip *chip;
3270 chip = desc->gdev->chip;
3271 offset = gpio_chip_hwgpio(desc);
3272 value = chip->get ? chip->get(chip, offset) : -EIO;
3273 value = value < 0 ? value : !!value;
3274 trace_gpio_value(desc_to_gpio(desc), 1, value);
3278 static int gpio_chip_get_multiple(struct gpio_chip *chip,
3279 unsigned long *mask, unsigned long *bits)
3281 if (chip->get_multiple) {
3282 return chip->get_multiple(chip, mask, bits);
3283 } else if (chip->get) {
3286 for_each_set_bit(i, mask, chip->ngpio) {
3287 value = chip->get(chip, i);
3290 __assign_bit(i, bits, value);
3297 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
3298 unsigned int array_size,
3299 struct gpio_desc **desc_array,
3300 struct gpio_array *array_info,
3301 unsigned long *value_bitmap)
3306 * Validate array_info against desc_array and its size.
3307 * It should immediately follow desc_array if both
3308 * have been obtained from the same gpiod_get_array() call.
3310 if (array_info && array_info->desc == desc_array &&
3311 array_size <= array_info->size &&
3312 (void *)array_info == desc_array + array_info->size) {
3314 WARN_ON(array_info->chip->can_sleep);
3316 ret = gpio_chip_get_multiple(array_info->chip,
3317 array_info->get_mask,
3322 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3323 bitmap_xor(value_bitmap, value_bitmap,
3324 array_info->invert_mask, array_size);
3326 if (bitmap_full(array_info->get_mask, array_size))
3329 i = find_first_zero_bit(array_info->get_mask, array_size);
3334 while (i < array_size) {
3335 struct gpio_chip *chip = desc_array[i]->gdev->chip;
3336 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
3337 unsigned long *mask, *bits;
3340 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
3343 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
3345 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
3350 bits = mask + BITS_TO_LONGS(chip->ngpio);
3351 bitmap_zero(mask, chip->ngpio);
3354 WARN_ON(chip->can_sleep);
3356 /* collect all inputs belonging to the same chip */
3359 const struct gpio_desc *desc = desc_array[i];
3360 int hwgpio = gpio_chip_hwgpio(desc);
3362 __set_bit(hwgpio, mask);
3366 i = find_next_zero_bit(array_info->get_mask,
3368 } while ((i < array_size) &&
3369 (desc_array[i]->gdev->chip == chip));
3371 ret = gpio_chip_get_multiple(chip, mask, bits);
3373 if (mask != fastpath)
3378 for (j = first; j < i; ) {
3379 const struct gpio_desc *desc = desc_array[j];
3380 int hwgpio = gpio_chip_hwgpio(desc);
3381 int value = test_bit(hwgpio, bits);
3383 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3385 __assign_bit(j, value_bitmap, value);
3386 trace_gpio_value(desc_to_gpio(desc), 1, value);
3390 j = find_next_zero_bit(array_info->get_mask, i,
3394 if (mask != fastpath)
3401 * gpiod_get_raw_value() - return a gpio's raw value
3402 * @desc: gpio whose value will be returned
3404 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
3405 * its ACTIVE_LOW status, or negative errno on failure.
3407 * This function can be called from contexts where we cannot sleep, and will
3408 * complain if the GPIO chip functions potentially sleep.
3410 int gpiod_get_raw_value(const struct gpio_desc *desc)
3412 VALIDATE_DESC(desc);
3413 /* Should be using gpiod_get_raw_value_cansleep() */
3414 WARN_ON(desc->gdev->chip->can_sleep);
3415 return gpiod_get_raw_value_commit(desc);
3417 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
3420 * gpiod_get_value() - return a gpio's value
3421 * @desc: gpio whose value will be returned
3423 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3424 * account, or negative errno on failure.
3426 * This function can be called from contexts where we cannot sleep, and will
3427 * complain if the GPIO chip functions potentially sleep.
3429 int gpiod_get_value(const struct gpio_desc *desc)
3433 VALIDATE_DESC(desc);
3434 /* Should be using gpiod_get_value_cansleep() */
3435 WARN_ON(desc->gdev->chip->can_sleep);
3437 value = gpiod_get_raw_value_commit(desc);
3441 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3446 EXPORT_SYMBOL_GPL(gpiod_get_value);
3449 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
3450 * @array_size: number of elements in the descriptor array / value bitmap
3451 * @desc_array: array of GPIO descriptors whose values will be read
3452 * @array_info: information on applicability of fast bitmap processing path
3453 * @value_bitmap: bitmap to store the read values
3455 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3456 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3457 * else an error code.
3459 * This function can be called from contexts where we cannot sleep,
3460 * and it will complain if the GPIO chip functions potentially sleep.
3462 int gpiod_get_raw_array_value(unsigned int array_size,
3463 struct gpio_desc **desc_array,
3464 struct gpio_array *array_info,
3465 unsigned long *value_bitmap)
3469 return gpiod_get_array_value_complex(true, false, array_size,
3470 desc_array, array_info,
3473 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
3476 * gpiod_get_array_value() - read values from an array of GPIOs
3477 * @array_size: number of elements in the descriptor array / value bitmap
3478 * @desc_array: array of GPIO descriptors whose values will be read
3479 * @array_info: information on applicability of fast bitmap processing path
3480 * @value_bitmap: bitmap to store the read values
3482 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3483 * into account. Return 0 in case of success, else an error code.
3485 * This function can be called from contexts where we cannot sleep,
3486 * and it will complain if the GPIO chip functions potentially sleep.
3488 int gpiod_get_array_value(unsigned int array_size,
3489 struct gpio_desc **desc_array,
3490 struct gpio_array *array_info,
3491 unsigned long *value_bitmap)
3495 return gpiod_get_array_value_complex(false, false, array_size,
3496 desc_array, array_info,
3499 EXPORT_SYMBOL_GPL(gpiod_get_array_value);
3502 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
3503 * @desc: gpio descriptor whose state need to be set.
3504 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3506 static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
3509 struct gpio_chip *chip = desc->gdev->chip;
3510 int offset = gpio_chip_hwgpio(desc);
3513 ret = chip->direction_input(chip, offset);
3515 ret = chip->direction_output(chip, offset, 0);
3517 set_bit(FLAG_IS_OUT, &desc->flags);
3519 trace_gpio_direction(desc_to_gpio(desc), value, ret);
3522 "%s: Error in set_value for open drain err %d\n",
3527 * _gpio_set_open_source_value() - Set the open source gpio's value.
3528 * @desc: gpio descriptor whose state need to be set.
3529 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3531 static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
3534 struct gpio_chip *chip = desc->gdev->chip;
3535 int offset = gpio_chip_hwgpio(desc);
3538 ret = chip->direction_output(chip, offset, 1);
3540 set_bit(FLAG_IS_OUT, &desc->flags);
3542 ret = chip->direction_input(chip, offset);
3544 trace_gpio_direction(desc_to_gpio(desc), !value, ret);
3547 "%s: Error in set_value for open source err %d\n",
3551 static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
3553 struct gpio_chip *chip;
3555 chip = desc->gdev->chip;
3556 trace_gpio_value(desc_to_gpio(desc), 0, value);
3557 chip->set(chip, gpio_chip_hwgpio(desc), value);
3561 * set multiple outputs on the same chip;
3562 * use the chip's set_multiple function if available;
3563 * otherwise set the outputs sequentially;
3564 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
3565 * defines which outputs are to be changed
3566 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
3567 * defines the values the outputs specified by mask are to be set to
3569 static void gpio_chip_set_multiple(struct gpio_chip *chip,
3570 unsigned long *mask, unsigned long *bits)
3572 if (chip->set_multiple) {
3573 chip->set_multiple(chip, mask, bits);
3577 /* set outputs if the corresponding mask bit is set */
3578 for_each_set_bit(i, mask, chip->ngpio)
3579 chip->set(chip, i, test_bit(i, bits));
3583 int gpiod_set_array_value_complex(bool raw, bool can_sleep,
3584 unsigned int array_size,
3585 struct gpio_desc **desc_array,
3586 struct gpio_array *array_info,
3587 unsigned long *value_bitmap)
3592 * Validate array_info against desc_array and its size.
3593 * It should immediately follow desc_array if both
3594 * have been obtained from the same gpiod_get_array() call.
3596 if (array_info && array_info->desc == desc_array &&
3597 array_size <= array_info->size &&
3598 (void *)array_info == desc_array + array_info->size) {
3600 WARN_ON(array_info->chip->can_sleep);
3602 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3603 bitmap_xor(value_bitmap, value_bitmap,
3604 array_info->invert_mask, array_size);
3606 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
3609 if (bitmap_full(array_info->set_mask, array_size))
3612 i = find_first_zero_bit(array_info->set_mask, array_size);
3617 while (i < array_size) {
3618 struct gpio_chip *chip = desc_array[i]->gdev->chip;
3619 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
3620 unsigned long *mask, *bits;
3623 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
3626 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
3628 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
3633 bits = mask + BITS_TO_LONGS(chip->ngpio);
3634 bitmap_zero(mask, chip->ngpio);
3637 WARN_ON(chip->can_sleep);
3640 struct gpio_desc *desc = desc_array[i];
3641 int hwgpio = gpio_chip_hwgpio(desc);
3642 int value = test_bit(i, value_bitmap);
3645 * Pins applicable for fast input but not for
3646 * fast output processing may have been already
3647 * inverted inside the fast path, skip them.
3649 if (!raw && !(array_info &&
3650 test_bit(i, array_info->invert_mask)) &&
3651 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3653 trace_gpio_value(desc_to_gpio(desc), 0, value);
3655 * collect all normal outputs belonging to the same chip
3656 * open drain and open source outputs are set individually
3658 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
3659 gpio_set_open_drain_value_commit(desc, value);
3660 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
3661 gpio_set_open_source_value_commit(desc, value);
3663 __set_bit(hwgpio, mask);
3665 __set_bit(hwgpio, bits);
3667 __clear_bit(hwgpio, bits);
3673 i = find_next_zero_bit(array_info->set_mask,
3675 } while ((i < array_size) &&
3676 (desc_array[i]->gdev->chip == chip));
3677 /* push collected bits to outputs */
3679 gpio_chip_set_multiple(chip, mask, bits);
3681 if (mask != fastpath)
3688 * gpiod_set_raw_value() - assign a gpio's raw value
3689 * @desc: gpio whose value will be assigned
3690 * @value: value to assign
3692 * Set the raw value of the GPIO, i.e. the value of its physical line without
3693 * regard for its ACTIVE_LOW status.
3695 * This function can be called from contexts where we cannot sleep, and will
3696 * complain if the GPIO chip functions potentially sleep.
3698 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
3700 VALIDATE_DESC_VOID(desc);
3701 /* Should be using gpiod_set_raw_value_cansleep() */
3702 WARN_ON(desc->gdev->chip->can_sleep);
3703 gpiod_set_raw_value_commit(desc, value);
3705 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
3708 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3709 * @desc: the descriptor to set the value on
3710 * @value: value to set
3712 * This sets the value of a GPIO line backing a descriptor, applying
3713 * different semantic quirks like active low and open drain/source
3716 static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3718 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3720 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3721 gpio_set_open_drain_value_commit(desc, value);
3722 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3723 gpio_set_open_source_value_commit(desc, value);
3725 gpiod_set_raw_value_commit(desc, value);
3729 * gpiod_set_value() - assign a gpio's value
3730 * @desc: gpio whose value will be assigned
3731 * @value: value to assign
3733 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3734 * OPEN_DRAIN and OPEN_SOURCE flags into account.
3736 * This function can be called from contexts where we cannot sleep, and will
3737 * complain if the GPIO chip functions potentially sleep.
3739 void gpiod_set_value(struct gpio_desc *desc, int value)
3741 VALIDATE_DESC_VOID(desc);
3742 /* Should be using gpiod_set_value_cansleep() */
3743 WARN_ON(desc->gdev->chip->can_sleep);
3744 gpiod_set_value_nocheck(desc, value);
3746 EXPORT_SYMBOL_GPL(gpiod_set_value);
3749 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
3750 * @array_size: number of elements in the descriptor array / value bitmap
3751 * @desc_array: array of GPIO descriptors whose values will be assigned
3752 * @array_info: information on applicability of fast bitmap processing path
3753 * @value_bitmap: bitmap of values to assign
3755 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3756 * without regard for their ACTIVE_LOW status.
3758 * This function can be called from contexts where we cannot sleep, and will
3759 * complain if the GPIO chip functions potentially sleep.
3761 int gpiod_set_raw_array_value(unsigned int array_size,
3762 struct gpio_desc **desc_array,
3763 struct gpio_array *array_info,
3764 unsigned long *value_bitmap)
3768 return gpiod_set_array_value_complex(true, false, array_size,
3769 desc_array, array_info, value_bitmap);
3771 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
3774 * gpiod_set_array_value() - assign values to an array of GPIOs
3775 * @array_size: number of elements in the descriptor array / value bitmap
3776 * @desc_array: array of GPIO descriptors whose values will be assigned
3777 * @array_info: information on applicability of fast bitmap processing path
3778 * @value_bitmap: bitmap of values to assign
3780 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3783 * This function can be called from contexts where we cannot sleep, and will
3784 * complain if the GPIO chip functions potentially sleep.
3786 int gpiod_set_array_value(unsigned int array_size,
3787 struct gpio_desc **desc_array,
3788 struct gpio_array *array_info,
3789 unsigned long *value_bitmap)
3793 return gpiod_set_array_value_complex(false, false, array_size,
3794 desc_array, array_info,
3797 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
3800 * gpiod_cansleep() - report whether gpio value access may sleep
3801 * @desc: gpio to check
3804 int gpiod_cansleep(const struct gpio_desc *desc)
3806 VALIDATE_DESC(desc);
3807 return desc->gdev->chip->can_sleep;
3809 EXPORT_SYMBOL_GPL(gpiod_cansleep);
3812 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3813 * @desc: gpio to set the consumer name on
3814 * @name: the new consumer name
3816 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
3818 VALIDATE_DESC(desc);
3820 name = kstrdup_const(name, GFP_KERNEL);
3825 kfree_const(desc->label);
3826 desc_set_label(desc, name);
3830 EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3833 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3834 * @desc: gpio whose IRQ will be returned (already requested)
3836 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3839 int gpiod_to_irq(const struct gpio_desc *desc)
3841 struct gpio_chip *chip;
3845 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3846 * requires this function to not return zero on an invalid descriptor
3847 * but rather a negative error number.
3849 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
3852 chip = desc->gdev->chip;
3853 offset = gpio_chip_hwgpio(desc);
3855 int retirq = chip->to_irq(chip, offset);
3857 /* Zero means NO_IRQ */
3865 EXPORT_SYMBOL_GPL(gpiod_to_irq);
3868 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3869 * @chip: the chip the GPIO to lock belongs to
3870 * @offset: the offset of the GPIO to lock as IRQ
3872 * This is used directly by GPIO drivers that want to lock down
3873 * a certain GPIO line to be used for IRQs.
3875 int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
3877 struct gpio_desc *desc;
3879 desc = gpiochip_get_desc(chip, offset);
3881 return PTR_ERR(desc);
3884 * If it's fast: flush the direction setting if something changed
3887 if (!chip->can_sleep && chip->get_direction) {
3888 int dir = gpiod_get_direction(desc);
3891 chip_err(chip, "%s: cannot get GPIO direction\n",
3897 /* To be valid for IRQ the line needs to be input or open drain */
3898 if (test_bit(FLAG_IS_OUT, &desc->flags) &&
3899 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
3901 "%s: tried to flag a GPIO set as output for IRQ\n",
3906 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
3907 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3910 * If the consumer has not set up a label (such as when the
3911 * IRQ is referenced from .to_irq()) we set up a label here
3912 * so it is clear this is used as an interrupt.
3915 desc_set_label(desc, "interrupt");
3919 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
3922 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3923 * @chip: the chip the GPIO to lock belongs to
3924 * @offset: the offset of the GPIO to lock as IRQ
3926 * This is used directly by GPIO drivers that want to indicate
3927 * that a certain GPIO is no longer used exclusively for IRQ.
3929 void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
3931 struct gpio_desc *desc;
3933 desc = gpiochip_get_desc(chip, offset);
3937 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3938 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3940 /* If we only had this marking, erase it */
3941 if (desc->label && !strcmp(desc->label, "interrupt"))
3942 desc_set_label(desc, NULL);
3944 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
3946 void gpiochip_disable_irq(struct gpio_chip *chip, unsigned int offset)
3948 struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
3950 if (!IS_ERR(desc) &&
3951 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
3952 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3954 EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
3956 void gpiochip_enable_irq(struct gpio_chip *chip, unsigned int offset)
3958 struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
3960 if (!IS_ERR(desc) &&
3961 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
3963 * We must not be output when using IRQ UNLESS we are
3966 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) &&
3967 !test_bit(FLAG_OPEN_DRAIN, &desc->flags));
3968 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3971 EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
3973 bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
3975 if (offset >= chip->ngpio)
3978 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
3980 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3982 int gpiochip_reqres_irq(struct gpio_chip *chip, unsigned int offset)
3986 if (!try_module_get(chip->gpiodev->owner))
3989 ret = gpiochip_lock_as_irq(chip, offset);
3991 chip_err(chip, "unable to lock HW IRQ %u for IRQ\n", offset);
3992 module_put(chip->gpiodev->owner);
3997 EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
3999 void gpiochip_relres_irq(struct gpio_chip *chip, unsigned int offset)
4001 gpiochip_unlock_as_irq(chip, offset);
4002 module_put(chip->gpiodev->owner);
4004 EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
4006 bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
4008 if (offset >= chip->ngpio)
4011 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
4013 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
4015 bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
4017 if (offset >= chip->ngpio)
4020 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
4022 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
4024 bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
4026 if (offset >= chip->ngpio)
4029 return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
4031 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
4034 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
4035 * @desc: gpio whose value will be returned
4037 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
4038 * its ACTIVE_LOW status, or negative errno on failure.
4040 * This function is to be called from contexts that can sleep.
4042 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
4044 might_sleep_if(extra_checks);
4045 VALIDATE_DESC(desc);
4046 return gpiod_get_raw_value_commit(desc);
4048 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
4051 * gpiod_get_value_cansleep() - return a gpio's value
4052 * @desc: gpio whose value will be returned
4054 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
4055 * account, or negative errno on failure.
4057 * This function is to be called from contexts that can sleep.
4059 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
4063 might_sleep_if(extra_checks);
4064 VALIDATE_DESC(desc);
4065 value = gpiod_get_raw_value_commit(desc);
4069 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
4074 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
4077 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
4078 * @array_size: number of elements in the descriptor array / value bitmap
4079 * @desc_array: array of GPIO descriptors whose values will be read
4080 * @array_info: information on applicability of fast bitmap processing path
4081 * @value_bitmap: bitmap to store the read values
4083 * Read the raw values of the GPIOs, i.e. the values of the physical lines
4084 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
4085 * else an error code.
4087 * This function is to be called from contexts that can sleep.
4089 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
4090 struct gpio_desc **desc_array,
4091 struct gpio_array *array_info,
4092 unsigned long *value_bitmap)
4094 might_sleep_if(extra_checks);
4097 return gpiod_get_array_value_complex(true, true, array_size,
4098 desc_array, array_info,
4101 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
4104 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
4105 * @array_size: number of elements in the descriptor array / value bitmap
4106 * @desc_array: array of GPIO descriptors whose values will be read
4107 * @array_info: information on applicability of fast bitmap processing path
4108 * @value_bitmap: bitmap to store the read values
4110 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
4111 * into account. Return 0 in case of success, else an error code.
4113 * This function is to be called from contexts that can sleep.
4115 int gpiod_get_array_value_cansleep(unsigned int array_size,
4116 struct gpio_desc **desc_array,
4117 struct gpio_array *array_info,
4118 unsigned long *value_bitmap)
4120 might_sleep_if(extra_checks);
4123 return gpiod_get_array_value_complex(false, true, array_size,
4124 desc_array, array_info,
4127 EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
4130 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
4131 * @desc: gpio whose value will be assigned
4132 * @value: value to assign
4134 * Set the raw value of the GPIO, i.e. the value of its physical line without
4135 * regard for its ACTIVE_LOW status.
4137 * This function is to be called from contexts that can sleep.
4139 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
4141 might_sleep_if(extra_checks);
4142 VALIDATE_DESC_VOID(desc);
4143 gpiod_set_raw_value_commit(desc, value);
4145 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
4148 * gpiod_set_value_cansleep() - assign a gpio's value
4149 * @desc: gpio whose value will be assigned
4150 * @value: value to assign
4152 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
4155 * This function is to be called from contexts that can sleep.
4157 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
4159 might_sleep_if(extra_checks);
4160 VALIDATE_DESC_VOID(desc);
4161 gpiod_set_value_nocheck(desc, value);
4163 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
4166 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
4167 * @array_size: number of elements in the descriptor array / value bitmap
4168 * @desc_array: array of GPIO descriptors whose values will be assigned
4169 * @array_info: information on applicability of fast bitmap processing path
4170 * @value_bitmap: bitmap of values to assign
4172 * Set the raw values of the GPIOs, i.e. the values of the physical lines
4173 * without regard for their ACTIVE_LOW status.
4175 * This function is to be called from contexts that can sleep.
4177 int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
4178 struct gpio_desc **desc_array,
4179 struct gpio_array *array_info,
4180 unsigned long *value_bitmap)
4182 might_sleep_if(extra_checks);
4185 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
4186 array_info, value_bitmap);
4188 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
4191 * gpiod_add_lookup_tables() - register GPIO device consumers
4192 * @tables: list of tables of consumers to register
4193 * @n: number of tables in the list
4195 void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
4199 mutex_lock(&gpio_lookup_lock);
4201 for (i = 0; i < n; i++)
4202 list_add_tail(&tables[i]->list, &gpio_lookup_list);
4204 mutex_unlock(&gpio_lookup_lock);
4208 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
4209 * @array_size: number of elements in the descriptor array / value bitmap
4210 * @desc_array: array of GPIO descriptors whose values will be assigned
4211 * @array_info: information on applicability of fast bitmap processing path
4212 * @value_bitmap: bitmap of values to assign
4214 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
4217 * This function is to be called from contexts that can sleep.
4219 int gpiod_set_array_value_cansleep(unsigned int array_size,
4220 struct gpio_desc **desc_array,
4221 struct gpio_array *array_info,
4222 unsigned long *value_bitmap)
4224 might_sleep_if(extra_checks);
4227 return gpiod_set_array_value_complex(false, true, array_size,
4228 desc_array, array_info,
4231 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
4234 * gpiod_add_lookup_table() - register GPIO device consumers
4235 * @table: table of consumers to register
4237 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
4239 mutex_lock(&gpio_lookup_lock);
4241 list_add_tail(&table->list, &gpio_lookup_list);
4243 mutex_unlock(&gpio_lookup_lock);
4245 EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
4248 * gpiod_remove_lookup_table() - unregister GPIO device consumers
4249 * @table: table of consumers to unregister
4251 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
4253 mutex_lock(&gpio_lookup_lock);
4255 list_del(&table->list);
4257 mutex_unlock(&gpio_lookup_lock);
4259 EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
4262 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
4263 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
4265 void gpiod_add_hogs(struct gpiod_hog *hogs)
4267 struct gpio_chip *chip;
4268 struct gpiod_hog *hog;
4270 mutex_lock(&gpio_machine_hogs_mutex);
4272 for (hog = &hogs[0]; hog->chip_label; hog++) {
4273 list_add_tail(&hog->list, &gpio_machine_hogs);
4276 * The chip may have been registered earlier, so check if it
4277 * exists and, if so, try to hog the line now.
4279 chip = find_chip_by_name(hog->chip_label);
4281 gpiochip_machine_hog(chip, hog);
4284 mutex_unlock(&gpio_machine_hogs_mutex);
4286 EXPORT_SYMBOL_GPL(gpiod_add_hogs);
4288 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
4290 const char *dev_id = dev ? dev_name(dev) : NULL;
4291 struct gpiod_lookup_table *table;
4293 mutex_lock(&gpio_lookup_lock);
4295 list_for_each_entry(table, &gpio_lookup_list, list) {
4296 if (table->dev_id && dev_id) {
4298 * Valid strings on both ends, must be identical to have
4301 if (!strcmp(table->dev_id, dev_id))
4305 * One of the pointers is NULL, so both must be to have
4308 if (dev_id == table->dev_id)
4315 mutex_unlock(&gpio_lookup_lock);
4319 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
4320 unsigned int idx, unsigned long *flags)
4322 struct gpio_desc *desc = ERR_PTR(-ENOENT);
4323 struct gpiod_lookup_table *table;
4324 struct gpiod_lookup *p;
4326 table = gpiod_find_lookup_table(dev);
4330 for (p = &table->table[0]; p->chip_label; p++) {
4331 struct gpio_chip *chip;
4333 /* idx must always match exactly */
4337 /* If the lookup entry has a con_id, require exact match */
4338 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
4341 chip = find_chip_by_name(p->chip_label);
4345 * As the lookup table indicates a chip with
4346 * p->chip_label should exist, assume it may
4347 * still appear later and let the interested
4348 * consumer be probed again or let the Deferred
4349 * Probe infrastructure handle the error.
4351 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
4353 return ERR_PTR(-EPROBE_DEFER);
4356 if (chip->ngpio <= p->chip_hwnum) {
4358 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
4359 idx, p->chip_hwnum, chip->ngpio - 1,
4361 return ERR_PTR(-EINVAL);
4364 desc = gpiochip_get_desc(chip, p->chip_hwnum);
4373 static int platform_gpio_count(struct device *dev, const char *con_id)
4375 struct gpiod_lookup_table *table;
4376 struct gpiod_lookup *p;
4377 unsigned int count = 0;
4379 table = gpiod_find_lookup_table(dev);
4383 for (p = &table->table[0]; p->chip_label; p++) {
4384 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
4385 (!con_id && !p->con_id))
4395 * gpiod_count - return the number of GPIOs associated with a device / function
4396 * or -ENOENT if no GPIO has been assigned to the requested function
4397 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4398 * @con_id: function within the GPIO consumer
4400 int gpiod_count(struct device *dev, const char *con_id)
4402 int count = -ENOENT;
4404 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
4405 count = of_gpio_get_count(dev, con_id);
4406 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
4407 count = acpi_gpio_count(dev, con_id);
4410 count = platform_gpio_count(dev, con_id);
4414 EXPORT_SYMBOL_GPL(gpiod_count);
4417 * gpiod_get - obtain a GPIO for a given GPIO function
4418 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4419 * @con_id: function within the GPIO consumer
4420 * @flags: optional GPIO initialization flags
4422 * Return the GPIO descriptor corresponding to the function con_id of device
4423 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
4424 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
4426 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
4427 enum gpiod_flags flags)
4429 return gpiod_get_index(dev, con_id, 0, flags);
4431 EXPORT_SYMBOL_GPL(gpiod_get);
4434 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
4435 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4436 * @con_id: function within the GPIO consumer
4437 * @flags: optional GPIO initialization flags
4439 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
4440 * the requested function it will return NULL. This is convenient for drivers
4441 * that need to handle optional GPIOs.
4443 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
4445 enum gpiod_flags flags)
4447 return gpiod_get_index_optional(dev, con_id, 0, flags);
4449 EXPORT_SYMBOL_GPL(gpiod_get_optional);
4453 * gpiod_configure_flags - helper function to configure a given GPIO
4454 * @desc: gpio whose value will be assigned
4455 * @con_id: function within the GPIO consumer
4456 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4457 * of_find_gpio() or of_get_gpio_hog()
4458 * @dflags: gpiod_flags - optional GPIO initialization flags
4460 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
4461 * requested function and/or index, or another IS_ERR() code if an error
4462 * occurred while trying to acquire the GPIO.
4464 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
4465 unsigned long lflags, enum gpiod_flags dflags)
4469 if (lflags & GPIO_ACTIVE_LOW)
4470 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
4472 if (lflags & GPIO_OPEN_DRAIN)
4473 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4474 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
4476 * This enforces open drain mode from the consumer side.
4477 * This is necessary for some busses like I2C, but the lookup
4478 * should *REALLY* have specified them as open drain in the
4479 * first place, so print a little warning here.
4481 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4483 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
4486 if (lflags & GPIO_OPEN_SOURCE)
4487 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
4489 if ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) {
4491 "both pull-up and pull-down enabled, invalid configuration\n");
4495 if (lflags & GPIO_PULL_UP)
4496 set_bit(FLAG_PULL_UP, &desc->flags);
4497 else if (lflags & GPIO_PULL_DOWN)
4498 set_bit(FLAG_PULL_DOWN, &desc->flags);
4500 ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
4504 /* No particular flag request, return here... */
4505 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
4506 pr_debug("no flags found for %s\n", con_id);
4511 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
4512 ret = gpiod_direction_output(desc,
4513 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
4515 ret = gpiod_direction_input(desc);
4521 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
4522 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4523 * @con_id: function within the GPIO consumer
4524 * @idx: index of the GPIO to obtain in the consumer
4525 * @flags: optional GPIO initialization flags
4527 * This variant of gpiod_get() allows to access GPIOs other than the first
4528 * defined one for functions that define several GPIOs.
4530 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
4531 * requested function and/or index, or another IS_ERR() code if an error
4532 * occurred while trying to acquire the GPIO.
4534 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
4537 enum gpiod_flags flags)
4539 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
4540 struct gpio_desc *desc = NULL;
4542 /* Maybe we have a device name, maybe not */
4543 const char *devname = dev ? dev_name(dev) : "?";
4545 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
4548 /* Using device tree? */
4549 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
4550 dev_dbg(dev, "using device tree for GPIO lookup\n");
4551 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
4552 } else if (ACPI_COMPANION(dev)) {
4553 dev_dbg(dev, "using ACPI for GPIO lookup\n");
4554 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
4559 * Either we are not using DT or ACPI, or their lookup did not return
4560 * a result. In that case, use platform lookup as a fallback.
4562 if (!desc || desc == ERR_PTR(-ENOENT)) {
4563 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
4564 desc = gpiod_find(dev, con_id, idx, &lookupflags);
4568 dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
4573 * If a connection label was passed use that, else attempt to use
4574 * the device name as label
4576 ret = gpiod_request(desc, con_id ? con_id : devname);
4578 if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
4580 * This happens when there are several consumers for
4581 * the same GPIO line: we just return here without
4582 * further initialization. It is a bit if a hack.
4583 * This is necessary to support fixed regulators.
4585 * FIXME: Make this more sane and safe.
4587 dev_info(dev, "nonexclusive access to GPIO for %s\n",
4588 con_id ? con_id : devname);
4591 return ERR_PTR(ret);
4595 ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
4597 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
4599 return ERR_PTR(ret);
4604 EXPORT_SYMBOL_GPL(gpiod_get_index);
4607 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
4608 * @fwnode: handle of the firmware node
4609 * @propname: name of the firmware property representing the GPIO
4610 * @index: index of the GPIO to obtain for the consumer
4611 * @dflags: GPIO initialization flags
4612 * @label: label to attach to the requested GPIO
4614 * This function can be used for drivers that get their configuration
4615 * from opaque firmware.
4617 * The function properly finds the corresponding GPIO using whatever is the
4618 * underlying firmware interface and then makes sure that the GPIO
4619 * descriptor is requested before it is returned to the caller.
4622 * On successful request the GPIO pin is configured in accordance with
4625 * In case of error an ERR_PTR() is returned.
4627 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
4628 const char *propname, int index,
4629 enum gpiod_flags dflags,
4632 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
4633 struct gpio_desc *desc = ERR_PTR(-ENODEV);
4637 return ERR_PTR(-EINVAL);
4639 if (is_of_node(fwnode)) {
4640 desc = gpiod_get_from_of_node(to_of_node(fwnode),
4645 } else if (is_acpi_node(fwnode)) {
4646 struct acpi_gpio_info info;
4648 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
4652 acpi_gpio_update_gpiod_flags(&dflags, &info);
4653 acpi_gpio_update_gpiod_lookup_flags(&lflags, &info);
4656 /* Currently only ACPI takes this path */
4657 ret = gpiod_request(desc, label);
4659 return ERR_PTR(ret);
4661 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
4664 return ERR_PTR(ret);
4669 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
4672 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
4674 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4675 * @con_id: function within the GPIO consumer
4676 * @index: index of the GPIO to obtain in the consumer
4677 * @flags: optional GPIO initialization flags
4679 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
4680 * specified index was assigned to the requested function it will return NULL.
4681 * This is convenient for drivers that need to handle optional GPIOs.
4683 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
4686 enum gpiod_flags flags)
4688 struct gpio_desc *desc;
4690 desc = gpiod_get_index(dev, con_id, index, flags);
4692 if (PTR_ERR(desc) == -ENOENT)
4698 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
4701 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4702 * @desc: gpio whose value will be assigned
4703 * @name: gpio line name
4704 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4705 * of_find_gpio() or of_get_gpio_hog()
4706 * @dflags: gpiod_flags - optional GPIO initialization flags
4708 int gpiod_hog(struct gpio_desc *desc, const char *name,
4709 unsigned long lflags, enum gpiod_flags dflags)
4711 struct gpio_chip *chip;
4712 struct gpio_desc *local_desc;
4716 chip = gpiod_to_chip(desc);
4717 hwnum = gpio_chip_hwgpio(desc);
4719 local_desc = gpiochip_request_own_desc(chip, hwnum, name,
4721 if (IS_ERR(local_desc)) {
4722 ret = PTR_ERR(local_desc);
4723 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
4724 name, chip->label, hwnum, ret);
4728 /* Mark GPIO as hogged so it can be identified and removed later */
4729 set_bit(FLAG_IS_HOGGED, &desc->flags);
4731 pr_info("GPIO line %d (%s) hogged as %s%s\n",
4732 desc_to_gpio(desc), name,
4733 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4734 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
4735 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
4741 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4742 * @chip: gpio chip to act on
4744 static void gpiochip_free_hogs(struct gpio_chip *chip)
4748 for (id = 0; id < chip->ngpio; id++) {
4749 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
4750 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
4755 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4756 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4757 * @con_id: function within the GPIO consumer
4758 * @flags: optional GPIO initialization flags
4760 * This function acquires all the GPIOs defined under a given function.
4762 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4763 * no GPIO has been assigned to the requested function, or another IS_ERR()
4764 * code if an error occurred while trying to acquire the GPIOs.
4766 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4768 enum gpiod_flags flags)
4770 struct gpio_desc *desc;
4771 struct gpio_descs *descs;
4772 struct gpio_array *array_info = NULL;
4773 struct gpio_chip *chip;
4774 int count, bitmap_size;
4776 count = gpiod_count(dev, con_id);
4778 return ERR_PTR(count);
4780 descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
4782 return ERR_PTR(-ENOMEM);
4784 for (descs->ndescs = 0; descs->ndescs < count; ) {
4785 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4787 gpiod_put_array(descs);
4788 return ERR_CAST(desc);
4791 descs->desc[descs->ndescs] = desc;
4793 chip = gpiod_to_chip(desc);
4795 * If pin hardware number of array member 0 is also 0, select
4796 * its chip as a candidate for fast bitmap processing path.
4798 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
4799 struct gpio_descs *array;
4801 bitmap_size = BITS_TO_LONGS(chip->ngpio > count ?
4802 chip->ngpio : count);
4804 array = kzalloc(struct_size(descs, desc, count) +
4805 struct_size(array_info, invert_mask,
4806 3 * bitmap_size), GFP_KERNEL);
4808 gpiod_put_array(descs);
4809 return ERR_PTR(-ENOMEM);
4812 memcpy(array, descs,
4813 struct_size(descs, desc, descs->ndescs + 1));
4817 array_info = (void *)(descs->desc + count);
4818 array_info->get_mask = array_info->invert_mask +
4820 array_info->set_mask = array_info->get_mask +
4823 array_info->desc = descs->desc;
4824 array_info->size = count;
4825 array_info->chip = chip;
4826 bitmap_set(array_info->get_mask, descs->ndescs,
4827 count - descs->ndescs);
4828 bitmap_set(array_info->set_mask, descs->ndescs,
4829 count - descs->ndescs);
4830 descs->info = array_info;
4832 /* Unmark array members which don't belong to the 'fast' chip */
4833 if (array_info && array_info->chip != chip) {
4834 __clear_bit(descs->ndescs, array_info->get_mask);
4835 __clear_bit(descs->ndescs, array_info->set_mask);
4838 * Detect array members which belong to the 'fast' chip
4839 * but their pins are not in hardware order.
4841 else if (array_info &&
4842 gpio_chip_hwgpio(desc) != descs->ndescs) {
4844 * Don't use fast path if all array members processed so
4845 * far belong to the same chip as this one but its pin
4846 * hardware number is different from its array index.
4848 if (bitmap_full(array_info->get_mask, descs->ndescs)) {
4851 __clear_bit(descs->ndescs,
4852 array_info->get_mask);
4853 __clear_bit(descs->ndescs,
4854 array_info->set_mask);
4856 } else if (array_info) {
4857 /* Exclude open drain or open source from fast output */
4858 if (gpiochip_line_is_open_drain(chip, descs->ndescs) ||
4859 gpiochip_line_is_open_source(chip, descs->ndescs))
4860 __clear_bit(descs->ndescs,
4861 array_info->set_mask);
4862 /* Identify 'fast' pins which require invertion */
4863 if (gpiod_is_active_low(desc))
4864 __set_bit(descs->ndescs,
4865 array_info->invert_mask);
4872 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4873 array_info->chip->label, array_info->size,
4874 *array_info->get_mask, *array_info->set_mask,
4875 *array_info->invert_mask);
4878 EXPORT_SYMBOL_GPL(gpiod_get_array);
4881 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4883 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4884 * @con_id: function within the GPIO consumer
4885 * @flags: optional GPIO initialization flags
4887 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4888 * assigned to the requested function it will return NULL.
4890 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4892 enum gpiod_flags flags)
4894 struct gpio_descs *descs;
4896 descs = gpiod_get_array(dev, con_id, flags);
4897 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
4902 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4905 * gpiod_put - dispose of a GPIO descriptor
4906 * @desc: GPIO descriptor to dispose of
4908 * No descriptor can be used after gpiod_put() has been called on it.
4910 void gpiod_put(struct gpio_desc *desc)
4915 EXPORT_SYMBOL_GPL(gpiod_put);
4918 * gpiod_put_array - dispose of multiple GPIO descriptors
4919 * @descs: struct gpio_descs containing an array of descriptors
4921 void gpiod_put_array(struct gpio_descs *descs)
4925 for (i = 0; i < descs->ndescs; i++)
4926 gpiod_put(descs->desc[i]);
4930 EXPORT_SYMBOL_GPL(gpiod_put_array);
4932 static int __init gpiolib_dev_init(void)
4936 /* Register GPIO sysfs bus */
4937 ret = bus_register(&gpio_bus_type);
4939 pr_err("gpiolib: could not register GPIO bus type\n");
4943 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
4945 pr_err("gpiolib: failed to allocate char dev region\n");
4946 bus_unregister(&gpio_bus_type);
4948 gpiolib_initialized = true;
4949 gpiochip_setup_devs();
4953 core_initcall(gpiolib_dev_init);
4955 #ifdef CONFIG_DEBUG_FS
4957 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
4960 struct gpio_chip *chip = gdev->chip;
4961 unsigned gpio = gdev->base;
4962 struct gpio_desc *gdesc = &gdev->descs[0];
4967 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
4968 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
4970 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
4976 gpiod_get_direction(gdesc);
4977 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
4978 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
4979 active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags);
4980 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s",
4981 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
4982 is_out ? "out" : "in ",
4983 chip->get ? (chip->get(chip, i) ? "hi" : "lo") : "? ",
4984 is_irq ? "IRQ " : "",
4985 active_low ? "ACTIVE LOW" : "");
4986 seq_printf(s, "\n");
4990 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
4992 unsigned long flags;
4993 struct gpio_device *gdev = NULL;
4994 loff_t index = *pos;
4998 spin_lock_irqsave(&gpio_lock, flags);
4999 list_for_each_entry(gdev, &gpio_devices, list)
5001 spin_unlock_irqrestore(&gpio_lock, flags);
5004 spin_unlock_irqrestore(&gpio_lock, flags);
5009 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
5011 unsigned long flags;
5012 struct gpio_device *gdev = v;
5015 spin_lock_irqsave(&gpio_lock, flags);
5016 if (list_is_last(&gdev->list, &gpio_devices))
5019 ret = list_entry(gdev->list.next, struct gpio_device, list);
5020 spin_unlock_irqrestore(&gpio_lock, flags);
5028 static void gpiolib_seq_stop(struct seq_file *s, void *v)
5032 static int gpiolib_seq_show(struct seq_file *s, void *v)
5034 struct gpio_device *gdev = v;
5035 struct gpio_chip *chip = gdev->chip;
5036 struct device *parent;
5039 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
5040 dev_name(&gdev->dev));
5044 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
5045 dev_name(&gdev->dev),
5046 gdev->base, gdev->base + gdev->ngpio - 1);
5047 parent = chip->parent;
5049 seq_printf(s, ", parent: %s/%s",
5050 parent->bus ? parent->bus->name : "no-bus",
5053 seq_printf(s, ", %s", chip->label);
5054 if (chip->can_sleep)
5055 seq_printf(s, ", can sleep");
5056 seq_printf(s, ":\n");
5059 chip->dbg_show(s, chip);
5061 gpiolib_dbg_show(s, gdev);
5066 static const struct seq_operations gpiolib_seq_ops = {
5067 .start = gpiolib_seq_start,
5068 .next = gpiolib_seq_next,
5069 .stop = gpiolib_seq_stop,
5070 .show = gpiolib_seq_show,
5073 static int gpiolib_open(struct inode *inode, struct file *file)
5075 return seq_open(file, &gpiolib_seq_ops);
5078 static const struct file_operations gpiolib_operations = {
5079 .owner = THIS_MODULE,
5080 .open = gpiolib_open,
5082 .llseek = seq_lseek,
5083 .release = seq_release,
5086 static int __init gpiolib_debugfs_init(void)
5088 /* /sys/kernel/debug/gpio */
5089 debugfs_create_file("gpio", S_IFREG | S_IRUGO, NULL, NULL,
5090 &gpiolib_operations);
5093 subsys_initcall(gpiolib_debugfs_init);
5095 #endif /* DEBUG_FS */