xref: /linux/drivers/devfreq/governor_simpleondemand.c (revision f2ee442115c9b6219083c019939a9cc0c9abb2f8)
1 /*
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>
15 
16 /* Default constants for DevFreq-Simple-Ondemand (DFSO) */
17 #define DFSO_UPTHRESHOLD	(90)
18 #define DFSO_DOWNDIFFERENCTIAL	(5)
19 static int devfreq_simple_ondemand_func(struct devfreq *df,
20 					unsigned long *freq)
21 {
22 	struct devfreq_dev_status stat;
23 	int err = df->profile->get_dev_status(df->dev.parent, &stat);
24 	unsigned long long a, b;
25 	unsigned int dfso_upthreshold = DFSO_UPTHRESHOLD;
26 	unsigned int dfso_downdifferential = DFSO_DOWNDIFFERENCTIAL;
27 	struct devfreq_simple_ondemand_data *data = df->data;
28 
29 	if (err)
30 		return err;
31 
32 	if (data) {
33 		if (data->upthreshold)
34 			dfso_upthreshold = data->upthreshold;
35 		if (data->downdifferential)
36 			dfso_downdifferential = data->downdifferential;
37 	}
38 	if (dfso_upthreshold > 100 ||
39 	    dfso_upthreshold < dfso_downdifferential)
40 		return -EINVAL;
41 
42 	/* Assume MAX if it is going to be divided by zero */
43 	if (stat.total_time == 0) {
44 		*freq = UINT_MAX;
45 		return 0;
46 	}
47 
48 	/* Prevent overflow */
49 	if (stat.busy_time >= (1 << 24) || stat.total_time >= (1 << 24)) {
50 		stat.busy_time >>= 7;
51 		stat.total_time >>= 7;
52 	}
53 
54 	/* Set MAX if it's busy enough */
55 	if (stat.busy_time * 100 >
56 	    stat.total_time * dfso_upthreshold) {
57 		*freq = UINT_MAX;
58 		return 0;
59 	}
60 
61 	/* Set MAX if we do not know the initial frequency */
62 	if (stat.current_frequency == 0) {
63 		*freq = UINT_MAX;
64 		return 0;
65 	}
66 
67 	/* Keep the current frequency */
68 	if (stat.busy_time * 100 >
69 	    stat.total_time * (dfso_upthreshold - dfso_downdifferential)) {
70 		*freq = stat.current_frequency;
71 		return 0;
72 	}
73 
74 	/* Set the desired frequency based on the load */
75 	a = stat.busy_time;
76 	a *= stat.current_frequency;
77 	b = div_u64(a, stat.total_time);
78 	b *= 100;
79 	b = div_u64(b, (dfso_upthreshold - dfso_downdifferential / 2));
80 	*freq = (unsigned long) b;
81 
82 	return 0;
83 }
84 
85 const struct devfreq_governor devfreq_simple_ondemand = {
86 	.name = "simple_ondemand",
87 	.get_target_freq = devfreq_simple_ondemand_func,
88 };
89