exynos_tmu.c 14.5 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 121 122
	unsigned int status, trim_info;
	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
	/* Count trigger levels to be enabled */
	for (i = 0; i < MAX_THRESHOLD_LEVS; i++)
		if (pdata->trigger_levels[i])
			trigger_levs++;

152 153 154 155 156 157 158 159
	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,
160
			data->base + reg->threshold_temp);
161
		for (i = 0; i < trigger_levs; i++)
162 163
			writeb(pdata->trigger_levels[i], data->base +
			reg->threshold_th0 + i * sizeof(reg->threshold_th0));
164

165
		writel(reg->inten_rise_mask, data->base + reg->tmu_intclear);
166
	} else if (data->soc == SOC_ARCH_EXYNOS) {
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
		/* Write temperature code for rising and falling threshold */
		for (i = 0; i < trigger_levs; i++) {
			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;
			}
184 185 186
		}

		writel(rising_threshold,
187
				data->base + reg->threshold_th0);
188
		writel(falling_threshold,
189
				data->base + reg->threshold_th1);
190

191 192 193
		writel((reg->inten_rise_mask << reg->inten_rise_shift) |
			(reg->inten_fall_mask << reg->inten_fall_shift),
				data->base + reg->tmu_intclear);
D
Donggeun Kim 已提交
194 195 196 197 198 199 200 201
	}
out:
	clk_disable(data->clk);
	mutex_unlock(&data->lock);

	return ret;
}

202
static void exynos_tmu_control(struct platform_device *pdev, bool on)
D
Donggeun Kim 已提交
203
{
204 205
	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
	struct exynos_tmu_platform_data *pdata = data->pdata;
206
	const struct exynos_tmu_registers *reg = pdata->registers;
D
Donggeun Kim 已提交
207 208 209 210 211
	unsigned int con, interrupt_en;

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

212
	con = readl(data->base + reg->tmu_ctrl);
213

214
	if (pdata->reference_voltage) {
215 216
		con &= ~(reg->buf_vref_sel_mask << reg->buf_vref_sel_shift);
		con |= pdata->reference_voltage << reg->buf_vref_sel_shift;
217 218 219
	}

	if (pdata->gain) {
220 221
		con &= ~(reg->buf_slope_sel_mask << reg->buf_slope_sel_shift);
		con |= (pdata->gain << reg->buf_slope_sel_shift);
222 223 224
	}

	if (pdata->noise_cancel_mode) {
225 226 227
		con &= ~(reg->therm_trip_mode_mask <<
					reg->therm_trip_mode_shift);
		con |= (pdata->noise_cancel_mode << reg->therm_trip_mode_shift);
228 229
	}

D
Donggeun Kim 已提交
230
	if (on) {
231
		con |= (1 << reg->core_en_shift);
232
		interrupt_en =
233 234 235 236
			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;
237
		if (pdata->threshold_falling)
238
			interrupt_en |=
239
				interrupt_en << reg->inten_fall0_shift;
D
Donggeun Kim 已提交
240
	} else {
241
		con &= ~(1 << reg->core_en_shift);
D
Donggeun Kim 已提交
242 243
		interrupt_en = 0; /* Disable all interrupts */
	}
244 245
	writel(interrupt_en, data->base + reg->tmu_inten);
	writel(con, data->base + reg->tmu_ctrl);
D
Donggeun Kim 已提交
246 247 248 249 250

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

251
static int exynos_tmu_read(struct exynos_tmu_data *data)
D
Donggeun Kim 已提交
252
{
253 254
	struct exynos_tmu_platform_data *pdata = data->pdata;
	const struct exynos_tmu_registers *reg = pdata->registers;
D
Donggeun Kim 已提交
255 256 257 258 259 260
	u8 temp_code;
	int temp;

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

261
	temp_code = readb(data->base + reg->tmu_cur_temp);
D
Donggeun Kim 已提交
262 263 264 265 266 267 268 269
	temp = code_to_temp(data, temp_code);

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

	return temp;
}

270 271 272 273
#ifdef CONFIG_THERMAL_EMULATION
static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp)
{
	struct exynos_tmu_data *data = drv_data;
274 275 276
	struct exynos_tmu_platform_data *pdata = data->pdata;
	const struct exynos_tmu_registers *reg = pdata->registers;
	unsigned int val;
277 278 279 280 281 282 283 284 285 286 287
	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);

288
	val = readl(data->base + reg->emul_con);
289 290 291 292

	if (temp) {
		temp /= MCELSIUS;

293
		val = (EXYNOS_EMUL_TIME << reg->emul_time_shift) |
294
			(temp_to_code(data, temp)
295
			 << reg->emul_temp_shift) | EXYNOS_EMUL_ENABLE;
296
	} else {
297
		val &= ~EXYNOS_EMUL_ENABLE;
298 299
	}

300
	writel(val, data->base + reg->emul_con);
301 302 303 304 305 306 307 308 309 310 311 312

	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*/

313
static void exynos_tmu_work(struct work_struct *work)
D
Donggeun Kim 已提交
314
{
315 316
	struct exynos_tmu_data *data = container_of(work,
			struct exynos_tmu_data, irq_work);
317 318
	struct exynos_tmu_platform_data *pdata = data->pdata;
	const struct exynos_tmu_registers *reg = pdata->registers;
D
Donggeun Kim 已提交
319

320
	exynos_report_trigger();
D
Donggeun Kim 已提交
321 322
	mutex_lock(&data->lock);
	clk_enable(data->clk);
323

324
	if (data->soc == SOC_ARCH_EXYNOS)
325 326 327
		writel((reg->inten_rise_mask << reg->inten_rise_shift) |
			(reg->inten_fall_mask << reg->inten_fall_shift),
				data->base + reg->tmu_intclear);
328
	else
329 330
		writel(reg->inten_rise_mask, data->base + reg->tmu_intclear);

D
Donggeun Kim 已提交
331 332
	clk_disable(data->clk);
	mutex_unlock(&data->lock);
333

334
	enable_irq(data->irq);
D
Donggeun Kim 已提交
335 336
}

337
static irqreturn_t exynos_tmu_irq(int irq, void *id)
D
Donggeun Kim 已提交
338
{
339
	struct exynos_tmu_data *data = id;
D
Donggeun Kim 已提交
340 341 342 343 344 345

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

	return IRQ_HANDLED;
}
346 347 348
static struct thermal_sensor_conf exynos_sensor_conf = {
	.name			= "exynos-therm",
	.read_temperature	= (int (*)(void *))exynos_tmu_read,
349
	.write_emul_temp	= exynos_tmu_set_emulation,
350 351 352 353 354 355 356 357
};

#ifdef CONFIG_OF
static const struct of_device_id exynos_tmu_match[] = {
	{
		.compatible = "samsung,exynos4210-tmu",
		.data = (void *)EXYNOS4210_TMU_DRV_DATA,
	},
358 359
	{
		.compatible = "samsung,exynos4412-tmu",
360
		.data = (void *)EXYNOS5250_TMU_DRV_DATA,
361
	},
362 363
	{
		.compatible = "samsung,exynos5250-tmu",
364
		.data = (void *)EXYNOS5250_TMU_DRV_DATA,
365 366 367 368 369 370 371 372 373 374 375 376 377
	},
	{},
};
MODULE_DEVICE_TABLE(of, exynos_tmu_match);
#endif

static struct platform_device_id exynos_tmu_driver_ids[] = {
	{
		.name		= "exynos4210-tmu",
		.driver_data    = (kernel_ulong_t)EXYNOS4210_TMU_DRV_DATA,
	},
	{
		.name		= "exynos5250-tmu",
378
		.driver_data    = (kernel_ulong_t)EXYNOS5250_TMU_DRV_DATA,
379 380 381
	},
	{ },
};
382
MODULE_DEVICE_TABLE(platform, exynos_tmu_driver_ids);
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397

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
	return (struct exynos_tmu_platform_data *)
			platform_get_device_id(pdev)->driver_data;
398
}
399

400
static int exynos_tmu_probe(struct platform_device *pdev)
D
Donggeun Kim 已提交
401
{
402 403
	struct exynos_tmu_data *data;
	struct exynos_tmu_platform_data *pdata = pdev->dev.platform_data;
404
	int ret, i;
D
Donggeun Kim 已提交
405

406 407 408
	if (!pdata)
		pdata = exynos_get_driver_data(pdev);

D
Donggeun Kim 已提交
409 410 411 412
	if (!pdata) {
		dev_err(&pdev->dev, "No platform init data supplied.\n");
		return -ENODEV;
	}
413 414
	data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data),
					GFP_KERNEL);
D
Donggeun Kim 已提交
415 416 417 418 419 420 421 422
	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");
423
		return data->irq;
D
Donggeun Kim 已提交
424 425
	}

426
	INIT_WORK(&data->irq_work, exynos_tmu_work);
D
Donggeun Kim 已提交
427 428

	data->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
429 430 431
	data->base = devm_ioremap_resource(&pdev->dev, data->mem);
	if (IS_ERR(data->base))
		return PTR_ERR(data->base);
D
Donggeun Kim 已提交
432

433
	ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq,
434
		IRQF_TRIGGER_RISING, "exynos-tmu", data);
D
Donggeun Kim 已提交
435 436
	if (ret) {
		dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq);
437
		return ret;
D
Donggeun Kim 已提交
438 439
	}

440
	data->clk = devm_clk_get(&pdev->dev, "tmu_apbif");
D
Donggeun Kim 已提交
441 442
	if (IS_ERR(data->clk)) {
		dev_err(&pdev->dev, "Failed to get clock\n");
443
		return  PTR_ERR(data->clk);
D
Donggeun Kim 已提交
444 445
	}

446 447 448 449
	ret = clk_prepare(data->clk);
	if (ret)
		return ret;

450 451 452 453 454 455 456 457 458
	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 已提交
459 460 461 462
	data->pdata = pdata;
	platform_set_drvdata(pdev, data);
	mutex_init(&data->lock);

463
	ret = exynos_tmu_initialize(pdev);
D
Donggeun Kim 已提交
464 465 466 467 468
	if (ret) {
		dev_err(&pdev->dev, "Failed to initialize TMU\n");
		goto err_clk;
	}

469
	exynos_tmu_control(pdev, true);
D
Donggeun Kim 已提交
470

471 472
	/* Register the sensor with thermal management interface */
	(&exynos_sensor_conf)->private_data = data;
473 474 475
	exynos_sensor_conf.trip_data.trip_count = pdata->trigger_enable[0] +
			pdata->trigger_enable[1] + pdata->trigger_enable[2]+
			pdata->trigger_enable[3];
476 477 478 479 480

	for (i = 0; i < exynos_sensor_conf.trip_data.trip_count; i++)
		exynos_sensor_conf.trip_data.trip_val[i] =
			pdata->threshold + pdata->trigger_levels[i];

481 482
	exynos_sensor_conf.trip_data.trigger_falling = pdata->threshold_falling;

483 484 485 486 487 488 489 490 491 492 493 494 495 496
	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;
	}
497

D
Donggeun Kim 已提交
498 499
	return 0;
err_clk:
500
	clk_unprepare(data->clk);
D
Donggeun Kim 已提交
501 502 503
	return ret;
}

504
static int exynos_tmu_remove(struct platform_device *pdev)
D
Donggeun Kim 已提交
505
{
506
	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
D
Donggeun Kim 已提交
507

508
	exynos_tmu_control(pdev, false);
D
Donggeun Kim 已提交
509

510 511
	exynos_unregister_thermal();

512
	clk_unprepare(data->clk);
D
Donggeun Kim 已提交
513 514 515 516

	return 0;
}

517
#ifdef CONFIG_PM_SLEEP
518
static int exynos_tmu_suspend(struct device *dev)
D
Donggeun Kim 已提交
519
{
520
	exynos_tmu_control(to_platform_device(dev), false);
D
Donggeun Kim 已提交
521 522 523 524

	return 0;
}

525
static int exynos_tmu_resume(struct device *dev)
D
Donggeun Kim 已提交
526
{
527 528
	struct platform_device *pdev = to_platform_device(dev);

529 530
	exynos_tmu_initialize(pdev);
	exynos_tmu_control(pdev, true);
D
Donggeun Kim 已提交
531 532 533

	return 0;
}
534

535 536 537
static SIMPLE_DEV_PM_OPS(exynos_tmu_pm,
			 exynos_tmu_suspend, exynos_tmu_resume);
#define EXYNOS_TMU_PM	(&exynos_tmu_pm)
D
Donggeun Kim 已提交
538
#else
539
#define EXYNOS_TMU_PM	NULL
D
Donggeun Kim 已提交
540 541
#endif

542
static struct platform_driver exynos_tmu_driver = {
D
Donggeun Kim 已提交
543
	.driver = {
544
		.name   = "exynos-tmu",
D
Donggeun Kim 已提交
545
		.owner  = THIS_MODULE,
546
		.pm     = EXYNOS_TMU_PM,
547
		.of_match_table = of_match_ptr(exynos_tmu_match),
D
Donggeun Kim 已提交
548
	},
549
	.probe = exynos_tmu_probe,
550
	.remove	= exynos_tmu_remove,
551
	.id_table = exynos_tmu_driver_ids,
D
Donggeun Kim 已提交
552 553
};

554
module_platform_driver(exynos_tmu_driver);
D
Donggeun Kim 已提交
555

556
MODULE_DESCRIPTION("EXYNOS TMU Driver");
D
Donggeun Kim 已提交
557 558
MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
MODULE_LICENSE("GPL");
559
MODULE_ALIAS("platform:exynos-tmu");