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 19686f5362eSLukasz Majewski if (pdata->test_mux) 197bfb2b88cSBartlomiej Zolnierkiewicz con |= (pdata->test_mux << EXYNOS4412_MUX_ADDR_SHIFT); 19886f5362eSLukasz Majewski 19999d67fb9SBartlomiej Zolnierkiewicz con &= ~(EXYNOS_TMU_REF_VOLTAGE_MASK << EXYNOS_TMU_REF_VOLTAGE_SHIFT); 20099d67fb9SBartlomiej Zolnierkiewicz con |= pdata->reference_voltage << EXYNOS_TMU_REF_VOLTAGE_SHIFT; 201d0a0ce3eSAmit Daniel Kachhap 20299d67fb9SBartlomiej Zolnierkiewicz con &= ~(EXYNOS_TMU_BUF_SLOPE_SEL_MASK << EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT); 20399d67fb9SBartlomiej Zolnierkiewicz con |= (pdata->gain << EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT); 204d0a0ce3eSAmit Daniel Kachhap 205d0a0ce3eSAmit Daniel Kachhap if (pdata->noise_cancel_mode) { 206b9504a6aSBartlomiej Zolnierkiewicz con &= ~(EXYNOS_TMU_TRIP_MODE_MASK << EXYNOS_TMU_TRIP_MODE_SHIFT); 207b9504a6aSBartlomiej Zolnierkiewicz con |= (pdata->noise_cancel_mode << EXYNOS_TMU_TRIP_MODE_SHIFT); 20859dfa54cSAmit Daniel Kachhap } 20959dfa54cSAmit Daniel Kachhap 210d00671c3SBartlomiej Zolnierkiewicz return con; 211d00671c3SBartlomiej Zolnierkiewicz } 212d00671c3SBartlomiej Zolnierkiewicz 213d00671c3SBartlomiej Zolnierkiewicz static void exynos_tmu_control(struct platform_device *pdev, bool on) 214d00671c3SBartlomiej Zolnierkiewicz { 215d00671c3SBartlomiej Zolnierkiewicz struct exynos_tmu_data *data = platform_get_drvdata(pdev); 216d00671c3SBartlomiej Zolnierkiewicz 217d00671c3SBartlomiej Zolnierkiewicz mutex_lock(&data->lock); 218d00671c3SBartlomiej Zolnierkiewicz clk_enable(data->clk); 21937f9034fSBartlomiej Zolnierkiewicz data->tmu_control(pdev, on); 22059dfa54cSAmit Daniel Kachhap clk_disable(data->clk); 22159dfa54cSAmit Daniel Kachhap mutex_unlock(&data->lock); 22259dfa54cSAmit Daniel Kachhap } 22359dfa54cSAmit Daniel Kachhap 22472d1100bSBartlomiej Zolnierkiewicz static int exynos4210_tmu_initialize(struct platform_device *pdev) 22572d1100bSBartlomiej Zolnierkiewicz { 22672d1100bSBartlomiej Zolnierkiewicz struct exynos_tmu_data *data = platform_get_drvdata(pdev); 22772d1100bSBartlomiej Zolnierkiewicz struct exynos_tmu_platform_data *pdata = data->pdata; 22872d1100bSBartlomiej Zolnierkiewicz unsigned int status; 22972d1100bSBartlomiej Zolnierkiewicz int ret = 0, threshold_code, i; 23072d1100bSBartlomiej Zolnierkiewicz 23172d1100bSBartlomiej Zolnierkiewicz status = readb(data->base + EXYNOS_TMU_REG_STATUS); 23272d1100bSBartlomiej Zolnierkiewicz if (!status) { 23372d1100bSBartlomiej Zolnierkiewicz ret = -EBUSY; 23472d1100bSBartlomiej Zolnierkiewicz goto out; 23572d1100bSBartlomiej Zolnierkiewicz } 23672d1100bSBartlomiej Zolnierkiewicz 23772d1100bSBartlomiej Zolnierkiewicz sanitize_temp_error(data, readl(data->base + EXYNOS_TMU_REG_TRIMINFO)); 23872d1100bSBartlomiej Zolnierkiewicz 23972d1100bSBartlomiej Zolnierkiewicz /* Write temperature code for threshold */ 24072d1100bSBartlomiej Zolnierkiewicz threshold_code = temp_to_code(data, pdata->threshold); 24172d1100bSBartlomiej Zolnierkiewicz writeb(threshold_code, data->base + EXYNOS4210_TMU_REG_THRESHOLD_TEMP); 24272d1100bSBartlomiej Zolnierkiewicz 24372d1100bSBartlomiej Zolnierkiewicz for (i = 0; i < pdata->non_hw_trigger_levels; i++) 24472d1100bSBartlomiej Zolnierkiewicz writeb(pdata->trigger_levels[i], data->base + 24572d1100bSBartlomiej Zolnierkiewicz EXYNOS4210_TMU_REG_TRIG_LEVEL0 + i * 4); 24672d1100bSBartlomiej Zolnierkiewicz 247a7331f72SBartlomiej Zolnierkiewicz data->tmu_clear_irqs(data); 24872d1100bSBartlomiej Zolnierkiewicz out: 24972d1100bSBartlomiej Zolnierkiewicz return ret; 25072d1100bSBartlomiej Zolnierkiewicz } 25172d1100bSBartlomiej Zolnierkiewicz 25272d1100bSBartlomiej Zolnierkiewicz static int exynos4412_tmu_initialize(struct platform_device *pdev) 25372d1100bSBartlomiej Zolnierkiewicz { 25472d1100bSBartlomiej Zolnierkiewicz struct exynos_tmu_data *data = platform_get_drvdata(pdev); 25572d1100bSBartlomiej Zolnierkiewicz struct exynos_tmu_platform_data *pdata = data->pdata; 25672d1100bSBartlomiej Zolnierkiewicz unsigned int status, trim_info, con, ctrl, rising_threshold; 25772d1100bSBartlomiej Zolnierkiewicz int ret = 0, threshold_code, i; 25872d1100bSBartlomiej Zolnierkiewicz 25972d1100bSBartlomiej Zolnierkiewicz status = readb(data->base + EXYNOS_TMU_REG_STATUS); 26072d1100bSBartlomiej Zolnierkiewicz if (!status) { 26172d1100bSBartlomiej Zolnierkiewicz ret = -EBUSY; 26272d1100bSBartlomiej Zolnierkiewicz goto out; 26372d1100bSBartlomiej Zolnierkiewicz } 26472d1100bSBartlomiej Zolnierkiewicz 26572d1100bSBartlomiej Zolnierkiewicz if (data->soc == SOC_ARCH_EXYNOS3250 || 26672d1100bSBartlomiej Zolnierkiewicz data->soc == SOC_ARCH_EXYNOS4412 || 26772d1100bSBartlomiej Zolnierkiewicz data->soc == SOC_ARCH_EXYNOS5250) { 26872d1100bSBartlomiej Zolnierkiewicz if (data->soc == SOC_ARCH_EXYNOS3250) { 26972d1100bSBartlomiej Zolnierkiewicz ctrl = readl(data->base + EXYNOS_TMU_TRIMINFO_CON1); 27072d1100bSBartlomiej Zolnierkiewicz ctrl |= EXYNOS_TRIMINFO_RELOAD_ENABLE; 27172d1100bSBartlomiej Zolnierkiewicz writel(ctrl, data->base + EXYNOS_TMU_TRIMINFO_CON1); 27272d1100bSBartlomiej Zolnierkiewicz } 27372d1100bSBartlomiej Zolnierkiewicz ctrl = readl(data->base + EXYNOS_TMU_TRIMINFO_CON2); 27472d1100bSBartlomiej Zolnierkiewicz ctrl |= EXYNOS_TRIMINFO_RELOAD_ENABLE; 27572d1100bSBartlomiej Zolnierkiewicz writel(ctrl, data->base + EXYNOS_TMU_TRIMINFO_CON2); 27672d1100bSBartlomiej Zolnierkiewicz } 27772d1100bSBartlomiej Zolnierkiewicz 27872d1100bSBartlomiej Zolnierkiewicz /* On exynos5420 the triminfo register is in the shared space */ 27972d1100bSBartlomiej Zolnierkiewicz if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO) 28072d1100bSBartlomiej Zolnierkiewicz trim_info = readl(data->base_second + EXYNOS_TMU_REG_TRIMINFO); 28172d1100bSBartlomiej Zolnierkiewicz else 28272d1100bSBartlomiej Zolnierkiewicz trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO); 28372d1100bSBartlomiej Zolnierkiewicz 28472d1100bSBartlomiej Zolnierkiewicz sanitize_temp_error(data, trim_info); 28572d1100bSBartlomiej Zolnierkiewicz 28672d1100bSBartlomiej Zolnierkiewicz /* Write temperature code for rising and falling threshold */ 28772d1100bSBartlomiej Zolnierkiewicz rising_threshold = readl(data->base + EXYNOS_THD_TEMP_RISE); 28872d1100bSBartlomiej Zolnierkiewicz rising_threshold = get_th_reg(data, rising_threshold, false); 28972d1100bSBartlomiej Zolnierkiewicz writel(rising_threshold, data->base + EXYNOS_THD_TEMP_RISE); 29072d1100bSBartlomiej Zolnierkiewicz writel(get_th_reg(data, 0, true), data->base + EXYNOS_THD_TEMP_FALL); 29172d1100bSBartlomiej Zolnierkiewicz 292a7331f72SBartlomiej Zolnierkiewicz data->tmu_clear_irqs(data); 29372d1100bSBartlomiej Zolnierkiewicz 29472d1100bSBartlomiej Zolnierkiewicz /* if last threshold limit is also present */ 29572d1100bSBartlomiej Zolnierkiewicz i = pdata->max_trigger_level - 1; 29672d1100bSBartlomiej Zolnierkiewicz if (pdata->trigger_levels[i] && pdata->trigger_type[i] == HW_TRIP) { 29772d1100bSBartlomiej Zolnierkiewicz threshold_code = temp_to_code(data, pdata->trigger_levels[i]); 29872d1100bSBartlomiej Zolnierkiewicz /* 1-4 level to be assigned in th0 reg */ 29972d1100bSBartlomiej Zolnierkiewicz rising_threshold &= ~(0xff << 8 * i); 30072d1100bSBartlomiej Zolnierkiewicz rising_threshold |= threshold_code << 8 * i; 30172d1100bSBartlomiej Zolnierkiewicz writel(rising_threshold, data->base + EXYNOS_THD_TEMP_RISE); 30272d1100bSBartlomiej Zolnierkiewicz con = readl(data->base + EXYNOS_TMU_REG_CONTROL); 30372d1100bSBartlomiej Zolnierkiewicz con |= (1 << EXYNOS_TMU_THERM_TRIP_EN_SHIFT); 30472d1100bSBartlomiej Zolnierkiewicz writel(con, data->base + EXYNOS_TMU_REG_CONTROL); 30572d1100bSBartlomiej Zolnierkiewicz } 30672d1100bSBartlomiej Zolnierkiewicz out: 30772d1100bSBartlomiej Zolnierkiewicz return ret; 30872d1100bSBartlomiej Zolnierkiewicz } 30972d1100bSBartlomiej Zolnierkiewicz 31072d1100bSBartlomiej Zolnierkiewicz static int exynos5440_tmu_initialize(struct platform_device *pdev) 31172d1100bSBartlomiej Zolnierkiewicz { 31272d1100bSBartlomiej Zolnierkiewicz struct exynos_tmu_data *data = platform_get_drvdata(pdev); 31372d1100bSBartlomiej Zolnierkiewicz struct exynos_tmu_platform_data *pdata = data->pdata; 31472d1100bSBartlomiej Zolnierkiewicz unsigned int trim_info = 0, con, rising_threshold; 31572d1100bSBartlomiej Zolnierkiewicz int ret = 0, threshold_code, i; 31672d1100bSBartlomiej Zolnierkiewicz 31772d1100bSBartlomiej Zolnierkiewicz /* 31872d1100bSBartlomiej Zolnierkiewicz * For exynos5440 soc triminfo value is swapped between TMU0 and 31972d1100bSBartlomiej Zolnierkiewicz * TMU2, so the below logic is needed. 32072d1100bSBartlomiej Zolnierkiewicz */ 32172d1100bSBartlomiej Zolnierkiewicz switch (data->id) { 32272d1100bSBartlomiej Zolnierkiewicz case 0: 32372d1100bSBartlomiej Zolnierkiewicz trim_info = readl(data->base + EXYNOS5440_EFUSE_SWAP_OFFSET + 32472d1100bSBartlomiej Zolnierkiewicz EXYNOS5440_TMU_S0_7_TRIM); 32572d1100bSBartlomiej Zolnierkiewicz break; 32672d1100bSBartlomiej Zolnierkiewicz case 1: 32772d1100bSBartlomiej Zolnierkiewicz trim_info = readl(data->base + EXYNOS5440_TMU_S0_7_TRIM); 32872d1100bSBartlomiej Zolnierkiewicz break; 32972d1100bSBartlomiej Zolnierkiewicz case 2: 33072d1100bSBartlomiej Zolnierkiewicz trim_info = readl(data->base - EXYNOS5440_EFUSE_SWAP_OFFSET + 33172d1100bSBartlomiej Zolnierkiewicz EXYNOS5440_TMU_S0_7_TRIM); 33272d1100bSBartlomiej Zolnierkiewicz } 33372d1100bSBartlomiej Zolnierkiewicz sanitize_temp_error(data, trim_info); 33472d1100bSBartlomiej Zolnierkiewicz 33572d1100bSBartlomiej Zolnierkiewicz /* Write temperature code for rising and falling threshold */ 33672d1100bSBartlomiej Zolnierkiewicz rising_threshold = readl(data->base + EXYNOS5440_TMU_S0_7_TH0); 33772d1100bSBartlomiej Zolnierkiewicz rising_threshold = get_th_reg(data, rising_threshold, false); 33872d1100bSBartlomiej Zolnierkiewicz writel(rising_threshold, data->base + EXYNOS5440_TMU_S0_7_TH0); 33972d1100bSBartlomiej Zolnierkiewicz writel(0, data->base + EXYNOS5440_TMU_S0_7_TH1); 34072d1100bSBartlomiej Zolnierkiewicz 341a7331f72SBartlomiej Zolnierkiewicz data->tmu_clear_irqs(data); 34272d1100bSBartlomiej Zolnierkiewicz 34372d1100bSBartlomiej Zolnierkiewicz /* if last threshold limit is also present */ 34472d1100bSBartlomiej Zolnierkiewicz i = pdata->max_trigger_level - 1; 34572d1100bSBartlomiej Zolnierkiewicz if (pdata->trigger_levels[i] && pdata->trigger_type[i] == HW_TRIP) { 34672d1100bSBartlomiej Zolnierkiewicz threshold_code = temp_to_code(data, pdata->trigger_levels[i]); 34772d1100bSBartlomiej Zolnierkiewicz /* 5th level to be assigned in th2 reg */ 34872d1100bSBartlomiej Zolnierkiewicz rising_threshold = 34972d1100bSBartlomiej Zolnierkiewicz threshold_code << EXYNOS5440_TMU_TH_RISE4_SHIFT; 35072d1100bSBartlomiej Zolnierkiewicz writel(rising_threshold, data->base + EXYNOS5440_TMU_S0_7_TH2); 35172d1100bSBartlomiej Zolnierkiewicz con = readl(data->base + EXYNOS5440_TMU_S0_7_CTRL); 35272d1100bSBartlomiej Zolnierkiewicz con |= (1 << EXYNOS_TMU_THERM_TRIP_EN_SHIFT); 35372d1100bSBartlomiej Zolnierkiewicz writel(con, data->base + EXYNOS5440_TMU_S0_7_CTRL); 35472d1100bSBartlomiej Zolnierkiewicz } 35572d1100bSBartlomiej Zolnierkiewicz /* Clear the PMIN in the common TMU register */ 35672d1100bSBartlomiej Zolnierkiewicz if (!data->id) 35772d1100bSBartlomiej Zolnierkiewicz writel(0, data->base_second + EXYNOS5440_TMU_PMIN); 35872d1100bSBartlomiej Zolnierkiewicz return ret; 35972d1100bSBartlomiej Zolnierkiewicz } 36072d1100bSBartlomiej Zolnierkiewicz 36137f9034fSBartlomiej Zolnierkiewicz static void exynos4210_tmu_control(struct platform_device *pdev, bool on) 36237f9034fSBartlomiej Zolnierkiewicz { 36337f9034fSBartlomiej Zolnierkiewicz struct exynos_tmu_data *data = platform_get_drvdata(pdev); 36437f9034fSBartlomiej Zolnierkiewicz struct exynos_tmu_platform_data *pdata = data->pdata; 36537f9034fSBartlomiej Zolnierkiewicz unsigned int con, interrupt_en; 36637f9034fSBartlomiej Zolnierkiewicz 36737f9034fSBartlomiej Zolnierkiewicz con = get_con_reg(data, readl(data->base + EXYNOS_TMU_REG_CONTROL)); 36837f9034fSBartlomiej Zolnierkiewicz 36937f9034fSBartlomiej Zolnierkiewicz if (on) { 37037f9034fSBartlomiej Zolnierkiewicz con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT); 37137f9034fSBartlomiej Zolnierkiewicz interrupt_en = 37237f9034fSBartlomiej Zolnierkiewicz pdata->trigger_enable[3] << EXYNOS_TMU_INTEN_RISE3_SHIFT | 37337f9034fSBartlomiej Zolnierkiewicz pdata->trigger_enable[2] << EXYNOS_TMU_INTEN_RISE2_SHIFT | 37437f9034fSBartlomiej Zolnierkiewicz pdata->trigger_enable[1] << EXYNOS_TMU_INTEN_RISE1_SHIFT | 37537f9034fSBartlomiej Zolnierkiewicz pdata->trigger_enable[0] << EXYNOS_TMU_INTEN_RISE0_SHIFT; 376e0761533SBartlomiej Zolnierkiewicz if (data->soc != SOC_ARCH_EXYNOS4210) 37737f9034fSBartlomiej Zolnierkiewicz interrupt_en |= 37837f9034fSBartlomiej Zolnierkiewicz interrupt_en << EXYNOS_TMU_INTEN_FALL0_SHIFT; 37937f9034fSBartlomiej Zolnierkiewicz } else { 38037f9034fSBartlomiej Zolnierkiewicz con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT); 38137f9034fSBartlomiej Zolnierkiewicz interrupt_en = 0; /* Disable all interrupts */ 38237f9034fSBartlomiej Zolnierkiewicz } 38337f9034fSBartlomiej Zolnierkiewicz writel(interrupt_en, data->base + EXYNOS_TMU_REG_INTEN); 38437f9034fSBartlomiej Zolnierkiewicz writel(con, data->base + EXYNOS_TMU_REG_CONTROL); 38537f9034fSBartlomiej Zolnierkiewicz } 38637f9034fSBartlomiej Zolnierkiewicz 38737f9034fSBartlomiej Zolnierkiewicz static void exynos5440_tmu_control(struct platform_device *pdev, bool on) 38837f9034fSBartlomiej Zolnierkiewicz { 38937f9034fSBartlomiej Zolnierkiewicz struct exynos_tmu_data *data = platform_get_drvdata(pdev); 39037f9034fSBartlomiej Zolnierkiewicz struct exynos_tmu_platform_data *pdata = data->pdata; 39137f9034fSBartlomiej Zolnierkiewicz unsigned int con, interrupt_en; 39237f9034fSBartlomiej Zolnierkiewicz 39337f9034fSBartlomiej Zolnierkiewicz con = get_con_reg(data, readl(data->base + EXYNOS5440_TMU_S0_7_CTRL)); 39437f9034fSBartlomiej Zolnierkiewicz 39537f9034fSBartlomiej Zolnierkiewicz if (on) { 39637f9034fSBartlomiej Zolnierkiewicz con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT); 39737f9034fSBartlomiej Zolnierkiewicz interrupt_en = 39837f9034fSBartlomiej Zolnierkiewicz pdata->trigger_enable[3] << EXYNOS5440_TMU_INTEN_RISE3_SHIFT | 39937f9034fSBartlomiej Zolnierkiewicz pdata->trigger_enable[2] << EXYNOS5440_TMU_INTEN_RISE2_SHIFT | 40037f9034fSBartlomiej Zolnierkiewicz pdata->trigger_enable[1] << EXYNOS5440_TMU_INTEN_RISE1_SHIFT | 40137f9034fSBartlomiej Zolnierkiewicz pdata->trigger_enable[0] << EXYNOS5440_TMU_INTEN_RISE0_SHIFT; 402e0761533SBartlomiej Zolnierkiewicz interrupt_en |= interrupt_en << EXYNOS5440_TMU_INTEN_FALL0_SHIFT; 40337f9034fSBartlomiej Zolnierkiewicz } else { 40437f9034fSBartlomiej Zolnierkiewicz con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT); 40537f9034fSBartlomiej Zolnierkiewicz interrupt_en = 0; /* Disable all interrupts */ 40637f9034fSBartlomiej Zolnierkiewicz } 40737f9034fSBartlomiej Zolnierkiewicz writel(interrupt_en, data->base + EXYNOS5440_TMU_S0_7_IRQEN); 40837f9034fSBartlomiej Zolnierkiewicz writel(con, data->base + EXYNOS5440_TMU_S0_7_CTRL); 40937f9034fSBartlomiej Zolnierkiewicz } 41037f9034fSBartlomiej Zolnierkiewicz 41159dfa54cSAmit Daniel Kachhap static int exynos_tmu_read(struct exynos_tmu_data *data) 41259dfa54cSAmit Daniel Kachhap { 413b79985caSBartlomiej Zolnierkiewicz int ret; 41459dfa54cSAmit Daniel Kachhap 41559dfa54cSAmit Daniel Kachhap mutex_lock(&data->lock); 41659dfa54cSAmit Daniel Kachhap clk_enable(data->clk); 417b79985caSBartlomiej Zolnierkiewicz ret = data->tmu_read(data); 418b79985caSBartlomiej Zolnierkiewicz if (ret >= 0) 419b79985caSBartlomiej Zolnierkiewicz ret = code_to_temp(data, ret); 42059dfa54cSAmit Daniel Kachhap clk_disable(data->clk); 42159dfa54cSAmit Daniel Kachhap mutex_unlock(&data->lock); 42259dfa54cSAmit Daniel Kachhap 423b79985caSBartlomiej Zolnierkiewicz return ret; 42459dfa54cSAmit Daniel Kachhap } 42559dfa54cSAmit Daniel Kachhap 42659dfa54cSAmit Daniel Kachhap #ifdef CONFIG_THERMAL_EMULATION 427154013eaSBartlomiej Zolnierkiewicz static u32 get_emul_con_reg(struct exynos_tmu_data *data, unsigned int val, 428154013eaSBartlomiej Zolnierkiewicz unsigned long temp) 429154013eaSBartlomiej Zolnierkiewicz { 430154013eaSBartlomiej Zolnierkiewicz if (temp) { 431154013eaSBartlomiej Zolnierkiewicz temp /= MCELSIUS; 432154013eaSBartlomiej Zolnierkiewicz 433d564b55aSBartlomiej Zolnierkiewicz if (data->soc != SOC_ARCH_EXYNOS5440) { 434154013eaSBartlomiej Zolnierkiewicz val &= ~(EXYNOS_EMUL_TIME_MASK << EXYNOS_EMUL_TIME_SHIFT); 435154013eaSBartlomiej Zolnierkiewicz val |= (EXYNOS_EMUL_TIME << EXYNOS_EMUL_TIME_SHIFT); 436154013eaSBartlomiej Zolnierkiewicz } 437154013eaSBartlomiej Zolnierkiewicz val &= ~(EXYNOS_EMUL_DATA_MASK << EXYNOS_EMUL_DATA_SHIFT); 438154013eaSBartlomiej Zolnierkiewicz val |= (temp_to_code(data, temp) << EXYNOS_EMUL_DATA_SHIFT) | 439154013eaSBartlomiej Zolnierkiewicz EXYNOS_EMUL_ENABLE; 440154013eaSBartlomiej Zolnierkiewicz } else { 441154013eaSBartlomiej Zolnierkiewicz val &= ~EXYNOS_EMUL_ENABLE; 442154013eaSBartlomiej Zolnierkiewicz } 443154013eaSBartlomiej Zolnierkiewicz 444154013eaSBartlomiej Zolnierkiewicz return val; 445154013eaSBartlomiej Zolnierkiewicz } 446154013eaSBartlomiej Zolnierkiewicz 447285d994aSBartlomiej Zolnierkiewicz static void exynos4412_tmu_set_emulation(struct exynos_tmu_data *data, 448285d994aSBartlomiej Zolnierkiewicz unsigned long temp) 449285d994aSBartlomiej Zolnierkiewicz { 450285d994aSBartlomiej Zolnierkiewicz unsigned int val; 451285d994aSBartlomiej Zolnierkiewicz u32 emul_con; 452285d994aSBartlomiej Zolnierkiewicz 453285d994aSBartlomiej Zolnierkiewicz if (data->soc == SOC_ARCH_EXYNOS5260) 454285d994aSBartlomiej Zolnierkiewicz emul_con = EXYNOS5260_EMUL_CON; 455285d994aSBartlomiej Zolnierkiewicz else 456285d994aSBartlomiej Zolnierkiewicz emul_con = EXYNOS_EMUL_CON; 457285d994aSBartlomiej Zolnierkiewicz 458285d994aSBartlomiej Zolnierkiewicz val = readl(data->base + emul_con); 459285d994aSBartlomiej Zolnierkiewicz val = get_emul_con_reg(data, val, temp); 460285d994aSBartlomiej Zolnierkiewicz writel(val, data->base + emul_con); 461285d994aSBartlomiej Zolnierkiewicz } 462285d994aSBartlomiej Zolnierkiewicz 463285d994aSBartlomiej Zolnierkiewicz static void exynos5440_tmu_set_emulation(struct exynos_tmu_data *data, 464285d994aSBartlomiej Zolnierkiewicz unsigned long temp) 465285d994aSBartlomiej Zolnierkiewicz { 466285d994aSBartlomiej Zolnierkiewicz unsigned int val; 467285d994aSBartlomiej Zolnierkiewicz 468285d994aSBartlomiej Zolnierkiewicz val = readl(data->base + EXYNOS5440_TMU_S0_7_DEBUG); 469285d994aSBartlomiej Zolnierkiewicz val = get_emul_con_reg(data, val, temp); 470285d994aSBartlomiej Zolnierkiewicz writel(val, data->base + EXYNOS5440_TMU_S0_7_DEBUG); 471285d994aSBartlomiej Zolnierkiewicz } 472285d994aSBartlomiej Zolnierkiewicz 47359dfa54cSAmit Daniel Kachhap static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp) 47459dfa54cSAmit Daniel Kachhap { 47559dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = drv_data; 476b8d582b9SAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata = data->pdata; 47759dfa54cSAmit Daniel Kachhap int ret = -EINVAL; 47859dfa54cSAmit Daniel Kachhap 479*ef3f80fcSBartlomiej 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 } 699cebe7373SAmit Daniel Kachhap data->pdata = pdata; 700d9b6ee14SAmit Daniel Kachhap /* 701d9b6ee14SAmit Daniel Kachhap * Check if the TMU shares some registers and then try to map the 702d9b6ee14SAmit Daniel Kachhap * memory of common registers. 703d9b6ee14SAmit Daniel Kachhap */ 7049025d563SNaveen Krishna Chatradhi if (!TMU_SUPPORTS(pdata, ADDRESS_MULTIPLE)) 705d9b6ee14SAmit Daniel Kachhap return 0; 706d9b6ee14SAmit Daniel Kachhap 707d9b6ee14SAmit Daniel Kachhap if (of_address_to_resource(pdev->dev.of_node, 1, &res)) { 708d9b6ee14SAmit Daniel Kachhap dev_err(&pdev->dev, "failed to get Resource 1\n"); 709d9b6ee14SAmit Daniel Kachhap return -ENODEV; 710d9b6ee14SAmit Daniel Kachhap } 711d9b6ee14SAmit Daniel Kachhap 7129025d563SNaveen Krishna Chatradhi data->base_second = devm_ioremap(&pdev->dev, res.start, 713d9b6ee14SAmit Daniel Kachhap resource_size(&res)); 7149025d563SNaveen Krishna Chatradhi if (!data->base_second) { 715d9b6ee14SAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to ioremap memory\n"); 716d9b6ee14SAmit Daniel Kachhap return -ENOMEM; 717d9b6ee14SAmit Daniel Kachhap } 718cebe7373SAmit Daniel Kachhap 719cebe7373SAmit Daniel Kachhap return 0; 720cebe7373SAmit Daniel Kachhap } 721cebe7373SAmit Daniel Kachhap 722cebe7373SAmit Daniel Kachhap static int exynos_tmu_probe(struct platform_device *pdev) 723cebe7373SAmit Daniel Kachhap { 724cebe7373SAmit Daniel Kachhap struct exynos_tmu_data *data; 725cebe7373SAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata; 726cebe7373SAmit Daniel Kachhap struct thermal_sensor_conf *sensor_conf; 727cebe7373SAmit Daniel Kachhap int ret, i; 728cebe7373SAmit Daniel Kachhap 72959dfa54cSAmit Daniel Kachhap data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data), 73059dfa54cSAmit Daniel Kachhap GFP_KERNEL); 7312a9675b3SJingoo Han if (!data) 73259dfa54cSAmit Daniel Kachhap return -ENOMEM; 73359dfa54cSAmit Daniel Kachhap 734cebe7373SAmit Daniel Kachhap platform_set_drvdata(pdev, data); 735cebe7373SAmit Daniel Kachhap mutex_init(&data->lock); 736cebe7373SAmit Daniel Kachhap 737cebe7373SAmit Daniel Kachhap ret = exynos_map_dt_data(pdev); 738cebe7373SAmit Daniel Kachhap if (ret) 739cebe7373SAmit Daniel Kachhap return ret; 740cebe7373SAmit Daniel Kachhap 741cebe7373SAmit Daniel Kachhap pdata = data->pdata; 74259dfa54cSAmit Daniel Kachhap 74359dfa54cSAmit Daniel Kachhap INIT_WORK(&data->irq_work, exynos_tmu_work); 74459dfa54cSAmit Daniel Kachhap 74559dfa54cSAmit Daniel Kachhap data->clk = devm_clk_get(&pdev->dev, "tmu_apbif"); 74659dfa54cSAmit Daniel Kachhap if (IS_ERR(data->clk)) { 74759dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to get clock\n"); 74859dfa54cSAmit Daniel Kachhap return PTR_ERR(data->clk); 74959dfa54cSAmit Daniel Kachhap } 75059dfa54cSAmit Daniel Kachhap 75114a11dc7SNaveen Krishna Chatradhi data->clk_sec = devm_clk_get(&pdev->dev, "tmu_triminfo_apbif"); 75214a11dc7SNaveen Krishna Chatradhi if (IS_ERR(data->clk_sec)) { 75314a11dc7SNaveen Krishna Chatradhi if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO) { 75414a11dc7SNaveen Krishna Chatradhi dev_err(&pdev->dev, "Failed to get triminfo clock\n"); 75514a11dc7SNaveen Krishna Chatradhi return PTR_ERR(data->clk_sec); 75614a11dc7SNaveen Krishna Chatradhi } 75714a11dc7SNaveen Krishna Chatradhi } else { 75814a11dc7SNaveen Krishna Chatradhi ret = clk_prepare(data->clk_sec); 75914a11dc7SNaveen Krishna Chatradhi if (ret) { 76014a11dc7SNaveen Krishna Chatradhi dev_err(&pdev->dev, "Failed to get clock\n"); 76159dfa54cSAmit Daniel Kachhap return ret; 76214a11dc7SNaveen Krishna Chatradhi } 76314a11dc7SNaveen Krishna Chatradhi } 76414a11dc7SNaveen Krishna Chatradhi 76514a11dc7SNaveen Krishna Chatradhi ret = clk_prepare(data->clk); 76614a11dc7SNaveen Krishna Chatradhi if (ret) { 76714a11dc7SNaveen Krishna Chatradhi dev_err(&pdev->dev, "Failed to get clock\n"); 76814a11dc7SNaveen Krishna Chatradhi goto err_clk_sec; 76914a11dc7SNaveen Krishna Chatradhi } 77059dfa54cSAmit Daniel Kachhap 77159dfa54cSAmit Daniel Kachhap data->soc = pdata->type; 77272d1100bSBartlomiej Zolnierkiewicz 77372d1100bSBartlomiej Zolnierkiewicz switch (data->soc) { 77472d1100bSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS4210: 77572d1100bSBartlomiej Zolnierkiewicz data->tmu_initialize = exynos4210_tmu_initialize; 77637f9034fSBartlomiej Zolnierkiewicz data->tmu_control = exynos4210_tmu_control; 777b79985caSBartlomiej Zolnierkiewicz data->tmu_read = exynos4210_tmu_read; 778a7331f72SBartlomiej Zolnierkiewicz data->tmu_clear_irqs = exynos4210_tmu_clear_irqs; 77972d1100bSBartlomiej Zolnierkiewicz break; 78072d1100bSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS3250: 78172d1100bSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS4412: 78272d1100bSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS5250: 78372d1100bSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS5260: 78472d1100bSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS5420: 78572d1100bSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS5420_TRIMINFO: 78672d1100bSBartlomiej Zolnierkiewicz data->tmu_initialize = exynos4412_tmu_initialize; 78737f9034fSBartlomiej Zolnierkiewicz data->tmu_control = exynos4210_tmu_control; 788b79985caSBartlomiej Zolnierkiewicz data->tmu_read = exynos4412_tmu_read; 789285d994aSBartlomiej Zolnierkiewicz data->tmu_set_emulation = exynos4412_tmu_set_emulation; 790a7331f72SBartlomiej Zolnierkiewicz data->tmu_clear_irqs = exynos4210_tmu_clear_irqs; 79172d1100bSBartlomiej Zolnierkiewicz break; 79272d1100bSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS5440: 79372d1100bSBartlomiej Zolnierkiewicz data->tmu_initialize = exynos5440_tmu_initialize; 79437f9034fSBartlomiej Zolnierkiewicz data->tmu_control = exynos5440_tmu_control; 795b79985caSBartlomiej Zolnierkiewicz data->tmu_read = exynos5440_tmu_read; 796285d994aSBartlomiej Zolnierkiewicz data->tmu_set_emulation = exynos5440_tmu_set_emulation; 797a7331f72SBartlomiej Zolnierkiewicz data->tmu_clear_irqs = exynos5440_tmu_clear_irqs; 79872d1100bSBartlomiej Zolnierkiewicz break; 79972d1100bSBartlomiej Zolnierkiewicz default: 80059dfa54cSAmit Daniel Kachhap ret = -EINVAL; 80159dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "Platform not supported\n"); 80259dfa54cSAmit Daniel Kachhap goto err_clk; 80359dfa54cSAmit Daniel Kachhap } 80459dfa54cSAmit Daniel Kachhap 80559dfa54cSAmit Daniel Kachhap ret = exynos_tmu_initialize(pdev); 80659dfa54cSAmit Daniel Kachhap if (ret) { 80759dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to initialize TMU\n"); 80859dfa54cSAmit Daniel Kachhap goto err_clk; 80959dfa54cSAmit Daniel Kachhap } 81059dfa54cSAmit Daniel Kachhap 81159dfa54cSAmit Daniel Kachhap exynos_tmu_control(pdev, true); 81259dfa54cSAmit Daniel Kachhap 813cebe7373SAmit Daniel Kachhap /* Allocate a structure to register with the exynos core thermal */ 814cebe7373SAmit Daniel Kachhap sensor_conf = devm_kzalloc(&pdev->dev, 815cebe7373SAmit Daniel Kachhap sizeof(struct thermal_sensor_conf), GFP_KERNEL); 816cebe7373SAmit Daniel Kachhap if (!sensor_conf) { 817cebe7373SAmit Daniel Kachhap ret = -ENOMEM; 818cebe7373SAmit Daniel Kachhap goto err_clk; 819cebe7373SAmit Daniel Kachhap } 820cebe7373SAmit Daniel Kachhap sprintf(sensor_conf->name, "therm_zone%d", data->id); 821cebe7373SAmit Daniel Kachhap sensor_conf->read_temperature = (int (*)(void *))exynos_tmu_read; 822cebe7373SAmit Daniel Kachhap sensor_conf->write_emul_temp = 823cebe7373SAmit Daniel Kachhap (int (*)(void *, unsigned long))exynos_tmu_set_emulation; 824cebe7373SAmit Daniel Kachhap sensor_conf->driver_data = data; 825cebe7373SAmit Daniel Kachhap sensor_conf->trip_data.trip_count = pdata->trigger_enable[0] + 826bb34b4c8SAmit Daniel Kachhap pdata->trigger_enable[1] + pdata->trigger_enable[2]+ 827bb34b4c8SAmit Daniel Kachhap pdata->trigger_enable[3]; 82859dfa54cSAmit Daniel Kachhap 829cebe7373SAmit Daniel Kachhap for (i = 0; i < sensor_conf->trip_data.trip_count; i++) { 830cebe7373SAmit Daniel Kachhap sensor_conf->trip_data.trip_val[i] = 83159dfa54cSAmit Daniel Kachhap pdata->threshold + pdata->trigger_levels[i]; 832cebe7373SAmit Daniel Kachhap sensor_conf->trip_data.trip_type[i] = 8335c3cf552SAmit Daniel Kachhap pdata->trigger_type[i]; 8345c3cf552SAmit Daniel Kachhap } 83559dfa54cSAmit Daniel Kachhap 836cebe7373SAmit Daniel Kachhap sensor_conf->trip_data.trigger_falling = pdata->threshold_falling; 83759dfa54cSAmit Daniel Kachhap 838cebe7373SAmit Daniel Kachhap sensor_conf->cooling_data.freq_clip_count = pdata->freq_tab_count; 83959dfa54cSAmit Daniel Kachhap for (i = 0; i < pdata->freq_tab_count; i++) { 840cebe7373SAmit Daniel Kachhap sensor_conf->cooling_data.freq_data[i].freq_clip_max = 84159dfa54cSAmit Daniel Kachhap pdata->freq_tab[i].freq_clip_max; 842cebe7373SAmit Daniel Kachhap sensor_conf->cooling_data.freq_data[i].temp_level = 84359dfa54cSAmit Daniel Kachhap pdata->freq_tab[i].temp_level; 84459dfa54cSAmit Daniel Kachhap } 845cebe7373SAmit Daniel Kachhap sensor_conf->dev = &pdev->dev; 846cebe7373SAmit Daniel Kachhap /* Register the sensor with thermal management interface */ 847cebe7373SAmit Daniel Kachhap ret = exynos_register_thermal(sensor_conf); 84859dfa54cSAmit Daniel Kachhap if (ret) { 84959dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to register thermal interface\n"); 85059dfa54cSAmit Daniel Kachhap goto err_clk; 85159dfa54cSAmit Daniel Kachhap } 852cebe7373SAmit Daniel Kachhap data->reg_conf = sensor_conf; 853cebe7373SAmit Daniel Kachhap 854cebe7373SAmit Daniel Kachhap ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq, 855cebe7373SAmit Daniel Kachhap IRQF_TRIGGER_RISING | IRQF_SHARED, dev_name(&pdev->dev), data); 856cebe7373SAmit Daniel Kachhap if (ret) { 857cebe7373SAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq); 858cebe7373SAmit Daniel Kachhap goto err_clk; 859cebe7373SAmit Daniel Kachhap } 86059dfa54cSAmit Daniel Kachhap 86159dfa54cSAmit Daniel Kachhap return 0; 86259dfa54cSAmit Daniel Kachhap err_clk: 86359dfa54cSAmit Daniel Kachhap clk_unprepare(data->clk); 86414a11dc7SNaveen Krishna Chatradhi err_clk_sec: 86514a11dc7SNaveen Krishna Chatradhi if (!IS_ERR(data->clk_sec)) 86614a11dc7SNaveen Krishna Chatradhi clk_unprepare(data->clk_sec); 86759dfa54cSAmit Daniel Kachhap return ret; 86859dfa54cSAmit Daniel Kachhap } 86959dfa54cSAmit Daniel Kachhap 87059dfa54cSAmit Daniel Kachhap static int exynos_tmu_remove(struct platform_device *pdev) 87159dfa54cSAmit Daniel Kachhap { 87259dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = platform_get_drvdata(pdev); 87359dfa54cSAmit Daniel Kachhap 874cebe7373SAmit Daniel Kachhap exynos_unregister_thermal(data->reg_conf); 87559dfa54cSAmit Daniel Kachhap 8764215688eSBartlomiej Zolnierkiewicz exynos_tmu_control(pdev, false); 8774215688eSBartlomiej Zolnierkiewicz 87859dfa54cSAmit Daniel Kachhap clk_unprepare(data->clk); 87914a11dc7SNaveen Krishna Chatradhi if (!IS_ERR(data->clk_sec)) 88014a11dc7SNaveen Krishna Chatradhi clk_unprepare(data->clk_sec); 88159dfa54cSAmit Daniel Kachhap 882498d22f6SAmit Daniel Kachhap if (!IS_ERR(data->regulator)) 883498d22f6SAmit Daniel Kachhap regulator_disable(data->regulator); 884498d22f6SAmit Daniel Kachhap 88559dfa54cSAmit Daniel Kachhap return 0; 88659dfa54cSAmit Daniel Kachhap } 88759dfa54cSAmit Daniel Kachhap 88859dfa54cSAmit Daniel Kachhap #ifdef CONFIG_PM_SLEEP 88959dfa54cSAmit Daniel Kachhap static int exynos_tmu_suspend(struct device *dev) 89059dfa54cSAmit Daniel Kachhap { 89159dfa54cSAmit Daniel Kachhap exynos_tmu_control(to_platform_device(dev), false); 89259dfa54cSAmit Daniel Kachhap 89359dfa54cSAmit Daniel Kachhap return 0; 89459dfa54cSAmit Daniel Kachhap } 89559dfa54cSAmit Daniel Kachhap 89659dfa54cSAmit Daniel Kachhap static int exynos_tmu_resume(struct device *dev) 89759dfa54cSAmit Daniel Kachhap { 89859dfa54cSAmit Daniel Kachhap struct platform_device *pdev = to_platform_device(dev); 89959dfa54cSAmit Daniel Kachhap 90059dfa54cSAmit Daniel Kachhap exynos_tmu_initialize(pdev); 90159dfa54cSAmit Daniel Kachhap exynos_tmu_control(pdev, true); 90259dfa54cSAmit Daniel Kachhap 90359dfa54cSAmit Daniel Kachhap return 0; 90459dfa54cSAmit Daniel Kachhap } 90559dfa54cSAmit Daniel Kachhap 90659dfa54cSAmit Daniel Kachhap static SIMPLE_DEV_PM_OPS(exynos_tmu_pm, 90759dfa54cSAmit Daniel Kachhap exynos_tmu_suspend, exynos_tmu_resume); 90859dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM (&exynos_tmu_pm) 90959dfa54cSAmit Daniel Kachhap #else 91059dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM NULL 91159dfa54cSAmit Daniel Kachhap #endif 91259dfa54cSAmit Daniel Kachhap 91359dfa54cSAmit Daniel Kachhap static struct platform_driver exynos_tmu_driver = { 91459dfa54cSAmit Daniel Kachhap .driver = { 91559dfa54cSAmit Daniel Kachhap .name = "exynos-tmu", 91659dfa54cSAmit Daniel Kachhap .owner = THIS_MODULE, 91759dfa54cSAmit Daniel Kachhap .pm = EXYNOS_TMU_PM, 91873b5b1d7SSachin Kamat .of_match_table = exynos_tmu_match, 91959dfa54cSAmit Daniel Kachhap }, 92059dfa54cSAmit Daniel Kachhap .probe = exynos_tmu_probe, 92159dfa54cSAmit Daniel Kachhap .remove = exynos_tmu_remove, 92259dfa54cSAmit Daniel Kachhap }; 92359dfa54cSAmit Daniel Kachhap 92459dfa54cSAmit Daniel Kachhap module_platform_driver(exynos_tmu_driver); 92559dfa54cSAmit Daniel Kachhap 92659dfa54cSAmit Daniel Kachhap MODULE_DESCRIPTION("EXYNOS TMU Driver"); 92759dfa54cSAmit Daniel Kachhap MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>"); 92859dfa54cSAmit Daniel Kachhap MODULE_LICENSE("GPL"); 92959dfa54cSAmit Daniel Kachhap MODULE_ALIAS("platform:exynos-tmu"); 930