soctherm-fuse.c 5.2 KB

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