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