blob: 20a9422c25529a88c047b9fceb14b33c04094b2a [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#include <linux/kernel.h>
14#include <linux/sched.h>
15#include <linux/errno.h>
16#include <linux/err.h>
17#include <linux/init.h>
MyungJoo Ham952f6d12011-11-10 10:16:23 +010018#include <linux/module.h>
MyungJoo Hama3c98b82011-10-02 00:19:15 +020019#include <linux/slab.h>
MyungJoo Ham952f6d12011-11-10 10:16:23 +010020#include <linux/stat.h>
Nishanth Menone4db1c72013-09-19 16:03:52 -050021#include <linux/pm_opp.h>
MyungJoo Hama3c98b82011-10-02 00:19:15 +020022#include <linux/devfreq.h>
23#include <linux/workqueue.h>
24#include <linux/platform_device.h>
25#include <linux/list.h>
26#include <linux/printk.h>
27#include <linux/hrtimer.h>
Chanwoo Choi8f510ae2015-11-10 20:31:07 +090028#include <linux/of.h>
MyungJoo Hama3c98b82011-10-02 00:19:15 +020029#include "governor.h"
30
Nishanth Menon1a1357e2012-10-26 01:50:53 +020031static struct class *devfreq_class;
MyungJoo Hama3c98b82011-10-02 00:19:15 +020032
33/*
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +020034 * devfreq core provides delayed work based load monitoring helper
35 * functions. Governors can use these or can implement their own
36 * monitoring mechanism.
MyungJoo Hama3c98b82011-10-02 00:19:15 +020037 */
MyungJoo Hama3c98b82011-10-02 00:19:15 +020038static struct workqueue_struct *devfreq_wq;
MyungJoo Hama3c98b82011-10-02 00:19:15 +020039
Nishanth Menon3aa173b2012-10-29 15:01:43 -050040/* The list of all device-devfreq governors */
41static LIST_HEAD(devfreq_governor_list);
MyungJoo Hama3c98b82011-10-02 00:19:15 +020042/* The list of all device-devfreq */
43static LIST_HEAD(devfreq_list);
44static DEFINE_MUTEX(devfreq_list_lock);
45
46/**
47 * find_device_devfreq() - find devfreq struct using device pointer
48 * @dev: device pointer used to lookup device devfreq.
49 *
50 * Search the list of device devfreqs and return the matched device's
51 * devfreq info. devfreq_list_lock should be held by the caller.
52 */
53static struct devfreq *find_device_devfreq(struct device *dev)
54{
55 struct devfreq *tmp_devfreq;
56
Viresh Kumar9348da22015-08-10 11:42:25 +053057 if (IS_ERR_OR_NULL(dev)) {
MyungJoo Hama3c98b82011-10-02 00:19:15 +020058 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
59 return ERR_PTR(-EINVAL);
60 }
61 WARN(!mutex_is_locked(&devfreq_list_lock),
62 "devfreq_list_lock must be locked.");
63
64 list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
65 if (tmp_devfreq->dev.parent == dev)
66 return tmp_devfreq;
67 }
68
69 return ERR_PTR(-ENODEV);
70}
71
72/**
Jonghwa Leee552bba2012-08-23 20:00:46 +090073 * devfreq_get_freq_level() - Lookup freq_table for the frequency
74 * @devfreq: the devfreq instance
75 * @freq: the target frequency
76 */
77static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq)
78{
79 int lev;
80
81 for (lev = 0; lev < devfreq->profile->max_state; lev++)
82 if (freq == devfreq->profile->freq_table[lev])
83 return lev;
84
85 return -EINVAL;
86}
87
88/**
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +090089 * devfreq_set_freq_table() - Initialize freq_table for the frequency
90 * @devfreq: the devfreq instance
91 */
92static void devfreq_set_freq_table(struct devfreq *devfreq)
93{
94 struct devfreq_dev_profile *profile = devfreq->profile;
95 struct dev_pm_opp *opp;
96 unsigned long freq;
97 int i, count;
98
99 /* Initialize the freq_table from OPP table */
100 count = dev_pm_opp_get_opp_count(devfreq->dev.parent);
101 if (count <= 0)
102 return;
103
104 profile->max_state = count;
105 profile->freq_table = devm_kcalloc(devfreq->dev.parent,
106 profile->max_state,
107 sizeof(*profile->freq_table),
108 GFP_KERNEL);
109 if (!profile->freq_table) {
110 profile->max_state = 0;
111 return;
112 }
113
114 rcu_read_lock();
115 for (i = 0, freq = 0; i < profile->max_state; i++, freq++) {
116 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq);
117 if (IS_ERR(opp)) {
118 devm_kfree(devfreq->dev.parent, profile->freq_table);
119 profile->max_state = 0;
120 rcu_read_unlock();
121 return;
122 }
123 profile->freq_table[i] = freq;
124 }
125 rcu_read_unlock();
126}
127
128/**
Jonghwa Leee552bba2012-08-23 20:00:46 +0900129 * devfreq_update_status() - Update statistics of devfreq behavior
130 * @devfreq: the devfreq instance
131 * @freq: the update target frequency
132 */
133static int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
134{
Saravana Kannane35d35a2014-02-27 19:38:57 -0800135 int lev, prev_lev, ret = 0;
Jonghwa Leee552bba2012-08-23 20:00:46 +0900136 unsigned long cur_time;
137
Jonghwa Leee552bba2012-08-23 20:00:46 +0900138 cur_time = jiffies;
Saravana Kannane35d35a2014-02-27 19:38:57 -0800139
140 prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
141 if (prev_lev < 0) {
142 ret = prev_lev;
143 goto out;
144 }
145
146 devfreq->time_in_state[prev_lev] +=
Jonghwa Leee552bba2012-08-23 20:00:46 +0900147 cur_time - devfreq->last_stat_updated;
Saravana Kannane35d35a2014-02-27 19:38:57 -0800148
149 lev = devfreq_get_freq_level(devfreq, freq);
150 if (lev < 0) {
151 ret = lev;
152 goto out;
153 }
154
155 if (lev != prev_lev) {
Jonghwa Leee552bba2012-08-23 20:00:46 +0900156 devfreq->trans_table[(prev_lev *
157 devfreq->profile->max_state) + lev]++;
158 devfreq->total_trans++;
159 }
Jonghwa Leee552bba2012-08-23 20:00:46 +0900160
Saravana Kannane35d35a2014-02-27 19:38:57 -0800161out:
162 devfreq->last_stat_updated = cur_time;
163 return ret;
Jonghwa Leee552bba2012-08-23 20:00:46 +0900164}
165
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500166/**
167 * find_devfreq_governor() - find devfreq governor from name
168 * @name: name of the governor
169 *
170 * Search the list of devfreq governors and return the matched
171 * governor's pointer. devfreq_list_lock should be held by the caller.
172 */
173static struct devfreq_governor *find_devfreq_governor(const char *name)
174{
175 struct devfreq_governor *tmp_governor;
176
Viresh Kumar9348da22015-08-10 11:42:25 +0530177 if (IS_ERR_OR_NULL(name)) {
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500178 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
179 return ERR_PTR(-EINVAL);
180 }
181 WARN(!mutex_is_locked(&devfreq_list_lock),
182 "devfreq_list_lock must be locked.");
183
184 list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
185 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
186 return tmp_governor;
187 }
188
189 return ERR_PTR(-ENODEV);
190}
191
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200192/* Load monitoring helper functions for governors use */
193
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200194/**
195 * update_devfreq() - Reevaluate the device and configure frequency.
196 * @devfreq: the devfreq instance.
197 *
198 * Note: Lock devfreq->lock before calling update_devfreq
199 * This function is exported for governors.
200 */
201int update_devfreq(struct devfreq *devfreq)
202{
203 unsigned long freq;
204 int err = 0;
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100205 u32 flags = 0;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200206
207 if (!mutex_is_locked(&devfreq->lock)) {
208 WARN(true, "devfreq->lock must be locked by the caller.\n");
209 return -EINVAL;
210 }
211
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500212 if (!devfreq->governor)
213 return -EINVAL;
214
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200215 /* Reevaluate the proper frequency */
216 err = devfreq->governor->get_target_freq(devfreq, &freq);
217 if (err)
218 return err;
219
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100220 /*
Javi Merinod3b7e172015-08-14 18:57:00 +0100221 * Adjust the frequency with user freq and QoS.
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100222 *
Javi Merinod3b7e172015-08-14 18:57:00 +0100223 * List from the highest priority
224 * max_freq
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100225 * min_freq
226 */
227
228 if (devfreq->min_freq && freq < devfreq->min_freq) {
229 freq = devfreq->min_freq;
230 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
231 }
232 if (devfreq->max_freq && freq > devfreq->max_freq) {
233 freq = devfreq->max_freq;
234 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
235 }
236
237 err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200238 if (err)
239 return err;
240
Jonghwa Leee552bba2012-08-23 20:00:46 +0900241 if (devfreq->profile->freq_table)
242 if (devfreq_update_status(devfreq, freq))
243 dev_err(&devfreq->dev,
244 "Couldn't update frequency transition information.\n");
245
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200246 devfreq->previous_freq = freq;
247 return err;
248}
Nishanth Menon2df50212012-10-29 15:01:42 -0500249EXPORT_SYMBOL(update_devfreq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200250
251/**
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200252 * devfreq_monitor() - Periodically poll devfreq objects.
253 * @work: the work struct used to run devfreq_monitor periodically.
254 *
255 */
256static void devfreq_monitor(struct work_struct *work)
257{
258 int err;
259 struct devfreq *devfreq = container_of(work,
260 struct devfreq, work.work);
261
262 mutex_lock(&devfreq->lock);
263 err = update_devfreq(devfreq);
264 if (err)
265 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
266
267 queue_delayed_work(devfreq_wq, &devfreq->work,
268 msecs_to_jiffies(devfreq->profile->polling_ms));
269 mutex_unlock(&devfreq->lock);
270}
271
272/**
273 * devfreq_monitor_start() - Start load monitoring of devfreq instance
274 * @devfreq: the devfreq instance.
275 *
276 * Helper function for starting devfreq device load monitoing. By
277 * default delayed work based monitoring is supported. Function
278 * to be called from governor in response to DEVFREQ_GOV_START
279 * event when device is added to devfreq framework.
280 */
281void devfreq_monitor_start(struct devfreq *devfreq)
282{
283 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
284 if (devfreq->profile->polling_ms)
285 queue_delayed_work(devfreq_wq, &devfreq->work,
286 msecs_to_jiffies(devfreq->profile->polling_ms));
287}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100288EXPORT_SYMBOL(devfreq_monitor_start);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200289
290/**
291 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
292 * @devfreq: the devfreq instance.
293 *
294 * Helper function to stop devfreq device load monitoing. Function
295 * to be called from governor in response to DEVFREQ_GOV_STOP
296 * event when device is removed from devfreq framework.
297 */
298void devfreq_monitor_stop(struct devfreq *devfreq)
299{
300 cancel_delayed_work_sync(&devfreq->work);
301}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100302EXPORT_SYMBOL(devfreq_monitor_stop);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200303
304/**
305 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
306 * @devfreq: the devfreq instance.
307 *
308 * Helper function to suspend devfreq device load monitoing. Function
309 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
310 * event or when polling interval is set to zero.
311 *
312 * Note: Though this function is same as devfreq_monitor_stop(),
313 * intentionally kept separate to provide hooks for collecting
314 * transition statistics.
315 */
316void devfreq_monitor_suspend(struct devfreq *devfreq)
317{
318 mutex_lock(&devfreq->lock);
319 if (devfreq->stop_polling) {
320 mutex_unlock(&devfreq->lock);
321 return;
322 }
323
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530324 devfreq_update_status(devfreq, devfreq->previous_freq);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200325 devfreq->stop_polling = true;
326 mutex_unlock(&devfreq->lock);
327 cancel_delayed_work_sync(&devfreq->work);
328}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100329EXPORT_SYMBOL(devfreq_monitor_suspend);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200330
331/**
332 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
333 * @devfreq: the devfreq instance.
334 *
335 * Helper function to resume devfreq device load monitoing. Function
336 * to be called from governor in response to DEVFREQ_GOV_RESUME
337 * event or when polling interval is set to non-zero.
338 */
339void devfreq_monitor_resume(struct devfreq *devfreq)
340{
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530341 unsigned long freq;
342
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200343 mutex_lock(&devfreq->lock);
344 if (!devfreq->stop_polling)
345 goto out;
346
347 if (!delayed_work_pending(&devfreq->work) &&
348 devfreq->profile->polling_ms)
349 queue_delayed_work(devfreq_wq, &devfreq->work,
350 msecs_to_jiffies(devfreq->profile->polling_ms));
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530351
352 devfreq->last_stat_updated = jiffies;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200353 devfreq->stop_polling = false;
354
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530355 if (devfreq->profile->get_cur_freq &&
356 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
357 devfreq->previous_freq = freq;
358
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200359out:
360 mutex_unlock(&devfreq->lock);
361}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100362EXPORT_SYMBOL(devfreq_monitor_resume);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200363
364/**
365 * devfreq_interval_update() - Update device devfreq monitoring interval
366 * @devfreq: the devfreq instance.
367 * @delay: new polling interval to be set.
368 *
369 * Helper function to set new load monitoring polling interval. Function
370 * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
371 */
372void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
373{
374 unsigned int cur_delay = devfreq->profile->polling_ms;
375 unsigned int new_delay = *delay;
376
377 mutex_lock(&devfreq->lock);
378 devfreq->profile->polling_ms = new_delay;
379
380 if (devfreq->stop_polling)
381 goto out;
382
383 /* if new delay is zero, stop polling */
384 if (!new_delay) {
385 mutex_unlock(&devfreq->lock);
386 cancel_delayed_work_sync(&devfreq->work);
387 return;
388 }
389
390 /* if current delay is zero, start polling with new delay */
391 if (!cur_delay) {
392 queue_delayed_work(devfreq_wq, &devfreq->work,
393 msecs_to_jiffies(devfreq->profile->polling_ms));
394 goto out;
395 }
396
397 /* if current delay is greater than new delay, restart polling */
398 if (cur_delay > new_delay) {
399 mutex_unlock(&devfreq->lock);
400 cancel_delayed_work_sync(&devfreq->work);
401 mutex_lock(&devfreq->lock);
402 if (!devfreq->stop_polling)
403 queue_delayed_work(devfreq_wq, &devfreq->work,
404 msecs_to_jiffies(devfreq->profile->polling_ms));
405 }
406out:
407 mutex_unlock(&devfreq->lock);
408}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100409EXPORT_SYMBOL(devfreq_interval_update);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200410
411/**
412 * devfreq_notifier_call() - Notify that the device frequency requirements
413 * has been changed out of devfreq framework.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +0200414 * @nb: the notifier_block (supposed to be devfreq->nb)
415 * @type: not used
416 * @devp: not used
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200417 *
418 * Called by a notifier that uses devfreq->nb.
419 */
420static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
421 void *devp)
422{
423 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
424 int ret;
425
426 mutex_lock(&devfreq->lock);
427 ret = update_devfreq(devfreq);
428 mutex_unlock(&devfreq->lock);
429
430 return ret;
431}
432
433/**
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200434 * _remove_devfreq() - Remove devfreq from the list and release its resources.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200435 * @devfreq: the devfreq struct
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200436 */
Chanwoo Choi585fc832014-05-09 16:43:07 +0900437static void _remove_devfreq(struct devfreq *devfreq)
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200438{
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200439 mutex_lock(&devfreq_list_lock);
440 if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
441 mutex_unlock(&devfreq_list_lock);
442 dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200443 return;
444 }
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200445 list_del(&devfreq->node);
446 mutex_unlock(&devfreq_list_lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200447
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500448 if (devfreq->governor)
449 devfreq->governor->event_handler(devfreq,
450 DEVFREQ_GOV_STOP, NULL);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200451
452 if (devfreq->profile->exit)
453 devfreq->profile->exit(devfreq->dev.parent);
454
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200455 mutex_destroy(&devfreq->lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200456 kfree(devfreq);
457}
458
459/**
460 * devfreq_dev_release() - Callback for struct device to release the device.
461 * @dev: the devfreq device
462 *
463 * This calls _remove_devfreq() if _remove_devfreq() is not called.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200464 */
465static void devfreq_dev_release(struct device *dev)
466{
467 struct devfreq *devfreq = to_devfreq(dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200468
Chanwoo Choi585fc832014-05-09 16:43:07 +0900469 _remove_devfreq(devfreq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200470}
471
472/**
473 * devfreq_add_device() - Add devfreq feature to the device
474 * @dev: the device to add devfreq feature.
475 * @profile: device-specific profile to run devfreq.
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500476 * @governor_name: name of the policy to choose frequency.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200477 * @data: private data for the governor. The devfreq framework does not
478 * touch this value.
479 */
480struct devfreq *devfreq_add_device(struct device *dev,
481 struct devfreq_dev_profile *profile,
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500482 const char *governor_name,
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200483 void *data)
484{
485 struct devfreq *devfreq;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500486 struct devfreq_governor *governor;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200487 int err = 0;
488
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500489 if (!dev || !profile || !governor_name) {
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200490 dev_err(dev, "%s: Invalid parameters.\n", __func__);
491 return ERR_PTR(-EINVAL);
492 }
493
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200494 mutex_lock(&devfreq_list_lock);
495 devfreq = find_device_devfreq(dev);
496 mutex_unlock(&devfreq_list_lock);
497 if (!IS_ERR(devfreq)) {
498 dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__);
499 err = -EINVAL;
500 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200501 }
502
503 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
504 if (!devfreq) {
505 dev_err(dev, "%s: Unable to create devfreq for the device\n",
506 __func__);
507 err = -ENOMEM;
Axel Lin3f19f082011-11-15 21:59:09 +0100508 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200509 }
510
511 mutex_init(&devfreq->lock);
512 mutex_lock(&devfreq->lock);
513 devfreq->dev.parent = dev;
514 devfreq->dev.class = devfreq_class;
515 devfreq->dev.release = devfreq_dev_release;
516 devfreq->profile = profile;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500517 strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200518 devfreq->previous_freq = profile->initial_freq;
519 devfreq->data = data;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200520 devfreq->nb.notifier_call = devfreq_notifier_call;
521
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900522 if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
523 mutex_unlock(&devfreq->lock);
524 devfreq_set_freq_table(devfreq);
525 mutex_lock(&devfreq->lock);
526 }
527
Jonghwa Leee552bba2012-08-23 20:00:46 +0900528 devfreq->trans_table = devm_kzalloc(dev, sizeof(unsigned int) *
529 devfreq->profile->max_state *
530 devfreq->profile->max_state,
531 GFP_KERNEL);
Xiaolong Ye5f25f0662015-09-11 11:05:23 +0800532 devfreq->time_in_state = devm_kzalloc(dev, sizeof(unsigned long) *
Jonghwa Leee552bba2012-08-23 20:00:46 +0900533 devfreq->profile->max_state,
534 GFP_KERNEL);
535 devfreq->last_stat_updated = jiffies;
536
Kees Cook02aa2a32013-07-03 15:04:56 -0700537 dev_set_name(&devfreq->dev, "%s", dev_name(dev));
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200538 err = device_register(&devfreq->dev);
539 if (err) {
540 put_device(&devfreq->dev);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200541 mutex_unlock(&devfreq->lock);
Geliang Tang6d3cbfa2015-10-01 22:18:19 +0800542 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200543 }
544
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200545 mutex_unlock(&devfreq->lock);
546
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200547 mutex_lock(&devfreq_list_lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200548 list_add(&devfreq->node, &devfreq_list);
549
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500550 governor = find_devfreq_governor(devfreq->governor_name);
551 if (!IS_ERR(governor))
552 devfreq->governor = governor;
553 if (devfreq->governor)
554 err = devfreq->governor->event_handler(devfreq,
555 DEVFREQ_GOV_START, NULL);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200556 mutex_unlock(&devfreq_list_lock);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200557 if (err) {
558 dev_err(dev, "%s: Unable to start governor for the device\n",
559 __func__);
560 goto err_init;
561 }
562
Axel Lin3f19f082011-11-15 21:59:09 +0100563 return devfreq;
564
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200565err_init:
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200566 list_del(&devfreq->node);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200567 device_unregister(&devfreq->dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200568 kfree(devfreq);
Axel Lin3f19f082011-11-15 21:59:09 +0100569err_out:
570 return ERR_PTR(err);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200571}
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200572EXPORT_SYMBOL(devfreq_add_device);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200573
574/**
575 * devfreq_remove_device() - Remove devfreq feature from a device.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +0200576 * @devfreq: the devfreq instance to be removed
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900577 *
578 * The opposite of devfreq_add_device().
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200579 */
580int devfreq_remove_device(struct devfreq *devfreq)
581{
582 if (!devfreq)
583 return -EINVAL;
584
Chanwoo Choi585fc832014-05-09 16:43:07 +0900585 device_unregister(&devfreq->dev);
586 put_device(&devfreq->dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200587
588 return 0;
589}
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200590EXPORT_SYMBOL(devfreq_remove_device);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200591
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900592static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
593{
594 struct devfreq **r = res;
595
596 if (WARN_ON(!r || !*r))
597 return 0;
598
599 return *r == data;
600}
601
602static void devm_devfreq_dev_release(struct device *dev, void *res)
603{
604 devfreq_remove_device(*(struct devfreq **)res);
605}
606
607/**
608 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
609 * @dev: the device to add devfreq feature.
610 * @profile: device-specific profile to run devfreq.
611 * @governor_name: name of the policy to choose frequency.
612 * @data: private data for the governor. The devfreq framework does not
613 * touch this value.
614 *
615 * This function manages automatically the memory of devfreq device using device
616 * resource management and simplify the free operation for memory of devfreq
617 * device.
618 */
619struct devfreq *devm_devfreq_add_device(struct device *dev,
620 struct devfreq_dev_profile *profile,
621 const char *governor_name,
622 void *data)
623{
624 struct devfreq **ptr, *devfreq;
625
626 ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
627 if (!ptr)
628 return ERR_PTR(-ENOMEM);
629
630 devfreq = devfreq_add_device(dev, profile, governor_name, data);
631 if (IS_ERR(devfreq)) {
632 devres_free(ptr);
633 return ERR_PTR(-ENOMEM);
634 }
635
636 *ptr = devfreq;
637 devres_add(dev, ptr);
638
639 return devfreq;
640}
641EXPORT_SYMBOL(devm_devfreq_add_device);
642
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900643#ifdef CONFIG_OF
644/*
645 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
646 * @dev - instance to the given device
647 * @index - index into list of devfreq
648 *
649 * return the instance of devfreq device
650 */
651struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
652{
653 struct device_node *node;
654 struct devfreq *devfreq;
655
656 if (!dev)
657 return ERR_PTR(-EINVAL);
658
659 if (!dev->of_node)
660 return ERR_PTR(-EINVAL);
661
662 node = of_parse_phandle(dev->of_node, "devfreq", index);
663 if (!node)
664 return ERR_PTR(-ENODEV);
665
666 mutex_lock(&devfreq_list_lock);
667 list_for_each_entry(devfreq, &devfreq_list, node) {
668 if (devfreq->dev.parent
669 && devfreq->dev.parent->of_node == node) {
670 mutex_unlock(&devfreq_list_lock);
671 return devfreq;
672 }
673 }
674 mutex_unlock(&devfreq_list_lock);
675
676 return ERR_PTR(-EPROBE_DEFER);
677}
678#else
679struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
680{
681 return ERR_PTR(-ENODEV);
682}
683#endif /* CONFIG_OF */
684EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
685
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900686/**
687 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
688 * @dev: the device to add devfreq feature.
689 * @devfreq: the devfreq instance to be removed
690 */
691void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
692{
693 WARN_ON(devres_release(dev, devm_devfreq_dev_release,
694 devm_devfreq_dev_match, devfreq));
695}
696EXPORT_SYMBOL(devm_devfreq_remove_device);
697
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200698/**
699 * devfreq_suspend_device() - Suspend devfreq of a device.
700 * @devfreq: the devfreq instance to be suspended
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900701 *
702 * This function is intended to be called by the pm callbacks
703 * (e.g., runtime_suspend, suspend) of the device driver that
704 * holds the devfreq.
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200705 */
706int devfreq_suspend_device(struct devfreq *devfreq)
707{
708 if (!devfreq)
709 return -EINVAL;
710
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500711 if (!devfreq->governor)
712 return 0;
713
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200714 return devfreq->governor->event_handler(devfreq,
715 DEVFREQ_GOV_SUSPEND, NULL);
716}
717EXPORT_SYMBOL(devfreq_suspend_device);
718
719/**
720 * devfreq_resume_device() - Resume devfreq of a device.
721 * @devfreq: the devfreq instance to be resumed
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900722 *
723 * This function is intended to be called by the pm callbacks
724 * (e.g., runtime_resume, resume) of the device driver that
725 * holds the devfreq.
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200726 */
727int devfreq_resume_device(struct devfreq *devfreq)
728{
729 if (!devfreq)
730 return -EINVAL;
731
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500732 if (!devfreq->governor)
733 return 0;
734
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200735 return devfreq->governor->event_handler(devfreq,
736 DEVFREQ_GOV_RESUME, NULL);
737}
738EXPORT_SYMBOL(devfreq_resume_device);
739
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500740/**
741 * devfreq_add_governor() - Add devfreq governor
742 * @governor: the devfreq governor to be added
743 */
744int devfreq_add_governor(struct devfreq_governor *governor)
745{
746 struct devfreq_governor *g;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500747 struct devfreq *devfreq;
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500748 int err = 0;
749
750 if (!governor) {
751 pr_err("%s: Invalid parameters.\n", __func__);
752 return -EINVAL;
753 }
754
755 mutex_lock(&devfreq_list_lock);
756 g = find_devfreq_governor(governor->name);
757 if (!IS_ERR(g)) {
758 pr_err("%s: governor %s already registered\n", __func__,
759 g->name);
760 err = -EINVAL;
761 goto err_out;
762 }
763
764 list_add(&governor->node, &devfreq_governor_list);
765
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500766 list_for_each_entry(devfreq, &devfreq_list, node) {
767 int ret = 0;
768 struct device *dev = devfreq->dev.parent;
769
770 if (!strncmp(devfreq->governor_name, governor->name,
771 DEVFREQ_NAME_LEN)) {
772 /* The following should never occur */
773 if (devfreq->governor) {
774 dev_warn(dev,
775 "%s: Governor %s already present\n",
776 __func__, devfreq->governor->name);
777 ret = devfreq->governor->event_handler(devfreq,
778 DEVFREQ_GOV_STOP, NULL);
779 if (ret) {
780 dev_warn(dev,
781 "%s: Governor %s stop = %d\n",
782 __func__,
783 devfreq->governor->name, ret);
784 }
785 /* Fall through */
786 }
787 devfreq->governor = governor;
788 ret = devfreq->governor->event_handler(devfreq,
789 DEVFREQ_GOV_START, NULL);
790 if (ret) {
791 dev_warn(dev, "%s: Governor %s start=%d\n",
792 __func__, devfreq->governor->name,
793 ret);
794 }
795 }
796 }
797
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500798err_out:
799 mutex_unlock(&devfreq_list_lock);
800
801 return err;
802}
803EXPORT_SYMBOL(devfreq_add_governor);
804
805/**
806 * devfreq_remove_device() - Remove devfreq feature from a device.
807 * @governor: the devfreq governor to be removed
808 */
809int devfreq_remove_governor(struct devfreq_governor *governor)
810{
811 struct devfreq_governor *g;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500812 struct devfreq *devfreq;
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500813 int err = 0;
814
815 if (!governor) {
816 pr_err("%s: Invalid parameters.\n", __func__);
817 return -EINVAL;
818 }
819
820 mutex_lock(&devfreq_list_lock);
821 g = find_devfreq_governor(governor->name);
822 if (IS_ERR(g)) {
823 pr_err("%s: governor %s not registered\n", __func__,
Sachin Kamatb9e1c8e2012-11-21 10:36:13 +0530824 governor->name);
Sachin Kamatf9c08e22012-11-21 10:36:14 +0530825 err = PTR_ERR(g);
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500826 goto err_out;
827 }
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500828 list_for_each_entry(devfreq, &devfreq_list, node) {
829 int ret;
830 struct device *dev = devfreq->dev.parent;
831
832 if (!strncmp(devfreq->governor_name, governor->name,
833 DEVFREQ_NAME_LEN)) {
834 /* we should have a devfreq governor! */
835 if (!devfreq->governor) {
836 dev_warn(dev, "%s: Governor %s NOT present\n",
837 __func__, governor->name);
838 continue;
839 /* Fall through */
840 }
841 ret = devfreq->governor->event_handler(devfreq,
842 DEVFREQ_GOV_STOP, NULL);
843 if (ret) {
844 dev_warn(dev, "%s: Governor %s stop=%d\n",
845 __func__, devfreq->governor->name,
846 ret);
847 }
848 devfreq->governor = NULL;
849 }
850 }
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500851
852 list_del(&governor->node);
853err_out:
854 mutex_unlock(&devfreq_list_lock);
855
856 return err;
857}
858EXPORT_SYMBOL(devfreq_remove_governor);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200859
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700860static ssize_t governor_show(struct device *dev,
MyungJoo Ham9005b652011-10-02 00:19:28 +0200861 struct device_attribute *attr, char *buf)
862{
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500863 if (!to_devfreq(dev)->governor)
864 return -EINVAL;
865
MyungJoo Ham9005b652011-10-02 00:19:28 +0200866 return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
867}
868
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700869static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500870 const char *buf, size_t count)
871{
872 struct devfreq *df = to_devfreq(dev);
873 int ret;
874 char str_governor[DEVFREQ_NAME_LEN + 1];
875 struct devfreq_governor *governor;
876
877 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
878 if (ret != 1)
879 return -EINVAL;
880
881 mutex_lock(&devfreq_list_lock);
882 governor = find_devfreq_governor(str_governor);
883 if (IS_ERR(governor)) {
884 ret = PTR_ERR(governor);
885 goto out;
886 }
Tobias Jakobi14a21e72015-09-21 20:23:52 +0200887 if (df->governor == governor) {
888 ret = 0;
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500889 goto out;
Tobias Jakobi14a21e72015-09-21 20:23:52 +0200890 }
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500891
892 if (df->governor) {
893 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
894 if (ret) {
895 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
896 __func__, df->governor->name, ret);
897 goto out;
898 }
899 }
900 df->governor = governor;
901 strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
902 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
903 if (ret)
904 dev_warn(dev, "%s: Governor %s not started(%d)\n",
905 __func__, df->governor->name, ret);
906out:
907 mutex_unlock(&devfreq_list_lock);
908
909 if (!ret)
910 ret = count;
911 return ret;
912}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700913static DEVICE_ATTR_RW(governor);
914
915static ssize_t available_governors_show(struct device *d,
916 struct device_attribute *attr,
917 char *buf)
Nishanth Menon50a5b332012-10-29 15:01:48 -0500918{
919 struct devfreq_governor *tmp_governor;
920 ssize_t count = 0;
921
922 mutex_lock(&devfreq_list_lock);
923 list_for_each_entry(tmp_governor, &devfreq_governor_list, node)
924 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
925 "%s ", tmp_governor->name);
926 mutex_unlock(&devfreq_list_lock);
927
928 /* Truncate the trailing space */
929 if (count)
930 count--;
931
932 count += sprintf(&buf[count], "\n");
933
934 return count;
935}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700936static DEVICE_ATTR_RO(available_governors);
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500937
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700938static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
939 char *buf)
MyungJoo Ham9005b652011-10-02 00:19:28 +0200940{
Rajagopal Venkat7f98a902012-10-26 01:50:26 +0200941 unsigned long freq;
942 struct devfreq *devfreq = to_devfreq(dev);
943
944 if (devfreq->profile->get_cur_freq &&
945 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
946 return sprintf(buf, "%lu\n", freq);
947
948 return sprintf(buf, "%lu\n", devfreq->previous_freq);
949}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700950static DEVICE_ATTR_RO(cur_freq);
Rajagopal Venkat7f98a902012-10-26 01:50:26 +0200951
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700952static ssize_t target_freq_show(struct device *dev,
953 struct device_attribute *attr, char *buf)
Rajagopal Venkat7f98a902012-10-26 01:50:26 +0200954{
MyungJoo Ham9005b652011-10-02 00:19:28 +0200955 return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
956}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700957static DEVICE_ATTR_RO(target_freq);
MyungJoo Ham9005b652011-10-02 00:19:28 +0200958
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700959static ssize_t polling_interval_show(struct device *dev,
MyungJoo Ham9005b652011-10-02 00:19:28 +0200960 struct device_attribute *attr, char *buf)
961{
962 return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
963}
964
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700965static ssize_t polling_interval_store(struct device *dev,
MyungJoo Ham9005b652011-10-02 00:19:28 +0200966 struct device_attribute *attr,
967 const char *buf, size_t count)
968{
969 struct devfreq *df = to_devfreq(dev);
970 unsigned int value;
971 int ret;
972
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500973 if (!df->governor)
974 return -EINVAL;
975
MyungJoo Ham9005b652011-10-02 00:19:28 +0200976 ret = sscanf(buf, "%u", &value);
977 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +0200978 return -EINVAL;
MyungJoo Ham9005b652011-10-02 00:19:28 +0200979
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200980 df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
MyungJoo Ham9005b652011-10-02 00:19:28 +0200981 ret = count;
982
MyungJoo Ham9005b652011-10-02 00:19:28 +0200983 return ret;
984}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700985static DEVICE_ATTR_RW(polling_interval);
MyungJoo Ham9005b652011-10-02 00:19:28 +0200986
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700987static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
MyungJoo Ham6530b9de2011-12-09 16:42:19 +0900988 const char *buf, size_t count)
989{
990 struct devfreq *df = to_devfreq(dev);
991 unsigned long value;
992 int ret;
993 unsigned long max;
994
995 ret = sscanf(buf, "%lu", &value);
996 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +0200997 return -EINVAL;
MyungJoo Ham6530b9de2011-12-09 16:42:19 +0900998
999 mutex_lock(&df->lock);
1000 max = df->max_freq;
1001 if (value && max && value > max) {
1002 ret = -EINVAL;
1003 goto unlock;
1004 }
1005
1006 df->min_freq = value;
1007 update_devfreq(df);
1008 ret = count;
1009unlock:
1010 mutex_unlock(&df->lock);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001011 return ret;
1012}
1013
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001014static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001015 const char *buf, size_t count)
1016{
1017 struct devfreq *df = to_devfreq(dev);
1018 unsigned long value;
1019 int ret;
1020 unsigned long min;
1021
1022 ret = sscanf(buf, "%lu", &value);
1023 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +02001024 return -EINVAL;
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001025
1026 mutex_lock(&df->lock);
1027 min = df->min_freq;
1028 if (value && min && value < min) {
1029 ret = -EINVAL;
1030 goto unlock;
1031 }
1032
1033 df->max_freq = value;
1034 update_devfreq(df);
1035 ret = count;
1036unlock:
1037 mutex_unlock(&df->lock);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001038 return ret;
1039}
1040
Chanwoo Choi3104fa32015-11-13 19:25:28 +09001041#define show_one(name) \
1042static ssize_t name##_show \
1043(struct device *dev, struct device_attribute *attr, char *buf) \
1044{ \
1045 return sprintf(buf, "%lu\n", to_devfreq(dev)->name); \
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001046}
Chanwoo Choi3104fa32015-11-13 19:25:28 +09001047show_one(min_freq);
1048show_one(max_freq);
1049
1050static DEVICE_ATTR_RW(min_freq);
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001051static DEVICE_ATTR_RW(max_freq);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001052
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001053static ssize_t available_frequencies_show(struct device *d,
1054 struct device_attribute *attr,
1055 char *buf)
Nishanth Menond287de82012-10-25 19:48:59 -05001056{
1057 struct devfreq *df = to_devfreq(d);
1058 struct device *dev = df->dev.parent;
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001059 struct dev_pm_opp *opp;
Nishanth Menond287de82012-10-25 19:48:59 -05001060 ssize_t count = 0;
1061 unsigned long freq = 0;
1062
1063 rcu_read_lock();
1064 do {
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001065 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
Nishanth Menond287de82012-10-25 19:48:59 -05001066 if (IS_ERR(opp))
1067 break;
1068
1069 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1070 "%lu ", freq);
1071 freq++;
1072 } while (1);
1073 rcu_read_unlock();
1074
1075 /* Truncate the trailing space */
1076 if (count)
1077 count--;
1078
1079 count += sprintf(&buf[count], "\n");
1080
1081 return count;
1082}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001083static DEVICE_ATTR_RO(available_frequencies);
Nishanth Menond287de82012-10-25 19:48:59 -05001084
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001085static ssize_t trans_stat_show(struct device *dev,
1086 struct device_attribute *attr, char *buf)
Jonghwa Leee552bba2012-08-23 20:00:46 +09001087{
1088 struct devfreq *devfreq = to_devfreq(dev);
1089 ssize_t len;
Rajagopal Venkat39688ce2013-01-08 11:20:39 +05301090 int i, j;
Jonghwa Leee552bba2012-08-23 20:00:46 +09001091 unsigned int max_state = devfreq->profile->max_state;
1092
Rajagopal Venkat39688ce2013-01-08 11:20:39 +05301093 if (!devfreq->stop_polling &&
1094 devfreq_update_status(devfreq, devfreq->previous_freq))
Jonghwa Leee552bba2012-08-23 20:00:46 +09001095 return 0;
MyungJoo Ham34bd3222015-11-23 15:45:36 +09001096 if (max_state == 0)
1097 return sprintf(buf, "Not Supported.\n");
Jonghwa Leee552bba2012-08-23 20:00:46 +09001098
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001099 len = sprintf(buf, " From : To\n");
1100 len += sprintf(buf + len, " :");
Jonghwa Leee552bba2012-08-23 20:00:46 +09001101 for (i = 0; i < max_state; i++)
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001102 len += sprintf(buf + len, "%10lu",
Jonghwa Leee552bba2012-08-23 20:00:46 +09001103 devfreq->profile->freq_table[i]);
1104
1105 len += sprintf(buf + len, " time(ms)\n");
1106
1107 for (i = 0; i < max_state; i++) {
1108 if (devfreq->profile->freq_table[i]
1109 == devfreq->previous_freq) {
1110 len += sprintf(buf + len, "*");
1111 } else {
1112 len += sprintf(buf + len, " ");
1113 }
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001114 len += sprintf(buf + len, "%10lu:",
Jonghwa Leee552bba2012-08-23 20:00:46 +09001115 devfreq->profile->freq_table[i]);
1116 for (j = 0; j < max_state; j++)
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001117 len += sprintf(buf + len, "%10u",
Jonghwa Leee552bba2012-08-23 20:00:46 +09001118 devfreq->trans_table[(i * max_state) + j]);
1119 len += sprintf(buf + len, "%10u\n",
1120 jiffies_to_msecs(devfreq->time_in_state[i]));
1121 }
1122
1123 len += sprintf(buf + len, "Total transition : %u\n",
1124 devfreq->total_trans);
1125 return len;
1126}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001127static DEVICE_ATTR_RO(trans_stat);
Jonghwa Leee552bba2012-08-23 20:00:46 +09001128
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001129static struct attribute *devfreq_attrs[] = {
1130 &dev_attr_governor.attr,
1131 &dev_attr_available_governors.attr,
1132 &dev_attr_cur_freq.attr,
1133 &dev_attr_available_frequencies.attr,
1134 &dev_attr_target_freq.attr,
1135 &dev_attr_polling_interval.attr,
1136 &dev_attr_min_freq.attr,
1137 &dev_attr_max_freq.attr,
1138 &dev_attr_trans_stat.attr,
1139 NULL,
MyungJoo Ham9005b652011-10-02 00:19:28 +02001140};
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001141ATTRIBUTE_GROUPS(devfreq);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001142
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001143static int __init devfreq_init(void)
1144{
1145 devfreq_class = class_create(THIS_MODULE, "devfreq");
1146 if (IS_ERR(devfreq_class)) {
1147 pr_err("%s: couldn't create class\n", __FILE__);
1148 return PTR_ERR(devfreq_class);
1149 }
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001150
1151 devfreq_wq = create_freezable_workqueue("devfreq_wq");
Dan Carpenterea7f4542013-08-15 10:55:10 +03001152 if (!devfreq_wq) {
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001153 class_destroy(devfreq_class);
1154 pr_err("%s: couldn't create workqueue\n", __FILE__);
Dan Carpenterea7f4542013-08-15 10:55:10 +03001155 return -ENOMEM;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001156 }
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001157 devfreq_class->dev_groups = devfreq_groups;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001158
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001159 return 0;
1160}
1161subsys_initcall(devfreq_init);
1162
1163static void __exit devfreq_exit(void)
1164{
1165 class_destroy(devfreq_class);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001166 destroy_workqueue(devfreq_wq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001167}
1168module_exit(devfreq_exit);
1169
1170/*
1171 * The followings are helper functions for devfreq user device drivers with
1172 * OPP framework.
1173 */
1174
1175/**
1176 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1177 * freq value given to target callback.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001178 * @dev: The devfreq user device. (parent of devfreq)
1179 * @freq: The frequency given to target function
1180 * @flags: Flags handed from devfreq framework.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001181 *
Nishanth Menonbcb27542013-01-18 19:52:34 +00001182 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
1183 * protected pointer. The reason for the same is that the opp pointer which is
1184 * returned will remain valid for use with opp_get_{voltage, freq} only while
1185 * under the locked area. The pointer returned must be used prior to unlocking
1186 * with rcu_read_unlock() to maintain the integrity of the pointer.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001187 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001188struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1189 unsigned long *freq,
1190 u32 flags)
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001191{
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001192 struct dev_pm_opp *opp;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001193
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001194 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1195 /* The freq is an upper bound. opp should be lower */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001196 opp = dev_pm_opp_find_freq_floor(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001197
1198 /* If not available, use the closest opp */
Nishanth Menon07797262012-10-24 22:00:12 +02001199 if (opp == ERR_PTR(-ERANGE))
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001200 opp = dev_pm_opp_find_freq_ceil(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001201 } else {
1202 /* The freq is an lower bound. opp should be higher */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001203 opp = dev_pm_opp_find_freq_ceil(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001204
1205 /* If not available, use the closest opp */
Nishanth Menon07797262012-10-24 22:00:12 +02001206 if (opp == ERR_PTR(-ERANGE))
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001207 opp = dev_pm_opp_find_freq_floor(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001208 }
1209
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001210 return opp;
1211}
Ãrjan Eidebd7e9272014-07-18 15:09:53 +01001212EXPORT_SYMBOL(devfreq_recommended_opp);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001213
1214/**
1215 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1216 * for any changes in the OPP availability
1217 * changes
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001218 * @dev: The devfreq user device. (parent of devfreq)
1219 * @devfreq: The devfreq object.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001220 */
1221int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1222{
MyungJoo Ham389baed2012-11-21 19:04:51 +09001223 struct srcu_notifier_head *nh;
1224 int ret = 0;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001225
MyungJoo Ham389baed2012-11-21 19:04:51 +09001226 rcu_read_lock();
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001227 nh = dev_pm_opp_get_notifier(dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001228 if (IS_ERR(nh))
MyungJoo Ham389baed2012-11-21 19:04:51 +09001229 ret = PTR_ERR(nh);
1230 rcu_read_unlock();
1231 if (!ret)
1232 ret = srcu_notifier_chain_register(nh, &devfreq->nb);
1233
1234 return ret;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001235}
Ãrjan Eidebd7e9272014-07-18 15:09:53 +01001236EXPORT_SYMBOL(devfreq_register_opp_notifier);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001237
1238/**
1239 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1240 * notified for any changes in the OPP
1241 * availability changes anymore.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001242 * @dev: The devfreq user device. (parent of devfreq)
1243 * @devfreq: The devfreq object.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001244 *
1245 * At exit() callback of devfreq_dev_profile, this must be included if
1246 * devfreq_recommended_opp is used.
1247 */
1248int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1249{
MyungJoo Ham389baed2012-11-21 19:04:51 +09001250 struct srcu_notifier_head *nh;
1251 int ret = 0;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001252
MyungJoo Ham389baed2012-11-21 19:04:51 +09001253 rcu_read_lock();
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001254 nh = dev_pm_opp_get_notifier(dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001255 if (IS_ERR(nh))
MyungJoo Ham389baed2012-11-21 19:04:51 +09001256 ret = PTR_ERR(nh);
1257 rcu_read_unlock();
1258 if (!ret)
1259 ret = srcu_notifier_chain_unregister(nh, &devfreq->nb);
1260
1261 return ret;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001262}
Ãrjan Eidebd7e9272014-07-18 15:09:53 +01001263EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001264
Chanwoo Choid5b040d2014-05-09 16:43:09 +09001265static void devm_devfreq_opp_release(struct device *dev, void *res)
1266{
1267 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
1268}
1269
1270/**
1271 * devm_ devfreq_register_opp_notifier()
1272 * - Resource-managed devfreq_register_opp_notifier()
1273 * @dev: The devfreq user device. (parent of devfreq)
1274 * @devfreq: The devfreq object.
1275 */
1276int devm_devfreq_register_opp_notifier(struct device *dev,
1277 struct devfreq *devfreq)
1278{
1279 struct devfreq **ptr;
1280 int ret;
1281
1282 ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
1283 if (!ptr)
1284 return -ENOMEM;
1285
1286 ret = devfreq_register_opp_notifier(dev, devfreq);
1287 if (ret) {
1288 devres_free(ptr);
1289 return ret;
1290 }
1291
1292 *ptr = devfreq;
1293 devres_add(dev, ptr);
1294
1295 return 0;
1296}
1297EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
1298
1299/**
1300 * devm_devfreq_unregister_opp_notifier()
1301 * - Resource-managed devfreq_unregister_opp_notifier()
1302 * @dev: The devfreq user device. (parent of devfreq)
1303 * @devfreq: The devfreq object.
1304 */
1305void devm_devfreq_unregister_opp_notifier(struct device *dev,
1306 struct devfreq *devfreq)
1307{
1308 WARN_ON(devres_release(dev, devm_devfreq_opp_release,
1309 devm_devfreq_dev_match, devfreq));
1310}
1311EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
1312
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001313MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1314MODULE_DESCRIPTION("devfreq class support");
1315MODULE_LICENSE("GPL");