2 * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
5 * Copyright (C) 2011 Samsung Electronics
6 * MyungJoo Ham <myungjoo.ham@samsung.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/kernel.h>
14 #include <linux/kmod.h>
15 #include <linux/sched.h>
16 #include <linux/errno.h>
17 #include <linux/err.h>
18 #include <linux/init.h>
19 #include <linux/export.h>
20 #include <linux/slab.h>
21 #include <linux/stat.h>
22 #include <linux/pm_opp.h>
23 #include <linux/devfreq.h>
24 #include <linux/workqueue.h>
25 #include <linux/platform_device.h>
26 #include <linux/list.h>
27 #include <linux/printk.h>
28 #include <linux/hrtimer.h>
32 #define CREATE_TRACE_POINTS
33 #include <trace/events/devfreq.h>
35 static struct class *devfreq_class;
38 * devfreq core provides delayed work based load monitoring helper
39 * functions. Governors can use these or can implement their own
40 * monitoring mechanism.
42 static struct workqueue_struct *devfreq_wq;
44 /* The list of all device-devfreq governors */
45 static LIST_HEAD(devfreq_governor_list);
46 /* The list of all device-devfreq */
47 static LIST_HEAD(devfreq_list);
48 static DEFINE_MUTEX(devfreq_list_lock);
51 * find_device_devfreq() - find devfreq struct using device pointer
52 * @dev: device pointer used to lookup device devfreq.
54 * Search the list of device devfreqs and return the matched device's
55 * devfreq info. devfreq_list_lock should be held by the caller.
57 static struct devfreq *find_device_devfreq(struct device *dev)
59 struct devfreq *tmp_devfreq;
61 if (IS_ERR_OR_NULL(dev)) {
62 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
63 return ERR_PTR(-EINVAL);
65 WARN(!mutex_is_locked(&devfreq_list_lock),
66 "devfreq_list_lock must be locked.");
68 list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
69 if (tmp_devfreq->dev.parent == dev)
73 return ERR_PTR(-ENODEV);
76 static unsigned long find_available_min_freq(struct devfreq *devfreq)
78 struct dev_pm_opp *opp;
79 unsigned long min_freq = 0;
81 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &min_freq);
90 static unsigned long find_available_max_freq(struct devfreq *devfreq)
92 struct dev_pm_opp *opp;
93 unsigned long max_freq = ULONG_MAX;
95 opp = dev_pm_opp_find_freq_floor(devfreq->dev.parent, &max_freq);
105 * devfreq_get_freq_level() - Lookup freq_table for the frequency
106 * @devfreq: the devfreq instance
107 * @freq: the target frequency
109 static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq)
113 for (lev = 0; lev < devfreq->profile->max_state; lev++)
114 if (freq == devfreq->profile->freq_table[lev])
120 static int set_freq_table(struct devfreq *devfreq)
122 struct devfreq_dev_profile *profile = devfreq->profile;
123 struct dev_pm_opp *opp;
127 /* Initialize the freq_table from OPP table */
128 count = dev_pm_opp_get_opp_count(devfreq->dev.parent);
132 profile->max_state = count;
133 profile->freq_table = devm_kcalloc(devfreq->dev.parent,
135 sizeof(*profile->freq_table),
137 if (!profile->freq_table) {
138 profile->max_state = 0;
142 for (i = 0, freq = 0; i < profile->max_state; i++, freq++) {
143 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq);
145 devm_kfree(devfreq->dev.parent, profile->freq_table);
146 profile->max_state = 0;
150 profile->freq_table[i] = freq;
157 * devfreq_update_status() - Update statistics of devfreq behavior
158 * @devfreq: the devfreq instance
159 * @freq: the update target frequency
161 int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
163 int lev, prev_lev, ret = 0;
164 unsigned long cur_time;
168 /* Immediately exit if previous_freq is not initialized yet. */
169 if (!devfreq->previous_freq)
172 prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
178 devfreq->time_in_state[prev_lev] +=
179 cur_time - devfreq->last_stat_updated;
181 lev = devfreq_get_freq_level(devfreq, freq);
187 if (lev != prev_lev) {
188 devfreq->trans_table[(prev_lev *
189 devfreq->profile->max_state) + lev]++;
190 devfreq->total_trans++;
194 devfreq->last_stat_updated = cur_time;
197 EXPORT_SYMBOL(devfreq_update_status);
200 * find_devfreq_governor() - find devfreq governor from name
201 * @name: name of the governor
203 * Search the list of devfreq governors and return the matched
204 * governor's pointer. devfreq_list_lock should be held by the caller.
206 static struct devfreq_governor *find_devfreq_governor(const char *name)
208 struct devfreq_governor *tmp_governor;
210 if (IS_ERR_OR_NULL(name)) {
211 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
212 return ERR_PTR(-EINVAL);
214 WARN(!mutex_is_locked(&devfreq_list_lock),
215 "devfreq_list_lock must be locked.");
217 list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
218 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
222 return ERR_PTR(-ENODEV);
226 * try_then_request_governor() - Try to find the governor and request the
227 * module if is not found.
228 * @name: name of the governor
230 * Search the list of devfreq governors and request the module and try again
231 * if is not found. This can happen when both drivers (the governor driver
232 * and the driver that call devfreq_add_device) are built as modules.
233 * devfreq_list_lock should be held by the caller. Returns the matched
234 * governor's pointer or an error pointer.
236 static struct devfreq_governor *try_then_request_governor(const char *name)
238 struct devfreq_governor *governor;
241 if (IS_ERR_OR_NULL(name)) {
242 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
243 return ERR_PTR(-EINVAL);
245 WARN(!mutex_is_locked(&devfreq_list_lock),
246 "devfreq_list_lock must be locked.");
248 governor = find_devfreq_governor(name);
249 if (IS_ERR(governor)) {
250 mutex_unlock(&devfreq_list_lock);
252 if (!strncmp(name, DEVFREQ_GOV_SIMPLE_ONDEMAND,
254 err = request_module("governor_%s", "simpleondemand");
256 err = request_module("governor_%s", name);
257 /* Restore previous state before return */
258 mutex_lock(&devfreq_list_lock);
262 governor = find_devfreq_governor(name);
268 static int devfreq_notify_transition(struct devfreq *devfreq,
269 struct devfreq_freqs *freqs, unsigned int state)
275 case DEVFREQ_PRECHANGE:
276 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
277 DEVFREQ_PRECHANGE, freqs);
280 case DEVFREQ_POSTCHANGE:
281 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
282 DEVFREQ_POSTCHANGE, freqs);
291 static int devfreq_set_target(struct devfreq *devfreq, unsigned long new_freq,
294 struct devfreq_freqs freqs;
295 unsigned long cur_freq;
298 if (devfreq->profile->get_cur_freq)
299 devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
301 cur_freq = devfreq->previous_freq;
303 freqs.old = cur_freq;
304 freqs.new = new_freq;
305 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
307 err = devfreq->profile->target(devfreq->dev.parent, &new_freq, flags);
309 freqs.new = cur_freq;
310 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
314 freqs.new = new_freq;
315 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
317 if (devfreq_update_status(devfreq, new_freq))
318 dev_err(&devfreq->dev,
319 "Couldn't update frequency transition information.\n");
321 devfreq->previous_freq = new_freq;
323 if (devfreq->suspend_freq)
324 devfreq->resume_freq = cur_freq;
329 /* Load monitoring helper functions for governors use */
332 * update_devfreq() - Reevaluate the device and configure frequency.
333 * @devfreq: the devfreq instance.
335 * Note: Lock devfreq->lock before calling update_devfreq
336 * This function is exported for governors.
338 int update_devfreq(struct devfreq *devfreq)
340 unsigned long freq, min_freq, max_freq;
344 if (!mutex_is_locked(&devfreq->lock)) {
345 WARN(true, "devfreq->lock must be locked by the caller.\n");
349 if (!devfreq->governor)
352 /* Reevaluate the proper frequency */
353 err = devfreq->governor->get_target_freq(devfreq, &freq);
358 * Adjust the frequency with user freq, QoS and available freq.
360 * List from the highest priority
364 max_freq = min(devfreq->scaling_max_freq, devfreq->max_freq);
365 min_freq = max(devfreq->scaling_min_freq, devfreq->min_freq);
367 if (freq < min_freq) {
369 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
371 if (freq > max_freq) {
373 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
376 return devfreq_set_target(devfreq, freq, flags);
379 EXPORT_SYMBOL(update_devfreq);
382 * devfreq_monitor() - Periodically poll devfreq objects.
383 * @work: the work struct used to run devfreq_monitor periodically.
386 static void devfreq_monitor(struct work_struct *work)
389 struct devfreq *devfreq = container_of(work,
390 struct devfreq, work.work);
392 mutex_lock(&devfreq->lock);
393 err = update_devfreq(devfreq);
395 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
397 queue_delayed_work(devfreq_wq, &devfreq->work,
398 msecs_to_jiffies(devfreq->profile->polling_ms));
399 mutex_unlock(&devfreq->lock);
401 trace_devfreq_monitor(devfreq);
405 * devfreq_monitor_start() - Start load monitoring of devfreq instance
406 * @devfreq: the devfreq instance.
408 * Helper function for starting devfreq device load monitoing. By
409 * default delayed work based monitoring is supported. Function
410 * to be called from governor in response to DEVFREQ_GOV_START
411 * event when device is added to devfreq framework.
413 void devfreq_monitor_start(struct devfreq *devfreq)
415 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
416 if (devfreq->profile->polling_ms)
417 queue_delayed_work(devfreq_wq, &devfreq->work,
418 msecs_to_jiffies(devfreq->profile->polling_ms));
420 EXPORT_SYMBOL(devfreq_monitor_start);
423 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
424 * @devfreq: the devfreq instance.
426 * Helper function to stop devfreq device load monitoing. Function
427 * to be called from governor in response to DEVFREQ_GOV_STOP
428 * event when device is removed from devfreq framework.
430 void devfreq_monitor_stop(struct devfreq *devfreq)
432 cancel_delayed_work_sync(&devfreq->work);
434 EXPORT_SYMBOL(devfreq_monitor_stop);
437 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
438 * @devfreq: the devfreq instance.
440 * Helper function to suspend devfreq device load monitoing. Function
441 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
442 * event or when polling interval is set to zero.
444 * Note: Though this function is same as devfreq_monitor_stop(),
445 * intentionally kept separate to provide hooks for collecting
446 * transition statistics.
448 void devfreq_monitor_suspend(struct devfreq *devfreq)
450 mutex_lock(&devfreq->lock);
451 if (devfreq->stop_polling) {
452 mutex_unlock(&devfreq->lock);
456 devfreq_update_status(devfreq, devfreq->previous_freq);
457 devfreq->stop_polling = true;
458 mutex_unlock(&devfreq->lock);
459 cancel_delayed_work_sync(&devfreq->work);
461 EXPORT_SYMBOL(devfreq_monitor_suspend);
464 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
465 * @devfreq: the devfreq instance.
467 * Helper function to resume devfreq device load monitoing. Function
468 * to be called from governor in response to DEVFREQ_GOV_RESUME
469 * event or when polling interval is set to non-zero.
471 void devfreq_monitor_resume(struct devfreq *devfreq)
475 mutex_lock(&devfreq->lock);
476 if (!devfreq->stop_polling)
479 if (!delayed_work_pending(&devfreq->work) &&
480 devfreq->profile->polling_ms)
481 queue_delayed_work(devfreq_wq, &devfreq->work,
482 msecs_to_jiffies(devfreq->profile->polling_ms));
484 devfreq->last_stat_updated = jiffies;
485 devfreq->stop_polling = false;
487 if (devfreq->profile->get_cur_freq &&
488 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
489 devfreq->previous_freq = freq;
492 mutex_unlock(&devfreq->lock);
494 EXPORT_SYMBOL(devfreq_monitor_resume);
497 * devfreq_interval_update() - Update device devfreq monitoring interval
498 * @devfreq: the devfreq instance.
499 * @delay: new polling interval to be set.
501 * Helper function to set new load monitoring polling interval. Function
502 * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
504 void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
506 unsigned int cur_delay = devfreq->profile->polling_ms;
507 unsigned int new_delay = *delay;
509 mutex_lock(&devfreq->lock);
510 devfreq->profile->polling_ms = new_delay;
512 if (devfreq->stop_polling)
515 /* if new delay is zero, stop polling */
517 mutex_unlock(&devfreq->lock);
518 cancel_delayed_work_sync(&devfreq->work);
522 /* if current delay is zero, start polling with new delay */
524 queue_delayed_work(devfreq_wq, &devfreq->work,
525 msecs_to_jiffies(devfreq->profile->polling_ms));
529 /* if current delay is greater than new delay, restart polling */
530 if (cur_delay > new_delay) {
531 mutex_unlock(&devfreq->lock);
532 cancel_delayed_work_sync(&devfreq->work);
533 mutex_lock(&devfreq->lock);
534 if (!devfreq->stop_polling)
535 queue_delayed_work(devfreq_wq, &devfreq->work,
536 msecs_to_jiffies(devfreq->profile->polling_ms));
539 mutex_unlock(&devfreq->lock);
541 EXPORT_SYMBOL(devfreq_interval_update);
544 * devfreq_notifier_call() - Notify that the device frequency requirements
545 * has been changed out of devfreq framework.
546 * @nb: the notifier_block (supposed to be devfreq->nb)
550 * Called by a notifier that uses devfreq->nb.
552 static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
555 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
558 mutex_lock(&devfreq->lock);
560 devfreq->scaling_min_freq = find_available_min_freq(devfreq);
561 if (!devfreq->scaling_min_freq) {
562 mutex_unlock(&devfreq->lock);
566 devfreq->scaling_max_freq = find_available_max_freq(devfreq);
567 if (!devfreq->scaling_max_freq) {
568 mutex_unlock(&devfreq->lock);
572 ret = update_devfreq(devfreq);
573 mutex_unlock(&devfreq->lock);
579 * devfreq_dev_release() - Callback for struct device to release the device.
580 * @dev: the devfreq device
582 * Remove devfreq from the list and release its resources.
584 static void devfreq_dev_release(struct device *dev)
586 struct devfreq *devfreq = to_devfreq(dev);
588 mutex_lock(&devfreq_list_lock);
589 if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
590 mutex_unlock(&devfreq_list_lock);
591 dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
594 list_del(&devfreq->node);
595 mutex_unlock(&devfreq_list_lock);
597 if (devfreq->profile->exit)
598 devfreq->profile->exit(devfreq->dev.parent);
600 mutex_destroy(&devfreq->lock);
605 * devfreq_add_device() - Add devfreq feature to the device
606 * @dev: the device to add devfreq feature.
607 * @profile: device-specific profile to run devfreq.
608 * @governor_name: name of the policy to choose frequency.
609 * @data: private data for the governor. The devfreq framework does not
612 struct devfreq *devfreq_add_device(struct device *dev,
613 struct devfreq_dev_profile *profile,
614 const char *governor_name,
617 struct devfreq *devfreq;
618 struct devfreq_governor *governor;
619 static atomic_t devfreq_no = ATOMIC_INIT(-1);
622 if (!dev || !profile || !governor_name) {
623 dev_err(dev, "%s: Invalid parameters.\n", __func__);
624 return ERR_PTR(-EINVAL);
627 mutex_lock(&devfreq_list_lock);
628 devfreq = find_device_devfreq(dev);
629 mutex_unlock(&devfreq_list_lock);
630 if (!IS_ERR(devfreq)) {
631 dev_err(dev, "%s: Unable to create devfreq for the device.\n",
637 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
643 mutex_init(&devfreq->lock);
644 mutex_lock(&devfreq->lock);
645 devfreq->dev.parent = dev;
646 devfreq->dev.class = devfreq_class;
647 devfreq->dev.release = devfreq_dev_release;
648 devfreq->profile = profile;
649 strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
650 devfreq->previous_freq = profile->initial_freq;
651 devfreq->last_status.current_frequency = profile->initial_freq;
652 devfreq->data = data;
653 devfreq->nb.notifier_call = devfreq_notifier_call;
655 if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
656 mutex_unlock(&devfreq->lock);
657 err = set_freq_table(devfreq);
660 mutex_lock(&devfreq->lock);
663 devfreq->scaling_min_freq = find_available_min_freq(devfreq);
664 if (!devfreq->scaling_min_freq) {
665 mutex_unlock(&devfreq->lock);
669 devfreq->min_freq = devfreq->scaling_min_freq;
671 devfreq->scaling_max_freq = find_available_max_freq(devfreq);
672 if (!devfreq->scaling_max_freq) {
673 mutex_unlock(&devfreq->lock);
677 devfreq->max_freq = devfreq->scaling_max_freq;
679 devfreq->suspend_freq = dev_pm_opp_get_suspend_opp_freq(dev);
680 atomic_set(&devfreq->suspend_count, 0);
682 dev_set_name(&devfreq->dev, "devfreq%d",
683 atomic_inc_return(&devfreq_no));
684 err = device_register(&devfreq->dev);
686 mutex_unlock(&devfreq->lock);
687 put_device(&devfreq->dev);
691 devfreq->trans_table = devm_kzalloc(&devfreq->dev,
692 array3_size(sizeof(unsigned int),
693 devfreq->profile->max_state,
694 devfreq->profile->max_state),
696 if (!devfreq->trans_table) {
697 mutex_unlock(&devfreq->lock);
702 devfreq->time_in_state = devm_kcalloc(&devfreq->dev,
703 devfreq->profile->max_state,
704 sizeof(unsigned long),
706 if (!devfreq->time_in_state) {
707 mutex_unlock(&devfreq->lock);
712 devfreq->last_stat_updated = jiffies;
714 srcu_init_notifier_head(&devfreq->transition_notifier_list);
716 mutex_unlock(&devfreq->lock);
718 mutex_lock(&devfreq_list_lock);
720 governor = try_then_request_governor(devfreq->governor_name);
721 if (IS_ERR(governor)) {
722 dev_err(dev, "%s: Unable to find governor for the device\n",
724 err = PTR_ERR(governor);
728 devfreq->governor = governor;
729 err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
732 dev_err(dev, "%s: Unable to start governor for the device\n",
737 list_add(&devfreq->node, &devfreq_list);
739 mutex_unlock(&devfreq_list_lock);
744 mutex_unlock(&devfreq_list_lock);
746 devfreq_remove_device(devfreq);
753 EXPORT_SYMBOL(devfreq_add_device);
756 * devfreq_remove_device() - Remove devfreq feature from a device.
757 * @devfreq: the devfreq instance to be removed
759 * The opposite of devfreq_add_device().
761 int devfreq_remove_device(struct devfreq *devfreq)
766 if (devfreq->governor)
767 devfreq->governor->event_handler(devfreq,
768 DEVFREQ_GOV_STOP, NULL);
769 device_unregister(&devfreq->dev);
773 EXPORT_SYMBOL(devfreq_remove_device);
775 static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
777 struct devfreq **r = res;
779 if (WARN_ON(!r || !*r))
785 static void devm_devfreq_dev_release(struct device *dev, void *res)
787 devfreq_remove_device(*(struct devfreq **)res);
791 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
792 * @dev: the device to add devfreq feature.
793 * @profile: device-specific profile to run devfreq.
794 * @governor_name: name of the policy to choose frequency.
795 * @data: private data for the governor. The devfreq framework does not
798 * This function manages automatically the memory of devfreq device using device
799 * resource management and simplify the free operation for memory of devfreq
802 struct devfreq *devm_devfreq_add_device(struct device *dev,
803 struct devfreq_dev_profile *profile,
804 const char *governor_name,
807 struct devfreq **ptr, *devfreq;
809 ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
811 return ERR_PTR(-ENOMEM);
813 devfreq = devfreq_add_device(dev, profile, governor_name, data);
814 if (IS_ERR(devfreq)) {
820 devres_add(dev, ptr);
824 EXPORT_SYMBOL(devm_devfreq_add_device);
828 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
829 * @dev - instance to the given device
830 * @index - index into list of devfreq
832 * return the instance of devfreq device
834 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
836 struct device_node *node;
837 struct devfreq *devfreq;
840 return ERR_PTR(-EINVAL);
843 return ERR_PTR(-EINVAL);
845 node = of_parse_phandle(dev->of_node, "devfreq", index);
847 return ERR_PTR(-ENODEV);
849 mutex_lock(&devfreq_list_lock);
850 list_for_each_entry(devfreq, &devfreq_list, node) {
851 if (devfreq->dev.parent
852 && devfreq->dev.parent->of_node == node) {
853 mutex_unlock(&devfreq_list_lock);
858 mutex_unlock(&devfreq_list_lock);
861 return ERR_PTR(-EPROBE_DEFER);
864 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
866 return ERR_PTR(-ENODEV);
868 #endif /* CONFIG_OF */
869 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
872 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
873 * @dev: the device to add devfreq feature.
874 * @devfreq: the devfreq instance to be removed
876 void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
878 WARN_ON(devres_release(dev, devm_devfreq_dev_release,
879 devm_devfreq_dev_match, devfreq));
881 EXPORT_SYMBOL(devm_devfreq_remove_device);
884 * devfreq_suspend_device() - Suspend devfreq of a device.
885 * @devfreq: the devfreq instance to be suspended
887 * This function is intended to be called by the pm callbacks
888 * (e.g., runtime_suspend, suspend) of the device driver that
891 int devfreq_suspend_device(struct devfreq *devfreq)
898 if (atomic_inc_return(&devfreq->suspend_count) > 1)
901 if (devfreq->governor) {
902 ret = devfreq->governor->event_handler(devfreq,
903 DEVFREQ_GOV_SUSPEND, NULL);
908 if (devfreq->suspend_freq) {
909 ret = devfreq_set_target(devfreq, devfreq->suspend_freq, 0);
916 EXPORT_SYMBOL(devfreq_suspend_device);
919 * devfreq_resume_device() - Resume devfreq of a device.
920 * @devfreq: the devfreq instance to be resumed
922 * This function is intended to be called by the pm callbacks
923 * (e.g., runtime_resume, resume) of the device driver that
926 int devfreq_resume_device(struct devfreq *devfreq)
933 if (atomic_dec_return(&devfreq->suspend_count) >= 1)
936 if (devfreq->resume_freq) {
937 ret = devfreq_set_target(devfreq, devfreq->resume_freq, 0);
942 if (devfreq->governor) {
943 ret = devfreq->governor->event_handler(devfreq,
944 DEVFREQ_GOV_RESUME, NULL);
951 EXPORT_SYMBOL(devfreq_resume_device);
954 * devfreq_suspend() - Suspend devfreq governors and devices
956 * Called during system wide Suspend/Hibernate cycles for suspending governors
957 * and devices preserving the state for resume. On some platforms the devfreq
958 * device must have precise state (frequency) after resume in order to provide
959 * fully operating setup.
961 void devfreq_suspend(void)
963 struct devfreq *devfreq;
966 mutex_lock(&devfreq_list_lock);
967 list_for_each_entry(devfreq, &devfreq_list, node) {
968 ret = devfreq_suspend_device(devfreq);
970 dev_err(&devfreq->dev,
971 "failed to suspend devfreq device\n");
973 mutex_unlock(&devfreq_list_lock);
977 * devfreq_resume() - Resume devfreq governors and devices
979 * Called during system wide Suspend/Hibernate cycle for resuming governors and
980 * devices that are suspended with devfreq_suspend().
982 void devfreq_resume(void)
984 struct devfreq *devfreq;
987 mutex_lock(&devfreq_list_lock);
988 list_for_each_entry(devfreq, &devfreq_list, node) {
989 ret = devfreq_resume_device(devfreq);
991 dev_warn(&devfreq->dev,
992 "failed to resume devfreq device\n");
994 mutex_unlock(&devfreq_list_lock);
998 * devfreq_add_governor() - Add devfreq governor
999 * @governor: the devfreq governor to be added
1001 int devfreq_add_governor(struct devfreq_governor *governor)
1003 struct devfreq_governor *g;
1004 struct devfreq *devfreq;
1008 pr_err("%s: Invalid parameters.\n", __func__);
1012 mutex_lock(&devfreq_list_lock);
1013 g = find_devfreq_governor(governor->name);
1015 pr_err("%s: governor %s already registered\n", __func__,
1021 list_add(&governor->node, &devfreq_governor_list);
1023 list_for_each_entry(devfreq, &devfreq_list, node) {
1025 struct device *dev = devfreq->dev.parent;
1027 if (!strncmp(devfreq->governor_name, governor->name,
1028 DEVFREQ_NAME_LEN)) {
1029 /* The following should never occur */
1030 if (devfreq->governor) {
1032 "%s: Governor %s already present\n",
1033 __func__, devfreq->governor->name);
1034 ret = devfreq->governor->event_handler(devfreq,
1035 DEVFREQ_GOV_STOP, NULL);
1038 "%s: Governor %s stop = %d\n",
1040 devfreq->governor->name, ret);
1044 devfreq->governor = governor;
1045 ret = devfreq->governor->event_handler(devfreq,
1046 DEVFREQ_GOV_START, NULL);
1048 dev_warn(dev, "%s: Governor %s start=%d\n",
1049 __func__, devfreq->governor->name,
1056 mutex_unlock(&devfreq_list_lock);
1060 EXPORT_SYMBOL(devfreq_add_governor);
1063 * devfreq_remove_governor() - Remove devfreq feature from a device.
1064 * @governor: the devfreq governor to be removed
1066 int devfreq_remove_governor(struct devfreq_governor *governor)
1068 struct devfreq_governor *g;
1069 struct devfreq *devfreq;
1073 pr_err("%s: Invalid parameters.\n", __func__);
1077 mutex_lock(&devfreq_list_lock);
1078 g = find_devfreq_governor(governor->name);
1080 pr_err("%s: governor %s not registered\n", __func__,
1085 list_for_each_entry(devfreq, &devfreq_list, node) {
1087 struct device *dev = devfreq->dev.parent;
1089 if (!strncmp(devfreq->governor_name, governor->name,
1090 DEVFREQ_NAME_LEN)) {
1091 /* we should have a devfreq governor! */
1092 if (!devfreq->governor) {
1093 dev_warn(dev, "%s: Governor %s NOT present\n",
1094 __func__, governor->name);
1098 ret = devfreq->governor->event_handler(devfreq,
1099 DEVFREQ_GOV_STOP, NULL);
1101 dev_warn(dev, "%s: Governor %s stop=%d\n",
1102 __func__, devfreq->governor->name,
1105 devfreq->governor = NULL;
1109 list_del(&governor->node);
1111 mutex_unlock(&devfreq_list_lock);
1115 EXPORT_SYMBOL(devfreq_remove_governor);
1117 static ssize_t governor_show(struct device *dev,
1118 struct device_attribute *attr, char *buf)
1120 if (!to_devfreq(dev)->governor)
1123 return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
1126 static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
1127 const char *buf, size_t count)
1129 struct devfreq *df = to_devfreq(dev);
1131 char str_governor[DEVFREQ_NAME_LEN + 1];
1132 const struct devfreq_governor *governor, *prev_governor;
1134 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
1138 mutex_lock(&devfreq_list_lock);
1139 governor = try_then_request_governor(str_governor);
1140 if (IS_ERR(governor)) {
1141 ret = PTR_ERR(governor);
1144 if (df->governor == governor) {
1147 } else if ((df->governor && df->governor->immutable) ||
1148 governor->immutable) {
1154 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1156 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1157 __func__, df->governor->name, ret);
1161 prev_governor = df->governor;
1162 df->governor = governor;
1163 strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
1164 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1166 dev_warn(dev, "%s: Governor %s not started(%d)\n",
1167 __func__, df->governor->name, ret);
1168 df->governor = prev_governor;
1169 strncpy(df->governor_name, prev_governor->name,
1171 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1174 "%s: reverting to Governor %s failed (%d)\n",
1175 __func__, df->governor_name, ret);
1176 df->governor = NULL;
1180 mutex_unlock(&devfreq_list_lock);
1186 static DEVICE_ATTR_RW(governor);
1188 static ssize_t available_governors_show(struct device *d,
1189 struct device_attribute *attr,
1192 struct devfreq *df = to_devfreq(d);
1195 mutex_lock(&devfreq_list_lock);
1198 * The devfreq with immutable governor (e.g., passive) shows
1199 * only own governor.
1201 if (df->governor->immutable) {
1202 count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
1203 "%s ", df->governor_name);
1205 * The devfreq device shows the registered governor except for
1206 * immutable governors such as passive governor .
1209 struct devfreq_governor *governor;
1211 list_for_each_entry(governor, &devfreq_governor_list, node) {
1212 if (governor->immutable)
1214 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1215 "%s ", governor->name);
1219 mutex_unlock(&devfreq_list_lock);
1221 /* Truncate the trailing space */
1225 count += sprintf(&buf[count], "\n");
1229 static DEVICE_ATTR_RO(available_governors);
1231 static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
1235 struct devfreq *devfreq = to_devfreq(dev);
1237 if (devfreq->profile->get_cur_freq &&
1238 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
1239 return sprintf(buf, "%lu\n", freq);
1241 return sprintf(buf, "%lu\n", devfreq->previous_freq);
1243 static DEVICE_ATTR_RO(cur_freq);
1245 static ssize_t target_freq_show(struct device *dev,
1246 struct device_attribute *attr, char *buf)
1248 return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
1250 static DEVICE_ATTR_RO(target_freq);
1252 static ssize_t polling_interval_show(struct device *dev,
1253 struct device_attribute *attr, char *buf)
1255 return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
1258 static ssize_t polling_interval_store(struct device *dev,
1259 struct device_attribute *attr,
1260 const char *buf, size_t count)
1262 struct devfreq *df = to_devfreq(dev);
1269 ret = sscanf(buf, "%u", &value);
1273 df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
1278 static DEVICE_ATTR_RW(polling_interval);
1280 static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
1281 const char *buf, size_t count)
1283 struct devfreq *df = to_devfreq(dev);
1284 unsigned long value;
1287 ret = sscanf(buf, "%lu", &value);
1291 mutex_lock(&df->lock);
1294 if (value > df->max_freq) {
1299 unsigned long *freq_table = df->profile->freq_table;
1301 /* Get minimum frequency according to sorting order */
1302 if (freq_table[0] < freq_table[df->profile->max_state - 1])
1303 value = freq_table[0];
1305 value = freq_table[df->profile->max_state - 1];
1308 df->min_freq = value;
1312 mutex_unlock(&df->lock);
1316 static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
1319 struct devfreq *df = to_devfreq(dev);
1321 return sprintf(buf, "%lu\n", max(df->scaling_min_freq, df->min_freq));
1324 static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
1325 const char *buf, size_t count)
1327 struct devfreq *df = to_devfreq(dev);
1328 unsigned long value;
1331 ret = sscanf(buf, "%lu", &value);
1335 mutex_lock(&df->lock);
1338 if (value < df->min_freq) {
1343 unsigned long *freq_table = df->profile->freq_table;
1345 /* Get maximum frequency according to sorting order */
1346 if (freq_table[0] < freq_table[df->profile->max_state - 1])
1347 value = freq_table[df->profile->max_state - 1];
1349 value = freq_table[0];
1352 df->max_freq = value;
1356 mutex_unlock(&df->lock);
1359 static DEVICE_ATTR_RW(min_freq);
1361 static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
1364 struct devfreq *df = to_devfreq(dev);
1366 return sprintf(buf, "%lu\n", min(df->scaling_max_freq, df->max_freq));
1368 static DEVICE_ATTR_RW(max_freq);
1370 static ssize_t available_frequencies_show(struct device *d,
1371 struct device_attribute *attr,
1374 struct devfreq *df = to_devfreq(d);
1378 mutex_lock(&df->lock);
1380 for (i = 0; i < df->profile->max_state; i++)
1381 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1382 "%lu ", df->profile->freq_table[i]);
1384 mutex_unlock(&df->lock);
1385 /* Truncate the trailing space */
1389 count += sprintf(&buf[count], "\n");
1393 static DEVICE_ATTR_RO(available_frequencies);
1395 static ssize_t trans_stat_show(struct device *dev,
1396 struct device_attribute *attr, char *buf)
1398 struct devfreq *devfreq = to_devfreq(dev);
1401 unsigned int max_state = devfreq->profile->max_state;
1403 if (!devfreq->stop_polling &&
1404 devfreq_update_status(devfreq, devfreq->previous_freq))
1407 return sprintf(buf, "Not Supported.\n");
1409 len = sprintf(buf, " From : To\n");
1410 len += sprintf(buf + len, " :");
1411 for (i = 0; i < max_state; i++)
1412 len += sprintf(buf + len, "%10lu",
1413 devfreq->profile->freq_table[i]);
1415 len += sprintf(buf + len, " time(ms)\n");
1417 for (i = 0; i < max_state; i++) {
1418 if (devfreq->profile->freq_table[i]
1419 == devfreq->previous_freq) {
1420 len += sprintf(buf + len, "*");
1422 len += sprintf(buf + len, " ");
1424 len += sprintf(buf + len, "%10lu:",
1425 devfreq->profile->freq_table[i]);
1426 for (j = 0; j < max_state; j++)
1427 len += sprintf(buf + len, "%10u",
1428 devfreq->trans_table[(i * max_state) + j]);
1429 len += sprintf(buf + len, "%10u\n",
1430 jiffies_to_msecs(devfreq->time_in_state[i]));
1433 len += sprintf(buf + len, "Total transition : %u\n",
1434 devfreq->total_trans);
1437 static DEVICE_ATTR_RO(trans_stat);
1439 static struct attribute *devfreq_attrs[] = {
1440 &dev_attr_governor.attr,
1441 &dev_attr_available_governors.attr,
1442 &dev_attr_cur_freq.attr,
1443 &dev_attr_available_frequencies.attr,
1444 &dev_attr_target_freq.attr,
1445 &dev_attr_polling_interval.attr,
1446 &dev_attr_min_freq.attr,
1447 &dev_attr_max_freq.attr,
1448 &dev_attr_trans_stat.attr,
1451 ATTRIBUTE_GROUPS(devfreq);
1453 static int __init devfreq_init(void)
1455 devfreq_class = class_create(THIS_MODULE, "devfreq");
1456 if (IS_ERR(devfreq_class)) {
1457 pr_err("%s: couldn't create class\n", __FILE__);
1458 return PTR_ERR(devfreq_class);
1461 devfreq_wq = create_freezable_workqueue("devfreq_wq");
1463 class_destroy(devfreq_class);
1464 pr_err("%s: couldn't create workqueue\n", __FILE__);
1467 devfreq_class->dev_groups = devfreq_groups;
1471 subsys_initcall(devfreq_init);
1474 * The following are helper functions for devfreq user device drivers with
1479 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1480 * freq value given to target callback.
1481 * @dev: The devfreq user device. (parent of devfreq)
1482 * @freq: The frequency given to target function
1483 * @flags: Flags handed from devfreq framework.
1485 * The callers are required to call dev_pm_opp_put() for the returned OPP after
1488 struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1489 unsigned long *freq,
1492 struct dev_pm_opp *opp;
1494 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1495 /* The freq is an upper bound. opp should be lower */
1496 opp = dev_pm_opp_find_freq_floor(dev, freq);
1498 /* If not available, use the closest opp */
1499 if (opp == ERR_PTR(-ERANGE))
1500 opp = dev_pm_opp_find_freq_ceil(dev, freq);
1502 /* The freq is an lower bound. opp should be higher */
1503 opp = dev_pm_opp_find_freq_ceil(dev, freq);
1505 /* If not available, use the closest opp */
1506 if (opp == ERR_PTR(-ERANGE))
1507 opp = dev_pm_opp_find_freq_floor(dev, freq);
1512 EXPORT_SYMBOL(devfreq_recommended_opp);
1515 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1516 * for any changes in the OPP availability
1518 * @dev: The devfreq user device. (parent of devfreq)
1519 * @devfreq: The devfreq object.
1521 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1523 return dev_pm_opp_register_notifier(dev, &devfreq->nb);
1525 EXPORT_SYMBOL(devfreq_register_opp_notifier);
1528 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1529 * notified for any changes in the OPP
1530 * availability changes anymore.
1531 * @dev: The devfreq user device. (parent of devfreq)
1532 * @devfreq: The devfreq object.
1534 * At exit() callback of devfreq_dev_profile, this must be included if
1535 * devfreq_recommended_opp is used.
1537 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1539 return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
1541 EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
1543 static void devm_devfreq_opp_release(struct device *dev, void *res)
1545 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
1549 * devm_devfreq_register_opp_notifier() - Resource-managed
1550 * devfreq_register_opp_notifier()
1551 * @dev: The devfreq user device. (parent of devfreq)
1552 * @devfreq: The devfreq object.
1554 int devm_devfreq_register_opp_notifier(struct device *dev,
1555 struct devfreq *devfreq)
1557 struct devfreq **ptr;
1560 ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
1564 ret = devfreq_register_opp_notifier(dev, devfreq);
1571 devres_add(dev, ptr);
1575 EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
1578 * devm_devfreq_unregister_opp_notifier() - Resource-managed
1579 * devfreq_unregister_opp_notifier()
1580 * @dev: The devfreq user device. (parent of devfreq)
1581 * @devfreq: The devfreq object.
1583 void devm_devfreq_unregister_opp_notifier(struct device *dev,
1584 struct devfreq *devfreq)
1586 WARN_ON(devres_release(dev, devm_devfreq_opp_release,
1587 devm_devfreq_dev_match, devfreq));
1589 EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
1592 * devfreq_register_notifier() - Register a driver with devfreq
1593 * @devfreq: The devfreq object.
1594 * @nb: The notifier block to register.
1595 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1597 int devfreq_register_notifier(struct devfreq *devfreq,
1598 struct notifier_block *nb,
1607 case DEVFREQ_TRANSITION_NOTIFIER:
1608 ret = srcu_notifier_chain_register(
1609 &devfreq->transition_notifier_list, nb);
1617 EXPORT_SYMBOL(devfreq_register_notifier);
1620 * devfreq_unregister_notifier() - Unregister a driver with devfreq
1621 * @devfreq: The devfreq object.
1622 * @nb: The notifier block to be unregistered.
1623 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1625 int devfreq_unregister_notifier(struct devfreq *devfreq,
1626 struct notifier_block *nb,
1635 case DEVFREQ_TRANSITION_NOTIFIER:
1636 ret = srcu_notifier_chain_unregister(
1637 &devfreq->transition_notifier_list, nb);
1645 EXPORT_SYMBOL(devfreq_unregister_notifier);
1647 struct devfreq_notifier_devres {
1648 struct devfreq *devfreq;
1649 struct notifier_block *nb;
1653 static void devm_devfreq_notifier_release(struct device *dev, void *res)
1655 struct devfreq_notifier_devres *this = res;
1657 devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
1661 * devm_devfreq_register_notifier()
1662 - Resource-managed devfreq_register_notifier()
1663 * @dev: The devfreq user device. (parent of devfreq)
1664 * @devfreq: The devfreq object.
1665 * @nb: The notifier block to be unregistered.
1666 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1668 int devm_devfreq_register_notifier(struct device *dev,
1669 struct devfreq *devfreq,
1670 struct notifier_block *nb,
1673 struct devfreq_notifier_devres *ptr;
1676 ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
1681 ret = devfreq_register_notifier(devfreq, nb, list);
1687 ptr->devfreq = devfreq;
1690 devres_add(dev, ptr);
1694 EXPORT_SYMBOL(devm_devfreq_register_notifier);
1697 * devm_devfreq_unregister_notifier()
1698 - Resource-managed devfreq_unregister_notifier()
1699 * @dev: The devfreq user device. (parent of devfreq)
1700 * @devfreq: The devfreq object.
1701 * @nb: The notifier block to be unregistered.
1702 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1704 void devm_devfreq_unregister_notifier(struct device *dev,
1705 struct devfreq *devfreq,
1706 struct notifier_block *nb,
1709 WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
1710 devm_devfreq_dev_match, devfreq));
1712 EXPORT_SYMBOL(devm_devfreq_unregister_notifier);