1 // SPDX-License-Identifier: GPL-2.0-only
3 * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
6 * Copyright (C) 2011 Samsung Electronics
10 #include <linux/kernel.h>
11 #include <linux/kmod.h>
12 #include <linux/sched.h>
13 #include <linux/debugfs.h>
14 #include <linux/errno.h>
15 #include <linux/err.h>
16 #include <linux/init.h>
17 #include <linux/export.h>
18 #include <linux/slab.h>
19 #include <linux/stat.h>
20 #include <linux/pm_opp.h>
21 #include <linux/devfreq.h>
22 #include <linux/workqueue.h>
23 #include <linux/platform_device.h>
24 #include <linux/list.h>
25 #include <linux/printk.h>
26 #include <linux/hrtimer.h>
28 #include <linux/pm_qos.h>
31 #define CREATE_TRACE_POINTS
32 #include <trace/events/devfreq.h>
34 #define IS_SUPPORTED_FLAG(f, name) ((f & DEVFREQ_GOV_FLAG_##name) ? true : false)
35 #define IS_SUPPORTED_ATTR(f, name) ((f & DEVFREQ_GOV_ATTR_##name) ? true : false)
36 #define HZ_PER_KHZ 1000
38 static struct class *devfreq_class;
39 static struct dentry *devfreq_debugfs;
42 * devfreq core provides delayed work based load monitoring helper
43 * functions. Governors can use these or can implement their own
44 * monitoring mechanism.
46 static struct workqueue_struct *devfreq_wq;
48 /* The list of all device-devfreq governors */
49 static LIST_HEAD(devfreq_governor_list);
50 /* The list of all device-devfreq */
51 static LIST_HEAD(devfreq_list);
52 static DEFINE_MUTEX(devfreq_list_lock);
54 static const char timer_name[][DEVFREQ_NAME_LEN] = {
55 [DEVFREQ_TIMER_DEFERRABLE] = { "deferrable" },
56 [DEVFREQ_TIMER_DELAYED] = { "delayed" },
60 * find_device_devfreq() - find devfreq struct using device pointer
61 * @dev: device pointer used to lookup device devfreq.
63 * Search the list of device devfreqs and return the matched device's
64 * devfreq info. devfreq_list_lock should be held by the caller.
66 static struct devfreq *find_device_devfreq(struct device *dev)
68 struct devfreq *tmp_devfreq;
70 lockdep_assert_held(&devfreq_list_lock);
72 if (IS_ERR_OR_NULL(dev)) {
73 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
74 return ERR_PTR(-EINVAL);
77 list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
78 if (tmp_devfreq->dev.parent == dev)
82 return ERR_PTR(-ENODEV);
85 static unsigned long find_available_min_freq(struct devfreq *devfreq)
87 struct dev_pm_opp *opp;
88 unsigned long min_freq = 0;
90 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &min_freq);
99 static unsigned long find_available_max_freq(struct devfreq *devfreq)
101 struct dev_pm_opp *opp;
102 unsigned long max_freq = ULONG_MAX;
104 opp = dev_pm_opp_find_freq_floor(devfreq->dev.parent, &max_freq);
114 * get_freq_range() - Get the current freq range
115 * @devfreq: the devfreq instance
116 * @min_freq: the min frequency
117 * @max_freq: the max frequency
119 * This takes into consideration all constraints.
121 static void get_freq_range(struct devfreq *devfreq,
122 unsigned long *min_freq,
123 unsigned long *max_freq)
125 unsigned long *freq_table = devfreq->profile->freq_table;
126 s32 qos_min_freq, qos_max_freq;
128 lockdep_assert_held(&devfreq->lock);
131 * Initialize minimum/maximum frequency from freq table.
132 * The devfreq drivers can initialize this in either ascending or
133 * descending order and devfreq core supports both.
135 if (freq_table[0] < freq_table[devfreq->profile->max_state - 1]) {
136 *min_freq = freq_table[0];
137 *max_freq = freq_table[devfreq->profile->max_state - 1];
139 *min_freq = freq_table[devfreq->profile->max_state - 1];
140 *max_freq = freq_table[0];
143 /* Apply constraints from PM QoS */
144 qos_min_freq = dev_pm_qos_read_value(devfreq->dev.parent,
145 DEV_PM_QOS_MIN_FREQUENCY);
146 qos_max_freq = dev_pm_qos_read_value(devfreq->dev.parent,
147 DEV_PM_QOS_MAX_FREQUENCY);
148 *min_freq = max(*min_freq, (unsigned long)HZ_PER_KHZ * qos_min_freq);
149 if (qos_max_freq != PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE)
150 *max_freq = min(*max_freq,
151 (unsigned long)HZ_PER_KHZ * qos_max_freq);
153 /* Apply constraints from OPP interface */
154 *min_freq = max(*min_freq, devfreq->scaling_min_freq);
155 *max_freq = min(*max_freq, devfreq->scaling_max_freq);
157 if (*min_freq > *max_freq)
158 *min_freq = *max_freq;
162 * devfreq_get_freq_level() - Lookup freq_table for the frequency
163 * @devfreq: the devfreq instance
164 * @freq: the target frequency
166 static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq)
170 for (lev = 0; lev < devfreq->profile->max_state; lev++)
171 if (freq == devfreq->profile->freq_table[lev])
177 static int set_freq_table(struct devfreq *devfreq)
179 struct devfreq_dev_profile *profile = devfreq->profile;
180 struct dev_pm_opp *opp;
184 /* Initialize the freq_table from OPP table */
185 count = dev_pm_opp_get_opp_count(devfreq->dev.parent);
189 profile->max_state = count;
190 profile->freq_table = devm_kcalloc(devfreq->dev.parent,
192 sizeof(*profile->freq_table),
194 if (!profile->freq_table) {
195 profile->max_state = 0;
199 for (i = 0, freq = 0; i < profile->max_state; i++, freq++) {
200 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq);
202 devm_kfree(devfreq->dev.parent, profile->freq_table);
203 profile->max_state = 0;
207 profile->freq_table[i] = freq;
214 * devfreq_update_status() - Update statistics of devfreq behavior
215 * @devfreq: the devfreq instance
216 * @freq: the update target frequency
218 int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
220 int lev, prev_lev, ret = 0;
223 lockdep_assert_held(&devfreq->lock);
224 cur_time = get_jiffies_64();
226 /* Immediately exit if previous_freq is not initialized yet. */
227 if (!devfreq->previous_freq)
230 prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
236 devfreq->stats.time_in_state[prev_lev] +=
237 cur_time - devfreq->stats.last_update;
239 lev = devfreq_get_freq_level(devfreq, freq);
245 if (lev != prev_lev) {
246 devfreq->stats.trans_table[
247 (prev_lev * devfreq->profile->max_state) + lev]++;
248 devfreq->stats.total_trans++;
252 devfreq->stats.last_update = cur_time;
255 EXPORT_SYMBOL(devfreq_update_status);
258 * find_devfreq_governor() - find devfreq governor from name
259 * @name: name of the governor
261 * Search the list of devfreq governors and return the matched
262 * governor's pointer. devfreq_list_lock should be held by the caller.
264 static struct devfreq_governor *find_devfreq_governor(const char *name)
266 struct devfreq_governor *tmp_governor;
268 lockdep_assert_held(&devfreq_list_lock);
270 if (IS_ERR_OR_NULL(name)) {
271 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
272 return ERR_PTR(-EINVAL);
275 list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
276 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
280 return ERR_PTR(-ENODEV);
284 * try_then_request_governor() - Try to find the governor and request the
285 * module if is not found.
286 * @name: name of the governor
288 * Search the list of devfreq governors and request the module and try again
289 * if is not found. This can happen when both drivers (the governor driver
290 * and the driver that call devfreq_add_device) are built as modules.
291 * devfreq_list_lock should be held by the caller. Returns the matched
292 * governor's pointer or an error pointer.
294 static struct devfreq_governor *try_then_request_governor(const char *name)
296 struct devfreq_governor *governor;
299 lockdep_assert_held(&devfreq_list_lock);
301 if (IS_ERR_OR_NULL(name)) {
302 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
303 return ERR_PTR(-EINVAL);
306 governor = find_devfreq_governor(name);
307 if (IS_ERR(governor)) {
308 mutex_unlock(&devfreq_list_lock);
310 if (!strncmp(name, DEVFREQ_GOV_SIMPLE_ONDEMAND,
312 err = request_module("governor_%s", "simpleondemand");
314 err = request_module("governor_%s", name);
315 /* Restore previous state before return */
316 mutex_lock(&devfreq_list_lock);
318 return (err < 0) ? ERR_PTR(err) : ERR_PTR(-EINVAL);
320 governor = find_devfreq_governor(name);
326 static int devfreq_notify_transition(struct devfreq *devfreq,
327 struct devfreq_freqs *freqs, unsigned int state)
333 case DEVFREQ_PRECHANGE:
334 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
335 DEVFREQ_PRECHANGE, freqs);
338 case DEVFREQ_POSTCHANGE:
339 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
340 DEVFREQ_POSTCHANGE, freqs);
349 static int devfreq_set_target(struct devfreq *devfreq, unsigned long new_freq,
352 struct devfreq_freqs freqs;
353 unsigned long cur_freq;
356 if (devfreq->profile->get_cur_freq)
357 devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
359 cur_freq = devfreq->previous_freq;
361 freqs.old = cur_freq;
362 freqs.new = new_freq;
363 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
365 err = devfreq->profile->target(devfreq->dev.parent, &new_freq, flags);
367 freqs.new = cur_freq;
368 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
373 * Print devfreq_frequency trace information between DEVFREQ_PRECHANGE
374 * and DEVFREQ_POSTCHANGE because for showing the correct frequency
375 * change order of between devfreq device and passive devfreq device.
377 if (trace_devfreq_frequency_enabled() && new_freq != cur_freq)
378 trace_devfreq_frequency(devfreq, new_freq, cur_freq);
380 freqs.new = new_freq;
381 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
383 if (devfreq_update_status(devfreq, new_freq))
384 dev_err(&devfreq->dev,
385 "Couldn't update frequency transition information.\n");
387 devfreq->previous_freq = new_freq;
389 if (devfreq->suspend_freq)
390 devfreq->resume_freq = cur_freq;
396 * devfreq_update_target() - Reevaluate the device and configure frequency
397 * on the final stage.
398 * @devfreq: the devfreq instance.
399 * @freq: the new frequency of parent device. This argument
400 * is only used for devfreq device using passive governor.
402 * Note: Lock devfreq->lock before calling devfreq_update_target. This function
403 * should be only used by both update_devfreq() and devfreq governors.
405 int devfreq_update_target(struct devfreq *devfreq, unsigned long freq)
407 unsigned long min_freq, max_freq;
411 lockdep_assert_held(&devfreq->lock);
413 if (!devfreq->governor)
416 /* Reevaluate the proper frequency */
417 err = devfreq->governor->get_target_freq(devfreq, &freq);
420 get_freq_range(devfreq, &min_freq, &max_freq);
422 if (freq < min_freq) {
424 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
426 if (freq > max_freq) {
428 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
431 return devfreq_set_target(devfreq, freq, flags);
433 EXPORT_SYMBOL(devfreq_update_target);
435 /* Load monitoring helper functions for governors use */
438 * update_devfreq() - Reevaluate the device and configure frequency.
439 * @devfreq: the devfreq instance.
441 * Note: Lock devfreq->lock before calling update_devfreq
442 * This function is exported for governors.
444 int update_devfreq(struct devfreq *devfreq)
446 return devfreq_update_target(devfreq, 0L);
448 EXPORT_SYMBOL(update_devfreq);
451 * devfreq_monitor() - Periodically poll devfreq objects.
452 * @work: the work struct used to run devfreq_monitor periodically.
455 static void devfreq_monitor(struct work_struct *work)
458 struct devfreq *devfreq = container_of(work,
459 struct devfreq, work.work);
461 mutex_lock(&devfreq->lock);
462 err = update_devfreq(devfreq);
464 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
466 queue_delayed_work(devfreq_wq, &devfreq->work,
467 msecs_to_jiffies(devfreq->profile->polling_ms));
468 mutex_unlock(&devfreq->lock);
470 trace_devfreq_monitor(devfreq);
474 * devfreq_monitor_start() - Start load monitoring of devfreq instance
475 * @devfreq: the devfreq instance.
477 * Helper function for starting devfreq device load monitoring. By
478 * default delayed work based monitoring is supported. Function
479 * to be called from governor in response to DEVFREQ_GOV_START
480 * event when device is added to devfreq framework.
482 void devfreq_monitor_start(struct devfreq *devfreq)
484 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
487 switch (devfreq->profile->timer) {
488 case DEVFREQ_TIMER_DEFERRABLE:
489 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
491 case DEVFREQ_TIMER_DELAYED:
492 INIT_DELAYED_WORK(&devfreq->work, devfreq_monitor);
498 if (devfreq->profile->polling_ms)
499 queue_delayed_work(devfreq_wq, &devfreq->work,
500 msecs_to_jiffies(devfreq->profile->polling_ms));
502 EXPORT_SYMBOL(devfreq_monitor_start);
505 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
506 * @devfreq: the devfreq instance.
508 * Helper function to stop devfreq device load monitoring. Function
509 * to be called from governor in response to DEVFREQ_GOV_STOP
510 * event when device is removed from devfreq framework.
512 void devfreq_monitor_stop(struct devfreq *devfreq)
514 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
517 cancel_delayed_work_sync(&devfreq->work);
519 EXPORT_SYMBOL(devfreq_monitor_stop);
522 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
523 * @devfreq: the devfreq instance.
525 * Helper function to suspend devfreq device load monitoring. Function
526 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
527 * event or when polling interval is set to zero.
529 * Note: Though this function is same as devfreq_monitor_stop(),
530 * intentionally kept separate to provide hooks for collecting
531 * transition statistics.
533 void devfreq_monitor_suspend(struct devfreq *devfreq)
535 mutex_lock(&devfreq->lock);
536 if (devfreq->stop_polling) {
537 mutex_unlock(&devfreq->lock);
541 devfreq_update_status(devfreq, devfreq->previous_freq);
542 devfreq->stop_polling = true;
543 mutex_unlock(&devfreq->lock);
545 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
548 cancel_delayed_work_sync(&devfreq->work);
550 EXPORT_SYMBOL(devfreq_monitor_suspend);
553 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
554 * @devfreq: the devfreq instance.
556 * Helper function to resume devfreq device load monitoring. Function
557 * to be called from governor in response to DEVFREQ_GOV_RESUME
558 * event or when polling interval is set to non-zero.
560 void devfreq_monitor_resume(struct devfreq *devfreq)
564 mutex_lock(&devfreq->lock);
566 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
569 if (!devfreq->stop_polling)
572 if (!delayed_work_pending(&devfreq->work) &&
573 devfreq->profile->polling_ms)
574 queue_delayed_work(devfreq_wq, &devfreq->work,
575 msecs_to_jiffies(devfreq->profile->polling_ms));
578 devfreq->stats.last_update = get_jiffies_64();
579 devfreq->stop_polling = false;
581 if (devfreq->profile->get_cur_freq &&
582 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
583 devfreq->previous_freq = freq;
586 mutex_unlock(&devfreq->lock);
588 EXPORT_SYMBOL(devfreq_monitor_resume);
591 * devfreq_update_interval() - Update device devfreq monitoring interval
592 * @devfreq: the devfreq instance.
593 * @delay: new polling interval to be set.
595 * Helper function to set new load monitoring polling interval. Function
596 * to be called from governor in response to DEVFREQ_GOV_UPDATE_INTERVAL event.
598 void devfreq_update_interval(struct devfreq *devfreq, unsigned int *delay)
600 unsigned int cur_delay = devfreq->profile->polling_ms;
601 unsigned int new_delay = *delay;
603 mutex_lock(&devfreq->lock);
604 devfreq->profile->polling_ms = new_delay;
606 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
609 if (devfreq->stop_polling)
612 /* if new delay is zero, stop polling */
614 mutex_unlock(&devfreq->lock);
615 cancel_delayed_work_sync(&devfreq->work);
619 /* if current delay is zero, start polling with new delay */
621 queue_delayed_work(devfreq_wq, &devfreq->work,
622 msecs_to_jiffies(devfreq->profile->polling_ms));
626 /* if current delay is greater than new delay, restart polling */
627 if (cur_delay > new_delay) {
628 mutex_unlock(&devfreq->lock);
629 cancel_delayed_work_sync(&devfreq->work);
630 mutex_lock(&devfreq->lock);
631 if (!devfreq->stop_polling)
632 queue_delayed_work(devfreq_wq, &devfreq->work,
633 msecs_to_jiffies(devfreq->profile->polling_ms));
636 mutex_unlock(&devfreq->lock);
638 EXPORT_SYMBOL(devfreq_update_interval);
641 * devfreq_notifier_call() - Notify that the device frequency requirements
642 * has been changed out of devfreq framework.
643 * @nb: the notifier_block (supposed to be devfreq->nb)
647 * Called by a notifier that uses devfreq->nb.
649 static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
652 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
655 mutex_lock(&devfreq->lock);
657 devfreq->scaling_min_freq = find_available_min_freq(devfreq);
658 if (!devfreq->scaling_min_freq)
661 devfreq->scaling_max_freq = find_available_max_freq(devfreq);
662 if (!devfreq->scaling_max_freq) {
663 devfreq->scaling_max_freq = ULONG_MAX;
667 err = update_devfreq(devfreq);
670 mutex_unlock(&devfreq->lock);
672 dev_err(devfreq->dev.parent,
673 "failed to update frequency from OPP notifier (%d)\n",
680 * qos_notifier_call() - Common handler for QoS constraints.
681 * @devfreq: the devfreq instance.
683 static int qos_notifier_call(struct devfreq *devfreq)
687 mutex_lock(&devfreq->lock);
688 err = update_devfreq(devfreq);
689 mutex_unlock(&devfreq->lock);
691 dev_err(devfreq->dev.parent,
692 "failed to update frequency from PM QoS (%d)\n",
699 * qos_min_notifier_call() - Callback for QoS min_freq changes.
700 * @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
712 static int qos_max_notifier_call(struct notifier_block *nb,
713 unsigned long val, void *ptr)
715 return qos_notifier_call(container_of(nb, struct devfreq, nb_max));
719 * devfreq_dev_release() - Callback for struct device to release the device.
720 * @dev: the devfreq device
722 * Remove devfreq from the list and release its resources.
724 static void devfreq_dev_release(struct device *dev)
726 struct devfreq *devfreq = to_devfreq(dev);
729 mutex_lock(&devfreq_list_lock);
730 list_del(&devfreq->node);
731 mutex_unlock(&devfreq_list_lock);
733 err = dev_pm_qos_remove_notifier(devfreq->dev.parent, &devfreq->nb_max,
734 DEV_PM_QOS_MAX_FREQUENCY);
735 if (err && err != -ENOENT)
736 dev_warn(dev->parent,
737 "Failed to remove max_freq notifier: %d\n", err);
738 err = dev_pm_qos_remove_notifier(devfreq->dev.parent, &devfreq->nb_min,
739 DEV_PM_QOS_MIN_FREQUENCY);
740 if (err && err != -ENOENT)
741 dev_warn(dev->parent,
742 "Failed to remove min_freq notifier: %d\n", err);
744 if (dev_pm_qos_request_active(&devfreq->user_max_freq_req)) {
745 err = dev_pm_qos_remove_request(&devfreq->user_max_freq_req);
747 dev_warn(dev->parent,
748 "Failed to remove max_freq request: %d\n", err);
750 if (dev_pm_qos_request_active(&devfreq->user_min_freq_req)) {
751 err = dev_pm_qos_remove_request(&devfreq->user_min_freq_req);
753 dev_warn(dev->parent,
754 "Failed to remove min_freq request: %d\n", err);
757 if (devfreq->profile->exit)
758 devfreq->profile->exit(devfreq->dev.parent);
760 if (devfreq->opp_table)
761 dev_pm_opp_put_opp_table(devfreq->opp_table);
763 mutex_destroy(&devfreq->lock);
767 static void create_sysfs_files(struct devfreq *devfreq,
768 const struct devfreq_governor *gov);
769 static void remove_sysfs_files(struct devfreq *devfreq,
770 const struct devfreq_governor *gov);
773 * devfreq_add_device() - Add devfreq feature to the device
774 * @dev: the device to add devfreq feature.
775 * @profile: device-specific profile to run devfreq.
776 * @governor_name: name of the policy to choose frequency.
777 * @data: private data for the governor. The devfreq framework does not
780 struct devfreq *devfreq_add_device(struct device *dev,
781 struct devfreq_dev_profile *profile,
782 const char *governor_name,
785 struct devfreq *devfreq;
786 struct devfreq_governor *governor;
789 if (!dev || !profile || !governor_name) {
790 dev_err(dev, "%s: Invalid parameters.\n", __func__);
791 return ERR_PTR(-EINVAL);
794 mutex_lock(&devfreq_list_lock);
795 devfreq = find_device_devfreq(dev);
796 mutex_unlock(&devfreq_list_lock);
797 if (!IS_ERR(devfreq)) {
798 dev_err(dev, "%s: devfreq device already exists!\n",
804 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
810 mutex_init(&devfreq->lock);
811 mutex_lock(&devfreq->lock);
812 devfreq->dev.parent = dev;
813 devfreq->dev.class = devfreq_class;
814 devfreq->dev.release = devfreq_dev_release;
815 INIT_LIST_HEAD(&devfreq->node);
816 devfreq->profile = profile;
817 devfreq->previous_freq = profile->initial_freq;
818 devfreq->last_status.current_frequency = profile->initial_freq;
819 devfreq->data = data;
820 devfreq->nb.notifier_call = devfreq_notifier_call;
822 if (devfreq->profile->timer < 0
823 || devfreq->profile->timer >= DEVFREQ_TIMER_NUM) {
827 if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
828 mutex_unlock(&devfreq->lock);
829 err = set_freq_table(devfreq);
832 mutex_lock(&devfreq->lock);
835 devfreq->scaling_min_freq = find_available_min_freq(devfreq);
836 if (!devfreq->scaling_min_freq) {
837 mutex_unlock(&devfreq->lock);
842 devfreq->scaling_max_freq = find_available_max_freq(devfreq);
843 if (!devfreq->scaling_max_freq) {
844 mutex_unlock(&devfreq->lock);
849 devfreq->suspend_freq = dev_pm_opp_get_suspend_opp_freq(dev);
850 devfreq->opp_table = dev_pm_opp_get_opp_table(dev);
851 if (IS_ERR(devfreq->opp_table))
852 devfreq->opp_table = NULL;
854 atomic_set(&devfreq->suspend_count, 0);
856 dev_set_name(&devfreq->dev, "%s", dev_name(dev));
857 err = device_register(&devfreq->dev);
859 mutex_unlock(&devfreq->lock);
860 put_device(&devfreq->dev);
864 devfreq->stats.trans_table = devm_kzalloc(&devfreq->dev,
865 array3_size(sizeof(unsigned int),
866 devfreq->profile->max_state,
867 devfreq->profile->max_state),
869 if (!devfreq->stats.trans_table) {
870 mutex_unlock(&devfreq->lock);
875 devfreq->stats.time_in_state = devm_kcalloc(&devfreq->dev,
876 devfreq->profile->max_state,
877 sizeof(*devfreq->stats.time_in_state),
879 if (!devfreq->stats.time_in_state) {
880 mutex_unlock(&devfreq->lock);
885 devfreq->stats.total_trans = 0;
886 devfreq->stats.last_update = get_jiffies_64();
888 srcu_init_notifier_head(&devfreq->transition_notifier_list);
890 mutex_unlock(&devfreq->lock);
892 err = dev_pm_qos_add_request(dev, &devfreq->user_min_freq_req,
893 DEV_PM_QOS_MIN_FREQUENCY, 0);
896 err = dev_pm_qos_add_request(dev, &devfreq->user_max_freq_req,
897 DEV_PM_QOS_MAX_FREQUENCY,
898 PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE);
902 devfreq->nb_min.notifier_call = qos_min_notifier_call;
903 err = dev_pm_qos_add_notifier(dev, &devfreq->nb_min,
904 DEV_PM_QOS_MIN_FREQUENCY);
908 devfreq->nb_max.notifier_call = qos_max_notifier_call;
909 err = dev_pm_qos_add_notifier(dev, &devfreq->nb_max,
910 DEV_PM_QOS_MAX_FREQUENCY);
914 mutex_lock(&devfreq_list_lock);
916 governor = try_then_request_governor(governor_name);
917 if (IS_ERR(governor)) {
918 dev_err(dev, "%s: Unable to find governor for the device\n",
920 err = PTR_ERR(governor);
924 devfreq->governor = governor;
925 err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
928 dev_err(dev, "%s: Unable to start governor for the device\n",
932 create_sysfs_files(devfreq, devfreq->governor);
934 list_add(&devfreq->node, &devfreq_list);
936 mutex_unlock(&devfreq_list_lock);
941 mutex_unlock(&devfreq_list_lock);
943 devfreq_remove_device(devfreq);
950 EXPORT_SYMBOL(devfreq_add_device);
953 * devfreq_remove_device() - Remove devfreq feature from a device.
954 * @devfreq: the devfreq instance to be removed
956 * The opposite of devfreq_add_device().
958 int devfreq_remove_device(struct devfreq *devfreq)
963 if (devfreq->governor) {
964 devfreq->governor->event_handler(devfreq,
965 DEVFREQ_GOV_STOP, NULL);
966 remove_sysfs_files(devfreq, devfreq->governor);
969 device_unregister(&devfreq->dev);
973 EXPORT_SYMBOL(devfreq_remove_device);
975 static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
977 struct devfreq **r = res;
979 if (WARN_ON(!r || !*r))
985 static void devm_devfreq_dev_release(struct device *dev, void *res)
987 devfreq_remove_device(*(struct devfreq **)res);
991 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
992 * @dev: the device to add devfreq feature.
993 * @profile: device-specific profile to run devfreq.
994 * @governor_name: name of the policy to choose frequency.
995 * @data: private data for the governor. The devfreq framework does not
998 * This function manages automatically the memory of devfreq device using device
999 * resource management and simplify the free operation for memory of devfreq
1002 struct devfreq *devm_devfreq_add_device(struct device *dev,
1003 struct devfreq_dev_profile *profile,
1004 const char *governor_name,
1007 struct devfreq **ptr, *devfreq;
1009 ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
1011 return ERR_PTR(-ENOMEM);
1013 devfreq = devfreq_add_device(dev, profile, governor_name, data);
1014 if (IS_ERR(devfreq)) {
1020 devres_add(dev, ptr);
1024 EXPORT_SYMBOL(devm_devfreq_add_device);
1028 * devfreq_get_devfreq_by_node - Get the devfreq device from devicetree
1029 * @node - pointer to device_node
1031 * return the instance of devfreq device
1033 struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node)
1035 struct devfreq *devfreq;
1038 return ERR_PTR(-EINVAL);
1040 mutex_lock(&devfreq_list_lock);
1041 list_for_each_entry(devfreq, &devfreq_list, node) {
1042 if (devfreq->dev.parent
1043 && devfreq->dev.parent->of_node == node) {
1044 mutex_unlock(&devfreq_list_lock);
1048 mutex_unlock(&devfreq_list_lock);
1050 return ERR_PTR(-ENODEV);
1054 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
1055 * @dev - instance to the given device
1056 * @phandle_name - name of property holding a phandle value
1057 * @index - index into list of devfreq
1059 * return the instance of devfreq device
1061 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
1062 const char *phandle_name, int index)
1064 struct device_node *node;
1065 struct devfreq *devfreq;
1067 if (!dev || !phandle_name)
1068 return ERR_PTR(-EINVAL);
1071 return ERR_PTR(-EINVAL);
1073 node = of_parse_phandle(dev->of_node, phandle_name, index);
1075 return ERR_PTR(-ENODEV);
1077 devfreq = devfreq_get_devfreq_by_node(node);
1084 struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node)
1086 return ERR_PTR(-ENODEV);
1089 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
1090 const char *phandle_name, int index)
1092 return ERR_PTR(-ENODEV);
1094 #endif /* CONFIG_OF */
1095 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_node);
1096 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
1099 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
1100 * @dev: the device from which to remove devfreq feature.
1101 * @devfreq: the devfreq instance to be removed
1103 void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
1105 WARN_ON(devres_release(dev, devm_devfreq_dev_release,
1106 devm_devfreq_dev_match, devfreq));
1108 EXPORT_SYMBOL(devm_devfreq_remove_device);
1111 * devfreq_suspend_device() - Suspend devfreq of a device.
1112 * @devfreq: the devfreq instance to be suspended
1114 * This function is intended to be called by the pm callbacks
1115 * (e.g., runtime_suspend, suspend) of the device driver that
1116 * holds the devfreq.
1118 int devfreq_suspend_device(struct devfreq *devfreq)
1125 if (atomic_inc_return(&devfreq->suspend_count) > 1)
1128 if (devfreq->governor) {
1129 ret = devfreq->governor->event_handler(devfreq,
1130 DEVFREQ_GOV_SUSPEND, NULL);
1135 if (devfreq->suspend_freq) {
1136 mutex_lock(&devfreq->lock);
1137 ret = devfreq_set_target(devfreq, devfreq->suspend_freq, 0);
1138 mutex_unlock(&devfreq->lock);
1145 EXPORT_SYMBOL(devfreq_suspend_device);
1148 * devfreq_resume_device() - Resume devfreq of a device.
1149 * @devfreq: the devfreq instance to be resumed
1151 * This function is intended to be called by the pm callbacks
1152 * (e.g., runtime_resume, resume) of the device driver that
1153 * holds the devfreq.
1155 int devfreq_resume_device(struct devfreq *devfreq)
1162 if (atomic_dec_return(&devfreq->suspend_count) >= 1)
1165 if (devfreq->resume_freq) {
1166 mutex_lock(&devfreq->lock);
1167 ret = devfreq_set_target(devfreq, devfreq->resume_freq, 0);
1168 mutex_unlock(&devfreq->lock);
1173 if (devfreq->governor) {
1174 ret = devfreq->governor->event_handler(devfreq,
1175 DEVFREQ_GOV_RESUME, NULL);
1182 EXPORT_SYMBOL(devfreq_resume_device);
1185 * devfreq_suspend() - Suspend devfreq governors and devices
1187 * Called during system wide Suspend/Hibernate cycles for suspending governors
1188 * and devices preserving the state for resume. On some platforms the devfreq
1189 * device must have precise state (frequency) after resume in order to provide
1190 * fully operating setup.
1192 void devfreq_suspend(void)
1194 struct devfreq *devfreq;
1197 mutex_lock(&devfreq_list_lock);
1198 list_for_each_entry(devfreq, &devfreq_list, node) {
1199 ret = devfreq_suspend_device(devfreq);
1201 dev_err(&devfreq->dev,
1202 "failed to suspend devfreq device\n");
1204 mutex_unlock(&devfreq_list_lock);
1208 * devfreq_resume() - Resume devfreq governors and devices
1210 * Called during system wide Suspend/Hibernate cycle for resuming governors and
1211 * devices that are suspended with devfreq_suspend().
1213 void devfreq_resume(void)
1215 struct devfreq *devfreq;
1218 mutex_lock(&devfreq_list_lock);
1219 list_for_each_entry(devfreq, &devfreq_list, node) {
1220 ret = devfreq_resume_device(devfreq);
1222 dev_warn(&devfreq->dev,
1223 "failed to resume devfreq device\n");
1225 mutex_unlock(&devfreq_list_lock);
1229 * devfreq_add_governor() - Add devfreq governor
1230 * @governor: the devfreq governor to be added
1232 int devfreq_add_governor(struct devfreq_governor *governor)
1234 struct devfreq_governor *g;
1235 struct devfreq *devfreq;
1239 pr_err("%s: Invalid parameters.\n", __func__);
1243 mutex_lock(&devfreq_list_lock);
1244 g = find_devfreq_governor(governor->name);
1246 pr_err("%s: governor %s already registered\n", __func__,
1252 list_add(&governor->node, &devfreq_governor_list);
1254 list_for_each_entry(devfreq, &devfreq_list, node) {
1256 struct device *dev = devfreq->dev.parent;
1258 if (!strncmp(devfreq->governor->name, governor->name,
1259 DEVFREQ_NAME_LEN)) {
1260 /* The following should never occur */
1261 if (devfreq->governor) {
1263 "%s: Governor %s already present\n",
1264 __func__, devfreq->governor->name);
1265 ret = devfreq->governor->event_handler(devfreq,
1266 DEVFREQ_GOV_STOP, NULL);
1269 "%s: Governor %s stop = %d\n",
1271 devfreq->governor->name, ret);
1275 devfreq->governor = governor;
1276 ret = devfreq->governor->event_handler(devfreq,
1277 DEVFREQ_GOV_START, NULL);
1279 dev_warn(dev, "%s: Governor %s start=%d\n",
1280 __func__, devfreq->governor->name,
1287 mutex_unlock(&devfreq_list_lock);
1291 EXPORT_SYMBOL(devfreq_add_governor);
1294 * devfreq_remove_governor() - Remove devfreq feature from a device.
1295 * @governor: the devfreq governor to be removed
1297 int devfreq_remove_governor(struct devfreq_governor *governor)
1299 struct devfreq_governor *g;
1300 struct devfreq *devfreq;
1304 pr_err("%s: Invalid parameters.\n", __func__);
1308 mutex_lock(&devfreq_list_lock);
1309 g = find_devfreq_governor(governor->name);
1311 pr_err("%s: governor %s not registered\n", __func__,
1316 list_for_each_entry(devfreq, &devfreq_list, node) {
1318 struct device *dev = devfreq->dev.parent;
1320 if (!strncmp(devfreq->governor->name, governor->name,
1321 DEVFREQ_NAME_LEN)) {
1322 /* we should have a devfreq governor! */
1323 if (!devfreq->governor) {
1324 dev_warn(dev, "%s: Governor %s NOT present\n",
1325 __func__, governor->name);
1329 ret = devfreq->governor->event_handler(devfreq,
1330 DEVFREQ_GOV_STOP, NULL);
1332 dev_warn(dev, "%s: Governor %s stop=%d\n",
1333 __func__, devfreq->governor->name,
1336 devfreq->governor = NULL;
1340 list_del(&governor->node);
1342 mutex_unlock(&devfreq_list_lock);
1346 EXPORT_SYMBOL(devfreq_remove_governor);
1348 static ssize_t name_show(struct device *dev,
1349 struct device_attribute *attr, char *buf)
1351 struct devfreq *df = to_devfreq(dev);
1352 return sprintf(buf, "%s\n", dev_name(df->dev.parent));
1354 static DEVICE_ATTR_RO(name);
1356 static ssize_t governor_show(struct device *dev,
1357 struct device_attribute *attr, char *buf)
1359 struct devfreq *df = to_devfreq(dev);
1364 return sprintf(buf, "%s\n", df->governor->name);
1367 static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
1368 const char *buf, size_t count)
1370 struct devfreq *df = to_devfreq(dev);
1372 char str_governor[DEVFREQ_NAME_LEN + 1];
1373 const struct devfreq_governor *governor, *prev_governor;
1378 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
1382 mutex_lock(&devfreq_list_lock);
1383 governor = try_then_request_governor(str_governor);
1384 if (IS_ERR(governor)) {
1385 ret = PTR_ERR(governor);
1388 if (df->governor == governor) {
1391 } else if (IS_SUPPORTED_FLAG(df->governor->flags, IMMUTABLE)
1392 || IS_SUPPORTED_FLAG(governor->flags, IMMUTABLE)) {
1398 * Stop the current governor and remove the specific sysfs files
1399 * which depend on current governor.
1401 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1403 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1404 __func__, df->governor->name, ret);
1407 remove_sysfs_files(df, df->governor);
1410 * Start the new governor and create the specific sysfs files
1411 * which depend on the new governor.
1413 prev_governor = df->governor;
1414 df->governor = governor;
1415 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1417 dev_warn(dev, "%s: Governor %s not started(%d)\n",
1418 __func__, df->governor->name, ret);
1420 /* Restore previous governor */
1421 df->governor = prev_governor;
1422 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1425 "%s: reverting to Governor %s failed (%d)\n",
1426 __func__, prev_governor->name, ret);
1427 df->governor = NULL;
1433 * Create the sysfs files for the new governor. But if failed to start
1434 * the new governor, restore the sysfs files of previous governor.
1436 create_sysfs_files(df, df->governor);
1439 mutex_unlock(&devfreq_list_lock);
1445 static DEVICE_ATTR_RW(governor);
1447 static ssize_t available_governors_show(struct device *d,
1448 struct device_attribute *attr,
1451 struct devfreq *df = to_devfreq(d);
1457 mutex_lock(&devfreq_list_lock);
1460 * The devfreq with immutable governor (e.g., passive) shows
1461 * only own governor.
1463 if (IS_SUPPORTED_FLAG(df->governor->flags, IMMUTABLE)) {
1464 count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
1465 "%s ", df->governor->name);
1467 * The devfreq device shows the registered governor except for
1468 * immutable governors such as passive governor .
1471 struct devfreq_governor *governor;
1473 list_for_each_entry(governor, &devfreq_governor_list, node) {
1474 if (IS_SUPPORTED_FLAG(governor->flags, IMMUTABLE))
1476 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1477 "%s ", governor->name);
1481 mutex_unlock(&devfreq_list_lock);
1483 /* Truncate the trailing space */
1487 count += sprintf(&buf[count], "\n");
1491 static DEVICE_ATTR_RO(available_governors);
1493 static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
1497 struct devfreq *df = to_devfreq(dev);
1502 if (df->profile->get_cur_freq &&
1503 !df->profile->get_cur_freq(df->dev.parent, &freq))
1504 return sprintf(buf, "%lu\n", freq);
1506 return sprintf(buf, "%lu\n", df->previous_freq);
1508 static DEVICE_ATTR_RO(cur_freq);
1510 static ssize_t target_freq_show(struct device *dev,
1511 struct device_attribute *attr, char *buf)
1513 struct devfreq *df = to_devfreq(dev);
1515 return sprintf(buf, "%lu\n", df->previous_freq);
1517 static DEVICE_ATTR_RO(target_freq);
1519 static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
1520 const char *buf, size_t count)
1522 struct devfreq *df = to_devfreq(dev);
1523 unsigned long value;
1527 * Protect against theoretical sysfs writes between
1528 * device_add and dev_pm_qos_add_request
1530 if (!dev_pm_qos_request_active(&df->user_min_freq_req))
1533 ret = sscanf(buf, "%lu", &value);
1537 /* Round down to kHz for PM QoS */
1538 ret = dev_pm_qos_update_request(&df->user_min_freq_req,
1539 value / HZ_PER_KHZ);
1546 static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
1549 struct devfreq *df = to_devfreq(dev);
1550 unsigned long min_freq, max_freq;
1552 mutex_lock(&df->lock);
1553 get_freq_range(df, &min_freq, &max_freq);
1554 mutex_unlock(&df->lock);
1556 return sprintf(buf, "%lu\n", min_freq);
1558 static DEVICE_ATTR_RW(min_freq);
1560 static ssize_t max_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_max_freq_req))
1574 ret = sscanf(buf, "%lu", &value);
1579 * PM QoS frequencies are in kHz so we need to convert. Convert by
1580 * rounding upwards so that the acceptable interval never shrinks.
1582 * For example if the user writes "666666666" to sysfs this value will
1583 * be converted to 666667 kHz and back to 666667000 Hz before an OPP
1584 * lookup, this ensures that an OPP of 666666666Hz is still accepted.
1586 * A value of zero means "no limit".
1589 value = DIV_ROUND_UP(value, HZ_PER_KHZ);
1591 value = PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE;
1593 ret = dev_pm_qos_update_request(&df->user_max_freq_req, value);
1600 static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
1603 struct devfreq *df = to_devfreq(dev);
1604 unsigned long min_freq, max_freq;
1606 mutex_lock(&df->lock);
1607 get_freq_range(df, &min_freq, &max_freq);
1608 mutex_unlock(&df->lock);
1610 return sprintf(buf, "%lu\n", max_freq);
1612 static DEVICE_ATTR_RW(max_freq);
1614 static ssize_t available_frequencies_show(struct device *d,
1615 struct device_attribute *attr,
1618 struct devfreq *df = to_devfreq(d);
1625 mutex_lock(&df->lock);
1627 for (i = 0; i < df->profile->max_state; i++)
1628 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1629 "%lu ", df->profile->freq_table[i]);
1631 mutex_unlock(&df->lock);
1632 /* Truncate the trailing space */
1636 count += sprintf(&buf[count], "\n");
1640 static DEVICE_ATTR_RO(available_frequencies);
1642 static ssize_t trans_stat_show(struct device *dev,
1643 struct device_attribute *attr, char *buf)
1645 struct devfreq *df = to_devfreq(dev);
1648 unsigned int max_state;
1652 max_state = df->profile->max_state;
1655 return sprintf(buf, "Not Supported.\n");
1657 mutex_lock(&df->lock);
1658 if (!df->stop_polling &&
1659 devfreq_update_status(df, df->previous_freq)) {
1660 mutex_unlock(&df->lock);
1663 mutex_unlock(&df->lock);
1665 len = sprintf(buf, " From : To\n");
1666 len += sprintf(buf + len, " :");
1667 for (i = 0; i < max_state; i++)
1668 len += sprintf(buf + len, "%10lu",
1669 df->profile->freq_table[i]);
1671 len += sprintf(buf + len, " time(ms)\n");
1673 for (i = 0; i < max_state; i++) {
1674 if (df->profile->freq_table[i]
1675 == df->previous_freq) {
1676 len += sprintf(buf + len, "*");
1678 len += sprintf(buf + len, " ");
1680 len += sprintf(buf + len, "%10lu:",
1681 df->profile->freq_table[i]);
1682 for (j = 0; j < max_state; j++)
1683 len += sprintf(buf + len, "%10u",
1684 df->stats.trans_table[(i * max_state) + j]);
1686 len += sprintf(buf + len, "%10llu\n", (u64)
1687 jiffies64_to_msecs(df->stats.time_in_state[i]));
1690 len += sprintf(buf + len, "Total transition : %u\n",
1691 df->stats.total_trans);
1695 static ssize_t trans_stat_store(struct device *dev,
1696 struct device_attribute *attr,
1697 const char *buf, size_t count)
1699 struct devfreq *df = to_devfreq(dev);
1705 if (df->profile->max_state == 0)
1708 err = kstrtoint(buf, 10, &value);
1709 if (err || value != 0)
1712 mutex_lock(&df->lock);
1713 memset(df->stats.time_in_state, 0, (df->profile->max_state *
1714 sizeof(*df->stats.time_in_state)));
1715 memset(df->stats.trans_table, 0, array3_size(sizeof(unsigned int),
1716 df->profile->max_state,
1717 df->profile->max_state));
1718 df->stats.total_trans = 0;
1719 df->stats.last_update = get_jiffies_64();
1720 mutex_unlock(&df->lock);
1724 static DEVICE_ATTR_RW(trans_stat);
1726 static struct attribute *devfreq_attrs[] = {
1727 &dev_attr_name.attr,
1728 &dev_attr_governor.attr,
1729 &dev_attr_available_governors.attr,
1730 &dev_attr_cur_freq.attr,
1731 &dev_attr_available_frequencies.attr,
1732 &dev_attr_target_freq.attr,
1733 &dev_attr_min_freq.attr,
1734 &dev_attr_max_freq.attr,
1735 &dev_attr_trans_stat.attr,
1738 ATTRIBUTE_GROUPS(devfreq);
1740 static ssize_t polling_interval_show(struct device *dev,
1741 struct device_attribute *attr, char *buf)
1743 struct devfreq *df = to_devfreq(dev);
1748 return sprintf(buf, "%d\n", df->profile->polling_ms);
1751 static ssize_t polling_interval_store(struct device *dev,
1752 struct device_attribute *attr,
1753 const char *buf, size_t count)
1755 struct devfreq *df = to_devfreq(dev);
1762 ret = sscanf(buf, "%u", &value);
1766 df->governor->event_handler(df, DEVFREQ_GOV_UPDATE_INTERVAL, &value);
1771 static DEVICE_ATTR_RW(polling_interval);
1773 static ssize_t timer_show(struct device *dev,
1774 struct device_attribute *attr, char *buf)
1776 struct devfreq *df = to_devfreq(dev);
1781 return sprintf(buf, "%s\n", timer_name[df->profile->timer]);
1784 static ssize_t timer_store(struct device *dev, struct device_attribute *attr,
1785 const char *buf, size_t count)
1787 struct devfreq *df = to_devfreq(dev);
1788 char str_timer[DEVFREQ_NAME_LEN + 1];
1792 if (!df->governor || !df->profile)
1795 ret = sscanf(buf, "%16s", str_timer);
1799 for (i = 0; i < DEVFREQ_TIMER_NUM; i++) {
1800 if (!strncmp(timer_name[i], str_timer, DEVFREQ_NAME_LEN)) {
1811 if (df->profile->timer == timer) {
1816 mutex_lock(&df->lock);
1817 df->profile->timer = timer;
1818 mutex_unlock(&df->lock);
1820 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1822 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1823 __func__, df->governor->name, ret);
1827 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1829 dev_warn(dev, "%s: Governor %s not started(%d)\n",
1830 __func__, df->governor->name, ret);
1832 return ret ? ret : count;
1834 static DEVICE_ATTR_RW(timer);
1836 #define CREATE_SYSFS_FILE(df, name) \
1839 ret = sysfs_create_file(&df->dev.kobj, &dev_attr_##name.attr); \
1841 dev_warn(&df->dev, \
1842 "Unable to create attr(%s)\n", "##name"); \
1846 /* Create the specific sysfs files which depend on each governor. */
1847 static void create_sysfs_files(struct devfreq *devfreq,
1848 const struct devfreq_governor *gov)
1850 if (IS_SUPPORTED_ATTR(gov->attrs, POLLING_INTERVAL))
1851 CREATE_SYSFS_FILE(devfreq, polling_interval);
1852 if (IS_SUPPORTED_ATTR(gov->attrs, TIMER))
1853 CREATE_SYSFS_FILE(devfreq, timer);
1856 /* Remove the specific sysfs files which depend on each governor. */
1857 static void remove_sysfs_files(struct devfreq *devfreq,
1858 const struct devfreq_governor *gov)
1860 if (IS_SUPPORTED_ATTR(gov->attrs, POLLING_INTERVAL))
1861 sysfs_remove_file(&devfreq->dev.kobj,
1862 &dev_attr_polling_interval.attr);
1863 if (IS_SUPPORTED_ATTR(gov->attrs, TIMER))
1864 sysfs_remove_file(&devfreq->dev.kobj, &dev_attr_timer.attr);
1868 * devfreq_summary_show() - Show the summary of the devfreq devices
1869 * @s: seq_file instance to show the summary of devfreq devices
1872 * Show the summary of the devfreq devices via 'devfreq_summary' debugfs file.
1873 * It helps that user can know the detailed information of the devfreq devices.
1875 * Return 0 always because it shows the information without any data change.
1877 static int devfreq_summary_show(struct seq_file *s, void *data)
1879 struct devfreq *devfreq;
1880 struct devfreq *p_devfreq = NULL;
1881 unsigned long cur_freq, min_freq, max_freq;
1882 unsigned int polling_ms;
1885 seq_printf(s, "%-30s %-30s %-15s %-10s %10s %12s %12s %12s\n",
1894 seq_printf(s, "%30s %30s %15s %10s %10s %12s %12s %12s\n",
1895 "------------------------------",
1896 "------------------------------",
1904 mutex_lock(&devfreq_list_lock);
1906 list_for_each_entry_reverse(devfreq, &devfreq_list, node) {
1907 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE)
1908 if (!strncmp(devfreq->governor->name, DEVFREQ_GOV_PASSIVE,
1909 DEVFREQ_NAME_LEN)) {
1910 struct devfreq_passive_data *data = devfreq->data;
1913 p_devfreq = data->parent;
1919 mutex_lock(&devfreq->lock);
1920 cur_freq = devfreq->previous_freq;
1921 get_freq_range(devfreq, &min_freq, &max_freq);
1922 timer = devfreq->profile->timer;
1924 if (IS_SUPPORTED_ATTR(devfreq->governor->attrs, POLLING_INTERVAL))
1925 polling_ms = devfreq->profile->polling_ms;
1928 mutex_unlock(&devfreq->lock);
1931 "%-30s %-30s %-15s %-10s %10d %12ld %12ld %12ld\n",
1932 dev_name(&devfreq->dev),
1933 p_devfreq ? dev_name(&p_devfreq->dev) : "null",
1934 devfreq->governor->name,
1935 polling_ms ? timer_name[timer] : "null",
1942 mutex_unlock(&devfreq_list_lock);
1946 DEFINE_SHOW_ATTRIBUTE(devfreq_summary);
1948 static int __init devfreq_init(void)
1950 devfreq_class = class_create(THIS_MODULE, "devfreq");
1951 if (IS_ERR(devfreq_class)) {
1952 pr_err("%s: couldn't create class\n", __FILE__);
1953 return PTR_ERR(devfreq_class);
1956 devfreq_wq = create_freezable_workqueue("devfreq_wq");
1958 class_destroy(devfreq_class);
1959 pr_err("%s: couldn't create workqueue\n", __FILE__);
1962 devfreq_class->dev_groups = devfreq_groups;
1964 devfreq_debugfs = debugfs_create_dir("devfreq", NULL);
1965 debugfs_create_file("devfreq_summary", 0444,
1966 devfreq_debugfs, NULL,
1967 &devfreq_summary_fops);
1971 subsys_initcall(devfreq_init);
1974 * The following are helper functions for devfreq user device drivers with
1979 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1980 * freq value given to target callback.
1981 * @dev: The devfreq user device. (parent of devfreq)
1982 * @freq: The frequency given to target function
1983 * @flags: Flags handed from devfreq framework.
1985 * The callers are required to call dev_pm_opp_put() for the returned OPP after
1988 struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1989 unsigned long *freq,
1992 struct dev_pm_opp *opp;
1994 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1995 /* The freq is an upper bound. opp should be lower */
1996 opp = dev_pm_opp_find_freq_floor(dev, freq);
1998 /* If not available, use the closest opp */
1999 if (opp == ERR_PTR(-ERANGE))
2000 opp = dev_pm_opp_find_freq_ceil(dev, freq);
2002 /* The freq is an lower bound. opp should be higher */
2003 opp = dev_pm_opp_find_freq_ceil(dev, freq);
2005 /* If not available, use the closest opp */
2006 if (opp == ERR_PTR(-ERANGE))
2007 opp = dev_pm_opp_find_freq_floor(dev, freq);
2012 EXPORT_SYMBOL(devfreq_recommended_opp);
2015 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
2016 * for any changes in the OPP availability
2018 * @dev: The devfreq user device. (parent of devfreq)
2019 * @devfreq: The devfreq object.
2021 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
2023 return dev_pm_opp_register_notifier(dev, &devfreq->nb);
2025 EXPORT_SYMBOL(devfreq_register_opp_notifier);
2028 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
2029 * notified for any changes in the OPP
2030 * availability changes anymore.
2031 * @dev: The devfreq user device. (parent of devfreq)
2032 * @devfreq: The devfreq object.
2034 * At exit() callback of devfreq_dev_profile, this must be included if
2035 * devfreq_recommended_opp is used.
2037 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
2039 return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
2041 EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
2043 static void devm_devfreq_opp_release(struct device *dev, void *res)
2045 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
2049 * devm_devfreq_register_opp_notifier() - Resource-managed
2050 * devfreq_register_opp_notifier()
2051 * @dev: The devfreq user device. (parent of devfreq)
2052 * @devfreq: The devfreq object.
2054 int devm_devfreq_register_opp_notifier(struct device *dev,
2055 struct devfreq *devfreq)
2057 struct devfreq **ptr;
2060 ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
2064 ret = devfreq_register_opp_notifier(dev, devfreq);
2071 devres_add(dev, ptr);
2075 EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
2078 * devm_devfreq_unregister_opp_notifier() - Resource-managed
2079 * devfreq_unregister_opp_notifier()
2080 * @dev: The devfreq user device. (parent of devfreq)
2081 * @devfreq: The devfreq object.
2083 void devm_devfreq_unregister_opp_notifier(struct device *dev,
2084 struct devfreq *devfreq)
2086 WARN_ON(devres_release(dev, devm_devfreq_opp_release,
2087 devm_devfreq_dev_match, devfreq));
2089 EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
2092 * devfreq_register_notifier() - Register a driver with devfreq
2093 * @devfreq: The devfreq object.
2094 * @nb: The notifier block to register.
2095 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2097 int devfreq_register_notifier(struct devfreq *devfreq,
2098 struct notifier_block *nb,
2107 case DEVFREQ_TRANSITION_NOTIFIER:
2108 ret = srcu_notifier_chain_register(
2109 &devfreq->transition_notifier_list, nb);
2117 EXPORT_SYMBOL(devfreq_register_notifier);
2120 * devfreq_unregister_notifier() - Unregister a driver with devfreq
2121 * @devfreq: The devfreq object.
2122 * @nb: The notifier block to be unregistered.
2123 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2125 int devfreq_unregister_notifier(struct devfreq *devfreq,
2126 struct notifier_block *nb,
2135 case DEVFREQ_TRANSITION_NOTIFIER:
2136 ret = srcu_notifier_chain_unregister(
2137 &devfreq->transition_notifier_list, nb);
2145 EXPORT_SYMBOL(devfreq_unregister_notifier);
2147 struct devfreq_notifier_devres {
2148 struct devfreq *devfreq;
2149 struct notifier_block *nb;
2153 static void devm_devfreq_notifier_release(struct device *dev, void *res)
2155 struct devfreq_notifier_devres *this = res;
2157 devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
2161 * devm_devfreq_register_notifier()
2162 * - Resource-managed devfreq_register_notifier()
2163 * @dev: The devfreq user device. (parent of devfreq)
2164 * @devfreq: The devfreq object.
2165 * @nb: The notifier block to be unregistered.
2166 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2168 int devm_devfreq_register_notifier(struct device *dev,
2169 struct devfreq *devfreq,
2170 struct notifier_block *nb,
2173 struct devfreq_notifier_devres *ptr;
2176 ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
2181 ret = devfreq_register_notifier(devfreq, nb, list);
2187 ptr->devfreq = devfreq;
2190 devres_add(dev, ptr);
2194 EXPORT_SYMBOL(devm_devfreq_register_notifier);
2197 * devm_devfreq_unregister_notifier()
2198 * - Resource-managed devfreq_unregister_notifier()
2199 * @dev: The devfreq user device. (parent of devfreq)
2200 * @devfreq: The devfreq object.
2201 * @nb: The notifier block to be unregistered.
2202 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2204 void devm_devfreq_unregister_notifier(struct device *dev,
2205 struct devfreq *devfreq,
2206 struct notifier_block *nb,
2209 WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
2210 devm_devfreq_dev_match, devfreq));
2212 EXPORT_SYMBOL(devm_devfreq_unregister_notifier);