159dfa54cSAmit Daniel Kachhap /* 259dfa54cSAmit Daniel Kachhap * exynos_tmu.c - Samsung EXYNOS TMU (Thermal Management Unit) 359dfa54cSAmit Daniel Kachhap * 459dfa54cSAmit Daniel Kachhap * Copyright (C) 2011 Samsung Electronics 559dfa54cSAmit Daniel Kachhap * Donggeun Kim <dg77.kim@samsung.com> 659dfa54cSAmit Daniel Kachhap * Amit Daniel Kachhap <amit.kachhap@linaro.org> 759dfa54cSAmit Daniel Kachhap * 859dfa54cSAmit Daniel Kachhap * This program is free software; you can redistribute it and/or modify 959dfa54cSAmit Daniel Kachhap * it under the terms of the GNU General Public License as published by 1059dfa54cSAmit Daniel Kachhap * the Free Software Foundation; either version 2 of the License, or 1159dfa54cSAmit Daniel Kachhap * (at your option) any later version. 1259dfa54cSAmit Daniel Kachhap * 1359dfa54cSAmit Daniel Kachhap * This program is distributed in the hope that it will be useful, 1459dfa54cSAmit Daniel Kachhap * but WITHOUT ANY WARRANTY; without even the implied warranty of 1559dfa54cSAmit Daniel Kachhap * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1659dfa54cSAmit Daniel Kachhap * GNU General Public License for more details. 1759dfa54cSAmit Daniel Kachhap * 1859dfa54cSAmit Daniel Kachhap * You should have received a copy of the GNU General Public License 1959dfa54cSAmit Daniel Kachhap * along with this program; if not, write to the Free Software 2059dfa54cSAmit Daniel Kachhap * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 2159dfa54cSAmit Daniel Kachhap * 2259dfa54cSAmit Daniel Kachhap */ 2359dfa54cSAmit Daniel Kachhap 2459dfa54cSAmit Daniel Kachhap #include <linux/clk.h> 2559dfa54cSAmit Daniel Kachhap #include <linux/io.h> 2659dfa54cSAmit Daniel Kachhap #include <linux/interrupt.h> 2759dfa54cSAmit Daniel Kachhap #include <linux/module.h> 2859dfa54cSAmit Daniel Kachhap #include <linux/of.h> 2959dfa54cSAmit Daniel Kachhap #include <linux/platform_device.h> 3059dfa54cSAmit Daniel Kachhap 3159dfa54cSAmit Daniel Kachhap #include "exynos_thermal_common.h" 320c1836a6SAmit Daniel Kachhap #include "exynos_tmu.h" 33*e6b7991eSAmit Daniel Kachhap #include "exynos_tmu_data.h" 3459dfa54cSAmit Daniel Kachhap 3559dfa54cSAmit Daniel Kachhap /* Exynos generic registers */ 3659dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_REG_TRIMINFO 0x0 3759dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_REG_CONTROL 0x20 3859dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_REG_STATUS 0x28 3959dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_REG_CURRENT_TEMP 0x40 4059dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_REG_INTEN 0x70 4159dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_REG_INTSTAT 0x74 4259dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_REG_INTCLEAR 0x78 4359dfa54cSAmit Daniel Kachhap 4459dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_TRIM_TEMP_MASK 0xff 4559dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_GAIN_SHIFT 8 4659dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_REF_VOLTAGE_SHIFT 24 4759dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_CORE_ON 3 4859dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_CORE_OFF 2 4959dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_DEF_CODE_TO_TEMP_OFFSET 50 5059dfa54cSAmit Daniel Kachhap 5159dfa54cSAmit Daniel Kachhap /* Exynos4210 specific registers */ 5259dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_REG_THRESHOLD_TEMP 0x44 5359dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_REG_TRIG_LEVEL0 0x50 5459dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_REG_TRIG_LEVEL1 0x54 5559dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_REG_TRIG_LEVEL2 0x58 5659dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_REG_TRIG_LEVEL3 0x5C 5759dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_REG_PAST_TEMP0 0x60 5859dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_REG_PAST_TEMP1 0x64 5959dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_REG_PAST_TEMP2 0x68 6059dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_REG_PAST_TEMP3 0x6C 6159dfa54cSAmit Daniel Kachhap 6259dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_TRIG_LEVEL0_MASK 0x1 6359dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_TRIG_LEVEL1_MASK 0x10 6459dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_TRIG_LEVEL2_MASK 0x100 6559dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_TRIG_LEVEL3_MASK 0x1000 6659dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_INTCLEAR_VAL 0x1111 6759dfa54cSAmit Daniel Kachhap 6859dfa54cSAmit Daniel Kachhap /* Exynos5250 and Exynos4412 specific registers */ 6959dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_TRIMINFO_CON 0x14 7059dfa54cSAmit Daniel Kachhap #define EXYNOS_THD_TEMP_RISE 0x50 7159dfa54cSAmit Daniel Kachhap #define EXYNOS_THD_TEMP_FALL 0x54 7259dfa54cSAmit Daniel Kachhap #define EXYNOS_EMUL_CON 0x80 7359dfa54cSAmit Daniel Kachhap 7459dfa54cSAmit Daniel Kachhap #define EXYNOS_TRIMINFO_RELOAD 0x1 7559dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_CLEAR_RISE_INT 0x111 7659dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_CLEAR_FALL_INT (0x111 << 12) 7759dfa54cSAmit Daniel Kachhap #define EXYNOS_MUX_ADDR_VALUE 6 7859dfa54cSAmit Daniel Kachhap #define EXYNOS_MUX_ADDR_SHIFT 20 7959dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_TRIP_MODE_SHIFT 13 8059dfa54cSAmit Daniel Kachhap 8159dfa54cSAmit Daniel Kachhap #define EFUSE_MIN_VALUE 40 8259dfa54cSAmit Daniel Kachhap #define EFUSE_MAX_VALUE 100 8359dfa54cSAmit Daniel Kachhap 8459dfa54cSAmit Daniel Kachhap #ifdef CONFIG_THERMAL_EMULATION 8559dfa54cSAmit Daniel Kachhap #define EXYNOS_EMUL_TIME 0x57F0 8659dfa54cSAmit Daniel Kachhap #define EXYNOS_EMUL_TIME_SHIFT 16 8759dfa54cSAmit Daniel Kachhap #define EXYNOS_EMUL_DATA_SHIFT 8 8859dfa54cSAmit Daniel Kachhap #define EXYNOS_EMUL_DATA_MASK 0xFF 8959dfa54cSAmit Daniel Kachhap #define EXYNOS_EMUL_ENABLE 0x1 9059dfa54cSAmit Daniel Kachhap #endif /* CONFIG_THERMAL_EMULATION */ 9159dfa54cSAmit Daniel Kachhap 9259dfa54cSAmit Daniel Kachhap struct exynos_tmu_data { 9359dfa54cSAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata; 9459dfa54cSAmit Daniel Kachhap struct resource *mem; 9559dfa54cSAmit Daniel Kachhap void __iomem *base; 9659dfa54cSAmit Daniel Kachhap int irq; 9759dfa54cSAmit Daniel Kachhap enum soc_type soc; 9859dfa54cSAmit Daniel Kachhap struct work_struct irq_work; 9959dfa54cSAmit Daniel Kachhap struct mutex lock; 10059dfa54cSAmit Daniel Kachhap struct clk *clk; 10159dfa54cSAmit Daniel Kachhap u8 temp_error1, temp_error2; 10259dfa54cSAmit Daniel Kachhap }; 10359dfa54cSAmit Daniel Kachhap 10459dfa54cSAmit Daniel Kachhap /* 10559dfa54cSAmit Daniel Kachhap * TMU treats temperature as a mapped temperature code. 10659dfa54cSAmit Daniel Kachhap * The temperature is converted differently depending on the calibration type. 10759dfa54cSAmit Daniel Kachhap */ 10859dfa54cSAmit Daniel Kachhap static int temp_to_code(struct exynos_tmu_data *data, u8 temp) 10959dfa54cSAmit Daniel Kachhap { 11059dfa54cSAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata = data->pdata; 11159dfa54cSAmit Daniel Kachhap int temp_code; 11259dfa54cSAmit Daniel Kachhap 11359dfa54cSAmit Daniel Kachhap if (data->soc == SOC_ARCH_EXYNOS4210) 11459dfa54cSAmit Daniel Kachhap /* temp should range between 25 and 125 */ 11559dfa54cSAmit Daniel Kachhap if (temp < 25 || temp > 125) { 11659dfa54cSAmit Daniel Kachhap temp_code = -EINVAL; 11759dfa54cSAmit Daniel Kachhap goto out; 11859dfa54cSAmit Daniel Kachhap } 11959dfa54cSAmit Daniel Kachhap 12059dfa54cSAmit Daniel Kachhap switch (pdata->cal_type) { 12159dfa54cSAmit Daniel Kachhap case TYPE_TWO_POINT_TRIMMING: 12259dfa54cSAmit Daniel Kachhap temp_code = (temp - 25) * 12359dfa54cSAmit Daniel Kachhap (data->temp_error2 - data->temp_error1) / 12459dfa54cSAmit Daniel Kachhap (85 - 25) + data->temp_error1; 12559dfa54cSAmit Daniel Kachhap break; 12659dfa54cSAmit Daniel Kachhap case TYPE_ONE_POINT_TRIMMING: 12759dfa54cSAmit Daniel Kachhap temp_code = temp + data->temp_error1 - 25; 12859dfa54cSAmit Daniel Kachhap break; 12959dfa54cSAmit Daniel Kachhap default: 13059dfa54cSAmit Daniel Kachhap temp_code = temp + EXYNOS_TMU_DEF_CODE_TO_TEMP_OFFSET; 13159dfa54cSAmit Daniel Kachhap break; 13259dfa54cSAmit Daniel Kachhap } 13359dfa54cSAmit Daniel Kachhap out: 13459dfa54cSAmit Daniel Kachhap return temp_code; 13559dfa54cSAmit Daniel Kachhap } 13659dfa54cSAmit Daniel Kachhap 13759dfa54cSAmit Daniel Kachhap /* 13859dfa54cSAmit Daniel Kachhap * Calculate a temperature value from a temperature code. 13959dfa54cSAmit Daniel Kachhap * The unit of the temperature is degree Celsius. 14059dfa54cSAmit Daniel Kachhap */ 14159dfa54cSAmit Daniel Kachhap static int code_to_temp(struct exynos_tmu_data *data, u8 temp_code) 14259dfa54cSAmit Daniel Kachhap { 14359dfa54cSAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata = data->pdata; 14459dfa54cSAmit Daniel Kachhap int temp; 14559dfa54cSAmit Daniel Kachhap 14659dfa54cSAmit Daniel Kachhap if (data->soc == SOC_ARCH_EXYNOS4210) 14759dfa54cSAmit Daniel Kachhap /* temp_code should range between 75 and 175 */ 14859dfa54cSAmit Daniel Kachhap if (temp_code < 75 || temp_code > 175) { 14959dfa54cSAmit Daniel Kachhap temp = -ENODATA; 15059dfa54cSAmit Daniel Kachhap goto out; 15159dfa54cSAmit Daniel Kachhap } 15259dfa54cSAmit Daniel Kachhap 15359dfa54cSAmit Daniel Kachhap switch (pdata->cal_type) { 15459dfa54cSAmit Daniel Kachhap case TYPE_TWO_POINT_TRIMMING: 15559dfa54cSAmit Daniel Kachhap temp = (temp_code - data->temp_error1) * (85 - 25) / 15659dfa54cSAmit Daniel Kachhap (data->temp_error2 - data->temp_error1) + 25; 15759dfa54cSAmit Daniel Kachhap break; 15859dfa54cSAmit Daniel Kachhap case TYPE_ONE_POINT_TRIMMING: 15959dfa54cSAmit Daniel Kachhap temp = temp_code - data->temp_error1 + 25; 16059dfa54cSAmit Daniel Kachhap break; 16159dfa54cSAmit Daniel Kachhap default: 16259dfa54cSAmit Daniel Kachhap temp = temp_code - EXYNOS_TMU_DEF_CODE_TO_TEMP_OFFSET; 16359dfa54cSAmit Daniel Kachhap break; 16459dfa54cSAmit Daniel Kachhap } 16559dfa54cSAmit Daniel Kachhap out: 16659dfa54cSAmit Daniel Kachhap return temp; 16759dfa54cSAmit Daniel Kachhap } 16859dfa54cSAmit Daniel Kachhap 16959dfa54cSAmit Daniel Kachhap static int exynos_tmu_initialize(struct platform_device *pdev) 17059dfa54cSAmit Daniel Kachhap { 17159dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = platform_get_drvdata(pdev); 17259dfa54cSAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata = data->pdata; 17359dfa54cSAmit Daniel Kachhap unsigned int status, trim_info; 17459dfa54cSAmit Daniel Kachhap unsigned int rising_threshold = 0, falling_threshold = 0; 17559dfa54cSAmit Daniel Kachhap int ret = 0, threshold_code, i, trigger_levs = 0; 17659dfa54cSAmit Daniel Kachhap 17759dfa54cSAmit Daniel Kachhap mutex_lock(&data->lock); 17859dfa54cSAmit Daniel Kachhap clk_enable(data->clk); 17959dfa54cSAmit Daniel Kachhap 18059dfa54cSAmit Daniel Kachhap status = readb(data->base + EXYNOS_TMU_REG_STATUS); 18159dfa54cSAmit Daniel Kachhap if (!status) { 18259dfa54cSAmit Daniel Kachhap ret = -EBUSY; 18359dfa54cSAmit Daniel Kachhap goto out; 18459dfa54cSAmit Daniel Kachhap } 18559dfa54cSAmit Daniel Kachhap 18659dfa54cSAmit Daniel Kachhap if (data->soc == SOC_ARCH_EXYNOS) { 18759dfa54cSAmit Daniel Kachhap __raw_writel(EXYNOS_TRIMINFO_RELOAD, 18859dfa54cSAmit Daniel Kachhap data->base + EXYNOS_TMU_TRIMINFO_CON); 18959dfa54cSAmit Daniel Kachhap } 19059dfa54cSAmit Daniel Kachhap /* Save trimming info in order to perform calibration */ 19159dfa54cSAmit Daniel Kachhap trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO); 19259dfa54cSAmit Daniel Kachhap data->temp_error1 = trim_info & EXYNOS_TMU_TRIM_TEMP_MASK; 19359dfa54cSAmit Daniel Kachhap data->temp_error2 = ((trim_info >> 8) & EXYNOS_TMU_TRIM_TEMP_MASK); 19459dfa54cSAmit Daniel Kachhap 19559dfa54cSAmit Daniel Kachhap if ((EFUSE_MIN_VALUE > data->temp_error1) || 19659dfa54cSAmit Daniel Kachhap (data->temp_error1 > EFUSE_MAX_VALUE) || 19759dfa54cSAmit Daniel Kachhap (data->temp_error2 != 0)) 19859dfa54cSAmit Daniel Kachhap data->temp_error1 = pdata->efuse_value; 19959dfa54cSAmit Daniel Kachhap 20059dfa54cSAmit Daniel Kachhap /* Count trigger levels to be enabled */ 20159dfa54cSAmit Daniel Kachhap for (i = 0; i < MAX_THRESHOLD_LEVS; i++) 20259dfa54cSAmit Daniel Kachhap if (pdata->trigger_levels[i]) 20359dfa54cSAmit Daniel Kachhap trigger_levs++; 20459dfa54cSAmit Daniel Kachhap 20559dfa54cSAmit Daniel Kachhap if (data->soc == SOC_ARCH_EXYNOS4210) { 20659dfa54cSAmit Daniel Kachhap /* Write temperature code for threshold */ 20759dfa54cSAmit Daniel Kachhap threshold_code = temp_to_code(data, pdata->threshold); 20859dfa54cSAmit Daniel Kachhap if (threshold_code < 0) { 20959dfa54cSAmit Daniel Kachhap ret = threshold_code; 21059dfa54cSAmit Daniel Kachhap goto out; 21159dfa54cSAmit Daniel Kachhap } 21259dfa54cSAmit Daniel Kachhap writeb(threshold_code, 21359dfa54cSAmit Daniel Kachhap data->base + EXYNOS4210_TMU_REG_THRESHOLD_TEMP); 21459dfa54cSAmit Daniel Kachhap for (i = 0; i < trigger_levs; i++) 21559dfa54cSAmit Daniel Kachhap writeb(pdata->trigger_levels[i], 21659dfa54cSAmit Daniel Kachhap data->base + EXYNOS4210_TMU_REG_TRIG_LEVEL0 + i * 4); 21759dfa54cSAmit Daniel Kachhap 21859dfa54cSAmit Daniel Kachhap writel(EXYNOS4210_TMU_INTCLEAR_VAL, 21959dfa54cSAmit Daniel Kachhap data->base + EXYNOS_TMU_REG_INTCLEAR); 22059dfa54cSAmit Daniel Kachhap } else if (data->soc == SOC_ARCH_EXYNOS) { 22159dfa54cSAmit Daniel Kachhap /* Write temperature code for rising and falling threshold */ 22259dfa54cSAmit Daniel Kachhap for (i = 0; i < trigger_levs; i++) { 22359dfa54cSAmit Daniel Kachhap threshold_code = temp_to_code(data, 22459dfa54cSAmit Daniel Kachhap pdata->trigger_levels[i]); 22559dfa54cSAmit Daniel Kachhap if (threshold_code < 0) { 22659dfa54cSAmit Daniel Kachhap ret = threshold_code; 22759dfa54cSAmit Daniel Kachhap goto out; 22859dfa54cSAmit Daniel Kachhap } 22959dfa54cSAmit Daniel Kachhap rising_threshold |= threshold_code << 8 * i; 23059dfa54cSAmit Daniel Kachhap if (pdata->threshold_falling) { 23159dfa54cSAmit Daniel Kachhap threshold_code = temp_to_code(data, 23259dfa54cSAmit Daniel Kachhap pdata->trigger_levels[i] - 23359dfa54cSAmit Daniel Kachhap pdata->threshold_falling); 23459dfa54cSAmit Daniel Kachhap if (threshold_code > 0) 23559dfa54cSAmit Daniel Kachhap falling_threshold |= 23659dfa54cSAmit Daniel Kachhap threshold_code << 8 * i; 23759dfa54cSAmit Daniel Kachhap } 23859dfa54cSAmit Daniel Kachhap } 23959dfa54cSAmit Daniel Kachhap 24059dfa54cSAmit Daniel Kachhap writel(rising_threshold, 24159dfa54cSAmit Daniel Kachhap data->base + EXYNOS_THD_TEMP_RISE); 24259dfa54cSAmit Daniel Kachhap writel(falling_threshold, 24359dfa54cSAmit Daniel Kachhap data->base + EXYNOS_THD_TEMP_FALL); 24459dfa54cSAmit Daniel Kachhap 24559dfa54cSAmit Daniel Kachhap writel(EXYNOS_TMU_CLEAR_RISE_INT | EXYNOS_TMU_CLEAR_FALL_INT, 24659dfa54cSAmit Daniel Kachhap data->base + EXYNOS_TMU_REG_INTCLEAR); 24759dfa54cSAmit Daniel Kachhap } 24859dfa54cSAmit Daniel Kachhap out: 24959dfa54cSAmit Daniel Kachhap clk_disable(data->clk); 25059dfa54cSAmit Daniel Kachhap mutex_unlock(&data->lock); 25159dfa54cSAmit Daniel Kachhap 25259dfa54cSAmit Daniel Kachhap return ret; 25359dfa54cSAmit Daniel Kachhap } 25459dfa54cSAmit Daniel Kachhap 25559dfa54cSAmit Daniel Kachhap static void exynos_tmu_control(struct platform_device *pdev, bool on) 25659dfa54cSAmit Daniel Kachhap { 25759dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = platform_get_drvdata(pdev); 25859dfa54cSAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata = data->pdata; 25959dfa54cSAmit Daniel Kachhap unsigned int con, interrupt_en; 26059dfa54cSAmit Daniel Kachhap 26159dfa54cSAmit Daniel Kachhap mutex_lock(&data->lock); 26259dfa54cSAmit Daniel Kachhap clk_enable(data->clk); 26359dfa54cSAmit Daniel Kachhap 26459dfa54cSAmit Daniel Kachhap con = pdata->reference_voltage << EXYNOS_TMU_REF_VOLTAGE_SHIFT | 26559dfa54cSAmit Daniel Kachhap pdata->gain << EXYNOS_TMU_GAIN_SHIFT; 26659dfa54cSAmit Daniel Kachhap 26759dfa54cSAmit Daniel Kachhap if (data->soc == SOC_ARCH_EXYNOS) { 26859dfa54cSAmit Daniel Kachhap con |= pdata->noise_cancel_mode << EXYNOS_TMU_TRIP_MODE_SHIFT; 26959dfa54cSAmit Daniel Kachhap con |= (EXYNOS_MUX_ADDR_VALUE << EXYNOS_MUX_ADDR_SHIFT); 27059dfa54cSAmit Daniel Kachhap } 27159dfa54cSAmit Daniel Kachhap 27259dfa54cSAmit Daniel Kachhap if (on) { 27359dfa54cSAmit Daniel Kachhap con |= EXYNOS_TMU_CORE_ON; 27459dfa54cSAmit Daniel Kachhap interrupt_en = pdata->trigger_level3_en << 12 | 27559dfa54cSAmit Daniel Kachhap pdata->trigger_level2_en << 8 | 27659dfa54cSAmit Daniel Kachhap pdata->trigger_level1_en << 4 | 27759dfa54cSAmit Daniel Kachhap pdata->trigger_level0_en; 27859dfa54cSAmit Daniel Kachhap if (pdata->threshold_falling) 27959dfa54cSAmit Daniel Kachhap interrupt_en |= interrupt_en << 16; 28059dfa54cSAmit Daniel Kachhap } else { 28159dfa54cSAmit Daniel Kachhap con |= EXYNOS_TMU_CORE_OFF; 28259dfa54cSAmit Daniel Kachhap interrupt_en = 0; /* Disable all interrupts */ 28359dfa54cSAmit Daniel Kachhap } 28459dfa54cSAmit Daniel Kachhap writel(interrupt_en, data->base + EXYNOS_TMU_REG_INTEN); 28559dfa54cSAmit Daniel Kachhap writel(con, data->base + EXYNOS_TMU_REG_CONTROL); 28659dfa54cSAmit Daniel Kachhap 28759dfa54cSAmit Daniel Kachhap clk_disable(data->clk); 28859dfa54cSAmit Daniel Kachhap mutex_unlock(&data->lock); 28959dfa54cSAmit Daniel Kachhap } 29059dfa54cSAmit Daniel Kachhap 29159dfa54cSAmit Daniel Kachhap static int exynos_tmu_read(struct exynos_tmu_data *data) 29259dfa54cSAmit Daniel Kachhap { 29359dfa54cSAmit Daniel Kachhap u8 temp_code; 29459dfa54cSAmit Daniel Kachhap int temp; 29559dfa54cSAmit Daniel Kachhap 29659dfa54cSAmit Daniel Kachhap mutex_lock(&data->lock); 29759dfa54cSAmit Daniel Kachhap clk_enable(data->clk); 29859dfa54cSAmit Daniel Kachhap 29959dfa54cSAmit Daniel Kachhap temp_code = readb(data->base + EXYNOS_TMU_REG_CURRENT_TEMP); 30059dfa54cSAmit Daniel Kachhap temp = code_to_temp(data, temp_code); 30159dfa54cSAmit Daniel Kachhap 30259dfa54cSAmit Daniel Kachhap clk_disable(data->clk); 30359dfa54cSAmit Daniel Kachhap mutex_unlock(&data->lock); 30459dfa54cSAmit Daniel Kachhap 30559dfa54cSAmit Daniel Kachhap return temp; 30659dfa54cSAmit Daniel Kachhap } 30759dfa54cSAmit Daniel Kachhap 30859dfa54cSAmit Daniel Kachhap #ifdef CONFIG_THERMAL_EMULATION 30959dfa54cSAmit Daniel Kachhap static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp) 31059dfa54cSAmit Daniel Kachhap { 31159dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = drv_data; 31259dfa54cSAmit Daniel Kachhap unsigned int reg; 31359dfa54cSAmit Daniel Kachhap int ret = -EINVAL; 31459dfa54cSAmit Daniel Kachhap 31559dfa54cSAmit Daniel Kachhap if (data->soc == SOC_ARCH_EXYNOS4210) 31659dfa54cSAmit Daniel Kachhap goto out; 31759dfa54cSAmit Daniel Kachhap 31859dfa54cSAmit Daniel Kachhap if (temp && temp < MCELSIUS) 31959dfa54cSAmit Daniel Kachhap goto out; 32059dfa54cSAmit Daniel Kachhap 32159dfa54cSAmit Daniel Kachhap mutex_lock(&data->lock); 32259dfa54cSAmit Daniel Kachhap clk_enable(data->clk); 32359dfa54cSAmit Daniel Kachhap 32459dfa54cSAmit Daniel Kachhap reg = readl(data->base + EXYNOS_EMUL_CON); 32559dfa54cSAmit Daniel Kachhap 32659dfa54cSAmit Daniel Kachhap if (temp) { 32759dfa54cSAmit Daniel Kachhap temp /= MCELSIUS; 32859dfa54cSAmit Daniel Kachhap 32959dfa54cSAmit Daniel Kachhap reg = (EXYNOS_EMUL_TIME << EXYNOS_EMUL_TIME_SHIFT) | 33059dfa54cSAmit Daniel Kachhap (temp_to_code(data, temp) 33159dfa54cSAmit Daniel Kachhap << EXYNOS_EMUL_DATA_SHIFT) | EXYNOS_EMUL_ENABLE; 33259dfa54cSAmit Daniel Kachhap } else { 33359dfa54cSAmit Daniel Kachhap reg &= ~EXYNOS_EMUL_ENABLE; 33459dfa54cSAmit Daniel Kachhap } 33559dfa54cSAmit Daniel Kachhap 33659dfa54cSAmit Daniel Kachhap writel(reg, data->base + EXYNOS_EMUL_CON); 33759dfa54cSAmit Daniel Kachhap 33859dfa54cSAmit Daniel Kachhap clk_disable(data->clk); 33959dfa54cSAmit Daniel Kachhap mutex_unlock(&data->lock); 34059dfa54cSAmit Daniel Kachhap return 0; 34159dfa54cSAmit Daniel Kachhap out: 34259dfa54cSAmit Daniel Kachhap return ret; 34359dfa54cSAmit Daniel Kachhap } 34459dfa54cSAmit Daniel Kachhap #else 34559dfa54cSAmit Daniel Kachhap static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp) 34659dfa54cSAmit Daniel Kachhap { return -EINVAL; } 34759dfa54cSAmit Daniel Kachhap #endif/*CONFIG_THERMAL_EMULATION*/ 34859dfa54cSAmit Daniel Kachhap 34959dfa54cSAmit Daniel Kachhap static void exynos_tmu_work(struct work_struct *work) 35059dfa54cSAmit Daniel Kachhap { 35159dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = container_of(work, 35259dfa54cSAmit Daniel Kachhap struct exynos_tmu_data, irq_work); 35359dfa54cSAmit Daniel Kachhap 35459dfa54cSAmit Daniel Kachhap exynos_report_trigger(); 35559dfa54cSAmit Daniel Kachhap mutex_lock(&data->lock); 35659dfa54cSAmit Daniel Kachhap clk_enable(data->clk); 35759dfa54cSAmit Daniel Kachhap if (data->soc == SOC_ARCH_EXYNOS) 35859dfa54cSAmit Daniel Kachhap writel(EXYNOS_TMU_CLEAR_RISE_INT | 35959dfa54cSAmit Daniel Kachhap EXYNOS_TMU_CLEAR_FALL_INT, 36059dfa54cSAmit Daniel Kachhap data->base + EXYNOS_TMU_REG_INTCLEAR); 36159dfa54cSAmit Daniel Kachhap else 36259dfa54cSAmit Daniel Kachhap writel(EXYNOS4210_TMU_INTCLEAR_VAL, 36359dfa54cSAmit Daniel Kachhap data->base + EXYNOS_TMU_REG_INTCLEAR); 36459dfa54cSAmit Daniel Kachhap clk_disable(data->clk); 36559dfa54cSAmit Daniel Kachhap mutex_unlock(&data->lock); 36659dfa54cSAmit Daniel Kachhap 36759dfa54cSAmit Daniel Kachhap enable_irq(data->irq); 36859dfa54cSAmit Daniel Kachhap } 36959dfa54cSAmit Daniel Kachhap 37059dfa54cSAmit Daniel Kachhap static irqreturn_t exynos_tmu_irq(int irq, void *id) 37159dfa54cSAmit Daniel Kachhap { 37259dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = id; 37359dfa54cSAmit Daniel Kachhap 37459dfa54cSAmit Daniel Kachhap disable_irq_nosync(irq); 37559dfa54cSAmit Daniel Kachhap schedule_work(&data->irq_work); 37659dfa54cSAmit Daniel Kachhap 37759dfa54cSAmit Daniel Kachhap return IRQ_HANDLED; 37859dfa54cSAmit Daniel Kachhap } 37959dfa54cSAmit Daniel Kachhap static struct thermal_sensor_conf exynos_sensor_conf = { 38059dfa54cSAmit Daniel Kachhap .name = "exynos-therm", 38159dfa54cSAmit Daniel Kachhap .read_temperature = (int (*)(void *))exynos_tmu_read, 38259dfa54cSAmit Daniel Kachhap .write_emul_temp = exynos_tmu_set_emulation, 38359dfa54cSAmit Daniel Kachhap }; 38459dfa54cSAmit Daniel Kachhap 38559dfa54cSAmit Daniel Kachhap #ifdef CONFIG_OF 38659dfa54cSAmit Daniel Kachhap static const struct of_device_id exynos_tmu_match[] = { 38759dfa54cSAmit Daniel Kachhap { 38859dfa54cSAmit Daniel Kachhap .compatible = "samsung,exynos4210-tmu", 38959dfa54cSAmit Daniel Kachhap .data = (void *)EXYNOS4210_TMU_DRV_DATA, 39059dfa54cSAmit Daniel Kachhap }, 39159dfa54cSAmit Daniel Kachhap { 39259dfa54cSAmit Daniel Kachhap .compatible = "samsung,exynos4412-tmu", 393*e6b7991eSAmit Daniel Kachhap .data = (void *)EXYNOS5250_TMU_DRV_DATA, 39459dfa54cSAmit Daniel Kachhap }, 39559dfa54cSAmit Daniel Kachhap { 39659dfa54cSAmit Daniel Kachhap .compatible = "samsung,exynos5250-tmu", 397*e6b7991eSAmit Daniel Kachhap .data = (void *)EXYNOS5250_TMU_DRV_DATA, 39859dfa54cSAmit Daniel Kachhap }, 39959dfa54cSAmit Daniel Kachhap {}, 40059dfa54cSAmit Daniel Kachhap }; 40159dfa54cSAmit Daniel Kachhap MODULE_DEVICE_TABLE(of, exynos_tmu_match); 40259dfa54cSAmit Daniel Kachhap #endif 40359dfa54cSAmit Daniel Kachhap 40459dfa54cSAmit Daniel Kachhap static struct platform_device_id exynos_tmu_driver_ids[] = { 40559dfa54cSAmit Daniel Kachhap { 40659dfa54cSAmit Daniel Kachhap .name = "exynos4210-tmu", 40759dfa54cSAmit Daniel Kachhap .driver_data = (kernel_ulong_t)EXYNOS4210_TMU_DRV_DATA, 40859dfa54cSAmit Daniel Kachhap }, 40959dfa54cSAmit Daniel Kachhap { 41059dfa54cSAmit Daniel Kachhap .name = "exynos5250-tmu", 411*e6b7991eSAmit Daniel Kachhap .driver_data = (kernel_ulong_t)EXYNOS5250_TMU_DRV_DATA, 41259dfa54cSAmit Daniel Kachhap }, 41359dfa54cSAmit Daniel Kachhap { }, 41459dfa54cSAmit Daniel Kachhap }; 41559dfa54cSAmit Daniel Kachhap MODULE_DEVICE_TABLE(platform, exynos_tmu_driver_ids); 41659dfa54cSAmit Daniel Kachhap 41759dfa54cSAmit Daniel Kachhap static inline struct exynos_tmu_platform_data *exynos_get_driver_data( 41859dfa54cSAmit Daniel Kachhap struct platform_device *pdev) 41959dfa54cSAmit Daniel Kachhap { 42059dfa54cSAmit Daniel Kachhap #ifdef CONFIG_OF 42159dfa54cSAmit Daniel Kachhap if (pdev->dev.of_node) { 42259dfa54cSAmit Daniel Kachhap const struct of_device_id *match; 42359dfa54cSAmit Daniel Kachhap match = of_match_node(exynos_tmu_match, pdev->dev.of_node); 42459dfa54cSAmit Daniel Kachhap if (!match) 42559dfa54cSAmit Daniel Kachhap return NULL; 42659dfa54cSAmit Daniel Kachhap return (struct exynos_tmu_platform_data *) match->data; 42759dfa54cSAmit Daniel Kachhap } 42859dfa54cSAmit Daniel Kachhap #endif 42959dfa54cSAmit Daniel Kachhap return (struct exynos_tmu_platform_data *) 43059dfa54cSAmit Daniel Kachhap platform_get_device_id(pdev)->driver_data; 43159dfa54cSAmit Daniel Kachhap } 43259dfa54cSAmit Daniel Kachhap 43359dfa54cSAmit Daniel Kachhap static int exynos_tmu_probe(struct platform_device *pdev) 43459dfa54cSAmit Daniel Kachhap { 43559dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data; 43659dfa54cSAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata = pdev->dev.platform_data; 43759dfa54cSAmit Daniel Kachhap int ret, i; 43859dfa54cSAmit Daniel Kachhap 43959dfa54cSAmit Daniel Kachhap if (!pdata) 44059dfa54cSAmit Daniel Kachhap pdata = exynos_get_driver_data(pdev); 44159dfa54cSAmit Daniel Kachhap 44259dfa54cSAmit Daniel Kachhap if (!pdata) { 44359dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "No platform init data supplied.\n"); 44459dfa54cSAmit Daniel Kachhap return -ENODEV; 44559dfa54cSAmit Daniel Kachhap } 44659dfa54cSAmit Daniel Kachhap data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data), 44759dfa54cSAmit Daniel Kachhap GFP_KERNEL); 44859dfa54cSAmit Daniel Kachhap if (!data) { 44959dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to allocate driver structure\n"); 45059dfa54cSAmit Daniel Kachhap return -ENOMEM; 45159dfa54cSAmit Daniel Kachhap } 45259dfa54cSAmit Daniel Kachhap 45359dfa54cSAmit Daniel Kachhap data->irq = platform_get_irq(pdev, 0); 45459dfa54cSAmit Daniel Kachhap if (data->irq < 0) { 45559dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to get platform irq\n"); 45659dfa54cSAmit Daniel Kachhap return data->irq; 45759dfa54cSAmit Daniel Kachhap } 45859dfa54cSAmit Daniel Kachhap 45959dfa54cSAmit Daniel Kachhap INIT_WORK(&data->irq_work, exynos_tmu_work); 46059dfa54cSAmit Daniel Kachhap 46159dfa54cSAmit Daniel Kachhap data->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 46259dfa54cSAmit Daniel Kachhap data->base = devm_ioremap_resource(&pdev->dev, data->mem); 46359dfa54cSAmit Daniel Kachhap if (IS_ERR(data->base)) 46459dfa54cSAmit Daniel Kachhap return PTR_ERR(data->base); 46559dfa54cSAmit Daniel Kachhap 46659dfa54cSAmit Daniel Kachhap ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq, 46759dfa54cSAmit Daniel Kachhap IRQF_TRIGGER_RISING, "exynos-tmu", data); 46859dfa54cSAmit Daniel Kachhap if (ret) { 46959dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq); 47059dfa54cSAmit Daniel Kachhap return ret; 47159dfa54cSAmit Daniel Kachhap } 47259dfa54cSAmit Daniel Kachhap 47359dfa54cSAmit Daniel Kachhap data->clk = devm_clk_get(&pdev->dev, "tmu_apbif"); 47459dfa54cSAmit Daniel Kachhap if (IS_ERR(data->clk)) { 47559dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to get clock\n"); 47659dfa54cSAmit Daniel Kachhap return PTR_ERR(data->clk); 47759dfa54cSAmit Daniel Kachhap } 47859dfa54cSAmit Daniel Kachhap 47959dfa54cSAmit Daniel Kachhap ret = clk_prepare(data->clk); 48059dfa54cSAmit Daniel Kachhap if (ret) 48159dfa54cSAmit Daniel Kachhap return ret; 48259dfa54cSAmit Daniel Kachhap 48359dfa54cSAmit Daniel Kachhap if (pdata->type == SOC_ARCH_EXYNOS || 48459dfa54cSAmit Daniel Kachhap pdata->type == SOC_ARCH_EXYNOS4210) 48559dfa54cSAmit Daniel Kachhap data->soc = pdata->type; 48659dfa54cSAmit Daniel Kachhap else { 48759dfa54cSAmit Daniel Kachhap ret = -EINVAL; 48859dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "Platform not supported\n"); 48959dfa54cSAmit Daniel Kachhap goto err_clk; 49059dfa54cSAmit Daniel Kachhap } 49159dfa54cSAmit Daniel Kachhap 49259dfa54cSAmit Daniel Kachhap data->pdata = pdata; 49359dfa54cSAmit Daniel Kachhap platform_set_drvdata(pdev, data); 49459dfa54cSAmit Daniel Kachhap mutex_init(&data->lock); 49559dfa54cSAmit Daniel Kachhap 49659dfa54cSAmit Daniel Kachhap ret = exynos_tmu_initialize(pdev); 49759dfa54cSAmit Daniel Kachhap if (ret) { 49859dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to initialize TMU\n"); 49959dfa54cSAmit Daniel Kachhap goto err_clk; 50059dfa54cSAmit Daniel Kachhap } 50159dfa54cSAmit Daniel Kachhap 50259dfa54cSAmit Daniel Kachhap exynos_tmu_control(pdev, true); 50359dfa54cSAmit Daniel Kachhap 50459dfa54cSAmit Daniel Kachhap /* Register the sensor with thermal management interface */ 50559dfa54cSAmit Daniel Kachhap (&exynos_sensor_conf)->private_data = data; 50659dfa54cSAmit Daniel Kachhap exynos_sensor_conf.trip_data.trip_count = pdata->trigger_level0_en + 50759dfa54cSAmit Daniel Kachhap pdata->trigger_level1_en + pdata->trigger_level2_en + 50859dfa54cSAmit Daniel Kachhap pdata->trigger_level3_en; 50959dfa54cSAmit Daniel Kachhap 51059dfa54cSAmit Daniel Kachhap for (i = 0; i < exynos_sensor_conf.trip_data.trip_count; i++) 51159dfa54cSAmit Daniel Kachhap exynos_sensor_conf.trip_data.trip_val[i] = 51259dfa54cSAmit Daniel Kachhap pdata->threshold + pdata->trigger_levels[i]; 51359dfa54cSAmit Daniel Kachhap 51459dfa54cSAmit Daniel Kachhap exynos_sensor_conf.trip_data.trigger_falling = pdata->threshold_falling; 51559dfa54cSAmit Daniel Kachhap 51659dfa54cSAmit Daniel Kachhap exynos_sensor_conf.cooling_data.freq_clip_count = 51759dfa54cSAmit Daniel Kachhap pdata->freq_tab_count; 51859dfa54cSAmit Daniel Kachhap for (i = 0; i < pdata->freq_tab_count; i++) { 51959dfa54cSAmit Daniel Kachhap exynos_sensor_conf.cooling_data.freq_data[i].freq_clip_max = 52059dfa54cSAmit Daniel Kachhap pdata->freq_tab[i].freq_clip_max; 52159dfa54cSAmit Daniel Kachhap exynos_sensor_conf.cooling_data.freq_data[i].temp_level = 52259dfa54cSAmit Daniel Kachhap pdata->freq_tab[i].temp_level; 52359dfa54cSAmit Daniel Kachhap } 52459dfa54cSAmit Daniel Kachhap 52559dfa54cSAmit Daniel Kachhap ret = exynos_register_thermal(&exynos_sensor_conf); 52659dfa54cSAmit Daniel Kachhap if (ret) { 52759dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to register thermal interface\n"); 52859dfa54cSAmit Daniel Kachhap goto err_clk; 52959dfa54cSAmit Daniel Kachhap } 53059dfa54cSAmit Daniel Kachhap 53159dfa54cSAmit Daniel Kachhap return 0; 53259dfa54cSAmit Daniel Kachhap err_clk: 53359dfa54cSAmit Daniel Kachhap clk_unprepare(data->clk); 53459dfa54cSAmit Daniel Kachhap return ret; 53559dfa54cSAmit Daniel Kachhap } 53659dfa54cSAmit Daniel Kachhap 53759dfa54cSAmit Daniel Kachhap static int exynos_tmu_remove(struct platform_device *pdev) 53859dfa54cSAmit Daniel Kachhap { 53959dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = platform_get_drvdata(pdev); 54059dfa54cSAmit Daniel Kachhap 54159dfa54cSAmit Daniel Kachhap exynos_tmu_control(pdev, false); 54259dfa54cSAmit Daniel Kachhap 54359dfa54cSAmit Daniel Kachhap exynos_unregister_thermal(); 54459dfa54cSAmit Daniel Kachhap 54559dfa54cSAmit Daniel Kachhap clk_unprepare(data->clk); 54659dfa54cSAmit Daniel Kachhap 54759dfa54cSAmit Daniel Kachhap return 0; 54859dfa54cSAmit Daniel Kachhap } 54959dfa54cSAmit Daniel Kachhap 55059dfa54cSAmit Daniel Kachhap #ifdef CONFIG_PM_SLEEP 55159dfa54cSAmit Daniel Kachhap static int exynos_tmu_suspend(struct device *dev) 55259dfa54cSAmit Daniel Kachhap { 55359dfa54cSAmit Daniel Kachhap exynos_tmu_control(to_platform_device(dev), false); 55459dfa54cSAmit Daniel Kachhap 55559dfa54cSAmit Daniel Kachhap return 0; 55659dfa54cSAmit Daniel Kachhap } 55759dfa54cSAmit Daniel Kachhap 55859dfa54cSAmit Daniel Kachhap static int exynos_tmu_resume(struct device *dev) 55959dfa54cSAmit Daniel Kachhap { 56059dfa54cSAmit Daniel Kachhap struct platform_device *pdev = to_platform_device(dev); 56159dfa54cSAmit Daniel Kachhap 56259dfa54cSAmit Daniel Kachhap exynos_tmu_initialize(pdev); 56359dfa54cSAmit Daniel Kachhap exynos_tmu_control(pdev, true); 56459dfa54cSAmit Daniel Kachhap 56559dfa54cSAmit Daniel Kachhap return 0; 56659dfa54cSAmit Daniel Kachhap } 56759dfa54cSAmit Daniel Kachhap 56859dfa54cSAmit Daniel Kachhap static SIMPLE_DEV_PM_OPS(exynos_tmu_pm, 56959dfa54cSAmit Daniel Kachhap exynos_tmu_suspend, exynos_tmu_resume); 57059dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM (&exynos_tmu_pm) 57159dfa54cSAmit Daniel Kachhap #else 57259dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM NULL 57359dfa54cSAmit Daniel Kachhap #endif 57459dfa54cSAmit Daniel Kachhap 57559dfa54cSAmit Daniel Kachhap static struct platform_driver exynos_tmu_driver = { 57659dfa54cSAmit Daniel Kachhap .driver = { 57759dfa54cSAmit Daniel Kachhap .name = "exynos-tmu", 57859dfa54cSAmit Daniel Kachhap .owner = THIS_MODULE, 57959dfa54cSAmit Daniel Kachhap .pm = EXYNOS_TMU_PM, 58059dfa54cSAmit Daniel Kachhap .of_match_table = of_match_ptr(exynos_tmu_match), 58159dfa54cSAmit Daniel Kachhap }, 58259dfa54cSAmit Daniel Kachhap .probe = exynos_tmu_probe, 58359dfa54cSAmit Daniel Kachhap .remove = exynos_tmu_remove, 58459dfa54cSAmit Daniel Kachhap .id_table = exynos_tmu_driver_ids, 58559dfa54cSAmit Daniel Kachhap }; 58659dfa54cSAmit Daniel Kachhap 58759dfa54cSAmit Daniel Kachhap module_platform_driver(exynos_tmu_driver); 58859dfa54cSAmit Daniel Kachhap 58959dfa54cSAmit Daniel Kachhap MODULE_DESCRIPTION("EXYNOS TMU Driver"); 59059dfa54cSAmit Daniel Kachhap MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>"); 59159dfa54cSAmit Daniel Kachhap MODULE_LICENSE("GPL"); 59259dfa54cSAmit Daniel Kachhap MODULE_ALIAS("platform:exynos-tmu"); 593