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