blob: fad0b3fd7dc35561255aca846335e879fa2b4b7d [file] [log] [blame]
MyungJoo Hamce26c5b2011-10-02 00:19:34 +02001/*
2 * linux/drivers/devfreq/governor_simpleondemand.c
3 *
4 * Copyright (C) 2011 Samsung Electronics
5 * MyungJoo Ham <myungjoo.ham@samsung.com>
6 *
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.
10 */
11
12#include <linux/errno.h>
Nishanth Menoneff607f2012-10-29 15:01:46 -050013#include <linux/module.h>
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020014#include <linux/devfreq.h>
15#include <linux/math64.h>
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +020016#include "governor.h"
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020017
18/* Default constants for DevFreq-Simple-Ondemand (DFSO) */
19#define DFSO_UPTHRESHOLD (90)
20#define DFSO_DOWNDIFFERENCTIAL (5)
21static int devfreq_simple_ondemand_func(struct devfreq *df,
22 unsigned long *freq)
23{
Javi Merino08e75e72015-08-14 18:56:56 +010024 int err;
25 struct devfreq_dev_status *stat;
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020026 unsigned long long a, b;
27 unsigned int dfso_upthreshold = DFSO_UPTHRESHOLD;
28 unsigned int dfso_downdifferential = DFSO_DOWNDIFFERENCTIAL;
29 struct devfreq_simple_ondemand_data *data = df->data;
MyungJoo Ham6530b9de2011-12-09 16:42:19 +090030 unsigned long max = (df->max_freq) ? df->max_freq : UINT_MAX;
Sahitya Tummala4b185482013-10-21 11:27:13 +053031 unsigned long min = (df->min_freq) ? df->min_freq : 0;
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020032
Javi Merino08e75e72015-08-14 18:56:56 +010033 err = devfreq_update_stats(df);
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020034 if (err)
35 return err;
36
Javi Merino08e75e72015-08-14 18:56:56 +010037 stat = &df->last_status;
38
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020039 if (data) {
40 if (data->upthreshold)
41 dfso_upthreshold = data->upthreshold;
42 if (data->downdifferential)
43 dfso_downdifferential = data->downdifferential;
44 }
45 if (dfso_upthreshold > 100 ||
46 dfso_upthreshold < dfso_downdifferential)
47 return -EINVAL;
48
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020049 /* Prevent overflow */
Javi Merino08e75e72015-08-14 18:56:56 +010050 if (stat->busy_time >= (1 << 24) || stat->total_time >= (1 << 24)) {
51 stat->busy_time >>= 7;
52 stat->total_time >>= 7;
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020053 }
54
Sahitya Tummala4b185482013-10-21 11:27:13 +053055 if (data && data->simple_scaling) {
56 if (stat->busy_time * 100 >
57 stat->total_time * dfso_upthreshold)
58 *freq = max;
59 else if (stat->busy_time * 100 <
60 stat->total_time * dfso_downdifferential)
61 *freq = min;
62 else
63 *freq = df->previous_freq;
64 return 0;
65 }
66
67 /* Assume MAX if it is going to be divided by zero */
68 if (stat->total_time == 0) {
69 *freq = max;
70 return 0;
71 }
72
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020073 /* Set MAX if it's busy enough */
Javi Merino08e75e72015-08-14 18:56:56 +010074 if (stat->busy_time * 100 >
75 stat->total_time * dfso_upthreshold) {
MyungJoo Ham6530b9de2011-12-09 16:42:19 +090076 *freq = max;
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020077 return 0;
78 }
79
80 /* Set MAX if we do not know the initial frequency */
Javi Merino08e75e72015-08-14 18:56:56 +010081 if (stat->current_frequency == 0) {
MyungJoo Ham6530b9de2011-12-09 16:42:19 +090082 *freq = max;
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020083 return 0;
84 }
85
86 /* Keep the current frequency */
Javi Merino08e75e72015-08-14 18:56:56 +010087 if (stat->busy_time * 100 >
88 stat->total_time * (dfso_upthreshold - dfso_downdifferential)) {
89 *freq = stat->current_frequency;
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020090 return 0;
91 }
92
93 /* Set the desired frequency based on the load */
Javi Merino08e75e72015-08-14 18:56:56 +010094 a = stat->busy_time;
95 a *= stat->current_frequency;
96 b = div_u64(a, stat->total_time);
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020097 b *= 100;
98 b = div_u64(b, (dfso_upthreshold - dfso_downdifferential / 2));
99 *freq = (unsigned long) b;
100
MyungJoo Ham6530b9de2011-12-09 16:42:19 +0900101 if (df->min_freq && *freq < df->min_freq)
102 *freq = df->min_freq;
103 if (df->max_freq && *freq > df->max_freq)
104 *freq = df->max_freq;
105
MyungJoo Hamce26c5b2011-10-02 00:19:34 +0200106 return 0;
107}
108
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200109static int devfreq_simple_ondemand_handler(struct devfreq *devfreq,
110 unsigned int event, void *data)
111{
112 switch (event) {
113 case DEVFREQ_GOV_START:
114 devfreq_monitor_start(devfreq);
115 break;
116
117 case DEVFREQ_GOV_STOP:
118 devfreq_monitor_stop(devfreq);
119 break;
120
121 case DEVFREQ_GOV_INTERVAL:
122 devfreq_interval_update(devfreq, (unsigned int *)data);
123 break;
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200124
125 case DEVFREQ_GOV_SUSPEND:
126 devfreq_monitor_suspend(devfreq);
127 break;
128
129 case DEVFREQ_GOV_RESUME:
130 devfreq_monitor_resume(devfreq);
131 break;
132
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200133 default:
134 break;
135 }
136
137 return 0;
138}
139
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500140static struct devfreq_governor devfreq_simple_ondemand = {
MyungJoo Hamce26c5b2011-10-02 00:19:34 +0200141 .name = "simple_ondemand",
142 .get_target_freq = devfreq_simple_ondemand_func,
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200143 .event_handler = devfreq_simple_ondemand_handler,
MyungJoo Hamce26c5b2011-10-02 00:19:34 +0200144};
Nishanth Menon83116e62012-10-29 15:01:44 -0500145
146static int __init devfreq_simple_ondemand_init(void)
147{
148 return devfreq_add_governor(&devfreq_simple_ondemand);
149}
150subsys_initcall(devfreq_simple_ondemand_init);
151
152static void __exit devfreq_simple_ondemand_exit(void)
153{
154 int ret;
155
156 ret = devfreq_remove_governor(&devfreq_simple_ondemand);
157 if (ret)
158 pr_err("%s: failed remove governor %d\n", __func__, ret);
159
160 return;
161}
162module_exit(devfreq_simple_ondemand_exit);
Nishanth Menoneff607f2012-10-29 15:01:46 -0500163MODULE_LICENSE("GPL");