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/devfreq_cooling.h>
15 #include <linux/errno.h>
16 #include <linux/err.h>
17 #include <linux/init.h>
18 #include <linux/export.h>
19 #include <linux/slab.h>
20 #include <linux/stat.h>
21 #include <linux/pm_opp.h>
22 #include <linux/devfreq.h>
23 #include <linux/workqueue.h>
24 #include <linux/platform_device.h>
25 #include <linux/list.h>
26 #include <linux/printk.h>
27 #include <linux/hrtimer.h>
29 #include <linux/pm_qos.h>
30 #include <linux/units.h>
33 #define CREATE_TRACE_POINTS
34 #include <trace/events/devfreq.h>
36 #define IS_SUPPORTED_FLAG(f, name) ((f & DEVFREQ_GOV_FLAG_##name) ? true : false)
37 #define IS_SUPPORTED_ATTR(f, name) ((f & DEVFREQ_GOV_ATTR_##name) ? true : false)
39 static struct class *devfreq_class;
40 static struct dentry *devfreq_debugfs;
43 * devfreq core provides delayed work based load monitoring helper
44 * functions. Governors can use these or can implement their own
45 * monitoring mechanism.
47 static struct workqueue_struct *devfreq_wq;
49 /* The list of all device-devfreq governors */
50 static LIST_HEAD(devfreq_governor_list);
51 /* The list of all device-devfreq */
52 static LIST_HEAD(devfreq_list);
53 static DEFINE_MUTEX(devfreq_list_lock);
55 static const char timer_name[][DEVFREQ_NAME_LEN] = {
56 [DEVFREQ_TIMER_DEFERRABLE] = { "deferrable" },
57 [DEVFREQ_TIMER_DELAYED] = { "delayed" },
61 * find_device_devfreq() - find devfreq struct using device pointer
62 * @dev: device pointer used to lookup device devfreq.
64 * Search the list of device devfreqs and return the matched device's
65 * devfreq info. devfreq_list_lock should be held by the caller.
67 static struct devfreq *find_device_devfreq(struct device *dev)
69 struct devfreq *tmp_devfreq;
71 lockdep_assert_held(&devfreq_list_lock);
73 if (IS_ERR_OR_NULL(dev)) {
74 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
75 return ERR_PTR(-EINVAL);
78 list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
79 if (tmp_devfreq->dev.parent == dev)
83 return ERR_PTR(-ENODEV);
86 static unsigned long find_available_min_freq(struct devfreq *devfreq)
88 struct dev_pm_opp *opp;
89 unsigned long min_freq = 0;
91 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &min_freq);
100 static unsigned long find_available_max_freq(struct devfreq *devfreq)
102 struct dev_pm_opp *opp;
103 unsigned long max_freq = ULONG_MAX;
105 opp = dev_pm_opp_find_freq_floor(devfreq->dev.parent, &max_freq);
115 * devfreq_get_freq_range() - Get the current freq range
116 * @devfreq: the devfreq instance
117 * @min_freq: the min frequency
118 * @max_freq: the max frequency
120 * This takes into consideration all constraints.
122 void devfreq_get_freq_range(struct devfreq *devfreq,
123 unsigned long *min_freq,
124 unsigned long *max_freq)
126 unsigned long *freq_table = devfreq->freq_table;
127 s32 qos_min_freq, qos_max_freq;
129 lockdep_assert_held(&devfreq->lock);
132 * Initialize minimum/maximum frequency from freq table.
133 * The devfreq drivers can initialize this in either ascending or
134 * descending order and devfreq core supports both.
136 if (freq_table[0] < freq_table[devfreq->max_state - 1]) {
137 *min_freq = freq_table[0];
138 *max_freq = freq_table[devfreq->max_state - 1];
140 *min_freq = freq_table[devfreq->max_state - 1];
141 *max_freq = freq_table[0];
144 /* Apply constraints from PM QoS */
145 qos_min_freq = dev_pm_qos_read_value(devfreq->dev.parent,
146 DEV_PM_QOS_MIN_FREQUENCY);
147 qos_max_freq = dev_pm_qos_read_value(devfreq->dev.parent,
148 DEV_PM_QOS_MAX_FREQUENCY);
149 *min_freq = max(*min_freq, (unsigned long)HZ_PER_KHZ * qos_min_freq);
150 if (qos_max_freq != PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE)
151 *max_freq = min(*max_freq,
152 (unsigned long)HZ_PER_KHZ * qos_max_freq);
154 /* Apply constraints from OPP interface */
155 *min_freq = max(*min_freq, devfreq->scaling_min_freq);
156 *max_freq = min(*max_freq, devfreq->scaling_max_freq);
158 if (*min_freq > *max_freq)
159 *min_freq = *max_freq;
161 EXPORT_SYMBOL(devfreq_get_freq_range);
164 * devfreq_get_freq_level() - Lookup freq_table for the frequency
165 * @devfreq: the devfreq instance
166 * @freq: the target frequency
168 static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq)
172 for (lev = 0; lev < devfreq->max_state; lev++)
173 if (freq == devfreq->freq_table[lev])
179 static int set_freq_table(struct devfreq *devfreq)
181 struct dev_pm_opp *opp;
185 /* Initialize the freq_table from OPP table */
186 count = dev_pm_opp_get_opp_count(devfreq->dev.parent);
190 devfreq->max_state = count;
191 devfreq->freq_table = devm_kcalloc(devfreq->dev.parent,
193 sizeof(*devfreq->freq_table),
195 if (!devfreq->freq_table)
198 for (i = 0, freq = 0; i < devfreq->max_state; i++, freq++) {
199 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq);
201 devm_kfree(devfreq->dev.parent, devfreq->freq_table);
205 devfreq->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->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);
371 * Print devfreq_frequency trace information between DEVFREQ_PRECHANGE
372 * and DEVFREQ_POSTCHANGE because for showing the correct frequency
373 * change order of between devfreq device and passive devfreq device.
375 if (trace_devfreq_frequency_enabled() && new_freq != cur_freq)
376 trace_devfreq_frequency(devfreq, new_freq, cur_freq);
378 freqs.new = new_freq;
379 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
381 if (devfreq_update_status(devfreq, new_freq))
382 dev_warn(&devfreq->dev,
383 "Couldn't update frequency transition information.\n");
385 devfreq->previous_freq = new_freq;
387 if (devfreq->suspend_freq)
388 devfreq->resume_freq = new_freq;
394 * devfreq_update_target() - Reevaluate the device and configure frequency
395 * on the final stage.
396 * @devfreq: the devfreq instance.
397 * @freq: the new frequency of parent device. This argument
398 * is only used for devfreq device using passive governor.
400 * Note: Lock devfreq->lock before calling devfreq_update_target. This function
401 * should be only used by both update_devfreq() and devfreq governors.
403 int devfreq_update_target(struct devfreq *devfreq, unsigned long freq)
405 unsigned long min_freq, max_freq;
409 lockdep_assert_held(&devfreq->lock);
411 if (!devfreq->governor)
414 /* Reevaluate the proper frequency */
415 err = devfreq->governor->get_target_freq(devfreq, &freq);
418 devfreq_get_freq_range(devfreq, &min_freq, &max_freq);
420 if (freq < min_freq) {
422 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
424 if (freq > max_freq) {
426 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
429 return devfreq_set_target(devfreq, freq, flags);
431 EXPORT_SYMBOL(devfreq_update_target);
433 /* Load monitoring helper functions for governors use */
436 * update_devfreq() - Reevaluate the device and configure frequency.
437 * @devfreq: the devfreq instance.
439 * Note: Lock devfreq->lock before calling update_devfreq
440 * This function is exported for governors.
442 int update_devfreq(struct devfreq *devfreq)
444 return devfreq_update_target(devfreq, 0L);
446 EXPORT_SYMBOL(update_devfreq);
449 * devfreq_monitor() - Periodically poll devfreq objects.
450 * @work: the work struct used to run devfreq_monitor periodically.
453 static void devfreq_monitor(struct work_struct *work)
456 struct devfreq *devfreq = container_of(work,
457 struct devfreq, work.work);
459 mutex_lock(&devfreq->lock);
460 err = update_devfreq(devfreq);
462 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
464 queue_delayed_work(devfreq_wq, &devfreq->work,
465 msecs_to_jiffies(devfreq->profile->polling_ms));
466 mutex_unlock(&devfreq->lock);
468 trace_devfreq_monitor(devfreq);
472 * devfreq_monitor_start() - Start load monitoring of devfreq instance
473 * @devfreq: the devfreq instance.
475 * Helper function for starting devfreq device load monitoring. By
476 * default delayed work based monitoring is supported. Function
477 * to be called from governor in response to DEVFREQ_GOV_START
478 * event when device is added to devfreq framework.
480 void devfreq_monitor_start(struct devfreq *devfreq)
482 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
485 switch (devfreq->profile->timer) {
486 case DEVFREQ_TIMER_DEFERRABLE:
487 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
489 case DEVFREQ_TIMER_DELAYED:
490 INIT_DELAYED_WORK(&devfreq->work, devfreq_monitor);
496 if (devfreq->profile->polling_ms)
497 queue_delayed_work(devfreq_wq, &devfreq->work,
498 msecs_to_jiffies(devfreq->profile->polling_ms));
500 EXPORT_SYMBOL(devfreq_monitor_start);
503 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
504 * @devfreq: the devfreq instance.
506 * Helper function to stop devfreq device load monitoring. Function
507 * to be called from governor in response to DEVFREQ_GOV_STOP
508 * event when device is removed from devfreq framework.
510 void devfreq_monitor_stop(struct devfreq *devfreq)
512 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
515 cancel_delayed_work_sync(&devfreq->work);
517 EXPORT_SYMBOL(devfreq_monitor_stop);
520 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
521 * @devfreq: the devfreq instance.
523 * Helper function to suspend devfreq device load monitoring. Function
524 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
525 * event or when polling interval is set to zero.
527 * Note: Though this function is same as devfreq_monitor_stop(),
528 * intentionally kept separate to provide hooks for collecting
529 * transition statistics.
531 void devfreq_monitor_suspend(struct devfreq *devfreq)
533 mutex_lock(&devfreq->lock);
534 if (devfreq->stop_polling) {
535 mutex_unlock(&devfreq->lock);
539 devfreq_update_status(devfreq, devfreq->previous_freq);
540 devfreq->stop_polling = true;
541 mutex_unlock(&devfreq->lock);
543 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
546 cancel_delayed_work_sync(&devfreq->work);
548 EXPORT_SYMBOL(devfreq_monitor_suspend);
551 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
552 * @devfreq: the devfreq instance.
554 * Helper function to resume devfreq device load monitoring. Function
555 * to be called from governor in response to DEVFREQ_GOV_RESUME
556 * event or when polling interval is set to non-zero.
558 void devfreq_monitor_resume(struct devfreq *devfreq)
562 mutex_lock(&devfreq->lock);
564 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
567 if (!devfreq->stop_polling)
570 if (!delayed_work_pending(&devfreq->work) &&
571 devfreq->profile->polling_ms)
572 queue_delayed_work(devfreq_wq, &devfreq->work,
573 msecs_to_jiffies(devfreq->profile->polling_ms));
576 devfreq->stats.last_update = get_jiffies_64();
577 devfreq->stop_polling = false;
579 if (devfreq->profile->get_cur_freq &&
580 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
581 devfreq->previous_freq = freq;
584 mutex_unlock(&devfreq->lock);
586 EXPORT_SYMBOL(devfreq_monitor_resume);
589 * devfreq_update_interval() - Update device devfreq monitoring interval
590 * @devfreq: the devfreq instance.
591 * @delay: new polling interval to be set.
593 * Helper function to set new load monitoring polling interval. Function
594 * to be called from governor in response to DEVFREQ_GOV_UPDATE_INTERVAL event.
596 void devfreq_update_interval(struct devfreq *devfreq, unsigned int *delay)
598 unsigned int cur_delay = devfreq->profile->polling_ms;
599 unsigned int new_delay = *delay;
601 mutex_lock(&devfreq->lock);
602 devfreq->profile->polling_ms = new_delay;
604 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
607 if (devfreq->stop_polling)
610 /* if new delay is zero, stop polling */
612 mutex_unlock(&devfreq->lock);
613 cancel_delayed_work_sync(&devfreq->work);
617 /* if current delay is zero, start polling with new delay */
619 queue_delayed_work(devfreq_wq, &devfreq->work,
620 msecs_to_jiffies(devfreq->profile->polling_ms));
624 /* if current delay is greater than new delay, restart polling */
625 if (cur_delay > new_delay) {
626 mutex_unlock(&devfreq->lock);
627 cancel_delayed_work_sync(&devfreq->work);
628 mutex_lock(&devfreq->lock);
629 if (!devfreq->stop_polling)
630 queue_delayed_work(devfreq_wq, &devfreq->work,
631 msecs_to_jiffies(devfreq->profile->polling_ms));
634 mutex_unlock(&devfreq->lock);
636 EXPORT_SYMBOL(devfreq_update_interval);
639 * devfreq_notifier_call() - Notify that the device frequency requirements
640 * has been changed out of devfreq framework.
641 * @nb: the notifier_block (supposed to be devfreq->nb)
645 * Called by a notifier that uses devfreq->nb.
647 static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
650 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
653 mutex_lock(&devfreq->lock);
655 devfreq->scaling_min_freq = find_available_min_freq(devfreq);
656 if (!devfreq->scaling_min_freq)
659 devfreq->scaling_max_freq = find_available_max_freq(devfreq);
660 if (!devfreq->scaling_max_freq) {
661 devfreq->scaling_max_freq = ULONG_MAX;
665 err = update_devfreq(devfreq);
668 mutex_unlock(&devfreq->lock);
670 dev_err(devfreq->dev.parent,
671 "failed to update frequency from OPP notifier (%d)\n",
678 * qos_notifier_call() - Common handler for QoS constraints.
679 * @devfreq: the devfreq instance.
681 static int qos_notifier_call(struct devfreq *devfreq)
685 mutex_lock(&devfreq->lock);
686 err = update_devfreq(devfreq);
687 mutex_unlock(&devfreq->lock);
689 dev_err(devfreq->dev.parent,
690 "failed to update frequency from PM QoS (%d)\n",
697 * qos_min_notifier_call() - Callback for QoS min_freq changes.
698 * @nb: Should be devfreq->nb_min
702 static int qos_min_notifier_call(struct notifier_block *nb,
703 unsigned long val, void *ptr)
705 return qos_notifier_call(container_of(nb, struct devfreq, nb_min));
709 * qos_max_notifier_call() - Callback for QoS max_freq changes.
710 * @nb: Should be devfreq->nb_max
714 static int qos_max_notifier_call(struct notifier_block *nb,
715 unsigned long val, void *ptr)
717 return qos_notifier_call(container_of(nb, struct devfreq, nb_max));
721 * devfreq_dev_release() - Callback for struct device to release the device.
722 * @dev: the devfreq device
724 * Remove devfreq from the list and release its resources.
726 static void devfreq_dev_release(struct device *dev)
728 struct devfreq *devfreq = to_devfreq(dev);
731 mutex_lock(&devfreq_list_lock);
732 list_del(&devfreq->node);
733 mutex_unlock(&devfreq_list_lock);
735 err = dev_pm_qos_remove_notifier(devfreq->dev.parent, &devfreq->nb_max,
736 DEV_PM_QOS_MAX_FREQUENCY);
737 if (err && err != -ENOENT)
738 dev_warn(dev->parent,
739 "Failed to remove max_freq notifier: %d\n", err);
740 err = dev_pm_qos_remove_notifier(devfreq->dev.parent, &devfreq->nb_min,
741 DEV_PM_QOS_MIN_FREQUENCY);
742 if (err && err != -ENOENT)
743 dev_warn(dev->parent,
744 "Failed to remove min_freq notifier: %d\n", err);
746 if (dev_pm_qos_request_active(&devfreq->user_max_freq_req)) {
747 err = dev_pm_qos_remove_request(&devfreq->user_max_freq_req);
749 dev_warn(dev->parent,
750 "Failed to remove max_freq request: %d\n", err);
752 if (dev_pm_qos_request_active(&devfreq->user_min_freq_req)) {
753 err = dev_pm_qos_remove_request(&devfreq->user_min_freq_req);
755 dev_warn(dev->parent,
756 "Failed to remove min_freq request: %d\n", err);
759 if (devfreq->profile->exit)
760 devfreq->profile->exit(devfreq->dev.parent);
762 if (devfreq->opp_table)
763 dev_pm_opp_put_opp_table(devfreq->opp_table);
765 mutex_destroy(&devfreq->lock);
769 static void create_sysfs_files(struct devfreq *devfreq,
770 const struct devfreq_governor *gov);
771 static void remove_sysfs_files(struct devfreq *devfreq,
772 const struct devfreq_governor *gov);
775 * devfreq_add_device() - Add devfreq feature to the device
776 * @dev: the device to add devfreq feature.
777 * @profile: device-specific profile to run devfreq.
778 * @governor_name: name of the policy to choose frequency.
779 * @data: private data for the governor. The devfreq framework does not
782 struct devfreq *devfreq_add_device(struct device *dev,
783 struct devfreq_dev_profile *profile,
784 const char *governor_name,
787 struct devfreq *devfreq;
788 struct devfreq_governor *governor;
789 unsigned long min_freq, max_freq;
792 if (!dev || !profile || !governor_name) {
793 dev_err(dev, "%s: Invalid parameters.\n", __func__);
794 return ERR_PTR(-EINVAL);
797 mutex_lock(&devfreq_list_lock);
798 devfreq = find_device_devfreq(dev);
799 mutex_unlock(&devfreq_list_lock);
800 if (!IS_ERR(devfreq)) {
801 dev_err(dev, "%s: devfreq device already exists!\n",
807 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
813 mutex_init(&devfreq->lock);
814 mutex_lock(&devfreq->lock);
815 devfreq->dev.parent = dev;
816 devfreq->dev.class = devfreq_class;
817 devfreq->dev.release = devfreq_dev_release;
818 INIT_LIST_HEAD(&devfreq->node);
819 devfreq->profile = profile;
820 devfreq->previous_freq = profile->initial_freq;
821 devfreq->last_status.current_frequency = profile->initial_freq;
822 devfreq->data = data;
823 devfreq->nb.notifier_call = devfreq_notifier_call;
825 if (devfreq->profile->timer < 0
826 || devfreq->profile->timer >= DEVFREQ_TIMER_NUM) {
827 mutex_unlock(&devfreq->lock);
832 if (!devfreq->profile->max_state || !devfreq->profile->freq_table) {
833 mutex_unlock(&devfreq->lock);
834 err = set_freq_table(devfreq);
837 mutex_lock(&devfreq->lock);
839 devfreq->freq_table = devfreq->profile->freq_table;
840 devfreq->max_state = devfreq->profile->max_state;
843 devfreq->scaling_min_freq = find_available_min_freq(devfreq);
844 if (!devfreq->scaling_min_freq) {
845 mutex_unlock(&devfreq->lock);
850 devfreq->scaling_max_freq = find_available_max_freq(devfreq);
851 if (!devfreq->scaling_max_freq) {
852 mutex_unlock(&devfreq->lock);
857 devfreq_get_freq_range(devfreq, &min_freq, &max_freq);
859 devfreq->suspend_freq = dev_pm_opp_get_suspend_opp_freq(dev);
860 devfreq->opp_table = dev_pm_opp_get_opp_table(dev);
861 if (IS_ERR(devfreq->opp_table))
862 devfreq->opp_table = NULL;
864 atomic_set(&devfreq->suspend_count, 0);
866 dev_set_name(&devfreq->dev, "%s", dev_name(dev));
867 err = device_register(&devfreq->dev);
869 mutex_unlock(&devfreq->lock);
870 put_device(&devfreq->dev);
874 devfreq->stats.trans_table = devm_kzalloc(&devfreq->dev,
875 array3_size(sizeof(unsigned int),
879 if (!devfreq->stats.trans_table) {
880 mutex_unlock(&devfreq->lock);
885 devfreq->stats.time_in_state = devm_kcalloc(&devfreq->dev,
887 sizeof(*devfreq->stats.time_in_state),
889 if (!devfreq->stats.time_in_state) {
890 mutex_unlock(&devfreq->lock);
895 devfreq->stats.total_trans = 0;
896 devfreq->stats.last_update = get_jiffies_64();
898 srcu_init_notifier_head(&devfreq->transition_notifier_list);
900 mutex_unlock(&devfreq->lock);
902 err = dev_pm_qos_add_request(dev, &devfreq->user_min_freq_req,
903 DEV_PM_QOS_MIN_FREQUENCY, 0);
906 err = dev_pm_qos_add_request(dev, &devfreq->user_max_freq_req,
907 DEV_PM_QOS_MAX_FREQUENCY,
908 PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE);
912 devfreq->nb_min.notifier_call = qos_min_notifier_call;
913 err = dev_pm_qos_add_notifier(dev, &devfreq->nb_min,
914 DEV_PM_QOS_MIN_FREQUENCY);
918 devfreq->nb_max.notifier_call = qos_max_notifier_call;
919 err = dev_pm_qos_add_notifier(dev, &devfreq->nb_max,
920 DEV_PM_QOS_MAX_FREQUENCY);
924 mutex_lock(&devfreq_list_lock);
926 governor = try_then_request_governor(governor_name);
927 if (IS_ERR(governor)) {
928 dev_err(dev, "%s: Unable to find governor for the device\n",
930 err = PTR_ERR(governor);
934 devfreq->governor = governor;
935 err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
938 dev_err_probe(dev, err,
939 "%s: Unable to start governor for the device\n",
943 create_sysfs_files(devfreq, devfreq->governor);
945 list_add(&devfreq->node, &devfreq_list);
947 mutex_unlock(&devfreq_list_lock);
949 if (devfreq->profile->is_cooling_device) {
950 devfreq->cdev = devfreq_cooling_em_register(devfreq, NULL);
951 if (IS_ERR(devfreq->cdev))
952 devfreq->cdev = NULL;
958 mutex_unlock(&devfreq_list_lock);
960 devfreq_remove_device(devfreq);
967 EXPORT_SYMBOL(devfreq_add_device);
970 * devfreq_remove_device() - Remove devfreq feature from a device.
971 * @devfreq: the devfreq instance to be removed
973 * The opposite of devfreq_add_device().
975 int devfreq_remove_device(struct devfreq *devfreq)
980 devfreq_cooling_unregister(devfreq->cdev);
982 if (devfreq->governor) {
983 devfreq->governor->event_handler(devfreq,
984 DEVFREQ_GOV_STOP, NULL);
985 remove_sysfs_files(devfreq, devfreq->governor);
988 device_unregister(&devfreq->dev);
992 EXPORT_SYMBOL(devfreq_remove_device);
994 static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
996 struct devfreq **r = res;
998 if (WARN_ON(!r || !*r))
1004 static void devm_devfreq_dev_release(struct device *dev, void *res)
1006 devfreq_remove_device(*(struct devfreq **)res);
1010 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
1011 * @dev: the device to add devfreq feature.
1012 * @profile: device-specific profile to run devfreq.
1013 * @governor_name: name of the policy to choose frequency.
1014 * @data: private data for the governor. The devfreq framework does not
1017 * This function manages automatically the memory of devfreq device using device
1018 * resource management and simplify the free operation for memory of devfreq
1021 struct devfreq *devm_devfreq_add_device(struct device *dev,
1022 struct devfreq_dev_profile *profile,
1023 const char *governor_name,
1026 struct devfreq **ptr, *devfreq;
1028 ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
1030 return ERR_PTR(-ENOMEM);
1032 devfreq = devfreq_add_device(dev, profile, governor_name, data);
1033 if (IS_ERR(devfreq)) {
1039 devres_add(dev, ptr);
1043 EXPORT_SYMBOL(devm_devfreq_add_device);
1047 * devfreq_get_devfreq_by_node - Get the devfreq device from devicetree
1048 * @node - pointer to device_node
1050 * return the instance of devfreq device
1052 struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node)
1054 struct devfreq *devfreq;
1057 return ERR_PTR(-EINVAL);
1059 mutex_lock(&devfreq_list_lock);
1060 list_for_each_entry(devfreq, &devfreq_list, node) {
1061 if (devfreq->dev.parent
1062 && devfreq->dev.parent->of_node == node) {
1063 mutex_unlock(&devfreq_list_lock);
1067 mutex_unlock(&devfreq_list_lock);
1069 return ERR_PTR(-ENODEV);
1073 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
1074 * @dev - instance to the given device
1075 * @phandle_name - name of property holding a phandle value
1076 * @index - index into list of devfreq
1078 * return the instance of devfreq device
1080 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
1081 const char *phandle_name, int index)
1083 struct device_node *node;
1084 struct devfreq *devfreq;
1086 if (!dev || !phandle_name)
1087 return ERR_PTR(-EINVAL);
1090 return ERR_PTR(-EINVAL);
1092 node = of_parse_phandle(dev->of_node, phandle_name, index);
1094 return ERR_PTR(-ENODEV);
1096 devfreq = devfreq_get_devfreq_by_node(node);
1103 struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node)
1105 return ERR_PTR(-ENODEV);
1108 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
1109 const char *phandle_name, int index)
1111 return ERR_PTR(-ENODEV);
1113 #endif /* CONFIG_OF */
1114 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_node);
1115 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
1118 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
1119 * @dev: the device from which to remove devfreq feature.
1120 * @devfreq: the devfreq instance to be removed
1122 void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
1124 WARN_ON(devres_release(dev, devm_devfreq_dev_release,
1125 devm_devfreq_dev_match, devfreq));
1127 EXPORT_SYMBOL(devm_devfreq_remove_device);
1130 * devfreq_suspend_device() - Suspend devfreq of a device.
1131 * @devfreq: the devfreq instance to be suspended
1133 * This function is intended to be called by the pm callbacks
1134 * (e.g., runtime_suspend, suspend) of the device driver that
1135 * holds the devfreq.
1137 int devfreq_suspend_device(struct devfreq *devfreq)
1144 if (atomic_inc_return(&devfreq->suspend_count) > 1)
1147 if (devfreq->governor) {
1148 ret = devfreq->governor->event_handler(devfreq,
1149 DEVFREQ_GOV_SUSPEND, NULL);
1154 if (devfreq->suspend_freq) {
1155 mutex_lock(&devfreq->lock);
1156 ret = devfreq_set_target(devfreq, devfreq->suspend_freq, 0);
1157 mutex_unlock(&devfreq->lock);
1164 EXPORT_SYMBOL(devfreq_suspend_device);
1167 * devfreq_resume_device() - Resume devfreq of a device.
1168 * @devfreq: the devfreq instance to be resumed
1170 * This function is intended to be called by the pm callbacks
1171 * (e.g., runtime_resume, resume) of the device driver that
1172 * holds the devfreq.
1174 int devfreq_resume_device(struct devfreq *devfreq)
1181 if (atomic_dec_return(&devfreq->suspend_count) >= 1)
1184 if (devfreq->resume_freq) {
1185 mutex_lock(&devfreq->lock);
1186 ret = devfreq_set_target(devfreq, devfreq->resume_freq, 0);
1187 mutex_unlock(&devfreq->lock);
1192 if (devfreq->governor) {
1193 ret = devfreq->governor->event_handler(devfreq,
1194 DEVFREQ_GOV_RESUME, NULL);
1201 EXPORT_SYMBOL(devfreq_resume_device);
1204 * devfreq_suspend() - Suspend devfreq governors and devices
1206 * Called during system wide Suspend/Hibernate cycles for suspending governors
1207 * and devices preserving the state for resume. On some platforms the devfreq
1208 * device must have precise state (frequency) after resume in order to provide
1209 * fully operating setup.
1211 void devfreq_suspend(void)
1213 struct devfreq *devfreq;
1216 mutex_lock(&devfreq_list_lock);
1217 list_for_each_entry(devfreq, &devfreq_list, node) {
1218 ret = devfreq_suspend_device(devfreq);
1220 dev_err(&devfreq->dev,
1221 "failed to suspend devfreq device\n");
1223 mutex_unlock(&devfreq_list_lock);
1227 * devfreq_resume() - Resume devfreq governors and devices
1229 * Called during system wide Suspend/Hibernate cycle for resuming governors and
1230 * devices that are suspended with devfreq_suspend().
1232 void devfreq_resume(void)
1234 struct devfreq *devfreq;
1237 mutex_lock(&devfreq_list_lock);
1238 list_for_each_entry(devfreq, &devfreq_list, node) {
1239 ret = devfreq_resume_device(devfreq);
1241 dev_warn(&devfreq->dev,
1242 "failed to resume devfreq device\n");
1244 mutex_unlock(&devfreq_list_lock);
1248 * devfreq_add_governor() - Add devfreq governor
1249 * @governor: the devfreq governor to be added
1251 int devfreq_add_governor(struct devfreq_governor *governor)
1253 struct devfreq_governor *g;
1254 struct devfreq *devfreq;
1258 pr_err("%s: Invalid parameters.\n", __func__);
1262 mutex_lock(&devfreq_list_lock);
1263 g = find_devfreq_governor(governor->name);
1265 pr_err("%s: governor %s already registered\n", __func__,
1271 list_add(&governor->node, &devfreq_governor_list);
1273 list_for_each_entry(devfreq, &devfreq_list, node) {
1275 struct device *dev = devfreq->dev.parent;
1277 if (!strncmp(devfreq->governor->name, governor->name,
1278 DEVFREQ_NAME_LEN)) {
1279 /* The following should never occur */
1280 if (devfreq->governor) {
1282 "%s: Governor %s already present\n",
1283 __func__, devfreq->governor->name);
1284 ret = devfreq->governor->event_handler(devfreq,
1285 DEVFREQ_GOV_STOP, NULL);
1288 "%s: Governor %s stop = %d\n",
1290 devfreq->governor->name, ret);
1294 devfreq->governor = governor;
1295 ret = devfreq->governor->event_handler(devfreq,
1296 DEVFREQ_GOV_START, NULL);
1298 dev_warn(dev, "%s: Governor %s start=%d\n",
1299 __func__, devfreq->governor->name,
1306 mutex_unlock(&devfreq_list_lock);
1310 EXPORT_SYMBOL(devfreq_add_governor);
1312 static void devm_devfreq_remove_governor(void *governor)
1314 WARN_ON(devfreq_remove_governor(governor));
1318 * devm_devfreq_add_governor() - Add devfreq governor
1319 * @dev: device which adds devfreq governor
1320 * @governor: the devfreq governor to be added
1322 * This is a resource-managed variant of devfreq_add_governor().
1324 int devm_devfreq_add_governor(struct device *dev,
1325 struct devfreq_governor *governor)
1329 err = devfreq_add_governor(governor);
1333 return devm_add_action_or_reset(dev, devm_devfreq_remove_governor,
1336 EXPORT_SYMBOL(devm_devfreq_add_governor);
1339 * devfreq_remove_governor() - Remove devfreq feature from a device.
1340 * @governor: the devfreq governor to be removed
1342 int devfreq_remove_governor(struct devfreq_governor *governor)
1344 struct devfreq_governor *g;
1345 struct devfreq *devfreq;
1349 pr_err("%s: Invalid parameters.\n", __func__);
1353 mutex_lock(&devfreq_list_lock);
1354 g = find_devfreq_governor(governor->name);
1356 pr_err("%s: governor %s not registered\n", __func__,
1361 list_for_each_entry(devfreq, &devfreq_list, node) {
1363 struct device *dev = devfreq->dev.parent;
1365 if (!strncmp(devfreq->governor->name, governor->name,
1366 DEVFREQ_NAME_LEN)) {
1367 /* we should have a devfreq governor! */
1368 if (!devfreq->governor) {
1369 dev_warn(dev, "%s: Governor %s NOT present\n",
1370 __func__, governor->name);
1374 ret = devfreq->governor->event_handler(devfreq,
1375 DEVFREQ_GOV_STOP, NULL);
1377 dev_warn(dev, "%s: Governor %s stop=%d\n",
1378 __func__, devfreq->governor->name,
1381 devfreq->governor = NULL;
1385 list_del(&governor->node);
1387 mutex_unlock(&devfreq_list_lock);
1391 EXPORT_SYMBOL(devfreq_remove_governor);
1393 static ssize_t name_show(struct device *dev,
1394 struct device_attribute *attr, char *buf)
1396 struct devfreq *df = to_devfreq(dev);
1397 return sprintf(buf, "%s\n", dev_name(df->dev.parent));
1399 static DEVICE_ATTR_RO(name);
1401 static ssize_t governor_show(struct device *dev,
1402 struct device_attribute *attr, char *buf)
1404 struct devfreq *df = to_devfreq(dev);
1409 return sprintf(buf, "%s\n", df->governor->name);
1412 static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
1413 const char *buf, size_t count)
1415 struct devfreq *df = to_devfreq(dev);
1417 char str_governor[DEVFREQ_NAME_LEN + 1];
1418 const struct devfreq_governor *governor, *prev_governor;
1423 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
1427 mutex_lock(&devfreq_list_lock);
1428 governor = try_then_request_governor(str_governor);
1429 if (IS_ERR(governor)) {
1430 ret = PTR_ERR(governor);
1433 if (df->governor == governor) {
1436 } else if (IS_SUPPORTED_FLAG(df->governor->flags, IMMUTABLE)
1437 || IS_SUPPORTED_FLAG(governor->flags, IMMUTABLE)) {
1443 * Stop the current governor and remove the specific sysfs files
1444 * which depend on current governor.
1446 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1448 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1449 __func__, df->governor->name, ret);
1452 remove_sysfs_files(df, df->governor);
1455 * Start the new governor and create the specific sysfs files
1456 * which depend on the new governor.
1458 prev_governor = df->governor;
1459 df->governor = governor;
1460 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1462 dev_warn(dev, "%s: Governor %s not started(%d)\n",
1463 __func__, df->governor->name, ret);
1465 /* Restore previous governor */
1466 df->governor = prev_governor;
1467 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1470 "%s: reverting to Governor %s failed (%d)\n",
1471 __func__, prev_governor->name, ret);
1472 df->governor = NULL;
1478 * Create the sysfs files for the new governor. But if failed to start
1479 * the new governor, restore the sysfs files of previous governor.
1481 create_sysfs_files(df, df->governor);
1484 mutex_unlock(&devfreq_list_lock);
1490 static DEVICE_ATTR_RW(governor);
1492 static ssize_t available_governors_show(struct device *d,
1493 struct device_attribute *attr,
1496 struct devfreq *df = to_devfreq(d);
1502 mutex_lock(&devfreq_list_lock);
1505 * The devfreq with immutable governor (e.g., passive) shows
1506 * only own governor.
1508 if (IS_SUPPORTED_FLAG(df->governor->flags, IMMUTABLE)) {
1509 count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
1510 "%s ", df->governor->name);
1512 * The devfreq device shows the registered governor except for
1513 * immutable governors such as passive governor .
1516 struct devfreq_governor *governor;
1518 list_for_each_entry(governor, &devfreq_governor_list, node) {
1519 if (IS_SUPPORTED_FLAG(governor->flags, IMMUTABLE))
1521 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1522 "%s ", governor->name);
1526 mutex_unlock(&devfreq_list_lock);
1528 /* Truncate the trailing space */
1532 count += sprintf(&buf[count], "\n");
1536 static DEVICE_ATTR_RO(available_governors);
1538 static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
1542 struct devfreq *df = to_devfreq(dev);
1547 if (df->profile->get_cur_freq &&
1548 !df->profile->get_cur_freq(df->dev.parent, &freq))
1549 return sprintf(buf, "%lu\n", freq);
1551 return sprintf(buf, "%lu\n", df->previous_freq);
1553 static DEVICE_ATTR_RO(cur_freq);
1555 static ssize_t target_freq_show(struct device *dev,
1556 struct device_attribute *attr, char *buf)
1558 struct devfreq *df = to_devfreq(dev);
1560 return sprintf(buf, "%lu\n", df->previous_freq);
1562 static DEVICE_ATTR_RO(target_freq);
1564 static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
1565 const char *buf, size_t count)
1567 struct devfreq *df = to_devfreq(dev);
1568 unsigned long value;
1572 * Protect against theoretical sysfs writes between
1573 * device_add and dev_pm_qos_add_request
1575 if (!dev_pm_qos_request_active(&df->user_min_freq_req))
1578 ret = sscanf(buf, "%lu", &value);
1582 /* Round down to kHz for PM QoS */
1583 ret = dev_pm_qos_update_request(&df->user_min_freq_req,
1584 value / HZ_PER_KHZ);
1591 static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
1594 struct devfreq *df = to_devfreq(dev);
1595 unsigned long min_freq, max_freq;
1597 mutex_lock(&df->lock);
1598 devfreq_get_freq_range(df, &min_freq, &max_freq);
1599 mutex_unlock(&df->lock);
1601 return sprintf(buf, "%lu\n", min_freq);
1603 static DEVICE_ATTR_RW(min_freq);
1605 static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
1606 const char *buf, size_t count)
1608 struct devfreq *df = to_devfreq(dev);
1609 unsigned long value;
1613 * Protect against theoretical sysfs writes between
1614 * device_add and dev_pm_qos_add_request
1616 if (!dev_pm_qos_request_active(&df->user_max_freq_req))
1619 ret = sscanf(buf, "%lu", &value);
1624 * PM QoS frequencies are in kHz so we need to convert. Convert by
1625 * rounding upwards so that the acceptable interval never shrinks.
1627 * For example if the user writes "666666666" to sysfs this value will
1628 * be converted to 666667 kHz and back to 666667000 Hz before an OPP
1629 * lookup, this ensures that an OPP of 666666666Hz is still accepted.
1631 * A value of zero means "no limit".
1634 value = DIV_ROUND_UP(value, HZ_PER_KHZ);
1636 value = PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE;
1638 ret = dev_pm_qos_update_request(&df->user_max_freq_req, value);
1645 static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
1648 struct devfreq *df = to_devfreq(dev);
1649 unsigned long min_freq, max_freq;
1651 mutex_lock(&df->lock);
1652 devfreq_get_freq_range(df, &min_freq, &max_freq);
1653 mutex_unlock(&df->lock);
1655 return sprintf(buf, "%lu\n", max_freq);
1657 static DEVICE_ATTR_RW(max_freq);
1659 static ssize_t available_frequencies_show(struct device *d,
1660 struct device_attribute *attr,
1663 struct devfreq *df = to_devfreq(d);
1670 mutex_lock(&df->lock);
1672 for (i = 0; i < df->max_state; i++)
1673 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1674 "%lu ", df->freq_table[i]);
1676 mutex_unlock(&df->lock);
1677 /* Truncate the trailing space */
1681 count += sprintf(&buf[count], "\n");
1685 static DEVICE_ATTR_RO(available_frequencies);
1687 static ssize_t trans_stat_show(struct device *dev,
1688 struct device_attribute *attr, char *buf)
1690 struct devfreq *df = to_devfreq(dev);
1693 unsigned int max_state;
1697 max_state = df->max_state;
1700 return sprintf(buf, "Not Supported.\n");
1702 mutex_lock(&df->lock);
1703 if (!df->stop_polling &&
1704 devfreq_update_status(df, df->previous_freq)) {
1705 mutex_unlock(&df->lock);
1708 mutex_unlock(&df->lock);
1710 len = sprintf(buf, " From : To\n");
1711 len += sprintf(buf + len, " :");
1712 for (i = 0; i < max_state; i++)
1713 len += sprintf(buf + len, "%10lu",
1716 len += sprintf(buf + len, " time(ms)\n");
1718 for (i = 0; i < max_state; i++) {
1719 if (df->freq_table[i] == df->previous_freq)
1720 len += sprintf(buf + len, "*");
1722 len += sprintf(buf + len, " ");
1724 len += sprintf(buf + len, "%10lu:", df->freq_table[i]);
1725 for (j = 0; j < max_state; j++)
1726 len += sprintf(buf + len, "%10u",
1727 df->stats.trans_table[(i * max_state) + j]);
1729 len += sprintf(buf + len, "%10llu\n", (u64)
1730 jiffies64_to_msecs(df->stats.time_in_state[i]));
1733 len += sprintf(buf + len, "Total transition : %u\n",
1734 df->stats.total_trans);
1738 static ssize_t trans_stat_store(struct device *dev,
1739 struct device_attribute *attr,
1740 const char *buf, size_t count)
1742 struct devfreq *df = to_devfreq(dev);
1748 if (df->max_state == 0)
1751 err = kstrtoint(buf, 10, &value);
1752 if (err || value != 0)
1755 mutex_lock(&df->lock);
1756 memset(df->stats.time_in_state, 0, (df->max_state *
1757 sizeof(*df->stats.time_in_state)));
1758 memset(df->stats.trans_table, 0, array3_size(sizeof(unsigned int),
1761 df->stats.total_trans = 0;
1762 df->stats.last_update = get_jiffies_64();
1763 mutex_unlock(&df->lock);
1767 static DEVICE_ATTR_RW(trans_stat);
1769 static struct attribute *devfreq_attrs[] = {
1770 &dev_attr_name.attr,
1771 &dev_attr_governor.attr,
1772 &dev_attr_available_governors.attr,
1773 &dev_attr_cur_freq.attr,
1774 &dev_attr_available_frequencies.attr,
1775 &dev_attr_target_freq.attr,
1776 &dev_attr_min_freq.attr,
1777 &dev_attr_max_freq.attr,
1778 &dev_attr_trans_stat.attr,
1781 ATTRIBUTE_GROUPS(devfreq);
1783 static ssize_t polling_interval_show(struct device *dev,
1784 struct device_attribute *attr, char *buf)
1786 struct devfreq *df = to_devfreq(dev);
1791 return sprintf(buf, "%d\n", df->profile->polling_ms);
1794 static ssize_t polling_interval_store(struct device *dev,
1795 struct device_attribute *attr,
1796 const char *buf, size_t count)
1798 struct devfreq *df = to_devfreq(dev);
1805 ret = sscanf(buf, "%u", &value);
1809 df->governor->event_handler(df, DEVFREQ_GOV_UPDATE_INTERVAL, &value);
1814 static DEVICE_ATTR_RW(polling_interval);
1816 static ssize_t timer_show(struct device *dev,
1817 struct device_attribute *attr, char *buf)
1819 struct devfreq *df = to_devfreq(dev);
1824 return sprintf(buf, "%s\n", timer_name[df->profile->timer]);
1827 static ssize_t timer_store(struct device *dev, struct device_attribute *attr,
1828 const char *buf, size_t count)
1830 struct devfreq *df = to_devfreq(dev);
1831 char str_timer[DEVFREQ_NAME_LEN + 1];
1835 if (!df->governor || !df->profile)
1838 ret = sscanf(buf, "%16s", str_timer);
1842 for (i = 0; i < DEVFREQ_TIMER_NUM; i++) {
1843 if (!strncmp(timer_name[i], str_timer, DEVFREQ_NAME_LEN)) {
1854 if (df->profile->timer == timer) {
1859 mutex_lock(&df->lock);
1860 df->profile->timer = timer;
1861 mutex_unlock(&df->lock);
1863 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1865 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1866 __func__, df->governor->name, ret);
1870 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1872 dev_warn(dev, "%s: Governor %s not started(%d)\n",
1873 __func__, df->governor->name, ret);
1875 return ret ? ret : count;
1877 static DEVICE_ATTR_RW(timer);
1879 #define CREATE_SYSFS_FILE(df, name) \
1882 ret = sysfs_create_file(&df->dev.kobj, &dev_attr_##name.attr); \
1884 dev_warn(&df->dev, \
1885 "Unable to create attr(%s)\n", "##name"); \
1889 /* Create the specific sysfs files which depend on each governor. */
1890 static void create_sysfs_files(struct devfreq *devfreq,
1891 const struct devfreq_governor *gov)
1893 if (IS_SUPPORTED_ATTR(gov->attrs, POLLING_INTERVAL))
1894 CREATE_SYSFS_FILE(devfreq, polling_interval);
1895 if (IS_SUPPORTED_ATTR(gov->attrs, TIMER))
1896 CREATE_SYSFS_FILE(devfreq, timer);
1899 /* Remove the specific sysfs files which depend on each governor. */
1900 static void remove_sysfs_files(struct devfreq *devfreq,
1901 const struct devfreq_governor *gov)
1903 if (IS_SUPPORTED_ATTR(gov->attrs, POLLING_INTERVAL))
1904 sysfs_remove_file(&devfreq->dev.kobj,
1905 &dev_attr_polling_interval.attr);
1906 if (IS_SUPPORTED_ATTR(gov->attrs, TIMER))
1907 sysfs_remove_file(&devfreq->dev.kobj, &dev_attr_timer.attr);
1911 * devfreq_summary_show() - Show the summary of the devfreq devices
1912 * @s: seq_file instance to show the summary of devfreq devices
1915 * Show the summary of the devfreq devices via 'devfreq_summary' debugfs file.
1916 * It helps that user can know the detailed information of the devfreq devices.
1918 * Return 0 always because it shows the information without any data change.
1920 static int devfreq_summary_show(struct seq_file *s, void *data)
1922 struct devfreq *devfreq;
1923 struct devfreq *p_devfreq = NULL;
1924 unsigned long cur_freq, min_freq, max_freq;
1925 unsigned int polling_ms;
1928 seq_printf(s, "%-30s %-30s %-15s %-10s %10s %12s %12s %12s\n",
1937 seq_printf(s, "%30s %30s %15s %10s %10s %12s %12s %12s\n",
1938 "------------------------------",
1939 "------------------------------",
1947 mutex_lock(&devfreq_list_lock);
1949 list_for_each_entry_reverse(devfreq, &devfreq_list, node) {
1950 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE)
1951 if (!strncmp(devfreq->governor->name, DEVFREQ_GOV_PASSIVE,
1952 DEVFREQ_NAME_LEN)) {
1953 struct devfreq_passive_data *data = devfreq->data;
1956 p_devfreq = data->parent;
1962 mutex_lock(&devfreq->lock);
1963 cur_freq = devfreq->previous_freq;
1964 devfreq_get_freq_range(devfreq, &min_freq, &max_freq);
1965 timer = devfreq->profile->timer;
1967 if (IS_SUPPORTED_ATTR(devfreq->governor->attrs, POLLING_INTERVAL))
1968 polling_ms = devfreq->profile->polling_ms;
1971 mutex_unlock(&devfreq->lock);
1974 "%-30s %-30s %-15s %-10s %10d %12ld %12ld %12ld\n",
1975 dev_name(&devfreq->dev),
1976 p_devfreq ? dev_name(&p_devfreq->dev) : "null",
1977 devfreq->governor->name,
1978 polling_ms ? timer_name[timer] : "null",
1985 mutex_unlock(&devfreq_list_lock);
1989 DEFINE_SHOW_ATTRIBUTE(devfreq_summary);
1991 static int __init devfreq_init(void)
1993 devfreq_class = class_create(THIS_MODULE, "devfreq");
1994 if (IS_ERR(devfreq_class)) {
1995 pr_err("%s: couldn't create class\n", __FILE__);
1996 return PTR_ERR(devfreq_class);
1999 devfreq_wq = create_freezable_workqueue("devfreq_wq");
2001 class_destroy(devfreq_class);
2002 pr_err("%s: couldn't create workqueue\n", __FILE__);
2005 devfreq_class->dev_groups = devfreq_groups;
2007 devfreq_debugfs = debugfs_create_dir("devfreq", NULL);
2008 debugfs_create_file("devfreq_summary", 0444,
2009 devfreq_debugfs, NULL,
2010 &devfreq_summary_fops);
2014 subsys_initcall(devfreq_init);
2017 * The following are helper functions for devfreq user device drivers with
2022 * devfreq_recommended_opp() - Helper function to get proper OPP for the
2023 * freq value given to target callback.
2024 * @dev: The devfreq user device. (parent of devfreq)
2025 * @freq: The frequency given to target function
2026 * @flags: Flags handed from devfreq framework.
2028 * The callers are required to call dev_pm_opp_put() for the returned OPP after
2031 struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
2032 unsigned long *freq,
2035 struct dev_pm_opp *opp;
2037 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
2038 /* The freq is an upper bound. opp should be lower */
2039 opp = dev_pm_opp_find_freq_floor(dev, freq);
2041 /* If not available, use the closest opp */
2042 if (opp == ERR_PTR(-ERANGE))
2043 opp = dev_pm_opp_find_freq_ceil(dev, freq);
2045 /* The freq is an lower bound. opp should be higher */
2046 opp = dev_pm_opp_find_freq_ceil(dev, freq);
2048 /* If not available, use the closest opp */
2049 if (opp == ERR_PTR(-ERANGE))
2050 opp = dev_pm_opp_find_freq_floor(dev, freq);
2055 EXPORT_SYMBOL(devfreq_recommended_opp);
2058 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
2059 * for any changes in the OPP availability
2061 * @dev: The devfreq user device. (parent of devfreq)
2062 * @devfreq: The devfreq object.
2064 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
2066 return dev_pm_opp_register_notifier(dev, &devfreq->nb);
2068 EXPORT_SYMBOL(devfreq_register_opp_notifier);
2071 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
2072 * notified for any changes in the OPP
2073 * availability changes anymore.
2074 * @dev: The devfreq user device. (parent of devfreq)
2075 * @devfreq: The devfreq object.
2077 * At exit() callback of devfreq_dev_profile, this must be included if
2078 * devfreq_recommended_opp is used.
2080 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
2082 return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
2084 EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
2086 static void devm_devfreq_opp_release(struct device *dev, void *res)
2088 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
2092 * devm_devfreq_register_opp_notifier() - Resource-managed
2093 * devfreq_register_opp_notifier()
2094 * @dev: The devfreq user device. (parent of devfreq)
2095 * @devfreq: The devfreq object.
2097 int devm_devfreq_register_opp_notifier(struct device *dev,
2098 struct devfreq *devfreq)
2100 struct devfreq **ptr;
2103 ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
2107 ret = devfreq_register_opp_notifier(dev, devfreq);
2114 devres_add(dev, ptr);
2118 EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
2121 * devm_devfreq_unregister_opp_notifier() - Resource-managed
2122 * devfreq_unregister_opp_notifier()
2123 * @dev: The devfreq user device. (parent of devfreq)
2124 * @devfreq: The devfreq object.
2126 void devm_devfreq_unregister_opp_notifier(struct device *dev,
2127 struct devfreq *devfreq)
2129 WARN_ON(devres_release(dev, devm_devfreq_opp_release,
2130 devm_devfreq_dev_match, devfreq));
2132 EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
2135 * devfreq_register_notifier() - Register a driver with devfreq
2136 * @devfreq: The devfreq object.
2137 * @nb: The notifier block to register.
2138 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2140 int devfreq_register_notifier(struct devfreq *devfreq,
2141 struct notifier_block *nb,
2150 case DEVFREQ_TRANSITION_NOTIFIER:
2151 ret = srcu_notifier_chain_register(
2152 &devfreq->transition_notifier_list, nb);
2160 EXPORT_SYMBOL(devfreq_register_notifier);
2163 * devfreq_unregister_notifier() - Unregister a driver with devfreq
2164 * @devfreq: The devfreq object.
2165 * @nb: The notifier block to be unregistered.
2166 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2168 int devfreq_unregister_notifier(struct devfreq *devfreq,
2169 struct notifier_block *nb,
2178 case DEVFREQ_TRANSITION_NOTIFIER:
2179 ret = srcu_notifier_chain_unregister(
2180 &devfreq->transition_notifier_list, nb);
2188 EXPORT_SYMBOL(devfreq_unregister_notifier);
2190 struct devfreq_notifier_devres {
2191 struct devfreq *devfreq;
2192 struct notifier_block *nb;
2196 static void devm_devfreq_notifier_release(struct device *dev, void *res)
2198 struct devfreq_notifier_devres *this = res;
2200 devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
2204 * devm_devfreq_register_notifier()
2205 * - Resource-managed devfreq_register_notifier()
2206 * @dev: The devfreq user device. (parent of devfreq)
2207 * @devfreq: The devfreq object.
2208 * @nb: The notifier block to be unregistered.
2209 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2211 int devm_devfreq_register_notifier(struct device *dev,
2212 struct devfreq *devfreq,
2213 struct notifier_block *nb,
2216 struct devfreq_notifier_devres *ptr;
2219 ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
2224 ret = devfreq_register_notifier(devfreq, nb, list);
2230 ptr->devfreq = devfreq;
2233 devres_add(dev, ptr);
2237 EXPORT_SYMBOL(devm_devfreq_register_notifier);
2240 * devm_devfreq_unregister_notifier()
2241 * - Resource-managed devfreq_unregister_notifier()
2242 * @dev: The devfreq user device. (parent of devfreq)
2243 * @devfreq: The devfreq object.
2244 * @nb: The notifier block to be unregistered.
2245 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2247 void devm_devfreq_unregister_notifier(struct device *dev,
2248 struct devfreq *devfreq,
2249 struct notifier_block *nb,
2252 WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
2253 devm_devfreq_dev_match, devfreq));
2255 EXPORT_SYMBOL(devm_devfreq_unregister_notifier);