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