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 56cebe7373SAmit Daniel Kachhap */ 5759dfa54cSAmit Daniel Kachhap struct exynos_tmu_data { 58cebe7373SAmit Daniel Kachhap int id; 5959dfa54cSAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata; 6059dfa54cSAmit Daniel Kachhap void __iomem *base; 619025d563SNaveen Krishna Chatradhi void __iomem *base_second; 6259dfa54cSAmit Daniel Kachhap int irq; 6359dfa54cSAmit Daniel Kachhap enum soc_type soc; 6459dfa54cSAmit Daniel Kachhap struct work_struct irq_work; 6559dfa54cSAmit Daniel Kachhap struct mutex lock; 6614a11dc7SNaveen Krishna Chatradhi struct clk *clk, *clk_sec; 6759dfa54cSAmit Daniel Kachhap u8 temp_error1, temp_error2; 68498d22f6SAmit Daniel Kachhap struct regulator *regulator; 69cebe7373SAmit Daniel Kachhap struct thermal_sensor_conf *reg_conf; 7072d1100bSBartlomiej Zolnierkiewicz int (*tmu_initialize)(struct platform_device *pdev); 7159dfa54cSAmit Daniel Kachhap }; 7259dfa54cSAmit Daniel Kachhap 7359dfa54cSAmit Daniel Kachhap /* 7459dfa54cSAmit Daniel Kachhap * TMU treats temperature as a mapped temperature code. 7559dfa54cSAmit Daniel Kachhap * The temperature is converted differently depending on the calibration type. 7659dfa54cSAmit Daniel Kachhap */ 7759dfa54cSAmit Daniel Kachhap static int temp_to_code(struct exynos_tmu_data *data, u8 temp) 7859dfa54cSAmit Daniel Kachhap { 7959dfa54cSAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata = data->pdata; 8059dfa54cSAmit Daniel Kachhap int temp_code; 8159dfa54cSAmit Daniel Kachhap 8259dfa54cSAmit Daniel Kachhap switch (pdata->cal_type) { 8359dfa54cSAmit Daniel Kachhap case TYPE_TWO_POINT_TRIMMING: 84bb34b4c8SAmit Daniel Kachhap temp_code = (temp - pdata->first_point_trim) * 8559dfa54cSAmit Daniel Kachhap (data->temp_error2 - data->temp_error1) / 86bb34b4c8SAmit Daniel Kachhap (pdata->second_point_trim - pdata->first_point_trim) + 87bb34b4c8SAmit Daniel Kachhap data->temp_error1; 8859dfa54cSAmit Daniel Kachhap break; 8959dfa54cSAmit Daniel Kachhap case TYPE_ONE_POINT_TRIMMING: 90bb34b4c8SAmit Daniel Kachhap temp_code = temp + data->temp_error1 - pdata->first_point_trim; 9159dfa54cSAmit Daniel Kachhap break; 9259dfa54cSAmit Daniel Kachhap default: 93bb34b4c8SAmit Daniel Kachhap temp_code = temp + pdata->default_temp_offset; 9459dfa54cSAmit Daniel Kachhap break; 9559dfa54cSAmit Daniel Kachhap } 96ddb31d43SBartlomiej Zolnierkiewicz 9759dfa54cSAmit Daniel Kachhap return temp_code; 9859dfa54cSAmit Daniel Kachhap } 9959dfa54cSAmit Daniel Kachhap 10059dfa54cSAmit Daniel Kachhap /* 10159dfa54cSAmit Daniel Kachhap * Calculate a temperature value from a temperature code. 10259dfa54cSAmit Daniel Kachhap * The unit of the temperature is degree Celsius. 10359dfa54cSAmit Daniel Kachhap */ 10459dfa54cSAmit Daniel Kachhap static int code_to_temp(struct exynos_tmu_data *data, u8 temp_code) 10559dfa54cSAmit Daniel Kachhap { 10659dfa54cSAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata = data->pdata; 10759dfa54cSAmit Daniel Kachhap int temp; 10859dfa54cSAmit Daniel Kachhap 10959dfa54cSAmit Daniel Kachhap switch (pdata->cal_type) { 11059dfa54cSAmit Daniel Kachhap case TYPE_TWO_POINT_TRIMMING: 111bb34b4c8SAmit Daniel Kachhap temp = (temp_code - data->temp_error1) * 112bb34b4c8SAmit Daniel Kachhap (pdata->second_point_trim - pdata->first_point_trim) / 113bb34b4c8SAmit Daniel Kachhap (data->temp_error2 - data->temp_error1) + 114bb34b4c8SAmit Daniel Kachhap pdata->first_point_trim; 11559dfa54cSAmit Daniel Kachhap break; 11659dfa54cSAmit Daniel Kachhap case TYPE_ONE_POINT_TRIMMING: 117bb34b4c8SAmit Daniel Kachhap temp = temp_code - data->temp_error1 + pdata->first_point_trim; 11859dfa54cSAmit Daniel Kachhap break; 11959dfa54cSAmit Daniel Kachhap default: 120bb34b4c8SAmit Daniel Kachhap temp = temp_code - pdata->default_temp_offset; 12159dfa54cSAmit Daniel Kachhap break; 12259dfa54cSAmit Daniel Kachhap } 123ddb31d43SBartlomiej Zolnierkiewicz 12459dfa54cSAmit Daniel Kachhap return temp; 12559dfa54cSAmit Daniel Kachhap } 12659dfa54cSAmit Daniel Kachhap 127b835ced1SBartlomiej Zolnierkiewicz static void exynos_tmu_clear_irqs(struct exynos_tmu_data *data) 128b835ced1SBartlomiej Zolnierkiewicz { 129b835ced1SBartlomiej Zolnierkiewicz const struct exynos_tmu_registers *reg = data->pdata->registers; 130b835ced1SBartlomiej Zolnierkiewicz unsigned int val_irq; 131b835ced1SBartlomiej Zolnierkiewicz 132b835ced1SBartlomiej Zolnierkiewicz val_irq = readl(data->base + reg->tmu_intstat); 133b835ced1SBartlomiej Zolnierkiewicz /* 134b835ced1SBartlomiej Zolnierkiewicz * Clear the interrupts. Please note that the documentation for 135b835ced1SBartlomiej Zolnierkiewicz * Exynos3250, Exynos4412, Exynos5250 and Exynos5260 incorrectly 136b835ced1SBartlomiej Zolnierkiewicz * states that INTCLEAR register has a different placing of bits 137b835ced1SBartlomiej Zolnierkiewicz * responsible for FALL IRQs than INTSTAT register. Exynos5420 138b835ced1SBartlomiej Zolnierkiewicz * and Exynos5440 documentation is correct (Exynos4210 doesn't 139b835ced1SBartlomiej Zolnierkiewicz * support FALL IRQs at all). 140b835ced1SBartlomiej Zolnierkiewicz */ 141b835ced1SBartlomiej Zolnierkiewicz writel(val_irq, data->base + reg->tmu_intclear); 142b835ced1SBartlomiej Zolnierkiewicz } 143b835ced1SBartlomiej Zolnierkiewicz 1448328a4b1SBartlomiej Zolnierkiewicz static void sanitize_temp_error(struct exynos_tmu_data *data, u32 trim_info) 1458328a4b1SBartlomiej Zolnierkiewicz { 1468328a4b1SBartlomiej Zolnierkiewicz struct exynos_tmu_platform_data *pdata = data->pdata; 1478328a4b1SBartlomiej Zolnierkiewicz 1488328a4b1SBartlomiej Zolnierkiewicz data->temp_error1 = trim_info & EXYNOS_TMU_TEMP_MASK; 1498328a4b1SBartlomiej Zolnierkiewicz data->temp_error2 = ((trim_info >> EXYNOS_TRIMINFO_85_SHIFT) & 1508328a4b1SBartlomiej Zolnierkiewicz EXYNOS_TMU_TEMP_MASK); 1518328a4b1SBartlomiej Zolnierkiewicz 1528328a4b1SBartlomiej Zolnierkiewicz if (!data->temp_error1 || 1538328a4b1SBartlomiej Zolnierkiewicz (pdata->min_efuse_value > data->temp_error1) || 1548328a4b1SBartlomiej Zolnierkiewicz (data->temp_error1 > pdata->max_efuse_value)) 1558328a4b1SBartlomiej Zolnierkiewicz data->temp_error1 = pdata->efuse_value & EXYNOS_TMU_TEMP_MASK; 1568328a4b1SBartlomiej Zolnierkiewicz 1578328a4b1SBartlomiej Zolnierkiewicz if (!data->temp_error2) 1588328a4b1SBartlomiej Zolnierkiewicz data->temp_error2 = 1598328a4b1SBartlomiej Zolnierkiewicz (pdata->efuse_value >> EXYNOS_TRIMINFO_85_SHIFT) & 1608328a4b1SBartlomiej Zolnierkiewicz EXYNOS_TMU_TEMP_MASK; 1618328a4b1SBartlomiej Zolnierkiewicz } 1628328a4b1SBartlomiej Zolnierkiewicz 163fe87789cSBartlomiej Zolnierkiewicz static u32 get_th_reg(struct exynos_tmu_data *data, u32 threshold, bool falling) 164fe87789cSBartlomiej Zolnierkiewicz { 165fe87789cSBartlomiej Zolnierkiewicz struct exynos_tmu_platform_data *pdata = data->pdata; 166fe87789cSBartlomiej Zolnierkiewicz int i; 167fe87789cSBartlomiej Zolnierkiewicz 168fe87789cSBartlomiej Zolnierkiewicz for (i = 0; i < pdata->non_hw_trigger_levels; i++) { 169fe87789cSBartlomiej Zolnierkiewicz u8 temp = pdata->trigger_levels[i]; 170fe87789cSBartlomiej Zolnierkiewicz 171fe87789cSBartlomiej Zolnierkiewicz if (falling) 172fe87789cSBartlomiej Zolnierkiewicz temp -= pdata->threshold_falling; 173fe87789cSBartlomiej Zolnierkiewicz else 174fe87789cSBartlomiej Zolnierkiewicz threshold &= ~(0xff << 8 * i); 175fe87789cSBartlomiej Zolnierkiewicz 176fe87789cSBartlomiej Zolnierkiewicz threshold |= temp_to_code(data, temp) << 8 * i; 177fe87789cSBartlomiej Zolnierkiewicz } 178fe87789cSBartlomiej Zolnierkiewicz 179fe87789cSBartlomiej Zolnierkiewicz return threshold; 180fe87789cSBartlomiej Zolnierkiewicz } 181fe87789cSBartlomiej Zolnierkiewicz 18259dfa54cSAmit Daniel Kachhap static int exynos_tmu_initialize(struct platform_device *pdev) 18359dfa54cSAmit Daniel Kachhap { 18459dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = platform_get_drvdata(pdev); 18572d1100bSBartlomiej Zolnierkiewicz int ret; 18659dfa54cSAmit Daniel Kachhap 18759dfa54cSAmit Daniel Kachhap mutex_lock(&data->lock); 18859dfa54cSAmit Daniel Kachhap clk_enable(data->clk); 18914a11dc7SNaveen Krishna Chatradhi if (!IS_ERR(data->clk_sec)) 19014a11dc7SNaveen Krishna Chatradhi clk_enable(data->clk_sec); 19172d1100bSBartlomiej Zolnierkiewicz ret = data->tmu_initialize(pdev); 19259dfa54cSAmit Daniel Kachhap clk_disable(data->clk); 19359dfa54cSAmit Daniel Kachhap mutex_unlock(&data->lock); 19414a11dc7SNaveen Krishna Chatradhi if (!IS_ERR(data->clk_sec)) 19514a11dc7SNaveen Krishna Chatradhi clk_disable(data->clk_sec); 19659dfa54cSAmit Daniel Kachhap 19759dfa54cSAmit Daniel Kachhap return ret; 19859dfa54cSAmit Daniel Kachhap } 19959dfa54cSAmit Daniel Kachhap 200*d00671c3SBartlomiej Zolnierkiewicz static u32 get_con_reg(struct exynos_tmu_data *data, u32 con) 20159dfa54cSAmit Daniel Kachhap { 20259dfa54cSAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata = data->pdata; 20359dfa54cSAmit Daniel Kachhap 20486f5362eSLukasz Majewski if (pdata->test_mux) 205bfb2b88cSBartlomiej Zolnierkiewicz con |= (pdata->test_mux << EXYNOS4412_MUX_ADDR_SHIFT); 20686f5362eSLukasz Majewski 20799d67fb9SBartlomiej Zolnierkiewicz con &= ~(EXYNOS_TMU_REF_VOLTAGE_MASK << EXYNOS_TMU_REF_VOLTAGE_SHIFT); 20899d67fb9SBartlomiej Zolnierkiewicz con |= pdata->reference_voltage << EXYNOS_TMU_REF_VOLTAGE_SHIFT; 209d0a0ce3eSAmit Daniel Kachhap 21099d67fb9SBartlomiej Zolnierkiewicz con &= ~(EXYNOS_TMU_BUF_SLOPE_SEL_MASK << EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT); 21199d67fb9SBartlomiej Zolnierkiewicz con |= (pdata->gain << EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT); 212d0a0ce3eSAmit Daniel Kachhap 213d0a0ce3eSAmit Daniel Kachhap if (pdata->noise_cancel_mode) { 214b9504a6aSBartlomiej Zolnierkiewicz con &= ~(EXYNOS_TMU_TRIP_MODE_MASK << EXYNOS_TMU_TRIP_MODE_SHIFT); 215b9504a6aSBartlomiej Zolnierkiewicz con |= (pdata->noise_cancel_mode << EXYNOS_TMU_TRIP_MODE_SHIFT); 21659dfa54cSAmit Daniel Kachhap } 21759dfa54cSAmit Daniel Kachhap 218*d00671c3SBartlomiej Zolnierkiewicz return con; 219*d00671c3SBartlomiej Zolnierkiewicz } 220*d00671c3SBartlomiej Zolnierkiewicz 221*d00671c3SBartlomiej Zolnierkiewicz static void exynos_tmu_control(struct platform_device *pdev, bool on) 222*d00671c3SBartlomiej Zolnierkiewicz { 223*d00671c3SBartlomiej Zolnierkiewicz struct exynos_tmu_data *data = platform_get_drvdata(pdev); 224*d00671c3SBartlomiej Zolnierkiewicz struct exynos_tmu_platform_data *pdata = data->pdata; 225*d00671c3SBartlomiej Zolnierkiewicz const struct exynos_tmu_registers *reg = pdata->registers; 226*d00671c3SBartlomiej Zolnierkiewicz unsigned int con, interrupt_en; 227*d00671c3SBartlomiej Zolnierkiewicz 228*d00671c3SBartlomiej Zolnierkiewicz mutex_lock(&data->lock); 229*d00671c3SBartlomiej Zolnierkiewicz clk_enable(data->clk); 230*d00671c3SBartlomiej Zolnierkiewicz 231*d00671c3SBartlomiej Zolnierkiewicz con = get_con_reg(data, readl(data->base + reg->tmu_ctrl)); 232*d00671c3SBartlomiej Zolnierkiewicz 23359dfa54cSAmit Daniel Kachhap if (on) { 23499d67fb9SBartlomiej Zolnierkiewicz con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT); 235d0a0ce3eSAmit Daniel Kachhap interrupt_en = 236b8d582b9SAmit Daniel Kachhap pdata->trigger_enable[3] << reg->inten_rise3_shift | 237b8d582b9SAmit Daniel Kachhap pdata->trigger_enable[2] << reg->inten_rise2_shift | 238b8d582b9SAmit Daniel Kachhap pdata->trigger_enable[1] << reg->inten_rise1_shift | 239b8d582b9SAmit Daniel Kachhap pdata->trigger_enable[0] << reg->inten_rise0_shift; 240f4dae753SAmit Daniel Kachhap if (TMU_SUPPORTS(pdata, FALLING_TRIP)) 241d0a0ce3eSAmit Daniel Kachhap interrupt_en |= 242b8d582b9SAmit Daniel Kachhap interrupt_en << reg->inten_fall0_shift; 24359dfa54cSAmit Daniel Kachhap } else { 24499d67fb9SBartlomiej Zolnierkiewicz con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT); 24559dfa54cSAmit Daniel Kachhap interrupt_en = 0; /* Disable all interrupts */ 24659dfa54cSAmit Daniel Kachhap } 247b8d582b9SAmit Daniel Kachhap writel(interrupt_en, data->base + reg->tmu_inten); 248b8d582b9SAmit Daniel Kachhap writel(con, data->base + reg->tmu_ctrl); 24959dfa54cSAmit Daniel Kachhap 25059dfa54cSAmit Daniel Kachhap clk_disable(data->clk); 25159dfa54cSAmit Daniel Kachhap mutex_unlock(&data->lock); 25259dfa54cSAmit Daniel Kachhap } 25359dfa54cSAmit Daniel Kachhap 25472d1100bSBartlomiej Zolnierkiewicz static int exynos4210_tmu_initialize(struct platform_device *pdev) 25572d1100bSBartlomiej Zolnierkiewicz { 25672d1100bSBartlomiej Zolnierkiewicz struct exynos_tmu_data *data = platform_get_drvdata(pdev); 25772d1100bSBartlomiej Zolnierkiewicz struct exynos_tmu_platform_data *pdata = data->pdata; 25872d1100bSBartlomiej Zolnierkiewicz unsigned int status; 25972d1100bSBartlomiej Zolnierkiewicz int ret = 0, threshold_code, i; 26072d1100bSBartlomiej Zolnierkiewicz 26172d1100bSBartlomiej Zolnierkiewicz status = readb(data->base + EXYNOS_TMU_REG_STATUS); 26272d1100bSBartlomiej Zolnierkiewicz if (!status) { 26372d1100bSBartlomiej Zolnierkiewicz ret = -EBUSY; 26472d1100bSBartlomiej Zolnierkiewicz goto out; 26572d1100bSBartlomiej Zolnierkiewicz } 26672d1100bSBartlomiej Zolnierkiewicz 26772d1100bSBartlomiej Zolnierkiewicz sanitize_temp_error(data, readl(data->base + EXYNOS_TMU_REG_TRIMINFO)); 26872d1100bSBartlomiej Zolnierkiewicz 26972d1100bSBartlomiej Zolnierkiewicz /* Write temperature code for threshold */ 27072d1100bSBartlomiej Zolnierkiewicz threshold_code = temp_to_code(data, pdata->threshold); 27172d1100bSBartlomiej Zolnierkiewicz writeb(threshold_code, data->base + EXYNOS4210_TMU_REG_THRESHOLD_TEMP); 27272d1100bSBartlomiej Zolnierkiewicz 27372d1100bSBartlomiej Zolnierkiewicz for (i = 0; i < pdata->non_hw_trigger_levels; i++) 27472d1100bSBartlomiej Zolnierkiewicz writeb(pdata->trigger_levels[i], data->base + 27572d1100bSBartlomiej Zolnierkiewicz EXYNOS4210_TMU_REG_TRIG_LEVEL0 + i * 4); 27672d1100bSBartlomiej Zolnierkiewicz 27772d1100bSBartlomiej Zolnierkiewicz exynos_tmu_clear_irqs(data); 27872d1100bSBartlomiej Zolnierkiewicz out: 27972d1100bSBartlomiej Zolnierkiewicz return ret; 28072d1100bSBartlomiej Zolnierkiewicz } 28172d1100bSBartlomiej Zolnierkiewicz 28272d1100bSBartlomiej Zolnierkiewicz static int exynos4412_tmu_initialize(struct platform_device *pdev) 28372d1100bSBartlomiej Zolnierkiewicz { 28472d1100bSBartlomiej Zolnierkiewicz struct exynos_tmu_data *data = platform_get_drvdata(pdev); 28572d1100bSBartlomiej Zolnierkiewicz struct exynos_tmu_platform_data *pdata = data->pdata; 28672d1100bSBartlomiej Zolnierkiewicz unsigned int status, trim_info, con, ctrl, rising_threshold; 28772d1100bSBartlomiej Zolnierkiewicz int ret = 0, threshold_code, i; 28872d1100bSBartlomiej Zolnierkiewicz 28972d1100bSBartlomiej Zolnierkiewicz status = readb(data->base + EXYNOS_TMU_REG_STATUS); 29072d1100bSBartlomiej Zolnierkiewicz if (!status) { 29172d1100bSBartlomiej Zolnierkiewicz ret = -EBUSY; 29272d1100bSBartlomiej Zolnierkiewicz goto out; 29372d1100bSBartlomiej Zolnierkiewicz } 29472d1100bSBartlomiej Zolnierkiewicz 29572d1100bSBartlomiej Zolnierkiewicz if (data->soc == SOC_ARCH_EXYNOS3250 || 29672d1100bSBartlomiej Zolnierkiewicz data->soc == SOC_ARCH_EXYNOS4412 || 29772d1100bSBartlomiej Zolnierkiewicz data->soc == SOC_ARCH_EXYNOS5250) { 29872d1100bSBartlomiej Zolnierkiewicz if (data->soc == SOC_ARCH_EXYNOS3250) { 29972d1100bSBartlomiej Zolnierkiewicz ctrl = readl(data->base + EXYNOS_TMU_TRIMINFO_CON1); 30072d1100bSBartlomiej Zolnierkiewicz ctrl |= EXYNOS_TRIMINFO_RELOAD_ENABLE; 30172d1100bSBartlomiej Zolnierkiewicz writel(ctrl, data->base + EXYNOS_TMU_TRIMINFO_CON1); 30272d1100bSBartlomiej Zolnierkiewicz } 30372d1100bSBartlomiej Zolnierkiewicz ctrl = readl(data->base + EXYNOS_TMU_TRIMINFO_CON2); 30472d1100bSBartlomiej Zolnierkiewicz ctrl |= EXYNOS_TRIMINFO_RELOAD_ENABLE; 30572d1100bSBartlomiej Zolnierkiewicz writel(ctrl, data->base + EXYNOS_TMU_TRIMINFO_CON2); 30672d1100bSBartlomiej Zolnierkiewicz } 30772d1100bSBartlomiej Zolnierkiewicz 30872d1100bSBartlomiej Zolnierkiewicz /* On exynos5420 the triminfo register is in the shared space */ 30972d1100bSBartlomiej Zolnierkiewicz if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO) 31072d1100bSBartlomiej Zolnierkiewicz trim_info = readl(data->base_second + EXYNOS_TMU_REG_TRIMINFO); 31172d1100bSBartlomiej Zolnierkiewicz else 31272d1100bSBartlomiej Zolnierkiewicz trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO); 31372d1100bSBartlomiej Zolnierkiewicz 31472d1100bSBartlomiej Zolnierkiewicz sanitize_temp_error(data, trim_info); 31572d1100bSBartlomiej Zolnierkiewicz 31672d1100bSBartlomiej Zolnierkiewicz /* Write temperature code for rising and falling threshold */ 31772d1100bSBartlomiej Zolnierkiewicz rising_threshold = readl(data->base + EXYNOS_THD_TEMP_RISE); 31872d1100bSBartlomiej Zolnierkiewicz rising_threshold = get_th_reg(data, rising_threshold, false); 31972d1100bSBartlomiej Zolnierkiewicz writel(rising_threshold, data->base + EXYNOS_THD_TEMP_RISE); 32072d1100bSBartlomiej Zolnierkiewicz writel(get_th_reg(data, 0, true), data->base + EXYNOS_THD_TEMP_FALL); 32172d1100bSBartlomiej Zolnierkiewicz 32272d1100bSBartlomiej Zolnierkiewicz exynos_tmu_clear_irqs(data); 32372d1100bSBartlomiej Zolnierkiewicz 32472d1100bSBartlomiej Zolnierkiewicz /* if last threshold limit is also present */ 32572d1100bSBartlomiej Zolnierkiewicz i = pdata->max_trigger_level - 1; 32672d1100bSBartlomiej Zolnierkiewicz if (pdata->trigger_levels[i] && pdata->trigger_type[i] == HW_TRIP) { 32772d1100bSBartlomiej Zolnierkiewicz threshold_code = temp_to_code(data, pdata->trigger_levels[i]); 32872d1100bSBartlomiej Zolnierkiewicz /* 1-4 level to be assigned in th0 reg */ 32972d1100bSBartlomiej Zolnierkiewicz rising_threshold &= ~(0xff << 8 * i); 33072d1100bSBartlomiej Zolnierkiewicz rising_threshold |= threshold_code << 8 * i; 33172d1100bSBartlomiej Zolnierkiewicz writel(rising_threshold, data->base + EXYNOS_THD_TEMP_RISE); 33272d1100bSBartlomiej Zolnierkiewicz con = readl(data->base + EXYNOS_TMU_REG_CONTROL); 33372d1100bSBartlomiej Zolnierkiewicz con |= (1 << EXYNOS_TMU_THERM_TRIP_EN_SHIFT); 33472d1100bSBartlomiej Zolnierkiewicz writel(con, data->base + EXYNOS_TMU_REG_CONTROL); 33572d1100bSBartlomiej Zolnierkiewicz } 33672d1100bSBartlomiej Zolnierkiewicz out: 33772d1100bSBartlomiej Zolnierkiewicz return ret; 33872d1100bSBartlomiej Zolnierkiewicz } 33972d1100bSBartlomiej Zolnierkiewicz 34072d1100bSBartlomiej Zolnierkiewicz static int exynos5440_tmu_initialize(struct platform_device *pdev) 34172d1100bSBartlomiej Zolnierkiewicz { 34272d1100bSBartlomiej Zolnierkiewicz struct exynos_tmu_data *data = platform_get_drvdata(pdev); 34372d1100bSBartlomiej Zolnierkiewicz struct exynos_tmu_platform_data *pdata = data->pdata; 34472d1100bSBartlomiej Zolnierkiewicz unsigned int trim_info = 0, con, rising_threshold; 34572d1100bSBartlomiej Zolnierkiewicz int ret = 0, threshold_code, i; 34672d1100bSBartlomiej Zolnierkiewicz 34772d1100bSBartlomiej Zolnierkiewicz /* 34872d1100bSBartlomiej Zolnierkiewicz * For exynos5440 soc triminfo value is swapped between TMU0 and 34972d1100bSBartlomiej Zolnierkiewicz * TMU2, so the below logic is needed. 35072d1100bSBartlomiej Zolnierkiewicz */ 35172d1100bSBartlomiej Zolnierkiewicz switch (data->id) { 35272d1100bSBartlomiej Zolnierkiewicz case 0: 35372d1100bSBartlomiej Zolnierkiewicz trim_info = readl(data->base + EXYNOS5440_EFUSE_SWAP_OFFSET + 35472d1100bSBartlomiej Zolnierkiewicz EXYNOS5440_TMU_S0_7_TRIM); 35572d1100bSBartlomiej Zolnierkiewicz break; 35672d1100bSBartlomiej Zolnierkiewicz case 1: 35772d1100bSBartlomiej Zolnierkiewicz trim_info = readl(data->base + EXYNOS5440_TMU_S0_7_TRIM); 35872d1100bSBartlomiej Zolnierkiewicz break; 35972d1100bSBartlomiej Zolnierkiewicz case 2: 36072d1100bSBartlomiej Zolnierkiewicz trim_info = readl(data->base - EXYNOS5440_EFUSE_SWAP_OFFSET + 36172d1100bSBartlomiej Zolnierkiewicz EXYNOS5440_TMU_S0_7_TRIM); 36272d1100bSBartlomiej Zolnierkiewicz } 36372d1100bSBartlomiej Zolnierkiewicz sanitize_temp_error(data, trim_info); 36472d1100bSBartlomiej Zolnierkiewicz 36572d1100bSBartlomiej Zolnierkiewicz /* Write temperature code for rising and falling threshold */ 36672d1100bSBartlomiej Zolnierkiewicz rising_threshold = readl(data->base + EXYNOS5440_TMU_S0_7_TH0); 36772d1100bSBartlomiej Zolnierkiewicz rising_threshold = get_th_reg(data, rising_threshold, false); 36872d1100bSBartlomiej Zolnierkiewicz writel(rising_threshold, data->base + EXYNOS5440_TMU_S0_7_TH0); 36972d1100bSBartlomiej Zolnierkiewicz writel(0, data->base + EXYNOS5440_TMU_S0_7_TH1); 37072d1100bSBartlomiej Zolnierkiewicz 37172d1100bSBartlomiej Zolnierkiewicz exynos_tmu_clear_irqs(data); 37272d1100bSBartlomiej Zolnierkiewicz 37372d1100bSBartlomiej Zolnierkiewicz /* if last threshold limit is also present */ 37472d1100bSBartlomiej Zolnierkiewicz i = pdata->max_trigger_level - 1; 37572d1100bSBartlomiej Zolnierkiewicz if (pdata->trigger_levels[i] && pdata->trigger_type[i] == HW_TRIP) { 37672d1100bSBartlomiej Zolnierkiewicz threshold_code = temp_to_code(data, pdata->trigger_levels[i]); 37772d1100bSBartlomiej Zolnierkiewicz /* 5th level to be assigned in th2 reg */ 37872d1100bSBartlomiej Zolnierkiewicz rising_threshold = 37972d1100bSBartlomiej Zolnierkiewicz threshold_code << EXYNOS5440_TMU_TH_RISE4_SHIFT; 38072d1100bSBartlomiej Zolnierkiewicz writel(rising_threshold, data->base + EXYNOS5440_TMU_S0_7_TH2); 38172d1100bSBartlomiej Zolnierkiewicz con = readl(data->base + EXYNOS5440_TMU_S0_7_CTRL); 38272d1100bSBartlomiej Zolnierkiewicz con |= (1 << EXYNOS_TMU_THERM_TRIP_EN_SHIFT); 38372d1100bSBartlomiej Zolnierkiewicz writel(con, data->base + EXYNOS5440_TMU_S0_7_CTRL); 38472d1100bSBartlomiej Zolnierkiewicz } 38572d1100bSBartlomiej Zolnierkiewicz /* Clear the PMIN in the common TMU register */ 38672d1100bSBartlomiej Zolnierkiewicz if (!data->id) 38772d1100bSBartlomiej Zolnierkiewicz writel(0, data->base_second + EXYNOS5440_TMU_PMIN); 38872d1100bSBartlomiej Zolnierkiewicz return ret; 38972d1100bSBartlomiej Zolnierkiewicz } 39072d1100bSBartlomiej Zolnierkiewicz 39159dfa54cSAmit Daniel Kachhap static int exynos_tmu_read(struct exynos_tmu_data *data) 39259dfa54cSAmit Daniel Kachhap { 393b8d582b9SAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata = data->pdata; 394b8d582b9SAmit Daniel Kachhap const struct exynos_tmu_registers *reg = pdata->registers; 39559dfa54cSAmit Daniel Kachhap u8 temp_code; 39659dfa54cSAmit Daniel Kachhap int temp; 39759dfa54cSAmit Daniel Kachhap 39859dfa54cSAmit Daniel Kachhap mutex_lock(&data->lock); 39959dfa54cSAmit Daniel Kachhap clk_enable(data->clk); 40059dfa54cSAmit Daniel Kachhap 401b8d582b9SAmit Daniel Kachhap temp_code = readb(data->base + reg->tmu_cur_temp); 40259dfa54cSAmit Daniel Kachhap 403ddb31d43SBartlomiej Zolnierkiewicz if (data->soc == SOC_ARCH_EXYNOS4210) 404ddb31d43SBartlomiej Zolnierkiewicz /* temp_code should range between 75 and 175 */ 405ddb31d43SBartlomiej Zolnierkiewicz if (temp_code < 75 || temp_code > 175) { 406ddb31d43SBartlomiej Zolnierkiewicz temp = -ENODATA; 407ddb31d43SBartlomiej Zolnierkiewicz goto out; 408ddb31d43SBartlomiej Zolnierkiewicz } 409ddb31d43SBartlomiej Zolnierkiewicz 410ddb31d43SBartlomiej Zolnierkiewicz temp = code_to_temp(data, temp_code); 411ddb31d43SBartlomiej Zolnierkiewicz out: 41259dfa54cSAmit Daniel Kachhap clk_disable(data->clk); 41359dfa54cSAmit Daniel Kachhap mutex_unlock(&data->lock); 41459dfa54cSAmit Daniel Kachhap 41559dfa54cSAmit Daniel Kachhap return temp; 41659dfa54cSAmit Daniel Kachhap } 41759dfa54cSAmit Daniel Kachhap 41859dfa54cSAmit Daniel Kachhap #ifdef CONFIG_THERMAL_EMULATION 41959dfa54cSAmit Daniel Kachhap static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp) 42059dfa54cSAmit Daniel Kachhap { 42159dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = drv_data; 422b8d582b9SAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata = data->pdata; 423b8d582b9SAmit Daniel Kachhap const struct exynos_tmu_registers *reg = pdata->registers; 424b8d582b9SAmit Daniel Kachhap unsigned int val; 42559dfa54cSAmit Daniel Kachhap int ret = -EINVAL; 42659dfa54cSAmit Daniel Kachhap 427f4dae753SAmit Daniel Kachhap if (!TMU_SUPPORTS(pdata, EMULATION)) 42859dfa54cSAmit Daniel Kachhap goto out; 42959dfa54cSAmit Daniel Kachhap 43059dfa54cSAmit Daniel Kachhap if (temp && temp < MCELSIUS) 43159dfa54cSAmit Daniel Kachhap goto out; 43259dfa54cSAmit Daniel Kachhap 43359dfa54cSAmit Daniel Kachhap mutex_lock(&data->lock); 43459dfa54cSAmit Daniel Kachhap clk_enable(data->clk); 43559dfa54cSAmit Daniel Kachhap 436b8d582b9SAmit Daniel Kachhap val = readl(data->base + reg->emul_con); 43759dfa54cSAmit Daniel Kachhap 43859dfa54cSAmit Daniel Kachhap if (temp) { 43959dfa54cSAmit Daniel Kachhap temp /= MCELSIUS; 44059dfa54cSAmit Daniel Kachhap 441f4dae753SAmit Daniel Kachhap if (TMU_SUPPORTS(pdata, EMUL_TIME)) { 4426070c2caSBartlomiej Zolnierkiewicz val &= ~(EXYNOS_EMUL_TIME_MASK << EXYNOS_EMUL_TIME_SHIFT); 4436070c2caSBartlomiej Zolnierkiewicz val |= (EXYNOS_EMUL_TIME << EXYNOS_EMUL_TIME_SHIFT); 444f4dae753SAmit Daniel Kachhap } 4459e288d64SBartlomiej Zolnierkiewicz val &= ~(EXYNOS_EMUL_DATA_MASK << EXYNOS_EMUL_DATA_SHIFT); 4469e288d64SBartlomiej Zolnierkiewicz val |= (temp_to_code(data, temp) << EXYNOS_EMUL_DATA_SHIFT) | 447f4dae753SAmit Daniel Kachhap EXYNOS_EMUL_ENABLE; 44859dfa54cSAmit Daniel Kachhap } else { 449b8d582b9SAmit Daniel Kachhap val &= ~EXYNOS_EMUL_ENABLE; 45059dfa54cSAmit Daniel Kachhap } 45159dfa54cSAmit Daniel Kachhap 452b8d582b9SAmit Daniel Kachhap writel(val, data->base + reg->emul_con); 45359dfa54cSAmit Daniel Kachhap 45459dfa54cSAmit Daniel Kachhap clk_disable(data->clk); 45559dfa54cSAmit Daniel Kachhap mutex_unlock(&data->lock); 45659dfa54cSAmit Daniel Kachhap return 0; 45759dfa54cSAmit Daniel Kachhap out: 45859dfa54cSAmit Daniel Kachhap return ret; 45959dfa54cSAmit Daniel Kachhap } 46059dfa54cSAmit Daniel Kachhap #else 46159dfa54cSAmit Daniel Kachhap static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp) 46259dfa54cSAmit Daniel Kachhap { return -EINVAL; } 46359dfa54cSAmit Daniel Kachhap #endif/*CONFIG_THERMAL_EMULATION*/ 46459dfa54cSAmit Daniel Kachhap 46559dfa54cSAmit Daniel Kachhap static void exynos_tmu_work(struct work_struct *work) 46659dfa54cSAmit Daniel Kachhap { 46759dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = container_of(work, 46859dfa54cSAmit Daniel Kachhap struct exynos_tmu_data, irq_work); 469b835ced1SBartlomiej Zolnierkiewicz unsigned int val_type; 470a0395eeeSAmit Daniel Kachhap 47114a11dc7SNaveen Krishna Chatradhi if (!IS_ERR(data->clk_sec)) 47214a11dc7SNaveen Krishna Chatradhi clk_enable(data->clk_sec); 473a0395eeeSAmit Daniel Kachhap /* Find which sensor generated this interrupt */ 474421d5d12SBartlomiej Zolnierkiewicz if (data->soc == SOC_ARCH_EXYNOS5440) { 475421d5d12SBartlomiej Zolnierkiewicz val_type = readl(data->base_second + EXYNOS5440_TMU_IRQ_STATUS); 476a0395eeeSAmit Daniel Kachhap if (!((val_type >> data->id) & 0x1)) 477a0395eeeSAmit Daniel Kachhap goto out; 478a0395eeeSAmit Daniel Kachhap } 47914a11dc7SNaveen Krishna Chatradhi if (!IS_ERR(data->clk_sec)) 48014a11dc7SNaveen Krishna Chatradhi clk_disable(data->clk_sec); 48159dfa54cSAmit Daniel Kachhap 482cebe7373SAmit Daniel Kachhap exynos_report_trigger(data->reg_conf); 48359dfa54cSAmit Daniel Kachhap mutex_lock(&data->lock); 48459dfa54cSAmit Daniel Kachhap clk_enable(data->clk); 485b8d582b9SAmit Daniel Kachhap 486a4463c4fSAmit Daniel Kachhap /* TODO: take action based on particular interrupt */ 487b835ced1SBartlomiej Zolnierkiewicz exynos_tmu_clear_irqs(data); 488b8d582b9SAmit Daniel Kachhap 48959dfa54cSAmit Daniel Kachhap clk_disable(data->clk); 49059dfa54cSAmit Daniel Kachhap mutex_unlock(&data->lock); 491a0395eeeSAmit Daniel Kachhap out: 49259dfa54cSAmit Daniel Kachhap enable_irq(data->irq); 49359dfa54cSAmit Daniel Kachhap } 49459dfa54cSAmit Daniel Kachhap 49559dfa54cSAmit Daniel Kachhap static irqreturn_t exynos_tmu_irq(int irq, void *id) 49659dfa54cSAmit Daniel Kachhap { 49759dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = id; 49859dfa54cSAmit Daniel Kachhap 49959dfa54cSAmit Daniel Kachhap disable_irq_nosync(irq); 50059dfa54cSAmit Daniel Kachhap schedule_work(&data->irq_work); 50159dfa54cSAmit Daniel Kachhap 50259dfa54cSAmit Daniel Kachhap return IRQ_HANDLED; 50359dfa54cSAmit Daniel Kachhap } 50459dfa54cSAmit Daniel Kachhap 50559dfa54cSAmit Daniel Kachhap static const struct of_device_id exynos_tmu_match[] = { 50659dfa54cSAmit Daniel Kachhap { 5071fe56dc1SChanwoo Choi .compatible = "samsung,exynos3250-tmu", 5081fe56dc1SChanwoo Choi .data = (void *)EXYNOS3250_TMU_DRV_DATA, 5091fe56dc1SChanwoo Choi }, 5101fe56dc1SChanwoo Choi { 51159dfa54cSAmit Daniel Kachhap .compatible = "samsung,exynos4210-tmu", 51259dfa54cSAmit Daniel Kachhap .data = (void *)EXYNOS4210_TMU_DRV_DATA, 51359dfa54cSAmit Daniel Kachhap }, 51459dfa54cSAmit Daniel Kachhap { 51559dfa54cSAmit Daniel Kachhap .compatible = "samsung,exynos4412-tmu", 51614ddfaecSLukasz Majewski .data = (void *)EXYNOS4412_TMU_DRV_DATA, 51759dfa54cSAmit Daniel Kachhap }, 51859dfa54cSAmit Daniel Kachhap { 51959dfa54cSAmit Daniel Kachhap .compatible = "samsung,exynos5250-tmu", 520e6b7991eSAmit Daniel Kachhap .data = (void *)EXYNOS5250_TMU_DRV_DATA, 52159dfa54cSAmit Daniel Kachhap }, 52290542546SAmit Daniel Kachhap { 523923488a5SNaveen Krishna Chatradhi .compatible = "samsung,exynos5260-tmu", 524923488a5SNaveen Krishna Chatradhi .data = (void *)EXYNOS5260_TMU_DRV_DATA, 525923488a5SNaveen Krishna Chatradhi }, 526923488a5SNaveen Krishna Chatradhi { 52714a11dc7SNaveen Krishna Chatradhi .compatible = "samsung,exynos5420-tmu", 52814a11dc7SNaveen Krishna Chatradhi .data = (void *)EXYNOS5420_TMU_DRV_DATA, 52914a11dc7SNaveen Krishna Chatradhi }, 53014a11dc7SNaveen Krishna Chatradhi { 53114a11dc7SNaveen Krishna Chatradhi .compatible = "samsung,exynos5420-tmu-ext-triminfo", 53214a11dc7SNaveen Krishna Chatradhi .data = (void *)EXYNOS5420_TMU_DRV_DATA, 53314a11dc7SNaveen Krishna Chatradhi }, 53414a11dc7SNaveen Krishna Chatradhi { 53590542546SAmit Daniel Kachhap .compatible = "samsung,exynos5440-tmu", 53690542546SAmit Daniel Kachhap .data = (void *)EXYNOS5440_TMU_DRV_DATA, 53790542546SAmit Daniel Kachhap }, 53859dfa54cSAmit Daniel Kachhap {}, 53959dfa54cSAmit Daniel Kachhap }; 54059dfa54cSAmit Daniel Kachhap MODULE_DEVICE_TABLE(of, exynos_tmu_match); 54159dfa54cSAmit Daniel Kachhap 54259dfa54cSAmit Daniel Kachhap static inline struct exynos_tmu_platform_data *exynos_get_driver_data( 543cebe7373SAmit Daniel Kachhap struct platform_device *pdev, int id) 54459dfa54cSAmit Daniel Kachhap { 545cebe7373SAmit Daniel Kachhap struct exynos_tmu_init_data *data_table; 546cebe7373SAmit Daniel Kachhap struct exynos_tmu_platform_data *tmu_data; 54759dfa54cSAmit Daniel Kachhap const struct of_device_id *match; 54873b5b1d7SSachin Kamat 54959dfa54cSAmit Daniel Kachhap match = of_match_node(exynos_tmu_match, pdev->dev.of_node); 55059dfa54cSAmit Daniel Kachhap if (!match) 55159dfa54cSAmit Daniel Kachhap return NULL; 552cebe7373SAmit Daniel Kachhap data_table = (struct exynos_tmu_init_data *) match->data; 553cebe7373SAmit Daniel Kachhap if (!data_table || id >= data_table->tmu_count) 554cebe7373SAmit Daniel Kachhap return NULL; 555cebe7373SAmit Daniel Kachhap tmu_data = data_table->tmu_data; 556cebe7373SAmit Daniel Kachhap return (struct exynos_tmu_platform_data *) (tmu_data + id); 55759dfa54cSAmit Daniel Kachhap } 55859dfa54cSAmit Daniel Kachhap 559cebe7373SAmit Daniel Kachhap static int exynos_map_dt_data(struct platform_device *pdev) 56059dfa54cSAmit Daniel Kachhap { 561cebe7373SAmit Daniel Kachhap struct exynos_tmu_data *data = platform_get_drvdata(pdev); 562cebe7373SAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata; 563cebe7373SAmit Daniel Kachhap struct resource res; 564498d22f6SAmit Daniel Kachhap int ret; 56559dfa54cSAmit Daniel Kachhap 56673b5b1d7SSachin Kamat if (!data || !pdev->dev.of_node) 567cebe7373SAmit Daniel Kachhap return -ENODEV; 56859dfa54cSAmit Daniel Kachhap 569498d22f6SAmit Daniel Kachhap /* 570498d22f6SAmit Daniel Kachhap * Try enabling the regulator if found 571498d22f6SAmit Daniel Kachhap * TODO: Add regulator as an SOC feature, so that regulator enable 572498d22f6SAmit Daniel Kachhap * is a compulsory call. 573498d22f6SAmit Daniel Kachhap */ 574498d22f6SAmit Daniel Kachhap data->regulator = devm_regulator_get(&pdev->dev, "vtmu"); 575498d22f6SAmit Daniel Kachhap if (!IS_ERR(data->regulator)) { 576498d22f6SAmit Daniel Kachhap ret = regulator_enable(data->regulator); 577498d22f6SAmit Daniel Kachhap if (ret) { 578498d22f6SAmit Daniel Kachhap dev_err(&pdev->dev, "failed to enable vtmu\n"); 579498d22f6SAmit Daniel Kachhap return ret; 580498d22f6SAmit Daniel Kachhap } 581498d22f6SAmit Daniel Kachhap } else { 582498d22f6SAmit Daniel Kachhap dev_info(&pdev->dev, "Regulator node (vtmu) not found\n"); 583498d22f6SAmit Daniel Kachhap } 584498d22f6SAmit Daniel Kachhap 585cebe7373SAmit Daniel Kachhap data->id = of_alias_get_id(pdev->dev.of_node, "tmuctrl"); 586cebe7373SAmit Daniel Kachhap if (data->id < 0) 587cebe7373SAmit Daniel Kachhap data->id = 0; 588cebe7373SAmit Daniel Kachhap 589cebe7373SAmit Daniel Kachhap data->irq = irq_of_parse_and_map(pdev->dev.of_node, 0); 590cebe7373SAmit Daniel Kachhap if (data->irq <= 0) { 591cebe7373SAmit Daniel Kachhap dev_err(&pdev->dev, "failed to get IRQ\n"); 592cebe7373SAmit Daniel Kachhap return -ENODEV; 593cebe7373SAmit Daniel Kachhap } 594cebe7373SAmit Daniel Kachhap 595cebe7373SAmit Daniel Kachhap if (of_address_to_resource(pdev->dev.of_node, 0, &res)) { 596cebe7373SAmit Daniel Kachhap dev_err(&pdev->dev, "failed to get Resource 0\n"); 597cebe7373SAmit Daniel Kachhap return -ENODEV; 598cebe7373SAmit Daniel Kachhap } 599cebe7373SAmit Daniel Kachhap 600cebe7373SAmit Daniel Kachhap data->base = devm_ioremap(&pdev->dev, res.start, resource_size(&res)); 601cebe7373SAmit Daniel Kachhap if (!data->base) { 602cebe7373SAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to ioremap memory\n"); 603cebe7373SAmit Daniel Kachhap return -EADDRNOTAVAIL; 604cebe7373SAmit Daniel Kachhap } 605cebe7373SAmit Daniel Kachhap 606cebe7373SAmit Daniel Kachhap pdata = exynos_get_driver_data(pdev, data->id); 60759dfa54cSAmit Daniel Kachhap if (!pdata) { 60859dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "No platform init data supplied.\n"); 60959dfa54cSAmit Daniel Kachhap return -ENODEV; 61059dfa54cSAmit Daniel Kachhap } 611cebe7373SAmit Daniel Kachhap data->pdata = pdata; 612d9b6ee14SAmit Daniel Kachhap /* 613d9b6ee14SAmit Daniel Kachhap * Check if the TMU shares some registers and then try to map the 614d9b6ee14SAmit Daniel Kachhap * memory of common registers. 615d9b6ee14SAmit Daniel Kachhap */ 6169025d563SNaveen Krishna Chatradhi if (!TMU_SUPPORTS(pdata, ADDRESS_MULTIPLE)) 617d9b6ee14SAmit Daniel Kachhap return 0; 618d9b6ee14SAmit Daniel Kachhap 619d9b6ee14SAmit Daniel Kachhap if (of_address_to_resource(pdev->dev.of_node, 1, &res)) { 620d9b6ee14SAmit Daniel Kachhap dev_err(&pdev->dev, "failed to get Resource 1\n"); 621d9b6ee14SAmit Daniel Kachhap return -ENODEV; 622d9b6ee14SAmit Daniel Kachhap } 623d9b6ee14SAmit Daniel Kachhap 6249025d563SNaveen Krishna Chatradhi data->base_second = devm_ioremap(&pdev->dev, res.start, 625d9b6ee14SAmit Daniel Kachhap resource_size(&res)); 6269025d563SNaveen Krishna Chatradhi if (!data->base_second) { 627d9b6ee14SAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to ioremap memory\n"); 628d9b6ee14SAmit Daniel Kachhap return -ENOMEM; 629d9b6ee14SAmit Daniel Kachhap } 630cebe7373SAmit Daniel Kachhap 631cebe7373SAmit Daniel Kachhap return 0; 632cebe7373SAmit Daniel Kachhap } 633cebe7373SAmit Daniel Kachhap 634cebe7373SAmit Daniel Kachhap static int exynos_tmu_probe(struct platform_device *pdev) 635cebe7373SAmit Daniel Kachhap { 636cebe7373SAmit Daniel Kachhap struct exynos_tmu_data *data; 637cebe7373SAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata; 638cebe7373SAmit Daniel Kachhap struct thermal_sensor_conf *sensor_conf; 639cebe7373SAmit Daniel Kachhap int ret, i; 640cebe7373SAmit Daniel Kachhap 64159dfa54cSAmit Daniel Kachhap data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data), 64259dfa54cSAmit Daniel Kachhap GFP_KERNEL); 6432a9675b3SJingoo Han if (!data) 64459dfa54cSAmit Daniel Kachhap return -ENOMEM; 64559dfa54cSAmit Daniel Kachhap 646cebe7373SAmit Daniel Kachhap platform_set_drvdata(pdev, data); 647cebe7373SAmit Daniel Kachhap mutex_init(&data->lock); 648cebe7373SAmit Daniel Kachhap 649cebe7373SAmit Daniel Kachhap ret = exynos_map_dt_data(pdev); 650cebe7373SAmit Daniel Kachhap if (ret) 651cebe7373SAmit Daniel Kachhap return ret; 652cebe7373SAmit Daniel Kachhap 653cebe7373SAmit Daniel Kachhap pdata = data->pdata; 65459dfa54cSAmit Daniel Kachhap 65559dfa54cSAmit Daniel Kachhap INIT_WORK(&data->irq_work, exynos_tmu_work); 65659dfa54cSAmit Daniel Kachhap 65759dfa54cSAmit Daniel Kachhap data->clk = devm_clk_get(&pdev->dev, "tmu_apbif"); 65859dfa54cSAmit Daniel Kachhap if (IS_ERR(data->clk)) { 65959dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to get clock\n"); 66059dfa54cSAmit Daniel Kachhap return PTR_ERR(data->clk); 66159dfa54cSAmit Daniel Kachhap } 66259dfa54cSAmit Daniel Kachhap 66314a11dc7SNaveen Krishna Chatradhi data->clk_sec = devm_clk_get(&pdev->dev, "tmu_triminfo_apbif"); 66414a11dc7SNaveen Krishna Chatradhi if (IS_ERR(data->clk_sec)) { 66514a11dc7SNaveen Krishna Chatradhi if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO) { 66614a11dc7SNaveen Krishna Chatradhi dev_err(&pdev->dev, "Failed to get triminfo clock\n"); 66714a11dc7SNaveen Krishna Chatradhi return PTR_ERR(data->clk_sec); 66814a11dc7SNaveen Krishna Chatradhi } 66914a11dc7SNaveen Krishna Chatradhi } else { 67014a11dc7SNaveen Krishna Chatradhi ret = clk_prepare(data->clk_sec); 67114a11dc7SNaveen Krishna Chatradhi if (ret) { 67214a11dc7SNaveen Krishna Chatradhi dev_err(&pdev->dev, "Failed to get clock\n"); 67359dfa54cSAmit Daniel Kachhap return ret; 67414a11dc7SNaveen Krishna Chatradhi } 67514a11dc7SNaveen Krishna Chatradhi } 67614a11dc7SNaveen Krishna Chatradhi 67714a11dc7SNaveen Krishna Chatradhi ret = clk_prepare(data->clk); 67814a11dc7SNaveen Krishna Chatradhi if (ret) { 67914a11dc7SNaveen Krishna Chatradhi dev_err(&pdev->dev, "Failed to get clock\n"); 68014a11dc7SNaveen Krishna Chatradhi goto err_clk_sec; 68114a11dc7SNaveen Krishna Chatradhi } 68259dfa54cSAmit Daniel Kachhap 68359dfa54cSAmit Daniel Kachhap data->soc = pdata->type; 68472d1100bSBartlomiej Zolnierkiewicz 68572d1100bSBartlomiej Zolnierkiewicz switch (data->soc) { 68672d1100bSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS4210: 68772d1100bSBartlomiej Zolnierkiewicz data->tmu_initialize = exynos4210_tmu_initialize; 68872d1100bSBartlomiej Zolnierkiewicz break; 68972d1100bSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS3250: 69072d1100bSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS4412: 69172d1100bSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS5250: 69272d1100bSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS5260: 69372d1100bSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS5420: 69472d1100bSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS5420_TRIMINFO: 69572d1100bSBartlomiej Zolnierkiewicz data->tmu_initialize = exynos4412_tmu_initialize; 69672d1100bSBartlomiej Zolnierkiewicz break; 69772d1100bSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS5440: 69872d1100bSBartlomiej Zolnierkiewicz data->tmu_initialize = exynos5440_tmu_initialize; 69972d1100bSBartlomiej Zolnierkiewicz break; 70072d1100bSBartlomiej Zolnierkiewicz default: 70159dfa54cSAmit Daniel Kachhap ret = -EINVAL; 70259dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "Platform not supported\n"); 70359dfa54cSAmit Daniel Kachhap goto err_clk; 70459dfa54cSAmit Daniel Kachhap } 70559dfa54cSAmit Daniel Kachhap 70659dfa54cSAmit Daniel Kachhap ret = exynos_tmu_initialize(pdev); 70759dfa54cSAmit Daniel Kachhap if (ret) { 70859dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to initialize TMU\n"); 70959dfa54cSAmit Daniel Kachhap goto err_clk; 71059dfa54cSAmit Daniel Kachhap } 71159dfa54cSAmit Daniel Kachhap 71259dfa54cSAmit Daniel Kachhap exynos_tmu_control(pdev, true); 71359dfa54cSAmit Daniel Kachhap 714cebe7373SAmit Daniel Kachhap /* Allocate a structure to register with the exynos core thermal */ 715cebe7373SAmit Daniel Kachhap sensor_conf = devm_kzalloc(&pdev->dev, 716cebe7373SAmit Daniel Kachhap sizeof(struct thermal_sensor_conf), GFP_KERNEL); 717cebe7373SAmit Daniel Kachhap if (!sensor_conf) { 718cebe7373SAmit Daniel Kachhap ret = -ENOMEM; 719cebe7373SAmit Daniel Kachhap goto err_clk; 720cebe7373SAmit Daniel Kachhap } 721cebe7373SAmit Daniel Kachhap sprintf(sensor_conf->name, "therm_zone%d", data->id); 722cebe7373SAmit Daniel Kachhap sensor_conf->read_temperature = (int (*)(void *))exynos_tmu_read; 723cebe7373SAmit Daniel Kachhap sensor_conf->write_emul_temp = 724cebe7373SAmit Daniel Kachhap (int (*)(void *, unsigned long))exynos_tmu_set_emulation; 725cebe7373SAmit Daniel Kachhap sensor_conf->driver_data = data; 726cebe7373SAmit Daniel Kachhap sensor_conf->trip_data.trip_count = pdata->trigger_enable[0] + 727bb34b4c8SAmit Daniel Kachhap pdata->trigger_enable[1] + pdata->trigger_enable[2]+ 728bb34b4c8SAmit Daniel Kachhap pdata->trigger_enable[3]; 72959dfa54cSAmit Daniel Kachhap 730cebe7373SAmit Daniel Kachhap for (i = 0; i < sensor_conf->trip_data.trip_count; i++) { 731cebe7373SAmit Daniel Kachhap sensor_conf->trip_data.trip_val[i] = 73259dfa54cSAmit Daniel Kachhap pdata->threshold + pdata->trigger_levels[i]; 733cebe7373SAmit Daniel Kachhap sensor_conf->trip_data.trip_type[i] = 7345c3cf552SAmit Daniel Kachhap pdata->trigger_type[i]; 7355c3cf552SAmit Daniel Kachhap } 73659dfa54cSAmit Daniel Kachhap 737cebe7373SAmit Daniel Kachhap sensor_conf->trip_data.trigger_falling = pdata->threshold_falling; 73859dfa54cSAmit Daniel Kachhap 739cebe7373SAmit Daniel Kachhap sensor_conf->cooling_data.freq_clip_count = pdata->freq_tab_count; 74059dfa54cSAmit Daniel Kachhap for (i = 0; i < pdata->freq_tab_count; i++) { 741cebe7373SAmit Daniel Kachhap sensor_conf->cooling_data.freq_data[i].freq_clip_max = 74259dfa54cSAmit Daniel Kachhap pdata->freq_tab[i].freq_clip_max; 743cebe7373SAmit Daniel Kachhap sensor_conf->cooling_data.freq_data[i].temp_level = 74459dfa54cSAmit Daniel Kachhap pdata->freq_tab[i].temp_level; 74559dfa54cSAmit Daniel Kachhap } 746cebe7373SAmit Daniel Kachhap sensor_conf->dev = &pdev->dev; 747cebe7373SAmit Daniel Kachhap /* Register the sensor with thermal management interface */ 748cebe7373SAmit Daniel Kachhap ret = exynos_register_thermal(sensor_conf); 74959dfa54cSAmit Daniel Kachhap if (ret) { 75059dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to register thermal interface\n"); 75159dfa54cSAmit Daniel Kachhap goto err_clk; 75259dfa54cSAmit Daniel Kachhap } 753cebe7373SAmit Daniel Kachhap data->reg_conf = sensor_conf; 754cebe7373SAmit Daniel Kachhap 755cebe7373SAmit Daniel Kachhap ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq, 756cebe7373SAmit Daniel Kachhap IRQF_TRIGGER_RISING | IRQF_SHARED, dev_name(&pdev->dev), data); 757cebe7373SAmit Daniel Kachhap if (ret) { 758cebe7373SAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq); 759cebe7373SAmit Daniel Kachhap goto err_clk; 760cebe7373SAmit Daniel Kachhap } 76159dfa54cSAmit Daniel Kachhap 76259dfa54cSAmit Daniel Kachhap return 0; 76359dfa54cSAmit Daniel Kachhap err_clk: 76459dfa54cSAmit Daniel Kachhap clk_unprepare(data->clk); 76514a11dc7SNaveen Krishna Chatradhi err_clk_sec: 76614a11dc7SNaveen Krishna Chatradhi if (!IS_ERR(data->clk_sec)) 76714a11dc7SNaveen Krishna Chatradhi clk_unprepare(data->clk_sec); 76859dfa54cSAmit Daniel Kachhap return ret; 76959dfa54cSAmit Daniel Kachhap } 77059dfa54cSAmit Daniel Kachhap 77159dfa54cSAmit Daniel Kachhap static int exynos_tmu_remove(struct platform_device *pdev) 77259dfa54cSAmit Daniel Kachhap { 77359dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = platform_get_drvdata(pdev); 77459dfa54cSAmit Daniel Kachhap 775cebe7373SAmit Daniel Kachhap exynos_unregister_thermal(data->reg_conf); 77659dfa54cSAmit Daniel Kachhap 7774215688eSBartlomiej Zolnierkiewicz exynos_tmu_control(pdev, false); 7784215688eSBartlomiej Zolnierkiewicz 77959dfa54cSAmit Daniel Kachhap clk_unprepare(data->clk); 78014a11dc7SNaveen Krishna Chatradhi if (!IS_ERR(data->clk_sec)) 78114a11dc7SNaveen Krishna Chatradhi clk_unprepare(data->clk_sec); 78259dfa54cSAmit Daniel Kachhap 783498d22f6SAmit Daniel Kachhap if (!IS_ERR(data->regulator)) 784498d22f6SAmit Daniel Kachhap regulator_disable(data->regulator); 785498d22f6SAmit Daniel Kachhap 78659dfa54cSAmit Daniel Kachhap return 0; 78759dfa54cSAmit Daniel Kachhap } 78859dfa54cSAmit Daniel Kachhap 78959dfa54cSAmit Daniel Kachhap #ifdef CONFIG_PM_SLEEP 79059dfa54cSAmit Daniel Kachhap static int exynos_tmu_suspend(struct device *dev) 79159dfa54cSAmit Daniel Kachhap { 79259dfa54cSAmit Daniel Kachhap exynos_tmu_control(to_platform_device(dev), false); 79359dfa54cSAmit Daniel Kachhap 79459dfa54cSAmit Daniel Kachhap return 0; 79559dfa54cSAmit Daniel Kachhap } 79659dfa54cSAmit Daniel Kachhap 79759dfa54cSAmit Daniel Kachhap static int exynos_tmu_resume(struct device *dev) 79859dfa54cSAmit Daniel Kachhap { 79959dfa54cSAmit Daniel Kachhap struct platform_device *pdev = to_platform_device(dev); 80059dfa54cSAmit Daniel Kachhap 80159dfa54cSAmit Daniel Kachhap exynos_tmu_initialize(pdev); 80259dfa54cSAmit Daniel Kachhap exynos_tmu_control(pdev, true); 80359dfa54cSAmit Daniel Kachhap 80459dfa54cSAmit Daniel Kachhap return 0; 80559dfa54cSAmit Daniel Kachhap } 80659dfa54cSAmit Daniel Kachhap 80759dfa54cSAmit Daniel Kachhap static SIMPLE_DEV_PM_OPS(exynos_tmu_pm, 80859dfa54cSAmit Daniel Kachhap exynos_tmu_suspend, exynos_tmu_resume); 80959dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM (&exynos_tmu_pm) 81059dfa54cSAmit Daniel Kachhap #else 81159dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM NULL 81259dfa54cSAmit Daniel Kachhap #endif 81359dfa54cSAmit Daniel Kachhap 81459dfa54cSAmit Daniel Kachhap static struct platform_driver exynos_tmu_driver = { 81559dfa54cSAmit Daniel Kachhap .driver = { 81659dfa54cSAmit Daniel Kachhap .name = "exynos-tmu", 81759dfa54cSAmit Daniel Kachhap .owner = THIS_MODULE, 81859dfa54cSAmit Daniel Kachhap .pm = EXYNOS_TMU_PM, 81973b5b1d7SSachin Kamat .of_match_table = exynos_tmu_match, 82059dfa54cSAmit Daniel Kachhap }, 82159dfa54cSAmit Daniel Kachhap .probe = exynos_tmu_probe, 82259dfa54cSAmit Daniel Kachhap .remove = exynos_tmu_remove, 82359dfa54cSAmit Daniel Kachhap }; 82459dfa54cSAmit Daniel Kachhap 82559dfa54cSAmit Daniel Kachhap module_platform_driver(exynos_tmu_driver); 82659dfa54cSAmit Daniel Kachhap 82759dfa54cSAmit Daniel Kachhap MODULE_DESCRIPTION("EXYNOS TMU Driver"); 82859dfa54cSAmit Daniel Kachhap MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>"); 82959dfa54cSAmit Daniel Kachhap MODULE_LICENSE("GPL"); 83059dfa54cSAmit Daniel Kachhap MODULE_ALIAS("platform:exynos-tmu"); 831