16a32f564bf3523aa91aee15e61451898f4a917a
[openwrt/staging/wigyori.git] /
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
5 configurable
6
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.
10
11 Add a field in the lvts_ctrl pointing to the lvts_data which now
12 contains the soc-specific temperature coefficents.
13
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.
16
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
22 ---
23 drivers/thermal/mediatek/lvts_thermal.c | 51 ++++++++++++++++---------
24 1 file changed, 34 insertions(+), 17 deletions(-)
25
26 --- a/drivers/thermal/mediatek/lvts_thermal.c
27 +++ b/drivers/thermal/mediatek/lvts_thermal.c
28 @@ -80,8 +80,8 @@
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
36
37 #define LVTS_MSR_IMMEDIATE_MODE 0
38 #define LVTS_MSR_FILTERED_MODE 1
39 @@ -94,7 +94,7 @@
40 #define LVTS_MINIMUM_THRESHOLD 20000
41
42 static int golden_temp = LVTS_GOLDEN_TEMP_DEFAULT;
43 -static int coeff_b = LVTS_COEFF_B;
44 +static int golden_temp_offset;
45
46 struct lvts_sensor_data {
47 int dt_id;
48 @@ -112,6 +112,8 @@ struct lvts_ctrl_data {
49 struct lvts_data {
50 const struct lvts_ctrl_data *lvts_ctrl;
51 int num_lvts_ctrl;
52 + int temp_factor;
53 + int temp_offset;
54 };
55
56 struct lvts_sensor {
57 @@ -126,6 +128,7 @@ struct lvts_sensor {
58
59 struct lvts_ctrl {
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;
64 int num_lvts_sensor;
65 @@ -247,21 +250,21 @@ static void lvts_debugfs_exit(struct lvt
66
67 #endif
68
69 -static int lvts_raw_to_temp(u32 raw_temp)
70 +static int lvts_raw_to_temp(u32 raw_temp, int temp_factor)
71 {
72 int temperature;
73
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;
78
79 return temperature;
80 }
81
82 -static u32 lvts_temp_to_raw(int temperature)
83 +static u32 lvts_temp_to_raw(int temperature, int temp_factor)
84 {
85 - u32 raw_temp = ((s64)(coeff_b - temperature)) << 14;
86 + u32 raw_temp = ((s64)(golden_temp_offset - temperature)) << 14;
87
88 - raw_temp = div_s64(raw_temp, -LVTS_COEFF_A);
89 + raw_temp = div_s64(raw_temp, -temp_factor);
90
91 return raw_temp;
92 }
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)
95 {
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;
101 u32 value;
102 int rc;
103 @@ -301,7 +307,7 @@ static int lvts_get_temp(struct thermal_
104 if (rc)
105 return -EAGAIN;
106
107 - *temp = lvts_raw_to_temp(value & 0xFFFF);
108 + *temp = lvts_raw_to_temp(value & 0xFFFF, lvts_data->temp_factor);
109
110 return 0;
111 }
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)
114 {
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;
127
128 lvts_sensor->low_thresh = low;
129 @@ -692,7 +701,7 @@ static int lvts_calibration_read(struct
130 return 0;
131 }
132
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)
135 {
136 u32 gt;
137
138 @@ -701,7 +710,7 @@ static int lvts_golden_temp_init(struct
139 if (gt && gt < LVTS_GOLDEN_TEMP_MAX)
140 golden_temp = gt;
141
142 - coeff_b = golden_temp * 500 + LVTS_COEFF_B;
143 + golden_temp_offset = golden_temp * 500 + temp_offset;
144
145 return 0;
146 }
147 @@ -724,7 +733,7 @@ static int lvts_ctrl_init(struct device
148 * The golden temp information is contained in the first chunk
149 * of efuse data.
150 */
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);
153 if (ret)
154 return ret;
155
156 @@ -735,6 +744,7 @@ static int lvts_ctrl_init(struct device
157 for (i = 0; i < lvts_data->num_lvts_ctrl; i++) {
158
159 lvts_ctrl[i].base = lvts_td->base + lvts_data->lvts_ctrl[i].offset;
160 + lvts_ctrl[i].lvts_data = lvts_data;
161
162 ret = lvts_sensor_init(dev, &lvts_ctrl[i],
163 &lvts_data->lvts_ctrl[i]);
164 @@ -758,7 +768,8 @@ static int lvts_ctrl_init(struct device
165 * after initializing the calibration.
166 */
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);
171
172 lvts_ctrl[i].low_thresh = INT_MIN;
173 lvts_ctrl[i].high_thresh = INT_MIN;
174 @@ -1223,6 +1234,8 @@ static int lvts_probe(struct platform_de
175 if (irq < 0)
176 return irq;
177
178 + golden_temp_offset = lvts_data->temp_offset;
179 +
180 ret = lvts_domain_init(dev, lvts_td, lvts_data);
181 if (ret)
182 return dev_err_probe(dev, ret, "Failed to initialize the lvts domain\n");
183 @@ -1336,11 +1349,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,
189 };
190
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,
196 };
197
198 static const struct of_device_id lvts_of_match[] = {