blob: 2bae9ed3c7831cfe1a22e1e33e411165c520d87a [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001/* SPDX-License-Identifier: GPL-2.0-only */
MyungJoo Hama3c98b82011-10-02 00:19:15 +02002/*
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>
MyungJoo Hama3c98b82011-10-02 00:19:15 +02008 */
9
10#ifndef __LINUX_DEVFREQ_H__
11#define __LINUX_DEVFREQ_H__
12
13#include <linux/device.h>
14#include <linux/notifier.h>
Nishanth Menone4db1c72013-09-19 16:03:52 -050015#include <linux/pm_opp.h>
MyungJoo Hama3c98b82011-10-02 00:19:15 +020016
17#define DEVFREQ_NAME_LEN 16
18
Chanwoo Choiaa7c3522017-10-23 10:32:12 +090019/* 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
Chanwoo Choi0fe3a662016-01-26 13:21:26 +090026/* 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
MyungJoo Hama3c98b82011-10-02 00:19:15 +020033struct devfreq;
Chanwoo Choi3ea6b702017-04-06 13:19:35 +090034struct devfreq_governor;
MyungJoo Hama3c98b82011-10-02 00:19:15 +020035
36/**
37 * struct devfreq_dev_status - Data given from devfreq user device to
38 * governors. Represents the performance
39 * statistics.
Nishanth Menone09651f2012-10-29 08:02:23 -050040 * @total_time: The total time represented by this instance of
MyungJoo Hama3c98b82011-10-02 00:19:15 +020041 * devfreq_dev_status
Nishanth Menone09651f2012-10-29 08:02:23 -050042 * @busy_time: The time that the device was working among the
MyungJoo Hama3c98b82011-10-02 00:19:15 +020043 * total_time.
Nishanth Menone09651f2012-10-29 08:02:23 -050044 * @current_frequency: The operating frequency.
45 * @private_data: An entry not specified by the devfreq framework.
MyungJoo Hama3c98b82011-10-02 00:19:15 +020046 * 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 */
51struct devfreq_dev_status {
52 /* both since the last measure */
53 unsigned long total_time;
54 unsigned long busy_time;
55 unsigned long current_frequency;
Jonathan Corbet1a51cfd2011-11-07 23:54:53 +010056 void *private_data;
MyungJoo Hama3c98b82011-10-02 00:19:15 +020057};
58
MyungJoo Hamab5f2992012-03-16 21:54:53 +010059/*
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
MyungJoo Hama3c98b82011-10-02 00:19:15 +020067/**
68 * struct devfreq_dev_profile - Devfreq's user device profile
Nishanth Menone09651f2012-10-29 08:02:23 -050069 * @initial_freq: The operating frequency when devfreq_add_device() is
MyungJoo Hama3c98b82011-10-02 00:19:15 +020070 * called.
Nishanth Menone09651f2012-10-29 08:02:23 -050071 * @polling_ms: The polling interval in ms. 0 disables polling.
72 * @target: The device should set its operating frequency at
MyungJoo Hama3c98b82011-10-02 00:19:15 +020073 * 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.
MyungJoo Hamab5f2992012-03-16 21:54:53 +010077 * The "flags" parameter's possible values are
78 * explained above with "DEVFREQ_FLAG_*" macros.
Nishanth Menone09651f2012-10-29 08:02:23 -050079 * @get_dev_status: The device should provide the current performance
MyungJoo Hamd54cdf32015-08-18 13:45:49 +090080 * 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.
Nishanth Menone09651f2012-10-29 08:02:23 -050084 * @get_cur_freq: The device should provide the current frequency
Rajagopal Venkat7f98a902012-10-26 01:50:26 +020085 * at which it is operating.
Nishanth Menone09651f2012-10-29 08:02:23 -050086 * @exit: An optional callback that is called when devfreq
MyungJoo Hama3c98b82011-10-02 00:19:15 +020087 * 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.
Chanwoo Choi416b46a2017-10-23 10:32:10 +090091 * @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.
MyungJoo Hama3c98b82011-10-02 00:19:15 +020094 */
95struct devfreq_dev_profile {
96 unsigned long initial_freq;
97 unsigned int polling_ms;
98
MyungJoo Hamab5f2992012-03-16 21:54:53 +010099 int (*target)(struct device *dev, unsigned long *freq, u32 flags);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200100 int (*get_dev_status)(struct device *dev,
101 struct devfreq_dev_status *stat);
Rajagopal Venkat7f98a902012-10-26 01:50:26 +0200102 int (*get_cur_freq)(struct device *dev, unsigned long *freq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200103 void (*exit)(struct device *dev);
Jonghwa Leee552bba2012-08-23 20:00:46 +0900104
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900105 unsigned long *freq_table;
Jonghwa Leee552bba2012-08-23 20:00:46 +0900106 unsigned int max_state;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200107};
108
109/**
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200110 * struct devfreq - Device devfreq structure
Nishanth Menone09651f2012-10-29 08:02:23 -0500111 * @node: list node - contains the devices with devfreq that have been
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200112 * registered.
Nishanth Menone09651f2012-10-29 08:02:23 -0500113 * @lock: a mutex to protect accessing devfreq.
114 * @dev: device registered by devfreq class. dev.parent is the device
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200115 * using devfreq.
Nishanth Menone09651f2012-10-29 08:02:23 -0500116 * @profile: device-specific devfreq profile
117 * @governor: method how to choose frequency based on the usage.
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500118 * @governor_name: devfreq governor name for use with this devfreq
Nishanth Menone09651f2012-10-29 08:02:23 -0500119 * @nb: notifier block used to notify devfreq object that it should
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200120 * reevaluate operable frequencies. Devfreq users may use
121 * devfreq.nb to the corresponding register notifier call chain.
Nishanth Menone09651f2012-10-29 08:02:23 -0500122 * @work: delayed work for load monitoring.
123 * @previous_freq: previously configured frequency value.
124 * @data: Private data of the governor. The devfreq framework does not
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200125 * touch this.
Nishanth Menone09651f2012-10-29 08:02:23 -0500126 * @min_freq: Limit minimum frequency requested by user (0: none)
127 * @max_freq: Limit maximum frequency requested by user (0: none)
Chanwoo Choif1d981e2017-10-23 10:32:08 +0900128 * @scaling_min_freq: Limit minimum frequency requested by OPP interface
129 * @scaling_max_freq: Limit maximum frequency requested by OPP interface
Nishanth Menone09651f2012-10-29 08:02:23 -0500130 * @stop_polling: devfreq polling status of a device.
Lukasz Luba83f8ca42018-12-05 12:05:53 +0100131 * @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.
Jonghwa Leee552bba2012-08-23 20:00:46 +0900134 * @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
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900138 * @transition_notifier_list: list head of DEVFREQ_TRANSITION_NOTIFIER notifier
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200139 *
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 */
148struct 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;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500155 char governor_name[DEVFREQ_NAME_LEN];
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200156 struct notifier_block nb;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200157 struct delayed_work work;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200158
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200159 unsigned long previous_freq;
Javi Merino08e75e72015-08-14 18:56:56 +0100160 struct devfreq_dev_status last_status;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200161
162 void *data; /* private data for governors */
163
MyungJoo Ham6530b9de2011-12-09 16:42:19 +0900164 unsigned long min_freq;
165 unsigned long max_freq;
Chanwoo Choif1d981e2017-10-23 10:32:08 +0900166 unsigned long scaling_min_freq;
167 unsigned long scaling_max_freq;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200168 bool stop_polling;
Jonghwa Leee552bba2012-08-23 20:00:46 +0900169
Lukasz Luba83f8ca42018-12-05 12:05:53 +0100170 unsigned long suspend_freq;
171 unsigned long resume_freq;
172 atomic_t suspend_count;
173
LABBE Corentin05851232013-09-26 16:50:21 +0200174 /* information for device frequency transition */
Jonghwa Leee552bba2012-08-23 20:00:46 +0900175 unsigned int total_trans;
176 unsigned int *trans_table;
177 unsigned long *time_in_state;
178 unsigned long last_stat_updated;
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900179
180 struct srcu_notifier_head transition_notifier_list;
181};
182
183struct devfreq_freqs {
184 unsigned long old;
185 unsigned long new;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200186};
187
188#if defined(CONFIG_PM_DEVFREQ)
189extern struct devfreq *devfreq_add_device(struct device *dev,
190 struct devfreq_dev_profile *profile,
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500191 const char *governor_name,
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200192 void *data);
193extern int devfreq_remove_device(struct devfreq *devfreq);
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900194extern struct devfreq *devm_devfreq_add_device(struct device *dev,
195 struct devfreq_dev_profile *profile,
196 const char *governor_name,
197 void *data);
198extern void devm_devfreq_remove_device(struct device *dev,
199 struct devfreq *devfreq);
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900200
Rafael J. Wysocki464ed182014-12-19 15:37:54 +0100201/* Supposed to be called by PM callbacks */
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200202extern int devfreq_suspend_device(struct devfreq *devfreq);
203extern int devfreq_resume_device(struct devfreq *devfreq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200204
Lukasz Luba59031952018-12-05 12:05:54 +0100205extern void devfreq_suspend(void);
206extern void devfreq_resume(void);
207
Matthias Kaehlckeb596d892018-08-03 13:05:11 -0700208/**
209 * update_devfreq() - Reevaluate the device and configure frequency
210 * @devfreq: the devfreq device
211 *
212 * Note: devfreq->lock must be held
213 */
214extern int update_devfreq(struct devfreq *devfreq);
215
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200216/* Helper functions for devfreq user device driver with OPP. */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500217extern struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100218 unsigned long *freq, u32 flags);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200219extern int devfreq_register_opp_notifier(struct device *dev,
220 struct devfreq *devfreq);
221extern int devfreq_unregister_opp_notifier(struct device *dev,
222 struct devfreq *devfreq);
Chanwoo Choid5b040d2014-05-09 16:43:09 +0900223extern int devm_devfreq_register_opp_notifier(struct device *dev,
224 struct devfreq *devfreq);
225extern void devm_devfreq_unregister_opp_notifier(struct device *dev,
226 struct devfreq *devfreq);
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900227extern int devfreq_register_notifier(struct devfreq *devfreq,
228 struct notifier_block *nb,
229 unsigned int list);
230extern int devfreq_unregister_notifier(struct devfreq *devfreq,
231 struct notifier_block *nb,
232 unsigned int list);
233extern int devm_devfreq_register_notifier(struct device *dev,
234 struct devfreq *devfreq,
235 struct notifier_block *nb,
236 unsigned int list);
237extern void devm_devfreq_unregister_notifier(struct device *dev,
238 struct devfreq *devfreq,
239 struct notifier_block *nb,
240 unsigned int list);
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900241extern struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
242 int index);
243
MyungJoo Ham883d5882012-11-21 17:17:11 +0900244#if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND)
MyungJoo Hamce26c5b2011-10-02 00:19:34 +0200245/**
246 * struct devfreq_simple_ondemand_data - void *data fed to struct devfreq
247 * and devfreq_add_device
Nishanth Menone09651f2012-10-29 08:02:23 -0500248 * @upthreshold: If the load is over this value, the frequency jumps.
MyungJoo Hamce26c5b2011-10-02 00:19:34 +0200249 * Specify 0 to use the default. Valid value = 0 to 100.
Nishanth Menone09651f2012-10-29 08:02:23 -0500250 * @downdifferential: If the load is under upthreshold - downdifferential,
MyungJoo Hamce26c5b2011-10-02 00:19:34 +0200251 * the governor may consider slowing the frequency down.
252 * Specify 0 to use the default. Valid value = 0 to 100.
253 * downdifferential < upthreshold must hold.
254 *
255 * If the fed devfreq_simple_ondemand_data pointer is NULL to the governor,
256 * the governor uses the default values.
257 */
258struct devfreq_simple_ondemand_data {
259 unsigned int upthreshold;
260 unsigned int downdifferential;
261};
262#endif
263
Chanwoo Choi99613312016-03-22 13:44:03 +0900264#if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE)
265/**
266 * struct devfreq_passive_data - void *data fed to struct devfreq
267 * and devfreq_add_device
268 * @parent: the devfreq instance of parent device.
269 * @get_target_freq: Optional callback, Returns desired operating frequency
270 * for the device using passive governor. That is called
271 * when passive governor should decide the next frequency
272 * by using the new frequency of parent devfreq device
273 * using governors except for passive governor.
274 * If the devfreq device has the specific method to decide
275 * the next frequency, should use this callback.
276 * @this: the devfreq instance of own device.
277 * @nb: the notifier block for DEVFREQ_TRANSITION_NOTIFIER list
278 *
279 * The devfreq_passive_data have to set the devfreq instance of parent
280 * device with governors except for the passive governor. But, don't need to
281 * initialize the 'this' and 'nb' field because the devfreq core will handle
282 * them.
283 */
284struct devfreq_passive_data {
285 /* Should set the devfreq instance of parent device */
286 struct devfreq *parent;
287
288 /* Optional callback to decide the next frequency of passvice device */
289 int (*get_target_freq)(struct devfreq *this, unsigned long *freq);
290
291 /* For passive governor's internal use. Don't need to set them */
292 struct devfreq *this;
293 struct notifier_block nb;
294};
295#endif
296
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200297#else /* !CONFIG_PM_DEVFREQ */
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000298static inline struct devfreq *devfreq_add_device(struct device *dev,
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200299 struct devfreq_dev_profile *profile,
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500300 const char *governor_name,
MyungJoo Hama95e1f52012-01-11 17:44:28 +0900301 void *data)
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200302{
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900303 return ERR_PTR(-ENOSYS);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200304}
305
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000306static inline int devfreq_remove_device(struct devfreq *devfreq)
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200307{
308 return 0;
309}
310
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900311static inline struct devfreq *devm_devfreq_add_device(struct device *dev,
312 struct devfreq_dev_profile *profile,
313 const char *governor_name,
314 void *data)
315{
316 return ERR_PTR(-ENOSYS);
317}
318
319static inline void devm_devfreq_remove_device(struct device *dev,
320 struct devfreq *devfreq)
321{
322}
323
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000324static inline int devfreq_suspend_device(struct devfreq *devfreq)
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200325{
326 return 0;
327}
328
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000329static inline int devfreq_resume_device(struct devfreq *devfreq)
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200330{
331 return 0;
332}
333
Lukasz Luba59031952018-12-05 12:05:54 +0100334static inline void devfreq_suspend(void) {}
335static inline void devfreq_resume(void) {}
336
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500337static inline struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100338 unsigned long *freq, u32 flags)
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200339{
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000340 return ERR_PTR(-EINVAL);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200341}
342
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000343static inline int devfreq_register_opp_notifier(struct device *dev,
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200344 struct devfreq *devfreq)
345{
346 return -EINVAL;
347}
348
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000349static inline int devfreq_unregister_opp_notifier(struct device *dev,
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200350 struct devfreq *devfreq)
351{
352 return -EINVAL;
353}
354
Chanwoo Choid5b040d2014-05-09 16:43:09 +0900355static inline int devm_devfreq_register_opp_notifier(struct device *dev,
356 struct devfreq *devfreq)
357{
358 return -EINVAL;
359}
360
361static inline void devm_devfreq_unregister_opp_notifier(struct device *dev,
362 struct devfreq *devfreq)
363{
364}
Javi Merino08e75e72015-08-14 18:56:56 +0100365
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900366static inline int devfreq_register_notifier(struct devfreq *devfreq,
367 struct notifier_block *nb,
368 unsigned int list)
369{
370 return 0;
371}
372
373static inline int devfreq_unregister_notifier(struct devfreq *devfreq,
374 struct notifier_block *nb,
375 unsigned int list)
376{
377 return 0;
378}
379
380static inline int devm_devfreq_register_notifier(struct device *dev,
381 struct devfreq *devfreq,
382 struct notifier_block *nb,
383 unsigned int list)
384{
385 return 0;
386}
387
388static inline void devm_devfreq_unregister_notifier(struct device *dev,
389 struct devfreq *devfreq,
390 struct notifier_block *nb,
391 unsigned int list)
392{
393}
394
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900395static inline struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
396 int index)
397{
398 return ERR_PTR(-ENODEV);
399}
400
Javi Merino08e75e72015-08-14 18:56:56 +0100401static inline int devfreq_update_stats(struct devfreq *df)
402{
403 return -EINVAL;
404}
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200405#endif /* CONFIG_PM_DEVFREQ */
406
407#endif /* __LINUX_DEVFREQ_H__ */