blob: fbffa74bfc1bb94c5099a21f438da2c95f251a5e [file] [log] [blame]
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001/*
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>
Nishanth Menone4db1c72013-09-19 16:03:52 -050018#include <linux/pm_opp.h>
MyungJoo Hama3c98b82011-10-02 00:19:15 +020019
20#define DEVFREQ_NAME_LEN 16
21
Chanwoo Choiaa7c3522017-10-23 10:32:12 +090022/* DEVFREQ governor name */
23#define DEVFREQ_GOV_SIMPLE_ONDEMAND "simple_ondemand"
24#define DEVFREQ_GOV_PERFORMANCE "performance"
25#define DEVFREQ_GOV_POWERSAVE "powersave"
26#define DEVFREQ_GOV_USERSPACE "userspace"
27#define DEVFREQ_GOV_PASSIVE "passive"
28
Chanwoo Choi0fe3a662016-01-26 13:21:26 +090029/* DEVFREQ notifier interface */
30#define DEVFREQ_TRANSITION_NOTIFIER (0)
31
32/* Transition notifiers of DEVFREQ_TRANSITION_NOTIFIER */
33#define DEVFREQ_PRECHANGE (0)
34#define DEVFREQ_POSTCHANGE (1)
35
MyungJoo Hama3c98b82011-10-02 00:19:15 +020036struct devfreq;
Chanwoo Choi3ea6b702017-04-06 13:19:35 +090037struct devfreq_governor;
MyungJoo Hama3c98b82011-10-02 00:19:15 +020038
39/**
40 * struct devfreq_dev_status - Data given from devfreq user device to
41 * governors. Represents the performance
42 * statistics.
Nishanth Menone09651f2012-10-29 08:02:23 -050043 * @total_time: The total time represented by this instance of
MyungJoo Hama3c98b82011-10-02 00:19:15 +020044 * devfreq_dev_status
Nishanth Menone09651f2012-10-29 08:02:23 -050045 * @busy_time: The time that the device was working among the
MyungJoo Hama3c98b82011-10-02 00:19:15 +020046 * total_time.
Nishanth Menone09651f2012-10-29 08:02:23 -050047 * @current_frequency: The operating frequency.
48 * @private_data: An entry not specified by the devfreq framework.
MyungJoo Hama3c98b82011-10-02 00:19:15 +020049 * A device and a specific governor may have their
50 * own protocol with private_data. However, because
51 * this is governor-specific, a governor using this
52 * will be only compatible with devices aware of it.
53 */
54struct devfreq_dev_status {
55 /* both since the last measure */
56 unsigned long total_time;
57 unsigned long busy_time;
58 unsigned long current_frequency;
Jonathan Corbet1a51cfd2011-11-07 23:54:53 +010059 void *private_data;
MyungJoo Hama3c98b82011-10-02 00:19:15 +020060};
61
MyungJoo Hamab5f2992012-03-16 21:54:53 +010062/*
63 * The resulting frequency should be at most this. (this bound is the
64 * least upper bound; thus, the resulting freq should be lower or same)
65 * If the flag is not set, the resulting frequency should be at most the
66 * bound (greatest lower bound)
67 */
68#define DEVFREQ_FLAG_LEAST_UPPER_BOUND 0x1
69
MyungJoo Hama3c98b82011-10-02 00:19:15 +020070/**
71 * struct devfreq_dev_profile - Devfreq's user device profile
Nishanth Menone09651f2012-10-29 08:02:23 -050072 * @initial_freq: The operating frequency when devfreq_add_device() is
MyungJoo Hama3c98b82011-10-02 00:19:15 +020073 * called.
Nishanth Menone09651f2012-10-29 08:02:23 -050074 * @polling_ms: The polling interval in ms. 0 disables polling.
75 * @target: The device should set its operating frequency at
MyungJoo Hama3c98b82011-10-02 00:19:15 +020076 * freq or lowest-upper-than-freq value. If freq is
77 * higher than any operable frequency, set maximum.
78 * Before returning, target function should set
79 * freq at the current frequency.
MyungJoo Hamab5f2992012-03-16 21:54:53 +010080 * The "flags" parameter's possible values are
81 * explained above with "DEVFREQ_FLAG_*" macros.
Nishanth Menone09651f2012-10-29 08:02:23 -050082 * @get_dev_status: The device should provide the current performance
MyungJoo Hamd54cdf32015-08-18 13:45:49 +090083 * status to devfreq. Governors are recommended not to
84 * use this directly. Instead, governors are recommended
85 * to use devfreq_update_stats() along with
86 * devfreq.last_status.
Nishanth Menone09651f2012-10-29 08:02:23 -050087 * @get_cur_freq: The device should provide the current frequency
Rajagopal Venkat7f98a902012-10-26 01:50:26 +020088 * at which it is operating.
Nishanth Menone09651f2012-10-29 08:02:23 -050089 * @exit: An optional callback that is called when devfreq
MyungJoo Hama3c98b82011-10-02 00:19:15 +020090 * is removing the devfreq object due to error or
91 * from devfreq_remove_device() call. If the user
92 * has registered devfreq->nb at a notifier-head,
93 * this is the time to unregister it.
Chanwoo Choi416b46a2017-10-23 10:32:10 +090094 * @freq_table: Optional list of frequencies to support statistics
95 * and freq_table must be generated in ascending order.
96 * @max_state: The size of freq_table.
MyungJoo Hama3c98b82011-10-02 00:19:15 +020097 */
98struct devfreq_dev_profile {
99 unsigned long initial_freq;
100 unsigned int polling_ms;
101
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100102 int (*target)(struct device *dev, unsigned long *freq, u32 flags);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200103 int (*get_dev_status)(struct device *dev,
104 struct devfreq_dev_status *stat);
Rajagopal Venkat7f98a902012-10-26 01:50:26 +0200105 int (*get_cur_freq)(struct device *dev, unsigned long *freq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200106 void (*exit)(struct device *dev);
Jonghwa Leee552bba2012-08-23 20:00:46 +0900107
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900108 unsigned long *freq_table;
Jonghwa Leee552bba2012-08-23 20:00:46 +0900109 unsigned int max_state;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200110};
111
112/**
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200113 * struct devfreq - Device devfreq structure
Nishanth Menone09651f2012-10-29 08:02:23 -0500114 * @node: list node - contains the devices with devfreq that have been
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200115 * registered.
Nishanth Menone09651f2012-10-29 08:02:23 -0500116 * @lock: a mutex to protect accessing devfreq.
117 * @dev: device registered by devfreq class. dev.parent is the device
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200118 * using devfreq.
Nishanth Menone09651f2012-10-29 08:02:23 -0500119 * @profile: device-specific devfreq profile
120 * @governor: method how to choose frequency based on the usage.
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500121 * @governor_name: devfreq governor name for use with this devfreq
Nishanth Menone09651f2012-10-29 08:02:23 -0500122 * @nb: notifier block used to notify devfreq object that it should
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200123 * reevaluate operable frequencies. Devfreq users may use
124 * devfreq.nb to the corresponding register notifier call chain.
Nishanth Menone09651f2012-10-29 08:02:23 -0500125 * @work: delayed work for load monitoring.
126 * @previous_freq: previously configured frequency value.
127 * @data: Private data of the governor. The devfreq framework does not
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200128 * touch this.
Nishanth Menone09651f2012-10-29 08:02:23 -0500129 * @min_freq: Limit minimum frequency requested by user (0: none)
130 * @max_freq: Limit maximum frequency requested by user (0: none)
Chanwoo Choif1d981e2017-10-23 10:32:08 +0900131 * @scaling_min_freq: Limit minimum frequency requested by OPP interface
132 * @scaling_max_freq: Limit maximum frequency requested by OPP interface
Nishanth Menone09651f2012-10-29 08:02:23 -0500133 * @stop_polling: devfreq polling status of a device.
Lukasz Luba83f8ca42018-12-05 12:05:53 +0100134 * @suspend_freq: frequency of a device set during suspend phase.
135 * @resume_freq: frequency of a device set in resume phase.
136 * @suspend_count: suspend requests counter for a device.
Jonghwa Leee552bba2012-08-23 20:00:46 +0900137 * @total_trans: Number of devfreq transitions
138 * @trans_table: Statistics of devfreq transitions
139 * @time_in_state: Statistics of devfreq states
140 * @last_stat_updated: The last time stat updated
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900141 * @transition_notifier_list: list head of DEVFREQ_TRANSITION_NOTIFIER notifier
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200142 *
143 * This structure stores the devfreq information for a give device.
144 *
145 * Note that when a governor accesses entries in struct devfreq in its
146 * functions except for the context of callbacks defined in struct
147 * devfreq_governor, the governor should protect its access with the
148 * struct mutex lock in struct devfreq. A governor may use this mutex
149 * to protect its own private data in void *data as well.
150 */
151struct devfreq {
152 struct list_head node;
153
154 struct mutex lock;
155 struct device dev;
156 struct devfreq_dev_profile *profile;
157 const struct devfreq_governor *governor;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500158 char governor_name[DEVFREQ_NAME_LEN];
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200159 struct notifier_block nb;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200160 struct delayed_work work;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200161
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200162 unsigned long previous_freq;
Javi Merino08e75e72015-08-14 18:56:56 +0100163 struct devfreq_dev_status last_status;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200164
165 void *data; /* private data for governors */
166
MyungJoo Ham6530b9de2011-12-09 16:42:19 +0900167 unsigned long min_freq;
168 unsigned long max_freq;
Chanwoo Choif1d981e2017-10-23 10:32:08 +0900169 unsigned long scaling_min_freq;
170 unsigned long scaling_max_freq;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200171 bool stop_polling;
Jonghwa Leee552bba2012-08-23 20:00:46 +0900172
Lukasz Luba83f8ca42018-12-05 12:05:53 +0100173 unsigned long suspend_freq;
174 unsigned long resume_freq;
175 atomic_t suspend_count;
176
LABBE Corentin05851232013-09-26 16:50:21 +0200177 /* information for device frequency transition */
Jonghwa Leee552bba2012-08-23 20:00:46 +0900178 unsigned int total_trans;
179 unsigned int *trans_table;
180 unsigned long *time_in_state;
181 unsigned long last_stat_updated;
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900182
183 struct srcu_notifier_head transition_notifier_list;
184};
185
186struct devfreq_freqs {
187 unsigned long old;
188 unsigned long new;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200189};
190
191#if defined(CONFIG_PM_DEVFREQ)
192extern struct devfreq *devfreq_add_device(struct device *dev,
193 struct devfreq_dev_profile *profile,
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500194 const char *governor_name,
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200195 void *data);
196extern int devfreq_remove_device(struct devfreq *devfreq);
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900197extern struct devfreq *devm_devfreq_add_device(struct device *dev,
198 struct devfreq_dev_profile *profile,
199 const char *governor_name,
200 void *data);
201extern void devm_devfreq_remove_device(struct device *dev,
202 struct devfreq *devfreq);
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900203
Rafael J. Wysocki464ed182014-12-19 15:37:54 +0100204/* Supposed to be called by PM callbacks */
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200205extern int devfreq_suspend_device(struct devfreq *devfreq);
206extern int devfreq_resume_device(struct devfreq *devfreq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200207
Lukasz Luba59031952018-12-05 12:05:54 +0100208extern void devfreq_suspend(void);
209extern void devfreq_resume(void);
210
Matthias Kaehlckeb596d892018-08-03 13:05:11 -0700211/**
212 * update_devfreq() - Reevaluate the device and configure frequency
213 * @devfreq: the devfreq device
214 *
215 * Note: devfreq->lock must be held
216 */
217extern int update_devfreq(struct devfreq *devfreq);
218
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200219/* Helper functions for devfreq user device driver with OPP. */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500220extern struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100221 unsigned long *freq, u32 flags);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200222extern int devfreq_register_opp_notifier(struct device *dev,
223 struct devfreq *devfreq);
224extern int devfreq_unregister_opp_notifier(struct device *dev,
225 struct devfreq *devfreq);
Chanwoo Choid5b040d2014-05-09 16:43:09 +0900226extern int devm_devfreq_register_opp_notifier(struct device *dev,
227 struct devfreq *devfreq);
228extern void devm_devfreq_unregister_opp_notifier(struct device *dev,
229 struct devfreq *devfreq);
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900230extern int devfreq_register_notifier(struct devfreq *devfreq,
231 struct notifier_block *nb,
232 unsigned int list);
233extern int devfreq_unregister_notifier(struct devfreq *devfreq,
234 struct notifier_block *nb,
235 unsigned int list);
236extern int devm_devfreq_register_notifier(struct device *dev,
237 struct devfreq *devfreq,
238 struct notifier_block *nb,
239 unsigned int list);
240extern void devm_devfreq_unregister_notifier(struct device *dev,
241 struct devfreq *devfreq,
242 struct notifier_block *nb,
243 unsigned int list);
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900244extern struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
245 int index);
246
MyungJoo Ham883d5882012-11-21 17:17:11 +0900247#if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND)
MyungJoo Hamce26c5b2011-10-02 00:19:34 +0200248/**
249 * struct devfreq_simple_ondemand_data - void *data fed to struct devfreq
250 * and devfreq_add_device
Nishanth Menone09651f2012-10-29 08:02:23 -0500251 * @upthreshold: If the load is over this value, the frequency jumps.
MyungJoo Hamce26c5b2011-10-02 00:19:34 +0200252 * Specify 0 to use the default. Valid value = 0 to 100.
Nishanth Menone09651f2012-10-29 08:02:23 -0500253 * @downdifferential: If the load is under upthreshold - downdifferential,
MyungJoo Hamce26c5b2011-10-02 00:19:34 +0200254 * the governor may consider slowing the frequency down.
255 * Specify 0 to use the default. Valid value = 0 to 100.
256 * downdifferential < upthreshold must hold.
257 *
258 * If the fed devfreq_simple_ondemand_data pointer is NULL to the governor,
259 * the governor uses the default values.
260 */
261struct devfreq_simple_ondemand_data {
262 unsigned int upthreshold;
263 unsigned int downdifferential;
264};
265#endif
266
Chanwoo Choi99613312016-03-22 13:44:03 +0900267#if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE)
268/**
269 * struct devfreq_passive_data - void *data fed to struct devfreq
270 * and devfreq_add_device
271 * @parent: the devfreq instance of parent device.
272 * @get_target_freq: Optional callback, Returns desired operating frequency
273 * for the device using passive governor. That is called
274 * when passive governor should decide the next frequency
275 * by using the new frequency of parent devfreq device
276 * using governors except for passive governor.
277 * If the devfreq device has the specific method to decide
278 * the next frequency, should use this callback.
279 * @this: the devfreq instance of own device.
280 * @nb: the notifier block for DEVFREQ_TRANSITION_NOTIFIER list
281 *
282 * The devfreq_passive_data have to set the devfreq instance of parent
283 * device with governors except for the passive governor. But, don't need to
284 * initialize the 'this' and 'nb' field because the devfreq core will handle
285 * them.
286 */
287struct devfreq_passive_data {
288 /* Should set the devfreq instance of parent device */
289 struct devfreq *parent;
290
291 /* Optional callback to decide the next frequency of passvice device */
292 int (*get_target_freq)(struct devfreq *this, unsigned long *freq);
293
294 /* For passive governor's internal use. Don't need to set them */
295 struct devfreq *this;
296 struct notifier_block nb;
297};
298#endif
299
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200300#else /* !CONFIG_PM_DEVFREQ */
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000301static inline struct devfreq *devfreq_add_device(struct device *dev,
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200302 struct devfreq_dev_profile *profile,
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500303 const char *governor_name,
MyungJoo Hama95e1f52012-01-11 17:44:28 +0900304 void *data)
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200305{
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900306 return ERR_PTR(-ENOSYS);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200307}
308
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000309static inline int devfreq_remove_device(struct devfreq *devfreq)
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200310{
311 return 0;
312}
313
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900314static inline struct devfreq *devm_devfreq_add_device(struct device *dev,
315 struct devfreq_dev_profile *profile,
316 const char *governor_name,
317 void *data)
318{
319 return ERR_PTR(-ENOSYS);
320}
321
322static inline void devm_devfreq_remove_device(struct device *dev,
323 struct devfreq *devfreq)
324{
325}
326
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000327static inline int devfreq_suspend_device(struct devfreq *devfreq)
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200328{
329 return 0;
330}
331
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000332static inline int devfreq_resume_device(struct devfreq *devfreq)
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200333{
334 return 0;
335}
336
Lukasz Luba59031952018-12-05 12:05:54 +0100337static inline void devfreq_suspend(void) {}
338static inline void devfreq_resume(void) {}
339
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500340static inline struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100341 unsigned long *freq, u32 flags)
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200342{
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000343 return ERR_PTR(-EINVAL);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200344}
345
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000346static inline int devfreq_register_opp_notifier(struct device *dev,
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200347 struct devfreq *devfreq)
348{
349 return -EINVAL;
350}
351
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000352static inline int devfreq_unregister_opp_notifier(struct device *dev,
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200353 struct devfreq *devfreq)
354{
355 return -EINVAL;
356}
357
Chanwoo Choid5b040d2014-05-09 16:43:09 +0900358static inline int devm_devfreq_register_opp_notifier(struct device *dev,
359 struct devfreq *devfreq)
360{
361 return -EINVAL;
362}
363
364static inline void devm_devfreq_unregister_opp_notifier(struct device *dev,
365 struct devfreq *devfreq)
366{
367}
Javi Merino08e75e72015-08-14 18:56:56 +0100368
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900369static inline int devfreq_register_notifier(struct devfreq *devfreq,
370 struct notifier_block *nb,
371 unsigned int list)
372{
373 return 0;
374}
375
376static inline int devfreq_unregister_notifier(struct devfreq *devfreq,
377 struct notifier_block *nb,
378 unsigned int list)
379{
380 return 0;
381}
382
383static inline int devm_devfreq_register_notifier(struct device *dev,
384 struct devfreq *devfreq,
385 struct notifier_block *nb,
386 unsigned int list)
387{
388 return 0;
389}
390
391static inline void devm_devfreq_unregister_notifier(struct device *dev,
392 struct devfreq *devfreq,
393 struct notifier_block *nb,
394 unsigned int list)
395{
396}
397
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900398static inline struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
399 int index)
400{
401 return ERR_PTR(-ENODEV);
402}
403
Javi Merino08e75e72015-08-14 18:56:56 +0100404static inline int devfreq_update_stats(struct devfreq *df)
405{
406 return -EINVAL;
407}
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200408#endif /* CONFIG_PM_DEVFREQ */
409
410#endif /* __LINUX_DEVFREQ_H__ */