Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms and conditions of the GNU General Public License, |
| 6 | * version 2, as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/clk.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/io.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/of.h> |
| 20 | #include <linux/of_address.h> |
| 21 | #include <linux/of_irq.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/reset.h> |
| 24 | #include <linux/thermal.h> |
Caesar Wang | c970872 | 2015-11-11 19:43:11 -0800 | [diff] [blame] | 25 | #include <linux/pinctrl/consumer.h> |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * If the temperature over a period of time High, |
| 29 | * the resulting TSHUT gave CRU module,let it reset the entire chip, |
| 30 | * or via GPIO give PMIC. |
| 31 | */ |
| 32 | enum tshut_mode { |
| 33 | TSHUT_MODE_CRU = 0, |
| 34 | TSHUT_MODE_GPIO, |
| 35 | }; |
| 36 | |
| 37 | /** |
| 38 | * the system Temperature Sensors tshut(tshut) polarity |
| 39 | * the bit 8 is tshut polarity. |
| 40 | * 0: low active, 1: high active |
| 41 | */ |
| 42 | enum tshut_polarity { |
| 43 | TSHUT_LOW_ACTIVE = 0, |
| 44 | TSHUT_HIGH_ACTIVE, |
| 45 | }; |
| 46 | |
| 47 | /** |
Caesar Wang | 1d98b61 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 48 | * The system has two Temperature Sensors. |
| 49 | * sensor0 is for CPU, and sensor1 is for GPU. |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 50 | */ |
| 51 | enum sensor_id { |
Caesar Wang | 1d98b61 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 52 | SENSOR_CPU = 0, |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 53 | SENSOR_GPU, |
| 54 | }; |
| 55 | |
Caesar Wang | 1d98b61 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 56 | /** |
| 57 | * The max sensors is two in rockchip SoCs. |
| 58 | * Two sensors: CPU and GPU sensor. |
| 59 | */ |
| 60 | #define SOC_MAX_SENSORS 2 |
| 61 | |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 62 | struct rockchip_tsadc_chip { |
Caesar Wang | 1d98b61 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 63 | /* The sensor id of chip correspond to the ADC channel */ |
| 64 | int chn_id[SOC_MAX_SENSORS]; |
| 65 | int chn_num; |
| 66 | |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 67 | /* The hardware-controlled tshut property */ |
| 68 | long tshut_temp; |
| 69 | enum tshut_mode tshut_mode; |
| 70 | enum tshut_polarity tshut_polarity; |
| 71 | |
| 72 | /* Chip-wide methods */ |
| 73 | void (*initialize)(void __iomem *reg, enum tshut_polarity p); |
| 74 | void (*irq_ack)(void __iomem *reg); |
| 75 | void (*control)(void __iomem *reg, bool on); |
| 76 | |
| 77 | /* Per-sensor methods */ |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 78 | int (*get_temp)(int chn, void __iomem *reg, int *temp); |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 79 | void (*set_tshut_temp)(int chn, void __iomem *reg, long temp); |
| 80 | void (*set_tshut_mode)(int chn, void __iomem *reg, enum tshut_mode m); |
| 81 | }; |
| 82 | |
| 83 | struct rockchip_thermal_sensor { |
| 84 | struct rockchip_thermal_data *thermal; |
| 85 | struct thermal_zone_device *tzd; |
Caesar Wang | 1d98b61 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 86 | int id; |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 87 | }; |
| 88 | |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 89 | struct rockchip_thermal_data { |
| 90 | const struct rockchip_tsadc_chip *chip; |
| 91 | struct platform_device *pdev; |
| 92 | struct reset_control *reset; |
| 93 | |
Caesar Wang | 1d98b61 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 94 | struct rockchip_thermal_sensor sensors[SOC_MAX_SENSORS]; |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 95 | |
| 96 | struct clk *clk; |
| 97 | struct clk *pclk; |
| 98 | |
| 99 | void __iomem *regs; |
| 100 | |
| 101 | long tshut_temp; |
| 102 | enum tshut_mode tshut_mode; |
| 103 | enum tshut_polarity tshut_polarity; |
| 104 | }; |
| 105 | |
Caesar Wang | 1d98b61 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 106 | /* TSADC Sensor info define: */ |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 107 | #define TSADCV2_AUTO_CON 0x04 |
| 108 | #define TSADCV2_INT_EN 0x08 |
| 109 | #define TSADCV2_INT_PD 0x0c |
| 110 | #define TSADCV2_DATA(chn) (0x20 + (chn) * 0x04) |
| 111 | #define TSADCV2_COMP_SHUT(chn) (0x40 + (chn) * 0x04) |
| 112 | #define TSADCV2_HIGHT_INT_DEBOUNCE 0x60 |
| 113 | #define TSADCV2_HIGHT_TSHUT_DEBOUNCE 0x64 |
| 114 | #define TSADCV2_AUTO_PERIOD 0x68 |
| 115 | #define TSADCV2_AUTO_PERIOD_HT 0x6c |
| 116 | |
| 117 | #define TSADCV2_AUTO_EN BIT(0) |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 118 | #define TSADCV2_AUTO_SRC_EN(chn) BIT(4 + (chn)) |
| 119 | #define TSADCV2_AUTO_TSHUT_POLARITY_HIGH BIT(8) |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 120 | |
| 121 | #define TSADCV2_INT_SRC_EN(chn) BIT(chn) |
| 122 | #define TSADCV2_SHUT_2GPIO_SRC_EN(chn) BIT(4 + (chn)) |
| 123 | #define TSADCV2_SHUT_2CRU_SRC_EN(chn) BIT(8 + (chn)) |
| 124 | |
Dmitry Torokhov | 452e01b | 2015-08-07 14:00:52 -0700 | [diff] [blame] | 125 | #define TSADCV2_INT_PD_CLEAR_MASK ~BIT(8) |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 126 | |
| 127 | #define TSADCV2_DATA_MASK 0xfff |
| 128 | #define TSADCV2_HIGHT_INT_DEBOUNCE_COUNT 4 |
| 129 | #define TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT 4 |
| 130 | #define TSADCV2_AUTO_PERIOD_TIME 250 /* msec */ |
| 131 | #define TSADCV2_AUTO_PERIOD_HT_TIME 50 /* msec */ |
| 132 | |
| 133 | struct tsadc_table { |
Dmitry Torokhov | d9a241c | 2015-08-07 13:59:23 -0700 | [diff] [blame] | 134 | u32 code; |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 135 | long temp; |
| 136 | }; |
| 137 | |
| 138 | static const struct tsadc_table v2_code_table[] = { |
| 139 | {TSADCV2_DATA_MASK, -40000}, |
| 140 | {3800, -40000}, |
| 141 | {3792, -35000}, |
| 142 | {3783, -30000}, |
| 143 | {3774, -25000}, |
| 144 | {3765, -20000}, |
| 145 | {3756, -15000}, |
| 146 | {3747, -10000}, |
| 147 | {3737, -5000}, |
| 148 | {3728, 0}, |
| 149 | {3718, 5000}, |
| 150 | {3708, 10000}, |
| 151 | {3698, 15000}, |
| 152 | {3688, 20000}, |
| 153 | {3678, 25000}, |
| 154 | {3667, 30000}, |
| 155 | {3656, 35000}, |
| 156 | {3645, 40000}, |
| 157 | {3634, 45000}, |
| 158 | {3623, 50000}, |
| 159 | {3611, 55000}, |
| 160 | {3600, 60000}, |
| 161 | {3588, 65000}, |
| 162 | {3575, 70000}, |
| 163 | {3563, 75000}, |
| 164 | {3550, 80000}, |
| 165 | {3537, 85000}, |
| 166 | {3524, 90000}, |
| 167 | {3510, 95000}, |
| 168 | {3496, 100000}, |
| 169 | {3482, 105000}, |
| 170 | {3467, 110000}, |
| 171 | {3452, 115000}, |
| 172 | {3437, 120000}, |
| 173 | {3421, 125000}, |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | static u32 rk_tsadcv2_temp_to_code(long temp) |
| 177 | { |
| 178 | int high, low, mid; |
| 179 | |
| 180 | low = 0; |
| 181 | high = ARRAY_SIZE(v2_code_table) - 1; |
| 182 | mid = (high + low) / 2; |
| 183 | |
| 184 | if (temp < v2_code_table[low].temp || temp > v2_code_table[high].temp) |
| 185 | return 0; |
| 186 | |
| 187 | while (low <= high) { |
| 188 | if (temp == v2_code_table[mid].temp) |
| 189 | return v2_code_table[mid].code; |
| 190 | else if (temp < v2_code_table[mid].temp) |
| 191 | high = mid - 1; |
| 192 | else |
| 193 | low = mid + 1; |
| 194 | mid = (low + high) / 2; |
| 195 | } |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
Dmitry Torokhov | d9a241c | 2015-08-07 13:59:23 -0700 | [diff] [blame] | 200 | static int rk_tsadcv2_code_to_temp(u32 code, int *temp) |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 201 | { |
Dmitry Torokhov | d9a241c | 2015-08-07 13:59:23 -0700 | [diff] [blame] | 202 | unsigned int low = 1; |
Caesar Wang | 1e9a1ae | 2015-01-25 10:11:11 +0800 | [diff] [blame] | 203 | unsigned int high = ARRAY_SIZE(v2_code_table) - 1; |
| 204 | unsigned int mid = (low + high) / 2; |
| 205 | unsigned int num; |
| 206 | unsigned long denom; |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 207 | |
Dmitry Torokhov | d9a241c | 2015-08-07 13:59:23 -0700 | [diff] [blame] | 208 | BUILD_BUG_ON(ARRAY_SIZE(v2_code_table) < 2); |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 209 | |
Dmitry Torokhov | d9a241c | 2015-08-07 13:59:23 -0700 | [diff] [blame] | 210 | code &= TSADCV2_DATA_MASK; |
| 211 | if (code < v2_code_table[high].code) |
| 212 | return -EAGAIN; /* Incorrect reading */ |
| 213 | |
| 214 | while (low <= high) { |
Caesar Wang | 1e9a1ae | 2015-01-25 10:11:11 +0800 | [diff] [blame] | 215 | if (code >= v2_code_table[mid].code && |
| 216 | code < v2_code_table[mid - 1].code) |
| 217 | break; |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 218 | else if (code < v2_code_table[mid].code) |
| 219 | low = mid + 1; |
| 220 | else |
| 221 | high = mid - 1; |
| 222 | mid = (low + high) / 2; |
| 223 | } |
| 224 | |
Caesar Wang | 1e9a1ae | 2015-01-25 10:11:11 +0800 | [diff] [blame] | 225 | /* |
| 226 | * The 5C granularity provided by the table is too much. Let's |
| 227 | * assume that the relationship between sensor readings and |
| 228 | * temperature between 2 table entries is linear and interpolate |
| 229 | * to produce less granular result. |
| 230 | */ |
| 231 | num = v2_code_table[mid].temp - v2_code_table[mid - 1].temp; |
| 232 | num *= v2_code_table[mid - 1].code - code; |
| 233 | denom = v2_code_table[mid - 1].code - v2_code_table[mid].code; |
Dmitry Torokhov | d9a241c | 2015-08-07 13:59:23 -0700 | [diff] [blame] | 234 | *temp = v2_code_table[mid - 1].temp + (num / denom); |
| 235 | |
| 236 | return 0; |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /** |
Caesar Wang | 144c556 | 2015-11-05 13:17:59 +0800 | [diff] [blame^] | 240 | * rk_tsadcv2_initialize - initialize TASDC Controller. |
| 241 | * |
| 242 | * (1) Set TSADC_V2_AUTO_PERIOD: |
| 243 | * Configure the interleave between every two accessing of |
| 244 | * TSADC in normal operation. |
| 245 | * |
| 246 | * (2) Set TSADCV2_AUTO_PERIOD_HT: |
| 247 | * Configure the interleave between every two accessing of |
| 248 | * TSADC after the temperature is higher than COM_SHUT or COM_INT. |
| 249 | * |
| 250 | * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE: |
| 251 | * If the temperature is higher than COMP_INT or COMP_SHUT for |
| 252 | * "debounce" times, TSADC controller will generate interrupt or TSHUT. |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 253 | */ |
| 254 | static void rk_tsadcv2_initialize(void __iomem *regs, |
| 255 | enum tshut_polarity tshut_polarity) |
| 256 | { |
| 257 | if (tshut_polarity == TSHUT_HIGH_ACTIVE) |
Dmitry Torokhov | 452e01b | 2015-08-07 14:00:52 -0700 | [diff] [blame] | 258 | writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH, |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 259 | regs + TSADCV2_AUTO_CON); |
| 260 | else |
Dmitry Torokhov | 452e01b | 2015-08-07 14:00:52 -0700 | [diff] [blame] | 261 | writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH, |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 262 | regs + TSADCV2_AUTO_CON); |
| 263 | |
| 264 | writel_relaxed(TSADCV2_AUTO_PERIOD_TIME, regs + TSADCV2_AUTO_PERIOD); |
| 265 | writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT, |
| 266 | regs + TSADCV2_HIGHT_INT_DEBOUNCE); |
| 267 | writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME, |
| 268 | regs + TSADCV2_AUTO_PERIOD_HT); |
| 269 | writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT, |
| 270 | regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE); |
| 271 | } |
| 272 | |
| 273 | static void rk_tsadcv2_irq_ack(void __iomem *regs) |
| 274 | { |
| 275 | u32 val; |
| 276 | |
| 277 | val = readl_relaxed(regs + TSADCV2_INT_PD); |
Dmitry Torokhov | 452e01b | 2015-08-07 14:00:52 -0700 | [diff] [blame] | 278 | writel_relaxed(val & TSADCV2_INT_PD_CLEAR_MASK, regs + TSADCV2_INT_PD); |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static void rk_tsadcv2_control(void __iomem *regs, bool enable) |
| 282 | { |
| 283 | u32 val; |
| 284 | |
| 285 | val = readl_relaxed(regs + TSADCV2_AUTO_CON); |
| 286 | if (enable) |
| 287 | val |= TSADCV2_AUTO_EN; |
| 288 | else |
| 289 | val &= ~TSADCV2_AUTO_EN; |
| 290 | |
| 291 | writel_relaxed(val, regs + TSADCV2_AUTO_CON); |
| 292 | } |
| 293 | |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 294 | static int rk_tsadcv2_get_temp(int chn, void __iomem *regs, int *temp) |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 295 | { |
| 296 | u32 val; |
| 297 | |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 298 | val = readl_relaxed(regs + TSADCV2_DATA(chn)); |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 299 | |
Dmitry Torokhov | d9a241c | 2015-08-07 13:59:23 -0700 | [diff] [blame] | 300 | return rk_tsadcv2_code_to_temp(val, temp); |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | static void rk_tsadcv2_tshut_temp(int chn, void __iomem *regs, long temp) |
| 304 | { |
| 305 | u32 tshut_value, val; |
| 306 | |
| 307 | tshut_value = rk_tsadcv2_temp_to_code(temp); |
| 308 | writel_relaxed(tshut_value, regs + TSADCV2_COMP_SHUT(chn)); |
| 309 | |
| 310 | /* TSHUT will be valid */ |
| 311 | val = readl_relaxed(regs + TSADCV2_AUTO_CON); |
| 312 | writel_relaxed(val | TSADCV2_AUTO_SRC_EN(chn), regs + TSADCV2_AUTO_CON); |
| 313 | } |
| 314 | |
| 315 | static void rk_tsadcv2_tshut_mode(int chn, void __iomem *regs, |
| 316 | enum tshut_mode mode) |
| 317 | { |
| 318 | u32 val; |
| 319 | |
| 320 | val = readl_relaxed(regs + TSADCV2_INT_EN); |
| 321 | if (mode == TSHUT_MODE_GPIO) { |
| 322 | val &= ~TSADCV2_SHUT_2CRU_SRC_EN(chn); |
| 323 | val |= TSADCV2_SHUT_2GPIO_SRC_EN(chn); |
| 324 | } else { |
| 325 | val &= ~TSADCV2_SHUT_2GPIO_SRC_EN(chn); |
| 326 | val |= TSADCV2_SHUT_2CRU_SRC_EN(chn); |
| 327 | } |
| 328 | |
| 329 | writel_relaxed(val, regs + TSADCV2_INT_EN); |
| 330 | } |
| 331 | |
| 332 | static const struct rockchip_tsadc_chip rk3288_tsadc_data = { |
Caesar Wang | 1d98b61 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 333 | .chn_id[SENSOR_CPU] = 1, /* cpu sensor is channel 1 */ |
| 334 | .chn_id[SENSOR_GPU] = 2, /* gpu sensor is channel 2 */ |
| 335 | .chn_num = 2, /* two channels for tsadc */ |
| 336 | |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 337 | .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */ |
| 338 | .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */ |
| 339 | .tshut_temp = 95000, |
| 340 | |
| 341 | .initialize = rk_tsadcv2_initialize, |
| 342 | .irq_ack = rk_tsadcv2_irq_ack, |
| 343 | .control = rk_tsadcv2_control, |
| 344 | .get_temp = rk_tsadcv2_get_temp, |
| 345 | .set_tshut_temp = rk_tsadcv2_tshut_temp, |
| 346 | .set_tshut_mode = rk_tsadcv2_tshut_mode, |
| 347 | }; |
| 348 | |
| 349 | static const struct of_device_id of_rockchip_thermal_match[] = { |
| 350 | { |
| 351 | .compatible = "rockchip,rk3288-tsadc", |
| 352 | .data = (void *)&rk3288_tsadc_data, |
| 353 | }, |
| 354 | { /* end */ }, |
| 355 | }; |
| 356 | MODULE_DEVICE_TABLE(of, of_rockchip_thermal_match); |
| 357 | |
| 358 | static void |
| 359 | rockchip_thermal_toggle_sensor(struct rockchip_thermal_sensor *sensor, bool on) |
| 360 | { |
| 361 | struct thermal_zone_device *tzd = sensor->tzd; |
| 362 | |
| 363 | tzd->ops->set_mode(tzd, |
| 364 | on ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED); |
| 365 | } |
| 366 | |
| 367 | static irqreturn_t rockchip_thermal_alarm_irq_thread(int irq, void *dev) |
| 368 | { |
| 369 | struct rockchip_thermal_data *thermal = dev; |
| 370 | int i; |
| 371 | |
| 372 | dev_dbg(&thermal->pdev->dev, "thermal alarm\n"); |
| 373 | |
| 374 | thermal->chip->irq_ack(thermal->regs); |
| 375 | |
Caesar Wang | 1d98b61 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 376 | for (i = 0; i < thermal->chip->chn_num; i++) |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 377 | thermal_zone_device_update(thermal->sensors[i].tzd); |
| 378 | |
| 379 | return IRQ_HANDLED; |
| 380 | } |
| 381 | |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 382 | static int rockchip_thermal_get_temp(void *_sensor, int *out_temp) |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 383 | { |
| 384 | struct rockchip_thermal_sensor *sensor = _sensor; |
| 385 | struct rockchip_thermal_data *thermal = sensor->thermal; |
| 386 | const struct rockchip_tsadc_chip *tsadc = sensor->thermal->chip; |
| 387 | int retval; |
| 388 | |
| 389 | retval = tsadc->get_temp(sensor->id, thermal->regs, out_temp); |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 390 | dev_dbg(&thermal->pdev->dev, "sensor %d - temp: %d, retval: %d\n", |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 391 | sensor->id, *out_temp, retval); |
| 392 | |
| 393 | return retval; |
| 394 | } |
| 395 | |
| 396 | static const struct thermal_zone_of_device_ops rockchip_of_thermal_ops = { |
| 397 | .get_temp = rockchip_thermal_get_temp, |
| 398 | }; |
| 399 | |
| 400 | static int rockchip_configure_from_dt(struct device *dev, |
| 401 | struct device_node *np, |
| 402 | struct rockchip_thermal_data *thermal) |
| 403 | { |
| 404 | u32 shut_temp, tshut_mode, tshut_polarity; |
| 405 | |
| 406 | if (of_property_read_u32(np, "rockchip,hw-tshut-temp", &shut_temp)) { |
| 407 | dev_warn(dev, |
| 408 | "Missing tshut temp property, using default %ld\n", |
| 409 | thermal->chip->tshut_temp); |
| 410 | thermal->tshut_temp = thermal->chip->tshut_temp; |
| 411 | } else { |
| 412 | thermal->tshut_temp = shut_temp; |
| 413 | } |
| 414 | |
| 415 | if (thermal->tshut_temp > INT_MAX) { |
| 416 | dev_err(dev, "Invalid tshut temperature specified: %ld\n", |
| 417 | thermal->tshut_temp); |
| 418 | return -ERANGE; |
| 419 | } |
| 420 | |
| 421 | if (of_property_read_u32(np, "rockchip,hw-tshut-mode", &tshut_mode)) { |
| 422 | dev_warn(dev, |
| 423 | "Missing tshut mode property, using default (%s)\n", |
| 424 | thermal->chip->tshut_mode == TSHUT_MODE_GPIO ? |
| 425 | "gpio" : "cru"); |
| 426 | thermal->tshut_mode = thermal->chip->tshut_mode; |
| 427 | } else { |
| 428 | thermal->tshut_mode = tshut_mode; |
| 429 | } |
| 430 | |
| 431 | if (thermal->tshut_mode > 1) { |
| 432 | dev_err(dev, "Invalid tshut mode specified: %d\n", |
| 433 | thermal->tshut_mode); |
| 434 | return -EINVAL; |
| 435 | } |
| 436 | |
| 437 | if (of_property_read_u32(np, "rockchip,hw-tshut-polarity", |
| 438 | &tshut_polarity)) { |
| 439 | dev_warn(dev, |
| 440 | "Missing tshut-polarity property, using default (%s)\n", |
| 441 | thermal->chip->tshut_polarity == TSHUT_LOW_ACTIVE ? |
| 442 | "low" : "high"); |
| 443 | thermal->tshut_polarity = thermal->chip->tshut_polarity; |
| 444 | } else { |
| 445 | thermal->tshut_polarity = tshut_polarity; |
| 446 | } |
| 447 | |
| 448 | if (thermal->tshut_polarity > 1) { |
| 449 | dev_err(dev, "Invalid tshut-polarity specified: %d\n", |
| 450 | thermal->tshut_polarity); |
| 451 | return -EINVAL; |
| 452 | } |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | static int |
| 458 | rockchip_thermal_register_sensor(struct platform_device *pdev, |
| 459 | struct rockchip_thermal_data *thermal, |
| 460 | struct rockchip_thermal_sensor *sensor, |
Caesar Wang | 1d98b61 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 461 | int id) |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 462 | { |
| 463 | const struct rockchip_tsadc_chip *tsadc = thermal->chip; |
| 464 | int error; |
| 465 | |
| 466 | tsadc->set_tshut_mode(id, thermal->regs, thermal->tshut_mode); |
| 467 | tsadc->set_tshut_temp(id, thermal->regs, thermal->tshut_temp); |
| 468 | |
| 469 | sensor->thermal = thermal; |
| 470 | sensor->id = id; |
| 471 | sensor->tzd = thermal_zone_of_sensor_register(&pdev->dev, id, sensor, |
| 472 | &rockchip_of_thermal_ops); |
| 473 | if (IS_ERR(sensor->tzd)) { |
| 474 | error = PTR_ERR(sensor->tzd); |
| 475 | dev_err(&pdev->dev, "failed to register sensor %d: %d\n", |
| 476 | id, error); |
| 477 | return error; |
| 478 | } |
| 479 | |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * Reset TSADC Controller, reset all tsadc registers. |
| 485 | */ |
| 486 | static void rockchip_thermal_reset_controller(struct reset_control *reset) |
| 487 | { |
| 488 | reset_control_assert(reset); |
| 489 | usleep_range(10, 20); |
| 490 | reset_control_deassert(reset); |
| 491 | } |
| 492 | |
| 493 | static int rockchip_thermal_probe(struct platform_device *pdev) |
| 494 | { |
| 495 | struct device_node *np = pdev->dev.of_node; |
| 496 | struct rockchip_thermal_data *thermal; |
| 497 | const struct of_device_id *match; |
| 498 | struct resource *res; |
| 499 | int irq; |
Caesar Wang | 1d98b61 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 500 | int i, j; |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 501 | int error; |
| 502 | |
| 503 | match = of_match_node(of_rockchip_thermal_match, np); |
| 504 | if (!match) |
| 505 | return -ENXIO; |
| 506 | |
| 507 | irq = platform_get_irq(pdev, 0); |
| 508 | if (irq < 0) { |
| 509 | dev_err(&pdev->dev, "no irq resource?\n"); |
| 510 | return -EINVAL; |
| 511 | } |
| 512 | |
| 513 | thermal = devm_kzalloc(&pdev->dev, sizeof(struct rockchip_thermal_data), |
| 514 | GFP_KERNEL); |
| 515 | if (!thermal) |
| 516 | return -ENOMEM; |
| 517 | |
| 518 | thermal->pdev = pdev; |
| 519 | |
| 520 | thermal->chip = (const struct rockchip_tsadc_chip *)match->data; |
| 521 | if (!thermal->chip) |
| 522 | return -EINVAL; |
| 523 | |
| 524 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 525 | thermal->regs = devm_ioremap_resource(&pdev->dev, res); |
| 526 | if (IS_ERR(thermal->regs)) |
| 527 | return PTR_ERR(thermal->regs); |
| 528 | |
| 529 | thermal->reset = devm_reset_control_get(&pdev->dev, "tsadc-apb"); |
| 530 | if (IS_ERR(thermal->reset)) { |
| 531 | error = PTR_ERR(thermal->reset); |
| 532 | dev_err(&pdev->dev, "failed to get tsadc reset: %d\n", error); |
| 533 | return error; |
| 534 | } |
| 535 | |
| 536 | thermal->clk = devm_clk_get(&pdev->dev, "tsadc"); |
| 537 | if (IS_ERR(thermal->clk)) { |
| 538 | error = PTR_ERR(thermal->clk); |
| 539 | dev_err(&pdev->dev, "failed to get tsadc clock: %d\n", error); |
| 540 | return error; |
| 541 | } |
| 542 | |
| 543 | thermal->pclk = devm_clk_get(&pdev->dev, "apb_pclk"); |
| 544 | if (IS_ERR(thermal->pclk)) { |
Dan Carpenter | 0d0a2bf | 2015-04-21 12:34:10 +0300 | [diff] [blame] | 545 | error = PTR_ERR(thermal->pclk); |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 546 | dev_err(&pdev->dev, "failed to get apb_pclk clock: %d\n", |
| 547 | error); |
| 548 | return error; |
| 549 | } |
| 550 | |
| 551 | error = clk_prepare_enable(thermal->clk); |
| 552 | if (error) { |
| 553 | dev_err(&pdev->dev, "failed to enable converter clock: %d\n", |
| 554 | error); |
| 555 | return error; |
| 556 | } |
| 557 | |
| 558 | error = clk_prepare_enable(thermal->pclk); |
| 559 | if (error) { |
| 560 | dev_err(&pdev->dev, "failed to enable pclk: %d\n", error); |
| 561 | goto err_disable_clk; |
| 562 | } |
| 563 | |
| 564 | rockchip_thermal_reset_controller(thermal->reset); |
| 565 | |
| 566 | error = rockchip_configure_from_dt(&pdev->dev, np, thermal); |
| 567 | if (error) { |
| 568 | dev_err(&pdev->dev, "failed to parse device tree data: %d\n", |
| 569 | error); |
| 570 | goto err_disable_pclk; |
| 571 | } |
| 572 | |
| 573 | thermal->chip->initialize(thermal->regs, thermal->tshut_polarity); |
| 574 | |
Caesar Wang | 1d98b61 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 575 | for (i = 0; i < thermal->chip->chn_num; i++) { |
| 576 | error = rockchip_thermal_register_sensor(pdev, thermal, |
| 577 | &thermal->sensors[i], |
| 578 | thermal->chip->chn_id[i]); |
| 579 | if (error) { |
| 580 | dev_err(&pdev->dev, |
| 581 | "failed to register sensor[%d] : error = %d\n", |
| 582 | i, error); |
| 583 | for (j = 0; j < i; j++) |
| 584 | thermal_zone_of_sensor_unregister(&pdev->dev, |
| 585 | thermal->sensors[j].tzd); |
| 586 | goto err_disable_pclk; |
| 587 | } |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | error = devm_request_threaded_irq(&pdev->dev, irq, NULL, |
| 591 | &rockchip_thermal_alarm_irq_thread, |
| 592 | IRQF_ONESHOT, |
| 593 | "rockchip_thermal", thermal); |
| 594 | if (error) { |
| 595 | dev_err(&pdev->dev, |
| 596 | "failed to request tsadc irq: %d\n", error); |
Caesar Wang | 1d98b61 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 597 | goto err_unregister_sensor; |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | thermal->chip->control(thermal->regs, true); |
| 601 | |
Caesar Wang | 1d98b61 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 602 | for (i = 0; i < thermal->chip->chn_num; i++) |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 603 | rockchip_thermal_toggle_sensor(&thermal->sensors[i], true); |
| 604 | |
| 605 | platform_set_drvdata(pdev, thermal); |
| 606 | |
| 607 | return 0; |
| 608 | |
Caesar Wang | 1d98b61 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 609 | err_unregister_sensor: |
| 610 | while (i--) |
| 611 | thermal_zone_of_sensor_unregister(&pdev->dev, |
| 612 | thermal->sensors[i].tzd); |
| 613 | |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 614 | err_disable_pclk: |
| 615 | clk_disable_unprepare(thermal->pclk); |
| 616 | err_disable_clk: |
| 617 | clk_disable_unprepare(thermal->clk); |
| 618 | |
| 619 | return error; |
| 620 | } |
| 621 | |
| 622 | static int rockchip_thermal_remove(struct platform_device *pdev) |
| 623 | { |
| 624 | struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev); |
| 625 | int i; |
| 626 | |
Caesar Wang | 1d98b61 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 627 | for (i = 0; i < thermal->chip->chn_num; i++) { |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 628 | struct rockchip_thermal_sensor *sensor = &thermal->sensors[i]; |
| 629 | |
| 630 | rockchip_thermal_toggle_sensor(sensor, false); |
| 631 | thermal_zone_of_sensor_unregister(&pdev->dev, sensor->tzd); |
| 632 | } |
| 633 | |
| 634 | thermal->chip->control(thermal->regs, false); |
| 635 | |
| 636 | clk_disable_unprepare(thermal->pclk); |
| 637 | clk_disable_unprepare(thermal->clk); |
| 638 | |
| 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | static int __maybe_unused rockchip_thermal_suspend(struct device *dev) |
| 643 | { |
| 644 | struct platform_device *pdev = to_platform_device(dev); |
| 645 | struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev); |
| 646 | int i; |
| 647 | |
Caesar Wang | 1d98b61 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 648 | for (i = 0; i < thermal->chip->chn_num; i++) |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 649 | rockchip_thermal_toggle_sensor(&thermal->sensors[i], false); |
| 650 | |
| 651 | thermal->chip->control(thermal->regs, false); |
| 652 | |
| 653 | clk_disable(thermal->pclk); |
| 654 | clk_disable(thermal->clk); |
| 655 | |
Caesar Wang | 7e38a5b | 2015-10-23 19:25:27 +0800 | [diff] [blame] | 656 | pinctrl_pm_select_sleep_state(dev); |
| 657 | |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | static int __maybe_unused rockchip_thermal_resume(struct device *dev) |
| 662 | { |
| 663 | struct platform_device *pdev = to_platform_device(dev); |
| 664 | struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev); |
| 665 | int i; |
| 666 | int error; |
| 667 | |
| 668 | error = clk_enable(thermal->clk); |
| 669 | if (error) |
| 670 | return error; |
| 671 | |
| 672 | error = clk_enable(thermal->pclk); |
| 673 | if (error) |
| 674 | return error; |
| 675 | |
| 676 | rockchip_thermal_reset_controller(thermal->reset); |
| 677 | |
| 678 | thermal->chip->initialize(thermal->regs, thermal->tshut_polarity); |
| 679 | |
Caesar Wang | 1d98b61 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 680 | for (i = 0; i < thermal->chip->chn_num; i++) { |
| 681 | int id = thermal->sensors[i].id; |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 682 | |
| 683 | thermal->chip->set_tshut_mode(id, thermal->regs, |
| 684 | thermal->tshut_mode); |
| 685 | thermal->chip->set_tshut_temp(id, thermal->regs, |
| 686 | thermal->tshut_temp); |
| 687 | } |
| 688 | |
| 689 | thermal->chip->control(thermal->regs, true); |
| 690 | |
Caesar Wang | 1d98b61 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 691 | for (i = 0; i < thermal->chip->chn_num; i++) |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 692 | rockchip_thermal_toggle_sensor(&thermal->sensors[i], true); |
| 693 | |
Caesar Wang | 7e38a5b | 2015-10-23 19:25:27 +0800 | [diff] [blame] | 694 | pinctrl_pm_select_default_state(dev); |
| 695 | |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | static SIMPLE_DEV_PM_OPS(rockchip_thermal_pm_ops, |
| 700 | rockchip_thermal_suspend, rockchip_thermal_resume); |
| 701 | |
| 702 | static struct platform_driver rockchip_thermal_driver = { |
| 703 | .driver = { |
| 704 | .name = "rockchip-thermal", |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 705 | .pm = &rockchip_thermal_pm_ops, |
| 706 | .of_match_table = of_rockchip_thermal_match, |
| 707 | }, |
| 708 | .probe = rockchip_thermal_probe, |
| 709 | .remove = rockchip_thermal_remove, |
| 710 | }; |
| 711 | |
| 712 | module_platform_driver(rockchip_thermal_driver); |
| 713 | |
| 714 | MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver"); |
| 715 | MODULE_AUTHOR("Rockchip, Inc."); |
| 716 | MODULE_LICENSE("GPL v2"); |
| 717 | MODULE_ALIAS("platform:rockchip-thermal"); |