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 static unsigned int lineevent_poll(struct file *filep,
560 struct poll_table_struct *wait)
562 struct lineevent_state *le = filep->private_data;
563 unsigned int events = 0;
565 poll_wait(filep, &le->wait, wait);
567 if (!kfifo_is_empty(&le->events))
568 events = POLLIN | POLLRDNORM;
574 static ssize_t lineevent_read(struct file *filep,
579 struct lineevent_state *le = filep->private_data;
583 if (count < sizeof(struct gpioevent_data))
587 if (kfifo_is_empty(&le->events)) {
588 if (filep->f_flags & O_NONBLOCK)
591 ret = wait_event_interruptible(le->wait,
592 !kfifo_is_empty(&le->events));
597 if (mutex_lock_interruptible(&le->read_lock))
599 ret = kfifo_to_user(&le->events, buf, count, &copied);
600 mutex_unlock(&le->read_lock);
606 * If we couldn't read anything from the fifo (a different
607 * thread might have been faster) we either return -EAGAIN if
608 * the file descriptor is non-blocking, otherwise we go back to
609 * sleep and wait for more data to arrive.
611 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
614 } while (copied == 0);
619 static int lineevent_release(struct inode *inode, struct file *filep)
621 struct lineevent_state *le = filep->private_data;
622 struct gpio_device *gdev = le->gdev;
624 free_irq(le->irq, le);
625 gpiod_free(le->desc);
628 put_device(&gdev->dev);
632 static long lineevent_ioctl(struct file *filep, unsigned int cmd,
635 struct lineevent_state *le = filep->private_data;
636 void __user *ip = (void __user *)arg;
637 struct gpiohandle_data ghd;
640 * We can get the value for an event line but not set it,
641 * because it is input by definition.
643 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
646 memset(&ghd, 0, sizeof(ghd));
648 val = gpiod_get_value_cansleep(le->desc);
653 if (copy_to_user(ip, &ghd, sizeof(ghd)))
662 static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
665 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
669 static const struct file_operations lineevent_fileops = {
670 .release = lineevent_release,
671 .read = lineevent_read,
672 .poll = lineevent_poll,
673 .owner = THIS_MODULE,
674 .llseek = noop_llseek,
675 .unlocked_ioctl = lineevent_ioctl,
677 .compat_ioctl = lineevent_ioctl_compat,
681 static irqreturn_t lineevent_irq_thread(int irq, void *p)
683 struct lineevent_state *le = p;
684 struct gpioevent_data ge;
687 ge.timestamp = ktime_get_real_ns();
689 if (le->eflags & GPIOEVENT_REQUEST_BOTH_EDGES) {
690 int level = gpiod_get_value_cansleep(le->desc);
693 /* Emit low-to-high event */
694 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
696 /* Emit high-to-low event */
697 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
698 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
699 /* Emit low-to-high event */
700 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
701 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
702 /* Emit high-to-low event */
703 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
708 ret = kfifo_put(&le->events, ge);
710 wake_up_poll(&le->wait, POLLIN);
715 static int lineevent_create(struct gpio_device *gdev, void __user *ip)
717 struct gpioevent_request eventreq;
718 struct lineevent_state *le;
719 struct gpio_desc *desc;
727 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
730 le = kzalloc(sizeof(*le), GFP_KERNEL);
734 get_device(&gdev->dev);
736 /* Make sure this is terminated */
737 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
738 if (strlen(eventreq.consumer_label)) {
739 le->label = kstrdup(eventreq.consumer_label,
747 offset = eventreq.lineoffset;
748 lflags = eventreq.handleflags;
749 eflags = eventreq.eventflags;
751 if (offset >= gdev->ngpio) {
756 /* This is just wrong: we don't look for events on output lines */
757 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
762 desc = &gdev->descs[offset];
763 ret = gpiod_request(desc, le->label);
769 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
770 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
771 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
772 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
773 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
774 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
776 ret = gpiod_direction_input(desc);
780 le->irq = gpiod_to_irq(desc);
786 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
787 irqflags |= IRQF_TRIGGER_RISING;
788 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
789 irqflags |= IRQF_TRIGGER_FALLING;
790 irqflags |= IRQF_ONESHOT;
791 irqflags |= IRQF_SHARED;
793 INIT_KFIFO(le->events);
794 init_waitqueue_head(&le->wait);
795 mutex_init(&le->read_lock);
797 /* Request a thread to read the events */
798 ret = request_threaded_irq(le->irq,
800 lineevent_irq_thread,
807 fd = anon_inode_getfd("gpio-event",
810 O_RDONLY | O_CLOEXEC);
817 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
825 free_irq(le->irq, le);
827 gpiod_free(le->desc);
832 put_device(&gdev->dev);
837 * gpio_ioctl() - ioctl handler for the GPIO chardev
839 static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
841 struct gpio_device *gdev = filp->private_data;
842 struct gpio_chip *chip = gdev->chip;
843 void __user *ip = (void __user *)arg;
845 /* We fail any subsequent ioctl():s when the chip is gone */
849 /* Fill in the struct and pass to userspace */
850 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
851 struct gpiochip_info chipinfo;
853 memset(&chipinfo, 0, sizeof(chipinfo));
855 strncpy(chipinfo.name, dev_name(&gdev->dev),
856 sizeof(chipinfo.name));
857 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
858 strncpy(chipinfo.label, gdev->label,
859 sizeof(chipinfo.label));
860 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
861 chipinfo.lines = gdev->ngpio;
862 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
865 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
866 struct gpioline_info lineinfo;
867 struct gpio_desc *desc;
869 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
871 if (lineinfo.line_offset >= gdev->ngpio)
874 desc = &gdev->descs[lineinfo.line_offset];
876 strncpy(lineinfo.name, desc->name,
877 sizeof(lineinfo.name));
878 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
880 lineinfo.name[0] = '\0';
883 strncpy(lineinfo.consumer, desc->label,
884 sizeof(lineinfo.consumer));
885 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
887 lineinfo.consumer[0] = '\0';
891 * Userspace only need to know that the kernel is using
892 * this GPIO so it can't use it.
895 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
896 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
897 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
898 test_bit(FLAG_EXPORT, &desc->flags) ||
899 test_bit(FLAG_SYSFS, &desc->flags))
900 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
901 if (test_bit(FLAG_IS_OUT, &desc->flags))
902 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
903 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
904 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
905 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
906 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
907 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
908 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
910 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
913 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
914 return linehandle_create(gdev, ip);
915 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
916 return lineevent_create(gdev, ip);
922 static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
925 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
930 * gpio_chrdev_open() - open the chardev for ioctl operations
931 * @inode: inode for this chardev
932 * @filp: file struct for storing private data
933 * Returns 0 on success
935 static int gpio_chrdev_open(struct inode *inode, struct file *filp)
937 struct gpio_device *gdev = container_of(inode->i_cdev,
938 struct gpio_device, chrdev);
940 /* Fail on open if the backing gpiochip is gone */
941 if (!gdev || !gdev->chip)
943 get_device(&gdev->dev);
944 filp->private_data = gdev;
949 * gpio_chrdev_release() - close chardev after ioctl operations
950 * @inode: inode for this chardev
951 * @filp: file struct for storing private data
952 * Returns 0 on success
954 static int gpio_chrdev_release(struct inode *inode, struct file *filp)
956 struct gpio_device *gdev = container_of(inode->i_cdev,
957 struct gpio_device, chrdev);
961 put_device(&gdev->dev);
966 static const struct file_operations gpio_fileops = {
967 .release = gpio_chrdev_release,
968 .open = gpio_chrdev_open,
969 .owner = THIS_MODULE,
970 .llseek = noop_llseek,
971 .unlocked_ioctl = gpio_ioctl,
973 .compat_ioctl = gpio_ioctl_compat,
977 static void gpiodevice_release(struct device *dev)
979 struct gpio_device *gdev = dev_get_drvdata(dev);
981 list_del(&gdev->list);
982 ida_simple_remove(&gpio_ida, gdev->id);
988 static int gpiochip_setup_dev(struct gpio_device *gdev)
992 cdev_init(&gdev->chrdev, &gpio_fileops);
993 gdev->chrdev.owner = THIS_MODULE;
994 gdev->chrdev.kobj.parent = &gdev->dev.kobj;
995 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
996 status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
998 chip_warn(gdev->chip, "failed to add char device %d:%d\n",
999 MAJOR(gpio_devt), gdev->id);
1001 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1002 MAJOR(gpio_devt), gdev->id);
1003 status = device_add(&gdev->dev);
1005 goto err_remove_chardev;
1007 status = gpiochip_sysfs_register(gdev);
1009 goto err_remove_device;
1011 /* From this point, the .release() function cleans up gpio_device */
1012 gdev->dev.release = gpiodevice_release;
1013 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1014 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1015 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1020 device_del(&gdev->dev);
1022 cdev_del(&gdev->chrdev);
1026 static void gpiochip_setup_devs(void)
1028 struct gpio_device *gdev;
1031 list_for_each_entry(gdev, &gpio_devices, list) {
1032 err = gpiochip_setup_dev(gdev);
1034 pr_err("%s: Failed to initialize gpio device (%d)\n",
1035 dev_name(&gdev->dev), err);
1040 * gpiochip_add_data() - register a gpio_chip
1041 * @chip: the chip to register, with chip->base initialized
1042 * Context: potentially before irqs will work
1044 * Returns a negative errno if the chip can't be registered, such as
1045 * because the chip->base is invalid or already associated with a
1046 * different chip. Otherwise it returns zero as a success code.
1048 * When gpiochip_add_data() is called very early during boot, so that GPIOs
1049 * can be freely used, the chip->parent device must be registered before
1050 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1051 * for GPIOs will fail rudely.
1053 * gpiochip_add_data() must only be called after gpiolib initialization,
1054 * ie after core_initcall().
1056 * If chip->base is negative, this requests dynamic assignment of
1057 * a range of valid GPIOs.
1059 int gpiochip_add_data(struct gpio_chip *chip, void *data)
1061 unsigned long flags;
1064 int base = chip->base;
1065 struct gpio_device *gdev;
1068 * First: allocate and populate the internal stat container, and
1069 * set up the struct device.
1071 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
1074 gdev->dev.bus = &gpio_bus_type;
1076 chip->gpiodev = gdev;
1078 gdev->dev.parent = chip->parent;
1079 gdev->dev.of_node = chip->parent->of_node;
1082 #ifdef CONFIG_OF_GPIO
1083 /* If the gpiochip has an assigned OF node this takes precedence */
1085 gdev->dev.of_node = chip->of_node;
1088 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1093 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1094 device_initialize(&gdev->dev);
1095 dev_set_drvdata(&gdev->dev, gdev);
1096 if (chip->parent && chip->parent->driver)
1097 gdev->owner = chip->parent->driver->owner;
1098 else if (chip->owner)
1099 /* TODO: remove chip->owner */
1100 gdev->owner = chip->owner;
1102 gdev->owner = THIS_MODULE;
1104 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
1110 if (chip->ngpio == 0) {
1111 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
1113 goto err_free_descs;
1117 gdev->label = kstrdup(chip->label, GFP_KERNEL);
1119 gdev->label = kstrdup("unknown", GFP_KERNEL);
1122 goto err_free_descs;
1125 gdev->ngpio = chip->ngpio;
1128 spin_lock_irqsave(&gpio_lock, flags);
1131 * TODO: this allocates a Linux GPIO number base in the global
1132 * GPIO numberspace for this chip. In the long run we want to
1133 * get *rid* of this numberspace and use only descriptors, but
1134 * it may be a pipe dream. It will not happen before we get rid
1135 * of the sysfs interface anyways.
1138 base = gpiochip_find_base(chip->ngpio);
1141 spin_unlock_irqrestore(&gpio_lock, flags);
1142 goto err_free_label;
1145 * TODO: it should not be necessary to reflect the assigned
1146 * base outside of the GPIO subsystem. Go over drivers and
1147 * see if anyone makes use of this, else drop this and assign
1154 status = gpiodev_add_to_list(gdev);
1156 spin_unlock_irqrestore(&gpio_lock, flags);
1157 goto err_free_label;
1160 spin_unlock_irqrestore(&gpio_lock, flags);
1162 for (i = 0; i < chip->ngpio; i++) {
1163 struct gpio_desc *desc = &gdev->descs[i];
1167 * REVISIT: most hardware initializes GPIOs as inputs
1168 * (often with pullups enabled) so power usage is
1169 * minimized. Linux code should set the gpio direction
1170 * first thing; but until it does, and in case
1171 * chip->get_direction is not set, we may expose the
1172 * wrong direction in sysfs.
1175 if (chip->get_direction) {
1177 * If we have .get_direction, set up the initial
1178 * direction flag from the hardware.
1180 int dir = chip->get_direction(chip, i);
1183 set_bit(FLAG_IS_OUT, &desc->flags);
1184 } else if (!chip->direction_input) {
1186 * If the chip lacks the .direction_input callback
1187 * we logically assume all lines are outputs.
1189 set_bit(FLAG_IS_OUT, &desc->flags);
1193 #ifdef CONFIG_PINCTRL
1194 INIT_LIST_HEAD(&gdev->pin_ranges);
1197 status = gpiochip_set_desc_names(chip);
1199 goto err_remove_from_list;
1201 status = gpiochip_irqchip_init_valid_mask(chip);
1203 goto err_remove_from_list;
1205 status = of_gpiochip_add(chip);
1207 goto err_remove_chip;
1209 acpi_gpiochip_add(chip);
1212 * By first adding the chardev, and then adding the device,
1213 * we get a device node entry in sysfs under
1214 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1215 * coldplug of device nodes and other udev business.
1216 * We can do this only if gpiolib has been initialized.
1217 * Otherwise, defer until later.
1219 if (gpiolib_initialized) {
1220 status = gpiochip_setup_dev(gdev);
1222 goto err_remove_chip;
1227 acpi_gpiochip_remove(chip);
1228 gpiochip_free_hogs(chip);
1229 of_gpiochip_remove(chip);
1230 gpiochip_irqchip_free_valid_mask(chip);
1231 err_remove_from_list:
1232 spin_lock_irqsave(&gpio_lock, flags);
1233 list_del(&gdev->list);
1234 spin_unlock_irqrestore(&gpio_lock, flags);
1240 ida_simple_remove(&gpio_ida, gdev->id);
1241 /* failures here can mean systems won't boot... */
1242 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
1243 gdev->base, gdev->base + gdev->ngpio - 1,
1244 chip->label ? : "generic");
1248 EXPORT_SYMBOL_GPL(gpiochip_add_data);
1251 * gpiochip_get_data() - get per-subdriver data for the chip
1253 void *gpiochip_get_data(struct gpio_chip *chip)
1255 return chip->gpiodev->data;
1257 EXPORT_SYMBOL_GPL(gpiochip_get_data);
1260 * gpiochip_remove() - unregister a gpio_chip
1261 * @chip: the chip to unregister
1263 * A gpio_chip with any GPIOs still requested may not be removed.
1265 void gpiochip_remove(struct gpio_chip *chip)
1267 struct gpio_device *gdev = chip->gpiodev;
1268 struct gpio_desc *desc;
1269 unsigned long flags;
1271 bool requested = false;
1273 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
1274 gpiochip_sysfs_unregister(gdev);
1275 /* Numb the device, cancelling all outstanding operations */
1277 gpiochip_irqchip_remove(chip);
1278 acpi_gpiochip_remove(chip);
1279 gpiochip_remove_pin_ranges(chip);
1280 gpiochip_free_hogs(chip);
1281 of_gpiochip_remove(chip);
1283 * We accept no more calls into the driver from this point, so
1284 * NULL the driver data pointer
1288 spin_lock_irqsave(&gpio_lock, flags);
1289 for (i = 0; i < gdev->ngpio; i++) {
1290 desc = &gdev->descs[i];
1291 if (test_bit(FLAG_REQUESTED, &desc->flags))
1294 spin_unlock_irqrestore(&gpio_lock, flags);
1297 dev_crit(&gdev->dev,
1298 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
1301 * The gpiochip side puts its use of the device to rest here:
1302 * if there are no userspace clients, the chardev and device will
1303 * be removed, else it will be dangling until the last user is
1306 cdev_del(&gdev->chrdev);
1307 device_del(&gdev->dev);
1308 put_device(&gdev->dev);
1310 EXPORT_SYMBOL_GPL(gpiochip_remove);
1312 static void devm_gpio_chip_release(struct device *dev, void *res)
1314 struct gpio_chip *chip = *(struct gpio_chip **)res;
1316 gpiochip_remove(chip);
1319 static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1322 struct gpio_chip **r = res;
1333 * devm_gpiochip_add_data() - Resource manager piochip_add_data()
1334 * @dev: the device pointer on which irq_chip belongs to.
1335 * @chip: the chip to register, with chip->base initialized
1336 * Context: potentially before irqs will work
1338 * Returns a negative errno if the chip can't be registered, such as
1339 * because the chip->base is invalid or already associated with a
1340 * different chip. Otherwise it returns zero as a success code.
1342 * The gpio chip automatically be released when the device is unbound.
1344 int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1347 struct gpio_chip **ptr;
1350 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1355 ret = gpiochip_add_data(chip, data);
1362 devres_add(dev, ptr);
1366 EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1369 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1370 * @dev: device for which which resource was allocated
1371 * @chip: the chip to remove
1373 * A gpio_chip with any GPIOs still requested may not be removed.
1375 void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1379 ret = devres_release(dev, devm_gpio_chip_release,
1380 devm_gpio_chip_match, chip);
1384 EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1387 * gpiochip_find() - iterator for locating a specific gpio_chip
1388 * @data: data to pass to match function
1389 * @callback: Callback function to check gpio_chip
1391 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1392 * determined by a user supplied @match callback. The callback should return
1393 * 0 if the device doesn't match and non-zero if it does. If the callback is
1394 * non-zero, this function will return to the caller and not iterate over any
1397 struct gpio_chip *gpiochip_find(void *data,
1398 int (*match)(struct gpio_chip *chip,
1401 struct gpio_device *gdev;
1402 struct gpio_chip *chip = NULL;
1403 unsigned long flags;
1405 spin_lock_irqsave(&gpio_lock, flags);
1406 list_for_each_entry(gdev, &gpio_devices, list)
1407 if (gdev->chip && match(gdev->chip, data)) {
1412 spin_unlock_irqrestore(&gpio_lock, flags);
1416 EXPORT_SYMBOL_GPL(gpiochip_find);
1418 static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1420 const char *name = data;
1422 return !strcmp(chip->label, name);
1425 static struct gpio_chip *find_chip_by_name(const char *name)
1427 return gpiochip_find((void *)name, gpiochip_match_name);
1430 #ifdef CONFIG_GPIOLIB_IRQCHIP
1433 * The following is irqchip helper code for gpiochips.
1436 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1440 if (!gpiochip->irq_need_valid_mask)
1443 gpiochip->irq_valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio),
1444 sizeof(long), GFP_KERNEL);
1445 if (!gpiochip->irq_valid_mask)
1448 /* Assume by default all GPIOs are valid */
1449 for (i = 0; i < gpiochip->ngpio; i++)
1450 set_bit(i, gpiochip->irq_valid_mask);
1455 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1457 kfree(gpiochip->irq_valid_mask);
1458 gpiochip->irq_valid_mask = NULL;
1461 static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1462 unsigned int offset)
1464 /* No mask means all valid */
1465 if (likely(!gpiochip->irq_valid_mask))
1467 return test_bit(offset, gpiochip->irq_valid_mask);
1471 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
1472 * @gpiochip: the gpiochip to set the irqchip chain to
1473 * @irqchip: the irqchip to chain to the gpiochip
1474 * @parent_irq: the irq number corresponding to the parent IRQ for this
1476 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1477 * coming out of the gpiochip. If the interrupt is nested rather than
1478 * cascaded, pass NULL in this handler argument
1480 void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1481 struct irq_chip *irqchip,
1483 irq_flow_handler_t parent_handler)
1485 unsigned int offset;
1487 if (!gpiochip->irqdomain) {
1488 chip_err(gpiochip, "called %s before setting up irqchip\n",
1493 if (parent_handler) {
1494 if (gpiochip->can_sleep) {
1496 "you cannot have chained interrupts on a "
1497 "chip that may sleep\n");
1501 * The parent irqchip is already using the chip_data for this
1502 * irqchip, so our callbacks simply use the handler_data.
1504 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1507 gpiochip->irq_parent = parent_irq;
1510 /* Set the parent IRQ for all affected IRQs */
1511 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1512 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1514 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
1518 EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1521 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1522 * @d: the irqdomain used by this irqchip
1523 * @irq: the global irq number used by this GPIO irqchip irq
1524 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1526 * This function will set up the mapping for a certain IRQ line on a
1527 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1528 * stored inside the gpiochip.
1530 static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1531 irq_hw_number_t hwirq)
1533 struct gpio_chip *chip = d->host_data;
1535 irq_set_chip_data(irq, chip);
1537 * This lock class tells lockdep that GPIO irqs are in a different
1538 * category than their parents, so it won't report false recursion.
1540 irq_set_lockdep_class(irq, chip->lock_key);
1541 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
1542 /* Chips that can sleep need nested thread handlers */
1543 if (chip->can_sleep && !chip->irq_not_threaded)
1544 irq_set_nested_thread(irq, 1);
1545 irq_set_noprobe(irq);
1548 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1549 * is passed as default type.
1551 if (chip->irq_default_type != IRQ_TYPE_NONE)
1552 irq_set_irq_type(irq, chip->irq_default_type);
1557 static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1559 struct gpio_chip *chip = d->host_data;
1561 if (chip->can_sleep)
1562 irq_set_nested_thread(irq, 0);
1563 irq_set_chip_and_handler(irq, NULL, NULL);
1564 irq_set_chip_data(irq, NULL);
1567 static const struct irq_domain_ops gpiochip_domain_ops = {
1568 .map = gpiochip_irq_map,
1569 .unmap = gpiochip_irq_unmap,
1570 /* Virtually all GPIO irqchips are twocell:ed */
1571 .xlate = irq_domain_xlate_twocell,
1574 static int gpiochip_irq_reqres(struct irq_data *d)
1576 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1578 if (!try_module_get(chip->gpiodev->owner))
1581 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
1583 "unable to lock HW IRQ %lu for IRQ\n",
1585 module_put(chip->gpiodev->owner);
1591 static void gpiochip_irq_relres(struct irq_data *d)
1593 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1595 gpiochip_unlock_as_irq(chip, d->hwirq);
1596 module_put(chip->gpiodev->owner);
1599 static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1601 return irq_find_mapping(chip->irqdomain, offset);
1605 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1606 * @gpiochip: the gpiochip to remove the irqchip from
1608 * This is called only from gpiochip_remove()
1610 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1612 unsigned int offset;
1614 acpi_gpiochip_free_interrupts(gpiochip);
1616 if (gpiochip->irq_parent) {
1617 irq_set_chained_handler(gpiochip->irq_parent, NULL);
1618 irq_set_handler_data(gpiochip->irq_parent, NULL);
1621 /* Remove all IRQ mappings and delete the domain */
1622 if (gpiochip->irqdomain) {
1623 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1624 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1626 irq_dispose_mapping(
1627 irq_find_mapping(gpiochip->irqdomain, offset));
1629 irq_domain_remove(gpiochip->irqdomain);
1632 if (gpiochip->irqchip) {
1633 gpiochip->irqchip->irq_request_resources = NULL;
1634 gpiochip->irqchip->irq_release_resources = NULL;
1635 gpiochip->irqchip = NULL;
1638 gpiochip_irqchip_free_valid_mask(gpiochip);
1642 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
1643 * @gpiochip: the gpiochip to add the irqchip to
1644 * @irqchip: the irqchip to add to the gpiochip
1645 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1646 * allocate gpiochip irqs from
1647 * @handler: the irq handler to use (often a predefined irq core function)
1648 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1649 * to have the core avoid setting up any default type in the hardware.
1650 * @lock_key: lockdep class
1652 * This function closely associates a certain irqchip with a certain
1653 * gpiochip, providing an irq domain to translate the local IRQs to
1654 * global irqs in the gpiolib core, and making sure that the gpiochip
1655 * is passed as chip data to all related functions. Driver callbacks
1656 * need to use gpiochip_get_data() to get their local state containers back
1657 * from the gpiochip passed as chip data. An irqdomain will be stored
1658 * in the gpiochip that shall be used by the driver to handle IRQ number
1659 * translation. The gpiochip will need to be initialized and registered
1660 * before calling this function.
1662 * This function will handle two cell:ed simple IRQs and assumes all
1663 * the pins on the gpiochip can generate a unique IRQ. Everything else
1664 * need to be open coded.
1666 int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
1667 struct irq_chip *irqchip,
1668 unsigned int first_irq,
1669 irq_flow_handler_t handler,
1671 struct lock_class_key *lock_key)
1673 struct device_node *of_node;
1674 bool irq_base_set = false;
1675 unsigned int offset;
1676 unsigned irq_base = 0;
1678 if (!gpiochip || !irqchip)
1681 if (!gpiochip->parent) {
1682 pr_err("missing gpiochip .dev parent pointer\n");
1685 of_node = gpiochip->parent->of_node;
1686 #ifdef CONFIG_OF_GPIO
1688 * If the gpiochip has an assigned OF node this takes precedence
1689 * FIXME: get rid of this and use gpiochip->parent->of_node
1692 if (gpiochip->of_node)
1693 of_node = gpiochip->of_node;
1696 * Specifying a default trigger is a terrible idea if DT or ACPI is
1697 * used to configure the interrupts, as you may end-up with
1698 * conflicting triggers. Tell the user, and reset to NONE.
1700 if (WARN(of_node && type != IRQ_TYPE_NONE,
1701 "%s: Ignoring %d default trigger\n", of_node->full_name, type))
1702 type = IRQ_TYPE_NONE;
1703 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1704 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1705 "Ignoring %d default trigger\n", type);
1706 type = IRQ_TYPE_NONE;
1709 gpiochip->irqchip = irqchip;
1710 gpiochip->irq_handler = handler;
1711 gpiochip->irq_default_type = type;
1712 gpiochip->to_irq = gpiochip_to_irq;
1713 gpiochip->lock_key = lock_key;
1714 gpiochip->irqdomain = irq_domain_add_simple(of_node,
1715 gpiochip->ngpio, first_irq,
1716 &gpiochip_domain_ops, gpiochip);
1717 if (!gpiochip->irqdomain) {
1718 gpiochip->irqchip = NULL;
1723 * It is possible for a driver to override this, but only if the
1724 * alternative functions are both implemented.
1726 if (!irqchip->irq_request_resources &&
1727 !irqchip->irq_release_resources) {
1728 irqchip->irq_request_resources = gpiochip_irq_reqres;
1729 irqchip->irq_release_resources = gpiochip_irq_relres;
1733 * Prepare the mapping since the irqchip shall be orthogonal to
1734 * any gpiochip calls. If the first_irq was zero, this is
1735 * necessary to allocate descriptors for all IRQs.
1737 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1738 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1740 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
1741 if (!irq_base_set) {
1743 * Store the base into the gpiochip to be used when
1744 * unmapping the irqs.
1746 gpiochip->irq_base = irq_base;
1747 irq_base_set = true;
1751 acpi_gpiochip_request_interrupts(gpiochip);
1755 EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
1757 #else /* CONFIG_GPIOLIB_IRQCHIP */
1759 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
1760 static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1764 static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1767 #endif /* CONFIG_GPIOLIB_IRQCHIP */
1770 * gpiochip_generic_request() - request the gpio function for a pin
1771 * @chip: the gpiochip owning the GPIO
1772 * @offset: the offset of the GPIO to request for GPIO function
1774 int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1776 return pinctrl_request_gpio(chip->gpiodev->base + offset);
1778 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1781 * gpiochip_generic_free() - free the gpio function from a pin
1782 * @chip: the gpiochip to request the gpio function for
1783 * @offset: the offset of the GPIO to free from GPIO function
1785 void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1787 pinctrl_free_gpio(chip->gpiodev->base + offset);
1789 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1791 #ifdef CONFIG_PINCTRL
1794 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1795 * @chip: the gpiochip to add the range for
1796 * @pctldev: the pin controller to map to
1797 * @gpio_offset: the start offset in the current gpio_chip number space
1798 * @pin_group: name of the pin group inside the pin controller
1800 int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1801 struct pinctrl_dev *pctldev,
1802 unsigned int gpio_offset, const char *pin_group)
1804 struct gpio_pin_range *pin_range;
1805 struct gpio_device *gdev = chip->gpiodev;
1808 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1810 chip_err(chip, "failed to allocate pin ranges\n");
1814 /* Use local offset as range ID */
1815 pin_range->range.id = gpio_offset;
1816 pin_range->range.gc = chip;
1817 pin_range->range.name = chip->label;
1818 pin_range->range.base = gdev->base + gpio_offset;
1819 pin_range->pctldev = pctldev;
1821 ret = pinctrl_get_group_pins(pctldev, pin_group,
1822 &pin_range->range.pins,
1823 &pin_range->range.npins);
1829 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1831 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1832 gpio_offset, gpio_offset + pin_range->range.npins - 1,
1833 pinctrl_dev_get_devname(pctldev), pin_group);
1835 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1839 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1842 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1843 * @chip: the gpiochip to add the range for
1844 * @pinctrl_name: the dev_name() of the pin controller to map to
1845 * @gpio_offset: the start offset in the current gpio_chip number space
1846 * @pin_offset: the start offset in the pin controller number space
1847 * @npins: the number of pins from the offset of each pin space (GPIO and
1848 * pin controller) to accumulate in this range
1850 int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
1851 unsigned int gpio_offset, unsigned int pin_offset,
1854 struct gpio_pin_range *pin_range;
1855 struct gpio_device *gdev = chip->gpiodev;
1858 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1860 chip_err(chip, "failed to allocate pin ranges\n");
1864 /* Use local offset as range ID */
1865 pin_range->range.id = gpio_offset;
1866 pin_range->range.gc = chip;
1867 pin_range->range.name = chip->label;
1868 pin_range->range.base = gdev->base + gpio_offset;
1869 pin_range->range.pin_base = pin_offset;
1870 pin_range->range.npins = npins;
1871 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
1873 if (IS_ERR(pin_range->pctldev)) {
1874 ret = PTR_ERR(pin_range->pctldev);
1875 chip_err(chip, "could not create pin range\n");
1879 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1880 gpio_offset, gpio_offset + npins - 1,
1882 pin_offset, pin_offset + npins - 1);
1884 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1888 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
1891 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1892 * @chip: the chip to remove all the mappings for
1894 void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1896 struct gpio_pin_range *pin_range, *tmp;
1897 struct gpio_device *gdev = chip->gpiodev;
1899 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
1900 list_del(&pin_range->node);
1901 pinctrl_remove_gpio_range(pin_range->pctldev,
1906 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1908 #endif /* CONFIG_PINCTRL */
1910 /* These "optional" allocation calls help prevent drivers from stomping
1911 * on each other, and help provide better diagnostics in debugfs.
1912 * They're called even less than the "set direction" calls.
1914 static int __gpiod_request(struct gpio_desc *desc, const char *label)
1916 struct gpio_chip *chip = desc->gdev->chip;
1918 unsigned long flags;
1920 spin_lock_irqsave(&gpio_lock, flags);
1922 /* NOTE: gpio_request() can be called in early boot,
1923 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1926 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1927 desc_set_label(desc, label ? : "?");
1934 if (chip->request) {
1935 /* chip->request may sleep */
1936 spin_unlock_irqrestore(&gpio_lock, flags);
1937 status = chip->request(chip, gpio_chip_hwgpio(desc));
1938 spin_lock_irqsave(&gpio_lock, flags);
1941 desc_set_label(desc, NULL);
1942 clear_bit(FLAG_REQUESTED, &desc->flags);
1946 if (chip->get_direction) {
1947 /* chip->get_direction may sleep */
1948 spin_unlock_irqrestore(&gpio_lock, flags);
1949 gpiod_get_direction(desc);
1950 spin_lock_irqsave(&gpio_lock, flags);
1953 spin_unlock_irqrestore(&gpio_lock, flags);
1958 * This descriptor validation needs to be inserted verbatim into each
1959 * function taking a descriptor, so we need to use a preprocessor
1960 * macro to avoid endless duplication. If the desc is NULL it is an
1961 * optional GPIO and calls should just bail out.
1963 #define VALIDATE_DESC(desc) do { \
1966 if (IS_ERR(desc)) { \
1967 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
1968 return PTR_ERR(desc); \
1970 if (!desc->gdev) { \
1971 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
1974 if ( !desc->gdev->chip ) { \
1975 dev_warn(&desc->gdev->dev, \
1976 "%s: backing chip is gone\n", __func__); \
1980 #define VALIDATE_DESC_VOID(desc) do { \
1983 if (IS_ERR(desc)) { \
1984 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
1987 if (!desc->gdev) { \
1988 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
1991 if (!desc->gdev->chip) { \
1992 dev_warn(&desc->gdev->dev, \
1993 "%s: backing chip is gone\n", __func__); \
1998 int gpiod_request(struct gpio_desc *desc, const char *label)
2000 int status = -EPROBE_DEFER;
2001 struct gpio_device *gdev;
2003 VALIDATE_DESC(desc);
2006 if (try_module_get(gdev->owner)) {
2007 status = __gpiod_request(desc, label);
2009 module_put(gdev->owner);
2011 get_device(&gdev->dev);
2015 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
2020 static bool __gpiod_free(struct gpio_desc *desc)
2023 unsigned long flags;
2024 struct gpio_chip *chip;
2028 gpiod_unexport(desc);
2030 spin_lock_irqsave(&gpio_lock, flags);
2032 chip = desc->gdev->chip;
2033 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2035 spin_unlock_irqrestore(&gpio_lock, flags);
2036 might_sleep_if(chip->can_sleep);
2037 chip->free(chip, gpio_chip_hwgpio(desc));
2038 spin_lock_irqsave(&gpio_lock, flags);
2040 desc_set_label(desc, NULL);
2041 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
2042 clear_bit(FLAG_REQUESTED, &desc->flags);
2043 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
2044 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
2045 clear_bit(FLAG_IS_HOGGED, &desc->flags);
2049 spin_unlock_irqrestore(&gpio_lock, flags);
2053 void gpiod_free(struct gpio_desc *desc)
2055 if (desc && desc->gdev && __gpiod_free(desc)) {
2056 module_put(desc->gdev->owner);
2057 put_device(&desc->gdev->dev);
2059 WARN_ON(extra_checks);
2064 * gpiochip_is_requested - return string iff signal was requested
2065 * @chip: controller managing the signal
2066 * @offset: of signal within controller's 0..(ngpio - 1) range
2068 * Returns NULL if the GPIO is not currently requested, else a string.
2069 * The string returned is the label passed to gpio_request(); if none has been
2070 * passed it is a meaningless, non-NULL constant.
2072 * This function is for use by GPIO controller drivers. The label can
2073 * help with diagnostics, and knowing that the signal is used as a GPIO
2074 * can help avoid accidentally multiplexing it to another controller.
2076 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2078 struct gpio_desc *desc;
2080 if (offset >= chip->ngpio)
2083 desc = &chip->gpiodev->descs[offset];
2085 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
2089 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2092 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2093 * @desc: GPIO descriptor to request
2094 * @label: label for the GPIO
2096 * Function allows GPIO chip drivers to request and use their own GPIO
2097 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2098 * function will not increase reference count of the GPIO chip module. This
2099 * allows the GPIO chip module to be unloaded as needed (we assume that the
2100 * GPIO chip driver handles freeing the GPIOs it has requested).
2102 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2105 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2109 chip_err(chip, "failed to get GPIO descriptor\n");
2113 err = __gpiod_request(desc, label);
2115 return ERR_PTR(err);
2119 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
2122 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2123 * @desc: GPIO descriptor to free
2125 * Function frees the given GPIO requested previously with
2126 * gpiochip_request_own_desc().
2128 void gpiochip_free_own_desc(struct gpio_desc *desc)
2133 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
2136 * Drivers MUST set GPIO direction before making get/set calls. In
2137 * some cases this is done in early boot, before IRQs are enabled.
2139 * As a rule these aren't called more than once (except for drivers
2140 * using the open-drain emulation idiom) so these are natural places
2141 * to accumulate extra debugging checks. Note that we can't (yet)
2142 * rely on gpio_request() having been called beforehand.
2146 * gpiod_direction_input - set the GPIO direction to input
2147 * @desc: GPIO to set to input
2149 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2150 * be called safely on it.
2152 * Return 0 in case of success, else an error code.
2154 int gpiod_direction_input(struct gpio_desc *desc)
2156 struct gpio_chip *chip;
2157 int status = -EINVAL;
2159 VALIDATE_DESC(desc);
2160 chip = desc->gdev->chip;
2162 if (!chip->get || !chip->direction_input) {
2164 "%s: missing get() or direction_input() operations\n",
2169 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
2171 clear_bit(FLAG_IS_OUT, &desc->flags);
2173 trace_gpio_direction(desc_to_gpio(desc), 1, status);
2177 EXPORT_SYMBOL_GPL(gpiod_direction_input);
2179 static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2181 struct gpio_chip *gc = desc->gdev->chip;
2184 /* GPIOs used for IRQs shall not be set as output */
2185 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2187 "%s: tried to set a GPIO tied to an IRQ as output\n",
2192 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2193 /* First see if we can enable open drain in hardware */
2194 if (gc->set_single_ended) {
2195 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
2196 LINE_MODE_OPEN_DRAIN);
2198 goto set_output_value;
2200 /* Emulate open drain by not actively driving the line high */
2202 return gpiod_direction_input(desc);
2204 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2205 if (gc->set_single_ended) {
2206 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
2207 LINE_MODE_OPEN_SOURCE);
2209 goto set_output_value;
2211 /* Emulate open source by not actively driving the line low */
2213 return gpiod_direction_input(desc);
2215 /* Make sure to disable open drain/source hardware, if any */
2216 if (gc->set_single_ended)
2217 gc->set_single_ended(gc,
2218 gpio_chip_hwgpio(desc),
2219 LINE_MODE_PUSH_PULL);
2223 if (!gc->set || !gc->direction_output) {
2225 "%s: missing set() or direction_output() operations\n",
2230 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), value);
2232 set_bit(FLAG_IS_OUT, &desc->flags);
2233 trace_gpio_value(desc_to_gpio(desc), 0, value);
2234 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2239 * gpiod_direction_output_raw - set the GPIO direction to output
2240 * @desc: GPIO to set to output
2241 * @value: initial output value of the GPIO
2243 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2244 * be called safely on it. The initial value of the output must be specified
2245 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2247 * Return 0 in case of success, else an error code.
2249 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2251 VALIDATE_DESC(desc);
2252 return _gpiod_direction_output_raw(desc, value);
2254 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2257 * gpiod_direction_output - set the GPIO direction to output
2258 * @desc: GPIO to set to output
2259 * @value: initial output value of the GPIO
2261 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2262 * be called safely on it. The initial value of the output must be specified
2263 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2266 * Return 0 in case of success, else an error code.
2268 int gpiod_direction_output(struct gpio_desc *desc, int value)
2270 VALIDATE_DESC(desc);
2271 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2273 return _gpiod_direction_output_raw(desc, value);
2275 EXPORT_SYMBOL_GPL(gpiod_direction_output);
2278 * gpiod_set_debounce - sets @debounce time for a @gpio
2279 * @gpio: the gpio to set debounce time
2280 * @debounce: debounce time is microseconds
2282 * returns -ENOTSUPP if the controller does not support setting
2285 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
2287 struct gpio_chip *chip;
2289 VALIDATE_DESC(desc);
2290 chip = desc->gdev->chip;
2291 if (!chip->set || !chip->set_debounce) {
2293 "%s: missing set() or set_debounce() operations\n",
2298 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
2300 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
2303 * gpiod_is_active_low - test whether a GPIO is active-low or not
2304 * @desc: the gpio descriptor to test
2306 * Returns 1 if the GPIO is active-low, 0 otherwise.
2308 int gpiod_is_active_low(const struct gpio_desc *desc)
2310 VALIDATE_DESC(desc);
2311 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
2313 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
2315 /* I/O calls are only valid after configuration completed; the relevant
2316 * "is this a valid GPIO" error checks should already have been done.
2318 * "Get" operations are often inlinable as reading a pin value register,
2319 * and masking the relevant bit in that register.
2321 * When "set" operations are inlinable, they involve writing that mask to
2322 * one register to set a low value, or a different register to set it high.
2323 * Otherwise locking is needed, so there may be little value to inlining.
2325 *------------------------------------------------------------------------
2327 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2328 * have requested the GPIO. That can include implicit requesting by
2329 * a direction setting call. Marking a gpio as requested locks its chip
2330 * in memory, guaranteeing that these table lookups need no more locking
2331 * and that gpiochip_remove() will fail.
2333 * REVISIT when debugging, consider adding some instrumentation to ensure
2334 * that the GPIO was actually requested.
2337 static int _gpiod_get_raw_value(const struct gpio_desc *desc)
2339 struct gpio_chip *chip;
2343 chip = desc->gdev->chip;
2344 offset = gpio_chip_hwgpio(desc);
2345 value = chip->get ? chip->get(chip, offset) : -EIO;
2346 value = value < 0 ? value : !!value;
2347 trace_gpio_value(desc_to_gpio(desc), 1, value);
2352 * gpiod_get_raw_value() - return a gpio's raw value
2353 * @desc: gpio whose value will be returned
2355 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2356 * its ACTIVE_LOW status, or negative errno on failure.
2358 * This function should be called from contexts where we cannot sleep, and will
2359 * complain if the GPIO chip functions potentially sleep.
2361 int gpiod_get_raw_value(const struct gpio_desc *desc)
2363 VALIDATE_DESC(desc);
2364 /* Should be using gpio_get_value_cansleep() */
2365 WARN_ON(desc->gdev->chip->can_sleep);
2366 return _gpiod_get_raw_value(desc);
2368 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
2371 * gpiod_get_value() - return a gpio's value
2372 * @desc: gpio whose value will be returned
2374 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2375 * account, or negative errno on failure.
2377 * This function should be called from contexts where we cannot sleep, and will
2378 * complain if the GPIO chip functions potentially sleep.
2380 int gpiod_get_value(const struct gpio_desc *desc)
2384 VALIDATE_DESC(desc);
2385 /* Should be using gpio_get_value_cansleep() */
2386 WARN_ON(desc->gdev->chip->can_sleep);
2388 value = _gpiod_get_raw_value(desc);
2392 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2397 EXPORT_SYMBOL_GPL(gpiod_get_value);
2400 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
2401 * @desc: gpio descriptor whose state need to be set.
2402 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2404 static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
2407 struct gpio_chip *chip = desc->gdev->chip;
2408 int offset = gpio_chip_hwgpio(desc);
2411 err = chip->direction_input(chip, offset);
2413 clear_bit(FLAG_IS_OUT, &desc->flags);
2415 err = chip->direction_output(chip, offset, 0);
2417 set_bit(FLAG_IS_OUT, &desc->flags);
2419 trace_gpio_direction(desc_to_gpio(desc), value, err);
2422 "%s: Error in set_value for open drain err %d\n",
2427 * _gpio_set_open_source_value() - Set the open source gpio's value.
2428 * @desc: gpio descriptor whose state need to be set.
2429 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2431 static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
2434 struct gpio_chip *chip = desc->gdev->chip;
2435 int offset = gpio_chip_hwgpio(desc);
2438 err = chip->direction_output(chip, offset, 1);
2440 set_bit(FLAG_IS_OUT, &desc->flags);
2442 err = chip->direction_input(chip, offset);
2444 clear_bit(FLAG_IS_OUT, &desc->flags);
2446 trace_gpio_direction(desc_to_gpio(desc), !value, err);
2449 "%s: Error in set_value for open source err %d\n",
2453 static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
2455 struct gpio_chip *chip;
2457 chip = desc->gdev->chip;
2458 trace_gpio_value(desc_to_gpio(desc), 0, value);
2459 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2460 _gpio_set_open_drain_value(desc, value);
2461 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2462 _gpio_set_open_source_value(desc, value);
2464 chip->set(chip, gpio_chip_hwgpio(desc), value);
2468 * set multiple outputs on the same chip;
2469 * use the chip's set_multiple function if available;
2470 * otherwise set the outputs sequentially;
2471 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2472 * defines which outputs are to be changed
2473 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2474 * defines the values the outputs specified by mask are to be set to
2476 static void gpio_chip_set_multiple(struct gpio_chip *chip,
2477 unsigned long *mask, unsigned long *bits)
2479 if (chip->set_multiple) {
2480 chip->set_multiple(chip, mask, bits);
2483 for (i = 0; i < chip->ngpio; i++) {
2484 if (mask[BIT_WORD(i)] == 0) {
2485 /* no more set bits in this mask word;
2486 * skip ahead to the next word */
2487 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
2490 /* set outputs if the corresponding mask bit is set */
2491 if (__test_and_clear_bit(i, mask))
2492 chip->set(chip, i, test_bit(i, bits));
2497 void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2498 unsigned int array_size,
2499 struct gpio_desc **desc_array,
2504 while (i < array_size) {
2505 struct gpio_chip *chip = desc_array[i]->gdev->chip;
2506 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2507 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2511 WARN_ON(chip->can_sleep);
2513 memset(mask, 0, sizeof(mask));
2515 struct gpio_desc *desc = desc_array[i];
2516 int hwgpio = gpio_chip_hwgpio(desc);
2517 int value = value_array[i];
2519 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2521 trace_gpio_value(desc_to_gpio(desc), 0, value);
2523 * collect all normal outputs belonging to the same chip
2524 * open drain and open source outputs are set individually
2526 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2527 _gpio_set_open_drain_value(desc, value);
2528 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2529 _gpio_set_open_source_value(desc, value);
2531 __set_bit(hwgpio, mask);
2533 __set_bit(hwgpio, bits);
2535 __clear_bit(hwgpio, bits);
2539 } while ((i < array_size) &&
2540 (desc_array[i]->gdev->chip == chip));
2541 /* push collected bits to outputs */
2543 gpio_chip_set_multiple(chip, mask, bits);
2548 * gpiod_set_raw_value() - assign a gpio's raw value
2549 * @desc: gpio whose value will be assigned
2550 * @value: value to assign
2552 * Set the raw value of the GPIO, i.e. the value of its physical line without
2553 * regard for its ACTIVE_LOW status.
2555 * This function should be called from contexts where we cannot sleep, and will
2556 * complain if the GPIO chip functions potentially sleep.
2558 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
2560 VALIDATE_DESC_VOID(desc);
2561 /* Should be using gpiod_set_value_cansleep() */
2562 WARN_ON(desc->gdev->chip->can_sleep);
2563 _gpiod_set_raw_value(desc, value);
2565 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
2568 * gpiod_set_value() - assign a gpio's value
2569 * @desc: gpio whose value will be assigned
2570 * @value: value to assign
2572 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2575 * This function should be called from contexts where we cannot sleep, and will
2576 * complain if the GPIO chip functions potentially sleep.
2578 void gpiod_set_value(struct gpio_desc *desc, int value)
2580 VALIDATE_DESC_VOID(desc);
2581 /* Should be using gpiod_set_value_cansleep() */
2582 WARN_ON(desc->gdev->chip->can_sleep);
2583 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2585 _gpiod_set_raw_value(desc, value);
2587 EXPORT_SYMBOL_GPL(gpiod_set_value);
2590 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
2591 * @array_size: number of elements in the descriptor / value arrays
2592 * @desc_array: array of GPIO descriptors whose values will be assigned
2593 * @value_array: array of values to assign
2595 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2596 * without regard for their ACTIVE_LOW status.
2598 * This function should be called from contexts where we cannot sleep, and will
2599 * complain if the GPIO chip functions potentially sleep.
2601 void gpiod_set_raw_array_value(unsigned int array_size,
2602 struct gpio_desc **desc_array, int *value_array)
2606 gpiod_set_array_value_complex(true, false, array_size, desc_array,
2609 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
2612 * gpiod_set_array_value() - assign values to an array of GPIOs
2613 * @array_size: number of elements in the descriptor / value arrays
2614 * @desc_array: array of GPIO descriptors whose values will be assigned
2615 * @value_array: array of values to assign
2617 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2620 * This function should be called from contexts where we cannot sleep, and will
2621 * complain if the GPIO chip functions potentially sleep.
2623 void gpiod_set_array_value(unsigned int array_size,
2624 struct gpio_desc **desc_array, int *value_array)
2628 gpiod_set_array_value_complex(false, false, array_size, desc_array,
2631 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
2634 * gpiod_cansleep() - report whether gpio value access may sleep
2635 * @desc: gpio to check
2638 int gpiod_cansleep(const struct gpio_desc *desc)
2640 VALIDATE_DESC(desc);
2641 return desc->gdev->chip->can_sleep;
2643 EXPORT_SYMBOL_GPL(gpiod_cansleep);
2646 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2647 * @desc: gpio whose IRQ will be returned (already requested)
2649 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2652 int gpiod_to_irq(const struct gpio_desc *desc)
2654 struct gpio_chip *chip;
2658 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
2659 * requires this function to not return zero on an invalid descriptor
2660 * but rather a negative error number.
2662 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
2665 chip = desc->gdev->chip;
2666 offset = gpio_chip_hwgpio(desc);
2668 int retirq = chip->to_irq(chip, offset);
2670 /* Zero means NO_IRQ */
2678 EXPORT_SYMBOL_GPL(gpiod_to_irq);
2681 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
2682 * @chip: the chip the GPIO to lock belongs to
2683 * @offset: the offset of the GPIO to lock as IRQ
2685 * This is used directly by GPIO drivers that want to lock down
2686 * a certain GPIO line to be used for IRQs.
2688 int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
2690 struct gpio_desc *desc;
2692 desc = gpiochip_get_desc(chip, offset);
2694 return PTR_ERR(desc);
2696 /* Flush direction if something changed behind our back */
2697 if (chip->get_direction) {
2698 int dir = chip->get_direction(chip, offset);
2701 clear_bit(FLAG_IS_OUT, &desc->flags);
2703 set_bit(FLAG_IS_OUT, &desc->flags);
2706 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
2708 "%s: tried to flag a GPIO set as output for IRQ\n",
2713 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
2716 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
2719 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
2720 * @chip: the chip the GPIO to lock belongs to
2721 * @offset: the offset of the GPIO to lock as IRQ
2723 * This is used directly by GPIO drivers that want to indicate
2724 * that a certain GPIO is no longer used exclusively for IRQ.
2726 void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
2728 if (offset >= chip->ngpio)
2731 clear_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
2733 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
2735 bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
2737 if (offset >= chip->ngpio)
2740 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
2742 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
2744 bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
2746 if (offset >= chip->ngpio)
2749 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
2751 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
2753 bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
2755 if (offset >= chip->ngpio)
2758 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
2760 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
2763 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2764 * @desc: gpio whose value will be returned
2766 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2767 * its ACTIVE_LOW status, or negative errno on failure.
2769 * This function is to be called from contexts that can sleep.
2771 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
2773 might_sleep_if(extra_checks);
2774 VALIDATE_DESC(desc);
2775 return _gpiod_get_raw_value(desc);
2777 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
2780 * gpiod_get_value_cansleep() - return a gpio's value
2781 * @desc: gpio whose value will be returned
2783 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2784 * account, or negative errno on failure.
2786 * This function is to be called from contexts that can sleep.
2788 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
2792 might_sleep_if(extra_checks);
2793 VALIDATE_DESC(desc);
2794 value = _gpiod_get_raw_value(desc);
2798 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2803 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
2806 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2807 * @desc: gpio whose value will be assigned
2808 * @value: value to assign
2810 * Set the raw value of the GPIO, i.e. the value of its physical line without
2811 * regard for its ACTIVE_LOW status.
2813 * This function is to be called from contexts that can sleep.
2815 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
2817 might_sleep_if(extra_checks);
2818 VALIDATE_DESC_VOID(desc);
2819 _gpiod_set_raw_value(desc, value);
2821 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
2824 * gpiod_set_value_cansleep() - assign a gpio's value
2825 * @desc: gpio whose value will be assigned
2826 * @value: value to assign
2828 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2831 * This function is to be called from contexts that can sleep.
2833 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
2835 might_sleep_if(extra_checks);
2836 VALIDATE_DESC_VOID(desc);
2837 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2839 _gpiod_set_raw_value(desc, value);
2841 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
2844 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
2845 * @array_size: number of elements in the descriptor / value arrays
2846 * @desc_array: array of GPIO descriptors whose values will be assigned
2847 * @value_array: array of values to assign
2849 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2850 * without regard for their ACTIVE_LOW status.
2852 * This function is to be called from contexts that can sleep.
2854 void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
2855 struct gpio_desc **desc_array,
2858 might_sleep_if(extra_checks);
2861 gpiod_set_array_value_complex(true, true, array_size, desc_array,
2864 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
2867 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
2868 * @array_size: number of elements in the descriptor / value arrays
2869 * @desc_array: array of GPIO descriptors whose values will be assigned
2870 * @value_array: array of values to assign
2872 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2875 * This function is to be called from contexts that can sleep.
2877 void gpiod_set_array_value_cansleep(unsigned int array_size,
2878 struct gpio_desc **desc_array,
2881 might_sleep_if(extra_checks);
2884 gpiod_set_array_value_complex(false, true, array_size, desc_array,
2887 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
2890 * gpiod_add_lookup_table() - register GPIO device consumers
2891 * @table: table of consumers to register
2893 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
2895 mutex_lock(&gpio_lookup_lock);
2897 list_add_tail(&table->list, &gpio_lookup_list);
2899 mutex_unlock(&gpio_lookup_lock);
2903 * gpiod_remove_lookup_table() - unregister GPIO device consumers
2904 * @table: table of consumers to unregister
2906 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
2908 mutex_lock(&gpio_lookup_lock);
2910 list_del(&table->list);
2912 mutex_unlock(&gpio_lookup_lock);
2915 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2917 const char *dev_id = dev ? dev_name(dev) : NULL;
2918 struct gpiod_lookup_table *table;
2920 mutex_lock(&gpio_lookup_lock);
2922 list_for_each_entry(table, &gpio_lookup_list, list) {
2923 if (table->dev_id && dev_id) {
2925 * Valid strings on both ends, must be identical to have
2928 if (!strcmp(table->dev_id, dev_id))
2932 * One of the pointers is NULL, so both must be to have
2935 if (dev_id == table->dev_id)
2942 mutex_unlock(&gpio_lookup_lock);
2946 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
2948 enum gpio_lookup_flags *flags)
2950 struct gpio_desc *desc = ERR_PTR(-ENOENT);
2951 struct gpiod_lookup_table *table;
2952 struct gpiod_lookup *p;
2954 table = gpiod_find_lookup_table(dev);
2958 for (p = &table->table[0]; p->chip_label; p++) {
2959 struct gpio_chip *chip;
2961 /* idx must always match exactly */
2965 /* If the lookup entry has a con_id, require exact match */
2966 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
2969 chip = find_chip_by_name(p->chip_label);
2972 dev_err(dev, "cannot find GPIO chip %s\n",
2974 return ERR_PTR(-ENODEV);
2977 if (chip->ngpio <= p->chip_hwnum) {
2979 "requested GPIO %d is out of range [0..%d] for chip %s\n",
2980 idx, chip->ngpio, chip->label);
2981 return ERR_PTR(-EINVAL);
2984 desc = gpiochip_get_desc(chip, p->chip_hwnum);
2993 static int dt_gpio_count(struct device *dev, const char *con_id)
2999 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3001 snprintf(propname, sizeof(propname), "%s-%s",
3002 con_id, gpio_suffixes[i]);
3004 snprintf(propname, sizeof(propname), "%s",
3007 ret = of_gpio_named_count(dev->of_node, propname);
3014 static int platform_gpio_count(struct device *dev, const char *con_id)
3016 struct gpiod_lookup_table *table;
3017 struct gpiod_lookup *p;
3018 unsigned int count = 0;
3020 table = gpiod_find_lookup_table(dev);
3024 for (p = &table->table[0]; p->chip_label; p++) {
3025 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3026 (!con_id && !p->con_id))
3036 * gpiod_count - return the number of GPIOs associated with a device / function
3037 * or -ENOENT if no GPIO has been assigned to the requested function
3038 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3039 * @con_id: function within the GPIO consumer
3041 int gpiod_count(struct device *dev, const char *con_id)
3043 int count = -ENOENT;
3045 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3046 count = dt_gpio_count(dev, con_id);
3047 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3048 count = acpi_gpio_count(dev, con_id);
3051 count = platform_gpio_count(dev, con_id);
3055 EXPORT_SYMBOL_GPL(gpiod_count);
3058 * gpiod_get - obtain a GPIO for a given GPIO function
3059 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3060 * @con_id: function within the GPIO consumer
3061 * @flags: optional GPIO initialization flags
3063 * Return the GPIO descriptor corresponding to the function con_id of device
3064 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
3065 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
3067 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
3068 enum gpiod_flags flags)
3070 return gpiod_get_index(dev, con_id, 0, flags);
3072 EXPORT_SYMBOL_GPL(gpiod_get);
3075 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3076 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3077 * @con_id: function within the GPIO consumer
3078 * @flags: optional GPIO initialization flags
3080 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3081 * the requested function it will return NULL. This is convenient for drivers
3082 * that need to handle optional GPIOs.
3084 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
3086 enum gpiod_flags flags)
3088 return gpiod_get_index_optional(dev, con_id, 0, flags);
3090 EXPORT_SYMBOL_GPL(gpiod_get_optional);
3094 * gpiod_configure_flags - helper function to configure a given GPIO
3095 * @desc: gpio whose value will be assigned
3096 * @con_id: function within the GPIO consumer
3097 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3099 * @dflags: gpiod_flags - optional GPIO initialization flags
3101 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3102 * requested function and/or index, or another IS_ERR() code if an error
3103 * occurred while trying to acquire the GPIO.
3105 static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
3106 unsigned long lflags, enum gpiod_flags dflags)
3110 if (lflags & GPIO_ACTIVE_LOW)
3111 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3112 if (lflags & GPIO_OPEN_DRAIN)
3113 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3114 if (lflags & GPIO_OPEN_SOURCE)
3115 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3117 /* No particular flag request, return here... */
3118 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3119 pr_debug("no flags found for %s\n", con_id);
3124 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3125 status = gpiod_direction_output(desc,
3126 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
3128 status = gpiod_direction_input(desc);
3134 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
3135 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3136 * @con_id: function within the GPIO consumer
3137 * @idx: index of the GPIO to obtain in the consumer
3138 * @flags: optional GPIO initialization flags
3140 * This variant of gpiod_get() allows to access GPIOs other than the first
3141 * defined one for functions that define several GPIOs.
3143 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3144 * requested function and/or index, or another IS_ERR() code if an error
3145 * occurred while trying to acquire the GPIO.
3147 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
3150 enum gpiod_flags flags)
3152 struct gpio_desc *desc = NULL;
3154 enum gpio_lookup_flags lookupflags = 0;
3156 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3159 /* Using device tree? */
3160 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3161 dev_dbg(dev, "using device tree for GPIO lookup\n");
3162 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3163 } else if (ACPI_COMPANION(dev)) {
3164 dev_dbg(dev, "using ACPI for GPIO lookup\n");
3165 desc = acpi_find_gpio(dev, con_id, idx, flags, &lookupflags);
3170 * Either we are not using DT or ACPI, or their lookup did not return
3171 * a result. In that case, use platform lookup as a fallback.
3173 if (!desc || desc == ERR_PTR(-ENOENT)) {
3174 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
3175 desc = gpiod_find(dev, con_id, idx, &lookupflags);
3179 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
3183 status = gpiod_request(desc, con_id);
3185 return ERR_PTR(status);
3187 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
3189 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3191 return ERR_PTR(status);
3196 EXPORT_SYMBOL_GPL(gpiod_get_index);
3199 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3200 * @fwnode: handle of the firmware node
3201 * @propname: name of the firmware property representing the GPIO
3203 * This function can be used for drivers that get their configuration
3206 * Function properly finds the corresponding GPIO using whatever is the
3207 * underlying firmware interface and then makes sure that the GPIO
3208 * descriptor is requested before it is returned to the caller.
3210 * In case of error an ERR_PTR() is returned.
3212 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
3213 const char *propname)
3215 struct gpio_desc *desc = ERR_PTR(-ENODEV);
3216 bool active_low = false;
3217 bool single_ended = false;
3221 return ERR_PTR(-EINVAL);
3223 if (is_of_node(fwnode)) {
3224 enum of_gpio_flags flags;
3226 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
3228 if (!IS_ERR(desc)) {
3229 active_low = flags & OF_GPIO_ACTIVE_LOW;
3230 single_ended = flags & OF_GPIO_SINGLE_ENDED;
3232 } else if (is_acpi_node(fwnode)) {
3233 struct acpi_gpio_info info;
3235 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
3237 active_low = info.polarity == GPIO_ACTIVE_LOW;
3243 ret = gpiod_request(desc, NULL);
3245 return ERR_PTR(ret);
3248 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3252 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3254 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3259 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3262 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3264 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3265 * @con_id: function within the GPIO consumer
3266 * @index: index of the GPIO to obtain in the consumer
3267 * @flags: optional GPIO initialization flags
3269 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3270 * specified index was assigned to the requested function it will return NULL.
3271 * This is convenient for drivers that need to handle optional GPIOs.
3273 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
3276 enum gpiod_flags flags)
3278 struct gpio_desc *desc;
3280 desc = gpiod_get_index(dev, con_id, index, flags);
3282 if (PTR_ERR(desc) == -ENOENT)
3288 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
3291 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3292 * @desc: gpio whose value will be assigned
3293 * @name: gpio line name
3294 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3296 * @dflags: gpiod_flags - optional GPIO initialization flags
3298 int gpiod_hog(struct gpio_desc *desc, const char *name,
3299 unsigned long lflags, enum gpiod_flags dflags)
3301 struct gpio_chip *chip;
3302 struct gpio_desc *local_desc;
3306 chip = gpiod_to_chip(desc);
3307 hwnum = gpio_chip_hwgpio(desc);
3309 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
3310 if (IS_ERR(local_desc)) {
3311 status = PTR_ERR(local_desc);
3312 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3313 name, chip->label, hwnum, status);
3317 status = gpiod_configure_flags(desc, name, lflags, dflags);
3319 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3320 name, chip->label, hwnum, status);
3321 gpiochip_free_own_desc(desc);
3325 /* Mark GPIO as hogged so it can be identified and removed later */
3326 set_bit(FLAG_IS_HOGGED, &desc->flags);
3328 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3329 desc_to_gpio(desc), name,
3330 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3331 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3332 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3338 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3339 * @chip: gpio chip to act on
3341 * This is only used by of_gpiochip_remove to free hogged gpios
3343 static void gpiochip_free_hogs(struct gpio_chip *chip)
3347 for (id = 0; id < chip->ngpio; id++) {
3348 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
3349 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
3354 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
3355 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3356 * @con_id: function within the GPIO consumer
3357 * @flags: optional GPIO initialization flags
3359 * This function acquires all the GPIOs defined under a given function.
3361 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
3362 * no GPIO has been assigned to the requested function, or another IS_ERR()
3363 * code if an error occurred while trying to acquire the GPIOs.
3365 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
3367 enum gpiod_flags flags)
3369 struct gpio_desc *desc;
3370 struct gpio_descs *descs;
3373 count = gpiod_count(dev, con_id);
3375 return ERR_PTR(count);
3377 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
3380 return ERR_PTR(-ENOMEM);
3382 for (descs->ndescs = 0; descs->ndescs < count; ) {
3383 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
3385 gpiod_put_array(descs);
3386 return ERR_CAST(desc);
3388 descs->desc[descs->ndescs] = desc;
3393 EXPORT_SYMBOL_GPL(gpiod_get_array);
3396 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
3398 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3399 * @con_id: function within the GPIO consumer
3400 * @flags: optional GPIO initialization flags
3402 * This is equivalent to gpiod_get_array(), except that when no GPIO was
3403 * assigned to the requested function it will return NULL.
3405 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
3407 enum gpiod_flags flags)
3409 struct gpio_descs *descs;
3411 descs = gpiod_get_array(dev, con_id, flags);
3412 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
3417 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
3420 * gpiod_put - dispose of a GPIO descriptor
3421 * @desc: GPIO descriptor to dispose of
3423 * No descriptor can be used after gpiod_put() has been called on it.
3425 void gpiod_put(struct gpio_desc *desc)
3429 EXPORT_SYMBOL_GPL(gpiod_put);
3432 * gpiod_put_array - dispose of multiple GPIO descriptors
3433 * @descs: struct gpio_descs containing an array of descriptors
3435 void gpiod_put_array(struct gpio_descs *descs)
3439 for (i = 0; i < descs->ndescs; i++)
3440 gpiod_put(descs->desc[i]);
3444 EXPORT_SYMBOL_GPL(gpiod_put_array);
3446 static int __init gpiolib_dev_init(void)
3450 /* Register GPIO sysfs bus */
3451 ret = bus_register(&gpio_bus_type);
3453 pr_err("gpiolib: could not register GPIO bus type\n");
3457 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
3459 pr_err("gpiolib: failed to allocate char dev region\n");
3460 bus_unregister(&gpio_bus_type);
3462 gpiolib_initialized = true;
3463 gpiochip_setup_devs();
3467 core_initcall(gpiolib_dev_init);
3469 #ifdef CONFIG_DEBUG_FS
3471 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
3474 struct gpio_chip *chip = gdev->chip;
3475 unsigned gpio = gdev->base;
3476 struct gpio_desc *gdesc = &gdev->descs[0];
3480 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
3481 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
3483 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
3489 gpiod_get_direction(gdesc);
3490 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
3491 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
3492 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
3493 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
3494 is_out ? "out" : "in ",
3496 ? (chip->get(chip, i) ? "hi" : "lo")
3498 is_irq ? "IRQ" : " ");
3499 seq_printf(s, "\n");
3503 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
3505 unsigned long flags;
3506 struct gpio_device *gdev = NULL;
3507 loff_t index = *pos;
3511 spin_lock_irqsave(&gpio_lock, flags);
3512 list_for_each_entry(gdev, &gpio_devices, list)
3514 spin_unlock_irqrestore(&gpio_lock, flags);
3517 spin_unlock_irqrestore(&gpio_lock, flags);
3522 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
3524 unsigned long flags;
3525 struct gpio_device *gdev = v;
3528 spin_lock_irqsave(&gpio_lock, flags);
3529 if (list_is_last(&gdev->list, &gpio_devices))
3532 ret = list_entry(gdev->list.next, struct gpio_device, list);
3533 spin_unlock_irqrestore(&gpio_lock, flags);
3541 static void gpiolib_seq_stop(struct seq_file *s, void *v)
3545 static int gpiolib_seq_show(struct seq_file *s, void *v)
3547 struct gpio_device *gdev = v;
3548 struct gpio_chip *chip = gdev->chip;
3549 struct device *parent;
3552 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
3553 dev_name(&gdev->dev));
3557 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
3558 dev_name(&gdev->dev),
3559 gdev->base, gdev->base + gdev->ngpio - 1);
3560 parent = chip->parent;
3562 seq_printf(s, ", parent: %s/%s",
3563 parent->bus ? parent->bus->name : "no-bus",
3566 seq_printf(s, ", %s", chip->label);
3567 if (chip->can_sleep)
3568 seq_printf(s, ", can sleep");
3569 seq_printf(s, ":\n");
3572 chip->dbg_show(s, chip);
3574 gpiolib_dbg_show(s, gdev);
3579 static const struct seq_operations gpiolib_seq_ops = {
3580 .start = gpiolib_seq_start,
3581 .next = gpiolib_seq_next,
3582 .stop = gpiolib_seq_stop,
3583 .show = gpiolib_seq_show,
3586 static int gpiolib_open(struct inode *inode, struct file *file)
3588 return seq_open(file, &gpiolib_seq_ops);
3591 static const struct file_operations gpiolib_operations = {
3592 .owner = THIS_MODULE,
3593 .open = gpiolib_open,
3595 .llseek = seq_lseek,
3596 .release = seq_release,
3599 static int __init gpiolib_debugfs_init(void)
3601 /* /sys/kernel/debug/gpio */
3602 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
3603 NULL, NULL, &gpiolib_operations);
3606 subsys_initcall(gpiolib_debugfs_init);
3608 #endif /* DEBUG_FS */