1 From 26cc18a3d6d9eac21c4f4b4bb96147b2c6617c86 Mon Sep 17 00:00:00 2001
2 From: Frank Wunderlich <frank-w@public-files.de>
3 Date: Fri, 22 Sep 2023 07:50:19 +0200
4 Subject: [PATCH 36/42] thermal/drivers/mediatek/lvts_thermal: Make coeff
7 The upcoming mt7988 has different temperature coefficients so we
8 cannot use constants in the functions lvts_golden_temp_init,
9 lvts_golden_temp_init and lvts_raw_to_temp anymore.
11 Add a field in the lvts_ctrl pointing to the lvts_data which now
12 contains the soc-specific temperature coefficents.
14 To make the code better readable, rename static int coeff_b to
15 golden_temp_offset, COEFF_A to temp_factor and COEFF_B to temp_offset.
17 Signed-off-by: Frank Wunderlich <frank-w@public-files.de>
18 Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
19 Tested-by: Daniel Golle <daniel@makrotopia.org>
20 Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
21 Link: https://lore.kernel.org/r/20230922055020.6436-4-linux@fw-web.de
23 drivers/thermal/mediatek/lvts_thermal.c | 51 ++++++++++++++++---------
24 1 file changed, 34 insertions(+), 17 deletions(-)
26 --- a/drivers/thermal/mediatek/lvts_thermal.c
27 +++ b/drivers/thermal/mediatek/lvts_thermal.c
29 #define LVTS_SENSOR_MAX 4
30 #define LVTS_GOLDEN_TEMP_MAX 62
31 #define LVTS_GOLDEN_TEMP_DEFAULT 50
32 -#define LVTS_COEFF_A -250460
33 -#define LVTS_COEFF_B 250460
34 +#define LVTS_COEFF_A_MT8195 -250460
35 +#define LVTS_COEFF_B_MT8195 250460
37 #define LVTS_MSR_IMMEDIATE_MODE 0
38 #define LVTS_MSR_FILTERED_MODE 1
40 #define LVTS_MINIMUM_THRESHOLD 20000
42 static int golden_temp = LVTS_GOLDEN_TEMP_DEFAULT;
43 -static int coeff_b = LVTS_COEFF_B;
44 +static int golden_temp_offset;
46 struct lvts_sensor_data {
48 @@ -112,6 +112,8 @@ struct lvts_ctrl_data {
50 const struct lvts_ctrl_data *lvts_ctrl;
57 @@ -126,6 +128,7 @@ struct lvts_sensor {
60 struct lvts_sensor sensors[LVTS_SENSOR_MAX];
61 + const struct lvts_data *lvts_data;
62 u32 calibration[LVTS_SENSOR_MAX];
63 u32 hw_tshut_raw_temp;
65 @@ -247,21 +250,21 @@ static void lvts_debugfs_exit(struct lvt
69 -static int lvts_raw_to_temp(u32 raw_temp)
70 +static int lvts_raw_to_temp(u32 raw_temp, int temp_factor)
74 - temperature = ((s64)(raw_temp & 0xFFFF) * LVTS_COEFF_A) >> 14;
75 - temperature += coeff_b;
76 + temperature = ((s64)(raw_temp & 0xFFFF) * temp_factor) >> 14;
77 + temperature += golden_temp_offset;
82 -static u32 lvts_temp_to_raw(int temperature)
83 +static u32 lvts_temp_to_raw(int temperature, int temp_factor)
85 - u32 raw_temp = ((s64)(coeff_b - temperature)) << 14;
86 + u32 raw_temp = ((s64)(golden_temp_offset - temperature)) << 14;
88 - raw_temp = div_s64(raw_temp, -LVTS_COEFF_A);
89 + raw_temp = div_s64(raw_temp, -temp_factor);
93 @@ -269,6 +272,9 @@ static u32 lvts_temp_to_raw(int temperat
94 static int lvts_get_temp(struct thermal_zone_device *tz, int *temp)
96 struct lvts_sensor *lvts_sensor = thermal_zone_device_priv(tz);
97 + struct lvts_ctrl *lvts_ctrl = container_of(lvts_sensor, struct lvts_ctrl,
98 + sensors[lvts_sensor->id]);
99 + const struct lvts_data *lvts_data = lvts_ctrl->lvts_data;
100 void __iomem *msr = lvts_sensor->msr;
103 @@ -301,7 +307,7 @@ static int lvts_get_temp(struct thermal_
107 - *temp = lvts_raw_to_temp(value & 0xFFFF);
108 + *temp = lvts_raw_to_temp(value & 0xFFFF, lvts_data->temp_factor);
112 @@ -348,10 +354,13 @@ static bool lvts_should_update_thresh(st
113 static int lvts_set_trips(struct thermal_zone_device *tz, int low, int high)
115 struct lvts_sensor *lvts_sensor = thermal_zone_device_priv(tz);
116 - struct lvts_ctrl *lvts_ctrl = container_of(lvts_sensor, struct lvts_ctrl, sensors[lvts_sensor->id]);
117 + struct lvts_ctrl *lvts_ctrl = container_of(lvts_sensor, struct lvts_ctrl,
118 + sensors[lvts_sensor->id]);
119 + const struct lvts_data *lvts_data = lvts_ctrl->lvts_data;
120 void __iomem *base = lvts_sensor->base;
121 - u32 raw_low = lvts_temp_to_raw(low != -INT_MAX ? low : LVTS_MINIMUM_THRESHOLD);
122 - u32 raw_high = lvts_temp_to_raw(high);
123 + u32 raw_low = lvts_temp_to_raw(low != -INT_MAX ? low : LVTS_MINIMUM_THRESHOLD,
124 + lvts_data->temp_factor);
125 + u32 raw_high = lvts_temp_to_raw(high, lvts_data->temp_factor);
126 bool should_update_thresh;
128 lvts_sensor->low_thresh = low;
129 @@ -694,7 +703,7 @@ static int lvts_calibration_read(struct
133 -static int lvts_golden_temp_init(struct device *dev, u32 *value)
134 +static int lvts_golden_temp_init(struct device *dev, u32 *value, int temp_offset)
138 @@ -703,7 +712,7 @@ static int lvts_golden_temp_init(struct
139 if (gt && gt < LVTS_GOLDEN_TEMP_MAX)
142 - coeff_b = golden_temp * 500 + LVTS_COEFF_B;
143 + golden_temp_offset = golden_temp * 500 + temp_offset;
147 @@ -726,7 +735,7 @@ static int lvts_ctrl_init(struct device
148 * The golden temp information is contained in the first chunk
151 - ret = lvts_golden_temp_init(dev, (u32 *)lvts_td->calib);
152 + ret = lvts_golden_temp_init(dev, (u32 *)lvts_td->calib, lvts_data->temp_offset);
156 @@ -737,6 +746,7 @@ static int lvts_ctrl_init(struct device
157 for (i = 0; i < lvts_data->num_lvts_ctrl; i++) {
159 lvts_ctrl[i].base = lvts_td->base + lvts_data->lvts_ctrl[i].offset;
160 + lvts_ctrl[i].lvts_data = lvts_data;
162 ret = lvts_sensor_init(dev, &lvts_ctrl[i],
163 &lvts_data->lvts_ctrl[i]);
164 @@ -760,7 +770,8 @@ static int lvts_ctrl_init(struct device
165 * after initializing the calibration.
167 lvts_ctrl[i].hw_tshut_raw_temp =
168 - lvts_temp_to_raw(lvts_data->lvts_ctrl[i].hw_tshut_temp);
169 + lvts_temp_to_raw(lvts_data->lvts_ctrl[i].hw_tshut_temp,
170 + lvts_data->temp_factor);
172 lvts_ctrl[i].low_thresh = INT_MIN;
173 lvts_ctrl[i].high_thresh = INT_MIN;
174 @@ -1225,6 +1236,8 @@ static int lvts_probe(struct platform_de
178 + golden_temp_offset = lvts_data->temp_offset;
180 ret = lvts_domain_init(dev, lvts_td, lvts_data);
182 return dev_err_probe(dev, ret, "Failed to initialize the lvts domain\n");
183 @@ -1338,11 +1351,15 @@ static const struct lvts_ctrl_data mt819
184 static const struct lvts_data mt8195_lvts_mcu_data = {
185 .lvts_ctrl = mt8195_lvts_mcu_data_ctrl,
186 .num_lvts_ctrl = ARRAY_SIZE(mt8195_lvts_mcu_data_ctrl),
187 + .temp_factor = LVTS_COEFF_A_MT8195,
188 + .temp_offset = LVTS_COEFF_B_MT8195,
191 static const struct lvts_data mt8195_lvts_ap_data = {
192 .lvts_ctrl = mt8195_lvts_ap_data_ctrl,
193 .num_lvts_ctrl = ARRAY_SIZE(mt8195_lvts_ap_data_ctrl),
194 + .temp_factor = LVTS_COEFF_A_MT8195,
195 + .temp_offset = LVTS_COEFF_B_MT8195,
198 static const struct of_device_id lvts_of_match[] = {