1 #include <linux/bitmap.h>
2 #include <linux/kernel.h>
3 #include <linux/module.h>
4 #include <linux/interrupt.h>
6 #include <linux/spinlock.h>
7 #include <linux/list.h>
8 #include <linux/device.h>
10 #include <linux/debugfs.h>
11 #include <linux/seq_file.h>
12 #include <linux/gpio.h>
13 #include <linux/of_gpio.h>
14 #include <linux/idr.h>
15 #include <linux/slab.h>
16 #include <linux/acpi.h>
17 #include <linux/gpio/driver.h>
18 #include <linux/gpio/machine.h>
19 #include <linux/pinctrl/consumer.h>
20 #include <linux/cdev.h>
22 #include <linux/uaccess.h>
23 #include <linux/compat.h>
24 #include <linux/anon_inodes.h>
25 #include <linux/file.h>
26 #include <linux/kfifo.h>
27 #include <linux/poll.h>
28 #include <linux/timekeeping.h>
29 #include <uapi/linux/gpio.h>
33 #define CREATE_TRACE_POINTS
34 #include <trace/events/gpio.h>
36 /* Implementation infrastructure for GPIO interfaces.
38 * The GPIO programming interface allows for inlining speed-critical
39 * get/set operations for common cases, so that access to SOC-integrated
40 * GPIOs can sometimes cost only an instruction or two per bit.
44 /* When debugging, extend minimal trust to callers and platform code.
45 * Also emit diagnostic messages that may help initial bringup, when
46 * board setup or driver bugs are most common.
48 * Otherwise, minimize overhead in what may be bitbanging codepaths.
51 #define extra_checks 1
53 #define extra_checks 0
56 /* Device and char device-related information */
57 static DEFINE_IDA(gpio_ida
);
58 static dev_t gpio_devt
;
59 #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
60 static struct bus_type gpio_bus_type
= {
64 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
65 * While any GPIO is requested, its gpio_chip is not removable;
66 * each GPIO's "requested" flag serves as a lock and refcount.
68 DEFINE_SPINLOCK(gpio_lock
);
70 static DEFINE_MUTEX(gpio_lookup_lock
);
71 static LIST_HEAD(gpio_lookup_list
);
72 LIST_HEAD(gpio_devices
);
74 static void gpiochip_free_hogs(struct gpio_chip
*chip
);
75 static int gpiochip_add_irqchip(struct gpio_chip
*gpiochip
,
76 struct lock_class_key
*lock_key
,
77 struct lock_class_key
*request_key
);
78 static void gpiochip_irqchip_remove(struct gpio_chip
*gpiochip
);
79 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip
*gpiochip
);
80 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip
*gpiochip
);
82 static bool gpiolib_initialized
;
84 static inline void desc_set_label(struct gpio_desc
*d
, const char *label
)
90 * gpio_to_desc - Convert a GPIO number to its descriptor
91 * @gpio: global GPIO number
94 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
95 * with the given number exists in the system.
97 struct gpio_desc
*gpio_to_desc(unsigned gpio
)
99 struct gpio_device
*gdev
;
102 spin_lock_irqsave(&gpio_lock
, flags
);
104 list_for_each_entry(gdev
, &gpio_devices
, list
) {
105 if (gdev
->base
<= gpio
&&
106 gdev
->base
+ gdev
->ngpio
> gpio
) {
107 spin_unlock_irqrestore(&gpio_lock
, flags
);
108 return &gdev
->descs
[gpio
- gdev
->base
];
112 spin_unlock_irqrestore(&gpio_lock
, flags
);
114 if (!gpio_is_valid(gpio
))
115 WARN(1, "invalid GPIO %d\n", gpio
);
119 EXPORT_SYMBOL_GPL(gpio_to_desc
);
122 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
123 * hardware number for this chip
125 * @hwnum: hardware number of the GPIO for this chip
128 * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists
129 * in the given chip for the specified hardware number.
131 struct gpio_desc
*gpiochip_get_desc(struct gpio_chip
*chip
,
134 struct gpio_device
*gdev
= chip
->gpiodev
;
136 if (hwnum
>= gdev
->ngpio
)
137 return ERR_PTR(-EINVAL
);
139 return &gdev
->descs
[hwnum
];
143 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
144 * @desc: GPIO descriptor
146 * This should disappear in the future but is needed since we still
147 * use GPIO numbers for error messages and sysfs nodes.
150 * The global GPIO number for the GPIO specified by its descriptor.
152 int desc_to_gpio(const struct gpio_desc
*desc
)
154 return desc
->gdev
->base
+ (desc
- &desc
->gdev
->descs
[0]);
156 EXPORT_SYMBOL_GPL(desc_to_gpio
);
160 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
161 * @desc: descriptor to return the chip of
163 struct gpio_chip
*gpiod_to_chip(const struct gpio_desc
*desc
)
165 if (!desc
|| !desc
->gdev
)
167 return desc
->gdev
->chip
;
169 EXPORT_SYMBOL_GPL(gpiod_to_chip
);
171 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
172 static int gpiochip_find_base(int ngpio
)
174 struct gpio_device
*gdev
;
175 int base
= ARCH_NR_GPIOS
- ngpio
;
177 list_for_each_entry_reverse(gdev
, &gpio_devices
, list
) {
178 /* found a free space? */
179 if (gdev
->base
+ gdev
->ngpio
<= base
)
182 /* nope, check the space right before the chip */
183 base
= gdev
->base
- ngpio
;
186 if (gpio_is_valid(base
)) {
187 pr_debug("%s: found new base at %d\n", __func__
, base
);
190 pr_err("%s: cannot find free range\n", __func__
);
196 * gpiod_get_direction - return the current direction of a GPIO
197 * @desc: GPIO to get the direction of
199 * Returns 0 for output, 1 for input, or an error code in case of error.
201 * This function may sleep if gpiod_cansleep() is true.
203 int gpiod_get_direction(struct gpio_desc
*desc
)
205 struct gpio_chip
*chip
;
207 int status
= -EINVAL
;
209 chip
= gpiod_to_chip(desc
);
210 offset
= gpio_chip_hwgpio(desc
);
212 if (!chip
->get_direction
)
215 status
= chip
->get_direction(chip
, offset
);
217 /* GPIOF_DIR_IN, or other positive */
219 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
223 set_bit(FLAG_IS_OUT
, &desc
->flags
);
227 EXPORT_SYMBOL_GPL(gpiod_get_direction
);
230 * Add a new chip to the global chips list, keeping the list of chips sorted
231 * by range(means [base, base + ngpio - 1]) order.
233 * Return -EBUSY if the new chip overlaps with some other chip's integer
236 static int gpiodev_add_to_list(struct gpio_device
*gdev
)
238 struct gpio_device
*prev
, *next
;
240 if (list_empty(&gpio_devices
)) {
241 /* initial entry in list */
242 list_add_tail(&gdev
->list
, &gpio_devices
);
246 next
= list_entry(gpio_devices
.next
, struct gpio_device
, list
);
247 if (gdev
->base
+ gdev
->ngpio
<= next
->base
) {
248 /* add before first entry */
249 list_add(&gdev
->list
, &gpio_devices
);
253 prev
= list_entry(gpio_devices
.prev
, struct gpio_device
, list
);
254 if (prev
->base
+ prev
->ngpio
<= gdev
->base
) {
255 /* add behind last entry */
256 list_add_tail(&gdev
->list
, &gpio_devices
);
260 list_for_each_entry_safe(prev
, next
, &gpio_devices
, list
) {
261 /* at the end of the list */
262 if (&next
->list
== &gpio_devices
)
265 /* add between prev and next */
266 if (prev
->base
+ prev
->ngpio
<= gdev
->base
267 && gdev
->base
+ gdev
->ngpio
<= next
->base
) {
268 list_add(&gdev
->list
, &prev
->list
);
273 dev_err(&gdev
->dev
, "GPIO integer space overlap, cannot add chip\n");
278 * Convert a GPIO name to its descriptor
280 static struct gpio_desc
*gpio_name_to_desc(const char * const name
)
282 struct gpio_device
*gdev
;
285 spin_lock_irqsave(&gpio_lock
, flags
);
287 list_for_each_entry(gdev
, &gpio_devices
, list
) {
290 for (i
= 0; i
!= gdev
->ngpio
; ++i
) {
291 struct gpio_desc
*desc
= &gdev
->descs
[i
];
293 if (!desc
->name
|| !name
)
296 if (!strcmp(desc
->name
, name
)) {
297 spin_unlock_irqrestore(&gpio_lock
, flags
);
303 spin_unlock_irqrestore(&gpio_lock
, flags
);
309 * Takes the names from gc->names and checks if they are all unique. If they
310 * are, they are assigned to their gpio descriptors.
312 * Warning if one of the names is already used for a different GPIO.
314 static int gpiochip_set_desc_names(struct gpio_chip
*gc
)
316 struct gpio_device
*gdev
= gc
->gpiodev
;
322 /* First check all names if they are unique */
323 for (i
= 0; i
!= gc
->ngpio
; ++i
) {
324 struct gpio_desc
*gpio
;
326 gpio
= gpio_name_to_desc(gc
->names
[i
]);
329 "Detected name collision for GPIO name '%s'\n",
333 /* Then add all names to the GPIO descriptors */
334 for (i
= 0; i
!= gc
->ngpio
; ++i
)
335 gdev
->descs
[i
].name
= gc
->names
[i
];
341 * GPIO line handle management
345 * struct linehandle_state - contains the state of a userspace handle
346 * @gdev: the GPIO device the handle pertains to
347 * @label: consumer label used to tag descriptors
348 * @descs: the GPIO descriptors held by this handle
349 * @numdescs: the number of descriptors held in the descs array
351 struct linehandle_state
{
352 struct gpio_device
*gdev
;
354 struct gpio_desc
*descs
[GPIOHANDLES_MAX
];
358 #define GPIOHANDLE_REQUEST_VALID_FLAGS \
359 (GPIOHANDLE_REQUEST_INPUT | \
360 GPIOHANDLE_REQUEST_OUTPUT | \
361 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
362 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
363 GPIOHANDLE_REQUEST_OPEN_SOURCE)
365 static long linehandle_ioctl(struct file
*filep
, unsigned int cmd
,
368 struct linehandle_state
*lh
= filep
->private_data
;
369 void __user
*ip
= (void __user
*)arg
;
370 struct gpiohandle_data ghd
;
371 int vals
[GPIOHANDLES_MAX
];
374 if (cmd
== GPIOHANDLE_GET_LINE_VALUES_IOCTL
) {
375 /* TODO: check if descriptors are really input */
376 int ret
= gpiod_get_array_value_complex(false,
384 memset(&ghd
, 0, sizeof(ghd
));
385 for (i
= 0; i
< lh
->numdescs
; i
++)
386 ghd
.values
[i
] = vals
[i
];
388 if (copy_to_user(ip
, &ghd
, sizeof(ghd
)))
392 } else if (cmd
== GPIOHANDLE_SET_LINE_VALUES_IOCTL
) {
393 /* TODO: check if descriptors are really output */
394 if (copy_from_user(&ghd
, ip
, sizeof(ghd
)))
397 /* Clamp all values to [0,1] */
398 for (i
= 0; i
< lh
->numdescs
; i
++)
399 vals
[i
] = !!ghd
.values
[i
];
401 /* Reuse the array setting function */
402 gpiod_set_array_value_complex(false,
413 static long linehandle_ioctl_compat(struct file
*filep
, unsigned int cmd
,
416 return linehandle_ioctl(filep
, cmd
, (unsigned long)compat_ptr(arg
));
420 static int linehandle_release(struct inode
*inode
, struct file
*filep
)
422 struct linehandle_state
*lh
= filep
->private_data
;
423 struct gpio_device
*gdev
= lh
->gdev
;
426 for (i
= 0; i
< lh
->numdescs
; i
++)
427 gpiod_free(lh
->descs
[i
]);
430 put_device(&gdev
->dev
);
434 static const struct file_operations linehandle_fileops
= {
435 .release
= linehandle_release
,
436 .owner
= THIS_MODULE
,
437 .llseek
= noop_llseek
,
438 .unlocked_ioctl
= linehandle_ioctl
,
440 .compat_ioctl
= linehandle_ioctl_compat
,
444 static int linehandle_create(struct gpio_device
*gdev
, void __user
*ip
)
446 struct gpiohandle_request handlereq
;
447 struct linehandle_state
*lh
;
452 if (copy_from_user(&handlereq
, ip
, sizeof(handlereq
)))
454 if ((handlereq
.lines
== 0) || (handlereq
.lines
> GPIOHANDLES_MAX
))
457 lflags
= handlereq
.flags
;
459 /* Return an error if an unknown flag is set */
460 if (lflags
& ~GPIOHANDLE_REQUEST_VALID_FLAGS
)
464 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
465 * the hardware actually supports enabling both at the same time the
466 * electrical result would be disastrous.
468 if ((lflags
& GPIOHANDLE_REQUEST_OPEN_DRAIN
) &&
469 (lflags
& GPIOHANDLE_REQUEST_OPEN_SOURCE
))
472 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
473 if (!(lflags
& GPIOHANDLE_REQUEST_OUTPUT
) &&
474 ((lflags
& GPIOHANDLE_REQUEST_OPEN_DRAIN
) ||
475 (lflags
& GPIOHANDLE_REQUEST_OPEN_SOURCE
)))
478 lh
= kzalloc(sizeof(*lh
), GFP_KERNEL
);
482 get_device(&gdev
->dev
);
484 /* Make sure this is terminated */
485 handlereq
.consumer_label
[sizeof(handlereq
.consumer_label
)-1] = '\0';
486 if (strlen(handlereq
.consumer_label
)) {
487 lh
->label
= kstrdup(handlereq
.consumer_label
,
495 /* Request each GPIO */
496 for (i
= 0; i
< handlereq
.lines
; i
++) {
497 u32 offset
= handlereq
.lineoffsets
[i
];
498 struct gpio_desc
*desc
;
500 if (offset
>= gdev
->ngpio
) {
505 desc
= &gdev
->descs
[offset
];
506 ret
= gpiod_request(desc
, lh
->label
);
511 if (lflags
& GPIOHANDLE_REQUEST_ACTIVE_LOW
)
512 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
513 if (lflags
& GPIOHANDLE_REQUEST_OPEN_DRAIN
)
514 set_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
515 if (lflags
& GPIOHANDLE_REQUEST_OPEN_SOURCE
)
516 set_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
518 ret
= gpiod_set_transitory(desc
, false);
523 * Lines have to be requested explicitly for input
524 * or output, else the line will be treated "as is".
526 if (lflags
& GPIOHANDLE_REQUEST_OUTPUT
) {
527 int val
= !!handlereq
.default_values
[i
];
529 ret
= gpiod_direction_output(desc
, val
);
532 } else if (lflags
& GPIOHANDLE_REQUEST_INPUT
) {
533 ret
= gpiod_direction_input(desc
);
537 dev_dbg(&gdev
->dev
, "registered chardev handle for line %d\n",
540 /* Let i point at the last handle */
542 lh
->numdescs
= handlereq
.lines
;
544 fd
= get_unused_fd_flags(O_RDONLY
| O_CLOEXEC
);
550 file
= anon_inode_getfile("gpio-linehandle",
553 O_RDONLY
| O_CLOEXEC
);
556 goto out_put_unused_fd
;
560 if (copy_to_user(ip
, &handlereq
, sizeof(handlereq
))) {
562 * fput() will trigger the release() callback, so do not go onto
563 * the regular error cleanup path here.
570 fd_install(fd
, file
);
572 dev_dbg(&gdev
->dev
, "registered chardev handle for %d lines\n",
581 gpiod_free(lh
->descs
[i
]);
585 put_device(&gdev
->dev
);
590 * GPIO line event management
594 * struct lineevent_state - contains the state of a userspace event
595 * @gdev: the GPIO device the event pertains to
596 * @label: consumer label used to tag descriptors
597 * @desc: the GPIO descriptor held by this event
598 * @eflags: the event flags this line was requested with
599 * @irq: the interrupt that trigger in response to events on this GPIO
600 * @wait: wait queue that handles blocking reads of events
601 * @events: KFIFO for the GPIO events
602 * @read_lock: mutex lock to protect reads from colliding with adding
603 * new events to the FIFO
604 * @timestamp: cache for the timestamp storing it between hardirq
605 * and IRQ thread, used to bring the timestamp close to the actual
608 struct lineevent_state
{
609 struct gpio_device
*gdev
;
611 struct gpio_desc
*desc
;
614 wait_queue_head_t wait
;
615 DECLARE_KFIFO(events
, struct gpioevent_data
, 16);
616 struct mutex read_lock
;
620 #define GPIOEVENT_REQUEST_VALID_FLAGS \
621 (GPIOEVENT_REQUEST_RISING_EDGE | \
622 GPIOEVENT_REQUEST_FALLING_EDGE)
624 static __poll_t
lineevent_poll(struct file
*filep
,
625 struct poll_table_struct
*wait
)
627 struct lineevent_state
*le
= filep
->private_data
;
630 poll_wait(filep
, &le
->wait
, wait
);
632 if (!kfifo_is_empty(&le
->events
))
633 events
= EPOLLIN
| EPOLLRDNORM
;
639 static ssize_t
lineevent_read(struct file
*filep
,
644 struct lineevent_state
*le
= filep
->private_data
;
648 if (count
< sizeof(struct gpioevent_data
))
652 if (kfifo_is_empty(&le
->events
)) {
653 if (filep
->f_flags
& O_NONBLOCK
)
656 ret
= wait_event_interruptible(le
->wait
,
657 !kfifo_is_empty(&le
->events
));
662 if (mutex_lock_interruptible(&le
->read_lock
))
664 ret
= kfifo_to_user(&le
->events
, buf
, count
, &copied
);
665 mutex_unlock(&le
->read_lock
);
671 * If we couldn't read anything from the fifo (a different
672 * thread might have been faster) we either return -EAGAIN if
673 * the file descriptor is non-blocking, otherwise we go back to
674 * sleep and wait for more data to arrive.
676 if (copied
== 0 && (filep
->f_flags
& O_NONBLOCK
))
679 } while (copied
== 0);
684 static int lineevent_release(struct inode
*inode
, struct file
*filep
)
686 struct lineevent_state
*le
= filep
->private_data
;
687 struct gpio_device
*gdev
= le
->gdev
;
689 free_irq(le
->irq
, le
);
690 gpiod_free(le
->desc
);
693 put_device(&gdev
->dev
);
697 static long lineevent_ioctl(struct file
*filep
, unsigned int cmd
,
700 struct lineevent_state
*le
= filep
->private_data
;
701 void __user
*ip
= (void __user
*)arg
;
702 struct gpiohandle_data ghd
;
705 * We can get the value for an event line but not set it,
706 * because it is input by definition.
708 if (cmd
== GPIOHANDLE_GET_LINE_VALUES_IOCTL
) {
711 memset(&ghd
, 0, sizeof(ghd
));
713 val
= gpiod_get_value_cansleep(le
->desc
);
718 if (copy_to_user(ip
, &ghd
, sizeof(ghd
)))
727 static long lineevent_ioctl_compat(struct file
*filep
, unsigned int cmd
,
730 return lineevent_ioctl(filep
, cmd
, (unsigned long)compat_ptr(arg
));
734 static const struct file_operations lineevent_fileops
= {
735 .release
= lineevent_release
,
736 .read
= lineevent_read
,
737 .poll
= lineevent_poll
,
738 .owner
= THIS_MODULE
,
739 .llseek
= noop_llseek
,
740 .unlocked_ioctl
= lineevent_ioctl
,
742 .compat_ioctl
= lineevent_ioctl_compat
,
746 static irqreturn_t
lineevent_irq_thread(int irq
, void *p
)
748 struct lineevent_state
*le
= p
;
749 struct gpioevent_data ge
;
752 /* Do not leak kernel stack to userspace */
753 memset(&ge
, 0, sizeof(ge
));
755 ge
.timestamp
= le
->timestamp
;
756 level
= gpiod_get_value_cansleep(le
->desc
);
758 if (le
->eflags
& GPIOEVENT_REQUEST_RISING_EDGE
759 && le
->eflags
& GPIOEVENT_REQUEST_FALLING_EDGE
) {
761 /* Emit low-to-high event */
762 ge
.id
= GPIOEVENT_EVENT_RISING_EDGE
;
764 /* Emit high-to-low event */
765 ge
.id
= GPIOEVENT_EVENT_FALLING_EDGE
;
766 } else if (le
->eflags
& GPIOEVENT_REQUEST_RISING_EDGE
&& level
) {
767 /* Emit low-to-high event */
768 ge
.id
= GPIOEVENT_EVENT_RISING_EDGE
;
769 } else if (le
->eflags
& GPIOEVENT_REQUEST_FALLING_EDGE
&& !level
) {
770 /* Emit high-to-low event */
771 ge
.id
= GPIOEVENT_EVENT_FALLING_EDGE
;
776 ret
= kfifo_put(&le
->events
, ge
);
778 wake_up_poll(&le
->wait
, EPOLLIN
);
783 static irqreturn_t
lineevent_irq_handler(int irq
, void *p
)
785 struct lineevent_state
*le
= p
;
788 * Just store the timestamp in hardirq context so we get it as
789 * close in time as possible to the actual event.
791 le
->timestamp
= ktime_get_real_ns();
793 return IRQ_WAKE_THREAD
;
796 static int lineevent_create(struct gpio_device
*gdev
, void __user
*ip
)
798 struct gpioevent_request eventreq
;
799 struct lineevent_state
*le
;
800 struct gpio_desc
*desc
;
809 if (copy_from_user(&eventreq
, ip
, sizeof(eventreq
)))
812 le
= kzalloc(sizeof(*le
), GFP_KERNEL
);
816 get_device(&gdev
->dev
);
818 /* Make sure this is terminated */
819 eventreq
.consumer_label
[sizeof(eventreq
.consumer_label
)-1] = '\0';
820 if (strlen(eventreq
.consumer_label
)) {
821 le
->label
= kstrdup(eventreq
.consumer_label
,
829 offset
= eventreq
.lineoffset
;
830 lflags
= eventreq
.handleflags
;
831 eflags
= eventreq
.eventflags
;
833 if (offset
>= gdev
->ngpio
) {
838 /* Return an error if a unknown flag is set */
839 if ((lflags
& ~GPIOHANDLE_REQUEST_VALID_FLAGS
) ||
840 (eflags
& ~GPIOEVENT_REQUEST_VALID_FLAGS
)) {
845 /* This is just wrong: we don't look for events on output lines */
846 if (lflags
& GPIOHANDLE_REQUEST_OUTPUT
) {
851 desc
= &gdev
->descs
[offset
];
852 ret
= gpiod_request(desc
, le
->label
);
858 if (lflags
& GPIOHANDLE_REQUEST_ACTIVE_LOW
)
859 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
860 if (lflags
& GPIOHANDLE_REQUEST_OPEN_DRAIN
)
861 set_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
862 if (lflags
& GPIOHANDLE_REQUEST_OPEN_SOURCE
)
863 set_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
865 ret
= gpiod_direction_input(desc
);
869 le
->irq
= gpiod_to_irq(desc
);
875 if (eflags
& GPIOEVENT_REQUEST_RISING_EDGE
)
876 irqflags
|= IRQF_TRIGGER_RISING
;
877 if (eflags
& GPIOEVENT_REQUEST_FALLING_EDGE
)
878 irqflags
|= IRQF_TRIGGER_FALLING
;
879 irqflags
|= IRQF_ONESHOT
;
880 irqflags
|= IRQF_SHARED
;
882 INIT_KFIFO(le
->events
);
883 init_waitqueue_head(&le
->wait
);
884 mutex_init(&le
->read_lock
);
886 /* Request a thread to read the events */
887 ret
= request_threaded_irq(le
->irq
,
888 lineevent_irq_handler
,
889 lineevent_irq_thread
,
896 fd
= get_unused_fd_flags(O_RDONLY
| O_CLOEXEC
);
902 file
= anon_inode_getfile("gpio-event",
905 O_RDONLY
| O_CLOEXEC
);
908 goto out_put_unused_fd
;
912 if (copy_to_user(ip
, &eventreq
, sizeof(eventreq
))) {
914 * fput() will trigger the release() callback, so do not go onto
915 * the regular error cleanup path here.
922 fd_install(fd
, file
);
929 free_irq(le
->irq
, le
);
931 gpiod_free(le
->desc
);
936 put_device(&gdev
->dev
);
941 * gpio_ioctl() - ioctl handler for the GPIO chardev
943 static long gpio_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
945 struct gpio_device
*gdev
= filp
->private_data
;
946 struct gpio_chip
*chip
= gdev
->chip
;
947 void __user
*ip
= (void __user
*)arg
;
949 /* We fail any subsequent ioctl():s when the chip is gone */
953 /* Fill in the struct and pass to userspace */
954 if (cmd
== GPIO_GET_CHIPINFO_IOCTL
) {
955 struct gpiochip_info chipinfo
;
957 memset(&chipinfo
, 0, sizeof(chipinfo
));
959 strncpy(chipinfo
.name
, dev_name(&gdev
->dev
),
960 sizeof(chipinfo
.name
));
961 chipinfo
.name
[sizeof(chipinfo
.name
)-1] = '\0';
962 strncpy(chipinfo
.label
, gdev
->label
,
963 sizeof(chipinfo
.label
));
964 chipinfo
.label
[sizeof(chipinfo
.label
)-1] = '\0';
965 chipinfo
.lines
= gdev
->ngpio
;
966 if (copy_to_user(ip
, &chipinfo
, sizeof(chipinfo
)))
969 } else if (cmd
== GPIO_GET_LINEINFO_IOCTL
) {
970 struct gpioline_info lineinfo
;
971 struct gpio_desc
*desc
;
973 if (copy_from_user(&lineinfo
, ip
, sizeof(lineinfo
)))
975 if (lineinfo
.line_offset
>= gdev
->ngpio
)
978 desc
= &gdev
->descs
[lineinfo
.line_offset
];
980 strncpy(lineinfo
.name
, desc
->name
,
981 sizeof(lineinfo
.name
));
982 lineinfo
.name
[sizeof(lineinfo
.name
)-1] = '\0';
984 lineinfo
.name
[0] = '\0';
987 strncpy(lineinfo
.consumer
, desc
->label
,
988 sizeof(lineinfo
.consumer
));
989 lineinfo
.consumer
[sizeof(lineinfo
.consumer
)-1] = '\0';
991 lineinfo
.consumer
[0] = '\0';
995 * Userspace only need to know that the kernel is using
996 * this GPIO so it can't use it.
999 if (test_bit(FLAG_REQUESTED
, &desc
->flags
) ||
1000 test_bit(FLAG_IS_HOGGED
, &desc
->flags
) ||
1001 test_bit(FLAG_USED_AS_IRQ
, &desc
->flags
) ||
1002 test_bit(FLAG_EXPORT
, &desc
->flags
) ||
1003 test_bit(FLAG_SYSFS
, &desc
->flags
))
1004 lineinfo
.flags
|= GPIOLINE_FLAG_KERNEL
;
1005 if (test_bit(FLAG_IS_OUT
, &desc
->flags
))
1006 lineinfo
.flags
|= GPIOLINE_FLAG_IS_OUT
;
1007 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1008 lineinfo
.flags
|= GPIOLINE_FLAG_ACTIVE_LOW
;
1009 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
))
1010 lineinfo
.flags
|= GPIOLINE_FLAG_OPEN_DRAIN
;
1011 if (test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
))
1012 lineinfo
.flags
|= GPIOLINE_FLAG_OPEN_SOURCE
;
1014 if (copy_to_user(ip
, &lineinfo
, sizeof(lineinfo
)))
1017 } else if (cmd
== GPIO_GET_LINEHANDLE_IOCTL
) {
1018 return linehandle_create(gdev
, ip
);
1019 } else if (cmd
== GPIO_GET_LINEEVENT_IOCTL
) {
1020 return lineevent_create(gdev
, ip
);
1025 #ifdef CONFIG_COMPAT
1026 static long gpio_ioctl_compat(struct file
*filp
, unsigned int cmd
,
1029 return gpio_ioctl(filp
, cmd
, (unsigned long)compat_ptr(arg
));
1034 * gpio_chrdev_open() - open the chardev for ioctl operations
1035 * @inode: inode for this chardev
1036 * @filp: file struct for storing private data
1037 * Returns 0 on success
1039 static int gpio_chrdev_open(struct inode
*inode
, struct file
*filp
)
1041 struct gpio_device
*gdev
= container_of(inode
->i_cdev
,
1042 struct gpio_device
, chrdev
);
1044 /* Fail on open if the backing gpiochip is gone */
1047 get_device(&gdev
->dev
);
1048 filp
->private_data
= gdev
;
1050 return nonseekable_open(inode
, filp
);
1054 * gpio_chrdev_release() - close chardev after ioctl operations
1055 * @inode: inode for this chardev
1056 * @filp: file struct for storing private data
1057 * Returns 0 on success
1059 static int gpio_chrdev_release(struct inode
*inode
, struct file
*filp
)
1061 struct gpio_device
*gdev
= container_of(inode
->i_cdev
,
1062 struct gpio_device
, chrdev
);
1064 put_device(&gdev
->dev
);
1069 static const struct file_operations gpio_fileops
= {
1070 .release
= gpio_chrdev_release
,
1071 .open
= gpio_chrdev_open
,
1072 .owner
= THIS_MODULE
,
1073 .llseek
= no_llseek
,
1074 .unlocked_ioctl
= gpio_ioctl
,
1075 #ifdef CONFIG_COMPAT
1076 .compat_ioctl
= gpio_ioctl_compat
,
1080 static void gpiodevice_release(struct device
*dev
)
1082 struct gpio_device
*gdev
= dev_get_drvdata(dev
);
1084 list_del(&gdev
->list
);
1085 ida_simple_remove(&gpio_ida
, gdev
->id
);
1086 kfree_const(gdev
->label
);
1091 static int gpiochip_setup_dev(struct gpio_device
*gdev
)
1095 cdev_init(&gdev
->chrdev
, &gpio_fileops
);
1096 gdev
->chrdev
.owner
= THIS_MODULE
;
1097 gdev
->dev
.devt
= MKDEV(MAJOR(gpio_devt
), gdev
->id
);
1099 status
= cdev_device_add(&gdev
->chrdev
, &gdev
->dev
);
1103 chip_dbg(gdev
->chip
, "added GPIO chardev (%d:%d)\n",
1104 MAJOR(gpio_devt
), gdev
->id
);
1106 status
= gpiochip_sysfs_register(gdev
);
1108 goto err_remove_device
;
1110 /* From this point, the .release() function cleans up gpio_device */
1111 gdev
->dev
.release
= gpiodevice_release
;
1112 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1113 __func__
, gdev
->base
, gdev
->base
+ gdev
->ngpio
- 1,
1114 dev_name(&gdev
->dev
), gdev
->chip
->label
? : "generic");
1119 cdev_device_del(&gdev
->chrdev
, &gdev
->dev
);
1123 static void gpiochip_setup_devs(void)
1125 struct gpio_device
*gdev
;
1128 list_for_each_entry(gdev
, &gpio_devices
, list
) {
1129 err
= gpiochip_setup_dev(gdev
);
1131 pr_err("%s: Failed to initialize gpio device (%d)\n",
1132 dev_name(&gdev
->dev
), err
);
1136 int gpiochip_add_data_with_key(struct gpio_chip
*chip
, void *data
,
1137 struct lock_class_key
*lock_key
,
1138 struct lock_class_key
*request_key
)
1140 unsigned long flags
;
1143 int base
= chip
->base
;
1144 struct gpio_device
*gdev
;
1147 * First: allocate and populate the internal stat container, and
1148 * set up the struct device.
1150 gdev
= kzalloc(sizeof(*gdev
), GFP_KERNEL
);
1153 gdev
->dev
.bus
= &gpio_bus_type
;
1155 chip
->gpiodev
= gdev
;
1157 gdev
->dev
.parent
= chip
->parent
;
1158 gdev
->dev
.of_node
= chip
->parent
->of_node
;
1161 #ifdef CONFIG_OF_GPIO
1162 /* If the gpiochip has an assigned OF node this takes precedence */
1164 gdev
->dev
.of_node
= chip
->of_node
;
1167 gdev
->id
= ida_simple_get(&gpio_ida
, 0, 0, GFP_KERNEL
);
1172 dev_set_name(&gdev
->dev
, "gpiochip%d", gdev
->id
);
1173 device_initialize(&gdev
->dev
);
1174 dev_set_drvdata(&gdev
->dev
, gdev
);
1175 if (chip
->parent
&& chip
->parent
->driver
)
1176 gdev
->owner
= chip
->parent
->driver
->owner
;
1177 else if (chip
->owner
)
1178 /* TODO: remove chip->owner */
1179 gdev
->owner
= chip
->owner
;
1181 gdev
->owner
= THIS_MODULE
;
1183 gdev
->descs
= kcalloc(chip
->ngpio
, sizeof(gdev
->descs
[0]), GFP_KERNEL
);
1189 if (chip
->ngpio
== 0) {
1190 chip_err(chip
, "tried to insert a GPIO chip with zero lines\n");
1192 goto err_free_descs
;
1195 gdev
->label
= kstrdup_const(chip
->label
?: "unknown", GFP_KERNEL
);
1198 goto err_free_descs
;
1201 gdev
->ngpio
= chip
->ngpio
;
1204 spin_lock_irqsave(&gpio_lock
, flags
);
1207 * TODO: this allocates a Linux GPIO number base in the global
1208 * GPIO numberspace for this chip. In the long run we want to
1209 * get *rid* of this numberspace and use only descriptors, but
1210 * it may be a pipe dream. It will not happen before we get rid
1211 * of the sysfs interface anyways.
1214 base
= gpiochip_find_base(chip
->ngpio
);
1217 spin_unlock_irqrestore(&gpio_lock
, flags
);
1218 goto err_free_label
;
1221 * TODO: it should not be necessary to reflect the assigned
1222 * base outside of the GPIO subsystem. Go over drivers and
1223 * see if anyone makes use of this, else drop this and assign
1230 status
= gpiodev_add_to_list(gdev
);
1232 spin_unlock_irqrestore(&gpio_lock
, flags
);
1233 goto err_free_label
;
1236 spin_unlock_irqrestore(&gpio_lock
, flags
);
1238 for (i
= 0; i
< chip
->ngpio
; i
++) {
1239 struct gpio_desc
*desc
= &gdev
->descs
[i
];
1243 /* REVISIT: most hardware initializes GPIOs as inputs (often
1244 * with pullups enabled) so power usage is minimized. Linux
1245 * code should set the gpio direction first thing; but until
1246 * it does, and in case chip->get_direction is not set, we may
1247 * expose the wrong direction in sysfs.
1249 desc
->flags
= !chip
->direction_input
? (1 << FLAG_IS_OUT
) : 0;
1252 #ifdef CONFIG_PINCTRL
1253 INIT_LIST_HEAD(&gdev
->pin_ranges
);
1256 status
= gpiochip_set_desc_names(chip
);
1258 goto err_remove_from_list
;
1260 status
= gpiochip_irqchip_init_valid_mask(chip
);
1262 goto err_remove_from_list
;
1264 status
= gpiochip_add_irqchip(chip
, lock_key
, request_key
);
1266 goto err_remove_chip
;
1268 status
= of_gpiochip_add(chip
);
1270 goto err_remove_chip
;
1272 acpi_gpiochip_add(chip
);
1275 * By first adding the chardev, and then adding the device,
1276 * we get a device node entry in sysfs under
1277 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1278 * coldplug of device nodes and other udev business.
1279 * We can do this only if gpiolib has been initialized.
1280 * Otherwise, defer until later.
1282 if (gpiolib_initialized
) {
1283 status
= gpiochip_setup_dev(gdev
);
1285 goto err_remove_chip
;
1290 acpi_gpiochip_remove(chip
);
1291 gpiochip_free_hogs(chip
);
1292 of_gpiochip_remove(chip
);
1293 gpiochip_irqchip_free_valid_mask(chip
);
1294 err_remove_from_list
:
1295 spin_lock_irqsave(&gpio_lock
, flags
);
1296 list_del(&gdev
->list
);
1297 spin_unlock_irqrestore(&gpio_lock
, flags
);
1299 kfree_const(gdev
->label
);
1303 ida_simple_remove(&gpio_ida
, gdev
->id
);
1304 /* failures here can mean systems won't boot... */
1305 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__
,
1306 gdev
->base
, gdev
->base
+ gdev
->ngpio
- 1,
1307 chip
->label
? : "generic");
1311 EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key
);
1314 * gpiochip_get_data() - get per-subdriver data for the chip
1318 * The per-subdriver data for the chip.
1320 void *gpiochip_get_data(struct gpio_chip
*chip
)
1322 return chip
->gpiodev
->data
;
1324 EXPORT_SYMBOL_GPL(gpiochip_get_data
);
1327 * gpiochip_remove() - unregister a gpio_chip
1328 * @chip: the chip to unregister
1330 * A gpio_chip with any GPIOs still requested may not be removed.
1332 void gpiochip_remove(struct gpio_chip
*chip
)
1334 struct gpio_device
*gdev
= chip
->gpiodev
;
1335 struct gpio_desc
*desc
;
1336 unsigned long flags
;
1338 bool requested
= false;
1340 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
1341 gpiochip_sysfs_unregister(gdev
);
1342 gpiochip_free_hogs(chip
);
1343 /* Numb the device, cancelling all outstanding operations */
1345 gpiochip_irqchip_remove(chip
);
1346 acpi_gpiochip_remove(chip
);
1347 gpiochip_remove_pin_ranges(chip
);
1348 of_gpiochip_remove(chip
);
1350 * We accept no more calls into the driver from this point, so
1351 * NULL the driver data pointer
1355 spin_lock_irqsave(&gpio_lock
, flags
);
1356 for (i
= 0; i
< gdev
->ngpio
; i
++) {
1357 desc
= &gdev
->descs
[i
];
1358 if (test_bit(FLAG_REQUESTED
, &desc
->flags
))
1361 spin_unlock_irqrestore(&gpio_lock
, flags
);
1364 dev_crit(&gdev
->dev
,
1365 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
1368 * The gpiochip side puts its use of the device to rest here:
1369 * if there are no userspace clients, the chardev and device will
1370 * be removed, else it will be dangling until the last user is
1373 cdev_device_del(&gdev
->chrdev
, &gdev
->dev
);
1374 put_device(&gdev
->dev
);
1376 EXPORT_SYMBOL_GPL(gpiochip_remove
);
1378 static void devm_gpio_chip_release(struct device
*dev
, void *res
)
1380 struct gpio_chip
*chip
= *(struct gpio_chip
**)res
;
1382 gpiochip_remove(chip
);
1385 static int devm_gpio_chip_match(struct device
*dev
, void *res
, void *data
)
1388 struct gpio_chip
**r
= res
;
1399 * devm_gpiochip_add_data() - Resource manager gpiochip_add_data()
1400 * @dev: the device pointer on which irq_chip belongs to.
1401 * @chip: the chip to register, with chip->base initialized
1402 * @data: driver-private data associated with this chip
1404 * Context: potentially before irqs will work
1406 * The gpio chip automatically be released when the device is unbound.
1409 * A negative errno if the chip can't be registered, such as because the
1410 * chip->base is invalid or already associated with a different chip.
1411 * Otherwise it returns zero as a success code.
1413 int devm_gpiochip_add_data(struct device
*dev
, struct gpio_chip
*chip
,
1416 struct gpio_chip
**ptr
;
1419 ptr
= devres_alloc(devm_gpio_chip_release
, sizeof(*ptr
),
1424 ret
= gpiochip_add_data(chip
, data
);
1431 devres_add(dev
, ptr
);
1435 EXPORT_SYMBOL_GPL(devm_gpiochip_add_data
);
1438 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1439 * @dev: device for which which resource was allocated
1440 * @chip: the chip to remove
1442 * A gpio_chip with any GPIOs still requested may not be removed.
1444 void devm_gpiochip_remove(struct device
*dev
, struct gpio_chip
*chip
)
1448 ret
= devres_release(dev
, devm_gpio_chip_release
,
1449 devm_gpio_chip_match
, chip
);
1452 EXPORT_SYMBOL_GPL(devm_gpiochip_remove
);
1455 * gpiochip_find() - iterator for locating a specific gpio_chip
1456 * @data: data to pass to match function
1457 * @match: Callback function to check gpio_chip
1459 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1460 * determined by a user supplied @match callback. The callback should return
1461 * 0 if the device doesn't match and non-zero if it does. If the callback is
1462 * non-zero, this function will return to the caller and not iterate over any
1465 struct gpio_chip
*gpiochip_find(void *data
,
1466 int (*match
)(struct gpio_chip
*chip
,
1469 struct gpio_device
*gdev
;
1470 struct gpio_chip
*chip
= NULL
;
1471 unsigned long flags
;
1473 spin_lock_irqsave(&gpio_lock
, flags
);
1474 list_for_each_entry(gdev
, &gpio_devices
, list
)
1475 if (gdev
->chip
&& match(gdev
->chip
, data
)) {
1480 spin_unlock_irqrestore(&gpio_lock
, flags
);
1484 EXPORT_SYMBOL_GPL(gpiochip_find
);
1486 static int gpiochip_match_name(struct gpio_chip
*chip
, void *data
)
1488 const char *name
= data
;
1490 return !strcmp(chip
->label
, name
);
1493 static struct gpio_chip
*find_chip_by_name(const char *name
)
1495 return gpiochip_find((void *)name
, gpiochip_match_name
);
1498 #ifdef CONFIG_GPIOLIB_IRQCHIP
1501 * The following is irqchip helper code for gpiochips.
1504 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip
*gpiochip
)
1506 if (!gpiochip
->irq
.need_valid_mask
)
1509 gpiochip
->irq
.valid_mask
= kcalloc(BITS_TO_LONGS(gpiochip
->ngpio
),
1510 sizeof(long), GFP_KERNEL
);
1511 if (!gpiochip
->irq
.valid_mask
)
1514 /* Assume by default all GPIOs are valid */
1515 bitmap_fill(gpiochip
->irq
.valid_mask
, gpiochip
->ngpio
);
1520 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip
*gpiochip
)
1522 kfree(gpiochip
->irq
.valid_mask
);
1523 gpiochip
->irq
.valid_mask
= NULL
;
1526 bool gpiochip_irqchip_irq_valid(const struct gpio_chip
*gpiochip
,
1527 unsigned int offset
)
1529 /* No mask means all valid */
1530 if (likely(!gpiochip
->irq
.valid_mask
))
1532 return test_bit(offset
, gpiochip
->irq
.valid_mask
);
1534 EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid
);
1537 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
1538 * @gpiochip: the gpiochip to set the irqchip chain to
1539 * @irqchip: the irqchip to chain to the gpiochip
1540 * @parent_irq: the irq number corresponding to the parent IRQ for this
1542 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1543 * coming out of the gpiochip. If the interrupt is nested rather than
1544 * cascaded, pass NULL in this handler argument
1546 static void gpiochip_set_cascaded_irqchip(struct gpio_chip
*gpiochip
,
1547 struct irq_chip
*irqchip
,
1548 unsigned int parent_irq
,
1549 irq_flow_handler_t parent_handler
)
1551 unsigned int offset
;
1553 if (!gpiochip
->irq
.domain
) {
1554 chip_err(gpiochip
, "called %s before setting up irqchip\n",
1559 if (parent_handler
) {
1560 if (gpiochip
->can_sleep
) {
1562 "you cannot have chained interrupts on a "
1563 "chip that may sleep\n");
1567 * The parent irqchip is already using the chip_data for this
1568 * irqchip, so our callbacks simply use the handler_data.
1570 irq_set_chained_handler_and_data(parent_irq
, parent_handler
,
1573 gpiochip
->irq
.parents
= &parent_irq
;
1574 gpiochip
->irq
.num_parents
= 1;
1577 /* Set the parent IRQ for all affected IRQs */
1578 for (offset
= 0; offset
< gpiochip
->ngpio
; offset
++) {
1579 if (!gpiochip_irqchip_irq_valid(gpiochip
, offset
))
1581 irq_set_parent(irq_find_mapping(gpiochip
->irq
.domain
, offset
),
1587 * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
1588 * @gpiochip: the gpiochip to set the irqchip chain to
1589 * @irqchip: the irqchip to chain to the gpiochip
1590 * @parent_irq: the irq number corresponding to the parent IRQ for this
1592 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1593 * coming out of the gpiochip. If the interrupt is nested rather than
1594 * cascaded, pass NULL in this handler argument
1596 void gpiochip_set_chained_irqchip(struct gpio_chip
*gpiochip
,
1597 struct irq_chip
*irqchip
,
1598 unsigned int parent_irq
,
1599 irq_flow_handler_t parent_handler
)
1601 if (gpiochip
->irq
.threaded
) {
1602 chip_err(gpiochip
, "tried to chain a threaded gpiochip\n");
1606 gpiochip_set_cascaded_irqchip(gpiochip
, irqchip
, parent_irq
,
1609 EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip
);
1612 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1613 * @gpiochip: the gpiochip to set the irqchip nested handler to
1614 * @irqchip: the irqchip to nest to the gpiochip
1615 * @parent_irq: the irq number corresponding to the parent IRQ for this
1618 void gpiochip_set_nested_irqchip(struct gpio_chip
*gpiochip
,
1619 struct irq_chip
*irqchip
,
1620 unsigned int parent_irq
)
1622 gpiochip_set_cascaded_irqchip(gpiochip
, irqchip
, parent_irq
,
1625 EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip
);
1628 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1629 * @d: the irqdomain used by this irqchip
1630 * @irq: the global irq number used by this GPIO irqchip irq
1631 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1633 * This function will set up the mapping for a certain IRQ line on a
1634 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1635 * stored inside the gpiochip.
1637 int gpiochip_irq_map(struct irq_domain
*d
, unsigned int irq
,
1638 irq_hw_number_t hwirq
)
1640 struct gpio_chip
*chip
= d
->host_data
;
1643 if (!gpiochip_irqchip_irq_valid(chip
, hwirq
))
1646 irq_set_chip_data(irq
, chip
);
1648 * This lock class tells lockdep that GPIO irqs are in a different
1649 * category than their parents, so it won't report false recursion.
1651 irq_set_lockdep_class(irq
, chip
->irq
.lock_key
, chip
->irq
.request_key
);
1652 irq_set_chip_and_handler(irq
, chip
->irq
.chip
, chip
->irq
.handler
);
1653 /* Chips that use nested thread handlers have them marked */
1654 if (chip
->irq
.threaded
)
1655 irq_set_nested_thread(irq
, 1);
1656 irq_set_noprobe(irq
);
1658 if (chip
->irq
.num_parents
== 1)
1659 err
= irq_set_parent(irq
, chip
->irq
.parents
[0]);
1660 else if (chip
->irq
.map
)
1661 err
= irq_set_parent(irq
, chip
->irq
.map
[hwirq
]);
1667 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1668 * is passed as default type.
1670 if (chip
->irq
.default_type
!= IRQ_TYPE_NONE
)
1671 irq_set_irq_type(irq
, chip
->irq
.default_type
);
1675 EXPORT_SYMBOL_GPL(gpiochip_irq_map
);
1677 void gpiochip_irq_unmap(struct irq_domain
*d
, unsigned int irq
)
1679 struct gpio_chip
*chip
= d
->host_data
;
1681 if (chip
->irq
.threaded
)
1682 irq_set_nested_thread(irq
, 0);
1683 irq_set_chip_and_handler(irq
, NULL
, NULL
);
1684 irq_set_chip_data(irq
, NULL
);
1686 EXPORT_SYMBOL_GPL(gpiochip_irq_unmap
);
1688 static const struct irq_domain_ops gpiochip_domain_ops
= {
1689 .map
= gpiochip_irq_map
,
1690 .unmap
= gpiochip_irq_unmap
,
1691 /* Virtually all GPIO irqchips are twocell:ed */
1692 .xlate
= irq_domain_xlate_twocell
,
1695 static int gpiochip_irq_reqres(struct irq_data
*d
)
1697 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(d
);
1699 if (!try_module_get(chip
->gpiodev
->owner
))
1702 if (gpiochip_lock_as_irq(chip
, d
->hwirq
)) {
1704 "unable to lock HW IRQ %lu for IRQ\n",
1706 module_put(chip
->gpiodev
->owner
);
1712 static void gpiochip_irq_relres(struct irq_data
*d
)
1714 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(d
);
1716 gpiochip_unlock_as_irq(chip
, d
->hwirq
);
1717 module_put(chip
->gpiodev
->owner
);
1720 static int gpiochip_to_irq(struct gpio_chip
*chip
, unsigned offset
)
1722 if (!gpiochip_irqchip_irq_valid(chip
, offset
))
1725 return irq_create_mapping(chip
->irq
.domain
, offset
);
1729 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1730 * @gpiochip: the GPIO chip to add the IRQ chip to
1731 * @lock_key: lockdep class for IRQ lock
1732 * @request_key: lockdep class for IRQ request
1734 static int gpiochip_add_irqchip(struct gpio_chip
*gpiochip
,
1735 struct lock_class_key
*lock_key
,
1736 struct lock_class_key
*request_key
)
1738 struct irq_chip
*irqchip
= gpiochip
->irq
.chip
;
1739 const struct irq_domain_ops
*ops
;
1740 struct device_node
*np
;
1747 if (gpiochip
->irq
.parent_handler
&& gpiochip
->can_sleep
) {
1748 chip_err(gpiochip
, "you cannot have chained interrupts on a "
1749 "chip that may sleep\n");
1753 np
= gpiochip
->gpiodev
->dev
.of_node
;
1754 type
= gpiochip
->irq
.default_type
;
1757 * Specifying a default trigger is a terrible idea if DT or ACPI is
1758 * used to configure the interrupts, as you may end up with
1759 * conflicting triggers. Tell the user, and reset to NONE.
1761 if (WARN(np
&& type
!= IRQ_TYPE_NONE
,
1762 "%s: Ignoring %u default trigger\n", np
->full_name
, type
))
1763 type
= IRQ_TYPE_NONE
;
1765 if (has_acpi_companion(gpiochip
->parent
) && type
!= IRQ_TYPE_NONE
) {
1766 acpi_handle_warn(ACPI_HANDLE(gpiochip
->parent
),
1767 "Ignoring %u default trigger\n", type
);
1768 type
= IRQ_TYPE_NONE
;
1771 gpiochip
->to_irq
= gpiochip_to_irq
;
1772 gpiochip
->irq
.default_type
= type
;
1773 gpiochip
->irq
.lock_key
= lock_key
;
1774 gpiochip
->irq
.request_key
= request_key
;
1776 if (gpiochip
->irq
.domain_ops
)
1777 ops
= gpiochip
->irq
.domain_ops
;
1779 ops
= &gpiochip_domain_ops
;
1781 gpiochip
->irq
.domain
= irq_domain_add_simple(np
, gpiochip
->ngpio
,
1782 gpiochip
->irq
.first
,
1784 if (!gpiochip
->irq
.domain
)
1788 * It is possible for a driver to override this, but only if the
1789 * alternative functions are both implemented.
1791 if (!irqchip
->irq_request_resources
&&
1792 !irqchip
->irq_release_resources
) {
1793 irqchip
->irq_request_resources
= gpiochip_irq_reqres
;
1794 irqchip
->irq_release_resources
= gpiochip_irq_relres
;
1797 if (gpiochip
->irq
.parent_handler
) {
1798 void *data
= gpiochip
->irq
.parent_handler_data
?: gpiochip
;
1800 for (i
= 0; i
< gpiochip
->irq
.num_parents
; i
++) {
1802 * The parent IRQ chip is already using the chip_data
1803 * for this IRQ chip, so our callbacks simply use the
1806 irq_set_chained_handler_and_data(gpiochip
->irq
.parents
[i
],
1807 gpiochip
->irq
.parent_handler
,
1812 acpi_gpiochip_request_interrupts(gpiochip
);
1818 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1819 * @gpiochip: the gpiochip to remove the irqchip from
1821 * This is called only from gpiochip_remove()
1823 static void gpiochip_irqchip_remove(struct gpio_chip
*gpiochip
)
1825 unsigned int offset
;
1827 acpi_gpiochip_free_interrupts(gpiochip
);
1829 if (gpiochip
->irq
.chip
&& gpiochip
->irq
.parent_handler
) {
1830 struct gpio_irq_chip
*irq
= &gpiochip
->irq
;
1833 for (i
= 0; i
< irq
->num_parents
; i
++)
1834 irq_set_chained_handler_and_data(irq
->parents
[i
],
1838 /* Remove all IRQ mappings and delete the domain */
1839 if (gpiochip
->irq
.domain
) {
1842 for (offset
= 0; offset
< gpiochip
->ngpio
; offset
++) {
1843 if (!gpiochip_irqchip_irq_valid(gpiochip
, offset
))
1846 irq
= irq_find_mapping(gpiochip
->irq
.domain
, offset
);
1847 irq_dispose_mapping(irq
);
1850 irq_domain_remove(gpiochip
->irq
.domain
);
1853 if (gpiochip
->irq
.chip
) {
1854 gpiochip
->irq
.chip
->irq_request_resources
= NULL
;
1855 gpiochip
->irq
.chip
->irq_release_resources
= NULL
;
1856 gpiochip
->irq
.chip
= NULL
;
1859 gpiochip_irqchip_free_valid_mask(gpiochip
);
1863 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
1864 * @gpiochip: the gpiochip to add the irqchip to
1865 * @irqchip: the irqchip to add to the gpiochip
1866 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1867 * allocate gpiochip irqs from
1868 * @handler: the irq handler to use (often a predefined irq core function)
1869 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1870 * to have the core avoid setting up any default type in the hardware.
1871 * @threaded: whether this irqchip uses a nested thread handler
1872 * @lock_key: lockdep class for IRQ lock
1873 * @request_key: lockdep class for IRQ request
1875 * This function closely associates a certain irqchip with a certain
1876 * gpiochip, providing an irq domain to translate the local IRQs to
1877 * global irqs in the gpiolib core, and making sure that the gpiochip
1878 * is passed as chip data to all related functions. Driver callbacks
1879 * need to use gpiochip_get_data() to get their local state containers back
1880 * from the gpiochip passed as chip data. An irqdomain will be stored
1881 * in the gpiochip that shall be used by the driver to handle IRQ number
1882 * translation. The gpiochip will need to be initialized and registered
1883 * before calling this function.
1885 * This function will handle two cell:ed simple IRQs and assumes all
1886 * the pins on the gpiochip can generate a unique IRQ. Everything else
1887 * need to be open coded.
1889 int gpiochip_irqchip_add_key(struct gpio_chip
*gpiochip
,
1890 struct irq_chip
*irqchip
,
1891 unsigned int first_irq
,
1892 irq_flow_handler_t handler
,
1895 struct lock_class_key
*lock_key
,
1896 struct lock_class_key
*request_key
)
1898 struct device_node
*of_node
;
1900 if (!gpiochip
|| !irqchip
)
1903 if (!gpiochip
->parent
) {
1904 pr_err("missing gpiochip .dev parent pointer\n");
1907 gpiochip
->irq
.threaded
= threaded
;
1908 of_node
= gpiochip
->parent
->of_node
;
1909 #ifdef CONFIG_OF_GPIO
1911 * If the gpiochip has an assigned OF node this takes precedence
1912 * FIXME: get rid of this and use gpiochip->parent->of_node
1915 if (gpiochip
->of_node
)
1916 of_node
= gpiochip
->of_node
;
1919 * Specifying a default trigger is a terrible idea if DT or ACPI is
1920 * used to configure the interrupts, as you may end-up with
1921 * conflicting triggers. Tell the user, and reset to NONE.
1923 if (WARN(of_node
&& type
!= IRQ_TYPE_NONE
,
1924 "%pOF: Ignoring %d default trigger\n", of_node
, type
))
1925 type
= IRQ_TYPE_NONE
;
1926 if (has_acpi_companion(gpiochip
->parent
) && type
!= IRQ_TYPE_NONE
) {
1927 acpi_handle_warn(ACPI_HANDLE(gpiochip
->parent
),
1928 "Ignoring %d default trigger\n", type
);
1929 type
= IRQ_TYPE_NONE
;
1932 gpiochip
->irq
.chip
= irqchip
;
1933 gpiochip
->irq
.handler
= handler
;
1934 gpiochip
->irq
.default_type
= type
;
1935 gpiochip
->to_irq
= gpiochip_to_irq
;
1936 gpiochip
->irq
.lock_key
= lock_key
;
1937 gpiochip
->irq
.request_key
= request_key
;
1938 gpiochip
->irq
.domain
= irq_domain_add_simple(of_node
,
1939 gpiochip
->ngpio
, first_irq
,
1940 &gpiochip_domain_ops
, gpiochip
);
1941 if (!gpiochip
->irq
.domain
) {
1942 gpiochip
->irq
.chip
= NULL
;
1947 * It is possible for a driver to override this, but only if the
1948 * alternative functions are both implemented.
1950 if (!irqchip
->irq_request_resources
&&
1951 !irqchip
->irq_release_resources
) {
1952 irqchip
->irq_request_resources
= gpiochip_irq_reqres
;
1953 irqchip
->irq_release_resources
= gpiochip_irq_relres
;
1956 acpi_gpiochip_request_interrupts(gpiochip
);
1960 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key
);
1962 #else /* CONFIG_GPIOLIB_IRQCHIP */
1964 static inline int gpiochip_add_irqchip(struct gpio_chip
*gpiochip
,
1965 struct lock_class_key
*lock_key
,
1966 struct lock_class_key
*request_key
)
1971 static void gpiochip_irqchip_remove(struct gpio_chip
*gpiochip
) {}
1972 static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip
*gpiochip
)
1976 static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip
*gpiochip
)
1979 #endif /* CONFIG_GPIOLIB_IRQCHIP */
1982 * gpiochip_generic_request() - request the gpio function for a pin
1983 * @chip: the gpiochip owning the GPIO
1984 * @offset: the offset of the GPIO to request for GPIO function
1986 int gpiochip_generic_request(struct gpio_chip
*chip
, unsigned offset
)
1988 return pinctrl_gpio_request(chip
->gpiodev
->base
+ offset
);
1990 EXPORT_SYMBOL_GPL(gpiochip_generic_request
);
1993 * gpiochip_generic_free() - free the gpio function from a pin
1994 * @chip: the gpiochip to request the gpio function for
1995 * @offset: the offset of the GPIO to free from GPIO function
1997 void gpiochip_generic_free(struct gpio_chip
*chip
, unsigned offset
)
1999 pinctrl_gpio_free(chip
->gpiodev
->base
+ offset
);
2001 EXPORT_SYMBOL_GPL(gpiochip_generic_free
);
2004 * gpiochip_generic_config() - apply configuration for a pin
2005 * @chip: the gpiochip owning the GPIO
2006 * @offset: the offset of the GPIO to apply the configuration
2007 * @config: the configuration to be applied
2009 int gpiochip_generic_config(struct gpio_chip
*chip
, unsigned offset
,
2010 unsigned long config
)
2012 return pinctrl_gpio_set_config(chip
->gpiodev
->base
+ offset
, config
);
2014 EXPORT_SYMBOL_GPL(gpiochip_generic_config
);
2016 #ifdef CONFIG_PINCTRL
2019 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2020 * @chip: the gpiochip to add the range for
2021 * @pctldev: the pin controller to map to
2022 * @gpio_offset: the start offset in the current gpio_chip number space
2023 * @pin_group: name of the pin group inside the pin controller
2025 int gpiochip_add_pingroup_range(struct gpio_chip
*chip
,
2026 struct pinctrl_dev
*pctldev
,
2027 unsigned int gpio_offset
, const char *pin_group
)
2029 struct gpio_pin_range
*pin_range
;
2030 struct gpio_device
*gdev
= chip
->gpiodev
;
2033 pin_range
= kzalloc(sizeof(*pin_range
), GFP_KERNEL
);
2035 chip_err(chip
, "failed to allocate pin ranges\n");
2039 /* Use local offset as range ID */
2040 pin_range
->range
.id
= gpio_offset
;
2041 pin_range
->range
.gc
= chip
;
2042 pin_range
->range
.name
= chip
->label
;
2043 pin_range
->range
.base
= gdev
->base
+ gpio_offset
;
2044 pin_range
->pctldev
= pctldev
;
2046 ret
= pinctrl_get_group_pins(pctldev
, pin_group
,
2047 &pin_range
->range
.pins
,
2048 &pin_range
->range
.npins
);
2054 pinctrl_add_gpio_range(pctldev
, &pin_range
->range
);
2056 chip_dbg(chip
, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2057 gpio_offset
, gpio_offset
+ pin_range
->range
.npins
- 1,
2058 pinctrl_dev_get_devname(pctldev
), pin_group
);
2060 list_add_tail(&pin_range
->node
, &gdev
->pin_ranges
);
2064 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range
);
2067 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2068 * @chip: the gpiochip to add the range for
2069 * @pinctl_name: the dev_name() of the pin controller to map to
2070 * @gpio_offset: the start offset in the current gpio_chip number space
2071 * @pin_offset: the start offset in the pin controller number space
2072 * @npins: the number of pins from the offset of each pin space (GPIO and
2073 * pin controller) to accumulate in this range
2076 * 0 on success, or a negative error-code on failure.
2078 int gpiochip_add_pin_range(struct gpio_chip
*chip
, const char *pinctl_name
,
2079 unsigned int gpio_offset
, unsigned int pin_offset
,
2082 struct gpio_pin_range
*pin_range
;
2083 struct gpio_device
*gdev
= chip
->gpiodev
;
2086 pin_range
= kzalloc(sizeof(*pin_range
), GFP_KERNEL
);
2088 chip_err(chip
, "failed to allocate pin ranges\n");
2092 /* Use local offset as range ID */
2093 pin_range
->range
.id
= gpio_offset
;
2094 pin_range
->range
.gc
= chip
;
2095 pin_range
->range
.name
= chip
->label
;
2096 pin_range
->range
.base
= gdev
->base
+ gpio_offset
;
2097 pin_range
->range
.pin_base
= pin_offset
;
2098 pin_range
->range
.npins
= npins
;
2099 pin_range
->pctldev
= pinctrl_find_and_add_gpio_range(pinctl_name
,
2101 if (IS_ERR(pin_range
->pctldev
)) {
2102 ret
= PTR_ERR(pin_range
->pctldev
);
2103 chip_err(chip
, "could not create pin range\n");
2107 chip_dbg(chip
, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2108 gpio_offset
, gpio_offset
+ npins
- 1,
2110 pin_offset
, pin_offset
+ npins
- 1);
2112 list_add_tail(&pin_range
->node
, &gdev
->pin_ranges
);
2116 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range
);
2119 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2120 * @chip: the chip to remove all the mappings for
2122 void gpiochip_remove_pin_ranges(struct gpio_chip
*chip
)
2124 struct gpio_pin_range
*pin_range
, *tmp
;
2125 struct gpio_device
*gdev
= chip
->gpiodev
;
2127 list_for_each_entry_safe(pin_range
, tmp
, &gdev
->pin_ranges
, node
) {
2128 list_del(&pin_range
->node
);
2129 pinctrl_remove_gpio_range(pin_range
->pctldev
,
2134 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges
);
2136 #endif /* CONFIG_PINCTRL */
2138 /* These "optional" allocation calls help prevent drivers from stomping
2139 * on each other, and help provide better diagnostics in debugfs.
2140 * They're called even less than the "set direction" calls.
2142 static int gpiod_request_commit(struct gpio_desc
*desc
, const char *label
)
2144 struct gpio_chip
*chip
= desc
->gdev
->chip
;
2146 unsigned long flags
;
2148 spin_lock_irqsave(&gpio_lock
, flags
);
2150 /* NOTE: gpio_request() can be called in early boot,
2151 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
2154 if (test_and_set_bit(FLAG_REQUESTED
, &desc
->flags
) == 0) {
2155 desc_set_label(desc
, label
? : "?");
2162 if (chip
->request
) {
2163 /* chip->request may sleep */
2164 spin_unlock_irqrestore(&gpio_lock
, flags
);
2165 status
= chip
->request(chip
, gpio_chip_hwgpio(desc
));
2166 spin_lock_irqsave(&gpio_lock
, flags
);
2169 desc_set_label(desc
, NULL
);
2170 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
2174 if (chip
->get_direction
) {
2175 /* chip->get_direction may sleep */
2176 spin_unlock_irqrestore(&gpio_lock
, flags
);
2177 gpiod_get_direction(desc
);
2178 spin_lock_irqsave(&gpio_lock
, flags
);
2181 spin_unlock_irqrestore(&gpio_lock
, flags
);
2186 * This descriptor validation needs to be inserted verbatim into each
2187 * function taking a descriptor, so we need to use a preprocessor
2188 * macro to avoid endless duplication. If the desc is NULL it is an
2189 * optional GPIO and calls should just bail out.
2191 static int validate_desc(const struct gpio_desc
*desc
, const char *func
)
2196 pr_warn("%s: invalid GPIO (errorpointer)\n", func
);
2197 return PTR_ERR(desc
);
2200 pr_warn("%s: invalid GPIO (no device)\n", func
);
2203 if (!desc
->gdev
->chip
) {
2204 dev_warn(&desc
->gdev
->dev
,
2205 "%s: backing chip is gone\n", func
);
2211 #define VALIDATE_DESC(desc) do { \
2212 int __valid = validate_desc(desc, __func__); \
2217 #define VALIDATE_DESC_VOID(desc) do { \
2218 int __valid = validate_desc(desc, __func__); \
2223 int gpiod_request(struct gpio_desc
*desc
, const char *label
)
2225 int status
= -EPROBE_DEFER
;
2226 struct gpio_device
*gdev
;
2228 VALIDATE_DESC(desc
);
2231 if (try_module_get(gdev
->owner
)) {
2232 status
= gpiod_request_commit(desc
, label
);
2234 module_put(gdev
->owner
);
2236 get_device(&gdev
->dev
);
2240 gpiod_dbg(desc
, "%s: status %d\n", __func__
, status
);
2245 static bool gpiod_free_commit(struct gpio_desc
*desc
)
2248 unsigned long flags
;
2249 struct gpio_chip
*chip
;
2253 gpiod_unexport(desc
);
2255 spin_lock_irqsave(&gpio_lock
, flags
);
2257 chip
= desc
->gdev
->chip
;
2258 if (chip
&& test_bit(FLAG_REQUESTED
, &desc
->flags
)) {
2260 spin_unlock_irqrestore(&gpio_lock
, flags
);
2261 might_sleep_if(chip
->can_sleep
);
2262 chip
->free(chip
, gpio_chip_hwgpio(desc
));
2263 spin_lock_irqsave(&gpio_lock
, flags
);
2265 desc_set_label(desc
, NULL
);
2266 clear_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
2267 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
2268 clear_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
2269 clear_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
2270 clear_bit(FLAG_IS_HOGGED
, &desc
->flags
);
2274 spin_unlock_irqrestore(&gpio_lock
, flags
);
2278 void gpiod_free(struct gpio_desc
*desc
)
2280 if (desc
&& desc
->gdev
&& gpiod_free_commit(desc
)) {
2281 module_put(desc
->gdev
->owner
);
2282 put_device(&desc
->gdev
->dev
);
2284 WARN_ON(extra_checks
);
2289 * gpiochip_is_requested - return string iff signal was requested
2290 * @chip: controller managing the signal
2291 * @offset: of signal within controller's 0..(ngpio - 1) range
2293 * Returns NULL if the GPIO is not currently requested, else a string.
2294 * The string returned is the label passed to gpio_request(); if none has been
2295 * passed it is a meaningless, non-NULL constant.
2297 * This function is for use by GPIO controller drivers. The label can
2298 * help with diagnostics, and knowing that the signal is used as a GPIO
2299 * can help avoid accidentally multiplexing it to another controller.
2301 const char *gpiochip_is_requested(struct gpio_chip
*chip
, unsigned offset
)
2303 struct gpio_desc
*desc
;
2305 if (offset
>= chip
->ngpio
)
2308 desc
= &chip
->gpiodev
->descs
[offset
];
2310 if (test_bit(FLAG_REQUESTED
, &desc
->flags
) == 0)
2314 EXPORT_SYMBOL_GPL(gpiochip_is_requested
);
2317 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2319 * @hwnum: hardware number of the GPIO for which to request the descriptor
2320 * @label: label for the GPIO
2322 * Function allows GPIO chip drivers to request and use their own GPIO
2323 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2324 * function will not increase reference count of the GPIO chip module. This
2325 * allows the GPIO chip module to be unloaded as needed (we assume that the
2326 * GPIO chip driver handles freeing the GPIOs it has requested).
2329 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2332 struct gpio_desc
*gpiochip_request_own_desc(struct gpio_chip
*chip
, u16 hwnum
,
2335 struct gpio_desc
*desc
= gpiochip_get_desc(chip
, hwnum
);
2339 chip_err(chip
, "failed to get GPIO descriptor\n");
2343 err
= gpiod_request_commit(desc
, label
);
2345 return ERR_PTR(err
);
2349 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc
);
2352 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2353 * @desc: GPIO descriptor to free
2355 * Function frees the given GPIO requested previously with
2356 * gpiochip_request_own_desc().
2358 void gpiochip_free_own_desc(struct gpio_desc
*desc
)
2361 gpiod_free_commit(desc
);
2363 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc
);
2366 * Drivers MUST set GPIO direction before making get/set calls. In
2367 * some cases this is done in early boot, before IRQs are enabled.
2369 * As a rule these aren't called more than once (except for drivers
2370 * using the open-drain emulation idiom) so these are natural places
2371 * to accumulate extra debugging checks. Note that we can't (yet)
2372 * rely on gpio_request() having been called beforehand.
2376 * gpiod_direction_input - set the GPIO direction to input
2377 * @desc: GPIO to set to input
2379 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2380 * be called safely on it.
2382 * Return 0 in case of success, else an error code.
2384 int gpiod_direction_input(struct gpio_desc
*desc
)
2386 struct gpio_chip
*chip
;
2387 int status
= -EINVAL
;
2389 VALIDATE_DESC(desc
);
2390 chip
= desc
->gdev
->chip
;
2392 if (!chip
->get
|| !chip
->direction_input
) {
2394 "%s: missing get() or direction_input() operations\n",
2399 status
= chip
->direction_input(chip
, gpio_chip_hwgpio(desc
));
2401 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
2403 trace_gpio_direction(desc_to_gpio(desc
), 1, status
);
2407 EXPORT_SYMBOL_GPL(gpiod_direction_input
);
2409 static int gpio_set_drive_single_ended(struct gpio_chip
*gc
, unsigned offset
,
2410 enum pin_config_param mode
)
2412 unsigned long config
= { PIN_CONF_PACKED(mode
, 0) };
2414 return gc
->set_config
? gc
->set_config(gc
, offset
, config
) : -ENOTSUPP
;
2417 static int gpiod_direction_output_raw_commit(struct gpio_desc
*desc
, int value
)
2419 struct gpio_chip
*gc
= desc
->gdev
->chip
;
2423 if (!gc
->set
|| !gc
->direction_output
) {
2425 "%s: missing set() or direction_output() operations\n",
2430 ret
= gc
->direction_output(gc
, gpio_chip_hwgpio(desc
), val
);
2432 set_bit(FLAG_IS_OUT
, &desc
->flags
);
2433 trace_gpio_value(desc_to_gpio(desc
), 0, val
);
2434 trace_gpio_direction(desc_to_gpio(desc
), 0, ret
);
2439 * gpiod_direction_output_raw - set the GPIO direction to output
2440 * @desc: GPIO to set to output
2441 * @value: initial output value of the GPIO
2443 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2444 * be called safely on it. The initial value of the output must be specified
2445 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2447 * Return 0 in case of success, else an error code.
2449 int gpiod_direction_output_raw(struct gpio_desc
*desc
, int value
)
2451 VALIDATE_DESC(desc
);
2452 return gpiod_direction_output_raw_commit(desc
, value
);
2454 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw
);
2457 * gpiod_direction_output - set the GPIO direction to output
2458 * @desc: GPIO to set to output
2459 * @value: initial output value of the GPIO
2461 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2462 * be called safely on it. The initial value of the output must be specified
2463 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2466 * Return 0 in case of success, else an error code.
2468 int gpiod_direction_output(struct gpio_desc
*desc
, int value
)
2470 struct gpio_chip
*gc
;
2473 VALIDATE_DESC(desc
);
2474 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
2479 /* GPIOs used for IRQs shall not be set as output */
2480 if (test_bit(FLAG_USED_AS_IRQ
, &desc
->flags
)) {
2482 "%s: tried to set a GPIO tied to an IRQ as output\n",
2487 gc
= desc
->gdev
->chip
;
2488 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
)) {
2489 /* First see if we can enable open drain in hardware */
2490 ret
= gpio_set_drive_single_ended(gc
, gpio_chip_hwgpio(desc
),
2491 PIN_CONFIG_DRIVE_OPEN_DRAIN
);
2493 goto set_output_value
;
2494 /* Emulate open drain by not actively driving the line high */
2496 return gpiod_direction_input(desc
);
2498 else if (test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
)) {
2499 ret
= gpio_set_drive_single_ended(gc
, gpio_chip_hwgpio(desc
),
2500 PIN_CONFIG_DRIVE_OPEN_SOURCE
);
2502 goto set_output_value
;
2503 /* Emulate open source by not actively driving the line low */
2505 return gpiod_direction_input(desc
);
2507 gpio_set_drive_single_ended(gc
, gpio_chip_hwgpio(desc
),
2508 PIN_CONFIG_DRIVE_PUSH_PULL
);
2512 return gpiod_direction_output_raw_commit(desc
, value
);
2514 EXPORT_SYMBOL_GPL(gpiod_direction_output
);
2517 * gpiod_set_debounce - sets @debounce time for a GPIO
2518 * @desc: descriptor of the GPIO for which to set debounce time
2519 * @debounce: debounce time in microseconds
2522 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2525 int gpiod_set_debounce(struct gpio_desc
*desc
, unsigned debounce
)
2527 struct gpio_chip
*chip
;
2528 unsigned long config
;
2530 VALIDATE_DESC(desc
);
2531 chip
= desc
->gdev
->chip
;
2532 if (!chip
->set
|| !chip
->set_config
) {
2534 "%s: missing set() or set_config() operations\n",
2539 config
= pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE
, debounce
);
2540 return chip
->set_config(chip
, gpio_chip_hwgpio(desc
), config
);
2542 EXPORT_SYMBOL_GPL(gpiod_set_debounce
);
2545 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2546 * @desc: descriptor of the GPIO for which to configure persistence
2547 * @transitory: True to lose state on suspend or reset, false for persistence
2550 * 0 on success, otherwise a negative error code.
2552 int gpiod_set_transitory(struct gpio_desc
*desc
, bool transitory
)
2554 struct gpio_chip
*chip
;
2555 unsigned long packed
;
2559 VALIDATE_DESC(desc
);
2561 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2562 * persistence state.
2565 set_bit(FLAG_TRANSITORY
, &desc
->flags
);
2567 clear_bit(FLAG_TRANSITORY
, &desc
->flags
);
2569 /* If the driver supports it, set the persistence state now */
2570 chip
= desc
->gdev
->chip
;
2571 if (!chip
->set_config
)
2574 packed
= pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE
,
2576 gpio
= gpio_chip_hwgpio(desc
);
2577 rc
= chip
->set_config(chip
, gpio
, packed
);
2578 if (rc
== -ENOTSUPP
) {
2579 dev_dbg(&desc
->gdev
->dev
, "Persistence not supported for GPIO %d\n",
2586 EXPORT_SYMBOL_GPL(gpiod_set_transitory
);
2589 * gpiod_is_active_low - test whether a GPIO is active-low or not
2590 * @desc: the gpio descriptor to test
2592 * Returns 1 if the GPIO is active-low, 0 otherwise.
2594 int gpiod_is_active_low(const struct gpio_desc
*desc
)
2596 VALIDATE_DESC(desc
);
2597 return test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
2599 EXPORT_SYMBOL_GPL(gpiod_is_active_low
);
2601 /* I/O calls are only valid after configuration completed; the relevant
2602 * "is this a valid GPIO" error checks should already have been done.
2604 * "Get" operations are often inlinable as reading a pin value register,
2605 * and masking the relevant bit in that register.
2607 * When "set" operations are inlinable, they involve writing that mask to
2608 * one register to set a low value, or a different register to set it high.
2609 * Otherwise locking is needed, so there may be little value to inlining.
2611 *------------------------------------------------------------------------
2613 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2614 * have requested the GPIO. That can include implicit requesting by
2615 * a direction setting call. Marking a gpio as requested locks its chip
2616 * in memory, guaranteeing that these table lookups need no more locking
2617 * and that gpiochip_remove() will fail.
2619 * REVISIT when debugging, consider adding some instrumentation to ensure
2620 * that the GPIO was actually requested.
2623 static int gpiod_get_raw_value_commit(const struct gpio_desc
*desc
)
2625 struct gpio_chip
*chip
;
2629 chip
= desc
->gdev
->chip
;
2630 offset
= gpio_chip_hwgpio(desc
);
2631 value
= chip
->get
? chip
->get(chip
, offset
) : -EIO
;
2632 value
= value
< 0 ? value
: !!value
;
2633 trace_gpio_value(desc_to_gpio(desc
), 1, value
);
2637 static int gpio_chip_get_multiple(struct gpio_chip
*chip
,
2638 unsigned long *mask
, unsigned long *bits
)
2640 if (chip
->get_multiple
) {
2641 return chip
->get_multiple(chip
, mask
, bits
);
2642 } else if (chip
->get
) {
2645 for_each_set_bit(i
, mask
, chip
->ngpio
) {
2646 value
= chip
->get(chip
, i
);
2649 __assign_bit(i
, bits
, value
);
2656 int gpiod_get_array_value_complex(bool raw
, bool can_sleep
,
2657 unsigned int array_size
,
2658 struct gpio_desc
**desc_array
,
2663 while (i
< array_size
) {
2664 struct gpio_chip
*chip
= desc_array
[i
]->gdev
->chip
;
2665 unsigned long mask
[BITS_TO_LONGS(chip
->ngpio
)];
2666 unsigned long bits
[BITS_TO_LONGS(chip
->ngpio
)];
2670 WARN_ON(chip
->can_sleep
);
2672 /* collect all inputs belonging to the same chip */
2674 memset(mask
, 0, sizeof(mask
));
2676 const struct gpio_desc
*desc
= desc_array
[i
];
2677 int hwgpio
= gpio_chip_hwgpio(desc
);
2679 __set_bit(hwgpio
, mask
);
2681 } while ((i
< array_size
) &&
2682 (desc_array
[i
]->gdev
->chip
== chip
));
2684 ret
= gpio_chip_get_multiple(chip
, mask
, bits
);
2688 for (j
= first
; j
< i
; j
++) {
2689 const struct gpio_desc
*desc
= desc_array
[j
];
2690 int hwgpio
= gpio_chip_hwgpio(desc
);
2691 int value
= test_bit(hwgpio
, bits
);
2693 if (!raw
&& test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
2695 value_array
[j
] = value
;
2696 trace_gpio_value(desc_to_gpio(desc
), 1, value
);
2703 * gpiod_get_raw_value() - return a gpio's raw value
2704 * @desc: gpio whose value will be returned
2706 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2707 * its ACTIVE_LOW status, or negative errno on failure.
2709 * This function should be called from contexts where we cannot sleep, and will
2710 * complain if the GPIO chip functions potentially sleep.
2712 int gpiod_get_raw_value(const struct gpio_desc
*desc
)
2714 VALIDATE_DESC(desc
);
2715 /* Should be using gpio_get_value_cansleep() */
2716 WARN_ON(desc
->gdev
->chip
->can_sleep
);
2717 return gpiod_get_raw_value_commit(desc
);
2719 EXPORT_SYMBOL_GPL(gpiod_get_raw_value
);
2722 * gpiod_get_value() - return a gpio's value
2723 * @desc: gpio whose value will be returned
2725 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2726 * account, or negative errno on failure.
2728 * This function should be called from contexts where we cannot sleep, and will
2729 * complain if the GPIO chip functions potentially sleep.
2731 int gpiod_get_value(const struct gpio_desc
*desc
)
2735 VALIDATE_DESC(desc
);
2736 /* Should be using gpio_get_value_cansleep() */
2737 WARN_ON(desc
->gdev
->chip
->can_sleep
);
2739 value
= gpiod_get_raw_value_commit(desc
);
2743 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
2748 EXPORT_SYMBOL_GPL(gpiod_get_value
);
2751 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2752 * @array_size: number of elements in the descriptor / value arrays
2753 * @desc_array: array of GPIO descriptors whose values will be read
2754 * @value_array: array to store the read values
2756 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2757 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2758 * else an error code.
2760 * This function should be called from contexts where we cannot sleep,
2761 * and it will complain if the GPIO chip functions potentially sleep.
2763 int gpiod_get_raw_array_value(unsigned int array_size
,
2764 struct gpio_desc
**desc_array
, int *value_array
)
2768 return gpiod_get_array_value_complex(true, false, array_size
,
2769 desc_array
, value_array
);
2771 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value
);
2774 * gpiod_get_array_value() - read values from an array of GPIOs
2775 * @array_size: number of elements in the descriptor / value arrays
2776 * @desc_array: array of GPIO descriptors whose values will be read
2777 * @value_array: array to store the read values
2779 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2780 * into account. Return 0 in case of success, else an error code.
2782 * This function should be called from contexts where we cannot sleep,
2783 * and it will complain if the GPIO chip functions potentially sleep.
2785 int gpiod_get_array_value(unsigned int array_size
,
2786 struct gpio_desc
**desc_array
, int *value_array
)
2790 return gpiod_get_array_value_complex(false, false, array_size
,
2791 desc_array
, value_array
);
2793 EXPORT_SYMBOL_GPL(gpiod_get_array_value
);
2796 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
2797 * @desc: gpio descriptor whose state need to be set.
2798 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2800 static void gpio_set_open_drain_value_commit(struct gpio_desc
*desc
, bool value
)
2803 struct gpio_chip
*chip
= desc
->gdev
->chip
;
2804 int offset
= gpio_chip_hwgpio(desc
);
2807 err
= chip
->direction_input(chip
, offset
);
2809 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
2811 err
= chip
->direction_output(chip
, offset
, 0);
2813 set_bit(FLAG_IS_OUT
, &desc
->flags
);
2815 trace_gpio_direction(desc_to_gpio(desc
), value
, err
);
2818 "%s: Error in set_value for open drain err %d\n",
2823 * _gpio_set_open_source_value() - Set the open source gpio's value.
2824 * @desc: gpio descriptor whose state need to be set.
2825 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2827 static void gpio_set_open_source_value_commit(struct gpio_desc
*desc
, bool value
)
2830 struct gpio_chip
*chip
= desc
->gdev
->chip
;
2831 int offset
= gpio_chip_hwgpio(desc
);
2834 err
= chip
->direction_output(chip
, offset
, 1);
2836 set_bit(FLAG_IS_OUT
, &desc
->flags
);
2838 err
= chip
->direction_input(chip
, offset
);
2840 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
2842 trace_gpio_direction(desc_to_gpio(desc
), !value
, err
);
2845 "%s: Error in set_value for open source err %d\n",
2849 static void gpiod_set_raw_value_commit(struct gpio_desc
*desc
, bool value
)
2851 struct gpio_chip
*chip
;
2853 chip
= desc
->gdev
->chip
;
2854 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
2855 chip
->set(chip
, gpio_chip_hwgpio(desc
), value
);
2859 * set multiple outputs on the same chip;
2860 * use the chip's set_multiple function if available;
2861 * otherwise set the outputs sequentially;
2862 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2863 * defines which outputs are to be changed
2864 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2865 * defines the values the outputs specified by mask are to be set to
2867 static void gpio_chip_set_multiple(struct gpio_chip
*chip
,
2868 unsigned long *mask
, unsigned long *bits
)
2870 if (chip
->set_multiple
) {
2871 chip
->set_multiple(chip
, mask
, bits
);
2875 /* set outputs if the corresponding mask bit is set */
2876 for_each_set_bit(i
, mask
, chip
->ngpio
)
2877 chip
->set(chip
, i
, test_bit(i
, bits
));
2881 void gpiod_set_array_value_complex(bool raw
, bool can_sleep
,
2882 unsigned int array_size
,
2883 struct gpio_desc
**desc_array
,
2888 while (i
< array_size
) {
2889 struct gpio_chip
*chip
= desc_array
[i
]->gdev
->chip
;
2890 unsigned long mask
[BITS_TO_LONGS(chip
->ngpio
)];
2891 unsigned long bits
[BITS_TO_LONGS(chip
->ngpio
)];
2895 WARN_ON(chip
->can_sleep
);
2897 memset(mask
, 0, sizeof(mask
));
2899 struct gpio_desc
*desc
= desc_array
[i
];
2900 int hwgpio
= gpio_chip_hwgpio(desc
);
2901 int value
= value_array
[i
];
2903 if (!raw
&& test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
2905 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
2907 * collect all normal outputs belonging to the same chip
2908 * open drain and open source outputs are set individually
2910 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
) && !raw
) {
2911 gpio_set_open_drain_value_commit(desc
, value
);
2912 } else if (test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
) && !raw
) {
2913 gpio_set_open_source_value_commit(desc
, value
);
2915 __set_bit(hwgpio
, mask
);
2917 __set_bit(hwgpio
, bits
);
2919 __clear_bit(hwgpio
, bits
);
2923 } while ((i
< array_size
) &&
2924 (desc_array
[i
]->gdev
->chip
== chip
));
2925 /* push collected bits to outputs */
2927 gpio_chip_set_multiple(chip
, mask
, bits
);
2932 * gpiod_set_raw_value() - assign a gpio's raw value
2933 * @desc: gpio whose value will be assigned
2934 * @value: value to assign
2936 * Set the raw value of the GPIO, i.e. the value of its physical line without
2937 * regard for its ACTIVE_LOW status.
2939 * This function should be called from contexts where we cannot sleep, and will
2940 * complain if the GPIO chip functions potentially sleep.
2942 void gpiod_set_raw_value(struct gpio_desc
*desc
, int value
)
2944 VALIDATE_DESC_VOID(desc
);
2945 /* Should be using gpiod_set_value_cansleep() */
2946 WARN_ON(desc
->gdev
->chip
->can_sleep
);
2947 gpiod_set_raw_value_commit(desc
, value
);
2949 EXPORT_SYMBOL_GPL(gpiod_set_raw_value
);
2952 * gpiod_set_value_nocheck() - set a GPIO line value without checking
2953 * @desc: the descriptor to set the value on
2954 * @value: value to set
2956 * This sets the value of a GPIO line backing a descriptor, applying
2957 * different semantic quirks like active low and open drain/source
2960 static void gpiod_set_value_nocheck(struct gpio_desc
*desc
, int value
)
2962 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
2964 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
))
2965 gpio_set_open_drain_value_commit(desc
, value
);
2966 else if (test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
))
2967 gpio_set_open_source_value_commit(desc
, value
);
2969 gpiod_set_raw_value_commit(desc
, value
);
2973 * gpiod_set_value() - assign a gpio's value
2974 * @desc: gpio whose value will be assigned
2975 * @value: value to assign
2977 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
2978 * OPEN_DRAIN and OPEN_SOURCE flags into account.
2980 * This function should be called from contexts where we cannot sleep, and will
2981 * complain if the GPIO chip functions potentially sleep.
2983 void gpiod_set_value(struct gpio_desc
*desc
, int value
)
2985 VALIDATE_DESC_VOID(desc
);
2986 WARN_ON(desc
->gdev
->chip
->can_sleep
);
2987 gpiod_set_value_nocheck(desc
, value
);
2989 EXPORT_SYMBOL_GPL(gpiod_set_value
);
2992 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
2993 * @array_size: number of elements in the descriptor / value arrays
2994 * @desc_array: array of GPIO descriptors whose values will be assigned
2995 * @value_array: array of values to assign
2997 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2998 * without regard for their ACTIVE_LOW status.
3000 * This function should be called from contexts where we cannot sleep, and will
3001 * complain if the GPIO chip functions potentially sleep.
3003 void gpiod_set_raw_array_value(unsigned int array_size
,
3004 struct gpio_desc
**desc_array
, int *value_array
)
3008 gpiod_set_array_value_complex(true, false, array_size
, desc_array
,
3011 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value
);
3014 * gpiod_set_array_value() - assign values to an array of GPIOs
3015 * @array_size: number of elements in the descriptor / value arrays
3016 * @desc_array: array of GPIO descriptors whose values will be assigned
3017 * @value_array: array of values to assign
3019 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3022 * This function should be called from contexts where we cannot sleep, and will
3023 * complain if the GPIO chip functions potentially sleep.
3025 void gpiod_set_array_value(unsigned int array_size
,
3026 struct gpio_desc
**desc_array
, int *value_array
)
3030 gpiod_set_array_value_complex(false, false, array_size
, desc_array
,
3033 EXPORT_SYMBOL_GPL(gpiod_set_array_value
);
3036 * gpiod_cansleep() - report whether gpio value access may sleep
3037 * @desc: gpio to check
3040 int gpiod_cansleep(const struct gpio_desc
*desc
)
3042 VALIDATE_DESC(desc
);
3043 return desc
->gdev
->chip
->can_sleep
;
3045 EXPORT_SYMBOL_GPL(gpiod_cansleep
);
3048 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3049 * @desc: gpio whose IRQ will be returned (already requested)
3051 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3054 int gpiod_to_irq(const struct gpio_desc
*desc
)
3056 struct gpio_chip
*chip
;
3060 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3061 * requires this function to not return zero on an invalid descriptor
3062 * but rather a negative error number.
3064 if (!desc
|| IS_ERR(desc
) || !desc
->gdev
|| !desc
->gdev
->chip
)
3067 chip
= desc
->gdev
->chip
;
3068 offset
= gpio_chip_hwgpio(desc
);
3070 int retirq
= chip
->to_irq(chip
, offset
);
3072 /* Zero means NO_IRQ */
3080 EXPORT_SYMBOL_GPL(gpiod_to_irq
);
3083 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3084 * @chip: the chip the GPIO to lock belongs to
3085 * @offset: the offset of the GPIO to lock as IRQ
3087 * This is used directly by GPIO drivers that want to lock down
3088 * a certain GPIO line to be used for IRQs.
3090 int gpiochip_lock_as_irq(struct gpio_chip
*chip
, unsigned int offset
)
3092 struct gpio_desc
*desc
;
3094 desc
= gpiochip_get_desc(chip
, offset
);
3096 return PTR_ERR(desc
);
3099 * If it's fast: flush the direction setting if something changed
3102 if (!chip
->can_sleep
&& chip
->get_direction
) {
3103 int dir
= chip
->get_direction(chip
, offset
);
3106 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
3108 set_bit(FLAG_IS_OUT
, &desc
->flags
);
3111 if (test_bit(FLAG_IS_OUT
, &desc
->flags
)) {
3113 "%s: tried to flag a GPIO set as output for IRQ\n",
3118 set_bit(FLAG_USED_AS_IRQ
, &desc
->flags
);
3121 * If the consumer has not set up a label (such as when the
3122 * IRQ is referenced from .to_irq()) we set up a label here
3123 * so it is clear this is used as an interrupt.
3126 desc_set_label(desc
, "interrupt");
3130 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq
);
3133 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3134 * @chip: the chip the GPIO to lock belongs to
3135 * @offset: the offset of the GPIO to lock as IRQ
3137 * This is used directly by GPIO drivers that want to indicate
3138 * that a certain GPIO is no longer used exclusively for IRQ.
3140 void gpiochip_unlock_as_irq(struct gpio_chip
*chip
, unsigned int offset
)
3142 struct gpio_desc
*desc
;
3144 desc
= gpiochip_get_desc(chip
, offset
);
3148 clear_bit(FLAG_USED_AS_IRQ
, &desc
->flags
);
3150 /* If we only had this marking, erase it */
3151 if (desc
->label
&& !strcmp(desc
->label
, "interrupt"))
3152 desc_set_label(desc
, NULL
);
3154 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq
);
3156 bool gpiochip_line_is_irq(struct gpio_chip
*chip
, unsigned int offset
)
3158 if (offset
>= chip
->ngpio
)
3161 return test_bit(FLAG_USED_AS_IRQ
, &chip
->gpiodev
->descs
[offset
].flags
);
3163 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq
);
3165 bool gpiochip_line_is_open_drain(struct gpio_chip
*chip
, unsigned int offset
)
3167 if (offset
>= chip
->ngpio
)
3170 return test_bit(FLAG_OPEN_DRAIN
, &chip
->gpiodev
->descs
[offset
].flags
);
3172 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain
);
3174 bool gpiochip_line_is_open_source(struct gpio_chip
*chip
, unsigned int offset
)
3176 if (offset
>= chip
->ngpio
)
3179 return test_bit(FLAG_OPEN_SOURCE
, &chip
->gpiodev
->descs
[offset
].flags
);
3181 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source
);
3183 bool gpiochip_line_is_persistent(struct gpio_chip
*chip
, unsigned int offset
)
3185 if (offset
>= chip
->ngpio
)
3188 return !test_bit(FLAG_TRANSITORY
, &chip
->gpiodev
->descs
[offset
].flags
);
3190 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent
);
3193 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3194 * @desc: gpio whose value will be returned
3196 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
3197 * its ACTIVE_LOW status, or negative errno on failure.
3199 * This function is to be called from contexts that can sleep.
3201 int gpiod_get_raw_value_cansleep(const struct gpio_desc
*desc
)
3203 might_sleep_if(extra_checks
);
3204 VALIDATE_DESC(desc
);
3205 return gpiod_get_raw_value_commit(desc
);
3207 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep
);
3210 * gpiod_get_value_cansleep() - return a gpio's value
3211 * @desc: gpio whose value will be returned
3213 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3214 * account, or negative errno on failure.
3216 * This function is to be called from contexts that can sleep.
3218 int gpiod_get_value_cansleep(const struct gpio_desc
*desc
)
3222 might_sleep_if(extra_checks
);
3223 VALIDATE_DESC(desc
);
3224 value
= gpiod_get_raw_value_commit(desc
);
3228 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
3233 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep
);
3236 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3237 * @array_size: number of elements in the descriptor / value arrays
3238 * @desc_array: array of GPIO descriptors whose values will be read
3239 * @value_array: array to store the read values
3241 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3242 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3243 * else an error code.
3245 * This function is to be called from contexts that can sleep.
3247 int gpiod_get_raw_array_value_cansleep(unsigned int array_size
,
3248 struct gpio_desc
**desc_array
,
3251 might_sleep_if(extra_checks
);
3254 return gpiod_get_array_value_complex(true, true, array_size
,
3255 desc_array
, value_array
);
3257 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep
);
3260 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3261 * @array_size: number of elements in the descriptor / value arrays
3262 * @desc_array: array of GPIO descriptors whose values will be read
3263 * @value_array: array to store the read values
3265 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3266 * into account. Return 0 in case of success, else an error code.
3268 * This function is to be called from contexts that can sleep.
3270 int gpiod_get_array_value_cansleep(unsigned int array_size
,
3271 struct gpio_desc
**desc_array
,
3274 might_sleep_if(extra_checks
);
3277 return gpiod_get_array_value_complex(false, true, array_size
,
3278 desc_array
, value_array
);
3280 EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep
);
3283 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3284 * @desc: gpio whose value will be assigned
3285 * @value: value to assign
3287 * Set the raw value of the GPIO, i.e. the value of its physical line without
3288 * regard for its ACTIVE_LOW status.
3290 * This function is to be called from contexts that can sleep.
3292 void gpiod_set_raw_value_cansleep(struct gpio_desc
*desc
, int value
)
3294 might_sleep_if(extra_checks
);
3295 VALIDATE_DESC_VOID(desc
);
3296 gpiod_set_raw_value_commit(desc
, value
);
3298 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep
);
3301 * gpiod_set_value_cansleep() - assign a gpio's value
3302 * @desc: gpio whose value will be assigned
3303 * @value: value to assign
3305 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3308 * This function is to be called from contexts that can sleep.
3310 void gpiod_set_value_cansleep(struct gpio_desc
*desc
, int value
)
3312 might_sleep_if(extra_checks
);
3313 VALIDATE_DESC_VOID(desc
);
3314 gpiod_set_value_nocheck(desc
, value
);
3316 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep
);
3319 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
3320 * @array_size: number of elements in the descriptor / value arrays
3321 * @desc_array: array of GPIO descriptors whose values will be assigned
3322 * @value_array: array of values to assign
3324 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3325 * without regard for their ACTIVE_LOW status.
3327 * This function is to be called from contexts that can sleep.
3329 void gpiod_set_raw_array_value_cansleep(unsigned int array_size
,
3330 struct gpio_desc
**desc_array
,
3333 might_sleep_if(extra_checks
);
3336 gpiod_set_array_value_complex(true, true, array_size
, desc_array
,
3339 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep
);
3342 * gpiod_add_lookup_tables() - register GPIO device consumers
3343 * @tables: list of tables of consumers to register
3344 * @n: number of tables in the list
3346 void gpiod_add_lookup_tables(struct gpiod_lookup_table
**tables
, size_t n
)
3350 mutex_lock(&gpio_lookup_lock
);
3352 for (i
= 0; i
< n
; i
++)
3353 list_add_tail(&tables
[i
]->list
, &gpio_lookup_list
);
3355 mutex_unlock(&gpio_lookup_lock
);
3359 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
3360 * @array_size: number of elements in the descriptor / value arrays
3361 * @desc_array: array of GPIO descriptors whose values will be assigned
3362 * @value_array: array of values to assign
3364 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3367 * This function is to be called from contexts that can sleep.
3369 void gpiod_set_array_value_cansleep(unsigned int array_size
,
3370 struct gpio_desc
**desc_array
,
3373 might_sleep_if(extra_checks
);
3376 gpiod_set_array_value_complex(false, true, array_size
, desc_array
,
3379 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep
);
3382 * gpiod_add_lookup_table() - register GPIO device consumers
3383 * @table: table of consumers to register
3385 void gpiod_add_lookup_table(struct gpiod_lookup_table
*table
)
3387 mutex_lock(&gpio_lookup_lock
);
3389 list_add_tail(&table
->list
, &gpio_lookup_list
);
3391 mutex_unlock(&gpio_lookup_lock
);
3393 EXPORT_SYMBOL_GPL(gpiod_add_lookup_table
);
3396 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3397 * @table: table of consumers to unregister
3399 void gpiod_remove_lookup_table(struct gpiod_lookup_table
*table
)
3401 mutex_lock(&gpio_lookup_lock
);
3403 list_del(&table
->list
);
3405 mutex_unlock(&gpio_lookup_lock
);
3407 EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table
);
3409 static struct gpiod_lookup_table
*gpiod_find_lookup_table(struct device
*dev
)
3411 const char *dev_id
= dev
? dev_name(dev
) : NULL
;
3412 struct gpiod_lookup_table
*table
;
3414 mutex_lock(&gpio_lookup_lock
);
3416 list_for_each_entry(table
, &gpio_lookup_list
, list
) {
3417 if (table
->dev_id
&& dev_id
) {
3419 * Valid strings on both ends, must be identical to have
3422 if (!strcmp(table
->dev_id
, dev_id
))
3426 * One of the pointers is NULL, so both must be to have
3429 if (dev_id
== table
->dev_id
)
3436 mutex_unlock(&gpio_lookup_lock
);
3440 static struct gpio_desc
*gpiod_find(struct device
*dev
, const char *con_id
,
3442 enum gpio_lookup_flags
*flags
)
3444 struct gpio_desc
*desc
= ERR_PTR(-ENOENT
);
3445 struct gpiod_lookup_table
*table
;
3446 struct gpiod_lookup
*p
;
3448 table
= gpiod_find_lookup_table(dev
);
3452 for (p
= &table
->table
[0]; p
->chip_label
; p
++) {
3453 struct gpio_chip
*chip
;
3455 /* idx must always match exactly */
3459 /* If the lookup entry has a con_id, require exact match */
3460 if (p
->con_id
&& (!con_id
|| strcmp(p
->con_id
, con_id
)))
3463 chip
= find_chip_by_name(p
->chip_label
);
3466 dev_err(dev
, "cannot find GPIO chip %s\n",
3468 return ERR_PTR(-ENODEV
);
3471 if (chip
->ngpio
<= p
->chip_hwnum
) {
3473 "requested GPIO %d is out of range [0..%d] for chip %s\n",
3474 idx
, chip
->ngpio
, chip
->label
);
3475 return ERR_PTR(-EINVAL
);
3478 desc
= gpiochip_get_desc(chip
, p
->chip_hwnum
);
3487 static int dt_gpio_count(struct device
*dev
, const char *con_id
)
3493 for (i
= 0; i
< ARRAY_SIZE(gpio_suffixes
); i
++) {
3495 snprintf(propname
, sizeof(propname
), "%s-%s",
3496 con_id
, gpio_suffixes
[i
]);
3498 snprintf(propname
, sizeof(propname
), "%s",
3501 ret
= of_gpio_named_count(dev
->of_node
, propname
);
3505 return ret
? ret
: -ENOENT
;
3508 static int platform_gpio_count(struct device
*dev
, const char *con_id
)
3510 struct gpiod_lookup_table
*table
;
3511 struct gpiod_lookup
*p
;
3512 unsigned int count
= 0;
3514 table
= gpiod_find_lookup_table(dev
);
3518 for (p
= &table
->table
[0]; p
->chip_label
; p
++) {
3519 if ((con_id
&& p
->con_id
&& !strcmp(con_id
, p
->con_id
)) ||
3520 (!con_id
&& !p
->con_id
))
3530 * gpiod_count - return the number of GPIOs associated with a device / function
3531 * or -ENOENT if no GPIO has been assigned to the requested function
3532 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3533 * @con_id: function within the GPIO consumer
3535 int gpiod_count(struct device
*dev
, const char *con_id
)
3537 int count
= -ENOENT
;
3539 if (IS_ENABLED(CONFIG_OF
) && dev
&& dev
->of_node
)
3540 count
= dt_gpio_count(dev
, con_id
);
3541 else if (IS_ENABLED(CONFIG_ACPI
) && dev
&& ACPI_HANDLE(dev
))
3542 count
= acpi_gpio_count(dev
, con_id
);
3545 count
= platform_gpio_count(dev
, con_id
);
3549 EXPORT_SYMBOL_GPL(gpiod_count
);
3552 * gpiod_get - obtain a GPIO for a given GPIO function
3553 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3554 * @con_id: function within the GPIO consumer
3555 * @flags: optional GPIO initialization flags
3557 * Return the GPIO descriptor corresponding to the function con_id of device
3558 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
3559 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
3561 struct gpio_desc
*__must_check
gpiod_get(struct device
*dev
, const char *con_id
,
3562 enum gpiod_flags flags
)
3564 return gpiod_get_index(dev
, con_id
, 0, flags
);
3566 EXPORT_SYMBOL_GPL(gpiod_get
);
3569 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3570 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3571 * @con_id: function within the GPIO consumer
3572 * @flags: optional GPIO initialization flags
3574 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3575 * the requested function it will return NULL. This is convenient for drivers
3576 * that need to handle optional GPIOs.
3578 struct gpio_desc
*__must_check
gpiod_get_optional(struct device
*dev
,
3580 enum gpiod_flags flags
)
3582 return gpiod_get_index_optional(dev
, con_id
, 0, flags
);
3584 EXPORT_SYMBOL_GPL(gpiod_get_optional
);
3588 * gpiod_configure_flags - helper function to configure a given GPIO
3589 * @desc: gpio whose value will be assigned
3590 * @con_id: function within the GPIO consumer
3591 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3593 * @dflags: gpiod_flags - optional GPIO initialization flags
3595 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3596 * requested function and/or index, or another IS_ERR() code if an error
3597 * occurred while trying to acquire the GPIO.
3599 int gpiod_configure_flags(struct gpio_desc
*desc
, const char *con_id
,
3600 unsigned long lflags
, enum gpiod_flags dflags
)
3604 if (lflags
& GPIO_ACTIVE_LOW
)
3605 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
3607 if (lflags
& GPIO_OPEN_DRAIN
)
3608 set_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
3609 else if (dflags
& GPIOD_FLAGS_BIT_OPEN_DRAIN
) {
3611 * This enforces open drain mode from the consumer side.
3612 * This is necessary for some busses like I2C, but the lookup
3613 * should *REALLY* have specified them as open drain in the
3614 * first place, so print a little warning here.
3616 set_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
3618 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
3621 if (lflags
& GPIO_OPEN_SOURCE
)
3622 set_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
3624 status
= gpiod_set_transitory(desc
, (lflags
& GPIO_TRANSITORY
));
3628 /* No particular flag request, return here... */
3629 if (!(dflags
& GPIOD_FLAGS_BIT_DIR_SET
)) {
3630 pr_debug("no flags found for %s\n", con_id
);
3635 if (dflags
& GPIOD_FLAGS_BIT_DIR_OUT
)
3636 status
= gpiod_direction_output(desc
,
3637 !!(dflags
& GPIOD_FLAGS_BIT_DIR_VAL
));
3639 status
= gpiod_direction_input(desc
);
3645 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
3646 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3647 * @con_id: function within the GPIO consumer
3648 * @idx: index of the GPIO to obtain in the consumer
3649 * @flags: optional GPIO initialization flags
3651 * This variant of gpiod_get() allows to access GPIOs other than the first
3652 * defined one for functions that define several GPIOs.
3654 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3655 * requested function and/or index, or another IS_ERR() code if an error
3656 * occurred while trying to acquire the GPIO.
3658 struct gpio_desc
*__must_check
gpiod_get_index(struct device
*dev
,
3661 enum gpiod_flags flags
)
3663 struct gpio_desc
*desc
= NULL
;
3665 enum gpio_lookup_flags lookupflags
= 0;
3666 /* Maybe we have a device name, maybe not */
3667 const char *devname
= dev
? dev_name(dev
) : "?";
3669 dev_dbg(dev
, "GPIO lookup for consumer %s\n", con_id
);
3672 /* Using device tree? */
3673 if (IS_ENABLED(CONFIG_OF
) && dev
->of_node
) {
3674 dev_dbg(dev
, "using device tree for GPIO lookup\n");
3675 desc
= of_find_gpio(dev
, con_id
, idx
, &lookupflags
);
3676 } else if (ACPI_COMPANION(dev
)) {
3677 dev_dbg(dev
, "using ACPI for GPIO lookup\n");
3678 desc
= acpi_find_gpio(dev
, con_id
, idx
, &flags
, &lookupflags
);
3683 * Either we are not using DT or ACPI, or their lookup did not return
3684 * a result. In that case, use platform lookup as a fallback.
3686 if (!desc
|| desc
== ERR_PTR(-ENOENT
)) {
3687 dev_dbg(dev
, "using lookup tables for GPIO lookup\n");
3688 desc
= gpiod_find(dev
, con_id
, idx
, &lookupflags
);
3692 dev_dbg(dev
, "lookup for GPIO %s failed\n", con_id
);
3697 * If a connection label was passed use that, else attempt to use
3698 * the device name as label
3700 status
= gpiod_request(desc
, con_id
? con_id
: devname
);
3702 return ERR_PTR(status
);
3704 status
= gpiod_configure_flags(desc
, con_id
, lookupflags
, flags
);
3706 dev_dbg(dev
, "setup of GPIO %s failed\n", con_id
);
3708 return ERR_PTR(status
);
3713 EXPORT_SYMBOL_GPL(gpiod_get_index
);
3716 * gpiod_get_from_of_node() - obtain a GPIO from an OF node
3717 * @node: handle of the OF node
3718 * @propname: name of the DT property representing the GPIO
3719 * @index: index of the GPIO to obtain for the consumer
3720 * @dflags: GPIO initialization flags
3721 * @label: label to attach to the requested GPIO
3724 * On successful request the GPIO pin is configured in accordance with
3725 * provided @dflags. If the node does not have the requested GPIO
3726 * property, NULL is returned.
3728 * In case of error an ERR_PTR() is returned.
3730 struct gpio_desc
*gpiod_get_from_of_node(struct device_node
*node
,
3731 const char *propname
, int index
,
3732 enum gpiod_flags dflags
,
3735 struct gpio_desc
*desc
;
3736 unsigned long lflags
= 0;
3737 enum of_gpio_flags flags
;
3738 bool active_low
= false;
3739 bool single_ended
= false;
3740 bool open_drain
= false;
3741 bool transitory
= false;
3744 desc
= of_get_named_gpiod_flags(node
, propname
,
3747 if (!desc
|| IS_ERR(desc
)) {
3748 /* If it is not there, just return NULL */
3749 if (PTR_ERR(desc
) == -ENOENT
)
3754 active_low
= flags
& OF_GPIO_ACTIVE_LOW
;
3755 single_ended
= flags
& OF_GPIO_SINGLE_ENDED
;
3756 open_drain
= flags
& OF_GPIO_OPEN_DRAIN
;
3757 transitory
= flags
& OF_GPIO_TRANSITORY
;
3759 ret
= gpiod_request(desc
, label
);
3761 return ERR_PTR(ret
);
3764 lflags
|= GPIO_ACTIVE_LOW
;
3768 lflags
|= GPIO_OPEN_DRAIN
;
3770 lflags
|= GPIO_OPEN_SOURCE
;
3774 lflags
|= GPIO_TRANSITORY
;
3776 ret
= gpiod_configure_flags(desc
, propname
, lflags
, dflags
);
3779 return ERR_PTR(ret
);
3784 EXPORT_SYMBOL(gpiod_get_from_of_node
);
3787 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3788 * @fwnode: handle of the firmware node
3789 * @propname: name of the firmware property representing the GPIO
3790 * @index: index of the GPIO to obtain for the consumer
3791 * @dflags: GPIO initialization flags
3792 * @label: label to attach to the requested GPIO
3794 * This function can be used for drivers that get their configuration
3795 * from opaque firmware.
3797 * The function properly finds the corresponding GPIO using whatever is the
3798 * underlying firmware interface and then makes sure that the GPIO
3799 * descriptor is requested before it is returned to the caller.
3802 * On successful request the GPIO pin is configured in accordance with
3805 * In case of error an ERR_PTR() is returned.
3807 struct gpio_desc
*fwnode_get_named_gpiod(struct fwnode_handle
*fwnode
,
3808 const char *propname
, int index
,
3809 enum gpiod_flags dflags
,
3812 struct gpio_desc
*desc
= ERR_PTR(-ENODEV
);
3813 unsigned long lflags
= 0;
3817 return ERR_PTR(-EINVAL
);
3819 if (is_of_node(fwnode
)) {
3820 desc
= gpiod_get_from_of_node(to_of_node(fwnode
),
3825 } else if (is_acpi_node(fwnode
)) {
3826 struct acpi_gpio_info info
;
3828 desc
= acpi_node_get_gpiod(fwnode
, propname
, index
, &info
);
3832 acpi_gpio_update_gpiod_flags(&dflags
, &info
);
3834 if (info
.polarity
== GPIO_ACTIVE_LOW
)
3835 lflags
|= GPIO_ACTIVE_LOW
;
3838 /* Currently only ACPI takes this path */
3839 ret
= gpiod_request(desc
, label
);
3841 return ERR_PTR(ret
);
3843 ret
= gpiod_configure_flags(desc
, propname
, lflags
, dflags
);
3846 return ERR_PTR(ret
);
3851 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod
);
3854 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3856 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3857 * @con_id: function within the GPIO consumer
3858 * @index: index of the GPIO to obtain in the consumer
3859 * @flags: optional GPIO initialization flags
3861 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3862 * specified index was assigned to the requested function it will return NULL.
3863 * This is convenient for drivers that need to handle optional GPIOs.
3865 struct gpio_desc
*__must_check
gpiod_get_index_optional(struct device
*dev
,
3868 enum gpiod_flags flags
)
3870 struct gpio_desc
*desc
;
3872 desc
= gpiod_get_index(dev
, con_id
, index
, flags
);
3874 if (PTR_ERR(desc
) == -ENOENT
)
3880 EXPORT_SYMBOL_GPL(gpiod_get_index_optional
);
3883 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3884 * @desc: gpio whose value will be assigned
3885 * @name: gpio line name
3886 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3888 * @dflags: gpiod_flags - optional GPIO initialization flags
3890 int gpiod_hog(struct gpio_desc
*desc
, const char *name
,
3891 unsigned long lflags
, enum gpiod_flags dflags
)
3893 struct gpio_chip
*chip
;
3894 struct gpio_desc
*local_desc
;
3898 chip
= gpiod_to_chip(desc
);
3899 hwnum
= gpio_chip_hwgpio(desc
);
3901 local_desc
= gpiochip_request_own_desc(chip
, hwnum
, name
);
3902 if (IS_ERR(local_desc
)) {
3903 status
= PTR_ERR(local_desc
);
3904 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3905 name
, chip
->label
, hwnum
, status
);
3909 status
= gpiod_configure_flags(desc
, name
, lflags
, dflags
);
3911 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3912 name
, chip
->label
, hwnum
, status
);
3913 gpiochip_free_own_desc(desc
);
3917 /* Mark GPIO as hogged so it can be identified and removed later */
3918 set_bit(FLAG_IS_HOGGED
, &desc
->flags
);
3920 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3921 desc_to_gpio(desc
), name
,
3922 (dflags
&GPIOD_FLAGS_BIT_DIR_OUT
) ? "output" : "input",
3923 (dflags
&GPIOD_FLAGS_BIT_DIR_OUT
) ?
3924 (dflags
&GPIOD_FLAGS_BIT_DIR_VAL
) ? "/high" : "/low":"");
3930 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3931 * @chip: gpio chip to act on
3933 * This is only used by of_gpiochip_remove to free hogged gpios
3935 static void gpiochip_free_hogs(struct gpio_chip
*chip
)
3939 for (id
= 0; id
< chip
->ngpio
; id
++) {
3940 if (test_bit(FLAG_IS_HOGGED
, &chip
->gpiodev
->descs
[id
].flags
))
3941 gpiochip_free_own_desc(&chip
->gpiodev
->descs
[id
]);
3946 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
3947 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3948 * @con_id: function within the GPIO consumer
3949 * @flags: optional GPIO initialization flags
3951 * This function acquires all the GPIOs defined under a given function.
3953 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
3954 * no GPIO has been assigned to the requested function, or another IS_ERR()
3955 * code if an error occurred while trying to acquire the GPIOs.
3957 struct gpio_descs
*__must_check
gpiod_get_array(struct device
*dev
,
3959 enum gpiod_flags flags
)
3961 struct gpio_desc
*desc
;
3962 struct gpio_descs
*descs
;
3965 count
= gpiod_count(dev
, con_id
);
3967 return ERR_PTR(count
);
3969 descs
= kzalloc(sizeof(*descs
) + sizeof(descs
->desc
[0]) * count
,
3972 return ERR_PTR(-ENOMEM
);
3974 for (descs
->ndescs
= 0; descs
->ndescs
< count
; ) {
3975 desc
= gpiod_get_index(dev
, con_id
, descs
->ndescs
, flags
);
3977 gpiod_put_array(descs
);
3978 return ERR_CAST(desc
);
3980 descs
->desc
[descs
->ndescs
] = desc
;
3985 EXPORT_SYMBOL_GPL(gpiod_get_array
);
3988 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
3990 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3991 * @con_id: function within the GPIO consumer
3992 * @flags: optional GPIO initialization flags
3994 * This is equivalent to gpiod_get_array(), except that when no GPIO was
3995 * assigned to the requested function it will return NULL.
3997 struct gpio_descs
*__must_check
gpiod_get_array_optional(struct device
*dev
,
3999 enum gpiod_flags flags
)
4001 struct gpio_descs
*descs
;
4003 descs
= gpiod_get_array(dev
, con_id
, flags
);
4004 if (IS_ERR(descs
) && (PTR_ERR(descs
) == -ENOENT
))
4009 EXPORT_SYMBOL_GPL(gpiod_get_array_optional
);
4012 * gpiod_put - dispose of a GPIO descriptor
4013 * @desc: GPIO descriptor to dispose of
4015 * No descriptor can be used after gpiod_put() has been called on it.
4017 void gpiod_put(struct gpio_desc
*desc
)
4021 EXPORT_SYMBOL_GPL(gpiod_put
);
4024 * gpiod_put_array - dispose of multiple GPIO descriptors
4025 * @descs: struct gpio_descs containing an array of descriptors
4027 void gpiod_put_array(struct gpio_descs
*descs
)
4031 for (i
= 0; i
< descs
->ndescs
; i
++)
4032 gpiod_put(descs
->desc
[i
]);
4036 EXPORT_SYMBOL_GPL(gpiod_put_array
);
4038 static int __init
gpiolib_dev_init(void)
4042 /* Register GPIO sysfs bus */
4043 ret
= bus_register(&gpio_bus_type
);
4045 pr_err("gpiolib: could not register GPIO bus type\n");
4049 ret
= alloc_chrdev_region(&gpio_devt
, 0, GPIO_DEV_MAX
, "gpiochip");
4051 pr_err("gpiolib: failed to allocate char dev region\n");
4052 bus_unregister(&gpio_bus_type
);
4054 gpiolib_initialized
= true;
4055 gpiochip_setup_devs();
4059 core_initcall(gpiolib_dev_init
);
4061 #ifdef CONFIG_DEBUG_FS
4063 static void gpiolib_dbg_show(struct seq_file
*s
, struct gpio_device
*gdev
)
4066 struct gpio_chip
*chip
= gdev
->chip
;
4067 unsigned gpio
= gdev
->base
;
4068 struct gpio_desc
*gdesc
= &gdev
->descs
[0];
4072 for (i
= 0; i
< gdev
->ngpio
; i
++, gpio
++, gdesc
++) {
4073 if (!test_bit(FLAG_REQUESTED
, &gdesc
->flags
)) {
4075 seq_printf(s
, " gpio-%-3d (%-20.20s)\n",
4081 gpiod_get_direction(gdesc
);
4082 is_out
= test_bit(FLAG_IS_OUT
, &gdesc
->flags
);
4083 is_irq
= test_bit(FLAG_USED_AS_IRQ
, &gdesc
->flags
);
4084 seq_printf(s
, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
4085 gpio
, gdesc
->name
? gdesc
->name
: "", gdesc
->label
,
4086 is_out
? "out" : "in ",
4088 ? (chip
->get(chip
, i
) ? "hi" : "lo")
4090 is_irq
? "IRQ" : " ");
4091 seq_printf(s
, "\n");
4095 static void *gpiolib_seq_start(struct seq_file
*s
, loff_t
*pos
)
4097 unsigned long flags
;
4098 struct gpio_device
*gdev
= NULL
;
4099 loff_t index
= *pos
;
4103 spin_lock_irqsave(&gpio_lock
, flags
);
4104 list_for_each_entry(gdev
, &gpio_devices
, list
)
4106 spin_unlock_irqrestore(&gpio_lock
, flags
);
4109 spin_unlock_irqrestore(&gpio_lock
, flags
);
4114 static void *gpiolib_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
4116 unsigned long flags
;
4117 struct gpio_device
*gdev
= v
;
4120 spin_lock_irqsave(&gpio_lock
, flags
);
4121 if (list_is_last(&gdev
->list
, &gpio_devices
))
4124 ret
= list_entry(gdev
->list
.next
, struct gpio_device
, list
);
4125 spin_unlock_irqrestore(&gpio_lock
, flags
);
4133 static void gpiolib_seq_stop(struct seq_file
*s
, void *v
)
4137 static int gpiolib_seq_show(struct seq_file
*s
, void *v
)
4139 struct gpio_device
*gdev
= v
;
4140 struct gpio_chip
*chip
= gdev
->chip
;
4141 struct device
*parent
;
4144 seq_printf(s
, "%s%s: (dangling chip)", (char *)s
->private,
4145 dev_name(&gdev
->dev
));
4149 seq_printf(s
, "%s%s: GPIOs %d-%d", (char *)s
->private,
4150 dev_name(&gdev
->dev
),
4151 gdev
->base
, gdev
->base
+ gdev
->ngpio
- 1);
4152 parent
= chip
->parent
;
4154 seq_printf(s
, ", parent: %s/%s",
4155 parent
->bus
? parent
->bus
->name
: "no-bus",
4158 seq_printf(s
, ", %s", chip
->label
);
4159 if (chip
->can_sleep
)
4160 seq_printf(s
, ", can sleep");
4161 seq_printf(s
, ":\n");
4164 chip
->dbg_show(s
, chip
);
4166 gpiolib_dbg_show(s
, gdev
);
4171 static const struct seq_operations gpiolib_seq_ops
= {
4172 .start
= gpiolib_seq_start
,
4173 .next
= gpiolib_seq_next
,
4174 .stop
= gpiolib_seq_stop
,
4175 .show
= gpiolib_seq_show
,
4178 static int gpiolib_open(struct inode
*inode
, struct file
*file
)
4180 return seq_open(file
, &gpiolib_seq_ops
);
4183 static const struct file_operations gpiolib_operations
= {
4184 .owner
= THIS_MODULE
,
4185 .open
= gpiolib_open
,
4187 .llseek
= seq_lseek
,
4188 .release
= seq_release
,
4191 static int __init
gpiolib_debugfs_init(void)
4193 /* /sys/kernel/debug/gpio */
4194 (void) debugfs_create_file("gpio", S_IFREG
| S_IRUGO
,
4195 NULL
, NULL
, &gpiolib_operations
);
4198 subsys_initcall(gpiolib_debugfs_init
);
4200 #endif /* DEBUG_FS */