]> Git Repo - linux.git/blob - drivers/thermal/mtk_thermal.c
net: Call into DSA netdevice_ops wrappers
[linux.git] / drivers / thermal / mtk_thermal.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015 MediaTek Inc.
4  * Author: Hanyi Wu <[email protected]>
5  *         Sascha Hauer <[email protected]>
6  *         Dawei Chien <[email protected]>
7  *         Louis Yu <[email protected]>
8  */
9
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/nvmem-consumer.h>
16 #include <linux/of.h>
17 #include <linux/of_address.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
21 #include <linux/io.h>
22 #include <linux/thermal.h>
23 #include <linux/reset.h>
24 #include <linux/types.h>
25
26 /* AUXADC Registers */
27 #define AUXADC_CON1_SET_V       0x008
28 #define AUXADC_CON1_CLR_V       0x00c
29 #define AUXADC_CON2_V           0x010
30 #define AUXADC_DATA(channel)    (0x14 + (channel) * 4)
31
32 #define APMIXED_SYS_TS_CON1     0x604
33
34 /* Thermal Controller Registers */
35 #define TEMP_MONCTL0            0x000
36 #define TEMP_MONCTL1            0x004
37 #define TEMP_MONCTL2            0x008
38 #define TEMP_MONIDET0           0x014
39 #define TEMP_MONIDET1           0x018
40 #define TEMP_MSRCTL0            0x038
41 #define TEMP_AHBPOLL            0x040
42 #define TEMP_AHBTO              0x044
43 #define TEMP_ADCPNP0            0x048
44 #define TEMP_ADCPNP1            0x04c
45 #define TEMP_ADCPNP2            0x050
46 #define TEMP_ADCPNP3            0x0b4
47
48 #define TEMP_ADCMUX             0x054
49 #define TEMP_ADCEN              0x060
50 #define TEMP_PNPMUXADDR         0x064
51 #define TEMP_ADCMUXADDR         0x068
52 #define TEMP_ADCENADDR          0x074
53 #define TEMP_ADCVALIDADDR       0x078
54 #define TEMP_ADCVOLTADDR        0x07c
55 #define TEMP_RDCTRL             0x080
56 #define TEMP_ADCVALIDMASK       0x084
57 #define TEMP_ADCVOLTAGESHIFT    0x088
58 #define TEMP_ADCWRITECTRL       0x08c
59 #define TEMP_MSR0               0x090
60 #define TEMP_MSR1               0x094
61 #define TEMP_MSR2               0x098
62 #define TEMP_MSR3               0x0B8
63
64 #define TEMP_SPARE0             0x0f0
65
66 #define TEMP_ADCPNP0_1          0x148
67 #define TEMP_ADCPNP1_1          0x14c
68 #define TEMP_ADCPNP2_1          0x150
69 #define TEMP_MSR0_1             0x190
70 #define TEMP_MSR1_1             0x194
71 #define TEMP_MSR2_1             0x198
72 #define TEMP_ADCPNP3_1          0x1b4
73 #define TEMP_MSR3_1             0x1B8
74
75 #define PTPCORESEL              0x400
76
77 #define TEMP_MONCTL1_PERIOD_UNIT(x)     ((x) & 0x3ff)
78
79 #define TEMP_MONCTL2_FILTER_INTERVAL(x) (((x) & 0x3ff) << 16)
80 #define TEMP_MONCTL2_SENSOR_INTERVAL(x) ((x) & 0x3ff)
81
82 #define TEMP_AHBPOLL_ADC_POLL_INTERVAL(x)       (x)
83
84 #define TEMP_ADCWRITECTRL_ADC_PNP_WRITE         BIT(0)
85 #define TEMP_ADCWRITECTRL_ADC_MUX_WRITE         BIT(1)
86
87 #define TEMP_ADCVALIDMASK_VALID_HIGH            BIT(5)
88 #define TEMP_ADCVALIDMASK_VALID_POS(bit)        (bit)
89
90 /* MT8173 thermal sensors */
91 #define MT8173_TS1      0
92 #define MT8173_TS2      1
93 #define MT8173_TS3      2
94 #define MT8173_TS4      3
95 #define MT8173_TSABB    4
96
97 /* AUXADC channel 11 is used for the temperature sensors */
98 #define MT8173_TEMP_AUXADC_CHANNEL      11
99
100 /* The total number of temperature sensors in the MT8173 */
101 #define MT8173_NUM_SENSORS              5
102
103 /* The number of banks in the MT8173 */
104 #define MT8173_NUM_ZONES                4
105
106 /* The number of sensing points per bank */
107 #define MT8173_NUM_SENSORS_PER_ZONE     4
108
109 /* The number of controller in the MT8173 */
110 #define MT8173_NUM_CONTROLLER           1
111
112 /* The calibration coefficient of sensor  */
113 #define MT8173_CALIBRATION      165
114
115 /*
116  * Layout of the fuses providing the calibration data
117  * These macros could be used for MT8183, MT8173, MT2701, and MT2712.
118  * MT8183 has 6 sensors and needs 6 VTS calibration data.
119  * MT8173 has 5 sensors and needs 5 VTS calibration data.
120  * MT2701 has 3 sensors and needs 3 VTS calibration data.
121  * MT2712 has 4 sensors and needs 4 VTS calibration data.
122  */
123 #define CALIB_BUF0_VALID                BIT(0)
124 #define CALIB_BUF1_ADC_GE(x)            (((x) >> 22) & 0x3ff)
125 #define CALIB_BUF0_VTS_TS1(x)           (((x) >> 17) & 0x1ff)
126 #define CALIB_BUF0_VTS_TS2(x)           (((x) >> 8) & 0x1ff)
127 #define CALIB_BUF1_VTS_TS3(x)           (((x) >> 0) & 0x1ff)
128 #define CALIB_BUF2_VTS_TS4(x)           (((x) >> 23) & 0x1ff)
129 #define CALIB_BUF2_VTS_TS5(x)           (((x) >> 5) & 0x1ff)
130 #define CALIB_BUF2_VTS_TSABB(x)         (((x) >> 14) & 0x1ff)
131 #define CALIB_BUF0_DEGC_CALI(x)         (((x) >> 1) & 0x3f)
132 #define CALIB_BUF0_O_SLOPE(x)           (((x) >> 26) & 0x3f)
133 #define CALIB_BUF0_O_SLOPE_SIGN(x)      (((x) >> 7) & 0x1)
134 #define CALIB_BUF1_ID(x)                (((x) >> 9) & 0x1)
135
136 enum {
137         VTS1,
138         VTS2,
139         VTS3,
140         VTS4,
141         VTS5,
142         VTSABB,
143         MAX_NUM_VTS,
144 };
145
146 /* MT2701 thermal sensors */
147 #define MT2701_TS1      0
148 #define MT2701_TS2      1
149 #define MT2701_TSABB    2
150
151 /* AUXADC channel 11 is used for the temperature sensors */
152 #define MT2701_TEMP_AUXADC_CHANNEL      11
153
154 /* The total number of temperature sensors in the MT2701 */
155 #define MT2701_NUM_SENSORS      3
156
157 /* The number of sensing points per bank */
158 #define MT2701_NUM_SENSORS_PER_ZONE     3
159
160 /* The number of controller in the MT2701 */
161 #define MT2701_NUM_CONTROLLER           1
162
163 /* The calibration coefficient of sensor  */
164 #define MT2701_CALIBRATION      165
165
166 /* MT2712 thermal sensors */
167 #define MT2712_TS1      0
168 #define MT2712_TS2      1
169 #define MT2712_TS3      2
170 #define MT2712_TS4      3
171
172 /* AUXADC channel 11 is used for the temperature sensors */
173 #define MT2712_TEMP_AUXADC_CHANNEL      11
174
175 /* The total number of temperature sensors in the MT2712 */
176 #define MT2712_NUM_SENSORS      4
177
178 /* The number of sensing points per bank */
179 #define MT2712_NUM_SENSORS_PER_ZONE     4
180
181 /* The number of controller in the MT2712 */
182 #define MT2712_NUM_CONTROLLER           1
183
184 /* The calibration coefficient of sensor  */
185 #define MT2712_CALIBRATION      165
186
187 #define MT7622_TEMP_AUXADC_CHANNEL      11
188 #define MT7622_NUM_SENSORS              1
189 #define MT7622_NUM_ZONES                1
190 #define MT7622_NUM_SENSORS_PER_ZONE     1
191 #define MT7622_TS1      0
192 #define MT7622_NUM_CONTROLLER           1
193
194 /* The maximum number of banks */
195 #define MAX_NUM_ZONES           8
196
197 /* The calibration coefficient of sensor  */
198 #define MT7622_CALIBRATION      165
199
200 /* MT8183 thermal sensors */
201 #define MT8183_TS1      0
202 #define MT8183_TS2      1
203 #define MT8183_TS3      2
204 #define MT8183_TS4      3
205 #define MT8183_TS5      4
206 #define MT8183_TSABB    5
207
208 /* AUXADC channel  is used for the temperature sensors */
209 #define MT8183_TEMP_AUXADC_CHANNEL      11
210
211 /* The total number of temperature sensors in the MT8183 */
212 #define MT8183_NUM_SENSORS      6
213
214 /* The number of banks in the MT8183 */
215 #define MT8183_NUM_ZONES               1
216
217 /* The number of sensing points per bank */
218 #define MT8183_NUM_SENSORS_PER_ZONE      6
219
220 /* The number of controller in the MT8183 */
221 #define MT8183_NUM_CONTROLLER           2
222
223 /* The calibration coefficient of sensor  */
224 #define MT8183_CALIBRATION      153
225
226 struct mtk_thermal;
227
228 struct thermal_bank_cfg {
229         unsigned int num_sensors;
230         const int *sensors;
231 };
232
233 struct mtk_thermal_bank {
234         struct mtk_thermal *mt;
235         int id;
236 };
237
238 struct mtk_thermal_data {
239         s32 num_banks;
240         s32 num_sensors;
241         s32 auxadc_channel;
242         const int *vts_index;
243         const int *sensor_mux_values;
244         const int *msr;
245         const int *adcpnp;
246         const int cali_val;
247         const int num_controller;
248         const int *controller_offset;
249         bool need_switch_bank;
250         struct thermal_bank_cfg bank_data[MAX_NUM_ZONES];
251 };
252
253 struct mtk_thermal {
254         struct device *dev;
255         void __iomem *thermal_base;
256
257         struct clk *clk_peri_therm;
258         struct clk *clk_auxadc;
259         /* lock: for getting and putting banks */
260         struct mutex lock;
261
262         /* Calibration values */
263         s32 adc_ge;
264         s32 degc_cali;
265         s32 o_slope;
266         s32 vts[MAX_NUM_VTS];
267
268         const struct mtk_thermal_data *conf;
269         struct mtk_thermal_bank banks[MAX_NUM_ZONES];
270 };
271
272 /* MT8183 thermal sensor data */
273 static const int mt8183_bank_data[MT8183_NUM_SENSORS] = {
274         MT8183_TS1, MT8183_TS2, MT8183_TS3, MT8183_TS4, MT8183_TS5, MT8183_TSABB
275 };
276
277 static const int mt8183_msr[MT8183_NUM_SENSORS_PER_ZONE] = {
278         TEMP_MSR0_1, TEMP_MSR1_1, TEMP_MSR2_1, TEMP_MSR1, TEMP_MSR0, TEMP_MSR3_1
279 };
280
281 static const int mt8183_adcpnp[MT8183_NUM_SENSORS_PER_ZONE] = {
282         TEMP_ADCPNP0_1, TEMP_ADCPNP1_1, TEMP_ADCPNP2_1,
283         TEMP_ADCPNP1, TEMP_ADCPNP0, TEMP_ADCPNP3_1
284 };
285
286 static const int mt8183_mux_values[MT8183_NUM_SENSORS] = { 0, 1, 2, 3, 4, 0 };
287 static const int mt8183_tc_offset[MT8183_NUM_CONTROLLER] = {0x0, 0x100};
288
289 static const int mt8183_vts_index[MT8183_NUM_SENSORS] = {
290         VTS1, VTS2, VTS3, VTS4, VTS5, VTSABB
291 };
292
293 /* MT8173 thermal sensor data */
294 static const int mt8173_bank_data[MT8173_NUM_ZONES][3] = {
295         { MT8173_TS2, MT8173_TS3 },
296         { MT8173_TS2, MT8173_TS4 },
297         { MT8173_TS1, MT8173_TS2, MT8173_TSABB },
298         { MT8173_TS2 },
299 };
300
301 static const int mt8173_msr[MT8173_NUM_SENSORS_PER_ZONE] = {
302         TEMP_MSR0, TEMP_MSR1, TEMP_MSR2, TEMP_MSR3
303 };
304
305 static const int mt8173_adcpnp[MT8173_NUM_SENSORS_PER_ZONE] = {
306         TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2, TEMP_ADCPNP3
307 };
308
309 static const int mt8173_mux_values[MT8173_NUM_SENSORS] = { 0, 1, 2, 3, 16 };
310 static const int mt8173_tc_offset[MT8173_NUM_CONTROLLER] = { 0x0, };
311
312 static const int mt8173_vts_index[MT8173_NUM_SENSORS] = {
313         VTS1, VTS2, VTS3, VTS4, VTSABB
314 };
315
316 /* MT2701 thermal sensor data */
317 static const int mt2701_bank_data[MT2701_NUM_SENSORS] = {
318         MT2701_TS1, MT2701_TS2, MT2701_TSABB
319 };
320
321 static const int mt2701_msr[MT2701_NUM_SENSORS_PER_ZONE] = {
322         TEMP_MSR0, TEMP_MSR1, TEMP_MSR2
323 };
324
325 static const int mt2701_adcpnp[MT2701_NUM_SENSORS_PER_ZONE] = {
326         TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2
327 };
328
329 static const int mt2701_mux_values[MT2701_NUM_SENSORS] = { 0, 1, 16 };
330 static const int mt2701_tc_offset[MT2701_NUM_CONTROLLER] = { 0x0, };
331
332 static const int mt2701_vts_index[MT2701_NUM_SENSORS] = {
333         VTS1, VTS2, VTS3
334 };
335
336 /* MT2712 thermal sensor data */
337 static const int mt2712_bank_data[MT2712_NUM_SENSORS] = {
338         MT2712_TS1, MT2712_TS2, MT2712_TS3, MT2712_TS4
339 };
340
341 static const int mt2712_msr[MT2712_NUM_SENSORS_PER_ZONE] = {
342         TEMP_MSR0, TEMP_MSR1, TEMP_MSR2, TEMP_MSR3
343 };
344
345 static const int mt2712_adcpnp[MT2712_NUM_SENSORS_PER_ZONE] = {
346         TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2, TEMP_ADCPNP3
347 };
348
349 static const int mt2712_mux_values[MT2712_NUM_SENSORS] = { 0, 1, 2, 3 };
350 static const int mt2712_tc_offset[MT2712_NUM_CONTROLLER] = { 0x0, };
351
352 static const int mt2712_vts_index[MT2712_NUM_SENSORS] = {
353         VTS1, VTS2, VTS3, VTS4
354 };
355
356 /* MT7622 thermal sensor data */
357 static const int mt7622_bank_data[MT7622_NUM_SENSORS] = { MT7622_TS1, };
358 static const int mt7622_msr[MT7622_NUM_SENSORS_PER_ZONE] = { TEMP_MSR0, };
359 static const int mt7622_adcpnp[MT7622_NUM_SENSORS_PER_ZONE] = { TEMP_ADCPNP0, };
360 static const int mt7622_mux_values[MT7622_NUM_SENSORS] = { 0, };
361 static const int mt7622_vts_index[MT7622_NUM_SENSORS] = { VTS1 };
362 static const int mt7622_tc_offset[MT7622_NUM_CONTROLLER] = { 0x0, };
363
364 /*
365  * The MT8173 thermal controller has four banks. Each bank can read up to
366  * four temperature sensors simultaneously. The MT8173 has a total of 5
367  * temperature sensors. We use each bank to measure a certain area of the
368  * SoC. Since TS2 is located centrally in the SoC it is influenced by multiple
369  * areas, hence is used in different banks.
370  *
371  * The thermal core only gets the maximum temperature of all banks, so
372  * the bank concept wouldn't be necessary here. However, the SVS (Smart
373  * Voltage Scaling) unit makes its decisions based on the same bank
374  * data, and this indeed needs the temperatures of the individual banks
375  * for making better decisions.
376  */
377 static const struct mtk_thermal_data mt8173_thermal_data = {
378         .auxadc_channel = MT8173_TEMP_AUXADC_CHANNEL,
379         .num_banks = MT8173_NUM_ZONES,
380         .num_sensors = MT8173_NUM_SENSORS,
381         .vts_index = mt8173_vts_index,
382         .cali_val = MT8173_CALIBRATION,
383         .num_controller = MT8173_NUM_CONTROLLER,
384         .controller_offset = mt8173_tc_offset,
385         .need_switch_bank = true,
386         .bank_data = {
387                 {
388                         .num_sensors = 2,
389                         .sensors = mt8173_bank_data[0],
390                 }, {
391                         .num_sensors = 2,
392                         .sensors = mt8173_bank_data[1],
393                 }, {
394                         .num_sensors = 3,
395                         .sensors = mt8173_bank_data[2],
396                 }, {
397                         .num_sensors = 1,
398                         .sensors = mt8173_bank_data[3],
399                 },
400         },
401         .msr = mt8173_msr,
402         .adcpnp = mt8173_adcpnp,
403         .sensor_mux_values = mt8173_mux_values,
404 };
405
406 /*
407  * The MT2701 thermal controller has one bank, which can read up to
408  * three temperature sensors simultaneously. The MT2701 has a total of 3
409  * temperature sensors.
410  *
411  * The thermal core only gets the maximum temperature of this one bank,
412  * so the bank concept wouldn't be necessary here. However, the SVS (Smart
413  * Voltage Scaling) unit makes its decisions based on the same bank
414  * data.
415  */
416 static const struct mtk_thermal_data mt2701_thermal_data = {
417         .auxadc_channel = MT2701_TEMP_AUXADC_CHANNEL,
418         .num_banks = 1,
419         .num_sensors = MT2701_NUM_SENSORS,
420         .vts_index = mt2701_vts_index,
421         .cali_val = MT2701_CALIBRATION,
422         .num_controller = MT2701_NUM_CONTROLLER,
423         .controller_offset = mt2701_tc_offset,
424         .need_switch_bank = true,
425         .bank_data = {
426                 {
427                         .num_sensors = 3,
428                         .sensors = mt2701_bank_data,
429                 },
430         },
431         .msr = mt2701_msr,
432         .adcpnp = mt2701_adcpnp,
433         .sensor_mux_values = mt2701_mux_values,
434 };
435
436 /*
437  * The MT2712 thermal controller has one bank, which can read up to
438  * four temperature sensors simultaneously. The MT2712 has a total of 4
439  * temperature sensors.
440  *
441  * The thermal core only gets the maximum temperature of this one bank,
442  * so the bank concept wouldn't be necessary here. However, the SVS (Smart
443  * Voltage Scaling) unit makes its decisions based on the same bank
444  * data.
445  */
446 static const struct mtk_thermal_data mt2712_thermal_data = {
447         .auxadc_channel = MT2712_TEMP_AUXADC_CHANNEL,
448         .num_banks = 1,
449         .num_sensors = MT2712_NUM_SENSORS,
450         .vts_index = mt2712_vts_index,
451         .cali_val = MT2712_CALIBRATION,
452         .num_controller = MT2712_NUM_CONTROLLER,
453         .controller_offset = mt2712_tc_offset,
454         .need_switch_bank = true,
455         .bank_data = {
456                 {
457                         .num_sensors = 4,
458                         .sensors = mt2712_bank_data,
459                 },
460         },
461         .msr = mt2712_msr,
462         .adcpnp = mt2712_adcpnp,
463         .sensor_mux_values = mt2712_mux_values,
464 };
465
466 /*
467  * MT7622 have only one sensing point which uses AUXADC Channel 11 for raw data
468  * access.
469  */
470 static const struct mtk_thermal_data mt7622_thermal_data = {
471         .auxadc_channel = MT7622_TEMP_AUXADC_CHANNEL,
472         .num_banks = MT7622_NUM_ZONES,
473         .num_sensors = MT7622_NUM_SENSORS,
474         .vts_index = mt7622_vts_index,
475         .cali_val = MT7622_CALIBRATION,
476         .num_controller = MT7622_NUM_CONTROLLER,
477         .controller_offset = mt7622_tc_offset,
478         .need_switch_bank = true,
479         .bank_data = {
480                 {
481                         .num_sensors = 1,
482                         .sensors = mt7622_bank_data,
483                 },
484         },
485         .msr = mt7622_msr,
486         .adcpnp = mt7622_adcpnp,
487         .sensor_mux_values = mt7622_mux_values,
488 };
489
490 /*
491  * The MT8183 thermal controller has one bank for the current SW framework.
492  * The MT8183 has a total of 6 temperature sensors.
493  * There are two thermal controller to control the six sensor.
494  * The first one bind 2 sensor, and the other bind 4 sensors.
495  * The thermal core only gets the maximum temperature of all sensor, so
496  * the bank concept wouldn't be necessary here. However, the SVS (Smart
497  * Voltage Scaling) unit makes its decisions based on the same bank
498  * data, and this indeed needs the temperatures of the individual banks
499  * for making better decisions.
500  */
501 static const struct mtk_thermal_data mt8183_thermal_data = {
502         .auxadc_channel = MT8183_TEMP_AUXADC_CHANNEL,
503         .num_banks = MT8183_NUM_ZONES,
504         .num_sensors = MT8183_NUM_SENSORS,
505         .vts_index = mt8183_vts_index,
506         .cali_val = MT8183_CALIBRATION,
507         .num_controller = MT8183_NUM_CONTROLLER,
508         .controller_offset = mt8183_tc_offset,
509         .need_switch_bank = false,
510         .bank_data = {
511                 {
512                         .num_sensors = 6,
513                         .sensors = mt8183_bank_data,
514                 },
515         },
516
517         .msr = mt8183_msr,
518         .adcpnp = mt8183_adcpnp,
519         .sensor_mux_values = mt8183_mux_values,
520 };
521
522 /**
523  * raw_to_mcelsius - convert a raw ADC value to mcelsius
524  * @mt: The thermal controller
525  * @sensno:     sensor number
526  * @raw:        raw ADC value
527  *
528  * This converts the raw ADC value to mcelsius using the SoC specific
529  * calibration constants
530  */
531 static int raw_to_mcelsius(struct mtk_thermal *mt, int sensno, s32 raw)
532 {
533         s32 tmp;
534
535         raw &= 0xfff;
536
537         tmp = 203450520 << 3;
538         tmp /= mt->conf->cali_val + mt->o_slope;
539         tmp /= 10000 + mt->adc_ge;
540         tmp *= raw - mt->vts[sensno] - 3350;
541         tmp >>= 3;
542
543         return mt->degc_cali * 500 - tmp;
544 }
545
546 /**
547  * mtk_thermal_get_bank - get bank
548  * @bank:       The bank
549  *
550  * The bank registers are banked, we have to select a bank in the
551  * PTPCORESEL register to access it.
552  */
553 static void mtk_thermal_get_bank(struct mtk_thermal_bank *bank)
554 {
555         struct mtk_thermal *mt = bank->mt;
556         u32 val;
557
558         if (mt->conf->need_switch_bank) {
559                 mutex_lock(&mt->lock);
560
561                 val = readl(mt->thermal_base + PTPCORESEL);
562                 val &= ~0xf;
563                 val |= bank->id;
564                 writel(val, mt->thermal_base + PTPCORESEL);
565         }
566 }
567
568 /**
569  * mtk_thermal_put_bank - release bank
570  * @bank:       The bank
571  *
572  * release a bank previously taken with mtk_thermal_get_bank,
573  */
574 static void mtk_thermal_put_bank(struct mtk_thermal_bank *bank)
575 {
576         struct mtk_thermal *mt = bank->mt;
577
578         if (mt->conf->need_switch_bank)
579                 mutex_unlock(&mt->lock);
580 }
581
582 /**
583  * mtk_thermal_bank_temperature - get the temperature of a bank
584  * @bank:       The bank
585  *
586  * The temperature of a bank is considered the maximum temperature of
587  * the sensors associated to the bank.
588  */
589 static int mtk_thermal_bank_temperature(struct mtk_thermal_bank *bank)
590 {
591         struct mtk_thermal *mt = bank->mt;
592         const struct mtk_thermal_data *conf = mt->conf;
593         int i, temp = INT_MIN, max = INT_MIN;
594         u32 raw;
595
596         for (i = 0; i < conf->bank_data[bank->id].num_sensors; i++) {
597                 raw = readl(mt->thermal_base +
598                             conf->msr[conf->bank_data[bank->id].sensors[i]]);
599
600                 temp = raw_to_mcelsius(mt,
601                                        conf->bank_data[bank->id].sensors[i],
602                                        raw);
603
604                 /*
605                  * The first read of a sensor often contains very high bogus
606                  * temperature value. Filter these out so that the system does
607                  * not immediately shut down.
608                  */
609                 if (temp > 200000)
610                         temp = 0;
611
612                 if (temp > max)
613                         max = temp;
614         }
615
616         return max;
617 }
618
619 static int mtk_read_temp(void *data, int *temperature)
620 {
621         struct mtk_thermal *mt = data;
622         int i;
623         int tempmax = INT_MIN;
624
625         for (i = 0; i < mt->conf->num_banks; i++) {
626                 struct mtk_thermal_bank *bank = &mt->banks[i];
627
628                 mtk_thermal_get_bank(bank);
629
630                 tempmax = max(tempmax, mtk_thermal_bank_temperature(bank));
631
632                 mtk_thermal_put_bank(bank);
633         }
634
635         *temperature = tempmax;
636
637         return 0;
638 }
639
640 static const struct thermal_zone_of_device_ops mtk_thermal_ops = {
641         .get_temp = mtk_read_temp,
642 };
643
644 static void mtk_thermal_init_bank(struct mtk_thermal *mt, int num,
645                                   u32 apmixed_phys_base, u32 auxadc_phys_base,
646                                   int ctrl_id)
647 {
648         struct mtk_thermal_bank *bank = &mt->banks[num];
649         const struct mtk_thermal_data *conf = mt->conf;
650         int i;
651
652         int offset = mt->conf->controller_offset[ctrl_id];
653         void __iomem *controller_base = mt->thermal_base + offset;
654
655         bank->id = num;
656         bank->mt = mt;
657
658         mtk_thermal_get_bank(bank);
659
660         /* bus clock 66M counting unit is 12 * 15.15ns * 256 = 46.540us */
661         writel(TEMP_MONCTL1_PERIOD_UNIT(12), controller_base + TEMP_MONCTL1);
662
663         /*
664          * filt interval is 1 * 46.540us = 46.54us,
665          * sen interval is 429 * 46.540us = 19.96ms
666          */
667         writel(TEMP_MONCTL2_FILTER_INTERVAL(1) |
668                         TEMP_MONCTL2_SENSOR_INTERVAL(429),
669                         controller_base + TEMP_MONCTL2);
670
671         /* poll is set to 10u */
672         writel(TEMP_AHBPOLL_ADC_POLL_INTERVAL(768),
673                controller_base + TEMP_AHBPOLL);
674
675         /* temperature sampling control, 1 sample */
676         writel(0x0, controller_base + TEMP_MSRCTL0);
677
678         /* exceed this polling time, IRQ would be inserted */
679         writel(0xffffffff, controller_base + TEMP_AHBTO);
680
681         /* number of interrupts per event, 1 is enough */
682         writel(0x0, controller_base + TEMP_MONIDET0);
683         writel(0x0, controller_base + TEMP_MONIDET1);
684
685         /*
686          * The MT8173 thermal controller does not have its own ADC. Instead it
687          * uses AHB bus accesses to control the AUXADC. To do this the thermal
688          * controller has to be programmed with the physical addresses of the
689          * AUXADC registers and with the various bit positions in the AUXADC.
690          * Also the thermal controller controls a mux in the APMIXEDSYS register
691          * space.
692          */
693
694         /*
695          * this value will be stored to TEMP_PNPMUXADDR (TEMP_SPARE0)
696          * automatically by hw
697          */
698         writel(BIT(conf->auxadc_channel), controller_base + TEMP_ADCMUX);
699
700         /* AHB address for auxadc mux selection */
701         writel(auxadc_phys_base + AUXADC_CON1_CLR_V,
702                controller_base + TEMP_ADCMUXADDR);
703
704         /* AHB address for pnp sensor mux selection */
705         writel(apmixed_phys_base + APMIXED_SYS_TS_CON1,
706                controller_base + TEMP_PNPMUXADDR);
707
708         /* AHB value for auxadc enable */
709         writel(BIT(conf->auxadc_channel), controller_base + TEMP_ADCEN);
710
711         /* AHB address for auxadc enable (channel 0 immediate mode selected) */
712         writel(auxadc_phys_base + AUXADC_CON1_SET_V,
713                controller_base + TEMP_ADCENADDR);
714
715         /* AHB address for auxadc valid bit */
716         writel(auxadc_phys_base + AUXADC_DATA(conf->auxadc_channel),
717                controller_base + TEMP_ADCVALIDADDR);
718
719         /* AHB address for auxadc voltage output */
720         writel(auxadc_phys_base + AUXADC_DATA(conf->auxadc_channel),
721                controller_base + TEMP_ADCVOLTADDR);
722
723         /* read valid & voltage are at the same register */
724         writel(0x0, controller_base + TEMP_RDCTRL);
725
726         /* indicate where the valid bit is */
727         writel(TEMP_ADCVALIDMASK_VALID_HIGH | TEMP_ADCVALIDMASK_VALID_POS(12),
728                controller_base + TEMP_ADCVALIDMASK);
729
730         /* no shift */
731         writel(0x0, controller_base + TEMP_ADCVOLTAGESHIFT);
732
733         /* enable auxadc mux write transaction */
734         writel(TEMP_ADCWRITECTRL_ADC_MUX_WRITE,
735                 controller_base + TEMP_ADCWRITECTRL);
736
737         for (i = 0; i < conf->bank_data[num].num_sensors; i++)
738                 writel(conf->sensor_mux_values[conf->bank_data[num].sensors[i]],
739                        mt->thermal_base +
740                        conf->adcpnp[conf->bank_data[num].sensors[i]]);
741
742         writel((1 << conf->bank_data[num].num_sensors) - 1,
743                controller_base + TEMP_MONCTL0);
744
745         writel(TEMP_ADCWRITECTRL_ADC_PNP_WRITE |
746                TEMP_ADCWRITECTRL_ADC_MUX_WRITE,
747                controller_base + TEMP_ADCWRITECTRL);
748
749         mtk_thermal_put_bank(bank);
750 }
751
752 static u64 of_get_phys_base(struct device_node *np)
753 {
754         u64 size64;
755         const __be32 *regaddr_p;
756
757         regaddr_p = of_get_address(np, 0, &size64, NULL);
758         if (!regaddr_p)
759                 return OF_BAD_ADDR;
760
761         return of_translate_address(np, regaddr_p);
762 }
763
764 static int mtk_thermal_get_calibration_data(struct device *dev,
765                                             struct mtk_thermal *mt)
766 {
767         struct nvmem_cell *cell;
768         u32 *buf;
769         size_t len;
770         int i, ret = 0;
771
772         /* Start with default values */
773         mt->adc_ge = 512;
774         for (i = 0; i < mt->conf->num_sensors; i++)
775                 mt->vts[i] = 260;
776         mt->degc_cali = 40;
777         mt->o_slope = 0;
778
779         cell = nvmem_cell_get(dev, "calibration-data");
780         if (IS_ERR(cell)) {
781                 if (PTR_ERR(cell) == -EPROBE_DEFER)
782                         return PTR_ERR(cell);
783                 return 0;
784         }
785
786         buf = (u32 *)nvmem_cell_read(cell, &len);
787
788         nvmem_cell_put(cell);
789
790         if (IS_ERR(buf))
791                 return PTR_ERR(buf);
792
793         if (len < 3 * sizeof(u32)) {
794                 dev_warn(dev, "invalid calibration data\n");
795                 ret = -EINVAL;
796                 goto out;
797         }
798
799         if (buf[0] & CALIB_BUF0_VALID) {
800                 mt->adc_ge = CALIB_BUF1_ADC_GE(buf[1]);
801
802                 for (i = 0; i < mt->conf->num_sensors; i++) {
803                         switch (mt->conf->vts_index[i]) {
804                         case VTS1:
805                                 mt->vts[VTS1] = CALIB_BUF0_VTS_TS1(buf[0]);
806                                 break;
807                         case VTS2:
808                                 mt->vts[VTS2] = CALIB_BUF0_VTS_TS2(buf[0]);
809                                 break;
810                         case VTS3:
811                                 mt->vts[VTS3] = CALIB_BUF1_VTS_TS3(buf[1]);
812                                 break;
813                         case VTS4:
814                                 mt->vts[VTS4] = CALIB_BUF2_VTS_TS4(buf[2]);
815                                 break;
816                         case VTS5:
817                                 mt->vts[VTS5] = CALIB_BUF2_VTS_TS5(buf[2]);
818                                 break;
819                         case VTSABB:
820                                 mt->vts[VTSABB] = CALIB_BUF2_VTS_TSABB(buf[2]);
821                                 break;
822                         default:
823                                 break;
824                         }
825                 }
826
827                 mt->degc_cali = CALIB_BUF0_DEGC_CALI(buf[0]);
828                 if (CALIB_BUF1_ID(buf[1]) &
829                     CALIB_BUF0_O_SLOPE_SIGN(buf[0]))
830                         mt->o_slope = -CALIB_BUF0_O_SLOPE(buf[0]);
831                 else
832                         mt->o_slope = CALIB_BUF0_O_SLOPE(buf[0]);
833         } else {
834                 dev_info(dev, "Device not calibrated, using default calibration values\n");
835         }
836
837 out:
838         kfree(buf);
839
840         return ret;
841 }
842
843 static const struct of_device_id mtk_thermal_of_match[] = {
844         {
845                 .compatible = "mediatek,mt8173-thermal",
846                 .data = (void *)&mt8173_thermal_data,
847         },
848         {
849                 .compatible = "mediatek,mt2701-thermal",
850                 .data = (void *)&mt2701_thermal_data,
851         },
852         {
853                 .compatible = "mediatek,mt2712-thermal",
854                 .data = (void *)&mt2712_thermal_data,
855         },
856         {
857                 .compatible = "mediatek,mt7622-thermal",
858                 .data = (void *)&mt7622_thermal_data,
859         },
860         {
861                 .compatible = "mediatek,mt8183-thermal",
862                 .data = (void *)&mt8183_thermal_data,
863         }, {
864         },
865 };
866 MODULE_DEVICE_TABLE(of, mtk_thermal_of_match);
867
868 static int mtk_thermal_probe(struct platform_device *pdev)
869 {
870         int ret, i, ctrl_id;
871         struct device_node *auxadc, *apmixedsys, *np = pdev->dev.of_node;
872         struct mtk_thermal *mt;
873         struct resource *res;
874         u64 auxadc_phys_base, apmixed_phys_base;
875         struct thermal_zone_device *tzdev;
876
877         mt = devm_kzalloc(&pdev->dev, sizeof(*mt), GFP_KERNEL);
878         if (!mt)
879                 return -ENOMEM;
880
881         mt->conf = of_device_get_match_data(&pdev->dev);
882
883         mt->clk_peri_therm = devm_clk_get(&pdev->dev, "therm");
884         if (IS_ERR(mt->clk_peri_therm))
885                 return PTR_ERR(mt->clk_peri_therm);
886
887         mt->clk_auxadc = devm_clk_get(&pdev->dev, "auxadc");
888         if (IS_ERR(mt->clk_auxadc))
889                 return PTR_ERR(mt->clk_auxadc);
890
891         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
892         mt->thermal_base = devm_ioremap_resource(&pdev->dev, res);
893         if (IS_ERR(mt->thermal_base))
894                 return PTR_ERR(mt->thermal_base);
895
896         ret = mtk_thermal_get_calibration_data(&pdev->dev, mt);
897         if (ret)
898                 return ret;
899
900         mutex_init(&mt->lock);
901
902         mt->dev = &pdev->dev;
903
904         auxadc = of_parse_phandle(np, "mediatek,auxadc", 0);
905         if (!auxadc) {
906                 dev_err(&pdev->dev, "missing auxadc node\n");
907                 return -ENODEV;
908         }
909
910         auxadc_phys_base = of_get_phys_base(auxadc);
911
912         of_node_put(auxadc);
913
914         if (auxadc_phys_base == OF_BAD_ADDR) {
915                 dev_err(&pdev->dev, "Can't get auxadc phys address\n");
916                 return -EINVAL;
917         }
918
919         apmixedsys = of_parse_phandle(np, "mediatek,apmixedsys", 0);
920         if (!apmixedsys) {
921                 dev_err(&pdev->dev, "missing apmixedsys node\n");
922                 return -ENODEV;
923         }
924
925         apmixed_phys_base = of_get_phys_base(apmixedsys);
926
927         of_node_put(apmixedsys);
928
929         if (apmixed_phys_base == OF_BAD_ADDR) {
930                 dev_err(&pdev->dev, "Can't get auxadc phys address\n");
931                 return -EINVAL;
932         }
933
934         ret = device_reset(&pdev->dev);
935         if (ret)
936                 return ret;
937
938         ret = clk_prepare_enable(mt->clk_auxadc);
939         if (ret) {
940                 dev_err(&pdev->dev, "Can't enable auxadc clk: %d\n", ret);
941                 return ret;
942         }
943
944         ret = clk_prepare_enable(mt->clk_peri_therm);
945         if (ret) {
946                 dev_err(&pdev->dev, "Can't enable peri clk: %d\n", ret);
947                 goto err_disable_clk_auxadc;
948         }
949
950         for (ctrl_id = 0; ctrl_id < mt->conf->num_controller ; ctrl_id++)
951                 for (i = 0; i < mt->conf->num_banks; i++)
952                         mtk_thermal_init_bank(mt, i, apmixed_phys_base,
953                                               auxadc_phys_base, ctrl_id);
954
955         platform_set_drvdata(pdev, mt);
956
957         tzdev = devm_thermal_zone_of_sensor_register(&pdev->dev, 0, mt,
958                                                      &mtk_thermal_ops);
959         if (IS_ERR(tzdev)) {
960                 ret = PTR_ERR(tzdev);
961                 goto err_disable_clk_peri_therm;
962         }
963
964         return 0;
965
966 err_disable_clk_peri_therm:
967         clk_disable_unprepare(mt->clk_peri_therm);
968 err_disable_clk_auxadc:
969         clk_disable_unprepare(mt->clk_auxadc);
970
971         return ret;
972 }
973
974 static int mtk_thermal_remove(struct platform_device *pdev)
975 {
976         struct mtk_thermal *mt = platform_get_drvdata(pdev);
977
978         clk_disable_unprepare(mt->clk_peri_therm);
979         clk_disable_unprepare(mt->clk_auxadc);
980
981         return 0;
982 }
983
984 static struct platform_driver mtk_thermal_driver = {
985         .probe = mtk_thermal_probe,
986         .remove = mtk_thermal_remove,
987         .driver = {
988                 .name = "mtk-thermal",
989                 .of_match_table = mtk_thermal_of_match,
990         },
991 };
992
993 module_platform_driver(mtk_thermal_driver);
994
995 MODULE_AUTHOR("Michael Kao <[email protected]>");
996 MODULE_AUTHOR("Louis Yu <[email protected]>");
997 MODULE_AUTHOR("Dawei Chien <[email protected]>");
998 MODULE_AUTHOR("Sascha Hauer <[email protected]>");
999 MODULE_AUTHOR("Hanyi Wu <[email protected]>");
1000 MODULE_DESCRIPTION("Mediatek thermal driver");
1001 MODULE_LICENSE("GPL v2");
This page took 0.090203 seconds and 4 git commands to generate.