blob: 31bf373734bb0aa330efd9450c14f8b7c6d776c4 [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 Kachhapcebe7372013-06-24 16:20:39 +053029#include <linux/of_address.h>
30#include <linux/of_irq.h>
Amit Daniel Kachhap1b678642013-06-24 16:20:25 +053031#include <linux/platform_device.h>
Amit Daniel Kachhap1b678642013-06-24 16:20:25 +053032
33#include "exynos_thermal_common.h"
Amit Daniel Kachhap0c1836a2013-06-24 16:20:27 +053034#include "exynos_tmu.h"
Amit Daniel Kachhape6b79912013-06-24 16:20:28 +053035#include "exynos_tmu_data.h"
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090036
Amit Daniel Kachhapcebe7372013-06-24 16:20:39 +053037/**
38 * struct exynos_tmu_data : A structure to hold the private data of the TMU
39 driver
40 * @id: identifier of the one instance of the TMU controller.
41 * @pdata: pointer to the tmu platform/configuration data
42 * @base: base address of the single instance of the TMU controller.
Amit Daniel Kachhapd9b6ee12013-06-24 16:20:42 +053043 * @base_common: base address of the common registers of the TMU controller.
Amit Daniel Kachhapcebe7372013-06-24 16:20:39 +053044 * @irq: irq number of the TMU controller.
45 * @soc: id of the SOC type.
46 * @irq_work: pointer to the irq work structure.
47 * @lock: lock to implement synchronization.
48 * @clk: pointer to the clock structure.
49 * @temp_error1: fused value of the first point trim.
50 * @temp_error2: fused value of the second point trim.
51 * @reg_conf: pointer to structure to register with core thermal.
52 */
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +053053struct exynos_tmu_data {
Amit Daniel Kachhapcebe7372013-06-24 16:20:39 +053054 int id;
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +053055 struct exynos_tmu_platform_data *pdata;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090056 void __iomem *base;
Amit Daniel Kachhapd9b6ee12013-06-24 16:20:42 +053057 void __iomem *base_common;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090058 int irq;
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +053059 enum soc_type soc;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090060 struct work_struct irq_work;
61 struct mutex lock;
62 struct clk *clk;
63 u8 temp_error1, temp_error2;
Amit Daniel Kachhapcebe7372013-06-24 16:20:39 +053064 struct thermal_sensor_conf *reg_conf;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090065};
66
67/*
68 * TMU treats temperature as a mapped temperature code.
69 * The temperature is converted differently depending on the calibration type.
70 */
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +053071static int temp_to_code(struct exynos_tmu_data *data, u8 temp)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090072{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +053073 struct exynos_tmu_platform_data *pdata = data->pdata;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090074 int temp_code;
75
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +053076 if (data->soc == SOC_ARCH_EXYNOS4210)
77 /* temp should range between 25 and 125 */
78 if (temp < 25 || temp > 125) {
79 temp_code = -EINVAL;
80 goto out;
81 }
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090082
83 switch (pdata->cal_type) {
84 case TYPE_TWO_POINT_TRIMMING:
Amit Daniel Kachhapbb34b4c2013-06-24 16:20:30 +053085 temp_code = (temp - pdata->first_point_trim) *
86 (data->temp_error2 - data->temp_error1) /
87 (pdata->second_point_trim - pdata->first_point_trim) +
88 data->temp_error1;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090089 break;
90 case TYPE_ONE_POINT_TRIMMING:
Amit Daniel Kachhapbb34b4c2013-06-24 16:20:30 +053091 temp_code = temp + data->temp_error1 - pdata->first_point_trim;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090092 break;
93 default:
Amit Daniel Kachhapbb34b4c2013-06-24 16:20:30 +053094 temp_code = temp + pdata->default_temp_offset;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +090095 break;
96 }
97out:
98 return temp_code;
99}
100
101/*
102 * Calculate a temperature value from a temperature code.
103 * The unit of the temperature is degree Celsius.
104 */
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530105static int code_to_temp(struct exynos_tmu_data *data, u8 temp_code)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900106{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530107 struct exynos_tmu_platform_data *pdata = data->pdata;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900108 int temp;
109
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530110 if (data->soc == SOC_ARCH_EXYNOS4210)
111 /* temp_code should range between 75 and 175 */
112 if (temp_code < 75 || temp_code > 175) {
113 temp = -ENODATA;
114 goto out;
115 }
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900116
117 switch (pdata->cal_type) {
118 case TYPE_TWO_POINT_TRIMMING:
Amit Daniel Kachhapbb34b4c2013-06-24 16:20:30 +0530119 temp = (temp_code - data->temp_error1) *
120 (pdata->second_point_trim - pdata->first_point_trim) /
121 (data->temp_error2 - data->temp_error1) +
122 pdata->first_point_trim;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900123 break;
124 case TYPE_ONE_POINT_TRIMMING:
Amit Daniel Kachhapbb34b4c2013-06-24 16:20:30 +0530125 temp = temp_code - data->temp_error1 + pdata->first_point_trim;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900126 break;
127 default:
Amit Daniel Kachhapbb34b4c2013-06-24 16:20:30 +0530128 temp = temp_code - pdata->default_temp_offset;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900129 break;
130 }
131out:
132 return temp;
133}
134
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530135static int exynos_tmu_initialize(struct platform_device *pdev)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900136{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530137 struct exynos_tmu_data *data = platform_get_drvdata(pdev);
138 struct exynos_tmu_platform_data *pdata = data->pdata;
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530139 const struct exynos_tmu_registers *reg = pdata->registers;
Amit Daniel Kachhap7ca04e52013-06-24 16:20:32 +0530140 unsigned int status, trim_info = 0, con;
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000141 unsigned int rising_threshold = 0, falling_threshold = 0;
142 int ret = 0, threshold_code, i, trigger_levs = 0;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900143
144 mutex_lock(&data->lock);
145 clk_enable(data->clk);
146
Amit Daniel Kachhapf4dae752013-06-24 16:20:40 +0530147 if (TMU_SUPPORTS(pdata, READY_STATUS)) {
148 status = readb(data->base + reg->tmu_status);
149 if (!status) {
150 ret = -EBUSY;
151 goto out;
152 }
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900153 }
154
Amit Daniel Kachhapf4dae752013-06-24 16:20:40 +0530155 if (TMU_SUPPORTS(pdata, TRIM_RELOAD))
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530156 __raw_writel(1, data->base + reg->triminfo_ctrl);
157
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530158 /* Save trimming info in order to perform calibration */
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530159 trim_info = readl(data->base + reg->triminfo_data);
160 data->temp_error1 = trim_info & EXYNOS_TMU_TEMP_MASK;
161 data->temp_error2 = ((trim_info >> reg->triminfo_85_shift) &
162 EXYNOS_TMU_TEMP_MASK);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900163
Amit Daniel Kachhapbb34b4c2013-06-24 16:20:30 +0530164 if ((pdata->min_efuse_value > data->temp_error1) ||
165 (data->temp_error1 > pdata->max_efuse_value) ||
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530166 (data->temp_error2 != 0))
167 data->temp_error1 = pdata->efuse_value;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900168
Amit Daniel Kachhap7ca04e52013-06-24 16:20:32 +0530169 if (pdata->max_trigger_level > MAX_THRESHOLD_LEVS) {
170 dev_err(&pdev->dev, "Invalid max trigger level\n");
171 goto out;
172 }
173
174 for (i = 0; i < pdata->max_trigger_level; i++) {
175 if (!pdata->trigger_levels[i])
176 continue;
177
178 if ((pdata->trigger_type[i] == HW_TRIP) &&
179 (!pdata->trigger_levels[pdata->max_trigger_level - 1])) {
180 dev_err(&pdev->dev, "Invalid hw trigger level\n");
181 ret = -EINVAL;
182 goto out;
183 }
184
185 /* Count trigger levels except the HW trip*/
186 if (!(pdata->trigger_type[i] == HW_TRIP))
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000187 trigger_levs++;
Amit Daniel Kachhap7ca04e52013-06-24 16:20:32 +0530188 }
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000189
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530190 if (data->soc == SOC_ARCH_EXYNOS4210) {
191 /* Write temperature code for threshold */
192 threshold_code = temp_to_code(data, pdata->threshold);
193 if (threshold_code < 0) {
194 ret = threshold_code;
195 goto out;
196 }
197 writeb(threshold_code,
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530198 data->base + reg->threshold_temp);
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000199 for (i = 0; i < trigger_levs; i++)
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530200 writeb(pdata->trigger_levels[i], data->base +
201 reg->threshold_th0 + i * sizeof(reg->threshold_th0));
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530202
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530203 writel(reg->inten_rise_mask, data->base + reg->tmu_intclear);
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530204 } else if (data->soc == SOC_ARCH_EXYNOS) {
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000205 /* Write temperature code for rising and falling threshold */
Amit Daniel Kachhap7ca04e52013-06-24 16:20:32 +0530206 for (i = 0;
207 i < trigger_levs && i < EXYNOS_MAX_TRIGGER_PER_REG; i++) {
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000208 threshold_code = temp_to_code(data,
209 pdata->trigger_levels[i]);
210 if (threshold_code < 0) {
211 ret = threshold_code;
212 goto out;
213 }
214 rising_threshold |= threshold_code << 8 * i;
215 if (pdata->threshold_falling) {
216 threshold_code = temp_to_code(data,
217 pdata->trigger_levels[i] -
218 pdata->threshold_falling);
219 if (threshold_code > 0)
220 falling_threshold |=
221 threshold_code << 8 * i;
222 }
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530223 }
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530224
225 writel(rising_threshold,
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530226 data->base + reg->threshold_th0);
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000227 writel(falling_threshold,
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530228 data->base + reg->threshold_th1);
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530229
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530230 writel((reg->inten_rise_mask << reg->inten_rise_shift) |
231 (reg->inten_fall_mask << reg->inten_fall_shift),
232 data->base + reg->tmu_intclear);
Amit Daniel Kachhap7ca04e52013-06-24 16:20:32 +0530233
234 /* if last threshold limit is also present */
235 i = pdata->max_trigger_level - 1;
236 if (pdata->trigger_levels[i] &&
237 (pdata->trigger_type[i] == HW_TRIP)) {
238 threshold_code = temp_to_code(data,
239 pdata->trigger_levels[i]);
240 if (threshold_code < 0) {
241 ret = threshold_code;
242 goto out;
243 }
244 rising_threshold |= threshold_code << 8 * i;
245 writel(rising_threshold,
246 data->base + reg->threshold_th0);
247 con = readl(data->base + reg->tmu_ctrl);
248 con |= (1 << reg->therm_trip_en_shift);
249 writel(con, data->base + reg->tmu_ctrl);
250 }
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530251 }
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900252out:
253 clk_disable(data->clk);
254 mutex_unlock(&data->lock);
255
256 return ret;
257}
258
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530259static void exynos_tmu_control(struct platform_device *pdev, bool on)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900260{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530261 struct exynos_tmu_data *data = platform_get_drvdata(pdev);
262 struct exynos_tmu_platform_data *pdata = data->pdata;
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530263 const struct exynos_tmu_registers *reg = pdata->registers;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900264 unsigned int con, interrupt_en;
265
266 mutex_lock(&data->lock);
267 clk_enable(data->clk);
268
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530269 con = readl(data->base + reg->tmu_ctrl);
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530270
Amit Daniel Kachhapd0a0ce32013-06-24 16:20:29 +0530271 if (pdata->reference_voltage) {
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530272 con &= ~(reg->buf_vref_sel_mask << reg->buf_vref_sel_shift);
273 con |= pdata->reference_voltage << reg->buf_vref_sel_shift;
Amit Daniel Kachhapd0a0ce32013-06-24 16:20:29 +0530274 }
275
276 if (pdata->gain) {
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530277 con &= ~(reg->buf_slope_sel_mask << reg->buf_slope_sel_shift);
278 con |= (pdata->gain << reg->buf_slope_sel_shift);
Amit Daniel Kachhapd0a0ce32013-06-24 16:20:29 +0530279 }
280
281 if (pdata->noise_cancel_mode) {
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530282 con &= ~(reg->therm_trip_mode_mask <<
283 reg->therm_trip_mode_shift);
284 con |= (pdata->noise_cancel_mode << reg->therm_trip_mode_shift);
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530285 }
286
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900287 if (on) {
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530288 con |= (1 << reg->core_en_shift);
Amit Daniel Kachhapd0a0ce32013-06-24 16:20:29 +0530289 interrupt_en =
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530290 pdata->trigger_enable[3] << reg->inten_rise3_shift |
291 pdata->trigger_enable[2] << reg->inten_rise2_shift |
292 pdata->trigger_enable[1] << reg->inten_rise1_shift |
293 pdata->trigger_enable[0] << reg->inten_rise0_shift;
Amit Daniel Kachhapf4dae752013-06-24 16:20:40 +0530294 if (TMU_SUPPORTS(pdata, FALLING_TRIP))
Amit Daniel Kachhapd0a0ce32013-06-24 16:20:29 +0530295 interrupt_en |=
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530296 interrupt_en << reg->inten_fall0_shift;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900297 } else {
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530298 con &= ~(1 << reg->core_en_shift);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900299 interrupt_en = 0; /* Disable all interrupts */
300 }
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530301 writel(interrupt_en, data->base + reg->tmu_inten);
302 writel(con, data->base + reg->tmu_ctrl);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900303
304 clk_disable(data->clk);
305 mutex_unlock(&data->lock);
306}
307
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530308static int exynos_tmu_read(struct exynos_tmu_data *data)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900309{
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530310 struct exynos_tmu_platform_data *pdata = data->pdata;
311 const struct exynos_tmu_registers *reg = pdata->registers;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900312 u8 temp_code;
313 int temp;
314
315 mutex_lock(&data->lock);
316 clk_enable(data->clk);
317
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530318 temp_code = readb(data->base + reg->tmu_cur_temp);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900319 temp = code_to_temp(data, temp_code);
320
321 clk_disable(data->clk);
322 mutex_unlock(&data->lock);
323
324 return temp;
325}
326
Amit Daniel Kachhapbffd1f82013-02-11 03:54:23 +0000327#ifdef CONFIG_THERMAL_EMULATION
328static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp)
329{
330 struct exynos_tmu_data *data = drv_data;
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530331 struct exynos_tmu_platform_data *pdata = data->pdata;
332 const struct exynos_tmu_registers *reg = pdata->registers;
333 unsigned int val;
Amit Daniel Kachhapbffd1f82013-02-11 03:54:23 +0000334 int ret = -EINVAL;
335
Amit Daniel Kachhapf4dae752013-06-24 16:20:40 +0530336 if (!TMU_SUPPORTS(pdata, EMULATION))
Amit Daniel Kachhapbffd1f82013-02-11 03:54:23 +0000337 goto out;
338
339 if (temp && temp < MCELSIUS)
340 goto out;
341
342 mutex_lock(&data->lock);
343 clk_enable(data->clk);
344
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530345 val = readl(data->base + reg->emul_con);
Amit Daniel Kachhapbffd1f82013-02-11 03:54:23 +0000346
347 if (temp) {
348 temp /= MCELSIUS;
349
Amit Daniel Kachhapf4dae752013-06-24 16:20:40 +0530350 if (TMU_SUPPORTS(pdata, EMUL_TIME)) {
351 val &= ~(EXYNOS_EMUL_TIME_MASK << reg->emul_time_shift);
352 val |= (EXYNOS_EMUL_TIME << reg->emul_time_shift);
353 }
354 val &= ~(EXYNOS_EMUL_DATA_MASK << reg->emul_temp_shift);
355 val |= (temp_to_code(data, temp) << reg->emul_temp_shift) |
356 EXYNOS_EMUL_ENABLE;
Amit Daniel Kachhapbffd1f82013-02-11 03:54:23 +0000357 } else {
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530358 val &= ~EXYNOS_EMUL_ENABLE;
Amit Daniel Kachhapbffd1f82013-02-11 03:54:23 +0000359 }
360
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530361 writel(val, data->base + reg->emul_con);
Amit Daniel Kachhapbffd1f82013-02-11 03:54:23 +0000362
363 clk_disable(data->clk);
364 mutex_unlock(&data->lock);
365 return 0;
366out:
367 return ret;
368}
369#else
370static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp)
371 { return -EINVAL; }
372#endif/*CONFIG_THERMAL_EMULATION*/
373
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530374static void exynos_tmu_work(struct work_struct *work)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900375{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530376 struct exynos_tmu_data *data = container_of(work,
377 struct exynos_tmu_data, irq_work);
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530378 struct exynos_tmu_platform_data *pdata = data->pdata;
379 const struct exynos_tmu_registers *reg = pdata->registers;
Amit Daniel Kachhapa4463c42013-06-24 16:20:33 +0530380 unsigned int val_irq;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900381
Amit Daniel Kachhapcebe7372013-06-24 16:20:39 +0530382 exynos_report_trigger(data->reg_conf);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900383 mutex_lock(&data->lock);
384 clk_enable(data->clk);
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530385
Amit Daniel Kachhapa4463c42013-06-24 16:20:33 +0530386 /* TODO: take action based on particular interrupt */
387 val_irq = readl(data->base + reg->tmu_intstat);
388 /* clear the interrupts */
389 writel(val_irq, data->base + reg->tmu_intclear);
Amit Daniel Kachhapb8d582b2013-06-24 16:20:31 +0530390
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900391 clk_disable(data->clk);
392 mutex_unlock(&data->lock);
Amit Daniel Kachhap3ad95242013-01-17 01:42:18 +0000393
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530394 enable_irq(data->irq);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900395}
396
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530397static irqreturn_t exynos_tmu_irq(int irq, void *id)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900398{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530399 struct exynos_tmu_data *data = id;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900400
401 disable_irq_nosync(irq);
402 schedule_work(&data->irq_work);
403
404 return IRQ_HANDLED;
405}
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530406
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530407#ifdef CONFIG_OF
408static const struct of_device_id exynos_tmu_match[] = {
409 {
410 .compatible = "samsung,exynos4210-tmu",
411 .data = (void *)EXYNOS4210_TMU_DRV_DATA,
412 },
413 {
Sachin Kamatb6cee532013-04-18 11:37:59 +0000414 .compatible = "samsung,exynos4412-tmu",
Amit Daniel Kachhape6b79912013-06-24 16:20:28 +0530415 .data = (void *)EXYNOS5250_TMU_DRV_DATA,
Sachin Kamatb6cee532013-04-18 11:37:59 +0000416 },
417 {
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530418 .compatible = "samsung,exynos5250-tmu",
Amit Daniel Kachhape6b79912013-06-24 16:20:28 +0530419 .data = (void *)EXYNOS5250_TMU_DRV_DATA,
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530420 },
421 {},
422};
423MODULE_DEVICE_TABLE(of, exynos_tmu_match);
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530424#endif
425
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530426static inline struct exynos_tmu_platform_data *exynos_get_driver_data(
Amit Daniel Kachhapcebe7372013-06-24 16:20:39 +0530427 struct platform_device *pdev, int id)
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530428{
429#ifdef CONFIG_OF
Amit Daniel Kachhapcebe7372013-06-24 16:20:39 +0530430 struct exynos_tmu_init_data *data_table;
431 struct exynos_tmu_platform_data *tmu_data;
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530432 if (pdev->dev.of_node) {
433 const struct of_device_id *match;
434 match = of_match_node(exynos_tmu_match, pdev->dev.of_node);
435 if (!match)
436 return NULL;
Amit Daniel Kachhapcebe7372013-06-24 16:20:39 +0530437 data_table = (struct exynos_tmu_init_data *) match->data;
438 if (!data_table || id >= data_table->tmu_count)
439 return NULL;
440 tmu_data = data_table->tmu_data;
441 return (struct exynos_tmu_platform_data *) (tmu_data + id);
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530442 }
443#endif
Amit Daniel Kachhap1cd1ecb2013-06-24 16:20:38 +0530444 return NULL;
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530445}
Jonghwa Leebbf63be2012-11-21 13:31:01 +0900446
Amit Daniel Kachhapcebe7372013-06-24 16:20:39 +0530447static int exynos_map_dt_data(struct platform_device *pdev)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900448{
Amit Daniel Kachhapcebe7372013-06-24 16:20:39 +0530449 struct exynos_tmu_data *data = platform_get_drvdata(pdev);
450 struct exynos_tmu_platform_data *pdata;
451 struct resource res;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900452
Amit Daniel Kachhapcebe7372013-06-24 16:20:39 +0530453 if (!data)
454 return -ENODEV;
Amit Daniel Kachhap17be8682012-08-16 17:11:44 +0530455
Amit Daniel Kachhapcebe7372013-06-24 16:20:39 +0530456 data->id = of_alias_get_id(pdev->dev.of_node, "tmuctrl");
457 if (data->id < 0)
458 data->id = 0;
459
460 data->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
461 if (data->irq <= 0) {
462 dev_err(&pdev->dev, "failed to get IRQ\n");
463 return -ENODEV;
464 }
465
466 if (of_address_to_resource(pdev->dev.of_node, 0, &res)) {
467 dev_err(&pdev->dev, "failed to get Resource 0\n");
468 return -ENODEV;
469 }
470
471 data->base = devm_ioremap(&pdev->dev, res.start, resource_size(&res));
472 if (!data->base) {
473 dev_err(&pdev->dev, "Failed to ioremap memory\n");
474 return -EADDRNOTAVAIL;
475 }
476
477 pdata = exynos_get_driver_data(pdev, data->id);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900478 if (!pdata) {
479 dev_err(&pdev->dev, "No platform init data supplied.\n");
480 return -ENODEV;
481 }
Amit Daniel Kachhapcebe7372013-06-24 16:20:39 +0530482 data->pdata = pdata;
Amit Daniel Kachhapd9b6ee12013-06-24 16:20:42 +0530483 /*
484 * Check if the TMU shares some registers and then try to map the
485 * memory of common registers.
486 */
487 if (!TMU_SUPPORTS(pdata, SHARED_MEMORY))
488 return 0;
489
490 if (of_address_to_resource(pdev->dev.of_node, 1, &res)) {
491 dev_err(&pdev->dev, "failed to get Resource 1\n");
492 return -ENODEV;
493 }
494
495 data->base_common = devm_ioremap(&pdev->dev, res.start,
496 resource_size(&res));
497 if (!data->base) {
498 dev_err(&pdev->dev, "Failed to ioremap memory\n");
499 return -ENOMEM;
500 }
Amit Daniel Kachhapcebe7372013-06-24 16:20:39 +0530501
502 return 0;
503}
504
505static int exynos_tmu_probe(struct platform_device *pdev)
506{
507 struct exynos_tmu_data *data;
508 struct exynos_tmu_platform_data *pdata;
509 struct thermal_sensor_conf *sensor_conf;
510 int ret, i;
511
Amit Daniel Kachhap79e093c2012-08-16 05:41:45 -0600512 data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data),
513 GFP_KERNEL);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900514 if (!data) {
515 dev_err(&pdev->dev, "Failed to allocate driver structure\n");
516 return -ENOMEM;
517 }
518
Amit Daniel Kachhapcebe7372013-06-24 16:20:39 +0530519 platform_set_drvdata(pdev, data);
520 mutex_init(&data->lock);
521
522 ret = exynos_map_dt_data(pdev);
523 if (ret)
524 return ret;
525
526 pdata = data->pdata;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900527
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530528 INIT_WORK(&data->irq_work, exynos_tmu_work);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900529
Sachin Kamat2a162792013-04-18 11:37:58 +0000530 data->clk = devm_clk_get(&pdev->dev, "tmu_apbif");
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900531 if (IS_ERR(data->clk)) {
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900532 dev_err(&pdev->dev, "Failed to get clock\n");
Amit Daniel Kachhap79e093c2012-08-16 05:41:45 -0600533 return PTR_ERR(data->clk);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900534 }
535
Sachin Kamat2a162792013-04-18 11:37:58 +0000536 ret = clk_prepare(data->clk);
537 if (ret)
538 return ret;
539
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530540 if (pdata->type == SOC_ARCH_EXYNOS ||
541 pdata->type == SOC_ARCH_EXYNOS4210)
542 data->soc = pdata->type;
543 else {
544 ret = -EINVAL;
545 dev_err(&pdev->dev, "Platform not supported\n");
546 goto err_clk;
547 }
548
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530549 ret = exynos_tmu_initialize(pdev);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900550 if (ret) {
551 dev_err(&pdev->dev, "Failed to initialize TMU\n");
552 goto err_clk;
553 }
554
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530555 exynos_tmu_control(pdev, true);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900556
Amit Daniel Kachhapcebe7372013-06-24 16:20:39 +0530557 /* Allocate a structure to register with the exynos core thermal */
558 sensor_conf = devm_kzalloc(&pdev->dev,
559 sizeof(struct thermal_sensor_conf), GFP_KERNEL);
560 if (!sensor_conf) {
561 dev_err(&pdev->dev, "Failed to allocate registration struct\n");
562 ret = -ENOMEM;
563 goto err_clk;
564 }
565 sprintf(sensor_conf->name, "therm_zone%d", data->id);
566 sensor_conf->read_temperature = (int (*)(void *))exynos_tmu_read;
567 sensor_conf->write_emul_temp =
568 (int (*)(void *, unsigned long))exynos_tmu_set_emulation;
569 sensor_conf->driver_data = data;
570 sensor_conf->trip_data.trip_count = pdata->trigger_enable[0] +
Amit Daniel Kachhapbb34b4c2013-06-24 16:20:30 +0530571 pdata->trigger_enable[1] + pdata->trigger_enable[2]+
572 pdata->trigger_enable[3];
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530573
Amit Daniel Kachhapcebe7372013-06-24 16:20:39 +0530574 for (i = 0; i < sensor_conf->trip_data.trip_count; i++) {
575 sensor_conf->trip_data.trip_val[i] =
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530576 pdata->threshold + pdata->trigger_levels[i];
Amit Daniel Kachhapcebe7372013-06-24 16:20:39 +0530577 sensor_conf->trip_data.trip_type[i] =
Amit Daniel Kachhap5c3cf552013-06-24 16:20:37 +0530578 pdata->trigger_type[i];
579 }
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530580
Amit Daniel Kachhapcebe7372013-06-24 16:20:39 +0530581 sensor_conf->trip_data.trigger_falling = pdata->threshold_falling;
Jonghwa Lee4f0a6842013-02-08 01:13:06 +0000582
Amit Daniel Kachhapcebe7372013-06-24 16:20:39 +0530583 sensor_conf->cooling_data.freq_clip_count = pdata->freq_tab_count;
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530584 for (i = 0; i < pdata->freq_tab_count; i++) {
Amit Daniel Kachhapcebe7372013-06-24 16:20:39 +0530585 sensor_conf->cooling_data.freq_data[i].freq_clip_max =
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530586 pdata->freq_tab[i].freq_clip_max;
Amit Daniel Kachhapcebe7372013-06-24 16:20:39 +0530587 sensor_conf->cooling_data.freq_data[i].temp_level =
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530588 pdata->freq_tab[i].temp_level;
589 }
Amit Daniel Kachhapcebe7372013-06-24 16:20:39 +0530590 sensor_conf->dev = &pdev->dev;
591 /* Register the sensor with thermal management interface */
592 ret = exynos_register_thermal(sensor_conf);
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530593 if (ret) {
594 dev_err(&pdev->dev, "Failed to register thermal interface\n");
595 goto err_clk;
596 }
Amit Daniel Kachhapcebe7372013-06-24 16:20:39 +0530597 data->reg_conf = sensor_conf;
598
599 ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq,
600 IRQF_TRIGGER_RISING | IRQF_SHARED, dev_name(&pdev->dev), data);
601 if (ret) {
602 dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq);
603 goto err_clk;
604 }
Jonghwa Leebbf63be2012-11-21 13:31:01 +0900605
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900606 return 0;
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900607err_clk:
Sachin Kamat2a162792013-04-18 11:37:58 +0000608 clk_unprepare(data->clk);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900609 return ret;
610}
611
Greg Kroah-Hartman4eab7a9e2012-12-21 13:15:52 -0800612static int exynos_tmu_remove(struct platform_device *pdev)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900613{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530614 struct exynos_tmu_data *data = platform_get_drvdata(pdev);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900615
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530616 exynos_tmu_control(pdev, false);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900617
Amit Daniel Kachhapcebe7372013-06-24 16:20:39 +0530618 exynos_unregister_thermal(data->reg_conf);
Amit Daniel Kachhap7e0b55e2012-08-16 17:11:43 +0530619
Sachin Kamat2a162792013-04-18 11:37:58 +0000620 clk_unprepare(data->clk);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900621
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900622 return 0;
623}
624
Rafael J. Wysocki08cd6752012-07-08 21:48:15 +0200625#ifdef CONFIG_PM_SLEEP
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530626static int exynos_tmu_suspend(struct device *dev)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900627{
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530628 exynos_tmu_control(to_platform_device(dev), false);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900629
630 return 0;
631}
632
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530633static int exynos_tmu_resume(struct device *dev)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900634{
Rafael J. Wysocki08cd6752012-07-08 21:48:15 +0200635 struct platform_device *pdev = to_platform_device(dev);
636
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530637 exynos_tmu_initialize(pdev);
638 exynos_tmu_control(pdev, true);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900639
640 return 0;
641}
Rafael J. Wysocki08cd6752012-07-08 21:48:15 +0200642
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530643static SIMPLE_DEV_PM_OPS(exynos_tmu_pm,
644 exynos_tmu_suspend, exynos_tmu_resume);
645#define EXYNOS_TMU_PM (&exynos_tmu_pm)
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900646#else
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530647#define EXYNOS_TMU_PM NULL
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900648#endif
649
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530650static struct platform_driver exynos_tmu_driver = {
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900651 .driver = {
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530652 .name = "exynos-tmu",
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900653 .owner = THIS_MODULE,
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530654 .pm = EXYNOS_TMU_PM,
Sachin Kamatcaa5cbd2012-12-12 15:54:24 +0530655 .of_match_table = of_match_ptr(exynos_tmu_match),
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900656 },
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530657 .probe = exynos_tmu_probe,
Greg Kroah-Hartman4eab7a9e2012-12-21 13:15:52 -0800658 .remove = exynos_tmu_remove,
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900659};
660
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530661module_platform_driver(exynos_tmu_driver);
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900662
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530663MODULE_DESCRIPTION("EXYNOS TMU Driver");
Donggeun Kim9d97e5c2011-09-07 18:49:08 +0900664MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
665MODULE_LICENSE("GPL");
Amit Daniel Kachhapf22d9c02012-08-16 17:11:42 +0530666MODULE_ALIAS("platform:exynos-tmu");