GNU Linux-libre 5.10.153-gnu1
[releases.git] / drivers / devfreq / devfreq.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
4  *          for Non-CPU Devices.
5  *
6  * Copyright (C) 2011 Samsung Electronics
7  *      MyungJoo Ham <myungjoo.ham@samsung.com>
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/kmod.h>
12 #include <linux/sched.h>
13 #include <linux/debugfs.h>
14 #include <linux/errno.h>
15 #include <linux/err.h>
16 #include <linux/init.h>
17 #include <linux/export.h>
18 #include <linux/slab.h>
19 #include <linux/stat.h>
20 #include <linux/pm_opp.h>
21 #include <linux/devfreq.h>
22 #include <linux/workqueue.h>
23 #include <linux/platform_device.h>
24 #include <linux/list.h>
25 #include <linux/printk.h>
26 #include <linux/hrtimer.h>
27 #include <linux/of.h>
28 #include <linux/pm_qos.h>
29 #include "governor.h"
30
31 #define CREATE_TRACE_POINTS
32 #include <trace/events/devfreq.h>
33
34 #define HZ_PER_KHZ      1000
35
36 static struct class *devfreq_class;
37 static struct dentry *devfreq_debugfs;
38
39 /*
40  * devfreq core provides delayed work based load monitoring helper
41  * functions. Governors can use these or can implement their own
42  * monitoring mechanism.
43  */
44 static struct workqueue_struct *devfreq_wq;
45
46 /* The list of all device-devfreq governors */
47 static LIST_HEAD(devfreq_governor_list);
48 /* The list of all device-devfreq */
49 static LIST_HEAD(devfreq_list);
50 static DEFINE_MUTEX(devfreq_list_lock);
51
52 static const char timer_name[][DEVFREQ_NAME_LEN] = {
53         [DEVFREQ_TIMER_DEFERRABLE] = { "deferrable" },
54         [DEVFREQ_TIMER_DELAYED] = { "delayed" },
55 };
56
57 /**
58  * find_device_devfreq() - find devfreq struct using device pointer
59  * @dev:        device pointer used to lookup device devfreq.
60  *
61  * Search the list of device devfreqs and return the matched device's
62  * devfreq info. devfreq_list_lock should be held by the caller.
63  */
64 static struct devfreq *find_device_devfreq(struct device *dev)
65 {
66         struct devfreq *tmp_devfreq;
67
68         lockdep_assert_held(&devfreq_list_lock);
69
70         if (IS_ERR_OR_NULL(dev)) {
71                 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
72                 return ERR_PTR(-EINVAL);
73         }
74
75         list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
76                 if (tmp_devfreq->dev.parent == dev)
77                         return tmp_devfreq;
78         }
79
80         return ERR_PTR(-ENODEV);
81 }
82
83 static unsigned long find_available_min_freq(struct devfreq *devfreq)
84 {
85         struct dev_pm_opp *opp;
86         unsigned long min_freq = 0;
87
88         opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &min_freq);
89         if (IS_ERR(opp))
90                 min_freq = 0;
91         else
92                 dev_pm_opp_put(opp);
93
94         return min_freq;
95 }
96
97 static unsigned long find_available_max_freq(struct devfreq *devfreq)
98 {
99         struct dev_pm_opp *opp;
100         unsigned long max_freq = ULONG_MAX;
101
102         opp = dev_pm_opp_find_freq_floor(devfreq->dev.parent, &max_freq);
103         if (IS_ERR(opp))
104                 max_freq = 0;
105         else
106                 dev_pm_opp_put(opp);
107
108         return max_freq;
109 }
110
111 /**
112  * get_freq_range() - Get the current freq range
113  * @devfreq:    the devfreq instance
114  * @min_freq:   the min frequency
115  * @max_freq:   the max frequency
116  *
117  * This takes into consideration all constraints.
118  */
119 static void get_freq_range(struct devfreq *devfreq,
120                            unsigned long *min_freq,
121                            unsigned long *max_freq)
122 {
123         unsigned long *freq_table = devfreq->profile->freq_table;
124         s32 qos_min_freq, qos_max_freq;
125
126         lockdep_assert_held(&devfreq->lock);
127
128         /*
129          * Initialize minimum/maximum frequency from freq table.
130          * The devfreq drivers can initialize this in either ascending or
131          * descending order and devfreq core supports both.
132          */
133         if (freq_table[0] < freq_table[devfreq->profile->max_state - 1]) {
134                 *min_freq = freq_table[0];
135                 *max_freq = freq_table[devfreq->profile->max_state - 1];
136         } else {
137                 *min_freq = freq_table[devfreq->profile->max_state - 1];
138                 *max_freq = freq_table[0];
139         }
140
141         /* Apply constraints from PM QoS */
142         qos_min_freq = dev_pm_qos_read_value(devfreq->dev.parent,
143                                              DEV_PM_QOS_MIN_FREQUENCY);
144         qos_max_freq = dev_pm_qos_read_value(devfreq->dev.parent,
145                                              DEV_PM_QOS_MAX_FREQUENCY);
146         *min_freq = max(*min_freq, (unsigned long)HZ_PER_KHZ * qos_min_freq);
147         if (qos_max_freq != PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE)
148                 *max_freq = min(*max_freq,
149                                 (unsigned long)HZ_PER_KHZ * qos_max_freq);
150
151         /* Apply constraints from OPP interface */
152         *min_freq = max(*min_freq, devfreq->scaling_min_freq);
153         *max_freq = min(*max_freq, devfreq->scaling_max_freq);
154
155         if (*min_freq > *max_freq)
156                 *min_freq = *max_freq;
157 }
158
159 /**
160  * devfreq_get_freq_level() - Lookup freq_table for the frequency
161  * @devfreq:    the devfreq instance
162  * @freq:       the target frequency
163  */
164 static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq)
165 {
166         int lev;
167
168         for (lev = 0; lev < devfreq->profile->max_state; lev++)
169                 if (freq == devfreq->profile->freq_table[lev])
170                         return lev;
171
172         return -EINVAL;
173 }
174
175 static int set_freq_table(struct devfreq *devfreq)
176 {
177         struct devfreq_dev_profile *profile = devfreq->profile;
178         struct dev_pm_opp *opp;
179         unsigned long freq;
180         int i, count;
181
182         /* Initialize the freq_table from OPP table */
183         count = dev_pm_opp_get_opp_count(devfreq->dev.parent);
184         if (count <= 0)
185                 return -EINVAL;
186
187         profile->max_state = count;
188         profile->freq_table = devm_kcalloc(devfreq->dev.parent,
189                                         profile->max_state,
190                                         sizeof(*profile->freq_table),
191                                         GFP_KERNEL);
192         if (!profile->freq_table) {
193                 profile->max_state = 0;
194                 return -ENOMEM;
195         }
196
197         for (i = 0, freq = 0; i < profile->max_state; i++, freq++) {
198                 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq);
199                 if (IS_ERR(opp)) {
200                         devm_kfree(devfreq->dev.parent, profile->freq_table);
201                         profile->max_state = 0;
202                         return PTR_ERR(opp);
203                 }
204                 dev_pm_opp_put(opp);
205                 profile->freq_table[i] = freq;
206         }
207
208         return 0;
209 }
210
211 /**
212  * devfreq_update_status() - Update statistics of devfreq behavior
213  * @devfreq:    the devfreq instance
214  * @freq:       the update target frequency
215  */
216 int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
217 {
218         int lev, prev_lev, ret = 0;
219         u64 cur_time;
220
221         lockdep_assert_held(&devfreq->lock);
222         cur_time = get_jiffies_64();
223
224         /* Immediately exit if previous_freq is not initialized yet. */
225         if (!devfreq->previous_freq)
226                 goto out;
227
228         prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
229         if (prev_lev < 0) {
230                 ret = prev_lev;
231                 goto out;
232         }
233
234         devfreq->stats.time_in_state[prev_lev] +=
235                         cur_time - devfreq->stats.last_update;
236
237         lev = devfreq_get_freq_level(devfreq, freq);
238         if (lev < 0) {
239                 ret = lev;
240                 goto out;
241         }
242
243         if (lev != prev_lev) {
244                 devfreq->stats.trans_table[
245                         (prev_lev * devfreq->profile->max_state) + lev]++;
246                 devfreq->stats.total_trans++;
247         }
248
249 out:
250         devfreq->stats.last_update = cur_time;
251         return ret;
252 }
253 EXPORT_SYMBOL(devfreq_update_status);
254
255 /**
256  * find_devfreq_governor() - find devfreq governor from name
257  * @name:       name of the governor
258  *
259  * Search the list of devfreq governors and return the matched
260  * governor's pointer. devfreq_list_lock should be held by the caller.
261  */
262 static struct devfreq_governor *find_devfreq_governor(const char *name)
263 {
264         struct devfreq_governor *tmp_governor;
265
266         lockdep_assert_held(&devfreq_list_lock);
267
268         if (IS_ERR_OR_NULL(name)) {
269                 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
270                 return ERR_PTR(-EINVAL);
271         }
272
273         list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
274                 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
275                         return tmp_governor;
276         }
277
278         return ERR_PTR(-ENODEV);
279 }
280
281 /**
282  * try_then_request_governor() - Try to find the governor and request the
283  *                               module if is not found.
284  * @name:       name of the governor
285  *
286  * Search the list of devfreq governors and request the module and try again
287  * if is not found. This can happen when both drivers (the governor driver
288  * and the driver that call devfreq_add_device) are built as modules.
289  * devfreq_list_lock should be held by the caller. Returns the matched
290  * governor's pointer or an error pointer.
291  */
292 static struct devfreq_governor *try_then_request_governor(const char *name)
293 {
294         struct devfreq_governor *governor;
295         int err = 0;
296
297         lockdep_assert_held(&devfreq_list_lock);
298
299         if (IS_ERR_OR_NULL(name)) {
300                 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
301                 return ERR_PTR(-EINVAL);
302         }
303
304         governor = find_devfreq_governor(name);
305         if (IS_ERR(governor)) {
306                 mutex_unlock(&devfreq_list_lock);
307
308                 if (!strncmp(name, DEVFREQ_GOV_SIMPLE_ONDEMAND,
309                              DEVFREQ_NAME_LEN))
310                         err = request_module("governor_%s", "simpleondemand");
311                 else
312                         err = request_module("governor_%s", name);
313                 /* Restore previous state before return */
314                 mutex_lock(&devfreq_list_lock);
315                 if (err)
316                         return (err < 0) ? ERR_PTR(err) : ERR_PTR(-EINVAL);
317
318                 governor = find_devfreq_governor(name);
319         }
320
321         return governor;
322 }
323
324 static int devfreq_notify_transition(struct devfreq *devfreq,
325                 struct devfreq_freqs *freqs, unsigned int state)
326 {
327         if (!devfreq)
328                 return -EINVAL;
329
330         switch (state) {
331         case DEVFREQ_PRECHANGE:
332                 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
333                                 DEVFREQ_PRECHANGE, freqs);
334                 break;
335
336         case DEVFREQ_POSTCHANGE:
337                 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
338                                 DEVFREQ_POSTCHANGE, freqs);
339                 break;
340         default:
341                 return -EINVAL;
342         }
343
344         return 0;
345 }
346
347 static int devfreq_set_target(struct devfreq *devfreq, unsigned long new_freq,
348                               u32 flags)
349 {
350         struct devfreq_freqs freqs;
351         unsigned long cur_freq;
352         int err = 0;
353
354         if (devfreq->profile->get_cur_freq)
355                 devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
356         else
357                 cur_freq = devfreq->previous_freq;
358
359         freqs.old = cur_freq;
360         freqs.new = new_freq;
361         devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
362
363         err = devfreq->profile->target(devfreq->dev.parent, &new_freq, flags);
364         if (err) {
365                 freqs.new = cur_freq;
366                 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
367                 return err;
368         }
369
370         freqs.new = new_freq;
371         devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
372
373         if (devfreq_update_status(devfreq, new_freq))
374                 dev_err(&devfreq->dev,
375                         "Couldn't update frequency transition information.\n");
376
377         devfreq->previous_freq = new_freq;
378
379         if (devfreq->suspend_freq)
380                 devfreq->resume_freq = new_freq;
381
382         return err;
383 }
384
385 /* Load monitoring helper functions for governors use */
386
387 /**
388  * update_devfreq() - Reevaluate the device and configure frequency.
389  * @devfreq:    the devfreq instance.
390  *
391  * Note: Lock devfreq->lock before calling update_devfreq
392  *       This function is exported for governors.
393  */
394 int update_devfreq(struct devfreq *devfreq)
395 {
396         unsigned long freq, min_freq, max_freq;
397         int err = 0;
398         u32 flags = 0;
399
400         lockdep_assert_held(&devfreq->lock);
401
402         if (!devfreq->governor)
403                 return -EINVAL;
404
405         /* Reevaluate the proper frequency */
406         err = devfreq->governor->get_target_freq(devfreq, &freq);
407         if (err)
408                 return err;
409         get_freq_range(devfreq, &min_freq, &max_freq);
410
411         if (freq < min_freq) {
412                 freq = min_freq;
413                 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
414         }
415         if (freq > max_freq) {
416                 freq = max_freq;
417                 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
418         }
419
420         return devfreq_set_target(devfreq, freq, flags);
421
422 }
423 EXPORT_SYMBOL(update_devfreq);
424
425 /**
426  * devfreq_monitor() - Periodically poll devfreq objects.
427  * @work:       the work struct used to run devfreq_monitor periodically.
428  *
429  */
430 static void devfreq_monitor(struct work_struct *work)
431 {
432         int err;
433         struct devfreq *devfreq = container_of(work,
434                                         struct devfreq, work.work);
435
436         mutex_lock(&devfreq->lock);
437         err = update_devfreq(devfreq);
438         if (err)
439                 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
440
441         queue_delayed_work(devfreq_wq, &devfreq->work,
442                                 msecs_to_jiffies(devfreq->profile->polling_ms));
443         mutex_unlock(&devfreq->lock);
444
445         trace_devfreq_monitor(devfreq);
446 }
447
448 /**
449  * devfreq_monitor_start() - Start load monitoring of devfreq instance
450  * @devfreq:    the devfreq instance.
451  *
452  * Helper function for starting devfreq device load monitoring. By
453  * default delayed work based monitoring is supported. Function
454  * to be called from governor in response to DEVFREQ_GOV_START
455  * event when device is added to devfreq framework.
456  */
457 void devfreq_monitor_start(struct devfreq *devfreq)
458 {
459         if (devfreq->governor->interrupt_driven)
460                 return;
461
462         switch (devfreq->profile->timer) {
463         case DEVFREQ_TIMER_DEFERRABLE:
464                 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
465                 break;
466         case DEVFREQ_TIMER_DELAYED:
467                 INIT_DELAYED_WORK(&devfreq->work, devfreq_monitor);
468                 break;
469         default:
470                 return;
471         }
472
473         if (devfreq->profile->polling_ms)
474                 queue_delayed_work(devfreq_wq, &devfreq->work,
475                         msecs_to_jiffies(devfreq->profile->polling_ms));
476 }
477 EXPORT_SYMBOL(devfreq_monitor_start);
478
479 /**
480  * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
481  * @devfreq:    the devfreq instance.
482  *
483  * Helper function to stop devfreq device load monitoring. Function
484  * to be called from governor in response to DEVFREQ_GOV_STOP
485  * event when device is removed from devfreq framework.
486  */
487 void devfreq_monitor_stop(struct devfreq *devfreq)
488 {
489         if (devfreq->governor->interrupt_driven)
490                 return;
491
492         cancel_delayed_work_sync(&devfreq->work);
493 }
494 EXPORT_SYMBOL(devfreq_monitor_stop);
495
496 /**
497  * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
498  * @devfreq:    the devfreq instance.
499  *
500  * Helper function to suspend devfreq device load monitoring. Function
501  * to be called from governor in response to DEVFREQ_GOV_SUSPEND
502  * event or when polling interval is set to zero.
503  *
504  * Note: Though this function is same as devfreq_monitor_stop(),
505  * intentionally kept separate to provide hooks for collecting
506  * transition statistics.
507  */
508 void devfreq_monitor_suspend(struct devfreq *devfreq)
509 {
510         mutex_lock(&devfreq->lock);
511         if (devfreq->stop_polling) {
512                 mutex_unlock(&devfreq->lock);
513                 return;
514         }
515
516         devfreq_update_status(devfreq, devfreq->previous_freq);
517         devfreq->stop_polling = true;
518         mutex_unlock(&devfreq->lock);
519
520         if (devfreq->governor->interrupt_driven)
521                 return;
522
523         cancel_delayed_work_sync(&devfreq->work);
524 }
525 EXPORT_SYMBOL(devfreq_monitor_suspend);
526
527 /**
528  * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
529  * @devfreq:    the devfreq instance.
530  *
531  * Helper function to resume devfreq device load monitoring. Function
532  * to be called from governor in response to DEVFREQ_GOV_RESUME
533  * event or when polling interval is set to non-zero.
534  */
535 void devfreq_monitor_resume(struct devfreq *devfreq)
536 {
537         unsigned long freq;
538
539         mutex_lock(&devfreq->lock);
540         if (!devfreq->stop_polling)
541                 goto out;
542
543         if (devfreq->governor->interrupt_driven)
544                 goto out_update;
545
546         if (!delayed_work_pending(&devfreq->work) &&
547                         devfreq->profile->polling_ms)
548                 queue_delayed_work(devfreq_wq, &devfreq->work,
549                         msecs_to_jiffies(devfreq->profile->polling_ms));
550
551 out_update:
552         devfreq->stats.last_update = get_jiffies_64();
553         devfreq->stop_polling = false;
554
555         if (devfreq->profile->get_cur_freq &&
556                 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
557                 devfreq->previous_freq = freq;
558
559 out:
560         mutex_unlock(&devfreq->lock);
561 }
562 EXPORT_SYMBOL(devfreq_monitor_resume);
563
564 /**
565  * devfreq_update_interval() - Update device devfreq monitoring interval
566  * @devfreq:    the devfreq instance.
567  * @delay:      new polling interval to be set.
568  *
569  * Helper function to set new load monitoring polling interval. Function
570  * to be called from governor in response to DEVFREQ_GOV_UPDATE_INTERVAL event.
571  */
572 void devfreq_update_interval(struct devfreq *devfreq, unsigned int *delay)
573 {
574         unsigned int cur_delay = devfreq->profile->polling_ms;
575         unsigned int new_delay = *delay;
576
577         mutex_lock(&devfreq->lock);
578         devfreq->profile->polling_ms = new_delay;
579
580         if (devfreq->stop_polling)
581                 goto out;
582
583         if (devfreq->governor->interrupt_driven)
584                 goto out;
585
586         /* if new delay is zero, stop polling */
587         if (!new_delay) {
588                 mutex_unlock(&devfreq->lock);
589                 cancel_delayed_work_sync(&devfreq->work);
590                 return;
591         }
592
593         /* if current delay is zero, start polling with new delay */
594         if (!cur_delay) {
595                 queue_delayed_work(devfreq_wq, &devfreq->work,
596                         msecs_to_jiffies(devfreq->profile->polling_ms));
597                 goto out;
598         }
599
600         /* if current delay is greater than new delay, restart polling */
601         if (cur_delay > new_delay) {
602                 mutex_unlock(&devfreq->lock);
603                 cancel_delayed_work_sync(&devfreq->work);
604                 mutex_lock(&devfreq->lock);
605                 if (!devfreq->stop_polling)
606                         queue_delayed_work(devfreq_wq, &devfreq->work,
607                                 msecs_to_jiffies(devfreq->profile->polling_ms));
608         }
609 out:
610         mutex_unlock(&devfreq->lock);
611 }
612 EXPORT_SYMBOL(devfreq_update_interval);
613
614 /**
615  * devfreq_notifier_call() - Notify that the device frequency requirements
616  *                           has been changed out of devfreq framework.
617  * @nb:         the notifier_block (supposed to be devfreq->nb)
618  * @type:       not used
619  * @devp:       not used
620  *
621  * Called by a notifier that uses devfreq->nb.
622  */
623 static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
624                                  void *devp)
625 {
626         struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
627         int err = -EINVAL;
628
629         mutex_lock(&devfreq->lock);
630
631         devfreq->scaling_min_freq = find_available_min_freq(devfreq);
632         if (!devfreq->scaling_min_freq)
633                 goto out;
634
635         devfreq->scaling_max_freq = find_available_max_freq(devfreq);
636         if (!devfreq->scaling_max_freq) {
637                 devfreq->scaling_max_freq = ULONG_MAX;
638                 goto out;
639         }
640
641         err = update_devfreq(devfreq);
642
643 out:
644         mutex_unlock(&devfreq->lock);
645         if (err)
646                 dev_err(devfreq->dev.parent,
647                         "failed to update frequency from OPP notifier (%d)\n",
648                         err);
649
650         return NOTIFY_OK;
651 }
652
653 /**
654  * qos_notifier_call() - Common handler for QoS constraints.
655  * @devfreq:    the devfreq instance.
656  */
657 static int qos_notifier_call(struct devfreq *devfreq)
658 {
659         int err;
660
661         mutex_lock(&devfreq->lock);
662         err = update_devfreq(devfreq);
663         mutex_unlock(&devfreq->lock);
664         if (err)
665                 dev_err(devfreq->dev.parent,
666                         "failed to update frequency from PM QoS (%d)\n",
667                         err);
668
669         return NOTIFY_OK;
670 }
671
672 /**
673  * qos_min_notifier_call() - Callback for QoS min_freq changes.
674  * @nb:         Should be devfreq->nb_min
675  */
676 static int qos_min_notifier_call(struct notifier_block *nb,
677                                          unsigned long val, void *ptr)
678 {
679         return qos_notifier_call(container_of(nb, struct devfreq, nb_min));
680 }
681
682 /**
683  * qos_max_notifier_call() - Callback for QoS max_freq changes.
684  * @nb:         Should be devfreq->nb_max
685  */
686 static int qos_max_notifier_call(struct notifier_block *nb,
687                                          unsigned long val, void *ptr)
688 {
689         return qos_notifier_call(container_of(nb, struct devfreq, nb_max));
690 }
691
692 /**
693  * devfreq_dev_release() - Callback for struct device to release the device.
694  * @dev:        the devfreq device
695  *
696  * Remove devfreq from the list and release its resources.
697  */
698 static void devfreq_dev_release(struct device *dev)
699 {
700         struct devfreq *devfreq = to_devfreq(dev);
701         int err;
702
703         mutex_lock(&devfreq_list_lock);
704         list_del(&devfreq->node);
705         mutex_unlock(&devfreq_list_lock);
706
707         err = dev_pm_qos_remove_notifier(devfreq->dev.parent, &devfreq->nb_max,
708                                          DEV_PM_QOS_MAX_FREQUENCY);
709         if (err && err != -ENOENT)
710                 dev_warn(dev->parent,
711                         "Failed to remove max_freq notifier: %d\n", err);
712         err = dev_pm_qos_remove_notifier(devfreq->dev.parent, &devfreq->nb_min,
713                                          DEV_PM_QOS_MIN_FREQUENCY);
714         if (err && err != -ENOENT)
715                 dev_warn(dev->parent,
716                         "Failed to remove min_freq notifier: %d\n", err);
717
718         if (dev_pm_qos_request_active(&devfreq->user_max_freq_req)) {
719                 err = dev_pm_qos_remove_request(&devfreq->user_max_freq_req);
720                 if (err < 0)
721                         dev_warn(dev->parent,
722                                 "Failed to remove max_freq request: %d\n", err);
723         }
724         if (dev_pm_qos_request_active(&devfreq->user_min_freq_req)) {
725                 err = dev_pm_qos_remove_request(&devfreq->user_min_freq_req);
726                 if (err < 0)
727                         dev_warn(dev->parent,
728                                 "Failed to remove min_freq request: %d\n", err);
729         }
730
731         if (devfreq->profile->exit)
732                 devfreq->profile->exit(devfreq->dev.parent);
733
734         mutex_destroy(&devfreq->lock);
735         kfree(devfreq);
736 }
737
738 /**
739  * devfreq_add_device() - Add devfreq feature to the device
740  * @dev:        the device to add devfreq feature.
741  * @profile:    device-specific profile to run devfreq.
742  * @governor_name:      name of the policy to choose frequency.
743  * @data:       private data for the governor. The devfreq framework does not
744  *              touch this value.
745  */
746 struct devfreq *devfreq_add_device(struct device *dev,
747                                    struct devfreq_dev_profile *profile,
748                                    const char *governor_name,
749                                    void *data)
750 {
751         struct devfreq *devfreq;
752         struct devfreq_governor *governor;
753         int err = 0;
754
755         if (!dev || !profile || !governor_name) {
756                 dev_err(dev, "%s: Invalid parameters.\n", __func__);
757                 return ERR_PTR(-EINVAL);
758         }
759
760         mutex_lock(&devfreq_list_lock);
761         devfreq = find_device_devfreq(dev);
762         mutex_unlock(&devfreq_list_lock);
763         if (!IS_ERR(devfreq)) {
764                 dev_err(dev, "%s: devfreq device already exists!\n",
765                         __func__);
766                 err = -EINVAL;
767                 goto err_out;
768         }
769
770         devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
771         if (!devfreq) {
772                 err = -ENOMEM;
773                 goto err_out;
774         }
775
776         mutex_init(&devfreq->lock);
777         mutex_lock(&devfreq->lock);
778         devfreq->dev.parent = dev;
779         devfreq->dev.class = devfreq_class;
780         devfreq->dev.release = devfreq_dev_release;
781         INIT_LIST_HEAD(&devfreq->node);
782         devfreq->profile = profile;
783         strscpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
784         devfreq->previous_freq = profile->initial_freq;
785         devfreq->last_status.current_frequency = profile->initial_freq;
786         devfreq->data = data;
787         devfreq->nb.notifier_call = devfreq_notifier_call;
788
789         if (devfreq->profile->timer < 0
790                 || devfreq->profile->timer >= DEVFREQ_TIMER_NUM) {
791                 mutex_unlock(&devfreq->lock);
792                 err = -EINVAL;
793                 goto err_dev;
794         }
795
796         if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
797                 mutex_unlock(&devfreq->lock);
798                 err = set_freq_table(devfreq);
799                 if (err < 0)
800                         goto err_dev;
801                 mutex_lock(&devfreq->lock);
802         }
803
804         devfreq->scaling_min_freq = find_available_min_freq(devfreq);
805         if (!devfreq->scaling_min_freq) {
806                 mutex_unlock(&devfreq->lock);
807                 err = -EINVAL;
808                 goto err_dev;
809         }
810
811         devfreq->scaling_max_freq = find_available_max_freq(devfreq);
812         if (!devfreq->scaling_max_freq) {
813                 mutex_unlock(&devfreq->lock);
814                 err = -EINVAL;
815                 goto err_dev;
816         }
817
818         devfreq->suspend_freq = dev_pm_opp_get_suspend_opp_freq(dev);
819         atomic_set(&devfreq->suspend_count, 0);
820
821         dev_set_name(&devfreq->dev, "%s", dev_name(dev));
822         err = device_register(&devfreq->dev);
823         if (err) {
824                 mutex_unlock(&devfreq->lock);
825                 put_device(&devfreq->dev);
826                 goto err_out;
827         }
828
829         devfreq->stats.trans_table = devm_kzalloc(&devfreq->dev,
830                         array3_size(sizeof(unsigned int),
831                                     devfreq->profile->max_state,
832                                     devfreq->profile->max_state),
833                         GFP_KERNEL);
834         if (!devfreq->stats.trans_table) {
835                 mutex_unlock(&devfreq->lock);
836                 err = -ENOMEM;
837                 goto err_devfreq;
838         }
839
840         devfreq->stats.time_in_state = devm_kcalloc(&devfreq->dev,
841                         devfreq->profile->max_state,
842                         sizeof(*devfreq->stats.time_in_state),
843                         GFP_KERNEL);
844         if (!devfreq->stats.time_in_state) {
845                 mutex_unlock(&devfreq->lock);
846                 err = -ENOMEM;
847                 goto err_devfreq;
848         }
849
850         devfreq->stats.total_trans = 0;
851         devfreq->stats.last_update = get_jiffies_64();
852
853         srcu_init_notifier_head(&devfreq->transition_notifier_list);
854
855         mutex_unlock(&devfreq->lock);
856
857         err = dev_pm_qos_add_request(dev, &devfreq->user_min_freq_req,
858                                      DEV_PM_QOS_MIN_FREQUENCY, 0);
859         if (err < 0)
860                 goto err_devfreq;
861         err = dev_pm_qos_add_request(dev, &devfreq->user_max_freq_req,
862                                      DEV_PM_QOS_MAX_FREQUENCY,
863                                      PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE);
864         if (err < 0)
865                 goto err_devfreq;
866
867         devfreq->nb_min.notifier_call = qos_min_notifier_call;
868         err = dev_pm_qos_add_notifier(devfreq->dev.parent, &devfreq->nb_min,
869                                       DEV_PM_QOS_MIN_FREQUENCY);
870         if (err)
871                 goto err_devfreq;
872
873         devfreq->nb_max.notifier_call = qos_max_notifier_call;
874         err = dev_pm_qos_add_notifier(devfreq->dev.parent, &devfreq->nb_max,
875                                       DEV_PM_QOS_MAX_FREQUENCY);
876         if (err)
877                 goto err_devfreq;
878
879         mutex_lock(&devfreq_list_lock);
880
881         governor = try_then_request_governor(devfreq->governor_name);
882         if (IS_ERR(governor)) {
883                 dev_err(dev, "%s: Unable to find governor for the device\n",
884                         __func__);
885                 err = PTR_ERR(governor);
886                 goto err_init;
887         }
888
889         devfreq->governor = governor;
890         err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
891                                                 NULL);
892         if (err) {
893                 dev_err(dev, "%s: Unable to start governor for the device\n",
894                         __func__);
895                 goto err_init;
896         }
897
898         list_add(&devfreq->node, &devfreq_list);
899
900         mutex_unlock(&devfreq_list_lock);
901
902         return devfreq;
903
904 err_init:
905         mutex_unlock(&devfreq_list_lock);
906 err_devfreq:
907         devfreq_remove_device(devfreq);
908         devfreq = NULL;
909 err_dev:
910         kfree(devfreq);
911 err_out:
912         return ERR_PTR(err);
913 }
914 EXPORT_SYMBOL(devfreq_add_device);
915
916 /**
917  * devfreq_remove_device() - Remove devfreq feature from a device.
918  * @devfreq:    the devfreq instance to be removed
919  *
920  * The opposite of devfreq_add_device().
921  */
922 int devfreq_remove_device(struct devfreq *devfreq)
923 {
924         if (!devfreq)
925                 return -EINVAL;
926
927         if (devfreq->governor)
928                 devfreq->governor->event_handler(devfreq,
929                                                  DEVFREQ_GOV_STOP, NULL);
930         device_unregister(&devfreq->dev);
931
932         return 0;
933 }
934 EXPORT_SYMBOL(devfreq_remove_device);
935
936 static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
937 {
938         struct devfreq **r = res;
939
940         if (WARN_ON(!r || !*r))
941                 return 0;
942
943         return *r == data;
944 }
945
946 static void devm_devfreq_dev_release(struct device *dev, void *res)
947 {
948         devfreq_remove_device(*(struct devfreq **)res);
949 }
950
951 /**
952  * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
953  * @dev:        the device to add devfreq feature.
954  * @profile:    device-specific profile to run devfreq.
955  * @governor_name:      name of the policy to choose frequency.
956  * @data:       private data for the governor. The devfreq framework does not
957  *              touch this value.
958  *
959  * This function manages automatically the memory of devfreq device using device
960  * resource management and simplify the free operation for memory of devfreq
961  * device.
962  */
963 struct devfreq *devm_devfreq_add_device(struct device *dev,
964                                         struct devfreq_dev_profile *profile,
965                                         const char *governor_name,
966                                         void *data)
967 {
968         struct devfreq **ptr, *devfreq;
969
970         ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
971         if (!ptr)
972                 return ERR_PTR(-ENOMEM);
973
974         devfreq = devfreq_add_device(dev, profile, governor_name, data);
975         if (IS_ERR(devfreq)) {
976                 devres_free(ptr);
977                 return devfreq;
978         }
979
980         *ptr = devfreq;
981         devres_add(dev, ptr);
982
983         return devfreq;
984 }
985 EXPORT_SYMBOL(devm_devfreq_add_device);
986
987 #ifdef CONFIG_OF
988 /*
989  * devfreq_get_devfreq_by_node - Get the devfreq device from devicetree
990  * @node - pointer to device_node
991  *
992  * return the instance of devfreq device
993  */
994 struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node)
995 {
996         struct devfreq *devfreq;
997
998         if (!node)
999                 return ERR_PTR(-EINVAL);
1000
1001         mutex_lock(&devfreq_list_lock);
1002         list_for_each_entry(devfreq, &devfreq_list, node) {
1003                 if (devfreq->dev.parent
1004                         && devfreq->dev.parent->of_node == node) {
1005                         mutex_unlock(&devfreq_list_lock);
1006                         return devfreq;
1007                 }
1008         }
1009         mutex_unlock(&devfreq_list_lock);
1010
1011         return ERR_PTR(-ENODEV);
1012 }
1013
1014 /*
1015  * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
1016  * @dev - instance to the given device
1017  * @phandle_name - name of property holding a phandle value
1018  * @index - index into list of devfreq
1019  *
1020  * return the instance of devfreq device
1021  */
1022 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
1023                                         const char *phandle_name, int index)
1024 {
1025         struct device_node *node;
1026         struct devfreq *devfreq;
1027
1028         if (!dev || !phandle_name)
1029                 return ERR_PTR(-EINVAL);
1030
1031         if (!dev->of_node)
1032                 return ERR_PTR(-EINVAL);
1033
1034         node = of_parse_phandle(dev->of_node, phandle_name, index);
1035         if (!node)
1036                 return ERR_PTR(-ENODEV);
1037
1038         devfreq = devfreq_get_devfreq_by_node(node);
1039         of_node_put(node);
1040
1041         return devfreq;
1042 }
1043
1044 #else
1045 struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node)
1046 {
1047         return ERR_PTR(-ENODEV);
1048 }
1049
1050 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
1051                                         const char *phandle_name, int index)
1052 {
1053         return ERR_PTR(-ENODEV);
1054 }
1055 #endif /* CONFIG_OF */
1056 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_node);
1057 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
1058
1059 /**
1060  * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
1061  * @dev:        the device from which to remove devfreq feature.
1062  * @devfreq:    the devfreq instance to be removed
1063  */
1064 void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
1065 {
1066         WARN_ON(devres_release(dev, devm_devfreq_dev_release,
1067                                devm_devfreq_dev_match, devfreq));
1068 }
1069 EXPORT_SYMBOL(devm_devfreq_remove_device);
1070
1071 /**
1072  * devfreq_suspend_device() - Suspend devfreq of a device.
1073  * @devfreq: the devfreq instance to be suspended
1074  *
1075  * This function is intended to be called by the pm callbacks
1076  * (e.g., runtime_suspend, suspend) of the device driver that
1077  * holds the devfreq.
1078  */
1079 int devfreq_suspend_device(struct devfreq *devfreq)
1080 {
1081         int ret;
1082
1083         if (!devfreq)
1084                 return -EINVAL;
1085
1086         if (atomic_inc_return(&devfreq->suspend_count) > 1)
1087                 return 0;
1088
1089         if (devfreq->governor) {
1090                 ret = devfreq->governor->event_handler(devfreq,
1091                                         DEVFREQ_GOV_SUSPEND, NULL);
1092                 if (ret)
1093                         return ret;
1094         }
1095
1096         if (devfreq->suspend_freq) {
1097                 mutex_lock(&devfreq->lock);
1098                 ret = devfreq_set_target(devfreq, devfreq->suspend_freq, 0);
1099                 mutex_unlock(&devfreq->lock);
1100                 if (ret)
1101                         return ret;
1102         }
1103
1104         return 0;
1105 }
1106 EXPORT_SYMBOL(devfreq_suspend_device);
1107
1108 /**
1109  * devfreq_resume_device() - Resume devfreq of a device.
1110  * @devfreq: the devfreq instance to be resumed
1111  *
1112  * This function is intended to be called by the pm callbacks
1113  * (e.g., runtime_resume, resume) of the device driver that
1114  * holds the devfreq.
1115  */
1116 int devfreq_resume_device(struct devfreq *devfreq)
1117 {
1118         int ret;
1119
1120         if (!devfreq)
1121                 return -EINVAL;
1122
1123         if (atomic_dec_return(&devfreq->suspend_count) >= 1)
1124                 return 0;
1125
1126         if (devfreq->resume_freq) {
1127                 mutex_lock(&devfreq->lock);
1128                 ret = devfreq_set_target(devfreq, devfreq->resume_freq, 0);
1129                 mutex_unlock(&devfreq->lock);
1130                 if (ret)
1131                         return ret;
1132         }
1133
1134         if (devfreq->governor) {
1135                 ret = devfreq->governor->event_handler(devfreq,
1136                                         DEVFREQ_GOV_RESUME, NULL);
1137                 if (ret)
1138                         return ret;
1139         }
1140
1141         return 0;
1142 }
1143 EXPORT_SYMBOL(devfreq_resume_device);
1144
1145 /**
1146  * devfreq_suspend() - Suspend devfreq governors and devices
1147  *
1148  * Called during system wide Suspend/Hibernate cycles for suspending governors
1149  * and devices preserving the state for resume. On some platforms the devfreq
1150  * device must have precise state (frequency) after resume in order to provide
1151  * fully operating setup.
1152  */
1153 void devfreq_suspend(void)
1154 {
1155         struct devfreq *devfreq;
1156         int ret;
1157
1158         mutex_lock(&devfreq_list_lock);
1159         list_for_each_entry(devfreq, &devfreq_list, node) {
1160                 ret = devfreq_suspend_device(devfreq);
1161                 if (ret)
1162                         dev_err(&devfreq->dev,
1163                                 "failed to suspend devfreq device\n");
1164         }
1165         mutex_unlock(&devfreq_list_lock);
1166 }
1167
1168 /**
1169  * devfreq_resume() - Resume devfreq governors and devices
1170  *
1171  * Called during system wide Suspend/Hibernate cycle for resuming governors and
1172  * devices that are suspended with devfreq_suspend().
1173  */
1174 void devfreq_resume(void)
1175 {
1176         struct devfreq *devfreq;
1177         int ret;
1178
1179         mutex_lock(&devfreq_list_lock);
1180         list_for_each_entry(devfreq, &devfreq_list, node) {
1181                 ret = devfreq_resume_device(devfreq);
1182                 if (ret)
1183                         dev_warn(&devfreq->dev,
1184                                  "failed to resume devfreq device\n");
1185         }
1186         mutex_unlock(&devfreq_list_lock);
1187 }
1188
1189 /**
1190  * devfreq_add_governor() - Add devfreq governor
1191  * @governor:   the devfreq governor to be added
1192  */
1193 int devfreq_add_governor(struct devfreq_governor *governor)
1194 {
1195         struct devfreq_governor *g;
1196         struct devfreq *devfreq;
1197         int err = 0;
1198
1199         if (!governor) {
1200                 pr_err("%s: Invalid parameters.\n", __func__);
1201                 return -EINVAL;
1202         }
1203
1204         mutex_lock(&devfreq_list_lock);
1205         g = find_devfreq_governor(governor->name);
1206         if (!IS_ERR(g)) {
1207                 pr_err("%s: governor %s already registered\n", __func__,
1208                        g->name);
1209                 err = -EINVAL;
1210                 goto err_out;
1211         }
1212
1213         list_add(&governor->node, &devfreq_governor_list);
1214
1215         list_for_each_entry(devfreq, &devfreq_list, node) {
1216                 int ret = 0;
1217                 struct device *dev = devfreq->dev.parent;
1218
1219                 if (!strncmp(devfreq->governor_name, governor->name,
1220                              DEVFREQ_NAME_LEN)) {
1221                         /* The following should never occur */
1222                         if (devfreq->governor) {
1223                                 dev_warn(dev,
1224                                          "%s: Governor %s already present\n",
1225                                          __func__, devfreq->governor->name);
1226                                 ret = devfreq->governor->event_handler(devfreq,
1227                                                         DEVFREQ_GOV_STOP, NULL);
1228                                 if (ret) {
1229                                         dev_warn(dev,
1230                                                  "%s: Governor %s stop = %d\n",
1231                                                  __func__,
1232                                                  devfreq->governor->name, ret);
1233                                 }
1234                                 /* Fall through */
1235                         }
1236                         devfreq->governor = governor;
1237                         ret = devfreq->governor->event_handler(devfreq,
1238                                                 DEVFREQ_GOV_START, NULL);
1239                         if (ret) {
1240                                 dev_warn(dev, "%s: Governor %s start=%d\n",
1241                                          __func__, devfreq->governor->name,
1242                                          ret);
1243                         }
1244                 }
1245         }
1246
1247 err_out:
1248         mutex_unlock(&devfreq_list_lock);
1249
1250         return err;
1251 }
1252 EXPORT_SYMBOL(devfreq_add_governor);
1253
1254 /**
1255  * devfreq_remove_governor() - Remove devfreq feature from a device.
1256  * @governor:   the devfreq governor to be removed
1257  */
1258 int devfreq_remove_governor(struct devfreq_governor *governor)
1259 {
1260         struct devfreq_governor *g;
1261         struct devfreq *devfreq;
1262         int err = 0;
1263
1264         if (!governor) {
1265                 pr_err("%s: Invalid parameters.\n", __func__);
1266                 return -EINVAL;
1267         }
1268
1269         mutex_lock(&devfreq_list_lock);
1270         g = find_devfreq_governor(governor->name);
1271         if (IS_ERR(g)) {
1272                 pr_err("%s: governor %s not registered\n", __func__,
1273                        governor->name);
1274                 err = PTR_ERR(g);
1275                 goto err_out;
1276         }
1277         list_for_each_entry(devfreq, &devfreq_list, node) {
1278                 int ret;
1279                 struct device *dev = devfreq->dev.parent;
1280
1281                 if (!strncmp(devfreq->governor_name, governor->name,
1282                              DEVFREQ_NAME_LEN)) {
1283                         /* we should have a devfreq governor! */
1284                         if (!devfreq->governor) {
1285                                 dev_warn(dev, "%s: Governor %s NOT present\n",
1286                                          __func__, governor->name);
1287                                 continue;
1288                                 /* Fall through */
1289                         }
1290                         ret = devfreq->governor->event_handler(devfreq,
1291                                                 DEVFREQ_GOV_STOP, NULL);
1292                         if (ret) {
1293                                 dev_warn(dev, "%s: Governor %s stop=%d\n",
1294                                          __func__, devfreq->governor->name,
1295                                          ret);
1296                         }
1297                         devfreq->governor = NULL;
1298                 }
1299         }
1300
1301         list_del(&governor->node);
1302 err_out:
1303         mutex_unlock(&devfreq_list_lock);
1304
1305         return err;
1306 }
1307 EXPORT_SYMBOL(devfreq_remove_governor);
1308
1309 static ssize_t name_show(struct device *dev,
1310                         struct device_attribute *attr, char *buf)
1311 {
1312         struct devfreq *df = to_devfreq(dev);
1313         return sprintf(buf, "%s\n", dev_name(df->dev.parent));
1314 }
1315 static DEVICE_ATTR_RO(name);
1316
1317 static ssize_t governor_show(struct device *dev,
1318                              struct device_attribute *attr, char *buf)
1319 {
1320         struct devfreq *df = to_devfreq(dev);
1321
1322         if (!df->governor)
1323                 return -EINVAL;
1324
1325         return sprintf(buf, "%s\n", df->governor->name);
1326 }
1327
1328 static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
1329                               const char *buf, size_t count)
1330 {
1331         struct devfreq *df = to_devfreq(dev);
1332         int ret;
1333         char str_governor[DEVFREQ_NAME_LEN + 1];
1334         const struct devfreq_governor *governor, *prev_governor;
1335
1336         if (!df->governor)
1337                 return -EINVAL;
1338
1339         ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
1340         if (ret != 1)
1341                 return -EINVAL;
1342
1343         mutex_lock(&devfreq_list_lock);
1344         governor = try_then_request_governor(str_governor);
1345         if (IS_ERR(governor)) {
1346                 ret = PTR_ERR(governor);
1347                 goto out;
1348         }
1349         if (df->governor == governor) {
1350                 ret = 0;
1351                 goto out;
1352         } else if (df->governor->immutable || governor->immutable) {
1353                 ret = -EINVAL;
1354                 goto out;
1355         }
1356
1357         ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1358         if (ret) {
1359                 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1360                          __func__, df->governor->name, ret);
1361                 goto out;
1362         }
1363
1364         prev_governor = df->governor;
1365         df->governor = governor;
1366         strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
1367         ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1368         if (ret) {
1369                 dev_warn(dev, "%s: Governor %s not started(%d)\n",
1370                          __func__, df->governor->name, ret);
1371                 df->governor = prev_governor;
1372                 strncpy(df->governor_name, prev_governor->name,
1373                         DEVFREQ_NAME_LEN);
1374                 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1375                 if (ret) {
1376                         dev_err(dev,
1377                                 "%s: reverting to Governor %s failed (%d)\n",
1378                                 __func__, df->governor_name, ret);
1379                         df->governor = NULL;
1380                 }
1381         }
1382 out:
1383         mutex_unlock(&devfreq_list_lock);
1384
1385         if (!ret)
1386                 ret = count;
1387         return ret;
1388 }
1389 static DEVICE_ATTR_RW(governor);
1390
1391 static ssize_t available_governors_show(struct device *d,
1392                                         struct device_attribute *attr,
1393                                         char *buf)
1394 {
1395         struct devfreq *df = to_devfreq(d);
1396         ssize_t count = 0;
1397
1398         if (!df->governor)
1399                 return -EINVAL;
1400
1401         mutex_lock(&devfreq_list_lock);
1402
1403         /*
1404          * The devfreq with immutable governor (e.g., passive) shows
1405          * only own governor.
1406          */
1407         if (df->governor->immutable) {
1408                 count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
1409                                   "%s ", df->governor_name);
1410         /*
1411          * The devfreq device shows the registered governor except for
1412          * immutable governors such as passive governor .
1413          */
1414         } else {
1415                 struct devfreq_governor *governor;
1416
1417                 list_for_each_entry(governor, &devfreq_governor_list, node) {
1418                         if (governor->immutable)
1419                                 continue;
1420                         count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1421                                            "%s ", governor->name);
1422                 }
1423         }
1424
1425         mutex_unlock(&devfreq_list_lock);
1426
1427         /* Truncate the trailing space */
1428         if (count)
1429                 count--;
1430
1431         count += sprintf(&buf[count], "\n");
1432
1433         return count;
1434 }
1435 static DEVICE_ATTR_RO(available_governors);
1436
1437 static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
1438                              char *buf)
1439 {
1440         unsigned long freq;
1441         struct devfreq *df = to_devfreq(dev);
1442
1443         if (!df->profile)
1444                 return -EINVAL;
1445
1446         if (df->profile->get_cur_freq &&
1447                 !df->profile->get_cur_freq(df->dev.parent, &freq))
1448                 return sprintf(buf, "%lu\n", freq);
1449
1450         return sprintf(buf, "%lu\n", df->previous_freq);
1451 }
1452 static DEVICE_ATTR_RO(cur_freq);
1453
1454 static ssize_t target_freq_show(struct device *dev,
1455                                 struct device_attribute *attr, char *buf)
1456 {
1457         struct devfreq *df = to_devfreq(dev);
1458
1459         return sprintf(buf, "%lu\n", df->previous_freq);
1460 }
1461 static DEVICE_ATTR_RO(target_freq);
1462
1463 static ssize_t polling_interval_show(struct device *dev,
1464                                      struct device_attribute *attr, char *buf)
1465 {
1466         struct devfreq *df = to_devfreq(dev);
1467
1468         if (!df->profile)
1469                 return -EINVAL;
1470
1471         return sprintf(buf, "%d\n", df->profile->polling_ms);
1472 }
1473
1474 static ssize_t polling_interval_store(struct device *dev,
1475                                       struct device_attribute *attr,
1476                                       const char *buf, size_t count)
1477 {
1478         struct devfreq *df = to_devfreq(dev);
1479         unsigned int value;
1480         int ret;
1481
1482         if (!df->governor)
1483                 return -EINVAL;
1484
1485         ret = sscanf(buf, "%u", &value);
1486         if (ret != 1)
1487                 return -EINVAL;
1488
1489         df->governor->event_handler(df, DEVFREQ_GOV_UPDATE_INTERVAL, &value);
1490         ret = count;
1491
1492         return ret;
1493 }
1494 static DEVICE_ATTR_RW(polling_interval);
1495
1496 static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
1497                               const char *buf, size_t count)
1498 {
1499         struct devfreq *df = to_devfreq(dev);
1500         unsigned long value;
1501         int ret;
1502
1503         /*
1504          * Protect against theoretical sysfs writes between
1505          * device_add and dev_pm_qos_add_request
1506          */
1507         if (!dev_pm_qos_request_active(&df->user_min_freq_req))
1508                 return -EAGAIN;
1509
1510         ret = sscanf(buf, "%lu", &value);
1511         if (ret != 1)
1512                 return -EINVAL;
1513
1514         /* Round down to kHz for PM QoS */
1515         ret = dev_pm_qos_update_request(&df->user_min_freq_req,
1516                                         value / HZ_PER_KHZ);
1517         if (ret < 0)
1518                 return ret;
1519
1520         return count;
1521 }
1522
1523 static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
1524                              char *buf)
1525 {
1526         struct devfreq *df = to_devfreq(dev);
1527         unsigned long min_freq, max_freq;
1528
1529         mutex_lock(&df->lock);
1530         get_freq_range(df, &min_freq, &max_freq);
1531         mutex_unlock(&df->lock);
1532
1533         return sprintf(buf, "%lu\n", min_freq);
1534 }
1535 static DEVICE_ATTR_RW(min_freq);
1536
1537 static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
1538                               const char *buf, size_t count)
1539 {
1540         struct devfreq *df = to_devfreq(dev);
1541         unsigned long value;
1542         int ret;
1543
1544         /*
1545          * Protect against theoretical sysfs writes between
1546          * device_add and dev_pm_qos_add_request
1547          */
1548         if (!dev_pm_qos_request_active(&df->user_max_freq_req))
1549                 return -EINVAL;
1550
1551         ret = sscanf(buf, "%lu", &value);
1552         if (ret != 1)
1553                 return -EINVAL;
1554
1555         /*
1556          * PM QoS frequencies are in kHz so we need to convert. Convert by
1557          * rounding upwards so that the acceptable interval never shrinks.
1558          *
1559          * For example if the user writes "666666666" to sysfs this value will
1560          * be converted to 666667 kHz and back to 666667000 Hz before an OPP
1561          * lookup, this ensures that an OPP of 666666666Hz is still accepted.
1562          *
1563          * A value of zero means "no limit".
1564          */
1565         if (value)
1566                 value = DIV_ROUND_UP(value, HZ_PER_KHZ);
1567         else
1568                 value = PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE;
1569
1570         ret = dev_pm_qos_update_request(&df->user_max_freq_req, value);
1571         if (ret < 0)
1572                 return ret;
1573
1574         return count;
1575 }
1576
1577 static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
1578                              char *buf)
1579 {
1580         struct devfreq *df = to_devfreq(dev);
1581         unsigned long min_freq, max_freq;
1582
1583         mutex_lock(&df->lock);
1584         get_freq_range(df, &min_freq, &max_freq);
1585         mutex_unlock(&df->lock);
1586
1587         return sprintf(buf, "%lu\n", max_freq);
1588 }
1589 static DEVICE_ATTR_RW(max_freq);
1590
1591 static ssize_t available_frequencies_show(struct device *d,
1592                                           struct device_attribute *attr,
1593                                           char *buf)
1594 {
1595         struct devfreq *df = to_devfreq(d);
1596         ssize_t count = 0;
1597         int i;
1598
1599         if (!df->profile)
1600                 return -EINVAL;
1601
1602         mutex_lock(&df->lock);
1603
1604         for (i = 0; i < df->profile->max_state; i++)
1605                 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1606                                 "%lu ", df->profile->freq_table[i]);
1607
1608         mutex_unlock(&df->lock);
1609         /* Truncate the trailing space */
1610         if (count)
1611                 count--;
1612
1613         count += sprintf(&buf[count], "\n");
1614
1615         return count;
1616 }
1617 static DEVICE_ATTR_RO(available_frequencies);
1618
1619 static ssize_t trans_stat_show(struct device *dev,
1620                                struct device_attribute *attr, char *buf)
1621 {
1622         struct devfreq *df = to_devfreq(dev);
1623         ssize_t len;
1624         int i, j;
1625         unsigned int max_state;
1626
1627         if (!df->profile)
1628                 return -EINVAL;
1629         max_state = df->profile->max_state;
1630
1631         if (max_state == 0)
1632                 return sprintf(buf, "Not Supported.\n");
1633
1634         mutex_lock(&df->lock);
1635         if (!df->stop_polling &&
1636                         devfreq_update_status(df, df->previous_freq)) {
1637                 mutex_unlock(&df->lock);
1638                 return 0;
1639         }
1640         mutex_unlock(&df->lock);
1641
1642         len = sprintf(buf, "     From  :   To\n");
1643         len += sprintf(buf + len, "           :");
1644         for (i = 0; i < max_state; i++)
1645                 len += sprintf(buf + len, "%10lu",
1646                                 df->profile->freq_table[i]);
1647
1648         len += sprintf(buf + len, "   time(ms)\n");
1649
1650         for (i = 0; i < max_state; i++) {
1651                 if (df->profile->freq_table[i]
1652                                         == df->previous_freq) {
1653                         len += sprintf(buf + len, "*");
1654                 } else {
1655                         len += sprintf(buf + len, " ");
1656                 }
1657                 len += sprintf(buf + len, "%10lu:",
1658                                 df->profile->freq_table[i]);
1659                 for (j = 0; j < max_state; j++)
1660                         len += sprintf(buf + len, "%10u",
1661                                 df->stats.trans_table[(i * max_state) + j]);
1662
1663                 len += sprintf(buf + len, "%10llu\n", (u64)
1664                         jiffies64_to_msecs(df->stats.time_in_state[i]));
1665         }
1666
1667         len += sprintf(buf + len, "Total transition : %u\n",
1668                                         df->stats.total_trans);
1669         return len;
1670 }
1671
1672 static ssize_t trans_stat_store(struct device *dev,
1673                                 struct device_attribute *attr,
1674                                 const char *buf, size_t count)
1675 {
1676         struct devfreq *df = to_devfreq(dev);
1677         int err, value;
1678
1679         if (!df->profile)
1680                 return -EINVAL;
1681
1682         if (df->profile->max_state == 0)
1683                 return count;
1684
1685         err = kstrtoint(buf, 10, &value);
1686         if (err || value != 0)
1687                 return -EINVAL;
1688
1689         mutex_lock(&df->lock);
1690         memset(df->stats.time_in_state, 0, (df->profile->max_state *
1691                                         sizeof(*df->stats.time_in_state)));
1692         memset(df->stats.trans_table, 0, array3_size(sizeof(unsigned int),
1693                                         df->profile->max_state,
1694                                         df->profile->max_state));
1695         df->stats.total_trans = 0;
1696         df->stats.last_update = get_jiffies_64();
1697         mutex_unlock(&df->lock);
1698
1699         return count;
1700 }
1701 static DEVICE_ATTR_RW(trans_stat);
1702
1703 static ssize_t timer_show(struct device *dev,
1704                              struct device_attribute *attr, char *buf)
1705 {
1706         struct devfreq *df = to_devfreq(dev);
1707
1708         if (!df->profile)
1709                 return -EINVAL;
1710
1711         return sprintf(buf, "%s\n", timer_name[df->profile->timer]);
1712 }
1713
1714 static ssize_t timer_store(struct device *dev, struct device_attribute *attr,
1715                               const char *buf, size_t count)
1716 {
1717         struct devfreq *df = to_devfreq(dev);
1718         char str_timer[DEVFREQ_NAME_LEN + 1];
1719         int timer = -1;
1720         int ret = 0, i;
1721
1722         if (!df->governor || !df->profile)
1723                 return -EINVAL;
1724
1725         ret = sscanf(buf, "%16s", str_timer);
1726         if (ret != 1)
1727                 return -EINVAL;
1728
1729         for (i = 0; i < DEVFREQ_TIMER_NUM; i++) {
1730                 if (!strncmp(timer_name[i], str_timer, DEVFREQ_NAME_LEN)) {
1731                         timer = i;
1732                         break;
1733                 }
1734         }
1735
1736         if (timer < 0) {
1737                 ret = -EINVAL;
1738                 goto out;
1739         }
1740
1741         if (df->profile->timer == timer) {
1742                 ret = 0;
1743                 goto out;
1744         }
1745
1746         mutex_lock(&df->lock);
1747         df->profile->timer = timer;
1748         mutex_unlock(&df->lock);
1749
1750         ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1751         if (ret) {
1752                 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1753                          __func__, df->governor->name, ret);
1754                 goto out;
1755         }
1756
1757         ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1758         if (ret)
1759                 dev_warn(dev, "%s: Governor %s not started(%d)\n",
1760                          __func__, df->governor->name, ret);
1761 out:
1762         return ret ? ret : count;
1763 }
1764 static DEVICE_ATTR_RW(timer);
1765
1766 static struct attribute *devfreq_attrs[] = {
1767         &dev_attr_name.attr,
1768         &dev_attr_governor.attr,
1769         &dev_attr_available_governors.attr,
1770         &dev_attr_cur_freq.attr,
1771         &dev_attr_available_frequencies.attr,
1772         &dev_attr_target_freq.attr,
1773         &dev_attr_polling_interval.attr,
1774         &dev_attr_min_freq.attr,
1775         &dev_attr_max_freq.attr,
1776         &dev_attr_trans_stat.attr,
1777         &dev_attr_timer.attr,
1778         NULL,
1779 };
1780 ATTRIBUTE_GROUPS(devfreq);
1781
1782 /**
1783  * devfreq_summary_show() - Show the summary of the devfreq devices
1784  * @s:          seq_file instance to show the summary of devfreq devices
1785  * @data:       not used
1786  *
1787  * Show the summary of the devfreq devices via 'devfreq_summary' debugfs file.
1788  * It helps that user can know the detailed information of the devfreq devices.
1789  *
1790  * Return 0 always because it shows the information without any data change.
1791  */
1792 static int devfreq_summary_show(struct seq_file *s, void *data)
1793 {
1794         struct devfreq *devfreq;
1795         struct devfreq *p_devfreq = NULL;
1796         unsigned long cur_freq, min_freq, max_freq;
1797         unsigned int polling_ms;
1798         unsigned int timer;
1799
1800         seq_printf(s, "%-30s %-30s %-15s %-10s %10s %12s %12s %12s\n",
1801                         "dev",
1802                         "parent_dev",
1803                         "governor",
1804                         "timer",
1805                         "polling_ms",
1806                         "cur_freq_Hz",
1807                         "min_freq_Hz",
1808                         "max_freq_Hz");
1809         seq_printf(s, "%30s %30s %15s %10s %10s %12s %12s %12s\n",
1810                         "------------------------------",
1811                         "------------------------------",
1812                         "---------------",
1813                         "----------",
1814                         "----------",
1815                         "------------",
1816                         "------------",
1817                         "------------");
1818
1819         mutex_lock(&devfreq_list_lock);
1820
1821         list_for_each_entry_reverse(devfreq, &devfreq_list, node) {
1822 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE)
1823                 if (!strncmp(devfreq->governor_name, DEVFREQ_GOV_PASSIVE,
1824                                                         DEVFREQ_NAME_LEN)) {
1825                         struct devfreq_passive_data *data = devfreq->data;
1826
1827                         if (data)
1828                                 p_devfreq = data->parent;
1829                 } else {
1830                         p_devfreq = NULL;
1831                 }
1832 #endif
1833
1834                 mutex_lock(&devfreq->lock);
1835                 cur_freq = devfreq->previous_freq;
1836                 get_freq_range(devfreq, &min_freq, &max_freq);
1837                 polling_ms = devfreq->profile->polling_ms;
1838                 timer = devfreq->profile->timer;
1839                 mutex_unlock(&devfreq->lock);
1840
1841                 seq_printf(s,
1842                         "%-30s %-30s %-15s %-10s %10d %12ld %12ld %12ld\n",
1843                         dev_name(&devfreq->dev),
1844                         p_devfreq ? dev_name(&p_devfreq->dev) : "null",
1845                         devfreq->governor_name,
1846                         polling_ms ? timer_name[timer] : "null",
1847                         polling_ms,
1848                         cur_freq,
1849                         min_freq,
1850                         max_freq);
1851         }
1852
1853         mutex_unlock(&devfreq_list_lock);
1854
1855         return 0;
1856 }
1857 DEFINE_SHOW_ATTRIBUTE(devfreq_summary);
1858
1859 static int __init devfreq_init(void)
1860 {
1861         devfreq_class = class_create(THIS_MODULE, "devfreq");
1862         if (IS_ERR(devfreq_class)) {
1863                 pr_err("%s: couldn't create class\n", __FILE__);
1864                 return PTR_ERR(devfreq_class);
1865         }
1866
1867         devfreq_wq = create_freezable_workqueue("devfreq_wq");
1868         if (!devfreq_wq) {
1869                 class_destroy(devfreq_class);
1870                 pr_err("%s: couldn't create workqueue\n", __FILE__);
1871                 return -ENOMEM;
1872         }
1873         devfreq_class->dev_groups = devfreq_groups;
1874
1875         devfreq_debugfs = debugfs_create_dir("devfreq", NULL);
1876         debugfs_create_file("devfreq_summary", 0444,
1877                                 devfreq_debugfs, NULL,
1878                                 &devfreq_summary_fops);
1879
1880         return 0;
1881 }
1882 subsys_initcall(devfreq_init);
1883
1884 /*
1885  * The following are helper functions for devfreq user device drivers with
1886  * OPP framework.
1887  */
1888
1889 /**
1890  * devfreq_recommended_opp() - Helper function to get proper OPP for the
1891  *                           freq value given to target callback.
1892  * @dev:        The devfreq user device. (parent of devfreq)
1893  * @freq:       The frequency given to target function
1894  * @flags:      Flags handed from devfreq framework.
1895  *
1896  * The callers are required to call dev_pm_opp_put() for the returned OPP after
1897  * use.
1898  */
1899 struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1900                                            unsigned long *freq,
1901                                            u32 flags)
1902 {
1903         struct dev_pm_opp *opp;
1904
1905         if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1906                 /* The freq is an upper bound. opp should be lower */
1907                 opp = dev_pm_opp_find_freq_floor(dev, freq);
1908
1909                 /* If not available, use the closest opp */
1910                 if (opp == ERR_PTR(-ERANGE))
1911                         opp = dev_pm_opp_find_freq_ceil(dev, freq);
1912         } else {
1913                 /* The freq is an lower bound. opp should be higher */
1914                 opp = dev_pm_opp_find_freq_ceil(dev, freq);
1915
1916                 /* If not available, use the closest opp */
1917                 if (opp == ERR_PTR(-ERANGE))
1918                         opp = dev_pm_opp_find_freq_floor(dev, freq);
1919         }
1920
1921         return opp;
1922 }
1923 EXPORT_SYMBOL(devfreq_recommended_opp);
1924
1925 /**
1926  * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1927  *                                   for any changes in the OPP availability
1928  *                                   changes
1929  * @dev:        The devfreq user device. (parent of devfreq)
1930  * @devfreq:    The devfreq object.
1931  */
1932 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1933 {
1934         return dev_pm_opp_register_notifier(dev, &devfreq->nb);
1935 }
1936 EXPORT_SYMBOL(devfreq_register_opp_notifier);
1937
1938 /**
1939  * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1940  *                                     notified for any changes in the OPP
1941  *                                     availability changes anymore.
1942  * @dev:        The devfreq user device. (parent of devfreq)
1943  * @devfreq:    The devfreq object.
1944  *
1945  * At exit() callback of devfreq_dev_profile, this must be included if
1946  * devfreq_recommended_opp is used.
1947  */
1948 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1949 {
1950         return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
1951 }
1952 EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
1953
1954 static void devm_devfreq_opp_release(struct device *dev, void *res)
1955 {
1956         devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
1957 }
1958
1959 /**
1960  * devm_devfreq_register_opp_notifier() - Resource-managed
1961  *                                        devfreq_register_opp_notifier()
1962  * @dev:        The devfreq user device. (parent of devfreq)
1963  * @devfreq:    The devfreq object.
1964  */
1965 int devm_devfreq_register_opp_notifier(struct device *dev,
1966                                        struct devfreq *devfreq)
1967 {
1968         struct devfreq **ptr;
1969         int ret;
1970
1971         ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
1972         if (!ptr)
1973                 return -ENOMEM;
1974
1975         ret = devfreq_register_opp_notifier(dev, devfreq);
1976         if (ret) {
1977                 devres_free(ptr);
1978                 return ret;
1979         }
1980
1981         *ptr = devfreq;
1982         devres_add(dev, ptr);
1983
1984         return 0;
1985 }
1986 EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
1987
1988 /**
1989  * devm_devfreq_unregister_opp_notifier() - Resource-managed
1990  *                                          devfreq_unregister_opp_notifier()
1991  * @dev:        The devfreq user device. (parent of devfreq)
1992  * @devfreq:    The devfreq object.
1993  */
1994 void devm_devfreq_unregister_opp_notifier(struct device *dev,
1995                                          struct devfreq *devfreq)
1996 {
1997         WARN_ON(devres_release(dev, devm_devfreq_opp_release,
1998                                devm_devfreq_dev_match, devfreq));
1999 }
2000 EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
2001
2002 /**
2003  * devfreq_register_notifier() - Register a driver with devfreq
2004  * @devfreq:    The devfreq object.
2005  * @nb:         The notifier block to register.
2006  * @list:       DEVFREQ_TRANSITION_NOTIFIER.
2007  */
2008 int devfreq_register_notifier(struct devfreq *devfreq,
2009                               struct notifier_block *nb,
2010                               unsigned int list)
2011 {
2012         int ret = 0;
2013
2014         if (!devfreq)
2015                 return -EINVAL;
2016
2017         switch (list) {
2018         case DEVFREQ_TRANSITION_NOTIFIER:
2019                 ret = srcu_notifier_chain_register(
2020                                 &devfreq->transition_notifier_list, nb);
2021                 break;
2022         default:
2023                 ret = -EINVAL;
2024         }
2025
2026         return ret;
2027 }
2028 EXPORT_SYMBOL(devfreq_register_notifier);
2029
2030 /*
2031  * devfreq_unregister_notifier() - Unregister a driver with devfreq
2032  * @devfreq:    The devfreq object.
2033  * @nb:         The notifier block to be unregistered.
2034  * @list:       DEVFREQ_TRANSITION_NOTIFIER.
2035  */
2036 int devfreq_unregister_notifier(struct devfreq *devfreq,
2037                                 struct notifier_block *nb,
2038                                 unsigned int list)
2039 {
2040         int ret = 0;
2041
2042         if (!devfreq)
2043                 return -EINVAL;
2044
2045         switch (list) {
2046         case DEVFREQ_TRANSITION_NOTIFIER:
2047                 ret = srcu_notifier_chain_unregister(
2048                                 &devfreq->transition_notifier_list, nb);
2049                 break;
2050         default:
2051                 ret = -EINVAL;
2052         }
2053
2054         return ret;
2055 }
2056 EXPORT_SYMBOL(devfreq_unregister_notifier);
2057
2058 struct devfreq_notifier_devres {
2059         struct devfreq *devfreq;
2060         struct notifier_block *nb;
2061         unsigned int list;
2062 };
2063
2064 static void devm_devfreq_notifier_release(struct device *dev, void *res)
2065 {
2066         struct devfreq_notifier_devres *this = res;
2067
2068         devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
2069 }
2070
2071 /**
2072  * devm_devfreq_register_notifier()
2073  *      - Resource-managed devfreq_register_notifier()
2074  * @dev:        The devfreq user device. (parent of devfreq)
2075  * @devfreq:    The devfreq object.
2076  * @nb:         The notifier block to be unregistered.
2077  * @list:       DEVFREQ_TRANSITION_NOTIFIER.
2078  */
2079 int devm_devfreq_register_notifier(struct device *dev,
2080                                 struct devfreq *devfreq,
2081                                 struct notifier_block *nb,
2082                                 unsigned int list)
2083 {
2084         struct devfreq_notifier_devres *ptr;
2085         int ret;
2086
2087         ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
2088                                 GFP_KERNEL);
2089         if (!ptr)
2090                 return -ENOMEM;
2091
2092         ret = devfreq_register_notifier(devfreq, nb, list);
2093         if (ret) {
2094                 devres_free(ptr);
2095                 return ret;
2096         }
2097
2098         ptr->devfreq = devfreq;
2099         ptr->nb = nb;
2100         ptr->list = list;
2101         devres_add(dev, ptr);
2102
2103         return 0;
2104 }
2105 EXPORT_SYMBOL(devm_devfreq_register_notifier);
2106
2107 /**
2108  * devm_devfreq_unregister_notifier()
2109  *      - Resource-managed devfreq_unregister_notifier()
2110  * @dev:        The devfreq user device. (parent of devfreq)
2111  * @devfreq:    The devfreq object.
2112  * @nb:         The notifier block to be unregistered.
2113  * @list:       DEVFREQ_TRANSITION_NOTIFIER.
2114  */
2115 void devm_devfreq_unregister_notifier(struct device *dev,
2116                                       struct devfreq *devfreq,
2117                                       struct notifier_block *nb,
2118                                       unsigned int list)
2119 {
2120         WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
2121                                devm_devfreq_dev_match, devfreq));
2122 }
2123 EXPORT_SYMBOL(devm_devfreq_unregister_notifier);