1 // SPDX-License-Identifier: GPL-2.0 2 // Copyright (C) 2020 Spreadtrum Communications Inc. 3 4 #include <linux/clk.h> 5 #include <linux/io.h> 6 #include <linux/iopoll.h> 7 #include <linux/module.h> 8 #include <linux/nvmem-consumer.h> 9 #include <linux/of.h> 10 #include <linux/platform_device.h> 11 #include <linux/slab.h> 12 #include <linux/thermal.h> 13 14 #define SPRD_THM_CTL 0x0 15 #define SPRD_THM_INT_EN 0x4 16 #define SPRD_THM_INT_STS 0x8 17 #define SPRD_THM_INT_RAW_STS 0xc 18 #define SPRD_THM_DET_PERIOD 0x10 19 #define SPRD_THM_INT_CLR 0x14 20 #define SPRD_THM_INT_CLR_ST 0x18 21 #define SPRD_THM_MON_PERIOD 0x4c 22 #define SPRD_THM_MON_CTL 0x50 23 #define SPRD_THM_INTERNAL_STS1 0x54 24 #define SPRD_THM_RAW_READ_MSK 0x3ff 25 26 #define SPRD_THM_OFFSET(id) ((id) * 0x4) 27 #define SPRD_THM_TEMP(id) (SPRD_THM_OFFSET(id) + 0x5c) 28 #define SPRD_THM_THRES(id) (SPRD_THM_OFFSET(id) + 0x2c) 29 30 #define SPRD_THM_SEN(id) BIT((id) + 2) 31 #define SPRD_THM_SEN_OVERHEAT_EN(id) BIT((id) + 8) 32 #define SPRD_THM_SEN_OVERHEAT_ALARM_EN(id) BIT((id) + 0) 33 34 /* bits definitions for register THM_CTL */ 35 #define SPRD_THM_SET_RDY_ST BIT(13) 36 #define SPRD_THM_SET_RDY BIT(12) 37 #define SPRD_THM_MON_EN BIT(1) 38 #define SPRD_THM_EN BIT(0) 39 40 /* bits definitions for register THM_INT_CTL */ 41 #define SPRD_THM_BIT_INT_EN BIT(26) 42 #define SPRD_THM_OVERHEAT_EN BIT(25) 43 #define SPRD_THM_OTP_TRIP_SHIFT 10 44 45 /* bits definitions for register SPRD_THM_INTERNAL_STS1 */ 46 #define SPRD_THM_TEMPER_RDY BIT(0) 47 48 #define SPRD_THM_DET_PERIOD_DATA 0x800 49 #define SPRD_THM_DET_PERIOD_MASK GENMASK(19, 0) 50 #define SPRD_THM_MON_MODE 0x7 51 #define SPRD_THM_MON_MODE_MASK GENMASK(3, 0) 52 #define SPRD_THM_MON_PERIOD_DATA 0x10 53 #define SPRD_THM_MON_PERIOD_MASK GENMASK(15, 0) 54 #define SPRD_THM_THRES_MASK GENMASK(19, 0) 55 #define SPRD_THM_INT_CLR_MASK GENMASK(24, 0) 56 57 /* thermal sensor calibration parameters */ 58 #define SPRD_THM_TEMP_LOW -40000 59 #define SPRD_THM_TEMP_HIGH 120000 60 #define SPRD_THM_OTP_TEMP 120000 61 #define SPRD_THM_HOT_TEMP 75000 62 #define SPRD_THM_RAW_DATA_LOW 0 63 #define SPRD_THM_RAW_DATA_HIGH 1000 64 #define SPRD_THM_SEN_NUM 8 65 #define SPRD_THM_DT_OFFSET 24 66 #define SPRD_THM_RATION_OFFSET 17 67 #define SPRD_THM_RATION_SIGN 16 68 69 #define SPRD_THM_RDYST_POLLING_TIME 10 70 #define SPRD_THM_RDYST_TIMEOUT 700 71 #define SPRD_THM_TEMP_READY_POLL_TIME 10000 72 #define SPRD_THM_TEMP_READY_TIMEOUT 600000 73 #define SPRD_THM_MAX_SENSOR 8 74 75 struct sprd_thermal_sensor { 76 struct thermal_zone_device *tzd; 77 struct sprd_thermal_data *data; 78 struct device *dev; 79 int cal_slope; 80 int cal_offset; 81 int id; 82 }; 83 84 struct sprd_thermal_data { 85 const struct sprd_thm_variant_data *var_data; 86 struct sprd_thermal_sensor *sensor[SPRD_THM_MAX_SENSOR]; 87 struct clk *clk; 88 void __iomem *base; 89 u32 ratio_off; 90 int ratio_sign; 91 int nr_sensors; 92 }; 93 94 /* 95 * The conversion between ADC and temperature is based on linear relationship, 96 * and use idea_k to specify the slope and ideal_b to specify the offset. 97 * 98 * Since different Spreadtrum SoCs have different ideal_k and ideal_b, 99 * we should save ideal_k and ideal_b in the device data structure. 100 */ 101 struct sprd_thm_variant_data { 102 u32 ideal_k; 103 u32 ideal_b; 104 }; 105 106 static const struct sprd_thm_variant_data ums512_data = { 107 .ideal_k = 262, 108 .ideal_b = 66400, 109 }; 110 111 static inline void sprd_thm_update_bits(void __iomem *reg, u32 mask, u32 val) 112 { 113 u32 tmp, orig; 114 115 orig = readl(reg); 116 tmp = orig & ~mask; 117 tmp |= val & mask; 118 writel(tmp, reg); 119 } 120 121 static int sprd_thm_cal_read(struct device_node *np, const char *cell_id, 122 u32 *val) 123 { 124 struct nvmem_cell *cell; 125 void *buf; 126 size_t len; 127 128 cell = of_nvmem_cell_get(np, cell_id); 129 if (IS_ERR(cell)) 130 return PTR_ERR(cell); 131 132 buf = nvmem_cell_read(cell, &len); 133 nvmem_cell_put(cell); 134 if (IS_ERR(buf)) 135 return PTR_ERR(buf); 136 137 if (len > sizeof(u32)) { 138 kfree(buf); 139 return -EINVAL; 140 } 141 142 memcpy(val, buf, len); 143 144 kfree(buf); 145 return 0; 146 } 147 148 static int sprd_thm_sensor_calibration(struct device_node *np, 149 struct sprd_thermal_data *thm, 150 struct sprd_thermal_sensor *sen) 151 { 152 int ret; 153 /* 154 * According to thermal datasheet, the default calibration offset is 64, 155 * and the default ratio is 1000. 156 */ 157 int dt_offset = 64, ratio = 1000; 158 159 ret = sprd_thm_cal_read(np, "sen_delta_cal", &dt_offset); 160 if (ret) 161 return ret; 162 163 ratio += thm->ratio_sign * thm->ratio_off; 164 165 /* 166 * According to the ideal slope K and ideal offset B, combined with 167 * calibration value of thermal from efuse, then calibrate the real 168 * slope k and offset b: 169 * k_cal = (k * ratio) / 1000. 170 * b_cal = b + (dt_offset - 64) * 500. 171 */ 172 sen->cal_slope = (thm->var_data->ideal_k * ratio) / 1000; 173 sen->cal_offset = thm->var_data->ideal_b + (dt_offset - 128) * 250; 174 175 return 0; 176 } 177 178 static int sprd_thm_rawdata_to_temp(struct sprd_thermal_sensor *sen, 179 u32 rawdata) 180 { 181 clamp(rawdata, (u32)SPRD_THM_RAW_DATA_LOW, (u32)SPRD_THM_RAW_DATA_HIGH); 182 183 /* 184 * According to the thermal datasheet, the formula of converting 185 * adc value to the temperature value should be: 186 * T_final = k_cal * x - b_cal. 187 */ 188 return sen->cal_slope * rawdata - sen->cal_offset; 189 } 190 191 static int sprd_thm_temp_to_rawdata(int temp, struct sprd_thermal_sensor *sen) 192 { 193 u32 val; 194 195 clamp(temp, (int)SPRD_THM_TEMP_LOW, (int)SPRD_THM_TEMP_HIGH); 196 197 /* 198 * According to the thermal datasheet, the formula of converting 199 * adc value to the temperature value should be: 200 * T_final = k_cal * x - b_cal. 201 */ 202 val = (temp + sen->cal_offset) / sen->cal_slope; 203 204 return clamp(val, val, (u32)(SPRD_THM_RAW_DATA_HIGH - 1)); 205 } 206 207 static int sprd_thm_read_temp(struct thermal_zone_device *tz, int *temp) 208 { 209 struct sprd_thermal_sensor *sen = thermal_zone_device_priv(tz); 210 u32 data; 211 212 data = readl(sen->data->base + SPRD_THM_TEMP(sen->id)) & 213 SPRD_THM_RAW_READ_MSK; 214 215 *temp = sprd_thm_rawdata_to_temp(sen, data); 216 217 return 0; 218 } 219 220 static const struct thermal_zone_device_ops sprd_thm_ops = { 221 .get_temp = sprd_thm_read_temp, 222 }; 223 224 static int sprd_thm_poll_ready_status(struct sprd_thermal_data *thm) 225 { 226 u32 val; 227 int ret; 228 229 /* 230 * Wait for thermal ready status before configuring thermal parameters. 231 */ 232 ret = readl_poll_timeout(thm->base + SPRD_THM_CTL, val, 233 !(val & SPRD_THM_SET_RDY_ST), 234 SPRD_THM_RDYST_POLLING_TIME, 235 SPRD_THM_RDYST_TIMEOUT); 236 if (ret) 237 return ret; 238 239 sprd_thm_update_bits(thm->base + SPRD_THM_CTL, SPRD_THM_MON_EN, 240 SPRD_THM_MON_EN); 241 sprd_thm_update_bits(thm->base + SPRD_THM_CTL, SPRD_THM_SET_RDY, 242 SPRD_THM_SET_RDY); 243 return 0; 244 } 245 246 static int sprd_thm_wait_temp_ready(struct sprd_thermal_data *thm) 247 { 248 u32 val; 249 250 /* Wait for first temperature data ready before reading temperature */ 251 return readl_poll_timeout(thm->base + SPRD_THM_INTERNAL_STS1, val, 252 !(val & SPRD_THM_TEMPER_RDY), 253 SPRD_THM_TEMP_READY_POLL_TIME, 254 SPRD_THM_TEMP_READY_TIMEOUT); 255 } 256 257 static int sprd_thm_set_ready(struct sprd_thermal_data *thm) 258 { 259 int ret; 260 261 ret = sprd_thm_poll_ready_status(thm); 262 if (ret) 263 return ret; 264 265 /* 266 * Clear interrupt status, enable thermal interrupt and enable thermal. 267 * 268 * The SPRD thermal controller integrates a hardware interrupt signal, 269 * which means if the temperature is overheat, it will generate an 270 * interrupt and notify the event to PMIC automatically to shutdown the 271 * system. So here we should enable the interrupt bits, though we have 272 * not registered an irq handler. 273 */ 274 writel(SPRD_THM_INT_CLR_MASK, thm->base + SPRD_THM_INT_CLR); 275 sprd_thm_update_bits(thm->base + SPRD_THM_INT_EN, 276 SPRD_THM_BIT_INT_EN, SPRD_THM_BIT_INT_EN); 277 sprd_thm_update_bits(thm->base + SPRD_THM_CTL, 278 SPRD_THM_EN, SPRD_THM_EN); 279 return 0; 280 } 281 282 static void sprd_thm_sensor_init(struct sprd_thermal_data *thm, 283 struct sprd_thermal_sensor *sen) 284 { 285 u32 otp_rawdata, hot_rawdata; 286 287 otp_rawdata = sprd_thm_temp_to_rawdata(SPRD_THM_OTP_TEMP, sen); 288 hot_rawdata = sprd_thm_temp_to_rawdata(SPRD_THM_HOT_TEMP, sen); 289 290 /* Enable the sensor' overheat temperature protection interrupt */ 291 sprd_thm_update_bits(thm->base + SPRD_THM_INT_EN, 292 SPRD_THM_SEN_OVERHEAT_ALARM_EN(sen->id), 293 SPRD_THM_SEN_OVERHEAT_ALARM_EN(sen->id)); 294 295 /* Set the sensor' overheat and hot threshold temperature */ 296 sprd_thm_update_bits(thm->base + SPRD_THM_THRES(sen->id), 297 SPRD_THM_THRES_MASK, 298 (otp_rawdata << SPRD_THM_OTP_TRIP_SHIFT) | 299 hot_rawdata); 300 301 /* Enable the corresponding sensor */ 302 sprd_thm_update_bits(thm->base + SPRD_THM_CTL, SPRD_THM_SEN(sen->id), 303 SPRD_THM_SEN(sen->id)); 304 } 305 306 static void sprd_thm_para_config(struct sprd_thermal_data *thm) 307 { 308 /* Set the period of two valid temperature detection action */ 309 sprd_thm_update_bits(thm->base + SPRD_THM_DET_PERIOD, 310 SPRD_THM_DET_PERIOD_MASK, SPRD_THM_DET_PERIOD); 311 312 /* Set the sensors' monitor mode */ 313 sprd_thm_update_bits(thm->base + SPRD_THM_MON_CTL, 314 SPRD_THM_MON_MODE_MASK, SPRD_THM_MON_MODE); 315 316 /* Set the sensors' monitor period */ 317 sprd_thm_update_bits(thm->base + SPRD_THM_MON_PERIOD, 318 SPRD_THM_MON_PERIOD_MASK, SPRD_THM_MON_PERIOD); 319 } 320 321 static void sprd_thm_toggle_sensor(struct sprd_thermal_sensor *sen, bool on) 322 { 323 struct thermal_zone_device *tzd = sen->tzd; 324 325 if (on) 326 thermal_zone_device_enable(tzd); 327 else 328 thermal_zone_device_disable(tzd); 329 } 330 331 static int sprd_thm_probe(struct platform_device *pdev) 332 { 333 struct device_node *np = pdev->dev.of_node; 334 struct device_node *sen_child; 335 struct sprd_thermal_data *thm; 336 struct sprd_thermal_sensor *sen; 337 const struct sprd_thm_variant_data *pdata; 338 int ret, i; 339 u32 val; 340 341 pdata = of_device_get_match_data(&pdev->dev); 342 if (!pdata) { 343 dev_err(&pdev->dev, "No matching driver data found\n"); 344 return -EINVAL; 345 } 346 347 thm = devm_kzalloc(&pdev->dev, sizeof(*thm), GFP_KERNEL); 348 if (!thm) 349 return -ENOMEM; 350 351 thm->var_data = pdata; 352 thm->base = devm_platform_ioremap_resource(pdev, 0); 353 if (IS_ERR(thm->base)) 354 return PTR_ERR(thm->base); 355 356 thm->nr_sensors = of_get_child_count(np); 357 if (thm->nr_sensors == 0 || thm->nr_sensors > SPRD_THM_MAX_SENSOR) { 358 dev_err(&pdev->dev, "incorrect sensor count\n"); 359 return -EINVAL; 360 } 361 362 thm->clk = devm_clk_get_enabled(&pdev->dev, "enable"); 363 if (IS_ERR(thm->clk)) { 364 dev_err(&pdev->dev, "failed to get enable clock\n"); 365 return PTR_ERR(thm->clk); 366 } 367 368 sprd_thm_para_config(thm); 369 370 ret = sprd_thm_cal_read(np, "thm_sign_cal", &val); 371 if (ret) 372 return ret; 373 374 if (val > 0) 375 thm->ratio_sign = -1; 376 else 377 thm->ratio_sign = 1; 378 379 ret = sprd_thm_cal_read(np, "thm_ratio_cal", &thm->ratio_off); 380 if (ret) 381 return ret; 382 383 for_each_child_of_node(np, sen_child) { 384 sen = devm_kzalloc(&pdev->dev, sizeof(*sen), GFP_KERNEL); 385 if (!sen) { 386 ret = -ENOMEM; 387 goto of_put; 388 } 389 390 sen->data = thm; 391 sen->dev = &pdev->dev; 392 393 ret = of_property_read_u32(sen_child, "reg", &sen->id); 394 if (ret) { 395 dev_err(&pdev->dev, "get sensor reg failed"); 396 goto of_put; 397 } 398 399 ret = sprd_thm_sensor_calibration(sen_child, thm, sen); 400 if (ret) { 401 dev_err(&pdev->dev, "efuse cal analysis failed"); 402 goto of_put; 403 } 404 405 sprd_thm_sensor_init(thm, sen); 406 407 sen->tzd = devm_thermal_of_zone_register(sen->dev, 408 sen->id, 409 sen, 410 &sprd_thm_ops); 411 if (IS_ERR(sen->tzd)) { 412 dev_err(&pdev->dev, "register thermal zone failed %d\n", 413 sen->id); 414 ret = PTR_ERR(sen->tzd); 415 goto of_put; 416 } 417 418 thm->sensor[sen->id] = sen; 419 } 420 /* sen_child set to NULL at this point */ 421 422 ret = sprd_thm_set_ready(thm); 423 if (ret) 424 goto of_put; 425 426 ret = sprd_thm_wait_temp_ready(thm); 427 if (ret) 428 goto of_put; 429 430 for (i = 0; i < thm->nr_sensors; i++) 431 sprd_thm_toggle_sensor(thm->sensor[i], true); 432 433 platform_set_drvdata(pdev, thm); 434 return 0; 435 436 of_put: 437 of_node_put(sen_child); 438 return ret; 439 } 440 441 #ifdef CONFIG_PM_SLEEP 442 static void sprd_thm_hw_suspend(struct sprd_thermal_data *thm) 443 { 444 int i; 445 446 for (i = 0; i < thm->nr_sensors; i++) { 447 sprd_thm_update_bits(thm->base + SPRD_THM_CTL, 448 SPRD_THM_SEN(thm->sensor[i]->id), 0); 449 } 450 451 sprd_thm_update_bits(thm->base + SPRD_THM_CTL, 452 SPRD_THM_EN, 0x0); 453 } 454 455 static int sprd_thm_suspend(struct device *dev) 456 { 457 struct sprd_thermal_data *thm = dev_get_drvdata(dev); 458 int i; 459 460 for (i = 0; i < thm->nr_sensors; i++) 461 sprd_thm_toggle_sensor(thm->sensor[i], false); 462 463 sprd_thm_hw_suspend(thm); 464 clk_disable_unprepare(thm->clk); 465 466 return 0; 467 } 468 469 static int sprd_thm_hw_resume(struct sprd_thermal_data *thm) 470 { 471 int ret, i; 472 473 for (i = 0; i < thm->nr_sensors; i++) { 474 sprd_thm_update_bits(thm->base + SPRD_THM_CTL, 475 SPRD_THM_SEN(thm->sensor[i]->id), 476 SPRD_THM_SEN(thm->sensor[i]->id)); 477 } 478 479 ret = sprd_thm_poll_ready_status(thm); 480 if (ret) 481 return ret; 482 483 writel(SPRD_THM_INT_CLR_MASK, thm->base + SPRD_THM_INT_CLR); 484 sprd_thm_update_bits(thm->base + SPRD_THM_CTL, 485 SPRD_THM_EN, SPRD_THM_EN); 486 return sprd_thm_wait_temp_ready(thm); 487 } 488 489 static int sprd_thm_resume(struct device *dev) 490 { 491 struct sprd_thermal_data *thm = dev_get_drvdata(dev); 492 int ret, i; 493 494 ret = clk_prepare_enable(thm->clk); 495 if (ret) 496 return ret; 497 498 ret = sprd_thm_hw_resume(thm); 499 if (ret) 500 goto disable_clk; 501 502 for (i = 0; i < thm->nr_sensors; i++) 503 sprd_thm_toggle_sensor(thm->sensor[i], true); 504 505 return 0; 506 507 disable_clk: 508 clk_disable_unprepare(thm->clk); 509 return ret; 510 } 511 #endif 512 513 static void sprd_thm_remove(struct platform_device *pdev) 514 { 515 struct sprd_thermal_data *thm = platform_get_drvdata(pdev); 516 int i; 517 518 for (i = 0; i < thm->nr_sensors; i++) { 519 sprd_thm_toggle_sensor(thm->sensor[i], false); 520 devm_thermal_of_zone_unregister(&pdev->dev, 521 thm->sensor[i]->tzd); 522 } 523 } 524 525 static const struct of_device_id sprd_thermal_of_match[] = { 526 { .compatible = "sprd,ums512-thermal", .data = &ums512_data }, 527 { }, 528 }; 529 MODULE_DEVICE_TABLE(of, sprd_thermal_of_match); 530 531 static const struct dev_pm_ops sprd_thermal_pm_ops = { 532 SET_SYSTEM_SLEEP_PM_OPS(sprd_thm_suspend, sprd_thm_resume) 533 }; 534 535 static struct platform_driver sprd_thermal_driver = { 536 .probe = sprd_thm_probe, 537 .remove_new = sprd_thm_remove, 538 .driver = { 539 .name = "sprd-thermal", 540 .pm = &sprd_thermal_pm_ops, 541 .of_match_table = sprd_thermal_of_match, 542 }, 543 }; 544 545 module_platform_driver(sprd_thermal_driver); 546 547 MODULE_AUTHOR("Freeman Liu <freeman.liu@unisoc.com>"); 548 MODULE_DESCRIPTION("Spreadtrum thermal driver"); 549 MODULE_LICENSE("GPL v2"); 550