aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/thermal/exynos_thermal.c
blob: 6e7e4d348298b93f10639eaf13c8c71be0fdaa3d (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
/* linux/drivers/thermal/exynos_thermal.c
 *
 * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
 *		http://www.samsung.com
 *
 * 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.
*/

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/thermal.h>
#include <linux/platform_device.h>
#include <linux/cpufreq.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/cpu_cooling.h>
#include <linux/platform_data/exynos4_tmu.h>
#include <linux/exynos_thermal.h>

struct exynos4_thermal_zone {
	unsigned int idle_interval;
	unsigned int active_interval;
	struct thermal_zone_device *therm_dev;
	struct thermal_cooling_device *cool_dev;
	struct platform_device *exynos4_dev;
	struct thermal_sensor_conf *sensor_conf;
	struct exynos4_tmu_platform_data *sensor_data;
};

static struct exynos4_thermal_zone *th_zone;

static int exynos4_get_mode(struct thermal_zone_device *thermal,
			    enum thermal_device_mode *mode)
{
	if (th_zone->sensor_conf) {
		pr_info("Temperature sensor not initialised\n");
		*mode = THERMAL_DEVICE_DISABLED;
	} else
		*mode = THERMAL_DEVICE_ENABLED;
	return 0;
}

static int exynos4_set_mode(struct thermal_zone_device *thermal,
			    enum thermal_device_mode mode)
{
	if (!th_zone->therm_dev) {
		pr_notice("thermal zone not registered\n");
		return 0;
	}
	if (mode == THERMAL_DEVICE_ENABLED)
		th_zone->therm_dev->polling_delay =
				th_zone->active_interval*1000;
	else
		th_zone->therm_dev->polling_delay =
				th_zone->idle_interval*1000;

	thermal_zone_device_update(th_zone->therm_dev);
	pr_info("thermal polling set for duration=%d sec\n",
				th_zone->therm_dev->polling_delay/1000);
	return 0;
}

/*This may be called from interrupt based temperature sensor*/
void exynos4_report_trigger(void)
{
	unsigned int th_temp = th_zone->sensor_data->threshold;
	unsigned int monitor_temp = th_temp +
			th_zone->sensor_data->trigger_levels[1];

	thermal_zone_device_update(th_zone->therm_dev);

	if (th_zone->therm_dev->last_temperature > monitor_temp)
		th_zone->therm_dev->polling_delay =
					th_zone->active_interval*1000;
	else
		th_zone->therm_dev->polling_delay =
					th_zone->idle_interval*1000;
}

static int exynos4_get_trip_type(struct thermal_zone_device *thermal, int trip,
				 enum thermal_trip_type *type)
{
	if (trip == 0 || trip == 1)
		*type = THERMAL_TRIP_STATE_ACTIVE;
	else if (trip == 2)
		*type = THERMAL_TRIP_HOT;
	else if (trip == 3)
		*type = THERMAL_TRIP_CRITICAL;
	else
		return -EINVAL;

	return 0;
}

static int exynos4_get_trip_temp(struct thermal_zone_device *thermal, int trip,
				 unsigned long *temp)
{
	unsigned int th_temp = th_zone->sensor_data->threshold;

	/*Monitor zone*/
	if (trip == 0)
		*temp = th_temp + th_zone->sensor_data->trigger_levels[0];
	/*Warn zone*/
	else if (trip == 1)
		*temp = th_temp + th_zone->sensor_data->trigger_levels[1];
	else if (trip == 2)
		*temp = th_temp + th_zone->sensor_data->trigger_levels[2];
	/*Panic zone*/
	else if (trip == 3)
		*temp = th_temp + th_zone->sensor_data->trigger_levels[3];
	else
		return -EINVAL;
	/*convert the temperature into millicelsius*/
	*temp = *temp * 1000;
	return 0;
}

static int exynos4_get_crit_temp(struct thermal_zone_device *thermal,
				 unsigned long *temp)
{
	unsigned int th_temp = th_zone->sensor_data->threshold;
	/*Panic zone*/
	*temp = th_temp + th_zone->sensor_data->trigger_levels[3];
	/*convert the temperature into millicelsius*/
	*temp = *temp * 1000;
	return 0;
}

static int exynos4_bind(struct thermal_zone_device *thermal,
			struct thermal_cooling_device *cdev)
{
	/* if the cooling device is the one from exynos4 bind it */
	if (cdev != th_zone->cool_dev)
		return 0;

	if (thermal_zone_bind_cooling_device(thermal, 0, cdev)) {
		pr_err("error binding cooling dev\n");
		return -EINVAL;
	}
	if (thermal_zone_bind_cooling_device(thermal, 1, cdev)) {
		pr_err("error binding cooling dev\n");
		return -EINVAL;
	}

	return 0;
}

static int exynos4_unbind(struct thermal_zone_device *thermal,
			  struct thermal_cooling_device *cdev)
{
	if (cdev != th_zone->cool_dev)
		return 0;
	if (thermal_zone_unbind_cooling_device(thermal, 0, cdev)) {
		pr_err("error unbinding cooling dev\n");
		return -EINVAL;
	}
	if (thermal_zone_unbind_cooling_device(thermal, 1, cdev)) {
		pr_err("error unbinding cooling dev\n");
		return -EINVAL;
	}
	return 0;
}

static int exynos4_get_temp(struct thermal_zone_device *thermal,
			       unsigned long *temp)
{
	void *data;

	if (!th_zone->sensor_conf) {
		pr_info("Temperature sensor not initialised\n");
		return -EINVAL;
	}
	data = th_zone->sensor_conf->private_data;
	*temp = th_zone->sensor_conf->read_temperature(data);
	/*convert the temperature into millicelsius*/
	*temp = *temp * 1000;
	return 0;
}

/* bind callback functions to thermalzone */
static struct thermal_zone_device_ops exynos4_dev_ops = {
	.bind = exynos4_bind,
	.unbind = exynos4_unbind,
	.get_temp = exynos4_get_temp,
	.get_mode = exynos4_get_mode,
	.set_mode = exynos4_set_mode,
	.get_trip_type = exynos4_get_trip_type,
	.get_trip_temp = exynos4_get_trip_temp,
	.get_crit_temp = exynos4_get_crit_temp,
};

int exynos4_register_thermal(struct thermal_sensor_conf *sensor_conf)
{
	int ret;

	if (!sensor_conf) {
		pr_err("Temperature sensor not initialised\n");
		return -EINVAL;
	}

	th_zone = kzalloc(sizeof(struct exynos4_thermal_zone), GFP_KERNEL);
	if (!th_zone) {
		ret = -ENOMEM;
		goto err_unregister;
	}

	th_zone->sensor_conf = sensor_conf;

	th_zone->sensor_data = sensor_conf->sensor_data;
	if (!th_zone->sensor_data) {
		pr_err("Temperature sensor data not initialised\n");
		ret = -EINVAL;
		goto err_unregister;
	}

	th_zone->cool_dev = cpufreq_cooling_register(
		(struct freq_pctg_table *)th_zone->sensor_data->freq_tab,
		th_zone->sensor_data->freq_tab_count, cpumask_of(0));

	if (IS_ERR(th_zone->cool_dev)) {
		pr_err("Failed to register cpufreq cooling device\n");
		ret = -EINVAL;
		goto err_unregister;
	}

	th_zone->therm_dev = thermal_zone_device_register(sensor_conf->name,
				4, NULL, &exynos4_dev_ops, 0, 0, 0, 1000);
	if (IS_ERR(th_zone->therm_dev)) {
		pr_err("Failed to register thermal zone device\n");
		ret = -EINVAL;
		goto err_unregister;
	}

	th_zone->active_interval = 5;
	th_zone->idle_interval = 10;

	exynos4_set_mode(th_zone->therm_dev, THERMAL_DEVICE_DISABLED);

	pr_info("Exynos: Kernel Thermal management registered\n");

	return 0;

err_unregister:
	exynos4_unregister_thermal();
	return ret;
}
EXPORT_SYMBOL(exynos4_register_thermal);

void exynos4_unregister_thermal(void)
{
	if (th_zone && th_zone->cool_dev)
		cpufreq_cooling_unregister();

	if (th_zone && th_zone->therm_dev)
		thermal_zone_device_unregister(th_zone->therm_dev);

	kfree(th_zone);

	pr_info("Exynos: Kernel Thermal management unregistered\n");
}
EXPORT_SYMBOL(exynos4_unregister_thermal);