159dfa54cSAmit Daniel Kachhap /* 259dfa54cSAmit Daniel Kachhap * exynos_tmu.c - Samsung EXYNOS TMU (Thermal Management Unit) 359dfa54cSAmit Daniel Kachhap * 43b6a1a80SLukasz Majewski * Copyright (C) 2014 Samsung Electronics 53b6a1a80SLukasz Majewski * Bartlomiej Zolnierkiewicz <b.zolnierkie@samsung.com> 63b6a1a80SLukasz Majewski * Lukasz Majewski <l.majewski@samsung.com> 73b6a1a80SLukasz Majewski * 859dfa54cSAmit Daniel Kachhap * Copyright (C) 2011 Samsung Electronics 959dfa54cSAmit Daniel Kachhap * Donggeun Kim <dg77.kim@samsung.com> 1059dfa54cSAmit Daniel Kachhap * Amit Daniel Kachhap <amit.kachhap@linaro.org> 1159dfa54cSAmit Daniel Kachhap * 1259dfa54cSAmit Daniel Kachhap * This program is free software; you can redistribute it and/or modify 1359dfa54cSAmit Daniel Kachhap * it under the terms of the GNU General Public License as published by 1459dfa54cSAmit Daniel Kachhap * the Free Software Foundation; either version 2 of the License, or 1559dfa54cSAmit Daniel Kachhap * (at your option) any later version. 1659dfa54cSAmit Daniel Kachhap * 1759dfa54cSAmit Daniel Kachhap * This program is distributed in the hope that it will be useful, 1859dfa54cSAmit Daniel Kachhap * but WITHOUT ANY WARRANTY; without even the implied warranty of 1959dfa54cSAmit Daniel Kachhap * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 2059dfa54cSAmit Daniel Kachhap * GNU General Public License for more details. 2159dfa54cSAmit Daniel Kachhap * 2259dfa54cSAmit Daniel Kachhap * You should have received a copy of the GNU General Public License 2359dfa54cSAmit Daniel Kachhap * along with this program; if not, write to the Free Software 2459dfa54cSAmit Daniel Kachhap * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 2559dfa54cSAmit Daniel Kachhap * 2659dfa54cSAmit Daniel Kachhap */ 2759dfa54cSAmit Daniel Kachhap 2859dfa54cSAmit Daniel Kachhap #include <linux/clk.h> 2959dfa54cSAmit Daniel Kachhap #include <linux/io.h> 3059dfa54cSAmit Daniel Kachhap #include <linux/interrupt.h> 3159dfa54cSAmit Daniel Kachhap #include <linux/module.h> 32fee88e2bSMaciej Purski #include <linux/of_device.h> 33cebe7373SAmit Daniel Kachhap #include <linux/of_address.h> 34cebe7373SAmit Daniel Kachhap #include <linux/of_irq.h> 3559dfa54cSAmit Daniel Kachhap #include <linux/platform_device.h> 36498d22f6SAmit Daniel Kachhap #include <linux/regulator/consumer.h> 3759dfa54cSAmit Daniel Kachhap 387efd18a2SBartlomiej Zolnierkiewicz #include <dt-bindings/thermal/thermal_exynos.h> 397efd18a2SBartlomiej Zolnierkiewicz 403b6a1a80SLukasz Majewski #include "../thermal_core.h" 412845f6ecSBartlomiej Zolnierkiewicz 422845f6ecSBartlomiej Zolnierkiewicz /* Exynos generic registers */ 432845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_REG_TRIMINFO 0x0 442845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_REG_CONTROL 0x20 452845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_REG_STATUS 0x28 462845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_REG_CURRENT_TEMP 0x40 472845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_REG_INTEN 0x70 482845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_REG_INTSTAT 0x74 492845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_REG_INTCLEAR 0x78 502845f6ecSBartlomiej Zolnierkiewicz 512845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_TEMP_MASK 0xff 522845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_REF_VOLTAGE_SHIFT 24 532845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_REF_VOLTAGE_MASK 0x1f 542845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_BUF_SLOPE_SEL_MASK 0xf 552845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT 8 562845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_CORE_EN_SHIFT 0 572845f6ecSBartlomiej Zolnierkiewicz 582845f6ecSBartlomiej Zolnierkiewicz /* Exynos3250 specific registers */ 592845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_TRIMINFO_CON1 0x10 602845f6ecSBartlomiej Zolnierkiewicz 612845f6ecSBartlomiej Zolnierkiewicz /* Exynos4210 specific registers */ 622845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS4210_TMU_REG_THRESHOLD_TEMP 0x44 632845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS4210_TMU_REG_TRIG_LEVEL0 0x50 642845f6ecSBartlomiej Zolnierkiewicz 652845f6ecSBartlomiej Zolnierkiewicz /* Exynos5250, Exynos4412, Exynos3250 specific registers */ 662845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_TRIMINFO_CON2 0x14 672845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_THD_TEMP_RISE 0x50 682845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_THD_TEMP_FALL 0x54 692845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_EMUL_CON 0x80 702845f6ecSBartlomiej Zolnierkiewicz 712845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TRIMINFO_RELOAD_ENABLE 1 722845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TRIMINFO_25_SHIFT 0 732845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TRIMINFO_85_SHIFT 8 742845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_TRIP_MODE_SHIFT 13 752845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_TRIP_MODE_MASK 0x7 762845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_THERM_TRIP_EN_SHIFT 12 772845f6ecSBartlomiej Zolnierkiewicz 782845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_INTEN_RISE0_SHIFT 0 792845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_INTEN_RISE1_SHIFT 4 802845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_INTEN_RISE2_SHIFT 8 812845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_INTEN_RISE3_SHIFT 12 822845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_INTEN_FALL0_SHIFT 16 832845f6ecSBartlomiej Zolnierkiewicz 842845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_EMUL_TIME 0x57F0 852845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_EMUL_TIME_MASK 0xffff 862845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_EMUL_TIME_SHIFT 16 872845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_EMUL_DATA_SHIFT 8 882845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_EMUL_DATA_MASK 0xFF 892845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_EMUL_ENABLE 0x1 902845f6ecSBartlomiej Zolnierkiewicz 912845f6ecSBartlomiej Zolnierkiewicz /* Exynos5260 specific */ 922845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS5260_TMU_REG_INTEN 0xC0 932845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS5260_TMU_REG_INTSTAT 0xC4 942845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS5260_TMU_REG_INTCLEAR 0xC8 952845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS5260_EMUL_CON 0x100 962845f6ecSBartlomiej Zolnierkiewicz 972845f6ecSBartlomiej Zolnierkiewicz /* Exynos4412 specific */ 982845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS4412_MUX_ADDR_VALUE 6 992845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS4412_MUX_ADDR_SHIFT 20 1002845f6ecSBartlomiej Zolnierkiewicz 101488c7455SChanwoo Choi /* Exynos5433 specific registers */ 102488c7455SChanwoo Choi #define EXYNOS5433_TMU_REG_CONTROL1 0x024 103488c7455SChanwoo Choi #define EXYNOS5433_TMU_SAMPLING_INTERVAL 0x02c 104488c7455SChanwoo Choi #define EXYNOS5433_TMU_COUNTER_VALUE0 0x030 105488c7455SChanwoo Choi #define EXYNOS5433_TMU_COUNTER_VALUE1 0x034 106488c7455SChanwoo Choi #define EXYNOS5433_TMU_REG_CURRENT_TEMP1 0x044 107488c7455SChanwoo Choi #define EXYNOS5433_THD_TEMP_RISE3_0 0x050 108488c7455SChanwoo Choi #define EXYNOS5433_THD_TEMP_RISE7_4 0x054 109488c7455SChanwoo Choi #define EXYNOS5433_THD_TEMP_FALL3_0 0x060 110488c7455SChanwoo Choi #define EXYNOS5433_THD_TEMP_FALL7_4 0x064 111488c7455SChanwoo Choi #define EXYNOS5433_TMU_REG_INTEN 0x0c0 112488c7455SChanwoo Choi #define EXYNOS5433_TMU_REG_INTPEND 0x0c8 113488c7455SChanwoo Choi #define EXYNOS5433_TMU_EMUL_CON 0x110 114488c7455SChanwoo Choi #define EXYNOS5433_TMU_PD_DET_EN 0x130 115488c7455SChanwoo Choi 116488c7455SChanwoo Choi #define EXYNOS5433_TRIMINFO_SENSOR_ID_SHIFT 16 117488c7455SChanwoo Choi #define EXYNOS5433_TRIMINFO_CALIB_SEL_SHIFT 23 118488c7455SChanwoo Choi #define EXYNOS5433_TRIMINFO_SENSOR_ID_MASK \ 119488c7455SChanwoo Choi (0xf << EXYNOS5433_TRIMINFO_SENSOR_ID_SHIFT) 120488c7455SChanwoo Choi #define EXYNOS5433_TRIMINFO_CALIB_SEL_MASK BIT(23) 121488c7455SChanwoo Choi 122488c7455SChanwoo Choi #define EXYNOS5433_TRIMINFO_ONE_POINT_TRIMMING 0 123488c7455SChanwoo Choi #define EXYNOS5433_TRIMINFO_TWO_POINT_TRIMMING 1 124488c7455SChanwoo Choi 125488c7455SChanwoo Choi #define EXYNOS5433_PD_DET_EN 1 126488c7455SChanwoo Choi 12761020d18SBartlomiej Zolnierkiewicz #define EXYNOS5433_G3D_BASE 0x10070000 12861020d18SBartlomiej Zolnierkiewicz 1296c247393SAbhilash Kesavan /* Exynos7 specific registers */ 1306c247393SAbhilash Kesavan #define EXYNOS7_THD_TEMP_RISE7_6 0x50 1316c247393SAbhilash Kesavan #define EXYNOS7_THD_TEMP_FALL7_6 0x60 1326c247393SAbhilash Kesavan #define EXYNOS7_TMU_REG_INTEN 0x110 1336c247393SAbhilash Kesavan #define EXYNOS7_TMU_REG_INTPEND 0x118 1346c247393SAbhilash Kesavan #define EXYNOS7_TMU_REG_EMUL_CON 0x160 1356c247393SAbhilash Kesavan 1366c247393SAbhilash Kesavan #define EXYNOS7_TMU_TEMP_MASK 0x1ff 1376c247393SAbhilash Kesavan #define EXYNOS7_PD_DET_EN_SHIFT 23 1386c247393SAbhilash Kesavan #define EXYNOS7_TMU_INTEN_RISE0_SHIFT 0 1396c247393SAbhilash Kesavan #define EXYNOS7_TMU_INTEN_RISE1_SHIFT 1 1406c247393SAbhilash Kesavan #define EXYNOS7_TMU_INTEN_RISE2_SHIFT 2 1416c247393SAbhilash Kesavan #define EXYNOS7_TMU_INTEN_RISE3_SHIFT 3 1426c247393SAbhilash Kesavan #define EXYNOS7_TMU_INTEN_RISE4_SHIFT 4 1436c247393SAbhilash Kesavan #define EXYNOS7_TMU_INTEN_RISE5_SHIFT 5 1446c247393SAbhilash Kesavan #define EXYNOS7_TMU_INTEN_RISE6_SHIFT 6 1456c247393SAbhilash Kesavan #define EXYNOS7_TMU_INTEN_RISE7_SHIFT 7 1466c247393SAbhilash Kesavan #define EXYNOS7_EMUL_DATA_SHIFT 7 1476c247393SAbhilash Kesavan #define EXYNOS7_EMUL_DATA_MASK 0x1ff 1486c247393SAbhilash Kesavan 149718b4ca1SBartlomiej Zolnierkiewicz #define EXYNOS_FIRST_POINT_TRIM 25 150718b4ca1SBartlomiej Zolnierkiewicz #define EXYNOS_SECOND_POINT_TRIM 85 151718b4ca1SBartlomiej Zolnierkiewicz 15209d29426SBartlomiej Zolnierkiewicz #define EXYNOS_NOISE_CANCEL_MODE 4 15309d29426SBartlomiej Zolnierkiewicz 1543b6a1a80SLukasz Majewski #define MCELSIUS 1000 1557efd18a2SBartlomiej Zolnierkiewicz 1567efd18a2SBartlomiej Zolnierkiewicz enum soc_type { 1577efd18a2SBartlomiej Zolnierkiewicz SOC_ARCH_EXYNOS3250 = 1, 1587efd18a2SBartlomiej Zolnierkiewicz SOC_ARCH_EXYNOS4210, 1597efd18a2SBartlomiej Zolnierkiewicz SOC_ARCH_EXYNOS4412, 1607efd18a2SBartlomiej Zolnierkiewicz SOC_ARCH_EXYNOS5250, 1617efd18a2SBartlomiej Zolnierkiewicz SOC_ARCH_EXYNOS5260, 1627efd18a2SBartlomiej Zolnierkiewicz SOC_ARCH_EXYNOS5420, 1637efd18a2SBartlomiej Zolnierkiewicz SOC_ARCH_EXYNOS5420_TRIMINFO, 1647efd18a2SBartlomiej Zolnierkiewicz SOC_ARCH_EXYNOS5433, 1657efd18a2SBartlomiej Zolnierkiewicz SOC_ARCH_EXYNOS7, 1667efd18a2SBartlomiej Zolnierkiewicz }; 1677efd18a2SBartlomiej Zolnierkiewicz 168cebe7373SAmit Daniel Kachhap /** 169cebe7373SAmit Daniel Kachhap * struct exynos_tmu_data : A structure to hold the private data of the TMU 170cebe7373SAmit Daniel Kachhap driver 171cebe7373SAmit Daniel Kachhap * @id: identifier of the one instance of the TMU controller. 172cebe7373SAmit Daniel Kachhap * @base: base address of the single instance of the TMU controller. 1739025d563SNaveen Krishna Chatradhi * @base_second: base address of the common registers of the TMU controller. 174cebe7373SAmit Daniel Kachhap * @irq: irq number of the TMU controller. 175cebe7373SAmit Daniel Kachhap * @soc: id of the SOC type. 176cebe7373SAmit Daniel Kachhap * @irq_work: pointer to the irq work structure. 177cebe7373SAmit Daniel Kachhap * @lock: lock to implement synchronization. 178cebe7373SAmit Daniel Kachhap * @clk: pointer to the clock structure. 17914a11dc7SNaveen Krishna Chatradhi * @clk_sec: pointer to the clock structure for accessing the base_second. 1806c247393SAbhilash Kesavan * @sclk: pointer to the clock structure for accessing the tmu special clk. 181199b3e3cSBartlomiej Zolnierkiewicz * @cal_type: calibration type for temperature 182e3ed3649SBartlomiej Zolnierkiewicz * @efuse_value: SoC defined fuse value 183e3ed3649SBartlomiej Zolnierkiewicz * @min_efuse_value: minimum valid trimming data 184e3ed3649SBartlomiej Zolnierkiewicz * @max_efuse_value: maximum valid trimming data 185cebe7373SAmit Daniel Kachhap * @temp_error1: fused value of the first point trim. 186cebe7373SAmit Daniel Kachhap * @temp_error2: fused value of the second point trim. 187fccfe099SBartlomiej Zolnierkiewicz * @gain: gain of amplifier in the positive-TC generator block 188fccfe099SBartlomiej Zolnierkiewicz * 0 < gain <= 15 18961020d18SBartlomiej Zolnierkiewicz * @reference_voltage: reference voltage of amplifier 19061020d18SBartlomiej Zolnierkiewicz * in the positive-TC generator block 19161020d18SBartlomiej Zolnierkiewicz * 0 < reference_voltage <= 31 192498d22f6SAmit Daniel Kachhap * @regulator: pointer to the TMU regulator structure. 193cebe7373SAmit Daniel Kachhap * @reg_conf: pointer to structure to register with core thermal. 1943a3a5f15SKrzysztof Kozlowski * @ntrip: number of supported trip points. 1950eb875d8SMarek Szyprowski * @enabled: current status of TMU device 19672d1100bSBartlomiej Zolnierkiewicz * @tmu_initialize: SoC specific TMU initialization method 19737f9034fSBartlomiej Zolnierkiewicz * @tmu_control: SoC specific TMU control method 198b79985caSBartlomiej Zolnierkiewicz * @tmu_read: SoC specific TMU temperature read method 199285d994aSBartlomiej Zolnierkiewicz * @tmu_set_emulation: SoC specific TMU emulation setting method 200a7331f72SBartlomiej Zolnierkiewicz * @tmu_clear_irqs: SoC specific TMU interrupts clearing method 201cebe7373SAmit Daniel Kachhap */ 20259dfa54cSAmit Daniel Kachhap struct exynos_tmu_data { 203cebe7373SAmit Daniel Kachhap int id; 20459dfa54cSAmit Daniel Kachhap void __iomem *base; 2059025d563SNaveen Krishna Chatradhi void __iomem *base_second; 20659dfa54cSAmit Daniel Kachhap int irq; 20759dfa54cSAmit Daniel Kachhap enum soc_type soc; 20859dfa54cSAmit Daniel Kachhap struct work_struct irq_work; 20959dfa54cSAmit Daniel Kachhap struct mutex lock; 2106c247393SAbhilash Kesavan struct clk *clk, *clk_sec, *sclk; 211199b3e3cSBartlomiej Zolnierkiewicz u32 cal_type; 212e3ed3649SBartlomiej Zolnierkiewicz u32 efuse_value; 213e3ed3649SBartlomiej Zolnierkiewicz u32 min_efuse_value; 214e3ed3649SBartlomiej Zolnierkiewicz u32 max_efuse_value; 2156c247393SAbhilash Kesavan u16 temp_error1, temp_error2; 216fccfe099SBartlomiej Zolnierkiewicz u8 gain; 21761020d18SBartlomiej Zolnierkiewicz u8 reference_voltage; 218498d22f6SAmit Daniel Kachhap struct regulator *regulator; 2193b6a1a80SLukasz Majewski struct thermal_zone_device *tzd; 2203a3a5f15SKrzysztof Kozlowski unsigned int ntrip; 2210eb875d8SMarek Szyprowski bool enabled; 2223b6a1a80SLukasz Majewski 223c8f8f768SBartlomiej Zolnierkiewicz void (*tmu_set_trip_temp)(struct exynos_tmu_data *data, int trip, 224c8f8f768SBartlomiej Zolnierkiewicz u8 temp); 225c8f8f768SBartlomiej Zolnierkiewicz void (*tmu_set_trip_hyst)(struct exynos_tmu_data *data, int trip, 226c8f8f768SBartlomiej Zolnierkiewicz u8 temp, u8 hyst); 227c35268f5SBartlomiej Zolnierkiewicz void (*tmu_initialize)(struct platform_device *pdev); 22837f9034fSBartlomiej Zolnierkiewicz void (*tmu_control)(struct platform_device *pdev, bool on); 229b79985caSBartlomiej Zolnierkiewicz int (*tmu_read)(struct exynos_tmu_data *data); 23017e8351aSSascha Hauer void (*tmu_set_emulation)(struct exynos_tmu_data *data, int temp); 231a7331f72SBartlomiej Zolnierkiewicz void (*tmu_clear_irqs)(struct exynos_tmu_data *data); 23259dfa54cSAmit Daniel Kachhap }; 23359dfa54cSAmit Daniel Kachhap 2343b6a1a80SLukasz Majewski static void exynos_report_trigger(struct exynos_tmu_data *p) 2353b6a1a80SLukasz Majewski { 2363b6a1a80SLukasz Majewski char data[10], *envp[] = { data, NULL }; 2373b6a1a80SLukasz Majewski struct thermal_zone_device *tz = p->tzd; 23817e8351aSSascha Hauer int temp; 2393b6a1a80SLukasz Majewski unsigned int i; 2403b6a1a80SLukasz Majewski 241eccb6014SLukasz Majewski if (!tz) { 242eccb6014SLukasz Majewski pr_err("No thermal zone device defined\n"); 2433b6a1a80SLukasz Majewski return; 2443b6a1a80SLukasz Majewski } 2453b6a1a80SLukasz Majewski 2460e70f466SSrinivas Pandruvada thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); 2473b6a1a80SLukasz Majewski 2483b6a1a80SLukasz Majewski mutex_lock(&tz->lock); 2493b6a1a80SLukasz Majewski /* Find the level for which trip happened */ 2503b6a1a80SLukasz Majewski for (i = 0; i < of_thermal_get_ntrips(tz); i++) { 2513b6a1a80SLukasz Majewski tz->ops->get_trip_temp(tz, i, &temp); 2523b6a1a80SLukasz Majewski if (tz->last_temperature < temp) 2533b6a1a80SLukasz Majewski break; 2543b6a1a80SLukasz Majewski } 2553b6a1a80SLukasz Majewski 2563b6a1a80SLukasz Majewski snprintf(data, sizeof(data), "%u", i); 2573b6a1a80SLukasz Majewski kobject_uevent_env(&tz->device.kobj, KOBJ_CHANGE, envp); 2583b6a1a80SLukasz Majewski mutex_unlock(&tz->lock); 2593b6a1a80SLukasz Majewski } 2603b6a1a80SLukasz Majewski 26159dfa54cSAmit Daniel Kachhap /* 26259dfa54cSAmit Daniel Kachhap * TMU treats temperature as a mapped temperature code. 26359dfa54cSAmit Daniel Kachhap * The temperature is converted differently depending on the calibration type. 26459dfa54cSAmit Daniel Kachhap */ 26559dfa54cSAmit Daniel Kachhap static int temp_to_code(struct exynos_tmu_data *data, u8 temp) 26659dfa54cSAmit Daniel Kachhap { 267199b3e3cSBartlomiej Zolnierkiewicz if (data->cal_type == TYPE_ONE_POINT_TRIMMING) 268718b4ca1SBartlomiej Zolnierkiewicz return temp + data->temp_error1 - EXYNOS_FIRST_POINT_TRIM; 2699c933b1bSBartlomiej Zolnierkiewicz 270718b4ca1SBartlomiej Zolnierkiewicz return (temp - EXYNOS_FIRST_POINT_TRIM) * 27159dfa54cSAmit Daniel Kachhap (data->temp_error2 - data->temp_error1) / 272718b4ca1SBartlomiej Zolnierkiewicz (EXYNOS_SECOND_POINT_TRIM - EXYNOS_FIRST_POINT_TRIM) + 273bb34b4c8SAmit Daniel Kachhap data->temp_error1; 27459dfa54cSAmit Daniel Kachhap } 27559dfa54cSAmit Daniel Kachhap 27659dfa54cSAmit Daniel Kachhap /* 27759dfa54cSAmit Daniel Kachhap * Calculate a temperature value from a temperature code. 27859dfa54cSAmit Daniel Kachhap * The unit of the temperature is degree Celsius. 27959dfa54cSAmit Daniel Kachhap */ 2806c247393SAbhilash Kesavan static int code_to_temp(struct exynos_tmu_data *data, u16 temp_code) 28159dfa54cSAmit Daniel Kachhap { 282199b3e3cSBartlomiej Zolnierkiewicz if (data->cal_type == TYPE_ONE_POINT_TRIMMING) 283718b4ca1SBartlomiej Zolnierkiewicz return temp_code - data->temp_error1 + EXYNOS_FIRST_POINT_TRIM; 2849c933b1bSBartlomiej Zolnierkiewicz 2859c933b1bSBartlomiej Zolnierkiewicz return (temp_code - data->temp_error1) * 286718b4ca1SBartlomiej Zolnierkiewicz (EXYNOS_SECOND_POINT_TRIM - EXYNOS_FIRST_POINT_TRIM) / 287bb34b4c8SAmit Daniel Kachhap (data->temp_error2 - data->temp_error1) + 288718b4ca1SBartlomiej Zolnierkiewicz EXYNOS_FIRST_POINT_TRIM; 28959dfa54cSAmit Daniel Kachhap } 29059dfa54cSAmit Daniel Kachhap 2918328a4b1SBartlomiej Zolnierkiewicz static void sanitize_temp_error(struct exynos_tmu_data *data, u32 trim_info) 292b835ced1SBartlomiej Zolnierkiewicz { 293aef27b65SBartlomiej Zolnierkiewicz u16 tmu_temp_mask = 294aef27b65SBartlomiej Zolnierkiewicz (data->soc == SOC_ARCH_EXYNOS7) ? EXYNOS7_TMU_TEMP_MASK 295aef27b65SBartlomiej Zolnierkiewicz : EXYNOS_TMU_TEMP_MASK; 296aef27b65SBartlomiej Zolnierkiewicz 297aef27b65SBartlomiej Zolnierkiewicz data->temp_error1 = trim_info & tmu_temp_mask; 29899d67fb9SBartlomiej Zolnierkiewicz data->temp_error2 = ((trim_info >> EXYNOS_TRIMINFO_85_SHIFT) & 299b8d582b9SAmit Daniel Kachhap EXYNOS_TMU_TEMP_MASK); 30059dfa54cSAmit Daniel Kachhap 3015000806cSAmit Daniel Kachhap if (!data->temp_error1 || 302e3ed3649SBartlomiej Zolnierkiewicz (data->min_efuse_value > data->temp_error1) || 303e3ed3649SBartlomiej Zolnierkiewicz (data->temp_error1 > data->max_efuse_value)) 304e3ed3649SBartlomiej Zolnierkiewicz data->temp_error1 = data->efuse_value & EXYNOS_TMU_TEMP_MASK; 3055000806cSAmit Daniel Kachhap 3065000806cSAmit Daniel Kachhap if (!data->temp_error2) 3075000806cSAmit Daniel Kachhap data->temp_error2 = 308e3ed3649SBartlomiej Zolnierkiewicz (data->efuse_value >> EXYNOS_TRIMINFO_85_SHIFT) & 3095000806cSAmit Daniel Kachhap EXYNOS_TMU_TEMP_MASK; 3108328a4b1SBartlomiej Zolnierkiewicz } 31159dfa54cSAmit Daniel Kachhap 31259dfa54cSAmit Daniel Kachhap static int exynos_tmu_initialize(struct platform_device *pdev) 31359dfa54cSAmit Daniel Kachhap { 31459dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = platform_get_drvdata(pdev); 31575e0f100SBartlomiej Zolnierkiewicz struct thermal_zone_device *tzd = data->tzd; 31675e0f100SBartlomiej Zolnierkiewicz const struct thermal_trip * const trips = 31775e0f100SBartlomiej Zolnierkiewicz of_thermal_get_trip_points(tzd); 31897b3881bSBartlomiej Zolnierkiewicz unsigned int status; 319c8f8f768SBartlomiej Zolnierkiewicz int ret = 0, temp, hyst; 3207ca04e58SAmit Daniel Kachhap 32175e0f100SBartlomiej Zolnierkiewicz if (!trips) { 32275e0f100SBartlomiej Zolnierkiewicz dev_err(&pdev->dev, 32375e0f100SBartlomiej Zolnierkiewicz "Cannot get trip points from device tree!\n"); 32475e0f100SBartlomiej Zolnierkiewicz return -ENODEV; 32575e0f100SBartlomiej Zolnierkiewicz } 32675e0f100SBartlomiej Zolnierkiewicz 3278f1c404bSBartlomiej Zolnierkiewicz if (data->soc != SOC_ARCH_EXYNOS5433) /* FIXME */ 3288f1c404bSBartlomiej Zolnierkiewicz ret = tzd->ops->get_crit_temp(tzd, &temp); 3298f1c404bSBartlomiej Zolnierkiewicz if (ret) { 3308f1c404bSBartlomiej Zolnierkiewicz dev_err(&pdev->dev, 3318f1c404bSBartlomiej Zolnierkiewicz "No CRITICAL trip point defined in device tree!\n"); 3328f1c404bSBartlomiej Zolnierkiewicz goto out; 3338f1c404bSBartlomiej Zolnierkiewicz } 3348f1c404bSBartlomiej Zolnierkiewicz 33575e0f100SBartlomiej Zolnierkiewicz if (of_thermal_get_ntrips(tzd) > data->ntrip) { 3363a3a5f15SKrzysztof Kozlowski dev_info(&pdev->dev, 3373a3a5f15SKrzysztof Kozlowski "More trip points than supported by this TMU.\n"); 3383a3a5f15SKrzysztof Kozlowski dev_info(&pdev->dev, 3393a3a5f15SKrzysztof Kozlowski "%d trip points should be configured in polling mode.\n", 34075e0f100SBartlomiej Zolnierkiewicz (of_thermal_get_ntrips(tzd) - data->ntrip)); 3413a3a5f15SKrzysztof Kozlowski } 3423a3a5f15SKrzysztof Kozlowski 34359dfa54cSAmit Daniel Kachhap mutex_lock(&data->lock); 34459dfa54cSAmit Daniel Kachhap clk_enable(data->clk); 34559dfa54cSAmit Daniel Kachhap if (!IS_ERR(data->clk_sec)) 34659dfa54cSAmit Daniel Kachhap clk_enable(data->clk_sec); 34797b3881bSBartlomiej Zolnierkiewicz 34897b3881bSBartlomiej Zolnierkiewicz status = readb(data->base + EXYNOS_TMU_REG_STATUS); 349fac36bacSBartlomiej Zolnierkiewicz if (!status) { 35097b3881bSBartlomiej Zolnierkiewicz ret = -EBUSY; 351fac36bacSBartlomiej Zolnierkiewicz } else { 352c8f8f768SBartlomiej Zolnierkiewicz int i, ntrips = 353c8f8f768SBartlomiej Zolnierkiewicz min_t(int, of_thermal_get_ntrips(tzd), data->ntrip); 354c8f8f768SBartlomiej Zolnierkiewicz 355c35268f5SBartlomiej Zolnierkiewicz data->tmu_initialize(pdev); 356c8f8f768SBartlomiej Zolnierkiewicz 357c8f8f768SBartlomiej Zolnierkiewicz /* Write temperature code for rising and falling threshold */ 358c8f8f768SBartlomiej Zolnierkiewicz for (i = 0; i < ntrips; i++) { 359c8f8f768SBartlomiej Zolnierkiewicz /* Write temperature code for rising threshold */ 360*89335c20SBartlomiej Zolnierkiewicz ret = tzd->ops->get_trip_temp(tzd, i, &temp); 361*89335c20SBartlomiej Zolnierkiewicz if (ret) 362*89335c20SBartlomiej Zolnierkiewicz goto err; 363c8f8f768SBartlomiej Zolnierkiewicz temp /= MCELSIUS; 364c8f8f768SBartlomiej Zolnierkiewicz data->tmu_set_trip_temp(data, i, temp); 365c8f8f768SBartlomiej Zolnierkiewicz 366c8f8f768SBartlomiej Zolnierkiewicz /* Write temperature code for falling threshold */ 367*89335c20SBartlomiej Zolnierkiewicz ret = tzd->ops->get_trip_hyst(tzd, i, &hyst); 368*89335c20SBartlomiej Zolnierkiewicz if (ret) 369*89335c20SBartlomiej Zolnierkiewicz goto err; 370c8f8f768SBartlomiej Zolnierkiewicz hyst /= MCELSIUS; 371c8f8f768SBartlomiej Zolnierkiewicz data->tmu_set_trip_hyst(data, i, temp, hyst); 372c8f8f768SBartlomiej Zolnierkiewicz } 373c8f8f768SBartlomiej Zolnierkiewicz 374fac36bacSBartlomiej Zolnierkiewicz data->tmu_clear_irqs(data); 375fac36bacSBartlomiej Zolnierkiewicz } 376*89335c20SBartlomiej Zolnierkiewicz err: 37759dfa54cSAmit Daniel Kachhap clk_disable(data->clk); 37859dfa54cSAmit Daniel Kachhap mutex_unlock(&data->lock); 37914a11dc7SNaveen Krishna Chatradhi if (!IS_ERR(data->clk_sec)) 38014a11dc7SNaveen Krishna Chatradhi clk_disable(data->clk_sec); 3818f1c404bSBartlomiej Zolnierkiewicz out: 38259dfa54cSAmit Daniel Kachhap return ret; 38359dfa54cSAmit Daniel Kachhap } 38459dfa54cSAmit Daniel Kachhap 385d00671c3SBartlomiej Zolnierkiewicz static u32 get_con_reg(struct exynos_tmu_data *data, u32 con) 38659dfa54cSAmit Daniel Kachhap { 3877575983cSBartlomiej Zolnierkiewicz if (data->soc == SOC_ARCH_EXYNOS4412 || 3887575983cSBartlomiej Zolnierkiewicz data->soc == SOC_ARCH_EXYNOS3250) 3897575983cSBartlomiej Zolnierkiewicz con |= (EXYNOS4412_MUX_ADDR_VALUE << EXYNOS4412_MUX_ADDR_SHIFT); 39086f5362eSLukasz Majewski 39199d67fb9SBartlomiej Zolnierkiewicz con &= ~(EXYNOS_TMU_REF_VOLTAGE_MASK << EXYNOS_TMU_REF_VOLTAGE_SHIFT); 39261020d18SBartlomiej Zolnierkiewicz con |= data->reference_voltage << EXYNOS_TMU_REF_VOLTAGE_SHIFT; 393d0a0ce3eSAmit Daniel Kachhap 39499d67fb9SBartlomiej Zolnierkiewicz con &= ~(EXYNOS_TMU_BUF_SLOPE_SEL_MASK << EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT); 395fccfe099SBartlomiej Zolnierkiewicz con |= (data->gain << EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT); 396d0a0ce3eSAmit Daniel Kachhap 397b9504a6aSBartlomiej Zolnierkiewicz con &= ~(EXYNOS_TMU_TRIP_MODE_MASK << EXYNOS_TMU_TRIP_MODE_SHIFT); 39809d29426SBartlomiej Zolnierkiewicz con |= (EXYNOS_NOISE_CANCEL_MODE << EXYNOS_TMU_TRIP_MODE_SHIFT); 39959dfa54cSAmit Daniel Kachhap 400d00671c3SBartlomiej Zolnierkiewicz return con; 401d00671c3SBartlomiej Zolnierkiewicz } 402d00671c3SBartlomiej Zolnierkiewicz 403d00671c3SBartlomiej Zolnierkiewicz static void exynos_tmu_control(struct platform_device *pdev, bool on) 404d00671c3SBartlomiej Zolnierkiewicz { 405d00671c3SBartlomiej Zolnierkiewicz struct exynos_tmu_data *data = platform_get_drvdata(pdev); 406d00671c3SBartlomiej Zolnierkiewicz 407d00671c3SBartlomiej Zolnierkiewicz mutex_lock(&data->lock); 408d00671c3SBartlomiej Zolnierkiewicz clk_enable(data->clk); 40937f9034fSBartlomiej Zolnierkiewicz data->tmu_control(pdev, on); 4100eb875d8SMarek Szyprowski data->enabled = on; 41159dfa54cSAmit Daniel Kachhap clk_disable(data->clk); 41259dfa54cSAmit Daniel Kachhap mutex_unlock(&data->lock); 41359dfa54cSAmit Daniel Kachhap } 41459dfa54cSAmit Daniel Kachhap 415a503a10fSBartlomiej Zolnierkiewicz static void exynos4210_tmu_set_trip_temp(struct exynos_tmu_data *data, 416a503a10fSBartlomiej Zolnierkiewicz int trip, u8 temp) 417a503a10fSBartlomiej Zolnierkiewicz { 418a503a10fSBartlomiej Zolnierkiewicz const struct thermal_trip * const trips = 419a503a10fSBartlomiej Zolnierkiewicz of_thermal_get_trip_points(data->tzd); 420a503a10fSBartlomiej Zolnierkiewicz u8 ref, th_code; 421a503a10fSBartlomiej Zolnierkiewicz 422a503a10fSBartlomiej Zolnierkiewicz ref = trips[0].temperature / MCELSIUS; 423a503a10fSBartlomiej Zolnierkiewicz 424a503a10fSBartlomiej Zolnierkiewicz if (trip == 0) { 425a503a10fSBartlomiej Zolnierkiewicz th_code = temp_to_code(data, ref); 426a503a10fSBartlomiej Zolnierkiewicz writeb(th_code, data->base + EXYNOS4210_TMU_REG_THRESHOLD_TEMP); 427a503a10fSBartlomiej Zolnierkiewicz } 428a503a10fSBartlomiej Zolnierkiewicz 429a503a10fSBartlomiej Zolnierkiewicz temp -= ref; 430a503a10fSBartlomiej Zolnierkiewicz writeb(temp, data->base + EXYNOS4210_TMU_REG_TRIG_LEVEL0 + trip * 4); 431a503a10fSBartlomiej Zolnierkiewicz } 432a503a10fSBartlomiej Zolnierkiewicz 433c8f8f768SBartlomiej Zolnierkiewicz /* failing thresholds are not supported on Exynos4210 */ 434c8f8f768SBartlomiej Zolnierkiewicz static void exynos4210_tmu_set_trip_hyst(struct exynos_tmu_data *data, 435c8f8f768SBartlomiej Zolnierkiewicz int trip, u8 temp, u8 hyst) 436c8f8f768SBartlomiej Zolnierkiewicz { 437c8f8f768SBartlomiej Zolnierkiewicz } 438c8f8f768SBartlomiej Zolnierkiewicz 439c35268f5SBartlomiej Zolnierkiewicz static void exynos4210_tmu_initialize(struct platform_device *pdev) 44072d1100bSBartlomiej Zolnierkiewicz { 44172d1100bSBartlomiej Zolnierkiewicz struct exynos_tmu_data *data = platform_get_drvdata(pdev); 44272d1100bSBartlomiej Zolnierkiewicz 44372d1100bSBartlomiej Zolnierkiewicz sanitize_temp_error(data, readl(data->base + EXYNOS_TMU_REG_TRIMINFO)); 44472d1100bSBartlomiej Zolnierkiewicz } 44572d1100bSBartlomiej Zolnierkiewicz 446a503a10fSBartlomiej Zolnierkiewicz static void exynos4412_tmu_set_trip_temp(struct exynos_tmu_data *data, 447a503a10fSBartlomiej Zolnierkiewicz int trip, u8 temp) 448a503a10fSBartlomiej Zolnierkiewicz { 449a503a10fSBartlomiej Zolnierkiewicz u32 th, con; 450a503a10fSBartlomiej Zolnierkiewicz 451a503a10fSBartlomiej Zolnierkiewicz th = readl(data->base + EXYNOS_THD_TEMP_RISE); 452a503a10fSBartlomiej Zolnierkiewicz th &= ~(0xff << 8 * trip); 453a503a10fSBartlomiej Zolnierkiewicz th |= temp_to_code(data, temp) << 8 * trip; 454a503a10fSBartlomiej Zolnierkiewicz writel(th, data->base + EXYNOS_THD_TEMP_RISE); 455a503a10fSBartlomiej Zolnierkiewicz 456a503a10fSBartlomiej Zolnierkiewicz if (trip == 3) { 457a503a10fSBartlomiej Zolnierkiewicz con = readl(data->base + EXYNOS_TMU_REG_CONTROL); 458a503a10fSBartlomiej Zolnierkiewicz con |= (1 << EXYNOS_TMU_THERM_TRIP_EN_SHIFT); 459a503a10fSBartlomiej Zolnierkiewicz writel(con, data->base + EXYNOS_TMU_REG_CONTROL); 460a503a10fSBartlomiej Zolnierkiewicz } 461a503a10fSBartlomiej Zolnierkiewicz } 462a503a10fSBartlomiej Zolnierkiewicz 463a503a10fSBartlomiej Zolnierkiewicz static void exynos4412_tmu_set_trip_hyst(struct exynos_tmu_data *data, 464a503a10fSBartlomiej Zolnierkiewicz int trip, u8 temp, u8 hyst) 465a503a10fSBartlomiej Zolnierkiewicz { 466a503a10fSBartlomiej Zolnierkiewicz u32 th; 467a503a10fSBartlomiej Zolnierkiewicz 468a503a10fSBartlomiej Zolnierkiewicz th = readl(data->base + EXYNOS_THD_TEMP_FALL); 469a503a10fSBartlomiej Zolnierkiewicz th &= ~(0xff << 8 * trip); 470a503a10fSBartlomiej Zolnierkiewicz if (hyst) 471a503a10fSBartlomiej Zolnierkiewicz th |= temp_to_code(data, temp - hyst) << 8 * trip; 472a503a10fSBartlomiej Zolnierkiewicz writel(th, data->base + EXYNOS_THD_TEMP_FALL); 473a503a10fSBartlomiej Zolnierkiewicz } 474a503a10fSBartlomiej Zolnierkiewicz 475c35268f5SBartlomiej Zolnierkiewicz static void exynos4412_tmu_initialize(struct platform_device *pdev) 47672d1100bSBartlomiej Zolnierkiewicz { 47772d1100bSBartlomiej Zolnierkiewicz struct exynos_tmu_data *data = platform_get_drvdata(pdev); 478a503a10fSBartlomiej Zolnierkiewicz unsigned int trim_info, ctrl; 47972d1100bSBartlomiej Zolnierkiewicz 48072d1100bSBartlomiej Zolnierkiewicz if (data->soc == SOC_ARCH_EXYNOS3250 || 48172d1100bSBartlomiej Zolnierkiewicz data->soc == SOC_ARCH_EXYNOS4412 || 48272d1100bSBartlomiej Zolnierkiewicz data->soc == SOC_ARCH_EXYNOS5250) { 48372d1100bSBartlomiej Zolnierkiewicz if (data->soc == SOC_ARCH_EXYNOS3250) { 48472d1100bSBartlomiej Zolnierkiewicz ctrl = readl(data->base + EXYNOS_TMU_TRIMINFO_CON1); 48572d1100bSBartlomiej Zolnierkiewicz ctrl |= EXYNOS_TRIMINFO_RELOAD_ENABLE; 48672d1100bSBartlomiej Zolnierkiewicz writel(ctrl, data->base + EXYNOS_TMU_TRIMINFO_CON1); 48772d1100bSBartlomiej Zolnierkiewicz } 48872d1100bSBartlomiej Zolnierkiewicz ctrl = readl(data->base + EXYNOS_TMU_TRIMINFO_CON2); 48972d1100bSBartlomiej Zolnierkiewicz ctrl |= EXYNOS_TRIMINFO_RELOAD_ENABLE; 49072d1100bSBartlomiej Zolnierkiewicz writel(ctrl, data->base + EXYNOS_TMU_TRIMINFO_CON2); 49172d1100bSBartlomiej Zolnierkiewicz } 49272d1100bSBartlomiej Zolnierkiewicz 49372d1100bSBartlomiej Zolnierkiewicz /* On exynos5420 the triminfo register is in the shared space */ 49472d1100bSBartlomiej Zolnierkiewicz if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO) 49572d1100bSBartlomiej Zolnierkiewicz trim_info = readl(data->base_second + EXYNOS_TMU_REG_TRIMINFO); 49672d1100bSBartlomiej Zolnierkiewicz else 49772d1100bSBartlomiej Zolnierkiewicz trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO); 49872d1100bSBartlomiej Zolnierkiewicz 49972d1100bSBartlomiej Zolnierkiewicz sanitize_temp_error(data, trim_info); 5003b6a1a80SLukasz Majewski } 5013b6a1a80SLukasz Majewski 502a503a10fSBartlomiej Zolnierkiewicz static void exynos5433_tmu_set_trip_temp(struct exynos_tmu_data *data, 503a503a10fSBartlomiej Zolnierkiewicz int trip, u8 temp) 504a503a10fSBartlomiej Zolnierkiewicz { 505a503a10fSBartlomiej Zolnierkiewicz unsigned int reg_off, j; 506a503a10fSBartlomiej Zolnierkiewicz u32 th; 507a503a10fSBartlomiej Zolnierkiewicz 508a503a10fSBartlomiej Zolnierkiewicz if (trip > 3) { 509a503a10fSBartlomiej Zolnierkiewicz reg_off = EXYNOS5433_THD_TEMP_RISE7_4; 510a503a10fSBartlomiej Zolnierkiewicz j = trip - 4; 511a503a10fSBartlomiej Zolnierkiewicz } else { 512a503a10fSBartlomiej Zolnierkiewicz reg_off = EXYNOS5433_THD_TEMP_RISE3_0; 513a503a10fSBartlomiej Zolnierkiewicz j = trip; 514a503a10fSBartlomiej Zolnierkiewicz } 515a503a10fSBartlomiej Zolnierkiewicz 516a503a10fSBartlomiej Zolnierkiewicz th = readl(data->base + reg_off); 517a503a10fSBartlomiej Zolnierkiewicz th &= ~(0xff << j * 8); 518a503a10fSBartlomiej Zolnierkiewicz th |= (temp_to_code(data, temp) << j * 8); 519a503a10fSBartlomiej Zolnierkiewicz writel(th, data->base + reg_off); 520a503a10fSBartlomiej Zolnierkiewicz } 521a503a10fSBartlomiej Zolnierkiewicz 522a503a10fSBartlomiej Zolnierkiewicz static void exynos5433_tmu_set_trip_hyst(struct exynos_tmu_data *data, 523a503a10fSBartlomiej Zolnierkiewicz int trip, u8 temp, u8 hyst) 524a503a10fSBartlomiej Zolnierkiewicz { 525a503a10fSBartlomiej Zolnierkiewicz unsigned int reg_off, j; 526a503a10fSBartlomiej Zolnierkiewicz u32 th; 527a503a10fSBartlomiej Zolnierkiewicz 528a503a10fSBartlomiej Zolnierkiewicz if (trip > 3) { 529a503a10fSBartlomiej Zolnierkiewicz reg_off = EXYNOS5433_THD_TEMP_FALL7_4; 530a503a10fSBartlomiej Zolnierkiewicz j = trip - 4; 531a503a10fSBartlomiej Zolnierkiewicz } else { 532a503a10fSBartlomiej Zolnierkiewicz reg_off = EXYNOS5433_THD_TEMP_FALL3_0; 533a503a10fSBartlomiej Zolnierkiewicz j = trip; 534a503a10fSBartlomiej Zolnierkiewicz } 535a503a10fSBartlomiej Zolnierkiewicz 536a503a10fSBartlomiej Zolnierkiewicz th = readl(data->base + reg_off); 537a503a10fSBartlomiej Zolnierkiewicz th &= ~(0xff << j * 8); 538a503a10fSBartlomiej Zolnierkiewicz th |= (temp_to_code(data, temp - hyst) << j * 8); 539a503a10fSBartlomiej Zolnierkiewicz writel(th, data->base + reg_off); 54072d1100bSBartlomiej Zolnierkiewicz } 54172d1100bSBartlomiej Zolnierkiewicz 542c35268f5SBartlomiej Zolnierkiewicz static void exynos5433_tmu_initialize(struct platform_device *pdev) 543488c7455SChanwoo Choi { 544488c7455SChanwoo Choi struct exynos_tmu_data *data = platform_get_drvdata(pdev); 54597b3881bSBartlomiej Zolnierkiewicz unsigned int trim_info; 546c8f8f768SBartlomiej Zolnierkiewicz int sensor_id, cal_type; 547488c7455SChanwoo Choi 548488c7455SChanwoo Choi trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO); 549488c7455SChanwoo Choi sanitize_temp_error(data, trim_info); 550488c7455SChanwoo Choi 551488c7455SChanwoo Choi /* Read the temperature sensor id */ 552488c7455SChanwoo Choi sensor_id = (trim_info & EXYNOS5433_TRIMINFO_SENSOR_ID_MASK) 553488c7455SChanwoo Choi >> EXYNOS5433_TRIMINFO_SENSOR_ID_SHIFT; 554488c7455SChanwoo Choi dev_info(&pdev->dev, "Temperature sensor ID: 0x%x\n", sensor_id); 555488c7455SChanwoo Choi 556488c7455SChanwoo Choi /* Read the calibration mode */ 557488c7455SChanwoo Choi writel(trim_info, data->base + EXYNOS_TMU_REG_TRIMINFO); 558488c7455SChanwoo Choi cal_type = (trim_info & EXYNOS5433_TRIMINFO_CALIB_SEL_MASK) 559488c7455SChanwoo Choi >> EXYNOS5433_TRIMINFO_CALIB_SEL_SHIFT; 560488c7455SChanwoo Choi 561488c7455SChanwoo Choi switch (cal_type) { 562488c7455SChanwoo Choi case EXYNOS5433_TRIMINFO_TWO_POINT_TRIMMING: 563199b3e3cSBartlomiej Zolnierkiewicz data->cal_type = TYPE_TWO_POINT_TRIMMING; 564488c7455SChanwoo Choi break; 565199b3e3cSBartlomiej Zolnierkiewicz case EXYNOS5433_TRIMINFO_ONE_POINT_TRIMMING: 566488c7455SChanwoo Choi default: 567199b3e3cSBartlomiej Zolnierkiewicz data->cal_type = TYPE_ONE_POINT_TRIMMING; 568488c7455SChanwoo Choi break; 569baba1ebbSKrzysztof Kozlowski } 570488c7455SChanwoo Choi 571488c7455SChanwoo Choi dev_info(&pdev->dev, "Calibration type is %d-point calibration\n", 572488c7455SChanwoo Choi cal_type ? 2 : 1); 573488c7455SChanwoo Choi } 574488c7455SChanwoo Choi 575a503a10fSBartlomiej Zolnierkiewicz static void exynos7_tmu_set_trip_temp(struct exynos_tmu_data *data, 576a503a10fSBartlomiej Zolnierkiewicz int trip, u8 temp) 577a503a10fSBartlomiej Zolnierkiewicz { 578a503a10fSBartlomiej Zolnierkiewicz unsigned int reg_off, bit_off; 579a503a10fSBartlomiej Zolnierkiewicz u32 th; 580a503a10fSBartlomiej Zolnierkiewicz 581a503a10fSBartlomiej Zolnierkiewicz reg_off = ((7 - trip) / 2) * 4; 582a503a10fSBartlomiej Zolnierkiewicz bit_off = ((8 - trip) % 2); 583a503a10fSBartlomiej Zolnierkiewicz 584a503a10fSBartlomiej Zolnierkiewicz th = readl(data->base + EXYNOS7_THD_TEMP_RISE7_6 + reg_off); 585a503a10fSBartlomiej Zolnierkiewicz th &= ~(EXYNOS7_TMU_TEMP_MASK << (16 * bit_off)); 586a503a10fSBartlomiej Zolnierkiewicz th |= temp_to_code(data, temp) << (16 * bit_off); 587a503a10fSBartlomiej Zolnierkiewicz writel(th, data->base + EXYNOS7_THD_TEMP_RISE7_6 + reg_off); 588a503a10fSBartlomiej Zolnierkiewicz } 589a503a10fSBartlomiej Zolnierkiewicz 590a503a10fSBartlomiej Zolnierkiewicz static void exynos7_tmu_set_trip_hyst(struct exynos_tmu_data *data, 591a503a10fSBartlomiej Zolnierkiewicz int trip, u8 temp, u8 hyst) 592a503a10fSBartlomiej Zolnierkiewicz { 593a503a10fSBartlomiej Zolnierkiewicz unsigned int reg_off, bit_off; 594a503a10fSBartlomiej Zolnierkiewicz u32 th; 595a503a10fSBartlomiej Zolnierkiewicz 596a503a10fSBartlomiej Zolnierkiewicz reg_off = ((7 - trip) / 2) * 4; 597a503a10fSBartlomiej Zolnierkiewicz bit_off = ((8 - trip) % 2); 598a503a10fSBartlomiej Zolnierkiewicz 599a503a10fSBartlomiej Zolnierkiewicz th = readl(data->base + EXYNOS7_THD_TEMP_FALL7_6 + reg_off); 600a503a10fSBartlomiej Zolnierkiewicz th &= ~(EXYNOS7_TMU_TEMP_MASK << (16 * bit_off)); 601a503a10fSBartlomiej Zolnierkiewicz th |= temp_to_code(data, temp - hyst) << (16 * bit_off); 602a503a10fSBartlomiej Zolnierkiewicz writel(th, data->base + EXYNOS7_THD_TEMP_FALL7_6 + reg_off); 603a503a10fSBartlomiej Zolnierkiewicz } 604a503a10fSBartlomiej Zolnierkiewicz 605c35268f5SBartlomiej Zolnierkiewicz static void exynos7_tmu_initialize(struct platform_device *pdev) 6066c247393SAbhilash Kesavan { 6076c247393SAbhilash Kesavan struct exynos_tmu_data *data = platform_get_drvdata(pdev); 60897b3881bSBartlomiej Zolnierkiewicz unsigned int trim_info; 6096c247393SAbhilash Kesavan 6106c247393SAbhilash Kesavan trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO); 611aef27b65SBartlomiej Zolnierkiewicz sanitize_temp_error(data, trim_info); 6126c247393SAbhilash Kesavan } 6136c247393SAbhilash Kesavan 61437f9034fSBartlomiej Zolnierkiewicz static void exynos4210_tmu_control(struct platform_device *pdev, bool on) 61537f9034fSBartlomiej Zolnierkiewicz { 61637f9034fSBartlomiej Zolnierkiewicz struct exynos_tmu_data *data = platform_get_drvdata(pdev); 6173b6a1a80SLukasz Majewski struct thermal_zone_device *tz = data->tzd; 61837f9034fSBartlomiej Zolnierkiewicz unsigned int con, interrupt_en; 61937f9034fSBartlomiej Zolnierkiewicz 62037f9034fSBartlomiej Zolnierkiewicz con = get_con_reg(data, readl(data->base + EXYNOS_TMU_REG_CONTROL)); 62137f9034fSBartlomiej Zolnierkiewicz 62259dfa54cSAmit Daniel Kachhap if (on) { 62359dfa54cSAmit Daniel Kachhap con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT); 62459dfa54cSAmit Daniel Kachhap interrupt_en = 6253b6a1a80SLukasz Majewski (of_thermal_is_trip_valid(tz, 3) 6263b6a1a80SLukasz Majewski << EXYNOS_TMU_INTEN_RISE3_SHIFT) | 6273b6a1a80SLukasz Majewski (of_thermal_is_trip_valid(tz, 2) 6283b6a1a80SLukasz Majewski << EXYNOS_TMU_INTEN_RISE2_SHIFT) | 6293b6a1a80SLukasz Majewski (of_thermal_is_trip_valid(tz, 1) 6303b6a1a80SLukasz Majewski << EXYNOS_TMU_INTEN_RISE1_SHIFT) | 6313b6a1a80SLukasz Majewski (of_thermal_is_trip_valid(tz, 0) 6323b6a1a80SLukasz Majewski << EXYNOS_TMU_INTEN_RISE0_SHIFT); 6333b6a1a80SLukasz Majewski 634e0761533SBartlomiej Zolnierkiewicz if (data->soc != SOC_ARCH_EXYNOS4210) 63559dfa54cSAmit Daniel Kachhap interrupt_en |= 63637f9034fSBartlomiej Zolnierkiewicz interrupt_en << EXYNOS_TMU_INTEN_FALL0_SHIFT; 63759dfa54cSAmit Daniel Kachhap } else { 63859dfa54cSAmit Daniel Kachhap con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT); 63959dfa54cSAmit Daniel Kachhap interrupt_en = 0; /* Disable all interrupts */ 64059dfa54cSAmit Daniel Kachhap } 64137f9034fSBartlomiej Zolnierkiewicz writel(interrupt_en, data->base + EXYNOS_TMU_REG_INTEN); 64237f9034fSBartlomiej Zolnierkiewicz writel(con, data->base + EXYNOS_TMU_REG_CONTROL); 64337f9034fSBartlomiej Zolnierkiewicz } 64459dfa54cSAmit Daniel Kachhap 645488c7455SChanwoo Choi static void exynos5433_tmu_control(struct platform_device *pdev, bool on) 646488c7455SChanwoo Choi { 647488c7455SChanwoo Choi struct exynos_tmu_data *data = platform_get_drvdata(pdev); 648488c7455SChanwoo Choi struct thermal_zone_device *tz = data->tzd; 649488c7455SChanwoo Choi unsigned int con, interrupt_en, pd_det_en; 650488c7455SChanwoo Choi 651488c7455SChanwoo Choi con = get_con_reg(data, readl(data->base + EXYNOS_TMU_REG_CONTROL)); 652488c7455SChanwoo Choi 653488c7455SChanwoo Choi if (on) { 654488c7455SChanwoo Choi con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT); 655488c7455SChanwoo Choi interrupt_en = 656488c7455SChanwoo Choi (of_thermal_is_trip_valid(tz, 7) 657488c7455SChanwoo Choi << EXYNOS7_TMU_INTEN_RISE7_SHIFT) | 658488c7455SChanwoo Choi (of_thermal_is_trip_valid(tz, 6) 659488c7455SChanwoo Choi << EXYNOS7_TMU_INTEN_RISE6_SHIFT) | 660488c7455SChanwoo Choi (of_thermal_is_trip_valid(tz, 5) 661488c7455SChanwoo Choi << EXYNOS7_TMU_INTEN_RISE5_SHIFT) | 662488c7455SChanwoo Choi (of_thermal_is_trip_valid(tz, 4) 663488c7455SChanwoo Choi << EXYNOS7_TMU_INTEN_RISE4_SHIFT) | 664488c7455SChanwoo Choi (of_thermal_is_trip_valid(tz, 3) 665488c7455SChanwoo Choi << EXYNOS7_TMU_INTEN_RISE3_SHIFT) | 666488c7455SChanwoo Choi (of_thermal_is_trip_valid(tz, 2) 667488c7455SChanwoo Choi << EXYNOS7_TMU_INTEN_RISE2_SHIFT) | 668488c7455SChanwoo Choi (of_thermal_is_trip_valid(tz, 1) 669488c7455SChanwoo Choi << EXYNOS7_TMU_INTEN_RISE1_SHIFT) | 670488c7455SChanwoo Choi (of_thermal_is_trip_valid(tz, 0) 671488c7455SChanwoo Choi << EXYNOS7_TMU_INTEN_RISE0_SHIFT); 672488c7455SChanwoo Choi 673488c7455SChanwoo Choi interrupt_en |= 674488c7455SChanwoo Choi interrupt_en << EXYNOS_TMU_INTEN_FALL0_SHIFT; 675488c7455SChanwoo Choi } else { 676488c7455SChanwoo Choi con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT); 677488c7455SChanwoo Choi interrupt_en = 0; /* Disable all interrupts */ 678488c7455SChanwoo Choi } 679488c7455SChanwoo Choi 680488c7455SChanwoo Choi pd_det_en = on ? EXYNOS5433_PD_DET_EN : 0; 681488c7455SChanwoo Choi 682488c7455SChanwoo Choi writel(pd_det_en, data->base + EXYNOS5433_TMU_PD_DET_EN); 683488c7455SChanwoo Choi writel(interrupt_en, data->base + EXYNOS5433_TMU_REG_INTEN); 684488c7455SChanwoo Choi writel(con, data->base + EXYNOS_TMU_REG_CONTROL); 685488c7455SChanwoo Choi } 686488c7455SChanwoo Choi 6876c247393SAbhilash Kesavan static void exynos7_tmu_control(struct platform_device *pdev, bool on) 6886c247393SAbhilash Kesavan { 6896c247393SAbhilash Kesavan struct exynos_tmu_data *data = platform_get_drvdata(pdev); 6906c247393SAbhilash Kesavan struct thermal_zone_device *tz = data->tzd; 6916c247393SAbhilash Kesavan unsigned int con, interrupt_en; 6926c247393SAbhilash Kesavan 6936c247393SAbhilash Kesavan con = get_con_reg(data, readl(data->base + EXYNOS_TMU_REG_CONTROL)); 6946c247393SAbhilash Kesavan 6956c247393SAbhilash Kesavan if (on) { 6966c247393SAbhilash Kesavan con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT); 69742b696e8SChanwoo Choi con |= (1 << EXYNOS7_PD_DET_EN_SHIFT); 6986c247393SAbhilash Kesavan interrupt_en = 6996c247393SAbhilash Kesavan (of_thermal_is_trip_valid(tz, 7) 7006c247393SAbhilash Kesavan << EXYNOS7_TMU_INTEN_RISE7_SHIFT) | 7016c247393SAbhilash Kesavan (of_thermal_is_trip_valid(tz, 6) 7026c247393SAbhilash Kesavan << EXYNOS7_TMU_INTEN_RISE6_SHIFT) | 7036c247393SAbhilash Kesavan (of_thermal_is_trip_valid(tz, 5) 7046c247393SAbhilash Kesavan << EXYNOS7_TMU_INTEN_RISE5_SHIFT) | 7056c247393SAbhilash Kesavan (of_thermal_is_trip_valid(tz, 4) 7066c247393SAbhilash Kesavan << EXYNOS7_TMU_INTEN_RISE4_SHIFT) | 7076c247393SAbhilash Kesavan (of_thermal_is_trip_valid(tz, 3) 7086c247393SAbhilash Kesavan << EXYNOS7_TMU_INTEN_RISE3_SHIFT) | 7096c247393SAbhilash Kesavan (of_thermal_is_trip_valid(tz, 2) 7106c247393SAbhilash Kesavan << EXYNOS7_TMU_INTEN_RISE2_SHIFT) | 7116c247393SAbhilash Kesavan (of_thermal_is_trip_valid(tz, 1) 7126c247393SAbhilash Kesavan << EXYNOS7_TMU_INTEN_RISE1_SHIFT) | 7136c247393SAbhilash Kesavan (of_thermal_is_trip_valid(tz, 0) 7146c247393SAbhilash Kesavan << EXYNOS7_TMU_INTEN_RISE0_SHIFT); 7156c247393SAbhilash Kesavan 7166c247393SAbhilash Kesavan interrupt_en |= 7176c247393SAbhilash Kesavan interrupt_en << EXYNOS_TMU_INTEN_FALL0_SHIFT; 7186c247393SAbhilash Kesavan } else { 7196c247393SAbhilash Kesavan con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT); 72042b696e8SChanwoo Choi con &= ~(1 << EXYNOS7_PD_DET_EN_SHIFT); 7216c247393SAbhilash Kesavan interrupt_en = 0; /* Disable all interrupts */ 7226c247393SAbhilash Kesavan } 7236c247393SAbhilash Kesavan 7246c247393SAbhilash Kesavan writel(interrupt_en, data->base + EXYNOS7_TMU_REG_INTEN); 7256c247393SAbhilash Kesavan writel(con, data->base + EXYNOS_TMU_REG_CONTROL); 7266c247393SAbhilash Kesavan } 7276c247393SAbhilash Kesavan 72817e8351aSSascha Hauer static int exynos_get_temp(void *p, int *temp) 72959dfa54cSAmit Daniel Kachhap { 7303b6a1a80SLukasz Majewski struct exynos_tmu_data *data = p; 73108d725cdSMarek Szyprowski int value, ret = 0; 7323b6a1a80SLukasz Majewski 7330eb875d8SMarek Szyprowski if (!data || !data->tmu_read || !data->enabled) 7343b6a1a80SLukasz Majewski return -EINVAL; 73559dfa54cSAmit Daniel Kachhap 73659dfa54cSAmit Daniel Kachhap mutex_lock(&data->lock); 73759dfa54cSAmit Daniel Kachhap clk_enable(data->clk); 7383b6a1a80SLukasz Majewski 73908d725cdSMarek Szyprowski value = data->tmu_read(data); 74008d725cdSMarek Szyprowski if (value < 0) 74108d725cdSMarek Szyprowski ret = value; 74208d725cdSMarek Szyprowski else 74308d725cdSMarek Szyprowski *temp = code_to_temp(data, value) * MCELSIUS; 7443b6a1a80SLukasz Majewski 74559dfa54cSAmit Daniel Kachhap clk_disable(data->clk); 74659dfa54cSAmit Daniel Kachhap mutex_unlock(&data->lock); 74759dfa54cSAmit Daniel Kachhap 74808d725cdSMarek Szyprowski return ret; 74959dfa54cSAmit Daniel Kachhap } 75059dfa54cSAmit Daniel Kachhap 75159dfa54cSAmit Daniel Kachhap #ifdef CONFIG_THERMAL_EMULATION 752154013eaSBartlomiej Zolnierkiewicz static u32 get_emul_con_reg(struct exynos_tmu_data *data, unsigned int val, 75317e8351aSSascha Hauer int temp) 754154013eaSBartlomiej Zolnierkiewicz { 755154013eaSBartlomiej Zolnierkiewicz if (temp) { 756154013eaSBartlomiej Zolnierkiewicz temp /= MCELSIUS; 757154013eaSBartlomiej Zolnierkiewicz 758154013eaSBartlomiej Zolnierkiewicz val &= ~(EXYNOS_EMUL_TIME_MASK << EXYNOS_EMUL_TIME_SHIFT); 759154013eaSBartlomiej Zolnierkiewicz val |= (EXYNOS_EMUL_TIME << EXYNOS_EMUL_TIME_SHIFT); 7606c247393SAbhilash Kesavan if (data->soc == SOC_ARCH_EXYNOS7) { 7616c247393SAbhilash Kesavan val &= ~(EXYNOS7_EMUL_DATA_MASK << 7626c247393SAbhilash Kesavan EXYNOS7_EMUL_DATA_SHIFT); 7636c247393SAbhilash Kesavan val |= (temp_to_code(data, temp) << 7646c247393SAbhilash Kesavan EXYNOS7_EMUL_DATA_SHIFT) | 765154013eaSBartlomiej Zolnierkiewicz EXYNOS_EMUL_ENABLE; 766154013eaSBartlomiej Zolnierkiewicz } else { 7676c247393SAbhilash Kesavan val &= ~(EXYNOS_EMUL_DATA_MASK << 7686c247393SAbhilash Kesavan EXYNOS_EMUL_DATA_SHIFT); 7696c247393SAbhilash Kesavan val |= (temp_to_code(data, temp) << 7706c247393SAbhilash Kesavan EXYNOS_EMUL_DATA_SHIFT) | 7716c247393SAbhilash Kesavan EXYNOS_EMUL_ENABLE; 7726c247393SAbhilash Kesavan } 7736c247393SAbhilash Kesavan } else { 774154013eaSBartlomiej Zolnierkiewicz val &= ~EXYNOS_EMUL_ENABLE; 775154013eaSBartlomiej Zolnierkiewicz } 776154013eaSBartlomiej Zolnierkiewicz 777154013eaSBartlomiej Zolnierkiewicz return val; 778154013eaSBartlomiej Zolnierkiewicz } 779154013eaSBartlomiej Zolnierkiewicz 780285d994aSBartlomiej Zolnierkiewicz static void exynos4412_tmu_set_emulation(struct exynos_tmu_data *data, 78117e8351aSSascha Hauer int temp) 782285d994aSBartlomiej Zolnierkiewicz { 783285d994aSBartlomiej Zolnierkiewicz unsigned int val; 784285d994aSBartlomiej Zolnierkiewicz u32 emul_con; 785285d994aSBartlomiej Zolnierkiewicz 786285d994aSBartlomiej Zolnierkiewicz if (data->soc == SOC_ARCH_EXYNOS5260) 787285d994aSBartlomiej Zolnierkiewicz emul_con = EXYNOS5260_EMUL_CON; 788b28fec13SSudip Mukherjee else if (data->soc == SOC_ARCH_EXYNOS5433) 789488c7455SChanwoo Choi emul_con = EXYNOS5433_TMU_EMUL_CON; 7906c247393SAbhilash Kesavan else if (data->soc == SOC_ARCH_EXYNOS7) 7916c247393SAbhilash Kesavan emul_con = EXYNOS7_TMU_REG_EMUL_CON; 792285d994aSBartlomiej Zolnierkiewicz else 793285d994aSBartlomiej Zolnierkiewicz emul_con = EXYNOS_EMUL_CON; 794285d994aSBartlomiej Zolnierkiewicz 795285d994aSBartlomiej Zolnierkiewicz val = readl(data->base + emul_con); 796285d994aSBartlomiej Zolnierkiewicz val = get_emul_con_reg(data, val, temp); 797285d994aSBartlomiej Zolnierkiewicz writel(val, data->base + emul_con); 798285d994aSBartlomiej Zolnierkiewicz } 799285d994aSBartlomiej Zolnierkiewicz 80017e8351aSSascha Hauer static int exynos_tmu_set_emulation(void *drv_data, int temp) 80159dfa54cSAmit Daniel Kachhap { 80259dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = drv_data; 80359dfa54cSAmit Daniel Kachhap int ret = -EINVAL; 80459dfa54cSAmit Daniel Kachhap 805ef3f80fcSBartlomiej Zolnierkiewicz if (data->soc == SOC_ARCH_EXYNOS4210) 80659dfa54cSAmit Daniel Kachhap goto out; 80759dfa54cSAmit Daniel Kachhap 80859dfa54cSAmit Daniel Kachhap if (temp && temp < MCELSIUS) 80959dfa54cSAmit Daniel Kachhap goto out; 81059dfa54cSAmit Daniel Kachhap 81159dfa54cSAmit Daniel Kachhap mutex_lock(&data->lock); 81259dfa54cSAmit Daniel Kachhap clk_enable(data->clk); 813285d994aSBartlomiej Zolnierkiewicz data->tmu_set_emulation(data, temp); 81459dfa54cSAmit Daniel Kachhap clk_disable(data->clk); 81559dfa54cSAmit Daniel Kachhap mutex_unlock(&data->lock); 81659dfa54cSAmit Daniel Kachhap return 0; 81759dfa54cSAmit Daniel Kachhap out: 81859dfa54cSAmit Daniel Kachhap return ret; 81959dfa54cSAmit Daniel Kachhap } 82059dfa54cSAmit Daniel Kachhap #else 821285d994aSBartlomiej Zolnierkiewicz #define exynos4412_tmu_set_emulation NULL 82217e8351aSSascha Hauer static int exynos_tmu_set_emulation(void *drv_data, int temp) 82359dfa54cSAmit Daniel Kachhap { return -EINVAL; } 82459dfa54cSAmit Daniel Kachhap #endif /* CONFIG_THERMAL_EMULATION */ 82559dfa54cSAmit Daniel Kachhap 826b79985caSBartlomiej Zolnierkiewicz static int exynos4210_tmu_read(struct exynos_tmu_data *data) 827b79985caSBartlomiej Zolnierkiewicz { 828b79985caSBartlomiej Zolnierkiewicz int ret = readb(data->base + EXYNOS_TMU_REG_CURRENT_TEMP); 829b79985caSBartlomiej Zolnierkiewicz 830b79985caSBartlomiej Zolnierkiewicz /* "temp_code" should range between 75 and 175 */ 831b79985caSBartlomiej Zolnierkiewicz return (ret < 75 || ret > 175) ? -ENODATA : ret; 832b79985caSBartlomiej Zolnierkiewicz } 833b79985caSBartlomiej Zolnierkiewicz 834b79985caSBartlomiej Zolnierkiewicz static int exynos4412_tmu_read(struct exynos_tmu_data *data) 835b79985caSBartlomiej Zolnierkiewicz { 836b79985caSBartlomiej Zolnierkiewicz return readb(data->base + EXYNOS_TMU_REG_CURRENT_TEMP); 837b79985caSBartlomiej Zolnierkiewicz } 838b79985caSBartlomiej Zolnierkiewicz 8396c247393SAbhilash Kesavan static int exynos7_tmu_read(struct exynos_tmu_data *data) 8406c247393SAbhilash Kesavan { 8416c247393SAbhilash Kesavan return readw(data->base + EXYNOS_TMU_REG_CURRENT_TEMP) & 8426c247393SAbhilash Kesavan EXYNOS7_TMU_TEMP_MASK; 8436c247393SAbhilash Kesavan } 8446c247393SAbhilash Kesavan 84559dfa54cSAmit Daniel Kachhap static void exynos_tmu_work(struct work_struct *work) 84659dfa54cSAmit Daniel Kachhap { 84759dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = container_of(work, 84859dfa54cSAmit Daniel Kachhap struct exynos_tmu_data, irq_work); 849a0395eeeSAmit Daniel Kachhap 85014a11dc7SNaveen Krishna Chatradhi if (!IS_ERR(data->clk_sec)) 85114a11dc7SNaveen Krishna Chatradhi clk_enable(data->clk_sec); 85214a11dc7SNaveen Krishna Chatradhi if (!IS_ERR(data->clk_sec)) 85314a11dc7SNaveen Krishna Chatradhi clk_disable(data->clk_sec); 85459dfa54cSAmit Daniel Kachhap 8553b6a1a80SLukasz Majewski exynos_report_trigger(data); 85659dfa54cSAmit Daniel Kachhap mutex_lock(&data->lock); 85759dfa54cSAmit Daniel Kachhap clk_enable(data->clk); 858b8d582b9SAmit Daniel Kachhap 859a4463c4fSAmit Daniel Kachhap /* TODO: take action based on particular interrupt */ 860a7331f72SBartlomiej Zolnierkiewicz data->tmu_clear_irqs(data); 861b8d582b9SAmit Daniel Kachhap 86259dfa54cSAmit Daniel Kachhap clk_disable(data->clk); 86359dfa54cSAmit Daniel Kachhap mutex_unlock(&data->lock); 86459dfa54cSAmit Daniel Kachhap enable_irq(data->irq); 86559dfa54cSAmit Daniel Kachhap } 86659dfa54cSAmit Daniel Kachhap 867a7331f72SBartlomiej Zolnierkiewicz static void exynos4210_tmu_clear_irqs(struct exynos_tmu_data *data) 868a7331f72SBartlomiej Zolnierkiewicz { 869a7331f72SBartlomiej Zolnierkiewicz unsigned int val_irq; 870a7331f72SBartlomiej Zolnierkiewicz u32 tmu_intstat, tmu_intclear; 871a7331f72SBartlomiej Zolnierkiewicz 872a7331f72SBartlomiej Zolnierkiewicz if (data->soc == SOC_ARCH_EXYNOS5260) { 873a7331f72SBartlomiej Zolnierkiewicz tmu_intstat = EXYNOS5260_TMU_REG_INTSTAT; 874a7331f72SBartlomiej Zolnierkiewicz tmu_intclear = EXYNOS5260_TMU_REG_INTCLEAR; 8756c247393SAbhilash Kesavan } else if (data->soc == SOC_ARCH_EXYNOS7) { 8766c247393SAbhilash Kesavan tmu_intstat = EXYNOS7_TMU_REG_INTPEND; 8776c247393SAbhilash Kesavan tmu_intclear = EXYNOS7_TMU_REG_INTPEND; 878488c7455SChanwoo Choi } else if (data->soc == SOC_ARCH_EXYNOS5433) { 879488c7455SChanwoo Choi tmu_intstat = EXYNOS5433_TMU_REG_INTPEND; 880488c7455SChanwoo Choi tmu_intclear = EXYNOS5433_TMU_REG_INTPEND; 881a7331f72SBartlomiej Zolnierkiewicz } else { 882a7331f72SBartlomiej Zolnierkiewicz tmu_intstat = EXYNOS_TMU_REG_INTSTAT; 883a7331f72SBartlomiej Zolnierkiewicz tmu_intclear = EXYNOS_TMU_REG_INTCLEAR; 884a7331f72SBartlomiej Zolnierkiewicz } 885a7331f72SBartlomiej Zolnierkiewicz 886a7331f72SBartlomiej Zolnierkiewicz val_irq = readl(data->base + tmu_intstat); 887a7331f72SBartlomiej Zolnierkiewicz /* 888a7331f72SBartlomiej Zolnierkiewicz * Clear the interrupts. Please note that the documentation for 889a7331f72SBartlomiej Zolnierkiewicz * Exynos3250, Exynos4412, Exynos5250 and Exynos5260 incorrectly 890a7331f72SBartlomiej Zolnierkiewicz * states that INTCLEAR register has a different placing of bits 891a7331f72SBartlomiej Zolnierkiewicz * responsible for FALL IRQs than INTSTAT register. Exynos5420 892a7331f72SBartlomiej Zolnierkiewicz * and Exynos5440 documentation is correct (Exynos4210 doesn't 893a7331f72SBartlomiej Zolnierkiewicz * support FALL IRQs at all). 894a7331f72SBartlomiej Zolnierkiewicz */ 895a7331f72SBartlomiej Zolnierkiewicz writel(val_irq, data->base + tmu_intclear); 896a7331f72SBartlomiej Zolnierkiewicz } 897a7331f72SBartlomiej Zolnierkiewicz 89859dfa54cSAmit Daniel Kachhap static irqreturn_t exynos_tmu_irq(int irq, void *id) 89959dfa54cSAmit Daniel Kachhap { 90059dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = id; 90159dfa54cSAmit Daniel Kachhap 90259dfa54cSAmit Daniel Kachhap disable_irq_nosync(irq); 90359dfa54cSAmit Daniel Kachhap schedule_work(&data->irq_work); 90459dfa54cSAmit Daniel Kachhap 90559dfa54cSAmit Daniel Kachhap return IRQ_HANDLED; 90659dfa54cSAmit Daniel Kachhap } 90759dfa54cSAmit Daniel Kachhap 90859dfa54cSAmit Daniel Kachhap static const struct of_device_id exynos_tmu_match[] = { 909fee88e2bSMaciej Purski { 910fee88e2bSMaciej Purski .compatible = "samsung,exynos3250-tmu", 911fee88e2bSMaciej Purski .data = (const void *)SOC_ARCH_EXYNOS3250, 912fee88e2bSMaciej Purski }, { 913fee88e2bSMaciej Purski .compatible = "samsung,exynos4210-tmu", 914fee88e2bSMaciej Purski .data = (const void *)SOC_ARCH_EXYNOS4210, 915fee88e2bSMaciej Purski }, { 916fee88e2bSMaciej Purski .compatible = "samsung,exynos4412-tmu", 917fee88e2bSMaciej Purski .data = (const void *)SOC_ARCH_EXYNOS4412, 918fee88e2bSMaciej Purski }, { 919fee88e2bSMaciej Purski .compatible = "samsung,exynos5250-tmu", 920fee88e2bSMaciej Purski .data = (const void *)SOC_ARCH_EXYNOS5250, 921fee88e2bSMaciej Purski }, { 922fee88e2bSMaciej Purski .compatible = "samsung,exynos5260-tmu", 923fee88e2bSMaciej Purski .data = (const void *)SOC_ARCH_EXYNOS5260, 924fee88e2bSMaciej Purski }, { 925fee88e2bSMaciej Purski .compatible = "samsung,exynos5420-tmu", 926fee88e2bSMaciej Purski .data = (const void *)SOC_ARCH_EXYNOS5420, 927fee88e2bSMaciej Purski }, { 928fee88e2bSMaciej Purski .compatible = "samsung,exynos5420-tmu-ext-triminfo", 929fee88e2bSMaciej Purski .data = (const void *)SOC_ARCH_EXYNOS5420_TRIMINFO, 930fee88e2bSMaciej Purski }, { 931fee88e2bSMaciej Purski .compatible = "samsung,exynos5433-tmu", 932fee88e2bSMaciej Purski .data = (const void *)SOC_ARCH_EXYNOS5433, 933fee88e2bSMaciej Purski }, { 934fee88e2bSMaciej Purski .compatible = "samsung,exynos7-tmu", 935fee88e2bSMaciej Purski .data = (const void *)SOC_ARCH_EXYNOS7, 936fee88e2bSMaciej Purski }, 937fee88e2bSMaciej Purski { }, 93859dfa54cSAmit Daniel Kachhap }; 93959dfa54cSAmit Daniel Kachhap MODULE_DEVICE_TABLE(of, exynos_tmu_match); 94059dfa54cSAmit Daniel Kachhap 941cebe7373SAmit Daniel Kachhap static int exynos_map_dt_data(struct platform_device *pdev) 94259dfa54cSAmit Daniel Kachhap { 943cebe7373SAmit Daniel Kachhap struct exynos_tmu_data *data = platform_get_drvdata(pdev); 944cebe7373SAmit Daniel Kachhap struct resource res; 94559dfa54cSAmit Daniel Kachhap 94673b5b1d7SSachin Kamat if (!data || !pdev->dev.of_node) 947cebe7373SAmit Daniel Kachhap return -ENODEV; 94859dfa54cSAmit Daniel Kachhap 949cebe7373SAmit Daniel Kachhap data->id = of_alias_get_id(pdev->dev.of_node, "tmuctrl"); 950cebe7373SAmit Daniel Kachhap if (data->id < 0) 951cebe7373SAmit Daniel Kachhap data->id = 0; 952cebe7373SAmit Daniel Kachhap 953cebe7373SAmit Daniel Kachhap data->irq = irq_of_parse_and_map(pdev->dev.of_node, 0); 954cebe7373SAmit Daniel Kachhap if (data->irq <= 0) { 955cebe7373SAmit Daniel Kachhap dev_err(&pdev->dev, "failed to get IRQ\n"); 956cebe7373SAmit Daniel Kachhap return -ENODEV; 957cebe7373SAmit Daniel Kachhap } 958cebe7373SAmit Daniel Kachhap 959cebe7373SAmit Daniel Kachhap if (of_address_to_resource(pdev->dev.of_node, 0, &res)) { 960cebe7373SAmit Daniel Kachhap dev_err(&pdev->dev, "failed to get Resource 0\n"); 961cebe7373SAmit Daniel Kachhap return -ENODEV; 962cebe7373SAmit Daniel Kachhap } 963cebe7373SAmit Daniel Kachhap 964cebe7373SAmit Daniel Kachhap data->base = devm_ioremap(&pdev->dev, res.start, resource_size(&res)); 965cebe7373SAmit Daniel Kachhap if (!data->base) { 966cebe7373SAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to ioremap memory\n"); 967cebe7373SAmit Daniel Kachhap return -EADDRNOTAVAIL; 968cebe7373SAmit Daniel Kachhap } 969cebe7373SAmit Daniel Kachhap 970fee88e2bSMaciej Purski data->soc = (enum soc_type)of_device_get_match_data(&pdev->dev); 97156adb9efSBartlomiej Zolnierkiewicz 97256adb9efSBartlomiej Zolnierkiewicz switch (data->soc) { 97356adb9efSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS4210: 974c8f8f768SBartlomiej Zolnierkiewicz data->tmu_set_trip_temp = exynos4210_tmu_set_trip_temp; 975c8f8f768SBartlomiej Zolnierkiewicz data->tmu_set_trip_hyst = exynos4210_tmu_set_trip_hyst; 97656adb9efSBartlomiej Zolnierkiewicz data->tmu_initialize = exynos4210_tmu_initialize; 97756adb9efSBartlomiej Zolnierkiewicz data->tmu_control = exynos4210_tmu_control; 97856adb9efSBartlomiej Zolnierkiewicz data->tmu_read = exynos4210_tmu_read; 97956adb9efSBartlomiej Zolnierkiewicz data->tmu_clear_irqs = exynos4210_tmu_clear_irqs; 9803a3a5f15SKrzysztof Kozlowski data->ntrip = 4; 981fccfe099SBartlomiej Zolnierkiewicz data->gain = 15; 98261020d18SBartlomiej Zolnierkiewicz data->reference_voltage = 7; 983e3ed3649SBartlomiej Zolnierkiewicz data->efuse_value = 55; 984e3ed3649SBartlomiej Zolnierkiewicz data->min_efuse_value = 40; 985e3ed3649SBartlomiej Zolnierkiewicz data->max_efuse_value = 100; 98656adb9efSBartlomiej Zolnierkiewicz break; 98756adb9efSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS3250: 98856adb9efSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS4412: 98956adb9efSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS5250: 99056adb9efSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS5260: 99156adb9efSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS5420: 99256adb9efSBartlomiej Zolnierkiewicz case SOC_ARCH_EXYNOS5420_TRIMINFO: 993c8f8f768SBartlomiej Zolnierkiewicz data->tmu_set_trip_temp = exynos4412_tmu_set_trip_temp; 994c8f8f768SBartlomiej Zolnierkiewicz data->tmu_set_trip_hyst = exynos4412_tmu_set_trip_hyst; 99556adb9efSBartlomiej Zolnierkiewicz data->tmu_initialize = exynos4412_tmu_initialize; 99656adb9efSBartlomiej Zolnierkiewicz data->tmu_control = exynos4210_tmu_control; 99756adb9efSBartlomiej Zolnierkiewicz data->tmu_read = exynos4412_tmu_read; 99856adb9efSBartlomiej Zolnierkiewicz data->tmu_set_emulation = exynos4412_tmu_set_emulation; 99956adb9efSBartlomiej Zolnierkiewicz data->tmu_clear_irqs = exynos4210_tmu_clear_irqs; 10003a3a5f15SKrzysztof Kozlowski data->ntrip = 4; 1001fccfe099SBartlomiej Zolnierkiewicz data->gain = 8; 100261020d18SBartlomiej Zolnierkiewicz data->reference_voltage = 16; 1003e3ed3649SBartlomiej Zolnierkiewicz data->efuse_value = 55; 1004e3ed3649SBartlomiej Zolnierkiewicz if (data->soc != SOC_ARCH_EXYNOS5420 && 1005e3ed3649SBartlomiej Zolnierkiewicz data->soc != SOC_ARCH_EXYNOS5420_TRIMINFO) 1006e3ed3649SBartlomiej Zolnierkiewicz data->min_efuse_value = 40; 1007e3ed3649SBartlomiej Zolnierkiewicz else 1008e3ed3649SBartlomiej Zolnierkiewicz data->min_efuse_value = 0; 1009e3ed3649SBartlomiej Zolnierkiewicz data->max_efuse_value = 100; 101056adb9efSBartlomiej Zolnierkiewicz break; 1011488c7455SChanwoo Choi case SOC_ARCH_EXYNOS5433: 1012c8f8f768SBartlomiej Zolnierkiewicz data->tmu_set_trip_temp = exynos5433_tmu_set_trip_temp; 1013c8f8f768SBartlomiej Zolnierkiewicz data->tmu_set_trip_hyst = exynos5433_tmu_set_trip_hyst; 1014488c7455SChanwoo Choi data->tmu_initialize = exynos5433_tmu_initialize; 1015488c7455SChanwoo Choi data->tmu_control = exynos5433_tmu_control; 1016488c7455SChanwoo Choi data->tmu_read = exynos4412_tmu_read; 1017488c7455SChanwoo Choi data->tmu_set_emulation = exynos4412_tmu_set_emulation; 1018488c7455SChanwoo Choi data->tmu_clear_irqs = exynos4210_tmu_clear_irqs; 10193a3a5f15SKrzysztof Kozlowski data->ntrip = 8; 1020fccfe099SBartlomiej Zolnierkiewicz data->gain = 8; 102161020d18SBartlomiej Zolnierkiewicz if (res.start == EXYNOS5433_G3D_BASE) 102261020d18SBartlomiej Zolnierkiewicz data->reference_voltage = 23; 102361020d18SBartlomiej Zolnierkiewicz else 102461020d18SBartlomiej Zolnierkiewicz data->reference_voltage = 16; 1025e3ed3649SBartlomiej Zolnierkiewicz data->efuse_value = 75; 1026e3ed3649SBartlomiej Zolnierkiewicz data->min_efuse_value = 40; 1027e3ed3649SBartlomiej Zolnierkiewicz data->max_efuse_value = 150; 1028488c7455SChanwoo Choi break; 10296c247393SAbhilash Kesavan case SOC_ARCH_EXYNOS7: 1030c8f8f768SBartlomiej Zolnierkiewicz data->tmu_set_trip_temp = exynos7_tmu_set_trip_temp; 1031c8f8f768SBartlomiej Zolnierkiewicz data->tmu_set_trip_hyst = exynos7_tmu_set_trip_hyst; 10326c247393SAbhilash Kesavan data->tmu_initialize = exynos7_tmu_initialize; 10336c247393SAbhilash Kesavan data->tmu_control = exynos7_tmu_control; 10346c247393SAbhilash Kesavan data->tmu_read = exynos7_tmu_read; 10356c247393SAbhilash Kesavan data->tmu_set_emulation = exynos4412_tmu_set_emulation; 10366c247393SAbhilash Kesavan data->tmu_clear_irqs = exynos4210_tmu_clear_irqs; 10373a3a5f15SKrzysztof Kozlowski data->ntrip = 8; 1038fccfe099SBartlomiej Zolnierkiewicz data->gain = 9; 103961020d18SBartlomiej Zolnierkiewicz data->reference_voltage = 17; 1040e3ed3649SBartlomiej Zolnierkiewicz data->efuse_value = 75; 1041e3ed3649SBartlomiej Zolnierkiewicz data->min_efuse_value = 15; 1042e3ed3649SBartlomiej Zolnierkiewicz data->max_efuse_value = 100; 10436c247393SAbhilash Kesavan break; 104456adb9efSBartlomiej Zolnierkiewicz default: 104556adb9efSBartlomiej Zolnierkiewicz dev_err(&pdev->dev, "Platform not supported\n"); 104656adb9efSBartlomiej Zolnierkiewicz return -EINVAL; 104756adb9efSBartlomiej Zolnierkiewicz } 104856adb9efSBartlomiej Zolnierkiewicz 1049199b3e3cSBartlomiej Zolnierkiewicz data->cal_type = TYPE_ONE_POINT_TRIMMING; 1050199b3e3cSBartlomiej Zolnierkiewicz 1051d9b6ee14SAmit Daniel Kachhap /* 1052d9b6ee14SAmit Daniel Kachhap * Check if the TMU shares some registers and then try to map the 1053d9b6ee14SAmit Daniel Kachhap * memory of common registers. 1054d9b6ee14SAmit Daniel Kachhap */ 10558014220dSKrzysztof Kozlowski if (data->soc != SOC_ARCH_EXYNOS5420_TRIMINFO) 1056d9b6ee14SAmit Daniel Kachhap return 0; 1057d9b6ee14SAmit Daniel Kachhap 1058d9b6ee14SAmit Daniel Kachhap if (of_address_to_resource(pdev->dev.of_node, 1, &res)) { 1059d9b6ee14SAmit Daniel Kachhap dev_err(&pdev->dev, "failed to get Resource 1\n"); 1060d9b6ee14SAmit Daniel Kachhap return -ENODEV; 1061d9b6ee14SAmit Daniel Kachhap } 1062d9b6ee14SAmit Daniel Kachhap 10639025d563SNaveen Krishna Chatradhi data->base_second = devm_ioremap(&pdev->dev, res.start, 1064d9b6ee14SAmit Daniel Kachhap resource_size(&res)); 10659025d563SNaveen Krishna Chatradhi if (!data->base_second) { 1066d9b6ee14SAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to ioremap memory\n"); 1067d9b6ee14SAmit Daniel Kachhap return -ENOMEM; 1068d9b6ee14SAmit Daniel Kachhap } 1069cebe7373SAmit Daniel Kachhap 1070cebe7373SAmit Daniel Kachhap return 0; 1071cebe7373SAmit Daniel Kachhap } 1072cebe7373SAmit Daniel Kachhap 1073c3c04d9dSJulia Lawall static const struct thermal_zone_of_device_ops exynos_sensor_ops = { 10743b6a1a80SLukasz Majewski .get_temp = exynos_get_temp, 10753b6a1a80SLukasz Majewski .set_emul_temp = exynos_tmu_set_emulation, 10763b6a1a80SLukasz Majewski }; 10773b6a1a80SLukasz Majewski 1078cebe7373SAmit Daniel Kachhap static int exynos_tmu_probe(struct platform_device *pdev) 1079cebe7373SAmit Daniel Kachhap { 10803b6a1a80SLukasz Majewski struct exynos_tmu_data *data; 10813b6a1a80SLukasz Majewski int ret; 1082cebe7373SAmit Daniel Kachhap 108359dfa54cSAmit Daniel Kachhap data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data), 108459dfa54cSAmit Daniel Kachhap GFP_KERNEL); 10852a9675b3SJingoo Han if (!data) 108659dfa54cSAmit Daniel Kachhap return -ENOMEM; 108759dfa54cSAmit Daniel Kachhap 1088cebe7373SAmit Daniel Kachhap platform_set_drvdata(pdev, data); 1089cebe7373SAmit Daniel Kachhap mutex_init(&data->lock); 1090cebe7373SAmit Daniel Kachhap 1091824ead03SKrzysztof Kozlowski /* 1092824ead03SKrzysztof Kozlowski * Try enabling the regulator if found 1093824ead03SKrzysztof Kozlowski * TODO: Add regulator as an SOC feature, so that regulator enable 1094824ead03SKrzysztof Kozlowski * is a compulsory call. 1095824ead03SKrzysztof Kozlowski */ 10964d3583cdSJavier Martinez Canillas data->regulator = devm_regulator_get_optional(&pdev->dev, "vtmu"); 1097824ead03SKrzysztof Kozlowski if (!IS_ERR(data->regulator)) { 1098824ead03SKrzysztof Kozlowski ret = regulator_enable(data->regulator); 1099824ead03SKrzysztof Kozlowski if (ret) { 1100824ead03SKrzysztof Kozlowski dev_err(&pdev->dev, "failed to enable vtmu\n"); 1101824ead03SKrzysztof Kozlowski return ret; 11023b6a1a80SLukasz Majewski } 1103824ead03SKrzysztof Kozlowski } else { 1104ccb361d2SJavier Martinez Canillas if (PTR_ERR(data->regulator) == -EPROBE_DEFER) 1105ccb361d2SJavier Martinez Canillas return -EPROBE_DEFER; 1106824ead03SKrzysztof Kozlowski dev_info(&pdev->dev, "Regulator node (vtmu) not found\n"); 1107824ead03SKrzysztof Kozlowski } 1108824ead03SKrzysztof Kozlowski 1109cebe7373SAmit Daniel Kachhap ret = exynos_map_dt_data(pdev); 1110cebe7373SAmit Daniel Kachhap if (ret) 11113b6a1a80SLukasz Majewski goto err_sensor; 1112cebe7373SAmit Daniel Kachhap 111359dfa54cSAmit Daniel Kachhap INIT_WORK(&data->irq_work, exynos_tmu_work); 111459dfa54cSAmit Daniel Kachhap 111559dfa54cSAmit Daniel Kachhap data->clk = devm_clk_get(&pdev->dev, "tmu_apbif"); 111659dfa54cSAmit Daniel Kachhap if (IS_ERR(data->clk)) { 111759dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to get clock\n"); 11183b6a1a80SLukasz Majewski ret = PTR_ERR(data->clk); 11193b6a1a80SLukasz Majewski goto err_sensor; 112059dfa54cSAmit Daniel Kachhap } 112159dfa54cSAmit Daniel Kachhap 112214a11dc7SNaveen Krishna Chatradhi data->clk_sec = devm_clk_get(&pdev->dev, "tmu_triminfo_apbif"); 112314a11dc7SNaveen Krishna Chatradhi if (IS_ERR(data->clk_sec)) { 112414a11dc7SNaveen Krishna Chatradhi if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO) { 112514a11dc7SNaveen Krishna Chatradhi dev_err(&pdev->dev, "Failed to get triminfo clock\n"); 11263b6a1a80SLukasz Majewski ret = PTR_ERR(data->clk_sec); 11273b6a1a80SLukasz Majewski goto err_sensor; 112814a11dc7SNaveen Krishna Chatradhi } 112914a11dc7SNaveen Krishna Chatradhi } else { 113014a11dc7SNaveen Krishna Chatradhi ret = clk_prepare(data->clk_sec); 113114a11dc7SNaveen Krishna Chatradhi if (ret) { 113214a11dc7SNaveen Krishna Chatradhi dev_err(&pdev->dev, "Failed to get clock\n"); 11333b6a1a80SLukasz Majewski goto err_sensor; 113414a11dc7SNaveen Krishna Chatradhi } 113514a11dc7SNaveen Krishna Chatradhi } 113614a11dc7SNaveen Krishna Chatradhi 113714a11dc7SNaveen Krishna Chatradhi ret = clk_prepare(data->clk); 113814a11dc7SNaveen Krishna Chatradhi if (ret) { 113914a11dc7SNaveen Krishna Chatradhi dev_err(&pdev->dev, "Failed to get clock\n"); 114014a11dc7SNaveen Krishna Chatradhi goto err_clk_sec; 114114a11dc7SNaveen Krishna Chatradhi } 114259dfa54cSAmit Daniel Kachhap 1143488c7455SChanwoo Choi switch (data->soc) { 1144488c7455SChanwoo Choi case SOC_ARCH_EXYNOS5433: 1145488c7455SChanwoo Choi case SOC_ARCH_EXYNOS7: 11466c247393SAbhilash Kesavan data->sclk = devm_clk_get(&pdev->dev, "tmu_sclk"); 11476c247393SAbhilash Kesavan if (IS_ERR(data->sclk)) { 11486c247393SAbhilash Kesavan dev_err(&pdev->dev, "Failed to get sclk\n"); 11496c247393SAbhilash Kesavan goto err_clk; 11506c247393SAbhilash Kesavan } else { 11516c247393SAbhilash Kesavan ret = clk_prepare_enable(data->sclk); 11526c247393SAbhilash Kesavan if (ret) { 11536c247393SAbhilash Kesavan dev_err(&pdev->dev, "Failed to enable sclk\n"); 11546c247393SAbhilash Kesavan goto err_clk; 11556c247393SAbhilash Kesavan } 11566c247393SAbhilash Kesavan } 1157488c7455SChanwoo Choi break; 1158488c7455SChanwoo Choi default: 1159488c7455SChanwoo Choi break; 1160baba1ebbSKrzysztof Kozlowski } 11616c247393SAbhilash Kesavan 11629e4249b4SKrzysztof Kozlowski /* 11639e4249b4SKrzysztof Kozlowski * data->tzd must be registered before calling exynos_tmu_initialize(), 11649e4249b4SKrzysztof Kozlowski * requesting irq and calling exynos_tmu_control(). 11659e4249b4SKrzysztof Kozlowski */ 11669e4249b4SKrzysztof Kozlowski data->tzd = thermal_zone_of_sensor_register(&pdev->dev, 0, data, 11679e4249b4SKrzysztof Kozlowski &exynos_sensor_ops); 11689e4249b4SKrzysztof Kozlowski if (IS_ERR(data->tzd)) { 11699e4249b4SKrzysztof Kozlowski ret = PTR_ERR(data->tzd); 11709e4249b4SKrzysztof Kozlowski dev_err(&pdev->dev, "Failed to register sensor: %d\n", ret); 11719e4249b4SKrzysztof Kozlowski goto err_sclk; 11729e4249b4SKrzysztof Kozlowski } 117359dfa54cSAmit Daniel Kachhap 117459dfa54cSAmit Daniel Kachhap ret = exynos_tmu_initialize(pdev); 117559dfa54cSAmit Daniel Kachhap if (ret) { 117659dfa54cSAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to initialize TMU\n"); 11779e4249b4SKrzysztof Kozlowski goto err_thermal; 117859dfa54cSAmit Daniel Kachhap } 117959dfa54cSAmit Daniel Kachhap 1180cebe7373SAmit Daniel Kachhap ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq, 1181cebe7373SAmit Daniel Kachhap IRQF_TRIGGER_RISING | IRQF_SHARED, dev_name(&pdev->dev), data); 1182cebe7373SAmit Daniel Kachhap if (ret) { 1183cebe7373SAmit Daniel Kachhap dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq); 11849e4249b4SKrzysztof Kozlowski goto err_thermal; 1185cebe7373SAmit Daniel Kachhap } 118659dfa54cSAmit Daniel Kachhap 11873b6a1a80SLukasz Majewski exynos_tmu_control(pdev, true); 118859dfa54cSAmit Daniel Kachhap return 0; 11899e4249b4SKrzysztof Kozlowski 11909e4249b4SKrzysztof Kozlowski err_thermal: 11919e4249b4SKrzysztof Kozlowski thermal_zone_of_sensor_unregister(&pdev->dev, data->tzd); 11926c247393SAbhilash Kesavan err_sclk: 11936c247393SAbhilash Kesavan clk_disable_unprepare(data->sclk); 119459dfa54cSAmit Daniel Kachhap err_clk: 119559dfa54cSAmit Daniel Kachhap clk_unprepare(data->clk); 119614a11dc7SNaveen Krishna Chatradhi err_clk_sec: 119714a11dc7SNaveen Krishna Chatradhi if (!IS_ERR(data->clk_sec)) 119814a11dc7SNaveen Krishna Chatradhi clk_unprepare(data->clk_sec); 11993b6a1a80SLukasz Majewski err_sensor: 1200bfa26838SKrzysztof Kozlowski if (!IS_ERR(data->regulator)) 12015f09a5cbSKrzysztof Kozlowski regulator_disable(data->regulator); 12023b6a1a80SLukasz Majewski 120359dfa54cSAmit Daniel Kachhap return ret; 120459dfa54cSAmit Daniel Kachhap } 120559dfa54cSAmit Daniel Kachhap 120659dfa54cSAmit Daniel Kachhap static int exynos_tmu_remove(struct platform_device *pdev) 120759dfa54cSAmit Daniel Kachhap { 120859dfa54cSAmit Daniel Kachhap struct exynos_tmu_data *data = platform_get_drvdata(pdev); 12093b6a1a80SLukasz Majewski struct thermal_zone_device *tzd = data->tzd; 121059dfa54cSAmit Daniel Kachhap 12113b6a1a80SLukasz Majewski thermal_zone_of_sensor_unregister(&pdev->dev, tzd); 12124215688eSBartlomiej Zolnierkiewicz exynos_tmu_control(pdev, false); 12134215688eSBartlomiej Zolnierkiewicz 12146c247393SAbhilash Kesavan clk_disable_unprepare(data->sclk); 121559dfa54cSAmit Daniel Kachhap clk_unprepare(data->clk); 121614a11dc7SNaveen Krishna Chatradhi if (!IS_ERR(data->clk_sec)) 121714a11dc7SNaveen Krishna Chatradhi clk_unprepare(data->clk_sec); 121859dfa54cSAmit Daniel Kachhap 1219498d22f6SAmit Daniel Kachhap if (!IS_ERR(data->regulator)) 1220498d22f6SAmit Daniel Kachhap regulator_disable(data->regulator); 1221498d22f6SAmit Daniel Kachhap 122259dfa54cSAmit Daniel Kachhap return 0; 122359dfa54cSAmit Daniel Kachhap } 122459dfa54cSAmit Daniel Kachhap 122559dfa54cSAmit Daniel Kachhap #ifdef CONFIG_PM_SLEEP 122659dfa54cSAmit Daniel Kachhap static int exynos_tmu_suspend(struct device *dev) 122759dfa54cSAmit Daniel Kachhap { 122859dfa54cSAmit Daniel Kachhap exynos_tmu_control(to_platform_device(dev), false); 122959dfa54cSAmit Daniel Kachhap 123059dfa54cSAmit Daniel Kachhap return 0; 123159dfa54cSAmit Daniel Kachhap } 123259dfa54cSAmit Daniel Kachhap 123359dfa54cSAmit Daniel Kachhap static int exynos_tmu_resume(struct device *dev) 123459dfa54cSAmit Daniel Kachhap { 123559dfa54cSAmit Daniel Kachhap struct platform_device *pdev = to_platform_device(dev); 123659dfa54cSAmit Daniel Kachhap 123759dfa54cSAmit Daniel Kachhap exynos_tmu_initialize(pdev); 123859dfa54cSAmit Daniel Kachhap exynos_tmu_control(pdev, true); 123959dfa54cSAmit Daniel Kachhap 124059dfa54cSAmit Daniel Kachhap return 0; 124159dfa54cSAmit Daniel Kachhap } 124259dfa54cSAmit Daniel Kachhap 124359dfa54cSAmit Daniel Kachhap static SIMPLE_DEV_PM_OPS(exynos_tmu_pm, 124459dfa54cSAmit Daniel Kachhap exynos_tmu_suspend, exynos_tmu_resume); 124559dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM (&exynos_tmu_pm) 124659dfa54cSAmit Daniel Kachhap #else 124759dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM NULL 124859dfa54cSAmit Daniel Kachhap #endif 124959dfa54cSAmit Daniel Kachhap 125059dfa54cSAmit Daniel Kachhap static struct platform_driver exynos_tmu_driver = { 125159dfa54cSAmit Daniel Kachhap .driver = { 125259dfa54cSAmit Daniel Kachhap .name = "exynos-tmu", 125359dfa54cSAmit Daniel Kachhap .pm = EXYNOS_TMU_PM, 125473b5b1d7SSachin Kamat .of_match_table = exynos_tmu_match, 125559dfa54cSAmit Daniel Kachhap }, 125659dfa54cSAmit Daniel Kachhap .probe = exynos_tmu_probe, 125759dfa54cSAmit Daniel Kachhap .remove = exynos_tmu_remove, 125859dfa54cSAmit Daniel Kachhap }; 125959dfa54cSAmit Daniel Kachhap 126059dfa54cSAmit Daniel Kachhap module_platform_driver(exynos_tmu_driver); 126159dfa54cSAmit Daniel Kachhap 126259dfa54cSAmit Daniel Kachhap MODULE_DESCRIPTION("EXYNOS TMU Driver"); 126359dfa54cSAmit Daniel Kachhap MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>"); 126459dfa54cSAmit Daniel Kachhap MODULE_LICENSE("GPL"); 126559dfa54cSAmit Daniel Kachhap MODULE_ALIAS("platform:exynos-tmu"); 1266