exynos_tmu.c 15.1 KB
Newer Older
D
Donggeun Kim 已提交
1
/*
2
 * exynos_tmu.c - Samsung EXYNOS TMU (Thermal Management Unit)
D
Donggeun Kim 已提交
3 4 5
 *
 *  Copyright (C) 2011 Samsung Electronics
 *  Donggeun Kim <dg77.kim@samsung.com>
6
 *  Amit Daniel Kachhap <amit.kachhap@linaro.org>
D
Donggeun Kim 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <linux/clk.h>
#include <linux/io.h>
26 27
#include <linux/interrupt.h>
#include <linux/module.h>
28
#include <linux/of.h>
29 30 31
#include <linux/platform_device.h>

#include "exynos_thermal_common.h"
32
#include "exynos_tmu.h"
33
#include "exynos_tmu_data.h"
34 35 36

struct exynos_tmu_data {
	struct exynos_tmu_platform_data *pdata;
D
Donggeun Kim 已提交
37 38 39
	struct resource *mem;
	void __iomem *base;
	int irq;
40
	enum soc_type soc;
D
Donggeun Kim 已提交
41 42 43 44 45 46 47 48 49 50
	struct work_struct irq_work;
	struct mutex lock;
	struct clk *clk;
	u8 temp_error1, temp_error2;
};

/*
 * TMU treats temperature as a mapped temperature code.
 * The temperature is converted differently depending on the calibration type.
 */
51
static int temp_to_code(struct exynos_tmu_data *data, u8 temp)
D
Donggeun Kim 已提交
52
{
53
	struct exynos_tmu_platform_data *pdata = data->pdata;
D
Donggeun Kim 已提交
54 55
	int temp_code;

56 57 58 59 60 61
	if (data->soc == SOC_ARCH_EXYNOS4210)
		/* temp should range between 25 and 125 */
		if (temp < 25 || temp > 125) {
			temp_code = -EINVAL;
			goto out;
		}
D
Donggeun Kim 已提交
62 63 64

	switch (pdata->cal_type) {
	case TYPE_TWO_POINT_TRIMMING:
65 66 67 68
		temp_code = (temp - pdata->first_point_trim) *
			(data->temp_error2 - data->temp_error1) /
			(pdata->second_point_trim - pdata->first_point_trim) +
			data->temp_error1;
D
Donggeun Kim 已提交
69 70
		break;
	case TYPE_ONE_POINT_TRIMMING:
71
		temp_code = temp + data->temp_error1 - pdata->first_point_trim;
D
Donggeun Kim 已提交
72 73
		break;
	default:
74
		temp_code = temp + pdata->default_temp_offset;
D
Donggeun Kim 已提交
75 76 77 78 79 80 81 82 83 84
		break;
	}
out:
	return temp_code;
}

/*
 * Calculate a temperature value from a temperature code.
 * The unit of the temperature is degree Celsius.
 */
85
static int code_to_temp(struct exynos_tmu_data *data, u8 temp_code)
D
Donggeun Kim 已提交
86
{
87
	struct exynos_tmu_platform_data *pdata = data->pdata;
D
Donggeun Kim 已提交
88 89
	int temp;

90 91 92 93 94 95
	if (data->soc == SOC_ARCH_EXYNOS4210)
		/* temp_code should range between 75 and 175 */
		if (temp_code < 75 || temp_code > 175) {
			temp = -ENODATA;
			goto out;
		}
D
Donggeun Kim 已提交
96 97 98

	switch (pdata->cal_type) {
	case TYPE_TWO_POINT_TRIMMING:
99 100 101 102
		temp = (temp_code - data->temp_error1) *
			(pdata->second_point_trim - pdata->first_point_trim) /
			(data->temp_error2 - data->temp_error1) +
			pdata->first_point_trim;
D
Donggeun Kim 已提交
103 104
		break;
	case TYPE_ONE_POINT_TRIMMING:
105
		temp = temp_code - data->temp_error1 + pdata->first_point_trim;
D
Donggeun Kim 已提交
106 107
		break;
	default:
108
		temp = temp_code - pdata->default_temp_offset;
D
Donggeun Kim 已提交
109 110 111 112 113 114
		break;
	}
out:
	return temp;
}

115
static int exynos_tmu_initialize(struct platform_device *pdev)
D
Donggeun Kim 已提交
116
{
117 118
	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
	struct exynos_tmu_platform_data *pdata = data->pdata;
119
	const struct exynos_tmu_registers *reg = pdata->registers;
120
	unsigned int status, trim_info = 0, con;
121 122
	unsigned int rising_threshold = 0, falling_threshold = 0;
	int ret = 0, threshold_code, i, trigger_levs = 0;
D
Donggeun Kim 已提交
123 124 125 126

	mutex_lock(&data->lock);
	clk_enable(data->clk);

127
	status = readb(data->base + reg->tmu_status);
D
Donggeun Kim 已提交
128 129 130 131 132
	if (!status) {
		ret = -EBUSY;
		goto out;
	}

133 134 135
	if (data->soc == SOC_ARCH_EXYNOS)
		__raw_writel(1, data->base + reg->triminfo_ctrl);

D
Donggeun Kim 已提交
136
	/* Save trimming info in order to perform calibration */
137 138 139 140
	trim_info = readl(data->base + reg->triminfo_data);
	data->temp_error1 = trim_info & EXYNOS_TMU_TEMP_MASK;
	data->temp_error2 = ((trim_info >> reg->triminfo_85_shift) &
				EXYNOS_TMU_TEMP_MASK);
141

142 143
	if ((pdata->min_efuse_value > data->temp_error1) ||
			(data->temp_error1 > pdata->max_efuse_value) ||
144 145 146
			(data->temp_error2 != 0))
		data->temp_error1 = pdata->efuse_value;

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	if (pdata->max_trigger_level > MAX_THRESHOLD_LEVS) {
		dev_err(&pdev->dev, "Invalid max trigger level\n");
		goto out;
	}

	for (i = 0; i < pdata->max_trigger_level; i++) {
		if (!pdata->trigger_levels[i])
			continue;

		if ((pdata->trigger_type[i] == HW_TRIP) &&
		(!pdata->trigger_levels[pdata->max_trigger_level - 1])) {
			dev_err(&pdev->dev, "Invalid hw trigger level\n");
			ret = -EINVAL;
			goto out;
		}

		/* Count trigger levels except the HW trip*/
		if (!(pdata->trigger_type[i] == HW_TRIP))
165
			trigger_levs++;
166
	}
167

168 169 170 171 172 173 174 175
	if (data->soc == SOC_ARCH_EXYNOS4210) {
		/* Write temperature code for threshold */
		threshold_code = temp_to_code(data, pdata->threshold);
		if (threshold_code < 0) {
			ret = threshold_code;
			goto out;
		}
		writeb(threshold_code,
176
			data->base + reg->threshold_temp);
177
		for (i = 0; i < trigger_levs; i++)
178 179
			writeb(pdata->trigger_levels[i], data->base +
			reg->threshold_th0 + i * sizeof(reg->threshold_th0));
180

181
		writel(reg->inten_rise_mask, data->base + reg->tmu_intclear);
182
	} else if (data->soc == SOC_ARCH_EXYNOS) {
183
		/* Write temperature code for rising and falling threshold */
184 185
		for (i = 0;
		i < trigger_levs && i < EXYNOS_MAX_TRIGGER_PER_REG; i++) {
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
			threshold_code = temp_to_code(data,
						pdata->trigger_levels[i]);
			if (threshold_code < 0) {
				ret = threshold_code;
				goto out;
			}
			rising_threshold |= threshold_code << 8 * i;
			if (pdata->threshold_falling) {
				threshold_code = temp_to_code(data,
						pdata->trigger_levels[i] -
						pdata->threshold_falling);
				if (threshold_code > 0)
					falling_threshold |=
						threshold_code << 8 * i;
			}
201 202 203
		}

		writel(rising_threshold,
204
				data->base + reg->threshold_th0);
205
		writel(falling_threshold,
206
				data->base + reg->threshold_th1);
207

208 209 210
		writel((reg->inten_rise_mask << reg->inten_rise_shift) |
			(reg->inten_fall_mask << reg->inten_fall_shift),
				data->base + reg->tmu_intclear);
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

		/* if last threshold limit is also present */
		i = pdata->max_trigger_level - 1;
		if (pdata->trigger_levels[i] &&
				(pdata->trigger_type[i] == HW_TRIP)) {
			threshold_code = temp_to_code(data,
						pdata->trigger_levels[i]);
			if (threshold_code < 0) {
				ret = threshold_code;
				goto out;
			}
			rising_threshold |= threshold_code << 8 * i;
			writel(rising_threshold,
				data->base + reg->threshold_th0);
			con = readl(data->base + reg->tmu_ctrl);
			con |= (1 << reg->therm_trip_en_shift);
			writel(con, data->base + reg->tmu_ctrl);
		}
D
Donggeun Kim 已提交
229 230 231 232 233 234 235 236
	}
out:
	clk_disable(data->clk);
	mutex_unlock(&data->lock);

	return ret;
}

237
static void exynos_tmu_control(struct platform_device *pdev, bool on)
D
Donggeun Kim 已提交
238
{
239 240
	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
	struct exynos_tmu_platform_data *pdata = data->pdata;
241
	const struct exynos_tmu_registers *reg = pdata->registers;
D
Donggeun Kim 已提交
242 243 244 245 246
	unsigned int con, interrupt_en;

	mutex_lock(&data->lock);
	clk_enable(data->clk);

247
	con = readl(data->base + reg->tmu_ctrl);
248

249
	if (pdata->reference_voltage) {
250 251
		con &= ~(reg->buf_vref_sel_mask << reg->buf_vref_sel_shift);
		con |= pdata->reference_voltage << reg->buf_vref_sel_shift;
252 253 254
	}

	if (pdata->gain) {
255 256
		con &= ~(reg->buf_slope_sel_mask << reg->buf_slope_sel_shift);
		con |= (pdata->gain << reg->buf_slope_sel_shift);
257 258 259
	}

	if (pdata->noise_cancel_mode) {
260 261 262
		con &= ~(reg->therm_trip_mode_mask <<
					reg->therm_trip_mode_shift);
		con |= (pdata->noise_cancel_mode << reg->therm_trip_mode_shift);
263 264
	}

D
Donggeun Kim 已提交
265
	if (on) {
266
		con |= (1 << reg->core_en_shift);
267
		interrupt_en =
268 269 270 271
			pdata->trigger_enable[3] << reg->inten_rise3_shift |
			pdata->trigger_enable[2] << reg->inten_rise2_shift |
			pdata->trigger_enable[1] << reg->inten_rise1_shift |
			pdata->trigger_enable[0] << reg->inten_rise0_shift;
272
		if (pdata->threshold_falling)
273
			interrupt_en |=
274
				interrupt_en << reg->inten_fall0_shift;
D
Donggeun Kim 已提交
275
	} else {
276
		con &= ~(1 << reg->core_en_shift);
D
Donggeun Kim 已提交
277 278
		interrupt_en = 0; /* Disable all interrupts */
	}
279 280
	writel(interrupt_en, data->base + reg->tmu_inten);
	writel(con, data->base + reg->tmu_ctrl);
D
Donggeun Kim 已提交
281 282 283 284 285

	clk_disable(data->clk);
	mutex_unlock(&data->lock);
}

286
static int exynos_tmu_read(struct exynos_tmu_data *data)
D
Donggeun Kim 已提交
287
{
288 289
	struct exynos_tmu_platform_data *pdata = data->pdata;
	const struct exynos_tmu_registers *reg = pdata->registers;
D
Donggeun Kim 已提交
290 291 292 293 294 295
	u8 temp_code;
	int temp;

	mutex_lock(&data->lock);
	clk_enable(data->clk);

296
	temp_code = readb(data->base + reg->tmu_cur_temp);
D
Donggeun Kim 已提交
297 298 299 300 301 302 303 304
	temp = code_to_temp(data, temp_code);

	clk_disable(data->clk);
	mutex_unlock(&data->lock);

	return temp;
}

305 306 307 308
#ifdef CONFIG_THERMAL_EMULATION
static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp)
{
	struct exynos_tmu_data *data = drv_data;
309 310 311
	struct exynos_tmu_platform_data *pdata = data->pdata;
	const struct exynos_tmu_registers *reg = pdata->registers;
	unsigned int val;
312 313 314 315 316 317 318 319 320 321 322
	int ret = -EINVAL;

	if (data->soc == SOC_ARCH_EXYNOS4210)
		goto out;

	if (temp && temp < MCELSIUS)
		goto out;

	mutex_lock(&data->lock);
	clk_enable(data->clk);

323
	val = readl(data->base + reg->emul_con);
324 325 326 327

	if (temp) {
		temp /= MCELSIUS;

328
		val = (EXYNOS_EMUL_TIME << reg->emul_time_shift) |
329
			(temp_to_code(data, temp)
330
			 << reg->emul_temp_shift) | EXYNOS_EMUL_ENABLE;
331
	} else {
332
		val &= ~EXYNOS_EMUL_ENABLE;
333 334
	}

335
	writel(val, data->base + reg->emul_con);
336 337 338 339 340 341 342 343 344 345 346 347

	clk_disable(data->clk);
	mutex_unlock(&data->lock);
	return 0;
out:
	return ret;
}
#else
static int exynos_tmu_set_emulation(void *drv_data,	unsigned long temp)
	{ return -EINVAL; }
#endif/*CONFIG_THERMAL_EMULATION*/

348 349 350 351 352 353
static struct thermal_sensor_conf exynos_sensor_conf = {
	.name			= "exynos-therm",
	.read_temperature	= (int (*)(void *))exynos_tmu_read,
	.write_emul_temp	= exynos_tmu_set_emulation,
};

354
static void exynos_tmu_work(struct work_struct *work)
D
Donggeun Kim 已提交
355
{
356 357
	struct exynos_tmu_data *data = container_of(work,
			struct exynos_tmu_data, irq_work);
358 359
	struct exynos_tmu_platform_data *pdata = data->pdata;
	const struct exynos_tmu_registers *reg = pdata->registers;
360
	unsigned int val_irq;
D
Donggeun Kim 已提交
361

362
	exynos_report_trigger(&exynos_sensor_conf);
D
Donggeun Kim 已提交
363 364
	mutex_lock(&data->lock);
	clk_enable(data->clk);
365

366 367 368 369
	/* TODO: take action based on particular interrupt */
	val_irq = readl(data->base + reg->tmu_intstat);
	/* clear the interrupts */
	writel(val_irq, data->base + reg->tmu_intclear);
370

D
Donggeun Kim 已提交
371 372
	clk_disable(data->clk);
	mutex_unlock(&data->lock);
373

374
	enable_irq(data->irq);
D
Donggeun Kim 已提交
375 376
}

377
static irqreturn_t exynos_tmu_irq(int irq, void *id)
D
Donggeun Kim 已提交
378
{
379
	struct exynos_tmu_data *data = id;
D
Donggeun Kim 已提交
380 381 382 383 384 385

	disable_irq_nosync(irq);
	schedule_work(&data->irq_work);

	return IRQ_HANDLED;
}
386 387 388 389 390 391 392

#ifdef CONFIG_OF
static const struct of_device_id exynos_tmu_match[] = {
	{
		.compatible = "samsung,exynos4210-tmu",
		.data = (void *)EXYNOS4210_TMU_DRV_DATA,
	},
393 394
	{
		.compatible = "samsung,exynos4412-tmu",
395
		.data = (void *)EXYNOS5250_TMU_DRV_DATA,
396
	},
397 398
	{
		.compatible = "samsung,exynos5250-tmu",
399
		.data = (void *)EXYNOS5250_TMU_DRV_DATA,
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
	},
	{},
};
MODULE_DEVICE_TABLE(of, exynos_tmu_match);
#endif

static inline struct  exynos_tmu_platform_data *exynos_get_driver_data(
			struct platform_device *pdev)
{
#ifdef CONFIG_OF
	if (pdev->dev.of_node) {
		const struct of_device_id *match;
		match = of_match_node(exynos_tmu_match, pdev->dev.of_node);
		if (!match)
			return NULL;
		return (struct exynos_tmu_platform_data *) match->data;
	}
#endif
418
	return NULL;
419
}
420

421
static int exynos_tmu_probe(struct platform_device *pdev)
D
Donggeun Kim 已提交
422
{
423 424
	struct exynos_tmu_data *data;
	struct exynos_tmu_platform_data *pdata = pdev->dev.platform_data;
425
	int ret, i;
D
Donggeun Kim 已提交
426

427 428 429
	if (!pdata)
		pdata = exynos_get_driver_data(pdev);

D
Donggeun Kim 已提交
430 431 432 433
	if (!pdata) {
		dev_err(&pdev->dev, "No platform init data supplied.\n");
		return -ENODEV;
	}
434 435
	data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data),
					GFP_KERNEL);
D
Donggeun Kim 已提交
436 437 438 439 440 441 442 443
	if (!data) {
		dev_err(&pdev->dev, "Failed to allocate driver structure\n");
		return -ENOMEM;
	}

	data->irq = platform_get_irq(pdev, 0);
	if (data->irq < 0) {
		dev_err(&pdev->dev, "Failed to get platform irq\n");
444
		return data->irq;
D
Donggeun Kim 已提交
445 446
	}

447
	INIT_WORK(&data->irq_work, exynos_tmu_work);
D
Donggeun Kim 已提交
448 449

	data->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
450 451 452
	data->base = devm_ioremap_resource(&pdev->dev, data->mem);
	if (IS_ERR(data->base))
		return PTR_ERR(data->base);
D
Donggeun Kim 已提交
453

454
	ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq,
455
		IRQF_TRIGGER_RISING, "exynos-tmu", data);
D
Donggeun Kim 已提交
456 457
	if (ret) {
		dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq);
458
		return ret;
D
Donggeun Kim 已提交
459 460
	}

461
	data->clk = devm_clk_get(&pdev->dev, "tmu_apbif");
D
Donggeun Kim 已提交
462 463
	if (IS_ERR(data->clk)) {
		dev_err(&pdev->dev, "Failed to get clock\n");
464
		return  PTR_ERR(data->clk);
D
Donggeun Kim 已提交
465 466
	}

467 468 469 470
	ret = clk_prepare(data->clk);
	if (ret)
		return ret;

471 472 473 474 475 476 477 478 479
	if (pdata->type == SOC_ARCH_EXYNOS ||
				pdata->type == SOC_ARCH_EXYNOS4210)
		data->soc = pdata->type;
	else {
		ret = -EINVAL;
		dev_err(&pdev->dev, "Platform not supported\n");
		goto err_clk;
	}

D
Donggeun Kim 已提交
480 481 482 483
	data->pdata = pdata;
	platform_set_drvdata(pdev, data);
	mutex_init(&data->lock);

484
	ret = exynos_tmu_initialize(pdev);
D
Donggeun Kim 已提交
485 486 487 488 489
	if (ret) {
		dev_err(&pdev->dev, "Failed to initialize TMU\n");
		goto err_clk;
	}

490
	exynos_tmu_control(pdev, true);
D
Donggeun Kim 已提交
491

492
	/* Register the sensor with thermal management interface */
493
	(&exynos_sensor_conf)->driver_data = data;
494 495 496
	exynos_sensor_conf.trip_data.trip_count = pdata->trigger_enable[0] +
			pdata->trigger_enable[1] + pdata->trigger_enable[2]+
			pdata->trigger_enable[3];
497

498
	for (i = 0; i < exynos_sensor_conf.trip_data.trip_count; i++) {
499 500
		exynos_sensor_conf.trip_data.trip_val[i] =
			pdata->threshold + pdata->trigger_levels[i];
501 502 503
		exynos_sensor_conf.trip_data.trip_type[i] =
					pdata->trigger_type[i];
	}
504

505 506
	exynos_sensor_conf.trip_data.trigger_falling = pdata->threshold_falling;

507 508 509 510 511 512 513 514 515 516 517 518 519 520
	exynos_sensor_conf.cooling_data.freq_clip_count =
						pdata->freq_tab_count;
	for (i = 0; i < pdata->freq_tab_count; i++) {
		exynos_sensor_conf.cooling_data.freq_data[i].freq_clip_max =
					pdata->freq_tab[i].freq_clip_max;
		exynos_sensor_conf.cooling_data.freq_data[i].temp_level =
					pdata->freq_tab[i].temp_level;
	}

	ret = exynos_register_thermal(&exynos_sensor_conf);
	if (ret) {
		dev_err(&pdev->dev, "Failed to register thermal interface\n");
		goto err_clk;
	}
521

D
Donggeun Kim 已提交
522 523
	return 0;
err_clk:
524
	clk_unprepare(data->clk);
D
Donggeun Kim 已提交
525 526 527
	return ret;
}

528
static int exynos_tmu_remove(struct platform_device *pdev)
D
Donggeun Kim 已提交
529
{
530
	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
D
Donggeun Kim 已提交
531

532
	exynos_tmu_control(pdev, false);
D
Donggeun Kim 已提交
533

534
	exynos_unregister_thermal(&exynos_sensor_conf);
535

536
	clk_unprepare(data->clk);
D
Donggeun Kim 已提交
537 538 539 540

	return 0;
}

541
#ifdef CONFIG_PM_SLEEP
542
static int exynos_tmu_suspend(struct device *dev)
D
Donggeun Kim 已提交
543
{
544
	exynos_tmu_control(to_platform_device(dev), false);
D
Donggeun Kim 已提交
545 546 547 548

	return 0;
}

549
static int exynos_tmu_resume(struct device *dev)
D
Donggeun Kim 已提交
550
{
551 552
	struct platform_device *pdev = to_platform_device(dev);

553 554
	exynos_tmu_initialize(pdev);
	exynos_tmu_control(pdev, true);
D
Donggeun Kim 已提交
555 556 557

	return 0;
}
558

559 560 561
static SIMPLE_DEV_PM_OPS(exynos_tmu_pm,
			 exynos_tmu_suspend, exynos_tmu_resume);
#define EXYNOS_TMU_PM	(&exynos_tmu_pm)
D
Donggeun Kim 已提交
562
#else
563
#define EXYNOS_TMU_PM	NULL
D
Donggeun Kim 已提交
564 565
#endif

566
static struct platform_driver exynos_tmu_driver = {
D
Donggeun Kim 已提交
567
	.driver = {
568
		.name   = "exynos-tmu",
D
Donggeun Kim 已提交
569
		.owner  = THIS_MODULE,
570
		.pm     = EXYNOS_TMU_PM,
571
		.of_match_table = of_match_ptr(exynos_tmu_match),
D
Donggeun Kim 已提交
572
	},
573
	.probe = exynos_tmu_probe,
574
	.remove	= exynos_tmu_remove,
D
Donggeun Kim 已提交
575 576
};

577
module_platform_driver(exynos_tmu_driver);
D
Donggeun Kim 已提交
578

579
MODULE_DESCRIPTION("EXYNOS TMU Driver");
D
Donggeun Kim 已提交
580 581
MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
MODULE_LICENSE("GPL");
582
MODULE_ALIAS("platform:exynos-tmu");