Skip to content

Commit 6725a29

Browse files
frank-wdlezcano
authored andcommitted
thermal/drivers/mediatek/lvts_thermal: Make coeff configurable
The upcoming mt7988 has different temperature coefficients so we cannot use constants in the functions lvts_golden_temp_init, lvts_golden_temp_init and lvts_raw_to_temp anymore. Add a field in the lvts_ctrl pointing to the lvts_data which now contains the soc-specific temperature coefficents. To make the code better readable, rename static int coeff_b to golden_temp_offset, COEFF_A to temp_factor and COEFF_B to temp_offset. Signed-off-by: Frank Wunderlich <[email protected]> Reviewed-by: AngeloGioacchino Del Regno <[email protected]> Tested-by: Daniel Golle <[email protected]> Signed-off-by: Daniel Lezcano <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent be2cc09 commit 6725a29

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

drivers/thermal/mediatek/lvts_thermal.c

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@
8080
#define LVTS_SENSOR_MAX 4
8181
#define LVTS_GOLDEN_TEMP_MAX 62
8282
#define LVTS_GOLDEN_TEMP_DEFAULT 50
83-
#define LVTS_COEFF_A -250460
84-
#define LVTS_COEFF_B 250460
83+
#define LVTS_COEFF_A_MT8195 -250460
84+
#define LVTS_COEFF_B_MT8195 250460
8585

8686
#define LVTS_MSR_IMMEDIATE_MODE 0
8787
#define LVTS_MSR_FILTERED_MODE 1
@@ -94,7 +94,7 @@
9494
#define LVTS_MINIMUM_THRESHOLD 20000
9595

9696
static int golden_temp = LVTS_GOLDEN_TEMP_DEFAULT;
97-
static int coeff_b = LVTS_COEFF_B;
97+
static int golden_temp_offset;
9898

9999
struct lvts_sensor_data {
100100
int dt_id;
@@ -112,6 +112,8 @@ struct lvts_ctrl_data {
112112
struct lvts_data {
113113
const struct lvts_ctrl_data *lvts_ctrl;
114114
int num_lvts_ctrl;
115+
int temp_factor;
116+
int temp_offset;
115117
};
116118

117119
struct lvts_sensor {
@@ -126,6 +128,7 @@ struct lvts_sensor {
126128

127129
struct lvts_ctrl {
128130
struct lvts_sensor sensors[LVTS_SENSOR_MAX];
131+
const struct lvts_data *lvts_data;
129132
u32 calibration[LVTS_SENSOR_MAX];
130133
u32 hw_tshut_raw_temp;
131134
int num_lvts_sensor;
@@ -247,28 +250,31 @@ static void lvts_debugfs_exit(struct lvts_domain *lvts_td) { }
247250

248251
#endif
249252

250-
static int lvts_raw_to_temp(u32 raw_temp)
253+
static int lvts_raw_to_temp(u32 raw_temp, int temp_factor)
251254
{
252255
int temperature;
253256

254-
temperature = ((s64)(raw_temp & 0xFFFF) * LVTS_COEFF_A) >> 14;
255-
temperature += coeff_b;
257+
temperature = ((s64)(raw_temp & 0xFFFF) * temp_factor) >> 14;
258+
temperature += golden_temp_offset;
256259

257260
return temperature;
258261
}
259262

260-
static u32 lvts_temp_to_raw(int temperature)
263+
static u32 lvts_temp_to_raw(int temperature, int temp_factor)
261264
{
262-
u32 raw_temp = ((s64)(coeff_b - temperature)) << 14;
265+
u32 raw_temp = ((s64)(golden_temp_offset - temperature)) << 14;
263266

264-
raw_temp = div_s64(raw_temp, -LVTS_COEFF_A);
267+
raw_temp = div_s64(raw_temp, -temp_factor);
265268

266269
return raw_temp;
267270
}
268271

269272
static int lvts_get_temp(struct thermal_zone_device *tz, int *temp)
270273
{
271274
struct lvts_sensor *lvts_sensor = thermal_zone_device_priv(tz);
275+
struct lvts_ctrl *lvts_ctrl = container_of(lvts_sensor, struct lvts_ctrl,
276+
sensors[lvts_sensor->id]);
277+
const struct lvts_data *lvts_data = lvts_ctrl->lvts_data;
272278
void __iomem *msr = lvts_sensor->msr;
273279
u32 value;
274280
int rc;
@@ -301,7 +307,7 @@ static int lvts_get_temp(struct thermal_zone_device *tz, int *temp)
301307
if (rc)
302308
return -EAGAIN;
303309

304-
*temp = lvts_raw_to_temp(value & 0xFFFF);
310+
*temp = lvts_raw_to_temp(value & 0xFFFF, lvts_data->temp_factor);
305311

306312
return 0;
307313
}
@@ -348,10 +354,13 @@ static bool lvts_should_update_thresh(struct lvts_ctrl *lvts_ctrl, int high)
348354
static int lvts_set_trips(struct thermal_zone_device *tz, int low, int high)
349355
{
350356
struct lvts_sensor *lvts_sensor = thermal_zone_device_priv(tz);
351-
struct lvts_ctrl *lvts_ctrl = container_of(lvts_sensor, struct lvts_ctrl, sensors[lvts_sensor->id]);
357+
struct lvts_ctrl *lvts_ctrl = container_of(lvts_sensor, struct lvts_ctrl,
358+
sensors[lvts_sensor->id]);
359+
const struct lvts_data *lvts_data = lvts_ctrl->lvts_data;
352360
void __iomem *base = lvts_sensor->base;
353-
u32 raw_low = lvts_temp_to_raw(low != -INT_MAX ? low : LVTS_MINIMUM_THRESHOLD);
354-
u32 raw_high = lvts_temp_to_raw(high);
361+
u32 raw_low = lvts_temp_to_raw(low != -INT_MAX ? low : LVTS_MINIMUM_THRESHOLD,
362+
lvts_data->temp_factor);
363+
u32 raw_high = lvts_temp_to_raw(high, lvts_data->temp_factor);
355364
bool should_update_thresh;
356365

357366
lvts_sensor->low_thresh = low;
@@ -692,7 +701,7 @@ static int lvts_calibration_read(struct device *dev, struct lvts_domain *lvts_td
692701
return 0;
693702
}
694703

695-
static int lvts_golden_temp_init(struct device *dev, u32 *value)
704+
static int lvts_golden_temp_init(struct device *dev, u32 *value, int temp_offset)
696705
{
697706
u32 gt;
698707

@@ -701,7 +710,7 @@ static int lvts_golden_temp_init(struct device *dev, u32 *value)
701710
if (gt && gt < LVTS_GOLDEN_TEMP_MAX)
702711
golden_temp = gt;
703712

704-
coeff_b = golden_temp * 500 + LVTS_COEFF_B;
713+
golden_temp_offset = golden_temp * 500 + temp_offset;
705714

706715
return 0;
707716
}
@@ -724,7 +733,7 @@ static int lvts_ctrl_init(struct device *dev, struct lvts_domain *lvts_td,
724733
* The golden temp information is contained in the first chunk
725734
* of efuse data.
726735
*/
727-
ret = lvts_golden_temp_init(dev, (u32 *)lvts_td->calib);
736+
ret = lvts_golden_temp_init(dev, (u32 *)lvts_td->calib, lvts_data->temp_offset);
728737
if (ret)
729738
return ret;
730739

@@ -735,6 +744,7 @@ static int lvts_ctrl_init(struct device *dev, struct lvts_domain *lvts_td,
735744
for (i = 0; i < lvts_data->num_lvts_ctrl; i++) {
736745

737746
lvts_ctrl[i].base = lvts_td->base + lvts_data->lvts_ctrl[i].offset;
747+
lvts_ctrl[i].lvts_data = lvts_data;
738748

739749
ret = lvts_sensor_init(dev, &lvts_ctrl[i],
740750
&lvts_data->lvts_ctrl[i]);
@@ -758,7 +768,8 @@ static int lvts_ctrl_init(struct device *dev, struct lvts_domain *lvts_td,
758768
* after initializing the calibration.
759769
*/
760770
lvts_ctrl[i].hw_tshut_raw_temp =
761-
lvts_temp_to_raw(lvts_data->lvts_ctrl[i].hw_tshut_temp);
771+
lvts_temp_to_raw(lvts_data->lvts_ctrl[i].hw_tshut_temp,
772+
lvts_data->temp_factor);
762773

763774
lvts_ctrl[i].low_thresh = INT_MIN;
764775
lvts_ctrl[i].high_thresh = INT_MIN;
@@ -1223,6 +1234,8 @@ static int lvts_probe(struct platform_device *pdev)
12231234
if (irq < 0)
12241235
return irq;
12251236

1237+
golden_temp_offset = lvts_data->temp_offset;
1238+
12261239
ret = lvts_domain_init(dev, lvts_td, lvts_data);
12271240
if (ret)
12281241
return dev_err_probe(dev, ret, "Failed to initialize the lvts domain\n");
@@ -1336,11 +1349,15 @@ static const struct lvts_ctrl_data mt8195_lvts_ap_data_ctrl[] = {
13361349
static const struct lvts_data mt8195_lvts_mcu_data = {
13371350
.lvts_ctrl = mt8195_lvts_mcu_data_ctrl,
13381351
.num_lvts_ctrl = ARRAY_SIZE(mt8195_lvts_mcu_data_ctrl),
1352+
.temp_factor = LVTS_COEFF_A_MT8195,
1353+
.temp_offset = LVTS_COEFF_B_MT8195,
13391354
};
13401355

13411356
static const struct lvts_data mt8195_lvts_ap_data = {
13421357
.lvts_ctrl = mt8195_lvts_ap_data_ctrl,
13431358
.num_lvts_ctrl = ARRAY_SIZE(mt8195_lvts_ap_data_ctrl),
1359+
.temp_factor = LVTS_COEFF_A_MT8195,
1360+
.temp_offset = LVTS_COEFF_B_MT8195,
13441361
};
13451362

13461363
static const struct of_device_id lvts_of_match[] = {

0 commit comments

Comments
 (0)