blob: 6a8988166899b0f3a97be387bfb16f246a7d0107 [file] [log] [blame]
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001/*
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>
Paul Gortmaker313162d2012-01-30 11:46:54 -050013#include <linux/mutex.h>
14#include <linux/pm.h>
Guennadi Liakhovetski4f042cd2011-12-01 00:05:31 +010015#include <linux/err.h>
Thomas Abrahamc8aa1302012-01-27 15:22:07 +090016#include <linux/of.h>
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +020017#include <linux/notifier.h>
Rafael J. Wysockif7218892011-07-01 22:12:45 +020018
Ulf Hanssonc11f6f52014-12-01 12:50:21 +010019/* 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
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020022enum gpd_status {
23 GPD_STATE_ACTIVE = 0, /* PM domain is active */
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020024 GPD_STATE_POWER_OFF, /* PM domain is off */
25};
Rafael J. Wysocki596ba342011-07-01 22:13:19 +020026
Rafael J. Wysockif7218892011-07-01 22:12:45 +020027struct dev_power_governor {
28 bool (*power_down_ok)(struct dev_pm_domain *domain);
Ulf Hansson9df39212016-03-31 11:21:25 +020029 bool (*suspend_ok)(struct device *dev);
Rafael J. Wysockif7218892011-07-01 22:12:45 +020030};
31
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010032struct gpd_dev_ops {
33 int (*start)(struct device *dev);
34 int (*stop)(struct device *dev);
35 bool (*active_wakeup)(struct device *dev);
36};
37
Axel Haslamfc5cbf02016-02-15 11:10:51 +010038struct genpd_power_state {
39 s64 power_off_latency_ns;
40 s64 power_on_latency_ns;
Lina Iyer405f7222016-10-14 10:47:50 -070041 s64 residency_ns;
Lina Iyer0c9b6942016-10-14 10:47:52 -070042 struct fwnode_handle *fwnode;
Axel Haslamfc5cbf02016-02-15 11:10:51 +010043};
44
Rafael J. Wysockif7218892011-07-01 22:12:45 +020045struct generic_pm_domain {
46 struct dev_pm_domain domain; /* PM domain operations */
Rafael J. Wysocki5125bbf382011-07-13 12:31:52 +020047 struct list_head gpd_list_node; /* Node in the global PM domains list */
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +020048 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 */
Rafael J. Wysockif7218892011-07-01 22:12:45 +020050 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;
Jon Hunterde0aa06d2016-09-12 12:01:12 +010054 struct fwnode_handle *provider; /* Identity of the domain provider */
55 bool has_provider;
Geert Uytterhoevend07e9c12014-08-29 15:13:21 +020056 const char *name;
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +020057 atomic_t sd_count; /* Number of subdomains with power "on" */
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020058 enum gpd_status status; /* Current state of the domain */
Rafael J. Wysocki596ba342011-07-01 22:13:19 +020059 unsigned int device_count; /* Number of devices */
60 unsigned int suspended_count; /* System suspend device counter */
61 unsigned int prepared_count; /* Suspend counter of prepared devices */
Rafael J. Wysockif7218892011-07-01 22:12:45 +020062 int (*power_off)(struct generic_pm_domain *domain);
63 int (*power_on)(struct generic_pm_domain *domain);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010064 struct gpd_dev_ops dev_ops;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +010065 s64 max_off_time_ns; /* Maximum allowed "suspended" time. */
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +020066 bool max_off_time_changed;
67 bool cached_power_down_ok;
Ulf Hanssonc16561e2014-11-06 00:37:08 +010068 int (*attach_dev)(struct generic_pm_domain *domain,
69 struct device *dev);
70 void (*detach_dev)(struct generic_pm_domain *domain,
71 struct device *dev);
Ulf Hanssonc11f6f52014-12-01 12:50:21 +010072 unsigned int flags; /* Bit field of configs for genpd */
Lina Iyer59d65b72016-10-14 10:47:49 -070073 struct genpd_power_state *states;
Axel Haslamfc5cbf02016-02-15 11:10:51 +010074 unsigned int state_count; /* number of states */
75 unsigned int state_idx; /* state that genpd will go to when off */
Lina Iyer59d65b72016-10-14 10:47:49 -070076 void *free; /* Free the state that was allocated for default */
Axel Haslamfc5cbf02016-02-15 11:10:51 +010077
Rafael J. Wysockif7218892011-07-01 22:12:45 +020078};
79
Rafael J. Wysocki596ba342011-07-01 22:13:19 +020080static inline struct generic_pm_domain *pd_to_genpd(struct dev_pm_domain *pd)
81{
82 return container_of(pd, struct generic_pm_domain, domain);
83}
84
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +020085struct gpd_link {
86 struct generic_pm_domain *master;
87 struct list_head master_node;
88 struct generic_pm_domain *slave;
89 struct list_head slave_node;
90};
91
Rafael J. Wysockib02c9992011-12-01 00:02:05 +010092struct gpd_timing_data {
Ulf Hansson2b1d88c2015-10-15 17:02:19 +020093 s64 suspend_latency_ns;
94 s64 resume_latency_ns;
Rafael J. Wysockia5bef8102012-04-29 22:54:17 +020095 s64 effective_constraint_ns;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +020096 bool constraint_changed;
Ulf Hansson9df39212016-03-31 11:21:25 +020097 bool cached_suspend_ok;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +010098};
99
Ulf Hansson00e7c292014-11-14 08:41:32 +0100100struct pm_domain_data {
101 struct list_head list_node;
102 struct device *dev;
103};
104
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200105struct generic_pm_domain_data {
106 struct pm_domain_data base;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100107 struct gpd_timing_data td;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200108 struct notifier_block nb;
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200109};
110
Guennadi Liakhovetski9b4f617b2012-02-04 22:26:49 +0100111#ifdef CONFIG_PM_GENERIC_DOMAINS
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200112static inline struct generic_pm_domain_data *to_gpd_data(struct pm_domain_data *pdd)
113{
114 return container_of(pdd, struct generic_pm_domain_data, base);
115}
116
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100117static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
118{
119 return to_gpd_data(dev->power.subsys_data->domain_data);
120}
121
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100122extern int __pm_genpd_add_device(struct generic_pm_domain *genpd,
123 struct device *dev,
124 struct gpd_timing_data *td);
125
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200126extern int pm_genpd_remove_device(struct generic_pm_domain *genpd,
127 struct device *dev);
128extern int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
129 struct generic_pm_domain *new_subdomain);
130extern int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
131 struct generic_pm_domain *target);
Ulf Hansson7eb231c2016-06-17 12:27:52 +0200132extern int pm_genpd_init(struct generic_pm_domain *genpd,
133 struct dev_power_governor *gov, bool is_off);
Jon Hunter3fe57712016-09-12 12:01:13 +0100134extern int pm_genpd_remove(struct generic_pm_domain *genpd);
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100135
Ulf Hanssonae3c5112014-09-03 12:52:31 +0200136extern struct dev_power_governor simple_qos_governor;
Mark Brown925b44a2011-12-08 23:27:28 +0100137extern struct dev_power_governor pm_domain_always_on_gov;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200138#else
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100139
Magnus Dammb6426312012-02-25 22:14:18 +0100140static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
141{
142 return ERR_PTR(-ENOSYS);
143}
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100144static inline int __pm_genpd_add_device(struct generic_pm_domain *genpd,
145 struct device *dev,
146 struct gpd_timing_data *td)
147{
148 return -ENOSYS;
149}
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200150static inline int pm_genpd_remove_device(struct generic_pm_domain *genpd,
151 struct device *dev)
152{
153 return -ENOSYS;
154}
155static inline int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
156 struct generic_pm_domain *new_sd)
157{
158 return -ENOSYS;
159}
160static inline int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
161 struct generic_pm_domain *target)
162{
163 return -ENOSYS;
164}
Ulf Hansson7eb231c2016-06-17 12:27:52 +0200165static inline int pm_genpd_init(struct generic_pm_domain *genpd,
166 struct dev_power_governor *gov, bool is_off)
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100167{
Ulf Hansson7eb231c2016-06-17 12:27:52 +0200168 return -ENOSYS;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100169}
Jon Hunter3fe57712016-09-12 12:01:13 +0100170static inline int pm_genpd_remove(struct generic_pm_domain *genpd)
171{
172 return -ENOTSUPP;
173}
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200174#endif
175
Rafael J. Wysockib5abb082012-08-07 01:06:11 +0200176static inline int pm_genpd_add_device(struct generic_pm_domain *genpd,
177 struct device *dev)
178{
179 return __pm_genpd_add_device(genpd, dev, NULL);
180}
181
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200182#ifdef CONFIG_PM_GENERIC_DOMAINS_SLEEP
Ulf Hanssond47e6462014-09-03 12:52:24 +0200183extern void pm_genpd_syscore_poweroff(struct device *dev);
184extern void pm_genpd_syscore_poweron(struct device *dev);
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200185#else
Ulf Hanssond47e6462014-09-03 12:52:24 +0200186static inline void pm_genpd_syscore_poweroff(struct device *dev) {}
187static inline void pm_genpd_syscore_poweron(struct device *dev) {}
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200188#endif
189
Tomasz Figaaa422402014-09-19 20:27:36 +0200190/* OF PM domain providers */
191struct of_device_id;
192
193struct genpd_onecell_data {
194 struct generic_pm_domain **domains;
195 unsigned int num_domains;
196};
197
Tomasz Figaaa422402014-09-19 20:27:36 +0200198#ifdef CONFIG_PM_GENERIC_DOMAINS_OF
Jon Hunter892ebdcc2016-09-12 12:01:09 +0100199int of_genpd_add_provider_simple(struct device_node *np,
200 struct generic_pm_domain *genpd);
201int of_genpd_add_provider_onecell(struct device_node *np,
202 struct genpd_onecell_data *data);
Tomasz Figaaa422402014-09-19 20:27:36 +0200203void of_genpd_del_provider(struct device_node *np);
Jon Hunterec695722016-09-12 12:01:05 +0100204extern int of_genpd_add_device(struct of_phandle_args *args,
205 struct device *dev);
206extern int of_genpd_add_subdomain(struct of_phandle_args *parent,
207 struct of_phandle_args *new_subdomain);
Jon Hunter17926552016-09-12 12:01:14 +0100208extern struct generic_pm_domain *of_genpd_remove_last(struct device_node *np);
Lina Iyer30f60422016-10-14 10:47:51 -0700209extern int of_genpd_parse_idle_states(struct device_node *dn,
210 struct genpd_power_state **states, int *n);
Tomasz Figaaa422402014-09-19 20:27:36 +0200211
212int genpd_dev_pm_attach(struct device *dev);
213#else /* !CONFIG_PM_GENERIC_DOMAINS_OF */
Jon Hunter892ebdcc2016-09-12 12:01:09 +0100214static inline int of_genpd_add_provider_simple(struct device_node *np,
215 struct generic_pm_domain *genpd)
Tomasz Figaaa422402014-09-19 20:27:36 +0200216{
Jon Hunter892ebdcc2016-09-12 12:01:09 +0100217 return -ENOTSUPP;
Tomasz Figaaa422402014-09-19 20:27:36 +0200218}
Tomasz Figaaa422402014-09-19 20:27:36 +0200219
Jon Hunter892ebdcc2016-09-12 12:01:09 +0100220static inline int of_genpd_add_provider_onecell(struct device_node *np,
221 struct genpd_onecell_data *data)
222{
223 return -ENOTSUPP;
224}
225
226static inline void of_genpd_del_provider(struct device_node *np) {}
Tomasz Figaaa422402014-09-19 20:27:36 +0200227
Jon Hunterec695722016-09-12 12:01:05 +0100228static inline int of_genpd_add_device(struct of_phandle_args *args,
229 struct device *dev)
230{
231 return -ENODEV;
232}
233
234static inline int of_genpd_add_subdomain(struct of_phandle_args *parent,
235 struct of_phandle_args *new_subdomain)
236{
237 return -ENODEV;
238}
239
Lina Iyer30f60422016-10-14 10:47:51 -0700240static inline int of_genpd_parse_idle_states(struct device_node *dn,
241 struct genpd_power_state **states, int *n)
242{
243 return -ENODEV;
244}
245
Tomasz Figaaa422402014-09-19 20:27:36 +0200246static inline int genpd_dev_pm_attach(struct device *dev)
247{
248 return -ENODEV;
249}
Jon Hunter17926552016-09-12 12:01:14 +0100250
251static inline
252struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
253{
254 return ERR_PTR(-ENOTSUPP);
255}
Tomasz Figaaa422402014-09-19 20:27:36 +0200256#endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
257
Ulf Hanssonf48c7672014-09-29 13:58:47 +0200258#ifdef CONFIG_PM
259extern int dev_pm_domain_attach(struct device *dev, bool power_on);
260extern void dev_pm_domain_detach(struct device *dev, bool power_off);
Tomeu Vizoso989561d2016-01-07 16:46:13 +0100261extern void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd);
Ulf Hanssonf48c7672014-09-29 13:58:47 +0200262#else
263static inline int dev_pm_domain_attach(struct device *dev, bool power_on)
264{
265 return -ENODEV;
266}
267static inline void dev_pm_domain_detach(struct device *dev, bool power_off) {}
Tomeu Vizoso989561d2016-01-07 16:46:13 +0100268static inline void dev_pm_domain_set(struct device *dev,
269 struct dev_pm_domain *pd) {}
Ulf Hanssonf48c7672014-09-29 13:58:47 +0200270#endif
271
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200272#endif /* _LINUX_PM_DOMAIN_H */