1 From cd47d86ab09f1f3ec5c86441d4fe95e0cf597c06 Mon Sep 17 00:00:00 2001
2 From: Daniel Golle <daniel@makrotopia.org>
3 Date: Tue, 13 Sep 2022 00:56:24 +0100
4 Subject: [PATCH] thermal/drivers/mediatek: add support for MT7986 and MT7981
6 Add support for V3 generation thermal found in MT7986 and MT7981 SoCs.
8 Signed-off-by: Daniel Golle <daniel@makrotopia.org>
10 drivers/thermal/mtk_thermal.c | 202 +++++++++++++++++++++++++++++++++-
11 1 file changed, 198 insertions(+), 4 deletions(-)
13 --- a/drivers/thermal/mtk_thermal.c
14 +++ b/drivers/thermal/mtk_thermal.c
16 #define CALIB_BUF1_VALID_V2(x) (((x) >> 4) & 0x1)
17 #define CALIB_BUF1_O_SLOPE_SIGN_V2(x) (((x) >> 3) & 0x1)
20 + * Layout of the fuses providing the calibration data
21 + * These macros could be used for MT7981 and MT7986.
23 +#define CALIB_BUF0_ADC_GE_V3(x) (((x) >> 0) & 0x3ff)
24 +#define CALIB_BUF0_ADC_OE_V3(x) (((x) >> 10) & 0x3ff)
25 +#define CALIB_BUF0_DEGC_CALI_V3(x) (((x) >> 20) & 0x3f)
26 +#define CALIB_BUF0_O_SLOPE_V3(x) (((x) >> 26) & 0x3f)
27 +#define CALIB_BUF1_VTS_TS1_V3(x) (((x) >> 0) & 0x1ff)
28 +#define CALIB_BUF1_VTS_TS2_V3(x) (((x) >> 21) & 0x1ff)
29 +#define CALIB_BUF1_VTS_TSABB_V3(x) (((x) >> 9) & 0x1ff)
30 +#define CALIB_BUF1_VALID_V3(x) (((x) >> 18) & 0x1)
31 +#define CALIB_BUF1_O_SLOPE_SIGN_V3(x) (((x) >> 19) & 0x1)
32 +#define CALIB_BUF1_ID_V3(x) (((x) >> 20) & 0x1)
37 @@ -163,6 +178,7 @@ enum {
38 enum mtk_thermal_version {
44 /* MT2701 thermal sensors */
45 @@ -245,6 +261,27 @@ enum mtk_thermal_version {
46 /* The calibration coefficient of sensor */
47 #define MT8183_CALIBRATION 153
49 +/* AUXADC channel 11 is used for the temperature sensors */
50 +#define MT7986_TEMP_AUXADC_CHANNEL 11
52 +/* The total number of temperature sensors in the MT7986 */
53 +#define MT7986_NUM_SENSORS 1
55 +/* The number of banks in the MT7986 */
56 +#define MT7986_NUM_ZONES 1
58 +/* The number of sensing points per bank */
59 +#define MT7986_NUM_SENSORS_PER_ZONE 1
61 +/* MT7986 thermal sensors */
64 +/* The number of controller in the MT7986 */
65 +#define MT7986_NUM_CONTROLLER 1
67 +/* The calibration coefficient of sensor */
68 +#define MT7986_CALIBRATION 165
72 struct thermal_bank_cfg {
73 @@ -386,6 +423,14 @@ static const int mt7622_mux_values[MT762
74 static const int mt7622_vts_index[MT7622_NUM_SENSORS] = { VTS1 };
75 static const int mt7622_tc_offset[MT7622_NUM_CONTROLLER] = { 0x0, };
77 +/* MT7986 thermal sensor data */
78 +static const int mt7986_bank_data[MT7986_NUM_SENSORS] = { MT7986_TS1, };
79 +static const int mt7986_msr[MT7986_NUM_SENSORS_PER_ZONE] = { TEMP_MSR0, };
80 +static const int mt7986_adcpnp[MT7986_NUM_SENSORS_PER_ZONE] = { TEMP_ADCPNP0, };
81 +static const int mt7986_mux_values[MT7986_NUM_SENSORS] = { 0, };
82 +static const int mt7986_vts_index[MT7986_NUM_SENSORS] = { VTS1 };
83 +static const int mt7986_tc_offset[MT7986_NUM_CONTROLLER] = { 0x0, };
86 * The MT8173 thermal controller has four banks. Each bank can read up to
87 * four temperature sensors simultaneously. The MT8173 has a total of 5
88 @@ -549,6 +594,30 @@ static const struct mtk_thermal_data mt8
89 .version = MTK_THERMAL_V1,
93 + * MT7986 uses AUXADC Channel 11 for raw data access.
95 +static const struct mtk_thermal_data mt7986_thermal_data = {
96 + .auxadc_channel = MT7986_TEMP_AUXADC_CHANNEL,
97 + .num_banks = MT7986_NUM_ZONES,
98 + .num_sensors = MT7986_NUM_SENSORS,
99 + .vts_index = mt7986_vts_index,
100 + .cali_val = MT7986_CALIBRATION,
101 + .num_controller = MT7986_NUM_CONTROLLER,
102 + .controller_offset = mt7986_tc_offset,
103 + .need_switch_bank = true,
107 + .sensors = mt7986_bank_data,
111 + .adcpnp = mt7986_adcpnp,
112 + .sensor_mux_values = mt7986_mux_values,
113 + .version = MTK_THERMAL_V3,
117 * raw_to_mcelsius - convert a raw ADC value to mcelsius
118 * @mt: The thermal controller
119 @@ -603,6 +672,22 @@ static int raw_to_mcelsius_v2(struct mtk
120 return (format_2 - tmp) * 100;
123 +static int raw_to_mcelsius_v3(struct mtk_thermal *mt, int sensno, s32 raw)
131 + tmp = 100000 * 15 / 16 * 10000;
132 + tmp /= 4096 - 512 + mt->adc_ge;
134 + tmp *= raw - mt->vts[sensno] - 2900 - mt->adc_oe + 512;
136 + return mt->degc_cali * 500 - tmp;
140 * mtk_thermal_get_bank - get bank
142 @@ -659,9 +744,12 @@ static int mtk_thermal_bank_temperature(
143 if (mt->conf->version == MTK_THERMAL_V1) {
144 temp = raw_to_mcelsius_v1(
145 mt, conf->bank_data[bank->id].sensors[i], raw);
147 + } else if (mt->conf->version == MTK_THERMAL_V2) {
148 temp = raw_to_mcelsius_v2(
149 mt, conf->bank_data[bank->id].sensors[i], raw);
151 + temp = raw_to_mcelsius_v3(
152 + mt, conf->bank_data[bank->id].sensors[i], raw);
156 @@ -887,6 +975,26 @@ static int mtk_thermal_extract_efuse_v2(
160 +static int mtk_thermal_extract_efuse_v3(struct mtk_thermal *mt, u32 *buf)
162 + if (!CALIB_BUF1_VALID_V3(buf[1]))
165 + mt->adc_oe = CALIB_BUF0_ADC_OE_V3(buf[0]);
166 + mt->adc_ge = CALIB_BUF0_ADC_GE_V3(buf[0]);
167 + mt->degc_cali = CALIB_BUF0_DEGC_CALI_V3(buf[0]);
168 + mt->o_slope = CALIB_BUF0_O_SLOPE_V3(buf[0]);
169 + mt->vts[VTS1] = CALIB_BUF1_VTS_TS1_V3(buf[1]);
170 + mt->vts[VTS2] = CALIB_BUF1_VTS_TS2_V3(buf[1]);
171 + mt->vts[VTSABB] = CALIB_BUF1_VTS_TSABB_V3(buf[1]);
172 + mt->o_slope_sign = CALIB_BUF1_O_SLOPE_SIGN_V3(buf[1]);
174 + if (CALIB_BUF1_ID_V3(buf[1]) == 0)
180 static int mtk_thermal_get_calibration_data(struct device *dev,
181 struct mtk_thermal *mt)
183 @@ -897,6 +1005,7 @@ static int mtk_thermal_get_calibration_d
185 /* Start with default values */
188 for (i = 0; i < mt->conf->num_sensors; i++)
191 @@ -924,8 +1033,10 @@ static int mtk_thermal_get_calibration_d
193 if (mt->conf->version == MTK_THERMAL_V1)
194 ret = mtk_thermal_extract_efuse_v1(mt, buf);
196 + else if (mt->conf->version == MTK_THERMAL_V2)
197 ret = mtk_thermal_extract_efuse_v2(mt, buf);
199 + ret = mtk_thermal_extract_efuse_v3(mt, buf);
202 dev_info(dev, "Device not calibrated, using default calibration values\n");
203 @@ -956,6 +1067,10 @@ static const struct of_device_id mtk_the
204 .data = (void *)&mt7622_thermal_data,
207 + .compatible = "mediatek,mt7986-thermal",
208 + .data = (void *)&mt7986_thermal_data,
211 .compatible = "mediatek,mt8183-thermal",
212 .data = (void *)&mt8183_thermal_data,
214 @@ -1070,7 +1185,8 @@ static int mtk_thermal_probe(struct plat
215 goto err_disable_clk_auxadc;
218 - if (mt->conf->version == MTK_THERMAL_V2) {
219 + if (mt->conf->version == MTK_THERMAL_V2 ||
220 + mt->conf->version == MTK_THERMAL_V3) {
221 mtk_thermal_turn_on_buffer(apmixed_base);
222 mtk_thermal_release_periodic_ts(mt, auxadc_base);