xref: /linux/drivers/base/power/power.h (revision 164666fa66669d437bdcc8d5f1744a2aee73be41)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <linux/pm_qos.h>
3 
4 static inline void device_pm_init_common(struct device *dev)
5 {
6 	if (!dev->power.early_init) {
7 		spin_lock_init(&dev->power.lock);
8 		dev->power.qos = NULL;
9 		dev->power.early_init = true;
10 	}
11 }
12 
13 #ifdef CONFIG_PM
14 
15 static inline void pm_runtime_early_init(struct device *dev)
16 {
17 	dev->power.disable_depth = 1;
18 	device_pm_init_common(dev);
19 }
20 
21 extern void pm_runtime_init(struct device *dev);
22 extern void pm_runtime_reinit(struct device *dev);
23 extern void pm_runtime_remove(struct device *dev);
24 extern u64 pm_runtime_active_time(struct device *dev);
25 
26 #define WAKE_IRQ_DEDICATED_ALLOCATED	BIT(0)
27 #define WAKE_IRQ_DEDICATED_MANAGED	BIT(1)
28 #define WAKE_IRQ_DEDICATED_REVERSE	BIT(2)
29 #define WAKE_IRQ_DEDICATED_MASK		(WAKE_IRQ_DEDICATED_ALLOCATED | \
30 					 WAKE_IRQ_DEDICATED_MANAGED | \
31 					 WAKE_IRQ_DEDICATED_REVERSE)
32 
33 struct wake_irq {
34 	struct device *dev;
35 	unsigned int status;
36 	int irq;
37 	const char *name;
38 };
39 
40 extern void dev_pm_arm_wake_irq(struct wake_irq *wirq);
41 extern void dev_pm_disarm_wake_irq(struct wake_irq *wirq);
42 extern void dev_pm_enable_wake_irq_check(struct device *dev,
43 					 bool can_change_status);
44 extern void dev_pm_disable_wake_irq_check(struct device *dev, bool cond_disable);
45 extern void dev_pm_enable_wake_irq_complete(struct device *dev);
46 
47 #ifdef CONFIG_PM_SLEEP
48 
49 extern void device_wakeup_attach_irq(struct device *dev, struct wake_irq *wakeirq);
50 extern void device_wakeup_detach_irq(struct device *dev);
51 extern void device_wakeup_arm_wake_irqs(void);
52 extern void device_wakeup_disarm_wake_irqs(void);
53 
54 #else
55 
56 static inline void device_wakeup_attach_irq(struct device *dev,
57 					    struct wake_irq *wakeirq) {}
58 
59 static inline void device_wakeup_detach_irq(struct device *dev)
60 {
61 }
62 
63 #endif /* CONFIG_PM_SLEEP */
64 
65 /*
66  * sysfs.c
67  */
68 
69 extern int dpm_sysfs_add(struct device *dev);
70 extern void dpm_sysfs_remove(struct device *dev);
71 extern void rpm_sysfs_remove(struct device *dev);
72 extern int wakeup_sysfs_add(struct device *dev);
73 extern void wakeup_sysfs_remove(struct device *dev);
74 extern int pm_qos_sysfs_add_resume_latency(struct device *dev);
75 extern void pm_qos_sysfs_remove_resume_latency(struct device *dev);
76 extern int pm_qos_sysfs_add_flags(struct device *dev);
77 extern void pm_qos_sysfs_remove_flags(struct device *dev);
78 extern int pm_qos_sysfs_add_latency_tolerance(struct device *dev);
79 extern void pm_qos_sysfs_remove_latency_tolerance(struct device *dev);
80 extern int dpm_sysfs_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid);
81 
82 #else /* CONFIG_PM */
83 
84 static inline void pm_runtime_early_init(struct device *dev)
85 {
86 	device_pm_init_common(dev);
87 }
88 
89 static inline void pm_runtime_init(struct device *dev) {}
90 static inline void pm_runtime_reinit(struct device *dev) {}
91 static inline void pm_runtime_remove(struct device *dev) {}
92 
93 static inline int dpm_sysfs_add(struct device *dev) { return 0; }
94 static inline void dpm_sysfs_remove(struct device *dev) {}
95 static inline int dpm_sysfs_change_owner(struct device *dev, kuid_t kuid,
96 					 kgid_t kgid) { return 0; }
97 
98 #endif
99 
100 #ifdef CONFIG_PM_SLEEP
101 
102 /* kernel/power/main.c */
103 extern int pm_async_enabled;
104 
105 /* drivers/base/power/main.c */
106 extern struct list_head dpm_list;	/* The active device list */
107 
108 static inline struct device *to_device(struct list_head *entry)
109 {
110 	return container_of(entry, struct device, power.entry);
111 }
112 
113 extern void device_pm_sleep_init(struct device *dev);
114 extern void device_pm_add(struct device *);
115 extern void device_pm_remove(struct device *);
116 extern void device_pm_move_before(struct device *, struct device *);
117 extern void device_pm_move_after(struct device *, struct device *);
118 extern void device_pm_move_last(struct device *);
119 extern void device_pm_check_callbacks(struct device *dev);
120 
121 static inline bool device_pm_initialized(struct device *dev)
122 {
123 	return dev->power.in_dpm_list;
124 }
125 
126 /* drivers/base/power/wakeup_stats.c */
127 extern int wakeup_source_sysfs_add(struct device *parent,
128 				   struct wakeup_source *ws);
129 extern void wakeup_source_sysfs_remove(struct wakeup_source *ws);
130 
131 extern int pm_wakeup_source_sysfs_add(struct device *parent);
132 
133 #else /* !CONFIG_PM_SLEEP */
134 
135 static inline void device_pm_sleep_init(struct device *dev) {}
136 
137 static inline void device_pm_add(struct device *dev) {}
138 
139 static inline void device_pm_remove(struct device *dev)
140 {
141 	pm_runtime_remove(dev);
142 }
143 
144 static inline void device_pm_move_before(struct device *deva,
145 					 struct device *devb) {}
146 static inline void device_pm_move_after(struct device *deva,
147 					struct device *devb) {}
148 static inline void device_pm_move_last(struct device *dev) {}
149 
150 static inline void device_pm_check_callbacks(struct device *dev) {}
151 
152 static inline bool device_pm_initialized(struct device *dev)
153 {
154 	return device_is_registered(dev);
155 }
156 
157 static inline int pm_wakeup_source_sysfs_add(struct device *parent)
158 {
159 	return 0;
160 }
161 
162 #endif /* !CONFIG_PM_SLEEP */
163 
164 static inline void device_pm_init(struct device *dev)
165 {
166 	device_pm_init_common(dev);
167 	device_pm_sleep_init(dev);
168 	pm_runtime_init(dev);
169 }
170