]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/devfreq/devfreq.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf
[linux.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 = cur_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         if (devfreq->governor->interrupt_driven)
414                 return;
415
416         INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
417         if (devfreq->profile->polling_ms)
418                 queue_delayed_work(devfreq_wq, &devfreq->work,
419                         msecs_to_jiffies(devfreq->profile->polling_ms));
420 }
421 EXPORT_SYMBOL(devfreq_monitor_start);
422
423 /**
424  * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
425  * @devfreq:    the devfreq instance.
426  *
427  * Helper function to stop devfreq device load monitoring. Function
428  * to be called from governor in response to DEVFREQ_GOV_STOP
429  * event when device is removed from devfreq framework.
430  */
431 void devfreq_monitor_stop(struct devfreq *devfreq)
432 {
433         if (devfreq->governor->interrupt_driven)
434                 return;
435
436         cancel_delayed_work_sync(&devfreq->work);
437 }
438 EXPORT_SYMBOL(devfreq_monitor_stop);
439
440 /**
441  * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
442  * @devfreq:    the devfreq instance.
443  *
444  * Helper function to suspend devfreq device load monitoring. Function
445  * to be called from governor in response to DEVFREQ_GOV_SUSPEND
446  * event or when polling interval is set to zero.
447  *
448  * Note: Though this function is same as devfreq_monitor_stop(),
449  * intentionally kept separate to provide hooks for collecting
450  * transition statistics.
451  */
452 void devfreq_monitor_suspend(struct devfreq *devfreq)
453 {
454         mutex_lock(&devfreq->lock);
455         if (devfreq->stop_polling) {
456                 mutex_unlock(&devfreq->lock);
457                 return;
458         }
459
460         devfreq_update_status(devfreq, devfreq->previous_freq);
461         devfreq->stop_polling = true;
462         mutex_unlock(&devfreq->lock);
463
464         if (devfreq->governor->interrupt_driven)
465                 return;
466
467         cancel_delayed_work_sync(&devfreq->work);
468 }
469 EXPORT_SYMBOL(devfreq_monitor_suspend);
470
471 /**
472  * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
473  * @devfreq:    the devfreq instance.
474  *
475  * Helper function to resume devfreq device load monitoring. Function
476  * to be called from governor in response to DEVFREQ_GOV_RESUME
477  * event or when polling interval is set to non-zero.
478  */
479 void devfreq_monitor_resume(struct devfreq *devfreq)
480 {
481         unsigned long freq;
482
483         mutex_lock(&devfreq->lock);
484         if (!devfreq->stop_polling)
485                 goto out;
486
487         if (devfreq->governor->interrupt_driven)
488                 goto out_update;
489
490         if (!delayed_work_pending(&devfreq->work) &&
491                         devfreq->profile->polling_ms)
492                 queue_delayed_work(devfreq_wq, &devfreq->work,
493                         msecs_to_jiffies(devfreq->profile->polling_ms));
494
495 out_update:
496         devfreq->last_stat_updated = jiffies;
497         devfreq->stop_polling = false;
498
499         if (devfreq->profile->get_cur_freq &&
500                 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
501                 devfreq->previous_freq = freq;
502
503 out:
504         mutex_unlock(&devfreq->lock);
505 }
506 EXPORT_SYMBOL(devfreq_monitor_resume);
507
508 /**
509  * devfreq_interval_update() - Update device devfreq monitoring interval
510  * @devfreq:    the devfreq instance.
511  * @delay:      new polling interval to be set.
512  *
513  * Helper function to set new load monitoring polling interval. Function
514  * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
515  */
516 void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
517 {
518         unsigned int cur_delay = devfreq->profile->polling_ms;
519         unsigned int new_delay = *delay;
520
521         mutex_lock(&devfreq->lock);
522         devfreq->profile->polling_ms = new_delay;
523
524         if (devfreq->stop_polling)
525                 goto out;
526
527         if (devfreq->governor->interrupt_driven)
528                 goto out;
529
530         /* if new delay is zero, stop polling */
531         if (!new_delay) {
532                 mutex_unlock(&devfreq->lock);
533                 cancel_delayed_work_sync(&devfreq->work);
534                 return;
535         }
536
537         /* if current delay is zero, start polling with new delay */
538         if (!cur_delay) {
539                 queue_delayed_work(devfreq_wq, &devfreq->work,
540                         msecs_to_jiffies(devfreq->profile->polling_ms));
541                 goto out;
542         }
543
544         /* if current delay is greater than new delay, restart polling */
545         if (cur_delay > new_delay) {
546                 mutex_unlock(&devfreq->lock);
547                 cancel_delayed_work_sync(&devfreq->work);
548                 mutex_lock(&devfreq->lock);
549                 if (!devfreq->stop_polling)
550                         queue_delayed_work(devfreq_wq, &devfreq->work,
551                                 msecs_to_jiffies(devfreq->profile->polling_ms));
552         }
553 out:
554         mutex_unlock(&devfreq->lock);
555 }
556 EXPORT_SYMBOL(devfreq_interval_update);
557
558 /**
559  * devfreq_notifier_call() - Notify that the device frequency requirements
560  *                           has been changed out of devfreq framework.
561  * @nb:         the notifier_block (supposed to be devfreq->nb)
562  * @type:       not used
563  * @devp:       not used
564  *
565  * Called by a notifier that uses devfreq->nb.
566  */
567 static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
568                                  void *devp)
569 {
570         struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
571         int ret;
572
573         mutex_lock(&devfreq->lock);
574
575         devfreq->scaling_min_freq = find_available_min_freq(devfreq);
576         if (!devfreq->scaling_min_freq) {
577                 mutex_unlock(&devfreq->lock);
578                 return -EINVAL;
579         }
580
581         devfreq->scaling_max_freq = find_available_max_freq(devfreq);
582         if (!devfreq->scaling_max_freq) {
583                 mutex_unlock(&devfreq->lock);
584                 return -EINVAL;
585         }
586
587         ret = update_devfreq(devfreq);
588         mutex_unlock(&devfreq->lock);
589
590         return ret;
591 }
592
593 /**
594  * devfreq_dev_release() - Callback for struct device to release the device.
595  * @dev:        the devfreq device
596  *
597  * Remove devfreq from the list and release its resources.
598  */
599 static void devfreq_dev_release(struct device *dev)
600 {
601         struct devfreq *devfreq = to_devfreq(dev);
602
603         mutex_lock(&devfreq_list_lock);
604         if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
605                 mutex_unlock(&devfreq_list_lock);
606                 dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
607                 return;
608         }
609         list_del(&devfreq->node);
610         mutex_unlock(&devfreq_list_lock);
611
612         if (devfreq->profile->exit)
613                 devfreq->profile->exit(devfreq->dev.parent);
614
615         mutex_destroy(&devfreq->lock);
616         kfree(devfreq);
617 }
618
619 /**
620  * devfreq_add_device() - Add devfreq feature to the device
621  * @dev:        the device to add devfreq feature.
622  * @profile:    device-specific profile to run devfreq.
623  * @governor_name:      name of the policy to choose frequency.
624  * @data:       private data for the governor. The devfreq framework does not
625  *              touch this value.
626  */
627 struct devfreq *devfreq_add_device(struct device *dev,
628                                    struct devfreq_dev_profile *profile,
629                                    const char *governor_name,
630                                    void *data)
631 {
632         struct devfreq *devfreq;
633         struct devfreq_governor *governor;
634         static atomic_t devfreq_no = ATOMIC_INIT(-1);
635         int err = 0;
636
637         if (!dev || !profile || !governor_name) {
638                 dev_err(dev, "%s: Invalid parameters.\n", __func__);
639                 return ERR_PTR(-EINVAL);
640         }
641
642         mutex_lock(&devfreq_list_lock);
643         devfreq = find_device_devfreq(dev);
644         mutex_unlock(&devfreq_list_lock);
645         if (!IS_ERR(devfreq)) {
646                 dev_err(dev, "%s: devfreq device already exists!\n",
647                         __func__);
648                 err = -EINVAL;
649                 goto err_out;
650         }
651
652         devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
653         if (!devfreq) {
654                 err = -ENOMEM;
655                 goto err_out;
656         }
657
658         mutex_init(&devfreq->lock);
659         mutex_lock(&devfreq->lock);
660         devfreq->dev.parent = dev;
661         devfreq->dev.class = devfreq_class;
662         devfreq->dev.release = devfreq_dev_release;
663         devfreq->profile = profile;
664         strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
665         devfreq->previous_freq = profile->initial_freq;
666         devfreq->last_status.current_frequency = profile->initial_freq;
667         devfreq->data = data;
668         devfreq->nb.notifier_call = devfreq_notifier_call;
669
670         if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
671                 mutex_unlock(&devfreq->lock);
672                 err = set_freq_table(devfreq);
673                 if (err < 0)
674                         goto err_dev;
675                 mutex_lock(&devfreq->lock);
676         }
677
678         devfreq->scaling_min_freq = find_available_min_freq(devfreq);
679         if (!devfreq->scaling_min_freq) {
680                 mutex_unlock(&devfreq->lock);
681                 err = -EINVAL;
682                 goto err_dev;
683         }
684         devfreq->min_freq = devfreq->scaling_min_freq;
685
686         devfreq->scaling_max_freq = find_available_max_freq(devfreq);
687         if (!devfreq->scaling_max_freq) {
688                 mutex_unlock(&devfreq->lock);
689                 err = -EINVAL;
690                 goto err_dev;
691         }
692         devfreq->max_freq = devfreq->scaling_max_freq;
693
694         devfreq->suspend_freq = dev_pm_opp_get_suspend_opp_freq(dev);
695         atomic_set(&devfreq->suspend_count, 0);
696
697         dev_set_name(&devfreq->dev, "devfreq%d",
698                                 atomic_inc_return(&devfreq_no));
699         err = device_register(&devfreq->dev);
700         if (err) {
701                 mutex_unlock(&devfreq->lock);
702                 put_device(&devfreq->dev);
703                 goto err_out;
704         }
705
706         devfreq->trans_table = devm_kzalloc(&devfreq->dev,
707                         array3_size(sizeof(unsigned int),
708                                     devfreq->profile->max_state,
709                                     devfreq->profile->max_state),
710                         GFP_KERNEL);
711         if (!devfreq->trans_table) {
712                 mutex_unlock(&devfreq->lock);
713                 err = -ENOMEM;
714                 goto err_devfreq;
715         }
716
717         devfreq->time_in_state = devm_kcalloc(&devfreq->dev,
718                         devfreq->profile->max_state,
719                         sizeof(unsigned long),
720                         GFP_KERNEL);
721         if (!devfreq->time_in_state) {
722                 mutex_unlock(&devfreq->lock);
723                 err = -ENOMEM;
724                 goto err_devfreq;
725         }
726
727         devfreq->last_stat_updated = jiffies;
728
729         srcu_init_notifier_head(&devfreq->transition_notifier_list);
730
731         mutex_unlock(&devfreq->lock);
732
733         mutex_lock(&devfreq_list_lock);
734
735         governor = try_then_request_governor(devfreq->governor_name);
736         if (IS_ERR(governor)) {
737                 dev_err(dev, "%s: Unable to find governor for the device\n",
738                         __func__);
739                 err = PTR_ERR(governor);
740                 goto err_init;
741         }
742
743         devfreq->governor = governor;
744         err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
745                                                 NULL);
746         if (err) {
747                 dev_err(dev, "%s: Unable to start governor for the device\n",
748                         __func__);
749                 goto err_init;
750         }
751
752         list_add(&devfreq->node, &devfreq_list);
753
754         mutex_unlock(&devfreq_list_lock);
755
756         return devfreq;
757
758 err_init:
759         mutex_unlock(&devfreq_list_lock);
760 err_devfreq:
761         devfreq_remove_device(devfreq);
762         devfreq = NULL;
763 err_dev:
764         kfree(devfreq);
765 err_out:
766         return ERR_PTR(err);
767 }
768 EXPORT_SYMBOL(devfreq_add_device);
769
770 /**
771  * devfreq_remove_device() - Remove devfreq feature from a device.
772  * @devfreq:    the devfreq instance to be removed
773  *
774  * The opposite of devfreq_add_device().
775  */
776 int devfreq_remove_device(struct devfreq *devfreq)
777 {
778         if (!devfreq)
779                 return -EINVAL;
780
781         if (devfreq->governor)
782                 devfreq->governor->event_handler(devfreq,
783                                                  DEVFREQ_GOV_STOP, NULL);
784         device_unregister(&devfreq->dev);
785
786         return 0;
787 }
788 EXPORT_SYMBOL(devfreq_remove_device);
789
790 static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
791 {
792         struct devfreq **r = res;
793
794         if (WARN_ON(!r || !*r))
795                 return 0;
796
797         return *r == data;
798 }
799
800 static void devm_devfreq_dev_release(struct device *dev, void *res)
801 {
802         devfreq_remove_device(*(struct devfreq **)res);
803 }
804
805 /**
806  * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
807  * @dev:        the device to add devfreq feature.
808  * @profile:    device-specific profile to run devfreq.
809  * @governor_name:      name of the policy to choose frequency.
810  * @data:       private data for the governor. The devfreq framework does not
811  *              touch this value.
812  *
813  * This function manages automatically the memory of devfreq device using device
814  * resource management and simplify the free operation for memory of devfreq
815  * device.
816  */
817 struct devfreq *devm_devfreq_add_device(struct device *dev,
818                                         struct devfreq_dev_profile *profile,
819                                         const char *governor_name,
820                                         void *data)
821 {
822         struct devfreq **ptr, *devfreq;
823
824         ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
825         if (!ptr)
826                 return ERR_PTR(-ENOMEM);
827
828         devfreq = devfreq_add_device(dev, profile, governor_name, data);
829         if (IS_ERR(devfreq)) {
830                 devres_free(ptr);
831                 return devfreq;
832         }
833
834         *ptr = devfreq;
835         devres_add(dev, ptr);
836
837         return devfreq;
838 }
839 EXPORT_SYMBOL(devm_devfreq_add_device);
840
841 #ifdef CONFIG_OF
842 /*
843  * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
844  * @dev - instance to the given device
845  * @index - index into list of devfreq
846  *
847  * return the instance of devfreq device
848  */
849 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
850 {
851         struct device_node *node;
852         struct devfreq *devfreq;
853
854         if (!dev)
855                 return ERR_PTR(-EINVAL);
856
857         if (!dev->of_node)
858                 return ERR_PTR(-EINVAL);
859
860         node = of_parse_phandle(dev->of_node, "devfreq", index);
861         if (!node)
862                 return ERR_PTR(-ENODEV);
863
864         mutex_lock(&devfreq_list_lock);
865         list_for_each_entry(devfreq, &devfreq_list, node) {
866                 if (devfreq->dev.parent
867                         && devfreq->dev.parent->of_node == node) {
868                         mutex_unlock(&devfreq_list_lock);
869                         of_node_put(node);
870                         return devfreq;
871                 }
872         }
873         mutex_unlock(&devfreq_list_lock);
874         of_node_put(node);
875
876         return ERR_PTR(-EPROBE_DEFER);
877 }
878 #else
879 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
880 {
881         return ERR_PTR(-ENODEV);
882 }
883 #endif /* CONFIG_OF */
884 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
885
886 /**
887  * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
888  * @dev:        the device from which to remove devfreq feature.
889  * @devfreq:    the devfreq instance to be removed
890  */
891 void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
892 {
893         WARN_ON(devres_release(dev, devm_devfreq_dev_release,
894                                devm_devfreq_dev_match, devfreq));
895 }
896 EXPORT_SYMBOL(devm_devfreq_remove_device);
897
898 /**
899  * devfreq_suspend_device() - Suspend devfreq of a device.
900  * @devfreq: the devfreq instance to be suspended
901  *
902  * This function is intended to be called by the pm callbacks
903  * (e.g., runtime_suspend, suspend) of the device driver that
904  * holds the devfreq.
905  */
906 int devfreq_suspend_device(struct devfreq *devfreq)
907 {
908         int ret;
909
910         if (!devfreq)
911                 return -EINVAL;
912
913         if (atomic_inc_return(&devfreq->suspend_count) > 1)
914                 return 0;
915
916         if (devfreq->governor) {
917                 ret = devfreq->governor->event_handler(devfreq,
918                                         DEVFREQ_GOV_SUSPEND, NULL);
919                 if (ret)
920                         return ret;
921         }
922
923         if (devfreq->suspend_freq) {
924                 ret = devfreq_set_target(devfreq, devfreq->suspend_freq, 0);
925                 if (ret)
926                         return ret;
927         }
928
929         return 0;
930 }
931 EXPORT_SYMBOL(devfreq_suspend_device);
932
933 /**
934  * devfreq_resume_device() - Resume devfreq of a device.
935  * @devfreq: the devfreq instance to be resumed
936  *
937  * This function is intended to be called by the pm callbacks
938  * (e.g., runtime_resume, resume) of the device driver that
939  * holds the devfreq.
940  */
941 int devfreq_resume_device(struct devfreq *devfreq)
942 {
943         int ret;
944
945         if (!devfreq)
946                 return -EINVAL;
947
948         if (atomic_dec_return(&devfreq->suspend_count) >= 1)
949                 return 0;
950
951         if (devfreq->resume_freq) {
952                 ret = devfreq_set_target(devfreq, devfreq->resume_freq, 0);
953                 if (ret)
954                         return ret;
955         }
956
957         if (devfreq->governor) {
958                 ret = devfreq->governor->event_handler(devfreq,
959                                         DEVFREQ_GOV_RESUME, NULL);
960                 if (ret)
961                         return ret;
962         }
963
964         return 0;
965 }
966 EXPORT_SYMBOL(devfreq_resume_device);
967
968 /**
969  * devfreq_suspend() - Suspend devfreq governors and devices
970  *
971  * Called during system wide Suspend/Hibernate cycles for suspending governors
972  * and devices preserving the state for resume. On some platforms the devfreq
973  * device must have precise state (frequency) after resume in order to provide
974  * fully operating setup.
975  */
976 void devfreq_suspend(void)
977 {
978         struct devfreq *devfreq;
979         int ret;
980
981         mutex_lock(&devfreq_list_lock);
982         list_for_each_entry(devfreq, &devfreq_list, node) {
983                 ret = devfreq_suspend_device(devfreq);
984                 if (ret)
985                         dev_err(&devfreq->dev,
986                                 "failed to suspend devfreq device\n");
987         }
988         mutex_unlock(&devfreq_list_lock);
989 }
990
991 /**
992  * devfreq_resume() - Resume devfreq governors and devices
993  *
994  * Called during system wide Suspend/Hibernate cycle for resuming governors and
995  * devices that are suspended with devfreq_suspend().
996  */
997 void devfreq_resume(void)
998 {
999         struct devfreq *devfreq;
1000         int ret;
1001
1002         mutex_lock(&devfreq_list_lock);
1003         list_for_each_entry(devfreq, &devfreq_list, node) {
1004                 ret = devfreq_resume_device(devfreq);
1005                 if (ret)
1006                         dev_warn(&devfreq->dev,
1007                                  "failed to resume devfreq device\n");
1008         }
1009         mutex_unlock(&devfreq_list_lock);
1010 }
1011
1012 /**
1013  * devfreq_add_governor() - Add devfreq governor
1014  * @governor:   the devfreq governor to be added
1015  */
1016 int devfreq_add_governor(struct devfreq_governor *governor)
1017 {
1018         struct devfreq_governor *g;
1019         struct devfreq *devfreq;
1020         int err = 0;
1021
1022         if (!governor) {
1023                 pr_err("%s: Invalid parameters.\n", __func__);
1024                 return -EINVAL;
1025         }
1026
1027         mutex_lock(&devfreq_list_lock);
1028         g = find_devfreq_governor(governor->name);
1029         if (!IS_ERR(g)) {
1030                 pr_err("%s: governor %s already registered\n", __func__,
1031                        g->name);
1032                 err = -EINVAL;
1033                 goto err_out;
1034         }
1035
1036         list_add(&governor->node, &devfreq_governor_list);
1037
1038         list_for_each_entry(devfreq, &devfreq_list, node) {
1039                 int ret = 0;
1040                 struct device *dev = devfreq->dev.parent;
1041
1042                 if (!strncmp(devfreq->governor_name, governor->name,
1043                              DEVFREQ_NAME_LEN)) {
1044                         /* The following should never occur */
1045                         if (devfreq->governor) {
1046                                 dev_warn(dev,
1047                                          "%s: Governor %s already present\n",
1048                                          __func__, devfreq->governor->name);
1049                                 ret = devfreq->governor->event_handler(devfreq,
1050                                                         DEVFREQ_GOV_STOP, NULL);
1051                                 if (ret) {
1052                                         dev_warn(dev,
1053                                                  "%s: Governor %s stop = %d\n",
1054                                                  __func__,
1055                                                  devfreq->governor->name, ret);
1056                                 }
1057                                 /* Fall through */
1058                         }
1059                         devfreq->governor = governor;
1060                         ret = devfreq->governor->event_handler(devfreq,
1061                                                 DEVFREQ_GOV_START, NULL);
1062                         if (ret) {
1063                                 dev_warn(dev, "%s: Governor %s start=%d\n",
1064                                          __func__, devfreq->governor->name,
1065                                          ret);
1066                         }
1067                 }
1068         }
1069
1070 err_out:
1071         mutex_unlock(&devfreq_list_lock);
1072
1073         return err;
1074 }
1075 EXPORT_SYMBOL(devfreq_add_governor);
1076
1077 /**
1078  * devfreq_remove_governor() - Remove devfreq feature from a device.
1079  * @governor:   the devfreq governor to be removed
1080  */
1081 int devfreq_remove_governor(struct devfreq_governor *governor)
1082 {
1083         struct devfreq_governor *g;
1084         struct devfreq *devfreq;
1085         int err = 0;
1086
1087         if (!governor) {
1088                 pr_err("%s: Invalid parameters.\n", __func__);
1089                 return -EINVAL;
1090         }
1091
1092         mutex_lock(&devfreq_list_lock);
1093         g = find_devfreq_governor(governor->name);
1094         if (IS_ERR(g)) {
1095                 pr_err("%s: governor %s not registered\n", __func__,
1096                        governor->name);
1097                 err = PTR_ERR(g);
1098                 goto err_out;
1099         }
1100         list_for_each_entry(devfreq, &devfreq_list, node) {
1101                 int ret;
1102                 struct device *dev = devfreq->dev.parent;
1103
1104                 if (!strncmp(devfreq->governor_name, governor->name,
1105                              DEVFREQ_NAME_LEN)) {
1106                         /* we should have a devfreq governor! */
1107                         if (!devfreq->governor) {
1108                                 dev_warn(dev, "%s: Governor %s NOT present\n",
1109                                          __func__, governor->name);
1110                                 continue;
1111                                 /* Fall through */
1112                         }
1113                         ret = devfreq->governor->event_handler(devfreq,
1114                                                 DEVFREQ_GOV_STOP, NULL);
1115                         if (ret) {
1116                                 dev_warn(dev, "%s: Governor %s stop=%d\n",
1117                                          __func__, devfreq->governor->name,
1118                                          ret);
1119                         }
1120                         devfreq->governor = NULL;
1121                 }
1122         }
1123
1124         list_del(&governor->node);
1125 err_out:
1126         mutex_unlock(&devfreq_list_lock);
1127
1128         return err;
1129 }
1130 EXPORT_SYMBOL(devfreq_remove_governor);
1131
1132 static ssize_t governor_show(struct device *dev,
1133                              struct device_attribute *attr, char *buf)
1134 {
1135         if (!to_devfreq(dev)->governor)
1136                 return -EINVAL;
1137
1138         return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
1139 }
1140
1141 static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
1142                               const char *buf, size_t count)
1143 {
1144         struct devfreq *df = to_devfreq(dev);
1145         int ret;
1146         char str_governor[DEVFREQ_NAME_LEN + 1];
1147         const struct devfreq_governor *governor, *prev_governor;
1148
1149         ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
1150         if (ret != 1)
1151                 return -EINVAL;
1152
1153         mutex_lock(&devfreq_list_lock);
1154         governor = try_then_request_governor(str_governor);
1155         if (IS_ERR(governor)) {
1156                 ret = PTR_ERR(governor);
1157                 goto out;
1158         }
1159         if (df->governor == governor) {
1160                 ret = 0;
1161                 goto out;
1162         } else if ((df->governor && df->governor->immutable) ||
1163                                         governor->immutable) {
1164                 ret = -EINVAL;
1165                 goto out;
1166         }
1167
1168         if (df->governor) {
1169                 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1170                 if (ret) {
1171                         dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1172                                  __func__, df->governor->name, ret);
1173                         goto out;
1174                 }
1175         }
1176         prev_governor = df->governor;
1177         df->governor = governor;
1178         strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
1179         ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1180         if (ret) {
1181                 dev_warn(dev, "%s: Governor %s not started(%d)\n",
1182                          __func__, df->governor->name, ret);
1183                 df->governor = prev_governor;
1184                 strncpy(df->governor_name, prev_governor->name,
1185                         DEVFREQ_NAME_LEN);
1186                 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1187                 if (ret) {
1188                         dev_err(dev,
1189                                 "%s: reverting to Governor %s failed (%d)\n",
1190                                 __func__, df->governor_name, ret);
1191                         df->governor = NULL;
1192                 }
1193         }
1194 out:
1195         mutex_unlock(&devfreq_list_lock);
1196
1197         if (!ret)
1198                 ret = count;
1199         return ret;
1200 }
1201 static DEVICE_ATTR_RW(governor);
1202
1203 static ssize_t available_governors_show(struct device *d,
1204                                         struct device_attribute *attr,
1205                                         char *buf)
1206 {
1207         struct devfreq *df = to_devfreq(d);
1208         ssize_t count = 0;
1209
1210         mutex_lock(&devfreq_list_lock);
1211
1212         /*
1213          * The devfreq with immutable governor (e.g., passive) shows
1214          * only own governor.
1215          */
1216         if (df->governor && df->governor->immutable) {
1217                 count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
1218                                   "%s ", df->governor_name);
1219         /*
1220          * The devfreq device shows the registered governor except for
1221          * immutable governors such as passive governor .
1222          */
1223         } else {
1224                 struct devfreq_governor *governor;
1225
1226                 list_for_each_entry(governor, &devfreq_governor_list, node) {
1227                         if (governor->immutable)
1228                                 continue;
1229                         count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1230                                            "%s ", governor->name);
1231                 }
1232         }
1233
1234         mutex_unlock(&devfreq_list_lock);
1235
1236         /* Truncate the trailing space */
1237         if (count)
1238                 count--;
1239
1240         count += sprintf(&buf[count], "\n");
1241
1242         return count;
1243 }
1244 static DEVICE_ATTR_RO(available_governors);
1245
1246 static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
1247                              char *buf)
1248 {
1249         unsigned long freq;
1250         struct devfreq *devfreq = to_devfreq(dev);
1251
1252         if (devfreq->profile->get_cur_freq &&
1253                 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
1254                 return sprintf(buf, "%lu\n", freq);
1255
1256         return sprintf(buf, "%lu\n", devfreq->previous_freq);
1257 }
1258 static DEVICE_ATTR_RO(cur_freq);
1259
1260 static ssize_t target_freq_show(struct device *dev,
1261                                 struct device_attribute *attr, char *buf)
1262 {
1263         return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
1264 }
1265 static DEVICE_ATTR_RO(target_freq);
1266
1267 static ssize_t polling_interval_show(struct device *dev,
1268                                      struct device_attribute *attr, char *buf)
1269 {
1270         return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
1271 }
1272
1273 static ssize_t polling_interval_store(struct device *dev,
1274                                       struct device_attribute *attr,
1275                                       const char *buf, size_t count)
1276 {
1277         struct devfreq *df = to_devfreq(dev);
1278         unsigned int value;
1279         int ret;
1280
1281         if (!df->governor)
1282                 return -EINVAL;
1283
1284         ret = sscanf(buf, "%u", &value);
1285         if (ret != 1)
1286                 return -EINVAL;
1287
1288         df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
1289         ret = count;
1290
1291         return ret;
1292 }
1293 static DEVICE_ATTR_RW(polling_interval);
1294
1295 static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
1296                               const char *buf, size_t count)
1297 {
1298         struct devfreq *df = to_devfreq(dev);
1299         unsigned long value;
1300         int ret;
1301
1302         ret = sscanf(buf, "%lu", &value);
1303         if (ret != 1)
1304                 return -EINVAL;
1305
1306         mutex_lock(&df->lock);
1307
1308         if (value) {
1309                 if (value > df->max_freq) {
1310                         ret = -EINVAL;
1311                         goto unlock;
1312                 }
1313         } else {
1314                 unsigned long *freq_table = df->profile->freq_table;
1315
1316                 /* Get minimum frequency according to sorting order */
1317                 if (freq_table[0] < freq_table[df->profile->max_state - 1])
1318                         value = freq_table[0];
1319                 else
1320                         value = freq_table[df->profile->max_state - 1];
1321         }
1322
1323         df->min_freq = value;
1324         update_devfreq(df);
1325         ret = count;
1326 unlock:
1327         mutex_unlock(&df->lock);
1328         return ret;
1329 }
1330
1331 static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
1332                              char *buf)
1333 {
1334         struct devfreq *df = to_devfreq(dev);
1335
1336         return sprintf(buf, "%lu\n", max(df->scaling_min_freq, df->min_freq));
1337 }
1338
1339 static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
1340                               const char *buf, size_t count)
1341 {
1342         struct devfreq *df = to_devfreq(dev);
1343         unsigned long value;
1344         int ret;
1345
1346         ret = sscanf(buf, "%lu", &value);
1347         if (ret != 1)
1348                 return -EINVAL;
1349
1350         mutex_lock(&df->lock);
1351
1352         if (value) {
1353                 if (value < df->min_freq) {
1354                         ret = -EINVAL;
1355                         goto unlock;
1356                 }
1357         } else {
1358                 unsigned long *freq_table = df->profile->freq_table;
1359
1360                 /* Get maximum frequency according to sorting order */
1361                 if (freq_table[0] < freq_table[df->profile->max_state - 1])
1362                         value = freq_table[df->profile->max_state - 1];
1363                 else
1364                         value = freq_table[0];
1365         }
1366
1367         df->max_freq = value;
1368         update_devfreq(df);
1369         ret = count;
1370 unlock:
1371         mutex_unlock(&df->lock);
1372         return ret;
1373 }
1374 static DEVICE_ATTR_RW(min_freq);
1375
1376 static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
1377                              char *buf)
1378 {
1379         struct devfreq *df = to_devfreq(dev);
1380
1381         return sprintf(buf, "%lu\n", min(df->scaling_max_freq, df->max_freq));
1382 }
1383 static DEVICE_ATTR_RW(max_freq);
1384
1385 static ssize_t available_frequencies_show(struct device *d,
1386                                           struct device_attribute *attr,
1387                                           char *buf)
1388 {
1389         struct devfreq *df = to_devfreq(d);
1390         ssize_t count = 0;
1391         int i;
1392
1393         mutex_lock(&df->lock);
1394
1395         for (i = 0; i < df->profile->max_state; i++)
1396                 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1397                                 "%lu ", df->profile->freq_table[i]);
1398
1399         mutex_unlock(&df->lock);
1400         /* Truncate the trailing space */
1401         if (count)
1402                 count--;
1403
1404         count += sprintf(&buf[count], "\n");
1405
1406         return count;
1407 }
1408 static DEVICE_ATTR_RO(available_frequencies);
1409
1410 static ssize_t trans_stat_show(struct device *dev,
1411                                struct device_attribute *attr, char *buf)
1412 {
1413         struct devfreq *devfreq = to_devfreq(dev);
1414         ssize_t len;
1415         int i, j;
1416         unsigned int max_state = devfreq->profile->max_state;
1417
1418         if (max_state == 0)
1419                 return sprintf(buf, "Not Supported.\n");
1420
1421         mutex_lock(&devfreq->lock);
1422         if (!devfreq->stop_polling &&
1423                         devfreq_update_status(devfreq, devfreq->previous_freq)) {
1424                 mutex_unlock(&devfreq->lock);
1425                 return 0;
1426         }
1427         mutex_unlock(&devfreq->lock);
1428
1429         len = sprintf(buf, "     From  :   To\n");
1430         len += sprintf(buf + len, "           :");
1431         for (i = 0; i < max_state; i++)
1432                 len += sprintf(buf + len, "%10lu",
1433                                 devfreq->profile->freq_table[i]);
1434
1435         len += sprintf(buf + len, "   time(ms)\n");
1436
1437         for (i = 0; i < max_state; i++) {
1438                 if (devfreq->profile->freq_table[i]
1439                                         == devfreq->previous_freq) {
1440                         len += sprintf(buf + len, "*");
1441                 } else {
1442                         len += sprintf(buf + len, " ");
1443                 }
1444                 len += sprintf(buf + len, "%10lu:",
1445                                 devfreq->profile->freq_table[i]);
1446                 for (j = 0; j < max_state; j++)
1447                         len += sprintf(buf + len, "%10u",
1448                                 devfreq->trans_table[(i * max_state) + j]);
1449                 len += sprintf(buf + len, "%10u\n",
1450                         jiffies_to_msecs(devfreq->time_in_state[i]));
1451         }
1452
1453         len += sprintf(buf + len, "Total transition : %u\n",
1454                                         devfreq->total_trans);
1455         return len;
1456 }
1457 static DEVICE_ATTR_RO(trans_stat);
1458
1459 static struct attribute *devfreq_attrs[] = {
1460         &dev_attr_governor.attr,
1461         &dev_attr_available_governors.attr,
1462         &dev_attr_cur_freq.attr,
1463         &dev_attr_available_frequencies.attr,
1464         &dev_attr_target_freq.attr,
1465         &dev_attr_polling_interval.attr,
1466         &dev_attr_min_freq.attr,
1467         &dev_attr_max_freq.attr,
1468         &dev_attr_trans_stat.attr,
1469         NULL,
1470 };
1471 ATTRIBUTE_GROUPS(devfreq);
1472
1473 static int __init devfreq_init(void)
1474 {
1475         devfreq_class = class_create(THIS_MODULE, "devfreq");
1476         if (IS_ERR(devfreq_class)) {
1477                 pr_err("%s: couldn't create class\n", __FILE__);
1478                 return PTR_ERR(devfreq_class);
1479         }
1480
1481         devfreq_wq = create_freezable_workqueue("devfreq_wq");
1482         if (!devfreq_wq) {
1483                 class_destroy(devfreq_class);
1484                 pr_err("%s: couldn't create workqueue\n", __FILE__);
1485                 return -ENOMEM;
1486         }
1487         devfreq_class->dev_groups = devfreq_groups;
1488
1489         return 0;
1490 }
1491 subsys_initcall(devfreq_init);
1492
1493 /*
1494  * The following are helper functions for devfreq user device drivers with
1495  * OPP framework.
1496  */
1497
1498 /**
1499  * devfreq_recommended_opp() - Helper function to get proper OPP for the
1500  *                           freq value given to target callback.
1501  * @dev:        The devfreq user device. (parent of devfreq)
1502  * @freq:       The frequency given to target function
1503  * @flags:      Flags handed from devfreq framework.
1504  *
1505  * The callers are required to call dev_pm_opp_put() for the returned OPP after
1506  * use.
1507  */
1508 struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1509                                            unsigned long *freq,
1510                                            u32 flags)
1511 {
1512         struct dev_pm_opp *opp;
1513
1514         if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1515                 /* The freq is an upper bound. opp should be lower */
1516                 opp = dev_pm_opp_find_freq_floor(dev, freq);
1517
1518                 /* If not available, use the closest opp */
1519                 if (opp == ERR_PTR(-ERANGE))
1520                         opp = dev_pm_opp_find_freq_ceil(dev, freq);
1521         } else {
1522                 /* The freq is an lower bound. opp should be higher */
1523                 opp = dev_pm_opp_find_freq_ceil(dev, freq);
1524
1525                 /* If not available, use the closest opp */
1526                 if (opp == ERR_PTR(-ERANGE))
1527                         opp = dev_pm_opp_find_freq_floor(dev, freq);
1528         }
1529
1530         return opp;
1531 }
1532 EXPORT_SYMBOL(devfreq_recommended_opp);
1533
1534 /**
1535  * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1536  *                                   for any changes in the OPP availability
1537  *                                   changes
1538  * @dev:        The devfreq user device. (parent of devfreq)
1539  * @devfreq:    The devfreq object.
1540  */
1541 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1542 {
1543         return dev_pm_opp_register_notifier(dev, &devfreq->nb);
1544 }
1545 EXPORT_SYMBOL(devfreq_register_opp_notifier);
1546
1547 /**
1548  * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1549  *                                     notified for any changes in the OPP
1550  *                                     availability changes anymore.
1551  * @dev:        The devfreq user device. (parent of devfreq)
1552  * @devfreq:    The devfreq object.
1553  *
1554  * At exit() callback of devfreq_dev_profile, this must be included if
1555  * devfreq_recommended_opp is used.
1556  */
1557 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1558 {
1559         return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
1560 }
1561 EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
1562
1563 static void devm_devfreq_opp_release(struct device *dev, void *res)
1564 {
1565         devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
1566 }
1567
1568 /**
1569  * devm_devfreq_register_opp_notifier() - Resource-managed
1570  *                                        devfreq_register_opp_notifier()
1571  * @dev:        The devfreq user device. (parent of devfreq)
1572  * @devfreq:    The devfreq object.
1573  */
1574 int devm_devfreq_register_opp_notifier(struct device *dev,
1575                                        struct devfreq *devfreq)
1576 {
1577         struct devfreq **ptr;
1578         int ret;
1579
1580         ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
1581         if (!ptr)
1582                 return -ENOMEM;
1583
1584         ret = devfreq_register_opp_notifier(dev, devfreq);
1585         if (ret) {
1586                 devres_free(ptr);
1587                 return ret;
1588         }
1589
1590         *ptr = devfreq;
1591         devres_add(dev, ptr);
1592
1593         return 0;
1594 }
1595 EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
1596
1597 /**
1598  * devm_devfreq_unregister_opp_notifier() - Resource-managed
1599  *                                          devfreq_unregister_opp_notifier()
1600  * @dev:        The devfreq user device. (parent of devfreq)
1601  * @devfreq:    The devfreq object.
1602  */
1603 void devm_devfreq_unregister_opp_notifier(struct device *dev,
1604                                          struct devfreq *devfreq)
1605 {
1606         WARN_ON(devres_release(dev, devm_devfreq_opp_release,
1607                                devm_devfreq_dev_match, devfreq));
1608 }
1609 EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
1610
1611 /**
1612  * devfreq_register_notifier() - Register a driver with devfreq
1613  * @devfreq:    The devfreq object.
1614  * @nb:         The notifier block to register.
1615  * @list:       DEVFREQ_TRANSITION_NOTIFIER.
1616  */
1617 int devfreq_register_notifier(struct devfreq *devfreq,
1618                               struct notifier_block *nb,
1619                               unsigned int list)
1620 {
1621         int ret = 0;
1622
1623         if (!devfreq)
1624                 return -EINVAL;
1625
1626         switch (list) {
1627         case DEVFREQ_TRANSITION_NOTIFIER:
1628                 ret = srcu_notifier_chain_register(
1629                                 &devfreq->transition_notifier_list, nb);
1630                 break;
1631         default:
1632                 ret = -EINVAL;
1633         }
1634
1635         return ret;
1636 }
1637 EXPORT_SYMBOL(devfreq_register_notifier);
1638
1639 /*
1640  * devfreq_unregister_notifier() - Unregister a driver with devfreq
1641  * @devfreq:    The devfreq object.
1642  * @nb:         The notifier block to be unregistered.
1643  * @list:       DEVFREQ_TRANSITION_NOTIFIER.
1644  */
1645 int devfreq_unregister_notifier(struct devfreq *devfreq,
1646                                 struct notifier_block *nb,
1647                                 unsigned int list)
1648 {
1649         int ret = 0;
1650
1651         if (!devfreq)
1652                 return -EINVAL;
1653
1654         switch (list) {
1655         case DEVFREQ_TRANSITION_NOTIFIER:
1656                 ret = srcu_notifier_chain_unregister(
1657                                 &devfreq->transition_notifier_list, nb);
1658                 break;
1659         default:
1660                 ret = -EINVAL;
1661         }
1662
1663         return ret;
1664 }
1665 EXPORT_SYMBOL(devfreq_unregister_notifier);
1666
1667 struct devfreq_notifier_devres {
1668         struct devfreq *devfreq;
1669         struct notifier_block *nb;
1670         unsigned int list;
1671 };
1672
1673 static void devm_devfreq_notifier_release(struct device *dev, void *res)
1674 {
1675         struct devfreq_notifier_devres *this = res;
1676
1677         devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
1678 }
1679
1680 /**
1681  * devm_devfreq_register_notifier()
1682         - Resource-managed devfreq_register_notifier()
1683  * @dev:        The devfreq user device. (parent of devfreq)
1684  * @devfreq:    The devfreq object.
1685  * @nb:         The notifier block to be unregistered.
1686  * @list:       DEVFREQ_TRANSITION_NOTIFIER.
1687  */
1688 int devm_devfreq_register_notifier(struct device *dev,
1689                                 struct devfreq *devfreq,
1690                                 struct notifier_block *nb,
1691                                 unsigned int list)
1692 {
1693         struct devfreq_notifier_devres *ptr;
1694         int ret;
1695
1696         ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
1697                                 GFP_KERNEL);
1698         if (!ptr)
1699                 return -ENOMEM;
1700
1701         ret = devfreq_register_notifier(devfreq, nb, list);
1702         if (ret) {
1703                 devres_free(ptr);
1704                 return ret;
1705         }
1706
1707         ptr->devfreq = devfreq;
1708         ptr->nb = nb;
1709         ptr->list = list;
1710         devres_add(dev, ptr);
1711
1712         return 0;
1713 }
1714 EXPORT_SYMBOL(devm_devfreq_register_notifier);
1715
1716 /**
1717  * devm_devfreq_unregister_notifier()
1718         - Resource-managed devfreq_unregister_notifier()
1719  * @dev:        The devfreq user device. (parent of devfreq)
1720  * @devfreq:    The devfreq object.
1721  * @nb:         The notifier block to be unregistered.
1722  * @list:       DEVFREQ_TRANSITION_NOTIFIER.
1723  */
1724 void devm_devfreq_unregister_notifier(struct device *dev,
1725                                       struct devfreq *devfreq,
1726                                       struct notifier_block *nb,
1727                                       unsigned int list)
1728 {
1729         WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
1730                                devm_devfreq_dev_match, devfreq));
1731 }
1732 EXPORT_SYMBOL(devm_devfreq_unregister_notifier);