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