2 * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
5 * Copyright (C) 2011 Samsung Electronics
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #ifndef __LINUX_DEVFREQ_H__
14 #define __LINUX_DEVFREQ_H__
16 #include <linux/device.h>
17 #include <linux/notifier.h>
18 #include <linux/pm_opp.h>
20 #define DEVFREQ_NAME_LEN 16
22 /* DEVFREQ notifier interface */
23 #define DEVFREQ_TRANSITION_NOTIFIER (0)
25 /* Transition notifiers of DEVFREQ_TRANSITION_NOTIFIER */
26 #define DEVFREQ_PRECHANGE (0)
27 #define DEVFREQ_POSTCHANGE (1)
30 struct devfreq_governor;
33 * struct devfreq_dev_status - Data given from devfreq user device to
34 * governors. Represents the performance
36 * @total_time: The total time represented by this instance of
38 * @busy_time: The time that the device was working among the
40 * @current_frequency: The operating frequency.
41 * @private_data: An entry not specified by the devfreq framework.
42 * A device and a specific governor may have their
43 * own protocol with private_data. However, because
44 * this is governor-specific, a governor using this
45 * will be only compatible with devices aware of it.
47 struct devfreq_dev_status {
48 /* both since the last measure */
49 unsigned long total_time;
50 unsigned long busy_time;
51 unsigned long current_frequency;
56 * The resulting frequency should be at most this. (this bound is the
57 * least upper bound; thus, the resulting freq should be lower or same)
58 * If the flag is not set, the resulting frequency should be at most the
59 * bound (greatest lower bound)
61 #define DEVFREQ_FLAG_LEAST_UPPER_BOUND 0x1
64 * struct devfreq_dev_profile - Devfreq's user device profile
65 * @initial_freq: The operating frequency when devfreq_add_device() is
67 * @polling_ms: The polling interval in ms. 0 disables polling.
68 * @target: The device should set its operating frequency at
69 * freq or lowest-upper-than-freq value. If freq is
70 * higher than any operable frequency, set maximum.
71 * Before returning, target function should set
72 * freq at the current frequency.
73 * The "flags" parameter's possible values are
74 * explained above with "DEVFREQ_FLAG_*" macros.
75 * @get_dev_status: The device should provide the current performance
76 * status to devfreq. Governors are recommended not to
77 * use this directly. Instead, governors are recommended
78 * to use devfreq_update_stats() along with
79 * devfreq.last_status.
80 * @get_cur_freq: The device should provide the current frequency
81 * at which it is operating.
82 * @exit: An optional callback that is called when devfreq
83 * is removing the devfreq object due to error or
84 * from devfreq_remove_device() call. If the user
85 * has registered devfreq->nb at a notifier-head,
86 * this is the time to unregister it.
87 * @freq_table: Optional list of frequencies to support statistics.
88 * @max_state: The size of freq_table.
90 struct devfreq_dev_profile {
91 unsigned long initial_freq;
92 unsigned int polling_ms;
94 int (*target)(struct device *dev, unsigned long *freq, u32 flags);
95 int (*get_dev_status)(struct device *dev,
96 struct devfreq_dev_status *stat);
97 int (*get_cur_freq)(struct device *dev, unsigned long *freq);
98 void (*exit)(struct device *dev);
100 unsigned long *freq_table;
101 unsigned int max_state;
105 * struct devfreq - Device devfreq structure
106 * @node: list node - contains the devices with devfreq that have been
108 * @lock: a mutex to protect accessing devfreq.
109 * @dev: device registered by devfreq class. dev.parent is the device
111 * @profile: device-specific devfreq profile
112 * @governor: method how to choose frequency based on the usage.
113 * @governor_name: devfreq governor name for use with this devfreq
114 * @nb: notifier block used to notify devfreq object that it should
115 * reevaluate operable frequencies. Devfreq users may use
116 * devfreq.nb to the corresponding register notifier call chain.
117 * @work: delayed work for load monitoring.
118 * @previous_freq: previously configured frequency value.
119 * @data: Private data of the governor. The devfreq framework does not
121 * @min_freq: Limit minimum frequency requested by user (0: none)
122 * @max_freq: Limit maximum frequency requested by user (0: none)
123 * @stop_polling: devfreq polling status of a device.
124 * @total_trans: Number of devfreq transitions
125 * @trans_table: Statistics of devfreq transitions
126 * @time_in_state: Statistics of devfreq states
127 * @last_stat_updated: The last time stat updated
128 * @transition_notifier_list: list head of DEVFREQ_TRANSITION_NOTIFIER notifier
130 * This structure stores the devfreq information for a give device.
132 * Note that when a governor accesses entries in struct devfreq in its
133 * functions except for the context of callbacks defined in struct
134 * devfreq_governor, the governor should protect its access with the
135 * struct mutex lock in struct devfreq. A governor may use this mutex
136 * to protect its own private data in void *data as well.
139 struct list_head node;
143 struct devfreq_dev_profile *profile;
144 const struct devfreq_governor *governor;
145 char governor_name[DEVFREQ_NAME_LEN];
146 struct notifier_block nb;
147 struct delayed_work work;
149 unsigned long previous_freq;
150 struct devfreq_dev_status last_status;
152 void *data; /* private data for governors */
154 unsigned long min_freq;
155 unsigned long max_freq;
158 /* information for device frequency transition */
159 unsigned int total_trans;
160 unsigned int *trans_table;
161 unsigned long *time_in_state;
162 unsigned long last_stat_updated;
164 struct srcu_notifier_head transition_notifier_list;
167 struct devfreq_freqs {
172 #if defined(CONFIG_PM_DEVFREQ)
173 extern struct devfreq *devfreq_add_device(struct device *dev,
174 struct devfreq_dev_profile *profile,
175 const char *governor_name,
177 extern int devfreq_remove_device(struct devfreq *devfreq);
178 extern struct devfreq *devm_devfreq_add_device(struct device *dev,
179 struct devfreq_dev_profile *profile,
180 const char *governor_name,
182 extern void devm_devfreq_remove_device(struct device *dev,
183 struct devfreq *devfreq);
185 /* Supposed to be called by PM callbacks */
186 extern int devfreq_suspend_device(struct devfreq *devfreq);
187 extern int devfreq_resume_device(struct devfreq *devfreq);
189 /* Helper functions for devfreq user device driver with OPP. */
190 extern struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
191 unsigned long *freq, u32 flags);
192 extern int devfreq_register_opp_notifier(struct device *dev,
193 struct devfreq *devfreq);
194 extern int devfreq_unregister_opp_notifier(struct device *dev,
195 struct devfreq *devfreq);
196 extern int devm_devfreq_register_opp_notifier(struct device *dev,
197 struct devfreq *devfreq);
198 extern void devm_devfreq_unregister_opp_notifier(struct device *dev,
199 struct devfreq *devfreq);
200 extern int devfreq_register_notifier(struct devfreq *devfreq,
201 struct notifier_block *nb,
203 extern int devfreq_unregister_notifier(struct devfreq *devfreq,
204 struct notifier_block *nb,
206 extern int devm_devfreq_register_notifier(struct device *dev,
207 struct devfreq *devfreq,
208 struct notifier_block *nb,
210 extern void devm_devfreq_unregister_notifier(struct device *dev,
211 struct devfreq *devfreq,
212 struct notifier_block *nb,
214 extern struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
217 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND)
219 * struct devfreq_simple_ondemand_data - void *data fed to struct devfreq
220 * and devfreq_add_device
221 * @upthreshold: If the load is over this value, the frequency jumps.
222 * Specify 0 to use the default. Valid value = 0 to 100.
223 * @downdifferential: If the load is under upthreshold - downdifferential,
224 * the governor may consider slowing the frequency down.
225 * Specify 0 to use the default. Valid value = 0 to 100.
226 * downdifferential < upthreshold must hold.
228 * If the fed devfreq_simple_ondemand_data pointer is NULL to the governor,
229 * the governor uses the default values.
231 struct devfreq_simple_ondemand_data {
232 unsigned int upthreshold;
233 unsigned int downdifferential;
237 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE)
239 * struct devfreq_passive_data - void *data fed to struct devfreq
240 * and devfreq_add_device
241 * @parent: the devfreq instance of parent device.
242 * @get_target_freq: Optional callback, Returns desired operating frequency
243 * for the device using passive governor. That is called
244 * when passive governor should decide the next frequency
245 * by using the new frequency of parent devfreq device
246 * using governors except for passive governor.
247 * If the devfreq device has the specific method to decide
248 * the next frequency, should use this callback.
249 * @this: the devfreq instance of own device.
250 * @nb: the notifier block for DEVFREQ_TRANSITION_NOTIFIER list
252 * The devfreq_passive_data have to set the devfreq instance of parent
253 * device with governors except for the passive governor. But, don't need to
254 * initialize the 'this' and 'nb' field because the devfreq core will handle
257 struct devfreq_passive_data {
258 /* Should set the devfreq instance of parent device */
259 struct devfreq *parent;
261 /* Optional callback to decide the next frequency of passvice device */
262 int (*get_target_freq)(struct devfreq *this, unsigned long *freq);
264 /* For passive governor's internal use. Don't need to set them */
265 struct devfreq *this;
266 struct notifier_block nb;
270 #else /* !CONFIG_PM_DEVFREQ */
271 static inline struct devfreq *devfreq_add_device(struct device *dev,
272 struct devfreq_dev_profile *profile,
273 const char *governor_name,
276 return ERR_PTR(-ENOSYS);
279 static inline int devfreq_remove_device(struct devfreq *devfreq)
284 static inline struct devfreq *devm_devfreq_add_device(struct device *dev,
285 struct devfreq_dev_profile *profile,
286 const char *governor_name,
289 return ERR_PTR(-ENOSYS);
292 static inline void devm_devfreq_remove_device(struct device *dev,
293 struct devfreq *devfreq)
297 static inline int devfreq_suspend_device(struct devfreq *devfreq)
302 static inline int devfreq_resume_device(struct devfreq *devfreq)
307 static inline struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
308 unsigned long *freq, u32 flags)
310 return ERR_PTR(-EINVAL);
313 static inline int devfreq_register_opp_notifier(struct device *dev,
314 struct devfreq *devfreq)
319 static inline int devfreq_unregister_opp_notifier(struct device *dev,
320 struct devfreq *devfreq)
325 static inline int devm_devfreq_register_opp_notifier(struct device *dev,
326 struct devfreq *devfreq)
331 static inline void devm_devfreq_unregister_opp_notifier(struct device *dev,
332 struct devfreq *devfreq)
336 static inline int devfreq_register_notifier(struct devfreq *devfreq,
337 struct notifier_block *nb,
343 static inline int devfreq_unregister_notifier(struct devfreq *devfreq,
344 struct notifier_block *nb,
350 static inline int devm_devfreq_register_notifier(struct device *dev,
351 struct devfreq *devfreq,
352 struct notifier_block *nb,
358 static inline void devm_devfreq_unregister_notifier(struct device *dev,
359 struct devfreq *devfreq,
360 struct notifier_block *nb,
365 static inline struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
368 return ERR_PTR(-ENODEV);
371 static inline int devfreq_update_stats(struct devfreq *df)
375 #endif /* CONFIG_PM_DEVFREQ */
377 #endif /* __LINUX_DEVFREQ_H__ */