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