Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 1 | /* |
Amit Daniel Kachhap | 59dfa54 | 2013-06-24 16:20:26 +0530 | [diff] [blame] | 2 | * exynos_tmu.c - Samsung EXYNOS TMU (Thermal Management Unit) |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2011 Samsung Electronics |
| 5 | * Donggeun Kim <dg77.kim@samsung.com> |
Amit Daniel Kachhap | c48cbba | 2012-08-16 17:11:41 +0530 | [diff] [blame] | 6 | * Amit Daniel Kachhap <amit.kachhap@linaro.org> |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
| 23 | |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 24 | #include <linux/clk.h> |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 25 | #include <linux/io.h> |
Amit Daniel Kachhap | 1b67864 | 2013-06-24 16:20:25 +0530 | [diff] [blame] | 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/module.h> |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 28 | #include <linux/of.h> |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 29 | #include <linux/of_address.h> |
| 30 | #include <linux/of_irq.h> |
Amit Daniel Kachhap | 1b67864 | 2013-06-24 16:20:25 +0530 | [diff] [blame] | 31 | #include <linux/platform_device.h> |
Amit Daniel Kachhap | 498d22f | 2013-06-24 16:20:47 +0530 | [diff] [blame] | 32 | #include <linux/regulator/consumer.h> |
Amit Daniel Kachhap | 1b67864 | 2013-06-24 16:20:25 +0530 | [diff] [blame] | 33 | |
| 34 | #include "exynos_thermal_common.h" |
Amit Daniel Kachhap | 0c1836a | 2013-06-24 16:20:27 +0530 | [diff] [blame] | 35 | #include "exynos_tmu.h" |
Amit Daniel Kachhap | e6b7991 | 2013-06-24 16:20:28 +0530 | [diff] [blame] | 36 | #include "exynos_tmu_data.h" |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 37 | |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 38 | /** |
| 39 | * struct exynos_tmu_data : A structure to hold the private data of the TMU |
| 40 | driver |
| 41 | * @id: identifier of the one instance of the TMU controller. |
| 42 | * @pdata: pointer to the tmu platform/configuration data |
| 43 | * @base: base address of the single instance of the TMU controller. |
Naveen Krishna Chatradhi | 9025d56 | 2013-12-19 11:36:08 +0530 | [diff] [blame] | 44 | * @base_second: base address of the common registers of the TMU controller. |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 45 | * @irq: irq number of the TMU controller. |
| 46 | * @soc: id of the SOC type. |
| 47 | * @irq_work: pointer to the irq work structure. |
| 48 | * @lock: lock to implement synchronization. |
| 49 | * @clk: pointer to the clock structure. |
Naveen Krishna Chatradhi | 14a11dc | 2013-12-19 11:36:31 +0530 | [diff] [blame] | 50 | * @clk_sec: pointer to the clock structure for accessing the base_second. |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 51 | * @temp_error1: fused value of the first point trim. |
| 52 | * @temp_error2: fused value of the second point trim. |
Amit Daniel Kachhap | 498d22f | 2013-06-24 16:20:47 +0530 | [diff] [blame] | 53 | * @regulator: pointer to the TMU regulator structure. |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 54 | * @reg_conf: pointer to structure to register with core thermal. |
| 55 | */ |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 56 | struct exynos_tmu_data { |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 57 | int id; |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 58 | struct exynos_tmu_platform_data *pdata; |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 59 | void __iomem *base; |
Naveen Krishna Chatradhi | 9025d56 | 2013-12-19 11:36:08 +0530 | [diff] [blame] | 60 | void __iomem *base_second; |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 61 | int irq; |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 62 | enum soc_type soc; |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 63 | struct work_struct irq_work; |
| 64 | struct mutex lock; |
Naveen Krishna Chatradhi | 14a11dc | 2013-12-19 11:36:31 +0530 | [diff] [blame] | 65 | struct clk *clk, *clk_sec; |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 66 | u8 temp_error1, temp_error2; |
Amit Daniel Kachhap | 498d22f | 2013-06-24 16:20:47 +0530 | [diff] [blame] | 67 | struct regulator *regulator; |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 68 | struct thermal_sensor_conf *reg_conf; |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | /* |
| 72 | * TMU treats temperature as a mapped temperature code. |
| 73 | * The temperature is converted differently depending on the calibration type. |
| 74 | */ |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 75 | static int temp_to_code(struct exynos_tmu_data *data, u8 temp) |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 76 | { |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 77 | struct exynos_tmu_platform_data *pdata = data->pdata; |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 78 | int temp_code; |
| 79 | |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 80 | switch (pdata->cal_type) { |
| 81 | case TYPE_TWO_POINT_TRIMMING: |
Amit Daniel Kachhap | bb34b4c | 2013-06-24 16:20:30 +0530 | [diff] [blame] | 82 | temp_code = (temp - pdata->first_point_trim) * |
| 83 | (data->temp_error2 - data->temp_error1) / |
| 84 | (pdata->second_point_trim - pdata->first_point_trim) + |
| 85 | data->temp_error1; |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 86 | break; |
| 87 | case TYPE_ONE_POINT_TRIMMING: |
Amit Daniel Kachhap | bb34b4c | 2013-06-24 16:20:30 +0530 | [diff] [blame] | 88 | temp_code = temp + data->temp_error1 - pdata->first_point_trim; |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 89 | break; |
| 90 | default: |
Amit Daniel Kachhap | bb34b4c | 2013-06-24 16:20:30 +0530 | [diff] [blame] | 91 | temp_code = temp + pdata->default_temp_offset; |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 92 | break; |
| 93 | } |
Bartlomiej Zolnierkiewicz | ddb31d4 | 2014-07-31 19:11:03 +0200 | [diff] [blame] | 94 | |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 95 | return temp_code; |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | * Calculate a temperature value from a temperature code. |
| 100 | * The unit of the temperature is degree Celsius. |
| 101 | */ |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 102 | static int code_to_temp(struct exynos_tmu_data *data, u8 temp_code) |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 103 | { |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 104 | struct exynos_tmu_platform_data *pdata = data->pdata; |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 105 | int temp; |
| 106 | |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 107 | switch (pdata->cal_type) { |
| 108 | case TYPE_TWO_POINT_TRIMMING: |
Amit Daniel Kachhap | bb34b4c | 2013-06-24 16:20:30 +0530 | [diff] [blame] | 109 | temp = (temp_code - data->temp_error1) * |
| 110 | (pdata->second_point_trim - pdata->first_point_trim) / |
| 111 | (data->temp_error2 - data->temp_error1) + |
| 112 | pdata->first_point_trim; |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 113 | break; |
| 114 | case TYPE_ONE_POINT_TRIMMING: |
Amit Daniel Kachhap | bb34b4c | 2013-06-24 16:20:30 +0530 | [diff] [blame] | 115 | temp = temp_code - data->temp_error1 + pdata->first_point_trim; |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 116 | break; |
| 117 | default: |
Amit Daniel Kachhap | bb34b4c | 2013-06-24 16:20:30 +0530 | [diff] [blame] | 118 | temp = temp_code - pdata->default_temp_offset; |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 119 | break; |
| 120 | } |
Bartlomiej Zolnierkiewicz | ddb31d4 | 2014-07-31 19:11:03 +0200 | [diff] [blame] | 121 | |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 122 | return temp; |
| 123 | } |
| 124 | |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 125 | static int exynos_tmu_initialize(struct platform_device *pdev) |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 126 | { |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 127 | struct exynos_tmu_data *data = platform_get_drvdata(pdev); |
| 128 | struct exynos_tmu_platform_data *pdata = data->pdata; |
Amit Daniel Kachhap | b8d582b | 2013-06-24 16:20:31 +0530 | [diff] [blame] | 129 | const struct exynos_tmu_registers *reg = pdata->registers; |
Amit Daniel Kachhap | 7ca04e5 | 2013-06-24 16:20:32 +0530 | [diff] [blame] | 130 | unsigned int status, trim_info = 0, con; |
Jonghwa Lee | 4f0a684 | 2013-02-08 01:13:06 +0000 | [diff] [blame] | 131 | unsigned int rising_threshold = 0, falling_threshold = 0; |
Bartlomiej Zolnierkiewicz | ac951af | 2014-07-31 19:11:04 +0200 | [diff] [blame^] | 132 | int ret = 0, threshold_code, i; |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 133 | |
| 134 | mutex_lock(&data->lock); |
| 135 | clk_enable(data->clk); |
Naveen Krishna Chatradhi | 14a11dc | 2013-12-19 11:36:31 +0530 | [diff] [blame] | 136 | if (!IS_ERR(data->clk_sec)) |
| 137 | clk_enable(data->clk_sec); |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 138 | |
Amit Daniel Kachhap | f4dae75 | 2013-06-24 16:20:40 +0530 | [diff] [blame] | 139 | if (TMU_SUPPORTS(pdata, READY_STATUS)) { |
| 140 | status = readb(data->base + reg->tmu_status); |
| 141 | if (!status) { |
| 142 | ret = -EBUSY; |
| 143 | goto out; |
| 144 | } |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 145 | } |
| 146 | |
Amit Daniel Kachhap | f4dae75 | 2013-06-24 16:20:40 +0530 | [diff] [blame] | 147 | if (TMU_SUPPORTS(pdata, TRIM_RELOAD)) |
Amit Daniel Kachhap | b8d582b | 2013-06-24 16:20:31 +0530 | [diff] [blame] | 148 | __raw_writel(1, data->base + reg->triminfo_ctrl); |
| 149 | |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 150 | /* Save trimming info in order to perform calibration */ |
Amit Daniel Kachhap | a0395ee | 2013-06-24 16:20:43 +0530 | [diff] [blame] | 151 | if (data->soc == SOC_ARCH_EXYNOS5440) { |
| 152 | /* |
| 153 | * For exynos5440 soc triminfo value is swapped between TMU0 and |
| 154 | * TMU2, so the below logic is needed. |
| 155 | */ |
| 156 | switch (data->id) { |
| 157 | case 0: |
| 158 | trim_info = readl(data->base + |
| 159 | EXYNOS5440_EFUSE_SWAP_OFFSET + reg->triminfo_data); |
| 160 | break; |
| 161 | case 1: |
| 162 | trim_info = readl(data->base + reg->triminfo_data); |
| 163 | break; |
| 164 | case 2: |
| 165 | trim_info = readl(data->base - |
| 166 | EXYNOS5440_EFUSE_SWAP_OFFSET + reg->triminfo_data); |
| 167 | } |
| 168 | } else { |
Naveen Krishna Chatradhi | 14a11dc | 2013-12-19 11:36:31 +0530 | [diff] [blame] | 169 | /* On exynos5420 the triminfo register is in the shared space */ |
| 170 | if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO) |
| 171 | trim_info = readl(data->base_second + |
| 172 | reg->triminfo_data); |
| 173 | else |
| 174 | trim_info = readl(data->base + reg->triminfo_data); |
Amit Daniel Kachhap | a0395ee | 2013-06-24 16:20:43 +0530 | [diff] [blame] | 175 | } |
Amit Daniel Kachhap | b8d582b | 2013-06-24 16:20:31 +0530 | [diff] [blame] | 176 | data->temp_error1 = trim_info & EXYNOS_TMU_TEMP_MASK; |
| 177 | data->temp_error2 = ((trim_info >> reg->triminfo_85_shift) & |
| 178 | EXYNOS_TMU_TEMP_MASK); |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 179 | |
Amit Daniel Kachhap | 5000806 | 2013-06-24 16:20:45 +0530 | [diff] [blame] | 180 | if (!data->temp_error1 || |
| 181 | (pdata->min_efuse_value > data->temp_error1) || |
| 182 | (data->temp_error1 > pdata->max_efuse_value)) |
| 183 | data->temp_error1 = pdata->efuse_value & EXYNOS_TMU_TEMP_MASK; |
| 184 | |
| 185 | if (!data->temp_error2) |
| 186 | data->temp_error2 = |
| 187 | (pdata->efuse_value >> reg->triminfo_85_shift) & |
| 188 | EXYNOS_TMU_TEMP_MASK; |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 189 | |
Tushar Behera | c65d347 | 2014-04-14 11:08:15 +0530 | [diff] [blame] | 190 | rising_threshold = readl(data->base + reg->threshold_th0); |
| 191 | |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 192 | if (data->soc == SOC_ARCH_EXYNOS4210) { |
| 193 | /* Write temperature code for threshold */ |
| 194 | threshold_code = temp_to_code(data, pdata->threshold); |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 195 | writeb(threshold_code, |
Amit Daniel Kachhap | b8d582b | 2013-06-24 16:20:31 +0530 | [diff] [blame] | 196 | data->base + reg->threshold_temp); |
Bartlomiej Zolnierkiewicz | ac951af | 2014-07-31 19:11:04 +0200 | [diff] [blame^] | 197 | for (i = 0; i < pdata->non_hw_trigger_levels; i++) |
Amit Daniel Kachhap | b8d582b | 2013-06-24 16:20:31 +0530 | [diff] [blame] | 198 | writeb(pdata->trigger_levels[i], data->base + |
| 199 | reg->threshold_th0 + i * sizeof(reg->threshold_th0)); |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 200 | |
Naveen Krishna Chatradhi | 74429c2 | 2013-12-19 11:35:39 +0530 | [diff] [blame] | 201 | writel(reg->intclr_rise_mask, data->base + reg->tmu_intclear); |
Amit Daniel Kachhap | a0395ee | 2013-06-24 16:20:43 +0530 | [diff] [blame] | 202 | } else { |
Jonghwa Lee | 4f0a684 | 2013-02-08 01:13:06 +0000 | [diff] [blame] | 203 | /* Write temperature code for rising and falling threshold */ |
Bartlomiej Zolnierkiewicz | ac951af | 2014-07-31 19:11:04 +0200 | [diff] [blame^] | 204 | for (i = 0; i < pdata->non_hw_trigger_levels; i++) { |
Jonghwa Lee | 4f0a684 | 2013-02-08 01:13:06 +0000 | [diff] [blame] | 205 | threshold_code = temp_to_code(data, |
| 206 | pdata->trigger_levels[i]); |
Tushar Behera | c65d347 | 2014-04-14 11:08:15 +0530 | [diff] [blame] | 207 | rising_threshold &= ~(0xff << 8 * i); |
Jonghwa Lee | 4f0a684 | 2013-02-08 01:13:06 +0000 | [diff] [blame] | 208 | rising_threshold |= threshold_code << 8 * i; |
| 209 | if (pdata->threshold_falling) { |
| 210 | threshold_code = temp_to_code(data, |
| 211 | pdata->trigger_levels[i] - |
| 212 | pdata->threshold_falling); |
Bartlomiej Zolnierkiewicz | 8131a24 | 2014-07-31 19:11:02 +0200 | [diff] [blame] | 213 | falling_threshold |= threshold_code << 8 * i; |
Jonghwa Lee | 4f0a684 | 2013-02-08 01:13:06 +0000 | [diff] [blame] | 214 | } |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 215 | } |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 216 | |
| 217 | writel(rising_threshold, |
Amit Daniel Kachhap | b8d582b | 2013-06-24 16:20:31 +0530 | [diff] [blame] | 218 | data->base + reg->threshold_th0); |
Jonghwa Lee | 4f0a684 | 2013-02-08 01:13:06 +0000 | [diff] [blame] | 219 | writel(falling_threshold, |
Amit Daniel Kachhap | b8d582b | 2013-06-24 16:20:31 +0530 | [diff] [blame] | 220 | data->base + reg->threshold_th1); |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 221 | |
Naveen Krishna Chatradhi | 74429c2 | 2013-12-19 11:35:39 +0530 | [diff] [blame] | 222 | writel((reg->intclr_rise_mask << reg->intclr_rise_shift) | |
| 223 | (reg->intclr_fall_mask << reg->intclr_fall_shift), |
Amit Daniel Kachhap | b8d582b | 2013-06-24 16:20:31 +0530 | [diff] [blame] | 224 | data->base + reg->tmu_intclear); |
Amit Daniel Kachhap | 7ca04e5 | 2013-06-24 16:20:32 +0530 | [diff] [blame] | 225 | |
| 226 | /* if last threshold limit is also present */ |
| 227 | i = pdata->max_trigger_level - 1; |
| 228 | if (pdata->trigger_levels[i] && |
| 229 | (pdata->trigger_type[i] == HW_TRIP)) { |
| 230 | threshold_code = temp_to_code(data, |
| 231 | pdata->trigger_levels[i]); |
Amit Daniel Kachhap | a0395ee | 2013-06-24 16:20:43 +0530 | [diff] [blame] | 232 | if (i == EXYNOS_MAX_TRIGGER_PER_REG - 1) { |
| 233 | /* 1-4 level to be assigned in th0 reg */ |
Tushar Behera | c65d347 | 2014-04-14 11:08:15 +0530 | [diff] [blame] | 234 | rising_threshold &= ~(0xff << 8 * i); |
Amit Daniel Kachhap | a0395ee | 2013-06-24 16:20:43 +0530 | [diff] [blame] | 235 | rising_threshold |= threshold_code << 8 * i; |
| 236 | writel(rising_threshold, |
| 237 | data->base + reg->threshold_th0); |
| 238 | } else if (i == EXYNOS_MAX_TRIGGER_PER_REG) { |
| 239 | /* 5th level to be assigned in th2 reg */ |
| 240 | rising_threshold = |
| 241 | threshold_code << reg->threshold_th3_l0_shift; |
| 242 | writel(rising_threshold, |
| 243 | data->base + reg->threshold_th2); |
| 244 | } |
Amit Daniel Kachhap | 7ca04e5 | 2013-06-24 16:20:32 +0530 | [diff] [blame] | 245 | con = readl(data->base + reg->tmu_ctrl); |
| 246 | con |= (1 << reg->therm_trip_en_shift); |
| 247 | writel(con, data->base + reg->tmu_ctrl); |
| 248 | } |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 249 | } |
Amit Daniel Kachhap | a0395ee | 2013-06-24 16:20:43 +0530 | [diff] [blame] | 250 | /*Clear the PMIN in the common TMU register*/ |
| 251 | if (reg->tmu_pmin && !data->id) |
Naveen Krishna Chatradhi | 9025d56 | 2013-12-19 11:36:08 +0530 | [diff] [blame] | 252 | writel(0, data->base_second + reg->tmu_pmin); |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 253 | out: |
| 254 | clk_disable(data->clk); |
| 255 | mutex_unlock(&data->lock); |
Naveen Krishna Chatradhi | 14a11dc | 2013-12-19 11:36:31 +0530 | [diff] [blame] | 256 | if (!IS_ERR(data->clk_sec)) |
| 257 | clk_disable(data->clk_sec); |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 258 | |
| 259 | return ret; |
| 260 | } |
| 261 | |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 262 | static void exynos_tmu_control(struct platform_device *pdev, bool on) |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 263 | { |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 264 | struct exynos_tmu_data *data = platform_get_drvdata(pdev); |
| 265 | struct exynos_tmu_platform_data *pdata = data->pdata; |
Amit Daniel Kachhap | b8d582b | 2013-06-24 16:20:31 +0530 | [diff] [blame] | 266 | const struct exynos_tmu_registers *reg = pdata->registers; |
Bartlomiej Zolnierkiewicz | d37761e | 2014-07-31 19:11:00 +0200 | [diff] [blame] | 267 | unsigned int con, interrupt_en; |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 268 | |
| 269 | mutex_lock(&data->lock); |
| 270 | clk_enable(data->clk); |
| 271 | |
Amit Daniel Kachhap | b8d582b | 2013-06-24 16:20:31 +0530 | [diff] [blame] | 272 | con = readl(data->base + reg->tmu_ctrl); |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 273 | |
Lukasz Majewski | 86f5362 | 2013-10-09 08:29:52 +0200 | [diff] [blame] | 274 | if (pdata->test_mux) |
| 275 | con |= (pdata->test_mux << reg->test_mux_addr_shift); |
| 276 | |
Amit Daniel Kachhap | d0a0ce3 | 2013-06-24 16:20:29 +0530 | [diff] [blame] | 277 | if (pdata->reference_voltage) { |
Amit Daniel Kachhap | b8d582b | 2013-06-24 16:20:31 +0530 | [diff] [blame] | 278 | con &= ~(reg->buf_vref_sel_mask << reg->buf_vref_sel_shift); |
| 279 | con |= pdata->reference_voltage << reg->buf_vref_sel_shift; |
Amit Daniel Kachhap | d0a0ce3 | 2013-06-24 16:20:29 +0530 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | if (pdata->gain) { |
Amit Daniel Kachhap | b8d582b | 2013-06-24 16:20:31 +0530 | [diff] [blame] | 283 | con &= ~(reg->buf_slope_sel_mask << reg->buf_slope_sel_shift); |
| 284 | con |= (pdata->gain << reg->buf_slope_sel_shift); |
Amit Daniel Kachhap | d0a0ce3 | 2013-06-24 16:20:29 +0530 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | if (pdata->noise_cancel_mode) { |
Amit Daniel Kachhap | b8d582b | 2013-06-24 16:20:31 +0530 | [diff] [blame] | 288 | con &= ~(reg->therm_trip_mode_mask << |
| 289 | reg->therm_trip_mode_shift); |
| 290 | con |= (pdata->noise_cancel_mode << reg->therm_trip_mode_shift); |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 291 | } |
| 292 | |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 293 | if (on) { |
Amit Daniel Kachhap | b8d582b | 2013-06-24 16:20:31 +0530 | [diff] [blame] | 294 | con |= (1 << reg->core_en_shift); |
Amit Daniel Kachhap | d0a0ce3 | 2013-06-24 16:20:29 +0530 | [diff] [blame] | 295 | interrupt_en = |
Amit Daniel Kachhap | b8d582b | 2013-06-24 16:20:31 +0530 | [diff] [blame] | 296 | pdata->trigger_enable[3] << reg->inten_rise3_shift | |
| 297 | pdata->trigger_enable[2] << reg->inten_rise2_shift | |
| 298 | pdata->trigger_enable[1] << reg->inten_rise1_shift | |
| 299 | pdata->trigger_enable[0] << reg->inten_rise0_shift; |
Amit Daniel Kachhap | f4dae75 | 2013-06-24 16:20:40 +0530 | [diff] [blame] | 300 | if (TMU_SUPPORTS(pdata, FALLING_TRIP)) |
Amit Daniel Kachhap | d0a0ce3 | 2013-06-24 16:20:29 +0530 | [diff] [blame] | 301 | interrupt_en |= |
Amit Daniel Kachhap | b8d582b | 2013-06-24 16:20:31 +0530 | [diff] [blame] | 302 | interrupt_en << reg->inten_fall0_shift; |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 303 | } else { |
Amit Daniel Kachhap | b8d582b | 2013-06-24 16:20:31 +0530 | [diff] [blame] | 304 | con &= ~(1 << reg->core_en_shift); |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 305 | interrupt_en = 0; /* Disable all interrupts */ |
| 306 | } |
Amit Daniel Kachhap | b8d582b | 2013-06-24 16:20:31 +0530 | [diff] [blame] | 307 | writel(interrupt_en, data->base + reg->tmu_inten); |
| 308 | writel(con, data->base + reg->tmu_ctrl); |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 309 | |
| 310 | clk_disable(data->clk); |
| 311 | mutex_unlock(&data->lock); |
| 312 | } |
| 313 | |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 314 | static int exynos_tmu_read(struct exynos_tmu_data *data) |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 315 | { |
Amit Daniel Kachhap | b8d582b | 2013-06-24 16:20:31 +0530 | [diff] [blame] | 316 | struct exynos_tmu_platform_data *pdata = data->pdata; |
| 317 | const struct exynos_tmu_registers *reg = pdata->registers; |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 318 | u8 temp_code; |
| 319 | int temp; |
| 320 | |
| 321 | mutex_lock(&data->lock); |
| 322 | clk_enable(data->clk); |
| 323 | |
Amit Daniel Kachhap | b8d582b | 2013-06-24 16:20:31 +0530 | [diff] [blame] | 324 | temp_code = readb(data->base + reg->tmu_cur_temp); |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 325 | |
Bartlomiej Zolnierkiewicz | ddb31d4 | 2014-07-31 19:11:03 +0200 | [diff] [blame] | 326 | if (data->soc == SOC_ARCH_EXYNOS4210) |
| 327 | /* temp_code should range between 75 and 175 */ |
| 328 | if (temp_code < 75 || temp_code > 175) { |
| 329 | temp = -ENODATA; |
| 330 | goto out; |
| 331 | } |
| 332 | |
| 333 | temp = code_to_temp(data, temp_code); |
| 334 | out: |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 335 | clk_disable(data->clk); |
| 336 | mutex_unlock(&data->lock); |
| 337 | |
| 338 | return temp; |
| 339 | } |
| 340 | |
Amit Daniel Kachhap | bffd1f8 | 2013-02-11 03:54:23 +0000 | [diff] [blame] | 341 | #ifdef CONFIG_THERMAL_EMULATION |
| 342 | static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp) |
| 343 | { |
| 344 | struct exynos_tmu_data *data = drv_data; |
Amit Daniel Kachhap | b8d582b | 2013-06-24 16:20:31 +0530 | [diff] [blame] | 345 | struct exynos_tmu_platform_data *pdata = data->pdata; |
| 346 | const struct exynos_tmu_registers *reg = pdata->registers; |
| 347 | unsigned int val; |
Amit Daniel Kachhap | bffd1f8 | 2013-02-11 03:54:23 +0000 | [diff] [blame] | 348 | int ret = -EINVAL; |
| 349 | |
Amit Daniel Kachhap | f4dae75 | 2013-06-24 16:20:40 +0530 | [diff] [blame] | 350 | if (!TMU_SUPPORTS(pdata, EMULATION)) |
Amit Daniel Kachhap | bffd1f8 | 2013-02-11 03:54:23 +0000 | [diff] [blame] | 351 | goto out; |
| 352 | |
| 353 | if (temp && temp < MCELSIUS) |
| 354 | goto out; |
| 355 | |
| 356 | mutex_lock(&data->lock); |
| 357 | clk_enable(data->clk); |
| 358 | |
Amit Daniel Kachhap | b8d582b | 2013-06-24 16:20:31 +0530 | [diff] [blame] | 359 | val = readl(data->base + reg->emul_con); |
Amit Daniel Kachhap | bffd1f8 | 2013-02-11 03:54:23 +0000 | [diff] [blame] | 360 | |
| 361 | if (temp) { |
| 362 | temp /= MCELSIUS; |
| 363 | |
Amit Daniel Kachhap | f4dae75 | 2013-06-24 16:20:40 +0530 | [diff] [blame] | 364 | if (TMU_SUPPORTS(pdata, EMUL_TIME)) { |
| 365 | val &= ~(EXYNOS_EMUL_TIME_MASK << reg->emul_time_shift); |
| 366 | val |= (EXYNOS_EMUL_TIME << reg->emul_time_shift); |
| 367 | } |
| 368 | val &= ~(EXYNOS_EMUL_DATA_MASK << reg->emul_temp_shift); |
| 369 | val |= (temp_to_code(data, temp) << reg->emul_temp_shift) | |
| 370 | EXYNOS_EMUL_ENABLE; |
Amit Daniel Kachhap | bffd1f8 | 2013-02-11 03:54:23 +0000 | [diff] [blame] | 371 | } else { |
Amit Daniel Kachhap | b8d582b | 2013-06-24 16:20:31 +0530 | [diff] [blame] | 372 | val &= ~EXYNOS_EMUL_ENABLE; |
Amit Daniel Kachhap | bffd1f8 | 2013-02-11 03:54:23 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Amit Daniel Kachhap | b8d582b | 2013-06-24 16:20:31 +0530 | [diff] [blame] | 375 | writel(val, data->base + reg->emul_con); |
Amit Daniel Kachhap | bffd1f8 | 2013-02-11 03:54:23 +0000 | [diff] [blame] | 376 | |
| 377 | clk_disable(data->clk); |
| 378 | mutex_unlock(&data->lock); |
| 379 | return 0; |
| 380 | out: |
| 381 | return ret; |
| 382 | } |
| 383 | #else |
| 384 | static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp) |
| 385 | { return -EINVAL; } |
| 386 | #endif/*CONFIG_THERMAL_EMULATION*/ |
| 387 | |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 388 | static void exynos_tmu_work(struct work_struct *work) |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 389 | { |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 390 | struct exynos_tmu_data *data = container_of(work, |
| 391 | struct exynos_tmu_data, irq_work); |
Amit Daniel Kachhap | b8d582b | 2013-06-24 16:20:31 +0530 | [diff] [blame] | 392 | struct exynos_tmu_platform_data *pdata = data->pdata; |
| 393 | const struct exynos_tmu_registers *reg = pdata->registers; |
Amit Daniel Kachhap | a0395ee | 2013-06-24 16:20:43 +0530 | [diff] [blame] | 394 | unsigned int val_irq, val_type; |
| 395 | |
Naveen Krishna Chatradhi | 14a11dc | 2013-12-19 11:36:31 +0530 | [diff] [blame] | 396 | if (!IS_ERR(data->clk_sec)) |
| 397 | clk_enable(data->clk_sec); |
Amit Daniel Kachhap | a0395ee | 2013-06-24 16:20:43 +0530 | [diff] [blame] | 398 | /* Find which sensor generated this interrupt */ |
| 399 | if (reg->tmu_irqstatus) { |
Naveen Krishna Chatradhi | 9025d56 | 2013-12-19 11:36:08 +0530 | [diff] [blame] | 400 | val_type = readl(data->base_second + reg->tmu_irqstatus); |
Amit Daniel Kachhap | a0395ee | 2013-06-24 16:20:43 +0530 | [diff] [blame] | 401 | if (!((val_type >> data->id) & 0x1)) |
| 402 | goto out; |
| 403 | } |
Naveen Krishna Chatradhi | 14a11dc | 2013-12-19 11:36:31 +0530 | [diff] [blame] | 404 | if (!IS_ERR(data->clk_sec)) |
| 405 | clk_disable(data->clk_sec); |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 406 | |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 407 | exynos_report_trigger(data->reg_conf); |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 408 | mutex_lock(&data->lock); |
| 409 | clk_enable(data->clk); |
Amit Daniel Kachhap | b8d582b | 2013-06-24 16:20:31 +0530 | [diff] [blame] | 410 | |
Amit Daniel Kachhap | a4463c4 | 2013-06-24 16:20:33 +0530 | [diff] [blame] | 411 | /* TODO: take action based on particular interrupt */ |
| 412 | val_irq = readl(data->base + reg->tmu_intstat); |
| 413 | /* clear the interrupts */ |
| 414 | writel(val_irq, data->base + reg->tmu_intclear); |
Amit Daniel Kachhap | b8d582b | 2013-06-24 16:20:31 +0530 | [diff] [blame] | 415 | |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 416 | clk_disable(data->clk); |
| 417 | mutex_unlock(&data->lock); |
Amit Daniel Kachhap | a0395ee | 2013-06-24 16:20:43 +0530 | [diff] [blame] | 418 | out: |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 419 | enable_irq(data->irq); |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 420 | } |
| 421 | |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 422 | static irqreturn_t exynos_tmu_irq(int irq, void *id) |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 423 | { |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 424 | struct exynos_tmu_data *data = id; |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 425 | |
| 426 | disable_irq_nosync(irq); |
| 427 | schedule_work(&data->irq_work); |
| 428 | |
| 429 | return IRQ_HANDLED; |
| 430 | } |
Amit Daniel Kachhap | 17be868 | 2012-08-16 17:11:44 +0530 | [diff] [blame] | 431 | |
Amit Daniel Kachhap | 17be868 | 2012-08-16 17:11:44 +0530 | [diff] [blame] | 432 | static const struct of_device_id exynos_tmu_match[] = { |
| 433 | { |
Chanwoo Choi | 1fe56dc | 2014-07-01 09:33:19 +0900 | [diff] [blame] | 434 | .compatible = "samsung,exynos3250-tmu", |
| 435 | .data = (void *)EXYNOS3250_TMU_DRV_DATA, |
| 436 | }, |
| 437 | { |
Amit Daniel Kachhap | 17be868 | 2012-08-16 17:11:44 +0530 | [diff] [blame] | 438 | .compatible = "samsung,exynos4210-tmu", |
| 439 | .data = (void *)EXYNOS4210_TMU_DRV_DATA, |
| 440 | }, |
| 441 | { |
Sachin Kamat | b6cee53 | 2013-04-18 11:37:59 +0000 | [diff] [blame] | 442 | .compatible = "samsung,exynos4412-tmu", |
Lukasz Majewski | 14ddfae | 2013-10-09 08:29:51 +0200 | [diff] [blame] | 443 | .data = (void *)EXYNOS4412_TMU_DRV_DATA, |
Sachin Kamat | b6cee53 | 2013-04-18 11:37:59 +0000 | [diff] [blame] | 444 | }, |
| 445 | { |
Amit Daniel Kachhap | 17be868 | 2012-08-16 17:11:44 +0530 | [diff] [blame] | 446 | .compatible = "samsung,exynos5250-tmu", |
Amit Daniel Kachhap | e6b7991 | 2013-06-24 16:20:28 +0530 | [diff] [blame] | 447 | .data = (void *)EXYNOS5250_TMU_DRV_DATA, |
Amit Daniel Kachhap | 17be868 | 2012-08-16 17:11:44 +0530 | [diff] [blame] | 448 | }, |
Amit Daniel Kachhap | 9054254 | 2013-06-24 16:20:44 +0530 | [diff] [blame] | 449 | { |
Naveen Krishna Chatradhi | 923488a | 2013-12-20 17:49:10 +0530 | [diff] [blame] | 450 | .compatible = "samsung,exynos5260-tmu", |
| 451 | .data = (void *)EXYNOS5260_TMU_DRV_DATA, |
| 452 | }, |
| 453 | { |
Naveen Krishna Chatradhi | 14a11dc | 2013-12-19 11:36:31 +0530 | [diff] [blame] | 454 | .compatible = "samsung,exynos5420-tmu", |
| 455 | .data = (void *)EXYNOS5420_TMU_DRV_DATA, |
| 456 | }, |
| 457 | { |
| 458 | .compatible = "samsung,exynos5420-tmu-ext-triminfo", |
| 459 | .data = (void *)EXYNOS5420_TMU_DRV_DATA, |
| 460 | }, |
| 461 | { |
Amit Daniel Kachhap | 9054254 | 2013-06-24 16:20:44 +0530 | [diff] [blame] | 462 | .compatible = "samsung,exynos5440-tmu", |
| 463 | .data = (void *)EXYNOS5440_TMU_DRV_DATA, |
| 464 | }, |
Amit Daniel Kachhap | 17be868 | 2012-08-16 17:11:44 +0530 | [diff] [blame] | 465 | {}, |
| 466 | }; |
| 467 | MODULE_DEVICE_TABLE(of, exynos_tmu_match); |
Amit Daniel Kachhap | 17be868 | 2012-08-16 17:11:44 +0530 | [diff] [blame] | 468 | |
Amit Daniel Kachhap | 17be868 | 2012-08-16 17:11:44 +0530 | [diff] [blame] | 469 | static inline struct exynos_tmu_platform_data *exynos_get_driver_data( |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 470 | struct platform_device *pdev, int id) |
Amit Daniel Kachhap | 17be868 | 2012-08-16 17:11:44 +0530 | [diff] [blame] | 471 | { |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 472 | struct exynos_tmu_init_data *data_table; |
| 473 | struct exynos_tmu_platform_data *tmu_data; |
Sachin Kamat | 73b5b1d | 2013-08-19 11:58:43 +0530 | [diff] [blame] | 474 | const struct of_device_id *match; |
| 475 | |
| 476 | match = of_match_node(exynos_tmu_match, pdev->dev.of_node); |
| 477 | if (!match) |
| 478 | return NULL; |
| 479 | data_table = (struct exynos_tmu_init_data *) match->data; |
| 480 | if (!data_table || id >= data_table->tmu_count) |
| 481 | return NULL; |
| 482 | tmu_data = data_table->tmu_data; |
| 483 | return (struct exynos_tmu_platform_data *) (tmu_data + id); |
Amit Daniel Kachhap | 7e0b55e | 2012-08-16 17:11:43 +0530 | [diff] [blame] | 484 | } |
Jonghwa Lee | bbf63be | 2012-11-21 13:31:01 +0900 | [diff] [blame] | 485 | |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 486 | static int exynos_map_dt_data(struct platform_device *pdev) |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 487 | { |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 488 | struct exynos_tmu_data *data = platform_get_drvdata(pdev); |
| 489 | struct exynos_tmu_platform_data *pdata; |
| 490 | struct resource res; |
Amit Daniel Kachhap | 498d22f | 2013-06-24 16:20:47 +0530 | [diff] [blame] | 491 | int ret; |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 492 | |
Sachin Kamat | 73b5b1d | 2013-08-19 11:58:43 +0530 | [diff] [blame] | 493 | if (!data || !pdev->dev.of_node) |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 494 | return -ENODEV; |
Amit Daniel Kachhap | 17be868 | 2012-08-16 17:11:44 +0530 | [diff] [blame] | 495 | |
Amit Daniel Kachhap | 498d22f | 2013-06-24 16:20:47 +0530 | [diff] [blame] | 496 | /* |
| 497 | * Try enabling the regulator if found |
| 498 | * TODO: Add regulator as an SOC feature, so that regulator enable |
| 499 | * is a compulsory call. |
| 500 | */ |
| 501 | data->regulator = devm_regulator_get(&pdev->dev, "vtmu"); |
| 502 | if (!IS_ERR(data->regulator)) { |
| 503 | ret = regulator_enable(data->regulator); |
| 504 | if (ret) { |
| 505 | dev_err(&pdev->dev, "failed to enable vtmu\n"); |
| 506 | return ret; |
| 507 | } |
| 508 | } else { |
| 509 | dev_info(&pdev->dev, "Regulator node (vtmu) not found\n"); |
| 510 | } |
| 511 | |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 512 | data->id = of_alias_get_id(pdev->dev.of_node, "tmuctrl"); |
| 513 | if (data->id < 0) |
| 514 | data->id = 0; |
| 515 | |
| 516 | data->irq = irq_of_parse_and_map(pdev->dev.of_node, 0); |
| 517 | if (data->irq <= 0) { |
| 518 | dev_err(&pdev->dev, "failed to get IRQ\n"); |
| 519 | return -ENODEV; |
| 520 | } |
| 521 | |
| 522 | if (of_address_to_resource(pdev->dev.of_node, 0, &res)) { |
| 523 | dev_err(&pdev->dev, "failed to get Resource 0\n"); |
| 524 | return -ENODEV; |
| 525 | } |
| 526 | |
| 527 | data->base = devm_ioremap(&pdev->dev, res.start, resource_size(&res)); |
| 528 | if (!data->base) { |
| 529 | dev_err(&pdev->dev, "Failed to ioremap memory\n"); |
| 530 | return -EADDRNOTAVAIL; |
| 531 | } |
| 532 | |
| 533 | pdata = exynos_get_driver_data(pdev, data->id); |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 534 | if (!pdata) { |
| 535 | dev_err(&pdev->dev, "No platform init data supplied.\n"); |
| 536 | return -ENODEV; |
| 537 | } |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 538 | data->pdata = pdata; |
Amit Daniel Kachhap | d9b6ee1 | 2013-06-24 16:20:42 +0530 | [diff] [blame] | 539 | /* |
| 540 | * Check if the TMU shares some registers and then try to map the |
| 541 | * memory of common registers. |
| 542 | */ |
Naveen Krishna Chatradhi | 9025d56 | 2013-12-19 11:36:08 +0530 | [diff] [blame] | 543 | if (!TMU_SUPPORTS(pdata, ADDRESS_MULTIPLE)) |
Amit Daniel Kachhap | d9b6ee1 | 2013-06-24 16:20:42 +0530 | [diff] [blame] | 544 | return 0; |
| 545 | |
| 546 | if (of_address_to_resource(pdev->dev.of_node, 1, &res)) { |
| 547 | dev_err(&pdev->dev, "failed to get Resource 1\n"); |
| 548 | return -ENODEV; |
| 549 | } |
| 550 | |
Naveen Krishna Chatradhi | 9025d56 | 2013-12-19 11:36:08 +0530 | [diff] [blame] | 551 | data->base_second = devm_ioremap(&pdev->dev, res.start, |
Amit Daniel Kachhap | d9b6ee1 | 2013-06-24 16:20:42 +0530 | [diff] [blame] | 552 | resource_size(&res)); |
Naveen Krishna Chatradhi | 9025d56 | 2013-12-19 11:36:08 +0530 | [diff] [blame] | 553 | if (!data->base_second) { |
Amit Daniel Kachhap | d9b6ee1 | 2013-06-24 16:20:42 +0530 | [diff] [blame] | 554 | dev_err(&pdev->dev, "Failed to ioremap memory\n"); |
| 555 | return -ENOMEM; |
| 556 | } |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 557 | |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | static int exynos_tmu_probe(struct platform_device *pdev) |
| 562 | { |
| 563 | struct exynos_tmu_data *data; |
| 564 | struct exynos_tmu_platform_data *pdata; |
| 565 | struct thermal_sensor_conf *sensor_conf; |
| 566 | int ret, i; |
| 567 | |
Amit Daniel Kachhap | 79e093c | 2012-08-16 05:41:45 -0600 | [diff] [blame] | 568 | data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data), |
| 569 | GFP_KERNEL); |
Jingoo Han | 2a9675b | 2014-05-07 15:04:48 +0900 | [diff] [blame] | 570 | if (!data) |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 571 | return -ENOMEM; |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 572 | |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 573 | platform_set_drvdata(pdev, data); |
| 574 | mutex_init(&data->lock); |
| 575 | |
| 576 | ret = exynos_map_dt_data(pdev); |
| 577 | if (ret) |
| 578 | return ret; |
| 579 | |
| 580 | pdata = data->pdata; |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 581 | |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 582 | INIT_WORK(&data->irq_work, exynos_tmu_work); |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 583 | |
Sachin Kamat | 2a16279 | 2013-04-18 11:37:58 +0000 | [diff] [blame] | 584 | data->clk = devm_clk_get(&pdev->dev, "tmu_apbif"); |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 585 | if (IS_ERR(data->clk)) { |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 586 | dev_err(&pdev->dev, "Failed to get clock\n"); |
Amit Daniel Kachhap | 79e093c | 2012-08-16 05:41:45 -0600 | [diff] [blame] | 587 | return PTR_ERR(data->clk); |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 588 | } |
| 589 | |
Naveen Krishna Chatradhi | 14a11dc | 2013-12-19 11:36:31 +0530 | [diff] [blame] | 590 | data->clk_sec = devm_clk_get(&pdev->dev, "tmu_triminfo_apbif"); |
| 591 | if (IS_ERR(data->clk_sec)) { |
| 592 | if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO) { |
| 593 | dev_err(&pdev->dev, "Failed to get triminfo clock\n"); |
| 594 | return PTR_ERR(data->clk_sec); |
| 595 | } |
| 596 | } else { |
| 597 | ret = clk_prepare(data->clk_sec); |
| 598 | if (ret) { |
| 599 | dev_err(&pdev->dev, "Failed to get clock\n"); |
| 600 | return ret; |
| 601 | } |
| 602 | } |
| 603 | |
Sachin Kamat | 2a16279 | 2013-04-18 11:37:58 +0000 | [diff] [blame] | 604 | ret = clk_prepare(data->clk); |
Naveen Krishna Chatradhi | 14a11dc | 2013-12-19 11:36:31 +0530 | [diff] [blame] | 605 | if (ret) { |
| 606 | dev_err(&pdev->dev, "Failed to get clock\n"); |
| 607 | goto err_clk_sec; |
| 608 | } |
Sachin Kamat | 2a16279 | 2013-04-18 11:37:58 +0000 | [diff] [blame] | 609 | |
Chanwoo Choi | 1fe56dc | 2014-07-01 09:33:19 +0900 | [diff] [blame] | 610 | if (pdata->type == SOC_ARCH_EXYNOS3250 || |
| 611 | pdata->type == SOC_ARCH_EXYNOS4210 || |
Lukasz Majewski | 14ddfae | 2013-10-09 08:29:51 +0200 | [diff] [blame] | 612 | pdata->type == SOC_ARCH_EXYNOS4412 || |
| 613 | pdata->type == SOC_ARCH_EXYNOS5250 || |
Naveen Krishna Chatradhi | 923488a | 2013-12-20 17:49:10 +0530 | [diff] [blame] | 614 | pdata->type == SOC_ARCH_EXYNOS5260 || |
Naveen Krishna Chatradhi | 14a11dc | 2013-12-19 11:36:31 +0530 | [diff] [blame] | 615 | pdata->type == SOC_ARCH_EXYNOS5420_TRIMINFO || |
Lukasz Majewski | 14ddfae | 2013-10-09 08:29:51 +0200 | [diff] [blame] | 616 | pdata->type == SOC_ARCH_EXYNOS5440) |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 617 | data->soc = pdata->type; |
| 618 | else { |
| 619 | ret = -EINVAL; |
| 620 | dev_err(&pdev->dev, "Platform not supported\n"); |
| 621 | goto err_clk; |
| 622 | } |
| 623 | |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 624 | ret = exynos_tmu_initialize(pdev); |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 625 | if (ret) { |
| 626 | dev_err(&pdev->dev, "Failed to initialize TMU\n"); |
| 627 | goto err_clk; |
| 628 | } |
| 629 | |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 630 | exynos_tmu_control(pdev, true); |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 631 | |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 632 | /* Allocate a structure to register with the exynos core thermal */ |
| 633 | sensor_conf = devm_kzalloc(&pdev->dev, |
| 634 | sizeof(struct thermal_sensor_conf), GFP_KERNEL); |
| 635 | if (!sensor_conf) { |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 636 | ret = -ENOMEM; |
| 637 | goto err_clk; |
| 638 | } |
| 639 | sprintf(sensor_conf->name, "therm_zone%d", data->id); |
| 640 | sensor_conf->read_temperature = (int (*)(void *))exynos_tmu_read; |
| 641 | sensor_conf->write_emul_temp = |
| 642 | (int (*)(void *, unsigned long))exynos_tmu_set_emulation; |
| 643 | sensor_conf->driver_data = data; |
| 644 | sensor_conf->trip_data.trip_count = pdata->trigger_enable[0] + |
Amit Daniel Kachhap | bb34b4c | 2013-06-24 16:20:30 +0530 | [diff] [blame] | 645 | pdata->trigger_enable[1] + pdata->trigger_enable[2]+ |
| 646 | pdata->trigger_enable[3]; |
Amit Daniel Kachhap | 7e0b55e | 2012-08-16 17:11:43 +0530 | [diff] [blame] | 647 | |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 648 | for (i = 0; i < sensor_conf->trip_data.trip_count; i++) { |
| 649 | sensor_conf->trip_data.trip_val[i] = |
Amit Daniel Kachhap | 7e0b55e | 2012-08-16 17:11:43 +0530 | [diff] [blame] | 650 | pdata->threshold + pdata->trigger_levels[i]; |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 651 | sensor_conf->trip_data.trip_type[i] = |
Amit Daniel Kachhap | 5c3cf55 | 2013-06-24 16:20:37 +0530 | [diff] [blame] | 652 | pdata->trigger_type[i]; |
| 653 | } |
Amit Daniel Kachhap | 7e0b55e | 2012-08-16 17:11:43 +0530 | [diff] [blame] | 654 | |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 655 | sensor_conf->trip_data.trigger_falling = pdata->threshold_falling; |
Jonghwa Lee | 4f0a684 | 2013-02-08 01:13:06 +0000 | [diff] [blame] | 656 | |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 657 | sensor_conf->cooling_data.freq_clip_count = pdata->freq_tab_count; |
Amit Daniel Kachhap | 7e0b55e | 2012-08-16 17:11:43 +0530 | [diff] [blame] | 658 | for (i = 0; i < pdata->freq_tab_count; i++) { |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 659 | sensor_conf->cooling_data.freq_data[i].freq_clip_max = |
Amit Daniel Kachhap | 7e0b55e | 2012-08-16 17:11:43 +0530 | [diff] [blame] | 660 | pdata->freq_tab[i].freq_clip_max; |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 661 | sensor_conf->cooling_data.freq_data[i].temp_level = |
Amit Daniel Kachhap | 7e0b55e | 2012-08-16 17:11:43 +0530 | [diff] [blame] | 662 | pdata->freq_tab[i].temp_level; |
| 663 | } |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 664 | sensor_conf->dev = &pdev->dev; |
| 665 | /* Register the sensor with thermal management interface */ |
| 666 | ret = exynos_register_thermal(sensor_conf); |
Amit Daniel Kachhap | 7e0b55e | 2012-08-16 17:11:43 +0530 | [diff] [blame] | 667 | if (ret) { |
| 668 | dev_err(&pdev->dev, "Failed to register thermal interface\n"); |
| 669 | goto err_clk; |
| 670 | } |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 671 | data->reg_conf = sensor_conf; |
| 672 | |
| 673 | ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq, |
| 674 | IRQF_TRIGGER_RISING | IRQF_SHARED, dev_name(&pdev->dev), data); |
| 675 | if (ret) { |
| 676 | dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq); |
| 677 | goto err_clk; |
| 678 | } |
Jonghwa Lee | bbf63be | 2012-11-21 13:31:01 +0900 | [diff] [blame] | 679 | |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 680 | return 0; |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 681 | err_clk: |
Sachin Kamat | 2a16279 | 2013-04-18 11:37:58 +0000 | [diff] [blame] | 682 | clk_unprepare(data->clk); |
Naveen Krishna Chatradhi | 14a11dc | 2013-12-19 11:36:31 +0530 | [diff] [blame] | 683 | err_clk_sec: |
| 684 | if (!IS_ERR(data->clk_sec)) |
| 685 | clk_unprepare(data->clk_sec); |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 686 | return ret; |
| 687 | } |
| 688 | |
Greg Kroah-Hartman | 4eab7a9 | 2012-12-21 13:15:52 -0800 | [diff] [blame] | 689 | static int exynos_tmu_remove(struct platform_device *pdev) |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 690 | { |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 691 | struct exynos_tmu_data *data = platform_get_drvdata(pdev); |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 692 | |
Amit Daniel Kachhap | cebe737 | 2013-06-24 16:20:39 +0530 | [diff] [blame] | 693 | exynos_unregister_thermal(data->reg_conf); |
Amit Daniel Kachhap | 7e0b55e | 2012-08-16 17:11:43 +0530 | [diff] [blame] | 694 | |
Bartlomiej Zolnierkiewicz | 4215688 | 2014-07-08 15:09:56 +0200 | [diff] [blame] | 695 | exynos_tmu_control(pdev, false); |
| 696 | |
Sachin Kamat | 2a16279 | 2013-04-18 11:37:58 +0000 | [diff] [blame] | 697 | clk_unprepare(data->clk); |
Naveen Krishna Chatradhi | 14a11dc | 2013-12-19 11:36:31 +0530 | [diff] [blame] | 698 | if (!IS_ERR(data->clk_sec)) |
| 699 | clk_unprepare(data->clk_sec); |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 700 | |
Amit Daniel Kachhap | 498d22f | 2013-06-24 16:20:47 +0530 | [diff] [blame] | 701 | if (!IS_ERR(data->regulator)) |
| 702 | regulator_disable(data->regulator); |
| 703 | |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 704 | return 0; |
| 705 | } |
| 706 | |
Rafael J. Wysocki | 08cd675 | 2012-07-08 21:48:15 +0200 | [diff] [blame] | 707 | #ifdef CONFIG_PM_SLEEP |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 708 | static int exynos_tmu_suspend(struct device *dev) |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 709 | { |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 710 | exynos_tmu_control(to_platform_device(dev), false); |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 711 | |
| 712 | return 0; |
| 713 | } |
| 714 | |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 715 | static int exynos_tmu_resume(struct device *dev) |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 716 | { |
Rafael J. Wysocki | 08cd675 | 2012-07-08 21:48:15 +0200 | [diff] [blame] | 717 | struct platform_device *pdev = to_platform_device(dev); |
| 718 | |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 719 | exynos_tmu_initialize(pdev); |
| 720 | exynos_tmu_control(pdev, true); |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 721 | |
| 722 | return 0; |
| 723 | } |
Rafael J. Wysocki | 08cd675 | 2012-07-08 21:48:15 +0200 | [diff] [blame] | 724 | |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 725 | static SIMPLE_DEV_PM_OPS(exynos_tmu_pm, |
| 726 | exynos_tmu_suspend, exynos_tmu_resume); |
| 727 | #define EXYNOS_TMU_PM (&exynos_tmu_pm) |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 728 | #else |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 729 | #define EXYNOS_TMU_PM NULL |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 730 | #endif |
| 731 | |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 732 | static struct platform_driver exynos_tmu_driver = { |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 733 | .driver = { |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 734 | .name = "exynos-tmu", |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 735 | .owner = THIS_MODULE, |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 736 | .pm = EXYNOS_TMU_PM, |
Sachin Kamat | 73b5b1d | 2013-08-19 11:58:43 +0530 | [diff] [blame] | 737 | .of_match_table = exynos_tmu_match, |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 738 | }, |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 739 | .probe = exynos_tmu_probe, |
Greg Kroah-Hartman | 4eab7a9 | 2012-12-21 13:15:52 -0800 | [diff] [blame] | 740 | .remove = exynos_tmu_remove, |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 741 | }; |
| 742 | |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 743 | module_platform_driver(exynos_tmu_driver); |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 744 | |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 745 | MODULE_DESCRIPTION("EXYNOS TMU Driver"); |
Donggeun Kim | 9d97e5c | 2011-09-07 18:49:08 +0900 | [diff] [blame] | 746 | MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>"); |
| 747 | MODULE_LICENSE("GPL"); |
Amit Daniel Kachhap | f22d9c0 | 2012-08-16 17:11:42 +0530 | [diff] [blame] | 748 | MODULE_ALIAS("platform:exynos-tmu"); |