Skip to content

Commit 6931d59

Browse files
laura-naodlezcano
authored andcommitted
thermal/drivers/mediatek/lvts: Make number of calibration offsets configurable
MT8196/MT6991 use 2-byte eFuse calibration data, whereas other SoCs supported by the driver rely on 3 bytes. Make the number of calibration bytes per sensor configurable, enabling support for SoCs with varying calibration formats. Signed-off-by: Laura Nao <laura.nao@collabora.com> Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com> Tested-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com> Link: https://patch.msgid.link/20251125-mt8196-lvts-v4-v5-2-6db7eb903fb7@collabora.com Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
1 parent 03656dc commit 6931d59

1 file changed

Lines changed: 44 additions & 11 deletions

File tree

drivers/thermal/mediatek/lvts_thermal.c

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,15 @@
9696

9797
#define LVTS_MINIMUM_THRESHOLD 20000
9898

99+
#define LVTS_MAX_CAL_OFFSETS 3
100+
#define LVTS_NUM_CAL_OFFSETS_MT7988 3
101+
99102
static int golden_temp = LVTS_GOLDEN_TEMP_DEFAULT;
100103
static int golden_temp_offset;
101104

102105
struct lvts_sensor_data {
103106
int dt_id;
104-
u8 cal_offsets[3];
107+
u8 cal_offsets[LVTS_MAX_CAL_OFFSETS];
105108
};
106109

107110
struct lvts_ctrl_data {
@@ -127,6 +130,7 @@ struct lvts_data {
127130
const struct lvts_ctrl_data *lvts_ctrl;
128131
const u32 *conn_cmd;
129132
const u32 *init_cmd;
133+
int num_cal_offsets;
130134
int num_lvts_ctrl;
131135
int num_conn_cmd;
132136
int num_init_cmd;
@@ -646,6 +650,26 @@ static int lvts_sensor_init(struct device *dev, struct lvts_ctrl *lvts_ctrl,
646650
return 0;
647651
}
648652

653+
static int lvts_decode_sensor_calibration(const struct lvts_sensor_data *sensor,
654+
const u8 *efuse_calibration, u32 calib_len,
655+
u8 num_offsets, u32 *calib)
656+
{
657+
int i;
658+
u32 calib_val = 0;
659+
660+
for (i = 0; i < num_offsets; i++) {
661+
u8 offset = sensor->cal_offsets[i];
662+
663+
if (offset >= calib_len)
664+
return -EINVAL;
665+
// Pack each calibration byte into the correct position
666+
calib_val |= efuse_calibration[offset] << (8 * i);
667+
}
668+
669+
*calib = calib_val;
670+
return 0;
671+
}
672+
649673
/*
650674
* The efuse blob values follows the sensor enumeration per thermal
651675
* controller. The decoding of the stream is as follow:
@@ -711,26 +735,27 @@ static int lvts_calibration_init(struct device *dev, struct lvts_ctrl *lvts_ctrl
711735
u8 *efuse_calibration,
712736
size_t calib_len)
713737
{
714-
int i;
738+
const struct lvts_data *lvts_data = lvts_ctrl->lvts_data;
739+
int i, ret;
715740
u32 gt;
716741

717742
/* A zero value for gt means that device has invalid efuse data */
718-
gt = (((u32 *)efuse_calibration)[0] >> lvts_ctrl->lvts_data->gt_calib_bit_offset) & 0xff;
743+
gt = (((u32 *)efuse_calibration)[0] >> lvts_data->gt_calib_bit_offset) & 0xff;
719744

720745
lvts_for_each_valid_sensor(i, lvts_ctrl_data) {
721746
const struct lvts_sensor_data *sensor =
722747
&lvts_ctrl_data->lvts_sensor[i];
748+
u32 calib = 0;
723749

724-
if (sensor->cal_offsets[0] >= calib_len ||
725-
sensor->cal_offsets[1] >= calib_len ||
726-
sensor->cal_offsets[2] >= calib_len)
727-
return -EINVAL;
750+
ret = lvts_decode_sensor_calibration(sensor, efuse_calibration,
751+
calib_len,
752+
lvts_data->num_cal_offsets,
753+
&calib);
754+
if (ret)
755+
return ret;
728756

729757
if (gt) {
730-
lvts_ctrl->calibration[i] =
731-
(efuse_calibration[sensor->cal_offsets[0]] << 0) +
732-
(efuse_calibration[sensor->cal_offsets[1]] << 8) +
733-
(efuse_calibration[sensor->cal_offsets[2]] << 16);
758+
lvts_ctrl->calibration[i] = calib;
734759
} else if (lvts_ctrl->lvts_data->def_calibration) {
735760
lvts_ctrl->calibration[i] = lvts_ctrl->lvts_data->def_calibration;
736761
} else {
@@ -1763,6 +1788,7 @@ static const struct lvts_data mt7988_lvts_ap_data = {
17631788
.temp_factor = LVTS_COEFF_A_MT7988,
17641789
.temp_offset = LVTS_COEFF_B_MT7988,
17651790
.gt_calib_bit_offset = 24,
1791+
.num_cal_offsets = LVTS_NUM_CAL_OFFSETS_MT7988,
17661792
};
17671793

17681794
static const struct lvts_data mt8186_lvts_data = {
@@ -1776,6 +1802,7 @@ static const struct lvts_data mt8186_lvts_data = {
17761802
.temp_offset = LVTS_COEFF_B_MT7988,
17771803
.gt_calib_bit_offset = 24,
17781804
.def_calibration = 19000,
1805+
.num_cal_offsets = LVTS_NUM_CAL_OFFSETS_MT7988,
17791806
};
17801807

17811808
static const struct lvts_data mt8188_lvts_mcu_data = {
@@ -1789,6 +1816,7 @@ static const struct lvts_data mt8188_lvts_mcu_data = {
17891816
.temp_offset = LVTS_COEFF_B_MT8195,
17901817
.gt_calib_bit_offset = 20,
17911818
.def_calibration = 35000,
1819+
.num_cal_offsets = LVTS_NUM_CAL_OFFSETS_MT7988,
17921820
};
17931821

17941822
static const struct lvts_data mt8188_lvts_ap_data = {
@@ -1802,6 +1830,7 @@ static const struct lvts_data mt8188_lvts_ap_data = {
18021830
.temp_offset = LVTS_COEFF_B_MT8195,
18031831
.gt_calib_bit_offset = 20,
18041832
.def_calibration = 35000,
1833+
.num_cal_offsets = LVTS_NUM_CAL_OFFSETS_MT7988,
18051834
};
18061835

18071836
static const struct lvts_data mt8192_lvts_mcu_data = {
@@ -1815,6 +1844,7 @@ static const struct lvts_data mt8192_lvts_mcu_data = {
18151844
.temp_offset = LVTS_COEFF_B_MT8195,
18161845
.gt_calib_bit_offset = 24,
18171846
.def_calibration = 35000,
1847+
.num_cal_offsets = LVTS_NUM_CAL_OFFSETS_MT7988,
18181848
};
18191849

18201850
static const struct lvts_data mt8192_lvts_ap_data = {
@@ -1828,6 +1858,7 @@ static const struct lvts_data mt8192_lvts_ap_data = {
18281858
.temp_offset = LVTS_COEFF_B_MT8195,
18291859
.gt_calib_bit_offset = 24,
18301860
.def_calibration = 35000,
1861+
.num_cal_offsets = LVTS_NUM_CAL_OFFSETS_MT7988,
18311862
};
18321863

18331864
static const struct lvts_data mt8195_lvts_mcu_data = {
@@ -1841,6 +1872,7 @@ static const struct lvts_data mt8195_lvts_mcu_data = {
18411872
.temp_offset = LVTS_COEFF_B_MT8195,
18421873
.gt_calib_bit_offset = 24,
18431874
.def_calibration = 35000,
1875+
.num_cal_offsets = LVTS_NUM_CAL_OFFSETS_MT7988,
18441876
};
18451877

18461878
static const struct lvts_data mt8195_lvts_ap_data = {
@@ -1854,6 +1886,7 @@ static const struct lvts_data mt8195_lvts_ap_data = {
18541886
.temp_offset = LVTS_COEFF_B_MT8195,
18551887
.gt_calib_bit_offset = 24,
18561888
.def_calibration = 35000,
1889+
.num_cal_offsets = LVTS_NUM_CAL_OFFSETS_MT7988,
18571890
};
18581891

18591892
static const struct of_device_id lvts_of_match[] = {

0 commit comments

Comments
 (0)