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 mutex_lock(&devfreq->lock);
925 ret = devfreq_set_target(devfreq, devfreq->suspend_freq, 0);
926 mutex_unlock(&devfreq->lock);
933 EXPORT_SYMBOL(devfreq_suspend_device);
936 * devfreq_resume_device() - Resume devfreq of a device.
937 * @devfreq: the devfreq instance to be resumed
939 * This function is intended to be called by the pm callbacks
940 * (e.g., runtime_resume, resume) of the device driver that
943 int devfreq_resume_device(struct devfreq *devfreq)
950 if (atomic_dec_return(&devfreq->suspend_count) >= 1)
953 if (devfreq->resume_freq) {
954 mutex_lock(&devfreq->lock);
955 ret = devfreq_set_target(devfreq, devfreq->resume_freq, 0);
956 mutex_unlock(&devfreq->lock);
961 if (devfreq->governor) {
962 ret = devfreq->governor->event_handler(devfreq,
963 DEVFREQ_GOV_RESUME, NULL);
970 EXPORT_SYMBOL(devfreq_resume_device);
973 * devfreq_suspend() - Suspend devfreq governors and devices
975 * Called during system wide Suspend/Hibernate cycles for suspending governors
976 * and devices preserving the state for resume. On some platforms the devfreq
977 * device must have precise state (frequency) after resume in order to provide
978 * fully operating setup.
980 void devfreq_suspend(void)
982 struct devfreq *devfreq;
985 mutex_lock(&devfreq_list_lock);
986 list_for_each_entry(devfreq, &devfreq_list, node) {
987 ret = devfreq_suspend_device(devfreq);
989 dev_err(&devfreq->dev,
990 "failed to suspend devfreq device\n");
992 mutex_unlock(&devfreq_list_lock);
996 * devfreq_resume() - Resume devfreq governors and devices
998 * Called during system wide Suspend/Hibernate cycle for resuming governors and
999 * devices that are suspended with devfreq_suspend().
1001 void devfreq_resume(void)
1003 struct devfreq *devfreq;
1006 mutex_lock(&devfreq_list_lock);
1007 list_for_each_entry(devfreq, &devfreq_list, node) {
1008 ret = devfreq_resume_device(devfreq);
1010 dev_warn(&devfreq->dev,
1011 "failed to resume devfreq device\n");
1013 mutex_unlock(&devfreq_list_lock);
1017 * devfreq_add_governor() - Add devfreq governor
1018 * @governor: the devfreq governor to be added
1020 int devfreq_add_governor(struct devfreq_governor *governor)
1022 struct devfreq_governor *g;
1023 struct devfreq *devfreq;
1027 pr_err("%s: Invalid parameters.\n", __func__);
1031 mutex_lock(&devfreq_list_lock);
1032 g = find_devfreq_governor(governor->name);
1034 pr_err("%s: governor %s already registered\n", __func__,
1040 list_add(&governor->node, &devfreq_governor_list);
1042 list_for_each_entry(devfreq, &devfreq_list, node) {
1044 struct device *dev = devfreq->dev.parent;
1046 if (!strncmp(devfreq->governor_name, governor->name,
1047 DEVFREQ_NAME_LEN)) {
1048 /* The following should never occur */
1049 if (devfreq->governor) {
1051 "%s: Governor %s already present\n",
1052 __func__, devfreq->governor->name);
1053 ret = devfreq->governor->event_handler(devfreq,
1054 DEVFREQ_GOV_STOP, NULL);
1057 "%s: Governor %s stop = %d\n",
1059 devfreq->governor->name, ret);
1063 devfreq->governor = governor;
1064 ret = devfreq->governor->event_handler(devfreq,
1065 DEVFREQ_GOV_START, NULL);
1067 dev_warn(dev, "%s: Governor %s start=%d\n",
1068 __func__, devfreq->governor->name,
1075 mutex_unlock(&devfreq_list_lock);
1079 EXPORT_SYMBOL(devfreq_add_governor);
1082 * devfreq_remove_governor() - Remove devfreq feature from a device.
1083 * @governor: the devfreq governor to be removed
1085 int devfreq_remove_governor(struct devfreq_governor *governor)
1087 struct devfreq_governor *g;
1088 struct devfreq *devfreq;
1092 pr_err("%s: Invalid parameters.\n", __func__);
1096 mutex_lock(&devfreq_list_lock);
1097 g = find_devfreq_governor(governor->name);
1099 pr_err("%s: governor %s not registered\n", __func__,
1104 list_for_each_entry(devfreq, &devfreq_list, node) {
1106 struct device *dev = devfreq->dev.parent;
1108 if (!strncmp(devfreq->governor_name, governor->name,
1109 DEVFREQ_NAME_LEN)) {
1110 /* we should have a devfreq governor! */
1111 if (!devfreq->governor) {
1112 dev_warn(dev, "%s: Governor %s NOT present\n",
1113 __func__, governor->name);
1117 ret = devfreq->governor->event_handler(devfreq,
1118 DEVFREQ_GOV_STOP, NULL);
1120 dev_warn(dev, "%s: Governor %s stop=%d\n",
1121 __func__, devfreq->governor->name,
1124 devfreq->governor = NULL;
1128 list_del(&governor->node);
1130 mutex_unlock(&devfreq_list_lock);
1134 EXPORT_SYMBOL(devfreq_remove_governor);
1136 static ssize_t governor_show(struct device *dev,
1137 struct device_attribute *attr, char *buf)
1139 if (!to_devfreq(dev)->governor)
1142 return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
1145 static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
1146 const char *buf, size_t count)
1148 struct devfreq *df = to_devfreq(dev);
1150 char str_governor[DEVFREQ_NAME_LEN + 1];
1151 const struct devfreq_governor *governor, *prev_governor;
1153 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
1157 mutex_lock(&devfreq_list_lock);
1158 governor = try_then_request_governor(str_governor);
1159 if (IS_ERR(governor)) {
1160 ret = PTR_ERR(governor);
1163 if (df->governor == governor) {
1166 } else if ((df->governor && df->governor->immutable) ||
1167 governor->immutable) {
1173 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1175 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1176 __func__, df->governor->name, ret);
1180 prev_governor = df->governor;
1181 df->governor = governor;
1182 strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
1183 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1185 dev_warn(dev, "%s: Governor %s not started(%d)\n",
1186 __func__, df->governor->name, ret);
1187 df->governor = prev_governor;
1188 strncpy(df->governor_name, prev_governor->name,
1190 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1193 "%s: reverting to Governor %s failed (%d)\n",
1194 __func__, df->governor_name, ret);
1195 df->governor = NULL;
1199 mutex_unlock(&devfreq_list_lock);
1205 static DEVICE_ATTR_RW(governor);
1207 static ssize_t available_governors_show(struct device *d,
1208 struct device_attribute *attr,
1211 struct devfreq *df = to_devfreq(d);
1214 mutex_lock(&devfreq_list_lock);
1217 * The devfreq with immutable governor (e.g., passive) shows
1218 * only own governor.
1220 if (df->governor && df->governor->immutable) {
1221 count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
1222 "%s ", df->governor_name);
1224 * The devfreq device shows the registered governor except for
1225 * immutable governors such as passive governor .
1228 struct devfreq_governor *governor;
1230 list_for_each_entry(governor, &devfreq_governor_list, node) {
1231 if (governor->immutable)
1233 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1234 "%s ", governor->name);
1238 mutex_unlock(&devfreq_list_lock);
1240 /* Truncate the trailing space */
1244 count += sprintf(&buf[count], "\n");
1248 static DEVICE_ATTR_RO(available_governors);
1250 static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
1254 struct devfreq *devfreq = to_devfreq(dev);
1256 if (devfreq->profile->get_cur_freq &&
1257 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
1258 return sprintf(buf, "%lu\n", freq);
1260 return sprintf(buf, "%lu\n", devfreq->previous_freq);
1262 static DEVICE_ATTR_RO(cur_freq);
1264 static ssize_t target_freq_show(struct device *dev,
1265 struct device_attribute *attr, char *buf)
1267 return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
1269 static DEVICE_ATTR_RO(target_freq);
1271 static ssize_t polling_interval_show(struct device *dev,
1272 struct device_attribute *attr, char *buf)
1274 return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
1277 static ssize_t polling_interval_store(struct device *dev,
1278 struct device_attribute *attr,
1279 const char *buf, size_t count)
1281 struct devfreq *df = to_devfreq(dev);
1288 ret = sscanf(buf, "%u", &value);
1292 df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
1297 static DEVICE_ATTR_RW(polling_interval);
1299 static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
1300 const char *buf, size_t count)
1302 struct devfreq *df = to_devfreq(dev);
1303 unsigned long value;
1306 ret = sscanf(buf, "%lu", &value);
1310 mutex_lock(&df->lock);
1313 if (value > df->max_freq) {
1318 unsigned long *freq_table = df->profile->freq_table;
1320 /* Get minimum frequency according to sorting order */
1321 if (freq_table[0] < freq_table[df->profile->max_state - 1])
1322 value = freq_table[0];
1324 value = freq_table[df->profile->max_state - 1];
1327 df->min_freq = value;
1331 mutex_unlock(&df->lock);
1335 static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
1338 struct devfreq *df = to_devfreq(dev);
1340 return sprintf(buf, "%lu\n", max(df->scaling_min_freq, df->min_freq));
1343 static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
1344 const char *buf, size_t count)
1346 struct devfreq *df = to_devfreq(dev);
1347 unsigned long value;
1350 ret = sscanf(buf, "%lu", &value);
1354 mutex_lock(&df->lock);
1357 if (value < df->min_freq) {
1362 unsigned long *freq_table = df->profile->freq_table;
1364 /* Get maximum frequency according to sorting order */
1365 if (freq_table[0] < freq_table[df->profile->max_state - 1])
1366 value = freq_table[df->profile->max_state - 1];
1368 value = freq_table[0];
1371 df->max_freq = value;
1375 mutex_unlock(&df->lock);
1378 static DEVICE_ATTR_RW(min_freq);
1380 static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
1383 struct devfreq *df = to_devfreq(dev);
1385 return sprintf(buf, "%lu\n", min(df->scaling_max_freq, df->max_freq));
1387 static DEVICE_ATTR_RW(max_freq);
1389 static ssize_t available_frequencies_show(struct device *d,
1390 struct device_attribute *attr,
1393 struct devfreq *df = to_devfreq(d);
1397 mutex_lock(&df->lock);
1399 for (i = 0; i < df->profile->max_state; i++)
1400 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1401 "%lu ", df->profile->freq_table[i]);
1403 mutex_unlock(&df->lock);
1404 /* Truncate the trailing space */
1408 count += sprintf(&buf[count], "\n");
1412 static DEVICE_ATTR_RO(available_frequencies);
1414 static ssize_t trans_stat_show(struct device *dev,
1415 struct device_attribute *attr, char *buf)
1417 struct devfreq *devfreq = to_devfreq(dev);
1420 unsigned int max_state = devfreq->profile->max_state;
1423 return sprintf(buf, "Not Supported.\n");
1425 mutex_lock(&devfreq->lock);
1426 if (!devfreq->stop_polling &&
1427 devfreq_update_status(devfreq, devfreq->previous_freq)) {
1428 mutex_unlock(&devfreq->lock);
1431 mutex_unlock(&devfreq->lock);
1433 len = sprintf(buf, " From : To\n");
1434 len += sprintf(buf + len, " :");
1435 for (i = 0; i < max_state; i++)
1436 len += sprintf(buf + len, "%10lu",
1437 devfreq->profile->freq_table[i]);
1439 len += sprintf(buf + len, " time(ms)\n");
1441 for (i = 0; i < max_state; i++) {
1442 if (devfreq->profile->freq_table[i]
1443 == devfreq->previous_freq) {
1444 len += sprintf(buf + len, "*");
1446 len += sprintf(buf + len, " ");
1448 len += sprintf(buf + len, "%10lu:",
1449 devfreq->profile->freq_table[i]);
1450 for (j = 0; j < max_state; j++)
1451 len += sprintf(buf + len, "%10u",
1452 devfreq->trans_table[(i * max_state) + j]);
1453 len += sprintf(buf + len, "%10u\n",
1454 jiffies_to_msecs(devfreq->time_in_state[i]));
1457 len += sprintf(buf + len, "Total transition : %u\n",
1458 devfreq->total_trans);
1461 static DEVICE_ATTR_RO(trans_stat);
1463 static struct attribute *devfreq_attrs[] = {
1464 &dev_attr_governor.attr,
1465 &dev_attr_available_governors.attr,
1466 &dev_attr_cur_freq.attr,
1467 &dev_attr_available_frequencies.attr,
1468 &dev_attr_target_freq.attr,
1469 &dev_attr_polling_interval.attr,
1470 &dev_attr_min_freq.attr,
1471 &dev_attr_max_freq.attr,
1472 &dev_attr_trans_stat.attr,
1475 ATTRIBUTE_GROUPS(devfreq);
1477 static int __init devfreq_init(void)
1479 devfreq_class = class_create(THIS_MODULE, "devfreq");
1480 if (IS_ERR(devfreq_class)) {
1481 pr_err("%s: couldn't create class\n", __FILE__);
1482 return PTR_ERR(devfreq_class);
1485 devfreq_wq = create_freezable_workqueue("devfreq_wq");
1487 class_destroy(devfreq_class);
1488 pr_err("%s: couldn't create workqueue\n", __FILE__);
1491 devfreq_class->dev_groups = devfreq_groups;
1495 subsys_initcall(devfreq_init);
1498 * The following are helper functions for devfreq user device drivers with
1503 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1504 * freq value given to target callback.
1505 * @dev: The devfreq user device. (parent of devfreq)
1506 * @freq: The frequency given to target function
1507 * @flags: Flags handed from devfreq framework.
1509 * The callers are required to call dev_pm_opp_put() for the returned OPP after
1512 struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1513 unsigned long *freq,
1516 struct dev_pm_opp *opp;
1518 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1519 /* The freq is an upper bound. opp should be lower */
1520 opp = dev_pm_opp_find_freq_floor(dev, freq);
1522 /* If not available, use the closest opp */
1523 if (opp == ERR_PTR(-ERANGE))
1524 opp = dev_pm_opp_find_freq_ceil(dev, freq);
1526 /* The freq is an lower bound. opp should be higher */
1527 opp = dev_pm_opp_find_freq_ceil(dev, freq);
1529 /* If not available, use the closest opp */
1530 if (opp == ERR_PTR(-ERANGE))
1531 opp = dev_pm_opp_find_freq_floor(dev, freq);
1536 EXPORT_SYMBOL(devfreq_recommended_opp);
1539 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1540 * for any changes in the OPP availability
1542 * @dev: The devfreq user device. (parent of devfreq)
1543 * @devfreq: The devfreq object.
1545 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1547 return dev_pm_opp_register_notifier(dev, &devfreq->nb);
1549 EXPORT_SYMBOL(devfreq_register_opp_notifier);
1552 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1553 * notified for any changes in the OPP
1554 * availability changes anymore.
1555 * @dev: The devfreq user device. (parent of devfreq)
1556 * @devfreq: The devfreq object.
1558 * At exit() callback of devfreq_dev_profile, this must be included if
1559 * devfreq_recommended_opp is used.
1561 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1563 return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
1565 EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
1567 static void devm_devfreq_opp_release(struct device *dev, void *res)
1569 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
1573 * devm_devfreq_register_opp_notifier() - Resource-managed
1574 * devfreq_register_opp_notifier()
1575 * @dev: The devfreq user device. (parent of devfreq)
1576 * @devfreq: The devfreq object.
1578 int devm_devfreq_register_opp_notifier(struct device *dev,
1579 struct devfreq *devfreq)
1581 struct devfreq **ptr;
1584 ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
1588 ret = devfreq_register_opp_notifier(dev, devfreq);
1595 devres_add(dev, ptr);
1599 EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
1602 * devm_devfreq_unregister_opp_notifier() - Resource-managed
1603 * devfreq_unregister_opp_notifier()
1604 * @dev: The devfreq user device. (parent of devfreq)
1605 * @devfreq: The devfreq object.
1607 void devm_devfreq_unregister_opp_notifier(struct device *dev,
1608 struct devfreq *devfreq)
1610 WARN_ON(devres_release(dev, devm_devfreq_opp_release,
1611 devm_devfreq_dev_match, devfreq));
1613 EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
1616 * devfreq_register_notifier() - Register a driver with devfreq
1617 * @devfreq: The devfreq object.
1618 * @nb: The notifier block to register.
1619 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1621 int devfreq_register_notifier(struct devfreq *devfreq,
1622 struct notifier_block *nb,
1631 case DEVFREQ_TRANSITION_NOTIFIER:
1632 ret = srcu_notifier_chain_register(
1633 &devfreq->transition_notifier_list, nb);
1641 EXPORT_SYMBOL(devfreq_register_notifier);
1644 * devfreq_unregister_notifier() - Unregister a driver with devfreq
1645 * @devfreq: The devfreq object.
1646 * @nb: The notifier block to be unregistered.
1647 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1649 int devfreq_unregister_notifier(struct devfreq *devfreq,
1650 struct notifier_block *nb,
1659 case DEVFREQ_TRANSITION_NOTIFIER:
1660 ret = srcu_notifier_chain_unregister(
1661 &devfreq->transition_notifier_list, nb);
1669 EXPORT_SYMBOL(devfreq_unregister_notifier);
1671 struct devfreq_notifier_devres {
1672 struct devfreq *devfreq;
1673 struct notifier_block *nb;
1677 static void devm_devfreq_notifier_release(struct device *dev, void *res)
1679 struct devfreq_notifier_devres *this = res;
1681 devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
1685 * devm_devfreq_register_notifier()
1686 - Resource-managed devfreq_register_notifier()
1687 * @dev: The devfreq user device. (parent of devfreq)
1688 * @devfreq: The devfreq object.
1689 * @nb: The notifier block to be unregistered.
1690 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1692 int devm_devfreq_register_notifier(struct device *dev,
1693 struct devfreq *devfreq,
1694 struct notifier_block *nb,
1697 struct devfreq_notifier_devres *ptr;
1700 ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
1705 ret = devfreq_register_notifier(devfreq, nb, list);
1711 ptr->devfreq = devfreq;
1714 devres_add(dev, ptr);
1718 EXPORT_SYMBOL(devm_devfreq_register_notifier);
1721 * devm_devfreq_unregister_notifier()
1722 - Resource-managed devfreq_unregister_notifier()
1723 * @dev: The devfreq user device. (parent of devfreq)
1724 * @devfreq: The devfreq object.
1725 * @nb: The notifier block to be unregistered.
1726 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1728 void devm_devfreq_unregister_notifier(struct device *dev,
1729 struct devfreq *devfreq,
1730 struct notifier_block *nb,
1733 WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
1734 devm_devfreq_dev_match, devfreq));
1736 EXPORT_SYMBOL(devm_devfreq_unregister_notifier);