Merge branch 'x86-boot-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / drivers / devfreq / devfreq.c
CommitLineData
a3c98b8b
MH
1/*
2 * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
3 * for Non-CPU Devices.
4 *
5 * Copyright (C) 2011 Samsung Electronics
6 * MyungJoo Ham <myungjoo.ham@samsung.com>
7 *
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>
417dc4bb 18#include <linux/export.h>
a3c98b8b 19#include <linux/slab.h>
952f6d13 20#include <linux/stat.h>
e4db1c74 21#include <linux/pm_opp.h>
a3c98b8b
MH
22#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>
8f510aeb 28#include <linux/of.h>
a3c98b8b
MH
29#include "governor.h"
30
1a1357ea 31static struct class *devfreq_class;
a3c98b8b
MH
32
33/*
7e6fdd4b
RV
34 * devfreq core provides delayed work based load monitoring helper
35 * functions. Governors can use these or can implement their own
36 * monitoring mechanism.
a3c98b8b 37 */
a3c98b8b 38static struct workqueue_struct *devfreq_wq;
a3c98b8b 39
3aa173b8
NM
40/* The list of all device-devfreq governors */
41static LIST_HEAD(devfreq_governor_list);
a3c98b8b
MH
42/* 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
9348da2f 57 if (IS_ERR_OR_NULL(dev)) {
a3c98b8b
MH
58 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
e552bbaf
JL
72/**
73 * 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
0ec09ac2
CC
88/**
89 * 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
0ec09ac2
CC
114 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;
0ec09ac2
CC
119 return;
120 }
8a31d9d9 121 dev_pm_opp_put(opp);
0ec09ac2
CC
122 profile->freq_table[i] = freq;
123 }
0ec09ac2
CC
124}
125
e552bbaf
JL
126/**
127 * devfreq_update_status() - Update statistics of devfreq behavior
128 * @devfreq: the devfreq instance
129 * @freq: the update target frequency
130 */
30582c25 131int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
e552bbaf 132{
e35d35a1 133 int lev, prev_lev, ret = 0;
e552bbaf
JL
134 unsigned long cur_time;
135
e552bbaf 136 cur_time = jiffies;
e35d35a1 137
d0563a03
TJ
138 /* Immediately exit if previous_freq is not initialized yet. */
139 if (!devfreq->previous_freq)
140 goto out;
141
e35d35a1
SK
142 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] +=
e552bbaf 149 cur_time - devfreq->last_stat_updated;
e35d35a1
SK
150
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) {
e552bbaf
JL
158 devfreq->trans_table[(prev_lev *
159 devfreq->profile->max_state) + lev]++;
160 devfreq->total_trans++;
161 }
e552bbaf 162
e35d35a1
SK
163out:
164 devfreq->last_stat_updated = cur_time;
165 return ret;
e552bbaf 166}
30582c25 167EXPORT_SYMBOL(devfreq_update_status);
e552bbaf 168
3aa173b8
NM
169/**
170 * find_devfreq_governor() - find devfreq governor from name
171 * @name: name of the governor
172 *
173 * Search the list of devfreq governors and return the matched
174 * governor's pointer. devfreq_list_lock should be held by the caller.
175 */
176static struct devfreq_governor *find_devfreq_governor(const char *name)
177{
178 struct devfreq_governor *tmp_governor;
179
9348da2f 180 if (IS_ERR_OR_NULL(name)) {
3aa173b8
NM
181 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
182 return ERR_PTR(-EINVAL);
183 }
184 WARN(!mutex_is_locked(&devfreq_list_lock),
185 "devfreq_list_lock must be locked.");
186
187 list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
188 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
189 return tmp_governor;
190 }
191
192 return ERR_PTR(-ENODEV);
193}
194
0fe3a664
CC
195static int devfreq_notify_transition(struct devfreq *devfreq,
196 struct devfreq_freqs *freqs, unsigned int state)
197{
198 if (!devfreq)
199 return -EINVAL;
200
201 switch (state) {
202 case DEVFREQ_PRECHANGE:
203 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
204 DEVFREQ_PRECHANGE, freqs);
205 break;
206
207 case DEVFREQ_POSTCHANGE:
208 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
209 DEVFREQ_POSTCHANGE, freqs);
210 break;
211 default:
212 return -EINVAL;
213 }
214
215 return 0;
216}
217
7e6fdd4b
RV
218/* Load monitoring helper functions for governors use */
219
a3c98b8b
MH
220/**
221 * update_devfreq() - Reevaluate the device and configure frequency.
222 * @devfreq: the devfreq instance.
223 *
224 * Note: Lock devfreq->lock before calling update_devfreq
225 * This function is exported for governors.
226 */
227int update_devfreq(struct devfreq *devfreq)
228{
0fe3a664
CC
229 struct devfreq_freqs freqs;
230 unsigned long freq, cur_freq;
a3c98b8b 231 int err = 0;
ab5f299f 232 u32 flags = 0;
a3c98b8b
MH
233
234 if (!mutex_is_locked(&devfreq->lock)) {
235 WARN(true, "devfreq->lock must be locked by the caller.\n");
236 return -EINVAL;
237 }
238
1b5c1be2
NM
239 if (!devfreq->governor)
240 return -EINVAL;
241
a3c98b8b
MH
242 /* Reevaluate the proper frequency */
243 err = devfreq->governor->get_target_freq(devfreq, &freq);
244 if (err)
245 return err;
246
ab5f299f 247 /*
d3b7e174 248 * Adjust the frequency with user freq and QoS.
ab5f299f 249 *
d3b7e174
JM
250 * List from the highest priority
251 * max_freq
ab5f299f
MH
252 * min_freq
253 */
254
255 if (devfreq->min_freq && freq < devfreq->min_freq) {
256 freq = devfreq->min_freq;
257 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
258 }
259 if (devfreq->max_freq && freq > devfreq->max_freq) {
260 freq = devfreq->max_freq;
261 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
262 }
263
0fe3a664
CC
264 if (devfreq->profile->get_cur_freq)
265 devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
266 else
267 cur_freq = devfreq->previous_freq;
268
269 freqs.old = cur_freq;
270 freqs.new = freq;
271 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
272
ab5f299f 273 err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
0d37189e
CC
274 if (err) {
275 freqs.new = cur_freq;
276 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
a3c98b8b 277 return err;
0d37189e 278 }
a3c98b8b 279
0fe3a664
CC
280 freqs.new = freq;
281 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
282
e552bbaf
JL
283 if (devfreq->profile->freq_table)
284 if (devfreq_update_status(devfreq, freq))
285 dev_err(&devfreq->dev,
286 "Couldn't update frequency transition information.\n");
287
a3c98b8b
MH
288 devfreq->previous_freq = freq;
289 return err;
290}
2df5021f 291EXPORT_SYMBOL(update_devfreq);
a3c98b8b 292
7e6fdd4b
RV
293/**
294 * devfreq_monitor() - Periodically poll devfreq objects.
295 * @work: the work struct used to run devfreq_monitor periodically.
296 *
297 */
298static void devfreq_monitor(struct work_struct *work)
299{
300 int err;
301 struct devfreq *devfreq = container_of(work,
302 struct devfreq, work.work);
303
304 mutex_lock(&devfreq->lock);
305 err = update_devfreq(devfreq);
306 if (err)
307 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
308
309 queue_delayed_work(devfreq_wq, &devfreq->work,
310 msecs_to_jiffies(devfreq->profile->polling_ms));
311 mutex_unlock(&devfreq->lock);
312}
313
314/**
315 * devfreq_monitor_start() - Start load monitoring of devfreq instance
316 * @devfreq: the devfreq instance.
317 *
318 * Helper function for starting devfreq device load monitoing. By
319 * default delayed work based monitoring is supported. Function
320 * to be called from governor in response to DEVFREQ_GOV_START
321 * event when device is added to devfreq framework.
322 */
323void devfreq_monitor_start(struct devfreq *devfreq)
324{
325 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
326 if (devfreq->profile->polling_ms)
327 queue_delayed_work(devfreq_wq, &devfreq->work,
328 msecs_to_jiffies(devfreq->profile->polling_ms));
329}
6dcdd8e3 330EXPORT_SYMBOL(devfreq_monitor_start);
7e6fdd4b
RV
331
332/**
333 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
334 * @devfreq: the devfreq instance.
335 *
336 * Helper function to stop devfreq device load monitoing. Function
337 * to be called from governor in response to DEVFREQ_GOV_STOP
338 * event when device is removed from devfreq framework.
339 */
340void devfreq_monitor_stop(struct devfreq *devfreq)
341{
342 cancel_delayed_work_sync(&devfreq->work);
343}
6dcdd8e3 344EXPORT_SYMBOL(devfreq_monitor_stop);
7e6fdd4b
RV
345
346/**
347 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
348 * @devfreq: the devfreq instance.
349 *
350 * Helper function to suspend devfreq device load monitoing. Function
351 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
352 * event or when polling interval is set to zero.
353 *
354 * Note: Though this function is same as devfreq_monitor_stop(),
355 * intentionally kept separate to provide hooks for collecting
356 * transition statistics.
357 */
358void devfreq_monitor_suspend(struct devfreq *devfreq)
359{
360 mutex_lock(&devfreq->lock);
361 if (devfreq->stop_polling) {
362 mutex_unlock(&devfreq->lock);
363 return;
364 }
365
39688ce6 366 devfreq_update_status(devfreq, devfreq->previous_freq);
7e6fdd4b
RV
367 devfreq->stop_polling = true;
368 mutex_unlock(&devfreq->lock);
369 cancel_delayed_work_sync(&devfreq->work);
370}
6dcdd8e3 371EXPORT_SYMBOL(devfreq_monitor_suspend);
7e6fdd4b
RV
372
373/**
374 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
375 * @devfreq: the devfreq instance.
376 *
377 * Helper function to resume devfreq device load monitoing. Function
378 * to be called from governor in response to DEVFREQ_GOV_RESUME
379 * event or when polling interval is set to non-zero.
380 */
381void devfreq_monitor_resume(struct devfreq *devfreq)
382{
39688ce6
RV
383 unsigned long freq;
384
7e6fdd4b
RV
385 mutex_lock(&devfreq->lock);
386 if (!devfreq->stop_polling)
387 goto out;
388
389 if (!delayed_work_pending(&devfreq->work) &&
390 devfreq->profile->polling_ms)
391 queue_delayed_work(devfreq_wq, &devfreq->work,
392 msecs_to_jiffies(devfreq->profile->polling_ms));
39688ce6
RV
393
394 devfreq->last_stat_updated = jiffies;
7e6fdd4b
RV
395 devfreq->stop_polling = false;
396
39688ce6
RV
397 if (devfreq->profile->get_cur_freq &&
398 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
399 devfreq->previous_freq = freq;
400
7e6fdd4b
RV
401out:
402 mutex_unlock(&devfreq->lock);
403}
6dcdd8e3 404EXPORT_SYMBOL(devfreq_monitor_resume);
7e6fdd4b
RV
405
406/**
407 * devfreq_interval_update() - Update device devfreq monitoring interval
408 * @devfreq: the devfreq instance.
409 * @delay: new polling interval to be set.
410 *
411 * Helper function to set new load monitoring polling interval. Function
412 * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
413 */
414void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
415{
416 unsigned int cur_delay = devfreq->profile->polling_ms;
417 unsigned int new_delay = *delay;
418
419 mutex_lock(&devfreq->lock);
420 devfreq->profile->polling_ms = new_delay;
421
422 if (devfreq->stop_polling)
423 goto out;
424
425 /* if new delay is zero, stop polling */
426 if (!new_delay) {
427 mutex_unlock(&devfreq->lock);
428 cancel_delayed_work_sync(&devfreq->work);
429 return;
430 }
431
432 /* if current delay is zero, start polling with new delay */
433 if (!cur_delay) {
434 queue_delayed_work(devfreq_wq, &devfreq->work,
435 msecs_to_jiffies(devfreq->profile->polling_ms));
436 goto out;
437 }
438
439 /* if current delay is greater than new delay, restart polling */
440 if (cur_delay > new_delay) {
441 mutex_unlock(&devfreq->lock);
442 cancel_delayed_work_sync(&devfreq->work);
443 mutex_lock(&devfreq->lock);
444 if (!devfreq->stop_polling)
445 queue_delayed_work(devfreq_wq, &devfreq->work,
446 msecs_to_jiffies(devfreq->profile->polling_ms));
447 }
448out:
449 mutex_unlock(&devfreq->lock);
450}
6dcdd8e3 451EXPORT_SYMBOL(devfreq_interval_update);
a3c98b8b
MH
452
453/**
454 * devfreq_notifier_call() - Notify that the device frequency requirements
455 * has been changed out of devfreq framework.
c5b4a1c1
NM
456 * @nb: the notifier_block (supposed to be devfreq->nb)
457 * @type: not used
458 * @devp: not used
a3c98b8b
MH
459 *
460 * Called by a notifier that uses devfreq->nb.
461 */
462static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
463 void *devp)
464{
465 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
466 int ret;
467
468 mutex_lock(&devfreq->lock);
469 ret = update_devfreq(devfreq);
470 mutex_unlock(&devfreq->lock);
471
472 return ret;
473}
474
475/**
29b6968b
CC
476 * devfreq_dev_release() - Callback for struct device to release the device.
477 * @dev: the devfreq device
478 *
479 * Remove devfreq from the list and release its resources.
a3c98b8b 480 */
29b6968b 481static void devfreq_dev_release(struct device *dev)
a3c98b8b 482{
29b6968b
CC
483 struct devfreq *devfreq = to_devfreq(dev);
484
7e6fdd4b
RV
485 mutex_lock(&devfreq_list_lock);
486 if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
487 mutex_unlock(&devfreq_list_lock);
488 dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
a3c98b8b
MH
489 return;
490 }
7e6fdd4b
RV
491 list_del(&devfreq->node);
492 mutex_unlock(&devfreq_list_lock);
a3c98b8b 493
1b5c1be2
NM
494 if (devfreq->governor)
495 devfreq->governor->event_handler(devfreq,
496 DEVFREQ_GOV_STOP, NULL);
a3c98b8b
MH
497
498 if (devfreq->profile->exit)
499 devfreq->profile->exit(devfreq->dev.parent);
500
a3c98b8b 501 mutex_destroy(&devfreq->lock);
a3c98b8b
MH
502 kfree(devfreq);
503}
504
a3c98b8b
MH
505/**
506 * devfreq_add_device() - Add devfreq feature to the device
507 * @dev: the device to add devfreq feature.
508 * @profile: device-specific profile to run devfreq.
1b5c1be2 509 * @governor_name: name of the policy to choose frequency.
a3c98b8b
MH
510 * @data: private data for the governor. The devfreq framework does not
511 * touch this value.
512 */
513struct devfreq *devfreq_add_device(struct device *dev,
514 struct devfreq_dev_profile *profile,
1b5c1be2 515 const char *governor_name,
a3c98b8b
MH
516 void *data)
517{
518 struct devfreq *devfreq;
1b5c1be2 519 struct devfreq_governor *governor;
4585fbcb 520 static atomic_t devfreq_no = ATOMIC_INIT(-1);
a3c98b8b
MH
521 int err = 0;
522
1b5c1be2 523 if (!dev || !profile || !governor_name) {
a3c98b8b
MH
524 dev_err(dev, "%s: Invalid parameters.\n", __func__);
525 return ERR_PTR(-EINVAL);
526 }
527
7e6fdd4b
RV
528 mutex_lock(&devfreq_list_lock);
529 devfreq = find_device_devfreq(dev);
530 mutex_unlock(&devfreq_list_lock);
531 if (!IS_ERR(devfreq)) {
9d0109be
CC
532 dev_err(dev, "%s: Unable to create devfreq for the device.\n",
533 __func__);
7e6fdd4b
RV
534 err = -EINVAL;
535 goto err_out;
a3c98b8b
MH
536 }
537
538 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
539 if (!devfreq) {
a3c98b8b 540 err = -ENOMEM;
3f19f08a 541 goto err_out;
a3c98b8b
MH
542 }
543
544 mutex_init(&devfreq->lock);
545 mutex_lock(&devfreq->lock);
546 devfreq->dev.parent = dev;
547 devfreq->dev.class = devfreq_class;
548 devfreq->dev.release = devfreq_dev_release;
549 devfreq->profile = profile;
1b5c1be2 550 strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
a3c98b8b 551 devfreq->previous_freq = profile->initial_freq;
8d39fc08 552 devfreq->last_status.current_frequency = profile->initial_freq;
a3c98b8b 553 devfreq->data = data;
a3c98b8b
MH
554 devfreq->nb.notifier_call = devfreq_notifier_call;
555
0ec09ac2
CC
556 if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
557 mutex_unlock(&devfreq->lock);
558 devfreq_set_freq_table(devfreq);
559 mutex_lock(&devfreq->lock);
560 }
561
4585fbcb
CC
562 dev_set_name(&devfreq->dev, "devfreq%d",
563 atomic_inc_return(&devfreq_no));
a3c98b8b
MH
564 err = device_register(&devfreq->dev);
565 if (err) {
7e6fdd4b 566 mutex_unlock(&devfreq->lock);
6d3cbfa7 567 goto err_out;
a3c98b8b
MH
568 }
569
9d0109be
CC
570 devfreq->trans_table = devm_kzalloc(&devfreq->dev,
571 sizeof(unsigned int) *
3e1d7fb0
MH
572 devfreq->profile->max_state *
573 devfreq->profile->max_state,
574 GFP_KERNEL);
9d0109be
CC
575 devfreq->time_in_state = devm_kzalloc(&devfreq->dev,
576 sizeof(unsigned long) *
3e1d7fb0
MH
577 devfreq->profile->max_state,
578 GFP_KERNEL);
579 devfreq->last_stat_updated = jiffies;
580
0fe3a664
CC
581 srcu_init_notifier_head(&devfreq->transition_notifier_list);
582
a3c98b8b
MH
583 mutex_unlock(&devfreq->lock);
584
a3c98b8b 585 mutex_lock(&devfreq_list_lock);
a3c98b8b
MH
586 list_add(&devfreq->node, &devfreq_list);
587
1b5c1be2 588 governor = find_devfreq_governor(devfreq->governor_name);
73613b16
CC
589 if (IS_ERR(governor)) {
590 dev_err(dev, "%s: Unable to find governor for the device\n",
591 __func__);
592 err = PTR_ERR(governor);
593 goto err_init;
594 }
595
596 devfreq->governor = governor;
597 err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
598 NULL);
7e6fdd4b
RV
599 if (err) {
600 dev_err(dev, "%s: Unable to start governor for the device\n",
601 __func__);
602 goto err_init;
a3c98b8b 603 }
0f376c9c 604 mutex_unlock(&devfreq_list_lock);
7e6fdd4b 605
3f19f08a
AL
606 return devfreq;
607
a3c98b8b 608err_init:
7e6fdd4b 609 list_del(&devfreq->node);
0f376c9c
AL
610 mutex_unlock(&devfreq_list_lock);
611
a3c98b8b 612 device_unregister(&devfreq->dev);
3f19f08a
AL
613err_out:
614 return ERR_PTR(err);
a3c98b8b 615}
7e6fdd4b 616EXPORT_SYMBOL(devfreq_add_device);
a3c98b8b
MH
617
618/**
619 * devfreq_remove_device() - Remove devfreq feature from a device.
c5b4a1c1 620 * @devfreq: the devfreq instance to be removed
de9c7394
MH
621 *
622 * The opposite of devfreq_add_device().
a3c98b8b
MH
623 */
624int devfreq_remove_device(struct devfreq *devfreq)
625{
626 if (!devfreq)
627 return -EINVAL;
628
585fc83e 629 device_unregister(&devfreq->dev);
9f3bdd4f 630
a3c98b8b
MH
631 return 0;
632}
7e6fdd4b 633EXPORT_SYMBOL(devfreq_remove_device);
a3c98b8b 634
8cd84092
CC
635static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
636{
637 struct devfreq **r = res;
638
639 if (WARN_ON(!r || !*r))
640 return 0;
641
642 return *r == data;
643}
644
645static void devm_devfreq_dev_release(struct device *dev, void *res)
646{
647 devfreq_remove_device(*(struct devfreq **)res);
648}
649
650/**
651 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
652 * @dev: the device to add devfreq feature.
653 * @profile: device-specific profile to run devfreq.
654 * @governor_name: name of the policy to choose frequency.
655 * @data: private data for the governor. The devfreq framework does not
656 * touch this value.
657 *
658 * This function manages automatically the memory of devfreq device using device
659 * resource management and simplify the free operation for memory of devfreq
660 * device.
661 */
662struct devfreq *devm_devfreq_add_device(struct device *dev,
663 struct devfreq_dev_profile *profile,
664 const char *governor_name,
665 void *data)
666{
667 struct devfreq **ptr, *devfreq;
668
669 ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
670 if (!ptr)
671 return ERR_PTR(-ENOMEM);
672
673 devfreq = devfreq_add_device(dev, profile, governor_name, data);
674 if (IS_ERR(devfreq)) {
675 devres_free(ptr);
676 return ERR_PTR(-ENOMEM);
677 }
678
679 *ptr = devfreq;
680 devres_add(dev, ptr);
681
682 return devfreq;
683}
684EXPORT_SYMBOL(devm_devfreq_add_device);
685
8f510aeb
CC
686#ifdef CONFIG_OF
687/*
688 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
689 * @dev - instance to the given device
690 * @index - index into list of devfreq
691 *
692 * return the instance of devfreq device
693 */
694struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
695{
696 struct device_node *node;
697 struct devfreq *devfreq;
698
699 if (!dev)
700 return ERR_PTR(-EINVAL);
701
702 if (!dev->of_node)
703 return ERR_PTR(-EINVAL);
704
705 node = of_parse_phandle(dev->of_node, "devfreq", index);
706 if (!node)
707 return ERR_PTR(-ENODEV);
708
709 mutex_lock(&devfreq_list_lock);
710 list_for_each_entry(devfreq, &devfreq_list, node) {
711 if (devfreq->dev.parent
712 && devfreq->dev.parent->of_node == node) {
713 mutex_unlock(&devfreq_list_lock);
3427c6f0 714 of_node_put(node);
8f510aeb
CC
715 return devfreq;
716 }
717 }
718 mutex_unlock(&devfreq_list_lock);
3427c6f0 719 of_node_put(node);
8f510aeb
CC
720
721 return ERR_PTR(-EPROBE_DEFER);
722}
723#else
724struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
725{
726 return ERR_PTR(-ENODEV);
727}
728#endif /* CONFIG_OF */
729EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
730
8cd84092
CC
731/**
732 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
733 * @dev: the device to add devfreq feature.
734 * @devfreq: the devfreq instance to be removed
735 */
736void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
737{
738 WARN_ON(devres_release(dev, devm_devfreq_dev_release,
739 devm_devfreq_dev_match, devfreq));
740}
741EXPORT_SYMBOL(devm_devfreq_remove_device);
742
206c30cf
RV
743/**
744 * devfreq_suspend_device() - Suspend devfreq of a device.
745 * @devfreq: the devfreq instance to be suspended
de9c7394
MH
746 *
747 * This function is intended to be called by the pm callbacks
748 * (e.g., runtime_suspend, suspend) of the device driver that
749 * holds the devfreq.
206c30cf
RV
750 */
751int devfreq_suspend_device(struct devfreq *devfreq)
752{
a3c98b8b
MH
753 if (!devfreq)
754 return -EINVAL;
755
1b5c1be2
NM
756 if (!devfreq->governor)
757 return 0;
758
206c30cf
RV
759 return devfreq->governor->event_handler(devfreq,
760 DEVFREQ_GOV_SUSPEND, NULL);
761}
762EXPORT_SYMBOL(devfreq_suspend_device);
763
764/**
765 * devfreq_resume_device() - Resume devfreq of a device.
766 * @devfreq: the devfreq instance to be resumed
de9c7394
MH
767 *
768 * This function is intended to be called by the pm callbacks
769 * (e.g., runtime_resume, resume) of the device driver that
770 * holds the devfreq.
206c30cf
RV
771 */
772int devfreq_resume_device(struct devfreq *devfreq)
773{
774 if (!devfreq)
775 return -EINVAL;
776
1b5c1be2
NM
777 if (!devfreq->governor)
778 return 0;
779
206c30cf
RV
780 return devfreq->governor->event_handler(devfreq,
781 DEVFREQ_GOV_RESUME, NULL);
782}
783EXPORT_SYMBOL(devfreq_resume_device);
784
3aa173b8
NM
785/**
786 * devfreq_add_governor() - Add devfreq governor
787 * @governor: the devfreq governor to be added
788 */
789int devfreq_add_governor(struct devfreq_governor *governor)
790{
791 struct devfreq_governor *g;
1b5c1be2 792 struct devfreq *devfreq;
3aa173b8
NM
793 int err = 0;
794
795 if (!governor) {
796 pr_err("%s: Invalid parameters.\n", __func__);
797 return -EINVAL;
798 }
799
800 mutex_lock(&devfreq_list_lock);
801 g = find_devfreq_governor(governor->name);
802 if (!IS_ERR(g)) {
803 pr_err("%s: governor %s already registered\n", __func__,
804 g->name);
805 err = -EINVAL;
806 goto err_out;
807 }
9f3bdd4f 808
3aa173b8
NM
809 list_add(&governor->node, &devfreq_governor_list);
810
1b5c1be2
NM
811 list_for_each_entry(devfreq, &devfreq_list, node) {
812 int ret = 0;
813 struct device *dev = devfreq->dev.parent;
814
815 if (!strncmp(devfreq->governor_name, governor->name,
816 DEVFREQ_NAME_LEN)) {
817 /* The following should never occur */
818 if (devfreq->governor) {
819 dev_warn(dev,
820 "%s: Governor %s already present\n",
821 __func__, devfreq->governor->name);
822 ret = devfreq->governor->event_handler(devfreq,
823 DEVFREQ_GOV_STOP, NULL);
824 if (ret) {
825 dev_warn(dev,
826 "%s: Governor %s stop = %d\n",
827 __func__,
828 devfreq->governor->name, ret);
829 }
830 /* Fall through */
831 }
832 devfreq->governor = governor;
833 ret = devfreq->governor->event_handler(devfreq,
834 DEVFREQ_GOV_START, NULL);
835 if (ret) {
836 dev_warn(dev, "%s: Governor %s start=%d\n",
837 __func__, devfreq->governor->name,
838 ret);
839 }
a3c98b8b
MH
840 }
841 }
842
3aa173b8
NM
843err_out:
844 mutex_unlock(&devfreq_list_lock);
a3c98b8b 845
3aa173b8
NM
846 return err;
847}
848EXPORT_SYMBOL(devfreq_add_governor);
a3c98b8b 849
3aa173b8 850/**
bafeb42b 851 * devfreq_remove_governor() - Remove devfreq feature from a device.
3aa173b8
NM
852 * @governor: the devfreq governor to be removed
853 */
854int devfreq_remove_governor(struct devfreq_governor *governor)
855{
856 struct devfreq_governor *g;
1b5c1be2 857 struct devfreq *devfreq;
3aa173b8
NM
858 int err = 0;
859
860 if (!governor) {
861 pr_err("%s: Invalid parameters.\n", __func__);
862 return -EINVAL;
863 }
864
865 mutex_lock(&devfreq_list_lock);
866 g = find_devfreq_governor(governor->name);
867 if (IS_ERR(g)) {
868 pr_err("%s: governor %s not registered\n", __func__,
b9e1c8e8 869 governor->name);
f9c08e2a 870 err = PTR_ERR(g);
3aa173b8
NM
871 goto err_out;
872 }
1b5c1be2
NM
873 list_for_each_entry(devfreq, &devfreq_list, node) {
874 int ret;
875 struct device *dev = devfreq->dev.parent;
876
877 if (!strncmp(devfreq->governor_name, governor->name,
878 DEVFREQ_NAME_LEN)) {
879 /* we should have a devfreq governor! */
880 if (!devfreq->governor) {
881 dev_warn(dev, "%s: Governor %s NOT present\n",
882 __func__, governor->name);
883 continue;
884 /* Fall through */
885 }
886 ret = devfreq->governor->event_handler(devfreq,
887 DEVFREQ_GOV_STOP, NULL);
888 if (ret) {
889 dev_warn(dev, "%s: Governor %s stop=%d\n",
890 __func__, devfreq->governor->name,
891 ret);
892 }
893 devfreq->governor = NULL;
894 }
895 }
3aa173b8
NM
896
897 list_del(&governor->node);
898err_out:
899 mutex_unlock(&devfreq_list_lock);
900
901 return err;
a3c98b8b 902}
3aa173b8 903EXPORT_SYMBOL(devfreq_remove_governor);
a3c98b8b 904
a93d6b0a 905static ssize_t governor_show(struct device *dev,
9005b650
MH
906 struct device_attribute *attr, char *buf)
907{
1b5c1be2
NM
908 if (!to_devfreq(dev)->governor)
909 return -EINVAL;
910
9005b650
MH
911 return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
912}
913
a93d6b0a 914static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
0359d1af
NM
915 const char *buf, size_t count)
916{
917 struct devfreq *df = to_devfreq(dev);
918 int ret;
919 char str_governor[DEVFREQ_NAME_LEN + 1];
920 struct devfreq_governor *governor;
921
922 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
923 if (ret != 1)
924 return -EINVAL;
925
926 mutex_lock(&devfreq_list_lock);
927 governor = find_devfreq_governor(str_governor);
928 if (IS_ERR(governor)) {
929 ret = PTR_ERR(governor);
930 goto out;
931 }
14a21e7b
TJ
932 if (df->governor == governor) {
933 ret = 0;
0359d1af 934 goto out;
bcf23c79
CC
935 } else if (df->governor->immutable || governor->immutable) {
936 ret = -EINVAL;
937 goto out;
14a21e7b 938 }
0359d1af
NM
939
940 if (df->governor) {
941 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
942 if (ret) {
943 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
944 __func__, df->governor->name, ret);
945 goto out;
946 }
947 }
948 df->governor = governor;
949 strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
950 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
951 if (ret)
952 dev_warn(dev, "%s: Governor %s not started(%d)\n",
953 __func__, df->governor->name, ret);
954out:
955 mutex_unlock(&devfreq_list_lock);
956
957 if (!ret)
958 ret = count;
959 return ret;
960}
a93d6b0a
GKH
961static DEVICE_ATTR_RW(governor);
962
963static ssize_t available_governors_show(struct device *d,
964 struct device_attribute *attr,
965 char *buf)
50a5b33e 966{
bcf23c79 967 struct devfreq *df = to_devfreq(d);
50a5b33e
NM
968 ssize_t count = 0;
969
970 mutex_lock(&devfreq_list_lock);
bcf23c79
CC
971
972 /*
973 * The devfreq with immutable governor (e.g., passive) shows
974 * only own governor.
975 */
976 if (df->governor->immutable) {
977 count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
978 "%s ", df->governor_name);
979 /*
980 * The devfreq device shows the registered governor except for
981 * immutable governors such as passive governor .
982 */
983 } else {
984 struct devfreq_governor *governor;
985
986 list_for_each_entry(governor, &devfreq_governor_list, node) {
987 if (governor->immutable)
988 continue;
989 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
990 "%s ", governor->name);
991 }
992 }
993
50a5b33e
NM
994 mutex_unlock(&devfreq_list_lock);
995
996 /* Truncate the trailing space */
997 if (count)
998 count--;
999
1000 count += sprintf(&buf[count], "\n");
1001
1002 return count;
1003}
a93d6b0a 1004static DEVICE_ATTR_RO(available_governors);
0359d1af 1005
a93d6b0a
GKH
1006static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
1007 char *buf)
7f98a905
RV
1008{
1009 unsigned long freq;
1010 struct devfreq *devfreq = to_devfreq(dev);
1011
1012 if (devfreq->profile->get_cur_freq &&
1013 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
9d0109be 1014 return sprintf(buf, "%lu\n", freq);
7f98a905
RV
1015
1016 return sprintf(buf, "%lu\n", devfreq->previous_freq);
1017}
a93d6b0a 1018static DEVICE_ATTR_RO(cur_freq);
7f98a905 1019
a93d6b0a
GKH
1020static ssize_t target_freq_show(struct device *dev,
1021 struct device_attribute *attr, char *buf)
9005b650
MH
1022{
1023 return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
1024}
a93d6b0a 1025static DEVICE_ATTR_RO(target_freq);
9005b650 1026
a93d6b0a 1027static ssize_t polling_interval_show(struct device *dev,
9005b650
MH
1028 struct device_attribute *attr, char *buf)
1029{
1030 return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
1031}
1032
a93d6b0a 1033static ssize_t polling_interval_store(struct device *dev,
9005b650
MH
1034 struct device_attribute *attr,
1035 const char *buf, size_t count)
1036{
1037 struct devfreq *df = to_devfreq(dev);
1038 unsigned int value;
1039 int ret;
1040
1b5c1be2
NM
1041 if (!df->governor)
1042 return -EINVAL;
1043
9005b650
MH
1044 ret = sscanf(buf, "%u", &value);
1045 if (ret != 1)
12e26265 1046 return -EINVAL;
9005b650 1047
7e6fdd4b 1048 df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
9005b650
MH
1049 ret = count;
1050
9005b650
MH
1051 return ret;
1052}
a93d6b0a 1053static DEVICE_ATTR_RW(polling_interval);
9005b650 1054
a93d6b0a 1055static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
6530b9de
MH
1056 const char *buf, size_t count)
1057{
1058 struct devfreq *df = to_devfreq(dev);
1059 unsigned long value;
1060 int ret;
1061 unsigned long max;
1062
1063 ret = sscanf(buf, "%lu", &value);
1064 if (ret != 1)
12e26265 1065 return -EINVAL;
6530b9de
MH
1066
1067 mutex_lock(&df->lock);
1068 max = df->max_freq;
1069 if (value && max && value > max) {
1070 ret = -EINVAL;
1071 goto unlock;
1072 }
1073
1074 df->min_freq = value;
1075 update_devfreq(df);
1076 ret = count;
1077unlock:
1078 mutex_unlock(&df->lock);
6530b9de
MH
1079 return ret;
1080}
1081
a93d6b0a 1082static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
6530b9de
MH
1083 const char *buf, size_t count)
1084{
1085 struct devfreq *df = to_devfreq(dev);
1086 unsigned long value;
1087 int ret;
1088 unsigned long min;
1089
1090 ret = sscanf(buf, "%lu", &value);
1091 if (ret != 1)
12e26265 1092 return -EINVAL;
6530b9de
MH
1093
1094 mutex_lock(&df->lock);
1095 min = df->min_freq;
1096 if (value && min && value < min) {
1097 ret = -EINVAL;
1098 goto unlock;
1099 }
1100
1101 df->max_freq = value;
1102 update_devfreq(df);
1103 ret = count;
1104unlock:
1105 mutex_unlock(&df->lock);
6530b9de
MH
1106 return ret;
1107}
1108
3104fa30
CC
1109#define show_one(name) \
1110static ssize_t name##_show \
1111(struct device *dev, struct device_attribute *attr, char *buf) \
1112{ \
1113 return sprintf(buf, "%lu\n", to_devfreq(dev)->name); \
6530b9de 1114}
3104fa30
CC
1115show_one(min_freq);
1116show_one(max_freq);
1117
1118static DEVICE_ATTR_RW(min_freq);
a93d6b0a 1119static DEVICE_ATTR_RW(max_freq);
6530b9de 1120
a93d6b0a
GKH
1121static ssize_t available_frequencies_show(struct device *d,
1122 struct device_attribute *attr,
1123 char *buf)
d287de85
NM
1124{
1125 struct devfreq *df = to_devfreq(d);
1126 struct device *dev = df->dev.parent;
47d43ba7 1127 struct dev_pm_opp *opp;
d287de85
NM
1128 ssize_t count = 0;
1129 unsigned long freq = 0;
1130
d287de85 1131 do {
5d4879cd 1132 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
d287de85
NM
1133 if (IS_ERR(opp))
1134 break;
1135
8a31d9d9 1136 dev_pm_opp_put(opp);
d287de85
NM
1137 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1138 "%lu ", freq);
1139 freq++;
1140 } while (1);
d287de85
NM
1141
1142 /* Truncate the trailing space */
1143 if (count)
1144 count--;
1145
1146 count += sprintf(&buf[count], "\n");
1147
1148 return count;
1149}
a93d6b0a 1150static DEVICE_ATTR_RO(available_frequencies);
d287de85 1151
a93d6b0a
GKH
1152static ssize_t trans_stat_show(struct device *dev,
1153 struct device_attribute *attr, char *buf)
e552bbaf
JL
1154{
1155 struct devfreq *devfreq = to_devfreq(dev);
1156 ssize_t len;
39688ce6 1157 int i, j;
e552bbaf
JL
1158 unsigned int max_state = devfreq->profile->max_state;
1159
39688ce6
RV
1160 if (!devfreq->stop_polling &&
1161 devfreq_update_status(devfreq, devfreq->previous_freq))
e552bbaf 1162 return 0;
34bd3220
MH
1163 if (max_state == 0)
1164 return sprintf(buf, "Not Supported.\n");
e552bbaf 1165
d7df1e46
CC
1166 len = sprintf(buf, " From : To\n");
1167 len += sprintf(buf + len, " :");
e552bbaf 1168 for (i = 0; i < max_state; i++)
d7df1e46 1169 len += sprintf(buf + len, "%10lu",
e552bbaf
JL
1170 devfreq->profile->freq_table[i]);
1171
1172 len += sprintf(buf + len, " time(ms)\n");
1173
1174 for (i = 0; i < max_state; i++) {
1175 if (devfreq->profile->freq_table[i]
1176 == devfreq->previous_freq) {
1177 len += sprintf(buf + len, "*");
1178 } else {
1179 len += sprintf(buf + len, " ");
1180 }
d7df1e46 1181 len += sprintf(buf + len, "%10lu:",
e552bbaf
JL
1182 devfreq->profile->freq_table[i]);
1183 for (j = 0; j < max_state; j++)
d7df1e46 1184 len += sprintf(buf + len, "%10u",
e552bbaf
JL
1185 devfreq->trans_table[(i * max_state) + j]);
1186 len += sprintf(buf + len, "%10u\n",
1187 jiffies_to_msecs(devfreq->time_in_state[i]));
1188 }
1189
1190 len += sprintf(buf + len, "Total transition : %u\n",
1191 devfreq->total_trans);
1192 return len;
1193}
a93d6b0a
GKH
1194static DEVICE_ATTR_RO(trans_stat);
1195
1196static struct attribute *devfreq_attrs[] = {
1197 &dev_attr_governor.attr,
1198 &dev_attr_available_governors.attr,
1199 &dev_attr_cur_freq.attr,
1200 &dev_attr_available_frequencies.attr,
1201 &dev_attr_target_freq.attr,
1202 &dev_attr_polling_interval.attr,
1203 &dev_attr_min_freq.attr,
1204 &dev_attr_max_freq.attr,
1205 &dev_attr_trans_stat.attr,
1206 NULL,
9005b650 1207};
a93d6b0a 1208ATTRIBUTE_GROUPS(devfreq);
9005b650 1209
a3c98b8b
MH
1210static int __init devfreq_init(void)
1211{
1212 devfreq_class = class_create(THIS_MODULE, "devfreq");
1213 if (IS_ERR(devfreq_class)) {
1214 pr_err("%s: couldn't create class\n", __FILE__);
1215 return PTR_ERR(devfreq_class);
1216 }
7e6fdd4b
RV
1217
1218 devfreq_wq = create_freezable_workqueue("devfreq_wq");
ea7f4548 1219 if (!devfreq_wq) {
7e6fdd4b
RV
1220 class_destroy(devfreq_class);
1221 pr_err("%s: couldn't create workqueue\n", __FILE__);
ea7f4548 1222 return -ENOMEM;
7e6fdd4b 1223 }
a93d6b0a 1224 devfreq_class->dev_groups = devfreq_groups;
7e6fdd4b 1225
a3c98b8b
MH
1226 return 0;
1227}
1228subsys_initcall(devfreq_init);
1229
a3c98b8b 1230/*
4091fb95 1231 * The following are helper functions for devfreq user device drivers with
a3c98b8b
MH
1232 * OPP framework.
1233 */
1234
1235/**
1236 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1237 * freq value given to target callback.
c5b4a1c1
NM
1238 * @dev: The devfreq user device. (parent of devfreq)
1239 * @freq: The frequency given to target function
1240 * @flags: Flags handed from devfreq framework.
a3c98b8b 1241 *
8a31d9d9
VK
1242 * The callers are required to call dev_pm_opp_put() for the returned OPP after
1243 * use.
a3c98b8b 1244 */
47d43ba7
NM
1245struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1246 unsigned long *freq,
1247 u32 flags)
a3c98b8b 1248{
47d43ba7 1249 struct dev_pm_opp *opp;
a3c98b8b 1250
ab5f299f
MH
1251 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1252 /* The freq is an upper bound. opp should be lower */
5d4879cd 1253 opp = dev_pm_opp_find_freq_floor(dev, freq);
ab5f299f
MH
1254
1255 /* If not available, use the closest opp */
0779726c 1256 if (opp == ERR_PTR(-ERANGE))
5d4879cd 1257 opp = dev_pm_opp_find_freq_ceil(dev, freq);
ab5f299f
MH
1258 } else {
1259 /* The freq is an lower bound. opp should be higher */
5d4879cd 1260 opp = dev_pm_opp_find_freq_ceil(dev, freq);
ab5f299f
MH
1261
1262 /* If not available, use the closest opp */
0779726c 1263 if (opp == ERR_PTR(-ERANGE))
5d4879cd 1264 opp = dev_pm_opp_find_freq_floor(dev, freq);
ab5f299f
MH
1265 }
1266
a3c98b8b
MH
1267 return opp;
1268}
bd7e9277 1269EXPORT_SYMBOL(devfreq_recommended_opp);
a3c98b8b
MH
1270
1271/**
1272 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1273 * for any changes in the OPP availability
1274 * changes
c5b4a1c1
NM
1275 * @dev: The devfreq user device. (parent of devfreq)
1276 * @devfreq: The devfreq object.
a3c98b8b
MH
1277 */
1278int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1279{
dc2c9ad5 1280 return dev_pm_opp_register_notifier(dev, &devfreq->nb);
a3c98b8b 1281}
bd7e9277 1282EXPORT_SYMBOL(devfreq_register_opp_notifier);
a3c98b8b
MH
1283
1284/**
1285 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1286 * notified for any changes in the OPP
1287 * availability changes anymore.
c5b4a1c1
NM
1288 * @dev: The devfreq user device. (parent of devfreq)
1289 * @devfreq: The devfreq object.
a3c98b8b
MH
1290 *
1291 * At exit() callback of devfreq_dev_profile, this must be included if
1292 * devfreq_recommended_opp is used.
1293 */
1294int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1295{
dc2c9ad5 1296 return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
a3c98b8b 1297}
bd7e9277 1298EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
a3c98b8b 1299
d5b040d0
CC
1300static void devm_devfreq_opp_release(struct device *dev, void *res)
1301{
1302 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
1303}
1304
1305/**
1306 * devm_ devfreq_register_opp_notifier()
1307 * - Resource-managed devfreq_register_opp_notifier()
1308 * @dev: The devfreq user device. (parent of devfreq)
1309 * @devfreq: The devfreq object.
1310 */
1311int devm_devfreq_register_opp_notifier(struct device *dev,
1312 struct devfreq *devfreq)
1313{
1314 struct devfreq **ptr;
1315 int ret;
1316
1317 ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
1318 if (!ptr)
1319 return -ENOMEM;
1320
1321 ret = devfreq_register_opp_notifier(dev, devfreq);
1322 if (ret) {
1323 devres_free(ptr);
1324 return ret;
1325 }
1326
1327 *ptr = devfreq;
1328 devres_add(dev, ptr);
1329
1330 return 0;
1331}
1332EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
1333
1334/**
1335 * devm_devfreq_unregister_opp_notifier()
1336 * - Resource-managed devfreq_unregister_opp_notifier()
1337 * @dev: The devfreq user device. (parent of devfreq)
1338 * @devfreq: The devfreq object.
1339 */
1340void devm_devfreq_unregister_opp_notifier(struct device *dev,
1341 struct devfreq *devfreq)
1342{
1343 WARN_ON(devres_release(dev, devm_devfreq_opp_release,
1344 devm_devfreq_dev_match, devfreq));
1345}
1346EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
1347
0fe3a664
CC
1348/**
1349 * devfreq_register_notifier() - Register a driver with devfreq
1350 * @devfreq: The devfreq object.
1351 * @nb: The notifier block to register.
1352 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1353 */
1354int devfreq_register_notifier(struct devfreq *devfreq,
1355 struct notifier_block *nb,
1356 unsigned int list)
1357{
1358 int ret = 0;
1359
1360 if (!devfreq)
1361 return -EINVAL;
1362
1363 switch (list) {
1364 case DEVFREQ_TRANSITION_NOTIFIER:
1365 ret = srcu_notifier_chain_register(
1366 &devfreq->transition_notifier_list, nb);
1367 break;
1368 default:
1369 ret = -EINVAL;
1370 }
1371
1372 return ret;
1373}
1374EXPORT_SYMBOL(devfreq_register_notifier);
1375
1376/*
1377 * devfreq_unregister_notifier() - Unregister a driver with devfreq
1378 * @devfreq: The devfreq object.
1379 * @nb: The notifier block to be unregistered.
1380 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1381 */
1382int devfreq_unregister_notifier(struct devfreq *devfreq,
1383 struct notifier_block *nb,
1384 unsigned int list)
1385{
1386 int ret = 0;
1387
1388 if (!devfreq)
1389 return -EINVAL;
1390
1391 switch (list) {
1392 case DEVFREQ_TRANSITION_NOTIFIER:
1393 ret = srcu_notifier_chain_unregister(
1394 &devfreq->transition_notifier_list, nb);
1395 break;
1396 default:
1397 ret = -EINVAL;
1398 }
1399
1400 return ret;
1401}
1402EXPORT_SYMBOL(devfreq_unregister_notifier);
1403
1404struct devfreq_notifier_devres {
1405 struct devfreq *devfreq;
1406 struct notifier_block *nb;
1407 unsigned int list;
1408};
1409
1410static void devm_devfreq_notifier_release(struct device *dev, void *res)
1411{
1412 struct devfreq_notifier_devres *this = res;
1413
1414 devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
1415}
1416
1417/**
1418 * devm_devfreq_register_notifier()
1419 - Resource-managed devfreq_register_notifier()
1420 * @dev: The devfreq user device. (parent of devfreq)
1421 * @devfreq: The devfreq object.
1422 * @nb: The notifier block to be unregistered.
1423 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1424 */
1425int devm_devfreq_register_notifier(struct device *dev,
1426 struct devfreq *devfreq,
1427 struct notifier_block *nb,
1428 unsigned int list)
1429{
1430 struct devfreq_notifier_devres *ptr;
1431 int ret;
1432
1433 ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
1434 GFP_KERNEL);
1435 if (!ptr)
1436 return -ENOMEM;
1437
1438 ret = devfreq_register_notifier(devfreq, nb, list);
1439 if (ret) {
1440 devres_free(ptr);
1441 return ret;
1442 }
1443
1444 ptr->devfreq = devfreq;
1445 ptr->nb = nb;
1446 ptr->list = list;
1447 devres_add(dev, ptr);
1448
1449 return 0;
1450}
1451EXPORT_SYMBOL(devm_devfreq_register_notifier);
1452
1453/**
1454 * devm_devfreq_unregister_notifier()
1455 - Resource-managed devfreq_unregister_notifier()
1456 * @dev: The devfreq user device. (parent of devfreq)
1457 * @devfreq: The devfreq object.
1458 * @nb: The notifier block to be unregistered.
1459 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1460 */
1461void devm_devfreq_unregister_notifier(struct device *dev,
1462 struct devfreq *devfreq,
1463 struct notifier_block *nb,
1464 unsigned int list)
1465{
1466 WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
1467 devm_devfreq_dev_match, devfreq));
1468}
1469EXPORT_SYMBOL(devm_devfreq_unregister_notifier);