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/errno.h>
14 #include <linux/err.h>
15 #include <linux/init.h>
16 #include <linux/export.h>
17 #include <linux/slab.h>
18 #include <linux/stat.h>
19 #include <linux/pm_opp.h>
20 #include <linux/devfreq.h>
21 #include <linux/workqueue.h>
22 #include <linux/platform_device.h>
23 #include <linux/list.h>
24 #include <linux/printk.h>
25 #include <linux/hrtimer.h>
29 #define CREATE_TRACE_POINTS
30 #include <trace/events/devfreq.h>
32 static struct class *devfreq_class;
35 * devfreq core provides delayed work based load monitoring helper
36 * functions. Governors can use these or can implement their own
37 * monitoring mechanism.
39 static struct workqueue_struct *devfreq_wq;
41 /* The list of all device-devfreq governors */
42 static LIST_HEAD(devfreq_governor_list);
43 /* The list of all device-devfreq */
44 static LIST_HEAD(devfreq_list);
45 static DEFINE_MUTEX(devfreq_list_lock);
48 * find_device_devfreq() - find devfreq struct using device pointer
49 * @dev: device pointer used to lookup device devfreq.
51 * Search the list of device devfreqs and return the matched device's
52 * devfreq info. devfreq_list_lock should be held by the caller.
54 static struct devfreq *find_device_devfreq(struct device *dev)
56 struct devfreq *tmp_devfreq;
58 if (IS_ERR_OR_NULL(dev)) {
59 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
60 return ERR_PTR(-EINVAL);
62 WARN(!mutex_is_locked(&devfreq_list_lock),
63 "devfreq_list_lock must be locked.");
65 list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
66 if (tmp_devfreq->dev.parent == dev)
70 return ERR_PTR(-ENODEV);
73 static unsigned long find_available_min_freq(struct devfreq *devfreq)
75 struct dev_pm_opp *opp;
76 unsigned long min_freq = 0;
78 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &min_freq);
87 static unsigned long find_available_max_freq(struct devfreq *devfreq)
89 struct dev_pm_opp *opp;
90 unsigned long max_freq = ULONG_MAX;
92 opp = dev_pm_opp_find_freq_floor(devfreq->dev.parent, &max_freq);
102 * devfreq_get_freq_level() - Lookup freq_table for the frequency
103 * @devfreq: the devfreq instance
104 * @freq: the target frequency
106 static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq)
110 for (lev = 0; lev < devfreq->profile->max_state; lev++)
111 if (freq == devfreq->profile->freq_table[lev])
117 static int set_freq_table(struct devfreq *devfreq)
119 struct devfreq_dev_profile *profile = devfreq->profile;
120 struct dev_pm_opp *opp;
124 /* Initialize the freq_table from OPP table */
125 count = dev_pm_opp_get_opp_count(devfreq->dev.parent);
129 profile->max_state = count;
130 profile->freq_table = devm_kcalloc(devfreq->dev.parent,
132 sizeof(*profile->freq_table),
134 if (!profile->freq_table) {
135 profile->max_state = 0;
139 for (i = 0, freq = 0; i < profile->max_state; i++, freq++) {
140 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq);
142 devm_kfree(devfreq->dev.parent, profile->freq_table);
143 profile->max_state = 0;
147 profile->freq_table[i] = freq;
154 * devfreq_update_status() - Update statistics of devfreq behavior
155 * @devfreq: the devfreq instance
156 * @freq: the update target frequency
158 int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
160 int lev, prev_lev, ret = 0;
161 unsigned long cur_time;
163 lockdep_assert_held(&devfreq->lock);
166 /* Immediately exit if previous_freq is not initialized yet. */
167 if (!devfreq->previous_freq)
170 prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
176 devfreq->time_in_state[prev_lev] +=
177 cur_time - devfreq->last_stat_updated;
179 lev = devfreq_get_freq_level(devfreq, freq);
185 if (lev != prev_lev) {
186 devfreq->trans_table[(prev_lev *
187 devfreq->profile->max_state) + lev]++;
188 devfreq->total_trans++;
192 devfreq->last_stat_updated = cur_time;
195 EXPORT_SYMBOL(devfreq_update_status);
198 * find_devfreq_governor() - find devfreq governor from name
199 * @name: name of the governor
201 * Search the list of devfreq governors and return the matched
202 * governor's pointer. devfreq_list_lock should be held by the caller.
204 static struct devfreq_governor *find_devfreq_governor(const char *name)
206 struct devfreq_governor *tmp_governor;
208 if (IS_ERR_OR_NULL(name)) {
209 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
210 return ERR_PTR(-EINVAL);
212 WARN(!mutex_is_locked(&devfreq_list_lock),
213 "devfreq_list_lock must be locked.");
215 list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
216 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
220 return ERR_PTR(-ENODEV);
224 * try_then_request_governor() - Try to find the governor and request the
225 * module if is not found.
226 * @name: name of the governor
228 * Search the list of devfreq governors and request the module and try again
229 * if is not found. This can happen when both drivers (the governor driver
230 * and the driver that call devfreq_add_device) are built as modules.
231 * devfreq_list_lock should be held by the caller. Returns the matched
232 * governor's pointer or an error pointer.
234 static struct devfreq_governor *try_then_request_governor(const char *name)
236 struct devfreq_governor *governor;
239 if (IS_ERR_OR_NULL(name)) {
240 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
241 return ERR_PTR(-EINVAL);
243 WARN(!mutex_is_locked(&devfreq_list_lock),
244 "devfreq_list_lock must be locked.");
246 governor = find_devfreq_governor(name);
247 if (IS_ERR(governor)) {
248 mutex_unlock(&devfreq_list_lock);
250 if (!strncmp(name, DEVFREQ_GOV_SIMPLE_ONDEMAND,
252 err = request_module("governor_%s", "simpleondemand");
254 err = request_module("governor_%s", name);
255 /* Restore previous state before return */
256 mutex_lock(&devfreq_list_lock);
258 return (err < 0) ? ERR_PTR(err) : ERR_PTR(-EINVAL);
260 governor = find_devfreq_governor(name);
266 static int devfreq_notify_transition(struct devfreq *devfreq,
267 struct devfreq_freqs *freqs, unsigned int state)
273 case DEVFREQ_PRECHANGE:
274 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
275 DEVFREQ_PRECHANGE, freqs);
278 case DEVFREQ_POSTCHANGE:
279 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
280 DEVFREQ_POSTCHANGE, freqs);
289 static int devfreq_set_target(struct devfreq *devfreq, unsigned long new_freq,
292 struct devfreq_freqs freqs;
293 unsigned long cur_freq;
296 if (devfreq->profile->get_cur_freq)
297 devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
299 cur_freq = devfreq->previous_freq;
301 freqs.old = cur_freq;
302 freqs.new = new_freq;
303 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
305 err = devfreq->profile->target(devfreq->dev.parent, &new_freq, flags);
307 freqs.new = cur_freq;
308 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
312 freqs.new = new_freq;
313 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
315 if (devfreq_update_status(devfreq, new_freq))
316 dev_err(&devfreq->dev,
317 "Couldn't update frequency transition information.\n");
319 devfreq->previous_freq = new_freq;
321 if (devfreq->suspend_freq)
322 devfreq->resume_freq = new_freq;
327 /* Load monitoring helper functions for governors use */
330 * update_devfreq() - Reevaluate the device and configure frequency.
331 * @devfreq: the devfreq instance.
333 * Note: Lock devfreq->lock before calling update_devfreq
334 * This function is exported for governors.
336 int update_devfreq(struct devfreq *devfreq)
338 unsigned long freq, min_freq, max_freq;
342 if (!mutex_is_locked(&devfreq->lock)) {
343 WARN(true, "devfreq->lock must be locked by the caller.\n");
347 if (!devfreq->governor)
350 /* Reevaluate the proper frequency */
351 err = devfreq->governor->get_target_freq(devfreq, &freq);
356 * Adjust the frequency with user freq, QoS and available freq.
358 * List from the highest priority
362 max_freq = min(devfreq->scaling_max_freq, devfreq->max_freq);
363 min_freq = max(devfreq->scaling_min_freq, devfreq->min_freq);
365 if (freq < min_freq) {
367 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
369 if (freq > max_freq) {
371 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
374 return devfreq_set_target(devfreq, freq, flags);
377 EXPORT_SYMBOL(update_devfreq);
380 * devfreq_monitor() - Periodically poll devfreq objects.
381 * @work: the work struct used to run devfreq_monitor periodically.
384 static void devfreq_monitor(struct work_struct *work)
387 struct devfreq *devfreq = container_of(work,
388 struct devfreq, work.work);
390 mutex_lock(&devfreq->lock);
391 err = update_devfreq(devfreq);
393 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
395 queue_delayed_work(devfreq_wq, &devfreq->work,
396 msecs_to_jiffies(devfreq->profile->polling_ms));
397 mutex_unlock(&devfreq->lock);
399 trace_devfreq_monitor(devfreq);
403 * devfreq_monitor_start() - Start load monitoring of devfreq instance
404 * @devfreq: the devfreq instance.
406 * Helper function for starting devfreq device load monitoring. By
407 * default delayed work based monitoring is supported. Function
408 * to be called from governor in response to DEVFREQ_GOV_START
409 * event when device is added to devfreq framework.
411 void devfreq_monitor_start(struct devfreq *devfreq)
413 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
414 if (devfreq->profile->polling_ms)
415 queue_delayed_work(devfreq_wq, &devfreq->work,
416 msecs_to_jiffies(devfreq->profile->polling_ms));
418 EXPORT_SYMBOL(devfreq_monitor_start);
421 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
422 * @devfreq: the devfreq instance.
424 * Helper function to stop devfreq device load monitoring. Function
425 * to be called from governor in response to DEVFREQ_GOV_STOP
426 * event when device is removed from devfreq framework.
428 void devfreq_monitor_stop(struct devfreq *devfreq)
430 cancel_delayed_work_sync(&devfreq->work);
432 EXPORT_SYMBOL(devfreq_monitor_stop);
435 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
436 * @devfreq: the devfreq instance.
438 * Helper function to suspend devfreq device load monitoring. Function
439 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
440 * event or when polling interval is set to zero.
442 * Note: Though this function is same as devfreq_monitor_stop(),
443 * intentionally kept separate to provide hooks for collecting
444 * transition statistics.
446 void devfreq_monitor_suspend(struct devfreq *devfreq)
448 mutex_lock(&devfreq->lock);
449 if (devfreq->stop_polling) {
450 mutex_unlock(&devfreq->lock);
454 devfreq_update_status(devfreq, devfreq->previous_freq);
455 devfreq->stop_polling = true;
456 mutex_unlock(&devfreq->lock);
457 cancel_delayed_work_sync(&devfreq->work);
459 EXPORT_SYMBOL(devfreq_monitor_suspend);
462 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
463 * @devfreq: the devfreq instance.
465 * Helper function to resume devfreq device load monitoring. Function
466 * to be called from governor in response to DEVFREQ_GOV_RESUME
467 * event or when polling interval is set to non-zero.
469 void devfreq_monitor_resume(struct devfreq *devfreq)
473 mutex_lock(&devfreq->lock);
474 if (!devfreq->stop_polling)
477 if (!delayed_work_pending(&devfreq->work) &&
478 devfreq->profile->polling_ms)
479 queue_delayed_work(devfreq_wq, &devfreq->work,
480 msecs_to_jiffies(devfreq->profile->polling_ms));
482 devfreq->last_stat_updated = jiffies;
483 devfreq->stop_polling = false;
485 if (devfreq->profile->get_cur_freq &&
486 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
487 devfreq->previous_freq = freq;
490 mutex_unlock(&devfreq->lock);
492 EXPORT_SYMBOL(devfreq_monitor_resume);
495 * devfreq_interval_update() - Update device devfreq monitoring interval
496 * @devfreq: the devfreq instance.
497 * @delay: new polling interval to be set.
499 * Helper function to set new load monitoring polling interval. Function
500 * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
502 void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
504 unsigned int cur_delay = devfreq->profile->polling_ms;
505 unsigned int new_delay = *delay;
507 mutex_lock(&devfreq->lock);
508 devfreq->profile->polling_ms = new_delay;
510 if (devfreq->stop_polling)
513 /* if new delay is zero, stop polling */
515 mutex_unlock(&devfreq->lock);
516 cancel_delayed_work_sync(&devfreq->work);
520 /* if current delay is zero, start polling with new delay */
522 queue_delayed_work(devfreq_wq, &devfreq->work,
523 msecs_to_jiffies(devfreq->profile->polling_ms));
527 /* if current delay is greater than new delay, restart polling */
528 if (cur_delay > new_delay) {
529 mutex_unlock(&devfreq->lock);
530 cancel_delayed_work_sync(&devfreq->work);
531 mutex_lock(&devfreq->lock);
532 if (!devfreq->stop_polling)
533 queue_delayed_work(devfreq_wq, &devfreq->work,
534 msecs_to_jiffies(devfreq->profile->polling_ms));
537 mutex_unlock(&devfreq->lock);
539 EXPORT_SYMBOL(devfreq_interval_update);
542 * devfreq_notifier_call() - Notify that the device frequency requirements
543 * has been changed out of devfreq framework.
544 * @nb: the notifier_block (supposed to be devfreq->nb)
548 * Called by a notifier that uses devfreq->nb.
550 static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
553 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
556 mutex_lock(&devfreq->lock);
558 devfreq->scaling_min_freq = find_available_min_freq(devfreq);
559 if (!devfreq->scaling_min_freq)
562 devfreq->scaling_max_freq = find_available_max_freq(devfreq);
563 if (!devfreq->scaling_max_freq) {
564 devfreq->scaling_max_freq = ULONG_MAX;
568 err = update_devfreq(devfreq);
571 mutex_unlock(&devfreq->lock);
573 dev_err(devfreq->dev.parent,
574 "failed to update frequency from OPP notifier (%d)\n",
581 * devfreq_dev_release() - Callback for struct device to release the device.
582 * @dev: the devfreq device
584 * Remove devfreq from the list and release its resources.
586 static void devfreq_dev_release(struct device *dev)
588 struct devfreq *devfreq = to_devfreq(dev);
590 mutex_lock(&devfreq_list_lock);
591 list_del(&devfreq->node);
592 mutex_unlock(&devfreq_list_lock);
594 if (devfreq->profile->exit)
595 devfreq->profile->exit(devfreq->dev.parent);
597 mutex_destroy(&devfreq->lock);
602 * devfreq_add_device() - Add devfreq feature to the device
603 * @dev: the device to add devfreq feature.
604 * @profile: device-specific profile to run devfreq.
605 * @governor_name: name of the policy to choose frequency.
606 * @data: devfreq driver pass to governors, governor should not change it.
608 struct devfreq *devfreq_add_device(struct device *dev,
609 struct devfreq_dev_profile *profile,
610 const char *governor_name,
613 struct devfreq *devfreq;
614 struct devfreq_governor *governor;
617 if (!dev || !profile || !governor_name) {
618 dev_err(dev, "%s: Invalid parameters.\n", __func__);
619 return ERR_PTR(-EINVAL);
622 mutex_lock(&devfreq_list_lock);
623 devfreq = find_device_devfreq(dev);
624 mutex_unlock(&devfreq_list_lock);
625 if (!IS_ERR(devfreq)) {
626 dev_err(dev, "%s: Unable to create devfreq for the device.\n",
632 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
638 mutex_init(&devfreq->lock);
639 mutex_lock(&devfreq->lock);
640 devfreq->dev.parent = dev;
641 devfreq->dev.class = devfreq_class;
642 devfreq->dev.release = devfreq_dev_release;
643 INIT_LIST_HEAD(&devfreq->node);
644 devfreq->profile = profile;
645 strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
646 devfreq->previous_freq = profile->initial_freq;
647 devfreq->last_status.current_frequency = profile->initial_freq;
648 devfreq->data = data;
649 devfreq->nb.notifier_call = devfreq_notifier_call;
651 if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
652 mutex_unlock(&devfreq->lock);
653 err = set_freq_table(devfreq);
656 mutex_lock(&devfreq->lock);
659 devfreq->scaling_min_freq = find_available_min_freq(devfreq);
660 if (!devfreq->scaling_min_freq) {
661 mutex_unlock(&devfreq->lock);
665 devfreq->min_freq = devfreq->scaling_min_freq;
667 devfreq->scaling_max_freq = find_available_max_freq(devfreq);
668 if (!devfreq->scaling_max_freq) {
669 mutex_unlock(&devfreq->lock);
673 devfreq->max_freq = devfreq->scaling_max_freq;
675 devfreq->suspend_freq = dev_pm_opp_get_suspend_opp_freq(dev);
676 atomic_set(&devfreq->suspend_count, 0);
678 dev_set_name(&devfreq->dev, "%s", dev_name(dev));
679 err = device_register(&devfreq->dev);
681 mutex_unlock(&devfreq->lock);
682 put_device(&devfreq->dev);
686 devfreq->trans_table = devm_kzalloc(&devfreq->dev,
687 array3_size(sizeof(unsigned int),
688 devfreq->profile->max_state,
689 devfreq->profile->max_state),
691 if (!devfreq->trans_table) {
692 mutex_unlock(&devfreq->lock);
697 devfreq->time_in_state = devm_kcalloc(&devfreq->dev,
698 devfreq->profile->max_state,
699 sizeof(unsigned long),
701 if (!devfreq->time_in_state) {
702 mutex_unlock(&devfreq->lock);
707 devfreq->last_stat_updated = jiffies;
709 srcu_init_notifier_head(&devfreq->transition_notifier_list);
711 mutex_unlock(&devfreq->lock);
713 mutex_lock(&devfreq_list_lock);
715 governor = try_then_request_governor(devfreq->governor_name);
716 if (IS_ERR(governor)) {
717 dev_err(dev, "%s: Unable to find governor for the device\n",
719 err = PTR_ERR(governor);
723 devfreq->governor = governor;
724 err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
727 dev_err(dev, "%s: Unable to start governor for the device\n",
732 list_add(&devfreq->node, &devfreq_list);
734 mutex_unlock(&devfreq_list_lock);
739 mutex_unlock(&devfreq_list_lock);
741 devfreq_remove_device(devfreq);
748 EXPORT_SYMBOL(devfreq_add_device);
751 * devfreq_remove_device() - Remove devfreq feature from a device.
752 * @devfreq: the devfreq instance to be removed
754 * The opposite of devfreq_add_device().
756 int devfreq_remove_device(struct devfreq *devfreq)
761 if (devfreq->governor)
762 devfreq->governor->event_handler(devfreq,
763 DEVFREQ_GOV_STOP, NULL);
764 device_unregister(&devfreq->dev);
768 EXPORT_SYMBOL(devfreq_remove_device);
770 static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
772 struct devfreq **r = res;
774 if (WARN_ON(!r || !*r))
780 static void devm_devfreq_dev_release(struct device *dev, void *res)
782 devfreq_remove_device(*(struct devfreq **)res);
786 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
787 * @dev: the device to add devfreq feature.
788 * @profile: device-specific profile to run devfreq.
789 * @governor_name: name of the policy to choose frequency.
790 * @data: devfreq driver pass to governors, governor should not change it.
792 * This function manages automatically the memory of devfreq device using device
793 * resource management and simplify the free operation for memory of devfreq
796 struct devfreq *devm_devfreq_add_device(struct device *dev,
797 struct devfreq_dev_profile *profile,
798 const char *governor_name,
801 struct devfreq **ptr, *devfreq;
803 ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
805 return ERR_PTR(-ENOMEM);
807 devfreq = devfreq_add_device(dev, profile, governor_name, data);
808 if (IS_ERR(devfreq)) {
814 devres_add(dev, ptr);
818 EXPORT_SYMBOL(devm_devfreq_add_device);
822 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
823 * @dev - instance to the given device
824 * @index - index into list of devfreq
826 * return the instance of devfreq device
828 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
830 struct device_node *node;
831 struct devfreq *devfreq;
834 return ERR_PTR(-EINVAL);
837 return ERR_PTR(-EINVAL);
839 node = of_parse_phandle(dev->of_node, "devfreq", index);
841 return ERR_PTR(-ENODEV);
843 mutex_lock(&devfreq_list_lock);
844 list_for_each_entry(devfreq, &devfreq_list, node) {
845 if (devfreq->dev.parent
846 && devfreq->dev.parent->of_node == node) {
847 mutex_unlock(&devfreq_list_lock);
852 mutex_unlock(&devfreq_list_lock);
855 return ERR_PTR(-EPROBE_DEFER);
858 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
860 return ERR_PTR(-ENODEV);
862 #endif /* CONFIG_OF */
863 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
866 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
867 * @dev: the device from which to remove devfreq feature.
868 * @devfreq: the devfreq instance to be removed
870 void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
872 WARN_ON(devres_release(dev, devm_devfreq_dev_release,
873 devm_devfreq_dev_match, devfreq));
875 EXPORT_SYMBOL(devm_devfreq_remove_device);
878 * devfreq_suspend_device() - Suspend devfreq of a device.
879 * @devfreq: the devfreq instance to be suspended
881 * This function is intended to be called by the pm callbacks
882 * (e.g., runtime_suspend, suspend) of the device driver that
885 int devfreq_suspend_device(struct devfreq *devfreq)
892 if (atomic_inc_return(&devfreq->suspend_count) > 1)
895 if (devfreq->governor) {
896 ret = devfreq->governor->event_handler(devfreq,
897 DEVFREQ_GOV_SUSPEND, NULL);
902 if (devfreq->suspend_freq) {
903 mutex_lock(&devfreq->lock);
904 ret = devfreq_set_target(devfreq, devfreq->suspend_freq, 0);
905 mutex_unlock(&devfreq->lock);
912 EXPORT_SYMBOL(devfreq_suspend_device);
915 * devfreq_resume_device() - Resume devfreq of a device.
916 * @devfreq: the devfreq instance to be resumed
918 * This function is intended to be called by the pm callbacks
919 * (e.g., runtime_resume, resume) of the device driver that
922 int devfreq_resume_device(struct devfreq *devfreq)
929 if (atomic_dec_return(&devfreq->suspend_count) >= 1)
932 if (devfreq->resume_freq) {
933 mutex_lock(&devfreq->lock);
934 ret = devfreq_set_target(devfreq, devfreq->resume_freq, 0);
935 mutex_unlock(&devfreq->lock);
940 if (devfreq->governor) {
941 ret = devfreq->governor->event_handler(devfreq,
942 DEVFREQ_GOV_RESUME, NULL);
949 EXPORT_SYMBOL(devfreq_resume_device);
952 * devfreq_suspend() - Suspend devfreq governors and devices
954 * Called during system wide Suspend/Hibernate cycles for suspending governors
955 * and devices preserving the state for resume. On some platforms the devfreq
956 * device must have precise state (frequency) after resume in order to provide
957 * fully operating setup.
959 void devfreq_suspend(void)
961 struct devfreq *devfreq;
964 mutex_lock(&devfreq_list_lock);
965 list_for_each_entry(devfreq, &devfreq_list, node) {
966 ret = devfreq_suspend_device(devfreq);
968 dev_err(&devfreq->dev,
969 "failed to suspend devfreq device\n");
971 mutex_unlock(&devfreq_list_lock);
975 * devfreq_resume() - Resume devfreq governors and devices
977 * Called during system wide Suspend/Hibernate cycle for resuming governors and
978 * devices that are suspended with devfreq_suspend().
980 void devfreq_resume(void)
982 struct devfreq *devfreq;
985 mutex_lock(&devfreq_list_lock);
986 list_for_each_entry(devfreq, &devfreq_list, node) {
987 ret = devfreq_resume_device(devfreq);
989 dev_warn(&devfreq->dev,
990 "failed to resume devfreq device\n");
992 mutex_unlock(&devfreq_list_lock);
996 * devfreq_add_governor() - Add devfreq governor
997 * @governor: the devfreq governor to be added
999 int devfreq_add_governor(struct devfreq_governor *governor)
1001 struct devfreq_governor *g;
1002 struct devfreq *devfreq;
1006 pr_err("%s: Invalid parameters.\n", __func__);
1010 mutex_lock(&devfreq_list_lock);
1011 g = find_devfreq_governor(governor->name);
1013 pr_err("%s: governor %s already registered\n", __func__,
1019 list_add(&governor->node, &devfreq_governor_list);
1021 list_for_each_entry(devfreq, &devfreq_list, node) {
1023 struct device *dev = devfreq->dev.parent;
1025 if (!strncmp(devfreq->governor_name, governor->name,
1026 DEVFREQ_NAME_LEN)) {
1027 /* The following should never occur */
1028 if (devfreq->governor) {
1030 "%s: Governor %s already present\n",
1031 __func__, devfreq->governor->name);
1032 ret = devfreq->governor->event_handler(devfreq,
1033 DEVFREQ_GOV_STOP, NULL);
1036 "%s: Governor %s stop = %d\n",
1038 devfreq->governor->name, ret);
1042 devfreq->governor = governor;
1043 ret = devfreq->governor->event_handler(devfreq,
1044 DEVFREQ_GOV_START, NULL);
1046 dev_warn(dev, "%s: Governor %s start=%d\n",
1047 __func__, devfreq->governor->name,
1054 mutex_unlock(&devfreq_list_lock);
1058 EXPORT_SYMBOL(devfreq_add_governor);
1061 * devfreq_remove_governor() - Remove devfreq feature from a device.
1062 * @governor: the devfreq governor to be removed
1064 int devfreq_remove_governor(struct devfreq_governor *governor)
1066 struct devfreq_governor *g;
1067 struct devfreq *devfreq;
1071 pr_err("%s: Invalid parameters.\n", __func__);
1075 mutex_lock(&devfreq_list_lock);
1076 g = find_devfreq_governor(governor->name);
1078 pr_err("%s: governor %s not registered\n", __func__,
1083 list_for_each_entry(devfreq, &devfreq_list, node) {
1085 struct device *dev = devfreq->dev.parent;
1087 if (!strncmp(devfreq->governor_name, governor->name,
1088 DEVFREQ_NAME_LEN)) {
1089 /* we should have a devfreq governor! */
1090 if (!devfreq->governor) {
1091 dev_warn(dev, "%s: Governor %s NOT present\n",
1092 __func__, governor->name);
1096 ret = devfreq->governor->event_handler(devfreq,
1097 DEVFREQ_GOV_STOP, NULL);
1099 dev_warn(dev, "%s: Governor %s stop=%d\n",
1100 __func__, devfreq->governor->name,
1103 devfreq->governor = NULL;
1107 list_del(&governor->node);
1109 mutex_unlock(&devfreq_list_lock);
1113 EXPORT_SYMBOL(devfreq_remove_governor);
1115 static ssize_t name_show(struct device *dev,
1116 struct device_attribute *attr, char *buf)
1118 struct devfreq *devfreq = to_devfreq(dev);
1119 return sprintf(buf, "%s\n", dev_name(devfreq->dev.parent));
1121 static DEVICE_ATTR_RO(name);
1123 static ssize_t governor_show(struct device *dev,
1124 struct device_attribute *attr, char *buf)
1126 if (!to_devfreq(dev)->governor)
1129 return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
1132 static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
1133 const char *buf, size_t count)
1135 struct devfreq *df = to_devfreq(dev);
1137 char str_governor[DEVFREQ_NAME_LEN + 1];
1138 const struct devfreq_governor *governor, *prev_governor;
1140 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
1144 mutex_lock(&devfreq_list_lock);
1145 governor = try_then_request_governor(str_governor);
1146 if (IS_ERR(governor)) {
1147 ret = PTR_ERR(governor);
1150 if (df->governor == governor) {
1153 } else if ((df->governor && df->governor->immutable) ||
1154 governor->immutable) {
1160 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1162 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1163 __func__, df->governor->name, ret);
1167 prev_governor = df->governor;
1168 df->governor = governor;
1169 strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
1170 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1172 dev_warn(dev, "%s: Governor %s not started(%d)\n",
1173 __func__, df->governor->name, ret);
1174 df->governor = prev_governor;
1175 strncpy(df->governor_name, prev_governor->name,
1177 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1180 "%s: reverting to Governor %s failed (%d)\n",
1181 __func__, df->governor_name, ret);
1182 df->governor = NULL;
1186 mutex_unlock(&devfreq_list_lock);
1192 static DEVICE_ATTR_RW(governor);
1194 static ssize_t available_governors_show(struct device *d,
1195 struct device_attribute *attr,
1198 struct devfreq *df = to_devfreq(d);
1201 mutex_lock(&devfreq_list_lock);
1204 * The devfreq with immutable governor (e.g., passive) shows
1205 * only own governor.
1207 if (df->governor && df->governor->immutable) {
1208 count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
1209 "%s ", df->governor_name);
1211 * The devfreq device shows the registered governor except for
1212 * immutable governors such as passive governor .
1215 struct devfreq_governor *governor;
1217 list_for_each_entry(governor, &devfreq_governor_list, node) {
1218 if (governor->immutable)
1220 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1221 "%s ", governor->name);
1225 mutex_unlock(&devfreq_list_lock);
1227 /* Truncate the trailing space */
1231 count += sprintf(&buf[count], "\n");
1235 static DEVICE_ATTR_RO(available_governors);
1237 static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
1241 struct devfreq *devfreq = to_devfreq(dev);
1243 if (devfreq->profile->get_cur_freq &&
1244 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
1245 return sprintf(buf, "%lu\n", freq);
1247 return sprintf(buf, "%lu\n", devfreq->previous_freq);
1249 static DEVICE_ATTR_RO(cur_freq);
1251 static ssize_t target_freq_show(struct device *dev,
1252 struct device_attribute *attr, char *buf)
1254 return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
1256 static DEVICE_ATTR_RO(target_freq);
1258 static ssize_t polling_interval_show(struct device *dev,
1259 struct device_attribute *attr, char *buf)
1261 return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
1264 static ssize_t polling_interval_store(struct device *dev,
1265 struct device_attribute *attr,
1266 const char *buf, size_t count)
1268 struct devfreq *df = to_devfreq(dev);
1275 ret = sscanf(buf, "%u", &value);
1279 df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
1284 static DEVICE_ATTR_RW(polling_interval);
1286 static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
1287 const char *buf, size_t count)
1289 struct devfreq *df = to_devfreq(dev);
1290 unsigned long value;
1293 ret = sscanf(buf, "%lu", &value);
1297 mutex_lock(&df->lock);
1300 if (value > df->max_freq) {
1305 unsigned long *freq_table = df->profile->freq_table;
1307 /* Get minimum frequency according to sorting order */
1308 if (freq_table[0] < freq_table[df->profile->max_state - 1])
1309 value = freq_table[0];
1311 value = freq_table[df->profile->max_state - 1];
1314 df->min_freq = value;
1318 mutex_unlock(&df->lock);
1322 static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
1325 struct devfreq *df = to_devfreq(dev);
1327 return sprintf(buf, "%lu\n", max(df->scaling_min_freq, df->min_freq));
1330 static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
1331 const char *buf, size_t count)
1333 struct devfreq *df = to_devfreq(dev);
1334 unsigned long value;
1337 ret = sscanf(buf, "%lu", &value);
1341 mutex_lock(&df->lock);
1344 if (value < df->min_freq) {
1349 unsigned long *freq_table = df->profile->freq_table;
1351 /* Get maximum frequency according to sorting order */
1352 if (freq_table[0] < freq_table[df->profile->max_state - 1])
1353 value = freq_table[df->profile->max_state - 1];
1355 value = freq_table[0];
1358 df->max_freq = value;
1362 mutex_unlock(&df->lock);
1365 static DEVICE_ATTR_RW(min_freq);
1367 static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
1370 struct devfreq *df = to_devfreq(dev);
1372 return sprintf(buf, "%lu\n", min(df->scaling_max_freq, df->max_freq));
1374 static DEVICE_ATTR_RW(max_freq);
1376 static ssize_t available_frequencies_show(struct device *d,
1377 struct device_attribute *attr,
1380 struct devfreq *df = to_devfreq(d);
1384 mutex_lock(&df->lock);
1386 for (i = 0; i < df->profile->max_state; i++)
1387 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1388 "%lu ", df->profile->freq_table[i]);
1390 mutex_unlock(&df->lock);
1391 /* Truncate the trailing space */
1395 count += sprintf(&buf[count], "\n");
1399 static DEVICE_ATTR_RO(available_frequencies);
1401 static ssize_t trans_stat_show(struct device *dev,
1402 struct device_attribute *attr, char *buf)
1404 struct devfreq *devfreq = to_devfreq(dev);
1407 unsigned int max_state = devfreq->profile->max_state;
1410 return sprintf(buf, "Not Supported.\n");
1412 mutex_lock(&devfreq->lock);
1413 if (!devfreq->stop_polling &&
1414 devfreq_update_status(devfreq, devfreq->previous_freq)) {
1415 mutex_unlock(&devfreq->lock);
1418 mutex_unlock(&devfreq->lock);
1420 len = sprintf(buf, " From : To\n");
1421 len += sprintf(buf + len, " :");
1422 for (i = 0; i < max_state; i++)
1423 len += sprintf(buf + len, "%10lu",
1424 devfreq->profile->freq_table[i]);
1426 len += sprintf(buf + len, " time(ms)\n");
1428 for (i = 0; i < max_state; i++) {
1429 if (devfreq->profile->freq_table[i]
1430 == devfreq->previous_freq) {
1431 len += sprintf(buf + len, "*");
1433 len += sprintf(buf + len, " ");
1435 len += sprintf(buf + len, "%10lu:",
1436 devfreq->profile->freq_table[i]);
1437 for (j = 0; j < max_state; j++)
1438 len += sprintf(buf + len, "%10u",
1439 devfreq->trans_table[(i * max_state) + j]);
1440 len += sprintf(buf + len, "%10u\n",
1441 jiffies_to_msecs(devfreq->time_in_state[i]));
1444 len += sprintf(buf + len, "Total transition : %u\n",
1445 devfreq->total_trans);
1448 static DEVICE_ATTR_RO(trans_stat);
1450 static struct attribute *devfreq_attrs[] = {
1451 &dev_attr_name.attr,
1452 &dev_attr_governor.attr,
1453 &dev_attr_available_governors.attr,
1454 &dev_attr_cur_freq.attr,
1455 &dev_attr_available_frequencies.attr,
1456 &dev_attr_target_freq.attr,
1457 &dev_attr_polling_interval.attr,
1458 &dev_attr_min_freq.attr,
1459 &dev_attr_max_freq.attr,
1460 &dev_attr_trans_stat.attr,
1463 ATTRIBUTE_GROUPS(devfreq);
1465 static int __init devfreq_init(void)
1467 devfreq_class = class_create(THIS_MODULE, "devfreq");
1468 if (IS_ERR(devfreq_class)) {
1469 pr_err("%s: couldn't create class\n", __FILE__);
1470 return PTR_ERR(devfreq_class);
1473 devfreq_wq = create_freezable_workqueue("devfreq_wq");
1475 class_destroy(devfreq_class);
1476 pr_err("%s: couldn't create workqueue\n", __FILE__);
1479 devfreq_class->dev_groups = devfreq_groups;
1483 subsys_initcall(devfreq_init);
1486 * The following are helper functions for devfreq user device drivers with
1491 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1492 * freq value given to target callback.
1493 * @dev: The devfreq user device. (parent of devfreq)
1494 * @freq: The frequency given to target function
1495 * @flags: Flags handed from devfreq framework.
1497 * The callers are required to call dev_pm_opp_put() for the returned OPP after
1500 struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1501 unsigned long *freq,
1504 struct dev_pm_opp *opp;
1506 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1507 /* The freq is an upper bound. opp should be lower */
1508 opp = dev_pm_opp_find_freq_floor(dev, freq);
1510 /* If not available, use the closest opp */
1511 if (opp == ERR_PTR(-ERANGE))
1512 opp = dev_pm_opp_find_freq_ceil(dev, freq);
1514 /* The freq is an lower bound. opp should be higher */
1515 opp = dev_pm_opp_find_freq_ceil(dev, freq);
1517 /* If not available, use the closest opp */
1518 if (opp == ERR_PTR(-ERANGE))
1519 opp = dev_pm_opp_find_freq_floor(dev, freq);
1524 EXPORT_SYMBOL(devfreq_recommended_opp);
1527 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1528 * for any changes in the OPP availability
1530 * @dev: The devfreq user device. (parent of devfreq)
1531 * @devfreq: The devfreq object.
1533 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1535 return dev_pm_opp_register_notifier(dev, &devfreq->nb);
1537 EXPORT_SYMBOL(devfreq_register_opp_notifier);
1540 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1541 * notified for any changes in the OPP
1542 * availability changes anymore.
1543 * @dev: The devfreq user device. (parent of devfreq)
1544 * @devfreq: The devfreq object.
1546 * At exit() callback of devfreq_dev_profile, this must be included if
1547 * devfreq_recommended_opp is used.
1549 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1551 return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
1553 EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
1555 static void devm_devfreq_opp_release(struct device *dev, void *res)
1557 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
1561 * devm_devfreq_register_opp_notifier() - Resource-managed
1562 * devfreq_register_opp_notifier()
1563 * @dev: The devfreq user device. (parent of devfreq)
1564 * @devfreq: The devfreq object.
1566 int devm_devfreq_register_opp_notifier(struct device *dev,
1567 struct devfreq *devfreq)
1569 struct devfreq **ptr;
1572 ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
1576 ret = devfreq_register_opp_notifier(dev, devfreq);
1583 devres_add(dev, ptr);
1587 EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
1590 * devm_devfreq_unregister_opp_notifier() - Resource-managed
1591 * devfreq_unregister_opp_notifier()
1592 * @dev: The devfreq user device. (parent of devfreq)
1593 * @devfreq: The devfreq object.
1595 void devm_devfreq_unregister_opp_notifier(struct device *dev,
1596 struct devfreq *devfreq)
1598 WARN_ON(devres_release(dev, devm_devfreq_opp_release,
1599 devm_devfreq_dev_match, devfreq));
1601 EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
1604 * devfreq_register_notifier() - Register a driver with devfreq
1605 * @devfreq: The devfreq object.
1606 * @nb: The notifier block to register.
1607 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1609 int devfreq_register_notifier(struct devfreq *devfreq,
1610 struct notifier_block *nb,
1619 case DEVFREQ_TRANSITION_NOTIFIER:
1620 ret = srcu_notifier_chain_register(
1621 &devfreq->transition_notifier_list, nb);
1629 EXPORT_SYMBOL(devfreq_register_notifier);
1632 * devfreq_unregister_notifier() - Unregister a driver with devfreq
1633 * @devfreq: The devfreq object.
1634 * @nb: The notifier block to be unregistered.
1635 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1637 int devfreq_unregister_notifier(struct devfreq *devfreq,
1638 struct notifier_block *nb,
1647 case DEVFREQ_TRANSITION_NOTIFIER:
1648 ret = srcu_notifier_chain_unregister(
1649 &devfreq->transition_notifier_list, nb);
1657 EXPORT_SYMBOL(devfreq_unregister_notifier);
1659 struct devfreq_notifier_devres {
1660 struct devfreq *devfreq;
1661 struct notifier_block *nb;
1665 static void devm_devfreq_notifier_release(struct device *dev, void *res)
1667 struct devfreq_notifier_devres *this = res;
1669 devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
1673 * devm_devfreq_register_notifier()
1674 - Resource-managed devfreq_register_notifier()
1675 * @dev: The devfreq user device. (parent of devfreq)
1676 * @devfreq: The devfreq object.
1677 * @nb: The notifier block to be unregistered.
1678 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1680 int devm_devfreq_register_notifier(struct device *dev,
1681 struct devfreq *devfreq,
1682 struct notifier_block *nb,
1685 struct devfreq_notifier_devres *ptr;
1688 ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
1693 ret = devfreq_register_notifier(devfreq, nb, list);
1699 ptr->devfreq = devfreq;
1702 devres_add(dev, ptr);
1706 EXPORT_SYMBOL(devm_devfreq_register_notifier);
1709 * devm_devfreq_unregister_notifier()
1710 - Resource-managed devfreq_unregister_notifier()
1711 * @dev: The devfreq user device. (parent of devfreq)
1712 * @devfreq: The devfreq object.
1713 * @nb: The notifier block to be unregistered.
1714 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1716 void devm_devfreq_unregister_notifier(struct device *dev,
1717 struct devfreq *devfreq,
1718 struct notifier_block *nb,
1721 WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
1722 devm_devfreq_dev_match, devfreq));
1724 EXPORT_SYMBOL(devm_devfreq_unregister_notifier);