aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/power/cpufreq_governor_chg.c
blob: 719b5f99cb4fd253fce8d38c1a646772068e57f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <linux/fs.h>
#include <linux/cpufreq.h>
#if 0
extern void cpufreq_save_default_governor(void);
extern void cpufreq_restore_default_governor(void);
extern void cpufreq_set_conservative_governor(void);
extern void cpufreq_set_performance_governor(void);
extern void cpufreq_set_conservative_governor_param(int up_th, int down_th);
#endif

#define GOV_CHG_DBG		1

#define SET_CONSERVATIVE_GOVERNOR_UP_THRESHOLD 95
#define SET_CONSERVATIVE_GOVERNOR_DOWN_THRESHOLD 50

static char cpufreq_gov_default[32];

static char *sz_cpufreq_gov_performance = "performance";
static char *sz_cpufreq_gov_conservative = "conservative";

static char *cpufreq_sysfs_place_holder = "/sys/devices/system/cpu/cpu%i/cpufreq/scaling_governor";
static char *cpufreq_gov_conservative_param = "/sys/devices/system/cpu/cpufreq/conservative/%s";

static void cpufreq_set_governor(char *governor)
{
	struct file *scaling_gov = NULL;
	char    buf[128];
	int i;
	loff_t offset = 0;

	if (governor == NULL)
		return;

	for_each_online_cpu(i) {
		sprintf(buf, cpufreq_sysfs_place_holder, i);
		scaling_gov = filp_open(buf, O_RDWR, 0);
		if (scaling_gov != NULL) {
			if (scaling_gov->f_op != NULL &&
				scaling_gov->f_op->write != NULL) 
			{
				scaling_gov->f_op->write(scaling_gov,
						governor,
						strlen(governor),
						&offset);
#ifdef GOV_CHG_DBG//[
				printk("%s():set policy \"%s\"\n",__FUNCTION__,governor);
#endif //]GOV_CHG_DBG
			}
			else
				pr_err("f_op might be null\n");

			filp_close(scaling_gov, NULL);
		} else {
			pr_err("%s. Can't open %s\n", __func__, buf);
		}
	}
}

void cpufreq_save_default_governor(void)
{
	int ret;
	struct cpufreq_policy current_policy;
	ret = cpufreq_get_policy(&current_policy, 0);
	if (ret < 0)
		pr_err("%s: cpufreq_get_policy got error", __func__);
	memcpy(cpufreq_gov_default, current_policy.governor->name, 32);
#ifdef GOV_CHG_DBG//[
	printk("%s():save policy \"%s\"\n",__FUNCTION__,cpufreq_gov_default);
#endif //]GOV_CHG_DBG
}

void cpufreq_restore_default_governor(void)
{
	cpufreq_set_governor(cpufreq_gov_default);
#ifdef GOV_CHG_DBG//[
	printk("%s():restore policy \"%s\"\n",__FUNCTION__,cpufreq_gov_default);
#endif //]GOV_CHG_DBG
}

void cpufreq_set_conservative_governor_param(int up_th, int down_th)
{
	struct file *gov_param = NULL;
	static char buf[128], parm[8];
	loff_t offset = 0;

	if (up_th <= down_th) {
		printk(KERN_ERR "%s: up_th(%d) is lesser than down_th(%d)\n",
			__func__, up_th, down_th);
		return;
	}

	sprintf(parm, "%d", up_th);
	sprintf(buf, cpufreq_gov_conservative_param , "up_threshold");
	gov_param = filp_open(buf, O_RDONLY, 0);
	if (gov_param != NULL) {
		if (gov_param->f_op != NULL &&
			gov_param->f_op->write != NULL)
			gov_param->f_op->write(gov_param,
					parm,
					strlen(parm),
					&offset);
		else
			pr_err("f_op might be null\n");

		filp_close(gov_param, NULL);
	} else {
		pr_err("%s. Can't open %s\n", __func__, buf);
	}

	sprintf(parm, "%d", down_th);
	sprintf(buf, cpufreq_gov_conservative_param , "down_threshold");
	gov_param = filp_open(buf, O_RDONLY, 0);
	if (gov_param != NULL) {
		if (gov_param->f_op != NULL &&
			gov_param->f_op->write != NULL)
			gov_param->f_op->write(gov_param,
					parm,
					strlen(parm),
					&offset);
		else
			pr_err("f_op might be null\n");

		filp_close(gov_param, NULL);
	} else {
		pr_err("%s. Can't open %s\n", __func__, buf);
	}
}
void cpufreq_set_performance_governor(void)
{
	cpufreq_set_governor(sz_cpufreq_gov_performance);
}
void cpufreq_set_conservative_governor(void)
{
	cpufreq_set_governor(sz_cpufreq_gov_conservative);
}