aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/staging/omap-thermal/omap-thermal-common.c
blob: 15e9723ba4d62eea856201b6ffd49a45316cff61 (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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
 * OMAP thermal driver interface
 *
 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
 * Contact:
 *   Eduardo Valentin <eduardo.valentin@ti.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.
 *
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include <linux/device.h>
#include <linux/err.h>
#include <linux/mutex.h>
#include <linux/gfp.h>
#include <linux/kernel.h>
#include <linux/workqueue.h>
#include <linux/thermal.h>
#include <linux/cpufreq.h>
#include <linux/cpumask.h>
#include <linux/cpu_cooling.h>

#include "omap-thermal.h"
#include "omap-bandgap.h"

/* common data structures */
struct omap_thermal_data {
	struct thermal_zone_device *omap_thermal;
	struct thermal_cooling_device *cool_dev;
	struct omap_bandgap *bg_ptr;
	enum thermal_device_mode mode;
	struct work_struct thermal_wq;
	int sensor_id;
};

static void omap_thermal_work(struct work_struct *work)
{
	struct omap_thermal_data *data = container_of(work,
					struct omap_thermal_data, thermal_wq);

	thermal_zone_device_update(data->omap_thermal);

	dev_dbg(&data->omap_thermal->device, "updated thermal zone %s\n",
		data->omap_thermal->type);
}

/**
 * omap_thermal_hotspot_temperature - returns sensor extrapolated temperature
 * @t:	omap sensor temperature
 * @s:	omap sensor slope value
 * @c:	omap sensor const value
 */
static inline int omap_thermal_hotspot_temperature(int t, int s, int c)
{
	int delta = t * s / 1000 + c;

	if (delta < 0)
		delta = 0;

	return t + delta;
}

/* thermal zone ops */
/* Get temperature callback function for thermal zone*/
static inline int omap_thermal_get_temp(struct thermal_zone_device *thermal,
					 unsigned long *temp)
{
	struct omap_thermal_data *data = thermal->devdata;
	struct omap_bandgap *bg_ptr;
	struct omap_temp_sensor *s;
	int ret, tmp, pcb_temp, slope, constant;

	if (!data)
		return 0;

	bg_ptr = data->bg_ptr;
	s = &bg_ptr->conf->sensors[data->sensor_id];

	ret = omap_bandgap_read_temperature(bg_ptr, data->sensor_id, &tmp);
	if (ret)
		return ret;

	pcb_temp = 0;
	/* TODO: Introduce pcb temperature lookup */
	/* In case pcb zone is available, use the extrapolation rule with it */
	if (pcb_temp) {
		tmp -= pcb_temp;
		slope = s->slope_pcb;
		constant = s->constant_pcb;
	} else {
		slope = s->slope;
		constant = s->constant;
	}
	*temp = omap_thermal_hotspot_temperature(tmp, slope, constant);

	return ret;
}

/* Bind callback functions for thermal zone */
static int omap_thermal_bind(struct thermal_zone_device *thermal,
			      struct thermal_cooling_device *cdev)
{
	struct omap_thermal_data *data = thermal->devdata;
	int id;

	if (IS_ERR_OR_NULL(data))
		return -ENODEV;

	/* check if this is the cooling device we registered */
	if (data->cool_dev != cdev)
		return 0;

	id = data->sensor_id;

	/* TODO: bind with min and max states */
	/* Simple thing, two trips, one passive another critical */
	return thermal_zone_bind_cooling_device(thermal, 0, cdev,
						THERMAL_NO_LIMIT,
						THERMAL_NO_LIMIT);
}

/* Unbind callback functions for thermal zone */
static int omap_thermal_unbind(struct thermal_zone_device *thermal,
				struct thermal_cooling_device *cdev)
{
	struct omap_thermal_data *data = thermal->devdata;

	if (IS_ERR_OR_NULL(data))
		return -ENODEV;

	/* check if this is the cooling device we registered */
	if (data->cool_dev != cdev)
		return 0;

	/* Simple thing, two trips, one passive another critical */
	return thermal_zone_unbind_cooling_device(thermal, 0, cdev);
}

/* Get mode callback functions for thermal zone */
static int omap_thermal_get_mode(struct thermal_zone_device *thermal,
				  enum thermal_device_mode *mode)
{
	struct omap_thermal_data *data = thermal->devdata;

	if (data)
		*mode = data->mode;

	return 0;
}

/* Set mode callback functions for thermal zone */
static int omap_thermal_set_mode(struct thermal_zone_device *thermal,
				  enum thermal_device_mode mode)
{
	struct omap_thermal_data *data = thermal->devdata;

	if (!data->omap_thermal) {
		dev_notice(&thermal->device, "thermal zone not registered\n");
		return 0;
	}

	mutex_lock(&data->omap_thermal->lock);

	if (mode == THERMAL_DEVICE_ENABLED)
		data->omap_thermal->polling_delay = FAST_TEMP_MONITORING_RATE;
	else
		data->omap_thermal->polling_delay = 0;

	mutex_unlock(&data->omap_thermal->lock);

	data->mode = mode;
	thermal_zone_device_update(data->omap_thermal);
	dev_dbg(&thermal->device, "thermal polling set for duration=%d msec\n",
		data->omap_thermal->polling_delay);

	return 0;
}

/* Get trip type callback functions for thermal zone */
static int omap_thermal_get_trip_type(struct thermal_zone_device *thermal,
				       int trip, enum thermal_trip_type *type)
{
	if (!omap_thermal_is_valid_trip(trip))
		return -EINVAL;

	if (trip + 1 == OMAP_TRIP_NUMBER)
		*type = THERMAL_TRIP_CRITICAL;
	else
		*type = THERMAL_TRIP_PASSIVE;

	return 0;
}

/* Get trip temperature callback functions for thermal zone */
static int omap_thermal_get_trip_temp(struct thermal_zone_device *thermal,
				       int trip, unsigned long *temp)
{
	if (!omap_thermal_is_valid_trip(trip))
		return -EINVAL;

	*temp = omap_thermal_get_trip_value(trip);

	return 0;
}

/* Get critical temperature callback functions for thermal zone */
static int omap_thermal_get_crit_temp(struct thermal_zone_device *thermal,
				       unsigned long *temp)
{
	/* shutdown zone */
	return omap_thermal_get_trip_temp(thermal, OMAP_TRIP_NUMBER - 1, temp);
}

static struct thermal_zone_device_ops omap_thermal_ops = {
	.get_temp = omap_thermal_get_temp,
	/* TODO: add .get_trend */
	.bind = omap_thermal_bind,
	.unbind = omap_thermal_unbind,
	.get_mode = omap_thermal_get_mode,
	.set_mode = omap_thermal_set_mode,
	.get_trip_type = omap_thermal_get_trip_type,
	.get_trip_temp = omap_thermal_get_trip_temp,
	.get_crit_temp = omap_thermal_get_crit_temp,
};

static struct omap_thermal_data
*omap_thermal_build_data(struct omap_bandgap *bg_ptr, int id)
{
	struct omap_thermal_data *data;

	data = devm_kzalloc(bg_ptr->dev, sizeof(*data), GFP_KERNEL);
	if (!data) {
		dev_err(bg_ptr->dev, "kzalloc fail\n");
		return NULL;
	}
	data->sensor_id = id;
	data->bg_ptr = bg_ptr;
	data->mode = THERMAL_DEVICE_ENABLED;
	INIT_WORK(&data->thermal_wq, omap_thermal_work);

	return data;
}

int omap_thermal_expose_sensor(struct omap_bandgap *bg_ptr, int id,
			       char *domain)
{
	struct omap_thermal_data *data;

	data = omap_bandgap_get_sensor_data(bg_ptr, id);

	if (!data)
		data = omap_thermal_build_data(bg_ptr, id);

	if (!data)
		return -EINVAL;

	/* TODO: remove TC1 TC2 */
	/* Create thermal zone */
	data->omap_thermal = thermal_zone_device_register(domain,
				OMAP_TRIP_NUMBER, 0, data, &omap_thermal_ops,
				FAST_TEMP_MONITORING_RATE,
				FAST_TEMP_MONITORING_RATE);
	if (IS_ERR_OR_NULL(data->omap_thermal)) {
		dev_err(bg_ptr->dev, "thermal zone device is NULL\n");
		return PTR_ERR(data->omap_thermal);
	}
	data->omap_thermal->polling_delay = FAST_TEMP_MONITORING_RATE;
	omap_bandgap_set_sensor_data(bg_ptr, id, data);

	return 0;
}

int omap_thermal_remove_sensor(struct omap_bandgap *bg_ptr, int id)
{
	struct omap_thermal_data *data;

	data = omap_bandgap_get_sensor_data(bg_ptr, id);

	thermal_zone_device_unregister(data->omap_thermal);

	return 0;
}

int omap_thermal_report_sensor_temperature(struct omap_bandgap *bg_ptr, int id)
{
	struct omap_thermal_data *data;

	data = omap_bandgap_get_sensor_data(bg_ptr, id);

	schedule_work(&data->thermal_wq);

	return 0;
}

int omap_thermal_register_cpu_cooling(struct omap_bandgap *bg_ptr, int id)
{
	struct omap_thermal_data *data;

	data = omap_bandgap_get_sensor_data(bg_ptr, id);
	if (!data)
		data = omap_thermal_build_data(bg_ptr, id);

	if (!data)
		return -EINVAL;

	/* Register cooling device */
	data->cool_dev = cpufreq_cooling_register(cpu_present_mask);
	if (IS_ERR_OR_NULL(data->cool_dev)) {
		dev_err(bg_ptr->dev,
			"Failed to register cpufreq cooling device\n");
		return PTR_ERR(data->cool_dev);
	}
	omap_bandgap_set_sensor_data(bg_ptr, id, data);

	return 0;
}

int omap_thermal_unregister_cpu_cooling(struct omap_bandgap *bg_ptr, int id)
{
	struct omap_thermal_data *data;

	data = omap_bandgap_get_sensor_data(bg_ptr, id);
	cpufreq_cooling_unregister(data->cool_dev);

	return 0;
}