soctherm-fuse.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014-2016, NVIDIA CORPORATION. All rights reserved.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/platform_device.h>
  7. #include <soc/tegra/fuse.h>
  8. #include "soctherm.h"
  9. #define NOMINAL_CALIB_FT 105
  10. #define NOMINAL_CALIB_CP 25
  11. #define FUSE_TSENSOR_CALIB_CP_TS_BASE_MASK 0x1fff
  12. #define FUSE_TSENSOR_CALIB_FT_TS_BASE_MASK (0x1fff << 13)
  13. #define FUSE_TSENSOR_CALIB_FT_TS_BASE_SHIFT 13
  14. #define FUSE_TSENSOR_COMMON 0x180
  15. /*
  16. * Tegra210: Layout of bits in FUSE_TSENSOR_COMMON:
  17. * 3 2 1 0
  18. * 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  19. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  20. * | BASE_FT | BASE_CP | SHFT_FT | SHIFT_CP |
  21. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  22. *
  23. * Tegra12x, etc:
  24. * In chips prior to Tegra210, this fuse was incorrectly sized as 26 bits,
  25. * and didn't hold SHIFT_CP in [31:26]. Therefore these missing six bits
  26. * were obtained via the FUSE_SPARE_REALIGNMENT_REG register [5:0].
  27. *
  28. * FUSE_TSENSOR_COMMON:
  29. * 3 2 1 0
  30. * 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  31. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  32. * |-----------| SHFT_FT | BASE_FT | BASE_CP |
  33. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  34. *
  35. * FUSE_SPARE_REALIGNMENT_REG:
  36. * 3 2 1 0
  37. * 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  38. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  39. * |---------------------------------------------------| SHIFT_CP |
  40. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  41. */
  42. #define CALIB_COEFFICIENT 1000000LL
  43. /**
  44. * div64_s64_precise() - wrapper for div64_s64()
  45. * @a: the dividend
  46. * @b: the divisor
  47. *
  48. * Implements division with fairly accurate rounding instead of truncation by
  49. * shifting the dividend to the left by 16 so that the quotient has a
  50. * much higher precision.
  51. *
  52. * Return: the quotient of a / b.
  53. */
  54. static s64 div64_s64_precise(s64 a, s32 b)
  55. {
  56. s64 r, al;
  57. /* Scale up for increased precision division */
  58. al = a << 16;
  59. r = div64_s64(al * 2 + 1, 2 * b);
  60. return r >> 16;
  61. }
  62. int tegra_calc_shared_calib(const struct tegra_soctherm_fuse *tfuse,
  63. struct tsensor_shared_calib *shared)
  64. {
  65. u32 val;
  66. s32 shifted_cp, shifted_ft;
  67. int err;
  68. err = tegra_fuse_readl(FUSE_TSENSOR_COMMON, &val);
  69. if (err)
  70. return err;
  71. shared->base_cp = (val & tfuse->fuse_base_cp_mask) >>
  72. tfuse->fuse_base_cp_shift;
  73. shared->base_ft = (val & tfuse->fuse_base_ft_mask) >>
  74. tfuse->fuse_base_ft_shift;
  75. shifted_ft = (val & tfuse->fuse_shift_ft_mask) >>
  76. tfuse->fuse_shift_ft_shift;
  77. shifted_ft = sign_extend32(shifted_ft, 4);
  78. if (tfuse->fuse_spare_realignment) {
  79. err = tegra_fuse_readl(tfuse->fuse_spare_realignment, &val);
  80. if (err)
  81. return err;
  82. }
  83. shifted_cp = sign_extend32(val, 5);
  84. shared->actual_temp_cp = 2 * NOMINAL_CALIB_CP + shifted_cp;
  85. shared->actual_temp_ft = 2 * NOMINAL_CALIB_FT + shifted_ft;
  86. return 0;
  87. }
  88. int tegra_calc_tsensor_calib(const struct tegra_tsensor *sensor,
  89. const struct tsensor_shared_calib *shared,
  90. u32 *calibration)
  91. {
  92. const struct tegra_tsensor_group *sensor_group;
  93. u32 val, calib;
  94. s32 actual_tsensor_ft, actual_tsensor_cp;
  95. s32 delta_sens, delta_temp;
  96. s32 mult, div;
  97. s16 therma, thermb;
  98. s64 temp;
  99. int err;
  100. sensor_group = sensor->group;
  101. err = tegra_fuse_readl(sensor->calib_fuse_offset, &val);
  102. if (err)
  103. return err;
  104. actual_tsensor_cp = (shared->base_cp * 64) + sign_extend32(val, 12);
  105. val = (val & FUSE_TSENSOR_CALIB_FT_TS_BASE_MASK) >>
  106. FUSE_TSENSOR_CALIB_FT_TS_BASE_SHIFT;
  107. actual_tsensor_ft = (shared->base_ft * 32) + sign_extend32(val, 12);
  108. delta_sens = actual_tsensor_ft - actual_tsensor_cp;
  109. delta_temp = shared->actual_temp_ft - shared->actual_temp_cp;
  110. mult = sensor_group->pdiv * sensor->config->tsample_ate;
  111. div = sensor->config->tsample * sensor_group->pdiv_ate;
  112. temp = (s64)delta_temp * (1LL << 13) * mult;
  113. therma = div64_s64_precise(temp, (s64)delta_sens * div);
  114. temp = ((s64)actual_tsensor_ft * shared->actual_temp_cp) -
  115. ((s64)actual_tsensor_cp * shared->actual_temp_ft);
  116. thermb = div64_s64_precise(temp, delta_sens);
  117. temp = (s64)therma * sensor->fuse_corr_alpha;
  118. therma = div64_s64_precise(temp, CALIB_COEFFICIENT);
  119. temp = (s64)thermb * sensor->fuse_corr_alpha + sensor->fuse_corr_beta;
  120. thermb = div64_s64_precise(temp, CALIB_COEFFICIENT);
  121. calib = ((u16)therma << SENSOR_CONFIG2_THERMA_SHIFT) |
  122. ((u16)thermb << SENSOR_CONFIG2_THERMB_SHIFT);
  123. *calibration = calib;
  124. return 0;
  125. }
  126. MODULE_AUTHOR("Wei Ni <[email protected]>");
  127. MODULE_DESCRIPTION("Tegra SOCTHERM fuse management");
  128. MODULE_LICENSE("GPL v2");