blob: 541257888c3e071be3ee59b80e587c8d79d7b794 [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
Jonghwa Leebbf63be2012-11-21 13:31:01 +0900103#ifdef CONFIG_EXYNOS_THERMAL_EMUL
104#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
109#endif /* CONFIG_EXYNOS_THERMAL_EMUL */
110
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);
148 struct thermal_trip_point_conf trip_data;
149 struct thermal_cooling_conf cooling_data;
150 void *private_data;
151};
152
153struct exynos_thermal_zone {
154 enum thermal_device_mode mode;
155 struct thermal_zone_device *therm_dev;
156 struct thermal_cooling_device *cool_dev[MAX_COOLING_DEVICE];
157 unsigned int cool_dev_size;
158 struct platform_device *exynos4_dev;
159 struct thermal_sensor_conf *sensor_conf;
160 bool bind;
161};
162
163static struct exynos_thermal_zone *th_zone;
164static void exynos_unregister_thermal(void);
165static int exynos_register_thermal(struct thermal_sensor_conf *sensor_conf);
166
167/* Get mode callback functions for thermal zone */
168static int exynos_get_mode(struct thermal_zone_device *thermal,
169 enum thermal_device_mode *mode)
170{
171 if (th_zone)
172 *mode = th_zone->mode;
173 return 0;
174}
175
176/* Set mode callback functions for thermal zone */
177static int exynos_set_mode(struct thermal_zone_device *thermal,
178 enum thermal_device_mode mode)
179{
180 if (!th_zone->therm_dev) {
181 pr_notice("thermal zone not registered\n");
182 return 0;
183 }
184
185 mutex_lock(&th_zone->therm_dev->lock);
186
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000187 if (mode == THERMAL_DEVICE_ENABLED &&
188 !th_zone->sensor_conf->trip_data.trigger_falling)
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530189 th_zone->therm_dev->polling_delay = IDLE_INTERVAL;
190 else
191 th_zone->therm_dev->polling_delay = 0;
192
193 mutex_unlock(&th_zone->therm_dev->lock);
194
195 th_zone->mode = mode;
196 thermal_zone_device_update(th_zone->therm_dev);
197 pr_info("thermal polling set for duration=%d msec\n",
198 th_zone->therm_dev->polling_delay);
199 return 0;
200}
201
202
203/* Get trip type callback functions for thermal zone */
204static int exynos_get_trip_type(struct thermal_zone_device *thermal, int trip,
205 enum thermal_trip_type *type)
206{
207 switch (GET_ZONE(trip)) {
208 case MONITOR_ZONE:
209 case WARN_ZONE:
210 *type = THERMAL_TRIP_ACTIVE;
211 break;
212 case PANIC_ZONE:
213 *type = THERMAL_TRIP_CRITICAL;
214 break;
215 default:
216 return -EINVAL;
217 }
218 return 0;
219}
220
221/* Get trip temperature callback functions for thermal zone */
222static int exynos_get_trip_temp(struct thermal_zone_device *thermal, int trip,
223 unsigned long *temp)
224{
225 if (trip < GET_TRIP(MONITOR_ZONE) || trip > GET_TRIP(PANIC_ZONE))
226 return -EINVAL;
227
228 *temp = th_zone->sensor_conf->trip_data.trip_val[trip];
229 /* convert the temperature into millicelsius */
230 *temp = *temp * MCELSIUS;
231
232 return 0;
233}
234
235/* Get critical temperature callback functions for thermal zone */
236static int exynos_get_crit_temp(struct thermal_zone_device *thermal,
237 unsigned long *temp)
238{
239 int ret;
240 /* Panic zone */
241 ret = exynos_get_trip_temp(thermal, GET_TRIP(PANIC_ZONE), temp);
242 return ret;
243}
244
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530245/* Bind callback functions for thermal zone */
246static int exynos_bind(struct thermal_zone_device *thermal,
247 struct thermal_cooling_device *cdev)
248{
249 int ret = 0, i, tab_size, level;
250 struct freq_clip_table *tab_ptr, *clip_data;
251 struct thermal_sensor_conf *data = th_zone->sensor_conf;
252
253 tab_ptr = (struct freq_clip_table *)data->cooling_data.freq_data;
254 tab_size = data->cooling_data.freq_clip_count;
255
256 if (tab_ptr == NULL || tab_size == 0)
257 return -EINVAL;
258
259 /* find the cooling device registered*/
260 for (i = 0; i < th_zone->cool_dev_size; i++)
261 if (cdev == th_zone->cool_dev[i])
262 break;
263
264 /* No matching cooling device */
265 if (i == th_zone->cool_dev_size)
266 return 0;
267
268 /* Bind the thermal zone to the cpufreq cooling device */
269 for (i = 0; i < tab_size; i++) {
270 clip_data = (struct freq_clip_table *)&(tab_ptr[i]);
Zhang Rui57df8102013-02-08 14:52:06 +0800271 level = cpufreq_cooling_get_level(0, clip_data->freq_clip_max);
272 if (level == THERMAL_CSTATE_INVALID)
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530273 return 0;
274 switch (GET_ZONE(i)) {
275 case MONITOR_ZONE:
276 case WARN_ZONE:
277 if (thermal_zone_bind_cooling_device(thermal, i, cdev,
Amit Daniel Kachhapce760ed2013-02-08 01:13:07 +0000278 level, 0)) {
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530279 pr_err("error binding cdev inst %d\n", i);
280 ret = -EINVAL;
281 }
282 th_zone->bind = true;
283 break;
284 default:
285 ret = -EINVAL;
286 }
287 }
288
289 return ret;
290}
291
292/* Unbind callback functions for thermal zone */
293static int exynos_unbind(struct thermal_zone_device *thermal,
294 struct thermal_cooling_device *cdev)
295{
296 int ret = 0, i, tab_size;
297 struct thermal_sensor_conf *data = th_zone->sensor_conf;
298
299 if (th_zone->bind == false)
300 return 0;
301
302 tab_size = data->cooling_data.freq_clip_count;
303
304 if (tab_size == 0)
305 return -EINVAL;
306
307 /* find the cooling device registered*/
308 for (i = 0; i < th_zone->cool_dev_size; i++)
309 if (cdev == th_zone->cool_dev[i])
310 break;
311
312 /* No matching cooling device */
313 if (i == th_zone->cool_dev_size)
314 return 0;
315
316 /* Bind the thermal zone to the cpufreq cooling device */
317 for (i = 0; i < tab_size; i++) {
318 switch (GET_ZONE(i)) {
319 case MONITOR_ZONE:
320 case WARN_ZONE:
321 if (thermal_zone_unbind_cooling_device(thermal, i,
322 cdev)) {
323 pr_err("error unbinding cdev inst=%d\n", i);
324 ret = -EINVAL;
325 }
326 th_zone->bind = false;
327 break;
328 default:
329 ret = -EINVAL;
330 }
331 }
332 return ret;
333}
334
335/* Get temperature callback functions for thermal zone */
336static int exynos_get_temp(struct thermal_zone_device *thermal,
337 unsigned long *temp)
338{
339 void *data;
340
341 if (!th_zone->sensor_conf) {
342 pr_info("Temperature sensor not initialised\n");
343 return -EINVAL;
344 }
345 data = th_zone->sensor_conf->private_data;
346 *temp = th_zone->sensor_conf->read_temperature(data);
347 /* convert the temperature into millicelsius */
348 *temp = *temp * MCELSIUS;
349 return 0;
350}
351
352/* Get the temperature trend */
353static int exynos_get_trend(struct thermal_zone_device *thermal,
354 int trip, enum thermal_trend *trend)
355{
Amit Daniel Kachhap3ad95242013-01-17 01:42:18 +0000356 int ret;
357 unsigned long trip_temp;
358
359 ret = exynos_get_trip_temp(thermal, trip, &trip_temp);
360 if (ret < 0)
361 return ret;
362
363 if (thermal->temperature >= trip_temp)
Amit Daniel Kachhapce760ed2013-02-08 01:13:07 +0000364 *trend = THERMAL_TREND_RAISE_FULL;
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530365 else
Amit Daniel Kachhapce760ed2013-02-08 01:13:07 +0000366 *trend = THERMAL_TREND_DROP_FULL;
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530367
368 return 0;
369}
370/* Operation callback functions for thermal zone */
371static struct thermal_zone_device_ops const exynos_dev_ops = {
372 .bind = exynos_bind,
373 .unbind = exynos_unbind,
374 .get_temp = exynos_get_temp,
375 .get_trend = exynos_get_trend,
376 .get_mode = exynos_get_mode,
377 .set_mode = exynos_set_mode,
378 .get_trip_type = exynos_get_trip_type,
379 .get_trip_temp = exynos_get_trip_temp,
380 .get_crit_temp = exynos_get_crit_temp,
381};
382
383/*
384 * This function may be called from interrupt based temperature sensor
385 * when threshold is changed.
386 */
387static void exynos_report_trigger(void)
388{
389 unsigned int i;
390 char data[10];
391 char *envp[] = { data, NULL };
392
393 if (!th_zone || !th_zone->therm_dev)
394 return;
395 if (th_zone->bind == false) {
396 for (i = 0; i < th_zone->cool_dev_size; i++) {
397 if (!th_zone->cool_dev[i])
398 continue;
399 exynos_bind(th_zone->therm_dev,
400 th_zone->cool_dev[i]);
401 }
402 }
403
404 thermal_zone_device_update(th_zone->therm_dev);
405
406 mutex_lock(&th_zone->therm_dev->lock);
407 /* Find the level for which trip happened */
408 for (i = 0; i < th_zone->sensor_conf->trip_data.trip_count; i++) {
409 if (th_zone->therm_dev->last_temperature <
410 th_zone->sensor_conf->trip_data.trip_val[i] * MCELSIUS)
411 break;
412 }
413
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000414 if (th_zone->mode == THERMAL_DEVICE_ENABLED &&
415 !th_zone->sensor_conf->trip_data.trigger_falling) {
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530416 if (i > 0)
417 th_zone->therm_dev->polling_delay = ACTIVE_INTERVAL;
418 else
419 th_zone->therm_dev->polling_delay = IDLE_INTERVAL;
420 }
421
422 snprintf(data, sizeof(data), "%u", i);
423 kobject_uevent_env(&th_zone->therm_dev->device.kobj, KOBJ_CHANGE, envp);
424 mutex_unlock(&th_zone->therm_dev->lock);
425}
426
427/* Register with the in-kernel thermal management */
428static int exynos_register_thermal(struct thermal_sensor_conf *sensor_conf)
429{
430 int ret;
431 struct cpumask mask_val;
432
433 if (!sensor_conf || !sensor_conf->read_temperature) {
434 pr_err("Temperature sensor not initialised\n");
435 return -EINVAL;
436 }
437
438 th_zone = kzalloc(sizeof(struct exynos_thermal_zone), GFP_KERNEL);
439 if (!th_zone)
440 return -ENOMEM;
441
442 th_zone->sensor_conf = sensor_conf;
443 cpumask_set_cpu(0, &mask_val);
444 th_zone->cool_dev[0] = cpufreq_cooling_register(&mask_val);
445 if (IS_ERR(th_zone->cool_dev[0])) {
446 pr_err("Failed to register cpufreq cooling device\n");
447 ret = -EINVAL;
448 goto err_unregister;
449 }
450 th_zone->cool_dev_size++;
451
452 th_zone->therm_dev = thermal_zone_device_register(sensor_conf->name,
Durgadoss R50125a92012-09-18 11:04:56 +0530453 EXYNOS_ZONE_COUNT, 0, NULL, &exynos_dev_ops, NULL, 0,
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000454 sensor_conf->trip_data.trigger_falling ?
455 0 : IDLE_INTERVAL);
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530456
457 if (IS_ERR(th_zone->therm_dev)) {
458 pr_err("Failed to register thermal zone device\n");
Devendra Naga043e4652013-03-04 16:52:48 +0000459 ret = PTR_ERR(th_zone->therm_dev);
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530460 goto err_unregister;
461 }
462 th_zone->mode = THERMAL_DEVICE_ENABLED;
463
464 pr_info("Exynos: Kernel Thermal management registered\n");
465
466 return 0;
467
468err_unregister:
469 exynos_unregister_thermal();
470 return ret;
471}
472
473/* Un-Register with the in-kernel thermal management */
474static void exynos_unregister_thermal(void)
475{
476 int i;
477
Sachin Kamatc072fed2012-09-27 16:20:38 +0530478 if (!th_zone)
479 return;
480
481 if (th_zone->therm_dev)
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530482 thermal_zone_device_unregister(th_zone->therm_dev);
483
484 for (i = 0; i < th_zone->cool_dev_size; i++) {
Sachin Kamatc072fed2012-09-27 16:20:38 +0530485 if (th_zone->cool_dev[i])
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530486 cpufreq_cooling_unregister(th_zone->cool_dev[i]);
487 }
488
489 kfree(th_zone);
490 pr_info("Exynos: Kernel Thermal management unregistered\n");
491}
492
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900493/*
494 * TMU treats temperature as a mapped temperature code.
495 * The temperature is converted differently depending on the calibration type.
496 */
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530497static int temp_to_code(struct exynos_tmu_data *data, u8 temp)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900498{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530499 struct exynos_tmu_platform_data *pdata = data->pdata;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900500 int temp_code;
501
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530502 if (data->soc == SOC_ARCH_EXYNOS4210)
503 /* temp should range between 25 and 125 */
504 if (temp < 25 || temp > 125) {
505 temp_code = -EINVAL;
506 goto out;
507 }
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900508
509 switch (pdata->cal_type) {
510 case TYPE_TWO_POINT_TRIMMING:
511 temp_code = (temp - 25) *
512 (data->temp_error2 - data->temp_error1) /
513 (85 - 25) + data->temp_error1;
514 break;
515 case TYPE_ONE_POINT_TRIMMING:
516 temp_code = temp + data->temp_error1 - 25;
517 break;
518 default:
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530519 temp_code = temp + EXYNOS_TMU_DEF_CODE_TO_TEMP_OFFSET;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900520 break;
521 }
522out:
523 return temp_code;
524}
525
526/*
527 * Calculate a temperature value from a temperature code.
528 * The unit of the temperature is degree Celsius.
529 */
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530530static int code_to_temp(struct exynos_tmu_data *data, u8 temp_code)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900531{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530532 struct exynos_tmu_platform_data *pdata = data->pdata;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900533 int temp;
534
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530535 if (data->soc == SOC_ARCH_EXYNOS4210)
536 /* temp_code should range between 75 and 175 */
537 if (temp_code < 75 || temp_code > 175) {
538 temp = -ENODATA;
539 goto out;
540 }
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900541
542 switch (pdata->cal_type) {
543 case TYPE_TWO_POINT_TRIMMING:
544 temp = (temp_code - data->temp_error1) * (85 - 25) /
545 (data->temp_error2 - data->temp_error1) + 25;
546 break;
547 case TYPE_ONE_POINT_TRIMMING:
548 temp = temp_code - data->temp_error1 + 25;
549 break;
550 default:
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530551 temp = temp_code - EXYNOS_TMU_DEF_CODE_TO_TEMP_OFFSET;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900552 break;
553 }
554out:
555 return temp;
556}
557
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530558static int exynos_tmu_initialize(struct platform_device *pdev)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900559{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530560 struct exynos_tmu_data *data = platform_get_drvdata(pdev);
561 struct exynos_tmu_platform_data *pdata = data->pdata;
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000562 unsigned int status, trim_info;
563 unsigned int rising_threshold = 0, falling_threshold = 0;
564 int ret = 0, threshold_code, i, trigger_levs = 0;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900565
566 mutex_lock(&data->lock);
567 clk_enable(data->clk);
568
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530569 status = readb(data->base + EXYNOS_TMU_REG_STATUS);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900570 if (!status) {
571 ret = -EBUSY;
572 goto out;
573 }
574
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530575 if (data->soc == SOC_ARCH_EXYNOS) {
576 __raw_writel(EXYNOS_TRIMINFO_RELOAD,
577 data->base + EXYNOS_TMU_TRIMINFO_CON);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900578 }
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530579 /* Save trimming info in order to perform calibration */
580 trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO);
581 data->temp_error1 = trim_info & EXYNOS_TMU_TRIM_TEMP_MASK;
582 data->temp_error2 = ((trim_info >> 8) & EXYNOS_TMU_TRIM_TEMP_MASK);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900583
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530584 if ((EFUSE_MIN_VALUE > data->temp_error1) ||
585 (data->temp_error1 > EFUSE_MAX_VALUE) ||
586 (data->temp_error2 != 0))
587 data->temp_error1 = pdata->efuse_value;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900588
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000589 /* Count trigger levels to be enabled */
590 for (i = 0; i < MAX_THRESHOLD_LEVS; i++)
591 if (pdata->trigger_levels[i])
592 trigger_levs++;
593
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530594 if (data->soc == SOC_ARCH_EXYNOS4210) {
595 /* Write temperature code for threshold */
596 threshold_code = temp_to_code(data, pdata->threshold);
597 if (threshold_code < 0) {
598 ret = threshold_code;
599 goto out;
600 }
601 writeb(threshold_code,
602 data->base + EXYNOS4210_TMU_REG_THRESHOLD_TEMP);
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000603 for (i = 0; i < trigger_levs; i++)
604 writeb(pdata->trigger_levels[i],
605 data->base + EXYNOS4210_TMU_REG_TRIG_LEVEL0 + i * 4);
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530606
607 writel(EXYNOS4210_TMU_INTCLEAR_VAL,
608 data->base + EXYNOS_TMU_REG_INTCLEAR);
609 } else if (data->soc == SOC_ARCH_EXYNOS) {
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000610 /* Write temperature code for rising and falling threshold */
611 for (i = 0; i < trigger_levs; i++) {
612 threshold_code = temp_to_code(data,
613 pdata->trigger_levels[i]);
614 if (threshold_code < 0) {
615 ret = threshold_code;
616 goto out;
617 }
618 rising_threshold |= threshold_code << 8 * i;
619 if (pdata->threshold_falling) {
620 threshold_code = temp_to_code(data,
621 pdata->trigger_levels[i] -
622 pdata->threshold_falling);
623 if (threshold_code > 0)
624 falling_threshold |=
625 threshold_code << 8 * i;
626 }
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530627 }
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530628
629 writel(rising_threshold,
630 data->base + EXYNOS_THD_TEMP_RISE);
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000631 writel(falling_threshold,
632 data->base + EXYNOS_THD_TEMP_FALL);
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530633
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000634 writel(EXYNOS_TMU_CLEAR_RISE_INT | EXYNOS_TMU_CLEAR_FALL_INT,
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530635 data->base + EXYNOS_TMU_REG_INTCLEAR);
636 }
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900637out:
638 clk_disable(data->clk);
639 mutex_unlock(&data->lock);
640
641 return ret;
642}
643
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530644static void exynos_tmu_control(struct platform_device *pdev, bool on)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900645{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530646 struct exynos_tmu_data *data = platform_get_drvdata(pdev);
647 struct exynos_tmu_platform_data *pdata = data->pdata;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900648 unsigned int con, interrupt_en;
649
650 mutex_lock(&data->lock);
651 clk_enable(data->clk);
652
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530653 con = pdata->reference_voltage << EXYNOS_TMU_REF_VOLTAGE_SHIFT |
654 pdata->gain << EXYNOS_TMU_GAIN_SHIFT;
655
656 if (data->soc == SOC_ARCH_EXYNOS) {
657 con |= pdata->noise_cancel_mode << EXYNOS_TMU_TRIP_MODE_SHIFT;
658 con |= (EXYNOS_MUX_ADDR_VALUE << EXYNOS_MUX_ADDR_SHIFT);
659 }
660
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900661 if (on) {
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530662 con |= EXYNOS_TMU_CORE_ON;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900663 interrupt_en = pdata->trigger_level3_en << 12 |
664 pdata->trigger_level2_en << 8 |
665 pdata->trigger_level1_en << 4 |
666 pdata->trigger_level0_en;
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000667 if (pdata->threshold_falling)
668 interrupt_en |= interrupt_en << 16;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900669 } else {
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530670 con |= EXYNOS_TMU_CORE_OFF;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900671 interrupt_en = 0; /* Disable all interrupts */
672 }
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530673 writel(interrupt_en, data->base + EXYNOS_TMU_REG_INTEN);
674 writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900675
676 clk_disable(data->clk);
677 mutex_unlock(&data->lock);
678}
679
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530680static int exynos_tmu_read(struct exynos_tmu_data *data)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900681{
682 u8 temp_code;
683 int temp;
684
685 mutex_lock(&data->lock);
686 clk_enable(data->clk);
687
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530688 temp_code = readb(data->base + EXYNOS_TMU_REG_CURRENT_TEMP);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900689 temp = code_to_temp(data, temp_code);
690
691 clk_disable(data->clk);
692 mutex_unlock(&data->lock);
693
694 return temp;
695}
696
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530697static void exynos_tmu_work(struct work_struct *work)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900698{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530699 struct exynos_tmu_data *data = container_of(work,
700 struct exynos_tmu_data, irq_work);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900701
Amit Daniel Kachhap3ad95242013-01-17 01:42:18 +0000702 exynos_report_trigger();
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900703 mutex_lock(&data->lock);
704 clk_enable(data->clk);
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530705 if (data->soc == SOC_ARCH_EXYNOS)
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000706 writel(EXYNOS_TMU_CLEAR_RISE_INT |
707 EXYNOS_TMU_CLEAR_FALL_INT,
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530708 data->base + EXYNOS_TMU_REG_INTCLEAR);
709 else
710 writel(EXYNOS4210_TMU_INTCLEAR_VAL,
711 data->base + EXYNOS_TMU_REG_INTCLEAR);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900712 clk_disable(data->clk);
713 mutex_unlock(&data->lock);
Amit Daniel Kachhap3ad95242013-01-17 01:42:18 +0000714
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530715 enable_irq(data->irq);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900716}
717
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530718static irqreturn_t exynos_tmu_irq(int irq, void *id)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900719{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530720 struct exynos_tmu_data *data = id;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900721
722 disable_irq_nosync(irq);
723 schedule_work(&data->irq_work);
724
725 return IRQ_HANDLED;
726}
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530727static struct thermal_sensor_conf exynos_sensor_conf = {
728 .name = "exynos-therm",
729 .read_temperature = (int (*)(void *))exynos_tmu_read,
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530730};
731
732#if defined(CONFIG_CPU_EXYNOS4210)
733static struct exynos_tmu_platform_data const exynos4210_default_tmu_data = {
734 .threshold = 80,
735 .trigger_levels[0] = 5,
736 .trigger_levels[1] = 20,
737 .trigger_levels[2] = 30,
738 .trigger_level0_en = 1,
739 .trigger_level1_en = 1,
740 .trigger_level2_en = 1,
741 .trigger_level3_en = 0,
742 .gain = 15,
743 .reference_voltage = 7,
744 .cal_type = TYPE_ONE_POINT_TRIMMING,
745 .freq_tab[0] = {
746 .freq_clip_max = 800 * 1000,
747 .temp_level = 85,
748 },
749 .freq_tab[1] = {
750 .freq_clip_max = 200 * 1000,
751 .temp_level = 100,
752 },
753 .freq_tab_count = 2,
754 .type = SOC_ARCH_EXYNOS4210,
755};
756#define EXYNOS4210_TMU_DRV_DATA (&exynos4210_default_tmu_data)
757#else
758#define EXYNOS4210_TMU_DRV_DATA (NULL)
759#endif
760
761#if defined(CONFIG_SOC_EXYNOS5250) || defined(CONFIG_SOC_EXYNOS4412)
762static struct exynos_tmu_platform_data const exynos_default_tmu_data = {
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000763 .threshold_falling = 10,
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530764 .trigger_levels[0] = 85,
765 .trigger_levels[1] = 103,
766 .trigger_levels[2] = 110,
767 .trigger_level0_en = 1,
768 .trigger_level1_en = 1,
769 .trigger_level2_en = 1,
770 .trigger_level3_en = 0,
771 .gain = 8,
772 .reference_voltage = 16,
773 .noise_cancel_mode = 4,
774 .cal_type = TYPE_ONE_POINT_TRIMMING,
775 .efuse_value = 55,
776 .freq_tab[0] = {
777 .freq_clip_max = 800 * 1000,
778 .temp_level = 85,
779 },
780 .freq_tab[1] = {
781 .freq_clip_max = 200 * 1000,
782 .temp_level = 103,
783 },
784 .freq_tab_count = 2,
785 .type = SOC_ARCH_EXYNOS,
786};
787#define EXYNOS_TMU_DRV_DATA (&exynos_default_tmu_data)
788#else
789#define EXYNOS_TMU_DRV_DATA (NULL)
790#endif
791
792#ifdef CONFIG_OF
793static const struct of_device_id exynos_tmu_match[] = {
794 {
795 .compatible = "samsung,exynos4210-tmu",
796 .data = (void *)EXYNOS4210_TMU_DRV_DATA,
797 },
798 {
799 .compatible = "samsung,exynos5250-tmu",
800 .data = (void *)EXYNOS_TMU_DRV_DATA,
801 },
802 {},
803};
804MODULE_DEVICE_TABLE(of, exynos_tmu_match);
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530805#endif
806
807static struct platform_device_id exynos_tmu_driver_ids[] = {
808 {
809 .name = "exynos4210-tmu",
810 .driver_data = (kernel_ulong_t)EXYNOS4210_TMU_DRV_DATA,
811 },
812 {
813 .name = "exynos5250-tmu",
814 .driver_data = (kernel_ulong_t)EXYNOS_TMU_DRV_DATA,
815 },
816 { },
817};
Jonghwan Choi3ae53b12012-10-23 14:54:42 +0800818MODULE_DEVICE_TABLE(platform, exynos_tmu_driver_ids);
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530819
820static inline struct exynos_tmu_platform_data *exynos_get_driver_data(
821 struct platform_device *pdev)
822{
823#ifdef CONFIG_OF
824 if (pdev->dev.of_node) {
825 const struct of_device_id *match;
826 match = of_match_node(exynos_tmu_match, pdev->dev.of_node);
827 if (!match)
828 return NULL;
829 return (struct exynos_tmu_platform_data *) match->data;
830 }
831#endif
832 return (struct exynos_tmu_platform_data *)
833 platform_get_device_id(pdev)->driver_data;
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530834}
Jonghwa Leebbf63be2012-11-21 13:31:01 +0900835
836#ifdef CONFIG_EXYNOS_THERMAL_EMUL
837static ssize_t exynos_tmu_emulation_show(struct device *dev,
838 struct device_attribute *attr,
839 char *buf)
840{
841 struct platform_device *pdev = container_of(dev,
842 struct platform_device, dev);
843 struct exynos_tmu_data *data = platform_get_drvdata(pdev);
844 unsigned int reg;
845 u8 temp_code;
846 int temp = 0;
847
848 if (data->soc == SOC_ARCH_EXYNOS4210)
849 goto out;
850
851 mutex_lock(&data->lock);
852 clk_enable(data->clk);
853 reg = readl(data->base + EXYNOS_EMUL_CON);
854 clk_disable(data->clk);
855 mutex_unlock(&data->lock);
856
857 if (reg & EXYNOS_EMUL_ENABLE) {
858 reg >>= EXYNOS_EMUL_DATA_SHIFT;
859 temp_code = reg & EXYNOS_EMUL_DATA_MASK;
860 temp = code_to_temp(data, temp_code);
861 }
862out:
863 return sprintf(buf, "%d\n", temp * MCELSIUS);
864}
865
866static ssize_t exynos_tmu_emulation_store(struct device *dev,
867 struct device_attribute *attr,
868 const char *buf, size_t count)
869{
870 struct platform_device *pdev = container_of(dev,
871 struct platform_device, dev);
872 struct exynos_tmu_data *data = platform_get_drvdata(pdev);
873 unsigned int reg;
874 int temp;
875
876 if (data->soc == SOC_ARCH_EXYNOS4210)
877 goto out;
878
879 if (!sscanf(buf, "%d\n", &temp) || temp < 0)
880 return -EINVAL;
881
882 mutex_lock(&data->lock);
883 clk_enable(data->clk);
884
885 reg = readl(data->base + EXYNOS_EMUL_CON);
886
887 if (temp) {
888 /* Both CELSIUS and MCELSIUS type are available for input */
889 if (temp > MCELSIUS)
890 temp /= MCELSIUS;
891
892 reg = (EXYNOS_EMUL_TIME << EXYNOS_EMUL_TIME_SHIFT) |
893 (temp_to_code(data, (temp / MCELSIUS))
894 << EXYNOS_EMUL_DATA_SHIFT) | EXYNOS_EMUL_ENABLE;
895 } else {
896 reg &= ~EXYNOS_EMUL_ENABLE;
897 }
898
899 writel(reg, data->base + EXYNOS_EMUL_CON);
900
901 clk_disable(data->clk);
902 mutex_unlock(&data->lock);
903
904out:
905 return count;
906}
907
908static DEVICE_ATTR(emulation, 0644, exynos_tmu_emulation_show,
909 exynos_tmu_emulation_store);
910static int create_emulation_sysfs(struct device *dev)
911{
912 return device_create_file(dev, &dev_attr_emulation);
913}
914static void remove_emulation_sysfs(struct device *dev)
915{
916 device_remove_file(dev, &dev_attr_emulation);
917}
918#else
919static inline int create_emulation_sysfs(struct device *dev) { return 0; }
920static inline void remove_emulation_sysfs(struct device *dev) {}
921#endif
922
Greg Kroah-Hartman4eab7a9e2012-12-21 13:15:52 -0800923static int exynos_tmu_probe(struct platform_device *pdev)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900924{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530925 struct exynos_tmu_data *data;
926 struct exynos_tmu_platform_data *pdata = pdev->dev.platform_data;
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530927 int ret, i;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900928
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530929 if (!pdata)
930 pdata = exynos_get_driver_data(pdev);
931
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900932 if (!pdata) {
933 dev_err(&pdev->dev, "No platform init data supplied.\n");
934 return -ENODEV;
935 }
Amit Daniel Kachhap79e093c2012-08-16 05:41:45 -0600936 data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data),
937 GFP_KERNEL);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900938 if (!data) {
939 dev_err(&pdev->dev, "Failed to allocate driver structure\n");
940 return -ENOMEM;
941 }
942
943 data->irq = platform_get_irq(pdev, 0);
944 if (data->irq < 0) {
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900945 dev_err(&pdev->dev, "Failed to get platform irq\n");
Amit Daniel Kachhap79e093c2012-08-16 05:41:45 -0600946 return data->irq;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900947 }
948
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530949 INIT_WORK(&data->irq_work, exynos_tmu_work);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900950
951 data->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
952 if (!data->mem) {
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900953 dev_err(&pdev->dev, "Failed to get platform resource\n");
Amit Daniel Kachhap79e093c2012-08-16 05:41:45 -0600954 return -ENOENT;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900955 }
956
Thierry Redingca36b1b2013-01-21 11:09:20 +0100957 data->base = devm_ioremap_resource(&pdev->dev, data->mem);
958 if (IS_ERR(data->base))
959 return PTR_ERR(data->base);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900960
Amit Daniel Kachhap79e093c2012-08-16 05:41:45 -0600961 ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq,
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530962 IRQF_TRIGGER_RISING, "exynos-tmu", data);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900963 if (ret) {
964 dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq);
Amit Daniel Kachhap79e093c2012-08-16 05:41:45 -0600965 return ret;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900966 }
967
968 data->clk = clk_get(NULL, "tmu_apbif");
969 if (IS_ERR(data->clk)) {
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900970 dev_err(&pdev->dev, "Failed to get clock\n");
Amit Daniel Kachhap79e093c2012-08-16 05:41:45 -0600971 return PTR_ERR(data->clk);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900972 }
973
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530974 if (pdata->type == SOC_ARCH_EXYNOS ||
975 pdata->type == SOC_ARCH_EXYNOS4210)
976 data->soc = pdata->type;
977 else {
978 ret = -EINVAL;
979 dev_err(&pdev->dev, "Platform not supported\n");
980 goto err_clk;
981 }
982
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900983 data->pdata = pdata;
984 platform_set_drvdata(pdev, data);
985 mutex_init(&data->lock);
986
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530987 ret = exynos_tmu_initialize(pdev);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900988 if (ret) {
989 dev_err(&pdev->dev, "Failed to initialize TMU\n");
990 goto err_clk;
991 }
992
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530993 exynos_tmu_control(pdev, true);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900994
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530995 /* Register the sensor with thermal management interface */
996 (&exynos_sensor_conf)->private_data = data;
997 exynos_sensor_conf.trip_data.trip_count = pdata->trigger_level0_en +
998 pdata->trigger_level1_en + pdata->trigger_level2_en +
999 pdata->trigger_level3_en;
1000
1001 for (i = 0; i < exynos_sensor_conf.trip_data.trip_count; i++)
1002 exynos_sensor_conf.trip_data.trip_val[i] =
1003 pdata->threshold + pdata->trigger_levels[i];
1004
Jonghwa Lee4f0a6842013-02-08 01:13:06 +00001005 exynos_sensor_conf.trip_data.trigger_falling = pdata->threshold_falling;
1006
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +05301007 exynos_sensor_conf.cooling_data.freq_clip_count =
1008 pdata->freq_tab_count;
1009 for (i = 0; i < pdata->freq_tab_count; i++) {
1010 exynos_sensor_conf.cooling_data.freq_data[i].freq_clip_max =
1011 pdata->freq_tab[i].freq_clip_max;
1012 exynos_sensor_conf.cooling_data.freq_data[i].temp_level =
1013 pdata->freq_tab[i].temp_level;
1014 }
1015
1016 ret = exynos_register_thermal(&exynos_sensor_conf);
1017 if (ret) {
1018 dev_err(&pdev->dev, "Failed to register thermal interface\n");
1019 goto err_clk;
1020 }
Jonghwa Leebbf63be2012-11-21 13:31:01 +09001021
1022 ret = create_emulation_sysfs(&pdev->dev);
1023 if (ret)
1024 dev_err(&pdev->dev, "Failed to create emulation mode sysfs node\n");
1025
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001026 return 0;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001027err_clk:
1028 platform_set_drvdata(pdev, NULL);
1029 clk_put(data->clk);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001030 return ret;
1031}
1032
Greg Kroah-Hartman4eab7a9e2012-12-21 13:15:52 -08001033static int exynos_tmu_remove(struct platform_device *pdev)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001034{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301035 struct exynos_tmu_data *data = platform_get_drvdata(pdev);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001036
Jonghwa Leebbf63be2012-11-21 13:31:01 +09001037 remove_emulation_sysfs(&pdev->dev);
1038
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301039 exynos_tmu_control(pdev, false);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001040
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +05301041 exynos_unregister_thermal();
1042
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001043 clk_put(data->clk);
1044
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001045 platform_set_drvdata(pdev, NULL);
1046
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001047 return 0;
1048}
1049
Rafael J. Wysocki08cd6752012-07-08 21:48:15 +02001050#ifdef CONFIG_PM_SLEEP
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301051static int exynos_tmu_suspend(struct device *dev)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001052{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301053 exynos_tmu_control(to_platform_device(dev), false);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001054
1055 return 0;
1056}
1057
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301058static int exynos_tmu_resume(struct device *dev)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001059{
Rafael J. Wysocki08cd6752012-07-08 21:48:15 +02001060 struct platform_device *pdev = to_platform_device(dev);
1061
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301062 exynos_tmu_initialize(pdev);
1063 exynos_tmu_control(pdev, true);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001064
1065 return 0;
1066}
Rafael J. Wysocki08cd6752012-07-08 21:48:15 +02001067
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301068static SIMPLE_DEV_PM_OPS(exynos_tmu_pm,
1069 exynos_tmu_suspend, exynos_tmu_resume);
1070#define EXYNOS_TMU_PM (&exynos_tmu_pm)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001071#else
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301072#define EXYNOS_TMU_PM NULL
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001073#endif
1074
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301075static struct platform_driver exynos_tmu_driver = {
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001076 .driver = {
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301077 .name = "exynos-tmu",
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001078 .owner = THIS_MODULE,
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301079 .pm = EXYNOS_TMU_PM,
Sachin Kamatcaa5cbd2012-12-12 15:54:24 +05301080 .of_match_table = of_match_ptr(exynos_tmu_match),
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001081 },
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301082 .probe = exynos_tmu_probe,
Greg Kroah-Hartman4eab7a9e2012-12-21 13:15:52 -08001083 .remove = exynos_tmu_remove,
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +05301084 .id_table = exynos_tmu_driver_ids,
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001085};
1086
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301087module_platform_driver(exynos_tmu_driver);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001088
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301089MODULE_DESCRIPTION("EXYNOS TMU Driver");
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001090MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
1091MODULE_LICENSE("GPL");
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +05301092MODULE_ALIAS("platform:exynos-tmu");