1
2
3
4
5
6
7
8
9#ifndef _LINUX_PM_DOMAIN_H
10#define _LINUX_PM_DOMAIN_H
11
12#include <linux/device.h>
13#include <linux/mutex.h>
14#include <linux/pm.h>
15#include <linux/err.h>
16#include <linux/of.h>
17#include <linux/notifier.h>
18#include <linux/spinlock.h>
19
20
21#define GENPD_FLAG_PM_CLK (1U << 0)
22#define GENPD_FLAG_IRQ_SAFE (1U << 1)
23#define GENPD_FLAG_ALWAYS_ON (1U << 2)
24#define GENPD_FLAG_ACTIVE_WAKEUP (1U << 3)
25
26enum gpd_status {
27 GPD_STATE_ACTIVE = 0,
28 GPD_STATE_POWER_OFF,
29};
30
31struct dev_power_governor {
32 bool (*power_down_ok)(struct dev_pm_domain *domain);
33 bool (*suspend_ok)(struct device *dev);
34};
35
36struct gpd_dev_ops {
37 int (*start)(struct device *dev);
38 int (*stop)(struct device *dev);
39};
40
41struct genpd_power_state {
42 s64 power_off_latency_ns;
43 s64 power_on_latency_ns;
44 s64 residency_ns;
45 struct fwnode_handle *fwnode;
46 ktime_t idle_time;
47};
48
49struct genpd_lock_ops;
50struct dev_pm_opp;
51
52struct generic_pm_domain {
53 struct device dev;
54 struct dev_pm_domain domain;
55 struct list_head gpd_list_node;
56 struct list_head master_links;
57 struct list_head slave_links;
58 struct list_head dev_list;
59 struct dev_power_governor *gov;
60 struct work_struct power_off_work;
61 struct fwnode_handle *provider;
62 bool has_provider;
63 const char *name;
64 atomic_t sd_count;
65 enum gpd_status status;
66 unsigned int device_count;
67 unsigned int suspended_count;
68 unsigned int prepared_count;
69 unsigned int performance_state;
70 int (*power_off)(struct generic_pm_domain *domain);
71 int (*power_on)(struct generic_pm_domain *domain);
72 unsigned int (*opp_to_performance_state)(struct generic_pm_domain *genpd,
73 struct dev_pm_opp *opp);
74 int (*set_performance_state)(struct generic_pm_domain *genpd,
75 unsigned int state);
76 struct gpd_dev_ops dev_ops;
77 s64 max_off_time_ns;
78 bool max_off_time_changed;
79 bool cached_power_down_ok;
80 int (*attach_dev)(struct generic_pm_domain *domain,
81 struct device *dev);
82 void (*detach_dev)(struct generic_pm_domain *domain,
83 struct device *dev);
84 unsigned int flags;
85 struct genpd_power_state *states;
86 unsigned int state_count;
87 unsigned int state_idx;
88 void *free;
89 ktime_t on_time;
90 ktime_t accounting_time;
91 const struct genpd_lock_ops *lock_ops;
92 union {
93 struct mutex mlock;
94 struct {
95 spinlock_t slock;
96 unsigned long lock_flags;
97 };
98 };
99
100};
101
102static inline struct generic_pm_domain *pd_to_genpd(struct dev_pm_domain *pd)
103{
104 return container_of(pd, struct generic_pm_domain, domain);
105}
106
107struct gpd_link {
108 struct generic_pm_domain *master;
109 struct list_head master_node;
110 struct generic_pm_domain *slave;
111 struct list_head slave_node;
112};
113
114struct gpd_timing_data {
115 s64 suspend_latency_ns;
116 s64 resume_latency_ns;
117 s64 effective_constraint_ns;
118 bool constraint_changed;
119 bool cached_suspend_ok;
120};
121
122struct pm_domain_data {
123 struct list_head list_node;
124 struct device *dev;
125};
126
127struct generic_pm_domain_data {
128 struct pm_domain_data base;
129 struct gpd_timing_data td;
130 struct notifier_block nb;
131 unsigned int performance_state;
132 void *data;
133};
134
135#ifdef CONFIG_PM_GENERIC_DOMAINS
136static inline struct generic_pm_domain_data *to_gpd_data(struct pm_domain_data *pdd)
137{
138 return container_of(pdd, struct generic_pm_domain_data, base);
139}
140
141static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
142{
143 return to_gpd_data(dev->power.subsys_data->domain_data);
144}
145
146int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev);
147int pm_genpd_remove_device(struct device *dev);
148int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
149 struct generic_pm_domain *new_subdomain);
150int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
151 struct generic_pm_domain *target);
152int pm_genpd_init(struct generic_pm_domain *genpd,
153 struct dev_power_governor *gov, bool is_off);
154int pm_genpd_remove(struct generic_pm_domain *genpd);
155int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state);
156
157extern struct dev_power_governor simple_qos_governor;
158extern struct dev_power_governor pm_domain_always_on_gov;
159#else
160
161static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
162{
163 return ERR_PTR(-ENOSYS);
164}
165static inline int pm_genpd_add_device(struct generic_pm_domain *genpd,
166 struct device *dev)
167{
168 return -ENOSYS;
169}
170static inline int pm_genpd_remove_device(struct device *dev)
171{
172 return -ENOSYS;
173}
174static inline int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
175 struct generic_pm_domain *new_sd)
176{
177 return -ENOSYS;
178}
179static inline int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
180 struct generic_pm_domain *target)
181{
182 return -ENOSYS;
183}
184static inline int pm_genpd_init(struct generic_pm_domain *genpd,
185 struct dev_power_governor *gov, bool is_off)
186{
187 return -ENOSYS;
188}
189static inline int pm_genpd_remove(struct generic_pm_domain *genpd)
190{
191 return -ENOTSUPP;
192}
193
194static inline int dev_pm_genpd_set_performance_state(struct device *dev,
195 unsigned int state)
196{
197 return -ENOTSUPP;
198}
199
200#define simple_qos_governor (*(struct dev_power_governor *)(NULL))
201#define pm_domain_always_on_gov (*(struct dev_power_governor *)(NULL))
202#endif
203
204#ifdef CONFIG_PM_GENERIC_DOMAINS_SLEEP
205void pm_genpd_syscore_poweroff(struct device *dev);
206void pm_genpd_syscore_poweron(struct device *dev);
207#else
208static inline void pm_genpd_syscore_poweroff(struct device *dev) {}
209static inline void pm_genpd_syscore_poweron(struct device *dev) {}
210#endif
211
212
213struct of_device_id;
214
215typedef struct generic_pm_domain *(*genpd_xlate_t)(struct of_phandle_args *args,
216 void *data);
217
218struct genpd_onecell_data {
219 struct generic_pm_domain **domains;
220 unsigned int num_domains;
221 genpd_xlate_t xlate;
222};
223
224#ifdef CONFIG_PM_GENERIC_DOMAINS_OF
225int of_genpd_add_provider_simple(struct device_node *np,
226 struct generic_pm_domain *genpd);
227int of_genpd_add_provider_onecell(struct device_node *np,
228 struct genpd_onecell_data *data);
229void of_genpd_del_provider(struct device_node *np);
230int of_genpd_add_device(struct of_phandle_args *args, struct device *dev);
231int of_genpd_add_subdomain(struct of_phandle_args *parent,
232 struct of_phandle_args *new_subdomain);
233struct generic_pm_domain *of_genpd_remove_last(struct device_node *np);
234int of_genpd_parse_idle_states(struct device_node *dn,
235 struct genpd_power_state **states, int *n);
236unsigned int of_genpd_opp_to_performance_state(struct device *dev,
237 struct device_node *np);
238
239int genpd_dev_pm_attach(struct device *dev);
240struct device *genpd_dev_pm_attach_by_id(struct device *dev,
241 unsigned int index);
242struct device *genpd_dev_pm_attach_by_name(struct device *dev,
243 char *name);
244#else
245static inline int of_genpd_add_provider_simple(struct device_node *np,
246 struct generic_pm_domain *genpd)
247{
248 return -ENOTSUPP;
249}
250
251static inline int of_genpd_add_provider_onecell(struct device_node *np,
252 struct genpd_onecell_data *data)
253{
254 return -ENOTSUPP;
255}
256
257static inline void of_genpd_del_provider(struct device_node *np) {}
258
259static inline int of_genpd_add_device(struct of_phandle_args *args,
260 struct device *dev)
261{
262 return -ENODEV;
263}
264
265static inline int of_genpd_add_subdomain(struct of_phandle_args *parent,
266 struct of_phandle_args *new_subdomain)
267{
268 return -ENODEV;
269}
270
271static inline int of_genpd_parse_idle_states(struct device_node *dn,
272 struct genpd_power_state **states, int *n)
273{
274 return -ENODEV;
275}
276
277static inline unsigned int
278of_genpd_opp_to_performance_state(struct device *dev,
279 struct device_node *np)
280{
281 return 0;
282}
283
284static inline int genpd_dev_pm_attach(struct device *dev)
285{
286 return 0;
287}
288
289static inline struct device *genpd_dev_pm_attach_by_id(struct device *dev,
290 unsigned int index)
291{
292 return NULL;
293}
294
295static inline struct device *genpd_dev_pm_attach_by_name(struct device *dev,
296 char *name)
297{
298 return NULL;
299}
300
301static inline
302struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
303{
304 return ERR_PTR(-ENOTSUPP);
305}
306#endif
307
308#ifdef CONFIG_PM
309int dev_pm_domain_attach(struct device *dev, bool power_on);
310struct device *dev_pm_domain_attach_by_id(struct device *dev,
311 unsigned int index);
312struct device *dev_pm_domain_attach_by_name(struct device *dev,
313 char *name);
314void dev_pm_domain_detach(struct device *dev, bool power_off);
315void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd);
316#else
317static inline int dev_pm_domain_attach(struct device *dev, bool power_on)
318{
319 return 0;
320}
321static inline struct device *dev_pm_domain_attach_by_id(struct device *dev,
322 unsigned int index)
323{
324 return NULL;
325}
326static inline struct device *dev_pm_domain_attach_by_name(struct device *dev,
327 char *name)
328{
329 return NULL;
330}
331static inline void dev_pm_domain_detach(struct device *dev, bool power_off) {}
332static inline void dev_pm_domain_set(struct device *dev,
333 struct dev_pm_domain *pd) {}
334#endif
335
336#endif
337