1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/acpi.h>
4 #include <linux/array_size.h>
5 #include <linux/bitmap.h>
6 #include <linux/cleanup.h>
7 #include <linux/compat.h>
8 #include <linux/debugfs.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/errno.h>
12 #include <linux/file.h>
14 #include <linux/idr.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/kernel.h>
18 #include <linux/list.h>
19 #include <linux/lockdep.h>
20 #include <linux/module.h>
21 #include <linux/nospec.h>
23 #include <linux/pinctrl/consumer.h>
24 #include <linux/seq_file.h>
25 #include <linux/slab.h>
26 #include <linux/spinlock.h>
27 #include <linux/srcu.h>
28 #include <linux/string.h>
30 #include <linux/gpio.h>
31 #include <linux/gpio/driver.h>
32 #include <linux/gpio/machine.h>
34 #include <uapi/linux/gpio.h>
36 #include "gpiolib-acpi.h"
37 #include "gpiolib-cdev.h"
38 #include "gpiolib-of.h"
39 #include "gpiolib-swnode.h"
40 #include "gpiolib-sysfs.h"
43 #define CREATE_TRACE_POINTS
44 #include <trace/events/gpio.h>
46 /* Implementation infrastructure for GPIO interfaces.
48 * The GPIO programming interface allows for inlining speed-critical
49 * get/set operations for common cases, so that access to SOC-integrated
50 * GPIOs can sometimes cost only an instruction or two per bit.
53 /* Device and char device-related information */
54 static DEFINE_IDA(gpio_ida
);
55 static dev_t gpio_devt
;
56 #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
58 static int gpio_bus_match(struct device
*dev
, const struct device_driver
*drv
)
60 struct fwnode_handle
*fwnode
= dev_fwnode(dev
);
63 * Only match if the fwnode doesn't already have a proper struct device
66 if (fwnode
&& fwnode
->dev
!= dev
)
71 static const struct bus_type gpio_bus_type
= {
73 .match
= gpio_bus_match
,
77 * Number of GPIOs to use for the fast path in set array
79 #define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT
81 static DEFINE_MUTEX(gpio_lookup_lock
);
82 static LIST_HEAD(gpio_lookup_list
);
84 static LIST_HEAD(gpio_devices
);
85 /* Protects the GPIO device list against concurrent modifications. */
86 static DEFINE_MUTEX(gpio_devices_lock
);
87 /* Ensures coherence during read-only accesses to the list of GPIO devices. */
88 DEFINE_STATIC_SRCU(gpio_devices_srcu
);
90 static DEFINE_MUTEX(gpio_machine_hogs_mutex
);
91 static LIST_HEAD(gpio_machine_hogs
);
93 const char *const gpio_suffixes
[] = { "gpios", "gpio", NULL
};
95 static void gpiochip_free_hogs(struct gpio_chip
*gc
);
96 static int gpiochip_add_irqchip(struct gpio_chip
*gc
,
97 struct lock_class_key
*lock_key
,
98 struct lock_class_key
*request_key
);
99 static void gpiochip_irqchip_remove(struct gpio_chip
*gc
);
100 static int gpiochip_irqchip_init_hw(struct gpio_chip
*gc
);
101 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip
*gc
);
102 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip
*gc
);
104 static bool gpiolib_initialized
;
106 const char *gpiod_get_label(struct gpio_desc
*desc
)
108 struct gpio_desc_label
*label
;
111 flags
= READ_ONCE(desc
->flags
);
113 label
= srcu_dereference_check(desc
->label
, &desc
->gdev
->desc_srcu
,
114 srcu_read_lock_held(&desc
->gdev
->desc_srcu
));
116 if (test_bit(FLAG_USED_AS_IRQ
, &flags
))
117 return label
? label
->str
: "interrupt";
119 if (!test_bit(FLAG_REQUESTED
, &flags
))
122 return label
? label
->str
: NULL
;
125 static void desc_free_label(struct rcu_head
*rh
)
127 kfree(container_of(rh
, struct gpio_desc_label
, rh
));
130 static int desc_set_label(struct gpio_desc
*desc
, const char *label
)
132 struct gpio_desc_label
*new = NULL
, *old
;
135 new = kzalloc(struct_size(new, str
, strlen(label
) + 1),
140 strcpy(new->str
, label
);
143 old
= rcu_replace_pointer(desc
->label
, new, 1);
145 call_srcu(&desc
->gdev
->desc_srcu
, &old
->rh
, desc_free_label
);
151 * gpio_to_desc - Convert a GPIO number to its descriptor
152 * @gpio: global GPIO number
155 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
156 * with the given number exists in the system.
158 struct gpio_desc
*gpio_to_desc(unsigned gpio
)
160 struct gpio_device
*gdev
;
162 scoped_guard(srcu
, &gpio_devices_srcu
) {
163 list_for_each_entry_srcu(gdev
, &gpio_devices
, list
,
164 srcu_read_lock_held(&gpio_devices_srcu
)) {
165 if (gdev
->base
<= gpio
&&
166 gdev
->base
+ gdev
->ngpio
> gpio
)
167 return &gdev
->descs
[gpio
- gdev
->base
];
173 EXPORT_SYMBOL_GPL(gpio_to_desc
);
175 /* This function is deprecated and will be removed soon, don't use. */
176 struct gpio_desc
*gpiochip_get_desc(struct gpio_chip
*gc
,
179 return gpio_device_get_desc(gc
->gpiodev
, hwnum
);
183 * gpio_device_get_desc() - get the GPIO descriptor corresponding to the given
184 * hardware number for this GPIO device
185 * @gdev: GPIO device to get the descriptor from
186 * @hwnum: hardware number of the GPIO for this chip
189 * A pointer to the GPIO descriptor or %EINVAL if no GPIO exists in the given
190 * chip for the specified hardware number or %ENODEV if the underlying chip
193 * The reference count of struct gpio_device is *NOT* increased like when the
194 * GPIO is being requested for exclusive usage. It's up to the caller to make
195 * sure the GPIO device will stay alive together with the descriptor returned
199 gpio_device_get_desc(struct gpio_device
*gdev
, unsigned int hwnum
)
201 if (hwnum
>= gdev
->ngpio
)
202 return ERR_PTR(-EINVAL
);
204 return &gdev
->descs
[array_index_nospec(hwnum
, gdev
->ngpio
)];
206 EXPORT_SYMBOL_GPL(gpio_device_get_desc
);
209 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
210 * @desc: GPIO descriptor
212 * This should disappear in the future but is needed since we still
213 * use GPIO numbers for error messages and sysfs nodes.
216 * The global GPIO number for the GPIO specified by its descriptor.
218 int desc_to_gpio(const struct gpio_desc
*desc
)
220 return desc
->gdev
->base
+ (desc
- &desc
->gdev
->descs
[0]);
222 EXPORT_SYMBOL_GPL(desc_to_gpio
);
226 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
227 * @desc: descriptor to return the chip of
230 * This function is unsafe and should not be used. Using the chip address
231 * without taking the SRCU read lock may result in dereferencing a dangling
235 * Address of the GPIO chip backing this device.
237 struct gpio_chip
*gpiod_to_chip(const struct gpio_desc
*desc
)
242 return gpio_device_get_chip(desc
->gdev
);
244 EXPORT_SYMBOL_GPL(gpiod_to_chip
);
247 * gpiod_to_gpio_device() - Return the GPIO device to which this descriptor
249 * @desc: Descriptor for which to return the GPIO device.
251 * This *DOES NOT* increase the reference count of the GPIO device as it's
252 * expected that the descriptor is requested and the users already holds a
253 * reference to the device.
256 * Address of the GPIO device owning this descriptor.
258 struct gpio_device
*gpiod_to_gpio_device(struct gpio_desc
*desc
)
265 EXPORT_SYMBOL_GPL(gpiod_to_gpio_device
);
268 * gpio_device_get_base() - Get the base GPIO number allocated by this device
272 * First GPIO number in the global GPIO numberspace for this device.
274 int gpio_device_get_base(struct gpio_device
*gdev
)
278 EXPORT_SYMBOL_GPL(gpio_device_get_base
);
281 * gpio_device_get_label() - Get the label of this GPIO device
285 * Pointer to the string containing the GPIO device label. The string's
286 * lifetime is tied to that of the underlying GPIO device.
288 const char *gpio_device_get_label(struct gpio_device
*gdev
)
292 EXPORT_SYMBOL(gpio_device_get_label
);
295 * gpio_device_get_chip() - Get the gpio_chip implementation of this GPIO device
299 * Address of the GPIO chip backing this device.
302 * Until we can get rid of all non-driver users of struct gpio_chip, we must
303 * provide a way of retrieving the pointer to it from struct gpio_device. This
304 * is *NOT* safe as the GPIO API is considered to be hot-unpluggable and the
305 * chip can dissapear at any moment (unlike reference-counted struct
308 * Use at your own risk.
310 struct gpio_chip
*gpio_device_get_chip(struct gpio_device
*gdev
)
312 return rcu_dereference_check(gdev
->chip
, 1);
314 EXPORT_SYMBOL_GPL(gpio_device_get_chip
);
316 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
317 static int gpiochip_find_base_unlocked(u16 ngpio
)
319 unsigned int base
= GPIO_DYNAMIC_BASE
;
320 struct gpio_device
*gdev
;
322 list_for_each_entry_srcu(gdev
, &gpio_devices
, list
,
323 lockdep_is_held(&gpio_devices_lock
)) {
324 /* found a free space? */
325 if (gdev
->base
>= base
+ ngpio
)
327 /* nope, check the space right after the chip */
328 base
= gdev
->base
+ gdev
->ngpio
;
329 if (base
< GPIO_DYNAMIC_BASE
)
330 base
= GPIO_DYNAMIC_BASE
;
331 if (base
> GPIO_DYNAMIC_MAX
- ngpio
)
335 if (base
<= GPIO_DYNAMIC_MAX
- ngpio
) {
336 pr_debug("%s: found new base at %d\n", __func__
, base
);
339 pr_err("%s: cannot find free range\n", __func__
);
345 * gpiod_get_direction - return the current direction of a GPIO
346 * @desc: GPIO to get the direction of
349 * 0 for output, 1 for input, or an error code in case of error.
351 * This function may sleep if gpiod_cansleep() is true.
353 int gpiod_get_direction(struct gpio_desc
*desc
)
360 * We cannot use VALIDATE_DESC() as we must not return 0 for a NULL
361 * descriptor like we usually do.
363 if (IS_ERR_OR_NULL(desc
))
366 CLASS(gpio_chip_guard
, guard
)(desc
);
370 offset
= gpio_chip_hwgpio(desc
);
371 flags
= READ_ONCE(desc
->flags
);
374 * Open drain emulation using input mode may incorrectly report
375 * input here, fix that up.
377 if (test_bit(FLAG_OPEN_DRAIN
, &flags
) &&
378 test_bit(FLAG_IS_OUT
, &flags
))
381 if (!guard
.gc
->get_direction
)
384 ret
= guard
.gc
->get_direction(guard
.gc
, offset
);
389 * GPIO_LINE_DIRECTION_IN or other positive,
390 * otherwise GPIO_LINE_DIRECTION_OUT.
395 assign_bit(FLAG_IS_OUT
, &flags
, !ret
);
396 WRITE_ONCE(desc
->flags
, flags
);
400 EXPORT_SYMBOL_GPL(gpiod_get_direction
);
403 * Add a new chip to the global chips list, keeping the list of chips sorted
404 * by range(means [base, base + ngpio - 1]) order.
407 * -EBUSY if the new chip overlaps with some other chip's integer space.
409 static int gpiodev_add_to_list_unlocked(struct gpio_device
*gdev
)
411 struct gpio_device
*prev
, *next
;
413 lockdep_assert_held(&gpio_devices_lock
);
415 if (list_empty(&gpio_devices
)) {
416 /* initial entry in list */
417 list_add_tail_rcu(&gdev
->list
, &gpio_devices
);
421 next
= list_first_entry(&gpio_devices
, struct gpio_device
, list
);
422 if (gdev
->base
+ gdev
->ngpio
<= next
->base
) {
423 /* add before first entry */
424 list_add_rcu(&gdev
->list
, &gpio_devices
);
428 prev
= list_last_entry(&gpio_devices
, struct gpio_device
, list
);
429 if (prev
->base
+ prev
->ngpio
<= gdev
->base
) {
430 /* add behind last entry */
431 list_add_tail_rcu(&gdev
->list
, &gpio_devices
);
435 list_for_each_entry_safe(prev
, next
, &gpio_devices
, list
) {
436 /* at the end of the list */
437 if (&next
->list
== &gpio_devices
)
440 /* add between prev and next */
441 if (prev
->base
+ prev
->ngpio
<= gdev
->base
442 && gdev
->base
+ gdev
->ngpio
<= next
->base
) {
443 list_add_rcu(&gdev
->list
, &prev
->list
);
448 synchronize_srcu(&gpio_devices_srcu
);
454 * Convert a GPIO name to its descriptor
455 * Note that there is no guarantee that GPIO names are globally unique!
456 * Hence this function will return, if it exists, a reference to the first GPIO
457 * line found that matches the given name.
459 static struct gpio_desc
*gpio_name_to_desc(const char * const name
)
461 struct gpio_device
*gdev
;
462 struct gpio_desc
*desc
;
463 struct gpio_chip
*gc
;
468 guard(srcu
)(&gpio_devices_srcu
);
470 list_for_each_entry_srcu(gdev
, &gpio_devices
, list
,
471 srcu_read_lock_held(&gpio_devices_srcu
)) {
472 guard(srcu
)(&gdev
->srcu
);
474 gc
= srcu_dereference(gdev
->chip
, &gdev
->srcu
);
478 for_each_gpio_desc(gc
, desc
) {
479 if (desc
->name
&& !strcmp(desc
->name
, name
))
488 * Take the names from gc->names and assign them to their GPIO descriptors.
489 * Warn if a name is already used for a GPIO line on a different GPIO chip.
492 * 1. Non-unique names are still accepted,
493 * 2. Name collisions within the same GPIO chip are not reported.
495 static void gpiochip_set_desc_names(struct gpio_chip
*gc
)
497 struct gpio_device
*gdev
= gc
->gpiodev
;
500 /* First check all names if they are unique */
501 for (i
= 0; i
!= gc
->ngpio
; ++i
) {
502 struct gpio_desc
*gpio
;
504 gpio
= gpio_name_to_desc(gc
->names
[i
]);
507 "Detected name collision for GPIO name '%s'\n",
511 /* Then add all names to the GPIO descriptors */
512 for (i
= 0; i
!= gc
->ngpio
; ++i
)
513 gdev
->descs
[i
].name
= gc
->names
[i
];
517 * gpiochip_set_names - Set GPIO line names using device properties
518 * @chip: GPIO chip whose lines should be named, if possible
520 * Looks for device property "gpio-line-names" and if it exists assigns
521 * GPIO line names for the chip. The memory allocated for the assigned
522 * names belong to the underlying firmware node and should not be released
525 static int gpiochip_set_names(struct gpio_chip
*chip
)
527 struct gpio_device
*gdev
= chip
->gpiodev
;
528 struct device
*dev
= &gdev
->dev
;
533 count
= device_property_string_array_count(dev
, "gpio-line-names");
538 * When offset is set in the driver side we assume the driver internally
539 * is using more than one gpiochip per the same device. We have to stop
540 * setting friendly names if the specified ones with 'gpio-line-names'
541 * are less than the offset in the device itself. This means all the
542 * lines are not present for every single pin within all the internal
545 if (count
<= chip
->offset
) {
546 dev_warn(dev
, "gpio-line-names too short (length %d), cannot map names for the gpiochip at offset %u\n",
547 count
, chip
->offset
);
551 names
= kcalloc(count
, sizeof(*names
), GFP_KERNEL
);
555 ret
= device_property_read_string_array(dev
, "gpio-line-names",
558 dev_warn(dev
, "failed to read GPIO line names\n");
564 * When more that one gpiochip per device is used, 'count' can
565 * contain at most number gpiochips x chip->ngpio. We have to
566 * correctly distribute all defined lines taking into account
567 * chip->offset as starting point from where we will assign
568 * the names to pins from the 'names' array. Since property
569 * 'gpio-line-names' cannot contains gaps, we have to be sure
570 * we only assign those pins that really exists since chip->ngpio
571 * can be different of the chip->offset.
573 count
= (count
> chip
->offset
) ? count
- chip
->offset
: count
;
574 if (count
> chip
->ngpio
)
577 for (i
= 0; i
< count
; i
++) {
579 * Allow overriding "fixed" names provided by the GPIO
580 * provider. The "fixed" names are more often than not
581 * generic and less informative than the names given in
584 if (names
[chip
->offset
+ i
] && names
[chip
->offset
+ i
][0])
585 gdev
->descs
[i
].name
= names
[chip
->offset
+ i
];
593 static unsigned long *gpiochip_allocate_mask(struct gpio_chip
*gc
)
597 p
= bitmap_alloc(gc
->ngpio
, GFP_KERNEL
);
601 /* Assume by default all GPIOs are valid */
602 bitmap_fill(p
, gc
->ngpio
);
607 static void gpiochip_free_mask(unsigned long **p
)
613 static unsigned int gpiochip_count_reserved_ranges(struct gpio_chip
*gc
)
615 struct device
*dev
= &gc
->gpiodev
->dev
;
618 /* Format is "start, count, ..." */
619 size
= device_property_count_u32(dev
, "gpio-reserved-ranges");
620 if (size
> 0 && size
% 2 == 0)
626 static int gpiochip_apply_reserved_ranges(struct gpio_chip
*gc
)
628 struct device
*dev
= &gc
->gpiodev
->dev
;
633 size
= gpiochip_count_reserved_ranges(gc
);
637 ranges
= kmalloc_array(size
, sizeof(*ranges
), GFP_KERNEL
);
641 ret
= device_property_read_u32_array(dev
, "gpio-reserved-ranges",
649 u32 count
= ranges
[--size
];
650 u32 start
= ranges
[--size
];
652 if (start
>= gc
->ngpio
|| start
+ count
> gc
->ngpio
)
655 bitmap_clear(gc
->valid_mask
, start
, count
);
662 static int gpiochip_init_valid_mask(struct gpio_chip
*gc
)
666 if (!(gpiochip_count_reserved_ranges(gc
) || gc
->init_valid_mask
))
669 gc
->valid_mask
= gpiochip_allocate_mask(gc
);
673 ret
= gpiochip_apply_reserved_ranges(gc
);
677 if (gc
->init_valid_mask
)
678 return gc
->init_valid_mask(gc
,
685 static void gpiochip_free_valid_mask(struct gpio_chip
*gc
)
687 gpiochip_free_mask(&gc
->valid_mask
);
690 static int gpiochip_add_pin_ranges(struct gpio_chip
*gc
)
693 * Device Tree platforms are supposed to use "gpio-ranges"
694 * property. This check ensures that the ->add_pin_ranges()
695 * won't be called for them.
697 if (device_property_present(&gc
->gpiodev
->dev
, "gpio-ranges"))
700 if (gc
->add_pin_ranges
)
701 return gc
->add_pin_ranges(gc
);
706 bool gpiochip_line_is_valid(const struct gpio_chip
*gc
,
709 /* No mask means all valid */
710 if (likely(!gc
->valid_mask
))
712 return test_bit(offset
, gc
->valid_mask
);
714 EXPORT_SYMBOL_GPL(gpiochip_line_is_valid
);
716 static void gpiodev_release(struct device
*dev
)
718 struct gpio_device
*gdev
= to_gpio_device(dev
);
720 /* Call pending kfree()s for descriptor labels. */
721 synchronize_srcu(&gdev
->desc_srcu
);
722 cleanup_srcu_struct(&gdev
->desc_srcu
);
724 ida_free(&gpio_ida
, gdev
->id
);
725 kfree_const(gdev
->label
);
727 cleanup_srcu_struct(&gdev
->srcu
);
731 static const struct device_type gpio_dev_type
= {
733 .release
= gpiodev_release
,
736 #ifdef CONFIG_GPIO_CDEV
737 #define gcdev_register(gdev, devt) gpiolib_cdev_register((gdev), (devt))
738 #define gcdev_unregister(gdev) gpiolib_cdev_unregister((gdev))
741 * gpiolib_cdev_register() indirectly calls device_add(), which is still
742 * required even when cdev is not selected.
744 #define gcdev_register(gdev, devt) device_add(&(gdev)->dev)
745 #define gcdev_unregister(gdev) device_del(&(gdev)->dev)
748 static int gpiochip_setup_dev(struct gpio_device
*gdev
)
750 struct fwnode_handle
*fwnode
= dev_fwnode(&gdev
->dev
);
753 device_initialize(&gdev
->dev
);
756 * If fwnode doesn't belong to another device, it's safe to clear its
759 if (fwnode
&& !fwnode
->dev
)
760 fwnode_dev_initialized(fwnode
, false);
762 ret
= gcdev_register(gdev
, gpio_devt
);
766 ret
= gpiochip_sysfs_register(gdev
);
768 goto err_remove_device
;
770 dev_dbg(&gdev
->dev
, "registered GPIOs %u to %u on %s\n", gdev
->base
,
771 gdev
->base
+ gdev
->ngpio
- 1, gdev
->label
);
776 gcdev_unregister(gdev
);
780 static void gpiochip_machine_hog(struct gpio_chip
*gc
, struct gpiod_hog
*hog
)
782 struct gpio_desc
*desc
;
785 desc
= gpiochip_get_desc(gc
, hog
->chip_hwnum
);
787 chip_err(gc
, "%s: unable to get GPIO desc: %ld\n", __func__
,
792 rv
= gpiod_hog(desc
, hog
->line_name
, hog
->lflags
, hog
->dflags
);
794 gpiod_err(desc
, "%s: unable to hog GPIO line (%s:%u): %d\n",
795 __func__
, gc
->label
, hog
->chip_hwnum
, rv
);
798 static void machine_gpiochip_add(struct gpio_chip
*gc
)
800 struct gpiod_hog
*hog
;
802 mutex_lock(&gpio_machine_hogs_mutex
);
804 list_for_each_entry(hog
, &gpio_machine_hogs
, list
) {
805 if (!strcmp(gc
->label
, hog
->chip_label
))
806 gpiochip_machine_hog(gc
, hog
);
809 mutex_unlock(&gpio_machine_hogs_mutex
);
812 static void gpiochip_setup_devs(void)
814 struct gpio_device
*gdev
;
817 guard(srcu
)(&gpio_devices_srcu
);
819 list_for_each_entry_srcu(gdev
, &gpio_devices
, list
,
820 srcu_read_lock_held(&gpio_devices_srcu
)) {
821 ret
= gpiochip_setup_dev(gdev
);
824 "Failed to initialize gpio device (%d)\n", ret
);
828 static void gpiochip_set_data(struct gpio_chip
*gc
, void *data
)
830 gc
->gpiodev
->data
= data
;
834 * gpiochip_get_data() - get per-subdriver data for the chip
838 * The per-subdriver data for the chip.
840 void *gpiochip_get_data(struct gpio_chip
*gc
)
842 return gc
->gpiodev
->data
;
844 EXPORT_SYMBOL_GPL(gpiochip_get_data
);
846 int gpiochip_get_ngpios(struct gpio_chip
*gc
, struct device
*dev
)
848 u32 ngpios
= gc
->ngpio
;
852 ret
= device_property_read_u32(dev
, "ngpios", &ngpios
);
855 * -ENODATA means that there is no property found and
856 * we want to issue the error message to the user.
857 * Besides that, we want to return different error code
858 * to state that supplied value is not valid.
867 if (gc
->ngpio
== 0) {
868 chip_err(gc
, "tried to insert a GPIO chip with zero lines\n");
872 if (gc
->ngpio
> FASTPATH_NGPIO
)
873 chip_warn(gc
, "line cnt %u is greater than fast path cnt %u\n",
874 gc
->ngpio
, FASTPATH_NGPIO
);
878 EXPORT_SYMBOL_GPL(gpiochip_get_ngpios
);
880 int gpiochip_add_data_with_key(struct gpio_chip
*gc
, void *data
,
881 struct lock_class_key
*lock_key
,
882 struct lock_class_key
*request_key
)
884 struct gpio_device
*gdev
;
885 unsigned int desc_index
;
890 * First: allocate and populate the internal stat container, and
891 * set up the struct device.
893 gdev
= kzalloc(sizeof(*gdev
), GFP_KERNEL
);
897 gdev
->dev
.type
= &gpio_dev_type
;
898 gdev
->dev
.bus
= &gpio_bus_type
;
899 gdev
->dev
.parent
= gc
->parent
;
900 rcu_assign_pointer(gdev
->chip
, gc
);
903 gpiochip_set_data(gc
, data
);
906 * If the calling driver did not initialize firmware node,
907 * do it here using the parent device, if any.
910 device_set_node(&gdev
->dev
, gc
->fwnode
);
912 device_set_node(&gdev
->dev
, dev_fwnode(gc
->parent
));
914 gdev
->id
= ida_alloc(&gpio_ida
, GFP_KERNEL
);
920 ret
= dev_set_name(&gdev
->dev
, GPIOCHIP_NAME
"%d", gdev
->id
);
924 if (gc
->parent
&& gc
->parent
->driver
)
925 gdev
->owner
= gc
->parent
->driver
->owner
;
927 /* TODO: remove chip->owner */
928 gdev
->owner
= gc
->owner
;
930 gdev
->owner
= THIS_MODULE
;
932 ret
= gpiochip_get_ngpios(gc
, &gdev
->dev
);
934 goto err_free_dev_name
;
936 gdev
->descs
= kcalloc(gc
->ngpio
, sizeof(*gdev
->descs
), GFP_KERNEL
);
939 goto err_free_dev_name
;
942 gdev
->label
= kstrdup_const(gc
->label
?: "unknown", GFP_KERNEL
);
948 gdev
->ngpio
= gc
->ngpio
;
949 gdev
->can_sleep
= gc
->can_sleep
;
951 scoped_guard(mutex
, &gpio_devices_lock
) {
953 * TODO: this allocates a Linux GPIO number base in the global
954 * GPIO numberspace for this chip. In the long run we want to
955 * get *rid* of this numberspace and use only descriptors, but
956 * it may be a pipe dream. It will not happen before we get rid
957 * of the sysfs interface anyways.
961 base
= gpiochip_find_base_unlocked(gc
->ngpio
);
969 * TODO: it should not be necessary to reflect the
970 * assigned base outside of the GPIO subsystem. Go over
971 * drivers and see if anyone makes use of this, else
972 * drop this and assign a poison instead.
977 "Static allocation of GPIO base is deprecated, use dynamic allocation.\n");
982 ret
= gpiodev_add_to_list_unlocked(gdev
);
984 chip_err(gc
, "GPIO integer space overlap, cannot add chip\n");
989 for (desc_index
= 0; desc_index
< gc
->ngpio
; desc_index
++)
990 gdev
->descs
[desc_index
].gdev
= gdev
;
992 BLOCKING_INIT_NOTIFIER_HEAD(&gdev
->line_state_notifier
);
993 BLOCKING_INIT_NOTIFIER_HEAD(&gdev
->device_notifier
);
995 ret
= init_srcu_struct(&gdev
->srcu
);
997 goto err_remove_from_list
;
999 ret
= init_srcu_struct(&gdev
->desc_srcu
);
1001 goto err_cleanup_gdev_srcu
;
1003 #ifdef CONFIG_PINCTRL
1004 INIT_LIST_HEAD(&gdev
->pin_ranges
);
1008 gpiochip_set_desc_names(gc
);
1010 ret
= gpiochip_set_names(gc
);
1012 goto err_cleanup_desc_srcu
;
1014 ret
= gpiochip_init_valid_mask(gc
);
1016 goto err_cleanup_desc_srcu
;
1018 for (desc_index
= 0; desc_index
< gc
->ngpio
; desc_index
++) {
1019 struct gpio_desc
*desc
= &gdev
->descs
[desc_index
];
1021 if (gc
->get_direction
&& gpiochip_line_is_valid(gc
, desc_index
)) {
1022 assign_bit(FLAG_IS_OUT
,
1023 &desc
->flags
, !gc
->get_direction(gc
, desc_index
));
1025 assign_bit(FLAG_IS_OUT
,
1026 &desc
->flags
, !gc
->direction_input
);
1030 ret
= of_gpiochip_add(gc
);
1032 goto err_free_valid_mask
;
1034 ret
= gpiochip_add_pin_ranges(gc
);
1036 goto err_remove_of_chip
;
1038 acpi_gpiochip_add(gc
);
1040 machine_gpiochip_add(gc
);
1042 ret
= gpiochip_irqchip_init_valid_mask(gc
);
1046 ret
= gpiochip_irqchip_init_hw(gc
);
1048 goto err_remove_irqchip_mask
;
1050 ret
= gpiochip_add_irqchip(gc
, lock_key
, request_key
);
1052 goto err_remove_irqchip_mask
;
1055 * By first adding the chardev, and then adding the device,
1056 * we get a device node entry in sysfs under
1057 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1058 * coldplug of device nodes and other udev business.
1059 * We can do this only if gpiolib has been initialized.
1060 * Otherwise, defer until later.
1062 if (gpiolib_initialized
) {
1063 ret
= gpiochip_setup_dev(gdev
);
1065 goto err_remove_irqchip
;
1070 gpiochip_irqchip_remove(gc
);
1071 err_remove_irqchip_mask
:
1072 gpiochip_irqchip_free_valid_mask(gc
);
1074 gpiochip_free_hogs(gc
);
1075 acpi_gpiochip_remove(gc
);
1076 gpiochip_remove_pin_ranges(gc
);
1078 of_gpiochip_remove(gc
);
1079 err_free_valid_mask
:
1080 gpiochip_free_valid_mask(gc
);
1081 err_cleanup_desc_srcu
:
1082 cleanup_srcu_struct(&gdev
->desc_srcu
);
1083 err_cleanup_gdev_srcu
:
1084 cleanup_srcu_struct(&gdev
->srcu
);
1085 err_remove_from_list
:
1086 scoped_guard(mutex
, &gpio_devices_lock
)
1087 list_del_rcu(&gdev
->list
);
1088 synchronize_srcu(&gpio_devices_srcu
);
1089 if (gdev
->dev
.release
) {
1090 /* release() has been registered by gpiochip_setup_dev() */
1091 gpio_device_put(gdev
);
1092 goto err_print_message
;
1095 kfree_const(gdev
->label
);
1099 kfree(dev_name(&gdev
->dev
));
1101 ida_free(&gpio_ida
, gdev
->id
);
1105 /* failures here can mean systems won't boot... */
1106 if (ret
!= -EPROBE_DEFER
) {
1107 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__
,
1108 base
, base
+ (int)gc
->ngpio
- 1,
1109 gc
->label
? : "generic", ret
);
1113 EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key
);
1116 * gpiochip_remove() - unregister a gpio_chip
1117 * @gc: the chip to unregister
1119 * A gpio_chip with any GPIOs still requested may not be removed.
1121 void gpiochip_remove(struct gpio_chip
*gc
)
1123 struct gpio_device
*gdev
= gc
->gpiodev
;
1125 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
1126 gpiochip_sysfs_unregister(gdev
);
1127 gpiochip_free_hogs(gc
);
1129 scoped_guard(mutex
, &gpio_devices_lock
)
1130 list_del_rcu(&gdev
->list
);
1131 synchronize_srcu(&gpio_devices_srcu
);
1133 /* Numb the device, cancelling all outstanding operations */
1134 rcu_assign_pointer(gdev
->chip
, NULL
);
1135 synchronize_srcu(&gdev
->srcu
);
1136 gpiochip_irqchip_remove(gc
);
1137 acpi_gpiochip_remove(gc
);
1138 of_gpiochip_remove(gc
);
1139 gpiochip_remove_pin_ranges(gc
);
1140 gpiochip_free_valid_mask(gc
);
1142 * We accept no more calls into the driver from this point, so
1143 * NULL the driver data pointer.
1145 gpiochip_set_data(gc
, NULL
);
1148 * The gpiochip side puts its use of the device to rest here:
1149 * if there are no userspace clients, the chardev and device will
1150 * be removed, else it will be dangling until the last user is
1153 gcdev_unregister(gdev
);
1154 gpio_device_put(gdev
);
1156 EXPORT_SYMBOL_GPL(gpiochip_remove
);
1159 * gpio_device_find() - find a specific GPIO device
1160 * @data: data to pass to match function
1161 * @match: Callback function to check gpio_chip
1164 * New reference to struct gpio_device.
1166 * Similar to bus_find_device(). It returns a reference to a gpio_device as
1167 * determined by a user supplied @match callback. The callback should return
1168 * 0 if the device doesn't match and non-zero if it does. If the callback
1169 * returns non-zero, this function will return to the caller and not iterate
1170 * over any more gpio_devices.
1172 * The callback takes the GPIO chip structure as argument. During the execution
1173 * of the callback function the chip is protected from being freed. TODO: This
1174 * actually has yet to be implemented.
1176 * If the function returns non-NULL, the returned reference must be freed by
1177 * the caller using gpio_device_put().
1179 struct gpio_device
*gpio_device_find(const void *data
,
1180 int (*match
)(struct gpio_chip
*gc
,
1183 struct gpio_device
*gdev
;
1184 struct gpio_chip
*gc
;
1187 * Not yet but in the future the spinlock below will become a mutex.
1188 * Annotate this function before anyone tries to use it in interrupt
1189 * context like it happened with gpiochip_find().
1193 guard(srcu
)(&gpio_devices_srcu
);
1195 list_for_each_entry_srcu(gdev
, &gpio_devices
, list
,
1196 srcu_read_lock_held(&gpio_devices_srcu
)) {
1197 if (!device_is_registered(&gdev
->dev
))
1200 guard(srcu
)(&gdev
->srcu
);
1202 gc
= srcu_dereference(gdev
->chip
, &gdev
->srcu
);
1204 if (gc
&& match(gc
, data
))
1205 return gpio_device_get(gdev
);
1210 EXPORT_SYMBOL_GPL(gpio_device_find
);
1212 static int gpio_chip_match_by_label(struct gpio_chip
*gc
, const void *label
)
1214 return gc
->label
&& !strcmp(gc
->label
, label
);
1218 * gpio_device_find_by_label() - wrapper around gpio_device_find() finding the
1219 * GPIO device by its backing chip's label
1220 * @label: Label to lookup
1223 * Reference to the GPIO device or NULL. Reference must be released with
1224 * gpio_device_put().
1226 struct gpio_device
*gpio_device_find_by_label(const char *label
)
1228 return gpio_device_find((void *)label
, gpio_chip_match_by_label
);
1230 EXPORT_SYMBOL_GPL(gpio_device_find_by_label
);
1232 static int gpio_chip_match_by_fwnode(struct gpio_chip
*gc
, const void *fwnode
)
1234 return device_match_fwnode(&gc
->gpiodev
->dev
, fwnode
);
1238 * gpio_device_find_by_fwnode() - wrapper around gpio_device_find() finding
1239 * the GPIO device by its fwnode
1240 * @fwnode: Firmware node to lookup
1243 * Reference to the GPIO device or NULL. Reference must be released with
1244 * gpio_device_put().
1246 struct gpio_device
*gpio_device_find_by_fwnode(const struct fwnode_handle
*fwnode
)
1248 return gpio_device_find((void *)fwnode
, gpio_chip_match_by_fwnode
);
1250 EXPORT_SYMBOL_GPL(gpio_device_find_by_fwnode
);
1253 * gpio_device_get() - Increase the reference count of this GPIO device
1254 * @gdev: GPIO device to increase the refcount for
1259 struct gpio_device
*gpio_device_get(struct gpio_device
*gdev
)
1261 return to_gpio_device(get_device(&gdev
->dev
));
1263 EXPORT_SYMBOL_GPL(gpio_device_get
);
1266 * gpio_device_put() - Decrease the reference count of this GPIO device and
1267 * possibly free all resources associated with it.
1268 * @gdev: GPIO device to decrease the reference count for
1270 void gpio_device_put(struct gpio_device
*gdev
)
1272 put_device(&gdev
->dev
);
1274 EXPORT_SYMBOL_GPL(gpio_device_put
);
1277 * gpio_device_to_device() - Retrieve the address of the underlying struct
1279 * @gdev: GPIO device for which to return the address.
1281 * This does not increase the reference count of the GPIO device nor the
1282 * underlying struct device.
1285 * Address of struct device backing this GPIO device.
1287 struct device
*gpio_device_to_device(struct gpio_device
*gdev
)
1291 EXPORT_SYMBOL_GPL(gpio_device_to_device
);
1293 #ifdef CONFIG_GPIOLIB_IRQCHIP
1296 * The following is irqchip helper code for gpiochips.
1299 static int gpiochip_irqchip_init_hw(struct gpio_chip
*gc
)
1301 struct gpio_irq_chip
*girq
= &gc
->irq
;
1306 return girq
->init_hw(gc
);
1309 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip
*gc
)
1311 struct gpio_irq_chip
*girq
= &gc
->irq
;
1313 if (!girq
->init_valid_mask
)
1316 girq
->valid_mask
= gpiochip_allocate_mask(gc
);
1317 if (!girq
->valid_mask
)
1320 girq
->init_valid_mask(gc
, girq
->valid_mask
, gc
->ngpio
);
1325 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip
*gc
)
1327 gpiochip_free_mask(&gc
->irq
.valid_mask
);
1330 static bool gpiochip_irqchip_irq_valid(const struct gpio_chip
*gc
,
1331 unsigned int offset
)
1333 if (!gpiochip_line_is_valid(gc
, offset
))
1335 /* No mask means all valid */
1336 if (likely(!gc
->irq
.valid_mask
))
1338 return test_bit(offset
, gc
->irq
.valid_mask
);
1341 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1344 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
1346 * @gc: the gpiochip to set the irqchip hierarchical handler to
1347 * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
1348 * will then percolate up to the parent
1350 static void gpiochip_set_hierarchical_irqchip(struct gpio_chip
*gc
,
1351 struct irq_chip
*irqchip
)
1353 /* DT will deal with mapping each IRQ as we go along */
1354 if (is_of_node(gc
->irq
.fwnode
))
1358 * This is for legacy and boardfile "irqchip" fwnodes: allocate
1359 * irqs upfront instead of dynamically since we don't have the
1360 * dynamic type of allocation that hardware description languages
1361 * provide. Once all GPIO drivers using board files are gone from
1362 * the kernel we can delete this code, but for a transitional period
1363 * it is necessary to keep this around.
1365 if (is_fwnode_irqchip(gc
->irq
.fwnode
)) {
1369 for (i
= 0; i
< gc
->ngpio
; i
++) {
1370 struct irq_fwspec fwspec
;
1371 unsigned int parent_hwirq
;
1372 unsigned int parent_type
;
1373 struct gpio_irq_chip
*girq
= &gc
->irq
;
1376 * We call the child to parent translation function
1377 * only to check if the child IRQ is valid or not.
1378 * Just pick the rising edge type here as that is what
1379 * we likely need to support.
1381 ret
= girq
->child_to_parent_hwirq(gc
, i
,
1382 IRQ_TYPE_EDGE_RISING
,
1386 chip_err(gc
, "skip set-up on hwirq %d\n",
1391 fwspec
.fwnode
= gc
->irq
.fwnode
;
1392 /* This is the hwirq for the GPIO line side of things */
1393 fwspec
.param
[0] = girq
->child_offset_to_irq(gc
, i
);
1394 /* Just pick something */
1395 fwspec
.param
[1] = IRQ_TYPE_EDGE_RISING
;
1396 fwspec
.param_count
= 2;
1397 ret
= irq_domain_alloc_irqs(gc
->irq
.domain
, 1,
1398 NUMA_NO_NODE
, &fwspec
);
1401 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
1408 chip_err(gc
, "%s unknown fwnode type proceed anyway\n", __func__
);
1413 static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain
*d
,
1414 struct irq_fwspec
*fwspec
,
1415 unsigned long *hwirq
,
1418 /* We support standard DT translation */
1419 if (is_of_node(fwspec
->fwnode
) && fwspec
->param_count
== 2) {
1420 return irq_domain_translate_twocell(d
, fwspec
, hwirq
, type
);
1423 /* This is for board files and others not using DT */
1424 if (is_fwnode_irqchip(fwspec
->fwnode
)) {
1427 ret
= irq_domain_translate_twocell(d
, fwspec
, hwirq
, type
);
1430 WARN_ON(*type
== IRQ_TYPE_NONE
);
1436 static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain
*d
,
1438 unsigned int nr_irqs
,
1441 struct gpio_chip
*gc
= d
->host_data
;
1442 irq_hw_number_t hwirq
;
1443 unsigned int type
= IRQ_TYPE_NONE
;
1444 struct irq_fwspec
*fwspec
= data
;
1445 union gpio_irq_fwspec gpio_parent_fwspec
= {};
1446 unsigned int parent_hwirq
;
1447 unsigned int parent_type
;
1448 struct gpio_irq_chip
*girq
= &gc
->irq
;
1452 * The nr_irqs parameter is always one except for PCI multi-MSI
1453 * so this should not happen.
1455 WARN_ON(nr_irqs
!= 1);
1457 ret
= gc
->irq
.child_irq_domain_ops
.translate(d
, fwspec
, &hwirq
, &type
);
1461 chip_dbg(gc
, "allocate IRQ %d, hwirq %lu\n", irq
, hwirq
);
1463 ret
= girq
->child_to_parent_hwirq(gc
, hwirq
, type
,
1464 &parent_hwirq
, &parent_type
);
1466 chip_err(gc
, "can't look up hwirq %lu\n", hwirq
);
1469 chip_dbg(gc
, "found parent hwirq %u\n", parent_hwirq
);
1472 * We set handle_bad_irq because the .set_type() should
1473 * always be invoked and set the right type of handler.
1475 irq_domain_set_info(d
,
1484 /* This parent only handles asserted level IRQs */
1485 ret
= girq
->populate_parent_alloc_arg(gc
, &gpio_parent_fwspec
,
1486 parent_hwirq
, parent_type
);
1490 chip_dbg(gc
, "alloc_irqs_parent for %d parent hwirq %d\n",
1492 irq_set_lockdep_class(irq
, gc
->irq
.lock_key
, gc
->irq
.request_key
);
1493 ret
= irq_domain_alloc_irqs_parent(d
, irq
, 1, &gpio_parent_fwspec
);
1495 * If the parent irqdomain is msi, the interrupts have already
1496 * been allocated, so the EEXIST is good.
1498 if (irq_domain_is_msi(d
->parent
) && (ret
== -EEXIST
))
1502 "failed to allocate parent hwirq %d for hwirq %lu\n",
1503 parent_hwirq
, hwirq
);
1508 static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip
*gc
,
1509 unsigned int offset
)
1515 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
1516 * @domain: The IRQ domain used by this IRQ chip
1517 * @data: Outermost irq_data associated with the IRQ
1518 * @reserve: If set, only reserve an interrupt vector instead of assigning one
1520 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
1521 * used as the activate function for the &struct irq_domain_ops. The host_data
1522 * for the IRQ domain must be the &struct gpio_chip.
1525 * 0 on success, or negative errno on failure.
1527 static int gpiochip_irq_domain_activate(struct irq_domain
*domain
,
1528 struct irq_data
*data
, bool reserve
)
1530 struct gpio_chip
*gc
= domain
->host_data
;
1531 unsigned int hwirq
= irqd_to_hwirq(data
);
1533 return gpiochip_lock_as_irq(gc
, hwirq
);
1537 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
1538 * @domain: The IRQ domain used by this IRQ chip
1539 * @data: Outermost irq_data associated with the IRQ
1541 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
1542 * be used as the deactivate function for the &struct irq_domain_ops. The
1543 * host_data for the IRQ domain must be the &struct gpio_chip.
1545 static void gpiochip_irq_domain_deactivate(struct irq_domain
*domain
,
1546 struct irq_data
*data
)
1548 struct gpio_chip
*gc
= domain
->host_data
;
1549 unsigned int hwirq
= irqd_to_hwirq(data
);
1551 return gpiochip_unlock_as_irq(gc
, hwirq
);
1554 static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops
*ops
)
1556 ops
->activate
= gpiochip_irq_domain_activate
;
1557 ops
->deactivate
= gpiochip_irq_domain_deactivate
;
1558 ops
->alloc
= gpiochip_hierarchy_irq_domain_alloc
;
1561 * We only allow overriding the translate() and free() functions for
1562 * hierarchical chips, and this should only be done if the user
1563 * really need something other than 1:1 translation for translate()
1564 * callback and free if user wants to free up any resources which
1565 * were allocated during callbacks, for example populate_parent_alloc_arg.
1567 if (!ops
->translate
)
1568 ops
->translate
= gpiochip_hierarchy_irq_domain_translate
;
1570 ops
->free
= irq_domain_free_irqs_common
;
1573 static struct irq_domain
*gpiochip_hierarchy_create_domain(struct gpio_chip
*gc
)
1575 struct irq_domain
*domain
;
1577 if (!gc
->irq
.child_to_parent_hwirq
||
1579 chip_err(gc
, "missing irqdomain vital data\n");
1580 return ERR_PTR(-EINVAL
);
1583 if (!gc
->irq
.child_offset_to_irq
)
1584 gc
->irq
.child_offset_to_irq
= gpiochip_child_offset_to_irq_noop
;
1586 if (!gc
->irq
.populate_parent_alloc_arg
)
1587 gc
->irq
.populate_parent_alloc_arg
=
1588 gpiochip_populate_parent_fwspec_twocell
;
1590 gpiochip_hierarchy_setup_domain_ops(&gc
->irq
.child_irq_domain_ops
);
1592 domain
= irq_domain_create_hierarchy(
1593 gc
->irq
.parent_domain
,
1597 &gc
->irq
.child_irq_domain_ops
,
1601 return ERR_PTR(-ENOMEM
);
1603 gpiochip_set_hierarchical_irqchip(gc
, gc
->irq
.chip
);
1608 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip
*gc
)
1610 return !!gc
->irq
.parent_domain
;
1613 int gpiochip_populate_parent_fwspec_twocell(struct gpio_chip
*gc
,
1614 union gpio_irq_fwspec
*gfwspec
,
1615 unsigned int parent_hwirq
,
1616 unsigned int parent_type
)
1618 struct irq_fwspec
*fwspec
= &gfwspec
->fwspec
;
1620 fwspec
->fwnode
= gc
->irq
.parent_domain
->fwnode
;
1621 fwspec
->param_count
= 2;
1622 fwspec
->param
[0] = parent_hwirq
;
1623 fwspec
->param
[1] = parent_type
;
1627 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell
);
1629 int gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip
*gc
,
1630 union gpio_irq_fwspec
*gfwspec
,
1631 unsigned int parent_hwirq
,
1632 unsigned int parent_type
)
1634 struct irq_fwspec
*fwspec
= &gfwspec
->fwspec
;
1636 fwspec
->fwnode
= gc
->irq
.parent_domain
->fwnode
;
1637 fwspec
->param_count
= 4;
1638 fwspec
->param
[0] = 0;
1639 fwspec
->param
[1] = parent_hwirq
;
1640 fwspec
->param
[2] = 0;
1641 fwspec
->param
[3] = parent_type
;
1645 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell
);
1649 static struct irq_domain
*gpiochip_hierarchy_create_domain(struct gpio_chip
*gc
)
1651 return ERR_PTR(-EINVAL
);
1654 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip
*gc
)
1659 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1662 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1663 * @d: the irqdomain used by this irqchip
1664 * @irq: the global irq number used by this GPIO irqchip irq
1665 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1667 * This function will set up the mapping for a certain IRQ line on a
1668 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1669 * stored inside the gpiochip.
1672 * 0 on success, or negative errno on failure.
1674 static int gpiochip_irq_map(struct irq_domain
*d
, unsigned int irq
,
1675 irq_hw_number_t hwirq
)
1677 struct gpio_chip
*gc
= d
->host_data
;
1680 if (!gpiochip_irqchip_irq_valid(gc
, hwirq
))
1683 irq_set_chip_data(irq
, gc
);
1685 * This lock class tells lockdep that GPIO irqs are in a different
1686 * category than their parents, so it won't report false recursion.
1688 irq_set_lockdep_class(irq
, gc
->irq
.lock_key
, gc
->irq
.request_key
);
1689 irq_set_chip_and_handler(irq
, gc
->irq
.chip
, gc
->irq
.handler
);
1690 /* Chips that use nested thread handlers have them marked */
1691 if (gc
->irq
.threaded
)
1692 irq_set_nested_thread(irq
, 1);
1693 irq_set_noprobe(irq
);
1695 if (gc
->irq
.num_parents
== 1)
1696 ret
= irq_set_parent(irq
, gc
->irq
.parents
[0]);
1697 else if (gc
->irq
.map
)
1698 ret
= irq_set_parent(irq
, gc
->irq
.map
[hwirq
]);
1704 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1705 * is passed as default type.
1707 if (gc
->irq
.default_type
!= IRQ_TYPE_NONE
)
1708 irq_set_irq_type(irq
, gc
->irq
.default_type
);
1713 static void gpiochip_irq_unmap(struct irq_domain
*d
, unsigned int irq
)
1715 struct gpio_chip
*gc
= d
->host_data
;
1717 if (gc
->irq
.threaded
)
1718 irq_set_nested_thread(irq
, 0);
1719 irq_set_chip_and_handler(irq
, NULL
, NULL
);
1720 irq_set_chip_data(irq
, NULL
);
1723 static const struct irq_domain_ops gpiochip_domain_ops
= {
1724 .map
= gpiochip_irq_map
,
1725 .unmap
= gpiochip_irq_unmap
,
1726 /* Virtually all GPIO irqchips are twocell:ed */
1727 .xlate
= irq_domain_xlate_twocell
,
1730 static struct irq_domain
*gpiochip_simple_create_domain(struct gpio_chip
*gc
)
1732 struct fwnode_handle
*fwnode
= dev_fwnode(&gc
->gpiodev
->dev
);
1733 struct irq_domain
*domain
;
1735 domain
= irq_domain_create_simple(fwnode
, gc
->ngpio
, gc
->irq
.first
,
1736 &gpiochip_domain_ops
, gc
);
1738 return ERR_PTR(-EINVAL
);
1743 static int gpiochip_to_irq(struct gpio_chip
*gc
, unsigned int offset
)
1745 struct irq_domain
*domain
= gc
->irq
.domain
;
1747 #ifdef CONFIG_GPIOLIB_IRQCHIP
1749 * Avoid race condition with other code, which tries to lookup
1750 * an IRQ before the irqchip has been properly registered,
1751 * i.e. while gpiochip is still being brought up.
1753 if (!gc
->irq
.initialized
)
1754 return -EPROBE_DEFER
;
1757 if (!gpiochip_irqchip_irq_valid(gc
, offset
))
1760 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1761 if (irq_domain_is_hierarchy(domain
)) {
1762 struct irq_fwspec spec
;
1764 spec
.fwnode
= domain
->fwnode
;
1765 spec
.param_count
= 2;
1766 spec
.param
[0] = gc
->irq
.child_offset_to_irq(gc
, offset
);
1767 spec
.param
[1] = IRQ_TYPE_NONE
;
1769 return irq_create_fwspec_mapping(&spec
);
1773 return irq_create_mapping(domain
, offset
);
1776 int gpiochip_irq_reqres(struct irq_data
*d
)
1778 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
1779 unsigned int hwirq
= irqd_to_hwirq(d
);
1781 return gpiochip_reqres_irq(gc
, hwirq
);
1783 EXPORT_SYMBOL(gpiochip_irq_reqres
);
1785 void gpiochip_irq_relres(struct irq_data
*d
)
1787 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
1788 unsigned int hwirq
= irqd_to_hwirq(d
);
1790 gpiochip_relres_irq(gc
, hwirq
);
1792 EXPORT_SYMBOL(gpiochip_irq_relres
);
1794 static void gpiochip_irq_mask(struct irq_data
*d
)
1796 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
1797 unsigned int hwirq
= irqd_to_hwirq(d
);
1799 if (gc
->irq
.irq_mask
)
1800 gc
->irq
.irq_mask(d
);
1801 gpiochip_disable_irq(gc
, hwirq
);
1804 static void gpiochip_irq_unmask(struct irq_data
*d
)
1806 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
1807 unsigned int hwirq
= irqd_to_hwirq(d
);
1809 gpiochip_enable_irq(gc
, hwirq
);
1810 if (gc
->irq
.irq_unmask
)
1811 gc
->irq
.irq_unmask(d
);
1814 static void gpiochip_irq_enable(struct irq_data
*d
)
1816 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
1817 unsigned int hwirq
= irqd_to_hwirq(d
);
1819 gpiochip_enable_irq(gc
, hwirq
);
1820 gc
->irq
.irq_enable(d
);
1823 static void gpiochip_irq_disable(struct irq_data
*d
)
1825 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
1826 unsigned int hwirq
= irqd_to_hwirq(d
);
1828 gc
->irq
.irq_disable(d
);
1829 gpiochip_disable_irq(gc
, hwirq
);
1832 static void gpiochip_set_irq_hooks(struct gpio_chip
*gc
)
1834 struct irq_chip
*irqchip
= gc
->irq
.chip
;
1836 if (irqchip
->flags
& IRQCHIP_IMMUTABLE
)
1839 chip_warn(gc
, "not an immutable chip, please consider fixing it!\n");
1841 if (!irqchip
->irq_request_resources
&&
1842 !irqchip
->irq_release_resources
) {
1843 irqchip
->irq_request_resources
= gpiochip_irq_reqres
;
1844 irqchip
->irq_release_resources
= gpiochip_irq_relres
;
1846 if (WARN_ON(gc
->irq
.irq_enable
))
1848 /* Check if the irqchip already has this hook... */
1849 if (irqchip
->irq_enable
== gpiochip_irq_enable
||
1850 irqchip
->irq_mask
== gpiochip_irq_mask
) {
1852 * ...and if so, give a gentle warning that this is bad
1856 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
1860 if (irqchip
->irq_disable
) {
1861 gc
->irq
.irq_disable
= irqchip
->irq_disable
;
1862 irqchip
->irq_disable
= gpiochip_irq_disable
;
1864 gc
->irq
.irq_mask
= irqchip
->irq_mask
;
1865 irqchip
->irq_mask
= gpiochip_irq_mask
;
1868 if (irqchip
->irq_enable
) {
1869 gc
->irq
.irq_enable
= irqchip
->irq_enable
;
1870 irqchip
->irq_enable
= gpiochip_irq_enable
;
1872 gc
->irq
.irq_unmask
= irqchip
->irq_unmask
;
1873 irqchip
->irq_unmask
= gpiochip_irq_unmask
;
1877 static int gpiochip_irqchip_add_allocated_domain(struct gpio_chip
*gc
,
1878 struct irq_domain
*domain
,
1879 bool allocated_externally
)
1885 chip_warn(gc
, "to_irq is redefined in %s and you shouldn't rely on it\n", __func__
);
1887 gc
->to_irq
= gpiochip_to_irq
;
1888 gc
->irq
.domain
= domain
;
1889 gc
->irq
.domain_is_allocated_externally
= allocated_externally
;
1892 * Using barrier() here to prevent compiler from reordering
1893 * gc->irq.initialized before adding irqdomain.
1897 gc
->irq
.initialized
= true;
1903 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1904 * @gc: the GPIO chip to add the IRQ chip to
1905 * @lock_key: lockdep class for IRQ lock
1906 * @request_key: lockdep class for IRQ request
1909 * 0 on success, or a negative errno on failure.
1911 static int gpiochip_add_irqchip(struct gpio_chip
*gc
,
1912 struct lock_class_key
*lock_key
,
1913 struct lock_class_key
*request_key
)
1915 struct fwnode_handle
*fwnode
= dev_fwnode(&gc
->gpiodev
->dev
);
1916 struct irq_chip
*irqchip
= gc
->irq
.chip
;
1917 struct irq_domain
*domain
;
1925 if (gc
->irq
.parent_handler
&& gc
->can_sleep
) {
1926 chip_err(gc
, "you cannot have chained interrupts on a chip that may sleep\n");
1930 type
= gc
->irq
.default_type
;
1933 * Specifying a default trigger is a terrible idea if DT or ACPI is
1934 * used to configure the interrupts, as you may end up with
1935 * conflicting triggers. Tell the user, and reset to NONE.
1937 if (WARN(fwnode
&& type
!= IRQ_TYPE_NONE
,
1938 "%pfw: Ignoring %u default trigger\n", fwnode
, type
))
1939 type
= IRQ_TYPE_NONE
;
1941 gc
->irq
.default_type
= type
;
1942 gc
->irq
.lock_key
= lock_key
;
1943 gc
->irq
.request_key
= request_key
;
1945 /* If a parent irqdomain is provided, let's build a hierarchy */
1946 if (gpiochip_hierarchy_is_hierarchical(gc
)) {
1947 domain
= gpiochip_hierarchy_create_domain(gc
);
1949 domain
= gpiochip_simple_create_domain(gc
);
1952 return PTR_ERR(domain
);
1954 if (gc
->irq
.parent_handler
) {
1955 for (i
= 0; i
< gc
->irq
.num_parents
; i
++) {
1958 if (gc
->irq
.per_parent_data
)
1959 data
= gc
->irq
.parent_handler_data_array
[i
];
1961 data
= gc
->irq
.parent_handler_data
?: gc
;
1964 * The parent IRQ chip is already using the chip_data
1965 * for this IRQ chip, so our callbacks simply use the
1968 irq_set_chained_handler_and_data(gc
->irq
.parents
[i
],
1969 gc
->irq
.parent_handler
,
1974 gpiochip_set_irq_hooks(gc
);
1976 ret
= gpiochip_irqchip_add_allocated_domain(gc
, domain
, false);
1980 acpi_gpiochip_request_interrupts(gc
);
1986 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1987 * @gc: the gpiochip to remove the irqchip from
1989 * This is called only from gpiochip_remove()
1991 static void gpiochip_irqchip_remove(struct gpio_chip
*gc
)
1993 struct irq_chip
*irqchip
= gc
->irq
.chip
;
1994 unsigned int offset
;
1996 acpi_gpiochip_free_interrupts(gc
);
1998 if (irqchip
&& gc
->irq
.parent_handler
) {
1999 struct gpio_irq_chip
*irq
= &gc
->irq
;
2002 for (i
= 0; i
< irq
->num_parents
; i
++)
2003 irq_set_chained_handler_and_data(irq
->parents
[i
],
2007 /* Remove all IRQ mappings and delete the domain */
2008 if (!gc
->irq
.domain_is_allocated_externally
&& gc
->irq
.domain
) {
2011 for (offset
= 0; offset
< gc
->ngpio
; offset
++) {
2012 if (!gpiochip_irqchip_irq_valid(gc
, offset
))
2015 irq
= irq_find_mapping(gc
->irq
.domain
, offset
);
2016 irq_dispose_mapping(irq
);
2019 irq_domain_remove(gc
->irq
.domain
);
2022 if (irqchip
&& !(irqchip
->flags
& IRQCHIP_IMMUTABLE
)) {
2023 if (irqchip
->irq_request_resources
== gpiochip_irq_reqres
) {
2024 irqchip
->irq_request_resources
= NULL
;
2025 irqchip
->irq_release_resources
= NULL
;
2027 if (irqchip
->irq_enable
== gpiochip_irq_enable
) {
2028 irqchip
->irq_enable
= gc
->irq
.irq_enable
;
2029 irqchip
->irq_disable
= gc
->irq
.irq_disable
;
2032 gc
->irq
.irq_enable
= NULL
;
2033 gc
->irq
.irq_disable
= NULL
;
2034 gc
->irq
.chip
= NULL
;
2036 gpiochip_irqchip_free_valid_mask(gc
);
2040 * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip
2041 * @gc: the gpiochip to add the irqchip to
2042 * @domain: the irqdomain to add to the gpiochip
2044 * This function adds an IRQ domain to the gpiochip.
2047 * 0 on success, or negative errno on failure.
2049 int gpiochip_irqchip_add_domain(struct gpio_chip
*gc
,
2050 struct irq_domain
*domain
)
2052 return gpiochip_irqchip_add_allocated_domain(gc
, domain
, true);
2054 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_domain
);
2056 #else /* CONFIG_GPIOLIB_IRQCHIP */
2058 static inline int gpiochip_add_irqchip(struct gpio_chip
*gc
,
2059 struct lock_class_key
*lock_key
,
2060 struct lock_class_key
*request_key
)
2064 static void gpiochip_irqchip_remove(struct gpio_chip
*gc
) {}
2066 static inline int gpiochip_irqchip_init_hw(struct gpio_chip
*gc
)
2071 static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip
*gc
)
2075 static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip
*gc
)
2078 #endif /* CONFIG_GPIOLIB_IRQCHIP */
2081 * gpiochip_generic_request() - request the gpio function for a pin
2082 * @gc: the gpiochip owning the GPIO
2083 * @offset: the offset of the GPIO to request for GPIO function
2086 * 0 on success, or negative errno on failure.
2088 int gpiochip_generic_request(struct gpio_chip
*gc
, unsigned int offset
)
2090 #ifdef CONFIG_PINCTRL
2091 if (list_empty(&gc
->gpiodev
->pin_ranges
))
2095 return pinctrl_gpio_request(gc
, offset
);
2097 EXPORT_SYMBOL_GPL(gpiochip_generic_request
);
2100 * gpiochip_generic_free() - free the gpio function from a pin
2101 * @gc: the gpiochip to request the gpio function for
2102 * @offset: the offset of the GPIO to free from GPIO function
2104 void gpiochip_generic_free(struct gpio_chip
*gc
, unsigned int offset
)
2106 #ifdef CONFIG_PINCTRL
2107 if (list_empty(&gc
->gpiodev
->pin_ranges
))
2111 pinctrl_gpio_free(gc
, offset
);
2113 EXPORT_SYMBOL_GPL(gpiochip_generic_free
);
2116 * gpiochip_generic_config() - apply configuration for a pin
2117 * @gc: the gpiochip owning the GPIO
2118 * @offset: the offset of the GPIO to apply the configuration
2119 * @config: the configuration to be applied
2122 * 0 on success, or negative errno on failure.
2124 int gpiochip_generic_config(struct gpio_chip
*gc
, unsigned int offset
,
2125 unsigned long config
)
2127 #ifdef CONFIG_PINCTRL
2128 if (list_empty(&gc
->gpiodev
->pin_ranges
))
2132 return pinctrl_gpio_set_config(gc
, offset
, config
);
2134 EXPORT_SYMBOL_GPL(gpiochip_generic_config
);
2136 #ifdef CONFIG_PINCTRL
2139 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2140 * @gc: the gpiochip to add the range for
2141 * @pctldev: the pin controller to map to
2142 * @gpio_offset: the start offset in the current gpio_chip number space
2143 * @pin_group: name of the pin group inside the pin controller
2145 * Calling this function directly from a DeviceTree-supported
2146 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2147 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2148 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
2151 * 0 on success, or negative errno on failure.
2153 int gpiochip_add_pingroup_range(struct gpio_chip
*gc
,
2154 struct pinctrl_dev
*pctldev
,
2155 unsigned int gpio_offset
, const char *pin_group
)
2157 struct gpio_pin_range
*pin_range
;
2158 struct gpio_device
*gdev
= gc
->gpiodev
;
2161 pin_range
= kzalloc(sizeof(*pin_range
), GFP_KERNEL
);
2163 chip_err(gc
, "failed to allocate pin ranges\n");
2167 /* Use local offset as range ID */
2168 pin_range
->range
.id
= gpio_offset
;
2169 pin_range
->range
.gc
= gc
;
2170 pin_range
->range
.name
= gc
->label
;
2171 pin_range
->range
.base
= gdev
->base
+ gpio_offset
;
2172 pin_range
->pctldev
= pctldev
;
2174 ret
= pinctrl_get_group_pins(pctldev
, pin_group
,
2175 &pin_range
->range
.pins
,
2176 &pin_range
->range
.npins
);
2182 pinctrl_add_gpio_range(pctldev
, &pin_range
->range
);
2184 chip_dbg(gc
, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2185 gpio_offset
, gpio_offset
+ pin_range
->range
.npins
- 1,
2186 pinctrl_dev_get_devname(pctldev
), pin_group
);
2188 list_add_tail(&pin_range
->node
, &gdev
->pin_ranges
);
2192 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range
);
2195 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2196 * @gc: the gpiochip to add the range for
2197 * @pinctl_name: the dev_name() of the pin controller to map to
2198 * @gpio_offset: the start offset in the current gpio_chip number space
2199 * @pin_offset: the start offset in the pin controller number space
2200 * @npins: the number of pins from the offset of each pin space (GPIO and
2201 * pin controller) to accumulate in this range
2203 * Calling this function directly from a DeviceTree-supported
2204 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2205 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2206 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
2209 * 0 on success, or a negative errno on failure.
2211 int gpiochip_add_pin_range(struct gpio_chip
*gc
, const char *pinctl_name
,
2212 unsigned int gpio_offset
, unsigned int pin_offset
,
2215 struct gpio_pin_range
*pin_range
;
2216 struct gpio_device
*gdev
= gc
->gpiodev
;
2219 pin_range
= kzalloc(sizeof(*pin_range
), GFP_KERNEL
);
2221 chip_err(gc
, "failed to allocate pin ranges\n");
2225 /* Use local offset as range ID */
2226 pin_range
->range
.id
= gpio_offset
;
2227 pin_range
->range
.gc
= gc
;
2228 pin_range
->range
.name
= gc
->label
;
2229 pin_range
->range
.base
= gdev
->base
+ gpio_offset
;
2230 pin_range
->range
.pin_base
= pin_offset
;
2231 pin_range
->range
.npins
= npins
;
2232 pin_range
->pctldev
= pinctrl_find_and_add_gpio_range(pinctl_name
,
2234 if (IS_ERR(pin_range
->pctldev
)) {
2235 ret
= PTR_ERR(pin_range
->pctldev
);
2236 chip_err(gc
, "could not create pin range\n");
2240 chip_dbg(gc
, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2241 gpio_offset
, gpio_offset
+ npins
- 1,
2243 pin_offset
, pin_offset
+ npins
- 1);
2245 list_add_tail(&pin_range
->node
, &gdev
->pin_ranges
);
2249 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range
);
2252 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2253 * @gc: the chip to remove all the mappings for
2255 void gpiochip_remove_pin_ranges(struct gpio_chip
*gc
)
2257 struct gpio_pin_range
*pin_range
, *tmp
;
2258 struct gpio_device
*gdev
= gc
->gpiodev
;
2260 list_for_each_entry_safe(pin_range
, tmp
, &gdev
->pin_ranges
, node
) {
2261 list_del(&pin_range
->node
);
2262 pinctrl_remove_gpio_range(pin_range
->pctldev
,
2267 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges
);
2269 #endif /* CONFIG_PINCTRL */
2271 /* These "optional" allocation calls help prevent drivers from stomping
2272 * on each other, and help provide better diagnostics in debugfs.
2273 * They're called even less than the "set direction" calls.
2275 static int gpiod_request_commit(struct gpio_desc
*desc
, const char *label
)
2277 unsigned int offset
;
2280 CLASS(gpio_chip_guard
, guard
)(desc
);
2284 if (test_and_set_bit(FLAG_REQUESTED
, &desc
->flags
))
2287 /* NOTE: gpio_request() can be called in early boot,
2288 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
2291 if (guard
.gc
->request
) {
2292 offset
= gpio_chip_hwgpio(desc
);
2293 if (gpiochip_line_is_valid(guard
.gc
, offset
))
2294 ret
= guard
.gc
->request(guard
.gc
, offset
);
2301 if (guard
.gc
->get_direction
)
2302 gpiod_get_direction(desc
);
2304 ret
= desc_set_label(desc
, label
? : "?");
2311 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
2316 * This descriptor validation needs to be inserted verbatim into each
2317 * function taking a descriptor, so we need to use a preprocessor
2318 * macro to avoid endless duplication. If the desc is NULL it is an
2319 * optional GPIO and calls should just bail out.
2321 static int validate_desc(const struct gpio_desc
*desc
, const char *func
)
2327 pr_warn("%s: invalid GPIO (errorpointer)\n", func
);
2328 return PTR_ERR(desc
);
2334 #define VALIDATE_DESC(desc) do { \
2335 int __valid = validate_desc(desc, __func__); \
2340 #define VALIDATE_DESC_VOID(desc) do { \
2341 int __valid = validate_desc(desc, __func__); \
2346 int gpiod_request(struct gpio_desc
*desc
, const char *label
)
2348 int ret
= -EPROBE_DEFER
;
2350 VALIDATE_DESC(desc
);
2352 if (try_module_get(desc
->gdev
->owner
)) {
2353 ret
= gpiod_request_commit(desc
, label
);
2355 module_put(desc
->gdev
->owner
);
2357 gpio_device_get(desc
->gdev
);
2361 gpiod_dbg(desc
, "%s: status %d\n", __func__
, ret
);
2366 static void gpiod_free_commit(struct gpio_desc
*desc
)
2368 unsigned long flags
;
2372 CLASS(gpio_chip_guard
, guard
)(desc
);
2374 flags
= READ_ONCE(desc
->flags
);
2376 if (guard
.gc
&& test_bit(FLAG_REQUESTED
, &flags
)) {
2378 guard
.gc
->free(guard
.gc
, gpio_chip_hwgpio(desc
));
2380 clear_bit(FLAG_ACTIVE_LOW
, &flags
);
2381 clear_bit(FLAG_REQUESTED
, &flags
);
2382 clear_bit(FLAG_OPEN_DRAIN
, &flags
);
2383 clear_bit(FLAG_OPEN_SOURCE
, &flags
);
2384 clear_bit(FLAG_PULL_UP
, &flags
);
2385 clear_bit(FLAG_PULL_DOWN
, &flags
);
2386 clear_bit(FLAG_BIAS_DISABLE
, &flags
);
2387 clear_bit(FLAG_EDGE_RISING
, &flags
);
2388 clear_bit(FLAG_EDGE_FALLING
, &flags
);
2389 clear_bit(FLAG_IS_HOGGED
, &flags
);
2390 #ifdef CONFIG_OF_DYNAMIC
2391 WRITE_ONCE(desc
->hog
, NULL
);
2393 desc_set_label(desc
, NULL
);
2394 WRITE_ONCE(desc
->flags
, flags
);
2396 gpiod_line_state_notify(desc
, GPIOLINE_CHANGED_RELEASED
);
2400 void gpiod_free(struct gpio_desc
*desc
)
2402 VALIDATE_DESC_VOID(desc
);
2404 gpiod_free_commit(desc
);
2405 module_put(desc
->gdev
->owner
);
2406 gpio_device_put(desc
->gdev
);
2410 * gpiochip_dup_line_label - Get a copy of the consumer label.
2411 * @gc: GPIO chip controlling this line.
2412 * @offset: Hardware offset of the line.
2415 * Pointer to a copy of the consumer label if the line is requested or NULL
2416 * if it's not. If a valid pointer was returned, it must be freed using
2417 * kfree(). In case of a memory allocation error, the function returns %ENOMEM.
2419 * Must not be called from atomic context.
2421 char *gpiochip_dup_line_label(struct gpio_chip
*gc
, unsigned int offset
)
2423 struct gpio_desc
*desc
;
2426 desc
= gpiochip_get_desc(gc
, offset
);
2430 if (!test_bit(FLAG_REQUESTED
, &desc
->flags
))
2433 guard(srcu
)(&desc
->gdev
->desc_srcu
);
2435 label
= kstrdup(gpiod_get_label(desc
), GFP_KERNEL
);
2437 return ERR_PTR(-ENOMEM
);
2441 EXPORT_SYMBOL_GPL(gpiochip_dup_line_label
);
2443 static inline const char *function_name_or_default(const char *con_id
)
2445 return con_id
?: "(default)";
2449 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2451 * @hwnum: hardware number of the GPIO for which to request the descriptor
2452 * @label: label for the GPIO
2453 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2454 * specify things like line inversion semantics with the machine flags
2455 * such as GPIO_OUT_LOW
2456 * @dflags: descriptor request flags for this GPIO or 0 if default, this
2457 * can be used to specify consumer semantics such as open drain
2459 * Function allows GPIO chip drivers to request and use their own GPIO
2460 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2461 * function will not increase reference count of the GPIO chip module. This
2462 * allows the GPIO chip module to be unloaded as needed (we assume that the
2463 * GPIO chip driver handles freeing the GPIOs it has requested).
2466 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2469 struct gpio_desc
*gpiochip_request_own_desc(struct gpio_chip
*gc
,
2472 enum gpio_lookup_flags lflags
,
2473 enum gpiod_flags dflags
)
2475 struct gpio_desc
*desc
= gpiochip_get_desc(gc
, hwnum
);
2476 const char *name
= function_name_or_default(label
);
2480 chip_err(gc
, "failed to get GPIO %s descriptor\n", name
);
2484 ret
= gpiod_request_commit(desc
, label
);
2486 return ERR_PTR(ret
);
2488 ret
= gpiod_configure_flags(desc
, label
, lflags
, dflags
);
2490 gpiod_free_commit(desc
);
2491 chip_err(gc
, "setup of own GPIO %s failed\n", name
);
2492 return ERR_PTR(ret
);
2497 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc
);
2500 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2501 * @desc: GPIO descriptor to free
2503 * Function frees the given GPIO requested previously with
2504 * gpiochip_request_own_desc().
2506 void gpiochip_free_own_desc(struct gpio_desc
*desc
)
2509 gpiod_free_commit(desc
);
2511 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc
);
2514 * Drivers MUST set GPIO direction before making get/set calls. In
2515 * some cases this is done in early boot, before IRQs are enabled.
2517 * As a rule these aren't called more than once (except for drivers
2518 * using the open-drain emulation idiom) so these are natural places
2519 * to accumulate extra debugging checks. Note that we can't (yet)
2520 * rely on gpio_request() having been called beforehand.
2523 static int gpio_do_set_config(struct gpio_chip
*gc
, unsigned int offset
,
2524 unsigned long config
)
2526 if (!gc
->set_config
)
2529 return gc
->set_config(gc
, offset
, config
);
2532 static int gpio_set_config_with_argument(struct gpio_desc
*desc
,
2533 enum pin_config_param mode
,
2536 unsigned long config
;
2538 CLASS(gpio_chip_guard
, guard
)(desc
);
2542 config
= pinconf_to_config_packed(mode
, argument
);
2543 return gpio_do_set_config(guard
.gc
, gpio_chip_hwgpio(desc
), config
);
2546 static int gpio_set_config_with_argument_optional(struct gpio_desc
*desc
,
2547 enum pin_config_param mode
,
2550 struct device
*dev
= &desc
->gdev
->dev
;
2551 int gpio
= gpio_chip_hwgpio(desc
);
2554 ret
= gpio_set_config_with_argument(desc
, mode
, argument
);
2555 if (ret
!= -ENOTSUPP
)
2559 case PIN_CONFIG_PERSIST_STATE
:
2560 dev_dbg(dev
, "Persistence not supported for GPIO %d\n", gpio
);
2569 static int gpio_set_config(struct gpio_desc
*desc
, enum pin_config_param mode
)
2571 return gpio_set_config_with_argument(desc
, mode
, 0);
2574 static int gpio_set_bias(struct gpio_desc
*desc
)
2576 enum pin_config_param bias
;
2577 unsigned long flags
;
2580 flags
= READ_ONCE(desc
->flags
);
2582 if (test_bit(FLAG_BIAS_DISABLE
, &flags
))
2583 bias
= PIN_CONFIG_BIAS_DISABLE
;
2584 else if (test_bit(FLAG_PULL_UP
, &flags
))
2585 bias
= PIN_CONFIG_BIAS_PULL_UP
;
2586 else if (test_bit(FLAG_PULL_DOWN
, &flags
))
2587 bias
= PIN_CONFIG_BIAS_PULL_DOWN
;
2592 case PIN_CONFIG_BIAS_PULL_DOWN
:
2593 case PIN_CONFIG_BIAS_PULL_UP
:
2602 return gpio_set_config_with_argument_optional(desc
, bias
, arg
);
2606 * gpio_set_debounce_timeout() - Set debounce timeout
2607 * @desc: GPIO descriptor to set the debounce timeout
2608 * @debounce: Debounce timeout in microseconds
2610 * The function calls the certain GPIO driver to set debounce timeout
2614 * 0 on success, or negative errno on failure.
2616 int gpio_set_debounce_timeout(struct gpio_desc
*desc
, unsigned int debounce
)
2618 return gpio_set_config_with_argument_optional(desc
,
2619 PIN_CONFIG_INPUT_DEBOUNCE
,
2624 * gpiod_direction_input - set the GPIO direction to input
2625 * @desc: GPIO to set to input
2627 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2628 * be called safely on it.
2631 * 0 on success, or negative errno on failure.
2633 int gpiod_direction_input(struct gpio_desc
*desc
)
2637 VALIDATE_DESC(desc
);
2639 CLASS(gpio_chip_guard
, guard
)(desc
);
2644 * It is legal to have no .get() and .direction_input() specified if
2645 * the chip is output-only, but you can't specify .direction_input()
2646 * and not support the .get() operation, that doesn't make sense.
2648 if (!guard
.gc
->get
&& guard
.gc
->direction_input
) {
2650 "%s: missing get() but have direction_input()\n",
2656 * If we have a .direction_input() callback, things are simple,
2657 * just call it. Else we are some input-only chip so try to check the
2658 * direction (if .get_direction() is supported) else we silently
2659 * assume we are in input mode after this.
2661 if (guard
.gc
->direction_input
) {
2662 ret
= guard
.gc
->direction_input(guard
.gc
,
2663 gpio_chip_hwgpio(desc
));
2664 } else if (guard
.gc
->get_direction
&&
2665 (guard
.gc
->get_direction(guard
.gc
,
2666 gpio_chip_hwgpio(desc
)) != 1)) {
2668 "%s: missing direction_input() operation and line is output\n",
2673 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
2674 ret
= gpio_set_bias(desc
);
2677 trace_gpio_direction(desc_to_gpio(desc
), 1, ret
);
2681 EXPORT_SYMBOL_GPL(gpiod_direction_input
);
2683 static int gpiod_direction_output_raw_commit(struct gpio_desc
*desc
, int value
)
2685 int val
= !!value
, ret
= 0;
2687 CLASS(gpio_chip_guard
, guard
)(desc
);
2692 * It's OK not to specify .direction_output() if the gpiochip is
2693 * output-only, but if there is then not even a .set() operation it
2694 * is pretty tricky to drive the output line.
2696 if (!guard
.gc
->set
&& !guard
.gc
->direction_output
) {
2698 "%s: missing set() and direction_output() operations\n",
2703 if (guard
.gc
->direction_output
) {
2704 ret
= guard
.gc
->direction_output(guard
.gc
,
2705 gpio_chip_hwgpio(desc
), val
);
2707 /* Check that we are in output mode if we can */
2708 if (guard
.gc
->get_direction
&&
2709 guard
.gc
->get_direction(guard
.gc
, gpio_chip_hwgpio(desc
))) {
2711 "%s: missing direction_output() operation\n",
2716 * If we can't actively set the direction, we are some
2717 * output-only chip, so just drive the output as desired.
2719 guard
.gc
->set(guard
.gc
, gpio_chip_hwgpio(desc
), val
);
2723 set_bit(FLAG_IS_OUT
, &desc
->flags
);
2724 trace_gpio_value(desc_to_gpio(desc
), 0, val
);
2725 trace_gpio_direction(desc_to_gpio(desc
), 0, ret
);
2730 * gpiod_direction_output_raw - set the GPIO direction to output
2731 * @desc: GPIO to set to output
2732 * @value: initial output value of the GPIO
2734 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2735 * be called safely on it. The initial value of the output must be specified
2736 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2739 * 0 on success, or negative errno on failure.
2741 int gpiod_direction_output_raw(struct gpio_desc
*desc
, int value
)
2743 VALIDATE_DESC(desc
);
2744 return gpiod_direction_output_raw_commit(desc
, value
);
2746 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw
);
2749 * gpiod_direction_output - set the GPIO direction to output
2750 * @desc: GPIO to set to output
2751 * @value: initial output value of the GPIO
2753 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2754 * be called safely on it. The initial value of the output must be specified
2755 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2759 * 0 on success, or negative errno on failure.
2761 int gpiod_direction_output(struct gpio_desc
*desc
, int value
)
2763 unsigned long flags
;
2766 VALIDATE_DESC(desc
);
2768 flags
= READ_ONCE(desc
->flags
);
2770 if (test_bit(FLAG_ACTIVE_LOW
, &flags
))
2775 /* GPIOs used for enabled IRQs shall not be set as output */
2776 if (test_bit(FLAG_USED_AS_IRQ
, &flags
) &&
2777 test_bit(FLAG_IRQ_IS_ENABLED
, &flags
)) {
2779 "%s: tried to set a GPIO tied to an IRQ as output\n",
2784 if (test_bit(FLAG_OPEN_DRAIN
, &flags
)) {
2785 /* First see if we can enable open drain in hardware */
2786 ret
= gpio_set_config(desc
, PIN_CONFIG_DRIVE_OPEN_DRAIN
);
2788 goto set_output_value
;
2789 /* Emulate open drain by not actively driving the line high */
2791 ret
= gpiod_direction_input(desc
);
2792 goto set_output_flag
;
2794 } else if (test_bit(FLAG_OPEN_SOURCE
, &flags
)) {
2795 ret
= gpio_set_config(desc
, PIN_CONFIG_DRIVE_OPEN_SOURCE
);
2797 goto set_output_value
;
2798 /* Emulate open source by not actively driving the line low */
2800 ret
= gpiod_direction_input(desc
);
2801 goto set_output_flag
;
2804 gpio_set_config(desc
, PIN_CONFIG_DRIVE_PUSH_PULL
);
2808 ret
= gpio_set_bias(desc
);
2811 return gpiod_direction_output_raw_commit(desc
, value
);
2815 * When emulating open-source or open-drain functionalities by not
2816 * actively driving the line (setting mode to input) we still need to
2817 * set the IS_OUT flag or otherwise we won't be able to set the line
2821 set_bit(FLAG_IS_OUT
, &desc
->flags
);
2824 EXPORT_SYMBOL_GPL(gpiod_direction_output
);
2827 * gpiod_enable_hw_timestamp_ns - Enable hardware timestamp in nanoseconds.
2829 * @desc: GPIO to enable.
2830 * @flags: Flags related to GPIO edge.
2833 * 0 on success, or negative errno on failure.
2835 int gpiod_enable_hw_timestamp_ns(struct gpio_desc
*desc
, unsigned long flags
)
2839 VALIDATE_DESC(desc
);
2841 CLASS(gpio_chip_guard
, guard
)(desc
);
2845 if (!guard
.gc
->en_hw_timestamp
) {
2846 gpiod_warn(desc
, "%s: hw ts not supported\n", __func__
);
2850 ret
= guard
.gc
->en_hw_timestamp(guard
.gc
,
2851 gpio_chip_hwgpio(desc
), flags
);
2853 gpiod_warn(desc
, "%s: hw ts request failed\n", __func__
);
2857 EXPORT_SYMBOL_GPL(gpiod_enable_hw_timestamp_ns
);
2860 * gpiod_disable_hw_timestamp_ns - Disable hardware timestamp.
2862 * @desc: GPIO to disable.
2863 * @flags: Flags related to GPIO edge, same value as used during enable call.
2866 * 0 on success, or negative errno on failure.
2868 int gpiod_disable_hw_timestamp_ns(struct gpio_desc
*desc
, unsigned long flags
)
2872 VALIDATE_DESC(desc
);
2874 CLASS(gpio_chip_guard
, guard
)(desc
);
2878 if (!guard
.gc
->dis_hw_timestamp
) {
2879 gpiod_warn(desc
, "%s: hw ts not supported\n", __func__
);
2883 ret
= guard
.gc
->dis_hw_timestamp(guard
.gc
, gpio_chip_hwgpio(desc
),
2886 gpiod_warn(desc
, "%s: hw ts release failed\n", __func__
);
2890 EXPORT_SYMBOL_GPL(gpiod_disable_hw_timestamp_ns
);
2893 * gpiod_set_config - sets @config for a GPIO
2894 * @desc: descriptor of the GPIO for which to set the configuration
2895 * @config: Same packed config format as generic pinconf
2898 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2901 int gpiod_set_config(struct gpio_desc
*desc
, unsigned long config
)
2903 VALIDATE_DESC(desc
);
2905 CLASS(gpio_chip_guard
, guard
)(desc
);
2909 return gpio_do_set_config(guard
.gc
, gpio_chip_hwgpio(desc
), config
);
2911 EXPORT_SYMBOL_GPL(gpiod_set_config
);
2914 * gpiod_set_debounce - sets @debounce time for a GPIO
2915 * @desc: descriptor of the GPIO for which to set debounce time
2916 * @debounce: debounce time in microseconds
2919 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2922 int gpiod_set_debounce(struct gpio_desc
*desc
, unsigned int debounce
)
2924 unsigned long config
;
2926 config
= pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE
, debounce
);
2927 return gpiod_set_config(desc
, config
);
2929 EXPORT_SYMBOL_GPL(gpiod_set_debounce
);
2932 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2933 * @desc: descriptor of the GPIO for which to configure persistence
2934 * @transitory: True to lose state on suspend or reset, false for persistence
2937 * 0 on success, otherwise a negative error code.
2939 int gpiod_set_transitory(struct gpio_desc
*desc
, bool transitory
)
2941 VALIDATE_DESC(desc
);
2943 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2944 * persistence state.
2946 assign_bit(FLAG_TRANSITORY
, &desc
->flags
, transitory
);
2948 /* If the driver supports it, set the persistence state now */
2949 return gpio_set_config_with_argument_optional(desc
,
2950 PIN_CONFIG_PERSIST_STATE
,
2955 * gpiod_is_active_low - test whether a GPIO is active-low or not
2956 * @desc: the gpio descriptor to test
2959 * 1 if the GPIO is active-low, 0 otherwise.
2961 int gpiod_is_active_low(const struct gpio_desc
*desc
)
2963 VALIDATE_DESC(desc
);
2964 return test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
2966 EXPORT_SYMBOL_GPL(gpiod_is_active_low
);
2969 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
2970 * @desc: the gpio descriptor to change
2972 void gpiod_toggle_active_low(struct gpio_desc
*desc
)
2974 VALIDATE_DESC_VOID(desc
);
2975 change_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
2977 EXPORT_SYMBOL_GPL(gpiod_toggle_active_low
);
2979 static int gpio_chip_get_value(struct gpio_chip
*gc
, const struct gpio_desc
*desc
)
2981 return gc
->get
? gc
->get(gc
, gpio_chip_hwgpio(desc
)) : -EIO
;
2984 /* I/O calls are only valid after configuration completed; the relevant
2985 * "is this a valid GPIO" error checks should already have been done.
2987 * "Get" operations are often inlinable as reading a pin value register,
2988 * and masking the relevant bit in that register.
2990 * When "set" operations are inlinable, they involve writing that mask to
2991 * one register to set a low value, or a different register to set it high.
2992 * Otherwise locking is needed, so there may be little value to inlining.
2994 *------------------------------------------------------------------------
2996 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2997 * have requested the GPIO. That can include implicit requesting by
2998 * a direction setting call. Marking a gpio as requested locks its chip
2999 * in memory, guaranteeing that these table lookups need no more locking
3000 * and that gpiochip_remove() will fail.
3002 * REVISIT when debugging, consider adding some instrumentation to ensure
3003 * that the GPIO was actually requested.
3006 static int gpiod_get_raw_value_commit(const struct gpio_desc
*desc
)
3008 struct gpio_device
*gdev
;
3009 struct gpio_chip
*gc
;
3012 /* FIXME Unable to use gpio_chip_guard due to const desc. */
3015 guard(srcu
)(&gdev
->srcu
);
3017 gc
= srcu_dereference(gdev
->chip
, &gdev
->srcu
);
3021 value
= gpio_chip_get_value(gc
, desc
);
3022 value
= value
< 0 ? value
: !!value
;
3023 trace_gpio_value(desc_to_gpio(desc
), 1, value
);
3027 static int gpio_chip_get_multiple(struct gpio_chip
*gc
,
3028 unsigned long *mask
, unsigned long *bits
)
3030 if (gc
->get_multiple
)
3031 return gc
->get_multiple(gc
, mask
, bits
);
3035 for_each_set_bit(i
, mask
, gc
->ngpio
) {
3036 value
= gc
->get(gc
, i
);
3039 __assign_bit(i
, bits
, value
);
3046 /* The 'other' chip must be protected with its GPIO device's SRCU. */
3047 static bool gpio_device_chip_cmp(struct gpio_device
*gdev
, struct gpio_chip
*gc
)
3049 guard(srcu
)(&gdev
->srcu
);
3051 return gc
== srcu_dereference(gdev
->chip
, &gdev
->srcu
);
3054 int gpiod_get_array_value_complex(bool raw
, bool can_sleep
,
3055 unsigned int array_size
,
3056 struct gpio_desc
**desc_array
,
3057 struct gpio_array
*array_info
,
3058 unsigned long *value_bitmap
)
3063 * Validate array_info against desc_array and its size.
3064 * It should immediately follow desc_array if both
3065 * have been obtained from the same gpiod_get_array() call.
3067 if (array_info
&& array_info
->desc
== desc_array
&&
3068 array_size
<= array_info
->size
&&
3069 (void *)array_info
== desc_array
+ array_info
->size
) {
3071 WARN_ON(array_info
->chip
->can_sleep
);
3073 ret
= gpio_chip_get_multiple(array_info
->chip
,
3074 array_info
->get_mask
,
3079 if (!raw
&& !bitmap_empty(array_info
->invert_mask
, array_size
))
3080 bitmap_xor(value_bitmap
, value_bitmap
,
3081 array_info
->invert_mask
, array_size
);
3083 i
= find_first_zero_bit(array_info
->get_mask
, array_size
);
3084 if (i
== array_size
)
3090 while (i
< array_size
) {
3091 DECLARE_BITMAP(fastpath_mask
, FASTPATH_NGPIO
);
3092 DECLARE_BITMAP(fastpath_bits
, FASTPATH_NGPIO
);
3093 unsigned long *mask
, *bits
;
3096 CLASS(gpio_chip_guard
, guard
)(desc_array
[i
]);
3100 if (likely(guard
.gc
->ngpio
<= FASTPATH_NGPIO
)) {
3101 mask
= fastpath_mask
;
3102 bits
= fastpath_bits
;
3104 gfp_t flags
= can_sleep
? GFP_KERNEL
: GFP_ATOMIC
;
3106 mask
= bitmap_alloc(guard
.gc
->ngpio
, flags
);
3110 bits
= bitmap_alloc(guard
.gc
->ngpio
, flags
);
3117 bitmap_zero(mask
, guard
.gc
->ngpio
);
3120 WARN_ON(guard
.gc
->can_sleep
);
3122 /* collect all inputs belonging to the same chip */
3125 const struct gpio_desc
*desc
= desc_array
[i
];
3126 int hwgpio
= gpio_chip_hwgpio(desc
);
3128 __set_bit(hwgpio
, mask
);
3132 i
= find_next_zero_bit(array_info
->get_mask
,
3134 } while ((i
< array_size
) &&
3135 gpio_device_chip_cmp(desc_array
[i
]->gdev
, guard
.gc
));
3137 ret
= gpio_chip_get_multiple(guard
.gc
, mask
, bits
);
3139 if (mask
!= fastpath_mask
)
3141 if (bits
!= fastpath_bits
)
3146 for (j
= first
; j
< i
; ) {
3147 const struct gpio_desc
*desc
= desc_array
[j
];
3148 int hwgpio
= gpio_chip_hwgpio(desc
);
3149 int value
= test_bit(hwgpio
, bits
);
3151 if (!raw
&& test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
3153 __assign_bit(j
, value_bitmap
, value
);
3154 trace_gpio_value(desc_to_gpio(desc
), 1, value
);
3158 j
= find_next_zero_bit(array_info
->get_mask
, i
,
3162 if (mask
!= fastpath_mask
)
3164 if (bits
!= fastpath_bits
)
3171 * gpiod_get_raw_value() - return a gpio's raw value
3172 * @desc: gpio whose value will be returned
3175 * The GPIO's raw value, i.e. the value of the physical line disregarding
3176 * its ACTIVE_LOW status, or negative errno on failure.
3178 * This function can be called from contexts where we cannot sleep, and will
3179 * complain if the GPIO chip functions potentially sleep.
3181 int gpiod_get_raw_value(const struct gpio_desc
*desc
)
3183 VALIDATE_DESC(desc
);
3184 /* Should be using gpiod_get_raw_value_cansleep() */
3185 WARN_ON(desc
->gdev
->can_sleep
);
3186 return gpiod_get_raw_value_commit(desc
);
3188 EXPORT_SYMBOL_GPL(gpiod_get_raw_value
);
3191 * gpiod_get_value() - return a gpio's value
3192 * @desc: gpio whose value will be returned
3195 * The GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3196 * account, or negative errno on failure.
3198 * This function can be called from contexts where we cannot sleep, and will
3199 * complain if the GPIO chip functions potentially sleep.
3201 int gpiod_get_value(const struct gpio_desc
*desc
)
3205 VALIDATE_DESC(desc
);
3206 /* Should be using gpiod_get_value_cansleep() */
3207 WARN_ON(desc
->gdev
->can_sleep
);
3209 value
= gpiod_get_raw_value_commit(desc
);
3213 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
3218 EXPORT_SYMBOL_GPL(gpiod_get_value
);
3221 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
3222 * @array_size: number of elements in the descriptor array / value bitmap
3223 * @desc_array: array of GPIO descriptors whose values will be read
3224 * @array_info: information on applicability of fast bitmap processing path
3225 * @value_bitmap: bitmap to store the read values
3227 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3228 * without regard for their ACTIVE_LOW status.
3230 * This function can be called from contexts where we cannot sleep,
3231 * and it will complain if the GPIO chip functions potentially sleep.
3234 * 0 on success, or negative errno on failure.
3236 int gpiod_get_raw_array_value(unsigned int array_size
,
3237 struct gpio_desc
**desc_array
,
3238 struct gpio_array
*array_info
,
3239 unsigned long *value_bitmap
)
3243 return gpiod_get_array_value_complex(true, false, array_size
,
3244 desc_array
, array_info
,
3247 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value
);
3250 * gpiod_get_array_value() - read values from an array of GPIOs
3251 * @array_size: number of elements in the descriptor array / value bitmap
3252 * @desc_array: array of GPIO descriptors whose values will be read
3253 * @array_info: information on applicability of fast bitmap processing path
3254 * @value_bitmap: bitmap to store the read values
3256 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3259 * This function can be called from contexts where we cannot sleep,
3260 * and it will complain if the GPIO chip functions potentially sleep.
3263 * 0 on success, or negative errno on failure.
3265 int gpiod_get_array_value(unsigned int array_size
,
3266 struct gpio_desc
**desc_array
,
3267 struct gpio_array
*array_info
,
3268 unsigned long *value_bitmap
)
3272 return gpiod_get_array_value_complex(false, false, array_size
,
3273 desc_array
, array_info
,
3276 EXPORT_SYMBOL_GPL(gpiod_get_array_value
);
3279 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
3280 * @desc: gpio descriptor whose state need to be set.
3281 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3283 static void gpio_set_open_drain_value_commit(struct gpio_desc
*desc
, bool value
)
3285 int ret
= 0, offset
= gpio_chip_hwgpio(desc
);
3287 CLASS(gpio_chip_guard
, guard
)(desc
);
3292 ret
= guard
.gc
->direction_input(guard
.gc
, offset
);
3294 ret
= guard
.gc
->direction_output(guard
.gc
, offset
, 0);
3296 set_bit(FLAG_IS_OUT
, &desc
->flags
);
3298 trace_gpio_direction(desc_to_gpio(desc
), value
, ret
);
3301 "%s: Error in set_value for open drain err %d\n",
3306 * _gpio_set_open_source_value() - Set the open source gpio's value.
3307 * @desc: gpio descriptor whose state need to be set.
3308 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3310 static void gpio_set_open_source_value_commit(struct gpio_desc
*desc
, bool value
)
3312 int ret
= 0, offset
= gpio_chip_hwgpio(desc
);
3314 CLASS(gpio_chip_guard
, guard
)(desc
);
3319 ret
= guard
.gc
->direction_output(guard
.gc
, offset
, 1);
3321 set_bit(FLAG_IS_OUT
, &desc
->flags
);
3323 ret
= guard
.gc
->direction_input(guard
.gc
, offset
);
3325 trace_gpio_direction(desc_to_gpio(desc
), !value
, ret
);
3328 "%s: Error in set_value for open source err %d\n",
3332 static void gpiod_set_raw_value_commit(struct gpio_desc
*desc
, bool value
)
3334 CLASS(gpio_chip_guard
, guard
)(desc
);
3338 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
3339 guard
.gc
->set(guard
.gc
, gpio_chip_hwgpio(desc
), value
);
3343 * set multiple outputs on the same chip;
3344 * use the chip's set_multiple function if available;
3345 * otherwise set the outputs sequentially;
3346 * @chip: the GPIO chip we operate on
3347 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
3348 * defines which outputs are to be changed
3349 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
3350 * defines the values the outputs specified by mask are to be set to
3352 static void gpio_chip_set_multiple(struct gpio_chip
*gc
,
3353 unsigned long *mask
, unsigned long *bits
)
3355 if (gc
->set_multiple
) {
3356 gc
->set_multiple(gc
, mask
, bits
);
3360 /* set outputs if the corresponding mask bit is set */
3361 for_each_set_bit(i
, mask
, gc
->ngpio
)
3362 gc
->set(gc
, i
, test_bit(i
, bits
));
3366 int gpiod_set_array_value_complex(bool raw
, bool can_sleep
,
3367 unsigned int array_size
,
3368 struct gpio_desc
**desc_array
,
3369 struct gpio_array
*array_info
,
3370 unsigned long *value_bitmap
)
3375 * Validate array_info against desc_array and its size.
3376 * It should immediately follow desc_array if both
3377 * have been obtained from the same gpiod_get_array() call.
3379 if (array_info
&& array_info
->desc
== desc_array
&&
3380 array_size
<= array_info
->size
&&
3381 (void *)array_info
== desc_array
+ array_info
->size
) {
3383 WARN_ON(array_info
->chip
->can_sleep
);
3385 if (!raw
&& !bitmap_empty(array_info
->invert_mask
, array_size
))
3386 bitmap_xor(value_bitmap
, value_bitmap
,
3387 array_info
->invert_mask
, array_size
);
3389 gpio_chip_set_multiple(array_info
->chip
, array_info
->set_mask
,
3392 i
= find_first_zero_bit(array_info
->set_mask
, array_size
);
3393 if (i
== array_size
)
3399 while (i
< array_size
) {
3400 DECLARE_BITMAP(fastpath_mask
, FASTPATH_NGPIO
);
3401 DECLARE_BITMAP(fastpath_bits
, FASTPATH_NGPIO
);
3402 unsigned long *mask
, *bits
;
3405 CLASS(gpio_chip_guard
, guard
)(desc_array
[i
]);
3409 if (likely(guard
.gc
->ngpio
<= FASTPATH_NGPIO
)) {
3410 mask
= fastpath_mask
;
3411 bits
= fastpath_bits
;
3413 gfp_t flags
= can_sleep
? GFP_KERNEL
: GFP_ATOMIC
;
3415 mask
= bitmap_alloc(guard
.gc
->ngpio
, flags
);
3419 bits
= bitmap_alloc(guard
.gc
->ngpio
, flags
);
3426 bitmap_zero(mask
, guard
.gc
->ngpio
);
3429 WARN_ON(guard
.gc
->can_sleep
);
3432 struct gpio_desc
*desc
= desc_array
[i
];
3433 int hwgpio
= gpio_chip_hwgpio(desc
);
3434 int value
= test_bit(i
, value_bitmap
);
3437 * Pins applicable for fast input but not for
3438 * fast output processing may have been already
3439 * inverted inside the fast path, skip them.
3441 if (!raw
&& !(array_info
&&
3442 test_bit(i
, array_info
->invert_mask
)) &&
3443 test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
3445 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
3447 * collect all normal outputs belonging to the same chip
3448 * open drain and open source outputs are set individually
3450 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
) && !raw
) {
3451 gpio_set_open_drain_value_commit(desc
, value
);
3452 } else if (test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
) && !raw
) {
3453 gpio_set_open_source_value_commit(desc
, value
);
3455 __set_bit(hwgpio
, mask
);
3456 __assign_bit(hwgpio
, bits
, value
);
3462 i
= find_next_zero_bit(array_info
->set_mask
,
3464 } while ((i
< array_size
) &&
3465 gpio_device_chip_cmp(desc_array
[i
]->gdev
, guard
.gc
));
3466 /* push collected bits to outputs */
3468 gpio_chip_set_multiple(guard
.gc
, mask
, bits
);
3470 if (mask
!= fastpath_mask
)
3472 if (bits
!= fastpath_bits
)
3479 * gpiod_set_raw_value() - assign a gpio's raw value
3480 * @desc: gpio whose value will be assigned
3481 * @value: value to assign
3483 * Set the raw value of the GPIO, i.e. the value of its physical line without
3484 * regard for its ACTIVE_LOW status.
3486 * This function can be called from contexts where we cannot sleep, and will
3487 * complain if the GPIO chip functions potentially sleep.
3489 void gpiod_set_raw_value(struct gpio_desc
*desc
, int value
)
3491 VALIDATE_DESC_VOID(desc
);
3492 /* Should be using gpiod_set_raw_value_cansleep() */
3493 WARN_ON(desc
->gdev
->can_sleep
);
3494 gpiod_set_raw_value_commit(desc
, value
);
3496 EXPORT_SYMBOL_GPL(gpiod_set_raw_value
);
3499 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3500 * @desc: the descriptor to set the value on
3501 * @value: value to set
3503 * This sets the value of a GPIO line backing a descriptor, applying
3504 * different semantic quirks like active low and open drain/source
3507 static void gpiod_set_value_nocheck(struct gpio_desc
*desc
, int value
)
3509 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
3511 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
))
3512 gpio_set_open_drain_value_commit(desc
, value
);
3513 else if (test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
))
3514 gpio_set_open_source_value_commit(desc
, value
);
3516 gpiod_set_raw_value_commit(desc
, value
);
3520 * gpiod_set_value() - assign a gpio's value
3521 * @desc: gpio whose value will be assigned
3522 * @value: value to assign
3524 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3525 * OPEN_DRAIN and OPEN_SOURCE flags into account.
3527 * This function can be called from contexts where we cannot sleep, and will
3528 * complain if the GPIO chip functions potentially sleep.
3530 void gpiod_set_value(struct gpio_desc
*desc
, int value
)
3532 VALIDATE_DESC_VOID(desc
);
3533 /* Should be using gpiod_set_value_cansleep() */
3534 WARN_ON(desc
->gdev
->can_sleep
);
3535 gpiod_set_value_nocheck(desc
, value
);
3537 EXPORT_SYMBOL_GPL(gpiod_set_value
);
3540 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
3541 * @array_size: number of elements in the descriptor array / value bitmap
3542 * @desc_array: array of GPIO descriptors whose values will be assigned
3543 * @array_info: information on applicability of fast bitmap processing path
3544 * @value_bitmap: bitmap of values to assign
3546 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3547 * without regard for their ACTIVE_LOW status.
3549 * This function can be called from contexts where we cannot sleep, and will
3550 * complain if the GPIO chip functions potentially sleep.
3553 * 0 on success, or negative errno on failure.
3555 int gpiod_set_raw_array_value(unsigned int array_size
,
3556 struct gpio_desc
**desc_array
,
3557 struct gpio_array
*array_info
,
3558 unsigned long *value_bitmap
)
3562 return gpiod_set_array_value_complex(true, false, array_size
,
3563 desc_array
, array_info
, value_bitmap
);
3565 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value
);
3568 * gpiod_set_array_value() - assign values to an array of GPIOs
3569 * @array_size: number of elements in the descriptor array / value bitmap
3570 * @desc_array: array of GPIO descriptors whose values will be assigned
3571 * @array_info: information on applicability of fast bitmap processing path
3572 * @value_bitmap: bitmap of values to assign
3574 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3577 * This function can be called from contexts where we cannot sleep, and will
3578 * complain if the GPIO chip functions potentially sleep.
3581 * 0 on success, or negative errno on failure.
3583 int gpiod_set_array_value(unsigned int array_size
,
3584 struct gpio_desc
**desc_array
,
3585 struct gpio_array
*array_info
,
3586 unsigned long *value_bitmap
)
3590 return gpiod_set_array_value_complex(false, false, array_size
,
3591 desc_array
, array_info
,
3594 EXPORT_SYMBOL_GPL(gpiod_set_array_value
);
3597 * gpiod_cansleep() - report whether gpio value access may sleep
3598 * @desc: gpio to check
3601 * 0 for non-sleepable, 1 for sleepable, or an error code in case of error.
3603 int gpiod_cansleep(const struct gpio_desc
*desc
)
3605 VALIDATE_DESC(desc
);
3606 return desc
->gdev
->can_sleep
;
3608 EXPORT_SYMBOL_GPL(gpiod_cansleep
);
3611 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3612 * @desc: gpio to set the consumer name on
3613 * @name: the new consumer name
3616 * 0 on success, or negative errno on failure.
3618 int gpiod_set_consumer_name(struct gpio_desc
*desc
, const char *name
)
3620 VALIDATE_DESC(desc
);
3622 return desc_set_label(desc
, name
);
3624 EXPORT_SYMBOL_GPL(gpiod_set_consumer_name
);
3627 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3628 * @desc: gpio whose IRQ will be returned (already requested)
3631 * The IRQ corresponding to the passed GPIO, or an error code in case of error.
3633 int gpiod_to_irq(const struct gpio_desc
*desc
)
3635 struct gpio_device
*gdev
;
3636 struct gpio_chip
*gc
;
3640 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3641 * requires this function to not return zero on an invalid descriptor
3642 * but rather a negative error number.
3644 if (IS_ERR_OR_NULL(desc
))
3648 /* FIXME Cannot use gpio_chip_guard due to const desc. */
3649 guard(srcu
)(&gdev
->srcu
);
3650 gc
= srcu_dereference(gdev
->chip
, &gdev
->srcu
);
3654 offset
= gpio_chip_hwgpio(desc
);
3656 int retirq
= gc
->to_irq(gc
, offset
);
3658 /* Zero means NO_IRQ */
3664 #ifdef CONFIG_GPIOLIB_IRQCHIP
3667 * Avoid race condition with other code, which tries to lookup
3668 * an IRQ before the irqchip has been properly registered,
3669 * i.e. while gpiochip is still being brought up.
3671 return -EPROBE_DEFER
;
3676 EXPORT_SYMBOL_GPL(gpiod_to_irq
);
3679 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3680 * @gc: the chip the GPIO to lock belongs to
3681 * @offset: the offset of the GPIO to lock as IRQ
3683 * This is used directly by GPIO drivers that want to lock down
3684 * a certain GPIO line to be used for IRQs.
3687 * 0 on success, or negative errno on failure.
3689 int gpiochip_lock_as_irq(struct gpio_chip
*gc
, unsigned int offset
)
3691 struct gpio_desc
*desc
;
3693 desc
= gpiochip_get_desc(gc
, offset
);
3695 return PTR_ERR(desc
);
3698 * If it's fast: flush the direction setting if something changed
3701 if (!gc
->can_sleep
&& gc
->get_direction
) {
3702 int dir
= gpiod_get_direction(desc
);
3705 chip_err(gc
, "%s: cannot get GPIO direction\n",
3711 /* To be valid for IRQ the line needs to be input or open drain */
3712 if (test_bit(FLAG_IS_OUT
, &desc
->flags
) &&
3713 !test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
)) {
3715 "%s: tried to flag a GPIO set as output for IRQ\n",
3720 set_bit(FLAG_USED_AS_IRQ
, &desc
->flags
);
3721 set_bit(FLAG_IRQ_IS_ENABLED
, &desc
->flags
);
3725 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq
);
3728 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3729 * @gc: the chip the GPIO to lock belongs to
3730 * @offset: the offset of the GPIO to lock as IRQ
3732 * This is used directly by GPIO drivers that want to indicate
3733 * that a certain GPIO is no longer used exclusively for IRQ.
3735 void gpiochip_unlock_as_irq(struct gpio_chip
*gc
, unsigned int offset
)
3737 struct gpio_desc
*desc
;
3739 desc
= gpiochip_get_desc(gc
, offset
);
3743 clear_bit(FLAG_USED_AS_IRQ
, &desc
->flags
);
3744 clear_bit(FLAG_IRQ_IS_ENABLED
, &desc
->flags
);
3746 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq
);
3748 void gpiochip_disable_irq(struct gpio_chip
*gc
, unsigned int offset
)
3750 struct gpio_desc
*desc
= gpiochip_get_desc(gc
, offset
);
3752 if (!IS_ERR(desc
) &&
3753 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ
, &desc
->flags
)))
3754 clear_bit(FLAG_IRQ_IS_ENABLED
, &desc
->flags
);
3756 EXPORT_SYMBOL_GPL(gpiochip_disable_irq
);
3758 void gpiochip_enable_irq(struct gpio_chip
*gc
, unsigned int offset
)
3760 struct gpio_desc
*desc
= gpiochip_get_desc(gc
, offset
);
3762 if (!IS_ERR(desc
) &&
3763 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ
, &desc
->flags
))) {
3765 * We must not be output when using IRQ UNLESS we are
3768 WARN_ON(test_bit(FLAG_IS_OUT
, &desc
->flags
) &&
3769 !test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
));
3770 set_bit(FLAG_IRQ_IS_ENABLED
, &desc
->flags
);
3773 EXPORT_SYMBOL_GPL(gpiochip_enable_irq
);
3775 bool gpiochip_line_is_irq(struct gpio_chip
*gc
, unsigned int offset
)
3777 if (offset
>= gc
->ngpio
)
3780 return test_bit(FLAG_USED_AS_IRQ
, &gc
->gpiodev
->descs
[offset
].flags
);
3782 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq
);
3784 int gpiochip_reqres_irq(struct gpio_chip
*gc
, unsigned int offset
)
3788 if (!try_module_get(gc
->gpiodev
->owner
))
3791 ret
= gpiochip_lock_as_irq(gc
, offset
);
3793 chip_err(gc
, "unable to lock HW IRQ %u for IRQ\n", offset
);
3794 module_put(gc
->gpiodev
->owner
);
3799 EXPORT_SYMBOL_GPL(gpiochip_reqres_irq
);
3801 void gpiochip_relres_irq(struct gpio_chip
*gc
, unsigned int offset
)
3803 gpiochip_unlock_as_irq(gc
, offset
);
3804 module_put(gc
->gpiodev
->owner
);
3806 EXPORT_SYMBOL_GPL(gpiochip_relres_irq
);
3808 bool gpiochip_line_is_open_drain(struct gpio_chip
*gc
, unsigned int offset
)
3810 if (offset
>= gc
->ngpio
)
3813 return test_bit(FLAG_OPEN_DRAIN
, &gc
->gpiodev
->descs
[offset
].flags
);
3815 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain
);
3817 bool gpiochip_line_is_open_source(struct gpio_chip
*gc
, unsigned int offset
)
3819 if (offset
>= gc
->ngpio
)
3822 return test_bit(FLAG_OPEN_SOURCE
, &gc
->gpiodev
->descs
[offset
].flags
);
3824 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source
);
3826 bool gpiochip_line_is_persistent(struct gpio_chip
*gc
, unsigned int offset
)
3828 if (offset
>= gc
->ngpio
)
3831 return !test_bit(FLAG_TRANSITORY
, &gc
->gpiodev
->descs
[offset
].flags
);
3833 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent
);
3836 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3837 * @desc: gpio whose value will be returned
3840 * The GPIO's raw value, i.e. the value of the physical line disregarding
3841 * its ACTIVE_LOW status, or negative errno on failure.
3843 * This function is to be called from contexts that can sleep.
3845 int gpiod_get_raw_value_cansleep(const struct gpio_desc
*desc
)
3848 VALIDATE_DESC(desc
);
3849 return gpiod_get_raw_value_commit(desc
);
3851 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep
);
3854 * gpiod_get_value_cansleep() - return a gpio's value
3855 * @desc: gpio whose value will be returned
3858 * The GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3859 * account, or negative errno on failure.
3861 * This function is to be called from contexts that can sleep.
3863 int gpiod_get_value_cansleep(const struct gpio_desc
*desc
)
3868 VALIDATE_DESC(desc
);
3869 value
= gpiod_get_raw_value_commit(desc
);
3873 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
3878 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep
);
3881 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3882 * @array_size: number of elements in the descriptor array / value bitmap
3883 * @desc_array: array of GPIO descriptors whose values will be read
3884 * @array_info: information on applicability of fast bitmap processing path
3885 * @value_bitmap: bitmap to store the read values
3887 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3888 * without regard for their ACTIVE_LOW status.
3890 * This function is to be called from contexts that can sleep.
3893 * 0 on success, or negative errno on failure.
3895 int gpiod_get_raw_array_value_cansleep(unsigned int array_size
,
3896 struct gpio_desc
**desc_array
,
3897 struct gpio_array
*array_info
,
3898 unsigned long *value_bitmap
)
3903 return gpiod_get_array_value_complex(true, true, array_size
,
3904 desc_array
, array_info
,
3907 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep
);
3910 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3911 * @array_size: number of elements in the descriptor array / value bitmap
3912 * @desc_array: array of GPIO descriptors whose values will be read
3913 * @array_info: information on applicability of fast bitmap processing path
3914 * @value_bitmap: bitmap to store the read values
3916 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3919 * This function is to be called from contexts that can sleep.
3922 * 0 on success, or negative errno on failure.
3924 int gpiod_get_array_value_cansleep(unsigned int array_size
,
3925 struct gpio_desc
**desc_array
,
3926 struct gpio_array
*array_info
,
3927 unsigned long *value_bitmap
)
3932 return gpiod_get_array_value_complex(false, true, array_size
,
3933 desc_array
, array_info
,
3936 EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep
);
3939 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3940 * @desc: gpio whose value will be assigned
3941 * @value: value to assign
3943 * Set the raw value of the GPIO, i.e. the value of its physical line without
3944 * regard for its ACTIVE_LOW status.
3946 * This function is to be called from contexts that can sleep.
3948 void gpiod_set_raw_value_cansleep(struct gpio_desc
*desc
, int value
)
3951 VALIDATE_DESC_VOID(desc
);
3952 gpiod_set_raw_value_commit(desc
, value
);
3954 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep
);
3957 * gpiod_set_value_cansleep() - assign a gpio's value
3958 * @desc: gpio whose value will be assigned
3959 * @value: value to assign
3961 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3964 * This function is to be called from contexts that can sleep.
3966 void gpiod_set_value_cansleep(struct gpio_desc
*desc
, int value
)
3969 VALIDATE_DESC_VOID(desc
);
3970 gpiod_set_value_nocheck(desc
, value
);
3972 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep
);
3975 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
3976 * @array_size: number of elements in the descriptor array / value bitmap
3977 * @desc_array: array of GPIO descriptors whose values will be assigned
3978 * @array_info: information on applicability of fast bitmap processing path
3979 * @value_bitmap: bitmap of values to assign
3981 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3982 * without regard for their ACTIVE_LOW status.
3984 * This function is to be called from contexts that can sleep.
3987 * 0 on success, or negative errno on failure.
3989 int gpiod_set_raw_array_value_cansleep(unsigned int array_size
,
3990 struct gpio_desc
**desc_array
,
3991 struct gpio_array
*array_info
,
3992 unsigned long *value_bitmap
)
3997 return gpiod_set_array_value_complex(true, true, array_size
, desc_array
,
3998 array_info
, value_bitmap
);
4000 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep
);
4003 * gpiod_add_lookup_tables() - register GPIO device consumers
4004 * @tables: list of tables of consumers to register
4005 * @n: number of tables in the list
4007 void gpiod_add_lookup_tables(struct gpiod_lookup_table
**tables
, size_t n
)
4011 mutex_lock(&gpio_lookup_lock
);
4013 for (i
= 0; i
< n
; i
++)
4014 list_add_tail(&tables
[i
]->list
, &gpio_lookup_list
);
4016 mutex_unlock(&gpio_lookup_lock
);
4020 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
4021 * @array_size: number of elements in the descriptor array / value bitmap
4022 * @desc_array: array of GPIO descriptors whose values will be assigned
4023 * @array_info: information on applicability of fast bitmap processing path
4024 * @value_bitmap: bitmap of values to assign
4026 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
4029 * This function is to be called from contexts that can sleep.
4032 * 0 on success, or negative errno on failure.
4034 int gpiod_set_array_value_cansleep(unsigned int array_size
,
4035 struct gpio_desc
**desc_array
,
4036 struct gpio_array
*array_info
,
4037 unsigned long *value_bitmap
)
4042 return gpiod_set_array_value_complex(false, true, array_size
,
4043 desc_array
, array_info
,
4046 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep
);
4048 void gpiod_line_state_notify(struct gpio_desc
*desc
, unsigned long action
)
4050 blocking_notifier_call_chain(&desc
->gdev
->line_state_notifier
,
4055 * gpiod_add_lookup_table() - register GPIO device consumers
4056 * @table: table of consumers to register
4058 void gpiod_add_lookup_table(struct gpiod_lookup_table
*table
)
4060 gpiod_add_lookup_tables(&table
, 1);
4062 EXPORT_SYMBOL_GPL(gpiod_add_lookup_table
);
4065 * gpiod_remove_lookup_table() - unregister GPIO device consumers
4066 * @table: table of consumers to unregister
4068 void gpiod_remove_lookup_table(struct gpiod_lookup_table
*table
)
4070 /* Nothing to remove */
4074 mutex_lock(&gpio_lookup_lock
);
4076 list_del(&table
->list
);
4078 mutex_unlock(&gpio_lookup_lock
);
4080 EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table
);
4083 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
4084 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
4086 void gpiod_add_hogs(struct gpiod_hog
*hogs
)
4088 struct gpiod_hog
*hog
;
4090 mutex_lock(&gpio_machine_hogs_mutex
);
4092 for (hog
= &hogs
[0]; hog
->chip_label
; hog
++) {
4093 list_add_tail(&hog
->list
, &gpio_machine_hogs
);
4096 * The chip may have been registered earlier, so check if it
4097 * exists and, if so, try to hog the line now.
4099 struct gpio_device
*gdev
__free(gpio_device_put
) =
4100 gpio_device_find_by_label(hog
->chip_label
);
4102 gpiochip_machine_hog(gpio_device_get_chip(gdev
), hog
);
4105 mutex_unlock(&gpio_machine_hogs_mutex
);
4107 EXPORT_SYMBOL_GPL(gpiod_add_hogs
);
4109 void gpiod_remove_hogs(struct gpiod_hog
*hogs
)
4111 struct gpiod_hog
*hog
;
4113 mutex_lock(&gpio_machine_hogs_mutex
);
4114 for (hog
= &hogs
[0]; hog
->chip_label
; hog
++)
4115 list_del(&hog
->list
);
4116 mutex_unlock(&gpio_machine_hogs_mutex
);
4118 EXPORT_SYMBOL_GPL(gpiod_remove_hogs
);
4120 static struct gpiod_lookup_table
*gpiod_find_lookup_table(struct device
*dev
)
4122 const char *dev_id
= dev
? dev_name(dev
) : NULL
;
4123 struct gpiod_lookup_table
*table
;
4125 list_for_each_entry(table
, &gpio_lookup_list
, list
) {
4126 if (table
->dev_id
&& dev_id
) {
4128 * Valid strings on both ends, must be identical to have
4131 if (!strcmp(table
->dev_id
, dev_id
))
4135 * One of the pointers is NULL, so both must be to have
4138 if (dev_id
== table
->dev_id
)
4146 static struct gpio_desc
*gpiod_find(struct device
*dev
, const char *con_id
,
4147 unsigned int idx
, unsigned long *flags
)
4149 struct gpio_desc
*desc
= ERR_PTR(-ENOENT
);
4150 struct gpiod_lookup_table
*table
;
4151 struct gpiod_lookup
*p
;
4152 struct gpio_chip
*gc
;
4154 guard(mutex
)(&gpio_lookup_lock
);
4156 table
= gpiod_find_lookup_table(dev
);
4160 for (p
= &table
->table
[0]; p
->key
; p
++) {
4161 /* idx must always match exactly */
4165 /* If the lookup entry has a con_id, require exact match */
4166 if (p
->con_id
&& (!con_id
|| strcmp(p
->con_id
, con_id
)))
4169 if (p
->chip_hwnum
== U16_MAX
) {
4170 desc
= gpio_name_to_desc(p
->key
);
4176 dev_warn(dev
, "cannot find GPIO line %s, deferring\n",
4178 return ERR_PTR(-EPROBE_DEFER
);
4181 struct gpio_device
*gdev
__free(gpio_device_put
) =
4182 gpio_device_find_by_label(p
->key
);
4185 * As the lookup table indicates a chip with
4186 * p->key should exist, assume it may
4187 * still appear later and let the interested
4188 * consumer be probed again or let the Deferred
4189 * Probe infrastructure handle the error.
4191 dev_warn(dev
, "cannot find GPIO chip %s, deferring\n",
4193 return ERR_PTR(-EPROBE_DEFER
);
4196 gc
= gpio_device_get_chip(gdev
);
4198 if (gc
->ngpio
<= p
->chip_hwnum
) {
4200 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
4201 idx
, p
->chip_hwnum
, gc
->ngpio
- 1,
4203 return ERR_PTR(-EINVAL
);
4206 desc
= gpio_device_get_desc(gdev
, p
->chip_hwnum
);
4215 static int platform_gpio_count(struct device
*dev
, const char *con_id
)
4217 struct gpiod_lookup_table
*table
;
4218 struct gpiod_lookup
*p
;
4219 unsigned int count
= 0;
4221 scoped_guard(mutex
, &gpio_lookup_lock
) {
4222 table
= gpiod_find_lookup_table(dev
);
4226 for (p
= &table
->table
[0]; p
->key
; p
++) {
4227 if ((con_id
&& p
->con_id
&& !strcmp(con_id
, p
->con_id
)) ||
4228 (!con_id
&& !p
->con_id
))
4239 static struct gpio_desc
*gpiod_find_by_fwnode(struct fwnode_handle
*fwnode
,
4240 struct device
*consumer
,
4243 enum gpiod_flags
*flags
,
4244 unsigned long *lookupflags
)
4246 const char *name
= function_name_or_default(con_id
);
4247 struct gpio_desc
*desc
= ERR_PTR(-ENOENT
);
4249 if (is_of_node(fwnode
)) {
4250 dev_dbg(consumer
, "using DT '%pfw' for '%s' GPIO lookup\n", fwnode
, name
);
4251 desc
= of_find_gpio(to_of_node(fwnode
), con_id
, idx
, lookupflags
);
4252 } else if (is_acpi_node(fwnode
)) {
4253 dev_dbg(consumer
, "using ACPI '%pfw' for '%s' GPIO lookup\n", fwnode
, name
);
4254 desc
= acpi_find_gpio(fwnode
, con_id
, idx
, flags
, lookupflags
);
4255 } else if (is_software_node(fwnode
)) {
4256 dev_dbg(consumer
, "using swnode '%pfw' for '%s' GPIO lookup\n", fwnode
, name
);
4257 desc
= swnode_find_gpio(fwnode
, con_id
, idx
, lookupflags
);
4263 struct gpio_desc
*gpiod_find_and_request(struct device
*consumer
,
4264 struct fwnode_handle
*fwnode
,
4267 enum gpiod_flags flags
,
4269 bool platform_lookup_allowed
)
4271 unsigned long lookupflags
= GPIO_LOOKUP_FLAGS_DEFAULT
;
4272 const char *name
= function_name_or_default(con_id
);
4274 * scoped_guard() is implemented as a for loop, meaning static
4275 * analyzers will complain about these two not being initialized.
4277 struct gpio_desc
*desc
= NULL
;
4280 scoped_guard(srcu
, &gpio_devices_srcu
) {
4281 desc
= gpiod_find_by_fwnode(fwnode
, consumer
, con_id
, idx
,
4282 &flags
, &lookupflags
);
4283 if (gpiod_not_found(desc
) && platform_lookup_allowed
) {
4285 * Either we are not using DT or ACPI, or their lookup
4286 * did not return a result. In that case, use platform
4287 * lookup as a fallback.
4290 "using lookup tables for GPIO lookup\n");
4291 desc
= gpiod_find(consumer
, con_id
, idx
, &lookupflags
);
4295 dev_dbg(consumer
, "No GPIO consumer %s found\n", name
);
4300 * If a connection label was passed use that, else attempt to use
4301 * the device name as label
4303 ret
= gpiod_request(desc
, label
);
4306 if (!(ret
== -EBUSY
&& flags
& GPIOD_FLAGS_BIT_NONEXCLUSIVE
))
4307 return ERR_PTR(ret
);
4310 * This happens when there are several consumers for
4311 * the same GPIO line: we just return here without
4312 * further initialization. It is a bit of a hack.
4313 * This is necessary to support fixed regulators.
4315 * FIXME: Make this more sane and safe.
4317 dev_info(consumer
, "nonexclusive access to GPIO for %s\n", name
);
4321 ret
= gpiod_configure_flags(desc
, con_id
, lookupflags
, flags
);
4324 dev_err(consumer
, "setup of GPIO %s failed: %d\n", name
, ret
);
4325 return ERR_PTR(ret
);
4328 gpiod_line_state_notify(desc
, GPIOLINE_CHANGED_REQUESTED
);
4334 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
4335 * @fwnode: handle of the firmware node
4336 * @con_id: function within the GPIO consumer
4337 * @index: index of the GPIO to obtain for the consumer
4338 * @flags: GPIO initialization flags
4339 * @label: label to attach to the requested GPIO
4341 * This function can be used for drivers that get their configuration
4342 * from opaque firmware.
4344 * The function properly finds the corresponding GPIO using whatever is the
4345 * underlying firmware interface and then makes sure that the GPIO
4346 * descriptor is requested before it is returned to the caller.
4349 * On successful request the GPIO pin is configured in accordance with
4352 * In case of error an ERR_PTR() is returned.
4354 struct gpio_desc
*fwnode_gpiod_get_index(struct fwnode_handle
*fwnode
,
4357 enum gpiod_flags flags
,
4360 return gpiod_find_and_request(NULL
, fwnode
, con_id
, index
, flags
, label
, false);
4362 EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index
);
4365 * gpiod_count - return the number of GPIOs associated with a device / function
4366 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4367 * @con_id: function within the GPIO consumer
4370 * The number of GPIOs associated with a device / function or -ENOENT if no
4371 * GPIO has been assigned to the requested function.
4373 int gpiod_count(struct device
*dev
, const char *con_id
)
4375 const struct fwnode_handle
*fwnode
= dev
? dev_fwnode(dev
) : NULL
;
4376 int count
= -ENOENT
;
4378 if (is_of_node(fwnode
))
4379 count
= of_gpio_count(fwnode
, con_id
);
4380 else if (is_acpi_node(fwnode
))
4381 count
= acpi_gpio_count(fwnode
, con_id
);
4382 else if (is_software_node(fwnode
))
4383 count
= swnode_gpio_count(fwnode
, con_id
);
4386 count
= platform_gpio_count(dev
, con_id
);
4390 EXPORT_SYMBOL_GPL(gpiod_count
);
4393 * gpiod_get - obtain a GPIO for a given GPIO function
4394 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4395 * @con_id: function within the GPIO consumer
4396 * @flags: optional GPIO initialization flags
4399 * The GPIO descriptor corresponding to the function @con_id of device
4400 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
4401 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
4403 struct gpio_desc
*__must_check
gpiod_get(struct device
*dev
, const char *con_id
,
4404 enum gpiod_flags flags
)
4406 return gpiod_get_index(dev
, con_id
, 0, flags
);
4408 EXPORT_SYMBOL_GPL(gpiod_get
);
4411 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
4412 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4413 * @con_id: function within the GPIO consumer
4414 * @flags: optional GPIO initialization flags
4416 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
4417 * the requested function it will return NULL. This is convenient for drivers
4418 * that need to handle optional GPIOs.
4421 * The GPIO descriptor corresponding to the function @con_id of device
4422 * dev, NULL if no GPIO has been assigned to the requested function, or
4423 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
4425 struct gpio_desc
*__must_check
gpiod_get_optional(struct device
*dev
,
4427 enum gpiod_flags flags
)
4429 return gpiod_get_index_optional(dev
, con_id
, 0, flags
);
4431 EXPORT_SYMBOL_GPL(gpiod_get_optional
);
4435 * gpiod_configure_flags - helper function to configure a given GPIO
4436 * @desc: gpio whose value will be assigned
4437 * @con_id: function within the GPIO consumer
4438 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4439 * of_find_gpio() or of_get_gpio_hog()
4440 * @dflags: gpiod_flags - optional GPIO initialization flags
4443 * 0 on success, -ENOENT if no GPIO has been assigned to the
4444 * requested function and/or index, or another IS_ERR() code if an error
4445 * occurred while trying to acquire the GPIO.
4447 int gpiod_configure_flags(struct gpio_desc
*desc
, const char *con_id
,
4448 unsigned long lflags
, enum gpiod_flags dflags
)
4450 const char *name
= function_name_or_default(con_id
);
4453 if (lflags
& GPIO_ACTIVE_LOW
)
4454 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
4456 if (lflags
& GPIO_OPEN_DRAIN
)
4457 set_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
4458 else if (dflags
& GPIOD_FLAGS_BIT_OPEN_DRAIN
) {
4460 * This enforces open drain mode from the consumer side.
4461 * This is necessary for some busses like I2C, but the lookup
4462 * should *REALLY* have specified them as open drain in the
4463 * first place, so print a little warning here.
4465 set_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
4467 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
4470 if (lflags
& GPIO_OPEN_SOURCE
)
4471 set_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
4473 if (((lflags
& GPIO_PULL_UP
) && (lflags
& GPIO_PULL_DOWN
)) ||
4474 ((lflags
& GPIO_PULL_UP
) && (lflags
& GPIO_PULL_DISABLE
)) ||
4475 ((lflags
& GPIO_PULL_DOWN
) && (lflags
& GPIO_PULL_DISABLE
))) {
4477 "multiple pull-up, pull-down or pull-disable enabled, invalid configuration\n");
4481 if (lflags
& GPIO_PULL_UP
)
4482 set_bit(FLAG_PULL_UP
, &desc
->flags
);
4483 else if (lflags
& GPIO_PULL_DOWN
)
4484 set_bit(FLAG_PULL_DOWN
, &desc
->flags
);
4485 else if (lflags
& GPIO_PULL_DISABLE
)
4486 set_bit(FLAG_BIAS_DISABLE
, &desc
->flags
);
4488 ret
= gpiod_set_transitory(desc
, (lflags
& GPIO_TRANSITORY
));
4492 /* No particular flag request, return here... */
4493 if (!(dflags
& GPIOD_FLAGS_BIT_DIR_SET
)) {
4494 gpiod_dbg(desc
, "no flags found for GPIO %s\n", name
);
4499 if (dflags
& GPIOD_FLAGS_BIT_DIR_OUT
)
4500 ret
= gpiod_direction_output(desc
,
4501 !!(dflags
& GPIOD_FLAGS_BIT_DIR_VAL
));
4503 ret
= gpiod_direction_input(desc
);
4509 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
4510 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4511 * @con_id: function within the GPIO consumer
4512 * @idx: index of the GPIO to obtain in the consumer
4513 * @flags: optional GPIO initialization flags
4515 * This variant of gpiod_get() allows to access GPIOs other than the first
4516 * defined one for functions that define several GPIOs.
4519 * A valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
4520 * requested function and/or index, or another IS_ERR() code if an error
4521 * occurred while trying to acquire the GPIO.
4523 struct gpio_desc
*__must_check
gpiod_get_index(struct device
*dev
,
4526 enum gpiod_flags flags
)
4528 struct fwnode_handle
*fwnode
= dev
? dev_fwnode(dev
) : NULL
;
4529 const char *devname
= dev
? dev_name(dev
) : "?";
4530 const char *label
= con_id
?: devname
;
4532 return gpiod_find_and_request(dev
, fwnode
, con_id
, idx
, flags
, label
, true);
4534 EXPORT_SYMBOL_GPL(gpiod_get_index
);
4537 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
4539 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4540 * @con_id: function within the GPIO consumer
4541 * @index: index of the GPIO to obtain in the consumer
4542 * @flags: optional GPIO initialization flags
4544 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
4545 * specified index was assigned to the requested function it will return NULL.
4546 * This is convenient for drivers that need to handle optional GPIOs.
4549 * A valid GPIO descriptor, NULL if no GPIO has been assigned to the
4550 * requested function and/or index, or another IS_ERR() code if an error
4551 * occurred while trying to acquire the GPIO.
4553 struct gpio_desc
*__must_check
gpiod_get_index_optional(struct device
*dev
,
4556 enum gpiod_flags flags
)
4558 struct gpio_desc
*desc
;
4560 desc
= gpiod_get_index(dev
, con_id
, index
, flags
);
4561 if (gpiod_not_found(desc
))
4566 EXPORT_SYMBOL_GPL(gpiod_get_index_optional
);
4569 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4570 * @desc: gpio whose value will be assigned
4571 * @name: gpio line name
4572 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4573 * of_find_gpio() or of_get_gpio_hog()
4574 * @dflags: gpiod_flags - optional GPIO initialization flags
4577 * 0 on success, or negative errno on failure.
4579 int gpiod_hog(struct gpio_desc
*desc
, const char *name
,
4580 unsigned long lflags
, enum gpiod_flags dflags
)
4582 struct gpio_device
*gdev
= desc
->gdev
;
4583 struct gpio_desc
*local_desc
;
4587 CLASS(gpio_chip_guard
, guard
)(desc
);
4591 if (test_and_set_bit(FLAG_IS_HOGGED
, &desc
->flags
))
4594 hwnum
= gpio_chip_hwgpio(desc
);
4596 local_desc
= gpiochip_request_own_desc(guard
.gc
, hwnum
, name
,
4598 if (IS_ERR(local_desc
)) {
4599 clear_bit(FLAG_IS_HOGGED
, &desc
->flags
);
4600 ret
= PTR_ERR(local_desc
);
4601 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
4602 name
, gdev
->label
, hwnum
, ret
);
4606 gpiod_dbg(desc
, "hogged as %s%s\n",
4607 (dflags
& GPIOD_FLAGS_BIT_DIR_OUT
) ? "output" : "input",
4608 (dflags
& GPIOD_FLAGS_BIT_DIR_OUT
) ?
4609 (dflags
& GPIOD_FLAGS_BIT_DIR_VAL
) ? "/high" : "/low" : "");
4615 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4616 * @gc: gpio chip to act on
4618 static void gpiochip_free_hogs(struct gpio_chip
*gc
)
4620 struct gpio_desc
*desc
;
4622 for_each_gpio_desc_with_flag(gc
, desc
, FLAG_IS_HOGGED
)
4623 gpiochip_free_own_desc(desc
);
4627 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4628 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4629 * @con_id: function within the GPIO consumer
4630 * @flags: optional GPIO initialization flags
4632 * This function acquires all the GPIOs defined under a given function.
4635 * The GPIO descriptors corresponding to the function @con_id of device
4636 * dev, -ENOENT if no GPIO has been assigned to the requested function,
4637 * or another IS_ERR() code if an error occurred while trying to acquire
4640 struct gpio_descs
*__must_check
gpiod_get_array(struct device
*dev
,
4642 enum gpiod_flags flags
)
4644 struct gpio_desc
*desc
;
4645 struct gpio_descs
*descs
;
4646 struct gpio_array
*array_info
= NULL
;
4647 struct gpio_chip
*gc
;
4648 int count
, bitmap_size
;
4651 count
= gpiod_count(dev
, con_id
);
4653 return ERR_PTR(count
);
4655 descs_size
= struct_size(descs
, desc
, count
);
4656 descs
= kzalloc(descs_size
, GFP_KERNEL
);
4658 return ERR_PTR(-ENOMEM
);
4660 for (descs
->ndescs
= 0; descs
->ndescs
< count
; descs
->ndescs
++) {
4661 desc
= gpiod_get_index(dev
, con_id
, descs
->ndescs
, flags
);
4663 gpiod_put_array(descs
);
4664 return ERR_CAST(desc
);
4667 descs
->desc
[descs
->ndescs
] = desc
;
4669 gc
= gpiod_to_chip(desc
);
4671 * If pin hardware number of array member 0 is also 0, select
4672 * its chip as a candidate for fast bitmap processing path.
4674 if (descs
->ndescs
== 0 && gpio_chip_hwgpio(desc
) == 0) {
4675 struct gpio_descs
*array
;
4677 bitmap_size
= BITS_TO_LONGS(gc
->ngpio
> count
?
4680 array
= krealloc(descs
, descs_size
+
4681 struct_size(array_info
, invert_mask
, 3 * bitmap_size
),
4682 GFP_KERNEL
| __GFP_ZERO
);
4684 gpiod_put_array(descs
);
4685 return ERR_PTR(-ENOMEM
);
4690 array_info
= (void *)descs
+ descs_size
;
4691 array_info
->get_mask
= array_info
->invert_mask
+
4693 array_info
->set_mask
= array_info
->get_mask
+
4696 array_info
->desc
= descs
->desc
;
4697 array_info
->size
= count
;
4698 array_info
->chip
= gc
;
4699 bitmap_set(array_info
->get_mask
, descs
->ndescs
,
4700 count
- descs
->ndescs
);
4701 bitmap_set(array_info
->set_mask
, descs
->ndescs
,
4702 count
- descs
->ndescs
);
4703 descs
->info
= array_info
;
4706 /* If there is no cache for fast bitmap processing path, continue */
4710 /* Unmark array members which don't belong to the 'fast' chip */
4711 if (array_info
->chip
!= gc
) {
4712 __clear_bit(descs
->ndescs
, array_info
->get_mask
);
4713 __clear_bit(descs
->ndescs
, array_info
->set_mask
);
4716 * Detect array members which belong to the 'fast' chip
4717 * but their pins are not in hardware order.
4719 else if (gpio_chip_hwgpio(desc
) != descs
->ndescs
) {
4721 * Don't use fast path if all array members processed so
4722 * far belong to the same chip as this one but its pin
4723 * hardware number is different from its array index.
4725 if (bitmap_full(array_info
->get_mask
, descs
->ndescs
)) {
4728 __clear_bit(descs
->ndescs
,
4729 array_info
->get_mask
);
4730 __clear_bit(descs
->ndescs
,
4731 array_info
->set_mask
);
4734 /* Exclude open drain or open source from fast output */
4735 if (gpiochip_line_is_open_drain(gc
, descs
->ndescs
) ||
4736 gpiochip_line_is_open_source(gc
, descs
->ndescs
))
4737 __clear_bit(descs
->ndescs
,
4738 array_info
->set_mask
);
4739 /* Identify 'fast' pins which require invertion */
4740 if (gpiod_is_active_low(desc
))
4741 __set_bit(descs
->ndescs
,
4742 array_info
->invert_mask
);
4747 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4748 array_info
->chip
->label
, array_info
->size
,
4749 *array_info
->get_mask
, *array_info
->set_mask
,
4750 *array_info
->invert_mask
);
4753 EXPORT_SYMBOL_GPL(gpiod_get_array
);
4756 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4758 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4759 * @con_id: function within the GPIO consumer
4760 * @flags: optional GPIO initialization flags
4762 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4763 * assigned to the requested function it will return NULL.
4766 * The GPIO descriptors corresponding to the function @con_id of device
4767 * dev, NULL if no GPIO has been assigned to the requested function,
4768 * or another IS_ERR() code if an error occurred while trying to acquire
4771 struct gpio_descs
*__must_check
gpiod_get_array_optional(struct device
*dev
,
4773 enum gpiod_flags flags
)
4775 struct gpio_descs
*descs
;
4777 descs
= gpiod_get_array(dev
, con_id
, flags
);
4778 if (gpiod_not_found(descs
))
4783 EXPORT_SYMBOL_GPL(gpiod_get_array_optional
);
4786 * gpiod_put - dispose of a GPIO descriptor
4787 * @desc: GPIO descriptor to dispose of
4789 * No descriptor can be used after gpiod_put() has been called on it.
4791 void gpiod_put(struct gpio_desc
*desc
)
4796 EXPORT_SYMBOL_GPL(gpiod_put
);
4799 * gpiod_put_array - dispose of multiple GPIO descriptors
4800 * @descs: struct gpio_descs containing an array of descriptors
4802 void gpiod_put_array(struct gpio_descs
*descs
)
4806 for (i
= 0; i
< descs
->ndescs
; i
++)
4807 gpiod_put(descs
->desc
[i
]);
4811 EXPORT_SYMBOL_GPL(gpiod_put_array
);
4813 static int gpio_stub_drv_probe(struct device
*dev
)
4816 * The DT node of some GPIO chips have a "compatible" property, but
4817 * never have a struct device added and probed by a driver to register
4818 * the GPIO chip with gpiolib. In such cases, fw_devlink=on will cause
4819 * the consumers of the GPIO chip to get probe deferred forever because
4820 * they will be waiting for a device associated with the GPIO chip
4821 * firmware node to get added and bound to a driver.
4823 * To allow these consumers to probe, we associate the struct
4824 * gpio_device of the GPIO chip with the firmware node and then simply
4825 * bind it to this stub driver.
4830 static struct device_driver gpio_stub_drv
= {
4831 .name
= "gpio_stub_drv",
4832 .bus
= &gpio_bus_type
,
4833 .probe
= gpio_stub_drv_probe
,
4836 static int __init
gpiolib_dev_init(void)
4840 /* Register GPIO sysfs bus */
4841 ret
= bus_register(&gpio_bus_type
);
4843 pr_err("gpiolib: could not register GPIO bus type\n");
4847 ret
= driver_register(&gpio_stub_drv
);
4849 pr_err("gpiolib: could not register GPIO stub driver\n");
4850 bus_unregister(&gpio_bus_type
);
4854 ret
= alloc_chrdev_region(&gpio_devt
, 0, GPIO_DEV_MAX
, GPIOCHIP_NAME
);
4856 pr_err("gpiolib: failed to allocate char dev region\n");
4857 driver_unregister(&gpio_stub_drv
);
4858 bus_unregister(&gpio_bus_type
);
4862 gpiolib_initialized
= true;
4863 gpiochip_setup_devs();
4865 #if IS_ENABLED(CONFIG_OF_DYNAMIC) && IS_ENABLED(CONFIG_OF_GPIO)
4866 WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier
));
4867 #endif /* CONFIG_OF_DYNAMIC && CONFIG_OF_GPIO */
4871 core_initcall(gpiolib_dev_init
);
4873 #ifdef CONFIG_DEBUG_FS
4875 static void gpiolib_dbg_show(struct seq_file
*s
, struct gpio_device
*gdev
)
4877 bool active_low
, is_irq
, is_out
;
4878 unsigned int gpio
= gdev
->base
;
4879 struct gpio_desc
*desc
;
4880 struct gpio_chip
*gc
;
4883 guard(srcu
)(&gdev
->srcu
);
4885 gc
= srcu_dereference(gdev
->chip
, &gdev
->srcu
);
4887 seq_puts(s
, "Underlying GPIO chip is gone\n");
4891 for_each_gpio_desc(gc
, desc
) {
4892 guard(srcu
)(&desc
->gdev
->desc_srcu
);
4893 is_irq
= test_bit(FLAG_USED_AS_IRQ
, &desc
->flags
);
4894 if (is_irq
|| test_bit(FLAG_REQUESTED
, &desc
->flags
)) {
4895 gpiod_get_direction(desc
);
4896 is_out
= test_bit(FLAG_IS_OUT
, &desc
->flags
);
4897 value
= gpio_chip_get_value(gc
, desc
);
4898 active_low
= test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
4899 seq_printf(s
, " gpio-%-3u (%-20.20s|%-20.20s) %s %s %s%s\n",
4900 gpio
, desc
->name
?: "", gpiod_get_label(desc
),
4901 is_out
? "out" : "in ",
4902 value
>= 0 ? (value
? "hi" : "lo") : "? ",
4903 is_irq
? "IRQ " : "",
4904 active_low
? "ACTIVE LOW" : "");
4905 } else if (desc
->name
) {
4906 seq_printf(s
, " gpio-%-3u (%-20.20s)\n", gpio
, desc
->name
);
4913 struct gpiolib_seq_priv
{
4918 static void *gpiolib_seq_start(struct seq_file
*s
, loff_t
*pos
)
4920 struct gpiolib_seq_priv
*priv
;
4921 struct gpio_device
*gdev
;
4922 loff_t index
= *pos
;
4924 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
4929 priv
->idx
= srcu_read_lock(&gpio_devices_srcu
);
4931 list_for_each_entry_srcu(gdev
, &gpio_devices
, list
,
4932 srcu_read_lock_held(&gpio_devices_srcu
)) {
4940 static void *gpiolib_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
4942 struct gpiolib_seq_priv
*priv
= s
->private;
4943 struct gpio_device
*gdev
= v
, *next
;
4945 next
= list_entry_rcu(gdev
->list
.next
, struct gpio_device
, list
);
4946 gdev
= &next
->list
== &gpio_devices
? NULL
: next
;
4947 priv
->newline
= true;
4953 static void gpiolib_seq_stop(struct seq_file
*s
, void *v
)
4955 struct gpiolib_seq_priv
*priv
= s
->private;
4957 srcu_read_unlock(&gpio_devices_srcu
, priv
->idx
);
4961 static int gpiolib_seq_show(struct seq_file
*s
, void *v
)
4963 struct gpiolib_seq_priv
*priv
= s
->private;
4964 struct gpio_device
*gdev
= v
;
4965 struct gpio_chip
*gc
;
4966 struct device
*parent
;
4968 guard(srcu
)(&gdev
->srcu
);
4970 gc
= srcu_dereference(gdev
->chip
, &gdev
->srcu
);
4972 seq_printf(s
, "%s%s: (dangling chip)",
4973 priv
->newline
? "\n" : "",
4974 dev_name(&gdev
->dev
));
4978 seq_printf(s
, "%s%s: GPIOs %u-%u", priv
->newline
? "\n" : "",
4979 dev_name(&gdev
->dev
),
4980 gdev
->base
, gdev
->base
+ gdev
->ngpio
- 1);
4981 parent
= gc
->parent
;
4983 seq_printf(s
, ", parent: %s/%s",
4984 parent
->bus
? parent
->bus
->name
: "no-bus",
4987 seq_printf(s
, ", %s", gc
->label
);
4989 seq_printf(s
, ", can sleep");
4990 seq_printf(s
, ":\n");
4993 gc
->dbg_show(s
, gc
);
4995 gpiolib_dbg_show(s
, gdev
);
5000 static const struct seq_operations gpiolib_sops
= {
5001 .start
= gpiolib_seq_start
,
5002 .next
= gpiolib_seq_next
,
5003 .stop
= gpiolib_seq_stop
,
5004 .show
= gpiolib_seq_show
,
5006 DEFINE_SEQ_ATTRIBUTE(gpiolib
);
5008 static int __init
gpiolib_debugfs_init(void)
5010 /* /sys/kernel/debug/gpio */
5011 debugfs_create_file("gpio", 0444, NULL
, NULL
, &gpiolib_fops
);
5014 subsys_initcall(gpiolib_debugfs_init
);
5016 #endif /* DEBUG_FS */