]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - drivers/devfreq/devfreq.c
PM / devfreq: exynos-nocp: Remove redundant code
[thirdparty/kernel/stable.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
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
e552bbaf
JL
128/**
129 * 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{
e35d35a1 135 int lev, prev_lev, ret = 0;
e552bbaf
JL
136 unsigned long cur_time;
137
e552bbaf 138 cur_time = jiffies;
e35d35a1
SK
139
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] +=
e552bbaf 147 cur_time - devfreq->last_stat_updated;
e35d35a1
SK
148
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) {
e552bbaf
JL
156 devfreq->trans_table[(prev_lev *
157 devfreq->profile->max_state) + lev]++;
158 devfreq->total_trans++;
159 }
e552bbaf 160
e35d35a1
SK
161out:
162 devfreq->last_stat_updated = cur_time;
163 return ret;
e552bbaf
JL
164}
165
3aa173b8
NM
166/**
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
9348da2f 177 if (IS_ERR_OR_NULL(name)) {
3aa173b8
NM
178 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
0fe3a664
CC
192static 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
7e6fdd4b
RV
215/* Load monitoring helper functions for governors use */
216
a3c98b8b
MH
217/**
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{
0fe3a664
CC
226 struct devfreq_freqs freqs;
227 unsigned long freq, cur_freq;
a3c98b8b 228 int err = 0;
ab5f299f 229 u32 flags = 0;
a3c98b8b
MH
230
231 if (!mutex_is_locked(&devfreq->lock)) {
232 WARN(true, "devfreq->lock must be locked by the caller.\n");
233 return -EINVAL;
234 }
235
1b5c1be2
NM
236 if (!devfreq->governor)
237 return -EINVAL;
238
a3c98b8b
MH
239 /* Reevaluate the proper frequency */
240 err = devfreq->governor->get_target_freq(devfreq, &freq);
241 if (err)
242 return err;
243
ab5f299f 244 /*
d3b7e174 245 * Adjust the frequency with user freq and QoS.
ab5f299f 246 *
d3b7e174
JM
247 * List from the highest priority
248 * max_freq
ab5f299f
MH
249 * 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
0fe3a664
CC
261 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
ab5f299f 270 err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
0d37189e
CC
271 if (err) {
272 freqs.new = cur_freq;
273 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
a3c98b8b 274 return err;
0d37189e 275 }
a3c98b8b 276
0fe3a664
CC
277 freqs.new = freq;
278 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
279
e552bbaf
JL
280 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
a3c98b8b
MH
285 devfreq->previous_freq = freq;
286 return err;
287}
2df5021f 288EXPORT_SYMBOL(update_devfreq);
a3c98b8b 289
7e6fdd4b
RV
290/**
291 * 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}
6dcdd8e3 327EXPORT_SYMBOL(devfreq_monitor_start);
7e6fdd4b
RV
328
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}
6dcdd8e3 341EXPORT_SYMBOL(devfreq_monitor_stop);
7e6fdd4b
RV
342
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
39688ce6 363 devfreq_update_status(devfreq, devfreq->previous_freq);
7e6fdd4b
RV
364 devfreq->stop_polling = true;
365 mutex_unlock(&devfreq->lock);
366 cancel_delayed_work_sync(&devfreq->work);
367}
6dcdd8e3 368EXPORT_SYMBOL(devfreq_monitor_suspend);
7e6fdd4b
RV
369
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{
39688ce6
RV
380 unsigned long freq;
381
7e6fdd4b
RV
382 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));
39688ce6
RV
390
391 devfreq->last_stat_updated = jiffies;
7e6fdd4b
RV
392 devfreq->stop_polling = false;
393
39688ce6
RV
394 if (devfreq->profile->get_cur_freq &&
395 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
396 devfreq->previous_freq = freq;
397
7e6fdd4b
RV
398out:
399 mutex_unlock(&devfreq->lock);
400}
6dcdd8e3 401EXPORT_SYMBOL(devfreq_monitor_resume);
7e6fdd4b
RV
402
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}
6dcdd8e3 448EXPORT_SYMBOL(devfreq_interval_update);
a3c98b8b
MH
449
450/**
451 * devfreq_notifier_call() - Notify that the device frequency requirements
452 * has been changed out of devfreq framework.
c5b4a1c1
NM
453 * @nb: the notifier_block (supposed to be devfreq->nb)
454 * @type: not used
455 * @devp: not used
a3c98b8b
MH
456 *
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/**
7e6fdd4b 473 * _remove_devfreq() - Remove devfreq from the list and release its resources.
a3c98b8b 474 * @devfreq: the devfreq struct
a3c98b8b 475 */
585fc83e 476static void _remove_devfreq(struct devfreq *devfreq)
a3c98b8b 477{
7e6fdd4b
RV
478 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");
a3c98b8b
MH
482 return;
483 }
7e6fdd4b
RV
484 list_del(&devfreq->node);
485 mutex_unlock(&devfreq_list_lock);
a3c98b8b 486
1b5c1be2
NM
487 if (devfreq->governor)
488 devfreq->governor->event_handler(devfreq,
489 DEVFREQ_GOV_STOP, NULL);
a3c98b8b
MH
490
491 if (devfreq->profile->exit)
492 devfreq->profile->exit(devfreq->dev.parent);
493
a3c98b8b 494 mutex_destroy(&devfreq->lock);
a3c98b8b
MH
495 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.
a3c98b8b
MH
503 */
504static void devfreq_dev_release(struct device *dev)
505{
506 struct devfreq *devfreq = to_devfreq(dev);
a3c98b8b 507
585fc83e 508 _remove_devfreq(devfreq);
a3c98b8b
MH
509}
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.
1b5c1be2 515 * @governor_name: name of the policy to choose frequency.
a3c98b8b
MH
516 * @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,
1b5c1be2 521 const char *governor_name,
a3c98b8b
MH
522 void *data)
523{
524 struct devfreq *devfreq;
1b5c1be2 525 struct devfreq_governor *governor;
a3c98b8b
MH
526 int err = 0;
527
1b5c1be2 528 if (!dev || !profile || !governor_name) {
a3c98b8b
MH
529 dev_err(dev, "%s: Invalid parameters.\n", __func__);
530 return ERR_PTR(-EINVAL);
531 }
532
7e6fdd4b
RV
533 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;
a3c98b8b
MH
540 }
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;
3f19f08a 547 goto err_out;
a3c98b8b
MH
548 }
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;
1b5c1be2 556 strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
a3c98b8b 557 devfreq->previous_freq = profile->initial_freq;
8d39fc08 558 devfreq->last_status.current_frequency = profile->initial_freq;
a3c98b8b 559 devfreq->data = data;
a3c98b8b
MH
560 devfreq->nb.notifier_call = devfreq_notifier_call;
561
0ec09ac2
CC
562 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
02aa2a37 568 dev_set_name(&devfreq->dev, "%s", dev_name(dev));
a3c98b8b
MH
569 err = device_register(&devfreq->dev);
570 if (err) {
7e6fdd4b 571 mutex_unlock(&devfreq->lock);
6d3cbfa7 572 goto err_out;
a3c98b8b
MH
573 }
574
3e1d7fb0
MH
575 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
0fe3a664
CC
584 srcu_init_notifier_head(&devfreq->transition_notifier_list);
585
a3c98b8b
MH
586 mutex_unlock(&devfreq->lock);
587
a3c98b8b 588 mutex_lock(&devfreq_list_lock);
a3c98b8b
MH
589 list_add(&devfreq->node, &devfreq_list);
590
1b5c1be2
NM
591 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);
a3c98b8b 597 mutex_unlock(&devfreq_list_lock);
7e6fdd4b
RV
598 if (err) {
599 dev_err(dev, "%s: Unable to start governor for the device\n",
600 __func__);
601 goto err_init;
a3c98b8b 602 }
7e6fdd4b 603
3f19f08a
AL
604 return devfreq;
605
a3c98b8b 606err_init:
7e6fdd4b 607 list_del(&devfreq->node);
a3c98b8b 608 device_unregister(&devfreq->dev);
3f19f08a
AL
609err_out:
610 return ERR_PTR(err);
a3c98b8b 611}
7e6fdd4b 612EXPORT_SYMBOL(devfreq_add_device);
a3c98b8b
MH
613
614/**
615 * devfreq_remove_device() - Remove devfreq feature from a device.
c5b4a1c1 616 * @devfreq: the devfreq instance to be removed
de9c7394
MH
617 *
618 * The opposite of devfreq_add_device().
a3c98b8b
MH
619 */
620int devfreq_remove_device(struct devfreq *devfreq)
621{
622 if (!devfreq)
623 return -EINVAL;
624
585fc83e 625 device_unregister(&devfreq->dev);
9f3bdd4f 626
a3c98b8b
MH
627 return 0;
628}
7e6fdd4b 629EXPORT_SYMBOL(devfreq_remove_device);
a3c98b8b 630
8cd84092
CC
631static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
632{
633 struct devfreq **r = res;
634
635 if (WARN_ON(!r || !*r))
636 return 0;
637
638 return *r == data;
639}
640
641static void devm_devfreq_dev_release(struct device *dev, void *res)
642{
643 devfreq_remove_device(*(struct devfreq **)res);
644}
645
646/**
647 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
648 * @dev: the device to add devfreq feature.
649 * @profile: device-specific profile to run devfreq.
650 * @governor_name: name of the policy to choose frequency.
651 * @data: private data for the governor. The devfreq framework does not
652 * touch this value.
653 *
654 * This function manages automatically the memory of devfreq device using device
655 * resource management and simplify the free operation for memory of devfreq
656 * device.
657 */
658struct devfreq *devm_devfreq_add_device(struct device *dev,
659 struct devfreq_dev_profile *profile,
660 const char *governor_name,
661 void *data)
662{
663 struct devfreq **ptr, *devfreq;
664
665 ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
666 if (!ptr)
667 return ERR_PTR(-ENOMEM);
668
669 devfreq = devfreq_add_device(dev, profile, governor_name, data);
670 if (IS_ERR(devfreq)) {
671 devres_free(ptr);
672 return ERR_PTR(-ENOMEM);
673 }
674
675 *ptr = devfreq;
676 devres_add(dev, ptr);
677
678 return devfreq;
679}
680EXPORT_SYMBOL(devm_devfreq_add_device);
681
8f510aeb
CC
682#ifdef CONFIG_OF
683/*
684 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
685 * @dev - instance to the given device
686 * @index - index into list of devfreq
687 *
688 * return the instance of devfreq device
689 */
690struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
691{
692 struct device_node *node;
693 struct devfreq *devfreq;
694
695 if (!dev)
696 return ERR_PTR(-EINVAL);
697
698 if (!dev->of_node)
699 return ERR_PTR(-EINVAL);
700
701 node = of_parse_phandle(dev->of_node, "devfreq", index);
702 if (!node)
703 return ERR_PTR(-ENODEV);
704
705 mutex_lock(&devfreq_list_lock);
706 list_for_each_entry(devfreq, &devfreq_list, node) {
707 if (devfreq->dev.parent
708 && devfreq->dev.parent->of_node == node) {
709 mutex_unlock(&devfreq_list_lock);
3427c6f0 710 of_node_put(node);
8f510aeb
CC
711 return devfreq;
712 }
713 }
714 mutex_unlock(&devfreq_list_lock);
3427c6f0 715 of_node_put(node);
8f510aeb
CC
716
717 return ERR_PTR(-EPROBE_DEFER);
718}
719#else
720struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
721{
722 return ERR_PTR(-ENODEV);
723}
724#endif /* CONFIG_OF */
725EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
726
8cd84092
CC
727/**
728 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
729 * @dev: the device to add devfreq feature.
730 * @devfreq: the devfreq instance to be removed
731 */
732void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
733{
734 WARN_ON(devres_release(dev, devm_devfreq_dev_release,
735 devm_devfreq_dev_match, devfreq));
736}
737EXPORT_SYMBOL(devm_devfreq_remove_device);
738
206c30cf
RV
739/**
740 * devfreq_suspend_device() - Suspend devfreq of a device.
741 * @devfreq: the devfreq instance to be suspended
de9c7394
MH
742 *
743 * This function is intended to be called by the pm callbacks
744 * (e.g., runtime_suspend, suspend) of the device driver that
745 * holds the devfreq.
206c30cf
RV
746 */
747int devfreq_suspend_device(struct devfreq *devfreq)
748{
a3c98b8b
MH
749 if (!devfreq)
750 return -EINVAL;
751
1b5c1be2
NM
752 if (!devfreq->governor)
753 return 0;
754
206c30cf
RV
755 return devfreq->governor->event_handler(devfreq,
756 DEVFREQ_GOV_SUSPEND, NULL);
757}
758EXPORT_SYMBOL(devfreq_suspend_device);
759
760/**
761 * devfreq_resume_device() - Resume devfreq of a device.
762 * @devfreq: the devfreq instance to be resumed
de9c7394
MH
763 *
764 * This function is intended to be called by the pm callbacks
765 * (e.g., runtime_resume, resume) of the device driver that
766 * holds the devfreq.
206c30cf
RV
767 */
768int devfreq_resume_device(struct devfreq *devfreq)
769{
770 if (!devfreq)
771 return -EINVAL;
772
1b5c1be2
NM
773 if (!devfreq->governor)
774 return 0;
775
206c30cf
RV
776 return devfreq->governor->event_handler(devfreq,
777 DEVFREQ_GOV_RESUME, NULL);
778}
779EXPORT_SYMBOL(devfreq_resume_device);
780
3aa173b8
NM
781/**
782 * devfreq_add_governor() - Add devfreq governor
783 * @governor: the devfreq governor to be added
784 */
785int devfreq_add_governor(struct devfreq_governor *governor)
786{
787 struct devfreq_governor *g;
1b5c1be2 788 struct devfreq *devfreq;
3aa173b8
NM
789 int err = 0;
790
791 if (!governor) {
792 pr_err("%s: Invalid parameters.\n", __func__);
793 return -EINVAL;
794 }
795
796 mutex_lock(&devfreq_list_lock);
797 g = find_devfreq_governor(governor->name);
798 if (!IS_ERR(g)) {
799 pr_err("%s: governor %s already registered\n", __func__,
800 g->name);
801 err = -EINVAL;
802 goto err_out;
803 }
9f3bdd4f 804
3aa173b8
NM
805 list_add(&governor->node, &devfreq_governor_list);
806
1b5c1be2
NM
807 list_for_each_entry(devfreq, &devfreq_list, node) {
808 int ret = 0;
809 struct device *dev = devfreq->dev.parent;
810
811 if (!strncmp(devfreq->governor_name, governor->name,
812 DEVFREQ_NAME_LEN)) {
813 /* The following should never occur */
814 if (devfreq->governor) {
815 dev_warn(dev,
816 "%s: Governor %s already present\n",
817 __func__, devfreq->governor->name);
818 ret = devfreq->governor->event_handler(devfreq,
819 DEVFREQ_GOV_STOP, NULL);
820 if (ret) {
821 dev_warn(dev,
822 "%s: Governor %s stop = %d\n",
823 __func__,
824 devfreq->governor->name, ret);
825 }
826 /* Fall through */
827 }
828 devfreq->governor = governor;
829 ret = devfreq->governor->event_handler(devfreq,
830 DEVFREQ_GOV_START, NULL);
831 if (ret) {
832 dev_warn(dev, "%s: Governor %s start=%d\n",
833 __func__, devfreq->governor->name,
834 ret);
835 }
a3c98b8b
MH
836 }
837 }
838
3aa173b8
NM
839err_out:
840 mutex_unlock(&devfreq_list_lock);
a3c98b8b 841
3aa173b8
NM
842 return err;
843}
844EXPORT_SYMBOL(devfreq_add_governor);
a3c98b8b 845
3aa173b8
NM
846/**
847 * devfreq_remove_device() - Remove devfreq feature from a device.
848 * @governor: the devfreq governor to be removed
849 */
850int devfreq_remove_governor(struct devfreq_governor *governor)
851{
852 struct devfreq_governor *g;
1b5c1be2 853 struct devfreq *devfreq;
3aa173b8
NM
854 int err = 0;
855
856 if (!governor) {
857 pr_err("%s: Invalid parameters.\n", __func__);
858 return -EINVAL;
859 }
860
861 mutex_lock(&devfreq_list_lock);
862 g = find_devfreq_governor(governor->name);
863 if (IS_ERR(g)) {
864 pr_err("%s: governor %s not registered\n", __func__,
b9e1c8e8 865 governor->name);
f9c08e2a 866 err = PTR_ERR(g);
3aa173b8
NM
867 goto err_out;
868 }
1b5c1be2
NM
869 list_for_each_entry(devfreq, &devfreq_list, node) {
870 int ret;
871 struct device *dev = devfreq->dev.parent;
872
873 if (!strncmp(devfreq->governor_name, governor->name,
874 DEVFREQ_NAME_LEN)) {
875 /* we should have a devfreq governor! */
876 if (!devfreq->governor) {
877 dev_warn(dev, "%s: Governor %s NOT present\n",
878 __func__, governor->name);
879 continue;
880 /* Fall through */
881 }
882 ret = devfreq->governor->event_handler(devfreq,
883 DEVFREQ_GOV_STOP, NULL);
884 if (ret) {
885 dev_warn(dev, "%s: Governor %s stop=%d\n",
886 __func__, devfreq->governor->name,
887 ret);
888 }
889 devfreq->governor = NULL;
890 }
891 }
3aa173b8
NM
892
893 list_del(&governor->node);
894err_out:
895 mutex_unlock(&devfreq_list_lock);
896
897 return err;
a3c98b8b 898}
3aa173b8 899EXPORT_SYMBOL(devfreq_remove_governor);
a3c98b8b 900
a93d6b0a 901static ssize_t governor_show(struct device *dev,
9005b650
MH
902 struct device_attribute *attr, char *buf)
903{
1b5c1be2
NM
904 if (!to_devfreq(dev)->governor)
905 return -EINVAL;
906
9005b650
MH
907 return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
908}
909
a93d6b0a 910static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
0359d1af
NM
911 const char *buf, size_t count)
912{
913 struct devfreq *df = to_devfreq(dev);
914 int ret;
915 char str_governor[DEVFREQ_NAME_LEN + 1];
916 struct devfreq_governor *governor;
917
918 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
919 if (ret != 1)
920 return -EINVAL;
921
922 mutex_lock(&devfreq_list_lock);
923 governor = find_devfreq_governor(str_governor);
924 if (IS_ERR(governor)) {
925 ret = PTR_ERR(governor);
926 goto out;
927 }
14a21e7b
TJ
928 if (df->governor == governor) {
929 ret = 0;
0359d1af 930 goto out;
14a21e7b 931 }
0359d1af
NM
932
933 if (df->governor) {
934 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
935 if (ret) {
936 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
937 __func__, df->governor->name, ret);
938 goto out;
939 }
940 }
941 df->governor = governor;
942 strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
943 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
944 if (ret)
945 dev_warn(dev, "%s: Governor %s not started(%d)\n",
946 __func__, df->governor->name, ret);
947out:
948 mutex_unlock(&devfreq_list_lock);
949
950 if (!ret)
951 ret = count;
952 return ret;
953}
a93d6b0a
GKH
954static DEVICE_ATTR_RW(governor);
955
956static ssize_t available_governors_show(struct device *d,
957 struct device_attribute *attr,
958 char *buf)
50a5b33e
NM
959{
960 struct devfreq_governor *tmp_governor;
961 ssize_t count = 0;
962
963 mutex_lock(&devfreq_list_lock);
964 list_for_each_entry(tmp_governor, &devfreq_governor_list, node)
965 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
966 "%s ", tmp_governor->name);
967 mutex_unlock(&devfreq_list_lock);
968
969 /* Truncate the trailing space */
970 if (count)
971 count--;
972
973 count += sprintf(&buf[count], "\n");
974
975 return count;
976}
a93d6b0a 977static DEVICE_ATTR_RO(available_governors);
0359d1af 978
a93d6b0a
GKH
979static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
980 char *buf)
7f98a905
RV
981{
982 unsigned long freq;
983 struct devfreq *devfreq = to_devfreq(dev);
984
985 if (devfreq->profile->get_cur_freq &&
986 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
987 return sprintf(buf, "%lu\n", freq);
988
989 return sprintf(buf, "%lu\n", devfreq->previous_freq);
990}
a93d6b0a 991static DEVICE_ATTR_RO(cur_freq);
7f98a905 992
a93d6b0a
GKH
993static ssize_t target_freq_show(struct device *dev,
994 struct device_attribute *attr, char *buf)
9005b650
MH
995{
996 return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
997}
a93d6b0a 998static DEVICE_ATTR_RO(target_freq);
9005b650 999
a93d6b0a 1000static ssize_t polling_interval_show(struct device *dev,
9005b650
MH
1001 struct device_attribute *attr, char *buf)
1002{
1003 return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
1004}
1005
a93d6b0a 1006static ssize_t polling_interval_store(struct device *dev,
9005b650
MH
1007 struct device_attribute *attr,
1008 const char *buf, size_t count)
1009{
1010 struct devfreq *df = to_devfreq(dev);
1011 unsigned int value;
1012 int ret;
1013
1b5c1be2
NM
1014 if (!df->governor)
1015 return -EINVAL;
1016
9005b650
MH
1017 ret = sscanf(buf, "%u", &value);
1018 if (ret != 1)
12e26265 1019 return -EINVAL;
9005b650 1020
7e6fdd4b 1021 df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
9005b650
MH
1022 ret = count;
1023
9005b650
MH
1024 return ret;
1025}
a93d6b0a 1026static DEVICE_ATTR_RW(polling_interval);
9005b650 1027
a93d6b0a 1028static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
6530b9de
MH
1029 const char *buf, size_t count)
1030{
1031 struct devfreq *df = to_devfreq(dev);
1032 unsigned long value;
1033 int ret;
1034 unsigned long max;
1035
1036 ret = sscanf(buf, "%lu", &value);
1037 if (ret != 1)
12e26265 1038 return -EINVAL;
6530b9de
MH
1039
1040 mutex_lock(&df->lock);
1041 max = df->max_freq;
1042 if (value && max && value > max) {
1043 ret = -EINVAL;
1044 goto unlock;
1045 }
1046
1047 df->min_freq = value;
1048 update_devfreq(df);
1049 ret = count;
1050unlock:
1051 mutex_unlock(&df->lock);
6530b9de
MH
1052 return ret;
1053}
1054
a93d6b0a 1055static ssize_t max_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 min;
1062
1063 ret = sscanf(buf, "%lu", &value);
1064 if (ret != 1)
12e26265 1065 return -EINVAL;
6530b9de
MH
1066
1067 mutex_lock(&df->lock);
1068 min = df->min_freq;
1069 if (value && min && value < min) {
1070 ret = -EINVAL;
1071 goto unlock;
1072 }
1073
1074 df->max_freq = value;
1075 update_devfreq(df);
1076 ret = count;
1077unlock:
1078 mutex_unlock(&df->lock);
6530b9de
MH
1079 return ret;
1080}
1081
3104fa30
CC
1082#define show_one(name) \
1083static ssize_t name##_show \
1084(struct device *dev, struct device_attribute *attr, char *buf) \
1085{ \
1086 return sprintf(buf, "%lu\n", to_devfreq(dev)->name); \
6530b9de 1087}
3104fa30
CC
1088show_one(min_freq);
1089show_one(max_freq);
1090
1091static DEVICE_ATTR_RW(min_freq);
a93d6b0a 1092static DEVICE_ATTR_RW(max_freq);
6530b9de 1093
a93d6b0a
GKH
1094static ssize_t available_frequencies_show(struct device *d,
1095 struct device_attribute *attr,
1096 char *buf)
d287de85
NM
1097{
1098 struct devfreq *df = to_devfreq(d);
1099 struct device *dev = df->dev.parent;
47d43ba7 1100 struct dev_pm_opp *opp;
d287de85
NM
1101 ssize_t count = 0;
1102 unsigned long freq = 0;
1103
1104 rcu_read_lock();
1105 do {
5d4879cd 1106 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
d287de85
NM
1107 if (IS_ERR(opp))
1108 break;
1109
1110 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1111 "%lu ", freq);
1112 freq++;
1113 } while (1);
1114 rcu_read_unlock();
1115
1116 /* Truncate the trailing space */
1117 if (count)
1118 count--;
1119
1120 count += sprintf(&buf[count], "\n");
1121
1122 return count;
1123}
a93d6b0a 1124static DEVICE_ATTR_RO(available_frequencies);
d287de85 1125
a93d6b0a
GKH
1126static ssize_t trans_stat_show(struct device *dev,
1127 struct device_attribute *attr, char *buf)
e552bbaf
JL
1128{
1129 struct devfreq *devfreq = to_devfreq(dev);
1130 ssize_t len;
39688ce6 1131 int i, j;
e552bbaf
JL
1132 unsigned int max_state = devfreq->profile->max_state;
1133
39688ce6
RV
1134 if (!devfreq->stop_polling &&
1135 devfreq_update_status(devfreq, devfreq->previous_freq))
e552bbaf 1136 return 0;
34bd3220
MH
1137 if (max_state == 0)
1138 return sprintf(buf, "Not Supported.\n");
e552bbaf 1139
d7df1e46
CC
1140 len = sprintf(buf, " From : To\n");
1141 len += sprintf(buf + len, " :");
e552bbaf 1142 for (i = 0; i < max_state; i++)
d7df1e46 1143 len += sprintf(buf + len, "%10lu",
e552bbaf
JL
1144 devfreq->profile->freq_table[i]);
1145
1146 len += sprintf(buf + len, " time(ms)\n");
1147
1148 for (i = 0; i < max_state; i++) {
1149 if (devfreq->profile->freq_table[i]
1150 == devfreq->previous_freq) {
1151 len += sprintf(buf + len, "*");
1152 } else {
1153 len += sprintf(buf + len, " ");
1154 }
d7df1e46 1155 len += sprintf(buf + len, "%10lu:",
e552bbaf
JL
1156 devfreq->profile->freq_table[i]);
1157 for (j = 0; j < max_state; j++)
d7df1e46 1158 len += sprintf(buf + len, "%10u",
e552bbaf
JL
1159 devfreq->trans_table[(i * max_state) + j]);
1160 len += sprintf(buf + len, "%10u\n",
1161 jiffies_to_msecs(devfreq->time_in_state[i]));
1162 }
1163
1164 len += sprintf(buf + len, "Total transition : %u\n",
1165 devfreq->total_trans);
1166 return len;
1167}
a93d6b0a
GKH
1168static DEVICE_ATTR_RO(trans_stat);
1169
1170static struct attribute *devfreq_attrs[] = {
1171 &dev_attr_governor.attr,
1172 &dev_attr_available_governors.attr,
1173 &dev_attr_cur_freq.attr,
1174 &dev_attr_available_frequencies.attr,
1175 &dev_attr_target_freq.attr,
1176 &dev_attr_polling_interval.attr,
1177 &dev_attr_min_freq.attr,
1178 &dev_attr_max_freq.attr,
1179 &dev_attr_trans_stat.attr,
1180 NULL,
9005b650 1181};
a93d6b0a 1182ATTRIBUTE_GROUPS(devfreq);
9005b650 1183
a3c98b8b
MH
1184static int __init devfreq_init(void)
1185{
1186 devfreq_class = class_create(THIS_MODULE, "devfreq");
1187 if (IS_ERR(devfreq_class)) {
1188 pr_err("%s: couldn't create class\n", __FILE__);
1189 return PTR_ERR(devfreq_class);
1190 }
7e6fdd4b
RV
1191
1192 devfreq_wq = create_freezable_workqueue("devfreq_wq");
ea7f4548 1193 if (!devfreq_wq) {
7e6fdd4b
RV
1194 class_destroy(devfreq_class);
1195 pr_err("%s: couldn't create workqueue\n", __FILE__);
ea7f4548 1196 return -ENOMEM;
7e6fdd4b 1197 }
a93d6b0a 1198 devfreq_class->dev_groups = devfreq_groups;
7e6fdd4b 1199
a3c98b8b
MH
1200 return 0;
1201}
1202subsys_initcall(devfreq_init);
1203
a3c98b8b
MH
1204/*
1205 * The followings are helper functions for devfreq user device drivers with
1206 * OPP framework.
1207 */
1208
1209/**
1210 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1211 * freq value given to target callback.
c5b4a1c1
NM
1212 * @dev: The devfreq user device. (parent of devfreq)
1213 * @freq: The frequency given to target function
1214 * @flags: Flags handed from devfreq framework.
a3c98b8b 1215 *
bcb27549
NM
1216 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
1217 * protected pointer. The reason for the same is that the opp pointer which is
1218 * returned will remain valid for use with opp_get_{voltage, freq} only while
1219 * under the locked area. The pointer returned must be used prior to unlocking
1220 * with rcu_read_unlock() to maintain the integrity of the pointer.
a3c98b8b 1221 */
47d43ba7
NM
1222struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1223 unsigned long *freq,
1224 u32 flags)
a3c98b8b 1225{
47d43ba7 1226 struct dev_pm_opp *opp;
a3c98b8b 1227
ab5f299f
MH
1228 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1229 /* The freq is an upper bound. opp should be lower */
5d4879cd 1230 opp = dev_pm_opp_find_freq_floor(dev, freq);
ab5f299f
MH
1231
1232 /* If not available, use the closest opp */
0779726c 1233 if (opp == ERR_PTR(-ERANGE))
5d4879cd 1234 opp = dev_pm_opp_find_freq_ceil(dev, freq);
ab5f299f
MH
1235 } else {
1236 /* The freq is an lower bound. opp should be higher */
5d4879cd 1237 opp = dev_pm_opp_find_freq_ceil(dev, freq);
ab5f299f
MH
1238
1239 /* If not available, use the closest opp */
0779726c 1240 if (opp == ERR_PTR(-ERANGE))
5d4879cd 1241 opp = dev_pm_opp_find_freq_floor(dev, freq);
ab5f299f
MH
1242 }
1243
a3c98b8b
MH
1244 return opp;
1245}
bd7e9277 1246EXPORT_SYMBOL(devfreq_recommended_opp);
a3c98b8b
MH
1247
1248/**
1249 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1250 * for any changes in the OPP availability
1251 * changes
c5b4a1c1
NM
1252 * @dev: The devfreq user device. (parent of devfreq)
1253 * @devfreq: The devfreq object.
a3c98b8b
MH
1254 */
1255int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1256{
389baed0
MH
1257 struct srcu_notifier_head *nh;
1258 int ret = 0;
a3c98b8b 1259
389baed0 1260 rcu_read_lock();
5d4879cd 1261 nh = dev_pm_opp_get_notifier(dev);
a3c98b8b 1262 if (IS_ERR(nh))
389baed0
MH
1263 ret = PTR_ERR(nh);
1264 rcu_read_unlock();
1265 if (!ret)
1266 ret = srcu_notifier_chain_register(nh, &devfreq->nb);
1267
1268 return ret;
a3c98b8b 1269}
bd7e9277 1270EXPORT_SYMBOL(devfreq_register_opp_notifier);
a3c98b8b
MH
1271
1272/**
1273 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1274 * notified for any changes in the OPP
1275 * availability changes anymore.
c5b4a1c1
NM
1276 * @dev: The devfreq user device. (parent of devfreq)
1277 * @devfreq: The devfreq object.
a3c98b8b
MH
1278 *
1279 * At exit() callback of devfreq_dev_profile, this must be included if
1280 * devfreq_recommended_opp is used.
1281 */
1282int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1283{
389baed0
MH
1284 struct srcu_notifier_head *nh;
1285 int ret = 0;
a3c98b8b 1286
389baed0 1287 rcu_read_lock();
5d4879cd 1288 nh = dev_pm_opp_get_notifier(dev);
a3c98b8b 1289 if (IS_ERR(nh))
389baed0
MH
1290 ret = PTR_ERR(nh);
1291 rcu_read_unlock();
1292 if (!ret)
1293 ret = srcu_notifier_chain_unregister(nh, &devfreq->nb);
1294
1295 return ret;
a3c98b8b 1296}
bd7e9277 1297EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
a3c98b8b 1298
d5b040d0
CC
1299static void devm_devfreq_opp_release(struct device *dev, void *res)
1300{
1301 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
1302}
1303
1304/**
1305 * devm_ devfreq_register_opp_notifier()
1306 * - Resource-managed devfreq_register_opp_notifier()
1307 * @dev: The devfreq user device. (parent of devfreq)
1308 * @devfreq: The devfreq object.
1309 */
1310int devm_devfreq_register_opp_notifier(struct device *dev,
1311 struct devfreq *devfreq)
1312{
1313 struct devfreq **ptr;
1314 int ret;
1315
1316 ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
1317 if (!ptr)
1318 return -ENOMEM;
1319
1320 ret = devfreq_register_opp_notifier(dev, devfreq);
1321 if (ret) {
1322 devres_free(ptr);
1323 return ret;
1324 }
1325
1326 *ptr = devfreq;
1327 devres_add(dev, ptr);
1328
1329 return 0;
1330}
1331EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
1332
1333/**
1334 * devm_devfreq_unregister_opp_notifier()
1335 * - Resource-managed devfreq_unregister_opp_notifier()
1336 * @dev: The devfreq user device. (parent of devfreq)
1337 * @devfreq: The devfreq object.
1338 */
1339void devm_devfreq_unregister_opp_notifier(struct device *dev,
1340 struct devfreq *devfreq)
1341{
1342 WARN_ON(devres_release(dev, devm_devfreq_opp_release,
1343 devm_devfreq_dev_match, devfreq));
1344}
1345EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
1346
0fe3a664
CC
1347/**
1348 * devfreq_register_notifier() - Register a driver with devfreq
1349 * @devfreq: The devfreq object.
1350 * @nb: The notifier block to register.
1351 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1352 */
1353int devfreq_register_notifier(struct devfreq *devfreq,
1354 struct notifier_block *nb,
1355 unsigned int list)
1356{
1357 int ret = 0;
1358
1359 if (!devfreq)
1360 return -EINVAL;
1361
1362 switch (list) {
1363 case DEVFREQ_TRANSITION_NOTIFIER:
1364 ret = srcu_notifier_chain_register(
1365 &devfreq->transition_notifier_list, nb);
1366 break;
1367 default:
1368 ret = -EINVAL;
1369 }
1370
1371 return ret;
1372}
1373EXPORT_SYMBOL(devfreq_register_notifier);
1374
1375/*
1376 * devfreq_unregister_notifier() - Unregister a driver with devfreq
1377 * @devfreq: The devfreq object.
1378 * @nb: The notifier block to be unregistered.
1379 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1380 */
1381int devfreq_unregister_notifier(struct devfreq *devfreq,
1382 struct notifier_block *nb,
1383 unsigned int list)
1384{
1385 int ret = 0;
1386
1387 if (!devfreq)
1388 return -EINVAL;
1389
1390 switch (list) {
1391 case DEVFREQ_TRANSITION_NOTIFIER:
1392 ret = srcu_notifier_chain_unregister(
1393 &devfreq->transition_notifier_list, nb);
1394 break;
1395 default:
1396 ret = -EINVAL;
1397 }
1398
1399 return ret;
1400}
1401EXPORT_SYMBOL(devfreq_unregister_notifier);
1402
1403struct devfreq_notifier_devres {
1404 struct devfreq *devfreq;
1405 struct notifier_block *nb;
1406 unsigned int list;
1407};
1408
1409static void devm_devfreq_notifier_release(struct device *dev, void *res)
1410{
1411 struct devfreq_notifier_devres *this = res;
1412
1413 devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
1414}
1415
1416/**
1417 * devm_devfreq_register_notifier()
1418 - Resource-managed devfreq_register_notifier()
1419 * @dev: The devfreq user device. (parent of devfreq)
1420 * @devfreq: The devfreq object.
1421 * @nb: The notifier block to be unregistered.
1422 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1423 */
1424int devm_devfreq_register_notifier(struct device *dev,
1425 struct devfreq *devfreq,
1426 struct notifier_block *nb,
1427 unsigned int list)
1428{
1429 struct devfreq_notifier_devres *ptr;
1430 int ret;
1431
1432 ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
1433 GFP_KERNEL);
1434 if (!ptr)
1435 return -ENOMEM;
1436
1437 ret = devfreq_register_notifier(devfreq, nb, list);
1438 if (ret) {
1439 devres_free(ptr);
1440 return ret;
1441 }
1442
1443 ptr->devfreq = devfreq;
1444 ptr->nb = nb;
1445 ptr->list = list;
1446 devres_add(dev, ptr);
1447
1448 return 0;
1449}
1450EXPORT_SYMBOL(devm_devfreq_register_notifier);
1451
1452/**
1453 * devm_devfreq_unregister_notifier()
1454 - Resource-managed devfreq_unregister_notifier()
1455 * @dev: The devfreq user device. (parent of devfreq)
1456 * @devfreq: The devfreq object.
1457 * @nb: The notifier block to be unregistered.
1458 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1459 */
1460void devm_devfreq_unregister_notifier(struct device *dev,
1461 struct devfreq *devfreq,
1462 struct notifier_block *nb,
1463 unsigned int list)
1464{
1465 WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
1466 devm_devfreq_dev_match, devfreq));
1467}
1468EXPORT_SYMBOL(devm_devfreq_unregister_notifier);