1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/interrupt.h>
5 #include <linux/spinlock.h>
6 #include <linux/list.h>
7 #include <linux/device.h>
9 #include <linux/debugfs.h>
10 #include <linux/seq_file.h>
11 #include <linux/gpio.h>
12 #include <linux/of_gpio.h>
13 #include <linux/idr.h>
14 #include <linux/slab.h>
15 #include <linux/acpi.h>
16 #include <linux/gpio/driver.h>
17 #include <linux/gpio/machine.h>
18 #include <linux/pinctrl/consumer.h>
19 #include <linux/cdev.h>
21 #include <linux/uaccess.h>
22 #include <linux/compat.h>
23 #include <linux/anon_inodes.h>
24 #include <linux/kfifo.h>
25 #include <linux/poll.h>
26 #include <linux/timekeeping.h>
27 #include <uapi/linux/gpio.h>
31 #define CREATE_TRACE_POINTS
32 #include <trace/events/gpio.h>
34 /* Implementation infrastructure for GPIO interfaces.
36 * The GPIO programming interface allows for inlining speed-critical
37 * get/set operations for common cases, so that access to SOC-integrated
38 * GPIOs can sometimes cost only an instruction or two per bit.
42 /* When debugging, extend minimal trust to callers and platform code.
43 * Also emit diagnostic messages that may help initial bringup, when
44 * board setup or driver bugs are most common.
46 * Otherwise, minimize overhead in what may be bitbanging codepaths.
49 #define extra_checks 1
51 #define extra_checks 0
54 /* Device and char device-related information */
55 static DEFINE_IDA(gpio_ida);
56 static dev_t gpio_devt;
57 #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
58 static struct bus_type gpio_bus_type = {
62 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
63 * While any GPIO is requested, its gpio_chip is not removable;
64 * each GPIO's "requested" flag serves as a lock and refcount.
66 DEFINE_SPINLOCK(gpio_lock);
68 static DEFINE_MUTEX(gpio_lookup_lock);
69 static LIST_HEAD(gpio_lookup_list);
70 LIST_HEAD(gpio_devices);
72 static void gpiochip_free_hogs(struct gpio_chip *chip);
73 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
74 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
75 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
77 static bool gpiolib_initialized;
79 static inline void desc_set_label(struct gpio_desc *d, const char *label)
85 * Convert a GPIO number to its descriptor
87 struct gpio_desc *gpio_to_desc(unsigned gpio)
89 struct gpio_device *gdev;
92 spin_lock_irqsave(&gpio_lock, flags);
94 list_for_each_entry(gdev, &gpio_devices, list) {
95 if (gdev->base <= gpio &&
96 gdev->base + gdev->ngpio > gpio) {
97 spin_unlock_irqrestore(&gpio_lock, flags);
98 return &gdev->descs[gpio - gdev->base];
102 spin_unlock_irqrestore(&gpio_lock, flags);
104 if (!gpio_is_valid(gpio))
105 WARN(1, "invalid GPIO %d\n", gpio);
109 EXPORT_SYMBOL_GPL(gpio_to_desc);
112 * Get the GPIO descriptor corresponding to the given hw number for this chip.
114 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
117 struct gpio_device *gdev = chip->gpiodev;
119 if (hwnum >= gdev->ngpio)
120 return ERR_PTR(-EINVAL);
122 return &gdev->descs[hwnum];
126 * Convert a GPIO descriptor to the integer namespace.
127 * This should disappear in the future but is needed since we still
128 * use GPIO numbers for error messages and sysfs nodes
130 int desc_to_gpio(const struct gpio_desc *desc)
132 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
134 EXPORT_SYMBOL_GPL(desc_to_gpio);
138 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
139 * @desc: descriptor to return the chip of
141 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
143 if (!desc || !desc->gdev || !desc->gdev->chip)
145 return desc->gdev->chip;
147 EXPORT_SYMBOL_GPL(gpiod_to_chip);
149 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
150 static int gpiochip_find_base(int ngpio)
152 struct gpio_device *gdev;
153 int base = ARCH_NR_GPIOS - ngpio;
155 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
156 /* found a free space? */
157 if (gdev->base + gdev->ngpio <= base)
160 /* nope, check the space right before the chip */
161 base = gdev->base - ngpio;
164 if (gpio_is_valid(base)) {
165 pr_debug("%s: found new base at %d\n", __func__, base);
168 pr_err("%s: cannot find free range\n", __func__);
174 * gpiod_get_direction - return the current direction of a GPIO
175 * @desc: GPIO to get the direction of
177 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
179 * This function may sleep if gpiod_cansleep() is true.
181 int gpiod_get_direction(struct gpio_desc *desc)
183 struct gpio_chip *chip;
185 int status = -EINVAL;
187 chip = gpiod_to_chip(desc);
188 offset = gpio_chip_hwgpio(desc);
190 if (!chip->get_direction)
193 status = chip->get_direction(chip, offset);
195 /* GPIOF_DIR_IN, or other positive */
197 clear_bit(FLAG_IS_OUT, &desc->flags);
201 set_bit(FLAG_IS_OUT, &desc->flags);
205 EXPORT_SYMBOL_GPL(gpiod_get_direction);
208 * Add a new chip to the global chips list, keeping the list of chips sorted
209 * by range(means [base, base + ngpio - 1]) order.
211 * Return -EBUSY if the new chip overlaps with some other chip's integer
214 static int gpiodev_add_to_list(struct gpio_device *gdev)
216 struct gpio_device *prev, *next;
218 if (list_empty(&gpio_devices)) {
219 /* initial entry in list */
220 list_add_tail(&gdev->list, &gpio_devices);
224 next = list_entry(gpio_devices.next, struct gpio_device, list);
225 if (gdev->base + gdev->ngpio <= next->base) {
226 /* add before first entry */
227 list_add(&gdev->list, &gpio_devices);
231 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
232 if (prev->base + prev->ngpio <= gdev->base) {
233 /* add behind last entry */
234 list_add_tail(&gdev->list, &gpio_devices);
238 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
239 /* at the end of the list */
240 if (&next->list == &gpio_devices)
243 /* add between prev and next */
244 if (prev->base + prev->ngpio <= gdev->base
245 && gdev->base + gdev->ngpio <= next->base) {
246 list_add(&gdev->list, &prev->list);
251 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
256 * Convert a GPIO name to its descriptor
258 static struct gpio_desc *gpio_name_to_desc(const char * const name)
260 struct gpio_device *gdev;
263 spin_lock_irqsave(&gpio_lock, flags);
265 list_for_each_entry(gdev, &gpio_devices, list) {
268 for (i = 0; i != gdev->ngpio; ++i) {
269 struct gpio_desc *desc = &gdev->descs[i];
271 if (!desc->name || !name)
274 if (!strcmp(desc->name, name)) {
275 spin_unlock_irqrestore(&gpio_lock, flags);
281 spin_unlock_irqrestore(&gpio_lock, flags);
287 * Takes the names from gc->names and checks if they are all unique. If they
288 * are, they are assigned to their gpio descriptors.
290 * Warning if one of the names is already used for a different GPIO.
292 static int gpiochip_set_desc_names(struct gpio_chip *gc)
294 struct gpio_device *gdev = gc->gpiodev;
300 /* First check all names if they are unique */
301 for (i = 0; i != gc->ngpio; ++i) {
302 struct gpio_desc *gpio;
304 gpio = gpio_name_to_desc(gc->names[i]);
307 "Detected name collision for GPIO name '%s'\n",
311 /* Then add all names to the GPIO descriptors */
312 for (i = 0; i != gc->ngpio; ++i)
313 gdev->descs[i].name = gc->names[i];
319 * GPIO line handle management
323 * struct linehandle_state - contains the state of a userspace handle
324 * @gdev: the GPIO device the handle pertains to
325 * @label: consumer label used to tag descriptors
326 * @descs: the GPIO descriptors held by this handle
327 * @numdescs: the number of descriptors held in the descs array
329 struct linehandle_state {
330 struct gpio_device *gdev;
332 struct gpio_desc *descs[GPIOHANDLES_MAX];
336 #define GPIOHANDLE_REQUEST_VALID_FLAGS \
337 (GPIOHANDLE_REQUEST_INPUT | \
338 GPIOHANDLE_REQUEST_OUTPUT | \
339 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
340 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
341 GPIOHANDLE_REQUEST_OPEN_SOURCE)
343 static long linehandle_ioctl(struct file *filep, unsigned int cmd,
346 struct linehandle_state *lh = filep->private_data;
347 void __user *ip = (void __user *)arg;
348 struct gpiohandle_data ghd;
351 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
354 memset(&ghd, 0, sizeof(ghd));
356 /* TODO: check if descriptors are really input */
357 for (i = 0; i < lh->numdescs; i++) {
358 val = gpiod_get_value_cansleep(lh->descs[i]);
364 if (copy_to_user(ip, &ghd, sizeof(ghd)))
368 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
369 int vals[GPIOHANDLES_MAX];
371 /* TODO: check if descriptors are really output */
372 if (copy_from_user(&ghd, ip, sizeof(ghd)))
375 /* Clamp all values to [0,1] */
376 for (i = 0; i < lh->numdescs; i++)
377 vals[i] = !!ghd.values[i];
379 /* Reuse the array setting function */
380 gpiod_set_array_value_complex(false,
391 static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
394 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
398 static int linehandle_release(struct inode *inode, struct file *filep)
400 struct linehandle_state *lh = filep->private_data;
401 struct gpio_device *gdev = lh->gdev;
404 for (i = 0; i < lh->numdescs; i++)
405 gpiod_free(lh->descs[i]);
408 put_device(&gdev->dev);
412 static const struct file_operations linehandle_fileops = {
413 .release = linehandle_release,
414 .owner = THIS_MODULE,
415 .llseek = noop_llseek,
416 .unlocked_ioctl = linehandle_ioctl,
418 .compat_ioctl = linehandle_ioctl_compat,
422 static int linehandle_create(struct gpio_device *gdev, void __user *ip)
424 struct gpiohandle_request handlereq;
425 struct linehandle_state *lh;
428 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
430 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
433 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
437 get_device(&gdev->dev);
439 /* Make sure this is terminated */
440 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
441 if (strlen(handlereq.consumer_label)) {
442 lh->label = kstrdup(handlereq.consumer_label,
450 /* Request each GPIO */
451 for (i = 0; i < handlereq.lines; i++) {
452 u32 offset = handlereq.lineoffsets[i];
453 u32 lflags = handlereq.flags;
454 struct gpio_desc *desc;
456 if (offset >= gdev->ngpio) {
461 /* Return an error if a unknown flag is set */
462 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) {
467 desc = &gdev->descs[offset];
468 ret = gpiod_request(desc, lh->label);
473 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
474 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
475 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
476 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
477 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
478 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
481 * Lines have to be requested explicitly for input
482 * or output, else the line will be treated "as is".
484 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
485 int val = !!handlereq.default_values[i];
487 ret = gpiod_direction_output(desc, val);
490 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
491 ret = gpiod_direction_input(desc);
495 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
498 /* Let i point at the last handle */
500 lh->numdescs = handlereq.lines;
502 fd = anon_inode_getfd("gpio-linehandle",
505 O_RDONLY | O_CLOEXEC);
512 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
517 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
524 gpiod_free(lh->descs[i]);
528 put_device(&gdev->dev);
533 * GPIO line event management
537 * struct lineevent_state - contains the state of a userspace event
538 * @gdev: the GPIO device the event pertains to
539 * @label: consumer label used to tag descriptors
540 * @desc: the GPIO descriptor held by this event
541 * @eflags: the event flags this line was requested with
542 * @irq: the interrupt that trigger in response to events on this GPIO
543 * @wait: wait queue that handles blocking reads of events
544 * @events: KFIFO for the GPIO events
545 * @read_lock: mutex lock to protect reads from colliding with adding
546 * new events to the FIFO
548 struct lineevent_state {
549 struct gpio_device *gdev;
551 struct gpio_desc *desc;
554 wait_queue_head_t wait;
555 DECLARE_KFIFO(events, struct gpioevent_data, 16);
556 struct mutex read_lock;
559 #define GPIOEVENT_REQUEST_VALID_FLAGS \
560 (GPIOEVENT_REQUEST_RISING_EDGE | \
561 GPIOEVENT_REQUEST_FALLING_EDGE)
563 static unsigned int lineevent_poll(struct file *filep,
564 struct poll_table_struct *wait)
566 struct lineevent_state *le = filep->private_data;
567 unsigned int events = 0;
569 poll_wait(filep, &le->wait, wait);
571 if (!kfifo_is_empty(&le->events))
572 events = POLLIN | POLLRDNORM;
578 static ssize_t lineevent_read(struct file *filep,
583 struct lineevent_state *le = filep->private_data;
587 if (count < sizeof(struct gpioevent_data))
591 if (kfifo_is_empty(&le->events)) {
592 if (filep->f_flags & O_NONBLOCK)
595 ret = wait_event_interruptible(le->wait,
596 !kfifo_is_empty(&le->events));
601 if (mutex_lock_interruptible(&le->read_lock))
603 ret = kfifo_to_user(&le->events, buf, count, &copied);
604 mutex_unlock(&le->read_lock);
610 * If we couldn't read anything from the fifo (a different
611 * thread might have been faster) we either return -EAGAIN if
612 * the file descriptor is non-blocking, otherwise we go back to
613 * sleep and wait for more data to arrive.
615 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
618 } while (copied == 0);
623 static int lineevent_release(struct inode *inode, struct file *filep)
625 struct lineevent_state *le = filep->private_data;
626 struct gpio_device *gdev = le->gdev;
628 free_irq(le->irq, le);
629 gpiod_free(le->desc);
632 put_device(&gdev->dev);
636 static long lineevent_ioctl(struct file *filep, unsigned int cmd,
639 struct lineevent_state *le = filep->private_data;
640 void __user *ip = (void __user *)arg;
641 struct gpiohandle_data ghd;
644 * We can get the value for an event line but not set it,
645 * because it is input by definition.
647 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
650 memset(&ghd, 0, sizeof(ghd));
652 val = gpiod_get_value_cansleep(le->desc);
657 if (copy_to_user(ip, &ghd, sizeof(ghd)))
666 static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
669 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
673 static const struct file_operations lineevent_fileops = {
674 .release = lineevent_release,
675 .read = lineevent_read,
676 .poll = lineevent_poll,
677 .owner = THIS_MODULE,
678 .llseek = noop_llseek,
679 .unlocked_ioctl = lineevent_ioctl,
681 .compat_ioctl = lineevent_ioctl_compat,
685 static irqreturn_t lineevent_irq_thread(int irq, void *p)
687 struct lineevent_state *le = p;
688 struct gpioevent_data ge;
691 ge.timestamp = ktime_get_real_ns();
693 if (le->eflags & GPIOEVENT_REQUEST_BOTH_EDGES) {
694 int level = gpiod_get_value_cansleep(le->desc);
697 /* Emit low-to-high event */
698 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
700 /* Emit high-to-low event */
701 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
702 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
703 /* Emit low-to-high event */
704 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
705 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
706 /* Emit high-to-low event */
707 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
712 ret = kfifo_put(&le->events, ge);
714 wake_up_poll(&le->wait, POLLIN);
719 static int lineevent_create(struct gpio_device *gdev, void __user *ip)
721 struct gpioevent_request eventreq;
722 struct lineevent_state *le;
723 struct gpio_desc *desc;
731 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
734 le = kzalloc(sizeof(*le), GFP_KERNEL);
738 get_device(&gdev->dev);
740 /* Make sure this is terminated */
741 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
742 if (strlen(eventreq.consumer_label)) {
743 le->label = kstrdup(eventreq.consumer_label,
751 offset = eventreq.lineoffset;
752 lflags = eventreq.handleflags;
753 eflags = eventreq.eventflags;
755 if (offset >= gdev->ngpio) {
760 /* Return an error if a unknown flag is set */
761 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
762 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
767 /* This is just wrong: we don't look for events on output lines */
768 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
773 desc = &gdev->descs[offset];
774 ret = gpiod_request(desc, le->label);
780 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
781 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
782 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
783 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
784 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
785 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
787 ret = gpiod_direction_input(desc);
791 le->irq = gpiod_to_irq(desc);
797 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
798 irqflags |= IRQF_TRIGGER_RISING;
799 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
800 irqflags |= IRQF_TRIGGER_FALLING;
801 irqflags |= IRQF_ONESHOT;
802 irqflags |= IRQF_SHARED;
804 INIT_KFIFO(le->events);
805 init_waitqueue_head(&le->wait);
806 mutex_init(&le->read_lock);
808 /* Request a thread to read the events */
809 ret = request_threaded_irq(le->irq,
811 lineevent_irq_thread,
818 fd = anon_inode_getfd("gpio-event",
821 O_RDONLY | O_CLOEXEC);
828 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
836 free_irq(le->irq, le);
838 gpiod_free(le->desc);
843 put_device(&gdev->dev);
848 * gpio_ioctl() - ioctl handler for the GPIO chardev
850 static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
852 struct gpio_device *gdev = filp->private_data;
853 struct gpio_chip *chip = gdev->chip;
854 void __user *ip = (void __user *)arg;
856 /* We fail any subsequent ioctl():s when the chip is gone */
860 /* Fill in the struct and pass to userspace */
861 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
862 struct gpiochip_info chipinfo;
864 memset(&chipinfo, 0, sizeof(chipinfo));
866 strncpy(chipinfo.name, dev_name(&gdev->dev),
867 sizeof(chipinfo.name));
868 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
869 strncpy(chipinfo.label, gdev->label,
870 sizeof(chipinfo.label));
871 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
872 chipinfo.lines = gdev->ngpio;
873 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
876 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
877 struct gpioline_info lineinfo;
878 struct gpio_desc *desc;
880 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
882 if (lineinfo.line_offset >= gdev->ngpio)
885 desc = &gdev->descs[lineinfo.line_offset];
887 strncpy(lineinfo.name, desc->name,
888 sizeof(lineinfo.name));
889 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
891 lineinfo.name[0] = '\0';
894 strncpy(lineinfo.consumer, desc->label,
895 sizeof(lineinfo.consumer));
896 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
898 lineinfo.consumer[0] = '\0';
902 * Userspace only need to know that the kernel is using
903 * this GPIO so it can't use it.
906 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
907 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
908 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
909 test_bit(FLAG_EXPORT, &desc->flags) ||
910 test_bit(FLAG_SYSFS, &desc->flags))
911 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
912 if (test_bit(FLAG_IS_OUT, &desc->flags))
913 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
914 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
915 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
916 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
917 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
918 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
919 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
921 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
924 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
925 return linehandle_create(gdev, ip);
926 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
927 return lineevent_create(gdev, ip);
933 static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
936 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
941 * gpio_chrdev_open() - open the chardev for ioctl operations
942 * @inode: inode for this chardev
943 * @filp: file struct for storing private data
944 * Returns 0 on success
946 static int gpio_chrdev_open(struct inode *inode, struct file *filp)
948 struct gpio_device *gdev = container_of(inode->i_cdev,
949 struct gpio_device, chrdev);
951 /* Fail on open if the backing gpiochip is gone */
952 if (!gdev || !gdev->chip)
954 get_device(&gdev->dev);
955 filp->private_data = gdev;
960 * gpio_chrdev_release() - close chardev after ioctl operations
961 * @inode: inode for this chardev
962 * @filp: file struct for storing private data
963 * Returns 0 on success
965 static int gpio_chrdev_release(struct inode *inode, struct file *filp)
967 struct gpio_device *gdev = container_of(inode->i_cdev,
968 struct gpio_device, chrdev);
972 put_device(&gdev->dev);
977 static const struct file_operations gpio_fileops = {
978 .release = gpio_chrdev_release,
979 .open = gpio_chrdev_open,
980 .owner = THIS_MODULE,
981 .llseek = noop_llseek,
982 .unlocked_ioctl = gpio_ioctl,
984 .compat_ioctl = gpio_ioctl_compat,
988 static void gpiodevice_release(struct device *dev)
990 struct gpio_device *gdev = dev_get_drvdata(dev);
992 list_del(&gdev->list);
993 ida_simple_remove(&gpio_ida, gdev->id);
999 static int gpiochip_setup_dev(struct gpio_device *gdev)
1003 cdev_init(&gdev->chrdev, &gpio_fileops);
1004 gdev->chrdev.owner = THIS_MODULE;
1005 gdev->chrdev.kobj.parent = &gdev->dev.kobj;
1006 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
1007 status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
1009 chip_warn(gdev->chip, "failed to add char device %d:%d\n",
1010 MAJOR(gpio_devt), gdev->id);
1012 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1013 MAJOR(gpio_devt), gdev->id);
1014 status = device_add(&gdev->dev);
1016 goto err_remove_chardev;
1018 status = gpiochip_sysfs_register(gdev);
1020 goto err_remove_device;
1022 /* From this point, the .release() function cleans up gpio_device */
1023 gdev->dev.release = gpiodevice_release;
1024 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1025 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1026 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1031 device_del(&gdev->dev);
1033 cdev_del(&gdev->chrdev);
1037 static void gpiochip_setup_devs(void)
1039 struct gpio_device *gdev;
1042 list_for_each_entry(gdev, &gpio_devices, list) {
1043 err = gpiochip_setup_dev(gdev);
1045 pr_err("%s: Failed to initialize gpio device (%d)\n",
1046 dev_name(&gdev->dev), err);
1051 * gpiochip_add_data() - register a gpio_chip
1052 * @chip: the chip to register, with chip->base initialized
1053 * Context: potentially before irqs will work
1055 * Returns a negative errno if the chip can't be registered, such as
1056 * because the chip->base is invalid or already associated with a
1057 * different chip. Otherwise it returns zero as a success code.
1059 * When gpiochip_add_data() is called very early during boot, so that GPIOs
1060 * can be freely used, the chip->parent device must be registered before
1061 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1062 * for GPIOs will fail rudely.
1064 * gpiochip_add_data() must only be called after gpiolib initialization,
1065 * ie after core_initcall().
1067 * If chip->base is negative, this requests dynamic assignment of
1068 * a range of valid GPIOs.
1070 int gpiochip_add_data(struct gpio_chip *chip, void *data)
1072 unsigned long flags;
1075 int base = chip->base;
1076 struct gpio_device *gdev;
1079 * First: allocate and populate the internal stat container, and
1080 * set up the struct device.
1082 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
1085 gdev->dev.bus = &gpio_bus_type;
1087 chip->gpiodev = gdev;
1089 gdev->dev.parent = chip->parent;
1090 gdev->dev.of_node = chip->parent->of_node;
1093 #ifdef CONFIG_OF_GPIO
1094 /* If the gpiochip has an assigned OF node this takes precedence */
1096 gdev->dev.of_node = chip->of_node;
1099 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1104 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1105 device_initialize(&gdev->dev);
1106 dev_set_drvdata(&gdev->dev, gdev);
1107 if (chip->parent && chip->parent->driver)
1108 gdev->owner = chip->parent->driver->owner;
1109 else if (chip->owner)
1110 /* TODO: remove chip->owner */
1111 gdev->owner = chip->owner;
1113 gdev->owner = THIS_MODULE;
1115 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
1121 if (chip->ngpio == 0) {
1122 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
1124 goto err_free_descs;
1128 gdev->label = kstrdup(chip->label, GFP_KERNEL);
1130 gdev->label = kstrdup("unknown", GFP_KERNEL);
1133 goto err_free_descs;
1136 gdev->ngpio = chip->ngpio;
1139 spin_lock_irqsave(&gpio_lock, flags);
1142 * TODO: this allocates a Linux GPIO number base in the global
1143 * GPIO numberspace for this chip. In the long run we want to
1144 * get *rid* of this numberspace and use only descriptors, but
1145 * it may be a pipe dream. It will not happen before we get rid
1146 * of the sysfs interface anyways.
1149 base = gpiochip_find_base(chip->ngpio);
1152 spin_unlock_irqrestore(&gpio_lock, flags);
1153 goto err_free_label;
1156 * TODO: it should not be necessary to reflect the assigned
1157 * base outside of the GPIO subsystem. Go over drivers and
1158 * see if anyone makes use of this, else drop this and assign
1165 status = gpiodev_add_to_list(gdev);
1167 spin_unlock_irqrestore(&gpio_lock, flags);
1168 goto err_free_label;
1171 spin_unlock_irqrestore(&gpio_lock, flags);
1173 for (i = 0; i < chip->ngpio; i++) {
1174 struct gpio_desc *desc = &gdev->descs[i];
1178 * REVISIT: most hardware initializes GPIOs as inputs
1179 * (often with pullups enabled) so power usage is
1180 * minimized. Linux code should set the gpio direction
1181 * first thing; but until it does, and in case
1182 * chip->get_direction is not set, we may expose the
1183 * wrong direction in sysfs.
1186 if (chip->get_direction) {
1188 * If we have .get_direction, set up the initial
1189 * direction flag from the hardware.
1191 int dir = chip->get_direction(chip, i);
1194 set_bit(FLAG_IS_OUT, &desc->flags);
1195 } else if (!chip->direction_input) {
1197 * If the chip lacks the .direction_input callback
1198 * we logically assume all lines are outputs.
1200 set_bit(FLAG_IS_OUT, &desc->flags);
1204 #ifdef CONFIG_PINCTRL
1205 INIT_LIST_HEAD(&gdev->pin_ranges);
1208 status = gpiochip_set_desc_names(chip);
1210 goto err_remove_from_list;
1212 status = gpiochip_irqchip_init_valid_mask(chip);
1214 goto err_remove_from_list;
1216 status = of_gpiochip_add(chip);
1218 goto err_remove_chip;
1220 acpi_gpiochip_add(chip);
1223 * By first adding the chardev, and then adding the device,
1224 * we get a device node entry in sysfs under
1225 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1226 * coldplug of device nodes and other udev business.
1227 * We can do this only if gpiolib has been initialized.
1228 * Otherwise, defer until later.
1230 if (gpiolib_initialized) {
1231 status = gpiochip_setup_dev(gdev);
1233 goto err_remove_chip;
1238 acpi_gpiochip_remove(chip);
1239 gpiochip_free_hogs(chip);
1240 of_gpiochip_remove(chip);
1241 gpiochip_irqchip_free_valid_mask(chip);
1242 err_remove_from_list:
1243 spin_lock_irqsave(&gpio_lock, flags);
1244 list_del(&gdev->list);
1245 spin_unlock_irqrestore(&gpio_lock, flags);
1251 ida_simple_remove(&gpio_ida, gdev->id);
1252 /* failures here can mean systems won't boot... */
1253 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
1254 gdev->base, gdev->base + gdev->ngpio - 1,
1255 chip->label ? : "generic");
1259 EXPORT_SYMBOL_GPL(gpiochip_add_data);
1262 * gpiochip_get_data() - get per-subdriver data for the chip
1264 void *gpiochip_get_data(struct gpio_chip *chip)
1266 return chip->gpiodev->data;
1268 EXPORT_SYMBOL_GPL(gpiochip_get_data);
1271 * gpiochip_remove() - unregister a gpio_chip
1272 * @chip: the chip to unregister
1274 * A gpio_chip with any GPIOs still requested may not be removed.
1276 void gpiochip_remove(struct gpio_chip *chip)
1278 struct gpio_device *gdev = chip->gpiodev;
1279 struct gpio_desc *desc;
1280 unsigned long flags;
1282 bool requested = false;
1284 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
1285 gpiochip_sysfs_unregister(gdev);
1286 /* Numb the device, cancelling all outstanding operations */
1288 gpiochip_irqchip_remove(chip);
1289 acpi_gpiochip_remove(chip);
1290 gpiochip_remove_pin_ranges(chip);
1291 gpiochip_free_hogs(chip);
1292 of_gpiochip_remove(chip);
1294 * We accept no more calls into the driver from this point, so
1295 * NULL the driver data pointer
1299 spin_lock_irqsave(&gpio_lock, flags);
1300 for (i = 0; i < gdev->ngpio; i++) {
1301 desc = &gdev->descs[i];
1302 if (test_bit(FLAG_REQUESTED, &desc->flags))
1305 spin_unlock_irqrestore(&gpio_lock, flags);
1308 dev_crit(&gdev->dev,
1309 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
1312 * The gpiochip side puts its use of the device to rest here:
1313 * if there are no userspace clients, the chardev and device will
1314 * be removed, else it will be dangling until the last user is
1317 cdev_del(&gdev->chrdev);
1318 device_del(&gdev->dev);
1319 put_device(&gdev->dev);
1321 EXPORT_SYMBOL_GPL(gpiochip_remove);
1323 static void devm_gpio_chip_release(struct device *dev, void *res)
1325 struct gpio_chip *chip = *(struct gpio_chip **)res;
1327 gpiochip_remove(chip);
1330 static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1333 struct gpio_chip **r = res;
1344 * devm_gpiochip_add_data() - Resource manager piochip_add_data()
1345 * @dev: the device pointer on which irq_chip belongs to.
1346 * @chip: the chip to register, with chip->base initialized
1347 * Context: potentially before irqs will work
1349 * Returns a negative errno if the chip can't be registered, such as
1350 * because the chip->base is invalid or already associated with a
1351 * different chip. Otherwise it returns zero as a success code.
1353 * The gpio chip automatically be released when the device is unbound.
1355 int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1358 struct gpio_chip **ptr;
1361 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1366 ret = gpiochip_add_data(chip, data);
1373 devres_add(dev, ptr);
1377 EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1380 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1381 * @dev: device for which which resource was allocated
1382 * @chip: the chip to remove
1384 * A gpio_chip with any GPIOs still requested may not be removed.
1386 void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1390 ret = devres_release(dev, devm_gpio_chip_release,
1391 devm_gpio_chip_match, chip);
1395 EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1398 * gpiochip_find() - iterator for locating a specific gpio_chip
1399 * @data: data to pass to match function
1400 * @callback: Callback function to check gpio_chip
1402 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1403 * determined by a user supplied @match callback. The callback should return
1404 * 0 if the device doesn't match and non-zero if it does. If the callback is
1405 * non-zero, this function will return to the caller and not iterate over any
1408 struct gpio_chip *gpiochip_find(void *data,
1409 int (*match)(struct gpio_chip *chip,
1412 struct gpio_device *gdev;
1413 struct gpio_chip *chip = NULL;
1414 unsigned long flags;
1416 spin_lock_irqsave(&gpio_lock, flags);
1417 list_for_each_entry(gdev, &gpio_devices, list)
1418 if (gdev->chip && match(gdev->chip, data)) {
1423 spin_unlock_irqrestore(&gpio_lock, flags);
1427 EXPORT_SYMBOL_GPL(gpiochip_find);
1429 static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1431 const char *name = data;
1433 return !strcmp(chip->label, name);
1436 static struct gpio_chip *find_chip_by_name(const char *name)
1438 return gpiochip_find((void *)name, gpiochip_match_name);
1441 #ifdef CONFIG_GPIOLIB_IRQCHIP
1444 * The following is irqchip helper code for gpiochips.
1447 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1451 if (!gpiochip->irq_need_valid_mask)
1454 gpiochip->irq_valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio),
1455 sizeof(long), GFP_KERNEL);
1456 if (!gpiochip->irq_valid_mask)
1459 /* Assume by default all GPIOs are valid */
1460 for (i = 0; i < gpiochip->ngpio; i++)
1461 set_bit(i, gpiochip->irq_valid_mask);
1466 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1468 kfree(gpiochip->irq_valid_mask);
1469 gpiochip->irq_valid_mask = NULL;
1472 static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1473 unsigned int offset)
1475 /* No mask means all valid */
1476 if (likely(!gpiochip->irq_valid_mask))
1478 return test_bit(offset, gpiochip->irq_valid_mask);
1482 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
1483 * @gpiochip: the gpiochip to set the irqchip chain to
1484 * @irqchip: the irqchip to chain to the gpiochip
1485 * @parent_irq: the irq number corresponding to the parent IRQ for this
1487 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1488 * coming out of the gpiochip. If the interrupt is nested rather than
1489 * cascaded, pass NULL in this handler argument
1491 void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1492 struct irq_chip *irqchip,
1494 irq_flow_handler_t parent_handler)
1496 unsigned int offset;
1498 if (!gpiochip->irqdomain) {
1499 chip_err(gpiochip, "called %s before setting up irqchip\n",
1504 if (parent_handler) {
1505 if (gpiochip->can_sleep) {
1507 "you cannot have chained interrupts on a "
1508 "chip that may sleep\n");
1512 * The parent irqchip is already using the chip_data for this
1513 * irqchip, so our callbacks simply use the handler_data.
1515 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1518 gpiochip->irq_parent = parent_irq;
1521 /* Set the parent IRQ for all affected IRQs */
1522 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1523 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1525 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
1529 EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1532 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1533 * @d: the irqdomain used by this irqchip
1534 * @irq: the global irq number used by this GPIO irqchip irq
1535 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1537 * This function will set up the mapping for a certain IRQ line on a
1538 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1539 * stored inside the gpiochip.
1541 static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1542 irq_hw_number_t hwirq)
1544 struct gpio_chip *chip = d->host_data;
1546 irq_set_chip_data(irq, chip);
1548 * This lock class tells lockdep that GPIO irqs are in a different
1549 * category than their parents, so it won't report false recursion.
1551 irq_set_lockdep_class(irq, chip->lock_key);
1552 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
1553 /* Chips that can sleep need nested thread handlers */
1554 if (chip->can_sleep && !chip->irq_not_threaded)
1555 irq_set_nested_thread(irq, 1);
1556 irq_set_noprobe(irq);
1559 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1560 * is passed as default type.
1562 if (chip->irq_default_type != IRQ_TYPE_NONE)
1563 irq_set_irq_type(irq, chip->irq_default_type);
1568 static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1570 struct gpio_chip *chip = d->host_data;
1572 if (chip->can_sleep)
1573 irq_set_nested_thread(irq, 0);
1574 irq_set_chip_and_handler(irq, NULL, NULL);
1575 irq_set_chip_data(irq, NULL);
1578 static const struct irq_domain_ops gpiochip_domain_ops = {
1579 .map = gpiochip_irq_map,
1580 .unmap = gpiochip_irq_unmap,
1581 /* Virtually all GPIO irqchips are twocell:ed */
1582 .xlate = irq_domain_xlate_twocell,
1585 static int gpiochip_irq_reqres(struct irq_data *d)
1587 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1589 if (!try_module_get(chip->gpiodev->owner))
1592 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
1594 "unable to lock HW IRQ %lu for IRQ\n",
1596 module_put(chip->gpiodev->owner);
1602 static void gpiochip_irq_relres(struct irq_data *d)
1604 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1606 gpiochip_unlock_as_irq(chip, d->hwirq);
1607 module_put(chip->gpiodev->owner);
1610 static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1612 return irq_find_mapping(chip->irqdomain, offset);
1616 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1617 * @gpiochip: the gpiochip to remove the irqchip from
1619 * This is called only from gpiochip_remove()
1621 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1623 unsigned int offset;
1625 acpi_gpiochip_free_interrupts(gpiochip);
1627 if (gpiochip->irq_parent) {
1628 irq_set_chained_handler(gpiochip->irq_parent, NULL);
1629 irq_set_handler_data(gpiochip->irq_parent, NULL);
1632 /* Remove all IRQ mappings and delete the domain */
1633 if (gpiochip->irqdomain) {
1634 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1635 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1637 irq_dispose_mapping(
1638 irq_find_mapping(gpiochip->irqdomain, offset));
1640 irq_domain_remove(gpiochip->irqdomain);
1643 if (gpiochip->irqchip) {
1644 gpiochip->irqchip->irq_request_resources = NULL;
1645 gpiochip->irqchip->irq_release_resources = NULL;
1646 gpiochip->irqchip = NULL;
1649 gpiochip_irqchip_free_valid_mask(gpiochip);
1653 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
1654 * @gpiochip: the gpiochip to add the irqchip to
1655 * @irqchip: the irqchip to add to the gpiochip
1656 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1657 * allocate gpiochip irqs from
1658 * @handler: the irq handler to use (often a predefined irq core function)
1659 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1660 * to have the core avoid setting up any default type in the hardware.
1661 * @lock_key: lockdep class
1663 * This function closely associates a certain irqchip with a certain
1664 * gpiochip, providing an irq domain to translate the local IRQs to
1665 * global irqs in the gpiolib core, and making sure that the gpiochip
1666 * is passed as chip data to all related functions. Driver callbacks
1667 * need to use gpiochip_get_data() to get their local state containers back
1668 * from the gpiochip passed as chip data. An irqdomain will be stored
1669 * in the gpiochip that shall be used by the driver to handle IRQ number
1670 * translation. The gpiochip will need to be initialized and registered
1671 * before calling this function.
1673 * This function will handle two cell:ed simple IRQs and assumes all
1674 * the pins on the gpiochip can generate a unique IRQ. Everything else
1675 * need to be open coded.
1677 int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
1678 struct irq_chip *irqchip,
1679 unsigned int first_irq,
1680 irq_flow_handler_t handler,
1682 struct lock_class_key *lock_key)
1684 struct device_node *of_node;
1685 bool irq_base_set = false;
1686 unsigned int offset;
1687 unsigned irq_base = 0;
1689 if (!gpiochip || !irqchip)
1692 if (!gpiochip->parent) {
1693 pr_err("missing gpiochip .dev parent pointer\n");
1696 of_node = gpiochip->parent->of_node;
1697 #ifdef CONFIG_OF_GPIO
1699 * If the gpiochip has an assigned OF node this takes precedence
1700 * FIXME: get rid of this and use gpiochip->parent->of_node
1703 if (gpiochip->of_node)
1704 of_node = gpiochip->of_node;
1707 * Specifying a default trigger is a terrible idea if DT or ACPI is
1708 * used to configure the interrupts, as you may end-up with
1709 * conflicting triggers. Tell the user, and reset to NONE.
1711 if (WARN(of_node && type != IRQ_TYPE_NONE,
1712 "%s: Ignoring %d default trigger\n", of_node->full_name, type))
1713 type = IRQ_TYPE_NONE;
1714 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1715 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1716 "Ignoring %d default trigger\n", type);
1717 type = IRQ_TYPE_NONE;
1720 gpiochip->irqchip = irqchip;
1721 gpiochip->irq_handler = handler;
1722 gpiochip->irq_default_type = type;
1723 gpiochip->to_irq = gpiochip_to_irq;
1724 gpiochip->lock_key = lock_key;
1725 gpiochip->irqdomain = irq_domain_add_simple(of_node,
1726 gpiochip->ngpio, first_irq,
1727 &gpiochip_domain_ops, gpiochip);
1728 if (!gpiochip->irqdomain) {
1729 gpiochip->irqchip = NULL;
1734 * It is possible for a driver to override this, but only if the
1735 * alternative functions are both implemented.
1737 if (!irqchip->irq_request_resources &&
1738 !irqchip->irq_release_resources) {
1739 irqchip->irq_request_resources = gpiochip_irq_reqres;
1740 irqchip->irq_release_resources = gpiochip_irq_relres;
1744 * Prepare the mapping since the irqchip shall be orthogonal to
1745 * any gpiochip calls. If the first_irq was zero, this is
1746 * necessary to allocate descriptors for all IRQs.
1748 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1749 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1751 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
1752 if (!irq_base_set) {
1754 * Store the base into the gpiochip to be used when
1755 * unmapping the irqs.
1757 gpiochip->irq_base = irq_base;
1758 irq_base_set = true;
1762 acpi_gpiochip_request_interrupts(gpiochip);
1766 EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
1768 #else /* CONFIG_GPIOLIB_IRQCHIP */
1770 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
1771 static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1775 static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1778 #endif /* CONFIG_GPIOLIB_IRQCHIP */
1781 * gpiochip_generic_request() - request the gpio function for a pin
1782 * @chip: the gpiochip owning the GPIO
1783 * @offset: the offset of the GPIO to request for GPIO function
1785 int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1787 return pinctrl_request_gpio(chip->gpiodev->base + offset);
1789 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1792 * gpiochip_generic_free() - free the gpio function from a pin
1793 * @chip: the gpiochip to request the gpio function for
1794 * @offset: the offset of the GPIO to free from GPIO function
1796 void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1798 pinctrl_free_gpio(chip->gpiodev->base + offset);
1800 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1802 #ifdef CONFIG_PINCTRL
1805 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1806 * @chip: the gpiochip to add the range for
1807 * @pctldev: the pin controller to map to
1808 * @gpio_offset: the start offset in the current gpio_chip number space
1809 * @pin_group: name of the pin group inside the pin controller
1811 int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1812 struct pinctrl_dev *pctldev,
1813 unsigned int gpio_offset, const char *pin_group)
1815 struct gpio_pin_range *pin_range;
1816 struct gpio_device *gdev = chip->gpiodev;
1819 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1821 chip_err(chip, "failed to allocate pin ranges\n");
1825 /* Use local offset as range ID */
1826 pin_range->range.id = gpio_offset;
1827 pin_range->range.gc = chip;
1828 pin_range->range.name = chip->label;
1829 pin_range->range.base = gdev->base + gpio_offset;
1830 pin_range->pctldev = pctldev;
1832 ret = pinctrl_get_group_pins(pctldev, pin_group,
1833 &pin_range->range.pins,
1834 &pin_range->range.npins);
1840 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1842 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1843 gpio_offset, gpio_offset + pin_range->range.npins - 1,
1844 pinctrl_dev_get_devname(pctldev), pin_group);
1846 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1850 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1853 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1854 * @chip: the gpiochip to add the range for
1855 * @pinctrl_name: the dev_name() of the pin controller to map to
1856 * @gpio_offset: the start offset in the current gpio_chip number space
1857 * @pin_offset: the start offset in the pin controller number space
1858 * @npins: the number of pins from the offset of each pin space (GPIO and
1859 * pin controller) to accumulate in this range
1861 int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
1862 unsigned int gpio_offset, unsigned int pin_offset,
1865 struct gpio_pin_range *pin_range;
1866 struct gpio_device *gdev = chip->gpiodev;
1869 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1871 chip_err(chip, "failed to allocate pin ranges\n");
1875 /* Use local offset as range ID */
1876 pin_range->range.id = gpio_offset;
1877 pin_range->range.gc = chip;
1878 pin_range->range.name = chip->label;
1879 pin_range->range.base = gdev->base + gpio_offset;
1880 pin_range->range.pin_base = pin_offset;
1881 pin_range->range.npins = npins;
1882 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
1884 if (IS_ERR(pin_range->pctldev)) {
1885 ret = PTR_ERR(pin_range->pctldev);
1886 chip_err(chip, "could not create pin range\n");
1890 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1891 gpio_offset, gpio_offset + npins - 1,
1893 pin_offset, pin_offset + npins - 1);
1895 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1899 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
1902 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1903 * @chip: the chip to remove all the mappings for
1905 void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1907 struct gpio_pin_range *pin_range, *tmp;
1908 struct gpio_device *gdev = chip->gpiodev;
1910 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
1911 list_del(&pin_range->node);
1912 pinctrl_remove_gpio_range(pin_range->pctldev,
1917 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1919 #endif /* CONFIG_PINCTRL */
1921 /* These "optional" allocation calls help prevent drivers from stomping
1922 * on each other, and help provide better diagnostics in debugfs.
1923 * They're called even less than the "set direction" calls.
1925 static int __gpiod_request(struct gpio_desc *desc, const char *label)
1927 struct gpio_chip *chip = desc->gdev->chip;
1929 unsigned long flags;
1931 spin_lock_irqsave(&gpio_lock, flags);
1933 /* NOTE: gpio_request() can be called in early boot,
1934 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1937 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1938 desc_set_label(desc, label ? : "?");
1945 if (chip->request) {
1946 /* chip->request may sleep */
1947 spin_unlock_irqrestore(&gpio_lock, flags);
1948 status = chip->request(chip, gpio_chip_hwgpio(desc));
1949 spin_lock_irqsave(&gpio_lock, flags);
1952 desc_set_label(desc, NULL);
1953 clear_bit(FLAG_REQUESTED, &desc->flags);
1957 if (chip->get_direction) {
1958 /* chip->get_direction may sleep */
1959 spin_unlock_irqrestore(&gpio_lock, flags);
1960 gpiod_get_direction(desc);
1961 spin_lock_irqsave(&gpio_lock, flags);
1964 spin_unlock_irqrestore(&gpio_lock, flags);
1969 * This descriptor validation needs to be inserted verbatim into each
1970 * function taking a descriptor, so we need to use a preprocessor
1971 * macro to avoid endless duplication. If the desc is NULL it is an
1972 * optional GPIO and calls should just bail out.
1974 #define VALIDATE_DESC(desc) do { \
1977 if (IS_ERR(desc)) { \
1978 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
1979 return PTR_ERR(desc); \
1981 if (!desc->gdev) { \
1982 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
1985 if ( !desc->gdev->chip ) { \
1986 dev_warn(&desc->gdev->dev, \
1987 "%s: backing chip is gone\n", __func__); \
1991 #define VALIDATE_DESC_VOID(desc) do { \
1994 if (IS_ERR(desc)) { \
1995 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
1998 if (!desc->gdev) { \
1999 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
2002 if (!desc->gdev->chip) { \
2003 dev_warn(&desc->gdev->dev, \
2004 "%s: backing chip is gone\n", __func__); \
2009 int gpiod_request(struct gpio_desc *desc, const char *label)
2011 int status = -EPROBE_DEFER;
2012 struct gpio_device *gdev;
2014 VALIDATE_DESC(desc);
2017 if (try_module_get(gdev->owner)) {
2018 status = __gpiod_request(desc, label);
2020 module_put(gdev->owner);
2022 get_device(&gdev->dev);
2026 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
2031 static bool __gpiod_free(struct gpio_desc *desc)
2034 unsigned long flags;
2035 struct gpio_chip *chip;
2039 gpiod_unexport(desc);
2041 spin_lock_irqsave(&gpio_lock, flags);
2043 chip = desc->gdev->chip;
2044 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2046 spin_unlock_irqrestore(&gpio_lock, flags);
2047 might_sleep_if(chip->can_sleep);
2048 chip->free(chip, gpio_chip_hwgpio(desc));
2049 spin_lock_irqsave(&gpio_lock, flags);
2051 desc_set_label(desc, NULL);
2052 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
2053 clear_bit(FLAG_REQUESTED, &desc->flags);
2054 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
2055 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
2056 clear_bit(FLAG_IS_HOGGED, &desc->flags);
2060 spin_unlock_irqrestore(&gpio_lock, flags);
2064 void gpiod_free(struct gpio_desc *desc)
2066 if (desc && desc->gdev && __gpiod_free(desc)) {
2067 module_put(desc->gdev->owner);
2068 put_device(&desc->gdev->dev);
2070 WARN_ON(extra_checks);
2075 * gpiochip_is_requested - return string iff signal was requested
2076 * @chip: controller managing the signal
2077 * @offset: of signal within controller's 0..(ngpio - 1) range
2079 * Returns NULL if the GPIO is not currently requested, else a string.
2080 * The string returned is the label passed to gpio_request(); if none has been
2081 * passed it is a meaningless, non-NULL constant.
2083 * This function is for use by GPIO controller drivers. The label can
2084 * help with diagnostics, and knowing that the signal is used as a GPIO
2085 * can help avoid accidentally multiplexing it to another controller.
2087 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2089 struct gpio_desc *desc;
2091 if (offset >= chip->ngpio)
2094 desc = &chip->gpiodev->descs[offset];
2096 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
2100 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2103 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2104 * @desc: GPIO descriptor to request
2105 * @label: label for the GPIO
2107 * Function allows GPIO chip drivers to request and use their own GPIO
2108 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2109 * function will not increase reference count of the GPIO chip module. This
2110 * allows the GPIO chip module to be unloaded as needed (we assume that the
2111 * GPIO chip driver handles freeing the GPIOs it has requested).
2113 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2116 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2120 chip_err(chip, "failed to get GPIO descriptor\n");
2124 err = __gpiod_request(desc, label);
2126 return ERR_PTR(err);
2130 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
2133 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2134 * @desc: GPIO descriptor to free
2136 * Function frees the given GPIO requested previously with
2137 * gpiochip_request_own_desc().
2139 void gpiochip_free_own_desc(struct gpio_desc *desc)
2144 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
2147 * Drivers MUST set GPIO direction before making get/set calls. In
2148 * some cases this is done in early boot, before IRQs are enabled.
2150 * As a rule these aren't called more than once (except for drivers
2151 * using the open-drain emulation idiom) so these are natural places
2152 * to accumulate extra debugging checks. Note that we can't (yet)
2153 * rely on gpio_request() having been called beforehand.
2157 * gpiod_direction_input - set the GPIO direction to input
2158 * @desc: GPIO to set to input
2160 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2161 * be called safely on it.
2163 * Return 0 in case of success, else an error code.
2165 int gpiod_direction_input(struct gpio_desc *desc)
2167 struct gpio_chip *chip;
2168 int status = -EINVAL;
2170 VALIDATE_DESC(desc);
2171 chip = desc->gdev->chip;
2173 if (!chip->get || !chip->direction_input) {
2175 "%s: missing get() or direction_input() operations\n",
2180 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
2182 clear_bit(FLAG_IS_OUT, &desc->flags);
2184 trace_gpio_direction(desc_to_gpio(desc), 1, status);
2188 EXPORT_SYMBOL_GPL(gpiod_direction_input);
2190 static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2192 struct gpio_chip *gc = desc->gdev->chip;
2195 /* GPIOs used for IRQs shall not be set as output */
2196 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2198 "%s: tried to set a GPIO tied to an IRQ as output\n",
2203 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2204 /* First see if we can enable open drain in hardware */
2205 if (gc->set_single_ended) {
2206 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
2207 LINE_MODE_OPEN_DRAIN);
2209 goto set_output_value;
2211 /* Emulate open drain by not actively driving the line high */
2213 return gpiod_direction_input(desc);
2215 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2216 if (gc->set_single_ended) {
2217 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
2218 LINE_MODE_OPEN_SOURCE);
2220 goto set_output_value;
2222 /* Emulate open source by not actively driving the line low */
2224 return gpiod_direction_input(desc);
2226 /* Make sure to disable open drain/source hardware, if any */
2227 if (gc->set_single_ended)
2228 gc->set_single_ended(gc,
2229 gpio_chip_hwgpio(desc),
2230 LINE_MODE_PUSH_PULL);
2234 if (!gc->set || !gc->direction_output) {
2236 "%s: missing set() or direction_output() operations\n",
2241 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), value);
2243 set_bit(FLAG_IS_OUT, &desc->flags);
2244 trace_gpio_value(desc_to_gpio(desc), 0, value);
2245 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2250 * gpiod_direction_output_raw - set the GPIO direction to output
2251 * @desc: GPIO to set to output
2252 * @value: initial output value of the GPIO
2254 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2255 * be called safely on it. The initial value of the output must be specified
2256 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2258 * Return 0 in case of success, else an error code.
2260 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2262 VALIDATE_DESC(desc);
2263 return _gpiod_direction_output_raw(desc, value);
2265 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2268 * gpiod_direction_output - set the GPIO direction to output
2269 * @desc: GPIO to set to output
2270 * @value: initial output value of the GPIO
2272 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2273 * be called safely on it. The initial value of the output must be specified
2274 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2277 * Return 0 in case of success, else an error code.
2279 int gpiod_direction_output(struct gpio_desc *desc, int value)
2281 VALIDATE_DESC(desc);
2282 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2284 return _gpiod_direction_output_raw(desc, value);
2286 EXPORT_SYMBOL_GPL(gpiod_direction_output);
2289 * gpiod_set_debounce - sets @debounce time for a @gpio
2290 * @gpio: the gpio to set debounce time
2291 * @debounce: debounce time is microseconds
2293 * returns -ENOTSUPP if the controller does not support setting
2296 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
2298 struct gpio_chip *chip;
2300 VALIDATE_DESC(desc);
2301 chip = desc->gdev->chip;
2302 if (!chip->set || !chip->set_debounce) {
2304 "%s: missing set() or set_debounce() operations\n",
2309 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
2311 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
2314 * gpiod_is_active_low - test whether a GPIO is active-low or not
2315 * @desc: the gpio descriptor to test
2317 * Returns 1 if the GPIO is active-low, 0 otherwise.
2319 int gpiod_is_active_low(const struct gpio_desc *desc)
2321 VALIDATE_DESC(desc);
2322 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
2324 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
2326 /* I/O calls are only valid after configuration completed; the relevant
2327 * "is this a valid GPIO" error checks should already have been done.
2329 * "Get" operations are often inlinable as reading a pin value register,
2330 * and masking the relevant bit in that register.
2332 * When "set" operations are inlinable, they involve writing that mask to
2333 * one register to set a low value, or a different register to set it high.
2334 * Otherwise locking is needed, so there may be little value to inlining.
2336 *------------------------------------------------------------------------
2338 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2339 * have requested the GPIO. That can include implicit requesting by
2340 * a direction setting call. Marking a gpio as requested locks its chip
2341 * in memory, guaranteeing that these table lookups need no more locking
2342 * and that gpiochip_remove() will fail.
2344 * REVISIT when debugging, consider adding some instrumentation to ensure
2345 * that the GPIO was actually requested.
2348 static int _gpiod_get_raw_value(const struct gpio_desc *desc)
2350 struct gpio_chip *chip;
2354 chip = desc->gdev->chip;
2355 offset = gpio_chip_hwgpio(desc);
2356 value = chip->get ? chip->get(chip, offset) : -EIO;
2357 value = value < 0 ? value : !!value;
2358 trace_gpio_value(desc_to_gpio(desc), 1, value);
2363 * gpiod_get_raw_value() - return a gpio's raw value
2364 * @desc: gpio whose value will be returned
2366 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2367 * its ACTIVE_LOW status, or negative errno on failure.
2369 * This function should be called from contexts where we cannot sleep, and will
2370 * complain if the GPIO chip functions potentially sleep.
2372 int gpiod_get_raw_value(const struct gpio_desc *desc)
2374 VALIDATE_DESC(desc);
2375 /* Should be using gpio_get_value_cansleep() */
2376 WARN_ON(desc->gdev->chip->can_sleep);
2377 return _gpiod_get_raw_value(desc);
2379 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
2382 * gpiod_get_value() - return a gpio's value
2383 * @desc: gpio whose value will be returned
2385 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2386 * account, or negative errno on failure.
2388 * This function should be called from contexts where we cannot sleep, and will
2389 * complain if the GPIO chip functions potentially sleep.
2391 int gpiod_get_value(const struct gpio_desc *desc)
2395 VALIDATE_DESC(desc);
2396 /* Should be using gpio_get_value_cansleep() */
2397 WARN_ON(desc->gdev->chip->can_sleep);
2399 value = _gpiod_get_raw_value(desc);
2403 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2408 EXPORT_SYMBOL_GPL(gpiod_get_value);
2411 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
2412 * @desc: gpio descriptor whose state need to be set.
2413 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2415 static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
2418 struct gpio_chip *chip = desc->gdev->chip;
2419 int offset = gpio_chip_hwgpio(desc);
2422 err = chip->direction_input(chip, offset);
2424 clear_bit(FLAG_IS_OUT, &desc->flags);
2426 err = chip->direction_output(chip, offset, 0);
2428 set_bit(FLAG_IS_OUT, &desc->flags);
2430 trace_gpio_direction(desc_to_gpio(desc), value, err);
2433 "%s: Error in set_value for open drain err %d\n",
2438 * _gpio_set_open_source_value() - Set the open source gpio's value.
2439 * @desc: gpio descriptor whose state need to be set.
2440 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2442 static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
2445 struct gpio_chip *chip = desc->gdev->chip;
2446 int offset = gpio_chip_hwgpio(desc);
2449 err = chip->direction_output(chip, offset, 1);
2451 set_bit(FLAG_IS_OUT, &desc->flags);
2453 err = chip->direction_input(chip, offset);
2455 clear_bit(FLAG_IS_OUT, &desc->flags);
2457 trace_gpio_direction(desc_to_gpio(desc), !value, err);
2460 "%s: Error in set_value for open source err %d\n",
2464 static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
2466 struct gpio_chip *chip;
2468 chip = desc->gdev->chip;
2469 trace_gpio_value(desc_to_gpio(desc), 0, value);
2470 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2471 _gpio_set_open_drain_value(desc, value);
2472 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2473 _gpio_set_open_source_value(desc, value);
2475 chip->set(chip, gpio_chip_hwgpio(desc), value);
2479 * set multiple outputs on the same chip;
2480 * use the chip's set_multiple function if available;
2481 * otherwise set the outputs sequentially;
2482 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2483 * defines which outputs are to be changed
2484 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2485 * defines the values the outputs specified by mask are to be set to
2487 static void gpio_chip_set_multiple(struct gpio_chip *chip,
2488 unsigned long *mask, unsigned long *bits)
2490 if (chip->set_multiple) {
2491 chip->set_multiple(chip, mask, bits);
2494 for (i = 0; i < chip->ngpio; i++) {
2495 if (mask[BIT_WORD(i)] == 0) {
2496 /* no more set bits in this mask word;
2497 * skip ahead to the next word */
2498 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
2501 /* set outputs if the corresponding mask bit is set */
2502 if (__test_and_clear_bit(i, mask))
2503 chip->set(chip, i, test_bit(i, bits));
2508 void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2509 unsigned int array_size,
2510 struct gpio_desc **desc_array,
2515 while (i < array_size) {
2516 struct gpio_chip *chip = desc_array[i]->gdev->chip;
2517 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2518 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2522 WARN_ON(chip->can_sleep);
2524 memset(mask, 0, sizeof(mask));
2526 struct gpio_desc *desc = desc_array[i];
2527 int hwgpio = gpio_chip_hwgpio(desc);
2528 int value = value_array[i];
2530 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2532 trace_gpio_value(desc_to_gpio(desc), 0, value);
2534 * collect all normal outputs belonging to the same chip
2535 * open drain and open source outputs are set individually
2537 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2538 _gpio_set_open_drain_value(desc, value);
2539 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2540 _gpio_set_open_source_value(desc, value);
2542 __set_bit(hwgpio, mask);
2544 __set_bit(hwgpio, bits);
2546 __clear_bit(hwgpio, bits);
2550 } while ((i < array_size) &&
2551 (desc_array[i]->gdev->chip == chip));
2552 /* push collected bits to outputs */
2554 gpio_chip_set_multiple(chip, mask, bits);
2559 * gpiod_set_raw_value() - assign a gpio's raw value
2560 * @desc: gpio whose value will be assigned
2561 * @value: value to assign
2563 * Set the raw value of the GPIO, i.e. the value of its physical line without
2564 * regard for its ACTIVE_LOW status.
2566 * This function should be called from contexts where we cannot sleep, and will
2567 * complain if the GPIO chip functions potentially sleep.
2569 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
2571 VALIDATE_DESC_VOID(desc);
2572 /* Should be using gpiod_set_value_cansleep() */
2573 WARN_ON(desc->gdev->chip->can_sleep);
2574 _gpiod_set_raw_value(desc, value);
2576 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
2579 * gpiod_set_value() - assign a gpio's value
2580 * @desc: gpio whose value will be assigned
2581 * @value: value to assign
2583 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2586 * This function should be called from contexts where we cannot sleep, and will
2587 * complain if the GPIO chip functions potentially sleep.
2589 void gpiod_set_value(struct gpio_desc *desc, int value)
2591 VALIDATE_DESC_VOID(desc);
2592 /* Should be using gpiod_set_value_cansleep() */
2593 WARN_ON(desc->gdev->chip->can_sleep);
2594 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2596 _gpiod_set_raw_value(desc, value);
2598 EXPORT_SYMBOL_GPL(gpiod_set_value);
2601 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
2602 * @array_size: number of elements in the descriptor / value arrays
2603 * @desc_array: array of GPIO descriptors whose values will be assigned
2604 * @value_array: array of values to assign
2606 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2607 * without regard for their ACTIVE_LOW status.
2609 * This function should be called from contexts where we cannot sleep, and will
2610 * complain if the GPIO chip functions potentially sleep.
2612 void gpiod_set_raw_array_value(unsigned int array_size,
2613 struct gpio_desc **desc_array, int *value_array)
2617 gpiod_set_array_value_complex(true, false, array_size, desc_array,
2620 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
2623 * gpiod_set_array_value() - assign values to an array of GPIOs
2624 * @array_size: number of elements in the descriptor / value arrays
2625 * @desc_array: array of GPIO descriptors whose values will be assigned
2626 * @value_array: array of values to assign
2628 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2631 * This function should be called from contexts where we cannot sleep, and will
2632 * complain if the GPIO chip functions potentially sleep.
2634 void gpiod_set_array_value(unsigned int array_size,
2635 struct gpio_desc **desc_array, int *value_array)
2639 gpiod_set_array_value_complex(false, false, array_size, desc_array,
2642 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
2645 * gpiod_cansleep() - report whether gpio value access may sleep
2646 * @desc: gpio to check
2649 int gpiod_cansleep(const struct gpio_desc *desc)
2651 VALIDATE_DESC(desc);
2652 return desc->gdev->chip->can_sleep;
2654 EXPORT_SYMBOL_GPL(gpiod_cansleep);
2657 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2658 * @desc: gpio whose IRQ will be returned (already requested)
2660 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2663 int gpiod_to_irq(const struct gpio_desc *desc)
2665 struct gpio_chip *chip;
2669 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
2670 * requires this function to not return zero on an invalid descriptor
2671 * but rather a negative error number.
2673 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
2676 chip = desc->gdev->chip;
2677 offset = gpio_chip_hwgpio(desc);
2679 int retirq = chip->to_irq(chip, offset);
2681 /* Zero means NO_IRQ */
2689 EXPORT_SYMBOL_GPL(gpiod_to_irq);
2692 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
2693 * @chip: the chip the GPIO to lock belongs to
2694 * @offset: the offset of the GPIO to lock as IRQ
2696 * This is used directly by GPIO drivers that want to lock down
2697 * a certain GPIO line to be used for IRQs.
2699 int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
2701 struct gpio_desc *desc;
2703 desc = gpiochip_get_desc(chip, offset);
2705 return PTR_ERR(desc);
2707 /* Flush direction if something changed behind our back */
2708 if (chip->get_direction) {
2709 int dir = chip->get_direction(chip, offset);
2712 clear_bit(FLAG_IS_OUT, &desc->flags);
2714 set_bit(FLAG_IS_OUT, &desc->flags);
2717 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
2719 "%s: tried to flag a GPIO set as output for IRQ\n",
2724 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
2727 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
2730 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
2731 * @chip: the chip the GPIO to lock belongs to
2732 * @offset: the offset of the GPIO to lock as IRQ
2734 * This is used directly by GPIO drivers that want to indicate
2735 * that a certain GPIO is no longer used exclusively for IRQ.
2737 void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
2739 if (offset >= chip->ngpio)
2742 clear_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
2744 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
2746 bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
2748 if (offset >= chip->ngpio)
2751 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
2753 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
2755 bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
2757 if (offset >= chip->ngpio)
2760 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
2762 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
2764 bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
2766 if (offset >= chip->ngpio)
2769 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
2771 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
2774 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2775 * @desc: gpio whose value will be returned
2777 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2778 * its ACTIVE_LOW status, or negative errno on failure.
2780 * This function is to be called from contexts that can sleep.
2782 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
2784 might_sleep_if(extra_checks);
2785 VALIDATE_DESC(desc);
2786 return _gpiod_get_raw_value(desc);
2788 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
2791 * gpiod_get_value_cansleep() - return a gpio's value
2792 * @desc: gpio whose value will be returned
2794 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2795 * account, or negative errno on failure.
2797 * This function is to be called from contexts that can sleep.
2799 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
2803 might_sleep_if(extra_checks);
2804 VALIDATE_DESC(desc);
2805 value = _gpiod_get_raw_value(desc);
2809 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2814 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
2817 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2818 * @desc: gpio whose value will be assigned
2819 * @value: value to assign
2821 * Set the raw value of the GPIO, i.e. the value of its physical line without
2822 * regard for its ACTIVE_LOW status.
2824 * This function is to be called from contexts that can sleep.
2826 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
2828 might_sleep_if(extra_checks);
2829 VALIDATE_DESC_VOID(desc);
2830 _gpiod_set_raw_value(desc, value);
2832 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
2835 * gpiod_set_value_cansleep() - assign a gpio's value
2836 * @desc: gpio whose value will be assigned
2837 * @value: value to assign
2839 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2842 * This function is to be called from contexts that can sleep.
2844 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
2846 might_sleep_if(extra_checks);
2847 VALIDATE_DESC_VOID(desc);
2848 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2850 _gpiod_set_raw_value(desc, value);
2852 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
2855 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
2856 * @array_size: number of elements in the descriptor / value arrays
2857 * @desc_array: array of GPIO descriptors whose values will be assigned
2858 * @value_array: array of values to assign
2860 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2861 * without regard for their ACTIVE_LOW status.
2863 * This function is to be called from contexts that can sleep.
2865 void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
2866 struct gpio_desc **desc_array,
2869 might_sleep_if(extra_checks);
2872 gpiod_set_array_value_complex(true, true, array_size, desc_array,
2875 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
2878 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
2879 * @array_size: number of elements in the descriptor / value arrays
2880 * @desc_array: array of GPIO descriptors whose values will be assigned
2881 * @value_array: array of values to assign
2883 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2886 * This function is to be called from contexts that can sleep.
2888 void gpiod_set_array_value_cansleep(unsigned int array_size,
2889 struct gpio_desc **desc_array,
2892 might_sleep_if(extra_checks);
2895 gpiod_set_array_value_complex(false, true, array_size, desc_array,
2898 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
2901 * gpiod_add_lookup_table() - register GPIO device consumers
2902 * @table: table of consumers to register
2904 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
2906 mutex_lock(&gpio_lookup_lock);
2908 list_add_tail(&table->list, &gpio_lookup_list);
2910 mutex_unlock(&gpio_lookup_lock);
2914 * gpiod_remove_lookup_table() - unregister GPIO device consumers
2915 * @table: table of consumers to unregister
2917 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
2919 mutex_lock(&gpio_lookup_lock);
2921 list_del(&table->list);
2923 mutex_unlock(&gpio_lookup_lock);
2926 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2928 const char *dev_id = dev ? dev_name(dev) : NULL;
2929 struct gpiod_lookup_table *table;
2931 mutex_lock(&gpio_lookup_lock);
2933 list_for_each_entry(table, &gpio_lookup_list, list) {
2934 if (table->dev_id && dev_id) {
2936 * Valid strings on both ends, must be identical to have
2939 if (!strcmp(table->dev_id, dev_id))
2943 * One of the pointers is NULL, so both must be to have
2946 if (dev_id == table->dev_id)
2953 mutex_unlock(&gpio_lookup_lock);
2957 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
2959 enum gpio_lookup_flags *flags)
2961 struct gpio_desc *desc = ERR_PTR(-ENOENT);
2962 struct gpiod_lookup_table *table;
2963 struct gpiod_lookup *p;
2965 table = gpiod_find_lookup_table(dev);
2969 for (p = &table->table[0]; p->chip_label; p++) {
2970 struct gpio_chip *chip;
2972 /* idx must always match exactly */
2976 /* If the lookup entry has a con_id, require exact match */
2977 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
2980 chip = find_chip_by_name(p->chip_label);
2983 dev_err(dev, "cannot find GPIO chip %s\n",
2985 return ERR_PTR(-ENODEV);
2988 if (chip->ngpio <= p->chip_hwnum) {
2990 "requested GPIO %d is out of range [0..%d] for chip %s\n",
2991 idx, chip->ngpio, chip->label);
2992 return ERR_PTR(-EINVAL);
2995 desc = gpiochip_get_desc(chip, p->chip_hwnum);
3004 static int dt_gpio_count(struct device *dev, const char *con_id)
3010 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3012 snprintf(propname, sizeof(propname), "%s-%s",
3013 con_id, gpio_suffixes[i]);
3015 snprintf(propname, sizeof(propname), "%s",
3018 ret = of_gpio_named_count(dev->of_node, propname);
3025 static int platform_gpio_count(struct device *dev, const char *con_id)
3027 struct gpiod_lookup_table *table;
3028 struct gpiod_lookup *p;
3029 unsigned int count = 0;
3031 table = gpiod_find_lookup_table(dev);
3035 for (p = &table->table[0]; p->chip_label; p++) {
3036 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3037 (!con_id && !p->con_id))
3047 * gpiod_count - return the number of GPIOs associated with a device / function
3048 * or -ENOENT if no GPIO has been assigned to the requested function
3049 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3050 * @con_id: function within the GPIO consumer
3052 int gpiod_count(struct device *dev, const char *con_id)
3054 int count = -ENOENT;
3056 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3057 count = dt_gpio_count(dev, con_id);
3058 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3059 count = acpi_gpio_count(dev, con_id);
3062 count = platform_gpio_count(dev, con_id);
3066 EXPORT_SYMBOL_GPL(gpiod_count);
3069 * gpiod_get - obtain a GPIO for a given GPIO function
3070 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3071 * @con_id: function within the GPIO consumer
3072 * @flags: optional GPIO initialization flags
3074 * Return the GPIO descriptor corresponding to the function con_id of device
3075 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
3076 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
3078 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
3079 enum gpiod_flags flags)
3081 return gpiod_get_index(dev, con_id, 0, flags);
3083 EXPORT_SYMBOL_GPL(gpiod_get);
3086 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3087 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3088 * @con_id: function within the GPIO consumer
3089 * @flags: optional GPIO initialization flags
3091 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3092 * the requested function it will return NULL. This is convenient for drivers
3093 * that need to handle optional GPIOs.
3095 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
3097 enum gpiod_flags flags)
3099 return gpiod_get_index_optional(dev, con_id, 0, flags);
3101 EXPORT_SYMBOL_GPL(gpiod_get_optional);
3105 * gpiod_configure_flags - helper function to configure a given GPIO
3106 * @desc: gpio whose value will be assigned
3107 * @con_id: function within the GPIO consumer
3108 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3110 * @dflags: gpiod_flags - optional GPIO initialization flags
3112 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3113 * requested function and/or index, or another IS_ERR() code if an error
3114 * occurred while trying to acquire the GPIO.
3116 static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
3117 unsigned long lflags, enum gpiod_flags dflags)
3121 if (lflags & GPIO_ACTIVE_LOW)
3122 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3123 if (lflags & GPIO_OPEN_DRAIN)
3124 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3125 if (lflags & GPIO_OPEN_SOURCE)
3126 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3128 /* No particular flag request, return here... */
3129 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3130 pr_debug("no flags found for %s\n", con_id);
3135 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3136 status = gpiod_direction_output(desc,
3137 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
3139 status = gpiod_direction_input(desc);
3145 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
3146 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3147 * @con_id: function within the GPIO consumer
3148 * @idx: index of the GPIO to obtain in the consumer
3149 * @flags: optional GPIO initialization flags
3151 * This variant of gpiod_get() allows to access GPIOs other than the first
3152 * defined one for functions that define several GPIOs.
3154 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3155 * requested function and/or index, or another IS_ERR() code if an error
3156 * occurred while trying to acquire the GPIO.
3158 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
3161 enum gpiod_flags flags)
3163 struct gpio_desc *desc = NULL;
3165 enum gpio_lookup_flags lookupflags = 0;
3167 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3170 /* Using device tree? */
3171 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3172 dev_dbg(dev, "using device tree for GPIO lookup\n");
3173 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3174 } else if (ACPI_COMPANION(dev)) {
3175 dev_dbg(dev, "using ACPI for GPIO lookup\n");
3176 desc = acpi_find_gpio(dev, con_id, idx, flags, &lookupflags);
3181 * Either we are not using DT or ACPI, or their lookup did not return
3182 * a result. In that case, use platform lookup as a fallback.
3184 if (!desc || desc == ERR_PTR(-ENOENT)) {
3185 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
3186 desc = gpiod_find(dev, con_id, idx, &lookupflags);
3190 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
3194 status = gpiod_request(desc, con_id);
3196 return ERR_PTR(status);
3198 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
3200 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3202 return ERR_PTR(status);
3207 EXPORT_SYMBOL_GPL(gpiod_get_index);
3210 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3211 * @fwnode: handle of the firmware node
3212 * @propname: name of the firmware property representing the GPIO
3214 * This function can be used for drivers that get their configuration
3217 * Function properly finds the corresponding GPIO using whatever is the
3218 * underlying firmware interface and then makes sure that the GPIO
3219 * descriptor is requested before it is returned to the caller.
3221 * In case of error an ERR_PTR() is returned.
3223 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
3224 const char *propname)
3226 struct gpio_desc *desc = ERR_PTR(-ENODEV);
3227 bool active_low = false;
3228 bool single_ended = false;
3232 return ERR_PTR(-EINVAL);
3234 if (is_of_node(fwnode)) {
3235 enum of_gpio_flags flags;
3237 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
3239 if (!IS_ERR(desc)) {
3240 active_low = flags & OF_GPIO_ACTIVE_LOW;
3241 single_ended = flags & OF_GPIO_SINGLE_ENDED;
3243 } else if (is_acpi_node(fwnode)) {
3244 struct acpi_gpio_info info;
3246 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
3248 active_low = info.polarity == GPIO_ACTIVE_LOW;
3254 ret = gpiod_request(desc, NULL);
3256 return ERR_PTR(ret);
3259 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3263 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3265 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3270 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3273 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3275 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3276 * @con_id: function within the GPIO consumer
3277 * @index: index of the GPIO to obtain in the consumer
3278 * @flags: optional GPIO initialization flags
3280 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3281 * specified index was assigned to the requested function it will return NULL.
3282 * This is convenient for drivers that need to handle optional GPIOs.
3284 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
3287 enum gpiod_flags flags)
3289 struct gpio_desc *desc;
3291 desc = gpiod_get_index(dev, con_id, index, flags);
3293 if (PTR_ERR(desc) == -ENOENT)
3299 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
3302 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3303 * @desc: gpio whose value will be assigned
3304 * @name: gpio line name
3305 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3307 * @dflags: gpiod_flags - optional GPIO initialization flags
3309 int gpiod_hog(struct gpio_desc *desc, const char *name,
3310 unsigned long lflags, enum gpiod_flags dflags)
3312 struct gpio_chip *chip;
3313 struct gpio_desc *local_desc;
3317 chip = gpiod_to_chip(desc);
3318 hwnum = gpio_chip_hwgpio(desc);
3320 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
3321 if (IS_ERR(local_desc)) {
3322 status = PTR_ERR(local_desc);
3323 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3324 name, chip->label, hwnum, status);
3328 status = gpiod_configure_flags(desc, name, lflags, dflags);
3330 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3331 name, chip->label, hwnum, status);
3332 gpiochip_free_own_desc(desc);
3336 /* Mark GPIO as hogged so it can be identified and removed later */
3337 set_bit(FLAG_IS_HOGGED, &desc->flags);
3339 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3340 desc_to_gpio(desc), name,
3341 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3342 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3343 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3349 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3350 * @chip: gpio chip to act on
3352 * This is only used by of_gpiochip_remove to free hogged gpios
3354 static void gpiochip_free_hogs(struct gpio_chip *chip)
3358 for (id = 0; id < chip->ngpio; id++) {
3359 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
3360 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
3365 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
3366 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3367 * @con_id: function within the GPIO consumer
3368 * @flags: optional GPIO initialization flags
3370 * This function acquires all the GPIOs defined under a given function.
3372 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
3373 * no GPIO has been assigned to the requested function, or another IS_ERR()
3374 * code if an error occurred while trying to acquire the GPIOs.
3376 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
3378 enum gpiod_flags flags)
3380 struct gpio_desc *desc;
3381 struct gpio_descs *descs;
3384 count = gpiod_count(dev, con_id);
3386 return ERR_PTR(count);
3388 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
3391 return ERR_PTR(-ENOMEM);
3393 for (descs->ndescs = 0; descs->ndescs < count; ) {
3394 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
3396 gpiod_put_array(descs);
3397 return ERR_CAST(desc);
3399 descs->desc[descs->ndescs] = desc;
3404 EXPORT_SYMBOL_GPL(gpiod_get_array);
3407 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
3409 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3410 * @con_id: function within the GPIO consumer
3411 * @flags: optional GPIO initialization flags
3413 * This is equivalent to gpiod_get_array(), except that when no GPIO was
3414 * assigned to the requested function it will return NULL.
3416 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
3418 enum gpiod_flags flags)
3420 struct gpio_descs *descs;
3422 descs = gpiod_get_array(dev, con_id, flags);
3423 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
3428 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
3431 * gpiod_put - dispose of a GPIO descriptor
3432 * @desc: GPIO descriptor to dispose of
3434 * No descriptor can be used after gpiod_put() has been called on it.
3436 void gpiod_put(struct gpio_desc *desc)
3440 EXPORT_SYMBOL_GPL(gpiod_put);
3443 * gpiod_put_array - dispose of multiple GPIO descriptors
3444 * @descs: struct gpio_descs containing an array of descriptors
3446 void gpiod_put_array(struct gpio_descs *descs)
3450 for (i = 0; i < descs->ndescs; i++)
3451 gpiod_put(descs->desc[i]);
3455 EXPORT_SYMBOL_GPL(gpiod_put_array);
3457 static int __init gpiolib_dev_init(void)
3461 /* Register GPIO sysfs bus */
3462 ret = bus_register(&gpio_bus_type);
3464 pr_err("gpiolib: could not register GPIO bus type\n");
3468 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
3470 pr_err("gpiolib: failed to allocate char dev region\n");
3471 bus_unregister(&gpio_bus_type);
3473 gpiolib_initialized = true;
3474 gpiochip_setup_devs();
3478 core_initcall(gpiolib_dev_init);
3480 #ifdef CONFIG_DEBUG_FS
3482 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
3485 struct gpio_chip *chip = gdev->chip;
3486 unsigned gpio = gdev->base;
3487 struct gpio_desc *gdesc = &gdev->descs[0];
3491 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
3492 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
3494 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
3500 gpiod_get_direction(gdesc);
3501 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
3502 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
3503 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
3504 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
3505 is_out ? "out" : "in ",
3507 ? (chip->get(chip, i) ? "hi" : "lo")
3509 is_irq ? "IRQ" : " ");
3510 seq_printf(s, "\n");
3514 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
3516 unsigned long flags;
3517 struct gpio_device *gdev = NULL;
3518 loff_t index = *pos;
3522 spin_lock_irqsave(&gpio_lock, flags);
3523 list_for_each_entry(gdev, &gpio_devices, list)
3525 spin_unlock_irqrestore(&gpio_lock, flags);
3528 spin_unlock_irqrestore(&gpio_lock, flags);
3533 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
3535 unsigned long flags;
3536 struct gpio_device *gdev = v;
3539 spin_lock_irqsave(&gpio_lock, flags);
3540 if (list_is_last(&gdev->list, &gpio_devices))
3543 ret = list_entry(gdev->list.next, struct gpio_device, list);
3544 spin_unlock_irqrestore(&gpio_lock, flags);
3552 static void gpiolib_seq_stop(struct seq_file *s, void *v)
3556 static int gpiolib_seq_show(struct seq_file *s, void *v)
3558 struct gpio_device *gdev = v;
3559 struct gpio_chip *chip = gdev->chip;
3560 struct device *parent;
3563 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
3564 dev_name(&gdev->dev));
3568 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
3569 dev_name(&gdev->dev),
3570 gdev->base, gdev->base + gdev->ngpio - 1);
3571 parent = chip->parent;
3573 seq_printf(s, ", parent: %s/%s",
3574 parent->bus ? parent->bus->name : "no-bus",
3577 seq_printf(s, ", %s", chip->label);
3578 if (chip->can_sleep)
3579 seq_printf(s, ", can sleep");
3580 seq_printf(s, ":\n");
3583 chip->dbg_show(s, chip);
3585 gpiolib_dbg_show(s, gdev);
3590 static const struct seq_operations gpiolib_seq_ops = {
3591 .start = gpiolib_seq_start,
3592 .next = gpiolib_seq_next,
3593 .stop = gpiolib_seq_stop,
3594 .show = gpiolib_seq_show,
3597 static int gpiolib_open(struct inode *inode, struct file *file)
3599 return seq_open(file, &gpiolib_seq_ops);
3602 static const struct file_operations gpiolib_operations = {
3603 .owner = THIS_MODULE,
3604 .open = gpiolib_open,
3606 .llseek = seq_lseek,
3607 .release = seq_release,
3610 static int __init gpiolib_debugfs_init(void)
3612 /* /sys/kernel/debug/gpio */
3613 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
3614 NULL, NULL, &gpiolib_operations);
3617 subsys_initcall(gpiolib_debugfs_init);
3619 #endif /* DEBUG_FS */