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