fs: make fiemap work from compat_ioctl
[linux/fpc-iii.git] / drivers / gpio / gpiolib.c
blobeb80dac4e26a52e6f18a4652d3c7fb03451b516a
1 #include <linux/bitmap.h>
2 #include <linux/kernel.h>
3 #include <linux/module.h>
4 #include <linux/interrupt.h>
5 #include <linux/irq.h>
6 #include <linux/spinlock.h>
7 #include <linux/list.h>
8 #include <linux/device.h>
9 #include <linux/err.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>
21 #include <linux/fs.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>
31 #include "gpiolib.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.
50 #ifdef DEBUG
51 #define extra_checks 1
52 #else
53 #define extra_checks 0
54 #endif
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 = {
61 .name = "gpio",
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 void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
76 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
77 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
79 static bool gpiolib_initialized;
81 static inline void desc_set_label(struct gpio_desc *d, const char *label)
83 d->label = label;
86 /**
87 * gpio_to_desc - Convert a GPIO number to its descriptor
88 * @gpio: global GPIO number
90 * Returns:
91 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
92 * with the given number exists in the system.
94 struct gpio_desc *gpio_to_desc(unsigned gpio)
96 struct gpio_device *gdev;
97 unsigned long flags;
99 spin_lock_irqsave(&gpio_lock, flags);
101 list_for_each_entry(gdev, &gpio_devices, list) {
102 if (gdev->base <= gpio &&
103 gdev->base + gdev->ngpio > gpio) {
104 spin_unlock_irqrestore(&gpio_lock, flags);
105 return &gdev->descs[gpio - gdev->base];
109 spin_unlock_irqrestore(&gpio_lock, flags);
111 if (!gpio_is_valid(gpio))
112 WARN(1, "invalid GPIO %d\n", gpio);
114 return NULL;
116 EXPORT_SYMBOL_GPL(gpio_to_desc);
119 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
120 * hardware number for this chip
121 * @chip: GPIO chip
122 * @hwnum: hardware number of the GPIO for this chip
124 * Returns:
125 * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists
126 * in the given chip for the specified hardware number.
128 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
129 u16 hwnum)
131 struct gpio_device *gdev = chip->gpiodev;
133 if (hwnum >= gdev->ngpio)
134 return ERR_PTR(-EINVAL);
136 return &gdev->descs[hwnum];
140 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
141 * @desc: GPIO descriptor
143 * This should disappear in the future but is needed since we still
144 * use GPIO numbers for error messages and sysfs nodes.
146 * Returns:
147 * The global GPIO number for the GPIO specified by its descriptor.
149 int desc_to_gpio(const struct gpio_desc *desc)
151 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
153 EXPORT_SYMBOL_GPL(desc_to_gpio);
157 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
158 * @desc: descriptor to return the chip of
160 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
162 if (!desc || !desc->gdev || !desc->gdev->chip)
163 return NULL;
164 return desc->gdev->chip;
166 EXPORT_SYMBOL_GPL(gpiod_to_chip);
168 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
169 static int gpiochip_find_base(int ngpio)
171 struct gpio_device *gdev;
172 int base = ARCH_NR_GPIOS - ngpio;
174 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
175 /* found a free space? */
176 if (gdev->base + gdev->ngpio <= base)
177 break;
178 else
179 /* nope, check the space right before the chip */
180 base = gdev->base - ngpio;
183 if (gpio_is_valid(base)) {
184 pr_debug("%s: found new base at %d\n", __func__, base);
185 return base;
186 } else {
187 pr_err("%s: cannot find free range\n", __func__);
188 return -ENOSPC;
193 * gpiod_get_direction - return the current direction of a GPIO
194 * @desc: GPIO to get the direction of
196 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
198 * This function may sleep if gpiod_cansleep() is true.
200 int gpiod_get_direction(struct gpio_desc *desc)
202 struct gpio_chip *chip;
203 unsigned offset;
204 int status = -EINVAL;
206 chip = gpiod_to_chip(desc);
207 offset = gpio_chip_hwgpio(desc);
209 if (!chip->get_direction)
210 return status;
212 status = chip->get_direction(chip, offset);
213 if (status > 0) {
214 /* GPIOF_DIR_IN, or other positive */
215 status = 1;
216 clear_bit(FLAG_IS_OUT, &desc->flags);
218 if (status == 0) {
219 /* GPIOF_DIR_OUT */
220 set_bit(FLAG_IS_OUT, &desc->flags);
222 return status;
224 EXPORT_SYMBOL_GPL(gpiod_get_direction);
227 * Add a new chip to the global chips list, keeping the list of chips sorted
228 * by range(means [base, base + ngpio - 1]) order.
230 * Return -EBUSY if the new chip overlaps with some other chip's integer
231 * space.
233 static int gpiodev_add_to_list(struct gpio_device *gdev)
235 struct gpio_device *prev, *next;
237 if (list_empty(&gpio_devices)) {
238 /* initial entry in list */
239 list_add_tail(&gdev->list, &gpio_devices);
240 return 0;
243 next = list_entry(gpio_devices.next, struct gpio_device, list);
244 if (gdev->base + gdev->ngpio <= next->base) {
245 /* add before first entry */
246 list_add(&gdev->list, &gpio_devices);
247 return 0;
250 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
251 if (prev->base + prev->ngpio <= gdev->base) {
252 /* add behind last entry */
253 list_add_tail(&gdev->list, &gpio_devices);
254 return 0;
257 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
258 /* at the end of the list */
259 if (&next->list == &gpio_devices)
260 break;
262 /* add between prev and next */
263 if (prev->base + prev->ngpio <= gdev->base
264 && gdev->base + gdev->ngpio <= next->base) {
265 list_add(&gdev->list, &prev->list);
266 return 0;
270 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
271 return -EBUSY;
275 * Convert a GPIO name to its descriptor
277 static struct gpio_desc *gpio_name_to_desc(const char * const name)
279 struct gpio_device *gdev;
280 unsigned long flags;
282 spin_lock_irqsave(&gpio_lock, flags);
284 list_for_each_entry(gdev, &gpio_devices, list) {
285 int i;
287 for (i = 0; i != gdev->ngpio; ++i) {
288 struct gpio_desc *desc = &gdev->descs[i];
290 if (!desc->name || !name)
291 continue;
293 if (!strcmp(desc->name, name)) {
294 spin_unlock_irqrestore(&gpio_lock, flags);
295 return desc;
300 spin_unlock_irqrestore(&gpio_lock, flags);
302 return NULL;
306 * Takes the names from gc->names and checks if they are all unique. If they
307 * are, they are assigned to their gpio descriptors.
309 * Warning if one of the names is already used for a different GPIO.
311 static int gpiochip_set_desc_names(struct gpio_chip *gc)
313 struct gpio_device *gdev = gc->gpiodev;
314 int i;
316 if (!gc->names)
317 return 0;
319 /* First check all names if they are unique */
320 for (i = 0; i != gc->ngpio; ++i) {
321 struct gpio_desc *gpio;
323 gpio = gpio_name_to_desc(gc->names[i]);
324 if (gpio)
325 dev_warn(&gdev->dev,
326 "Detected name collision for GPIO name '%s'\n",
327 gc->names[i]);
330 /* Then add all names to the GPIO descriptors */
331 for (i = 0; i != gc->ngpio; ++i)
332 gdev->descs[i].name = gc->names[i];
334 return 0;
338 * GPIO line handle management
342 * struct linehandle_state - contains the state of a userspace handle
343 * @gdev: the GPIO device the handle pertains to
344 * @label: consumer label used to tag descriptors
345 * @descs: the GPIO descriptors held by this handle
346 * @numdescs: the number of descriptors held in the descs array
348 struct linehandle_state {
349 struct gpio_device *gdev;
350 const char *label;
351 struct gpio_desc *descs[GPIOHANDLES_MAX];
352 u32 numdescs;
355 #define GPIOHANDLE_REQUEST_VALID_FLAGS \
356 (GPIOHANDLE_REQUEST_INPUT | \
357 GPIOHANDLE_REQUEST_OUTPUT | \
358 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
359 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
360 GPIOHANDLE_REQUEST_OPEN_SOURCE)
362 static long linehandle_ioctl(struct file *filep, unsigned int cmd,
363 unsigned long arg)
365 struct linehandle_state *lh = filep->private_data;
366 void __user *ip = (void __user *)arg;
367 struct gpiohandle_data ghd;
368 int i;
370 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
371 int val;
373 memset(&ghd, 0, sizeof(ghd));
375 /* TODO: check if descriptors are really input */
376 for (i = 0; i < lh->numdescs; i++) {
377 val = gpiod_get_value_cansleep(lh->descs[i]);
378 if (val < 0)
379 return val;
380 ghd.values[i] = val;
383 if (copy_to_user(ip, &ghd, sizeof(ghd)))
384 return -EFAULT;
386 return 0;
387 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
388 int vals[GPIOHANDLES_MAX];
390 /* TODO: check if descriptors are really output */
391 if (copy_from_user(&ghd, ip, sizeof(ghd)))
392 return -EFAULT;
394 /* Clamp all values to [0,1] */
395 for (i = 0; i < lh->numdescs; i++)
396 vals[i] = !!ghd.values[i];
398 /* Reuse the array setting function */
399 gpiod_set_array_value_complex(false,
400 true,
401 lh->numdescs,
402 lh->descs,
403 vals);
404 return 0;
406 return -EINVAL;
409 #ifdef CONFIG_COMPAT
410 static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
411 unsigned long arg)
413 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
415 #endif
417 static int linehandle_release(struct inode *inode, struct file *filep)
419 struct linehandle_state *lh = filep->private_data;
420 struct gpio_device *gdev = lh->gdev;
421 int i;
423 for (i = 0; i < lh->numdescs; i++)
424 gpiod_free(lh->descs[i]);
425 kfree(lh->label);
426 kfree(lh);
427 put_device(&gdev->dev);
428 return 0;
431 static const struct file_operations linehandle_fileops = {
432 .release = linehandle_release,
433 .owner = THIS_MODULE,
434 .llseek = noop_llseek,
435 .unlocked_ioctl = linehandle_ioctl,
436 #ifdef CONFIG_COMPAT
437 .compat_ioctl = linehandle_ioctl_compat,
438 #endif
441 static int linehandle_create(struct gpio_device *gdev, void __user *ip)
443 struct gpiohandle_request handlereq;
444 struct linehandle_state *lh;
445 struct file *file;
446 int fd, i, ret;
448 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
449 return -EFAULT;
450 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
451 return -EINVAL;
453 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
454 if (!lh)
455 return -ENOMEM;
456 lh->gdev = gdev;
457 get_device(&gdev->dev);
459 /* Make sure this is terminated */
460 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
461 if (strlen(handlereq.consumer_label)) {
462 lh->label = kstrdup(handlereq.consumer_label,
463 GFP_KERNEL);
464 if (!lh->label) {
465 ret = -ENOMEM;
466 goto out_free_lh;
470 /* Request each GPIO */
471 for (i = 0; i < handlereq.lines; i++) {
472 u32 offset = handlereq.lineoffsets[i];
473 u32 lflags = handlereq.flags;
474 struct gpio_desc *desc;
476 if (offset >= gdev->ngpio) {
477 ret = -EINVAL;
478 goto out_free_descs;
481 /* Return an error if a unknown flag is set */
482 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) {
483 ret = -EINVAL;
484 goto out_free_descs;
487 desc = &gdev->descs[offset];
488 ret = gpiod_request(desc, lh->label);
489 if (ret)
490 goto out_free_descs;
491 lh->descs[i] = desc;
493 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
494 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
495 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
496 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
497 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
498 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
501 * Lines have to be requested explicitly for input
502 * or output, else the line will be treated "as is".
504 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
505 int val = !!handlereq.default_values[i];
507 ret = gpiod_direction_output(desc, val);
508 if (ret)
509 goto out_free_descs;
510 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
511 ret = gpiod_direction_input(desc);
512 if (ret)
513 goto out_free_descs;
515 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
516 offset);
518 /* Let i point at the last handle */
519 i--;
520 lh->numdescs = handlereq.lines;
522 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
523 if (fd < 0) {
524 ret = fd;
525 goto out_free_descs;
528 file = anon_inode_getfile("gpio-linehandle",
529 &linehandle_fileops,
531 O_RDONLY | O_CLOEXEC);
532 if (IS_ERR(file)) {
533 ret = PTR_ERR(file);
534 goto out_put_unused_fd;
537 handlereq.fd = fd;
538 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
540 * fput() will trigger the release() callback, so do not go onto
541 * the regular error cleanup path here.
543 fput(file);
544 put_unused_fd(fd);
545 return -EFAULT;
548 fd_install(fd, file);
550 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
551 lh->numdescs);
553 return 0;
555 out_put_unused_fd:
556 put_unused_fd(fd);
557 out_free_descs:
558 for (; i >= 0; i--)
559 gpiod_free(lh->descs[i]);
560 kfree(lh->label);
561 out_free_lh:
562 kfree(lh);
563 put_device(&gdev->dev);
564 return ret;
568 * GPIO line event management
572 * struct lineevent_state - contains the state of a userspace event
573 * @gdev: the GPIO device the event pertains to
574 * @label: consumer label used to tag descriptors
575 * @desc: the GPIO descriptor held by this event
576 * @eflags: the event flags this line was requested with
577 * @irq: the interrupt that trigger in response to events on this GPIO
578 * @wait: wait queue that handles blocking reads of events
579 * @events: KFIFO for the GPIO events
580 * @read_lock: mutex lock to protect reads from colliding with adding
581 * new events to the FIFO
583 struct lineevent_state {
584 struct gpio_device *gdev;
585 const char *label;
586 struct gpio_desc *desc;
587 u32 eflags;
588 int irq;
589 wait_queue_head_t wait;
590 DECLARE_KFIFO(events, struct gpioevent_data, 16);
591 struct mutex read_lock;
594 #define GPIOEVENT_REQUEST_VALID_FLAGS \
595 (GPIOEVENT_REQUEST_RISING_EDGE | \
596 GPIOEVENT_REQUEST_FALLING_EDGE)
598 static unsigned int lineevent_poll(struct file *filep,
599 struct poll_table_struct *wait)
601 struct lineevent_state *le = filep->private_data;
602 unsigned int events = 0;
604 poll_wait(filep, &le->wait, wait);
606 if (!kfifo_is_empty(&le->events))
607 events = POLLIN | POLLRDNORM;
609 return events;
613 static ssize_t lineevent_read(struct file *filep,
614 char __user *buf,
615 size_t count,
616 loff_t *f_ps)
618 struct lineevent_state *le = filep->private_data;
619 unsigned int copied;
620 int ret;
622 if (count < sizeof(struct gpioevent_data))
623 return -EINVAL;
625 do {
626 if (kfifo_is_empty(&le->events)) {
627 if (filep->f_flags & O_NONBLOCK)
628 return -EAGAIN;
630 ret = wait_event_interruptible(le->wait,
631 !kfifo_is_empty(&le->events));
632 if (ret)
633 return ret;
636 if (mutex_lock_interruptible(&le->read_lock))
637 return -ERESTARTSYS;
638 ret = kfifo_to_user(&le->events, buf, count, &copied);
639 mutex_unlock(&le->read_lock);
641 if (ret)
642 return ret;
645 * If we couldn't read anything from the fifo (a different
646 * thread might have been faster) we either return -EAGAIN if
647 * the file descriptor is non-blocking, otherwise we go back to
648 * sleep and wait for more data to arrive.
650 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
651 return -EAGAIN;
653 } while (copied == 0);
655 return copied;
658 static int lineevent_release(struct inode *inode, struct file *filep)
660 struct lineevent_state *le = filep->private_data;
661 struct gpio_device *gdev = le->gdev;
663 free_irq(le->irq, le);
664 gpiod_free(le->desc);
665 kfree(le->label);
666 kfree(le);
667 put_device(&gdev->dev);
668 return 0;
671 static long lineevent_ioctl(struct file *filep, unsigned int cmd,
672 unsigned long arg)
674 struct lineevent_state *le = filep->private_data;
675 void __user *ip = (void __user *)arg;
676 struct gpiohandle_data ghd;
679 * We can get the value for an event line but not set it,
680 * because it is input by definition.
682 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
683 int val;
685 memset(&ghd, 0, sizeof(ghd));
687 val = gpiod_get_value_cansleep(le->desc);
688 if (val < 0)
689 return val;
690 ghd.values[0] = val;
692 if (copy_to_user(ip, &ghd, sizeof(ghd)))
693 return -EFAULT;
695 return 0;
697 return -EINVAL;
700 #ifdef CONFIG_COMPAT
701 static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
702 unsigned long arg)
704 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
706 #endif
708 static const struct file_operations lineevent_fileops = {
709 .release = lineevent_release,
710 .read = lineevent_read,
711 .poll = lineevent_poll,
712 .owner = THIS_MODULE,
713 .llseek = noop_llseek,
714 .unlocked_ioctl = lineevent_ioctl,
715 #ifdef CONFIG_COMPAT
716 .compat_ioctl = lineevent_ioctl_compat,
717 #endif
720 static irqreturn_t lineevent_irq_thread(int irq, void *p)
722 struct lineevent_state *le = p;
723 struct gpioevent_data ge;
724 int ret, level;
726 ge.timestamp = ktime_get_real_ns();
727 level = gpiod_get_value_cansleep(le->desc);
729 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
730 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
731 if (level)
732 /* Emit low-to-high event */
733 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
734 else
735 /* Emit high-to-low event */
736 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
737 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE && level) {
738 /* Emit low-to-high event */
739 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
740 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE && !level) {
741 /* Emit high-to-low event */
742 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
743 } else {
744 return IRQ_NONE;
747 ret = kfifo_put(&le->events, ge);
748 if (ret != 0)
749 wake_up_poll(&le->wait, POLLIN);
751 return IRQ_HANDLED;
754 static int lineevent_create(struct gpio_device *gdev, void __user *ip)
756 struct gpioevent_request eventreq;
757 struct lineevent_state *le;
758 struct gpio_desc *desc;
759 struct file *file;
760 u32 offset;
761 u32 lflags;
762 u32 eflags;
763 int fd;
764 int ret;
765 int irqflags = 0;
767 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
768 return -EFAULT;
770 le = kzalloc(sizeof(*le), GFP_KERNEL);
771 if (!le)
772 return -ENOMEM;
773 le->gdev = gdev;
774 get_device(&gdev->dev);
776 /* Make sure this is terminated */
777 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
778 if (strlen(eventreq.consumer_label)) {
779 le->label = kstrdup(eventreq.consumer_label,
780 GFP_KERNEL);
781 if (!le->label) {
782 ret = -ENOMEM;
783 goto out_free_le;
787 offset = eventreq.lineoffset;
788 lflags = eventreq.handleflags;
789 eflags = eventreq.eventflags;
791 if (offset >= gdev->ngpio) {
792 ret = -EINVAL;
793 goto out_free_label;
796 /* Return an error if a unknown flag is set */
797 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
798 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
799 ret = -EINVAL;
800 goto out_free_label;
803 /* This is just wrong: we don't look for events on output lines */
804 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
805 ret = -EINVAL;
806 goto out_free_label;
809 desc = &gdev->descs[offset];
810 ret = gpiod_request(desc, le->label);
811 if (ret)
812 goto out_free_desc;
813 le->desc = desc;
814 le->eflags = eflags;
816 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
817 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
818 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
819 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
820 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
821 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
823 ret = gpiod_direction_input(desc);
824 if (ret)
825 goto out_free_desc;
827 le->irq = gpiod_to_irq(desc);
828 if (le->irq <= 0) {
829 ret = -ENODEV;
830 goto out_free_desc;
833 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
834 irqflags |= IRQF_TRIGGER_RISING;
835 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
836 irqflags |= IRQF_TRIGGER_FALLING;
837 irqflags |= IRQF_ONESHOT;
838 irqflags |= IRQF_SHARED;
840 INIT_KFIFO(le->events);
841 init_waitqueue_head(&le->wait);
842 mutex_init(&le->read_lock);
844 /* Request a thread to read the events */
845 ret = request_threaded_irq(le->irq,
846 NULL,
847 lineevent_irq_thread,
848 irqflags,
849 le->label,
850 le);
851 if (ret)
852 goto out_free_desc;
854 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
855 if (fd < 0) {
856 ret = fd;
857 goto out_free_irq;
860 file = anon_inode_getfile("gpio-event",
861 &lineevent_fileops,
863 O_RDONLY | O_CLOEXEC);
864 if (IS_ERR(file)) {
865 ret = PTR_ERR(file);
866 goto out_put_unused_fd;
869 eventreq.fd = fd;
870 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
872 * fput() will trigger the release() callback, so do not go onto
873 * the regular error cleanup path here.
875 fput(file);
876 put_unused_fd(fd);
877 return -EFAULT;
880 fd_install(fd, file);
882 return 0;
884 out_put_unused_fd:
885 put_unused_fd(fd);
886 out_free_irq:
887 free_irq(le->irq, le);
888 out_free_desc:
889 gpiod_free(le->desc);
890 out_free_label:
891 kfree(le->label);
892 out_free_le:
893 kfree(le);
894 put_device(&gdev->dev);
895 return ret;
899 * gpio_ioctl() - ioctl handler for the GPIO chardev
901 static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
903 struct gpio_device *gdev = filp->private_data;
904 struct gpio_chip *chip = gdev->chip;
905 void __user *ip = (void __user *)arg;
907 /* We fail any subsequent ioctl():s when the chip is gone */
908 if (!chip)
909 return -ENODEV;
911 /* Fill in the struct and pass to userspace */
912 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
913 struct gpiochip_info chipinfo;
915 memset(&chipinfo, 0, sizeof(chipinfo));
917 strncpy(chipinfo.name, dev_name(&gdev->dev),
918 sizeof(chipinfo.name));
919 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
920 strncpy(chipinfo.label, gdev->label,
921 sizeof(chipinfo.label));
922 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
923 chipinfo.lines = gdev->ngpio;
924 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
925 return -EFAULT;
926 return 0;
927 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
928 struct gpioline_info lineinfo;
929 struct gpio_desc *desc;
931 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
932 return -EFAULT;
933 if (lineinfo.line_offset >= gdev->ngpio)
934 return -EINVAL;
936 desc = &gdev->descs[lineinfo.line_offset];
937 if (desc->name) {
938 strncpy(lineinfo.name, desc->name,
939 sizeof(lineinfo.name));
940 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
941 } else {
942 lineinfo.name[0] = '\0';
944 if (desc->label) {
945 strncpy(lineinfo.consumer, desc->label,
946 sizeof(lineinfo.consumer));
947 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
948 } else {
949 lineinfo.consumer[0] = '\0';
953 * Userspace only need to know that the kernel is using
954 * this GPIO so it can't use it.
956 lineinfo.flags = 0;
957 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
958 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
959 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
960 test_bit(FLAG_EXPORT, &desc->flags) ||
961 test_bit(FLAG_SYSFS, &desc->flags))
962 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
963 if (test_bit(FLAG_IS_OUT, &desc->flags))
964 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
965 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
966 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
967 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
968 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
969 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
970 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
972 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
973 return -EFAULT;
974 return 0;
975 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
976 return linehandle_create(gdev, ip);
977 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
978 return lineevent_create(gdev, ip);
980 return -EINVAL;
983 #ifdef CONFIG_COMPAT
984 static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
985 unsigned long arg)
987 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
989 #endif
992 * gpio_chrdev_open() - open the chardev for ioctl operations
993 * @inode: inode for this chardev
994 * @filp: file struct for storing private data
995 * Returns 0 on success
997 static int gpio_chrdev_open(struct inode *inode, struct file *filp)
999 struct gpio_device *gdev = container_of(inode->i_cdev,
1000 struct gpio_device, chrdev);
1002 /* Fail on open if the backing gpiochip is gone */
1003 if (!gdev->chip)
1004 return -ENODEV;
1005 get_device(&gdev->dev);
1006 filp->private_data = gdev;
1008 return nonseekable_open(inode, filp);
1012 * gpio_chrdev_release() - close chardev after ioctl operations
1013 * @inode: inode for this chardev
1014 * @filp: file struct for storing private data
1015 * Returns 0 on success
1017 static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1019 struct gpio_device *gdev = container_of(inode->i_cdev,
1020 struct gpio_device, chrdev);
1022 put_device(&gdev->dev);
1023 return 0;
1027 static const struct file_operations gpio_fileops = {
1028 .release = gpio_chrdev_release,
1029 .open = gpio_chrdev_open,
1030 .owner = THIS_MODULE,
1031 .llseek = no_llseek,
1032 .unlocked_ioctl = gpio_ioctl,
1033 #ifdef CONFIG_COMPAT
1034 .compat_ioctl = gpio_ioctl_compat,
1035 #endif
1038 static void gpiodevice_release(struct device *dev)
1040 struct gpio_device *gdev = dev_get_drvdata(dev);
1042 list_del(&gdev->list);
1043 ida_simple_remove(&gpio_ida, gdev->id);
1044 kfree(gdev->label);
1045 kfree(gdev->descs);
1046 kfree(gdev);
1049 static int gpiochip_setup_dev(struct gpio_device *gdev)
1051 int status;
1053 cdev_init(&gdev->chrdev, &gpio_fileops);
1054 gdev->chrdev.owner = THIS_MODULE;
1055 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
1057 status = cdev_device_add(&gdev->chrdev, &gdev->dev);
1058 if (status)
1059 return status;
1061 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1062 MAJOR(gpio_devt), gdev->id);
1064 status = gpiochip_sysfs_register(gdev);
1065 if (status)
1066 goto err_remove_device;
1068 /* From this point, the .release() function cleans up gpio_device */
1069 gdev->dev.release = gpiodevice_release;
1070 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1071 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1072 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1074 return 0;
1076 err_remove_device:
1077 cdev_device_del(&gdev->chrdev, &gdev->dev);
1078 return status;
1081 static void gpiochip_setup_devs(void)
1083 struct gpio_device *gdev;
1084 int err;
1086 list_for_each_entry(gdev, &gpio_devices, list) {
1087 err = gpiochip_setup_dev(gdev);
1088 if (err)
1089 pr_err("%s: Failed to initialize gpio device (%d)\n",
1090 dev_name(&gdev->dev), err);
1095 * gpiochip_add_data() - register a gpio_chip
1096 * @chip: the chip to register, with chip->base initialized
1097 * @data: driver-private data associated with this chip
1099 * Context: potentially before irqs will work
1101 * When gpiochip_add_data() is called very early during boot, so that GPIOs
1102 * can be freely used, the chip->parent device must be registered before
1103 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1104 * for GPIOs will fail rudely.
1106 * gpiochip_add_data() must only be called after gpiolib initialization,
1107 * ie after core_initcall().
1109 * If chip->base is negative, this requests dynamic assignment of
1110 * a range of valid GPIOs.
1112 * Returns:
1113 * A negative errno if the chip can't be registered, such as because the
1114 * chip->base is invalid or already associated with a different chip.
1115 * Otherwise it returns zero as a success code.
1117 int gpiochip_add_data(struct gpio_chip *chip, void *data)
1119 unsigned long flags;
1120 int status = 0;
1121 unsigned i;
1122 int base = chip->base;
1123 struct gpio_device *gdev;
1126 * First: allocate and populate the internal stat container, and
1127 * set up the struct device.
1129 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
1130 if (!gdev)
1131 return -ENOMEM;
1132 gdev->dev.bus = &gpio_bus_type;
1133 gdev->chip = chip;
1134 chip->gpiodev = gdev;
1135 if (chip->parent) {
1136 gdev->dev.parent = chip->parent;
1137 gdev->dev.of_node = chip->parent->of_node;
1140 #ifdef CONFIG_OF_GPIO
1141 /* If the gpiochip has an assigned OF node this takes precedence */
1142 if (chip->of_node)
1143 gdev->dev.of_node = chip->of_node;
1144 #endif
1146 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1147 if (gdev->id < 0) {
1148 status = gdev->id;
1149 goto err_free_gdev;
1151 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1152 device_initialize(&gdev->dev);
1153 dev_set_drvdata(&gdev->dev, gdev);
1154 if (chip->parent && chip->parent->driver)
1155 gdev->owner = chip->parent->driver->owner;
1156 else if (chip->owner)
1157 /* TODO: remove chip->owner */
1158 gdev->owner = chip->owner;
1159 else
1160 gdev->owner = THIS_MODULE;
1162 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
1163 if (!gdev->descs) {
1164 status = -ENOMEM;
1165 goto err_free_gdev;
1168 if (chip->ngpio == 0) {
1169 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
1170 status = -EINVAL;
1171 goto err_free_descs;
1174 if (chip->label)
1175 gdev->label = kstrdup(chip->label, GFP_KERNEL);
1176 else
1177 gdev->label = kstrdup("unknown", GFP_KERNEL);
1178 if (!gdev->label) {
1179 status = -ENOMEM;
1180 goto err_free_descs;
1183 gdev->ngpio = chip->ngpio;
1184 gdev->data = data;
1186 spin_lock_irqsave(&gpio_lock, flags);
1189 * TODO: this allocates a Linux GPIO number base in the global
1190 * GPIO numberspace for this chip. In the long run we want to
1191 * get *rid* of this numberspace and use only descriptors, but
1192 * it may be a pipe dream. It will not happen before we get rid
1193 * of the sysfs interface anyways.
1195 if (base < 0) {
1196 base = gpiochip_find_base(chip->ngpio);
1197 if (base < 0) {
1198 status = base;
1199 spin_unlock_irqrestore(&gpio_lock, flags);
1200 goto err_free_label;
1203 * TODO: it should not be necessary to reflect the assigned
1204 * base outside of the GPIO subsystem. Go over drivers and
1205 * see if anyone makes use of this, else drop this and assign
1206 * a poison instead.
1208 chip->base = base;
1210 gdev->base = base;
1212 status = gpiodev_add_to_list(gdev);
1213 if (status) {
1214 spin_unlock_irqrestore(&gpio_lock, flags);
1215 goto err_free_label;
1218 spin_unlock_irqrestore(&gpio_lock, flags);
1220 for (i = 0; i < chip->ngpio; i++) {
1221 struct gpio_desc *desc = &gdev->descs[i];
1223 desc->gdev = gdev;
1225 * REVISIT: most hardware initializes GPIOs as inputs
1226 * (often with pullups enabled) so power usage is
1227 * minimized. Linux code should set the gpio direction
1228 * first thing; but until it does, and in case
1229 * chip->get_direction is not set, we may expose the
1230 * wrong direction in sysfs.
1233 if (chip->get_direction) {
1235 * If we have .get_direction, set up the initial
1236 * direction flag from the hardware.
1238 int dir = chip->get_direction(chip, i);
1240 if (!dir)
1241 set_bit(FLAG_IS_OUT, &desc->flags);
1242 } else if (!chip->direction_input) {
1244 * If the chip lacks the .direction_input callback
1245 * we logically assume all lines are outputs.
1247 set_bit(FLAG_IS_OUT, &desc->flags);
1251 #ifdef CONFIG_PINCTRL
1252 INIT_LIST_HEAD(&gdev->pin_ranges);
1253 #endif
1255 status = gpiochip_set_desc_names(chip);
1256 if (status)
1257 goto err_remove_from_list;
1259 status = gpiochip_irqchip_init_valid_mask(chip);
1260 if (status)
1261 goto err_remove_from_list;
1263 status = of_gpiochip_add(chip);
1264 if (status)
1265 goto err_remove_chip;
1267 acpi_gpiochip_add(chip);
1270 * By first adding the chardev, and then adding the device,
1271 * we get a device node entry in sysfs under
1272 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1273 * coldplug of device nodes and other udev business.
1274 * We can do this only if gpiolib has been initialized.
1275 * Otherwise, defer until later.
1277 if (gpiolib_initialized) {
1278 status = gpiochip_setup_dev(gdev);
1279 if (status)
1280 goto err_remove_chip;
1282 return 0;
1284 err_remove_chip:
1285 acpi_gpiochip_remove(chip);
1286 gpiochip_free_hogs(chip);
1287 of_gpiochip_remove(chip);
1288 gpiochip_irqchip_free_valid_mask(chip);
1289 err_remove_from_list:
1290 spin_lock_irqsave(&gpio_lock, flags);
1291 list_del(&gdev->list);
1292 spin_unlock_irqrestore(&gpio_lock, flags);
1293 err_free_label:
1294 kfree(gdev->label);
1295 err_free_descs:
1296 kfree(gdev->descs);
1297 err_free_gdev:
1298 ida_simple_remove(&gpio_ida, gdev->id);
1299 /* failures here can mean systems won't boot... */
1300 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
1301 gdev->base, gdev->base + gdev->ngpio - 1,
1302 chip->label ? : "generic");
1303 kfree(gdev);
1304 return status;
1306 EXPORT_SYMBOL_GPL(gpiochip_add_data);
1309 * gpiochip_get_data() - get per-subdriver data for the chip
1310 * @chip: GPIO chip
1312 * Returns:
1313 * The per-subdriver data for the chip.
1315 void *gpiochip_get_data(struct gpio_chip *chip)
1317 return chip->gpiodev->data;
1319 EXPORT_SYMBOL_GPL(gpiochip_get_data);
1322 * gpiochip_remove() - unregister a gpio_chip
1323 * @chip: the chip to unregister
1325 * A gpio_chip with any GPIOs still requested may not be removed.
1327 void gpiochip_remove(struct gpio_chip *chip)
1329 struct gpio_device *gdev = chip->gpiodev;
1330 struct gpio_desc *desc;
1331 unsigned long flags;
1332 unsigned i;
1333 bool requested = false;
1335 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
1336 gpiochip_sysfs_unregister(gdev);
1337 gpiochip_free_hogs(chip);
1338 /* Numb the device, cancelling all outstanding operations */
1339 gdev->chip = NULL;
1340 gpiochip_irqchip_remove(chip);
1341 acpi_gpiochip_remove(chip);
1342 gpiochip_remove_pin_ranges(chip);
1343 of_gpiochip_remove(chip);
1345 * We accept no more calls into the driver from this point, so
1346 * NULL the driver data pointer
1348 gdev->data = NULL;
1350 spin_lock_irqsave(&gpio_lock, flags);
1351 for (i = 0; i < gdev->ngpio; i++) {
1352 desc = &gdev->descs[i];
1353 if (test_bit(FLAG_REQUESTED, &desc->flags))
1354 requested = true;
1356 spin_unlock_irqrestore(&gpio_lock, flags);
1358 if (requested)
1359 dev_crit(&gdev->dev,
1360 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
1363 * The gpiochip side puts its use of the device to rest here:
1364 * if there are no userspace clients, the chardev and device will
1365 * be removed, else it will be dangling until the last user is
1366 * gone.
1368 cdev_device_del(&gdev->chrdev, &gdev->dev);
1369 put_device(&gdev->dev);
1371 EXPORT_SYMBOL_GPL(gpiochip_remove);
1373 static void devm_gpio_chip_release(struct device *dev, void *res)
1375 struct gpio_chip *chip = *(struct gpio_chip **)res;
1377 gpiochip_remove(chip);
1380 static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1383 struct gpio_chip **r = res;
1385 if (!r || !*r) {
1386 WARN_ON(!r || !*r);
1387 return 0;
1390 return *r == data;
1394 * devm_gpiochip_add_data() - Resource manager piochip_add_data()
1395 * @dev: the device pointer on which irq_chip belongs to.
1396 * @chip: the chip to register, with chip->base initialized
1397 * @data: driver-private data associated with this chip
1399 * Context: potentially before irqs will work
1401 * The gpio chip automatically be released when the device is unbound.
1403 * Returns:
1404 * A negative errno if the chip can't be registered, such as because the
1405 * chip->base is invalid or already associated with a different chip.
1406 * Otherwise it returns zero as a success code.
1408 int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1409 void *data)
1411 struct gpio_chip **ptr;
1412 int ret;
1414 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1415 GFP_KERNEL);
1416 if (!ptr)
1417 return -ENOMEM;
1419 ret = gpiochip_add_data(chip, data);
1420 if (ret < 0) {
1421 devres_free(ptr);
1422 return ret;
1425 *ptr = chip;
1426 devres_add(dev, ptr);
1428 return 0;
1430 EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1433 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1434 * @dev: device for which which resource was allocated
1435 * @chip: the chip to remove
1437 * A gpio_chip with any GPIOs still requested may not be removed.
1439 void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1441 int ret;
1443 ret = devres_release(dev, devm_gpio_chip_release,
1444 devm_gpio_chip_match, chip);
1445 WARN_ON(ret);
1447 EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1450 * gpiochip_find() - iterator for locating a specific gpio_chip
1451 * @data: data to pass to match function
1452 * @match: Callback function to check gpio_chip
1454 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1455 * determined by a user supplied @match callback. The callback should return
1456 * 0 if the device doesn't match and non-zero if it does. If the callback is
1457 * non-zero, this function will return to the caller and not iterate over any
1458 * more gpio_chips.
1460 struct gpio_chip *gpiochip_find(void *data,
1461 int (*match)(struct gpio_chip *chip,
1462 void *data))
1464 struct gpio_device *gdev;
1465 struct gpio_chip *chip = NULL;
1466 unsigned long flags;
1468 spin_lock_irqsave(&gpio_lock, flags);
1469 list_for_each_entry(gdev, &gpio_devices, list)
1470 if (gdev->chip && match(gdev->chip, data)) {
1471 chip = gdev->chip;
1472 break;
1475 spin_unlock_irqrestore(&gpio_lock, flags);
1477 return chip;
1479 EXPORT_SYMBOL_GPL(gpiochip_find);
1481 static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1483 const char *name = data;
1485 return !strcmp(chip->label, name);
1488 static struct gpio_chip *find_chip_by_name(const char *name)
1490 return gpiochip_find((void *)name, gpiochip_match_name);
1493 #ifdef CONFIG_GPIOLIB_IRQCHIP
1496 * The following is irqchip helper code for gpiochips.
1499 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1501 if (!gpiochip->irq_need_valid_mask)
1502 return 0;
1504 gpiochip->irq_valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio),
1505 sizeof(long), GFP_KERNEL);
1506 if (!gpiochip->irq_valid_mask)
1507 return -ENOMEM;
1509 /* Assume by default all GPIOs are valid */
1510 bitmap_fill(gpiochip->irq_valid_mask, gpiochip->ngpio);
1512 return 0;
1515 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1517 kfree(gpiochip->irq_valid_mask);
1518 gpiochip->irq_valid_mask = NULL;
1521 static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1522 unsigned int offset)
1524 /* No mask means all valid */
1525 if (likely(!gpiochip->irq_valid_mask))
1526 return true;
1527 return test_bit(offset, gpiochip->irq_valid_mask);
1531 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
1532 * @gpiochip: the gpiochip to set the irqchip chain to
1533 * @irqchip: the irqchip to chain to the gpiochip
1534 * @parent_irq: the irq number corresponding to the parent IRQ for this
1535 * chained irqchip
1536 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1537 * coming out of the gpiochip. If the interrupt is nested rather than
1538 * cascaded, pass NULL in this handler argument
1540 static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip,
1541 struct irq_chip *irqchip,
1542 unsigned int parent_irq,
1543 irq_flow_handler_t parent_handler)
1545 unsigned int offset;
1547 if (!gpiochip->irqdomain) {
1548 chip_err(gpiochip, "called %s before setting up irqchip\n",
1549 __func__);
1550 return;
1553 if (parent_handler) {
1554 if (gpiochip->can_sleep) {
1555 chip_err(gpiochip,
1556 "you cannot have chained interrupts on a "
1557 "chip that may sleep\n");
1558 return;
1561 * The parent irqchip is already using the chip_data for this
1562 * irqchip, so our callbacks simply use the handler_data.
1564 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1565 gpiochip);
1567 gpiochip->irq_chained_parent = parent_irq;
1570 /* Set the parent IRQ for all affected IRQs */
1571 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1572 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1573 continue;
1574 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
1575 parent_irq);
1580 * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
1581 * @gpiochip: the gpiochip to set the irqchip chain to
1582 * @irqchip: the irqchip to chain to the gpiochip
1583 * @parent_irq: the irq number corresponding to the parent IRQ for this
1584 * chained irqchip
1585 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1586 * coming out of the gpiochip. If the interrupt is nested rather than
1587 * cascaded, pass NULL in this handler argument
1589 void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1590 struct irq_chip *irqchip,
1591 unsigned int parent_irq,
1592 irq_flow_handler_t parent_handler)
1594 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1595 parent_handler);
1597 EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1600 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1601 * @gpiochip: the gpiochip to set the irqchip nested handler to
1602 * @irqchip: the irqchip to nest to the gpiochip
1603 * @parent_irq: the irq number corresponding to the parent IRQ for this
1604 * nested irqchip
1606 void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1607 struct irq_chip *irqchip,
1608 unsigned int parent_irq)
1610 if (!gpiochip->irq_nested) {
1611 chip_err(gpiochip, "tried to nest a chained gpiochip\n");
1612 return;
1614 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1615 NULL);
1617 EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1620 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1621 * @d: the irqdomain used by this irqchip
1622 * @irq: the global irq number used by this GPIO irqchip irq
1623 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1625 * This function will set up the mapping for a certain IRQ line on a
1626 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1627 * stored inside the gpiochip.
1629 static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1630 irq_hw_number_t hwirq)
1632 struct gpio_chip *chip = d->host_data;
1634 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
1635 return -ENXIO;
1637 irq_set_chip_data(irq, chip);
1639 * This lock class tells lockdep that GPIO irqs are in a different
1640 * category than their parents, so it won't report false recursion.
1642 irq_set_lockdep_class(irq, chip->lock_key);
1643 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
1644 /* Chips that use nested thread handlers have them marked */
1645 if (chip->irq_nested)
1646 irq_set_nested_thread(irq, 1);
1647 irq_set_noprobe(irq);
1650 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1651 * is passed as default type.
1653 if (chip->irq_default_type != IRQ_TYPE_NONE)
1654 irq_set_irq_type(irq, chip->irq_default_type);
1656 return 0;
1659 static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1661 struct gpio_chip *chip = d->host_data;
1663 if (chip->irq_nested)
1664 irq_set_nested_thread(irq, 0);
1665 irq_set_chip_and_handler(irq, NULL, NULL);
1666 irq_set_chip_data(irq, NULL);
1669 static const struct irq_domain_ops gpiochip_domain_ops = {
1670 .map = gpiochip_irq_map,
1671 .unmap = gpiochip_irq_unmap,
1672 /* Virtually all GPIO irqchips are twocell:ed */
1673 .xlate = irq_domain_xlate_twocell,
1676 static int gpiochip_irq_reqres(struct irq_data *d)
1678 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1680 if (!try_module_get(chip->gpiodev->owner))
1681 return -ENODEV;
1683 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
1684 chip_err(chip,
1685 "unable to lock HW IRQ %lu for IRQ\n",
1686 d->hwirq);
1687 module_put(chip->gpiodev->owner);
1688 return -EINVAL;
1690 return 0;
1693 static void gpiochip_irq_relres(struct irq_data *d)
1695 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1697 gpiochip_unlock_as_irq(chip, d->hwirq);
1698 module_put(chip->gpiodev->owner);
1701 static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1703 if (!gpiochip_irqchip_irq_valid(chip, offset))
1704 return -ENXIO;
1705 return irq_create_mapping(chip->irqdomain, offset);
1709 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1710 * @gpiochip: the gpiochip to remove the irqchip from
1712 * This is called only from gpiochip_remove()
1714 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1716 unsigned int offset;
1718 acpi_gpiochip_free_interrupts(gpiochip);
1720 if (gpiochip->irq_chained_parent) {
1721 irq_set_chained_handler(gpiochip->irq_chained_parent, NULL);
1722 irq_set_handler_data(gpiochip->irq_chained_parent, NULL);
1725 /* Remove all IRQ mappings and delete the domain */
1726 if (gpiochip->irqdomain) {
1727 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1728 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1729 continue;
1730 irq_dispose_mapping(
1731 irq_find_mapping(gpiochip->irqdomain, offset));
1733 irq_domain_remove(gpiochip->irqdomain);
1736 if (gpiochip->irqchip) {
1737 gpiochip->irqchip->irq_request_resources = NULL;
1738 gpiochip->irqchip->irq_release_resources = NULL;
1739 gpiochip->irqchip = NULL;
1742 gpiochip_irqchip_free_valid_mask(gpiochip);
1746 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
1747 * @gpiochip: the gpiochip to add the irqchip to
1748 * @irqchip: the irqchip to add to the gpiochip
1749 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1750 * allocate gpiochip irqs from
1751 * @handler: the irq handler to use (often a predefined irq core function)
1752 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1753 * to have the core avoid setting up any default type in the hardware.
1754 * @nested: whether this is a nested irqchip calling handle_nested_irq()
1755 * in its IRQ handler
1756 * @lock_key: lockdep class
1758 * This function closely associates a certain irqchip with a certain
1759 * gpiochip, providing an irq domain to translate the local IRQs to
1760 * global irqs in the gpiolib core, and making sure that the gpiochip
1761 * is passed as chip data to all related functions. Driver callbacks
1762 * need to use gpiochip_get_data() to get their local state containers back
1763 * from the gpiochip passed as chip data. An irqdomain will be stored
1764 * in the gpiochip that shall be used by the driver to handle IRQ number
1765 * translation. The gpiochip will need to be initialized and registered
1766 * before calling this function.
1768 * This function will handle two cell:ed simple IRQs and assumes all
1769 * the pins on the gpiochip can generate a unique IRQ. Everything else
1770 * need to be open coded.
1772 int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
1773 struct irq_chip *irqchip,
1774 unsigned int first_irq,
1775 irq_flow_handler_t handler,
1776 unsigned int type,
1777 bool nested,
1778 struct lock_class_key *lock_key)
1780 struct device_node *of_node;
1782 if (!gpiochip || !irqchip)
1783 return -EINVAL;
1785 if (!gpiochip->parent) {
1786 pr_err("missing gpiochip .dev parent pointer\n");
1787 return -EINVAL;
1789 gpiochip->irq_nested = nested;
1790 of_node = gpiochip->parent->of_node;
1791 #ifdef CONFIG_OF_GPIO
1793 * If the gpiochip has an assigned OF node this takes precedence
1794 * FIXME: get rid of this and use gpiochip->parent->of_node
1795 * everywhere
1797 if (gpiochip->of_node)
1798 of_node = gpiochip->of_node;
1799 #endif
1801 * Specifying a default trigger is a terrible idea if DT or ACPI is
1802 * used to configure the interrupts, as you may end-up with
1803 * conflicting triggers. Tell the user, and reset to NONE.
1805 if (WARN(of_node && type != IRQ_TYPE_NONE,
1806 "%pOF: Ignoring %d default trigger\n", of_node, type))
1807 type = IRQ_TYPE_NONE;
1808 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1809 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1810 "Ignoring %d default trigger\n", type);
1811 type = IRQ_TYPE_NONE;
1814 gpiochip->irqchip = irqchip;
1815 gpiochip->irq_handler = handler;
1816 gpiochip->irq_default_type = type;
1817 gpiochip->to_irq = gpiochip_to_irq;
1818 gpiochip->lock_key = lock_key;
1819 gpiochip->irqdomain = irq_domain_add_simple(of_node,
1820 gpiochip->ngpio, first_irq,
1821 &gpiochip_domain_ops, gpiochip);
1822 if (!gpiochip->irqdomain) {
1823 gpiochip->irqchip = NULL;
1824 return -EINVAL;
1828 * It is possible for a driver to override this, but only if the
1829 * alternative functions are both implemented.
1831 if (!irqchip->irq_request_resources &&
1832 !irqchip->irq_release_resources) {
1833 irqchip->irq_request_resources = gpiochip_irq_reqres;
1834 irqchip->irq_release_resources = gpiochip_irq_relres;
1837 acpi_gpiochip_request_interrupts(gpiochip);
1839 return 0;
1841 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
1843 #else /* CONFIG_GPIOLIB_IRQCHIP */
1845 static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
1846 static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1848 return 0;
1850 static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1853 #endif /* CONFIG_GPIOLIB_IRQCHIP */
1856 * gpiochip_generic_request() - request the gpio function for a pin
1857 * @chip: the gpiochip owning the GPIO
1858 * @offset: the offset of the GPIO to request for GPIO function
1860 int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1862 return pinctrl_request_gpio(chip->gpiodev->base + offset);
1864 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1867 * gpiochip_generic_free() - free the gpio function from a pin
1868 * @chip: the gpiochip to request the gpio function for
1869 * @offset: the offset of the GPIO to free from GPIO function
1871 void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1873 pinctrl_free_gpio(chip->gpiodev->base + offset);
1875 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1878 * gpiochip_generic_config() - apply configuration for a pin
1879 * @chip: the gpiochip owning the GPIO
1880 * @offset: the offset of the GPIO to apply the configuration
1881 * @config: the configuration to be applied
1883 int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
1884 unsigned long config)
1886 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
1888 EXPORT_SYMBOL_GPL(gpiochip_generic_config);
1890 #ifdef CONFIG_PINCTRL
1893 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1894 * @chip: the gpiochip to add the range for
1895 * @pctldev: the pin controller to map to
1896 * @gpio_offset: the start offset in the current gpio_chip number space
1897 * @pin_group: name of the pin group inside the pin controller
1899 int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1900 struct pinctrl_dev *pctldev,
1901 unsigned int gpio_offset, const char *pin_group)
1903 struct gpio_pin_range *pin_range;
1904 struct gpio_device *gdev = chip->gpiodev;
1905 int ret;
1907 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1908 if (!pin_range) {
1909 chip_err(chip, "failed to allocate pin ranges\n");
1910 return -ENOMEM;
1913 /* Use local offset as range ID */
1914 pin_range->range.id = gpio_offset;
1915 pin_range->range.gc = chip;
1916 pin_range->range.name = chip->label;
1917 pin_range->range.base = gdev->base + gpio_offset;
1918 pin_range->pctldev = pctldev;
1920 ret = pinctrl_get_group_pins(pctldev, pin_group,
1921 &pin_range->range.pins,
1922 &pin_range->range.npins);
1923 if (ret < 0) {
1924 kfree(pin_range);
1925 return ret;
1928 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1930 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1931 gpio_offset, gpio_offset + pin_range->range.npins - 1,
1932 pinctrl_dev_get_devname(pctldev), pin_group);
1934 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1936 return 0;
1938 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1941 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1942 * @chip: the gpiochip to add the range for
1943 * @pinctl_name: the dev_name() of the pin controller to map to
1944 * @gpio_offset: the start offset in the current gpio_chip number space
1945 * @pin_offset: the start offset in the pin controller number space
1946 * @npins: the number of pins from the offset of each pin space (GPIO and
1947 * pin controller) to accumulate in this range
1949 * Returns:
1950 * 0 on success, or a negative error-code on failure.
1952 int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
1953 unsigned int gpio_offset, unsigned int pin_offset,
1954 unsigned int npins)
1956 struct gpio_pin_range *pin_range;
1957 struct gpio_device *gdev = chip->gpiodev;
1958 int ret;
1960 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1961 if (!pin_range) {
1962 chip_err(chip, "failed to allocate pin ranges\n");
1963 return -ENOMEM;
1966 /* Use local offset as range ID */
1967 pin_range->range.id = gpio_offset;
1968 pin_range->range.gc = chip;
1969 pin_range->range.name = chip->label;
1970 pin_range->range.base = gdev->base + gpio_offset;
1971 pin_range->range.pin_base = pin_offset;
1972 pin_range->range.npins = npins;
1973 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
1974 &pin_range->range);
1975 if (IS_ERR(pin_range->pctldev)) {
1976 ret = PTR_ERR(pin_range->pctldev);
1977 chip_err(chip, "could not create pin range\n");
1978 kfree(pin_range);
1979 return ret;
1981 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1982 gpio_offset, gpio_offset + npins - 1,
1983 pinctl_name,
1984 pin_offset, pin_offset + npins - 1);
1986 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1988 return 0;
1990 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
1993 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1994 * @chip: the chip to remove all the mappings for
1996 void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1998 struct gpio_pin_range *pin_range, *tmp;
1999 struct gpio_device *gdev = chip->gpiodev;
2001 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
2002 list_del(&pin_range->node);
2003 pinctrl_remove_gpio_range(pin_range->pctldev,
2004 &pin_range->range);
2005 kfree(pin_range);
2008 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2010 #endif /* CONFIG_PINCTRL */
2012 /* These "optional" allocation calls help prevent drivers from stomping
2013 * on each other, and help provide better diagnostics in debugfs.
2014 * They're called even less than the "set direction" calls.
2016 static int __gpiod_request(struct gpio_desc *desc, const char *label)
2018 struct gpio_chip *chip = desc->gdev->chip;
2019 int status;
2020 unsigned long flags;
2022 spin_lock_irqsave(&gpio_lock, flags);
2024 /* NOTE: gpio_request() can be called in early boot,
2025 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
2028 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2029 desc_set_label(desc, label ? : "?");
2030 status = 0;
2031 } else {
2032 status = -EBUSY;
2033 goto done;
2036 if (chip->request) {
2037 /* chip->request may sleep */
2038 spin_unlock_irqrestore(&gpio_lock, flags);
2039 status = chip->request(chip, gpio_chip_hwgpio(desc));
2040 spin_lock_irqsave(&gpio_lock, flags);
2042 if (status < 0) {
2043 desc_set_label(desc, NULL);
2044 clear_bit(FLAG_REQUESTED, &desc->flags);
2045 goto done;
2048 if (chip->get_direction) {
2049 /* chip->get_direction may sleep */
2050 spin_unlock_irqrestore(&gpio_lock, flags);
2051 gpiod_get_direction(desc);
2052 spin_lock_irqsave(&gpio_lock, flags);
2054 done:
2055 spin_unlock_irqrestore(&gpio_lock, flags);
2056 return status;
2060 * This descriptor validation needs to be inserted verbatim into each
2061 * function taking a descriptor, so we need to use a preprocessor
2062 * macro to avoid endless duplication. If the desc is NULL it is an
2063 * optional GPIO and calls should just bail out.
2065 #define VALIDATE_DESC(desc) do { \
2066 if (!desc) \
2067 return 0; \
2068 if (IS_ERR(desc)) { \
2069 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2070 return PTR_ERR(desc); \
2072 if (!desc->gdev) { \
2073 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
2074 return -EINVAL; \
2076 if ( !desc->gdev->chip ) { \
2077 dev_warn(&desc->gdev->dev, \
2078 "%s: backing chip is gone\n", __func__); \
2079 return 0; \
2080 } } while (0)
2082 #define VALIDATE_DESC_VOID(desc) do { \
2083 if (!desc) \
2084 return; \
2085 if (IS_ERR(desc)) { \
2086 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2087 return; \
2089 if (!desc->gdev) { \
2090 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
2091 return; \
2093 if (!desc->gdev->chip) { \
2094 dev_warn(&desc->gdev->dev, \
2095 "%s: backing chip is gone\n", __func__); \
2096 return; \
2097 } } while (0)
2100 int gpiod_request(struct gpio_desc *desc, const char *label)
2102 int status = -EPROBE_DEFER;
2103 struct gpio_device *gdev;
2105 VALIDATE_DESC(desc);
2106 gdev = desc->gdev;
2108 if (try_module_get(gdev->owner)) {
2109 status = __gpiod_request(desc, label);
2110 if (status < 0)
2111 module_put(gdev->owner);
2112 else
2113 get_device(&gdev->dev);
2116 if (status)
2117 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
2119 return status;
2122 static bool __gpiod_free(struct gpio_desc *desc)
2124 bool ret = false;
2125 unsigned long flags;
2126 struct gpio_chip *chip;
2128 might_sleep();
2130 gpiod_unexport(desc);
2132 spin_lock_irqsave(&gpio_lock, flags);
2134 chip = desc->gdev->chip;
2135 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2136 if (chip->free) {
2137 spin_unlock_irqrestore(&gpio_lock, flags);
2138 might_sleep_if(chip->can_sleep);
2139 chip->free(chip, gpio_chip_hwgpio(desc));
2140 spin_lock_irqsave(&gpio_lock, flags);
2142 desc_set_label(desc, NULL);
2143 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
2144 clear_bit(FLAG_REQUESTED, &desc->flags);
2145 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
2146 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
2147 clear_bit(FLAG_IS_HOGGED, &desc->flags);
2148 ret = true;
2151 spin_unlock_irqrestore(&gpio_lock, flags);
2152 return ret;
2155 void gpiod_free(struct gpio_desc *desc)
2157 if (desc && desc->gdev && __gpiod_free(desc)) {
2158 module_put(desc->gdev->owner);
2159 put_device(&desc->gdev->dev);
2160 } else {
2161 WARN_ON(extra_checks);
2166 * gpiochip_is_requested - return string iff signal was requested
2167 * @chip: controller managing the signal
2168 * @offset: of signal within controller's 0..(ngpio - 1) range
2170 * Returns NULL if the GPIO is not currently requested, else a string.
2171 * The string returned is the label passed to gpio_request(); if none has been
2172 * passed it is a meaningless, non-NULL constant.
2174 * This function is for use by GPIO controller drivers. The label can
2175 * help with diagnostics, and knowing that the signal is used as a GPIO
2176 * can help avoid accidentally multiplexing it to another controller.
2178 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2180 struct gpio_desc *desc;
2182 if (offset >= chip->ngpio)
2183 return NULL;
2185 desc = &chip->gpiodev->descs[offset];
2187 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
2188 return NULL;
2189 return desc->label;
2191 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2194 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2195 * @chip: GPIO chip
2196 * @hwnum: hardware number of the GPIO for which to request the descriptor
2197 * @label: label for the GPIO
2199 * Function allows GPIO chip drivers to request and use their own GPIO
2200 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2201 * function will not increase reference count of the GPIO chip module. This
2202 * allows the GPIO chip module to be unloaded as needed (we assume that the
2203 * GPIO chip driver handles freeing the GPIOs it has requested).
2205 * Returns:
2206 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2207 * code on failure.
2209 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2210 const char *label)
2212 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2213 int err;
2215 if (IS_ERR(desc)) {
2216 chip_err(chip, "failed to get GPIO descriptor\n");
2217 return desc;
2220 err = __gpiod_request(desc, label);
2221 if (err < 0)
2222 return ERR_PTR(err);
2224 return desc;
2226 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
2229 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2230 * @desc: GPIO descriptor to free
2232 * Function frees the given GPIO requested previously with
2233 * gpiochip_request_own_desc().
2235 void gpiochip_free_own_desc(struct gpio_desc *desc)
2237 if (desc)
2238 __gpiod_free(desc);
2240 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
2243 * Drivers MUST set GPIO direction before making get/set calls. In
2244 * some cases this is done in early boot, before IRQs are enabled.
2246 * As a rule these aren't called more than once (except for drivers
2247 * using the open-drain emulation idiom) so these are natural places
2248 * to accumulate extra debugging checks. Note that we can't (yet)
2249 * rely on gpio_request() having been called beforehand.
2253 * gpiod_direction_input - set the GPIO direction to input
2254 * @desc: GPIO to set to input
2256 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2257 * be called safely on it.
2259 * Return 0 in case of success, else an error code.
2261 int gpiod_direction_input(struct gpio_desc *desc)
2263 struct gpio_chip *chip;
2264 int status = -EINVAL;
2266 VALIDATE_DESC(desc);
2267 chip = desc->gdev->chip;
2269 if (!chip->get || !chip->direction_input) {
2270 gpiod_warn(desc,
2271 "%s: missing get() or direction_input() operations\n",
2272 __func__);
2273 return -EIO;
2276 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
2277 if (status == 0)
2278 clear_bit(FLAG_IS_OUT, &desc->flags);
2280 trace_gpio_direction(desc_to_gpio(desc), 1, status);
2282 return status;
2284 EXPORT_SYMBOL_GPL(gpiod_direction_input);
2286 static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset,
2287 enum pin_config_param mode)
2289 unsigned long config = { PIN_CONF_PACKED(mode, 0) };
2291 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2294 static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2296 struct gpio_chip *gc = desc->gdev->chip;
2297 int val = !!value;
2298 int ret;
2300 /* GPIOs used for IRQs shall not be set as output */
2301 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2302 gpiod_err(desc,
2303 "%s: tried to set a GPIO tied to an IRQ as output\n",
2304 __func__);
2305 return -EIO;
2308 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2309 /* First see if we can enable open drain in hardware */
2310 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2311 PIN_CONFIG_DRIVE_OPEN_DRAIN);
2312 if (!ret)
2313 goto set_output_value;
2314 /* Emulate open drain by not actively driving the line high */
2315 if (val)
2316 return gpiod_direction_input(desc);
2318 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2319 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2320 PIN_CONFIG_DRIVE_OPEN_SOURCE);
2321 if (!ret)
2322 goto set_output_value;
2323 /* Emulate open source by not actively driving the line low */
2324 if (!val)
2325 return gpiod_direction_input(desc);
2326 } else {
2327 gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2328 PIN_CONFIG_DRIVE_PUSH_PULL);
2331 set_output_value:
2332 if (!gc->set || !gc->direction_output) {
2333 gpiod_warn(desc,
2334 "%s: missing set() or direction_output() operations\n",
2335 __func__);
2336 return -EIO;
2339 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
2340 if (!ret)
2341 set_bit(FLAG_IS_OUT, &desc->flags);
2342 trace_gpio_value(desc_to_gpio(desc), 0, val);
2343 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2344 return ret;
2348 * gpiod_direction_output_raw - set the GPIO direction to output
2349 * @desc: GPIO to set to output
2350 * @value: initial output value of the GPIO
2352 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2353 * be called safely on it. The initial value of the output must be specified
2354 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2356 * Return 0 in case of success, else an error code.
2358 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2360 VALIDATE_DESC(desc);
2361 return _gpiod_direction_output_raw(desc, value);
2363 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2366 * gpiod_direction_output - set the GPIO direction to output
2367 * @desc: GPIO to set to output
2368 * @value: initial output value of the GPIO
2370 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2371 * be called safely on it. The initial value of the output must be specified
2372 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2373 * account.
2375 * Return 0 in case of success, else an error code.
2377 int gpiod_direction_output(struct gpio_desc *desc, int value)
2379 VALIDATE_DESC(desc);
2380 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2381 value = !value;
2382 else
2383 value = !!value;
2384 return _gpiod_direction_output_raw(desc, value);
2386 EXPORT_SYMBOL_GPL(gpiod_direction_output);
2389 * gpiod_set_debounce - sets @debounce time for a GPIO
2390 * @desc: descriptor of the GPIO for which to set debounce time
2391 * @debounce: debounce time in microseconds
2393 * Returns:
2394 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2395 * debounce time.
2397 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
2399 struct gpio_chip *chip;
2400 unsigned long config;
2402 VALIDATE_DESC(desc);
2403 chip = desc->gdev->chip;
2404 if (!chip->set || !chip->set_config) {
2405 gpiod_dbg(desc,
2406 "%s: missing set() or set_config() operations\n",
2407 __func__);
2408 return -ENOTSUPP;
2411 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2412 return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
2414 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
2417 * gpiod_is_active_low - test whether a GPIO is active-low or not
2418 * @desc: the gpio descriptor to test
2420 * Returns 1 if the GPIO is active-low, 0 otherwise.
2422 int gpiod_is_active_low(const struct gpio_desc *desc)
2424 VALIDATE_DESC(desc);
2425 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
2427 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
2429 /* I/O calls are only valid after configuration completed; the relevant
2430 * "is this a valid GPIO" error checks should already have been done.
2432 * "Get" operations are often inlinable as reading a pin value register,
2433 * and masking the relevant bit in that register.
2435 * When "set" operations are inlinable, they involve writing that mask to
2436 * one register to set a low value, or a different register to set it high.
2437 * Otherwise locking is needed, so there may be little value to inlining.
2439 *------------------------------------------------------------------------
2441 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2442 * have requested the GPIO. That can include implicit requesting by
2443 * a direction setting call. Marking a gpio as requested locks its chip
2444 * in memory, guaranteeing that these table lookups need no more locking
2445 * and that gpiochip_remove() will fail.
2447 * REVISIT when debugging, consider adding some instrumentation to ensure
2448 * that the GPIO was actually requested.
2451 static int _gpiod_get_raw_value(const struct gpio_desc *desc)
2453 struct gpio_chip *chip;
2454 int offset;
2455 int value;
2457 chip = desc->gdev->chip;
2458 offset = gpio_chip_hwgpio(desc);
2459 value = chip->get ? chip->get(chip, offset) : -EIO;
2460 value = value < 0 ? value : !!value;
2461 trace_gpio_value(desc_to_gpio(desc), 1, value);
2462 return value;
2466 * gpiod_get_raw_value() - return a gpio's raw value
2467 * @desc: gpio whose value will be returned
2469 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2470 * its ACTIVE_LOW status, or negative errno on failure.
2472 * This function should be called from contexts where we cannot sleep, and will
2473 * complain if the GPIO chip functions potentially sleep.
2475 int gpiod_get_raw_value(const struct gpio_desc *desc)
2477 VALIDATE_DESC(desc);
2478 /* Should be using gpio_get_value_cansleep() */
2479 WARN_ON(desc->gdev->chip->can_sleep);
2480 return _gpiod_get_raw_value(desc);
2482 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
2485 * gpiod_get_value() - return a gpio's value
2486 * @desc: gpio whose value will be returned
2488 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2489 * account, or negative errno on failure.
2491 * This function should be called from contexts where we cannot sleep, and will
2492 * complain if the GPIO chip functions potentially sleep.
2494 int gpiod_get_value(const struct gpio_desc *desc)
2496 int value;
2498 VALIDATE_DESC(desc);
2499 /* Should be using gpio_get_value_cansleep() */
2500 WARN_ON(desc->gdev->chip->can_sleep);
2502 value = _gpiod_get_raw_value(desc);
2503 if (value < 0)
2504 return value;
2506 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2507 value = !value;
2509 return value;
2511 EXPORT_SYMBOL_GPL(gpiod_get_value);
2514 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
2515 * @desc: gpio descriptor whose state need to be set.
2516 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2518 static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
2520 int err = 0;
2521 struct gpio_chip *chip = desc->gdev->chip;
2522 int offset = gpio_chip_hwgpio(desc);
2524 if (value) {
2525 err = chip->direction_input(chip, offset);
2526 if (!err)
2527 clear_bit(FLAG_IS_OUT, &desc->flags);
2528 } else {
2529 err = chip->direction_output(chip, offset, 0);
2530 if (!err)
2531 set_bit(FLAG_IS_OUT, &desc->flags);
2533 trace_gpio_direction(desc_to_gpio(desc), value, err);
2534 if (err < 0)
2535 gpiod_err(desc,
2536 "%s: Error in set_value for open drain err %d\n",
2537 __func__, err);
2541 * _gpio_set_open_source_value() - Set the open source gpio's value.
2542 * @desc: gpio descriptor whose state need to be set.
2543 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2545 static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
2547 int err = 0;
2548 struct gpio_chip *chip = desc->gdev->chip;
2549 int offset = gpio_chip_hwgpio(desc);
2551 if (value) {
2552 err = chip->direction_output(chip, offset, 1);
2553 if (!err)
2554 set_bit(FLAG_IS_OUT, &desc->flags);
2555 } else {
2556 err = chip->direction_input(chip, offset);
2557 if (!err)
2558 clear_bit(FLAG_IS_OUT, &desc->flags);
2560 trace_gpio_direction(desc_to_gpio(desc), !value, err);
2561 if (err < 0)
2562 gpiod_err(desc,
2563 "%s: Error in set_value for open source err %d\n",
2564 __func__, err);
2567 static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
2569 struct gpio_chip *chip;
2571 chip = desc->gdev->chip;
2572 trace_gpio_value(desc_to_gpio(desc), 0, value);
2573 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2574 _gpio_set_open_drain_value(desc, value);
2575 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2576 _gpio_set_open_source_value(desc, value);
2577 else
2578 chip->set(chip, gpio_chip_hwgpio(desc), value);
2582 * set multiple outputs on the same chip;
2583 * use the chip's set_multiple function if available;
2584 * otherwise set the outputs sequentially;
2585 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2586 * defines which outputs are to be changed
2587 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2588 * defines the values the outputs specified by mask are to be set to
2590 static void gpio_chip_set_multiple(struct gpio_chip *chip,
2591 unsigned long *mask, unsigned long *bits)
2593 if (chip->set_multiple) {
2594 chip->set_multiple(chip, mask, bits);
2595 } else {
2596 unsigned int i;
2598 /* set outputs if the corresponding mask bit is set */
2599 for_each_set_bit(i, mask, chip->ngpio)
2600 chip->set(chip, i, test_bit(i, bits));
2604 void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2605 unsigned int array_size,
2606 struct gpio_desc **desc_array,
2607 int *value_array)
2609 int i = 0;
2611 while (i < array_size) {
2612 struct gpio_chip *chip = desc_array[i]->gdev->chip;
2613 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2614 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2615 int count = 0;
2617 if (!can_sleep)
2618 WARN_ON(chip->can_sleep);
2620 memset(mask, 0, sizeof(mask));
2621 do {
2622 struct gpio_desc *desc = desc_array[i];
2623 int hwgpio = gpio_chip_hwgpio(desc);
2624 int value = value_array[i];
2626 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2627 value = !value;
2628 trace_gpio_value(desc_to_gpio(desc), 0, value);
2630 * collect all normal outputs belonging to the same chip
2631 * open drain and open source outputs are set individually
2633 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2634 _gpio_set_open_drain_value(desc, value);
2635 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2636 _gpio_set_open_source_value(desc, value);
2637 } else {
2638 __set_bit(hwgpio, mask);
2639 if (value)
2640 __set_bit(hwgpio, bits);
2641 else
2642 __clear_bit(hwgpio, bits);
2643 count++;
2645 i++;
2646 } while ((i < array_size) &&
2647 (desc_array[i]->gdev->chip == chip));
2648 /* push collected bits to outputs */
2649 if (count != 0)
2650 gpio_chip_set_multiple(chip, mask, bits);
2655 * gpiod_set_raw_value() - assign a gpio's raw value
2656 * @desc: gpio whose value will be assigned
2657 * @value: value to assign
2659 * Set the raw value of the GPIO, i.e. the value of its physical line without
2660 * regard for its ACTIVE_LOW status.
2662 * This function should be called from contexts where we cannot sleep, and will
2663 * complain if the GPIO chip functions potentially sleep.
2665 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
2667 VALIDATE_DESC_VOID(desc);
2668 /* Should be using gpiod_set_value_cansleep() */
2669 WARN_ON(desc->gdev->chip->can_sleep);
2670 _gpiod_set_raw_value(desc, value);
2672 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
2675 * gpiod_set_value() - assign a gpio's value
2676 * @desc: gpio whose value will be assigned
2677 * @value: value to assign
2679 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2680 * account
2682 * This function should be called from contexts where we cannot sleep, and will
2683 * complain if the GPIO chip functions potentially sleep.
2685 void gpiod_set_value(struct gpio_desc *desc, int value)
2687 VALIDATE_DESC_VOID(desc);
2688 /* Should be using gpiod_set_value_cansleep() */
2689 WARN_ON(desc->gdev->chip->can_sleep);
2690 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2691 value = !value;
2692 _gpiod_set_raw_value(desc, value);
2694 EXPORT_SYMBOL_GPL(gpiod_set_value);
2697 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
2698 * @array_size: number of elements in the descriptor / value arrays
2699 * @desc_array: array of GPIO descriptors whose values will be assigned
2700 * @value_array: array of values to assign
2702 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2703 * without regard for their ACTIVE_LOW status.
2705 * This function should be called from contexts where we cannot sleep, and will
2706 * complain if the GPIO chip functions potentially sleep.
2708 void gpiod_set_raw_array_value(unsigned int array_size,
2709 struct gpio_desc **desc_array, int *value_array)
2711 if (!desc_array)
2712 return;
2713 gpiod_set_array_value_complex(true, false, array_size, desc_array,
2714 value_array);
2716 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
2719 * gpiod_set_array_value() - assign values to an array of GPIOs
2720 * @array_size: number of elements in the descriptor / value arrays
2721 * @desc_array: array of GPIO descriptors whose values will be assigned
2722 * @value_array: array of values to assign
2724 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2725 * into account.
2727 * This function should be called from contexts where we cannot sleep, and will
2728 * complain if the GPIO chip functions potentially sleep.
2730 void gpiod_set_array_value(unsigned int array_size,
2731 struct gpio_desc **desc_array, int *value_array)
2733 if (!desc_array)
2734 return;
2735 gpiod_set_array_value_complex(false, false, array_size, desc_array,
2736 value_array);
2738 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
2741 * gpiod_cansleep() - report whether gpio value access may sleep
2742 * @desc: gpio to check
2745 int gpiod_cansleep(const struct gpio_desc *desc)
2747 VALIDATE_DESC(desc);
2748 return desc->gdev->chip->can_sleep;
2750 EXPORT_SYMBOL_GPL(gpiod_cansleep);
2753 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2754 * @desc: gpio whose IRQ will be returned (already requested)
2756 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2757 * error.
2759 int gpiod_to_irq(const struct gpio_desc *desc)
2761 struct gpio_chip *chip;
2762 int offset;
2765 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
2766 * requires this function to not return zero on an invalid descriptor
2767 * but rather a negative error number.
2769 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
2770 return -EINVAL;
2772 chip = desc->gdev->chip;
2773 offset = gpio_chip_hwgpio(desc);
2774 if (chip->to_irq) {
2775 int retirq = chip->to_irq(chip, offset);
2777 /* Zero means NO_IRQ */
2778 if (!retirq)
2779 return -ENXIO;
2781 return retirq;
2783 return -ENXIO;
2785 EXPORT_SYMBOL_GPL(gpiod_to_irq);
2788 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
2789 * @chip: the chip the GPIO to lock belongs to
2790 * @offset: the offset of the GPIO to lock as IRQ
2792 * This is used directly by GPIO drivers that want to lock down
2793 * a certain GPIO line to be used for IRQs.
2795 int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
2797 struct gpio_desc *desc;
2799 desc = gpiochip_get_desc(chip, offset);
2800 if (IS_ERR(desc))
2801 return PTR_ERR(desc);
2804 * If it's fast: flush the direction setting if something changed
2805 * behind our back
2807 if (!chip->can_sleep && chip->get_direction) {
2808 int dir = chip->get_direction(chip, offset);
2810 if (dir)
2811 clear_bit(FLAG_IS_OUT, &desc->flags);
2812 else
2813 set_bit(FLAG_IS_OUT, &desc->flags);
2816 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
2817 chip_err(chip,
2818 "%s: tried to flag a GPIO set as output for IRQ\n",
2819 __func__);
2820 return -EIO;
2823 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
2826 * If the consumer has not set up a label (such as when the
2827 * IRQ is referenced from .to_irq()) we set up a label here
2828 * so it is clear this is used as an interrupt.
2830 if (!desc->label)
2831 desc_set_label(desc, "interrupt");
2833 return 0;
2835 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
2838 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
2839 * @chip: the chip the GPIO to lock belongs to
2840 * @offset: the offset of the GPIO to lock as IRQ
2842 * This is used directly by GPIO drivers that want to indicate
2843 * that a certain GPIO is no longer used exclusively for IRQ.
2845 void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
2847 struct gpio_desc *desc;
2849 desc = gpiochip_get_desc(chip, offset);
2850 if (IS_ERR(desc))
2851 return;
2853 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
2855 /* If we only had this marking, erase it */
2856 if (desc->label && !strcmp(desc->label, "interrupt"))
2857 desc_set_label(desc, NULL);
2859 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
2861 bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
2863 if (offset >= chip->ngpio)
2864 return false;
2866 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
2868 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
2870 bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
2872 if (offset >= chip->ngpio)
2873 return false;
2875 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
2877 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
2879 bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
2881 if (offset >= chip->ngpio)
2882 return false;
2884 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
2886 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
2888 bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
2890 if (offset >= chip->ngpio)
2891 return false;
2893 return !test_bit(FLAG_SLEEP_MAY_LOOSE_VALUE,
2894 &chip->gpiodev->descs[offset].flags);
2896 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
2899 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2900 * @desc: gpio whose value will be returned
2902 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2903 * its ACTIVE_LOW status, or negative errno on failure.
2905 * This function is to be called from contexts that can sleep.
2907 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
2909 might_sleep_if(extra_checks);
2910 VALIDATE_DESC(desc);
2911 return _gpiod_get_raw_value(desc);
2913 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
2916 * gpiod_get_value_cansleep() - return a gpio's value
2917 * @desc: gpio whose value will be returned
2919 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2920 * account, or negative errno on failure.
2922 * This function is to be called from contexts that can sleep.
2924 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
2926 int value;
2928 might_sleep_if(extra_checks);
2929 VALIDATE_DESC(desc);
2930 value = _gpiod_get_raw_value(desc);
2931 if (value < 0)
2932 return value;
2934 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2935 value = !value;
2937 return value;
2939 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
2942 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2943 * @desc: gpio whose value will be assigned
2944 * @value: value to assign
2946 * Set the raw value of the GPIO, i.e. the value of its physical line without
2947 * regard for its ACTIVE_LOW status.
2949 * This function is to be called from contexts that can sleep.
2951 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
2953 might_sleep_if(extra_checks);
2954 VALIDATE_DESC_VOID(desc);
2955 _gpiod_set_raw_value(desc, value);
2957 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
2960 * gpiod_set_value_cansleep() - assign a gpio's value
2961 * @desc: gpio whose value will be assigned
2962 * @value: value to assign
2964 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2965 * account
2967 * This function is to be called from contexts that can sleep.
2969 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
2971 might_sleep_if(extra_checks);
2972 VALIDATE_DESC_VOID(desc);
2973 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2974 value = !value;
2975 _gpiod_set_raw_value(desc, value);
2977 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
2980 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
2981 * @array_size: number of elements in the descriptor / value arrays
2982 * @desc_array: array of GPIO descriptors whose values will be assigned
2983 * @value_array: array of values to assign
2985 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2986 * without regard for their ACTIVE_LOW status.
2988 * This function is to be called from contexts that can sleep.
2990 void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
2991 struct gpio_desc **desc_array,
2992 int *value_array)
2994 might_sleep_if(extra_checks);
2995 if (!desc_array)
2996 return;
2997 gpiod_set_array_value_complex(true, true, array_size, desc_array,
2998 value_array);
3000 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
3003 * gpiod_add_lookup_tables() - register GPIO device consumers
3004 * @tables: list of tables of consumers to register
3005 * @n: number of tables in the list
3007 void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3009 unsigned int i;
3011 mutex_lock(&gpio_lookup_lock);
3013 for (i = 0; i < n; i++)
3014 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3016 mutex_unlock(&gpio_lookup_lock);
3020 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
3021 * @array_size: number of elements in the descriptor / value arrays
3022 * @desc_array: array of GPIO descriptors whose values will be assigned
3023 * @value_array: array of values to assign
3025 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3026 * into account.
3028 * This function is to be called from contexts that can sleep.
3030 void gpiod_set_array_value_cansleep(unsigned int array_size,
3031 struct gpio_desc **desc_array,
3032 int *value_array)
3034 might_sleep_if(extra_checks);
3035 if (!desc_array)
3036 return;
3037 gpiod_set_array_value_complex(false, true, array_size, desc_array,
3038 value_array);
3040 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
3043 * gpiod_add_lookup_table() - register GPIO device consumers
3044 * @table: table of consumers to register
3046 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
3048 mutex_lock(&gpio_lookup_lock);
3050 list_add_tail(&table->list, &gpio_lookup_list);
3052 mutex_unlock(&gpio_lookup_lock);
3054 EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
3057 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3058 * @table: table of consumers to unregister
3060 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3062 mutex_lock(&gpio_lookup_lock);
3064 list_del(&table->list);
3066 mutex_unlock(&gpio_lookup_lock);
3068 EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
3070 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3072 const char *dev_id = dev ? dev_name(dev) : NULL;
3073 struct gpiod_lookup_table *table;
3075 mutex_lock(&gpio_lookup_lock);
3077 list_for_each_entry(table, &gpio_lookup_list, list) {
3078 if (table->dev_id && dev_id) {
3080 * Valid strings on both ends, must be identical to have
3081 * a match
3083 if (!strcmp(table->dev_id, dev_id))
3084 goto found;
3085 } else {
3087 * One of the pointers is NULL, so both must be to have
3088 * a match
3090 if (dev_id == table->dev_id)
3091 goto found;
3094 table = NULL;
3096 found:
3097 mutex_unlock(&gpio_lookup_lock);
3098 return table;
3101 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
3102 unsigned int idx,
3103 enum gpio_lookup_flags *flags)
3105 struct gpio_desc *desc = ERR_PTR(-ENOENT);
3106 struct gpiod_lookup_table *table;
3107 struct gpiod_lookup *p;
3109 table = gpiod_find_lookup_table(dev);
3110 if (!table)
3111 return desc;
3113 for (p = &table->table[0]; p->chip_label; p++) {
3114 struct gpio_chip *chip;
3116 /* idx must always match exactly */
3117 if (p->idx != idx)
3118 continue;
3120 /* If the lookup entry has a con_id, require exact match */
3121 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3122 continue;
3124 chip = find_chip_by_name(p->chip_label);
3126 if (!chip) {
3127 dev_err(dev, "cannot find GPIO chip %s\n",
3128 p->chip_label);
3129 return ERR_PTR(-ENODEV);
3132 if (chip->ngpio <= p->chip_hwnum) {
3133 dev_err(dev,
3134 "requested GPIO %d is out of range [0..%d] for chip %s\n",
3135 idx, chip->ngpio, chip->label);
3136 return ERR_PTR(-EINVAL);
3139 desc = gpiochip_get_desc(chip, p->chip_hwnum);
3140 *flags = p->flags;
3142 return desc;
3145 return desc;
3148 static int dt_gpio_count(struct device *dev, const char *con_id)
3150 int ret;
3151 char propname[32];
3152 unsigned int i;
3154 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3155 if (con_id)
3156 snprintf(propname, sizeof(propname), "%s-%s",
3157 con_id, gpio_suffixes[i]);
3158 else
3159 snprintf(propname, sizeof(propname), "%s",
3160 gpio_suffixes[i]);
3162 ret = of_gpio_named_count(dev->of_node, propname);
3163 if (ret > 0)
3164 break;
3166 return ret ? ret : -ENOENT;
3169 static int platform_gpio_count(struct device *dev, const char *con_id)
3171 struct gpiod_lookup_table *table;
3172 struct gpiod_lookup *p;
3173 unsigned int count = 0;
3175 table = gpiod_find_lookup_table(dev);
3176 if (!table)
3177 return -ENOENT;
3179 for (p = &table->table[0]; p->chip_label; p++) {
3180 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3181 (!con_id && !p->con_id))
3182 count++;
3184 if (!count)
3185 return -ENOENT;
3187 return count;
3191 * gpiod_count - return the number of GPIOs associated with a device / function
3192 * or -ENOENT if no GPIO has been assigned to the requested function
3193 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3194 * @con_id: function within the GPIO consumer
3196 int gpiod_count(struct device *dev, const char *con_id)
3198 int count = -ENOENT;
3200 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3201 count = dt_gpio_count(dev, con_id);
3202 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3203 count = acpi_gpio_count(dev, con_id);
3205 if (count < 0)
3206 count = platform_gpio_count(dev, con_id);
3208 return count;
3210 EXPORT_SYMBOL_GPL(gpiod_count);
3213 * gpiod_get - obtain a GPIO for a given GPIO function
3214 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3215 * @con_id: function within the GPIO consumer
3216 * @flags: optional GPIO initialization flags
3218 * Return the GPIO descriptor corresponding to the function con_id of device
3219 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
3220 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
3222 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
3223 enum gpiod_flags flags)
3225 return gpiod_get_index(dev, con_id, 0, flags);
3227 EXPORT_SYMBOL_GPL(gpiod_get);
3230 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3231 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3232 * @con_id: function within the GPIO consumer
3233 * @flags: optional GPIO initialization flags
3235 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3236 * the requested function it will return NULL. This is convenient for drivers
3237 * that need to handle optional GPIOs.
3239 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
3240 const char *con_id,
3241 enum gpiod_flags flags)
3243 return gpiod_get_index_optional(dev, con_id, 0, flags);
3245 EXPORT_SYMBOL_GPL(gpiod_get_optional);
3249 * gpiod_configure_flags - helper function to configure a given GPIO
3250 * @desc: gpio whose value will be assigned
3251 * @con_id: function within the GPIO consumer
3252 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3253 * of_get_gpio_hog()
3254 * @dflags: gpiod_flags - optional GPIO initialization flags
3256 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3257 * requested function and/or index, or another IS_ERR() code if an error
3258 * occurred while trying to acquire the GPIO.
3260 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
3261 unsigned long lflags, enum gpiod_flags dflags)
3263 int status;
3265 if (lflags & GPIO_ACTIVE_LOW)
3266 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3267 if (lflags & GPIO_OPEN_DRAIN)
3268 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3269 if (lflags & GPIO_OPEN_SOURCE)
3270 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3271 if (lflags & GPIO_SLEEP_MAY_LOOSE_VALUE)
3272 set_bit(FLAG_SLEEP_MAY_LOOSE_VALUE, &desc->flags);
3274 /* No particular flag request, return here... */
3275 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3276 pr_debug("no flags found for %s\n", con_id);
3277 return 0;
3280 /* Process flags */
3281 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3282 status = gpiod_direction_output(desc,
3283 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
3284 else
3285 status = gpiod_direction_input(desc);
3287 return status;
3291 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
3292 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3293 * @con_id: function within the GPIO consumer
3294 * @idx: index of the GPIO to obtain in the consumer
3295 * @flags: optional GPIO initialization flags
3297 * This variant of gpiod_get() allows to access GPIOs other than the first
3298 * defined one for functions that define several GPIOs.
3300 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3301 * requested function and/or index, or another IS_ERR() code if an error
3302 * occurred while trying to acquire the GPIO.
3304 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
3305 const char *con_id,
3306 unsigned int idx,
3307 enum gpiod_flags flags)
3309 struct gpio_desc *desc = NULL;
3310 int status;
3311 enum gpio_lookup_flags lookupflags = 0;
3313 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3315 if (dev) {
3316 /* Using device tree? */
3317 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3318 dev_dbg(dev, "using device tree for GPIO lookup\n");
3319 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3320 } else if (ACPI_COMPANION(dev)) {
3321 dev_dbg(dev, "using ACPI for GPIO lookup\n");
3322 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
3327 * Either we are not using DT or ACPI, or their lookup did not return
3328 * a result. In that case, use platform lookup as a fallback.
3330 if (!desc || desc == ERR_PTR(-ENOENT)) {
3331 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
3332 desc = gpiod_find(dev, con_id, idx, &lookupflags);
3335 if (IS_ERR(desc)) {
3336 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
3337 return desc;
3340 status = gpiod_request(desc, con_id);
3341 if (status < 0)
3342 return ERR_PTR(status);
3344 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
3345 if (status < 0) {
3346 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3347 gpiod_put(desc);
3348 return ERR_PTR(status);
3351 return desc;
3353 EXPORT_SYMBOL_GPL(gpiod_get_index);
3356 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3357 * @fwnode: handle of the firmware node
3358 * @propname: name of the firmware property representing the GPIO
3359 * @index: index of the GPIO to obtain in the consumer
3360 * @dflags: GPIO initialization flags
3361 * @label: label to attach to the requested GPIO
3363 * This function can be used for drivers that get their configuration
3364 * from firmware.
3366 * Function properly finds the corresponding GPIO using whatever is the
3367 * underlying firmware interface and then makes sure that the GPIO
3368 * descriptor is requested before it is returned to the caller.
3370 * Returns:
3371 * On successful request the GPIO pin is configured in accordance with
3372 * provided @dflags.
3374 * In case of error an ERR_PTR() is returned.
3376 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
3377 const char *propname, int index,
3378 enum gpiod_flags dflags,
3379 const char *label)
3381 struct gpio_desc *desc = ERR_PTR(-ENODEV);
3382 unsigned long lflags = 0;
3383 bool active_low = false;
3384 bool single_ended = false;
3385 bool open_drain = false;
3386 int ret;
3388 if (!fwnode)
3389 return ERR_PTR(-EINVAL);
3391 if (is_of_node(fwnode)) {
3392 enum of_gpio_flags flags;
3394 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname,
3395 index, &flags);
3396 if (!IS_ERR(desc)) {
3397 active_low = flags & OF_GPIO_ACTIVE_LOW;
3398 single_ended = flags & OF_GPIO_SINGLE_ENDED;
3399 open_drain = flags & OF_GPIO_OPEN_DRAIN;
3401 } else if (is_acpi_node(fwnode)) {
3402 struct acpi_gpio_info info;
3404 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
3405 if (!IS_ERR(desc)) {
3406 active_low = info.polarity == GPIO_ACTIVE_LOW;
3407 ret = acpi_gpio_update_gpiod_flags(&dflags, info.flags);
3408 if (ret)
3409 pr_debug("Override GPIO initialization flags\n");
3413 if (IS_ERR(desc))
3414 return desc;
3416 ret = gpiod_request(desc, label);
3417 if (ret)
3418 return ERR_PTR(ret);
3420 if (active_low)
3421 lflags |= GPIO_ACTIVE_LOW;
3423 if (single_ended) {
3424 if (open_drain)
3425 lflags |= GPIO_OPEN_DRAIN;
3426 else
3427 lflags |= GPIO_OPEN_SOURCE;
3430 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3431 if (ret < 0) {
3432 gpiod_put(desc);
3433 return ERR_PTR(ret);
3436 return desc;
3438 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3441 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3442 * function
3443 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3444 * @con_id: function within the GPIO consumer
3445 * @index: index of the GPIO to obtain in the consumer
3446 * @flags: optional GPIO initialization flags
3448 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3449 * specified index was assigned to the requested function it will return NULL.
3450 * This is convenient for drivers that need to handle optional GPIOs.
3452 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
3453 const char *con_id,
3454 unsigned int index,
3455 enum gpiod_flags flags)
3457 struct gpio_desc *desc;
3459 desc = gpiod_get_index(dev, con_id, index, flags);
3460 if (IS_ERR(desc)) {
3461 if (PTR_ERR(desc) == -ENOENT)
3462 return NULL;
3465 return desc;
3467 EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
3470 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3471 * @desc: gpio whose value will be assigned
3472 * @name: gpio line name
3473 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3474 * of_get_gpio_hog()
3475 * @dflags: gpiod_flags - optional GPIO initialization flags
3477 int gpiod_hog(struct gpio_desc *desc, const char *name,
3478 unsigned long lflags, enum gpiod_flags dflags)
3480 struct gpio_chip *chip;
3481 struct gpio_desc *local_desc;
3482 int hwnum;
3483 int status;
3485 chip = gpiod_to_chip(desc);
3486 hwnum = gpio_chip_hwgpio(desc);
3488 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
3489 if (IS_ERR(local_desc)) {
3490 status = PTR_ERR(local_desc);
3491 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3492 name, chip->label, hwnum, status);
3493 return status;
3496 status = gpiod_configure_flags(desc, name, lflags, dflags);
3497 if (status < 0) {
3498 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3499 name, chip->label, hwnum, status);
3500 gpiochip_free_own_desc(desc);
3501 return status;
3504 /* Mark GPIO as hogged so it can be identified and removed later */
3505 set_bit(FLAG_IS_HOGGED, &desc->flags);
3507 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3508 desc_to_gpio(desc), name,
3509 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3510 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3511 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3513 return 0;
3517 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3518 * @chip: gpio chip to act on
3520 * This is only used by of_gpiochip_remove to free hogged gpios
3522 static void gpiochip_free_hogs(struct gpio_chip *chip)
3524 int id;
3526 for (id = 0; id < chip->ngpio; id++) {
3527 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
3528 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
3533 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
3534 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3535 * @con_id: function within the GPIO consumer
3536 * @flags: optional GPIO initialization flags
3538 * This function acquires all the GPIOs defined under a given function.
3540 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
3541 * no GPIO has been assigned to the requested function, or another IS_ERR()
3542 * code if an error occurred while trying to acquire the GPIOs.
3544 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
3545 const char *con_id,
3546 enum gpiod_flags flags)
3548 struct gpio_desc *desc;
3549 struct gpio_descs *descs;
3550 int count;
3552 count = gpiod_count(dev, con_id);
3553 if (count < 0)
3554 return ERR_PTR(count);
3556 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
3557 GFP_KERNEL);
3558 if (!descs)
3559 return ERR_PTR(-ENOMEM);
3561 for (descs->ndescs = 0; descs->ndescs < count; ) {
3562 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
3563 if (IS_ERR(desc)) {
3564 gpiod_put_array(descs);
3565 return ERR_CAST(desc);
3567 descs->desc[descs->ndescs] = desc;
3568 descs->ndescs++;
3570 return descs;
3572 EXPORT_SYMBOL_GPL(gpiod_get_array);
3575 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
3576 * function
3577 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3578 * @con_id: function within the GPIO consumer
3579 * @flags: optional GPIO initialization flags
3581 * This is equivalent to gpiod_get_array(), except that when no GPIO was
3582 * assigned to the requested function it will return NULL.
3584 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
3585 const char *con_id,
3586 enum gpiod_flags flags)
3588 struct gpio_descs *descs;
3590 descs = gpiod_get_array(dev, con_id, flags);
3591 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
3592 return NULL;
3594 return descs;
3596 EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
3599 * gpiod_put - dispose of a GPIO descriptor
3600 * @desc: GPIO descriptor to dispose of
3602 * No descriptor can be used after gpiod_put() has been called on it.
3604 void gpiod_put(struct gpio_desc *desc)
3606 gpiod_free(desc);
3608 EXPORT_SYMBOL_GPL(gpiod_put);
3611 * gpiod_put_array - dispose of multiple GPIO descriptors
3612 * @descs: struct gpio_descs containing an array of descriptors
3614 void gpiod_put_array(struct gpio_descs *descs)
3616 unsigned int i;
3618 for (i = 0; i < descs->ndescs; i++)
3619 gpiod_put(descs->desc[i]);
3621 kfree(descs);
3623 EXPORT_SYMBOL_GPL(gpiod_put_array);
3625 static int __init gpiolib_dev_init(void)
3627 int ret;
3629 /* Register GPIO sysfs bus */
3630 ret = bus_register(&gpio_bus_type);
3631 if (ret < 0) {
3632 pr_err("gpiolib: could not register GPIO bus type\n");
3633 return ret;
3636 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
3637 if (ret < 0) {
3638 pr_err("gpiolib: failed to allocate char dev region\n");
3639 bus_unregister(&gpio_bus_type);
3640 } else {
3641 gpiolib_initialized = true;
3642 gpiochip_setup_devs();
3644 return ret;
3646 core_initcall(gpiolib_dev_init);
3648 #ifdef CONFIG_DEBUG_FS
3650 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
3652 unsigned i;
3653 struct gpio_chip *chip = gdev->chip;
3654 unsigned gpio = gdev->base;
3655 struct gpio_desc *gdesc = &gdev->descs[0];
3656 int is_out;
3657 int is_irq;
3659 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
3660 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
3661 if (gdesc->name) {
3662 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
3663 gpio, gdesc->name);
3665 continue;
3668 gpiod_get_direction(gdesc);
3669 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
3670 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
3671 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
3672 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
3673 is_out ? "out" : "in ",
3674 chip->get
3675 ? (chip->get(chip, i) ? "hi" : "lo")
3676 : "? ",
3677 is_irq ? "IRQ" : " ");
3678 seq_printf(s, "\n");
3682 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
3684 unsigned long flags;
3685 struct gpio_device *gdev = NULL;
3686 loff_t index = *pos;
3688 s->private = "";
3690 spin_lock_irqsave(&gpio_lock, flags);
3691 list_for_each_entry(gdev, &gpio_devices, list)
3692 if (index-- == 0) {
3693 spin_unlock_irqrestore(&gpio_lock, flags);
3694 return gdev;
3696 spin_unlock_irqrestore(&gpio_lock, flags);
3698 return NULL;
3701 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
3703 unsigned long flags;
3704 struct gpio_device *gdev = v;
3705 void *ret = NULL;
3707 spin_lock_irqsave(&gpio_lock, flags);
3708 if (list_is_last(&gdev->list, &gpio_devices))
3709 ret = NULL;
3710 else
3711 ret = list_entry(gdev->list.next, struct gpio_device, list);
3712 spin_unlock_irqrestore(&gpio_lock, flags);
3714 s->private = "\n";
3715 ++*pos;
3717 return ret;
3720 static void gpiolib_seq_stop(struct seq_file *s, void *v)
3724 static int gpiolib_seq_show(struct seq_file *s, void *v)
3726 struct gpio_device *gdev = v;
3727 struct gpio_chip *chip = gdev->chip;
3728 struct device *parent;
3730 if (!chip) {
3731 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
3732 dev_name(&gdev->dev));
3733 return 0;
3736 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
3737 dev_name(&gdev->dev),
3738 gdev->base, gdev->base + gdev->ngpio - 1);
3739 parent = chip->parent;
3740 if (parent)
3741 seq_printf(s, ", parent: %s/%s",
3742 parent->bus ? parent->bus->name : "no-bus",
3743 dev_name(parent));
3744 if (chip->label)
3745 seq_printf(s, ", %s", chip->label);
3746 if (chip->can_sleep)
3747 seq_printf(s, ", can sleep");
3748 seq_printf(s, ":\n");
3750 if (chip->dbg_show)
3751 chip->dbg_show(s, chip);
3752 else
3753 gpiolib_dbg_show(s, gdev);
3755 return 0;
3758 static const struct seq_operations gpiolib_seq_ops = {
3759 .start = gpiolib_seq_start,
3760 .next = gpiolib_seq_next,
3761 .stop = gpiolib_seq_stop,
3762 .show = gpiolib_seq_show,
3765 static int gpiolib_open(struct inode *inode, struct file *file)
3767 return seq_open(file, &gpiolib_seq_ops);
3770 static const struct file_operations gpiolib_operations = {
3771 .owner = THIS_MODULE,
3772 .open = gpiolib_open,
3773 .read = seq_read,
3774 .llseek = seq_lseek,
3775 .release = seq_release,
3778 static int __init gpiolib_debugfs_init(void)
3780 /* /sys/kernel/debug/gpio */
3781 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
3782 NULL, NULL, &gpiolib_operations);
3783 return 0;
3785 subsys_initcall(gpiolib_debugfs_init);
3787 #endif /* DEBUG_FS */