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