blob: 253525ea17afabef1b61c36d06969aa82a3dd7e9 [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>
Paul Gortmaker417dc4b2016-06-26 03:43:47 +090018#include <linux/export.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
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900114 for (i = 0, freq = 0; i < profile->max_state; i++, freq++) {
115 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq);
116 if (IS_ERR(opp)) {
117 devm_kfree(devfreq->dev.parent, profile->freq_table);
118 profile->max_state = 0;
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900119 return;
120 }
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530121 dev_pm_opp_put(opp);
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900122 profile->freq_table[i] = freq;
123 }
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900124}
125
126/**
Jonghwa Leee552bba2012-08-23 20:00:46 +0900127 * devfreq_update_status() - Update statistics of devfreq behavior
128 * @devfreq: the devfreq instance
129 * @freq: the update target frequency
130 */
131static int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
132{
Saravana Kannane35d35a2014-02-27 19:38:57 -0800133 int lev, prev_lev, ret = 0;
Jonghwa Leee552bba2012-08-23 20:00:46 +0900134 unsigned long cur_time;
135
Jonghwa Leee552bba2012-08-23 20:00:46 +0900136 cur_time = jiffies;
Saravana Kannane35d35a2014-02-27 19:38:57 -0800137
Tobias Jakobid0563a02016-09-29 14:36:36 +0200138 /* Immediately exit if previous_freq is not initialized yet. */
139 if (!devfreq->previous_freq)
140 goto out;
141
Saravana Kannane35d35a2014-02-27 19:38:57 -0800142 prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
143 if (prev_lev < 0) {
144 ret = prev_lev;
145 goto out;
146 }
147
148 devfreq->time_in_state[prev_lev] +=
Jonghwa Leee552bba2012-08-23 20:00:46 +0900149 cur_time - devfreq->last_stat_updated;
Saravana Kannane35d35a2014-02-27 19:38:57 -0800150
151 lev = devfreq_get_freq_level(devfreq, freq);
152 if (lev < 0) {
153 ret = lev;
154 goto out;
155 }
156
157 if (lev != prev_lev) {
Jonghwa Leee552bba2012-08-23 20:00:46 +0900158 devfreq->trans_table[(prev_lev *
159 devfreq->profile->max_state) + lev]++;
160 devfreq->total_trans++;
161 }
Jonghwa Leee552bba2012-08-23 20:00:46 +0900162
Saravana Kannane35d35a2014-02-27 19:38:57 -0800163out:
164 devfreq->last_stat_updated = cur_time;
165 return ret;
Jonghwa Leee552bba2012-08-23 20:00:46 +0900166}
167
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500168/**
169 * find_devfreq_governor() - find devfreq governor from name
170 * @name: name of the governor
171 *
172 * Search the list of devfreq governors and return the matched
173 * governor's pointer. devfreq_list_lock should be held by the caller.
174 */
175static struct devfreq_governor *find_devfreq_governor(const char *name)
176{
177 struct devfreq_governor *tmp_governor;
178
Viresh Kumar9348da22015-08-10 11:42:25 +0530179 if (IS_ERR_OR_NULL(name)) {
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500180 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
181 return ERR_PTR(-EINVAL);
182 }
183 WARN(!mutex_is_locked(&devfreq_list_lock),
184 "devfreq_list_lock must be locked.");
185
186 list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
187 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
188 return tmp_governor;
189 }
190
191 return ERR_PTR(-ENODEV);
192}
193
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900194static int devfreq_notify_transition(struct devfreq *devfreq,
195 struct devfreq_freqs *freqs, unsigned int state)
196{
197 if (!devfreq)
198 return -EINVAL;
199
200 switch (state) {
201 case DEVFREQ_PRECHANGE:
202 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
203 DEVFREQ_PRECHANGE, freqs);
204 break;
205
206 case DEVFREQ_POSTCHANGE:
207 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
208 DEVFREQ_POSTCHANGE, freqs);
209 break;
210 default:
211 return -EINVAL;
212 }
213
214 return 0;
215}
216
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200217/* Load monitoring helper functions for governors use */
218
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200219/**
220 * update_devfreq() - Reevaluate the device and configure frequency.
221 * @devfreq: the devfreq instance.
222 *
223 * Note: Lock devfreq->lock before calling update_devfreq
224 * This function is exported for governors.
225 */
226int update_devfreq(struct devfreq *devfreq)
227{
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900228 struct devfreq_freqs freqs;
229 unsigned long freq, cur_freq;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200230 int err = 0;
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100231 u32 flags = 0;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200232
233 if (!mutex_is_locked(&devfreq->lock)) {
234 WARN(true, "devfreq->lock must be locked by the caller.\n");
235 return -EINVAL;
236 }
237
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500238 if (!devfreq->governor)
239 return -EINVAL;
240
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200241 /* Reevaluate the proper frequency */
242 err = devfreq->governor->get_target_freq(devfreq, &freq);
243 if (err)
244 return err;
245
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100246 /*
Javi Merinod3b7e172015-08-14 18:57:00 +0100247 * Adjust the frequency with user freq and QoS.
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100248 *
Javi Merinod3b7e172015-08-14 18:57:00 +0100249 * List from the highest priority
250 * max_freq
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100251 * min_freq
252 */
253
254 if (devfreq->min_freq && freq < devfreq->min_freq) {
255 freq = devfreq->min_freq;
256 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
257 }
258 if (devfreq->max_freq && freq > devfreq->max_freq) {
259 freq = devfreq->max_freq;
260 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
261 }
262
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900263 if (devfreq->profile->get_cur_freq)
264 devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
265 else
266 cur_freq = devfreq->previous_freq;
267
268 freqs.old = cur_freq;
269 freqs.new = freq;
270 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
271
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100272 err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
Chanwoo Choi0d371892016-06-23 11:18:43 +0900273 if (err) {
274 freqs.new = cur_freq;
275 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200276 return err;
Chanwoo Choi0d371892016-06-23 11:18:43 +0900277 }
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200278
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900279 freqs.new = freq;
280 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
281
Jonghwa Leee552bba2012-08-23 20:00:46 +0900282 if (devfreq->profile->freq_table)
283 if (devfreq_update_status(devfreq, freq))
284 dev_err(&devfreq->dev,
285 "Couldn't update frequency transition information.\n");
286
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200287 devfreq->previous_freq = freq;
288 return err;
289}
Nishanth Menon2df50212012-10-29 15:01:42 -0500290EXPORT_SYMBOL(update_devfreq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200291
292/**
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200293 * devfreq_monitor() - Periodically poll devfreq objects.
294 * @work: the work struct used to run devfreq_monitor periodically.
295 *
296 */
297static void devfreq_monitor(struct work_struct *work)
298{
299 int err;
300 struct devfreq *devfreq = container_of(work,
301 struct devfreq, work.work);
302
303 mutex_lock(&devfreq->lock);
304 err = update_devfreq(devfreq);
305 if (err)
306 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
307
308 queue_delayed_work(devfreq_wq, &devfreq->work,
309 msecs_to_jiffies(devfreq->profile->polling_ms));
310 mutex_unlock(&devfreq->lock);
311}
312
313/**
314 * devfreq_monitor_start() - Start load monitoring of devfreq instance
315 * @devfreq: the devfreq instance.
316 *
317 * Helper function for starting devfreq device load monitoing. By
318 * default delayed work based monitoring is supported. Function
319 * to be called from governor in response to DEVFREQ_GOV_START
320 * event when device is added to devfreq framework.
321 */
322void devfreq_monitor_start(struct devfreq *devfreq)
323{
324 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
325 if (devfreq->profile->polling_ms)
326 queue_delayed_work(devfreq_wq, &devfreq->work,
327 msecs_to_jiffies(devfreq->profile->polling_ms));
328}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100329EXPORT_SYMBOL(devfreq_monitor_start);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200330
331/**
332 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
333 * @devfreq: the devfreq instance.
334 *
335 * Helper function to stop devfreq device load monitoing. Function
336 * to be called from governor in response to DEVFREQ_GOV_STOP
337 * event when device is removed from devfreq framework.
338 */
339void devfreq_monitor_stop(struct devfreq *devfreq)
340{
341 cancel_delayed_work_sync(&devfreq->work);
342}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100343EXPORT_SYMBOL(devfreq_monitor_stop);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200344
345/**
346 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
347 * @devfreq: the devfreq instance.
348 *
349 * Helper function to suspend devfreq device load monitoing. Function
350 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
351 * event or when polling interval is set to zero.
352 *
353 * Note: Though this function is same as devfreq_monitor_stop(),
354 * intentionally kept separate to provide hooks for collecting
355 * transition statistics.
356 */
357void devfreq_monitor_suspend(struct devfreq *devfreq)
358{
359 mutex_lock(&devfreq->lock);
360 if (devfreq->stop_polling) {
361 mutex_unlock(&devfreq->lock);
362 return;
363 }
364
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530365 devfreq_update_status(devfreq, devfreq->previous_freq);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200366 devfreq->stop_polling = true;
367 mutex_unlock(&devfreq->lock);
368 cancel_delayed_work_sync(&devfreq->work);
369}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100370EXPORT_SYMBOL(devfreq_monitor_suspend);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200371
372/**
373 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
374 * @devfreq: the devfreq instance.
375 *
376 * Helper function to resume devfreq device load monitoing. Function
377 * to be called from governor in response to DEVFREQ_GOV_RESUME
378 * event or when polling interval is set to non-zero.
379 */
380void devfreq_monitor_resume(struct devfreq *devfreq)
381{
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530382 unsigned long freq;
383
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200384 mutex_lock(&devfreq->lock);
385 if (!devfreq->stop_polling)
386 goto out;
387
388 if (!delayed_work_pending(&devfreq->work) &&
389 devfreq->profile->polling_ms)
390 queue_delayed_work(devfreq_wq, &devfreq->work,
391 msecs_to_jiffies(devfreq->profile->polling_ms));
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530392
393 devfreq->last_stat_updated = jiffies;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200394 devfreq->stop_polling = false;
395
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530396 if (devfreq->profile->get_cur_freq &&
397 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
398 devfreq->previous_freq = freq;
399
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200400out:
401 mutex_unlock(&devfreq->lock);
402}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100403EXPORT_SYMBOL(devfreq_monitor_resume);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200404
405/**
406 * devfreq_interval_update() - Update device devfreq monitoring interval
407 * @devfreq: the devfreq instance.
408 * @delay: new polling interval to be set.
409 *
410 * Helper function to set new load monitoring polling interval. Function
411 * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
412 */
413void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
414{
415 unsigned int cur_delay = devfreq->profile->polling_ms;
416 unsigned int new_delay = *delay;
417
418 mutex_lock(&devfreq->lock);
419 devfreq->profile->polling_ms = new_delay;
420
421 if (devfreq->stop_polling)
422 goto out;
423
424 /* if new delay is zero, stop polling */
425 if (!new_delay) {
426 mutex_unlock(&devfreq->lock);
427 cancel_delayed_work_sync(&devfreq->work);
428 return;
429 }
430
431 /* if current delay is zero, start polling with new delay */
432 if (!cur_delay) {
433 queue_delayed_work(devfreq_wq, &devfreq->work,
434 msecs_to_jiffies(devfreq->profile->polling_ms));
435 goto out;
436 }
437
438 /* if current delay is greater than new delay, restart polling */
439 if (cur_delay > new_delay) {
440 mutex_unlock(&devfreq->lock);
441 cancel_delayed_work_sync(&devfreq->work);
442 mutex_lock(&devfreq->lock);
443 if (!devfreq->stop_polling)
444 queue_delayed_work(devfreq_wq, &devfreq->work,
445 msecs_to_jiffies(devfreq->profile->polling_ms));
446 }
447out:
448 mutex_unlock(&devfreq->lock);
449}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100450EXPORT_SYMBOL(devfreq_interval_update);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200451
452/**
453 * devfreq_notifier_call() - Notify that the device frequency requirements
454 * has been changed out of devfreq framework.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +0200455 * @nb: the notifier_block (supposed to be devfreq->nb)
456 * @type: not used
457 * @devp: not used
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200458 *
459 * Called by a notifier that uses devfreq->nb.
460 */
461static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
462 void *devp)
463{
464 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
465 int ret;
466
467 mutex_lock(&devfreq->lock);
468 ret = update_devfreq(devfreq);
469 mutex_unlock(&devfreq->lock);
470
471 return ret;
472}
473
474/**
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200475 * _remove_devfreq() - Remove devfreq from the list and release its resources.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200476 * @devfreq: the devfreq struct
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200477 */
Chanwoo Choi585fc832014-05-09 16:43:07 +0900478static void _remove_devfreq(struct devfreq *devfreq)
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200479{
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200480 mutex_lock(&devfreq_list_lock);
481 if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
482 mutex_unlock(&devfreq_list_lock);
483 dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200484 return;
485 }
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200486 list_del(&devfreq->node);
487 mutex_unlock(&devfreq_list_lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200488
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500489 if (devfreq->governor)
490 devfreq->governor->event_handler(devfreq,
491 DEVFREQ_GOV_STOP, NULL);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200492
493 if (devfreq->profile->exit)
494 devfreq->profile->exit(devfreq->dev.parent);
495
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200496 mutex_destroy(&devfreq->lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200497 kfree(devfreq);
498}
499
500/**
501 * devfreq_dev_release() - Callback for struct device to release the device.
502 * @dev: the devfreq device
503 *
504 * This calls _remove_devfreq() if _remove_devfreq() is not called.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200505 */
506static void devfreq_dev_release(struct device *dev)
507{
508 struct devfreq *devfreq = to_devfreq(dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200509
Chanwoo Choi585fc832014-05-09 16:43:07 +0900510 _remove_devfreq(devfreq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200511}
512
513/**
514 * devfreq_add_device() - Add devfreq feature to the device
515 * @dev: the device to add devfreq feature.
516 * @profile: device-specific profile to run devfreq.
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500517 * @governor_name: name of the policy to choose frequency.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200518 * @data: private data for the governor. The devfreq framework does not
519 * touch this value.
520 */
521struct devfreq *devfreq_add_device(struct device *dev,
522 struct devfreq_dev_profile *profile,
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500523 const char *governor_name,
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200524 void *data)
525{
526 struct devfreq *devfreq;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500527 struct devfreq_governor *governor;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200528 int err = 0;
529
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500530 if (!dev || !profile || !governor_name) {
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200531 dev_err(dev, "%s: Invalid parameters.\n", __func__);
532 return ERR_PTR(-EINVAL);
533 }
534
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200535 mutex_lock(&devfreq_list_lock);
536 devfreq = find_device_devfreq(dev);
537 mutex_unlock(&devfreq_list_lock);
538 if (!IS_ERR(devfreq)) {
539 dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__);
540 err = -EINVAL;
541 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200542 }
543
544 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
545 if (!devfreq) {
546 dev_err(dev, "%s: Unable to create devfreq for the device\n",
547 __func__);
548 err = -ENOMEM;
Axel Lin3f19f082011-11-15 21:59:09 +0100549 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200550 }
551
552 mutex_init(&devfreq->lock);
553 mutex_lock(&devfreq->lock);
554 devfreq->dev.parent = dev;
555 devfreq->dev.class = devfreq_class;
556 devfreq->dev.release = devfreq_dev_release;
557 devfreq->profile = profile;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500558 strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200559 devfreq->previous_freq = profile->initial_freq;
Lukasz Luba8d39fc082016-05-31 11:25:09 +0100560 devfreq->last_status.current_frequency = profile->initial_freq;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200561 devfreq->data = data;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200562 devfreq->nb.notifier_call = devfreq_notifier_call;
563
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900564 if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
565 mutex_unlock(&devfreq->lock);
566 devfreq_set_freq_table(devfreq);
567 mutex_lock(&devfreq->lock);
568 }
569
Kees Cook02aa2a32013-07-03 15:04:56 -0700570 dev_set_name(&devfreq->dev, "%s", dev_name(dev));
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200571 err = device_register(&devfreq->dev);
572 if (err) {
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200573 mutex_unlock(&devfreq->lock);
Geliang Tang6d3cbfa2015-10-01 22:18:19 +0800574 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200575 }
576
MyungJoo Ham3e1d7fb2015-10-02 12:39:23 +0900577 devfreq->trans_table = devm_kzalloc(&devfreq->dev, sizeof(unsigned int) *
578 devfreq->profile->max_state *
579 devfreq->profile->max_state,
580 GFP_KERNEL);
581 devfreq->time_in_state = devm_kzalloc(&devfreq->dev, sizeof(unsigned long) *
582 devfreq->profile->max_state,
583 GFP_KERNEL);
584 devfreq->last_stat_updated = jiffies;
585
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900586 srcu_init_notifier_head(&devfreq->transition_notifier_list);
587
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200588 mutex_unlock(&devfreq->lock);
589
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200590 mutex_lock(&devfreq_list_lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200591 list_add(&devfreq->node, &devfreq_list);
592
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500593 governor = find_devfreq_governor(devfreq->governor_name);
Chanwoo Choi73613b12016-12-28 20:52:35 +0900594 if (IS_ERR(governor)) {
595 dev_err(dev, "%s: Unable to find governor for the device\n",
596 __func__);
597 err = PTR_ERR(governor);
598 goto err_init;
599 }
600
601 devfreq->governor = governor;
602 err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
603 NULL);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200604 if (err) {
605 dev_err(dev, "%s: Unable to start governor for the device\n",
606 __func__);
607 goto err_init;
608 }
Axel Lin0f376c92016-09-29 10:13:28 +0800609 mutex_unlock(&devfreq_list_lock);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200610
Axel Lin3f19f082011-11-15 21:59:09 +0100611 return devfreq;
612
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200613err_init:
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200614 list_del(&devfreq->node);
Axel Lin0f376c92016-09-29 10:13:28 +0800615 mutex_unlock(&devfreq_list_lock);
616
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200617 device_unregister(&devfreq->dev);
Axel Lin3f19f082011-11-15 21:59:09 +0100618err_out:
619 return ERR_PTR(err);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200620}
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200621EXPORT_SYMBOL(devfreq_add_device);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200622
623/**
624 * devfreq_remove_device() - Remove devfreq feature from a device.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +0200625 * @devfreq: the devfreq instance to be removed
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900626 *
627 * The opposite of devfreq_add_device().
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200628 */
629int devfreq_remove_device(struct devfreq *devfreq)
630{
631 if (!devfreq)
632 return -EINVAL;
633
Chanwoo Choi585fc832014-05-09 16:43:07 +0900634 device_unregister(&devfreq->dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200635
636 return 0;
637}
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200638EXPORT_SYMBOL(devfreq_remove_device);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200639
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900640static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
641{
642 struct devfreq **r = res;
643
644 if (WARN_ON(!r || !*r))
645 return 0;
646
647 return *r == data;
648}
649
650static void devm_devfreq_dev_release(struct device *dev, void *res)
651{
652 devfreq_remove_device(*(struct devfreq **)res);
653}
654
655/**
656 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
657 * @dev: the device to add devfreq feature.
658 * @profile: device-specific profile to run devfreq.
659 * @governor_name: name of the policy to choose frequency.
660 * @data: private data for the governor. The devfreq framework does not
661 * touch this value.
662 *
663 * This function manages automatically the memory of devfreq device using device
664 * resource management and simplify the free operation for memory of devfreq
665 * device.
666 */
667struct devfreq *devm_devfreq_add_device(struct device *dev,
668 struct devfreq_dev_profile *profile,
669 const char *governor_name,
670 void *data)
671{
672 struct devfreq **ptr, *devfreq;
673
674 ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
675 if (!ptr)
676 return ERR_PTR(-ENOMEM);
677
678 devfreq = devfreq_add_device(dev, profile, governor_name, data);
679 if (IS_ERR(devfreq)) {
680 devres_free(ptr);
681 return ERR_PTR(-ENOMEM);
682 }
683
684 *ptr = devfreq;
685 devres_add(dev, ptr);
686
687 return devfreq;
688}
689EXPORT_SYMBOL(devm_devfreq_add_device);
690
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900691#ifdef CONFIG_OF
692/*
693 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
694 * @dev - instance to the given device
695 * @index - index into list of devfreq
696 *
697 * return the instance of devfreq device
698 */
699struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
700{
701 struct device_node *node;
702 struct devfreq *devfreq;
703
704 if (!dev)
705 return ERR_PTR(-EINVAL);
706
707 if (!dev->of_node)
708 return ERR_PTR(-EINVAL);
709
710 node = of_parse_phandle(dev->of_node, "devfreq", index);
711 if (!node)
712 return ERR_PTR(-ENODEV);
713
714 mutex_lock(&devfreq_list_lock);
715 list_for_each_entry(devfreq, &devfreq_list, node) {
716 if (devfreq->dev.parent
717 && devfreq->dev.parent->of_node == node) {
718 mutex_unlock(&devfreq_list_lock);
Peter Chen3427c6f2016-07-01 17:42:00 +0800719 of_node_put(node);
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900720 return devfreq;
721 }
722 }
723 mutex_unlock(&devfreq_list_lock);
Peter Chen3427c6f2016-07-01 17:42:00 +0800724 of_node_put(node);
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900725
726 return ERR_PTR(-EPROBE_DEFER);
727}
728#else
729struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
730{
731 return ERR_PTR(-ENODEV);
732}
733#endif /* CONFIG_OF */
734EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
735
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900736/**
737 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
738 * @dev: the device to add devfreq feature.
739 * @devfreq: the devfreq instance to be removed
740 */
741void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
742{
743 WARN_ON(devres_release(dev, devm_devfreq_dev_release,
744 devm_devfreq_dev_match, devfreq));
745}
746EXPORT_SYMBOL(devm_devfreq_remove_device);
747
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200748/**
749 * devfreq_suspend_device() - Suspend devfreq of a device.
750 * @devfreq: the devfreq instance to be suspended
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900751 *
752 * This function is intended to be called by the pm callbacks
753 * (e.g., runtime_suspend, suspend) of the device driver that
754 * holds the devfreq.
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200755 */
756int devfreq_suspend_device(struct devfreq *devfreq)
757{
758 if (!devfreq)
759 return -EINVAL;
760
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500761 if (!devfreq->governor)
762 return 0;
763
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200764 return devfreq->governor->event_handler(devfreq,
765 DEVFREQ_GOV_SUSPEND, NULL);
766}
767EXPORT_SYMBOL(devfreq_suspend_device);
768
769/**
770 * devfreq_resume_device() - Resume devfreq of a device.
771 * @devfreq: the devfreq instance to be resumed
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900772 *
773 * This function is intended to be called by the pm callbacks
774 * (e.g., runtime_resume, resume) of the device driver that
775 * holds the devfreq.
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200776 */
777int devfreq_resume_device(struct devfreq *devfreq)
778{
779 if (!devfreq)
780 return -EINVAL;
781
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500782 if (!devfreq->governor)
783 return 0;
784
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200785 return devfreq->governor->event_handler(devfreq,
786 DEVFREQ_GOV_RESUME, NULL);
787}
788EXPORT_SYMBOL(devfreq_resume_device);
789
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500790/**
791 * devfreq_add_governor() - Add devfreq governor
792 * @governor: the devfreq governor to be added
793 */
794int devfreq_add_governor(struct devfreq_governor *governor)
795{
796 struct devfreq_governor *g;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500797 struct devfreq *devfreq;
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500798 int err = 0;
799
800 if (!governor) {
801 pr_err("%s: Invalid parameters.\n", __func__);
802 return -EINVAL;
803 }
804
805 mutex_lock(&devfreq_list_lock);
806 g = find_devfreq_governor(governor->name);
807 if (!IS_ERR(g)) {
808 pr_err("%s: governor %s already registered\n", __func__,
809 g->name);
810 err = -EINVAL;
811 goto err_out;
812 }
813
814 list_add(&governor->node, &devfreq_governor_list);
815
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500816 list_for_each_entry(devfreq, &devfreq_list, node) {
817 int ret = 0;
818 struct device *dev = devfreq->dev.parent;
819
820 if (!strncmp(devfreq->governor_name, governor->name,
821 DEVFREQ_NAME_LEN)) {
822 /* The following should never occur */
823 if (devfreq->governor) {
824 dev_warn(dev,
825 "%s: Governor %s already present\n",
826 __func__, devfreq->governor->name);
827 ret = devfreq->governor->event_handler(devfreq,
828 DEVFREQ_GOV_STOP, NULL);
829 if (ret) {
830 dev_warn(dev,
831 "%s: Governor %s stop = %d\n",
832 __func__,
833 devfreq->governor->name, ret);
834 }
835 /* Fall through */
836 }
837 devfreq->governor = governor;
838 ret = devfreq->governor->event_handler(devfreq,
839 DEVFREQ_GOV_START, NULL);
840 if (ret) {
841 dev_warn(dev, "%s: Governor %s start=%d\n",
842 __func__, devfreq->governor->name,
843 ret);
844 }
845 }
846 }
847
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500848err_out:
849 mutex_unlock(&devfreq_list_lock);
850
851 return err;
852}
853EXPORT_SYMBOL(devfreq_add_governor);
854
855/**
MyungJoo Hambafeb422016-11-09 10:29:14 +0900856 * devfreq_remove_governor() - Remove devfreq feature from a device.
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500857 * @governor: the devfreq governor to be removed
858 */
859int devfreq_remove_governor(struct devfreq_governor *governor)
860{
861 struct devfreq_governor *g;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500862 struct devfreq *devfreq;
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500863 int err = 0;
864
865 if (!governor) {
866 pr_err("%s: Invalid parameters.\n", __func__);
867 return -EINVAL;
868 }
869
870 mutex_lock(&devfreq_list_lock);
871 g = find_devfreq_governor(governor->name);
872 if (IS_ERR(g)) {
873 pr_err("%s: governor %s not registered\n", __func__,
Sachin Kamatb9e1c8e2012-11-21 10:36:13 +0530874 governor->name);
Sachin Kamatf9c08e22012-11-21 10:36:14 +0530875 err = PTR_ERR(g);
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500876 goto err_out;
877 }
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500878 list_for_each_entry(devfreq, &devfreq_list, node) {
879 int ret;
880 struct device *dev = devfreq->dev.parent;
881
882 if (!strncmp(devfreq->governor_name, governor->name,
883 DEVFREQ_NAME_LEN)) {
884 /* we should have a devfreq governor! */
885 if (!devfreq->governor) {
886 dev_warn(dev, "%s: Governor %s NOT present\n",
887 __func__, governor->name);
888 continue;
889 /* Fall through */
890 }
891 ret = devfreq->governor->event_handler(devfreq,
892 DEVFREQ_GOV_STOP, NULL);
893 if (ret) {
894 dev_warn(dev, "%s: Governor %s stop=%d\n",
895 __func__, devfreq->governor->name,
896 ret);
897 }
898 devfreq->governor = NULL;
899 }
900 }
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500901
902 list_del(&governor->node);
903err_out:
904 mutex_unlock(&devfreq_list_lock);
905
906 return err;
907}
908EXPORT_SYMBOL(devfreq_remove_governor);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200909
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700910static ssize_t governor_show(struct device *dev,
MyungJoo Ham9005b652011-10-02 00:19:28 +0200911 struct device_attribute *attr, char *buf)
912{
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500913 if (!to_devfreq(dev)->governor)
914 return -EINVAL;
915
MyungJoo Ham9005b652011-10-02 00:19:28 +0200916 return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
917}
918
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700919static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500920 const char *buf, size_t count)
921{
922 struct devfreq *df = to_devfreq(dev);
923 int ret;
924 char str_governor[DEVFREQ_NAME_LEN + 1];
925 struct devfreq_governor *governor;
926
927 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
928 if (ret != 1)
929 return -EINVAL;
930
931 mutex_lock(&devfreq_list_lock);
932 governor = find_devfreq_governor(str_governor);
933 if (IS_ERR(governor)) {
934 ret = PTR_ERR(governor);
935 goto out;
936 }
Tobias Jakobi14a21e72015-09-21 20:23:52 +0200937 if (df->governor == governor) {
938 ret = 0;
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500939 goto out;
Tobias Jakobi14a21e72015-09-21 20:23:52 +0200940 }
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500941
942 if (df->governor) {
943 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
944 if (ret) {
945 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
946 __func__, df->governor->name, ret);
947 goto out;
948 }
949 }
950 df->governor = governor;
951 strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
952 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
953 if (ret)
954 dev_warn(dev, "%s: Governor %s not started(%d)\n",
955 __func__, df->governor->name, ret);
956out:
957 mutex_unlock(&devfreq_list_lock);
958
959 if (!ret)
960 ret = count;
961 return ret;
962}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700963static DEVICE_ATTR_RW(governor);
964
965static ssize_t available_governors_show(struct device *d,
966 struct device_attribute *attr,
967 char *buf)
Nishanth Menon50a5b332012-10-29 15:01:48 -0500968{
969 struct devfreq_governor *tmp_governor;
970 ssize_t count = 0;
971
972 mutex_lock(&devfreq_list_lock);
973 list_for_each_entry(tmp_governor, &devfreq_governor_list, node)
974 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
975 "%s ", tmp_governor->name);
976 mutex_unlock(&devfreq_list_lock);
977
978 /* Truncate the trailing space */
979 if (count)
980 count--;
981
982 count += sprintf(&buf[count], "\n");
983
984 return count;
985}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700986static DEVICE_ATTR_RO(available_governors);
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500987
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700988static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
989 char *buf)
MyungJoo Ham9005b652011-10-02 00:19:28 +0200990{
Rajagopal Venkat7f98a902012-10-26 01:50:26 +0200991 unsigned long freq;
992 struct devfreq *devfreq = to_devfreq(dev);
993
994 if (devfreq->profile->get_cur_freq &&
995 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
996 return sprintf(buf, "%lu\n", freq);
997
998 return sprintf(buf, "%lu\n", devfreq->previous_freq);
999}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001000static DEVICE_ATTR_RO(cur_freq);
Rajagopal Venkat7f98a902012-10-26 01:50:26 +02001001
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001002static ssize_t target_freq_show(struct device *dev,
1003 struct device_attribute *attr, char *buf)
Rajagopal Venkat7f98a902012-10-26 01:50:26 +02001004{
MyungJoo Ham9005b652011-10-02 00:19:28 +02001005 return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
1006}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001007static DEVICE_ATTR_RO(target_freq);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001008
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001009static ssize_t polling_interval_show(struct device *dev,
MyungJoo Ham9005b652011-10-02 00:19:28 +02001010 struct device_attribute *attr, char *buf)
1011{
1012 return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
1013}
1014
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001015static ssize_t polling_interval_store(struct device *dev,
MyungJoo Ham9005b652011-10-02 00:19:28 +02001016 struct device_attribute *attr,
1017 const char *buf, size_t count)
1018{
1019 struct devfreq *df = to_devfreq(dev);
1020 unsigned int value;
1021 int ret;
1022
Nishanth Menon1b5c1be2012-10-29 15:01:45 -05001023 if (!df->governor)
1024 return -EINVAL;
1025
MyungJoo Ham9005b652011-10-02 00:19:28 +02001026 ret = sscanf(buf, "%u", &value);
1027 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +02001028 return -EINVAL;
MyungJoo Ham9005b652011-10-02 00:19:28 +02001029
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001030 df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001031 ret = count;
1032
MyungJoo Ham9005b652011-10-02 00:19:28 +02001033 return ret;
1034}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001035static DEVICE_ATTR_RW(polling_interval);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001036
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001037static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001038 const char *buf, size_t count)
1039{
1040 struct devfreq *df = to_devfreq(dev);
1041 unsigned long value;
1042 int ret;
1043 unsigned long max;
1044
1045 ret = sscanf(buf, "%lu", &value);
1046 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +02001047 return -EINVAL;
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001048
1049 mutex_lock(&df->lock);
1050 max = df->max_freq;
1051 if (value && max && value > max) {
1052 ret = -EINVAL;
1053 goto unlock;
1054 }
1055
1056 df->min_freq = value;
1057 update_devfreq(df);
1058 ret = count;
1059unlock:
1060 mutex_unlock(&df->lock);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001061 return ret;
1062}
1063
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001064static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001065 const char *buf, size_t count)
1066{
1067 struct devfreq *df = to_devfreq(dev);
1068 unsigned long value;
1069 int ret;
1070 unsigned long min;
1071
1072 ret = sscanf(buf, "%lu", &value);
1073 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +02001074 return -EINVAL;
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001075
1076 mutex_lock(&df->lock);
1077 min = df->min_freq;
1078 if (value && min && value < min) {
1079 ret = -EINVAL;
1080 goto unlock;
1081 }
1082
1083 df->max_freq = value;
1084 update_devfreq(df);
1085 ret = count;
1086unlock:
1087 mutex_unlock(&df->lock);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001088 return ret;
1089}
1090
Chanwoo Choi3104fa32015-11-13 19:25:28 +09001091#define show_one(name) \
1092static ssize_t name##_show \
1093(struct device *dev, struct device_attribute *attr, char *buf) \
1094{ \
1095 return sprintf(buf, "%lu\n", to_devfreq(dev)->name); \
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001096}
Chanwoo Choi3104fa32015-11-13 19:25:28 +09001097show_one(min_freq);
1098show_one(max_freq);
1099
1100static DEVICE_ATTR_RW(min_freq);
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001101static DEVICE_ATTR_RW(max_freq);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001102
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001103static ssize_t available_frequencies_show(struct device *d,
1104 struct device_attribute *attr,
1105 char *buf)
Nishanth Menond287de82012-10-25 19:48:59 -05001106{
1107 struct devfreq *df = to_devfreq(d);
1108 struct device *dev = df->dev.parent;
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001109 struct dev_pm_opp *opp;
Nishanth Menond287de82012-10-25 19:48:59 -05001110 ssize_t count = 0;
1111 unsigned long freq = 0;
1112
Nishanth Menond287de82012-10-25 19:48:59 -05001113 do {
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001114 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
Nishanth Menond287de82012-10-25 19:48:59 -05001115 if (IS_ERR(opp))
1116 break;
1117
Viresh Kumar8a31d9d92017-01-23 10:11:47 +05301118 dev_pm_opp_put(opp);
Nishanth Menond287de82012-10-25 19:48:59 -05001119 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1120 "%lu ", freq);
1121 freq++;
1122 } while (1);
Nishanth Menond287de82012-10-25 19:48:59 -05001123
1124 /* Truncate the trailing space */
1125 if (count)
1126 count--;
1127
1128 count += sprintf(&buf[count], "\n");
1129
1130 return count;
1131}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001132static DEVICE_ATTR_RO(available_frequencies);
Nishanth Menond287de82012-10-25 19:48:59 -05001133
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001134static ssize_t trans_stat_show(struct device *dev,
1135 struct device_attribute *attr, char *buf)
Jonghwa Leee552bba2012-08-23 20:00:46 +09001136{
1137 struct devfreq *devfreq = to_devfreq(dev);
1138 ssize_t len;
Rajagopal Venkat39688ce2013-01-08 11:20:39 +05301139 int i, j;
Jonghwa Leee552bba2012-08-23 20:00:46 +09001140 unsigned int max_state = devfreq->profile->max_state;
1141
Rajagopal Venkat39688ce2013-01-08 11:20:39 +05301142 if (!devfreq->stop_polling &&
1143 devfreq_update_status(devfreq, devfreq->previous_freq))
Jonghwa Leee552bba2012-08-23 20:00:46 +09001144 return 0;
MyungJoo Ham34bd3222015-11-23 15:45:36 +09001145 if (max_state == 0)
1146 return sprintf(buf, "Not Supported.\n");
Jonghwa Leee552bba2012-08-23 20:00:46 +09001147
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001148 len = sprintf(buf, " From : To\n");
1149 len += sprintf(buf + len, " :");
Jonghwa Leee552bba2012-08-23 20:00:46 +09001150 for (i = 0; i < max_state; i++)
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001151 len += sprintf(buf + len, "%10lu",
Jonghwa Leee552bba2012-08-23 20:00:46 +09001152 devfreq->profile->freq_table[i]);
1153
1154 len += sprintf(buf + len, " time(ms)\n");
1155
1156 for (i = 0; i < max_state; i++) {
1157 if (devfreq->profile->freq_table[i]
1158 == devfreq->previous_freq) {
1159 len += sprintf(buf + len, "*");
1160 } else {
1161 len += sprintf(buf + len, " ");
1162 }
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001163 len += sprintf(buf + len, "%10lu:",
Jonghwa Leee552bba2012-08-23 20:00:46 +09001164 devfreq->profile->freq_table[i]);
1165 for (j = 0; j < max_state; j++)
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001166 len += sprintf(buf + len, "%10u",
Jonghwa Leee552bba2012-08-23 20:00:46 +09001167 devfreq->trans_table[(i * max_state) + j]);
1168 len += sprintf(buf + len, "%10u\n",
1169 jiffies_to_msecs(devfreq->time_in_state[i]));
1170 }
1171
1172 len += sprintf(buf + len, "Total transition : %u\n",
1173 devfreq->total_trans);
1174 return len;
1175}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001176static DEVICE_ATTR_RO(trans_stat);
Jonghwa Leee552bba2012-08-23 20:00:46 +09001177
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001178static struct attribute *devfreq_attrs[] = {
1179 &dev_attr_governor.attr,
1180 &dev_attr_available_governors.attr,
1181 &dev_attr_cur_freq.attr,
1182 &dev_attr_available_frequencies.attr,
1183 &dev_attr_target_freq.attr,
1184 &dev_attr_polling_interval.attr,
1185 &dev_attr_min_freq.attr,
1186 &dev_attr_max_freq.attr,
1187 &dev_attr_trans_stat.attr,
1188 NULL,
MyungJoo Ham9005b652011-10-02 00:19:28 +02001189};
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001190ATTRIBUTE_GROUPS(devfreq);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001191
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001192static int __init devfreq_init(void)
1193{
1194 devfreq_class = class_create(THIS_MODULE, "devfreq");
1195 if (IS_ERR(devfreq_class)) {
1196 pr_err("%s: couldn't create class\n", __FILE__);
1197 return PTR_ERR(devfreq_class);
1198 }
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001199
1200 devfreq_wq = create_freezable_workqueue("devfreq_wq");
Dan Carpenterea7f4542013-08-15 10:55:10 +03001201 if (!devfreq_wq) {
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001202 class_destroy(devfreq_class);
1203 pr_err("%s: couldn't create workqueue\n", __FILE__);
Dan Carpenterea7f4542013-08-15 10:55:10 +03001204 return -ENOMEM;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001205 }
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001206 devfreq_class->dev_groups = devfreq_groups;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001207
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001208 return 0;
1209}
1210subsys_initcall(devfreq_init);
1211
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001212/*
1213 * The followings are helper functions for devfreq user device drivers with
1214 * OPP framework.
1215 */
1216
1217/**
1218 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1219 * freq value given to target callback.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001220 * @dev: The devfreq user device. (parent of devfreq)
1221 * @freq: The frequency given to target function
1222 * @flags: Flags handed from devfreq framework.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001223 *
Viresh Kumar8a31d9d92017-01-23 10:11:47 +05301224 * The callers are required to call dev_pm_opp_put() for the returned OPP after
1225 * use.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001226 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001227struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1228 unsigned long *freq,
1229 u32 flags)
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001230{
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001231 struct dev_pm_opp *opp;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001232
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001233 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1234 /* The freq is an upper bound. opp should be lower */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001235 opp = dev_pm_opp_find_freq_floor(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001236
1237 /* If not available, use the closest opp */
Nishanth Menon07797262012-10-24 22:00:12 +02001238 if (opp == ERR_PTR(-ERANGE))
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001239 opp = dev_pm_opp_find_freq_ceil(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001240 } else {
1241 /* The freq is an lower bound. opp should be higher */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001242 opp = dev_pm_opp_find_freq_ceil(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001243
1244 /* If not available, use the closest opp */
Nishanth Menon07797262012-10-24 22:00:12 +02001245 if (opp == ERR_PTR(-ERANGE))
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001246 opp = dev_pm_opp_find_freq_floor(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001247 }
1248
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001249 return opp;
1250}
Ãrjan Eidebd7e9272014-07-18 15:09:53 +01001251EXPORT_SYMBOL(devfreq_recommended_opp);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001252
1253/**
1254 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1255 * for any changes in the OPP availability
1256 * changes
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001257 * @dev: The devfreq user device. (parent of devfreq)
1258 * @devfreq: The devfreq object.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001259 */
1260int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1261{
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301262 return dev_pm_opp_register_notifier(dev, &devfreq->nb);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001263}
Ãrjan Eidebd7e9272014-07-18 15:09:53 +01001264EXPORT_SYMBOL(devfreq_register_opp_notifier);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001265
1266/**
1267 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1268 * notified for any changes in the OPP
1269 * availability changes anymore.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001270 * @dev: The devfreq user device. (parent of devfreq)
1271 * @devfreq: The devfreq object.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001272 *
1273 * At exit() callback of devfreq_dev_profile, this must be included if
1274 * devfreq_recommended_opp is used.
1275 */
1276int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1277{
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301278 return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001279}
Ãrjan Eidebd7e9272014-07-18 15:09:53 +01001280EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001281
Chanwoo Choid5b040d2014-05-09 16:43:09 +09001282static void devm_devfreq_opp_release(struct device *dev, void *res)
1283{
1284 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
1285}
1286
1287/**
1288 * devm_ devfreq_register_opp_notifier()
1289 * - Resource-managed devfreq_register_opp_notifier()
1290 * @dev: The devfreq user device. (parent of devfreq)
1291 * @devfreq: The devfreq object.
1292 */
1293int devm_devfreq_register_opp_notifier(struct device *dev,
1294 struct devfreq *devfreq)
1295{
1296 struct devfreq **ptr;
1297 int ret;
1298
1299 ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
1300 if (!ptr)
1301 return -ENOMEM;
1302
1303 ret = devfreq_register_opp_notifier(dev, devfreq);
1304 if (ret) {
1305 devres_free(ptr);
1306 return ret;
1307 }
1308
1309 *ptr = devfreq;
1310 devres_add(dev, ptr);
1311
1312 return 0;
1313}
1314EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
1315
1316/**
1317 * devm_devfreq_unregister_opp_notifier()
1318 * - Resource-managed devfreq_unregister_opp_notifier()
1319 * @dev: The devfreq user device. (parent of devfreq)
1320 * @devfreq: The devfreq object.
1321 */
1322void devm_devfreq_unregister_opp_notifier(struct device *dev,
1323 struct devfreq *devfreq)
1324{
1325 WARN_ON(devres_release(dev, devm_devfreq_opp_release,
1326 devm_devfreq_dev_match, devfreq));
1327}
1328EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
1329
Chanwoo Choi0fe3a662016-01-26 13:21:26 +09001330/**
1331 * devfreq_register_notifier() - Register a driver with devfreq
1332 * @devfreq: The devfreq object.
1333 * @nb: The notifier block to register.
1334 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1335 */
1336int devfreq_register_notifier(struct devfreq *devfreq,
1337 struct notifier_block *nb,
1338 unsigned int list)
1339{
1340 int ret = 0;
1341
1342 if (!devfreq)
1343 return -EINVAL;
1344
1345 switch (list) {
1346 case DEVFREQ_TRANSITION_NOTIFIER:
1347 ret = srcu_notifier_chain_register(
1348 &devfreq->transition_notifier_list, nb);
1349 break;
1350 default:
1351 ret = -EINVAL;
1352 }
1353
1354 return ret;
1355}
1356EXPORT_SYMBOL(devfreq_register_notifier);
1357
1358/*
1359 * devfreq_unregister_notifier() - Unregister a driver with devfreq
1360 * @devfreq: The devfreq object.
1361 * @nb: The notifier block to be unregistered.
1362 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1363 */
1364int devfreq_unregister_notifier(struct devfreq *devfreq,
1365 struct notifier_block *nb,
1366 unsigned int list)
1367{
1368 int ret = 0;
1369
1370 if (!devfreq)
1371 return -EINVAL;
1372
1373 switch (list) {
1374 case DEVFREQ_TRANSITION_NOTIFIER:
1375 ret = srcu_notifier_chain_unregister(
1376 &devfreq->transition_notifier_list, nb);
1377 break;
1378 default:
1379 ret = -EINVAL;
1380 }
1381
1382 return ret;
1383}
1384EXPORT_SYMBOL(devfreq_unregister_notifier);
1385
1386struct devfreq_notifier_devres {
1387 struct devfreq *devfreq;
1388 struct notifier_block *nb;
1389 unsigned int list;
1390};
1391
1392static void devm_devfreq_notifier_release(struct device *dev, void *res)
1393{
1394 struct devfreq_notifier_devres *this = res;
1395
1396 devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
1397}
1398
1399/**
1400 * devm_devfreq_register_notifier()
1401 - Resource-managed devfreq_register_notifier()
1402 * @dev: The devfreq user device. (parent of devfreq)
1403 * @devfreq: The devfreq object.
1404 * @nb: The notifier block to be unregistered.
1405 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1406 */
1407int devm_devfreq_register_notifier(struct device *dev,
1408 struct devfreq *devfreq,
1409 struct notifier_block *nb,
1410 unsigned int list)
1411{
1412 struct devfreq_notifier_devres *ptr;
1413 int ret;
1414
1415 ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
1416 GFP_KERNEL);
1417 if (!ptr)
1418 return -ENOMEM;
1419
1420 ret = devfreq_register_notifier(devfreq, nb, list);
1421 if (ret) {
1422 devres_free(ptr);
1423 return ret;
1424 }
1425
1426 ptr->devfreq = devfreq;
1427 ptr->nb = nb;
1428 ptr->list = list;
1429 devres_add(dev, ptr);
1430
1431 return 0;
1432}
1433EXPORT_SYMBOL(devm_devfreq_register_notifier);
1434
1435/**
1436 * devm_devfreq_unregister_notifier()
1437 - Resource-managed devfreq_unregister_notifier()
1438 * @dev: The devfreq user device. (parent of devfreq)
1439 * @devfreq: The devfreq object.
1440 * @nb: The notifier block to be unregistered.
1441 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1442 */
1443void devm_devfreq_unregister_notifier(struct device *dev,
1444 struct devfreq *devfreq,
1445 struct notifier_block *nb,
1446 unsigned int list)
1447{
1448 WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
1449 devm_devfreq_dev_match, devfreq));
1450}
1451EXPORT_SYMBOL(devm_devfreq_unregister_notifier);