2 * linux/drivers/devfreq/governor_simpleondemand.c
4 * Copyright (C) 2011 Samsung Electronics
5 * MyungJoo Ham <myungjoo.ham@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/errno.h>
13 #include <linux/devfreq.h>
14 #include <linux/math64.h>
17 /* Default constants for DevFreq-Simple-Ondemand (DFSO) */
18 #define DFSO_UPTHRESHOLD (90)
19 #define DFSO_DOWNDIFFERENCTIAL (5)
20 static int devfreq_simple_ondemand_func(struct devfreq *df,
23 struct devfreq_dev_status stat;
24 int err = df->profile->get_dev_status(df->dev.parent, &stat);
25 unsigned long long a, b;
26 unsigned int dfso_upthreshold = DFSO_UPTHRESHOLD;
27 unsigned int dfso_downdifferential = DFSO_DOWNDIFFERENCTIAL;
28 struct devfreq_simple_ondemand_data *data = df->data;
29 unsigned long max = (df->max_freq) ? df->max_freq : UINT_MAX;
35 if (data->upthreshold)
36 dfso_upthreshold = data->upthreshold;
37 if (data->downdifferential)
38 dfso_downdifferential = data->downdifferential;
40 if (dfso_upthreshold > 100 ||
41 dfso_upthreshold < dfso_downdifferential)
44 /* Assume MAX if it is going to be divided by zero */
45 if (stat.total_time == 0) {
50 /* Prevent overflow */
51 if (stat.busy_time >= (1 << 24) || stat.total_time >= (1 << 24)) {
53 stat.total_time >>= 7;
56 /* Set MAX if it's busy enough */
57 if (stat.busy_time * 100 >
58 stat.total_time * dfso_upthreshold) {
63 /* Set MAX if we do not know the initial frequency */
64 if (stat.current_frequency == 0) {
69 /* Keep the current frequency */
70 if (stat.busy_time * 100 >
71 stat.total_time * (dfso_upthreshold - dfso_downdifferential)) {
72 *freq = stat.current_frequency;
76 /* Set the desired frequency based on the load */
78 a *= stat.current_frequency;
79 b = div_u64(a, stat.total_time);
81 b = div_u64(b, (dfso_upthreshold - dfso_downdifferential / 2));
82 *freq = (unsigned long) b;
84 if (df->min_freq && *freq < df->min_freq)
86 if (df->max_freq && *freq > df->max_freq)
92 static int devfreq_simple_ondemand_handler(struct devfreq *devfreq,
93 unsigned int event, void *data)
96 case DEVFREQ_GOV_START:
97 devfreq_monitor_start(devfreq);
100 case DEVFREQ_GOV_STOP:
101 devfreq_monitor_stop(devfreq);
104 case DEVFREQ_GOV_INTERVAL:
105 devfreq_interval_update(devfreq, (unsigned int *)data);
108 case DEVFREQ_GOV_SUSPEND:
109 devfreq_monitor_suspend(devfreq);
112 case DEVFREQ_GOV_RESUME:
113 devfreq_monitor_resume(devfreq);
123 const struct devfreq_governor devfreq_simple_ondemand = {
124 .name = "simple_ondemand",
125 .get_target_freq = devfreq_simple_ondemand_func,
126 .event_handler = devfreq_simple_ondemand_handler,
129 static int __init devfreq_simple_ondemand_init(void)
131 return devfreq_add_governor(&devfreq_simple_ondemand);
133 subsys_initcall(devfreq_simple_ondemand_init);
135 static void __exit devfreq_simple_ondemand_exit(void)
139 ret = devfreq_remove_governor(&devfreq_simple_ondemand);
141 pr_err("%s: failed remove governor %d\n", __func__, ret);
145 module_exit(devfreq_simple_ondemand_exit);