blob: 42e0d649e6531577a841cbb7d4b16842a6f413e4 [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>
Lina Iyerd716f472016-10-14 10:47:55 -070018#include <linux/spinlock.h>
Rafael J. Wysockif7218892011-07-01 22:12:45 +020019
Ulf Hanssonc11f6f52014-12-01 12:50:21 +010020/* Defines used for the flags field in the struct generic_pm_domain */
Geert Uytterhoeven95a20ef2017-11-07 13:48:11 +010021#define GENPD_FLAG_PM_CLK (1U << 0) /* PM domain uses PM clk */
22#define GENPD_FLAG_IRQ_SAFE (1U << 1) /* PM domain operates in atomic */
23#define GENPD_FLAG_ALWAYS_ON (1U << 2) /* PM domain is always powered on */
24#define GENPD_FLAG_ACTIVE_WAKEUP (1U << 3) /* Keep devices active if wakeup */
Ulf Hanssonc11f6f52014-12-01 12:50:21 +010025
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020026enum gpd_status {
27 GPD_STATE_ACTIVE = 0, /* PM domain is active */
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020028 GPD_STATE_POWER_OFF, /* PM domain is off */
29};
Rafael J. Wysocki596ba342011-07-01 22:13:19 +020030
Rafael J. Wysockif7218892011-07-01 22:12:45 +020031struct dev_power_governor {
32 bool (*power_down_ok)(struct dev_pm_domain *domain);
Ulf Hansson9df39212016-03-31 11:21:25 +020033 bool (*suspend_ok)(struct device *dev);
Rafael J. Wysockif7218892011-07-01 22:12:45 +020034};
35
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010036struct gpd_dev_ops {
37 int (*start)(struct device *dev);
38 int (*stop)(struct device *dev);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010039};
40
Axel Haslamfc5cbf02016-02-15 11:10:51 +010041struct genpd_power_state {
42 s64 power_off_latency_ns;
43 s64 power_on_latency_ns;
Lina Iyer405f7222016-10-14 10:47:50 -070044 s64 residency_ns;
Lina Iyer0c9b6942016-10-14 10:47:52 -070045 struct fwnode_handle *fwnode;
Thara Gopinathafece3a2017-07-14 13:10:15 -040046 ktime_t idle_time;
Axel Haslamfc5cbf02016-02-15 11:10:51 +010047};
48
Lina Iyer35241d12016-10-14 10:47:54 -070049struct genpd_lock_ops;
Viresh Kumar6e417662017-11-29 15:21:51 +053050struct dev_pm_opp;
Lina Iyer35241d12016-10-14 10:47:54 -070051
Rafael J. Wysockif7218892011-07-01 22:12:45 +020052struct generic_pm_domain {
Viresh Kumar401ea152017-03-17 11:26:19 +053053 struct device dev;
Rafael J. Wysockif7218892011-07-01 22:12:45 +020054 struct dev_pm_domain domain; /* PM domain operations */
Rafael J. Wysocki5125bbf382011-07-13 12:31:52 +020055 struct list_head gpd_list_node; /* Node in the global PM domains list */
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +020056 struct list_head master_links; /* Links with PM domain as a master */
57 struct list_head slave_links; /* Links with PM domain as a slave */
Rafael J. Wysockif7218892011-07-01 22:12:45 +020058 struct list_head dev_list; /* List of devices */
Rafael J. Wysockif7218892011-07-01 22:12:45 +020059 struct dev_power_governor *gov;
60 struct work_struct power_off_work;
Jon Hunterde0aa06d2016-09-12 12:01:12 +010061 struct fwnode_handle *provider; /* Identity of the domain provider */
62 bool has_provider;
Geert Uytterhoevend07e9c12014-08-29 15:13:21 +020063 const char *name;
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +020064 atomic_t sd_count; /* Number of subdomains with power "on" */
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020065 enum gpd_status status; /* Current state of the domain */
Rafael J. Wysocki596ba342011-07-01 22:13:19 +020066 unsigned int device_count; /* Number of devices */
67 unsigned int suspended_count; /* System suspend device counter */
68 unsigned int prepared_count; /* Suspend counter of prepared devices */
Viresh Kumar42f62842017-10-12 15:07:23 +053069 unsigned int performance_state; /* Aggregated max performance state */
Rafael J. Wysockif7218892011-07-01 22:12:45 +020070 int (*power_off)(struct generic_pm_domain *domain);
71 int (*power_on)(struct generic_pm_domain *domain);
Viresh Kumar6e417662017-11-29 15:21:51 +053072 unsigned int (*opp_to_performance_state)(struct generic_pm_domain *genpd,
73 struct dev_pm_opp *opp);
Viresh Kumar42f62842017-10-12 15:07:23 +053074 int (*set_performance_state)(struct generic_pm_domain *genpd,
75 unsigned int state);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010076 struct gpd_dev_ops dev_ops;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +010077 s64 max_off_time_ns; /* Maximum allowed "suspended" time. */
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +020078 bool max_off_time_changed;
79 bool cached_power_down_ok;
Ulf Hanssonc16561e2014-11-06 00:37:08 +010080 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);
Ulf Hanssonc11f6f52014-12-01 12:50:21 +010084 unsigned int flags; /* Bit field of configs for genpd */
Lina Iyer59d65b72016-10-14 10:47:49 -070085 struct genpd_power_state *states;
Axel Haslamfc5cbf02016-02-15 11:10:51 +010086 unsigned int state_count; /* number of states */
87 unsigned int state_idx; /* state that genpd will go to when off */
Lina Iyer59d65b72016-10-14 10:47:49 -070088 void *free; /* Free the state that was allocated for default */
Thara Gopinathafece3a2017-07-14 13:10:15 -040089 ktime_t on_time;
90 ktime_t accounting_time;
Lina Iyer35241d12016-10-14 10:47:54 -070091 const struct genpd_lock_ops *lock_ops;
Lina Iyerd716f472016-10-14 10:47:55 -070092 union {
93 struct mutex mlock;
94 struct {
95 spinlock_t slock;
96 unsigned long lock_flags;
97 };
98 };
Axel Haslamfc5cbf02016-02-15 11:10:51 +010099
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200100};
101
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200102static 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
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200107struct 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
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100114struct gpd_timing_data {
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200115 s64 suspend_latency_ns;
116 s64 resume_latency_ns;
Rafael J. Wysockia5bef8102012-04-29 22:54:17 +0200117 s64 effective_constraint_ns;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200118 bool constraint_changed;
Ulf Hansson9df39212016-03-31 11:21:25 +0200119 bool cached_suspend_ok;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100120};
121
Ulf Hansson00e7c292014-11-14 08:41:32 +0100122struct pm_domain_data {
123 struct list_head list_node;
124 struct device *dev;
125};
126
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200127struct generic_pm_domain_data {
128 struct pm_domain_data base;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100129 struct gpd_timing_data td;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200130 struct notifier_block nb;
Viresh Kumar42f62842017-10-12 15:07:23 +0530131 unsigned int performance_state;
Dave Gerlacha5ea7a02017-04-04 08:51:29 -0700132 void *data;
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200133};
134
Guennadi Liakhovetski9b4f617b2012-02-04 22:26:49 +0100135#ifdef CONFIG_PM_GENERIC_DOMAINS
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200136static 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
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100141static 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
Ulf Hansson1a7a6702018-05-29 12:04:14 +0200146int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev);
Ulf Hansson924f4482018-05-29 12:04:15 +0200147int pm_genpd_remove_device(struct device *dev);
Ulf Hansson781b9d62018-05-29 12:04:13 +0200148int 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);
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100156
Ulf Hanssonae3c5112014-09-03 12:52:31 +0200157extern struct dev_power_governor simple_qos_governor;
Mark Brown925b44a2011-12-08 23:27:28 +0100158extern struct dev_power_governor pm_domain_always_on_gov;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200159#else
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100160
Magnus Dammb6426312012-02-25 22:14:18 +0100161static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
162{
163 return ERR_PTR(-ENOSYS);
164}
Ulf Hansson1a7a6702018-05-29 12:04:14 +0200165static inline int pm_genpd_add_device(struct generic_pm_domain *genpd,
166 struct device *dev)
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100167{
168 return -ENOSYS;
169}
Ulf Hansson924f4482018-05-29 12:04:15 +0200170static inline int pm_genpd_remove_device(struct device *dev)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200171{
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}
Ulf Hansson7eb231c2016-06-17 12:27:52 +0200184static inline int pm_genpd_init(struct generic_pm_domain *genpd,
185 struct dev_power_governor *gov, bool is_off)
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100186{
Ulf Hansson7eb231c2016-06-17 12:27:52 +0200187 return -ENOSYS;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100188}
Jon Hunter3fe57712016-09-12 12:01:13 +0100189static inline int pm_genpd_remove(struct generic_pm_domain *genpd)
190{
191 return -ENOTSUPP;
192}
Geert Uytterhoeven16d0bf12017-02-08 19:37:55 +0100193
Viresh Kumar42f62842017-10-12 15:07:23 +0530194static inline int dev_pm_genpd_set_performance_state(struct device *dev,
195 unsigned int state)
196{
197 return -ENOTSUPP;
198}
199
Geert Uytterhoeven16d0bf12017-02-08 19:37:55 +0100200#define simple_qos_governor (*(struct dev_power_governor *)(NULL))
201#define pm_domain_always_on_gov (*(struct dev_power_governor *)(NULL))
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200202#endif
203
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200204#ifdef CONFIG_PM_GENERIC_DOMAINS_SLEEP
Ulf Hansson781b9d62018-05-29 12:04:13 +0200205void pm_genpd_syscore_poweroff(struct device *dev);
206void pm_genpd_syscore_poweron(struct device *dev);
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200207#else
Ulf Hanssond47e6462014-09-03 12:52:24 +0200208static inline void pm_genpd_syscore_poweroff(struct device *dev) {}
209static inline void pm_genpd_syscore_poweron(struct device *dev) {}
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200210#endif
211
Tomasz Figaaa422402014-09-19 20:27:36 +0200212/* OF PM domain providers */
213struct of_device_id;
214
Thierry Reding40845522017-03-29 18:34:50 +0200215typedef struct generic_pm_domain *(*genpd_xlate_t)(struct of_phandle_args *args,
216 void *data);
217
Tomasz Figaaa422402014-09-19 20:27:36 +0200218struct genpd_onecell_data {
219 struct generic_pm_domain **domains;
220 unsigned int num_domains;
Thierry Reding40845522017-03-29 18:34:50 +0200221 genpd_xlate_t xlate;
Tomasz Figaaa422402014-09-19 20:27:36 +0200222};
223
Tomasz Figaaa422402014-09-19 20:27:36 +0200224#ifdef CONFIG_PM_GENERIC_DOMAINS_OF
Jon Hunter892ebdcc2016-09-12 12:01:09 +0100225int 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);
Tomasz Figaaa422402014-09-19 20:27:36 +0200229void of_genpd_del_provider(struct device_node *np);
Ulf Hansson781b9d62018-05-29 12:04:13 +0200230int 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,
Viresh Kumar6e417662017-11-29 15:21:51 +0530237 struct device_node *opp_node);
Tomasz Figaaa422402014-09-19 20:27:36 +0200238
239int genpd_dev_pm_attach(struct device *dev);
240#else /* !CONFIG_PM_GENERIC_DOMAINS_OF */
Jon Hunter892ebdcc2016-09-12 12:01:09 +0100241static inline int of_genpd_add_provider_simple(struct device_node *np,
242 struct generic_pm_domain *genpd)
Tomasz Figaaa422402014-09-19 20:27:36 +0200243{
Jon Hunter892ebdcc2016-09-12 12:01:09 +0100244 return -ENOTSUPP;
Tomasz Figaaa422402014-09-19 20:27:36 +0200245}
Tomasz Figaaa422402014-09-19 20:27:36 +0200246
Jon Hunter892ebdcc2016-09-12 12:01:09 +0100247static inline int of_genpd_add_provider_onecell(struct device_node *np,
248 struct genpd_onecell_data *data)
249{
250 return -ENOTSUPP;
251}
252
253static inline void of_genpd_del_provider(struct device_node *np) {}
Tomasz Figaaa422402014-09-19 20:27:36 +0200254
Jon Hunterec695722016-09-12 12:01:05 +0100255static inline int of_genpd_add_device(struct of_phandle_args *args,
256 struct device *dev)
257{
258 return -ENODEV;
259}
260
261static inline int of_genpd_add_subdomain(struct of_phandle_args *parent,
262 struct of_phandle_args *new_subdomain)
263{
264 return -ENODEV;
265}
266
Lina Iyer30f60422016-10-14 10:47:51 -0700267static inline int of_genpd_parse_idle_states(struct device_node *dn,
268 struct genpd_power_state **states, int *n)
269{
270 return -ENODEV;
271}
272
Viresh Kumar6e417662017-11-29 15:21:51 +0530273static inline unsigned int
274of_genpd_opp_to_performance_state(struct device *dev,
275 struct device_node *opp_node)
276{
277 return -ENODEV;
278}
279
Tomasz Figaaa422402014-09-19 20:27:36 +0200280static inline int genpd_dev_pm_attach(struct device *dev)
281{
Ulf Hansson919b7302018-05-09 12:17:52 +0200282 return 0;
Tomasz Figaaa422402014-09-19 20:27:36 +0200283}
Jon Hunter17926552016-09-12 12:01:14 +0100284
285static inline
286struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
287{
288 return ERR_PTR(-ENOTSUPP);
289}
Tomasz Figaaa422402014-09-19 20:27:36 +0200290#endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
291
Ulf Hanssonf48c7672014-09-29 13:58:47 +0200292#ifdef CONFIG_PM
Ulf Hansson781b9d62018-05-29 12:04:13 +0200293int dev_pm_domain_attach(struct device *dev, bool power_on);
294void dev_pm_domain_detach(struct device *dev, bool power_off);
295void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd);
Ulf Hanssonf48c7672014-09-29 13:58:47 +0200296#else
297static inline int dev_pm_domain_attach(struct device *dev, bool power_on)
298{
Ulf Hansson919b7302018-05-09 12:17:52 +0200299 return 0;
Ulf Hanssonf48c7672014-09-29 13:58:47 +0200300}
301static inline void dev_pm_domain_detach(struct device *dev, bool power_off) {}
Tomeu Vizoso989561d2016-01-07 16:46:13 +0100302static inline void dev_pm_domain_set(struct device *dev,
303 struct dev_pm_domain *pd) {}
Ulf Hanssonf48c7672014-09-29 13:58:47 +0200304#endif
305
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200306#endif /* _LINUX_PM_DOMAIN_H */