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 = cur_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 if (devfreq->governor->interrupt_driven)
416 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
417 if (devfreq->profile->polling_ms)
418 queue_delayed_work(devfreq_wq, &devfreq->work,
419 msecs_to_jiffies(devfreq->profile->polling_ms));
421 EXPORT_SYMBOL(devfreq_monitor_start);
424 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
425 * @devfreq: the devfreq instance.
427 * Helper function to stop devfreq device load monitoring. Function
428 * to be called from governor in response to DEVFREQ_GOV_STOP
429 * event when device is removed from devfreq framework.
431 void devfreq_monitor_stop(struct devfreq *devfreq)
433 if (devfreq->governor->interrupt_driven)
436 cancel_delayed_work_sync(&devfreq->work);
438 EXPORT_SYMBOL(devfreq_monitor_stop);
441 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
442 * @devfreq: the devfreq instance.
444 * Helper function to suspend devfreq device load monitoring. Function
445 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
446 * event or when polling interval is set to zero.
448 * Note: Though this function is same as devfreq_monitor_stop(),
449 * intentionally kept separate to provide hooks for collecting
450 * transition statistics.
452 void devfreq_monitor_suspend(struct devfreq *devfreq)
454 mutex_lock(&devfreq->lock);
455 if (devfreq->stop_polling) {
456 mutex_unlock(&devfreq->lock);
460 devfreq_update_status(devfreq, devfreq->previous_freq);
461 devfreq->stop_polling = true;
462 mutex_unlock(&devfreq->lock);
464 if (devfreq->governor->interrupt_driven)
467 cancel_delayed_work_sync(&devfreq->work);
469 EXPORT_SYMBOL(devfreq_monitor_suspend);
472 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
473 * @devfreq: the devfreq instance.
475 * Helper function to resume devfreq device load monitoring. Function
476 * to be called from governor in response to DEVFREQ_GOV_RESUME
477 * event or when polling interval is set to non-zero.
479 void devfreq_monitor_resume(struct devfreq *devfreq)
483 mutex_lock(&devfreq->lock);
484 if (!devfreq->stop_polling)
487 if (devfreq->governor->interrupt_driven)
490 if (!delayed_work_pending(&devfreq->work) &&
491 devfreq->profile->polling_ms)
492 queue_delayed_work(devfreq_wq, &devfreq->work,
493 msecs_to_jiffies(devfreq->profile->polling_ms));
496 devfreq->last_stat_updated = jiffies;
497 devfreq->stop_polling = false;
499 if (devfreq->profile->get_cur_freq &&
500 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
501 devfreq->previous_freq = freq;
504 mutex_unlock(&devfreq->lock);
506 EXPORT_SYMBOL(devfreq_monitor_resume);
509 * devfreq_interval_update() - Update device devfreq monitoring interval
510 * @devfreq: the devfreq instance.
511 * @delay: new polling interval to be set.
513 * Helper function to set new load monitoring polling interval. Function
514 * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
516 void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
518 unsigned int cur_delay = devfreq->profile->polling_ms;
519 unsigned int new_delay = *delay;
521 mutex_lock(&devfreq->lock);
522 devfreq->profile->polling_ms = new_delay;
524 if (devfreq->stop_polling)
527 if (devfreq->governor->interrupt_driven)
530 /* if new delay is zero, stop polling */
532 mutex_unlock(&devfreq->lock);
533 cancel_delayed_work_sync(&devfreq->work);
537 /* if current delay is zero, start polling with new delay */
539 queue_delayed_work(devfreq_wq, &devfreq->work,
540 msecs_to_jiffies(devfreq->profile->polling_ms));
544 /* if current delay is greater than new delay, restart polling */
545 if (cur_delay > new_delay) {
546 mutex_unlock(&devfreq->lock);
547 cancel_delayed_work_sync(&devfreq->work);
548 mutex_lock(&devfreq->lock);
549 if (!devfreq->stop_polling)
550 queue_delayed_work(devfreq_wq, &devfreq->work,
551 msecs_to_jiffies(devfreq->profile->polling_ms));
554 mutex_unlock(&devfreq->lock);
556 EXPORT_SYMBOL(devfreq_interval_update);
559 * devfreq_notifier_call() - Notify that the device frequency requirements
560 * has been changed out of devfreq framework.
561 * @nb: the notifier_block (supposed to be devfreq->nb)
565 * Called by a notifier that uses devfreq->nb.
567 static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
570 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
573 mutex_lock(&devfreq->lock);
575 devfreq->scaling_min_freq = find_available_min_freq(devfreq);
576 if (!devfreq->scaling_min_freq) {
577 mutex_unlock(&devfreq->lock);
581 devfreq->scaling_max_freq = find_available_max_freq(devfreq);
582 if (!devfreq->scaling_max_freq) {
583 mutex_unlock(&devfreq->lock);
587 ret = update_devfreq(devfreq);
588 mutex_unlock(&devfreq->lock);
594 * devfreq_dev_release() - Callback for struct device to release the device.
595 * @dev: the devfreq device
597 * Remove devfreq from the list and release its resources.
599 static void devfreq_dev_release(struct device *dev)
601 struct devfreq *devfreq = to_devfreq(dev);
603 mutex_lock(&devfreq_list_lock);
604 if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
605 mutex_unlock(&devfreq_list_lock);
606 dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
609 list_del(&devfreq->node);
610 mutex_unlock(&devfreq_list_lock);
612 if (devfreq->profile->exit)
613 devfreq->profile->exit(devfreq->dev.parent);
615 mutex_destroy(&devfreq->lock);
620 * devfreq_add_device() - Add devfreq feature to the device
621 * @dev: the device to add devfreq feature.
622 * @profile: device-specific profile to run devfreq.
623 * @governor_name: name of the policy to choose frequency.
624 * @data: private data for the governor. The devfreq framework does not
627 struct devfreq *devfreq_add_device(struct device *dev,
628 struct devfreq_dev_profile *profile,
629 const char *governor_name,
632 struct devfreq *devfreq;
633 struct devfreq_governor *governor;
634 static atomic_t devfreq_no = ATOMIC_INIT(-1);
637 if (!dev || !profile || !governor_name) {
638 dev_err(dev, "%s: Invalid parameters.\n", __func__);
639 return ERR_PTR(-EINVAL);
642 mutex_lock(&devfreq_list_lock);
643 devfreq = find_device_devfreq(dev);
644 mutex_unlock(&devfreq_list_lock);
645 if (!IS_ERR(devfreq)) {
646 dev_err(dev, "%s: devfreq device already exists!\n",
652 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
658 mutex_init(&devfreq->lock);
659 mutex_lock(&devfreq->lock);
660 devfreq->dev.parent = dev;
661 devfreq->dev.class = devfreq_class;
662 devfreq->dev.release = devfreq_dev_release;
663 devfreq->profile = profile;
664 strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
665 devfreq->previous_freq = profile->initial_freq;
666 devfreq->last_status.current_frequency = profile->initial_freq;
667 devfreq->data = data;
668 devfreq->nb.notifier_call = devfreq_notifier_call;
670 if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
671 mutex_unlock(&devfreq->lock);
672 err = set_freq_table(devfreq);
675 mutex_lock(&devfreq->lock);
678 devfreq->scaling_min_freq = find_available_min_freq(devfreq);
679 if (!devfreq->scaling_min_freq) {
680 mutex_unlock(&devfreq->lock);
684 devfreq->min_freq = devfreq->scaling_min_freq;
686 devfreq->scaling_max_freq = find_available_max_freq(devfreq);
687 if (!devfreq->scaling_max_freq) {
688 mutex_unlock(&devfreq->lock);
692 devfreq->max_freq = devfreq->scaling_max_freq;
694 devfreq->suspend_freq = dev_pm_opp_get_suspend_opp_freq(dev);
695 atomic_set(&devfreq->suspend_count, 0);
697 dev_set_name(&devfreq->dev, "devfreq%d",
698 atomic_inc_return(&devfreq_no));
699 err = device_register(&devfreq->dev);
701 mutex_unlock(&devfreq->lock);
702 put_device(&devfreq->dev);
706 devfreq->trans_table = devm_kzalloc(&devfreq->dev,
707 array3_size(sizeof(unsigned int),
708 devfreq->profile->max_state,
709 devfreq->profile->max_state),
711 if (!devfreq->trans_table) {
712 mutex_unlock(&devfreq->lock);
717 devfreq->time_in_state = devm_kcalloc(&devfreq->dev,
718 devfreq->profile->max_state,
719 sizeof(unsigned long),
721 if (!devfreq->time_in_state) {
722 mutex_unlock(&devfreq->lock);
727 devfreq->last_stat_updated = jiffies;
729 srcu_init_notifier_head(&devfreq->transition_notifier_list);
731 mutex_unlock(&devfreq->lock);
733 mutex_lock(&devfreq_list_lock);
735 governor = try_then_request_governor(devfreq->governor_name);
736 if (IS_ERR(governor)) {
737 dev_err(dev, "%s: Unable to find governor for the device\n",
739 err = PTR_ERR(governor);
743 devfreq->governor = governor;
744 err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
747 dev_err(dev, "%s: Unable to start governor for the device\n",
752 list_add(&devfreq->node, &devfreq_list);
754 mutex_unlock(&devfreq_list_lock);
759 mutex_unlock(&devfreq_list_lock);
761 devfreq_remove_device(devfreq);
768 EXPORT_SYMBOL(devfreq_add_device);
771 * devfreq_remove_device() - Remove devfreq feature from a device.
772 * @devfreq: the devfreq instance to be removed
774 * The opposite of devfreq_add_device().
776 int devfreq_remove_device(struct devfreq *devfreq)
781 if (devfreq->governor)
782 devfreq->governor->event_handler(devfreq,
783 DEVFREQ_GOV_STOP, NULL);
784 device_unregister(&devfreq->dev);
788 EXPORT_SYMBOL(devfreq_remove_device);
790 static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
792 struct devfreq **r = res;
794 if (WARN_ON(!r || !*r))
800 static void devm_devfreq_dev_release(struct device *dev, void *res)
802 devfreq_remove_device(*(struct devfreq **)res);
806 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
807 * @dev: the device to add devfreq feature.
808 * @profile: device-specific profile to run devfreq.
809 * @governor_name: name of the policy to choose frequency.
810 * @data: private data for the governor. The devfreq framework does not
813 * This function manages automatically the memory of devfreq device using device
814 * resource management and simplify the free operation for memory of devfreq
817 struct devfreq *devm_devfreq_add_device(struct device *dev,
818 struct devfreq_dev_profile *profile,
819 const char *governor_name,
822 struct devfreq **ptr, *devfreq;
824 ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
826 return ERR_PTR(-ENOMEM);
828 devfreq = devfreq_add_device(dev, profile, governor_name, data);
829 if (IS_ERR(devfreq)) {
835 devres_add(dev, ptr);
839 EXPORT_SYMBOL(devm_devfreq_add_device);
843 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
844 * @dev - instance to the given device
845 * @index - index into list of devfreq
847 * return the instance of devfreq device
849 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
851 struct device_node *node;
852 struct devfreq *devfreq;
855 return ERR_PTR(-EINVAL);
858 return ERR_PTR(-EINVAL);
860 node = of_parse_phandle(dev->of_node, "devfreq", index);
862 return ERR_PTR(-ENODEV);
864 mutex_lock(&devfreq_list_lock);
865 list_for_each_entry(devfreq, &devfreq_list, node) {
866 if (devfreq->dev.parent
867 && devfreq->dev.parent->of_node == node) {
868 mutex_unlock(&devfreq_list_lock);
873 mutex_unlock(&devfreq_list_lock);
876 return ERR_PTR(-EPROBE_DEFER);
879 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
881 return ERR_PTR(-ENODEV);
883 #endif /* CONFIG_OF */
884 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
887 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
888 * @dev: the device from which to remove devfreq feature.
889 * @devfreq: the devfreq instance to be removed
891 void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
893 WARN_ON(devres_release(dev, devm_devfreq_dev_release,
894 devm_devfreq_dev_match, devfreq));
896 EXPORT_SYMBOL(devm_devfreq_remove_device);
899 * devfreq_suspend_device() - Suspend devfreq of a device.
900 * @devfreq: the devfreq instance to be suspended
902 * This function is intended to be called by the pm callbacks
903 * (e.g., runtime_suspend, suspend) of the device driver that
906 int devfreq_suspend_device(struct devfreq *devfreq)
913 if (atomic_inc_return(&devfreq->suspend_count) > 1)
916 if (devfreq->governor) {
917 ret = devfreq->governor->event_handler(devfreq,
918 DEVFREQ_GOV_SUSPEND, NULL);
923 if (devfreq->suspend_freq) {
924 ret = devfreq_set_target(devfreq, devfreq->suspend_freq, 0);
931 EXPORT_SYMBOL(devfreq_suspend_device);
934 * devfreq_resume_device() - Resume devfreq of a device.
935 * @devfreq: the devfreq instance to be resumed
937 * This function is intended to be called by the pm callbacks
938 * (e.g., runtime_resume, resume) of the device driver that
941 int devfreq_resume_device(struct devfreq *devfreq)
948 if (atomic_dec_return(&devfreq->suspend_count) >= 1)
951 if (devfreq->resume_freq) {
952 ret = devfreq_set_target(devfreq, devfreq->resume_freq, 0);
957 if (devfreq->governor) {
958 ret = devfreq->governor->event_handler(devfreq,
959 DEVFREQ_GOV_RESUME, NULL);
966 EXPORT_SYMBOL(devfreq_resume_device);
969 * devfreq_suspend() - Suspend devfreq governors and devices
971 * Called during system wide Suspend/Hibernate cycles for suspending governors
972 * and devices preserving the state for resume. On some platforms the devfreq
973 * device must have precise state (frequency) after resume in order to provide
974 * fully operating setup.
976 void devfreq_suspend(void)
978 struct devfreq *devfreq;
981 mutex_lock(&devfreq_list_lock);
982 list_for_each_entry(devfreq, &devfreq_list, node) {
983 ret = devfreq_suspend_device(devfreq);
985 dev_err(&devfreq->dev,
986 "failed to suspend devfreq device\n");
988 mutex_unlock(&devfreq_list_lock);
992 * devfreq_resume() - Resume devfreq governors and devices
994 * Called during system wide Suspend/Hibernate cycle for resuming governors and
995 * devices that are suspended with devfreq_suspend().
997 void devfreq_resume(void)
999 struct devfreq *devfreq;
1002 mutex_lock(&devfreq_list_lock);
1003 list_for_each_entry(devfreq, &devfreq_list, node) {
1004 ret = devfreq_resume_device(devfreq);
1006 dev_warn(&devfreq->dev,
1007 "failed to resume devfreq device\n");
1009 mutex_unlock(&devfreq_list_lock);
1013 * devfreq_add_governor() - Add devfreq governor
1014 * @governor: the devfreq governor to be added
1016 int devfreq_add_governor(struct devfreq_governor *governor)
1018 struct devfreq_governor *g;
1019 struct devfreq *devfreq;
1023 pr_err("%s: Invalid parameters.\n", __func__);
1027 mutex_lock(&devfreq_list_lock);
1028 g = find_devfreq_governor(governor->name);
1030 pr_err("%s: governor %s already registered\n", __func__,
1036 list_add(&governor->node, &devfreq_governor_list);
1038 list_for_each_entry(devfreq, &devfreq_list, node) {
1040 struct device *dev = devfreq->dev.parent;
1042 if (!strncmp(devfreq->governor_name, governor->name,
1043 DEVFREQ_NAME_LEN)) {
1044 /* The following should never occur */
1045 if (devfreq->governor) {
1047 "%s: Governor %s already present\n",
1048 __func__, devfreq->governor->name);
1049 ret = devfreq->governor->event_handler(devfreq,
1050 DEVFREQ_GOV_STOP, NULL);
1053 "%s: Governor %s stop = %d\n",
1055 devfreq->governor->name, ret);
1059 devfreq->governor = governor;
1060 ret = devfreq->governor->event_handler(devfreq,
1061 DEVFREQ_GOV_START, NULL);
1063 dev_warn(dev, "%s: Governor %s start=%d\n",
1064 __func__, devfreq->governor->name,
1071 mutex_unlock(&devfreq_list_lock);
1075 EXPORT_SYMBOL(devfreq_add_governor);
1078 * devfreq_remove_governor() - Remove devfreq feature from a device.
1079 * @governor: the devfreq governor to be removed
1081 int devfreq_remove_governor(struct devfreq_governor *governor)
1083 struct devfreq_governor *g;
1084 struct devfreq *devfreq;
1088 pr_err("%s: Invalid parameters.\n", __func__);
1092 mutex_lock(&devfreq_list_lock);
1093 g = find_devfreq_governor(governor->name);
1095 pr_err("%s: governor %s not registered\n", __func__,
1100 list_for_each_entry(devfreq, &devfreq_list, node) {
1102 struct device *dev = devfreq->dev.parent;
1104 if (!strncmp(devfreq->governor_name, governor->name,
1105 DEVFREQ_NAME_LEN)) {
1106 /* we should have a devfreq governor! */
1107 if (!devfreq->governor) {
1108 dev_warn(dev, "%s: Governor %s NOT present\n",
1109 __func__, governor->name);
1113 ret = devfreq->governor->event_handler(devfreq,
1114 DEVFREQ_GOV_STOP, NULL);
1116 dev_warn(dev, "%s: Governor %s stop=%d\n",
1117 __func__, devfreq->governor->name,
1120 devfreq->governor = NULL;
1124 list_del(&governor->node);
1126 mutex_unlock(&devfreq_list_lock);
1130 EXPORT_SYMBOL(devfreq_remove_governor);
1132 static ssize_t governor_show(struct device *dev,
1133 struct device_attribute *attr, char *buf)
1135 if (!to_devfreq(dev)->governor)
1138 return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
1141 static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
1142 const char *buf, size_t count)
1144 struct devfreq *df = to_devfreq(dev);
1146 char str_governor[DEVFREQ_NAME_LEN + 1];
1147 const struct devfreq_governor *governor, *prev_governor;
1149 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
1153 mutex_lock(&devfreq_list_lock);
1154 governor = try_then_request_governor(str_governor);
1155 if (IS_ERR(governor)) {
1156 ret = PTR_ERR(governor);
1159 if (df->governor == governor) {
1162 } else if ((df->governor && df->governor->immutable) ||
1163 governor->immutable) {
1169 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1171 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1172 __func__, df->governor->name, ret);
1176 prev_governor = df->governor;
1177 df->governor = governor;
1178 strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
1179 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1181 dev_warn(dev, "%s: Governor %s not started(%d)\n",
1182 __func__, df->governor->name, ret);
1183 df->governor = prev_governor;
1184 strncpy(df->governor_name, prev_governor->name,
1186 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1189 "%s: reverting to Governor %s failed (%d)\n",
1190 __func__, df->governor_name, ret);
1191 df->governor = NULL;
1195 mutex_unlock(&devfreq_list_lock);
1201 static DEVICE_ATTR_RW(governor);
1203 static ssize_t available_governors_show(struct device *d,
1204 struct device_attribute *attr,
1207 struct devfreq *df = to_devfreq(d);
1210 mutex_lock(&devfreq_list_lock);
1213 * The devfreq with immutable governor (e.g., passive) shows
1214 * only own governor.
1216 if (df->governor && df->governor->immutable) {
1217 count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
1218 "%s ", df->governor_name);
1220 * The devfreq device shows the registered governor except for
1221 * immutable governors such as passive governor .
1224 struct devfreq_governor *governor;
1226 list_for_each_entry(governor, &devfreq_governor_list, node) {
1227 if (governor->immutable)
1229 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1230 "%s ", governor->name);
1234 mutex_unlock(&devfreq_list_lock);
1236 /* Truncate the trailing space */
1240 count += sprintf(&buf[count], "\n");
1244 static DEVICE_ATTR_RO(available_governors);
1246 static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
1250 struct devfreq *devfreq = to_devfreq(dev);
1252 if (devfreq->profile->get_cur_freq &&
1253 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
1254 return sprintf(buf, "%lu\n", freq);
1256 return sprintf(buf, "%lu\n", devfreq->previous_freq);
1258 static DEVICE_ATTR_RO(cur_freq);
1260 static ssize_t target_freq_show(struct device *dev,
1261 struct device_attribute *attr, char *buf)
1263 return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
1265 static DEVICE_ATTR_RO(target_freq);
1267 static ssize_t polling_interval_show(struct device *dev,
1268 struct device_attribute *attr, char *buf)
1270 return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
1273 static ssize_t polling_interval_store(struct device *dev,
1274 struct device_attribute *attr,
1275 const char *buf, size_t count)
1277 struct devfreq *df = to_devfreq(dev);
1284 ret = sscanf(buf, "%u", &value);
1288 df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
1293 static DEVICE_ATTR_RW(polling_interval);
1295 static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
1296 const char *buf, size_t count)
1298 struct devfreq *df = to_devfreq(dev);
1299 unsigned long value;
1302 ret = sscanf(buf, "%lu", &value);
1306 mutex_lock(&df->lock);
1309 if (value > df->max_freq) {
1314 unsigned long *freq_table = df->profile->freq_table;
1316 /* Get minimum frequency according to sorting order */
1317 if (freq_table[0] < freq_table[df->profile->max_state - 1])
1318 value = freq_table[0];
1320 value = freq_table[df->profile->max_state - 1];
1323 df->min_freq = value;
1327 mutex_unlock(&df->lock);
1331 static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
1334 struct devfreq *df = to_devfreq(dev);
1336 return sprintf(buf, "%lu\n", max(df->scaling_min_freq, df->min_freq));
1339 static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
1340 const char *buf, size_t count)
1342 struct devfreq *df = to_devfreq(dev);
1343 unsigned long value;
1346 ret = sscanf(buf, "%lu", &value);
1350 mutex_lock(&df->lock);
1353 if (value < df->min_freq) {
1358 unsigned long *freq_table = df->profile->freq_table;
1360 /* Get maximum frequency according to sorting order */
1361 if (freq_table[0] < freq_table[df->profile->max_state - 1])
1362 value = freq_table[df->profile->max_state - 1];
1364 value = freq_table[0];
1367 df->max_freq = value;
1371 mutex_unlock(&df->lock);
1374 static DEVICE_ATTR_RW(min_freq);
1376 static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
1379 struct devfreq *df = to_devfreq(dev);
1381 return sprintf(buf, "%lu\n", min(df->scaling_max_freq, df->max_freq));
1383 static DEVICE_ATTR_RW(max_freq);
1385 static ssize_t available_frequencies_show(struct device *d,
1386 struct device_attribute *attr,
1389 struct devfreq *df = to_devfreq(d);
1393 mutex_lock(&df->lock);
1395 for (i = 0; i < df->profile->max_state; i++)
1396 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1397 "%lu ", df->profile->freq_table[i]);
1399 mutex_unlock(&df->lock);
1400 /* Truncate the trailing space */
1404 count += sprintf(&buf[count], "\n");
1408 static DEVICE_ATTR_RO(available_frequencies);
1410 static ssize_t trans_stat_show(struct device *dev,
1411 struct device_attribute *attr, char *buf)
1413 struct devfreq *devfreq = to_devfreq(dev);
1416 unsigned int max_state = devfreq->profile->max_state;
1419 return sprintf(buf, "Not Supported.\n");
1421 mutex_lock(&devfreq->lock);
1422 if (!devfreq->stop_polling &&
1423 devfreq_update_status(devfreq, devfreq->previous_freq)) {
1424 mutex_unlock(&devfreq->lock);
1427 mutex_unlock(&devfreq->lock);
1429 len = sprintf(buf, " From : To\n");
1430 len += sprintf(buf + len, " :");
1431 for (i = 0; i < max_state; i++)
1432 len += sprintf(buf + len, "%10lu",
1433 devfreq->profile->freq_table[i]);
1435 len += sprintf(buf + len, " time(ms)\n");
1437 for (i = 0; i < max_state; i++) {
1438 if (devfreq->profile->freq_table[i]
1439 == devfreq->previous_freq) {
1440 len += sprintf(buf + len, "*");
1442 len += sprintf(buf + len, " ");
1444 len += sprintf(buf + len, "%10lu:",
1445 devfreq->profile->freq_table[i]);
1446 for (j = 0; j < max_state; j++)
1447 len += sprintf(buf + len, "%10u",
1448 devfreq->trans_table[(i * max_state) + j]);
1449 len += sprintf(buf + len, "%10u\n",
1450 jiffies_to_msecs(devfreq->time_in_state[i]));
1453 len += sprintf(buf + len, "Total transition : %u\n",
1454 devfreq->total_trans);
1457 static DEVICE_ATTR_RO(trans_stat);
1459 static struct attribute *devfreq_attrs[] = {
1460 &dev_attr_governor.attr,
1461 &dev_attr_available_governors.attr,
1462 &dev_attr_cur_freq.attr,
1463 &dev_attr_available_frequencies.attr,
1464 &dev_attr_target_freq.attr,
1465 &dev_attr_polling_interval.attr,
1466 &dev_attr_min_freq.attr,
1467 &dev_attr_max_freq.attr,
1468 &dev_attr_trans_stat.attr,
1471 ATTRIBUTE_GROUPS(devfreq);
1473 static int __init devfreq_init(void)
1475 devfreq_class = class_create(THIS_MODULE, "devfreq");
1476 if (IS_ERR(devfreq_class)) {
1477 pr_err("%s: couldn't create class\n", __FILE__);
1478 return PTR_ERR(devfreq_class);
1481 devfreq_wq = create_freezable_workqueue("devfreq_wq");
1483 class_destroy(devfreq_class);
1484 pr_err("%s: couldn't create workqueue\n", __FILE__);
1487 devfreq_class->dev_groups = devfreq_groups;
1491 subsys_initcall(devfreq_init);
1494 * The following are helper functions for devfreq user device drivers with
1499 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1500 * freq value given to target callback.
1501 * @dev: The devfreq user device. (parent of devfreq)
1502 * @freq: The frequency given to target function
1503 * @flags: Flags handed from devfreq framework.
1505 * The callers are required to call dev_pm_opp_put() for the returned OPP after
1508 struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1509 unsigned long *freq,
1512 struct dev_pm_opp *opp;
1514 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1515 /* The freq is an upper bound. opp should be lower */
1516 opp = dev_pm_opp_find_freq_floor(dev, freq);
1518 /* If not available, use the closest opp */
1519 if (opp == ERR_PTR(-ERANGE))
1520 opp = dev_pm_opp_find_freq_ceil(dev, freq);
1522 /* The freq is an lower bound. opp should be higher */
1523 opp = dev_pm_opp_find_freq_ceil(dev, freq);
1525 /* If not available, use the closest opp */
1526 if (opp == ERR_PTR(-ERANGE))
1527 opp = dev_pm_opp_find_freq_floor(dev, freq);
1532 EXPORT_SYMBOL(devfreq_recommended_opp);
1535 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1536 * for any changes in the OPP availability
1538 * @dev: The devfreq user device. (parent of devfreq)
1539 * @devfreq: The devfreq object.
1541 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1543 return dev_pm_opp_register_notifier(dev, &devfreq->nb);
1545 EXPORT_SYMBOL(devfreq_register_opp_notifier);
1548 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1549 * notified for any changes in the OPP
1550 * availability changes anymore.
1551 * @dev: The devfreq user device. (parent of devfreq)
1552 * @devfreq: The devfreq object.
1554 * At exit() callback of devfreq_dev_profile, this must be included if
1555 * devfreq_recommended_opp is used.
1557 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1559 return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
1561 EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
1563 static void devm_devfreq_opp_release(struct device *dev, void *res)
1565 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
1569 * devm_devfreq_register_opp_notifier() - Resource-managed
1570 * devfreq_register_opp_notifier()
1571 * @dev: The devfreq user device. (parent of devfreq)
1572 * @devfreq: The devfreq object.
1574 int devm_devfreq_register_opp_notifier(struct device *dev,
1575 struct devfreq *devfreq)
1577 struct devfreq **ptr;
1580 ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
1584 ret = devfreq_register_opp_notifier(dev, devfreq);
1591 devres_add(dev, ptr);
1595 EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
1598 * devm_devfreq_unregister_opp_notifier() - Resource-managed
1599 * devfreq_unregister_opp_notifier()
1600 * @dev: The devfreq user device. (parent of devfreq)
1601 * @devfreq: The devfreq object.
1603 void devm_devfreq_unregister_opp_notifier(struct device *dev,
1604 struct devfreq *devfreq)
1606 WARN_ON(devres_release(dev, devm_devfreq_opp_release,
1607 devm_devfreq_dev_match, devfreq));
1609 EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
1612 * devfreq_register_notifier() - Register a driver with devfreq
1613 * @devfreq: The devfreq object.
1614 * @nb: The notifier block to register.
1615 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1617 int devfreq_register_notifier(struct devfreq *devfreq,
1618 struct notifier_block *nb,
1627 case DEVFREQ_TRANSITION_NOTIFIER:
1628 ret = srcu_notifier_chain_register(
1629 &devfreq->transition_notifier_list, nb);
1637 EXPORT_SYMBOL(devfreq_register_notifier);
1640 * devfreq_unregister_notifier() - Unregister a driver with devfreq
1641 * @devfreq: The devfreq object.
1642 * @nb: The notifier block to be unregistered.
1643 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1645 int devfreq_unregister_notifier(struct devfreq *devfreq,
1646 struct notifier_block *nb,
1655 case DEVFREQ_TRANSITION_NOTIFIER:
1656 ret = srcu_notifier_chain_unregister(
1657 &devfreq->transition_notifier_list, nb);
1665 EXPORT_SYMBOL(devfreq_unregister_notifier);
1667 struct devfreq_notifier_devres {
1668 struct devfreq *devfreq;
1669 struct notifier_block *nb;
1673 static void devm_devfreq_notifier_release(struct device *dev, void *res)
1675 struct devfreq_notifier_devres *this = res;
1677 devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
1681 * devm_devfreq_register_notifier()
1682 - Resource-managed devfreq_register_notifier()
1683 * @dev: The devfreq user device. (parent of devfreq)
1684 * @devfreq: The devfreq object.
1685 * @nb: The notifier block to be unregistered.
1686 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1688 int devm_devfreq_register_notifier(struct device *dev,
1689 struct devfreq *devfreq,
1690 struct notifier_block *nb,
1693 struct devfreq_notifier_devres *ptr;
1696 ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
1701 ret = devfreq_register_notifier(devfreq, nb, list);
1707 ptr->devfreq = devfreq;
1710 devres_add(dev, ptr);
1714 EXPORT_SYMBOL(devm_devfreq_register_notifier);
1717 * devm_devfreq_unregister_notifier()
1718 - Resource-managed devfreq_unregister_notifier()
1719 * @dev: The devfreq user device. (parent of devfreq)
1720 * @devfreq: The devfreq object.
1721 * @nb: The notifier block to be unregistered.
1722 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1724 void devm_devfreq_unregister_notifier(struct device *dev,
1725 struct devfreq *devfreq,
1726 struct notifier_block *nb,
1729 WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
1730 devm_devfreq_dev_match, devfreq));
1732 EXPORT_SYMBOL(devm_devfreq_unregister_notifier);