xref: /openbmc/linux/drivers/net/wireless/realtek/rtw88/phy.c (revision 226746fd12013b80ef16eceb9081012d2a6efcc0)
1e3037485SYan-Hsuan Chuang // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2e3037485SYan-Hsuan Chuang /* Copyright(c) 2018-2019  Realtek Corporation
3e3037485SYan-Hsuan Chuang  */
4e3037485SYan-Hsuan Chuang 
5e3037485SYan-Hsuan Chuang #include <linux/bcd.h>
6e3037485SYan-Hsuan Chuang 
7e3037485SYan-Hsuan Chuang #include "main.h"
8e3037485SYan-Hsuan Chuang #include "reg.h"
9e3037485SYan-Hsuan Chuang #include "fw.h"
10e3037485SYan-Hsuan Chuang #include "phy.h"
11e3037485SYan-Hsuan Chuang #include "debug.h"
12e3037485SYan-Hsuan Chuang 
13e3037485SYan-Hsuan Chuang struct phy_cfg_pair {
14e3037485SYan-Hsuan Chuang 	u32 addr;
15e3037485SYan-Hsuan Chuang 	u32 data;
16e3037485SYan-Hsuan Chuang };
17e3037485SYan-Hsuan Chuang 
18e3037485SYan-Hsuan Chuang union phy_table_tile {
19e3037485SYan-Hsuan Chuang 	struct rtw_phy_cond cond;
20e3037485SYan-Hsuan Chuang 	struct phy_cfg_pair cfg;
21e3037485SYan-Hsuan Chuang };
22e3037485SYan-Hsuan Chuang 
23e3037485SYan-Hsuan Chuang struct phy_pg_cfg_pair {
24e3037485SYan-Hsuan Chuang 	u32 band;
25e3037485SYan-Hsuan Chuang 	u32 rf_path;
26e3037485SYan-Hsuan Chuang 	u32 tx_num;
27e3037485SYan-Hsuan Chuang 	u32 addr;
28e3037485SYan-Hsuan Chuang 	u32 bitmask;
29e3037485SYan-Hsuan Chuang 	u32 data;
30e3037485SYan-Hsuan Chuang };
31e3037485SYan-Hsuan Chuang 
32e3037485SYan-Hsuan Chuang struct txpwr_lmt_cfg_pair {
33e3037485SYan-Hsuan Chuang 	u8 regd;
34e3037485SYan-Hsuan Chuang 	u8 band;
35e3037485SYan-Hsuan Chuang 	u8 bw;
36e3037485SYan-Hsuan Chuang 	u8 rs;
37e3037485SYan-Hsuan Chuang 	u8 ch;
38e3037485SYan-Hsuan Chuang 	s8 txpwr_lmt;
39e3037485SYan-Hsuan Chuang };
40e3037485SYan-Hsuan Chuang 
41e3037485SYan-Hsuan Chuang static const u32 db_invert_table[12][8] = {
42e3037485SYan-Hsuan Chuang 	{10,		13,		16,		20,
43e3037485SYan-Hsuan Chuang 	 25,		32,		40,		50},
44e3037485SYan-Hsuan Chuang 	{64,		80,		101,		128,
45e3037485SYan-Hsuan Chuang 	 160,		201,		256,		318},
46e3037485SYan-Hsuan Chuang 	{401,		505,		635,		800,
47e3037485SYan-Hsuan Chuang 	 1007,		1268,		1596,		2010},
48e3037485SYan-Hsuan Chuang 	{316,		398,		501,		631,
49e3037485SYan-Hsuan Chuang 	 794,		1000,		1259,		1585},
50e3037485SYan-Hsuan Chuang 	{1995,		2512,		3162,		3981,
51e3037485SYan-Hsuan Chuang 	 5012,		6310,		7943,		10000},
52e3037485SYan-Hsuan Chuang 	{12589,		15849,		19953,		25119,
53e3037485SYan-Hsuan Chuang 	 31623,		39811,		50119,		63098},
54e3037485SYan-Hsuan Chuang 	{79433,		100000,		125893,		158489,
55e3037485SYan-Hsuan Chuang 	 199526,	251189,		316228,		398107},
56e3037485SYan-Hsuan Chuang 	{501187,	630957,		794328,		1000000,
57e3037485SYan-Hsuan Chuang 	 1258925,	1584893,	1995262,	2511886},
58e3037485SYan-Hsuan Chuang 	{3162278,	3981072,	5011872,	6309573,
59e3037485SYan-Hsuan Chuang 	 7943282,	1000000,	12589254,	15848932},
60e3037485SYan-Hsuan Chuang 	{19952623,	25118864,	31622777,	39810717,
61e3037485SYan-Hsuan Chuang 	 50118723,	63095734,	79432823,	100000000},
62e3037485SYan-Hsuan Chuang 	{125892541,	158489319,	199526232,	251188643,
63e3037485SYan-Hsuan Chuang 	 316227766,	398107171,	501187234,	630957345},
64e3037485SYan-Hsuan Chuang 	{794328235,	1000000000,	1258925412,	1584893192,
65e3037485SYan-Hsuan Chuang 	 1995262315,	2511886432U,	3162277660U,	3981071706U}
66e3037485SYan-Hsuan Chuang };
67e3037485SYan-Hsuan Chuang 
68fa6dfe6bSYan-Hsuan Chuang u8 rtw_cck_rates[] = { DESC_RATE1M, DESC_RATE2M, DESC_RATE5_5M, DESC_RATE11M };
69fa6dfe6bSYan-Hsuan Chuang u8 rtw_ofdm_rates[] = {
70fa6dfe6bSYan-Hsuan Chuang 	DESC_RATE6M,  DESC_RATE9M,  DESC_RATE12M,
71fa6dfe6bSYan-Hsuan Chuang 	DESC_RATE18M, DESC_RATE24M, DESC_RATE36M,
72fa6dfe6bSYan-Hsuan Chuang 	DESC_RATE48M, DESC_RATE54M
73fa6dfe6bSYan-Hsuan Chuang };
74fa6dfe6bSYan-Hsuan Chuang u8 rtw_ht_1s_rates[] = {
75fa6dfe6bSYan-Hsuan Chuang 	DESC_RATEMCS0, DESC_RATEMCS1, DESC_RATEMCS2,
76fa6dfe6bSYan-Hsuan Chuang 	DESC_RATEMCS3, DESC_RATEMCS4, DESC_RATEMCS5,
77fa6dfe6bSYan-Hsuan Chuang 	DESC_RATEMCS6, DESC_RATEMCS7
78fa6dfe6bSYan-Hsuan Chuang };
79fa6dfe6bSYan-Hsuan Chuang u8 rtw_ht_2s_rates[] = {
80fa6dfe6bSYan-Hsuan Chuang 	DESC_RATEMCS8,  DESC_RATEMCS9,  DESC_RATEMCS10,
81fa6dfe6bSYan-Hsuan Chuang 	DESC_RATEMCS11, DESC_RATEMCS12, DESC_RATEMCS13,
82fa6dfe6bSYan-Hsuan Chuang 	DESC_RATEMCS14, DESC_RATEMCS15
83fa6dfe6bSYan-Hsuan Chuang };
84fa6dfe6bSYan-Hsuan Chuang u8 rtw_vht_1s_rates[] = {
85fa6dfe6bSYan-Hsuan Chuang 	DESC_RATEVHT1SS_MCS0, DESC_RATEVHT1SS_MCS1,
86fa6dfe6bSYan-Hsuan Chuang 	DESC_RATEVHT1SS_MCS2, DESC_RATEVHT1SS_MCS3,
87fa6dfe6bSYan-Hsuan Chuang 	DESC_RATEVHT1SS_MCS4, DESC_RATEVHT1SS_MCS5,
88fa6dfe6bSYan-Hsuan Chuang 	DESC_RATEVHT1SS_MCS6, DESC_RATEVHT1SS_MCS7,
89fa6dfe6bSYan-Hsuan Chuang 	DESC_RATEVHT1SS_MCS8, DESC_RATEVHT1SS_MCS9
90fa6dfe6bSYan-Hsuan Chuang };
91fa6dfe6bSYan-Hsuan Chuang u8 rtw_vht_2s_rates[] = {
92fa6dfe6bSYan-Hsuan Chuang 	DESC_RATEVHT2SS_MCS0, DESC_RATEVHT2SS_MCS1,
93fa6dfe6bSYan-Hsuan Chuang 	DESC_RATEVHT2SS_MCS2, DESC_RATEVHT2SS_MCS3,
94fa6dfe6bSYan-Hsuan Chuang 	DESC_RATEVHT2SS_MCS4, DESC_RATEVHT2SS_MCS5,
95fa6dfe6bSYan-Hsuan Chuang 	DESC_RATEVHT2SS_MCS6, DESC_RATEVHT2SS_MCS7,
96fa6dfe6bSYan-Hsuan Chuang 	DESC_RATEVHT2SS_MCS8, DESC_RATEVHT2SS_MCS9
97fa6dfe6bSYan-Hsuan Chuang };
98fa6dfe6bSYan-Hsuan Chuang u8 *rtw_rate_section[RTW_RATE_SECTION_MAX] = {
99fa6dfe6bSYan-Hsuan Chuang 	rtw_cck_rates, rtw_ofdm_rates,
100fa6dfe6bSYan-Hsuan Chuang 	rtw_ht_1s_rates, rtw_ht_2s_rates,
101fa6dfe6bSYan-Hsuan Chuang 	rtw_vht_1s_rates, rtw_vht_2s_rates
102fa6dfe6bSYan-Hsuan Chuang };
103fa6dfe6bSYan-Hsuan Chuang u8 rtw_rate_size[RTW_RATE_SECTION_MAX] = {
104fa6dfe6bSYan-Hsuan Chuang 	ARRAY_SIZE(rtw_cck_rates),
105fa6dfe6bSYan-Hsuan Chuang 	ARRAY_SIZE(rtw_ofdm_rates),
106fa6dfe6bSYan-Hsuan Chuang 	ARRAY_SIZE(rtw_ht_1s_rates),
107fa6dfe6bSYan-Hsuan Chuang 	ARRAY_SIZE(rtw_ht_2s_rates),
108fa6dfe6bSYan-Hsuan Chuang 	ARRAY_SIZE(rtw_vht_1s_rates),
109fa6dfe6bSYan-Hsuan Chuang 	ARRAY_SIZE(rtw_vht_2s_rates)
110fa6dfe6bSYan-Hsuan Chuang };
111fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_cck_size = ARRAY_SIZE(rtw_cck_rates);
112fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_ofdm_size = ARRAY_SIZE(rtw_ofdm_rates);
113fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_ht_1s_size = ARRAY_SIZE(rtw_ht_1s_rates);
114fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_ht_2s_size = ARRAY_SIZE(rtw_ht_2s_rates);
115fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_vht_1s_size = ARRAY_SIZE(rtw_vht_1s_rates);
116fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_vht_2s_size = ARRAY_SIZE(rtw_vht_2s_rates);
117fa6dfe6bSYan-Hsuan Chuang 
118e3037485SYan-Hsuan Chuang enum rtw_phy_band_type {
119e3037485SYan-Hsuan Chuang 	PHY_BAND_2G	= 0,
120e3037485SYan-Hsuan Chuang 	PHY_BAND_5G	= 1,
121e3037485SYan-Hsuan Chuang };
122e3037485SYan-Hsuan Chuang 
123e3037485SYan-Hsuan Chuang void rtw_phy_init(struct rtw_dev *rtwdev)
124e3037485SYan-Hsuan Chuang {
125e3037485SYan-Hsuan Chuang 	struct rtw_chip_info *chip = rtwdev->chip;
126e3037485SYan-Hsuan Chuang 	struct rtw_dm_info *dm_info = &rtwdev->dm_info;
127e3037485SYan-Hsuan Chuang 	u32 addr, mask;
128e3037485SYan-Hsuan Chuang 
129e3037485SYan-Hsuan Chuang 	dm_info->fa_history[3] = 0;
130e3037485SYan-Hsuan Chuang 	dm_info->fa_history[2] = 0;
131e3037485SYan-Hsuan Chuang 	dm_info->fa_history[1] = 0;
132e3037485SYan-Hsuan Chuang 	dm_info->fa_history[0] = 0;
133e3037485SYan-Hsuan Chuang 	dm_info->igi_bitmap = 0;
134e3037485SYan-Hsuan Chuang 	dm_info->igi_history[3] = 0;
135e3037485SYan-Hsuan Chuang 	dm_info->igi_history[2] = 0;
136e3037485SYan-Hsuan Chuang 	dm_info->igi_history[1] = 0;
137e3037485SYan-Hsuan Chuang 
138e3037485SYan-Hsuan Chuang 	addr = chip->dig[0].addr;
139e3037485SYan-Hsuan Chuang 	mask = chip->dig[0].mask;
140e3037485SYan-Hsuan Chuang 	dm_info->igi_history[0] = rtw_read32_mask(rtwdev, addr, mask);
141e3037485SYan-Hsuan Chuang }
142e3037485SYan-Hsuan Chuang 
143e3037485SYan-Hsuan Chuang void rtw_phy_dig_write(struct rtw_dev *rtwdev, u8 igi)
144e3037485SYan-Hsuan Chuang {
145e3037485SYan-Hsuan Chuang 	struct rtw_chip_info *chip = rtwdev->chip;
146e3037485SYan-Hsuan Chuang 	struct rtw_hal *hal = &rtwdev->hal;
147e3037485SYan-Hsuan Chuang 	u32 addr, mask;
148e3037485SYan-Hsuan Chuang 	u8 path;
149e3037485SYan-Hsuan Chuang 
150e3037485SYan-Hsuan Chuang 	for (path = 0; path < hal->rf_path_num; path++) {
151e3037485SYan-Hsuan Chuang 		addr = chip->dig[path].addr;
152e3037485SYan-Hsuan Chuang 		mask = chip->dig[path].mask;
153e3037485SYan-Hsuan Chuang 		rtw_write32_mask(rtwdev, addr, mask, igi);
154e3037485SYan-Hsuan Chuang 	}
155e3037485SYan-Hsuan Chuang }
156e3037485SYan-Hsuan Chuang 
157e3037485SYan-Hsuan Chuang static void rtw_phy_stat_false_alarm(struct rtw_dev *rtwdev)
158e3037485SYan-Hsuan Chuang {
159e3037485SYan-Hsuan Chuang 	struct rtw_chip_info *chip = rtwdev->chip;
160e3037485SYan-Hsuan Chuang 
161e3037485SYan-Hsuan Chuang 	chip->ops->false_alarm_statistics(rtwdev);
162e3037485SYan-Hsuan Chuang }
163e3037485SYan-Hsuan Chuang 
164e3037485SYan-Hsuan Chuang #define RA_FLOOR_TABLE_SIZE	7
165e3037485SYan-Hsuan Chuang #define RA_FLOOR_UP_GAP		3
166e3037485SYan-Hsuan Chuang 
167e3037485SYan-Hsuan Chuang static u8 rtw_phy_get_rssi_level(u8 old_level, u8 rssi)
168e3037485SYan-Hsuan Chuang {
169e3037485SYan-Hsuan Chuang 	u8 table[RA_FLOOR_TABLE_SIZE] = {20, 34, 38, 42, 46, 50, 100};
170e3037485SYan-Hsuan Chuang 	u8 new_level = 0;
171e3037485SYan-Hsuan Chuang 	int i;
172e3037485SYan-Hsuan Chuang 
173e3037485SYan-Hsuan Chuang 	for (i = 0; i < RA_FLOOR_TABLE_SIZE; i++)
174e3037485SYan-Hsuan Chuang 		if (i >= old_level)
175e3037485SYan-Hsuan Chuang 			table[i] += RA_FLOOR_UP_GAP;
176e3037485SYan-Hsuan Chuang 
177e3037485SYan-Hsuan Chuang 	for (i = 0; i < RA_FLOOR_TABLE_SIZE; i++) {
178e3037485SYan-Hsuan Chuang 		if (rssi < table[i]) {
179e3037485SYan-Hsuan Chuang 			new_level = i;
180e3037485SYan-Hsuan Chuang 			break;
181e3037485SYan-Hsuan Chuang 		}
182e3037485SYan-Hsuan Chuang 	}
183e3037485SYan-Hsuan Chuang 
184e3037485SYan-Hsuan Chuang 	return new_level;
185e3037485SYan-Hsuan Chuang }
186e3037485SYan-Hsuan Chuang 
187e3037485SYan-Hsuan Chuang struct rtw_phy_stat_iter_data {
188e3037485SYan-Hsuan Chuang 	struct rtw_dev *rtwdev;
189e3037485SYan-Hsuan Chuang 	u8 min_rssi;
190e3037485SYan-Hsuan Chuang };
191e3037485SYan-Hsuan Chuang 
192e3037485SYan-Hsuan Chuang static void rtw_phy_stat_rssi_iter(void *data, struct ieee80211_sta *sta)
193e3037485SYan-Hsuan Chuang {
194e3037485SYan-Hsuan Chuang 	struct rtw_phy_stat_iter_data *iter_data = data;
195e3037485SYan-Hsuan Chuang 	struct rtw_dev *rtwdev = iter_data->rtwdev;
196e3037485SYan-Hsuan Chuang 	struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv;
197a24bad74SYan-Hsuan Chuang 	u8 rssi;
198e3037485SYan-Hsuan Chuang 
199e3037485SYan-Hsuan Chuang 	rssi = ewma_rssi_read(&si->avg_rssi);
200a24bad74SYan-Hsuan Chuang 	si->rssi_level = rtw_phy_get_rssi_level(si->rssi_level, rssi);
201e3037485SYan-Hsuan Chuang 
202e3037485SYan-Hsuan Chuang 	rtw_fw_send_rssi_info(rtwdev, si);
203e3037485SYan-Hsuan Chuang 
204e3037485SYan-Hsuan Chuang 	iter_data->min_rssi = min_t(u8, rssi, iter_data->min_rssi);
205e3037485SYan-Hsuan Chuang }
206e3037485SYan-Hsuan Chuang 
207e3037485SYan-Hsuan Chuang static void rtw_phy_stat_rssi(struct rtw_dev *rtwdev)
208e3037485SYan-Hsuan Chuang {
209e3037485SYan-Hsuan Chuang 	struct rtw_dm_info *dm_info = &rtwdev->dm_info;
210e3037485SYan-Hsuan Chuang 	struct rtw_phy_stat_iter_data data = {};
211e3037485SYan-Hsuan Chuang 
212e3037485SYan-Hsuan Chuang 	data.rtwdev = rtwdev;
213e3037485SYan-Hsuan Chuang 	data.min_rssi = U8_MAX;
214e3037485SYan-Hsuan Chuang 	rtw_iterate_stas_atomic(rtwdev, rtw_phy_stat_rssi_iter, &data);
215e3037485SYan-Hsuan Chuang 
216e3037485SYan-Hsuan Chuang 	dm_info->pre_min_rssi = dm_info->min_rssi;
217e3037485SYan-Hsuan Chuang 	dm_info->min_rssi = data.min_rssi;
218e3037485SYan-Hsuan Chuang }
219e3037485SYan-Hsuan Chuang 
220e3037485SYan-Hsuan Chuang static void rtw_phy_statistics(struct rtw_dev *rtwdev)
221e3037485SYan-Hsuan Chuang {
222e3037485SYan-Hsuan Chuang 	rtw_phy_stat_rssi(rtwdev);
223e3037485SYan-Hsuan Chuang 	rtw_phy_stat_false_alarm(rtwdev);
224e3037485SYan-Hsuan Chuang }
225e3037485SYan-Hsuan Chuang 
226e3037485SYan-Hsuan Chuang #define DIG_PERF_FA_TH_LOW			250
227e3037485SYan-Hsuan Chuang #define DIG_PERF_FA_TH_HIGH			500
228e3037485SYan-Hsuan Chuang #define DIG_PERF_FA_TH_EXTRA_HIGH		750
229e3037485SYan-Hsuan Chuang #define DIG_PERF_MAX				0x5a
230e3037485SYan-Hsuan Chuang #define DIG_PERF_MID				0x40
231e3037485SYan-Hsuan Chuang #define DIG_CVRG_FA_TH_LOW			2000
232e3037485SYan-Hsuan Chuang #define DIG_CVRG_FA_TH_HIGH			4000
233e3037485SYan-Hsuan Chuang #define DIG_CVRG_FA_TH_EXTRA_HIGH		5000
234e3037485SYan-Hsuan Chuang #define DIG_CVRG_MAX				0x2a
235e3037485SYan-Hsuan Chuang #define DIG_CVRG_MID				0x26
236e3037485SYan-Hsuan Chuang #define DIG_CVRG_MIN				0x1c
237e3037485SYan-Hsuan Chuang #define DIG_RSSI_GAIN_OFFSET			15
238e3037485SYan-Hsuan Chuang 
239e3037485SYan-Hsuan Chuang static bool
240e3037485SYan-Hsuan Chuang rtw_phy_dig_check_damping(struct rtw_dm_info *dm_info)
241e3037485SYan-Hsuan Chuang {
242e3037485SYan-Hsuan Chuang 	u16 fa_lo = DIG_PERF_FA_TH_LOW;
243e3037485SYan-Hsuan Chuang 	u16 fa_hi = DIG_PERF_FA_TH_HIGH;
244e3037485SYan-Hsuan Chuang 	u16 *fa_history;
245e3037485SYan-Hsuan Chuang 	u8 *igi_history;
246e3037485SYan-Hsuan Chuang 	u8 damping_rssi;
247e3037485SYan-Hsuan Chuang 	u8 min_rssi;
248e3037485SYan-Hsuan Chuang 	u8 diff;
249e3037485SYan-Hsuan Chuang 	u8 igi_bitmap;
250e3037485SYan-Hsuan Chuang 	bool damping = false;
251e3037485SYan-Hsuan Chuang 
252e3037485SYan-Hsuan Chuang 	min_rssi = dm_info->min_rssi;
253e3037485SYan-Hsuan Chuang 	if (dm_info->damping) {
254e3037485SYan-Hsuan Chuang 		damping_rssi = dm_info->damping_rssi;
255e3037485SYan-Hsuan Chuang 		diff = min_rssi > damping_rssi ? min_rssi - damping_rssi :
256e3037485SYan-Hsuan Chuang 						 damping_rssi - min_rssi;
257e3037485SYan-Hsuan Chuang 		if (diff > 3 || dm_info->damping_cnt++ > 20) {
258e3037485SYan-Hsuan Chuang 			dm_info->damping = false;
259e3037485SYan-Hsuan Chuang 			return false;
260e3037485SYan-Hsuan Chuang 		}
261e3037485SYan-Hsuan Chuang 
262e3037485SYan-Hsuan Chuang 		return true;
263e3037485SYan-Hsuan Chuang 	}
264e3037485SYan-Hsuan Chuang 
265e3037485SYan-Hsuan Chuang 	igi_history = dm_info->igi_history;
266e3037485SYan-Hsuan Chuang 	fa_history = dm_info->fa_history;
267e3037485SYan-Hsuan Chuang 	igi_bitmap = dm_info->igi_bitmap & 0xf;
268e3037485SYan-Hsuan Chuang 	switch (igi_bitmap) {
269e3037485SYan-Hsuan Chuang 	case 5:
270e3037485SYan-Hsuan Chuang 		/* down -> up -> down -> up */
271e3037485SYan-Hsuan Chuang 		if (igi_history[0] > igi_history[1] &&
272e3037485SYan-Hsuan Chuang 		    igi_history[2] > igi_history[3] &&
273e3037485SYan-Hsuan Chuang 		    igi_history[0] - igi_history[1] >= 2 &&
274e3037485SYan-Hsuan Chuang 		    igi_history[2] - igi_history[3] >= 2 &&
275e3037485SYan-Hsuan Chuang 		    fa_history[0] > fa_hi && fa_history[1] < fa_lo &&
276e3037485SYan-Hsuan Chuang 		    fa_history[2] > fa_hi && fa_history[3] < fa_lo)
277e3037485SYan-Hsuan Chuang 			damping = true;
278e3037485SYan-Hsuan Chuang 		break;
279e3037485SYan-Hsuan Chuang 	case 9:
280e3037485SYan-Hsuan Chuang 		/* up -> down -> down -> up */
281e3037485SYan-Hsuan Chuang 		if (igi_history[0] > igi_history[1] &&
282e3037485SYan-Hsuan Chuang 		    igi_history[3] > igi_history[2] &&
283e3037485SYan-Hsuan Chuang 		    igi_history[0] - igi_history[1] >= 4 &&
284e3037485SYan-Hsuan Chuang 		    igi_history[3] - igi_history[2] >= 2 &&
285e3037485SYan-Hsuan Chuang 		    fa_history[0] > fa_hi && fa_history[1] < fa_lo &&
286e3037485SYan-Hsuan Chuang 		    fa_history[2] < fa_lo && fa_history[3] > fa_hi)
287e3037485SYan-Hsuan Chuang 			damping = true;
288e3037485SYan-Hsuan Chuang 		break;
289e3037485SYan-Hsuan Chuang 	default:
290e3037485SYan-Hsuan Chuang 		return false;
291e3037485SYan-Hsuan Chuang 	}
292e3037485SYan-Hsuan Chuang 
293e3037485SYan-Hsuan Chuang 	if (damping) {
294e3037485SYan-Hsuan Chuang 		dm_info->damping = true;
295e3037485SYan-Hsuan Chuang 		dm_info->damping_cnt = 0;
296e3037485SYan-Hsuan Chuang 		dm_info->damping_rssi = min_rssi;
297e3037485SYan-Hsuan Chuang 	}
298e3037485SYan-Hsuan Chuang 
299e3037485SYan-Hsuan Chuang 	return damping;
300e3037485SYan-Hsuan Chuang }
301e3037485SYan-Hsuan Chuang 
302e3037485SYan-Hsuan Chuang static void rtw_phy_dig_get_boundary(struct rtw_dm_info *dm_info,
303e3037485SYan-Hsuan Chuang 				     u8 *upper, u8 *lower, bool linked)
304e3037485SYan-Hsuan Chuang {
305e3037485SYan-Hsuan Chuang 	u8 dig_max, dig_min, dig_mid;
306e3037485SYan-Hsuan Chuang 	u8 min_rssi;
307e3037485SYan-Hsuan Chuang 
308e3037485SYan-Hsuan Chuang 	if (linked) {
309e3037485SYan-Hsuan Chuang 		dig_max = DIG_PERF_MAX;
310e3037485SYan-Hsuan Chuang 		dig_mid = DIG_PERF_MID;
311e3037485SYan-Hsuan Chuang 		/* 22B=0x1c, 22C=0x20 */
312e3037485SYan-Hsuan Chuang 		dig_min = 0x1c;
313e3037485SYan-Hsuan Chuang 		min_rssi = max_t(u8, dm_info->min_rssi, dig_min);
314e3037485SYan-Hsuan Chuang 	} else {
315e3037485SYan-Hsuan Chuang 		dig_max = DIG_CVRG_MAX;
316e3037485SYan-Hsuan Chuang 		dig_mid = DIG_CVRG_MID;
317e3037485SYan-Hsuan Chuang 		dig_min = DIG_CVRG_MIN;
318e3037485SYan-Hsuan Chuang 		min_rssi = dig_min;
319e3037485SYan-Hsuan Chuang 	}
320e3037485SYan-Hsuan Chuang 
321e3037485SYan-Hsuan Chuang 	/* DIG MAX should be bounded by minimum RSSI with offset +15 */
322e3037485SYan-Hsuan Chuang 	dig_max = min_t(u8, dig_max, min_rssi + DIG_RSSI_GAIN_OFFSET);
323e3037485SYan-Hsuan Chuang 
324e3037485SYan-Hsuan Chuang 	*lower = clamp_t(u8, min_rssi, dig_min, dig_mid);
325e3037485SYan-Hsuan Chuang 	*upper = clamp_t(u8, *lower + DIG_RSSI_GAIN_OFFSET, dig_min, dig_max);
326e3037485SYan-Hsuan Chuang }
327e3037485SYan-Hsuan Chuang 
328e3037485SYan-Hsuan Chuang static void rtw_phy_dig_get_threshold(struct rtw_dm_info *dm_info,
329e3037485SYan-Hsuan Chuang 				      u16 *fa_th, u8 *step, bool linked)
330e3037485SYan-Hsuan Chuang {
331e3037485SYan-Hsuan Chuang 	u8 min_rssi, pre_min_rssi;
332e3037485SYan-Hsuan Chuang 
333e3037485SYan-Hsuan Chuang 	min_rssi = dm_info->min_rssi;
334e3037485SYan-Hsuan Chuang 	pre_min_rssi = dm_info->pre_min_rssi;
335e3037485SYan-Hsuan Chuang 	step[0] = 4;
336e3037485SYan-Hsuan Chuang 	step[1] = 3;
337e3037485SYan-Hsuan Chuang 	step[2] = 2;
338e3037485SYan-Hsuan Chuang 
339e3037485SYan-Hsuan Chuang 	if (linked) {
340e3037485SYan-Hsuan Chuang 		fa_th[0] = DIG_PERF_FA_TH_EXTRA_HIGH;
341e3037485SYan-Hsuan Chuang 		fa_th[1] = DIG_PERF_FA_TH_HIGH;
342e3037485SYan-Hsuan Chuang 		fa_th[2] = DIG_PERF_FA_TH_LOW;
343e3037485SYan-Hsuan Chuang 		if (pre_min_rssi > min_rssi) {
344e3037485SYan-Hsuan Chuang 			step[0] = 6;
345e3037485SYan-Hsuan Chuang 			step[1] = 4;
346e3037485SYan-Hsuan Chuang 			step[2] = 2;
347e3037485SYan-Hsuan Chuang 		}
348e3037485SYan-Hsuan Chuang 	} else {
349e3037485SYan-Hsuan Chuang 		fa_th[0] = DIG_CVRG_FA_TH_EXTRA_HIGH;
350e3037485SYan-Hsuan Chuang 		fa_th[1] = DIG_CVRG_FA_TH_HIGH;
351e3037485SYan-Hsuan Chuang 		fa_th[2] = DIG_CVRG_FA_TH_LOW;
352e3037485SYan-Hsuan Chuang 	}
353e3037485SYan-Hsuan Chuang }
354e3037485SYan-Hsuan Chuang 
355e3037485SYan-Hsuan Chuang static void rtw_phy_dig_recorder(struct rtw_dm_info *dm_info, u8 igi, u16 fa)
356e3037485SYan-Hsuan Chuang {
357e3037485SYan-Hsuan Chuang 	u8 *igi_history;
358e3037485SYan-Hsuan Chuang 	u16 *fa_history;
359e3037485SYan-Hsuan Chuang 	u8 igi_bitmap;
360e3037485SYan-Hsuan Chuang 	bool up;
361e3037485SYan-Hsuan Chuang 
362e3037485SYan-Hsuan Chuang 	igi_bitmap = dm_info->igi_bitmap << 1 & 0xfe;
363e3037485SYan-Hsuan Chuang 	igi_history = dm_info->igi_history;
364e3037485SYan-Hsuan Chuang 	fa_history = dm_info->fa_history;
365e3037485SYan-Hsuan Chuang 
366e3037485SYan-Hsuan Chuang 	up = igi > igi_history[0];
367e3037485SYan-Hsuan Chuang 	igi_bitmap |= up;
368e3037485SYan-Hsuan Chuang 
369e3037485SYan-Hsuan Chuang 	igi_history[3] = igi_history[2];
370e3037485SYan-Hsuan Chuang 	igi_history[2] = igi_history[1];
371e3037485SYan-Hsuan Chuang 	igi_history[1] = igi_history[0];
372e3037485SYan-Hsuan Chuang 	igi_history[0] = igi;
373e3037485SYan-Hsuan Chuang 
374e3037485SYan-Hsuan Chuang 	fa_history[3] = fa_history[2];
375e3037485SYan-Hsuan Chuang 	fa_history[2] = fa_history[1];
376e3037485SYan-Hsuan Chuang 	fa_history[1] = fa_history[0];
377e3037485SYan-Hsuan Chuang 	fa_history[0] = fa;
378e3037485SYan-Hsuan Chuang 
379e3037485SYan-Hsuan Chuang 	dm_info->igi_bitmap = igi_bitmap;
380e3037485SYan-Hsuan Chuang }
381e3037485SYan-Hsuan Chuang 
382e3037485SYan-Hsuan Chuang static void rtw_phy_dig(struct rtw_dev *rtwdev)
383e3037485SYan-Hsuan Chuang {
384e3037485SYan-Hsuan Chuang 	struct rtw_dm_info *dm_info = &rtwdev->dm_info;
385e3037485SYan-Hsuan Chuang 	u8 upper_bound, lower_bound;
386e3037485SYan-Hsuan Chuang 	u8 pre_igi, cur_igi;
387e3037485SYan-Hsuan Chuang 	u16 fa_th[3], fa_cnt;
388e3037485SYan-Hsuan Chuang 	u8 level;
389e3037485SYan-Hsuan Chuang 	u8 step[3];
390e3037485SYan-Hsuan Chuang 	bool linked;
391e3037485SYan-Hsuan Chuang 
392e3037485SYan-Hsuan Chuang 	if (rtw_flag_check(rtwdev, RTW_FLAG_DIG_DISABLE))
393e3037485SYan-Hsuan Chuang 		return;
394e3037485SYan-Hsuan Chuang 
395e3037485SYan-Hsuan Chuang 	if (rtw_phy_dig_check_damping(dm_info))
396e3037485SYan-Hsuan Chuang 		return;
397e3037485SYan-Hsuan Chuang 
398e3037485SYan-Hsuan Chuang 	linked = !!rtwdev->sta_cnt;
399e3037485SYan-Hsuan Chuang 
400e3037485SYan-Hsuan Chuang 	fa_cnt = dm_info->total_fa_cnt;
401e3037485SYan-Hsuan Chuang 	pre_igi = dm_info->igi_history[0];
402e3037485SYan-Hsuan Chuang 
403e3037485SYan-Hsuan Chuang 	rtw_phy_dig_get_threshold(dm_info, fa_th, step, linked);
404e3037485SYan-Hsuan Chuang 
405e3037485SYan-Hsuan Chuang 	/* test the false alarm count from the highest threshold level first,
406e3037485SYan-Hsuan Chuang 	 * and increase it by corresponding step size
407e3037485SYan-Hsuan Chuang 	 *
408e3037485SYan-Hsuan Chuang 	 * note that the step size is offset by -2, compensate it afterall
409e3037485SYan-Hsuan Chuang 	 */
410e3037485SYan-Hsuan Chuang 	cur_igi = pre_igi;
411e3037485SYan-Hsuan Chuang 	for (level = 0; level < 3; level++) {
412e3037485SYan-Hsuan Chuang 		if (fa_cnt > fa_th[level]) {
413e3037485SYan-Hsuan Chuang 			cur_igi += step[level];
414e3037485SYan-Hsuan Chuang 			break;
415e3037485SYan-Hsuan Chuang 		}
416e3037485SYan-Hsuan Chuang 	}
417e3037485SYan-Hsuan Chuang 	cur_igi -= 2;
418e3037485SYan-Hsuan Chuang 
419e3037485SYan-Hsuan Chuang 	/* calculate the upper/lower bound by the minimum rssi we have among
420e3037485SYan-Hsuan Chuang 	 * the peers connected with us, meanwhile make sure the igi value does
421e3037485SYan-Hsuan Chuang 	 * not beyond the hardware limitation
422e3037485SYan-Hsuan Chuang 	 */
423e3037485SYan-Hsuan Chuang 	rtw_phy_dig_get_boundary(dm_info, &upper_bound, &lower_bound, linked);
424e3037485SYan-Hsuan Chuang 	cur_igi = clamp_t(u8, cur_igi, lower_bound, upper_bound);
425e3037485SYan-Hsuan Chuang 
426e3037485SYan-Hsuan Chuang 	/* record current igi value and false alarm statistics for further
427e3037485SYan-Hsuan Chuang 	 * damping checks, and record the trend of igi values
428e3037485SYan-Hsuan Chuang 	 */
429e3037485SYan-Hsuan Chuang 	rtw_phy_dig_recorder(dm_info, cur_igi, fa_cnt);
430e3037485SYan-Hsuan Chuang 
431e3037485SYan-Hsuan Chuang 	if (cur_igi != pre_igi)
432e3037485SYan-Hsuan Chuang 		rtw_phy_dig_write(rtwdev, cur_igi);
433e3037485SYan-Hsuan Chuang }
434e3037485SYan-Hsuan Chuang 
435e3037485SYan-Hsuan Chuang static void rtw_phy_ra_info_update_iter(void *data, struct ieee80211_sta *sta)
436e3037485SYan-Hsuan Chuang {
437e3037485SYan-Hsuan Chuang 	struct rtw_dev *rtwdev = data;
438e3037485SYan-Hsuan Chuang 	struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv;
439e3037485SYan-Hsuan Chuang 
440e3037485SYan-Hsuan Chuang 	rtw_update_sta_info(rtwdev, si);
441e3037485SYan-Hsuan Chuang }
442e3037485SYan-Hsuan Chuang 
443e3037485SYan-Hsuan Chuang static void rtw_phy_ra_info_update(struct rtw_dev *rtwdev)
444e3037485SYan-Hsuan Chuang {
445e3037485SYan-Hsuan Chuang 	if (rtwdev->watch_dog_cnt & 0x3)
446e3037485SYan-Hsuan Chuang 		return;
447e3037485SYan-Hsuan Chuang 
448e3037485SYan-Hsuan Chuang 	rtw_iterate_stas_atomic(rtwdev, rtw_phy_ra_info_update_iter, rtwdev);
449e3037485SYan-Hsuan Chuang }
450e3037485SYan-Hsuan Chuang 
451e3037485SYan-Hsuan Chuang void rtw_phy_dynamic_mechanism(struct rtw_dev *rtwdev)
452e3037485SYan-Hsuan Chuang {
453e3037485SYan-Hsuan Chuang 	/* for further calculation */
454e3037485SYan-Hsuan Chuang 	rtw_phy_statistics(rtwdev);
455e3037485SYan-Hsuan Chuang 	rtw_phy_dig(rtwdev);
456e3037485SYan-Hsuan Chuang 	rtw_phy_ra_info_update(rtwdev);
457e3037485SYan-Hsuan Chuang }
458e3037485SYan-Hsuan Chuang 
459e3037485SYan-Hsuan Chuang #define FRAC_BITS 3
460e3037485SYan-Hsuan Chuang 
461e3037485SYan-Hsuan Chuang static u8 rtw_phy_power_2_db(s8 power)
462e3037485SYan-Hsuan Chuang {
463e3037485SYan-Hsuan Chuang 	if (power <= -100 || power >= 20)
464e3037485SYan-Hsuan Chuang 		return 0;
465e3037485SYan-Hsuan Chuang 	else if (power >= 0)
466e3037485SYan-Hsuan Chuang 		return 100;
467e3037485SYan-Hsuan Chuang 	else
468e3037485SYan-Hsuan Chuang 		return 100 + power;
469e3037485SYan-Hsuan Chuang }
470e3037485SYan-Hsuan Chuang 
471e3037485SYan-Hsuan Chuang static u64 rtw_phy_db_2_linear(u8 power_db)
472e3037485SYan-Hsuan Chuang {
473e3037485SYan-Hsuan Chuang 	u8 i, j;
474e3037485SYan-Hsuan Chuang 	u64 linear;
475e3037485SYan-Hsuan Chuang 
4768a03447dSStanislaw Gruszka 	if (power_db > 96)
4778a03447dSStanislaw Gruszka 		power_db = 96;
4788a03447dSStanislaw Gruszka 	else if (power_db < 1)
4798a03447dSStanislaw Gruszka 		return 1;
4808a03447dSStanislaw Gruszka 
481e3037485SYan-Hsuan Chuang 	/* 1dB ~ 96dB */
482e3037485SYan-Hsuan Chuang 	i = (power_db - 1) >> 3;
483e3037485SYan-Hsuan Chuang 	j = (power_db - 1) - (i << 3);
484e3037485SYan-Hsuan Chuang 
485e3037485SYan-Hsuan Chuang 	linear = db_invert_table[i][j];
486e3037485SYan-Hsuan Chuang 	linear = i > 2 ? linear << FRAC_BITS : linear;
487e3037485SYan-Hsuan Chuang 
488e3037485SYan-Hsuan Chuang 	return linear;
489e3037485SYan-Hsuan Chuang }
490e3037485SYan-Hsuan Chuang 
491e3037485SYan-Hsuan Chuang static u8 rtw_phy_linear_2_db(u64 linear)
492e3037485SYan-Hsuan Chuang {
493e3037485SYan-Hsuan Chuang 	u8 i;
494e3037485SYan-Hsuan Chuang 	u8 j;
495e3037485SYan-Hsuan Chuang 	u32 dB;
496e3037485SYan-Hsuan Chuang 
497e3037485SYan-Hsuan Chuang 	if (linear >= db_invert_table[11][7])
498e3037485SYan-Hsuan Chuang 		return 96; /* maximum 96 dB */
499e3037485SYan-Hsuan Chuang 
500e3037485SYan-Hsuan Chuang 	for (i = 0; i < 12; i++) {
501e3037485SYan-Hsuan Chuang 		if (i <= 2 && (linear << FRAC_BITS) <= db_invert_table[i][7])
502e3037485SYan-Hsuan Chuang 			break;
503e3037485SYan-Hsuan Chuang 		else if (i > 2 && linear <= db_invert_table[i][7])
504e3037485SYan-Hsuan Chuang 			break;
505e3037485SYan-Hsuan Chuang 	}
506e3037485SYan-Hsuan Chuang 
507e3037485SYan-Hsuan Chuang 	for (j = 0; j < 8; j++) {
508e3037485SYan-Hsuan Chuang 		if (i <= 2 && (linear << FRAC_BITS) <= db_invert_table[i][j])
509e3037485SYan-Hsuan Chuang 			break;
510e3037485SYan-Hsuan Chuang 		else if (i > 2 && linear <= db_invert_table[i][j])
511e3037485SYan-Hsuan Chuang 			break;
512e3037485SYan-Hsuan Chuang 	}
513e3037485SYan-Hsuan Chuang 
514e3037485SYan-Hsuan Chuang 	if (j == 0 && i == 0)
515e3037485SYan-Hsuan Chuang 		goto end;
516e3037485SYan-Hsuan Chuang 
517e3037485SYan-Hsuan Chuang 	if (j == 0) {
518e3037485SYan-Hsuan Chuang 		if (i != 3) {
519e3037485SYan-Hsuan Chuang 			if (db_invert_table[i][0] - linear >
520e3037485SYan-Hsuan Chuang 			    linear - db_invert_table[i - 1][7]) {
521e3037485SYan-Hsuan Chuang 				i = i - 1;
522e3037485SYan-Hsuan Chuang 				j = 7;
523e3037485SYan-Hsuan Chuang 			}
524e3037485SYan-Hsuan Chuang 		} else {
525e3037485SYan-Hsuan Chuang 			if (db_invert_table[3][0] - linear >
526e3037485SYan-Hsuan Chuang 			    linear - db_invert_table[2][7]) {
527e3037485SYan-Hsuan Chuang 				i = 2;
528e3037485SYan-Hsuan Chuang 				j = 7;
529e3037485SYan-Hsuan Chuang 			}
530e3037485SYan-Hsuan Chuang 		}
531e3037485SYan-Hsuan Chuang 	} else {
532e3037485SYan-Hsuan Chuang 		if (db_invert_table[i][j] - linear >
533e3037485SYan-Hsuan Chuang 		    linear - db_invert_table[i][j - 1]) {
534e3037485SYan-Hsuan Chuang 			j = j - 1;
535e3037485SYan-Hsuan Chuang 		}
536e3037485SYan-Hsuan Chuang 	}
537e3037485SYan-Hsuan Chuang end:
538e3037485SYan-Hsuan Chuang 	dB = (i << 3) + j + 1;
539e3037485SYan-Hsuan Chuang 
540e3037485SYan-Hsuan Chuang 	return dB;
541e3037485SYan-Hsuan Chuang }
542e3037485SYan-Hsuan Chuang 
543e3037485SYan-Hsuan Chuang u8 rtw_phy_rf_power_2_rssi(s8 *rf_power, u8 path_num)
544e3037485SYan-Hsuan Chuang {
545e3037485SYan-Hsuan Chuang 	s8 power;
546e3037485SYan-Hsuan Chuang 	u8 power_db;
547e3037485SYan-Hsuan Chuang 	u64 linear;
548e3037485SYan-Hsuan Chuang 	u64 sum = 0;
549e3037485SYan-Hsuan Chuang 	u8 path;
550e3037485SYan-Hsuan Chuang 
551e3037485SYan-Hsuan Chuang 	for (path = 0; path < path_num; path++) {
552e3037485SYan-Hsuan Chuang 		power = rf_power[path];
553e3037485SYan-Hsuan Chuang 		power_db = rtw_phy_power_2_db(power);
554e3037485SYan-Hsuan Chuang 		linear = rtw_phy_db_2_linear(power_db);
555e3037485SYan-Hsuan Chuang 		sum += linear;
556e3037485SYan-Hsuan Chuang 	}
557e3037485SYan-Hsuan Chuang 
558e3037485SYan-Hsuan Chuang 	sum = (sum + (1 << (FRAC_BITS - 1))) >> FRAC_BITS;
559e3037485SYan-Hsuan Chuang 	switch (path_num) {
560e3037485SYan-Hsuan Chuang 	case 2:
561e3037485SYan-Hsuan Chuang 		sum >>= 1;
562e3037485SYan-Hsuan Chuang 		break;
563e3037485SYan-Hsuan Chuang 	case 3:
564e3037485SYan-Hsuan Chuang 		sum = ((sum) + ((sum) << 1) + ((sum) << 3)) >> 5;
565e3037485SYan-Hsuan Chuang 		break;
566e3037485SYan-Hsuan Chuang 	case 4:
567e3037485SYan-Hsuan Chuang 		sum >>= 2;
568e3037485SYan-Hsuan Chuang 		break;
569e3037485SYan-Hsuan Chuang 	default:
570e3037485SYan-Hsuan Chuang 		break;
571e3037485SYan-Hsuan Chuang 	}
572e3037485SYan-Hsuan Chuang 
573e3037485SYan-Hsuan Chuang 	return rtw_phy_linear_2_db(sum);
574e3037485SYan-Hsuan Chuang }
575e3037485SYan-Hsuan Chuang 
576e3037485SYan-Hsuan Chuang u32 rtw_phy_read_rf(struct rtw_dev *rtwdev, enum rtw_rf_path rf_path,
577e3037485SYan-Hsuan Chuang 		    u32 addr, u32 mask)
578e3037485SYan-Hsuan Chuang {
579e3037485SYan-Hsuan Chuang 	struct rtw_hal *hal = &rtwdev->hal;
580e3037485SYan-Hsuan Chuang 	struct rtw_chip_info *chip = rtwdev->chip;
581e3037485SYan-Hsuan Chuang 	const u32 *base_addr = chip->rf_base_addr;
582e3037485SYan-Hsuan Chuang 	u32 val, direct_addr;
583e3037485SYan-Hsuan Chuang 
584e3037485SYan-Hsuan Chuang 	if (rf_path >= hal->rf_path_num) {
585e3037485SYan-Hsuan Chuang 		rtw_err(rtwdev, "unsupported rf path (%d)\n", rf_path);
586e3037485SYan-Hsuan Chuang 		return INV_RF_DATA;
587e3037485SYan-Hsuan Chuang 	}
588e3037485SYan-Hsuan Chuang 
589e3037485SYan-Hsuan Chuang 	addr &= 0xff;
590e3037485SYan-Hsuan Chuang 	direct_addr = base_addr[rf_path] + (addr << 2);
591e3037485SYan-Hsuan Chuang 	mask &= RFREG_MASK;
592e3037485SYan-Hsuan Chuang 
593e3037485SYan-Hsuan Chuang 	val = rtw_read32_mask(rtwdev, direct_addr, mask);
594e3037485SYan-Hsuan Chuang 
595e3037485SYan-Hsuan Chuang 	return val;
596e3037485SYan-Hsuan Chuang }
597e3037485SYan-Hsuan Chuang 
598e3037485SYan-Hsuan Chuang bool rtw_phy_write_rf_reg_sipi(struct rtw_dev *rtwdev, enum rtw_rf_path rf_path,
599e3037485SYan-Hsuan Chuang 			       u32 addr, u32 mask, u32 data)
600e3037485SYan-Hsuan Chuang {
601e3037485SYan-Hsuan Chuang 	struct rtw_hal *hal = &rtwdev->hal;
602e3037485SYan-Hsuan Chuang 	struct rtw_chip_info *chip = rtwdev->chip;
603e3037485SYan-Hsuan Chuang 	u32 *sipi_addr = chip->rf_sipi_addr;
604e3037485SYan-Hsuan Chuang 	u32 data_and_addr;
605e3037485SYan-Hsuan Chuang 	u32 old_data = 0;
606e3037485SYan-Hsuan Chuang 	u32 shift;
607e3037485SYan-Hsuan Chuang 
608e3037485SYan-Hsuan Chuang 	if (rf_path >= hal->rf_path_num) {
609e3037485SYan-Hsuan Chuang 		rtw_err(rtwdev, "unsupported rf path (%d)\n", rf_path);
610e3037485SYan-Hsuan Chuang 		return false;
611e3037485SYan-Hsuan Chuang 	}
612e3037485SYan-Hsuan Chuang 
613e3037485SYan-Hsuan Chuang 	addr &= 0xff;
614e3037485SYan-Hsuan Chuang 	mask &= RFREG_MASK;
615e3037485SYan-Hsuan Chuang 
616e3037485SYan-Hsuan Chuang 	if (mask != RFREG_MASK) {
617e3037485SYan-Hsuan Chuang 		old_data = rtw_phy_read_rf(rtwdev, rf_path, addr, RFREG_MASK);
618e3037485SYan-Hsuan Chuang 
619e3037485SYan-Hsuan Chuang 		if (old_data == INV_RF_DATA) {
620e3037485SYan-Hsuan Chuang 			rtw_err(rtwdev, "Write fail, rf is disabled\n");
621e3037485SYan-Hsuan Chuang 			return false;
622e3037485SYan-Hsuan Chuang 		}
623e3037485SYan-Hsuan Chuang 
624e3037485SYan-Hsuan Chuang 		shift = __ffs(mask);
625e3037485SYan-Hsuan Chuang 		data = ((old_data) & (~mask)) | (data << shift);
626e3037485SYan-Hsuan Chuang 	}
627e3037485SYan-Hsuan Chuang 
628e3037485SYan-Hsuan Chuang 	data_and_addr = ((addr << 20) | (data & 0x000fffff)) & 0x0fffffff;
629e3037485SYan-Hsuan Chuang 
630e3037485SYan-Hsuan Chuang 	rtw_write32(rtwdev, sipi_addr[rf_path], data_and_addr);
631e3037485SYan-Hsuan Chuang 
632e3037485SYan-Hsuan Chuang 	udelay(13);
633e3037485SYan-Hsuan Chuang 
634e3037485SYan-Hsuan Chuang 	return true;
635e3037485SYan-Hsuan Chuang }
636e3037485SYan-Hsuan Chuang 
637e3037485SYan-Hsuan Chuang bool rtw_phy_write_rf_reg(struct rtw_dev *rtwdev, enum rtw_rf_path rf_path,
638e3037485SYan-Hsuan Chuang 			  u32 addr, u32 mask, u32 data)
639e3037485SYan-Hsuan Chuang {
640e3037485SYan-Hsuan Chuang 	struct rtw_hal *hal = &rtwdev->hal;
641e3037485SYan-Hsuan Chuang 	struct rtw_chip_info *chip = rtwdev->chip;
642e3037485SYan-Hsuan Chuang 	const u32 *base_addr = chip->rf_base_addr;
643e3037485SYan-Hsuan Chuang 	u32 direct_addr;
644e3037485SYan-Hsuan Chuang 
645e3037485SYan-Hsuan Chuang 	if (rf_path >= hal->rf_path_num) {
646e3037485SYan-Hsuan Chuang 		rtw_err(rtwdev, "unsupported rf path (%d)\n", rf_path);
647e3037485SYan-Hsuan Chuang 		return false;
648e3037485SYan-Hsuan Chuang 	}
649e3037485SYan-Hsuan Chuang 
650e3037485SYan-Hsuan Chuang 	addr &= 0xff;
651e3037485SYan-Hsuan Chuang 	direct_addr = base_addr[rf_path] + (addr << 2);
652e3037485SYan-Hsuan Chuang 	mask &= RFREG_MASK;
653e3037485SYan-Hsuan Chuang 
654818d46e7SChien-Hsun Liao 	if (addr == RF_CFGCH) {
655e3037485SYan-Hsuan Chuang 		rtw_write32_mask(rtwdev, REG_RSV_CTRL, BITS_RFC_DIRECT, DISABLE_PI);
656e3037485SYan-Hsuan Chuang 		rtw_write32_mask(rtwdev, REG_WLRF1, BITS_RFC_DIRECT, DISABLE_PI);
657818d46e7SChien-Hsun Liao 	}
658818d46e7SChien-Hsun Liao 
659e3037485SYan-Hsuan Chuang 	rtw_write32_mask(rtwdev, direct_addr, mask, data);
660e3037485SYan-Hsuan Chuang 
661e3037485SYan-Hsuan Chuang 	udelay(1);
662e3037485SYan-Hsuan Chuang 
663818d46e7SChien-Hsun Liao 	if (addr == RF_CFGCH) {
664e3037485SYan-Hsuan Chuang 		rtw_write32_mask(rtwdev, REG_RSV_CTRL, BITS_RFC_DIRECT, ENABLE_PI);
665e3037485SYan-Hsuan Chuang 		rtw_write32_mask(rtwdev, REG_WLRF1, BITS_RFC_DIRECT, ENABLE_PI);
666818d46e7SChien-Hsun Liao 	}
667e3037485SYan-Hsuan Chuang 
668e3037485SYan-Hsuan Chuang 	return true;
669e3037485SYan-Hsuan Chuang }
670e3037485SYan-Hsuan Chuang 
671e3037485SYan-Hsuan Chuang bool rtw_phy_write_rf_reg_mix(struct rtw_dev *rtwdev, enum rtw_rf_path rf_path,
672e3037485SYan-Hsuan Chuang 			      u32 addr, u32 mask, u32 data)
673e3037485SYan-Hsuan Chuang {
674e3037485SYan-Hsuan Chuang 	if (addr != 0x00)
675e3037485SYan-Hsuan Chuang 		return rtw_phy_write_rf_reg(rtwdev, rf_path, addr, mask, data);
676e3037485SYan-Hsuan Chuang 
677e3037485SYan-Hsuan Chuang 	return rtw_phy_write_rf_reg_sipi(rtwdev, rf_path, addr, mask, data);
678e3037485SYan-Hsuan Chuang }
679e3037485SYan-Hsuan Chuang 
680e3037485SYan-Hsuan Chuang void rtw_phy_setup_phy_cond(struct rtw_dev *rtwdev, u32 pkg)
681e3037485SYan-Hsuan Chuang {
682e3037485SYan-Hsuan Chuang 	struct rtw_hal *hal = &rtwdev->hal;
683e3037485SYan-Hsuan Chuang 	struct rtw_efuse *efuse = &rtwdev->efuse;
684e3037485SYan-Hsuan Chuang 	struct rtw_phy_cond cond = {0};
685e3037485SYan-Hsuan Chuang 
686e3037485SYan-Hsuan Chuang 	cond.cut = hal->cut_version ? hal->cut_version : 15;
687e3037485SYan-Hsuan Chuang 	cond.pkg = pkg ? pkg : 15;
688e3037485SYan-Hsuan Chuang 	cond.plat = 0x04;
689e3037485SYan-Hsuan Chuang 	cond.rfe = efuse->rfe_option;
690e3037485SYan-Hsuan Chuang 
691e3037485SYan-Hsuan Chuang 	switch (rtw_hci_type(rtwdev)) {
692e3037485SYan-Hsuan Chuang 	case RTW_HCI_TYPE_USB:
693e3037485SYan-Hsuan Chuang 		cond.intf = INTF_USB;
694e3037485SYan-Hsuan Chuang 		break;
695e3037485SYan-Hsuan Chuang 	case RTW_HCI_TYPE_SDIO:
696e3037485SYan-Hsuan Chuang 		cond.intf = INTF_SDIO;
697e3037485SYan-Hsuan Chuang 		break;
698e3037485SYan-Hsuan Chuang 	case RTW_HCI_TYPE_PCIE:
699e3037485SYan-Hsuan Chuang 	default:
700e3037485SYan-Hsuan Chuang 		cond.intf = INTF_PCIE;
701e3037485SYan-Hsuan Chuang 		break;
702e3037485SYan-Hsuan Chuang 	}
703e3037485SYan-Hsuan Chuang 
704e3037485SYan-Hsuan Chuang 	hal->phy_cond = cond;
705e3037485SYan-Hsuan Chuang 
706e3037485SYan-Hsuan Chuang 	rtw_dbg(rtwdev, RTW_DBG_PHY, "phy cond=0x%08x\n", *((u32 *)&hal->phy_cond));
707e3037485SYan-Hsuan Chuang }
708e3037485SYan-Hsuan Chuang 
709e3037485SYan-Hsuan Chuang static bool check_positive(struct rtw_dev *rtwdev, struct rtw_phy_cond cond)
710e3037485SYan-Hsuan Chuang {
711e3037485SYan-Hsuan Chuang 	struct rtw_hal *hal = &rtwdev->hal;
712e3037485SYan-Hsuan Chuang 	struct rtw_phy_cond drv_cond = hal->phy_cond;
713e3037485SYan-Hsuan Chuang 
714e3037485SYan-Hsuan Chuang 	if (cond.cut && cond.cut != drv_cond.cut)
715e3037485SYan-Hsuan Chuang 		return false;
716e3037485SYan-Hsuan Chuang 
717e3037485SYan-Hsuan Chuang 	if (cond.pkg && cond.pkg != drv_cond.pkg)
718e3037485SYan-Hsuan Chuang 		return false;
719e3037485SYan-Hsuan Chuang 
720e3037485SYan-Hsuan Chuang 	if (cond.intf && cond.intf != drv_cond.intf)
721e3037485SYan-Hsuan Chuang 		return false;
722e3037485SYan-Hsuan Chuang 
723e3037485SYan-Hsuan Chuang 	if (cond.rfe != drv_cond.rfe)
724e3037485SYan-Hsuan Chuang 		return false;
725e3037485SYan-Hsuan Chuang 
726e3037485SYan-Hsuan Chuang 	return true;
727e3037485SYan-Hsuan Chuang }
728e3037485SYan-Hsuan Chuang 
729e3037485SYan-Hsuan Chuang void rtw_parse_tbl_phy_cond(struct rtw_dev *rtwdev, const struct rtw_table *tbl)
730e3037485SYan-Hsuan Chuang {
731e3037485SYan-Hsuan Chuang 	const union phy_table_tile *p = tbl->data;
732e3037485SYan-Hsuan Chuang 	const union phy_table_tile *end = p + tbl->size / 2;
733e3037485SYan-Hsuan Chuang 	struct rtw_phy_cond pos_cond = {0};
734e3037485SYan-Hsuan Chuang 	bool is_matched = true, is_skipped = false;
735e3037485SYan-Hsuan Chuang 
736e3037485SYan-Hsuan Chuang 	BUILD_BUG_ON(sizeof(union phy_table_tile) != sizeof(struct phy_cfg_pair));
737e3037485SYan-Hsuan Chuang 
738e3037485SYan-Hsuan Chuang 	for (; p < end; p++) {
739e3037485SYan-Hsuan Chuang 		if (p->cond.pos) {
740e3037485SYan-Hsuan Chuang 			switch (p->cond.branch) {
741e3037485SYan-Hsuan Chuang 			case BRANCH_ENDIF:
742e3037485SYan-Hsuan Chuang 				is_matched = true;
743e3037485SYan-Hsuan Chuang 				is_skipped = false;
744e3037485SYan-Hsuan Chuang 				break;
745e3037485SYan-Hsuan Chuang 			case BRANCH_ELSE:
746e3037485SYan-Hsuan Chuang 				is_matched = is_skipped ? false : true;
747e3037485SYan-Hsuan Chuang 				break;
748e3037485SYan-Hsuan Chuang 			case BRANCH_IF:
749e3037485SYan-Hsuan Chuang 			case BRANCH_ELIF:
750e3037485SYan-Hsuan Chuang 			default:
751e3037485SYan-Hsuan Chuang 				pos_cond = p->cond;
752e3037485SYan-Hsuan Chuang 				break;
753e3037485SYan-Hsuan Chuang 			}
754e3037485SYan-Hsuan Chuang 		} else if (p->cond.neg) {
755e3037485SYan-Hsuan Chuang 			if (!is_skipped) {
756e3037485SYan-Hsuan Chuang 				if (check_positive(rtwdev, pos_cond)) {
757e3037485SYan-Hsuan Chuang 					is_matched = true;
758e3037485SYan-Hsuan Chuang 					is_skipped = true;
759e3037485SYan-Hsuan Chuang 				} else {
760e3037485SYan-Hsuan Chuang 					is_matched = false;
761e3037485SYan-Hsuan Chuang 					is_skipped = false;
762e3037485SYan-Hsuan Chuang 				}
763e3037485SYan-Hsuan Chuang 			} else {
764e3037485SYan-Hsuan Chuang 				is_matched = false;
765e3037485SYan-Hsuan Chuang 			}
766e3037485SYan-Hsuan Chuang 		} else if (is_matched) {
767e3037485SYan-Hsuan Chuang 			(*tbl->do_cfg)(rtwdev, tbl, p->cfg.addr, p->cfg.data);
768e3037485SYan-Hsuan Chuang 		}
769e3037485SYan-Hsuan Chuang 	}
770e3037485SYan-Hsuan Chuang }
771e3037485SYan-Hsuan Chuang 
772e3037485SYan-Hsuan Chuang #define bcd_to_dec_pwr_by_rate(val, i) bcd2bin(val >> (i * 8))
773e3037485SYan-Hsuan Chuang 
774e3037485SYan-Hsuan Chuang static u8 tbl_to_dec_pwr_by_rate(struct rtw_dev *rtwdev, u32 hex, u8 i)
775e3037485SYan-Hsuan Chuang {
776e3037485SYan-Hsuan Chuang 	if (rtwdev->chip->is_pwr_by_rate_dec)
777e3037485SYan-Hsuan Chuang 		return bcd_to_dec_pwr_by_rate(hex, i);
778fa6dfe6bSYan-Hsuan Chuang 
779e3037485SYan-Hsuan Chuang 	return (hex >> (i * 8)) & 0xFF;
780e3037485SYan-Hsuan Chuang }
781e3037485SYan-Hsuan Chuang 
782e3037485SYan-Hsuan Chuang static void phy_get_rate_values_of_txpwr_by_rate(struct rtw_dev *rtwdev,
783e3037485SYan-Hsuan Chuang 						 u32 addr, u32 mask,
784e3037485SYan-Hsuan Chuang 						 u32 val, u8 *rate,
785e3037485SYan-Hsuan Chuang 						 u8 *pwr_by_rate, u8 *rate_num)
786e3037485SYan-Hsuan Chuang {
787e3037485SYan-Hsuan Chuang 	int i;
788e3037485SYan-Hsuan Chuang 
789e3037485SYan-Hsuan Chuang 	switch (addr) {
790e3037485SYan-Hsuan Chuang 	case 0xE00:
791e3037485SYan-Hsuan Chuang 	case 0x830:
792e3037485SYan-Hsuan Chuang 		rate[0] = DESC_RATE6M;
793e3037485SYan-Hsuan Chuang 		rate[1] = DESC_RATE9M;
794e3037485SYan-Hsuan Chuang 		rate[2] = DESC_RATE12M;
795e3037485SYan-Hsuan Chuang 		rate[3] = DESC_RATE18M;
796e3037485SYan-Hsuan Chuang 		for (i = 0; i < 4; ++i)
797e3037485SYan-Hsuan Chuang 			pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i);
798e3037485SYan-Hsuan Chuang 		*rate_num = 4;
799e3037485SYan-Hsuan Chuang 		break;
800e3037485SYan-Hsuan Chuang 	case 0xE04:
801e3037485SYan-Hsuan Chuang 	case 0x834:
802e3037485SYan-Hsuan Chuang 		rate[0] = DESC_RATE24M;
803e3037485SYan-Hsuan Chuang 		rate[1] = DESC_RATE36M;
804e3037485SYan-Hsuan Chuang 		rate[2] = DESC_RATE48M;
805e3037485SYan-Hsuan Chuang 		rate[3] = DESC_RATE54M;
806e3037485SYan-Hsuan Chuang 		for (i = 0; i < 4; ++i)
807e3037485SYan-Hsuan Chuang 			pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i);
808e3037485SYan-Hsuan Chuang 		*rate_num = 4;
809e3037485SYan-Hsuan Chuang 		break;
810e3037485SYan-Hsuan Chuang 	case 0xE08:
811e3037485SYan-Hsuan Chuang 		rate[0] = DESC_RATE1M;
812e3037485SYan-Hsuan Chuang 		pwr_by_rate[0] = bcd_to_dec_pwr_by_rate(val, 1);
813e3037485SYan-Hsuan Chuang 		*rate_num = 1;
814e3037485SYan-Hsuan Chuang 		break;
815e3037485SYan-Hsuan Chuang 	case 0x86C:
816e3037485SYan-Hsuan Chuang 		if (mask == 0xffffff00) {
817e3037485SYan-Hsuan Chuang 			rate[0] = DESC_RATE2M;
818e3037485SYan-Hsuan Chuang 			rate[1] = DESC_RATE5_5M;
819e3037485SYan-Hsuan Chuang 			rate[2] = DESC_RATE11M;
820e3037485SYan-Hsuan Chuang 			for (i = 1; i < 4; ++i)
821e3037485SYan-Hsuan Chuang 				pwr_by_rate[i - 1] =
822e3037485SYan-Hsuan Chuang 					tbl_to_dec_pwr_by_rate(rtwdev, val, i);
823e3037485SYan-Hsuan Chuang 			*rate_num = 3;
824e3037485SYan-Hsuan Chuang 		} else if (mask == 0x000000ff) {
825e3037485SYan-Hsuan Chuang 			rate[0] = DESC_RATE11M;
826e3037485SYan-Hsuan Chuang 			pwr_by_rate[0] = bcd_to_dec_pwr_by_rate(val, 0);
827e3037485SYan-Hsuan Chuang 			*rate_num = 1;
828e3037485SYan-Hsuan Chuang 		}
829e3037485SYan-Hsuan Chuang 		break;
830e3037485SYan-Hsuan Chuang 	case 0xE10:
831e3037485SYan-Hsuan Chuang 	case 0x83C:
832e3037485SYan-Hsuan Chuang 		rate[0] = DESC_RATEMCS0;
833e3037485SYan-Hsuan Chuang 		rate[1] = DESC_RATEMCS1;
834e3037485SYan-Hsuan Chuang 		rate[2] = DESC_RATEMCS2;
835e3037485SYan-Hsuan Chuang 		rate[3] = DESC_RATEMCS3;
836e3037485SYan-Hsuan Chuang 		for (i = 0; i < 4; ++i)
837e3037485SYan-Hsuan Chuang 			pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i);
838e3037485SYan-Hsuan Chuang 		*rate_num = 4;
839e3037485SYan-Hsuan Chuang 		break;
840e3037485SYan-Hsuan Chuang 	case 0xE14:
841e3037485SYan-Hsuan Chuang 	case 0x848:
842e3037485SYan-Hsuan Chuang 		rate[0] = DESC_RATEMCS4;
843e3037485SYan-Hsuan Chuang 		rate[1] = DESC_RATEMCS5;
844e3037485SYan-Hsuan Chuang 		rate[2] = DESC_RATEMCS6;
845e3037485SYan-Hsuan Chuang 		rate[3] = DESC_RATEMCS7;
846e3037485SYan-Hsuan Chuang 		for (i = 0; i < 4; ++i)
847e3037485SYan-Hsuan Chuang 			pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i);
848e3037485SYan-Hsuan Chuang 		*rate_num = 4;
849e3037485SYan-Hsuan Chuang 		break;
850e3037485SYan-Hsuan Chuang 	case 0xE18:
851e3037485SYan-Hsuan Chuang 	case 0x84C:
852e3037485SYan-Hsuan Chuang 		rate[0] = DESC_RATEMCS8;
853e3037485SYan-Hsuan Chuang 		rate[1] = DESC_RATEMCS9;
854e3037485SYan-Hsuan Chuang 		rate[2] = DESC_RATEMCS10;
855e3037485SYan-Hsuan Chuang 		rate[3] = DESC_RATEMCS11;
856e3037485SYan-Hsuan Chuang 		for (i = 0; i < 4; ++i)
857e3037485SYan-Hsuan Chuang 			pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i);
858e3037485SYan-Hsuan Chuang 		*rate_num = 4;
859e3037485SYan-Hsuan Chuang 		break;
860e3037485SYan-Hsuan Chuang 	case 0xE1C:
861e3037485SYan-Hsuan Chuang 	case 0x868:
862e3037485SYan-Hsuan Chuang 		rate[0] = DESC_RATEMCS12;
863e3037485SYan-Hsuan Chuang 		rate[1] = DESC_RATEMCS13;
864e3037485SYan-Hsuan Chuang 		rate[2] = DESC_RATEMCS14;
865e3037485SYan-Hsuan Chuang 		rate[3] = DESC_RATEMCS15;
866e3037485SYan-Hsuan Chuang 		for (i = 0; i < 4; ++i)
867e3037485SYan-Hsuan Chuang 			pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i);
868e3037485SYan-Hsuan Chuang 		*rate_num = 4;
869e3037485SYan-Hsuan Chuang 		break;
870e3037485SYan-Hsuan Chuang 	case 0x838:
871e3037485SYan-Hsuan Chuang 		rate[0] = DESC_RATE1M;
872e3037485SYan-Hsuan Chuang 		rate[1] = DESC_RATE2M;
873e3037485SYan-Hsuan Chuang 		rate[2] = DESC_RATE5_5M;
874e3037485SYan-Hsuan Chuang 		for (i = 1; i < 4; ++i)
875e3037485SYan-Hsuan Chuang 			pwr_by_rate[i - 1] = tbl_to_dec_pwr_by_rate(rtwdev,
876e3037485SYan-Hsuan Chuang 								    val, i);
877e3037485SYan-Hsuan Chuang 		*rate_num = 3;
878e3037485SYan-Hsuan Chuang 		break;
879e3037485SYan-Hsuan Chuang 	case 0xC20:
880e3037485SYan-Hsuan Chuang 	case 0xE20:
881e3037485SYan-Hsuan Chuang 	case 0x1820:
882e3037485SYan-Hsuan Chuang 	case 0x1A20:
883e3037485SYan-Hsuan Chuang 		rate[0] = DESC_RATE1M;
884e3037485SYan-Hsuan Chuang 		rate[1] = DESC_RATE2M;
885e3037485SYan-Hsuan Chuang 		rate[2] = DESC_RATE5_5M;
886e3037485SYan-Hsuan Chuang 		rate[3] = DESC_RATE11M;
887e3037485SYan-Hsuan Chuang 		for (i = 0; i < 4; ++i)
888e3037485SYan-Hsuan Chuang 			pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i);
889e3037485SYan-Hsuan Chuang 		*rate_num = 4;
890e3037485SYan-Hsuan Chuang 		break;
891e3037485SYan-Hsuan Chuang 	case 0xC24:
892e3037485SYan-Hsuan Chuang 	case 0xE24:
893e3037485SYan-Hsuan Chuang 	case 0x1824:
894e3037485SYan-Hsuan Chuang 	case 0x1A24:
895e3037485SYan-Hsuan Chuang 		rate[0] = DESC_RATE6M;
896e3037485SYan-Hsuan Chuang 		rate[1] = DESC_RATE9M;
897e3037485SYan-Hsuan Chuang 		rate[2] = DESC_RATE12M;
898e3037485SYan-Hsuan Chuang 		rate[3] = DESC_RATE18M;
899e3037485SYan-Hsuan Chuang 		for (i = 0; i < 4; ++i)
900e3037485SYan-Hsuan Chuang 			pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i);
901e3037485SYan-Hsuan Chuang 		*rate_num = 4;
902e3037485SYan-Hsuan Chuang 		break;
903e3037485SYan-Hsuan Chuang 	case 0xC28:
904e3037485SYan-Hsuan Chuang 	case 0xE28:
905e3037485SYan-Hsuan Chuang 	case 0x1828:
906e3037485SYan-Hsuan Chuang 	case 0x1A28:
907e3037485SYan-Hsuan Chuang 		rate[0] = DESC_RATE24M;
908e3037485SYan-Hsuan Chuang 		rate[1] = DESC_RATE36M;
909e3037485SYan-Hsuan Chuang 		rate[2] = DESC_RATE48M;
910e3037485SYan-Hsuan Chuang 		rate[3] = DESC_RATE54M;
911e3037485SYan-Hsuan Chuang 		for (i = 0; i < 4; ++i)
912e3037485SYan-Hsuan Chuang 			pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i);
913e3037485SYan-Hsuan Chuang 		*rate_num = 4;
914e3037485SYan-Hsuan Chuang 		break;
915e3037485SYan-Hsuan Chuang 	case 0xC2C:
916e3037485SYan-Hsuan Chuang 	case 0xE2C:
917e3037485SYan-Hsuan Chuang 	case 0x182C:
918e3037485SYan-Hsuan Chuang 	case 0x1A2C:
919e3037485SYan-Hsuan Chuang 		rate[0] = DESC_RATEMCS0;
920e3037485SYan-Hsuan Chuang 		rate[1] = DESC_RATEMCS1;
921e3037485SYan-Hsuan Chuang 		rate[2] = DESC_RATEMCS2;
922e3037485SYan-Hsuan Chuang 		rate[3] = DESC_RATEMCS3;
923e3037485SYan-Hsuan Chuang 		for (i = 0; i < 4; ++i)
924e3037485SYan-Hsuan Chuang 			pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i);
925e3037485SYan-Hsuan Chuang 		*rate_num = 4;
926e3037485SYan-Hsuan Chuang 		break;
927e3037485SYan-Hsuan Chuang 	case 0xC30:
928e3037485SYan-Hsuan Chuang 	case 0xE30:
929e3037485SYan-Hsuan Chuang 	case 0x1830:
930e3037485SYan-Hsuan Chuang 	case 0x1A30:
931e3037485SYan-Hsuan Chuang 		rate[0] = DESC_RATEMCS4;
932e3037485SYan-Hsuan Chuang 		rate[1] = DESC_RATEMCS5;
933e3037485SYan-Hsuan Chuang 		rate[2] = DESC_RATEMCS6;
934e3037485SYan-Hsuan Chuang 		rate[3] = DESC_RATEMCS7;
935e3037485SYan-Hsuan Chuang 		for (i = 0; i < 4; ++i)
936e3037485SYan-Hsuan Chuang 			pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i);
937e3037485SYan-Hsuan Chuang 		*rate_num = 4;
938e3037485SYan-Hsuan Chuang 		break;
939e3037485SYan-Hsuan Chuang 	case 0xC34:
940e3037485SYan-Hsuan Chuang 	case 0xE34:
941e3037485SYan-Hsuan Chuang 	case 0x1834:
942e3037485SYan-Hsuan Chuang 	case 0x1A34:
943e3037485SYan-Hsuan Chuang 		rate[0] = DESC_RATEMCS8;
944e3037485SYan-Hsuan Chuang 		rate[1] = DESC_RATEMCS9;
945e3037485SYan-Hsuan Chuang 		rate[2] = DESC_RATEMCS10;
946e3037485SYan-Hsuan Chuang 		rate[3] = DESC_RATEMCS11;
947e3037485SYan-Hsuan Chuang 		for (i = 0; i < 4; ++i)
948e3037485SYan-Hsuan Chuang 			pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i);
949e3037485SYan-Hsuan Chuang 		*rate_num = 4;
950e3037485SYan-Hsuan Chuang 		break;
951e3037485SYan-Hsuan Chuang 	case 0xC38:
952e3037485SYan-Hsuan Chuang 	case 0xE38:
953e3037485SYan-Hsuan Chuang 	case 0x1838:
954e3037485SYan-Hsuan Chuang 	case 0x1A38:
955e3037485SYan-Hsuan Chuang 		rate[0] = DESC_RATEMCS12;
956e3037485SYan-Hsuan Chuang 		rate[1] = DESC_RATEMCS13;
957e3037485SYan-Hsuan Chuang 		rate[2] = DESC_RATEMCS14;
958e3037485SYan-Hsuan Chuang 		rate[3] = DESC_RATEMCS15;
959e3037485SYan-Hsuan Chuang 		for (i = 0; i < 4; ++i)
960e3037485SYan-Hsuan Chuang 			pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i);
961e3037485SYan-Hsuan Chuang 		*rate_num = 4;
962e3037485SYan-Hsuan Chuang 		break;
963e3037485SYan-Hsuan Chuang 	case 0xC3C:
964e3037485SYan-Hsuan Chuang 	case 0xE3C:
965e3037485SYan-Hsuan Chuang 	case 0x183C:
966e3037485SYan-Hsuan Chuang 	case 0x1A3C:
967e3037485SYan-Hsuan Chuang 		rate[0] = DESC_RATEVHT1SS_MCS0;
968e3037485SYan-Hsuan Chuang 		rate[1] = DESC_RATEVHT1SS_MCS1;
969e3037485SYan-Hsuan Chuang 		rate[2] = DESC_RATEVHT1SS_MCS2;
970e3037485SYan-Hsuan Chuang 		rate[3] = DESC_RATEVHT1SS_MCS3;
971e3037485SYan-Hsuan Chuang 		for (i = 0; i < 4; ++i)
972e3037485SYan-Hsuan Chuang 			pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i);
973e3037485SYan-Hsuan Chuang 		*rate_num = 4;
974e3037485SYan-Hsuan Chuang 		break;
975e3037485SYan-Hsuan Chuang 	case 0xC40:
976e3037485SYan-Hsuan Chuang 	case 0xE40:
977e3037485SYan-Hsuan Chuang 	case 0x1840:
978e3037485SYan-Hsuan Chuang 	case 0x1A40:
979e3037485SYan-Hsuan Chuang 		rate[0] = DESC_RATEVHT1SS_MCS4;
980e3037485SYan-Hsuan Chuang 		rate[1] = DESC_RATEVHT1SS_MCS5;
981e3037485SYan-Hsuan Chuang 		rate[2] = DESC_RATEVHT1SS_MCS6;
982e3037485SYan-Hsuan Chuang 		rate[3] = DESC_RATEVHT1SS_MCS7;
983e3037485SYan-Hsuan Chuang 		for (i = 0; i < 4; ++i)
984e3037485SYan-Hsuan Chuang 			pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i);
985e3037485SYan-Hsuan Chuang 		*rate_num = 4;
986e3037485SYan-Hsuan Chuang 		break;
987e3037485SYan-Hsuan Chuang 	case 0xC44:
988e3037485SYan-Hsuan Chuang 	case 0xE44:
989e3037485SYan-Hsuan Chuang 	case 0x1844:
990e3037485SYan-Hsuan Chuang 	case 0x1A44:
991e3037485SYan-Hsuan Chuang 		rate[0] = DESC_RATEVHT1SS_MCS8;
992e3037485SYan-Hsuan Chuang 		rate[1] = DESC_RATEVHT1SS_MCS9;
993e3037485SYan-Hsuan Chuang 		rate[2] = DESC_RATEVHT2SS_MCS0;
994e3037485SYan-Hsuan Chuang 		rate[3] = DESC_RATEVHT2SS_MCS1;
995e3037485SYan-Hsuan Chuang 		for (i = 0; i < 4; ++i)
996e3037485SYan-Hsuan Chuang 			pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i);
997e3037485SYan-Hsuan Chuang 		*rate_num = 4;
998e3037485SYan-Hsuan Chuang 		break;
999e3037485SYan-Hsuan Chuang 	case 0xC48:
1000e3037485SYan-Hsuan Chuang 	case 0xE48:
1001e3037485SYan-Hsuan Chuang 	case 0x1848:
1002e3037485SYan-Hsuan Chuang 	case 0x1A48:
1003e3037485SYan-Hsuan Chuang 		rate[0] = DESC_RATEVHT2SS_MCS2;
1004e3037485SYan-Hsuan Chuang 		rate[1] = DESC_RATEVHT2SS_MCS3;
1005e3037485SYan-Hsuan Chuang 		rate[2] = DESC_RATEVHT2SS_MCS4;
1006e3037485SYan-Hsuan Chuang 		rate[3] = DESC_RATEVHT2SS_MCS5;
1007e3037485SYan-Hsuan Chuang 		for (i = 0; i < 4; ++i)
1008e3037485SYan-Hsuan Chuang 			pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i);
1009e3037485SYan-Hsuan Chuang 		*rate_num = 4;
1010e3037485SYan-Hsuan Chuang 		break;
1011e3037485SYan-Hsuan Chuang 	case 0xC4C:
1012e3037485SYan-Hsuan Chuang 	case 0xE4C:
1013e3037485SYan-Hsuan Chuang 	case 0x184C:
1014e3037485SYan-Hsuan Chuang 	case 0x1A4C:
1015e3037485SYan-Hsuan Chuang 		rate[0] = DESC_RATEVHT2SS_MCS6;
1016e3037485SYan-Hsuan Chuang 		rate[1] = DESC_RATEVHT2SS_MCS7;
1017e3037485SYan-Hsuan Chuang 		rate[2] = DESC_RATEVHT2SS_MCS8;
1018e3037485SYan-Hsuan Chuang 		rate[3] = DESC_RATEVHT2SS_MCS9;
1019e3037485SYan-Hsuan Chuang 		for (i = 0; i < 4; ++i)
1020e3037485SYan-Hsuan Chuang 			pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i);
1021e3037485SYan-Hsuan Chuang 		*rate_num = 4;
1022e3037485SYan-Hsuan Chuang 		break;
1023e3037485SYan-Hsuan Chuang 	case 0xCD8:
1024e3037485SYan-Hsuan Chuang 	case 0xED8:
1025e3037485SYan-Hsuan Chuang 	case 0x18D8:
1026e3037485SYan-Hsuan Chuang 	case 0x1AD8:
1027e3037485SYan-Hsuan Chuang 		rate[0] = DESC_RATEMCS16;
1028e3037485SYan-Hsuan Chuang 		rate[1] = DESC_RATEMCS17;
1029e3037485SYan-Hsuan Chuang 		rate[2] = DESC_RATEMCS18;
1030e3037485SYan-Hsuan Chuang 		rate[3] = DESC_RATEMCS19;
1031e3037485SYan-Hsuan Chuang 		for (i = 0; i < 4; ++i)
1032e3037485SYan-Hsuan Chuang 			pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i);
1033e3037485SYan-Hsuan Chuang 		*rate_num = 4;
1034e3037485SYan-Hsuan Chuang 		break;
1035e3037485SYan-Hsuan Chuang 	case 0xCDC:
1036e3037485SYan-Hsuan Chuang 	case 0xEDC:
1037e3037485SYan-Hsuan Chuang 	case 0x18DC:
1038e3037485SYan-Hsuan Chuang 	case 0x1ADC:
1039e3037485SYan-Hsuan Chuang 		rate[0] = DESC_RATEMCS20;
1040e3037485SYan-Hsuan Chuang 		rate[1] = DESC_RATEMCS21;
1041e3037485SYan-Hsuan Chuang 		rate[2] = DESC_RATEMCS22;
1042e3037485SYan-Hsuan Chuang 		rate[3] = DESC_RATEMCS23;
1043e3037485SYan-Hsuan Chuang 		for (i = 0; i < 4; ++i)
1044e3037485SYan-Hsuan Chuang 			pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i);
1045e3037485SYan-Hsuan Chuang 		*rate_num = 4;
1046e3037485SYan-Hsuan Chuang 		break;
1047e3037485SYan-Hsuan Chuang 	case 0xCE0:
1048e3037485SYan-Hsuan Chuang 	case 0xEE0:
1049e3037485SYan-Hsuan Chuang 	case 0x18E0:
1050e3037485SYan-Hsuan Chuang 	case 0x1AE0:
1051e3037485SYan-Hsuan Chuang 		rate[0] = DESC_RATEVHT3SS_MCS0;
1052e3037485SYan-Hsuan Chuang 		rate[1] = DESC_RATEVHT3SS_MCS1;
1053e3037485SYan-Hsuan Chuang 		rate[2] = DESC_RATEVHT3SS_MCS2;
1054e3037485SYan-Hsuan Chuang 		rate[3] = DESC_RATEVHT3SS_MCS3;
1055e3037485SYan-Hsuan Chuang 		for (i = 0; i < 4; ++i)
1056e3037485SYan-Hsuan Chuang 			pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i);
1057e3037485SYan-Hsuan Chuang 		*rate_num = 4;
1058e3037485SYan-Hsuan Chuang 		break;
1059e3037485SYan-Hsuan Chuang 	case 0xCE4:
1060e3037485SYan-Hsuan Chuang 	case 0xEE4:
1061e3037485SYan-Hsuan Chuang 	case 0x18E4:
1062e3037485SYan-Hsuan Chuang 	case 0x1AE4:
1063e3037485SYan-Hsuan Chuang 		rate[0] = DESC_RATEVHT3SS_MCS4;
1064e3037485SYan-Hsuan Chuang 		rate[1] = DESC_RATEVHT3SS_MCS5;
1065e3037485SYan-Hsuan Chuang 		rate[2] = DESC_RATEVHT3SS_MCS6;
1066e3037485SYan-Hsuan Chuang 		rate[3] = DESC_RATEVHT3SS_MCS7;
1067e3037485SYan-Hsuan Chuang 		for (i = 0; i < 4; ++i)
1068e3037485SYan-Hsuan Chuang 			pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i);
1069e3037485SYan-Hsuan Chuang 		*rate_num = 4;
1070e3037485SYan-Hsuan Chuang 		break;
1071e3037485SYan-Hsuan Chuang 	case 0xCE8:
1072e3037485SYan-Hsuan Chuang 	case 0xEE8:
1073e3037485SYan-Hsuan Chuang 	case 0x18E8:
1074e3037485SYan-Hsuan Chuang 	case 0x1AE8:
1075e3037485SYan-Hsuan Chuang 		rate[0] = DESC_RATEVHT3SS_MCS8;
1076e3037485SYan-Hsuan Chuang 		rate[1] = DESC_RATEVHT3SS_MCS9;
1077e3037485SYan-Hsuan Chuang 		for (i = 0; i < 2; ++i)
1078e3037485SYan-Hsuan Chuang 			pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i);
1079e3037485SYan-Hsuan Chuang 		*rate_num = 2;
1080e3037485SYan-Hsuan Chuang 		break;
1081e3037485SYan-Hsuan Chuang 	default:
1082e3037485SYan-Hsuan Chuang 		rtw_warn(rtwdev, "invalid tx power index addr 0x%08x\n", addr);
1083e3037485SYan-Hsuan Chuang 		break;
1084e3037485SYan-Hsuan Chuang 	}
1085e3037485SYan-Hsuan Chuang }
1086e3037485SYan-Hsuan Chuang 
1087*226746fdSYan-Hsuan Chuang static void phy_store_tx_power_by_rate(struct rtw_dev *rtwdev,
1088fa6dfe6bSYan-Hsuan Chuang 				       u32 band, u32 rfpath, u32 txnum,
1089e3037485SYan-Hsuan Chuang 				       u32 regaddr, u32 bitmask, u32 data)
1090e3037485SYan-Hsuan Chuang {
1091e3037485SYan-Hsuan Chuang 	struct rtw_hal *hal = &rtwdev->hal;
1092e3037485SYan-Hsuan Chuang 	u8 rate_num = 0;
1093e3037485SYan-Hsuan Chuang 	u8 rate;
1094e3037485SYan-Hsuan Chuang 	u8 rates[RTW_RF_PATH_MAX] = {0};
1095e3037485SYan-Hsuan Chuang 	s8 offset;
1096e3037485SYan-Hsuan Chuang 	s8 pwr_by_rate[RTW_RF_PATH_MAX] = {0};
1097e3037485SYan-Hsuan Chuang 	int i;
1098e3037485SYan-Hsuan Chuang 
1099e3037485SYan-Hsuan Chuang 	phy_get_rate_values_of_txpwr_by_rate(rtwdev, regaddr, bitmask, data,
1100e3037485SYan-Hsuan Chuang 					     rates, pwr_by_rate, &rate_num);
1101e3037485SYan-Hsuan Chuang 
1102e3037485SYan-Hsuan Chuang 	if (WARN_ON(rfpath >= RTW_RF_PATH_MAX ||
1103e3037485SYan-Hsuan Chuang 		    (band != PHY_BAND_2G && band != PHY_BAND_5G) ||
1104e3037485SYan-Hsuan Chuang 		    rate_num > RTW_RF_PATH_MAX))
1105e3037485SYan-Hsuan Chuang 		return;
1106e3037485SYan-Hsuan Chuang 
1107e3037485SYan-Hsuan Chuang 	for (i = 0; i < rate_num; i++) {
1108e3037485SYan-Hsuan Chuang 		offset = pwr_by_rate[i];
1109e3037485SYan-Hsuan Chuang 		rate = rates[i];
1110e3037485SYan-Hsuan Chuang 		if (band == PHY_BAND_2G)
1111e3037485SYan-Hsuan Chuang 			hal->tx_pwr_by_rate_offset_2g[rfpath][rate] = offset;
1112e3037485SYan-Hsuan Chuang 		else if (band == PHY_BAND_5G)
1113e3037485SYan-Hsuan Chuang 			hal->tx_pwr_by_rate_offset_5g[rfpath][rate] = offset;
1114e3037485SYan-Hsuan Chuang 		else
1115e3037485SYan-Hsuan Chuang 			continue;
1116e3037485SYan-Hsuan Chuang 	}
1117e3037485SYan-Hsuan Chuang }
1118e3037485SYan-Hsuan Chuang 
1119fa6dfe6bSYan-Hsuan Chuang void rtw_parse_tbl_bb_pg(struct rtw_dev *rtwdev, const struct rtw_table *tbl)
1120fa6dfe6bSYan-Hsuan Chuang {
1121fa6dfe6bSYan-Hsuan Chuang 	const struct phy_pg_cfg_pair *p = tbl->data;
1122fa6dfe6bSYan-Hsuan Chuang 	const struct phy_pg_cfg_pair *end = p + tbl->size / 6;
1123fa6dfe6bSYan-Hsuan Chuang 
1124fa6dfe6bSYan-Hsuan Chuang 	BUILD_BUG_ON(sizeof(struct phy_pg_cfg_pair) != sizeof(u32) * 6);
1125fa6dfe6bSYan-Hsuan Chuang 
1126fa6dfe6bSYan-Hsuan Chuang 	for (; p < end; p++) {
1127fa6dfe6bSYan-Hsuan Chuang 		if (p->addr == 0xfe || p->addr == 0xffe) {
1128fa6dfe6bSYan-Hsuan Chuang 			msleep(50);
1129fa6dfe6bSYan-Hsuan Chuang 			continue;
1130fa6dfe6bSYan-Hsuan Chuang 		}
1131fa6dfe6bSYan-Hsuan Chuang 		phy_store_tx_power_by_rate(rtwdev, p->band, p->rf_path,
1132fa6dfe6bSYan-Hsuan Chuang 					   p->tx_num, p->addr, p->bitmask,
1133fa6dfe6bSYan-Hsuan Chuang 					   p->data);
1134fa6dfe6bSYan-Hsuan Chuang 	}
1135fa6dfe6bSYan-Hsuan Chuang }
1136fa6dfe6bSYan-Hsuan Chuang 
1137fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_channel_idx_5g[RTW_MAX_CHANNEL_NUM_5G] = {
1138fa6dfe6bSYan-Hsuan Chuang 	36,  38,  40,  42,  44,  46,  48, /* Band 1 */
1139fa6dfe6bSYan-Hsuan Chuang 	52,  54,  56,  58,  60,  62,  64, /* Band 2 */
1140fa6dfe6bSYan-Hsuan Chuang 	100, 102, 104, 106, 108, 110, 112, /* Band 3 */
1141fa6dfe6bSYan-Hsuan Chuang 	116, 118, 120, 122, 124, 126, 128, /* Band 3 */
1142fa6dfe6bSYan-Hsuan Chuang 	132, 134, 136, 138, 140, 142, 144, /* Band 3 */
1143fa6dfe6bSYan-Hsuan Chuang 	149, 151, 153, 155, 157, 159, 161, /* Band 4 */
1144fa6dfe6bSYan-Hsuan Chuang 	165, 167, 169, 171, 173, 175, 177}; /* Band 4 */
1145fa6dfe6bSYan-Hsuan Chuang 
1146fa6dfe6bSYan-Hsuan Chuang static int rtw_channel_to_idx(u8 band, u8 channel)
1147fa6dfe6bSYan-Hsuan Chuang {
1148fa6dfe6bSYan-Hsuan Chuang 	int ch_idx;
1149fa6dfe6bSYan-Hsuan Chuang 	u8 n_channel;
1150fa6dfe6bSYan-Hsuan Chuang 
1151fa6dfe6bSYan-Hsuan Chuang 	if (band == PHY_BAND_2G) {
1152fa6dfe6bSYan-Hsuan Chuang 		ch_idx = channel - 1;
1153fa6dfe6bSYan-Hsuan Chuang 		n_channel = RTW_MAX_CHANNEL_NUM_2G;
1154fa6dfe6bSYan-Hsuan Chuang 	} else if (band == PHY_BAND_5G) {
1155fa6dfe6bSYan-Hsuan Chuang 		n_channel = RTW_MAX_CHANNEL_NUM_5G;
1156fa6dfe6bSYan-Hsuan Chuang 		for (ch_idx = 0; ch_idx < n_channel; ch_idx++)
1157fa6dfe6bSYan-Hsuan Chuang 			if (rtw_channel_idx_5g[ch_idx] == channel)
1158fa6dfe6bSYan-Hsuan Chuang 				break;
1159fa6dfe6bSYan-Hsuan Chuang 	} else {
1160fa6dfe6bSYan-Hsuan Chuang 		return -1;
1161fa6dfe6bSYan-Hsuan Chuang 	}
1162fa6dfe6bSYan-Hsuan Chuang 
1163fa6dfe6bSYan-Hsuan Chuang 	if (ch_idx >= n_channel)
1164fa6dfe6bSYan-Hsuan Chuang 		return -1;
1165fa6dfe6bSYan-Hsuan Chuang 
1166fa6dfe6bSYan-Hsuan Chuang 	return ch_idx;
1167fa6dfe6bSYan-Hsuan Chuang }
1168fa6dfe6bSYan-Hsuan Chuang 
1169fa6dfe6bSYan-Hsuan Chuang static void phy_set_tx_power_limit(struct rtw_dev *rtwdev, u8 regd, u8 band,
1170fa6dfe6bSYan-Hsuan Chuang 				   u8 bw, u8 rs, u8 ch, s8 pwr_limit)
1171fa6dfe6bSYan-Hsuan Chuang {
1172fa6dfe6bSYan-Hsuan Chuang 	struct rtw_hal *hal = &rtwdev->hal;
1173fa6dfe6bSYan-Hsuan Chuang 	int ch_idx;
1174fa6dfe6bSYan-Hsuan Chuang 
1175fa6dfe6bSYan-Hsuan Chuang 	pwr_limit = clamp_t(s8, pwr_limit,
1176fa6dfe6bSYan-Hsuan Chuang 			    -RTW_MAX_POWER_INDEX, RTW_MAX_POWER_INDEX);
1177fa6dfe6bSYan-Hsuan Chuang 	ch_idx = rtw_channel_to_idx(band, ch);
1178fa6dfe6bSYan-Hsuan Chuang 
1179fa6dfe6bSYan-Hsuan Chuang 	if (regd >= RTW_REGD_MAX || bw >= RTW_CHANNEL_WIDTH_MAX ||
1180fa6dfe6bSYan-Hsuan Chuang 	    rs >= RTW_RATE_SECTION_MAX || ch_idx < 0) {
1181fa6dfe6bSYan-Hsuan Chuang 		WARN(1,
1182fa6dfe6bSYan-Hsuan Chuang 		     "wrong txpwr_lmt regd=%u, band=%u bw=%u, rs=%u, ch_idx=%u, pwr_limit=%d\n",
1183fa6dfe6bSYan-Hsuan Chuang 		     regd, band, bw, rs, ch_idx, pwr_limit);
1184fa6dfe6bSYan-Hsuan Chuang 		return;
1185fa6dfe6bSYan-Hsuan Chuang 	}
1186fa6dfe6bSYan-Hsuan Chuang 
1187fa6dfe6bSYan-Hsuan Chuang 	if (band == PHY_BAND_2G)
1188fa6dfe6bSYan-Hsuan Chuang 		hal->tx_pwr_limit_2g[regd][bw][rs][ch_idx] = pwr_limit;
1189fa6dfe6bSYan-Hsuan Chuang 	else if (band == PHY_BAND_5G)
1190fa6dfe6bSYan-Hsuan Chuang 		hal->tx_pwr_limit_5g[regd][bw][rs][ch_idx] = pwr_limit;
1191fa6dfe6bSYan-Hsuan Chuang }
1192fa6dfe6bSYan-Hsuan Chuang 
1193fa6dfe6bSYan-Hsuan Chuang void rtw_parse_tbl_txpwr_lmt(struct rtw_dev *rtwdev,
1194fa6dfe6bSYan-Hsuan Chuang 			     const struct rtw_table *tbl)
1195fa6dfe6bSYan-Hsuan Chuang {
1196fa6dfe6bSYan-Hsuan Chuang 	const struct txpwr_lmt_cfg_pair *p = tbl->data;
1197fa6dfe6bSYan-Hsuan Chuang 	const struct txpwr_lmt_cfg_pair *end = p + tbl->size / 6;
1198fa6dfe6bSYan-Hsuan Chuang 
1199fa6dfe6bSYan-Hsuan Chuang 	BUILD_BUG_ON(sizeof(struct txpwr_lmt_cfg_pair) != sizeof(u8) * 6);
1200fa6dfe6bSYan-Hsuan Chuang 
1201fa6dfe6bSYan-Hsuan Chuang 	for (; p < end; p++) {
1202fa6dfe6bSYan-Hsuan Chuang 		phy_set_tx_power_limit(rtwdev, p->regd, p->band,
1203fa6dfe6bSYan-Hsuan Chuang 				       p->bw, p->rs,
1204fa6dfe6bSYan-Hsuan Chuang 				       p->ch, p->txpwr_lmt);
1205fa6dfe6bSYan-Hsuan Chuang 	}
1206fa6dfe6bSYan-Hsuan Chuang }
1207fa6dfe6bSYan-Hsuan Chuang 
1208fa6dfe6bSYan-Hsuan Chuang void rtw_phy_cfg_mac(struct rtw_dev *rtwdev, const struct rtw_table *tbl,
1209fa6dfe6bSYan-Hsuan Chuang 		     u32 addr, u32 data)
1210fa6dfe6bSYan-Hsuan Chuang {
1211fa6dfe6bSYan-Hsuan Chuang 	rtw_write8(rtwdev, addr, data);
1212fa6dfe6bSYan-Hsuan Chuang }
1213fa6dfe6bSYan-Hsuan Chuang 
1214fa6dfe6bSYan-Hsuan Chuang void rtw_phy_cfg_agc(struct rtw_dev *rtwdev, const struct rtw_table *tbl,
1215fa6dfe6bSYan-Hsuan Chuang 		     u32 addr, u32 data)
1216fa6dfe6bSYan-Hsuan Chuang {
1217fa6dfe6bSYan-Hsuan Chuang 	rtw_write32(rtwdev, addr, data);
1218fa6dfe6bSYan-Hsuan Chuang }
1219fa6dfe6bSYan-Hsuan Chuang 
1220fa6dfe6bSYan-Hsuan Chuang void rtw_phy_cfg_bb(struct rtw_dev *rtwdev, const struct rtw_table *tbl,
1221fa6dfe6bSYan-Hsuan Chuang 		    u32 addr, u32 data)
1222fa6dfe6bSYan-Hsuan Chuang {
1223fa6dfe6bSYan-Hsuan Chuang 	if (addr == 0xfe)
1224fa6dfe6bSYan-Hsuan Chuang 		msleep(50);
1225fa6dfe6bSYan-Hsuan Chuang 	else if (addr == 0xfd)
1226fa6dfe6bSYan-Hsuan Chuang 		mdelay(5);
1227fa6dfe6bSYan-Hsuan Chuang 	else if (addr == 0xfc)
1228fa6dfe6bSYan-Hsuan Chuang 		mdelay(1);
1229fa6dfe6bSYan-Hsuan Chuang 	else if (addr == 0xfb)
1230fa6dfe6bSYan-Hsuan Chuang 		usleep_range(50, 60);
1231fa6dfe6bSYan-Hsuan Chuang 	else if (addr == 0xfa)
1232fa6dfe6bSYan-Hsuan Chuang 		udelay(5);
1233fa6dfe6bSYan-Hsuan Chuang 	else if (addr == 0xf9)
1234fa6dfe6bSYan-Hsuan Chuang 		udelay(1);
1235fa6dfe6bSYan-Hsuan Chuang 	else
1236fa6dfe6bSYan-Hsuan Chuang 		rtw_write32(rtwdev, addr, data);
1237fa6dfe6bSYan-Hsuan Chuang }
1238fa6dfe6bSYan-Hsuan Chuang 
1239fa6dfe6bSYan-Hsuan Chuang void rtw_phy_cfg_rf(struct rtw_dev *rtwdev, const struct rtw_table *tbl,
1240fa6dfe6bSYan-Hsuan Chuang 		    u32 addr, u32 data)
1241fa6dfe6bSYan-Hsuan Chuang {
1242fa6dfe6bSYan-Hsuan Chuang 	if (addr == 0xffe) {
1243fa6dfe6bSYan-Hsuan Chuang 		msleep(50);
1244fa6dfe6bSYan-Hsuan Chuang 	} else if (addr == 0xfe) {
1245fa6dfe6bSYan-Hsuan Chuang 		usleep_range(100, 110);
1246fa6dfe6bSYan-Hsuan Chuang 	} else {
1247fa6dfe6bSYan-Hsuan Chuang 		rtw_write_rf(rtwdev, tbl->rf_path, addr, RFREG_MASK, data);
1248fa6dfe6bSYan-Hsuan Chuang 		udelay(1);
1249fa6dfe6bSYan-Hsuan Chuang 	}
1250fa6dfe6bSYan-Hsuan Chuang }
1251fa6dfe6bSYan-Hsuan Chuang 
1252fa6dfe6bSYan-Hsuan Chuang static void rtw_load_rfk_table(struct rtw_dev *rtwdev)
1253fa6dfe6bSYan-Hsuan Chuang {
1254fa6dfe6bSYan-Hsuan Chuang 	struct rtw_chip_info *chip = rtwdev->chip;
1255fa6dfe6bSYan-Hsuan Chuang 
1256fa6dfe6bSYan-Hsuan Chuang 	if (!chip->rfk_init_tbl)
1257fa6dfe6bSYan-Hsuan Chuang 		return;
1258fa6dfe6bSYan-Hsuan Chuang 
1259fa6dfe6bSYan-Hsuan Chuang 	rtw_load_table(rtwdev, chip->rfk_init_tbl);
1260fa6dfe6bSYan-Hsuan Chuang }
1261fa6dfe6bSYan-Hsuan Chuang 
1262fa6dfe6bSYan-Hsuan Chuang void rtw_phy_load_tables(struct rtw_dev *rtwdev)
1263fa6dfe6bSYan-Hsuan Chuang {
1264fa6dfe6bSYan-Hsuan Chuang 	struct rtw_chip_info *chip = rtwdev->chip;
1265fa6dfe6bSYan-Hsuan Chuang 	u8 rf_path;
1266fa6dfe6bSYan-Hsuan Chuang 
1267fa6dfe6bSYan-Hsuan Chuang 	rtw_load_table(rtwdev, chip->mac_tbl);
1268fa6dfe6bSYan-Hsuan Chuang 	rtw_load_table(rtwdev, chip->bb_tbl);
1269fa6dfe6bSYan-Hsuan Chuang 	rtw_load_table(rtwdev, chip->agc_tbl);
1270fa6dfe6bSYan-Hsuan Chuang 	rtw_load_rfk_table(rtwdev);
1271fa6dfe6bSYan-Hsuan Chuang 
1272fa6dfe6bSYan-Hsuan Chuang 	for (rf_path = 0; rf_path < rtwdev->hal.rf_path_num; rf_path++) {
1273fa6dfe6bSYan-Hsuan Chuang 		const struct rtw_table *tbl;
1274fa6dfe6bSYan-Hsuan Chuang 
1275fa6dfe6bSYan-Hsuan Chuang 		tbl = chip->rf_tbl[rf_path];
1276fa6dfe6bSYan-Hsuan Chuang 		rtw_load_table(rtwdev, tbl);
1277fa6dfe6bSYan-Hsuan Chuang 	}
1278fa6dfe6bSYan-Hsuan Chuang }
1279fa6dfe6bSYan-Hsuan Chuang 
1280fa6dfe6bSYan-Hsuan Chuang static u8 rtw_get_channel_group(u8 channel)
1281fa6dfe6bSYan-Hsuan Chuang {
1282fa6dfe6bSYan-Hsuan Chuang 	switch (channel) {
1283fa6dfe6bSYan-Hsuan Chuang 	default:
1284fa6dfe6bSYan-Hsuan Chuang 		WARN_ON(1);
1285fa6dfe6bSYan-Hsuan Chuang 		/* fall through */
1286fa6dfe6bSYan-Hsuan Chuang 	case 1:
1287fa6dfe6bSYan-Hsuan Chuang 	case 2:
1288fa6dfe6bSYan-Hsuan Chuang 	case 36:
1289fa6dfe6bSYan-Hsuan Chuang 	case 38:
1290fa6dfe6bSYan-Hsuan Chuang 	case 40:
1291fa6dfe6bSYan-Hsuan Chuang 	case 42:
1292fa6dfe6bSYan-Hsuan Chuang 		return 0;
1293fa6dfe6bSYan-Hsuan Chuang 	case 3:
1294fa6dfe6bSYan-Hsuan Chuang 	case 4:
1295fa6dfe6bSYan-Hsuan Chuang 	case 5:
1296fa6dfe6bSYan-Hsuan Chuang 	case 44:
1297fa6dfe6bSYan-Hsuan Chuang 	case 46:
1298fa6dfe6bSYan-Hsuan Chuang 	case 48:
1299fa6dfe6bSYan-Hsuan Chuang 	case 50:
1300fa6dfe6bSYan-Hsuan Chuang 		return 1;
1301fa6dfe6bSYan-Hsuan Chuang 	case 6:
1302fa6dfe6bSYan-Hsuan Chuang 	case 7:
1303fa6dfe6bSYan-Hsuan Chuang 	case 8:
1304fa6dfe6bSYan-Hsuan Chuang 	case 52:
1305fa6dfe6bSYan-Hsuan Chuang 	case 54:
1306fa6dfe6bSYan-Hsuan Chuang 	case 56:
1307fa6dfe6bSYan-Hsuan Chuang 	case 58:
1308fa6dfe6bSYan-Hsuan Chuang 		return 2;
1309fa6dfe6bSYan-Hsuan Chuang 	case 9:
1310fa6dfe6bSYan-Hsuan Chuang 	case 10:
1311fa6dfe6bSYan-Hsuan Chuang 	case 11:
1312fa6dfe6bSYan-Hsuan Chuang 	case 60:
1313fa6dfe6bSYan-Hsuan Chuang 	case 62:
1314fa6dfe6bSYan-Hsuan Chuang 	case 64:
1315fa6dfe6bSYan-Hsuan Chuang 		return 3;
1316fa6dfe6bSYan-Hsuan Chuang 	case 12:
1317fa6dfe6bSYan-Hsuan Chuang 	case 13:
1318fa6dfe6bSYan-Hsuan Chuang 	case 100:
1319fa6dfe6bSYan-Hsuan Chuang 	case 102:
1320fa6dfe6bSYan-Hsuan Chuang 	case 104:
1321fa6dfe6bSYan-Hsuan Chuang 	case 106:
1322fa6dfe6bSYan-Hsuan Chuang 		return 4;
1323fa6dfe6bSYan-Hsuan Chuang 	case 14:
1324fa6dfe6bSYan-Hsuan Chuang 	case 108:
1325fa6dfe6bSYan-Hsuan Chuang 	case 110:
1326fa6dfe6bSYan-Hsuan Chuang 	case 112:
1327fa6dfe6bSYan-Hsuan Chuang 	case 114:
1328fa6dfe6bSYan-Hsuan Chuang 		return 5;
1329fa6dfe6bSYan-Hsuan Chuang 	case 116:
1330fa6dfe6bSYan-Hsuan Chuang 	case 118:
1331fa6dfe6bSYan-Hsuan Chuang 	case 120:
1332fa6dfe6bSYan-Hsuan Chuang 	case 122:
1333fa6dfe6bSYan-Hsuan Chuang 		return 6;
1334fa6dfe6bSYan-Hsuan Chuang 	case 124:
1335fa6dfe6bSYan-Hsuan Chuang 	case 126:
1336fa6dfe6bSYan-Hsuan Chuang 	case 128:
1337fa6dfe6bSYan-Hsuan Chuang 	case 130:
1338fa6dfe6bSYan-Hsuan Chuang 		return 7;
1339fa6dfe6bSYan-Hsuan Chuang 	case 132:
1340fa6dfe6bSYan-Hsuan Chuang 	case 134:
1341fa6dfe6bSYan-Hsuan Chuang 	case 136:
1342fa6dfe6bSYan-Hsuan Chuang 	case 138:
1343fa6dfe6bSYan-Hsuan Chuang 		return 8;
1344fa6dfe6bSYan-Hsuan Chuang 	case 140:
1345fa6dfe6bSYan-Hsuan Chuang 	case 142:
1346fa6dfe6bSYan-Hsuan Chuang 	case 144:
1347fa6dfe6bSYan-Hsuan Chuang 		return 9;
1348fa6dfe6bSYan-Hsuan Chuang 	case 149:
1349fa6dfe6bSYan-Hsuan Chuang 	case 151:
1350fa6dfe6bSYan-Hsuan Chuang 	case 153:
1351fa6dfe6bSYan-Hsuan Chuang 	case 155:
1352fa6dfe6bSYan-Hsuan Chuang 		return 10;
1353fa6dfe6bSYan-Hsuan Chuang 	case 157:
1354fa6dfe6bSYan-Hsuan Chuang 	case 159:
1355fa6dfe6bSYan-Hsuan Chuang 	case 161:
1356fa6dfe6bSYan-Hsuan Chuang 		return 11;
1357fa6dfe6bSYan-Hsuan Chuang 	case 165:
1358fa6dfe6bSYan-Hsuan Chuang 	case 167:
1359fa6dfe6bSYan-Hsuan Chuang 	case 169:
1360fa6dfe6bSYan-Hsuan Chuang 	case 171:
1361fa6dfe6bSYan-Hsuan Chuang 		return 12;
1362fa6dfe6bSYan-Hsuan Chuang 	case 173:
1363fa6dfe6bSYan-Hsuan Chuang 	case 175:
1364fa6dfe6bSYan-Hsuan Chuang 	case 177:
1365fa6dfe6bSYan-Hsuan Chuang 		return 13;
1366fa6dfe6bSYan-Hsuan Chuang 	}
1367fa6dfe6bSYan-Hsuan Chuang }
1368fa6dfe6bSYan-Hsuan Chuang 
1369fa6dfe6bSYan-Hsuan Chuang static u8 phy_get_2g_tx_power_index(struct rtw_dev *rtwdev,
1370fa6dfe6bSYan-Hsuan Chuang 				    struct rtw_2g_txpwr_idx *pwr_idx_2g,
1371fa6dfe6bSYan-Hsuan Chuang 				    enum rtw_bandwidth bandwidth,
1372fa6dfe6bSYan-Hsuan Chuang 				    u8 rate, u8 group)
1373fa6dfe6bSYan-Hsuan Chuang {
1374fa6dfe6bSYan-Hsuan Chuang 	struct rtw_chip_info *chip = rtwdev->chip;
1375fa6dfe6bSYan-Hsuan Chuang 	u8 tx_power;
1376fa6dfe6bSYan-Hsuan Chuang 	bool mcs_rate;
1377fa6dfe6bSYan-Hsuan Chuang 	bool above_2ss;
1378fa6dfe6bSYan-Hsuan Chuang 	u8 factor = chip->txgi_factor;
1379fa6dfe6bSYan-Hsuan Chuang 
1380fa6dfe6bSYan-Hsuan Chuang 	if (rate <= DESC_RATE11M)
1381fa6dfe6bSYan-Hsuan Chuang 		tx_power = pwr_idx_2g->cck_base[group];
1382fa6dfe6bSYan-Hsuan Chuang 	else
1383fa6dfe6bSYan-Hsuan Chuang 		tx_power = pwr_idx_2g->bw40_base[group];
1384fa6dfe6bSYan-Hsuan Chuang 
1385fa6dfe6bSYan-Hsuan Chuang 	if (rate >= DESC_RATE6M && rate <= DESC_RATE54M)
1386fa6dfe6bSYan-Hsuan Chuang 		tx_power += pwr_idx_2g->ht_1s_diff.ofdm * factor;
1387fa6dfe6bSYan-Hsuan Chuang 
1388fa6dfe6bSYan-Hsuan Chuang 	mcs_rate = (rate >= DESC_RATEMCS0 && rate <= DESC_RATEMCS15) ||
1389fa6dfe6bSYan-Hsuan Chuang 		   (rate >= DESC_RATEVHT1SS_MCS0 &&
1390fa6dfe6bSYan-Hsuan Chuang 		    rate <= DESC_RATEVHT2SS_MCS9);
1391fa6dfe6bSYan-Hsuan Chuang 	above_2ss = (rate >= DESC_RATEMCS8 && rate <= DESC_RATEMCS15) ||
1392fa6dfe6bSYan-Hsuan Chuang 		    (rate >= DESC_RATEVHT2SS_MCS0);
1393fa6dfe6bSYan-Hsuan Chuang 
1394fa6dfe6bSYan-Hsuan Chuang 	if (!mcs_rate)
1395fa6dfe6bSYan-Hsuan Chuang 		return tx_power;
1396fa6dfe6bSYan-Hsuan Chuang 
1397fa6dfe6bSYan-Hsuan Chuang 	switch (bandwidth) {
1398fa6dfe6bSYan-Hsuan Chuang 	default:
1399fa6dfe6bSYan-Hsuan Chuang 		WARN_ON(1);
1400fa6dfe6bSYan-Hsuan Chuang 		/* fall through */
1401fa6dfe6bSYan-Hsuan Chuang 	case RTW_CHANNEL_WIDTH_20:
1402fa6dfe6bSYan-Hsuan Chuang 		tx_power += pwr_idx_2g->ht_1s_diff.bw20 * factor;
1403fa6dfe6bSYan-Hsuan Chuang 		if (above_2ss)
1404fa6dfe6bSYan-Hsuan Chuang 			tx_power += pwr_idx_2g->ht_2s_diff.bw20 * factor;
1405fa6dfe6bSYan-Hsuan Chuang 		break;
1406fa6dfe6bSYan-Hsuan Chuang 	case RTW_CHANNEL_WIDTH_40:
1407fa6dfe6bSYan-Hsuan Chuang 		/* bw40 is the base power */
1408fa6dfe6bSYan-Hsuan Chuang 		if (above_2ss)
1409fa6dfe6bSYan-Hsuan Chuang 			tx_power += pwr_idx_2g->ht_2s_diff.bw40 * factor;
1410fa6dfe6bSYan-Hsuan Chuang 		break;
1411fa6dfe6bSYan-Hsuan Chuang 	}
1412fa6dfe6bSYan-Hsuan Chuang 
1413fa6dfe6bSYan-Hsuan Chuang 	return tx_power;
1414fa6dfe6bSYan-Hsuan Chuang }
1415fa6dfe6bSYan-Hsuan Chuang 
1416fa6dfe6bSYan-Hsuan Chuang static u8 phy_get_5g_tx_power_index(struct rtw_dev *rtwdev,
1417fa6dfe6bSYan-Hsuan Chuang 				    struct rtw_5g_txpwr_idx *pwr_idx_5g,
1418fa6dfe6bSYan-Hsuan Chuang 				    enum rtw_bandwidth bandwidth,
1419fa6dfe6bSYan-Hsuan Chuang 				    u8 rate, u8 group)
1420fa6dfe6bSYan-Hsuan Chuang {
1421fa6dfe6bSYan-Hsuan Chuang 	struct rtw_chip_info *chip = rtwdev->chip;
1422fa6dfe6bSYan-Hsuan Chuang 	u8 tx_power;
1423fa6dfe6bSYan-Hsuan Chuang 	u8 upper, lower;
1424fa6dfe6bSYan-Hsuan Chuang 	bool mcs_rate;
1425fa6dfe6bSYan-Hsuan Chuang 	bool above_2ss;
1426fa6dfe6bSYan-Hsuan Chuang 	u8 factor = chip->txgi_factor;
1427fa6dfe6bSYan-Hsuan Chuang 
1428fa6dfe6bSYan-Hsuan Chuang 	tx_power = pwr_idx_5g->bw40_base[group];
1429fa6dfe6bSYan-Hsuan Chuang 
1430fa6dfe6bSYan-Hsuan Chuang 	mcs_rate = (rate >= DESC_RATEMCS0 && rate <= DESC_RATEMCS15) ||
1431fa6dfe6bSYan-Hsuan Chuang 		   (rate >= DESC_RATEVHT1SS_MCS0 &&
1432fa6dfe6bSYan-Hsuan Chuang 		    rate <= DESC_RATEVHT2SS_MCS9);
1433fa6dfe6bSYan-Hsuan Chuang 	above_2ss = (rate >= DESC_RATEMCS8 && rate <= DESC_RATEMCS15) ||
1434fa6dfe6bSYan-Hsuan Chuang 		    (rate >= DESC_RATEVHT2SS_MCS0);
1435fa6dfe6bSYan-Hsuan Chuang 
1436fa6dfe6bSYan-Hsuan Chuang 	if (!mcs_rate) {
1437fa6dfe6bSYan-Hsuan Chuang 		tx_power += pwr_idx_5g->ht_1s_diff.ofdm * factor;
1438fa6dfe6bSYan-Hsuan Chuang 		return tx_power;
1439fa6dfe6bSYan-Hsuan Chuang 	}
1440fa6dfe6bSYan-Hsuan Chuang 
1441fa6dfe6bSYan-Hsuan Chuang 	switch (bandwidth) {
1442fa6dfe6bSYan-Hsuan Chuang 	default:
1443fa6dfe6bSYan-Hsuan Chuang 		WARN_ON(1);
1444fa6dfe6bSYan-Hsuan Chuang 		/* fall through */
1445fa6dfe6bSYan-Hsuan Chuang 	case RTW_CHANNEL_WIDTH_20:
1446fa6dfe6bSYan-Hsuan Chuang 		tx_power += pwr_idx_5g->ht_1s_diff.bw20 * factor;
1447fa6dfe6bSYan-Hsuan Chuang 		if (above_2ss)
1448fa6dfe6bSYan-Hsuan Chuang 			tx_power += pwr_idx_5g->ht_2s_diff.bw20 * factor;
1449fa6dfe6bSYan-Hsuan Chuang 		break;
1450fa6dfe6bSYan-Hsuan Chuang 	case RTW_CHANNEL_WIDTH_40:
1451fa6dfe6bSYan-Hsuan Chuang 		/* bw40 is the base power */
1452fa6dfe6bSYan-Hsuan Chuang 		if (above_2ss)
1453fa6dfe6bSYan-Hsuan Chuang 			tx_power += pwr_idx_5g->ht_2s_diff.bw40 * factor;
1454fa6dfe6bSYan-Hsuan Chuang 		break;
1455fa6dfe6bSYan-Hsuan Chuang 	case RTW_CHANNEL_WIDTH_80:
1456fa6dfe6bSYan-Hsuan Chuang 		/* the base idx of bw80 is the average of bw40+/bw40- */
1457fa6dfe6bSYan-Hsuan Chuang 		lower = pwr_idx_5g->bw40_base[group];
1458fa6dfe6bSYan-Hsuan Chuang 		upper = pwr_idx_5g->bw40_base[group + 1];
1459fa6dfe6bSYan-Hsuan Chuang 
1460fa6dfe6bSYan-Hsuan Chuang 		tx_power = (lower + upper) / 2;
1461fa6dfe6bSYan-Hsuan Chuang 		tx_power += pwr_idx_5g->vht_1s_diff.bw80 * factor;
1462fa6dfe6bSYan-Hsuan Chuang 		if (above_2ss)
1463fa6dfe6bSYan-Hsuan Chuang 			tx_power += pwr_idx_5g->vht_2s_diff.bw80 * factor;
1464fa6dfe6bSYan-Hsuan Chuang 		break;
1465fa6dfe6bSYan-Hsuan Chuang 	}
1466fa6dfe6bSYan-Hsuan Chuang 
1467fa6dfe6bSYan-Hsuan Chuang 	return tx_power;
1468fa6dfe6bSYan-Hsuan Chuang }
1469fa6dfe6bSYan-Hsuan Chuang 
1470fa6dfe6bSYan-Hsuan Chuang static s8 get_tx_power_limit(struct rtw_hal *hal, u8 bw, u8 rs, u8 ch, u8 regd)
1471fa6dfe6bSYan-Hsuan Chuang {
1472fa6dfe6bSYan-Hsuan Chuang 	if (regd > RTW_REGD_WW)
1473fa6dfe6bSYan-Hsuan Chuang 		return RTW_MAX_POWER_INDEX;
1474fa6dfe6bSYan-Hsuan Chuang 
1475fa6dfe6bSYan-Hsuan Chuang 	return hal->tx_pwr_limit_2g[regd][bw][rs][ch];
1476fa6dfe6bSYan-Hsuan Chuang }
1477fa6dfe6bSYan-Hsuan Chuang 
1478fa6dfe6bSYan-Hsuan Chuang static s8 phy_get_tx_power_limit(struct rtw_dev *rtwdev, u8 band,
1479fa6dfe6bSYan-Hsuan Chuang 				 enum rtw_bandwidth bw, u8 rf_path,
1480fa6dfe6bSYan-Hsuan Chuang 				 u8 rate, u8 channel, u8 regd)
1481fa6dfe6bSYan-Hsuan Chuang {
1482fa6dfe6bSYan-Hsuan Chuang 	struct rtw_hal *hal = &rtwdev->hal;
1483fa6dfe6bSYan-Hsuan Chuang 	s8 power_limit;
1484fa6dfe6bSYan-Hsuan Chuang 	u8 rs;
1485fa6dfe6bSYan-Hsuan Chuang 	int ch_idx;
1486fa6dfe6bSYan-Hsuan Chuang 
1487fa6dfe6bSYan-Hsuan Chuang 	if (rate >= DESC_RATE1M && rate <= DESC_RATE11M)
1488fa6dfe6bSYan-Hsuan Chuang 		rs = RTW_RATE_SECTION_CCK;
1489fa6dfe6bSYan-Hsuan Chuang 	else if (rate >= DESC_RATE6M && rate <= DESC_RATE54M)
1490fa6dfe6bSYan-Hsuan Chuang 		rs = RTW_RATE_SECTION_OFDM;
1491fa6dfe6bSYan-Hsuan Chuang 	else if (rate >= DESC_RATEMCS0 && rate <= DESC_RATEMCS7)
1492fa6dfe6bSYan-Hsuan Chuang 		rs = RTW_RATE_SECTION_HT_1S;
1493fa6dfe6bSYan-Hsuan Chuang 	else if (rate >= DESC_RATEMCS8 && rate <= DESC_RATEMCS15)
1494fa6dfe6bSYan-Hsuan Chuang 		rs = RTW_RATE_SECTION_HT_2S;
1495fa6dfe6bSYan-Hsuan Chuang 	else if (rate >= DESC_RATEVHT1SS_MCS0 && rate <= DESC_RATEVHT1SS_MCS9)
1496fa6dfe6bSYan-Hsuan Chuang 		rs = RTW_RATE_SECTION_VHT_1S;
1497fa6dfe6bSYan-Hsuan Chuang 	else if (rate >= DESC_RATEVHT2SS_MCS0 && rate <= DESC_RATEVHT2SS_MCS9)
1498fa6dfe6bSYan-Hsuan Chuang 		rs = RTW_RATE_SECTION_VHT_2S;
1499fa6dfe6bSYan-Hsuan Chuang 	else
1500fa6dfe6bSYan-Hsuan Chuang 		goto err;
1501fa6dfe6bSYan-Hsuan Chuang 
1502fa6dfe6bSYan-Hsuan Chuang 	ch_idx = rtw_channel_to_idx(band, channel);
1503fa6dfe6bSYan-Hsuan Chuang 	if (ch_idx < 0)
1504fa6dfe6bSYan-Hsuan Chuang 		goto err;
1505fa6dfe6bSYan-Hsuan Chuang 
1506fa6dfe6bSYan-Hsuan Chuang 	power_limit = get_tx_power_limit(hal, bw, rs, ch_idx, regd);
1507fa6dfe6bSYan-Hsuan Chuang 
1508fa6dfe6bSYan-Hsuan Chuang 	return power_limit;
1509fa6dfe6bSYan-Hsuan Chuang 
1510fa6dfe6bSYan-Hsuan Chuang err:
1511fa6dfe6bSYan-Hsuan Chuang 	WARN(1, "invalid arguments, band=%d, bw=%d, path=%d, rate=%d, ch=%d\n",
1512fa6dfe6bSYan-Hsuan Chuang 	     band, bw, rf_path, rate, channel);
1513fa6dfe6bSYan-Hsuan Chuang 	return RTW_MAX_POWER_INDEX;
1514fa6dfe6bSYan-Hsuan Chuang }
1515fa6dfe6bSYan-Hsuan Chuang 
1516fa6dfe6bSYan-Hsuan Chuang static
1517*226746fdSYan-Hsuan Chuang u8 phy_get_tx_power_index(struct rtw_dev *rtwdev, u8 rf_path, u8 rate,
1518fa6dfe6bSYan-Hsuan Chuang 			  enum rtw_bandwidth bandwidth, u8 channel, u8 regd)
1519fa6dfe6bSYan-Hsuan Chuang {
1520fa6dfe6bSYan-Hsuan Chuang 	struct rtw_hal *hal = &rtwdev->hal;
1521fa6dfe6bSYan-Hsuan Chuang 	struct rtw_txpwr_idx *pwr_idx;
1522fa6dfe6bSYan-Hsuan Chuang 	u8 tx_power;
1523fa6dfe6bSYan-Hsuan Chuang 	u8 group;
1524fa6dfe6bSYan-Hsuan Chuang 	u8 band;
1525fa6dfe6bSYan-Hsuan Chuang 	s8 offset, limit;
1526fa6dfe6bSYan-Hsuan Chuang 
1527fa6dfe6bSYan-Hsuan Chuang 	pwr_idx = &rtwdev->efuse.txpwr_idx_table[rf_path];
1528fa6dfe6bSYan-Hsuan Chuang 	group = rtw_get_channel_group(channel);
1529fa6dfe6bSYan-Hsuan Chuang 
1530fa6dfe6bSYan-Hsuan Chuang 	/* base power index for 2.4G/5G */
1531fa6dfe6bSYan-Hsuan Chuang 	if (channel <= 14) {
1532fa6dfe6bSYan-Hsuan Chuang 		band = PHY_BAND_2G;
1533fa6dfe6bSYan-Hsuan Chuang 		tx_power = phy_get_2g_tx_power_index(rtwdev,
1534fa6dfe6bSYan-Hsuan Chuang 						     &pwr_idx->pwr_idx_2g,
1535fa6dfe6bSYan-Hsuan Chuang 						     bandwidth, rate, group);
1536fa6dfe6bSYan-Hsuan Chuang 		offset = hal->tx_pwr_by_rate_offset_2g[rf_path][rate];
1537fa6dfe6bSYan-Hsuan Chuang 	} else {
1538fa6dfe6bSYan-Hsuan Chuang 		band = PHY_BAND_5G;
1539fa6dfe6bSYan-Hsuan Chuang 		tx_power = phy_get_5g_tx_power_index(rtwdev,
1540fa6dfe6bSYan-Hsuan Chuang 						     &pwr_idx->pwr_idx_5g,
1541fa6dfe6bSYan-Hsuan Chuang 						     bandwidth, rate, group);
1542fa6dfe6bSYan-Hsuan Chuang 		offset = hal->tx_pwr_by_rate_offset_5g[rf_path][rate];
1543fa6dfe6bSYan-Hsuan Chuang 	}
1544fa6dfe6bSYan-Hsuan Chuang 
1545fa6dfe6bSYan-Hsuan Chuang 	limit = phy_get_tx_power_limit(rtwdev, band, bandwidth, rf_path,
1546fa6dfe6bSYan-Hsuan Chuang 				       rate, channel, regd);
1547fa6dfe6bSYan-Hsuan Chuang 
1548fa6dfe6bSYan-Hsuan Chuang 	if (offset > limit)
1549fa6dfe6bSYan-Hsuan Chuang 		offset = limit;
1550fa6dfe6bSYan-Hsuan Chuang 
1551fa6dfe6bSYan-Hsuan Chuang 	tx_power += offset;
1552fa6dfe6bSYan-Hsuan Chuang 
1553fa6dfe6bSYan-Hsuan Chuang 	if (tx_power > rtwdev->chip->max_power_index)
1554fa6dfe6bSYan-Hsuan Chuang 		tx_power = rtwdev->chip->max_power_index;
1555fa6dfe6bSYan-Hsuan Chuang 
1556fa6dfe6bSYan-Hsuan Chuang 	return tx_power;
1557fa6dfe6bSYan-Hsuan Chuang }
1558fa6dfe6bSYan-Hsuan Chuang 
1559*226746fdSYan-Hsuan Chuang static void phy_set_tx_power_index_by_rs(struct rtw_dev *rtwdev,
1560*226746fdSYan-Hsuan Chuang 					 u8 ch, u8 path, u8 rs)
1561fa6dfe6bSYan-Hsuan Chuang {
1562fa6dfe6bSYan-Hsuan Chuang 	struct rtw_hal *hal = &rtwdev->hal;
1563fa6dfe6bSYan-Hsuan Chuang 	u8 regd = rtwdev->regd.txpwr_regd;
1564fa6dfe6bSYan-Hsuan Chuang 	u8 *rates;
1565fa6dfe6bSYan-Hsuan Chuang 	u8 size;
1566fa6dfe6bSYan-Hsuan Chuang 	u8 rate;
1567fa6dfe6bSYan-Hsuan Chuang 	u8 pwr_idx;
1568fa6dfe6bSYan-Hsuan Chuang 	u8 bw;
1569fa6dfe6bSYan-Hsuan Chuang 	int i;
1570fa6dfe6bSYan-Hsuan Chuang 
1571fa6dfe6bSYan-Hsuan Chuang 	if (rs >= RTW_RATE_SECTION_MAX)
1572fa6dfe6bSYan-Hsuan Chuang 		return;
1573fa6dfe6bSYan-Hsuan Chuang 
1574fa6dfe6bSYan-Hsuan Chuang 	rates = rtw_rate_section[rs];
1575fa6dfe6bSYan-Hsuan Chuang 	size = rtw_rate_size[rs];
1576fa6dfe6bSYan-Hsuan Chuang 	bw = hal->current_band_width;
1577fa6dfe6bSYan-Hsuan Chuang 	for (i = 0; i < size; i++) {
1578fa6dfe6bSYan-Hsuan Chuang 		rate = rates[i];
1579*226746fdSYan-Hsuan Chuang 		pwr_idx = phy_get_tx_power_index(rtwdev, path, rate, bw, ch,
1580fa6dfe6bSYan-Hsuan Chuang 						 regd);
1581fa6dfe6bSYan-Hsuan Chuang 		hal->tx_pwr_tbl[path][rate] = pwr_idx;
1582fa6dfe6bSYan-Hsuan Chuang 	}
1583fa6dfe6bSYan-Hsuan Chuang }
1584fa6dfe6bSYan-Hsuan Chuang 
1585fa6dfe6bSYan-Hsuan Chuang /* set tx power level by path for each rates, note that the order of the rates
1586fa6dfe6bSYan-Hsuan Chuang  * are *very* important, bacause 8822B/8821C combines every four bytes of tx
1587fa6dfe6bSYan-Hsuan Chuang  * power index into a four-byte power index register, and calls set_tx_agc to
1588fa6dfe6bSYan-Hsuan Chuang  * write these values into hardware
1589fa6dfe6bSYan-Hsuan Chuang  */
1590fa6dfe6bSYan-Hsuan Chuang static
1591fa6dfe6bSYan-Hsuan Chuang void phy_set_tx_power_level_by_path(struct rtw_dev *rtwdev, u8 ch, u8 path)
1592fa6dfe6bSYan-Hsuan Chuang {
1593fa6dfe6bSYan-Hsuan Chuang 	struct rtw_hal *hal = &rtwdev->hal;
1594fa6dfe6bSYan-Hsuan Chuang 	u8 rs;
1595fa6dfe6bSYan-Hsuan Chuang 
1596fa6dfe6bSYan-Hsuan Chuang 	/* do not need cck rates if we are not in 2.4G */
1597fa6dfe6bSYan-Hsuan Chuang 	if (hal->current_band_type == RTW_BAND_2G)
1598fa6dfe6bSYan-Hsuan Chuang 		rs = RTW_RATE_SECTION_CCK;
1599fa6dfe6bSYan-Hsuan Chuang 	else
1600fa6dfe6bSYan-Hsuan Chuang 		rs = RTW_RATE_SECTION_OFDM;
1601fa6dfe6bSYan-Hsuan Chuang 
1602fa6dfe6bSYan-Hsuan Chuang 	for (; rs < RTW_RATE_SECTION_MAX; rs++)
1603fa6dfe6bSYan-Hsuan Chuang 		phy_set_tx_power_index_by_rs(rtwdev, ch, path, rs);
1604fa6dfe6bSYan-Hsuan Chuang }
1605fa6dfe6bSYan-Hsuan Chuang 
1606fa6dfe6bSYan-Hsuan Chuang void rtw_phy_set_tx_power_level(struct rtw_dev *rtwdev, u8 channel)
1607fa6dfe6bSYan-Hsuan Chuang {
1608fa6dfe6bSYan-Hsuan Chuang 	struct rtw_chip_info *chip = rtwdev->chip;
1609fa6dfe6bSYan-Hsuan Chuang 	struct rtw_hal *hal = &rtwdev->hal;
1610fa6dfe6bSYan-Hsuan Chuang 	u8 path;
1611fa6dfe6bSYan-Hsuan Chuang 
1612fa6dfe6bSYan-Hsuan Chuang 	mutex_lock(&hal->tx_power_mutex);
1613fa6dfe6bSYan-Hsuan Chuang 
1614fa6dfe6bSYan-Hsuan Chuang 	for (path = 0; path < hal->rf_path_num; path++)
1615fa6dfe6bSYan-Hsuan Chuang 		phy_set_tx_power_level_by_path(rtwdev, channel, path);
1616fa6dfe6bSYan-Hsuan Chuang 
1617fa6dfe6bSYan-Hsuan Chuang 	chip->ops->set_tx_power_index(rtwdev);
1618fa6dfe6bSYan-Hsuan Chuang 	mutex_unlock(&hal->tx_power_mutex);
1619fa6dfe6bSYan-Hsuan Chuang }
1620fa6dfe6bSYan-Hsuan Chuang 
1621e3037485SYan-Hsuan Chuang static
1622e3037485SYan-Hsuan Chuang void phy_tx_power_by_rate_config_by_path(struct rtw_hal *hal, u8 path,
1623e3037485SYan-Hsuan Chuang 					 u8 rs, u8 size, u8 *rates)
1624e3037485SYan-Hsuan Chuang {
1625e3037485SYan-Hsuan Chuang 	u8 rate;
1626e3037485SYan-Hsuan Chuang 	u8 base_idx, rate_idx;
1627e3037485SYan-Hsuan Chuang 	s8 base_2g, base_5g;
1628e3037485SYan-Hsuan Chuang 
1629e3037485SYan-Hsuan Chuang 	if (rs >= RTW_RATE_SECTION_VHT_1S)
1630e3037485SYan-Hsuan Chuang 		base_idx = rates[size - 3];
1631e3037485SYan-Hsuan Chuang 	else
1632e3037485SYan-Hsuan Chuang 		base_idx = rates[size - 1];
1633e3037485SYan-Hsuan Chuang 	base_2g = hal->tx_pwr_by_rate_offset_2g[path][base_idx];
1634e3037485SYan-Hsuan Chuang 	base_5g = hal->tx_pwr_by_rate_offset_5g[path][base_idx];
1635e3037485SYan-Hsuan Chuang 	hal->tx_pwr_by_rate_base_2g[path][rs] = base_2g;
1636e3037485SYan-Hsuan Chuang 	hal->tx_pwr_by_rate_base_5g[path][rs] = base_5g;
1637e3037485SYan-Hsuan Chuang 	for (rate = 0; rate < size; rate++) {
1638e3037485SYan-Hsuan Chuang 		rate_idx = rates[rate];
1639e3037485SYan-Hsuan Chuang 		hal->tx_pwr_by_rate_offset_2g[path][rate_idx] -= base_2g;
1640e3037485SYan-Hsuan Chuang 		hal->tx_pwr_by_rate_offset_5g[path][rate_idx] -= base_5g;
1641e3037485SYan-Hsuan Chuang 	}
1642e3037485SYan-Hsuan Chuang }
1643e3037485SYan-Hsuan Chuang 
1644e3037485SYan-Hsuan Chuang void rtw_phy_tx_power_by_rate_config(struct rtw_hal *hal)
1645e3037485SYan-Hsuan Chuang {
1646e3037485SYan-Hsuan Chuang 	u8 path;
1647e3037485SYan-Hsuan Chuang 
1648e3037485SYan-Hsuan Chuang 	for (path = 0; path < RTW_RF_PATH_MAX; path++) {
1649e3037485SYan-Hsuan Chuang 		phy_tx_power_by_rate_config_by_path(hal, path,
1650e3037485SYan-Hsuan Chuang 				RTW_RATE_SECTION_CCK,
1651e3037485SYan-Hsuan Chuang 				rtw_cck_size, rtw_cck_rates);
1652e3037485SYan-Hsuan Chuang 		phy_tx_power_by_rate_config_by_path(hal, path,
1653e3037485SYan-Hsuan Chuang 				RTW_RATE_SECTION_OFDM,
1654e3037485SYan-Hsuan Chuang 				rtw_ofdm_size, rtw_ofdm_rates);
1655e3037485SYan-Hsuan Chuang 		phy_tx_power_by_rate_config_by_path(hal, path,
1656e3037485SYan-Hsuan Chuang 				RTW_RATE_SECTION_HT_1S,
1657e3037485SYan-Hsuan Chuang 				rtw_ht_1s_size, rtw_ht_1s_rates);
1658e3037485SYan-Hsuan Chuang 		phy_tx_power_by_rate_config_by_path(hal, path,
1659e3037485SYan-Hsuan Chuang 				RTW_RATE_SECTION_HT_2S,
1660e3037485SYan-Hsuan Chuang 				rtw_ht_2s_size, rtw_ht_2s_rates);
1661e3037485SYan-Hsuan Chuang 		phy_tx_power_by_rate_config_by_path(hal, path,
1662e3037485SYan-Hsuan Chuang 				RTW_RATE_SECTION_VHT_1S,
1663e3037485SYan-Hsuan Chuang 				rtw_vht_1s_size, rtw_vht_1s_rates);
1664e3037485SYan-Hsuan Chuang 		phy_tx_power_by_rate_config_by_path(hal, path,
1665e3037485SYan-Hsuan Chuang 				RTW_RATE_SECTION_VHT_2S,
1666e3037485SYan-Hsuan Chuang 				rtw_vht_2s_size, rtw_vht_2s_rates);
1667e3037485SYan-Hsuan Chuang 	}
1668e3037485SYan-Hsuan Chuang }
1669e3037485SYan-Hsuan Chuang 
1670e3037485SYan-Hsuan Chuang static void
1671e3037485SYan-Hsuan Chuang phy_tx_power_limit_config(struct rtw_hal *hal, u8 regd, u8 bw, u8 rs)
1672e3037485SYan-Hsuan Chuang {
1673e3037485SYan-Hsuan Chuang 	s8 base, orig;
1674e3037485SYan-Hsuan Chuang 	u8 ch;
1675e3037485SYan-Hsuan Chuang 
1676e3037485SYan-Hsuan Chuang 	for (ch = 0; ch < RTW_MAX_CHANNEL_NUM_2G; ch++) {
1677e3037485SYan-Hsuan Chuang 		base = hal->tx_pwr_by_rate_base_2g[0][rs];
1678e3037485SYan-Hsuan Chuang 		orig = hal->tx_pwr_limit_2g[regd][bw][rs][ch];
1679e3037485SYan-Hsuan Chuang 		hal->tx_pwr_limit_2g[regd][bw][rs][ch] -= base;
1680e3037485SYan-Hsuan Chuang 	}
1681e3037485SYan-Hsuan Chuang 
1682e3037485SYan-Hsuan Chuang 	for (ch = 0; ch < RTW_MAX_CHANNEL_NUM_5G; ch++) {
1683e3037485SYan-Hsuan Chuang 		base = hal->tx_pwr_by_rate_base_5g[0][rs];
1684e3037485SYan-Hsuan Chuang 		hal->tx_pwr_limit_5g[regd][bw][rs][ch] -= base;
1685e3037485SYan-Hsuan Chuang 	}
1686e3037485SYan-Hsuan Chuang }
1687e3037485SYan-Hsuan Chuang 
1688e3037485SYan-Hsuan Chuang void rtw_phy_tx_power_limit_config(struct rtw_hal *hal)
1689e3037485SYan-Hsuan Chuang {
1690e3037485SYan-Hsuan Chuang 	u8 regd, bw, rs;
1691e3037485SYan-Hsuan Chuang 
1692e3037485SYan-Hsuan Chuang 	for (regd = 0; regd < RTW_REGD_MAX; regd++)
1693e3037485SYan-Hsuan Chuang 		for (bw = 0; bw < RTW_CHANNEL_WIDTH_MAX; bw++)
1694e3037485SYan-Hsuan Chuang 			for (rs = 0; rs < RTW_RATE_SECTION_MAX; rs++)
1695e3037485SYan-Hsuan Chuang 				phy_tx_power_limit_config(hal, regd, bw, rs);
1696e3037485SYan-Hsuan Chuang }
1697e3037485SYan-Hsuan Chuang 
1698e3037485SYan-Hsuan Chuang static
1699e3037485SYan-Hsuan Chuang void rtw_hw_tx_power_limit_init(struct rtw_hal *hal, u8 regd, u8 bw, u8 rs)
1700e3037485SYan-Hsuan Chuang {
1701e3037485SYan-Hsuan Chuang 	u8 ch;
1702e3037485SYan-Hsuan Chuang 
1703e3037485SYan-Hsuan Chuang 	/* 2.4G channels */
1704e3037485SYan-Hsuan Chuang 	for (ch = 0; ch < RTW_MAX_CHANNEL_NUM_2G; ch++)
1705e3037485SYan-Hsuan Chuang 		hal->tx_pwr_limit_2g[regd][bw][rs][ch] = RTW_MAX_POWER_INDEX;
1706e3037485SYan-Hsuan Chuang 
1707e3037485SYan-Hsuan Chuang 	/* 5G channels */
1708e3037485SYan-Hsuan Chuang 	for (ch = 0; ch < RTW_MAX_CHANNEL_NUM_5G; ch++)
1709e3037485SYan-Hsuan Chuang 		hal->tx_pwr_limit_5g[regd][bw][rs][ch] = RTW_MAX_POWER_INDEX;
1710e3037485SYan-Hsuan Chuang }
1711e3037485SYan-Hsuan Chuang 
1712e3037485SYan-Hsuan Chuang void rtw_hw_init_tx_power(struct rtw_hal *hal)
1713e3037485SYan-Hsuan Chuang {
1714e3037485SYan-Hsuan Chuang 	u8 regd, path, rate, rs, bw;
1715e3037485SYan-Hsuan Chuang 
1716e3037485SYan-Hsuan Chuang 	/* init tx power by rate offset */
1717e3037485SYan-Hsuan Chuang 	for (path = 0; path < RTW_RF_PATH_MAX; path++) {
1718e3037485SYan-Hsuan Chuang 		for (rate = 0; rate < DESC_RATE_MAX; rate++) {
1719e3037485SYan-Hsuan Chuang 			hal->tx_pwr_by_rate_offset_2g[path][rate] = 0;
1720e3037485SYan-Hsuan Chuang 			hal->tx_pwr_by_rate_offset_5g[path][rate] = 0;
1721e3037485SYan-Hsuan Chuang 		}
1722e3037485SYan-Hsuan Chuang 	}
1723e3037485SYan-Hsuan Chuang 
1724e3037485SYan-Hsuan Chuang 	/* init tx power limit */
1725e3037485SYan-Hsuan Chuang 	for (regd = 0; regd < RTW_REGD_MAX; regd++)
1726e3037485SYan-Hsuan Chuang 		for (bw = 0; bw < RTW_CHANNEL_WIDTH_MAX; bw++)
1727e3037485SYan-Hsuan Chuang 			for (rs = 0; rs < RTW_RATE_SECTION_MAX; rs++)
1728e3037485SYan-Hsuan Chuang 				rtw_hw_tx_power_limit_init(hal, regd, bw, rs);
1729e3037485SYan-Hsuan Chuang }
1730