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->profile->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->profile->max_state - 1]) {
137 *min_freq = freq_table[0];
138 *max_freq = freq_table[devfreq->profile->max_state - 1];
140 *min_freq = freq_table[devfreq->profile->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->profile->max_state; lev++)
173 if (freq == devfreq->profile->freq_table[lev])
179 static int set_freq_table(struct devfreq *devfreq)
181 struct devfreq_dev_profile *profile = devfreq->profile;
182 struct dev_pm_opp *opp;
186 /* Initialize the freq_table from OPP table */
187 count = dev_pm_opp_get_opp_count(devfreq->dev.parent);
191 profile->max_state = count;
192 profile->freq_table = devm_kcalloc(devfreq->dev.parent,
194 sizeof(*profile->freq_table),
196 if (!profile->freq_table) {
197 profile->max_state = 0;
201 for (i = 0, freq = 0; i < profile->max_state; i++, freq++) {
202 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq);
204 devm_kfree(devfreq->dev.parent, profile->freq_table);
205 profile->max_state = 0;
209 profile->freq_table[i] = freq;
216 * devfreq_update_status() - Update statistics of devfreq behavior
217 * @devfreq: the devfreq instance
218 * @freq: the update target frequency
220 int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
222 int lev, prev_lev, ret = 0;
225 lockdep_assert_held(&devfreq->lock);
226 cur_time = get_jiffies_64();
228 /* Immediately exit if previous_freq is not initialized yet. */
229 if (!devfreq->previous_freq)
232 prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
238 devfreq->stats.time_in_state[prev_lev] +=
239 cur_time - devfreq->stats.last_update;
241 lev = devfreq_get_freq_level(devfreq, freq);
247 if (lev != prev_lev) {
248 devfreq->stats.trans_table[
249 (prev_lev * devfreq->profile->max_state) + lev]++;
250 devfreq->stats.total_trans++;
254 devfreq->stats.last_update = cur_time;
257 EXPORT_SYMBOL(devfreq_update_status);
260 * find_devfreq_governor() - find devfreq governor from name
261 * @name: name of the governor
263 * Search the list of devfreq governors and return the matched
264 * governor's pointer. devfreq_list_lock should be held by the caller.
266 static struct devfreq_governor *find_devfreq_governor(const char *name)
268 struct devfreq_governor *tmp_governor;
270 lockdep_assert_held(&devfreq_list_lock);
272 if (IS_ERR_OR_NULL(name)) {
273 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
274 return ERR_PTR(-EINVAL);
277 list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
278 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
282 return ERR_PTR(-ENODEV);
286 * try_then_request_governor() - Try to find the governor and request the
287 * module if is not found.
288 * @name: name of the governor
290 * Search the list of devfreq governors and request the module and try again
291 * if is not found. This can happen when both drivers (the governor driver
292 * and the driver that call devfreq_add_device) are built as modules.
293 * devfreq_list_lock should be held by the caller. Returns the matched
294 * governor's pointer or an error pointer.
296 static struct devfreq_governor *try_then_request_governor(const char *name)
298 struct devfreq_governor *governor;
301 lockdep_assert_held(&devfreq_list_lock);
303 if (IS_ERR_OR_NULL(name)) {
304 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
305 return ERR_PTR(-EINVAL);
308 governor = find_devfreq_governor(name);
309 if (IS_ERR(governor)) {
310 mutex_unlock(&devfreq_list_lock);
312 if (!strncmp(name, DEVFREQ_GOV_SIMPLE_ONDEMAND,
314 err = request_module("governor_%s", "simpleondemand");
316 err = request_module("governor_%s", name);
317 /* Restore previous state before return */
318 mutex_lock(&devfreq_list_lock);
320 return (err < 0) ? ERR_PTR(err) : ERR_PTR(-EINVAL);
322 governor = find_devfreq_governor(name);
328 static int devfreq_notify_transition(struct devfreq *devfreq,
329 struct devfreq_freqs *freqs, unsigned int state)
335 case DEVFREQ_PRECHANGE:
336 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
337 DEVFREQ_PRECHANGE, freqs);
340 case DEVFREQ_POSTCHANGE:
341 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
342 DEVFREQ_POSTCHANGE, freqs);
351 static int devfreq_set_target(struct devfreq *devfreq, unsigned long new_freq,
354 struct devfreq_freqs freqs;
355 unsigned long cur_freq;
358 if (devfreq->profile->get_cur_freq)
359 devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
361 cur_freq = devfreq->previous_freq;
363 freqs.old = cur_freq;
364 freqs.new = new_freq;
365 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
367 err = devfreq->profile->target(devfreq->dev.parent, &new_freq, flags);
369 freqs.new = cur_freq;
370 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
375 * Print devfreq_frequency trace information between DEVFREQ_PRECHANGE
376 * and DEVFREQ_POSTCHANGE because for showing the correct frequency
377 * change order of between devfreq device and passive devfreq device.
379 if (trace_devfreq_frequency_enabled() && new_freq != cur_freq)
380 trace_devfreq_frequency(devfreq, new_freq, cur_freq);
382 freqs.new = new_freq;
383 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
385 if (devfreq_update_status(devfreq, new_freq))
386 dev_warn(&devfreq->dev,
387 "Couldn't update frequency transition information.\n");
389 devfreq->previous_freq = new_freq;
391 if (devfreq->suspend_freq)
392 devfreq->resume_freq = new_freq;
398 * devfreq_update_target() - Reevaluate the device and configure frequency
399 * on the final stage.
400 * @devfreq: the devfreq instance.
401 * @freq: the new frequency of parent device. This argument
402 * is only used for devfreq device using passive governor.
404 * Note: Lock devfreq->lock before calling devfreq_update_target. This function
405 * should be only used by both update_devfreq() and devfreq governors.
407 int devfreq_update_target(struct devfreq *devfreq, unsigned long freq)
409 unsigned long min_freq, max_freq;
413 lockdep_assert_held(&devfreq->lock);
415 if (!devfreq->governor)
418 /* Reevaluate the proper frequency */
419 err = devfreq->governor->get_target_freq(devfreq, &freq);
422 devfreq_get_freq_range(devfreq, &min_freq, &max_freq);
424 if (freq < min_freq) {
426 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
428 if (freq > max_freq) {
430 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
433 return devfreq_set_target(devfreq, freq, flags);
435 EXPORT_SYMBOL(devfreq_update_target);
437 /* Load monitoring helper functions for governors use */
440 * update_devfreq() - Reevaluate the device and configure frequency.
441 * @devfreq: the devfreq instance.
443 * Note: Lock devfreq->lock before calling update_devfreq
444 * This function is exported for governors.
446 int update_devfreq(struct devfreq *devfreq)
448 return devfreq_update_target(devfreq, 0L);
450 EXPORT_SYMBOL(update_devfreq);
453 * devfreq_monitor() - Periodically poll devfreq objects.
454 * @work: the work struct used to run devfreq_monitor periodically.
457 static void devfreq_monitor(struct work_struct *work)
460 struct devfreq *devfreq = container_of(work,
461 struct devfreq, work.work);
463 mutex_lock(&devfreq->lock);
464 err = update_devfreq(devfreq);
466 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
468 queue_delayed_work(devfreq_wq, &devfreq->work,
469 msecs_to_jiffies(devfreq->profile->polling_ms));
470 mutex_unlock(&devfreq->lock);
472 trace_devfreq_monitor(devfreq);
476 * devfreq_monitor_start() - Start load monitoring of devfreq instance
477 * @devfreq: the devfreq instance.
479 * Helper function for starting devfreq device load monitoring. By
480 * default delayed work based monitoring is supported. Function
481 * to be called from governor in response to DEVFREQ_GOV_START
482 * event when device is added to devfreq framework.
484 void devfreq_monitor_start(struct devfreq *devfreq)
486 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
489 switch (devfreq->profile->timer) {
490 case DEVFREQ_TIMER_DEFERRABLE:
491 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
493 case DEVFREQ_TIMER_DELAYED:
494 INIT_DELAYED_WORK(&devfreq->work, devfreq_monitor);
500 if (devfreq->profile->polling_ms)
501 queue_delayed_work(devfreq_wq, &devfreq->work,
502 msecs_to_jiffies(devfreq->profile->polling_ms));
504 EXPORT_SYMBOL(devfreq_monitor_start);
507 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
508 * @devfreq: the devfreq instance.
510 * Helper function to stop devfreq device load monitoring. Function
511 * to be called from governor in response to DEVFREQ_GOV_STOP
512 * event when device is removed from devfreq framework.
514 void devfreq_monitor_stop(struct devfreq *devfreq)
516 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
519 cancel_delayed_work_sync(&devfreq->work);
521 EXPORT_SYMBOL(devfreq_monitor_stop);
524 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
525 * @devfreq: the devfreq instance.
527 * Helper function to suspend devfreq device load monitoring. Function
528 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
529 * event or when polling interval is set to zero.
531 * Note: Though this function is same as devfreq_monitor_stop(),
532 * intentionally kept separate to provide hooks for collecting
533 * transition statistics.
535 void devfreq_monitor_suspend(struct devfreq *devfreq)
537 mutex_lock(&devfreq->lock);
538 if (devfreq->stop_polling) {
539 mutex_unlock(&devfreq->lock);
543 devfreq_update_status(devfreq, devfreq->previous_freq);
544 devfreq->stop_polling = true;
545 mutex_unlock(&devfreq->lock);
547 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
550 cancel_delayed_work_sync(&devfreq->work);
552 EXPORT_SYMBOL(devfreq_monitor_suspend);
555 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
556 * @devfreq: the devfreq instance.
558 * Helper function to resume devfreq device load monitoring. Function
559 * to be called from governor in response to DEVFREQ_GOV_RESUME
560 * event or when polling interval is set to non-zero.
562 void devfreq_monitor_resume(struct devfreq *devfreq)
566 mutex_lock(&devfreq->lock);
568 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
571 if (!devfreq->stop_polling)
574 if (!delayed_work_pending(&devfreq->work) &&
575 devfreq->profile->polling_ms)
576 queue_delayed_work(devfreq_wq, &devfreq->work,
577 msecs_to_jiffies(devfreq->profile->polling_ms));
580 devfreq->stats.last_update = get_jiffies_64();
581 devfreq->stop_polling = false;
583 if (devfreq->profile->get_cur_freq &&
584 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
585 devfreq->previous_freq = freq;
588 mutex_unlock(&devfreq->lock);
590 EXPORT_SYMBOL(devfreq_monitor_resume);
593 * devfreq_update_interval() - Update device devfreq monitoring interval
594 * @devfreq: the devfreq instance.
595 * @delay: new polling interval to be set.
597 * Helper function to set new load monitoring polling interval. Function
598 * to be called from governor in response to DEVFREQ_GOV_UPDATE_INTERVAL event.
600 void devfreq_update_interval(struct devfreq *devfreq, unsigned int *delay)
602 unsigned int cur_delay = devfreq->profile->polling_ms;
603 unsigned int new_delay = *delay;
605 mutex_lock(&devfreq->lock);
606 devfreq->profile->polling_ms = new_delay;
608 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
611 if (devfreq->stop_polling)
614 /* if new delay is zero, stop polling */
616 mutex_unlock(&devfreq->lock);
617 cancel_delayed_work_sync(&devfreq->work);
621 /* if current delay is zero, start polling with new delay */
623 queue_delayed_work(devfreq_wq, &devfreq->work,
624 msecs_to_jiffies(devfreq->profile->polling_ms));
628 /* if current delay is greater than new delay, restart polling */
629 if (cur_delay > new_delay) {
630 mutex_unlock(&devfreq->lock);
631 cancel_delayed_work_sync(&devfreq->work);
632 mutex_lock(&devfreq->lock);
633 if (!devfreq->stop_polling)
634 queue_delayed_work(devfreq_wq, &devfreq->work,
635 msecs_to_jiffies(devfreq->profile->polling_ms));
638 mutex_unlock(&devfreq->lock);
640 EXPORT_SYMBOL(devfreq_update_interval);
643 * devfreq_notifier_call() - Notify that the device frequency requirements
644 * has been changed out of devfreq framework.
645 * @nb: the notifier_block (supposed to be devfreq->nb)
649 * Called by a notifier that uses devfreq->nb.
651 static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
654 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
657 mutex_lock(&devfreq->lock);
659 devfreq->scaling_min_freq = find_available_min_freq(devfreq);
660 if (!devfreq->scaling_min_freq)
663 devfreq->scaling_max_freq = find_available_max_freq(devfreq);
664 if (!devfreq->scaling_max_freq) {
665 devfreq->scaling_max_freq = ULONG_MAX;
669 err = update_devfreq(devfreq);
672 mutex_unlock(&devfreq->lock);
674 dev_err(devfreq->dev.parent,
675 "failed to update frequency from OPP notifier (%d)\n",
682 * qos_notifier_call() - Common handler for QoS constraints.
683 * @devfreq: the devfreq instance.
685 static int qos_notifier_call(struct devfreq *devfreq)
689 mutex_lock(&devfreq->lock);
690 err = update_devfreq(devfreq);
691 mutex_unlock(&devfreq->lock);
693 dev_err(devfreq->dev.parent,
694 "failed to update frequency from PM QoS (%d)\n",
701 * qos_min_notifier_call() - Callback for QoS min_freq changes.
702 * @nb: Should be devfreq->nb_min
704 static int qos_min_notifier_call(struct notifier_block *nb,
705 unsigned long val, void *ptr)
707 return qos_notifier_call(container_of(nb, struct devfreq, nb_min));
711 * qos_max_notifier_call() - Callback for QoS max_freq changes.
712 * @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);
840 devfreq->scaling_min_freq = find_available_min_freq(devfreq);
841 if (!devfreq->scaling_min_freq) {
842 mutex_unlock(&devfreq->lock);
847 devfreq->scaling_max_freq = find_available_max_freq(devfreq);
848 if (!devfreq->scaling_max_freq) {
849 mutex_unlock(&devfreq->lock);
854 devfreq_get_freq_range(devfreq, &min_freq, &max_freq);
856 devfreq->suspend_freq = dev_pm_opp_get_suspend_opp_freq(dev);
857 devfreq->opp_table = dev_pm_opp_get_opp_table(dev);
858 if (IS_ERR(devfreq->opp_table))
859 devfreq->opp_table = NULL;
861 atomic_set(&devfreq->suspend_count, 0);
863 dev_set_name(&devfreq->dev, "%s", dev_name(dev));
864 err = device_register(&devfreq->dev);
866 mutex_unlock(&devfreq->lock);
867 put_device(&devfreq->dev);
871 devfreq->stats.trans_table = devm_kzalloc(&devfreq->dev,
872 array3_size(sizeof(unsigned int),
873 devfreq->profile->max_state,
874 devfreq->profile->max_state),
876 if (!devfreq->stats.trans_table) {
877 mutex_unlock(&devfreq->lock);
882 devfreq->stats.time_in_state = devm_kcalloc(&devfreq->dev,
883 devfreq->profile->max_state,
884 sizeof(*devfreq->stats.time_in_state),
886 if (!devfreq->stats.time_in_state) {
887 mutex_unlock(&devfreq->lock);
892 devfreq->stats.total_trans = 0;
893 devfreq->stats.last_update = get_jiffies_64();
895 srcu_init_notifier_head(&devfreq->transition_notifier_list);
897 mutex_unlock(&devfreq->lock);
899 err = dev_pm_qos_add_request(dev, &devfreq->user_min_freq_req,
900 DEV_PM_QOS_MIN_FREQUENCY, 0);
903 err = dev_pm_qos_add_request(dev, &devfreq->user_max_freq_req,
904 DEV_PM_QOS_MAX_FREQUENCY,
905 PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE);
909 devfreq->nb_min.notifier_call = qos_min_notifier_call;
910 err = dev_pm_qos_add_notifier(dev, &devfreq->nb_min,
911 DEV_PM_QOS_MIN_FREQUENCY);
915 devfreq->nb_max.notifier_call = qos_max_notifier_call;
916 err = dev_pm_qos_add_notifier(dev, &devfreq->nb_max,
917 DEV_PM_QOS_MAX_FREQUENCY);
921 mutex_lock(&devfreq_list_lock);
923 governor = try_then_request_governor(governor_name);
924 if (IS_ERR(governor)) {
925 dev_err(dev, "%s: Unable to find governor for the device\n",
927 err = PTR_ERR(governor);
931 devfreq->governor = governor;
932 err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
935 dev_err(dev, "%s: Unable to start governor for the device\n",
939 create_sysfs_files(devfreq, devfreq->governor);
941 list_add(&devfreq->node, &devfreq_list);
943 mutex_unlock(&devfreq_list_lock);
945 if (devfreq->profile->is_cooling_device) {
946 devfreq->cdev = devfreq_cooling_em_register(devfreq, NULL);
947 if (IS_ERR(devfreq->cdev))
948 devfreq->cdev = NULL;
954 mutex_unlock(&devfreq_list_lock);
956 devfreq_remove_device(devfreq);
963 EXPORT_SYMBOL(devfreq_add_device);
966 * devfreq_remove_device() - Remove devfreq feature from a device.
967 * @devfreq: the devfreq instance to be removed
969 * The opposite of devfreq_add_device().
971 int devfreq_remove_device(struct devfreq *devfreq)
976 devfreq_cooling_unregister(devfreq->cdev);
978 if (devfreq->governor) {
979 devfreq->governor->event_handler(devfreq,
980 DEVFREQ_GOV_STOP, NULL);
981 remove_sysfs_files(devfreq, devfreq->governor);
984 device_unregister(&devfreq->dev);
988 EXPORT_SYMBOL(devfreq_remove_device);
990 static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
992 struct devfreq **r = res;
994 if (WARN_ON(!r || !*r))
1000 static void devm_devfreq_dev_release(struct device *dev, void *res)
1002 devfreq_remove_device(*(struct devfreq **)res);
1006 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
1007 * @dev: the device to add devfreq feature.
1008 * @profile: device-specific profile to run devfreq.
1009 * @governor_name: name of the policy to choose frequency.
1010 * @data: private data for the governor. The devfreq framework does not
1013 * This function manages automatically the memory of devfreq device using device
1014 * resource management and simplify the free operation for memory of devfreq
1017 struct devfreq *devm_devfreq_add_device(struct device *dev,
1018 struct devfreq_dev_profile *profile,
1019 const char *governor_name,
1022 struct devfreq **ptr, *devfreq;
1024 ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
1026 return ERR_PTR(-ENOMEM);
1028 devfreq = devfreq_add_device(dev, profile, governor_name, data);
1029 if (IS_ERR(devfreq)) {
1035 devres_add(dev, ptr);
1039 EXPORT_SYMBOL(devm_devfreq_add_device);
1043 * devfreq_get_devfreq_by_node - Get the devfreq device from devicetree
1044 * @node - pointer to device_node
1046 * return the instance of devfreq device
1048 struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node)
1050 struct devfreq *devfreq;
1053 return ERR_PTR(-EINVAL);
1055 mutex_lock(&devfreq_list_lock);
1056 list_for_each_entry(devfreq, &devfreq_list, node) {
1057 if (devfreq->dev.parent
1058 && devfreq->dev.parent->of_node == node) {
1059 mutex_unlock(&devfreq_list_lock);
1063 mutex_unlock(&devfreq_list_lock);
1065 return ERR_PTR(-ENODEV);
1069 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
1070 * @dev - instance to the given device
1071 * @phandle_name - name of property holding a phandle value
1072 * @index - index into list of devfreq
1074 * return the instance of devfreq device
1076 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
1077 const char *phandle_name, int index)
1079 struct device_node *node;
1080 struct devfreq *devfreq;
1082 if (!dev || !phandle_name)
1083 return ERR_PTR(-EINVAL);
1086 return ERR_PTR(-EINVAL);
1088 node = of_parse_phandle(dev->of_node, phandle_name, index);
1090 return ERR_PTR(-ENODEV);
1092 devfreq = devfreq_get_devfreq_by_node(node);
1099 struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node)
1101 return ERR_PTR(-ENODEV);
1104 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
1105 const char *phandle_name, int index)
1107 return ERR_PTR(-ENODEV);
1109 #endif /* CONFIG_OF */
1110 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_node);
1111 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
1114 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
1115 * @dev: the device from which to remove devfreq feature.
1116 * @devfreq: the devfreq instance to be removed
1118 void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
1120 WARN_ON(devres_release(dev, devm_devfreq_dev_release,
1121 devm_devfreq_dev_match, devfreq));
1123 EXPORT_SYMBOL(devm_devfreq_remove_device);
1126 * devfreq_suspend_device() - Suspend devfreq of a device.
1127 * @devfreq: the devfreq instance to be suspended
1129 * This function is intended to be called by the pm callbacks
1130 * (e.g., runtime_suspend, suspend) of the device driver that
1131 * holds the devfreq.
1133 int devfreq_suspend_device(struct devfreq *devfreq)
1140 if (atomic_inc_return(&devfreq->suspend_count) > 1)
1143 if (devfreq->governor) {
1144 ret = devfreq->governor->event_handler(devfreq,
1145 DEVFREQ_GOV_SUSPEND, NULL);
1150 if (devfreq->suspend_freq) {
1151 mutex_lock(&devfreq->lock);
1152 ret = devfreq_set_target(devfreq, devfreq->suspend_freq, 0);
1153 mutex_unlock(&devfreq->lock);
1160 EXPORT_SYMBOL(devfreq_suspend_device);
1163 * devfreq_resume_device() - Resume devfreq of a device.
1164 * @devfreq: the devfreq instance to be resumed
1166 * This function is intended to be called by the pm callbacks
1167 * (e.g., runtime_resume, resume) of the device driver that
1168 * holds the devfreq.
1170 int devfreq_resume_device(struct devfreq *devfreq)
1177 if (atomic_dec_return(&devfreq->suspend_count) >= 1)
1180 if (devfreq->resume_freq) {
1181 mutex_lock(&devfreq->lock);
1182 ret = devfreq_set_target(devfreq, devfreq->resume_freq, 0);
1183 mutex_unlock(&devfreq->lock);
1188 if (devfreq->governor) {
1189 ret = devfreq->governor->event_handler(devfreq,
1190 DEVFREQ_GOV_RESUME, NULL);
1197 EXPORT_SYMBOL(devfreq_resume_device);
1200 * devfreq_suspend() - Suspend devfreq governors and devices
1202 * Called during system wide Suspend/Hibernate cycles for suspending governors
1203 * and devices preserving the state for resume. On some platforms the devfreq
1204 * device must have precise state (frequency) after resume in order to provide
1205 * fully operating setup.
1207 void devfreq_suspend(void)
1209 struct devfreq *devfreq;
1212 mutex_lock(&devfreq_list_lock);
1213 list_for_each_entry(devfreq, &devfreq_list, node) {
1214 ret = devfreq_suspend_device(devfreq);
1216 dev_err(&devfreq->dev,
1217 "failed to suspend devfreq device\n");
1219 mutex_unlock(&devfreq_list_lock);
1223 * devfreq_resume() - Resume devfreq governors and devices
1225 * Called during system wide Suspend/Hibernate cycle for resuming governors and
1226 * devices that are suspended with devfreq_suspend().
1228 void devfreq_resume(void)
1230 struct devfreq *devfreq;
1233 mutex_lock(&devfreq_list_lock);
1234 list_for_each_entry(devfreq, &devfreq_list, node) {
1235 ret = devfreq_resume_device(devfreq);
1237 dev_warn(&devfreq->dev,
1238 "failed to resume devfreq device\n");
1240 mutex_unlock(&devfreq_list_lock);
1244 * devfreq_add_governor() - Add devfreq governor
1245 * @governor: the devfreq governor to be added
1247 int devfreq_add_governor(struct devfreq_governor *governor)
1249 struct devfreq_governor *g;
1250 struct devfreq *devfreq;
1254 pr_err("%s: Invalid parameters.\n", __func__);
1258 mutex_lock(&devfreq_list_lock);
1259 g = find_devfreq_governor(governor->name);
1261 pr_err("%s: governor %s already registered\n", __func__,
1267 list_add(&governor->node, &devfreq_governor_list);
1269 list_for_each_entry(devfreq, &devfreq_list, node) {
1271 struct device *dev = devfreq->dev.parent;
1273 if (!strncmp(devfreq->governor->name, governor->name,
1274 DEVFREQ_NAME_LEN)) {
1275 /* The following should never occur */
1276 if (devfreq->governor) {
1278 "%s: Governor %s already present\n",
1279 __func__, devfreq->governor->name);
1280 ret = devfreq->governor->event_handler(devfreq,
1281 DEVFREQ_GOV_STOP, NULL);
1284 "%s: Governor %s stop = %d\n",
1286 devfreq->governor->name, ret);
1290 devfreq->governor = governor;
1291 ret = devfreq->governor->event_handler(devfreq,
1292 DEVFREQ_GOV_START, NULL);
1294 dev_warn(dev, "%s: Governor %s start=%d\n",
1295 __func__, devfreq->governor->name,
1302 mutex_unlock(&devfreq_list_lock);
1306 EXPORT_SYMBOL(devfreq_add_governor);
1308 static void devm_devfreq_remove_governor(void *governor)
1310 WARN_ON(devfreq_remove_governor(governor));
1314 * devm_devfreq_add_governor() - Add devfreq governor
1315 * @dev: device which adds devfreq governor
1316 * @governor: the devfreq governor to be added
1318 * This is a resource-managed variant of devfreq_add_governor().
1320 int devm_devfreq_add_governor(struct device *dev,
1321 struct devfreq_governor *governor)
1325 err = devfreq_add_governor(governor);
1329 return devm_add_action_or_reset(dev, devm_devfreq_remove_governor,
1332 EXPORT_SYMBOL(devm_devfreq_add_governor);
1335 * devfreq_remove_governor() - Remove devfreq feature from a device.
1336 * @governor: the devfreq governor to be removed
1338 int devfreq_remove_governor(struct devfreq_governor *governor)
1340 struct devfreq_governor *g;
1341 struct devfreq *devfreq;
1345 pr_err("%s: Invalid parameters.\n", __func__);
1349 mutex_lock(&devfreq_list_lock);
1350 g = find_devfreq_governor(governor->name);
1352 pr_err("%s: governor %s not registered\n", __func__,
1357 list_for_each_entry(devfreq, &devfreq_list, node) {
1359 struct device *dev = devfreq->dev.parent;
1361 if (!strncmp(devfreq->governor->name, governor->name,
1362 DEVFREQ_NAME_LEN)) {
1363 /* we should have a devfreq governor! */
1364 if (!devfreq->governor) {
1365 dev_warn(dev, "%s: Governor %s NOT present\n",
1366 __func__, governor->name);
1370 ret = devfreq->governor->event_handler(devfreq,
1371 DEVFREQ_GOV_STOP, NULL);
1373 dev_warn(dev, "%s: Governor %s stop=%d\n",
1374 __func__, devfreq->governor->name,
1377 devfreq->governor = NULL;
1381 list_del(&governor->node);
1383 mutex_unlock(&devfreq_list_lock);
1387 EXPORT_SYMBOL(devfreq_remove_governor);
1389 static ssize_t name_show(struct device *dev,
1390 struct device_attribute *attr, char *buf)
1392 struct devfreq *df = to_devfreq(dev);
1393 return sprintf(buf, "%s\n", dev_name(df->dev.parent));
1395 static DEVICE_ATTR_RO(name);
1397 static ssize_t governor_show(struct device *dev,
1398 struct device_attribute *attr, char *buf)
1400 struct devfreq *df = to_devfreq(dev);
1405 return sprintf(buf, "%s\n", df->governor->name);
1408 static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
1409 const char *buf, size_t count)
1411 struct devfreq *df = to_devfreq(dev);
1413 char str_governor[DEVFREQ_NAME_LEN + 1];
1414 const struct devfreq_governor *governor, *prev_governor;
1419 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
1423 mutex_lock(&devfreq_list_lock);
1424 governor = try_then_request_governor(str_governor);
1425 if (IS_ERR(governor)) {
1426 ret = PTR_ERR(governor);
1429 if (df->governor == governor) {
1432 } else if (IS_SUPPORTED_FLAG(df->governor->flags, IMMUTABLE)
1433 || IS_SUPPORTED_FLAG(governor->flags, IMMUTABLE)) {
1439 * Stop the current governor and remove the specific sysfs files
1440 * which depend on current governor.
1442 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1444 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1445 __func__, df->governor->name, ret);
1448 remove_sysfs_files(df, df->governor);
1451 * Start the new governor and create the specific sysfs files
1452 * which depend on the new governor.
1454 prev_governor = df->governor;
1455 df->governor = governor;
1456 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1458 dev_warn(dev, "%s: Governor %s not started(%d)\n",
1459 __func__, df->governor->name, ret);
1461 /* Restore previous governor */
1462 df->governor = prev_governor;
1463 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1466 "%s: reverting to Governor %s failed (%d)\n",
1467 __func__, prev_governor->name, ret);
1468 df->governor = NULL;
1474 * Create the sysfs files for the new governor. But if failed to start
1475 * the new governor, restore the sysfs files of previous governor.
1477 create_sysfs_files(df, df->governor);
1480 mutex_unlock(&devfreq_list_lock);
1486 static DEVICE_ATTR_RW(governor);
1488 static ssize_t available_governors_show(struct device *d,
1489 struct device_attribute *attr,
1492 struct devfreq *df = to_devfreq(d);
1498 mutex_lock(&devfreq_list_lock);
1501 * The devfreq with immutable governor (e.g., passive) shows
1502 * only own governor.
1504 if (IS_SUPPORTED_FLAG(df->governor->flags, IMMUTABLE)) {
1505 count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
1506 "%s ", df->governor->name);
1508 * The devfreq device shows the registered governor except for
1509 * immutable governors such as passive governor .
1512 struct devfreq_governor *governor;
1514 list_for_each_entry(governor, &devfreq_governor_list, node) {
1515 if (IS_SUPPORTED_FLAG(governor->flags, IMMUTABLE))
1517 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1518 "%s ", governor->name);
1522 mutex_unlock(&devfreq_list_lock);
1524 /* Truncate the trailing space */
1528 count += sprintf(&buf[count], "\n");
1532 static DEVICE_ATTR_RO(available_governors);
1534 static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
1538 struct devfreq *df = to_devfreq(dev);
1543 if (df->profile->get_cur_freq &&
1544 !df->profile->get_cur_freq(df->dev.parent, &freq))
1545 return sprintf(buf, "%lu\n", freq);
1547 return sprintf(buf, "%lu\n", df->previous_freq);
1549 static DEVICE_ATTR_RO(cur_freq);
1551 static ssize_t target_freq_show(struct device *dev,
1552 struct device_attribute *attr, char *buf)
1554 struct devfreq *df = to_devfreq(dev);
1556 return sprintf(buf, "%lu\n", df->previous_freq);
1558 static DEVICE_ATTR_RO(target_freq);
1560 static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
1561 const char *buf, size_t count)
1563 struct devfreq *df = to_devfreq(dev);
1564 unsigned long value;
1568 * Protect against theoretical sysfs writes between
1569 * device_add and dev_pm_qos_add_request
1571 if (!dev_pm_qos_request_active(&df->user_min_freq_req))
1574 ret = sscanf(buf, "%lu", &value);
1578 /* Round down to kHz for PM QoS */
1579 ret = dev_pm_qos_update_request(&df->user_min_freq_req,
1580 value / HZ_PER_KHZ);
1587 static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
1590 struct devfreq *df = to_devfreq(dev);
1591 unsigned long min_freq, max_freq;
1593 mutex_lock(&df->lock);
1594 devfreq_get_freq_range(df, &min_freq, &max_freq);
1595 mutex_unlock(&df->lock);
1597 return sprintf(buf, "%lu\n", min_freq);
1599 static DEVICE_ATTR_RW(min_freq);
1601 static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
1602 const char *buf, size_t count)
1604 struct devfreq *df = to_devfreq(dev);
1605 unsigned long value;
1609 * Protect against theoretical sysfs writes between
1610 * device_add and dev_pm_qos_add_request
1612 if (!dev_pm_qos_request_active(&df->user_max_freq_req))
1615 ret = sscanf(buf, "%lu", &value);
1620 * PM QoS frequencies are in kHz so we need to convert. Convert by
1621 * rounding upwards so that the acceptable interval never shrinks.
1623 * For example if the user writes "666666666" to sysfs this value will
1624 * be converted to 666667 kHz and back to 666667000 Hz before an OPP
1625 * lookup, this ensures that an OPP of 666666666Hz is still accepted.
1627 * A value of zero means "no limit".
1630 value = DIV_ROUND_UP(value, HZ_PER_KHZ);
1632 value = PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE;
1634 ret = dev_pm_qos_update_request(&df->user_max_freq_req, value);
1641 static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
1644 struct devfreq *df = to_devfreq(dev);
1645 unsigned long min_freq, max_freq;
1647 mutex_lock(&df->lock);
1648 devfreq_get_freq_range(df, &min_freq, &max_freq);
1649 mutex_unlock(&df->lock);
1651 return sprintf(buf, "%lu\n", max_freq);
1653 static DEVICE_ATTR_RW(max_freq);
1655 static ssize_t available_frequencies_show(struct device *d,
1656 struct device_attribute *attr,
1659 struct devfreq *df = to_devfreq(d);
1666 mutex_lock(&df->lock);
1668 for (i = 0; i < df->profile->max_state; i++)
1669 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1670 "%lu ", df->profile->freq_table[i]);
1672 mutex_unlock(&df->lock);
1673 /* Truncate the trailing space */
1677 count += sprintf(&buf[count], "\n");
1681 static DEVICE_ATTR_RO(available_frequencies);
1683 static ssize_t trans_stat_show(struct device *dev,
1684 struct device_attribute *attr, char *buf)
1686 struct devfreq *df = to_devfreq(dev);
1689 unsigned int max_state;
1693 max_state = df->profile->max_state;
1696 return sprintf(buf, "Not Supported.\n");
1698 mutex_lock(&df->lock);
1699 if (!df->stop_polling &&
1700 devfreq_update_status(df, df->previous_freq)) {
1701 mutex_unlock(&df->lock);
1704 mutex_unlock(&df->lock);
1706 len = sprintf(buf, " From : To\n");
1707 len += sprintf(buf + len, " :");
1708 for (i = 0; i < max_state; i++)
1709 len += sprintf(buf + len, "%10lu",
1710 df->profile->freq_table[i]);
1712 len += sprintf(buf + len, " time(ms)\n");
1714 for (i = 0; i < max_state; i++) {
1715 if (df->profile->freq_table[i]
1716 == df->previous_freq) {
1717 len += sprintf(buf + len, "*");
1719 len += sprintf(buf + len, " ");
1721 len += sprintf(buf + len, "%10lu:",
1722 df->profile->freq_table[i]);
1723 for (j = 0; j < max_state; j++)
1724 len += sprintf(buf + len, "%10u",
1725 df->stats.trans_table[(i * max_state) + j]);
1727 len += sprintf(buf + len, "%10llu\n", (u64)
1728 jiffies64_to_msecs(df->stats.time_in_state[i]));
1731 len += sprintf(buf + len, "Total transition : %u\n",
1732 df->stats.total_trans);
1736 static ssize_t trans_stat_store(struct device *dev,
1737 struct device_attribute *attr,
1738 const char *buf, size_t count)
1740 struct devfreq *df = to_devfreq(dev);
1746 if (df->profile->max_state == 0)
1749 err = kstrtoint(buf, 10, &value);
1750 if (err || value != 0)
1753 mutex_lock(&df->lock);
1754 memset(df->stats.time_in_state, 0, (df->profile->max_state *
1755 sizeof(*df->stats.time_in_state)));
1756 memset(df->stats.trans_table, 0, array3_size(sizeof(unsigned int),
1757 df->profile->max_state,
1758 df->profile->max_state));
1759 df->stats.total_trans = 0;
1760 df->stats.last_update = get_jiffies_64();
1761 mutex_unlock(&df->lock);
1765 static DEVICE_ATTR_RW(trans_stat);
1767 static struct attribute *devfreq_attrs[] = {
1768 &dev_attr_name.attr,
1769 &dev_attr_governor.attr,
1770 &dev_attr_available_governors.attr,
1771 &dev_attr_cur_freq.attr,
1772 &dev_attr_available_frequencies.attr,
1773 &dev_attr_target_freq.attr,
1774 &dev_attr_min_freq.attr,
1775 &dev_attr_max_freq.attr,
1776 &dev_attr_trans_stat.attr,
1779 ATTRIBUTE_GROUPS(devfreq);
1781 static ssize_t polling_interval_show(struct device *dev,
1782 struct device_attribute *attr, char *buf)
1784 struct devfreq *df = to_devfreq(dev);
1789 return sprintf(buf, "%d\n", df->profile->polling_ms);
1792 static ssize_t polling_interval_store(struct device *dev,
1793 struct device_attribute *attr,
1794 const char *buf, size_t count)
1796 struct devfreq *df = to_devfreq(dev);
1803 ret = sscanf(buf, "%u", &value);
1807 df->governor->event_handler(df, DEVFREQ_GOV_UPDATE_INTERVAL, &value);
1812 static DEVICE_ATTR_RW(polling_interval);
1814 static ssize_t timer_show(struct device *dev,
1815 struct device_attribute *attr, char *buf)
1817 struct devfreq *df = to_devfreq(dev);
1822 return sprintf(buf, "%s\n", timer_name[df->profile->timer]);
1825 static ssize_t timer_store(struct device *dev, struct device_attribute *attr,
1826 const char *buf, size_t count)
1828 struct devfreq *df = to_devfreq(dev);
1829 char str_timer[DEVFREQ_NAME_LEN + 1];
1833 if (!df->governor || !df->profile)
1836 ret = sscanf(buf, "%16s", str_timer);
1840 for (i = 0; i < DEVFREQ_TIMER_NUM; i++) {
1841 if (!strncmp(timer_name[i], str_timer, DEVFREQ_NAME_LEN)) {
1852 if (df->profile->timer == timer) {
1857 mutex_lock(&df->lock);
1858 df->profile->timer = timer;
1859 mutex_unlock(&df->lock);
1861 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1863 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1864 __func__, df->governor->name, ret);
1868 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1870 dev_warn(dev, "%s: Governor %s not started(%d)\n",
1871 __func__, df->governor->name, ret);
1873 return ret ? ret : count;
1875 static DEVICE_ATTR_RW(timer);
1877 #define CREATE_SYSFS_FILE(df, name) \
1880 ret = sysfs_create_file(&df->dev.kobj, &dev_attr_##name.attr); \
1882 dev_warn(&df->dev, \
1883 "Unable to create attr(%s)\n", "##name"); \
1887 /* Create the specific sysfs files which depend on each governor. */
1888 static void create_sysfs_files(struct devfreq *devfreq,
1889 const struct devfreq_governor *gov)
1891 if (IS_SUPPORTED_ATTR(gov->attrs, POLLING_INTERVAL))
1892 CREATE_SYSFS_FILE(devfreq, polling_interval);
1893 if (IS_SUPPORTED_ATTR(gov->attrs, TIMER))
1894 CREATE_SYSFS_FILE(devfreq, timer);
1897 /* Remove the specific sysfs files which depend on each governor. */
1898 static void remove_sysfs_files(struct devfreq *devfreq,
1899 const struct devfreq_governor *gov)
1901 if (IS_SUPPORTED_ATTR(gov->attrs, POLLING_INTERVAL))
1902 sysfs_remove_file(&devfreq->dev.kobj,
1903 &dev_attr_polling_interval.attr);
1904 if (IS_SUPPORTED_ATTR(gov->attrs, TIMER))
1905 sysfs_remove_file(&devfreq->dev.kobj, &dev_attr_timer.attr);
1909 * devfreq_summary_show() - Show the summary of the devfreq devices
1910 * @s: seq_file instance to show the summary of devfreq devices
1913 * Show the summary of the devfreq devices via 'devfreq_summary' debugfs file.
1914 * It helps that user can know the detailed information of the devfreq devices.
1916 * Return 0 always because it shows the information without any data change.
1918 static int devfreq_summary_show(struct seq_file *s, void *data)
1920 struct devfreq *devfreq;
1921 struct devfreq *p_devfreq = NULL;
1922 unsigned long cur_freq, min_freq, max_freq;
1923 unsigned int polling_ms;
1926 seq_printf(s, "%-30s %-30s %-15s %-10s %10s %12s %12s %12s\n",
1935 seq_printf(s, "%30s %30s %15s %10s %10s %12s %12s %12s\n",
1936 "------------------------------",
1937 "------------------------------",
1945 mutex_lock(&devfreq_list_lock);
1947 list_for_each_entry_reverse(devfreq, &devfreq_list, node) {
1948 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE)
1949 if (!strncmp(devfreq->governor->name, DEVFREQ_GOV_PASSIVE,
1950 DEVFREQ_NAME_LEN)) {
1951 struct devfreq_passive_data *data = devfreq->data;
1954 p_devfreq = data->parent;
1960 mutex_lock(&devfreq->lock);
1961 cur_freq = devfreq->previous_freq;
1962 devfreq_get_freq_range(devfreq, &min_freq, &max_freq);
1963 timer = devfreq->profile->timer;
1965 if (IS_SUPPORTED_ATTR(devfreq->governor->attrs, POLLING_INTERVAL))
1966 polling_ms = devfreq->profile->polling_ms;
1969 mutex_unlock(&devfreq->lock);
1972 "%-30s %-30s %-15s %-10s %10d %12ld %12ld %12ld\n",
1973 dev_name(&devfreq->dev),
1974 p_devfreq ? dev_name(&p_devfreq->dev) : "null",
1975 devfreq->governor->name,
1976 polling_ms ? timer_name[timer] : "null",
1983 mutex_unlock(&devfreq_list_lock);
1987 DEFINE_SHOW_ATTRIBUTE(devfreq_summary);
1989 static int __init devfreq_init(void)
1991 devfreq_class = class_create(THIS_MODULE, "devfreq");
1992 if (IS_ERR(devfreq_class)) {
1993 pr_err("%s: couldn't create class\n", __FILE__);
1994 return PTR_ERR(devfreq_class);
1997 devfreq_wq = create_freezable_workqueue("devfreq_wq");
1999 class_destroy(devfreq_class);
2000 pr_err("%s: couldn't create workqueue\n", __FILE__);
2003 devfreq_class->dev_groups = devfreq_groups;
2005 devfreq_debugfs = debugfs_create_dir("devfreq", NULL);
2006 debugfs_create_file("devfreq_summary", 0444,
2007 devfreq_debugfs, NULL,
2008 &devfreq_summary_fops);
2012 subsys_initcall(devfreq_init);
2015 * The following are helper functions for devfreq user device drivers with
2020 * devfreq_recommended_opp() - Helper function to get proper OPP for the
2021 * freq value given to target callback.
2022 * @dev: The devfreq user device. (parent of devfreq)
2023 * @freq: The frequency given to target function
2024 * @flags: Flags handed from devfreq framework.
2026 * The callers are required to call dev_pm_opp_put() for the returned OPP after
2029 struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
2030 unsigned long *freq,
2033 struct dev_pm_opp *opp;
2035 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
2036 /* The freq is an upper bound. opp should be lower */
2037 opp = dev_pm_opp_find_freq_floor(dev, freq);
2039 /* If not available, use the closest opp */
2040 if (opp == ERR_PTR(-ERANGE))
2041 opp = dev_pm_opp_find_freq_ceil(dev, freq);
2043 /* The freq is an lower bound. opp should be higher */
2044 opp = dev_pm_opp_find_freq_ceil(dev, freq);
2046 /* If not available, use the closest opp */
2047 if (opp == ERR_PTR(-ERANGE))
2048 opp = dev_pm_opp_find_freq_floor(dev, freq);
2053 EXPORT_SYMBOL(devfreq_recommended_opp);
2056 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
2057 * for any changes in the OPP availability
2059 * @dev: The devfreq user device. (parent of devfreq)
2060 * @devfreq: The devfreq object.
2062 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
2064 return dev_pm_opp_register_notifier(dev, &devfreq->nb);
2066 EXPORT_SYMBOL(devfreq_register_opp_notifier);
2069 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
2070 * notified for any changes in the OPP
2071 * availability changes anymore.
2072 * @dev: The devfreq user device. (parent of devfreq)
2073 * @devfreq: The devfreq object.
2075 * At exit() callback of devfreq_dev_profile, this must be included if
2076 * devfreq_recommended_opp is used.
2078 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
2080 return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
2082 EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
2084 static void devm_devfreq_opp_release(struct device *dev, void *res)
2086 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
2090 * devm_devfreq_register_opp_notifier() - Resource-managed
2091 * devfreq_register_opp_notifier()
2092 * @dev: The devfreq user device. (parent of devfreq)
2093 * @devfreq: The devfreq object.
2095 int devm_devfreq_register_opp_notifier(struct device *dev,
2096 struct devfreq *devfreq)
2098 struct devfreq **ptr;
2101 ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
2105 ret = devfreq_register_opp_notifier(dev, devfreq);
2112 devres_add(dev, ptr);
2116 EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
2119 * devm_devfreq_unregister_opp_notifier() - Resource-managed
2120 * devfreq_unregister_opp_notifier()
2121 * @dev: The devfreq user device. (parent of devfreq)
2122 * @devfreq: The devfreq object.
2124 void devm_devfreq_unregister_opp_notifier(struct device *dev,
2125 struct devfreq *devfreq)
2127 WARN_ON(devres_release(dev, devm_devfreq_opp_release,
2128 devm_devfreq_dev_match, devfreq));
2130 EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
2133 * devfreq_register_notifier() - Register a driver with devfreq
2134 * @devfreq: The devfreq object.
2135 * @nb: The notifier block to register.
2136 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2138 int devfreq_register_notifier(struct devfreq *devfreq,
2139 struct notifier_block *nb,
2148 case DEVFREQ_TRANSITION_NOTIFIER:
2149 ret = srcu_notifier_chain_register(
2150 &devfreq->transition_notifier_list, nb);
2158 EXPORT_SYMBOL(devfreq_register_notifier);
2161 * devfreq_unregister_notifier() - Unregister a driver with devfreq
2162 * @devfreq: The devfreq object.
2163 * @nb: The notifier block to be unregistered.
2164 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2166 int devfreq_unregister_notifier(struct devfreq *devfreq,
2167 struct notifier_block *nb,
2176 case DEVFREQ_TRANSITION_NOTIFIER:
2177 ret = srcu_notifier_chain_unregister(
2178 &devfreq->transition_notifier_list, nb);
2186 EXPORT_SYMBOL(devfreq_unregister_notifier);
2188 struct devfreq_notifier_devres {
2189 struct devfreq *devfreq;
2190 struct notifier_block *nb;
2194 static void devm_devfreq_notifier_release(struct device *dev, void *res)
2196 struct devfreq_notifier_devres *this = res;
2198 devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
2202 * devm_devfreq_register_notifier()
2203 * - Resource-managed devfreq_register_notifier()
2204 * @dev: The devfreq user device. (parent of devfreq)
2205 * @devfreq: The devfreq object.
2206 * @nb: The notifier block to be unregistered.
2207 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2209 int devm_devfreq_register_notifier(struct device *dev,
2210 struct devfreq *devfreq,
2211 struct notifier_block *nb,
2214 struct devfreq_notifier_devres *ptr;
2217 ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
2222 ret = devfreq_register_notifier(devfreq, nb, list);
2228 ptr->devfreq = devfreq;
2231 devres_add(dev, ptr);
2235 EXPORT_SYMBOL(devm_devfreq_register_notifier);
2238 * devm_devfreq_unregister_notifier()
2239 * - Resource-managed devfreq_unregister_notifier()
2240 * @dev: The devfreq user device. (parent of devfreq)
2241 * @devfreq: The devfreq object.
2242 * @nb: The notifier block to be unregistered.
2243 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2245 void devm_devfreq_unregister_notifier(struct device *dev,
2246 struct devfreq *devfreq,
2247 struct notifier_block *nb,
2250 WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
2251 devm_devfreq_dev_match, devfreq));
2253 EXPORT_SYMBOL(devm_devfreq_unregister_notifier);