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> 29cebe7373SAmit Daniel Kachhap #include <linux/of_address.h> 30cebe7373SAmit Daniel Kachhap #include <linux/of_irq.h> 3159dfa54cSAmit Daniel Kachhap #include <linux/platform_device.h> 32498d22f6SAmit Daniel Kachhap #include <linux/regulator/consumer.h> 3359dfa54cSAmit Daniel Kachhap 3459dfa54cSAmit Daniel Kachhap #include "exynos_thermal_common.h" 350c1836a6SAmit Daniel Kachhap #include "exynos_tmu.h" 36e6b7991eSAmit Daniel Kachhap #include "exynos_tmu_data.h" 3759dfa54cSAmit Daniel Kachhap 38cebe7373SAmit Daniel Kachhap /** 39cebe7373SAmit Daniel Kachhap * struct exynos_tmu_data : A structure to hold the private data of the TMU 40cebe7373SAmit Daniel Kachhap driver 41cebe7373SAmit Daniel Kachhap * @id: identifier of the one instance of the TMU controller. 42cebe7373SAmit Daniel Kachhap * @pdata: pointer to the tmu platform/configuration data 43cebe7373SAmit Daniel Kachhap * @base: base address of the single instance of the TMU controller. 449025d563SNaveen Krishna Chatradhi * @base_second: base address of the common registers of the TMU controller. 45cebe7373SAmit Daniel Kachhap * @irq: irq number of the TMU controller. 46cebe7373SAmit Daniel Kachhap * @soc: id of the SOC type. 47cebe7373SAmit Daniel Kachhap * @irq_work: pointer to the irq work structure. 48cebe7373SAmit Daniel Kachhap * @lock: lock to implement synchronization. 49cebe7373SAmit Daniel Kachhap * @clk: pointer to the clock structure. 5014a11dc7SNaveen Krishna Chatradhi * @clk_sec: pointer to the clock structure for accessing the base_second. 51cebe7373SAmit Daniel Kachhap * @temp_error1: fused value of the first point trim. 52cebe7373SAmit Daniel Kachhap * @temp_error2: fused value of the second point trim. 53498d22f6SAmit Daniel Kachhap * @regulator: pointer to the TMU regulator structure. 54cebe7373SAmit Daniel Kachhap * @reg_conf: pointer to structure to register with core thermal. 5572d1100bSBartlomiej Zolnierkiewicz * @tmu_initialize: SoC specific TMU initialization method 5637f9034fSBartlomiej Zolnierkiewicz * @tmu_control: SoC specific TMU control method 57b79985caSBartlomiej Zolnierkiewicz * @tmu_read: SoC specific TMU temperature read method 58285d994aSBartlomiej Zolnierkiewicz * @tmu_set_emulation: SoC specific TMU emulation setting method 59a7331f72SBartlomiej Zolnierkiewicz * @tmu_clear_irqs: SoC specific TMU interrupts clearing method 60cebe7373SAmit Daniel Kachhap */ 6159dfa54cSAmit Daniel Kachhap struct exynos_tmu_data { 62cebe7373SAmit Daniel Kachhap int id; 6359dfa54cSAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata; 6459dfa54cSAmit Daniel Kachhap void __iomem *base; 659025d563SNaveen Krishna Chatradhi void __iomem *base_second; 6659dfa54cSAmit Daniel Kachhap int irq; 6759dfa54cSAmit Daniel Kachhap enum soc_type soc; 6859dfa54cSAmit Daniel Kachhap struct work_struct irq_work; 6959dfa54cSAmit Daniel Kachhap struct mutex lock; 7014a11dc7SNaveen Krishna Chatradhi struct clk *clk, *clk_sec; 7159dfa54cSAmit Daniel Kachhap u8 temp_error1, temp_error2; 72498d22f6SAmit Daniel Kachhap struct regulator *regulator; 73cebe7373SAmit Daniel Kachhap struct thermal_sensor_conf *reg_conf; 7472d1100bSBartlomiej Zolnierkiewicz int (*tmu_initialize)(struct platform_device *pdev); 7537f9034fSBartlomiej Zolnierkiewicz void (*tmu_control)(struct platform_device *pdev, bool on); 76b79985caSBartlomiej Zolnierkiewicz int (*tmu_read)(struct exynos_tmu_data *data); 77285d994aSBartlomiej Zolnierkiewicz void (*tmu_set_emulation)(struct exynos_tmu_data *data, 78285d994aSBartlomiej Zolnierkiewicz unsigned long temp); 79a7331f72SBartlomiej Zolnierkiewicz void (*tmu_clear_irqs)(struct exynos_tmu_data *data); 8059dfa54cSAmit Daniel Kachhap }; 8159dfa54cSAmit Daniel Kachhap 8259dfa54cSAmit Daniel Kachhap /* 8359dfa54cSAmit Daniel Kachhap * TMU treats temperature as a mapped temperature code. 8459dfa54cSAmit Daniel Kachhap * The temperature is converted differently depending on the calibration type. 8559dfa54cSAmit Daniel Kachhap */ 8659dfa54cSAmit Daniel Kachhap static int temp_to_code(struct exynos_tmu_data *data, u8 temp) 8759dfa54cSAmit Daniel Kachhap { 8859dfa54cSAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata = data->pdata; 8959dfa54cSAmit Daniel Kachhap int temp_code; 9059dfa54cSAmit Daniel Kachhap 9159dfa54cSAmit Daniel Kachhap switch (pdata->cal_type) { 9259dfa54cSAmit Daniel Kachhap case TYPE_TWO_POINT_TRIMMING: 93bb34b4c8SAmit Daniel Kachhap temp_code = (temp - pdata->first_point_trim) * 9459dfa54cSAmit Daniel Kachhap (data->temp_error2 - data->temp_error1) / 95bb34b4c8SAmit Daniel Kachhap (pdata->second_point_trim - pdata->first_point_trim) + 96bb34b4c8SAmit Daniel Kachhap data->temp_error1; 9759dfa54cSAmit Daniel Kachhap break; 9859dfa54cSAmit Daniel Kachhap case TYPE_ONE_POINT_TRIMMING: 99bb34b4c8SAmit Daniel Kachhap temp_code = temp + data->temp_error1 - pdata->first_point_trim; 10059dfa54cSAmit Daniel Kachhap break; 10159dfa54cSAmit Daniel Kachhap default: 102bb34b4c8SAmit Daniel Kachhap temp_code = temp + pdata->default_temp_offset; 10359dfa54cSAmit Daniel Kachhap break; 10459dfa54cSAmit Daniel Kachhap } 105ddb31d43SBartlomiej Zolnierkiewicz 10659dfa54cSAmit Daniel Kachhap return temp_code; 10759dfa54cSAmit Daniel Kachhap } 10859dfa54cSAmit Daniel Kachhap 10959dfa54cSAmit Daniel Kachhap /* 11059dfa54cSAmit Daniel Kachhap * Calculate a temperature value from a temperature code. 11159dfa54cSAmit Daniel Kachhap * The unit of the temperature is degree Celsius. 11259dfa54cSAmit Daniel Kachhap */ 11359dfa54cSAmit Daniel Kachhap static int code_to_temp(struct exynos_tmu_data *data, u8 temp_code) 11459dfa54cSAmit Daniel Kachhap { 11559dfa54cSAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata = data->pdata; 11659dfa54cSAmit Daniel Kachhap int temp; 11759dfa54cSAmit Daniel Kachhap 11859dfa54cSAmit Daniel Kachhap switch (pdata->cal_type) { 11959dfa54cSAmit Daniel Kachhap case TYPE_TWO_POINT_TRIMMING: 120bb34b4c8SAmit Daniel Kachhap temp = (temp_code - data->temp_error1) * 121bb34b4c8SAmit Daniel Kachhap (pdata->second_point_trim - pdata->first_point_trim) / 122bb34b4c8SAmit Daniel Kachhap (data->temp_error2 - data->temp_error1) + 123bb34b4c8SAmit Daniel Kachhap pdata->first_point_trim; 12459dfa54cSAmit Daniel Kachhap break; 12559dfa54cSAmit Daniel Kachhap case TYPE_ONE_POINT_TRIMMING: 126bb34b4c8SAmit Daniel Kachhap temp = temp_code - data->temp_error1 + pdata->first_point_trim; 12759dfa54cSAmit Daniel Kachhap break; 12859dfa54cSAmit Daniel Kachhap default: 129bb34b4c8SAmit Daniel Kachhap temp = temp_code - pdata->default_temp_offset; 13059dfa54cSAmit Daniel Kachhap break; 13159dfa54cSAmit Daniel Kachhap } 132ddb31d43SBartlomiej Zolnierkiewicz 13359dfa54cSAmit Daniel Kachhap return temp; 13459dfa54cSAmit Daniel Kachhap } 13559dfa54cSAmit Daniel Kachhap 1368328a4b1SBartlomiej Zolnierkiewicz static void sanitize_temp_error(struct exynos_tmu_data *data, u32 trim_info) 1378328a4b1SBartlomiej Zolnierkiewicz { 1388328a4b1SBartlomiej Zolnierkiewicz struct exynos_tmu_platform_data *pdata = data->pdata; 1398328a4b1SBartlomiej Zolnierkiewicz 1408328a4b1SBartlomiej Zolnierkiewicz data->temp_error1 = trim_info & EXYNOS_TMU_TEMP_MASK; 1418328a4b1SBartlomiej Zolnierkiewicz data->temp_error2 = ((trim_info >> EXYNOS_TRIMINFO_85_SHIFT) & 1428328a4b1SBartlomiej Zolnierkiewicz EXYNOS_TMU_TEMP_MASK); 1438328a4b1SBartlomiej Zolnierkiewicz 1448328a4b1SBartlomiej Zolnierkiewicz if (!data->temp_error1 || 1458328a4b1SBartlomiej Zolnierkiewicz (pdata->min_efuse_value > data->temp_error1) || 1468328a4b1SBartlomiej Zolnierkiewicz (data->temp_error1 > pdata->max_efuse_value)) 1478328a4b1SBartlomiej Zolnierkiewicz data->temp_error1 = pdata->efuse_value & EXYNOS_TMU_TEMP_MASK; 1488328a4b1SBartlomiej Zolnierkiewicz 1498328a4b1SBartlomiej Zolnierkiewicz if (!data->temp_error2) 1508328a4b1SBartlomiej Zolnierkiewicz data->temp_error2 = 1518328a4b1SBartlomiej Zolnierkiewicz (pdata->efuse_value >> EXYNOS_TRIMINFO_85_SHIFT) & 1528328a4b1SBartlomiej Zolnierkiewicz EXYNOS_TMU_TEMP_MASK; 1538328a4b1SBartlomiej Zolnierkiewicz } 1548328a4b1SBartlomiej Zolnierkiewicz 155fe87789cSBartlomiej Zolnierkiewicz static u32 get_th_reg(struct exynos_tmu_data *data, u32 threshold, bool falling) 156fe87789cSBartlomiej Zolnierkiewicz { 157fe87789cSBartlomiej Zolnierkiewicz struct exynos_tmu_platform_data *pdata = data->pdata; 158fe87789cSBartlomiej Zolnierkiewicz int i; 159fe87789cSBartlomiej Zolnierkiewicz 160fe87789cSBartlomiej Zolnierkiewicz for (i = 0; i < pdata->non_hw_trigger_levels; i++) { 161fe87789cSBartlomiej Zolnierkiewicz u8 temp = pdata->trigger_levels[i]; 162fe87789cSBartlomiej Zolnierkiewicz 163fe87789cSBartlomiej Zolnierkiewicz if (falling) 164fe87789cSBartlomiej Zolnierkiewicz temp -= pdata->threshold_falling; 165fe87789cSBartlomiej Zolnierkiewicz else 166fe87789cSBartlomiej Zolnierkiewicz threshold &= ~(0xff << 8 * i); 167fe87789cSBartlomiej Zolnierkiewicz 168fe87789cSBartlomiej Zolnierkiewicz threshold |= temp_to_code(data, temp) << 8 * i; 169fe87789cSBartlomiej Zolnierkiewicz } 170fe87789cSBartlomiej Zolnierkiewicz 171fe87789cSBartlomiej Zolnierkiewicz return threshold; 172fe87789cSBartlomiej Zolnierkiewicz } 173fe87789cSBartlomiej Zolnierkiewicz 17459dfa54cSAmit Daniel Kachhap static int exynos_tmu_initialize(struct platform_device *pdev) 17559dfa54cSAmit Daniel Kachhap { 17659dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = platform_get_drvdata(pdev); 17772d1100bSBartlomiej Zolnierkiewicz int ret; 17859dfa54cSAmit Daniel Kachhap 17959dfa54cSAmit Daniel Kachhap mutex_lock(&data->lock); 18059dfa54cSAmit Daniel Kachhap clk_enable(data->clk); 18114a11dc7SNaveen Krishna Chatradhi if (!IS_ERR(data->clk_sec)) 18214a11dc7SNaveen Krishna Chatradhi clk_enable(data->clk_sec); 18372d1100bSBartlomiej Zolnierkiewicz ret = data->tmu_initialize(pdev); 18459dfa54cSAmit Daniel Kachhap clk_disable(data->clk); 18559dfa54cSAmit Daniel Kachhap mutex_unlock(&data->lock); 18614a11dc7SNaveen Krishna Chatradhi if (!IS_ERR(data->clk_sec)) 18714a11dc7SNaveen Krishna Chatradhi clk_disable(data->clk_sec); 18859dfa54cSAmit Daniel Kachhap 18959dfa54cSAmit Daniel Kachhap return ret; 19059dfa54cSAmit Daniel Kachhap } 19159dfa54cSAmit Daniel Kachhap 192d00671c3SBartlomiej Zolnierkiewicz static u32 get_con_reg(struct exynos_tmu_data *data, u32 con) 19359dfa54cSAmit Daniel Kachhap { 19459dfa54cSAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata = data->pdata; 19559dfa54cSAmit Daniel Kachhap 196*7575983cSBartlomiej Zolnierkiewicz if (data->soc == SOC_ARCH_EXYNOS4412 || 197*7575983cSBartlomiej Zolnierkiewicz data->soc == SOC_ARCH_EXYNOS3250) 198*7575983cSBartlomiej Zolnierkiewicz con |= (EXYNOS4412_MUX_ADDR_VALUE << EXYNOS4412_MUX_ADDR_SHIFT); 19986f5362eSLukasz Majewski 20099d67fb9SBartlomiej Zolnierkiewicz con &= ~(EXYNOS_TMU_REF_VOLTAGE_MASK << EXYNOS_TMU_REF_VOLTAGE_SHIFT); 20199d67fb9SBartlomiej Zolnierkiewicz con |= pdata->reference_voltage << EXYNOS_TMU_REF_VOLTAGE_SHIFT; 202d0a0ce3eSAmit Daniel Kachhap 20399d67fb9SBartlomiej Zolnierkiewicz con &= ~(EXYNOS_TMU_BUF_SLOPE_SEL_MASK << EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT); 20499d67fb9SBartlomiej Zolnierkiewicz con |= (pdata->gain << EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT); 205d0a0ce3eSAmit Daniel Kachhap 206d0a0ce3eSAmit Daniel Kachhap if (pdata->noise_cancel_mode) { 207b9504a6aSBartlomiej Zolnierkiewicz con &= ~(EXYNOS_TMU_TRIP_MODE_MASK << EXYNOS_TMU_TRIP_MODE_SHIFT); 208b9504a6aSBartlomiej Zolnierkiewicz con |= (pdata->noise_cancel_mode << EXYNOS_TMU_TRIP_MODE_SHIFT); 20959dfa54cSAmit Daniel Kachhap } 21059dfa54cSAmit Daniel Kachhap 211d00671c3SBartlomiej Zolnierkiewicz return con; 212d00671c3SBartlomiej Zolnierkiewicz } 213d00671c3SBartlomiej Zolnierkiewicz 214d00671c3SBartlomiej Zolnierkiewicz static void exynos_tmu_control(struct platform_device *pdev, bool on) 215d00671c3SBartlomiej Zolnierkiewicz { 216d00671c3SBartlomiej Zolnierkiewicz struct exynos_tmu_data *data = platform_get_drvdata(pdev); 217d00671c3SBartlomiej Zolnierkiewicz 218d00671c3SBartlomiej Zolnierkiewicz mutex_lock(&data->lock); 219d00671c3SBartlomiej Zolnierkiewicz clk_enable(data->clk); 22037f9034fSBartlomiej Zolnierkiewicz data->tmu_control(pdev, on); 22159dfa54cSAmit Daniel Kachhap clk_disable(data->clk); 22259dfa54cSAmit Daniel Kachhap mutex_unlock(&data->lock); 22359dfa54cSAmit Daniel Kachhap } 22459dfa54cSAmit Daniel Kachhap 22572d1100bSBartlomiej Zolnierkiewicz static int exynos4210_tmu_initialize(struct platform_device *pdev) 22672d1100bSBartlomiej Zolnierkiewicz { 22772d1100bSBartlomiej Zolnierkiewicz struct exynos_tmu_data *data = platform_get_drvdata(pdev); 22872d1100bSBartlomiej Zolnierkiewicz struct exynos_tmu_platform_data *pdata = data->pdata; 22972d1100bSBartlomiej Zolnierkiewicz unsigned int status; 23072d1100bSBartlomiej Zolnierkiewicz int ret = 0, threshold_code, i; 23172d1100bSBartlomiej Zolnierkiewicz 23272d1100bSBartlomiej Zolnierkiewicz status = readb(data->base + EXYNOS_TMU_REG_STATUS); 23372d1100bSBartlomiej Zolnierkiewicz if (!status) { 23472d1100bSBartlomiej Zolnierkiewicz ret = -EBUSY; 23572d1100bSBartlomiej Zolnierkiewicz goto out; 23672d1100bSBartlomiej Zolnierkiewicz } 23772d1100bSBartlomiej Zolnierkiewicz 23872d1100bSBartlomiej Zolnierkiewicz sanitize_temp_error(data, readl(data->base + EXYNOS_TMU_REG_TRIMINFO)); 23972d1100bSBartlomiej Zolnierkiewicz 24072d1100bSBartlomiej Zolnierkiewicz /* Write temperature code for threshold */ 24172d1100bSBartlomiej Zolnierkiewicz threshold_code = temp_to_code(data, pdata->threshold); 24272d1100bSBartlomiej Zolnierkiewicz writeb(threshold_code, data->base + EXYNOS4210_TMU_REG_THRESHOLD_TEMP); 24372d1100bSBartlomiej Zolnierkiewicz 24472d1100bSBartlomiej Zolnierkiewicz for (i = 0; i < pdata->non_hw_trigger_levels; i++) 24572d1100bSBartlomiej Zolnierkiewicz writeb(pdata->trigger_levels[i], data->base + 24672d1100bSBartlomiej Zolnierkiewicz EXYNOS4210_TMU_REG_TRIG_LEVEL0 + i * 4); 24772d1100bSBartlomiej Zolnierkiewicz 248a7331f72SBartlomiej Zolnierkiewicz data->tmu_clear_irqs(data); 24972d1100bSBartlomiej Zolnierkiewicz out: 25072d1100bSBartlomiej Zolnierkiewicz return ret; 25172d1100bSBartlomiej Zolnierkiewicz } 25272d1100bSBartlomiej Zolnierkiewicz 25372d1100bSBartlomiej Zolnierkiewicz static int exynos4412_tmu_initialize(struct platform_device *pdev) 25472d1100bSBartlomiej Zolnierkiewicz { 25572d1100bSBartlomiej Zolnierkiewicz struct exynos_tmu_data *data = platform_get_drvdata(pdev); 25672d1100bSBartlomiej Zolnierkiewicz struct exynos_tmu_platform_data *pdata = data->pdata; 25772d1100bSBartlomiej Zolnierkiewicz unsigned int status, trim_info, con, ctrl, rising_threshold; 25872d1100bSBartlomiej Zolnierkiewicz int ret = 0, threshold_code, i; 25972d1100bSBartlomiej Zolnierkiewicz 26072d1100bSBartlomiej Zolnierkiewicz status = readb(data->base + EXYNOS_TMU_REG_STATUS); 26172d1100bSBartlomiej Zolnierkiewicz if (!status) { 26272d1100bSBartlomiej Zolnierkiewicz ret = -EBUSY; 26372d1100bSBartlomiej Zolnierkiewicz goto out; 26472d1100bSBartlomiej Zolnierkiewicz } 26572d1100bSBartlomiej Zolnierkiewicz 26672d1100bSBartlomiej Zolnierkiewicz if (data->soc == SOC_ARCH_EXYNOS3250 || 26772d1100bSBartlomiej Zolnierkiewicz data->soc == SOC_ARCH_EXYNOS4412 || 26872d1100bSBartlomiej Zolnierkiewicz data->soc == SOC_ARCH_EXYNOS5250) { 26972d1100bSBartlomiej Zolnierkiewicz if (data->soc == SOC_ARCH_EXYNOS3250) { 27072d1100bSBartlomiej Zolnierkiewicz ctrl = readl(data->base + EXYNOS_TMU_TRIMINFO_CON1); 27172d1100bSBartlomiej Zolnierkiewicz ctrl |= EXYNOS_TRIMINFO_RELOAD_ENABLE; 27272d1100bSBartlomiej Zolnierkiewicz writel(ctrl, data->base + EXYNOS_TMU_TRIMINFO_CON1); 27372d1100bSBartlomiej Zolnierkiewicz } 27472d1100bSBartlomiej Zolnierkiewicz ctrl = readl(data->base + EXYNOS_TMU_TRIMINFO_CON2); 27572d1100bSBartlomiej Zolnierkiewicz ctrl |= EXYNOS_TRIMINFO_RELOAD_ENABLE; 27672d1100bSBartlomiej Zolnierkiewicz writel(ctrl, data->base + EXYNOS_TMU_TRIMINFO_CON2); 27772d1100bSBartlomiej Zolnierkiewicz } 27872d1100bSBartlomiej Zolnierkiewicz 27972d1100bSBartlomiej Zolnierkiewicz /* On exynos5420 the triminfo register is in the shared space */ 28072d1100bSBartlomiej Zolnierkiewicz if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO) 28172d1100bSBartlomiej Zolnierkiewicz trim_info = readl(data->base_second + EXYNOS_TMU_REG_TRIMINFO); 28272d1100bSBartlomiej Zolnierkiewicz else 28372d1100bSBartlomiej Zolnierkiewicz trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO); 28472d1100bSBartlomiej Zolnierkiewicz 28572d1100bSBartlomiej Zolnierkiewicz sanitize_temp_error(data, trim_info); 28672d1100bSBartlomiej Zolnierkiewicz 28772d1100bSBartlomiej Zolnierkiewicz /* Write temperature code for rising and falling threshold */ 28872d1100bSBartlomiej Zolnierkiewicz rising_threshold = readl(data->base + EXYNOS_THD_TEMP_RISE); 28972d1100bSBartlomiej Zolnierkiewicz rising_threshold = get_th_reg(data, rising_threshold, false); 29072d1100bSBartlomiej Zolnierkiewicz writel(rising_threshold, data->base + EXYNOS_THD_TEMP_RISE); 29172d1100bSBartlomiej Zolnierkiewicz writel(get_th_reg(data, 0, true), data->base + EXYNOS_THD_TEMP_FALL); 29272d1100bSBartlomiej Zolnierkiewicz 293a7331f72SBartlomiej Zolnierkiewicz data->tmu_clear_irqs(data); 29472d1100bSBartlomiej Zolnierkiewicz 29572d1100bSBartlomiej Zolnierkiewicz /* if last threshold limit is also present */ 29672d1100bSBartlomiej Zolnierkiewicz i = pdata->max_trigger_level - 1; 29772d1100bSBartlomiej Zolnierkiewicz if (pdata->trigger_levels[i] && pdata->trigger_type[i] == HW_TRIP) { 29872d1100bSBartlomiej Zolnierkiewicz threshold_code = temp_to_code(data, pdata->trigger_levels[i]); 29972d1100bSBartlomiej Zolnierkiewicz /* 1-4 level to be assigned in th0 reg */ 30072d1100bSBartlomiej Zolnierkiewicz rising_threshold &= ~(0xff << 8 * i); 30172d1100bSBartlomiej Zolnierkiewicz rising_threshold |= threshold_code << 8 * i; 30272d1100bSBartlomiej Zolnierkiewicz writel(rising_threshold, data->base + EXYNOS_THD_TEMP_RISE); 30372d1100bSBartlomiej Zolnierkiewicz con = readl(data->base + EXYNOS_TMU_REG_CONTROL); 30472d1100bSBartlomiej Zolnierkiewicz con |= (1 << EXYNOS_TMU_THERM_TRIP_EN_SHIFT); 30572d1100bSBartlomiej Zolnierkiewicz writel(con, data->base + EXYNOS_TMU_REG_CONTROL); 30672d1100bSBartlomiej Zolnierkiewicz } 30772d1100bSBartlomiej Zolnierkiewicz out: 30872d1100bSBartlomiej Zolnierkiewicz return ret; 30972d1100bSBartlomiej Zolnierkiewicz } 31072d1100bSBartlomiej Zolnierkiewicz 31172d1100bSBartlomiej Zolnierkiewicz static int exynos5440_tmu_initialize(struct platform_device *pdev) 31272d1100bSBartlomiej Zolnierkiewicz { 31372d1100bSBartlomiej Zolnierkiewicz struct exynos_tmu_data *data = platform_get_drvdata(pdev); 31472d1100bSBartlomiej Zolnierkiewicz struct exynos_tmu_platform_data *pdata = data->pdata; 31572d1100bSBartlomiej Zolnierkiewicz unsigned int trim_info = 0, con, rising_threshold; 31672d1100bSBartlomiej Zolnierkiewicz int ret = 0, threshold_code, i; 31772d1100bSBartlomiej Zolnierkiewicz 31872d1100bSBartlomiej Zolnierkiewicz /* 31972d1100bSBartlomiej Zolnierkiewicz * For exynos5440 soc triminfo value is swapped between TMU0 and 32072d1100bSBartlomiej Zolnierkiewicz * TMU2, so the below logic is needed. 32172d1100bSBartlomiej Zolnierkiewicz */ 32272d1100bSBartlomiej Zolnierkiewicz switch (data->id) { 32372d1100bSBartlomiej Zolnierkiewicz case 0: 32472d1100bSBartlomiej Zolnierkiewicz trim_info = readl(data->base + EXYNOS5440_EFUSE_SWAP_OFFSET + 32572d1100bSBartlomiej Zolnierkiewicz EXYNOS5440_TMU_S0_7_TRIM); 32672d1100bSBartlomiej Zolnierkiewicz break; 32772d1100bSBartlomiej Zolnierkiewicz case 1: 32872d1100bSBartlomiej Zolnierkiewicz trim_info = readl(data->base + EXYNOS5440_TMU_S0_7_TRIM); 32972d1100bSBartlomiej Zolnierkiewicz break; 33072d1100bSBartlomiej Zolnierkiewicz case 2: 33172d1100bSBartlomiej Zolnierkiewicz trim_info = readl(data->base - EXYNOS5440_EFUSE_SWAP_OFFSET + 33272d1100bSBartlomiej Zolnierkiewicz EXYNOS5440_TMU_S0_7_TRIM); 33372d1100bSBartlomiej Zolnierkiewicz } 33472d1100bSBartlomiej Zolnierkiewicz sanitize_temp_error(data, trim_info); 33572d1100bSBartlomiej Zolnierkiewicz 33672d1100bSBartlomiej Zolnierkiewicz /* Write temperature code for rising and falling threshold */ 33772d1100bSBartlomiej Zolnierkiewicz rising_threshold = readl(data->base + EXYNOS5440_TMU_S0_7_TH0); 33872d1100bSBartlomiej Zolnierkiewicz rising_threshold = get_th_reg(data, rising_threshold, false); 33972d1100bSBartlomiej Zolnierkiewicz writel(rising_threshold, data->base + EXYNOS5440_TMU_S0_7_TH0); 34072d1100bSBartlomiej Zolnierkiewicz writel(0, data->base + EXYNOS5440_TMU_S0_7_TH1); 34172d1100bSBartlomiej Zolnierkiewicz 342a7331f72SBartlomiej Zolnierkiewicz data->tmu_clear_irqs(data); 34372d1100bSBartlomiej Zolnierkiewicz 34472d1100bSBartlomiej Zolnierkiewicz /* if last threshold limit is also present */ 34572d1100bSBartlomiej Zolnierkiewicz i = pdata->max_trigger_level - 1; 34672d1100bSBartlomiej Zolnierkiewicz if (pdata->trigger_levels[i] && pdata->trigger_type[i] == HW_TRIP) { 34772d1100bSBartlomiej Zolnierkiewicz threshold_code = temp_to_code(data, pdata->trigger_levels[i]); 34872d1100bSBartlomiej Zolnierkiewicz /* 5th level to be assigned in th2 reg */ 34972d1100bSBartlomiej Zolnierkiewicz rising_threshold = 35072d1100bSBartlomiej Zolnierkiewicz threshold_code << EXYNOS5440_TMU_TH_RISE4_SHIFT; 35172d1100bSBartlomiej Zolnierkiewicz writel(rising_threshold, data->base + EXYNOS5440_TMU_S0_7_TH2); 35272d1100bSBartlomiej Zolnierkiewicz con = readl(data->base + EXYNOS5440_TMU_S0_7_CTRL); 35372d1100bSBartlomiej Zolnierkiewicz con |= (1 << EXYNOS_TMU_THERM_TRIP_EN_SHIFT); 35472d1100bSBartlomiej Zolnierkiewicz writel(con, data->base + EXYNOS5440_TMU_S0_7_CTRL); 35572d1100bSBartlomiej Zolnierkiewicz } 35672d1100bSBartlomiej Zolnierkiewicz /* Clear the PMIN in the common TMU register */ 35772d1100bSBartlomiej Zolnierkiewicz if (!data->id) 35872d1100bSBartlomiej Zolnierkiewicz writel(0, data->base_second + EXYNOS5440_TMU_PMIN); 35972d1100bSBartlomiej Zolnierkiewicz return ret; 36072d1100bSBartlomiej Zolnierkiewicz } 36172d1100bSBartlomiej Zolnierkiewicz 36237f9034fSBartlomiej Zolnierkiewicz static void exynos4210_tmu_control(struct platform_device *pdev, bool on) 36337f9034fSBartlomiej Zolnierkiewicz { 36437f9034fSBartlomiej Zolnierkiewicz struct exynos_tmu_data *data = platform_get_drvdata(pdev); 36537f9034fSBartlomiej Zolnierkiewicz struct exynos_tmu_platform_data *pdata = data->pdata; 36637f9034fSBartlomiej Zolnierkiewicz unsigned int con, interrupt_en; 36737f9034fSBartlomiej Zolnierkiewicz 36837f9034fSBartlomiej Zolnierkiewicz con = get_con_reg(data, readl(data->base + EXYNOS_TMU_REG_CONTROL)); 36937f9034fSBartlomiej Zolnierkiewicz 37037f9034fSBartlomiej Zolnierkiewicz if (on) { 37137f9034fSBartlomiej Zolnierkiewicz con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT); 37237f9034fSBartlomiej Zolnierkiewicz interrupt_en = 37337f9034fSBartlomiej Zolnierkiewicz pdata->trigger_enable[3] << EXYNOS_TMU_INTEN_RISE3_SHIFT | 37437f9034fSBartlomiej Zolnierkiewicz pdata->trigger_enable[2] << EXYNOS_TMU_INTEN_RISE2_SHIFT | 37537f9034fSBartlomiej Zolnierkiewicz pdata->trigger_enable[1] << EXYNOS_TMU_INTEN_RISE1_SHIFT | 37637f9034fSBartlomiej Zolnierkiewicz pdata->trigger_enable[0] << EXYNOS_TMU_INTEN_RISE0_SHIFT; 377e0761533SBartlomiej Zolnierkiewicz if (data->soc != SOC_ARCH_EXYNOS4210) 37837f9034fSBartlomiej Zolnierkiewicz interrupt_en |= 37937f9034fSBartlomiej Zolnierkiewicz interrupt_en << EXYNOS_TMU_INTEN_FALL0_SHIFT; 38037f9034fSBartlomiej Zolnierkiewicz } else { 38137f9034fSBartlomiej Zolnierkiewicz con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT); 38237f9034fSBartlomiej Zolnierkiewicz interrupt_en = 0; /* Disable all interrupts */ 38337f9034fSBartlomiej Zolnierkiewicz } 38437f9034fSBartlomiej Zolnierkiewicz writel(interrupt_en, data->base + EXYNOS_TMU_REG_INTEN); 38537f9034fSBartlomiej Zolnierkiewicz writel(con, data->base + EXYNOS_TMU_REG_CONTROL); 38637f9034fSBartlomiej Zolnierkiewicz } 38737f9034fSBartlomiej Zolnierkiewicz 38837f9034fSBartlomiej Zolnierkiewicz static void exynos5440_tmu_control(struct platform_device *pdev, bool on) 38937f9034fSBartlomiej Zolnierkiewicz { 39037f9034fSBartlomiej Zolnierkiewicz struct exynos_tmu_data *data = platform_get_drvdata(pdev); 39137f9034fSBartlomiej Zolnierkiewicz struct exynos_tmu_platform_data *pdata = data->pdata; 39237f9034fSBartlomiej Zolnierkiewicz unsigned int con, interrupt_en; 39337f9034fSBartlomiej Zolnierkiewicz 39437f9034fSBartlomiej Zolnierkiewicz con = get_con_reg(data, readl(data->base + EXYNOS5440_TMU_S0_7_CTRL)); 39537f9034fSBartlomiej Zolnierkiewicz 39637f9034fSBartlomiej Zolnierkiewicz if (on) { 39737f9034fSBartlomiej Zolnierkiewicz con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT); 39837f9034fSBartlomiej Zolnierkiewicz interrupt_en = 39937f9034fSBartlomiej Zolnierkiewicz pdata->trigger_enable[3] << EXYNOS5440_TMU_INTEN_RISE3_SHIFT | 40037f9034fSBartlomiej Zolnierkiewicz pdata->trigger_enable[2] << EXYNOS5440_TMU_INTEN_RISE2_SHIFT | 40137f9034fSBartlomiej Zolnierkiewicz pdata->trigger_enable[1] << EXYNOS5440_TMU_INTEN_RISE1_SHIFT | 40237f9034fSBartlomiej Zolnierkiewicz pdata->trigger_enable[0] << EXYNOS5440_TMU_INTEN_RISE0_SHIFT; 403e0761533SBartlomiej Zolnierkiewicz interrupt_en |= interrupt_en << EXYNOS5440_TMU_INTEN_FALL0_SHIFT; 40437f9034fSBartlomiej Zolnierkiewicz } else { 40537f9034fSBartlomiej Zolnierkiewicz con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT); 40637f9034fSBartlomiej Zolnierkiewicz interrupt_en = 0; /* Disable all interrupts */ 40737f9034fSBartlomiej Zolnierkiewicz } 40837f9034fSBartlomiej Zolnierkiewicz writel(interrupt_en, data->base + EXYNOS5440_TMU_S0_7_IRQEN); 40937f9034fSBartlomiej Zolnierkiewicz writel(con, data->base + EXYNOS5440_TMU_S0_7_CTRL); 41037f9034fSBartlomiej Zolnierkiewicz } 41137f9034fSBartlomiej Zolnierkiewicz 41259dfa54cSAmit Daniel Kachhap static int exynos_tmu_read(struct exynos_tmu_data *data) 41359dfa54cSAmit Daniel Kachhap { 414b79985caSBartlomiej Zolnierkiewicz int ret; 41559dfa54cSAmit Daniel Kachhap 41659dfa54cSAmit Daniel Kachhap mutex_lock(&data->lock); 41759dfa54cSAmit Daniel Kachhap clk_enable(data->clk); 418b79985caSBartlomiej Zolnierkiewicz ret = data->tmu_read(data); 419b79985caSBartlomiej Zolnierkiewicz if (ret >= 0) 420b79985caSBartlomiej Zolnierkiewicz ret = code_to_temp(data, ret); 42159dfa54cSAmit Daniel Kachhap clk_disable(data->clk); 42259dfa54cSAmit Daniel Kachhap mutex_unlock(&data->lock); 42359dfa54cSAmit Daniel Kachhap 424b79985caSBartlomiej Zolnierkiewicz return ret; 42559dfa54cSAmit Daniel Kachhap } 42659dfa54cSAmit Daniel Kachhap 42759dfa54cSAmit Daniel Kachhap #ifdef CONFIG_THERMAL_EMULATION 428154013eaSBartlomiej Zolnierkiewicz static u32 get_emul_con_reg(struct exynos_tmu_data *data, unsigned int val, 429154013eaSBartlomiej Zolnierkiewicz unsigned long temp) 430154013eaSBartlomiej Zolnierkiewicz { 431154013eaSBartlomiej Zolnierkiewicz if (temp) { 432154013eaSBartlomiej Zolnierkiewicz temp /= MCELSIUS; 433154013eaSBartlomiej Zolnierkiewicz 434d564b55aSBartlomiej Zolnierkiewicz if (data->soc != SOC_ARCH_EXYNOS5440) { 435154013eaSBartlomiej Zolnierkiewicz val &= ~(EXYNOS_EMUL_TIME_MASK << EXYNOS_EMUL_TIME_SHIFT); 436154013eaSBartlomiej Zolnierkiewicz val |= (EXYNOS_EMUL_TIME << EXYNOS_EMUL_TIME_SHIFT); 437154013eaSBartlomiej Zolnierkiewicz } 438154013eaSBartlomiej Zolnierkiewicz val &= ~(EXYNOS_EMUL_DATA_MASK << EXYNOS_EMUL_DATA_SHIFT); 439154013eaSBartlomiej Zolnierkiewicz val |= (temp_to_code(data, temp) << EXYNOS_EMUL_DATA_SHIFT) | 440154013eaSBartlomiej Zolnierkiewicz EXYNOS_EMUL_ENABLE; 441154013eaSBartlomiej Zolnierkiewicz } else { 442154013eaSBartlomiej Zolnierkiewicz val &= ~EXYNOS_EMUL_ENABLE; 443154013eaSBartlomiej Zolnierkiewicz } 444154013eaSBartlomiej Zolnierkiewicz 445154013eaSBartlomiej Zolnierkiewicz return val; 446154013eaSBartlomiej Zolnierkiewicz } 447154013eaSBartlomiej Zolnierkiewicz 448285d994aSBartlomiej Zolnierkiewicz static void exynos4412_tmu_set_emulation(struct exynos_tmu_data *data, 449285d994aSBartlomiej Zolnierkiewicz unsigned long temp) 450285d994aSBartlomiej Zolnierkiewicz { 451285d994aSBartlomiej Zolnierkiewicz unsigned int val; 452285d994aSBartlomiej Zolnierkiewicz u32 emul_con; 453285d994aSBartlomiej Zolnierkiewicz 454285d994aSBartlomiej Zolnierkiewicz if (data->soc == SOC_ARCH_EXYNOS5260) 455285d994aSBartlomiej Zolnierkiewicz emul_con = EXYNOS5260_EMUL_CON; 456285d994aSBartlomiej Zolnierkiewicz else 457285d994aSBartlomiej Zolnierkiewicz emul_con = EXYNOS_EMUL_CON; 458285d994aSBartlomiej Zolnierkiewicz 459285d994aSBartlomiej Zolnierkiewicz val = readl(data->base + emul_con); 460285d994aSBartlomiej Zolnierkiewicz val = get_emul_con_reg(data, val, temp); 461285d994aSBartlomiej Zolnierkiewicz writel(val, data->base + emul_con); 462285d994aSBartlomiej Zolnierkiewicz } 463285d994aSBartlomiej Zolnierkiewicz 464285d994aSBartlomiej Zolnierkiewicz static void exynos5440_tmu_set_emulation(struct exynos_tmu_data *data, 465285d994aSBartlomiej Zolnierkiewicz unsigned long temp) 466285d994aSBartlomiej Zolnierkiewicz { 467285d994aSBartlomiej Zolnierkiewicz unsigned int val; 468285d994aSBartlomiej Zolnierkiewicz 469285d994aSBartlomiej Zolnierkiewicz val = readl(data->base + EXYNOS5440_TMU_S0_7_DEBUG); 470285d994aSBartlomiej Zolnierkiewicz val = get_emul_con_reg(data, val, temp); 471285d994aSBartlomiej Zolnierkiewicz writel(val, data->base + EXYNOS5440_TMU_S0_7_DEBUG); 472285d994aSBartlomiej Zolnierkiewicz } 473285d994aSBartlomiej Zolnierkiewicz 47459dfa54cSAmit Daniel Kachhap static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp) 47559dfa54cSAmit Daniel Kachhap { 47659dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = drv_data; 47759dfa54cSAmit Daniel Kachhap int ret = -EINVAL; 47859dfa54cSAmit Daniel Kachhap 479ef3f80fcSBartlomiej Zolnierkiewicz if (data->soc == SOC_ARCH_EXYNOS4210) 48059dfa54cSAmit Daniel Kachhap goto out; 48159dfa54cSAmit Daniel Kachhap 48259dfa54cSAmit Daniel Kachhap if (temp && temp < MCELSIUS) 48359dfa54cSAmit Daniel Kachhap goto out; 48459dfa54cSAmit Daniel Kachhap 48559dfa54cSAmit Daniel Kachhap mutex_lock(&data->lock); 48659dfa54cSAmit Daniel Kachhap clk_enable(data->clk); 487285d994aSBartlomiej Zolnierkiewicz data->tmu_set_emulation(data, temp); 48859dfa54cSAmit Daniel Kachhap clk_disable(data->clk); 48959dfa54cSAmit Daniel Kachhap mutex_unlock(&data->lock); 49059dfa54cSAmit Daniel Kachhap return 0; 49159dfa54cSAmit Daniel Kachhap out: 49259dfa54cSAmit Daniel Kachhap return ret; 49359dfa54cSAmit Daniel Kachhap } 49459dfa54cSAmit Daniel Kachhap #else 495285d994aSBartlomiej Zolnierkiewicz #define exynos4412_tmu_set_emulation NULL 496285d994aSBartlomiej Zolnierkiewicz #define exynos5440_tmu_set_emulation NULL 49759dfa54cSAmit Daniel Kachhap static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp) 49859dfa54cSAmit Daniel Kachhap { return -EINVAL; } 49959dfa54cSAmit Daniel Kachhap #endif/*CONFIG_THERMAL_EMULATION*/ 50059dfa54cSAmit Daniel Kachhap 501b79985caSBartlomiej Zolnierkiewicz static int exynos4210_tmu_read(struct exynos_tmu_data *data) 502b79985caSBartlomiej Zolnierkiewicz { 503b79985caSBartlomiej Zolnierkiewicz int ret = readb(data->base + EXYNOS_TMU_REG_CURRENT_TEMP); 504b79985caSBartlomiej Zolnierkiewicz 505b79985caSBartlomiej Zolnierkiewicz /* "temp_code" should range between 75 and 175 */ 506b79985caSBartlomiej Zolnierkiewicz return (ret < 75 || ret > 175) ? -ENODATA : ret; 507b79985caSBartlomiej Zolnierkiewicz } 508b79985caSBartlomiej Zolnierkiewicz 509b79985caSBartlomiej Zolnierkiewicz static int exynos4412_tmu_read(struct exynos_tmu_data *data) 510b79985caSBartlomiej Zolnierkiewicz { 511b79985caSBartlomiej Zolnierkiewicz return readb(data->base + EXYNOS_TMU_REG_CURRENT_TEMP); 512b79985caSBartlomiej Zolnierkiewicz } 513b79985caSBartlomiej Zolnierkiewicz 514b79985caSBartlomiej Zolnierkiewicz static int exynos5440_tmu_read(struct exynos_tmu_data *data) 515b79985caSBartlomiej Zolnierkiewicz { 516b79985caSBartlomiej Zolnierkiewicz return readb(data->base + EXYNOS5440_TMU_S0_7_TEMP); 517b79985caSBartlomiej Zolnierkiewicz } 518b79985caSBartlomiej Zolnierkiewicz 51959dfa54cSAmit Daniel Kachhap static void exynos_tmu_work(struct work_struct *work) 52059dfa54cSAmit Daniel Kachhap { 52159dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = container_of(work, 52259dfa54cSAmit Daniel Kachhap struct exynos_tmu_data, irq_work); 523b835ced1SBartlomiej Zolnierkiewicz unsigned int val_type; 524a0395eeeSAmit Daniel Kachhap 52514a11dc7SNaveen Krishna Chatradhi if (!IS_ERR(data->clk_sec)) 52614a11dc7SNaveen Krishna Chatradhi clk_enable(data->clk_sec); 527a0395eeeSAmit Daniel Kachhap /* Find which sensor generated this interrupt */ 528421d5d12SBartlomiej Zolnierkiewicz if (data->soc == SOC_ARCH_EXYNOS5440) { 529421d5d12SBartlomiej Zolnierkiewicz val_type = readl(data->base_second + EXYNOS5440_TMU_IRQ_STATUS); 530a0395eeeSAmit Daniel Kachhap if (!((val_type >> data->id) & 0x1)) 531a0395eeeSAmit Daniel Kachhap goto out; 532a0395eeeSAmit Daniel Kachhap } 53314a11dc7SNaveen Krishna Chatradhi if (!IS_ERR(data->clk_sec)) 53414a11dc7SNaveen Krishna Chatradhi clk_disable(data->clk_sec); 53559dfa54cSAmit Daniel Kachhap 536cebe7373SAmit Daniel Kachhap exynos_report_trigger(data->reg_conf); 53759dfa54cSAmit Daniel Kachhap mutex_lock(&data->lock); 53859dfa54cSAmit Daniel Kachhap clk_enable(data->clk); 539b8d582b9SAmit Daniel Kachhap 540a4463c4fSAmit Daniel Kachhap /* TODO: take action based on particular interrupt */ 541a7331f72SBartlomiej Zolnierkiewicz data->tmu_clear_irqs(data); 542b8d582b9SAmit Daniel Kachhap 54359dfa54cSAmit Daniel Kachhap clk_disable(data->clk); 54459dfa54cSAmit Daniel Kachhap mutex_unlock(&data->lock); 545a0395eeeSAmit Daniel Kachhap out: 54659dfa54cSAmit Daniel Kachhap enable_irq(data->irq); 54759dfa54cSAmit Daniel Kachhap } 54859dfa54cSAmit Daniel Kachhap 549a7331f72SBartlomiej Zolnierkiewicz static void exynos4210_tmu_clear_irqs(struct exynos_tmu_data *data) 550a7331f72SBartlomiej Zolnierkiewicz { 551a7331f72SBartlomiej Zolnierkiewicz unsigned int val_irq; 552a7331f72SBartlomiej Zolnierkiewicz u32 tmu_intstat, tmu_intclear; 553a7331f72SBartlomiej Zolnierkiewicz 554a7331f72SBartlomiej Zolnierkiewicz if (data->soc == SOC_ARCH_EXYNOS5260) { 555a7331f72SBartlomiej Zolnierkiewicz tmu_intstat = EXYNOS5260_TMU_REG_INTSTAT; 556a7331f72SBartlomiej Zolnierkiewicz tmu_intclear = EXYNOS5260_TMU_REG_INTCLEAR; 557a7331f72SBartlomiej Zolnierkiewicz } else { 558a7331f72SBartlomiej Zolnierkiewicz tmu_intstat = EXYNOS_TMU_REG_INTSTAT; 559a7331f72SBartlomiej Zolnierkiewicz tmu_intclear = EXYNOS_TMU_REG_INTCLEAR; 560a7331f72SBartlomiej Zolnierkiewicz } 561a7331f72SBartlomiej Zolnierkiewicz 562a7331f72SBartlomiej Zolnierkiewicz val_irq = readl(data->base + tmu_intstat); 563a7331f72SBartlomiej Zolnierkiewicz /* 564a7331f72SBartlomiej Zolnierkiewicz * Clear the interrupts. Please note that the documentation for 565a7331f72SBartlomiej Zolnierkiewicz * Exynos3250, Exynos4412, Exynos5250 and Exynos5260 incorrectly 566a7331f72SBartlomiej Zolnierkiewicz * states that INTCLEAR register has a different placing of bits 567a7331f72SBartlomiej Zolnierkiewicz * responsible for FALL IRQs than INTSTAT register. Exynos5420 568a7331f72SBartlomiej Zolnierkiewicz * and Exynos5440 documentation is correct (Exynos4210 doesn't 569a7331f72SBartlomiej Zolnierkiewicz * support FALL IRQs at all). 570a7331f72SBartlomiej Zolnierkiewicz */ 571a7331f72SBartlomiej Zolnierkiewicz writel(val_irq, data->base + tmu_intclear); 572a7331f72SBartlomiej Zolnierkiewicz } 573a7331f72SBartlomiej Zolnierkiewicz 574a7331f72SBartlomiej Zolnierkiewicz static void exynos5440_tmu_clear_irqs(struct exynos_tmu_data *data) 575a7331f72SBartlomiej Zolnierkiewicz { 576a7331f72SBartlomiej Zolnierkiewicz unsigned int val_irq; 577a7331f72SBartlomiej Zolnierkiewicz 578a7331f72SBartlomiej Zolnierkiewicz val_irq = readl(data->base + EXYNOS5440_TMU_S0_7_IRQ); 579a7331f72SBartlomiej Zolnierkiewicz /* clear the interrupts */ 580a7331f72SBartlomiej Zolnierkiewicz writel(val_irq, data->base + EXYNOS5440_TMU_S0_7_IRQ); 581a7331f72SBartlomiej Zolnierkiewicz } 582a7331f72SBartlomiej Zolnierkiewicz 58359dfa54cSAmit Daniel Kachhap static irqreturn_t exynos_tmu_irq(int irq, void *id) 58459dfa54cSAmit Daniel Kachhap { 58559dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = id; 58659dfa54cSAmit Daniel Kachhap 58759dfa54cSAmit Daniel Kachhap disable_irq_nosync(irq); 58859dfa54cSAmit Daniel Kachhap schedule_work(&data->irq_work); 58959dfa54cSAmit Daniel Kachhap 59059dfa54cSAmit Daniel Kachhap return IRQ_HANDLED; 59159dfa54cSAmit Daniel Kachhap } 59259dfa54cSAmit Daniel Kachhap 59359dfa54cSAmit Daniel Kachhap static const struct of_device_id exynos_tmu_match[] = { 59459dfa54cSAmit Daniel Kachhap { 5951fe56dc1SChanwoo Choi .compatible = "samsung,exynos3250-tmu", 5961fe56dc1SChanwoo Choi .data = (void *)EXYNOS3250_TMU_DRV_DATA, 5971fe56dc1SChanwoo Choi }, 5981fe56dc1SChanwoo Choi { 59959dfa54cSAmit Daniel Kachhap .compatible = "samsung,exynos4210-tmu", 60059dfa54cSAmit Daniel Kachhap .data = (void *)EXYNOS4210_TMU_DRV_DATA, 60159dfa54cSAmit Daniel Kachhap }, 60259dfa54cSAmit Daniel Kachhap { 60359dfa54cSAmit Daniel Kachhap .compatible = "samsung,exynos4412-tmu", 60414ddfaecSLukasz Majewski .data = (void *)EXYNOS4412_TMU_DRV_DATA, 60559dfa54cSAmit Daniel Kachhap }, 60659dfa54cSAmit Daniel Kachhap { 60759dfa54cSAmit Daniel Kachhap .compatible = "samsung,exynos5250-tmu", 608e6b7991eSAmit Daniel Kachhap .data = (void *)EXYNOS5250_TMU_DRV_DATA, 60959dfa54cSAmit Daniel Kachhap }, 61090542546SAmit Daniel Kachhap { 611923488a5SNaveen Krishna Chatradhi .compatible = "samsung,exynos5260-tmu", 612923488a5SNaveen Krishna Chatradhi .data = (void *)EXYNOS5260_TMU_DRV_DATA, 613923488a5SNaveen Krishna Chatradhi }, 614923488a5SNaveen Krishna Chatradhi { 61514a11dc7SNaveen Krishna Chatradhi .compatible = "samsung,exynos5420-tmu", 61614a11dc7SNaveen Krishna Chatradhi .data = (void *)EXYNOS5420_TMU_DRV_DATA, 61714a11dc7SNaveen Krishna Chatradhi }, 61814a11dc7SNaveen Krishna Chatradhi { 61914a11dc7SNaveen Krishna Chatradhi .compatible = "samsung,exynos5420-tmu-ext-triminfo", 62014a11dc7SNaveen Krishna Chatradhi .data = (void *)EXYNOS5420_TMU_DRV_DATA, 62114a11dc7SNaveen Krishna Chatradhi }, 62214a11dc7SNaveen Krishna Chatradhi { 62390542546SAmit Daniel Kachhap .compatible = "samsung,exynos5440-tmu", 62490542546SAmit Daniel Kachhap .data = (void *)EXYNOS5440_TMU_DRV_DATA, 62590542546SAmit Daniel Kachhap }, 62659dfa54cSAmit Daniel Kachhap {}, 62759dfa54cSAmit Daniel Kachhap }; 62859dfa54cSAmit Daniel Kachhap MODULE_DEVICE_TABLE(of, exynos_tmu_match); 62959dfa54cSAmit Daniel Kachhap 63059dfa54cSAmit Daniel Kachhap static inline struct exynos_tmu_platform_data *exynos_get_driver_data( 631cebe7373SAmit Daniel Kachhap struct platform_device *pdev, int id) 63259dfa54cSAmit Daniel Kachhap { 633cebe7373SAmit Daniel Kachhap struct exynos_tmu_init_data *data_table; 634cebe7373SAmit Daniel Kachhap struct exynos_tmu_platform_data *tmu_data; 63559dfa54cSAmit Daniel Kachhap const struct of_device_id *match; 63673b5b1d7SSachin Kamat 63759dfa54cSAmit Daniel Kachhap match = of_match_node(exynos_tmu_match, pdev->dev.of_node); 63859dfa54cSAmit Daniel Kachhap if (!match) 63959dfa54cSAmit Daniel Kachhap return NULL; 640cebe7373SAmit Daniel Kachhap data_table = (struct exynos_tmu_init_data *) match->data; 641cebe7373SAmit Daniel Kachhap if (!data_table || id >= data_table->tmu_count) 642cebe7373SAmit Daniel Kachhap return NULL; 643cebe7373SAmit Daniel Kachhap tmu_data = data_table->tmu_data; 644cebe7373SAmit Daniel Kachhap return (struct exynos_tmu_platform_data *) (tmu_data + id); 64559dfa54cSAmit Daniel Kachhap } 64659dfa54cSAmit Daniel Kachhap 647cebe7373SAmit Daniel Kachhap static int exynos_map_dt_data(struct platform_device *pdev) 64859dfa54cSAmit Daniel Kachhap { 649cebe7373SAmit Daniel Kachhap struct exynos_tmu_data *data = platform_get_drvdata(pdev); 650cebe7373SAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata; 651cebe7373SAmit Daniel Kachhap struct resource res; 652498d22f6SAmit Daniel Kachhap int ret; 65359dfa54cSAmit Daniel Kachhap 65473b5b1d7SSachin Kamat if (!data || !pdev->dev.of_node) 655cebe7373SAmit Daniel Kachhap return -ENODEV; 65659dfa54cSAmit Daniel Kachhap 657498d22f6SAmit Daniel Kachhap /* 658498d22f6SAmit Daniel Kachhap * Try enabling the regulator if found 659498d22f6SAmit Daniel Kachhap * TODO: Add regulator as an SOC feature, so that regulator enable 660498d22f6SAmit Daniel Kachhap * is a compulsory call. 661498d22f6SAmit Daniel Kachhap */ 662498d22f6SAmit Daniel Kachhap data->regulator = devm_regulator_get(&pdev->dev, "vtmu"); 663498d22f6SAmit Daniel Kachhap if (!IS_ERR(data->regulator)) { 664498d22f6SAmit Daniel Kachhap ret = regulator_enable(data->regulator); 665498d22f6SAmit Daniel Kachhap if (ret) { 666498d22f6SAmit Daniel Kachhap dev_err(&pdev->dev, "failed to enable vtmu\n"); 667498d22f6SAmit Daniel Kachhap return ret; 668498d22f6SAmit Daniel Kachhap } 669498d22f6SAmit Daniel Kachhap } else { 670498d22f6SAmit Daniel Kachhap dev_info(&pdev->dev, "Regulator node (vtmu) not found\n"); 671498d22f6SAmit Daniel Kachhap } 672498d22f6SAmit Daniel Kachhap 673cebe7373SAmit Daniel Kachhap data->id = of_alias_get_id(pdev->dev.of_node, "tmuctrl"); 674cebe7373SAmit Daniel Kachhap if (data->id < 0) 675cebe7373SAmit Daniel Kachhap data->id = 0; 676cebe7373SAmit Daniel Kachhap 677cebe7373SAmit Daniel Kachhap data->irq = irq_of_parse_and_map(pdev->dev.of_node, 0); 678cebe7373SAmit Daniel Kachhap if (data->irq <= 0) { 679cebe7373SAmit Daniel Kachhap dev_err(&pdev->dev, "failed to get IRQ\n"); 680cebe7373SAmit Daniel Kachhap return -ENODEV; 681cebe7373SAmit Daniel Kachhap } 682cebe7373SAmit Daniel Kachhap 683cebe7373SAmit Daniel Kachhap if (of_address_to_resource(pdev->dev.of_node, 0, &res)) { 684cebe7373SAmit Daniel Kachhap dev_err(&pdev->dev, "failed to get Resource 0\n"); 685cebe7373SAmit Daniel Kachhap return -ENODEV; 686cebe7373SAmit Daniel Kachhap } 687cebe7373SAmit Daniel Kachhap 688cebe7373SAmit Daniel Kachhap data->base = devm_ioremap(&pdev->dev, res.start, resource_size(&res)); 689cebe7373SAmit Daniel Kachhap if (!data->base) { 690cebe7373SAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to ioremap memory\n"); 691cebe7373SAmit Daniel Kachhap return -EADDRNOTAVAIL; 692cebe7373SAmit Daniel Kachhap } 693cebe7373SAmit Daniel Kachhap 694cebe7373SAmit Daniel Kachhap pdata = exynos_get_driver_data(pdev, data->id); 69559dfa54cSAmit Daniel Kachhap if (!pdata) { 69659dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "No platform init data supplied.\n"); 69759dfa54cSAmit Daniel Kachhap return -ENODEV; 69859dfa54cSAmit Daniel Kachhap } 69956adb9efSBartlomiej Zolnierkiewicz 700cebe7373SAmit Daniel Kachhap data->pdata = pdata; 70156adb9efSBartlomiej Zolnierkiewicz data->soc = pdata->type; 70256adb9efSBartlomiej Zolnierkiewicz 70356adb9efSBartlomiej Zolnierkiewicz switch (data->soc) { 70456adb9efSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS4210: 70556adb9efSBartlomiej Zolnierkiewicz data->tmu_initialize = exynos4210_tmu_initialize; 70656adb9efSBartlomiej Zolnierkiewicz data->tmu_control = exynos4210_tmu_control; 70756adb9efSBartlomiej Zolnierkiewicz data->tmu_read = exynos4210_tmu_read; 70856adb9efSBartlomiej Zolnierkiewicz data->tmu_clear_irqs = exynos4210_tmu_clear_irqs; 70956adb9efSBartlomiej Zolnierkiewicz break; 71056adb9efSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS3250: 71156adb9efSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS4412: 71256adb9efSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS5250: 71356adb9efSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS5260: 71456adb9efSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS5420: 71556adb9efSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS5420_TRIMINFO: 71656adb9efSBartlomiej Zolnierkiewicz data->tmu_initialize = exynos4412_tmu_initialize; 71756adb9efSBartlomiej Zolnierkiewicz data->tmu_control = exynos4210_tmu_control; 71856adb9efSBartlomiej Zolnierkiewicz data->tmu_read = exynos4412_tmu_read; 71956adb9efSBartlomiej Zolnierkiewicz data->tmu_set_emulation = exynos4412_tmu_set_emulation; 72056adb9efSBartlomiej Zolnierkiewicz data->tmu_clear_irqs = exynos4210_tmu_clear_irqs; 72156adb9efSBartlomiej Zolnierkiewicz break; 72256adb9efSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS5440: 72356adb9efSBartlomiej Zolnierkiewicz data->tmu_initialize = exynos5440_tmu_initialize; 72456adb9efSBartlomiej Zolnierkiewicz data->tmu_control = exynos5440_tmu_control; 72556adb9efSBartlomiej Zolnierkiewicz data->tmu_read = exynos5440_tmu_read; 72656adb9efSBartlomiej Zolnierkiewicz data->tmu_set_emulation = exynos5440_tmu_set_emulation; 72756adb9efSBartlomiej Zolnierkiewicz data->tmu_clear_irqs = exynos5440_tmu_clear_irqs; 72856adb9efSBartlomiej Zolnierkiewicz break; 72956adb9efSBartlomiej Zolnierkiewicz default: 73056adb9efSBartlomiej Zolnierkiewicz dev_err(&pdev->dev, "Platform not supported\n"); 73156adb9efSBartlomiej Zolnierkiewicz return -EINVAL; 73256adb9efSBartlomiej Zolnierkiewicz } 73356adb9efSBartlomiej Zolnierkiewicz 734d9b6ee14SAmit Daniel Kachhap /* 735d9b6ee14SAmit Daniel Kachhap * Check if the TMU shares some registers and then try to map the 736d9b6ee14SAmit Daniel Kachhap * memory of common registers. 737d9b6ee14SAmit Daniel Kachhap */ 73856adb9efSBartlomiej Zolnierkiewicz if (data->soc != SOC_ARCH_EXYNOS5420_TRIMINFO && 73956adb9efSBartlomiej Zolnierkiewicz data->soc != SOC_ARCH_EXYNOS5440) 740d9b6ee14SAmit Daniel Kachhap return 0; 741d9b6ee14SAmit Daniel Kachhap 742d9b6ee14SAmit Daniel Kachhap if (of_address_to_resource(pdev->dev.of_node, 1, &res)) { 743d9b6ee14SAmit Daniel Kachhap dev_err(&pdev->dev, "failed to get Resource 1\n"); 744d9b6ee14SAmit Daniel Kachhap return -ENODEV; 745d9b6ee14SAmit Daniel Kachhap } 746d9b6ee14SAmit Daniel Kachhap 7479025d563SNaveen Krishna Chatradhi data->base_second = devm_ioremap(&pdev->dev, res.start, 748d9b6ee14SAmit Daniel Kachhap resource_size(&res)); 7499025d563SNaveen Krishna Chatradhi if (!data->base_second) { 750d9b6ee14SAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to ioremap memory\n"); 751d9b6ee14SAmit Daniel Kachhap return -ENOMEM; 752d9b6ee14SAmit Daniel Kachhap } 753cebe7373SAmit Daniel Kachhap 754cebe7373SAmit Daniel Kachhap return 0; 755cebe7373SAmit Daniel Kachhap } 756cebe7373SAmit Daniel Kachhap 757cebe7373SAmit Daniel Kachhap static int exynos_tmu_probe(struct platform_device *pdev) 758cebe7373SAmit Daniel Kachhap { 759cebe7373SAmit Daniel Kachhap struct exynos_tmu_data *data; 760cebe7373SAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata; 761cebe7373SAmit Daniel Kachhap struct thermal_sensor_conf *sensor_conf; 762cebe7373SAmit Daniel Kachhap int ret, i; 763cebe7373SAmit Daniel Kachhap 76459dfa54cSAmit Daniel Kachhap data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data), 76559dfa54cSAmit Daniel Kachhap GFP_KERNEL); 7662a9675b3SJingoo Han if (!data) 76759dfa54cSAmit Daniel Kachhap return -ENOMEM; 76859dfa54cSAmit Daniel Kachhap 769cebe7373SAmit Daniel Kachhap platform_set_drvdata(pdev, data); 770cebe7373SAmit Daniel Kachhap mutex_init(&data->lock); 771cebe7373SAmit Daniel Kachhap 772cebe7373SAmit Daniel Kachhap ret = exynos_map_dt_data(pdev); 773cebe7373SAmit Daniel Kachhap if (ret) 774cebe7373SAmit Daniel Kachhap return ret; 775cebe7373SAmit Daniel Kachhap 776cebe7373SAmit Daniel Kachhap pdata = data->pdata; 77759dfa54cSAmit Daniel Kachhap 77859dfa54cSAmit Daniel Kachhap INIT_WORK(&data->irq_work, exynos_tmu_work); 77959dfa54cSAmit Daniel Kachhap 78059dfa54cSAmit Daniel Kachhap data->clk = devm_clk_get(&pdev->dev, "tmu_apbif"); 78159dfa54cSAmit Daniel Kachhap if (IS_ERR(data->clk)) { 78259dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to get clock\n"); 78359dfa54cSAmit Daniel Kachhap return PTR_ERR(data->clk); 78459dfa54cSAmit Daniel Kachhap } 78559dfa54cSAmit Daniel Kachhap 78614a11dc7SNaveen Krishna Chatradhi data->clk_sec = devm_clk_get(&pdev->dev, "tmu_triminfo_apbif"); 78714a11dc7SNaveen Krishna Chatradhi if (IS_ERR(data->clk_sec)) { 78814a11dc7SNaveen Krishna Chatradhi if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO) { 78914a11dc7SNaveen Krishna Chatradhi dev_err(&pdev->dev, "Failed to get triminfo clock\n"); 79014a11dc7SNaveen Krishna Chatradhi return PTR_ERR(data->clk_sec); 79114a11dc7SNaveen Krishna Chatradhi } 79214a11dc7SNaveen Krishna Chatradhi } else { 79314a11dc7SNaveen Krishna Chatradhi ret = clk_prepare(data->clk_sec); 79414a11dc7SNaveen Krishna Chatradhi if (ret) { 79514a11dc7SNaveen Krishna Chatradhi dev_err(&pdev->dev, "Failed to get clock\n"); 79659dfa54cSAmit Daniel Kachhap return ret; 79714a11dc7SNaveen Krishna Chatradhi } 79814a11dc7SNaveen Krishna Chatradhi } 79914a11dc7SNaveen Krishna Chatradhi 80014a11dc7SNaveen Krishna Chatradhi ret = clk_prepare(data->clk); 80114a11dc7SNaveen Krishna Chatradhi if (ret) { 80214a11dc7SNaveen Krishna Chatradhi dev_err(&pdev->dev, "Failed to get clock\n"); 80314a11dc7SNaveen Krishna Chatradhi goto err_clk_sec; 80414a11dc7SNaveen Krishna Chatradhi } 80559dfa54cSAmit Daniel Kachhap 80659dfa54cSAmit Daniel Kachhap ret = exynos_tmu_initialize(pdev); 80759dfa54cSAmit Daniel Kachhap if (ret) { 80859dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to initialize TMU\n"); 80959dfa54cSAmit Daniel Kachhap goto err_clk; 81059dfa54cSAmit Daniel Kachhap } 81159dfa54cSAmit Daniel Kachhap 81259dfa54cSAmit Daniel Kachhap exynos_tmu_control(pdev, true); 81359dfa54cSAmit Daniel Kachhap 814cebe7373SAmit Daniel Kachhap /* Allocate a structure to register with the exynos core thermal */ 815cebe7373SAmit Daniel Kachhap sensor_conf = devm_kzalloc(&pdev->dev, 816cebe7373SAmit Daniel Kachhap sizeof(struct thermal_sensor_conf), GFP_KERNEL); 817cebe7373SAmit Daniel Kachhap if (!sensor_conf) { 818cebe7373SAmit Daniel Kachhap ret = -ENOMEM; 819cebe7373SAmit Daniel Kachhap goto err_clk; 820cebe7373SAmit Daniel Kachhap } 821cebe7373SAmit Daniel Kachhap sprintf(sensor_conf->name, "therm_zone%d", data->id); 822cebe7373SAmit Daniel Kachhap sensor_conf->read_temperature = (int (*)(void *))exynos_tmu_read; 823cebe7373SAmit Daniel Kachhap sensor_conf->write_emul_temp = 824cebe7373SAmit Daniel Kachhap (int (*)(void *, unsigned long))exynos_tmu_set_emulation; 825cebe7373SAmit Daniel Kachhap sensor_conf->driver_data = data; 826cebe7373SAmit Daniel Kachhap sensor_conf->trip_data.trip_count = pdata->trigger_enable[0] + 827bb34b4c8SAmit Daniel Kachhap pdata->trigger_enable[1] + pdata->trigger_enable[2]+ 828bb34b4c8SAmit Daniel Kachhap pdata->trigger_enable[3]; 82959dfa54cSAmit Daniel Kachhap 830cebe7373SAmit Daniel Kachhap for (i = 0; i < sensor_conf->trip_data.trip_count; i++) { 831cebe7373SAmit Daniel Kachhap sensor_conf->trip_data.trip_val[i] = 83259dfa54cSAmit Daniel Kachhap pdata->threshold + pdata->trigger_levels[i]; 833cebe7373SAmit Daniel Kachhap sensor_conf->trip_data.trip_type[i] = 8345c3cf552SAmit Daniel Kachhap pdata->trigger_type[i]; 8355c3cf552SAmit Daniel Kachhap } 83659dfa54cSAmit Daniel Kachhap 837cebe7373SAmit Daniel Kachhap sensor_conf->trip_data.trigger_falling = pdata->threshold_falling; 83859dfa54cSAmit Daniel Kachhap 839cebe7373SAmit Daniel Kachhap sensor_conf->cooling_data.freq_clip_count = pdata->freq_tab_count; 84059dfa54cSAmit Daniel Kachhap for (i = 0; i < pdata->freq_tab_count; i++) { 841cebe7373SAmit Daniel Kachhap sensor_conf->cooling_data.freq_data[i].freq_clip_max = 84259dfa54cSAmit Daniel Kachhap pdata->freq_tab[i].freq_clip_max; 843cebe7373SAmit Daniel Kachhap sensor_conf->cooling_data.freq_data[i].temp_level = 84459dfa54cSAmit Daniel Kachhap pdata->freq_tab[i].temp_level; 84559dfa54cSAmit Daniel Kachhap } 846cebe7373SAmit Daniel Kachhap sensor_conf->dev = &pdev->dev; 847cebe7373SAmit Daniel Kachhap /* Register the sensor with thermal management interface */ 848cebe7373SAmit Daniel Kachhap ret = exynos_register_thermal(sensor_conf); 84959dfa54cSAmit Daniel Kachhap if (ret) { 85059dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to register thermal interface\n"); 85159dfa54cSAmit Daniel Kachhap goto err_clk; 85259dfa54cSAmit Daniel Kachhap } 853cebe7373SAmit Daniel Kachhap data->reg_conf = sensor_conf; 854cebe7373SAmit Daniel Kachhap 855cebe7373SAmit Daniel Kachhap ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq, 856cebe7373SAmit Daniel Kachhap IRQF_TRIGGER_RISING | IRQF_SHARED, dev_name(&pdev->dev), data); 857cebe7373SAmit Daniel Kachhap if (ret) { 858cebe7373SAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq); 859cebe7373SAmit Daniel Kachhap goto err_clk; 860cebe7373SAmit Daniel Kachhap } 86159dfa54cSAmit Daniel Kachhap 86259dfa54cSAmit Daniel Kachhap return 0; 86359dfa54cSAmit Daniel Kachhap err_clk: 86459dfa54cSAmit Daniel Kachhap clk_unprepare(data->clk); 86514a11dc7SNaveen Krishna Chatradhi err_clk_sec: 86614a11dc7SNaveen Krishna Chatradhi if (!IS_ERR(data->clk_sec)) 86714a11dc7SNaveen Krishna Chatradhi clk_unprepare(data->clk_sec); 86859dfa54cSAmit Daniel Kachhap return ret; 86959dfa54cSAmit Daniel Kachhap } 87059dfa54cSAmit Daniel Kachhap 87159dfa54cSAmit Daniel Kachhap static int exynos_tmu_remove(struct platform_device *pdev) 87259dfa54cSAmit Daniel Kachhap { 87359dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = platform_get_drvdata(pdev); 87459dfa54cSAmit Daniel Kachhap 875cebe7373SAmit Daniel Kachhap exynos_unregister_thermal(data->reg_conf); 87659dfa54cSAmit Daniel Kachhap 8774215688eSBartlomiej Zolnierkiewicz exynos_tmu_control(pdev, false); 8784215688eSBartlomiej Zolnierkiewicz 87959dfa54cSAmit Daniel Kachhap clk_unprepare(data->clk); 88014a11dc7SNaveen Krishna Chatradhi if (!IS_ERR(data->clk_sec)) 88114a11dc7SNaveen Krishna Chatradhi clk_unprepare(data->clk_sec); 88259dfa54cSAmit Daniel Kachhap 883498d22f6SAmit Daniel Kachhap if (!IS_ERR(data->regulator)) 884498d22f6SAmit Daniel Kachhap regulator_disable(data->regulator); 885498d22f6SAmit Daniel Kachhap 88659dfa54cSAmit Daniel Kachhap return 0; 88759dfa54cSAmit Daniel Kachhap } 88859dfa54cSAmit Daniel Kachhap 88959dfa54cSAmit Daniel Kachhap #ifdef CONFIG_PM_SLEEP 89059dfa54cSAmit Daniel Kachhap static int exynos_tmu_suspend(struct device *dev) 89159dfa54cSAmit Daniel Kachhap { 89259dfa54cSAmit Daniel Kachhap exynos_tmu_control(to_platform_device(dev), false); 89359dfa54cSAmit Daniel Kachhap 89459dfa54cSAmit Daniel Kachhap return 0; 89559dfa54cSAmit Daniel Kachhap } 89659dfa54cSAmit Daniel Kachhap 89759dfa54cSAmit Daniel Kachhap static int exynos_tmu_resume(struct device *dev) 89859dfa54cSAmit Daniel Kachhap { 89959dfa54cSAmit Daniel Kachhap struct platform_device *pdev = to_platform_device(dev); 90059dfa54cSAmit Daniel Kachhap 90159dfa54cSAmit Daniel Kachhap exynos_tmu_initialize(pdev); 90259dfa54cSAmit Daniel Kachhap exynos_tmu_control(pdev, true); 90359dfa54cSAmit Daniel Kachhap 90459dfa54cSAmit Daniel Kachhap return 0; 90559dfa54cSAmit Daniel Kachhap } 90659dfa54cSAmit Daniel Kachhap 90759dfa54cSAmit Daniel Kachhap static SIMPLE_DEV_PM_OPS(exynos_tmu_pm, 90859dfa54cSAmit Daniel Kachhap exynos_tmu_suspend, exynos_tmu_resume); 90959dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM (&exynos_tmu_pm) 91059dfa54cSAmit Daniel Kachhap #else 91159dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM NULL 91259dfa54cSAmit Daniel Kachhap #endif 91359dfa54cSAmit Daniel Kachhap 91459dfa54cSAmit Daniel Kachhap static struct platform_driver exynos_tmu_driver = { 91559dfa54cSAmit Daniel Kachhap .driver = { 91659dfa54cSAmit Daniel Kachhap .name = "exynos-tmu", 91759dfa54cSAmit Daniel Kachhap .owner = THIS_MODULE, 91859dfa54cSAmit Daniel Kachhap .pm = EXYNOS_TMU_PM, 91973b5b1d7SSachin Kamat .of_match_table = exynos_tmu_match, 92059dfa54cSAmit Daniel Kachhap }, 92159dfa54cSAmit Daniel Kachhap .probe = exynos_tmu_probe, 92259dfa54cSAmit Daniel Kachhap .remove = exynos_tmu_remove, 92359dfa54cSAmit Daniel Kachhap }; 92459dfa54cSAmit Daniel Kachhap 92559dfa54cSAmit Daniel Kachhap module_platform_driver(exynos_tmu_driver); 92659dfa54cSAmit Daniel Kachhap 92759dfa54cSAmit Daniel Kachhap MODULE_DESCRIPTION("EXYNOS TMU Driver"); 92859dfa54cSAmit Daniel Kachhap MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>"); 92959dfa54cSAmit Daniel Kachhap MODULE_LICENSE("GPL"); 93059dfa54cSAmit Daniel Kachhap MODULE_ALIAS("platform:exynos-tmu"); 931