xref: /linux/drivers/gpio/gpiolib-shared.h (revision c31f4aa8fed048fa70e742c4bb49bb48dc489ab3)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #ifndef __LINUX_GPIO_SHARED_H
4 #define __LINUX_GPIO_SHARED_H
5 
6 #include <linux/cleanup.h>
7 #include <linux/lockdep.h>
8 #include <linux/mutex.h>
9 #include <linux/spinlock.h>
10 
11 struct gpio_device;
12 struct gpio_desc;
13 struct device;
14 
15 #if IS_ENABLED(CONFIG_GPIO_SHARED)
16 
17 int gpio_device_setup_shared(struct gpio_device *gdev);
18 void gpio_device_teardown_shared(struct gpio_device *gdev);
19 int gpio_shared_add_proxy_lookup(struct device *consumer, unsigned long lflags);
20 
21 #else
22 
23 static inline int gpio_device_setup_shared(struct gpio_device *gdev)
24 {
25 	return 0;
26 }
27 
28 static inline void gpio_device_teardown_shared(struct gpio_device *gdev) { }
29 
30 static inline int gpio_shared_add_proxy_lookup(struct device *consumer,
31 					       unsigned long lflags)
32 {
33 	return 0;
34 }
35 
36 #endif /* CONFIG_GPIO_SHARED */
37 
38 struct gpio_shared_desc {
39 	struct gpio_desc *desc;
40 	bool can_sleep;
41 	unsigned long cfg;
42 	unsigned int usecnt;
43 	unsigned int highcnt;
44 	union {
45 		struct mutex mutex;
46 		spinlock_t spinlock;
47 	};
48 };
49 
50 struct gpio_shared_desc *devm_gpiod_shared_get(struct device *dev);
51 
52 DEFINE_LOCK_GUARD_1(gpio_shared_desc_lock, struct gpio_shared_desc,
53 	if (_T->lock->can_sleep)
54 		mutex_lock(&_T->lock->mutex);
55 	else
56 		spin_lock_irqsave(&_T->lock->spinlock, _T->flags),
57 	if (_T->lock->can_sleep)
58 		mutex_unlock(&_T->lock->mutex);
59 	else
60 		spin_unlock_irqrestore(&_T->lock->spinlock, _T->flags),
61 	unsigned long flags)
62 
63 static inline void gpio_shared_lockdep_assert(struct gpio_shared_desc *shared_desc)
64 {
65 	if (shared_desc->can_sleep)
66 		lockdep_assert_held(&shared_desc->mutex);
67 	else
68 		lockdep_assert_held(&shared_desc->spinlock);
69 }
70 
71 #endif /* __LINUX_GPIO_SHARED_H */
72