Add thermal support for MT8516

- Add support for MT8516 to the mtk_thermal driver.
- Add device tree configuration for the thermal driver. Currently we
only configure enough to be able to read back the temperature, but don't
apply any throttling at trip points.

Change-Id: Ibb919b6a9896aca2047cf579753cccd078167a49
diff --git a/arch/arm64/boot/dts/mediatek/mt8167-coral.dts b/arch/arm64/boot/dts/mediatek/mt8167-coral.dts
index 43456a4..8f93985 100644
--- a/arch/arm64/boot/dts/mediatek/mt8167-coral.dts
+++ b/arch/arm64/boot/dts/mediatek/mt8167-coral.dts
@@ -85,6 +85,34 @@
 		pinctrl-0 = <&aud_pins_default>;
 		mediatek,jack-detect-gpio = <&pio 40 0>;
 	};
+
+	auxadc: adc@11003000 {
+		compatible = "mediatek,mt2712-auxadc";
+		reg = <0 0x11003000 0 0x1000>;
+		clocks = <&topckgen CLK_TOP_AUX_ADC>;
+		clock-names = "main";
+		#io-channel-cells = <1>;
+		status = "okay";
+	};
+
+	thermal: thermal@1100d000 {
+		#thermal-sensor-cells = <0>;
+		compatible = "mediatek,mt8516-thermal";
+		reg = <0 0x1100d000 0 0x1000>;
+		interrupts = <GIC_SPI 70 IRQ_TYPE_LEVEL_LOW>;
+		clocks = <&topckgen CLK_TOP_THEM>, <&topckgen CLK_TOP_AUX_ADC>;
+		clock-names = "therm", "auxadc";
+		mediatek,auxadc = <&auxadc>;
+		mediatek,apmixedsys = <&apmixedsys>;
+	};
+
+	thermal-zones {
+		cpu_thermal: cpu_thermal {
+			polling-delay-passive = <1000>; /* ms */
+			polling-delay = <1000>; /* ms */
+			thermal-sensors = <&thermal>;
+		};
+	};
 };
 
 &afe {
diff --git a/drivers/thermal/mtk_thermal.c b/drivers/thermal/mtk_thermal.c
index 248a236..f80d685 100644
--- a/drivers/thermal/mtk_thermal.c
+++ b/drivers/thermal/mtk_thermal.c
@@ -232,6 +232,12 @@
 /* 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)
+
 struct mtk_thermal;
 
 struct mtk_thermal_zone {
@@ -375,6 +381,21 @@
 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] = {
+	0, 1, 2
+};
+static const int mt8516_msr[MT8516_NUM_SENSORS_PER_ZONE] = {
+	TEMP_MSR0, TEMP_MSR1, TEMP_MSR2
+};
+static const int mt8516_adcpnp[MT8516_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] = {
+	VTS1, VTS2, VTS3
+};
 /*
  * The MT8173 thermal controller has four banks. Each bank can read up to
  * four temperature sensors simultaneously. The MT8173 has a total of 5
@@ -447,6 +468,27 @@
 	.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,
+	.num_banks = 1,
+	.num_sensors = MT8516_NUM_SENSORS,
+	.vts_index = mt8516_vts_index,
+	.cali_val = MT8516_CALIBRATION,
+	.num_controller = 1,
+	.controller_offset = mt8516_tc_offset,
+	.need_switch_bank = true,
+	.bank_data = {
+		{
+			.num_sensors = 3,
+			.sensors = mt8516_bank_data,
+		},
+	},
+	.msr = mt8516_msr,
+	.adcpnp = mt8516_adcpnp,
+	.sensor_mux_values = mt8516_mux_values,
+};
+
 /*
  * The MT2712 thermal controller has one bank, which can read up to
  * four temperature sensors simultaneously. The MT2712 has a total of 4
@@ -907,7 +949,12 @@
 	{
 		.compatible = "mediatek,mt8183-thermal",
 		.data = (void *)&mt8183_thermal_data,
-	}, {
+	},
+	{
+		.compatible = "mediatek,mt8516-thermal",
+		.data = (void *)&mt8516_thermal_data,
+	},
+	{
 	},
 };
 MODULE_DEVICE_TABLE(of, mtk_thermal_of_match);
@@ -977,9 +1024,11 @@
 		return -EINVAL;
 	}
 
-	ret = device_reset(&pdev->dev);
-	if (ret)
-		return ret;
+	if (mt->conf != &mt8516_thermal_data) {
+		ret = device_reset(&pdev->dev);
+		if (ret)
+			return ret;
+	}
 
 	ret = clk_prepare_enable(mt->clk_auxadc);
 	if (ret) {
@@ -1012,8 +1061,11 @@
 				tz, (i == 0) ?
 				&mtk_thermal_ops : &mtk_thermal_sensor_ops);
 
-		if (IS_ERR(tzdev)) {
-			if (IS_ERR(tzdev) != -EACCES) {
+		// Thermal framework seems to only allow registering one sensor per zone.
+		// So if we're on any index but 1, don't error out!
+		// See drivers/thermal/of-thermal.c:835 for details.
+		if (IS_ERR(tzdev) && i == 0) {
+			if (PTR_ERR(tzdev) != -EACCES) {
 				ret = PTR_ERR(tzdev);
 				goto err_disable_clk_peri_therm;
 			}