blob: 121a2430d7f7e9555f33b40e09ebe8bde610213c [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001/* SPDX-License-Identifier: GPL-2.0-only */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
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>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008 */
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>
Olivier Deprez157378f2022-04-04 15:47:50 +020016#include <linux/pm_qos.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000017
18#define DEVFREQ_NAME_LEN 16
19
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"
26
27/* DEVFREQ notifier interface */
28#define DEVFREQ_TRANSITION_NOTIFIER (0)
29
30/* Transition notifiers of DEVFREQ_TRANSITION_NOTIFIER */
31#define DEVFREQ_PRECHANGE (0)
32#define DEVFREQ_POSTCHANGE (1)
33
Olivier Deprez157378f2022-04-04 15:47:50 +020034/* DEVFREQ work timers */
35enum devfreq_timer {
36 DEVFREQ_TIMER_DEFERRABLE = 0,
37 DEVFREQ_TIMER_DELAYED,
38 DEVFREQ_TIMER_NUM,
39};
40
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000041struct devfreq;
42struct devfreq_governor;
43
44/**
45 * struct devfreq_dev_status - Data given from devfreq user device to
46 * governors. Represents the performance
47 * statistics.
48 * @total_time: The total time represented by this instance of
49 * devfreq_dev_status
50 * @busy_time: The time that the device was working among the
51 * total_time.
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.
58 */
59struct devfreq_dev_status {
60 /* both since the last measure */
61 unsigned long total_time;
62 unsigned long busy_time;
63 unsigned long current_frequency;
64 void *private_data;
65};
66
67/*
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)
72 */
73#define DEVFREQ_FLAG_LEAST_UPPER_BOUND 0x1
74
75/**
76 * struct devfreq_dev_profile - Devfreq's user device profile
77 * @initial_freq: The operating frequency when devfreq_add_device() is
78 * called.
79 * @polling_ms: The polling interval in ms. 0 disables polling.
Olivier Deprez157378f2022-04-04 15:47:50 +020080 * @timer: Timer type is either deferrable or delayed timer.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000081 * @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.
103 */
104struct devfreq_dev_profile {
105 unsigned long initial_freq;
106 unsigned int polling_ms;
Olivier Deprez157378f2022-04-04 15:47:50 +0200107 enum devfreq_timer timer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000108
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);
114
115 unsigned long *freq_table;
116 unsigned int max_state;
117};
118
119/**
Olivier Deprez157378f2022-04-04 15:47:50 +0200120 * 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.
125 */
126struct devfreq_stats {
127 unsigned int total_trans;
128 unsigned int *trans_table;
129 u64 *time_in_state;
130 u64 last_update;
131};
132
133/**
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000134 * struct devfreq - Device devfreq structure
135 * @node: list node - contains the devices with devfreq that have been
136 * registered.
137 * @lock: a mutex to protect accessing devfreq.
138 * @dev: device registered by devfreq class. dev.parent is the device
139 * using devfreq.
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.
Olivier Deprez157378f2022-04-04 15:47:50 +0200148 * @last_status: devfreq user device info, performance statistics
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000149 * @data: Private data of the governor. The devfreq framework does not
150 * touch this.
Olivier Deprez157378f2022-04-04 15:47:50 +0200151 * @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)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000153 * @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.
David Brazdil0f672f62019-12-10 10:32:29 +0000156 * @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.
Olivier Deprez157378f2022-04-04 15:47:50 +0200159 * @stats: Statistics of devfreq device behavior
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000160 * @transition_notifier_list: list head of DEVFREQ_TRANSITION_NOTIFIER notifier
Olivier Deprez157378f2022-04-04 15:47:50 +0200161 * @nb_min: Notifier block for DEV_PM_QOS_MIN_FREQUENCY
162 * @nb_max: Notifier block for DEV_PM_QOS_MAX_FREQUENCY
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000163 *
Olivier Deprez157378f2022-04-04 15:47:50 +0200164 * This structure stores the devfreq information for a given device.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000165 *
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
Olivier Deprez157378f2022-04-04 15:47:50 +0200170 * to protect its own private data in ``void *data`` as well.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000171 */
172struct devfreq {
173 struct list_head node;
174
175 struct mutex lock;
176 struct device dev;
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;
182
183 unsigned long previous_freq;
184 struct devfreq_dev_status last_status;
185
186 void *data; /* private data for governors */
187
Olivier Deprez157378f2022-04-04 15:47:50 +0200188 struct dev_pm_qos_request user_min_freq_req;
189 struct dev_pm_qos_request user_max_freq_req;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000190 unsigned long scaling_min_freq;
191 unsigned long scaling_max_freq;
192 bool stop_polling;
193
David Brazdil0f672f62019-12-10 10:32:29 +0000194 unsigned long suspend_freq;
195 unsigned long resume_freq;
196 atomic_t suspend_count;
197
Olivier Deprez157378f2022-04-04 15:47:50 +0200198 /* information for device frequency transitions */
199 struct devfreq_stats stats;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000200
201 struct srcu_notifier_head transition_notifier_list;
Olivier Deprez157378f2022-04-04 15:47:50 +0200202
203 struct notifier_block nb_min;
204 struct notifier_block nb_max;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000205};
206
207struct devfreq_freqs {
208 unsigned long old;
209 unsigned long new;
210};
211
212#if defined(CONFIG_PM_DEVFREQ)
Olivier Deprez157378f2022-04-04 15:47:50 +0200213struct devfreq *devfreq_add_device(struct device *dev,
214 struct devfreq_dev_profile *profile,
215 const char *governor_name,
216 void *data);
217int devfreq_remove_device(struct devfreq *devfreq);
218struct devfreq *devm_devfreq_add_device(struct device *dev,
219 struct devfreq_dev_profile *profile,
220 const char *governor_name,
221 void *data);
222void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000223
224/* Supposed to be called by PM callbacks */
Olivier Deprez157378f2022-04-04 15:47:50 +0200225int devfreq_suspend_device(struct devfreq *devfreq);
226int devfreq_resume_device(struct devfreq *devfreq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000227
Olivier Deprez157378f2022-04-04 15:47:50 +0200228void devfreq_suspend(void);
229void devfreq_resume(void);
David Brazdil0f672f62019-12-10 10:32:29 +0000230
Olivier Deprez157378f2022-04-04 15:47:50 +0200231/* update_devfreq() - Reevaluate the device and configure frequency */
232int update_devfreq(struct devfreq *devfreq);
David Brazdil0f672f62019-12-10 10:32:29 +0000233
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000234/* Helper functions for devfreq user device driver with OPP. */
Olivier Deprez157378f2022-04-04 15:47:50 +0200235struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
236 unsigned long *freq, u32 flags);
237int devfreq_register_opp_notifier(struct device *dev,
238 struct devfreq *devfreq);
239int devfreq_unregister_opp_notifier(struct device *dev,
240 struct devfreq *devfreq);
241int devm_devfreq_register_opp_notifier(struct device *dev,
242 struct devfreq *devfreq);
243void devm_devfreq_unregister_opp_notifier(struct device *dev,
244 struct devfreq *devfreq);
245int devfreq_register_notifier(struct devfreq *devfreq,
246 struct notifier_block *nb,
247 unsigned int list);
248int devfreq_unregister_notifier(struct devfreq *devfreq,
249 struct notifier_block *nb,
250 unsigned int list);
251int devm_devfreq_register_notifier(struct device *dev,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000252 struct devfreq *devfreq,
253 struct notifier_block *nb,
254 unsigned int list);
Olivier Deprez157378f2022-04-04 15:47:50 +0200255void devm_devfreq_unregister_notifier(struct device *dev,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000256 struct devfreq *devfreq,
257 struct notifier_block *nb,
258 unsigned int list);
Olivier Deprez157378f2022-04-04 15:47:50 +0200259struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node);
260struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
261 const char *phandle_name, int index);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000262
263#if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND)
264/**
Olivier Deprez157378f2022-04-04 15:47:50 +0200265 * struct devfreq_simple_ondemand_data - ``void *data`` fed to struct devfreq
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000266 * and devfreq_add_device
267 * @upthreshold: If the load is over this value, the frequency jumps.
268 * Specify 0 to use the default. Valid value = 0 to 100.
269 * @downdifferential: If the load is under upthreshold - downdifferential,
270 * the governor may consider slowing the frequency down.
271 * Specify 0 to use the default. Valid value = 0 to 100.
272 * downdifferential < upthreshold must hold.
273 *
274 * If the fed devfreq_simple_ondemand_data pointer is NULL to the governor,
275 * the governor uses the default values.
276 */
277struct devfreq_simple_ondemand_data {
278 unsigned int upthreshold;
279 unsigned int downdifferential;
280};
281#endif
282
283#if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE)
284/**
Olivier Deprez157378f2022-04-04 15:47:50 +0200285 * struct devfreq_passive_data - ``void *data`` fed to struct devfreq
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000286 * and devfreq_add_device
287 * @parent: the devfreq instance of parent device.
288 * @get_target_freq: Optional callback, Returns desired operating frequency
289 * for the device using passive governor. That is called
290 * when passive governor should decide the next frequency
291 * by using the new frequency of parent devfreq device
292 * using governors except for passive governor.
293 * If the devfreq device has the specific method to decide
294 * the next frequency, should use this callback.
295 * @this: the devfreq instance of own device.
296 * @nb: the notifier block for DEVFREQ_TRANSITION_NOTIFIER list
297 *
298 * The devfreq_passive_data have to set the devfreq instance of parent
299 * device with governors except for the passive governor. But, don't need to
300 * initialize the 'this' and 'nb' field because the devfreq core will handle
301 * them.
302 */
303struct devfreq_passive_data {
304 /* Should set the devfreq instance of parent device */
305 struct devfreq *parent;
306
307 /* Optional callback to decide the next frequency of passvice device */
308 int (*get_target_freq)(struct devfreq *this, unsigned long *freq);
309
310 /* For passive governor's internal use. Don't need to set them */
311 struct devfreq *this;
312 struct notifier_block nb;
313};
314#endif
315
316#else /* !CONFIG_PM_DEVFREQ */
317static inline struct devfreq *devfreq_add_device(struct device *dev,
Olivier Deprez157378f2022-04-04 15:47:50 +0200318 struct devfreq_dev_profile *profile,
319 const char *governor_name,
320 void *data)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000321{
322 return ERR_PTR(-ENOSYS);
323}
324
325static inline int devfreq_remove_device(struct devfreq *devfreq)
326{
327 return 0;
328}
329
330static inline struct devfreq *devm_devfreq_add_device(struct device *dev,
331 struct devfreq_dev_profile *profile,
332 const char *governor_name,
333 void *data)
334{
335 return ERR_PTR(-ENOSYS);
336}
337
338static inline void devm_devfreq_remove_device(struct device *dev,
339 struct devfreq *devfreq)
340{
341}
342
343static inline int devfreq_suspend_device(struct devfreq *devfreq)
344{
345 return 0;
346}
347
348static inline int devfreq_resume_device(struct devfreq *devfreq)
349{
350 return 0;
351}
352
David Brazdil0f672f62019-12-10 10:32:29 +0000353static inline void devfreq_suspend(void) {}
354static inline void devfreq_resume(void) {}
355
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000356static inline struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
Olivier Deprez157378f2022-04-04 15:47:50 +0200357 unsigned long *freq, u32 flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000358{
359 return ERR_PTR(-EINVAL);
360}
361
362static inline int devfreq_register_opp_notifier(struct device *dev,
Olivier Deprez157378f2022-04-04 15:47:50 +0200363 struct devfreq *devfreq)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000364{
365 return -EINVAL;
366}
367
368static inline int devfreq_unregister_opp_notifier(struct device *dev,
Olivier Deprez157378f2022-04-04 15:47:50 +0200369 struct devfreq *devfreq)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000370{
371 return -EINVAL;
372}
373
374static inline int devm_devfreq_register_opp_notifier(struct device *dev,
Olivier Deprez157378f2022-04-04 15:47:50 +0200375 struct devfreq *devfreq)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000376{
377 return -EINVAL;
378}
379
380static inline void devm_devfreq_unregister_opp_notifier(struct device *dev,
Olivier Deprez157378f2022-04-04 15:47:50 +0200381 struct devfreq *devfreq)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000382{
383}
384
385static inline int devfreq_register_notifier(struct devfreq *devfreq,
386 struct notifier_block *nb,
387 unsigned int list)
388{
389 return 0;
390}
391
392static inline int devfreq_unregister_notifier(struct devfreq *devfreq,
393 struct notifier_block *nb,
394 unsigned int list)
395{
396 return 0;
397}
398
399static inline int devm_devfreq_register_notifier(struct device *dev,
Olivier Deprez157378f2022-04-04 15:47:50 +0200400 struct devfreq *devfreq,
401 struct notifier_block *nb,
402 unsigned int list)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000403{
404 return 0;
405}
406
407static inline void devm_devfreq_unregister_notifier(struct device *dev,
Olivier Deprez157378f2022-04-04 15:47:50 +0200408 struct devfreq *devfreq,
409 struct notifier_block *nb,
410 unsigned int list)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000411{
412}
413
Olivier Deprez157378f2022-04-04 15:47:50 +0200414static inline struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node)
415{
416 return ERR_PTR(-ENODEV);
417}
418
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000419static inline struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
Olivier Deprez157378f2022-04-04 15:47:50 +0200420 const char *phandle_name, int index)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000421{
422 return ERR_PTR(-ENODEV);
423}
424
425static inline int devfreq_update_stats(struct devfreq *df)
426{
427 return -EINVAL;
428}
429#endif /* CONFIG_PM_DEVFREQ */
430
431#endif /* __LINUX_DEVFREQ_H__ */