1 // SPDX-License-Identifier: GPL-2.0-only
3 * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
6 * Copyright (C) 2011 Samsung Electronics
7 * MyungJoo Ham <myungjoo.ham@samsung.com>
10 #include <linux/kernel.h>
11 #include <linux/kmod.h>
12 #include <linux/sched.h>
13 #include <linux/debugfs.h>
14 #include <linux/errno.h>
15 #include <linux/err.h>
16 #include <linux/init.h>
17 #include <linux/export.h>
18 #include <linux/slab.h>
19 #include <linux/stat.h>
20 #include <linux/pm_opp.h>
21 #include <linux/devfreq.h>
22 #include <linux/workqueue.h>
23 #include <linux/platform_device.h>
24 #include <linux/list.h>
25 #include <linux/printk.h>
26 #include <linux/hrtimer.h>
28 #include <linux/pm_qos.h>
31 #define CREATE_TRACE_POINTS
32 #include <trace/events/devfreq.h>
34 #define HZ_PER_KHZ 1000
36 static struct class *devfreq_class;
37 static struct dentry *devfreq_debugfs;
40 * devfreq core provides delayed work based load monitoring helper
41 * functions. Governors can use these or can implement their own
42 * monitoring mechanism.
44 static struct workqueue_struct *devfreq_wq;
46 /* The list of all device-devfreq governors */
47 static LIST_HEAD(devfreq_governor_list);
48 /* The list of all device-devfreq */
49 static LIST_HEAD(devfreq_list);
50 static DEFINE_MUTEX(devfreq_list_lock);
52 static const char timer_name[][DEVFREQ_NAME_LEN] = {
53 [DEVFREQ_TIMER_DEFERRABLE] = { "deferrable" },
54 [DEVFREQ_TIMER_DELAYED] = { "delayed" },
58 * find_device_devfreq() - find devfreq struct using device pointer
59 * @dev: device pointer used to lookup device devfreq.
61 * Search the list of device devfreqs and return the matched device's
62 * devfreq info. devfreq_list_lock should be held by the caller.
64 static struct devfreq *find_device_devfreq(struct device *dev)
66 struct devfreq *tmp_devfreq;
68 lockdep_assert_held(&devfreq_list_lock);
70 if (IS_ERR_OR_NULL(dev)) {
71 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
72 return ERR_PTR(-EINVAL);
75 list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
76 if (tmp_devfreq->dev.parent == dev)
80 return ERR_PTR(-ENODEV);
83 static unsigned long find_available_min_freq(struct devfreq *devfreq)
85 struct dev_pm_opp *opp;
86 unsigned long min_freq = 0;
88 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &min_freq);
97 static unsigned long find_available_max_freq(struct devfreq *devfreq)
99 struct dev_pm_opp *opp;
100 unsigned long max_freq = ULONG_MAX;
102 opp = dev_pm_opp_find_freq_floor(devfreq->dev.parent, &max_freq);
112 * get_freq_range() - Get the current freq range
113 * @devfreq: the devfreq instance
114 * @min_freq: the min frequency
115 * @max_freq: the max frequency
117 * This takes into consideration all constraints.
119 static void get_freq_range(struct devfreq *devfreq,
120 unsigned long *min_freq,
121 unsigned long *max_freq)
123 unsigned long *freq_table = devfreq->profile->freq_table;
124 s32 qos_min_freq, qos_max_freq;
126 lockdep_assert_held(&devfreq->lock);
129 * Initialize minimum/maximum frequency from freq table.
130 * The devfreq drivers can initialize this in either ascending or
131 * descending order and devfreq core supports both.
133 if (freq_table[0] < freq_table[devfreq->profile->max_state - 1]) {
134 *min_freq = freq_table[0];
135 *max_freq = freq_table[devfreq->profile->max_state - 1];
137 *min_freq = freq_table[devfreq->profile->max_state - 1];
138 *max_freq = freq_table[0];
141 /* Apply constraints from PM QoS */
142 qos_min_freq = dev_pm_qos_read_value(devfreq->dev.parent,
143 DEV_PM_QOS_MIN_FREQUENCY);
144 qos_max_freq = dev_pm_qos_read_value(devfreq->dev.parent,
145 DEV_PM_QOS_MAX_FREQUENCY);
146 *min_freq = max(*min_freq, (unsigned long)HZ_PER_KHZ * qos_min_freq);
147 if (qos_max_freq != PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE)
148 *max_freq = min(*max_freq,
149 (unsigned long)HZ_PER_KHZ * qos_max_freq);
151 /* Apply constraints from OPP interface */
152 *min_freq = max(*min_freq, devfreq->scaling_min_freq);
153 *max_freq = min(*max_freq, devfreq->scaling_max_freq);
155 if (*min_freq > *max_freq)
156 *min_freq = *max_freq;
160 * devfreq_get_freq_level() - Lookup freq_table for the frequency
161 * @devfreq: the devfreq instance
162 * @freq: the target frequency
164 static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq)
168 for (lev = 0; lev < devfreq->profile->max_state; lev++)
169 if (freq == devfreq->profile->freq_table[lev])
175 static int set_freq_table(struct devfreq *devfreq)
177 struct devfreq_dev_profile *profile = devfreq->profile;
178 struct dev_pm_opp *opp;
182 /* Initialize the freq_table from OPP table */
183 count = dev_pm_opp_get_opp_count(devfreq->dev.parent);
187 profile->max_state = count;
188 profile->freq_table = devm_kcalloc(devfreq->dev.parent,
190 sizeof(*profile->freq_table),
192 if (!profile->freq_table) {
193 profile->max_state = 0;
197 for (i = 0, freq = 0; i < profile->max_state; i++, freq++) {
198 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq);
200 devm_kfree(devfreq->dev.parent, profile->freq_table);
201 profile->max_state = 0;
205 profile->freq_table[i] = freq;
212 * devfreq_update_status() - Update statistics of devfreq behavior
213 * @devfreq: the devfreq instance
214 * @freq: the update target frequency
216 int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
218 int lev, prev_lev, ret = 0;
221 lockdep_assert_held(&devfreq->lock);
222 cur_time = get_jiffies_64();
224 /* Immediately exit if previous_freq is not initialized yet. */
225 if (!devfreq->previous_freq)
228 prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
234 devfreq->stats.time_in_state[prev_lev] +=
235 cur_time - devfreq->stats.last_update;
237 lev = devfreq_get_freq_level(devfreq, freq);
243 if (lev != prev_lev) {
244 devfreq->stats.trans_table[
245 (prev_lev * devfreq->profile->max_state) + lev]++;
246 devfreq->stats.total_trans++;
250 devfreq->stats.last_update = cur_time;
253 EXPORT_SYMBOL(devfreq_update_status);
256 * find_devfreq_governor() - find devfreq governor from name
257 * @name: name of the governor
259 * Search the list of devfreq governors and return the matched
260 * governor's pointer. devfreq_list_lock should be held by the caller.
262 static struct devfreq_governor *find_devfreq_governor(const char *name)
264 struct devfreq_governor *tmp_governor;
266 lockdep_assert_held(&devfreq_list_lock);
268 if (IS_ERR_OR_NULL(name)) {
269 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
270 return ERR_PTR(-EINVAL);
273 list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
274 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
278 return ERR_PTR(-ENODEV);
282 * try_then_request_governor() - Try to find the governor and request the
283 * module if is not found.
284 * @name: name of the governor
286 * Search the list of devfreq governors and request the module and try again
287 * if is not found. This can happen when both drivers (the governor driver
288 * and the driver that call devfreq_add_device) are built as modules.
289 * devfreq_list_lock should be held by the caller. Returns the matched
290 * governor's pointer or an error pointer.
292 static struct devfreq_governor *try_then_request_governor(const char *name)
294 struct devfreq_governor *governor;
297 lockdep_assert_held(&devfreq_list_lock);
299 if (IS_ERR_OR_NULL(name)) {
300 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
301 return ERR_PTR(-EINVAL);
304 governor = find_devfreq_governor(name);
305 if (IS_ERR(governor)) {
306 mutex_unlock(&devfreq_list_lock);
308 if (!strncmp(name, DEVFREQ_GOV_SIMPLE_ONDEMAND,
310 err = request_module("governor_%s", "simpleondemand");
312 err = request_module("governor_%s", name);
313 /* Restore previous state before return */
314 mutex_lock(&devfreq_list_lock);
316 return (err < 0) ? ERR_PTR(err) : ERR_PTR(-EINVAL);
318 governor = find_devfreq_governor(name);
324 static int devfreq_notify_transition(struct devfreq *devfreq,
325 struct devfreq_freqs *freqs, unsigned int state)
331 case DEVFREQ_PRECHANGE:
332 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
333 DEVFREQ_PRECHANGE, freqs);
336 case DEVFREQ_POSTCHANGE:
337 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
338 DEVFREQ_POSTCHANGE, freqs);
347 static int devfreq_set_target(struct devfreq *devfreq, unsigned long new_freq,
350 struct devfreq_freqs freqs;
351 unsigned long cur_freq;
354 if (devfreq->profile->get_cur_freq)
355 devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
357 cur_freq = devfreq->previous_freq;
359 freqs.old = cur_freq;
360 freqs.new = new_freq;
361 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
363 err = devfreq->profile->target(devfreq->dev.parent, &new_freq, flags);
365 freqs.new = cur_freq;
366 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
370 freqs.new = new_freq;
371 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
373 if (devfreq_update_status(devfreq, new_freq))
374 dev_err(&devfreq->dev,
375 "Couldn't update frequency transition information.\n");
377 devfreq->previous_freq = new_freq;
379 if (devfreq->suspend_freq)
380 devfreq->resume_freq = new_freq;
385 /* Load monitoring helper functions for governors use */
388 * update_devfreq() - Reevaluate the device and configure frequency.
389 * @devfreq: the devfreq instance.
391 * Note: Lock devfreq->lock before calling update_devfreq
392 * This function is exported for governors.
394 int update_devfreq(struct devfreq *devfreq)
396 unsigned long freq, min_freq, max_freq;
400 lockdep_assert_held(&devfreq->lock);
402 if (!devfreq->governor)
405 /* Reevaluate the proper frequency */
406 err = devfreq->governor->get_target_freq(devfreq, &freq);
409 get_freq_range(devfreq, &min_freq, &max_freq);
411 if (freq < min_freq) {
413 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
415 if (freq > max_freq) {
417 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
420 return devfreq_set_target(devfreq, freq, flags);
423 EXPORT_SYMBOL(update_devfreq);
426 * devfreq_monitor() - Periodically poll devfreq objects.
427 * @work: the work struct used to run devfreq_monitor periodically.
430 static void devfreq_monitor(struct work_struct *work)
433 struct devfreq *devfreq = container_of(work,
434 struct devfreq, work.work);
436 mutex_lock(&devfreq->lock);
437 err = update_devfreq(devfreq);
439 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
441 if (devfreq->stop_polling)
444 queue_delayed_work(devfreq_wq, &devfreq->work,
445 msecs_to_jiffies(devfreq->profile->polling_ms));
448 mutex_unlock(&devfreq->lock);
449 trace_devfreq_monitor(devfreq);
453 * devfreq_monitor_start() - Start load monitoring of devfreq instance
454 * @devfreq: the devfreq instance.
456 * Helper function for starting devfreq device load monitoring. By
457 * default delayed work based monitoring is supported. Function
458 * to be called from governor in response to DEVFREQ_GOV_START
459 * event when device is added to devfreq framework.
461 void devfreq_monitor_start(struct devfreq *devfreq)
463 if (devfreq->governor->interrupt_driven)
466 mutex_lock(&devfreq->lock);
467 if (delayed_work_pending(&devfreq->work))
470 switch (devfreq->profile->timer) {
471 case DEVFREQ_TIMER_DEFERRABLE:
472 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
474 case DEVFREQ_TIMER_DELAYED:
475 INIT_DELAYED_WORK(&devfreq->work, devfreq_monitor);
481 if (devfreq->profile->polling_ms)
482 queue_delayed_work(devfreq_wq, &devfreq->work,
483 msecs_to_jiffies(devfreq->profile->polling_ms));
486 devfreq->stop_polling = false;
487 mutex_unlock(&devfreq->lock);
489 EXPORT_SYMBOL(devfreq_monitor_start);
492 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
493 * @devfreq: the devfreq instance.
495 * Helper function to stop devfreq device load monitoring. Function
496 * to be called from governor in response to DEVFREQ_GOV_STOP
497 * event when device is removed from devfreq framework.
499 void devfreq_monitor_stop(struct devfreq *devfreq)
501 if (devfreq->governor->interrupt_driven)
504 mutex_lock(&devfreq->lock);
505 if (devfreq->stop_polling) {
506 mutex_unlock(&devfreq->lock);
510 devfreq->stop_polling = true;
511 mutex_unlock(&devfreq->lock);
512 cancel_delayed_work_sync(&devfreq->work);
514 EXPORT_SYMBOL(devfreq_monitor_stop);
517 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
518 * @devfreq: the devfreq instance.
520 * Helper function to suspend devfreq device load monitoring. Function
521 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
522 * event or when polling interval is set to zero.
524 * Note: Though this function is same as devfreq_monitor_stop(),
525 * intentionally kept separate to provide hooks for collecting
526 * transition statistics.
528 void devfreq_monitor_suspend(struct devfreq *devfreq)
530 mutex_lock(&devfreq->lock);
531 if (devfreq->stop_polling) {
532 mutex_unlock(&devfreq->lock);
536 devfreq_update_status(devfreq, devfreq->previous_freq);
537 devfreq->stop_polling = true;
538 mutex_unlock(&devfreq->lock);
540 if (devfreq->governor->interrupt_driven)
543 cancel_delayed_work_sync(&devfreq->work);
545 EXPORT_SYMBOL(devfreq_monitor_suspend);
548 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
549 * @devfreq: the devfreq instance.
551 * Helper function to resume devfreq device load monitoring. Function
552 * to be called from governor in response to DEVFREQ_GOV_RESUME
553 * event or when polling interval is set to non-zero.
555 void devfreq_monitor_resume(struct devfreq *devfreq)
559 mutex_lock(&devfreq->lock);
560 if (!devfreq->stop_polling)
563 if (devfreq->governor->interrupt_driven)
566 if (!delayed_work_pending(&devfreq->work) &&
567 devfreq->profile->polling_ms)
568 queue_delayed_work(devfreq_wq, &devfreq->work,
569 msecs_to_jiffies(devfreq->profile->polling_ms));
572 devfreq->stats.last_update = get_jiffies_64();
573 devfreq->stop_polling = false;
575 if (devfreq->profile->get_cur_freq &&
576 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
577 devfreq->previous_freq = freq;
580 mutex_unlock(&devfreq->lock);
582 EXPORT_SYMBOL(devfreq_monitor_resume);
585 * devfreq_update_interval() - Update device devfreq monitoring interval
586 * @devfreq: the devfreq instance.
587 * @delay: new polling interval to be set.
589 * Helper function to set new load monitoring polling interval. Function
590 * to be called from governor in response to DEVFREQ_GOV_UPDATE_INTERVAL event.
592 void devfreq_update_interval(struct devfreq *devfreq, unsigned int *delay)
594 unsigned int cur_delay = devfreq->profile->polling_ms;
595 unsigned int new_delay = *delay;
597 mutex_lock(&devfreq->lock);
598 devfreq->profile->polling_ms = new_delay;
600 if (devfreq->stop_polling)
603 if (devfreq->governor->interrupt_driven)
606 /* if new delay is zero, stop polling */
608 mutex_unlock(&devfreq->lock);
609 cancel_delayed_work_sync(&devfreq->work);
613 /* if current delay is zero, start polling with new delay */
615 queue_delayed_work(devfreq_wq, &devfreq->work,
616 msecs_to_jiffies(devfreq->profile->polling_ms));
620 /* if current delay is greater than new delay, restart polling */
621 if (cur_delay > new_delay) {
622 mutex_unlock(&devfreq->lock);
623 cancel_delayed_work_sync(&devfreq->work);
624 mutex_lock(&devfreq->lock);
625 if (!devfreq->stop_polling)
626 queue_delayed_work(devfreq_wq, &devfreq->work,
627 msecs_to_jiffies(devfreq->profile->polling_ms));
630 mutex_unlock(&devfreq->lock);
632 EXPORT_SYMBOL(devfreq_update_interval);
635 * devfreq_notifier_call() - Notify that the device frequency requirements
636 * has been changed out of devfreq framework.
637 * @nb: the notifier_block (supposed to be devfreq->nb)
641 * Called by a notifier that uses devfreq->nb.
643 static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
646 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
649 mutex_lock(&devfreq->lock);
651 devfreq->scaling_min_freq = find_available_min_freq(devfreq);
652 if (!devfreq->scaling_min_freq)
655 devfreq->scaling_max_freq = find_available_max_freq(devfreq);
656 if (!devfreq->scaling_max_freq) {
657 devfreq->scaling_max_freq = ULONG_MAX;
661 err = update_devfreq(devfreq);
664 mutex_unlock(&devfreq->lock);
666 dev_err(devfreq->dev.parent,
667 "failed to update frequency from OPP notifier (%d)\n",
674 * qos_notifier_call() - Common handler for QoS constraints.
675 * @devfreq: the devfreq instance.
677 static int qos_notifier_call(struct devfreq *devfreq)
681 mutex_lock(&devfreq->lock);
682 err = update_devfreq(devfreq);
683 mutex_unlock(&devfreq->lock);
685 dev_err(devfreq->dev.parent,
686 "failed to update frequency from PM QoS (%d)\n",
693 * qos_min_notifier_call() - Callback for QoS min_freq changes.
694 * @nb: Should be devfreq->nb_min
696 static int qos_min_notifier_call(struct notifier_block *nb,
697 unsigned long val, void *ptr)
699 return qos_notifier_call(container_of(nb, struct devfreq, nb_min));
703 * qos_max_notifier_call() - Callback for QoS max_freq changes.
704 * @nb: Should be devfreq->nb_max
706 static int qos_max_notifier_call(struct notifier_block *nb,
707 unsigned long val, void *ptr)
709 return qos_notifier_call(container_of(nb, struct devfreq, nb_max));
713 * devfreq_dev_release() - Callback for struct device to release the device.
714 * @dev: the devfreq device
716 * Remove devfreq from the list and release its resources.
718 static void devfreq_dev_release(struct device *dev)
720 struct devfreq *devfreq = to_devfreq(dev);
723 mutex_lock(&devfreq_list_lock);
724 list_del(&devfreq->node);
725 mutex_unlock(&devfreq_list_lock);
727 err = dev_pm_qos_remove_notifier(devfreq->dev.parent, &devfreq->nb_max,
728 DEV_PM_QOS_MAX_FREQUENCY);
729 if (err && err != -ENOENT)
730 dev_warn(dev->parent,
731 "Failed to remove max_freq notifier: %d\n", err);
732 err = dev_pm_qos_remove_notifier(devfreq->dev.parent, &devfreq->nb_min,
733 DEV_PM_QOS_MIN_FREQUENCY);
734 if (err && err != -ENOENT)
735 dev_warn(dev->parent,
736 "Failed to remove min_freq notifier: %d\n", err);
738 if (dev_pm_qos_request_active(&devfreq->user_max_freq_req)) {
739 err = dev_pm_qos_remove_request(&devfreq->user_max_freq_req);
741 dev_warn(dev->parent,
742 "Failed to remove max_freq request: %d\n", err);
744 if (dev_pm_qos_request_active(&devfreq->user_min_freq_req)) {
745 err = dev_pm_qos_remove_request(&devfreq->user_min_freq_req);
747 dev_warn(dev->parent,
748 "Failed to remove min_freq request: %d\n", err);
751 if (devfreq->profile->exit)
752 devfreq->profile->exit(devfreq->dev.parent);
754 mutex_destroy(&devfreq->lock);
755 srcu_cleanup_notifier_head(&devfreq->transition_notifier_list);
760 * devfreq_add_device() - Add devfreq feature to the device
761 * @dev: the device to add devfreq feature.
762 * @profile: device-specific profile to run devfreq.
763 * @governor_name: name of the policy to choose frequency.
764 * @data: devfreq driver pass to governors, governor should not change it.
766 struct devfreq *devfreq_add_device(struct device *dev,
767 struct devfreq_dev_profile *profile,
768 const char *governor_name,
771 struct devfreq *devfreq;
772 struct devfreq_governor *governor;
775 if (!dev || !profile || !governor_name) {
776 dev_err(dev, "%s: Invalid parameters.\n", __func__);
777 return ERR_PTR(-EINVAL);
780 mutex_lock(&devfreq_list_lock);
781 devfreq = find_device_devfreq(dev);
782 mutex_unlock(&devfreq_list_lock);
783 if (!IS_ERR(devfreq)) {
784 dev_err(dev, "%s: devfreq device already exists!\n",
790 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
796 mutex_init(&devfreq->lock);
797 mutex_lock(&devfreq->lock);
798 devfreq->dev.parent = dev;
799 devfreq->dev.class = devfreq_class;
800 devfreq->dev.release = devfreq_dev_release;
801 INIT_LIST_HEAD(&devfreq->node);
802 devfreq->profile = profile;
803 strscpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
804 devfreq->previous_freq = profile->initial_freq;
805 devfreq->last_status.current_frequency = profile->initial_freq;
806 devfreq->data = data;
807 devfreq->nb.notifier_call = devfreq_notifier_call;
809 if (devfreq->profile->timer < 0
810 || devfreq->profile->timer >= DEVFREQ_TIMER_NUM) {
811 mutex_unlock(&devfreq->lock);
816 if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
817 mutex_unlock(&devfreq->lock);
818 err = set_freq_table(devfreq);
821 mutex_lock(&devfreq->lock);
824 devfreq->scaling_min_freq = find_available_min_freq(devfreq);
825 if (!devfreq->scaling_min_freq) {
826 mutex_unlock(&devfreq->lock);
831 devfreq->scaling_max_freq = find_available_max_freq(devfreq);
832 if (!devfreq->scaling_max_freq) {
833 mutex_unlock(&devfreq->lock);
838 devfreq->suspend_freq = dev_pm_opp_get_suspend_opp_freq(dev);
839 atomic_set(&devfreq->suspend_count, 0);
841 dev_set_name(&devfreq->dev, "%s", dev_name(dev));
842 err = device_register(&devfreq->dev);
844 mutex_unlock(&devfreq->lock);
845 put_device(&devfreq->dev);
849 devfreq->stats.trans_table = devm_kzalloc(&devfreq->dev,
850 array3_size(sizeof(unsigned int),
851 devfreq->profile->max_state,
852 devfreq->profile->max_state),
854 if (!devfreq->stats.trans_table) {
855 mutex_unlock(&devfreq->lock);
860 devfreq->stats.time_in_state = devm_kcalloc(&devfreq->dev,
861 devfreq->profile->max_state,
862 sizeof(*devfreq->stats.time_in_state),
864 if (!devfreq->stats.time_in_state) {
865 mutex_unlock(&devfreq->lock);
870 devfreq->stats.total_trans = 0;
871 devfreq->stats.last_update = get_jiffies_64();
873 srcu_init_notifier_head(&devfreq->transition_notifier_list);
875 mutex_unlock(&devfreq->lock);
877 err = dev_pm_qos_add_request(dev, &devfreq->user_min_freq_req,
878 DEV_PM_QOS_MIN_FREQUENCY, 0);
881 err = dev_pm_qos_add_request(dev, &devfreq->user_max_freq_req,
882 DEV_PM_QOS_MAX_FREQUENCY,
883 PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE);
887 devfreq->nb_min.notifier_call = qos_min_notifier_call;
888 err = dev_pm_qos_add_notifier(devfreq->dev.parent, &devfreq->nb_min,
889 DEV_PM_QOS_MIN_FREQUENCY);
893 devfreq->nb_max.notifier_call = qos_max_notifier_call;
894 err = dev_pm_qos_add_notifier(devfreq->dev.parent, &devfreq->nb_max,
895 DEV_PM_QOS_MAX_FREQUENCY);
899 mutex_lock(&devfreq_list_lock);
901 governor = try_then_request_governor(devfreq->governor_name);
902 if (IS_ERR(governor)) {
903 dev_err(dev, "%s: Unable to find governor for the device\n",
905 err = PTR_ERR(governor);
909 devfreq->governor = governor;
910 err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
913 dev_err(dev, "%s: Unable to start governor for the device\n",
918 list_add(&devfreq->node, &devfreq_list);
920 mutex_unlock(&devfreq_list_lock);
925 mutex_unlock(&devfreq_list_lock);
927 devfreq_remove_device(devfreq);
934 EXPORT_SYMBOL(devfreq_add_device);
937 * devfreq_remove_device() - Remove devfreq feature from a device.
938 * @devfreq: the devfreq instance to be removed
940 * The opposite of devfreq_add_device().
942 int devfreq_remove_device(struct devfreq *devfreq)
947 if (devfreq->governor)
948 devfreq->governor->event_handler(devfreq,
949 DEVFREQ_GOV_STOP, NULL);
950 device_unregister(&devfreq->dev);
954 EXPORT_SYMBOL(devfreq_remove_device);
956 static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
958 struct devfreq **r = res;
960 if (WARN_ON(!r || !*r))
966 static void devm_devfreq_dev_release(struct device *dev, void *res)
968 devfreq_remove_device(*(struct devfreq **)res);
972 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
973 * @dev: the device to add devfreq feature.
974 * @profile: device-specific profile to run devfreq.
975 * @governor_name: name of the policy to choose frequency.
976 * @data: devfreq driver pass to governors, governor should not change it.
978 * This function manages automatically the memory of devfreq device using device
979 * resource management and simplify the free operation for memory of devfreq
982 struct devfreq *devm_devfreq_add_device(struct device *dev,
983 struct devfreq_dev_profile *profile,
984 const char *governor_name,
987 struct devfreq **ptr, *devfreq;
989 ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
991 return ERR_PTR(-ENOMEM);
993 devfreq = devfreq_add_device(dev, profile, governor_name, data);
994 if (IS_ERR(devfreq)) {
1000 devres_add(dev, ptr);
1004 EXPORT_SYMBOL(devm_devfreq_add_device);
1008 * devfreq_get_devfreq_by_node - Get the devfreq device from devicetree
1009 * @node - pointer to device_node
1011 * return the instance of devfreq device
1013 struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node)
1015 struct devfreq *devfreq;
1018 return ERR_PTR(-EINVAL);
1020 mutex_lock(&devfreq_list_lock);
1021 list_for_each_entry(devfreq, &devfreq_list, node) {
1022 if (devfreq->dev.parent
1023 && devfreq->dev.parent->of_node == node) {
1024 mutex_unlock(&devfreq_list_lock);
1028 mutex_unlock(&devfreq_list_lock);
1030 return ERR_PTR(-ENODEV);
1034 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
1035 * @dev - instance to the given device
1036 * @phandle_name - name of property holding a phandle value
1037 * @index - index into list of devfreq
1039 * return the instance of devfreq device
1041 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
1042 const char *phandle_name, int index)
1044 struct device_node *node;
1045 struct devfreq *devfreq;
1047 if (!dev || !phandle_name)
1048 return ERR_PTR(-EINVAL);
1051 return ERR_PTR(-EINVAL);
1053 node = of_parse_phandle(dev->of_node, phandle_name, index);
1055 return ERR_PTR(-ENODEV);
1057 devfreq = devfreq_get_devfreq_by_node(node);
1064 struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node)
1066 return ERR_PTR(-ENODEV);
1069 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
1070 const char *phandle_name, int index)
1072 return ERR_PTR(-ENODEV);
1074 #endif /* CONFIG_OF */
1075 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_node);
1076 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
1079 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
1080 * @dev: the device from which to remove devfreq feature.
1081 * @devfreq: the devfreq instance to be removed
1083 void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
1085 WARN_ON(devres_release(dev, devm_devfreq_dev_release,
1086 devm_devfreq_dev_match, devfreq));
1088 EXPORT_SYMBOL(devm_devfreq_remove_device);
1091 * devfreq_suspend_device() - Suspend devfreq of a device.
1092 * @devfreq: the devfreq instance to be suspended
1094 * This function is intended to be called by the pm callbacks
1095 * (e.g., runtime_suspend, suspend) of the device driver that
1096 * holds the devfreq.
1098 int devfreq_suspend_device(struct devfreq *devfreq)
1105 if (atomic_inc_return(&devfreq->suspend_count) > 1)
1108 if (devfreq->governor) {
1109 ret = devfreq->governor->event_handler(devfreq,
1110 DEVFREQ_GOV_SUSPEND, NULL);
1115 if (devfreq->suspend_freq) {
1116 mutex_lock(&devfreq->lock);
1117 ret = devfreq_set_target(devfreq, devfreq->suspend_freq, 0);
1118 mutex_unlock(&devfreq->lock);
1125 EXPORT_SYMBOL(devfreq_suspend_device);
1128 * devfreq_resume_device() - Resume devfreq of a device.
1129 * @devfreq: the devfreq instance to be resumed
1131 * This function is intended to be called by the pm callbacks
1132 * (e.g., runtime_resume, resume) of the device driver that
1133 * holds the devfreq.
1135 int devfreq_resume_device(struct devfreq *devfreq)
1142 if (atomic_dec_return(&devfreq->suspend_count) >= 1)
1145 if (devfreq->resume_freq) {
1146 mutex_lock(&devfreq->lock);
1147 ret = devfreq_set_target(devfreq, devfreq->resume_freq, 0);
1148 mutex_unlock(&devfreq->lock);
1153 if (devfreq->governor) {
1154 ret = devfreq->governor->event_handler(devfreq,
1155 DEVFREQ_GOV_RESUME, NULL);
1162 EXPORT_SYMBOL(devfreq_resume_device);
1165 * devfreq_suspend() - Suspend devfreq governors and devices
1167 * Called during system wide Suspend/Hibernate cycles for suspending governors
1168 * and devices preserving the state for resume. On some platforms the devfreq
1169 * device must have precise state (frequency) after resume in order to provide
1170 * fully operating setup.
1172 void devfreq_suspend(void)
1174 struct devfreq *devfreq;
1177 mutex_lock(&devfreq_list_lock);
1178 list_for_each_entry(devfreq, &devfreq_list, node) {
1179 ret = devfreq_suspend_device(devfreq);
1181 dev_err(&devfreq->dev,
1182 "failed to suspend devfreq device\n");
1184 mutex_unlock(&devfreq_list_lock);
1188 * devfreq_resume() - Resume devfreq governors and devices
1190 * Called during system wide Suspend/Hibernate cycle for resuming governors and
1191 * devices that are suspended with devfreq_suspend().
1193 void devfreq_resume(void)
1195 struct devfreq *devfreq;
1198 mutex_lock(&devfreq_list_lock);
1199 list_for_each_entry(devfreq, &devfreq_list, node) {
1200 ret = devfreq_resume_device(devfreq);
1202 dev_warn(&devfreq->dev,
1203 "failed to resume devfreq device\n");
1205 mutex_unlock(&devfreq_list_lock);
1209 * devfreq_add_governor() - Add devfreq governor
1210 * @governor: the devfreq governor to be added
1212 int devfreq_add_governor(struct devfreq_governor *governor)
1214 struct devfreq_governor *g;
1215 struct devfreq *devfreq;
1219 pr_err("%s: Invalid parameters.\n", __func__);
1223 mutex_lock(&devfreq_list_lock);
1224 g = find_devfreq_governor(governor->name);
1226 pr_err("%s: governor %s already registered\n", __func__,
1232 list_add(&governor->node, &devfreq_governor_list);
1234 list_for_each_entry(devfreq, &devfreq_list, node) {
1236 struct device *dev = devfreq->dev.parent;
1238 if (!strncmp(devfreq->governor_name, governor->name,
1239 DEVFREQ_NAME_LEN)) {
1240 /* The following should never occur */
1241 if (devfreq->governor) {
1243 "%s: Governor %s already present\n",
1244 __func__, devfreq->governor->name);
1245 ret = devfreq->governor->event_handler(devfreq,
1246 DEVFREQ_GOV_STOP, NULL);
1249 "%s: Governor %s stop = %d\n",
1251 devfreq->governor->name, ret);
1255 devfreq->governor = governor;
1256 ret = devfreq->governor->event_handler(devfreq,
1257 DEVFREQ_GOV_START, NULL);
1259 dev_warn(dev, "%s: Governor %s start=%d\n",
1260 __func__, devfreq->governor->name,
1267 mutex_unlock(&devfreq_list_lock);
1271 EXPORT_SYMBOL(devfreq_add_governor);
1274 * devfreq_remove_governor() - Remove devfreq feature from a device.
1275 * @governor: the devfreq governor to be removed
1277 int devfreq_remove_governor(struct devfreq_governor *governor)
1279 struct devfreq_governor *g;
1280 struct devfreq *devfreq;
1284 pr_err("%s: Invalid parameters.\n", __func__);
1288 mutex_lock(&devfreq_list_lock);
1289 g = find_devfreq_governor(governor->name);
1291 pr_err("%s: governor %s not registered\n", __func__,
1296 list_for_each_entry(devfreq, &devfreq_list, node) {
1298 struct device *dev = devfreq->dev.parent;
1300 if (!strncmp(devfreq->governor_name, governor->name,
1301 DEVFREQ_NAME_LEN)) {
1302 /* we should have a devfreq governor! */
1303 if (!devfreq->governor) {
1304 dev_warn(dev, "%s: Governor %s NOT present\n",
1305 __func__, governor->name);
1309 ret = devfreq->governor->event_handler(devfreq,
1310 DEVFREQ_GOV_STOP, NULL);
1312 dev_warn(dev, "%s: Governor %s stop=%d\n",
1313 __func__, devfreq->governor->name,
1316 devfreq->governor = NULL;
1320 list_del(&governor->node);
1322 mutex_unlock(&devfreq_list_lock);
1326 EXPORT_SYMBOL(devfreq_remove_governor);
1328 static ssize_t name_show(struct device *dev,
1329 struct device_attribute *attr, char *buf)
1331 struct devfreq *df = to_devfreq(dev);
1332 return sprintf(buf, "%s\n", dev_name(df->dev.parent));
1334 static DEVICE_ATTR_RO(name);
1336 static ssize_t governor_show(struct device *dev,
1337 struct device_attribute *attr, char *buf)
1339 struct devfreq *df = to_devfreq(dev);
1344 return sprintf(buf, "%s\n", df->governor->name);
1347 static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
1348 const char *buf, size_t count)
1350 struct devfreq *df = to_devfreq(dev);
1352 char str_governor[DEVFREQ_NAME_LEN + 1];
1353 const struct devfreq_governor *governor, *prev_governor;
1358 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
1362 mutex_lock(&devfreq_list_lock);
1363 governor = try_then_request_governor(str_governor);
1364 if (IS_ERR(governor)) {
1365 ret = PTR_ERR(governor);
1368 if (df->governor == governor) {
1371 } else if (df->governor->immutable || governor->immutable) {
1376 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1378 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1379 __func__, df->governor->name, ret);
1383 prev_governor = df->governor;
1384 df->governor = governor;
1385 strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
1386 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1388 dev_warn(dev, "%s: Governor %s not started(%d)\n",
1389 __func__, df->governor->name, ret);
1390 df->governor = prev_governor;
1391 strncpy(df->governor_name, prev_governor->name,
1393 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1396 "%s: reverting to Governor %s failed (%d)\n",
1397 __func__, df->governor_name, ret);
1398 df->governor = NULL;
1402 mutex_unlock(&devfreq_list_lock);
1408 static DEVICE_ATTR_RW(governor);
1410 static ssize_t available_governors_show(struct device *d,
1411 struct device_attribute *attr,
1414 struct devfreq *df = to_devfreq(d);
1420 mutex_lock(&devfreq_list_lock);
1423 * The devfreq with immutable governor (e.g., passive) shows
1424 * only own governor.
1426 if (df->governor->immutable) {
1427 count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
1428 "%s ", df->governor_name);
1430 * The devfreq device shows the registered governor except for
1431 * immutable governors such as passive governor .
1434 struct devfreq_governor *governor;
1436 list_for_each_entry(governor, &devfreq_governor_list, node) {
1437 if (governor->immutable)
1439 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1440 "%s ", governor->name);
1444 mutex_unlock(&devfreq_list_lock);
1446 /* Truncate the trailing space */
1450 count += sprintf(&buf[count], "\n");
1454 static DEVICE_ATTR_RO(available_governors);
1456 static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
1460 struct devfreq *df = to_devfreq(dev);
1465 if (df->profile->get_cur_freq &&
1466 !df->profile->get_cur_freq(df->dev.parent, &freq))
1467 return sprintf(buf, "%lu\n", freq);
1469 return sprintf(buf, "%lu\n", df->previous_freq);
1471 static DEVICE_ATTR_RO(cur_freq);
1473 static ssize_t target_freq_show(struct device *dev,
1474 struct device_attribute *attr, char *buf)
1476 struct devfreq *df = to_devfreq(dev);
1478 return sprintf(buf, "%lu\n", df->previous_freq);
1480 static DEVICE_ATTR_RO(target_freq);
1482 static ssize_t polling_interval_show(struct device *dev,
1483 struct device_attribute *attr, char *buf)
1485 struct devfreq *df = to_devfreq(dev);
1490 return sprintf(buf, "%d\n", df->profile->polling_ms);
1493 static ssize_t polling_interval_store(struct device *dev,
1494 struct device_attribute *attr,
1495 const char *buf, size_t count)
1497 struct devfreq *df = to_devfreq(dev);
1504 ret = sscanf(buf, "%u", &value);
1508 df->governor->event_handler(df, DEVFREQ_GOV_UPDATE_INTERVAL, &value);
1513 static DEVICE_ATTR_RW(polling_interval);
1515 static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
1516 const char *buf, size_t count)
1518 struct devfreq *df = to_devfreq(dev);
1519 unsigned long value;
1523 * Protect against theoretical sysfs writes between
1524 * device_add and dev_pm_qos_add_request
1526 if (!dev_pm_qos_request_active(&df->user_min_freq_req))
1529 ret = sscanf(buf, "%lu", &value);
1533 /* Round down to kHz for PM QoS */
1534 ret = dev_pm_qos_update_request(&df->user_min_freq_req,
1535 value / HZ_PER_KHZ);
1542 static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
1545 struct devfreq *df = to_devfreq(dev);
1546 unsigned long min_freq, max_freq;
1548 mutex_lock(&df->lock);
1549 get_freq_range(df, &min_freq, &max_freq);
1550 mutex_unlock(&df->lock);
1552 return sprintf(buf, "%lu\n", min_freq);
1554 static DEVICE_ATTR_RW(min_freq);
1556 static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
1557 const char *buf, size_t count)
1559 struct devfreq *df = to_devfreq(dev);
1560 unsigned long value;
1564 * Protect against theoretical sysfs writes between
1565 * device_add and dev_pm_qos_add_request
1567 if (!dev_pm_qos_request_active(&df->user_max_freq_req))
1570 ret = sscanf(buf, "%lu", &value);
1575 * PM QoS frequencies are in kHz so we need to convert. Convert by
1576 * rounding upwards so that the acceptable interval never shrinks.
1578 * For example if the user writes "666666666" to sysfs this value will
1579 * be converted to 666667 kHz and back to 666667000 Hz before an OPP
1580 * lookup, this ensures that an OPP of 666666666Hz is still accepted.
1582 * A value of zero means "no limit".
1585 value = DIV_ROUND_UP(value, HZ_PER_KHZ);
1587 value = PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE;
1589 ret = dev_pm_qos_update_request(&df->user_max_freq_req, value);
1596 static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
1599 struct devfreq *df = to_devfreq(dev);
1600 unsigned long min_freq, max_freq;
1602 mutex_lock(&df->lock);
1603 get_freq_range(df, &min_freq, &max_freq);
1604 mutex_unlock(&df->lock);
1606 return sprintf(buf, "%lu\n", max_freq);
1608 static DEVICE_ATTR_RW(max_freq);
1610 static ssize_t available_frequencies_show(struct device *d,
1611 struct device_attribute *attr,
1614 struct devfreq *df = to_devfreq(d);
1621 mutex_lock(&df->lock);
1623 for (i = 0; i < df->profile->max_state; i++)
1624 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1625 "%lu ", df->profile->freq_table[i]);
1627 mutex_unlock(&df->lock);
1628 /* Truncate the trailing space */
1632 count += sprintf(&buf[count], "\n");
1636 static DEVICE_ATTR_RO(available_frequencies);
1638 static ssize_t trans_stat_show(struct device *dev,
1639 struct device_attribute *attr, char *buf)
1641 struct devfreq *df = to_devfreq(dev);
1644 unsigned int max_state;
1648 max_state = df->profile->max_state;
1651 return scnprintf(buf, PAGE_SIZE, "Not Supported.\n");
1653 mutex_lock(&df->lock);
1654 if (!df->stop_polling &&
1655 devfreq_update_status(df, df->previous_freq)) {
1656 mutex_unlock(&df->lock);
1659 mutex_unlock(&df->lock);
1661 len += scnprintf(buf + len, PAGE_SIZE - len, " From : To\n");
1662 len += scnprintf(buf + len, PAGE_SIZE - len, " :");
1663 for (i = 0; i < max_state; i++) {
1664 if (len >= PAGE_SIZE - 1)
1666 len += scnprintf(buf + len, PAGE_SIZE - len, "%10lu",
1667 df->profile->freq_table[i]);
1669 if (len >= PAGE_SIZE - 1)
1670 return PAGE_SIZE - 1;
1672 len += scnprintf(buf + len, PAGE_SIZE - len, " time(ms)\n");
1674 for (i = 0; i < max_state; i++) {
1675 if (len >= PAGE_SIZE - 1)
1677 if (df->profile->freq_table[i]
1678 == df->previous_freq) {
1679 len += scnprintf(buf + len, PAGE_SIZE - len, "*");
1681 len += scnprintf(buf + len, PAGE_SIZE - len, " ");
1683 if (len >= PAGE_SIZE - 1)
1686 len += scnprintf(buf + len, PAGE_SIZE - len, "%10lu:",
1687 df->profile->freq_table[i]);
1688 for (j = 0; j < max_state; j++) {
1689 if (len >= PAGE_SIZE - 1)
1691 len += scnprintf(buf + len, PAGE_SIZE - len, "%10u",
1692 df->stats.trans_table[(i * max_state) + j]);
1694 if (len >= PAGE_SIZE - 1)
1696 len += scnprintf(buf + len, PAGE_SIZE - len, "%10llu\n", (u64)
1697 jiffies64_to_msecs(df->stats.time_in_state[i]));
1700 if (len < PAGE_SIZE - 1)
1701 len += scnprintf(buf + len, PAGE_SIZE - len, "Total transition : %u\n",
1702 df->stats.total_trans);
1704 if (len >= PAGE_SIZE - 1) {
1705 pr_warn_once("devfreq transition table exceeds PAGE_SIZE. Disabling\n");
1712 static ssize_t trans_stat_store(struct device *dev,
1713 struct device_attribute *attr,
1714 const char *buf, size_t count)
1716 struct devfreq *df = to_devfreq(dev);
1722 if (df->profile->max_state == 0)
1725 err = kstrtoint(buf, 10, &value);
1726 if (err || value != 0)
1729 mutex_lock(&df->lock);
1730 memset(df->stats.time_in_state, 0, (df->profile->max_state *
1731 sizeof(*df->stats.time_in_state)));
1732 memset(df->stats.trans_table, 0, array3_size(sizeof(unsigned int),
1733 df->profile->max_state,
1734 df->profile->max_state));
1735 df->stats.total_trans = 0;
1736 df->stats.last_update = get_jiffies_64();
1737 mutex_unlock(&df->lock);
1741 static DEVICE_ATTR_RW(trans_stat);
1743 static ssize_t timer_show(struct device *dev,
1744 struct device_attribute *attr, char *buf)
1746 struct devfreq *df = to_devfreq(dev);
1751 return sprintf(buf, "%s\n", timer_name[df->profile->timer]);
1754 static ssize_t timer_store(struct device *dev, struct device_attribute *attr,
1755 const char *buf, size_t count)
1757 struct devfreq *df = to_devfreq(dev);
1758 char str_timer[DEVFREQ_NAME_LEN + 1];
1762 if (!df->governor || !df->profile)
1765 ret = sscanf(buf, "%16s", str_timer);
1769 for (i = 0; i < DEVFREQ_TIMER_NUM; i++) {
1770 if (!strncmp(timer_name[i], str_timer, DEVFREQ_NAME_LEN)) {
1781 if (df->profile->timer == timer) {
1786 mutex_lock(&df->lock);
1787 df->profile->timer = timer;
1788 mutex_unlock(&df->lock);
1790 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1792 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1793 __func__, df->governor->name, ret);
1797 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1799 dev_warn(dev, "%s: Governor %s not started(%d)\n",
1800 __func__, df->governor->name, ret);
1802 return ret ? ret : count;
1804 static DEVICE_ATTR_RW(timer);
1806 static struct attribute *devfreq_attrs[] = {
1807 &dev_attr_name.attr,
1808 &dev_attr_governor.attr,
1809 &dev_attr_available_governors.attr,
1810 &dev_attr_cur_freq.attr,
1811 &dev_attr_available_frequencies.attr,
1812 &dev_attr_target_freq.attr,
1813 &dev_attr_polling_interval.attr,
1814 &dev_attr_min_freq.attr,
1815 &dev_attr_max_freq.attr,
1816 &dev_attr_trans_stat.attr,
1817 &dev_attr_timer.attr,
1820 ATTRIBUTE_GROUPS(devfreq);
1823 * devfreq_summary_show() - Show the summary of the devfreq devices
1824 * @s: seq_file instance to show the summary of devfreq devices
1827 * Show the summary of the devfreq devices via 'devfreq_summary' debugfs file.
1828 * It helps that user can know the detailed information of the devfreq devices.
1830 * Return 0 always because it shows the information without any data change.
1832 static int devfreq_summary_show(struct seq_file *s, void *data)
1834 struct devfreq *devfreq;
1835 struct devfreq *p_devfreq = NULL;
1836 unsigned long cur_freq, min_freq, max_freq;
1837 unsigned int polling_ms;
1840 seq_printf(s, "%-30s %-30s %-15s %-10s %10s %12s %12s %12s\n",
1849 seq_printf(s, "%30s %30s %15s %10s %10s %12s %12s %12s\n",
1850 "------------------------------",
1851 "------------------------------",
1859 mutex_lock(&devfreq_list_lock);
1861 list_for_each_entry_reverse(devfreq, &devfreq_list, node) {
1862 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE)
1863 if (!strncmp(devfreq->governor_name, DEVFREQ_GOV_PASSIVE,
1864 DEVFREQ_NAME_LEN)) {
1865 struct devfreq_passive_data *data = devfreq->data;
1868 p_devfreq = data->parent;
1874 mutex_lock(&devfreq->lock);
1875 cur_freq = devfreq->previous_freq;
1876 get_freq_range(devfreq, &min_freq, &max_freq);
1877 polling_ms = devfreq->profile->polling_ms;
1878 timer = devfreq->profile->timer;
1879 mutex_unlock(&devfreq->lock);
1882 "%-30s %-30s %-15s %-10s %10d %12ld %12ld %12ld\n",
1883 dev_name(&devfreq->dev),
1884 p_devfreq ? dev_name(&p_devfreq->dev) : "null",
1885 devfreq->governor_name,
1886 polling_ms ? timer_name[timer] : "null",
1893 mutex_unlock(&devfreq_list_lock);
1897 DEFINE_SHOW_ATTRIBUTE(devfreq_summary);
1899 static int __init devfreq_init(void)
1901 devfreq_class = class_create(THIS_MODULE, "devfreq");
1902 if (IS_ERR(devfreq_class)) {
1903 pr_err("%s: couldn't create class\n", __FILE__);
1904 return PTR_ERR(devfreq_class);
1907 devfreq_wq = create_freezable_workqueue("devfreq_wq");
1909 class_destroy(devfreq_class);
1910 pr_err("%s: couldn't create workqueue\n", __FILE__);
1913 devfreq_class->dev_groups = devfreq_groups;
1915 devfreq_debugfs = debugfs_create_dir("devfreq", NULL);
1916 debugfs_create_file("devfreq_summary", 0444,
1917 devfreq_debugfs, NULL,
1918 &devfreq_summary_fops);
1922 subsys_initcall(devfreq_init);
1925 * The following are helper functions for devfreq user device drivers with
1930 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1931 * freq value given to target callback.
1932 * @dev: The devfreq user device. (parent of devfreq)
1933 * @freq: The frequency given to target function
1934 * @flags: Flags handed from devfreq framework.
1936 * The callers are required to call dev_pm_opp_put() for the returned OPP after
1939 struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1940 unsigned long *freq,
1943 struct dev_pm_opp *opp;
1945 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1946 /* The freq is an upper bound. opp should be lower */
1947 opp = dev_pm_opp_find_freq_floor(dev, freq);
1949 /* If not available, use the closest opp */
1950 if (opp == ERR_PTR(-ERANGE))
1951 opp = dev_pm_opp_find_freq_ceil(dev, freq);
1953 /* The freq is an lower bound. opp should be higher */
1954 opp = dev_pm_opp_find_freq_ceil(dev, freq);
1956 /* If not available, use the closest opp */
1957 if (opp == ERR_PTR(-ERANGE))
1958 opp = dev_pm_opp_find_freq_floor(dev, freq);
1963 EXPORT_SYMBOL(devfreq_recommended_opp);
1966 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1967 * for any changes in the OPP availability
1969 * @dev: The devfreq user device. (parent of devfreq)
1970 * @devfreq: The devfreq object.
1972 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1974 return dev_pm_opp_register_notifier(dev, &devfreq->nb);
1976 EXPORT_SYMBOL(devfreq_register_opp_notifier);
1979 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1980 * notified for any changes in the OPP
1981 * availability changes anymore.
1982 * @dev: The devfreq user device. (parent of devfreq)
1983 * @devfreq: The devfreq object.
1985 * At exit() callback of devfreq_dev_profile, this must be included if
1986 * devfreq_recommended_opp is used.
1988 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1990 return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
1992 EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
1994 static void devm_devfreq_opp_release(struct device *dev, void *res)
1996 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
2000 * devm_devfreq_register_opp_notifier() - Resource-managed
2001 * devfreq_register_opp_notifier()
2002 * @dev: The devfreq user device. (parent of devfreq)
2003 * @devfreq: The devfreq object.
2005 int devm_devfreq_register_opp_notifier(struct device *dev,
2006 struct devfreq *devfreq)
2008 struct devfreq **ptr;
2011 ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
2015 ret = devfreq_register_opp_notifier(dev, devfreq);
2022 devres_add(dev, ptr);
2026 EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
2029 * devm_devfreq_unregister_opp_notifier() - Resource-managed
2030 * devfreq_unregister_opp_notifier()
2031 * @dev: The devfreq user device. (parent of devfreq)
2032 * @devfreq: The devfreq object.
2034 void devm_devfreq_unregister_opp_notifier(struct device *dev,
2035 struct devfreq *devfreq)
2037 WARN_ON(devres_release(dev, devm_devfreq_opp_release,
2038 devm_devfreq_dev_match, devfreq));
2040 EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
2043 * devfreq_register_notifier() - Register a driver with devfreq
2044 * @devfreq: The devfreq object.
2045 * @nb: The notifier block to register.
2046 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2048 int devfreq_register_notifier(struct devfreq *devfreq,
2049 struct notifier_block *nb,
2058 case DEVFREQ_TRANSITION_NOTIFIER:
2059 ret = srcu_notifier_chain_register(
2060 &devfreq->transition_notifier_list, nb);
2068 EXPORT_SYMBOL(devfreq_register_notifier);
2071 * devfreq_unregister_notifier() - Unregister a driver with devfreq
2072 * @devfreq: The devfreq object.
2073 * @nb: The notifier block to be unregistered.
2074 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2076 int devfreq_unregister_notifier(struct devfreq *devfreq,
2077 struct notifier_block *nb,
2086 case DEVFREQ_TRANSITION_NOTIFIER:
2087 ret = srcu_notifier_chain_unregister(
2088 &devfreq->transition_notifier_list, nb);
2096 EXPORT_SYMBOL(devfreq_unregister_notifier);
2098 struct devfreq_notifier_devres {
2099 struct devfreq *devfreq;
2100 struct notifier_block *nb;
2104 static void devm_devfreq_notifier_release(struct device *dev, void *res)
2106 struct devfreq_notifier_devres *this = res;
2108 devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
2112 * devm_devfreq_register_notifier()
2113 * - Resource-managed devfreq_register_notifier()
2114 * @dev: The devfreq user device. (parent of devfreq)
2115 * @devfreq: The devfreq object.
2116 * @nb: The notifier block to be unregistered.
2117 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2119 int devm_devfreq_register_notifier(struct device *dev,
2120 struct devfreq *devfreq,
2121 struct notifier_block *nb,
2124 struct devfreq_notifier_devres *ptr;
2127 ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
2132 ret = devfreq_register_notifier(devfreq, nb, list);
2138 ptr->devfreq = devfreq;
2141 devres_add(dev, ptr);
2145 EXPORT_SYMBOL(devm_devfreq_register_notifier);
2148 * devm_devfreq_unregister_notifier()
2149 * - Resource-managed devfreq_unregister_notifier()
2150 * @dev: The devfreq user device. (parent of devfreq)
2151 * @devfreq: The devfreq object.
2152 * @nb: The notifier block to be unregistered.
2153 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2155 void devm_devfreq_unregister_notifier(struct device *dev,
2156 struct devfreq *devfreq,
2157 struct notifier_block *nb,
2160 WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
2161 devm_devfreq_dev_match, devfreq));
2163 EXPORT_SYMBOL(devm_devfreq_unregister_notifier);