blob: 66d3c7184a0f0cfaf568879088d7bc3b6f584fc1 [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
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
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900192static int devfreq_notify_transition(struct devfreq *devfreq,
193 struct devfreq_freqs *freqs, unsigned int state)
194{
195 if (!devfreq)
196 return -EINVAL;
197
198 switch (state) {
199 case DEVFREQ_PRECHANGE:
200 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
201 DEVFREQ_PRECHANGE, freqs);
202 break;
203
204 case DEVFREQ_POSTCHANGE:
205 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
206 DEVFREQ_POSTCHANGE, freqs);
207 break;
208 default:
209 return -EINVAL;
210 }
211
212 return 0;
213}
214
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200215/* Load monitoring helper functions for governors use */
216
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200217/**
218 * update_devfreq() - Reevaluate the device and configure frequency.
219 * @devfreq: the devfreq instance.
220 *
221 * Note: Lock devfreq->lock before calling update_devfreq
222 * This function is exported for governors.
223 */
224int update_devfreq(struct devfreq *devfreq)
225{
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900226 struct devfreq_freqs freqs;
227 unsigned long freq, cur_freq;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200228 int err = 0;
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100229 u32 flags = 0;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200230
231 if (!mutex_is_locked(&devfreq->lock)) {
232 WARN(true, "devfreq->lock must be locked by the caller.\n");
233 return -EINVAL;
234 }
235
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500236 if (!devfreq->governor)
237 return -EINVAL;
238
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200239 /* Reevaluate the proper frequency */
240 err = devfreq->governor->get_target_freq(devfreq, &freq);
241 if (err)
242 return err;
243
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100244 /*
Javi Merinod3b7e172015-08-14 18:57:00 +0100245 * Adjust the frequency with user freq and QoS.
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100246 *
Javi Merinod3b7e172015-08-14 18:57:00 +0100247 * List from the highest priority
248 * max_freq
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100249 * min_freq
250 */
251
252 if (devfreq->min_freq && freq < devfreq->min_freq) {
253 freq = devfreq->min_freq;
254 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
255 }
256 if (devfreq->max_freq && freq > devfreq->max_freq) {
257 freq = devfreq->max_freq;
258 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
259 }
260
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900261 if (devfreq->profile->get_cur_freq)
262 devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
263 else
264 cur_freq = devfreq->previous_freq;
265
266 freqs.old = cur_freq;
267 freqs.new = freq;
268 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
269
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100270 err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
Chanwoo Choi0d371892016-06-23 11:18:43 +0900271 if (err) {
272 freqs.new = cur_freq;
273 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200274 return err;
Chanwoo Choi0d371892016-06-23 11:18:43 +0900275 }
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200276
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900277 freqs.new = freq;
278 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
279
Jonghwa Leee552bba2012-08-23 20:00:46 +0900280 if (devfreq->profile->freq_table)
281 if (devfreq_update_status(devfreq, freq))
282 dev_err(&devfreq->dev,
283 "Couldn't update frequency transition information.\n");
284
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200285 devfreq->previous_freq = freq;
286 return err;
287}
Nishanth Menon2df50212012-10-29 15:01:42 -0500288EXPORT_SYMBOL(update_devfreq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200289
290/**
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200291 * devfreq_monitor() - Periodically poll devfreq objects.
292 * @work: the work struct used to run devfreq_monitor periodically.
293 *
294 */
295static void devfreq_monitor(struct work_struct *work)
296{
297 int err;
298 struct devfreq *devfreq = container_of(work,
299 struct devfreq, work.work);
300
301 mutex_lock(&devfreq->lock);
302 err = update_devfreq(devfreq);
303 if (err)
304 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
305
306 queue_delayed_work(devfreq_wq, &devfreq->work,
307 msecs_to_jiffies(devfreq->profile->polling_ms));
308 mutex_unlock(&devfreq->lock);
309}
310
311/**
312 * devfreq_monitor_start() - Start load monitoring of devfreq instance
313 * @devfreq: the devfreq instance.
314 *
315 * Helper function for starting devfreq device load monitoing. By
316 * default delayed work based monitoring is supported. Function
317 * to be called from governor in response to DEVFREQ_GOV_START
318 * event when device is added to devfreq framework.
319 */
320void devfreq_monitor_start(struct devfreq *devfreq)
321{
322 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
323 if (devfreq->profile->polling_ms)
324 queue_delayed_work(devfreq_wq, &devfreq->work,
325 msecs_to_jiffies(devfreq->profile->polling_ms));
326}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100327EXPORT_SYMBOL(devfreq_monitor_start);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200328
329/**
330 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
331 * @devfreq: the devfreq instance.
332 *
333 * Helper function to stop devfreq device load monitoing. Function
334 * to be called from governor in response to DEVFREQ_GOV_STOP
335 * event when device is removed from devfreq framework.
336 */
337void devfreq_monitor_stop(struct devfreq *devfreq)
338{
339 cancel_delayed_work_sync(&devfreq->work);
340}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100341EXPORT_SYMBOL(devfreq_monitor_stop);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200342
343/**
344 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
345 * @devfreq: the devfreq instance.
346 *
347 * Helper function to suspend devfreq device load monitoing. Function
348 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
349 * event or when polling interval is set to zero.
350 *
351 * Note: Though this function is same as devfreq_monitor_stop(),
352 * intentionally kept separate to provide hooks for collecting
353 * transition statistics.
354 */
355void devfreq_monitor_suspend(struct devfreq *devfreq)
356{
357 mutex_lock(&devfreq->lock);
358 if (devfreq->stop_polling) {
359 mutex_unlock(&devfreq->lock);
360 return;
361 }
362
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530363 devfreq_update_status(devfreq, devfreq->previous_freq);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200364 devfreq->stop_polling = true;
365 mutex_unlock(&devfreq->lock);
366 cancel_delayed_work_sync(&devfreq->work);
367}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100368EXPORT_SYMBOL(devfreq_monitor_suspend);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200369
370/**
371 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
372 * @devfreq: the devfreq instance.
373 *
374 * Helper function to resume devfreq device load monitoing. Function
375 * to be called from governor in response to DEVFREQ_GOV_RESUME
376 * event or when polling interval is set to non-zero.
377 */
378void devfreq_monitor_resume(struct devfreq *devfreq)
379{
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530380 unsigned long freq;
381
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200382 mutex_lock(&devfreq->lock);
383 if (!devfreq->stop_polling)
384 goto out;
385
386 if (!delayed_work_pending(&devfreq->work) &&
387 devfreq->profile->polling_ms)
388 queue_delayed_work(devfreq_wq, &devfreq->work,
389 msecs_to_jiffies(devfreq->profile->polling_ms));
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530390
391 devfreq->last_stat_updated = jiffies;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200392 devfreq->stop_polling = false;
393
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530394 if (devfreq->profile->get_cur_freq &&
395 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
396 devfreq->previous_freq = freq;
397
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200398out:
399 mutex_unlock(&devfreq->lock);
400}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100401EXPORT_SYMBOL(devfreq_monitor_resume);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200402
403/**
404 * devfreq_interval_update() - Update device devfreq monitoring interval
405 * @devfreq: the devfreq instance.
406 * @delay: new polling interval to be set.
407 *
408 * Helper function to set new load monitoring polling interval. Function
409 * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
410 */
411void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
412{
413 unsigned int cur_delay = devfreq->profile->polling_ms;
414 unsigned int new_delay = *delay;
415
416 mutex_lock(&devfreq->lock);
417 devfreq->profile->polling_ms = new_delay;
418
419 if (devfreq->stop_polling)
420 goto out;
421
422 /* if new delay is zero, stop polling */
423 if (!new_delay) {
424 mutex_unlock(&devfreq->lock);
425 cancel_delayed_work_sync(&devfreq->work);
426 return;
427 }
428
429 /* if current delay is zero, start polling with new delay */
430 if (!cur_delay) {
431 queue_delayed_work(devfreq_wq, &devfreq->work,
432 msecs_to_jiffies(devfreq->profile->polling_ms));
433 goto out;
434 }
435
436 /* if current delay is greater than new delay, restart polling */
437 if (cur_delay > new_delay) {
438 mutex_unlock(&devfreq->lock);
439 cancel_delayed_work_sync(&devfreq->work);
440 mutex_lock(&devfreq->lock);
441 if (!devfreq->stop_polling)
442 queue_delayed_work(devfreq_wq, &devfreq->work,
443 msecs_to_jiffies(devfreq->profile->polling_ms));
444 }
445out:
446 mutex_unlock(&devfreq->lock);
447}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100448EXPORT_SYMBOL(devfreq_interval_update);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200449
450/**
451 * devfreq_notifier_call() - Notify that the device frequency requirements
452 * has been changed out of devfreq framework.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +0200453 * @nb: the notifier_block (supposed to be devfreq->nb)
454 * @type: not used
455 * @devp: not used
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200456 *
457 * Called by a notifier that uses devfreq->nb.
458 */
459static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
460 void *devp)
461{
462 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
463 int ret;
464
465 mutex_lock(&devfreq->lock);
466 ret = update_devfreq(devfreq);
467 mutex_unlock(&devfreq->lock);
468
469 return ret;
470}
471
472/**
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200473 * _remove_devfreq() - Remove devfreq from the list and release its resources.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200474 * @devfreq: the devfreq struct
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200475 */
Chanwoo Choi585fc832014-05-09 16:43:07 +0900476static void _remove_devfreq(struct devfreq *devfreq)
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200477{
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200478 mutex_lock(&devfreq_list_lock);
479 if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
480 mutex_unlock(&devfreq_list_lock);
481 dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200482 return;
483 }
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200484 list_del(&devfreq->node);
485 mutex_unlock(&devfreq_list_lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200486
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500487 if (devfreq->governor)
488 devfreq->governor->event_handler(devfreq,
489 DEVFREQ_GOV_STOP, NULL);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200490
491 if (devfreq->profile->exit)
492 devfreq->profile->exit(devfreq->dev.parent);
493
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200494 mutex_destroy(&devfreq->lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200495 kfree(devfreq);
496}
497
498/**
499 * devfreq_dev_release() - Callback for struct device to release the device.
500 * @dev: the devfreq device
501 *
502 * This calls _remove_devfreq() if _remove_devfreq() is not called.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200503 */
504static void devfreq_dev_release(struct device *dev)
505{
506 struct devfreq *devfreq = to_devfreq(dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200507
Chanwoo Choi585fc832014-05-09 16:43:07 +0900508 _remove_devfreq(devfreq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200509}
510
511/**
512 * devfreq_add_device() - Add devfreq feature to the device
513 * @dev: the device to add devfreq feature.
514 * @profile: device-specific profile to run devfreq.
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500515 * @governor_name: name of the policy to choose frequency.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200516 * @data: private data for the governor. The devfreq framework does not
517 * touch this value.
518 */
519struct devfreq *devfreq_add_device(struct device *dev,
520 struct devfreq_dev_profile *profile,
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500521 const char *governor_name,
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200522 void *data)
523{
524 struct devfreq *devfreq;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500525 struct devfreq_governor *governor;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200526 int err = 0;
527
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500528 if (!dev || !profile || !governor_name) {
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200529 dev_err(dev, "%s: Invalid parameters.\n", __func__);
530 return ERR_PTR(-EINVAL);
531 }
532
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200533 mutex_lock(&devfreq_list_lock);
534 devfreq = find_device_devfreq(dev);
535 mutex_unlock(&devfreq_list_lock);
536 if (!IS_ERR(devfreq)) {
537 dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__);
538 err = -EINVAL;
539 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200540 }
541
542 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
543 if (!devfreq) {
544 dev_err(dev, "%s: Unable to create devfreq for the device\n",
545 __func__);
546 err = -ENOMEM;
Axel Lin3f19f082011-11-15 21:59:09 +0100547 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200548 }
549
550 mutex_init(&devfreq->lock);
551 mutex_lock(&devfreq->lock);
552 devfreq->dev.parent = dev;
553 devfreq->dev.class = devfreq_class;
554 devfreq->dev.release = devfreq_dev_release;
555 devfreq->profile = profile;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500556 strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200557 devfreq->previous_freq = profile->initial_freq;
Lukasz Luba8d39fc082016-05-31 11:25:09 +0100558 devfreq->last_status.current_frequency = profile->initial_freq;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200559 devfreq->data = data;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200560 devfreq->nb.notifier_call = devfreq_notifier_call;
561
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900562 if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
563 mutex_unlock(&devfreq->lock);
564 devfreq_set_freq_table(devfreq);
565 mutex_lock(&devfreq->lock);
566 }
567
Kees Cook02aa2a32013-07-03 15:04:56 -0700568 dev_set_name(&devfreq->dev, "%s", dev_name(dev));
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200569 err = device_register(&devfreq->dev);
570 if (err) {
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200571 mutex_unlock(&devfreq->lock);
Geliang Tang6d3cbfa2015-10-01 22:18:19 +0800572 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200573 }
574
MyungJoo Ham3e1d7fb2015-10-02 12:39:23 +0900575 devfreq->trans_table = devm_kzalloc(&devfreq->dev, sizeof(unsigned int) *
576 devfreq->profile->max_state *
577 devfreq->profile->max_state,
578 GFP_KERNEL);
579 devfreq->time_in_state = devm_kzalloc(&devfreq->dev, sizeof(unsigned long) *
580 devfreq->profile->max_state,
581 GFP_KERNEL);
582 devfreq->last_stat_updated = jiffies;
583
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900584 srcu_init_notifier_head(&devfreq->transition_notifier_list);
585
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200586 mutex_unlock(&devfreq->lock);
587
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200588 mutex_lock(&devfreq_list_lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200589 list_add(&devfreq->node, &devfreq_list);
590
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500591 governor = find_devfreq_governor(devfreq->governor_name);
592 if (!IS_ERR(governor))
593 devfreq->governor = governor;
594 if (devfreq->governor)
595 err = devfreq->governor->event_handler(devfreq,
596 DEVFREQ_GOV_START, NULL);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200597 if (err) {
598 dev_err(dev, "%s: Unable to start governor for the device\n",
599 __func__);
600 goto err_init;
601 }
Axel Lin0f376c92016-09-29 10:13:28 +0800602 mutex_unlock(&devfreq_list_lock);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200603
Axel Lin3f19f082011-11-15 21:59:09 +0100604 return devfreq;
605
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200606err_init:
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200607 list_del(&devfreq->node);
Axel Lin0f376c92016-09-29 10:13:28 +0800608 mutex_unlock(&devfreq_list_lock);
609
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200610 device_unregister(&devfreq->dev);
Axel Lin3f19f082011-11-15 21:59:09 +0100611err_out:
612 return ERR_PTR(err);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200613}
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200614EXPORT_SYMBOL(devfreq_add_device);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200615
616/**
617 * devfreq_remove_device() - Remove devfreq feature from a device.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +0200618 * @devfreq: the devfreq instance to be removed
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900619 *
620 * The opposite of devfreq_add_device().
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200621 */
622int devfreq_remove_device(struct devfreq *devfreq)
623{
624 if (!devfreq)
625 return -EINVAL;
626
Chanwoo Choi585fc832014-05-09 16:43:07 +0900627 device_unregister(&devfreq->dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200628
629 return 0;
630}
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200631EXPORT_SYMBOL(devfreq_remove_device);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200632
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900633static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
634{
635 struct devfreq **r = res;
636
637 if (WARN_ON(!r || !*r))
638 return 0;
639
640 return *r == data;
641}
642
643static void devm_devfreq_dev_release(struct device *dev, void *res)
644{
645 devfreq_remove_device(*(struct devfreq **)res);
646}
647
648/**
649 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
650 * @dev: the device to add devfreq feature.
651 * @profile: device-specific profile to run devfreq.
652 * @governor_name: name of the policy to choose frequency.
653 * @data: private data for the governor. The devfreq framework does not
654 * touch this value.
655 *
656 * This function manages automatically the memory of devfreq device using device
657 * resource management and simplify the free operation for memory of devfreq
658 * device.
659 */
660struct devfreq *devm_devfreq_add_device(struct device *dev,
661 struct devfreq_dev_profile *profile,
662 const char *governor_name,
663 void *data)
664{
665 struct devfreq **ptr, *devfreq;
666
667 ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
668 if (!ptr)
669 return ERR_PTR(-ENOMEM);
670
671 devfreq = devfreq_add_device(dev, profile, governor_name, data);
672 if (IS_ERR(devfreq)) {
673 devres_free(ptr);
674 return ERR_PTR(-ENOMEM);
675 }
676
677 *ptr = devfreq;
678 devres_add(dev, ptr);
679
680 return devfreq;
681}
682EXPORT_SYMBOL(devm_devfreq_add_device);
683
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900684#ifdef CONFIG_OF
685/*
686 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
687 * @dev - instance to the given device
688 * @index - index into list of devfreq
689 *
690 * return the instance of devfreq device
691 */
692struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
693{
694 struct device_node *node;
695 struct devfreq *devfreq;
696
697 if (!dev)
698 return ERR_PTR(-EINVAL);
699
700 if (!dev->of_node)
701 return ERR_PTR(-EINVAL);
702
703 node = of_parse_phandle(dev->of_node, "devfreq", index);
704 if (!node)
705 return ERR_PTR(-ENODEV);
706
707 mutex_lock(&devfreq_list_lock);
708 list_for_each_entry(devfreq, &devfreq_list, node) {
709 if (devfreq->dev.parent
710 && devfreq->dev.parent->of_node == node) {
711 mutex_unlock(&devfreq_list_lock);
Peter Chen3427c6f2016-07-01 17:42:00 +0800712 of_node_put(node);
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900713 return devfreq;
714 }
715 }
716 mutex_unlock(&devfreq_list_lock);
Peter Chen3427c6f2016-07-01 17:42:00 +0800717 of_node_put(node);
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900718
719 return ERR_PTR(-EPROBE_DEFER);
720}
721#else
722struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
723{
724 return ERR_PTR(-ENODEV);
725}
726#endif /* CONFIG_OF */
727EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
728
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900729/**
730 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
731 * @dev: the device to add devfreq feature.
732 * @devfreq: the devfreq instance to be removed
733 */
734void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
735{
736 WARN_ON(devres_release(dev, devm_devfreq_dev_release,
737 devm_devfreq_dev_match, devfreq));
738}
739EXPORT_SYMBOL(devm_devfreq_remove_device);
740
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200741/**
742 * devfreq_suspend_device() - Suspend devfreq of a device.
743 * @devfreq: the devfreq instance to be suspended
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900744 *
745 * This function is intended to be called by the pm callbacks
746 * (e.g., runtime_suspend, suspend) of the device driver that
747 * holds the devfreq.
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200748 */
749int devfreq_suspend_device(struct devfreq *devfreq)
750{
751 if (!devfreq)
752 return -EINVAL;
753
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500754 if (!devfreq->governor)
755 return 0;
756
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200757 return devfreq->governor->event_handler(devfreq,
758 DEVFREQ_GOV_SUSPEND, NULL);
759}
760EXPORT_SYMBOL(devfreq_suspend_device);
761
762/**
763 * devfreq_resume_device() - Resume devfreq of a device.
764 * @devfreq: the devfreq instance to be resumed
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900765 *
766 * This function is intended to be called by the pm callbacks
767 * (e.g., runtime_resume, resume) of the device driver that
768 * holds the devfreq.
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200769 */
770int devfreq_resume_device(struct devfreq *devfreq)
771{
772 if (!devfreq)
773 return -EINVAL;
774
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500775 if (!devfreq->governor)
776 return 0;
777
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200778 return devfreq->governor->event_handler(devfreq,
779 DEVFREQ_GOV_RESUME, NULL);
780}
781EXPORT_SYMBOL(devfreq_resume_device);
782
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500783/**
784 * devfreq_add_governor() - Add devfreq governor
785 * @governor: the devfreq governor to be added
786 */
787int devfreq_add_governor(struct devfreq_governor *governor)
788{
789 struct devfreq_governor *g;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500790 struct devfreq *devfreq;
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500791 int err = 0;
792
793 if (!governor) {
794 pr_err("%s: Invalid parameters.\n", __func__);
795 return -EINVAL;
796 }
797
798 mutex_lock(&devfreq_list_lock);
799 g = find_devfreq_governor(governor->name);
800 if (!IS_ERR(g)) {
801 pr_err("%s: governor %s already registered\n", __func__,
802 g->name);
803 err = -EINVAL;
804 goto err_out;
805 }
806
807 list_add(&governor->node, &devfreq_governor_list);
808
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500809 list_for_each_entry(devfreq, &devfreq_list, node) {
810 int ret = 0;
811 struct device *dev = devfreq->dev.parent;
812
813 if (!strncmp(devfreq->governor_name, governor->name,
814 DEVFREQ_NAME_LEN)) {
815 /* The following should never occur */
816 if (devfreq->governor) {
817 dev_warn(dev,
818 "%s: Governor %s already present\n",
819 __func__, devfreq->governor->name);
820 ret = devfreq->governor->event_handler(devfreq,
821 DEVFREQ_GOV_STOP, NULL);
822 if (ret) {
823 dev_warn(dev,
824 "%s: Governor %s stop = %d\n",
825 __func__,
826 devfreq->governor->name, ret);
827 }
828 /* Fall through */
829 }
830 devfreq->governor = governor;
831 ret = devfreq->governor->event_handler(devfreq,
832 DEVFREQ_GOV_START, NULL);
833 if (ret) {
834 dev_warn(dev, "%s: Governor %s start=%d\n",
835 __func__, devfreq->governor->name,
836 ret);
837 }
838 }
839 }
840
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500841err_out:
842 mutex_unlock(&devfreq_list_lock);
843
844 return err;
845}
846EXPORT_SYMBOL(devfreq_add_governor);
847
848/**
849 * devfreq_remove_device() - Remove devfreq feature from a device.
850 * @governor: the devfreq governor to be removed
851 */
852int devfreq_remove_governor(struct devfreq_governor *governor)
853{
854 struct devfreq_governor *g;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500855 struct devfreq *devfreq;
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500856 int err = 0;
857
858 if (!governor) {
859 pr_err("%s: Invalid parameters.\n", __func__);
860 return -EINVAL;
861 }
862
863 mutex_lock(&devfreq_list_lock);
864 g = find_devfreq_governor(governor->name);
865 if (IS_ERR(g)) {
866 pr_err("%s: governor %s not registered\n", __func__,
Sachin Kamatb9e1c8e2012-11-21 10:36:13 +0530867 governor->name);
Sachin Kamatf9c08e22012-11-21 10:36:14 +0530868 err = PTR_ERR(g);
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500869 goto err_out;
870 }
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500871 list_for_each_entry(devfreq, &devfreq_list, node) {
872 int ret;
873 struct device *dev = devfreq->dev.parent;
874
875 if (!strncmp(devfreq->governor_name, governor->name,
876 DEVFREQ_NAME_LEN)) {
877 /* we should have a devfreq governor! */
878 if (!devfreq->governor) {
879 dev_warn(dev, "%s: Governor %s NOT present\n",
880 __func__, governor->name);
881 continue;
882 /* Fall through */
883 }
884 ret = devfreq->governor->event_handler(devfreq,
885 DEVFREQ_GOV_STOP, NULL);
886 if (ret) {
887 dev_warn(dev, "%s: Governor %s stop=%d\n",
888 __func__, devfreq->governor->name,
889 ret);
890 }
891 devfreq->governor = NULL;
892 }
893 }
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500894
895 list_del(&governor->node);
896err_out:
897 mutex_unlock(&devfreq_list_lock);
898
899 return err;
900}
901EXPORT_SYMBOL(devfreq_remove_governor);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200902
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700903static ssize_t governor_show(struct device *dev,
MyungJoo Ham9005b652011-10-02 00:19:28 +0200904 struct device_attribute *attr, char *buf)
905{
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500906 if (!to_devfreq(dev)->governor)
907 return -EINVAL;
908
MyungJoo Ham9005b652011-10-02 00:19:28 +0200909 return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
910}
911
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700912static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500913 const char *buf, size_t count)
914{
915 struct devfreq *df = to_devfreq(dev);
916 int ret;
917 char str_governor[DEVFREQ_NAME_LEN + 1];
918 struct devfreq_governor *governor;
919
920 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
921 if (ret != 1)
922 return -EINVAL;
923
924 mutex_lock(&devfreq_list_lock);
925 governor = find_devfreq_governor(str_governor);
926 if (IS_ERR(governor)) {
927 ret = PTR_ERR(governor);
928 goto out;
929 }
Tobias Jakobi14a21e72015-09-21 20:23:52 +0200930 if (df->governor == governor) {
931 ret = 0;
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500932 goto out;
Tobias Jakobi14a21e72015-09-21 20:23:52 +0200933 }
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500934
935 if (df->governor) {
936 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
937 if (ret) {
938 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
939 __func__, df->governor->name, ret);
940 goto out;
941 }
942 }
943 df->governor = governor;
944 strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
945 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
946 if (ret)
947 dev_warn(dev, "%s: Governor %s not started(%d)\n",
948 __func__, df->governor->name, ret);
949out:
950 mutex_unlock(&devfreq_list_lock);
951
952 if (!ret)
953 ret = count;
954 return ret;
955}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700956static DEVICE_ATTR_RW(governor);
957
958static ssize_t available_governors_show(struct device *d,
959 struct device_attribute *attr,
960 char *buf)
Nishanth Menon50a5b332012-10-29 15:01:48 -0500961{
962 struct devfreq_governor *tmp_governor;
963 ssize_t count = 0;
964
965 mutex_lock(&devfreq_list_lock);
966 list_for_each_entry(tmp_governor, &devfreq_governor_list, node)
967 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
968 "%s ", tmp_governor->name);
969 mutex_unlock(&devfreq_list_lock);
970
971 /* Truncate the trailing space */
972 if (count)
973 count--;
974
975 count += sprintf(&buf[count], "\n");
976
977 return count;
978}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700979static DEVICE_ATTR_RO(available_governors);
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500980
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700981static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
982 char *buf)
MyungJoo Ham9005b652011-10-02 00:19:28 +0200983{
Rajagopal Venkat7f98a902012-10-26 01:50:26 +0200984 unsigned long freq;
985 struct devfreq *devfreq = to_devfreq(dev);
986
987 if (devfreq->profile->get_cur_freq &&
988 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
989 return sprintf(buf, "%lu\n", freq);
990
991 return sprintf(buf, "%lu\n", devfreq->previous_freq);
992}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700993static DEVICE_ATTR_RO(cur_freq);
Rajagopal Venkat7f98a902012-10-26 01:50:26 +0200994
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700995static ssize_t target_freq_show(struct device *dev,
996 struct device_attribute *attr, char *buf)
Rajagopal Venkat7f98a902012-10-26 01:50:26 +0200997{
MyungJoo Ham9005b652011-10-02 00:19:28 +0200998 return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
999}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001000static DEVICE_ATTR_RO(target_freq);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001001
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001002static ssize_t polling_interval_show(struct device *dev,
MyungJoo Ham9005b652011-10-02 00:19:28 +02001003 struct device_attribute *attr, char *buf)
1004{
1005 return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
1006}
1007
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001008static ssize_t polling_interval_store(struct device *dev,
MyungJoo Ham9005b652011-10-02 00:19:28 +02001009 struct device_attribute *attr,
1010 const char *buf, size_t count)
1011{
1012 struct devfreq *df = to_devfreq(dev);
1013 unsigned int value;
1014 int ret;
1015
Nishanth Menon1b5c1be2012-10-29 15:01:45 -05001016 if (!df->governor)
1017 return -EINVAL;
1018
MyungJoo Ham9005b652011-10-02 00:19:28 +02001019 ret = sscanf(buf, "%u", &value);
1020 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +02001021 return -EINVAL;
MyungJoo Ham9005b652011-10-02 00:19:28 +02001022
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001023 df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001024 ret = count;
1025
MyungJoo Ham9005b652011-10-02 00:19:28 +02001026 return ret;
1027}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001028static DEVICE_ATTR_RW(polling_interval);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001029
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001030static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001031 const char *buf, size_t count)
1032{
1033 struct devfreq *df = to_devfreq(dev);
1034 unsigned long value;
1035 int ret;
1036 unsigned long max;
1037
1038 ret = sscanf(buf, "%lu", &value);
1039 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +02001040 return -EINVAL;
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001041
1042 mutex_lock(&df->lock);
1043 max = df->max_freq;
1044 if (value && max && value > max) {
1045 ret = -EINVAL;
1046 goto unlock;
1047 }
1048
1049 df->min_freq = value;
1050 update_devfreq(df);
1051 ret = count;
1052unlock:
1053 mutex_unlock(&df->lock);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001054 return ret;
1055}
1056
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001057static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001058 const char *buf, size_t count)
1059{
1060 struct devfreq *df = to_devfreq(dev);
1061 unsigned long value;
1062 int ret;
1063 unsigned long min;
1064
1065 ret = sscanf(buf, "%lu", &value);
1066 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +02001067 return -EINVAL;
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001068
1069 mutex_lock(&df->lock);
1070 min = df->min_freq;
1071 if (value && min && value < min) {
1072 ret = -EINVAL;
1073 goto unlock;
1074 }
1075
1076 df->max_freq = value;
1077 update_devfreq(df);
1078 ret = count;
1079unlock:
1080 mutex_unlock(&df->lock);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001081 return ret;
1082}
1083
Chanwoo Choi3104fa32015-11-13 19:25:28 +09001084#define show_one(name) \
1085static ssize_t name##_show \
1086(struct device *dev, struct device_attribute *attr, char *buf) \
1087{ \
1088 return sprintf(buf, "%lu\n", to_devfreq(dev)->name); \
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001089}
Chanwoo Choi3104fa32015-11-13 19:25:28 +09001090show_one(min_freq);
1091show_one(max_freq);
1092
1093static DEVICE_ATTR_RW(min_freq);
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001094static DEVICE_ATTR_RW(max_freq);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001095
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001096static ssize_t available_frequencies_show(struct device *d,
1097 struct device_attribute *attr,
1098 char *buf)
Nishanth Menond287de82012-10-25 19:48:59 -05001099{
1100 struct devfreq *df = to_devfreq(d);
1101 struct device *dev = df->dev.parent;
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001102 struct dev_pm_opp *opp;
Nishanth Menond287de82012-10-25 19:48:59 -05001103 ssize_t count = 0;
1104 unsigned long freq = 0;
1105
1106 rcu_read_lock();
1107 do {
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001108 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
Nishanth Menond287de82012-10-25 19:48:59 -05001109 if (IS_ERR(opp))
1110 break;
1111
1112 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1113 "%lu ", freq);
1114 freq++;
1115 } while (1);
1116 rcu_read_unlock();
1117
1118 /* Truncate the trailing space */
1119 if (count)
1120 count--;
1121
1122 count += sprintf(&buf[count], "\n");
1123
1124 return count;
1125}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001126static DEVICE_ATTR_RO(available_frequencies);
Nishanth Menond287de82012-10-25 19:48:59 -05001127
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001128static ssize_t trans_stat_show(struct device *dev,
1129 struct device_attribute *attr, char *buf)
Jonghwa Leee552bba2012-08-23 20:00:46 +09001130{
1131 struct devfreq *devfreq = to_devfreq(dev);
1132 ssize_t len;
Rajagopal Venkat39688ce2013-01-08 11:20:39 +05301133 int i, j;
Jonghwa Leee552bba2012-08-23 20:00:46 +09001134 unsigned int max_state = devfreq->profile->max_state;
1135
Rajagopal Venkat39688ce2013-01-08 11:20:39 +05301136 if (!devfreq->stop_polling &&
1137 devfreq_update_status(devfreq, devfreq->previous_freq))
Jonghwa Leee552bba2012-08-23 20:00:46 +09001138 return 0;
MyungJoo Ham34bd3222015-11-23 15:45:36 +09001139 if (max_state == 0)
1140 return sprintf(buf, "Not Supported.\n");
Jonghwa Leee552bba2012-08-23 20:00:46 +09001141
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001142 len = sprintf(buf, " From : To\n");
1143 len += sprintf(buf + len, " :");
Jonghwa Leee552bba2012-08-23 20:00:46 +09001144 for (i = 0; i < max_state; i++)
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001145 len += sprintf(buf + len, "%10lu",
Jonghwa Leee552bba2012-08-23 20:00:46 +09001146 devfreq->profile->freq_table[i]);
1147
1148 len += sprintf(buf + len, " time(ms)\n");
1149
1150 for (i = 0; i < max_state; i++) {
1151 if (devfreq->profile->freq_table[i]
1152 == devfreq->previous_freq) {
1153 len += sprintf(buf + len, "*");
1154 } else {
1155 len += sprintf(buf + len, " ");
1156 }
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001157 len += sprintf(buf + len, "%10lu:",
Jonghwa Leee552bba2012-08-23 20:00:46 +09001158 devfreq->profile->freq_table[i]);
1159 for (j = 0; j < max_state; j++)
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001160 len += sprintf(buf + len, "%10u",
Jonghwa Leee552bba2012-08-23 20:00:46 +09001161 devfreq->trans_table[(i * max_state) + j]);
1162 len += sprintf(buf + len, "%10u\n",
1163 jiffies_to_msecs(devfreq->time_in_state[i]));
1164 }
1165
1166 len += sprintf(buf + len, "Total transition : %u\n",
1167 devfreq->total_trans);
1168 return len;
1169}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001170static DEVICE_ATTR_RO(trans_stat);
Jonghwa Leee552bba2012-08-23 20:00:46 +09001171
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001172static struct attribute *devfreq_attrs[] = {
1173 &dev_attr_governor.attr,
1174 &dev_attr_available_governors.attr,
1175 &dev_attr_cur_freq.attr,
1176 &dev_attr_available_frequencies.attr,
1177 &dev_attr_target_freq.attr,
1178 &dev_attr_polling_interval.attr,
1179 &dev_attr_min_freq.attr,
1180 &dev_attr_max_freq.attr,
1181 &dev_attr_trans_stat.attr,
1182 NULL,
MyungJoo Ham9005b652011-10-02 00:19:28 +02001183};
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001184ATTRIBUTE_GROUPS(devfreq);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001185
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001186static int __init devfreq_init(void)
1187{
1188 devfreq_class = class_create(THIS_MODULE, "devfreq");
1189 if (IS_ERR(devfreq_class)) {
1190 pr_err("%s: couldn't create class\n", __FILE__);
1191 return PTR_ERR(devfreq_class);
1192 }
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001193
1194 devfreq_wq = create_freezable_workqueue("devfreq_wq");
Dan Carpenterea7f4542013-08-15 10:55:10 +03001195 if (!devfreq_wq) {
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001196 class_destroy(devfreq_class);
1197 pr_err("%s: couldn't create workqueue\n", __FILE__);
Dan Carpenterea7f4542013-08-15 10:55:10 +03001198 return -ENOMEM;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001199 }
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001200 devfreq_class->dev_groups = devfreq_groups;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001201
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001202 return 0;
1203}
1204subsys_initcall(devfreq_init);
1205
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001206/*
1207 * The followings are helper functions for devfreq user device drivers with
1208 * OPP framework.
1209 */
1210
1211/**
1212 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1213 * freq value given to target callback.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001214 * @dev: The devfreq user device. (parent of devfreq)
1215 * @freq: The frequency given to target function
1216 * @flags: Flags handed from devfreq framework.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001217 *
Nishanth Menonbcb27542013-01-18 19:52:34 +00001218 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
1219 * protected pointer. The reason for the same is that the opp pointer which is
1220 * returned will remain valid for use with opp_get_{voltage, freq} only while
1221 * under the locked area. The pointer returned must be used prior to unlocking
1222 * with rcu_read_unlock() to maintain the integrity of the pointer.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001223 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001224struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1225 unsigned long *freq,
1226 u32 flags)
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001227{
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001228 struct dev_pm_opp *opp;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001229
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001230 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1231 /* The freq is an upper bound. opp should be lower */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001232 opp = dev_pm_opp_find_freq_floor(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001233
1234 /* If not available, use the closest opp */
Nishanth Menon07797262012-10-24 22:00:12 +02001235 if (opp == ERR_PTR(-ERANGE))
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001236 opp = dev_pm_opp_find_freq_ceil(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001237 } else {
1238 /* The freq is an lower bound. opp should be higher */
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
1241 /* If not available, use the closest opp */
Nishanth Menon07797262012-10-24 22:00:12 +02001242 if (opp == ERR_PTR(-ERANGE))
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001243 opp = dev_pm_opp_find_freq_floor(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001244 }
1245
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001246 return opp;
1247}
Ãrjan Eidebd7e9272014-07-18 15:09:53 +01001248EXPORT_SYMBOL(devfreq_recommended_opp);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001249
1250/**
1251 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1252 * for any changes in the OPP availability
1253 * changes
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001254 * @dev: The devfreq user device. (parent of devfreq)
1255 * @devfreq: The devfreq object.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001256 */
1257int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1258{
MyungJoo Ham389baed2012-11-21 19:04:51 +09001259 struct srcu_notifier_head *nh;
1260 int ret = 0;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001261
MyungJoo Ham389baed2012-11-21 19:04:51 +09001262 rcu_read_lock();
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001263 nh = dev_pm_opp_get_notifier(dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001264 if (IS_ERR(nh))
MyungJoo Ham389baed2012-11-21 19:04:51 +09001265 ret = PTR_ERR(nh);
1266 rcu_read_unlock();
1267 if (!ret)
1268 ret = srcu_notifier_chain_register(nh, &devfreq->nb);
1269
1270 return ret;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001271}
Ãrjan Eidebd7e9272014-07-18 15:09:53 +01001272EXPORT_SYMBOL(devfreq_register_opp_notifier);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001273
1274/**
1275 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1276 * notified for any changes in the OPP
1277 * availability changes anymore.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001278 * @dev: The devfreq user device. (parent of devfreq)
1279 * @devfreq: The devfreq object.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001280 *
1281 * At exit() callback of devfreq_dev_profile, this must be included if
1282 * devfreq_recommended_opp is used.
1283 */
1284int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1285{
MyungJoo Ham389baed2012-11-21 19:04:51 +09001286 struct srcu_notifier_head *nh;
1287 int ret = 0;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001288
MyungJoo Ham389baed2012-11-21 19:04:51 +09001289 rcu_read_lock();
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001290 nh = dev_pm_opp_get_notifier(dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001291 if (IS_ERR(nh))
MyungJoo Ham389baed2012-11-21 19:04:51 +09001292 ret = PTR_ERR(nh);
1293 rcu_read_unlock();
1294 if (!ret)
1295 ret = srcu_notifier_chain_unregister(nh, &devfreq->nb);
1296
1297 return ret;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001298}
Ãrjan Eidebd7e9272014-07-18 15:09:53 +01001299EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001300
Chanwoo Choid5b040d2014-05-09 16:43:09 +09001301static void devm_devfreq_opp_release(struct device *dev, void *res)
1302{
1303 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
1304}
1305
1306/**
1307 * devm_ devfreq_register_opp_notifier()
1308 * - Resource-managed devfreq_register_opp_notifier()
1309 * @dev: The devfreq user device. (parent of devfreq)
1310 * @devfreq: The devfreq object.
1311 */
1312int devm_devfreq_register_opp_notifier(struct device *dev,
1313 struct devfreq *devfreq)
1314{
1315 struct devfreq **ptr;
1316 int ret;
1317
1318 ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
1319 if (!ptr)
1320 return -ENOMEM;
1321
1322 ret = devfreq_register_opp_notifier(dev, devfreq);
1323 if (ret) {
1324 devres_free(ptr);
1325 return ret;
1326 }
1327
1328 *ptr = devfreq;
1329 devres_add(dev, ptr);
1330
1331 return 0;
1332}
1333EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
1334
1335/**
1336 * devm_devfreq_unregister_opp_notifier()
1337 * - Resource-managed devfreq_unregister_opp_notifier()
1338 * @dev: The devfreq user device. (parent of devfreq)
1339 * @devfreq: The devfreq object.
1340 */
1341void devm_devfreq_unregister_opp_notifier(struct device *dev,
1342 struct devfreq *devfreq)
1343{
1344 WARN_ON(devres_release(dev, devm_devfreq_opp_release,
1345 devm_devfreq_dev_match, devfreq));
1346}
1347EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
1348
Chanwoo Choi0fe3a662016-01-26 13:21:26 +09001349/**
1350 * devfreq_register_notifier() - Register a driver with devfreq
1351 * @devfreq: The devfreq object.
1352 * @nb: The notifier block to register.
1353 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1354 */
1355int devfreq_register_notifier(struct devfreq *devfreq,
1356 struct notifier_block *nb,
1357 unsigned int list)
1358{
1359 int ret = 0;
1360
1361 if (!devfreq)
1362 return -EINVAL;
1363
1364 switch (list) {
1365 case DEVFREQ_TRANSITION_NOTIFIER:
1366 ret = srcu_notifier_chain_register(
1367 &devfreq->transition_notifier_list, nb);
1368 break;
1369 default:
1370 ret = -EINVAL;
1371 }
1372
1373 return ret;
1374}
1375EXPORT_SYMBOL(devfreq_register_notifier);
1376
1377/*
1378 * devfreq_unregister_notifier() - Unregister a driver with devfreq
1379 * @devfreq: The devfreq object.
1380 * @nb: The notifier block to be unregistered.
1381 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1382 */
1383int devfreq_unregister_notifier(struct devfreq *devfreq,
1384 struct notifier_block *nb,
1385 unsigned int list)
1386{
1387 int ret = 0;
1388
1389 if (!devfreq)
1390 return -EINVAL;
1391
1392 switch (list) {
1393 case DEVFREQ_TRANSITION_NOTIFIER:
1394 ret = srcu_notifier_chain_unregister(
1395 &devfreq->transition_notifier_list, nb);
1396 break;
1397 default:
1398 ret = -EINVAL;
1399 }
1400
1401 return ret;
1402}
1403EXPORT_SYMBOL(devfreq_unregister_notifier);
1404
1405struct devfreq_notifier_devres {
1406 struct devfreq *devfreq;
1407 struct notifier_block *nb;
1408 unsigned int list;
1409};
1410
1411static void devm_devfreq_notifier_release(struct device *dev, void *res)
1412{
1413 struct devfreq_notifier_devres *this = res;
1414
1415 devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
1416}
1417
1418/**
1419 * devm_devfreq_register_notifier()
1420 - Resource-managed devfreq_register_notifier()
1421 * @dev: The devfreq user device. (parent of devfreq)
1422 * @devfreq: The devfreq object.
1423 * @nb: The notifier block to be unregistered.
1424 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1425 */
1426int devm_devfreq_register_notifier(struct device *dev,
1427 struct devfreq *devfreq,
1428 struct notifier_block *nb,
1429 unsigned int list)
1430{
1431 struct devfreq_notifier_devres *ptr;
1432 int ret;
1433
1434 ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
1435 GFP_KERNEL);
1436 if (!ptr)
1437 return -ENOMEM;
1438
1439 ret = devfreq_register_notifier(devfreq, nb, list);
1440 if (ret) {
1441 devres_free(ptr);
1442 return ret;
1443 }
1444
1445 ptr->devfreq = devfreq;
1446 ptr->nb = nb;
1447 ptr->list = list;
1448 devres_add(dev, ptr);
1449
1450 return 0;
1451}
1452EXPORT_SYMBOL(devm_devfreq_register_notifier);
1453
1454/**
1455 * devm_devfreq_unregister_notifier()
1456 - Resource-managed devfreq_unregister_notifier()
1457 * @dev: The devfreq user device. (parent of devfreq)
1458 * @devfreq: The devfreq object.
1459 * @nb: The notifier block to be unregistered.
1460 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1461 */
1462void devm_devfreq_unregister_notifier(struct device *dev,
1463 struct devfreq *devfreq,
1464 struct notifier_block *nb,
1465 unsigned int list)
1466{
1467 WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
1468 devm_devfreq_dev_match, devfreq));
1469}
1470EXPORT_SYMBOL(devm_devfreq_unregister_notifier);