3838#define TEMP_MONIDET0 0x014
3939#define TEMP_MONIDET1 0x018
4040#define TEMP_MSRCTL0 0x038
41+ #define TEMP_MSRCTL1 0x03c
4142#define TEMP_AHBPOLL 0x040
4243#define TEMP_AHBTO 0x044
4344#define TEMP_ADCPNP0 0x048
133134#define CALIB_BUF0_O_SLOPE_SIGN_V1 (x ) (((x) >> 7) & 0x1)
134135#define CALIB_BUF1_ID_V1 (x ) (((x) >> 9) & 0x1)
135136
137+ /*
138+ * Layout of the fuses providing the calibration data
139+ * These macros could be used for MT7622.
140+ */
141+ #define CALIB_BUF0_ADC_OE_V2 (x ) (((x) >> 22) & 0x3ff)
142+ #define CALIB_BUF0_ADC_GE_V2 (x ) (((x) >> 12) & 0x3ff)
143+ #define CALIB_BUF0_DEGC_CALI_V2 (x ) (((x) >> 6) & 0x3f)
144+ #define CALIB_BUF0_O_SLOPE_V2 (x ) (((x) >> 0) & 0x3f)
145+ #define CALIB_BUF1_VTS_TS1_V2 (x ) (((x) >> 23) & 0x1ff)
146+ #define CALIB_BUF1_VTS_TS2_V2 (x ) (((x) >> 14) & 0x1ff)
147+ #define CALIB_BUF1_VTS_TSABB_V2 (x ) (((x) >> 5) & 0x1ff)
148+ #define CALIB_BUF1_VALID_V2 (x ) (((x) >> 4) & 0x1)
149+ #define CALIB_BUF1_O_SLOPE_SIGN_V2 (x ) (((x) >> 3) & 0x1)
150+
136151enum {
137152 VTS1 ,
138153 VTS2 ,
@@ -143,6 +158,11 @@ enum {
143158 MAX_NUM_VTS ,
144159};
145160
161+ enum mtk_thermal_version {
162+ MTK_THERMAL_V1 = 1 ,
163+ MTK_THERMAL_V2 ,
164+ };
165+
146166/* MT2701 thermal sensors */
147167#define MT2701_TS1 0
148168#define MT2701_TS2 1
@@ -245,6 +265,7 @@ struct mtk_thermal_data {
245265 const int * controller_offset ;
246266 bool need_switch_bank ;
247267 struct thermal_bank_cfg bank_data [MAX_NUM_ZONES ];
268+ enum mtk_thermal_version version ;
248269};
249270
250271struct mtk_thermal {
@@ -258,8 +279,10 @@ struct mtk_thermal {
258279
259280 /* Calibration values */
260281 s32 adc_ge ;
282+ s32 adc_oe ;
261283 s32 degc_cali ;
262284 s32 o_slope ;
285+ s32 o_slope_sign ;
263286 s32 vts [MAX_NUM_VTS ];
264287
265288 const struct mtk_thermal_data * conf ;
@@ -398,6 +421,7 @@ static const struct mtk_thermal_data mt8173_thermal_data = {
398421 .msr = mt8173_msr ,
399422 .adcpnp = mt8173_adcpnp ,
400423 .sensor_mux_values = mt8173_mux_values ,
424+ .version = MTK_THERMAL_V1 ,
401425};
402426
403427/*
@@ -428,6 +452,7 @@ static const struct mtk_thermal_data mt2701_thermal_data = {
428452 .msr = mt2701_msr ,
429453 .adcpnp = mt2701_adcpnp ,
430454 .sensor_mux_values = mt2701_mux_values ,
455+ .version = MTK_THERMAL_V1 ,
431456};
432457
433458/*
@@ -458,6 +483,7 @@ static const struct mtk_thermal_data mt2712_thermal_data = {
458483 .msr = mt2712_msr ,
459484 .adcpnp = mt2712_adcpnp ,
460485 .sensor_mux_values = mt2712_mux_values ,
486+ .version = MTK_THERMAL_V1 ,
461487};
462488
463489/*
@@ -482,6 +508,7 @@ static const struct mtk_thermal_data mt7622_thermal_data = {
482508 .msr = mt7622_msr ,
483509 .adcpnp = mt7622_adcpnp ,
484510 .sensor_mux_values = mt7622_mux_values ,
511+ .version = MTK_THERMAL_V2 ,
485512};
486513
487514/*
@@ -514,6 +541,7 @@ static const struct mtk_thermal_data mt8183_thermal_data = {
514541 .msr = mt8183_msr ,
515542 .adcpnp = mt8183_adcpnp ,
516543 .sensor_mux_values = mt8183_mux_values ,
544+ .version = MTK_THERMAL_V1 ,
517545};
518546
519547/**
@@ -540,6 +568,36 @@ static int raw_to_mcelsius_v1(struct mtk_thermal *mt, int sensno, s32 raw)
540568 return mt -> degc_cali * 500 - tmp ;
541569}
542570
571+ static int raw_to_mcelsius_v2 (struct mtk_thermal * mt , int sensno , s32 raw )
572+ {
573+ s32 format_1 = 0 ;
574+ s32 format_2 = 0 ;
575+ s32 g_oe = 1 ;
576+ s32 g_gain = 1 ;
577+ s32 g_x_roomt = 0 ;
578+ s32 tmp = 0 ;
579+
580+ if (raw == 0 )
581+ return 0 ;
582+
583+ raw &= 0xfff ;
584+ g_gain = 10000 + (((mt -> adc_ge - 512 ) * 10000 ) >> 12 );
585+ g_oe = mt -> adc_oe - 512 ;
586+ format_1 = mt -> vts [VTS2 ] + 3105 - g_oe ;
587+ format_2 = (mt -> degc_cali * 10 ) >> 1 ;
588+ g_x_roomt = (((format_1 * 10000 ) >> 12 ) * 10000 ) / g_gain ;
589+
590+ tmp = (((((raw - g_oe ) * 10000 ) >> 12 ) * 10000 ) / g_gain ) - g_x_roomt ;
591+ tmp = tmp * 10 * 100 / 11 ;
592+
593+ if (mt -> o_slope_sign == 0 )
594+ tmp = tmp / (165 - mt -> o_slope );
595+ else
596+ tmp = tmp / (165 + mt -> o_slope );
597+
598+ return (format_2 - tmp ) * 100 ;
599+ }
600+
543601/**
544602 * mtk_thermal_get_bank - get bank
545603 * @bank: The bank
@@ -594,9 +652,13 @@ static int mtk_thermal_bank_temperature(struct mtk_thermal_bank *bank)
594652 raw = readl (mt -> thermal_base +
595653 conf -> msr [conf -> bank_data [bank -> id ].sensors [i ]]);
596654
597- temp = raw_to_mcelsius_v1 (mt ,
598- conf -> bank_data [bank -> id ].sensors [i ],
599- raw );
655+ if (mt -> conf -> version == MTK_THERMAL_V1 ) {
656+ temp = raw_to_mcelsius_v1 (
657+ mt , conf -> bank_data [bank -> id ].sensors [i ], raw );
658+ } else {
659+ temp = raw_to_mcelsius_v2 (
660+ mt , conf -> bank_data [bank -> id ].sensors [i ], raw );
661+ }
600662
601663 /*
602664 * The first read of a sensor often contains very high bogus
@@ -698,9 +760,11 @@ static void mtk_thermal_init_bank(struct mtk_thermal *mt, int num,
698760 writel (auxadc_phys_base + AUXADC_CON1_CLR_V ,
699761 controller_base + TEMP_ADCMUXADDR );
700762
701- /* AHB address for pnp sensor mux selection */
702- writel (apmixed_phys_base + APMIXED_SYS_TS_CON1 ,
703- controller_base + TEMP_PNPMUXADDR );
763+ if (mt -> conf -> version == MTK_THERMAL_V1 ) {
764+ /* AHB address for pnp sensor mux selection */
765+ writel (apmixed_phys_base + APMIXED_SYS_TS_CON1 ,
766+ controller_base + TEMP_PNPMUXADDR );
767+ }
704768
705769 /* AHB value for auxadc enable */
706770 writel (BIT (conf -> auxadc_channel ), controller_base + TEMP_ADCEN );
@@ -803,6 +867,23 @@ static int mtk_thermal_extract_efuse_v1(struct mtk_thermal *mt, u32 *buf)
803867 return 0 ;
804868}
805869
870+ static int mtk_thermal_extract_efuse_v2 (struct mtk_thermal * mt , u32 * buf )
871+ {
872+ if (!CALIB_BUF1_VALID_V2 (buf [1 ]))
873+ return - EINVAL ;
874+
875+ mt -> adc_oe = CALIB_BUF0_ADC_OE_V2 (buf [0 ]);
876+ mt -> adc_ge = CALIB_BUF0_ADC_GE_V2 (buf [0 ]);
877+ mt -> degc_cali = CALIB_BUF0_DEGC_CALI_V2 (buf [0 ]);
878+ mt -> o_slope = CALIB_BUF0_O_SLOPE_V2 (buf [0 ]);
879+ mt -> vts [VTS1 ] = CALIB_BUF1_VTS_TS1_V2 (buf [1 ]);
880+ mt -> vts [VTS2 ] = CALIB_BUF1_VTS_TS2_V2 (buf [1 ]);
881+ mt -> vts [VTSABB ] = CALIB_BUF1_VTS_TSABB_V2 (buf [1 ]);
882+ mt -> o_slope_sign = CALIB_BUF1_O_SLOPE_SIGN_V2 (buf [1 ]);
883+
884+ return 0 ;
885+ }
886+
806887static int mtk_thermal_get_calibration_data (struct device * dev ,
807888 struct mtk_thermal * mt )
808889{
@@ -838,8 +919,15 @@ static int mtk_thermal_get_calibration_data(struct device *dev,
838919 goto out ;
839920 }
840921
841- if (mtk_thermal_extract_efuse_v1 (mt , buf ))
922+ if (mt -> conf -> version == MTK_THERMAL_V1 )
923+ ret = mtk_thermal_extract_efuse_v1 (mt , buf );
924+ else
925+ ret = mtk_thermal_extract_efuse_v2 (mt , buf );
926+
927+ if (ret ) {
842928 dev_info (dev , "Device not calibrated, using default calibration values\n" );
929+ ret = 0 ;
930+ }
843931
844932out :
845933 kfree (buf );
@@ -872,6 +960,28 @@ static const struct of_device_id mtk_thermal_of_match[] = {
872960};
873961MODULE_DEVICE_TABLE (of , mtk_thermal_of_match );
874962
963+ static void mtk_thermal_turn_on_buffer (void __iomem * apmixed_base )
964+ {
965+ int tmp ;
966+
967+ tmp = readl (apmixed_base + APMIXED_SYS_TS_CON1 );
968+ tmp &= ~(0x37 );
969+ tmp |= 0x1 ;
970+ writel (tmp , apmixed_base + APMIXED_SYS_TS_CON1 );
971+ udelay (200 );
972+ }
973+
974+ static void mtk_thermal_release_periodic_ts (struct mtk_thermal * mt ,
975+ void __iomem * auxadc_base )
976+ {
977+ int tmp ;
978+
979+ writel (0x800 , auxadc_base + AUXADC_CON1_SET_V );
980+ writel (0x1 , mt -> thermal_base + TEMP_MONCTL0 );
981+ tmp = readl (mt -> thermal_base + TEMP_MSRCTL1 );
982+ writel ((tmp & (~0x10e )), mt -> thermal_base + TEMP_MSRCTL1 );
983+ }
984+
875985static int mtk_thermal_probe (struct platform_device * pdev )
876986{
877987 int ret , i , ctrl_id ;
@@ -880,6 +990,7 @@ static int mtk_thermal_probe(struct platform_device *pdev)
880990 struct resource * res ;
881991 u64 auxadc_phys_base , apmixed_phys_base ;
882992 struct thermal_zone_device * tzdev ;
993+ void __iomem * apmixed_base , * auxadc_base ;
883994
884995 mt = devm_kzalloc (& pdev -> dev , sizeof (* mt ), GFP_KERNEL );
885996 if (!mt )
@@ -914,6 +1025,7 @@ static int mtk_thermal_probe(struct platform_device *pdev)
9141025 return - ENODEV ;
9151026 }
9161027
1028+ auxadc_base = of_iomap (auxadc , 0 );
9171029 auxadc_phys_base = of_get_phys_base (auxadc );
9181030
9191031 of_node_put (auxadc );
@@ -929,6 +1041,7 @@ static int mtk_thermal_probe(struct platform_device *pdev)
9291041 return - ENODEV ;
9301042 }
9311043
1044+ apmixed_base = of_iomap (apmixedsys , 0 );
9321045 apmixed_phys_base = of_get_phys_base (apmixedsys );
9331046
9341047 of_node_put (apmixedsys );
@@ -954,6 +1067,11 @@ static int mtk_thermal_probe(struct platform_device *pdev)
9541067 goto err_disable_clk_auxadc ;
9551068 }
9561069
1070+ if (mt -> conf -> version == MTK_THERMAL_V2 ) {
1071+ mtk_thermal_turn_on_buffer (apmixed_base );
1072+ mtk_thermal_release_periodic_ts (mt , auxadc_base );
1073+ }
1074+
9571075 for (ctrl_id = 0 ; ctrl_id < mt -> conf -> num_controller ; ctrl_id ++ )
9581076 for (i = 0 ; i < mt -> conf -> num_banks ; i ++ )
9591077 mtk_thermal_init_bank (mt , i , apmixed_phys_base ,
0 commit comments