summaryrefslogtreecommitdiff
path: root/arch/arm/mach-tegra/tegra3_thermal.c
blob: f6f3c40007e63fe25575d795a93041870c6cb10c (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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * arch/arm/mach-tegra/tegra3_thermal.c
 *
 * Copyright (C) 2010-2011 NVIDIA Corporation.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * 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.
 *
 */

#include <linux/kernel.h>
#include <linux/cpufreq.h>
#include <linux/delay.h>
#include <linux/mutex.h>
#include <linux/init.h>
#include <linux/err.h>
#include <linux/clk.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>
#include <linux/uaccess.h>
#include <linux/thermal.h>
#include <mach/thermal.h>
#include <mach/edp.h>
#include <linux/slab.h>

#include "clock.h"
#include "cpu-tegra.h"
#include "dvfs.h"

#define MAX_ZONES (16)

struct tegra_thermal {
	struct tegra_thermal_data data;
	struct tegra_thermal_device *device;
#ifdef CONFIG_TEGRA_THERMAL_SYSFS
	struct thermal_zone_device *thz;
#endif
#ifdef CONFIG_TEGRA_EDP_LIMITS
	int edp_thermal_zone_val;
#endif
};

static struct tegra_thermal thermal_state = {
	.device = NULL,
#ifdef CONFIG_TEGRA_EDP_LIMITS
	.edp_thermal_zone_val = -1,
#endif
};

#ifndef CONFIG_TEGRA_THERMAL_SYSFS
static bool throttle_enb;
struct mutex mutex;
#endif

#ifdef CONFIG_TEGRA_THERMAL_SYSFS

static int tegra_thermal_zone_bind(struct thermal_zone_device *thermal,
				struct thermal_cooling_device *cdevice) {
	/* Support only Thermal Throttling (1 trip) for now */
	return thermal_zone_bind_cooling_device(thermal, 0, cdevice);
}

static int tegra_thermal_zone_unbind(struct thermal_zone_device *thermal,
				struct thermal_cooling_device *cdevice) {
	/* Support only Thermal Throttling (1 trip) for now */
	return thermal_zone_unbind_cooling_device(thermal, 0, cdevice);
}

static int tegra_thermal_zone_get_temp(struct thermal_zone_device *thz,
						long *temp)
{
	struct tegra_thermal *thermal = thz->devdata;
	thermal->device->get_temp(thermal->device_client, temp);

	return 0;
}

static int tegra_thermal_zone_get_trip_type(
			struct thermal_zone_device *thermal,
			int trip,
			enum thermal_trip_type *type) {

	/* Support only Thermal Throttling (1 trip) for now */
	if (trip != 0)
		return -EINVAL;

	*type = THERMAL_TRIP_PASSIVE;

	return 0;
}

static int tegra_thermal_zone_get_trip_temp(struct thermal_zone_device *thermal,
						int trip,
						long *temp) {
	/* Support only Thermal Throttling (1 trip) for now */
	if (trip != 0)
		return -EINVAL;

	*temp = thermal->data.temp_throttle +
		thermal->data.temp_offset -
		thermal->device->offset;

	return 0;
}

static struct thermal_zone_device_ops tegra_thermal_zone_ops = {
	.bind = tegra_thermal_zone_bind,
	.unbind = tegra_thermal_zone_unbind,
	.get_temp = tegra_thermal_zone_get_temp,
	.get_trip_type = tegra_thermal_zone_get_trip_type,
	.get_trip_temp = tegra_thermal_zone_get_trip_temp,
};
#endif

/* The thermal sysfs handles notifying the throttling
 * cooling device */
#ifndef CONFIG_TEGRA_THERMAL_SYSFS
static void tegra_therm_throttle(bool enable)
{
	if (throttle_enb != enable) {
		mutex_lock(&mutex);
		tegra_throttling_enable(enable);
		throttle_enb = enable;
		mutex_unlock(&mutex);
	}
}
#endif

void tegra_thermal_alert(void *data)
{
	struct tegra_thermal *thermal = data;
	int err;
	long temp;
	long lo_limit_throttle, hi_limit_throttle;
	long lo_limit_edp = 0, hi_limit_edp = 0;
	long tj_temp, tj_throttle_temp, tj_shutdown_temp;
	int lo_limit_tj = 0, hi_limit_tj = 0;
	int lo_limit = 0, hi_limit = 0;
	const struct tegra_edp_limits *z;
	int zones_sz;
	int i;


	if (thermal != &thermal_state)
		BUG();

#ifdef CONFIG_TEGRA_THERMAL_SYSFS
	if (thermal->thz) {
		if (!thermal->thz->passive)
			thermal_zone_device_update(thermal->thz);
	}
#endif

	err = thermal->device->get_temp(thermal->device->data, &temp);
	if (err) {
		pr_err("%s: get temp fail(%d)", __func__, err);
		return;
	}

	tj_temp = temp + thermal->device->offset;
	tj_throttle_temp = thermal->data.temp_throttle
				+ thermal->data.temp_offset;
	tj_shutdown_temp = thermal->data.temp_shutdown
				+ thermal->data.temp_offset;

	if ((tegra_is_throttling() &&
		(tj_temp >
			(tj_throttle_temp - thermal->data.hysteresis_throttle)))
		|| (tj_temp >= tj_throttle_temp)) {
		lo_limit_throttle = tj_throttle_temp -
					thermal->data.hysteresis_throttle;
		hi_limit_throttle = tj_shutdown_temp;
	} else {
		lo_limit_throttle = 0;
		hi_limit_throttle = tj_throttle_temp;
	}

#ifdef CONFIG_TEGRA_EDP_LIMITS
	tegra_get_cpu_edp_limits(&z, &zones_sz);

/* edp table based off of tdiode measurements */
#define EDP_TEMP(_index)	((z[_index].temperature * 1000)\
				+ thermal->data.edp_offset)
	if (tj_temp < EDP_TEMP(0)) {
		lo_limit_edp = 0;
		hi_limit_edp = EDP_TEMP(0);
	} else if (tj_temp >= EDP_TEMP(zones_sz-1)) {
		lo_limit_edp = EDP_TEMP(zones_sz-1) -
				thermal->data.hysteresis_edp;
		hi_limit_edp = tj_shutdown_temp;
	} else {
		for (i = 0; (i + 1) < zones_sz; i++) {
			if ((tj_temp >= EDP_TEMP(i)) &&
				(tj_temp < EDP_TEMP(i+1))) {
				lo_limit_edp = EDP_TEMP(i) -
						thermal->data.hysteresis_edp;
				hi_limit_edp = EDP_TEMP(i+1);
				break;
			}
		}
	}
#undef EDP_TEMP
#else
	lo_limit_edp = 0;
	hi_limit_edp = tj_shutdown_temp;
#endif

	/* Get smallest window size */
	lo_limit_tj = max(lo_limit_throttle, lo_limit_edp);
	hi_limit_tj = min(hi_limit_throttle, hi_limit_edp);

	/* Get corresponding device temps */
	lo_limit = lo_limit_tj ? (lo_limit_tj - thermal->device->offset) : 0;
	hi_limit = hi_limit_tj ? (hi_limit_tj - thermal->device->offset) : 0;

	thermal->device->set_limits(thermal->device->data, lo_limit, hi_limit);

#ifndef CONFIG_TEGRA_THERMAL_SYSFS
	if (tj_temp >= tj_throttle_temp) {
		/* start throttling */
		if (!tegra_is_throttling())
			tegra_therm_throttle(true);
	} else if (tj_temp <=
			(tj_throttle_temp -
			thermal->data.hysteresis_throttle)) {
		/* switch off throttling */
		if (tegra_is_throttling())
			tegra_therm_throttle(false);
	}
#endif

#ifdef CONFIG_TEGRA_EDP_LIMITS
	/* inform edp governor */
	if (thermal->edp_thermal_zone_val != tj_temp)
		tegra_edp_update_thermal_zone(
			(tj_temp - thermal->data.edp_offset)/1000);

	thermal->edp_thermal_zone_val = tj_temp;
#endif
}

int tegra_thermal_set_device(struct tegra_thermal_device *device)
{
#ifdef CONFIG_THERMAL_SYSFS
	struct thermal_zone_device *thz;
#endif

	/* only support one device */
	if (thermal_state.device)
		return -EINVAL;

	thermal_state.device = device;

#ifdef CONFIG_TEGRA_THERMAL_SYSFS
	thz = thermal_zone_device_register("thermal",
					1, /* trips */
					&thermal_state,
					&tegra_thermal_zone_ops,
					2, /* tc1 */
					1, /* tc2 */
					2000, /* passive delay */
					0); /* polling delay */

	if (IS_ERR(thz)) {
		thz = NULL;
		kfree(thermal);
		return -ENODEV;
	}

	thermal_state.thz = thz;
#endif

	thermal_state.device->set_alert(thermal_state.device->data,
					tegra_thermal_alert,
					&thermal_state);
	thermal_state.device->set_shutdown_temp(thermal_state.device->data,
					thermal_state.data.temp_shutdown +
					thermal_state.data.temp_offset -
					thermal_state.device->offset);
	/* initialize limits */
	tegra_thermal_alert(&thermal_state);

	return 0;
}

int __init tegra_thermal_init(struct tegra_thermal_data *data)
{
#ifndef CONFIG_TEGRA_THERMAL_SYSFS
	mutex_init(&mutex);
#endif

	memcpy(&thermal_state.data, data, sizeof(struct tegra_thermal_data));

	return 0;
}

int tegra_thermal_exit(void)
{
#ifdef CONFIG_TEGRA_THERMAL_SYSFS
	if (thermal->thz)
		thermal_zone_device_unregister(thermal->thz);
#endif

	return 0;
}