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