830-v6.4-21-thermal-drivers-mediatek-Add-temperature-constraints.patch 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. From 681b652c9dfc4037d4a55b2733e091a4e1a5de18 Mon Sep 17 00:00:00 2001
  2. From: AngeloGioacchino Del Regno <[email protected]>
  3. Date: Wed, 19 Apr 2023 08:11:46 +0200
  4. Subject: [PATCH 17/42] thermal/drivers/mediatek: Add temperature constraints
  5. to validate read
  6. MIME-Version: 1.0
  7. Content-Type: text/plain; charset=UTF-8
  8. Content-Transfer-Encoding: 8bit
  9. The AUXADC thermal v1 allows reading temperature range between -20°C to
  10. 150°C and any value out of this range is invalid.
  11. Add new definitions for MT8173_TEMP_{MIN_MAX} and a new small helper
  12. mtk_thermal_temp_is_valid() to check if new readings are in range: if
  13. not, we tell to the API that the reading is invalid by returning
  14. THERMAL_TEMP_INVALID.
  15. It was chosen to introduce the helper function because, even though this
  16. temperature range is realistically ok for all, it comes from a downstream
  17. kernel driver for version 1, but here we also support v2 and v3 which may
  18. may have wider constraints.
  19. Signed-off-by: AngeloGioacchino Del Regno <[email protected]>
  20. Signed-off-by: Daniel Lezcano <[email protected]>
  21. Link: https://lore.kernel.org/r/[email protected]
  22. ---
  23. drivers/thermal/mediatek/auxadc_thermal.c | 24 +++++++++++++++++------
  24. 1 file changed, 18 insertions(+), 6 deletions(-)
  25. --- a/drivers/thermal/mediatek/auxadc_thermal.c
  26. +++ b/drivers/thermal/mediatek/auxadc_thermal.c
  27. @@ -116,6 +116,10 @@
  28. /* The calibration coefficient of sensor */
  29. #define MT8173_CALIBRATION 165
  30. +/* Valid temperatures range */
  31. +#define MT8173_TEMP_MIN -20000
  32. +#define MT8173_TEMP_MAX 150000
  33. +
  34. /*
  35. * Layout of the fuses providing the calibration data
  36. * These macros could be used for MT8183, MT8173, MT2701, and MT2712.
  37. @@ -689,6 +693,11 @@ static const struct mtk_thermal_data mt7
  38. .version = MTK_THERMAL_V3,
  39. };
  40. +static bool mtk_thermal_temp_is_valid(int temp)
  41. +{
  42. + return (temp >= MT8173_TEMP_MIN) && (temp <= MT8173_TEMP_MAX);
  43. +}
  44. +
  45. /**
  46. * raw_to_mcelsius_v1 - convert a raw ADC value to mcelsius
  47. * @mt: The thermal controller
  48. @@ -815,14 +824,17 @@ static int mtk_thermal_bank_temperature(
  49. temp = mt->raw_to_mcelsius(
  50. mt, conf->bank_data[bank->id].sensors[i], raw);
  51. -
  52. /*
  53. - * The first read of a sensor often contains very high bogus
  54. - * temperature value. Filter these out so that the system does
  55. - * not immediately shut down.
  56. + * Depending on the filt/sen intervals and ADC polling time,
  57. + * we may need up to 60 milliseconds after initialization: this
  58. + * will result in the first reading containing an out of range
  59. + * temperature value.
  60. + * Validate the reading to both address the aforementioned issue
  61. + * and to eventually avoid bogus readings during runtime in the
  62. + * event that the AUXADC gets unstable due to high EMI, etc.
  63. */
  64. - if (temp > 200000)
  65. - temp = 0;
  66. + if (!mtk_thermal_temp_is_valid(temp))
  67. + temp = THERMAL_TEMP_INVALID;
  68. if (temp > max)
  69. max = temp;