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