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)
34 /* DEVFREQ work timers */
36 DEVFREQ_TIMER_DEFERRABLE = 0,
37 DEVFREQ_TIMER_DELAYED,
42 struct devfreq_governor;
45 * struct devfreq_dev_status - Data given from devfreq user device to
46 * governors. Represents the performance
48 * @total_time: The total time represented by this instance of
50 * @busy_time: The time that the device was working among the
52 * @current_frequency: The operating frequency.
53 * @private_data: An entry not specified by the devfreq framework.
54 * A device and a specific governor may have their
55 * own protocol with private_data. However, because
56 * this is governor-specific, a governor using this
57 * will be only compatible with devices aware of it.
59 struct devfreq_dev_status {
60 /* both since the last measure */
61 unsigned long total_time;
62 unsigned long busy_time;
63 unsigned long current_frequency;
68 * The resulting frequency should be at most this. (this bound is the
69 * least upper bound; thus, the resulting freq should be lower or same)
70 * If the flag is not set, the resulting frequency should be at most the
71 * bound (greatest lower bound)
73 #define DEVFREQ_FLAG_LEAST_UPPER_BOUND 0x1
76 * struct devfreq_dev_profile - Devfreq's user device profile
77 * @initial_freq: The operating frequency when devfreq_add_device() is
79 * @polling_ms: The polling interval in ms. 0 disables polling.
80 * @timer: Timer type is either deferrable or delayed timer.
81 * @target: The device should set its operating frequency at
82 * freq or lowest-upper-than-freq value. If freq is
83 * higher than any operable frequency, set maximum.
84 * Before returning, target function should set
85 * freq at the current frequency.
86 * The "flags" parameter's possible values are
87 * explained above with "DEVFREQ_FLAG_*" macros.
88 * @get_dev_status: The device should provide the current performance
89 * status to devfreq. Governors are recommended not to
90 * use this directly. Instead, governors are recommended
91 * to use devfreq_update_stats() along with
92 * devfreq.last_status.
93 * @get_cur_freq: The device should provide the current frequency
94 * at which it is operating.
95 * @exit: An optional callback that is called when devfreq
96 * is removing the devfreq object due to error or
97 * from devfreq_remove_device() call. If the user
98 * has registered devfreq->nb at a notifier-head,
99 * this is the time to unregister it.
100 * @freq_table: Optional list of frequencies to support statistics
101 * and freq_table must be generated in ascending order.
102 * @max_state: The size of freq_table.
104 struct devfreq_dev_profile {
105 unsigned long initial_freq;
106 unsigned int polling_ms;
107 enum devfreq_timer timer;
109 int (*target)(struct device *dev, unsigned long *freq, u32 flags);
110 int (*get_dev_status)(struct device *dev,
111 struct devfreq_dev_status *stat);
112 int (*get_cur_freq)(struct device *dev, unsigned long *freq);
113 void (*exit)(struct device *dev);
115 unsigned long *freq_table;
116 unsigned int max_state;
120 * struct devfreq_stats - Statistics of devfreq device behavior
121 * @total_trans: Number of devfreq transitions.
122 * @trans_table: Statistics of devfreq transitions.
123 * @time_in_state: Statistics of devfreq states.
124 * @last_update: The last time stats were updated.
126 struct devfreq_stats {
127 unsigned int total_trans;
128 unsigned int *trans_table;
134 * struct devfreq - Device devfreq structure
135 * @node: list node - contains the devices with devfreq that have been
137 * @lock: a mutex to protect accessing devfreq.
138 * @dev: device registered by devfreq class. dev.parent is the device
140 * @profile: device-specific devfreq profile
141 * @governor: method how to choose frequency based on the usage.
142 * @governor_name: devfreq governor name for use with this devfreq
143 * @nb: notifier block used to notify devfreq object that it should
144 * reevaluate operable frequencies. Devfreq users may use
145 * devfreq.nb to the corresponding register notifier call chain.
146 * @work: delayed work for load monitoring.
147 * @previous_freq: previously configured frequency value.
148 * @last_status: devfreq user device info, performance statistics
149 * @data: devfreq driver pass to governors, governor should not change it.
150 * @governor_data: private data for governors, devfreq core doesn't touch it.
151 * @user_min_freq_req: PM QoS minimum frequency request from user (via sysfs)
152 * @user_max_freq_req: PM QoS maximum frequency request from user (via sysfs)
153 * @scaling_min_freq: Limit minimum frequency requested by OPP interface
154 * @scaling_max_freq: Limit maximum frequency requested by OPP interface
155 * @stop_polling: devfreq polling status of a device.
156 * @suspend_freq: frequency of a device set during suspend phase.
157 * @resume_freq: frequency of a device set in resume phase.
158 * @suspend_count: suspend requests counter for a device.
159 * @stats: Statistics of devfreq device behavior
160 * @transition_notifier_list: list head of DEVFREQ_TRANSITION_NOTIFIER notifier
161 * @nb_min: Notifier block for DEV_PM_QOS_MIN_FREQUENCY
162 * @nb_max: Notifier block for DEV_PM_QOS_MAX_FREQUENCY
164 * This structure stores the devfreq information for a given device.
166 * Note that when a governor accesses entries in struct devfreq in its
167 * functions except for the context of callbacks defined in struct
168 * devfreq_governor, the governor should protect its access with the
169 * struct mutex lock in struct devfreq. A governor may use this mutex
170 * to protect its own private data in ``void *data`` as well.
173 struct list_head node;
177 struct devfreq_dev_profile *profile;
178 const struct devfreq_governor *governor;
179 char governor_name[DEVFREQ_NAME_LEN];
180 struct notifier_block nb;
181 struct delayed_work work;
183 unsigned long previous_freq;
184 struct devfreq_dev_status last_status;
189 struct dev_pm_qos_request user_min_freq_req;
190 struct dev_pm_qos_request user_max_freq_req;
191 unsigned long scaling_min_freq;
192 unsigned long scaling_max_freq;
195 unsigned long suspend_freq;
196 unsigned long resume_freq;
197 atomic_t suspend_count;
199 /* information for device frequency transitions */
200 struct devfreq_stats stats;
202 struct srcu_notifier_head transition_notifier_list;
204 struct notifier_block nb_min;
205 struct notifier_block nb_max;
208 struct devfreq_freqs {
213 #if defined(CONFIG_PM_DEVFREQ)
214 struct devfreq *devfreq_add_device(struct device *dev,
215 struct devfreq_dev_profile *profile,
216 const char *governor_name,
218 int devfreq_remove_device(struct devfreq *devfreq);
219 struct devfreq *devm_devfreq_add_device(struct device *dev,
220 struct devfreq_dev_profile *profile,
221 const char *governor_name,
223 void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq);
225 /* Supposed to be called by PM callbacks */
226 int devfreq_suspend_device(struct devfreq *devfreq);
227 int devfreq_resume_device(struct devfreq *devfreq);
229 void devfreq_suspend(void);
230 void devfreq_resume(void);
232 /* update_devfreq() - Reevaluate the device and configure frequency */
233 int update_devfreq(struct devfreq *devfreq);
235 /* Helper functions for devfreq user device driver with OPP. */
236 struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
237 unsigned long *freq, u32 flags);
238 int devfreq_register_opp_notifier(struct device *dev,
239 struct devfreq *devfreq);
240 int devfreq_unregister_opp_notifier(struct device *dev,
241 struct devfreq *devfreq);
242 int devm_devfreq_register_opp_notifier(struct device *dev,
243 struct devfreq *devfreq);
244 void devm_devfreq_unregister_opp_notifier(struct device *dev,
245 struct devfreq *devfreq);
246 int devfreq_register_notifier(struct devfreq *devfreq,
247 struct notifier_block *nb,
249 int devfreq_unregister_notifier(struct devfreq *devfreq,
250 struct notifier_block *nb,
252 int devm_devfreq_register_notifier(struct device *dev,
253 struct devfreq *devfreq,
254 struct notifier_block *nb,
256 void devm_devfreq_unregister_notifier(struct device *dev,
257 struct devfreq *devfreq,
258 struct notifier_block *nb,
260 struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node);
261 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
262 const char *phandle_name, int index);
264 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND)
266 * struct devfreq_simple_ondemand_data - ``void *data`` fed to struct devfreq
267 * and devfreq_add_device
268 * @upthreshold: If the load is over this value, the frequency jumps.
269 * Specify 0 to use the default. Valid value = 0 to 100.
270 * @downdifferential: If the load is under upthreshold - downdifferential,
271 * the governor may consider slowing the frequency down.
272 * Specify 0 to use the default. Valid value = 0 to 100.
273 * downdifferential < upthreshold must hold.
275 * If the fed devfreq_simple_ondemand_data pointer is NULL to the governor,
276 * the governor uses the default values.
278 struct devfreq_simple_ondemand_data {
279 unsigned int upthreshold;
280 unsigned int downdifferential;
284 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE)
286 * struct devfreq_passive_data - ``void *data`` fed to struct devfreq
287 * and devfreq_add_device
288 * @parent: the devfreq instance of parent device.
289 * @get_target_freq: Optional callback, Returns desired operating frequency
290 * for the device using passive governor. That is called
291 * when passive governor should decide the next frequency
292 * by using the new frequency of parent devfreq device
293 * using governors except for passive governor.
294 * If the devfreq device has the specific method to decide
295 * the next frequency, should use this callback.
296 * @this: the devfreq instance of own device.
297 * @nb: the notifier block for DEVFREQ_TRANSITION_NOTIFIER list
299 * The devfreq_passive_data have to set the devfreq instance of parent
300 * device with governors except for the passive governor. But, don't need to
301 * initialize the 'this' and 'nb' field because the devfreq core will handle
304 struct devfreq_passive_data {
305 /* Should set the devfreq instance of parent device */
306 struct devfreq *parent;
308 /* Optional callback to decide the next frequency of passvice device */
309 int (*get_target_freq)(struct devfreq *this, unsigned long *freq);
311 /* For passive governor's internal use. Don't need to set them */
312 struct devfreq *this;
313 struct notifier_block nb;
317 #else /* !CONFIG_PM_DEVFREQ */
318 static inline struct devfreq *devfreq_add_device(struct device *dev,
319 struct devfreq_dev_profile *profile,
320 const char *governor_name,
323 return ERR_PTR(-ENOSYS);
326 static inline int devfreq_remove_device(struct devfreq *devfreq)
331 static inline struct devfreq *devm_devfreq_add_device(struct device *dev,
332 struct devfreq_dev_profile *profile,
333 const char *governor_name,
336 return ERR_PTR(-ENOSYS);
339 static inline void devm_devfreq_remove_device(struct device *dev,
340 struct devfreq *devfreq)
344 static inline int devfreq_suspend_device(struct devfreq *devfreq)
349 static inline int devfreq_resume_device(struct devfreq *devfreq)
354 static inline void devfreq_suspend(void) {}
355 static inline void devfreq_resume(void) {}
357 static inline struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
358 unsigned long *freq, u32 flags)
360 return ERR_PTR(-EINVAL);
363 static inline int devfreq_register_opp_notifier(struct device *dev,
364 struct devfreq *devfreq)
369 static inline int devfreq_unregister_opp_notifier(struct device *dev,
370 struct devfreq *devfreq)
375 static inline int devm_devfreq_register_opp_notifier(struct device *dev,
376 struct devfreq *devfreq)
381 static inline void devm_devfreq_unregister_opp_notifier(struct device *dev,
382 struct devfreq *devfreq)
386 static inline int devfreq_register_notifier(struct devfreq *devfreq,
387 struct notifier_block *nb,
393 static inline int devfreq_unregister_notifier(struct devfreq *devfreq,
394 struct notifier_block *nb,
400 static inline int devm_devfreq_register_notifier(struct device *dev,
401 struct devfreq *devfreq,
402 struct notifier_block *nb,
408 static inline void devm_devfreq_unregister_notifier(struct device *dev,
409 struct devfreq *devfreq,
410 struct notifier_block *nb,
415 static inline struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node)
417 return ERR_PTR(-ENODEV);
420 static inline struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
421 const char *phandle_name, int index)
423 return ERR_PTR(-ENODEV);
426 static inline int devfreq_update_stats(struct devfreq *df)
430 #endif /* CONFIG_PM_DEVFREQ */
432 #endif /* __LINUX_DEVFREQ_H__ */