1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * exynos_tmu.c - Samsung Exynos TMU (Thermal Management Unit)
5 * Copyright (C) 2014 Samsung Electronics
6 * Bartlomiej Zolnierkiewicz <b.zolnierkie@samsung.com>
7 * Lukasz Majewski <l.majewski@samsung.com>
9 * Copyright (C) 2011 Samsung Electronics
10 * Donggeun Kim <dg77.kim@samsung.com>
11 * Amit Daniel Kachhap <amit.kachhap@linaro.org>
14 #include <linux/clk.h>
16 #include <linux/interrupt.h>
17 #include <linux/module.h>
19 #include <linux/of_address.h>
20 #include <linux/of_irq.h>
21 #include <linux/platform_device.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/thermal.h>
25 #include <dt-bindings/thermal/thermal_exynos.h>
27 /* Exynos generic registers */
28 #define EXYNOS_TMU_REG_TRIMINFO 0x0
29 #define EXYNOS_TMU_REG_CONTROL 0x20
30 #define EXYNOS_TMU_REG_STATUS 0x28
31 #define EXYNOS_TMU_REG_CURRENT_TEMP 0x40
32 #define EXYNOS_TMU_REG_INTEN 0x70
33 #define EXYNOS_TMU_REG_INTSTAT 0x74
34 #define EXYNOS_TMU_REG_INTCLEAR 0x78
36 #define EXYNOS_TMU_TEMP_MASK 0xff
37 #define EXYNOS_TMU_REF_VOLTAGE_SHIFT 24
38 #define EXYNOS_TMU_REF_VOLTAGE_MASK 0x1f
39 #define EXYNOS_TMU_BUF_SLOPE_SEL_MASK 0xf
40 #define EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT 8
41 #define EXYNOS_TMU_CORE_EN_SHIFT 0
43 /* Exynos3250 specific registers */
44 #define EXYNOS_TMU_TRIMINFO_CON1 0x10
46 /* Exynos4210 specific registers */
47 #define EXYNOS4210_TMU_REG_THRESHOLD_TEMP 0x44
48 #define EXYNOS4210_TMU_REG_TRIG_LEVEL0 0x50
50 /* Exynos5250, Exynos4412, Exynos3250 specific registers */
51 #define EXYNOS_TMU_TRIMINFO_CON2 0x14
52 #define EXYNOS_THD_TEMP_RISE 0x50
53 #define EXYNOS_THD_TEMP_FALL 0x54
54 #define EXYNOS_EMUL_CON 0x80
56 #define EXYNOS_TRIMINFO_RELOAD_ENABLE 1
57 #define EXYNOS_TRIMINFO_25_SHIFT 0
58 #define EXYNOS_TRIMINFO_85_SHIFT 8
59 #define EXYNOS_TMU_TRIP_MODE_SHIFT 13
60 #define EXYNOS_TMU_TRIP_MODE_MASK 0x7
61 #define EXYNOS_TMU_THERM_TRIP_EN_SHIFT 12
63 #define EXYNOS_TMU_INTEN_RISE0_SHIFT 0
64 #define EXYNOS_TMU_INTEN_FALL0_SHIFT 16
66 #define EXYNOS_EMUL_TIME 0x57F0
67 #define EXYNOS_EMUL_TIME_MASK 0xffff
68 #define EXYNOS_EMUL_TIME_SHIFT 16
69 #define EXYNOS_EMUL_DATA_SHIFT 8
70 #define EXYNOS_EMUL_DATA_MASK 0xFF
71 #define EXYNOS_EMUL_ENABLE 0x1
73 /* Exynos5260 specific */
74 #define EXYNOS5260_TMU_REG_INTEN 0xC0
75 #define EXYNOS5260_TMU_REG_INTSTAT 0xC4
76 #define EXYNOS5260_TMU_REG_INTCLEAR 0xC8
77 #define EXYNOS5260_EMUL_CON 0x100
79 /* Exynos4412 specific */
80 #define EXYNOS4412_MUX_ADDR_VALUE 6
81 #define EXYNOS4412_MUX_ADDR_SHIFT 20
83 /* Exynos5433 specific registers */
84 #define EXYNOS5433_THD_TEMP_RISE3_0 0x050
85 #define EXYNOS5433_THD_TEMP_RISE7_4 0x054
86 #define EXYNOS5433_THD_TEMP_FALL3_0 0x060
87 #define EXYNOS5433_THD_TEMP_FALL7_4 0x064
88 #define EXYNOS5433_TMU_REG_INTEN 0x0c0
89 #define EXYNOS5433_TMU_REG_INTPEND 0x0c8
90 #define EXYNOS5433_TMU_EMUL_CON 0x110
91 #define EXYNOS5433_TMU_PD_DET_EN 0x130
93 #define EXYNOS5433_TRIMINFO_SENSOR_ID_SHIFT 16
94 #define EXYNOS5433_TRIMINFO_CALIB_SEL_SHIFT 23
95 #define EXYNOS5433_TRIMINFO_SENSOR_ID_MASK \
96 (0xf << EXYNOS5433_TRIMINFO_SENSOR_ID_SHIFT)
97 #define EXYNOS5433_TRIMINFO_CALIB_SEL_MASK BIT(23)
99 #define EXYNOS5433_TRIMINFO_ONE_POINT_TRIMMING 0
100 #define EXYNOS5433_TRIMINFO_TWO_POINT_TRIMMING 1
102 #define EXYNOS5433_PD_DET_EN 1
104 #define EXYNOS5433_G3D_BASE 0x10070000
106 /* Exynos7 specific registers */
107 #define EXYNOS7_THD_TEMP_RISE7_6 0x50
108 #define EXYNOS7_THD_TEMP_FALL7_6 0x60
109 #define EXYNOS7_TMU_REG_INTEN 0x110
110 #define EXYNOS7_TMU_REG_INTPEND 0x118
111 #define EXYNOS7_TMU_REG_EMUL_CON 0x160
113 #define EXYNOS7_TMU_TEMP_MASK 0x1ff
114 #define EXYNOS7_PD_DET_EN_SHIFT 23
115 #define EXYNOS7_TMU_INTEN_RISE0_SHIFT 0
116 #define EXYNOS7_EMUL_DATA_SHIFT 7
117 #define EXYNOS7_EMUL_DATA_MASK 0x1ff
119 #define EXYNOS_FIRST_POINT_TRIM 25
120 #define EXYNOS_SECOND_POINT_TRIM 85
122 #define EXYNOS_NOISE_CANCEL_MODE 4
124 #define MCELSIUS 1000
127 SOC_ARCH_EXYNOS3250 = 1,
133 SOC_ARCH_EXYNOS5420_TRIMINFO,
139 * struct exynos_tmu_data : A structure to hold the private data of the TMU
141 * @id: identifier of the one instance of the TMU controller.
142 * @base: base address of the single instance of the TMU controller.
143 * @base_second: base address of the common registers of the TMU controller.
144 * @irq: irq number of the TMU controller.
145 * @soc: id of the SOC type.
146 * @irq_work: pointer to the irq work structure.
147 * @lock: lock to implement synchronization.
148 * @clk: pointer to the clock structure.
149 * @clk_sec: pointer to the clock structure for accessing the base_second.
150 * @sclk: pointer to the clock structure for accessing the tmu special clk.
151 * @cal_type: calibration type for temperature
152 * @efuse_value: SoC defined fuse value
153 * @min_efuse_value: minimum valid trimming data
154 * @max_efuse_value: maximum valid trimming data
155 * @temp_error1: fused value of the first point trim.
156 * @temp_error2: fused value of the second point trim.
157 * @gain: gain of amplifier in the positive-TC generator block
159 * @reference_voltage: reference voltage of amplifier
160 * in the positive-TC generator block
161 * 0 < reference_voltage <= 31
162 * @regulator: pointer to the TMU regulator structure.
163 * @tzd: pointer to thermal_zone_device structure
164 * @ntrip: number of supported trip points.
165 * @enabled: current status of TMU device
166 * @tmu_set_trip_temp: SoC specific method to set trip (rising threshold)
167 * @tmu_set_trip_hyst: SoC specific to set hysteresis (falling threshold)
168 * @tmu_initialize: SoC specific TMU initialization method
169 * @tmu_control: SoC specific TMU control method
170 * @tmu_read: SoC specific TMU temperature read method
171 * @tmu_set_emulation: SoC specific TMU emulation setting method
172 * @tmu_clear_irqs: SoC specific TMU interrupts clearing method
174 struct exynos_tmu_data {
177 void __iomem *base_second;
180 struct work_struct irq_work;
182 struct clk *clk, *clk_sec, *sclk;
187 u16 temp_error1, temp_error2;
189 u8 reference_voltage;
190 struct regulator *regulator;
191 struct thermal_zone_device *tzd;
195 void (*tmu_set_trip_temp)(struct exynos_tmu_data *data, int trip,
197 void (*tmu_set_trip_hyst)(struct exynos_tmu_data *data, int trip,
199 void (*tmu_initialize)(struct platform_device *pdev);
200 void (*tmu_control)(struct platform_device *pdev, bool on);
201 int (*tmu_read)(struct exynos_tmu_data *data);
202 void (*tmu_set_emulation)(struct exynos_tmu_data *data, int temp);
203 void (*tmu_clear_irqs)(struct exynos_tmu_data *data);
207 * TMU treats temperature as a mapped temperature code.
208 * The temperature is converted differently depending on the calibration type.
210 static int temp_to_code(struct exynos_tmu_data *data, u8 temp)
212 if (data->cal_type == TYPE_ONE_POINT_TRIMMING)
213 return temp + data->temp_error1 - EXYNOS_FIRST_POINT_TRIM;
215 return (temp - EXYNOS_FIRST_POINT_TRIM) *
216 (data->temp_error2 - data->temp_error1) /
217 (EXYNOS_SECOND_POINT_TRIM - EXYNOS_FIRST_POINT_TRIM) +
222 * Calculate a temperature value from a temperature code.
223 * The unit of the temperature is degree Celsius.
225 static int code_to_temp(struct exynos_tmu_data *data, u16 temp_code)
227 if (data->cal_type == TYPE_ONE_POINT_TRIMMING)
228 return temp_code - data->temp_error1 + EXYNOS_FIRST_POINT_TRIM;
230 return (temp_code - data->temp_error1) *
231 (EXYNOS_SECOND_POINT_TRIM - EXYNOS_FIRST_POINT_TRIM) /
232 (data->temp_error2 - data->temp_error1) +
233 EXYNOS_FIRST_POINT_TRIM;
236 static void sanitize_temp_error(struct exynos_tmu_data *data, u32 trim_info)
239 (data->soc == SOC_ARCH_EXYNOS7) ? EXYNOS7_TMU_TEMP_MASK
240 : EXYNOS_TMU_TEMP_MASK;
242 data->temp_error1 = trim_info & tmu_temp_mask;
243 data->temp_error2 = ((trim_info >> EXYNOS_TRIMINFO_85_SHIFT) &
244 EXYNOS_TMU_TEMP_MASK);
246 if (!data->temp_error1 ||
247 (data->min_efuse_value > data->temp_error1) ||
248 (data->temp_error1 > data->max_efuse_value))
249 data->temp_error1 = data->efuse_value & EXYNOS_TMU_TEMP_MASK;
251 if (!data->temp_error2)
253 (data->efuse_value >> EXYNOS_TRIMINFO_85_SHIFT) &
254 EXYNOS_TMU_TEMP_MASK;
257 static int exynos_tmu_initialize(struct platform_device *pdev)
259 struct exynos_tmu_data *data = platform_get_drvdata(pdev);
260 struct thermal_zone_device *tzd = data->tzd;
261 int num_trips = thermal_zone_get_num_trips(tzd);
265 ret = thermal_zone_get_crit_temp(tzd, &temp);
266 if (ret && data->soc != SOC_ARCH_EXYNOS5433) { /* FIXME */
268 "No CRITICAL trip point defined in device tree!\n");
272 if (num_trips > data->ntrip) {
274 "More trip points than supported by this TMU.\n");
276 "%d trip points should be configured in polling mode.\n",
277 num_trips - data->ntrip);
280 mutex_lock(&data->lock);
281 clk_enable(data->clk);
282 if (!IS_ERR(data->clk_sec))
283 clk_enable(data->clk_sec);
285 status = readb(data->base + EXYNOS_TMU_REG_STATUS);
290 min_t(int, num_trips, data->ntrip);
292 data->tmu_initialize(pdev);
294 /* Write temperature code for rising and falling threshold */
295 for (i = 0; i < ntrips; i++) {
297 struct thermal_trip trip;
299 ret = thermal_zone_get_trip(tzd, i, &trip);
303 data->tmu_set_trip_temp(data, i, trip.temperature / MCELSIUS);
304 data->tmu_set_trip_hyst(data, i, trip.temperature / MCELSIUS,
305 trip.hysteresis / MCELSIUS);
308 data->tmu_clear_irqs(data);
311 clk_disable(data->clk);
312 mutex_unlock(&data->lock);
313 if (!IS_ERR(data->clk_sec))
314 clk_disable(data->clk_sec);
319 static u32 get_con_reg(struct exynos_tmu_data *data, u32 con)
321 if (data->soc == SOC_ARCH_EXYNOS4412 ||
322 data->soc == SOC_ARCH_EXYNOS3250)
323 con |= (EXYNOS4412_MUX_ADDR_VALUE << EXYNOS4412_MUX_ADDR_SHIFT);
325 con &= ~(EXYNOS_TMU_REF_VOLTAGE_MASK << EXYNOS_TMU_REF_VOLTAGE_SHIFT);
326 con |= data->reference_voltage << EXYNOS_TMU_REF_VOLTAGE_SHIFT;
328 con &= ~(EXYNOS_TMU_BUF_SLOPE_SEL_MASK << EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT);
329 con |= (data->gain << EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT);
331 con &= ~(EXYNOS_TMU_TRIP_MODE_MASK << EXYNOS_TMU_TRIP_MODE_SHIFT);
332 con |= (EXYNOS_NOISE_CANCEL_MODE << EXYNOS_TMU_TRIP_MODE_SHIFT);
337 static void exynos_tmu_control(struct platform_device *pdev, bool on)
339 struct exynos_tmu_data *data = platform_get_drvdata(pdev);
341 mutex_lock(&data->lock);
342 clk_enable(data->clk);
343 data->tmu_control(pdev, on);
345 clk_disable(data->clk);
346 mutex_unlock(&data->lock);
349 static void exynos4210_tmu_set_trip_temp(struct exynos_tmu_data *data,
350 int trip_id, u8 temp)
352 struct thermal_trip trip;
355 if (thermal_zone_get_trip(data->tzd, 0, &trip))
358 ref = trip.temperature / MCELSIUS;
361 th_code = temp_to_code(data, ref);
362 writeb(th_code, data->base + EXYNOS4210_TMU_REG_THRESHOLD_TEMP);
366 writeb(temp, data->base + EXYNOS4210_TMU_REG_TRIG_LEVEL0 + trip_id * 4);
369 /* failing thresholds are not supported on Exynos4210 */
370 static void exynos4210_tmu_set_trip_hyst(struct exynos_tmu_data *data,
371 int trip, u8 temp, u8 hyst)
375 static void exynos4210_tmu_initialize(struct platform_device *pdev)
377 struct exynos_tmu_data *data = platform_get_drvdata(pdev);
379 sanitize_temp_error(data, readl(data->base + EXYNOS_TMU_REG_TRIMINFO));
382 static void exynos4412_tmu_set_trip_temp(struct exynos_tmu_data *data,
387 th = readl(data->base + EXYNOS_THD_TEMP_RISE);
388 th &= ~(0xff << 8 * trip);
389 th |= temp_to_code(data, temp) << 8 * trip;
390 writel(th, data->base + EXYNOS_THD_TEMP_RISE);
393 con = readl(data->base + EXYNOS_TMU_REG_CONTROL);
394 con |= (1 << EXYNOS_TMU_THERM_TRIP_EN_SHIFT);
395 writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
399 static void exynos4412_tmu_set_trip_hyst(struct exynos_tmu_data *data,
400 int trip, u8 temp, u8 hyst)
404 th = readl(data->base + EXYNOS_THD_TEMP_FALL);
405 th &= ~(0xff << 8 * trip);
407 th |= temp_to_code(data, temp - hyst) << 8 * trip;
408 writel(th, data->base + EXYNOS_THD_TEMP_FALL);
411 static void exynos4412_tmu_initialize(struct platform_device *pdev)
413 struct exynos_tmu_data *data = platform_get_drvdata(pdev);
414 unsigned int trim_info, ctrl;
416 if (data->soc == SOC_ARCH_EXYNOS3250 ||
417 data->soc == SOC_ARCH_EXYNOS4412 ||
418 data->soc == SOC_ARCH_EXYNOS5250) {
419 if (data->soc == SOC_ARCH_EXYNOS3250) {
420 ctrl = readl(data->base + EXYNOS_TMU_TRIMINFO_CON1);
421 ctrl |= EXYNOS_TRIMINFO_RELOAD_ENABLE;
422 writel(ctrl, data->base + EXYNOS_TMU_TRIMINFO_CON1);
424 ctrl = readl(data->base + EXYNOS_TMU_TRIMINFO_CON2);
425 ctrl |= EXYNOS_TRIMINFO_RELOAD_ENABLE;
426 writel(ctrl, data->base + EXYNOS_TMU_TRIMINFO_CON2);
429 /* On exynos5420 the triminfo register is in the shared space */
430 if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO)
431 trim_info = readl(data->base_second + EXYNOS_TMU_REG_TRIMINFO);
433 trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO);
435 sanitize_temp_error(data, trim_info);
438 static void exynos5433_tmu_set_trip_temp(struct exynos_tmu_data *data,
441 unsigned int reg_off, j;
445 reg_off = EXYNOS5433_THD_TEMP_RISE7_4;
448 reg_off = EXYNOS5433_THD_TEMP_RISE3_0;
452 th = readl(data->base + reg_off);
453 th &= ~(0xff << j * 8);
454 th |= (temp_to_code(data, temp) << j * 8);
455 writel(th, data->base + reg_off);
458 static void exynos5433_tmu_set_trip_hyst(struct exynos_tmu_data *data,
459 int trip, u8 temp, u8 hyst)
461 unsigned int reg_off, j;
465 reg_off = EXYNOS5433_THD_TEMP_FALL7_4;
468 reg_off = EXYNOS5433_THD_TEMP_FALL3_0;
472 th = readl(data->base + reg_off);
473 th &= ~(0xff << j * 8);
474 th |= (temp_to_code(data, temp - hyst) << j * 8);
475 writel(th, data->base + reg_off);
478 static void exynos5433_tmu_initialize(struct platform_device *pdev)
480 struct exynos_tmu_data *data = platform_get_drvdata(pdev);
481 unsigned int trim_info;
482 int sensor_id, cal_type;
484 trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO);
485 sanitize_temp_error(data, trim_info);
487 /* Read the temperature sensor id */
488 sensor_id = (trim_info & EXYNOS5433_TRIMINFO_SENSOR_ID_MASK)
489 >> EXYNOS5433_TRIMINFO_SENSOR_ID_SHIFT;
490 dev_info(&pdev->dev, "Temperature sensor ID: 0x%x\n", sensor_id);
492 /* Read the calibration mode */
493 writel(trim_info, data->base + EXYNOS_TMU_REG_TRIMINFO);
494 cal_type = (trim_info & EXYNOS5433_TRIMINFO_CALIB_SEL_MASK)
495 >> EXYNOS5433_TRIMINFO_CALIB_SEL_SHIFT;
498 case EXYNOS5433_TRIMINFO_TWO_POINT_TRIMMING:
499 data->cal_type = TYPE_TWO_POINT_TRIMMING;
501 case EXYNOS5433_TRIMINFO_ONE_POINT_TRIMMING:
503 data->cal_type = TYPE_ONE_POINT_TRIMMING;
507 dev_info(&pdev->dev, "Calibration type is %d-point calibration\n",
511 static void exynos7_tmu_set_trip_temp(struct exynos_tmu_data *data,
514 unsigned int reg_off, bit_off;
517 reg_off = ((7 - trip) / 2) * 4;
518 bit_off = ((8 - trip) % 2);
520 th = readl(data->base + EXYNOS7_THD_TEMP_RISE7_6 + reg_off);
521 th &= ~(EXYNOS7_TMU_TEMP_MASK << (16 * bit_off));
522 th |= temp_to_code(data, temp) << (16 * bit_off);
523 writel(th, data->base + EXYNOS7_THD_TEMP_RISE7_6 + reg_off);
526 static void exynos7_tmu_set_trip_hyst(struct exynos_tmu_data *data,
527 int trip, u8 temp, u8 hyst)
529 unsigned int reg_off, bit_off;
532 reg_off = ((7 - trip) / 2) * 4;
533 bit_off = ((8 - trip) % 2);
535 th = readl(data->base + EXYNOS7_THD_TEMP_FALL7_6 + reg_off);
536 th &= ~(EXYNOS7_TMU_TEMP_MASK << (16 * bit_off));
537 th |= temp_to_code(data, temp - hyst) << (16 * bit_off);
538 writel(th, data->base + EXYNOS7_THD_TEMP_FALL7_6 + reg_off);
541 static void exynos7_tmu_initialize(struct platform_device *pdev)
543 struct exynos_tmu_data *data = platform_get_drvdata(pdev);
544 unsigned int trim_info;
546 trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO);
547 sanitize_temp_error(data, trim_info);
550 static void exynos4210_tmu_control(struct platform_device *pdev, bool on)
552 struct exynos_tmu_data *data = platform_get_drvdata(pdev);
553 struct thermal_zone_device *tz = data->tzd;
554 struct thermal_trip trip;
555 unsigned int con, interrupt_en = 0, i;
557 con = get_con_reg(data, readl(data->base + EXYNOS_TMU_REG_CONTROL));
560 for (i = 0; i < data->ntrip; i++) {
561 if (thermal_zone_get_trip(tz, i, &trip))
565 (1 << (EXYNOS_TMU_INTEN_RISE0_SHIFT + i * 4));
568 if (data->soc != SOC_ARCH_EXYNOS4210)
570 interrupt_en << EXYNOS_TMU_INTEN_FALL0_SHIFT;
572 con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT);
574 con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT);
577 writel(interrupt_en, data->base + EXYNOS_TMU_REG_INTEN);
578 writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
581 static void exynos5433_tmu_control(struct platform_device *pdev, bool on)
583 struct exynos_tmu_data *data = platform_get_drvdata(pdev);
584 struct thermal_zone_device *tz = data->tzd;
585 struct thermal_trip trip;
586 unsigned int con, interrupt_en = 0, pd_det_en, i;
588 con = get_con_reg(data, readl(data->base + EXYNOS_TMU_REG_CONTROL));
591 for (i = 0; i < data->ntrip; i++) {
592 if (thermal_zone_get_trip(tz, i, &trip))
596 (1 << (EXYNOS7_TMU_INTEN_RISE0_SHIFT + i));
600 interrupt_en << EXYNOS_TMU_INTEN_FALL0_SHIFT;
602 con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT);
604 con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT);
606 pd_det_en = on ? EXYNOS5433_PD_DET_EN : 0;
608 writel(pd_det_en, data->base + EXYNOS5433_TMU_PD_DET_EN);
609 writel(interrupt_en, data->base + EXYNOS5433_TMU_REG_INTEN);
610 writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
613 static void exynos7_tmu_control(struct platform_device *pdev, bool on)
615 struct exynos_tmu_data *data = platform_get_drvdata(pdev);
616 struct thermal_zone_device *tz = data->tzd;
617 struct thermal_trip trip;
618 unsigned int con, interrupt_en = 0, i;
620 con = get_con_reg(data, readl(data->base + EXYNOS_TMU_REG_CONTROL));
623 for (i = 0; i < data->ntrip; i++) {
624 if (thermal_zone_get_trip(tz, i, &trip))
628 (1 << (EXYNOS7_TMU_INTEN_RISE0_SHIFT + i));
632 interrupt_en << EXYNOS_TMU_INTEN_FALL0_SHIFT;
634 con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT);
635 con |= (1 << EXYNOS7_PD_DET_EN_SHIFT);
637 con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT);
638 con &= ~(1 << EXYNOS7_PD_DET_EN_SHIFT);
641 writel(interrupt_en, data->base + EXYNOS7_TMU_REG_INTEN);
642 writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
645 static int exynos_get_temp(struct thermal_zone_device *tz, int *temp)
647 struct exynos_tmu_data *data = thermal_zone_device_priv(tz);
650 if (!data || !data->tmu_read)
652 else if (!data->enabled)
654 * Called too early, probably
655 * from thermal_zone_of_sensor_register().
659 mutex_lock(&data->lock);
660 clk_enable(data->clk);
662 value = data->tmu_read(data);
666 *temp = code_to_temp(data, value) * MCELSIUS;
668 clk_disable(data->clk);
669 mutex_unlock(&data->lock);
674 #ifdef CONFIG_THERMAL_EMULATION
675 static u32 get_emul_con_reg(struct exynos_tmu_data *data, unsigned int val,
681 val &= ~(EXYNOS_EMUL_TIME_MASK << EXYNOS_EMUL_TIME_SHIFT);
682 val |= (EXYNOS_EMUL_TIME << EXYNOS_EMUL_TIME_SHIFT);
683 if (data->soc == SOC_ARCH_EXYNOS7) {
684 val &= ~(EXYNOS7_EMUL_DATA_MASK <<
685 EXYNOS7_EMUL_DATA_SHIFT);
686 val |= (temp_to_code(data, temp) <<
687 EXYNOS7_EMUL_DATA_SHIFT) |
690 val &= ~(EXYNOS_EMUL_DATA_MASK <<
691 EXYNOS_EMUL_DATA_SHIFT);
692 val |= (temp_to_code(data, temp) <<
693 EXYNOS_EMUL_DATA_SHIFT) |
697 val &= ~EXYNOS_EMUL_ENABLE;
703 static void exynos4412_tmu_set_emulation(struct exynos_tmu_data *data,
709 if (data->soc == SOC_ARCH_EXYNOS5260)
710 emul_con = EXYNOS5260_EMUL_CON;
711 else if (data->soc == SOC_ARCH_EXYNOS5433)
712 emul_con = EXYNOS5433_TMU_EMUL_CON;
713 else if (data->soc == SOC_ARCH_EXYNOS7)
714 emul_con = EXYNOS7_TMU_REG_EMUL_CON;
716 emul_con = EXYNOS_EMUL_CON;
718 val = readl(data->base + emul_con);
719 val = get_emul_con_reg(data, val, temp);
720 writel(val, data->base + emul_con);
723 static int exynos_tmu_set_emulation(struct thermal_zone_device *tz, int temp)
725 struct exynos_tmu_data *data = thermal_zone_device_priv(tz);
728 if (data->soc == SOC_ARCH_EXYNOS4210)
731 if (temp && temp < MCELSIUS)
734 mutex_lock(&data->lock);
735 clk_enable(data->clk);
736 data->tmu_set_emulation(data, temp);
737 clk_disable(data->clk);
738 mutex_unlock(&data->lock);
744 #define exynos4412_tmu_set_emulation NULL
745 static int exynos_tmu_set_emulation(struct thermal_zone_device *tz, int temp)
747 #endif /* CONFIG_THERMAL_EMULATION */
749 static int exynos4210_tmu_read(struct exynos_tmu_data *data)
751 int ret = readb(data->base + EXYNOS_TMU_REG_CURRENT_TEMP);
753 /* "temp_code" should range between 75 and 175 */
754 return (ret < 75 || ret > 175) ? -ENODATA : ret;
757 static int exynos4412_tmu_read(struct exynos_tmu_data *data)
759 return readb(data->base + EXYNOS_TMU_REG_CURRENT_TEMP);
762 static int exynos7_tmu_read(struct exynos_tmu_data *data)
764 return readw(data->base + EXYNOS_TMU_REG_CURRENT_TEMP) &
765 EXYNOS7_TMU_TEMP_MASK;
768 static void exynos_tmu_work(struct work_struct *work)
770 struct exynos_tmu_data *data = container_of(work,
771 struct exynos_tmu_data, irq_work);
773 thermal_zone_device_update(data->tzd, THERMAL_EVENT_UNSPECIFIED);
775 mutex_lock(&data->lock);
776 clk_enable(data->clk);
778 /* TODO: take action based on particular interrupt */
779 data->tmu_clear_irqs(data);
781 clk_disable(data->clk);
782 mutex_unlock(&data->lock);
783 enable_irq(data->irq);
786 static void exynos4210_tmu_clear_irqs(struct exynos_tmu_data *data)
788 unsigned int val_irq;
789 u32 tmu_intstat, tmu_intclear;
791 if (data->soc == SOC_ARCH_EXYNOS5260) {
792 tmu_intstat = EXYNOS5260_TMU_REG_INTSTAT;
793 tmu_intclear = EXYNOS5260_TMU_REG_INTCLEAR;
794 } else if (data->soc == SOC_ARCH_EXYNOS7) {
795 tmu_intstat = EXYNOS7_TMU_REG_INTPEND;
796 tmu_intclear = EXYNOS7_TMU_REG_INTPEND;
797 } else if (data->soc == SOC_ARCH_EXYNOS5433) {
798 tmu_intstat = EXYNOS5433_TMU_REG_INTPEND;
799 tmu_intclear = EXYNOS5433_TMU_REG_INTPEND;
801 tmu_intstat = EXYNOS_TMU_REG_INTSTAT;
802 tmu_intclear = EXYNOS_TMU_REG_INTCLEAR;
805 val_irq = readl(data->base + tmu_intstat);
807 * Clear the interrupts. Please note that the documentation for
808 * Exynos3250, Exynos4412, Exynos5250 and Exynos5260 incorrectly
809 * states that INTCLEAR register has a different placing of bits
810 * responsible for FALL IRQs than INTSTAT register. Exynos5420
811 * and Exynos5440 documentation is correct (Exynos4210 doesn't
812 * support FALL IRQs at all).
814 writel(val_irq, data->base + tmu_intclear);
817 static irqreturn_t exynos_tmu_irq(int irq, void *id)
819 struct exynos_tmu_data *data = id;
821 disable_irq_nosync(irq);
822 schedule_work(&data->irq_work);
827 static const struct of_device_id exynos_tmu_match[] = {
829 .compatible = "samsung,exynos3250-tmu",
830 .data = (const void *)SOC_ARCH_EXYNOS3250,
832 .compatible = "samsung,exynos4210-tmu",
833 .data = (const void *)SOC_ARCH_EXYNOS4210,
835 .compatible = "samsung,exynos4412-tmu",
836 .data = (const void *)SOC_ARCH_EXYNOS4412,
838 .compatible = "samsung,exynos5250-tmu",
839 .data = (const void *)SOC_ARCH_EXYNOS5250,
841 .compatible = "samsung,exynos5260-tmu",
842 .data = (const void *)SOC_ARCH_EXYNOS5260,
844 .compatible = "samsung,exynos5420-tmu",
845 .data = (const void *)SOC_ARCH_EXYNOS5420,
847 .compatible = "samsung,exynos5420-tmu-ext-triminfo",
848 .data = (const void *)SOC_ARCH_EXYNOS5420_TRIMINFO,
850 .compatible = "samsung,exynos5433-tmu",
851 .data = (const void *)SOC_ARCH_EXYNOS5433,
853 .compatible = "samsung,exynos7-tmu",
854 .data = (const void *)SOC_ARCH_EXYNOS7,
858 MODULE_DEVICE_TABLE(of, exynos_tmu_match);
860 static int exynos_map_dt_data(struct platform_device *pdev)
862 struct exynos_tmu_data *data = platform_get_drvdata(pdev);
865 if (!data || !pdev->dev.of_node)
868 data->id = of_alias_get_id(pdev->dev.of_node, "tmuctrl");
872 data->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
873 if (data->irq <= 0) {
874 dev_err(&pdev->dev, "failed to get IRQ\n");
878 if (of_address_to_resource(pdev->dev.of_node, 0, &res)) {
879 dev_err(&pdev->dev, "failed to get Resource 0\n");
883 data->base = devm_ioremap(&pdev->dev, res.start, resource_size(&res));
885 dev_err(&pdev->dev, "Failed to ioremap memory\n");
886 return -EADDRNOTAVAIL;
889 data->soc = (uintptr_t)of_device_get_match_data(&pdev->dev);
892 case SOC_ARCH_EXYNOS4210:
893 data->tmu_set_trip_temp = exynos4210_tmu_set_trip_temp;
894 data->tmu_set_trip_hyst = exynos4210_tmu_set_trip_hyst;
895 data->tmu_initialize = exynos4210_tmu_initialize;
896 data->tmu_control = exynos4210_tmu_control;
897 data->tmu_read = exynos4210_tmu_read;
898 data->tmu_clear_irqs = exynos4210_tmu_clear_irqs;
901 data->reference_voltage = 7;
902 data->efuse_value = 55;
903 data->min_efuse_value = 40;
904 data->max_efuse_value = 100;
906 case SOC_ARCH_EXYNOS3250:
907 case SOC_ARCH_EXYNOS4412:
908 case SOC_ARCH_EXYNOS5250:
909 case SOC_ARCH_EXYNOS5260:
910 case SOC_ARCH_EXYNOS5420:
911 case SOC_ARCH_EXYNOS5420_TRIMINFO:
912 data->tmu_set_trip_temp = exynos4412_tmu_set_trip_temp;
913 data->tmu_set_trip_hyst = exynos4412_tmu_set_trip_hyst;
914 data->tmu_initialize = exynos4412_tmu_initialize;
915 data->tmu_control = exynos4210_tmu_control;
916 data->tmu_read = exynos4412_tmu_read;
917 data->tmu_set_emulation = exynos4412_tmu_set_emulation;
918 data->tmu_clear_irqs = exynos4210_tmu_clear_irqs;
921 data->reference_voltage = 16;
922 data->efuse_value = 55;
923 if (data->soc != SOC_ARCH_EXYNOS5420 &&
924 data->soc != SOC_ARCH_EXYNOS5420_TRIMINFO)
925 data->min_efuse_value = 40;
927 data->min_efuse_value = 0;
928 data->max_efuse_value = 100;
930 case SOC_ARCH_EXYNOS5433:
931 data->tmu_set_trip_temp = exynos5433_tmu_set_trip_temp;
932 data->tmu_set_trip_hyst = exynos5433_tmu_set_trip_hyst;
933 data->tmu_initialize = exynos5433_tmu_initialize;
934 data->tmu_control = exynos5433_tmu_control;
935 data->tmu_read = exynos4412_tmu_read;
936 data->tmu_set_emulation = exynos4412_tmu_set_emulation;
937 data->tmu_clear_irqs = exynos4210_tmu_clear_irqs;
940 if (res.start == EXYNOS5433_G3D_BASE)
941 data->reference_voltage = 23;
943 data->reference_voltage = 16;
944 data->efuse_value = 75;
945 data->min_efuse_value = 40;
946 data->max_efuse_value = 150;
948 case SOC_ARCH_EXYNOS7:
949 data->tmu_set_trip_temp = exynos7_tmu_set_trip_temp;
950 data->tmu_set_trip_hyst = exynos7_tmu_set_trip_hyst;
951 data->tmu_initialize = exynos7_tmu_initialize;
952 data->tmu_control = exynos7_tmu_control;
953 data->tmu_read = exynos7_tmu_read;
954 data->tmu_set_emulation = exynos4412_tmu_set_emulation;
955 data->tmu_clear_irqs = exynos4210_tmu_clear_irqs;
958 data->reference_voltage = 17;
959 data->efuse_value = 75;
960 data->min_efuse_value = 15;
961 data->max_efuse_value = 100;
964 dev_err(&pdev->dev, "Platform not supported\n");
968 data->cal_type = TYPE_ONE_POINT_TRIMMING;
971 * Check if the TMU shares some registers and then try to map the
972 * memory of common registers.
974 if (data->soc != SOC_ARCH_EXYNOS5420_TRIMINFO)
977 if (of_address_to_resource(pdev->dev.of_node, 1, &res)) {
978 dev_err(&pdev->dev, "failed to get Resource 1\n");
982 data->base_second = devm_ioremap(&pdev->dev, res.start,
983 resource_size(&res));
984 if (!data->base_second) {
985 dev_err(&pdev->dev, "Failed to ioremap memory\n");
992 static const struct thermal_zone_device_ops exynos_sensor_ops = {
993 .get_temp = exynos_get_temp,
994 .set_emul_temp = exynos_tmu_set_emulation,
997 static int exynos_tmu_probe(struct platform_device *pdev)
999 struct exynos_tmu_data *data;
1002 data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data),
1007 platform_set_drvdata(pdev, data);
1008 mutex_init(&data->lock);
1011 * Try enabling the regulator if found
1012 * TODO: Add regulator as an SOC feature, so that regulator enable
1013 * is a compulsory call.
1015 data->regulator = devm_regulator_get_optional(&pdev->dev, "vtmu");
1016 if (!IS_ERR(data->regulator)) {
1017 ret = regulator_enable(data->regulator);
1019 dev_err(&pdev->dev, "failed to enable vtmu\n");
1023 if (PTR_ERR(data->regulator) == -EPROBE_DEFER)
1024 return -EPROBE_DEFER;
1025 dev_info(&pdev->dev, "Regulator node (vtmu) not found\n");
1028 ret = exynos_map_dt_data(pdev);
1032 INIT_WORK(&data->irq_work, exynos_tmu_work);
1034 data->clk = devm_clk_get(&pdev->dev, "tmu_apbif");
1035 if (IS_ERR(data->clk)) {
1036 dev_err(&pdev->dev, "Failed to get clock\n");
1037 ret = PTR_ERR(data->clk);
1041 data->clk_sec = devm_clk_get(&pdev->dev, "tmu_triminfo_apbif");
1042 if (IS_ERR(data->clk_sec)) {
1043 if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO) {
1044 dev_err(&pdev->dev, "Failed to get triminfo clock\n");
1045 ret = PTR_ERR(data->clk_sec);
1049 ret = clk_prepare(data->clk_sec);
1051 dev_err(&pdev->dev, "Failed to get clock\n");
1056 ret = clk_prepare(data->clk);
1058 dev_err(&pdev->dev, "Failed to get clock\n");
1062 switch (data->soc) {
1063 case SOC_ARCH_EXYNOS5433:
1064 case SOC_ARCH_EXYNOS7:
1065 data->sclk = devm_clk_get(&pdev->dev, "tmu_sclk");
1066 if (IS_ERR(data->sclk)) {
1067 dev_err(&pdev->dev, "Failed to get sclk\n");
1068 ret = PTR_ERR(data->sclk);
1071 ret = clk_prepare_enable(data->sclk);
1073 dev_err(&pdev->dev, "Failed to enable sclk\n");
1083 * data->tzd must be registered before calling exynos_tmu_initialize(),
1084 * requesting irq and calling exynos_tmu_control().
1086 data->tzd = devm_thermal_of_zone_register(&pdev->dev, 0, data,
1087 &exynos_sensor_ops);
1088 if (IS_ERR(data->tzd)) {
1089 ret = PTR_ERR(data->tzd);
1090 if (ret != -EPROBE_DEFER)
1091 dev_err(&pdev->dev, "Failed to register sensor: %d\n",
1096 ret = exynos_tmu_initialize(pdev);
1098 dev_err(&pdev->dev, "Failed to initialize TMU\n");
1102 ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq,
1103 IRQF_TRIGGER_RISING | IRQF_SHARED, dev_name(&pdev->dev), data);
1105 dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq);
1109 exynos_tmu_control(pdev, true);
1113 clk_disable_unprepare(data->sclk);
1115 clk_unprepare(data->clk);
1117 if (!IS_ERR(data->clk_sec))
1118 clk_unprepare(data->clk_sec);
1120 if (!IS_ERR(data->regulator))
1121 regulator_disable(data->regulator);
1126 static void exynos_tmu_remove(struct platform_device *pdev)
1128 struct exynos_tmu_data *data = platform_get_drvdata(pdev);
1130 exynos_tmu_control(pdev, false);
1132 clk_disable_unprepare(data->sclk);
1133 clk_unprepare(data->clk);
1134 if (!IS_ERR(data->clk_sec))
1135 clk_unprepare(data->clk_sec);
1137 if (!IS_ERR(data->regulator))
1138 regulator_disable(data->regulator);
1141 #ifdef CONFIG_PM_SLEEP
1142 static int exynos_tmu_suspend(struct device *dev)
1144 exynos_tmu_control(to_platform_device(dev), false);
1149 static int exynos_tmu_resume(struct device *dev)
1151 struct platform_device *pdev = to_platform_device(dev);
1153 exynos_tmu_initialize(pdev);
1154 exynos_tmu_control(pdev, true);
1159 static SIMPLE_DEV_PM_OPS(exynos_tmu_pm,
1160 exynos_tmu_suspend, exynos_tmu_resume);
1161 #define EXYNOS_TMU_PM (&exynos_tmu_pm)
1163 #define EXYNOS_TMU_PM NULL
1166 static struct platform_driver exynos_tmu_driver = {
1168 .name = "exynos-tmu",
1169 .pm = EXYNOS_TMU_PM,
1170 .of_match_table = exynos_tmu_match,
1172 .probe = exynos_tmu_probe,
1173 .remove_new = exynos_tmu_remove,
1176 module_platform_driver(exynos_tmu_driver);
1178 MODULE_DESCRIPTION("Exynos TMU Driver");
1179 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
1180 MODULE_LICENSE("GPL");
1181 MODULE_ALIAS("platform:exynos-tmu");