blob: f6f63ca6faf3db629dd848bdccb77d063373d024 [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;
Amit Daniel Kachhapa4463c42013-06-24 16:20:33 +0530354 unsigned int val_irq;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900355
Amit Daniel Kachhap3ad95242013-01-17 01:42:18 +0000356 exynos_report_trigger();
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900357 mutex_lock(&data->lock);
358 clk_enable(data->clk);
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530359
Amit Daniel Kachhapa4463c42013-06-24 16:20:33 +0530360 /* TODO: take action based on particular interrupt */
361 val_irq = readl(data->base + reg->tmu_intstat);
362 /* clear the interrupts */
363 writel(val_irq, data->base + reg->tmu_intclear);
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530364
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900365 clk_disable(data->clk);
366 mutex_unlock(&data->lock);
Amit Daniel Kachhap3ad95242013-01-17 01:42:18 +0000367
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530368 enable_irq(data->irq);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900369}
370
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530371static irqreturn_t exynos_tmu_irq(int irq, void *id)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900372{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530373 struct exynos_tmu_data *data = id;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900374
375 disable_irq_nosync(irq);
376 schedule_work(&data->irq_work);
377
378 return IRQ_HANDLED;
379}
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530380static struct thermal_sensor_conf exynos_sensor_conf = {
381 .name = "exynos-therm",
382 .read_temperature = (int (*)(void *))exynos_tmu_read,
Amit Daniel Kachhapbffd1f82013-02-11 03:54:23 +0000383 .write_emul_temp = exynos_tmu_set_emulation,
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530384};
385
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530386#ifdef CONFIG_OF
387static const struct of_device_id exynos_tmu_match[] = {
388 {
389 .compatible = "samsung,exynos4210-tmu",
390 .data = (void *)EXYNOS4210_TMU_DRV_DATA,
391 },
392 {
Sachin Kamatb6cee532013-04-18 11:37:59 +0000393 .compatible = "samsung,exynos4412-tmu",
Amit Daniel Kachhape6b79912013-06-24 16:20:28 +0530394 .data = (void *)EXYNOS5250_TMU_DRV_DATA,
Sachin Kamatb6cee532013-04-18 11:37:59 +0000395 },
396 {
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530397 .compatible = "samsung,exynos5250-tmu",
Amit Daniel Kachhape6b79912013-06-24 16:20:28 +0530398 .data = (void *)EXYNOS5250_TMU_DRV_DATA,
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530399 },
400 {},
401};
402MODULE_DEVICE_TABLE(of, exynos_tmu_match);
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530403#endif
404
405static struct platform_device_id exynos_tmu_driver_ids[] = {
406 {
407 .name = "exynos4210-tmu",
408 .driver_data = (kernel_ulong_t)EXYNOS4210_TMU_DRV_DATA,
409 },
410 {
411 .name = "exynos5250-tmu",
Amit Daniel Kachhape6b79912013-06-24 16:20:28 +0530412 .driver_data = (kernel_ulong_t)EXYNOS5250_TMU_DRV_DATA,
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530413 },
414 { },
415};
Jonghwan Choi3ae53b12012-10-23 14:54:42 +0800416MODULE_DEVICE_TABLE(platform, exynos_tmu_driver_ids);
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530417
418static inline struct exynos_tmu_platform_data *exynos_get_driver_data(
419 struct platform_device *pdev)
420{
421#ifdef CONFIG_OF
422 if (pdev->dev.of_node) {
423 const struct of_device_id *match;
424 match = of_match_node(exynos_tmu_match, pdev->dev.of_node);
425 if (!match)
426 return NULL;
427 return (struct exynos_tmu_platform_data *) match->data;
428 }
429#endif
430 return (struct exynos_tmu_platform_data *)
431 platform_get_device_id(pdev)->driver_data;
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530432}
Jonghwa Leebbf63be2012-11-21 13:31:01 +0900433
Greg Kroah-Hartman4eab7a9e2012-12-21 13:15:52 -0800434static int exynos_tmu_probe(struct platform_device *pdev)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900435{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530436 struct exynos_tmu_data *data;
437 struct exynos_tmu_platform_data *pdata = pdev->dev.platform_data;
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530438 int ret, i;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900439
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530440 if (!pdata)
441 pdata = exynos_get_driver_data(pdev);
442
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900443 if (!pdata) {
444 dev_err(&pdev->dev, "No platform init data supplied.\n");
445 return -ENODEV;
446 }
Amit Daniel Kachhap79e093c2012-08-16 05:41:45 -0600447 data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data),
448 GFP_KERNEL);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900449 if (!data) {
450 dev_err(&pdev->dev, "Failed to allocate driver structure\n");
451 return -ENOMEM;
452 }
453
454 data->irq = platform_get_irq(pdev, 0);
455 if (data->irq < 0) {
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900456 dev_err(&pdev->dev, "Failed to get platform irq\n");
Amit Daniel Kachhap79e093c2012-08-16 05:41:45 -0600457 return data->irq;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900458 }
459
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530460 INIT_WORK(&data->irq_work, exynos_tmu_work);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900461
462 data->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingca36b1b2013-01-21 11:09:20 +0100463 data->base = devm_ioremap_resource(&pdev->dev, data->mem);
464 if (IS_ERR(data->base))
465 return PTR_ERR(data->base);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900466
Amit Daniel Kachhap79e093c2012-08-16 05:41:45 -0600467 ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq,
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530468 IRQF_TRIGGER_RISING, "exynos-tmu", data);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900469 if (ret) {
470 dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq);
Amit Daniel Kachhap79e093c2012-08-16 05:41:45 -0600471 return ret;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900472 }
473
Sachin Kamat2a162792013-04-18 11:37:58 +0000474 data->clk = devm_clk_get(&pdev->dev, "tmu_apbif");
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900475 if (IS_ERR(data->clk)) {
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900476 dev_err(&pdev->dev, "Failed to get clock\n");
Amit Daniel Kachhap79e093c2012-08-16 05:41:45 -0600477 return PTR_ERR(data->clk);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900478 }
479
Sachin Kamat2a162792013-04-18 11:37:58 +0000480 ret = clk_prepare(data->clk);
481 if (ret)
482 return ret;
483
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530484 if (pdata->type == SOC_ARCH_EXYNOS ||
485 pdata->type == SOC_ARCH_EXYNOS4210)
486 data->soc = pdata->type;
487 else {
488 ret = -EINVAL;
489 dev_err(&pdev->dev, "Platform not supported\n");
490 goto err_clk;
491 }
492
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900493 data->pdata = pdata;
494 platform_set_drvdata(pdev, data);
495 mutex_init(&data->lock);
496
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530497 ret = exynos_tmu_initialize(pdev);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900498 if (ret) {
499 dev_err(&pdev->dev, "Failed to initialize TMU\n");
500 goto err_clk;
501 }
502
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530503 exynos_tmu_control(pdev, true);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900504
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530505 /* Register the sensor with thermal management interface */
506 (&exynos_sensor_conf)->private_data = data;
Amit Daniel Kachhapbb34b4c2013-06-24 16:20:30 +0530507 exynos_sensor_conf.trip_data.trip_count = pdata->trigger_enable[0] +
508 pdata->trigger_enable[1] + pdata->trigger_enable[2]+
509 pdata->trigger_enable[3];
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530510
511 for (i = 0; i < exynos_sensor_conf.trip_data.trip_count; i++)
512 exynos_sensor_conf.trip_data.trip_val[i] =
513 pdata->threshold + pdata->trigger_levels[i];
514
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000515 exynos_sensor_conf.trip_data.trigger_falling = pdata->threshold_falling;
516
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530517 exynos_sensor_conf.cooling_data.freq_clip_count =
518 pdata->freq_tab_count;
519 for (i = 0; i < pdata->freq_tab_count; i++) {
520 exynos_sensor_conf.cooling_data.freq_data[i].freq_clip_max =
521 pdata->freq_tab[i].freq_clip_max;
522 exynos_sensor_conf.cooling_data.freq_data[i].temp_level =
523 pdata->freq_tab[i].temp_level;
524 }
525
526 ret = exynos_register_thermal(&exynos_sensor_conf);
527 if (ret) {
528 dev_err(&pdev->dev, "Failed to register thermal interface\n");
529 goto err_clk;
530 }
Jonghwa Leebbf63be2012-11-21 13:31:01 +0900531
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900532 return 0;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900533err_clk:
Sachin Kamat2a162792013-04-18 11:37:58 +0000534 clk_unprepare(data->clk);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900535 return ret;
536}
537
Greg Kroah-Hartman4eab7a9e2012-12-21 13:15:52 -0800538static int exynos_tmu_remove(struct platform_device *pdev)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900539{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530540 struct exynos_tmu_data *data = platform_get_drvdata(pdev);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900541
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530542 exynos_tmu_control(pdev, false);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900543
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530544 exynos_unregister_thermal();
545
Sachin Kamat2a162792013-04-18 11:37:58 +0000546 clk_unprepare(data->clk);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900547
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900548 return 0;
549}
550
Rafael J. Wysocki08cd6752012-07-08 21:48:15 +0200551#ifdef CONFIG_PM_SLEEP
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530552static int exynos_tmu_suspend(struct device *dev)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900553{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530554 exynos_tmu_control(to_platform_device(dev), false);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900555
556 return 0;
557}
558
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530559static int exynos_tmu_resume(struct device *dev)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900560{
Rafael J. Wysocki08cd6752012-07-08 21:48:15 +0200561 struct platform_device *pdev = to_platform_device(dev);
562
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530563 exynos_tmu_initialize(pdev);
564 exynos_tmu_control(pdev, true);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900565
566 return 0;
567}
Rafael J. Wysocki08cd6752012-07-08 21:48:15 +0200568
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530569static SIMPLE_DEV_PM_OPS(exynos_tmu_pm,
570 exynos_tmu_suspend, exynos_tmu_resume);
571#define EXYNOS_TMU_PM (&exynos_tmu_pm)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900572#else
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530573#define EXYNOS_TMU_PM NULL
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900574#endif
575
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530576static struct platform_driver exynos_tmu_driver = {
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900577 .driver = {
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530578 .name = "exynos-tmu",
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900579 .owner = THIS_MODULE,
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530580 .pm = EXYNOS_TMU_PM,
Sachin Kamatcaa5cbd2012-12-12 15:54:24 +0530581 .of_match_table = of_match_ptr(exynos_tmu_match),
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900582 },
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530583 .probe = exynos_tmu_probe,
Greg Kroah-Hartman4eab7a9e2012-12-21 13:15:52 -0800584 .remove = exynos_tmu_remove,
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530585 .id_table = exynos_tmu_driver_ids,
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900586};
587
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530588module_platform_driver(exynos_tmu_driver);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900589
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530590MODULE_DESCRIPTION("EXYNOS TMU Driver");
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900591MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
592MODULE_LICENSE("GPL");
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530593MODULE_ALIAS("platform:exynos-tmu");