1 /* 2 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved. 3 * 4 * Author: 5 * Mikko Perttunen <mperttunen@nvidia.com> 6 * 7 * This software is licensed under the terms of the GNU General Public 8 * License version 2, as published by the Free Software Foundation, and 9 * may be copied, distributed, and modified under those terms. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 */ 17 18 #include <linux/debugfs.h> 19 #include <linux/bitops.h> 20 #include <linux/clk.h> 21 #include <linux/delay.h> 22 #include <linux/err.h> 23 #include <linux/interrupt.h> 24 #include <linux/io.h> 25 #include <linux/module.h> 26 #include <linux/of.h> 27 #include <linux/platform_device.h> 28 #include <linux/reset.h> 29 #include <linux/thermal.h> 30 31 #include <dt-bindings/thermal/tegra124-soctherm.h> 32 33 #include "../thermal_core.h" 34 #include "soctherm.h" 35 36 #define SENSOR_CONFIG0 0 37 #define SENSOR_CONFIG0_STOP BIT(0) 38 #define SENSOR_CONFIG0_CPTR_OVER BIT(2) 39 #define SENSOR_CONFIG0_OVER BIT(3) 40 #define SENSOR_CONFIG0_TCALC_OVER BIT(4) 41 #define SENSOR_CONFIG0_TALL_MASK (0xfffff << 8) 42 #define SENSOR_CONFIG0_TALL_SHIFT 8 43 44 #define SENSOR_CONFIG1 4 45 #define SENSOR_CONFIG1_TSAMPLE_MASK 0x3ff 46 #define SENSOR_CONFIG1_TSAMPLE_SHIFT 0 47 #define SENSOR_CONFIG1_TIDDQ_EN_MASK (0x3f << 15) 48 #define SENSOR_CONFIG1_TIDDQ_EN_SHIFT 15 49 #define SENSOR_CONFIG1_TEN_COUNT_MASK (0x3f << 24) 50 #define SENSOR_CONFIG1_TEN_COUNT_SHIFT 24 51 #define SENSOR_CONFIG1_TEMP_ENABLE BIT(31) 52 53 /* 54 * SENSOR_CONFIG2 is defined in soctherm.h 55 * because, it will be used by tegra_soctherm_fuse.c 56 */ 57 58 #define SENSOR_STATUS0 0xc 59 #define SENSOR_STATUS0_VALID_MASK BIT(31) 60 #define SENSOR_STATUS0_CAPTURE_MASK 0xffff 61 62 #define SENSOR_STATUS1 0x10 63 #define SENSOR_STATUS1_TEMP_VALID_MASK BIT(31) 64 #define SENSOR_STATUS1_TEMP_MASK 0xffff 65 66 #define READBACK_VALUE_MASK 0xff00 67 #define READBACK_VALUE_SHIFT 8 68 #define READBACK_ADD_HALF BIT(7) 69 #define READBACK_NEGATE BIT(0) 70 71 /* 72 * THERMCTL_LEVEL0_GROUP_CPU is defined in soctherm.h 73 * because it will be used by tegraxxx_soctherm.c 74 */ 75 #define THERMCTL_LVL0_CPU0_EN_MASK BIT(8) 76 #define THERMCTL_LVL0_CPU0_CPU_THROT_MASK (0x3 << 5) 77 #define THERMCTL_LVL0_CPU0_CPU_THROT_LIGHT 0x1 78 #define THERMCTL_LVL0_CPU0_CPU_THROT_HEAVY 0x2 79 #define THERMCTL_LVL0_CPU0_GPU_THROT_MASK (0x3 << 3) 80 #define THERMCTL_LVL0_CPU0_GPU_THROT_LIGHT 0x1 81 #define THERMCTL_LVL0_CPU0_GPU_THROT_HEAVY 0x2 82 #define THERMCTL_LVL0_CPU0_MEM_THROT_MASK BIT(2) 83 #define THERMCTL_LVL0_CPU0_STATUS_MASK 0x3 84 85 #define THERMCTL_LVL0_UP_STATS 0x10 86 #define THERMCTL_LVL0_DN_STATS 0x14 87 88 #define THERMCTL_STATS_CTL 0x94 89 #define STATS_CTL_CLR_DN 0x8 90 #define STATS_CTL_EN_DN 0x4 91 #define STATS_CTL_CLR_UP 0x2 92 #define STATS_CTL_EN_UP 0x1 93 94 #define THROT_GLOBAL_CFG 0x400 95 #define THROT_GLOBAL_ENB_MASK BIT(0) 96 97 #define CPU_PSKIP_STATUS 0x418 98 #define XPU_PSKIP_STATUS_M_MASK (0xff << 12) 99 #define XPU_PSKIP_STATUS_N_MASK (0xff << 4) 100 #define XPU_PSKIP_STATUS_SW_OVERRIDE_MASK BIT(1) 101 #define XPU_PSKIP_STATUS_ENABLED_MASK BIT(0) 102 103 #define THROT_PRIORITY_LOCK 0x424 104 #define THROT_PRIORITY_LOCK_PRIORITY_MASK 0xff 105 106 #define THROT_STATUS 0x428 107 #define THROT_STATUS_BREACH_MASK BIT(12) 108 #define THROT_STATUS_STATE_MASK (0xff << 4) 109 #define THROT_STATUS_ENABLED_MASK BIT(0) 110 111 #define THROT_PSKIP_CTRL_LITE_CPU 0x430 112 #define THROT_PSKIP_CTRL_ENABLE_MASK BIT(31) 113 #define THROT_PSKIP_CTRL_DIVIDEND_MASK (0xff << 8) 114 #define THROT_PSKIP_CTRL_DIVISOR_MASK 0xff 115 #define THROT_PSKIP_CTRL_VECT_GPU_MASK (0x7 << 16) 116 #define THROT_PSKIP_CTRL_VECT_CPU_MASK (0x7 << 8) 117 #define THROT_PSKIP_CTRL_VECT2_CPU_MASK 0x7 118 119 #define THROT_VECT_NONE 0x0 /* 3'b000 */ 120 #define THROT_VECT_LOW 0x1 /* 3'b001 */ 121 #define THROT_VECT_MED 0x3 /* 3'b011 */ 122 #define THROT_VECT_HIGH 0x7 /* 3'b111 */ 123 124 #define THROT_PSKIP_RAMP_LITE_CPU 0x434 125 #define THROT_PSKIP_RAMP_SEQ_BYPASS_MODE_MASK BIT(31) 126 #define THROT_PSKIP_RAMP_DURATION_MASK (0xffff << 8) 127 #define THROT_PSKIP_RAMP_STEP_MASK 0xff 128 129 #define THROT_PRIORITY_LITE 0x444 130 #define THROT_PRIORITY_LITE_PRIO_MASK 0xff 131 132 #define THROT_DELAY_LITE 0x448 133 #define THROT_DELAY_LITE_DELAY_MASK 0xff 134 135 /* car register offsets needed for enabling HW throttling */ 136 #define CAR_SUPER_CCLKG_DIVIDER 0x36c 137 #define CDIVG_USE_THERM_CONTROLS_MASK BIT(30) 138 139 /* ccroc register offsets needed for enabling HW throttling for Tegra132 */ 140 #define CCROC_SUPER_CCLKG_DIVIDER 0x024 141 142 #define CCROC_GLOBAL_CFG 0x148 143 144 #define CCROC_THROT_PSKIP_RAMP_CPU 0x150 145 #define CCROC_THROT_PSKIP_RAMP_SEQ_BYPASS_MODE_MASK BIT(31) 146 #define CCROC_THROT_PSKIP_RAMP_DURATION_MASK (0xffff << 8) 147 #define CCROC_THROT_PSKIP_RAMP_STEP_MASK 0xff 148 149 #define CCROC_THROT_PSKIP_CTRL_CPU 0x154 150 #define CCROC_THROT_PSKIP_CTRL_ENB_MASK BIT(31) 151 #define CCROC_THROT_PSKIP_CTRL_DIVIDEND_MASK (0xff << 8) 152 #define CCROC_THROT_PSKIP_CTRL_DIVISOR_MASK 0xff 153 154 /* get val from register(r) mask bits(m) */ 155 #define REG_GET_MASK(r, m) (((r) & (m)) >> (ffs(m) - 1)) 156 /* set val(v) to mask bits(m) of register(r) */ 157 #define REG_SET_MASK(r, m, v) (((r) & ~(m)) | \ 158 (((v) & (m >> (ffs(m) - 1))) << (ffs(m) - 1))) 159 160 /* get dividend from the depth */ 161 #define THROT_DEPTH_DIVIDEND(depth) ((256 * (100 - (depth)) / 100) - 1) 162 163 /* get THROT_PSKIP_xxx offset per LIGHT/HEAVY throt and CPU/GPU dev */ 164 #define THROT_OFFSET 0x30 165 #define THROT_PSKIP_CTRL(throt, dev) (THROT_PSKIP_CTRL_LITE_CPU + \ 166 (THROT_OFFSET * throt) + (8 * dev)) 167 #define THROT_PSKIP_RAMP(throt, dev) (THROT_PSKIP_RAMP_LITE_CPU + \ 168 (THROT_OFFSET * throt) + (8 * dev)) 169 170 /* get THROT_xxx_CTRL offset per LIGHT/HEAVY throt */ 171 #define THROT_PRIORITY_CTRL(throt) (THROT_PRIORITY_LITE + \ 172 (THROT_OFFSET * throt)) 173 #define THROT_DELAY_CTRL(throt) (THROT_DELAY_LITE + \ 174 (THROT_OFFSET * throt)) 175 176 /* get CCROC_THROT_PSKIP_xxx offset per HIGH/MED/LOW vect*/ 177 #define CCROC_THROT_OFFSET 0x0c 178 #define CCROC_THROT_PSKIP_CTRL_CPU_REG(vect) (CCROC_THROT_PSKIP_CTRL_CPU + \ 179 (CCROC_THROT_OFFSET * vect)) 180 #define CCROC_THROT_PSKIP_RAMP_CPU_REG(vect) (CCROC_THROT_PSKIP_RAMP_CPU + \ 181 (CCROC_THROT_OFFSET * vect)) 182 183 /* get THERMCTL_LEVELx offset per CPU/GPU/MEM/TSENSE rg and LEVEL0~3 lv */ 184 #define THERMCTL_LVL_REGS_SIZE 0x20 185 #define THERMCTL_LVL_REG(rg, lv) ((rg) + ((lv) * THERMCTL_LVL_REGS_SIZE)) 186 187 static const int min_low_temp = -127000; 188 static const int max_high_temp = 127000; 189 190 enum soctherm_throttle_id { 191 THROTTLE_LIGHT = 0, 192 THROTTLE_HEAVY, 193 THROTTLE_SIZE, 194 }; 195 196 enum soctherm_throttle_dev_id { 197 THROTTLE_DEV_CPU = 0, 198 THROTTLE_DEV_GPU, 199 THROTTLE_DEV_SIZE, 200 }; 201 202 static const char *const throt_names[] = { 203 [THROTTLE_LIGHT] = "light", 204 [THROTTLE_HEAVY] = "heavy", 205 }; 206 207 struct tegra_soctherm; 208 struct tegra_thermctl_zone { 209 void __iomem *reg; 210 struct device *dev; 211 struct tegra_soctherm *ts; 212 struct thermal_zone_device *tz; 213 const struct tegra_tsensor_group *sg; 214 }; 215 216 struct soctherm_throt_cfg { 217 const char *name; 218 unsigned int id; 219 u8 priority; 220 u8 cpu_throt_level; 221 u32 cpu_throt_depth; 222 struct thermal_cooling_device *cdev; 223 bool init; 224 }; 225 226 struct tegra_soctherm { 227 struct reset_control *reset; 228 struct clk *clock_tsensor; 229 struct clk *clock_soctherm; 230 void __iomem *regs; 231 void __iomem *clk_regs; 232 void __iomem *ccroc_regs; 233 234 u32 *calib; 235 struct thermal_zone_device **thermctl_tzs; 236 struct tegra_soctherm_soc *soc; 237 238 struct soctherm_throt_cfg throt_cfgs[THROTTLE_SIZE]; 239 240 struct dentry *debugfs_dir; 241 }; 242 243 /** 244 * ccroc_writel() - writes a value to a CCROC register 245 * @ts: pointer to a struct tegra_soctherm 246 * @v: the value to write 247 * @reg: the register offset 248 * 249 * Writes @v to @reg. No return value. 250 */ 251 static inline void ccroc_writel(struct tegra_soctherm *ts, u32 value, u32 reg) 252 { 253 writel(value, (ts->ccroc_regs + reg)); 254 } 255 256 /** 257 * ccroc_readl() - reads specified register from CCROC IP block 258 * @ts: pointer to a struct tegra_soctherm 259 * @reg: register address to be read 260 * 261 * Return: the value of the register 262 */ 263 static inline u32 ccroc_readl(struct tegra_soctherm *ts, u32 reg) 264 { 265 return readl(ts->ccroc_regs + reg); 266 } 267 268 static void enable_tsensor(struct tegra_soctherm *tegra, unsigned int i) 269 { 270 const struct tegra_tsensor *sensor = &tegra->soc->tsensors[i]; 271 void __iomem *base = tegra->regs + sensor->base; 272 unsigned int val; 273 274 val = sensor->config->tall << SENSOR_CONFIG0_TALL_SHIFT; 275 writel(val, base + SENSOR_CONFIG0); 276 277 val = (sensor->config->tsample - 1) << SENSOR_CONFIG1_TSAMPLE_SHIFT; 278 val |= sensor->config->tiddq_en << SENSOR_CONFIG1_TIDDQ_EN_SHIFT; 279 val |= sensor->config->ten_count << SENSOR_CONFIG1_TEN_COUNT_SHIFT; 280 val |= SENSOR_CONFIG1_TEMP_ENABLE; 281 writel(val, base + SENSOR_CONFIG1); 282 283 writel(tegra->calib[i], base + SENSOR_CONFIG2); 284 } 285 286 /* 287 * Translate from soctherm readback format to millicelsius. 288 * The soctherm readback format in bits is as follows: 289 * TTTTTTTT H______N 290 * where T's contain the temperature in Celsius, 291 * H denotes an addition of 0.5 Celsius and N denotes negation 292 * of the final value. 293 */ 294 static int translate_temp(u16 val) 295 { 296 int t; 297 298 t = ((val & READBACK_VALUE_MASK) >> READBACK_VALUE_SHIFT) * 1000; 299 if (val & READBACK_ADD_HALF) 300 t += 500; 301 if (val & READBACK_NEGATE) 302 t *= -1; 303 304 return t; 305 } 306 307 static int tegra_thermctl_get_temp(void *data, int *out_temp) 308 { 309 struct tegra_thermctl_zone *zone = data; 310 u32 val; 311 312 val = readl(zone->reg); 313 val = REG_GET_MASK(val, zone->sg->sensor_temp_mask); 314 *out_temp = translate_temp(val); 315 316 return 0; 317 } 318 319 /** 320 * enforce_temp_range() - check and enforce temperature range [min, max] 321 * @trip_temp: the trip temperature to check 322 * 323 * Checks and enforces the permitted temperature range that SOC_THERM 324 * HW can support This is 325 * done while taking care of precision. 326 * 327 * Return: The precision adjusted capped temperature in millicelsius. 328 */ 329 static int enforce_temp_range(struct device *dev, int trip_temp) 330 { 331 int temp; 332 333 temp = clamp_val(trip_temp, min_low_temp, max_high_temp); 334 if (temp != trip_temp) 335 dev_info(dev, "soctherm: trip temperature %d forced to %d\n", 336 trip_temp, temp); 337 return temp; 338 } 339 340 /** 341 * thermtrip_program() - Configures the hardware to shut down the 342 * system if a given sensor group reaches a given temperature 343 * @dev: ptr to the struct device for the SOC_THERM IP block 344 * @sg: pointer to the sensor group to set the thermtrip temperature for 345 * @trip_temp: the temperature in millicelsius to trigger the thermal trip at 346 * 347 * Sets the thermal trip threshold of the given sensor group to be the 348 * @trip_temp. If this threshold is crossed, the hardware will shut 349 * down. 350 * 351 * Note that, although @trip_temp is specified in millicelsius, the 352 * hardware is programmed in degrees Celsius. 353 * 354 * Return: 0 upon success, or %-EINVAL upon failure. 355 */ 356 static int thermtrip_program(struct device *dev, 357 const struct tegra_tsensor_group *sg, 358 int trip_temp) 359 { 360 struct tegra_soctherm *ts = dev_get_drvdata(dev); 361 int temp; 362 u32 r; 363 364 if (!sg || !sg->thermtrip_threshold_mask) 365 return -EINVAL; 366 367 temp = enforce_temp_range(dev, trip_temp) / ts->soc->thresh_grain; 368 369 r = readl(ts->regs + THERMCTL_THERMTRIP_CTL); 370 r = REG_SET_MASK(r, sg->thermtrip_threshold_mask, temp); 371 r = REG_SET_MASK(r, sg->thermtrip_enable_mask, 1); 372 r = REG_SET_MASK(r, sg->thermtrip_any_en_mask, 0); 373 writel(r, ts->regs + THERMCTL_THERMTRIP_CTL); 374 375 return 0; 376 } 377 378 /** 379 * throttrip_program() - Configures the hardware to throttle the 380 * pulse if a given sensor group reaches a given temperature 381 * @dev: ptr to the struct device for the SOC_THERM IP block 382 * @sg: pointer to the sensor group to set the thermtrip temperature for 383 * @stc: pointer to the throttle need to be triggered 384 * @trip_temp: the temperature in millicelsius to trigger the thermal trip at 385 * 386 * Sets the thermal trip threshold and throttle event of the given sensor 387 * group. If this threshold is crossed, the hardware will trigger the 388 * throttle. 389 * 390 * Note that, although @trip_temp is specified in millicelsius, the 391 * hardware is programmed in degrees Celsius. 392 * 393 * Return: 0 upon success, or %-EINVAL upon failure. 394 */ 395 static int throttrip_program(struct device *dev, 396 const struct tegra_tsensor_group *sg, 397 struct soctherm_throt_cfg *stc, 398 int trip_temp) 399 { 400 struct tegra_soctherm *ts = dev_get_drvdata(dev); 401 int temp, cpu_throt, gpu_throt; 402 unsigned int throt; 403 u32 r, reg_off; 404 405 if (!sg || !stc || !stc->init) 406 return -EINVAL; 407 408 temp = enforce_temp_range(dev, trip_temp) / ts->soc->thresh_grain; 409 410 /* Hardcode LIGHT on LEVEL1 and HEAVY on LEVEL2 */ 411 throt = stc->id; 412 reg_off = THERMCTL_LVL_REG(sg->thermctl_lvl0_offset, throt + 1); 413 414 if (throt == THROTTLE_LIGHT) { 415 cpu_throt = THERMCTL_LVL0_CPU0_CPU_THROT_LIGHT; 416 gpu_throt = THERMCTL_LVL0_CPU0_GPU_THROT_LIGHT; 417 } else { 418 cpu_throt = THERMCTL_LVL0_CPU0_CPU_THROT_HEAVY; 419 gpu_throt = THERMCTL_LVL0_CPU0_GPU_THROT_HEAVY; 420 if (throt != THROTTLE_HEAVY) 421 dev_warn(dev, 422 "invalid throt id %d - assuming HEAVY", 423 throt); 424 } 425 426 r = readl(ts->regs + reg_off); 427 r = REG_SET_MASK(r, sg->thermctl_lvl0_up_thresh_mask, temp); 428 r = REG_SET_MASK(r, sg->thermctl_lvl0_dn_thresh_mask, temp); 429 r = REG_SET_MASK(r, THERMCTL_LVL0_CPU0_CPU_THROT_MASK, cpu_throt); 430 r = REG_SET_MASK(r, THERMCTL_LVL0_CPU0_GPU_THROT_MASK, gpu_throt); 431 r = REG_SET_MASK(r, THERMCTL_LVL0_CPU0_EN_MASK, 1); 432 writel(r, ts->regs + reg_off); 433 434 return 0; 435 } 436 437 static struct soctherm_throt_cfg * 438 find_throttle_cfg_by_name(struct tegra_soctherm *ts, const char *name) 439 { 440 unsigned int i; 441 442 for (i = 0; ts->throt_cfgs[i].name; i++) 443 if (!strcmp(ts->throt_cfgs[i].name, name)) 444 return &ts->throt_cfgs[i]; 445 446 return NULL; 447 } 448 449 static int tegra_thermctl_set_trip_temp(void *data, int trip, int temp) 450 { 451 struct tegra_thermctl_zone *zone = data; 452 struct thermal_zone_device *tz = zone->tz; 453 struct tegra_soctherm *ts = zone->ts; 454 const struct tegra_tsensor_group *sg = zone->sg; 455 struct device *dev = zone->dev; 456 enum thermal_trip_type type; 457 int ret; 458 459 if (!tz) 460 return -EINVAL; 461 462 ret = tz->ops->get_trip_type(tz, trip, &type); 463 if (ret) 464 return ret; 465 466 if (type == THERMAL_TRIP_CRITICAL) { 467 return thermtrip_program(dev, sg, temp); 468 } else if (type == THERMAL_TRIP_HOT) { 469 int i; 470 471 for (i = 0; i < THROTTLE_SIZE; i++) { 472 struct thermal_cooling_device *cdev; 473 struct soctherm_throt_cfg *stc; 474 475 if (!ts->throt_cfgs[i].init) 476 continue; 477 478 cdev = ts->throt_cfgs[i].cdev; 479 if (get_thermal_instance(tz, cdev, trip)) 480 stc = find_throttle_cfg_by_name(ts, cdev->type); 481 else 482 continue; 483 484 return throttrip_program(dev, sg, stc, temp); 485 } 486 } 487 488 return 0; 489 } 490 491 static int tegra_thermctl_get_trend(void *data, int trip, 492 enum thermal_trend *trend) 493 { 494 struct tegra_thermctl_zone *zone = data; 495 struct thermal_zone_device *tz = zone->tz; 496 int trip_temp, temp, last_temp, ret; 497 498 if (!tz) 499 return -EINVAL; 500 501 ret = tz->ops->get_trip_temp(zone->tz, trip, &trip_temp); 502 if (ret) 503 return ret; 504 505 temp = READ_ONCE(tz->temperature); 506 last_temp = READ_ONCE(tz->last_temperature); 507 508 if (temp > trip_temp) { 509 if (temp >= last_temp) 510 *trend = THERMAL_TREND_RAISING; 511 else 512 *trend = THERMAL_TREND_STABLE; 513 } else if (temp < trip_temp) { 514 *trend = THERMAL_TREND_DROPPING; 515 } else { 516 *trend = THERMAL_TREND_STABLE; 517 } 518 519 return 0; 520 } 521 522 static const struct thermal_zone_of_device_ops tegra_of_thermal_ops = { 523 .get_temp = tegra_thermctl_get_temp, 524 .set_trip_temp = tegra_thermctl_set_trip_temp, 525 .get_trend = tegra_thermctl_get_trend, 526 }; 527 528 static int get_hot_temp(struct thermal_zone_device *tz, int *trip, int *temp) 529 { 530 int ntrips, i, ret; 531 enum thermal_trip_type type; 532 533 ntrips = of_thermal_get_ntrips(tz); 534 if (ntrips <= 0) 535 return -EINVAL; 536 537 for (i = 0; i < ntrips; i++) { 538 ret = tz->ops->get_trip_type(tz, i, &type); 539 if (ret) 540 return -EINVAL; 541 if (type == THERMAL_TRIP_HOT) { 542 ret = tz->ops->get_trip_temp(tz, i, temp); 543 if (!ret) 544 *trip = i; 545 546 return ret; 547 } 548 } 549 550 return -EINVAL; 551 } 552 553 /** 554 * tegra_soctherm_set_hwtrips() - set HW trip point from DT data 555 * @dev: struct device * of the SOC_THERM instance 556 * 557 * Configure the SOC_THERM HW trip points, setting "THERMTRIP" 558 * "THROTTLE" trip points , using "critical" or "hot" type trip_temp 559 * from thermal zone. 560 * After they have been configured, THERMTRIP or THROTTLE will take 561 * action when the configured SoC thermal sensor group reaches a 562 * certain temperature. 563 * 564 * Return: 0 upon success, or a negative error code on failure. 565 * "Success" does not mean that trips was enabled; it could also 566 * mean that no node was found in DT. 567 * THERMTRIP has been enabled successfully when a message similar to 568 * this one appears on the serial console: 569 * "thermtrip: will shut down when sensor group XXX reaches YYYYYY mC" 570 * THROTTLE has been enabled successfully when a message similar to 571 * this one appears on the serial console: 572 * ""throttrip: will throttle when sensor group XXX reaches YYYYYY mC" 573 */ 574 static int tegra_soctherm_set_hwtrips(struct device *dev, 575 const struct tegra_tsensor_group *sg, 576 struct thermal_zone_device *tz) 577 { 578 struct tegra_soctherm *ts = dev_get_drvdata(dev); 579 struct soctherm_throt_cfg *stc; 580 int i, trip, temperature; 581 int ret; 582 583 ret = tz->ops->get_crit_temp(tz, &temperature); 584 if (ret) { 585 dev_warn(dev, "thermtrip: %s: missing critical temperature\n", 586 sg->name); 587 goto set_throttle; 588 } 589 590 ret = thermtrip_program(dev, sg, temperature); 591 if (ret) { 592 dev_err(dev, "thermtrip: %s: error during enable\n", 593 sg->name); 594 return ret; 595 } 596 597 dev_info(dev, 598 "thermtrip: will shut down when %s reaches %d mC\n", 599 sg->name, temperature); 600 601 set_throttle: 602 ret = get_hot_temp(tz, &trip, &temperature); 603 if (ret) { 604 dev_info(dev, "throttrip: %s: missing hot temperature\n", 605 sg->name); 606 return 0; 607 } 608 609 for (i = 0; i < THROTTLE_SIZE; i++) { 610 struct thermal_cooling_device *cdev; 611 612 if (!ts->throt_cfgs[i].init) 613 continue; 614 615 cdev = ts->throt_cfgs[i].cdev; 616 if (get_thermal_instance(tz, cdev, trip)) 617 stc = find_throttle_cfg_by_name(ts, cdev->type); 618 else 619 continue; 620 621 ret = throttrip_program(dev, sg, stc, temperature); 622 if (ret) { 623 dev_err(dev, "throttrip: %s: error during enable\n", 624 sg->name); 625 return ret; 626 } 627 628 dev_info(dev, 629 "throttrip: will throttle when %s reaches %d mC\n", 630 sg->name, temperature); 631 break; 632 } 633 634 if (i == THROTTLE_SIZE) 635 dev_info(dev, "throttrip: %s: missing throttle cdev\n", 636 sg->name); 637 638 return 0; 639 } 640 641 #ifdef CONFIG_DEBUG_FS 642 static int regs_show(struct seq_file *s, void *data) 643 { 644 struct platform_device *pdev = s->private; 645 struct tegra_soctherm *ts = platform_get_drvdata(pdev); 646 const struct tegra_tsensor *tsensors = ts->soc->tsensors; 647 const struct tegra_tsensor_group **ttgs = ts->soc->ttgs; 648 u32 r, state; 649 int i, level; 650 651 seq_puts(s, "-----TSENSE (convert HW)-----\n"); 652 653 for (i = 0; i < ts->soc->num_tsensors; i++) { 654 r = readl(ts->regs + tsensors[i].base + SENSOR_CONFIG1); 655 state = REG_GET_MASK(r, SENSOR_CONFIG1_TEMP_ENABLE); 656 657 seq_printf(s, "%s: ", tsensors[i].name); 658 seq_printf(s, "En(%d) ", state); 659 660 if (!state) { 661 seq_puts(s, "\n"); 662 continue; 663 } 664 665 state = REG_GET_MASK(r, SENSOR_CONFIG1_TIDDQ_EN_MASK); 666 seq_printf(s, "tiddq(%d) ", state); 667 state = REG_GET_MASK(r, SENSOR_CONFIG1_TEN_COUNT_MASK); 668 seq_printf(s, "ten_count(%d) ", state); 669 state = REG_GET_MASK(r, SENSOR_CONFIG1_TSAMPLE_MASK); 670 seq_printf(s, "tsample(%d) ", state + 1); 671 672 r = readl(ts->regs + tsensors[i].base + SENSOR_STATUS1); 673 state = REG_GET_MASK(r, SENSOR_STATUS1_TEMP_VALID_MASK); 674 seq_printf(s, "Temp(%d/", state); 675 state = REG_GET_MASK(r, SENSOR_STATUS1_TEMP_MASK); 676 seq_printf(s, "%d) ", translate_temp(state)); 677 678 r = readl(ts->regs + tsensors[i].base + SENSOR_STATUS0); 679 state = REG_GET_MASK(r, SENSOR_STATUS0_VALID_MASK); 680 seq_printf(s, "Capture(%d/", state); 681 state = REG_GET_MASK(r, SENSOR_STATUS0_CAPTURE_MASK); 682 seq_printf(s, "%d) ", state); 683 684 r = readl(ts->regs + tsensors[i].base + SENSOR_CONFIG0); 685 state = REG_GET_MASK(r, SENSOR_CONFIG0_STOP); 686 seq_printf(s, "Stop(%d) ", state); 687 state = REG_GET_MASK(r, SENSOR_CONFIG0_TALL_MASK); 688 seq_printf(s, "Tall(%d) ", state); 689 state = REG_GET_MASK(r, SENSOR_CONFIG0_TCALC_OVER); 690 seq_printf(s, "Over(%d/", state); 691 state = REG_GET_MASK(r, SENSOR_CONFIG0_OVER); 692 seq_printf(s, "%d/", state); 693 state = REG_GET_MASK(r, SENSOR_CONFIG0_CPTR_OVER); 694 seq_printf(s, "%d) ", state); 695 696 r = readl(ts->regs + tsensors[i].base + SENSOR_CONFIG2); 697 state = REG_GET_MASK(r, SENSOR_CONFIG2_THERMA_MASK); 698 seq_printf(s, "Therm_A/B(%d/", state); 699 state = REG_GET_MASK(r, SENSOR_CONFIG2_THERMB_MASK); 700 seq_printf(s, "%d)\n", (s16)state); 701 } 702 703 r = readl(ts->regs + SENSOR_PDIV); 704 seq_printf(s, "PDIV: 0x%x\n", r); 705 706 r = readl(ts->regs + SENSOR_HOTSPOT_OFF); 707 seq_printf(s, "HOTSPOT: 0x%x\n", r); 708 709 seq_puts(s, "\n"); 710 seq_puts(s, "-----SOC_THERM-----\n"); 711 712 r = readl(ts->regs + SENSOR_TEMP1); 713 state = REG_GET_MASK(r, SENSOR_TEMP1_CPU_TEMP_MASK); 714 seq_printf(s, "Temperatures: CPU(%d) ", translate_temp(state)); 715 state = REG_GET_MASK(r, SENSOR_TEMP1_GPU_TEMP_MASK); 716 seq_printf(s, " GPU(%d) ", translate_temp(state)); 717 r = readl(ts->regs + SENSOR_TEMP2); 718 state = REG_GET_MASK(r, SENSOR_TEMP2_PLLX_TEMP_MASK); 719 seq_printf(s, " PLLX(%d) ", translate_temp(state)); 720 state = REG_GET_MASK(r, SENSOR_TEMP2_MEM_TEMP_MASK); 721 seq_printf(s, " MEM(%d)\n", translate_temp(state)); 722 723 for (i = 0; i < ts->soc->num_ttgs; i++) { 724 seq_printf(s, "%s:\n", ttgs[i]->name); 725 for (level = 0; level < 4; level++) { 726 s32 v; 727 u32 mask; 728 u16 off = ttgs[i]->thermctl_lvl0_offset; 729 730 r = readl(ts->regs + THERMCTL_LVL_REG(off, level)); 731 732 mask = ttgs[i]->thermctl_lvl0_up_thresh_mask; 733 state = REG_GET_MASK(r, mask); 734 v = sign_extend32(state, ts->soc->bptt - 1); 735 v *= ts->soc->thresh_grain; 736 seq_printf(s, " %d: Up/Dn(%d /", level, v); 737 738 mask = ttgs[i]->thermctl_lvl0_dn_thresh_mask; 739 state = REG_GET_MASK(r, mask); 740 v = sign_extend32(state, ts->soc->bptt - 1); 741 v *= ts->soc->thresh_grain; 742 seq_printf(s, "%d ) ", v); 743 744 mask = THERMCTL_LVL0_CPU0_EN_MASK; 745 state = REG_GET_MASK(r, mask); 746 seq_printf(s, "En(%d) ", state); 747 748 mask = THERMCTL_LVL0_CPU0_CPU_THROT_MASK; 749 state = REG_GET_MASK(r, mask); 750 seq_puts(s, "CPU Throt"); 751 if (!state) 752 seq_printf(s, "(%s) ", "none"); 753 else if (state == THERMCTL_LVL0_CPU0_CPU_THROT_LIGHT) 754 seq_printf(s, "(%s) ", "L"); 755 else if (state == THERMCTL_LVL0_CPU0_CPU_THROT_HEAVY) 756 seq_printf(s, "(%s) ", "H"); 757 else 758 seq_printf(s, "(%s) ", "H+L"); 759 760 mask = THERMCTL_LVL0_CPU0_GPU_THROT_MASK; 761 state = REG_GET_MASK(r, mask); 762 seq_puts(s, "GPU Throt"); 763 if (!state) 764 seq_printf(s, "(%s) ", "none"); 765 else if (state == THERMCTL_LVL0_CPU0_GPU_THROT_LIGHT) 766 seq_printf(s, "(%s) ", "L"); 767 else if (state == THERMCTL_LVL0_CPU0_GPU_THROT_HEAVY) 768 seq_printf(s, "(%s) ", "H"); 769 else 770 seq_printf(s, "(%s) ", "H+L"); 771 772 mask = THERMCTL_LVL0_CPU0_STATUS_MASK; 773 state = REG_GET_MASK(r, mask); 774 seq_printf(s, "Status(%s)\n", 775 state == 0 ? "LO" : 776 state == 1 ? "In" : 777 state == 2 ? "Res" : "HI"); 778 } 779 } 780 781 r = readl(ts->regs + THERMCTL_STATS_CTL); 782 seq_printf(s, "STATS: Up(%s) Dn(%s)\n", 783 r & STATS_CTL_EN_UP ? "En" : "--", 784 r & STATS_CTL_EN_DN ? "En" : "--"); 785 786 for (level = 0; level < 4; level++) { 787 u16 off; 788 789 off = THERMCTL_LVL0_UP_STATS; 790 r = readl(ts->regs + THERMCTL_LVL_REG(off, level)); 791 seq_printf(s, " Level_%d Up(%d) ", level, r); 792 793 off = THERMCTL_LVL0_DN_STATS; 794 r = readl(ts->regs + THERMCTL_LVL_REG(off, level)); 795 seq_printf(s, "Dn(%d)\n", r); 796 } 797 798 r = readl(ts->regs + THERMCTL_THERMTRIP_CTL); 799 state = REG_GET_MASK(r, ttgs[0]->thermtrip_any_en_mask); 800 seq_printf(s, "Thermtrip Any En(%d)\n", state); 801 for (i = 0; i < ts->soc->num_ttgs; i++) { 802 state = REG_GET_MASK(r, ttgs[i]->thermtrip_enable_mask); 803 seq_printf(s, " %s En(%d) ", ttgs[i]->name, state); 804 state = REG_GET_MASK(r, ttgs[i]->thermtrip_threshold_mask); 805 state *= ts->soc->thresh_grain; 806 seq_printf(s, "Thresh(%d)\n", state); 807 } 808 809 r = readl(ts->regs + THROT_GLOBAL_CFG); 810 seq_puts(s, "\n"); 811 seq_printf(s, "GLOBAL THROTTLE CONFIG: 0x%08x\n", r); 812 813 seq_puts(s, "---------------------------------------------------\n"); 814 r = readl(ts->regs + THROT_STATUS); 815 state = REG_GET_MASK(r, THROT_STATUS_BREACH_MASK); 816 seq_printf(s, "THROT STATUS: breach(%d) ", state); 817 state = REG_GET_MASK(r, THROT_STATUS_STATE_MASK); 818 seq_printf(s, "state(%d) ", state); 819 state = REG_GET_MASK(r, THROT_STATUS_ENABLED_MASK); 820 seq_printf(s, "enabled(%d)\n", state); 821 822 r = readl(ts->regs + CPU_PSKIP_STATUS); 823 if (ts->soc->use_ccroc) { 824 state = REG_GET_MASK(r, XPU_PSKIP_STATUS_ENABLED_MASK); 825 seq_printf(s, "CPU PSKIP STATUS: enabled(%d)\n", state); 826 } else { 827 state = REG_GET_MASK(r, XPU_PSKIP_STATUS_M_MASK); 828 seq_printf(s, "CPU PSKIP STATUS: M(%d) ", state); 829 state = REG_GET_MASK(r, XPU_PSKIP_STATUS_N_MASK); 830 seq_printf(s, "N(%d) ", state); 831 state = REG_GET_MASK(r, XPU_PSKIP_STATUS_ENABLED_MASK); 832 seq_printf(s, "enabled(%d)\n", state); 833 } 834 835 return 0; 836 } 837 838 DEFINE_SHOW_ATTRIBUTE(regs); 839 840 static void soctherm_debug_init(struct platform_device *pdev) 841 { 842 struct tegra_soctherm *tegra = platform_get_drvdata(pdev); 843 struct dentry *root, *file; 844 845 root = debugfs_create_dir("soctherm", NULL); 846 if (!root) { 847 dev_err(&pdev->dev, "failed to create debugfs directory\n"); 848 return; 849 } 850 851 tegra->debugfs_dir = root; 852 853 file = debugfs_create_file("reg_contents", 0644, root, 854 pdev, ®s_fops); 855 if (!file) { 856 dev_err(&pdev->dev, "failed to create debugfs file\n"); 857 debugfs_remove_recursive(tegra->debugfs_dir); 858 tegra->debugfs_dir = NULL; 859 } 860 } 861 #else 862 static inline void soctherm_debug_init(struct platform_device *pdev) {} 863 #endif 864 865 static int soctherm_clk_enable(struct platform_device *pdev, bool enable) 866 { 867 struct tegra_soctherm *tegra = platform_get_drvdata(pdev); 868 int err; 869 870 if (!tegra->clock_soctherm || !tegra->clock_tsensor) 871 return -EINVAL; 872 873 reset_control_assert(tegra->reset); 874 875 if (enable) { 876 err = clk_prepare_enable(tegra->clock_soctherm); 877 if (err) { 878 reset_control_deassert(tegra->reset); 879 return err; 880 } 881 882 err = clk_prepare_enable(tegra->clock_tsensor); 883 if (err) { 884 clk_disable_unprepare(tegra->clock_soctherm); 885 reset_control_deassert(tegra->reset); 886 return err; 887 } 888 } else { 889 clk_disable_unprepare(tegra->clock_tsensor); 890 clk_disable_unprepare(tegra->clock_soctherm); 891 } 892 893 reset_control_deassert(tegra->reset); 894 895 return 0; 896 } 897 898 static int throt_get_cdev_max_state(struct thermal_cooling_device *cdev, 899 unsigned long *max_state) 900 { 901 *max_state = 1; 902 return 0; 903 } 904 905 static int throt_get_cdev_cur_state(struct thermal_cooling_device *cdev, 906 unsigned long *cur_state) 907 { 908 struct tegra_soctherm *ts = cdev->devdata; 909 u32 r; 910 911 r = readl(ts->regs + THROT_STATUS); 912 if (REG_GET_MASK(r, THROT_STATUS_STATE_MASK)) 913 *cur_state = 1; 914 else 915 *cur_state = 0; 916 917 return 0; 918 } 919 920 static int throt_set_cdev_state(struct thermal_cooling_device *cdev, 921 unsigned long cur_state) 922 { 923 return 0; 924 } 925 926 static const struct thermal_cooling_device_ops throt_cooling_ops = { 927 .get_max_state = throt_get_cdev_max_state, 928 .get_cur_state = throt_get_cdev_cur_state, 929 .set_cur_state = throt_set_cdev_state, 930 }; 931 932 /** 933 * soctherm_init_hw_throt_cdev() - Parse the HW throttle configurations 934 * and register them as cooling devices. 935 */ 936 static void soctherm_init_hw_throt_cdev(struct platform_device *pdev) 937 { 938 struct device *dev = &pdev->dev; 939 struct tegra_soctherm *ts = dev_get_drvdata(dev); 940 struct device_node *np_stc, *np_stcc; 941 const char *name; 942 u32 val; 943 int i, r; 944 945 for (i = 0; i < THROTTLE_SIZE; i++) { 946 ts->throt_cfgs[i].name = throt_names[i]; 947 ts->throt_cfgs[i].id = i; 948 ts->throt_cfgs[i].init = false; 949 } 950 951 np_stc = of_get_child_by_name(dev->of_node, "throttle-cfgs"); 952 if (!np_stc) { 953 dev_info(dev, 954 "throttle-cfg: no throttle-cfgs - not enabling\n"); 955 return; 956 } 957 958 for_each_child_of_node(np_stc, np_stcc) { 959 struct soctherm_throt_cfg *stc; 960 struct thermal_cooling_device *tcd; 961 962 name = np_stcc->name; 963 stc = find_throttle_cfg_by_name(ts, name); 964 if (!stc) { 965 dev_err(dev, 966 "throttle-cfg: could not find %s\n", name); 967 continue; 968 } 969 970 r = of_property_read_u32(np_stcc, "nvidia,priority", &val); 971 if (r) { 972 dev_info(dev, 973 "throttle-cfg: %s: missing priority\n", name); 974 continue; 975 } 976 stc->priority = val; 977 978 if (ts->soc->use_ccroc) { 979 r = of_property_read_u32(np_stcc, 980 "nvidia,cpu-throt-level", 981 &val); 982 if (r) { 983 dev_info(dev, 984 "throttle-cfg: %s: missing cpu-throt-level\n", 985 name); 986 continue; 987 } 988 stc->cpu_throt_level = val; 989 } else { 990 r = of_property_read_u32(np_stcc, 991 "nvidia,cpu-throt-percent", 992 &val); 993 if (r) { 994 dev_info(dev, 995 "throttle-cfg: %s: missing cpu-throt-percent\n", 996 name); 997 continue; 998 } 999 stc->cpu_throt_depth = val; 1000 } 1001 1002 tcd = thermal_of_cooling_device_register(np_stcc, 1003 (char *)name, ts, 1004 &throt_cooling_ops); 1005 of_node_put(np_stcc); 1006 if (IS_ERR_OR_NULL(tcd)) { 1007 dev_err(dev, 1008 "throttle-cfg: %s: failed to register cooling device\n", 1009 name); 1010 continue; 1011 } 1012 1013 stc->cdev = tcd; 1014 stc->init = true; 1015 } 1016 1017 of_node_put(np_stc); 1018 } 1019 1020 /** 1021 * throttlectl_cpu_level_cfg() - programs CCROC NV_THERM level config 1022 * @level: describing the level LOW/MED/HIGH of throttling 1023 * 1024 * It's necessary to set up the CPU-local CCROC NV_THERM instance with 1025 * the M/N values desired for each level. This function does this. 1026 * 1027 * This function pre-programs the CCROC NV_THERM levels in terms of 1028 * pre-configured "Low", "Medium" or "Heavy" throttle levels which are 1029 * mapped to THROT_LEVEL_LOW, THROT_LEVEL_MED and THROT_LEVEL_HVY. 1030 */ 1031 static void throttlectl_cpu_level_cfg(struct tegra_soctherm *ts, int level) 1032 { 1033 u8 depth, dividend; 1034 u32 r; 1035 1036 switch (level) { 1037 case TEGRA_SOCTHERM_THROT_LEVEL_LOW: 1038 depth = 50; 1039 break; 1040 case TEGRA_SOCTHERM_THROT_LEVEL_MED: 1041 depth = 75; 1042 break; 1043 case TEGRA_SOCTHERM_THROT_LEVEL_HIGH: 1044 depth = 80; 1045 break; 1046 case TEGRA_SOCTHERM_THROT_LEVEL_NONE: 1047 return; 1048 default: 1049 return; 1050 } 1051 1052 dividend = THROT_DEPTH_DIVIDEND(depth); 1053 1054 /* setup PSKIP in ccroc nv_therm registers */ 1055 r = ccroc_readl(ts, CCROC_THROT_PSKIP_RAMP_CPU_REG(level)); 1056 r = REG_SET_MASK(r, CCROC_THROT_PSKIP_RAMP_DURATION_MASK, 0xff); 1057 r = REG_SET_MASK(r, CCROC_THROT_PSKIP_RAMP_STEP_MASK, 0xf); 1058 ccroc_writel(ts, r, CCROC_THROT_PSKIP_RAMP_CPU_REG(level)); 1059 1060 r = ccroc_readl(ts, CCROC_THROT_PSKIP_CTRL_CPU_REG(level)); 1061 r = REG_SET_MASK(r, CCROC_THROT_PSKIP_CTRL_ENB_MASK, 1); 1062 r = REG_SET_MASK(r, CCROC_THROT_PSKIP_CTRL_DIVIDEND_MASK, dividend); 1063 r = REG_SET_MASK(r, CCROC_THROT_PSKIP_CTRL_DIVISOR_MASK, 0xff); 1064 ccroc_writel(ts, r, CCROC_THROT_PSKIP_CTRL_CPU_REG(level)); 1065 } 1066 1067 /** 1068 * throttlectl_cpu_level_select() - program CPU pulse skipper config 1069 * @throt: the LIGHT/HEAVY of throttle event id 1070 * 1071 * Pulse skippers are used to throttle clock frequencies. This 1072 * function programs the pulse skippers based on @throt and platform 1073 * data. This function is used on SoCs which have CPU-local pulse 1074 * skipper control, such as T13x. It programs soctherm's interface to 1075 * Denver:CCROC NV_THERM in terms of Low, Medium and HIGH throttling 1076 * vectors. PSKIP_BYPASS mode is set as required per HW spec. 1077 */ 1078 static void throttlectl_cpu_level_select(struct tegra_soctherm *ts, 1079 enum soctherm_throttle_id throt) 1080 { 1081 u32 r, throt_vect; 1082 1083 /* Denver:CCROC NV_THERM interface N:3 Mapping */ 1084 switch (ts->throt_cfgs[throt].cpu_throt_level) { 1085 case TEGRA_SOCTHERM_THROT_LEVEL_LOW: 1086 throt_vect = THROT_VECT_LOW; 1087 break; 1088 case TEGRA_SOCTHERM_THROT_LEVEL_MED: 1089 throt_vect = THROT_VECT_MED; 1090 break; 1091 case TEGRA_SOCTHERM_THROT_LEVEL_HIGH: 1092 throt_vect = THROT_VECT_HIGH; 1093 break; 1094 default: 1095 throt_vect = THROT_VECT_NONE; 1096 break; 1097 } 1098 1099 r = readl(ts->regs + THROT_PSKIP_CTRL(throt, THROTTLE_DEV_CPU)); 1100 r = REG_SET_MASK(r, THROT_PSKIP_CTRL_ENABLE_MASK, 1); 1101 r = REG_SET_MASK(r, THROT_PSKIP_CTRL_VECT_CPU_MASK, throt_vect); 1102 r = REG_SET_MASK(r, THROT_PSKIP_CTRL_VECT2_CPU_MASK, throt_vect); 1103 writel(r, ts->regs + THROT_PSKIP_CTRL(throt, THROTTLE_DEV_CPU)); 1104 1105 /* bypass sequencer in soc_therm as it is programmed in ccroc */ 1106 r = REG_SET_MASK(0, THROT_PSKIP_RAMP_SEQ_BYPASS_MODE_MASK, 1); 1107 writel(r, ts->regs + THROT_PSKIP_RAMP(throt, THROTTLE_DEV_CPU)); 1108 } 1109 1110 /** 1111 * throttlectl_cpu_mn() - program CPU pulse skipper configuration 1112 * @throt: the LIGHT/HEAVY of throttle event id 1113 * 1114 * Pulse skippers are used to throttle clock frequencies. This 1115 * function programs the pulse skippers based on @throt and platform 1116 * data. This function is used for CPUs that have "remote" pulse 1117 * skipper control, e.g., the CPU pulse skipper is controlled by the 1118 * SOC_THERM IP block. (SOC_THERM is located outside the CPU 1119 * complex.) 1120 */ 1121 static void throttlectl_cpu_mn(struct tegra_soctherm *ts, 1122 enum soctherm_throttle_id throt) 1123 { 1124 u32 r; 1125 int depth; 1126 u8 dividend; 1127 1128 depth = ts->throt_cfgs[throt].cpu_throt_depth; 1129 dividend = THROT_DEPTH_DIVIDEND(depth); 1130 1131 r = readl(ts->regs + THROT_PSKIP_CTRL(throt, THROTTLE_DEV_CPU)); 1132 r = REG_SET_MASK(r, THROT_PSKIP_CTRL_ENABLE_MASK, 1); 1133 r = REG_SET_MASK(r, THROT_PSKIP_CTRL_DIVIDEND_MASK, dividend); 1134 r = REG_SET_MASK(r, THROT_PSKIP_CTRL_DIVISOR_MASK, 0xff); 1135 writel(r, ts->regs + THROT_PSKIP_CTRL(throt, THROTTLE_DEV_CPU)); 1136 1137 r = readl(ts->regs + THROT_PSKIP_RAMP(throt, THROTTLE_DEV_CPU)); 1138 r = REG_SET_MASK(r, THROT_PSKIP_RAMP_DURATION_MASK, 0xff); 1139 r = REG_SET_MASK(r, THROT_PSKIP_RAMP_STEP_MASK, 0xf); 1140 writel(r, ts->regs + THROT_PSKIP_RAMP(throt, THROTTLE_DEV_CPU)); 1141 } 1142 1143 /** 1144 * soctherm_throttle_program() - programs pulse skippers' configuration 1145 * @throt: the LIGHT/HEAVY of the throttle event id. 1146 * 1147 * Pulse skippers are used to throttle clock frequencies. 1148 * This function programs the pulse skippers. 1149 */ 1150 static void soctherm_throttle_program(struct tegra_soctherm *ts, 1151 enum soctherm_throttle_id throt) 1152 { 1153 u32 r; 1154 struct soctherm_throt_cfg stc = ts->throt_cfgs[throt]; 1155 1156 if (!stc.init) 1157 return; 1158 1159 /* Setup PSKIP parameters */ 1160 if (ts->soc->use_ccroc) 1161 throttlectl_cpu_level_select(ts, throt); 1162 else 1163 throttlectl_cpu_mn(ts, throt); 1164 1165 r = REG_SET_MASK(0, THROT_PRIORITY_LITE_PRIO_MASK, stc.priority); 1166 writel(r, ts->regs + THROT_PRIORITY_CTRL(throt)); 1167 1168 r = REG_SET_MASK(0, THROT_DELAY_LITE_DELAY_MASK, 0); 1169 writel(r, ts->regs + THROT_DELAY_CTRL(throt)); 1170 1171 r = readl(ts->regs + THROT_PRIORITY_LOCK); 1172 r = REG_GET_MASK(r, THROT_PRIORITY_LOCK_PRIORITY_MASK); 1173 if (r >= stc.priority) 1174 return; 1175 r = REG_SET_MASK(0, THROT_PRIORITY_LOCK_PRIORITY_MASK, 1176 stc.priority); 1177 writel(r, ts->regs + THROT_PRIORITY_LOCK); 1178 } 1179 1180 static void tegra_soctherm_throttle(struct device *dev) 1181 { 1182 struct tegra_soctherm *ts = dev_get_drvdata(dev); 1183 u32 v; 1184 int i; 1185 1186 /* configure LOW, MED and HIGH levels for CCROC NV_THERM */ 1187 if (ts->soc->use_ccroc) { 1188 throttlectl_cpu_level_cfg(ts, TEGRA_SOCTHERM_THROT_LEVEL_LOW); 1189 throttlectl_cpu_level_cfg(ts, TEGRA_SOCTHERM_THROT_LEVEL_MED); 1190 throttlectl_cpu_level_cfg(ts, TEGRA_SOCTHERM_THROT_LEVEL_HIGH); 1191 } 1192 1193 /* Thermal HW throttle programming */ 1194 for (i = 0; i < THROTTLE_SIZE; i++) 1195 soctherm_throttle_program(ts, i); 1196 1197 v = REG_SET_MASK(0, THROT_GLOBAL_ENB_MASK, 1); 1198 if (ts->soc->use_ccroc) { 1199 ccroc_writel(ts, v, CCROC_GLOBAL_CFG); 1200 1201 v = ccroc_readl(ts, CCROC_SUPER_CCLKG_DIVIDER); 1202 v = REG_SET_MASK(v, CDIVG_USE_THERM_CONTROLS_MASK, 1); 1203 ccroc_writel(ts, v, CCROC_SUPER_CCLKG_DIVIDER); 1204 } else { 1205 writel(v, ts->regs + THROT_GLOBAL_CFG); 1206 1207 v = readl(ts->clk_regs + CAR_SUPER_CCLKG_DIVIDER); 1208 v = REG_SET_MASK(v, CDIVG_USE_THERM_CONTROLS_MASK, 1); 1209 writel(v, ts->clk_regs + CAR_SUPER_CCLKG_DIVIDER); 1210 } 1211 1212 /* initialize stats collection */ 1213 v = STATS_CTL_CLR_DN | STATS_CTL_EN_DN | 1214 STATS_CTL_CLR_UP | STATS_CTL_EN_UP; 1215 writel(v, ts->regs + THERMCTL_STATS_CTL); 1216 } 1217 1218 static void soctherm_init(struct platform_device *pdev) 1219 { 1220 struct tegra_soctherm *tegra = platform_get_drvdata(pdev); 1221 const struct tegra_tsensor_group **ttgs = tegra->soc->ttgs; 1222 int i; 1223 u32 pdiv, hotspot; 1224 1225 /* Initialize raw sensors */ 1226 for (i = 0; i < tegra->soc->num_tsensors; ++i) 1227 enable_tsensor(tegra, i); 1228 1229 /* program pdiv and hotspot offsets per THERM */ 1230 pdiv = readl(tegra->regs + SENSOR_PDIV); 1231 hotspot = readl(tegra->regs + SENSOR_HOTSPOT_OFF); 1232 for (i = 0; i < tegra->soc->num_ttgs; ++i) { 1233 pdiv = REG_SET_MASK(pdiv, ttgs[i]->pdiv_mask, 1234 ttgs[i]->pdiv); 1235 /* hotspot offset from PLLX, doesn't need to configure PLLX */ 1236 if (ttgs[i]->id == TEGRA124_SOCTHERM_SENSOR_PLLX) 1237 continue; 1238 hotspot = REG_SET_MASK(hotspot, 1239 ttgs[i]->pllx_hotspot_mask, 1240 ttgs[i]->pllx_hotspot_diff); 1241 } 1242 writel(pdiv, tegra->regs + SENSOR_PDIV); 1243 writel(hotspot, tegra->regs + SENSOR_HOTSPOT_OFF); 1244 1245 /* Configure hw throttle */ 1246 tegra_soctherm_throttle(&pdev->dev); 1247 } 1248 1249 static const struct of_device_id tegra_soctherm_of_match[] = { 1250 #ifdef CONFIG_ARCH_TEGRA_124_SOC 1251 { 1252 .compatible = "nvidia,tegra124-soctherm", 1253 .data = &tegra124_soctherm, 1254 }, 1255 #endif 1256 #ifdef CONFIG_ARCH_TEGRA_132_SOC 1257 { 1258 .compatible = "nvidia,tegra132-soctherm", 1259 .data = &tegra132_soctherm, 1260 }, 1261 #endif 1262 #ifdef CONFIG_ARCH_TEGRA_210_SOC 1263 { 1264 .compatible = "nvidia,tegra210-soctherm", 1265 .data = &tegra210_soctherm, 1266 }, 1267 #endif 1268 { }, 1269 }; 1270 MODULE_DEVICE_TABLE(of, tegra_soctherm_of_match); 1271 1272 static int tegra_soctherm_probe(struct platform_device *pdev) 1273 { 1274 const struct of_device_id *match; 1275 struct tegra_soctherm *tegra; 1276 struct thermal_zone_device *z; 1277 struct tsensor_shared_calib shared_calib; 1278 struct resource *res; 1279 struct tegra_soctherm_soc *soc; 1280 unsigned int i; 1281 int err; 1282 1283 match = of_match_node(tegra_soctherm_of_match, pdev->dev.of_node); 1284 if (!match) 1285 return -ENODEV; 1286 1287 soc = (struct tegra_soctherm_soc *)match->data; 1288 if (soc->num_ttgs > TEGRA124_SOCTHERM_SENSOR_NUM) 1289 return -EINVAL; 1290 1291 tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL); 1292 if (!tegra) 1293 return -ENOMEM; 1294 1295 dev_set_drvdata(&pdev->dev, tegra); 1296 1297 tegra->soc = soc; 1298 1299 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, 1300 "soctherm-reg"); 1301 tegra->regs = devm_ioremap_resource(&pdev->dev, res); 1302 if (IS_ERR(tegra->regs)) { 1303 dev_err(&pdev->dev, "can't get soctherm registers"); 1304 return PTR_ERR(tegra->regs); 1305 } 1306 1307 if (!tegra->soc->use_ccroc) { 1308 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, 1309 "car-reg"); 1310 tegra->clk_regs = devm_ioremap_resource(&pdev->dev, res); 1311 if (IS_ERR(tegra->clk_regs)) { 1312 dev_err(&pdev->dev, "can't get car clk registers"); 1313 return PTR_ERR(tegra->clk_regs); 1314 } 1315 } else { 1316 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, 1317 "ccroc-reg"); 1318 tegra->ccroc_regs = devm_ioremap_resource(&pdev->dev, res); 1319 if (IS_ERR(tegra->ccroc_regs)) { 1320 dev_err(&pdev->dev, "can't get ccroc registers"); 1321 return PTR_ERR(tegra->ccroc_regs); 1322 } 1323 } 1324 1325 tegra->reset = devm_reset_control_get(&pdev->dev, "soctherm"); 1326 if (IS_ERR(tegra->reset)) { 1327 dev_err(&pdev->dev, "can't get soctherm reset\n"); 1328 return PTR_ERR(tegra->reset); 1329 } 1330 1331 tegra->clock_tsensor = devm_clk_get(&pdev->dev, "tsensor"); 1332 if (IS_ERR(tegra->clock_tsensor)) { 1333 dev_err(&pdev->dev, "can't get tsensor clock\n"); 1334 return PTR_ERR(tegra->clock_tsensor); 1335 } 1336 1337 tegra->clock_soctherm = devm_clk_get(&pdev->dev, "soctherm"); 1338 if (IS_ERR(tegra->clock_soctherm)) { 1339 dev_err(&pdev->dev, "can't get soctherm clock\n"); 1340 return PTR_ERR(tegra->clock_soctherm); 1341 } 1342 1343 tegra->calib = devm_kcalloc(&pdev->dev, 1344 soc->num_tsensors, sizeof(u32), 1345 GFP_KERNEL); 1346 if (!tegra->calib) 1347 return -ENOMEM; 1348 1349 /* calculate shared calibration data */ 1350 err = tegra_calc_shared_calib(soc->tfuse, &shared_calib); 1351 if (err) 1352 return err; 1353 1354 /* calculate tsensor calibaration data */ 1355 for (i = 0; i < soc->num_tsensors; ++i) { 1356 err = tegra_calc_tsensor_calib(&soc->tsensors[i], 1357 &shared_calib, 1358 &tegra->calib[i]); 1359 if (err) 1360 return err; 1361 } 1362 1363 tegra->thermctl_tzs = devm_kcalloc(&pdev->dev, 1364 soc->num_ttgs, sizeof(z), 1365 GFP_KERNEL); 1366 if (!tegra->thermctl_tzs) 1367 return -ENOMEM; 1368 1369 err = soctherm_clk_enable(pdev, true); 1370 if (err) 1371 return err; 1372 1373 soctherm_init_hw_throt_cdev(pdev); 1374 1375 soctherm_init(pdev); 1376 1377 for (i = 0; i < soc->num_ttgs; ++i) { 1378 struct tegra_thermctl_zone *zone = 1379 devm_kzalloc(&pdev->dev, sizeof(*zone), GFP_KERNEL); 1380 if (!zone) { 1381 err = -ENOMEM; 1382 goto disable_clocks; 1383 } 1384 1385 zone->reg = tegra->regs + soc->ttgs[i]->sensor_temp_offset; 1386 zone->dev = &pdev->dev; 1387 zone->sg = soc->ttgs[i]; 1388 zone->ts = tegra; 1389 1390 z = devm_thermal_zone_of_sensor_register(&pdev->dev, 1391 soc->ttgs[i]->id, zone, 1392 &tegra_of_thermal_ops); 1393 if (IS_ERR(z)) { 1394 err = PTR_ERR(z); 1395 dev_err(&pdev->dev, "failed to register sensor: %d\n", 1396 err); 1397 goto disable_clocks; 1398 } 1399 1400 zone->tz = z; 1401 tegra->thermctl_tzs[soc->ttgs[i]->id] = z; 1402 1403 /* Configure hw trip points */ 1404 err = tegra_soctherm_set_hwtrips(&pdev->dev, soc->ttgs[i], z); 1405 if (err) 1406 goto disable_clocks; 1407 } 1408 1409 soctherm_debug_init(pdev); 1410 1411 return 0; 1412 1413 disable_clocks: 1414 soctherm_clk_enable(pdev, false); 1415 1416 return err; 1417 } 1418 1419 static int tegra_soctherm_remove(struct platform_device *pdev) 1420 { 1421 struct tegra_soctherm *tegra = platform_get_drvdata(pdev); 1422 1423 debugfs_remove_recursive(tegra->debugfs_dir); 1424 1425 soctherm_clk_enable(pdev, false); 1426 1427 return 0; 1428 } 1429 1430 static int __maybe_unused soctherm_suspend(struct device *dev) 1431 { 1432 struct platform_device *pdev = to_platform_device(dev); 1433 1434 soctherm_clk_enable(pdev, false); 1435 1436 return 0; 1437 } 1438 1439 static int __maybe_unused soctherm_resume(struct device *dev) 1440 { 1441 struct platform_device *pdev = to_platform_device(dev); 1442 struct tegra_soctherm *tegra = platform_get_drvdata(pdev); 1443 struct tegra_soctherm_soc *soc = tegra->soc; 1444 int err, i; 1445 1446 err = soctherm_clk_enable(pdev, true); 1447 if (err) { 1448 dev_err(&pdev->dev, 1449 "Resume failed: enable clocks failed\n"); 1450 return err; 1451 } 1452 1453 soctherm_init(pdev); 1454 1455 for (i = 0; i < soc->num_ttgs; ++i) { 1456 struct thermal_zone_device *tz; 1457 1458 tz = tegra->thermctl_tzs[soc->ttgs[i]->id]; 1459 err = tegra_soctherm_set_hwtrips(dev, soc->ttgs[i], tz); 1460 if (err) { 1461 dev_err(&pdev->dev, 1462 "Resume failed: set hwtrips failed\n"); 1463 return err; 1464 } 1465 } 1466 1467 return 0; 1468 } 1469 1470 static SIMPLE_DEV_PM_OPS(tegra_soctherm_pm, soctherm_suspend, soctherm_resume); 1471 1472 static struct platform_driver tegra_soctherm_driver = { 1473 .probe = tegra_soctherm_probe, 1474 .remove = tegra_soctherm_remove, 1475 .driver = { 1476 .name = "tegra_soctherm", 1477 .pm = &tegra_soctherm_pm, 1478 .of_match_table = tegra_soctherm_of_match, 1479 }, 1480 }; 1481 module_platform_driver(tegra_soctherm_driver); 1482 1483 MODULE_AUTHOR("Mikko Perttunen <mperttunen@nvidia.com>"); 1484 MODULE_DESCRIPTION("NVIDIA Tegra SOCTHERM thermal management driver"); 1485 MODULE_LICENSE("GPL v2"); 1486