Re: [PATCH v6 4/4] thermal: k3_j72xx_bandgap: Add the bandgap driver support
From: Dan Carpenter
Date: Wed May 04 2022 - 04:01:10 EST
Hi Keerthy,
url: https://github.com/intel-lab-lkp/linux/commits/Keerthy/thermal-k3_j72xx_bandgap-Add-the-bandgap-driver-support/20220427-144949
base: https://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git for-next
config: arm-randconfig-m031-20220501 (https://download.01.org/0day-ci/archive/20220503/202205030045.5GxgWkst-lkp@xxxxxxxxx/config)
compiler: arm-linux-gnueabi-gcc (GCC) 11.3.0
If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@xxxxxxxxx>
Reported-by: Dan Carpenter <dan.carpenter@xxxxxxxxxx>
smatch warnings:
drivers/thermal/k3_j72xx_bandgap.c:521 k3_j72xx_bandgap_probe() warn: possible memory leak of 'ref_table'
vim +/ref_table +521 drivers/thermal/k3_j72xx_bandgap.c
bb7f95a68cbaa1 Keerthy 2022-04-27 355 static int k3_j72xx_bandgap_probe(struct platform_device *pdev)
bb7f95a68cbaa1 Keerthy 2022-04-27 356 {
bb7f95a68cbaa1 Keerthy 2022-04-27 357 int ret = 0, cnt, val, id;
bb7f95a68cbaa1 Keerthy 2022-04-27 358 int high_max, low_temp;
bb7f95a68cbaa1 Keerthy 2022-04-27 359 struct resource *res;
bb7f95a68cbaa1 Keerthy 2022-04-27 360 struct device *dev = &pdev->dev;
bb7f95a68cbaa1 Keerthy 2022-04-27 361 struct k3_j72xx_bandgap *bgp;
bb7f95a68cbaa1 Keerthy 2022-04-27 362 struct k3_thermal_data *data;
bb7f95a68cbaa1 Keerthy 2022-04-27 363 int workaround_needed = 0;
bb7f95a68cbaa1 Keerthy 2022-04-27 364 const struct k3_j72xx_bandgap_data *driver_data;
bb7f95a68cbaa1 Keerthy 2022-04-27 365 struct thermal_zone_device *ti_thermal;
bb7f95a68cbaa1 Keerthy 2022-04-27 366 int *ref_table;
bb7f95a68cbaa1 Keerthy 2022-04-27 367 struct err_values err_vals;
bb7f95a68cbaa1 Keerthy 2022-04-27 368
bb7f95a68cbaa1 Keerthy 2022-04-27 369 const s64 golden_factors[] = {
bb7f95a68cbaa1 Keerthy 2022-04-27 370 -490019999999999936,
bb7f95a68cbaa1 Keerthy 2022-04-27 371 3251200000000000,
bb7f95a68cbaa1 Keerthy 2022-04-27 372 -1705800000000,
bb7f95a68cbaa1 Keerthy 2022-04-27 373 603730000,
bb7f95a68cbaa1 Keerthy 2022-04-27 374 -92627,
bb7f95a68cbaa1 Keerthy 2022-04-27 375 };
bb7f95a68cbaa1 Keerthy 2022-04-27 376
bb7f95a68cbaa1 Keerthy 2022-04-27 377 const s64 pvt_wa_factors[] = {
bb7f95a68cbaa1 Keerthy 2022-04-27 378 -415230000000000000,
bb7f95a68cbaa1 Keerthy 2022-04-27 379 3126600000000000,
bb7f95a68cbaa1 Keerthy 2022-04-27 380 -1157800000000,
bb7f95a68cbaa1 Keerthy 2022-04-27 381 };
bb7f95a68cbaa1 Keerthy 2022-04-27 382
bb7f95a68cbaa1 Keerthy 2022-04-27 383 bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
bb7f95a68cbaa1 Keerthy 2022-04-27 384 if (!bgp)
bb7f95a68cbaa1 Keerthy 2022-04-27 385 return -ENOMEM;
bb7f95a68cbaa1 Keerthy 2022-04-27 386
bb7f95a68cbaa1 Keerthy 2022-04-27 387 bgp->dev = dev;
bb7f95a68cbaa1 Keerthy 2022-04-27 388 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
bb7f95a68cbaa1 Keerthy 2022-04-27 389 bgp->base = devm_ioremap_resource(dev, res);
bb7f95a68cbaa1 Keerthy 2022-04-27 390 if (IS_ERR(bgp->base))
bb7f95a68cbaa1 Keerthy 2022-04-27 391 return PTR_ERR(bgp->base);
bb7f95a68cbaa1 Keerthy 2022-04-27 392
bb7f95a68cbaa1 Keerthy 2022-04-27 393 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
bb7f95a68cbaa1 Keerthy 2022-04-27 394 bgp->cfg2_base = devm_ioremap_resource(dev, res);
bb7f95a68cbaa1 Keerthy 2022-04-27 395 if (IS_ERR(bgp->cfg2_base))
bb7f95a68cbaa1 Keerthy 2022-04-27 396 return PTR_ERR(bgp->cfg2_base);
bb7f95a68cbaa1 Keerthy 2022-04-27 397
bb7f95a68cbaa1 Keerthy 2022-04-27 398 res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
bb7f95a68cbaa1 Keerthy 2022-04-27 399 bgp->fuse_base = devm_ioremap_resource(dev, res);
bb7f95a68cbaa1 Keerthy 2022-04-27 400 if (IS_ERR(bgp->fuse_base))
bb7f95a68cbaa1 Keerthy 2022-04-27 401 return PTR_ERR(bgp->fuse_base);
bb7f95a68cbaa1 Keerthy 2022-04-27 402
bb7f95a68cbaa1 Keerthy 2022-04-27 403 driver_data = of_device_get_match_data(dev);
bb7f95a68cbaa1 Keerthy 2022-04-27 404 if (driver_data)
bb7f95a68cbaa1 Keerthy 2022-04-27 405 workaround_needed = driver_data->has_errata_i2128;
bb7f95a68cbaa1 Keerthy 2022-04-27 406
bb7f95a68cbaa1 Keerthy 2022-04-27 407 pm_runtime_enable(dev);
bb7f95a68cbaa1 Keerthy 2022-04-27 408 ret = pm_runtime_get_sync(dev);
bb7f95a68cbaa1 Keerthy 2022-04-27 409 if (ret < 0) {
bb7f95a68cbaa1 Keerthy 2022-04-27 410 pm_runtime_put_noidle(dev);
bb7f95a68cbaa1 Keerthy 2022-04-27 411 pm_runtime_disable(dev);
bb7f95a68cbaa1 Keerthy 2022-04-27 412 return ret;
bb7f95a68cbaa1 Keerthy 2022-04-27 413 }
bb7f95a68cbaa1 Keerthy 2022-04-27 414
bb7f95a68cbaa1 Keerthy 2022-04-27 415 /* Get the sensor count in the VTM */
bb7f95a68cbaa1 Keerthy 2022-04-27 416 val = readl(bgp->base + K3_VTM_DEVINFO_PWR0_OFFSET);
bb7f95a68cbaa1 Keerthy 2022-04-27 417 cnt = val & K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK;
bb7f95a68cbaa1 Keerthy 2022-04-27 418 cnt >>= __ffs(K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK);
bb7f95a68cbaa1 Keerthy 2022-04-27 419
bb7f95a68cbaa1 Keerthy 2022-04-27 420 data = devm_kcalloc(bgp->dev, cnt, sizeof(*data), GFP_KERNEL);
bb7f95a68cbaa1 Keerthy 2022-04-27 421 if (!data) {
bb7f95a68cbaa1 Keerthy 2022-04-27 422 ret = -ENOMEM;
bb7f95a68cbaa1 Keerthy 2022-04-27 423 goto err_alloc;
bb7f95a68cbaa1 Keerthy 2022-04-27 424 }
bb7f95a68cbaa1 Keerthy 2022-04-27 425
bb7f95a68cbaa1 Keerthy 2022-04-27 426 ref_table = kzalloc(sizeof(*ref_table) * TABLE_SIZE, GFP_KERNEL);
bb7f95a68cbaa1 Keerthy 2022-04-27 427 if (!ref_table) {
bb7f95a68cbaa1 Keerthy 2022-04-27 428 ret = -ENOMEM;
bb7f95a68cbaa1 Keerthy 2022-04-27 429 goto err_alloc;
bb7f95a68cbaa1 Keerthy 2022-04-27 430 }
bb7f95a68cbaa1 Keerthy 2022-04-27 431
bb7f95a68cbaa1 Keerthy 2022-04-27 432 derived_table = devm_kzalloc(bgp->dev, sizeof(*derived_table) * TABLE_SIZE,
bb7f95a68cbaa1 Keerthy 2022-04-27 433 GFP_KERNEL);
bb7f95a68cbaa1 Keerthy 2022-04-27 434 if (!derived_table) {
bb7f95a68cbaa1 Keerthy 2022-04-27 435 ret = -ENOMEM;
bb7f95a68cbaa1 Keerthy 2022-04-27 436 goto err_alloc;
ref_table is not freed on error.
bb7f95a68cbaa1 Keerthy 2022-04-27 437 }
bb7f95a68cbaa1 Keerthy 2022-04-27 438
bb7f95a68cbaa1 Keerthy 2022-04-27 439 /* Workaround not needed if bit30/bit31 is set even for J721e */
bb7f95a68cbaa1 Keerthy 2022-04-27 440 if (workaround_needed && (readl(bgp->fuse_base + 0x0) & 0xc0000000) == 0xc0000000)
bb7f95a68cbaa1 Keerthy 2022-04-27 441 workaround_needed = false;
bb7f95a68cbaa1 Keerthy 2022-04-27 442
bb7f95a68cbaa1 Keerthy 2022-04-27 443 dev_dbg(bgp->dev, "Work around %sneeded\n",
bb7f95a68cbaa1 Keerthy 2022-04-27 444 workaround_needed ? "not " : "");
bb7f95a68cbaa1 Keerthy 2022-04-27 445
bb7f95a68cbaa1 Keerthy 2022-04-27 446 if (!workaround_needed)
bb7f95a68cbaa1 Keerthy 2022-04-27 447 init_table(5, ref_table, golden_factors);
bb7f95a68cbaa1 Keerthy 2022-04-27 448 else
bb7f95a68cbaa1 Keerthy 2022-04-27 449 init_table(3, ref_table, pvt_wa_factors);
bb7f95a68cbaa1 Keerthy 2022-04-27 450
bb7f95a68cbaa1 Keerthy 2022-04-27 451 /* Register the thermal sensors */
bb7f95a68cbaa1 Keerthy 2022-04-27 452 for (id = 0; id < cnt; id++) {
bb7f95a68cbaa1 Keerthy 2022-04-27 453 data[id].bgp = bgp;
bb7f95a68cbaa1 Keerthy 2022-04-27 454 data[id].ctrl_offset = K3_VTM_TMPSENS0_CTRL_OFFSET + id * 0x20;
bb7f95a68cbaa1 Keerthy 2022-04-27 455 data[id].stat_offset = data[id].ctrl_offset +
bb7f95a68cbaa1 Keerthy 2022-04-27 456 K3_VTM_TMPSENS_STAT_OFFSET;
bb7f95a68cbaa1 Keerthy 2022-04-27 457
bb7f95a68cbaa1 Keerthy 2022-04-27 458 if (workaround_needed) {
bb7f95a68cbaa1 Keerthy 2022-04-27 459 /* ref adc values for -40C, 30C & 125C respectively */
bb7f95a68cbaa1 Keerthy 2022-04-27 460 err_vals.refs[0] = MINUS40CREF;
bb7f95a68cbaa1 Keerthy 2022-04-27 461 err_vals.refs[1] = PLUS30CREF;
bb7f95a68cbaa1 Keerthy 2022-04-27 462 err_vals.refs[2] = PLUS125CREF;
bb7f95a68cbaa1 Keerthy 2022-04-27 463 err_vals.refs[3] = PLUS150CREF;
bb7f95a68cbaa1 Keerthy 2022-04-27 464 get_efuse_values(id, &data[id], err_vals.errs, bgp);
bb7f95a68cbaa1 Keerthy 2022-04-27 465 }
bb7f95a68cbaa1 Keerthy 2022-04-27 466
bb7f95a68cbaa1 Keerthy 2022-04-27 467 if (id == 0 && workaround_needed)
bb7f95a68cbaa1 Keerthy 2022-04-27 468 prep_lookup_table(&err_vals, ref_table);
bb7f95a68cbaa1 Keerthy 2022-04-27 469 else if (id == 0 && !workaround_needed)
bb7f95a68cbaa1 Keerthy 2022-04-27 470 memcpy(derived_table, ref_table, TABLE_SIZE * 4);
bb7f95a68cbaa1 Keerthy 2022-04-27 471
bb7f95a68cbaa1 Keerthy 2022-04-27 472 val = readl(data[id].bgp->cfg2_base + data[id].ctrl_offset);
bb7f95a68cbaa1 Keerthy 2022-04-27 473 val |= (K3_VTM_TMPSENS_CTRL_MAXT_OUTRG_EN |
bb7f95a68cbaa1 Keerthy 2022-04-27 474 K3_VTM_TMPSENS_CTRL_SOC |
bb7f95a68cbaa1 Keerthy 2022-04-27 475 K3_VTM_TMPSENS_CTRL_CLRZ | BIT(4));
bb7f95a68cbaa1 Keerthy 2022-04-27 476 writel(val, data[id].bgp->cfg2_base + data[id].ctrl_offset);
bb7f95a68cbaa1 Keerthy 2022-04-27 477
bb7f95a68cbaa1 Keerthy 2022-04-27 478 bgp->ts_data[id] = &data[id];
bb7f95a68cbaa1 Keerthy 2022-04-27 479 ti_thermal =
bb7f95a68cbaa1 Keerthy 2022-04-27 480 devm_thermal_zone_of_sensor_register(bgp->dev, id,
bb7f95a68cbaa1 Keerthy 2022-04-27 481 &data[id],
bb7f95a68cbaa1 Keerthy 2022-04-27 482 &k3_of_thermal_ops);
bb7f95a68cbaa1 Keerthy 2022-04-27 483 if (IS_ERR(ti_thermal)) {
bb7f95a68cbaa1 Keerthy 2022-04-27 484 dev_err(bgp->dev, "thermal zone device is NULL\n");
bb7f95a68cbaa1 Keerthy 2022-04-27 485 ret = PTR_ERR(ti_thermal);
bb7f95a68cbaa1 Keerthy 2022-04-27 486 goto err_alloc;
bb7f95a68cbaa1 Keerthy 2022-04-27 487 }
bb7f95a68cbaa1 Keerthy 2022-04-27 488 }
bb7f95a68cbaa1 Keerthy 2022-04-27 489
bb7f95a68cbaa1 Keerthy 2022-04-27 490 /*
bb7f95a68cbaa1 Keerthy 2022-04-27 491 * Program TSHUT thresholds
bb7f95a68cbaa1 Keerthy 2022-04-27 492 * Step 1: set the thresholds to ~123C and 105C WKUP_VTM_MISC_CTRL2
bb7f95a68cbaa1 Keerthy 2022-04-27 493 * Step 2: WKUP_VTM_TMPSENS_CTRL_j set the MAXT_OUTRG_EN bit
bb7f95a68cbaa1 Keerthy 2022-04-27 494 * This is already taken care as per of init
bb7f95a68cbaa1 Keerthy 2022-04-27 495 * Step 3: WKUP_VTM_MISC_CTRL set the ANYMAXT_OUTRG_ALERT_EN bit
bb7f95a68cbaa1 Keerthy 2022-04-27 496 */
bb7f95a68cbaa1 Keerthy 2022-04-27 497 high_max = k3_j72xx_bandgap_temp_to_adc_code(MAX_TEMP);
bb7f95a68cbaa1 Keerthy 2022-04-27 498 low_temp = k3_j72xx_bandgap_temp_to_adc_code(COOL_DOWN_TEMP);
bb7f95a68cbaa1 Keerthy 2022-04-27 499
bb7f95a68cbaa1 Keerthy 2022-04-27 500 writel((low_temp << 16) | high_max, data[0].bgp->cfg2_base +
bb7f95a68cbaa1 Keerthy 2022-04-27 501 K3_VTM_MISC_CTRL2_OFFSET);
bb7f95a68cbaa1 Keerthy 2022-04-27 502 mdelay(100);
bb7f95a68cbaa1 Keerthy 2022-04-27 503 writel(K3_VTM_ANYMAXT_OUTRG_ALERT_EN, data[0].bgp->cfg2_base +
bb7f95a68cbaa1 Keerthy 2022-04-27 504 K3_VTM_MISC_CTRL_OFFSET);
bb7f95a68cbaa1 Keerthy 2022-04-27 505
bb7f95a68cbaa1 Keerthy 2022-04-27 506 platform_set_drvdata(pdev, bgp);
bb7f95a68cbaa1 Keerthy 2022-04-27 507
bb7f95a68cbaa1 Keerthy 2022-04-27 508 print_look_up_table(dev, ref_table);
bb7f95a68cbaa1 Keerthy 2022-04-27 509 /*
bb7f95a68cbaa1 Keerthy 2022-04-27 510 * Now that the derived_table has the appropriate look up values
bb7f95a68cbaa1 Keerthy 2022-04-27 511 * Free up the ref_table
bb7f95a68cbaa1 Keerthy 2022-04-27 512 */
bb7f95a68cbaa1 Keerthy 2022-04-27 513 kfree(ref_table);
bb7f95a68cbaa1 Keerthy 2022-04-27 514
bb7f95a68cbaa1 Keerthy 2022-04-27 515 return 0;
bb7f95a68cbaa1 Keerthy 2022-04-27 516
bb7f95a68cbaa1 Keerthy 2022-04-27 517 err_alloc:
bb7f95a68cbaa1 Keerthy 2022-04-27 518 pm_runtime_put_sync(&pdev->dev);
bb7f95a68cbaa1 Keerthy 2022-04-27 519 pm_runtime_disable(&pdev->dev);
bb7f95a68cbaa1 Keerthy 2022-04-27 520
bb7f95a68cbaa1 Keerthy 2022-04-27 @521 return ret;
bb7f95a68cbaa1 Keerthy 2022-04-27 522 }
--
0-DAY CI Kernel Test Service
https://01.org/lkp