]>
Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
a3c98b8b MH |
2 | /* |
3 | * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework | |
4 | * for Non-CPU Devices. | |
5 | * | |
6 | * Copyright (C) 2011 Samsung Electronics | |
7 | * MyungJoo Ham <myungjoo.ham@samsung.com> | |
a3c98b8b MH |
8 | */ |
9 | ||
10 | #ifndef __LINUX_DEVFREQ_H__ | |
11 | #define __LINUX_DEVFREQ_H__ | |
12 | ||
13 | #include <linux/device.h> | |
14 | #include <linux/notifier.h> | |
e4db1c74 | 15 | #include <linux/pm_opp.h> |
27dbc542 | 16 | #include <linux/pm_qos.h> |
a3c98b8b | 17 | |
aa7c352f CC |
18 | /* DEVFREQ governor name */ |
19 | #define DEVFREQ_GOV_SIMPLE_ONDEMAND "simple_ondemand" | |
20 | #define DEVFREQ_GOV_PERFORMANCE "performance" | |
21 | #define DEVFREQ_GOV_POWERSAVE "powersave" | |
22 | #define DEVFREQ_GOV_USERSPACE "userspace" | |
23 | #define DEVFREQ_GOV_PASSIVE "passive" | |
24 | ||
0fe3a664 CC |
25 | /* DEVFREQ notifier interface */ |
26 | #define DEVFREQ_TRANSITION_NOTIFIER (0) | |
27 | ||
28 | /* Transition notifiers of DEVFREQ_TRANSITION_NOTIFIER */ | |
29 | #define DEVFREQ_PRECHANGE (0) | |
30 | #define DEVFREQ_POSTCHANGE (1) | |
31 | ||
4dc3bab8 CC |
32 | /* DEVFREQ work timers */ |
33 | enum devfreq_timer { | |
34 | DEVFREQ_TIMER_DEFERRABLE = 0, | |
35 | DEVFREQ_TIMER_DELAYED, | |
36 | DEVFREQ_TIMER_NUM, | |
37 | }; | |
38 | ||
a3c98b8b | 39 | struct devfreq; |
3ea6b700 | 40 | struct devfreq_governor; |
1224451b | 41 | struct thermal_cooling_device; |
a3c98b8b MH |
42 | |
43 | /** | |
44 | * struct devfreq_dev_status - Data given from devfreq user device to | |
45 | * governors. Represents the performance | |
46 | * statistics. | |
e09651fc | 47 | * @total_time: The total time represented by this instance of |
a3c98b8b | 48 | * devfreq_dev_status |
e09651fc | 49 | * @busy_time: The time that the device was working among the |
a3c98b8b | 50 | * total_time. |
e09651fc NM |
51 | * @current_frequency: The operating frequency. |
52 | * @private_data: An entry not specified by the devfreq framework. | |
a3c98b8b MH |
53 | * A device and a specific governor may have their |
54 | * own protocol with private_data. However, because | |
55 | * this is governor-specific, a governor using this | |
56 | * will be only compatible with devices aware of it. | |
57 | */ | |
58 | struct devfreq_dev_status { | |
59 | /* both since the last measure */ | |
60 | unsigned long total_time; | |
61 | unsigned long busy_time; | |
62 | unsigned long current_frequency; | |
1a51cfdc | 63 | void *private_data; |
a3c98b8b MH |
64 | }; |
65 | ||
ab5f299f MH |
66 | /* |
67 | * The resulting frequency should be at most this. (this bound is the | |
68 | * least upper bound; thus, the resulting freq should be lower or same) | |
69 | * If the flag is not set, the resulting frequency should be at most the | |
70 | * bound (greatest lower bound) | |
71 | */ | |
72 | #define DEVFREQ_FLAG_LEAST_UPPER_BOUND 0x1 | |
73 | ||
a3c98b8b MH |
74 | /** |
75 | * struct devfreq_dev_profile - Devfreq's user device profile | |
e09651fc | 76 | * @initial_freq: The operating frequency when devfreq_add_device() is |
a3c98b8b | 77 | * called. |
e09651fc | 78 | * @polling_ms: The polling interval in ms. 0 disables polling. |
4dc3bab8 | 79 | * @timer: Timer type is either deferrable or delayed timer. |
e09651fc | 80 | * @target: The device should set its operating frequency at |
a3c98b8b MH |
81 | * freq or lowest-upper-than-freq value. If freq is |
82 | * higher than any operable frequency, set maximum. | |
83 | * Before returning, target function should set | |
84 | * freq at the current frequency. | |
ab5f299f MH |
85 | * The "flags" parameter's possible values are |
86 | * explained above with "DEVFREQ_FLAG_*" macros. | |
e09651fc | 87 | * @get_dev_status: The device should provide the current performance |
d54cdf3f MH |
88 | * status to devfreq. Governors are recommended not to |
89 | * use this directly. Instead, governors are recommended | |
90 | * to use devfreq_update_stats() along with | |
91 | * devfreq.last_status. | |
e09651fc | 92 | * @get_cur_freq: The device should provide the current frequency |
7f98a905 | 93 | * at which it is operating. |
e09651fc | 94 | * @exit: An optional callback that is called when devfreq |
a3c98b8b MH |
95 | * is removing the devfreq object due to error or |
96 | * from devfreq_remove_device() call. If the user | |
97 | * has registered devfreq->nb at a notifier-head, | |
98 | * this is the time to unregister it. | |
416b46a2 CC |
99 | * @freq_table: Optional list of frequencies to support statistics |
100 | * and freq_table must be generated in ascending order. | |
101 | * @max_state: The size of freq_table. | |
1224451b DL |
102 | * |
103 | * @is_cooling_device: A self-explanatory boolean giving the device a | |
104 | * cooling effect property. | |
a3c98b8b MH |
105 | */ |
106 | struct devfreq_dev_profile { | |
107 | unsigned long initial_freq; | |
108 | unsigned int polling_ms; | |
4dc3bab8 | 109 | enum devfreq_timer timer; |
1224451b | 110 | bool is_cooling_device; |
a3c98b8b | 111 | |
ab5f299f | 112 | int (*target)(struct device *dev, unsigned long *freq, u32 flags); |
a3c98b8b MH |
113 | int (*get_dev_status)(struct device *dev, |
114 | struct devfreq_dev_status *stat); | |
7f98a905 | 115 | int (*get_cur_freq)(struct device *dev, unsigned long *freq); |
a3c98b8b | 116 | void (*exit)(struct device *dev); |
e552bbaf | 117 | |
0ec09ac2 | 118 | unsigned long *freq_table; |
e552bbaf | 119 | unsigned int max_state; |
a3c98b8b MH |
120 | }; |
121 | ||
1ebd0bc0 KK |
122 | /** |
123 | * struct devfreq_stats - Statistics of devfreq device behavior | |
124 | * @total_trans: Number of devfreq transitions. | |
125 | * @trans_table: Statistics of devfreq transitions. | |
126 | * @time_in_state: Statistics of devfreq states. | |
127 | * @last_update: The last time stats were updated. | |
128 | */ | |
129 | struct devfreq_stats { | |
130 | unsigned int total_trans; | |
131 | unsigned int *trans_table; | |
132 | u64 *time_in_state; | |
133 | u64 last_update; | |
134 | }; | |
135 | ||
a3c98b8b MH |
136 | /** |
137 | * struct devfreq - Device devfreq structure | |
e09651fc | 138 | * @node: list node - contains the devices with devfreq that have been |
a3c98b8b | 139 | * registered. |
e09651fc NM |
140 | * @lock: a mutex to protect accessing devfreq. |
141 | * @dev: device registered by devfreq class. dev.parent is the device | |
a3c98b8b | 142 | * using devfreq. |
e09651fc NM |
143 | * @profile: device-specific devfreq profile |
144 | * @governor: method how to choose frequency based on the usage. | |
26f9c7cc | 145 | * @opp_table: Reference to OPP table of dev.parent, if one exists. |
e09651fc | 146 | * @nb: notifier block used to notify devfreq object that it should |
a3c98b8b MH |
147 | * reevaluate operable frequencies. Devfreq users may use |
148 | * devfreq.nb to the corresponding register notifier call chain. | |
e09651fc NM |
149 | * @work: delayed work for load monitoring. |
150 | * @previous_freq: previously configured frequency value. | |
54cb5740 | 151 | * @last_status: devfreq user device info, performance statistics |
e09651fc | 152 | * @data: Private data of the governor. The devfreq framework does not |
a3c98b8b | 153 | * touch this. |
27dbc542 LC |
154 | * @user_min_freq_req: PM QoS minimum frequency request from user (via sysfs) |
155 | * @user_max_freq_req: PM QoS maximum frequency request from user (via sysfs) | |
f1d981ea CC |
156 | * @scaling_min_freq: Limit minimum frequency requested by OPP interface |
157 | * @scaling_max_freq: Limit maximum frequency requested by OPP interface | |
e09651fc | 158 | * @stop_polling: devfreq polling status of a device. |
83f8ca45 LL |
159 | * @suspend_freq: frequency of a device set during suspend phase. |
160 | * @resume_freq: frequency of a device set in resume phase. | |
161 | * @suspend_count: suspend requests counter for a device. | |
1ebd0bc0 | 162 | * @stats: Statistics of devfreq device behavior |
0fe3a664 | 163 | * @transition_notifier_list: list head of DEVFREQ_TRANSITION_NOTIFIER notifier |
1224451b | 164 | * @cdev: Cooling device pointer if the devfreq has cooling property |
05d7ae15 LC |
165 | * @nb_min: Notifier block for DEV_PM_QOS_MIN_FREQUENCY |
166 | * @nb_max: Notifier block for DEV_PM_QOS_MAX_FREQUENCY | |
a3c98b8b | 167 | * |
54cb5740 | 168 | * This structure stores the devfreq information for a given device. |
a3c98b8b MH |
169 | * |
170 | * Note that when a governor accesses entries in struct devfreq in its | |
171 | * functions except for the context of callbacks defined in struct | |
172 | * devfreq_governor, the governor should protect its access with the | |
173 | * struct mutex lock in struct devfreq. A governor may use this mutex | |
7a51320e | 174 | * to protect its own private data in ``void *data`` as well. |
a3c98b8b MH |
175 | */ |
176 | struct devfreq { | |
177 | struct list_head node; | |
178 | ||
179 | struct mutex lock; | |
180 | struct device dev; | |
181 | struct devfreq_dev_profile *profile; | |
182 | const struct devfreq_governor *governor; | |
26f9c7cc | 183 | struct opp_table *opp_table; |
a3c98b8b | 184 | struct notifier_block nb; |
7e6fdd4b | 185 | struct delayed_work work; |
a3c98b8b | 186 | |
a3c98b8b | 187 | unsigned long previous_freq; |
08e75e75 | 188 | struct devfreq_dev_status last_status; |
a3c98b8b MH |
189 | |
190 | void *data; /* private data for governors */ | |
191 | ||
27dbc542 LC |
192 | struct dev_pm_qos_request user_min_freq_req; |
193 | struct dev_pm_qos_request user_max_freq_req; | |
f1d981ea CC |
194 | unsigned long scaling_min_freq; |
195 | unsigned long scaling_max_freq; | |
7e6fdd4b | 196 | bool stop_polling; |
e552bbaf | 197 | |
83f8ca45 LL |
198 | unsigned long suspend_freq; |
199 | unsigned long resume_freq; | |
200 | atomic_t suspend_count; | |
201 | ||
1ebd0bc0 KK |
202 | /* information for device frequency transitions */ |
203 | struct devfreq_stats stats; | |
0fe3a664 CC |
204 | |
205 | struct srcu_notifier_head transition_notifier_list; | |
05d7ae15 | 206 | |
1224451b DL |
207 | /* Pointer to the cooling device if used for thermal mitigation */ |
208 | struct thermal_cooling_device *cdev; | |
209 | ||
05d7ae15 LC |
210 | struct notifier_block nb_min; |
211 | struct notifier_block nb_max; | |
0fe3a664 CC |
212 | }; |
213 | ||
214 | struct devfreq_freqs { | |
215 | unsigned long old; | |
216 | unsigned long new; | |
a3c98b8b MH |
217 | }; |
218 | ||
219 | #if defined(CONFIG_PM_DEVFREQ) | |
6d743493 CC |
220 | struct devfreq *devfreq_add_device(struct device *dev, |
221 | struct devfreq_dev_profile *profile, | |
222 | const char *governor_name, | |
223 | void *data); | |
224 | int devfreq_remove_device(struct devfreq *devfreq); | |
225 | struct devfreq *devm_devfreq_add_device(struct device *dev, | |
226 | struct devfreq_dev_profile *profile, | |
227 | const char *governor_name, | |
228 | void *data); | |
229 | void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq); | |
de9c7394 | 230 | |
464ed18e | 231 | /* Supposed to be called by PM callbacks */ |
6d743493 CC |
232 | int devfreq_suspend_device(struct devfreq *devfreq); |
233 | int devfreq_resume_device(struct devfreq *devfreq); | |
a3c98b8b | 234 | |
6d743493 CC |
235 | void devfreq_suspend(void); |
236 | void devfreq_resume(void); | |
59031956 | 237 | |
3e2ac979 | 238 | /* update_devfreq() - Reevaluate the device and configure frequency */ |
6d743493 | 239 | int update_devfreq(struct devfreq *devfreq); |
b596d895 | 240 | |
a3c98b8b | 241 | /* Helper functions for devfreq user device driver with OPP. */ |
6d743493 CC |
242 | struct dev_pm_opp *devfreq_recommended_opp(struct device *dev, |
243 | unsigned long *freq, u32 flags); | |
244 | int devfreq_register_opp_notifier(struct device *dev, | |
245 | struct devfreq *devfreq); | |
246 | int devfreq_unregister_opp_notifier(struct device *dev, | |
247 | struct devfreq *devfreq); | |
248 | int devm_devfreq_register_opp_notifier(struct device *dev, | |
249 | struct devfreq *devfreq); | |
250 | void devm_devfreq_unregister_opp_notifier(struct device *dev, | |
251 | struct devfreq *devfreq); | |
252 | int devfreq_register_notifier(struct devfreq *devfreq, | |
253 | struct notifier_block *nb, | |
254 | unsigned int list); | |
255 | int devfreq_unregister_notifier(struct devfreq *devfreq, | |
256 | struct notifier_block *nb, | |
257 | unsigned int list); | |
258 | int devm_devfreq_register_notifier(struct device *dev, | |
0fe3a664 CC |
259 | struct devfreq *devfreq, |
260 | struct notifier_block *nb, | |
261 | unsigned int list); | |
6d743493 | 262 | void devm_devfreq_unregister_notifier(struct device *dev, |
0fe3a664 CC |
263 | struct devfreq *devfreq, |
264 | struct notifier_block *nb, | |
265 | unsigned int list); | |
7b38b7b0 | 266 | struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node); |
86d90fd9 CC |
267 | struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, |
268 | const char *phandle_name, int index); | |
8f510aeb | 269 | |
883d588e | 270 | #if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND) |
ce26c5bb | 271 | /** |
7a51320e | 272 | * struct devfreq_simple_ondemand_data - ``void *data`` fed to struct devfreq |
ce26c5bb | 273 | * and devfreq_add_device |
e09651fc | 274 | * @upthreshold: If the load is over this value, the frequency jumps. |
ce26c5bb | 275 | * Specify 0 to use the default. Valid value = 0 to 100. |
e09651fc | 276 | * @downdifferential: If the load is under upthreshold - downdifferential, |
ce26c5bb MH |
277 | * the governor may consider slowing the frequency down. |
278 | * Specify 0 to use the default. Valid value = 0 to 100. | |
279 | * downdifferential < upthreshold must hold. | |
280 | * | |
281 | * If the fed devfreq_simple_ondemand_data pointer is NULL to the governor, | |
282 | * the governor uses the default values. | |
283 | */ | |
284 | struct devfreq_simple_ondemand_data { | |
285 | unsigned int upthreshold; | |
286 | unsigned int downdifferential; | |
287 | }; | |
288 | #endif | |
289 | ||
99613311 CC |
290 | #if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE) |
291 | /** | |
7a51320e | 292 | * struct devfreq_passive_data - ``void *data`` fed to struct devfreq |
99613311 CC |
293 | * and devfreq_add_device |
294 | * @parent: the devfreq instance of parent device. | |
295 | * @get_target_freq: Optional callback, Returns desired operating frequency | |
296 | * for the device using passive governor. That is called | |
297 | * when passive governor should decide the next frequency | |
298 | * by using the new frequency of parent devfreq device | |
299 | * using governors except for passive governor. | |
300 | * If the devfreq device has the specific method to decide | |
301 | * the next frequency, should use this callback. | |
302 | * @this: the devfreq instance of own device. | |
303 | * @nb: the notifier block for DEVFREQ_TRANSITION_NOTIFIER list | |
304 | * | |
305 | * The devfreq_passive_data have to set the devfreq instance of parent | |
306 | * device with governors except for the passive governor. But, don't need to | |
307 | * initialize the 'this' and 'nb' field because the devfreq core will handle | |
308 | * them. | |
309 | */ | |
310 | struct devfreq_passive_data { | |
311 | /* Should set the devfreq instance of parent device */ | |
312 | struct devfreq *parent; | |
313 | ||
314 | /* Optional callback to decide the next frequency of passvice device */ | |
315 | int (*get_target_freq)(struct devfreq *this, unsigned long *freq); | |
316 | ||
317 | /* For passive governor's internal use. Don't need to set them */ | |
318 | struct devfreq *this; | |
319 | struct notifier_block nb; | |
320 | }; | |
321 | #endif | |
322 | ||
a3c98b8b | 323 | #else /* !CONFIG_PM_DEVFREQ */ |
5faaa035 | 324 | static inline struct devfreq *devfreq_add_device(struct device *dev, |
6d743493 CC |
325 | struct devfreq_dev_profile *profile, |
326 | const char *governor_name, | |
327 | void *data) | |
a3c98b8b | 328 | { |
8cd84092 | 329 | return ERR_PTR(-ENOSYS); |
a3c98b8b MH |
330 | } |
331 | ||
5faaa035 | 332 | static inline int devfreq_remove_device(struct devfreq *devfreq) |
a3c98b8b MH |
333 | { |
334 | return 0; | |
335 | } | |
336 | ||
8cd84092 CC |
337 | static inline struct devfreq *devm_devfreq_add_device(struct device *dev, |
338 | struct devfreq_dev_profile *profile, | |
339 | const char *governor_name, | |
340 | void *data) | |
341 | { | |
342 | return ERR_PTR(-ENOSYS); | |
343 | } | |
344 | ||
345 | static inline void devm_devfreq_remove_device(struct device *dev, | |
346 | struct devfreq *devfreq) | |
347 | { | |
348 | } | |
349 | ||
5faaa035 | 350 | static inline int devfreq_suspend_device(struct devfreq *devfreq) |
206c30cf RV |
351 | { |
352 | return 0; | |
353 | } | |
354 | ||
5faaa035 | 355 | static inline int devfreq_resume_device(struct devfreq *devfreq) |
206c30cf RV |
356 | { |
357 | return 0; | |
358 | } | |
359 | ||
59031956 LL |
360 | static inline void devfreq_suspend(void) {} |
361 | static inline void devfreq_resume(void) {} | |
362 | ||
47d43ba7 | 363 | static inline struct dev_pm_opp *devfreq_recommended_opp(struct device *dev, |
6d743493 | 364 | unsigned long *freq, u32 flags) |
a3c98b8b | 365 | { |
5faaa035 | 366 | return ERR_PTR(-EINVAL); |
a3c98b8b MH |
367 | } |
368 | ||
5faaa035 | 369 | static inline int devfreq_register_opp_notifier(struct device *dev, |
6d743493 | 370 | struct devfreq *devfreq) |
a3c98b8b MH |
371 | { |
372 | return -EINVAL; | |
373 | } | |
374 | ||
5faaa035 | 375 | static inline int devfreq_unregister_opp_notifier(struct device *dev, |
6d743493 | 376 | struct devfreq *devfreq) |
a3c98b8b MH |
377 | { |
378 | return -EINVAL; | |
379 | } | |
380 | ||
d5b040d0 | 381 | static inline int devm_devfreq_register_opp_notifier(struct device *dev, |
6d743493 | 382 | struct devfreq *devfreq) |
d5b040d0 CC |
383 | { |
384 | return -EINVAL; | |
385 | } | |
386 | ||
387 | static inline void devm_devfreq_unregister_opp_notifier(struct device *dev, | |
6d743493 | 388 | struct devfreq *devfreq) |
d5b040d0 CC |
389 | { |
390 | } | |
08e75e75 | 391 | |
0fe3a664 CC |
392 | static inline int devfreq_register_notifier(struct devfreq *devfreq, |
393 | struct notifier_block *nb, | |
394 | unsigned int list) | |
395 | { | |
396 | return 0; | |
397 | } | |
398 | ||
399 | static inline int devfreq_unregister_notifier(struct devfreq *devfreq, | |
400 | struct notifier_block *nb, | |
401 | unsigned int list) | |
402 | { | |
403 | return 0; | |
404 | } | |
405 | ||
406 | static inline int devm_devfreq_register_notifier(struct device *dev, | |
6d743493 CC |
407 | struct devfreq *devfreq, |
408 | struct notifier_block *nb, | |
409 | unsigned int list) | |
0fe3a664 CC |
410 | { |
411 | return 0; | |
412 | } | |
413 | ||
414 | static inline void devm_devfreq_unregister_notifier(struct device *dev, | |
6d743493 CC |
415 | struct devfreq *devfreq, |
416 | struct notifier_block *nb, | |
417 | unsigned int list) | |
0fe3a664 CC |
418 | { |
419 | } | |
420 | ||
7b38b7b0 LC |
421 | static inline struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node) |
422 | { | |
423 | return ERR_PTR(-ENODEV); | |
424 | } | |
425 | ||
8f510aeb | 426 | static inline struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, |
86d90fd9 | 427 | const char *phandle_name, int index) |
8f510aeb CC |
428 | { |
429 | return ERR_PTR(-ENODEV); | |
430 | } | |
431 | ||
08e75e75 JM |
432 | static inline int devfreq_update_stats(struct devfreq *df) |
433 | { | |
434 | return -EINVAL; | |
435 | } | |
a3c98b8b MH |
436 | #endif /* CONFIG_PM_DEVFREQ */ |
437 | ||
438 | #endif /* __LINUX_DEVFREQ_H__ */ |