18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright(c) 2009-2013  Realtek Corporation.*/
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include "../wifi.h"
58c2ecf20Sopenharmony_ci#include "../base.h"
68c2ecf20Sopenharmony_ci#include "../pci.h"
78c2ecf20Sopenharmony_ci#include "../core.h"
88c2ecf20Sopenharmony_ci#include "reg.h"
98c2ecf20Sopenharmony_ci#include "def.h"
108c2ecf20Sopenharmony_ci#include "phy.h"
118c2ecf20Sopenharmony_ci#include "dm.h"
128c2ecf20Sopenharmony_ci#include "fw.h"
138c2ecf20Sopenharmony_ci#include "trx.h"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_cistatic const u32 ofdmswing_table[OFDM_TABLE_SIZE] = {
168c2ecf20Sopenharmony_ci	0x7f8001fe,		/* 0, +6.0dB */
178c2ecf20Sopenharmony_ci	0x788001e2,		/* 1, +5.5dB */
188c2ecf20Sopenharmony_ci	0x71c001c7,		/* 2, +5.0dB */
198c2ecf20Sopenharmony_ci	0x6b8001ae,		/* 3, +4.5dB */
208c2ecf20Sopenharmony_ci	0x65400195,		/* 4, +4.0dB */
218c2ecf20Sopenharmony_ci	0x5fc0017f,		/* 5, +3.5dB */
228c2ecf20Sopenharmony_ci	0x5a400169,		/* 6, +3.0dB */
238c2ecf20Sopenharmony_ci	0x55400155,		/* 7, +2.5dB */
248c2ecf20Sopenharmony_ci	0x50800142,		/* 8, +2.0dB */
258c2ecf20Sopenharmony_ci	0x4c000130,		/* 9, +1.5dB */
268c2ecf20Sopenharmony_ci	0x47c0011f,		/* 10, +1.0dB */
278c2ecf20Sopenharmony_ci	0x43c0010f,		/* 11, +0.5dB */
288c2ecf20Sopenharmony_ci	0x40000100,		/* 12, +0dB */
298c2ecf20Sopenharmony_ci	0x3c8000f2,		/* 13, -0.5dB */
308c2ecf20Sopenharmony_ci	0x390000e4,		/* 14, -1.0dB */
318c2ecf20Sopenharmony_ci	0x35c000d7,		/* 15, -1.5dB */
328c2ecf20Sopenharmony_ci	0x32c000cb,		/* 16, -2.0dB */
338c2ecf20Sopenharmony_ci	0x300000c0,		/* 17, -2.5dB */
348c2ecf20Sopenharmony_ci	0x2d4000b5,		/* 18, -3.0dB */
358c2ecf20Sopenharmony_ci	0x2ac000ab,		/* 19, -3.5dB */
368c2ecf20Sopenharmony_ci	0x288000a2,		/* 20, -4.0dB */
378c2ecf20Sopenharmony_ci	0x26000098,		/* 21, -4.5dB */
388c2ecf20Sopenharmony_ci	0x24000090,		/* 22, -5.0dB */
398c2ecf20Sopenharmony_ci	0x22000088,		/* 23, -5.5dB */
408c2ecf20Sopenharmony_ci	0x20000080,		/* 24, -6.0dB */
418c2ecf20Sopenharmony_ci	0x1e400079,		/* 25, -6.5dB */
428c2ecf20Sopenharmony_ci	0x1c800072,		/* 26, -7.0dB */
438c2ecf20Sopenharmony_ci	0x1b00006c,		/* 27. -7.5dB */
448c2ecf20Sopenharmony_ci	0x19800066,		/* 28, -8.0dB */
458c2ecf20Sopenharmony_ci	0x18000060,		/* 29, -8.5dB */
468c2ecf20Sopenharmony_ci	0x16c0005b,		/* 30, -9.0dB */
478c2ecf20Sopenharmony_ci	0x15800056,		/* 31, -9.5dB */
488c2ecf20Sopenharmony_ci	0x14400051,		/* 32, -10.0dB */
498c2ecf20Sopenharmony_ci	0x1300004c,		/* 33, -10.5dB */
508c2ecf20Sopenharmony_ci	0x12000048,		/* 34, -11.0dB */
518c2ecf20Sopenharmony_ci	0x11000044,		/* 35, -11.5dB */
528c2ecf20Sopenharmony_ci	0x10000040,		/* 36, -12.0dB */
538c2ecf20Sopenharmony_ci	0x0f00003c,		/* 37, -12.5dB */
548c2ecf20Sopenharmony_ci	0x0e400039,		/* 38, -13.0dB */
558c2ecf20Sopenharmony_ci	0x0d800036,		/* 39, -13.5dB */
568c2ecf20Sopenharmony_ci	0x0cc00033,		/* 40, -14.0dB */
578c2ecf20Sopenharmony_ci	0x0c000030,		/* 41, -14.5dB */
588c2ecf20Sopenharmony_ci	0x0b40002d,		/* 42, -15.0dB */
598c2ecf20Sopenharmony_ci};
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic const u8 cck_tbl_ch1_13[CCK_TABLE_SIZE][8] = {
628c2ecf20Sopenharmony_ci	{0x36, 0x35, 0x2e, 0x25, 0x1c, 0x12, 0x09, 0x04},	/* 0, +0dB */
638c2ecf20Sopenharmony_ci	{0x33, 0x32, 0x2b, 0x23, 0x1a, 0x11, 0x08, 0x04},	/* 1, -0.5dB */
648c2ecf20Sopenharmony_ci	{0x30, 0x2f, 0x29, 0x21, 0x19, 0x10, 0x08, 0x03},	/* 2, -1.0dB */
658c2ecf20Sopenharmony_ci	{0x2d, 0x2d, 0x27, 0x1f, 0x18, 0x0f, 0x08, 0x03},	/* 3, -1.5dB */
668c2ecf20Sopenharmony_ci	{0x2b, 0x2a, 0x25, 0x1e, 0x16, 0x0e, 0x07, 0x03},	/* 4, -2.0dB */
678c2ecf20Sopenharmony_ci	{0x28, 0x28, 0x22, 0x1c, 0x15, 0x0d, 0x07, 0x03},	/* 5, -2.5dB */
688c2ecf20Sopenharmony_ci	{0x26, 0x25, 0x21, 0x1b, 0x14, 0x0d, 0x06, 0x03},	/* 6, -3.0dB */
698c2ecf20Sopenharmony_ci	{0x24, 0x23, 0x1f, 0x19, 0x13, 0x0c, 0x06, 0x03},	/* 7, -3.5dB */
708c2ecf20Sopenharmony_ci	{0x22, 0x21, 0x1d, 0x18, 0x11, 0x0b, 0x06, 0x02},	/* 8, -4.0dB */
718c2ecf20Sopenharmony_ci	{0x20, 0x20, 0x1b, 0x16, 0x11, 0x08, 0x05, 0x02},	/* 9, -4.5dB */
728c2ecf20Sopenharmony_ci	{0x1f, 0x1e, 0x1a, 0x15, 0x10, 0x0a, 0x05, 0x02},	/* 10, -5.0dB */
738c2ecf20Sopenharmony_ci	{0x1d, 0x1c, 0x18, 0x14, 0x0f, 0x0a, 0x05, 0x02},	/* 11, -5.5dB */
748c2ecf20Sopenharmony_ci	{0x1b, 0x1a, 0x17, 0x13, 0x0e, 0x09, 0x04, 0x02},	/* 12, -6.0dB */
758c2ecf20Sopenharmony_ci	{0x1a, 0x19, 0x16, 0x12, 0x0d, 0x09, 0x04, 0x02},	/* 13, -6.5dB */
768c2ecf20Sopenharmony_ci	{0x18, 0x17, 0x15, 0x11, 0x0c, 0x08, 0x04, 0x02},	/* 14, -7.0dB */
778c2ecf20Sopenharmony_ci	{0x17, 0x16, 0x13, 0x10, 0x0c, 0x08, 0x04, 0x02},	/* 15, -7.5dB */
788c2ecf20Sopenharmony_ci	{0x16, 0x15, 0x12, 0x0f, 0x0b, 0x07, 0x04, 0x01},	/* 16, -8.0dB */
798c2ecf20Sopenharmony_ci	{0x14, 0x14, 0x11, 0x0e, 0x0b, 0x07, 0x03, 0x02},	/* 17, -8.5dB */
808c2ecf20Sopenharmony_ci	{0x13, 0x13, 0x10, 0x0d, 0x0a, 0x06, 0x03, 0x01},	/* 18, -9.0dB */
818c2ecf20Sopenharmony_ci	{0x12, 0x12, 0x0f, 0x0c, 0x09, 0x06, 0x03, 0x01},	/* 19, -9.5dB */
828c2ecf20Sopenharmony_ci	{0x11, 0x11, 0x0f, 0x0c, 0x09, 0x06, 0x03, 0x01},	/* 20, -10.0dB*/
838c2ecf20Sopenharmony_ci	{0x10, 0x10, 0x0e, 0x0b, 0x08, 0x05, 0x03, 0x01},	/* 21, -10.5dB*/
848c2ecf20Sopenharmony_ci	{0x0f, 0x0f, 0x0d, 0x0b, 0x08, 0x05, 0x03, 0x01},	/* 22, -11.0dB*/
858c2ecf20Sopenharmony_ci	{0x0e, 0x0e, 0x0c, 0x0a, 0x08, 0x05, 0x02, 0x01},	/* 23, -11.5dB*/
868c2ecf20Sopenharmony_ci	{0x0d, 0x0d, 0x0c, 0x0a, 0x07, 0x05, 0x02, 0x01},	/* 24, -12.0dB*/
878c2ecf20Sopenharmony_ci	{0x0d, 0x0c, 0x0b, 0x09, 0x07, 0x04, 0x02, 0x01},	/* 25, -12.5dB*/
888c2ecf20Sopenharmony_ci	{0x0c, 0x0c, 0x0a, 0x09, 0x06, 0x04, 0x02, 0x01},	/* 26, -13.0dB*/
898c2ecf20Sopenharmony_ci	{0x0b, 0x0b, 0x0a, 0x08, 0x06, 0x04, 0x02, 0x01},	/* 27, -13.5dB*/
908c2ecf20Sopenharmony_ci	{0x0b, 0x0a, 0x09, 0x08, 0x06, 0x04, 0x02, 0x01},	/* 28, -14.0dB*/
918c2ecf20Sopenharmony_ci	{0x0a, 0x0a, 0x09, 0x07, 0x05, 0x03, 0x02, 0x01},	/* 29, -14.5dB*/
928c2ecf20Sopenharmony_ci	{0x0a, 0x09, 0x08, 0x07, 0x05, 0x03, 0x02, 0x01},	/* 30, -15.0dB*/
938c2ecf20Sopenharmony_ci	{0x09, 0x09, 0x08, 0x06, 0x05, 0x03, 0x01, 0x01},	/* 31, -15.5dB*/
948c2ecf20Sopenharmony_ci	{0x09, 0x08, 0x07, 0x06, 0x04, 0x03, 0x01, 0x01}	/* 32, -16.0dB*/
958c2ecf20Sopenharmony_ci};
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic const u8 cck_tbl_ch14[CCK_TABLE_SIZE][8] = {
988c2ecf20Sopenharmony_ci	{0x36, 0x35, 0x2e, 0x1b, 0x00, 0x00, 0x00, 0x00},	/* 0, +0dB */
998c2ecf20Sopenharmony_ci	{0x33, 0x32, 0x2b, 0x19, 0x00, 0x00, 0x00, 0x00},	/* 1, -0.5dB */
1008c2ecf20Sopenharmony_ci	{0x30, 0x2f, 0x29, 0x18, 0x00, 0x00, 0x00, 0x00},	/* 2, -1.0dB */
1018c2ecf20Sopenharmony_ci	{0x2d, 0x2d, 0x17, 0x17, 0x00, 0x00, 0x00, 0x00},	/* 3, -1.5dB */
1028c2ecf20Sopenharmony_ci	{0x2b, 0x2a, 0x25, 0x15, 0x00, 0x00, 0x00, 0x00},	/* 4, -2.0dB */
1038c2ecf20Sopenharmony_ci	{0x28, 0x28, 0x24, 0x14, 0x00, 0x00, 0x00, 0x00},	/* 5, -2.5dB */
1048c2ecf20Sopenharmony_ci	{0x26, 0x25, 0x21, 0x13, 0x00, 0x00, 0x00, 0x00},	/* 6, -3.0dB */
1058c2ecf20Sopenharmony_ci	{0x24, 0x23, 0x1f, 0x12, 0x00, 0x00, 0x00, 0x00},	/* 7, -3.5dB */
1068c2ecf20Sopenharmony_ci	{0x22, 0x21, 0x1d, 0x11, 0x00, 0x00, 0x00, 0x00},	/* 8, -4.0dB */
1078c2ecf20Sopenharmony_ci	{0x20, 0x20, 0x1b, 0x10, 0x00, 0x00, 0x00, 0x00},	/* 9, -4.5dB */
1088c2ecf20Sopenharmony_ci	{0x1f, 0x1e, 0x1a, 0x0f, 0x00, 0x00, 0x00, 0x00},	/* 10, -5.0dB */
1098c2ecf20Sopenharmony_ci	{0x1d, 0x1c, 0x18, 0x0e, 0x00, 0x00, 0x00, 0x00},	/* 11, -5.5dB */
1108c2ecf20Sopenharmony_ci	{0x1b, 0x1a, 0x17, 0x0e, 0x00, 0x00, 0x00, 0x00},	/* 12, -6.0dB */
1118c2ecf20Sopenharmony_ci	{0x1a, 0x19, 0x16, 0x0d, 0x00, 0x00, 0x00, 0x00},	/* 13, -6.5dB */
1128c2ecf20Sopenharmony_ci	{0x18, 0x17, 0x15, 0x0c, 0x00, 0x00, 0x00, 0x00},	/* 14, -7.0dB */
1138c2ecf20Sopenharmony_ci	{0x17, 0x16, 0x13, 0x0b, 0x00, 0x00, 0x00, 0x00},	/* 15, -7.5dB */
1148c2ecf20Sopenharmony_ci	{0x16, 0x15, 0x12, 0x0b, 0x00, 0x00, 0x00, 0x00},	/* 16, -8.0dB */
1158c2ecf20Sopenharmony_ci	{0x14, 0x14, 0x11, 0x0a, 0x00, 0x00, 0x00, 0x00},	/* 17, -8.5dB */
1168c2ecf20Sopenharmony_ci	{0x13, 0x13, 0x10, 0x0a, 0x00, 0x00, 0x00, 0x00},	/* 18, -9.0dB */
1178c2ecf20Sopenharmony_ci	{0x12, 0x12, 0x0f, 0x09, 0x00, 0x00, 0x00, 0x00},	/* 19, -9.5dB */
1188c2ecf20Sopenharmony_ci	{0x11, 0x11, 0x0f, 0x09, 0x00, 0x00, 0x00, 0x00},	/* 20, -10.0dB*/
1198c2ecf20Sopenharmony_ci	{0x10, 0x10, 0x0e, 0x08, 0x00, 0x00, 0x00, 0x00},	/* 21, -10.5dB*/
1208c2ecf20Sopenharmony_ci	{0x0f, 0x0f, 0x0d, 0x08, 0x00, 0x00, 0x00, 0x00},	/* 22, -11.0dB*/
1218c2ecf20Sopenharmony_ci	{0x0e, 0x0e, 0x0c, 0x07, 0x00, 0x00, 0x00, 0x00},	/* 23, -11.5dB*/
1228c2ecf20Sopenharmony_ci	{0x0d, 0x0d, 0x0c, 0x07, 0x00, 0x00, 0x00, 0x00},	/* 24, -12.0dB*/
1238c2ecf20Sopenharmony_ci	{0x0d, 0x0c, 0x0b, 0x06, 0x00, 0x00, 0x00, 0x00},	/* 25, -12.5dB*/
1248c2ecf20Sopenharmony_ci	{0x0c, 0x0c, 0x0a, 0x06, 0x00, 0x00, 0x00, 0x00},	/* 26, -13.0dB*/
1258c2ecf20Sopenharmony_ci	{0x0b, 0x0b, 0x0a, 0x06, 0x00, 0x00, 0x00, 0x00},	/* 27, -13.5dB*/
1268c2ecf20Sopenharmony_ci	{0x0b, 0x0a, 0x09, 0x05, 0x00, 0x00, 0x00, 0x00},	/* 28, -14.0dB*/
1278c2ecf20Sopenharmony_ci	{0x0a, 0x0a, 0x09, 0x05, 0x00, 0x00, 0x00, 0x00},	/* 29, -14.5dB*/
1288c2ecf20Sopenharmony_ci	{0x0a, 0x09, 0x08, 0x05, 0x00, 0x00, 0x00, 0x00},	/* 30, -15.0dB*/
1298c2ecf20Sopenharmony_ci	{0x09, 0x09, 0x08, 0x05, 0x00, 0x00, 0x00, 0x00},	/* 31, -15.5dB*/
1308c2ecf20Sopenharmony_ci	{0x09, 0x08, 0x07, 0x04, 0x00, 0x00, 0x00, 0x00}	/* 32, -16.0dB*/
1318c2ecf20Sopenharmony_ci};
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci#define	CAL_SWING_OFF(_off, _dir, _size, _del)				\
1348c2ecf20Sopenharmony_ci	do {								\
1358c2ecf20Sopenharmony_ci		for (_off = 0; _off < _size; _off++) {			\
1368c2ecf20Sopenharmony_ci			if (_del < thermal_threshold[_dir][_off]) {	\
1378c2ecf20Sopenharmony_ci				if (_off != 0)				\
1388c2ecf20Sopenharmony_ci					_off--;				\
1398c2ecf20Sopenharmony_ci				break;					\
1408c2ecf20Sopenharmony_ci			}						\
1418c2ecf20Sopenharmony_ci		}							\
1428c2ecf20Sopenharmony_ci		if (_off >= _size)					\
1438c2ecf20Sopenharmony_ci			_off = _size - 1;				\
1448c2ecf20Sopenharmony_ci	} while (0)
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistatic void rtl88e_set_iqk_matrix(struct ieee80211_hw *hw,
1478c2ecf20Sopenharmony_ci				  u8 ofdm_index, u8 rfpath,
1488c2ecf20Sopenharmony_ci				  long iqk_result_x, long iqk_result_y)
1498c2ecf20Sopenharmony_ci{
1508c2ecf20Sopenharmony_ci	long ele_a = 0, ele_d, ele_c = 0, value32;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	ele_d = (ofdmswing_table[ofdm_index] & 0xFFC00000)>>22;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	if (iqk_result_x != 0) {
1558c2ecf20Sopenharmony_ci		if ((iqk_result_x & 0x00000200) != 0)
1568c2ecf20Sopenharmony_ci			iqk_result_x = iqk_result_x | 0xFFFFFC00;
1578c2ecf20Sopenharmony_ci		ele_a = ((iqk_result_x * ele_d)>>8)&0x000003FF;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci		if ((iqk_result_y & 0x00000200) != 0)
1608c2ecf20Sopenharmony_ci			iqk_result_y = iqk_result_y | 0xFFFFFC00;
1618c2ecf20Sopenharmony_ci		ele_c = ((iqk_result_y * ele_d)>>8)&0x000003FF;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci		switch (rfpath) {
1648c2ecf20Sopenharmony_ci		case RF90_PATH_A:
1658c2ecf20Sopenharmony_ci			value32 = (ele_d << 22)|((ele_c & 0x3F)<<16) | ele_a;
1668c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, ROFDM0_XATXIQIMBALANCE,
1678c2ecf20Sopenharmony_ci				      MASKDWORD, value32);
1688c2ecf20Sopenharmony_ci			value32 = (ele_c & 0x000003C0) >> 6;
1698c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, ROFDM0_XCTXAFE, MASKH4BITS,
1708c2ecf20Sopenharmony_ci				      value32);
1718c2ecf20Sopenharmony_ci			value32 = ((iqk_result_x * ele_d) >> 7) & 0x01;
1728c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD, BIT(24),
1738c2ecf20Sopenharmony_ci				      value32);
1748c2ecf20Sopenharmony_ci			break;
1758c2ecf20Sopenharmony_ci		case RF90_PATH_B:
1768c2ecf20Sopenharmony_ci			value32 = (ele_d << 22)|((ele_c & 0x3F)<<16) | ele_a;
1778c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, ROFDM0_XBTXIQIMBALANCE, MASKDWORD,
1788c2ecf20Sopenharmony_ci				      value32);
1798c2ecf20Sopenharmony_ci			value32 = (ele_c & 0x000003C0) >> 6;
1808c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, ROFDM0_XDTXAFE, MASKH4BITS, value32);
1818c2ecf20Sopenharmony_ci			value32 = ((iqk_result_x * ele_d) >> 7) & 0x01;
1828c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD, BIT(28),
1838c2ecf20Sopenharmony_ci				      value32);
1848c2ecf20Sopenharmony_ci			break;
1858c2ecf20Sopenharmony_ci		default:
1868c2ecf20Sopenharmony_ci			break;
1878c2ecf20Sopenharmony_ci		}
1888c2ecf20Sopenharmony_ci	} else {
1898c2ecf20Sopenharmony_ci		switch (rfpath) {
1908c2ecf20Sopenharmony_ci		case RF90_PATH_A:
1918c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, ROFDM0_XATXIQIMBALANCE,
1928c2ecf20Sopenharmony_ci				      MASKDWORD, ofdmswing_table[ofdm_index]);
1938c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, ROFDM0_XCTXAFE,
1948c2ecf20Sopenharmony_ci				      MASKH4BITS, 0x00);
1958c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD,
1968c2ecf20Sopenharmony_ci				      BIT(24), 0x00);
1978c2ecf20Sopenharmony_ci			break;
1988c2ecf20Sopenharmony_ci		case RF90_PATH_B:
1998c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, ROFDM0_XBTXIQIMBALANCE,
2008c2ecf20Sopenharmony_ci				      MASKDWORD, ofdmswing_table[ofdm_index]);
2018c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, ROFDM0_XDTXAFE,
2028c2ecf20Sopenharmony_ci				      MASKH4BITS, 0x00);
2038c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD,
2048c2ecf20Sopenharmony_ci				      BIT(28), 0x00);
2058c2ecf20Sopenharmony_ci			break;
2068c2ecf20Sopenharmony_ci		default:
2078c2ecf20Sopenharmony_ci			break;
2088c2ecf20Sopenharmony_ci		}
2098c2ecf20Sopenharmony_ci	}
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_civoid rtl88e_dm_txpower_track_adjust(struct ieee80211_hw *hw,
2138c2ecf20Sopenharmony_ci	u8 type, u8 *pdirection, u32 *poutwrite_val)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
2168c2ecf20Sopenharmony_ci	struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
2178c2ecf20Sopenharmony_ci	u8 pwr_val = 0;
2188c2ecf20Sopenharmony_ci	u8 cck_base = rtldm->swing_idx_cck_base;
2198c2ecf20Sopenharmony_ci	u8 cck_val = rtldm->swing_idx_cck;
2208c2ecf20Sopenharmony_ci	u8 ofdm_base = rtldm->swing_idx_ofdm_base[0];
2218c2ecf20Sopenharmony_ci	u8 ofdm_val = rtlpriv->dm.swing_idx_ofdm[RF90_PATH_A];
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	if (type == 0) {
2248c2ecf20Sopenharmony_ci		if (ofdm_val <= ofdm_base) {
2258c2ecf20Sopenharmony_ci			*pdirection = 1;
2268c2ecf20Sopenharmony_ci			pwr_val = ofdm_base - ofdm_val;
2278c2ecf20Sopenharmony_ci		} else {
2288c2ecf20Sopenharmony_ci			*pdirection = 2;
2298c2ecf20Sopenharmony_ci			pwr_val = ofdm_base - ofdm_val;
2308c2ecf20Sopenharmony_ci		}
2318c2ecf20Sopenharmony_ci	} else if (type == 1) {
2328c2ecf20Sopenharmony_ci		if (cck_val <= cck_base) {
2338c2ecf20Sopenharmony_ci			*pdirection = 1;
2348c2ecf20Sopenharmony_ci			pwr_val = cck_base - cck_val;
2358c2ecf20Sopenharmony_ci		} else {
2368c2ecf20Sopenharmony_ci			*pdirection = 2;
2378c2ecf20Sopenharmony_ci			pwr_val = cck_val - cck_base;
2388c2ecf20Sopenharmony_ci		}
2398c2ecf20Sopenharmony_ci	}
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	if (pwr_val >= TXPWRTRACK_MAX_IDX && (*pdirection == 1))
2428c2ecf20Sopenharmony_ci		pwr_val = TXPWRTRACK_MAX_IDX;
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	*poutwrite_val = pwr_val | (pwr_val << 8) | (pwr_val << 16) |
2458c2ecf20Sopenharmony_ci			 (pwr_val << 24);
2468c2ecf20Sopenharmony_ci}
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_cistatic void dm_tx_pwr_track_set_pwr(struct ieee80211_hw *hw,
2498c2ecf20Sopenharmony_ci				    enum pwr_track_control_method method,
2508c2ecf20Sopenharmony_ci				    u8 rfpath, u8 channel_mapped_index)
2518c2ecf20Sopenharmony_ci{
2528c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
2538c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &rtlpriv->phy;
2548c2ecf20Sopenharmony_ci	struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	if (method == TXAGC) {
2578c2ecf20Sopenharmony_ci		if (rtldm->swing_flag_ofdm ||
2588c2ecf20Sopenharmony_ci		    rtldm->swing_flag_cck) {
2598c2ecf20Sopenharmony_ci			rtl88e_phy_set_txpower_level(hw,
2608c2ecf20Sopenharmony_ci						     rtlphy->current_channel);
2618c2ecf20Sopenharmony_ci			rtldm->swing_flag_ofdm = false;
2628c2ecf20Sopenharmony_ci			rtldm->swing_flag_cck = false;
2638c2ecf20Sopenharmony_ci		}
2648c2ecf20Sopenharmony_ci	} else if (method == BBSWING) {
2658c2ecf20Sopenharmony_ci		if (!rtldm->cck_inch14) {
2668c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, 0xa22,
2678c2ecf20Sopenharmony_ci				       cck_tbl_ch1_13[rtldm->swing_idx_cck][0]);
2688c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, 0xa23,
2698c2ecf20Sopenharmony_ci				       cck_tbl_ch1_13[rtldm->swing_idx_cck][1]);
2708c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, 0xa24,
2718c2ecf20Sopenharmony_ci				       cck_tbl_ch1_13[rtldm->swing_idx_cck][2]);
2728c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, 0xa25,
2738c2ecf20Sopenharmony_ci				       cck_tbl_ch1_13[rtldm->swing_idx_cck][3]);
2748c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, 0xa26,
2758c2ecf20Sopenharmony_ci				       cck_tbl_ch1_13[rtldm->swing_idx_cck][4]);
2768c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, 0xa27,
2778c2ecf20Sopenharmony_ci				       cck_tbl_ch1_13[rtldm->swing_idx_cck][5]);
2788c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, 0xa28,
2798c2ecf20Sopenharmony_ci				       cck_tbl_ch1_13[rtldm->swing_idx_cck][6]);
2808c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, 0xa29,
2818c2ecf20Sopenharmony_ci				       cck_tbl_ch1_13[rtldm->swing_idx_cck][7]);
2828c2ecf20Sopenharmony_ci		} else {
2838c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, 0xa22,
2848c2ecf20Sopenharmony_ci				       cck_tbl_ch14[rtldm->swing_idx_cck][0]);
2858c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, 0xa23,
2868c2ecf20Sopenharmony_ci				       cck_tbl_ch14[rtldm->swing_idx_cck][1]);
2878c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, 0xa24,
2888c2ecf20Sopenharmony_ci				       cck_tbl_ch14[rtldm->swing_idx_cck][2]);
2898c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, 0xa25,
2908c2ecf20Sopenharmony_ci				       cck_tbl_ch14[rtldm->swing_idx_cck][3]);
2918c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, 0xa26,
2928c2ecf20Sopenharmony_ci				       cck_tbl_ch14[rtldm->swing_idx_cck][4]);
2938c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, 0xa27,
2948c2ecf20Sopenharmony_ci				       cck_tbl_ch14[rtldm->swing_idx_cck][5]);
2958c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, 0xa28,
2968c2ecf20Sopenharmony_ci				       cck_tbl_ch14[rtldm->swing_idx_cck][6]);
2978c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, 0xa29,
2988c2ecf20Sopenharmony_ci				       cck_tbl_ch14[rtldm->swing_idx_cck][7]);
2998c2ecf20Sopenharmony_ci		}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci		if (rfpath == RF90_PATH_A) {
3028c2ecf20Sopenharmony_ci			rtl88e_set_iqk_matrix(hw, rtldm->swing_idx_ofdm[rfpath],
3038c2ecf20Sopenharmony_ci					      rfpath, rtlphy->iqk_matrix
3048c2ecf20Sopenharmony_ci					      [channel_mapped_index].
3058c2ecf20Sopenharmony_ci					      value[0][0],
3068c2ecf20Sopenharmony_ci					      rtlphy->iqk_matrix
3078c2ecf20Sopenharmony_ci					      [channel_mapped_index].
3088c2ecf20Sopenharmony_ci					      value[0][1]);
3098c2ecf20Sopenharmony_ci		} else if (rfpath == RF90_PATH_B) {
3108c2ecf20Sopenharmony_ci			rtl88e_set_iqk_matrix(hw, rtldm->swing_idx_ofdm[rfpath],
3118c2ecf20Sopenharmony_ci					      rfpath, rtlphy->iqk_matrix
3128c2ecf20Sopenharmony_ci					      [channel_mapped_index].
3138c2ecf20Sopenharmony_ci					      value[0][4],
3148c2ecf20Sopenharmony_ci					      rtlphy->iqk_matrix
3158c2ecf20Sopenharmony_ci					      [channel_mapped_index].
3168c2ecf20Sopenharmony_ci					      value[0][5]);
3178c2ecf20Sopenharmony_ci		}
3188c2ecf20Sopenharmony_ci	} else {
3198c2ecf20Sopenharmony_ci		return;
3208c2ecf20Sopenharmony_ci	}
3218c2ecf20Sopenharmony_ci}
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_cistatic u8 rtl88e_dm_initial_gain_min_pwdb(struct ieee80211_hw *hw)
3248c2ecf20Sopenharmony_ci{
3258c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
3268c2ecf20Sopenharmony_ci	struct dig_t *dm_dig = &rtlpriv->dm_digtable;
3278c2ecf20Sopenharmony_ci	long rssi_val_min = 0;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	if ((dm_dig->curmultista_cstate == DIG_MULTISTA_CONNECT) &&
3308c2ecf20Sopenharmony_ci	    (dm_dig->cur_sta_cstate == DIG_STA_CONNECT)) {
3318c2ecf20Sopenharmony_ci		if (rtlpriv->dm.entry_min_undec_sm_pwdb != 0)
3328c2ecf20Sopenharmony_ci			rssi_val_min =
3338c2ecf20Sopenharmony_ci			    (rtlpriv->dm.entry_min_undec_sm_pwdb >
3348c2ecf20Sopenharmony_ci			     rtlpriv->dm.undec_sm_pwdb) ?
3358c2ecf20Sopenharmony_ci			    rtlpriv->dm.undec_sm_pwdb :
3368c2ecf20Sopenharmony_ci			    rtlpriv->dm.entry_min_undec_sm_pwdb;
3378c2ecf20Sopenharmony_ci		else
3388c2ecf20Sopenharmony_ci			rssi_val_min = rtlpriv->dm.undec_sm_pwdb;
3398c2ecf20Sopenharmony_ci	} else if (dm_dig->cur_sta_cstate == DIG_STA_CONNECT ||
3408c2ecf20Sopenharmony_ci		   dm_dig->cur_sta_cstate == DIG_STA_BEFORE_CONNECT) {
3418c2ecf20Sopenharmony_ci		rssi_val_min = rtlpriv->dm.undec_sm_pwdb;
3428c2ecf20Sopenharmony_ci	} else if (dm_dig->curmultista_cstate ==
3438c2ecf20Sopenharmony_ci		DIG_MULTISTA_CONNECT) {
3448c2ecf20Sopenharmony_ci		rssi_val_min = rtlpriv->dm.entry_min_undec_sm_pwdb;
3458c2ecf20Sopenharmony_ci	}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	return (u8)rssi_val_min;
3488c2ecf20Sopenharmony_ci}
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_cistatic void rtl88e_dm_false_alarm_counter_statistics(struct ieee80211_hw *hw)
3518c2ecf20Sopenharmony_ci{
3528c2ecf20Sopenharmony_ci	u32 ret_value;
3538c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
3548c2ecf20Sopenharmony_ci	struct false_alarm_statistics *falsealm_cnt = &rtlpriv->falsealm_cnt;
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, ROFDM0_LSTF, BIT(31), 1);
3578c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, ROFDM1_LSTF, BIT(31), 1);
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	ret_value = rtl_get_bbreg(hw, ROFDM0_FRAMESYNC, MASKDWORD);
3608c2ecf20Sopenharmony_ci	falsealm_cnt->cnt_fast_fsync_fail = (ret_value&0xffff);
3618c2ecf20Sopenharmony_ci	falsealm_cnt->cnt_sb_search_fail = ((ret_value&0xffff0000)>>16);
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	ret_value = rtl_get_bbreg(hw, ROFDM_PHYCOUNTER1, MASKDWORD);
3648c2ecf20Sopenharmony_ci	falsealm_cnt->cnt_ofdm_cca = (ret_value&0xffff);
3658c2ecf20Sopenharmony_ci	falsealm_cnt->cnt_parity_fail = ((ret_value & 0xffff0000) >> 16);
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	ret_value = rtl_get_bbreg(hw, ROFDM_PHYCOUNTER2, MASKDWORD);
3688c2ecf20Sopenharmony_ci	falsealm_cnt->cnt_rate_illegal = (ret_value & 0xffff);
3698c2ecf20Sopenharmony_ci	falsealm_cnt->cnt_crc8_fail = ((ret_value & 0xffff0000) >> 16);
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	ret_value = rtl_get_bbreg(hw, ROFDM_PHYCOUNTER3, MASKDWORD);
3728c2ecf20Sopenharmony_ci	falsealm_cnt->cnt_mcs_fail = (ret_value & 0xffff);
3738c2ecf20Sopenharmony_ci	falsealm_cnt->cnt_ofdm_fail = falsealm_cnt->cnt_parity_fail +
3748c2ecf20Sopenharmony_ci		falsealm_cnt->cnt_rate_illegal +
3758c2ecf20Sopenharmony_ci		falsealm_cnt->cnt_crc8_fail +
3768c2ecf20Sopenharmony_ci		falsealm_cnt->cnt_mcs_fail +
3778c2ecf20Sopenharmony_ci		falsealm_cnt->cnt_fast_fsync_fail +
3788c2ecf20Sopenharmony_ci		falsealm_cnt->cnt_sb_search_fail;
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	ret_value = rtl_get_bbreg(hw, REG_SC_CNT, MASKDWORD);
3818c2ecf20Sopenharmony_ci	falsealm_cnt->cnt_bw_lsc = (ret_value & 0xffff);
3828c2ecf20Sopenharmony_ci	falsealm_cnt->cnt_bw_usc = ((ret_value & 0xffff0000) >> 16);
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RCCK0_FALSEALARMREPORT, BIT(12), 1);
3858c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RCCK0_FALSEALARMREPORT, BIT(14), 1);
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	ret_value = rtl_get_bbreg(hw, RCCK0_FACOUNTERLOWER, MASKBYTE0);
3888c2ecf20Sopenharmony_ci	falsealm_cnt->cnt_cck_fail = ret_value;
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	ret_value = rtl_get_bbreg(hw, RCCK0_FACOUNTERUPPER, MASKBYTE3);
3918c2ecf20Sopenharmony_ci	falsealm_cnt->cnt_cck_fail += (ret_value & 0xff) << 8;
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	ret_value = rtl_get_bbreg(hw, RCCK0_CCA_CNT, MASKDWORD);
3948c2ecf20Sopenharmony_ci	falsealm_cnt->cnt_cck_cca = ((ret_value & 0xff) << 8) |
3958c2ecf20Sopenharmony_ci		((ret_value&0xFF00)>>8);
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	falsealm_cnt->cnt_all = (falsealm_cnt->cnt_fast_fsync_fail +
3988c2ecf20Sopenharmony_ci				falsealm_cnt->cnt_sb_search_fail +
3998c2ecf20Sopenharmony_ci				falsealm_cnt->cnt_parity_fail +
4008c2ecf20Sopenharmony_ci				falsealm_cnt->cnt_rate_illegal +
4018c2ecf20Sopenharmony_ci				falsealm_cnt->cnt_crc8_fail +
4028c2ecf20Sopenharmony_ci				falsealm_cnt->cnt_mcs_fail +
4038c2ecf20Sopenharmony_ci				falsealm_cnt->cnt_cck_fail);
4048c2ecf20Sopenharmony_ci	falsealm_cnt->cnt_cca_all = falsealm_cnt->cnt_ofdm_cca +
4058c2ecf20Sopenharmony_ci		falsealm_cnt->cnt_cck_cca;
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, ROFDM0_TRSWISOLATION, BIT(31), 1);
4088c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, ROFDM0_TRSWISOLATION, BIT(31), 0);
4098c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, ROFDM1_LSTF, BIT(27), 1);
4108c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, ROFDM1_LSTF, BIT(27), 0);
4118c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, ROFDM0_LSTF, BIT(31), 0);
4128c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, ROFDM1_LSTF, BIT(31), 0);
4138c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RCCK0_FALSEALARMREPORT, BIT(13)|BIT(12), 0);
4148c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RCCK0_FALSEALARMREPORT, BIT(13)|BIT(12), 2);
4158c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RCCK0_FALSEALARMREPORT, BIT(15)|BIT(14), 0);
4168c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RCCK0_FALSEALARMREPORT, BIT(15)|BIT(14), 2);
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_DIG, DBG_TRACE,
4198c2ecf20Sopenharmony_ci		"cnt_parity_fail = %d, cnt_rate_illegal = %d, cnt_crc8_fail = %d, cnt_mcs_fail = %d\n",
4208c2ecf20Sopenharmony_ci		falsealm_cnt->cnt_parity_fail,
4218c2ecf20Sopenharmony_ci		falsealm_cnt->cnt_rate_illegal,
4228c2ecf20Sopenharmony_ci		falsealm_cnt->cnt_crc8_fail, falsealm_cnt->cnt_mcs_fail);
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_DIG, DBG_TRACE,
4258c2ecf20Sopenharmony_ci		"cnt_ofdm_fail = %x, cnt_cck_fail = %x, cnt_all = %x\n",
4268c2ecf20Sopenharmony_ci		falsealm_cnt->cnt_ofdm_fail,
4278c2ecf20Sopenharmony_ci		falsealm_cnt->cnt_cck_fail, falsealm_cnt->cnt_all);
4288c2ecf20Sopenharmony_ci}
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_cistatic void rtl88e_dm_cck_packet_detection_thresh(struct ieee80211_hw *hw)
4318c2ecf20Sopenharmony_ci{
4328c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
4338c2ecf20Sopenharmony_ci	struct dig_t *dm_dig = &rtlpriv->dm_digtable;
4348c2ecf20Sopenharmony_ci	u8 cur_cck_cca_thresh;
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	if (dm_dig->cur_sta_cstate == DIG_STA_CONNECT) {
4378c2ecf20Sopenharmony_ci		dm_dig->rssi_val_min = rtl88e_dm_initial_gain_min_pwdb(hw);
4388c2ecf20Sopenharmony_ci		if (dm_dig->rssi_val_min > 25) {
4398c2ecf20Sopenharmony_ci			cur_cck_cca_thresh = 0xcd;
4408c2ecf20Sopenharmony_ci		} else if ((dm_dig->rssi_val_min <= 25) &&
4418c2ecf20Sopenharmony_ci			   (dm_dig->rssi_val_min > 10)) {
4428c2ecf20Sopenharmony_ci			cur_cck_cca_thresh = 0x83;
4438c2ecf20Sopenharmony_ci		} else {
4448c2ecf20Sopenharmony_ci			if (rtlpriv->falsealm_cnt.cnt_cck_fail > 1000)
4458c2ecf20Sopenharmony_ci				cur_cck_cca_thresh = 0x83;
4468c2ecf20Sopenharmony_ci			else
4478c2ecf20Sopenharmony_ci				cur_cck_cca_thresh = 0x40;
4488c2ecf20Sopenharmony_ci		}
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	} else {
4518c2ecf20Sopenharmony_ci		if (rtlpriv->falsealm_cnt.cnt_cck_fail > 1000)
4528c2ecf20Sopenharmony_ci			cur_cck_cca_thresh = 0x83;
4538c2ecf20Sopenharmony_ci		else
4548c2ecf20Sopenharmony_ci			cur_cck_cca_thresh = 0x40;
4558c2ecf20Sopenharmony_ci	}
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	if (dm_dig->cur_cck_cca_thres != cur_cck_cca_thresh)
4588c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, RCCK0_CCA, MASKBYTE2, cur_cck_cca_thresh);
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	dm_dig->cur_cck_cca_thres = cur_cck_cca_thresh;
4618c2ecf20Sopenharmony_ci	dm_dig->pre_cck_cca_thres = dm_dig->cur_cck_cca_thres;
4628c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_DIG, DBG_TRACE,
4638c2ecf20Sopenharmony_ci		"CCK cca thresh hold =%x\n", dm_dig->cur_cck_cca_thres);
4648c2ecf20Sopenharmony_ci}
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_cistatic void rtl88e_dm_dig(struct ieee80211_hw *hw)
4678c2ecf20Sopenharmony_ci{
4688c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
4698c2ecf20Sopenharmony_ci	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
4708c2ecf20Sopenharmony_ci	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
4718c2ecf20Sopenharmony_ci	struct dig_t *dm_dig = &rtlpriv->dm_digtable;
4728c2ecf20Sopenharmony_ci	u8 dig_dynamic_min, dig_maxofmin;
4738c2ecf20Sopenharmony_ci	bool bfirstconnect;
4748c2ecf20Sopenharmony_ci	u8 dm_dig_max, dm_dig_min;
4758c2ecf20Sopenharmony_ci	u8 current_igi = dm_dig->cur_igvalue;
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	if (rtlpriv->dm.dm_initialgain_enable == false)
4788c2ecf20Sopenharmony_ci		return;
4798c2ecf20Sopenharmony_ci	if (dm_dig->dig_enable_flag == false)
4808c2ecf20Sopenharmony_ci		return;
4818c2ecf20Sopenharmony_ci	if (mac->act_scanning == true)
4828c2ecf20Sopenharmony_ci		return;
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	if (mac->link_state >= MAC80211_LINKED)
4858c2ecf20Sopenharmony_ci		dm_dig->cur_sta_cstate = DIG_STA_CONNECT;
4868c2ecf20Sopenharmony_ci	else
4878c2ecf20Sopenharmony_ci		dm_dig->cur_sta_cstate = DIG_STA_DISCONNECT;
4888c2ecf20Sopenharmony_ci	if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP ||
4898c2ecf20Sopenharmony_ci	    rtlpriv->mac80211.opmode == NL80211_IFTYPE_ADHOC)
4908c2ecf20Sopenharmony_ci		dm_dig->cur_sta_cstate = DIG_STA_DISCONNECT;
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	dm_dig_max = DM_DIG_MAX;
4938c2ecf20Sopenharmony_ci	dm_dig_min = DM_DIG_MIN;
4948c2ecf20Sopenharmony_ci	dig_maxofmin = DM_DIG_MAX_AP;
4958c2ecf20Sopenharmony_ci	dig_dynamic_min = dm_dig->dig_min_0;
4968c2ecf20Sopenharmony_ci	bfirstconnect = ((mac->link_state >= MAC80211_LINKED) ? true : false) &&
4978c2ecf20Sopenharmony_ci			 !dm_dig->media_connect_0;
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci	dm_dig->rssi_val_min =
5008c2ecf20Sopenharmony_ci		rtl88e_dm_initial_gain_min_pwdb(hw);
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	if (mac->link_state >= MAC80211_LINKED) {
5038c2ecf20Sopenharmony_ci		if ((dm_dig->rssi_val_min + 20) > dm_dig_max)
5048c2ecf20Sopenharmony_ci			dm_dig->rx_gain_max = dm_dig_max;
5058c2ecf20Sopenharmony_ci		else if ((dm_dig->rssi_val_min + 20) < dm_dig_min)
5068c2ecf20Sopenharmony_ci			dm_dig->rx_gain_max = dm_dig_min;
5078c2ecf20Sopenharmony_ci		else
5088c2ecf20Sopenharmony_ci			dm_dig->rx_gain_max = dm_dig->rssi_val_min + 20;
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci		if (rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV) {
5118c2ecf20Sopenharmony_ci			dig_dynamic_min  = dm_dig->antdiv_rssi_max;
5128c2ecf20Sopenharmony_ci		} else {
5138c2ecf20Sopenharmony_ci			if (dm_dig->rssi_val_min < dm_dig_min)
5148c2ecf20Sopenharmony_ci				dig_dynamic_min = dm_dig_min;
5158c2ecf20Sopenharmony_ci			else if (dm_dig->rssi_val_min < dig_maxofmin)
5168c2ecf20Sopenharmony_ci				dig_dynamic_min = dig_maxofmin;
5178c2ecf20Sopenharmony_ci			else
5188c2ecf20Sopenharmony_ci				dig_dynamic_min = dm_dig->rssi_val_min;
5198c2ecf20Sopenharmony_ci		}
5208c2ecf20Sopenharmony_ci	} else {
5218c2ecf20Sopenharmony_ci		dm_dig->rx_gain_max = dm_dig_max;
5228c2ecf20Sopenharmony_ci		dig_dynamic_min = dm_dig_min;
5238c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_DIG, DBG_LOUD, "no link\n");
5248c2ecf20Sopenharmony_ci	}
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	if (rtlpriv->falsealm_cnt.cnt_all > 10000) {
5278c2ecf20Sopenharmony_ci		dm_dig->large_fa_hit++;
5288c2ecf20Sopenharmony_ci		if (dm_dig->forbidden_igi < current_igi) {
5298c2ecf20Sopenharmony_ci			dm_dig->forbidden_igi = current_igi;
5308c2ecf20Sopenharmony_ci			dm_dig->large_fa_hit = 1;
5318c2ecf20Sopenharmony_ci		}
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci		if (dm_dig->large_fa_hit >= 3) {
5348c2ecf20Sopenharmony_ci			if ((dm_dig->forbidden_igi + 1) >
5358c2ecf20Sopenharmony_ci				dm_dig->rx_gain_max)
5368c2ecf20Sopenharmony_ci				dm_dig->rx_gain_min =
5378c2ecf20Sopenharmony_ci					dm_dig->rx_gain_max;
5388c2ecf20Sopenharmony_ci			else
5398c2ecf20Sopenharmony_ci				dm_dig->rx_gain_min =
5408c2ecf20Sopenharmony_ci					dm_dig->forbidden_igi + 1;
5418c2ecf20Sopenharmony_ci			dm_dig->recover_cnt = 3600;
5428c2ecf20Sopenharmony_ci		}
5438c2ecf20Sopenharmony_ci	} else {
5448c2ecf20Sopenharmony_ci		if (dm_dig->recover_cnt != 0) {
5458c2ecf20Sopenharmony_ci			dm_dig->recover_cnt--;
5468c2ecf20Sopenharmony_ci		} else {
5478c2ecf20Sopenharmony_ci			if (dm_dig->large_fa_hit == 0) {
5488c2ecf20Sopenharmony_ci				if ((dm_dig->forbidden_igi - 1) <
5498c2ecf20Sopenharmony_ci				    dig_dynamic_min) {
5508c2ecf20Sopenharmony_ci					dm_dig->forbidden_igi = dig_dynamic_min;
5518c2ecf20Sopenharmony_ci					dm_dig->rx_gain_min = dig_dynamic_min;
5528c2ecf20Sopenharmony_ci				} else {
5538c2ecf20Sopenharmony_ci					dm_dig->forbidden_igi--;
5548c2ecf20Sopenharmony_ci					dm_dig->rx_gain_min =
5558c2ecf20Sopenharmony_ci						dm_dig->forbidden_igi + 1;
5568c2ecf20Sopenharmony_ci				}
5578c2ecf20Sopenharmony_ci			} else if (dm_dig->large_fa_hit == 3) {
5588c2ecf20Sopenharmony_ci				dm_dig->large_fa_hit = 0;
5598c2ecf20Sopenharmony_ci			}
5608c2ecf20Sopenharmony_ci		}
5618c2ecf20Sopenharmony_ci	}
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	if (dm_dig->cur_sta_cstate == DIG_STA_CONNECT) {
5648c2ecf20Sopenharmony_ci		if (bfirstconnect) {
5658c2ecf20Sopenharmony_ci			current_igi = dm_dig->rssi_val_min;
5668c2ecf20Sopenharmony_ci		} else {
5678c2ecf20Sopenharmony_ci			if (rtlpriv->falsealm_cnt.cnt_all > DM_DIG_FA_TH2)
5688c2ecf20Sopenharmony_ci				current_igi += 2;
5698c2ecf20Sopenharmony_ci			else if (rtlpriv->falsealm_cnt.cnt_all > DM_DIG_FA_TH1)
5708c2ecf20Sopenharmony_ci				current_igi++;
5718c2ecf20Sopenharmony_ci			else if (rtlpriv->falsealm_cnt.cnt_all < DM_DIG_FA_TH0)
5728c2ecf20Sopenharmony_ci				current_igi--;
5738c2ecf20Sopenharmony_ci		}
5748c2ecf20Sopenharmony_ci	} else {
5758c2ecf20Sopenharmony_ci		if (rtlpriv->falsealm_cnt.cnt_all > 10000)
5768c2ecf20Sopenharmony_ci			current_igi += 2;
5778c2ecf20Sopenharmony_ci		else if (rtlpriv->falsealm_cnt.cnt_all > 8000)
5788c2ecf20Sopenharmony_ci			current_igi++;
5798c2ecf20Sopenharmony_ci		else if (rtlpriv->falsealm_cnt.cnt_all < 500)
5808c2ecf20Sopenharmony_ci			current_igi--;
5818c2ecf20Sopenharmony_ci	}
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	if (current_igi > DM_DIG_FA_UPPER)
5848c2ecf20Sopenharmony_ci		current_igi = DM_DIG_FA_UPPER;
5858c2ecf20Sopenharmony_ci	else if (current_igi < DM_DIG_FA_LOWER)
5868c2ecf20Sopenharmony_ci		current_igi = DM_DIG_FA_LOWER;
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci	if (rtlpriv->falsealm_cnt.cnt_all > 10000)
5898c2ecf20Sopenharmony_ci		current_igi = DM_DIG_FA_UPPER;
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	dm_dig->cur_igvalue = current_igi;
5928c2ecf20Sopenharmony_ci	rtl88e_dm_write_dig(hw);
5938c2ecf20Sopenharmony_ci	dm_dig->media_connect_0 =
5948c2ecf20Sopenharmony_ci		((mac->link_state >= MAC80211_LINKED) ? true : false);
5958c2ecf20Sopenharmony_ci	dm_dig->dig_min_0 = dig_dynamic_min;
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	rtl88e_dm_cck_packet_detection_thresh(hw);
5988c2ecf20Sopenharmony_ci}
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_cistatic void rtl88e_dm_init_dynamic_txpower(struct ieee80211_hw *hw)
6018c2ecf20Sopenharmony_ci{
6028c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	rtlpriv->dm.dynamic_txpower_enable = false;
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	rtlpriv->dm.last_dtp_lvl = TXHIGHPWRLEVEL_NORMAL;
6078c2ecf20Sopenharmony_ci	rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_NORMAL;
6088c2ecf20Sopenharmony_ci}
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_cistatic void rtl92c_dm_dynamic_txpower(struct ieee80211_hw *hw)
6118c2ecf20Sopenharmony_ci{
6128c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
6138c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &rtlpriv->phy;
6148c2ecf20Sopenharmony_ci	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
6158c2ecf20Sopenharmony_ci	long undec_sm_pwdb;
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	if (!rtlpriv->dm.dynamic_txpower_enable)
6188c2ecf20Sopenharmony_ci		return;
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	if (rtlpriv->dm.dm_flag & HAL_DM_HIPWR_DISABLE) {
6218c2ecf20Sopenharmony_ci		rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_NORMAL;
6228c2ecf20Sopenharmony_ci		return;
6238c2ecf20Sopenharmony_ci	}
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	if ((mac->link_state < MAC80211_LINKED) &&
6268c2ecf20Sopenharmony_ci	    (rtlpriv->dm.entry_min_undec_sm_pwdb == 0)) {
6278c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_POWER, DBG_TRACE,
6288c2ecf20Sopenharmony_ci			"Not connected to any\n");
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci		rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_NORMAL;
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci		rtlpriv->dm.last_dtp_lvl = TXHIGHPWRLEVEL_NORMAL;
6338c2ecf20Sopenharmony_ci		return;
6348c2ecf20Sopenharmony_ci	}
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	if (mac->link_state >= MAC80211_LINKED) {
6378c2ecf20Sopenharmony_ci		if (mac->opmode == NL80211_IFTYPE_ADHOC) {
6388c2ecf20Sopenharmony_ci			undec_sm_pwdb =
6398c2ecf20Sopenharmony_ci			    rtlpriv->dm.entry_min_undec_sm_pwdb;
6408c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD,
6418c2ecf20Sopenharmony_ci				"AP Client PWDB = 0x%lx\n",
6428c2ecf20Sopenharmony_ci				undec_sm_pwdb);
6438c2ecf20Sopenharmony_ci		} else {
6448c2ecf20Sopenharmony_ci			undec_sm_pwdb =
6458c2ecf20Sopenharmony_ci			    rtlpriv->dm.undec_sm_pwdb;
6468c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD,
6478c2ecf20Sopenharmony_ci				"STA Default Port PWDB = 0x%lx\n",
6488c2ecf20Sopenharmony_ci				undec_sm_pwdb);
6498c2ecf20Sopenharmony_ci		}
6508c2ecf20Sopenharmony_ci	} else {
6518c2ecf20Sopenharmony_ci		undec_sm_pwdb =
6528c2ecf20Sopenharmony_ci		    rtlpriv->dm.entry_min_undec_sm_pwdb;
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD,
6558c2ecf20Sopenharmony_ci			"AP Ext Port PWDB = 0x%lx\n",
6568c2ecf20Sopenharmony_ci			undec_sm_pwdb);
6578c2ecf20Sopenharmony_ci	}
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci	if (undec_sm_pwdb >= TX_POWER_NEAR_FIELD_THRESH_LVL2) {
6608c2ecf20Sopenharmony_ci		rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_LEVEL1;
6618c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD,
6628c2ecf20Sopenharmony_ci			"TXHIGHPWRLEVEL_LEVEL1 (TxPwr = 0x0)\n");
6638c2ecf20Sopenharmony_ci	} else if ((undec_sm_pwdb <
6648c2ecf20Sopenharmony_ci		    (TX_POWER_NEAR_FIELD_THRESH_LVL2 - 3)) &&
6658c2ecf20Sopenharmony_ci		   (undec_sm_pwdb >=
6668c2ecf20Sopenharmony_ci		    TX_POWER_NEAR_FIELD_THRESH_LVL1)) {
6678c2ecf20Sopenharmony_ci		rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_LEVEL1;
6688c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD,
6698c2ecf20Sopenharmony_ci			"TXHIGHPWRLEVEL_LEVEL1 (TxPwr = 0x10)\n");
6708c2ecf20Sopenharmony_ci	} else if (undec_sm_pwdb <
6718c2ecf20Sopenharmony_ci		   (TX_POWER_NEAR_FIELD_THRESH_LVL1 - 5)) {
6728c2ecf20Sopenharmony_ci		rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_NORMAL;
6738c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD,
6748c2ecf20Sopenharmony_ci			"TXHIGHPWRLEVEL_NORMAL\n");
6758c2ecf20Sopenharmony_ci	}
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	if ((rtlpriv->dm.dynamic_txhighpower_lvl !=
6788c2ecf20Sopenharmony_ci		rtlpriv->dm.last_dtp_lvl)) {
6798c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD,
6808c2ecf20Sopenharmony_ci			"PHY_SetTxPowerLevel8192S() Channel = %d\n",
6818c2ecf20Sopenharmony_ci			  rtlphy->current_channel);
6828c2ecf20Sopenharmony_ci		rtl88e_phy_set_txpower_level(hw, rtlphy->current_channel);
6838c2ecf20Sopenharmony_ci	}
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_ci	rtlpriv->dm.last_dtp_lvl = rtlpriv->dm.dynamic_txhighpower_lvl;
6868c2ecf20Sopenharmony_ci}
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_civoid rtl88e_dm_write_dig(struct ieee80211_hw *hw)
6898c2ecf20Sopenharmony_ci{
6908c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
6918c2ecf20Sopenharmony_ci	struct dig_t *dm_dig = &rtlpriv->dm_digtable;
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_DIG, DBG_LOUD,
6948c2ecf20Sopenharmony_ci		"cur_igvalue = 0x%x, pre_igvalue = 0x%x, backoff_val = %d\n",
6958c2ecf20Sopenharmony_ci		 dm_dig->cur_igvalue, dm_dig->pre_igvalue,
6968c2ecf20Sopenharmony_ci		 dm_dig->back_val);
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci	if (dm_dig->cur_igvalue > 0x3f)
6998c2ecf20Sopenharmony_ci		dm_dig->cur_igvalue = 0x3f;
7008c2ecf20Sopenharmony_ci	if (dm_dig->pre_igvalue != dm_dig->cur_igvalue) {
7018c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_XAAGCCORE1, 0x7f,
7028c2ecf20Sopenharmony_ci			      dm_dig->cur_igvalue);
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci		dm_dig->pre_igvalue = dm_dig->cur_igvalue;
7058c2ecf20Sopenharmony_ci	}
7068c2ecf20Sopenharmony_ci}
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_cistatic void rtl88e_dm_pwdb_monitor(struct ieee80211_hw *hw)
7098c2ecf20Sopenharmony_ci{
7108c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
7118c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
7128c2ecf20Sopenharmony_ci	struct rtl_sta_info *drv_priv;
7138c2ecf20Sopenharmony_ci	static u64 last_record_txok_cnt;
7148c2ecf20Sopenharmony_ci	static u64 last_record_rxok_cnt;
7158c2ecf20Sopenharmony_ci	long tmp_entry_max_pwdb = 0, tmp_entry_min_pwdb = 0xff;
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci	if (rtlhal->oem_id == RT_CID_819X_HP) {
7188c2ecf20Sopenharmony_ci		u64 cur_txok_cnt = 0;
7198c2ecf20Sopenharmony_ci		u64 cur_rxok_cnt = 0;
7208c2ecf20Sopenharmony_ci		cur_txok_cnt = rtlpriv->stats.txbytesunicast -
7218c2ecf20Sopenharmony_ci			last_record_txok_cnt;
7228c2ecf20Sopenharmony_ci		cur_rxok_cnt = rtlpriv->stats.rxbytesunicast -
7238c2ecf20Sopenharmony_ci			last_record_rxok_cnt;
7248c2ecf20Sopenharmony_ci		last_record_txok_cnt = cur_txok_cnt;
7258c2ecf20Sopenharmony_ci		last_record_rxok_cnt = cur_rxok_cnt;
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci		if (cur_rxok_cnt > (cur_txok_cnt * 6))
7288c2ecf20Sopenharmony_ci			rtl_write_dword(rtlpriv, REG_ARFR0, 0x8f015);
7298c2ecf20Sopenharmony_ci		else
7308c2ecf20Sopenharmony_ci			rtl_write_dword(rtlpriv, REG_ARFR0, 0xff015);
7318c2ecf20Sopenharmony_ci	}
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	/* AP & ADHOC & MESH */
7348c2ecf20Sopenharmony_ci	spin_lock_bh(&rtlpriv->locks.entry_list_lock);
7358c2ecf20Sopenharmony_ci	list_for_each_entry(drv_priv, &rtlpriv->entry_list, list) {
7368c2ecf20Sopenharmony_ci		if (drv_priv->rssi_stat.undec_sm_pwdb <
7378c2ecf20Sopenharmony_ci			tmp_entry_min_pwdb)
7388c2ecf20Sopenharmony_ci			tmp_entry_min_pwdb = drv_priv->rssi_stat.undec_sm_pwdb;
7398c2ecf20Sopenharmony_ci		if (drv_priv->rssi_stat.undec_sm_pwdb >
7408c2ecf20Sopenharmony_ci			tmp_entry_max_pwdb)
7418c2ecf20Sopenharmony_ci			tmp_entry_max_pwdb = drv_priv->rssi_stat.undec_sm_pwdb;
7428c2ecf20Sopenharmony_ci	}
7438c2ecf20Sopenharmony_ci	spin_unlock_bh(&rtlpriv->locks.entry_list_lock);
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci	/* If associated entry is found */
7468c2ecf20Sopenharmony_ci	if (tmp_entry_max_pwdb != 0) {
7478c2ecf20Sopenharmony_ci		rtlpriv->dm.entry_max_undec_sm_pwdb = tmp_entry_max_pwdb;
7488c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FDM, DM_PWDB, "EntryMaxPWDB = 0x%lx(%ld)\n",
7498c2ecf20Sopenharmony_ci			tmp_entry_max_pwdb, tmp_entry_max_pwdb);
7508c2ecf20Sopenharmony_ci	} else {
7518c2ecf20Sopenharmony_ci		rtlpriv->dm.entry_max_undec_sm_pwdb = 0;
7528c2ecf20Sopenharmony_ci	}
7538c2ecf20Sopenharmony_ci	/* If associated entry is found */
7548c2ecf20Sopenharmony_ci	if (tmp_entry_min_pwdb != 0xff) {
7558c2ecf20Sopenharmony_ci		rtlpriv->dm.entry_min_undec_sm_pwdb = tmp_entry_min_pwdb;
7568c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FDM, DM_PWDB, "EntryMinPWDB = 0x%lx(%ld)\n",
7578c2ecf20Sopenharmony_ci					tmp_entry_min_pwdb, tmp_entry_min_pwdb);
7588c2ecf20Sopenharmony_ci	} else {
7598c2ecf20Sopenharmony_ci		rtlpriv->dm.entry_min_undec_sm_pwdb = 0;
7608c2ecf20Sopenharmony_ci	}
7618c2ecf20Sopenharmony_ci	/* Indicate Rx signal strength to FW. */
7628c2ecf20Sopenharmony_ci	if (!rtlpriv->dm.useramask)
7638c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, 0x4fe, rtlpriv->dm.undec_sm_pwdb);
7648c2ecf20Sopenharmony_ci}
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_civoid rtl88e_dm_init_edca_turbo(struct ieee80211_hw *hw)
7678c2ecf20Sopenharmony_ci{
7688c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci	rtlpriv->dm.current_turbo_edca = false;
7718c2ecf20Sopenharmony_ci	rtlpriv->dm.is_any_nonbepkts = false;
7728c2ecf20Sopenharmony_ci	rtlpriv->dm.is_cur_rdlstate = false;
7738c2ecf20Sopenharmony_ci}
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_cistatic void rtl88e_dm_check_edca_turbo(struct ieee80211_hw *hw)
7768c2ecf20Sopenharmony_ci{
7778c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
7788c2ecf20Sopenharmony_ci	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
7798c2ecf20Sopenharmony_ci	static u64 last_txok_cnt;
7808c2ecf20Sopenharmony_ci	static u64 last_rxok_cnt;
7818c2ecf20Sopenharmony_ci	static u32 last_bt_edca_ul;
7828c2ecf20Sopenharmony_ci	static u32 last_bt_edca_dl;
7838c2ecf20Sopenharmony_ci	u64 cur_txok_cnt = 0;
7848c2ecf20Sopenharmony_ci	u64 cur_rxok_cnt = 0;
7858c2ecf20Sopenharmony_ci	u32 edca_be_ul = 0x5ea42b;
7868c2ecf20Sopenharmony_ci	u32 edca_be_dl = 0x5ea42b;
7878c2ecf20Sopenharmony_ci	bool bt_change_edca = false;
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	if ((last_bt_edca_ul != rtlpriv->btcoexist.bt_edca_ul) ||
7908c2ecf20Sopenharmony_ci	    (last_bt_edca_dl != rtlpriv->btcoexist.bt_edca_dl)) {
7918c2ecf20Sopenharmony_ci		rtlpriv->dm.current_turbo_edca = false;
7928c2ecf20Sopenharmony_ci		last_bt_edca_ul = rtlpriv->btcoexist.bt_edca_ul;
7938c2ecf20Sopenharmony_ci		last_bt_edca_dl = rtlpriv->btcoexist.bt_edca_dl;
7948c2ecf20Sopenharmony_ci	}
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci	if (rtlpriv->btcoexist.bt_edca_ul != 0) {
7978c2ecf20Sopenharmony_ci		edca_be_ul = rtlpriv->btcoexist.bt_edca_ul;
7988c2ecf20Sopenharmony_ci		bt_change_edca = true;
7998c2ecf20Sopenharmony_ci	}
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	if (rtlpriv->btcoexist.bt_edca_dl != 0) {
8028c2ecf20Sopenharmony_ci		edca_be_dl = rtlpriv->btcoexist.bt_edca_dl;
8038c2ecf20Sopenharmony_ci		bt_change_edca = true;
8048c2ecf20Sopenharmony_ci	}
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	if (mac->link_state != MAC80211_LINKED) {
8078c2ecf20Sopenharmony_ci		rtlpriv->dm.current_turbo_edca = false;
8088c2ecf20Sopenharmony_ci		return;
8098c2ecf20Sopenharmony_ci	}
8108c2ecf20Sopenharmony_ci	if ((bt_change_edca) ||
8118c2ecf20Sopenharmony_ci	    ((!rtlpriv->dm.is_any_nonbepkts) &&
8128c2ecf20Sopenharmony_ci	     (!rtlpriv->dm.disable_framebursting))) {
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci		cur_txok_cnt = rtlpriv->stats.txbytesunicast - last_txok_cnt;
8158c2ecf20Sopenharmony_ci		cur_rxok_cnt = rtlpriv->stats.rxbytesunicast - last_rxok_cnt;
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci		if (cur_rxok_cnt > 4 * cur_txok_cnt) {
8188c2ecf20Sopenharmony_ci			if (!rtlpriv->dm.is_cur_rdlstate ||
8198c2ecf20Sopenharmony_ci			    !rtlpriv->dm.current_turbo_edca) {
8208c2ecf20Sopenharmony_ci				rtl_write_dword(rtlpriv,
8218c2ecf20Sopenharmony_ci						REG_EDCA_BE_PARAM,
8228c2ecf20Sopenharmony_ci						edca_be_dl);
8238c2ecf20Sopenharmony_ci				rtlpriv->dm.is_cur_rdlstate = true;
8248c2ecf20Sopenharmony_ci			}
8258c2ecf20Sopenharmony_ci		} else {
8268c2ecf20Sopenharmony_ci			if (rtlpriv->dm.is_cur_rdlstate ||
8278c2ecf20Sopenharmony_ci			    !rtlpriv->dm.current_turbo_edca) {
8288c2ecf20Sopenharmony_ci				rtl_write_dword(rtlpriv,
8298c2ecf20Sopenharmony_ci						REG_EDCA_BE_PARAM,
8308c2ecf20Sopenharmony_ci						edca_be_ul);
8318c2ecf20Sopenharmony_ci				rtlpriv->dm.is_cur_rdlstate = false;
8328c2ecf20Sopenharmony_ci			}
8338c2ecf20Sopenharmony_ci		}
8348c2ecf20Sopenharmony_ci		rtlpriv->dm.current_turbo_edca = true;
8358c2ecf20Sopenharmony_ci	} else {
8368c2ecf20Sopenharmony_ci		if (rtlpriv->dm.current_turbo_edca) {
8378c2ecf20Sopenharmony_ci			u8 tmp = AC0_BE;
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci			rtlpriv->cfg->ops->set_hw_reg(hw,
8408c2ecf20Sopenharmony_ci						      HW_VAR_AC_PARAM,
8418c2ecf20Sopenharmony_ci						      &tmp);
8428c2ecf20Sopenharmony_ci			rtlpriv->dm.current_turbo_edca = false;
8438c2ecf20Sopenharmony_ci		}
8448c2ecf20Sopenharmony_ci	}
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci	rtlpriv->dm.is_any_nonbepkts = false;
8478c2ecf20Sopenharmony_ci	last_txok_cnt = rtlpriv->stats.txbytesunicast;
8488c2ecf20Sopenharmony_ci	last_rxok_cnt = rtlpriv->stats.rxbytesunicast;
8498c2ecf20Sopenharmony_ci}
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_cistatic void dm_txpower_track_cb_therm(struct ieee80211_hw *hw)
8528c2ecf20Sopenharmony_ci{
8538c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
8548c2ecf20Sopenharmony_ci	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
8558c2ecf20Sopenharmony_ci	struct rtl_dm	*rtldm = rtl_dm(rtl_priv(hw));
8568c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
8578c2ecf20Sopenharmony_ci	u8 thermalvalue = 0, delta, delta_lck, delta_iqk, offset;
8588c2ecf20Sopenharmony_ci	u8 thermalvalue_avg_count = 0;
8598c2ecf20Sopenharmony_ci	u32 thermalvalue_avg = 0;
8608c2ecf20Sopenharmony_ci	long  ele_d, temp_cck;
8618c2ecf20Sopenharmony_ci	s8 ofdm_index[2], cck_index = 0,
8628c2ecf20Sopenharmony_ci		ofdm_index_old[2] = {0, 0}, cck_index_old = 0;
8638c2ecf20Sopenharmony_ci	int i = 0;
8648c2ecf20Sopenharmony_ci	/*bool is2t = false;*/
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_ci	u8 ofdm_min_index = 6, rf = 1;
8678c2ecf20Sopenharmony_ci	/*u8 index_for_channel;*/
8688c2ecf20Sopenharmony_ci	enum _power_dec_inc {power_dec, power_inc};
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_ci	/*0.1 the following TWO tables decide the
8718c2ecf20Sopenharmony_ci	 *final index of OFDM/CCK swing table
8728c2ecf20Sopenharmony_ci	 */
8738c2ecf20Sopenharmony_ci	static const s8 delta_swing_table_idx[2][15]  = {
8748c2ecf20Sopenharmony_ci		{0, 0, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9, 10, 10, 11},
8758c2ecf20Sopenharmony_ci		{0, 0, -1, -2, -3, -4, -4, -4, -4, -5, -7, -8, -9, -9, -10}
8768c2ecf20Sopenharmony_ci	};
8778c2ecf20Sopenharmony_ci	static const u8 thermal_threshold[2][15] = {
8788c2ecf20Sopenharmony_ci		{0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 27},
8798c2ecf20Sopenharmony_ci		{0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 25, 25, 25}
8808c2ecf20Sopenharmony_ci	};
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	/*Initilization (7 steps in total) */
8838c2ecf20Sopenharmony_ci	rtlpriv->dm.txpower_trackinginit = true;
8848c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
8858c2ecf20Sopenharmony_ci		"%s\n", __func__);
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_ci	thermalvalue = (u8)rtl_get_rfreg(hw, RF90_PATH_A, RF_T_METER,
8888c2ecf20Sopenharmony_ci					 0xfc00);
8898c2ecf20Sopenharmony_ci	if (!thermalvalue)
8908c2ecf20Sopenharmony_ci		return;
8918c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
8928c2ecf20Sopenharmony_ci		"Readback Thermal Meter = 0x%x pre thermal meter 0x%x eeprom_thermalmeter 0x%x\n",
8938c2ecf20Sopenharmony_ci		thermalvalue, rtlpriv->dm.thermalvalue,
8948c2ecf20Sopenharmony_ci		rtlefuse->eeprom_thermalmeter);
8958c2ecf20Sopenharmony_ci
8968c2ecf20Sopenharmony_ci	/*1. Query OFDM Default Setting: Path A*/
8978c2ecf20Sopenharmony_ci	ele_d = rtl_get_bbreg(hw, ROFDM0_XATXIQIMBALANCE, MASKDWORD) &
8988c2ecf20Sopenharmony_ci			      MASKOFDM_D;
8998c2ecf20Sopenharmony_ci	for (i = 0; i < OFDM_TABLE_LENGTH; i++) {
9008c2ecf20Sopenharmony_ci		if (ele_d == (ofdmswing_table[i] & MASKOFDM_D)) {
9018c2ecf20Sopenharmony_ci			ofdm_index_old[0] = (u8)i;
9028c2ecf20Sopenharmony_ci			rtldm->swing_idx_ofdm_base[RF90_PATH_A] = (u8)i;
9038c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
9048c2ecf20Sopenharmony_ci				"Initial pathA ele_d reg0x%x = 0x%lx, ofdm_index = 0x%x\n",
9058c2ecf20Sopenharmony_ci				 ROFDM0_XATXIQIMBALANCE,
9068c2ecf20Sopenharmony_ci				 ele_d, ofdm_index_old[0]);
9078c2ecf20Sopenharmony_ci			break;
9088c2ecf20Sopenharmony_ci		}
9098c2ecf20Sopenharmony_ci	}
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci	/*2.Query CCK default setting From 0xa24*/
9128c2ecf20Sopenharmony_ci	temp_cck = rtl_get_bbreg(hw, RCCK0_TXFILTER2, MASKDWORD) & MASKCCK;
9138c2ecf20Sopenharmony_ci	for (i = 0; i < CCK_TABLE_LENGTH; i++) {
9148c2ecf20Sopenharmony_ci		if (rtlpriv->dm.cck_inch14) {
9158c2ecf20Sopenharmony_ci			if (memcmp(&temp_cck, &cck_tbl_ch14[i][2], 4) == 0) {
9168c2ecf20Sopenharmony_ci				cck_index_old = (u8)i;
9178c2ecf20Sopenharmony_ci				rtldm->swing_idx_cck_base = (u8)i;
9188c2ecf20Sopenharmony_ci				rtl_dbg(rtlpriv, COMP_POWER_TRACKING,
9198c2ecf20Sopenharmony_ci					DBG_LOUD,
9208c2ecf20Sopenharmony_ci					"Initial reg0x%x = 0x%lx, cck_index = 0x%x, ch 14 %d\n",
9218c2ecf20Sopenharmony_ci					RCCK0_TXFILTER2, temp_cck,
9228c2ecf20Sopenharmony_ci					cck_index_old,
9238c2ecf20Sopenharmony_ci					rtlpriv->dm.cck_inch14);
9248c2ecf20Sopenharmony_ci				break;
9258c2ecf20Sopenharmony_ci			}
9268c2ecf20Sopenharmony_ci		} else {
9278c2ecf20Sopenharmony_ci			if (memcmp(&temp_cck, &cck_tbl_ch1_13[i][2], 4) == 0) {
9288c2ecf20Sopenharmony_ci				cck_index_old = (u8)i;
9298c2ecf20Sopenharmony_ci				rtldm->swing_idx_cck_base = (u8)i;
9308c2ecf20Sopenharmony_ci				rtl_dbg(rtlpriv, COMP_POWER_TRACKING,
9318c2ecf20Sopenharmony_ci					DBG_LOUD,
9328c2ecf20Sopenharmony_ci					"Initial reg0x%x = 0x%lx, cck_index = 0x%x, ch14 %d\n",
9338c2ecf20Sopenharmony_ci					RCCK0_TXFILTER2, temp_cck,
9348c2ecf20Sopenharmony_ci					cck_index_old,
9358c2ecf20Sopenharmony_ci					rtlpriv->dm.cck_inch14);
9368c2ecf20Sopenharmony_ci				break;
9378c2ecf20Sopenharmony_ci			}
9388c2ecf20Sopenharmony_ci		}
9398c2ecf20Sopenharmony_ci	}
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_ci	/*3 Initialize ThermalValues of RFCalibrateInfo*/
9428c2ecf20Sopenharmony_ci	if (!rtldm->thermalvalue) {
9438c2ecf20Sopenharmony_ci		rtlpriv->dm.thermalvalue = rtlefuse->eeprom_thermalmeter;
9448c2ecf20Sopenharmony_ci		rtlpriv->dm.thermalvalue_lck = thermalvalue;
9458c2ecf20Sopenharmony_ci		rtlpriv->dm.thermalvalue_iqk = thermalvalue;
9468c2ecf20Sopenharmony_ci		for (i = 0; i < rf; i++)
9478c2ecf20Sopenharmony_ci			rtlpriv->dm.ofdm_index[i] = ofdm_index_old[i];
9488c2ecf20Sopenharmony_ci		rtlpriv->dm.cck_index = cck_index_old;
9498c2ecf20Sopenharmony_ci	}
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_ci	/*4 Calculate average thermal meter*/
9528c2ecf20Sopenharmony_ci	rtldm->thermalvalue_avg[rtldm->thermalvalue_avg_index] = thermalvalue;
9538c2ecf20Sopenharmony_ci	rtldm->thermalvalue_avg_index++;
9548c2ecf20Sopenharmony_ci	if (rtldm->thermalvalue_avg_index == AVG_THERMAL_NUM_88E)
9558c2ecf20Sopenharmony_ci		rtldm->thermalvalue_avg_index = 0;
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_ci	for (i = 0; i < AVG_THERMAL_NUM_88E; i++) {
9588c2ecf20Sopenharmony_ci		if (rtldm->thermalvalue_avg[i]) {
9598c2ecf20Sopenharmony_ci			thermalvalue_avg += rtldm->thermalvalue_avg[i];
9608c2ecf20Sopenharmony_ci			thermalvalue_avg_count++;
9618c2ecf20Sopenharmony_ci		}
9628c2ecf20Sopenharmony_ci	}
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci	if (thermalvalue_avg_count)
9658c2ecf20Sopenharmony_ci		thermalvalue = (u8)(thermalvalue_avg / thermalvalue_avg_count);
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_ci	/* 5 Calculate delta, delta_LCK, delta_IQK.*/
9688c2ecf20Sopenharmony_ci	if (rtlhal->reloadtxpowerindex) {
9698c2ecf20Sopenharmony_ci		delta = (thermalvalue > rtlefuse->eeprom_thermalmeter) ?
9708c2ecf20Sopenharmony_ci		    (thermalvalue - rtlefuse->eeprom_thermalmeter) :
9718c2ecf20Sopenharmony_ci		    (rtlefuse->eeprom_thermalmeter - thermalvalue);
9728c2ecf20Sopenharmony_ci		rtlhal->reloadtxpowerindex = false;
9738c2ecf20Sopenharmony_ci		rtlpriv->dm.done_txpower = false;
9748c2ecf20Sopenharmony_ci	} else if (rtlpriv->dm.done_txpower) {
9758c2ecf20Sopenharmony_ci		delta = (thermalvalue > rtlpriv->dm.thermalvalue) ?
9768c2ecf20Sopenharmony_ci		    (thermalvalue - rtlpriv->dm.thermalvalue) :
9778c2ecf20Sopenharmony_ci		    (rtlpriv->dm.thermalvalue - thermalvalue);
9788c2ecf20Sopenharmony_ci	} else {
9798c2ecf20Sopenharmony_ci		delta = (thermalvalue > rtlefuse->eeprom_thermalmeter) ?
9808c2ecf20Sopenharmony_ci		    (thermalvalue - rtlefuse->eeprom_thermalmeter) :
9818c2ecf20Sopenharmony_ci		    (rtlefuse->eeprom_thermalmeter - thermalvalue);
9828c2ecf20Sopenharmony_ci	}
9838c2ecf20Sopenharmony_ci	delta_lck = (thermalvalue > rtlpriv->dm.thermalvalue_lck) ?
9848c2ecf20Sopenharmony_ci	    (thermalvalue - rtlpriv->dm.thermalvalue_lck) :
9858c2ecf20Sopenharmony_ci	    (rtlpriv->dm.thermalvalue_lck - thermalvalue);
9868c2ecf20Sopenharmony_ci	delta_iqk = (thermalvalue > rtlpriv->dm.thermalvalue_iqk) ?
9878c2ecf20Sopenharmony_ci	    (thermalvalue - rtlpriv->dm.thermalvalue_iqk) :
9888c2ecf20Sopenharmony_ci	    (rtlpriv->dm.thermalvalue_iqk - thermalvalue);
9898c2ecf20Sopenharmony_ci
9908c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
9918c2ecf20Sopenharmony_ci		"Readback Thermal Meter = 0x%x pre thermal meter 0x%x eeprom_thermalmeter 0x%x delta 0x%x delta_lck 0x%x delta_iqk 0x%x\n",
9928c2ecf20Sopenharmony_ci		thermalvalue, rtlpriv->dm.thermalvalue,
9938c2ecf20Sopenharmony_ci		rtlefuse->eeprom_thermalmeter, delta, delta_lck,
9948c2ecf20Sopenharmony_ci		delta_iqk);
9958c2ecf20Sopenharmony_ci	/* 6 If necessary, do LCK.*/
9968c2ecf20Sopenharmony_ci	if (delta_lck >= 8) {
9978c2ecf20Sopenharmony_ci		rtlpriv->dm.thermalvalue_lck = thermalvalue;
9988c2ecf20Sopenharmony_ci		rtl88e_phy_lc_calibrate(hw);
9998c2ecf20Sopenharmony_ci	}
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_ci	/* 7 If necessary, move the index of
10028c2ecf20Sopenharmony_ci	 * swing table to adjust Tx power.
10038c2ecf20Sopenharmony_ci	 */
10048c2ecf20Sopenharmony_ci	if (delta > 0 && rtlpriv->dm.txpower_track_control) {
10058c2ecf20Sopenharmony_ci		delta = (thermalvalue > rtlefuse->eeprom_thermalmeter) ?
10068c2ecf20Sopenharmony_ci		    (thermalvalue - rtlefuse->eeprom_thermalmeter) :
10078c2ecf20Sopenharmony_ci		    (rtlefuse->eeprom_thermalmeter - thermalvalue);
10088c2ecf20Sopenharmony_ci
10098c2ecf20Sopenharmony_ci		/* 7.1 Get the final CCK_index and OFDM_index for each
10108c2ecf20Sopenharmony_ci		 * swing table.
10118c2ecf20Sopenharmony_ci		 */
10128c2ecf20Sopenharmony_ci		if (thermalvalue > rtlefuse->eeprom_thermalmeter) {
10138c2ecf20Sopenharmony_ci			CAL_SWING_OFF(offset, power_inc, INDEX_MAPPING_NUM,
10148c2ecf20Sopenharmony_ci				      delta);
10158c2ecf20Sopenharmony_ci			for (i = 0; i < rf; i++)
10168c2ecf20Sopenharmony_ci				ofdm_index[i] =
10178c2ecf20Sopenharmony_ci				  rtldm->ofdm_index[i] +
10188c2ecf20Sopenharmony_ci				  delta_swing_table_idx[power_inc][offset];
10198c2ecf20Sopenharmony_ci			cck_index = rtldm->cck_index +
10208c2ecf20Sopenharmony_ci				delta_swing_table_idx[power_inc][offset];
10218c2ecf20Sopenharmony_ci		} else {
10228c2ecf20Sopenharmony_ci			CAL_SWING_OFF(offset, power_dec, INDEX_MAPPING_NUM,
10238c2ecf20Sopenharmony_ci				      delta);
10248c2ecf20Sopenharmony_ci			for (i = 0; i < rf; i++)
10258c2ecf20Sopenharmony_ci				ofdm_index[i] =
10268c2ecf20Sopenharmony_ci				  rtldm->ofdm_index[i] +
10278c2ecf20Sopenharmony_ci				  delta_swing_table_idx[power_dec][offset];
10288c2ecf20Sopenharmony_ci			cck_index = rtldm->cck_index +
10298c2ecf20Sopenharmony_ci				delta_swing_table_idx[power_dec][offset];
10308c2ecf20Sopenharmony_ci		}
10318c2ecf20Sopenharmony_ci
10328c2ecf20Sopenharmony_ci		/* 7.2 Handle boundary conditions of index.*/
10338c2ecf20Sopenharmony_ci		for (i = 0; i < rf; i++) {
10348c2ecf20Sopenharmony_ci			if (ofdm_index[i] > OFDM_TABLE_SIZE-1)
10358c2ecf20Sopenharmony_ci				ofdm_index[i] = OFDM_TABLE_SIZE-1;
10368c2ecf20Sopenharmony_ci			else if (rtldm->ofdm_index[i] < ofdm_min_index)
10378c2ecf20Sopenharmony_ci				ofdm_index[i] = ofdm_min_index;
10388c2ecf20Sopenharmony_ci		}
10398c2ecf20Sopenharmony_ci
10408c2ecf20Sopenharmony_ci		if (cck_index > CCK_TABLE_SIZE-1)
10418c2ecf20Sopenharmony_ci			cck_index = CCK_TABLE_SIZE-1;
10428c2ecf20Sopenharmony_ci		else if (cck_index < 0)
10438c2ecf20Sopenharmony_ci			cck_index = 0;
10448c2ecf20Sopenharmony_ci
10458c2ecf20Sopenharmony_ci		/*7.3Configure the Swing Table to adjust Tx Power.*/
10468c2ecf20Sopenharmony_ci		if (rtlpriv->dm.txpower_track_control) {
10478c2ecf20Sopenharmony_ci			rtldm->done_txpower = true;
10488c2ecf20Sopenharmony_ci			rtldm->swing_idx_ofdm[RF90_PATH_A] =
10498c2ecf20Sopenharmony_ci				(u8)ofdm_index[RF90_PATH_A];
10508c2ecf20Sopenharmony_ci			rtldm->swing_idx_cck = cck_index;
10518c2ecf20Sopenharmony_ci			if (rtldm->swing_idx_ofdm_cur !=
10528c2ecf20Sopenharmony_ci			    rtldm->swing_idx_ofdm[0]) {
10538c2ecf20Sopenharmony_ci				rtldm->swing_idx_ofdm_cur =
10548c2ecf20Sopenharmony_ci					 rtldm->swing_idx_ofdm[0];
10558c2ecf20Sopenharmony_ci				rtldm->swing_flag_ofdm = true;
10568c2ecf20Sopenharmony_ci			}
10578c2ecf20Sopenharmony_ci
10588c2ecf20Sopenharmony_ci			if (rtldm->swing_idx_cck_cur != rtldm->swing_idx_cck) {
10598c2ecf20Sopenharmony_ci				rtldm->swing_idx_cck_cur = rtldm->swing_idx_cck;
10608c2ecf20Sopenharmony_ci				rtldm->swing_flag_cck = true;
10618c2ecf20Sopenharmony_ci			}
10628c2ecf20Sopenharmony_ci
10638c2ecf20Sopenharmony_ci			dm_tx_pwr_track_set_pwr(hw, TXAGC, 0, 0);
10648c2ecf20Sopenharmony_ci		}
10658c2ecf20Sopenharmony_ci	}
10668c2ecf20Sopenharmony_ci
10678c2ecf20Sopenharmony_ci	if (delta_iqk >= 8) {
10688c2ecf20Sopenharmony_ci		rtlpriv->dm.thermalvalue_iqk = thermalvalue;
10698c2ecf20Sopenharmony_ci		rtl88e_phy_iq_calibrate(hw, false);
10708c2ecf20Sopenharmony_ci	}
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_ci	if (rtldm->txpower_track_control)
10738c2ecf20Sopenharmony_ci		rtldm->thermalvalue = thermalvalue;
10748c2ecf20Sopenharmony_ci	rtldm->txpowercount = 0;
10758c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD, "end\n");
10768c2ecf20Sopenharmony_ci}
10778c2ecf20Sopenharmony_ci
10788c2ecf20Sopenharmony_cistatic void rtl88e_dm_init_txpower_tracking(struct ieee80211_hw *hw)
10798c2ecf20Sopenharmony_ci{
10808c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_ci	rtlpriv->dm.txpower_tracking = true;
10838c2ecf20Sopenharmony_ci	rtlpriv->dm.txpower_trackinginit = false;
10848c2ecf20Sopenharmony_ci	rtlpriv->dm.txpowercount = 0;
10858c2ecf20Sopenharmony_ci	rtlpriv->dm.txpower_track_control = true;
10868c2ecf20Sopenharmony_ci
10878c2ecf20Sopenharmony_ci	rtlpriv->dm.swing_idx_ofdm[RF90_PATH_A] = 12;
10888c2ecf20Sopenharmony_ci	rtlpriv->dm.swing_idx_ofdm_cur = 12;
10898c2ecf20Sopenharmony_ci	rtlpriv->dm.swing_flag_ofdm = false;
10908c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
10918c2ecf20Sopenharmony_ci		"rtlpriv->dm.txpower_tracking = %d\n",
10928c2ecf20Sopenharmony_ci		rtlpriv->dm.txpower_tracking);
10938c2ecf20Sopenharmony_ci}
10948c2ecf20Sopenharmony_ci
10958c2ecf20Sopenharmony_civoid rtl88e_dm_check_txpower_tracking(struct ieee80211_hw *hw)
10968c2ecf20Sopenharmony_ci{
10978c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
10988c2ecf20Sopenharmony_ci
10998c2ecf20Sopenharmony_ci	if (!rtlpriv->dm.txpower_tracking)
11008c2ecf20Sopenharmony_ci		return;
11018c2ecf20Sopenharmony_ci
11028c2ecf20Sopenharmony_ci	if (!rtlpriv->dm.tm_trigger) {
11038c2ecf20Sopenharmony_ci		rtl_set_rfreg(hw, RF90_PATH_A, RF_T_METER, BIT(17)|BIT(16),
11048c2ecf20Sopenharmony_ci			      0x03);
11058c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
11068c2ecf20Sopenharmony_ci			"Trigger 88E Thermal Meter!!\n");
11078c2ecf20Sopenharmony_ci		rtlpriv->dm.tm_trigger = 1;
11088c2ecf20Sopenharmony_ci		return;
11098c2ecf20Sopenharmony_ci	} else {
11108c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
11118c2ecf20Sopenharmony_ci			"Schedule TxPowerTracking !!\n");
11128c2ecf20Sopenharmony_ci		dm_txpower_track_cb_therm(hw);
11138c2ecf20Sopenharmony_ci		rtlpriv->dm.tm_trigger = 0;
11148c2ecf20Sopenharmony_ci	}
11158c2ecf20Sopenharmony_ci}
11168c2ecf20Sopenharmony_ci
11178c2ecf20Sopenharmony_civoid rtl88e_dm_init_rate_adaptive_mask(struct ieee80211_hw *hw)
11188c2ecf20Sopenharmony_ci{
11198c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
11208c2ecf20Sopenharmony_ci	struct rate_adaptive *p_ra = &rtlpriv->ra;
11218c2ecf20Sopenharmony_ci
11228c2ecf20Sopenharmony_ci	p_ra->ratr_state = DM_RATR_STA_INIT;
11238c2ecf20Sopenharmony_ci	p_ra->pre_ratr_state = DM_RATR_STA_INIT;
11248c2ecf20Sopenharmony_ci
11258c2ecf20Sopenharmony_ci	if (rtlpriv->dm.dm_type == DM_TYPE_BYDRIVER)
11268c2ecf20Sopenharmony_ci		rtlpriv->dm.useramask = true;
11278c2ecf20Sopenharmony_ci	else
11288c2ecf20Sopenharmony_ci		rtlpriv->dm.useramask = false;
11298c2ecf20Sopenharmony_ci}
11308c2ecf20Sopenharmony_ci
11318c2ecf20Sopenharmony_cistatic void rtl88e_dm_refresh_rate_adaptive_mask(struct ieee80211_hw *hw)
11328c2ecf20Sopenharmony_ci{
11338c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
11348c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
11358c2ecf20Sopenharmony_ci	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
11368c2ecf20Sopenharmony_ci	struct rate_adaptive *p_ra = &rtlpriv->ra;
11378c2ecf20Sopenharmony_ci	u32 low_rssithresh_for_ra, high_rssithresh_for_ra;
11388c2ecf20Sopenharmony_ci	struct ieee80211_sta *sta = NULL;
11398c2ecf20Sopenharmony_ci
11408c2ecf20Sopenharmony_ci	if (is_hal_stop(rtlhal)) {
11418c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_RATE, DBG_LOUD,
11428c2ecf20Sopenharmony_ci			"driver is going to unload\n");
11438c2ecf20Sopenharmony_ci		return;
11448c2ecf20Sopenharmony_ci	}
11458c2ecf20Sopenharmony_ci
11468c2ecf20Sopenharmony_ci	if (!rtlpriv->dm.useramask) {
11478c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_RATE, DBG_LOUD,
11488c2ecf20Sopenharmony_ci			"driver does not control rate adaptive mask\n");
11498c2ecf20Sopenharmony_ci		return;
11508c2ecf20Sopenharmony_ci	}
11518c2ecf20Sopenharmony_ci
11528c2ecf20Sopenharmony_ci	if (mac->link_state == MAC80211_LINKED &&
11538c2ecf20Sopenharmony_ci	    mac->opmode == NL80211_IFTYPE_STATION) {
11548c2ecf20Sopenharmony_ci		switch (p_ra->pre_ratr_state) {
11558c2ecf20Sopenharmony_ci		case DM_RATR_STA_HIGH:
11568c2ecf20Sopenharmony_ci			high_rssithresh_for_ra = 50;
11578c2ecf20Sopenharmony_ci			low_rssithresh_for_ra = 20;
11588c2ecf20Sopenharmony_ci			break;
11598c2ecf20Sopenharmony_ci		case DM_RATR_STA_MIDDLE:
11608c2ecf20Sopenharmony_ci			high_rssithresh_for_ra = 55;
11618c2ecf20Sopenharmony_ci			low_rssithresh_for_ra = 20;
11628c2ecf20Sopenharmony_ci			break;
11638c2ecf20Sopenharmony_ci		case DM_RATR_STA_LOW:
11648c2ecf20Sopenharmony_ci			high_rssithresh_for_ra = 50;
11658c2ecf20Sopenharmony_ci			low_rssithresh_for_ra = 25;
11668c2ecf20Sopenharmony_ci			break;
11678c2ecf20Sopenharmony_ci		default:
11688c2ecf20Sopenharmony_ci			high_rssithresh_for_ra = 50;
11698c2ecf20Sopenharmony_ci			low_rssithresh_for_ra = 20;
11708c2ecf20Sopenharmony_ci			break;
11718c2ecf20Sopenharmony_ci		}
11728c2ecf20Sopenharmony_ci
11738c2ecf20Sopenharmony_ci		if (rtlpriv->dm.undec_sm_pwdb >
11748c2ecf20Sopenharmony_ci		    (long)high_rssithresh_for_ra)
11758c2ecf20Sopenharmony_ci			p_ra->ratr_state = DM_RATR_STA_HIGH;
11768c2ecf20Sopenharmony_ci		else if (rtlpriv->dm.undec_sm_pwdb >
11778c2ecf20Sopenharmony_ci			 (long)low_rssithresh_for_ra)
11788c2ecf20Sopenharmony_ci			p_ra->ratr_state = DM_RATR_STA_MIDDLE;
11798c2ecf20Sopenharmony_ci		else
11808c2ecf20Sopenharmony_ci			p_ra->ratr_state = DM_RATR_STA_LOW;
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_ci		if (p_ra->pre_ratr_state != p_ra->ratr_state) {
11838c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_RATE, DBG_LOUD,
11848c2ecf20Sopenharmony_ci				"RSSI = %ld\n",
11858c2ecf20Sopenharmony_ci				rtlpriv->dm.undec_sm_pwdb);
11868c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_RATE, DBG_LOUD,
11878c2ecf20Sopenharmony_ci				"RSSI_LEVEL = %d\n", p_ra->ratr_state);
11888c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_RATE, DBG_LOUD,
11898c2ecf20Sopenharmony_ci				"PreState = %d, CurState = %d\n",
11908c2ecf20Sopenharmony_ci				p_ra->pre_ratr_state, p_ra->ratr_state);
11918c2ecf20Sopenharmony_ci
11928c2ecf20Sopenharmony_ci			rcu_read_lock();
11938c2ecf20Sopenharmony_ci			sta = rtl_find_sta(hw, mac->bssid);
11948c2ecf20Sopenharmony_ci			if (sta)
11958c2ecf20Sopenharmony_ci				rtlpriv->cfg->ops->update_rate_tbl(hw, sta,
11968c2ecf20Sopenharmony_ci							p_ra->ratr_state,
11978c2ecf20Sopenharmony_ci								   true);
11988c2ecf20Sopenharmony_ci			rcu_read_unlock();
11998c2ecf20Sopenharmony_ci
12008c2ecf20Sopenharmony_ci			p_ra->pre_ratr_state = p_ra->ratr_state;
12018c2ecf20Sopenharmony_ci		}
12028c2ecf20Sopenharmony_ci	}
12038c2ecf20Sopenharmony_ci}
12048c2ecf20Sopenharmony_ci
12058c2ecf20Sopenharmony_cistatic void rtl92c_dm_init_dynamic_bb_powersaving(struct ieee80211_hw *hw)
12068c2ecf20Sopenharmony_ci{
12078c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
12088c2ecf20Sopenharmony_ci	struct ps_t *dm_pstable = &rtlpriv->dm_pstable;
12098c2ecf20Sopenharmony_ci
12108c2ecf20Sopenharmony_ci	dm_pstable->pre_ccastate = CCA_MAX;
12118c2ecf20Sopenharmony_ci	dm_pstable->cur_ccasate = CCA_MAX;
12128c2ecf20Sopenharmony_ci	dm_pstable->pre_rfstate = RF_MAX;
12138c2ecf20Sopenharmony_ci	dm_pstable->cur_rfstate = RF_MAX;
12148c2ecf20Sopenharmony_ci	dm_pstable->rssi_val_min = 0;
12158c2ecf20Sopenharmony_ci}
12168c2ecf20Sopenharmony_ci
12178c2ecf20Sopenharmony_cistatic void rtl88e_dm_update_rx_idle_ant(struct ieee80211_hw *hw,
12188c2ecf20Sopenharmony_ci					 u8 ant)
12198c2ecf20Sopenharmony_ci{
12208c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
12218c2ecf20Sopenharmony_ci	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
12228c2ecf20Sopenharmony_ci	struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
12238c2ecf20Sopenharmony_ci	struct fast_ant_training *pfat_table = &rtldm->fat_table;
12248c2ecf20Sopenharmony_ci	u32 default_ant, optional_ant;
12258c2ecf20Sopenharmony_ci
12268c2ecf20Sopenharmony_ci	if (pfat_table->rx_idle_ant != ant) {
12278c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
12288c2ecf20Sopenharmony_ci			"need to update rx idle ant\n");
12298c2ecf20Sopenharmony_ci		if (ant == MAIN_ANT) {
12308c2ecf20Sopenharmony_ci			default_ant =
12318c2ecf20Sopenharmony_ci			  (pfat_table->rx_idle_ant == CG_TRX_HW_ANTDIV) ?
12328c2ecf20Sopenharmony_ci			  MAIN_ANT_CG_TRX : MAIN_ANT_CGCS_RX;
12338c2ecf20Sopenharmony_ci			optional_ant =
12348c2ecf20Sopenharmony_ci			  (pfat_table->rx_idle_ant == CG_TRX_HW_ANTDIV) ?
12358c2ecf20Sopenharmony_ci			  AUX_ANT_CG_TRX : AUX_ANT_CGCS_RX;
12368c2ecf20Sopenharmony_ci		} else {
12378c2ecf20Sopenharmony_ci			default_ant =
12388c2ecf20Sopenharmony_ci			   (pfat_table->rx_idle_ant == CG_TRX_HW_ANTDIV) ?
12398c2ecf20Sopenharmony_ci			   AUX_ANT_CG_TRX : AUX_ANT_CGCS_RX;
12408c2ecf20Sopenharmony_ci			optional_ant =
12418c2ecf20Sopenharmony_ci			   (pfat_table->rx_idle_ant == CG_TRX_HW_ANTDIV) ?
12428c2ecf20Sopenharmony_ci			   MAIN_ANT_CG_TRX : MAIN_ANT_CGCS_RX;
12438c2ecf20Sopenharmony_ci		}
12448c2ecf20Sopenharmony_ci
12458c2ecf20Sopenharmony_ci		if (rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV) {
12468c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N,
12478c2ecf20Sopenharmony_ci				      BIT(5) | BIT(4) | BIT(3), default_ant);
12488c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N,
12498c2ecf20Sopenharmony_ci				      BIT(8) | BIT(7) | BIT(6), optional_ant);
12508c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, DM_REG_ANTSEL_CTRL_11N,
12518c2ecf20Sopenharmony_ci				      BIT(14) | BIT(13) | BIT(12),
12528c2ecf20Sopenharmony_ci				      default_ant);
12538c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, DM_REG_RESP_TX_11N,
12548c2ecf20Sopenharmony_ci				      BIT(6) | BIT(7), default_ant);
12558c2ecf20Sopenharmony_ci		} else if (rtlefuse->antenna_div_type == CGCS_RX_HW_ANTDIV) {
12568c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N,
12578c2ecf20Sopenharmony_ci				      BIT(5) | BIT(4) | BIT(3), default_ant);
12588c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N,
12598c2ecf20Sopenharmony_ci				      BIT(8) | BIT(7) | BIT(6), optional_ant);
12608c2ecf20Sopenharmony_ci		}
12618c2ecf20Sopenharmony_ci	}
12628c2ecf20Sopenharmony_ci	pfat_table->rx_idle_ant = ant;
12638c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD, "RxIdleAnt %s\n",
12648c2ecf20Sopenharmony_ci		(ant == MAIN_ANT) ? ("MAIN_ANT") : ("AUX_ANT"));
12658c2ecf20Sopenharmony_ci}
12668c2ecf20Sopenharmony_ci
12678c2ecf20Sopenharmony_cistatic void rtl88e_dm_update_tx_ant(struct ieee80211_hw *hw,
12688c2ecf20Sopenharmony_ci				    u8 ant, u32 mac_id)
12698c2ecf20Sopenharmony_ci{
12708c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
12718c2ecf20Sopenharmony_ci	struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
12728c2ecf20Sopenharmony_ci	struct fast_ant_training *pfat_table = &rtldm->fat_table;
12738c2ecf20Sopenharmony_ci	u8 target_ant;
12748c2ecf20Sopenharmony_ci
12758c2ecf20Sopenharmony_ci	if (ant == MAIN_ANT)
12768c2ecf20Sopenharmony_ci		target_ant = MAIN_ANT_CG_TRX;
12778c2ecf20Sopenharmony_ci	else
12788c2ecf20Sopenharmony_ci		target_ant = AUX_ANT_CG_TRX;
12798c2ecf20Sopenharmony_ci
12808c2ecf20Sopenharmony_ci	pfat_table->antsel_a[mac_id] = target_ant & BIT(0);
12818c2ecf20Sopenharmony_ci	pfat_table->antsel_b[mac_id] = (target_ant & BIT(1)) >> 1;
12828c2ecf20Sopenharmony_ci	pfat_table->antsel_c[mac_id] = (target_ant & BIT(2)) >> 2;
12838c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD, "txfrominfo target ant %s\n",
12848c2ecf20Sopenharmony_ci		(ant == MAIN_ANT) ? ("MAIN_ANT") : ("AUX_ANT"));
12858c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD, "antsel_tr_mux = 3'b%d%d%d\n",
12868c2ecf20Sopenharmony_ci		pfat_table->antsel_c[mac_id],
12878c2ecf20Sopenharmony_ci		pfat_table->antsel_b[mac_id],
12888c2ecf20Sopenharmony_ci		pfat_table->antsel_a[mac_id]);
12898c2ecf20Sopenharmony_ci}
12908c2ecf20Sopenharmony_ci
12918c2ecf20Sopenharmony_cistatic void rtl88e_dm_rx_hw_antena_div_init(struct ieee80211_hw *hw)
12928c2ecf20Sopenharmony_ci{
12938c2ecf20Sopenharmony_ci	u32  value32;
12948c2ecf20Sopenharmony_ci
12958c2ecf20Sopenharmony_ci	/*MAC Setting*/
12968c2ecf20Sopenharmony_ci	value32 = rtl_get_bbreg(hw, DM_REG_ANTSEL_PIN_11N, MASKDWORD);
12978c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_ANTSEL_PIN_11N,
12988c2ecf20Sopenharmony_ci		      MASKDWORD, value32 | (BIT(23) | BIT(25)));
12998c2ecf20Sopenharmony_ci	/*Pin Setting*/
13008c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_PIN_CTRL_11N, BIT(9) | BIT(8), 0);
13018c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N, BIT(10), 0);
13028c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_LNA_SWITCH_11N, BIT(22), 1);
13038c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_LNA_SWITCH_11N, BIT(31), 1);
13048c2ecf20Sopenharmony_ci	/*OFDM Setting*/
13058c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_ANTDIV_PARA1_11N, MASKDWORD, 0x000000a0);
13068c2ecf20Sopenharmony_ci	/*CCK Setting*/
13078c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_BB_PWR_SAV4_11N, BIT(7), 1);
13088c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_CCK_ANTDIV_PARA2_11N, BIT(4), 1);
13098c2ecf20Sopenharmony_ci	rtl88e_dm_update_rx_idle_ant(hw, MAIN_ANT);
13108c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_ANT_MAPPING1_11N, MASKLWORD, 0x0201);
13118c2ecf20Sopenharmony_ci}
13128c2ecf20Sopenharmony_ci
13138c2ecf20Sopenharmony_cistatic void rtl88e_dm_trx_hw_antenna_div_init(struct ieee80211_hw *hw)
13148c2ecf20Sopenharmony_ci{
13158c2ecf20Sopenharmony_ci	u32  value32;
13168c2ecf20Sopenharmony_ci
13178c2ecf20Sopenharmony_ci	/*MAC Setting*/
13188c2ecf20Sopenharmony_ci	value32 = rtl_get_bbreg(hw, DM_REG_ANTSEL_PIN_11N, MASKDWORD);
13198c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_ANTSEL_PIN_11N, MASKDWORD,
13208c2ecf20Sopenharmony_ci		      value32 | (BIT(23) | BIT(25)));
13218c2ecf20Sopenharmony_ci	/*Pin Setting*/
13228c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_PIN_CTRL_11N, BIT(9) | BIT(8), 0);
13238c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N, BIT(10), 0);
13248c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_LNA_SWITCH_11N, BIT(22), 0);
13258c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_LNA_SWITCH_11N, BIT(31), 1);
13268c2ecf20Sopenharmony_ci	/*OFDM Setting*/
13278c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_ANTDIV_PARA1_11N, MASKDWORD, 0x000000a0);
13288c2ecf20Sopenharmony_ci	/*CCK Setting*/
13298c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_BB_PWR_SAV4_11N, BIT(7), 1);
13308c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_CCK_ANTDIV_PARA2_11N, BIT(4), 1);
13318c2ecf20Sopenharmony_ci	/*TX Setting*/
13328c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_TX_ANT_CTRL_11N, BIT(21), 0);
13338c2ecf20Sopenharmony_ci	rtl88e_dm_update_rx_idle_ant(hw, MAIN_ANT);
13348c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_ANT_MAPPING1_11N, MASKLWORD, 0x0201);
13358c2ecf20Sopenharmony_ci}
13368c2ecf20Sopenharmony_ci
13378c2ecf20Sopenharmony_cistatic void rtl88e_dm_fast_training_init(struct ieee80211_hw *hw)
13388c2ecf20Sopenharmony_ci{
13398c2ecf20Sopenharmony_ci	struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
13408c2ecf20Sopenharmony_ci	struct fast_ant_training *pfat_table = &rtldm->fat_table;
13418c2ecf20Sopenharmony_ci	u32 ant_combination = 2;
13428c2ecf20Sopenharmony_ci	u32 value32, i;
13438c2ecf20Sopenharmony_ci
13448c2ecf20Sopenharmony_ci	for (i = 0; i < 6; i++) {
13458c2ecf20Sopenharmony_ci		pfat_table->bssid[i] = 0;
13468c2ecf20Sopenharmony_ci		pfat_table->ant_sum[i] = 0;
13478c2ecf20Sopenharmony_ci		pfat_table->ant_cnt[i] = 0;
13488c2ecf20Sopenharmony_ci		pfat_table->ant_ave[i] = 0;
13498c2ecf20Sopenharmony_ci	}
13508c2ecf20Sopenharmony_ci	pfat_table->train_idx = 0;
13518c2ecf20Sopenharmony_ci	pfat_table->fat_state = FAT_NORMAL_STATE;
13528c2ecf20Sopenharmony_ci
13538c2ecf20Sopenharmony_ci	/*MAC Setting*/
13548c2ecf20Sopenharmony_ci	value32 = rtl_get_bbreg(hw, DM_REG_ANTSEL_PIN_11N, MASKDWORD);
13558c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_ANTSEL_PIN_11N,
13568c2ecf20Sopenharmony_ci		      MASKDWORD, value32 | (BIT(23) | BIT(25)));
13578c2ecf20Sopenharmony_ci	value32 = rtl_get_bbreg(hw, DM_REG_ANT_TRAIN_PARA2_11N, MASKDWORD);
13588c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_ANT_TRAIN_PARA2_11N,
13598c2ecf20Sopenharmony_ci		      MASKDWORD, value32 | (BIT(16) | BIT(17)));
13608c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_ANT_TRAIN_PARA2_11N,
13618c2ecf20Sopenharmony_ci		      MASKLWORD, 0);
13628c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_ANT_TRAIN_PARA1_11N,
13638c2ecf20Sopenharmony_ci		      MASKDWORD, 0);
13648c2ecf20Sopenharmony_ci
13658c2ecf20Sopenharmony_ci	/*Pin Setting*/
13668c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_PIN_CTRL_11N, BIT(9) | BIT(8), 0);
13678c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N, BIT(10), 0);
13688c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_LNA_SWITCH_11N, BIT(22), 0);
13698c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_LNA_SWITCH_11N, BIT(31), 1);
13708c2ecf20Sopenharmony_ci
13718c2ecf20Sopenharmony_ci	/*OFDM Setting*/
13728c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_ANTDIV_PARA1_11N, MASKDWORD, 0x000000a0);
13738c2ecf20Sopenharmony_ci	/*antenna mapping table*/
13748c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_ANT_MAPPING1_11N, MASKBYTE0, 1);
13758c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_ANT_MAPPING1_11N, MASKBYTE1, 2);
13768c2ecf20Sopenharmony_ci
13778c2ecf20Sopenharmony_ci	/*TX Setting*/
13788c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_TX_ANT_CTRL_11N, BIT(21), 1);
13798c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N,
13808c2ecf20Sopenharmony_ci		      BIT(5) | BIT(4) | BIT(3), 0);
13818c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N,
13828c2ecf20Sopenharmony_ci		      BIT(8) | BIT(7) | BIT(6), 1);
13838c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N,
13848c2ecf20Sopenharmony_ci		      BIT(2) | BIT(1) | BIT(0), (ant_combination - 1));
13858c2ecf20Sopenharmony_ci
13868c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, DM_REG_IGI_A_11N, BIT(7), 1);
13878c2ecf20Sopenharmony_ci}
13888c2ecf20Sopenharmony_ci
13898c2ecf20Sopenharmony_cistatic void rtl88e_dm_antenna_div_init(struct ieee80211_hw *hw)
13908c2ecf20Sopenharmony_ci{
13918c2ecf20Sopenharmony_ci	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
13928c2ecf20Sopenharmony_ci
13938c2ecf20Sopenharmony_ci	if (rtlefuse->antenna_div_type == CGCS_RX_HW_ANTDIV)
13948c2ecf20Sopenharmony_ci		rtl88e_dm_rx_hw_antena_div_init(hw);
13958c2ecf20Sopenharmony_ci	else if (rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV)
13968c2ecf20Sopenharmony_ci		rtl88e_dm_trx_hw_antenna_div_init(hw);
13978c2ecf20Sopenharmony_ci	else if (rtlefuse->antenna_div_type == CG_TRX_SMART_ANTDIV)
13988c2ecf20Sopenharmony_ci		rtl88e_dm_fast_training_init(hw);
13998c2ecf20Sopenharmony_ci
14008c2ecf20Sopenharmony_ci}
14018c2ecf20Sopenharmony_ci
14028c2ecf20Sopenharmony_civoid rtl88e_dm_set_tx_ant_by_tx_info(struct ieee80211_hw *hw,
14038c2ecf20Sopenharmony_ci				     u8 *pdesc, u32 mac_id)
14048c2ecf20Sopenharmony_ci{
14058c2ecf20Sopenharmony_ci	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
14068c2ecf20Sopenharmony_ci	struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
14078c2ecf20Sopenharmony_ci	struct fast_ant_training *pfat_table = &rtldm->fat_table;
14088c2ecf20Sopenharmony_ci	__le32 *pdesc32 = (__le32 *)pdesc;
14098c2ecf20Sopenharmony_ci
14108c2ecf20Sopenharmony_ci	if ((rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV) ||
14118c2ecf20Sopenharmony_ci	    (rtlefuse->antenna_div_type == CG_TRX_SMART_ANTDIV)) {
14128c2ecf20Sopenharmony_ci		set_tx_desc_antsel_a(pdesc32, pfat_table->antsel_a[mac_id]);
14138c2ecf20Sopenharmony_ci		set_tx_desc_antsel_b(pdesc32, pfat_table->antsel_b[mac_id]);
14148c2ecf20Sopenharmony_ci		set_tx_desc_antsel_c(pdesc32, pfat_table->antsel_c[mac_id]);
14158c2ecf20Sopenharmony_ci	}
14168c2ecf20Sopenharmony_ci}
14178c2ecf20Sopenharmony_ci
14188c2ecf20Sopenharmony_civoid rtl88e_dm_ant_sel_statistics(struct ieee80211_hw *hw,
14198c2ecf20Sopenharmony_ci				  u8 antsel_tr_mux, u32 mac_id,
14208c2ecf20Sopenharmony_ci				  u32 rx_pwdb_all)
14218c2ecf20Sopenharmony_ci{
14228c2ecf20Sopenharmony_ci	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
14238c2ecf20Sopenharmony_ci	struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
14248c2ecf20Sopenharmony_ci	struct fast_ant_training *pfat_table = &rtldm->fat_table;
14258c2ecf20Sopenharmony_ci
14268c2ecf20Sopenharmony_ci	if (rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV) {
14278c2ecf20Sopenharmony_ci		if (antsel_tr_mux == MAIN_ANT_CG_TRX) {
14288c2ecf20Sopenharmony_ci			pfat_table->main_ant_sum[mac_id] += rx_pwdb_all;
14298c2ecf20Sopenharmony_ci			pfat_table->main_ant_cnt[mac_id]++;
14308c2ecf20Sopenharmony_ci		} else {
14318c2ecf20Sopenharmony_ci			pfat_table->aux_ant_sum[mac_id] += rx_pwdb_all;
14328c2ecf20Sopenharmony_ci			pfat_table->aux_ant_cnt[mac_id]++;
14338c2ecf20Sopenharmony_ci		}
14348c2ecf20Sopenharmony_ci	} else if (rtlefuse->antenna_div_type == CGCS_RX_HW_ANTDIV) {
14358c2ecf20Sopenharmony_ci		if (antsel_tr_mux == MAIN_ANT_CGCS_RX) {
14368c2ecf20Sopenharmony_ci			pfat_table->main_ant_sum[mac_id] += rx_pwdb_all;
14378c2ecf20Sopenharmony_ci			pfat_table->main_ant_cnt[mac_id]++;
14388c2ecf20Sopenharmony_ci		} else {
14398c2ecf20Sopenharmony_ci			pfat_table->aux_ant_sum[mac_id] += rx_pwdb_all;
14408c2ecf20Sopenharmony_ci			pfat_table->aux_ant_cnt[mac_id]++;
14418c2ecf20Sopenharmony_ci		}
14428c2ecf20Sopenharmony_ci	}
14438c2ecf20Sopenharmony_ci}
14448c2ecf20Sopenharmony_ci
14458c2ecf20Sopenharmony_cistatic void rtl88e_dm_hw_ant_div(struct ieee80211_hw *hw)
14468c2ecf20Sopenharmony_ci{
14478c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
14488c2ecf20Sopenharmony_ci	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
14498c2ecf20Sopenharmony_ci	struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
14508c2ecf20Sopenharmony_ci	struct rtl_sta_info *drv_priv;
14518c2ecf20Sopenharmony_ci	struct fast_ant_training *pfat_table = &rtldm->fat_table;
14528c2ecf20Sopenharmony_ci	struct dig_t *dm_dig = &rtlpriv->dm_digtable;
14538c2ecf20Sopenharmony_ci	u32 i, min_rssi = 0xff, ant_div_max_rssi = 0;
14548c2ecf20Sopenharmony_ci	u32 max_rssi = 0, local_min_rssi, local_max_rssi;
14558c2ecf20Sopenharmony_ci	u32 main_rssi, aux_rssi;
14568c2ecf20Sopenharmony_ci	u8 rx_idle_ant = 0, target_ant = 7;
14578c2ecf20Sopenharmony_ci
14588c2ecf20Sopenharmony_ci	/*for sta its self*/
14598c2ecf20Sopenharmony_ci	i = 0;
14608c2ecf20Sopenharmony_ci	main_rssi = (pfat_table->main_ant_cnt[i] != 0) ?
14618c2ecf20Sopenharmony_ci		(pfat_table->main_ant_sum[i] / pfat_table->main_ant_cnt[i]) : 0;
14628c2ecf20Sopenharmony_ci	aux_rssi = (pfat_table->aux_ant_cnt[i] != 0) ?
14638c2ecf20Sopenharmony_ci		(pfat_table->aux_ant_sum[i] / pfat_table->aux_ant_cnt[i]) : 0;
14648c2ecf20Sopenharmony_ci	target_ant = (main_rssi == aux_rssi) ?
14658c2ecf20Sopenharmony_ci		pfat_table->rx_idle_ant : ((main_rssi >= aux_rssi) ?
14668c2ecf20Sopenharmony_ci		MAIN_ANT : AUX_ANT);
14678c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
14688c2ecf20Sopenharmony_ci		"main_ant_sum %d main_ant_cnt %d\n",
14698c2ecf20Sopenharmony_ci		pfat_table->main_ant_sum[i],
14708c2ecf20Sopenharmony_ci		pfat_table->main_ant_cnt[i]);
14718c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
14728c2ecf20Sopenharmony_ci		"aux_ant_sum %d aux_ant_cnt %d\n",
14738c2ecf20Sopenharmony_ci		pfat_table->aux_ant_sum[i], pfat_table->aux_ant_cnt[i]);
14748c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD, "main_rssi %d aux_rssi%d\n",
14758c2ecf20Sopenharmony_ci		main_rssi, aux_rssi);
14768c2ecf20Sopenharmony_ci	local_max_rssi = (main_rssi > aux_rssi) ? main_rssi : aux_rssi;
14778c2ecf20Sopenharmony_ci	if ((local_max_rssi > ant_div_max_rssi) && (local_max_rssi < 40))
14788c2ecf20Sopenharmony_ci		ant_div_max_rssi = local_max_rssi;
14798c2ecf20Sopenharmony_ci	if (local_max_rssi > max_rssi)
14808c2ecf20Sopenharmony_ci		max_rssi = local_max_rssi;
14818c2ecf20Sopenharmony_ci
14828c2ecf20Sopenharmony_ci	if ((pfat_table->rx_idle_ant == MAIN_ANT) && (main_rssi == 0))
14838c2ecf20Sopenharmony_ci		main_rssi = aux_rssi;
14848c2ecf20Sopenharmony_ci	else if ((pfat_table->rx_idle_ant == AUX_ANT) && (aux_rssi == 0))
14858c2ecf20Sopenharmony_ci		aux_rssi = main_rssi;
14868c2ecf20Sopenharmony_ci
14878c2ecf20Sopenharmony_ci	local_min_rssi = (main_rssi > aux_rssi) ? aux_rssi : main_rssi;
14888c2ecf20Sopenharmony_ci	if (local_min_rssi < min_rssi) {
14898c2ecf20Sopenharmony_ci		min_rssi = local_min_rssi;
14908c2ecf20Sopenharmony_ci		rx_idle_ant = target_ant;
14918c2ecf20Sopenharmony_ci	}
14928c2ecf20Sopenharmony_ci	if (rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV)
14938c2ecf20Sopenharmony_ci		rtl88e_dm_update_tx_ant(hw, target_ant, i);
14948c2ecf20Sopenharmony_ci
14958c2ecf20Sopenharmony_ci	if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP ||
14968c2ecf20Sopenharmony_ci	    rtlpriv->mac80211.opmode == NL80211_IFTYPE_ADHOC) {
14978c2ecf20Sopenharmony_ci		spin_lock_bh(&rtlpriv->locks.entry_list_lock);
14988c2ecf20Sopenharmony_ci		list_for_each_entry(drv_priv, &rtlpriv->entry_list, list) {
14998c2ecf20Sopenharmony_ci			i++;
15008c2ecf20Sopenharmony_ci			main_rssi = (pfat_table->main_ant_cnt[i] != 0) ?
15018c2ecf20Sopenharmony_ci				(pfat_table->main_ant_sum[i] /
15028c2ecf20Sopenharmony_ci				pfat_table->main_ant_cnt[i]) : 0;
15038c2ecf20Sopenharmony_ci			aux_rssi = (pfat_table->aux_ant_cnt[i] != 0) ?
15048c2ecf20Sopenharmony_ci				(pfat_table->aux_ant_sum[i] /
15058c2ecf20Sopenharmony_ci				pfat_table->aux_ant_cnt[i]) : 0;
15068c2ecf20Sopenharmony_ci			target_ant = (main_rssi == aux_rssi) ?
15078c2ecf20Sopenharmony_ci				pfat_table->rx_idle_ant : ((main_rssi >=
15088c2ecf20Sopenharmony_ci				aux_rssi) ? MAIN_ANT : AUX_ANT);
15098c2ecf20Sopenharmony_ci
15108c2ecf20Sopenharmony_ci			local_max_rssi = (main_rssi > aux_rssi) ?
15118c2ecf20Sopenharmony_ci					 main_rssi : aux_rssi;
15128c2ecf20Sopenharmony_ci			if ((local_max_rssi > ant_div_max_rssi) &&
15138c2ecf20Sopenharmony_ci			    (local_max_rssi < 40))
15148c2ecf20Sopenharmony_ci				ant_div_max_rssi = local_max_rssi;
15158c2ecf20Sopenharmony_ci			if (local_max_rssi > max_rssi)
15168c2ecf20Sopenharmony_ci				max_rssi = local_max_rssi;
15178c2ecf20Sopenharmony_ci
15188c2ecf20Sopenharmony_ci			if ((pfat_table->rx_idle_ant == MAIN_ANT) &&
15198c2ecf20Sopenharmony_ci			    (main_rssi == 0))
15208c2ecf20Sopenharmony_ci				main_rssi = aux_rssi;
15218c2ecf20Sopenharmony_ci			else if ((pfat_table->rx_idle_ant == AUX_ANT) &&
15228c2ecf20Sopenharmony_ci				 (aux_rssi == 0))
15238c2ecf20Sopenharmony_ci				aux_rssi = main_rssi;
15248c2ecf20Sopenharmony_ci
15258c2ecf20Sopenharmony_ci			local_min_rssi = (main_rssi > aux_rssi) ?
15268c2ecf20Sopenharmony_ci				aux_rssi : main_rssi;
15278c2ecf20Sopenharmony_ci			if (local_min_rssi < min_rssi) {
15288c2ecf20Sopenharmony_ci				min_rssi = local_min_rssi;
15298c2ecf20Sopenharmony_ci				rx_idle_ant = target_ant;
15308c2ecf20Sopenharmony_ci			}
15318c2ecf20Sopenharmony_ci			if (rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV)
15328c2ecf20Sopenharmony_ci				rtl88e_dm_update_tx_ant(hw, target_ant, i);
15338c2ecf20Sopenharmony_ci		}
15348c2ecf20Sopenharmony_ci		spin_unlock_bh(&rtlpriv->locks.entry_list_lock);
15358c2ecf20Sopenharmony_ci	}
15368c2ecf20Sopenharmony_ci
15378c2ecf20Sopenharmony_ci	for (i = 0; i < ASSOCIATE_ENTRY_NUM; i++) {
15388c2ecf20Sopenharmony_ci		pfat_table->main_ant_sum[i] = 0;
15398c2ecf20Sopenharmony_ci		pfat_table->aux_ant_sum[i] = 0;
15408c2ecf20Sopenharmony_ci		pfat_table->main_ant_cnt[i] = 0;
15418c2ecf20Sopenharmony_ci		pfat_table->aux_ant_cnt[i] = 0;
15428c2ecf20Sopenharmony_ci	}
15438c2ecf20Sopenharmony_ci
15448c2ecf20Sopenharmony_ci	rtl88e_dm_update_rx_idle_ant(hw, rx_idle_ant);
15458c2ecf20Sopenharmony_ci
15468c2ecf20Sopenharmony_ci	dm_dig->antdiv_rssi_max = ant_div_max_rssi;
15478c2ecf20Sopenharmony_ci	dm_dig->rssi_max = max_rssi;
15488c2ecf20Sopenharmony_ci}
15498c2ecf20Sopenharmony_ci
15508c2ecf20Sopenharmony_cistatic void rtl88e_set_next_mac_address_target(struct ieee80211_hw *hw)
15518c2ecf20Sopenharmony_ci{
15528c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
15538c2ecf20Sopenharmony_ci	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
15548c2ecf20Sopenharmony_ci	struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
15558c2ecf20Sopenharmony_ci	struct rtl_sta_info *drv_priv;
15568c2ecf20Sopenharmony_ci	struct fast_ant_training *pfat_table = &rtldm->fat_table;
15578c2ecf20Sopenharmony_ci	u32 value32, i, j = 0;
15588c2ecf20Sopenharmony_ci
15598c2ecf20Sopenharmony_ci	if (mac->link_state >= MAC80211_LINKED) {
15608c2ecf20Sopenharmony_ci		for (i = 0; i < ASSOCIATE_ENTRY_NUM; i++) {
15618c2ecf20Sopenharmony_ci			if ((pfat_table->train_idx + 1) == ASSOCIATE_ENTRY_NUM)
15628c2ecf20Sopenharmony_ci				pfat_table->train_idx = 0;
15638c2ecf20Sopenharmony_ci			else
15648c2ecf20Sopenharmony_ci				pfat_table->train_idx++;
15658c2ecf20Sopenharmony_ci
15668c2ecf20Sopenharmony_ci			if (pfat_table->train_idx == 0) {
15678c2ecf20Sopenharmony_ci				value32 = (mac->mac_addr[5] << 8) |
15688c2ecf20Sopenharmony_ci					  mac->mac_addr[4];
15698c2ecf20Sopenharmony_ci				rtl_set_bbreg(hw, DM_REG_ANT_TRAIN_PARA2_11N,
15708c2ecf20Sopenharmony_ci					      MASKLWORD, value32);
15718c2ecf20Sopenharmony_ci
15728c2ecf20Sopenharmony_ci				value32 = (mac->mac_addr[3] << 24) |
15738c2ecf20Sopenharmony_ci					  (mac->mac_addr[2] << 16) |
15748c2ecf20Sopenharmony_ci					  (mac->mac_addr[1] << 8) |
15758c2ecf20Sopenharmony_ci					  mac->mac_addr[0];
15768c2ecf20Sopenharmony_ci				rtl_set_bbreg(hw, DM_REG_ANT_TRAIN_PARA1_11N,
15778c2ecf20Sopenharmony_ci					      MASKDWORD, value32);
15788c2ecf20Sopenharmony_ci				break;
15798c2ecf20Sopenharmony_ci			}
15808c2ecf20Sopenharmony_ci
15818c2ecf20Sopenharmony_ci			if (rtlpriv->mac80211.opmode !=
15828c2ecf20Sopenharmony_ci			    NL80211_IFTYPE_STATION) {
15838c2ecf20Sopenharmony_ci				spin_lock_bh(&rtlpriv->locks.entry_list_lock);
15848c2ecf20Sopenharmony_ci				list_for_each_entry(drv_priv,
15858c2ecf20Sopenharmony_ci						    &rtlpriv->entry_list, list) {
15868c2ecf20Sopenharmony_ci					j++;
15878c2ecf20Sopenharmony_ci					if (j != pfat_table->train_idx)
15888c2ecf20Sopenharmony_ci						continue;
15898c2ecf20Sopenharmony_ci
15908c2ecf20Sopenharmony_ci					value32 = (drv_priv->mac_addr[5] << 8) |
15918c2ecf20Sopenharmony_ci						  drv_priv->mac_addr[4];
15928c2ecf20Sopenharmony_ci					rtl_set_bbreg(hw,
15938c2ecf20Sopenharmony_ci						      DM_REG_ANT_TRAIN_PARA2_11N,
15948c2ecf20Sopenharmony_ci						      MASKLWORD, value32);
15958c2ecf20Sopenharmony_ci
15968c2ecf20Sopenharmony_ci					value32 = (drv_priv->mac_addr[3] << 24) |
15978c2ecf20Sopenharmony_ci						  (drv_priv->mac_addr[2] << 16) |
15988c2ecf20Sopenharmony_ci						  (drv_priv->mac_addr[1] << 8) |
15998c2ecf20Sopenharmony_ci						  drv_priv->mac_addr[0];
16008c2ecf20Sopenharmony_ci					rtl_set_bbreg(hw,
16018c2ecf20Sopenharmony_ci						      DM_REG_ANT_TRAIN_PARA1_11N,
16028c2ecf20Sopenharmony_ci						      MASKDWORD, value32);
16038c2ecf20Sopenharmony_ci					break;
16048c2ecf20Sopenharmony_ci				}
16058c2ecf20Sopenharmony_ci				spin_unlock_bh(&rtlpriv->locks.entry_list_lock);
16068c2ecf20Sopenharmony_ci				/*find entry, break*/
16078c2ecf20Sopenharmony_ci				if (j == pfat_table->train_idx)
16088c2ecf20Sopenharmony_ci					break;
16098c2ecf20Sopenharmony_ci			}
16108c2ecf20Sopenharmony_ci		}
16118c2ecf20Sopenharmony_ci	}
16128c2ecf20Sopenharmony_ci}
16138c2ecf20Sopenharmony_ci
16148c2ecf20Sopenharmony_cistatic void rtl88e_dm_fast_ant_training(struct ieee80211_hw *hw)
16158c2ecf20Sopenharmony_ci{
16168c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
16178c2ecf20Sopenharmony_ci	struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
16188c2ecf20Sopenharmony_ci	struct fast_ant_training *pfat_table = &rtldm->fat_table;
16198c2ecf20Sopenharmony_ci	u32 i, max_rssi = 0;
16208c2ecf20Sopenharmony_ci	u8 target_ant = 2;
16218c2ecf20Sopenharmony_ci	bool bpkt_filter_match = false;
16228c2ecf20Sopenharmony_ci
16238c2ecf20Sopenharmony_ci	if (pfat_table->fat_state == FAT_TRAINING_STATE) {
16248c2ecf20Sopenharmony_ci		for (i = 0; i < 7; i++) {
16258c2ecf20Sopenharmony_ci			if (pfat_table->ant_cnt[i] == 0) {
16268c2ecf20Sopenharmony_ci				pfat_table->ant_ave[i] = 0;
16278c2ecf20Sopenharmony_ci			} else {
16288c2ecf20Sopenharmony_ci				pfat_table->ant_ave[i] =
16298c2ecf20Sopenharmony_ci					pfat_table->ant_sum[i] /
16308c2ecf20Sopenharmony_ci					pfat_table->ant_cnt[i];
16318c2ecf20Sopenharmony_ci				bpkt_filter_match = true;
16328c2ecf20Sopenharmony_ci			}
16338c2ecf20Sopenharmony_ci
16348c2ecf20Sopenharmony_ci			if (pfat_table->ant_ave[i] > max_rssi) {
16358c2ecf20Sopenharmony_ci				max_rssi = pfat_table->ant_ave[i];
16368c2ecf20Sopenharmony_ci				target_ant = (u8) i;
16378c2ecf20Sopenharmony_ci			}
16388c2ecf20Sopenharmony_ci		}
16398c2ecf20Sopenharmony_ci
16408c2ecf20Sopenharmony_ci		if (bpkt_filter_match == false) {
16418c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, DM_REG_TXAGC_A_1_MCS32_11N,
16428c2ecf20Sopenharmony_ci				      BIT(16), 0);
16438c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, DM_REG_IGI_A_11N, BIT(7), 0);
16448c2ecf20Sopenharmony_ci		} else {
16458c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, DM_REG_TXAGC_A_1_MCS32_11N,
16468c2ecf20Sopenharmony_ci				      BIT(16), 0);
16478c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N, BIT(8) |
16488c2ecf20Sopenharmony_ci				      BIT(7) | BIT(6), target_ant);
16498c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, DM_REG_TX_ANT_CTRL_11N,
16508c2ecf20Sopenharmony_ci				      BIT(21), 1);
16518c2ecf20Sopenharmony_ci
16528c2ecf20Sopenharmony_ci			pfat_table->antsel_a[pfat_table->train_idx] =
16538c2ecf20Sopenharmony_ci				target_ant & BIT(0);
16548c2ecf20Sopenharmony_ci			pfat_table->antsel_b[pfat_table->train_idx] =
16558c2ecf20Sopenharmony_ci				(target_ant & BIT(1)) >> 1;
16568c2ecf20Sopenharmony_ci			pfat_table->antsel_c[pfat_table->train_idx] =
16578c2ecf20Sopenharmony_ci				(target_ant & BIT(2)) >> 2;
16588c2ecf20Sopenharmony_ci
16598c2ecf20Sopenharmony_ci			if (target_ant == 0)
16608c2ecf20Sopenharmony_ci				rtl_set_bbreg(hw, DM_REG_IGI_A_11N, BIT(7), 0);
16618c2ecf20Sopenharmony_ci		}
16628c2ecf20Sopenharmony_ci
16638c2ecf20Sopenharmony_ci		for (i = 0; i < 7; i++) {
16648c2ecf20Sopenharmony_ci			pfat_table->ant_sum[i] = 0;
16658c2ecf20Sopenharmony_ci			pfat_table->ant_cnt[i] = 0;
16668c2ecf20Sopenharmony_ci		}
16678c2ecf20Sopenharmony_ci
16688c2ecf20Sopenharmony_ci		pfat_table->fat_state = FAT_NORMAL_STATE;
16698c2ecf20Sopenharmony_ci		return;
16708c2ecf20Sopenharmony_ci	}
16718c2ecf20Sopenharmony_ci
16728c2ecf20Sopenharmony_ci	if (pfat_table->fat_state == FAT_NORMAL_STATE) {
16738c2ecf20Sopenharmony_ci		rtl88e_set_next_mac_address_target(hw);
16748c2ecf20Sopenharmony_ci
16758c2ecf20Sopenharmony_ci		pfat_table->fat_state = FAT_TRAINING_STATE;
16768c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, DM_REG_TXAGC_A_1_MCS32_11N, BIT(16), 1);
16778c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, DM_REG_IGI_A_11N, BIT(7), 1);
16788c2ecf20Sopenharmony_ci
16798c2ecf20Sopenharmony_ci		mod_timer(&rtlpriv->works.fast_antenna_training_timer,
16808c2ecf20Sopenharmony_ci			  jiffies + MSECS(RTL_WATCH_DOG_TIME));
16818c2ecf20Sopenharmony_ci	}
16828c2ecf20Sopenharmony_ci}
16838c2ecf20Sopenharmony_ci
16848c2ecf20Sopenharmony_civoid rtl88e_dm_fast_antenna_training_callback(struct timer_list *t)
16858c2ecf20Sopenharmony_ci{
16868c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv =
16878c2ecf20Sopenharmony_ci		from_timer(rtlpriv, t, works.fast_antenna_training_timer);
16888c2ecf20Sopenharmony_ci	struct ieee80211_hw *hw = rtlpriv->hw;
16898c2ecf20Sopenharmony_ci
16908c2ecf20Sopenharmony_ci	rtl88e_dm_fast_ant_training(hw);
16918c2ecf20Sopenharmony_ci}
16928c2ecf20Sopenharmony_ci
16938c2ecf20Sopenharmony_cistatic void rtl88e_dm_antenna_diversity(struct ieee80211_hw *hw)
16948c2ecf20Sopenharmony_ci{
16958c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
16968c2ecf20Sopenharmony_ci	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
16978c2ecf20Sopenharmony_ci	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
16988c2ecf20Sopenharmony_ci	struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
16998c2ecf20Sopenharmony_ci	struct fast_ant_training *pfat_table = &rtldm->fat_table;
17008c2ecf20Sopenharmony_ci
17018c2ecf20Sopenharmony_ci	if (mac->link_state < MAC80211_LINKED) {
17028c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_DIG, DBG_LOUD, "No Link\n");
17038c2ecf20Sopenharmony_ci		if (pfat_table->becomelinked) {
17048c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_DIG, DBG_LOUD,
17058c2ecf20Sopenharmony_ci				"need to turn off HW AntDiv\n");
17068c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, DM_REG_IGI_A_11N, BIT(7), 0);
17078c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, DM_REG_CCK_ANTDIV_PARA1_11N,
17088c2ecf20Sopenharmony_ci				      BIT(15), 0);
17098c2ecf20Sopenharmony_ci			if (rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV)
17108c2ecf20Sopenharmony_ci				rtl_set_bbreg(hw, DM_REG_TX_ANT_CTRL_11N,
17118c2ecf20Sopenharmony_ci					      BIT(21), 0);
17128c2ecf20Sopenharmony_ci			pfat_table->becomelinked =
17138c2ecf20Sopenharmony_ci				(mac->link_state == MAC80211_LINKED) ?
17148c2ecf20Sopenharmony_ci				true : false;
17158c2ecf20Sopenharmony_ci		}
17168c2ecf20Sopenharmony_ci		return;
17178c2ecf20Sopenharmony_ci	} else {
17188c2ecf20Sopenharmony_ci		if (!pfat_table->becomelinked) {
17198c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_DIG, DBG_LOUD,
17208c2ecf20Sopenharmony_ci				"Need to turn on HW AntDiv\n");
17218c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, DM_REG_IGI_A_11N, BIT(7), 1);
17228c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, DM_REG_CCK_ANTDIV_PARA1_11N,
17238c2ecf20Sopenharmony_ci				      BIT(15), 1);
17248c2ecf20Sopenharmony_ci			if (rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV)
17258c2ecf20Sopenharmony_ci				rtl_set_bbreg(hw, DM_REG_TX_ANT_CTRL_11N,
17268c2ecf20Sopenharmony_ci					      BIT(21), 1);
17278c2ecf20Sopenharmony_ci			pfat_table->becomelinked =
17288c2ecf20Sopenharmony_ci				(mac->link_state >= MAC80211_LINKED) ?
17298c2ecf20Sopenharmony_ci				true : false;
17308c2ecf20Sopenharmony_ci		}
17318c2ecf20Sopenharmony_ci	}
17328c2ecf20Sopenharmony_ci
17338c2ecf20Sopenharmony_ci	if ((rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV) ||
17348c2ecf20Sopenharmony_ci	    (rtlefuse->antenna_div_type == CGCS_RX_HW_ANTDIV))
17358c2ecf20Sopenharmony_ci		rtl88e_dm_hw_ant_div(hw);
17368c2ecf20Sopenharmony_ci	else if (rtlefuse->antenna_div_type == CG_TRX_SMART_ANTDIV)
17378c2ecf20Sopenharmony_ci		rtl88e_dm_fast_ant_training(hw);
17388c2ecf20Sopenharmony_ci}
17398c2ecf20Sopenharmony_ci
17408c2ecf20Sopenharmony_civoid rtl88e_dm_init(struct ieee80211_hw *hw)
17418c2ecf20Sopenharmony_ci{
17428c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
17438c2ecf20Sopenharmony_ci	u32 cur_igvalue = rtl_get_bbreg(hw, ROFDM0_XAAGCCORE1, 0x7f);
17448c2ecf20Sopenharmony_ci
17458c2ecf20Sopenharmony_ci	rtlpriv->dm.dm_type = DM_TYPE_BYDRIVER;
17468c2ecf20Sopenharmony_ci	rtl_dm_diginit(hw, cur_igvalue);
17478c2ecf20Sopenharmony_ci	rtl88e_dm_init_dynamic_txpower(hw);
17488c2ecf20Sopenharmony_ci	rtl88e_dm_init_edca_turbo(hw);
17498c2ecf20Sopenharmony_ci	rtl88e_dm_init_rate_adaptive_mask(hw);
17508c2ecf20Sopenharmony_ci	rtl88e_dm_init_txpower_tracking(hw);
17518c2ecf20Sopenharmony_ci	rtl92c_dm_init_dynamic_bb_powersaving(hw);
17528c2ecf20Sopenharmony_ci	rtl88e_dm_antenna_div_init(hw);
17538c2ecf20Sopenharmony_ci}
17548c2ecf20Sopenharmony_ci
17558c2ecf20Sopenharmony_civoid rtl88e_dm_watchdog(struct ieee80211_hw *hw)
17568c2ecf20Sopenharmony_ci{
17578c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
17588c2ecf20Sopenharmony_ci	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
17598c2ecf20Sopenharmony_ci	bool fw_current_inpsmode = false;
17608c2ecf20Sopenharmony_ci	bool fw_ps_awake = true;
17618c2ecf20Sopenharmony_ci
17628c2ecf20Sopenharmony_ci	rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_FW_PSMODE_STATUS,
17638c2ecf20Sopenharmony_ci				      (u8 *)(&fw_current_inpsmode));
17648c2ecf20Sopenharmony_ci	rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_FWLPS_RF_ON,
17658c2ecf20Sopenharmony_ci				      (u8 *)(&fw_ps_awake));
17668c2ecf20Sopenharmony_ci	if (ppsc->p2p_ps_info.p2p_ps_mode)
17678c2ecf20Sopenharmony_ci		fw_ps_awake = false;
17688c2ecf20Sopenharmony_ci
17698c2ecf20Sopenharmony_ci	spin_lock(&rtlpriv->locks.rf_ps_lock);
17708c2ecf20Sopenharmony_ci	if ((ppsc->rfpwr_state == ERFON) &&
17718c2ecf20Sopenharmony_ci	    ((!fw_current_inpsmode) && fw_ps_awake) &&
17728c2ecf20Sopenharmony_ci	    (!ppsc->rfchange_inprogress)) {
17738c2ecf20Sopenharmony_ci		rtl88e_dm_pwdb_monitor(hw);
17748c2ecf20Sopenharmony_ci		rtl88e_dm_dig(hw);
17758c2ecf20Sopenharmony_ci		rtl88e_dm_false_alarm_counter_statistics(hw);
17768c2ecf20Sopenharmony_ci		rtl92c_dm_dynamic_txpower(hw);
17778c2ecf20Sopenharmony_ci		rtl88e_dm_check_txpower_tracking(hw);
17788c2ecf20Sopenharmony_ci		rtl88e_dm_refresh_rate_adaptive_mask(hw);
17798c2ecf20Sopenharmony_ci		rtl88e_dm_check_edca_turbo(hw);
17808c2ecf20Sopenharmony_ci		rtl88e_dm_antenna_diversity(hw);
17818c2ecf20Sopenharmony_ci	}
17828c2ecf20Sopenharmony_ci	spin_unlock(&rtlpriv->locks.rf_ps_lock);
17838c2ecf20Sopenharmony_ci}
1784