]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - include/linux/devfreq.h
83cd456573fcd30ee543f505e15084e7eb1d93dc
[sojka/nv-tegra/linux-3.10.git] / include / linux / devfreq.h
1 /*
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  * Copyright (c) 2014, NVIDIA CORPORATION.  All rights reserved.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 #ifndef __LINUX_DEVFREQ_H__
15 #define __LINUX_DEVFREQ_H__
16
17 #include <linux/device.h>
18 #include <linux/notifier.h>
19 #include <linux/opp.h>
20
21 #define DEVFREQ_NAME_LEN 16
22
23 struct devfreq;
24
25 /**
26  * struct devfreq_dev_status - Data given from devfreq user device to
27  *                           governors. Represents the performance
28  *                           statistics.
29  * @total_time:         The total time represented by this instance of
30  *                      devfreq_dev_status
31  * @busy_time:          The time that the device was working among the
32  *                      total_time.
33  * @current_frequency:  The operating frequency.
34  * @private_data:       An entry not specified by the devfreq framework.
35  *                      A device and a specific governor may have their
36  *                      own protocol with private_data. However, because
37  *                      this is governor-specific, a governor using this
38  *                      will be only compatible with devices aware of it.
39  * @busy:               The current status of the device; True if the
40  *                      device is busy, false if it is not.
41  */
42 struct devfreq_dev_status {
43         /* both since the last measure */
44         unsigned long total_time;
45         unsigned long busy_time;
46         unsigned long current_frequency;
47         void *private_data;
48         bool busy;
49 };
50
51 /*
52  * The resulting frequency should be at most this. (this bound is the
53  * least upper bound; thus, the resulting freq should be lower or same)
54  * If the flag is not set, the resulting frequency should be at most the
55  * bound (greatest lower bound)
56  */
57 #define DEVFREQ_FLAG_LEAST_UPPER_BOUND          0x1
58
59 /**
60  * struct devfreq_dev_profile - Devfreq's user device profile
61  * @initial_freq:       The operating frequency when devfreq_add_device() is
62  *                      called.
63  * @polling_ms:         The polling interval in ms. 0 disables polling.
64  * @target:             The device should set its operating frequency at
65  *                      freq or lowest-upper-than-freq value. If freq is
66  *                      higher than any operable frequency, set maximum.
67  *                      Before returning, target function should set
68  *                      freq at the current frequency.
69  *                      The "flags" parameter's possible values are
70  *                      explained above with "DEVFREQ_FLAG_*" macros.
71  * @get_dev_status:     The device should provide the current performance
72  *                      status to devfreq, which is used by governors.
73  * @get_cur_freq:       The device should provide the current frequency
74  *                      at which it is operating.
75  * @exit:               An optional callback that is called when devfreq
76  *                      is removing the devfreq object due to error or
77  *                      from devfreq_remove_device() call. If the user
78  *                      has registered devfreq->nb at a notifier-head,
79  *                      this is the time to unregister it.
80  * @freq_table: Optional list of frequencies to support statistics.
81  * @max_state:  The size of freq_table.
82  */
83 struct devfreq_dev_profile {
84         unsigned long initial_freq;
85         unsigned int polling_ms;
86
87         int (*target)(struct device *dev, unsigned long *freq, u32 flags);
88         int (*get_dev_status)(struct device *dev,
89                               struct devfreq_dev_status *stat);
90         int (*get_cur_freq)(struct device *dev, unsigned long *freq);
91         void (*exit)(struct device *dev);
92
93         unsigned long *freq_table;
94         unsigned int max_state;
95 };
96
97 /**
98  * struct devfreq_governor - Devfreq policy governor
99  * @node:               list node - contains registered devfreq governors
100  * @name:               Governor's name
101  * @get_target_freq:    Returns desired operating frequency for the device.
102  *                      Basically, get_target_freq will run
103  *                      devfreq_dev_profile.get_dev_status() to get the
104  *                      status of the device (load = busy_time / total_time).
105  *                      If no_central_polling is set, this callback is called
106  *                      only with update_devfreq() notified by OPP.
107  * @event_handler:      Callback for devfreq core framework to notify events
108  *                      to governors. Events include per device governor
109  *                      init and exit, opp changes out of devfreq, suspend
110  *                      and resume of per device devfreq during device idle.
111  *
112  * Note that the callbacks are called with devfreq->lock locked by devfreq.
113  */
114 struct devfreq_governor {
115         struct list_head node;
116
117         const char name[DEVFREQ_NAME_LEN];
118         int (*get_target_freq)(struct devfreq *this, unsigned long *freq);
119         int (*event_handler)(struct devfreq *devfreq,
120                                 unsigned int event, void *data);
121 };
122
123 /**
124  * struct devfreq - Device devfreq structure
125  * @node:       list node - contains the devices with devfreq that have been
126  *              registered.
127  * @lock:       a mutex to protect accessing devfreq.
128  * @dev:        device registered by devfreq class. dev.parent is the device
129  *              using devfreq.
130  * @profile:    device-specific devfreq profile
131  * @governor:   method how to choose frequency based on the usage.
132  * @governor_name:      devfreq governor name for use with this devfreq
133  * @nb:         notifier block used to notify devfreq object that it should
134  *              reevaluate operable frequencies. Devfreq users may use
135  *              devfreq.nb to the corresponding register notifier call chain.
136  * @work:       delayed work for load monitoring.
137  * @previous_freq:      previously configured frequency value.
138  * @data:       Private data of the governor. The devfreq framework does not
139  *              touch this.
140  * @min_freq:   Limit minimum frequency requested by user (0: none)
141  * @max_freq:   Limit maximum frequency requested by user (0: none)
142  * @stop_polling:        devfreq polling status of a device.
143  * @total_trans:        Number of devfreq transitions
144  * @trans_table:        Statistics of devfreq transitions
145  * @time_in_state:      Statistics of devfreq states
146  * @last_stat_updated:  The last time stat updated
147  *
148  * This structure stores the devfreq information for a give device.
149  *
150  * Note that when a governor accesses entries in struct devfreq in its
151  * functions except for the context of callbacks defined in struct
152  * devfreq_governor, the governor should protect its access with the
153  * struct mutex lock in struct devfreq. A governor may use this mutex
154  * to protect its own private data in void *data as well.
155  */
156 struct devfreq {
157         struct list_head node;
158
159         struct mutex lock;
160         struct device dev;
161         struct devfreq_dev_profile *profile;
162         const struct devfreq_governor *governor;
163         char governor_name[DEVFREQ_NAME_LEN];
164         struct notifier_block nb;
165         struct delayed_work work;
166
167         unsigned long previous_freq;
168
169         void *data; /* private data for governors */
170
171         unsigned long min_freq;
172         unsigned long max_freq;
173         bool stop_polling;
174
175         /* information for device freqeuncy transition */
176         unsigned int total_trans;
177         unsigned int *trans_table;
178         unsigned long *time_in_state;
179         unsigned long last_stat_updated;
180 };
181
182 #if defined(CONFIG_PM_DEVFREQ)
183 extern struct devfreq *devfreq_add_device(struct device *dev,
184                                   struct devfreq_dev_profile *profile,
185                                   const char *governor_name,
186                                   void *data);
187 extern int devfreq_remove_device(struct devfreq *devfreq);
188 extern int devfreq_suspend_device(struct devfreq *devfreq);
189 extern int devfreq_resume_device(struct devfreq *devfreq);
190
191 /* Helper functions for devfreq user device driver with OPP. */
192 extern struct opp *devfreq_recommended_opp(struct device *dev,
193                                            unsigned long *freq, u32 flags);
194 extern int devfreq_register_opp_notifier(struct device *dev,
195                                          struct devfreq *devfreq);
196 extern int devfreq_unregister_opp_notifier(struct device *dev,
197                                            struct devfreq *devfreq);
198
199 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND)
200 /**
201  * struct devfreq_simple_ondemand_data - void *data fed to struct devfreq
202  *      and devfreq_add_device
203  * @upthreshold:        If the load is over this value, the frequency jumps.
204  *                      Specify 0 to use the default. Valid value = 0 to 100.
205  * @downdifferential:   If the load is under upthreshold - downdifferential,
206  *                      the governor may consider slowing the frequency down.
207  *                      Specify 0 to use the default. Valid value = 0 to 100.
208  *                      downdifferential < upthreshold must hold.
209  *
210  * If the fed devfreq_simple_ondemand_data pointer is NULL to the governor,
211  * the governor uses the default values.
212  */
213 struct devfreq_simple_ondemand_data {
214         unsigned int upthreshold;
215         unsigned int downdifferential;
216 };
217 #endif
218
219 #else /* !CONFIG_PM_DEVFREQ */
220 static inline struct devfreq *devfreq_add_device(struct device *dev,
221                                           struct devfreq_dev_profile *profile,
222                                           const char *governor_name,
223                                           void *data)
224 {
225         return NULL;
226 }
227
228 static inline int devfreq_remove_device(struct devfreq *devfreq)
229 {
230         return 0;
231 }
232
233 static inline int devfreq_suspend_device(struct devfreq *devfreq)
234 {
235         return 0;
236 }
237
238 static inline int devfreq_resume_device(struct devfreq *devfreq)
239 {
240         return 0;
241 }
242
243 static inline struct opp *devfreq_recommended_opp(struct device *dev,
244                                            unsigned long *freq, u32 flags)
245 {
246         return ERR_PTR(-EINVAL);
247 }
248
249 static inline int devfreq_register_opp_notifier(struct device *dev,
250                                          struct devfreq *devfreq)
251 {
252         return -EINVAL;
253 }
254
255 static inline int devfreq_unregister_opp_notifier(struct device *dev,
256                                            struct devfreq *devfreq)
257 {
258         return -EINVAL;
259 }
260
261 #endif /* CONFIG_PM_DEVFREQ */
262
263 #endif /* __LINUX_DEVFREQ_H__ */