blob: 19520625ea94e694e3e62b1dfb327bd0bfeb0616 [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 Choi0fe3a662016-01-26 13:21:26 +090022/* 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
MyungJoo Hama3c98b82011-10-02 00:19:15 +020029struct devfreq;
Chanwoo Choi3ea6b702017-04-06 13:19:35 +090030struct devfreq_governor;
MyungJoo Hama3c98b82011-10-02 00:19:15 +020031
32/**
33 * struct devfreq_dev_status - Data given from devfreq user device to
34 * governors. Represents the performance
35 * statistics.
Nishanth Menone09651f2012-10-29 08:02:23 -050036 * @total_time: The total time represented by this instance of
MyungJoo Hama3c98b82011-10-02 00:19:15 +020037 * devfreq_dev_status
Nishanth Menone09651f2012-10-29 08:02:23 -050038 * @busy_time: The time that the device was working among the
MyungJoo Hama3c98b82011-10-02 00:19:15 +020039 * total_time.
Nishanth Menone09651f2012-10-29 08:02:23 -050040 * @current_frequency: The operating frequency.
41 * @private_data: An entry not specified by the devfreq framework.
MyungJoo Hama3c98b82011-10-02 00:19:15 +020042 * A device and a specific governor may have their
43 * own protocol with private_data. However, because
44 * this is governor-specific, a governor using this
45 * will be only compatible with devices aware of it.
46 */
47struct devfreq_dev_status {
48 /* both since the last measure */
49 unsigned long total_time;
50 unsigned long busy_time;
51 unsigned long current_frequency;
Jonathan Corbet1a51cfd2011-11-07 23:54:53 +010052 void *private_data;
MyungJoo Hama3c98b82011-10-02 00:19:15 +020053};
54
MyungJoo Hamab5f2992012-03-16 21:54:53 +010055/*
56 * The resulting frequency should be at most this. (this bound is the
57 * least upper bound; thus, the resulting freq should be lower or same)
58 * If the flag is not set, the resulting frequency should be at most the
59 * bound (greatest lower bound)
60 */
61#define DEVFREQ_FLAG_LEAST_UPPER_BOUND 0x1
62
MyungJoo Hama3c98b82011-10-02 00:19:15 +020063/**
64 * struct devfreq_dev_profile - Devfreq's user device profile
Nishanth Menone09651f2012-10-29 08:02:23 -050065 * @initial_freq: The operating frequency when devfreq_add_device() is
MyungJoo Hama3c98b82011-10-02 00:19:15 +020066 * called.
Nishanth Menone09651f2012-10-29 08:02:23 -050067 * @polling_ms: The polling interval in ms. 0 disables polling.
68 * @target: The device should set its operating frequency at
MyungJoo Hama3c98b82011-10-02 00:19:15 +020069 * freq or lowest-upper-than-freq value. If freq is
70 * higher than any operable frequency, set maximum.
71 * Before returning, target function should set
72 * freq at the current frequency.
MyungJoo Hamab5f2992012-03-16 21:54:53 +010073 * The "flags" parameter's possible values are
74 * explained above with "DEVFREQ_FLAG_*" macros.
Nishanth Menone09651f2012-10-29 08:02:23 -050075 * @get_dev_status: The device should provide the current performance
MyungJoo Hamd54cdf32015-08-18 13:45:49 +090076 * status to devfreq. Governors are recommended not to
77 * use this directly. Instead, governors are recommended
78 * to use devfreq_update_stats() along with
79 * devfreq.last_status.
Nishanth Menone09651f2012-10-29 08:02:23 -050080 * @get_cur_freq: The device should provide the current frequency
Rajagopal Venkat7f98a902012-10-26 01:50:26 +020081 * at which it is operating.
Nishanth Menone09651f2012-10-29 08:02:23 -050082 * @exit: An optional callback that is called when devfreq
MyungJoo Hama3c98b82011-10-02 00:19:15 +020083 * is removing the devfreq object due to error or
84 * from devfreq_remove_device() call. If the user
85 * has registered devfreq->nb at a notifier-head,
86 * this is the time to unregister it.
Chanwoo Choi416b46a2017-10-23 10:32:10 +090087 * @freq_table: Optional list of frequencies to support statistics
88 * and freq_table must be generated in ascending order.
89 * @max_state: The size of freq_table.
MyungJoo Hama3c98b82011-10-02 00:19:15 +020090 */
91struct devfreq_dev_profile {
92 unsigned long initial_freq;
93 unsigned int polling_ms;
94
MyungJoo Hamab5f2992012-03-16 21:54:53 +010095 int (*target)(struct device *dev, unsigned long *freq, u32 flags);
MyungJoo Hama3c98b82011-10-02 00:19:15 +020096 int (*get_dev_status)(struct device *dev,
97 struct devfreq_dev_status *stat);
Rajagopal Venkat7f98a902012-10-26 01:50:26 +020098 int (*get_cur_freq)(struct device *dev, unsigned long *freq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +020099 void (*exit)(struct device *dev);
Jonghwa Leee552bba2012-08-23 20:00:46 +0900100
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900101 unsigned long *freq_table;
Jonghwa Leee552bba2012-08-23 20:00:46 +0900102 unsigned int max_state;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200103};
104
105/**
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200106 * struct devfreq - Device devfreq structure
Nishanth Menone09651f2012-10-29 08:02:23 -0500107 * @node: list node - contains the devices with devfreq that have been
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200108 * registered.
Nishanth Menone09651f2012-10-29 08:02:23 -0500109 * @lock: a mutex to protect accessing devfreq.
110 * @dev: device registered by devfreq class. dev.parent is the device
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200111 * using devfreq.
Nishanth Menone09651f2012-10-29 08:02:23 -0500112 * @profile: device-specific devfreq profile
113 * @governor: method how to choose frequency based on the usage.
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500114 * @governor_name: devfreq governor name for use with this devfreq
Nishanth Menone09651f2012-10-29 08:02:23 -0500115 * @nb: notifier block used to notify devfreq object that it should
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200116 * reevaluate operable frequencies. Devfreq users may use
117 * devfreq.nb to the corresponding register notifier call chain.
Nishanth Menone09651f2012-10-29 08:02:23 -0500118 * @work: delayed work for load monitoring.
119 * @previous_freq: previously configured frequency value.
120 * @data: Private data of the governor. The devfreq framework does not
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200121 * touch this.
Nishanth Menone09651f2012-10-29 08:02:23 -0500122 * @min_freq: Limit minimum frequency requested by user (0: none)
123 * @max_freq: Limit maximum frequency requested by user (0: none)
Chanwoo Choif1d981e2017-10-23 10:32:08 +0900124 * @scaling_min_freq: Limit minimum frequency requested by OPP interface
125 * @scaling_max_freq: Limit maximum frequency requested by OPP interface
Nishanth Menone09651f2012-10-29 08:02:23 -0500126 * @stop_polling: devfreq polling status of a device.
Jonghwa Leee552bba2012-08-23 20:00:46 +0900127 * @total_trans: Number of devfreq transitions
128 * @trans_table: Statistics of devfreq transitions
129 * @time_in_state: Statistics of devfreq states
130 * @last_stat_updated: The last time stat updated
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900131 * @transition_notifier_list: list head of DEVFREQ_TRANSITION_NOTIFIER notifier
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200132 *
133 * This structure stores the devfreq information for a give device.
134 *
135 * Note that when a governor accesses entries in struct devfreq in its
136 * functions except for the context of callbacks defined in struct
137 * devfreq_governor, the governor should protect its access with the
138 * struct mutex lock in struct devfreq. A governor may use this mutex
139 * to protect its own private data in void *data as well.
140 */
141struct devfreq {
142 struct list_head node;
143
144 struct mutex lock;
145 struct device dev;
146 struct devfreq_dev_profile *profile;
147 const struct devfreq_governor *governor;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500148 char governor_name[DEVFREQ_NAME_LEN];
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200149 struct notifier_block nb;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200150 struct delayed_work work;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200151
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200152 unsigned long previous_freq;
Javi Merino08e75e72015-08-14 18:56:56 +0100153 struct devfreq_dev_status last_status;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200154
155 void *data; /* private data for governors */
156
MyungJoo Ham6530b9de2011-12-09 16:42:19 +0900157 unsigned long min_freq;
158 unsigned long max_freq;
Chanwoo Choif1d981e2017-10-23 10:32:08 +0900159 unsigned long scaling_min_freq;
160 unsigned long scaling_max_freq;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200161 bool stop_polling;
Jonghwa Leee552bba2012-08-23 20:00:46 +0900162
LABBE Corentin05851232013-09-26 16:50:21 +0200163 /* information for device frequency transition */
Jonghwa Leee552bba2012-08-23 20:00:46 +0900164 unsigned int total_trans;
165 unsigned int *trans_table;
166 unsigned long *time_in_state;
167 unsigned long last_stat_updated;
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900168
169 struct srcu_notifier_head transition_notifier_list;
170};
171
172struct devfreq_freqs {
173 unsigned long old;
174 unsigned long new;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200175};
176
177#if defined(CONFIG_PM_DEVFREQ)
178extern struct devfreq *devfreq_add_device(struct device *dev,
179 struct devfreq_dev_profile *profile,
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500180 const char *governor_name,
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200181 void *data);
182extern int devfreq_remove_device(struct devfreq *devfreq);
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900183extern struct devfreq *devm_devfreq_add_device(struct device *dev,
184 struct devfreq_dev_profile *profile,
185 const char *governor_name,
186 void *data);
187extern void devm_devfreq_remove_device(struct device *dev,
188 struct devfreq *devfreq);
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900189
Rafael J. Wysocki464ed182014-12-19 15:37:54 +0100190/* Supposed to be called by PM callbacks */
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200191extern int devfreq_suspend_device(struct devfreq *devfreq);
192extern int devfreq_resume_device(struct devfreq *devfreq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200193
194/* Helper functions for devfreq user device driver with OPP. */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500195extern struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100196 unsigned long *freq, u32 flags);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200197extern int devfreq_register_opp_notifier(struct device *dev,
198 struct devfreq *devfreq);
199extern int devfreq_unregister_opp_notifier(struct device *dev,
200 struct devfreq *devfreq);
Chanwoo Choid5b040d2014-05-09 16:43:09 +0900201extern int devm_devfreq_register_opp_notifier(struct device *dev,
202 struct devfreq *devfreq);
203extern void devm_devfreq_unregister_opp_notifier(struct device *dev,
204 struct devfreq *devfreq);
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900205extern int devfreq_register_notifier(struct devfreq *devfreq,
206 struct notifier_block *nb,
207 unsigned int list);
208extern int devfreq_unregister_notifier(struct devfreq *devfreq,
209 struct notifier_block *nb,
210 unsigned int list);
211extern int devm_devfreq_register_notifier(struct device *dev,
212 struct devfreq *devfreq,
213 struct notifier_block *nb,
214 unsigned int list);
215extern void devm_devfreq_unregister_notifier(struct device *dev,
216 struct devfreq *devfreq,
217 struct notifier_block *nb,
218 unsigned int list);
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900219extern struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
220 int index);
221
MyungJoo Ham883d5882012-11-21 17:17:11 +0900222#if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND)
MyungJoo Hamce26c5b2011-10-02 00:19:34 +0200223/**
224 * struct devfreq_simple_ondemand_data - void *data fed to struct devfreq
225 * and devfreq_add_device
Nishanth Menone09651f2012-10-29 08:02:23 -0500226 * @upthreshold: If the load is over this value, the frequency jumps.
MyungJoo Hamce26c5b2011-10-02 00:19:34 +0200227 * Specify 0 to use the default. Valid value = 0 to 100.
Nishanth Menone09651f2012-10-29 08:02:23 -0500228 * @downdifferential: If the load is under upthreshold - downdifferential,
MyungJoo Hamce26c5b2011-10-02 00:19:34 +0200229 * the governor may consider slowing the frequency down.
230 * Specify 0 to use the default. Valid value = 0 to 100.
231 * downdifferential < upthreshold must hold.
232 *
233 * If the fed devfreq_simple_ondemand_data pointer is NULL to the governor,
234 * the governor uses the default values.
235 */
236struct devfreq_simple_ondemand_data {
237 unsigned int upthreshold;
238 unsigned int downdifferential;
239};
240#endif
241
Chanwoo Choi99613312016-03-22 13:44:03 +0900242#if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE)
243/**
244 * struct devfreq_passive_data - void *data fed to struct devfreq
245 * and devfreq_add_device
246 * @parent: the devfreq instance of parent device.
247 * @get_target_freq: Optional callback, Returns desired operating frequency
248 * for the device using passive governor. That is called
249 * when passive governor should decide the next frequency
250 * by using the new frequency of parent devfreq device
251 * using governors except for passive governor.
252 * If the devfreq device has the specific method to decide
253 * the next frequency, should use this callback.
254 * @this: the devfreq instance of own device.
255 * @nb: the notifier block for DEVFREQ_TRANSITION_NOTIFIER list
256 *
257 * The devfreq_passive_data have to set the devfreq instance of parent
258 * device with governors except for the passive governor. But, don't need to
259 * initialize the 'this' and 'nb' field because the devfreq core will handle
260 * them.
261 */
262struct devfreq_passive_data {
263 /* Should set the devfreq instance of parent device */
264 struct devfreq *parent;
265
266 /* Optional callback to decide the next frequency of passvice device */
267 int (*get_target_freq)(struct devfreq *this, unsigned long *freq);
268
269 /* For passive governor's internal use. Don't need to set them */
270 struct devfreq *this;
271 struct notifier_block nb;
272};
273#endif
274
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200275#else /* !CONFIG_PM_DEVFREQ */
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000276static inline struct devfreq *devfreq_add_device(struct device *dev,
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200277 struct devfreq_dev_profile *profile,
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500278 const char *governor_name,
MyungJoo Hama95e1f52012-01-11 17:44:28 +0900279 void *data)
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200280{
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900281 return ERR_PTR(-ENOSYS);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200282}
283
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000284static inline int devfreq_remove_device(struct devfreq *devfreq)
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200285{
286 return 0;
287}
288
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900289static inline struct devfreq *devm_devfreq_add_device(struct device *dev,
290 struct devfreq_dev_profile *profile,
291 const char *governor_name,
292 void *data)
293{
294 return ERR_PTR(-ENOSYS);
295}
296
297static inline void devm_devfreq_remove_device(struct device *dev,
298 struct devfreq *devfreq)
299{
300}
301
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000302static inline int devfreq_suspend_device(struct devfreq *devfreq)
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200303{
304 return 0;
305}
306
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000307static inline int devfreq_resume_device(struct devfreq *devfreq)
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200308{
309 return 0;
310}
311
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500312static inline struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100313 unsigned long *freq, u32 flags)
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200314{
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000315 return ERR_PTR(-EINVAL);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200316}
317
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000318static inline int devfreq_register_opp_notifier(struct device *dev,
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200319 struct devfreq *devfreq)
320{
321 return -EINVAL;
322}
323
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000324static inline int devfreq_unregister_opp_notifier(struct device *dev,
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200325 struct devfreq *devfreq)
326{
327 return -EINVAL;
328}
329
Chanwoo Choid5b040d2014-05-09 16:43:09 +0900330static inline int devm_devfreq_register_opp_notifier(struct device *dev,
331 struct devfreq *devfreq)
332{
333 return -EINVAL;
334}
335
336static inline void devm_devfreq_unregister_opp_notifier(struct device *dev,
337 struct devfreq *devfreq)
338{
339}
Javi Merino08e75e72015-08-14 18:56:56 +0100340
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900341static inline int devfreq_register_notifier(struct devfreq *devfreq,
342 struct notifier_block *nb,
343 unsigned int list)
344{
345 return 0;
346}
347
348static inline int devfreq_unregister_notifier(struct devfreq *devfreq,
349 struct notifier_block *nb,
350 unsigned int list)
351{
352 return 0;
353}
354
355static inline int devm_devfreq_register_notifier(struct device *dev,
356 struct devfreq *devfreq,
357 struct notifier_block *nb,
358 unsigned int list)
359{
360 return 0;
361}
362
363static inline void devm_devfreq_unregister_notifier(struct device *dev,
364 struct devfreq *devfreq,
365 struct notifier_block *nb,
366 unsigned int list)
367{
368}
369
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900370static inline struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
371 int index)
372{
373 return ERR_PTR(-ENODEV);
374}
375
Javi Merino08e75e72015-08-14 18:56:56 +0100376static inline int devfreq_update_stats(struct devfreq *df)
377{
378 return -EINVAL;
379}
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200380#endif /* CONFIG_PM_DEVFREQ */
381
382#endif /* __LINUX_DEVFREQ_H__ */