blob: 8e849a672dcd81ddd935bdc407e685a83215765d [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;
30
31/**
32 * struct devfreq_dev_status - Data given from devfreq user device to
33 * governors. Represents the performance
34 * statistics.
Nishanth Menone09651f2012-10-29 08:02:23 -050035 * @total_time: The total time represented by this instance of
MyungJoo Hama3c98b82011-10-02 00:19:15 +020036 * devfreq_dev_status
Nishanth Menone09651f2012-10-29 08:02:23 -050037 * @busy_time: The time that the device was working among the
MyungJoo Hama3c98b82011-10-02 00:19:15 +020038 * total_time.
Nishanth Menone09651f2012-10-29 08:02:23 -050039 * @current_frequency: The operating frequency.
40 * @private_data: An entry not specified by the devfreq framework.
MyungJoo Hama3c98b82011-10-02 00:19:15 +020041 * A device and a specific governor may have their
42 * own protocol with private_data. However, because
43 * this is governor-specific, a governor using this
44 * will be only compatible with devices aware of it.
45 */
46struct devfreq_dev_status {
47 /* both since the last measure */
48 unsigned long total_time;
49 unsigned long busy_time;
50 unsigned long current_frequency;
Jonathan Corbet1a51cfd2011-11-07 23:54:53 +010051 void *private_data;
MyungJoo Hama3c98b82011-10-02 00:19:15 +020052};
53
MyungJoo Hamab5f2992012-03-16 21:54:53 +010054/*
55 * The resulting frequency should be at most this. (this bound is the
56 * least upper bound; thus, the resulting freq should be lower or same)
57 * If the flag is not set, the resulting frequency should be at most the
58 * bound (greatest lower bound)
59 */
60#define DEVFREQ_FLAG_LEAST_UPPER_BOUND 0x1
61
MyungJoo Hama3c98b82011-10-02 00:19:15 +020062/**
63 * struct devfreq_dev_profile - Devfreq's user device profile
Nishanth Menone09651f2012-10-29 08:02:23 -050064 * @initial_freq: The operating frequency when devfreq_add_device() is
MyungJoo Hama3c98b82011-10-02 00:19:15 +020065 * called.
Nishanth Menone09651f2012-10-29 08:02:23 -050066 * @polling_ms: The polling interval in ms. 0 disables polling.
67 * @target: The device should set its operating frequency at
MyungJoo Hama3c98b82011-10-02 00:19:15 +020068 * freq or lowest-upper-than-freq value. If freq is
69 * higher than any operable frequency, set maximum.
70 * Before returning, target function should set
71 * freq at the current frequency.
MyungJoo Hamab5f2992012-03-16 21:54:53 +010072 * The "flags" parameter's possible values are
73 * explained above with "DEVFREQ_FLAG_*" macros.
Nishanth Menone09651f2012-10-29 08:02:23 -050074 * @get_dev_status: The device should provide the current performance
MyungJoo Hamd54cdf32015-08-18 13:45:49 +090075 * status to devfreq. Governors are recommended not to
76 * use this directly. Instead, governors are recommended
77 * to use devfreq_update_stats() along with
78 * devfreq.last_status.
Nishanth Menone09651f2012-10-29 08:02:23 -050079 * @get_cur_freq: The device should provide the current frequency
Rajagopal Venkat7f98a902012-10-26 01:50:26 +020080 * at which it is operating.
Nishanth Menone09651f2012-10-29 08:02:23 -050081 * @exit: An optional callback that is called when devfreq
MyungJoo Hama3c98b82011-10-02 00:19:15 +020082 * is removing the devfreq object due to error or
83 * from devfreq_remove_device() call. If the user
84 * has registered devfreq->nb at a notifier-head,
85 * this is the time to unregister it.
Jonghwa Leee552bba2012-08-23 20:00:46 +090086 * @freq_table: Optional list of frequencies to support statistics.
87 * @max_state: The size of freq_table.
MyungJoo Hama3c98b82011-10-02 00:19:15 +020088 */
89struct devfreq_dev_profile {
90 unsigned long initial_freq;
91 unsigned int polling_ms;
92
MyungJoo Hamab5f2992012-03-16 21:54:53 +010093 int (*target)(struct device *dev, unsigned long *freq, u32 flags);
MyungJoo Hama3c98b82011-10-02 00:19:15 +020094 int (*get_dev_status)(struct device *dev,
95 struct devfreq_dev_status *stat);
Rajagopal Venkat7f98a902012-10-26 01:50:26 +020096 int (*get_cur_freq)(struct device *dev, unsigned long *freq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +020097 void (*exit)(struct device *dev);
Jonghwa Leee552bba2012-08-23 20:00:46 +090098
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +090099 unsigned long *freq_table;
Jonghwa Leee552bba2012-08-23 20:00:46 +0900100 unsigned int max_state;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200101};
102
103/**
104 * struct devfreq_governor - Devfreq policy governor
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500105 * @node: list node - contains registered devfreq governors
Nishanth Menone09651f2012-10-29 08:02:23 -0500106 * @name: Governor's name
Chanwoo Choi2294b772017-01-31 15:38:16 +0900107 * @immutable: Immutable flag for governor. If the value is 1,
108 * this govenror is never changeable to other governor.
Nishanth Menone09651f2012-10-29 08:02:23 -0500109 * @get_target_freq: Returns desired operating frequency for the device.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200110 * Basically, get_target_freq will run
111 * devfreq_dev_profile.get_dev_status() to get the
112 * status of the device (load = busy_time / total_time).
113 * If no_central_polling is set, this callback is called
114 * only with update_devfreq() notified by OPP.
Nishanth Menone09651f2012-10-29 08:02:23 -0500115 * @event_handler: Callback for devfreq core framework to notify events
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200116 * to governors. Events include per device governor
117 * init and exit, opp changes out of devfreq, suspend
118 * and resume of per device devfreq during device idle.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200119 *
120 * Note that the callbacks are called with devfreq->lock locked by devfreq.
121 */
122struct devfreq_governor {
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500123 struct list_head node;
124
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200125 const char name[DEVFREQ_NAME_LEN];
Chanwoo Choi2294b772017-01-31 15:38:16 +0900126 const unsigned int immutable;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200127 int (*get_target_freq)(struct devfreq *this, unsigned long *freq);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200128 int (*event_handler)(struct devfreq *devfreq,
129 unsigned int event, void *data);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200130};
131
132/**
133 * struct devfreq - Device devfreq structure
Nishanth Menone09651f2012-10-29 08:02:23 -0500134 * @node: list node - contains the devices with devfreq that have been
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200135 * registered.
Nishanth Menone09651f2012-10-29 08:02:23 -0500136 * @lock: a mutex to protect accessing devfreq.
137 * @dev: device registered by devfreq class. dev.parent is the device
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200138 * using devfreq.
Nishanth Menone09651f2012-10-29 08:02:23 -0500139 * @profile: device-specific devfreq profile
140 * @governor: method how to choose frequency based on the usage.
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500141 * @governor_name: devfreq governor name for use with this devfreq
Nishanth Menone09651f2012-10-29 08:02:23 -0500142 * @nb: notifier block used to notify devfreq object that it should
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200143 * reevaluate operable frequencies. Devfreq users may use
144 * devfreq.nb to the corresponding register notifier call chain.
Nishanth Menone09651f2012-10-29 08:02:23 -0500145 * @work: delayed work for load monitoring.
146 * @previous_freq: previously configured frequency value.
147 * @data: Private data of the governor. The devfreq framework does not
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200148 * touch this.
Nishanth Menone09651f2012-10-29 08:02:23 -0500149 * @min_freq: Limit minimum frequency requested by user (0: none)
150 * @max_freq: Limit maximum frequency requested by user (0: none)
151 * @stop_polling: devfreq polling status of a device.
Jonghwa Leee552bba2012-08-23 20:00:46 +0900152 * @total_trans: Number of devfreq transitions
153 * @trans_table: Statistics of devfreq transitions
154 * @time_in_state: Statistics of devfreq states
155 * @last_stat_updated: The last time stat updated
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900156 * @transition_notifier_list: list head of DEVFREQ_TRANSITION_NOTIFIER notifier
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200157 *
158 * This structure stores the devfreq information for a give device.
159 *
160 * Note that when a governor accesses entries in struct devfreq in its
161 * functions except for the context of callbacks defined in struct
162 * devfreq_governor, the governor should protect its access with the
163 * struct mutex lock in struct devfreq. A governor may use this mutex
164 * to protect its own private data in void *data as well.
165 */
166struct devfreq {
167 struct list_head node;
168
169 struct mutex lock;
170 struct device dev;
171 struct devfreq_dev_profile *profile;
172 const struct devfreq_governor *governor;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500173 char governor_name[DEVFREQ_NAME_LEN];
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200174 struct notifier_block nb;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200175 struct delayed_work work;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200176
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200177 unsigned long previous_freq;
Javi Merino08e75e72015-08-14 18:56:56 +0100178 struct devfreq_dev_status last_status;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200179
180 void *data; /* private data for governors */
181
MyungJoo Ham6530b9de2011-12-09 16:42:19 +0900182 unsigned long min_freq;
183 unsigned long max_freq;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200184 bool stop_polling;
Jonghwa Leee552bba2012-08-23 20:00:46 +0900185
LABBE Corentin05851232013-09-26 16:50:21 +0200186 /* information for device frequency transition */
Jonghwa Leee552bba2012-08-23 20:00:46 +0900187 unsigned int total_trans;
188 unsigned int *trans_table;
189 unsigned long *time_in_state;
190 unsigned long last_stat_updated;
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900191
192 struct srcu_notifier_head transition_notifier_list;
193};
194
195struct devfreq_freqs {
196 unsigned long old;
197 unsigned long new;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200198};
199
200#if defined(CONFIG_PM_DEVFREQ)
201extern struct devfreq *devfreq_add_device(struct device *dev,
202 struct devfreq_dev_profile *profile,
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500203 const char *governor_name,
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200204 void *data);
205extern int devfreq_remove_device(struct devfreq *devfreq);
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900206extern struct devfreq *devm_devfreq_add_device(struct device *dev,
207 struct devfreq_dev_profile *profile,
208 const char *governor_name,
209 void *data);
210extern void devm_devfreq_remove_device(struct device *dev,
211 struct devfreq *devfreq);
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900212
Rafael J. Wysocki464ed182014-12-19 15:37:54 +0100213/* Supposed to be called by PM callbacks */
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200214extern int devfreq_suspend_device(struct devfreq *devfreq);
215extern int devfreq_resume_device(struct devfreq *devfreq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200216
217/* Helper functions for devfreq user device driver with OPP. */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500218extern struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100219 unsigned long *freq, u32 flags);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200220extern int devfreq_register_opp_notifier(struct device *dev,
221 struct devfreq *devfreq);
222extern int devfreq_unregister_opp_notifier(struct device *dev,
223 struct devfreq *devfreq);
Chanwoo Choid5b040d2014-05-09 16:43:09 +0900224extern int devm_devfreq_register_opp_notifier(struct device *dev,
225 struct devfreq *devfreq);
226extern void devm_devfreq_unregister_opp_notifier(struct device *dev,
227 struct devfreq *devfreq);
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900228extern int devfreq_register_notifier(struct devfreq *devfreq,
229 struct notifier_block *nb,
230 unsigned int list);
231extern int devfreq_unregister_notifier(struct devfreq *devfreq,
232 struct notifier_block *nb,
233 unsigned int list);
234extern int devm_devfreq_register_notifier(struct device *dev,
235 struct devfreq *devfreq,
236 struct notifier_block *nb,
237 unsigned int list);
238extern void devm_devfreq_unregister_notifier(struct device *dev,
239 struct devfreq *devfreq,
240 struct notifier_block *nb,
241 unsigned int list);
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900242extern struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
243 int index);
244
Javi Merino08e75e72015-08-14 18:56:56 +0100245/**
246 * devfreq_update_stats() - update the last_status pointer in struct devfreq
247 * @df: the devfreq instance whose status needs updating
MyungJoo Hamd54cdf32015-08-18 13:45:49 +0900248 *
249 * Governors are recommended to use this function along with last_status,
250 * which allows other entities to reuse the last_status without affecting
251 * the values fetched later by governors.
Javi Merino08e75e72015-08-14 18:56:56 +0100252 */
253static inline int devfreq_update_stats(struct devfreq *df)
254{
255 return df->profile->get_dev_status(df->dev.parent, &df->last_status);
256}
257
MyungJoo Ham883d5882012-11-21 17:17:11 +0900258#if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND)
MyungJoo Hamce26c5b2011-10-02 00:19:34 +0200259/**
260 * struct devfreq_simple_ondemand_data - void *data fed to struct devfreq
261 * and devfreq_add_device
Nishanth Menone09651f2012-10-29 08:02:23 -0500262 * @upthreshold: If the load is over this value, the frequency jumps.
MyungJoo Hamce26c5b2011-10-02 00:19:34 +0200263 * Specify 0 to use the default. Valid value = 0 to 100.
Nishanth Menone09651f2012-10-29 08:02:23 -0500264 * @downdifferential: If the load is under upthreshold - downdifferential,
MyungJoo Hamce26c5b2011-10-02 00:19:34 +0200265 * the governor may consider slowing the frequency down.
266 * Specify 0 to use the default. Valid value = 0 to 100.
267 * downdifferential < upthreshold must hold.
Sahitya Tummala4b185482013-10-21 11:27:13 +0530268 * @simple_scaling: Setting this flag will scale the clocks up only if the
269 * load is above @upthreshold and will scale the clocks
270 * down only if the load is below @downdifferential.
MyungJoo Hamce26c5b2011-10-02 00:19:34 +0200271 *
272 * If the fed devfreq_simple_ondemand_data pointer is NULL to the governor,
273 * the governor uses the default values.
274 */
275struct devfreq_simple_ondemand_data {
276 unsigned int upthreshold;
277 unsigned int downdifferential;
Sahitya Tummala4b185482013-10-21 11:27:13 +0530278 unsigned int simple_scaling;
MyungJoo Hamce26c5b2011-10-02 00:19:34 +0200279};
280#endif
281
Chanwoo Choi99613312016-03-22 13:44:03 +0900282#if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE)
283/**
284 * struct devfreq_passive_data - void *data fed to struct devfreq
285 * and devfreq_add_device
286 * @parent: the devfreq instance of parent device.
287 * @get_target_freq: Optional callback, Returns desired operating frequency
288 * for the device using passive governor. That is called
289 * when passive governor should decide the next frequency
290 * by using the new frequency of parent devfreq device
291 * using governors except for passive governor.
292 * If the devfreq device has the specific method to decide
293 * the next frequency, should use this callback.
294 * @this: the devfreq instance of own device.
295 * @nb: the notifier block for DEVFREQ_TRANSITION_NOTIFIER list
296 *
297 * The devfreq_passive_data have to set the devfreq instance of parent
298 * device with governors except for the passive governor. But, don't need to
299 * initialize the 'this' and 'nb' field because the devfreq core will handle
300 * them.
301 */
302struct devfreq_passive_data {
303 /* Should set the devfreq instance of parent device */
304 struct devfreq *parent;
305
306 /* Optional callback to decide the next frequency of passvice device */
307 int (*get_target_freq)(struct devfreq *this, unsigned long *freq);
308
309 /* For passive governor's internal use. Don't need to set them */
310 struct devfreq *this;
311 struct notifier_block nb;
312};
313#endif
314
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200315#else /* !CONFIG_PM_DEVFREQ */
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000316static inline struct devfreq *devfreq_add_device(struct device *dev,
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200317 struct devfreq_dev_profile *profile,
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500318 const char *governor_name,
MyungJoo Hama95e1f52012-01-11 17:44:28 +0900319 void *data)
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200320{
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900321 return ERR_PTR(-ENOSYS);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200322}
323
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000324static inline int devfreq_remove_device(struct devfreq *devfreq)
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200325{
326 return 0;
327}
328
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900329static inline struct devfreq *devm_devfreq_add_device(struct device *dev,
330 struct devfreq_dev_profile *profile,
331 const char *governor_name,
332 void *data)
333{
334 return ERR_PTR(-ENOSYS);
335}
336
337static inline void devm_devfreq_remove_device(struct device *dev,
338 struct devfreq *devfreq)
339{
340}
341
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000342static inline int devfreq_suspend_device(struct devfreq *devfreq)
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200343{
344 return 0;
345}
346
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000347static inline int devfreq_resume_device(struct devfreq *devfreq)
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200348{
349 return 0;
350}
351
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500352static inline struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100353 unsigned long *freq, u32 flags)
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200354{
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000355 return ERR_PTR(-EINVAL);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200356}
357
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000358static inline int devfreq_register_opp_notifier(struct device *dev,
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200359 struct devfreq *devfreq)
360{
361 return -EINVAL;
362}
363
Rajagopal Venkat5faaa032013-03-21 13:28:25 +0000364static inline int devfreq_unregister_opp_notifier(struct device *dev,
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200365 struct devfreq *devfreq)
366{
367 return -EINVAL;
368}
369
Chanwoo Choid5b040d2014-05-09 16:43:09 +0900370static inline int devm_devfreq_register_opp_notifier(struct device *dev,
371 struct devfreq *devfreq)
372{
373 return -EINVAL;
374}
375
376static inline void devm_devfreq_unregister_opp_notifier(struct device *dev,
377 struct devfreq *devfreq)
378{
379}
Javi Merino08e75e72015-08-14 18:56:56 +0100380
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900381static inline int devfreq_register_notifier(struct devfreq *devfreq,
382 struct notifier_block *nb,
383 unsigned int list)
384{
385 return 0;
386}
387
388static inline int devfreq_unregister_notifier(struct devfreq *devfreq,
389 struct notifier_block *nb,
390 unsigned int list)
391{
392 return 0;
393}
394
395static inline int devm_devfreq_register_notifier(struct device *dev,
396 struct devfreq *devfreq,
397 struct notifier_block *nb,
398 unsigned int list)
399{
400 return 0;
401}
402
403static inline void devm_devfreq_unregister_notifier(struct device *dev,
404 struct devfreq *devfreq,
405 struct notifier_block *nb,
406 unsigned int list)
407{
408}
409
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900410static inline struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
411 int index)
412{
413 return ERR_PTR(-ENODEV);
414}
415
Javi Merino08e75e72015-08-14 18:56:56 +0100416static inline int devfreq_update_stats(struct devfreq *df)
417{
418 return -EINVAL;
419}
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200420#endif /* CONFIG_PM_DEVFREQ */
421
422#endif /* __LINUX_DEVFREQ_H__ */