dd5b0447d5727dec9f3f5cbffc9f32df2011ff01
[cascardo/linux.git] / include / linux / pm_domain.h
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
19 /* Defines used for the flags field in the struct generic_pm_domain */
20 #define GENPD_FLAG_PM_CLK       (1U << 0) /* PM domain uses PM clk */
21
22 #define GENPD_MAX_NUM_STATES    8 /* Number of possible low power states */
23
24 enum gpd_status {
25         GPD_STATE_ACTIVE = 0,   /* PM domain is active */
26         GPD_STATE_POWER_OFF,    /* PM domain is off */
27 };
28
29 struct dev_power_governor {
30         bool (*power_down_ok)(struct dev_pm_domain *domain);
31         bool (*suspend_ok)(struct device *dev);
32 };
33
34 struct gpd_dev_ops {
35         int (*start)(struct device *dev);
36         int (*stop)(struct device *dev);
37         bool (*active_wakeup)(struct device *dev);
38 };
39
40 struct genpd_power_state {
41         s64 power_off_latency_ns;
42         s64 power_on_latency_ns;
43 };
44
45 struct generic_pm_domain {
46         struct dev_pm_domain domain;    /* PM domain operations */
47         struct list_head gpd_list_node; /* Node in the global PM domains list */
48         struct list_head master_links;  /* Links with PM domain as a master */
49         struct list_head slave_links;   /* Links with PM domain as a slave */
50         struct list_head dev_list;      /* List of devices */
51         struct mutex lock;
52         struct dev_power_governor *gov;
53         struct work_struct power_off_work;
54         const char *name;
55         atomic_t sd_count;      /* Number of subdomains with power "on" */
56         enum gpd_status status; /* Current state of the domain */
57         unsigned int device_count;      /* Number of devices */
58         unsigned int suspended_count;   /* System suspend device counter */
59         unsigned int prepared_count;    /* Suspend counter of prepared devices */
60         int (*power_off)(struct generic_pm_domain *domain);
61         int (*power_on)(struct generic_pm_domain *domain);
62         struct gpd_dev_ops dev_ops;
63         s64 max_off_time_ns;    /* Maximum allowed "suspended" time. */
64         bool max_off_time_changed;
65         bool cached_power_down_ok;
66         int (*attach_dev)(struct generic_pm_domain *domain,
67                           struct device *dev);
68         void (*detach_dev)(struct generic_pm_domain *domain,
69                            struct device *dev);
70         unsigned int flags;             /* Bit field of configs for genpd */
71         struct genpd_power_state states[GENPD_MAX_NUM_STATES];
72         unsigned int state_count; /* number of states */
73         unsigned int state_idx; /* state that genpd will go to when off */
74
75 };
76
77 static inline struct generic_pm_domain *pd_to_genpd(struct dev_pm_domain *pd)
78 {
79         return container_of(pd, struct generic_pm_domain, domain);
80 }
81
82 struct gpd_link {
83         struct generic_pm_domain *master;
84         struct list_head master_node;
85         struct generic_pm_domain *slave;
86         struct list_head slave_node;
87 };
88
89 struct gpd_timing_data {
90         s64 suspend_latency_ns;
91         s64 resume_latency_ns;
92         s64 effective_constraint_ns;
93         bool constraint_changed;
94         bool cached_suspend_ok;
95 };
96
97 struct pm_domain_data {
98         struct list_head list_node;
99         struct device *dev;
100 };
101
102 struct generic_pm_domain_data {
103         struct pm_domain_data base;
104         struct gpd_timing_data td;
105         struct notifier_block nb;
106 };
107
108 #ifdef CONFIG_PM_GENERIC_DOMAINS
109 static inline struct generic_pm_domain_data *to_gpd_data(struct pm_domain_data *pdd)
110 {
111         return container_of(pdd, struct generic_pm_domain_data, base);
112 }
113
114 static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
115 {
116         return to_gpd_data(dev->power.subsys_data->domain_data);
117 }
118
119 extern struct generic_pm_domain *pm_genpd_lookup_dev(struct device *dev);
120 extern int __pm_genpd_add_device(struct generic_pm_domain *genpd,
121                                  struct device *dev,
122                                  struct gpd_timing_data *td);
123
124 extern int pm_genpd_remove_device(struct generic_pm_domain *genpd,
125                                   struct device *dev);
126 extern int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
127                                   struct generic_pm_domain *new_subdomain);
128 extern int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
129                                      struct generic_pm_domain *target);
130 extern void pm_genpd_init(struct generic_pm_domain *genpd,
131                           struct dev_power_governor *gov, bool is_off);
132
133 extern struct dev_power_governor simple_qos_governor;
134 extern struct dev_power_governor pm_domain_always_on_gov;
135 #else
136
137 static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
138 {
139         return ERR_PTR(-ENOSYS);
140 }
141 static inline struct generic_pm_domain *pm_genpd_lookup_dev(struct device *dev)
142 {
143         return NULL;
144 }
145 static inline int __pm_genpd_add_device(struct generic_pm_domain *genpd,
146                                         struct device *dev,
147                                         struct gpd_timing_data *td)
148 {
149         return -ENOSYS;
150 }
151 static inline int pm_genpd_remove_device(struct generic_pm_domain *genpd,
152                                          struct device *dev)
153 {
154         return -ENOSYS;
155 }
156 static inline int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
157                                          struct generic_pm_domain *new_sd)
158 {
159         return -ENOSYS;
160 }
161 static inline int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
162                                             struct generic_pm_domain *target)
163 {
164         return -ENOSYS;
165 }
166 static inline void pm_genpd_init(struct generic_pm_domain *genpd,
167                                  struct dev_power_governor *gov, bool is_off)
168 {
169 }
170 #endif
171
172 static inline int pm_genpd_add_device(struct generic_pm_domain *genpd,
173                                       struct device *dev)
174 {
175         return __pm_genpd_add_device(genpd, dev, NULL);
176 }
177
178 #ifdef CONFIG_PM_GENERIC_DOMAINS_SLEEP
179 extern void pm_genpd_syscore_poweroff(struct device *dev);
180 extern void pm_genpd_syscore_poweron(struct device *dev);
181 #else
182 static inline void pm_genpd_syscore_poweroff(struct device *dev) {}
183 static inline void pm_genpd_syscore_poweron(struct device *dev) {}
184 #endif
185
186 /* OF PM domain providers */
187 struct of_device_id;
188
189 struct genpd_onecell_data {
190         struct generic_pm_domain **domains;
191         unsigned int num_domains;
192 };
193
194 typedef struct generic_pm_domain *(*genpd_xlate_t)(struct of_phandle_args *args,
195                                                 void *data);
196
197 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
198 int __of_genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
199                         void *data);
200 void of_genpd_del_provider(struct device_node *np);
201 struct generic_pm_domain *of_genpd_get_from_provider(
202                         struct of_phandle_args *genpdspec);
203
204 struct generic_pm_domain *__of_genpd_xlate_simple(
205                                         struct of_phandle_args *genpdspec,
206                                         void *data);
207 struct generic_pm_domain *__of_genpd_xlate_onecell(
208                                         struct of_phandle_args *genpdspec,
209                                         void *data);
210
211 int genpd_dev_pm_attach(struct device *dev);
212 #else /* !CONFIG_PM_GENERIC_DOMAINS_OF */
213 static inline int __of_genpd_add_provider(struct device_node *np,
214                                         genpd_xlate_t xlate, void *data)
215 {
216         return 0;
217 }
218 static inline void of_genpd_del_provider(struct device_node *np) {}
219
220 static inline struct generic_pm_domain *of_genpd_get_from_provider(
221                         struct of_phandle_args *genpdspec)
222 {
223         return NULL;
224 }
225
226 #define __of_genpd_xlate_simple         NULL
227 #define __of_genpd_xlate_onecell        NULL
228
229 static inline int genpd_dev_pm_attach(struct device *dev)
230 {
231         return -ENODEV;
232 }
233 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
234
235 static inline int of_genpd_add_provider_simple(struct device_node *np,
236                                         struct generic_pm_domain *genpd)
237 {
238         return __of_genpd_add_provider(np, __of_genpd_xlate_simple, genpd);
239 }
240 static inline int of_genpd_add_provider_onecell(struct device_node *np,
241                                         struct genpd_onecell_data *data)
242 {
243         return __of_genpd_add_provider(np, __of_genpd_xlate_onecell, data);
244 }
245
246 #ifdef CONFIG_PM
247 extern int dev_pm_domain_attach(struct device *dev, bool power_on);
248 extern void dev_pm_domain_detach(struct device *dev, bool power_off);
249 extern void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd);
250 #else
251 static inline int dev_pm_domain_attach(struct device *dev, bool power_on)
252 {
253         return -ENODEV;
254 }
255 static inline void dev_pm_domain_detach(struct device *dev, bool power_off) {}
256 static inline void dev_pm_domain_set(struct device *dev,
257                                      struct dev_pm_domain *pd) {}
258 #endif
259
260 #endif /* _LINUX_PM_DOMAIN_H */