1 #include <linux/bitmap.h>
2 #include <linux/kernel.h>
3 #include <linux/module.h>
4 #include <linux/interrupt.h>
6 #include <linux/spinlock.h>
7 #include <linux/list.h>
8 #include <linux/device.h>
10 #include <linux/debugfs.h>
11 #include <linux/seq_file.h>
12 #include <linux/gpio.h>
13 #include <linux/of_gpio.h>
14 #include <linux/idr.h>
15 #include <linux/slab.h>
16 #include <linux/acpi.h>
17 #include <linux/gpio/driver.h>
18 #include <linux/gpio/machine.h>
19 #include <linux/pinctrl/consumer.h>
20 #include <linux/cdev.h>
22 #include <linux/uaccess.h>
23 #include <linux/compat.h>
24 #include <linux/anon_inodes.h>
25 #include <linux/file.h>
26 #include <linux/kfifo.h>
27 #include <linux/poll.h>
28 #include <linux/timekeeping.h>
29 #include <uapi/linux/gpio.h>
33 #define CREATE_TRACE_POINTS
34 #include <trace/events/gpio.h>
36 /* Implementation infrastructure for GPIO interfaces.
38 * The GPIO programming interface allows for inlining speed-critical
39 * get/set operations for common cases, so that access to SOC-integrated
40 * GPIOs can sometimes cost only an instruction or two per bit.
44 /* When debugging, extend minimal trust to callers and platform code.
45 * Also emit diagnostic messages that may help initial bringup, when
46 * board setup or driver bugs are most common.
48 * Otherwise, minimize overhead in what may be bitbanging codepaths.
51 #define extra_checks 1
53 #define extra_checks 0
56 /* Device and char device-related information */
57 static DEFINE_IDA(gpio_ida);
58 static dev_t gpio_devt;
59 #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
60 static struct bus_type gpio_bus_type = {
64 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
65 * While any GPIO is requested, its gpio_chip is not removable;
66 * each GPIO's "requested" flag serves as a lock and refcount.
68 DEFINE_SPINLOCK(gpio_lock);
70 static DEFINE_MUTEX(gpio_lookup_lock);
71 static LIST_HEAD(gpio_lookup_list);
72 LIST_HEAD(gpio_devices);
74 static void gpiochip_free_hogs(struct gpio_chip *chip);
75 static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
76 struct lock_class_key *lock_key,
77 struct lock_class_key *request_key);
78 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
79 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
80 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
82 static bool gpiolib_initialized;
84 static inline void desc_set_label(struct gpio_desc *d, const char *label)
90 * gpio_to_desc - Convert a GPIO number to its descriptor
91 * @gpio: global GPIO number
94 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
95 * with the given number exists in the system.
97 struct gpio_desc *gpio_to_desc(unsigned gpio)
99 struct gpio_device *gdev;
102 spin_lock_irqsave(&gpio_lock, flags);
104 list_for_each_entry(gdev, &gpio_devices, list) {
105 if (gdev->base <= gpio &&
106 gdev->base + gdev->ngpio > gpio) {
107 spin_unlock_irqrestore(&gpio_lock, flags);
108 return &gdev->descs[gpio - gdev->base];
112 spin_unlock_irqrestore(&gpio_lock, flags);
114 if (!gpio_is_valid(gpio))
115 WARN(1, "invalid GPIO %d\n", gpio);
119 EXPORT_SYMBOL_GPL(gpio_to_desc);
122 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
123 * hardware number for this chip
125 * @hwnum: hardware number of the GPIO for this chip
128 * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists
129 * in the given chip for the specified hardware number.
131 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
134 struct gpio_device *gdev = chip->gpiodev;
136 if (hwnum >= gdev->ngpio)
137 return ERR_PTR(-EINVAL);
139 return &gdev->descs[hwnum];
143 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
144 * @desc: GPIO descriptor
146 * This should disappear in the future but is needed since we still
147 * use GPIO numbers for error messages and sysfs nodes.
150 * The global GPIO number for the GPIO specified by its descriptor.
152 int desc_to_gpio(const struct gpio_desc *desc)
154 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
156 EXPORT_SYMBOL_GPL(desc_to_gpio);
160 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
161 * @desc: descriptor to return the chip of
163 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
165 if (!desc || !desc->gdev)
167 return desc->gdev->chip;
169 EXPORT_SYMBOL_GPL(gpiod_to_chip);
171 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
172 static int gpiochip_find_base(int ngpio)
174 struct gpio_device *gdev;
175 int base = ARCH_NR_GPIOS - ngpio;
177 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
178 /* found a free space? */
179 if (gdev->base + gdev->ngpio <= base)
182 /* nope, check the space right before the chip */
183 base = gdev->base - ngpio;
186 if (gpio_is_valid(base)) {
187 pr_debug("%s: found new base at %d\n", __func__, base);
190 pr_err("%s: cannot find free range\n", __func__);
196 * gpiod_get_direction - return the current direction of a GPIO
197 * @desc: GPIO to get the direction of
199 * Returns 0 for output, 1 for input, or an error code in case of error.
201 * This function may sleep if gpiod_cansleep() is true.
203 int gpiod_get_direction(struct gpio_desc *desc)
205 struct gpio_chip *chip;
207 int status = -EINVAL;
209 chip = gpiod_to_chip(desc);
210 offset = gpio_chip_hwgpio(desc);
212 if (!chip->get_direction)
215 status = chip->get_direction(chip, offset);
217 /* GPIOF_DIR_IN, or other positive */
219 clear_bit(FLAG_IS_OUT, &desc->flags);
223 set_bit(FLAG_IS_OUT, &desc->flags);
227 EXPORT_SYMBOL_GPL(gpiod_get_direction);
230 * Add a new chip to the global chips list, keeping the list of chips sorted
231 * by range(means [base, base + ngpio - 1]) order.
233 * Return -EBUSY if the new chip overlaps with some other chip's integer
236 static int gpiodev_add_to_list(struct gpio_device *gdev)
238 struct gpio_device *prev, *next;
240 if (list_empty(&gpio_devices)) {
241 /* initial entry in list */
242 list_add_tail(&gdev->list, &gpio_devices);
246 next = list_entry(gpio_devices.next, struct gpio_device, list);
247 if (gdev->base + gdev->ngpio <= next->base) {
248 /* add before first entry */
249 list_add(&gdev->list, &gpio_devices);
253 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
254 if (prev->base + prev->ngpio <= gdev->base) {
255 /* add behind last entry */
256 list_add_tail(&gdev->list, &gpio_devices);
260 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
261 /* at the end of the list */
262 if (&next->list == &gpio_devices)
265 /* add between prev and next */
266 if (prev->base + prev->ngpio <= gdev->base
267 && gdev->base + gdev->ngpio <= next->base) {
268 list_add(&gdev->list, &prev->list);
273 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
278 * Convert a GPIO name to its descriptor
280 static struct gpio_desc *gpio_name_to_desc(const char * const name)
282 struct gpio_device *gdev;
285 spin_lock_irqsave(&gpio_lock, flags);
287 list_for_each_entry(gdev, &gpio_devices, list) {
290 for (i = 0; i != gdev->ngpio; ++i) {
291 struct gpio_desc *desc = &gdev->descs[i];
293 if (!desc->name || !name)
296 if (!strcmp(desc->name, name)) {
297 spin_unlock_irqrestore(&gpio_lock, flags);
303 spin_unlock_irqrestore(&gpio_lock, flags);
309 * Takes the names from gc->names and checks if they are all unique. If they
310 * are, they are assigned to their gpio descriptors.
312 * Warning if one of the names is already used for a different GPIO.
314 static int gpiochip_set_desc_names(struct gpio_chip *gc)
316 struct gpio_device *gdev = gc->gpiodev;
322 /* First check all names if they are unique */
323 for (i = 0; i != gc->ngpio; ++i) {
324 struct gpio_desc *gpio;
326 gpio = gpio_name_to_desc(gc->names[i]);
329 "Detected name collision for GPIO name '%s'\n",
333 /* Then add all names to the GPIO descriptors */
334 for (i = 0; i != gc->ngpio; ++i)
335 gdev->descs[i].name = gc->names[i];
340 static unsigned long *gpiochip_allocate_mask(struct gpio_chip *chip)
344 p = kmalloc_array(BITS_TO_LONGS(chip->ngpio), sizeof(*p), GFP_KERNEL);
348 /* Assume by default all GPIOs are valid */
349 bitmap_fill(p, chip->ngpio);
354 static int gpiochip_init_valid_mask(struct gpio_chip *gpiochip)
356 #ifdef CONFIG_OF_GPIO
358 struct device_node *np = gpiochip->of_node;
360 size = of_property_count_u32_elems(np, "gpio-reserved-ranges");
361 if (size > 0 && size % 2 == 0)
362 gpiochip->need_valid_mask = true;
365 if (!gpiochip->need_valid_mask)
368 gpiochip->valid_mask = gpiochip_allocate_mask(gpiochip);
369 if (!gpiochip->valid_mask)
375 static void gpiochip_free_valid_mask(struct gpio_chip *gpiochip)
377 kfree(gpiochip->valid_mask);
378 gpiochip->valid_mask = NULL;
381 bool gpiochip_line_is_valid(const struct gpio_chip *gpiochip,
384 /* No mask means all valid */
385 if (likely(!gpiochip->valid_mask))
387 return test_bit(offset, gpiochip->valid_mask);
389 EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
392 * GPIO line handle management
396 * struct linehandle_state - contains the state of a userspace handle
397 * @gdev: the GPIO device the handle pertains to
398 * @label: consumer label used to tag descriptors
399 * @descs: the GPIO descriptors held by this handle
400 * @numdescs: the number of descriptors held in the descs array
402 struct linehandle_state {
403 struct gpio_device *gdev;
405 struct gpio_desc *descs[GPIOHANDLES_MAX];
409 #define GPIOHANDLE_REQUEST_VALID_FLAGS \
410 (GPIOHANDLE_REQUEST_INPUT | \
411 GPIOHANDLE_REQUEST_OUTPUT | \
412 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
413 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
414 GPIOHANDLE_REQUEST_OPEN_SOURCE)
416 static long linehandle_ioctl(struct file *filep, unsigned int cmd,
419 struct linehandle_state *lh = filep->private_data;
420 void __user *ip = (void __user *)arg;
421 struct gpiohandle_data ghd;
422 int vals[GPIOHANDLES_MAX];
425 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
426 /* TODO: check if descriptors are really input */
427 int ret = gpiod_get_array_value_complex(false,
435 memset(&ghd, 0, sizeof(ghd));
436 for (i = 0; i < lh->numdescs; i++)
437 ghd.values[i] = vals[i];
439 if (copy_to_user(ip, &ghd, sizeof(ghd)))
443 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
444 /* TODO: check if descriptors are really output */
445 if (copy_from_user(&ghd, ip, sizeof(ghd)))
448 /* Clamp all values to [0,1] */
449 for (i = 0; i < lh->numdescs; i++)
450 vals[i] = !!ghd.values[i];
452 /* Reuse the array setting function */
453 gpiod_set_array_value_complex(false,
464 static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
467 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
471 static int linehandle_release(struct inode *inode, struct file *filep)
473 struct linehandle_state *lh = filep->private_data;
474 struct gpio_device *gdev = lh->gdev;
477 for (i = 0; i < lh->numdescs; i++)
478 gpiod_free(lh->descs[i]);
481 put_device(&gdev->dev);
485 static const struct file_operations linehandle_fileops = {
486 .release = linehandle_release,
487 .owner = THIS_MODULE,
488 .llseek = noop_llseek,
489 .unlocked_ioctl = linehandle_ioctl,
491 .compat_ioctl = linehandle_ioctl_compat,
495 static int linehandle_create(struct gpio_device *gdev, void __user *ip)
497 struct gpiohandle_request handlereq;
498 struct linehandle_state *lh;
503 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
505 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
508 lflags = handlereq.flags;
510 /* Return an error if an unknown flag is set */
511 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
515 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
516 * the hardware actually supports enabling both at the same time the
517 * electrical result would be disastrous.
519 if ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
520 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
523 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
524 if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) &&
525 ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
526 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
529 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
533 get_device(&gdev->dev);
535 /* Make sure this is terminated */
536 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
537 if (strlen(handlereq.consumer_label)) {
538 lh->label = kstrdup(handlereq.consumer_label,
546 /* Request each GPIO */
547 for (i = 0; i < handlereq.lines; i++) {
548 u32 offset = handlereq.lineoffsets[i];
549 struct gpio_desc *desc;
551 if (offset >= gdev->ngpio) {
556 desc = &gdev->descs[offset];
557 ret = gpiod_request(desc, lh->label);
562 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
563 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
564 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
565 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
566 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
567 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
569 ret = gpiod_set_transitory(desc, false);
574 * Lines have to be requested explicitly for input
575 * or output, else the line will be treated "as is".
577 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
578 int val = !!handlereq.default_values[i];
580 ret = gpiod_direction_output(desc, val);
583 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
584 ret = gpiod_direction_input(desc);
588 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
591 /* Let i point at the last handle */
593 lh->numdescs = handlereq.lines;
595 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
601 file = anon_inode_getfile("gpio-linehandle",
604 O_RDONLY | O_CLOEXEC);
607 goto out_put_unused_fd;
611 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
613 * fput() will trigger the release() callback, so do not go onto
614 * the regular error cleanup path here.
621 fd_install(fd, file);
623 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
632 gpiod_free(lh->descs[i]);
636 put_device(&gdev->dev);
641 * GPIO line event management
645 * struct lineevent_state - contains the state of a userspace event
646 * @gdev: the GPIO device the event pertains to
647 * @label: consumer label used to tag descriptors
648 * @desc: the GPIO descriptor held by this event
649 * @eflags: the event flags this line was requested with
650 * @irq: the interrupt that trigger in response to events on this GPIO
651 * @wait: wait queue that handles blocking reads of events
652 * @events: KFIFO for the GPIO events
653 * @read_lock: mutex lock to protect reads from colliding with adding
654 * new events to the FIFO
655 * @timestamp: cache for the timestamp storing it between hardirq
656 * and IRQ thread, used to bring the timestamp close to the actual
659 struct lineevent_state {
660 struct gpio_device *gdev;
662 struct gpio_desc *desc;
665 wait_queue_head_t wait;
666 DECLARE_KFIFO(events, struct gpioevent_data, 16);
667 struct mutex read_lock;
671 #define GPIOEVENT_REQUEST_VALID_FLAGS \
672 (GPIOEVENT_REQUEST_RISING_EDGE | \
673 GPIOEVENT_REQUEST_FALLING_EDGE)
675 static __poll_t lineevent_poll(struct file *filep,
676 struct poll_table_struct *wait)
678 struct lineevent_state *le = filep->private_data;
681 poll_wait(filep, &le->wait, wait);
683 if (!kfifo_is_empty(&le->events))
684 events = EPOLLIN | EPOLLRDNORM;
690 static ssize_t lineevent_read(struct file *filep,
695 struct lineevent_state *le = filep->private_data;
699 if (count < sizeof(struct gpioevent_data))
703 if (kfifo_is_empty(&le->events)) {
704 if (filep->f_flags & O_NONBLOCK)
707 ret = wait_event_interruptible(le->wait,
708 !kfifo_is_empty(&le->events));
713 if (mutex_lock_interruptible(&le->read_lock))
715 ret = kfifo_to_user(&le->events, buf, count, &copied);
716 mutex_unlock(&le->read_lock);
722 * If we couldn't read anything from the fifo (a different
723 * thread might have been faster) we either return -EAGAIN if
724 * the file descriptor is non-blocking, otherwise we go back to
725 * sleep and wait for more data to arrive.
727 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
730 } while (copied == 0);
735 static int lineevent_release(struct inode *inode, struct file *filep)
737 struct lineevent_state *le = filep->private_data;
738 struct gpio_device *gdev = le->gdev;
740 free_irq(le->irq, le);
741 gpiod_free(le->desc);
744 put_device(&gdev->dev);
748 static long lineevent_ioctl(struct file *filep, unsigned int cmd,
751 struct lineevent_state *le = filep->private_data;
752 void __user *ip = (void __user *)arg;
753 struct gpiohandle_data ghd;
756 * We can get the value for an event line but not set it,
757 * because it is input by definition.
759 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
762 memset(&ghd, 0, sizeof(ghd));
764 val = gpiod_get_value_cansleep(le->desc);
769 if (copy_to_user(ip, &ghd, sizeof(ghd)))
778 static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
781 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
785 static const struct file_operations lineevent_fileops = {
786 .release = lineevent_release,
787 .read = lineevent_read,
788 .poll = lineevent_poll,
789 .owner = THIS_MODULE,
790 .llseek = noop_llseek,
791 .unlocked_ioctl = lineevent_ioctl,
793 .compat_ioctl = lineevent_ioctl_compat,
797 static irqreturn_t lineevent_irq_thread(int irq, void *p)
799 struct lineevent_state *le = p;
800 struct gpioevent_data ge;
803 /* Do not leak kernel stack to userspace */
804 memset(&ge, 0, sizeof(ge));
806 ge.timestamp = le->timestamp;
807 level = gpiod_get_value_cansleep(le->desc);
809 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
810 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
812 /* Emit low-to-high event */
813 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
815 /* Emit high-to-low event */
816 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
817 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE && level) {
818 /* Emit low-to-high event */
819 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
820 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE && !level) {
821 /* Emit high-to-low event */
822 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
827 ret = kfifo_put(&le->events, ge);
829 wake_up_poll(&le->wait, EPOLLIN);
834 static irqreturn_t lineevent_irq_handler(int irq, void *p)
836 struct lineevent_state *le = p;
839 * Just store the timestamp in hardirq context so we get it as
840 * close in time as possible to the actual event.
842 le->timestamp = ktime_get_real_ns();
844 return IRQ_WAKE_THREAD;
847 static int lineevent_create(struct gpio_device *gdev, void __user *ip)
849 struct gpioevent_request eventreq;
850 struct lineevent_state *le;
851 struct gpio_desc *desc;
860 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
863 le = kzalloc(sizeof(*le), GFP_KERNEL);
867 get_device(&gdev->dev);
869 /* Make sure this is terminated */
870 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
871 if (strlen(eventreq.consumer_label)) {
872 le->label = kstrdup(eventreq.consumer_label,
880 offset = eventreq.lineoffset;
881 lflags = eventreq.handleflags;
882 eflags = eventreq.eventflags;
884 if (offset >= gdev->ngpio) {
889 /* Return an error if a unknown flag is set */
890 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
891 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
896 /* This is just wrong: we don't look for events on output lines */
897 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
902 desc = &gdev->descs[offset];
903 ret = gpiod_request(desc, le->label);
909 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
910 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
911 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
912 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
913 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
914 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
916 ret = gpiod_direction_input(desc);
920 le->irq = gpiod_to_irq(desc);
926 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
927 irqflags |= IRQF_TRIGGER_RISING;
928 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
929 irqflags |= IRQF_TRIGGER_FALLING;
930 irqflags |= IRQF_ONESHOT;
931 irqflags |= IRQF_SHARED;
933 INIT_KFIFO(le->events);
934 init_waitqueue_head(&le->wait);
935 mutex_init(&le->read_lock);
937 /* Request a thread to read the events */
938 ret = request_threaded_irq(le->irq,
939 lineevent_irq_handler,
940 lineevent_irq_thread,
947 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
953 file = anon_inode_getfile("gpio-event",
956 O_RDONLY | O_CLOEXEC);
959 goto out_put_unused_fd;
963 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
965 * fput() will trigger the release() callback, so do not go onto
966 * the regular error cleanup path here.
973 fd_install(fd, file);
980 free_irq(le->irq, le);
982 gpiod_free(le->desc);
987 put_device(&gdev->dev);
992 * gpio_ioctl() - ioctl handler for the GPIO chardev
994 static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
996 struct gpio_device *gdev = filp->private_data;
997 struct gpio_chip *chip = gdev->chip;
998 void __user *ip = (void __user *)arg;
1000 /* We fail any subsequent ioctl():s when the chip is gone */
1004 /* Fill in the struct and pass to userspace */
1005 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
1006 struct gpiochip_info chipinfo;
1008 memset(&chipinfo, 0, sizeof(chipinfo));
1010 strncpy(chipinfo.name, dev_name(&gdev->dev),
1011 sizeof(chipinfo.name));
1012 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
1013 strncpy(chipinfo.label, gdev->label,
1014 sizeof(chipinfo.label));
1015 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
1016 chipinfo.lines = gdev->ngpio;
1017 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
1020 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
1021 struct gpioline_info lineinfo;
1022 struct gpio_desc *desc;
1024 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1026 if (lineinfo.line_offset >= gdev->ngpio)
1029 desc = &gdev->descs[lineinfo.line_offset];
1031 strncpy(lineinfo.name, desc->name,
1032 sizeof(lineinfo.name));
1033 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
1035 lineinfo.name[0] = '\0';
1038 strncpy(lineinfo.consumer, desc->label,
1039 sizeof(lineinfo.consumer));
1040 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
1042 lineinfo.consumer[0] = '\0';
1046 * Userspace only need to know that the kernel is using
1047 * this GPIO so it can't use it.
1050 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
1051 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
1052 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
1053 test_bit(FLAG_EXPORT, &desc->flags) ||
1054 test_bit(FLAG_SYSFS, &desc->flags))
1055 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
1056 if (test_bit(FLAG_IS_OUT, &desc->flags))
1057 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
1058 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1059 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
1060 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1061 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
1062 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1063 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
1065 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
1068 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
1069 return linehandle_create(gdev, ip);
1070 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
1071 return lineevent_create(gdev, ip);
1076 #ifdef CONFIG_COMPAT
1077 static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
1080 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
1085 * gpio_chrdev_open() - open the chardev for ioctl operations
1086 * @inode: inode for this chardev
1087 * @filp: file struct for storing private data
1088 * Returns 0 on success
1090 static int gpio_chrdev_open(struct inode *inode, struct file *filp)
1092 struct gpio_device *gdev = container_of(inode->i_cdev,
1093 struct gpio_device, chrdev);
1095 /* Fail on open if the backing gpiochip is gone */
1098 get_device(&gdev->dev);
1099 filp->private_data = gdev;
1101 return nonseekable_open(inode, filp);
1105 * gpio_chrdev_release() - close chardev after ioctl operations
1106 * @inode: inode for this chardev
1107 * @filp: file struct for storing private data
1108 * Returns 0 on success
1110 static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1112 struct gpio_device *gdev = container_of(inode->i_cdev,
1113 struct gpio_device, chrdev);
1115 put_device(&gdev->dev);
1120 static const struct file_operations gpio_fileops = {
1121 .release = gpio_chrdev_release,
1122 .open = gpio_chrdev_open,
1123 .owner = THIS_MODULE,
1124 .llseek = no_llseek,
1125 .unlocked_ioctl = gpio_ioctl,
1126 #ifdef CONFIG_COMPAT
1127 .compat_ioctl = gpio_ioctl_compat,
1131 static void gpiodevice_release(struct device *dev)
1133 struct gpio_device *gdev = dev_get_drvdata(dev);
1135 list_del(&gdev->list);
1136 ida_simple_remove(&gpio_ida, gdev->id);
1137 kfree_const(gdev->label);
1142 static int gpiochip_setup_dev(struct gpio_device *gdev)
1146 cdev_init(&gdev->chrdev, &gpio_fileops);
1147 gdev->chrdev.owner = THIS_MODULE;
1148 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
1150 status = cdev_device_add(&gdev->chrdev, &gdev->dev);
1154 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1155 MAJOR(gpio_devt), gdev->id);
1157 status = gpiochip_sysfs_register(gdev);
1159 goto err_remove_device;
1161 /* From this point, the .release() function cleans up gpio_device */
1162 gdev->dev.release = gpiodevice_release;
1163 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1164 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1165 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1170 cdev_device_del(&gdev->chrdev, &gdev->dev);
1174 static void gpiochip_setup_devs(void)
1176 struct gpio_device *gdev;
1179 list_for_each_entry(gdev, &gpio_devices, list) {
1180 err = gpiochip_setup_dev(gdev);
1182 pr_err("%s: Failed to initialize gpio device (%d)\n",
1183 dev_name(&gdev->dev), err);
1187 int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
1188 struct lock_class_key *lock_key,
1189 struct lock_class_key *request_key)
1191 unsigned long flags;
1194 int base = chip->base;
1195 struct gpio_device *gdev;
1198 * First: allocate and populate the internal stat container, and
1199 * set up the struct device.
1201 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
1204 gdev->dev.bus = &gpio_bus_type;
1206 chip->gpiodev = gdev;
1208 gdev->dev.parent = chip->parent;
1209 gdev->dev.of_node = chip->parent->of_node;
1212 #ifdef CONFIG_OF_GPIO
1213 /* If the gpiochip has an assigned OF node this takes precedence */
1215 gdev->dev.of_node = chip->of_node;
1218 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1223 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1224 device_initialize(&gdev->dev);
1225 dev_set_drvdata(&gdev->dev, gdev);
1226 if (chip->parent && chip->parent->driver)
1227 gdev->owner = chip->parent->driver->owner;
1228 else if (chip->owner)
1229 /* TODO: remove chip->owner */
1230 gdev->owner = chip->owner;
1232 gdev->owner = THIS_MODULE;
1234 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
1240 if (chip->ngpio == 0) {
1241 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
1243 goto err_free_descs;
1246 gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);
1249 goto err_free_descs;
1252 gdev->ngpio = chip->ngpio;
1255 spin_lock_irqsave(&gpio_lock, flags);
1258 * TODO: this allocates a Linux GPIO number base in the global
1259 * GPIO numberspace for this chip. In the long run we want to
1260 * get *rid* of this numberspace and use only descriptors, but
1261 * it may be a pipe dream. It will not happen before we get rid
1262 * of the sysfs interface anyways.
1265 base = gpiochip_find_base(chip->ngpio);
1268 spin_unlock_irqrestore(&gpio_lock, flags);
1269 goto err_free_label;
1272 * TODO: it should not be necessary to reflect the assigned
1273 * base outside of the GPIO subsystem. Go over drivers and
1274 * see if anyone makes use of this, else drop this and assign
1281 status = gpiodev_add_to_list(gdev);
1283 spin_unlock_irqrestore(&gpio_lock, flags);
1284 goto err_free_label;
1287 spin_unlock_irqrestore(&gpio_lock, flags);
1289 for (i = 0; i < chip->ngpio; i++) {
1290 struct gpio_desc *desc = &gdev->descs[i];
1294 /* REVISIT: most hardware initializes GPIOs as inputs (often
1295 * with pullups enabled) so power usage is minimized. Linux
1296 * code should set the gpio direction first thing; but until
1297 * it does, and in case chip->get_direction is not set, we may
1298 * expose the wrong direction in sysfs.
1300 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
1303 #ifdef CONFIG_PINCTRL
1304 INIT_LIST_HEAD(&gdev->pin_ranges);
1307 status = gpiochip_set_desc_names(chip);
1309 goto err_remove_from_list;
1311 status = gpiochip_irqchip_init_valid_mask(chip);
1313 goto err_remove_from_list;
1315 status = gpiochip_init_valid_mask(chip);
1317 goto err_remove_irqchip_mask;
1319 status = gpiochip_add_irqchip(chip, lock_key, request_key);
1321 goto err_remove_chip;
1323 status = of_gpiochip_add(chip);
1325 goto err_remove_chip;
1327 acpi_gpiochip_add(chip);
1330 * By first adding the chardev, and then adding the device,
1331 * we get a device node entry in sysfs under
1332 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1333 * coldplug of device nodes and other udev business.
1334 * We can do this only if gpiolib has been initialized.
1335 * Otherwise, defer until later.
1337 if (gpiolib_initialized) {
1338 status = gpiochip_setup_dev(gdev);
1340 goto err_remove_chip;
1345 acpi_gpiochip_remove(chip);
1346 gpiochip_free_hogs(chip);
1347 of_gpiochip_remove(chip);
1348 gpiochip_free_valid_mask(chip);
1349 err_remove_irqchip_mask:
1350 gpiochip_irqchip_free_valid_mask(chip);
1351 err_remove_from_list:
1352 spin_lock_irqsave(&gpio_lock, flags);
1353 list_del(&gdev->list);
1354 spin_unlock_irqrestore(&gpio_lock, flags);
1356 kfree_const(gdev->label);
1360 ida_simple_remove(&gpio_ida, gdev->id);
1361 /* failures here can mean systems won't boot... */
1362 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
1363 gdev->base, gdev->base + gdev->ngpio - 1,
1364 chip->label ? : "generic");
1368 EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
1371 * gpiochip_get_data() - get per-subdriver data for the chip
1375 * The per-subdriver data for the chip.
1377 void *gpiochip_get_data(struct gpio_chip *chip)
1379 return chip->gpiodev->data;
1381 EXPORT_SYMBOL_GPL(gpiochip_get_data);
1384 * gpiochip_remove() - unregister a gpio_chip
1385 * @chip: the chip to unregister
1387 * A gpio_chip with any GPIOs still requested may not be removed.
1389 void gpiochip_remove(struct gpio_chip *chip)
1391 struct gpio_device *gdev = chip->gpiodev;
1392 struct gpio_desc *desc;
1393 unsigned long flags;
1395 bool requested = false;
1397 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
1398 gpiochip_sysfs_unregister(gdev);
1399 gpiochip_free_hogs(chip);
1400 /* Numb the device, cancelling all outstanding operations */
1402 gpiochip_irqchip_remove(chip);
1403 acpi_gpiochip_remove(chip);
1404 gpiochip_remove_pin_ranges(chip);
1405 of_gpiochip_remove(chip);
1406 gpiochip_free_valid_mask(chip);
1408 * We accept no more calls into the driver from this point, so
1409 * NULL the driver data pointer
1413 spin_lock_irqsave(&gpio_lock, flags);
1414 for (i = 0; i < gdev->ngpio; i++) {
1415 desc = &gdev->descs[i];
1416 if (test_bit(FLAG_REQUESTED, &desc->flags))
1419 spin_unlock_irqrestore(&gpio_lock, flags);
1422 dev_crit(&gdev->dev,
1423 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
1426 * The gpiochip side puts its use of the device to rest here:
1427 * if there are no userspace clients, the chardev and device will
1428 * be removed, else it will be dangling until the last user is
1431 cdev_device_del(&gdev->chrdev, &gdev->dev);
1432 put_device(&gdev->dev);
1434 EXPORT_SYMBOL_GPL(gpiochip_remove);
1436 static void devm_gpio_chip_release(struct device *dev, void *res)
1438 struct gpio_chip *chip = *(struct gpio_chip **)res;
1440 gpiochip_remove(chip);
1443 static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1446 struct gpio_chip **r = res;
1457 * devm_gpiochip_add_data() - Resource manager gpiochip_add_data()
1458 * @dev: the device pointer on which irq_chip belongs to.
1459 * @chip: the chip to register, with chip->base initialized
1460 * @data: driver-private data associated with this chip
1462 * Context: potentially before irqs will work
1464 * The gpio chip automatically be released when the device is unbound.
1467 * A negative errno if the chip can't be registered, such as because the
1468 * chip->base is invalid or already associated with a different chip.
1469 * Otherwise it returns zero as a success code.
1471 int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1474 struct gpio_chip **ptr;
1477 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1482 ret = gpiochip_add_data(chip, data);
1489 devres_add(dev, ptr);
1493 EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1496 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1497 * @dev: device for which which resource was allocated
1498 * @chip: the chip to remove
1500 * A gpio_chip with any GPIOs still requested may not be removed.
1502 void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1506 ret = devres_release(dev, devm_gpio_chip_release,
1507 devm_gpio_chip_match, chip);
1510 EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1513 * gpiochip_find() - iterator for locating a specific gpio_chip
1514 * @data: data to pass to match function
1515 * @match: Callback function to check gpio_chip
1517 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1518 * determined by a user supplied @match callback. The callback should return
1519 * 0 if the device doesn't match and non-zero if it does. If the callback is
1520 * non-zero, this function will return to the caller and not iterate over any
1523 struct gpio_chip *gpiochip_find(void *data,
1524 int (*match)(struct gpio_chip *chip,
1527 struct gpio_device *gdev;
1528 struct gpio_chip *chip = NULL;
1529 unsigned long flags;
1531 spin_lock_irqsave(&gpio_lock, flags);
1532 list_for_each_entry(gdev, &gpio_devices, list)
1533 if (gdev->chip && match(gdev->chip, data)) {
1538 spin_unlock_irqrestore(&gpio_lock, flags);
1542 EXPORT_SYMBOL_GPL(gpiochip_find);
1544 static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1546 const char *name = data;
1548 return !strcmp(chip->label, name);
1551 static struct gpio_chip *find_chip_by_name(const char *name)
1553 return gpiochip_find((void *)name, gpiochip_match_name);
1556 #ifdef CONFIG_GPIOLIB_IRQCHIP
1559 * The following is irqchip helper code for gpiochips.
1562 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1564 if (!gpiochip->irq.need_valid_mask)
1567 gpiochip->irq.valid_mask = gpiochip_allocate_mask(gpiochip);
1568 if (!gpiochip->irq.valid_mask)
1574 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1576 kfree(gpiochip->irq.valid_mask);
1577 gpiochip->irq.valid_mask = NULL;
1580 bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1581 unsigned int offset)
1583 if (!gpiochip_line_is_valid(gpiochip, offset))
1585 /* No mask means all valid */
1586 if (likely(!gpiochip->irq.valid_mask))
1588 return test_bit(offset, gpiochip->irq.valid_mask);
1590 EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
1593 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
1594 * @gpiochip: the gpiochip to set the irqchip chain to
1595 * @irqchip: the irqchip to chain to the gpiochip
1596 * @parent_irq: the irq number corresponding to the parent IRQ for this
1598 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1599 * coming out of the gpiochip. If the interrupt is nested rather than
1600 * cascaded, pass NULL in this handler argument
1602 static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip,
1603 struct irq_chip *irqchip,
1604 unsigned int parent_irq,
1605 irq_flow_handler_t parent_handler)
1607 unsigned int offset;
1609 if (!gpiochip->irq.domain) {
1610 chip_err(gpiochip, "called %s before setting up irqchip\n",
1615 if (parent_handler) {
1616 if (gpiochip->can_sleep) {
1618 "you cannot have chained interrupts on a "
1619 "chip that may sleep\n");
1623 * The parent irqchip is already using the chip_data for this
1624 * irqchip, so our callbacks simply use the handler_data.
1626 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1629 gpiochip->irq.parents = &parent_irq;
1630 gpiochip->irq.num_parents = 1;
1633 /* Set the parent IRQ for all affected IRQs */
1634 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1635 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1637 irq_set_parent(irq_find_mapping(gpiochip->irq.domain, offset),
1643 * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
1644 * @gpiochip: the gpiochip to set the irqchip chain to
1645 * @irqchip: the irqchip to chain to the gpiochip
1646 * @parent_irq: the irq number corresponding to the parent IRQ for this
1648 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1649 * coming out of the gpiochip. If the interrupt is nested rather than
1650 * cascaded, pass NULL in this handler argument
1652 void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1653 struct irq_chip *irqchip,
1654 unsigned int parent_irq,
1655 irq_flow_handler_t parent_handler)
1657 if (gpiochip->irq.threaded) {
1658 chip_err(gpiochip, "tried to chain a threaded gpiochip\n");
1662 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1665 EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1668 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1669 * @gpiochip: the gpiochip to set the irqchip nested handler to
1670 * @irqchip: the irqchip to nest to the gpiochip
1671 * @parent_irq: the irq number corresponding to the parent IRQ for this
1674 void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1675 struct irq_chip *irqchip,
1676 unsigned int parent_irq)
1678 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1681 EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1684 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1685 * @d: the irqdomain used by this irqchip
1686 * @irq: the global irq number used by this GPIO irqchip irq
1687 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1689 * This function will set up the mapping for a certain IRQ line on a
1690 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1691 * stored inside the gpiochip.
1693 int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1694 irq_hw_number_t hwirq)
1696 struct gpio_chip *chip = d->host_data;
1699 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
1702 irq_set_chip_data(irq, chip);
1704 * This lock class tells lockdep that GPIO irqs are in a different
1705 * category than their parents, so it won't report false recursion.
1707 irq_set_lockdep_class(irq, chip->irq.lock_key, chip->irq.request_key);
1708 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
1709 /* Chips that use nested thread handlers have them marked */
1710 if (chip->irq.threaded)
1711 irq_set_nested_thread(irq, 1);
1712 irq_set_noprobe(irq);
1714 if (chip->irq.num_parents == 1)
1715 err = irq_set_parent(irq, chip->irq.parents[0]);
1716 else if (chip->irq.map)
1717 err = irq_set_parent(irq, chip->irq.map[hwirq]);
1723 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1724 * is passed as default type.
1726 if (chip->irq.default_type != IRQ_TYPE_NONE)
1727 irq_set_irq_type(irq, chip->irq.default_type);
1731 EXPORT_SYMBOL_GPL(gpiochip_irq_map);
1733 void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1735 struct gpio_chip *chip = d->host_data;
1737 if (chip->irq.threaded)
1738 irq_set_nested_thread(irq, 0);
1739 irq_set_chip_and_handler(irq, NULL, NULL);
1740 irq_set_chip_data(irq, NULL);
1742 EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
1744 static const struct irq_domain_ops gpiochip_domain_ops = {
1745 .map = gpiochip_irq_map,
1746 .unmap = gpiochip_irq_unmap,
1747 /* Virtually all GPIO irqchips are twocell:ed */
1748 .xlate = irq_domain_xlate_twocell,
1751 static int gpiochip_irq_reqres(struct irq_data *d)
1753 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1755 if (!try_module_get(chip->gpiodev->owner))
1758 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
1760 "unable to lock HW IRQ %lu for IRQ\n",
1762 module_put(chip->gpiodev->owner);
1768 static void gpiochip_irq_relres(struct irq_data *d)
1770 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1772 gpiochip_unlock_as_irq(chip, d->hwirq);
1773 module_put(chip->gpiodev->owner);
1776 static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1778 if (!gpiochip_irqchip_irq_valid(chip, offset))
1781 return irq_create_mapping(chip->irq.domain, offset);
1785 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1786 * @gpiochip: the GPIO chip to add the IRQ chip to
1787 * @lock_key: lockdep class for IRQ lock
1788 * @request_key: lockdep class for IRQ request
1790 static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
1791 struct lock_class_key *lock_key,
1792 struct lock_class_key *request_key)
1794 struct irq_chip *irqchip = gpiochip->irq.chip;
1795 const struct irq_domain_ops *ops;
1796 struct device_node *np;
1803 if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
1804 chip_err(gpiochip, "you cannot have chained interrupts on a "
1805 "chip that may sleep\n");
1809 np = gpiochip->gpiodev->dev.of_node;
1810 type = gpiochip->irq.default_type;
1813 * Specifying a default trigger is a terrible idea if DT or ACPI is
1814 * used to configure the interrupts, as you may end up with
1815 * conflicting triggers. Tell the user, and reset to NONE.
1817 if (WARN(np && type != IRQ_TYPE_NONE,
1818 "%s: Ignoring %u default trigger\n", np->full_name, type))
1819 type = IRQ_TYPE_NONE;
1821 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1822 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1823 "Ignoring %u default trigger\n", type);
1824 type = IRQ_TYPE_NONE;
1827 gpiochip->to_irq = gpiochip_to_irq;
1828 gpiochip->irq.default_type = type;
1829 gpiochip->irq.lock_key = lock_key;
1830 gpiochip->irq.request_key = request_key;
1832 if (gpiochip->irq.domain_ops)
1833 ops = gpiochip->irq.domain_ops;
1835 ops = &gpiochip_domain_ops;
1837 gpiochip->irq.domain = irq_domain_add_simple(np, gpiochip->ngpio,
1838 gpiochip->irq.first,
1840 if (!gpiochip->irq.domain)
1844 * It is possible for a driver to override this, but only if the
1845 * alternative functions are both implemented.
1847 if (!irqchip->irq_request_resources &&
1848 !irqchip->irq_release_resources) {
1849 irqchip->irq_request_resources = gpiochip_irq_reqres;
1850 irqchip->irq_release_resources = gpiochip_irq_relres;
1853 if (gpiochip->irq.parent_handler) {
1854 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
1856 for (i = 0; i < gpiochip->irq.num_parents; i++) {
1858 * The parent IRQ chip is already using the chip_data
1859 * for this IRQ chip, so our callbacks simply use the
1862 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
1863 gpiochip->irq.parent_handler,
1868 acpi_gpiochip_request_interrupts(gpiochip);
1874 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1875 * @gpiochip: the gpiochip to remove the irqchip from
1877 * This is called only from gpiochip_remove()
1879 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1881 unsigned int offset;
1883 acpi_gpiochip_free_interrupts(gpiochip);
1885 if (gpiochip->irq.chip && gpiochip->irq.parent_handler) {
1886 struct gpio_irq_chip *irq = &gpiochip->irq;
1889 for (i = 0; i < irq->num_parents; i++)
1890 irq_set_chained_handler_and_data(irq->parents[i],
1894 /* Remove all IRQ mappings and delete the domain */
1895 if (gpiochip->irq.domain) {
1898 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1899 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1902 irq = irq_find_mapping(gpiochip->irq.domain, offset);
1903 irq_dispose_mapping(irq);
1906 irq_domain_remove(gpiochip->irq.domain);
1909 if (gpiochip->irq.chip) {
1910 gpiochip->irq.chip->irq_request_resources = NULL;
1911 gpiochip->irq.chip->irq_release_resources = NULL;
1912 gpiochip->irq.chip = NULL;
1915 gpiochip_irqchip_free_valid_mask(gpiochip);
1919 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
1920 * @gpiochip: the gpiochip to add the irqchip to
1921 * @irqchip: the irqchip to add to the gpiochip
1922 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1923 * allocate gpiochip irqs from
1924 * @handler: the irq handler to use (often a predefined irq core function)
1925 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1926 * to have the core avoid setting up any default type in the hardware.
1927 * @threaded: whether this irqchip uses a nested thread handler
1928 * @lock_key: lockdep class for IRQ lock
1929 * @request_key: lockdep class for IRQ request
1931 * This function closely associates a certain irqchip with a certain
1932 * gpiochip, providing an irq domain to translate the local IRQs to
1933 * global irqs in the gpiolib core, and making sure that the gpiochip
1934 * is passed as chip data to all related functions. Driver callbacks
1935 * need to use gpiochip_get_data() to get their local state containers back
1936 * from the gpiochip passed as chip data. An irqdomain will be stored
1937 * in the gpiochip that shall be used by the driver to handle IRQ number
1938 * translation. The gpiochip will need to be initialized and registered
1939 * before calling this function.
1941 * This function will handle two cell:ed simple IRQs and assumes all
1942 * the pins on the gpiochip can generate a unique IRQ. Everything else
1943 * need to be open coded.
1945 int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
1946 struct irq_chip *irqchip,
1947 unsigned int first_irq,
1948 irq_flow_handler_t handler,
1951 struct lock_class_key *lock_key,
1952 struct lock_class_key *request_key)
1954 struct device_node *of_node;
1956 if (!gpiochip || !irqchip)
1959 if (!gpiochip->parent) {
1960 pr_err("missing gpiochip .dev parent pointer\n");
1963 gpiochip->irq.threaded = threaded;
1964 of_node = gpiochip->parent->of_node;
1965 #ifdef CONFIG_OF_GPIO
1967 * If the gpiochip has an assigned OF node this takes precedence
1968 * FIXME: get rid of this and use gpiochip->parent->of_node
1971 if (gpiochip->of_node)
1972 of_node = gpiochip->of_node;
1975 * Specifying a default trigger is a terrible idea if DT or ACPI is
1976 * used to configure the interrupts, as you may end-up with
1977 * conflicting triggers. Tell the user, and reset to NONE.
1979 if (WARN(of_node && type != IRQ_TYPE_NONE,
1980 "%pOF: Ignoring %d default trigger\n", of_node, type))
1981 type = IRQ_TYPE_NONE;
1982 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1983 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1984 "Ignoring %d default trigger\n", type);
1985 type = IRQ_TYPE_NONE;
1988 gpiochip->irq.chip = irqchip;
1989 gpiochip->irq.handler = handler;
1990 gpiochip->irq.default_type = type;
1991 gpiochip->to_irq = gpiochip_to_irq;
1992 gpiochip->irq.lock_key = lock_key;
1993 gpiochip->irq.request_key = request_key;
1994 gpiochip->irq.domain = irq_domain_add_simple(of_node,
1995 gpiochip->ngpio, first_irq,
1996 &gpiochip_domain_ops, gpiochip);
1997 if (!gpiochip->irq.domain) {
1998 gpiochip->irq.chip = NULL;
2003 * It is possible for a driver to override this, but only if the
2004 * alternative functions are both implemented.
2006 if (!irqchip->irq_request_resources &&
2007 !irqchip->irq_release_resources) {
2008 irqchip->irq_request_resources = gpiochip_irq_reqres;
2009 irqchip->irq_release_resources = gpiochip_irq_relres;
2012 acpi_gpiochip_request_interrupts(gpiochip);
2016 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
2018 #else /* CONFIG_GPIOLIB_IRQCHIP */
2020 static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
2021 struct lock_class_key *lock_key,
2022 struct lock_class_key *request_key)
2027 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
2028 static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
2032 static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
2035 #endif /* CONFIG_GPIOLIB_IRQCHIP */
2038 * gpiochip_generic_request() - request the gpio function for a pin
2039 * @chip: the gpiochip owning the GPIO
2040 * @offset: the offset of the GPIO to request for GPIO function
2042 int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
2044 return pinctrl_gpio_request(chip->gpiodev->base + offset);
2046 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
2049 * gpiochip_generic_free() - free the gpio function from a pin
2050 * @chip: the gpiochip to request the gpio function for
2051 * @offset: the offset of the GPIO to free from GPIO function
2053 void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
2055 pinctrl_gpio_free(chip->gpiodev->base + offset);
2057 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
2060 * gpiochip_generic_config() - apply configuration for a pin
2061 * @chip: the gpiochip owning the GPIO
2062 * @offset: the offset of the GPIO to apply the configuration
2063 * @config: the configuration to be applied
2065 int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
2066 unsigned long config)
2068 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
2070 EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2072 #ifdef CONFIG_PINCTRL
2075 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2076 * @chip: the gpiochip to add the range for
2077 * @pctldev: the pin controller to map to
2078 * @gpio_offset: the start offset in the current gpio_chip number space
2079 * @pin_group: name of the pin group inside the pin controller
2081 int gpiochip_add_pingroup_range(struct gpio_chip *chip,
2082 struct pinctrl_dev *pctldev,
2083 unsigned int gpio_offset, const char *pin_group)
2085 struct gpio_pin_range *pin_range;
2086 struct gpio_device *gdev = chip->gpiodev;
2089 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2091 chip_err(chip, "failed to allocate pin ranges\n");
2095 /* Use local offset as range ID */
2096 pin_range->range.id = gpio_offset;
2097 pin_range->range.gc = chip;
2098 pin_range->range.name = chip->label;
2099 pin_range->range.base = gdev->base + gpio_offset;
2100 pin_range->pctldev = pctldev;
2102 ret = pinctrl_get_group_pins(pctldev, pin_group,
2103 &pin_range->range.pins,
2104 &pin_range->range.npins);
2110 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2112 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2113 gpio_offset, gpio_offset + pin_range->range.npins - 1,
2114 pinctrl_dev_get_devname(pctldev), pin_group);
2116 list_add_tail(&pin_range->node, &gdev->pin_ranges);
2120 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2123 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2124 * @chip: the gpiochip to add the range for
2125 * @pinctl_name: the dev_name() of the pin controller to map to
2126 * @gpio_offset: the start offset in the current gpio_chip number space
2127 * @pin_offset: the start offset in the pin controller number space
2128 * @npins: the number of pins from the offset of each pin space (GPIO and
2129 * pin controller) to accumulate in this range
2132 * 0 on success, or a negative error-code on failure.
2134 int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
2135 unsigned int gpio_offset, unsigned int pin_offset,
2138 struct gpio_pin_range *pin_range;
2139 struct gpio_device *gdev = chip->gpiodev;
2142 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2144 chip_err(chip, "failed to allocate pin ranges\n");
2148 /* Use local offset as range ID */
2149 pin_range->range.id = gpio_offset;
2150 pin_range->range.gc = chip;
2151 pin_range->range.name = chip->label;
2152 pin_range->range.base = gdev->base + gpio_offset;
2153 pin_range->range.pin_base = pin_offset;
2154 pin_range->range.npins = npins;
2155 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
2157 if (IS_ERR(pin_range->pctldev)) {
2158 ret = PTR_ERR(pin_range->pctldev);
2159 chip_err(chip, "could not create pin range\n");
2163 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2164 gpio_offset, gpio_offset + npins - 1,
2166 pin_offset, pin_offset + npins - 1);
2168 list_add_tail(&pin_range->node, &gdev->pin_ranges);
2172 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
2175 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2176 * @chip: the chip to remove all the mappings for
2178 void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
2180 struct gpio_pin_range *pin_range, *tmp;
2181 struct gpio_device *gdev = chip->gpiodev;
2183 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
2184 list_del(&pin_range->node);
2185 pinctrl_remove_gpio_range(pin_range->pctldev,
2190 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2192 #endif /* CONFIG_PINCTRL */
2194 /* These "optional" allocation calls help prevent drivers from stomping
2195 * on each other, and help provide better diagnostics in debugfs.
2196 * They're called even less than the "set direction" calls.
2198 static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
2200 struct gpio_chip *chip = desc->gdev->chip;
2202 unsigned long flags;
2204 spin_lock_irqsave(&gpio_lock, flags);
2206 /* NOTE: gpio_request() can be called in early boot,
2207 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
2210 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2211 desc_set_label(desc, label ? : "?");
2218 if (chip->request) {
2219 /* chip->request may sleep */
2220 spin_unlock_irqrestore(&gpio_lock, flags);
2221 status = chip->request(chip, gpio_chip_hwgpio(desc));
2222 spin_lock_irqsave(&gpio_lock, flags);
2225 desc_set_label(desc, NULL);
2226 clear_bit(FLAG_REQUESTED, &desc->flags);
2230 if (chip->get_direction) {
2231 /* chip->get_direction may sleep */
2232 spin_unlock_irqrestore(&gpio_lock, flags);
2233 gpiod_get_direction(desc);
2234 spin_lock_irqsave(&gpio_lock, flags);
2237 spin_unlock_irqrestore(&gpio_lock, flags);
2242 * This descriptor validation needs to be inserted verbatim into each
2243 * function taking a descriptor, so we need to use a preprocessor
2244 * macro to avoid endless duplication. If the desc is NULL it is an
2245 * optional GPIO and calls should just bail out.
2247 static int validate_desc(const struct gpio_desc *desc, const char *func)
2252 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2253 return PTR_ERR(desc);
2256 pr_warn("%s: invalid GPIO (no device)\n", func);
2259 if (!desc->gdev->chip) {
2260 dev_warn(&desc->gdev->dev,
2261 "%s: backing chip is gone\n", func);
2267 #define VALIDATE_DESC(desc) do { \
2268 int __valid = validate_desc(desc, __func__); \
2273 #define VALIDATE_DESC_VOID(desc) do { \
2274 int __valid = validate_desc(desc, __func__); \
2279 int gpiod_request(struct gpio_desc *desc, const char *label)
2281 int status = -EPROBE_DEFER;
2282 struct gpio_device *gdev;
2284 VALIDATE_DESC(desc);
2287 if (try_module_get(gdev->owner)) {
2288 status = gpiod_request_commit(desc, label);
2290 module_put(gdev->owner);
2292 get_device(&gdev->dev);
2296 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
2301 static bool gpiod_free_commit(struct gpio_desc *desc)
2304 unsigned long flags;
2305 struct gpio_chip *chip;
2309 gpiod_unexport(desc);
2311 spin_lock_irqsave(&gpio_lock, flags);
2313 chip = desc->gdev->chip;
2314 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2316 spin_unlock_irqrestore(&gpio_lock, flags);
2317 might_sleep_if(chip->can_sleep);
2318 chip->free(chip, gpio_chip_hwgpio(desc));
2319 spin_lock_irqsave(&gpio_lock, flags);
2321 desc_set_label(desc, NULL);
2322 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
2323 clear_bit(FLAG_REQUESTED, &desc->flags);
2324 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
2325 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
2326 clear_bit(FLAG_IS_HOGGED, &desc->flags);
2330 spin_unlock_irqrestore(&gpio_lock, flags);
2334 void gpiod_free(struct gpio_desc *desc)
2336 if (desc && desc->gdev && gpiod_free_commit(desc)) {
2337 module_put(desc->gdev->owner);
2338 put_device(&desc->gdev->dev);
2340 WARN_ON(extra_checks);
2345 * gpiochip_is_requested - return string iff signal was requested
2346 * @chip: controller managing the signal
2347 * @offset: of signal within controller's 0..(ngpio - 1) range
2349 * Returns NULL if the GPIO is not currently requested, else a string.
2350 * The string returned is the label passed to gpio_request(); if none has been
2351 * passed it is a meaningless, non-NULL constant.
2353 * This function is for use by GPIO controller drivers. The label can
2354 * help with diagnostics, and knowing that the signal is used as a GPIO
2355 * can help avoid accidentally multiplexing it to another controller.
2357 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2359 struct gpio_desc *desc;
2361 if (offset >= chip->ngpio)
2364 desc = &chip->gpiodev->descs[offset];
2366 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
2370 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2373 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2375 * @hwnum: hardware number of the GPIO for which to request the descriptor
2376 * @label: label for the GPIO
2378 * Function allows GPIO chip drivers to request and use their own GPIO
2379 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2380 * function will not increase reference count of the GPIO chip module. This
2381 * allows the GPIO chip module to be unloaded as needed (we assume that the
2382 * GPIO chip driver handles freeing the GPIOs it has requested).
2385 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2388 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2391 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2395 chip_err(chip, "failed to get GPIO descriptor\n");
2399 err = gpiod_request_commit(desc, label);
2401 return ERR_PTR(err);
2405 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
2408 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2409 * @desc: GPIO descriptor to free
2411 * Function frees the given GPIO requested previously with
2412 * gpiochip_request_own_desc().
2414 void gpiochip_free_own_desc(struct gpio_desc *desc)
2417 gpiod_free_commit(desc);
2419 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
2422 * Drivers MUST set GPIO direction before making get/set calls. In
2423 * some cases this is done in early boot, before IRQs are enabled.
2425 * As a rule these aren't called more than once (except for drivers
2426 * using the open-drain emulation idiom) so these are natural places
2427 * to accumulate extra debugging checks. Note that we can't (yet)
2428 * rely on gpio_request() having been called beforehand.
2432 * gpiod_direction_input - set the GPIO direction to input
2433 * @desc: GPIO to set to input
2435 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2436 * be called safely on it.
2438 * Return 0 in case of success, else an error code.
2440 int gpiod_direction_input(struct gpio_desc *desc)
2442 struct gpio_chip *chip;
2443 int status = -EINVAL;
2445 VALIDATE_DESC(desc);
2446 chip = desc->gdev->chip;
2448 if (!chip->get || !chip->direction_input) {
2450 "%s: missing get() or direction_input() operations\n",
2455 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
2457 clear_bit(FLAG_IS_OUT, &desc->flags);
2459 trace_gpio_direction(desc_to_gpio(desc), 1, status);
2463 EXPORT_SYMBOL_GPL(gpiod_direction_input);
2465 static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset,
2466 enum pin_config_param mode)
2468 unsigned long config = { PIN_CONF_PACKED(mode, 0) };
2470 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2473 static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
2475 struct gpio_chip *gc = desc->gdev->chip;
2479 if (!gc->set || !gc->direction_output) {
2481 "%s: missing set() or direction_output() operations\n",
2486 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
2488 set_bit(FLAG_IS_OUT, &desc->flags);
2489 trace_gpio_value(desc_to_gpio(desc), 0, val);
2490 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2495 * gpiod_direction_output_raw - set the GPIO direction to output
2496 * @desc: GPIO to set to output
2497 * @value: initial output value of the GPIO
2499 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2500 * be called safely on it. The initial value of the output must be specified
2501 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2503 * Return 0 in case of success, else an error code.
2505 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2507 VALIDATE_DESC(desc);
2508 return gpiod_direction_output_raw_commit(desc, value);
2510 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2513 * gpiod_direction_output - set the GPIO direction to output
2514 * @desc: GPIO to set to output
2515 * @value: initial output value of the GPIO
2517 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2518 * be called safely on it. The initial value of the output must be specified
2519 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2522 * Return 0 in case of success, else an error code.
2524 int gpiod_direction_output(struct gpio_desc *desc, int value)
2526 struct gpio_chip *gc;
2529 VALIDATE_DESC(desc);
2530 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2535 /* GPIOs used for IRQs shall not be set as output */
2536 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2538 "%s: tried to set a GPIO tied to an IRQ as output\n",
2543 gc = desc->gdev->chip;
2544 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2545 /* First see if we can enable open drain in hardware */
2546 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2547 PIN_CONFIG_DRIVE_OPEN_DRAIN);
2549 goto set_output_value;
2550 /* Emulate open drain by not actively driving the line high */
2552 return gpiod_direction_input(desc);
2554 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2555 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2556 PIN_CONFIG_DRIVE_OPEN_SOURCE);
2558 goto set_output_value;
2559 /* Emulate open source by not actively driving the line low */
2561 return gpiod_direction_input(desc);
2563 gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2564 PIN_CONFIG_DRIVE_PUSH_PULL);
2568 return gpiod_direction_output_raw_commit(desc, value);
2570 EXPORT_SYMBOL_GPL(gpiod_direction_output);
2573 * gpiod_set_debounce - sets @debounce time for a GPIO
2574 * @desc: descriptor of the GPIO for which to set debounce time
2575 * @debounce: debounce time in microseconds
2578 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2581 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
2583 struct gpio_chip *chip;
2584 unsigned long config;
2586 VALIDATE_DESC(desc);
2587 chip = desc->gdev->chip;
2588 if (!chip->set || !chip->set_config) {
2590 "%s: missing set() or set_config() operations\n",
2595 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2596 return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
2598 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
2601 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2602 * @desc: descriptor of the GPIO for which to configure persistence
2603 * @transitory: True to lose state on suspend or reset, false for persistence
2606 * 0 on success, otherwise a negative error code.
2608 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2610 struct gpio_chip *chip;
2611 unsigned long packed;
2615 VALIDATE_DESC(desc);
2617 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2618 * persistence state.
2621 set_bit(FLAG_TRANSITORY, &desc->flags);
2623 clear_bit(FLAG_TRANSITORY, &desc->flags);
2625 /* If the driver supports it, set the persistence state now */
2626 chip = desc->gdev->chip;
2627 if (!chip->set_config)
2630 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
2632 gpio = gpio_chip_hwgpio(desc);
2633 rc = chip->set_config(chip, gpio, packed);
2634 if (rc == -ENOTSUPP) {
2635 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
2642 EXPORT_SYMBOL_GPL(gpiod_set_transitory);
2645 * gpiod_is_active_low - test whether a GPIO is active-low or not
2646 * @desc: the gpio descriptor to test
2648 * Returns 1 if the GPIO is active-low, 0 otherwise.
2650 int gpiod_is_active_low(const struct gpio_desc *desc)
2652 VALIDATE_DESC(desc);
2653 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
2655 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
2657 /* I/O calls are only valid after configuration completed; the relevant
2658 * "is this a valid GPIO" error checks should already have been done.
2660 * "Get" operations are often inlinable as reading a pin value register,
2661 * and masking the relevant bit in that register.
2663 * When "set" operations are inlinable, they involve writing that mask to
2664 * one register to set a low value, or a different register to set it high.
2665 * Otherwise locking is needed, so there may be little value to inlining.
2667 *------------------------------------------------------------------------
2669 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2670 * have requested the GPIO. That can include implicit requesting by
2671 * a direction setting call. Marking a gpio as requested locks its chip
2672 * in memory, guaranteeing that these table lookups need no more locking
2673 * and that gpiochip_remove() will fail.
2675 * REVISIT when debugging, consider adding some instrumentation to ensure
2676 * that the GPIO was actually requested.
2679 static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
2681 struct gpio_chip *chip;
2685 chip = desc->gdev->chip;
2686 offset = gpio_chip_hwgpio(desc);
2687 value = chip->get ? chip->get(chip, offset) : -EIO;
2688 value = value < 0 ? value : !!value;
2689 trace_gpio_value(desc_to_gpio(desc), 1, value);
2693 static int gpio_chip_get_multiple(struct gpio_chip *chip,
2694 unsigned long *mask, unsigned long *bits)
2696 if (chip->get_multiple) {
2697 return chip->get_multiple(chip, mask, bits);
2698 } else if (chip->get) {
2701 for_each_set_bit(i, mask, chip->ngpio) {
2702 value = chip->get(chip, i);
2705 __assign_bit(i, bits, value);
2712 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2713 unsigned int array_size,
2714 struct gpio_desc **desc_array,
2719 while (i < array_size) {
2720 struct gpio_chip *chip = desc_array[i]->gdev->chip;
2721 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2722 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2726 WARN_ON(chip->can_sleep);
2728 /* collect all inputs belonging to the same chip */
2730 memset(mask, 0, sizeof(mask));
2732 const struct gpio_desc *desc = desc_array[i];
2733 int hwgpio = gpio_chip_hwgpio(desc);
2735 __set_bit(hwgpio, mask);
2737 } while ((i < array_size) &&
2738 (desc_array[i]->gdev->chip == chip));
2740 ret = gpio_chip_get_multiple(chip, mask, bits);
2744 for (j = first; j < i; j++) {
2745 const struct gpio_desc *desc = desc_array[j];
2746 int hwgpio = gpio_chip_hwgpio(desc);
2747 int value = test_bit(hwgpio, bits);
2749 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2751 value_array[j] = value;
2752 trace_gpio_value(desc_to_gpio(desc), 1, value);
2759 * gpiod_get_raw_value() - return a gpio's raw value
2760 * @desc: gpio whose value will be returned
2762 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2763 * its ACTIVE_LOW status, or negative errno on failure.
2765 * This function should be called from contexts where we cannot sleep, and will
2766 * complain if the GPIO chip functions potentially sleep.
2768 int gpiod_get_raw_value(const struct gpio_desc *desc)
2770 VALIDATE_DESC(desc);
2771 /* Should be using gpio_get_value_cansleep() */
2772 WARN_ON(desc->gdev->chip->can_sleep);
2773 return gpiod_get_raw_value_commit(desc);
2775 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
2778 * gpiod_get_value() - return a gpio's value
2779 * @desc: gpio whose value will be returned
2781 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2782 * account, or negative errno on failure.
2784 * This function should be called from contexts where we cannot sleep, and will
2785 * complain if the GPIO chip functions potentially sleep.
2787 int gpiod_get_value(const struct gpio_desc *desc)
2791 VALIDATE_DESC(desc);
2792 /* Should be using gpio_get_value_cansleep() */
2793 WARN_ON(desc->gdev->chip->can_sleep);
2795 value = gpiod_get_raw_value_commit(desc);
2799 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2804 EXPORT_SYMBOL_GPL(gpiod_get_value);
2807 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2808 * @array_size: number of elements in the descriptor / value arrays
2809 * @desc_array: array of GPIO descriptors whose values will be read
2810 * @value_array: array to store the read values
2812 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2813 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2814 * else an error code.
2816 * This function should be called from contexts where we cannot sleep,
2817 * and it will complain if the GPIO chip functions potentially sleep.
2819 int gpiod_get_raw_array_value(unsigned int array_size,
2820 struct gpio_desc **desc_array, int *value_array)
2824 return gpiod_get_array_value_complex(true, false, array_size,
2825 desc_array, value_array);
2827 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2830 * gpiod_get_array_value() - read values from an array of GPIOs
2831 * @array_size: number of elements in the descriptor / value arrays
2832 * @desc_array: array of GPIO descriptors whose values will be read
2833 * @value_array: array to store the read values
2835 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2836 * into account. Return 0 in case of success, else an error code.
2838 * This function should be called from contexts where we cannot sleep,
2839 * and it will complain if the GPIO chip functions potentially sleep.
2841 int gpiod_get_array_value(unsigned int array_size,
2842 struct gpio_desc **desc_array, int *value_array)
2846 return gpiod_get_array_value_complex(false, false, array_size,
2847 desc_array, value_array);
2849 EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2852 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
2853 * @desc: gpio descriptor whose state need to be set.
2854 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2856 static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
2859 struct gpio_chip *chip = desc->gdev->chip;
2860 int offset = gpio_chip_hwgpio(desc);
2863 err = chip->direction_input(chip, offset);
2865 clear_bit(FLAG_IS_OUT, &desc->flags);
2867 err = chip->direction_output(chip, offset, 0);
2869 set_bit(FLAG_IS_OUT, &desc->flags);
2871 trace_gpio_direction(desc_to_gpio(desc), value, err);
2874 "%s: Error in set_value for open drain err %d\n",
2879 * _gpio_set_open_source_value() - Set the open source gpio's value.
2880 * @desc: gpio descriptor whose state need to be set.
2881 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2883 static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
2886 struct gpio_chip *chip = desc->gdev->chip;
2887 int offset = gpio_chip_hwgpio(desc);
2890 err = chip->direction_output(chip, offset, 1);
2892 set_bit(FLAG_IS_OUT, &desc->flags);
2894 err = chip->direction_input(chip, offset);
2896 clear_bit(FLAG_IS_OUT, &desc->flags);
2898 trace_gpio_direction(desc_to_gpio(desc), !value, err);
2901 "%s: Error in set_value for open source err %d\n",
2905 static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
2907 struct gpio_chip *chip;
2909 chip = desc->gdev->chip;
2910 trace_gpio_value(desc_to_gpio(desc), 0, value);
2911 chip->set(chip, gpio_chip_hwgpio(desc), value);
2915 * set multiple outputs on the same chip;
2916 * use the chip's set_multiple function if available;
2917 * otherwise set the outputs sequentially;
2918 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2919 * defines which outputs are to be changed
2920 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2921 * defines the values the outputs specified by mask are to be set to
2923 static void gpio_chip_set_multiple(struct gpio_chip *chip,
2924 unsigned long *mask, unsigned long *bits)
2926 if (chip->set_multiple) {
2927 chip->set_multiple(chip, mask, bits);
2931 /* set outputs if the corresponding mask bit is set */
2932 for_each_set_bit(i, mask, chip->ngpio)
2933 chip->set(chip, i, test_bit(i, bits));
2937 void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2938 unsigned int array_size,
2939 struct gpio_desc **desc_array,
2944 while (i < array_size) {
2945 struct gpio_chip *chip = desc_array[i]->gdev->chip;
2946 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2947 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2951 WARN_ON(chip->can_sleep);
2953 memset(mask, 0, sizeof(mask));
2955 struct gpio_desc *desc = desc_array[i];
2956 int hwgpio = gpio_chip_hwgpio(desc);
2957 int value = value_array[i];
2959 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2961 trace_gpio_value(desc_to_gpio(desc), 0, value);
2963 * collect all normal outputs belonging to the same chip
2964 * open drain and open source outputs are set individually
2966 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
2967 gpio_set_open_drain_value_commit(desc, value);
2968 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
2969 gpio_set_open_source_value_commit(desc, value);
2971 __set_bit(hwgpio, mask);
2973 __set_bit(hwgpio, bits);
2975 __clear_bit(hwgpio, bits);
2979 } while ((i < array_size) &&
2980 (desc_array[i]->gdev->chip == chip));
2981 /* push collected bits to outputs */
2983 gpio_chip_set_multiple(chip, mask, bits);
2988 * gpiod_set_raw_value() - assign a gpio's raw value
2989 * @desc: gpio whose value will be assigned
2990 * @value: value to assign
2992 * Set the raw value of the GPIO, i.e. the value of its physical line without
2993 * regard for its ACTIVE_LOW status.
2995 * This function should be called from contexts where we cannot sleep, and will
2996 * complain if the GPIO chip functions potentially sleep.
2998 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
3000 VALIDATE_DESC_VOID(desc);
3001 /* Should be using gpiod_set_value_cansleep() */
3002 WARN_ON(desc->gdev->chip->can_sleep);
3003 gpiod_set_raw_value_commit(desc, value);
3005 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
3008 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3009 * @desc: the descriptor to set the value on
3010 * @value: value to set
3012 * This sets the value of a GPIO line backing a descriptor, applying
3013 * different semantic quirks like active low and open drain/source
3016 static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3018 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3020 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3021 gpio_set_open_drain_value_commit(desc, value);
3022 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3023 gpio_set_open_source_value_commit(desc, value);
3025 gpiod_set_raw_value_commit(desc, value);
3029 * gpiod_set_value() - assign a gpio's value
3030 * @desc: gpio whose value will be assigned
3031 * @value: value to assign
3033 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3034 * OPEN_DRAIN and OPEN_SOURCE flags into account.
3036 * This function should be called from contexts where we cannot sleep, and will
3037 * complain if the GPIO chip functions potentially sleep.
3039 void gpiod_set_value(struct gpio_desc *desc, int value)
3041 VALIDATE_DESC_VOID(desc);
3042 WARN_ON(desc->gdev->chip->can_sleep);
3043 gpiod_set_value_nocheck(desc, value);
3045 EXPORT_SYMBOL_GPL(gpiod_set_value);
3048 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
3049 * @array_size: number of elements in the descriptor / value arrays
3050 * @desc_array: array of GPIO descriptors whose values will be assigned
3051 * @value_array: array of values to assign
3053 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3054 * without regard for their ACTIVE_LOW status.
3056 * This function should be called from contexts where we cannot sleep, and will
3057 * complain if the GPIO chip functions potentially sleep.
3059 void gpiod_set_raw_array_value(unsigned int array_size,
3060 struct gpio_desc **desc_array, int *value_array)
3064 gpiod_set_array_value_complex(true, false, array_size, desc_array,
3067 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
3070 * gpiod_set_array_value() - assign values to an array of GPIOs
3071 * @array_size: number of elements in the descriptor / value arrays
3072 * @desc_array: array of GPIO descriptors whose values will be assigned
3073 * @value_array: array of values to assign
3075 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3078 * This function should be called from contexts where we cannot sleep, and will
3079 * complain if the GPIO chip functions potentially sleep.
3081 void gpiod_set_array_value(unsigned int array_size,
3082 struct gpio_desc **desc_array, int *value_array)
3086 gpiod_set_array_value_complex(false, false, array_size, desc_array,
3089 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
3092 * gpiod_cansleep() - report whether gpio value access may sleep
3093 * @desc: gpio to check
3096 int gpiod_cansleep(const struct gpio_desc *desc)
3098 VALIDATE_DESC(desc);
3099 return desc->gdev->chip->can_sleep;
3101 EXPORT_SYMBOL_GPL(gpiod_cansleep);
3104 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3105 * @desc: gpio whose IRQ will be returned (already requested)
3107 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3110 int gpiod_to_irq(const struct gpio_desc *desc)
3112 struct gpio_chip *chip;
3116 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3117 * requires this function to not return zero on an invalid descriptor
3118 * but rather a negative error number.
3120 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
3123 chip = desc->gdev->chip;
3124 offset = gpio_chip_hwgpio(desc);
3126 int retirq = chip->to_irq(chip, offset);
3128 /* Zero means NO_IRQ */
3136 EXPORT_SYMBOL_GPL(gpiod_to_irq);
3139 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3140 * @chip: the chip the GPIO to lock belongs to
3141 * @offset: the offset of the GPIO to lock as IRQ
3143 * This is used directly by GPIO drivers that want to lock down
3144 * a certain GPIO line to be used for IRQs.
3146 int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
3148 struct gpio_desc *desc;
3150 desc = gpiochip_get_desc(chip, offset);
3152 return PTR_ERR(desc);
3155 * If it's fast: flush the direction setting if something changed
3158 if (!chip->can_sleep && chip->get_direction) {
3159 int dir = chip->get_direction(chip, offset);
3162 clear_bit(FLAG_IS_OUT, &desc->flags);
3164 set_bit(FLAG_IS_OUT, &desc->flags);
3167 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
3169 "%s: tried to flag a GPIO set as output for IRQ\n",
3174 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
3177 * If the consumer has not set up a label (such as when the
3178 * IRQ is referenced from .to_irq()) we set up a label here
3179 * so it is clear this is used as an interrupt.
3182 desc_set_label(desc, "interrupt");
3186 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
3189 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3190 * @chip: the chip the GPIO to lock belongs to
3191 * @offset: the offset of the GPIO to lock as IRQ
3193 * This is used directly by GPIO drivers that want to indicate
3194 * that a certain GPIO is no longer used exclusively for IRQ.
3196 void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
3198 struct gpio_desc *desc;
3200 desc = gpiochip_get_desc(chip, offset);
3204 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3206 /* If we only had this marking, erase it */
3207 if (desc->label && !strcmp(desc->label, "interrupt"))
3208 desc_set_label(desc, NULL);
3210 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
3212 bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
3214 if (offset >= chip->ngpio)
3217 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
3219 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3221 bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
3223 if (offset >= chip->ngpio)
3226 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
3228 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3230 bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
3232 if (offset >= chip->ngpio)
3235 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
3237 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3239 bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
3241 if (offset >= chip->ngpio)
3244 return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
3246 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3249 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3250 * @desc: gpio whose value will be returned
3252 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
3253 * its ACTIVE_LOW status, or negative errno on failure.
3255 * This function is to be called from contexts that can sleep.
3257 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
3259 might_sleep_if(extra_checks);
3260 VALIDATE_DESC(desc);
3261 return gpiod_get_raw_value_commit(desc);
3263 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
3266 * gpiod_get_value_cansleep() - return a gpio's value
3267 * @desc: gpio whose value will be returned
3269 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3270 * account, or negative errno on failure.
3272 * This function is to be called from contexts that can sleep.
3274 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
3278 might_sleep_if(extra_checks);
3279 VALIDATE_DESC(desc);
3280 value = gpiod_get_raw_value_commit(desc);
3284 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3289 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
3292 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3293 * @array_size: number of elements in the descriptor / value arrays
3294 * @desc_array: array of GPIO descriptors whose values will be read
3295 * @value_array: array to store the read values
3297 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3298 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3299 * else an error code.
3301 * This function is to be called from contexts that can sleep.
3303 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3304 struct gpio_desc **desc_array,
3307 might_sleep_if(extra_checks);
3310 return gpiod_get_array_value_complex(true, true, array_size,
3311 desc_array, value_array);
3313 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3316 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3317 * @array_size: number of elements in the descriptor / value arrays
3318 * @desc_array: array of GPIO descriptors whose values will be read
3319 * @value_array: array to store the read values
3321 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3322 * into account. Return 0 in case of success, else an error code.
3324 * This function is to be called from contexts that can sleep.
3326 int gpiod_get_array_value_cansleep(unsigned int array_size,
3327 struct gpio_desc **desc_array,
3330 might_sleep_if(extra_checks);
3333 return gpiod_get_array_value_complex(false, true, array_size,
3334 desc_array, value_array);
3336 EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3339 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3340 * @desc: gpio whose value will be assigned
3341 * @value: value to assign
3343 * Set the raw value of the GPIO, i.e. the value of its physical line without
3344 * regard for its ACTIVE_LOW status.
3346 * This function is to be called from contexts that can sleep.
3348 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
3350 might_sleep_if(extra_checks);
3351 VALIDATE_DESC_VOID(desc);
3352 gpiod_set_raw_value_commit(desc, value);
3354 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
3357 * gpiod_set_value_cansleep() - assign a gpio's value
3358 * @desc: gpio whose value will be assigned
3359 * @value: value to assign
3361 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3364 * This function is to be called from contexts that can sleep.
3366 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
3368 might_sleep_if(extra_checks);
3369 VALIDATE_DESC_VOID(desc);
3370 gpiod_set_value_nocheck(desc, value);
3372 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
3375 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
3376 * @array_size: number of elements in the descriptor / value arrays
3377 * @desc_array: array of GPIO descriptors whose values will be assigned
3378 * @value_array: array of values to assign
3380 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3381 * without regard for their ACTIVE_LOW status.
3383 * This function is to be called from contexts that can sleep.
3385 void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3386 struct gpio_desc **desc_array,
3389 might_sleep_if(extra_checks);
3392 gpiod_set_array_value_complex(true, true, array_size, desc_array,
3395 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
3398 * gpiod_add_lookup_tables() - register GPIO device consumers
3399 * @tables: list of tables of consumers to register
3400 * @n: number of tables in the list
3402 void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3406 mutex_lock(&gpio_lookup_lock);
3408 for (i = 0; i < n; i++)
3409 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3411 mutex_unlock(&gpio_lookup_lock);
3415 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
3416 * @array_size: number of elements in the descriptor / value arrays
3417 * @desc_array: array of GPIO descriptors whose values will be assigned
3418 * @value_array: array of values to assign
3420 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3423 * This function is to be called from contexts that can sleep.
3425 void gpiod_set_array_value_cansleep(unsigned int array_size,
3426 struct gpio_desc **desc_array,
3429 might_sleep_if(extra_checks);
3432 gpiod_set_array_value_complex(false, true, array_size, desc_array,
3435 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
3438 * gpiod_add_lookup_table() - register GPIO device consumers
3439 * @table: table of consumers to register
3441 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
3443 mutex_lock(&gpio_lookup_lock);
3445 list_add_tail(&table->list, &gpio_lookup_list);
3447 mutex_unlock(&gpio_lookup_lock);
3449 EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
3452 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3453 * @table: table of consumers to unregister
3455 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3457 mutex_lock(&gpio_lookup_lock);
3459 list_del(&table->list);
3461 mutex_unlock(&gpio_lookup_lock);
3463 EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
3465 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3467 const char *dev_id = dev ? dev_name(dev) : NULL;
3468 struct gpiod_lookup_table *table;
3470 mutex_lock(&gpio_lookup_lock);
3472 list_for_each_entry(table, &gpio_lookup_list, list) {
3473 if (table->dev_id && dev_id) {
3475 * Valid strings on both ends, must be identical to have
3478 if (!strcmp(table->dev_id, dev_id))
3482 * One of the pointers is NULL, so both must be to have
3485 if (dev_id == table->dev_id)
3492 mutex_unlock(&gpio_lookup_lock);
3496 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
3498 enum gpio_lookup_flags *flags)
3500 struct gpio_desc *desc = ERR_PTR(-ENOENT);
3501 struct gpiod_lookup_table *table;
3502 struct gpiod_lookup *p;
3504 table = gpiod_find_lookup_table(dev);
3508 for (p = &table->table[0]; p->chip_label; p++) {
3509 struct gpio_chip *chip;
3511 /* idx must always match exactly */
3515 /* If the lookup entry has a con_id, require exact match */
3516 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3519 chip = find_chip_by_name(p->chip_label);
3522 dev_err(dev, "cannot find GPIO chip %s\n",
3524 return ERR_PTR(-ENODEV);
3527 if (chip->ngpio <= p->chip_hwnum) {
3529 "requested GPIO %d is out of range [0..%d] for chip %s\n",
3530 idx, chip->ngpio, chip->label);
3531 return ERR_PTR(-EINVAL);
3534 desc = gpiochip_get_desc(chip, p->chip_hwnum);
3543 static int dt_gpio_count(struct device *dev, const char *con_id)
3549 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3551 snprintf(propname, sizeof(propname), "%s-%s",
3552 con_id, gpio_suffixes[i]);
3554 snprintf(propname, sizeof(propname), "%s",
3557 ret = of_gpio_named_count(dev->of_node, propname);
3561 return ret ? ret : -ENOENT;
3564 static int platform_gpio_count(struct device *dev, const char *con_id)
3566 struct gpiod_lookup_table *table;
3567 struct gpiod_lookup *p;
3568 unsigned int count = 0;
3570 table = gpiod_find_lookup_table(dev);
3574 for (p = &table->table[0]; p->chip_label; p++) {
3575 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3576 (!con_id && !p->con_id))
3586 * gpiod_count - return the number of GPIOs associated with a device / function
3587 * or -ENOENT if no GPIO has been assigned to the requested function
3588 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3589 * @con_id: function within the GPIO consumer
3591 int gpiod_count(struct device *dev, const char *con_id)
3593 int count = -ENOENT;
3595 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3596 count = dt_gpio_count(dev, con_id);
3597 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3598 count = acpi_gpio_count(dev, con_id);
3601 count = platform_gpio_count(dev, con_id);
3605 EXPORT_SYMBOL_GPL(gpiod_count);
3608 * gpiod_get - obtain a GPIO for a given GPIO function
3609 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3610 * @con_id: function within the GPIO consumer
3611 * @flags: optional GPIO initialization flags
3613 * Return the GPIO descriptor corresponding to the function con_id of device
3614 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
3615 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
3617 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
3618 enum gpiod_flags flags)
3620 return gpiod_get_index(dev, con_id, 0, flags);
3622 EXPORT_SYMBOL_GPL(gpiod_get);
3625 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3626 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3627 * @con_id: function within the GPIO consumer
3628 * @flags: optional GPIO initialization flags
3630 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3631 * the requested function it will return NULL. This is convenient for drivers
3632 * that need to handle optional GPIOs.
3634 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
3636 enum gpiod_flags flags)
3638 return gpiod_get_index_optional(dev, con_id, 0, flags);
3640 EXPORT_SYMBOL_GPL(gpiod_get_optional);
3644 * gpiod_configure_flags - helper function to configure a given GPIO
3645 * @desc: gpio whose value will be assigned
3646 * @con_id: function within the GPIO consumer
3647 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3649 * @dflags: gpiod_flags - optional GPIO initialization flags
3651 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3652 * requested function and/or index, or another IS_ERR() code if an error
3653 * occurred while trying to acquire the GPIO.
3655 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
3656 unsigned long lflags, enum gpiod_flags dflags)
3660 if (lflags & GPIO_ACTIVE_LOW)
3661 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3663 if (lflags & GPIO_OPEN_DRAIN)
3664 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3665 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
3667 * This enforces open drain mode from the consumer side.
3668 * This is necessary for some busses like I2C, but the lookup
3669 * should *REALLY* have specified them as open drain in the
3670 * first place, so print a little warning here.
3672 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3674 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
3677 if (lflags & GPIO_OPEN_SOURCE)
3678 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3680 status = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
3684 /* No particular flag request, return here... */
3685 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3686 pr_debug("no flags found for %s\n", con_id);
3691 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3692 status = gpiod_direction_output(desc,
3693 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
3695 status = gpiod_direction_input(desc);
3701 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
3702 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3703 * @con_id: function within the GPIO consumer
3704 * @idx: index of the GPIO to obtain in the consumer
3705 * @flags: optional GPIO initialization flags
3707 * This variant of gpiod_get() allows to access GPIOs other than the first
3708 * defined one for functions that define several GPIOs.
3710 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3711 * requested function and/or index, or another IS_ERR() code if an error
3712 * occurred while trying to acquire the GPIO.
3714 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
3717 enum gpiod_flags flags)
3719 struct gpio_desc *desc = NULL;
3721 enum gpio_lookup_flags lookupflags = 0;
3722 /* Maybe we have a device name, maybe not */
3723 const char *devname = dev ? dev_name(dev) : "?";
3725 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3728 /* Using device tree? */
3729 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3730 dev_dbg(dev, "using device tree for GPIO lookup\n");
3731 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3732 } else if (ACPI_COMPANION(dev)) {
3733 dev_dbg(dev, "using ACPI for GPIO lookup\n");
3734 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
3739 * Either we are not using DT or ACPI, or their lookup did not return
3740 * a result. In that case, use platform lookup as a fallback.
3742 if (!desc || desc == ERR_PTR(-ENOENT)) {
3743 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
3744 desc = gpiod_find(dev, con_id, idx, &lookupflags);
3748 dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
3753 * If a connection label was passed use that, else attempt to use
3754 * the device name as label
3756 status = gpiod_request(desc, con_id ? con_id : devname);
3758 return ERR_PTR(status);
3760 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
3762 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3764 return ERR_PTR(status);
3769 EXPORT_SYMBOL_GPL(gpiod_get_index);
3772 * gpiod_get_from_of_node() - obtain a GPIO from an OF node
3773 * @node: handle of the OF node
3774 * @propname: name of the DT property representing the GPIO
3775 * @index: index of the GPIO to obtain for the consumer
3776 * @dflags: GPIO initialization flags
3777 * @label: label to attach to the requested GPIO
3780 * On successful request the GPIO pin is configured in accordance with
3781 * provided @dflags. If the node does not have the requested GPIO
3782 * property, NULL is returned.
3784 * In case of error an ERR_PTR() is returned.
3786 struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
3787 const char *propname, int index,
3788 enum gpiod_flags dflags,
3791 struct gpio_desc *desc;
3792 unsigned long lflags = 0;
3793 enum of_gpio_flags flags;
3794 bool active_low = false;
3795 bool single_ended = false;
3796 bool open_drain = false;
3797 bool transitory = false;
3800 desc = of_get_named_gpiod_flags(node, propname,
3803 if (!desc || IS_ERR(desc)) {
3804 /* If it is not there, just return NULL */
3805 if (PTR_ERR(desc) == -ENOENT)
3810 active_low = flags & OF_GPIO_ACTIVE_LOW;
3811 single_ended = flags & OF_GPIO_SINGLE_ENDED;
3812 open_drain = flags & OF_GPIO_OPEN_DRAIN;
3813 transitory = flags & OF_GPIO_TRANSITORY;
3815 ret = gpiod_request(desc, label);
3817 return ERR_PTR(ret);
3820 lflags |= GPIO_ACTIVE_LOW;
3824 lflags |= GPIO_OPEN_DRAIN;
3826 lflags |= GPIO_OPEN_SOURCE;
3830 lflags |= GPIO_TRANSITORY;
3832 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3835 return ERR_PTR(ret);
3840 EXPORT_SYMBOL(gpiod_get_from_of_node);
3843 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3844 * @fwnode: handle of the firmware node
3845 * @propname: name of the firmware property representing the GPIO
3846 * @index: index of the GPIO to obtain for the consumer
3847 * @dflags: GPIO initialization flags
3848 * @label: label to attach to the requested GPIO
3850 * This function can be used for drivers that get their configuration
3851 * from opaque firmware.
3853 * The function properly finds the corresponding GPIO using whatever is the
3854 * underlying firmware interface and then makes sure that the GPIO
3855 * descriptor is requested before it is returned to the caller.
3858 * On successful request the GPIO pin is configured in accordance with
3861 * In case of error an ERR_PTR() is returned.
3863 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
3864 const char *propname, int index,
3865 enum gpiod_flags dflags,
3868 struct gpio_desc *desc = ERR_PTR(-ENODEV);
3869 unsigned long lflags = 0;
3873 return ERR_PTR(-EINVAL);
3875 if (is_of_node(fwnode)) {
3876 desc = gpiod_get_from_of_node(to_of_node(fwnode),
3881 } else if (is_acpi_node(fwnode)) {
3882 struct acpi_gpio_info info;
3884 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
3888 acpi_gpio_update_gpiod_flags(&dflags, &info);
3890 if (info.polarity == GPIO_ACTIVE_LOW)
3891 lflags |= GPIO_ACTIVE_LOW;
3894 /* Currently only ACPI takes this path */
3895 ret = gpiod_request(desc, label);
3897 return ERR_PTR(ret);
3899 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3902 return ERR_PTR(ret);
3907 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3910 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3912 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3913 * @con_id: function within the GPIO consumer
3914 * @index: index of the GPIO to obtain in the consumer
3915 * @flags: optional GPIO initialization flags
3917 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3918 * specified index was assigned to the requested function it will return NULL.
3919 * This is convenient for drivers that need to handle optional GPIOs.
3921 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
3924 enum gpiod_flags flags)
3926 struct gpio_desc *desc;
3928 desc = gpiod_get_index(dev, con_id, index, flags);
3930 if (PTR_ERR(desc) == -ENOENT)
3936 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
3939 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3940 * @desc: gpio whose value will be assigned
3941 * @name: gpio line name
3942 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3944 * @dflags: gpiod_flags - optional GPIO initialization flags
3946 int gpiod_hog(struct gpio_desc *desc, const char *name,
3947 unsigned long lflags, enum gpiod_flags dflags)
3949 struct gpio_chip *chip;
3950 struct gpio_desc *local_desc;
3954 chip = gpiod_to_chip(desc);
3955 hwnum = gpio_chip_hwgpio(desc);
3957 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
3958 if (IS_ERR(local_desc)) {
3959 status = PTR_ERR(local_desc);
3960 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3961 name, chip->label, hwnum, status);
3965 status = gpiod_configure_flags(desc, name, lflags, dflags);
3967 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3968 name, chip->label, hwnum, status);
3969 gpiochip_free_own_desc(desc);
3973 /* Mark GPIO as hogged so it can be identified and removed later */
3974 set_bit(FLAG_IS_HOGGED, &desc->flags);
3976 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3977 desc_to_gpio(desc), name,
3978 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3979 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3980 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3986 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3987 * @chip: gpio chip to act on
3989 * This is only used by of_gpiochip_remove to free hogged gpios
3991 static void gpiochip_free_hogs(struct gpio_chip *chip)
3995 for (id = 0; id < chip->ngpio; id++) {
3996 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
3997 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
4002 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4003 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4004 * @con_id: function within the GPIO consumer
4005 * @flags: optional GPIO initialization flags
4007 * This function acquires all the GPIOs defined under a given function.
4009 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4010 * no GPIO has been assigned to the requested function, or another IS_ERR()
4011 * code if an error occurred while trying to acquire the GPIOs.
4013 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4015 enum gpiod_flags flags)
4017 struct gpio_desc *desc;
4018 struct gpio_descs *descs;
4021 count = gpiod_count(dev, con_id);
4023 return ERR_PTR(count);
4025 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
4028 return ERR_PTR(-ENOMEM);
4030 for (descs->ndescs = 0; descs->ndescs < count; ) {
4031 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4033 gpiod_put_array(descs);
4034 return ERR_CAST(desc);
4036 descs->desc[descs->ndescs] = desc;
4041 EXPORT_SYMBOL_GPL(gpiod_get_array);
4044 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4046 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4047 * @con_id: function within the GPIO consumer
4048 * @flags: optional GPIO initialization flags
4050 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4051 * assigned to the requested function it will return NULL.
4053 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4055 enum gpiod_flags flags)
4057 struct gpio_descs *descs;
4059 descs = gpiod_get_array(dev, con_id, flags);
4060 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
4065 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4068 * gpiod_put - dispose of a GPIO descriptor
4069 * @desc: GPIO descriptor to dispose of
4071 * No descriptor can be used after gpiod_put() has been called on it.
4073 void gpiod_put(struct gpio_desc *desc)
4077 EXPORT_SYMBOL_GPL(gpiod_put);
4080 * gpiod_put_array - dispose of multiple GPIO descriptors
4081 * @descs: struct gpio_descs containing an array of descriptors
4083 void gpiod_put_array(struct gpio_descs *descs)
4087 for (i = 0; i < descs->ndescs; i++)
4088 gpiod_put(descs->desc[i]);
4092 EXPORT_SYMBOL_GPL(gpiod_put_array);
4094 static int __init gpiolib_dev_init(void)
4098 /* Register GPIO sysfs bus */
4099 ret = bus_register(&gpio_bus_type);
4101 pr_err("gpiolib: could not register GPIO bus type\n");
4105 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
4107 pr_err("gpiolib: failed to allocate char dev region\n");
4108 bus_unregister(&gpio_bus_type);
4110 gpiolib_initialized = true;
4111 gpiochip_setup_devs();
4115 core_initcall(gpiolib_dev_init);
4117 #ifdef CONFIG_DEBUG_FS
4119 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
4122 struct gpio_chip *chip = gdev->chip;
4123 unsigned gpio = gdev->base;
4124 struct gpio_desc *gdesc = &gdev->descs[0];
4128 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
4129 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
4131 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
4137 gpiod_get_direction(gdesc);
4138 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
4139 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
4140 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
4141 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
4142 is_out ? "out" : "in ",
4144 ? (chip->get(chip, i) ? "hi" : "lo")
4146 is_irq ? "IRQ" : " ");
4147 seq_printf(s, "\n");
4151 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
4153 unsigned long flags;
4154 struct gpio_device *gdev = NULL;
4155 loff_t index = *pos;
4159 spin_lock_irqsave(&gpio_lock, flags);
4160 list_for_each_entry(gdev, &gpio_devices, list)
4162 spin_unlock_irqrestore(&gpio_lock, flags);
4165 spin_unlock_irqrestore(&gpio_lock, flags);
4170 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4172 unsigned long flags;
4173 struct gpio_device *gdev = v;
4176 spin_lock_irqsave(&gpio_lock, flags);
4177 if (list_is_last(&gdev->list, &gpio_devices))
4180 ret = list_entry(gdev->list.next, struct gpio_device, list);
4181 spin_unlock_irqrestore(&gpio_lock, flags);
4189 static void gpiolib_seq_stop(struct seq_file *s, void *v)
4193 static int gpiolib_seq_show(struct seq_file *s, void *v)
4195 struct gpio_device *gdev = v;
4196 struct gpio_chip *chip = gdev->chip;
4197 struct device *parent;
4200 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4201 dev_name(&gdev->dev));
4205 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4206 dev_name(&gdev->dev),
4207 gdev->base, gdev->base + gdev->ngpio - 1);
4208 parent = chip->parent;
4210 seq_printf(s, ", parent: %s/%s",
4211 parent->bus ? parent->bus->name : "no-bus",
4214 seq_printf(s, ", %s", chip->label);
4215 if (chip->can_sleep)
4216 seq_printf(s, ", can sleep");
4217 seq_printf(s, ":\n");
4220 chip->dbg_show(s, chip);
4222 gpiolib_dbg_show(s, gdev);
4227 static const struct seq_operations gpiolib_seq_ops = {
4228 .start = gpiolib_seq_start,
4229 .next = gpiolib_seq_next,
4230 .stop = gpiolib_seq_stop,
4231 .show = gpiolib_seq_show,
4234 static int gpiolib_open(struct inode *inode, struct file *file)
4236 return seq_open(file, &gpiolib_seq_ops);
4239 static const struct file_operations gpiolib_operations = {
4240 .owner = THIS_MODULE,
4241 .open = gpiolib_open,
4243 .llseek = seq_lseek,
4244 .release = seq_release,
4247 static int __init gpiolib_debugfs_init(void)
4249 /* /sys/kernel/debug/gpio */
4250 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
4251 NULL, NULL, &gpiolib_operations);
4254 subsys_initcall(gpiolib_debugfs_init);
4256 #endif /* DEBUG_FS */