summaryrefslogtreecommitdiffstats
blob: a58eb390a5369806bd594a821f90416f58b92b4e (plain) (blame)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * Copyright (C) 2012 Texas Instruments, Inc
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License 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.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0))

#include <linux/thermal.h>

static struct cool_data {
	int freq_cnt;
	unsigned long *freq_list;
	unsigned long state;
	struct thermal_cooling_device *cdev;
} cd;

static int sgxfreq_get_max_state(struct thermal_cooling_device *cdev,
		unsigned long *state)
{
	*state = cd.freq_cnt - 1;
	return 0;
}

static int sgxfreq_get_cur_state(struct thermal_cooling_device *cdev,
		unsigned long *state)
{
	*state = cd.state;
	return 0;
}

static int sgxfreq_set_cur_state(struct thermal_cooling_device *cdev,
		unsigned long state)
{
	int freq_max_index, freq_limit_index;

	freq_max_index = cd.freq_cnt - 1;

	freq_limit_index = freq_max_index - (unsigned int)state;

	if (freq_limit_index < 0)
		freq_limit_index = 0;

	sgxfreq_set_freq_limit(cd.freq_list[freq_limit_index]);

	cd.state = state;
	return 0;
}


static const struct thermal_cooling_device_ops sgxfreq_cooling_ops = {
	.get_max_state = sgxfreq_get_max_state,
	.get_cur_state = sgxfreq_get_cur_state,
	.set_cur_state = sgxfreq_set_cur_state,
};

int cool_init(void)
{
	int ret;
	struct thermal_zone_device *tz;

	cd.freq_cnt = sgxfreq_get_freq_list(&cd.freq_list);
	if (!cd.freq_cnt || !cd.freq_list)
		return -EINVAL;

	cd.cdev = thermal_cooling_device_register("gpu", (void *)NULL, &sgxfreq_cooling_ops);

	if(IS_ERR(cd.cdev)) {
		pr_err("sgxfreq: Error while regeistering cooling device: %ld\n", PTR_ERR(cd.cdev));
		return -1;
	}

	tz = thermal_zone_get_zone_by_name("gpu");
	if(IS_ERR(tz)) {
		pr_err("sgxfreq: Error while trying to obtain zone device: %ld\n", PTR_ERR(tz));
		return -1;
	}

	ret = thermal_zone_bind_cooling_device(tz, 0, cd.cdev, THERMAL_NO_LIMIT, THERMAL_NO_LIMIT);
	if (ret)
	{
		pr_err("sgxfreq: Error binding cooling device: %d\n", ret);
	}

	return 0;
}

void cool_deinit(void)
{
	thermal_cooling_device_unregister(cd.cdev);
}
#else //if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0))
#include <linux/thermal_framework.h>

static int cool_device(struct thermal_dev *dev, int cooling_level);

static struct cool_data {
	int freq_cnt;
	unsigned long *freq_list;
} cd;

static struct thermal_dev_ops cool_dev_ops = {
	.cool_device = cool_device,
};

static struct thermal_dev cool_dev = {
	.name = "gpu_cooling.0",
	.domain_name = "gpu",
	.dev_ops = &cool_dev_ops,
};

static struct thermal_dev case_cool_dev = {
	.name = "gpu_cooling.1",
	.domain_name = "case",
	.dev_ops = &cool_dev_ops,
};

static unsigned int gpu_cooling_level;
#if defined(CONFIG_CASE_TEMP_GOVERNOR)
static unsigned int case_cooling_level;
#endif

int cool_init(void)
{
	int ret;
	cd.freq_cnt = sgxfreq_get_freq_list(&cd.freq_list);
	if (!cd.freq_cnt || !cd.freq_list)
		return -EINVAL;

	ret = thermal_cooling_dev_register(&cool_dev);
	if (ret)
		return ret;

	return thermal_cooling_dev_register(&case_cool_dev);
}

void cool_deinit(void)
{
	thermal_cooling_dev_unregister(&cool_dev);
	thermal_cooling_dev_unregister(&case_cool_dev);
}

static int cool_device(struct thermal_dev *dev, int cooling_level)
{
	int freq_max_index, freq_limit_index;

#if defined(CONFIG_CASE_TEMP_GOVERNOR)
	if (!strcmp(dev->domain_name, "case"))
	{
		int tmp = 0;
		tmp = cooling_level - case_subzone_number;
		if (tmp < 0)
			tmp = 0;
		case_cooling_level = tmp;
	}
	else
#endif
	{
               gpu_cooling_level = cooling_level;
	}

	freq_max_index = cd.freq_cnt - 1;
#if defined(CONFIG_CASE_TEMP_GOVERNOR)
	if (case_cooling_level > gpu_cooling_level)
	{
		freq_limit_index = freq_max_index - case_cooling_level;
	}
	else
#endif
	{
		freq_limit_index = freq_max_index - gpu_cooling_level;
	}

	if (freq_limit_index < 0)
		freq_limit_index = 0;

	sgxfreq_set_freq_limit(cd.freq_list[freq_limit_index]);

	return 0;
}
#endif //if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0))