blob: b5cf0fb24efeb4d15ed762bf6df5a28a49bfc73b [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>
13#include <linux/devfreq.h>
14#include <linux/math64.h>
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +020015#include "governor.h"
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020016
17/* Default constants for DevFreq-Simple-Ondemand (DFSO) */
18#define DFSO_UPTHRESHOLD (90)
19#define DFSO_DOWNDIFFERENCTIAL (5)
20static int devfreq_simple_ondemand_func(struct devfreq *df,
21 unsigned long *freq)
22{
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;
MyungJoo Ham6530b9de2011-12-09 16:42:19 +090029 unsigned long max = (df->max_freq) ? df->max_freq : UINT_MAX;
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020030
31 if (err)
32 return err;
33
34 if (data) {
35 if (data->upthreshold)
36 dfso_upthreshold = data->upthreshold;
37 if (data->downdifferential)
38 dfso_downdifferential = data->downdifferential;
39 }
40 if (dfso_upthreshold > 100 ||
41 dfso_upthreshold < dfso_downdifferential)
42 return -EINVAL;
43
44 /* Assume MAX if it is going to be divided by zero */
45 if (stat.total_time == 0) {
MyungJoo Ham6530b9de2011-12-09 16:42:19 +090046 *freq = max;
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020047 return 0;
48 }
49
50 /* Prevent overflow */
51 if (stat.busy_time >= (1 << 24) || stat.total_time >= (1 << 24)) {
52 stat.busy_time >>= 7;
53 stat.total_time >>= 7;
54 }
55
56 /* Set MAX if it's busy enough */
57 if (stat.busy_time * 100 >
58 stat.total_time * dfso_upthreshold) {
MyungJoo Ham6530b9de2011-12-09 16:42:19 +090059 *freq = max;
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020060 return 0;
61 }
62
63 /* Set MAX if we do not know the initial frequency */
64 if (stat.current_frequency == 0) {
MyungJoo Ham6530b9de2011-12-09 16:42:19 +090065 *freq = max;
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020066 return 0;
67 }
68
69 /* Keep the current frequency */
70 if (stat.busy_time * 100 >
71 stat.total_time * (dfso_upthreshold - dfso_downdifferential)) {
72 *freq = stat.current_frequency;
73 return 0;
74 }
75
76 /* Set the desired frequency based on the load */
77 a = stat.busy_time;
78 a *= stat.current_frequency;
79 b = div_u64(a, stat.total_time);
80 b *= 100;
81 b = div_u64(b, (dfso_upthreshold - dfso_downdifferential / 2));
82 *freq = (unsigned long) b;
83
MyungJoo Ham6530b9de2011-12-09 16:42:19 +090084 if (df->min_freq && *freq < df->min_freq)
85 *freq = df->min_freq;
86 if (df->max_freq && *freq > df->max_freq)
87 *freq = df->max_freq;
88
MyungJoo Hamce26c5b2011-10-02 00:19:34 +020089 return 0;
90}
91
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +020092static int devfreq_simple_ondemand_handler(struct devfreq *devfreq,
93 unsigned int event, void *data)
94{
95 switch (event) {
96 case DEVFREQ_GOV_START:
97 devfreq_monitor_start(devfreq);
98 break;
99
100 case DEVFREQ_GOV_STOP:
101 devfreq_monitor_stop(devfreq);
102 break;
103
104 case DEVFREQ_GOV_INTERVAL:
105 devfreq_interval_update(devfreq, (unsigned int *)data);
106 break;
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200107
108 case DEVFREQ_GOV_SUSPEND:
109 devfreq_monitor_suspend(devfreq);
110 break;
111
112 case DEVFREQ_GOV_RESUME:
113 devfreq_monitor_resume(devfreq);
114 break;
115
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200116 default:
117 break;
118 }
119
120 return 0;
121}
122
MyungJoo Hamce26c5b2011-10-02 00:19:34 +0200123const struct devfreq_governor devfreq_simple_ondemand = {
124 .name = "simple_ondemand",
125 .get_target_freq = devfreq_simple_ondemand_func,
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200126 .event_handler = devfreq_simple_ondemand_handler,
MyungJoo Hamce26c5b2011-10-02 00:19:34 +0200127};