blob: 75bca0d6daf0ed9a0e100839adc1256ff83b7e48 [file] [log] [blame]
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001/*
Amit Daniel Kachhapc48cbba2012-08-16 17:11:41 +05302 * exynos_thermal.c - Samsung EXYNOS TMU (Thermal Management Unit)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09003 *
4 * Copyright (C) 2011 Samsung Electronics
5 * Donggeun Kim <dg77.kim@samsung.com>
Amit Daniel Kachhapc48cbba2012-08-16 17:11:41 +05306 * Amit Daniel Kachhap <amit.kachhap@linaro.org>
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24#include <linux/module.h>
25#include <linux/err.h>
26#include <linux/kernel.h>
27#include <linux/slab.h>
28#include <linux/platform_device.h>
29#include <linux/interrupt.h>
30#include <linux/clk.h>
31#include <linux/workqueue.h>
32#include <linux/sysfs.h>
33#include <linux/kobject.h>
34#include <linux/io.h>
35#include <linux/mutex.h>
Amit Daniel Kachhapc48cbba2012-08-16 17:11:41 +053036#include <linux/platform_data/exynos_thermal.h>
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +053037#include <linux/thermal.h>
38#include <linux/cpufreq.h>
39#include <linux/cpu_cooling.h>
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +053040#include <linux/of.h>
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090041
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +053042#include <plat/cpu.h>
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090043
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +053044/* Exynos generic registers */
45#define EXYNOS_TMU_REG_TRIMINFO 0x0
46#define EXYNOS_TMU_REG_CONTROL 0x20
47#define EXYNOS_TMU_REG_STATUS 0x28
48#define EXYNOS_TMU_REG_CURRENT_TEMP 0x40
49#define EXYNOS_TMU_REG_INTEN 0x70
50#define EXYNOS_TMU_REG_INTSTAT 0x74
51#define EXYNOS_TMU_REG_INTCLEAR 0x78
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090052
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +053053#define EXYNOS_TMU_TRIM_TEMP_MASK 0xff
54#define EXYNOS_TMU_GAIN_SHIFT 8
55#define EXYNOS_TMU_REF_VOLTAGE_SHIFT 24
56#define EXYNOS_TMU_CORE_ON 3
57#define EXYNOS_TMU_CORE_OFF 2
58#define EXYNOS_TMU_DEF_CODE_TO_TEMP_OFFSET 50
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090059
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +053060/* Exynos4210 specific registers */
61#define EXYNOS4210_TMU_REG_THRESHOLD_TEMP 0x44
62#define EXYNOS4210_TMU_REG_TRIG_LEVEL0 0x50
63#define EXYNOS4210_TMU_REG_TRIG_LEVEL1 0x54
64#define EXYNOS4210_TMU_REG_TRIG_LEVEL2 0x58
65#define EXYNOS4210_TMU_REG_TRIG_LEVEL3 0x5C
66#define EXYNOS4210_TMU_REG_PAST_TEMP0 0x60
67#define EXYNOS4210_TMU_REG_PAST_TEMP1 0x64
68#define EXYNOS4210_TMU_REG_PAST_TEMP2 0x68
69#define EXYNOS4210_TMU_REG_PAST_TEMP3 0x6C
70
71#define EXYNOS4210_TMU_TRIG_LEVEL0_MASK 0x1
72#define EXYNOS4210_TMU_TRIG_LEVEL1_MASK 0x10
73#define EXYNOS4210_TMU_TRIG_LEVEL2_MASK 0x100
74#define EXYNOS4210_TMU_TRIG_LEVEL3_MASK 0x1000
75#define EXYNOS4210_TMU_INTCLEAR_VAL 0x1111
76
77/* Exynos5250 and Exynos4412 specific registers */
78#define EXYNOS_TMU_TRIMINFO_CON 0x14
79#define EXYNOS_THD_TEMP_RISE 0x50
80#define EXYNOS_THD_TEMP_FALL 0x54
81#define EXYNOS_EMUL_CON 0x80
82
83#define EXYNOS_TRIMINFO_RELOAD 0x1
84#define EXYNOS_TMU_CLEAR_RISE_INT 0x111
Amit Daniel Kachhap3ad95242013-01-17 01:42:18 +000085#define EXYNOS_TMU_CLEAR_FALL_INT (0x111 << 12)
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +053086#define EXYNOS_MUX_ADDR_VALUE 6
87#define EXYNOS_MUX_ADDR_SHIFT 20
88#define EXYNOS_TMU_TRIP_MODE_SHIFT 13
89
90#define EFUSE_MIN_VALUE 40
91#define EFUSE_MAX_VALUE 100
92
93/* In-kernel thermal framework related macros & definations */
94#define SENSOR_NAME_LEN 16
95#define MAX_TRIP_COUNT 8
96#define MAX_COOLING_DEVICE 4
Jonghwa Lee4f0a6842013-02-08 01:13:06 +000097#define MAX_THRESHOLD_LEVS 4
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +053098
99#define ACTIVE_INTERVAL 500
100#define IDLE_INTERVAL 10000
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530101#define MCELSIUS 1000
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530102
Amit Daniel Kachhapbffd1f82013-02-11 03:54:23 +0000103#ifdef CONFIG_THERMAL_EMULATION
Jonghwa Leebbf63be2012-11-21 13:31:01 +0900104#define EXYNOS_EMUL_TIME 0x57F0
105#define EXYNOS_EMUL_TIME_SHIFT 16
106#define EXYNOS_EMUL_DATA_SHIFT 8
107#define EXYNOS_EMUL_DATA_MASK 0xFF
108#define EXYNOS_EMUL_ENABLE 0x1
Amit Daniel Kachhapbffd1f82013-02-11 03:54:23 +0000109#endif /* CONFIG_THERMAL_EMULATION */
Jonghwa Leebbf63be2012-11-21 13:31:01 +0900110
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530111/* CPU Zone information */
112#define PANIC_ZONE 4
113#define WARN_ZONE 3
114#define MONITOR_ZONE 2
115#define SAFE_ZONE 1
116
117#define GET_ZONE(trip) (trip + 2)
118#define GET_TRIP(zone) (zone - 2)
119
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530120#define EXYNOS_ZONE_COUNT 3
121
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530122struct exynos_tmu_data {
123 struct exynos_tmu_platform_data *pdata;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900124 struct resource *mem;
125 void __iomem *base;
126 int irq;
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530127 enum soc_type soc;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900128 struct work_struct irq_work;
129 struct mutex lock;
130 struct clk *clk;
131 u8 temp_error1, temp_error2;
132};
133
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530134struct thermal_trip_point_conf {
135 int trip_val[MAX_TRIP_COUNT];
136 int trip_count;
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000137 u8 trigger_falling;
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530138};
139
140struct thermal_cooling_conf {
141 struct freq_clip_table freq_data[MAX_TRIP_COUNT];
142 int freq_clip_count;
143};
144
145struct thermal_sensor_conf {
146 char name[SENSOR_NAME_LEN];
147 int (*read_temperature)(void *data);
Amit Daniel Kachhapbffd1f82013-02-11 03:54:23 +0000148 int (*write_emul_temp)(void *drv_data, unsigned long temp);
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530149 struct thermal_trip_point_conf trip_data;
150 struct thermal_cooling_conf cooling_data;
151 void *private_data;
152};
153
154struct exynos_thermal_zone {
155 enum thermal_device_mode mode;
156 struct thermal_zone_device *therm_dev;
157 struct thermal_cooling_device *cool_dev[MAX_COOLING_DEVICE];
158 unsigned int cool_dev_size;
159 struct platform_device *exynos4_dev;
160 struct thermal_sensor_conf *sensor_conf;
161 bool bind;
162};
163
164static struct exynos_thermal_zone *th_zone;
165static void exynos_unregister_thermal(void);
166static int exynos_register_thermal(struct thermal_sensor_conf *sensor_conf);
167
168/* Get mode callback functions for thermal zone */
169static int exynos_get_mode(struct thermal_zone_device *thermal,
170 enum thermal_device_mode *mode)
171{
172 if (th_zone)
173 *mode = th_zone->mode;
174 return 0;
175}
176
177/* Set mode callback functions for thermal zone */
178static int exynos_set_mode(struct thermal_zone_device *thermal,
179 enum thermal_device_mode mode)
180{
181 if (!th_zone->therm_dev) {
182 pr_notice("thermal zone not registered\n");
183 return 0;
184 }
185
186 mutex_lock(&th_zone->therm_dev->lock);
187
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000188 if (mode == THERMAL_DEVICE_ENABLED &&
189 !th_zone->sensor_conf->trip_data.trigger_falling)
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530190 th_zone->therm_dev->polling_delay = IDLE_INTERVAL;
191 else
192 th_zone->therm_dev->polling_delay = 0;
193
194 mutex_unlock(&th_zone->therm_dev->lock);
195
196 th_zone->mode = mode;
197 thermal_zone_device_update(th_zone->therm_dev);
198 pr_info("thermal polling set for duration=%d msec\n",
199 th_zone->therm_dev->polling_delay);
200 return 0;
201}
202
203
204/* Get trip type callback functions for thermal zone */
205static int exynos_get_trip_type(struct thermal_zone_device *thermal, int trip,
206 enum thermal_trip_type *type)
207{
208 switch (GET_ZONE(trip)) {
209 case MONITOR_ZONE:
210 case WARN_ZONE:
211 *type = THERMAL_TRIP_ACTIVE;
212 break;
213 case PANIC_ZONE:
214 *type = THERMAL_TRIP_CRITICAL;
215 break;
216 default:
217 return -EINVAL;
218 }
219 return 0;
220}
221
222/* Get trip temperature callback functions for thermal zone */
223static int exynos_get_trip_temp(struct thermal_zone_device *thermal, int trip,
224 unsigned long *temp)
225{
226 if (trip < GET_TRIP(MONITOR_ZONE) || trip > GET_TRIP(PANIC_ZONE))
227 return -EINVAL;
228
229 *temp = th_zone->sensor_conf->trip_data.trip_val[trip];
230 /* convert the temperature into millicelsius */
231 *temp = *temp * MCELSIUS;
232
233 return 0;
234}
235
236/* Get critical temperature callback functions for thermal zone */
237static int exynos_get_crit_temp(struct thermal_zone_device *thermal,
238 unsigned long *temp)
239{
240 int ret;
241 /* Panic zone */
242 ret = exynos_get_trip_temp(thermal, GET_TRIP(PANIC_ZONE), temp);
243 return ret;
244}
245
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530246/* Bind callback functions for thermal zone */
247static int exynos_bind(struct thermal_zone_device *thermal,
248 struct thermal_cooling_device *cdev)
249{
250 int ret = 0, i, tab_size, level;
251 struct freq_clip_table *tab_ptr, *clip_data;
252 struct thermal_sensor_conf *data = th_zone->sensor_conf;
253
254 tab_ptr = (struct freq_clip_table *)data->cooling_data.freq_data;
255 tab_size = data->cooling_data.freq_clip_count;
256
257 if (tab_ptr == NULL || tab_size == 0)
258 return -EINVAL;
259
260 /* find the cooling device registered*/
261 for (i = 0; i < th_zone->cool_dev_size; i++)
262 if (cdev == th_zone->cool_dev[i])
263 break;
264
265 /* No matching cooling device */
266 if (i == th_zone->cool_dev_size)
267 return 0;
268
269 /* Bind the thermal zone to the cpufreq cooling device */
270 for (i = 0; i < tab_size; i++) {
271 clip_data = (struct freq_clip_table *)&(tab_ptr[i]);
Zhang Rui57df8102013-02-08 14:52:06 +0800272 level = cpufreq_cooling_get_level(0, clip_data->freq_clip_max);
273 if (level == THERMAL_CSTATE_INVALID)
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530274 return 0;
275 switch (GET_ZONE(i)) {
276 case MONITOR_ZONE:
277 case WARN_ZONE:
278 if (thermal_zone_bind_cooling_device(thermal, i, cdev,
Amit Daniel Kachhapce760ed2013-02-08 01:13:07 +0000279 level, 0)) {
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530280 pr_err("error binding cdev inst %d\n", i);
281 ret = -EINVAL;
282 }
283 th_zone->bind = true;
284 break;
285 default:
286 ret = -EINVAL;
287 }
288 }
289
290 return ret;
291}
292
293/* Unbind callback functions for thermal zone */
294static int exynos_unbind(struct thermal_zone_device *thermal,
295 struct thermal_cooling_device *cdev)
296{
297 int ret = 0, i, tab_size;
298 struct thermal_sensor_conf *data = th_zone->sensor_conf;
299
300 if (th_zone->bind == false)
301 return 0;
302
303 tab_size = data->cooling_data.freq_clip_count;
304
305 if (tab_size == 0)
306 return -EINVAL;
307
308 /* find the cooling device registered*/
309 for (i = 0; i < th_zone->cool_dev_size; i++)
310 if (cdev == th_zone->cool_dev[i])
311 break;
312
313 /* No matching cooling device */
314 if (i == th_zone->cool_dev_size)
315 return 0;
316
317 /* Bind the thermal zone to the cpufreq cooling device */
318 for (i = 0; i < tab_size; i++) {
319 switch (GET_ZONE(i)) {
320 case MONITOR_ZONE:
321 case WARN_ZONE:
322 if (thermal_zone_unbind_cooling_device(thermal, i,
323 cdev)) {
324 pr_err("error unbinding cdev inst=%d\n", i);
325 ret = -EINVAL;
326 }
327 th_zone->bind = false;
328 break;
329 default:
330 ret = -EINVAL;
331 }
332 }
333 return ret;
334}
335
336/* Get temperature callback functions for thermal zone */
337static int exynos_get_temp(struct thermal_zone_device *thermal,
338 unsigned long *temp)
339{
340 void *data;
341
342 if (!th_zone->sensor_conf) {
343 pr_info("Temperature sensor not initialised\n");
344 return -EINVAL;
345 }
346 data = th_zone->sensor_conf->private_data;
347 *temp = th_zone->sensor_conf->read_temperature(data);
348 /* convert the temperature into millicelsius */
349 *temp = *temp * MCELSIUS;
350 return 0;
351}
352
Amit Daniel Kachhapbffd1f82013-02-11 03:54:23 +0000353/* Get temperature callback functions for thermal zone */
354static int exynos_set_emul_temp(struct thermal_zone_device *thermal,
355 unsigned long temp)
356{
357 void *data;
358 int ret = -EINVAL;
359
360 if (!th_zone->sensor_conf) {
361 pr_info("Temperature sensor not initialised\n");
362 return -EINVAL;
363 }
364 data = th_zone->sensor_conf->private_data;
365 if (th_zone->sensor_conf->write_emul_temp)
366 ret = th_zone->sensor_conf->write_emul_temp(data, temp);
367 return ret;
368}
369
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530370/* Get the temperature trend */
371static int exynos_get_trend(struct thermal_zone_device *thermal,
372 int trip, enum thermal_trend *trend)
373{
Amit Daniel Kachhap3ad95242013-01-17 01:42:18 +0000374 int ret;
375 unsigned long trip_temp;
376
377 ret = exynos_get_trip_temp(thermal, trip, &trip_temp);
378 if (ret < 0)
379 return ret;
380
381 if (thermal->temperature >= trip_temp)
Amit Daniel Kachhapce760ed2013-02-08 01:13:07 +0000382 *trend = THERMAL_TREND_RAISE_FULL;
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530383 else
Amit Daniel Kachhapce760ed2013-02-08 01:13:07 +0000384 *trend = THERMAL_TREND_DROP_FULL;
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530385
386 return 0;
387}
388/* Operation callback functions for thermal zone */
389static struct thermal_zone_device_ops const exynos_dev_ops = {
390 .bind = exynos_bind,
391 .unbind = exynos_unbind,
392 .get_temp = exynos_get_temp,
Amit Daniel Kachhapbffd1f82013-02-11 03:54:23 +0000393 .set_emul_temp = exynos_set_emul_temp,
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530394 .get_trend = exynos_get_trend,
395 .get_mode = exynos_get_mode,
396 .set_mode = exynos_set_mode,
397 .get_trip_type = exynos_get_trip_type,
398 .get_trip_temp = exynos_get_trip_temp,
399 .get_crit_temp = exynos_get_crit_temp,
400};
401
402/*
403 * This function may be called from interrupt based temperature sensor
404 * when threshold is changed.
405 */
406static void exynos_report_trigger(void)
407{
408 unsigned int i;
409 char data[10];
410 char *envp[] = { data, NULL };
411
412 if (!th_zone || !th_zone->therm_dev)
413 return;
414 if (th_zone->bind == false) {
415 for (i = 0; i < th_zone->cool_dev_size; i++) {
416 if (!th_zone->cool_dev[i])
417 continue;
418 exynos_bind(th_zone->therm_dev,
419 th_zone->cool_dev[i]);
420 }
421 }
422
423 thermal_zone_device_update(th_zone->therm_dev);
424
425 mutex_lock(&th_zone->therm_dev->lock);
426 /* Find the level for which trip happened */
427 for (i = 0; i < th_zone->sensor_conf->trip_data.trip_count; i++) {
428 if (th_zone->therm_dev->last_temperature <
429 th_zone->sensor_conf->trip_data.trip_val[i] * MCELSIUS)
430 break;
431 }
432
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000433 if (th_zone->mode == THERMAL_DEVICE_ENABLED &&
434 !th_zone->sensor_conf->trip_data.trigger_falling) {
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530435 if (i > 0)
436 th_zone->therm_dev->polling_delay = ACTIVE_INTERVAL;
437 else
438 th_zone->therm_dev->polling_delay = IDLE_INTERVAL;
439 }
440
441 snprintf(data, sizeof(data), "%u", i);
442 kobject_uevent_env(&th_zone->therm_dev->device.kobj, KOBJ_CHANGE, envp);
443 mutex_unlock(&th_zone->therm_dev->lock);
444}
445
446/* Register with the in-kernel thermal management */
447static int exynos_register_thermal(struct thermal_sensor_conf *sensor_conf)
448{
449 int ret;
450 struct cpumask mask_val;
451
452 if (!sensor_conf || !sensor_conf->read_temperature) {
453 pr_err("Temperature sensor not initialised\n");
454 return -EINVAL;
455 }
456
457 th_zone = kzalloc(sizeof(struct exynos_thermal_zone), GFP_KERNEL);
458 if (!th_zone)
459 return -ENOMEM;
460
461 th_zone->sensor_conf = sensor_conf;
462 cpumask_set_cpu(0, &mask_val);
463 th_zone->cool_dev[0] = cpufreq_cooling_register(&mask_val);
464 if (IS_ERR(th_zone->cool_dev[0])) {
465 pr_err("Failed to register cpufreq cooling device\n");
466 ret = -EINVAL;
467 goto err_unregister;
468 }
469 th_zone->cool_dev_size++;
470
471 th_zone->therm_dev = thermal_zone_device_register(sensor_conf->name,
Durgadoss R50125a92012-09-18 11:04:56 +0530472 EXYNOS_ZONE_COUNT, 0, NULL, &exynos_dev_ops, NULL, 0,
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000473 sensor_conf->trip_data.trigger_falling ?
474 0 : IDLE_INTERVAL);
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530475
476 if (IS_ERR(th_zone->therm_dev)) {
477 pr_err("Failed to register thermal zone device\n");
Devendra Naga043e4652013-03-04 16:52:48 +0000478 ret = PTR_ERR(th_zone->therm_dev);
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530479 goto err_unregister;
480 }
481 th_zone->mode = THERMAL_DEVICE_ENABLED;
482
483 pr_info("Exynos: Kernel Thermal management registered\n");
484
485 return 0;
486
487err_unregister:
488 exynos_unregister_thermal();
489 return ret;
490}
491
492/* Un-Register with the in-kernel thermal management */
493static void exynos_unregister_thermal(void)
494{
495 int i;
496
Sachin Kamatc072fed2012-09-27 16:20:38 +0530497 if (!th_zone)
498 return;
499
500 if (th_zone->therm_dev)
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530501 thermal_zone_device_unregister(th_zone->therm_dev);
502
503 for (i = 0; i < th_zone->cool_dev_size; i++) {
Sachin Kamatc072fed2012-09-27 16:20:38 +0530504 if (th_zone->cool_dev[i])
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530505 cpufreq_cooling_unregister(th_zone->cool_dev[i]);
506 }
507
508 kfree(th_zone);
509 pr_info("Exynos: Kernel Thermal management unregistered\n");
510}
511
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900512/*
513 * TMU treats temperature as a mapped temperature code.
514 * The temperature is converted differently depending on the calibration type.
515 */
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530516static int temp_to_code(struct exynos_tmu_data *data, u8 temp)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900517{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530518 struct exynos_tmu_platform_data *pdata = data->pdata;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900519 int temp_code;
520
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530521 if (data->soc == SOC_ARCH_EXYNOS4210)
522 /* temp should range between 25 and 125 */
523 if (temp < 25 || temp > 125) {
524 temp_code = -EINVAL;
525 goto out;
526 }
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900527
528 switch (pdata->cal_type) {
529 case TYPE_TWO_POINT_TRIMMING:
530 temp_code = (temp - 25) *
531 (data->temp_error2 - data->temp_error1) /
532 (85 - 25) + data->temp_error1;
533 break;
534 case TYPE_ONE_POINT_TRIMMING:
535 temp_code = temp + data->temp_error1 - 25;
536 break;
537 default:
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530538 temp_code = temp + EXYNOS_TMU_DEF_CODE_TO_TEMP_OFFSET;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900539 break;
540 }
541out:
542 return temp_code;
543}
544
545/*
546 * Calculate a temperature value from a temperature code.
547 * The unit of the temperature is degree Celsius.
548 */
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530549static int code_to_temp(struct exynos_tmu_data *data, u8 temp_code)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900550{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530551 struct exynos_tmu_platform_data *pdata = data->pdata;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900552 int temp;
553
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530554 if (data->soc == SOC_ARCH_EXYNOS4210)
555 /* temp_code should range between 75 and 175 */
556 if (temp_code < 75 || temp_code > 175) {
557 temp = -ENODATA;
558 goto out;
559 }
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900560
561 switch (pdata->cal_type) {
562 case TYPE_TWO_POINT_TRIMMING:
563 temp = (temp_code - data->temp_error1) * (85 - 25) /
564 (data->temp_error2 - data->temp_error1) + 25;
565 break;
566 case TYPE_ONE_POINT_TRIMMING:
567 temp = temp_code - data->temp_error1 + 25;
568 break;
569 default:
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530570 temp = temp_code - EXYNOS_TMU_DEF_CODE_TO_TEMP_OFFSET;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900571 break;
572 }
573out:
574 return temp;
575}
576
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530577static int exynos_tmu_initialize(struct platform_device *pdev)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900578{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530579 struct exynos_tmu_data *data = platform_get_drvdata(pdev);
580 struct exynos_tmu_platform_data *pdata = data->pdata;
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000581 unsigned int status, trim_info;
582 unsigned int rising_threshold = 0, falling_threshold = 0;
583 int ret = 0, threshold_code, i, trigger_levs = 0;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900584
585 mutex_lock(&data->lock);
586 clk_enable(data->clk);
587
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530588 status = readb(data->base + EXYNOS_TMU_REG_STATUS);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900589 if (!status) {
590 ret = -EBUSY;
591 goto out;
592 }
593
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530594 if (data->soc == SOC_ARCH_EXYNOS) {
595 __raw_writel(EXYNOS_TRIMINFO_RELOAD,
596 data->base + EXYNOS_TMU_TRIMINFO_CON);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900597 }
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530598 /* Save trimming info in order to perform calibration */
599 trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO);
600 data->temp_error1 = trim_info & EXYNOS_TMU_TRIM_TEMP_MASK;
601 data->temp_error2 = ((trim_info >> 8) & EXYNOS_TMU_TRIM_TEMP_MASK);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900602
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530603 if ((EFUSE_MIN_VALUE > data->temp_error1) ||
604 (data->temp_error1 > EFUSE_MAX_VALUE) ||
605 (data->temp_error2 != 0))
606 data->temp_error1 = pdata->efuse_value;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900607
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000608 /* Count trigger levels to be enabled */
609 for (i = 0; i < MAX_THRESHOLD_LEVS; i++)
610 if (pdata->trigger_levels[i])
611 trigger_levs++;
612
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530613 if (data->soc == SOC_ARCH_EXYNOS4210) {
614 /* Write temperature code for threshold */
615 threshold_code = temp_to_code(data, pdata->threshold);
616 if (threshold_code < 0) {
617 ret = threshold_code;
618 goto out;
619 }
620 writeb(threshold_code,
621 data->base + EXYNOS4210_TMU_REG_THRESHOLD_TEMP);
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000622 for (i = 0; i < trigger_levs; i++)
623 writeb(pdata->trigger_levels[i],
624 data->base + EXYNOS4210_TMU_REG_TRIG_LEVEL0 + i * 4);
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530625
626 writel(EXYNOS4210_TMU_INTCLEAR_VAL,
627 data->base + EXYNOS_TMU_REG_INTCLEAR);
628 } else if (data->soc == SOC_ARCH_EXYNOS) {
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000629 /* Write temperature code for rising and falling threshold */
630 for (i = 0; i < trigger_levs; i++) {
631 threshold_code = temp_to_code(data,
632 pdata->trigger_levels[i]);
633 if (threshold_code < 0) {
634 ret = threshold_code;
635 goto out;
636 }
637 rising_threshold |= threshold_code << 8 * i;
638 if (pdata->threshold_falling) {
639 threshold_code = temp_to_code(data,
640 pdata->trigger_levels[i] -
641 pdata->threshold_falling);
642 if (threshold_code > 0)
643 falling_threshold |=
644 threshold_code << 8 * i;
645 }
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530646 }
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530647
648 writel(rising_threshold,
649 data->base + EXYNOS_THD_TEMP_RISE);
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000650 writel(falling_threshold,
651 data->base + EXYNOS_THD_TEMP_FALL);
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530652
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000653 writel(EXYNOS_TMU_CLEAR_RISE_INT | EXYNOS_TMU_CLEAR_FALL_INT,
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530654 data->base + EXYNOS_TMU_REG_INTCLEAR);
655 }
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900656out:
657 clk_disable(data->clk);
658 mutex_unlock(&data->lock);
659
660 return ret;
661}
662
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530663static void exynos_tmu_control(struct platform_device *pdev, bool on)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900664{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530665 struct exynos_tmu_data *data = platform_get_drvdata(pdev);
666 struct exynos_tmu_platform_data *pdata = data->pdata;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900667 unsigned int con, interrupt_en;
668
669 mutex_lock(&data->lock);
670 clk_enable(data->clk);
671
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530672 con = pdata->reference_voltage << EXYNOS_TMU_REF_VOLTAGE_SHIFT |
673 pdata->gain << EXYNOS_TMU_GAIN_SHIFT;
674
675 if (data->soc == SOC_ARCH_EXYNOS) {
676 con |= pdata->noise_cancel_mode << EXYNOS_TMU_TRIP_MODE_SHIFT;
677 con |= (EXYNOS_MUX_ADDR_VALUE << EXYNOS_MUX_ADDR_SHIFT);
678 }
679
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900680 if (on) {
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530681 con |= EXYNOS_TMU_CORE_ON;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900682 interrupt_en = pdata->trigger_level3_en << 12 |
683 pdata->trigger_level2_en << 8 |
684 pdata->trigger_level1_en << 4 |
685 pdata->trigger_level0_en;
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000686 if (pdata->threshold_falling)
687 interrupt_en |= interrupt_en << 16;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900688 } else {
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530689 con |= EXYNOS_TMU_CORE_OFF;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900690 interrupt_en = 0; /* Disable all interrupts */
691 }
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530692 writel(interrupt_en, data->base + EXYNOS_TMU_REG_INTEN);
693 writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900694
695 clk_disable(data->clk);
696 mutex_unlock(&data->lock);
697}
698
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530699static int exynos_tmu_read(struct exynos_tmu_data *data)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900700{
701 u8 temp_code;
702 int temp;
703
704 mutex_lock(&data->lock);
705 clk_enable(data->clk);
706
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530707 temp_code = readb(data->base + EXYNOS_TMU_REG_CURRENT_TEMP);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900708 temp = code_to_temp(data, temp_code);
709
710 clk_disable(data->clk);
711 mutex_unlock(&data->lock);
712
713 return temp;
714}
715
Amit Daniel Kachhapbffd1f82013-02-11 03:54:23 +0000716#ifdef CONFIG_THERMAL_EMULATION
717static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp)
718{
719 struct exynos_tmu_data *data = drv_data;
720 unsigned int reg;
721 int ret = -EINVAL;
722
723 if (data->soc == SOC_ARCH_EXYNOS4210)
724 goto out;
725
726 if (temp && temp < MCELSIUS)
727 goto out;
728
729 mutex_lock(&data->lock);
730 clk_enable(data->clk);
731
732 reg = readl(data->base + EXYNOS_EMUL_CON);
733
734 if (temp) {
735 temp /= MCELSIUS;
736
737 reg = (EXYNOS_EMUL_TIME << EXYNOS_EMUL_TIME_SHIFT) |
738 (temp_to_code(data, temp)
739 << EXYNOS_EMUL_DATA_SHIFT) | EXYNOS_EMUL_ENABLE;
740 } else {
741 reg &= ~EXYNOS_EMUL_ENABLE;
742 }
743
744 writel(reg, data->base + EXYNOS_EMUL_CON);
745
746 clk_disable(data->clk);
747 mutex_unlock(&data->lock);
748 return 0;
749out:
750 return ret;
751}
752#else
753static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp)
754 { return -EINVAL; }
755#endif/*CONFIG_THERMAL_EMULATION*/
756
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530757static void exynos_tmu_work(struct work_struct *work)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900758{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530759 struct exynos_tmu_data *data = container_of(work,
760 struct exynos_tmu_data, irq_work);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900761
Amit Daniel Kachhap3ad95242013-01-17 01:42:18 +0000762 exynos_report_trigger();
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900763 mutex_lock(&data->lock);
764 clk_enable(data->clk);
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530765 if (data->soc == SOC_ARCH_EXYNOS)
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000766 writel(EXYNOS_TMU_CLEAR_RISE_INT |
767 EXYNOS_TMU_CLEAR_FALL_INT,
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530768 data->base + EXYNOS_TMU_REG_INTCLEAR);
769 else
770 writel(EXYNOS4210_TMU_INTCLEAR_VAL,
771 data->base + EXYNOS_TMU_REG_INTCLEAR);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900772 clk_disable(data->clk);
773 mutex_unlock(&data->lock);
Amit Daniel Kachhap3ad95242013-01-17 01:42:18 +0000774
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530775 enable_irq(data->irq);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900776}
777
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530778static irqreturn_t exynos_tmu_irq(int irq, void *id)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900779{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530780 struct exynos_tmu_data *data = id;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900781
782 disable_irq_nosync(irq);
783 schedule_work(&data->irq_work);
784
785 return IRQ_HANDLED;
786}
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530787static struct thermal_sensor_conf exynos_sensor_conf = {
788 .name = "exynos-therm",
789 .read_temperature = (int (*)(void *))exynos_tmu_read,
Amit Daniel Kachhapbffd1f82013-02-11 03:54:23 +0000790 .write_emul_temp = exynos_tmu_set_emulation,
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530791};
792
793#if defined(CONFIG_CPU_EXYNOS4210)
794static struct exynos_tmu_platform_data const exynos4210_default_tmu_data = {
795 .threshold = 80,
796 .trigger_levels[0] = 5,
797 .trigger_levels[1] = 20,
798 .trigger_levels[2] = 30,
799 .trigger_level0_en = 1,
800 .trigger_level1_en = 1,
801 .trigger_level2_en = 1,
802 .trigger_level3_en = 0,
803 .gain = 15,
804 .reference_voltage = 7,
805 .cal_type = TYPE_ONE_POINT_TRIMMING,
806 .freq_tab[0] = {
807 .freq_clip_max = 800 * 1000,
808 .temp_level = 85,
809 },
810 .freq_tab[1] = {
811 .freq_clip_max = 200 * 1000,
812 .temp_level = 100,
813 },
814 .freq_tab_count = 2,
815 .type = SOC_ARCH_EXYNOS4210,
816};
817#define EXYNOS4210_TMU_DRV_DATA (&exynos4210_default_tmu_data)
818#else
819#define EXYNOS4210_TMU_DRV_DATA (NULL)
820#endif
821
822#if defined(CONFIG_SOC_EXYNOS5250) || defined(CONFIG_SOC_EXYNOS4412)
823static struct exynos_tmu_platform_data const exynos_default_tmu_data = {
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000824 .threshold_falling = 10,
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530825 .trigger_levels[0] = 85,
826 .trigger_levels[1] = 103,
827 .trigger_levels[2] = 110,
828 .trigger_level0_en = 1,
829 .trigger_level1_en = 1,
830 .trigger_level2_en = 1,
831 .trigger_level3_en = 0,
832 .gain = 8,
833 .reference_voltage = 16,
834 .noise_cancel_mode = 4,
835 .cal_type = TYPE_ONE_POINT_TRIMMING,
836 .efuse_value = 55,
837 .freq_tab[0] = {
838 .freq_clip_max = 800 * 1000,
839 .temp_level = 85,
840 },
841 .freq_tab[1] = {
842 .freq_clip_max = 200 * 1000,
843 .temp_level = 103,
844 },
845 .freq_tab_count = 2,
846 .type = SOC_ARCH_EXYNOS,
847};
848#define EXYNOS_TMU_DRV_DATA (&exynos_default_tmu_data)
849#else
850#define EXYNOS_TMU_DRV_DATA (NULL)
851#endif
852
853#ifdef CONFIG_OF
854static const struct of_device_id exynos_tmu_match[] = {
855 {
856 .compatible = "samsung,exynos4210-tmu",
857 .data = (void *)EXYNOS4210_TMU_DRV_DATA,
858 },
859 {
860 .compatible = "samsung,exynos5250-tmu",
861 .data = (void *)EXYNOS_TMU_DRV_DATA,
862 },
863 {},
864};
865MODULE_DEVICE_TABLE(of, exynos_tmu_match);
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530866#endif
867
868static struct platform_device_id exynos_tmu_driver_ids[] = {
869 {
870 .name = "exynos4210-tmu",
871 .driver_data = (kernel_ulong_t)EXYNOS4210_TMU_DRV_DATA,
872 },
873 {
874 .name = "exynos5250-tmu",
875 .driver_data = (kernel_ulong_t)EXYNOS_TMU_DRV_DATA,
876 },
877 { },
878};
Jonghwan Choi3ae53b12012-10-23 14:54:42 +0800879MODULE_DEVICE_TABLE(platform, exynos_tmu_driver_ids);
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530880
881static inline struct exynos_tmu_platform_data *exynos_get_driver_data(
882 struct platform_device *pdev)
883{
884#ifdef CONFIG_OF
885 if (pdev->dev.of_node) {
886 const struct of_device_id *match;
887 match = of_match_node(exynos_tmu_match, pdev->dev.of_node);
888 if (!match)
889 return NULL;
890 return (struct exynos_tmu_platform_data *) match->data;
891 }
892#endif
893 return (struct exynos_tmu_platform_data *)
894 platform_get_device_id(pdev)->driver_data;
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530895}
Jonghwa Leebbf63be2012-11-21 13:31:01 +0900896
Greg Kroah-Hartman4eab7a9e2012-12-21 13:15:52 -0800897static int exynos_tmu_probe(struct platform_device *pdev)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900898{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530899 struct exynos_tmu_data *data;
900 struct exynos_tmu_platform_data *pdata = pdev->dev.platform_data;
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530901 int ret, i;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900902
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530903 if (!pdata)
904 pdata = exynos_get_driver_data(pdev);
905
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900906 if (!pdata) {
907 dev_err(&pdev->dev, "No platform init data supplied.\n");
908 return -ENODEV;
909 }
Amit Daniel Kachhap79e093c2012-08-16 05:41:45 -0600910 data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data),
911 GFP_KERNEL);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900912 if (!data) {
913 dev_err(&pdev->dev, "Failed to allocate driver structure\n");
914 return -ENOMEM;
915 }
916
917 data->irq = platform_get_irq(pdev, 0);
918 if (data->irq < 0) {
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900919 dev_err(&pdev->dev, "Failed to get platform irq\n");
Amit Daniel Kachhap79e093c2012-08-16 05:41:45 -0600920 return data->irq;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900921 }
922
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530923 INIT_WORK(&data->irq_work, exynos_tmu_work);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900924
925 data->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
926 if (!data->mem) {
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900927 dev_err(&pdev->dev, "Failed to get platform resource\n");
Amit Daniel Kachhap79e093c2012-08-16 05:41:45 -0600928 return -ENOENT;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900929 }
930
Thierry Redingca36b1b2013-01-21 11:09:20 +0100931 data->base = devm_ioremap_resource(&pdev->dev, data->mem);
932 if (IS_ERR(data->base))
933 return PTR_ERR(data->base);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900934
Amit Daniel Kachhap79e093c2012-08-16 05:41:45 -0600935 ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq,
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530936 IRQF_TRIGGER_RISING, "exynos-tmu", data);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900937 if (ret) {
938 dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq);
Amit Daniel Kachhap79e093c2012-08-16 05:41:45 -0600939 return ret;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900940 }
941
942 data->clk = clk_get(NULL, "tmu_apbif");
943 if (IS_ERR(data->clk)) {
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900944 dev_err(&pdev->dev, "Failed to get clock\n");
Amit Daniel Kachhap79e093c2012-08-16 05:41:45 -0600945 return PTR_ERR(data->clk);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900946 }
947
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530948 if (pdata->type == SOC_ARCH_EXYNOS ||
949 pdata->type == SOC_ARCH_EXYNOS4210)
950 data->soc = pdata->type;
951 else {
952 ret = -EINVAL;
953 dev_err(&pdev->dev, "Platform not supported\n");
954 goto err_clk;
955 }
956
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900957 data->pdata = pdata;
958 platform_set_drvdata(pdev, data);
959 mutex_init(&data->lock);
960
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530961 ret = exynos_tmu_initialize(pdev);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900962 if (ret) {
963 dev_err(&pdev->dev, "Failed to initialize TMU\n");
964 goto err_clk;
965 }
966
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530967 exynos_tmu_control(pdev, true);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900968
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530969 /* Register the sensor with thermal management interface */
970 (&exynos_sensor_conf)->private_data = data;
971 exynos_sensor_conf.trip_data.trip_count = pdata->trigger_level0_en +
972 pdata->trigger_level1_en + pdata->trigger_level2_en +
973 pdata->trigger_level3_en;
974
975 for (i = 0; i < exynos_sensor_conf.trip_data.trip_count; i++)
976 exynos_sensor_conf.trip_data.trip_val[i] =
977 pdata->threshold + pdata->trigger_levels[i];
978
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000979 exynos_sensor_conf.trip_data.trigger_falling = pdata->threshold_falling;
980
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530981 exynos_sensor_conf.cooling_data.freq_clip_count =
982 pdata->freq_tab_count;
983 for (i = 0; i < pdata->freq_tab_count; i++) {
984 exynos_sensor_conf.cooling_data.freq_data[i].freq_clip_max =
985 pdata->freq_tab[i].freq_clip_max;
986 exynos_sensor_conf.cooling_data.freq_data[i].temp_level =
987 pdata->freq_tab[i].temp_level;
988 }
989
990 ret = exynos_register_thermal(&exynos_sensor_conf);
991 if (ret) {
992 dev_err(&pdev->dev, "Failed to register thermal interface\n");
993 goto err_clk;
994 }
Jonghwa Leebbf63be2012-11-21 13:31:01 +0900995
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900996 return 0;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900997err_clk:
998 platform_set_drvdata(pdev, NULL);
999 clk_put(data->clk);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001000 return ret;
1001}
1002
Greg Kroah-Hartman4eab7a9e2012-12-21 13:15:52 -08001003static int exynos_tmu_remove(struct platform_device *pdev)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001004{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301005 struct exynos_tmu_data *data = platform_get_drvdata(pdev);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001006
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301007 exynos_tmu_control(pdev, false);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001008
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +05301009 exynos_unregister_thermal();
1010
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001011 clk_put(data->clk);
1012
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001013 platform_set_drvdata(pdev, NULL);
1014
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001015 return 0;
1016}
1017
Rafael J. Wysocki08cd6752012-07-08 21:48:15 +02001018#ifdef CONFIG_PM_SLEEP
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301019static int exynos_tmu_suspend(struct device *dev)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001020{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301021 exynos_tmu_control(to_platform_device(dev), false);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001022
1023 return 0;
1024}
1025
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301026static int exynos_tmu_resume(struct device *dev)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001027{
Rafael J. Wysocki08cd6752012-07-08 21:48:15 +02001028 struct platform_device *pdev = to_platform_device(dev);
1029
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301030 exynos_tmu_initialize(pdev);
1031 exynos_tmu_control(pdev, true);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001032
1033 return 0;
1034}
Rafael J. Wysocki08cd6752012-07-08 21:48:15 +02001035
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301036static SIMPLE_DEV_PM_OPS(exynos_tmu_pm,
1037 exynos_tmu_suspend, exynos_tmu_resume);
1038#define EXYNOS_TMU_PM (&exynos_tmu_pm)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001039#else
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301040#define EXYNOS_TMU_PM NULL
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001041#endif
1042
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301043static struct platform_driver exynos_tmu_driver = {
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001044 .driver = {
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301045 .name = "exynos-tmu",
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001046 .owner = THIS_MODULE,
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301047 .pm = EXYNOS_TMU_PM,
Sachin Kamatcaa5cbd2012-12-12 15:54:24 +05301048 .of_match_table = of_match_ptr(exynos_tmu_match),
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001049 },
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301050 .probe = exynos_tmu_probe,
Greg Kroah-Hartman4eab7a9e2012-12-21 13:15:52 -08001051 .remove = exynos_tmu_remove,
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +05301052 .id_table = exynos_tmu_driver_ids,
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001053};
1054
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301055module_platform_driver(exynos_tmu_driver);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001056
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301057MODULE_DESCRIPTION("EXYNOS TMU Driver");
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001058MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
1059MODULE_LICENSE("GPL");
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301060MODULE_ALIAS("platform:exynos-tmu");