1 #include <linux/bitmap.h>
2 #include <linux/kernel.h>
3 #include <linux/module.h>
4 #include <linux/interrupt.h>
6 #include <linux/spinlock.h>
7 #include <linux/list.h>
8 #include <linux/device.h>
10 #include <linux/debugfs.h>
11 #include <linux/seq_file.h>
12 #include <linux/gpio.h>
13 #include <linux/of_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>
33 #define CREATE_TRACE_POINTS
34 #include <trace/events/gpio.h>
36 /* Implementation infrastructure for GPIO interfaces.
38 * The GPIO programming interface allows for inlining speed-critical
39 * get/set operations for common cases, so that access to SOC-integrated
40 * GPIOs can sometimes cost only an instruction or two per bit.
44 /* When debugging, extend minimal trust to callers and platform code.
45 * Also emit diagnostic messages that may help initial bringup, when
46 * board setup or driver bugs are most common.
48 * Otherwise, minimize overhead in what may be bitbanging codepaths.
51 #define extra_checks 1
53 #define extra_checks 0
56 /* Device and char device-related information */
57 static DEFINE_IDA(gpio_ida);
58 static dev_t gpio_devt;
59 #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
60 static struct bus_type gpio_bus_type = {
64 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
65 * While any GPIO is requested, its gpio_chip is not removable;
66 * each GPIO's "requested" flag serves as a lock and refcount.
68 DEFINE_SPINLOCK(gpio_lock);
70 static DEFINE_MUTEX(gpio_lookup_lock);
71 static LIST_HEAD(gpio_lookup_list);
72 LIST_HEAD(gpio_devices);
74 static void gpiochip_free_hogs(struct gpio_chip *chip);
75 static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
76 struct lock_class_key *lock_key,
77 struct lock_class_key *request_key);
78 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
79 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
80 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
82 static bool gpiolib_initialized;
84 static inline void desc_set_label(struct gpio_desc *d, const char *label)
90 * gpio_to_desc - Convert a GPIO number to its descriptor
91 * @gpio: global GPIO number
94 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
95 * with the given number exists in the system.
97 struct gpio_desc *gpio_to_desc(unsigned gpio)
99 struct gpio_device *gdev;
102 spin_lock_irqsave(&gpio_lock, flags);
104 list_for_each_entry(gdev, &gpio_devices, list) {
105 if (gdev->base <= gpio &&
106 gdev->base + gdev->ngpio > gpio) {
107 spin_unlock_irqrestore(&gpio_lock, flags);
108 return &gdev->descs[gpio - gdev->base];
112 spin_unlock_irqrestore(&gpio_lock, flags);
114 if (!gpio_is_valid(gpio))
115 WARN(1, "invalid GPIO %d\n", gpio);
119 EXPORT_SYMBOL_GPL(gpio_to_desc);
122 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
123 * hardware number for this chip
125 * @hwnum: hardware number of the GPIO for this chip
128 * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists
129 * in the given chip for the specified hardware number.
131 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
134 struct gpio_device *gdev = chip->gpiodev;
136 if (hwnum >= gdev->ngpio)
137 return ERR_PTR(-EINVAL);
139 return &gdev->descs[hwnum];
143 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
144 * @desc: GPIO descriptor
146 * This should disappear in the future but is needed since we still
147 * use GPIO numbers for error messages and sysfs nodes.
150 * The global GPIO number for the GPIO specified by its descriptor.
152 int desc_to_gpio(const struct gpio_desc *desc)
154 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
156 EXPORT_SYMBOL_GPL(desc_to_gpio);
160 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
161 * @desc: descriptor to return the chip of
163 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
165 if (!desc || !desc->gdev)
167 return desc->gdev->chip;
169 EXPORT_SYMBOL_GPL(gpiod_to_chip);
171 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
172 static int gpiochip_find_base(int ngpio)
174 struct gpio_device *gdev;
175 int base = ARCH_NR_GPIOS - ngpio;
177 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
178 /* found a free space? */
179 if (gdev->base + gdev->ngpio <= base)
182 /* nope, check the space right before the chip */
183 base = gdev->base - ngpio;
186 if (gpio_is_valid(base)) {
187 pr_debug("%s: found new base at %d\n", __func__, base);
190 pr_err("%s: cannot find free range\n", __func__);
196 * gpiod_get_direction - return the current direction of a GPIO
197 * @desc: GPIO to get the direction of
199 * Returns 0 for output, 1 for input, or an error code in case of error.
201 * This function may sleep if gpiod_cansleep() is true.
203 int gpiod_get_direction(struct gpio_desc *desc)
205 struct gpio_chip *chip;
207 int status = -EINVAL;
209 chip = gpiod_to_chip(desc);
210 offset = gpio_chip_hwgpio(desc);
212 if (!chip->get_direction)
215 status = chip->get_direction(chip, offset);
217 /* GPIOF_DIR_IN, or other positive */
219 clear_bit(FLAG_IS_OUT, &desc->flags);
223 set_bit(FLAG_IS_OUT, &desc->flags);
227 EXPORT_SYMBOL_GPL(gpiod_get_direction);
230 * Add a new chip to the global chips list, keeping the list of chips sorted
231 * by range(means [base, base + ngpio - 1]) order.
233 * Return -EBUSY if the new chip overlaps with some other chip's integer
236 static int gpiodev_add_to_list(struct gpio_device *gdev)
238 struct gpio_device *prev, *next;
240 if (list_empty(&gpio_devices)) {
241 /* initial entry in list */
242 list_add_tail(&gdev->list, &gpio_devices);
246 next = list_entry(gpio_devices.next, struct gpio_device, list);
247 if (gdev->base + gdev->ngpio <= next->base) {
248 /* add before first entry */
249 list_add(&gdev->list, &gpio_devices);
253 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
254 if (prev->base + prev->ngpio <= gdev->base) {
255 /* add behind last entry */
256 list_add_tail(&gdev->list, &gpio_devices);
260 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
261 /* at the end of the list */
262 if (&next->list == &gpio_devices)
265 /* add between prev and next */
266 if (prev->base + prev->ngpio <= gdev->base
267 && gdev->base + gdev->ngpio <= next->base) {
268 list_add(&gdev->list, &prev->list);
273 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
278 * Convert a GPIO name to its descriptor
280 static struct gpio_desc *gpio_name_to_desc(const char * const name)
282 struct gpio_device *gdev;
285 spin_lock_irqsave(&gpio_lock, flags);
287 list_for_each_entry(gdev, &gpio_devices, list) {
290 for (i = 0; i != gdev->ngpio; ++i) {
291 struct gpio_desc *desc = &gdev->descs[i];
293 if (!desc->name || !name)
296 if (!strcmp(desc->name, name)) {
297 spin_unlock_irqrestore(&gpio_lock, flags);
303 spin_unlock_irqrestore(&gpio_lock, flags);
309 * Takes the names from gc->names and checks if they are all unique. If they
310 * are, they are assigned to their gpio descriptors.
312 * Warning if one of the names is already used for a different GPIO.
314 static int gpiochip_set_desc_names(struct gpio_chip *gc)
316 struct gpio_device *gdev = gc->gpiodev;
322 /* First check all names if they are unique */
323 for (i = 0; i != gc->ngpio; ++i) {
324 struct gpio_desc *gpio;
326 gpio = gpio_name_to_desc(gc->names[i]);
329 "Detected name collision for GPIO name '%s'\n",
333 /* Then add all names to the GPIO descriptors */
334 for (i = 0; i != gc->ngpio; ++i)
335 gdev->descs[i].name = gc->names[i];
340 static unsigned long *gpiochip_allocate_mask(struct gpio_chip *chip)
344 p = kmalloc_array(BITS_TO_LONGS(chip->ngpio), sizeof(*p), GFP_KERNEL);
348 /* Assume by default all GPIOs are valid */
349 bitmap_fill(p, chip->ngpio);
354 static int gpiochip_init_valid_mask(struct gpio_chip *gpiochip)
356 #ifdef CONFIG_OF_GPIO
358 struct device_node *np = gpiochip->of_node;
360 size = of_property_count_u32_elems(np, "gpio-reserved-ranges");
361 if (size > 0 && size % 2 == 0)
362 gpiochip->need_valid_mask = true;
365 if (!gpiochip->need_valid_mask)
368 gpiochip->valid_mask = gpiochip_allocate_mask(gpiochip);
369 if (!gpiochip->valid_mask)
375 static void gpiochip_free_valid_mask(struct gpio_chip *gpiochip)
377 kfree(gpiochip->valid_mask);
378 gpiochip->valid_mask = NULL;
381 bool gpiochip_line_is_valid(const struct gpio_chip *gpiochip,
384 /* No mask means all valid */
385 if (likely(!gpiochip->valid_mask))
387 return test_bit(offset, gpiochip->valid_mask);
389 EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
392 * GPIO line handle management
396 * struct linehandle_state - contains the state of a userspace handle
397 * @gdev: the GPIO device the handle pertains to
398 * @label: consumer label used to tag descriptors
399 * @descs: the GPIO descriptors held by this handle
400 * @numdescs: the number of descriptors held in the descs array
402 struct linehandle_state {
403 struct gpio_device *gdev;
405 struct gpio_desc *descs[GPIOHANDLES_MAX];
409 #define GPIOHANDLE_REQUEST_VALID_FLAGS \
410 (GPIOHANDLE_REQUEST_INPUT | \
411 GPIOHANDLE_REQUEST_OUTPUT | \
412 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
413 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
414 GPIOHANDLE_REQUEST_OPEN_SOURCE)
416 static long linehandle_ioctl(struct file *filep, unsigned int cmd,
419 struct linehandle_state *lh = filep->private_data;
420 void __user *ip = (void __user *)arg;
421 struct gpiohandle_data ghd;
422 int vals[GPIOHANDLES_MAX];
425 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
426 /* TODO: check if descriptors are really input */
427 int ret = gpiod_get_array_value_complex(false,
435 memset(&ghd, 0, sizeof(ghd));
436 for (i = 0; i < lh->numdescs; i++)
437 ghd.values[i] = vals[i];
439 if (copy_to_user(ip, &ghd, sizeof(ghd)))
443 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
444 /* TODO: check if descriptors are really output */
445 if (copy_from_user(&ghd, ip, sizeof(ghd)))
448 /* Clamp all values to [0,1] */
449 for (i = 0; i < lh->numdescs; i++)
450 vals[i] = !!ghd.values[i];
452 /* Reuse the array setting function */
453 gpiod_set_array_value_complex(false,
464 static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
467 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
471 static int linehandle_release(struct inode *inode, struct file *filep)
473 struct linehandle_state *lh = filep->private_data;
474 struct gpio_device *gdev = lh->gdev;
477 for (i = 0; i < lh->numdescs; i++)
478 gpiod_free(lh->descs[i]);
481 put_device(&gdev->dev);
485 static const struct file_operations linehandle_fileops = {
486 .release = linehandle_release,
487 .owner = THIS_MODULE,
488 .llseek = noop_llseek,
489 .unlocked_ioctl = linehandle_ioctl,
491 .compat_ioctl = linehandle_ioctl_compat,
495 static int linehandle_create(struct gpio_device *gdev, void __user *ip)
497 struct gpiohandle_request handlereq;
498 struct linehandle_state *lh;
503 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
505 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
508 lflags = handlereq.flags;
510 /* Return an error if an unknown flag is set */
511 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
515 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
516 * the hardware actually supports enabling both at the same time the
517 * electrical result would be disastrous.
519 if ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
520 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
523 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
524 if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) &&
525 ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
526 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
529 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
533 get_device(&gdev->dev);
535 /* Make sure this is terminated */
536 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
537 if (strlen(handlereq.consumer_label)) {
538 lh->label = kstrdup(handlereq.consumer_label,
546 /* Request each GPIO */
547 for (i = 0; i < handlereq.lines; i++) {
548 u32 offset = handlereq.lineoffsets[i];
549 struct gpio_desc *desc;
551 if (offset >= gdev->ngpio) {
556 desc = &gdev->descs[offset];
557 ret = gpiod_request(desc, lh->label);
562 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
563 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
564 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
565 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
566 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
567 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
569 ret = gpiod_set_transitory(desc, false);
574 * Lines have to be requested explicitly for input
575 * or output, else the line will be treated "as is".
577 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
578 int val = !!handlereq.default_values[i];
580 ret = gpiod_direction_output(desc, val);
583 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
584 ret = gpiod_direction_input(desc);
588 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
591 /* Let i point at the last handle */
593 lh->numdescs = handlereq.lines;
595 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
601 file = anon_inode_getfile("gpio-linehandle",
604 O_RDONLY | O_CLOEXEC);
607 goto out_put_unused_fd;
611 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
613 * fput() will trigger the release() callback, so do not go onto
614 * the regular error cleanup path here.
621 fd_install(fd, file);
623 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
632 gpiod_free(lh->descs[i]);
636 put_device(&gdev->dev);
641 * GPIO line event management
645 * struct lineevent_state - contains the state of a userspace event
646 * @gdev: the GPIO device the event pertains to
647 * @label: consumer label used to tag descriptors
648 * @desc: the GPIO descriptor held by this event
649 * @eflags: the event flags this line was requested with
650 * @irq: the interrupt that trigger in response to events on this GPIO
651 * @wait: wait queue that handles blocking reads of events
652 * @events: KFIFO for the GPIO events
653 * @read_lock: mutex lock to protect reads from colliding with adding
654 * new events to the FIFO
655 * @timestamp: cache for the timestamp storing it between hardirq
656 * and IRQ thread, used to bring the timestamp close to the actual
659 struct lineevent_state {
660 struct gpio_device *gdev;
662 struct gpio_desc *desc;
665 wait_queue_head_t wait;
666 DECLARE_KFIFO(events, struct gpioevent_data, 16);
667 struct mutex read_lock;
671 #define GPIOEVENT_REQUEST_VALID_FLAGS \
672 (GPIOEVENT_REQUEST_RISING_EDGE | \
673 GPIOEVENT_REQUEST_FALLING_EDGE)
675 static __poll_t lineevent_poll(struct file *filep,
676 struct poll_table_struct *wait)
678 struct lineevent_state *le = filep->private_data;
681 poll_wait(filep, &le->wait, wait);
683 if (!kfifo_is_empty(&le->events))
684 events = EPOLLIN | EPOLLRDNORM;
690 static ssize_t lineevent_read(struct file *filep,
695 struct lineevent_state *le = filep->private_data;
699 if (count < sizeof(struct gpioevent_data))
703 if (kfifo_is_empty(&le->events)) {
704 if (filep->f_flags & O_NONBLOCK)
707 ret = wait_event_interruptible(le->wait,
708 !kfifo_is_empty(&le->events));
713 if (mutex_lock_interruptible(&le->read_lock))
715 ret = kfifo_to_user(&le->events, buf, count, &copied);
716 mutex_unlock(&le->read_lock);
722 * If we couldn't read anything from the fifo (a different
723 * thread might have been faster) we either return -EAGAIN if
724 * the file descriptor is non-blocking, otherwise we go back to
725 * sleep and wait for more data to arrive.
727 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
730 } while (copied == 0);
735 static int lineevent_release(struct inode *inode, struct file *filep)
737 struct lineevent_state *le = filep->private_data;
738 struct gpio_device *gdev = le->gdev;
740 free_irq(le->irq, le);
741 gpiod_free(le->desc);
744 put_device(&gdev->dev);
748 static long lineevent_ioctl(struct file *filep, unsigned int cmd,
751 struct lineevent_state *le = filep->private_data;
752 void __user *ip = (void __user *)arg;
753 struct gpiohandle_data ghd;
756 * We can get the value for an event line but not set it,
757 * because it is input by definition.
759 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
762 memset(&ghd, 0, sizeof(ghd));
764 val = gpiod_get_value_cansleep(le->desc);
769 if (copy_to_user(ip, &ghd, sizeof(ghd)))
778 static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
781 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
785 static const struct file_operations lineevent_fileops = {
786 .release = lineevent_release,
787 .read = lineevent_read,
788 .poll = lineevent_poll,
789 .owner = THIS_MODULE,
790 .llseek = noop_llseek,
791 .unlocked_ioctl = lineevent_ioctl,
793 .compat_ioctl = lineevent_ioctl_compat,
797 static irqreturn_t lineevent_irq_thread(int irq, void *p)
799 struct lineevent_state *le = p;
800 struct gpioevent_data ge;
803 /* Do not leak kernel stack to userspace */
804 memset(&ge, 0, sizeof(ge));
806 ge.timestamp = le->timestamp;
807 level = gpiod_get_value_cansleep(le->desc);
809 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
810 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
812 /* Emit low-to-high event */
813 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
815 /* Emit high-to-low event */
816 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
817 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE && level) {
818 /* Emit low-to-high event */
819 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
820 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE && !level) {
821 /* Emit high-to-low event */
822 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
827 ret = kfifo_put(&le->events, ge);
829 wake_up_poll(&le->wait, EPOLLIN);
834 static irqreturn_t lineevent_irq_handler(int irq, void *p)
836 struct lineevent_state *le = p;
839 * Just store the timestamp in hardirq context so we get it as
840 * close in time as possible to the actual event.
842 le->timestamp = ktime_get_real_ns();
844 return IRQ_WAKE_THREAD;
847 static int lineevent_create(struct gpio_device *gdev, void __user *ip)
849 struct gpioevent_request eventreq;
850 struct lineevent_state *le;
851 struct gpio_desc *desc;
860 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
863 le = kzalloc(sizeof(*le), GFP_KERNEL);
867 get_device(&gdev->dev);
869 /* Make sure this is terminated */
870 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
871 if (strlen(eventreq.consumer_label)) {
872 le->label = kstrdup(eventreq.consumer_label,
880 offset = eventreq.lineoffset;
881 lflags = eventreq.handleflags;
882 eflags = eventreq.eventflags;
884 if (offset >= gdev->ngpio) {
889 /* Return an error if a unknown flag is set */
890 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
891 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
896 /* This is just wrong: we don't look for events on output lines */
897 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
902 desc = &gdev->descs[offset];
903 ret = gpiod_request(desc, le->label);
909 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
910 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
911 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
912 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
913 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
914 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
916 ret = gpiod_direction_input(desc);
920 le->irq = gpiod_to_irq(desc);
926 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
927 irqflags |= IRQF_TRIGGER_RISING;
928 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
929 irqflags |= IRQF_TRIGGER_FALLING;
930 irqflags |= IRQF_ONESHOT;
931 irqflags |= IRQF_SHARED;
933 INIT_KFIFO(le->events);
934 init_waitqueue_head(&le->wait);
935 mutex_init(&le->read_lock);
937 /* Request a thread to read the events */
938 ret = request_threaded_irq(le->irq,
939 lineevent_irq_handler,
940 lineevent_irq_thread,
947 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
953 file = anon_inode_getfile("gpio-event",
956 O_RDONLY | O_CLOEXEC);
959 goto out_put_unused_fd;
963 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
965 * fput() will trigger the release() callback, so do not go onto
966 * the regular error cleanup path here.
973 fd_install(fd, file);
980 free_irq(le->irq, le);
982 gpiod_free(le->desc);
987 put_device(&gdev->dev);
992 * gpio_ioctl() - ioctl handler for the GPIO chardev
994 static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
996 struct gpio_device *gdev = filp->private_data;
997 struct gpio_chip *chip = gdev->chip;
998 void __user *ip = (void __user *)arg;
1000 /* We fail any subsequent ioctl():s when the chip is gone */
1004 /* Fill in the struct and pass to userspace */
1005 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
1006 struct gpiochip_info chipinfo;
1008 memset(&chipinfo, 0, sizeof(chipinfo));
1010 strncpy(chipinfo.name, dev_name(&gdev->dev),
1011 sizeof(chipinfo.name));
1012 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
1013 strncpy(chipinfo.label, gdev->label,
1014 sizeof(chipinfo.label));
1015 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
1016 chipinfo.lines = gdev->ngpio;
1017 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
1020 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
1021 struct gpioline_info lineinfo;
1022 struct gpio_desc *desc;
1024 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1026 if (lineinfo.line_offset >= gdev->ngpio)
1029 desc = &gdev->descs[lineinfo.line_offset];
1031 strncpy(lineinfo.name, desc->name,
1032 sizeof(lineinfo.name));
1033 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
1035 lineinfo.name[0] = '\0';
1038 strncpy(lineinfo.consumer, desc->label,
1039 sizeof(lineinfo.consumer));
1040 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
1042 lineinfo.consumer[0] = '\0';
1046 * Userspace only need to know that the kernel is using
1047 * this GPIO so it can't use it.
1050 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
1051 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
1052 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
1053 test_bit(FLAG_EXPORT, &desc->flags) ||
1054 test_bit(FLAG_SYSFS, &desc->flags))
1055 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
1056 if (test_bit(FLAG_IS_OUT, &desc->flags))
1057 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
1058 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1059 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
1060 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1061 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
1062 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1063 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
1065 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
1068 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
1069 return linehandle_create(gdev, ip);
1070 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
1071 return lineevent_create(gdev, ip);
1076 #ifdef CONFIG_COMPAT
1077 static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
1080 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
1085 * gpio_chrdev_open() - open the chardev for ioctl operations
1086 * @inode: inode for this chardev
1087 * @filp: file struct for storing private data
1088 * Returns 0 on success
1090 static int gpio_chrdev_open(struct inode *inode, struct file *filp)
1092 struct gpio_device *gdev = container_of(inode->i_cdev,
1093 struct gpio_device, chrdev);
1095 /* Fail on open if the backing gpiochip is gone */
1098 get_device(&gdev->dev);
1099 filp->private_data = gdev;
1101 return nonseekable_open(inode, filp);
1105 * gpio_chrdev_release() - close chardev after ioctl operations
1106 * @inode: inode for this chardev
1107 * @filp: file struct for storing private data
1108 * Returns 0 on success
1110 static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1112 struct gpio_device *gdev = container_of(inode->i_cdev,
1113 struct gpio_device, chrdev);
1115 put_device(&gdev->dev);
1120 static const struct file_operations gpio_fileops = {
1121 .release = gpio_chrdev_release,
1122 .open = gpio_chrdev_open,
1123 .owner = THIS_MODULE,
1124 .llseek = no_llseek,
1125 .unlocked_ioctl = gpio_ioctl,
1126 #ifdef CONFIG_COMPAT
1127 .compat_ioctl = gpio_ioctl_compat,
1131 static void gpiodevice_release(struct device *dev)
1133 struct gpio_device *gdev = dev_get_drvdata(dev);
1135 list_del(&gdev->list);
1136 ida_simple_remove(&gpio_ida, gdev->id);
1137 kfree_const(gdev->label);
1142 static int gpiochip_setup_dev(struct gpio_device *gdev)
1146 cdev_init(&gdev->chrdev, &gpio_fileops);
1147 gdev->chrdev.owner = THIS_MODULE;
1148 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
1150 status = cdev_device_add(&gdev->chrdev, &gdev->dev);
1154 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1155 MAJOR(gpio_devt), gdev->id);
1157 status = gpiochip_sysfs_register(gdev);
1159 goto err_remove_device;
1161 /* From this point, the .release() function cleans up gpio_device */
1162 gdev->dev.release = gpiodevice_release;
1163 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1164 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1165 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1170 cdev_device_del(&gdev->chrdev, &gdev->dev);
1174 static void gpiochip_setup_devs(void)
1176 struct gpio_device *gdev;
1179 list_for_each_entry(gdev, &gpio_devices, list) {
1180 err = gpiochip_setup_dev(gdev);
1182 pr_err("%s: Failed to initialize gpio device (%d)\n",
1183 dev_name(&gdev->dev), err);
1187 int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
1188 struct lock_class_key *lock_key,
1189 struct lock_class_key *request_key)
1191 unsigned long flags;
1194 int base = chip->base;
1195 struct gpio_device *gdev;
1198 * First: allocate and populate the internal stat container, and
1199 * set up the struct device.
1201 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
1204 gdev->dev.bus = &gpio_bus_type;
1206 chip->gpiodev = gdev;
1208 gdev->dev.parent = chip->parent;
1209 gdev->dev.of_node = chip->parent->of_node;
1212 #ifdef CONFIG_OF_GPIO
1213 /* If the gpiochip has an assigned OF node this takes precedence */
1215 gdev->dev.of_node = chip->of_node;
1218 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1223 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1224 device_initialize(&gdev->dev);
1225 dev_set_drvdata(&gdev->dev, gdev);
1226 if (chip->parent && chip->parent->driver)
1227 gdev->owner = chip->parent->driver->owner;
1228 else if (chip->owner)
1229 /* TODO: remove chip->owner */
1230 gdev->owner = chip->owner;
1232 gdev->owner = THIS_MODULE;
1234 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
1240 if (chip->ngpio == 0) {
1241 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
1243 goto err_free_descs;
1246 gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);
1249 goto err_free_descs;
1252 gdev->ngpio = chip->ngpio;
1255 spin_lock_irqsave(&gpio_lock, flags);
1258 * TODO: this allocates a Linux GPIO number base in the global
1259 * GPIO numberspace for this chip. In the long run we want to
1260 * get *rid* of this numberspace and use only descriptors, but
1261 * it may be a pipe dream. It will not happen before we get rid
1262 * of the sysfs interface anyways.
1265 base = gpiochip_find_base(chip->ngpio);
1268 spin_unlock_irqrestore(&gpio_lock, flags);
1269 goto err_free_label;
1272 * TODO: it should not be necessary to reflect the assigned
1273 * base outside of the GPIO subsystem. Go over drivers and
1274 * see if anyone makes use of this, else drop this and assign
1281 status = gpiodev_add_to_list(gdev);
1283 spin_unlock_irqrestore(&gpio_lock, flags);
1284 goto err_free_label;
1287 spin_unlock_irqrestore(&gpio_lock, flags);
1289 for (i = 0; i < chip->ngpio; i++) {
1290 struct gpio_desc *desc = &gdev->descs[i];
1294 /* REVISIT: most hardware initializes GPIOs as inputs (often
1295 * with pullups enabled) so power usage is minimized. Linux
1296 * code should set the gpio direction first thing; but until
1297 * it does, and in case chip->get_direction is not set, we may
1298 * expose the wrong direction in sysfs.
1300 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
1303 #ifdef CONFIG_PINCTRL
1304 INIT_LIST_HEAD(&gdev->pin_ranges);
1307 status = gpiochip_set_desc_names(chip);
1309 goto err_remove_from_list;
1311 status = gpiochip_irqchip_init_valid_mask(chip);
1313 goto err_remove_from_list;
1315 status = gpiochip_init_valid_mask(chip);
1317 goto err_remove_irqchip_mask;
1319 status = gpiochip_add_irqchip(chip, lock_key, request_key);
1321 goto err_remove_chip;
1323 status = of_gpiochip_add(chip);
1325 goto err_remove_chip;
1327 acpi_gpiochip_add(chip);
1330 * By first adding the chardev, and then adding the device,
1331 * we get a device node entry in sysfs under
1332 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1333 * coldplug of device nodes and other udev business.
1334 * We can do this only if gpiolib has been initialized.
1335 * Otherwise, defer until later.
1337 if (gpiolib_initialized) {
1338 status = gpiochip_setup_dev(gdev);
1340 goto err_remove_chip;
1345 acpi_gpiochip_remove(chip);
1346 gpiochip_free_hogs(chip);
1347 of_gpiochip_remove(chip);
1348 gpiochip_free_valid_mask(chip);
1349 err_remove_irqchip_mask:
1350 gpiochip_irqchip_free_valid_mask(chip);
1351 err_remove_from_list:
1352 spin_lock_irqsave(&gpio_lock, flags);
1353 list_del(&gdev->list);
1354 spin_unlock_irqrestore(&gpio_lock, flags);
1356 kfree_const(gdev->label);
1360 ida_simple_remove(&gpio_ida, gdev->id);
1361 /* failures here can mean systems won't boot... */
1362 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
1363 gdev->base, gdev->base + gdev->ngpio - 1,
1364 chip->label ? : "generic");
1368 EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
1371 * gpiochip_get_data() - get per-subdriver data for the chip
1375 * The per-subdriver data for the chip.
1377 void *gpiochip_get_data(struct gpio_chip *chip)
1379 return chip->gpiodev->data;
1381 EXPORT_SYMBOL_GPL(gpiochip_get_data);
1384 * gpiochip_remove() - unregister a gpio_chip
1385 * @chip: the chip to unregister
1387 * A gpio_chip with any GPIOs still requested may not be removed.
1389 void gpiochip_remove(struct gpio_chip *chip)
1391 struct gpio_device *gdev = chip->gpiodev;
1392 struct gpio_desc *desc;
1393 unsigned long flags;
1395 bool requested = false;
1397 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
1398 gpiochip_sysfs_unregister(gdev);
1399 gpiochip_free_hogs(chip);
1400 /* Numb the device, cancelling all outstanding operations */
1402 gpiochip_irqchip_remove(chip);
1403 acpi_gpiochip_remove(chip);
1404 gpiochip_remove_pin_ranges(chip);
1405 of_gpiochip_remove(chip);
1406 gpiochip_free_valid_mask(chip);
1408 * We accept no more calls into the driver from this point, so
1409 * NULL the driver data pointer
1413 spin_lock_irqsave(&gpio_lock, flags);
1414 for (i = 0; i < gdev->ngpio; i++) {
1415 desc = &gdev->descs[i];
1416 if (test_bit(FLAG_REQUESTED, &desc->flags))
1419 spin_unlock_irqrestore(&gpio_lock, flags);
1422 dev_crit(&gdev->dev,
1423 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
1426 * The gpiochip side puts its use of the device to rest here:
1427 * if there are no userspace clients, the chardev and device will
1428 * be removed, else it will be dangling until the last user is
1431 cdev_device_del(&gdev->chrdev, &gdev->dev);
1432 put_device(&gdev->dev);
1434 EXPORT_SYMBOL_GPL(gpiochip_remove);
1436 static void devm_gpio_chip_release(struct device *dev, void *res)
1438 struct gpio_chip *chip = *(struct gpio_chip **)res;
1440 gpiochip_remove(chip);
1443 static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1446 struct gpio_chip **r = res;
1457 * devm_gpiochip_add_data() - Resource manager gpiochip_add_data()
1458 * @dev: the device pointer on which irq_chip belongs to.
1459 * @chip: the chip to register, with chip->base initialized
1460 * @data: driver-private data associated with this chip
1462 * Context: potentially before irqs will work
1464 * The gpio chip automatically be released when the device is unbound.
1467 * A negative errno if the chip can't be registered, such as because the
1468 * chip->base is invalid or already associated with a different chip.
1469 * Otherwise it returns zero as a success code.
1471 int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1474 struct gpio_chip **ptr;
1477 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1482 ret = gpiochip_add_data(chip, data);
1489 devres_add(dev, ptr);
1493 EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1496 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1497 * @dev: device for which which resource was allocated
1498 * @chip: the chip to remove
1500 * A gpio_chip with any GPIOs still requested may not be removed.
1502 void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1506 ret = devres_release(dev, devm_gpio_chip_release,
1507 devm_gpio_chip_match, chip);
1510 EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1513 * gpiochip_find() - iterator for locating a specific gpio_chip
1514 * @data: data to pass to match function
1515 * @match: Callback function to check gpio_chip
1517 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1518 * determined by a user supplied @match callback. The callback should return
1519 * 0 if the device doesn't match and non-zero if it does. If the callback is
1520 * non-zero, this function will return to the caller and not iterate over any
1523 struct gpio_chip *gpiochip_find(void *data,
1524 int (*match)(struct gpio_chip *chip,
1527 struct gpio_device *gdev;
1528 struct gpio_chip *chip = NULL;
1529 unsigned long flags;
1531 spin_lock_irqsave(&gpio_lock, flags);
1532 list_for_each_entry(gdev, &gpio_devices, list)
1533 if (gdev->chip && match(gdev->chip, data)) {
1538 spin_unlock_irqrestore(&gpio_lock, flags);
1542 EXPORT_SYMBOL_GPL(gpiochip_find);
1544 static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1546 const char *name = data;
1548 return !strcmp(chip->label, name);
1551 static struct gpio_chip *find_chip_by_name(const char *name)
1553 return gpiochip_find((void *)name, gpiochip_match_name);
1556 #ifdef CONFIG_GPIOLIB_IRQCHIP
1559 * The following is irqchip helper code for gpiochips.
1562 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1564 if (!gpiochip->irq.need_valid_mask)
1567 gpiochip->irq.valid_mask = gpiochip_allocate_mask(gpiochip);
1568 if (!gpiochip->irq.valid_mask)
1574 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1576 kfree(gpiochip->irq.valid_mask);
1577 gpiochip->irq.valid_mask = NULL;
1580 bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1581 unsigned int offset)
1583 if (!gpiochip_line_is_valid(gpiochip, offset))
1585 /* No mask means all valid */
1586 if (likely(!gpiochip->irq.valid_mask))
1588 return test_bit(offset, gpiochip->irq.valid_mask);
1590 EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
1593 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
1594 * @gpiochip: the gpiochip to set the irqchip chain to
1595 * @irqchip: the irqchip to chain to the gpiochip
1596 * @parent_irq: the irq number corresponding to the parent IRQ for this
1598 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1599 * coming out of the gpiochip. If the interrupt is nested rather than
1600 * cascaded, pass NULL in this handler argument
1602 static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip,
1603 struct irq_chip *irqchip,
1604 unsigned int parent_irq,
1605 irq_flow_handler_t parent_handler)
1607 unsigned int offset;
1609 if (!gpiochip->irq.domain) {
1610 chip_err(gpiochip, "called %s before setting up irqchip\n",
1615 if (parent_handler) {
1616 if (gpiochip->can_sleep) {
1618 "you cannot have chained interrupts on a "
1619 "chip that may sleep\n");
1623 * The parent irqchip is already using the chip_data for this
1624 * irqchip, so our callbacks simply use the handler_data.
1626 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1629 gpiochip->irq.parents = &parent_irq;
1630 gpiochip->irq.num_parents = 1;
1633 /* Set the parent IRQ for all affected IRQs */
1634 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1635 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1637 irq_set_parent(irq_find_mapping(gpiochip->irq.domain, offset),
1643 * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
1644 * @gpiochip: the gpiochip to set the irqchip chain to
1645 * @irqchip: the irqchip to chain to the gpiochip
1646 * @parent_irq: the irq number corresponding to the parent IRQ for this
1648 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1649 * coming out of the gpiochip. If the interrupt is nested rather than
1650 * cascaded, pass NULL in this handler argument
1652 void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1653 struct irq_chip *irqchip,
1654 unsigned int parent_irq,
1655 irq_flow_handler_t parent_handler)
1657 if (gpiochip->irq.threaded) {
1658 chip_err(gpiochip, "tried to chain a threaded gpiochip\n");
1662 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1665 EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1668 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1669 * @gpiochip: the gpiochip to set the irqchip nested handler to
1670 * @irqchip: the irqchip to nest to the gpiochip
1671 * @parent_irq: the irq number corresponding to the parent IRQ for this
1674 void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1675 struct irq_chip *irqchip,
1676 unsigned int parent_irq)
1678 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1681 EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1684 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1685 * @d: the irqdomain used by this irqchip
1686 * @irq: the global irq number used by this GPIO irqchip irq
1687 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1689 * This function will set up the mapping for a certain IRQ line on a
1690 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1691 * stored inside the gpiochip.
1693 int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1694 irq_hw_number_t hwirq)
1696 struct gpio_chip *chip = d->host_data;
1699 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
1702 irq_set_chip_data(irq, chip);
1704 * This lock class tells lockdep that GPIO irqs are in a different
1705 * category than their parents, so it won't report false recursion.
1707 irq_set_lockdep_class(irq, chip->irq.lock_key, chip->irq.request_key);
1708 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
1709 /* Chips that use nested thread handlers have them marked */
1710 if (chip->irq.threaded)
1711 irq_set_nested_thread(irq, 1);
1712 irq_set_noprobe(irq);
1714 if (chip->irq.num_parents == 1)
1715 err = irq_set_parent(irq, chip->irq.parents[0]);
1716 else if (chip->irq.map)
1717 err = irq_set_parent(irq, chip->irq.map[hwirq]);
1723 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1724 * is passed as default type.
1726 if (chip->irq.default_type != IRQ_TYPE_NONE)
1727 irq_set_irq_type(irq, chip->irq.default_type);
1731 EXPORT_SYMBOL_GPL(gpiochip_irq_map);
1733 void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1735 struct gpio_chip *chip = d->host_data;
1737 if (chip->irq.threaded)
1738 irq_set_nested_thread(irq, 0);
1739 irq_set_chip_and_handler(irq, NULL, NULL);
1740 irq_set_chip_data(irq, NULL);
1742 EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
1744 static const struct irq_domain_ops gpiochip_domain_ops = {
1745 .map = gpiochip_irq_map,
1746 .unmap = gpiochip_irq_unmap,
1747 /* Virtually all GPIO irqchips are twocell:ed */
1748 .xlate = irq_domain_xlate_twocell,
1751 static int gpiochip_irq_reqres(struct irq_data *d)
1753 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1755 if (!try_module_get(chip->gpiodev->owner))
1758 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
1760 "unable to lock HW IRQ %lu for IRQ\n",
1762 module_put(chip->gpiodev->owner);
1768 static void gpiochip_irq_relres(struct irq_data *d)
1770 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1772 gpiochip_unlock_as_irq(chip, d->hwirq);
1773 module_put(chip->gpiodev->owner);
1776 static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1778 if (!gpiochip_irqchip_irq_valid(chip, offset))
1781 return irq_create_mapping(chip->irq.domain, offset);
1785 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1786 * @gpiochip: the GPIO chip to add the IRQ chip to
1787 * @lock_key: lockdep class for IRQ lock
1788 * @request_key: lockdep class for IRQ request
1790 static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
1791 struct lock_class_key *lock_key,
1792 struct lock_class_key *request_key)
1794 struct irq_chip *irqchip = gpiochip->irq.chip;
1795 const struct irq_domain_ops *ops;
1796 struct device_node *np;
1803 if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
1804 chip_err(gpiochip, "you cannot have chained interrupts on a "
1805 "chip that may sleep\n");
1809 np = gpiochip->gpiodev->dev.of_node;
1810 type = gpiochip->irq.default_type;
1813 * Specifying a default trigger is a terrible idea if DT or ACPI is
1814 * used to configure the interrupts, as you may end up with
1815 * conflicting triggers. Tell the user, and reset to NONE.
1817 if (WARN(np && type != IRQ_TYPE_NONE,
1818 "%s: Ignoring %u default trigger\n", np->full_name, type))
1819 type = IRQ_TYPE_NONE;
1821 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1822 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1823 "Ignoring %u default trigger\n", type);
1824 type = IRQ_TYPE_NONE;
1827 gpiochip->to_irq = gpiochip_to_irq;
1828 gpiochip->irq.default_type = type;
1829 gpiochip->irq.lock_key = lock_key;
1830 gpiochip->irq.request_key = request_key;
1832 if (gpiochip->irq.domain_ops)
1833 ops = gpiochip->irq.domain_ops;
1835 ops = &gpiochip_domain_ops;
1837 gpiochip->irq.domain = irq_domain_add_simple(np, gpiochip->ngpio,
1838 gpiochip->irq.first,
1840 if (!gpiochip->irq.domain)
1844 * It is possible for a driver to override this, but only if the
1845 * alternative functions are both implemented.
1847 if (!irqchip->irq_request_resources &&
1848 !irqchip->irq_release_resources) {
1849 irqchip->irq_request_resources = gpiochip_irq_reqres;
1850 irqchip->irq_release_resources = gpiochip_irq_relres;
1853 if (gpiochip->irq.parent_handler) {
1854 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
1856 for (i = 0; i < gpiochip->irq.num_parents; i++) {
1858 * The parent IRQ chip is already using the chip_data
1859 * for this IRQ chip, so our callbacks simply use the
1862 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
1863 gpiochip->irq.parent_handler,
1868 acpi_gpiochip_request_interrupts(gpiochip);
1874 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1875 * @gpiochip: the gpiochip to remove the irqchip from
1877 * This is called only from gpiochip_remove()
1879 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1881 unsigned int offset;
1883 acpi_gpiochip_free_interrupts(gpiochip);
1885 if (gpiochip->irq.chip && gpiochip->irq.parent_handler) {
1886 struct gpio_irq_chip *irq = &gpiochip->irq;
1889 for (i = 0; i < irq->num_parents; i++)
1890 irq_set_chained_handler_and_data(irq->parents[i],
1894 /* Remove all IRQ mappings and delete the domain */
1895 if (gpiochip->irq.domain) {
1898 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1899 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1902 irq = irq_find_mapping(gpiochip->irq.domain, offset);
1903 irq_dispose_mapping(irq);
1906 irq_domain_remove(gpiochip->irq.domain);
1909 if (gpiochip->irq.chip) {
1910 gpiochip->irq.chip->irq_request_resources = NULL;
1911 gpiochip->irq.chip->irq_release_resources = NULL;
1912 gpiochip->irq.chip = NULL;
1915 gpiochip_irqchip_free_valid_mask(gpiochip);
1919 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
1920 * @gpiochip: the gpiochip to add the irqchip to
1921 * @irqchip: the irqchip to add to the gpiochip
1922 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1923 * allocate gpiochip irqs from
1924 * @handler: the irq handler to use (often a predefined irq core function)
1925 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1926 * to have the core avoid setting up any default type in the hardware.
1927 * @threaded: whether this irqchip uses a nested thread handler
1928 * @lock_key: lockdep class for IRQ lock
1929 * @request_key: lockdep class for IRQ request
1931 * This function closely associates a certain irqchip with a certain
1932 * gpiochip, providing an irq domain to translate the local IRQs to
1933 * global irqs in the gpiolib core, and making sure that the gpiochip
1934 * is passed as chip data to all related functions. Driver callbacks
1935 * need to use gpiochip_get_data() to get their local state containers back
1936 * from the gpiochip passed as chip data. An irqdomain will be stored
1937 * in the gpiochip that shall be used by the driver to handle IRQ number
1938 * translation. The gpiochip will need to be initialized and registered
1939 * before calling this function.
1941 * This function will handle two cell:ed simple IRQs and assumes all
1942 * the pins on the gpiochip can generate a unique IRQ. Everything else
1943 * need to be open coded.
1945 int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
1946 struct irq_chip *irqchip,
1947 unsigned int first_irq,
1948 irq_flow_handler_t handler,
1951 struct lock_class_key *lock_key,
1952 struct lock_class_key *request_key)
1954 struct device_node *of_node;
1956 if (!gpiochip || !irqchip)
1959 if (!gpiochip->parent) {
1960 pr_err("missing gpiochip .dev parent pointer\n");
1963 gpiochip->irq.threaded = threaded;
1964 of_node = gpiochip->parent->of_node;
1965 #ifdef CONFIG_OF_GPIO
1967 * If the gpiochip has an assigned OF node this takes precedence
1968 * FIXME: get rid of this and use gpiochip->parent->of_node
1971 if (gpiochip->of_node)
1972 of_node = gpiochip->of_node;
1975 * Specifying a default trigger is a terrible idea if DT or ACPI is
1976 * used to configure the interrupts, as you may end-up with
1977 * conflicting triggers. Tell the user, and reset to NONE.
1979 if (WARN(of_node && type != IRQ_TYPE_NONE,
1980 "%pOF: Ignoring %d default trigger\n", of_node, type))
1981 type = IRQ_TYPE_NONE;
1982 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1983 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1984 "Ignoring %d default trigger\n", type);
1985 type = IRQ_TYPE_NONE;
1988 gpiochip->irq.chip = irqchip;
1989 gpiochip->irq.handler = handler;
1990 gpiochip->irq.default_type = type;
1991 gpiochip->to_irq = gpiochip_to_irq;
1992 gpiochip->irq.lock_key = lock_key;
1993 gpiochip->irq.request_key = request_key;
1994 gpiochip->irq.domain = irq_domain_add_simple(of_node,
1995 gpiochip->ngpio, first_irq,
1996 &gpiochip_domain_ops, gpiochip);
1997 if (!gpiochip->irq.domain) {
1998 gpiochip->irq.chip = NULL;
2003 * It is possible for a driver to override this, but only if the
2004 * alternative functions are both implemented.
2006 if (!irqchip->irq_request_resources &&
2007 !irqchip->irq_release_resources) {
2008 irqchip->irq_request_resources = gpiochip_irq_reqres;
2009 irqchip->irq_release_resources = gpiochip_irq_relres;
2012 acpi_gpiochip_request_interrupts(gpiochip);
2016 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
2018 #else /* CONFIG_GPIOLIB_IRQCHIP */
2020 static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
2021 struct lock_class_key *lock_key,
2022 struct lock_class_key *request_key)
2027 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
2028 static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
2032 static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
2035 #endif /* CONFIG_GPIOLIB_IRQCHIP */
2038 * gpiochip_generic_request() - request the gpio function for a pin
2039 * @chip: the gpiochip owning the GPIO
2040 * @offset: the offset of the GPIO to request for GPIO function
2042 int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
2044 return pinctrl_gpio_request(chip->gpiodev->base + offset);
2046 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
2049 * gpiochip_generic_free() - free the gpio function from a pin
2050 * @chip: the gpiochip to request the gpio function for
2051 * @offset: the offset of the GPIO to free from GPIO function
2053 void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
2055 pinctrl_gpio_free(chip->gpiodev->base + offset);
2057 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
2060 * gpiochip_generic_config() - apply configuration for a pin
2061 * @chip: the gpiochip owning the GPIO
2062 * @offset: the offset of the GPIO to apply the configuration
2063 * @config: the configuration to be applied
2065 int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
2066 unsigned long config)
2068 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
2070 EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2072 #ifdef CONFIG_PINCTRL
2075 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2076 * @chip: the gpiochip to add the range for
2077 * @pctldev: the pin controller to map to
2078 * @gpio_offset: the start offset in the current gpio_chip number space
2079 * @pin_group: name of the pin group inside the pin controller
2081 * Calling this function directly from a DeviceTree-supported
2082 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2083 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2084 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
2086 int gpiochip_add_pingroup_range(struct gpio_chip *chip,
2087 struct pinctrl_dev *pctldev,
2088 unsigned int gpio_offset, const char *pin_group)
2090 struct gpio_pin_range *pin_range;
2091 struct gpio_device *gdev = chip->gpiodev;
2094 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2096 chip_err(chip, "failed to allocate pin ranges\n");
2100 /* Use local offset as range ID */
2101 pin_range->range.id = gpio_offset;
2102 pin_range->range.gc = chip;
2103 pin_range->range.name = chip->label;
2104 pin_range->range.base = gdev->base + gpio_offset;
2105 pin_range->pctldev = pctldev;
2107 ret = pinctrl_get_group_pins(pctldev, pin_group,
2108 &pin_range->range.pins,
2109 &pin_range->range.npins);
2115 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2117 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2118 gpio_offset, gpio_offset + pin_range->range.npins - 1,
2119 pinctrl_dev_get_devname(pctldev), pin_group);
2121 list_add_tail(&pin_range->node, &gdev->pin_ranges);
2125 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2128 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2129 * @chip: the gpiochip to add the range for
2130 * @pinctl_name: the dev_name() of the pin controller to map to
2131 * @gpio_offset: the start offset in the current gpio_chip number space
2132 * @pin_offset: the start offset in the pin controller number space
2133 * @npins: the number of pins from the offset of each pin space (GPIO and
2134 * pin controller) to accumulate in this range
2137 * 0 on success, or a negative error-code on failure.
2139 * Calling this function directly from a DeviceTree-supported
2140 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2141 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2142 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
2144 int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
2145 unsigned int gpio_offset, unsigned int pin_offset,
2148 struct gpio_pin_range *pin_range;
2149 struct gpio_device *gdev = chip->gpiodev;
2152 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2154 chip_err(chip, "failed to allocate pin ranges\n");
2158 /* Use local offset as range ID */
2159 pin_range->range.id = gpio_offset;
2160 pin_range->range.gc = chip;
2161 pin_range->range.name = chip->label;
2162 pin_range->range.base = gdev->base + gpio_offset;
2163 pin_range->range.pin_base = pin_offset;
2164 pin_range->range.npins = npins;
2165 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
2167 if (IS_ERR(pin_range->pctldev)) {
2168 ret = PTR_ERR(pin_range->pctldev);
2169 chip_err(chip, "could not create pin range\n");
2173 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2174 gpio_offset, gpio_offset + npins - 1,
2176 pin_offset, pin_offset + npins - 1);
2178 list_add_tail(&pin_range->node, &gdev->pin_ranges);
2182 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
2185 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2186 * @chip: the chip to remove all the mappings for
2188 void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
2190 struct gpio_pin_range *pin_range, *tmp;
2191 struct gpio_device *gdev = chip->gpiodev;
2193 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
2194 list_del(&pin_range->node);
2195 pinctrl_remove_gpio_range(pin_range->pctldev,
2200 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2202 #endif /* CONFIG_PINCTRL */
2204 /* These "optional" allocation calls help prevent drivers from stomping
2205 * on each other, and help provide better diagnostics in debugfs.
2206 * They're called even less than the "set direction" calls.
2208 static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
2210 struct gpio_chip *chip = desc->gdev->chip;
2212 unsigned long flags;
2214 spin_lock_irqsave(&gpio_lock, flags);
2216 /* NOTE: gpio_request() can be called in early boot,
2217 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
2220 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2221 desc_set_label(desc, label ? : "?");
2228 if (chip->request) {
2229 /* chip->request may sleep */
2230 spin_unlock_irqrestore(&gpio_lock, flags);
2231 status = chip->request(chip, gpio_chip_hwgpio(desc));
2232 spin_lock_irqsave(&gpio_lock, flags);
2235 desc_set_label(desc, NULL);
2236 clear_bit(FLAG_REQUESTED, &desc->flags);
2240 if (chip->get_direction) {
2241 /* chip->get_direction may sleep */
2242 spin_unlock_irqrestore(&gpio_lock, flags);
2243 gpiod_get_direction(desc);
2244 spin_lock_irqsave(&gpio_lock, flags);
2247 spin_unlock_irqrestore(&gpio_lock, flags);
2252 * This descriptor validation needs to be inserted verbatim into each
2253 * function taking a descriptor, so we need to use a preprocessor
2254 * macro to avoid endless duplication. If the desc is NULL it is an
2255 * optional GPIO and calls should just bail out.
2257 static int validate_desc(const struct gpio_desc *desc, const char *func)
2262 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2263 return PTR_ERR(desc);
2266 pr_warn("%s: invalid GPIO (no device)\n", func);
2269 if (!desc->gdev->chip) {
2270 dev_warn(&desc->gdev->dev,
2271 "%s: backing chip is gone\n", func);
2277 #define VALIDATE_DESC(desc) do { \
2278 int __valid = validate_desc(desc, __func__); \
2283 #define VALIDATE_DESC_VOID(desc) do { \
2284 int __valid = validate_desc(desc, __func__); \
2289 int gpiod_request(struct gpio_desc *desc, const char *label)
2291 int status = -EPROBE_DEFER;
2292 struct gpio_device *gdev;
2294 VALIDATE_DESC(desc);
2297 if (try_module_get(gdev->owner)) {
2298 status = gpiod_request_commit(desc, label);
2300 module_put(gdev->owner);
2302 get_device(&gdev->dev);
2306 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
2311 static bool gpiod_free_commit(struct gpio_desc *desc)
2314 unsigned long flags;
2315 struct gpio_chip *chip;
2319 gpiod_unexport(desc);
2321 spin_lock_irqsave(&gpio_lock, flags);
2323 chip = desc->gdev->chip;
2324 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2326 spin_unlock_irqrestore(&gpio_lock, flags);
2327 might_sleep_if(chip->can_sleep);
2328 chip->free(chip, gpio_chip_hwgpio(desc));
2329 spin_lock_irqsave(&gpio_lock, flags);
2331 desc_set_label(desc, NULL);
2332 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
2333 clear_bit(FLAG_REQUESTED, &desc->flags);
2334 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
2335 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
2336 clear_bit(FLAG_IS_HOGGED, &desc->flags);
2340 spin_unlock_irqrestore(&gpio_lock, flags);
2344 void gpiod_free(struct gpio_desc *desc)
2346 if (desc && desc->gdev && gpiod_free_commit(desc)) {
2347 module_put(desc->gdev->owner);
2348 put_device(&desc->gdev->dev);
2350 WARN_ON(extra_checks);
2355 * gpiochip_is_requested - return string iff signal was requested
2356 * @chip: controller managing the signal
2357 * @offset: of signal within controller's 0..(ngpio - 1) range
2359 * Returns NULL if the GPIO is not currently requested, else a string.
2360 * The string returned is the label passed to gpio_request(); if none has been
2361 * passed it is a meaningless, non-NULL constant.
2363 * This function is for use by GPIO controller drivers. The label can
2364 * help with diagnostics, and knowing that the signal is used as a GPIO
2365 * can help avoid accidentally multiplexing it to another controller.
2367 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2369 struct gpio_desc *desc;
2371 if (offset >= chip->ngpio)
2374 desc = &chip->gpiodev->descs[offset];
2376 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
2380 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2383 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2385 * @hwnum: hardware number of the GPIO for which to request the descriptor
2386 * @label: label for the GPIO
2388 * Function allows GPIO chip drivers to request and use their own GPIO
2389 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2390 * function will not increase reference count of the GPIO chip module. This
2391 * allows the GPIO chip module to be unloaded as needed (we assume that the
2392 * GPIO chip driver handles freeing the GPIOs it has requested).
2395 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2398 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2401 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2405 chip_err(chip, "failed to get GPIO descriptor\n");
2409 err = gpiod_request_commit(desc, label);
2411 return ERR_PTR(err);
2415 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
2418 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2419 * @desc: GPIO descriptor to free
2421 * Function frees the given GPIO requested previously with
2422 * gpiochip_request_own_desc().
2424 void gpiochip_free_own_desc(struct gpio_desc *desc)
2427 gpiod_free_commit(desc);
2429 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
2432 * Drivers MUST set GPIO direction before making get/set calls. In
2433 * some cases this is done in early boot, before IRQs are enabled.
2435 * As a rule these aren't called more than once (except for drivers
2436 * using the open-drain emulation idiom) so these are natural places
2437 * to accumulate extra debugging checks. Note that we can't (yet)
2438 * rely on gpio_request() having been called beforehand.
2442 * gpiod_direction_input - set the GPIO direction to input
2443 * @desc: GPIO to set to input
2445 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2446 * be called safely on it.
2448 * Return 0 in case of success, else an error code.
2450 int gpiod_direction_input(struct gpio_desc *desc)
2452 struct gpio_chip *chip;
2453 int status = -EINVAL;
2455 VALIDATE_DESC(desc);
2456 chip = desc->gdev->chip;
2458 if (!chip->get || !chip->direction_input) {
2460 "%s: missing get() or direction_input() operations\n",
2465 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
2467 clear_bit(FLAG_IS_OUT, &desc->flags);
2469 trace_gpio_direction(desc_to_gpio(desc), 1, status);
2473 EXPORT_SYMBOL_GPL(gpiod_direction_input);
2475 static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset,
2476 enum pin_config_param mode)
2478 unsigned long config = { PIN_CONF_PACKED(mode, 0) };
2480 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2483 static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
2485 struct gpio_chip *gc = desc->gdev->chip;
2489 if (!gc->set || !gc->direction_output) {
2491 "%s: missing set() or direction_output() operations\n",
2496 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
2498 set_bit(FLAG_IS_OUT, &desc->flags);
2499 trace_gpio_value(desc_to_gpio(desc), 0, val);
2500 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2505 * gpiod_direction_output_raw - set the GPIO direction to output
2506 * @desc: GPIO to set to output
2507 * @value: initial output value of the GPIO
2509 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2510 * be called safely on it. The initial value of the output must be specified
2511 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2513 * Return 0 in case of success, else an error code.
2515 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2517 VALIDATE_DESC(desc);
2518 return gpiod_direction_output_raw_commit(desc, value);
2520 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2523 * gpiod_direction_output - set the GPIO direction to output
2524 * @desc: GPIO to set to output
2525 * @value: initial output value of the GPIO
2527 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2528 * be called safely on it. The initial value of the output must be specified
2529 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2532 * Return 0 in case of success, else an error code.
2534 int gpiod_direction_output(struct gpio_desc *desc, int value)
2536 struct gpio_chip *gc;
2539 VALIDATE_DESC(desc);
2540 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2545 /* GPIOs used for IRQs shall not be set as output */
2546 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2548 "%s: tried to set a GPIO tied to an IRQ as output\n",
2553 gc = desc->gdev->chip;
2554 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2555 /* First see if we can enable open drain in hardware */
2556 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2557 PIN_CONFIG_DRIVE_OPEN_DRAIN);
2559 goto set_output_value;
2560 /* Emulate open drain by not actively driving the line high */
2562 return gpiod_direction_input(desc);
2564 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2565 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2566 PIN_CONFIG_DRIVE_OPEN_SOURCE);
2568 goto set_output_value;
2569 /* Emulate open source by not actively driving the line low */
2571 return gpiod_direction_input(desc);
2573 gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2574 PIN_CONFIG_DRIVE_PUSH_PULL);
2578 return gpiod_direction_output_raw_commit(desc, value);
2580 EXPORT_SYMBOL_GPL(gpiod_direction_output);
2583 * gpiod_set_debounce - sets @debounce time for a GPIO
2584 * @desc: descriptor of the GPIO for which to set debounce time
2585 * @debounce: debounce time in microseconds
2588 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2591 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
2593 struct gpio_chip *chip;
2594 unsigned long config;
2596 VALIDATE_DESC(desc);
2597 chip = desc->gdev->chip;
2598 if (!chip->set || !chip->set_config) {
2600 "%s: missing set() or set_config() operations\n",
2605 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2606 return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
2608 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
2611 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2612 * @desc: descriptor of the GPIO for which to configure persistence
2613 * @transitory: True to lose state on suspend or reset, false for persistence
2616 * 0 on success, otherwise a negative error code.
2618 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2620 struct gpio_chip *chip;
2621 unsigned long packed;
2625 VALIDATE_DESC(desc);
2627 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2628 * persistence state.
2631 set_bit(FLAG_TRANSITORY, &desc->flags);
2633 clear_bit(FLAG_TRANSITORY, &desc->flags);
2635 /* If the driver supports it, set the persistence state now */
2636 chip = desc->gdev->chip;
2637 if (!chip->set_config)
2640 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
2642 gpio = gpio_chip_hwgpio(desc);
2643 rc = chip->set_config(chip, gpio, packed);
2644 if (rc == -ENOTSUPP) {
2645 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
2652 EXPORT_SYMBOL_GPL(gpiod_set_transitory);
2655 * gpiod_is_active_low - test whether a GPIO is active-low or not
2656 * @desc: the gpio descriptor to test
2658 * Returns 1 if the GPIO is active-low, 0 otherwise.
2660 int gpiod_is_active_low(const struct gpio_desc *desc)
2662 VALIDATE_DESC(desc);
2663 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
2665 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
2667 /* I/O calls are only valid after configuration completed; the relevant
2668 * "is this a valid GPIO" error checks should already have been done.
2670 * "Get" operations are often inlinable as reading a pin value register,
2671 * and masking the relevant bit in that register.
2673 * When "set" operations are inlinable, they involve writing that mask to
2674 * one register to set a low value, or a different register to set it high.
2675 * Otherwise locking is needed, so there may be little value to inlining.
2677 *------------------------------------------------------------------------
2679 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2680 * have requested the GPIO. That can include implicit requesting by
2681 * a direction setting call. Marking a gpio as requested locks its chip
2682 * in memory, guaranteeing that these table lookups need no more locking
2683 * and that gpiochip_remove() will fail.
2685 * REVISIT when debugging, consider adding some instrumentation to ensure
2686 * that the GPIO was actually requested.
2689 static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
2691 struct gpio_chip *chip;
2695 chip = desc->gdev->chip;
2696 offset = gpio_chip_hwgpio(desc);
2697 value = chip->get ? chip->get(chip, offset) : -EIO;
2698 value = value < 0 ? value : !!value;
2699 trace_gpio_value(desc_to_gpio(desc), 1, value);
2703 static int gpio_chip_get_multiple(struct gpio_chip *chip,
2704 unsigned long *mask, unsigned long *bits)
2706 if (chip->get_multiple) {
2707 return chip->get_multiple(chip, mask, bits);
2708 } else if (chip->get) {
2711 for_each_set_bit(i, mask, chip->ngpio) {
2712 value = chip->get(chip, i);
2715 __assign_bit(i, bits, value);
2722 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2723 unsigned int array_size,
2724 struct gpio_desc **desc_array,
2729 while (i < array_size) {
2730 struct gpio_chip *chip = desc_array[i]->gdev->chip;
2731 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2732 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2736 WARN_ON(chip->can_sleep);
2738 /* collect all inputs belonging to the same chip */
2740 memset(mask, 0, sizeof(mask));
2742 const struct gpio_desc *desc = desc_array[i];
2743 int hwgpio = gpio_chip_hwgpio(desc);
2745 __set_bit(hwgpio, mask);
2747 } while ((i < array_size) &&
2748 (desc_array[i]->gdev->chip == chip));
2750 ret = gpio_chip_get_multiple(chip, mask, bits);
2754 for (j = first; j < i; j++) {
2755 const struct gpio_desc *desc = desc_array[j];
2756 int hwgpio = gpio_chip_hwgpio(desc);
2757 int value = test_bit(hwgpio, bits);
2759 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2761 value_array[j] = value;
2762 trace_gpio_value(desc_to_gpio(desc), 1, value);
2769 * gpiod_get_raw_value() - return a gpio's raw value
2770 * @desc: gpio whose value will be returned
2772 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2773 * its ACTIVE_LOW status, or negative errno on failure.
2775 * This function should be called from contexts where we cannot sleep, and will
2776 * complain if the GPIO chip functions potentially sleep.
2778 int gpiod_get_raw_value(const struct gpio_desc *desc)
2780 VALIDATE_DESC(desc);
2781 /* Should be using gpio_get_value_cansleep() */
2782 WARN_ON(desc->gdev->chip->can_sleep);
2783 return gpiod_get_raw_value_commit(desc);
2785 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
2788 * gpiod_get_value() - return a gpio's value
2789 * @desc: gpio whose value will be returned
2791 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2792 * account, or negative errno on failure.
2794 * This function should be called from contexts where we cannot sleep, and will
2795 * complain if the GPIO chip functions potentially sleep.
2797 int gpiod_get_value(const struct gpio_desc *desc)
2801 VALIDATE_DESC(desc);
2802 /* Should be using gpio_get_value_cansleep() */
2803 WARN_ON(desc->gdev->chip->can_sleep);
2805 value = gpiod_get_raw_value_commit(desc);
2809 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2814 EXPORT_SYMBOL_GPL(gpiod_get_value);
2817 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2818 * @array_size: number of elements in the descriptor / value arrays
2819 * @desc_array: array of GPIO descriptors whose values will be read
2820 * @value_array: array to store the read values
2822 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2823 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2824 * else an error code.
2826 * This function should be called from contexts where we cannot sleep,
2827 * and it will complain if the GPIO chip functions potentially sleep.
2829 int gpiod_get_raw_array_value(unsigned int array_size,
2830 struct gpio_desc **desc_array, int *value_array)
2834 return gpiod_get_array_value_complex(true, false, array_size,
2835 desc_array, value_array);
2837 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2840 * gpiod_get_array_value() - read values from an array of GPIOs
2841 * @array_size: number of elements in the descriptor / value arrays
2842 * @desc_array: array of GPIO descriptors whose values will be read
2843 * @value_array: array to store the read values
2845 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2846 * into account. Return 0 in case of success, else an error code.
2848 * This function should be called from contexts where we cannot sleep,
2849 * and it will complain if the GPIO chip functions potentially sleep.
2851 int gpiod_get_array_value(unsigned int array_size,
2852 struct gpio_desc **desc_array, int *value_array)
2856 return gpiod_get_array_value_complex(false, false, array_size,
2857 desc_array, value_array);
2859 EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2862 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
2863 * @desc: gpio descriptor whose state need to be set.
2864 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2866 static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
2869 struct gpio_chip *chip = desc->gdev->chip;
2870 int offset = gpio_chip_hwgpio(desc);
2873 err = chip->direction_input(chip, offset);
2875 clear_bit(FLAG_IS_OUT, &desc->flags);
2877 err = chip->direction_output(chip, offset, 0);
2879 set_bit(FLAG_IS_OUT, &desc->flags);
2881 trace_gpio_direction(desc_to_gpio(desc), value, err);
2884 "%s: Error in set_value for open drain err %d\n",
2889 * _gpio_set_open_source_value() - Set the open source gpio's value.
2890 * @desc: gpio descriptor whose state need to be set.
2891 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2893 static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
2896 struct gpio_chip *chip = desc->gdev->chip;
2897 int offset = gpio_chip_hwgpio(desc);
2900 err = chip->direction_output(chip, offset, 1);
2902 set_bit(FLAG_IS_OUT, &desc->flags);
2904 err = chip->direction_input(chip, offset);
2906 clear_bit(FLAG_IS_OUT, &desc->flags);
2908 trace_gpio_direction(desc_to_gpio(desc), !value, err);
2911 "%s: Error in set_value for open source err %d\n",
2915 static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
2917 struct gpio_chip *chip;
2919 chip = desc->gdev->chip;
2920 trace_gpio_value(desc_to_gpio(desc), 0, value);
2921 chip->set(chip, gpio_chip_hwgpio(desc), value);
2925 * set multiple outputs on the same chip;
2926 * use the chip's set_multiple function if available;
2927 * otherwise set the outputs sequentially;
2928 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2929 * defines which outputs are to be changed
2930 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2931 * defines the values the outputs specified by mask are to be set to
2933 static void gpio_chip_set_multiple(struct gpio_chip *chip,
2934 unsigned long *mask, unsigned long *bits)
2936 if (chip->set_multiple) {
2937 chip->set_multiple(chip, mask, bits);
2941 /* set outputs if the corresponding mask bit is set */
2942 for_each_set_bit(i, mask, chip->ngpio)
2943 chip->set(chip, i, test_bit(i, bits));
2947 void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2948 unsigned int array_size,
2949 struct gpio_desc **desc_array,
2954 while (i < array_size) {
2955 struct gpio_chip *chip = desc_array[i]->gdev->chip;
2956 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2957 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2961 WARN_ON(chip->can_sleep);
2963 memset(mask, 0, sizeof(mask));
2965 struct gpio_desc *desc = desc_array[i];
2966 int hwgpio = gpio_chip_hwgpio(desc);
2967 int value = value_array[i];
2969 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2971 trace_gpio_value(desc_to_gpio(desc), 0, value);
2973 * collect all normal outputs belonging to the same chip
2974 * open drain and open source outputs are set individually
2976 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
2977 gpio_set_open_drain_value_commit(desc, value);
2978 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
2979 gpio_set_open_source_value_commit(desc, value);
2981 __set_bit(hwgpio, mask);
2983 __set_bit(hwgpio, bits);
2985 __clear_bit(hwgpio, bits);
2989 } while ((i < array_size) &&
2990 (desc_array[i]->gdev->chip == chip));
2991 /* push collected bits to outputs */
2993 gpio_chip_set_multiple(chip, mask, bits);
2998 * gpiod_set_raw_value() - assign a gpio's raw value
2999 * @desc: gpio whose value will be assigned
3000 * @value: value to assign
3002 * Set the raw value of the GPIO, i.e. the value of its physical line without
3003 * regard for its ACTIVE_LOW status.
3005 * This function should be called from contexts where we cannot sleep, and will
3006 * complain if the GPIO chip functions potentially sleep.
3008 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
3010 VALIDATE_DESC_VOID(desc);
3011 /* Should be using gpiod_set_value_cansleep() */
3012 WARN_ON(desc->gdev->chip->can_sleep);
3013 gpiod_set_raw_value_commit(desc, value);
3015 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
3018 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3019 * @desc: the descriptor to set the value on
3020 * @value: value to set
3022 * This sets the value of a GPIO line backing a descriptor, applying
3023 * different semantic quirks like active low and open drain/source
3026 static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3028 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3030 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3031 gpio_set_open_drain_value_commit(desc, value);
3032 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3033 gpio_set_open_source_value_commit(desc, value);
3035 gpiod_set_raw_value_commit(desc, value);
3039 * gpiod_set_value() - assign a gpio's value
3040 * @desc: gpio whose value will be assigned
3041 * @value: value to assign
3043 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3044 * OPEN_DRAIN and OPEN_SOURCE flags into account.
3046 * This function should be called from contexts where we cannot sleep, and will
3047 * complain if the GPIO chip functions potentially sleep.
3049 void gpiod_set_value(struct gpio_desc *desc, int value)
3051 VALIDATE_DESC_VOID(desc);
3052 WARN_ON(desc->gdev->chip->can_sleep);
3053 gpiod_set_value_nocheck(desc, value);
3055 EXPORT_SYMBOL_GPL(gpiod_set_value);
3058 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
3059 * @array_size: number of elements in the descriptor / value arrays
3060 * @desc_array: array of GPIO descriptors whose values will be assigned
3061 * @value_array: array of values to assign
3063 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3064 * without regard for their ACTIVE_LOW status.
3066 * This function should be called from contexts where we cannot sleep, and will
3067 * complain if the GPIO chip functions potentially sleep.
3069 void gpiod_set_raw_array_value(unsigned int array_size,
3070 struct gpio_desc **desc_array, int *value_array)
3074 gpiod_set_array_value_complex(true, false, array_size, desc_array,
3077 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
3080 * gpiod_set_array_value() - assign values to an array of GPIOs
3081 * @array_size: number of elements in the descriptor / value arrays
3082 * @desc_array: array of GPIO descriptors whose values will be assigned
3083 * @value_array: array of values to assign
3085 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3088 * This function should be called from contexts where we cannot sleep, and will
3089 * complain if the GPIO chip functions potentially sleep.
3091 void gpiod_set_array_value(unsigned int array_size,
3092 struct gpio_desc **desc_array, int *value_array)
3096 gpiod_set_array_value_complex(false, false, array_size, desc_array,
3099 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
3102 * gpiod_cansleep() - report whether gpio value access may sleep
3103 * @desc: gpio to check
3106 int gpiod_cansleep(const struct gpio_desc *desc)
3108 VALIDATE_DESC(desc);
3109 return desc->gdev->chip->can_sleep;
3111 EXPORT_SYMBOL_GPL(gpiod_cansleep);
3114 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3115 * @desc: gpio whose IRQ will be returned (already requested)
3117 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3120 int gpiod_to_irq(const struct gpio_desc *desc)
3122 struct gpio_chip *chip;
3126 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3127 * requires this function to not return zero on an invalid descriptor
3128 * but rather a negative error number.
3130 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
3133 chip = desc->gdev->chip;
3134 offset = gpio_chip_hwgpio(desc);
3136 int retirq = chip->to_irq(chip, offset);
3138 /* Zero means NO_IRQ */
3146 EXPORT_SYMBOL_GPL(gpiod_to_irq);
3149 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3150 * @chip: the chip the GPIO to lock belongs to
3151 * @offset: the offset of the GPIO to lock as IRQ
3153 * This is used directly by GPIO drivers that want to lock down
3154 * a certain GPIO line to be used for IRQs.
3156 int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
3158 struct gpio_desc *desc;
3160 desc = gpiochip_get_desc(chip, offset);
3162 return PTR_ERR(desc);
3165 * If it's fast: flush the direction setting if something changed
3168 if (!chip->can_sleep && chip->get_direction) {
3169 int dir = chip->get_direction(chip, offset);
3172 clear_bit(FLAG_IS_OUT, &desc->flags);
3174 set_bit(FLAG_IS_OUT, &desc->flags);
3177 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
3179 "%s: tried to flag a GPIO set as output for IRQ\n",
3184 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
3187 * If the consumer has not set up a label (such as when the
3188 * IRQ is referenced from .to_irq()) we set up a label here
3189 * so it is clear this is used as an interrupt.
3192 desc_set_label(desc, "interrupt");
3196 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
3199 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3200 * @chip: the chip the GPIO to lock belongs to
3201 * @offset: the offset of the GPIO to lock as IRQ
3203 * This is used directly by GPIO drivers that want to indicate
3204 * that a certain GPIO is no longer used exclusively for IRQ.
3206 void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
3208 struct gpio_desc *desc;
3210 desc = gpiochip_get_desc(chip, offset);
3214 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3216 /* If we only had this marking, erase it */
3217 if (desc->label && !strcmp(desc->label, "interrupt"))
3218 desc_set_label(desc, NULL);
3220 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
3222 bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
3224 if (offset >= chip->ngpio)
3227 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
3229 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3231 bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
3233 if (offset >= chip->ngpio)
3236 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
3238 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3240 bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
3242 if (offset >= chip->ngpio)
3245 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
3247 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3249 bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
3251 if (offset >= chip->ngpio)
3254 return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
3256 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3259 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3260 * @desc: gpio whose value will be returned
3262 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
3263 * its ACTIVE_LOW status, or negative errno on failure.
3265 * This function is to be called from contexts that can sleep.
3267 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
3269 might_sleep_if(extra_checks);
3270 VALIDATE_DESC(desc);
3271 return gpiod_get_raw_value_commit(desc);
3273 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
3276 * gpiod_get_value_cansleep() - return a gpio's value
3277 * @desc: gpio whose value will be returned
3279 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3280 * account, or negative errno on failure.
3282 * This function is to be called from contexts that can sleep.
3284 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
3288 might_sleep_if(extra_checks);
3289 VALIDATE_DESC(desc);
3290 value = gpiod_get_raw_value_commit(desc);
3294 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3299 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
3302 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3303 * @array_size: number of elements in the descriptor / value arrays
3304 * @desc_array: array of GPIO descriptors whose values will be read
3305 * @value_array: array to store the read values
3307 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3308 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3309 * else an error code.
3311 * This function is to be called from contexts that can sleep.
3313 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3314 struct gpio_desc **desc_array,
3317 might_sleep_if(extra_checks);
3320 return gpiod_get_array_value_complex(true, true, array_size,
3321 desc_array, value_array);
3323 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3326 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3327 * @array_size: number of elements in the descriptor / value arrays
3328 * @desc_array: array of GPIO descriptors whose values will be read
3329 * @value_array: array to store the read values
3331 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3332 * into account. Return 0 in case of success, else an error code.
3334 * This function is to be called from contexts that can sleep.
3336 int gpiod_get_array_value_cansleep(unsigned int array_size,
3337 struct gpio_desc **desc_array,
3340 might_sleep_if(extra_checks);
3343 return gpiod_get_array_value_complex(false, true, array_size,
3344 desc_array, value_array);
3346 EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3349 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3350 * @desc: gpio whose value will be assigned
3351 * @value: value to assign
3353 * Set the raw value of the GPIO, i.e. the value of its physical line without
3354 * regard for its ACTIVE_LOW status.
3356 * This function is to be called from contexts that can sleep.
3358 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
3360 might_sleep_if(extra_checks);
3361 VALIDATE_DESC_VOID(desc);
3362 gpiod_set_raw_value_commit(desc, value);
3364 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
3367 * gpiod_set_value_cansleep() - assign a gpio's value
3368 * @desc: gpio whose value will be assigned
3369 * @value: value to assign
3371 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3374 * This function is to be called from contexts that can sleep.
3376 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
3378 might_sleep_if(extra_checks);
3379 VALIDATE_DESC_VOID(desc);
3380 gpiod_set_value_nocheck(desc, value);
3382 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
3385 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
3386 * @array_size: number of elements in the descriptor / value arrays
3387 * @desc_array: array of GPIO descriptors whose values will be assigned
3388 * @value_array: array of values to assign
3390 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3391 * without regard for their ACTIVE_LOW status.
3393 * This function is to be called from contexts that can sleep.
3395 void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3396 struct gpio_desc **desc_array,
3399 might_sleep_if(extra_checks);
3402 gpiod_set_array_value_complex(true, true, array_size, desc_array,
3405 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
3408 * gpiod_add_lookup_tables() - register GPIO device consumers
3409 * @tables: list of tables of consumers to register
3410 * @n: number of tables in the list
3412 void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3416 mutex_lock(&gpio_lookup_lock);
3418 for (i = 0; i < n; i++)
3419 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3421 mutex_unlock(&gpio_lookup_lock);
3425 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
3426 * @array_size: number of elements in the descriptor / value arrays
3427 * @desc_array: array of GPIO descriptors whose values will be assigned
3428 * @value_array: array of values to assign
3430 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3433 * This function is to be called from contexts that can sleep.
3435 void gpiod_set_array_value_cansleep(unsigned int array_size,
3436 struct gpio_desc **desc_array,
3439 might_sleep_if(extra_checks);
3442 gpiod_set_array_value_complex(false, true, array_size, desc_array,
3445 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
3448 * gpiod_add_lookup_table() - register GPIO device consumers
3449 * @table: table of consumers to register
3451 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
3453 mutex_lock(&gpio_lookup_lock);
3455 list_add_tail(&table->list, &gpio_lookup_list);
3457 mutex_unlock(&gpio_lookup_lock);
3459 EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
3462 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3463 * @table: table of consumers to unregister
3465 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3467 mutex_lock(&gpio_lookup_lock);
3469 list_del(&table->list);
3471 mutex_unlock(&gpio_lookup_lock);
3473 EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
3475 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3477 const char *dev_id = dev ? dev_name(dev) : NULL;
3478 struct gpiod_lookup_table *table;
3480 mutex_lock(&gpio_lookup_lock);
3482 list_for_each_entry(table, &gpio_lookup_list, list) {
3483 if (table->dev_id && dev_id) {
3485 * Valid strings on both ends, must be identical to have
3488 if (!strcmp(table->dev_id, dev_id))
3492 * One of the pointers is NULL, so both must be to have
3495 if (dev_id == table->dev_id)
3502 mutex_unlock(&gpio_lookup_lock);
3506 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
3508 enum gpio_lookup_flags *flags)
3510 struct gpio_desc *desc = ERR_PTR(-ENOENT);
3511 struct gpiod_lookup_table *table;
3512 struct gpiod_lookup *p;
3514 table = gpiod_find_lookup_table(dev);
3518 for (p = &table->table[0]; p->chip_label; p++) {
3519 struct gpio_chip *chip;
3521 /* idx must always match exactly */
3525 /* If the lookup entry has a con_id, require exact match */
3526 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3529 chip = find_chip_by_name(p->chip_label);
3532 dev_err(dev, "cannot find GPIO chip %s\n",
3534 return ERR_PTR(-ENODEV);
3537 if (chip->ngpio <= p->chip_hwnum) {
3539 "requested GPIO %d is out of range [0..%d] for chip %s\n",
3540 idx, chip->ngpio, chip->label);
3541 return ERR_PTR(-EINVAL);
3544 desc = gpiochip_get_desc(chip, p->chip_hwnum);
3553 static int dt_gpio_count(struct device *dev, const char *con_id)
3559 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3561 snprintf(propname, sizeof(propname), "%s-%s",
3562 con_id, gpio_suffixes[i]);
3564 snprintf(propname, sizeof(propname), "%s",
3567 ret = of_gpio_named_count(dev->of_node, propname);
3571 return ret ? ret : -ENOENT;
3574 static int platform_gpio_count(struct device *dev, const char *con_id)
3576 struct gpiod_lookup_table *table;
3577 struct gpiod_lookup *p;
3578 unsigned int count = 0;
3580 table = gpiod_find_lookup_table(dev);
3584 for (p = &table->table[0]; p->chip_label; p++) {
3585 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3586 (!con_id && !p->con_id))
3596 * gpiod_count - return the number of GPIOs associated with a device / function
3597 * or -ENOENT if no GPIO has been assigned to the requested function
3598 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3599 * @con_id: function within the GPIO consumer
3601 int gpiod_count(struct device *dev, const char *con_id)
3603 int count = -ENOENT;
3605 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3606 count = dt_gpio_count(dev, con_id);
3607 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3608 count = acpi_gpio_count(dev, con_id);
3611 count = platform_gpio_count(dev, con_id);
3615 EXPORT_SYMBOL_GPL(gpiod_count);
3618 * gpiod_get - obtain a GPIO for a given GPIO function
3619 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3620 * @con_id: function within the GPIO consumer
3621 * @flags: optional GPIO initialization flags
3623 * Return the GPIO descriptor corresponding to the function con_id of device
3624 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
3625 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
3627 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
3628 enum gpiod_flags flags)
3630 return gpiod_get_index(dev, con_id, 0, flags);
3632 EXPORT_SYMBOL_GPL(gpiod_get);
3635 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3636 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3637 * @con_id: function within the GPIO consumer
3638 * @flags: optional GPIO initialization flags
3640 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3641 * the requested function it will return NULL. This is convenient for drivers
3642 * that need to handle optional GPIOs.
3644 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
3646 enum gpiod_flags flags)
3648 return gpiod_get_index_optional(dev, con_id, 0, flags);
3650 EXPORT_SYMBOL_GPL(gpiod_get_optional);
3654 * gpiod_configure_flags - helper function to configure a given GPIO
3655 * @desc: gpio whose value will be assigned
3656 * @con_id: function within the GPIO consumer
3657 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3659 * @dflags: gpiod_flags - optional GPIO initialization flags
3661 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3662 * requested function and/or index, or another IS_ERR() code if an error
3663 * occurred while trying to acquire the GPIO.
3665 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
3666 unsigned long lflags, enum gpiod_flags dflags)
3670 if (lflags & GPIO_ACTIVE_LOW)
3671 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3673 if (lflags & GPIO_OPEN_DRAIN)
3674 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3675 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
3677 * This enforces open drain mode from the consumer side.
3678 * This is necessary for some busses like I2C, but the lookup
3679 * should *REALLY* have specified them as open drain in the
3680 * first place, so print a little warning here.
3682 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3684 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
3687 if (lflags & GPIO_OPEN_SOURCE)
3688 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3690 status = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
3694 /* No particular flag request, return here... */
3695 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3696 pr_debug("no flags found for %s\n", con_id);
3701 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3702 status = gpiod_direction_output(desc,
3703 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
3705 status = gpiod_direction_input(desc);
3711 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
3712 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3713 * @con_id: function within the GPIO consumer
3714 * @idx: index of the GPIO to obtain in the consumer
3715 * @flags: optional GPIO initialization flags
3717 * This variant of gpiod_get() allows to access GPIOs other than the first
3718 * defined one for functions that define several GPIOs.
3720 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3721 * requested function and/or index, or another IS_ERR() code if an error
3722 * occurred while trying to acquire the GPIO.
3724 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
3727 enum gpiod_flags flags)
3729 struct gpio_desc *desc = NULL;
3731 enum gpio_lookup_flags lookupflags = 0;
3732 /* Maybe we have a device name, maybe not */
3733 const char *devname = dev ? dev_name(dev) : "?";
3735 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3738 /* Using device tree? */
3739 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3740 dev_dbg(dev, "using device tree for GPIO lookup\n");
3741 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3742 } else if (ACPI_COMPANION(dev)) {
3743 dev_dbg(dev, "using ACPI for GPIO lookup\n");
3744 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
3749 * Either we are not using DT or ACPI, or their lookup did not return
3750 * a result. In that case, use platform lookup as a fallback.
3752 if (!desc || desc == ERR_PTR(-ENOENT)) {
3753 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
3754 desc = gpiod_find(dev, con_id, idx, &lookupflags);
3758 dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
3763 * If a connection label was passed use that, else attempt to use
3764 * the device name as label
3766 status = gpiod_request(desc, con_id ? con_id : devname);
3768 return ERR_PTR(status);
3770 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
3772 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3774 return ERR_PTR(status);
3779 EXPORT_SYMBOL_GPL(gpiod_get_index);
3782 * gpiod_get_from_of_node() - obtain a GPIO from an OF node
3783 * @node: handle of the OF node
3784 * @propname: name of the DT property representing the GPIO
3785 * @index: index of the GPIO to obtain for the consumer
3786 * @dflags: GPIO initialization flags
3787 * @label: label to attach to the requested GPIO
3790 * On successful request the GPIO pin is configured in accordance with
3791 * provided @dflags. If the node does not have the requested GPIO
3792 * property, NULL is returned.
3794 * In case of error an ERR_PTR() is returned.
3796 struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
3797 const char *propname, int index,
3798 enum gpiod_flags dflags,
3801 struct gpio_desc *desc;
3802 unsigned long lflags = 0;
3803 enum of_gpio_flags flags;
3804 bool active_low = false;
3805 bool single_ended = false;
3806 bool open_drain = false;
3807 bool transitory = false;
3810 desc = of_get_named_gpiod_flags(node, propname,
3813 if (!desc || IS_ERR(desc)) {
3814 /* If it is not there, just return NULL */
3815 if (PTR_ERR(desc) == -ENOENT)
3820 active_low = flags & OF_GPIO_ACTIVE_LOW;
3821 single_ended = flags & OF_GPIO_SINGLE_ENDED;
3822 open_drain = flags & OF_GPIO_OPEN_DRAIN;
3823 transitory = flags & OF_GPIO_TRANSITORY;
3825 ret = gpiod_request(desc, label);
3827 return ERR_PTR(ret);
3830 lflags |= GPIO_ACTIVE_LOW;
3834 lflags |= GPIO_OPEN_DRAIN;
3836 lflags |= GPIO_OPEN_SOURCE;
3840 lflags |= GPIO_TRANSITORY;
3842 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3845 return ERR_PTR(ret);
3850 EXPORT_SYMBOL(gpiod_get_from_of_node);
3853 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3854 * @fwnode: handle of the firmware node
3855 * @propname: name of the firmware property representing the GPIO
3856 * @index: index of the GPIO to obtain for the consumer
3857 * @dflags: GPIO initialization flags
3858 * @label: label to attach to the requested GPIO
3860 * This function can be used for drivers that get their configuration
3861 * from opaque firmware.
3863 * The function properly finds the corresponding GPIO using whatever is the
3864 * underlying firmware interface and then makes sure that the GPIO
3865 * descriptor is requested before it is returned to the caller.
3868 * On successful request the GPIO pin is configured in accordance with
3871 * In case of error an ERR_PTR() is returned.
3873 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
3874 const char *propname, int index,
3875 enum gpiod_flags dflags,
3878 struct gpio_desc *desc = ERR_PTR(-ENODEV);
3879 unsigned long lflags = 0;
3883 return ERR_PTR(-EINVAL);
3885 if (is_of_node(fwnode)) {
3886 desc = gpiod_get_from_of_node(to_of_node(fwnode),
3891 } else if (is_acpi_node(fwnode)) {
3892 struct acpi_gpio_info info;
3894 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
3898 acpi_gpio_update_gpiod_flags(&dflags, &info);
3900 if (info.polarity == GPIO_ACTIVE_LOW)
3901 lflags |= GPIO_ACTIVE_LOW;
3904 /* Currently only ACPI takes this path */
3905 ret = gpiod_request(desc, label);
3907 return ERR_PTR(ret);
3909 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3912 return ERR_PTR(ret);
3917 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3920 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3922 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3923 * @con_id: function within the GPIO consumer
3924 * @index: index of the GPIO to obtain in the consumer
3925 * @flags: optional GPIO initialization flags
3927 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3928 * specified index was assigned to the requested function it will return NULL.
3929 * This is convenient for drivers that need to handle optional GPIOs.
3931 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
3934 enum gpiod_flags flags)
3936 struct gpio_desc *desc;
3938 desc = gpiod_get_index(dev, con_id, index, flags);
3940 if (PTR_ERR(desc) == -ENOENT)
3946 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
3949 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3950 * @desc: gpio whose value will be assigned
3951 * @name: gpio line name
3952 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3954 * @dflags: gpiod_flags - optional GPIO initialization flags
3956 int gpiod_hog(struct gpio_desc *desc, const char *name,
3957 unsigned long lflags, enum gpiod_flags dflags)
3959 struct gpio_chip *chip;
3960 struct gpio_desc *local_desc;
3964 chip = gpiod_to_chip(desc);
3965 hwnum = gpio_chip_hwgpio(desc);
3967 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
3968 if (IS_ERR(local_desc)) {
3969 status = PTR_ERR(local_desc);
3970 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3971 name, chip->label, hwnum, status);
3975 status = gpiod_configure_flags(desc, name, lflags, dflags);
3977 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3978 name, chip->label, hwnum, status);
3979 gpiochip_free_own_desc(desc);
3983 /* Mark GPIO as hogged so it can be identified and removed later */
3984 set_bit(FLAG_IS_HOGGED, &desc->flags);
3986 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3987 desc_to_gpio(desc), name,
3988 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3989 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3990 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3996 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3997 * @chip: gpio chip to act on
3999 * This is only used by of_gpiochip_remove to free hogged gpios
4001 static void gpiochip_free_hogs(struct gpio_chip *chip)
4005 for (id = 0; id < chip->ngpio; id++) {
4006 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
4007 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
4012 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4013 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4014 * @con_id: function within the GPIO consumer
4015 * @flags: optional GPIO initialization flags
4017 * This function acquires all the GPIOs defined under a given function.
4019 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4020 * no GPIO has been assigned to the requested function, or another IS_ERR()
4021 * code if an error occurred while trying to acquire the GPIOs.
4023 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4025 enum gpiod_flags flags)
4027 struct gpio_desc *desc;
4028 struct gpio_descs *descs;
4031 count = gpiod_count(dev, con_id);
4033 return ERR_PTR(count);
4035 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
4038 return ERR_PTR(-ENOMEM);
4040 for (descs->ndescs = 0; descs->ndescs < count; ) {
4041 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4043 gpiod_put_array(descs);
4044 return ERR_CAST(desc);
4046 descs->desc[descs->ndescs] = desc;
4051 EXPORT_SYMBOL_GPL(gpiod_get_array);
4054 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4056 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4057 * @con_id: function within the GPIO consumer
4058 * @flags: optional GPIO initialization flags
4060 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4061 * assigned to the requested function it will return NULL.
4063 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4065 enum gpiod_flags flags)
4067 struct gpio_descs *descs;
4069 descs = gpiod_get_array(dev, con_id, flags);
4070 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
4075 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4078 * gpiod_put - dispose of a GPIO descriptor
4079 * @desc: GPIO descriptor to dispose of
4081 * No descriptor can be used after gpiod_put() has been called on it.
4083 void gpiod_put(struct gpio_desc *desc)
4087 EXPORT_SYMBOL_GPL(gpiod_put);
4090 * gpiod_put_array - dispose of multiple GPIO descriptors
4091 * @descs: struct gpio_descs containing an array of descriptors
4093 void gpiod_put_array(struct gpio_descs *descs)
4097 for (i = 0; i < descs->ndescs; i++)
4098 gpiod_put(descs->desc[i]);
4102 EXPORT_SYMBOL_GPL(gpiod_put_array);
4104 static int __init gpiolib_dev_init(void)
4108 /* Register GPIO sysfs bus */
4109 ret = bus_register(&gpio_bus_type);
4111 pr_err("gpiolib: could not register GPIO bus type\n");
4115 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
4117 pr_err("gpiolib: failed to allocate char dev region\n");
4118 bus_unregister(&gpio_bus_type);
4120 gpiolib_initialized = true;
4121 gpiochip_setup_devs();
4125 core_initcall(gpiolib_dev_init);
4127 #ifdef CONFIG_DEBUG_FS
4129 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
4132 struct gpio_chip *chip = gdev->chip;
4133 unsigned gpio = gdev->base;
4134 struct gpio_desc *gdesc = &gdev->descs[0];
4138 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
4139 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
4141 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
4147 gpiod_get_direction(gdesc);
4148 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
4149 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
4150 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
4151 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
4152 is_out ? "out" : "in ",
4154 ? (chip->get(chip, i) ? "hi" : "lo")
4156 is_irq ? "IRQ" : " ");
4157 seq_printf(s, "\n");
4161 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
4163 unsigned long flags;
4164 struct gpio_device *gdev = NULL;
4165 loff_t index = *pos;
4169 spin_lock_irqsave(&gpio_lock, flags);
4170 list_for_each_entry(gdev, &gpio_devices, list)
4172 spin_unlock_irqrestore(&gpio_lock, flags);
4175 spin_unlock_irqrestore(&gpio_lock, flags);
4180 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4182 unsigned long flags;
4183 struct gpio_device *gdev = v;
4186 spin_lock_irqsave(&gpio_lock, flags);
4187 if (list_is_last(&gdev->list, &gpio_devices))
4190 ret = list_entry(gdev->list.next, struct gpio_device, list);
4191 spin_unlock_irqrestore(&gpio_lock, flags);
4199 static void gpiolib_seq_stop(struct seq_file *s, void *v)
4203 static int gpiolib_seq_show(struct seq_file *s, void *v)
4205 struct gpio_device *gdev = v;
4206 struct gpio_chip *chip = gdev->chip;
4207 struct device *parent;
4210 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4211 dev_name(&gdev->dev));
4215 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4216 dev_name(&gdev->dev),
4217 gdev->base, gdev->base + gdev->ngpio - 1);
4218 parent = chip->parent;
4220 seq_printf(s, ", parent: %s/%s",
4221 parent->bus ? parent->bus->name : "no-bus",
4224 seq_printf(s, ", %s", chip->label);
4225 if (chip->can_sleep)
4226 seq_printf(s, ", can sleep");
4227 seq_printf(s, ":\n");
4230 chip->dbg_show(s, chip);
4232 gpiolib_dbg_show(s, gdev);
4237 static const struct seq_operations gpiolib_seq_ops = {
4238 .start = gpiolib_seq_start,
4239 .next = gpiolib_seq_next,
4240 .stop = gpiolib_seq_stop,
4241 .show = gpiolib_seq_show,
4244 static int gpiolib_open(struct inode *inode, struct file *file)
4246 return seq_open(file, &gpiolib_seq_ops);
4249 static const struct file_operations gpiolib_operations = {
4250 .owner = THIS_MODULE,
4251 .open = gpiolib_open,
4253 .llseek = seq_lseek,
4254 .release = seq_release,
4257 static int __init gpiolib_debugfs_init(void)
4259 /* /sys/kernel/debug/gpio */
4260 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
4261 NULL, NULL, &gpiolib_operations);
4264 subsys_initcall(gpiolib_debugfs_init);
4266 #endif /* DEBUG_FS */