1 /*
2  * pm_domain.h - Definitions and headers related to device power domains.
3  *
4  * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
5  *
6  * This file is released under the GPLv2.
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/cpuidle.h>
19 
20 /* Defines used for the flags field in the struct generic_pm_domain */
21 #define GENPD_FLAG_PM_CLK	(1U << 0) /* PM domain uses PM clk */
22 
23 enum gpd_status {
24 	GPD_STATE_ACTIVE = 0,	/* PM domain is active */
25 	GPD_STATE_WAIT_MASTER,	/* PM domain's master is being waited for */
26 	GPD_STATE_BUSY,		/* Something is happening to the PM domain */
27 	GPD_STATE_REPEAT,	/* Power off in progress, to be repeated */
28 	GPD_STATE_POWER_OFF,	/* PM domain is off */
29 };
30 
31 struct dev_power_governor {
32 	bool (*power_down_ok)(struct dev_pm_domain *domain);
33 	bool (*stop_ok)(struct device *dev);
34 };
35 
36 struct gpd_dev_ops {
37 	int (*start)(struct device *dev);
38 	int (*stop)(struct device *dev);
39 	int (*save_state)(struct device *dev);
40 	int (*restore_state)(struct device *dev);
41 	bool (*active_wakeup)(struct device *dev);
42 };
43 
44 struct gpd_cpuidle_data {
45 	unsigned int saved_exit_latency;
46 	struct cpuidle_state *idle_state;
47 };
48 
49 struct generic_pm_domain {
50 	struct dev_pm_domain domain;	/* PM domain operations */
51 	struct list_head gpd_list_node;	/* Node in the global PM domains list */
52 	struct list_head master_links;	/* Links with PM domain as a master */
53 	struct list_head slave_links;	/* Links with PM domain as a slave */
54 	struct list_head dev_list;	/* List of devices */
55 	struct mutex lock;
56 	struct dev_power_governor *gov;
57 	struct work_struct power_off_work;
58 	const char *name;
59 	unsigned int in_progress;	/* Number of devices being suspended now */
60 	atomic_t sd_count;	/* Number of subdomains with power "on" */
61 	enum gpd_status status;	/* Current state of the domain */
62 	wait_queue_head_t status_wait_queue;
63 	struct task_struct *poweroff_task;	/* Powering off task */
64 	unsigned int resume_count;	/* Number of devices being resumed */
65 	unsigned int device_count;	/* Number of devices */
66 	unsigned int suspended_count;	/* System suspend device counter */
67 	unsigned int prepared_count;	/* Suspend counter of prepared devices */
68 	bool suspend_power_off;	/* Power status before system suspend */
69 	int (*power_off)(struct generic_pm_domain *domain);
70 	s64 power_off_latency_ns;
71 	int (*power_on)(struct generic_pm_domain *domain);
72 	s64 power_on_latency_ns;
73 	struct gpd_dev_ops dev_ops;
74 	s64 max_off_time_ns;	/* Maximum allowed "suspended" time. */
75 	bool max_off_time_changed;
76 	bool cached_power_down_ok;
77 	struct gpd_cpuidle_data *cpuidle_data;
78 	int (*attach_dev)(struct generic_pm_domain *domain,
79 			  struct device *dev);
80 	void (*detach_dev)(struct generic_pm_domain *domain,
81 			   struct device *dev);
82 	unsigned int flags;		/* Bit field of configs for genpd */
83 };
84 
pd_to_genpd(struct dev_pm_domain * pd)85 static inline struct generic_pm_domain *pd_to_genpd(struct dev_pm_domain *pd)
86 {
87 	return container_of(pd, struct generic_pm_domain, domain);
88 }
89 
90 struct gpd_link {
91 	struct generic_pm_domain *master;
92 	struct list_head master_node;
93 	struct generic_pm_domain *slave;
94 	struct list_head slave_node;
95 };
96 
97 struct gpd_timing_data {
98 	s64 stop_latency_ns;
99 	s64 start_latency_ns;
100 	s64 save_state_latency_ns;
101 	s64 restore_state_latency_ns;
102 	s64 effective_constraint_ns;
103 	bool constraint_changed;
104 	bool cached_stop_ok;
105 };
106 
107 struct pm_domain_data {
108 	struct list_head list_node;
109 	struct device *dev;
110 };
111 
112 struct generic_pm_domain_data {
113 	struct pm_domain_data base;
114 	struct gpd_timing_data td;
115 	struct notifier_block nb;
116 	int need_restore;
117 };
118 
119 #ifdef CONFIG_PM_GENERIC_DOMAINS
to_gpd_data(struct pm_domain_data * pdd)120 static inline struct generic_pm_domain_data *to_gpd_data(struct pm_domain_data *pdd)
121 {
122 	return container_of(pdd, struct generic_pm_domain_data, base);
123 }
124 
dev_gpd_data(struct device * dev)125 static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
126 {
127 	return to_gpd_data(dev->power.subsys_data->domain_data);
128 }
129 
130 extern struct generic_pm_domain *pm_genpd_lookup_dev(struct device *dev);
131 extern int __pm_genpd_add_device(struct generic_pm_domain *genpd,
132 				 struct device *dev,
133 				 struct gpd_timing_data *td);
134 
135 extern int __pm_genpd_name_add_device(const char *domain_name,
136 				      struct device *dev,
137 				      struct gpd_timing_data *td);
138 
139 extern int pm_genpd_remove_device(struct generic_pm_domain *genpd,
140 				  struct device *dev);
141 extern int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
142 				  struct generic_pm_domain *new_subdomain);
143 extern int pm_genpd_add_subdomain_names(const char *master_name,
144 					const char *subdomain_name);
145 extern int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
146 				     struct generic_pm_domain *target);
147 extern int pm_genpd_attach_cpuidle(struct generic_pm_domain *genpd, int state);
148 extern int pm_genpd_name_attach_cpuidle(const char *name, int state);
149 extern int pm_genpd_detach_cpuidle(struct generic_pm_domain *genpd);
150 extern int pm_genpd_name_detach_cpuidle(const char *name);
151 extern void pm_genpd_init(struct generic_pm_domain *genpd,
152 			  struct dev_power_governor *gov, bool is_off);
153 
154 extern int pm_genpd_poweron(struct generic_pm_domain *genpd);
155 extern int pm_genpd_name_poweron(const char *domain_name);
156 extern void pm_genpd_poweroff_unused(void);
157 
158 extern struct dev_power_governor simple_qos_governor;
159 extern struct dev_power_governor pm_domain_always_on_gov;
160 #else
161 
dev_gpd_data(struct device * dev)162 static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
163 {
164 	return ERR_PTR(-ENOSYS);
165 }
pm_genpd_lookup_dev(struct device * dev)166 static inline struct generic_pm_domain *pm_genpd_lookup_dev(struct device *dev)
167 {
168 	return NULL;
169 }
__pm_genpd_add_device(struct generic_pm_domain * genpd,struct device * dev,struct gpd_timing_data * td)170 static inline int __pm_genpd_add_device(struct generic_pm_domain *genpd,
171 					struct device *dev,
172 					struct gpd_timing_data *td)
173 {
174 	return -ENOSYS;
175 }
__pm_genpd_name_add_device(const char * domain_name,struct device * dev,struct gpd_timing_data * td)176 static inline int __pm_genpd_name_add_device(const char *domain_name,
177 					     struct device *dev,
178 					     struct gpd_timing_data *td)
179 {
180 	return -ENOSYS;
181 }
pm_genpd_remove_device(struct generic_pm_domain * genpd,struct device * dev)182 static inline int pm_genpd_remove_device(struct generic_pm_domain *genpd,
183 					 struct device *dev)
184 {
185 	return -ENOSYS;
186 }
pm_genpd_add_subdomain(struct generic_pm_domain * genpd,struct generic_pm_domain * new_sd)187 static inline int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
188 					 struct generic_pm_domain *new_sd)
189 {
190 	return -ENOSYS;
191 }
pm_genpd_add_subdomain_names(const char * master_name,const char * subdomain_name)192 static inline int pm_genpd_add_subdomain_names(const char *master_name,
193 					       const char *subdomain_name)
194 {
195 	return -ENOSYS;
196 }
pm_genpd_remove_subdomain(struct generic_pm_domain * genpd,struct generic_pm_domain * target)197 static inline int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
198 					    struct generic_pm_domain *target)
199 {
200 	return -ENOSYS;
201 }
pm_genpd_attach_cpuidle(struct generic_pm_domain * genpd,int st)202 static inline int pm_genpd_attach_cpuidle(struct generic_pm_domain *genpd, int st)
203 {
204 	return -ENOSYS;
205 }
pm_genpd_name_attach_cpuidle(const char * name,int state)206 static inline int pm_genpd_name_attach_cpuidle(const char *name, int state)
207 {
208 	return -ENOSYS;
209 }
pm_genpd_detach_cpuidle(struct generic_pm_domain * genpd)210 static inline int pm_genpd_detach_cpuidle(struct generic_pm_domain *genpd)
211 {
212 	return -ENOSYS;
213 }
pm_genpd_name_detach_cpuidle(const char * name)214 static inline int pm_genpd_name_detach_cpuidle(const char *name)
215 {
216 	return -ENOSYS;
217 }
pm_genpd_init(struct generic_pm_domain * genpd,struct dev_power_governor * gov,bool is_off)218 static inline void pm_genpd_init(struct generic_pm_domain *genpd,
219 				 struct dev_power_governor *gov, bool is_off)
220 {
221 }
pm_genpd_poweron(struct generic_pm_domain * genpd)222 static inline int pm_genpd_poweron(struct generic_pm_domain *genpd)
223 {
224 	return -ENOSYS;
225 }
pm_genpd_name_poweron(const char * domain_name)226 static inline int pm_genpd_name_poweron(const char *domain_name)
227 {
228 	return -ENOSYS;
229 }
pm_genpd_poweroff_unused(void)230 static inline void pm_genpd_poweroff_unused(void) {}
231 #define simple_qos_governor NULL
232 #define pm_domain_always_on_gov NULL
233 #endif
234 
pm_genpd_add_device(struct generic_pm_domain * genpd,struct device * dev)235 static inline int pm_genpd_add_device(struct generic_pm_domain *genpd,
236 				      struct device *dev)
237 {
238 	return __pm_genpd_add_device(genpd, dev, NULL);
239 }
240 
pm_genpd_name_add_device(const char * domain_name,struct device * dev)241 static inline int pm_genpd_name_add_device(const char *domain_name,
242 					   struct device *dev)
243 {
244 	return __pm_genpd_name_add_device(domain_name, dev, NULL);
245 }
246 
247 #ifdef CONFIG_PM_GENERIC_DOMAINS_SLEEP
248 extern void pm_genpd_syscore_poweroff(struct device *dev);
249 extern void pm_genpd_syscore_poweron(struct device *dev);
250 #else
pm_genpd_syscore_poweroff(struct device * dev)251 static inline void pm_genpd_syscore_poweroff(struct device *dev) {}
pm_genpd_syscore_poweron(struct device * dev)252 static inline void pm_genpd_syscore_poweron(struct device *dev) {}
253 #endif
254 
255 /* OF PM domain providers */
256 struct of_device_id;
257 
258 struct genpd_onecell_data {
259 	struct generic_pm_domain **domains;
260 	unsigned int num_domains;
261 };
262 
263 typedef struct generic_pm_domain *(*genpd_xlate_t)(struct of_phandle_args *args,
264 						void *data);
265 
266 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
267 int __of_genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
268 			void *data);
269 void of_genpd_del_provider(struct device_node *np);
270 struct generic_pm_domain *of_genpd_get_from_provider(
271 			struct of_phandle_args *genpdspec);
272 
273 struct generic_pm_domain *__of_genpd_xlate_simple(
274 					struct of_phandle_args *genpdspec,
275 					void *data);
276 struct generic_pm_domain *__of_genpd_xlate_onecell(
277 					struct of_phandle_args *genpdspec,
278 					void *data);
279 
280 int genpd_dev_pm_attach(struct device *dev);
281 #else /* !CONFIG_PM_GENERIC_DOMAINS_OF */
282 static inline int __of_genpd_add_provider(struct device_node *np,
283 					genpd_xlate_t xlate, void *data)
284 {
285 	return 0;
286 }
287 static inline void of_genpd_del_provider(struct device_node *np) {}
288 
289 static inline struct generic_pm_domain *of_genpd_get_from_provider(
290 			struct of_phandle_args *genpdspec)
291 {
292 	return NULL;
293 }
294 
295 #define __of_genpd_xlate_simple		NULL
296 #define __of_genpd_xlate_onecell	NULL
297 
298 static inline int genpd_dev_pm_attach(struct device *dev)
299 {
300 	return -ENODEV;
301 }
302 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
303 
of_genpd_add_provider_simple(struct device_node * np,struct generic_pm_domain * genpd)304 static inline int of_genpd_add_provider_simple(struct device_node *np,
305 					struct generic_pm_domain *genpd)
306 {
307 	return __of_genpd_add_provider(np, __of_genpd_xlate_simple, genpd);
308 }
of_genpd_add_provider_onecell(struct device_node * np,struct genpd_onecell_data * data)309 static inline int of_genpd_add_provider_onecell(struct device_node *np,
310 					struct genpd_onecell_data *data)
311 {
312 	return __of_genpd_add_provider(np, __of_genpd_xlate_onecell, data);
313 }
314 
315 #ifdef CONFIG_PM
316 extern int dev_pm_domain_attach(struct device *dev, bool power_on);
317 extern void dev_pm_domain_detach(struct device *dev, bool power_off);
318 #else
dev_pm_domain_attach(struct device * dev,bool power_on)319 static inline int dev_pm_domain_attach(struct device *dev, bool power_on)
320 {
321 	return -ENODEV;
322 }
dev_pm_domain_detach(struct device * dev,bool power_off)323 static inline void dev_pm_domain_detach(struct device *dev, bool power_off) {}
324 #endif
325 
326 #endif /* _LINUX_PM_DOMAIN_H */
327