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