blob: bb2936051ccc8567c7056f46fdd83cdb51ade65c [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 Menondd556fa2012-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 Venkat97ad73e2012-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,
Lucille Sylvester3b6ee292013-06-27 10:56:18 -060022 unsigned long *freq,
23 u32 *flag)
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020024{
25 struct devfreq_dev_status stat;
26 int err = df->profile->get_dev_status(df->dev.parent, &stat);
27 unsigned long long a, b;
28 unsigned int dfso_upthreshold = DFSO_UPTHRESHOLD;
29 unsigned int dfso_downdifferential = DFSO_DOWNDIFFERENCTIAL;
30 struct devfreq_simple_ondemand_data *data = df->data;
MyungJoo Ham6530b9d2011-12-09 16:42:19 +090031 unsigned long max = (df->max_freq) ? df->max_freq : UINT_MAX;
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020032
33 if (err)
34 return err;
35
36 if (data) {
37 if (data->upthreshold)
38 dfso_upthreshold = data->upthreshold;
39 if (data->downdifferential)
40 dfso_downdifferential = data->downdifferential;
41 }
42 if (dfso_upthreshold > 100 ||
43 dfso_upthreshold < dfso_downdifferential)
44 return -EINVAL;
45
46 /* Assume MAX if it is going to be divided by zero */
47 if (stat.total_time == 0) {
MyungJoo Ham6530b9d2011-12-09 16:42:19 +090048 *freq = max;
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020049 return 0;
50 }
51
52 /* Prevent overflow */
53 if (stat.busy_time >= (1 << 24) || stat.total_time >= (1 << 24)) {
54 stat.busy_time >>= 7;
55 stat.total_time >>= 7;
56 }
57
58 /* Set MAX if it's busy enough */
59 if (stat.busy_time * 100 >
60 stat.total_time * dfso_upthreshold) {
MyungJoo Ham6530b9d2011-12-09 16:42:19 +090061 *freq = max;
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020062 return 0;
63 }
64
65 /* Set MAX if we do not know the initial frequency */
66 if (stat.current_frequency == 0) {
MyungJoo Ham6530b9d2011-12-09 16:42:19 +090067 *freq = max;
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020068 return 0;
69 }
70
71 /* Keep the current frequency */
72 if (stat.busy_time * 100 >
73 stat.total_time * (dfso_upthreshold - dfso_downdifferential)) {
74 *freq = stat.current_frequency;
75 return 0;
76 }
77
78 /* Set the desired frequency based on the load */
79 a = stat.busy_time;
80 a *= stat.current_frequency;
81 b = div_u64(a, stat.total_time);
82 b *= 100;
83 b = div_u64(b, (dfso_upthreshold - dfso_downdifferential / 2));
84 *freq = (unsigned long) b;
85
MyungJoo Ham6530b9d2011-12-09 16:42:19 +090086 if (df->min_freq && *freq < df->min_freq)
87 *freq = df->min_freq;
88 if (df->max_freq && *freq > df->max_freq)
89 *freq = df->max_freq;
90
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020091 return 0;
92}
93
Rajagopal Venkat97ad73e2012-10-26 01:50:09 +020094static int devfreq_simple_ondemand_handler(struct devfreq *devfreq,
95 unsigned int event, void *data)
96{
97 switch (event) {
98 case DEVFREQ_GOV_START:
99 devfreq_monitor_start(devfreq);
100 break;
101
102 case DEVFREQ_GOV_STOP:
103 devfreq_monitor_stop(devfreq);
104 break;
105
106 case DEVFREQ_GOV_INTERVAL:
107 devfreq_interval_update(devfreq, (unsigned int *)data);
108 break;
Rajagopal Venkat6efab212012-10-26 01:50:18 +0200109
110 case DEVFREQ_GOV_SUSPEND:
111 devfreq_monitor_suspend(devfreq);
112 break;
113
114 case DEVFREQ_GOV_RESUME:
115 devfreq_monitor_resume(devfreq);
116 break;
117
Rajagopal Venkat97ad73e2012-10-26 01:50:09 +0200118 default:
119 break;
120 }
121
122 return 0;
123}
124
Nishanth Menona6134a42012-10-29 15:01:45 -0500125static struct devfreq_governor devfreq_simple_ondemand = {
MyungJoo Hamce26c5b2011-10-02 00:19:34 +0200126 .name = "simple_ondemand",
127 .get_target_freq = devfreq_simple_ondemand_func,
Rajagopal Venkat97ad73e2012-10-26 01:50:09 +0200128 .event_handler = devfreq_simple_ondemand_handler,
MyungJoo Hamce26c5b2011-10-02 00:19:34 +0200129};
Nishanth Menon5b2f54e2012-10-29 15:01:44 -0500130
131static int __init devfreq_simple_ondemand_init(void)
132{
133 return devfreq_add_governor(&devfreq_simple_ondemand);
134}
135subsys_initcall(devfreq_simple_ondemand_init);
136
137static void __exit devfreq_simple_ondemand_exit(void)
138{
139 int ret;
140
141 ret = devfreq_remove_governor(&devfreq_simple_ondemand);
142 if (ret)
143 pr_err("%s: failed remove governor %d\n", __func__, ret);
144
145 return;
146}
147module_exit(devfreq_simple_ondemand_exit);
Nishanth Menondd556fa2012-10-29 15:01:46 -0500148MODULE_LICENSE("GPL");