xref: /linux/drivers/gpio/gpiolib-shared.h (revision 116a3308e1eb0ea59aa248e22ac29d65eb7cd250)
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 struct fwnode_handle;
15 
16 #if IS_ENABLED(CONFIG_GPIO_SHARED)
17 
18 int gpiochip_setup_shared(struct gpio_chip *gc);
19 void gpio_device_teardown_shared(struct gpio_device *gdev);
20 int gpio_shared_add_proxy_lookup(struct device *consumer,
21 				 struct fwnode_handle *fwnode,
22 				 const char *con_id, unsigned long lflags);
23 
24 #else
25 
gpiochip_setup_shared(struct gpio_chip * gc)26 static inline int gpiochip_setup_shared(struct gpio_chip *gc)
27 {
28 	return 0;
29 }
30 
gpio_device_teardown_shared(struct gpio_device * gdev)31 static inline void gpio_device_teardown_shared(struct gpio_device *gdev) { }
32 
gpio_shared_add_proxy_lookup(struct device * consumer,struct fwnode_handle * fwnode,const char * con_id,unsigned long lflags)33 static inline int gpio_shared_add_proxy_lookup(struct device *consumer,
34 					       struct fwnode_handle *fwnode,
35 					       const char *con_id,
36 					       unsigned long lflags)
37 {
38 	return 0;
39 }
40 
41 #endif /* CONFIG_GPIO_SHARED */
42 
43 struct gpio_shared_desc {
44 	struct gpio_desc *desc;
45 	bool can_sleep;
46 	unsigned long cfg;
47 	unsigned int usecnt;
48 	unsigned int highcnt;
49 	union {
50 		struct mutex mutex;
51 		spinlock_t spinlock;
52 	};
53 };
54 
55 struct gpio_shared_desc *devm_gpiod_shared_get(struct device *dev);
56 
57 DEFINE_LOCK_GUARD_1(gpio_shared_desc_lock, struct gpio_shared_desc,
58 	if (_T->lock->can_sleep)
59 		mutex_lock(&_T->lock->mutex);
60 	else
61 		spin_lock_irqsave(&_T->lock->spinlock, _T->flags),
62 	if (_T->lock->can_sleep)
63 		mutex_unlock(&_T->lock->mutex);
64 	else
65 		spin_unlock_irqrestore(&_T->lock->spinlock, _T->flags),
66 	unsigned long flags)
67 
gpio_shared_lockdep_assert(struct gpio_shared_desc * shared_desc)68 static inline void gpio_shared_lockdep_assert(struct gpio_shared_desc *shared_desc)
69 {
70 	if (shared_desc->can_sleep)
71 		lockdep_assert_held(&shared_desc->mutex);
72 	else
73 		lockdep_assert_held(&shared_desc->spinlock);
74 }
75 
76 #endif /* __LINUX_GPIO_SHARED_H */
77