aboutsummaryrefslogtreecommitdiff
path: root/drivers/cpufreq/cpufreq_limit.c
blob: 85c66e6110ea639d2643f9be848709e183c66a87 (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
139
140
141
142
143
144
145
/*
 * Copyright (c) 2012, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * TODO:
 * Use standard cpufreq APIs to limit CPU min/max frequencies
 *
 */

#include <linux/cpu.h>
#include <linux/cpufreq.h>
#include <linux/module.h>
#include <mach/cpufreq.h>

#define CPUFREQ_LIMIT "cpufreq_limit"

static uint32_t limited_max_freq = MSM_CPUFREQ_NO_LIMIT;
static uint32_t limited_min_freq = MSM_CPUFREQ_NO_LIMIT;

static int update_cpu_freq_limits(unsigned int cpu,
			uint32_t min_freq, uint32_t max_freq)
{
	int ret;

	ret = msm_cpufreq_set_freq_limits(cpu, min_freq, max_freq);
	if (ret)
		goto err;

	ret = cpufreq_update_policy(cpu);

err:
	return ret;
}

static ssize_t show_limited_min_freq(struct kobject *kobj,
			struct attribute *attr, char *buf)
{
	return sprintf(buf, "%u\n", limited_min_freq);
}

static ssize_t store_limited_min_freq(struct kobject *kobj,
			struct attribute *attr, const char *buf, size_t count)
{
	int ret;
	unsigned long new_freq;
	uint32_t cpu;

	ret = kstrtoul(buf, 0, &new_freq);
	if (ret < 0)
		return ret;

	if (new_freq == 384000)
		new_freq = MSM_CPUFREQ_NO_LIMIT;

	for_each_possible_cpu(cpu) {
		ret = update_cpu_freq_limits(cpu, new_freq, limited_max_freq);
		if (ret)
			pr_debug("%s: Failed to limit cpu%u min freq to %lu\n",
				__func__, cpu, new_freq);
	}

	limited_min_freq = new_freq;

	return count;
}

static struct global_attr limited_min_freq_attr = __ATTR(limited_min_freq,
		S_IRUGO | S_IWUSR | S_IWGRP,
		show_limited_min_freq,
		store_limited_min_freq);

static ssize_t show_limited_max_freq(struct kobject *kobj,
			struct attribute *attr, char *buf)
{
	return sprintf(buf, "%u\n", limited_max_freq);
}

static ssize_t store_limited_max_freq(struct kobject *kobj,
			struct attribute *attr, const char *buf, size_t count)
{
	int ret;
	unsigned long new_freq;
	uint32_t cpu;

	ret = kstrtoul(buf, 0, &new_freq);
	if (ret < 0)
		return ret;

	if (new_freq == 1512000)
		new_freq = MSM_CPUFREQ_NO_LIMIT;

	for_each_possible_cpu(cpu) {
		ret = update_cpu_freq_limits(cpu, limited_min_freq, new_freq);
		if (ret)
			pr_debug("%s: Failed to limit cpu%u max freq to %lu\n",
				__func__, cpu, new_freq);
	}

	limited_max_freq = new_freq;

	return count;
}

static struct global_attr limited_max_freq_attr = __ATTR(limited_max_freq,
		S_IRUGO | S_IWUSR | S_IWGRP,
		show_limited_max_freq,
		store_limited_max_freq);

static struct attribute *cpufreq_limit_attributes[] = {
	&limited_max_freq_attr.attr,
	&limited_min_freq_attr.attr,
	NULL
};

static struct attribute_group cpufreq_limit_attr_group = {
	.attrs = cpufreq_limit_attributes,
	.name = CPUFREQ_LIMIT,
};

static int cpufreq_limit_init(void)
{
	int ret;

	ret = sysfs_create_group(kernel_kobj, &cpufreq_limit_attr_group);
	if (ret)
		pr_err("%s: sysfs_creation failed, ret=%d\n", __func__, ret);

	return ret;
}

static void cpufreq_limit_exit(void)
{
	sysfs_remove_group(kernel_kobj, &cpufreq_limit_attr_group);
}

module_init(cpufreq_limit_init);
module_exit(cpufreq_limit_exit);