65311d5718d9f9cdebab527a3e23f3027fa8958a
[openwrt/staging/blogic.git] /
1 From aa957c759b1182aee00cc35178667f849f941b42 Mon Sep 17 00:00:00 2001
2 From: Daniel Golle <daniel@makrotopia.org>
3 Date: Wed, 30 Nov 2022 13:19:39 +0000
4 Subject: [PATCH 2/2] thermal: mediatek: add support for MT7986 and MT7981
5
6 Add support for V3 generation thermal found in MT7986 and MT7981 SoCs.
7 Brings code to assign values from efuse as well as new function to
8 convert raw temperature to millidegree celsius, as found in MediaTek's
9 SDK sources (but cleaned up and de-duplicated)
10
11 [1]: https://git01.mediatek.com/plugins/gitiles/openwrt/feeds/mtk-openwrt-feeds/+/baf36c7eef477aae1f8f2653b6c29e2caf48475b
12 Signed-off-by: Daniel Golle <daniel@makrotopia.org>
13 ---
14 drivers/thermal/mtk_thermal.c | 137 ++++++++++++++++++++++++++++++++--
15 1 file changed, 132 insertions(+), 5 deletions(-)
16
17 --- a/drivers/thermal/mtk_thermal.c
18 +++ b/drivers/thermal/mtk_thermal.c
19 @@ -150,6 +150,20 @@
20 #define CALIB_BUF1_VALID_V2(x) (((x) >> 4) & 0x1)
21 #define CALIB_BUF1_O_SLOPE_SIGN_V2(x) (((x) >> 3) & 0x1)
22
23 +/*
24 + * Layout of the fuses providing the calibration data
25 + * These macros can be used for MT7981 and MT7986.
26 + */
27 +#define CALIB_BUF0_ADC_GE_V3(x) (((x) >> 0) & 0x3ff)
28 +#define CALIB_BUF0_DEGC_CALI_V3(x) (((x) >> 20) & 0x3f)
29 +#define CALIB_BUF0_O_SLOPE_V3(x) (((x) >> 26) & 0x3f)
30 +#define CALIB_BUF1_VTS_TS1_V3(x) (((x) >> 0) & 0x1ff)
31 +#define CALIB_BUF1_VTS_TS2_V3(x) (((x) >> 21) & 0x1ff)
32 +#define CALIB_BUF1_VTS_TSABB_V3(x) (((x) >> 9) & 0x1ff)
33 +#define CALIB_BUF1_VALID_V3(x) (((x) >> 18) & 0x1)
34 +#define CALIB_BUF1_O_SLOPE_SIGN_V3(x) (((x) >> 19) & 0x1)
35 +#define CALIB_BUF1_ID_V3(x) (((x) >> 20) & 0x1)
36 +
37 enum {
38 VTS1,
39 VTS2,
40 @@ -163,6 +177,7 @@ enum {
41 enum mtk_thermal_version {
42 MTK_THERMAL_V1 = 1,
43 MTK_THERMAL_V2,
44 + MTK_THERMAL_V3,
45 };
46
47 /* MT2701 thermal sensors */
48 @@ -245,6 +260,27 @@ enum mtk_thermal_version {
49 /* The calibration coefficient of sensor */
50 #define MT8183_CALIBRATION 153
51
52 +/* AUXADC channel 11 is used for the temperature sensors */
53 +#define MT7986_TEMP_AUXADC_CHANNEL 11
54 +
55 +/* The total number of temperature sensors in the MT7986 */
56 +#define MT7986_NUM_SENSORS 1
57 +
58 +/* The number of banks in the MT7986 */
59 +#define MT7986_NUM_ZONES 1
60 +
61 +/* The number of sensing points per bank */
62 +#define MT7986_NUM_SENSORS_PER_ZONE 1
63 +
64 +/* MT7986 thermal sensors */
65 +#define MT7986_TS1 0
66 +
67 +/* The number of controller in the MT7986 */
68 +#define MT7986_NUM_CONTROLLER 1
69 +
70 +/* The calibration coefficient of sensor */
71 +#define MT7986_CALIBRATION 165
72 +
73 struct mtk_thermal;
74
75 struct thermal_bank_cfg {
76 @@ -388,6 +424,14 @@ static const int mt7622_mux_values[MT762
77 static const int mt7622_vts_index[MT7622_NUM_SENSORS] = { VTS1 };
78 static const int mt7622_tc_offset[MT7622_NUM_CONTROLLER] = { 0x0, };
79
80 +/* MT7986 thermal sensor data */
81 +static const int mt7986_bank_data[MT7986_NUM_SENSORS] = { MT7986_TS1, };
82 +static const int mt7986_msr[MT7986_NUM_SENSORS_PER_ZONE] = { TEMP_MSR0, };
83 +static const int mt7986_adcpnp[MT7986_NUM_SENSORS_PER_ZONE] = { TEMP_ADCPNP0, };
84 +static const int mt7986_mux_values[MT7986_NUM_SENSORS] = { 0, };
85 +static const int mt7986_vts_index[MT7986_NUM_SENSORS] = { VTS1 };
86 +static const int mt7986_tc_offset[MT7986_NUM_CONTROLLER] = { 0x0, };
87 +
88 /*
89 * The MT8173 thermal controller has four banks. Each bank can read up to
90 * four temperature sensors simultaneously. The MT8173 has a total of 5
91 @@ -551,6 +595,30 @@ static const struct mtk_thermal_data mt8
92 .version = MTK_THERMAL_V1,
93 };
94
95 +/*
96 + * MT7986 uses AUXADC Channel 11 for raw data access.
97 + */
98 +static const struct mtk_thermal_data mt7986_thermal_data = {
99 + .auxadc_channel = MT7986_TEMP_AUXADC_CHANNEL,
100 + .num_banks = MT7986_NUM_ZONES,
101 + .num_sensors = MT7986_NUM_SENSORS,
102 + .vts_index = mt7986_vts_index,
103 + .cali_val = MT7986_CALIBRATION,
104 + .num_controller = MT7986_NUM_CONTROLLER,
105 + .controller_offset = mt7986_tc_offset,
106 + .need_switch_bank = true,
107 + .bank_data = {
108 + {
109 + .num_sensors = 1,
110 + .sensors = mt7986_bank_data,
111 + },
112 + },
113 + .msr = mt7986_msr,
114 + .adcpnp = mt7986_adcpnp,
115 + .sensor_mux_values = mt7986_mux_values,
116 + .version = MTK_THERMAL_V3,
117 +};
118 +
119 /**
120 * raw_to_mcelsius - convert a raw ADC value to mcelsius
121 * @mt: The thermal controller
122 @@ -605,6 +673,22 @@ static int raw_to_mcelsius_v2(struct mtk
123 return (format_2 - tmp) * 100;
124 }
125
126 +static int raw_to_mcelsius_v3(struct mtk_thermal *mt, int sensno, s32 raw)
127 +{
128 + s32 tmp;
129 +
130 + if (raw == 0)
131 + return 0;
132 +
133 + raw &= 0xfff;
134 + tmp = 100000 * 15 / 16 * 10000;
135 + tmp /= 4096 - 512 + mt->adc_ge;
136 + tmp /= 1490;
137 + tmp *= raw - mt->vts[sensno] - 2900;
138 +
139 + return mt->degc_cali * 500 - tmp;
140 +}
141 +
142 /**
143 * mtk_thermal_get_bank - get bank
144 * @bank: The bank
145 @@ -885,6 +969,25 @@ static int mtk_thermal_extract_efuse_v2(
146 return 0;
147 }
148
149 +static int mtk_thermal_extract_efuse_v3(struct mtk_thermal *mt, u32 *buf)
150 +{
151 + if (!CALIB_BUF1_VALID_V3(buf[1]))
152 + return -EINVAL;
153 +
154 + mt->adc_ge = CALIB_BUF0_ADC_GE_V3(buf[0]);
155 + mt->degc_cali = CALIB_BUF0_DEGC_CALI_V3(buf[0]);
156 + mt->o_slope = CALIB_BUF0_O_SLOPE_V3(buf[0]);
157 + mt->vts[VTS1] = CALIB_BUF1_VTS_TS1_V3(buf[1]);
158 + mt->vts[VTS2] = CALIB_BUF1_VTS_TS2_V3(buf[1]);
159 + mt->vts[VTSABB] = CALIB_BUF1_VTS_TSABB_V3(buf[1]);
160 + mt->o_slope_sign = CALIB_BUF1_O_SLOPE_SIGN_V3(buf[1]);
161 +
162 + if (CALIB_BUF1_ID_V3(buf[1]) == 0)
163 + mt->o_slope = 0;
164 +
165 + return 0;
166 +}
167 +
168 static int mtk_thermal_get_calibration_data(struct device *dev,
169 struct mtk_thermal *mt)
170 {
171 @@ -895,6 +998,7 @@ static int mtk_thermal_get_calibration_d
172
173 /* Start with default values */
174 mt->adc_ge = 512;
175 + mt->adc_oe = 512;
176 for (i = 0; i < mt->conf->num_sensors; i++)
177 mt->vts[i] = 260;
178 mt->degc_cali = 40;
179 @@ -920,10 +1024,20 @@ static int mtk_thermal_get_calibration_d
180 goto out;
181 }
182
183 - if (mt->conf->version == MTK_THERMAL_V1)
184 + switch (mt->conf->version) {
185 + case MTK_THERMAL_V1:
186 ret = mtk_thermal_extract_efuse_v1(mt, buf);
187 - else
188 + break;
189 + case MTK_THERMAL_V2:
190 ret = mtk_thermal_extract_efuse_v2(mt, buf);
191 + break;
192 + case MTK_THERMAL_V3:
193 + ret = mtk_thermal_extract_efuse_v3(mt, buf);
194 + break;
195 + default:
196 + ret = -EINVAL;
197 + break;
198 + }
199
200 if (ret) {
201 dev_info(dev, "Device not calibrated, using default calibration values\n");
202 @@ -954,6 +1068,10 @@ static const struct of_device_id mtk_the
203 .data = (void *)&mt7622_thermal_data,
204 },
205 {
206 + .compatible = "mediatek,mt7986-thermal",
207 + .data = (void *)&mt7986_thermal_data,
208 + },
209 + {
210 .compatible = "mediatek,mt8183-thermal",
211 .data = (void *)&mt8183_thermal_data,
212 }, {
213 @@ -1068,15 +1186,24 @@ static int mtk_thermal_probe(struct plat
214 goto err_disable_clk_auxadc;
215 }
216
217 - if (mt->conf->version == MTK_THERMAL_V2) {
218 + if (mt->conf->version != MTK_THERMAL_V1) {
219 mtk_thermal_turn_on_buffer(apmixed_base);
220 mtk_thermal_release_periodic_ts(mt, auxadc_base);
221 }
222
223 - if (mt->conf->version == MTK_THERMAL_V1)
224 + switch (mt->conf->version) {
225 + case MTK_THERMAL_V1:
226 mt->raw_to_mcelsius = raw_to_mcelsius_v1;
227 - else
228 + break;
229 + case MTK_THERMAL_V2:
230 mt->raw_to_mcelsius = raw_to_mcelsius_v2;
231 + break;
232 + case MTK_THERMAL_V3:
233 + mt->raw_to_mcelsius = raw_to_mcelsius_v3;
234 + break;
235 + default:
236 + break;
237 + }
238
239 for (ctrl_id = 0; ctrl_id < mt->conf->num_controller ; ctrl_id++)
240 for (i = 0; i < mt->conf->num_banks; i++)