blob: ae72ba5e78dfce046804a7dc5b7a08017d8a25c7 [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;
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020031
Javi Merino08e75e72015-08-14 18:56:56 +010032 err = devfreq_update_stats(df);
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020033 if (err)
34 return err;
35
Javi Merino08e75e72015-08-14 18:56:56 +010036 stat = &df->last_status;
37
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020038 if (data) {
39 if (data->upthreshold)
40 dfso_upthreshold = data->upthreshold;
41 if (data->downdifferential)
42 dfso_downdifferential = data->downdifferential;
43 }
44 if (dfso_upthreshold > 100 ||
45 dfso_upthreshold < dfso_downdifferential)
46 return -EINVAL;
47
48 /* Assume MAX if it is going to be divided by zero */
Javi Merino08e75e72015-08-14 18:56:56 +010049 if (stat->total_time == 0) {
MyungJoo Ham6530b9de2011-12-09 16:42:19 +090050 *freq = max;
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020051 return 0;
52 }
53
54 /* Prevent overflow */
Javi Merino08e75e72015-08-14 18:56:56 +010055 if (stat->busy_time >= (1 << 24) || stat->total_time >= (1 << 24)) {
56 stat->busy_time >>= 7;
57 stat->total_time >>= 7;
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020058 }
59
60 /* Set MAX if it's busy enough */
Javi Merino08e75e72015-08-14 18:56:56 +010061 if (stat->busy_time * 100 >
62 stat->total_time * dfso_upthreshold) {
MyungJoo Ham6530b9de2011-12-09 16:42:19 +090063 *freq = max;
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020064 return 0;
65 }
66
67 /* Set MAX if we do not know the initial frequency */
Javi Merino08e75e72015-08-14 18:56:56 +010068 if (stat->current_frequency == 0) {
MyungJoo Ham6530b9de2011-12-09 16:42:19 +090069 *freq = max;
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020070 return 0;
71 }
72
73 /* Keep the current frequency */
Javi Merino08e75e72015-08-14 18:56:56 +010074 if (stat->busy_time * 100 >
75 stat->total_time * (dfso_upthreshold - dfso_downdifferential)) {
76 *freq = stat->current_frequency;
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020077 return 0;
78 }
79
80 /* Set the desired frequency based on the load */
Javi Merino08e75e72015-08-14 18:56:56 +010081 a = stat->busy_time;
82 a *= stat->current_frequency;
83 b = div_u64(a, stat->total_time);
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020084 b *= 100;
85 b = div_u64(b, (dfso_upthreshold - dfso_downdifferential / 2));
86 *freq = (unsigned long) b;
87
MyungJoo Ham6530b9de2011-12-09 16:42:19 +090088 if (df->min_freq && *freq < df->min_freq)
89 *freq = df->min_freq;
90 if (df->max_freq && *freq > df->max_freq)
91 *freq = df->max_freq;
92
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020093 return 0;
94}
95
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +020096static int devfreq_simple_ondemand_handler(struct devfreq *devfreq,
97 unsigned int event, void *data)
98{
99 switch (event) {
100 case DEVFREQ_GOV_START:
101 devfreq_monitor_start(devfreq);
102 break;
103
104 case DEVFREQ_GOV_STOP:
105 devfreq_monitor_stop(devfreq);
106 break;
107
108 case DEVFREQ_GOV_INTERVAL:
109 devfreq_interval_update(devfreq, (unsigned int *)data);
110 break;
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200111
112 case DEVFREQ_GOV_SUSPEND:
113 devfreq_monitor_suspend(devfreq);
114 break;
115
116 case DEVFREQ_GOV_RESUME:
117 devfreq_monitor_resume(devfreq);
118 break;
119
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200120 default:
121 break;
122 }
123
124 return 0;
125}
126
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500127static struct devfreq_governor devfreq_simple_ondemand = {
MyungJoo Hamce26c5b2011-10-02 00:19:34 +0200128 .name = "simple_ondemand",
129 .get_target_freq = devfreq_simple_ondemand_func,
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200130 .event_handler = devfreq_simple_ondemand_handler,
MyungJoo Hamce26c5b2011-10-02 00:19:34 +0200131};
Nishanth Menon83116e62012-10-29 15:01:44 -0500132
133static int __init devfreq_simple_ondemand_init(void)
134{
135 return devfreq_add_governor(&devfreq_simple_ondemand);
136}
137subsys_initcall(devfreq_simple_ondemand_init);
138
139static void __exit devfreq_simple_ondemand_exit(void)
140{
141 int ret;
142
143 ret = devfreq_remove_governor(&devfreq_simple_ondemand);
144 if (ret)
145 pr_err("%s: failed remove governor %d\n", __func__, ret);
146
147 return;
148}
149module_exit(devfreq_simple_ondemand_exit);
Nishanth Menoneff607f2012-10-29 15:01:46 -0500150MODULE_LICENSE("GPL");