1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/bitmap.h>
3 #include <linux/kernel.h>
4 #include <linux/module.h>
5 #include <linux/interrupt.h>
7 #include <linux/spinlock.h>
8 #include <linux/list.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/debugfs.h>
12 #include <linux/seq_file.h>
13 #include <linux/gpio.h>
14 #include <linux/idr.h>
15 #include <linux/slab.h>
16 #include <linux/acpi.h>
17 #include <linux/gpio/driver.h>
18 #include <linux/gpio/machine.h>
19 #include <linux/pinctrl/consumer.h>
20 #include <linux/cdev.h>
22 #include <linux/uaccess.h>
23 #include <linux/compat.h>
24 #include <linux/anon_inodes.h>
25 #include <linux/file.h>
26 #include <linux/kfifo.h>
27 #include <linux/poll.h>
28 #include <linux/timekeeping.h>
29 #include <uapi/linux/gpio.h>
32 #include "gpiolib-of.h"
33 #include "gpiolib-acpi.h"
35 #define CREATE_TRACE_POINTS
36 #include <trace/events/gpio.h>
38 /* Implementation infrastructure for GPIO interfaces.
40 * The GPIO programming interface allows for inlining speed-critical
41 * get/set operations for common cases, so that access to SOC-integrated
42 * GPIOs can sometimes cost only an instruction or two per bit.
46 /* When debugging, extend minimal trust to callers and platform code.
47 * Also emit diagnostic messages that may help initial bringup, when
48 * board setup or driver bugs are most common.
50 * Otherwise, minimize overhead in what may be bitbanging codepaths.
53 #define extra_checks 1
55 #define extra_checks 0
58 /* Device and char device-related information */
59 static DEFINE_IDA(gpio_ida
);
60 static dev_t gpio_devt
;
61 #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
62 static struct bus_type gpio_bus_type
= {
67 * Number of GPIOs to use for the fast path in set array
69 #define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT
71 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
72 * While any GPIO is requested, its gpio_chip is not removable;
73 * each GPIO's "requested" flag serves as a lock and refcount.
75 DEFINE_SPINLOCK(gpio_lock
);
77 static DEFINE_MUTEX(gpio_lookup_lock
);
78 static LIST_HEAD(gpio_lookup_list
);
79 LIST_HEAD(gpio_devices
);
81 static DEFINE_MUTEX(gpio_machine_hogs_mutex
);
82 static LIST_HEAD(gpio_machine_hogs
);
84 static void gpiochip_free_hogs(struct gpio_chip
*gc
);
85 static int gpiochip_add_irqchip(struct gpio_chip
*gc
,
86 struct lock_class_key
*lock_key
,
87 struct lock_class_key
*request_key
);
88 static void gpiochip_irqchip_remove(struct gpio_chip
*gc
);
89 static int gpiochip_irqchip_init_hw(struct gpio_chip
*gc
);
90 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip
*gc
);
91 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip
*gc
);
93 static bool gpiolib_initialized
;
95 static inline void desc_set_label(struct gpio_desc
*d
, const char *label
)
101 * gpio_to_desc - Convert a GPIO number to its descriptor
102 * @gpio: global GPIO number
105 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
106 * with the given number exists in the system.
108 struct gpio_desc
*gpio_to_desc(unsigned gpio
)
110 struct gpio_device
*gdev
;
113 spin_lock_irqsave(&gpio_lock
, flags
);
115 list_for_each_entry(gdev
, &gpio_devices
, list
) {
116 if (gdev
->base
<= gpio
&&
117 gdev
->base
+ gdev
->ngpio
> gpio
) {
118 spin_unlock_irqrestore(&gpio_lock
, flags
);
119 return &gdev
->descs
[gpio
- gdev
->base
];
123 spin_unlock_irqrestore(&gpio_lock
, flags
);
125 if (!gpio_is_valid(gpio
))
126 WARN(1, "invalid GPIO %d\n", gpio
);
130 EXPORT_SYMBOL_GPL(gpio_to_desc
);
133 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
134 * hardware number for this chip
136 * @hwnum: hardware number of the GPIO for this chip
139 * A pointer to the GPIO descriptor or ``ERR_PTR(-EINVAL)`` if no GPIO exists
140 * in the given chip for the specified hardware number.
142 struct gpio_desc
*gpiochip_get_desc(struct gpio_chip
*gc
,
145 struct gpio_device
*gdev
= gc
->gpiodev
;
147 if (hwnum
>= gdev
->ngpio
)
148 return ERR_PTR(-EINVAL
);
150 return &gdev
->descs
[hwnum
];
152 EXPORT_SYMBOL_GPL(gpiochip_get_desc
);
155 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
156 * @desc: GPIO descriptor
158 * This should disappear in the future but is needed since we still
159 * use GPIO numbers for error messages and sysfs nodes.
162 * The global GPIO number for the GPIO specified by its descriptor.
164 int desc_to_gpio(const struct gpio_desc
*desc
)
166 return desc
->gdev
->base
+ (desc
- &desc
->gdev
->descs
[0]);
168 EXPORT_SYMBOL_GPL(desc_to_gpio
);
172 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
173 * @desc: descriptor to return the chip of
175 struct gpio_chip
*gpiod_to_chip(const struct gpio_desc
*desc
)
177 if (!desc
|| !desc
->gdev
)
179 return desc
->gdev
->chip
;
181 EXPORT_SYMBOL_GPL(gpiod_to_chip
);
183 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
184 static int gpiochip_find_base(int ngpio
)
186 struct gpio_device
*gdev
;
187 int base
= ARCH_NR_GPIOS
- ngpio
;
189 list_for_each_entry_reverse(gdev
, &gpio_devices
, list
) {
190 /* found a free space? */
191 if (gdev
->base
+ gdev
->ngpio
<= base
)
194 /* nope, check the space right before the chip */
195 base
= gdev
->base
- ngpio
;
198 if (gpio_is_valid(base
)) {
199 pr_debug("%s: found new base at %d\n", __func__
, base
);
202 pr_err("%s: cannot find free range\n", __func__
);
208 * gpiod_get_direction - return the current direction of a GPIO
209 * @desc: GPIO to get the direction of
211 * Returns 0 for output, 1 for input, or an error code in case of error.
213 * This function may sleep if gpiod_cansleep() is true.
215 int gpiod_get_direction(struct gpio_desc
*desc
)
217 struct gpio_chip
*gc
;
221 gc
= gpiod_to_chip(desc
);
222 offset
= gpio_chip_hwgpio(desc
);
225 * Open drain emulation using input mode may incorrectly report
226 * input here, fix that up.
228 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
) &&
229 test_bit(FLAG_IS_OUT
, &desc
->flags
))
232 if (!gc
->get_direction
)
235 ret
= gc
->get_direction(gc
, offset
);
239 /* GPIOF_DIR_IN or other positive, otherwise GPIOF_DIR_OUT */
243 assign_bit(FLAG_IS_OUT
, &desc
->flags
, !ret
);
247 EXPORT_SYMBOL_GPL(gpiod_get_direction
);
250 * Add a new chip to the global chips list, keeping the list of chips sorted
251 * by range(means [base, base + ngpio - 1]) order.
253 * Return -EBUSY if the new chip overlaps with some other chip's integer
256 static int gpiodev_add_to_list(struct gpio_device
*gdev
)
258 struct gpio_device
*prev
, *next
;
260 if (list_empty(&gpio_devices
)) {
261 /* initial entry in list */
262 list_add_tail(&gdev
->list
, &gpio_devices
);
266 next
= list_entry(gpio_devices
.next
, struct gpio_device
, list
);
267 if (gdev
->base
+ gdev
->ngpio
<= next
->base
) {
268 /* add before first entry */
269 list_add(&gdev
->list
, &gpio_devices
);
273 prev
= list_entry(gpio_devices
.prev
, struct gpio_device
, list
);
274 if (prev
->base
+ prev
->ngpio
<= gdev
->base
) {
275 /* add behind last entry */
276 list_add_tail(&gdev
->list
, &gpio_devices
);
280 list_for_each_entry_safe(prev
, next
, &gpio_devices
, list
) {
281 /* at the end of the list */
282 if (&next
->list
== &gpio_devices
)
285 /* add between prev and next */
286 if (prev
->base
+ prev
->ngpio
<= gdev
->base
287 && gdev
->base
+ gdev
->ngpio
<= next
->base
) {
288 list_add(&gdev
->list
, &prev
->list
);
293 dev_err(&gdev
->dev
, "GPIO integer space overlap, cannot add chip\n");
298 * Convert a GPIO name to its descriptor
300 static struct gpio_desc
*gpio_name_to_desc(const char * const name
)
302 struct gpio_device
*gdev
;
308 spin_lock_irqsave(&gpio_lock
, flags
);
310 list_for_each_entry(gdev
, &gpio_devices
, list
) {
313 for (i
= 0; i
!= gdev
->ngpio
; ++i
) {
314 struct gpio_desc
*desc
= &gdev
->descs
[i
];
319 if (!strcmp(desc
->name
, name
)) {
320 spin_unlock_irqrestore(&gpio_lock
, flags
);
326 spin_unlock_irqrestore(&gpio_lock
, flags
);
332 * Takes the names from gc->names and checks if they are all unique. If they
333 * are, they are assigned to their gpio descriptors.
335 * Warning if one of the names is already used for a different GPIO.
337 static int gpiochip_set_desc_names(struct gpio_chip
*gc
)
339 struct gpio_device
*gdev
= gc
->gpiodev
;
345 /* First check all names if they are unique */
346 for (i
= 0; i
!= gc
->ngpio
; ++i
) {
347 struct gpio_desc
*gpio
;
349 gpio
= gpio_name_to_desc(gc
->names
[i
]);
352 "Detected name collision for GPIO name '%s'\n",
356 /* Then add all names to the GPIO descriptors */
357 for (i
= 0; i
!= gc
->ngpio
; ++i
)
358 gdev
->descs
[i
].name
= gc
->names
[i
];
363 static unsigned long *gpiochip_allocate_mask(struct gpio_chip
*gc
)
367 p
= bitmap_alloc(gc
->ngpio
, GFP_KERNEL
);
371 /* Assume by default all GPIOs are valid */
372 bitmap_fill(p
, gc
->ngpio
);
377 static int gpiochip_alloc_valid_mask(struct gpio_chip
*gc
)
379 if (!(of_gpio_need_valid_mask(gc
) || gc
->init_valid_mask
))
382 gc
->valid_mask
= gpiochip_allocate_mask(gc
);
389 static int gpiochip_init_valid_mask(struct gpio_chip
*gc
)
391 if (gc
->init_valid_mask
)
392 return gc
->init_valid_mask(gc
,
399 static void gpiochip_free_valid_mask(struct gpio_chip
*gc
)
401 bitmap_free(gc
->valid_mask
);
402 gc
->valid_mask
= NULL
;
405 static int gpiochip_add_pin_ranges(struct gpio_chip
*gc
)
407 if (gc
->add_pin_ranges
)
408 return gc
->add_pin_ranges(gc
);
413 bool gpiochip_line_is_valid(const struct gpio_chip
*gc
,
416 /* No mask means all valid */
417 if (likely(!gc
->valid_mask
))
419 return test_bit(offset
, gc
->valid_mask
);
421 EXPORT_SYMBOL_GPL(gpiochip_line_is_valid
);
424 * GPIO line handle management
428 * struct linehandle_state - contains the state of a userspace handle
429 * @gdev: the GPIO device the handle pertains to
430 * @label: consumer label used to tag descriptors
431 * @descs: the GPIO descriptors held by this handle
432 * @numdescs: the number of descriptors held in the descs array
434 struct linehandle_state
{
435 struct gpio_device
*gdev
;
437 struct gpio_desc
*descs
[GPIOHANDLES_MAX
];
441 #define GPIOHANDLE_REQUEST_VALID_FLAGS \
442 (GPIOHANDLE_REQUEST_INPUT | \
443 GPIOHANDLE_REQUEST_OUTPUT | \
444 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
445 GPIOHANDLE_REQUEST_BIAS_PULL_UP | \
446 GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \
447 GPIOHANDLE_REQUEST_BIAS_DISABLE | \
448 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
449 GPIOHANDLE_REQUEST_OPEN_SOURCE)
451 static int linehandle_validate_flags(u32 flags
)
453 /* Return an error if an unknown flag is set */
454 if (flags
& ~GPIOHANDLE_REQUEST_VALID_FLAGS
)
458 * Do not allow both INPUT & OUTPUT flags to be set as they are
461 if ((flags
& GPIOHANDLE_REQUEST_INPUT
) &&
462 (flags
& GPIOHANDLE_REQUEST_OUTPUT
))
466 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
467 * the hardware actually supports enabling both at the same time the
468 * electrical result would be disastrous.
470 if ((flags
& GPIOHANDLE_REQUEST_OPEN_DRAIN
) &&
471 (flags
& GPIOHANDLE_REQUEST_OPEN_SOURCE
))
474 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
475 if (!(flags
& GPIOHANDLE_REQUEST_OUTPUT
) &&
476 ((flags
& GPIOHANDLE_REQUEST_OPEN_DRAIN
) ||
477 (flags
& GPIOHANDLE_REQUEST_OPEN_SOURCE
)))
480 /* Bias flags only allowed for input or output mode. */
481 if (!((flags
& GPIOHANDLE_REQUEST_INPUT
) ||
482 (flags
& GPIOHANDLE_REQUEST_OUTPUT
)) &&
483 ((flags
& GPIOHANDLE_REQUEST_BIAS_DISABLE
) ||
484 (flags
& GPIOHANDLE_REQUEST_BIAS_PULL_UP
) ||
485 (flags
& GPIOHANDLE_REQUEST_BIAS_PULL_DOWN
)))
488 /* Only one bias flag can be set. */
489 if (((flags
& GPIOHANDLE_REQUEST_BIAS_DISABLE
) &&
490 (flags
& (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN
|
491 GPIOHANDLE_REQUEST_BIAS_PULL_UP
))) ||
492 ((flags
& GPIOHANDLE_REQUEST_BIAS_PULL_DOWN
) &&
493 (flags
& GPIOHANDLE_REQUEST_BIAS_PULL_UP
)))
499 static long linehandle_set_config(struct linehandle_state
*lh
,
502 struct gpiohandle_config gcnf
;
503 struct gpio_desc
*desc
;
506 unsigned long *flagsp
;
508 if (copy_from_user(&gcnf
, ip
, sizeof(gcnf
)))
512 ret
= linehandle_validate_flags(lflags
);
516 for (i
= 0; i
< lh
->numdescs
; i
++) {
518 flagsp
= &desc
->flags
;
520 assign_bit(FLAG_ACTIVE_LOW
, flagsp
,
521 lflags
& GPIOHANDLE_REQUEST_ACTIVE_LOW
);
523 assign_bit(FLAG_OPEN_DRAIN
, flagsp
,
524 lflags
& GPIOHANDLE_REQUEST_OPEN_DRAIN
);
526 assign_bit(FLAG_OPEN_SOURCE
, flagsp
,
527 lflags
& GPIOHANDLE_REQUEST_OPEN_SOURCE
);
529 assign_bit(FLAG_PULL_UP
, flagsp
,
530 lflags
& GPIOHANDLE_REQUEST_BIAS_PULL_UP
);
532 assign_bit(FLAG_PULL_DOWN
, flagsp
,
533 lflags
& GPIOHANDLE_REQUEST_BIAS_PULL_DOWN
);
535 assign_bit(FLAG_BIAS_DISABLE
, flagsp
,
536 lflags
& GPIOHANDLE_REQUEST_BIAS_DISABLE
);
539 * Lines have to be requested explicitly for input
540 * or output, else the line will be treated "as is".
542 if (lflags
& GPIOHANDLE_REQUEST_OUTPUT
) {
543 int val
= !!gcnf
.default_values
[i
];
545 ret
= gpiod_direction_output(desc
, val
);
548 } else if (lflags
& GPIOHANDLE_REQUEST_INPUT
) {
549 ret
= gpiod_direction_input(desc
);
554 atomic_notifier_call_chain(&desc
->gdev
->notifier
,
555 GPIOLINE_CHANGED_CONFIG
, desc
);
560 static long linehandle_ioctl(struct file
*filep
, unsigned int cmd
,
563 struct linehandle_state
*lh
= filep
->private_data
;
564 void __user
*ip
= (void __user
*)arg
;
565 struct gpiohandle_data ghd
;
566 DECLARE_BITMAP(vals
, GPIOHANDLES_MAX
);
569 if (cmd
== GPIOHANDLE_GET_LINE_VALUES_IOCTL
) {
570 /* NOTE: It's ok to read values of output lines. */
571 int ret
= gpiod_get_array_value_complex(false,
580 memset(&ghd
, 0, sizeof(ghd
));
581 for (i
= 0; i
< lh
->numdescs
; i
++)
582 ghd
.values
[i
] = test_bit(i
, vals
);
584 if (copy_to_user(ip
, &ghd
, sizeof(ghd
)))
588 } else if (cmd
== GPIOHANDLE_SET_LINE_VALUES_IOCTL
) {
590 * All line descriptors were created at once with the same
591 * flags so just check if the first one is really output.
593 if (!test_bit(FLAG_IS_OUT
, &lh
->descs
[0]->flags
))
596 if (copy_from_user(&ghd
, ip
, sizeof(ghd
)))
599 /* Clamp all values to [0,1] */
600 for (i
= 0; i
< lh
->numdescs
; i
++)
601 __assign_bit(i
, vals
, ghd
.values
[i
]);
603 /* Reuse the array setting function */
604 return gpiod_set_array_value_complex(false,
610 } else if (cmd
== GPIOHANDLE_SET_CONFIG_IOCTL
) {
611 return linehandle_set_config(lh
, ip
);
617 static long linehandle_ioctl_compat(struct file
*filep
, unsigned int cmd
,
620 return linehandle_ioctl(filep
, cmd
, (unsigned long)compat_ptr(arg
));
624 static int linehandle_release(struct inode
*inode
, struct file
*filep
)
626 struct linehandle_state
*lh
= filep
->private_data
;
627 struct gpio_device
*gdev
= lh
->gdev
;
630 for (i
= 0; i
< lh
->numdescs
; i
++)
631 gpiod_free(lh
->descs
[i
]);
634 put_device(&gdev
->dev
);
638 static const struct file_operations linehandle_fileops
= {
639 .release
= linehandle_release
,
640 .owner
= THIS_MODULE
,
641 .llseek
= noop_llseek
,
642 .unlocked_ioctl
= linehandle_ioctl
,
644 .compat_ioctl
= linehandle_ioctl_compat
,
648 static int linehandle_create(struct gpio_device
*gdev
, void __user
*ip
)
650 struct gpiohandle_request handlereq
;
651 struct linehandle_state
*lh
;
653 int fd
, i
, count
= 0, ret
;
656 if (copy_from_user(&handlereq
, ip
, sizeof(handlereq
)))
658 if ((handlereq
.lines
== 0) || (handlereq
.lines
> GPIOHANDLES_MAX
))
661 lflags
= handlereq
.flags
;
663 ret
= linehandle_validate_flags(lflags
);
667 lh
= kzalloc(sizeof(*lh
), GFP_KERNEL
);
671 get_device(&gdev
->dev
);
673 /* Make sure this is terminated */
674 handlereq
.consumer_label
[sizeof(handlereq
.consumer_label
)-1] = '\0';
675 if (strlen(handlereq
.consumer_label
)) {
676 lh
->label
= kstrdup(handlereq
.consumer_label
,
684 /* Request each GPIO */
685 for (i
= 0; i
< handlereq
.lines
; i
++) {
686 u32 offset
= handlereq
.lineoffsets
[i
];
687 struct gpio_desc
*desc
= gpiochip_get_desc(gdev
->chip
, offset
);
694 ret
= gpiod_request(desc
, lh
->label
);
700 if (lflags
& GPIOHANDLE_REQUEST_ACTIVE_LOW
)
701 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
702 if (lflags
& GPIOHANDLE_REQUEST_OPEN_DRAIN
)
703 set_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
704 if (lflags
& GPIOHANDLE_REQUEST_OPEN_SOURCE
)
705 set_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
706 if (lflags
& GPIOHANDLE_REQUEST_BIAS_DISABLE
)
707 set_bit(FLAG_BIAS_DISABLE
, &desc
->flags
);
708 if (lflags
& GPIOHANDLE_REQUEST_BIAS_PULL_DOWN
)
709 set_bit(FLAG_PULL_DOWN
, &desc
->flags
);
710 if (lflags
& GPIOHANDLE_REQUEST_BIAS_PULL_UP
)
711 set_bit(FLAG_PULL_UP
, &desc
->flags
);
713 ret
= gpiod_set_transitory(desc
, false);
718 * Lines have to be requested explicitly for input
719 * or output, else the line will be treated "as is".
721 if (lflags
& GPIOHANDLE_REQUEST_OUTPUT
) {
722 int val
= !!handlereq
.default_values
[i
];
724 ret
= gpiod_direction_output(desc
, val
);
727 } else if (lflags
& GPIOHANDLE_REQUEST_INPUT
) {
728 ret
= gpiod_direction_input(desc
);
732 dev_dbg(&gdev
->dev
, "registered chardev handle for line %d\n",
735 /* Let i point at the last handle */
737 lh
->numdescs
= handlereq
.lines
;
739 fd
= get_unused_fd_flags(O_RDONLY
| O_CLOEXEC
);
745 file
= anon_inode_getfile("gpio-linehandle",
748 O_RDONLY
| O_CLOEXEC
);
751 goto out_put_unused_fd
;
755 if (copy_to_user(ip
, &handlereq
, sizeof(handlereq
))) {
757 * fput() will trigger the release() callback, so do not go onto
758 * the regular error cleanup path here.
765 fd_install(fd
, file
);
767 dev_dbg(&gdev
->dev
, "registered chardev handle for %d lines\n",
775 for (i
= 0; i
< count
; i
++)
776 gpiod_free(lh
->descs
[i
]);
780 put_device(&gdev
->dev
);
785 * GPIO line event management
789 * struct lineevent_state - contains the state of a userspace event
790 * @gdev: the GPIO device the event pertains to
791 * @label: consumer label used to tag descriptors
792 * @desc: the GPIO descriptor held by this event
793 * @eflags: the event flags this line was requested with
794 * @irq: the interrupt that trigger in response to events on this GPIO
795 * @wait: wait queue that handles blocking reads of events
796 * @events: KFIFO for the GPIO events
797 * @timestamp: cache for the timestamp storing it between hardirq
798 * and IRQ thread, used to bring the timestamp close to the actual
801 struct lineevent_state
{
802 struct gpio_device
*gdev
;
804 struct gpio_desc
*desc
;
807 wait_queue_head_t wait
;
808 DECLARE_KFIFO(events
, struct gpioevent_data
, 16);
812 #define GPIOEVENT_REQUEST_VALID_FLAGS \
813 (GPIOEVENT_REQUEST_RISING_EDGE | \
814 GPIOEVENT_REQUEST_FALLING_EDGE)
816 static __poll_t
lineevent_poll(struct file
*filep
,
817 struct poll_table_struct
*wait
)
819 struct lineevent_state
*le
= filep
->private_data
;
822 poll_wait(filep
, &le
->wait
, wait
);
824 if (!kfifo_is_empty_spinlocked_noirqsave(&le
->events
, &le
->wait
.lock
))
825 events
= EPOLLIN
| EPOLLRDNORM
;
831 static ssize_t
lineevent_read(struct file
*filep
,
836 struct lineevent_state
*le
= filep
->private_data
;
837 struct gpioevent_data ge
;
838 ssize_t bytes_read
= 0;
841 if (count
< sizeof(ge
))
845 spin_lock(&le
->wait
.lock
);
846 if (kfifo_is_empty(&le
->events
)) {
848 spin_unlock(&le
->wait
.lock
);
852 if (filep
->f_flags
& O_NONBLOCK
) {
853 spin_unlock(&le
->wait
.lock
);
857 ret
= wait_event_interruptible_locked(le
->wait
,
858 !kfifo_is_empty(&le
->events
));
860 spin_unlock(&le
->wait
.lock
);
865 ret
= kfifo_out(&le
->events
, &ge
, 1);
866 spin_unlock(&le
->wait
.lock
);
869 * This should never happen - we were holding the lock
870 * from the moment we learned the fifo is no longer
877 if (copy_to_user(buf
+ bytes_read
, &ge
, sizeof(ge
)))
879 bytes_read
+= sizeof(ge
);
880 } while (count
>= bytes_read
+ sizeof(ge
));
885 static int lineevent_release(struct inode
*inode
, struct file
*filep
)
887 struct lineevent_state
*le
= filep
->private_data
;
888 struct gpio_device
*gdev
= le
->gdev
;
890 free_irq(le
->irq
, le
);
891 gpiod_free(le
->desc
);
894 put_device(&gdev
->dev
);
898 static long lineevent_ioctl(struct file
*filep
, unsigned int cmd
,
901 struct lineevent_state
*le
= filep
->private_data
;
902 void __user
*ip
= (void __user
*)arg
;
903 struct gpiohandle_data ghd
;
906 * We can get the value for an event line but not set it,
907 * because it is input by definition.
909 if (cmd
== GPIOHANDLE_GET_LINE_VALUES_IOCTL
) {
912 memset(&ghd
, 0, sizeof(ghd
));
914 val
= gpiod_get_value_cansleep(le
->desc
);
919 if (copy_to_user(ip
, &ghd
, sizeof(ghd
)))
928 static long lineevent_ioctl_compat(struct file
*filep
, unsigned int cmd
,
931 return lineevent_ioctl(filep
, cmd
, (unsigned long)compat_ptr(arg
));
935 static const struct file_operations lineevent_fileops
= {
936 .release
= lineevent_release
,
937 .read
= lineevent_read
,
938 .poll
= lineevent_poll
,
939 .owner
= THIS_MODULE
,
940 .llseek
= noop_llseek
,
941 .unlocked_ioctl
= lineevent_ioctl
,
943 .compat_ioctl
= lineevent_ioctl_compat
,
947 static irqreturn_t
lineevent_irq_thread(int irq
, void *p
)
949 struct lineevent_state
*le
= p
;
950 struct gpioevent_data ge
;
953 /* Do not leak kernel stack to userspace */
954 memset(&ge
, 0, sizeof(ge
));
957 * We may be running from a nested threaded interrupt in which case
958 * we didn't get the timestamp from lineevent_irq_handler().
961 ge
.timestamp
= ktime_get_ns();
963 ge
.timestamp
= le
->timestamp
;
965 if (le
->eflags
& GPIOEVENT_REQUEST_RISING_EDGE
966 && le
->eflags
& GPIOEVENT_REQUEST_FALLING_EDGE
) {
967 int level
= gpiod_get_value_cansleep(le
->desc
);
969 /* Emit low-to-high event */
970 ge
.id
= GPIOEVENT_EVENT_RISING_EDGE
;
972 /* Emit high-to-low event */
973 ge
.id
= GPIOEVENT_EVENT_FALLING_EDGE
;
974 } else if (le
->eflags
& GPIOEVENT_REQUEST_RISING_EDGE
) {
975 /* Emit low-to-high event */
976 ge
.id
= GPIOEVENT_EVENT_RISING_EDGE
;
977 } else if (le
->eflags
& GPIOEVENT_REQUEST_FALLING_EDGE
) {
978 /* Emit high-to-low event */
979 ge
.id
= GPIOEVENT_EVENT_FALLING_EDGE
;
984 ret
= kfifo_in_spinlocked_noirqsave(&le
->events
, &ge
,
987 wake_up_poll(&le
->wait
, EPOLLIN
);
989 pr_debug_ratelimited("event FIFO is full - event dropped\n");
994 static irqreturn_t
lineevent_irq_handler(int irq
, void *p
)
996 struct lineevent_state
*le
= p
;
999 * Just store the timestamp in hardirq context so we get it as
1000 * close in time as possible to the actual event.
1002 le
->timestamp
= ktime_get_ns();
1004 return IRQ_WAKE_THREAD
;
1007 static int lineevent_create(struct gpio_device
*gdev
, void __user
*ip
)
1009 struct gpioevent_request eventreq
;
1010 struct lineevent_state
*le
;
1011 struct gpio_desc
*desc
;
1020 if (copy_from_user(&eventreq
, ip
, sizeof(eventreq
)))
1023 offset
= eventreq
.lineoffset
;
1024 lflags
= eventreq
.handleflags
;
1025 eflags
= eventreq
.eventflags
;
1027 desc
= gpiochip_get_desc(gdev
->chip
, offset
);
1029 return PTR_ERR(desc
);
1031 /* Return an error if a unknown flag is set */
1032 if ((lflags
& ~GPIOHANDLE_REQUEST_VALID_FLAGS
) ||
1033 (eflags
& ~GPIOEVENT_REQUEST_VALID_FLAGS
))
1036 /* This is just wrong: we don't look for events on output lines */
1037 if ((lflags
& GPIOHANDLE_REQUEST_OUTPUT
) ||
1038 (lflags
& GPIOHANDLE_REQUEST_OPEN_DRAIN
) ||
1039 (lflags
& GPIOHANDLE_REQUEST_OPEN_SOURCE
))
1042 /* Only one bias flag can be set. */
1043 if (((lflags
& GPIOHANDLE_REQUEST_BIAS_DISABLE
) &&
1044 (lflags
& (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN
|
1045 GPIOHANDLE_REQUEST_BIAS_PULL_UP
))) ||
1046 ((lflags
& GPIOHANDLE_REQUEST_BIAS_PULL_DOWN
) &&
1047 (lflags
& GPIOHANDLE_REQUEST_BIAS_PULL_UP
)))
1050 le
= kzalloc(sizeof(*le
), GFP_KERNEL
);
1054 get_device(&gdev
->dev
);
1056 /* Make sure this is terminated */
1057 eventreq
.consumer_label
[sizeof(eventreq
.consumer_label
)-1] = '\0';
1058 if (strlen(eventreq
.consumer_label
)) {
1059 le
->label
= kstrdup(eventreq
.consumer_label
,
1067 ret
= gpiod_request(desc
, le
->label
);
1069 goto out_free_label
;
1071 le
->eflags
= eflags
;
1073 if (lflags
& GPIOHANDLE_REQUEST_ACTIVE_LOW
)
1074 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
1075 if (lflags
& GPIOHANDLE_REQUEST_BIAS_DISABLE
)
1076 set_bit(FLAG_BIAS_DISABLE
, &desc
->flags
);
1077 if (lflags
& GPIOHANDLE_REQUEST_BIAS_PULL_DOWN
)
1078 set_bit(FLAG_PULL_DOWN
, &desc
->flags
);
1079 if (lflags
& GPIOHANDLE_REQUEST_BIAS_PULL_UP
)
1080 set_bit(FLAG_PULL_UP
, &desc
->flags
);
1082 ret
= gpiod_direction_input(desc
);
1086 le
->irq
= gpiod_to_irq(desc
);
1092 if (eflags
& GPIOEVENT_REQUEST_RISING_EDGE
)
1093 irqflags
|= test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
) ?
1094 IRQF_TRIGGER_FALLING
: IRQF_TRIGGER_RISING
;
1095 if (eflags
& GPIOEVENT_REQUEST_FALLING_EDGE
)
1096 irqflags
|= test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
) ?
1097 IRQF_TRIGGER_RISING
: IRQF_TRIGGER_FALLING
;
1098 irqflags
|= IRQF_ONESHOT
;
1100 INIT_KFIFO(le
->events
);
1101 init_waitqueue_head(&le
->wait
);
1103 /* Request a thread to read the events */
1104 ret
= request_threaded_irq(le
->irq
,
1105 lineevent_irq_handler
,
1106 lineevent_irq_thread
,
1113 fd
= get_unused_fd_flags(O_RDONLY
| O_CLOEXEC
);
1119 file
= anon_inode_getfile("gpio-event",
1122 O_RDONLY
| O_CLOEXEC
);
1124 ret
= PTR_ERR(file
);
1125 goto out_put_unused_fd
;
1129 if (copy_to_user(ip
, &eventreq
, sizeof(eventreq
))) {
1131 * fput() will trigger the release() callback, so do not go onto
1132 * the regular error cleanup path here.
1139 fd_install(fd
, file
);
1146 free_irq(le
->irq
, le
);
1148 gpiod_free(le
->desc
);
1153 put_device(&gdev
->dev
);
1157 static void gpio_desc_to_lineinfo(struct gpio_desc
*desc
,
1158 struct gpioline_info
*info
)
1160 struct gpio_chip
*gc
= desc
->gdev
->chip
;
1161 unsigned long flags
;
1163 spin_lock_irqsave(&gpio_lock
, flags
);
1166 strncpy(info
->name
, desc
->name
, sizeof(info
->name
));
1167 info
->name
[sizeof(info
->name
) - 1] = '\0';
1169 info
->name
[0] = '\0';
1173 strncpy(info
->consumer
, desc
->label
, sizeof(info
->consumer
));
1174 info
->consumer
[sizeof(info
->consumer
) - 1] = '\0';
1176 info
->consumer
[0] = '\0';
1180 * Userspace only need to know that the kernel is using this GPIO so
1184 if (test_bit(FLAG_REQUESTED
, &desc
->flags
) ||
1185 test_bit(FLAG_IS_HOGGED
, &desc
->flags
) ||
1186 test_bit(FLAG_USED_AS_IRQ
, &desc
->flags
) ||
1187 test_bit(FLAG_EXPORT
, &desc
->flags
) ||
1188 test_bit(FLAG_SYSFS
, &desc
->flags
) ||
1189 !pinctrl_gpio_can_use_line(gc
->base
+ info
->line_offset
))
1190 info
->flags
|= GPIOLINE_FLAG_KERNEL
;
1191 if (test_bit(FLAG_IS_OUT
, &desc
->flags
))
1192 info
->flags
|= GPIOLINE_FLAG_IS_OUT
;
1193 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1194 info
->flags
|= GPIOLINE_FLAG_ACTIVE_LOW
;
1195 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
))
1196 info
->flags
|= (GPIOLINE_FLAG_OPEN_DRAIN
|
1197 GPIOLINE_FLAG_IS_OUT
);
1198 if (test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
))
1199 info
->flags
|= (GPIOLINE_FLAG_OPEN_SOURCE
|
1200 GPIOLINE_FLAG_IS_OUT
);
1201 if (test_bit(FLAG_BIAS_DISABLE
, &desc
->flags
))
1202 info
->flags
|= GPIOLINE_FLAG_BIAS_DISABLE
;
1203 if (test_bit(FLAG_PULL_DOWN
, &desc
->flags
))
1204 info
->flags
|= GPIOLINE_FLAG_BIAS_PULL_DOWN
;
1205 if (test_bit(FLAG_PULL_UP
, &desc
->flags
))
1206 info
->flags
|= GPIOLINE_FLAG_BIAS_PULL_UP
;
1208 spin_unlock_irqrestore(&gpio_lock
, flags
);
1211 struct gpio_chardev_data
{
1212 struct gpio_device
*gdev
;
1213 wait_queue_head_t wait
;
1214 DECLARE_KFIFO(events
, struct gpioline_info_changed
, 32);
1215 struct notifier_block lineinfo_changed_nb
;
1216 unsigned long *watched_lines
;
1220 * gpio_ioctl() - ioctl handler for the GPIO chardev
1222 static long gpio_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
1224 struct gpio_chardev_data
*priv
= filp
->private_data
;
1225 struct gpio_device
*gdev
= priv
->gdev
;
1226 struct gpio_chip
*gc
= gdev
->chip
;
1227 void __user
*ip
= (void __user
*)arg
;
1228 struct gpio_desc
*desc
;
1231 /* We fail any subsequent ioctl():s when the chip is gone */
1235 /* Fill in the struct and pass to userspace */
1236 if (cmd
== GPIO_GET_CHIPINFO_IOCTL
) {
1237 struct gpiochip_info chipinfo
;
1239 memset(&chipinfo
, 0, sizeof(chipinfo
));
1241 strncpy(chipinfo
.name
, dev_name(&gdev
->dev
),
1242 sizeof(chipinfo
.name
));
1243 chipinfo
.name
[sizeof(chipinfo
.name
)-1] = '\0';
1244 strncpy(chipinfo
.label
, gdev
->label
,
1245 sizeof(chipinfo
.label
));
1246 chipinfo
.label
[sizeof(chipinfo
.label
)-1] = '\0';
1247 chipinfo
.lines
= gdev
->ngpio
;
1248 if (copy_to_user(ip
, &chipinfo
, sizeof(chipinfo
)))
1251 } else if (cmd
== GPIO_GET_LINEINFO_IOCTL
||
1252 cmd
== GPIO_GET_LINEINFO_WATCH_IOCTL
) {
1253 struct gpioline_info lineinfo
;
1255 if (copy_from_user(&lineinfo
, ip
, sizeof(lineinfo
)))
1258 desc
= gpiochip_get_desc(gc
, lineinfo
.line_offset
);
1260 return PTR_ERR(desc
);
1262 gpio_desc_to_lineinfo(desc
, &lineinfo
);
1264 if (copy_to_user(ip
, &lineinfo
, sizeof(lineinfo
)))
1267 if (cmd
== GPIO_GET_LINEINFO_WATCH_IOCTL
)
1268 set_bit(gpio_chip_hwgpio(desc
), priv
->watched_lines
);
1271 } else if (cmd
== GPIO_GET_LINEHANDLE_IOCTL
) {
1272 return linehandle_create(gdev
, ip
);
1273 } else if (cmd
== GPIO_GET_LINEEVENT_IOCTL
) {
1274 return lineevent_create(gdev
, ip
);
1275 } else if (cmd
== GPIO_GET_LINEINFO_UNWATCH_IOCTL
) {
1276 if (copy_from_user(&offset
, ip
, sizeof(offset
)))
1279 desc
= gpiochip_get_desc(gc
, offset
);
1281 return PTR_ERR(desc
);
1283 clear_bit(gpio_chip_hwgpio(desc
), priv
->watched_lines
);
1289 #ifdef CONFIG_COMPAT
1290 static long gpio_ioctl_compat(struct file
*filp
, unsigned int cmd
,
1293 return gpio_ioctl(filp
, cmd
, (unsigned long)compat_ptr(arg
));
1297 static struct gpio_chardev_data
*
1298 to_gpio_chardev_data(struct notifier_block
*nb
)
1300 return container_of(nb
, struct gpio_chardev_data
, lineinfo_changed_nb
);
1303 static int lineinfo_changed_notify(struct notifier_block
*nb
,
1304 unsigned long action
, void *data
)
1306 struct gpio_chardev_data
*priv
= to_gpio_chardev_data(nb
);
1307 struct gpioline_info_changed chg
;
1308 struct gpio_desc
*desc
= data
;
1311 if (!test_bit(gpio_chip_hwgpio(desc
), priv
->watched_lines
))
1314 memset(&chg
, 0, sizeof(chg
));
1315 chg
.info
.line_offset
= gpio_chip_hwgpio(desc
);
1316 chg
.event_type
= action
;
1317 chg
.timestamp
= ktime_get_ns();
1318 gpio_desc_to_lineinfo(desc
, &chg
.info
);
1320 ret
= kfifo_in_spinlocked(&priv
->events
, &chg
, 1, &priv
->wait
.lock
);
1322 wake_up_poll(&priv
->wait
, EPOLLIN
);
1324 pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
1329 static __poll_t
lineinfo_watch_poll(struct file
*filep
,
1330 struct poll_table_struct
*pollt
)
1332 struct gpio_chardev_data
*priv
= filep
->private_data
;
1333 __poll_t events
= 0;
1335 poll_wait(filep
, &priv
->wait
, pollt
);
1337 if (!kfifo_is_empty_spinlocked_noirqsave(&priv
->events
,
1339 events
= EPOLLIN
| EPOLLRDNORM
;
1344 static ssize_t
lineinfo_watch_read(struct file
*filep
, char __user
*buf
,
1345 size_t count
, loff_t
*off
)
1347 struct gpio_chardev_data
*priv
= filep
->private_data
;
1348 struct gpioline_info_changed event
;
1349 ssize_t bytes_read
= 0;
1352 if (count
< sizeof(event
))
1356 spin_lock(&priv
->wait
.lock
);
1357 if (kfifo_is_empty(&priv
->events
)) {
1359 spin_unlock(&priv
->wait
.lock
);
1363 if (filep
->f_flags
& O_NONBLOCK
) {
1364 spin_unlock(&priv
->wait
.lock
);
1368 ret
= wait_event_interruptible_locked(priv
->wait
,
1369 !kfifo_is_empty(&priv
->events
));
1371 spin_unlock(&priv
->wait
.lock
);
1376 ret
= kfifo_out(&priv
->events
, &event
, 1);
1377 spin_unlock(&priv
->wait
.lock
);
1381 /* We should never get here. See lineevent_read(). */
1384 if (copy_to_user(buf
+ bytes_read
, &event
, sizeof(event
)))
1386 bytes_read
+= sizeof(event
);
1387 } while (count
>= bytes_read
+ sizeof(event
));
1393 * gpio_chrdev_open() - open the chardev for ioctl operations
1394 * @inode: inode for this chardev
1395 * @filp: file struct for storing private data
1396 * Returns 0 on success
1398 static int gpio_chrdev_open(struct inode
*inode
, struct file
*filp
)
1400 struct gpio_device
*gdev
= container_of(inode
->i_cdev
,
1401 struct gpio_device
, chrdev
);
1402 struct gpio_chardev_data
*priv
;
1405 /* Fail on open if the backing gpiochip is gone */
1409 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
1413 priv
->watched_lines
= bitmap_zalloc(gdev
->chip
->ngpio
, GFP_KERNEL
);
1414 if (!priv
->watched_lines
)
1417 init_waitqueue_head(&priv
->wait
);
1418 INIT_KFIFO(priv
->events
);
1421 priv
->lineinfo_changed_nb
.notifier_call
= lineinfo_changed_notify
;
1422 ret
= atomic_notifier_chain_register(&gdev
->notifier
,
1423 &priv
->lineinfo_changed_nb
);
1425 goto out_free_bitmap
;
1427 get_device(&gdev
->dev
);
1428 filp
->private_data
= priv
;
1430 ret
= nonseekable_open(inode
, filp
);
1432 goto out_unregister_notifier
;
1436 out_unregister_notifier
:
1437 atomic_notifier_chain_unregister(&gdev
->notifier
,
1438 &priv
->lineinfo_changed_nb
);
1440 bitmap_free(priv
->watched_lines
);
1447 * gpio_chrdev_release() - close chardev after ioctl operations
1448 * @inode: inode for this chardev
1449 * @filp: file struct for storing private data
1450 * Returns 0 on success
1452 static int gpio_chrdev_release(struct inode
*inode
, struct file
*filp
)
1454 struct gpio_chardev_data
*priv
= filp
->private_data
;
1455 struct gpio_device
*gdev
= priv
->gdev
;
1457 bitmap_free(priv
->watched_lines
);
1458 atomic_notifier_chain_unregister(&gdev
->notifier
,
1459 &priv
->lineinfo_changed_nb
);
1460 put_device(&gdev
->dev
);
1466 static const struct file_operations gpio_fileops
= {
1467 .release
= gpio_chrdev_release
,
1468 .open
= gpio_chrdev_open
,
1469 .poll
= lineinfo_watch_poll
,
1470 .read
= lineinfo_watch_read
,
1471 .owner
= THIS_MODULE
,
1472 .llseek
= no_llseek
,
1473 .unlocked_ioctl
= gpio_ioctl
,
1474 #ifdef CONFIG_COMPAT
1475 .compat_ioctl
= gpio_ioctl_compat
,
1479 static void gpiodevice_release(struct device
*dev
)
1481 struct gpio_device
*gdev
= dev_get_drvdata(dev
);
1483 list_del(&gdev
->list
);
1484 ida_simple_remove(&gpio_ida
, gdev
->id
);
1485 kfree_const(gdev
->label
);
1490 static int gpiochip_setup_dev(struct gpio_device
*gdev
)
1494 cdev_init(&gdev
->chrdev
, &gpio_fileops
);
1495 gdev
->chrdev
.owner
= THIS_MODULE
;
1496 gdev
->dev
.devt
= MKDEV(MAJOR(gpio_devt
), gdev
->id
);
1498 ret
= cdev_device_add(&gdev
->chrdev
, &gdev
->dev
);
1502 chip_dbg(gdev
->chip
, "added GPIO chardev (%d:%d)\n",
1503 MAJOR(gpio_devt
), gdev
->id
);
1505 ret
= gpiochip_sysfs_register(gdev
);
1507 goto err_remove_device
;
1509 /* From this point, the .release() function cleans up gpio_device */
1510 gdev
->dev
.release
= gpiodevice_release
;
1511 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1512 __func__
, gdev
->base
, gdev
->base
+ gdev
->ngpio
- 1,
1513 dev_name(&gdev
->dev
), gdev
->chip
->label
? : "generic");
1518 cdev_device_del(&gdev
->chrdev
, &gdev
->dev
);
1522 static void gpiochip_machine_hog(struct gpio_chip
*gc
, struct gpiod_hog
*hog
)
1524 struct gpio_desc
*desc
;
1527 desc
= gpiochip_get_desc(gc
, hog
->chip_hwnum
);
1529 pr_err("%s: unable to get GPIO desc: %ld\n",
1530 __func__
, PTR_ERR(desc
));
1534 if (test_bit(FLAG_IS_HOGGED
, &desc
->flags
))
1537 rv
= gpiod_hog(desc
, hog
->line_name
, hog
->lflags
, hog
->dflags
);
1539 pr_err("%s: unable to hog GPIO line (%s:%u): %d\n",
1540 __func__
, gc
->label
, hog
->chip_hwnum
, rv
);
1543 static void machine_gpiochip_add(struct gpio_chip
*gc
)
1545 struct gpiod_hog
*hog
;
1547 mutex_lock(&gpio_machine_hogs_mutex
);
1549 list_for_each_entry(hog
, &gpio_machine_hogs
, list
) {
1550 if (!strcmp(gc
->label
, hog
->chip_label
))
1551 gpiochip_machine_hog(gc
, hog
);
1554 mutex_unlock(&gpio_machine_hogs_mutex
);
1557 static void gpiochip_setup_devs(void)
1559 struct gpio_device
*gdev
;
1562 list_for_each_entry(gdev
, &gpio_devices
, list
) {
1563 ret
= gpiochip_setup_dev(gdev
);
1565 pr_err("%s: Failed to initialize gpio device (%d)\n",
1566 dev_name(&gdev
->dev
), ret
);
1570 int gpiochip_add_data_with_key(struct gpio_chip
*gc
, void *data
,
1571 struct lock_class_key
*lock_key
,
1572 struct lock_class_key
*request_key
)
1574 unsigned long flags
;
1577 int base
= gc
->base
;
1578 struct gpio_device
*gdev
;
1581 * First: allocate and populate the internal stat container, and
1582 * set up the struct device.
1584 gdev
= kzalloc(sizeof(*gdev
), GFP_KERNEL
);
1587 gdev
->dev
.bus
= &gpio_bus_type
;
1591 gdev
->dev
.parent
= gc
->parent
;
1592 gdev
->dev
.of_node
= gc
->parent
->of_node
;
1595 #ifdef CONFIG_OF_GPIO
1596 /* If the gpiochip has an assigned OF node this takes precedence */
1598 gdev
->dev
.of_node
= gc
->of_node
;
1600 gc
->of_node
= gdev
->dev
.of_node
;
1603 gdev
->id
= ida_simple_get(&gpio_ida
, 0, 0, GFP_KERNEL
);
1608 dev_set_name(&gdev
->dev
, GPIOCHIP_NAME
"%d", gdev
->id
);
1609 device_initialize(&gdev
->dev
);
1610 dev_set_drvdata(&gdev
->dev
, gdev
);
1611 if (gc
->parent
&& gc
->parent
->driver
)
1612 gdev
->owner
= gc
->parent
->driver
->owner
;
1614 /* TODO: remove chip->owner */
1615 gdev
->owner
= gc
->owner
;
1617 gdev
->owner
= THIS_MODULE
;
1619 gdev
->descs
= kcalloc(gc
->ngpio
, sizeof(gdev
->descs
[0]), GFP_KERNEL
);
1625 if (gc
->ngpio
== 0) {
1626 chip_err(gc
, "tried to insert a GPIO chip with zero lines\n");
1628 goto err_free_descs
;
1631 if (gc
->ngpio
> FASTPATH_NGPIO
)
1632 chip_warn(gc
, "line cnt %u is greater than fast path cnt %u\n",
1633 gc
->ngpio
, FASTPATH_NGPIO
);
1635 gdev
->label
= kstrdup_const(gc
->label
?: "unknown", GFP_KERNEL
);
1638 goto err_free_descs
;
1641 gdev
->ngpio
= gc
->ngpio
;
1644 spin_lock_irqsave(&gpio_lock
, flags
);
1647 * TODO: this allocates a Linux GPIO number base in the global
1648 * GPIO numberspace for this chip. In the long run we want to
1649 * get *rid* of this numberspace and use only descriptors, but
1650 * it may be a pipe dream. It will not happen before we get rid
1651 * of the sysfs interface anyways.
1654 base
= gpiochip_find_base(gc
->ngpio
);
1657 spin_unlock_irqrestore(&gpio_lock
, flags
);
1658 goto err_free_label
;
1661 * TODO: it should not be necessary to reflect the assigned
1662 * base outside of the GPIO subsystem. Go over drivers and
1663 * see if anyone makes use of this, else drop this and assign
1670 ret
= gpiodev_add_to_list(gdev
);
1672 spin_unlock_irqrestore(&gpio_lock
, flags
);
1673 goto err_free_label
;
1676 for (i
= 0; i
< gc
->ngpio
; i
++)
1677 gdev
->descs
[i
].gdev
= gdev
;
1679 spin_unlock_irqrestore(&gpio_lock
, flags
);
1681 ATOMIC_INIT_NOTIFIER_HEAD(&gdev
->notifier
);
1683 #ifdef CONFIG_PINCTRL
1684 INIT_LIST_HEAD(&gdev
->pin_ranges
);
1687 ret
= gpiochip_set_desc_names(gc
);
1689 goto err_remove_from_list
;
1691 ret
= gpiochip_alloc_valid_mask(gc
);
1693 goto err_remove_from_list
;
1695 ret
= of_gpiochip_add(gc
);
1697 goto err_free_gpiochip_mask
;
1699 ret
= gpiochip_init_valid_mask(gc
);
1701 goto err_remove_of_chip
;
1703 for (i
= 0; i
< gc
->ngpio
; i
++) {
1704 struct gpio_desc
*desc
= &gdev
->descs
[i
];
1706 if (gc
->get_direction
&& gpiochip_line_is_valid(gc
, i
)) {
1707 assign_bit(FLAG_IS_OUT
,
1708 &desc
->flags
, !gc
->get_direction(gc
, i
));
1710 assign_bit(FLAG_IS_OUT
,
1711 &desc
->flags
, !gc
->direction_input
);
1715 ret
= gpiochip_add_pin_ranges(gc
);
1717 goto err_remove_of_chip
;
1719 acpi_gpiochip_add(gc
);
1721 machine_gpiochip_add(gc
);
1723 ret
= gpiochip_irqchip_init_valid_mask(gc
);
1725 goto err_remove_acpi_chip
;
1727 ret
= gpiochip_irqchip_init_hw(gc
);
1729 goto err_remove_acpi_chip
;
1731 ret
= gpiochip_add_irqchip(gc
, lock_key
, request_key
);
1733 goto err_remove_irqchip_mask
;
1736 * By first adding the chardev, and then adding the device,
1737 * we get a device node entry in sysfs under
1738 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1739 * coldplug of device nodes and other udev business.
1740 * We can do this only if gpiolib has been initialized.
1741 * Otherwise, defer until later.
1743 if (gpiolib_initialized
) {
1744 ret
= gpiochip_setup_dev(gdev
);
1746 goto err_remove_irqchip
;
1751 gpiochip_irqchip_remove(gc
);
1752 err_remove_irqchip_mask
:
1753 gpiochip_irqchip_free_valid_mask(gc
);
1754 err_remove_acpi_chip
:
1755 acpi_gpiochip_remove(gc
);
1757 gpiochip_free_hogs(gc
);
1758 of_gpiochip_remove(gc
);
1759 err_free_gpiochip_mask
:
1760 gpiochip_remove_pin_ranges(gc
);
1761 gpiochip_free_valid_mask(gc
);
1762 err_remove_from_list
:
1763 spin_lock_irqsave(&gpio_lock
, flags
);
1764 list_del(&gdev
->list
);
1765 spin_unlock_irqrestore(&gpio_lock
, flags
);
1767 kfree_const(gdev
->label
);
1771 ida_simple_remove(&gpio_ida
, gdev
->id
);
1773 /* failures here can mean systems won't boot... */
1774 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__
,
1775 gdev
->base
, gdev
->base
+ gdev
->ngpio
- 1,
1776 gc
->label
? : "generic", ret
);
1780 EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key
);
1783 * gpiochip_get_data() - get per-subdriver data for the chip
1787 * The per-subdriver data for the chip.
1789 void *gpiochip_get_data(struct gpio_chip
*gc
)
1791 return gc
->gpiodev
->data
;
1793 EXPORT_SYMBOL_GPL(gpiochip_get_data
);
1796 * gpiochip_remove() - unregister a gpio_chip
1797 * @gc: the chip to unregister
1799 * A gpio_chip with any GPIOs still requested may not be removed.
1801 void gpiochip_remove(struct gpio_chip
*gc
)
1803 struct gpio_device
*gdev
= gc
->gpiodev
;
1804 unsigned long flags
;
1807 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
1808 gpiochip_sysfs_unregister(gdev
);
1809 gpiochip_free_hogs(gc
);
1810 /* Numb the device, cancelling all outstanding operations */
1812 gpiochip_irqchip_remove(gc
);
1813 acpi_gpiochip_remove(gc
);
1814 of_gpiochip_remove(gc
);
1815 gpiochip_remove_pin_ranges(gc
);
1816 gpiochip_free_valid_mask(gc
);
1818 * We accept no more calls into the driver from this point, so
1819 * NULL the driver data pointer
1823 spin_lock_irqsave(&gpio_lock
, flags
);
1824 for (i
= 0; i
< gdev
->ngpio
; i
++) {
1825 if (gpiochip_is_requested(gc
, i
))
1828 spin_unlock_irqrestore(&gpio_lock
, flags
);
1830 if (i
!= gdev
->ngpio
)
1831 dev_crit(&gdev
->dev
,
1832 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
1835 * The gpiochip side puts its use of the device to rest here:
1836 * if there are no userspace clients, the chardev and device will
1837 * be removed, else it will be dangling until the last user is
1840 cdev_device_del(&gdev
->chrdev
, &gdev
->dev
);
1841 put_device(&gdev
->dev
);
1843 EXPORT_SYMBOL_GPL(gpiochip_remove
);
1846 * gpiochip_find() - iterator for locating a specific gpio_chip
1847 * @data: data to pass to match function
1848 * @match: Callback function to check gpio_chip
1850 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1851 * determined by a user supplied @match callback. The callback should return
1852 * 0 if the device doesn't match and non-zero if it does. If the callback is
1853 * non-zero, this function will return to the caller and not iterate over any
1856 struct gpio_chip
*gpiochip_find(void *data
,
1857 int (*match
)(struct gpio_chip
*gc
,
1860 struct gpio_device
*gdev
;
1861 struct gpio_chip
*gc
= NULL
;
1862 unsigned long flags
;
1864 spin_lock_irqsave(&gpio_lock
, flags
);
1865 list_for_each_entry(gdev
, &gpio_devices
, list
)
1866 if (gdev
->chip
&& match(gdev
->chip
, data
)) {
1871 spin_unlock_irqrestore(&gpio_lock
, flags
);
1875 EXPORT_SYMBOL_GPL(gpiochip_find
);
1877 static int gpiochip_match_name(struct gpio_chip
*gc
, void *data
)
1879 const char *name
= data
;
1881 return !strcmp(gc
->label
, name
);
1884 static struct gpio_chip
*find_chip_by_name(const char *name
)
1886 return gpiochip_find((void *)name
, gpiochip_match_name
);
1889 #ifdef CONFIG_GPIOLIB_IRQCHIP
1892 * The following is irqchip helper code for gpiochips.
1895 static int gpiochip_irqchip_init_hw(struct gpio_chip
*gc
)
1897 struct gpio_irq_chip
*girq
= &gc
->irq
;
1902 return girq
->init_hw(gc
);
1905 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip
*gc
)
1907 struct gpio_irq_chip
*girq
= &gc
->irq
;
1909 if (!girq
->init_valid_mask
)
1912 girq
->valid_mask
= gpiochip_allocate_mask(gc
);
1913 if (!girq
->valid_mask
)
1916 girq
->init_valid_mask(gc
, girq
->valid_mask
, gc
->ngpio
);
1921 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip
*gc
)
1923 bitmap_free(gc
->irq
.valid_mask
);
1924 gc
->irq
.valid_mask
= NULL
;
1927 bool gpiochip_irqchip_irq_valid(const struct gpio_chip
*gc
,
1928 unsigned int offset
)
1930 if (!gpiochip_line_is_valid(gc
, offset
))
1932 /* No mask means all valid */
1933 if (likely(!gc
->irq
.valid_mask
))
1935 return test_bit(offset
, gc
->irq
.valid_mask
);
1937 EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid
);
1940 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
1941 * @gc: the gpiochip to set the irqchip chain to
1942 * @parent_irq: the irq number corresponding to the parent IRQ for this
1944 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1945 * coming out of the gpiochip. If the interrupt is nested rather than
1946 * cascaded, pass NULL in this handler argument
1948 static void gpiochip_set_cascaded_irqchip(struct gpio_chip
*gc
,
1949 unsigned int parent_irq
,
1950 irq_flow_handler_t parent_handler
)
1952 struct gpio_irq_chip
*girq
= &gc
->irq
;
1953 struct device
*dev
= &gc
->gpiodev
->dev
;
1955 if (!girq
->domain
) {
1956 chip_err(gc
, "called %s before setting up irqchip\n",
1961 if (parent_handler
) {
1962 if (gc
->can_sleep
) {
1964 "you cannot have chained interrupts on a chip that may sleep\n");
1967 girq
->parents
= devm_kcalloc(dev
, 1,
1968 sizeof(*girq
->parents
),
1970 if (!girq
->parents
) {
1971 chip_err(gc
, "out of memory allocating parent IRQ\n");
1974 girq
->parents
[0] = parent_irq
;
1975 girq
->num_parents
= 1;
1977 * The parent irqchip is already using the chip_data for this
1978 * irqchip, so our callbacks simply use the handler_data.
1980 irq_set_chained_handler_and_data(parent_irq
, parent_handler
,
1986 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1987 * @gc: the gpiochip to set the irqchip nested handler to
1988 * @irqchip: the irqchip to nest to the gpiochip
1989 * @parent_irq: the irq number corresponding to the parent IRQ for this
1992 void gpiochip_set_nested_irqchip(struct gpio_chip
*gc
,
1993 struct irq_chip
*irqchip
,
1994 unsigned int parent_irq
)
1996 gpiochip_set_cascaded_irqchip(gc
, parent_irq
, NULL
);
1998 EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip
);
2000 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2003 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
2005 * @gc: the gpiochip to set the irqchip hierarchical handler to
2006 * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
2007 * will then percolate up to the parent
2009 static void gpiochip_set_hierarchical_irqchip(struct gpio_chip
*gc
,
2010 struct irq_chip
*irqchip
)
2012 /* DT will deal with mapping each IRQ as we go along */
2013 if (is_of_node(gc
->irq
.fwnode
))
2017 * This is for legacy and boardfile "irqchip" fwnodes: allocate
2018 * irqs upfront instead of dynamically since we don't have the
2019 * dynamic type of allocation that hardware description languages
2020 * provide. Once all GPIO drivers using board files are gone from
2021 * the kernel we can delete this code, but for a transitional period
2022 * it is necessary to keep this around.
2024 if (is_fwnode_irqchip(gc
->irq
.fwnode
)) {
2028 for (i
= 0; i
< gc
->ngpio
; i
++) {
2029 struct irq_fwspec fwspec
;
2030 unsigned int parent_hwirq
;
2031 unsigned int parent_type
;
2032 struct gpio_irq_chip
*girq
= &gc
->irq
;
2035 * We call the child to parent translation function
2036 * only to check if the child IRQ is valid or not.
2037 * Just pick the rising edge type here as that is what
2038 * we likely need to support.
2040 ret
= girq
->child_to_parent_hwirq(gc
, i
,
2041 IRQ_TYPE_EDGE_RISING
,
2045 chip_err(gc
, "skip set-up on hwirq %d\n",
2050 fwspec
.fwnode
= gc
->irq
.fwnode
;
2051 /* This is the hwirq for the GPIO line side of things */
2052 fwspec
.param
[0] = girq
->child_offset_to_irq(gc
, i
);
2053 /* Just pick something */
2054 fwspec
.param
[1] = IRQ_TYPE_EDGE_RISING
;
2055 fwspec
.param_count
= 2;
2056 ret
= __irq_domain_alloc_irqs(gc
->irq
.domain
,
2057 /* just pick something */
2066 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
2073 chip_err(gc
, "%s unknown fwnode type proceed anyway\n", __func__
);
2078 static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain
*d
,
2079 struct irq_fwspec
*fwspec
,
2080 unsigned long *hwirq
,
2083 /* We support standard DT translation */
2084 if (is_of_node(fwspec
->fwnode
) && fwspec
->param_count
== 2) {
2085 return irq_domain_translate_twocell(d
, fwspec
, hwirq
, type
);
2088 /* This is for board files and others not using DT */
2089 if (is_fwnode_irqchip(fwspec
->fwnode
)) {
2092 ret
= irq_domain_translate_twocell(d
, fwspec
, hwirq
, type
);
2095 WARN_ON(*type
== IRQ_TYPE_NONE
);
2101 static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain
*d
,
2103 unsigned int nr_irqs
,
2106 struct gpio_chip
*gc
= d
->host_data
;
2107 irq_hw_number_t hwirq
;
2108 unsigned int type
= IRQ_TYPE_NONE
;
2109 struct irq_fwspec
*fwspec
= data
;
2111 unsigned int parent_hwirq
;
2112 unsigned int parent_type
;
2113 struct gpio_irq_chip
*girq
= &gc
->irq
;
2117 * The nr_irqs parameter is always one except for PCI multi-MSI
2118 * so this should not happen.
2120 WARN_ON(nr_irqs
!= 1);
2122 ret
= gc
->irq
.child_irq_domain_ops
.translate(d
, fwspec
, &hwirq
, &type
);
2126 chip_dbg(gc
, "allocate IRQ %d, hwirq %lu\n", irq
, hwirq
);
2128 ret
= girq
->child_to_parent_hwirq(gc
, hwirq
, type
,
2129 &parent_hwirq
, &parent_type
);
2131 chip_err(gc
, "can't look up hwirq %lu\n", hwirq
);
2134 chip_dbg(gc
, "found parent hwirq %u\n", parent_hwirq
);
2137 * We set handle_bad_irq because the .set_type() should
2138 * always be invoked and set the right type of handler.
2140 irq_domain_set_info(d
,
2149 /* This parent only handles asserted level IRQs */
2150 parent_arg
= girq
->populate_parent_alloc_arg(gc
, parent_hwirq
, parent_type
);
2154 chip_dbg(gc
, "alloc_irqs_parent for %d parent hwirq %d\n",
2156 irq_set_lockdep_class(irq
, gc
->irq
.lock_key
, gc
->irq
.request_key
);
2157 ret
= irq_domain_alloc_irqs_parent(d
, irq
, 1, parent_arg
);
2159 * If the parent irqdomain is msi, the interrupts have already
2160 * been allocated, so the EEXIST is good.
2162 if (irq_domain_is_msi(d
->parent
) && (ret
== -EEXIST
))
2166 "failed to allocate parent hwirq %d for hwirq %lu\n",
2167 parent_hwirq
, hwirq
);
2173 static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip
*gc
,
2174 unsigned int offset
)
2179 static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops
*ops
)
2181 ops
->activate
= gpiochip_irq_domain_activate
;
2182 ops
->deactivate
= gpiochip_irq_domain_deactivate
;
2183 ops
->alloc
= gpiochip_hierarchy_irq_domain_alloc
;
2184 ops
->free
= irq_domain_free_irqs_common
;
2187 * We only allow overriding the translate() function for
2188 * hierarchical chips, and this should only be done if the user
2189 * really need something other than 1:1 translation.
2191 if (!ops
->translate
)
2192 ops
->translate
= gpiochip_hierarchy_irq_domain_translate
;
2195 static int gpiochip_hierarchy_add_domain(struct gpio_chip
*gc
)
2197 if (!gc
->irq
.child_to_parent_hwirq
||
2199 chip_err(gc
, "missing irqdomain vital data\n");
2203 if (!gc
->irq
.child_offset_to_irq
)
2204 gc
->irq
.child_offset_to_irq
= gpiochip_child_offset_to_irq_noop
;
2206 if (!gc
->irq
.populate_parent_alloc_arg
)
2207 gc
->irq
.populate_parent_alloc_arg
=
2208 gpiochip_populate_parent_fwspec_twocell
;
2210 gpiochip_hierarchy_setup_domain_ops(&gc
->irq
.child_irq_domain_ops
);
2212 gc
->irq
.domain
= irq_domain_create_hierarchy(
2213 gc
->irq
.parent_domain
,
2217 &gc
->irq
.child_irq_domain_ops
,
2220 if (!gc
->irq
.domain
)
2223 gpiochip_set_hierarchical_irqchip(gc
, gc
->irq
.chip
);
2228 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip
*gc
)
2230 return !!gc
->irq
.parent_domain
;
2233 void *gpiochip_populate_parent_fwspec_twocell(struct gpio_chip
*gc
,
2234 unsigned int parent_hwirq
,
2235 unsigned int parent_type
)
2237 struct irq_fwspec
*fwspec
;
2239 fwspec
= kmalloc(sizeof(*fwspec
), GFP_KERNEL
);
2243 fwspec
->fwnode
= gc
->irq
.parent_domain
->fwnode
;
2244 fwspec
->param_count
= 2;
2245 fwspec
->param
[0] = parent_hwirq
;
2246 fwspec
->param
[1] = parent_type
;
2250 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell
);
2252 void *gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip
*gc
,
2253 unsigned int parent_hwirq
,
2254 unsigned int parent_type
)
2256 struct irq_fwspec
*fwspec
;
2258 fwspec
= kmalloc(sizeof(*fwspec
), GFP_KERNEL
);
2262 fwspec
->fwnode
= gc
->irq
.parent_domain
->fwnode
;
2263 fwspec
->param_count
= 4;
2264 fwspec
->param
[0] = 0;
2265 fwspec
->param
[1] = parent_hwirq
;
2266 fwspec
->param
[2] = 0;
2267 fwspec
->param
[3] = parent_type
;
2271 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell
);
2275 static int gpiochip_hierarchy_add_domain(struct gpio_chip
*gc
)
2280 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip
*gc
)
2285 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
2288 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
2289 * @d: the irqdomain used by this irqchip
2290 * @irq: the global irq number used by this GPIO irqchip irq
2291 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
2293 * This function will set up the mapping for a certain IRQ line on a
2294 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
2295 * stored inside the gpiochip.
2297 int gpiochip_irq_map(struct irq_domain
*d
, unsigned int irq
,
2298 irq_hw_number_t hwirq
)
2300 struct gpio_chip
*gc
= d
->host_data
;
2303 if (!gpiochip_irqchip_irq_valid(gc
, hwirq
))
2306 irq_set_chip_data(irq
, gc
);
2308 * This lock class tells lockdep that GPIO irqs are in a different
2309 * category than their parents, so it won't report false recursion.
2311 irq_set_lockdep_class(irq
, gc
->irq
.lock_key
, gc
->irq
.request_key
);
2312 irq_set_chip_and_handler(irq
, gc
->irq
.chip
, gc
->irq
.handler
);
2313 /* Chips that use nested thread handlers have them marked */
2314 if (gc
->irq
.threaded
)
2315 irq_set_nested_thread(irq
, 1);
2316 irq_set_noprobe(irq
);
2318 if (gc
->irq
.num_parents
== 1)
2319 ret
= irq_set_parent(irq
, gc
->irq
.parents
[0]);
2320 else if (gc
->irq
.map
)
2321 ret
= irq_set_parent(irq
, gc
->irq
.map
[hwirq
]);
2327 * No set-up of the hardware will happen if IRQ_TYPE_NONE
2328 * is passed as default type.
2330 if (gc
->irq
.default_type
!= IRQ_TYPE_NONE
)
2331 irq_set_irq_type(irq
, gc
->irq
.default_type
);
2335 EXPORT_SYMBOL_GPL(gpiochip_irq_map
);
2337 void gpiochip_irq_unmap(struct irq_domain
*d
, unsigned int irq
)
2339 struct gpio_chip
*gc
= d
->host_data
;
2341 if (gc
->irq
.threaded
)
2342 irq_set_nested_thread(irq
, 0);
2343 irq_set_chip_and_handler(irq
, NULL
, NULL
);
2344 irq_set_chip_data(irq
, NULL
);
2346 EXPORT_SYMBOL_GPL(gpiochip_irq_unmap
);
2348 static const struct irq_domain_ops gpiochip_domain_ops
= {
2349 .map
= gpiochip_irq_map
,
2350 .unmap
= gpiochip_irq_unmap
,
2351 /* Virtually all GPIO irqchips are twocell:ed */
2352 .xlate
= irq_domain_xlate_twocell
,
2356 * TODO: move these activate/deactivate in under the hierarchicial
2357 * irqchip implementation as static once SPMI and SSBI (all external
2358 * users) are phased over.
2361 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
2362 * @domain: The IRQ domain used by this IRQ chip
2363 * @data: Outermost irq_data associated with the IRQ
2364 * @reserve: If set, only reserve an interrupt vector instead of assigning one
2366 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
2367 * used as the activate function for the &struct irq_domain_ops. The host_data
2368 * for the IRQ domain must be the &struct gpio_chip.
2370 int gpiochip_irq_domain_activate(struct irq_domain
*domain
,
2371 struct irq_data
*data
, bool reserve
)
2373 struct gpio_chip
*gc
= domain
->host_data
;
2375 return gpiochip_lock_as_irq(gc
, data
->hwirq
);
2377 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate
);
2380 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
2381 * @domain: The IRQ domain used by this IRQ chip
2382 * @data: Outermost irq_data associated with the IRQ
2384 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
2385 * be used as the deactivate function for the &struct irq_domain_ops. The
2386 * host_data for the IRQ domain must be the &struct gpio_chip.
2388 void gpiochip_irq_domain_deactivate(struct irq_domain
*domain
,
2389 struct irq_data
*data
)
2391 struct gpio_chip
*gc
= domain
->host_data
;
2393 return gpiochip_unlock_as_irq(gc
, data
->hwirq
);
2395 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate
);
2397 static int gpiochip_to_irq(struct gpio_chip
*gc
, unsigned offset
)
2399 struct irq_domain
*domain
= gc
->irq
.domain
;
2401 if (!gpiochip_irqchip_irq_valid(gc
, offset
))
2404 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2405 if (irq_domain_is_hierarchy(domain
)) {
2406 struct irq_fwspec spec
;
2408 spec
.fwnode
= domain
->fwnode
;
2409 spec
.param_count
= 2;
2410 spec
.param
[0] = gc
->irq
.child_offset_to_irq(gc
, offset
);
2411 spec
.param
[1] = IRQ_TYPE_NONE
;
2413 return irq_create_fwspec_mapping(&spec
);
2417 return irq_create_mapping(domain
, offset
);
2420 static int gpiochip_irq_reqres(struct irq_data
*d
)
2422 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
2424 return gpiochip_reqres_irq(gc
, d
->hwirq
);
2427 static void gpiochip_irq_relres(struct irq_data
*d
)
2429 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
2431 gpiochip_relres_irq(gc
, d
->hwirq
);
2434 static void gpiochip_irq_enable(struct irq_data
*d
)
2436 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
2438 gpiochip_enable_irq(gc
, d
->hwirq
);
2439 if (gc
->irq
.irq_enable
)
2440 gc
->irq
.irq_enable(d
);
2442 gc
->irq
.chip
->irq_unmask(d
);
2445 static void gpiochip_irq_disable(struct irq_data
*d
)
2447 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
2450 * Since we override .irq_disable() we need to mimic the
2451 * behaviour of __irq_disable() in irq/chip.c.
2452 * First call .irq_disable() if it exists, else mimic the
2453 * behaviour of mask_irq() which calls .irq_mask() if
2456 if (gc
->irq
.irq_disable
)
2457 gc
->irq
.irq_disable(d
);
2458 else if (gc
->irq
.chip
->irq_mask
)
2459 gc
->irq
.chip
->irq_mask(d
);
2460 gpiochip_disable_irq(gc
, d
->hwirq
);
2463 static void gpiochip_set_irq_hooks(struct gpio_chip
*gc
)
2465 struct irq_chip
*irqchip
= gc
->irq
.chip
;
2467 if (!irqchip
->irq_request_resources
&&
2468 !irqchip
->irq_release_resources
) {
2469 irqchip
->irq_request_resources
= gpiochip_irq_reqres
;
2470 irqchip
->irq_release_resources
= gpiochip_irq_relres
;
2472 if (WARN_ON(gc
->irq
.irq_enable
))
2474 /* Check if the irqchip already has this hook... */
2475 if (irqchip
->irq_enable
== gpiochip_irq_enable
) {
2477 * ...and if so, give a gentle warning that this is bad
2481 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
2484 gc
->irq
.irq_enable
= irqchip
->irq_enable
;
2485 gc
->irq
.irq_disable
= irqchip
->irq_disable
;
2486 irqchip
->irq_enable
= gpiochip_irq_enable
;
2487 irqchip
->irq_disable
= gpiochip_irq_disable
;
2491 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
2492 * @gc: the GPIO chip to add the IRQ chip to
2493 * @lock_key: lockdep class for IRQ lock
2494 * @request_key: lockdep class for IRQ request
2496 static int gpiochip_add_irqchip(struct gpio_chip
*gc
,
2497 struct lock_class_key
*lock_key
,
2498 struct lock_class_key
*request_key
)
2500 struct irq_chip
*irqchip
= gc
->irq
.chip
;
2501 const struct irq_domain_ops
*ops
= NULL
;
2502 struct device_node
*np
;
2509 if (gc
->irq
.parent_handler
&& gc
->can_sleep
) {
2510 chip_err(gc
, "you cannot have chained interrupts on a chip that may sleep\n");
2514 np
= gc
->gpiodev
->dev
.of_node
;
2515 type
= gc
->irq
.default_type
;
2518 * Specifying a default trigger is a terrible idea if DT or ACPI is
2519 * used to configure the interrupts, as you may end up with
2520 * conflicting triggers. Tell the user, and reset to NONE.
2522 if (WARN(np
&& type
!= IRQ_TYPE_NONE
,
2523 "%s: Ignoring %u default trigger\n", np
->full_name
, type
))
2524 type
= IRQ_TYPE_NONE
;
2526 if (has_acpi_companion(gc
->parent
) && type
!= IRQ_TYPE_NONE
) {
2527 acpi_handle_warn(ACPI_HANDLE(gc
->parent
),
2528 "Ignoring %u default trigger\n", type
);
2529 type
= IRQ_TYPE_NONE
;
2532 gc
->to_irq
= gpiochip_to_irq
;
2533 gc
->irq
.default_type
= type
;
2534 gc
->irq
.lock_key
= lock_key
;
2535 gc
->irq
.request_key
= request_key
;
2537 /* If a parent irqdomain is provided, let's build a hierarchy */
2538 if (gpiochip_hierarchy_is_hierarchical(gc
)) {
2539 int ret
= gpiochip_hierarchy_add_domain(gc
);
2543 /* Some drivers provide custom irqdomain ops */
2544 if (gc
->irq
.domain_ops
)
2545 ops
= gc
->irq
.domain_ops
;
2548 ops
= &gpiochip_domain_ops
;
2549 gc
->irq
.domain
= irq_domain_add_simple(np
,
2553 if (!gc
->irq
.domain
)
2557 if (gc
->irq
.parent_handler
) {
2558 void *data
= gc
->irq
.parent_handler_data
?: gc
;
2560 for (i
= 0; i
< gc
->irq
.num_parents
; i
++) {
2562 * The parent IRQ chip is already using the chip_data
2563 * for this IRQ chip, so our callbacks simply use the
2566 irq_set_chained_handler_and_data(gc
->irq
.parents
[i
],
2567 gc
->irq
.parent_handler
,
2572 gpiochip_set_irq_hooks(gc
);
2574 acpi_gpiochip_request_interrupts(gc
);
2580 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
2581 * @gc: the gpiochip to remove the irqchip from
2583 * This is called only from gpiochip_remove()
2585 static void gpiochip_irqchip_remove(struct gpio_chip
*gc
)
2587 struct irq_chip
*irqchip
= gc
->irq
.chip
;
2588 unsigned int offset
;
2590 acpi_gpiochip_free_interrupts(gc
);
2592 if (irqchip
&& gc
->irq
.parent_handler
) {
2593 struct gpio_irq_chip
*irq
= &gc
->irq
;
2596 for (i
= 0; i
< irq
->num_parents
; i
++)
2597 irq_set_chained_handler_and_data(irq
->parents
[i
],
2601 /* Remove all IRQ mappings and delete the domain */
2602 if (gc
->irq
.domain
) {
2605 for (offset
= 0; offset
< gc
->ngpio
; offset
++) {
2606 if (!gpiochip_irqchip_irq_valid(gc
, offset
))
2609 irq
= irq_find_mapping(gc
->irq
.domain
, offset
);
2610 irq_dispose_mapping(irq
);
2613 irq_domain_remove(gc
->irq
.domain
);
2617 if (irqchip
->irq_request_resources
== gpiochip_irq_reqres
) {
2618 irqchip
->irq_request_resources
= NULL
;
2619 irqchip
->irq_release_resources
= NULL
;
2621 if (irqchip
->irq_enable
== gpiochip_irq_enable
) {
2622 irqchip
->irq_enable
= gc
->irq
.irq_enable
;
2623 irqchip
->irq_disable
= gc
->irq
.irq_disable
;
2626 gc
->irq
.irq_enable
= NULL
;
2627 gc
->irq
.irq_disable
= NULL
;
2628 gc
->irq
.chip
= NULL
;
2630 gpiochip_irqchip_free_valid_mask(gc
);
2634 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
2635 * @gc: the gpiochip to add the irqchip to
2636 * @irqchip: the irqchip to add to the gpiochip
2637 * @first_irq: if not dynamically assigned, the base (first) IRQ to
2638 * allocate gpiochip irqs from
2639 * @handler: the irq handler to use (often a predefined irq core function)
2640 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
2641 * to have the core avoid setting up any default type in the hardware.
2642 * @threaded: whether this irqchip uses a nested thread handler
2643 * @lock_key: lockdep class for IRQ lock
2644 * @request_key: lockdep class for IRQ request
2646 * This function closely associates a certain irqchip with a certain
2647 * gpiochip, providing an irq domain to translate the local IRQs to
2648 * global irqs in the gpiolib core, and making sure that the gpiochip
2649 * is passed as chip data to all related functions. Driver callbacks
2650 * need to use gpiochip_get_data() to get their local state containers back
2651 * from the gpiochip passed as chip data. An irqdomain will be stored
2652 * in the gpiochip that shall be used by the driver to handle IRQ number
2653 * translation. The gpiochip will need to be initialized and registered
2654 * before calling this function.
2656 * This function will handle two cell:ed simple IRQs and assumes all
2657 * the pins on the gpiochip can generate a unique IRQ. Everything else
2658 * need to be open coded.
2660 int gpiochip_irqchip_add_key(struct gpio_chip
*gc
,
2661 struct irq_chip
*irqchip
,
2662 unsigned int first_irq
,
2663 irq_flow_handler_t handler
,
2666 struct lock_class_key
*lock_key
,
2667 struct lock_class_key
*request_key
)
2669 struct device_node
*of_node
;
2671 if (!gc
|| !irqchip
)
2675 pr_err("missing gpiochip .dev parent pointer\n");
2678 gc
->irq
.threaded
= threaded
;
2679 of_node
= gc
->parent
->of_node
;
2680 #ifdef CONFIG_OF_GPIO
2682 * If the gpiochip has an assigned OF node this takes precedence
2683 * FIXME: get rid of this and use gc->parent->of_node
2687 of_node
= gc
->of_node
;
2690 * Specifying a default trigger is a terrible idea if DT or ACPI is
2691 * used to configure the interrupts, as you may end-up with
2692 * conflicting triggers. Tell the user, and reset to NONE.
2694 if (WARN(of_node
&& type
!= IRQ_TYPE_NONE
,
2695 "%pOF: Ignoring %d default trigger\n", of_node
, type
))
2696 type
= IRQ_TYPE_NONE
;
2697 if (has_acpi_companion(gc
->parent
) && type
!= IRQ_TYPE_NONE
) {
2698 acpi_handle_warn(ACPI_HANDLE(gc
->parent
),
2699 "Ignoring %d default trigger\n", type
);
2700 type
= IRQ_TYPE_NONE
;
2703 gc
->irq
.chip
= irqchip
;
2704 gc
->irq
.handler
= handler
;
2705 gc
->irq
.default_type
= type
;
2706 gc
->to_irq
= gpiochip_to_irq
;
2707 gc
->irq
.lock_key
= lock_key
;
2708 gc
->irq
.request_key
= request_key
;
2709 gc
->irq
.domain
= irq_domain_add_simple(of_node
,
2710 gc
->ngpio
, first_irq
,
2711 &gpiochip_domain_ops
, gc
);
2712 if (!gc
->irq
.domain
) {
2713 gc
->irq
.chip
= NULL
;
2717 gpiochip_set_irq_hooks(gc
);
2719 acpi_gpiochip_request_interrupts(gc
);
2723 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key
);
2725 #else /* CONFIG_GPIOLIB_IRQCHIP */
2727 static inline int gpiochip_add_irqchip(struct gpio_chip
*gc
,
2728 struct lock_class_key
*lock_key
,
2729 struct lock_class_key
*request_key
)
2733 static void gpiochip_irqchip_remove(struct gpio_chip
*gc
) {}
2735 static inline int gpiochip_irqchip_init_hw(struct gpio_chip
*gc
)
2740 static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip
*gc
)
2744 static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip
*gc
)
2747 #endif /* CONFIG_GPIOLIB_IRQCHIP */
2750 * gpiochip_generic_request() - request the gpio function for a pin
2751 * @gc: the gpiochip owning the GPIO
2752 * @offset: the offset of the GPIO to request for GPIO function
2754 int gpiochip_generic_request(struct gpio_chip
*gc
, unsigned offset
)
2756 #ifdef CONFIG_PINCTRL
2757 if (list_empty(&gc
->gpiodev
->pin_ranges
))
2761 return pinctrl_gpio_request(gc
->gpiodev
->base
+ offset
);
2763 EXPORT_SYMBOL_GPL(gpiochip_generic_request
);
2766 * gpiochip_generic_free() - free the gpio function from a pin
2767 * @gc: the gpiochip to request the gpio function for
2768 * @offset: the offset of the GPIO to free from GPIO function
2770 void gpiochip_generic_free(struct gpio_chip
*gc
, unsigned offset
)
2772 pinctrl_gpio_free(gc
->gpiodev
->base
+ offset
);
2774 EXPORT_SYMBOL_GPL(gpiochip_generic_free
);
2777 * gpiochip_generic_config() - apply configuration for a pin
2778 * @gc: the gpiochip owning the GPIO
2779 * @offset: the offset of the GPIO to apply the configuration
2780 * @config: the configuration to be applied
2782 int gpiochip_generic_config(struct gpio_chip
*gc
, unsigned offset
,
2783 unsigned long config
)
2785 return pinctrl_gpio_set_config(gc
->gpiodev
->base
+ offset
, config
);
2787 EXPORT_SYMBOL_GPL(gpiochip_generic_config
);
2789 #ifdef CONFIG_PINCTRL
2792 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2793 * @gc: the gpiochip to add the range for
2794 * @pctldev: the pin controller to map to
2795 * @gpio_offset: the start offset in the current gpio_chip number space
2796 * @pin_group: name of the pin group inside the pin controller
2798 * Calling this function directly from a DeviceTree-supported
2799 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2800 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2801 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
2803 int gpiochip_add_pingroup_range(struct gpio_chip
*gc
,
2804 struct pinctrl_dev
*pctldev
,
2805 unsigned int gpio_offset
, const char *pin_group
)
2807 struct gpio_pin_range
*pin_range
;
2808 struct gpio_device
*gdev
= gc
->gpiodev
;
2811 pin_range
= kzalloc(sizeof(*pin_range
), GFP_KERNEL
);
2813 chip_err(gc
, "failed to allocate pin ranges\n");
2817 /* Use local offset as range ID */
2818 pin_range
->range
.id
= gpio_offset
;
2819 pin_range
->range
.gc
= gc
;
2820 pin_range
->range
.name
= gc
->label
;
2821 pin_range
->range
.base
= gdev
->base
+ gpio_offset
;
2822 pin_range
->pctldev
= pctldev
;
2824 ret
= pinctrl_get_group_pins(pctldev
, pin_group
,
2825 &pin_range
->range
.pins
,
2826 &pin_range
->range
.npins
);
2832 pinctrl_add_gpio_range(pctldev
, &pin_range
->range
);
2834 chip_dbg(gc
, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2835 gpio_offset
, gpio_offset
+ pin_range
->range
.npins
- 1,
2836 pinctrl_dev_get_devname(pctldev
), pin_group
);
2838 list_add_tail(&pin_range
->node
, &gdev
->pin_ranges
);
2842 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range
);
2845 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2846 * @gc: the gpiochip to add the range for
2847 * @pinctl_name: the dev_name() of the pin controller to map to
2848 * @gpio_offset: the start offset in the current gpio_chip number space
2849 * @pin_offset: the start offset in the pin controller number space
2850 * @npins: the number of pins from the offset of each pin space (GPIO and
2851 * pin controller) to accumulate in this range
2854 * 0 on success, or a negative error-code on failure.
2856 * Calling this function directly from a DeviceTree-supported
2857 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2858 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2859 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
2861 int gpiochip_add_pin_range(struct gpio_chip
*gc
, const char *pinctl_name
,
2862 unsigned int gpio_offset
, unsigned int pin_offset
,
2865 struct gpio_pin_range
*pin_range
;
2866 struct gpio_device
*gdev
= gc
->gpiodev
;
2869 pin_range
= kzalloc(sizeof(*pin_range
), GFP_KERNEL
);
2871 chip_err(gc
, "failed to allocate pin ranges\n");
2875 /* Use local offset as range ID */
2876 pin_range
->range
.id
= gpio_offset
;
2877 pin_range
->range
.gc
= gc
;
2878 pin_range
->range
.name
= gc
->label
;
2879 pin_range
->range
.base
= gdev
->base
+ gpio_offset
;
2880 pin_range
->range
.pin_base
= pin_offset
;
2881 pin_range
->range
.npins
= npins
;
2882 pin_range
->pctldev
= pinctrl_find_and_add_gpio_range(pinctl_name
,
2884 if (IS_ERR(pin_range
->pctldev
)) {
2885 ret
= PTR_ERR(pin_range
->pctldev
);
2886 chip_err(gc
, "could not create pin range\n");
2890 chip_dbg(gc
, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2891 gpio_offset
, gpio_offset
+ npins
- 1,
2893 pin_offset
, pin_offset
+ npins
- 1);
2895 list_add_tail(&pin_range
->node
, &gdev
->pin_ranges
);
2899 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range
);
2902 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2903 * @gc: the chip to remove all the mappings for
2905 void gpiochip_remove_pin_ranges(struct gpio_chip
*gc
)
2907 struct gpio_pin_range
*pin_range
, *tmp
;
2908 struct gpio_device
*gdev
= gc
->gpiodev
;
2910 list_for_each_entry_safe(pin_range
, tmp
, &gdev
->pin_ranges
, node
) {
2911 list_del(&pin_range
->node
);
2912 pinctrl_remove_gpio_range(pin_range
->pctldev
,
2917 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges
);
2919 #endif /* CONFIG_PINCTRL */
2921 /* These "optional" allocation calls help prevent drivers from stomping
2922 * on each other, and help provide better diagnostics in debugfs.
2923 * They're called even less than the "set direction" calls.
2925 static int gpiod_request_commit(struct gpio_desc
*desc
, const char *label
)
2927 struct gpio_chip
*gc
= desc
->gdev
->chip
;
2929 unsigned long flags
;
2933 label
= kstrdup_const(label
, GFP_KERNEL
);
2938 spin_lock_irqsave(&gpio_lock
, flags
);
2940 /* NOTE: gpio_request() can be called in early boot,
2941 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
2944 if (test_and_set_bit(FLAG_REQUESTED
, &desc
->flags
) == 0) {
2945 desc_set_label(desc
, label
? : "?");
2954 /* gc->request may sleep */
2955 spin_unlock_irqrestore(&gpio_lock
, flags
);
2956 offset
= gpio_chip_hwgpio(desc
);
2957 if (gpiochip_line_is_valid(gc
, offset
))
2958 ret
= gc
->request(gc
, offset
);
2961 spin_lock_irqsave(&gpio_lock
, flags
);
2964 desc_set_label(desc
, NULL
);
2966 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
2970 if (gc
->get_direction
) {
2971 /* gc->get_direction may sleep */
2972 spin_unlock_irqrestore(&gpio_lock
, flags
);
2973 gpiod_get_direction(desc
);
2974 spin_lock_irqsave(&gpio_lock
, flags
);
2977 spin_unlock_irqrestore(&gpio_lock
, flags
);
2978 atomic_notifier_call_chain(&desc
->gdev
->notifier
,
2979 GPIOLINE_CHANGED_REQUESTED
, desc
);
2984 * This descriptor validation needs to be inserted verbatim into each
2985 * function taking a descriptor, so we need to use a preprocessor
2986 * macro to avoid endless duplication. If the desc is NULL it is an
2987 * optional GPIO and calls should just bail out.
2989 static int validate_desc(const struct gpio_desc
*desc
, const char *func
)
2994 pr_warn("%s: invalid GPIO (errorpointer)\n", func
);
2995 return PTR_ERR(desc
);
2998 pr_warn("%s: invalid GPIO (no device)\n", func
);
3001 if (!desc
->gdev
->chip
) {
3002 dev_warn(&desc
->gdev
->dev
,
3003 "%s: backing chip is gone\n", func
);
3009 #define VALIDATE_DESC(desc) do { \
3010 int __valid = validate_desc(desc, __func__); \
3015 #define VALIDATE_DESC_VOID(desc) do { \
3016 int __valid = validate_desc(desc, __func__); \
3021 int gpiod_request(struct gpio_desc
*desc
, const char *label
)
3023 int ret
= -EPROBE_DEFER
;
3024 struct gpio_device
*gdev
;
3026 VALIDATE_DESC(desc
);
3029 if (try_module_get(gdev
->owner
)) {
3030 ret
= gpiod_request_commit(desc
, label
);
3032 module_put(gdev
->owner
);
3034 get_device(&gdev
->dev
);
3038 gpiod_dbg(desc
, "%s: status %d\n", __func__
, ret
);
3043 static bool gpiod_free_commit(struct gpio_desc
*desc
)
3046 unsigned long flags
;
3047 struct gpio_chip
*gc
;
3051 gpiod_unexport(desc
);
3053 spin_lock_irqsave(&gpio_lock
, flags
);
3055 gc
= desc
->gdev
->chip
;
3056 if (gc
&& test_bit(FLAG_REQUESTED
, &desc
->flags
)) {
3058 spin_unlock_irqrestore(&gpio_lock
, flags
);
3059 might_sleep_if(gc
->can_sleep
);
3060 gc
->free(gc
, gpio_chip_hwgpio(desc
));
3061 spin_lock_irqsave(&gpio_lock
, flags
);
3063 kfree_const(desc
->label
);
3064 desc_set_label(desc
, NULL
);
3065 clear_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
3066 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
3067 clear_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
3068 clear_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
3069 clear_bit(FLAG_PULL_UP
, &desc
->flags
);
3070 clear_bit(FLAG_PULL_DOWN
, &desc
->flags
);
3071 clear_bit(FLAG_BIAS_DISABLE
, &desc
->flags
);
3072 clear_bit(FLAG_IS_HOGGED
, &desc
->flags
);
3073 #ifdef CONFIG_OF_DYNAMIC
3079 spin_unlock_irqrestore(&gpio_lock
, flags
);
3080 atomic_notifier_call_chain(&desc
->gdev
->notifier
,
3081 GPIOLINE_CHANGED_RELEASED
, desc
);
3086 void gpiod_free(struct gpio_desc
*desc
)
3088 if (desc
&& desc
->gdev
&& gpiod_free_commit(desc
)) {
3089 module_put(desc
->gdev
->owner
);
3090 put_device(&desc
->gdev
->dev
);
3092 WARN_ON(extra_checks
);
3097 * gpiochip_is_requested - return string iff signal was requested
3098 * @gc: controller managing the signal
3099 * @offset: of signal within controller's 0..(ngpio - 1) range
3101 * Returns NULL if the GPIO is not currently requested, else a string.
3102 * The string returned is the label passed to gpio_request(); if none has been
3103 * passed it is a meaningless, non-NULL constant.
3105 * This function is for use by GPIO controller drivers. The label can
3106 * help with diagnostics, and knowing that the signal is used as a GPIO
3107 * can help avoid accidentally multiplexing it to another controller.
3109 const char *gpiochip_is_requested(struct gpio_chip
*gc
, unsigned offset
)
3111 struct gpio_desc
*desc
;
3113 if (offset
>= gc
->ngpio
)
3116 desc
= gpiochip_get_desc(gc
, offset
);
3120 if (test_bit(FLAG_REQUESTED
, &desc
->flags
) == 0)
3124 EXPORT_SYMBOL_GPL(gpiochip_is_requested
);
3127 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
3129 * @hwnum: hardware number of the GPIO for which to request the descriptor
3130 * @label: label for the GPIO
3131 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
3132 * specify things like line inversion semantics with the machine flags
3133 * such as GPIO_OUT_LOW
3134 * @dflags: descriptor request flags for this GPIO or 0 if default, this
3135 * can be used to specify consumer semantics such as open drain
3137 * Function allows GPIO chip drivers to request and use their own GPIO
3138 * descriptors via gpiolib API. Difference to gpiod_request() is that this
3139 * function will not increase reference count of the GPIO chip module. This
3140 * allows the GPIO chip module to be unloaded as needed (we assume that the
3141 * GPIO chip driver handles freeing the GPIOs it has requested).
3144 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
3147 struct gpio_desc
*gpiochip_request_own_desc(struct gpio_chip
*gc
,
3150 enum gpio_lookup_flags lflags
,
3151 enum gpiod_flags dflags
)
3153 struct gpio_desc
*desc
= gpiochip_get_desc(gc
, hwnum
);
3157 chip_err(gc
, "failed to get GPIO descriptor\n");
3161 ret
= gpiod_request_commit(desc
, label
);
3163 return ERR_PTR(ret
);
3165 ret
= gpiod_configure_flags(desc
, label
, lflags
, dflags
);
3167 chip_err(gc
, "setup of own GPIO %s failed\n", label
);
3168 gpiod_free_commit(desc
);
3169 return ERR_PTR(ret
);
3174 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc
);
3177 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
3178 * @desc: GPIO descriptor to free
3180 * Function frees the given GPIO requested previously with
3181 * gpiochip_request_own_desc().
3183 void gpiochip_free_own_desc(struct gpio_desc
*desc
)
3186 gpiod_free_commit(desc
);
3188 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc
);
3191 * Drivers MUST set GPIO direction before making get/set calls. In
3192 * some cases this is done in early boot, before IRQs are enabled.
3194 * As a rule these aren't called more than once (except for drivers
3195 * using the open-drain emulation idiom) so these are natural places
3196 * to accumulate extra debugging checks. Note that we can't (yet)
3197 * rely on gpio_request() having been called beforehand.
3200 static int gpio_do_set_config(struct gpio_chip
*gc
, unsigned int offset
,
3201 unsigned long config
)
3203 if (!gc
->set_config
)
3206 return gc
->set_config(gc
, offset
, config
);
3209 static int gpio_set_config(struct gpio_desc
*desc
, enum pin_config_param mode
)
3211 struct gpio_chip
*gc
= desc
->gdev
->chip
;
3212 unsigned long config
;
3216 case PIN_CONFIG_BIAS_PULL_DOWN
:
3217 case PIN_CONFIG_BIAS_PULL_UP
:
3225 config
= PIN_CONF_PACKED(mode
, arg
);
3226 return gpio_do_set_config(gc
, gpio_chip_hwgpio(desc
), config
);
3229 static int gpio_set_bias(struct gpio_desc
*desc
)
3234 if (test_bit(FLAG_BIAS_DISABLE
, &desc
->flags
))
3235 bias
= PIN_CONFIG_BIAS_DISABLE
;
3236 else if (test_bit(FLAG_PULL_UP
, &desc
->flags
))
3237 bias
= PIN_CONFIG_BIAS_PULL_UP
;
3238 else if (test_bit(FLAG_PULL_DOWN
, &desc
->flags
))
3239 bias
= PIN_CONFIG_BIAS_PULL_DOWN
;
3242 ret
= gpio_set_config(desc
, bias
);
3243 if (ret
!= -ENOTSUPP
)
3250 * gpiod_direction_input - set the GPIO direction to input
3251 * @desc: GPIO to set to input
3253 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
3254 * be called safely on it.
3256 * Return 0 in case of success, else an error code.
3258 int gpiod_direction_input(struct gpio_desc
*desc
)
3260 struct gpio_chip
*gc
;
3263 VALIDATE_DESC(desc
);
3264 gc
= desc
->gdev
->chip
;
3267 * It is legal to have no .get() and .direction_input() specified if
3268 * the chip is output-only, but you can't specify .direction_input()
3269 * and not support the .get() operation, that doesn't make sense.
3271 if (!gc
->get
&& gc
->direction_input
) {
3273 "%s: missing get() but have direction_input()\n",
3279 * If we have a .direction_input() callback, things are simple,
3280 * just call it. Else we are some input-only chip so try to check the
3281 * direction (if .get_direction() is supported) else we silently
3282 * assume we are in input mode after this.
3284 if (gc
->direction_input
) {
3285 ret
= gc
->direction_input(gc
, gpio_chip_hwgpio(desc
));
3286 } else if (gc
->get_direction
&&
3287 (gc
->get_direction(gc
, gpio_chip_hwgpio(desc
)) != 1)) {
3289 "%s: missing direction_input() operation and line is output\n",
3294 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
3295 ret
= gpio_set_bias(desc
);
3298 trace_gpio_direction(desc_to_gpio(desc
), 1, ret
);
3302 EXPORT_SYMBOL_GPL(gpiod_direction_input
);
3304 static int gpiod_direction_output_raw_commit(struct gpio_desc
*desc
, int value
)
3306 struct gpio_chip
*gc
= desc
->gdev
->chip
;
3311 * It's OK not to specify .direction_output() if the gpiochip is
3312 * output-only, but if there is then not even a .set() operation it
3313 * is pretty tricky to drive the output line.
3315 if (!gc
->set
&& !gc
->direction_output
) {
3317 "%s: missing set() and direction_output() operations\n",
3322 if (gc
->direction_output
) {
3323 ret
= gc
->direction_output(gc
, gpio_chip_hwgpio(desc
), val
);
3325 /* Check that we are in output mode if we can */
3326 if (gc
->get_direction
&&
3327 gc
->get_direction(gc
, gpio_chip_hwgpio(desc
))) {
3329 "%s: missing direction_output() operation\n",
3334 * If we can't actively set the direction, we are some
3335 * output-only chip, so just drive the output as desired.
3337 gc
->set(gc
, gpio_chip_hwgpio(desc
), val
);
3341 set_bit(FLAG_IS_OUT
, &desc
->flags
);
3342 trace_gpio_value(desc_to_gpio(desc
), 0, val
);
3343 trace_gpio_direction(desc_to_gpio(desc
), 0, ret
);
3348 * gpiod_direction_output_raw - set the GPIO direction to output
3349 * @desc: GPIO to set to output
3350 * @value: initial output value of the GPIO
3352 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
3353 * be called safely on it. The initial value of the output must be specified
3354 * as raw value on the physical line without regard for the ACTIVE_LOW status.
3356 * Return 0 in case of success, else an error code.
3358 int gpiod_direction_output_raw(struct gpio_desc
*desc
, int value
)
3360 VALIDATE_DESC(desc
);
3361 return gpiod_direction_output_raw_commit(desc
, value
);
3363 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw
);
3366 * gpiod_direction_output - set the GPIO direction to output
3367 * @desc: GPIO to set to output
3368 * @value: initial output value of the GPIO
3370 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
3371 * be called safely on it. The initial value of the output must be specified
3372 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3375 * Return 0 in case of success, else an error code.
3377 int gpiod_direction_output(struct gpio_desc
*desc
, int value
)
3381 VALIDATE_DESC(desc
);
3382 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
3387 /* GPIOs used for enabled IRQs shall not be set as output */
3388 if (test_bit(FLAG_USED_AS_IRQ
, &desc
->flags
) &&
3389 test_bit(FLAG_IRQ_IS_ENABLED
, &desc
->flags
)) {
3391 "%s: tried to set a GPIO tied to an IRQ as output\n",
3396 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
)) {
3397 /* First see if we can enable open drain in hardware */
3398 ret
= gpio_set_config(desc
, PIN_CONFIG_DRIVE_OPEN_DRAIN
);
3400 goto set_output_value
;
3401 /* Emulate open drain by not actively driving the line high */
3403 ret
= gpiod_direction_input(desc
);
3404 goto set_output_flag
;
3407 else if (test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
)) {
3408 ret
= gpio_set_config(desc
, PIN_CONFIG_DRIVE_OPEN_SOURCE
);
3410 goto set_output_value
;
3411 /* Emulate open source by not actively driving the line low */
3413 ret
= gpiod_direction_input(desc
);
3414 goto set_output_flag
;
3417 gpio_set_config(desc
, PIN_CONFIG_DRIVE_PUSH_PULL
);
3421 ret
= gpio_set_bias(desc
);
3424 return gpiod_direction_output_raw_commit(desc
, value
);
3428 * When emulating open-source or open-drain functionalities by not
3429 * actively driving the line (setting mode to input) we still need to
3430 * set the IS_OUT flag or otherwise we won't be able to set the line
3434 set_bit(FLAG_IS_OUT
, &desc
->flags
);
3437 EXPORT_SYMBOL_GPL(gpiod_direction_output
);
3440 * gpiod_set_config - sets @config for a GPIO
3441 * @desc: descriptor of the GPIO for which to set the configuration
3442 * @config: Same packed config format as generic pinconf
3445 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
3448 int gpiod_set_config(struct gpio_desc
*desc
, unsigned long config
)
3450 struct gpio_chip
*gc
;
3452 VALIDATE_DESC(desc
);
3453 gc
= desc
->gdev
->chip
;
3455 return gpio_do_set_config(gc
, gpio_chip_hwgpio(desc
), config
);
3457 EXPORT_SYMBOL_GPL(gpiod_set_config
);
3460 * gpiod_set_debounce - sets @debounce time for a GPIO
3461 * @desc: descriptor of the GPIO for which to set debounce time
3462 * @debounce: debounce time in microseconds
3465 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
3468 int gpiod_set_debounce(struct gpio_desc
*desc
, unsigned debounce
)
3470 unsigned long config
;
3472 config
= pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE
, debounce
);
3473 return gpiod_set_config(desc
, config
);
3475 EXPORT_SYMBOL_GPL(gpiod_set_debounce
);
3478 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
3479 * @desc: descriptor of the GPIO for which to configure persistence
3480 * @transitory: True to lose state on suspend or reset, false for persistence
3483 * 0 on success, otherwise a negative error code.
3485 int gpiod_set_transitory(struct gpio_desc
*desc
, bool transitory
)
3487 struct gpio_chip
*gc
;
3488 unsigned long packed
;
3492 VALIDATE_DESC(desc
);
3494 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
3495 * persistence state.
3497 assign_bit(FLAG_TRANSITORY
, &desc
->flags
, transitory
);
3499 /* If the driver supports it, set the persistence state now */
3500 gc
= desc
->gdev
->chip
;
3501 if (!gc
->set_config
)
3504 packed
= pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE
,
3506 gpio
= gpio_chip_hwgpio(desc
);
3507 rc
= gpio_do_set_config(gc
, gpio
, packed
);
3508 if (rc
== -ENOTSUPP
) {
3509 dev_dbg(&desc
->gdev
->dev
, "Persistence not supported for GPIO %d\n",
3516 EXPORT_SYMBOL_GPL(gpiod_set_transitory
);
3519 * gpiod_is_active_low - test whether a GPIO is active-low or not
3520 * @desc: the gpio descriptor to test
3522 * Returns 1 if the GPIO is active-low, 0 otherwise.
3524 int gpiod_is_active_low(const struct gpio_desc
*desc
)
3526 VALIDATE_DESC(desc
);
3527 return test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
3529 EXPORT_SYMBOL_GPL(gpiod_is_active_low
);
3532 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
3533 * @desc: the gpio descriptor to change
3535 void gpiod_toggle_active_low(struct gpio_desc
*desc
)
3537 VALIDATE_DESC_VOID(desc
);
3538 change_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
3540 EXPORT_SYMBOL_GPL(gpiod_toggle_active_low
);
3542 /* I/O calls are only valid after configuration completed; the relevant
3543 * "is this a valid GPIO" error checks should already have been done.
3545 * "Get" operations are often inlinable as reading a pin value register,
3546 * and masking the relevant bit in that register.
3548 * When "set" operations are inlinable, they involve writing that mask to
3549 * one register to set a low value, or a different register to set it high.
3550 * Otherwise locking is needed, so there may be little value to inlining.
3552 *------------------------------------------------------------------------
3554 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
3555 * have requested the GPIO. That can include implicit requesting by
3556 * a direction setting call. Marking a gpio as requested locks its chip
3557 * in memory, guaranteeing that these table lookups need no more locking
3558 * and that gpiochip_remove() will fail.
3560 * REVISIT when debugging, consider adding some instrumentation to ensure
3561 * that the GPIO was actually requested.
3564 static int gpiod_get_raw_value_commit(const struct gpio_desc
*desc
)
3566 struct gpio_chip
*gc
;
3570 gc
= desc
->gdev
->chip
;
3571 offset
= gpio_chip_hwgpio(desc
);
3572 value
= gc
->get
? gc
->get(gc
, offset
) : -EIO
;
3573 value
= value
< 0 ? value
: !!value
;
3574 trace_gpio_value(desc_to_gpio(desc
), 1, value
);
3578 static int gpio_chip_get_multiple(struct gpio_chip
*gc
,
3579 unsigned long *mask
, unsigned long *bits
)
3581 if (gc
->get_multiple
) {
3582 return gc
->get_multiple(gc
, mask
, bits
);
3583 } else if (gc
->get
) {
3586 for_each_set_bit(i
, mask
, gc
->ngpio
) {
3587 value
= gc
->get(gc
, i
);
3590 __assign_bit(i
, bits
, value
);
3597 int gpiod_get_array_value_complex(bool raw
, bool can_sleep
,
3598 unsigned int array_size
,
3599 struct gpio_desc
**desc_array
,
3600 struct gpio_array
*array_info
,
3601 unsigned long *value_bitmap
)
3606 * Validate array_info against desc_array and its size.
3607 * It should immediately follow desc_array if both
3608 * have been obtained from the same gpiod_get_array() call.
3610 if (array_info
&& array_info
->desc
== desc_array
&&
3611 array_size
<= array_info
->size
&&
3612 (void *)array_info
== desc_array
+ array_info
->size
) {
3614 WARN_ON(array_info
->chip
->can_sleep
);
3616 ret
= gpio_chip_get_multiple(array_info
->chip
,
3617 array_info
->get_mask
,
3622 if (!raw
&& !bitmap_empty(array_info
->invert_mask
, array_size
))
3623 bitmap_xor(value_bitmap
, value_bitmap
,
3624 array_info
->invert_mask
, array_size
);
3626 if (bitmap_full(array_info
->get_mask
, array_size
))
3629 i
= find_first_zero_bit(array_info
->get_mask
, array_size
);
3634 while (i
< array_size
) {
3635 struct gpio_chip
*gc
= desc_array
[i
]->gdev
->chip
;
3636 unsigned long fastpath
[2 * BITS_TO_LONGS(FASTPATH_NGPIO
)];
3637 unsigned long *mask
, *bits
;
3640 if (likely(gc
->ngpio
<= FASTPATH_NGPIO
)) {
3643 mask
= kmalloc_array(2 * BITS_TO_LONGS(gc
->ngpio
),
3645 can_sleep
? GFP_KERNEL
: GFP_ATOMIC
);
3650 bits
= mask
+ BITS_TO_LONGS(gc
->ngpio
);
3651 bitmap_zero(mask
, gc
->ngpio
);
3654 WARN_ON(gc
->can_sleep
);
3656 /* collect all inputs belonging to the same chip */
3659 const struct gpio_desc
*desc
= desc_array
[i
];
3660 int hwgpio
= gpio_chip_hwgpio(desc
);
3662 __set_bit(hwgpio
, mask
);
3666 i
= find_next_zero_bit(array_info
->get_mask
,
3668 } while ((i
< array_size
) &&
3669 (desc_array
[i
]->gdev
->chip
== gc
));
3671 ret
= gpio_chip_get_multiple(gc
, mask
, bits
);
3673 if (mask
!= fastpath
)
3678 for (j
= first
; j
< i
; ) {
3679 const struct gpio_desc
*desc
= desc_array
[j
];
3680 int hwgpio
= gpio_chip_hwgpio(desc
);
3681 int value
= test_bit(hwgpio
, bits
);
3683 if (!raw
&& test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
3685 __assign_bit(j
, value_bitmap
, value
);
3686 trace_gpio_value(desc_to_gpio(desc
), 1, value
);
3690 j
= find_next_zero_bit(array_info
->get_mask
, i
,
3694 if (mask
!= fastpath
)
3701 * gpiod_get_raw_value() - return a gpio's raw value
3702 * @desc: gpio whose value will be returned
3704 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
3705 * its ACTIVE_LOW status, or negative errno on failure.
3707 * This function can be called from contexts where we cannot sleep, and will
3708 * complain if the GPIO chip functions potentially sleep.
3710 int gpiod_get_raw_value(const struct gpio_desc
*desc
)
3712 VALIDATE_DESC(desc
);
3713 /* Should be using gpiod_get_raw_value_cansleep() */
3714 WARN_ON(desc
->gdev
->chip
->can_sleep
);
3715 return gpiod_get_raw_value_commit(desc
);
3717 EXPORT_SYMBOL_GPL(gpiod_get_raw_value
);
3720 * gpiod_get_value() - return a gpio's value
3721 * @desc: gpio whose value will be returned
3723 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3724 * account, or negative errno on failure.
3726 * This function can be called from contexts where we cannot sleep, and will
3727 * complain if the GPIO chip functions potentially sleep.
3729 int gpiod_get_value(const struct gpio_desc
*desc
)
3733 VALIDATE_DESC(desc
);
3734 /* Should be using gpiod_get_value_cansleep() */
3735 WARN_ON(desc
->gdev
->chip
->can_sleep
);
3737 value
= gpiod_get_raw_value_commit(desc
);
3741 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
3746 EXPORT_SYMBOL_GPL(gpiod_get_value
);
3749 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
3750 * @array_size: number of elements in the descriptor array / value bitmap
3751 * @desc_array: array of GPIO descriptors whose values will be read
3752 * @array_info: information on applicability of fast bitmap processing path
3753 * @value_bitmap: bitmap to store the read values
3755 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3756 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3757 * else an error code.
3759 * This function can be called from contexts where we cannot sleep,
3760 * and it will complain if the GPIO chip functions potentially sleep.
3762 int gpiod_get_raw_array_value(unsigned int array_size
,
3763 struct gpio_desc
**desc_array
,
3764 struct gpio_array
*array_info
,
3765 unsigned long *value_bitmap
)
3769 return gpiod_get_array_value_complex(true, false, array_size
,
3770 desc_array
, array_info
,
3773 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value
);
3776 * gpiod_get_array_value() - read values from an array of GPIOs
3777 * @array_size: number of elements in the descriptor array / value bitmap
3778 * @desc_array: array of GPIO descriptors whose values will be read
3779 * @array_info: information on applicability of fast bitmap processing path
3780 * @value_bitmap: bitmap to store the read values
3782 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3783 * into account. Return 0 in case of success, else an error code.
3785 * This function can be called from contexts where we cannot sleep,
3786 * and it will complain if the GPIO chip functions potentially sleep.
3788 int gpiod_get_array_value(unsigned int array_size
,
3789 struct gpio_desc
**desc_array
,
3790 struct gpio_array
*array_info
,
3791 unsigned long *value_bitmap
)
3795 return gpiod_get_array_value_complex(false, false, array_size
,
3796 desc_array
, array_info
,
3799 EXPORT_SYMBOL_GPL(gpiod_get_array_value
);
3802 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
3803 * @desc: gpio descriptor whose state need to be set.
3804 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3806 static void gpio_set_open_drain_value_commit(struct gpio_desc
*desc
, bool value
)
3809 struct gpio_chip
*gc
= desc
->gdev
->chip
;
3810 int offset
= gpio_chip_hwgpio(desc
);
3813 ret
= gc
->direction_input(gc
, offset
);
3815 ret
= gc
->direction_output(gc
, offset
, 0);
3817 set_bit(FLAG_IS_OUT
, &desc
->flags
);
3819 trace_gpio_direction(desc_to_gpio(desc
), value
, ret
);
3822 "%s: Error in set_value for open drain err %d\n",
3827 * _gpio_set_open_source_value() - Set the open source gpio's value.
3828 * @desc: gpio descriptor whose state need to be set.
3829 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3831 static void gpio_set_open_source_value_commit(struct gpio_desc
*desc
, bool value
)
3834 struct gpio_chip
*gc
= desc
->gdev
->chip
;
3835 int offset
= gpio_chip_hwgpio(desc
);
3838 ret
= gc
->direction_output(gc
, offset
, 1);
3840 set_bit(FLAG_IS_OUT
, &desc
->flags
);
3842 ret
= gc
->direction_input(gc
, offset
);
3844 trace_gpio_direction(desc_to_gpio(desc
), !value
, ret
);
3847 "%s: Error in set_value for open source err %d\n",
3851 static void gpiod_set_raw_value_commit(struct gpio_desc
*desc
, bool value
)
3853 struct gpio_chip
*gc
;
3855 gc
= desc
->gdev
->chip
;
3856 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
3857 gc
->set(gc
, gpio_chip_hwgpio(desc
), value
);
3861 * set multiple outputs on the same chip;
3862 * use the chip's set_multiple function if available;
3863 * otherwise set the outputs sequentially;
3864 * @chip: the GPIO chip we operate on
3865 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
3866 * defines which outputs are to be changed
3867 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
3868 * defines the values the outputs specified by mask are to be set to
3870 static void gpio_chip_set_multiple(struct gpio_chip
*gc
,
3871 unsigned long *mask
, unsigned long *bits
)
3873 if (gc
->set_multiple
) {
3874 gc
->set_multiple(gc
, mask
, bits
);
3878 /* set outputs if the corresponding mask bit is set */
3879 for_each_set_bit(i
, mask
, gc
->ngpio
)
3880 gc
->set(gc
, i
, test_bit(i
, bits
));
3884 int gpiod_set_array_value_complex(bool raw
, bool can_sleep
,
3885 unsigned int array_size
,
3886 struct gpio_desc
**desc_array
,
3887 struct gpio_array
*array_info
,
3888 unsigned long *value_bitmap
)
3893 * Validate array_info against desc_array and its size.
3894 * It should immediately follow desc_array if both
3895 * have been obtained from the same gpiod_get_array() call.
3897 if (array_info
&& array_info
->desc
== desc_array
&&
3898 array_size
<= array_info
->size
&&
3899 (void *)array_info
== desc_array
+ array_info
->size
) {
3901 WARN_ON(array_info
->chip
->can_sleep
);
3903 if (!raw
&& !bitmap_empty(array_info
->invert_mask
, array_size
))
3904 bitmap_xor(value_bitmap
, value_bitmap
,
3905 array_info
->invert_mask
, array_size
);
3907 gpio_chip_set_multiple(array_info
->chip
, array_info
->set_mask
,
3910 if (bitmap_full(array_info
->set_mask
, array_size
))
3913 i
= find_first_zero_bit(array_info
->set_mask
, array_size
);
3918 while (i
< array_size
) {
3919 struct gpio_chip
*gc
= desc_array
[i
]->gdev
->chip
;
3920 unsigned long fastpath
[2 * BITS_TO_LONGS(FASTPATH_NGPIO
)];
3921 unsigned long *mask
, *bits
;
3924 if (likely(gc
->ngpio
<= FASTPATH_NGPIO
)) {
3927 mask
= kmalloc_array(2 * BITS_TO_LONGS(gc
->ngpio
),
3929 can_sleep
? GFP_KERNEL
: GFP_ATOMIC
);
3934 bits
= mask
+ BITS_TO_LONGS(gc
->ngpio
);
3935 bitmap_zero(mask
, gc
->ngpio
);
3938 WARN_ON(gc
->can_sleep
);
3941 struct gpio_desc
*desc
= desc_array
[i
];
3942 int hwgpio
= gpio_chip_hwgpio(desc
);
3943 int value
= test_bit(i
, value_bitmap
);
3946 * Pins applicable for fast input but not for
3947 * fast output processing may have been already
3948 * inverted inside the fast path, skip them.
3950 if (!raw
&& !(array_info
&&
3951 test_bit(i
, array_info
->invert_mask
)) &&
3952 test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
3954 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
3956 * collect all normal outputs belonging to the same chip
3957 * open drain and open source outputs are set individually
3959 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
) && !raw
) {
3960 gpio_set_open_drain_value_commit(desc
, value
);
3961 } else if (test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
) && !raw
) {
3962 gpio_set_open_source_value_commit(desc
, value
);
3964 __set_bit(hwgpio
, mask
);
3965 __assign_bit(hwgpio
, bits
, value
);
3971 i
= find_next_zero_bit(array_info
->set_mask
,
3973 } while ((i
< array_size
) &&
3974 (desc_array
[i
]->gdev
->chip
== gc
));
3975 /* push collected bits to outputs */
3977 gpio_chip_set_multiple(gc
, mask
, bits
);
3979 if (mask
!= fastpath
)
3986 * gpiod_set_raw_value() - assign a gpio's raw value
3987 * @desc: gpio whose value will be assigned
3988 * @value: value to assign
3990 * Set the raw value of the GPIO, i.e. the value of its physical line without
3991 * regard for its ACTIVE_LOW status.
3993 * This function can be called from contexts where we cannot sleep, and will
3994 * complain if the GPIO chip functions potentially sleep.
3996 void gpiod_set_raw_value(struct gpio_desc
*desc
, int value
)
3998 VALIDATE_DESC_VOID(desc
);
3999 /* Should be using gpiod_set_raw_value_cansleep() */
4000 WARN_ON(desc
->gdev
->chip
->can_sleep
);
4001 gpiod_set_raw_value_commit(desc
, value
);
4003 EXPORT_SYMBOL_GPL(gpiod_set_raw_value
);
4006 * gpiod_set_value_nocheck() - set a GPIO line value without checking
4007 * @desc: the descriptor to set the value on
4008 * @value: value to set
4010 * This sets the value of a GPIO line backing a descriptor, applying
4011 * different semantic quirks like active low and open drain/source
4014 static void gpiod_set_value_nocheck(struct gpio_desc
*desc
, int value
)
4016 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
4018 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
))
4019 gpio_set_open_drain_value_commit(desc
, value
);
4020 else if (test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
))
4021 gpio_set_open_source_value_commit(desc
, value
);
4023 gpiod_set_raw_value_commit(desc
, value
);
4027 * gpiod_set_value() - assign a gpio's value
4028 * @desc: gpio whose value will be assigned
4029 * @value: value to assign
4031 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
4032 * OPEN_DRAIN and OPEN_SOURCE flags into account.
4034 * This function can be called from contexts where we cannot sleep, and will
4035 * complain if the GPIO chip functions potentially sleep.
4037 void gpiod_set_value(struct gpio_desc
*desc
, int value
)
4039 VALIDATE_DESC_VOID(desc
);
4040 /* Should be using gpiod_set_value_cansleep() */
4041 WARN_ON(desc
->gdev
->chip
->can_sleep
);
4042 gpiod_set_value_nocheck(desc
, value
);
4044 EXPORT_SYMBOL_GPL(gpiod_set_value
);
4047 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
4048 * @array_size: number of elements in the descriptor array / value bitmap
4049 * @desc_array: array of GPIO descriptors whose values will be assigned
4050 * @array_info: information on applicability of fast bitmap processing path
4051 * @value_bitmap: bitmap of values to assign
4053 * Set the raw values of the GPIOs, i.e. the values of the physical lines
4054 * without regard for their ACTIVE_LOW status.
4056 * This function can be called from contexts where we cannot sleep, and will
4057 * complain if the GPIO chip functions potentially sleep.
4059 int gpiod_set_raw_array_value(unsigned int array_size
,
4060 struct gpio_desc
**desc_array
,
4061 struct gpio_array
*array_info
,
4062 unsigned long *value_bitmap
)
4066 return gpiod_set_array_value_complex(true, false, array_size
,
4067 desc_array
, array_info
, value_bitmap
);
4069 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value
);
4072 * gpiod_set_array_value() - assign values to an array of GPIOs
4073 * @array_size: number of elements in the descriptor array / value bitmap
4074 * @desc_array: array of GPIO descriptors whose values will be assigned
4075 * @array_info: information on applicability of fast bitmap processing path
4076 * @value_bitmap: bitmap of values to assign
4078 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
4081 * This function can be called from contexts where we cannot sleep, and will
4082 * complain if the GPIO chip functions potentially sleep.
4084 int gpiod_set_array_value(unsigned int array_size
,
4085 struct gpio_desc
**desc_array
,
4086 struct gpio_array
*array_info
,
4087 unsigned long *value_bitmap
)
4091 return gpiod_set_array_value_complex(false, false, array_size
,
4092 desc_array
, array_info
,
4095 EXPORT_SYMBOL_GPL(gpiod_set_array_value
);
4098 * gpiod_cansleep() - report whether gpio value access may sleep
4099 * @desc: gpio to check
4102 int gpiod_cansleep(const struct gpio_desc
*desc
)
4104 VALIDATE_DESC(desc
);
4105 return desc
->gdev
->chip
->can_sleep
;
4107 EXPORT_SYMBOL_GPL(gpiod_cansleep
);
4110 * gpiod_set_consumer_name() - set the consumer name for the descriptor
4111 * @desc: gpio to set the consumer name on
4112 * @name: the new consumer name
4114 int gpiod_set_consumer_name(struct gpio_desc
*desc
, const char *name
)
4116 VALIDATE_DESC(desc
);
4118 name
= kstrdup_const(name
, GFP_KERNEL
);
4123 kfree_const(desc
->label
);
4124 desc_set_label(desc
, name
);
4128 EXPORT_SYMBOL_GPL(gpiod_set_consumer_name
);
4131 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
4132 * @desc: gpio whose IRQ will be returned (already requested)
4134 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
4137 int gpiod_to_irq(const struct gpio_desc
*desc
)
4139 struct gpio_chip
*gc
;
4143 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
4144 * requires this function to not return zero on an invalid descriptor
4145 * but rather a negative error number.
4147 if (!desc
|| IS_ERR(desc
) || !desc
->gdev
|| !desc
->gdev
->chip
)
4150 gc
= desc
->gdev
->chip
;
4151 offset
= gpio_chip_hwgpio(desc
);
4153 int retirq
= gc
->to_irq(gc
, offset
);
4155 /* Zero means NO_IRQ */
4163 EXPORT_SYMBOL_GPL(gpiod_to_irq
);
4166 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
4167 * @gc: the chip the GPIO to lock belongs to
4168 * @offset: the offset of the GPIO to lock as IRQ
4170 * This is used directly by GPIO drivers that want to lock down
4171 * a certain GPIO line to be used for IRQs.
4173 int gpiochip_lock_as_irq(struct gpio_chip
*gc
, unsigned int offset
)
4175 struct gpio_desc
*desc
;
4177 desc
= gpiochip_get_desc(gc
, offset
);
4179 return PTR_ERR(desc
);
4182 * If it's fast: flush the direction setting if something changed
4185 if (!gc
->can_sleep
&& gc
->get_direction
) {
4186 int dir
= gpiod_get_direction(desc
);
4189 chip_err(gc
, "%s: cannot get GPIO direction\n",
4195 if (test_bit(FLAG_IS_OUT
, &desc
->flags
)) {
4197 "%s: tried to flag a GPIO set as output for IRQ\n",
4202 set_bit(FLAG_USED_AS_IRQ
, &desc
->flags
);
4203 set_bit(FLAG_IRQ_IS_ENABLED
, &desc
->flags
);
4206 * If the consumer has not set up a label (such as when the
4207 * IRQ is referenced from .to_irq()) we set up a label here
4208 * so it is clear this is used as an interrupt.
4211 desc_set_label(desc
, "interrupt");
4215 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq
);
4218 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
4219 * @gc: the chip the GPIO to lock belongs to
4220 * @offset: the offset of the GPIO to lock as IRQ
4222 * This is used directly by GPIO drivers that want to indicate
4223 * that a certain GPIO is no longer used exclusively for IRQ.
4225 void gpiochip_unlock_as_irq(struct gpio_chip
*gc
, unsigned int offset
)
4227 struct gpio_desc
*desc
;
4229 desc
= gpiochip_get_desc(gc
, offset
);
4233 clear_bit(FLAG_USED_AS_IRQ
, &desc
->flags
);
4234 clear_bit(FLAG_IRQ_IS_ENABLED
, &desc
->flags
);
4236 /* If we only had this marking, erase it */
4237 if (desc
->label
&& !strcmp(desc
->label
, "interrupt"))
4238 desc_set_label(desc
, NULL
);
4240 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq
);
4242 void gpiochip_disable_irq(struct gpio_chip
*gc
, unsigned int offset
)
4244 struct gpio_desc
*desc
= gpiochip_get_desc(gc
, offset
);
4246 if (!IS_ERR(desc
) &&
4247 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ
, &desc
->flags
)))
4248 clear_bit(FLAG_IRQ_IS_ENABLED
, &desc
->flags
);
4250 EXPORT_SYMBOL_GPL(gpiochip_disable_irq
);
4252 void gpiochip_enable_irq(struct gpio_chip
*gc
, unsigned int offset
)
4254 struct gpio_desc
*desc
= gpiochip_get_desc(gc
, offset
);
4256 if (!IS_ERR(desc
) &&
4257 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ
, &desc
->flags
))) {
4258 WARN_ON(test_bit(FLAG_IS_OUT
, &desc
->flags
));
4259 set_bit(FLAG_IRQ_IS_ENABLED
, &desc
->flags
);
4262 EXPORT_SYMBOL_GPL(gpiochip_enable_irq
);
4264 bool gpiochip_line_is_irq(struct gpio_chip
*gc
, unsigned int offset
)
4266 if (offset
>= gc
->ngpio
)
4269 return test_bit(FLAG_USED_AS_IRQ
, &gc
->gpiodev
->descs
[offset
].flags
);
4271 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq
);
4273 int gpiochip_reqres_irq(struct gpio_chip
*gc
, unsigned int offset
)
4277 if (!try_module_get(gc
->gpiodev
->owner
))
4280 ret
= gpiochip_lock_as_irq(gc
, offset
);
4282 chip_err(gc
, "unable to lock HW IRQ %u for IRQ\n", offset
);
4283 module_put(gc
->gpiodev
->owner
);
4288 EXPORT_SYMBOL_GPL(gpiochip_reqres_irq
);
4290 void gpiochip_relres_irq(struct gpio_chip
*gc
, unsigned int offset
)
4292 gpiochip_unlock_as_irq(gc
, offset
);
4293 module_put(gc
->gpiodev
->owner
);
4295 EXPORT_SYMBOL_GPL(gpiochip_relres_irq
);
4297 bool gpiochip_line_is_open_drain(struct gpio_chip
*gc
, unsigned int offset
)
4299 if (offset
>= gc
->ngpio
)
4302 return test_bit(FLAG_OPEN_DRAIN
, &gc
->gpiodev
->descs
[offset
].flags
);
4304 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain
);
4306 bool gpiochip_line_is_open_source(struct gpio_chip
*gc
, unsigned int offset
)
4308 if (offset
>= gc
->ngpio
)
4311 return test_bit(FLAG_OPEN_SOURCE
, &gc
->gpiodev
->descs
[offset
].flags
);
4313 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source
);
4315 bool gpiochip_line_is_persistent(struct gpio_chip
*gc
, unsigned int offset
)
4317 if (offset
>= gc
->ngpio
)
4320 return !test_bit(FLAG_TRANSITORY
, &gc
->gpiodev
->descs
[offset
].flags
);
4322 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent
);
4325 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
4326 * @desc: gpio whose value will be returned
4328 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
4329 * its ACTIVE_LOW status, or negative errno on failure.
4331 * This function is to be called from contexts that can sleep.
4333 int gpiod_get_raw_value_cansleep(const struct gpio_desc
*desc
)
4335 might_sleep_if(extra_checks
);
4336 VALIDATE_DESC(desc
);
4337 return gpiod_get_raw_value_commit(desc
);
4339 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep
);
4342 * gpiod_get_value_cansleep() - return a gpio's value
4343 * @desc: gpio whose value will be returned
4345 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
4346 * account, or negative errno on failure.
4348 * This function is to be called from contexts that can sleep.
4350 int gpiod_get_value_cansleep(const struct gpio_desc
*desc
)
4354 might_sleep_if(extra_checks
);
4355 VALIDATE_DESC(desc
);
4356 value
= gpiod_get_raw_value_commit(desc
);
4360 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
4365 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep
);
4368 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
4369 * @array_size: number of elements in the descriptor array / value bitmap
4370 * @desc_array: array of GPIO descriptors whose values will be read
4371 * @array_info: information on applicability of fast bitmap processing path
4372 * @value_bitmap: bitmap to store the read values
4374 * Read the raw values of the GPIOs, i.e. the values of the physical lines
4375 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
4376 * else an error code.
4378 * This function is to be called from contexts that can sleep.
4380 int gpiod_get_raw_array_value_cansleep(unsigned int array_size
,
4381 struct gpio_desc
**desc_array
,
4382 struct gpio_array
*array_info
,
4383 unsigned long *value_bitmap
)
4385 might_sleep_if(extra_checks
);
4388 return gpiod_get_array_value_complex(true, true, array_size
,
4389 desc_array
, array_info
,
4392 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep
);
4395 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
4396 * @array_size: number of elements in the descriptor array / value bitmap
4397 * @desc_array: array of GPIO descriptors whose values will be read
4398 * @array_info: information on applicability of fast bitmap processing path
4399 * @value_bitmap: bitmap to store the read values
4401 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
4402 * into account. Return 0 in case of success, else an error code.
4404 * This function is to be called from contexts that can sleep.
4406 int gpiod_get_array_value_cansleep(unsigned int array_size
,
4407 struct gpio_desc
**desc_array
,
4408 struct gpio_array
*array_info
,
4409 unsigned long *value_bitmap
)
4411 might_sleep_if(extra_checks
);
4414 return gpiod_get_array_value_complex(false, true, array_size
,
4415 desc_array
, array_info
,
4418 EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep
);
4421 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
4422 * @desc: gpio whose value will be assigned
4423 * @value: value to assign
4425 * Set the raw value of the GPIO, i.e. the value of its physical line without
4426 * regard for its ACTIVE_LOW status.
4428 * This function is to be called from contexts that can sleep.
4430 void gpiod_set_raw_value_cansleep(struct gpio_desc
*desc
, int value
)
4432 might_sleep_if(extra_checks
);
4433 VALIDATE_DESC_VOID(desc
);
4434 gpiod_set_raw_value_commit(desc
, value
);
4436 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep
);
4439 * gpiod_set_value_cansleep() - assign a gpio's value
4440 * @desc: gpio whose value will be assigned
4441 * @value: value to assign
4443 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
4446 * This function is to be called from contexts that can sleep.
4448 void gpiod_set_value_cansleep(struct gpio_desc
*desc
, int value
)
4450 might_sleep_if(extra_checks
);
4451 VALIDATE_DESC_VOID(desc
);
4452 gpiod_set_value_nocheck(desc
, value
);
4454 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep
);
4457 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
4458 * @array_size: number of elements in the descriptor array / value bitmap
4459 * @desc_array: array of GPIO descriptors whose values will be assigned
4460 * @array_info: information on applicability of fast bitmap processing path
4461 * @value_bitmap: bitmap of values to assign
4463 * Set the raw values of the GPIOs, i.e. the values of the physical lines
4464 * without regard for their ACTIVE_LOW status.
4466 * This function is to be called from contexts that can sleep.
4468 int gpiod_set_raw_array_value_cansleep(unsigned int array_size
,
4469 struct gpio_desc
**desc_array
,
4470 struct gpio_array
*array_info
,
4471 unsigned long *value_bitmap
)
4473 might_sleep_if(extra_checks
);
4476 return gpiod_set_array_value_complex(true, true, array_size
, desc_array
,
4477 array_info
, value_bitmap
);
4479 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep
);
4482 * gpiod_add_lookup_tables() - register GPIO device consumers
4483 * @tables: list of tables of consumers to register
4484 * @n: number of tables in the list
4486 void gpiod_add_lookup_tables(struct gpiod_lookup_table
**tables
, size_t n
)
4490 mutex_lock(&gpio_lookup_lock
);
4492 for (i
= 0; i
< n
; i
++)
4493 list_add_tail(&tables
[i
]->list
, &gpio_lookup_list
);
4495 mutex_unlock(&gpio_lookup_lock
);
4499 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
4500 * @array_size: number of elements in the descriptor array / value bitmap
4501 * @desc_array: array of GPIO descriptors whose values will be assigned
4502 * @array_info: information on applicability of fast bitmap processing path
4503 * @value_bitmap: bitmap of values to assign
4505 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
4508 * This function is to be called from contexts that can sleep.
4510 int gpiod_set_array_value_cansleep(unsigned int array_size
,
4511 struct gpio_desc
**desc_array
,
4512 struct gpio_array
*array_info
,
4513 unsigned long *value_bitmap
)
4515 might_sleep_if(extra_checks
);
4518 return gpiod_set_array_value_complex(false, true, array_size
,
4519 desc_array
, array_info
,
4522 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep
);
4525 * gpiod_add_lookup_table() - register GPIO device consumers
4526 * @table: table of consumers to register
4528 void gpiod_add_lookup_table(struct gpiod_lookup_table
*table
)
4530 mutex_lock(&gpio_lookup_lock
);
4532 list_add_tail(&table
->list
, &gpio_lookup_list
);
4534 mutex_unlock(&gpio_lookup_lock
);
4536 EXPORT_SYMBOL_GPL(gpiod_add_lookup_table
);
4539 * gpiod_remove_lookup_table() - unregister GPIO device consumers
4540 * @table: table of consumers to unregister
4542 void gpiod_remove_lookup_table(struct gpiod_lookup_table
*table
)
4544 mutex_lock(&gpio_lookup_lock
);
4546 list_del(&table
->list
);
4548 mutex_unlock(&gpio_lookup_lock
);
4550 EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table
);
4553 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
4554 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
4556 void gpiod_add_hogs(struct gpiod_hog
*hogs
)
4558 struct gpio_chip
*gc
;
4559 struct gpiod_hog
*hog
;
4561 mutex_lock(&gpio_machine_hogs_mutex
);
4563 for (hog
= &hogs
[0]; hog
->chip_label
; hog
++) {
4564 list_add_tail(&hog
->list
, &gpio_machine_hogs
);
4567 * The chip may have been registered earlier, so check if it
4568 * exists and, if so, try to hog the line now.
4570 gc
= find_chip_by_name(hog
->chip_label
);
4572 gpiochip_machine_hog(gc
, hog
);
4575 mutex_unlock(&gpio_machine_hogs_mutex
);
4577 EXPORT_SYMBOL_GPL(gpiod_add_hogs
);
4579 static struct gpiod_lookup_table
*gpiod_find_lookup_table(struct device
*dev
)
4581 const char *dev_id
= dev
? dev_name(dev
) : NULL
;
4582 struct gpiod_lookup_table
*table
;
4584 mutex_lock(&gpio_lookup_lock
);
4586 list_for_each_entry(table
, &gpio_lookup_list
, list
) {
4587 if (table
->dev_id
&& dev_id
) {
4589 * Valid strings on both ends, must be identical to have
4592 if (!strcmp(table
->dev_id
, dev_id
))
4596 * One of the pointers is NULL, so both must be to have
4599 if (dev_id
== table
->dev_id
)
4606 mutex_unlock(&gpio_lookup_lock
);
4610 static struct gpio_desc
*gpiod_find(struct device
*dev
, const char *con_id
,
4611 unsigned int idx
, unsigned long *flags
)
4613 struct gpio_desc
*desc
= ERR_PTR(-ENOENT
);
4614 struct gpiod_lookup_table
*table
;
4615 struct gpiod_lookup
*p
;
4617 table
= gpiod_find_lookup_table(dev
);
4621 for (p
= &table
->table
[0]; p
->chip_label
; p
++) {
4622 struct gpio_chip
*gc
;
4624 /* idx must always match exactly */
4628 /* If the lookup entry has a con_id, require exact match */
4629 if (p
->con_id
&& (!con_id
|| strcmp(p
->con_id
, con_id
)))
4632 gc
= find_chip_by_name(p
->chip_label
);
4636 * As the lookup table indicates a chip with
4637 * p->chip_label should exist, assume it may
4638 * still appear later and let the interested
4639 * consumer be probed again or let the Deferred
4640 * Probe infrastructure handle the error.
4642 dev_warn(dev
, "cannot find GPIO chip %s, deferring\n",
4644 return ERR_PTR(-EPROBE_DEFER
);
4647 if (gc
->ngpio
<= p
->chip_hwnum
) {
4649 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
4650 idx
, p
->chip_hwnum
, gc
->ngpio
- 1,
4652 return ERR_PTR(-EINVAL
);
4655 desc
= gpiochip_get_desc(gc
, p
->chip_hwnum
);
4664 static int platform_gpio_count(struct device
*dev
, const char *con_id
)
4666 struct gpiod_lookup_table
*table
;
4667 struct gpiod_lookup
*p
;
4668 unsigned int count
= 0;
4670 table
= gpiod_find_lookup_table(dev
);
4674 for (p
= &table
->table
[0]; p
->chip_label
; p
++) {
4675 if ((con_id
&& p
->con_id
&& !strcmp(con_id
, p
->con_id
)) ||
4676 (!con_id
&& !p
->con_id
))
4686 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
4687 * @fwnode: handle of the firmware node
4688 * @con_id: function within the GPIO consumer
4689 * @index: index of the GPIO to obtain for the consumer
4690 * @flags: GPIO initialization flags
4691 * @label: label to attach to the requested GPIO
4693 * This function can be used for drivers that get their configuration
4694 * from opaque firmware.
4696 * The function properly finds the corresponding GPIO using whatever is the
4697 * underlying firmware interface and then makes sure that the GPIO
4698 * descriptor is requested before it is returned to the caller.
4701 * On successful request the GPIO pin is configured in accordance with
4704 * In case of error an ERR_PTR() is returned.
4706 struct gpio_desc
*fwnode_gpiod_get_index(struct fwnode_handle
*fwnode
,
4707 const char *con_id
, int index
,
4708 enum gpiod_flags flags
,
4711 struct gpio_desc
*desc
;
4712 char prop_name
[32]; /* 32 is max size of property name */
4715 for (i
= 0; i
< ARRAY_SIZE(gpio_suffixes
); i
++) {
4717 snprintf(prop_name
, sizeof(prop_name
), "%s-%s",
4718 con_id
, gpio_suffixes
[i
]);
4720 snprintf(prop_name
, sizeof(prop_name
), "%s",
4723 desc
= fwnode_get_named_gpiod(fwnode
, prop_name
, index
, flags
,
4725 if (!IS_ERR(desc
) || (PTR_ERR(desc
) != -ENOENT
))
4731 EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index
);
4734 * gpiod_count - return the number of GPIOs associated with a device / function
4735 * or -ENOENT if no GPIO has been assigned to the requested function
4736 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4737 * @con_id: function within the GPIO consumer
4739 int gpiod_count(struct device
*dev
, const char *con_id
)
4741 int count
= -ENOENT
;
4743 if (IS_ENABLED(CONFIG_OF
) && dev
&& dev
->of_node
)
4744 count
= of_gpio_get_count(dev
, con_id
);
4745 else if (IS_ENABLED(CONFIG_ACPI
) && dev
&& ACPI_HANDLE(dev
))
4746 count
= acpi_gpio_count(dev
, con_id
);
4749 count
= platform_gpio_count(dev
, con_id
);
4753 EXPORT_SYMBOL_GPL(gpiod_count
);
4756 * gpiod_get - obtain a GPIO for a given GPIO function
4757 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4758 * @con_id: function within the GPIO consumer
4759 * @flags: optional GPIO initialization flags
4761 * Return the GPIO descriptor corresponding to the function con_id of device
4762 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
4763 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
4765 struct gpio_desc
*__must_check
gpiod_get(struct device
*dev
, const char *con_id
,
4766 enum gpiod_flags flags
)
4768 return gpiod_get_index(dev
, con_id
, 0, flags
);
4770 EXPORT_SYMBOL_GPL(gpiod_get
);
4773 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
4774 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4775 * @con_id: function within the GPIO consumer
4776 * @flags: optional GPIO initialization flags
4778 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
4779 * the requested function it will return NULL. This is convenient for drivers
4780 * that need to handle optional GPIOs.
4782 struct gpio_desc
*__must_check
gpiod_get_optional(struct device
*dev
,
4784 enum gpiod_flags flags
)
4786 return gpiod_get_index_optional(dev
, con_id
, 0, flags
);
4788 EXPORT_SYMBOL_GPL(gpiod_get_optional
);
4792 * gpiod_configure_flags - helper function to configure a given GPIO
4793 * @desc: gpio whose value will be assigned
4794 * @con_id: function within the GPIO consumer
4795 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4796 * of_find_gpio() or of_get_gpio_hog()
4797 * @dflags: gpiod_flags - optional GPIO initialization flags
4799 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
4800 * requested function and/or index, or another IS_ERR() code if an error
4801 * occurred while trying to acquire the GPIO.
4803 int gpiod_configure_flags(struct gpio_desc
*desc
, const char *con_id
,
4804 unsigned long lflags
, enum gpiod_flags dflags
)
4808 if (lflags
& GPIO_ACTIVE_LOW
)
4809 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
4811 if (lflags
& GPIO_OPEN_DRAIN
)
4812 set_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
4813 else if (dflags
& GPIOD_FLAGS_BIT_OPEN_DRAIN
) {
4815 * This enforces open drain mode from the consumer side.
4816 * This is necessary for some busses like I2C, but the lookup
4817 * should *REALLY* have specified them as open drain in the
4818 * first place, so print a little warning here.
4820 set_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
4822 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
4825 if (lflags
& GPIO_OPEN_SOURCE
)
4826 set_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
4828 if ((lflags
& GPIO_PULL_UP
) && (lflags
& GPIO_PULL_DOWN
)) {
4830 "both pull-up and pull-down enabled, invalid configuration\n");
4834 if (lflags
& GPIO_PULL_UP
)
4835 set_bit(FLAG_PULL_UP
, &desc
->flags
);
4836 else if (lflags
& GPIO_PULL_DOWN
)
4837 set_bit(FLAG_PULL_DOWN
, &desc
->flags
);
4839 ret
= gpiod_set_transitory(desc
, (lflags
& GPIO_TRANSITORY
));
4843 /* No particular flag request, return here... */
4844 if (!(dflags
& GPIOD_FLAGS_BIT_DIR_SET
)) {
4845 pr_debug("no flags found for %s\n", con_id
);
4850 if (dflags
& GPIOD_FLAGS_BIT_DIR_OUT
)
4851 ret
= gpiod_direction_output(desc
,
4852 !!(dflags
& GPIOD_FLAGS_BIT_DIR_VAL
));
4854 ret
= gpiod_direction_input(desc
);
4860 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
4861 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4862 * @con_id: function within the GPIO consumer
4863 * @idx: index of the GPIO to obtain in the consumer
4864 * @flags: optional GPIO initialization flags
4866 * This variant of gpiod_get() allows to access GPIOs other than the first
4867 * defined one for functions that define several GPIOs.
4869 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
4870 * requested function and/or index, or another IS_ERR() code if an error
4871 * occurred while trying to acquire the GPIO.
4873 struct gpio_desc
*__must_check
gpiod_get_index(struct device
*dev
,
4876 enum gpiod_flags flags
)
4878 unsigned long lookupflags
= GPIO_LOOKUP_FLAGS_DEFAULT
;
4879 struct gpio_desc
*desc
= NULL
;
4881 /* Maybe we have a device name, maybe not */
4882 const char *devname
= dev
? dev_name(dev
) : "?";
4884 dev_dbg(dev
, "GPIO lookup for consumer %s\n", con_id
);
4887 /* Using device tree? */
4888 if (IS_ENABLED(CONFIG_OF
) && dev
->of_node
) {
4889 dev_dbg(dev
, "using device tree for GPIO lookup\n");
4890 desc
= of_find_gpio(dev
, con_id
, idx
, &lookupflags
);
4891 } else if (ACPI_COMPANION(dev
)) {
4892 dev_dbg(dev
, "using ACPI for GPIO lookup\n");
4893 desc
= acpi_find_gpio(dev
, con_id
, idx
, &flags
, &lookupflags
);
4898 * Either we are not using DT or ACPI, or their lookup did not return
4899 * a result. In that case, use platform lookup as a fallback.
4901 if (!desc
|| desc
== ERR_PTR(-ENOENT
)) {
4902 dev_dbg(dev
, "using lookup tables for GPIO lookup\n");
4903 desc
= gpiod_find(dev
, con_id
, idx
, &lookupflags
);
4907 dev_dbg(dev
, "No GPIO consumer %s found\n", con_id
);
4912 * If a connection label was passed use that, else attempt to use
4913 * the device name as label
4915 ret
= gpiod_request(desc
, con_id
? con_id
: devname
);
4917 if (ret
== -EBUSY
&& flags
& GPIOD_FLAGS_BIT_NONEXCLUSIVE
) {
4919 * This happens when there are several consumers for
4920 * the same GPIO line: we just return here without
4921 * further initialization. It is a bit if a hack.
4922 * This is necessary to support fixed regulators.
4924 * FIXME: Make this more sane and safe.
4926 dev_info(dev
, "nonexclusive access to GPIO for %s\n",
4927 con_id
? con_id
: devname
);
4930 return ERR_PTR(ret
);
4934 ret
= gpiod_configure_flags(desc
, con_id
, lookupflags
, flags
);
4936 dev_dbg(dev
, "setup of GPIO %s failed\n", con_id
);
4938 return ERR_PTR(ret
);
4943 EXPORT_SYMBOL_GPL(gpiod_get_index
);
4946 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
4947 * @fwnode: handle of the firmware node
4948 * @propname: name of the firmware property representing the GPIO
4949 * @index: index of the GPIO to obtain for the consumer
4950 * @dflags: GPIO initialization flags
4951 * @label: label to attach to the requested GPIO
4953 * This function can be used for drivers that get their configuration
4954 * from opaque firmware.
4956 * The function properly finds the corresponding GPIO using whatever is the
4957 * underlying firmware interface and then makes sure that the GPIO
4958 * descriptor is requested before it is returned to the caller.
4961 * On successful request the GPIO pin is configured in accordance with
4964 * In case of error an ERR_PTR() is returned.
4966 struct gpio_desc
*fwnode_get_named_gpiod(struct fwnode_handle
*fwnode
,
4967 const char *propname
, int index
,
4968 enum gpiod_flags dflags
,
4971 unsigned long lflags
= GPIO_LOOKUP_FLAGS_DEFAULT
;
4972 struct gpio_desc
*desc
= ERR_PTR(-ENODEV
);
4976 return ERR_PTR(-EINVAL
);
4978 if (is_of_node(fwnode
)) {
4979 desc
= gpiod_get_from_of_node(to_of_node(fwnode
),
4984 } else if (is_acpi_node(fwnode
)) {
4985 struct acpi_gpio_info info
;
4987 desc
= acpi_node_get_gpiod(fwnode
, propname
, index
, &info
);
4991 acpi_gpio_update_gpiod_flags(&dflags
, &info
);
4992 acpi_gpio_update_gpiod_lookup_flags(&lflags
, &info
);
4995 /* Currently only ACPI takes this path */
4996 ret
= gpiod_request(desc
, label
);
4998 return ERR_PTR(ret
);
5000 ret
= gpiod_configure_flags(desc
, propname
, lflags
, dflags
);
5003 return ERR_PTR(ret
);
5008 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod
);
5011 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
5013 * @dev: GPIO consumer, can be NULL for system-global GPIOs
5014 * @con_id: function within the GPIO consumer
5015 * @index: index of the GPIO to obtain in the consumer
5016 * @flags: optional GPIO initialization flags
5018 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
5019 * specified index was assigned to the requested function it will return NULL.
5020 * This is convenient for drivers that need to handle optional GPIOs.
5022 struct gpio_desc
*__must_check
gpiod_get_index_optional(struct device
*dev
,
5025 enum gpiod_flags flags
)
5027 struct gpio_desc
*desc
;
5029 desc
= gpiod_get_index(dev
, con_id
, index
, flags
);
5031 if (PTR_ERR(desc
) == -ENOENT
)
5037 EXPORT_SYMBOL_GPL(gpiod_get_index_optional
);
5040 * gpiod_hog - Hog the specified GPIO desc given the provided flags
5041 * @desc: gpio whose value will be assigned
5042 * @name: gpio line name
5043 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
5044 * of_find_gpio() or of_get_gpio_hog()
5045 * @dflags: gpiod_flags - optional GPIO initialization flags
5047 int gpiod_hog(struct gpio_desc
*desc
, const char *name
,
5048 unsigned long lflags
, enum gpiod_flags dflags
)
5050 struct gpio_chip
*gc
;
5051 struct gpio_desc
*local_desc
;
5055 gc
= gpiod_to_chip(desc
);
5056 hwnum
= gpio_chip_hwgpio(desc
);
5058 local_desc
= gpiochip_request_own_desc(gc
, hwnum
, name
,
5060 if (IS_ERR(local_desc
)) {
5061 ret
= PTR_ERR(local_desc
);
5062 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
5063 name
, gc
->label
, hwnum
, ret
);
5067 /* Mark GPIO as hogged so it can be identified and removed later */
5068 set_bit(FLAG_IS_HOGGED
, &desc
->flags
);
5070 pr_info("GPIO line %d (%s) hogged as %s%s\n",
5071 desc_to_gpio(desc
), name
,
5072 (dflags
& GPIOD_FLAGS_BIT_DIR_OUT
) ? "output" : "input",
5073 (dflags
& GPIOD_FLAGS_BIT_DIR_OUT
) ?
5074 (dflags
& GPIOD_FLAGS_BIT_DIR_VAL
) ? "/high" : "/low" : "");
5080 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
5081 * @gc: gpio chip to act on
5083 static void gpiochip_free_hogs(struct gpio_chip
*gc
)
5087 for (id
= 0; id
< gc
->ngpio
; id
++) {
5088 if (test_bit(FLAG_IS_HOGGED
, &gc
->gpiodev
->descs
[id
].flags
))
5089 gpiochip_free_own_desc(&gc
->gpiodev
->descs
[id
]);
5094 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
5095 * @dev: GPIO consumer, can be NULL for system-global GPIOs
5096 * @con_id: function within the GPIO consumer
5097 * @flags: optional GPIO initialization flags
5099 * This function acquires all the GPIOs defined under a given function.
5101 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
5102 * no GPIO has been assigned to the requested function, or another IS_ERR()
5103 * code if an error occurred while trying to acquire the GPIOs.
5105 struct gpio_descs
*__must_check
gpiod_get_array(struct device
*dev
,
5107 enum gpiod_flags flags
)
5109 struct gpio_desc
*desc
;
5110 struct gpio_descs
*descs
;
5111 struct gpio_array
*array_info
= NULL
;
5112 struct gpio_chip
*gc
;
5113 int count
, bitmap_size
;
5115 count
= gpiod_count(dev
, con_id
);
5117 return ERR_PTR(count
);
5119 descs
= kzalloc(struct_size(descs
, desc
, count
), GFP_KERNEL
);
5121 return ERR_PTR(-ENOMEM
);
5123 for (descs
->ndescs
= 0; descs
->ndescs
< count
; ) {
5124 desc
= gpiod_get_index(dev
, con_id
, descs
->ndescs
, flags
);
5126 gpiod_put_array(descs
);
5127 return ERR_CAST(desc
);
5130 descs
->desc
[descs
->ndescs
] = desc
;
5132 gc
= gpiod_to_chip(desc
);
5134 * If pin hardware number of array member 0 is also 0, select
5135 * its chip as a candidate for fast bitmap processing path.
5137 if (descs
->ndescs
== 0 && gpio_chip_hwgpio(desc
) == 0) {
5138 struct gpio_descs
*array
;
5140 bitmap_size
= BITS_TO_LONGS(gc
->ngpio
> count
?
5143 array
= kzalloc(struct_size(descs
, desc
, count
) +
5144 struct_size(array_info
, invert_mask
,
5145 3 * bitmap_size
), GFP_KERNEL
);
5147 gpiod_put_array(descs
);
5148 return ERR_PTR(-ENOMEM
);
5151 memcpy(array
, descs
,
5152 struct_size(descs
, desc
, descs
->ndescs
+ 1));
5156 array_info
= (void *)(descs
->desc
+ count
);
5157 array_info
->get_mask
= array_info
->invert_mask
+
5159 array_info
->set_mask
= array_info
->get_mask
+
5162 array_info
->desc
= descs
->desc
;
5163 array_info
->size
= count
;
5164 array_info
->chip
= gc
;
5165 bitmap_set(array_info
->get_mask
, descs
->ndescs
,
5166 count
- descs
->ndescs
);
5167 bitmap_set(array_info
->set_mask
, descs
->ndescs
,
5168 count
- descs
->ndescs
);
5169 descs
->info
= array_info
;
5171 /* Unmark array members which don't belong to the 'fast' chip */
5172 if (array_info
&& array_info
->chip
!= gc
) {
5173 __clear_bit(descs
->ndescs
, array_info
->get_mask
);
5174 __clear_bit(descs
->ndescs
, array_info
->set_mask
);
5177 * Detect array members which belong to the 'fast' chip
5178 * but their pins are not in hardware order.
5180 else if (array_info
&&
5181 gpio_chip_hwgpio(desc
) != descs
->ndescs
) {
5183 * Don't use fast path if all array members processed so
5184 * far belong to the same chip as this one but its pin
5185 * hardware number is different from its array index.
5187 if (bitmap_full(array_info
->get_mask
, descs
->ndescs
)) {
5190 __clear_bit(descs
->ndescs
,
5191 array_info
->get_mask
);
5192 __clear_bit(descs
->ndescs
,
5193 array_info
->set_mask
);
5195 } else if (array_info
) {
5196 /* Exclude open drain or open source from fast output */
5197 if (gpiochip_line_is_open_drain(gc
, descs
->ndescs
) ||
5198 gpiochip_line_is_open_source(gc
, descs
->ndescs
))
5199 __clear_bit(descs
->ndescs
,
5200 array_info
->set_mask
);
5201 /* Identify 'fast' pins which require invertion */
5202 if (gpiod_is_active_low(desc
))
5203 __set_bit(descs
->ndescs
,
5204 array_info
->invert_mask
);
5211 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
5212 array_info
->chip
->label
, array_info
->size
,
5213 *array_info
->get_mask
, *array_info
->set_mask
,
5214 *array_info
->invert_mask
);
5217 EXPORT_SYMBOL_GPL(gpiod_get_array
);
5220 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
5222 * @dev: GPIO consumer, can be NULL for system-global GPIOs
5223 * @con_id: function within the GPIO consumer
5224 * @flags: optional GPIO initialization flags
5226 * This is equivalent to gpiod_get_array(), except that when no GPIO was
5227 * assigned to the requested function it will return NULL.
5229 struct gpio_descs
*__must_check
gpiod_get_array_optional(struct device
*dev
,
5231 enum gpiod_flags flags
)
5233 struct gpio_descs
*descs
;
5235 descs
= gpiod_get_array(dev
, con_id
, flags
);
5236 if (PTR_ERR(descs
) == -ENOENT
)
5241 EXPORT_SYMBOL_GPL(gpiod_get_array_optional
);
5244 * gpiod_put - dispose of a GPIO descriptor
5245 * @desc: GPIO descriptor to dispose of
5247 * No descriptor can be used after gpiod_put() has been called on it.
5249 void gpiod_put(struct gpio_desc
*desc
)
5254 EXPORT_SYMBOL_GPL(gpiod_put
);
5257 * gpiod_put_array - dispose of multiple GPIO descriptors
5258 * @descs: struct gpio_descs containing an array of descriptors
5260 void gpiod_put_array(struct gpio_descs
*descs
)
5264 for (i
= 0; i
< descs
->ndescs
; i
++)
5265 gpiod_put(descs
->desc
[i
]);
5269 EXPORT_SYMBOL_GPL(gpiod_put_array
);
5271 static int __init
gpiolib_dev_init(void)
5275 /* Register GPIO sysfs bus */
5276 ret
= bus_register(&gpio_bus_type
);
5278 pr_err("gpiolib: could not register GPIO bus type\n");
5282 ret
= alloc_chrdev_region(&gpio_devt
, 0, GPIO_DEV_MAX
, GPIOCHIP_NAME
);
5284 pr_err("gpiolib: failed to allocate char dev region\n");
5285 bus_unregister(&gpio_bus_type
);
5289 gpiolib_initialized
= true;
5290 gpiochip_setup_devs();
5292 if (IS_ENABLED(CONFIG_OF_DYNAMIC
))
5293 WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier
));
5297 core_initcall(gpiolib_dev_init
);
5299 #ifdef CONFIG_DEBUG_FS
5301 static void gpiolib_dbg_show(struct seq_file
*s
, struct gpio_device
*gdev
)
5304 struct gpio_chip
*gc
= gdev
->chip
;
5305 unsigned gpio
= gdev
->base
;
5306 struct gpio_desc
*gdesc
= &gdev
->descs
[0];
5311 for (i
= 0; i
< gdev
->ngpio
; i
++, gpio
++, gdesc
++) {
5312 if (!test_bit(FLAG_REQUESTED
, &gdesc
->flags
)) {
5314 seq_printf(s
, " gpio-%-3d (%-20.20s)\n",
5320 gpiod_get_direction(gdesc
);
5321 is_out
= test_bit(FLAG_IS_OUT
, &gdesc
->flags
);
5322 is_irq
= test_bit(FLAG_USED_AS_IRQ
, &gdesc
->flags
);
5323 active_low
= test_bit(FLAG_ACTIVE_LOW
, &gdesc
->flags
);
5324 seq_printf(s
, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s",
5325 gpio
, gdesc
->name
? gdesc
->name
: "", gdesc
->label
,
5326 is_out
? "out" : "in ",
5327 gc
->get
? (gc
->get(gc
, i
) ? "hi" : "lo") : "? ",
5328 is_irq
? "IRQ " : "",
5329 active_low
? "ACTIVE LOW" : "");
5330 seq_printf(s
, "\n");
5334 static void *gpiolib_seq_start(struct seq_file
*s
, loff_t
*pos
)
5336 unsigned long flags
;
5337 struct gpio_device
*gdev
= NULL
;
5338 loff_t index
= *pos
;
5342 spin_lock_irqsave(&gpio_lock
, flags
);
5343 list_for_each_entry(gdev
, &gpio_devices
, list
)
5345 spin_unlock_irqrestore(&gpio_lock
, flags
);
5348 spin_unlock_irqrestore(&gpio_lock
, flags
);
5353 static void *gpiolib_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
5355 unsigned long flags
;
5356 struct gpio_device
*gdev
= v
;
5359 spin_lock_irqsave(&gpio_lock
, flags
);
5360 if (list_is_last(&gdev
->list
, &gpio_devices
))
5363 ret
= list_entry(gdev
->list
.next
, struct gpio_device
, list
);
5364 spin_unlock_irqrestore(&gpio_lock
, flags
);
5372 static void gpiolib_seq_stop(struct seq_file
*s
, void *v
)
5376 static int gpiolib_seq_show(struct seq_file
*s
, void *v
)
5378 struct gpio_device
*gdev
= v
;
5379 struct gpio_chip
*gc
= gdev
->chip
;
5380 struct device
*parent
;
5383 seq_printf(s
, "%s%s: (dangling chip)", (char *)s
->private,
5384 dev_name(&gdev
->dev
));
5388 seq_printf(s
, "%s%s: GPIOs %d-%d", (char *)s
->private,
5389 dev_name(&gdev
->dev
),
5390 gdev
->base
, gdev
->base
+ gdev
->ngpio
- 1);
5391 parent
= gc
->parent
;
5393 seq_printf(s
, ", parent: %s/%s",
5394 parent
->bus
? parent
->bus
->name
: "no-bus",
5397 seq_printf(s
, ", %s", gc
->label
);
5399 seq_printf(s
, ", can sleep");
5400 seq_printf(s
, ":\n");
5403 gc
->dbg_show(s
, gc
);
5405 gpiolib_dbg_show(s
, gdev
);
5410 static const struct seq_operations gpiolib_seq_ops
= {
5411 .start
= gpiolib_seq_start
,
5412 .next
= gpiolib_seq_next
,
5413 .stop
= gpiolib_seq_stop
,
5414 .show
= gpiolib_seq_show
,
5417 static int gpiolib_open(struct inode
*inode
, struct file
*file
)
5419 return seq_open(file
, &gpiolib_seq_ops
);
5422 static const struct file_operations gpiolib_operations
= {
5423 .owner
= THIS_MODULE
,
5424 .open
= gpiolib_open
,
5426 .llseek
= seq_lseek
,
5427 .release
= seq_release
,
5430 static int __init
gpiolib_debugfs_init(void)
5432 /* /sys/kernel/debug/gpio */
5433 debugfs_create_file("gpio", S_IFREG
| S_IRUGO
, NULL
, NULL
,
5434 &gpiolib_operations
);
5437 subsys_initcall(gpiolib_debugfs_init
);
5439 #endif /* DEBUG_FS */