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 static const u32 db_invert_table[12][8] = { 24e3037485SYan-Hsuan Chuang {10, 13, 16, 20, 25e3037485SYan-Hsuan Chuang 25, 32, 40, 50}, 26e3037485SYan-Hsuan Chuang {64, 80, 101, 128, 27e3037485SYan-Hsuan Chuang 160, 201, 256, 318}, 28e3037485SYan-Hsuan Chuang {401, 505, 635, 800, 29e3037485SYan-Hsuan Chuang 1007, 1268, 1596, 2010}, 30e3037485SYan-Hsuan Chuang {316, 398, 501, 631, 31e3037485SYan-Hsuan Chuang 794, 1000, 1259, 1585}, 32e3037485SYan-Hsuan Chuang {1995, 2512, 3162, 3981, 33e3037485SYan-Hsuan Chuang 5012, 6310, 7943, 10000}, 34e3037485SYan-Hsuan Chuang {12589, 15849, 19953, 25119, 35e3037485SYan-Hsuan Chuang 31623, 39811, 50119, 63098}, 36e3037485SYan-Hsuan Chuang {79433, 100000, 125893, 158489, 37e3037485SYan-Hsuan Chuang 199526, 251189, 316228, 398107}, 38e3037485SYan-Hsuan Chuang {501187, 630957, 794328, 1000000, 39e3037485SYan-Hsuan Chuang 1258925, 1584893, 1995262, 2511886}, 40e3037485SYan-Hsuan Chuang {3162278, 3981072, 5011872, 6309573, 41e3037485SYan-Hsuan Chuang 7943282, 1000000, 12589254, 15848932}, 42e3037485SYan-Hsuan Chuang {19952623, 25118864, 31622777, 39810717, 43e3037485SYan-Hsuan Chuang 50118723, 63095734, 79432823, 100000000}, 44e3037485SYan-Hsuan Chuang {125892541, 158489319, 199526232, 251188643, 45e3037485SYan-Hsuan Chuang 316227766, 398107171, 501187234, 630957345}, 46e3037485SYan-Hsuan Chuang {794328235, 1000000000, 1258925412, 1584893192, 47e3037485SYan-Hsuan Chuang 1995262315, 2511886432U, 3162277660U, 3981071706U} 48e3037485SYan-Hsuan Chuang }; 49e3037485SYan-Hsuan Chuang 50fa6dfe6bSYan-Hsuan Chuang u8 rtw_cck_rates[] = { DESC_RATE1M, DESC_RATE2M, DESC_RATE5_5M, DESC_RATE11M }; 51fa6dfe6bSYan-Hsuan Chuang u8 rtw_ofdm_rates[] = { 52fa6dfe6bSYan-Hsuan Chuang DESC_RATE6M, DESC_RATE9M, DESC_RATE12M, 53fa6dfe6bSYan-Hsuan Chuang DESC_RATE18M, DESC_RATE24M, DESC_RATE36M, 54fa6dfe6bSYan-Hsuan Chuang DESC_RATE48M, DESC_RATE54M 55fa6dfe6bSYan-Hsuan Chuang }; 56fa6dfe6bSYan-Hsuan Chuang u8 rtw_ht_1s_rates[] = { 57fa6dfe6bSYan-Hsuan Chuang DESC_RATEMCS0, DESC_RATEMCS1, DESC_RATEMCS2, 58fa6dfe6bSYan-Hsuan Chuang DESC_RATEMCS3, DESC_RATEMCS4, DESC_RATEMCS5, 59fa6dfe6bSYan-Hsuan Chuang DESC_RATEMCS6, DESC_RATEMCS7 60fa6dfe6bSYan-Hsuan Chuang }; 61fa6dfe6bSYan-Hsuan Chuang u8 rtw_ht_2s_rates[] = { 62fa6dfe6bSYan-Hsuan Chuang DESC_RATEMCS8, DESC_RATEMCS9, DESC_RATEMCS10, 63fa6dfe6bSYan-Hsuan Chuang DESC_RATEMCS11, DESC_RATEMCS12, DESC_RATEMCS13, 64fa6dfe6bSYan-Hsuan Chuang DESC_RATEMCS14, DESC_RATEMCS15 65fa6dfe6bSYan-Hsuan Chuang }; 66fa6dfe6bSYan-Hsuan Chuang u8 rtw_vht_1s_rates[] = { 67fa6dfe6bSYan-Hsuan Chuang DESC_RATEVHT1SS_MCS0, DESC_RATEVHT1SS_MCS1, 68fa6dfe6bSYan-Hsuan Chuang DESC_RATEVHT1SS_MCS2, DESC_RATEVHT1SS_MCS3, 69fa6dfe6bSYan-Hsuan Chuang DESC_RATEVHT1SS_MCS4, DESC_RATEVHT1SS_MCS5, 70fa6dfe6bSYan-Hsuan Chuang DESC_RATEVHT1SS_MCS6, DESC_RATEVHT1SS_MCS7, 71fa6dfe6bSYan-Hsuan Chuang DESC_RATEVHT1SS_MCS8, DESC_RATEVHT1SS_MCS9 72fa6dfe6bSYan-Hsuan Chuang }; 73fa6dfe6bSYan-Hsuan Chuang u8 rtw_vht_2s_rates[] = { 74fa6dfe6bSYan-Hsuan Chuang DESC_RATEVHT2SS_MCS0, DESC_RATEVHT2SS_MCS1, 75fa6dfe6bSYan-Hsuan Chuang DESC_RATEVHT2SS_MCS2, DESC_RATEVHT2SS_MCS3, 76fa6dfe6bSYan-Hsuan Chuang DESC_RATEVHT2SS_MCS4, DESC_RATEVHT2SS_MCS5, 77fa6dfe6bSYan-Hsuan Chuang DESC_RATEVHT2SS_MCS6, DESC_RATEVHT2SS_MCS7, 78fa6dfe6bSYan-Hsuan Chuang DESC_RATEVHT2SS_MCS8, DESC_RATEVHT2SS_MCS9 79fa6dfe6bSYan-Hsuan Chuang }; 80fa6dfe6bSYan-Hsuan Chuang u8 *rtw_rate_section[RTW_RATE_SECTION_MAX] = { 81fa6dfe6bSYan-Hsuan Chuang rtw_cck_rates, rtw_ofdm_rates, 82fa6dfe6bSYan-Hsuan Chuang rtw_ht_1s_rates, rtw_ht_2s_rates, 83fa6dfe6bSYan-Hsuan Chuang rtw_vht_1s_rates, rtw_vht_2s_rates 84fa6dfe6bSYan-Hsuan Chuang }; 85449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_rate_section); 86449be866SZong-Zhe Yang 87fa6dfe6bSYan-Hsuan Chuang u8 rtw_rate_size[RTW_RATE_SECTION_MAX] = { 88fa6dfe6bSYan-Hsuan Chuang ARRAY_SIZE(rtw_cck_rates), 89fa6dfe6bSYan-Hsuan Chuang ARRAY_SIZE(rtw_ofdm_rates), 90fa6dfe6bSYan-Hsuan Chuang ARRAY_SIZE(rtw_ht_1s_rates), 91fa6dfe6bSYan-Hsuan Chuang ARRAY_SIZE(rtw_ht_2s_rates), 92fa6dfe6bSYan-Hsuan Chuang ARRAY_SIZE(rtw_vht_1s_rates), 93fa6dfe6bSYan-Hsuan Chuang ARRAY_SIZE(rtw_vht_2s_rates) 94fa6dfe6bSYan-Hsuan Chuang }; 95449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_rate_size); 96449be866SZong-Zhe Yang 97fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_cck_size = ARRAY_SIZE(rtw_cck_rates); 98fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_ofdm_size = ARRAY_SIZE(rtw_ofdm_rates); 99fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_ht_1s_size = ARRAY_SIZE(rtw_ht_1s_rates); 100fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_ht_2s_size = ARRAY_SIZE(rtw_ht_2s_rates); 101fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_vht_1s_size = ARRAY_SIZE(rtw_vht_1s_rates); 102fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_vht_2s_size = ARRAY_SIZE(rtw_vht_2s_rates); 103fa6dfe6bSYan-Hsuan Chuang 104e3037485SYan-Hsuan Chuang enum rtw_phy_band_type { 105e3037485SYan-Hsuan Chuang PHY_BAND_2G = 0, 106e3037485SYan-Hsuan Chuang PHY_BAND_5G = 1, 107e3037485SYan-Hsuan Chuang }; 108e3037485SYan-Hsuan Chuang 109479c4ee9STzu-En Huang static void rtw_phy_cck_pd_init(struct rtw_dev *rtwdev) 110479c4ee9STzu-En Huang { 111479c4ee9STzu-En Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 112479c4ee9STzu-En Huang u8 i, j; 113479c4ee9STzu-En Huang 114479c4ee9STzu-En Huang for (i = 0; i <= RTW_CHANNEL_WIDTH_40; i++) { 115479c4ee9STzu-En Huang for (j = 0; j < RTW_RF_PATH_MAX; j++) 11618a0696eSTzu-En Huang dm_info->cck_pd_lv[i][j] = CCK_PD_LV0; 117479c4ee9STzu-En Huang } 118479c4ee9STzu-En Huang 119479c4ee9STzu-En Huang dm_info->cck_fa_avg = CCK_FA_AVG_RESET; 120479c4ee9STzu-En Huang } 121479c4ee9STzu-En Huang 122e3037485SYan-Hsuan Chuang void rtw_phy_init(struct rtw_dev *rtwdev) 123e3037485SYan-Hsuan Chuang { 124e3037485SYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 125e3037485SYan-Hsuan Chuang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 126e3037485SYan-Hsuan Chuang u32 addr, mask; 127e3037485SYan-Hsuan Chuang 128e3037485SYan-Hsuan Chuang dm_info->fa_history[3] = 0; 129e3037485SYan-Hsuan Chuang dm_info->fa_history[2] = 0; 130e3037485SYan-Hsuan Chuang dm_info->fa_history[1] = 0; 131e3037485SYan-Hsuan Chuang dm_info->fa_history[0] = 0; 132e3037485SYan-Hsuan Chuang dm_info->igi_bitmap = 0; 133e3037485SYan-Hsuan Chuang dm_info->igi_history[3] = 0; 134e3037485SYan-Hsuan Chuang dm_info->igi_history[2] = 0; 135e3037485SYan-Hsuan Chuang dm_info->igi_history[1] = 0; 136e3037485SYan-Hsuan Chuang 137e3037485SYan-Hsuan Chuang addr = chip->dig[0].addr; 138e3037485SYan-Hsuan Chuang mask = chip->dig[0].mask; 139e3037485SYan-Hsuan Chuang dm_info->igi_history[0] = rtw_read32_mask(rtwdev, addr, mask); 140479c4ee9STzu-En Huang rtw_phy_cck_pd_init(rtwdev); 1411d229e88SPing-Ke Shih 1421d229e88SPing-Ke Shih dm_info->iqk.done = false; 143e3037485SYan-Hsuan Chuang } 144449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_init); 145e3037485SYan-Hsuan Chuang 146e3037485SYan-Hsuan Chuang void rtw_phy_dig_write(struct rtw_dev *rtwdev, u8 igi) 147e3037485SYan-Hsuan Chuang { 148e3037485SYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 149e3037485SYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 150e3037485SYan-Hsuan Chuang u32 addr, mask; 151e3037485SYan-Hsuan Chuang u8 path; 152e3037485SYan-Hsuan Chuang 15322b726cbSBrian Norris if (chip->dig_cck) { 15422b726cbSBrian Norris const struct rtw_hw_reg *dig_cck = &chip->dig_cck[0]; 155fc637a86SPing-Ke Shih rtw_write32_mask(rtwdev, dig_cck->addr, dig_cck->mask, igi >> 1); 15622b726cbSBrian Norris } 157fc637a86SPing-Ke Shih 158e3037485SYan-Hsuan Chuang for (path = 0; path < hal->rf_path_num; path++) { 159e3037485SYan-Hsuan Chuang addr = chip->dig[path].addr; 160e3037485SYan-Hsuan Chuang mask = chip->dig[path].mask; 161e3037485SYan-Hsuan Chuang rtw_write32_mask(rtwdev, addr, mask, igi); 162e3037485SYan-Hsuan Chuang } 163e3037485SYan-Hsuan Chuang } 164e3037485SYan-Hsuan Chuang 165e3037485SYan-Hsuan Chuang static void rtw_phy_stat_false_alarm(struct rtw_dev *rtwdev) 166e3037485SYan-Hsuan Chuang { 167e3037485SYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 168e3037485SYan-Hsuan Chuang 169e3037485SYan-Hsuan Chuang chip->ops->false_alarm_statistics(rtwdev); 170e3037485SYan-Hsuan Chuang } 171e3037485SYan-Hsuan Chuang 172e3037485SYan-Hsuan Chuang #define RA_FLOOR_TABLE_SIZE 7 173e3037485SYan-Hsuan Chuang #define RA_FLOOR_UP_GAP 3 174e3037485SYan-Hsuan Chuang 175e3037485SYan-Hsuan Chuang static u8 rtw_phy_get_rssi_level(u8 old_level, u8 rssi) 176e3037485SYan-Hsuan Chuang { 177e3037485SYan-Hsuan Chuang u8 table[RA_FLOOR_TABLE_SIZE] = {20, 34, 38, 42, 46, 50, 100}; 178e3037485SYan-Hsuan Chuang u8 new_level = 0; 179e3037485SYan-Hsuan Chuang int i; 180e3037485SYan-Hsuan Chuang 181e3037485SYan-Hsuan Chuang for (i = 0; i < RA_FLOOR_TABLE_SIZE; i++) 182e3037485SYan-Hsuan Chuang if (i >= old_level) 183e3037485SYan-Hsuan Chuang table[i] += RA_FLOOR_UP_GAP; 184e3037485SYan-Hsuan Chuang 185e3037485SYan-Hsuan Chuang for (i = 0; i < RA_FLOOR_TABLE_SIZE; i++) { 186e3037485SYan-Hsuan Chuang if (rssi < table[i]) { 187e3037485SYan-Hsuan Chuang new_level = i; 188e3037485SYan-Hsuan Chuang break; 189e3037485SYan-Hsuan Chuang } 190e3037485SYan-Hsuan Chuang } 191e3037485SYan-Hsuan Chuang 192e3037485SYan-Hsuan Chuang return new_level; 193e3037485SYan-Hsuan Chuang } 194e3037485SYan-Hsuan Chuang 195e3037485SYan-Hsuan Chuang struct rtw_phy_stat_iter_data { 196e3037485SYan-Hsuan Chuang struct rtw_dev *rtwdev; 197e3037485SYan-Hsuan Chuang u8 min_rssi; 198e3037485SYan-Hsuan Chuang }; 199e3037485SYan-Hsuan Chuang 200e3037485SYan-Hsuan Chuang static void rtw_phy_stat_rssi_iter(void *data, struct ieee80211_sta *sta) 201e3037485SYan-Hsuan Chuang { 202e3037485SYan-Hsuan Chuang struct rtw_phy_stat_iter_data *iter_data = data; 203e3037485SYan-Hsuan Chuang struct rtw_dev *rtwdev = iter_data->rtwdev; 204e3037485SYan-Hsuan Chuang struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv; 205a24bad74SYan-Hsuan Chuang u8 rssi; 206e3037485SYan-Hsuan Chuang 207e3037485SYan-Hsuan Chuang rssi = ewma_rssi_read(&si->avg_rssi); 208a24bad74SYan-Hsuan Chuang si->rssi_level = rtw_phy_get_rssi_level(si->rssi_level, rssi); 209e3037485SYan-Hsuan Chuang 210e3037485SYan-Hsuan Chuang rtw_fw_send_rssi_info(rtwdev, si); 211e3037485SYan-Hsuan Chuang 212e3037485SYan-Hsuan Chuang iter_data->min_rssi = min_t(u8, rssi, iter_data->min_rssi); 213e3037485SYan-Hsuan Chuang } 214e3037485SYan-Hsuan Chuang 215e3037485SYan-Hsuan Chuang static void rtw_phy_stat_rssi(struct rtw_dev *rtwdev) 216e3037485SYan-Hsuan Chuang { 217e3037485SYan-Hsuan Chuang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 218e3037485SYan-Hsuan Chuang struct rtw_phy_stat_iter_data data = {}; 219e3037485SYan-Hsuan Chuang 220e3037485SYan-Hsuan Chuang data.rtwdev = rtwdev; 221e3037485SYan-Hsuan Chuang data.min_rssi = U8_MAX; 222e3037485SYan-Hsuan Chuang rtw_iterate_stas_atomic(rtwdev, rtw_phy_stat_rssi_iter, &data); 223e3037485SYan-Hsuan Chuang 224e3037485SYan-Hsuan Chuang dm_info->pre_min_rssi = dm_info->min_rssi; 225e3037485SYan-Hsuan Chuang dm_info->min_rssi = data.min_rssi; 226e3037485SYan-Hsuan Chuang } 227e3037485SYan-Hsuan Chuang 228082a36dcSTsang-Shian Lin static void rtw_phy_stat_rate_cnt(struct rtw_dev *rtwdev) 229082a36dcSTsang-Shian Lin { 230082a36dcSTsang-Shian Lin struct rtw_dm_info *dm_info = &rtwdev->dm_info; 231082a36dcSTsang-Shian Lin 232082a36dcSTsang-Shian Lin dm_info->last_pkt_count = dm_info->cur_pkt_count; 233082a36dcSTsang-Shian Lin memset(&dm_info->cur_pkt_count, 0, sizeof(dm_info->cur_pkt_count)); 234082a36dcSTsang-Shian Lin } 235082a36dcSTsang-Shian Lin 236e3037485SYan-Hsuan Chuang static void rtw_phy_statistics(struct rtw_dev *rtwdev) 237e3037485SYan-Hsuan Chuang { 238e3037485SYan-Hsuan Chuang rtw_phy_stat_rssi(rtwdev); 239e3037485SYan-Hsuan Chuang rtw_phy_stat_false_alarm(rtwdev); 240082a36dcSTsang-Shian Lin rtw_phy_stat_rate_cnt(rtwdev); 241e3037485SYan-Hsuan Chuang } 242e3037485SYan-Hsuan Chuang 243e3037485SYan-Hsuan Chuang #define DIG_PERF_FA_TH_LOW 250 244e3037485SYan-Hsuan Chuang #define DIG_PERF_FA_TH_HIGH 500 245e3037485SYan-Hsuan Chuang #define DIG_PERF_FA_TH_EXTRA_HIGH 750 246e3037485SYan-Hsuan Chuang #define DIG_PERF_MAX 0x5a 247e3037485SYan-Hsuan Chuang #define DIG_PERF_MID 0x40 248e3037485SYan-Hsuan Chuang #define DIG_CVRG_FA_TH_LOW 2000 249e3037485SYan-Hsuan Chuang #define DIG_CVRG_FA_TH_HIGH 4000 250e3037485SYan-Hsuan Chuang #define DIG_CVRG_FA_TH_EXTRA_HIGH 5000 251e3037485SYan-Hsuan Chuang #define DIG_CVRG_MAX 0x2a 252e3037485SYan-Hsuan Chuang #define DIG_CVRG_MID 0x26 253e3037485SYan-Hsuan Chuang #define DIG_CVRG_MIN 0x1c 254e3037485SYan-Hsuan Chuang #define DIG_RSSI_GAIN_OFFSET 15 255e3037485SYan-Hsuan Chuang 256e3037485SYan-Hsuan Chuang static bool 257e3037485SYan-Hsuan Chuang rtw_phy_dig_check_damping(struct rtw_dm_info *dm_info) 258e3037485SYan-Hsuan Chuang { 259e3037485SYan-Hsuan Chuang u16 fa_lo = DIG_PERF_FA_TH_LOW; 260e3037485SYan-Hsuan Chuang u16 fa_hi = DIG_PERF_FA_TH_HIGH; 261e3037485SYan-Hsuan Chuang u16 *fa_history; 262e3037485SYan-Hsuan Chuang u8 *igi_history; 263e3037485SYan-Hsuan Chuang u8 damping_rssi; 264e3037485SYan-Hsuan Chuang u8 min_rssi; 265e3037485SYan-Hsuan Chuang u8 diff; 266e3037485SYan-Hsuan Chuang u8 igi_bitmap; 267e3037485SYan-Hsuan Chuang bool damping = false; 268e3037485SYan-Hsuan Chuang 269e3037485SYan-Hsuan Chuang min_rssi = dm_info->min_rssi; 270e3037485SYan-Hsuan Chuang if (dm_info->damping) { 271e3037485SYan-Hsuan Chuang damping_rssi = dm_info->damping_rssi; 272e3037485SYan-Hsuan Chuang diff = min_rssi > damping_rssi ? min_rssi - damping_rssi : 273e3037485SYan-Hsuan Chuang damping_rssi - min_rssi; 274e3037485SYan-Hsuan Chuang if (diff > 3 || dm_info->damping_cnt++ > 20) { 275e3037485SYan-Hsuan Chuang dm_info->damping = false; 276e3037485SYan-Hsuan Chuang return false; 277e3037485SYan-Hsuan Chuang } 278e3037485SYan-Hsuan Chuang 279e3037485SYan-Hsuan Chuang return true; 280e3037485SYan-Hsuan Chuang } 281e3037485SYan-Hsuan Chuang 282e3037485SYan-Hsuan Chuang igi_history = dm_info->igi_history; 283e3037485SYan-Hsuan Chuang fa_history = dm_info->fa_history; 284e3037485SYan-Hsuan Chuang igi_bitmap = dm_info->igi_bitmap & 0xf; 285e3037485SYan-Hsuan Chuang switch (igi_bitmap) { 286e3037485SYan-Hsuan Chuang case 5: 287e3037485SYan-Hsuan Chuang /* down -> up -> down -> up */ 288e3037485SYan-Hsuan Chuang if (igi_history[0] > igi_history[1] && 289e3037485SYan-Hsuan Chuang igi_history[2] > igi_history[3] && 290e3037485SYan-Hsuan Chuang igi_history[0] - igi_history[1] >= 2 && 291e3037485SYan-Hsuan Chuang igi_history[2] - igi_history[3] >= 2 && 292e3037485SYan-Hsuan Chuang fa_history[0] > fa_hi && fa_history[1] < fa_lo && 293e3037485SYan-Hsuan Chuang fa_history[2] > fa_hi && fa_history[3] < fa_lo) 294e3037485SYan-Hsuan Chuang damping = true; 295e3037485SYan-Hsuan Chuang break; 296e3037485SYan-Hsuan Chuang case 9: 297e3037485SYan-Hsuan Chuang /* up -> down -> down -> up */ 298e3037485SYan-Hsuan Chuang if (igi_history[0] > igi_history[1] && 299e3037485SYan-Hsuan Chuang igi_history[3] > igi_history[2] && 300e3037485SYan-Hsuan Chuang igi_history[0] - igi_history[1] >= 4 && 301e3037485SYan-Hsuan Chuang igi_history[3] - igi_history[2] >= 2 && 302e3037485SYan-Hsuan Chuang fa_history[0] > fa_hi && fa_history[1] < fa_lo && 303e3037485SYan-Hsuan Chuang fa_history[2] < fa_lo && fa_history[3] > fa_hi) 304e3037485SYan-Hsuan Chuang damping = true; 305e3037485SYan-Hsuan Chuang break; 306e3037485SYan-Hsuan Chuang default: 307e3037485SYan-Hsuan Chuang return false; 308e3037485SYan-Hsuan Chuang } 309e3037485SYan-Hsuan Chuang 310e3037485SYan-Hsuan Chuang if (damping) { 311e3037485SYan-Hsuan Chuang dm_info->damping = true; 312e3037485SYan-Hsuan Chuang dm_info->damping_cnt = 0; 313e3037485SYan-Hsuan Chuang dm_info->damping_rssi = min_rssi; 314e3037485SYan-Hsuan Chuang } 315e3037485SYan-Hsuan Chuang 316e3037485SYan-Hsuan Chuang return damping; 317e3037485SYan-Hsuan Chuang } 318e3037485SYan-Hsuan Chuang 319*76325506SZong-Zhe Yang static void rtw_phy_dig_get_boundary(struct rtw_dev *rtwdev, 320*76325506SZong-Zhe Yang struct rtw_dm_info *dm_info, 321e3037485SYan-Hsuan Chuang u8 *upper, u8 *lower, bool linked) 322e3037485SYan-Hsuan Chuang { 323e3037485SYan-Hsuan Chuang u8 dig_max, dig_min, dig_mid; 324e3037485SYan-Hsuan Chuang u8 min_rssi; 325e3037485SYan-Hsuan Chuang 326e3037485SYan-Hsuan Chuang if (linked) { 327e3037485SYan-Hsuan Chuang dig_max = DIG_PERF_MAX; 328e3037485SYan-Hsuan Chuang dig_mid = DIG_PERF_MID; 329*76325506SZong-Zhe Yang dig_min = rtwdev->chip->dig_min; 330e3037485SYan-Hsuan Chuang min_rssi = max_t(u8, dm_info->min_rssi, dig_min); 331e3037485SYan-Hsuan Chuang } else { 332e3037485SYan-Hsuan Chuang dig_max = DIG_CVRG_MAX; 333e3037485SYan-Hsuan Chuang dig_mid = DIG_CVRG_MID; 334e3037485SYan-Hsuan Chuang dig_min = DIG_CVRG_MIN; 335e3037485SYan-Hsuan Chuang min_rssi = dig_min; 336e3037485SYan-Hsuan Chuang } 337e3037485SYan-Hsuan Chuang 338e3037485SYan-Hsuan Chuang /* DIG MAX should be bounded by minimum RSSI with offset +15 */ 339e3037485SYan-Hsuan Chuang dig_max = min_t(u8, dig_max, min_rssi + DIG_RSSI_GAIN_OFFSET); 340e3037485SYan-Hsuan Chuang 341e3037485SYan-Hsuan Chuang *lower = clamp_t(u8, min_rssi, dig_min, dig_mid); 342e3037485SYan-Hsuan Chuang *upper = clamp_t(u8, *lower + DIG_RSSI_GAIN_OFFSET, dig_min, dig_max); 343e3037485SYan-Hsuan Chuang } 344e3037485SYan-Hsuan Chuang 345e3037485SYan-Hsuan Chuang static void rtw_phy_dig_get_threshold(struct rtw_dm_info *dm_info, 346e3037485SYan-Hsuan Chuang u16 *fa_th, u8 *step, bool linked) 347e3037485SYan-Hsuan Chuang { 348e3037485SYan-Hsuan Chuang u8 min_rssi, pre_min_rssi; 349e3037485SYan-Hsuan Chuang 350e3037485SYan-Hsuan Chuang min_rssi = dm_info->min_rssi; 351e3037485SYan-Hsuan Chuang pre_min_rssi = dm_info->pre_min_rssi; 352e3037485SYan-Hsuan Chuang step[0] = 4; 353e3037485SYan-Hsuan Chuang step[1] = 3; 354e3037485SYan-Hsuan Chuang step[2] = 2; 355e3037485SYan-Hsuan Chuang 356e3037485SYan-Hsuan Chuang if (linked) { 357e3037485SYan-Hsuan Chuang fa_th[0] = DIG_PERF_FA_TH_EXTRA_HIGH; 358e3037485SYan-Hsuan Chuang fa_th[1] = DIG_PERF_FA_TH_HIGH; 359e3037485SYan-Hsuan Chuang fa_th[2] = DIG_PERF_FA_TH_LOW; 360e3037485SYan-Hsuan Chuang if (pre_min_rssi > min_rssi) { 361e3037485SYan-Hsuan Chuang step[0] = 6; 362e3037485SYan-Hsuan Chuang step[1] = 4; 363e3037485SYan-Hsuan Chuang step[2] = 2; 364e3037485SYan-Hsuan Chuang } 365e3037485SYan-Hsuan Chuang } else { 366e3037485SYan-Hsuan Chuang fa_th[0] = DIG_CVRG_FA_TH_EXTRA_HIGH; 367e3037485SYan-Hsuan Chuang fa_th[1] = DIG_CVRG_FA_TH_HIGH; 368e3037485SYan-Hsuan Chuang fa_th[2] = DIG_CVRG_FA_TH_LOW; 369e3037485SYan-Hsuan Chuang } 370e3037485SYan-Hsuan Chuang } 371e3037485SYan-Hsuan Chuang 372e3037485SYan-Hsuan Chuang static void rtw_phy_dig_recorder(struct rtw_dm_info *dm_info, u8 igi, u16 fa) 373e3037485SYan-Hsuan Chuang { 374e3037485SYan-Hsuan Chuang u8 *igi_history; 375e3037485SYan-Hsuan Chuang u16 *fa_history; 376e3037485SYan-Hsuan Chuang u8 igi_bitmap; 377e3037485SYan-Hsuan Chuang bool up; 378e3037485SYan-Hsuan Chuang 379e3037485SYan-Hsuan Chuang igi_bitmap = dm_info->igi_bitmap << 1 & 0xfe; 380e3037485SYan-Hsuan Chuang igi_history = dm_info->igi_history; 381e3037485SYan-Hsuan Chuang fa_history = dm_info->fa_history; 382e3037485SYan-Hsuan Chuang 383e3037485SYan-Hsuan Chuang up = igi > igi_history[0]; 384e3037485SYan-Hsuan Chuang igi_bitmap |= up; 385e3037485SYan-Hsuan Chuang 386e3037485SYan-Hsuan Chuang igi_history[3] = igi_history[2]; 387e3037485SYan-Hsuan Chuang igi_history[2] = igi_history[1]; 388e3037485SYan-Hsuan Chuang igi_history[1] = igi_history[0]; 389e3037485SYan-Hsuan Chuang igi_history[0] = igi; 390e3037485SYan-Hsuan Chuang 391e3037485SYan-Hsuan Chuang fa_history[3] = fa_history[2]; 392e3037485SYan-Hsuan Chuang fa_history[2] = fa_history[1]; 393e3037485SYan-Hsuan Chuang fa_history[1] = fa_history[0]; 394e3037485SYan-Hsuan Chuang fa_history[0] = fa; 395e3037485SYan-Hsuan Chuang 396e3037485SYan-Hsuan Chuang dm_info->igi_bitmap = igi_bitmap; 397e3037485SYan-Hsuan Chuang } 398e3037485SYan-Hsuan Chuang 399e3037485SYan-Hsuan Chuang static void rtw_phy_dig(struct rtw_dev *rtwdev) 400e3037485SYan-Hsuan Chuang { 401e3037485SYan-Hsuan Chuang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 402e3037485SYan-Hsuan Chuang u8 upper_bound, lower_bound; 403e3037485SYan-Hsuan Chuang u8 pre_igi, cur_igi; 404e3037485SYan-Hsuan Chuang u16 fa_th[3], fa_cnt; 405e3037485SYan-Hsuan Chuang u8 level; 406e3037485SYan-Hsuan Chuang u8 step[3]; 407e3037485SYan-Hsuan Chuang bool linked; 408e3037485SYan-Hsuan Chuang 4093c519605SYan-Hsuan Chuang if (test_bit(RTW_FLAG_DIG_DISABLE, rtwdev->flags)) 410e3037485SYan-Hsuan Chuang return; 411e3037485SYan-Hsuan Chuang 412e3037485SYan-Hsuan Chuang if (rtw_phy_dig_check_damping(dm_info)) 413e3037485SYan-Hsuan Chuang return; 414e3037485SYan-Hsuan Chuang 415e3037485SYan-Hsuan Chuang linked = !!rtwdev->sta_cnt; 416e3037485SYan-Hsuan Chuang 417e3037485SYan-Hsuan Chuang fa_cnt = dm_info->total_fa_cnt; 418e3037485SYan-Hsuan Chuang pre_igi = dm_info->igi_history[0]; 419e3037485SYan-Hsuan Chuang 420e3037485SYan-Hsuan Chuang rtw_phy_dig_get_threshold(dm_info, fa_th, step, linked); 421e3037485SYan-Hsuan Chuang 422e3037485SYan-Hsuan Chuang /* test the false alarm count from the highest threshold level first, 423e3037485SYan-Hsuan Chuang * and increase it by corresponding step size 424e3037485SYan-Hsuan Chuang * 425e3037485SYan-Hsuan Chuang * note that the step size is offset by -2, compensate it afterall 426e3037485SYan-Hsuan Chuang */ 427e3037485SYan-Hsuan Chuang cur_igi = pre_igi; 428e3037485SYan-Hsuan Chuang for (level = 0; level < 3; level++) { 429e3037485SYan-Hsuan Chuang if (fa_cnt > fa_th[level]) { 430e3037485SYan-Hsuan Chuang cur_igi += step[level]; 431e3037485SYan-Hsuan Chuang break; 432e3037485SYan-Hsuan Chuang } 433e3037485SYan-Hsuan Chuang } 434e3037485SYan-Hsuan Chuang cur_igi -= 2; 435e3037485SYan-Hsuan Chuang 436e3037485SYan-Hsuan Chuang /* calculate the upper/lower bound by the minimum rssi we have among 437e3037485SYan-Hsuan Chuang * the peers connected with us, meanwhile make sure the igi value does 438e3037485SYan-Hsuan Chuang * not beyond the hardware limitation 439e3037485SYan-Hsuan Chuang */ 440*76325506SZong-Zhe Yang rtw_phy_dig_get_boundary(rtwdev, dm_info, &upper_bound, &lower_bound, 441*76325506SZong-Zhe Yang linked); 442e3037485SYan-Hsuan Chuang cur_igi = clamp_t(u8, cur_igi, lower_bound, upper_bound); 443e3037485SYan-Hsuan Chuang 444e3037485SYan-Hsuan Chuang /* record current igi value and false alarm statistics for further 445e3037485SYan-Hsuan Chuang * damping checks, and record the trend of igi values 446e3037485SYan-Hsuan Chuang */ 447e3037485SYan-Hsuan Chuang rtw_phy_dig_recorder(dm_info, cur_igi, fa_cnt); 448e3037485SYan-Hsuan Chuang 449e3037485SYan-Hsuan Chuang if (cur_igi != pre_igi) 450e3037485SYan-Hsuan Chuang rtw_phy_dig_write(rtwdev, cur_igi); 451e3037485SYan-Hsuan Chuang } 452e3037485SYan-Hsuan Chuang 453e3037485SYan-Hsuan Chuang static void rtw_phy_ra_info_update_iter(void *data, struct ieee80211_sta *sta) 454e3037485SYan-Hsuan Chuang { 455e3037485SYan-Hsuan Chuang struct rtw_dev *rtwdev = data; 456e3037485SYan-Hsuan Chuang struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv; 457e3037485SYan-Hsuan Chuang 458e3037485SYan-Hsuan Chuang rtw_update_sta_info(rtwdev, si); 459e3037485SYan-Hsuan Chuang } 460e3037485SYan-Hsuan Chuang 461e3037485SYan-Hsuan Chuang static void rtw_phy_ra_info_update(struct rtw_dev *rtwdev) 462e3037485SYan-Hsuan Chuang { 463e3037485SYan-Hsuan Chuang if (rtwdev->watch_dog_cnt & 0x3) 464e3037485SYan-Hsuan Chuang return; 465e3037485SYan-Hsuan Chuang 466e3037485SYan-Hsuan Chuang rtw_iterate_stas_atomic(rtwdev, rtw_phy_ra_info_update_iter, rtwdev); 467e3037485SYan-Hsuan Chuang } 468e3037485SYan-Hsuan Chuang 46948308726SPo-Hao Huang static u32 rtw_phy_get_rrsr_mask(struct rtw_dev *rtwdev, u8 rate_idx) 47048308726SPo-Hao Huang { 47148308726SPo-Hao Huang u8 rate_order; 47248308726SPo-Hao Huang 47348308726SPo-Hao Huang rate_order = rate_idx; 47448308726SPo-Hao Huang 47548308726SPo-Hao Huang if (rate_idx >= DESC_RATEVHT4SS_MCS0) 47648308726SPo-Hao Huang rate_order -= DESC_RATEVHT4SS_MCS0; 47748308726SPo-Hao Huang else if (rate_idx >= DESC_RATEVHT3SS_MCS0) 47848308726SPo-Hao Huang rate_order -= DESC_RATEVHT3SS_MCS0; 47948308726SPo-Hao Huang else if (rate_idx >= DESC_RATEVHT2SS_MCS0) 48048308726SPo-Hao Huang rate_order -= DESC_RATEVHT2SS_MCS0; 48148308726SPo-Hao Huang else if (rate_idx >= DESC_RATEVHT1SS_MCS0) 48248308726SPo-Hao Huang rate_order -= DESC_RATEVHT1SS_MCS0; 48348308726SPo-Hao Huang else if (rate_idx >= DESC_RATEMCS24) 48448308726SPo-Hao Huang rate_order -= DESC_RATEMCS24; 48548308726SPo-Hao Huang else if (rate_idx >= DESC_RATEMCS16) 48648308726SPo-Hao Huang rate_order -= DESC_RATEMCS16; 48748308726SPo-Hao Huang else if (rate_idx >= DESC_RATEMCS8) 48848308726SPo-Hao Huang rate_order -= DESC_RATEMCS8; 48948308726SPo-Hao Huang else if (rate_idx >= DESC_RATEMCS0) 49048308726SPo-Hao Huang rate_order -= DESC_RATEMCS0; 49148308726SPo-Hao Huang else if (rate_idx >= DESC_RATE6M) 49248308726SPo-Hao Huang rate_order -= DESC_RATE6M; 49348308726SPo-Hao Huang else 49448308726SPo-Hao Huang rate_order -= DESC_RATE1M; 49548308726SPo-Hao Huang 49648308726SPo-Hao Huang if (rate_idx >= DESC_RATEMCS0 || rate_order == 0) 49748308726SPo-Hao Huang rate_order++; 49848308726SPo-Hao Huang 49948308726SPo-Hao Huang return GENMASK(rate_order + RRSR_RATE_ORDER_CCK_LEN - 1, 0); 50048308726SPo-Hao Huang } 50148308726SPo-Hao Huang 50248308726SPo-Hao Huang static void rtw_phy_rrsr_mask_min_iter(void *data, struct ieee80211_sta *sta) 50348308726SPo-Hao Huang { 50448308726SPo-Hao Huang struct rtw_dev *rtwdev = (struct rtw_dev *)data; 50548308726SPo-Hao Huang struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv; 50648308726SPo-Hao Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 50748308726SPo-Hao Huang u32 mask = 0; 50848308726SPo-Hao Huang 50948308726SPo-Hao Huang mask = rtw_phy_get_rrsr_mask(rtwdev, si->ra_report.desc_rate); 51048308726SPo-Hao Huang if (mask < dm_info->rrsr_mask_min) 51148308726SPo-Hao Huang dm_info->rrsr_mask_min = mask; 51248308726SPo-Hao Huang } 51348308726SPo-Hao Huang 51448308726SPo-Hao Huang static void rtw_phy_rrsr_update(struct rtw_dev *rtwdev) 51548308726SPo-Hao Huang { 51648308726SPo-Hao Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 51748308726SPo-Hao Huang 51848308726SPo-Hao Huang dm_info->rrsr_mask_min = RRSR_RATE_ORDER_MAX; 51948308726SPo-Hao Huang rtw_iterate_stas_atomic(rtwdev, rtw_phy_rrsr_mask_min_iter, rtwdev); 52048308726SPo-Hao Huang rtw_write32(rtwdev, REG_RRSR, dm_info->rrsr_val_init & dm_info->rrsr_mask_min); 52148308726SPo-Hao Huang } 52248308726SPo-Hao Huang 5235227c2eeSTzu-En Huang static void rtw_phy_dpk_track(struct rtw_dev *rtwdev) 5245227c2eeSTzu-En Huang { 5255227c2eeSTzu-En Huang struct rtw_chip_info *chip = rtwdev->chip; 5265227c2eeSTzu-En Huang 5275227c2eeSTzu-En Huang if (chip->ops->dpk_track) 5285227c2eeSTzu-En Huang chip->ops->dpk_track(rtwdev); 5295227c2eeSTzu-En Huang } 5305227c2eeSTzu-En Huang 531479c4ee9STzu-En Huang #define CCK_PD_FA_LV1_MIN 1000 532479c4ee9STzu-En Huang #define CCK_PD_FA_LV0_MAX 500 533479c4ee9STzu-En Huang 534479c4ee9STzu-En Huang static u8 rtw_phy_cck_pd_lv_unlink(struct rtw_dev *rtwdev) 535479c4ee9STzu-En Huang { 536479c4ee9STzu-En Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 537479c4ee9STzu-En Huang u32 cck_fa_avg = dm_info->cck_fa_avg; 538479c4ee9STzu-En Huang 539479c4ee9STzu-En Huang if (cck_fa_avg > CCK_PD_FA_LV1_MIN) 54018a0696eSTzu-En Huang return CCK_PD_LV1; 541479c4ee9STzu-En Huang 542479c4ee9STzu-En Huang if (cck_fa_avg < CCK_PD_FA_LV0_MAX) 54318a0696eSTzu-En Huang return CCK_PD_LV0; 544479c4ee9STzu-En Huang 545479c4ee9STzu-En Huang return CCK_PD_LV_MAX; 546479c4ee9STzu-En Huang } 547479c4ee9STzu-En Huang 548479c4ee9STzu-En Huang #define CCK_PD_IGI_LV4_VAL 0x38 549479c4ee9STzu-En Huang #define CCK_PD_IGI_LV3_VAL 0x2a 550479c4ee9STzu-En Huang #define CCK_PD_IGI_LV2_VAL 0x24 551479c4ee9STzu-En Huang #define CCK_PD_RSSI_LV4_VAL 32 552479c4ee9STzu-En Huang #define CCK_PD_RSSI_LV3_VAL 32 553479c4ee9STzu-En Huang #define CCK_PD_RSSI_LV2_VAL 24 554479c4ee9STzu-En Huang 555479c4ee9STzu-En Huang static u8 rtw_phy_cck_pd_lv_link(struct rtw_dev *rtwdev) 556479c4ee9STzu-En Huang { 557479c4ee9STzu-En Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 558479c4ee9STzu-En Huang u8 igi = dm_info->igi_history[0]; 559479c4ee9STzu-En Huang u8 rssi = dm_info->min_rssi; 560479c4ee9STzu-En Huang u32 cck_fa_avg = dm_info->cck_fa_avg; 561479c4ee9STzu-En Huang 562479c4ee9STzu-En Huang if (igi > CCK_PD_IGI_LV4_VAL && rssi > CCK_PD_RSSI_LV4_VAL) 56318a0696eSTzu-En Huang return CCK_PD_LV4; 564479c4ee9STzu-En Huang if (igi > CCK_PD_IGI_LV3_VAL && rssi > CCK_PD_RSSI_LV3_VAL) 56518a0696eSTzu-En Huang return CCK_PD_LV3; 566479c4ee9STzu-En Huang if (igi > CCK_PD_IGI_LV2_VAL || rssi > CCK_PD_RSSI_LV2_VAL) 56718a0696eSTzu-En Huang return CCK_PD_LV2; 568479c4ee9STzu-En Huang if (cck_fa_avg > CCK_PD_FA_LV1_MIN) 56918a0696eSTzu-En Huang return CCK_PD_LV1; 570479c4ee9STzu-En Huang if (cck_fa_avg < CCK_PD_FA_LV0_MAX) 57118a0696eSTzu-En Huang return CCK_PD_LV0; 572479c4ee9STzu-En Huang 573479c4ee9STzu-En Huang return CCK_PD_LV_MAX; 574479c4ee9STzu-En Huang } 575479c4ee9STzu-En Huang 576479c4ee9STzu-En Huang static u8 rtw_phy_cck_pd_lv(struct rtw_dev *rtwdev) 577479c4ee9STzu-En Huang { 578479c4ee9STzu-En Huang if (!rtw_is_assoc(rtwdev)) 579479c4ee9STzu-En Huang return rtw_phy_cck_pd_lv_unlink(rtwdev); 580479c4ee9STzu-En Huang else 581479c4ee9STzu-En Huang return rtw_phy_cck_pd_lv_link(rtwdev); 582479c4ee9STzu-En Huang } 583479c4ee9STzu-En Huang 584479c4ee9STzu-En Huang static void rtw_phy_cck_pd(struct rtw_dev *rtwdev) 585479c4ee9STzu-En Huang { 586479c4ee9STzu-En Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 587479c4ee9STzu-En Huang struct rtw_chip_info *chip = rtwdev->chip; 588479c4ee9STzu-En Huang u32 cck_fa = dm_info->cck_fa_cnt; 589479c4ee9STzu-En Huang u8 level; 590479c4ee9STzu-En Huang 591479c4ee9STzu-En Huang if (rtwdev->hal.current_band_type != RTW_BAND_2G) 592479c4ee9STzu-En Huang return; 593479c4ee9STzu-En Huang 594479c4ee9STzu-En Huang if (dm_info->cck_fa_avg == CCK_FA_AVG_RESET) 595479c4ee9STzu-En Huang dm_info->cck_fa_avg = cck_fa; 596479c4ee9STzu-En Huang else 597479c4ee9STzu-En Huang dm_info->cck_fa_avg = (dm_info->cck_fa_avg * 3 + cck_fa) >> 2; 598479c4ee9STzu-En Huang 599760bb2abSPing-Ke Shih rtw_dbg(rtwdev, RTW_DBG_PHY, "IGI=0x%x, rssi_min=%d, cck_fa=%d\n", 600760bb2abSPing-Ke Shih dm_info->igi_history[0], dm_info->min_rssi, 601760bb2abSPing-Ke Shih dm_info->fa_history[0]); 602760bb2abSPing-Ke Shih rtw_dbg(rtwdev, RTW_DBG_PHY, "cck_fa_avg=%d, cck_pd_default=%d\n", 603760bb2abSPing-Ke Shih dm_info->cck_fa_avg, dm_info->cck_pd_default); 604760bb2abSPing-Ke Shih 605479c4ee9STzu-En Huang level = rtw_phy_cck_pd_lv(rtwdev); 606479c4ee9STzu-En Huang 607479c4ee9STzu-En Huang if (level >= CCK_PD_LV_MAX) 608479c4ee9STzu-En Huang return; 609479c4ee9STzu-En Huang 610479c4ee9STzu-En Huang if (chip->ops->cck_pd_set) 611479c4ee9STzu-En Huang chip->ops->cck_pd_set(rtwdev, level); 612479c4ee9STzu-En Huang } 613479c4ee9STzu-En Huang 614c97ee3e0STzu-En Huang static void rtw_phy_pwr_track(struct rtw_dev *rtwdev) 615c97ee3e0STzu-En Huang { 616c97ee3e0STzu-En Huang rtwdev->chip->ops->pwr_track(rtwdev); 617c97ee3e0STzu-En Huang } 618c97ee3e0STzu-En Huang 61948308726SPo-Hao Huang static void rtw_phy_ra_track(struct rtw_dev *rtwdev) 62048308726SPo-Hao Huang { 62148308726SPo-Hao Huang rtw_phy_ra_info_update(rtwdev); 62248308726SPo-Hao Huang rtw_phy_rrsr_update(rtwdev); 62348308726SPo-Hao Huang } 62448308726SPo-Hao Huang 625e3037485SYan-Hsuan Chuang void rtw_phy_dynamic_mechanism(struct rtw_dev *rtwdev) 626e3037485SYan-Hsuan Chuang { 627e3037485SYan-Hsuan Chuang /* for further calculation */ 628e3037485SYan-Hsuan Chuang rtw_phy_statistics(rtwdev); 629e3037485SYan-Hsuan Chuang rtw_phy_dig(rtwdev); 630479c4ee9STzu-En Huang rtw_phy_cck_pd(rtwdev); 63148308726SPo-Hao Huang rtw_phy_ra_track(rtwdev); 6325227c2eeSTzu-En Huang rtw_phy_dpk_track(rtwdev); 633c97ee3e0STzu-En Huang rtw_phy_pwr_track(rtwdev); 634e3037485SYan-Hsuan Chuang } 635e3037485SYan-Hsuan Chuang 636e3037485SYan-Hsuan Chuang #define FRAC_BITS 3 637e3037485SYan-Hsuan Chuang 638e3037485SYan-Hsuan Chuang static u8 rtw_phy_power_2_db(s8 power) 639e3037485SYan-Hsuan Chuang { 640e3037485SYan-Hsuan Chuang if (power <= -100 || power >= 20) 641e3037485SYan-Hsuan Chuang return 0; 642e3037485SYan-Hsuan Chuang else if (power >= 0) 643e3037485SYan-Hsuan Chuang return 100; 644e3037485SYan-Hsuan Chuang else 645e3037485SYan-Hsuan Chuang return 100 + power; 646e3037485SYan-Hsuan Chuang } 647e3037485SYan-Hsuan Chuang 648e3037485SYan-Hsuan Chuang static u64 rtw_phy_db_2_linear(u8 power_db) 649e3037485SYan-Hsuan Chuang { 650e3037485SYan-Hsuan Chuang u8 i, j; 651e3037485SYan-Hsuan Chuang u64 linear; 652e3037485SYan-Hsuan Chuang 6538a03447dSStanislaw Gruszka if (power_db > 96) 6548a03447dSStanislaw Gruszka power_db = 96; 6558a03447dSStanislaw Gruszka else if (power_db < 1) 6568a03447dSStanislaw Gruszka return 1; 6578a03447dSStanislaw Gruszka 658e3037485SYan-Hsuan Chuang /* 1dB ~ 96dB */ 659e3037485SYan-Hsuan Chuang i = (power_db - 1) >> 3; 660e3037485SYan-Hsuan Chuang j = (power_db - 1) - (i << 3); 661e3037485SYan-Hsuan Chuang 662e3037485SYan-Hsuan Chuang linear = db_invert_table[i][j]; 663e3037485SYan-Hsuan Chuang linear = i > 2 ? linear << FRAC_BITS : linear; 664e3037485SYan-Hsuan Chuang 665e3037485SYan-Hsuan Chuang return linear; 666e3037485SYan-Hsuan Chuang } 667e3037485SYan-Hsuan Chuang 668e3037485SYan-Hsuan Chuang static u8 rtw_phy_linear_2_db(u64 linear) 669e3037485SYan-Hsuan Chuang { 670e3037485SYan-Hsuan Chuang u8 i; 671e3037485SYan-Hsuan Chuang u8 j; 672e3037485SYan-Hsuan Chuang u32 dB; 673e3037485SYan-Hsuan Chuang 674e3037485SYan-Hsuan Chuang if (linear >= db_invert_table[11][7]) 675e3037485SYan-Hsuan Chuang return 96; /* maximum 96 dB */ 676e3037485SYan-Hsuan Chuang 677e3037485SYan-Hsuan Chuang for (i = 0; i < 12; i++) { 678e3037485SYan-Hsuan Chuang if (i <= 2 && (linear << FRAC_BITS) <= db_invert_table[i][7]) 679e3037485SYan-Hsuan Chuang break; 680e3037485SYan-Hsuan Chuang else if (i > 2 && linear <= db_invert_table[i][7]) 681e3037485SYan-Hsuan Chuang break; 682e3037485SYan-Hsuan Chuang } 683e3037485SYan-Hsuan Chuang 684e3037485SYan-Hsuan Chuang for (j = 0; j < 8; j++) { 685e3037485SYan-Hsuan Chuang if (i <= 2 && (linear << FRAC_BITS) <= db_invert_table[i][j]) 686e3037485SYan-Hsuan Chuang break; 687e3037485SYan-Hsuan Chuang else if (i > 2 && linear <= db_invert_table[i][j]) 688e3037485SYan-Hsuan Chuang break; 689e3037485SYan-Hsuan Chuang } 690e3037485SYan-Hsuan Chuang 691e3037485SYan-Hsuan Chuang if (j == 0 && i == 0) 692e3037485SYan-Hsuan Chuang goto end; 693e3037485SYan-Hsuan Chuang 694e3037485SYan-Hsuan Chuang if (j == 0) { 695e3037485SYan-Hsuan Chuang if (i != 3) { 696e3037485SYan-Hsuan Chuang if (db_invert_table[i][0] - linear > 697e3037485SYan-Hsuan Chuang linear - db_invert_table[i - 1][7]) { 698e3037485SYan-Hsuan Chuang i = i - 1; 699e3037485SYan-Hsuan Chuang j = 7; 700e3037485SYan-Hsuan Chuang } 701e3037485SYan-Hsuan Chuang } else { 702e3037485SYan-Hsuan Chuang if (db_invert_table[3][0] - linear > 703e3037485SYan-Hsuan Chuang linear - db_invert_table[2][7]) { 704e3037485SYan-Hsuan Chuang i = 2; 705e3037485SYan-Hsuan Chuang j = 7; 706e3037485SYan-Hsuan Chuang } 707e3037485SYan-Hsuan Chuang } 708e3037485SYan-Hsuan Chuang } else { 709e3037485SYan-Hsuan Chuang if (db_invert_table[i][j] - linear > 710e3037485SYan-Hsuan Chuang linear - db_invert_table[i][j - 1]) { 711e3037485SYan-Hsuan Chuang j = j - 1; 712e3037485SYan-Hsuan Chuang } 713e3037485SYan-Hsuan Chuang } 714e3037485SYan-Hsuan Chuang end: 715e3037485SYan-Hsuan Chuang dB = (i << 3) + j + 1; 716e3037485SYan-Hsuan Chuang 717e3037485SYan-Hsuan Chuang return dB; 718e3037485SYan-Hsuan Chuang } 719e3037485SYan-Hsuan Chuang 720e3037485SYan-Hsuan Chuang u8 rtw_phy_rf_power_2_rssi(s8 *rf_power, u8 path_num) 721e3037485SYan-Hsuan Chuang { 722e3037485SYan-Hsuan Chuang s8 power; 723e3037485SYan-Hsuan Chuang u8 power_db; 724e3037485SYan-Hsuan Chuang u64 linear; 725e3037485SYan-Hsuan Chuang u64 sum = 0; 726e3037485SYan-Hsuan Chuang u8 path; 727e3037485SYan-Hsuan Chuang 728e3037485SYan-Hsuan Chuang for (path = 0; path < path_num; path++) { 729e3037485SYan-Hsuan Chuang power = rf_power[path]; 730e3037485SYan-Hsuan Chuang power_db = rtw_phy_power_2_db(power); 731e3037485SYan-Hsuan Chuang linear = rtw_phy_db_2_linear(power_db); 732e3037485SYan-Hsuan Chuang sum += linear; 733e3037485SYan-Hsuan Chuang } 734e3037485SYan-Hsuan Chuang 735e3037485SYan-Hsuan Chuang sum = (sum + (1 << (FRAC_BITS - 1))) >> FRAC_BITS; 736e3037485SYan-Hsuan Chuang switch (path_num) { 737e3037485SYan-Hsuan Chuang case 2: 738e3037485SYan-Hsuan Chuang sum >>= 1; 739e3037485SYan-Hsuan Chuang break; 740e3037485SYan-Hsuan Chuang case 3: 741e3037485SYan-Hsuan Chuang sum = ((sum) + ((sum) << 1) + ((sum) << 3)) >> 5; 742e3037485SYan-Hsuan Chuang break; 743e3037485SYan-Hsuan Chuang case 4: 744e3037485SYan-Hsuan Chuang sum >>= 2; 745e3037485SYan-Hsuan Chuang break; 746e3037485SYan-Hsuan Chuang default: 747e3037485SYan-Hsuan Chuang break; 748e3037485SYan-Hsuan Chuang } 749e3037485SYan-Hsuan Chuang 750e3037485SYan-Hsuan Chuang return rtw_phy_linear_2_db(sum); 751e3037485SYan-Hsuan Chuang } 752449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_rf_power_2_rssi); 753e3037485SYan-Hsuan Chuang 754e3037485SYan-Hsuan Chuang u32 rtw_phy_read_rf(struct rtw_dev *rtwdev, enum rtw_rf_path rf_path, 755e3037485SYan-Hsuan Chuang u32 addr, u32 mask) 756e3037485SYan-Hsuan Chuang { 757e3037485SYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 758e3037485SYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 759e3037485SYan-Hsuan Chuang const u32 *base_addr = chip->rf_base_addr; 760e3037485SYan-Hsuan Chuang u32 val, direct_addr; 761e3037485SYan-Hsuan Chuang 762e0c27cdbSPing-Ke Shih if (rf_path >= hal->rf_phy_num) { 763e3037485SYan-Hsuan Chuang rtw_err(rtwdev, "unsupported rf path (%d)\n", rf_path); 764e3037485SYan-Hsuan Chuang return INV_RF_DATA; 765e3037485SYan-Hsuan Chuang } 766e3037485SYan-Hsuan Chuang 767e3037485SYan-Hsuan Chuang addr &= 0xff; 768e3037485SYan-Hsuan Chuang direct_addr = base_addr[rf_path] + (addr << 2); 769e3037485SYan-Hsuan Chuang mask &= RFREG_MASK; 770e3037485SYan-Hsuan Chuang 771e3037485SYan-Hsuan Chuang val = rtw_read32_mask(rtwdev, direct_addr, mask); 772e3037485SYan-Hsuan Chuang 773e3037485SYan-Hsuan Chuang return val; 774e3037485SYan-Hsuan Chuang } 775449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_read_rf); 776e3037485SYan-Hsuan Chuang 777e0c27cdbSPing-Ke Shih u32 rtw_phy_read_rf_sipi(struct rtw_dev *rtwdev, enum rtw_rf_path rf_path, 778e0c27cdbSPing-Ke Shih u32 addr, u32 mask) 779e0c27cdbSPing-Ke Shih { 780e0c27cdbSPing-Ke Shih struct rtw_hal *hal = &rtwdev->hal; 781e0c27cdbSPing-Ke Shih struct rtw_chip_info *chip = rtwdev->chip; 782e0c27cdbSPing-Ke Shih const struct rtw_rf_sipi_addr *rf_sipi_addr; 783e0c27cdbSPing-Ke Shih const struct rtw_rf_sipi_addr *rf_sipi_addr_a; 784e0c27cdbSPing-Ke Shih u32 val32; 785e0c27cdbSPing-Ke Shih u32 en_pi; 786e0c27cdbSPing-Ke Shih u32 r_addr; 787e0c27cdbSPing-Ke Shih u32 shift; 788e0c27cdbSPing-Ke Shih 789e0c27cdbSPing-Ke Shih if (rf_path >= hal->rf_phy_num) { 790e0c27cdbSPing-Ke Shih rtw_err(rtwdev, "unsupported rf path (%d)\n", rf_path); 791e0c27cdbSPing-Ke Shih return INV_RF_DATA; 792e0c27cdbSPing-Ke Shih } 793e0c27cdbSPing-Ke Shih 794e0c27cdbSPing-Ke Shih if (!chip->rf_sipi_read_addr) { 795e0c27cdbSPing-Ke Shih rtw_err(rtwdev, "rf_sipi_read_addr isn't defined\n"); 796e0c27cdbSPing-Ke Shih return INV_RF_DATA; 797e0c27cdbSPing-Ke Shih } 798e0c27cdbSPing-Ke Shih 799e0c27cdbSPing-Ke Shih rf_sipi_addr = &chip->rf_sipi_read_addr[rf_path]; 800e0c27cdbSPing-Ke Shih rf_sipi_addr_a = &chip->rf_sipi_read_addr[RF_PATH_A]; 801e0c27cdbSPing-Ke Shih 802e0c27cdbSPing-Ke Shih addr &= 0xff; 803e0c27cdbSPing-Ke Shih 804e0c27cdbSPing-Ke Shih val32 = rtw_read32(rtwdev, rf_sipi_addr->hssi_2); 805e0c27cdbSPing-Ke Shih val32 = (val32 & ~LSSI_READ_ADDR_MASK) | (addr << 23); 806e0c27cdbSPing-Ke Shih rtw_write32(rtwdev, rf_sipi_addr->hssi_2, val32); 807e0c27cdbSPing-Ke Shih 808e0c27cdbSPing-Ke Shih /* toggle read edge of path A */ 809e0c27cdbSPing-Ke Shih val32 = rtw_read32(rtwdev, rf_sipi_addr_a->hssi_2); 810e0c27cdbSPing-Ke Shih rtw_write32(rtwdev, rf_sipi_addr_a->hssi_2, val32 & ~LSSI_READ_EDGE_MASK); 811e0c27cdbSPing-Ke Shih rtw_write32(rtwdev, rf_sipi_addr_a->hssi_2, val32 | LSSI_READ_EDGE_MASK); 812e0c27cdbSPing-Ke Shih 813e0c27cdbSPing-Ke Shih udelay(120); 814e0c27cdbSPing-Ke Shih 815e0c27cdbSPing-Ke Shih en_pi = rtw_read32_mask(rtwdev, rf_sipi_addr->hssi_1, BIT(8)); 816e0c27cdbSPing-Ke Shih r_addr = en_pi ? rf_sipi_addr->lssi_read_pi : rf_sipi_addr->lssi_read; 817e0c27cdbSPing-Ke Shih 818e0c27cdbSPing-Ke Shih val32 = rtw_read32_mask(rtwdev, r_addr, LSSI_READ_DATA_MASK); 819e0c27cdbSPing-Ke Shih 820e0c27cdbSPing-Ke Shih shift = __ffs(mask); 821e0c27cdbSPing-Ke Shih 822e0c27cdbSPing-Ke Shih return (val32 & mask) >> shift; 823e0c27cdbSPing-Ke Shih } 824449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_read_rf_sipi); 825e0c27cdbSPing-Ke Shih 826e3037485SYan-Hsuan Chuang bool rtw_phy_write_rf_reg_sipi(struct rtw_dev *rtwdev, enum rtw_rf_path rf_path, 827e3037485SYan-Hsuan Chuang u32 addr, u32 mask, u32 data) 828e3037485SYan-Hsuan Chuang { 829e3037485SYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 830e3037485SYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 831e3037485SYan-Hsuan Chuang u32 *sipi_addr = chip->rf_sipi_addr; 832e3037485SYan-Hsuan Chuang u32 data_and_addr; 833e3037485SYan-Hsuan Chuang u32 old_data = 0; 834e3037485SYan-Hsuan Chuang u32 shift; 835e3037485SYan-Hsuan Chuang 836e0c27cdbSPing-Ke Shih if (rf_path >= hal->rf_phy_num) { 837e3037485SYan-Hsuan Chuang rtw_err(rtwdev, "unsupported rf path (%d)\n", rf_path); 838e3037485SYan-Hsuan Chuang return false; 839e3037485SYan-Hsuan Chuang } 840e3037485SYan-Hsuan Chuang 841e3037485SYan-Hsuan Chuang addr &= 0xff; 842e3037485SYan-Hsuan Chuang mask &= RFREG_MASK; 843e3037485SYan-Hsuan Chuang 844e3037485SYan-Hsuan Chuang if (mask != RFREG_MASK) { 845e0c27cdbSPing-Ke Shih old_data = chip->ops->read_rf(rtwdev, rf_path, addr, RFREG_MASK); 846e3037485SYan-Hsuan Chuang 847e3037485SYan-Hsuan Chuang if (old_data == INV_RF_DATA) { 848e3037485SYan-Hsuan Chuang rtw_err(rtwdev, "Write fail, rf is disabled\n"); 849e3037485SYan-Hsuan Chuang return false; 850e3037485SYan-Hsuan Chuang } 851e3037485SYan-Hsuan Chuang 852e3037485SYan-Hsuan Chuang shift = __ffs(mask); 853e3037485SYan-Hsuan Chuang data = ((old_data) & (~mask)) | (data << shift); 854e3037485SYan-Hsuan Chuang } 855e3037485SYan-Hsuan Chuang 856e3037485SYan-Hsuan Chuang data_and_addr = ((addr << 20) | (data & 0x000fffff)) & 0x0fffffff; 857e3037485SYan-Hsuan Chuang 858e3037485SYan-Hsuan Chuang rtw_write32(rtwdev, sipi_addr[rf_path], data_and_addr); 859e3037485SYan-Hsuan Chuang 860e3037485SYan-Hsuan Chuang udelay(13); 861e3037485SYan-Hsuan Chuang 862e3037485SYan-Hsuan Chuang return true; 863e3037485SYan-Hsuan Chuang } 864449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_write_rf_reg_sipi); 865e3037485SYan-Hsuan Chuang 866e3037485SYan-Hsuan Chuang bool rtw_phy_write_rf_reg(struct rtw_dev *rtwdev, enum rtw_rf_path rf_path, 867e3037485SYan-Hsuan Chuang u32 addr, u32 mask, u32 data) 868e3037485SYan-Hsuan Chuang { 869e3037485SYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 870e3037485SYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 871e3037485SYan-Hsuan Chuang const u32 *base_addr = chip->rf_base_addr; 872e3037485SYan-Hsuan Chuang u32 direct_addr; 873e3037485SYan-Hsuan Chuang 874e0c27cdbSPing-Ke Shih if (rf_path >= hal->rf_phy_num) { 875e3037485SYan-Hsuan Chuang rtw_err(rtwdev, "unsupported rf path (%d)\n", rf_path); 876e3037485SYan-Hsuan Chuang return false; 877e3037485SYan-Hsuan Chuang } 878e3037485SYan-Hsuan Chuang 879e3037485SYan-Hsuan Chuang addr &= 0xff; 880e3037485SYan-Hsuan Chuang direct_addr = base_addr[rf_path] + (addr << 2); 881e3037485SYan-Hsuan Chuang mask &= RFREG_MASK; 882e3037485SYan-Hsuan Chuang 883e3037485SYan-Hsuan Chuang rtw_write32_mask(rtwdev, direct_addr, mask, data); 884e3037485SYan-Hsuan Chuang 885e3037485SYan-Hsuan Chuang udelay(1); 886e3037485SYan-Hsuan Chuang 887e3037485SYan-Hsuan Chuang return true; 888e3037485SYan-Hsuan Chuang } 889e3037485SYan-Hsuan Chuang 890e3037485SYan-Hsuan Chuang bool rtw_phy_write_rf_reg_mix(struct rtw_dev *rtwdev, enum rtw_rf_path rf_path, 891e3037485SYan-Hsuan Chuang u32 addr, u32 mask, u32 data) 892e3037485SYan-Hsuan Chuang { 893e3037485SYan-Hsuan Chuang if (addr != 0x00) 894e3037485SYan-Hsuan Chuang return rtw_phy_write_rf_reg(rtwdev, rf_path, addr, mask, data); 895e3037485SYan-Hsuan Chuang 896e3037485SYan-Hsuan Chuang return rtw_phy_write_rf_reg_sipi(rtwdev, rf_path, addr, mask, data); 897e3037485SYan-Hsuan Chuang } 898449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_write_rf_reg_mix); 899e3037485SYan-Hsuan Chuang 900e3037485SYan-Hsuan Chuang void rtw_phy_setup_phy_cond(struct rtw_dev *rtwdev, u32 pkg) 901e3037485SYan-Hsuan Chuang { 902e3037485SYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 903e3037485SYan-Hsuan Chuang struct rtw_efuse *efuse = &rtwdev->efuse; 904e3037485SYan-Hsuan Chuang struct rtw_phy_cond cond = {0}; 905e3037485SYan-Hsuan Chuang 906e3037485SYan-Hsuan Chuang cond.cut = hal->cut_version ? hal->cut_version : 15; 907e3037485SYan-Hsuan Chuang cond.pkg = pkg ? pkg : 15; 908e3037485SYan-Hsuan Chuang cond.plat = 0x04; 909e3037485SYan-Hsuan Chuang cond.rfe = efuse->rfe_option; 910e3037485SYan-Hsuan Chuang 911e3037485SYan-Hsuan Chuang switch (rtw_hci_type(rtwdev)) { 912e3037485SYan-Hsuan Chuang case RTW_HCI_TYPE_USB: 913e3037485SYan-Hsuan Chuang cond.intf = INTF_USB; 914e3037485SYan-Hsuan Chuang break; 915e3037485SYan-Hsuan Chuang case RTW_HCI_TYPE_SDIO: 916e3037485SYan-Hsuan Chuang cond.intf = INTF_SDIO; 917e3037485SYan-Hsuan Chuang break; 918e3037485SYan-Hsuan Chuang case RTW_HCI_TYPE_PCIE: 919e3037485SYan-Hsuan Chuang default: 920e3037485SYan-Hsuan Chuang cond.intf = INTF_PCIE; 921e3037485SYan-Hsuan Chuang break; 922e3037485SYan-Hsuan Chuang } 923e3037485SYan-Hsuan Chuang 924e3037485SYan-Hsuan Chuang hal->phy_cond = cond; 925e3037485SYan-Hsuan Chuang 926e3037485SYan-Hsuan Chuang rtw_dbg(rtwdev, RTW_DBG_PHY, "phy cond=0x%08x\n", *((u32 *)&hal->phy_cond)); 927e3037485SYan-Hsuan Chuang } 928e3037485SYan-Hsuan Chuang 929e3037485SYan-Hsuan Chuang static bool check_positive(struct rtw_dev *rtwdev, struct rtw_phy_cond cond) 930e3037485SYan-Hsuan Chuang { 931e3037485SYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 932e3037485SYan-Hsuan Chuang struct rtw_phy_cond drv_cond = hal->phy_cond; 933e3037485SYan-Hsuan Chuang 934e3037485SYan-Hsuan Chuang if (cond.cut && cond.cut != drv_cond.cut) 935e3037485SYan-Hsuan Chuang return false; 936e3037485SYan-Hsuan Chuang 937e3037485SYan-Hsuan Chuang if (cond.pkg && cond.pkg != drv_cond.pkg) 938e3037485SYan-Hsuan Chuang return false; 939e3037485SYan-Hsuan Chuang 940e3037485SYan-Hsuan Chuang if (cond.intf && cond.intf != drv_cond.intf) 941e3037485SYan-Hsuan Chuang return false; 942e3037485SYan-Hsuan Chuang 943e3037485SYan-Hsuan Chuang if (cond.rfe != drv_cond.rfe) 944e3037485SYan-Hsuan Chuang return false; 945e3037485SYan-Hsuan Chuang 946e3037485SYan-Hsuan Chuang return true; 947e3037485SYan-Hsuan Chuang } 948e3037485SYan-Hsuan Chuang 949e3037485SYan-Hsuan Chuang void rtw_parse_tbl_phy_cond(struct rtw_dev *rtwdev, const struct rtw_table *tbl) 950e3037485SYan-Hsuan Chuang { 951e3037485SYan-Hsuan Chuang const union phy_table_tile *p = tbl->data; 952e3037485SYan-Hsuan Chuang const union phy_table_tile *end = p + tbl->size / 2; 953e3037485SYan-Hsuan Chuang struct rtw_phy_cond pos_cond = {0}; 954e3037485SYan-Hsuan Chuang bool is_matched = true, is_skipped = false; 955e3037485SYan-Hsuan Chuang 956e3037485SYan-Hsuan Chuang BUILD_BUG_ON(sizeof(union phy_table_tile) != sizeof(struct phy_cfg_pair)); 957e3037485SYan-Hsuan Chuang 958e3037485SYan-Hsuan Chuang for (; p < end; p++) { 959e3037485SYan-Hsuan Chuang if (p->cond.pos) { 960e3037485SYan-Hsuan Chuang switch (p->cond.branch) { 961e3037485SYan-Hsuan Chuang case BRANCH_ENDIF: 962e3037485SYan-Hsuan Chuang is_matched = true; 963e3037485SYan-Hsuan Chuang is_skipped = false; 964e3037485SYan-Hsuan Chuang break; 965e3037485SYan-Hsuan Chuang case BRANCH_ELSE: 966e3037485SYan-Hsuan Chuang is_matched = is_skipped ? false : true; 967e3037485SYan-Hsuan Chuang break; 968e3037485SYan-Hsuan Chuang case BRANCH_IF: 969e3037485SYan-Hsuan Chuang case BRANCH_ELIF: 970e3037485SYan-Hsuan Chuang default: 971e3037485SYan-Hsuan Chuang pos_cond = p->cond; 972e3037485SYan-Hsuan Chuang break; 973e3037485SYan-Hsuan Chuang } 974e3037485SYan-Hsuan Chuang } else if (p->cond.neg) { 975e3037485SYan-Hsuan Chuang if (!is_skipped) { 976e3037485SYan-Hsuan Chuang if (check_positive(rtwdev, pos_cond)) { 977e3037485SYan-Hsuan Chuang is_matched = true; 978e3037485SYan-Hsuan Chuang is_skipped = true; 979e3037485SYan-Hsuan Chuang } else { 980e3037485SYan-Hsuan Chuang is_matched = false; 981e3037485SYan-Hsuan Chuang is_skipped = false; 982e3037485SYan-Hsuan Chuang } 983e3037485SYan-Hsuan Chuang } else { 984e3037485SYan-Hsuan Chuang is_matched = false; 985e3037485SYan-Hsuan Chuang } 986e3037485SYan-Hsuan Chuang } else if (is_matched) { 987e3037485SYan-Hsuan Chuang (*tbl->do_cfg)(rtwdev, tbl, p->cfg.addr, p->cfg.data); 988e3037485SYan-Hsuan Chuang } 989e3037485SYan-Hsuan Chuang } 990e3037485SYan-Hsuan Chuang } 991449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_parse_tbl_phy_cond); 992e3037485SYan-Hsuan Chuang 993e3037485SYan-Hsuan Chuang #define bcd_to_dec_pwr_by_rate(val, i) bcd2bin(val >> (i * 8)) 994e3037485SYan-Hsuan Chuang 995e3037485SYan-Hsuan Chuang static u8 tbl_to_dec_pwr_by_rate(struct rtw_dev *rtwdev, u32 hex, u8 i) 996e3037485SYan-Hsuan Chuang { 997e3037485SYan-Hsuan Chuang if (rtwdev->chip->is_pwr_by_rate_dec) 998e3037485SYan-Hsuan Chuang return bcd_to_dec_pwr_by_rate(hex, i); 999fa6dfe6bSYan-Hsuan Chuang 1000e3037485SYan-Hsuan Chuang return (hex >> (i * 8)) & 0xFF; 1001e3037485SYan-Hsuan Chuang } 1002e3037485SYan-Hsuan Chuang 100343712199SYan-Hsuan Chuang static void 100443712199SYan-Hsuan Chuang rtw_phy_get_rate_values_of_txpwr_by_rate(struct rtw_dev *rtwdev, 100543712199SYan-Hsuan Chuang u32 addr, u32 mask, u32 val, u8 *rate, 1006e3037485SYan-Hsuan Chuang u8 *pwr_by_rate, u8 *rate_num) 1007e3037485SYan-Hsuan Chuang { 1008e3037485SYan-Hsuan Chuang int i; 1009e3037485SYan-Hsuan Chuang 1010e3037485SYan-Hsuan Chuang switch (addr) { 1011e3037485SYan-Hsuan Chuang case 0xE00: 1012e3037485SYan-Hsuan Chuang case 0x830: 1013e3037485SYan-Hsuan Chuang rate[0] = DESC_RATE6M; 1014e3037485SYan-Hsuan Chuang rate[1] = DESC_RATE9M; 1015e3037485SYan-Hsuan Chuang rate[2] = DESC_RATE12M; 1016e3037485SYan-Hsuan Chuang rate[3] = DESC_RATE18M; 1017e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1018e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1019e3037485SYan-Hsuan Chuang *rate_num = 4; 1020e3037485SYan-Hsuan Chuang break; 1021e3037485SYan-Hsuan Chuang case 0xE04: 1022e3037485SYan-Hsuan Chuang case 0x834: 1023e3037485SYan-Hsuan Chuang rate[0] = DESC_RATE24M; 1024e3037485SYan-Hsuan Chuang rate[1] = DESC_RATE36M; 1025e3037485SYan-Hsuan Chuang rate[2] = DESC_RATE48M; 1026e3037485SYan-Hsuan Chuang rate[3] = DESC_RATE54M; 1027e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1028e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1029e3037485SYan-Hsuan Chuang *rate_num = 4; 1030e3037485SYan-Hsuan Chuang break; 1031e3037485SYan-Hsuan Chuang case 0xE08: 1032e3037485SYan-Hsuan Chuang rate[0] = DESC_RATE1M; 1033e3037485SYan-Hsuan Chuang pwr_by_rate[0] = bcd_to_dec_pwr_by_rate(val, 1); 1034e3037485SYan-Hsuan Chuang *rate_num = 1; 1035e3037485SYan-Hsuan Chuang break; 1036e3037485SYan-Hsuan Chuang case 0x86C: 1037e3037485SYan-Hsuan Chuang if (mask == 0xffffff00) { 1038e3037485SYan-Hsuan Chuang rate[0] = DESC_RATE2M; 1039e3037485SYan-Hsuan Chuang rate[1] = DESC_RATE5_5M; 1040e3037485SYan-Hsuan Chuang rate[2] = DESC_RATE11M; 1041e3037485SYan-Hsuan Chuang for (i = 1; i < 4; ++i) 1042e3037485SYan-Hsuan Chuang pwr_by_rate[i - 1] = 1043e3037485SYan-Hsuan Chuang tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1044e3037485SYan-Hsuan Chuang *rate_num = 3; 1045e3037485SYan-Hsuan Chuang } else if (mask == 0x000000ff) { 1046e3037485SYan-Hsuan Chuang rate[0] = DESC_RATE11M; 1047e3037485SYan-Hsuan Chuang pwr_by_rate[0] = bcd_to_dec_pwr_by_rate(val, 0); 1048e3037485SYan-Hsuan Chuang *rate_num = 1; 1049e3037485SYan-Hsuan Chuang } 1050e3037485SYan-Hsuan Chuang break; 1051e3037485SYan-Hsuan Chuang case 0xE10: 1052e3037485SYan-Hsuan Chuang case 0x83C: 1053e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS0; 1054e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS1; 1055e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS2; 1056e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS3; 1057e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1058e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1059e3037485SYan-Hsuan Chuang *rate_num = 4; 1060e3037485SYan-Hsuan Chuang break; 1061e3037485SYan-Hsuan Chuang case 0xE14: 1062e3037485SYan-Hsuan Chuang case 0x848: 1063e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS4; 1064e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS5; 1065e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS6; 1066e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS7; 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 0xE18: 1072e3037485SYan-Hsuan Chuang case 0x84C: 1073e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS8; 1074e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS9; 1075e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS10; 1076e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS11; 1077e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1078e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1079e3037485SYan-Hsuan Chuang *rate_num = 4; 1080e3037485SYan-Hsuan Chuang break; 1081e3037485SYan-Hsuan Chuang case 0xE1C: 1082e3037485SYan-Hsuan Chuang case 0x868: 1083e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS12; 1084e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS13; 1085e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS14; 1086e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS15; 1087e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1088e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1089e3037485SYan-Hsuan Chuang *rate_num = 4; 1090e3037485SYan-Hsuan Chuang break; 1091e3037485SYan-Hsuan Chuang case 0x838: 1092e3037485SYan-Hsuan Chuang rate[0] = DESC_RATE1M; 1093e3037485SYan-Hsuan Chuang rate[1] = DESC_RATE2M; 1094e3037485SYan-Hsuan Chuang rate[2] = DESC_RATE5_5M; 1095e3037485SYan-Hsuan Chuang for (i = 1; i < 4; ++i) 1096e3037485SYan-Hsuan Chuang pwr_by_rate[i - 1] = tbl_to_dec_pwr_by_rate(rtwdev, 1097e3037485SYan-Hsuan Chuang val, i); 1098e3037485SYan-Hsuan Chuang *rate_num = 3; 1099e3037485SYan-Hsuan Chuang break; 1100e3037485SYan-Hsuan Chuang case 0xC20: 1101e3037485SYan-Hsuan Chuang case 0xE20: 1102e3037485SYan-Hsuan Chuang case 0x1820: 1103e3037485SYan-Hsuan Chuang case 0x1A20: 1104e3037485SYan-Hsuan Chuang rate[0] = DESC_RATE1M; 1105e3037485SYan-Hsuan Chuang rate[1] = DESC_RATE2M; 1106e3037485SYan-Hsuan Chuang rate[2] = DESC_RATE5_5M; 1107e3037485SYan-Hsuan Chuang rate[3] = DESC_RATE11M; 1108e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1109e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1110e3037485SYan-Hsuan Chuang *rate_num = 4; 1111e3037485SYan-Hsuan Chuang break; 1112e3037485SYan-Hsuan Chuang case 0xC24: 1113e3037485SYan-Hsuan Chuang case 0xE24: 1114e3037485SYan-Hsuan Chuang case 0x1824: 1115e3037485SYan-Hsuan Chuang case 0x1A24: 1116e3037485SYan-Hsuan Chuang rate[0] = DESC_RATE6M; 1117e3037485SYan-Hsuan Chuang rate[1] = DESC_RATE9M; 1118e3037485SYan-Hsuan Chuang rate[2] = DESC_RATE12M; 1119e3037485SYan-Hsuan Chuang rate[3] = DESC_RATE18M; 1120e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1121e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1122e3037485SYan-Hsuan Chuang *rate_num = 4; 1123e3037485SYan-Hsuan Chuang break; 1124e3037485SYan-Hsuan Chuang case 0xC28: 1125e3037485SYan-Hsuan Chuang case 0xE28: 1126e3037485SYan-Hsuan Chuang case 0x1828: 1127e3037485SYan-Hsuan Chuang case 0x1A28: 1128e3037485SYan-Hsuan Chuang rate[0] = DESC_RATE24M; 1129e3037485SYan-Hsuan Chuang rate[1] = DESC_RATE36M; 1130e3037485SYan-Hsuan Chuang rate[2] = DESC_RATE48M; 1131e3037485SYan-Hsuan Chuang rate[3] = DESC_RATE54M; 1132e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1133e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1134e3037485SYan-Hsuan Chuang *rate_num = 4; 1135e3037485SYan-Hsuan Chuang break; 1136e3037485SYan-Hsuan Chuang case 0xC2C: 1137e3037485SYan-Hsuan Chuang case 0xE2C: 1138e3037485SYan-Hsuan Chuang case 0x182C: 1139e3037485SYan-Hsuan Chuang case 0x1A2C: 1140e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS0; 1141e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS1; 1142e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS2; 1143e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS3; 1144e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1145e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1146e3037485SYan-Hsuan Chuang *rate_num = 4; 1147e3037485SYan-Hsuan Chuang break; 1148e3037485SYan-Hsuan Chuang case 0xC30: 1149e3037485SYan-Hsuan Chuang case 0xE30: 1150e3037485SYan-Hsuan Chuang case 0x1830: 1151e3037485SYan-Hsuan Chuang case 0x1A30: 1152e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS4; 1153e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS5; 1154e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS6; 1155e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS7; 1156e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1157e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1158e3037485SYan-Hsuan Chuang *rate_num = 4; 1159e3037485SYan-Hsuan Chuang break; 1160e3037485SYan-Hsuan Chuang case 0xC34: 1161e3037485SYan-Hsuan Chuang case 0xE34: 1162e3037485SYan-Hsuan Chuang case 0x1834: 1163e3037485SYan-Hsuan Chuang case 0x1A34: 1164e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS8; 1165e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS9; 1166e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS10; 1167e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS11; 1168e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1169e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1170e3037485SYan-Hsuan Chuang *rate_num = 4; 1171e3037485SYan-Hsuan Chuang break; 1172e3037485SYan-Hsuan Chuang case 0xC38: 1173e3037485SYan-Hsuan Chuang case 0xE38: 1174e3037485SYan-Hsuan Chuang case 0x1838: 1175e3037485SYan-Hsuan Chuang case 0x1A38: 1176e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS12; 1177e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS13; 1178e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS14; 1179e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS15; 1180e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1181e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1182e3037485SYan-Hsuan Chuang *rate_num = 4; 1183e3037485SYan-Hsuan Chuang break; 1184e3037485SYan-Hsuan Chuang case 0xC3C: 1185e3037485SYan-Hsuan Chuang case 0xE3C: 1186e3037485SYan-Hsuan Chuang case 0x183C: 1187e3037485SYan-Hsuan Chuang case 0x1A3C: 1188e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEVHT1SS_MCS0; 1189e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEVHT1SS_MCS1; 1190e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEVHT1SS_MCS2; 1191e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEVHT1SS_MCS3; 1192e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1193e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1194e3037485SYan-Hsuan Chuang *rate_num = 4; 1195e3037485SYan-Hsuan Chuang break; 1196e3037485SYan-Hsuan Chuang case 0xC40: 1197e3037485SYan-Hsuan Chuang case 0xE40: 1198e3037485SYan-Hsuan Chuang case 0x1840: 1199e3037485SYan-Hsuan Chuang case 0x1A40: 1200e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEVHT1SS_MCS4; 1201e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEVHT1SS_MCS5; 1202e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEVHT1SS_MCS6; 1203e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEVHT1SS_MCS7; 1204e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1205e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1206e3037485SYan-Hsuan Chuang *rate_num = 4; 1207e3037485SYan-Hsuan Chuang break; 1208e3037485SYan-Hsuan Chuang case 0xC44: 1209e3037485SYan-Hsuan Chuang case 0xE44: 1210e3037485SYan-Hsuan Chuang case 0x1844: 1211e3037485SYan-Hsuan Chuang case 0x1A44: 1212e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEVHT1SS_MCS8; 1213e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEVHT1SS_MCS9; 1214e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEVHT2SS_MCS0; 1215e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEVHT2SS_MCS1; 1216e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1217e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1218e3037485SYan-Hsuan Chuang *rate_num = 4; 1219e3037485SYan-Hsuan Chuang break; 1220e3037485SYan-Hsuan Chuang case 0xC48: 1221e3037485SYan-Hsuan Chuang case 0xE48: 1222e3037485SYan-Hsuan Chuang case 0x1848: 1223e3037485SYan-Hsuan Chuang case 0x1A48: 1224e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEVHT2SS_MCS2; 1225e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEVHT2SS_MCS3; 1226e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEVHT2SS_MCS4; 1227e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEVHT2SS_MCS5; 1228e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1229e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1230e3037485SYan-Hsuan Chuang *rate_num = 4; 1231e3037485SYan-Hsuan Chuang break; 1232e3037485SYan-Hsuan Chuang case 0xC4C: 1233e3037485SYan-Hsuan Chuang case 0xE4C: 1234e3037485SYan-Hsuan Chuang case 0x184C: 1235e3037485SYan-Hsuan Chuang case 0x1A4C: 1236e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEVHT2SS_MCS6; 1237e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEVHT2SS_MCS7; 1238e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEVHT2SS_MCS8; 1239e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEVHT2SS_MCS9; 1240e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1241e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1242e3037485SYan-Hsuan Chuang *rate_num = 4; 1243e3037485SYan-Hsuan Chuang break; 1244e3037485SYan-Hsuan Chuang case 0xCD8: 1245e3037485SYan-Hsuan Chuang case 0xED8: 1246e3037485SYan-Hsuan Chuang case 0x18D8: 1247e3037485SYan-Hsuan Chuang case 0x1AD8: 1248e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS16; 1249e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS17; 1250e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS18; 1251e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS19; 1252e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1253e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1254e3037485SYan-Hsuan Chuang *rate_num = 4; 1255e3037485SYan-Hsuan Chuang break; 1256e3037485SYan-Hsuan Chuang case 0xCDC: 1257e3037485SYan-Hsuan Chuang case 0xEDC: 1258e3037485SYan-Hsuan Chuang case 0x18DC: 1259e3037485SYan-Hsuan Chuang case 0x1ADC: 1260e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS20; 1261e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS21; 1262e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS22; 1263e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS23; 1264e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1265e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1266e3037485SYan-Hsuan Chuang *rate_num = 4; 1267e3037485SYan-Hsuan Chuang break; 1268e3037485SYan-Hsuan Chuang case 0xCE0: 1269e3037485SYan-Hsuan Chuang case 0xEE0: 1270e3037485SYan-Hsuan Chuang case 0x18E0: 1271e3037485SYan-Hsuan Chuang case 0x1AE0: 1272e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEVHT3SS_MCS0; 1273e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEVHT3SS_MCS1; 1274e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEVHT3SS_MCS2; 1275e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEVHT3SS_MCS3; 1276e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1277e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1278e3037485SYan-Hsuan Chuang *rate_num = 4; 1279e3037485SYan-Hsuan Chuang break; 1280e3037485SYan-Hsuan Chuang case 0xCE4: 1281e3037485SYan-Hsuan Chuang case 0xEE4: 1282e3037485SYan-Hsuan Chuang case 0x18E4: 1283e3037485SYan-Hsuan Chuang case 0x1AE4: 1284e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEVHT3SS_MCS4; 1285e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEVHT3SS_MCS5; 1286e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEVHT3SS_MCS6; 1287e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEVHT3SS_MCS7; 1288e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1289e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1290e3037485SYan-Hsuan Chuang *rate_num = 4; 1291e3037485SYan-Hsuan Chuang break; 1292e3037485SYan-Hsuan Chuang case 0xCE8: 1293e3037485SYan-Hsuan Chuang case 0xEE8: 1294e3037485SYan-Hsuan Chuang case 0x18E8: 1295e3037485SYan-Hsuan Chuang case 0x1AE8: 1296e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEVHT3SS_MCS8; 1297e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEVHT3SS_MCS9; 1298e3037485SYan-Hsuan Chuang for (i = 0; i < 2; ++i) 1299e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1300e3037485SYan-Hsuan Chuang *rate_num = 2; 1301e3037485SYan-Hsuan Chuang break; 1302e3037485SYan-Hsuan Chuang default: 1303e3037485SYan-Hsuan Chuang rtw_warn(rtwdev, "invalid tx power index addr 0x%08x\n", addr); 1304e3037485SYan-Hsuan Chuang break; 1305e3037485SYan-Hsuan Chuang } 1306e3037485SYan-Hsuan Chuang } 1307e3037485SYan-Hsuan Chuang 130843712199SYan-Hsuan Chuang static void rtw_phy_store_tx_power_by_rate(struct rtw_dev *rtwdev, 1309fa6dfe6bSYan-Hsuan Chuang u32 band, u32 rfpath, u32 txnum, 1310e3037485SYan-Hsuan Chuang u32 regaddr, u32 bitmask, u32 data) 1311e3037485SYan-Hsuan Chuang { 1312e3037485SYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 1313e3037485SYan-Hsuan Chuang u8 rate_num = 0; 1314e3037485SYan-Hsuan Chuang u8 rate; 1315e3037485SYan-Hsuan Chuang u8 rates[RTW_RF_PATH_MAX] = {0}; 1316e3037485SYan-Hsuan Chuang s8 offset; 1317e3037485SYan-Hsuan Chuang s8 pwr_by_rate[RTW_RF_PATH_MAX] = {0}; 1318e3037485SYan-Hsuan Chuang int i; 1319e3037485SYan-Hsuan Chuang 132043712199SYan-Hsuan Chuang rtw_phy_get_rate_values_of_txpwr_by_rate(rtwdev, regaddr, bitmask, data, 1321e3037485SYan-Hsuan Chuang rates, pwr_by_rate, &rate_num); 1322e3037485SYan-Hsuan Chuang 1323e3037485SYan-Hsuan Chuang if (WARN_ON(rfpath >= RTW_RF_PATH_MAX || 1324e3037485SYan-Hsuan Chuang (band != PHY_BAND_2G && band != PHY_BAND_5G) || 1325e3037485SYan-Hsuan Chuang rate_num > RTW_RF_PATH_MAX)) 1326e3037485SYan-Hsuan Chuang return; 1327e3037485SYan-Hsuan Chuang 1328e3037485SYan-Hsuan Chuang for (i = 0; i < rate_num; i++) { 1329e3037485SYan-Hsuan Chuang offset = pwr_by_rate[i]; 1330e3037485SYan-Hsuan Chuang rate = rates[i]; 1331e3037485SYan-Hsuan Chuang if (band == PHY_BAND_2G) 1332e3037485SYan-Hsuan Chuang hal->tx_pwr_by_rate_offset_2g[rfpath][rate] = offset; 1333e3037485SYan-Hsuan Chuang else if (band == PHY_BAND_5G) 1334e3037485SYan-Hsuan Chuang hal->tx_pwr_by_rate_offset_5g[rfpath][rate] = offset; 1335e3037485SYan-Hsuan Chuang else 1336e3037485SYan-Hsuan Chuang continue; 1337e3037485SYan-Hsuan Chuang } 1338e3037485SYan-Hsuan Chuang } 1339e3037485SYan-Hsuan Chuang 1340fa6dfe6bSYan-Hsuan Chuang void rtw_parse_tbl_bb_pg(struct rtw_dev *rtwdev, const struct rtw_table *tbl) 1341fa6dfe6bSYan-Hsuan Chuang { 13420b8db87dSYan-Hsuan Chuang const struct rtw_phy_pg_cfg_pair *p = tbl->data; 13430b8db87dSYan-Hsuan Chuang const struct rtw_phy_pg_cfg_pair *end = p + tbl->size; 1344fa6dfe6bSYan-Hsuan Chuang 1345fa6dfe6bSYan-Hsuan Chuang for (; p < end; p++) { 1346fa6dfe6bSYan-Hsuan Chuang if (p->addr == 0xfe || p->addr == 0xffe) { 1347fa6dfe6bSYan-Hsuan Chuang msleep(50); 1348fa6dfe6bSYan-Hsuan Chuang continue; 1349fa6dfe6bSYan-Hsuan Chuang } 135043712199SYan-Hsuan Chuang rtw_phy_store_tx_power_by_rate(rtwdev, p->band, p->rf_path, 1351fa6dfe6bSYan-Hsuan Chuang p->tx_num, p->addr, p->bitmask, 1352fa6dfe6bSYan-Hsuan Chuang p->data); 1353fa6dfe6bSYan-Hsuan Chuang } 1354fa6dfe6bSYan-Hsuan Chuang } 1355449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_parse_tbl_bb_pg); 1356fa6dfe6bSYan-Hsuan Chuang 1357fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_channel_idx_5g[RTW_MAX_CHANNEL_NUM_5G] = { 1358fa6dfe6bSYan-Hsuan Chuang 36, 38, 40, 42, 44, 46, 48, /* Band 1 */ 1359fa6dfe6bSYan-Hsuan Chuang 52, 54, 56, 58, 60, 62, 64, /* Band 2 */ 1360fa6dfe6bSYan-Hsuan Chuang 100, 102, 104, 106, 108, 110, 112, /* Band 3 */ 1361fa6dfe6bSYan-Hsuan Chuang 116, 118, 120, 122, 124, 126, 128, /* Band 3 */ 1362fa6dfe6bSYan-Hsuan Chuang 132, 134, 136, 138, 140, 142, 144, /* Band 3 */ 1363fa6dfe6bSYan-Hsuan Chuang 149, 151, 153, 155, 157, 159, 161, /* Band 4 */ 1364fa6dfe6bSYan-Hsuan Chuang 165, 167, 169, 171, 173, 175, 177}; /* Band 4 */ 1365fa6dfe6bSYan-Hsuan Chuang 1366fa6dfe6bSYan-Hsuan Chuang static int rtw_channel_to_idx(u8 band, u8 channel) 1367fa6dfe6bSYan-Hsuan Chuang { 1368fa6dfe6bSYan-Hsuan Chuang int ch_idx; 1369fa6dfe6bSYan-Hsuan Chuang u8 n_channel; 1370fa6dfe6bSYan-Hsuan Chuang 1371fa6dfe6bSYan-Hsuan Chuang if (band == PHY_BAND_2G) { 1372fa6dfe6bSYan-Hsuan Chuang ch_idx = channel - 1; 1373fa6dfe6bSYan-Hsuan Chuang n_channel = RTW_MAX_CHANNEL_NUM_2G; 1374fa6dfe6bSYan-Hsuan Chuang } else if (band == PHY_BAND_5G) { 1375fa6dfe6bSYan-Hsuan Chuang n_channel = RTW_MAX_CHANNEL_NUM_5G; 1376fa6dfe6bSYan-Hsuan Chuang for (ch_idx = 0; ch_idx < n_channel; ch_idx++) 1377fa6dfe6bSYan-Hsuan Chuang if (rtw_channel_idx_5g[ch_idx] == channel) 1378fa6dfe6bSYan-Hsuan Chuang break; 1379fa6dfe6bSYan-Hsuan Chuang } else { 1380fa6dfe6bSYan-Hsuan Chuang return -1; 1381fa6dfe6bSYan-Hsuan Chuang } 1382fa6dfe6bSYan-Hsuan Chuang 1383fa6dfe6bSYan-Hsuan Chuang if (ch_idx >= n_channel) 1384fa6dfe6bSYan-Hsuan Chuang return -1; 1385fa6dfe6bSYan-Hsuan Chuang 1386fa6dfe6bSYan-Hsuan Chuang return ch_idx; 1387fa6dfe6bSYan-Hsuan Chuang } 1388fa6dfe6bSYan-Hsuan Chuang 138943712199SYan-Hsuan Chuang static void rtw_phy_set_tx_power_limit(struct rtw_dev *rtwdev, u8 regd, u8 band, 1390fa6dfe6bSYan-Hsuan Chuang u8 bw, u8 rs, u8 ch, s8 pwr_limit) 1391fa6dfe6bSYan-Hsuan Chuang { 1392fa6dfe6bSYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 13930d350f0aSTzu-En Huang u8 max_power_index = rtwdev->chip->max_power_index; 1394adf3c676SYan-Hsuan Chuang s8 ww; 1395fa6dfe6bSYan-Hsuan Chuang int ch_idx; 1396fa6dfe6bSYan-Hsuan Chuang 1397fa6dfe6bSYan-Hsuan Chuang pwr_limit = clamp_t(s8, pwr_limit, 13980d350f0aSTzu-En Huang -max_power_index, max_power_index); 1399fa6dfe6bSYan-Hsuan Chuang ch_idx = rtw_channel_to_idx(band, ch); 1400fa6dfe6bSYan-Hsuan Chuang 1401fa6dfe6bSYan-Hsuan Chuang if (regd >= RTW_REGD_MAX || bw >= RTW_CHANNEL_WIDTH_MAX || 1402fa6dfe6bSYan-Hsuan Chuang rs >= RTW_RATE_SECTION_MAX || ch_idx < 0) { 1403fa6dfe6bSYan-Hsuan Chuang WARN(1, 1404fa6dfe6bSYan-Hsuan Chuang "wrong txpwr_lmt regd=%u, band=%u bw=%u, rs=%u, ch_idx=%u, pwr_limit=%d\n", 1405fa6dfe6bSYan-Hsuan Chuang regd, band, bw, rs, ch_idx, pwr_limit); 1406fa6dfe6bSYan-Hsuan Chuang return; 1407fa6dfe6bSYan-Hsuan Chuang } 1408fa6dfe6bSYan-Hsuan Chuang 1409adf3c676SYan-Hsuan Chuang if (band == PHY_BAND_2G) { 1410fa6dfe6bSYan-Hsuan Chuang hal->tx_pwr_limit_2g[regd][bw][rs][ch_idx] = pwr_limit; 1411adf3c676SYan-Hsuan Chuang ww = hal->tx_pwr_limit_2g[RTW_REGD_WW][bw][rs][ch_idx]; 1412adf3c676SYan-Hsuan Chuang ww = min_t(s8, ww, pwr_limit); 1413adf3c676SYan-Hsuan Chuang hal->tx_pwr_limit_2g[RTW_REGD_WW][bw][rs][ch_idx] = ww; 1414adf3c676SYan-Hsuan Chuang } else if (band == PHY_BAND_5G) { 1415fa6dfe6bSYan-Hsuan Chuang hal->tx_pwr_limit_5g[regd][bw][rs][ch_idx] = pwr_limit; 1416adf3c676SYan-Hsuan Chuang ww = hal->tx_pwr_limit_5g[RTW_REGD_WW][bw][rs][ch_idx]; 1417adf3c676SYan-Hsuan Chuang ww = min_t(s8, ww, pwr_limit); 1418adf3c676SYan-Hsuan Chuang hal->tx_pwr_limit_5g[RTW_REGD_WW][bw][rs][ch_idx] = ww; 1419adf3c676SYan-Hsuan Chuang } 1420fa6dfe6bSYan-Hsuan Chuang } 1421fa6dfe6bSYan-Hsuan Chuang 142293f68a86SZong-Zhe Yang /* cross-reference 5G power limits if values are not assigned */ 142393f68a86SZong-Zhe Yang static void 142493f68a86SZong-Zhe Yang rtw_xref_5g_txpwr_lmt(struct rtw_dev *rtwdev, u8 regd, 142593f68a86SZong-Zhe Yang u8 bw, u8 ch_idx, u8 rs_ht, u8 rs_vht) 142693f68a86SZong-Zhe Yang { 142793f68a86SZong-Zhe Yang struct rtw_hal *hal = &rtwdev->hal; 14280d350f0aSTzu-En Huang u8 max_power_index = rtwdev->chip->max_power_index; 142993f68a86SZong-Zhe Yang s8 lmt_ht = hal->tx_pwr_limit_5g[regd][bw][rs_ht][ch_idx]; 143093f68a86SZong-Zhe Yang s8 lmt_vht = hal->tx_pwr_limit_5g[regd][bw][rs_vht][ch_idx]; 143193f68a86SZong-Zhe Yang 143293f68a86SZong-Zhe Yang if (lmt_ht == lmt_vht) 143393f68a86SZong-Zhe Yang return; 143493f68a86SZong-Zhe Yang 14350d350f0aSTzu-En Huang if (lmt_ht == max_power_index) 143693f68a86SZong-Zhe Yang hal->tx_pwr_limit_5g[regd][bw][rs_ht][ch_idx] = lmt_vht; 143793f68a86SZong-Zhe Yang 14380d350f0aSTzu-En Huang else if (lmt_vht == max_power_index) 143993f68a86SZong-Zhe Yang hal->tx_pwr_limit_5g[regd][bw][rs_vht][ch_idx] = lmt_ht; 144093f68a86SZong-Zhe Yang } 144193f68a86SZong-Zhe Yang 144293f68a86SZong-Zhe Yang /* cross-reference power limits for ht and vht */ 144393f68a86SZong-Zhe Yang static void 144493f68a86SZong-Zhe Yang rtw_xref_txpwr_lmt_by_rs(struct rtw_dev *rtwdev, u8 regd, u8 bw, u8 ch_idx) 144593f68a86SZong-Zhe Yang { 144693f68a86SZong-Zhe Yang u8 rs_idx, rs_ht, rs_vht; 144793f68a86SZong-Zhe Yang u8 rs_cmp[2][2] = {{RTW_RATE_SECTION_HT_1S, RTW_RATE_SECTION_VHT_1S}, 144893f68a86SZong-Zhe Yang {RTW_RATE_SECTION_HT_2S, RTW_RATE_SECTION_VHT_2S} }; 144993f68a86SZong-Zhe Yang 145093f68a86SZong-Zhe Yang for (rs_idx = 0; rs_idx < 2; rs_idx++) { 145193f68a86SZong-Zhe Yang rs_ht = rs_cmp[rs_idx][0]; 145293f68a86SZong-Zhe Yang rs_vht = rs_cmp[rs_idx][1]; 145393f68a86SZong-Zhe Yang 145493f68a86SZong-Zhe Yang rtw_xref_5g_txpwr_lmt(rtwdev, regd, bw, ch_idx, rs_ht, rs_vht); 145593f68a86SZong-Zhe Yang } 145693f68a86SZong-Zhe Yang } 145793f68a86SZong-Zhe Yang 145893f68a86SZong-Zhe Yang /* cross-reference power limits for 5G channels */ 145993f68a86SZong-Zhe Yang static void 146093f68a86SZong-Zhe Yang rtw_xref_5g_txpwr_lmt_by_ch(struct rtw_dev *rtwdev, u8 regd, u8 bw) 146193f68a86SZong-Zhe Yang { 146293f68a86SZong-Zhe Yang u8 ch_idx; 146393f68a86SZong-Zhe Yang 146493f68a86SZong-Zhe Yang for (ch_idx = 0; ch_idx < RTW_MAX_CHANNEL_NUM_5G; ch_idx++) 146593f68a86SZong-Zhe Yang rtw_xref_txpwr_lmt_by_rs(rtwdev, regd, bw, ch_idx); 146693f68a86SZong-Zhe Yang } 146793f68a86SZong-Zhe Yang 146893f68a86SZong-Zhe Yang /* cross-reference power limits for 20/40M bandwidth */ 146993f68a86SZong-Zhe Yang static void 147093f68a86SZong-Zhe Yang rtw_xref_txpwr_lmt_by_bw(struct rtw_dev *rtwdev, u8 regd) 147193f68a86SZong-Zhe Yang { 147293f68a86SZong-Zhe Yang u8 bw; 147393f68a86SZong-Zhe Yang 147493f68a86SZong-Zhe Yang for (bw = RTW_CHANNEL_WIDTH_20; bw <= RTW_CHANNEL_WIDTH_40; bw++) 147593f68a86SZong-Zhe Yang rtw_xref_5g_txpwr_lmt_by_ch(rtwdev, regd, bw); 147693f68a86SZong-Zhe Yang } 147793f68a86SZong-Zhe Yang 147893f68a86SZong-Zhe Yang /* cross-reference power limits */ 147993f68a86SZong-Zhe Yang static void rtw_xref_txpwr_lmt(struct rtw_dev *rtwdev) 148093f68a86SZong-Zhe Yang { 148193f68a86SZong-Zhe Yang u8 regd; 148293f68a86SZong-Zhe Yang 148393f68a86SZong-Zhe Yang for (regd = 0; regd < RTW_REGD_MAX; regd++) 148493f68a86SZong-Zhe Yang rtw_xref_txpwr_lmt_by_bw(rtwdev, regd); 148593f68a86SZong-Zhe Yang } 148693f68a86SZong-Zhe Yang 1487fa6dfe6bSYan-Hsuan Chuang void rtw_parse_tbl_txpwr_lmt(struct rtw_dev *rtwdev, 1488fa6dfe6bSYan-Hsuan Chuang const struct rtw_table *tbl) 1489fa6dfe6bSYan-Hsuan Chuang { 14903457f86dSBrian Norris const struct rtw_txpwr_lmt_cfg_pair *p = tbl->data; 14913457f86dSBrian Norris const struct rtw_txpwr_lmt_cfg_pair *end = p + tbl->size; 1492fa6dfe6bSYan-Hsuan Chuang 1493fa6dfe6bSYan-Hsuan Chuang for (; p < end; p++) { 149443712199SYan-Hsuan Chuang rtw_phy_set_tx_power_limit(rtwdev, p->regd, p->band, 149543712199SYan-Hsuan Chuang p->bw, p->rs, p->ch, p->txpwr_lmt); 1496fa6dfe6bSYan-Hsuan Chuang } 149793f68a86SZong-Zhe Yang 149893f68a86SZong-Zhe Yang rtw_xref_txpwr_lmt(rtwdev); 1499fa6dfe6bSYan-Hsuan Chuang } 1500449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_parse_tbl_txpwr_lmt); 1501fa6dfe6bSYan-Hsuan Chuang 1502fa6dfe6bSYan-Hsuan Chuang void rtw_phy_cfg_mac(struct rtw_dev *rtwdev, const struct rtw_table *tbl, 1503fa6dfe6bSYan-Hsuan Chuang u32 addr, u32 data) 1504fa6dfe6bSYan-Hsuan Chuang { 1505fa6dfe6bSYan-Hsuan Chuang rtw_write8(rtwdev, addr, data); 1506fa6dfe6bSYan-Hsuan Chuang } 1507449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_cfg_mac); 1508fa6dfe6bSYan-Hsuan Chuang 1509fa6dfe6bSYan-Hsuan Chuang void rtw_phy_cfg_agc(struct rtw_dev *rtwdev, const struct rtw_table *tbl, 1510fa6dfe6bSYan-Hsuan Chuang u32 addr, u32 data) 1511fa6dfe6bSYan-Hsuan Chuang { 1512fa6dfe6bSYan-Hsuan Chuang rtw_write32(rtwdev, addr, data); 1513fa6dfe6bSYan-Hsuan Chuang } 1514449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_cfg_agc); 1515fa6dfe6bSYan-Hsuan Chuang 1516fa6dfe6bSYan-Hsuan Chuang void rtw_phy_cfg_bb(struct rtw_dev *rtwdev, const struct rtw_table *tbl, 1517fa6dfe6bSYan-Hsuan Chuang u32 addr, u32 data) 1518fa6dfe6bSYan-Hsuan Chuang { 1519fa6dfe6bSYan-Hsuan Chuang if (addr == 0xfe) 1520fa6dfe6bSYan-Hsuan Chuang msleep(50); 1521fa6dfe6bSYan-Hsuan Chuang else if (addr == 0xfd) 1522fa6dfe6bSYan-Hsuan Chuang mdelay(5); 1523fa6dfe6bSYan-Hsuan Chuang else if (addr == 0xfc) 1524fa6dfe6bSYan-Hsuan Chuang mdelay(1); 1525fa6dfe6bSYan-Hsuan Chuang else if (addr == 0xfb) 1526fa6dfe6bSYan-Hsuan Chuang usleep_range(50, 60); 1527fa6dfe6bSYan-Hsuan Chuang else if (addr == 0xfa) 1528fa6dfe6bSYan-Hsuan Chuang udelay(5); 1529fa6dfe6bSYan-Hsuan Chuang else if (addr == 0xf9) 1530fa6dfe6bSYan-Hsuan Chuang udelay(1); 1531fa6dfe6bSYan-Hsuan Chuang else 1532fa6dfe6bSYan-Hsuan Chuang rtw_write32(rtwdev, addr, data); 1533fa6dfe6bSYan-Hsuan Chuang } 1534449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_cfg_bb); 1535fa6dfe6bSYan-Hsuan Chuang 1536fa6dfe6bSYan-Hsuan Chuang void rtw_phy_cfg_rf(struct rtw_dev *rtwdev, const struct rtw_table *tbl, 1537fa6dfe6bSYan-Hsuan Chuang u32 addr, u32 data) 1538fa6dfe6bSYan-Hsuan Chuang { 1539fa6dfe6bSYan-Hsuan Chuang if (addr == 0xffe) { 1540fa6dfe6bSYan-Hsuan Chuang msleep(50); 1541fa6dfe6bSYan-Hsuan Chuang } else if (addr == 0xfe) { 1542fa6dfe6bSYan-Hsuan Chuang usleep_range(100, 110); 1543fa6dfe6bSYan-Hsuan Chuang } else { 1544fa6dfe6bSYan-Hsuan Chuang rtw_write_rf(rtwdev, tbl->rf_path, addr, RFREG_MASK, data); 1545fa6dfe6bSYan-Hsuan Chuang udelay(1); 1546fa6dfe6bSYan-Hsuan Chuang } 1547fa6dfe6bSYan-Hsuan Chuang } 1548449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_cfg_rf); 1549fa6dfe6bSYan-Hsuan Chuang 1550fa6dfe6bSYan-Hsuan Chuang static void rtw_load_rfk_table(struct rtw_dev *rtwdev) 1551fa6dfe6bSYan-Hsuan Chuang { 1552fa6dfe6bSYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 15535227c2eeSTzu-En Huang struct rtw_dpk_info *dpk_info = &rtwdev->dm_info.dpk_info; 1554fa6dfe6bSYan-Hsuan Chuang 1555fa6dfe6bSYan-Hsuan Chuang if (!chip->rfk_init_tbl) 1556fa6dfe6bSYan-Hsuan Chuang return; 1557fa6dfe6bSYan-Hsuan Chuang 15585227c2eeSTzu-En Huang rtw_write32_mask(rtwdev, 0x1e24, BIT(17), 0x1); 15595227c2eeSTzu-En Huang rtw_write32_mask(rtwdev, 0x1cd0, BIT(28), 0x1); 15605227c2eeSTzu-En Huang rtw_write32_mask(rtwdev, 0x1cd0, BIT(29), 0x1); 15615227c2eeSTzu-En Huang rtw_write32_mask(rtwdev, 0x1cd0, BIT(30), 0x1); 15625227c2eeSTzu-En Huang rtw_write32_mask(rtwdev, 0x1cd0, BIT(31), 0x0); 15635227c2eeSTzu-En Huang 1564fa6dfe6bSYan-Hsuan Chuang rtw_load_table(rtwdev, chip->rfk_init_tbl); 15655227c2eeSTzu-En Huang 1566891984bcSzhengbin dpk_info->is_dpk_pwr_on = true; 1567fa6dfe6bSYan-Hsuan Chuang } 1568fa6dfe6bSYan-Hsuan Chuang 1569fa6dfe6bSYan-Hsuan Chuang void rtw_phy_load_tables(struct rtw_dev *rtwdev) 1570fa6dfe6bSYan-Hsuan Chuang { 1571fa6dfe6bSYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 1572fa6dfe6bSYan-Hsuan Chuang u8 rf_path; 1573fa6dfe6bSYan-Hsuan Chuang 1574fa6dfe6bSYan-Hsuan Chuang rtw_load_table(rtwdev, chip->mac_tbl); 1575fa6dfe6bSYan-Hsuan Chuang rtw_load_table(rtwdev, chip->bb_tbl); 1576fa6dfe6bSYan-Hsuan Chuang rtw_load_table(rtwdev, chip->agc_tbl); 1577fa6dfe6bSYan-Hsuan Chuang rtw_load_rfk_table(rtwdev); 1578fa6dfe6bSYan-Hsuan Chuang 1579fa6dfe6bSYan-Hsuan Chuang for (rf_path = 0; rf_path < rtwdev->hal.rf_path_num; rf_path++) { 1580fa6dfe6bSYan-Hsuan Chuang const struct rtw_table *tbl; 1581fa6dfe6bSYan-Hsuan Chuang 1582fa6dfe6bSYan-Hsuan Chuang tbl = chip->rf_tbl[rf_path]; 1583fa6dfe6bSYan-Hsuan Chuang rtw_load_table(rtwdev, tbl); 1584fa6dfe6bSYan-Hsuan Chuang } 1585fa6dfe6bSYan-Hsuan Chuang } 1586449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_load_tables); 1587fa6dfe6bSYan-Hsuan Chuang 1588fa6dfe6bSYan-Hsuan Chuang static u8 rtw_get_channel_group(u8 channel) 1589fa6dfe6bSYan-Hsuan Chuang { 1590fa6dfe6bSYan-Hsuan Chuang switch (channel) { 1591fa6dfe6bSYan-Hsuan Chuang default: 1592fa6dfe6bSYan-Hsuan Chuang WARN_ON(1); 15935466aff8SGustavo A. R. Silva fallthrough; 1594fa6dfe6bSYan-Hsuan Chuang case 1: 1595fa6dfe6bSYan-Hsuan Chuang case 2: 1596fa6dfe6bSYan-Hsuan Chuang case 36: 1597fa6dfe6bSYan-Hsuan Chuang case 38: 1598fa6dfe6bSYan-Hsuan Chuang case 40: 1599fa6dfe6bSYan-Hsuan Chuang case 42: 1600fa6dfe6bSYan-Hsuan Chuang return 0; 1601fa6dfe6bSYan-Hsuan Chuang case 3: 1602fa6dfe6bSYan-Hsuan Chuang case 4: 1603fa6dfe6bSYan-Hsuan Chuang case 5: 1604fa6dfe6bSYan-Hsuan Chuang case 44: 1605fa6dfe6bSYan-Hsuan Chuang case 46: 1606fa6dfe6bSYan-Hsuan Chuang case 48: 1607fa6dfe6bSYan-Hsuan Chuang case 50: 1608fa6dfe6bSYan-Hsuan Chuang return 1; 1609fa6dfe6bSYan-Hsuan Chuang case 6: 1610fa6dfe6bSYan-Hsuan Chuang case 7: 1611fa6dfe6bSYan-Hsuan Chuang case 8: 1612fa6dfe6bSYan-Hsuan Chuang case 52: 1613fa6dfe6bSYan-Hsuan Chuang case 54: 1614fa6dfe6bSYan-Hsuan Chuang case 56: 1615fa6dfe6bSYan-Hsuan Chuang case 58: 1616fa6dfe6bSYan-Hsuan Chuang return 2; 1617fa6dfe6bSYan-Hsuan Chuang case 9: 1618fa6dfe6bSYan-Hsuan Chuang case 10: 1619fa6dfe6bSYan-Hsuan Chuang case 11: 1620fa6dfe6bSYan-Hsuan Chuang case 60: 1621fa6dfe6bSYan-Hsuan Chuang case 62: 1622fa6dfe6bSYan-Hsuan Chuang case 64: 1623fa6dfe6bSYan-Hsuan Chuang return 3; 1624fa6dfe6bSYan-Hsuan Chuang case 12: 1625fa6dfe6bSYan-Hsuan Chuang case 13: 1626fa6dfe6bSYan-Hsuan Chuang case 100: 1627fa6dfe6bSYan-Hsuan Chuang case 102: 1628fa6dfe6bSYan-Hsuan Chuang case 104: 1629fa6dfe6bSYan-Hsuan Chuang case 106: 1630fa6dfe6bSYan-Hsuan Chuang return 4; 1631fa6dfe6bSYan-Hsuan Chuang case 14: 1632fa6dfe6bSYan-Hsuan Chuang case 108: 1633fa6dfe6bSYan-Hsuan Chuang case 110: 1634fa6dfe6bSYan-Hsuan Chuang case 112: 1635fa6dfe6bSYan-Hsuan Chuang case 114: 1636fa6dfe6bSYan-Hsuan Chuang return 5; 1637fa6dfe6bSYan-Hsuan Chuang case 116: 1638fa6dfe6bSYan-Hsuan Chuang case 118: 1639fa6dfe6bSYan-Hsuan Chuang case 120: 1640fa6dfe6bSYan-Hsuan Chuang case 122: 1641fa6dfe6bSYan-Hsuan Chuang return 6; 1642fa6dfe6bSYan-Hsuan Chuang case 124: 1643fa6dfe6bSYan-Hsuan Chuang case 126: 1644fa6dfe6bSYan-Hsuan Chuang case 128: 1645fa6dfe6bSYan-Hsuan Chuang case 130: 1646fa6dfe6bSYan-Hsuan Chuang return 7; 1647fa6dfe6bSYan-Hsuan Chuang case 132: 1648fa6dfe6bSYan-Hsuan Chuang case 134: 1649fa6dfe6bSYan-Hsuan Chuang case 136: 1650fa6dfe6bSYan-Hsuan Chuang case 138: 1651fa6dfe6bSYan-Hsuan Chuang return 8; 1652fa6dfe6bSYan-Hsuan Chuang case 140: 1653fa6dfe6bSYan-Hsuan Chuang case 142: 1654fa6dfe6bSYan-Hsuan Chuang case 144: 1655fa6dfe6bSYan-Hsuan Chuang return 9; 1656fa6dfe6bSYan-Hsuan Chuang case 149: 1657fa6dfe6bSYan-Hsuan Chuang case 151: 1658fa6dfe6bSYan-Hsuan Chuang case 153: 1659fa6dfe6bSYan-Hsuan Chuang case 155: 1660fa6dfe6bSYan-Hsuan Chuang return 10; 1661fa6dfe6bSYan-Hsuan Chuang case 157: 1662fa6dfe6bSYan-Hsuan Chuang case 159: 1663fa6dfe6bSYan-Hsuan Chuang case 161: 1664fa6dfe6bSYan-Hsuan Chuang return 11; 1665fa6dfe6bSYan-Hsuan Chuang case 165: 1666fa6dfe6bSYan-Hsuan Chuang case 167: 1667fa6dfe6bSYan-Hsuan Chuang case 169: 1668fa6dfe6bSYan-Hsuan Chuang case 171: 1669fa6dfe6bSYan-Hsuan Chuang return 12; 1670fa6dfe6bSYan-Hsuan Chuang case 173: 1671fa6dfe6bSYan-Hsuan Chuang case 175: 1672fa6dfe6bSYan-Hsuan Chuang case 177: 1673fa6dfe6bSYan-Hsuan Chuang return 13; 1674fa6dfe6bSYan-Hsuan Chuang } 1675fa6dfe6bSYan-Hsuan Chuang } 1676fa6dfe6bSYan-Hsuan Chuang 16775227c2eeSTzu-En Huang static s8 rtw_phy_get_dis_dpd_by_rate_diff(struct rtw_dev *rtwdev, u16 rate) 16785227c2eeSTzu-En Huang { 16795227c2eeSTzu-En Huang struct rtw_chip_info *chip = rtwdev->chip; 16805227c2eeSTzu-En Huang s8 dpd_diff = 0; 16815227c2eeSTzu-En Huang 16825227c2eeSTzu-En Huang if (!chip->en_dis_dpd) 16835227c2eeSTzu-En Huang return 0; 16845227c2eeSTzu-En Huang 16855227c2eeSTzu-En Huang #define RTW_DPD_RATE_CHECK(_rate) \ 16865227c2eeSTzu-En Huang case DESC_RATE ## _rate: \ 16875227c2eeSTzu-En Huang if (DIS_DPD_RATE ## _rate & chip->dpd_ratemask) \ 16885227c2eeSTzu-En Huang dpd_diff = -6 * chip->txgi_factor; \ 16895227c2eeSTzu-En Huang break 16905227c2eeSTzu-En Huang 16915227c2eeSTzu-En Huang switch (rate) { 16925227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(6M); 16935227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(9M); 16945227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(MCS0); 16955227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(MCS1); 16965227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(MCS8); 16975227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(MCS9); 16985227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(VHT1SS_MCS0); 16995227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(VHT1SS_MCS1); 17005227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(VHT2SS_MCS0); 17015227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(VHT2SS_MCS1); 17025227c2eeSTzu-En Huang } 17035227c2eeSTzu-En Huang #undef RTW_DPD_RATE_CHECK 17045227c2eeSTzu-En Huang 17055227c2eeSTzu-En Huang return dpd_diff; 17065227c2eeSTzu-En Huang } 17075227c2eeSTzu-En Huang 170843712199SYan-Hsuan Chuang static u8 rtw_phy_get_2g_tx_power_index(struct rtw_dev *rtwdev, 1709fa6dfe6bSYan-Hsuan Chuang struct rtw_2g_txpwr_idx *pwr_idx_2g, 1710fa6dfe6bSYan-Hsuan Chuang enum rtw_bandwidth bandwidth, 1711fa6dfe6bSYan-Hsuan Chuang u8 rate, u8 group) 1712fa6dfe6bSYan-Hsuan Chuang { 1713fa6dfe6bSYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 1714fa6dfe6bSYan-Hsuan Chuang u8 tx_power; 1715fa6dfe6bSYan-Hsuan Chuang bool mcs_rate; 1716fa6dfe6bSYan-Hsuan Chuang bool above_2ss; 1717fa6dfe6bSYan-Hsuan Chuang u8 factor = chip->txgi_factor; 1718fa6dfe6bSYan-Hsuan Chuang 1719fa6dfe6bSYan-Hsuan Chuang if (rate <= DESC_RATE11M) 1720fa6dfe6bSYan-Hsuan Chuang tx_power = pwr_idx_2g->cck_base[group]; 1721fa6dfe6bSYan-Hsuan Chuang else 1722fa6dfe6bSYan-Hsuan Chuang tx_power = pwr_idx_2g->bw40_base[group]; 1723fa6dfe6bSYan-Hsuan Chuang 1724fa6dfe6bSYan-Hsuan Chuang if (rate >= DESC_RATE6M && rate <= DESC_RATE54M) 1725fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_2g->ht_1s_diff.ofdm * factor; 1726fa6dfe6bSYan-Hsuan Chuang 1727fa6dfe6bSYan-Hsuan Chuang mcs_rate = (rate >= DESC_RATEMCS0 && rate <= DESC_RATEMCS15) || 1728fa6dfe6bSYan-Hsuan Chuang (rate >= DESC_RATEVHT1SS_MCS0 && 1729fa6dfe6bSYan-Hsuan Chuang rate <= DESC_RATEVHT2SS_MCS9); 1730fa6dfe6bSYan-Hsuan Chuang above_2ss = (rate >= DESC_RATEMCS8 && rate <= DESC_RATEMCS15) || 1731fa6dfe6bSYan-Hsuan Chuang (rate >= DESC_RATEVHT2SS_MCS0); 1732fa6dfe6bSYan-Hsuan Chuang 1733fa6dfe6bSYan-Hsuan Chuang if (!mcs_rate) 1734fa6dfe6bSYan-Hsuan Chuang return tx_power; 1735fa6dfe6bSYan-Hsuan Chuang 1736fa6dfe6bSYan-Hsuan Chuang switch (bandwidth) { 1737fa6dfe6bSYan-Hsuan Chuang default: 1738fa6dfe6bSYan-Hsuan Chuang WARN_ON(1); 17395466aff8SGustavo A. R. Silva fallthrough; 1740fa6dfe6bSYan-Hsuan Chuang case RTW_CHANNEL_WIDTH_20: 1741fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_2g->ht_1s_diff.bw20 * factor; 1742fa6dfe6bSYan-Hsuan Chuang if (above_2ss) 1743fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_2g->ht_2s_diff.bw20 * factor; 1744fa6dfe6bSYan-Hsuan Chuang break; 1745fa6dfe6bSYan-Hsuan Chuang case RTW_CHANNEL_WIDTH_40: 1746fa6dfe6bSYan-Hsuan Chuang /* bw40 is the base power */ 1747fa6dfe6bSYan-Hsuan Chuang if (above_2ss) 1748fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_2g->ht_2s_diff.bw40 * factor; 1749fa6dfe6bSYan-Hsuan Chuang break; 1750fa6dfe6bSYan-Hsuan Chuang } 1751fa6dfe6bSYan-Hsuan Chuang 1752fa6dfe6bSYan-Hsuan Chuang return tx_power; 1753fa6dfe6bSYan-Hsuan Chuang } 1754fa6dfe6bSYan-Hsuan Chuang 175543712199SYan-Hsuan Chuang static u8 rtw_phy_get_5g_tx_power_index(struct rtw_dev *rtwdev, 1756fa6dfe6bSYan-Hsuan Chuang struct rtw_5g_txpwr_idx *pwr_idx_5g, 1757fa6dfe6bSYan-Hsuan Chuang enum rtw_bandwidth bandwidth, 1758fa6dfe6bSYan-Hsuan Chuang u8 rate, u8 group) 1759fa6dfe6bSYan-Hsuan Chuang { 1760fa6dfe6bSYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 1761fa6dfe6bSYan-Hsuan Chuang u8 tx_power; 1762fa6dfe6bSYan-Hsuan Chuang u8 upper, lower; 1763fa6dfe6bSYan-Hsuan Chuang bool mcs_rate; 1764fa6dfe6bSYan-Hsuan Chuang bool above_2ss; 1765fa6dfe6bSYan-Hsuan Chuang u8 factor = chip->txgi_factor; 1766fa6dfe6bSYan-Hsuan Chuang 1767fa6dfe6bSYan-Hsuan Chuang tx_power = pwr_idx_5g->bw40_base[group]; 1768fa6dfe6bSYan-Hsuan Chuang 1769fa6dfe6bSYan-Hsuan Chuang mcs_rate = (rate >= DESC_RATEMCS0 && rate <= DESC_RATEMCS15) || 1770fa6dfe6bSYan-Hsuan Chuang (rate >= DESC_RATEVHT1SS_MCS0 && 1771fa6dfe6bSYan-Hsuan Chuang rate <= DESC_RATEVHT2SS_MCS9); 1772fa6dfe6bSYan-Hsuan Chuang above_2ss = (rate >= DESC_RATEMCS8 && rate <= DESC_RATEMCS15) || 1773fa6dfe6bSYan-Hsuan Chuang (rate >= DESC_RATEVHT2SS_MCS0); 1774fa6dfe6bSYan-Hsuan Chuang 1775fa6dfe6bSYan-Hsuan Chuang if (!mcs_rate) { 1776fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_5g->ht_1s_diff.ofdm * factor; 1777fa6dfe6bSYan-Hsuan Chuang return tx_power; 1778fa6dfe6bSYan-Hsuan Chuang } 1779fa6dfe6bSYan-Hsuan Chuang 1780fa6dfe6bSYan-Hsuan Chuang switch (bandwidth) { 1781fa6dfe6bSYan-Hsuan Chuang default: 1782fa6dfe6bSYan-Hsuan Chuang WARN_ON(1); 17835466aff8SGustavo A. R. Silva fallthrough; 1784fa6dfe6bSYan-Hsuan Chuang case RTW_CHANNEL_WIDTH_20: 1785fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_5g->ht_1s_diff.bw20 * factor; 1786fa6dfe6bSYan-Hsuan Chuang if (above_2ss) 1787fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_5g->ht_2s_diff.bw20 * factor; 1788fa6dfe6bSYan-Hsuan Chuang break; 1789fa6dfe6bSYan-Hsuan Chuang case RTW_CHANNEL_WIDTH_40: 1790fa6dfe6bSYan-Hsuan Chuang /* bw40 is the base power */ 1791fa6dfe6bSYan-Hsuan Chuang if (above_2ss) 1792fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_5g->ht_2s_diff.bw40 * factor; 1793fa6dfe6bSYan-Hsuan Chuang break; 1794fa6dfe6bSYan-Hsuan Chuang case RTW_CHANNEL_WIDTH_80: 1795fa6dfe6bSYan-Hsuan Chuang /* the base idx of bw80 is the average of bw40+/bw40- */ 1796fa6dfe6bSYan-Hsuan Chuang lower = pwr_idx_5g->bw40_base[group]; 1797fa6dfe6bSYan-Hsuan Chuang upper = pwr_idx_5g->bw40_base[group + 1]; 1798fa6dfe6bSYan-Hsuan Chuang 1799fa6dfe6bSYan-Hsuan Chuang tx_power = (lower + upper) / 2; 1800fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_5g->vht_1s_diff.bw80 * factor; 1801fa6dfe6bSYan-Hsuan Chuang if (above_2ss) 1802fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_5g->vht_2s_diff.bw80 * factor; 1803fa6dfe6bSYan-Hsuan Chuang break; 1804fa6dfe6bSYan-Hsuan Chuang } 1805fa6dfe6bSYan-Hsuan Chuang 1806fa6dfe6bSYan-Hsuan Chuang return tx_power; 1807fa6dfe6bSYan-Hsuan Chuang } 1808fa6dfe6bSYan-Hsuan Chuang 180943712199SYan-Hsuan Chuang static s8 rtw_phy_get_tx_power_limit(struct rtw_dev *rtwdev, u8 band, 1810fa6dfe6bSYan-Hsuan Chuang enum rtw_bandwidth bw, u8 rf_path, 1811fa6dfe6bSYan-Hsuan Chuang u8 rate, u8 channel, u8 regd) 1812fa6dfe6bSYan-Hsuan Chuang { 1813fa6dfe6bSYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 181493f68a86SZong-Zhe Yang u8 *cch_by_bw = hal->cch_by_bw; 18150d350f0aSTzu-En Huang s8 power_limit = (s8)rtwdev->chip->max_power_index; 1816fa6dfe6bSYan-Hsuan Chuang u8 rs; 1817fa6dfe6bSYan-Hsuan Chuang int ch_idx; 181893f68a86SZong-Zhe Yang u8 cur_bw, cur_ch; 181993f68a86SZong-Zhe Yang s8 cur_lmt; 1820fa6dfe6bSYan-Hsuan Chuang 182176403816SYan-Hsuan Chuang if (regd > RTW_REGD_WW) 18220d350f0aSTzu-En Huang return power_limit; 182376403816SYan-Hsuan Chuang 1824fa6dfe6bSYan-Hsuan Chuang if (rate >= DESC_RATE1M && rate <= DESC_RATE11M) 1825fa6dfe6bSYan-Hsuan Chuang rs = RTW_RATE_SECTION_CCK; 1826fa6dfe6bSYan-Hsuan Chuang else if (rate >= DESC_RATE6M && rate <= DESC_RATE54M) 1827fa6dfe6bSYan-Hsuan Chuang rs = RTW_RATE_SECTION_OFDM; 1828fa6dfe6bSYan-Hsuan Chuang else if (rate >= DESC_RATEMCS0 && rate <= DESC_RATEMCS7) 1829fa6dfe6bSYan-Hsuan Chuang rs = RTW_RATE_SECTION_HT_1S; 1830fa6dfe6bSYan-Hsuan Chuang else if (rate >= DESC_RATEMCS8 && rate <= DESC_RATEMCS15) 1831fa6dfe6bSYan-Hsuan Chuang rs = RTW_RATE_SECTION_HT_2S; 1832fa6dfe6bSYan-Hsuan Chuang else if (rate >= DESC_RATEVHT1SS_MCS0 && rate <= DESC_RATEVHT1SS_MCS9) 1833fa6dfe6bSYan-Hsuan Chuang rs = RTW_RATE_SECTION_VHT_1S; 1834fa6dfe6bSYan-Hsuan Chuang else if (rate >= DESC_RATEVHT2SS_MCS0 && rate <= DESC_RATEVHT2SS_MCS9) 1835fa6dfe6bSYan-Hsuan Chuang rs = RTW_RATE_SECTION_VHT_2S; 1836fa6dfe6bSYan-Hsuan Chuang else 1837fa6dfe6bSYan-Hsuan Chuang goto err; 1838fa6dfe6bSYan-Hsuan Chuang 183993f68a86SZong-Zhe Yang /* only 20M BW with cck and ofdm */ 184093f68a86SZong-Zhe Yang if (rs == RTW_RATE_SECTION_CCK || rs == RTW_RATE_SECTION_OFDM) 184193f68a86SZong-Zhe Yang bw = RTW_CHANNEL_WIDTH_20; 184293f68a86SZong-Zhe Yang 184393f68a86SZong-Zhe Yang /* only 20/40M BW with ht */ 184493f68a86SZong-Zhe Yang if (rs == RTW_RATE_SECTION_HT_1S || rs == RTW_RATE_SECTION_HT_2S) 184593f68a86SZong-Zhe Yang bw = min_t(u8, bw, RTW_CHANNEL_WIDTH_40); 184693f68a86SZong-Zhe Yang 184793f68a86SZong-Zhe Yang /* select min power limit among [20M BW ~ current BW] */ 184893f68a86SZong-Zhe Yang for (cur_bw = RTW_CHANNEL_WIDTH_20; cur_bw <= bw; cur_bw++) { 184993f68a86SZong-Zhe Yang cur_ch = cch_by_bw[cur_bw]; 185093f68a86SZong-Zhe Yang 185193f68a86SZong-Zhe Yang ch_idx = rtw_channel_to_idx(band, cur_ch); 1852fa6dfe6bSYan-Hsuan Chuang if (ch_idx < 0) 1853fa6dfe6bSYan-Hsuan Chuang goto err; 1854fa6dfe6bSYan-Hsuan Chuang 185593f68a86SZong-Zhe Yang cur_lmt = cur_ch <= RTW_MAX_CHANNEL_NUM_2G ? 185693f68a86SZong-Zhe Yang hal->tx_pwr_limit_2g[regd][cur_bw][rs][ch_idx] : 185793f68a86SZong-Zhe Yang hal->tx_pwr_limit_5g[regd][cur_bw][rs][ch_idx]; 185893f68a86SZong-Zhe Yang 185993f68a86SZong-Zhe Yang power_limit = min_t(s8, cur_lmt, power_limit); 186093f68a86SZong-Zhe Yang } 1861fa6dfe6bSYan-Hsuan Chuang 1862fa6dfe6bSYan-Hsuan Chuang return power_limit; 1863fa6dfe6bSYan-Hsuan Chuang 1864fa6dfe6bSYan-Hsuan Chuang err: 1865fa6dfe6bSYan-Hsuan Chuang WARN(1, "invalid arguments, band=%d, bw=%d, path=%d, rate=%d, ch=%d\n", 1866fa6dfe6bSYan-Hsuan Chuang band, bw, rf_path, rate, channel); 18670d350f0aSTzu-En Huang return (s8)rtwdev->chip->max_power_index; 1868fa6dfe6bSYan-Hsuan Chuang } 1869fa6dfe6bSYan-Hsuan Chuang 1870b7414222SZong-Zhe Yang void rtw_get_tx_power_params(struct rtw_dev *rtwdev, u8 path, u8 rate, u8 bw, 1871b7414222SZong-Zhe Yang u8 ch, u8 regd, struct rtw_power_params *pwr_param) 1872fa6dfe6bSYan-Hsuan Chuang { 1873fa6dfe6bSYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 1874608d2a08SPing-Ke Shih struct rtw_dm_info *dm_info = &rtwdev->dm_info; 1875fa6dfe6bSYan-Hsuan Chuang struct rtw_txpwr_idx *pwr_idx; 1876b7414222SZong-Zhe Yang u8 group, band; 1877b7414222SZong-Zhe Yang u8 *base = &pwr_param->pwr_base; 1878b7414222SZong-Zhe Yang s8 *offset = &pwr_param->pwr_offset; 1879b7414222SZong-Zhe Yang s8 *limit = &pwr_param->pwr_limit; 1880608d2a08SPing-Ke Shih s8 *remnant = &pwr_param->pwr_remnant; 1881fa6dfe6bSYan-Hsuan Chuang 1882b7414222SZong-Zhe Yang pwr_idx = &rtwdev->efuse.txpwr_idx_table[path]; 1883b7414222SZong-Zhe Yang group = rtw_get_channel_group(ch); 1884fa6dfe6bSYan-Hsuan Chuang 1885fa6dfe6bSYan-Hsuan Chuang /* base power index for 2.4G/5G */ 18868575b534SYan-Hsuan Chuang if (IS_CH_2G_BAND(ch)) { 1887fa6dfe6bSYan-Hsuan Chuang band = PHY_BAND_2G; 1888b7414222SZong-Zhe Yang *base = rtw_phy_get_2g_tx_power_index(rtwdev, 1889fa6dfe6bSYan-Hsuan Chuang &pwr_idx->pwr_idx_2g, 1890b7414222SZong-Zhe Yang bw, rate, group); 1891b7414222SZong-Zhe Yang *offset = hal->tx_pwr_by_rate_offset_2g[path][rate]; 1892fa6dfe6bSYan-Hsuan Chuang } else { 1893fa6dfe6bSYan-Hsuan Chuang band = PHY_BAND_5G; 1894b7414222SZong-Zhe Yang *base = rtw_phy_get_5g_tx_power_index(rtwdev, 1895fa6dfe6bSYan-Hsuan Chuang &pwr_idx->pwr_idx_5g, 1896b7414222SZong-Zhe Yang bw, rate, group); 1897b7414222SZong-Zhe Yang *offset = hal->tx_pwr_by_rate_offset_5g[path][rate]; 1898fa6dfe6bSYan-Hsuan Chuang } 1899fa6dfe6bSYan-Hsuan Chuang 1900b7414222SZong-Zhe Yang *limit = rtw_phy_get_tx_power_limit(rtwdev, band, bw, path, 1901b7414222SZong-Zhe Yang rate, ch, regd); 1902608d2a08SPing-Ke Shih *remnant = (rate <= DESC_RATE11M ? dm_info->txagc_remnant_cck : 1903608d2a08SPing-Ke Shih dm_info->txagc_remnant_ofdm); 1904b7414222SZong-Zhe Yang } 1905fa6dfe6bSYan-Hsuan Chuang 1906b7414222SZong-Zhe Yang u8 1907b7414222SZong-Zhe Yang rtw_phy_get_tx_power_index(struct rtw_dev *rtwdev, u8 rf_path, u8 rate, 1908b7414222SZong-Zhe Yang enum rtw_bandwidth bandwidth, u8 channel, u8 regd) 1909b7414222SZong-Zhe Yang { 1910b7414222SZong-Zhe Yang struct rtw_power_params pwr_param = {0}; 1911b7414222SZong-Zhe Yang u8 tx_power; 1912b7414222SZong-Zhe Yang s8 offset; 1913b7414222SZong-Zhe Yang 1914b7414222SZong-Zhe Yang rtw_get_tx_power_params(rtwdev, rf_path, rate, bandwidth, 1915b7414222SZong-Zhe Yang channel, regd, &pwr_param); 1916b7414222SZong-Zhe Yang 1917b7414222SZong-Zhe Yang tx_power = pwr_param.pwr_base; 1918b7414222SZong-Zhe Yang offset = min_t(s8, pwr_param.pwr_offset, pwr_param.pwr_limit); 1919fa6dfe6bSYan-Hsuan Chuang 19205227c2eeSTzu-En Huang if (rtwdev->chip->en_dis_dpd) 19215227c2eeSTzu-En Huang offset += rtw_phy_get_dis_dpd_by_rate_diff(rtwdev, rate); 19225227c2eeSTzu-En Huang 1923608d2a08SPing-Ke Shih tx_power += offset + pwr_param.pwr_remnant; 1924fa6dfe6bSYan-Hsuan Chuang 1925fa6dfe6bSYan-Hsuan Chuang if (tx_power > rtwdev->chip->max_power_index) 1926fa6dfe6bSYan-Hsuan Chuang tx_power = rtwdev->chip->max_power_index; 1927fa6dfe6bSYan-Hsuan Chuang 1928fa6dfe6bSYan-Hsuan Chuang return tx_power; 1929fa6dfe6bSYan-Hsuan Chuang } 1930449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_get_tx_power_index); 1931fa6dfe6bSYan-Hsuan Chuang 193243712199SYan-Hsuan Chuang static void rtw_phy_set_tx_power_index_by_rs(struct rtw_dev *rtwdev, 1933226746fdSYan-Hsuan Chuang u8 ch, u8 path, u8 rs) 1934fa6dfe6bSYan-Hsuan Chuang { 1935fa6dfe6bSYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 1936fa6dfe6bSYan-Hsuan Chuang u8 regd = rtwdev->regd.txpwr_regd; 1937fa6dfe6bSYan-Hsuan Chuang u8 *rates; 1938fa6dfe6bSYan-Hsuan Chuang u8 size; 1939fa6dfe6bSYan-Hsuan Chuang u8 rate; 1940fa6dfe6bSYan-Hsuan Chuang u8 pwr_idx; 1941fa6dfe6bSYan-Hsuan Chuang u8 bw; 1942fa6dfe6bSYan-Hsuan Chuang int i; 1943fa6dfe6bSYan-Hsuan Chuang 1944fa6dfe6bSYan-Hsuan Chuang if (rs >= RTW_RATE_SECTION_MAX) 1945fa6dfe6bSYan-Hsuan Chuang return; 1946fa6dfe6bSYan-Hsuan Chuang 1947fa6dfe6bSYan-Hsuan Chuang rates = rtw_rate_section[rs]; 1948fa6dfe6bSYan-Hsuan Chuang size = rtw_rate_size[rs]; 1949fa6dfe6bSYan-Hsuan Chuang bw = hal->current_band_width; 1950fa6dfe6bSYan-Hsuan Chuang for (i = 0; i < size; i++) { 1951fa6dfe6bSYan-Hsuan Chuang rate = rates[i]; 195243712199SYan-Hsuan Chuang pwr_idx = rtw_phy_get_tx_power_index(rtwdev, path, rate, 195343712199SYan-Hsuan Chuang bw, ch, regd); 1954fa6dfe6bSYan-Hsuan Chuang hal->tx_pwr_tbl[path][rate] = pwr_idx; 1955fa6dfe6bSYan-Hsuan Chuang } 1956fa6dfe6bSYan-Hsuan Chuang } 1957fa6dfe6bSYan-Hsuan Chuang 1958fa6dfe6bSYan-Hsuan Chuang /* set tx power level by path for each rates, note that the order of the rates 1959fa6dfe6bSYan-Hsuan Chuang * are *very* important, bacause 8822B/8821C combines every four bytes of tx 1960fa6dfe6bSYan-Hsuan Chuang * power index into a four-byte power index register, and calls set_tx_agc to 1961fa6dfe6bSYan-Hsuan Chuang * write these values into hardware 1962fa6dfe6bSYan-Hsuan Chuang */ 196343712199SYan-Hsuan Chuang static void rtw_phy_set_tx_power_level_by_path(struct rtw_dev *rtwdev, 196443712199SYan-Hsuan Chuang u8 ch, u8 path) 1965fa6dfe6bSYan-Hsuan Chuang { 1966fa6dfe6bSYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 1967fa6dfe6bSYan-Hsuan Chuang u8 rs; 1968fa6dfe6bSYan-Hsuan Chuang 1969fa6dfe6bSYan-Hsuan Chuang /* do not need cck rates if we are not in 2.4G */ 1970fa6dfe6bSYan-Hsuan Chuang if (hal->current_band_type == RTW_BAND_2G) 1971fa6dfe6bSYan-Hsuan Chuang rs = RTW_RATE_SECTION_CCK; 1972fa6dfe6bSYan-Hsuan Chuang else 1973fa6dfe6bSYan-Hsuan Chuang rs = RTW_RATE_SECTION_OFDM; 1974fa6dfe6bSYan-Hsuan Chuang 1975fa6dfe6bSYan-Hsuan Chuang for (; rs < RTW_RATE_SECTION_MAX; rs++) 197643712199SYan-Hsuan Chuang rtw_phy_set_tx_power_index_by_rs(rtwdev, ch, path, rs); 1977fa6dfe6bSYan-Hsuan Chuang } 1978fa6dfe6bSYan-Hsuan Chuang 1979fa6dfe6bSYan-Hsuan Chuang void rtw_phy_set_tx_power_level(struct rtw_dev *rtwdev, u8 channel) 1980fa6dfe6bSYan-Hsuan Chuang { 1981fa6dfe6bSYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 1982fa6dfe6bSYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 1983fa6dfe6bSYan-Hsuan Chuang u8 path; 1984fa6dfe6bSYan-Hsuan Chuang 1985fa6dfe6bSYan-Hsuan Chuang mutex_lock(&hal->tx_power_mutex); 1986fa6dfe6bSYan-Hsuan Chuang 1987fa6dfe6bSYan-Hsuan Chuang for (path = 0; path < hal->rf_path_num; path++) 198843712199SYan-Hsuan Chuang rtw_phy_set_tx_power_level_by_path(rtwdev, channel, path); 1989fa6dfe6bSYan-Hsuan Chuang 1990fa6dfe6bSYan-Hsuan Chuang chip->ops->set_tx_power_index(rtwdev); 1991fa6dfe6bSYan-Hsuan Chuang mutex_unlock(&hal->tx_power_mutex); 1992fa6dfe6bSYan-Hsuan Chuang } 1993449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_set_tx_power_level); 1994fa6dfe6bSYan-Hsuan Chuang 199543712199SYan-Hsuan Chuang static void 199643712199SYan-Hsuan Chuang rtw_phy_tx_power_by_rate_config_by_path(struct rtw_hal *hal, u8 path, 1997e3037485SYan-Hsuan Chuang u8 rs, u8 size, u8 *rates) 1998e3037485SYan-Hsuan Chuang { 1999e3037485SYan-Hsuan Chuang u8 rate; 2000e3037485SYan-Hsuan Chuang u8 base_idx, rate_idx; 2001e3037485SYan-Hsuan Chuang s8 base_2g, base_5g; 2002e3037485SYan-Hsuan Chuang 2003e3037485SYan-Hsuan Chuang if (rs >= RTW_RATE_SECTION_VHT_1S) 2004e3037485SYan-Hsuan Chuang base_idx = rates[size - 3]; 2005e3037485SYan-Hsuan Chuang else 2006e3037485SYan-Hsuan Chuang base_idx = rates[size - 1]; 2007e3037485SYan-Hsuan Chuang base_2g = hal->tx_pwr_by_rate_offset_2g[path][base_idx]; 2008e3037485SYan-Hsuan Chuang base_5g = hal->tx_pwr_by_rate_offset_5g[path][base_idx]; 2009e3037485SYan-Hsuan Chuang hal->tx_pwr_by_rate_base_2g[path][rs] = base_2g; 2010e3037485SYan-Hsuan Chuang hal->tx_pwr_by_rate_base_5g[path][rs] = base_5g; 2011e3037485SYan-Hsuan Chuang for (rate = 0; rate < size; rate++) { 2012e3037485SYan-Hsuan Chuang rate_idx = rates[rate]; 2013e3037485SYan-Hsuan Chuang hal->tx_pwr_by_rate_offset_2g[path][rate_idx] -= base_2g; 2014e3037485SYan-Hsuan Chuang hal->tx_pwr_by_rate_offset_5g[path][rate_idx] -= base_5g; 2015e3037485SYan-Hsuan Chuang } 2016e3037485SYan-Hsuan Chuang } 2017e3037485SYan-Hsuan Chuang 2018e3037485SYan-Hsuan Chuang void rtw_phy_tx_power_by_rate_config(struct rtw_hal *hal) 2019e3037485SYan-Hsuan Chuang { 2020e3037485SYan-Hsuan Chuang u8 path; 2021e3037485SYan-Hsuan Chuang 2022e3037485SYan-Hsuan Chuang for (path = 0; path < RTW_RF_PATH_MAX; path++) { 202343712199SYan-Hsuan Chuang rtw_phy_tx_power_by_rate_config_by_path(hal, path, 2024e3037485SYan-Hsuan Chuang RTW_RATE_SECTION_CCK, 2025e3037485SYan-Hsuan Chuang rtw_cck_size, rtw_cck_rates); 202643712199SYan-Hsuan Chuang rtw_phy_tx_power_by_rate_config_by_path(hal, path, 2027e3037485SYan-Hsuan Chuang RTW_RATE_SECTION_OFDM, 2028e3037485SYan-Hsuan Chuang rtw_ofdm_size, rtw_ofdm_rates); 202943712199SYan-Hsuan Chuang rtw_phy_tx_power_by_rate_config_by_path(hal, path, 2030e3037485SYan-Hsuan Chuang RTW_RATE_SECTION_HT_1S, 2031e3037485SYan-Hsuan Chuang rtw_ht_1s_size, rtw_ht_1s_rates); 203243712199SYan-Hsuan Chuang rtw_phy_tx_power_by_rate_config_by_path(hal, path, 2033e3037485SYan-Hsuan Chuang RTW_RATE_SECTION_HT_2S, 2034e3037485SYan-Hsuan Chuang rtw_ht_2s_size, rtw_ht_2s_rates); 203543712199SYan-Hsuan Chuang rtw_phy_tx_power_by_rate_config_by_path(hal, path, 2036e3037485SYan-Hsuan Chuang RTW_RATE_SECTION_VHT_1S, 2037e3037485SYan-Hsuan Chuang rtw_vht_1s_size, rtw_vht_1s_rates); 203843712199SYan-Hsuan Chuang rtw_phy_tx_power_by_rate_config_by_path(hal, path, 2039e3037485SYan-Hsuan Chuang RTW_RATE_SECTION_VHT_2S, 2040e3037485SYan-Hsuan Chuang rtw_vht_2s_size, rtw_vht_2s_rates); 2041e3037485SYan-Hsuan Chuang } 2042e3037485SYan-Hsuan Chuang } 2043e3037485SYan-Hsuan Chuang 2044e3037485SYan-Hsuan Chuang static void 204543712199SYan-Hsuan Chuang __rtw_phy_tx_power_limit_config(struct rtw_hal *hal, u8 regd, u8 bw, u8 rs) 2046e3037485SYan-Hsuan Chuang { 204752280149SYan-Hsuan Chuang s8 base; 2048e3037485SYan-Hsuan Chuang u8 ch; 2049e3037485SYan-Hsuan Chuang 2050e3037485SYan-Hsuan Chuang for (ch = 0; ch < RTW_MAX_CHANNEL_NUM_2G; ch++) { 2051e3037485SYan-Hsuan Chuang base = hal->tx_pwr_by_rate_base_2g[0][rs]; 2052e3037485SYan-Hsuan Chuang hal->tx_pwr_limit_2g[regd][bw][rs][ch] -= base; 2053e3037485SYan-Hsuan Chuang } 2054e3037485SYan-Hsuan Chuang 2055e3037485SYan-Hsuan Chuang for (ch = 0; ch < RTW_MAX_CHANNEL_NUM_5G; ch++) { 2056e3037485SYan-Hsuan Chuang base = hal->tx_pwr_by_rate_base_5g[0][rs]; 2057e3037485SYan-Hsuan Chuang hal->tx_pwr_limit_5g[regd][bw][rs][ch] -= base; 2058e3037485SYan-Hsuan Chuang } 2059e3037485SYan-Hsuan Chuang } 2060e3037485SYan-Hsuan Chuang 2061e3037485SYan-Hsuan Chuang void rtw_phy_tx_power_limit_config(struct rtw_hal *hal) 2062e3037485SYan-Hsuan Chuang { 2063e3037485SYan-Hsuan Chuang u8 regd, bw, rs; 2064e3037485SYan-Hsuan Chuang 206593f68a86SZong-Zhe Yang /* default at channel 1 */ 206693f68a86SZong-Zhe Yang hal->cch_by_bw[RTW_CHANNEL_WIDTH_20] = 1; 206793f68a86SZong-Zhe Yang 2068e3037485SYan-Hsuan Chuang for (regd = 0; regd < RTW_REGD_MAX; regd++) 2069e3037485SYan-Hsuan Chuang for (bw = 0; bw < RTW_CHANNEL_WIDTH_MAX; bw++) 2070e3037485SYan-Hsuan Chuang for (rs = 0; rs < RTW_RATE_SECTION_MAX; rs++) 207143712199SYan-Hsuan Chuang __rtw_phy_tx_power_limit_config(hal, regd, bw, rs); 2072e3037485SYan-Hsuan Chuang } 2073e3037485SYan-Hsuan Chuang 20740d350f0aSTzu-En Huang static void rtw_phy_init_tx_power_limit(struct rtw_dev *rtwdev, 207543712199SYan-Hsuan Chuang u8 regd, u8 bw, u8 rs) 2076e3037485SYan-Hsuan Chuang { 20770d350f0aSTzu-En Huang struct rtw_hal *hal = &rtwdev->hal; 20780d350f0aSTzu-En Huang s8 max_power_index = (s8)rtwdev->chip->max_power_index; 2079e3037485SYan-Hsuan Chuang u8 ch; 2080e3037485SYan-Hsuan Chuang 2081e3037485SYan-Hsuan Chuang /* 2.4G channels */ 2082e3037485SYan-Hsuan Chuang for (ch = 0; ch < RTW_MAX_CHANNEL_NUM_2G; ch++) 20830d350f0aSTzu-En Huang hal->tx_pwr_limit_2g[regd][bw][rs][ch] = max_power_index; 2084e3037485SYan-Hsuan Chuang 2085e3037485SYan-Hsuan Chuang /* 5G channels */ 2086e3037485SYan-Hsuan Chuang for (ch = 0; ch < RTW_MAX_CHANNEL_NUM_5G; ch++) 20870d350f0aSTzu-En Huang hal->tx_pwr_limit_5g[regd][bw][rs][ch] = max_power_index; 2088e3037485SYan-Hsuan Chuang } 2089e3037485SYan-Hsuan Chuang 20900d350f0aSTzu-En Huang void rtw_phy_init_tx_power(struct rtw_dev *rtwdev) 2091e3037485SYan-Hsuan Chuang { 20920d350f0aSTzu-En Huang struct rtw_hal *hal = &rtwdev->hal; 2093e3037485SYan-Hsuan Chuang u8 regd, path, rate, rs, bw; 2094e3037485SYan-Hsuan Chuang 2095e3037485SYan-Hsuan Chuang /* init tx power by rate offset */ 2096e3037485SYan-Hsuan Chuang for (path = 0; path < RTW_RF_PATH_MAX; path++) { 2097e3037485SYan-Hsuan Chuang for (rate = 0; rate < DESC_RATE_MAX; rate++) { 2098e3037485SYan-Hsuan Chuang hal->tx_pwr_by_rate_offset_2g[path][rate] = 0; 2099e3037485SYan-Hsuan Chuang hal->tx_pwr_by_rate_offset_5g[path][rate] = 0; 2100e3037485SYan-Hsuan Chuang } 2101e3037485SYan-Hsuan Chuang } 2102e3037485SYan-Hsuan Chuang 2103e3037485SYan-Hsuan Chuang /* init tx power limit */ 2104e3037485SYan-Hsuan Chuang for (regd = 0; regd < RTW_REGD_MAX; regd++) 2105e3037485SYan-Hsuan Chuang for (bw = 0; bw < RTW_CHANNEL_WIDTH_MAX; bw++) 2106e3037485SYan-Hsuan Chuang for (rs = 0; rs < RTW_RATE_SECTION_MAX; rs++) 21070d350f0aSTzu-En Huang rtw_phy_init_tx_power_limit(rtwdev, regd, bw, 21080d350f0aSTzu-En Huang rs); 2109e3037485SYan-Hsuan Chuang } 2110c97ee3e0STzu-En Huang 2111c97ee3e0STzu-En Huang void rtw_phy_config_swing_table(struct rtw_dev *rtwdev, 2112c97ee3e0STzu-En Huang struct rtw_swing_table *swing_table) 2113c97ee3e0STzu-En Huang { 2114c97ee3e0STzu-En Huang const struct rtw_pwr_track_tbl *tbl = rtwdev->chip->pwr_track_tbl; 2115c97ee3e0STzu-En Huang u8 channel = rtwdev->hal.current_channel; 2116c97ee3e0STzu-En Huang 2117c97ee3e0STzu-En Huang if (IS_CH_2G_BAND(channel)) { 2118c97ee3e0STzu-En Huang if (rtwdev->dm_info.tx_rate <= DESC_RATE11M) { 2119c97ee3e0STzu-En Huang swing_table->p[RF_PATH_A] = tbl->pwrtrk_2g_ccka_p; 2120c97ee3e0STzu-En Huang swing_table->n[RF_PATH_A] = tbl->pwrtrk_2g_ccka_n; 2121c97ee3e0STzu-En Huang swing_table->p[RF_PATH_B] = tbl->pwrtrk_2g_cckb_p; 2122c97ee3e0STzu-En Huang swing_table->n[RF_PATH_B] = tbl->pwrtrk_2g_cckb_n; 2123c97ee3e0STzu-En Huang } else { 2124c97ee3e0STzu-En Huang swing_table->p[RF_PATH_A] = tbl->pwrtrk_2ga_p; 2125c97ee3e0STzu-En Huang swing_table->n[RF_PATH_A] = tbl->pwrtrk_2ga_n; 2126c97ee3e0STzu-En Huang swing_table->p[RF_PATH_B] = tbl->pwrtrk_2gb_p; 2127c97ee3e0STzu-En Huang swing_table->n[RF_PATH_B] = tbl->pwrtrk_2gb_n; 2128c97ee3e0STzu-En Huang } 2129c97ee3e0STzu-En Huang } else if (IS_CH_5G_BAND_1(channel) || IS_CH_5G_BAND_2(channel)) { 2130c97ee3e0STzu-En Huang swing_table->p[RF_PATH_A] = tbl->pwrtrk_5ga_p[RTW_PWR_TRK_5G_1]; 2131c97ee3e0STzu-En Huang swing_table->n[RF_PATH_A] = tbl->pwrtrk_5ga_n[RTW_PWR_TRK_5G_1]; 2132c97ee3e0STzu-En Huang swing_table->p[RF_PATH_B] = tbl->pwrtrk_5gb_p[RTW_PWR_TRK_5G_1]; 2133c97ee3e0STzu-En Huang swing_table->n[RF_PATH_B] = tbl->pwrtrk_5gb_n[RTW_PWR_TRK_5G_1]; 2134c97ee3e0STzu-En Huang } else if (IS_CH_5G_BAND_3(channel)) { 2135c97ee3e0STzu-En Huang swing_table->p[RF_PATH_A] = tbl->pwrtrk_5ga_p[RTW_PWR_TRK_5G_2]; 2136c97ee3e0STzu-En Huang swing_table->n[RF_PATH_A] = tbl->pwrtrk_5ga_n[RTW_PWR_TRK_5G_2]; 2137c97ee3e0STzu-En Huang swing_table->p[RF_PATH_B] = tbl->pwrtrk_5gb_p[RTW_PWR_TRK_5G_2]; 2138c97ee3e0STzu-En Huang swing_table->n[RF_PATH_B] = tbl->pwrtrk_5gb_n[RTW_PWR_TRK_5G_2]; 2139c97ee3e0STzu-En Huang } else if (IS_CH_5G_BAND_4(channel)) { 2140c97ee3e0STzu-En Huang swing_table->p[RF_PATH_A] = tbl->pwrtrk_5ga_p[RTW_PWR_TRK_5G_3]; 2141c97ee3e0STzu-En Huang swing_table->n[RF_PATH_A] = tbl->pwrtrk_5ga_n[RTW_PWR_TRK_5G_3]; 2142c97ee3e0STzu-En Huang swing_table->p[RF_PATH_B] = tbl->pwrtrk_5gb_p[RTW_PWR_TRK_5G_3]; 2143c97ee3e0STzu-En Huang swing_table->n[RF_PATH_B] = tbl->pwrtrk_5gb_n[RTW_PWR_TRK_5G_3]; 2144c97ee3e0STzu-En Huang } else { 2145c97ee3e0STzu-En Huang swing_table->p[RF_PATH_A] = tbl->pwrtrk_2ga_p; 2146c97ee3e0STzu-En Huang swing_table->n[RF_PATH_A] = tbl->pwrtrk_2ga_n; 2147c97ee3e0STzu-En Huang swing_table->p[RF_PATH_B] = tbl->pwrtrk_2gb_p; 2148c97ee3e0STzu-En Huang swing_table->n[RF_PATH_B] = tbl->pwrtrk_2gb_n; 2149c97ee3e0STzu-En Huang } 2150c97ee3e0STzu-En Huang } 2151449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_config_swing_table); 2152c97ee3e0STzu-En Huang 2153c97ee3e0STzu-En Huang void rtw_phy_pwrtrack_avg(struct rtw_dev *rtwdev, u8 thermal, u8 path) 2154c97ee3e0STzu-En Huang { 2155c97ee3e0STzu-En Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 2156c97ee3e0STzu-En Huang 2157c97ee3e0STzu-En Huang ewma_thermal_add(&dm_info->avg_thermal[path], thermal); 2158c97ee3e0STzu-En Huang dm_info->thermal_avg[path] = 2159c97ee3e0STzu-En Huang ewma_thermal_read(&dm_info->avg_thermal[path]); 2160c97ee3e0STzu-En Huang } 2161449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_pwrtrack_avg); 2162c97ee3e0STzu-En Huang 2163c97ee3e0STzu-En Huang bool rtw_phy_pwrtrack_thermal_changed(struct rtw_dev *rtwdev, u8 thermal, 2164c97ee3e0STzu-En Huang u8 path) 2165c97ee3e0STzu-En Huang { 2166c97ee3e0STzu-En Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 2167c97ee3e0STzu-En Huang u8 avg = ewma_thermal_read(&dm_info->avg_thermal[path]); 2168c97ee3e0STzu-En Huang 2169c97ee3e0STzu-En Huang if (avg == thermal) 2170c97ee3e0STzu-En Huang return false; 2171c97ee3e0STzu-En Huang 2172c97ee3e0STzu-En Huang return true; 2173c97ee3e0STzu-En Huang } 2174449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_pwrtrack_thermal_changed); 2175c97ee3e0STzu-En Huang 2176c97ee3e0STzu-En Huang u8 rtw_phy_pwrtrack_get_delta(struct rtw_dev *rtwdev, u8 path) 2177c97ee3e0STzu-En Huang { 2178c97ee3e0STzu-En Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 2179c97ee3e0STzu-En Huang u8 therm_avg, therm_efuse, therm_delta; 2180c97ee3e0STzu-En Huang 2181c97ee3e0STzu-En Huang therm_avg = dm_info->thermal_avg[path]; 2182c97ee3e0STzu-En Huang therm_efuse = rtwdev->efuse.thermal_meter[path]; 2183c97ee3e0STzu-En Huang therm_delta = abs(therm_avg - therm_efuse); 2184c97ee3e0STzu-En Huang 2185c97ee3e0STzu-En Huang return min_t(u8, therm_delta, RTW_PWR_TRK_TBL_SZ - 1); 2186c97ee3e0STzu-En Huang } 2187449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_pwrtrack_get_delta); 2188c97ee3e0STzu-En Huang 2189c97ee3e0STzu-En Huang s8 rtw_phy_pwrtrack_get_pwridx(struct rtw_dev *rtwdev, 2190c97ee3e0STzu-En Huang struct rtw_swing_table *swing_table, 2191c97ee3e0STzu-En Huang u8 tbl_path, u8 therm_path, u8 delta) 2192c97ee3e0STzu-En Huang { 2193c97ee3e0STzu-En Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 2194c97ee3e0STzu-En Huang const u8 *delta_swing_table_idx_pos; 2195c97ee3e0STzu-En Huang const u8 *delta_swing_table_idx_neg; 2196c97ee3e0STzu-En Huang 2197c97ee3e0STzu-En Huang if (delta >= RTW_PWR_TRK_TBL_SZ) { 2198c97ee3e0STzu-En Huang rtw_warn(rtwdev, "power track table overflow\n"); 2199c97ee3e0STzu-En Huang return 0; 2200c97ee3e0STzu-En Huang } 2201c97ee3e0STzu-En Huang 2202baff8da6SColin Ian King if (!swing_table) { 2203c97ee3e0STzu-En Huang rtw_warn(rtwdev, "swing table not configured\n"); 2204c97ee3e0STzu-En Huang return 0; 2205c97ee3e0STzu-En Huang } 2206c97ee3e0STzu-En Huang 2207c97ee3e0STzu-En Huang delta_swing_table_idx_pos = swing_table->p[tbl_path]; 2208c97ee3e0STzu-En Huang delta_swing_table_idx_neg = swing_table->n[tbl_path]; 2209c97ee3e0STzu-En Huang 2210c97ee3e0STzu-En Huang if (!delta_swing_table_idx_pos || !delta_swing_table_idx_neg) { 2211c97ee3e0STzu-En Huang rtw_warn(rtwdev, "invalid swing table index\n"); 2212c97ee3e0STzu-En Huang return 0; 2213c97ee3e0STzu-En Huang } 2214c97ee3e0STzu-En Huang 2215c97ee3e0STzu-En Huang if (dm_info->thermal_avg[therm_path] > 2216c97ee3e0STzu-En Huang rtwdev->efuse.thermal_meter[therm_path]) 2217c97ee3e0STzu-En Huang return delta_swing_table_idx_pos[delta]; 2218c97ee3e0STzu-En Huang else 2219c97ee3e0STzu-En Huang return -delta_swing_table_idx_neg[delta]; 2220c97ee3e0STzu-En Huang } 2221449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_pwrtrack_get_pwridx); 2222c97ee3e0STzu-En Huang 2223c97ee3e0STzu-En Huang bool rtw_phy_pwrtrack_need_iqk(struct rtw_dev *rtwdev) 2224c97ee3e0STzu-En Huang { 2225c97ee3e0STzu-En Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 2226c97ee3e0STzu-En Huang u8 delta_iqk; 2227c97ee3e0STzu-En Huang 2228c97ee3e0STzu-En Huang delta_iqk = abs(dm_info->thermal_avg[0] - dm_info->thermal_meter_k); 2229c97ee3e0STzu-En Huang if (delta_iqk >= rtwdev->chip->iqk_threshold) { 2230c97ee3e0STzu-En Huang dm_info->thermal_meter_k = dm_info->thermal_avg[0]; 2231c97ee3e0STzu-En Huang return true; 2232c97ee3e0STzu-En Huang } 2233c97ee3e0STzu-En Huang return false; 2234c97ee3e0STzu-En Huang } 2235449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_pwrtrack_need_iqk); 2236