blob: 33f494ea1ed12925d6c4c49860bb93677e8f3d4c [file] [log] [blame]
Donggeun Kim9d97e5c2011-09-07 18:49:08 +09001/*
Amit Daniel Kachhap59dfa542013-06-24 16:20:26 +05302 * exynos_tmu.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
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090024#include <linux/clk.h>
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090025#include <linux/io.h>
Amit Daniel Kachhap1b678642013-06-24 16:20:25 +053026#include <linux/interrupt.h>
27#include <linux/module.h>
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +053028#include <linux/of.h>
Amit Daniel Kachhap1b678642013-06-24 16:20:25 +053029#include <linux/platform_device.h>
Amit Daniel Kachhap1b678642013-06-24 16:20:25 +053030
31#include "exynos_thermal_common.h"
Amit Daniel Kachhap0c1836a2013-06-24 16:20:27 +053032#include "exynos_tmu.h"
Amit Daniel Kachhape6b79912013-06-24 16:20:28 +053033#include "exynos_tmu_data.h"
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090034
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +053035struct exynos_tmu_data {
36 struct exynos_tmu_platform_data *pdata;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090037 struct resource *mem;
38 void __iomem *base;
39 int irq;
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +053040 enum soc_type soc;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090041 struct work_struct irq_work;
42 struct mutex lock;
43 struct clk *clk;
44 u8 temp_error1, temp_error2;
45};
46
47/*
48 * TMU treats temperature as a mapped temperature code.
49 * The temperature is converted differently depending on the calibration type.
50 */
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +053051static int temp_to_code(struct exynos_tmu_data *data, u8 temp)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090052{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +053053 struct exynos_tmu_platform_data *pdata = data->pdata;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090054 int temp_code;
55
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +053056 if (data->soc == SOC_ARCH_EXYNOS4210)
57 /* temp should range between 25 and 125 */
58 if (temp < 25 || temp > 125) {
59 temp_code = -EINVAL;
60 goto out;
61 }
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090062
63 switch (pdata->cal_type) {
64 case TYPE_TWO_POINT_TRIMMING:
Amit Daniel Kachhapbb34b4c2013-06-24 16:20:30 +053065 temp_code = (temp - pdata->first_point_trim) *
66 (data->temp_error2 - data->temp_error1) /
67 (pdata->second_point_trim - pdata->first_point_trim) +
68 data->temp_error1;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090069 break;
70 case TYPE_ONE_POINT_TRIMMING:
Amit Daniel Kachhapbb34b4c2013-06-24 16:20:30 +053071 temp_code = temp + data->temp_error1 - pdata->first_point_trim;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090072 break;
73 default:
Amit Daniel Kachhapbb34b4c2013-06-24 16:20:30 +053074 temp_code = temp + pdata->default_temp_offset;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090075 break;
76 }
77out:
78 return temp_code;
79}
80
81/*
82 * Calculate a temperature value from a temperature code.
83 * The unit of the temperature is degree Celsius.
84 */
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +053085static int code_to_temp(struct exynos_tmu_data *data, u8 temp_code)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090086{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +053087 struct exynos_tmu_platform_data *pdata = data->pdata;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090088 int temp;
89
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +053090 if (data->soc == SOC_ARCH_EXYNOS4210)
91 /* temp_code should range between 75 and 175 */
92 if (temp_code < 75 || temp_code > 175) {
93 temp = -ENODATA;
94 goto out;
95 }
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090096
97 switch (pdata->cal_type) {
98 case TYPE_TWO_POINT_TRIMMING:
Amit Daniel Kachhapbb34b4c2013-06-24 16:20:30 +053099 temp = (temp_code - data->temp_error1) *
100 (pdata->second_point_trim - pdata->first_point_trim) /
101 (data->temp_error2 - data->temp_error1) +
102 pdata->first_point_trim;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900103 break;
104 case TYPE_ONE_POINT_TRIMMING:
Amit Daniel Kachhapbb34b4c2013-06-24 16:20:30 +0530105 temp = temp_code - data->temp_error1 + pdata->first_point_trim;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900106 break;
107 default:
Amit Daniel Kachhapbb34b4c2013-06-24 16:20:30 +0530108 temp = temp_code - pdata->default_temp_offset;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900109 break;
110 }
111out:
112 return temp;
113}
114
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530115static int exynos_tmu_initialize(struct platform_device *pdev)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900116{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530117 struct exynos_tmu_data *data = platform_get_drvdata(pdev);
118 struct exynos_tmu_platform_data *pdata = data->pdata;
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530119 const struct exynos_tmu_registers *reg = pdata->registers;
Amit Daniel Kachhap7ca04e52013-06-24 16:20:32 +0530120 unsigned int status, trim_info = 0, con;
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000121 unsigned int rising_threshold = 0, falling_threshold = 0;
122 int ret = 0, threshold_code, i, trigger_levs = 0;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900123
124 mutex_lock(&data->lock);
125 clk_enable(data->clk);
126
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530127 status = readb(data->base + reg->tmu_status);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900128 if (!status) {
129 ret = -EBUSY;
130 goto out;
131 }
132
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530133 if (data->soc == SOC_ARCH_EXYNOS)
134 __raw_writel(1, data->base + reg->triminfo_ctrl);
135
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530136 /* Save trimming info in order to perform calibration */
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530137 trim_info = readl(data->base + reg->triminfo_data);
138 data->temp_error1 = trim_info & EXYNOS_TMU_TEMP_MASK;
139 data->temp_error2 = ((trim_info >> reg->triminfo_85_shift) &
140 EXYNOS_TMU_TEMP_MASK);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900141
Amit Daniel Kachhapbb34b4c2013-06-24 16:20:30 +0530142 if ((pdata->min_efuse_value > data->temp_error1) ||
143 (data->temp_error1 > pdata->max_efuse_value) ||
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530144 (data->temp_error2 != 0))
145 data->temp_error1 = pdata->efuse_value;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900146
Amit Daniel Kachhap7ca04e52013-06-24 16:20:32 +0530147 if (pdata->max_trigger_level > MAX_THRESHOLD_LEVS) {
148 dev_err(&pdev->dev, "Invalid max trigger level\n");
149 goto out;
150 }
151
152 for (i = 0; i < pdata->max_trigger_level; i++) {
153 if (!pdata->trigger_levels[i])
154 continue;
155
156 if ((pdata->trigger_type[i] == HW_TRIP) &&
157 (!pdata->trigger_levels[pdata->max_trigger_level - 1])) {
158 dev_err(&pdev->dev, "Invalid hw trigger level\n");
159 ret = -EINVAL;
160 goto out;
161 }
162
163 /* Count trigger levels except the HW trip*/
164 if (!(pdata->trigger_type[i] == HW_TRIP))
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000165 trigger_levs++;
Amit Daniel Kachhap7ca04e52013-06-24 16:20:32 +0530166 }
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000167
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530168 if (data->soc == SOC_ARCH_EXYNOS4210) {
169 /* Write temperature code for threshold */
170 threshold_code = temp_to_code(data, pdata->threshold);
171 if (threshold_code < 0) {
172 ret = threshold_code;
173 goto out;
174 }
175 writeb(threshold_code,
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530176 data->base + reg->threshold_temp);
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000177 for (i = 0; i < trigger_levs; i++)
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530178 writeb(pdata->trigger_levels[i], data->base +
179 reg->threshold_th0 + i * sizeof(reg->threshold_th0));
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530180
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530181 writel(reg->inten_rise_mask, data->base + reg->tmu_intclear);
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530182 } else if (data->soc == SOC_ARCH_EXYNOS) {
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000183 /* Write temperature code for rising and falling threshold */
Amit Daniel Kachhap7ca04e52013-06-24 16:20:32 +0530184 for (i = 0;
185 i < trigger_levs && i < EXYNOS_MAX_TRIGGER_PER_REG; i++) {
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000186 threshold_code = temp_to_code(data,
187 pdata->trigger_levels[i]);
188 if (threshold_code < 0) {
189 ret = threshold_code;
190 goto out;
191 }
192 rising_threshold |= threshold_code << 8 * i;
193 if (pdata->threshold_falling) {
194 threshold_code = temp_to_code(data,
195 pdata->trigger_levels[i] -
196 pdata->threshold_falling);
197 if (threshold_code > 0)
198 falling_threshold |=
199 threshold_code << 8 * i;
200 }
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530201 }
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530202
203 writel(rising_threshold,
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530204 data->base + reg->threshold_th0);
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000205 writel(falling_threshold,
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530206 data->base + reg->threshold_th1);
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530207
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530208 writel((reg->inten_rise_mask << reg->inten_rise_shift) |
209 (reg->inten_fall_mask << reg->inten_fall_shift),
210 data->base + reg->tmu_intclear);
Amit Daniel Kachhap7ca04e52013-06-24 16:20:32 +0530211
212 /* if last threshold limit is also present */
213 i = pdata->max_trigger_level - 1;
214 if (pdata->trigger_levels[i] &&
215 (pdata->trigger_type[i] == HW_TRIP)) {
216 threshold_code = temp_to_code(data,
217 pdata->trigger_levels[i]);
218 if (threshold_code < 0) {
219 ret = threshold_code;
220 goto out;
221 }
222 rising_threshold |= threshold_code << 8 * i;
223 writel(rising_threshold,
224 data->base + reg->threshold_th0);
225 con = readl(data->base + reg->tmu_ctrl);
226 con |= (1 << reg->therm_trip_en_shift);
227 writel(con, data->base + reg->tmu_ctrl);
228 }
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530229 }
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900230out:
231 clk_disable(data->clk);
232 mutex_unlock(&data->lock);
233
234 return ret;
235}
236
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530237static void exynos_tmu_control(struct platform_device *pdev, bool on)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900238{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530239 struct exynos_tmu_data *data = platform_get_drvdata(pdev);
240 struct exynos_tmu_platform_data *pdata = data->pdata;
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530241 const struct exynos_tmu_registers *reg = pdata->registers;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900242 unsigned int con, interrupt_en;
243
244 mutex_lock(&data->lock);
245 clk_enable(data->clk);
246
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530247 con = readl(data->base + reg->tmu_ctrl);
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530248
Amit Daniel Kachhapd0a0ce32013-06-24 16:20:29 +0530249 if (pdata->reference_voltage) {
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530250 con &= ~(reg->buf_vref_sel_mask << reg->buf_vref_sel_shift);
251 con |= pdata->reference_voltage << reg->buf_vref_sel_shift;
Amit Daniel Kachhapd0a0ce32013-06-24 16:20:29 +0530252 }
253
254 if (pdata->gain) {
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530255 con &= ~(reg->buf_slope_sel_mask << reg->buf_slope_sel_shift);
256 con |= (pdata->gain << reg->buf_slope_sel_shift);
Amit Daniel Kachhapd0a0ce32013-06-24 16:20:29 +0530257 }
258
259 if (pdata->noise_cancel_mode) {
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530260 con &= ~(reg->therm_trip_mode_mask <<
261 reg->therm_trip_mode_shift);
262 con |= (pdata->noise_cancel_mode << reg->therm_trip_mode_shift);
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530263 }
264
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900265 if (on) {
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530266 con |= (1 << reg->core_en_shift);
Amit Daniel Kachhapd0a0ce32013-06-24 16:20:29 +0530267 interrupt_en =
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530268 pdata->trigger_enable[3] << reg->inten_rise3_shift |
269 pdata->trigger_enable[2] << reg->inten_rise2_shift |
270 pdata->trigger_enable[1] << reg->inten_rise1_shift |
271 pdata->trigger_enable[0] << reg->inten_rise0_shift;
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000272 if (pdata->threshold_falling)
Amit Daniel Kachhapd0a0ce32013-06-24 16:20:29 +0530273 interrupt_en |=
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530274 interrupt_en << reg->inten_fall0_shift;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900275 } else {
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530276 con &= ~(1 << reg->core_en_shift);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900277 interrupt_en = 0; /* Disable all interrupts */
278 }
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530279 writel(interrupt_en, data->base + reg->tmu_inten);
280 writel(con, data->base + reg->tmu_ctrl);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900281
282 clk_disable(data->clk);
283 mutex_unlock(&data->lock);
284}
285
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530286static int exynos_tmu_read(struct exynos_tmu_data *data)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900287{
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530288 struct exynos_tmu_platform_data *pdata = data->pdata;
289 const struct exynos_tmu_registers *reg = pdata->registers;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900290 u8 temp_code;
291 int temp;
292
293 mutex_lock(&data->lock);
294 clk_enable(data->clk);
295
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530296 temp_code = readb(data->base + reg->tmu_cur_temp);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900297 temp = code_to_temp(data, temp_code);
298
299 clk_disable(data->clk);
300 mutex_unlock(&data->lock);
301
302 return temp;
303}
304
Amit Daniel Kachhapbffd1f82013-02-11 03:54:23 +0000305#ifdef CONFIG_THERMAL_EMULATION
306static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp)
307{
308 struct exynos_tmu_data *data = drv_data;
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530309 struct exynos_tmu_platform_data *pdata = data->pdata;
310 const struct exynos_tmu_registers *reg = pdata->registers;
311 unsigned int val;
Amit Daniel Kachhapbffd1f82013-02-11 03:54:23 +0000312 int ret = -EINVAL;
313
314 if (data->soc == SOC_ARCH_EXYNOS4210)
315 goto out;
316
317 if (temp && temp < MCELSIUS)
318 goto out;
319
320 mutex_lock(&data->lock);
321 clk_enable(data->clk);
322
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530323 val = readl(data->base + reg->emul_con);
Amit Daniel Kachhapbffd1f82013-02-11 03:54:23 +0000324
325 if (temp) {
326 temp /= MCELSIUS;
327
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530328 val = (EXYNOS_EMUL_TIME << reg->emul_time_shift) |
Amit Daniel Kachhapbffd1f82013-02-11 03:54:23 +0000329 (temp_to_code(data, temp)
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530330 << reg->emul_temp_shift) | EXYNOS_EMUL_ENABLE;
Amit Daniel Kachhapbffd1f82013-02-11 03:54:23 +0000331 } else {
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530332 val &= ~EXYNOS_EMUL_ENABLE;
Amit Daniel Kachhapbffd1f82013-02-11 03:54:23 +0000333 }
334
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530335 writel(val, data->base + reg->emul_con);
Amit Daniel Kachhapbffd1f82013-02-11 03:54:23 +0000336
337 clk_disable(data->clk);
338 mutex_unlock(&data->lock);
339 return 0;
340out:
341 return ret;
342}
343#else
344static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp)
345 { return -EINVAL; }
346#endif/*CONFIG_THERMAL_EMULATION*/
347
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530348static void exynos_tmu_work(struct work_struct *work)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900349{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530350 struct exynos_tmu_data *data = container_of(work,
351 struct exynos_tmu_data, irq_work);
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530352 struct exynos_tmu_platform_data *pdata = data->pdata;
353 const struct exynos_tmu_registers *reg = pdata->registers;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900354
Amit Daniel Kachhap3ad95242013-01-17 01:42:18 +0000355 exynos_report_trigger();
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900356 mutex_lock(&data->lock);
357 clk_enable(data->clk);
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530358
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530359 if (data->soc == SOC_ARCH_EXYNOS)
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530360 writel((reg->inten_rise_mask << reg->inten_rise_shift) |
361 (reg->inten_fall_mask << reg->inten_fall_shift),
362 data->base + reg->tmu_intclear);
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530363 else
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530364 writel(reg->inten_rise_mask, data->base + reg->tmu_intclear);
365
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900366 clk_disable(data->clk);
367 mutex_unlock(&data->lock);
Amit Daniel Kachhap3ad95242013-01-17 01:42:18 +0000368
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530369 enable_irq(data->irq);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900370}
371
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530372static irqreturn_t exynos_tmu_irq(int irq, void *id)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900373{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530374 struct exynos_tmu_data *data = id;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900375
376 disable_irq_nosync(irq);
377 schedule_work(&data->irq_work);
378
379 return IRQ_HANDLED;
380}
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530381static struct thermal_sensor_conf exynos_sensor_conf = {
382 .name = "exynos-therm",
383 .read_temperature = (int (*)(void *))exynos_tmu_read,
Amit Daniel Kachhapbffd1f82013-02-11 03:54:23 +0000384 .write_emul_temp = exynos_tmu_set_emulation,
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530385};
386
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530387#ifdef CONFIG_OF
388static const struct of_device_id exynos_tmu_match[] = {
389 {
390 .compatible = "samsung,exynos4210-tmu",
391 .data = (void *)EXYNOS4210_TMU_DRV_DATA,
392 },
393 {
Sachin Kamatb6cee532013-04-18 11:37:59 +0000394 .compatible = "samsung,exynos4412-tmu",
Amit Daniel Kachhape6b79912013-06-24 16:20:28 +0530395 .data = (void *)EXYNOS5250_TMU_DRV_DATA,
Sachin Kamatb6cee532013-04-18 11:37:59 +0000396 },
397 {
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530398 .compatible = "samsung,exynos5250-tmu",
Amit Daniel Kachhape6b79912013-06-24 16:20:28 +0530399 .data = (void *)EXYNOS5250_TMU_DRV_DATA,
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530400 },
401 {},
402};
403MODULE_DEVICE_TABLE(of, exynos_tmu_match);
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530404#endif
405
406static struct platform_device_id exynos_tmu_driver_ids[] = {
407 {
408 .name = "exynos4210-tmu",
409 .driver_data = (kernel_ulong_t)EXYNOS4210_TMU_DRV_DATA,
410 },
411 {
412 .name = "exynos5250-tmu",
Amit Daniel Kachhape6b79912013-06-24 16:20:28 +0530413 .driver_data = (kernel_ulong_t)EXYNOS5250_TMU_DRV_DATA,
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530414 },
415 { },
416};
Jonghwan Choi3ae53b12012-10-23 14:54:42 +0800417MODULE_DEVICE_TABLE(platform, exynos_tmu_driver_ids);
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530418
419static inline struct exynos_tmu_platform_data *exynos_get_driver_data(
420 struct platform_device *pdev)
421{
422#ifdef CONFIG_OF
423 if (pdev->dev.of_node) {
424 const struct of_device_id *match;
425 match = of_match_node(exynos_tmu_match, pdev->dev.of_node);
426 if (!match)
427 return NULL;
428 return (struct exynos_tmu_platform_data *) match->data;
429 }
430#endif
431 return (struct exynos_tmu_platform_data *)
432 platform_get_device_id(pdev)->driver_data;
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530433}
Jonghwa Leebbf63be2012-11-21 13:31:01 +0900434
Greg Kroah-Hartman4eab7a9e2012-12-21 13:15:52 -0800435static int exynos_tmu_probe(struct platform_device *pdev)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900436{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530437 struct exynos_tmu_data *data;
438 struct exynos_tmu_platform_data *pdata = pdev->dev.platform_data;
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530439 int ret, i;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900440
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530441 if (!pdata)
442 pdata = exynos_get_driver_data(pdev);
443
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900444 if (!pdata) {
445 dev_err(&pdev->dev, "No platform init data supplied.\n");
446 return -ENODEV;
447 }
Amit Daniel Kachhap79e093c2012-08-16 05:41:45 -0600448 data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data),
449 GFP_KERNEL);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900450 if (!data) {
451 dev_err(&pdev->dev, "Failed to allocate driver structure\n");
452 return -ENOMEM;
453 }
454
455 data->irq = platform_get_irq(pdev, 0);
456 if (data->irq < 0) {
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900457 dev_err(&pdev->dev, "Failed to get platform irq\n");
Amit Daniel Kachhap79e093c2012-08-16 05:41:45 -0600458 return data->irq;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900459 }
460
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530461 INIT_WORK(&data->irq_work, exynos_tmu_work);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900462
463 data->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingca36b1b2013-01-21 11:09:20 +0100464 data->base = devm_ioremap_resource(&pdev->dev, data->mem);
465 if (IS_ERR(data->base))
466 return PTR_ERR(data->base);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900467
Amit Daniel Kachhap79e093c2012-08-16 05:41:45 -0600468 ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq,
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530469 IRQF_TRIGGER_RISING, "exynos-tmu", data);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900470 if (ret) {
471 dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq);
Amit Daniel Kachhap79e093c2012-08-16 05:41:45 -0600472 return ret;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900473 }
474
Sachin Kamat2a162792013-04-18 11:37:58 +0000475 data->clk = devm_clk_get(&pdev->dev, "tmu_apbif");
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900476 if (IS_ERR(data->clk)) {
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900477 dev_err(&pdev->dev, "Failed to get clock\n");
Amit Daniel Kachhap79e093c2012-08-16 05:41:45 -0600478 return PTR_ERR(data->clk);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900479 }
480
Sachin Kamat2a162792013-04-18 11:37:58 +0000481 ret = clk_prepare(data->clk);
482 if (ret)
483 return ret;
484
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530485 if (pdata->type == SOC_ARCH_EXYNOS ||
486 pdata->type == SOC_ARCH_EXYNOS4210)
487 data->soc = pdata->type;
488 else {
489 ret = -EINVAL;
490 dev_err(&pdev->dev, "Platform not supported\n");
491 goto err_clk;
492 }
493
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900494 data->pdata = pdata;
495 platform_set_drvdata(pdev, data);
496 mutex_init(&data->lock);
497
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530498 ret = exynos_tmu_initialize(pdev);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900499 if (ret) {
500 dev_err(&pdev->dev, "Failed to initialize TMU\n");
501 goto err_clk;
502 }
503
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530504 exynos_tmu_control(pdev, true);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900505
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530506 /* Register the sensor with thermal management interface */
507 (&exynos_sensor_conf)->private_data = data;
Amit Daniel Kachhapbb34b4c2013-06-24 16:20:30 +0530508 exynos_sensor_conf.trip_data.trip_count = pdata->trigger_enable[0] +
509 pdata->trigger_enable[1] + pdata->trigger_enable[2]+
510 pdata->trigger_enable[3];
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530511
512 for (i = 0; i < exynos_sensor_conf.trip_data.trip_count; i++)
513 exynos_sensor_conf.trip_data.trip_val[i] =
514 pdata->threshold + pdata->trigger_levels[i];
515
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000516 exynos_sensor_conf.trip_data.trigger_falling = pdata->threshold_falling;
517
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530518 exynos_sensor_conf.cooling_data.freq_clip_count =
519 pdata->freq_tab_count;
520 for (i = 0; i < pdata->freq_tab_count; i++) {
521 exynos_sensor_conf.cooling_data.freq_data[i].freq_clip_max =
522 pdata->freq_tab[i].freq_clip_max;
523 exynos_sensor_conf.cooling_data.freq_data[i].temp_level =
524 pdata->freq_tab[i].temp_level;
525 }
526
527 ret = exynos_register_thermal(&exynos_sensor_conf);
528 if (ret) {
529 dev_err(&pdev->dev, "Failed to register thermal interface\n");
530 goto err_clk;
531 }
Jonghwa Leebbf63be2012-11-21 13:31:01 +0900532
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900533 return 0;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900534err_clk:
Sachin Kamat2a162792013-04-18 11:37:58 +0000535 clk_unprepare(data->clk);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900536 return ret;
537}
538
Greg Kroah-Hartman4eab7a9e2012-12-21 13:15:52 -0800539static int exynos_tmu_remove(struct platform_device *pdev)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900540{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530541 struct exynos_tmu_data *data = platform_get_drvdata(pdev);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900542
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530543 exynos_tmu_control(pdev, false);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900544
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530545 exynos_unregister_thermal();
546
Sachin Kamat2a162792013-04-18 11:37:58 +0000547 clk_unprepare(data->clk);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900548
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900549 return 0;
550}
551
Rafael J. Wysocki08cd6752012-07-08 21:48:15 +0200552#ifdef CONFIG_PM_SLEEP
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530553static int exynos_tmu_suspend(struct device *dev)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900554{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530555 exynos_tmu_control(to_platform_device(dev), false);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900556
557 return 0;
558}
559
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530560static int exynos_tmu_resume(struct device *dev)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900561{
Rafael J. Wysocki08cd6752012-07-08 21:48:15 +0200562 struct platform_device *pdev = to_platform_device(dev);
563
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530564 exynos_tmu_initialize(pdev);
565 exynos_tmu_control(pdev, true);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900566
567 return 0;
568}
Rafael J. Wysocki08cd6752012-07-08 21:48:15 +0200569
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530570static SIMPLE_DEV_PM_OPS(exynos_tmu_pm,
571 exynos_tmu_suspend, exynos_tmu_resume);
572#define EXYNOS_TMU_PM (&exynos_tmu_pm)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900573#else
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530574#define EXYNOS_TMU_PM NULL
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900575#endif
576
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530577static struct platform_driver exynos_tmu_driver = {
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900578 .driver = {
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530579 .name = "exynos-tmu",
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900580 .owner = THIS_MODULE,
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530581 .pm = EXYNOS_TMU_PM,
Sachin Kamatcaa5cbd2012-12-12 15:54:24 +0530582 .of_match_table = of_match_ptr(exynos_tmu_match),
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900583 },
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530584 .probe = exynos_tmu_probe,
Greg Kroah-Hartman4eab7a9e2012-12-21 13:15:52 -0800585 .remove = exynos_tmu_remove,
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530586 .id_table = exynos_tmu_driver_ids,
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900587};
588
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530589module_platform_driver(exynos_tmu_driver);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900590
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530591MODULE_DESCRIPTION("EXYNOS TMU Driver");
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900592MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
593MODULE_LICENSE("GPL");
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530594MODULE_ALIAS("platform:exynos-tmu");