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