Update 8167 thermal sensor calibration function

Retrieve the calibration data based on MTK yocto SDK 4.4 kernel
implementation in
https://excelsior.git.corp.google.com/excelsior-mendel/kernel/+/refs/heads/excelsior-4.4/drivers/misc/mediatek/thermal/mt8167/mtk_ts_cpu.c#1157

Change the variable prefix to 8167.

With the change, idle temperature: 43C, idle with monitor: 46C, camera
ML+monitor: 78C

Bug: 170512236
Change-Id: I878f45046872c75781da32a341b134e09d642bd2
diff --git a/arch/arm64/boot/dts/mediatek/mt8167-coral.dts b/arch/arm64/boot/dts/mediatek/mt8167-coral.dts
index 1b0a4ab..f1adb7c 100644
--- a/arch/arm64/boot/dts/mediatek/mt8167-coral.dts
+++ b/arch/arm64/boot/dts/mediatek/mt8167-coral.dts
@@ -117,7 +117,7 @@
 
 	thermal: thermal@1100d000 {
 		#thermal-sensor-cells = <0>;
-		compatible = "mediatek,mt8516-thermal";
+		compatible = "mediatek,mt8167-thermal";
 		reg = <0 0x1100d000 0 0x1000>;
 		interrupts = <GIC_SPI 70 IRQ_TYPE_LEVEL_LOW>;
 		clocks = <&topckgen CLK_TOP_THEM>, <&topckgen CLK_TOP_AUX_ADC>;
diff --git a/drivers/thermal/mtk_thermal.c b/drivers/thermal/mtk_thermal.c
index f2d2a98..6c9fa37 100644
--- a/drivers/thermal/mtk_thermal.c
+++ b/drivers/thermal/mtk_thermal.c
@@ -142,6 +142,10 @@
 #define CALIB_BUF0_O_SLOPE_SIGN(x)	(((x) >> 7) & 0x1)
 #define CALIB_BUF1_ID(x)		(((x) >> 9) & 0x1)
 
+#define CALIB_8167_BUF1_ID(x)   (((x) >> 2) & 0x1)
+#define CALIB_8167_BUF2_VTS_TS3(x)  (((x) >> 23) & 0x1ff)
+#define CALIB_8167_BUF1_VTS_TSABB(x)  (((x) >> 3) & 0x1ff)
+
 enum {
 	VTS1,
 	VTS2,
@@ -232,11 +236,11 @@
 /* The calibration coefficient of sensor  */
 #define MT8183_CALIBRATION	153
 
-#define MT8516_TEMP_AUXADC_CHANNEL (11)
-#define MT8516_NUM_CONTROLLER (1)
-#define MT8516_NUM_SENSORS (3)
-#define MT8516_NUM_SENSORS_PER_ZONE (3)
-#define MT8516_CALIBRATION (165)
+#define MT8167_TEMP_AUXADC_CHANNEL (11)
+#define MT8167_NUM_CONTROLLER (1)
+#define MT8167_NUM_SENSORS (3)
+#define MT8167_NUM_SENSORS_PER_ZONE (3)
+#define MT8167_CALIBRATION (165)
 
 struct mtk_thermal;
 
@@ -381,19 +385,19 @@
 static const int mt7622_vts_index[MT7622_NUM_SENSORS] = { VTS1 };
 static const int mt7622_tc_offset[MT7622_NUM_CONTROLLER] = { 0x0, };
 
-/* MT8516 thermal sensor data */
-static const int mt8516_bank_data[MT8516_NUM_SENSORS] = {
+/* MT8167 thermal sensor data */
+static const int mt8167_bank_data[MT8167_NUM_SENSORS] = {
 	0, 1, 2
 };
-static const int mt8516_msr[MT8516_NUM_SENSORS_PER_ZONE] = {
+static const int mt8167_msr[MT8167_NUM_SENSORS_PER_ZONE] = {
 	TEMP_MSR0, TEMP_MSR1, TEMP_MSR2
 };
-static const int mt8516_adcpnp[MT8516_NUM_SENSORS_PER_ZONE] = {
+static const int mt8167_adcpnp[MT8167_NUM_SENSORS_PER_ZONE] = {
 	TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2
 };
-static const int mt8516_mux_values[MT8516_NUM_SENSORS] = { 0, 1, 16 };
-static const int mt8516_tc_offset[MT8516_NUM_CONTROLLER] = { 0x0, };
-static const int mt8516_vts_index[MT8516_NUM_SENSORS] = {
+static const int mt8167_mux_values[MT8167_NUM_SENSORS] = { 0, 1, 16 };
+static const int mt8167_tc_offset[MT8167_NUM_CONTROLLER] = { 0x0, };
+static const int mt8167_vts_index[MT8167_NUM_SENSORS] = {
 	VTS1, VTS2, VTS3
 };
 /*
@@ -468,25 +472,25 @@
 	.sensor_mux_values = mt2701_mux_values,
 };
 
-/* MT8516 has one bank, and three sensors. */
-static const struct mtk_thermal_data mt8516_thermal_data = {
-	.auxadc_channel = MT8516_TEMP_AUXADC_CHANNEL,
+/* MT8167 has one bank, and three sensors. */
+static const struct mtk_thermal_data mt8167_thermal_data = {
+	.auxadc_channel = MT8167_TEMP_AUXADC_CHANNEL,
 	.num_banks = 1,
-	.num_sensors = MT8516_NUM_SENSORS,
-	.vts_index = mt8516_vts_index,
-	.cali_val = MT8516_CALIBRATION,
+	.num_sensors = MT8167_NUM_SENSORS,
+	.vts_index = mt8167_vts_index,
+	.cali_val = MT8167_CALIBRATION,
 	.num_controller = 1,
-	.controller_offset = mt8516_tc_offset,
+	.controller_offset = mt8167_tc_offset,
 	.need_switch_bank = true,
 	.bank_data = {
 		{
 			.num_sensors = 3,
-			.sensors = mt8516_bank_data,
+			.sensors = mt8167_bank_data,
 		},
 	},
-	.msr = mt8516_msr,
-	.adcpnp = mt8516_adcpnp,
-	.sensor_mux_values = mt8516_mux_values,
+	.msr = mt8167_msr,
+	.adcpnp = mt8167_adcpnp,
+	.sensor_mux_values = mt8167_mux_values,
 };
 
 /*
@@ -861,8 +865,8 @@
 	/* Start with default values */
 	mt->adc_ge = 512;
 	for (i = 0; i < mt->conf->num_sensors; i++)
-		mt->vts[i] = 260;
-	mt->degc_cali = 40;
+		mt->vts[i] = 190;
+	mt->degc_cali = 50;
 	mt->o_slope = 0;
 
 	cell = nvmem_cell_get(dev, "calibration-data");
@@ -885,16 +889,12 @@
 		goto out;
 	}
 
-	// MT8516 stores the 3 words of calibration data
-	// at indices 0x184, 0x180, 0x188.
-	// The nvmem APIs give us the values in order that
-	// they are laid out, so we need to swap the first two
-	// words.
-	if (mt->conf == &mt8516_thermal_data) {
-		swap(buf[0], buf[1]);
-	}
-
 	if (buf[0] & CALIB_BUF0_VALID) {
+		if (mt->conf == &mt8167_thermal_data) {
+			dev_info(dev, "Calibration data: buf[0]:0x%x, buf[1]:0x%x, buf[2]:0x%x\n",
+			    buf[0], buf[1], buf[2]);
+		}
+
 		mt->adc_ge = CALIB_BUF1_ADC_GE(buf[1]);
 
 		for (i = 0; i < mt->conf->num_sensors; i++) {
@@ -906,7 +906,11 @@
 				mt->vts[VTS2] = CALIB_BUF0_VTS_TS2(buf[0]);
 				break;
 			case VTS3:
-				mt->vts[VTS3] = CALIB_BUF1_VTS_TS3(buf[1]);
+				if (mt->conf == &mt8167_thermal_data) {
+					mt->vts[VTS3] = CALIB_8167_BUF2_VTS_TS3(buf[2]);
+				} else {
+					mt->vts[VTS3] = CALIB_BUF1_VTS_TS3(buf[1]);
+				}
 				break;
 			case VTS4:
 				mt->vts[VTS4] = CALIB_BUF2_VTS_TS4(buf[2]);
@@ -915,7 +919,11 @@
 				mt->vts[VTS5] = CALIB_BUF2_VTS_TS5(buf[2]);
 				break;
 			case VTSABB:
-				mt->vts[VTSABB] = CALIB_BUF2_VTS_TSABB(buf[2]);
+				if (mt->conf == &mt8167_thermal_data) {
+					mt->vts[VTSABB] = CALIB_8167_BUF1_VTS_TSABB(buf[1]);
+				} else {
+					mt->vts[VTSABB] = CALIB_BUF2_VTS_TSABB(buf[2]);
+				}
 				break;
 			default:
 				break;
@@ -923,11 +931,24 @@
 		}
 
 		mt->degc_cali = CALIB_BUF0_DEGC_CALI(buf[0]);
-		if (CALIB_BUF1_ID(buf[1]) &
-		    CALIB_BUF0_O_SLOPE_SIGN(buf[0]))
-			mt->o_slope = -CALIB_BUF0_O_SLOPE(buf[0]);
-		else
-			mt->o_slope = CALIB_BUF0_O_SLOPE(buf[0]);
+		if (mt->conf == &mt8167_thermal_data) {
+			if (CALIB_8167_BUF1_ID(buf[1]) &
+			    CALIB_BUF0_O_SLOPE_SIGN(buf[0])) {
+				mt->o_slope = -CALIB_BUF0_O_SLOPE(buf[0]);
+			} else {
+				mt->o_slope = CALIB_BUF0_O_SLOPE(buf[0]);
+			}
+			dev_info(dev, "Calibration data: adc_ge:%d, vts1:%d, vts2:%d, vts3:%d, "
+			    "degc_cali:%d, o_slope:%d\n", mt->adc_ge, mt->vts[VTS1], mt->vts[VTS2],
+			     mt->vts[VTS3], mt->degc_cali, mt->o_slope);
+
+		} else {
+			if (CALIB_BUF1_ID(buf[1]) &
+			    CALIB_BUF0_O_SLOPE_SIGN(buf[0]))
+				mt->o_slope = -CALIB_BUF0_O_SLOPE(buf[0]);
+			else
+				mt->o_slope = CALIB_BUF0_O_SLOPE(buf[0]);
+		}
 	} else {
 		dev_info(dev, "Device not calibrated, using default calibration values\n");
 	}
@@ -960,8 +981,8 @@
 		.data = (void *)&mt8183_thermal_data,
 	},
 	{
-		.compatible = "mediatek,mt8516-thermal",
-		.data = (void *)&mt8516_thermal_data,
+		.compatible = "mediatek,mt8167-thermal",
+		.data = (void *)&mt8167_thermal_data,
 	},
 	{
 	},
@@ -1033,7 +1054,7 @@
 		return -EINVAL;
 	}
 
-	if (mt->conf != &mt8516_thermal_data) {
+	if (mt->conf != &mt8167_thermal_data) {
 		ret = device_reset(&pdev->dev);
 		if (ret)
 			return ret;