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. 44d9b6ee14SAmit Daniel Kachhap * @base_common: 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. 50cebe7373SAmit Daniel Kachhap * @temp_error1: fused value of the first point trim. 51cebe7373SAmit Daniel Kachhap * @temp_error2: fused value of the second point trim. 52498d22f6SAmit Daniel Kachhap * @regulator: pointer to the TMU regulator structure. 53cebe7373SAmit Daniel Kachhap * @reg_conf: pointer to structure to register with core thermal. 54cebe7373SAmit Daniel Kachhap */ 5559dfa54cSAmit Daniel Kachhap struct exynos_tmu_data { 56cebe7373SAmit Daniel Kachhap int id; 5759dfa54cSAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata; 5859dfa54cSAmit Daniel Kachhap void __iomem *base; 59d9b6ee14SAmit Daniel Kachhap void __iomem *base_common; 6059dfa54cSAmit Daniel Kachhap int irq; 6159dfa54cSAmit Daniel Kachhap enum soc_type soc; 6259dfa54cSAmit Daniel Kachhap struct work_struct irq_work; 6359dfa54cSAmit Daniel Kachhap struct mutex lock; 6459dfa54cSAmit Daniel Kachhap struct clk *clk; 6559dfa54cSAmit Daniel Kachhap u8 temp_error1, temp_error2; 66498d22f6SAmit Daniel Kachhap struct regulator *regulator; 67cebe7373SAmit Daniel Kachhap struct thermal_sensor_conf *reg_conf; 6859dfa54cSAmit Daniel Kachhap }; 6959dfa54cSAmit Daniel Kachhap 7059dfa54cSAmit Daniel Kachhap /* 7159dfa54cSAmit Daniel Kachhap * TMU treats temperature as a mapped temperature code. 7259dfa54cSAmit Daniel Kachhap * The temperature is converted differently depending on the calibration type. 7359dfa54cSAmit Daniel Kachhap */ 7459dfa54cSAmit Daniel Kachhap static int temp_to_code(struct exynos_tmu_data *data, u8 temp) 7559dfa54cSAmit Daniel Kachhap { 7659dfa54cSAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata = data->pdata; 7759dfa54cSAmit Daniel Kachhap int temp_code; 7859dfa54cSAmit Daniel Kachhap 791928457eSAmit Daniel Kachhap if (pdata->cal_mode == HW_MODE) 801928457eSAmit Daniel Kachhap return temp; 811928457eSAmit Daniel Kachhap 8259dfa54cSAmit Daniel Kachhap if (data->soc == SOC_ARCH_EXYNOS4210) 8359dfa54cSAmit Daniel Kachhap /* temp should range between 25 and 125 */ 8459dfa54cSAmit Daniel Kachhap if (temp < 25 || temp > 125) { 8559dfa54cSAmit Daniel Kachhap temp_code = -EINVAL; 8659dfa54cSAmit Daniel Kachhap goto out; 8759dfa54cSAmit Daniel Kachhap } 8859dfa54cSAmit Daniel Kachhap 8959dfa54cSAmit Daniel Kachhap switch (pdata->cal_type) { 9059dfa54cSAmit Daniel Kachhap case TYPE_TWO_POINT_TRIMMING: 91bb34b4c8SAmit Daniel Kachhap temp_code = (temp - pdata->first_point_trim) * 9259dfa54cSAmit Daniel Kachhap (data->temp_error2 - data->temp_error1) / 93bb34b4c8SAmit Daniel Kachhap (pdata->second_point_trim - pdata->first_point_trim) + 94bb34b4c8SAmit Daniel Kachhap data->temp_error1; 9559dfa54cSAmit Daniel Kachhap break; 9659dfa54cSAmit Daniel Kachhap case TYPE_ONE_POINT_TRIMMING: 97bb34b4c8SAmit Daniel Kachhap temp_code = temp + data->temp_error1 - pdata->first_point_trim; 9859dfa54cSAmit Daniel Kachhap break; 9959dfa54cSAmit Daniel Kachhap default: 100bb34b4c8SAmit Daniel Kachhap temp_code = temp + pdata->default_temp_offset; 10159dfa54cSAmit Daniel Kachhap break; 10259dfa54cSAmit Daniel Kachhap } 10359dfa54cSAmit Daniel Kachhap out: 10459dfa54cSAmit Daniel Kachhap return temp_code; 10559dfa54cSAmit Daniel Kachhap } 10659dfa54cSAmit Daniel Kachhap 10759dfa54cSAmit Daniel Kachhap /* 10859dfa54cSAmit Daniel Kachhap * Calculate a temperature value from a temperature code. 10959dfa54cSAmit Daniel Kachhap * The unit of the temperature is degree Celsius. 11059dfa54cSAmit Daniel Kachhap */ 11159dfa54cSAmit Daniel Kachhap static int code_to_temp(struct exynos_tmu_data *data, u8 temp_code) 11259dfa54cSAmit Daniel Kachhap { 11359dfa54cSAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata = data->pdata; 11459dfa54cSAmit Daniel Kachhap int temp; 11559dfa54cSAmit Daniel Kachhap 1161928457eSAmit Daniel Kachhap if (pdata->cal_mode == HW_MODE) 1171928457eSAmit Daniel Kachhap return temp_code; 1181928457eSAmit Daniel Kachhap 11959dfa54cSAmit Daniel Kachhap if (data->soc == SOC_ARCH_EXYNOS4210) 12059dfa54cSAmit Daniel Kachhap /* temp_code should range between 75 and 175 */ 12159dfa54cSAmit Daniel Kachhap if (temp_code < 75 || temp_code > 175) { 12259dfa54cSAmit Daniel Kachhap temp = -ENODATA; 12359dfa54cSAmit Daniel Kachhap goto out; 12459dfa54cSAmit Daniel Kachhap } 12559dfa54cSAmit Daniel Kachhap 12659dfa54cSAmit Daniel Kachhap switch (pdata->cal_type) { 12759dfa54cSAmit Daniel Kachhap case TYPE_TWO_POINT_TRIMMING: 128bb34b4c8SAmit Daniel Kachhap temp = (temp_code - data->temp_error1) * 129bb34b4c8SAmit Daniel Kachhap (pdata->second_point_trim - pdata->first_point_trim) / 130bb34b4c8SAmit Daniel Kachhap (data->temp_error2 - data->temp_error1) + 131bb34b4c8SAmit Daniel Kachhap pdata->first_point_trim; 13259dfa54cSAmit Daniel Kachhap break; 13359dfa54cSAmit Daniel Kachhap case TYPE_ONE_POINT_TRIMMING: 134bb34b4c8SAmit Daniel Kachhap temp = temp_code - data->temp_error1 + pdata->first_point_trim; 13559dfa54cSAmit Daniel Kachhap break; 13659dfa54cSAmit Daniel Kachhap default: 137bb34b4c8SAmit Daniel Kachhap temp = temp_code - pdata->default_temp_offset; 13859dfa54cSAmit Daniel Kachhap break; 13959dfa54cSAmit Daniel Kachhap } 14059dfa54cSAmit Daniel Kachhap out: 14159dfa54cSAmit Daniel Kachhap return temp; 14259dfa54cSAmit Daniel Kachhap } 14359dfa54cSAmit Daniel Kachhap 14459dfa54cSAmit Daniel Kachhap static int exynos_tmu_initialize(struct platform_device *pdev) 14559dfa54cSAmit Daniel Kachhap { 14659dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = platform_get_drvdata(pdev); 14759dfa54cSAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata = data->pdata; 148b8d582b9SAmit Daniel Kachhap const struct exynos_tmu_registers *reg = pdata->registers; 1497ca04e58SAmit Daniel Kachhap unsigned int status, trim_info = 0, con; 15059dfa54cSAmit Daniel Kachhap unsigned int rising_threshold = 0, falling_threshold = 0; 15159dfa54cSAmit Daniel Kachhap int ret = 0, threshold_code, i, trigger_levs = 0; 15259dfa54cSAmit Daniel Kachhap 15359dfa54cSAmit Daniel Kachhap mutex_lock(&data->lock); 15459dfa54cSAmit Daniel Kachhap clk_enable(data->clk); 15559dfa54cSAmit Daniel Kachhap 156f4dae753SAmit Daniel Kachhap if (TMU_SUPPORTS(pdata, READY_STATUS)) { 157b8d582b9SAmit Daniel Kachhap status = readb(data->base + reg->tmu_status); 15859dfa54cSAmit Daniel Kachhap if (!status) { 15959dfa54cSAmit Daniel Kachhap ret = -EBUSY; 16059dfa54cSAmit Daniel Kachhap goto out; 16159dfa54cSAmit Daniel Kachhap } 162f4dae753SAmit Daniel Kachhap } 16359dfa54cSAmit Daniel Kachhap 164f4dae753SAmit Daniel Kachhap if (TMU_SUPPORTS(pdata, TRIM_RELOAD)) 165b8d582b9SAmit Daniel Kachhap __raw_writel(1, data->base + reg->triminfo_ctrl); 166b8d582b9SAmit Daniel Kachhap 1671928457eSAmit Daniel Kachhap if (pdata->cal_mode == HW_MODE) 1681928457eSAmit Daniel Kachhap goto skip_calib_data; 1691928457eSAmit Daniel Kachhap 17059dfa54cSAmit Daniel Kachhap /* Save trimming info in order to perform calibration */ 171a0395eeeSAmit Daniel Kachhap if (data->soc == SOC_ARCH_EXYNOS5440) { 172a0395eeeSAmit Daniel Kachhap /* 173a0395eeeSAmit Daniel Kachhap * For exynos5440 soc triminfo value is swapped between TMU0 and 174a0395eeeSAmit Daniel Kachhap * TMU2, so the below logic is needed. 175a0395eeeSAmit Daniel Kachhap */ 176a0395eeeSAmit Daniel Kachhap switch (data->id) { 177a0395eeeSAmit Daniel Kachhap case 0: 178a0395eeeSAmit Daniel Kachhap trim_info = readl(data->base + 179a0395eeeSAmit Daniel Kachhap EXYNOS5440_EFUSE_SWAP_OFFSET + reg->triminfo_data); 180a0395eeeSAmit Daniel Kachhap break; 181a0395eeeSAmit Daniel Kachhap case 1: 182b8d582b9SAmit Daniel Kachhap trim_info = readl(data->base + reg->triminfo_data); 183a0395eeeSAmit Daniel Kachhap break; 184a0395eeeSAmit Daniel Kachhap case 2: 185a0395eeeSAmit Daniel Kachhap trim_info = readl(data->base - 186a0395eeeSAmit Daniel Kachhap EXYNOS5440_EFUSE_SWAP_OFFSET + reg->triminfo_data); 187a0395eeeSAmit Daniel Kachhap } 188a0395eeeSAmit Daniel Kachhap } else { 189a0395eeeSAmit Daniel Kachhap trim_info = readl(data->base + reg->triminfo_data); 190a0395eeeSAmit Daniel Kachhap } 191b8d582b9SAmit Daniel Kachhap data->temp_error1 = trim_info & EXYNOS_TMU_TEMP_MASK; 192b8d582b9SAmit Daniel Kachhap data->temp_error2 = ((trim_info >> reg->triminfo_85_shift) & 193b8d582b9SAmit Daniel Kachhap EXYNOS_TMU_TEMP_MASK); 19459dfa54cSAmit Daniel Kachhap 1955000806cSAmit Daniel Kachhap if (!data->temp_error1 || 1965000806cSAmit Daniel Kachhap (pdata->min_efuse_value > data->temp_error1) || 1975000806cSAmit Daniel Kachhap (data->temp_error1 > pdata->max_efuse_value)) 1985000806cSAmit Daniel Kachhap data->temp_error1 = pdata->efuse_value & EXYNOS_TMU_TEMP_MASK; 1995000806cSAmit Daniel Kachhap 2005000806cSAmit Daniel Kachhap if (!data->temp_error2) 2015000806cSAmit Daniel Kachhap data->temp_error2 = 2025000806cSAmit Daniel Kachhap (pdata->efuse_value >> reg->triminfo_85_shift) & 2035000806cSAmit Daniel Kachhap EXYNOS_TMU_TEMP_MASK; 20459dfa54cSAmit Daniel Kachhap 2051928457eSAmit Daniel Kachhap skip_calib_data: 2067ca04e58SAmit Daniel Kachhap if (pdata->max_trigger_level > MAX_THRESHOLD_LEVS) { 2077ca04e58SAmit Daniel Kachhap dev_err(&pdev->dev, "Invalid max trigger level\n"); 20860acb389SJulia Lawall ret = -EINVAL; 2097ca04e58SAmit Daniel Kachhap goto out; 2107ca04e58SAmit Daniel Kachhap } 2117ca04e58SAmit Daniel Kachhap 2127ca04e58SAmit Daniel Kachhap for (i = 0; i < pdata->max_trigger_level; i++) { 2137ca04e58SAmit Daniel Kachhap if (!pdata->trigger_levels[i]) 2147ca04e58SAmit Daniel Kachhap continue; 2157ca04e58SAmit Daniel Kachhap 2167ca04e58SAmit Daniel Kachhap if ((pdata->trigger_type[i] == HW_TRIP) && 2177ca04e58SAmit Daniel Kachhap (!pdata->trigger_levels[pdata->max_trigger_level - 1])) { 2187ca04e58SAmit Daniel Kachhap dev_err(&pdev->dev, "Invalid hw trigger level\n"); 2197ca04e58SAmit Daniel Kachhap ret = -EINVAL; 2207ca04e58SAmit Daniel Kachhap goto out; 2217ca04e58SAmit Daniel Kachhap } 2227ca04e58SAmit Daniel Kachhap 2237ca04e58SAmit Daniel Kachhap /* Count trigger levels except the HW trip*/ 2247ca04e58SAmit Daniel Kachhap if (!(pdata->trigger_type[i] == HW_TRIP)) 22559dfa54cSAmit Daniel Kachhap trigger_levs++; 2267ca04e58SAmit Daniel Kachhap } 22759dfa54cSAmit Daniel Kachhap 228*c65d3473STushar Behera rising_threshold = readl(data->base + reg->threshold_th0); 229*c65d3473STushar Behera 23059dfa54cSAmit Daniel Kachhap if (data->soc == SOC_ARCH_EXYNOS4210) { 23159dfa54cSAmit Daniel Kachhap /* Write temperature code for threshold */ 23259dfa54cSAmit Daniel Kachhap threshold_code = temp_to_code(data, pdata->threshold); 23359dfa54cSAmit Daniel Kachhap if (threshold_code < 0) { 23459dfa54cSAmit Daniel Kachhap ret = threshold_code; 23559dfa54cSAmit Daniel Kachhap goto out; 23659dfa54cSAmit Daniel Kachhap } 23759dfa54cSAmit Daniel Kachhap writeb(threshold_code, 238b8d582b9SAmit Daniel Kachhap data->base + reg->threshold_temp); 23959dfa54cSAmit Daniel Kachhap for (i = 0; i < trigger_levs; i++) 240b8d582b9SAmit Daniel Kachhap writeb(pdata->trigger_levels[i], data->base + 241b8d582b9SAmit Daniel Kachhap reg->threshold_th0 + i * sizeof(reg->threshold_th0)); 24259dfa54cSAmit Daniel Kachhap 243b8d582b9SAmit Daniel Kachhap writel(reg->inten_rise_mask, data->base + reg->tmu_intclear); 244a0395eeeSAmit Daniel Kachhap } else { 24559dfa54cSAmit Daniel Kachhap /* Write temperature code for rising and falling threshold */ 2467ca04e58SAmit Daniel Kachhap for (i = 0; 2477ca04e58SAmit Daniel Kachhap i < trigger_levs && i < EXYNOS_MAX_TRIGGER_PER_REG; i++) { 24859dfa54cSAmit Daniel Kachhap threshold_code = temp_to_code(data, 24959dfa54cSAmit Daniel Kachhap pdata->trigger_levels[i]); 25059dfa54cSAmit Daniel Kachhap if (threshold_code < 0) { 25159dfa54cSAmit Daniel Kachhap ret = threshold_code; 25259dfa54cSAmit Daniel Kachhap goto out; 25359dfa54cSAmit Daniel Kachhap } 254*c65d3473STushar Behera rising_threshold &= ~(0xff << 8 * i); 25559dfa54cSAmit Daniel Kachhap rising_threshold |= threshold_code << 8 * i; 25659dfa54cSAmit Daniel Kachhap if (pdata->threshold_falling) { 25759dfa54cSAmit Daniel Kachhap threshold_code = temp_to_code(data, 25859dfa54cSAmit Daniel Kachhap pdata->trigger_levels[i] - 25959dfa54cSAmit Daniel Kachhap pdata->threshold_falling); 26059dfa54cSAmit Daniel Kachhap if (threshold_code > 0) 26159dfa54cSAmit Daniel Kachhap falling_threshold |= 26259dfa54cSAmit Daniel Kachhap threshold_code << 8 * i; 26359dfa54cSAmit Daniel Kachhap } 26459dfa54cSAmit Daniel Kachhap } 26559dfa54cSAmit Daniel Kachhap 26659dfa54cSAmit Daniel Kachhap writel(rising_threshold, 267b8d582b9SAmit Daniel Kachhap data->base + reg->threshold_th0); 26859dfa54cSAmit Daniel Kachhap writel(falling_threshold, 269b8d582b9SAmit Daniel Kachhap data->base + reg->threshold_th1); 27059dfa54cSAmit Daniel Kachhap 271b8d582b9SAmit Daniel Kachhap writel((reg->inten_rise_mask << reg->inten_rise_shift) | 272b8d582b9SAmit Daniel Kachhap (reg->inten_fall_mask << reg->inten_fall_shift), 273b8d582b9SAmit Daniel Kachhap data->base + reg->tmu_intclear); 2747ca04e58SAmit Daniel Kachhap 2757ca04e58SAmit Daniel Kachhap /* if last threshold limit is also present */ 2767ca04e58SAmit Daniel Kachhap i = pdata->max_trigger_level - 1; 2777ca04e58SAmit Daniel Kachhap if (pdata->trigger_levels[i] && 2787ca04e58SAmit Daniel Kachhap (pdata->trigger_type[i] == HW_TRIP)) { 2797ca04e58SAmit Daniel Kachhap threshold_code = temp_to_code(data, 2807ca04e58SAmit Daniel Kachhap pdata->trigger_levels[i]); 2817ca04e58SAmit Daniel Kachhap if (threshold_code < 0) { 2827ca04e58SAmit Daniel Kachhap ret = threshold_code; 2837ca04e58SAmit Daniel Kachhap goto out; 2847ca04e58SAmit Daniel Kachhap } 285a0395eeeSAmit Daniel Kachhap if (i == EXYNOS_MAX_TRIGGER_PER_REG - 1) { 286a0395eeeSAmit Daniel Kachhap /* 1-4 level to be assigned in th0 reg */ 287*c65d3473STushar Behera rising_threshold &= ~(0xff << 8 * i); 2887ca04e58SAmit Daniel Kachhap rising_threshold |= threshold_code << 8 * i; 2897ca04e58SAmit Daniel Kachhap writel(rising_threshold, 2907ca04e58SAmit Daniel Kachhap data->base + reg->threshold_th0); 291a0395eeeSAmit Daniel Kachhap } else if (i == EXYNOS_MAX_TRIGGER_PER_REG) { 292a0395eeeSAmit Daniel Kachhap /* 5th level to be assigned in th2 reg */ 293a0395eeeSAmit Daniel Kachhap rising_threshold = 294a0395eeeSAmit Daniel Kachhap threshold_code << reg->threshold_th3_l0_shift; 295a0395eeeSAmit Daniel Kachhap writel(rising_threshold, 296a0395eeeSAmit Daniel Kachhap data->base + reg->threshold_th2); 297a0395eeeSAmit Daniel Kachhap } 2987ca04e58SAmit Daniel Kachhap con = readl(data->base + reg->tmu_ctrl); 2997ca04e58SAmit Daniel Kachhap con |= (1 << reg->therm_trip_en_shift); 3007ca04e58SAmit Daniel Kachhap writel(con, data->base + reg->tmu_ctrl); 3017ca04e58SAmit Daniel Kachhap } 30259dfa54cSAmit Daniel Kachhap } 303a0395eeeSAmit Daniel Kachhap /*Clear the PMIN in the common TMU register*/ 304a0395eeeSAmit Daniel Kachhap if (reg->tmu_pmin && !data->id) 305a0395eeeSAmit Daniel Kachhap writel(0, data->base_common + reg->tmu_pmin); 30659dfa54cSAmit Daniel Kachhap out: 30759dfa54cSAmit Daniel Kachhap clk_disable(data->clk); 30859dfa54cSAmit Daniel Kachhap mutex_unlock(&data->lock); 30959dfa54cSAmit Daniel Kachhap 31059dfa54cSAmit Daniel Kachhap return ret; 31159dfa54cSAmit Daniel Kachhap } 31259dfa54cSAmit Daniel Kachhap 31359dfa54cSAmit Daniel Kachhap static void exynos_tmu_control(struct platform_device *pdev, bool on) 31459dfa54cSAmit Daniel Kachhap { 31559dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = platform_get_drvdata(pdev); 31659dfa54cSAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata = data->pdata; 317b8d582b9SAmit Daniel Kachhap const struct exynos_tmu_registers *reg = pdata->registers; 3181928457eSAmit Daniel Kachhap unsigned int con, interrupt_en, cal_val; 31959dfa54cSAmit Daniel Kachhap 32059dfa54cSAmit Daniel Kachhap mutex_lock(&data->lock); 32159dfa54cSAmit Daniel Kachhap clk_enable(data->clk); 32259dfa54cSAmit Daniel Kachhap 323b8d582b9SAmit Daniel Kachhap con = readl(data->base + reg->tmu_ctrl); 32459dfa54cSAmit Daniel Kachhap 32586f5362eSLukasz Majewski if (pdata->test_mux) 32686f5362eSLukasz Majewski con |= (pdata->test_mux << reg->test_mux_addr_shift); 32786f5362eSLukasz Majewski 328d0a0ce3eSAmit Daniel Kachhap if (pdata->reference_voltage) { 329b8d582b9SAmit Daniel Kachhap con &= ~(reg->buf_vref_sel_mask << reg->buf_vref_sel_shift); 330b8d582b9SAmit Daniel Kachhap con |= pdata->reference_voltage << reg->buf_vref_sel_shift; 331d0a0ce3eSAmit Daniel Kachhap } 332d0a0ce3eSAmit Daniel Kachhap 333d0a0ce3eSAmit Daniel Kachhap if (pdata->gain) { 334b8d582b9SAmit Daniel Kachhap con &= ~(reg->buf_slope_sel_mask << reg->buf_slope_sel_shift); 335b8d582b9SAmit Daniel Kachhap con |= (pdata->gain << reg->buf_slope_sel_shift); 336d0a0ce3eSAmit Daniel Kachhap } 337d0a0ce3eSAmit Daniel Kachhap 338d0a0ce3eSAmit Daniel Kachhap if (pdata->noise_cancel_mode) { 339b8d582b9SAmit Daniel Kachhap con &= ~(reg->therm_trip_mode_mask << 340b8d582b9SAmit Daniel Kachhap reg->therm_trip_mode_shift); 341b8d582b9SAmit Daniel Kachhap con |= (pdata->noise_cancel_mode << reg->therm_trip_mode_shift); 34259dfa54cSAmit Daniel Kachhap } 34359dfa54cSAmit Daniel Kachhap 3441928457eSAmit Daniel Kachhap if (pdata->cal_mode == HW_MODE) { 3451928457eSAmit Daniel Kachhap con &= ~(reg->calib_mode_mask << reg->calib_mode_shift); 3461928457eSAmit Daniel Kachhap cal_val = 0; 3471928457eSAmit Daniel Kachhap switch (pdata->cal_type) { 3481928457eSAmit Daniel Kachhap case TYPE_TWO_POINT_TRIMMING: 3491928457eSAmit Daniel Kachhap cal_val = 3; 3501928457eSAmit Daniel Kachhap break; 3511928457eSAmit Daniel Kachhap case TYPE_ONE_POINT_TRIMMING_85: 3521928457eSAmit Daniel Kachhap cal_val = 2; 3531928457eSAmit Daniel Kachhap break; 3541928457eSAmit Daniel Kachhap case TYPE_ONE_POINT_TRIMMING_25: 3551928457eSAmit Daniel Kachhap cal_val = 1; 3561928457eSAmit Daniel Kachhap break; 3571928457eSAmit Daniel Kachhap case TYPE_NONE: 3581928457eSAmit Daniel Kachhap break; 3591928457eSAmit Daniel Kachhap default: 3601928457eSAmit Daniel Kachhap dev_err(&pdev->dev, "Invalid calibration type, using none\n"); 3611928457eSAmit Daniel Kachhap } 3621928457eSAmit Daniel Kachhap con |= cal_val << reg->calib_mode_shift; 3631928457eSAmit Daniel Kachhap } 3641928457eSAmit Daniel Kachhap 36559dfa54cSAmit Daniel Kachhap if (on) { 366b8d582b9SAmit Daniel Kachhap con |= (1 << reg->core_en_shift); 367d0a0ce3eSAmit Daniel Kachhap interrupt_en = 368b8d582b9SAmit Daniel Kachhap pdata->trigger_enable[3] << reg->inten_rise3_shift | 369b8d582b9SAmit Daniel Kachhap pdata->trigger_enable[2] << reg->inten_rise2_shift | 370b8d582b9SAmit Daniel Kachhap pdata->trigger_enable[1] << reg->inten_rise1_shift | 371b8d582b9SAmit Daniel Kachhap pdata->trigger_enable[0] << reg->inten_rise0_shift; 372f4dae753SAmit Daniel Kachhap if (TMU_SUPPORTS(pdata, FALLING_TRIP)) 373d0a0ce3eSAmit Daniel Kachhap interrupt_en |= 374b8d582b9SAmit Daniel Kachhap interrupt_en << reg->inten_fall0_shift; 37559dfa54cSAmit Daniel Kachhap } else { 376b8d582b9SAmit Daniel Kachhap con &= ~(1 << reg->core_en_shift); 37759dfa54cSAmit Daniel Kachhap interrupt_en = 0; /* Disable all interrupts */ 37859dfa54cSAmit Daniel Kachhap } 379b8d582b9SAmit Daniel Kachhap writel(interrupt_en, data->base + reg->tmu_inten); 380b8d582b9SAmit Daniel Kachhap writel(con, data->base + reg->tmu_ctrl); 38159dfa54cSAmit Daniel Kachhap 38259dfa54cSAmit Daniel Kachhap clk_disable(data->clk); 38359dfa54cSAmit Daniel Kachhap mutex_unlock(&data->lock); 38459dfa54cSAmit Daniel Kachhap } 38559dfa54cSAmit Daniel Kachhap 38659dfa54cSAmit Daniel Kachhap static int exynos_tmu_read(struct exynos_tmu_data *data) 38759dfa54cSAmit Daniel Kachhap { 388b8d582b9SAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata = data->pdata; 389b8d582b9SAmit Daniel Kachhap const struct exynos_tmu_registers *reg = pdata->registers; 39059dfa54cSAmit Daniel Kachhap u8 temp_code; 39159dfa54cSAmit Daniel Kachhap int temp; 39259dfa54cSAmit Daniel Kachhap 39359dfa54cSAmit Daniel Kachhap mutex_lock(&data->lock); 39459dfa54cSAmit Daniel Kachhap clk_enable(data->clk); 39559dfa54cSAmit Daniel Kachhap 396b8d582b9SAmit Daniel Kachhap temp_code = readb(data->base + reg->tmu_cur_temp); 39759dfa54cSAmit Daniel Kachhap temp = code_to_temp(data, temp_code); 39859dfa54cSAmit Daniel Kachhap 39959dfa54cSAmit Daniel Kachhap clk_disable(data->clk); 40059dfa54cSAmit Daniel Kachhap mutex_unlock(&data->lock); 40159dfa54cSAmit Daniel Kachhap 40259dfa54cSAmit Daniel Kachhap return temp; 40359dfa54cSAmit Daniel Kachhap } 40459dfa54cSAmit Daniel Kachhap 40559dfa54cSAmit Daniel Kachhap #ifdef CONFIG_THERMAL_EMULATION 40659dfa54cSAmit Daniel Kachhap static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp) 40759dfa54cSAmit Daniel Kachhap { 40859dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = drv_data; 409b8d582b9SAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata = data->pdata; 410b8d582b9SAmit Daniel Kachhap const struct exynos_tmu_registers *reg = pdata->registers; 411b8d582b9SAmit Daniel Kachhap unsigned int val; 41259dfa54cSAmit Daniel Kachhap int ret = -EINVAL; 41359dfa54cSAmit Daniel Kachhap 414f4dae753SAmit Daniel Kachhap if (!TMU_SUPPORTS(pdata, EMULATION)) 41559dfa54cSAmit Daniel Kachhap goto out; 41659dfa54cSAmit Daniel Kachhap 41759dfa54cSAmit Daniel Kachhap if (temp && temp < MCELSIUS) 41859dfa54cSAmit Daniel Kachhap goto out; 41959dfa54cSAmit Daniel Kachhap 42059dfa54cSAmit Daniel Kachhap mutex_lock(&data->lock); 42159dfa54cSAmit Daniel Kachhap clk_enable(data->clk); 42259dfa54cSAmit Daniel Kachhap 423b8d582b9SAmit Daniel Kachhap val = readl(data->base + reg->emul_con); 42459dfa54cSAmit Daniel Kachhap 42559dfa54cSAmit Daniel Kachhap if (temp) { 42659dfa54cSAmit Daniel Kachhap temp /= MCELSIUS; 42759dfa54cSAmit Daniel Kachhap 428f4dae753SAmit Daniel Kachhap if (TMU_SUPPORTS(pdata, EMUL_TIME)) { 429f4dae753SAmit Daniel Kachhap val &= ~(EXYNOS_EMUL_TIME_MASK << reg->emul_time_shift); 430f4dae753SAmit Daniel Kachhap val |= (EXYNOS_EMUL_TIME << reg->emul_time_shift); 431f4dae753SAmit Daniel Kachhap } 432f4dae753SAmit Daniel Kachhap val &= ~(EXYNOS_EMUL_DATA_MASK << reg->emul_temp_shift); 433f4dae753SAmit Daniel Kachhap val |= (temp_to_code(data, temp) << reg->emul_temp_shift) | 434f4dae753SAmit Daniel Kachhap EXYNOS_EMUL_ENABLE; 43559dfa54cSAmit Daniel Kachhap } else { 436b8d582b9SAmit Daniel Kachhap val &= ~EXYNOS_EMUL_ENABLE; 43759dfa54cSAmit Daniel Kachhap } 43859dfa54cSAmit Daniel Kachhap 439b8d582b9SAmit Daniel Kachhap writel(val, data->base + reg->emul_con); 44059dfa54cSAmit Daniel Kachhap 44159dfa54cSAmit Daniel Kachhap clk_disable(data->clk); 44259dfa54cSAmit Daniel Kachhap mutex_unlock(&data->lock); 44359dfa54cSAmit Daniel Kachhap return 0; 44459dfa54cSAmit Daniel Kachhap out: 44559dfa54cSAmit Daniel Kachhap return ret; 44659dfa54cSAmit Daniel Kachhap } 44759dfa54cSAmit Daniel Kachhap #else 44859dfa54cSAmit Daniel Kachhap static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp) 44959dfa54cSAmit Daniel Kachhap { return -EINVAL; } 45059dfa54cSAmit Daniel Kachhap #endif/*CONFIG_THERMAL_EMULATION*/ 45159dfa54cSAmit Daniel Kachhap 45259dfa54cSAmit Daniel Kachhap static void exynos_tmu_work(struct work_struct *work) 45359dfa54cSAmit Daniel Kachhap { 45459dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = container_of(work, 45559dfa54cSAmit Daniel Kachhap struct exynos_tmu_data, irq_work); 456b8d582b9SAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata = data->pdata; 457b8d582b9SAmit Daniel Kachhap const struct exynos_tmu_registers *reg = pdata->registers; 458a0395eeeSAmit Daniel Kachhap unsigned int val_irq, val_type; 459a0395eeeSAmit Daniel Kachhap 460a0395eeeSAmit Daniel Kachhap /* Find which sensor generated this interrupt */ 461a0395eeeSAmit Daniel Kachhap if (reg->tmu_irqstatus) { 462a0395eeeSAmit Daniel Kachhap val_type = readl(data->base_common + reg->tmu_irqstatus); 463a0395eeeSAmit Daniel Kachhap if (!((val_type >> data->id) & 0x1)) 464a0395eeeSAmit Daniel Kachhap goto out; 465a0395eeeSAmit Daniel Kachhap } 46659dfa54cSAmit Daniel Kachhap 467cebe7373SAmit Daniel Kachhap exynos_report_trigger(data->reg_conf); 46859dfa54cSAmit Daniel Kachhap mutex_lock(&data->lock); 46959dfa54cSAmit Daniel Kachhap clk_enable(data->clk); 470b8d582b9SAmit Daniel Kachhap 471a4463c4fSAmit Daniel Kachhap /* TODO: take action based on particular interrupt */ 472a4463c4fSAmit Daniel Kachhap val_irq = readl(data->base + reg->tmu_intstat); 473a4463c4fSAmit Daniel Kachhap /* clear the interrupts */ 474a4463c4fSAmit Daniel Kachhap writel(val_irq, data->base + reg->tmu_intclear); 475b8d582b9SAmit Daniel Kachhap 47659dfa54cSAmit Daniel Kachhap clk_disable(data->clk); 47759dfa54cSAmit Daniel Kachhap mutex_unlock(&data->lock); 478a0395eeeSAmit Daniel Kachhap out: 47959dfa54cSAmit Daniel Kachhap enable_irq(data->irq); 48059dfa54cSAmit Daniel Kachhap } 48159dfa54cSAmit Daniel Kachhap 48259dfa54cSAmit Daniel Kachhap static irqreturn_t exynos_tmu_irq(int irq, void *id) 48359dfa54cSAmit Daniel Kachhap { 48459dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = id; 48559dfa54cSAmit Daniel Kachhap 48659dfa54cSAmit Daniel Kachhap disable_irq_nosync(irq); 48759dfa54cSAmit Daniel Kachhap schedule_work(&data->irq_work); 48859dfa54cSAmit Daniel Kachhap 48959dfa54cSAmit Daniel Kachhap return IRQ_HANDLED; 49059dfa54cSAmit Daniel Kachhap } 49159dfa54cSAmit Daniel Kachhap 49259dfa54cSAmit Daniel Kachhap static const struct of_device_id exynos_tmu_match[] = { 49359dfa54cSAmit Daniel Kachhap { 49459dfa54cSAmit Daniel Kachhap .compatible = "samsung,exynos4210-tmu", 49559dfa54cSAmit Daniel Kachhap .data = (void *)EXYNOS4210_TMU_DRV_DATA, 49659dfa54cSAmit Daniel Kachhap }, 49759dfa54cSAmit Daniel Kachhap { 49859dfa54cSAmit Daniel Kachhap .compatible = "samsung,exynos4412-tmu", 49914ddfaecSLukasz Majewski .data = (void *)EXYNOS4412_TMU_DRV_DATA, 50059dfa54cSAmit Daniel Kachhap }, 50159dfa54cSAmit Daniel Kachhap { 50259dfa54cSAmit Daniel Kachhap .compatible = "samsung,exynos5250-tmu", 503e6b7991eSAmit Daniel Kachhap .data = (void *)EXYNOS5250_TMU_DRV_DATA, 50459dfa54cSAmit Daniel Kachhap }, 50590542546SAmit Daniel Kachhap { 50690542546SAmit Daniel Kachhap .compatible = "samsung,exynos5440-tmu", 50790542546SAmit Daniel Kachhap .data = (void *)EXYNOS5440_TMU_DRV_DATA, 50890542546SAmit Daniel Kachhap }, 50959dfa54cSAmit Daniel Kachhap {}, 51059dfa54cSAmit Daniel Kachhap }; 51159dfa54cSAmit Daniel Kachhap MODULE_DEVICE_TABLE(of, exynos_tmu_match); 51259dfa54cSAmit Daniel Kachhap 51359dfa54cSAmit Daniel Kachhap static inline struct exynos_tmu_platform_data *exynos_get_driver_data( 514cebe7373SAmit Daniel Kachhap struct platform_device *pdev, int id) 51559dfa54cSAmit Daniel Kachhap { 516cebe7373SAmit Daniel Kachhap struct exynos_tmu_init_data *data_table; 517cebe7373SAmit Daniel Kachhap struct exynos_tmu_platform_data *tmu_data; 51859dfa54cSAmit Daniel Kachhap const struct of_device_id *match; 51973b5b1d7SSachin Kamat 52059dfa54cSAmit Daniel Kachhap match = of_match_node(exynos_tmu_match, pdev->dev.of_node); 52159dfa54cSAmit Daniel Kachhap if (!match) 52259dfa54cSAmit Daniel Kachhap return NULL; 523cebe7373SAmit Daniel Kachhap data_table = (struct exynos_tmu_init_data *) match->data; 524cebe7373SAmit Daniel Kachhap if (!data_table || id >= data_table->tmu_count) 525cebe7373SAmit Daniel Kachhap return NULL; 526cebe7373SAmit Daniel Kachhap tmu_data = data_table->tmu_data; 527cebe7373SAmit Daniel Kachhap return (struct exynos_tmu_platform_data *) (tmu_data + id); 52859dfa54cSAmit Daniel Kachhap } 52959dfa54cSAmit Daniel Kachhap 530cebe7373SAmit Daniel Kachhap static int exynos_map_dt_data(struct platform_device *pdev) 53159dfa54cSAmit Daniel Kachhap { 532cebe7373SAmit Daniel Kachhap struct exynos_tmu_data *data = platform_get_drvdata(pdev); 533cebe7373SAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata; 534cebe7373SAmit Daniel Kachhap struct resource res; 535498d22f6SAmit Daniel Kachhap int ret; 53659dfa54cSAmit Daniel Kachhap 53773b5b1d7SSachin Kamat if (!data || !pdev->dev.of_node) 538cebe7373SAmit Daniel Kachhap return -ENODEV; 53959dfa54cSAmit Daniel Kachhap 540498d22f6SAmit Daniel Kachhap /* 541498d22f6SAmit Daniel Kachhap * Try enabling the regulator if found 542498d22f6SAmit Daniel Kachhap * TODO: Add regulator as an SOC feature, so that regulator enable 543498d22f6SAmit Daniel Kachhap * is a compulsory call. 544498d22f6SAmit Daniel Kachhap */ 545498d22f6SAmit Daniel Kachhap data->regulator = devm_regulator_get(&pdev->dev, "vtmu"); 546498d22f6SAmit Daniel Kachhap if (!IS_ERR(data->regulator)) { 547498d22f6SAmit Daniel Kachhap ret = regulator_enable(data->regulator); 548498d22f6SAmit Daniel Kachhap if (ret) { 549498d22f6SAmit Daniel Kachhap dev_err(&pdev->dev, "failed to enable vtmu\n"); 550498d22f6SAmit Daniel Kachhap return ret; 551498d22f6SAmit Daniel Kachhap } 552498d22f6SAmit Daniel Kachhap } else { 553498d22f6SAmit Daniel Kachhap dev_info(&pdev->dev, "Regulator node (vtmu) not found\n"); 554498d22f6SAmit Daniel Kachhap } 555498d22f6SAmit Daniel Kachhap 556cebe7373SAmit Daniel Kachhap data->id = of_alias_get_id(pdev->dev.of_node, "tmuctrl"); 557cebe7373SAmit Daniel Kachhap if (data->id < 0) 558cebe7373SAmit Daniel Kachhap data->id = 0; 559cebe7373SAmit Daniel Kachhap 560cebe7373SAmit Daniel Kachhap data->irq = irq_of_parse_and_map(pdev->dev.of_node, 0); 561cebe7373SAmit Daniel Kachhap if (data->irq <= 0) { 562cebe7373SAmit Daniel Kachhap dev_err(&pdev->dev, "failed to get IRQ\n"); 563cebe7373SAmit Daniel Kachhap return -ENODEV; 564cebe7373SAmit Daniel Kachhap } 565cebe7373SAmit Daniel Kachhap 566cebe7373SAmit Daniel Kachhap if (of_address_to_resource(pdev->dev.of_node, 0, &res)) { 567cebe7373SAmit Daniel Kachhap dev_err(&pdev->dev, "failed to get Resource 0\n"); 568cebe7373SAmit Daniel Kachhap return -ENODEV; 569cebe7373SAmit Daniel Kachhap } 570cebe7373SAmit Daniel Kachhap 571cebe7373SAmit Daniel Kachhap data->base = devm_ioremap(&pdev->dev, res.start, resource_size(&res)); 572cebe7373SAmit Daniel Kachhap if (!data->base) { 573cebe7373SAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to ioremap memory\n"); 574cebe7373SAmit Daniel Kachhap return -EADDRNOTAVAIL; 575cebe7373SAmit Daniel Kachhap } 576cebe7373SAmit Daniel Kachhap 577cebe7373SAmit Daniel Kachhap pdata = exynos_get_driver_data(pdev, data->id); 57859dfa54cSAmit Daniel Kachhap if (!pdata) { 57959dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "No platform init data supplied.\n"); 58059dfa54cSAmit Daniel Kachhap return -ENODEV; 58159dfa54cSAmit Daniel Kachhap } 582cebe7373SAmit Daniel Kachhap data->pdata = pdata; 583d9b6ee14SAmit Daniel Kachhap /* 584d9b6ee14SAmit Daniel Kachhap * Check if the TMU shares some registers and then try to map the 585d9b6ee14SAmit Daniel Kachhap * memory of common registers. 586d9b6ee14SAmit Daniel Kachhap */ 587d9b6ee14SAmit Daniel Kachhap if (!TMU_SUPPORTS(pdata, SHARED_MEMORY)) 588d9b6ee14SAmit Daniel Kachhap return 0; 589d9b6ee14SAmit Daniel Kachhap 590d9b6ee14SAmit Daniel Kachhap if (of_address_to_resource(pdev->dev.of_node, 1, &res)) { 591d9b6ee14SAmit Daniel Kachhap dev_err(&pdev->dev, "failed to get Resource 1\n"); 592d9b6ee14SAmit Daniel Kachhap return -ENODEV; 593d9b6ee14SAmit Daniel Kachhap } 594d9b6ee14SAmit Daniel Kachhap 595d9b6ee14SAmit Daniel Kachhap data->base_common = devm_ioremap(&pdev->dev, res.start, 596d9b6ee14SAmit Daniel Kachhap resource_size(&res)); 597aa1ab434SNaveen Krishna Chatradhi if (!data->base_common) { 598d9b6ee14SAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to ioremap memory\n"); 599d9b6ee14SAmit Daniel Kachhap return -ENOMEM; 600d9b6ee14SAmit Daniel Kachhap } 601cebe7373SAmit Daniel Kachhap 602cebe7373SAmit Daniel Kachhap return 0; 603cebe7373SAmit Daniel Kachhap } 604cebe7373SAmit Daniel Kachhap 605cebe7373SAmit Daniel Kachhap static int exynos_tmu_probe(struct platform_device *pdev) 606cebe7373SAmit Daniel Kachhap { 607cebe7373SAmit Daniel Kachhap struct exynos_tmu_data *data; 608cebe7373SAmit Daniel Kachhap struct exynos_tmu_platform_data *pdata; 609cebe7373SAmit Daniel Kachhap struct thermal_sensor_conf *sensor_conf; 610cebe7373SAmit Daniel Kachhap int ret, i; 611cebe7373SAmit Daniel Kachhap 61259dfa54cSAmit Daniel Kachhap data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data), 61359dfa54cSAmit Daniel Kachhap GFP_KERNEL); 61459dfa54cSAmit Daniel Kachhap if (!data) { 61559dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to allocate driver structure\n"); 61659dfa54cSAmit Daniel Kachhap return -ENOMEM; 61759dfa54cSAmit Daniel Kachhap } 61859dfa54cSAmit Daniel Kachhap 619cebe7373SAmit Daniel Kachhap platform_set_drvdata(pdev, data); 620cebe7373SAmit Daniel Kachhap mutex_init(&data->lock); 621cebe7373SAmit Daniel Kachhap 622cebe7373SAmit Daniel Kachhap ret = exynos_map_dt_data(pdev); 623cebe7373SAmit Daniel Kachhap if (ret) 624cebe7373SAmit Daniel Kachhap return ret; 625cebe7373SAmit Daniel Kachhap 626cebe7373SAmit Daniel Kachhap pdata = data->pdata; 62759dfa54cSAmit Daniel Kachhap 62859dfa54cSAmit Daniel Kachhap INIT_WORK(&data->irq_work, exynos_tmu_work); 62959dfa54cSAmit Daniel Kachhap 63059dfa54cSAmit Daniel Kachhap data->clk = devm_clk_get(&pdev->dev, "tmu_apbif"); 63159dfa54cSAmit Daniel Kachhap if (IS_ERR(data->clk)) { 63259dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to get clock\n"); 63359dfa54cSAmit Daniel Kachhap return PTR_ERR(data->clk); 63459dfa54cSAmit Daniel Kachhap } 63559dfa54cSAmit Daniel Kachhap 63659dfa54cSAmit Daniel Kachhap ret = clk_prepare(data->clk); 63759dfa54cSAmit Daniel Kachhap if (ret) 63859dfa54cSAmit Daniel Kachhap return ret; 63959dfa54cSAmit Daniel Kachhap 64014ddfaecSLukasz Majewski if (pdata->type == SOC_ARCH_EXYNOS4210 || 64114ddfaecSLukasz Majewski pdata->type == SOC_ARCH_EXYNOS4412 || 64214ddfaecSLukasz Majewski pdata->type == SOC_ARCH_EXYNOS5250 || 643a0395eeeSAmit Daniel Kachhap pdata->type == SOC_ARCH_EXYNOS5440) 64459dfa54cSAmit Daniel Kachhap data->soc = pdata->type; 64559dfa54cSAmit Daniel Kachhap else { 64659dfa54cSAmit Daniel Kachhap ret = -EINVAL; 64759dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "Platform not supported\n"); 64859dfa54cSAmit Daniel Kachhap goto err_clk; 64959dfa54cSAmit Daniel Kachhap } 65059dfa54cSAmit Daniel Kachhap 65159dfa54cSAmit Daniel Kachhap ret = exynos_tmu_initialize(pdev); 65259dfa54cSAmit Daniel Kachhap if (ret) { 65359dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to initialize TMU\n"); 65459dfa54cSAmit Daniel Kachhap goto err_clk; 65559dfa54cSAmit Daniel Kachhap } 65659dfa54cSAmit Daniel Kachhap 65759dfa54cSAmit Daniel Kachhap exynos_tmu_control(pdev, true); 65859dfa54cSAmit Daniel Kachhap 659cebe7373SAmit Daniel Kachhap /* Allocate a structure to register with the exynos core thermal */ 660cebe7373SAmit Daniel Kachhap sensor_conf = devm_kzalloc(&pdev->dev, 661cebe7373SAmit Daniel Kachhap sizeof(struct thermal_sensor_conf), GFP_KERNEL); 662cebe7373SAmit Daniel Kachhap if (!sensor_conf) { 663cebe7373SAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to allocate registration struct\n"); 664cebe7373SAmit Daniel Kachhap ret = -ENOMEM; 665cebe7373SAmit Daniel Kachhap goto err_clk; 666cebe7373SAmit Daniel Kachhap } 667cebe7373SAmit Daniel Kachhap sprintf(sensor_conf->name, "therm_zone%d", data->id); 668cebe7373SAmit Daniel Kachhap sensor_conf->read_temperature = (int (*)(void *))exynos_tmu_read; 669cebe7373SAmit Daniel Kachhap sensor_conf->write_emul_temp = 670cebe7373SAmit Daniel Kachhap (int (*)(void *, unsigned long))exynos_tmu_set_emulation; 671cebe7373SAmit Daniel Kachhap sensor_conf->driver_data = data; 672cebe7373SAmit Daniel Kachhap sensor_conf->trip_data.trip_count = pdata->trigger_enable[0] + 673bb34b4c8SAmit Daniel Kachhap pdata->trigger_enable[1] + pdata->trigger_enable[2]+ 674bb34b4c8SAmit Daniel Kachhap pdata->trigger_enable[3]; 67559dfa54cSAmit Daniel Kachhap 676cebe7373SAmit Daniel Kachhap for (i = 0; i < sensor_conf->trip_data.trip_count; i++) { 677cebe7373SAmit Daniel Kachhap sensor_conf->trip_data.trip_val[i] = 67859dfa54cSAmit Daniel Kachhap pdata->threshold + pdata->trigger_levels[i]; 679cebe7373SAmit Daniel Kachhap sensor_conf->trip_data.trip_type[i] = 6805c3cf552SAmit Daniel Kachhap pdata->trigger_type[i]; 6815c3cf552SAmit Daniel Kachhap } 68259dfa54cSAmit Daniel Kachhap 683cebe7373SAmit Daniel Kachhap sensor_conf->trip_data.trigger_falling = pdata->threshold_falling; 68459dfa54cSAmit Daniel Kachhap 685cebe7373SAmit Daniel Kachhap sensor_conf->cooling_data.freq_clip_count = pdata->freq_tab_count; 68659dfa54cSAmit Daniel Kachhap for (i = 0; i < pdata->freq_tab_count; i++) { 687cebe7373SAmit Daniel Kachhap sensor_conf->cooling_data.freq_data[i].freq_clip_max = 68859dfa54cSAmit Daniel Kachhap pdata->freq_tab[i].freq_clip_max; 689cebe7373SAmit Daniel Kachhap sensor_conf->cooling_data.freq_data[i].temp_level = 69059dfa54cSAmit Daniel Kachhap pdata->freq_tab[i].temp_level; 69159dfa54cSAmit Daniel Kachhap } 692cebe7373SAmit Daniel Kachhap sensor_conf->dev = &pdev->dev; 693cebe7373SAmit Daniel Kachhap /* Register the sensor with thermal management interface */ 694cebe7373SAmit Daniel Kachhap ret = exynos_register_thermal(sensor_conf); 69559dfa54cSAmit Daniel Kachhap if (ret) { 69659dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to register thermal interface\n"); 69759dfa54cSAmit Daniel Kachhap goto err_clk; 69859dfa54cSAmit Daniel Kachhap } 699cebe7373SAmit Daniel Kachhap data->reg_conf = sensor_conf; 700cebe7373SAmit Daniel Kachhap 701cebe7373SAmit Daniel Kachhap ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq, 702cebe7373SAmit Daniel Kachhap IRQF_TRIGGER_RISING | IRQF_SHARED, dev_name(&pdev->dev), data); 703cebe7373SAmit Daniel Kachhap if (ret) { 704cebe7373SAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq); 705cebe7373SAmit Daniel Kachhap goto err_clk; 706cebe7373SAmit Daniel Kachhap } 70759dfa54cSAmit Daniel Kachhap 70859dfa54cSAmit Daniel Kachhap return 0; 70959dfa54cSAmit Daniel Kachhap err_clk: 71059dfa54cSAmit Daniel Kachhap clk_unprepare(data->clk); 71159dfa54cSAmit Daniel Kachhap return ret; 71259dfa54cSAmit Daniel Kachhap } 71359dfa54cSAmit Daniel Kachhap 71459dfa54cSAmit Daniel Kachhap static int exynos_tmu_remove(struct platform_device *pdev) 71559dfa54cSAmit Daniel Kachhap { 71659dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = platform_get_drvdata(pdev); 71759dfa54cSAmit Daniel Kachhap 71859dfa54cSAmit Daniel Kachhap exynos_tmu_control(pdev, false); 71959dfa54cSAmit Daniel Kachhap 720cebe7373SAmit Daniel Kachhap exynos_unregister_thermal(data->reg_conf); 72159dfa54cSAmit Daniel Kachhap 72259dfa54cSAmit Daniel Kachhap clk_unprepare(data->clk); 72359dfa54cSAmit Daniel Kachhap 724498d22f6SAmit Daniel Kachhap if (!IS_ERR(data->regulator)) 725498d22f6SAmit Daniel Kachhap regulator_disable(data->regulator); 726498d22f6SAmit Daniel Kachhap 72759dfa54cSAmit Daniel Kachhap return 0; 72859dfa54cSAmit Daniel Kachhap } 72959dfa54cSAmit Daniel Kachhap 73059dfa54cSAmit Daniel Kachhap #ifdef CONFIG_PM_SLEEP 73159dfa54cSAmit Daniel Kachhap static int exynos_tmu_suspend(struct device *dev) 73259dfa54cSAmit Daniel Kachhap { 73359dfa54cSAmit Daniel Kachhap exynos_tmu_control(to_platform_device(dev), false); 73459dfa54cSAmit Daniel Kachhap 73559dfa54cSAmit Daniel Kachhap return 0; 73659dfa54cSAmit Daniel Kachhap } 73759dfa54cSAmit Daniel Kachhap 73859dfa54cSAmit Daniel Kachhap static int exynos_tmu_resume(struct device *dev) 73959dfa54cSAmit Daniel Kachhap { 74059dfa54cSAmit Daniel Kachhap struct platform_device *pdev = to_platform_device(dev); 74159dfa54cSAmit Daniel Kachhap 74259dfa54cSAmit Daniel Kachhap exynos_tmu_initialize(pdev); 74359dfa54cSAmit Daniel Kachhap exynos_tmu_control(pdev, true); 74459dfa54cSAmit Daniel Kachhap 74559dfa54cSAmit Daniel Kachhap return 0; 74659dfa54cSAmit Daniel Kachhap } 74759dfa54cSAmit Daniel Kachhap 74859dfa54cSAmit Daniel Kachhap static SIMPLE_DEV_PM_OPS(exynos_tmu_pm, 74959dfa54cSAmit Daniel Kachhap exynos_tmu_suspend, exynos_tmu_resume); 75059dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM (&exynos_tmu_pm) 75159dfa54cSAmit Daniel Kachhap #else 75259dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM NULL 75359dfa54cSAmit Daniel Kachhap #endif 75459dfa54cSAmit Daniel Kachhap 75559dfa54cSAmit Daniel Kachhap static struct platform_driver exynos_tmu_driver = { 75659dfa54cSAmit Daniel Kachhap .driver = { 75759dfa54cSAmit Daniel Kachhap .name = "exynos-tmu", 75859dfa54cSAmit Daniel Kachhap .owner = THIS_MODULE, 75959dfa54cSAmit Daniel Kachhap .pm = EXYNOS_TMU_PM, 76073b5b1d7SSachin Kamat .of_match_table = exynos_tmu_match, 76159dfa54cSAmit Daniel Kachhap }, 76259dfa54cSAmit Daniel Kachhap .probe = exynos_tmu_probe, 76359dfa54cSAmit Daniel Kachhap .remove = exynos_tmu_remove, 76459dfa54cSAmit Daniel Kachhap }; 76559dfa54cSAmit Daniel Kachhap 76659dfa54cSAmit Daniel Kachhap module_platform_driver(exynos_tmu_driver); 76759dfa54cSAmit Daniel Kachhap 76859dfa54cSAmit Daniel Kachhap MODULE_DESCRIPTION("EXYNOS TMU Driver"); 76959dfa54cSAmit Daniel Kachhap MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>"); 77059dfa54cSAmit Daniel Kachhap MODULE_LICENSE("GPL"); 77159dfa54cSAmit Daniel Kachhap MODULE_ALIAS("platform:exynos-tmu"); 772