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