GNU Linux-libre 4.19.304-gnu1
[releases.git] / drivers / devfreq / devfreq.c
1 /*
2  * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
3  *          for Non-CPU Devices.
4  *
5  * Copyright (C) 2011 Samsung Electronics
6  *      MyungJoo Ham <myungjoo.ham@samsung.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/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
35 static 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  */
42 static struct workqueue_struct *devfreq_wq;
43
44 /* The list of all device-devfreq governors */
45 static LIST_HEAD(devfreq_governor_list);
46 /* The list of all device-devfreq */
47 static LIST_HEAD(devfreq_list);
48 static 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  */
57 static 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
76 static 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
90 static 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  */
109 static 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
120 static 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  */
161 int 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
194 out:
195         devfreq->last_stat_updated = cur_time;
196         return ret;
197 }
198 EXPORT_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  */
207 static 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  */
237 static 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
269 static 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  */
301 int 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 }
366 EXPORT_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  */
373 static 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  */
398 void 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 }
405 EXPORT_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  */
415 void devfreq_monitor_stop(struct devfreq *devfreq)
416 {
417         cancel_delayed_work_sync(&devfreq->work);
418 }
419 EXPORT_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  */
433 void 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 }
446 EXPORT_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  */
456 void 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
476 out:
477         mutex_unlock(&devfreq->lock);
478 }
479 EXPORT_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  */
489 void 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         }
523 out:
524         mutex_unlock(&devfreq->lock);
525 }
526 EXPORT_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  */
537 static 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
557 out:
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  */
573 static 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         srcu_cleanup_notifier_head(&devfreq->transition_notifier_list);
586         kfree(devfreq);
587 }
588
589 /**
590  * devfreq_add_device() - Add devfreq feature to the device
591  * @dev:        the device to add devfreq feature.
592  * @profile:    device-specific profile to run devfreq.
593  * @governor_name:      name of the policy to choose frequency.
594  * @data:       private data for the governor. The devfreq framework does not
595  *              touch this value.
596  */
597 struct devfreq *devfreq_add_device(struct device *dev,
598                                    struct devfreq_dev_profile *profile,
599                                    const char *governor_name,
600                                    void *data)
601 {
602         struct devfreq *devfreq;
603         struct devfreq_governor *governor;
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, "%s", dev_name(dev));
665         err = device_register(&devfreq->dev);
666         if (err) {
667                 mutex_unlock(&devfreq->lock);
668                 put_device(&devfreq->dev);
669                 goto err_out;
670         }
671
672         devfreq->trans_table =
673                 devm_kzalloc(&devfreq->dev,
674                              array3_size(sizeof(unsigned int),
675                                          devfreq->profile->max_state,
676                                          devfreq->profile->max_state),
677                              GFP_KERNEL);
678         devfreq->time_in_state = devm_kcalloc(&devfreq->dev,
679                                                 devfreq->profile->max_state,
680                                                 sizeof(unsigned long),
681                                                 GFP_KERNEL);
682         devfreq->last_stat_updated = jiffies;
683
684         srcu_init_notifier_head(&devfreq->transition_notifier_list);
685
686         mutex_unlock(&devfreq->lock);
687
688         mutex_lock(&devfreq_list_lock);
689
690         governor = try_then_request_governor(devfreq->governor_name);
691         if (IS_ERR(governor)) {
692                 dev_err(dev, "%s: Unable to find governor for the device\n",
693                         __func__);
694                 err = PTR_ERR(governor);
695                 goto err_init;
696         }
697
698         devfreq->governor = governor;
699         err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
700                                                 NULL);
701         if (err) {
702                 dev_err(dev, "%s: Unable to start governor for the device\n",
703                         __func__);
704                 goto err_init;
705         }
706
707         list_add(&devfreq->node, &devfreq_list);
708
709         mutex_unlock(&devfreq_list_lock);
710
711         return devfreq;
712
713 err_init:
714         mutex_unlock(&devfreq_list_lock);
715
716         devfreq_remove_device(devfreq);
717         devfreq = NULL;
718 err_dev:
719         if (devfreq)
720                 kfree(devfreq);
721 err_out:
722         return ERR_PTR(err);
723 }
724 EXPORT_SYMBOL(devfreq_add_device);
725
726 /**
727  * devfreq_remove_device() - Remove devfreq feature from a device.
728  * @devfreq:    the devfreq instance to be removed
729  *
730  * The opposite of devfreq_add_device().
731  */
732 int devfreq_remove_device(struct devfreq *devfreq)
733 {
734         if (!devfreq)
735                 return -EINVAL;
736
737         if (devfreq->governor)
738                 devfreq->governor->event_handler(devfreq,
739                                                  DEVFREQ_GOV_STOP, NULL);
740         device_unregister(&devfreq->dev);
741
742         return 0;
743 }
744 EXPORT_SYMBOL(devfreq_remove_device);
745
746 static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
747 {
748         struct devfreq **r = res;
749
750         if (WARN_ON(!r || !*r))
751                 return 0;
752
753         return *r == data;
754 }
755
756 static void devm_devfreq_dev_release(struct device *dev, void *res)
757 {
758         devfreq_remove_device(*(struct devfreq **)res);
759 }
760
761 /**
762  * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
763  * @dev:        the device to add devfreq feature.
764  * @profile:    device-specific profile to run devfreq.
765  * @governor_name:      name of the policy to choose frequency.
766  * @data:       private data for the governor. The devfreq framework does not
767  *              touch this value.
768  *
769  * This function manages automatically the memory of devfreq device using device
770  * resource management and simplify the free operation for memory of devfreq
771  * device.
772  */
773 struct devfreq *devm_devfreq_add_device(struct device *dev,
774                                         struct devfreq_dev_profile *profile,
775                                         const char *governor_name,
776                                         void *data)
777 {
778         struct devfreq **ptr, *devfreq;
779
780         ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
781         if (!ptr)
782                 return ERR_PTR(-ENOMEM);
783
784         devfreq = devfreq_add_device(dev, profile, governor_name, data);
785         if (IS_ERR(devfreq)) {
786                 devres_free(ptr);
787                 return devfreq;
788         }
789
790         *ptr = devfreq;
791         devres_add(dev, ptr);
792
793         return devfreq;
794 }
795 EXPORT_SYMBOL(devm_devfreq_add_device);
796
797 #ifdef CONFIG_OF
798 /*
799  * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
800  * @dev - instance to the given device
801  * @index - index into list of devfreq
802  *
803  * return the instance of devfreq device
804  */
805 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
806 {
807         struct device_node *node;
808         struct devfreq *devfreq;
809
810         if (!dev)
811                 return ERR_PTR(-EINVAL);
812
813         if (!dev->of_node)
814                 return ERR_PTR(-EINVAL);
815
816         node = of_parse_phandle(dev->of_node, "devfreq", index);
817         if (!node)
818                 return ERR_PTR(-ENODEV);
819
820         mutex_lock(&devfreq_list_lock);
821         list_for_each_entry(devfreq, &devfreq_list, node) {
822                 if (devfreq->dev.parent
823                         && devfreq->dev.parent->of_node == node) {
824                         mutex_unlock(&devfreq_list_lock);
825                         of_node_put(node);
826                         return devfreq;
827                 }
828         }
829         mutex_unlock(&devfreq_list_lock);
830         of_node_put(node);
831
832         return ERR_PTR(-EPROBE_DEFER);
833 }
834 #else
835 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
836 {
837         return ERR_PTR(-ENODEV);
838 }
839 #endif /* CONFIG_OF */
840 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
841
842 /**
843  * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
844  * @dev:        the device to add devfreq feature.
845  * @devfreq:    the devfreq instance to be removed
846  */
847 void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
848 {
849         WARN_ON(devres_release(dev, devm_devfreq_dev_release,
850                                devm_devfreq_dev_match, devfreq));
851 }
852 EXPORT_SYMBOL(devm_devfreq_remove_device);
853
854 /**
855  * devfreq_suspend_device() - Suspend devfreq of a device.
856  * @devfreq: the devfreq instance to be suspended
857  *
858  * This function is intended to be called by the pm callbacks
859  * (e.g., runtime_suspend, suspend) of the device driver that
860  * holds the devfreq.
861  */
862 int devfreq_suspend_device(struct devfreq *devfreq)
863 {
864         if (!devfreq)
865                 return -EINVAL;
866
867         if (!devfreq->governor)
868                 return 0;
869
870         return devfreq->governor->event_handler(devfreq,
871                                 DEVFREQ_GOV_SUSPEND, NULL);
872 }
873 EXPORT_SYMBOL(devfreq_suspend_device);
874
875 /**
876  * devfreq_resume_device() - Resume devfreq of a device.
877  * @devfreq: the devfreq instance to be resumed
878  *
879  * This function is intended to be called by the pm callbacks
880  * (e.g., runtime_resume, resume) of the device driver that
881  * holds the devfreq.
882  */
883 int devfreq_resume_device(struct devfreq *devfreq)
884 {
885         if (!devfreq)
886                 return -EINVAL;
887
888         if (!devfreq->governor)
889                 return 0;
890
891         return devfreq->governor->event_handler(devfreq,
892                                 DEVFREQ_GOV_RESUME, NULL);
893 }
894 EXPORT_SYMBOL(devfreq_resume_device);
895
896 /**
897  * devfreq_add_governor() - Add devfreq governor
898  * @governor:   the devfreq governor to be added
899  */
900 int devfreq_add_governor(struct devfreq_governor *governor)
901 {
902         struct devfreq_governor *g;
903         struct devfreq *devfreq;
904         int err = 0;
905
906         if (!governor) {
907                 pr_err("%s: Invalid parameters.\n", __func__);
908                 return -EINVAL;
909         }
910
911         mutex_lock(&devfreq_list_lock);
912         g = find_devfreq_governor(governor->name);
913         if (!IS_ERR(g)) {
914                 pr_err("%s: governor %s already registered\n", __func__,
915                        g->name);
916                 err = -EINVAL;
917                 goto err_out;
918         }
919
920         list_add(&governor->node, &devfreq_governor_list);
921
922         list_for_each_entry(devfreq, &devfreq_list, node) {
923                 int ret = 0;
924                 struct device *dev = devfreq->dev.parent;
925
926                 if (!strncmp(devfreq->governor_name, governor->name,
927                              DEVFREQ_NAME_LEN)) {
928                         /* The following should never occur */
929                         if (devfreq->governor) {
930                                 dev_warn(dev,
931                                          "%s: Governor %s already present\n",
932                                          __func__, devfreq->governor->name);
933                                 ret = devfreq->governor->event_handler(devfreq,
934                                                         DEVFREQ_GOV_STOP, NULL);
935                                 if (ret) {
936                                         dev_warn(dev,
937                                                  "%s: Governor %s stop = %d\n",
938                                                  __func__,
939                                                  devfreq->governor->name, ret);
940                                 }
941                                 /* Fall through */
942                         }
943                         devfreq->governor = governor;
944                         ret = devfreq->governor->event_handler(devfreq,
945                                                 DEVFREQ_GOV_START, NULL);
946                         if (ret) {
947                                 dev_warn(dev, "%s: Governor %s start=%d\n",
948                                          __func__, devfreq->governor->name,
949                                          ret);
950                         }
951                 }
952         }
953
954 err_out:
955         mutex_unlock(&devfreq_list_lock);
956
957         return err;
958 }
959 EXPORT_SYMBOL(devfreq_add_governor);
960
961 /**
962  * devfreq_remove_governor() - Remove devfreq feature from a device.
963  * @governor:   the devfreq governor to be removed
964  */
965 int devfreq_remove_governor(struct devfreq_governor *governor)
966 {
967         struct devfreq_governor *g;
968         struct devfreq *devfreq;
969         int err = 0;
970
971         if (!governor) {
972                 pr_err("%s: Invalid parameters.\n", __func__);
973                 return -EINVAL;
974         }
975
976         mutex_lock(&devfreq_list_lock);
977         g = find_devfreq_governor(governor->name);
978         if (IS_ERR(g)) {
979                 pr_err("%s: governor %s not registered\n", __func__,
980                        governor->name);
981                 err = PTR_ERR(g);
982                 goto err_out;
983         }
984         list_for_each_entry(devfreq, &devfreq_list, node) {
985                 int ret;
986                 struct device *dev = devfreq->dev.parent;
987
988                 if (!strncmp(devfreq->governor_name, governor->name,
989                              DEVFREQ_NAME_LEN)) {
990                         /* we should have a devfreq governor! */
991                         if (!devfreq->governor) {
992                                 dev_warn(dev, "%s: Governor %s NOT present\n",
993                                          __func__, governor->name);
994                                 continue;
995                                 /* Fall through */
996                         }
997                         ret = devfreq->governor->event_handler(devfreq,
998                                                 DEVFREQ_GOV_STOP, NULL);
999                         if (ret) {
1000                                 dev_warn(dev, "%s: Governor %s stop=%d\n",
1001                                          __func__, devfreq->governor->name,
1002                                          ret);
1003                         }
1004                         devfreq->governor = NULL;
1005                 }
1006         }
1007
1008         list_del(&governor->node);
1009 err_out:
1010         mutex_unlock(&devfreq_list_lock);
1011
1012         return err;
1013 }
1014 EXPORT_SYMBOL(devfreq_remove_governor);
1015
1016 static ssize_t name_show(struct device *dev,
1017                         struct device_attribute *attr, char *buf)
1018 {
1019         struct devfreq *devfreq = to_devfreq(dev);
1020         return sprintf(buf, "%s\n", dev_name(devfreq->dev.parent));
1021 }
1022 static DEVICE_ATTR_RO(name);
1023
1024 static ssize_t governor_show(struct device *dev,
1025                              struct device_attribute *attr, char *buf)
1026 {
1027         if (!to_devfreq(dev)->governor)
1028                 return -EINVAL;
1029
1030         return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
1031 }
1032
1033 static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
1034                               const char *buf, size_t count)
1035 {
1036         struct devfreq *df = to_devfreq(dev);
1037         int ret;
1038         char str_governor[DEVFREQ_NAME_LEN + 1];
1039         struct devfreq_governor *governor;
1040
1041         ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
1042         if (ret != 1)
1043                 return -EINVAL;
1044
1045         mutex_lock(&devfreq_list_lock);
1046         governor = try_then_request_governor(str_governor);
1047         if (IS_ERR(governor)) {
1048                 ret = PTR_ERR(governor);
1049                 goto out;
1050         }
1051         if (df->governor == governor) {
1052                 ret = 0;
1053                 goto out;
1054         } else if ((df->governor && df->governor->immutable) ||
1055                                         governor->immutable) {
1056                 ret = -EINVAL;
1057                 goto out;
1058         }
1059
1060         if (df->governor) {
1061                 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1062                 if (ret) {
1063                         dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1064                                  __func__, df->governor->name, ret);
1065                         goto out;
1066                 }
1067         }
1068         df->governor = governor;
1069         strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
1070         ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1071         if (ret)
1072                 dev_warn(dev, "%s: Governor %s not started(%d)\n",
1073                          __func__, df->governor->name, ret);
1074 out:
1075         mutex_unlock(&devfreq_list_lock);
1076
1077         if (!ret)
1078                 ret = count;
1079         return ret;
1080 }
1081 static DEVICE_ATTR_RW(governor);
1082
1083 static ssize_t available_governors_show(struct device *d,
1084                                         struct device_attribute *attr,
1085                                         char *buf)
1086 {
1087         struct devfreq *df = to_devfreq(d);
1088         ssize_t count = 0;
1089
1090         mutex_lock(&devfreq_list_lock);
1091
1092         /*
1093          * The devfreq with immutable governor (e.g., passive) shows
1094          * only own governor.
1095          */
1096         if (df->governor && df->governor->immutable) {
1097                 count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
1098                                    "%s ", df->governor_name);
1099         /*
1100          * The devfreq device shows the registered governor except for
1101          * immutable governors such as passive governor .
1102          */
1103         } else {
1104                 struct devfreq_governor *governor;
1105
1106                 list_for_each_entry(governor, &devfreq_governor_list, node) {
1107                         if (governor->immutable)
1108                                 continue;
1109                         count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1110                                            "%s ", governor->name);
1111                 }
1112         }
1113
1114         mutex_unlock(&devfreq_list_lock);
1115
1116         /* Truncate the trailing space */
1117         if (count)
1118                 count--;
1119
1120         count += sprintf(&buf[count], "\n");
1121
1122         return count;
1123 }
1124 static DEVICE_ATTR_RO(available_governors);
1125
1126 static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
1127                              char *buf)
1128 {
1129         unsigned long freq;
1130         struct devfreq *devfreq = to_devfreq(dev);
1131
1132         if (devfreq->profile->get_cur_freq &&
1133                 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
1134                 return sprintf(buf, "%lu\n", freq);
1135
1136         return sprintf(buf, "%lu\n", devfreq->previous_freq);
1137 }
1138 static DEVICE_ATTR_RO(cur_freq);
1139
1140 static ssize_t target_freq_show(struct device *dev,
1141                                 struct device_attribute *attr, char *buf)
1142 {
1143         return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
1144 }
1145 static DEVICE_ATTR_RO(target_freq);
1146
1147 static ssize_t polling_interval_show(struct device *dev,
1148                                      struct device_attribute *attr, char *buf)
1149 {
1150         return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
1151 }
1152
1153 static ssize_t polling_interval_store(struct device *dev,
1154                                       struct device_attribute *attr,
1155                                       const char *buf, size_t count)
1156 {
1157         struct devfreq *df = to_devfreq(dev);
1158         unsigned int value;
1159         int ret;
1160
1161         if (!df->governor)
1162                 return -EINVAL;
1163
1164         ret = sscanf(buf, "%u", &value);
1165         if (ret != 1)
1166                 return -EINVAL;
1167
1168         df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
1169         ret = count;
1170
1171         return ret;
1172 }
1173 static DEVICE_ATTR_RW(polling_interval);
1174
1175 static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
1176                               const char *buf, size_t count)
1177 {
1178         struct devfreq *df = to_devfreq(dev);
1179         unsigned long value;
1180         int ret;
1181
1182         ret = sscanf(buf, "%lu", &value);
1183         if (ret != 1)
1184                 return -EINVAL;
1185
1186         mutex_lock(&df->lock);
1187
1188         if (value) {
1189                 if (value > df->max_freq) {
1190                         ret = -EINVAL;
1191                         goto unlock;
1192                 }
1193         } else {
1194                 unsigned long *freq_table = df->profile->freq_table;
1195
1196                 /* Get minimum frequency according to sorting order */
1197                 if (freq_table[0] < freq_table[df->profile->max_state - 1])
1198                         value = freq_table[0];
1199                 else
1200                         value = freq_table[df->profile->max_state - 1];
1201         }
1202
1203         df->min_freq = value;
1204         update_devfreq(df);
1205         ret = count;
1206 unlock:
1207         mutex_unlock(&df->lock);
1208         return ret;
1209 }
1210
1211 static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
1212                              char *buf)
1213 {
1214         struct devfreq *df = to_devfreq(dev);
1215
1216         return sprintf(buf, "%lu\n", MAX(df->scaling_min_freq, df->min_freq));
1217 }
1218
1219 static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
1220                               const char *buf, size_t count)
1221 {
1222         struct devfreq *df = to_devfreq(dev);
1223         unsigned long value;
1224         int ret;
1225
1226         ret = sscanf(buf, "%lu", &value);
1227         if (ret != 1)
1228                 return -EINVAL;
1229
1230         mutex_lock(&df->lock);
1231
1232         if (value) {
1233                 if (value < df->min_freq) {
1234                         ret = -EINVAL;
1235                         goto unlock;
1236                 }
1237         } else {
1238                 unsigned long *freq_table = df->profile->freq_table;
1239
1240                 /* Get maximum frequency according to sorting order */
1241                 if (freq_table[0] < freq_table[df->profile->max_state - 1])
1242                         value = freq_table[df->profile->max_state - 1];
1243                 else
1244                         value = freq_table[0];
1245         }
1246
1247         df->max_freq = value;
1248         update_devfreq(df);
1249         ret = count;
1250 unlock:
1251         mutex_unlock(&df->lock);
1252         return ret;
1253 }
1254 static DEVICE_ATTR_RW(min_freq);
1255
1256 static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
1257                              char *buf)
1258 {
1259         struct devfreq *df = to_devfreq(dev);
1260
1261         return sprintf(buf, "%lu\n", MIN(df->scaling_max_freq, df->max_freq));
1262 }
1263 static DEVICE_ATTR_RW(max_freq);
1264
1265 static ssize_t available_frequencies_show(struct device *d,
1266                                           struct device_attribute *attr,
1267                                           char *buf)
1268 {
1269         struct devfreq *df = to_devfreq(d);
1270         ssize_t count = 0;
1271         int i;
1272
1273         mutex_lock(&df->lock);
1274
1275         for (i = 0; i < df->profile->max_state; i++)
1276                 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1277                                 "%lu ", df->profile->freq_table[i]);
1278
1279         mutex_unlock(&df->lock);
1280         /* Truncate the trailing space */
1281         if (count)
1282                 count--;
1283
1284         count += sprintf(&buf[count], "\n");
1285
1286         return count;
1287 }
1288 static DEVICE_ATTR_RO(available_frequencies);
1289
1290 static ssize_t trans_stat_show(struct device *dev,
1291                                struct device_attribute *attr, char *buf)
1292 {
1293         struct devfreq *devfreq = to_devfreq(dev);
1294         ssize_t len;
1295         int i, j;
1296         unsigned int max_state = devfreq->profile->max_state;
1297
1298         if (max_state == 0)
1299                 return sprintf(buf, "Not Supported.\n");
1300
1301         mutex_lock(&devfreq->lock);
1302         if (!devfreq->stop_polling &&
1303                         devfreq_update_status(devfreq, devfreq->previous_freq)) {
1304                 mutex_unlock(&devfreq->lock);
1305                 return 0;
1306         }
1307         mutex_unlock(&devfreq->lock);
1308
1309         len = sprintf(buf, "     From  :   To\n");
1310         len += sprintf(buf + len, "           :");
1311         for (i = 0; i < max_state; i++)
1312                 len += sprintf(buf + len, "%10lu",
1313                                 devfreq->profile->freq_table[i]);
1314
1315         len += sprintf(buf + len, "   time(ms)\n");
1316
1317         for (i = 0; i < max_state; i++) {
1318                 if (devfreq->profile->freq_table[i]
1319                                         == devfreq->previous_freq) {
1320                         len += sprintf(buf + len, "*");
1321                 } else {
1322                         len += sprintf(buf + len, " ");
1323                 }
1324                 len += sprintf(buf + len, "%10lu:",
1325                                 devfreq->profile->freq_table[i]);
1326                 for (j = 0; j < max_state; j++)
1327                         len += sprintf(buf + len, "%10u",
1328                                 devfreq->trans_table[(i * max_state) + j]);
1329                 len += sprintf(buf + len, "%10u\n",
1330                         jiffies_to_msecs(devfreq->time_in_state[i]));
1331         }
1332
1333         len += sprintf(buf + len, "Total transition : %u\n",
1334                                         devfreq->total_trans);
1335         return len;
1336 }
1337 static DEVICE_ATTR_RO(trans_stat);
1338
1339 static struct attribute *devfreq_attrs[] = {
1340         &dev_attr_name.attr,
1341         &dev_attr_governor.attr,
1342         &dev_attr_available_governors.attr,
1343         &dev_attr_cur_freq.attr,
1344         &dev_attr_available_frequencies.attr,
1345         &dev_attr_target_freq.attr,
1346         &dev_attr_polling_interval.attr,
1347         &dev_attr_min_freq.attr,
1348         &dev_attr_max_freq.attr,
1349         &dev_attr_trans_stat.attr,
1350         NULL,
1351 };
1352 ATTRIBUTE_GROUPS(devfreq);
1353
1354 static int __init devfreq_init(void)
1355 {
1356         devfreq_class = class_create(THIS_MODULE, "devfreq");
1357         if (IS_ERR(devfreq_class)) {
1358                 pr_err("%s: couldn't create class\n", __FILE__);
1359                 return PTR_ERR(devfreq_class);
1360         }
1361
1362         devfreq_wq = create_freezable_workqueue("devfreq_wq");
1363         if (!devfreq_wq) {
1364                 class_destroy(devfreq_class);
1365                 pr_err("%s: couldn't create workqueue\n", __FILE__);
1366                 return -ENOMEM;
1367         }
1368         devfreq_class->dev_groups = devfreq_groups;
1369
1370         return 0;
1371 }
1372 subsys_initcall(devfreq_init);
1373
1374 /*
1375  * The following are helper functions for devfreq user device drivers with
1376  * OPP framework.
1377  */
1378
1379 /**
1380  * devfreq_recommended_opp() - Helper function to get proper OPP for the
1381  *                           freq value given to target callback.
1382  * @dev:        The devfreq user device. (parent of devfreq)
1383  * @freq:       The frequency given to target function
1384  * @flags:      Flags handed from devfreq framework.
1385  *
1386  * The callers are required to call dev_pm_opp_put() for the returned OPP after
1387  * use.
1388  */
1389 struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1390                                            unsigned long *freq,
1391                                            u32 flags)
1392 {
1393         struct dev_pm_opp *opp;
1394
1395         if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1396                 /* The freq is an upper bound. opp should be lower */
1397                 opp = dev_pm_opp_find_freq_floor(dev, freq);
1398
1399                 /* If not available, use the closest opp */
1400                 if (opp == ERR_PTR(-ERANGE))
1401                         opp = dev_pm_opp_find_freq_ceil(dev, freq);
1402         } else {
1403                 /* The freq is an lower bound. opp should be higher */
1404                 opp = dev_pm_opp_find_freq_ceil(dev, freq);
1405
1406                 /* If not available, use the closest opp */
1407                 if (opp == ERR_PTR(-ERANGE))
1408                         opp = dev_pm_opp_find_freq_floor(dev, freq);
1409         }
1410
1411         return opp;
1412 }
1413 EXPORT_SYMBOL(devfreq_recommended_opp);
1414
1415 /**
1416  * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1417  *                                 for any changes in the OPP availability
1418  *                                 changes
1419  * @dev:        The devfreq user device. (parent of devfreq)
1420  * @devfreq:    The devfreq object.
1421  */
1422 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1423 {
1424         return dev_pm_opp_register_notifier(dev, &devfreq->nb);
1425 }
1426 EXPORT_SYMBOL(devfreq_register_opp_notifier);
1427
1428 /**
1429  * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1430  *                                   notified for any changes in the OPP
1431  *                                   availability changes anymore.
1432  * @dev:        The devfreq user device. (parent of devfreq)
1433  * @devfreq:    The devfreq object.
1434  *
1435  * At exit() callback of devfreq_dev_profile, this must be included if
1436  * devfreq_recommended_opp is used.
1437  */
1438 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1439 {
1440         return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
1441 }
1442 EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
1443
1444 static void devm_devfreq_opp_release(struct device *dev, void *res)
1445 {
1446         devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
1447 }
1448
1449 /**
1450  * devm_ devfreq_register_opp_notifier()
1451  *              - Resource-managed devfreq_register_opp_notifier()
1452  * @dev:        The devfreq user device. (parent of devfreq)
1453  * @devfreq:    The devfreq object.
1454  */
1455 int devm_devfreq_register_opp_notifier(struct device *dev,
1456                                        struct devfreq *devfreq)
1457 {
1458         struct devfreq **ptr;
1459         int ret;
1460
1461         ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
1462         if (!ptr)
1463                 return -ENOMEM;
1464
1465         ret = devfreq_register_opp_notifier(dev, devfreq);
1466         if (ret) {
1467                 devres_free(ptr);
1468                 return ret;
1469         }
1470
1471         *ptr = devfreq;
1472         devres_add(dev, ptr);
1473
1474         return 0;
1475 }
1476 EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
1477
1478 /**
1479  * devm_devfreq_unregister_opp_notifier()
1480  *              - Resource-managed devfreq_unregister_opp_notifier()
1481  * @dev:        The devfreq user device. (parent of devfreq)
1482  * @devfreq:    The devfreq object.
1483  */
1484 void devm_devfreq_unregister_opp_notifier(struct device *dev,
1485                                          struct devfreq *devfreq)
1486 {
1487         WARN_ON(devres_release(dev, devm_devfreq_opp_release,
1488                                devm_devfreq_dev_match, devfreq));
1489 }
1490 EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
1491
1492 /**
1493  * devfreq_register_notifier() - Register a driver with devfreq
1494  * @devfreq:    The devfreq object.
1495  * @nb:         The notifier block to register.
1496  * @list:       DEVFREQ_TRANSITION_NOTIFIER.
1497  */
1498 int devfreq_register_notifier(struct devfreq *devfreq,
1499                                 struct notifier_block *nb,
1500                                 unsigned int list)
1501 {
1502         int ret = 0;
1503
1504         if (!devfreq)
1505                 return -EINVAL;
1506
1507         switch (list) {
1508         case DEVFREQ_TRANSITION_NOTIFIER:
1509                 ret = srcu_notifier_chain_register(
1510                                 &devfreq->transition_notifier_list, nb);
1511                 break;
1512         default:
1513                 ret = -EINVAL;
1514         }
1515
1516         return ret;
1517 }
1518 EXPORT_SYMBOL(devfreq_register_notifier);
1519
1520 /*
1521  * devfreq_unregister_notifier() - Unregister a driver with devfreq
1522  * @devfreq:    The devfreq object.
1523  * @nb:         The notifier block to be unregistered.
1524  * @list:       DEVFREQ_TRANSITION_NOTIFIER.
1525  */
1526 int devfreq_unregister_notifier(struct devfreq *devfreq,
1527                                 struct notifier_block *nb,
1528                                 unsigned int list)
1529 {
1530         int ret = 0;
1531
1532         if (!devfreq)
1533                 return -EINVAL;
1534
1535         switch (list) {
1536         case DEVFREQ_TRANSITION_NOTIFIER:
1537                 ret = srcu_notifier_chain_unregister(
1538                                 &devfreq->transition_notifier_list, nb);
1539                 break;
1540         default:
1541                 ret = -EINVAL;
1542         }
1543
1544         return ret;
1545 }
1546 EXPORT_SYMBOL(devfreq_unregister_notifier);
1547
1548 struct devfreq_notifier_devres {
1549         struct devfreq *devfreq;
1550         struct notifier_block *nb;
1551         unsigned int list;
1552 };
1553
1554 static void devm_devfreq_notifier_release(struct device *dev, void *res)
1555 {
1556         struct devfreq_notifier_devres *this = res;
1557
1558         devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
1559 }
1560
1561 /**
1562  * devm_devfreq_register_notifier()
1563         - Resource-managed devfreq_register_notifier()
1564  * @dev:        The devfreq user device. (parent of devfreq)
1565  * @devfreq:    The devfreq object.
1566  * @nb:         The notifier block to be unregistered.
1567  * @list:       DEVFREQ_TRANSITION_NOTIFIER.
1568  */
1569 int devm_devfreq_register_notifier(struct device *dev,
1570                                 struct devfreq *devfreq,
1571                                 struct notifier_block *nb,
1572                                 unsigned int list)
1573 {
1574         struct devfreq_notifier_devres *ptr;
1575         int ret;
1576
1577         ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
1578                                 GFP_KERNEL);
1579         if (!ptr)
1580                 return -ENOMEM;
1581
1582         ret = devfreq_register_notifier(devfreq, nb, list);
1583         if (ret) {
1584                 devres_free(ptr);
1585                 return ret;
1586         }
1587
1588         ptr->devfreq = devfreq;
1589         ptr->nb = nb;
1590         ptr->list = list;
1591         devres_add(dev, ptr);
1592
1593         return 0;
1594 }
1595 EXPORT_SYMBOL(devm_devfreq_register_notifier);
1596
1597 /**
1598  * devm_devfreq_unregister_notifier()
1599         - Resource-managed devfreq_unregister_notifier()
1600  * @dev:        The devfreq user device. (parent of devfreq)
1601  * @devfreq:    The devfreq object.
1602  * @nb:         The notifier block to be unregistered.
1603  * @list:       DEVFREQ_TRANSITION_NOTIFIER.
1604  */
1605 void devm_devfreq_unregister_notifier(struct device *dev,
1606                                 struct devfreq *devfreq,
1607                                 struct notifier_block *nb,
1608                                 unsigned int list)
1609 {
1610         WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
1611                                devm_devfreq_dev_match, devfreq));
1612 }
1613 EXPORT_SYMBOL(devm_devfreq_unregister_notifier);