1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
6 * Copyright (C) 2011 Samsung Electronics
7 * MyungJoo Ham <myungjoo.ham@samsung.com>
10 #ifndef __LINUX_DEVFREQ_H__
11 #define __LINUX_DEVFREQ_H__
13 #include <linux/device.h>
14 #include <linux/notifier.h>
15 #include <linux/pm_opp.h>
16 #include <linux/pm_qos.h>
18 #define DEVFREQ_NAME_LEN 16
20 /* DEVFREQ governor name */
21 #define DEVFREQ_GOV_SIMPLE_ONDEMAND "simple_ondemand"
22 #define DEVFREQ_GOV_PERFORMANCE "performance"
23 #define DEVFREQ_GOV_POWERSAVE "powersave"
24 #define DEVFREQ_GOV_USERSPACE "userspace"
25 #define DEVFREQ_GOV_PASSIVE "passive"
27 /* DEVFREQ notifier interface */
28 #define DEVFREQ_TRANSITION_NOTIFIER (0)
30 /* Transition notifiers of DEVFREQ_TRANSITION_NOTIFIER */
31 #define DEVFREQ_PRECHANGE (0)
32 #define DEVFREQ_POSTCHANGE (1)
35 struct devfreq_governor;
38 * struct devfreq_dev_status - Data given from devfreq user device to
39 * governors. Represents the performance
41 * @total_time: The total time represented by this instance of
43 * @busy_time: The time that the device was working among the
45 * @current_frequency: The operating frequency.
46 * @private_data: An entry not specified by the devfreq framework.
47 * A device and a specific governor may have their
48 * own protocol with private_data. However, because
49 * this is governor-specific, a governor using this
50 * will be only compatible with devices aware of it.
52 struct devfreq_dev_status {
53 /* both since the last measure */
54 unsigned long total_time;
55 unsigned long busy_time;
56 unsigned long current_frequency;
61 * The resulting frequency should be at most this. (this bound is the
62 * least upper bound; thus, the resulting freq should be lower or same)
63 * If the flag is not set, the resulting frequency should be at most the
64 * bound (greatest lower bound)
66 #define DEVFREQ_FLAG_LEAST_UPPER_BOUND 0x1
69 * struct devfreq_dev_profile - Devfreq's user device profile
70 * @initial_freq: The operating frequency when devfreq_add_device() is
72 * @polling_ms: The polling interval in ms. 0 disables polling.
73 * @target: The device should set its operating frequency at
74 * freq or lowest-upper-than-freq value. If freq is
75 * higher than any operable frequency, set maximum.
76 * Before returning, target function should set
77 * freq at the current frequency.
78 * The "flags" parameter's possible values are
79 * explained above with "DEVFREQ_FLAG_*" macros.
80 * @get_dev_status: The device should provide the current performance
81 * status to devfreq. Governors are recommended not to
82 * use this directly. Instead, governors are recommended
83 * to use devfreq_update_stats() along with
84 * devfreq.last_status.
85 * @get_cur_freq: The device should provide the current frequency
86 * at which it is operating.
87 * @exit: An optional callback that is called when devfreq
88 * is removing the devfreq object due to error or
89 * from devfreq_remove_device() call. If the user
90 * has registered devfreq->nb at a notifier-head,
91 * this is the time to unregister it.
92 * @freq_table: Optional list of frequencies to support statistics
93 * and freq_table must be generated in ascending order.
94 * @max_state: The size of freq_table.
96 struct devfreq_dev_profile {
97 unsigned long initial_freq;
98 unsigned int polling_ms;
100 int (*target)(struct device *dev, unsigned long *freq, u32 flags);
101 int (*get_dev_status)(struct device *dev,
102 struct devfreq_dev_status *stat);
103 int (*get_cur_freq)(struct device *dev, unsigned long *freq);
104 void (*exit)(struct device *dev);
106 unsigned long *freq_table;
107 unsigned int max_state;
111 * struct devfreq - Device devfreq structure
112 * @node: list node - contains the devices with devfreq that have been
114 * @lock: a mutex to protect accessing devfreq.
115 * @dev: device registered by devfreq class. dev.parent is the device
117 * @profile: device-specific devfreq profile
118 * @governor: method how to choose frequency based on the usage.
119 * @governor_name: devfreq governor name for use with this devfreq
120 * @nb: notifier block used to notify devfreq object that it should
121 * reevaluate operable frequencies. Devfreq users may use
122 * devfreq.nb to the corresponding register notifier call chain.
123 * @work: delayed work for load monitoring.
124 * @previous_freq: previously configured frequency value.
125 * @data: Private data of the governor. The devfreq framework does not
127 * @user_min_freq_req: PM QoS minimum frequency request from user (via sysfs)
128 * @user_max_freq_req: PM QoS maximum frequency request from user (via sysfs)
129 * @scaling_min_freq: Limit minimum frequency requested by OPP interface
130 * @scaling_max_freq: Limit maximum frequency requested by OPP interface
131 * @stop_polling: devfreq polling status of a device.
132 * @suspend_freq: frequency of a device set during suspend phase.
133 * @resume_freq: frequency of a device set in resume phase.
134 * @suspend_count: suspend requests counter for a device.
135 * @total_trans: Number of devfreq transitions
136 * @trans_table: Statistics of devfreq transitions
137 * @time_in_state: Statistics of devfreq states
138 * @last_stat_updated: The last time stat updated
139 * @transition_notifier_list: list head of DEVFREQ_TRANSITION_NOTIFIER notifier
140 * @nb_min: Notifier block for DEV_PM_QOS_MIN_FREQUENCY
141 * @nb_max: Notifier block for DEV_PM_QOS_MAX_FREQUENCY
143 * This structure stores the devfreq information for a give device.
145 * Note that when a governor accesses entries in struct devfreq in its
146 * functions except for the context of callbacks defined in struct
147 * devfreq_governor, the governor should protect its access with the
148 * struct mutex lock in struct devfreq. A governor may use this mutex
149 * to protect its own private data in void *data as well.
152 struct list_head node;
156 struct devfreq_dev_profile *profile;
157 const struct devfreq_governor *governor;
158 char governor_name[DEVFREQ_NAME_LEN];
159 struct notifier_block nb;
160 struct delayed_work work;
162 unsigned long previous_freq;
163 struct devfreq_dev_status last_status;
165 void *data; /* private data for governors */
167 struct dev_pm_qos_request user_min_freq_req;
168 struct dev_pm_qos_request user_max_freq_req;
169 unsigned long scaling_min_freq;
170 unsigned long scaling_max_freq;
173 unsigned long suspend_freq;
174 unsigned long resume_freq;
175 atomic_t suspend_count;
177 /* information for device frequency transition */
178 unsigned int total_trans;
179 unsigned int *trans_table;
180 unsigned long *time_in_state;
181 unsigned long last_stat_updated;
183 struct srcu_notifier_head transition_notifier_list;
185 struct notifier_block nb_min;
186 struct notifier_block nb_max;
189 struct devfreq_freqs {
194 #if defined(CONFIG_PM_DEVFREQ)
195 extern struct devfreq *devfreq_add_device(struct device *dev,
196 struct devfreq_dev_profile *profile,
197 const char *governor_name,
199 extern int devfreq_remove_device(struct devfreq *devfreq);
200 extern struct devfreq *devm_devfreq_add_device(struct device *dev,
201 struct devfreq_dev_profile *profile,
202 const char *governor_name,
204 extern void devm_devfreq_remove_device(struct device *dev,
205 struct devfreq *devfreq);
207 /* Supposed to be called by PM callbacks */
208 extern int devfreq_suspend_device(struct devfreq *devfreq);
209 extern int devfreq_resume_device(struct devfreq *devfreq);
211 extern void devfreq_suspend(void);
212 extern void devfreq_resume(void);
215 * update_devfreq() - Reevaluate the device and configure frequency
216 * @devfreq: the devfreq device
218 * Note: devfreq->lock must be held
220 extern int update_devfreq(struct devfreq *devfreq);
222 /* Helper functions for devfreq user device driver with OPP. */
223 extern struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
224 unsigned long *freq, u32 flags);
225 extern int devfreq_register_opp_notifier(struct device *dev,
226 struct devfreq *devfreq);
227 extern int devfreq_unregister_opp_notifier(struct device *dev,
228 struct devfreq *devfreq);
229 extern int devm_devfreq_register_opp_notifier(struct device *dev,
230 struct devfreq *devfreq);
231 extern void devm_devfreq_unregister_opp_notifier(struct device *dev,
232 struct devfreq *devfreq);
233 extern int devfreq_register_notifier(struct devfreq *devfreq,
234 struct notifier_block *nb,
236 extern int devfreq_unregister_notifier(struct devfreq *devfreq,
237 struct notifier_block *nb,
239 extern int devm_devfreq_register_notifier(struct device *dev,
240 struct devfreq *devfreq,
241 struct notifier_block *nb,
243 extern void devm_devfreq_unregister_notifier(struct device *dev,
244 struct devfreq *devfreq,
245 struct notifier_block *nb,
247 extern struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
250 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND)
252 * struct devfreq_simple_ondemand_data - void *data fed to struct devfreq
253 * and devfreq_add_device
254 * @upthreshold: If the load is over this value, the frequency jumps.
255 * Specify 0 to use the default. Valid value = 0 to 100.
256 * @downdifferential: If the load is under upthreshold - downdifferential,
257 * the governor may consider slowing the frequency down.
258 * Specify 0 to use the default. Valid value = 0 to 100.
259 * downdifferential < upthreshold must hold.
261 * If the fed devfreq_simple_ondemand_data pointer is NULL to the governor,
262 * the governor uses the default values.
264 struct devfreq_simple_ondemand_data {
265 unsigned int upthreshold;
266 unsigned int downdifferential;
270 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE)
272 * struct devfreq_passive_data - void *data fed to struct devfreq
273 * and devfreq_add_device
274 * @parent: the devfreq instance of parent device.
275 * @get_target_freq: Optional callback, Returns desired operating frequency
276 * for the device using passive governor. That is called
277 * when passive governor should decide the next frequency
278 * by using the new frequency of parent devfreq device
279 * using governors except for passive governor.
280 * If the devfreq device has the specific method to decide
281 * the next frequency, should use this callback.
282 * @this: the devfreq instance of own device.
283 * @nb: the notifier block for DEVFREQ_TRANSITION_NOTIFIER list
285 * The devfreq_passive_data have to set the devfreq instance of parent
286 * device with governors except for the passive governor. But, don't need to
287 * initialize the 'this' and 'nb' field because the devfreq core will handle
290 struct devfreq_passive_data {
291 /* Should set the devfreq instance of parent device */
292 struct devfreq *parent;
294 /* Optional callback to decide the next frequency of passvice device */
295 int (*get_target_freq)(struct devfreq *this, unsigned long *freq);
297 /* For passive governor's internal use. Don't need to set them */
298 struct devfreq *this;
299 struct notifier_block nb;
303 #else /* !CONFIG_PM_DEVFREQ */
304 static inline struct devfreq *devfreq_add_device(struct device *dev,
305 struct devfreq_dev_profile *profile,
306 const char *governor_name,
309 return ERR_PTR(-ENOSYS);
312 static inline int devfreq_remove_device(struct devfreq *devfreq)
317 static inline struct devfreq *devm_devfreq_add_device(struct device *dev,
318 struct devfreq_dev_profile *profile,
319 const char *governor_name,
322 return ERR_PTR(-ENOSYS);
325 static inline void devm_devfreq_remove_device(struct device *dev,
326 struct devfreq *devfreq)
330 static inline int devfreq_suspend_device(struct devfreq *devfreq)
335 static inline int devfreq_resume_device(struct devfreq *devfreq)
340 static inline void devfreq_suspend(void) {}
341 static inline void devfreq_resume(void) {}
343 static inline struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
344 unsigned long *freq, u32 flags)
346 return ERR_PTR(-EINVAL);
349 static inline int devfreq_register_opp_notifier(struct device *dev,
350 struct devfreq *devfreq)
355 static inline int devfreq_unregister_opp_notifier(struct device *dev,
356 struct devfreq *devfreq)
361 static inline int devm_devfreq_register_opp_notifier(struct device *dev,
362 struct devfreq *devfreq)
367 static inline void devm_devfreq_unregister_opp_notifier(struct device *dev,
368 struct devfreq *devfreq)
372 static inline int devfreq_register_notifier(struct devfreq *devfreq,
373 struct notifier_block *nb,
379 static inline int devfreq_unregister_notifier(struct devfreq *devfreq,
380 struct notifier_block *nb,
386 static inline int devm_devfreq_register_notifier(struct device *dev,
387 struct devfreq *devfreq,
388 struct notifier_block *nb,
394 static inline void devm_devfreq_unregister_notifier(struct device *dev,
395 struct devfreq *devfreq,
396 struct notifier_block *nb,
401 static inline struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
404 return ERR_PTR(-ENODEV);
407 static inline int devfreq_update_stats(struct devfreq *df)
411 #endif /* CONFIG_PM_DEVFREQ */
413 #endif /* __LINUX_DEVFREQ_H__ */