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