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 }; 85fa6dfe6bSYan-Hsuan Chuang u8 rtw_rate_size[RTW_RATE_SECTION_MAX] = { 86fa6dfe6bSYan-Hsuan Chuang ARRAY_SIZE(rtw_cck_rates), 87fa6dfe6bSYan-Hsuan Chuang ARRAY_SIZE(rtw_ofdm_rates), 88fa6dfe6bSYan-Hsuan Chuang ARRAY_SIZE(rtw_ht_1s_rates), 89fa6dfe6bSYan-Hsuan Chuang ARRAY_SIZE(rtw_ht_2s_rates), 90fa6dfe6bSYan-Hsuan Chuang ARRAY_SIZE(rtw_vht_1s_rates), 91fa6dfe6bSYan-Hsuan Chuang ARRAY_SIZE(rtw_vht_2s_rates) 92fa6dfe6bSYan-Hsuan Chuang }; 93fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_cck_size = ARRAY_SIZE(rtw_cck_rates); 94fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_ofdm_size = ARRAY_SIZE(rtw_ofdm_rates); 95fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_ht_1s_size = ARRAY_SIZE(rtw_ht_1s_rates); 96fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_ht_2s_size = ARRAY_SIZE(rtw_ht_2s_rates); 97fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_vht_1s_size = ARRAY_SIZE(rtw_vht_1s_rates); 98fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_vht_2s_size = ARRAY_SIZE(rtw_vht_2s_rates); 99fa6dfe6bSYan-Hsuan Chuang 100e3037485SYan-Hsuan Chuang enum rtw_phy_band_type { 101e3037485SYan-Hsuan Chuang PHY_BAND_2G = 0, 102e3037485SYan-Hsuan Chuang PHY_BAND_5G = 1, 103e3037485SYan-Hsuan Chuang }; 104e3037485SYan-Hsuan Chuang 105479c4ee9STzu-En Huang static void rtw_phy_cck_pd_init(struct rtw_dev *rtwdev) 106479c4ee9STzu-En Huang { 107479c4ee9STzu-En Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 108479c4ee9STzu-En Huang u8 i, j; 109479c4ee9STzu-En Huang 110479c4ee9STzu-En Huang for (i = 0; i <= RTW_CHANNEL_WIDTH_40; i++) { 111479c4ee9STzu-En Huang for (j = 0; j < RTW_RF_PATH_MAX; j++) 11218a0696eSTzu-En Huang dm_info->cck_pd_lv[i][j] = CCK_PD_LV0; 113479c4ee9STzu-En Huang } 114479c4ee9STzu-En Huang 115479c4ee9STzu-En Huang dm_info->cck_fa_avg = CCK_FA_AVG_RESET; 116479c4ee9STzu-En Huang } 117479c4ee9STzu-En Huang 118e3037485SYan-Hsuan Chuang void rtw_phy_init(struct rtw_dev *rtwdev) 119e3037485SYan-Hsuan Chuang { 120e3037485SYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 121e3037485SYan-Hsuan Chuang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 122e3037485SYan-Hsuan Chuang u32 addr, mask; 123e3037485SYan-Hsuan Chuang 124e3037485SYan-Hsuan Chuang dm_info->fa_history[3] = 0; 125e3037485SYan-Hsuan Chuang dm_info->fa_history[2] = 0; 126e3037485SYan-Hsuan Chuang dm_info->fa_history[1] = 0; 127e3037485SYan-Hsuan Chuang dm_info->fa_history[0] = 0; 128e3037485SYan-Hsuan Chuang dm_info->igi_bitmap = 0; 129e3037485SYan-Hsuan Chuang dm_info->igi_history[3] = 0; 130e3037485SYan-Hsuan Chuang dm_info->igi_history[2] = 0; 131e3037485SYan-Hsuan Chuang dm_info->igi_history[1] = 0; 132e3037485SYan-Hsuan Chuang 133e3037485SYan-Hsuan Chuang addr = chip->dig[0].addr; 134e3037485SYan-Hsuan Chuang mask = chip->dig[0].mask; 135e3037485SYan-Hsuan Chuang dm_info->igi_history[0] = rtw_read32_mask(rtwdev, addr, mask); 136479c4ee9STzu-En Huang rtw_phy_cck_pd_init(rtwdev); 137e3037485SYan-Hsuan Chuang } 138e3037485SYan-Hsuan Chuang 139e3037485SYan-Hsuan Chuang void rtw_phy_dig_write(struct rtw_dev *rtwdev, u8 igi) 140e3037485SYan-Hsuan Chuang { 141e3037485SYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 142e3037485SYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 143e3037485SYan-Hsuan Chuang u32 addr, mask; 144e3037485SYan-Hsuan Chuang u8 path; 145e3037485SYan-Hsuan Chuang 146e3037485SYan-Hsuan Chuang for (path = 0; path < hal->rf_path_num; path++) { 147e3037485SYan-Hsuan Chuang addr = chip->dig[path].addr; 148e3037485SYan-Hsuan Chuang mask = chip->dig[path].mask; 149e3037485SYan-Hsuan Chuang rtw_write32_mask(rtwdev, addr, mask, igi); 150e3037485SYan-Hsuan Chuang } 151e3037485SYan-Hsuan Chuang } 152e3037485SYan-Hsuan Chuang 153e3037485SYan-Hsuan Chuang static void rtw_phy_stat_false_alarm(struct rtw_dev *rtwdev) 154e3037485SYan-Hsuan Chuang { 155e3037485SYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 156e3037485SYan-Hsuan Chuang 157e3037485SYan-Hsuan Chuang chip->ops->false_alarm_statistics(rtwdev); 158e3037485SYan-Hsuan Chuang } 159e3037485SYan-Hsuan Chuang 160e3037485SYan-Hsuan Chuang #define RA_FLOOR_TABLE_SIZE 7 161e3037485SYan-Hsuan Chuang #define RA_FLOOR_UP_GAP 3 162e3037485SYan-Hsuan Chuang 163e3037485SYan-Hsuan Chuang static u8 rtw_phy_get_rssi_level(u8 old_level, u8 rssi) 164e3037485SYan-Hsuan Chuang { 165e3037485SYan-Hsuan Chuang u8 table[RA_FLOOR_TABLE_SIZE] = {20, 34, 38, 42, 46, 50, 100}; 166e3037485SYan-Hsuan Chuang u8 new_level = 0; 167e3037485SYan-Hsuan Chuang int i; 168e3037485SYan-Hsuan Chuang 169e3037485SYan-Hsuan Chuang for (i = 0; i < RA_FLOOR_TABLE_SIZE; i++) 170e3037485SYan-Hsuan Chuang if (i >= old_level) 171e3037485SYan-Hsuan Chuang table[i] += RA_FLOOR_UP_GAP; 172e3037485SYan-Hsuan Chuang 173e3037485SYan-Hsuan Chuang for (i = 0; i < RA_FLOOR_TABLE_SIZE; i++) { 174e3037485SYan-Hsuan Chuang if (rssi < table[i]) { 175e3037485SYan-Hsuan Chuang new_level = i; 176e3037485SYan-Hsuan Chuang break; 177e3037485SYan-Hsuan Chuang } 178e3037485SYan-Hsuan Chuang } 179e3037485SYan-Hsuan Chuang 180e3037485SYan-Hsuan Chuang return new_level; 181e3037485SYan-Hsuan Chuang } 182e3037485SYan-Hsuan Chuang 183e3037485SYan-Hsuan Chuang struct rtw_phy_stat_iter_data { 184e3037485SYan-Hsuan Chuang struct rtw_dev *rtwdev; 185e3037485SYan-Hsuan Chuang u8 min_rssi; 186e3037485SYan-Hsuan Chuang }; 187e3037485SYan-Hsuan Chuang 188e3037485SYan-Hsuan Chuang static void rtw_phy_stat_rssi_iter(void *data, struct ieee80211_sta *sta) 189e3037485SYan-Hsuan Chuang { 190e3037485SYan-Hsuan Chuang struct rtw_phy_stat_iter_data *iter_data = data; 191e3037485SYan-Hsuan Chuang struct rtw_dev *rtwdev = iter_data->rtwdev; 192e3037485SYan-Hsuan Chuang struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv; 193a24bad74SYan-Hsuan Chuang u8 rssi; 194e3037485SYan-Hsuan Chuang 195e3037485SYan-Hsuan Chuang rssi = ewma_rssi_read(&si->avg_rssi); 196a24bad74SYan-Hsuan Chuang si->rssi_level = rtw_phy_get_rssi_level(si->rssi_level, rssi); 197e3037485SYan-Hsuan Chuang 198e3037485SYan-Hsuan Chuang rtw_fw_send_rssi_info(rtwdev, si); 199e3037485SYan-Hsuan Chuang 200e3037485SYan-Hsuan Chuang iter_data->min_rssi = min_t(u8, rssi, iter_data->min_rssi); 201e3037485SYan-Hsuan Chuang } 202e3037485SYan-Hsuan Chuang 203e3037485SYan-Hsuan Chuang static void rtw_phy_stat_rssi(struct rtw_dev *rtwdev) 204e3037485SYan-Hsuan Chuang { 205e3037485SYan-Hsuan Chuang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 206e3037485SYan-Hsuan Chuang struct rtw_phy_stat_iter_data data = {}; 207e3037485SYan-Hsuan Chuang 208e3037485SYan-Hsuan Chuang data.rtwdev = rtwdev; 209e3037485SYan-Hsuan Chuang data.min_rssi = U8_MAX; 210e3037485SYan-Hsuan Chuang rtw_iterate_stas_atomic(rtwdev, rtw_phy_stat_rssi_iter, &data); 211e3037485SYan-Hsuan Chuang 212e3037485SYan-Hsuan Chuang dm_info->pre_min_rssi = dm_info->min_rssi; 213e3037485SYan-Hsuan Chuang dm_info->min_rssi = data.min_rssi; 214e3037485SYan-Hsuan Chuang } 215e3037485SYan-Hsuan Chuang 216082a36dcSTsang-Shian Lin static void rtw_phy_stat_rate_cnt(struct rtw_dev *rtwdev) 217082a36dcSTsang-Shian Lin { 218082a36dcSTsang-Shian Lin struct rtw_dm_info *dm_info = &rtwdev->dm_info; 219082a36dcSTsang-Shian Lin 220082a36dcSTsang-Shian Lin dm_info->last_pkt_count = dm_info->cur_pkt_count; 221082a36dcSTsang-Shian Lin memset(&dm_info->cur_pkt_count, 0, sizeof(dm_info->cur_pkt_count)); 222082a36dcSTsang-Shian Lin } 223082a36dcSTsang-Shian Lin 224e3037485SYan-Hsuan Chuang static void rtw_phy_statistics(struct rtw_dev *rtwdev) 225e3037485SYan-Hsuan Chuang { 226e3037485SYan-Hsuan Chuang rtw_phy_stat_rssi(rtwdev); 227e3037485SYan-Hsuan Chuang rtw_phy_stat_false_alarm(rtwdev); 228082a36dcSTsang-Shian Lin rtw_phy_stat_rate_cnt(rtwdev); 229e3037485SYan-Hsuan Chuang } 230e3037485SYan-Hsuan Chuang 231e3037485SYan-Hsuan Chuang #define DIG_PERF_FA_TH_LOW 250 232e3037485SYan-Hsuan Chuang #define DIG_PERF_FA_TH_HIGH 500 233e3037485SYan-Hsuan Chuang #define DIG_PERF_FA_TH_EXTRA_HIGH 750 234e3037485SYan-Hsuan Chuang #define DIG_PERF_MAX 0x5a 235e3037485SYan-Hsuan Chuang #define DIG_PERF_MID 0x40 236e3037485SYan-Hsuan Chuang #define DIG_CVRG_FA_TH_LOW 2000 237e3037485SYan-Hsuan Chuang #define DIG_CVRG_FA_TH_HIGH 4000 238e3037485SYan-Hsuan Chuang #define DIG_CVRG_FA_TH_EXTRA_HIGH 5000 239e3037485SYan-Hsuan Chuang #define DIG_CVRG_MAX 0x2a 240e3037485SYan-Hsuan Chuang #define DIG_CVRG_MID 0x26 241e3037485SYan-Hsuan Chuang #define DIG_CVRG_MIN 0x1c 242e3037485SYan-Hsuan Chuang #define DIG_RSSI_GAIN_OFFSET 15 243e3037485SYan-Hsuan Chuang 244e3037485SYan-Hsuan Chuang static bool 245e3037485SYan-Hsuan Chuang rtw_phy_dig_check_damping(struct rtw_dm_info *dm_info) 246e3037485SYan-Hsuan Chuang { 247e3037485SYan-Hsuan Chuang u16 fa_lo = DIG_PERF_FA_TH_LOW; 248e3037485SYan-Hsuan Chuang u16 fa_hi = DIG_PERF_FA_TH_HIGH; 249e3037485SYan-Hsuan Chuang u16 *fa_history; 250e3037485SYan-Hsuan Chuang u8 *igi_history; 251e3037485SYan-Hsuan Chuang u8 damping_rssi; 252e3037485SYan-Hsuan Chuang u8 min_rssi; 253e3037485SYan-Hsuan Chuang u8 diff; 254e3037485SYan-Hsuan Chuang u8 igi_bitmap; 255e3037485SYan-Hsuan Chuang bool damping = false; 256e3037485SYan-Hsuan Chuang 257e3037485SYan-Hsuan Chuang min_rssi = dm_info->min_rssi; 258e3037485SYan-Hsuan Chuang if (dm_info->damping) { 259e3037485SYan-Hsuan Chuang damping_rssi = dm_info->damping_rssi; 260e3037485SYan-Hsuan Chuang diff = min_rssi > damping_rssi ? min_rssi - damping_rssi : 261e3037485SYan-Hsuan Chuang damping_rssi - min_rssi; 262e3037485SYan-Hsuan Chuang if (diff > 3 || dm_info->damping_cnt++ > 20) { 263e3037485SYan-Hsuan Chuang dm_info->damping = false; 264e3037485SYan-Hsuan Chuang return false; 265e3037485SYan-Hsuan Chuang } 266e3037485SYan-Hsuan Chuang 267e3037485SYan-Hsuan Chuang return true; 268e3037485SYan-Hsuan Chuang } 269e3037485SYan-Hsuan Chuang 270e3037485SYan-Hsuan Chuang igi_history = dm_info->igi_history; 271e3037485SYan-Hsuan Chuang fa_history = dm_info->fa_history; 272e3037485SYan-Hsuan Chuang igi_bitmap = dm_info->igi_bitmap & 0xf; 273e3037485SYan-Hsuan Chuang switch (igi_bitmap) { 274e3037485SYan-Hsuan Chuang case 5: 275e3037485SYan-Hsuan Chuang /* down -> up -> down -> up */ 276e3037485SYan-Hsuan Chuang if (igi_history[0] > igi_history[1] && 277e3037485SYan-Hsuan Chuang igi_history[2] > igi_history[3] && 278e3037485SYan-Hsuan Chuang igi_history[0] - igi_history[1] >= 2 && 279e3037485SYan-Hsuan Chuang igi_history[2] - igi_history[3] >= 2 && 280e3037485SYan-Hsuan Chuang fa_history[0] > fa_hi && fa_history[1] < fa_lo && 281e3037485SYan-Hsuan Chuang fa_history[2] > fa_hi && fa_history[3] < fa_lo) 282e3037485SYan-Hsuan Chuang damping = true; 283e3037485SYan-Hsuan Chuang break; 284e3037485SYan-Hsuan Chuang case 9: 285e3037485SYan-Hsuan Chuang /* up -> down -> down -> up */ 286e3037485SYan-Hsuan Chuang if (igi_history[0] > igi_history[1] && 287e3037485SYan-Hsuan Chuang igi_history[3] > igi_history[2] && 288e3037485SYan-Hsuan Chuang igi_history[0] - igi_history[1] >= 4 && 289e3037485SYan-Hsuan Chuang igi_history[3] - igi_history[2] >= 2 && 290e3037485SYan-Hsuan Chuang fa_history[0] > fa_hi && fa_history[1] < fa_lo && 291e3037485SYan-Hsuan Chuang fa_history[2] < fa_lo && fa_history[3] > fa_hi) 292e3037485SYan-Hsuan Chuang damping = true; 293e3037485SYan-Hsuan Chuang break; 294e3037485SYan-Hsuan Chuang default: 295e3037485SYan-Hsuan Chuang return false; 296e3037485SYan-Hsuan Chuang } 297e3037485SYan-Hsuan Chuang 298e3037485SYan-Hsuan Chuang if (damping) { 299e3037485SYan-Hsuan Chuang dm_info->damping = true; 300e3037485SYan-Hsuan Chuang dm_info->damping_cnt = 0; 301e3037485SYan-Hsuan Chuang dm_info->damping_rssi = min_rssi; 302e3037485SYan-Hsuan Chuang } 303e3037485SYan-Hsuan Chuang 304e3037485SYan-Hsuan Chuang return damping; 305e3037485SYan-Hsuan Chuang } 306e3037485SYan-Hsuan Chuang 307e3037485SYan-Hsuan Chuang static void rtw_phy_dig_get_boundary(struct rtw_dm_info *dm_info, 308e3037485SYan-Hsuan Chuang u8 *upper, u8 *lower, bool linked) 309e3037485SYan-Hsuan Chuang { 310e3037485SYan-Hsuan Chuang u8 dig_max, dig_min, dig_mid; 311e3037485SYan-Hsuan Chuang u8 min_rssi; 312e3037485SYan-Hsuan Chuang 313e3037485SYan-Hsuan Chuang if (linked) { 314e3037485SYan-Hsuan Chuang dig_max = DIG_PERF_MAX; 315e3037485SYan-Hsuan Chuang dig_mid = DIG_PERF_MID; 316e3037485SYan-Hsuan Chuang /* 22B=0x1c, 22C=0x20 */ 317e3037485SYan-Hsuan Chuang dig_min = 0x1c; 318e3037485SYan-Hsuan Chuang min_rssi = max_t(u8, dm_info->min_rssi, dig_min); 319e3037485SYan-Hsuan Chuang } else { 320e3037485SYan-Hsuan Chuang dig_max = DIG_CVRG_MAX; 321e3037485SYan-Hsuan Chuang dig_mid = DIG_CVRG_MID; 322e3037485SYan-Hsuan Chuang dig_min = DIG_CVRG_MIN; 323e3037485SYan-Hsuan Chuang min_rssi = dig_min; 324e3037485SYan-Hsuan Chuang } 325e3037485SYan-Hsuan Chuang 326e3037485SYan-Hsuan Chuang /* DIG MAX should be bounded by minimum RSSI with offset +15 */ 327e3037485SYan-Hsuan Chuang dig_max = min_t(u8, dig_max, min_rssi + DIG_RSSI_GAIN_OFFSET); 328e3037485SYan-Hsuan Chuang 329e3037485SYan-Hsuan Chuang *lower = clamp_t(u8, min_rssi, dig_min, dig_mid); 330e3037485SYan-Hsuan Chuang *upper = clamp_t(u8, *lower + DIG_RSSI_GAIN_OFFSET, dig_min, dig_max); 331e3037485SYan-Hsuan Chuang } 332e3037485SYan-Hsuan Chuang 333e3037485SYan-Hsuan Chuang static void rtw_phy_dig_get_threshold(struct rtw_dm_info *dm_info, 334e3037485SYan-Hsuan Chuang u16 *fa_th, u8 *step, bool linked) 335e3037485SYan-Hsuan Chuang { 336e3037485SYan-Hsuan Chuang u8 min_rssi, pre_min_rssi; 337e3037485SYan-Hsuan Chuang 338e3037485SYan-Hsuan Chuang min_rssi = dm_info->min_rssi; 339e3037485SYan-Hsuan Chuang pre_min_rssi = dm_info->pre_min_rssi; 340e3037485SYan-Hsuan Chuang step[0] = 4; 341e3037485SYan-Hsuan Chuang step[1] = 3; 342e3037485SYan-Hsuan Chuang step[2] = 2; 343e3037485SYan-Hsuan Chuang 344e3037485SYan-Hsuan Chuang if (linked) { 345e3037485SYan-Hsuan Chuang fa_th[0] = DIG_PERF_FA_TH_EXTRA_HIGH; 346e3037485SYan-Hsuan Chuang fa_th[1] = DIG_PERF_FA_TH_HIGH; 347e3037485SYan-Hsuan Chuang fa_th[2] = DIG_PERF_FA_TH_LOW; 348e3037485SYan-Hsuan Chuang if (pre_min_rssi > min_rssi) { 349e3037485SYan-Hsuan Chuang step[0] = 6; 350e3037485SYan-Hsuan Chuang step[1] = 4; 351e3037485SYan-Hsuan Chuang step[2] = 2; 352e3037485SYan-Hsuan Chuang } 353e3037485SYan-Hsuan Chuang } else { 354e3037485SYan-Hsuan Chuang fa_th[0] = DIG_CVRG_FA_TH_EXTRA_HIGH; 355e3037485SYan-Hsuan Chuang fa_th[1] = DIG_CVRG_FA_TH_HIGH; 356e3037485SYan-Hsuan Chuang fa_th[2] = DIG_CVRG_FA_TH_LOW; 357e3037485SYan-Hsuan Chuang } 358e3037485SYan-Hsuan Chuang } 359e3037485SYan-Hsuan Chuang 360e3037485SYan-Hsuan Chuang static void rtw_phy_dig_recorder(struct rtw_dm_info *dm_info, u8 igi, u16 fa) 361e3037485SYan-Hsuan Chuang { 362e3037485SYan-Hsuan Chuang u8 *igi_history; 363e3037485SYan-Hsuan Chuang u16 *fa_history; 364e3037485SYan-Hsuan Chuang u8 igi_bitmap; 365e3037485SYan-Hsuan Chuang bool up; 366e3037485SYan-Hsuan Chuang 367e3037485SYan-Hsuan Chuang igi_bitmap = dm_info->igi_bitmap << 1 & 0xfe; 368e3037485SYan-Hsuan Chuang igi_history = dm_info->igi_history; 369e3037485SYan-Hsuan Chuang fa_history = dm_info->fa_history; 370e3037485SYan-Hsuan Chuang 371e3037485SYan-Hsuan Chuang up = igi > igi_history[0]; 372e3037485SYan-Hsuan Chuang igi_bitmap |= up; 373e3037485SYan-Hsuan Chuang 374e3037485SYan-Hsuan Chuang igi_history[3] = igi_history[2]; 375e3037485SYan-Hsuan Chuang igi_history[2] = igi_history[1]; 376e3037485SYan-Hsuan Chuang igi_history[1] = igi_history[0]; 377e3037485SYan-Hsuan Chuang igi_history[0] = igi; 378e3037485SYan-Hsuan Chuang 379e3037485SYan-Hsuan Chuang fa_history[3] = fa_history[2]; 380e3037485SYan-Hsuan Chuang fa_history[2] = fa_history[1]; 381e3037485SYan-Hsuan Chuang fa_history[1] = fa_history[0]; 382e3037485SYan-Hsuan Chuang fa_history[0] = fa; 383e3037485SYan-Hsuan Chuang 384e3037485SYan-Hsuan Chuang dm_info->igi_bitmap = igi_bitmap; 385e3037485SYan-Hsuan Chuang } 386e3037485SYan-Hsuan Chuang 387e3037485SYan-Hsuan Chuang static void rtw_phy_dig(struct rtw_dev *rtwdev) 388e3037485SYan-Hsuan Chuang { 389e3037485SYan-Hsuan Chuang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 390e3037485SYan-Hsuan Chuang u8 upper_bound, lower_bound; 391e3037485SYan-Hsuan Chuang u8 pre_igi, cur_igi; 392e3037485SYan-Hsuan Chuang u16 fa_th[3], fa_cnt; 393e3037485SYan-Hsuan Chuang u8 level; 394e3037485SYan-Hsuan Chuang u8 step[3]; 395e3037485SYan-Hsuan Chuang bool linked; 396e3037485SYan-Hsuan Chuang 3973c519605SYan-Hsuan Chuang if (test_bit(RTW_FLAG_DIG_DISABLE, rtwdev->flags)) 398e3037485SYan-Hsuan Chuang return; 399e3037485SYan-Hsuan Chuang 400e3037485SYan-Hsuan Chuang if (rtw_phy_dig_check_damping(dm_info)) 401e3037485SYan-Hsuan Chuang return; 402e3037485SYan-Hsuan Chuang 403e3037485SYan-Hsuan Chuang linked = !!rtwdev->sta_cnt; 404e3037485SYan-Hsuan Chuang 405e3037485SYan-Hsuan Chuang fa_cnt = dm_info->total_fa_cnt; 406e3037485SYan-Hsuan Chuang pre_igi = dm_info->igi_history[0]; 407e3037485SYan-Hsuan Chuang 408e3037485SYan-Hsuan Chuang rtw_phy_dig_get_threshold(dm_info, fa_th, step, linked); 409e3037485SYan-Hsuan Chuang 410e3037485SYan-Hsuan Chuang /* test the false alarm count from the highest threshold level first, 411e3037485SYan-Hsuan Chuang * and increase it by corresponding step size 412e3037485SYan-Hsuan Chuang * 413e3037485SYan-Hsuan Chuang * note that the step size is offset by -2, compensate it afterall 414e3037485SYan-Hsuan Chuang */ 415e3037485SYan-Hsuan Chuang cur_igi = pre_igi; 416e3037485SYan-Hsuan Chuang for (level = 0; level < 3; level++) { 417e3037485SYan-Hsuan Chuang if (fa_cnt > fa_th[level]) { 418e3037485SYan-Hsuan Chuang cur_igi += step[level]; 419e3037485SYan-Hsuan Chuang break; 420e3037485SYan-Hsuan Chuang } 421e3037485SYan-Hsuan Chuang } 422e3037485SYan-Hsuan Chuang cur_igi -= 2; 423e3037485SYan-Hsuan Chuang 424e3037485SYan-Hsuan Chuang /* calculate the upper/lower bound by the minimum rssi we have among 425e3037485SYan-Hsuan Chuang * the peers connected with us, meanwhile make sure the igi value does 426e3037485SYan-Hsuan Chuang * not beyond the hardware limitation 427e3037485SYan-Hsuan Chuang */ 428e3037485SYan-Hsuan Chuang rtw_phy_dig_get_boundary(dm_info, &upper_bound, &lower_bound, linked); 429e3037485SYan-Hsuan Chuang cur_igi = clamp_t(u8, cur_igi, lower_bound, upper_bound); 430e3037485SYan-Hsuan Chuang 431e3037485SYan-Hsuan Chuang /* record current igi value and false alarm statistics for further 432e3037485SYan-Hsuan Chuang * damping checks, and record the trend of igi values 433e3037485SYan-Hsuan Chuang */ 434e3037485SYan-Hsuan Chuang rtw_phy_dig_recorder(dm_info, cur_igi, fa_cnt); 435e3037485SYan-Hsuan Chuang 436e3037485SYan-Hsuan Chuang if (cur_igi != pre_igi) 437e3037485SYan-Hsuan Chuang rtw_phy_dig_write(rtwdev, cur_igi); 438e3037485SYan-Hsuan Chuang } 439e3037485SYan-Hsuan Chuang 440e3037485SYan-Hsuan Chuang static void rtw_phy_ra_info_update_iter(void *data, struct ieee80211_sta *sta) 441e3037485SYan-Hsuan Chuang { 442e3037485SYan-Hsuan Chuang struct rtw_dev *rtwdev = data; 443e3037485SYan-Hsuan Chuang struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv; 444e3037485SYan-Hsuan Chuang 445e3037485SYan-Hsuan Chuang rtw_update_sta_info(rtwdev, si); 446e3037485SYan-Hsuan Chuang } 447e3037485SYan-Hsuan Chuang 448e3037485SYan-Hsuan Chuang static void rtw_phy_ra_info_update(struct rtw_dev *rtwdev) 449e3037485SYan-Hsuan Chuang { 450e3037485SYan-Hsuan Chuang if (rtwdev->watch_dog_cnt & 0x3) 451e3037485SYan-Hsuan Chuang return; 452e3037485SYan-Hsuan Chuang 453e3037485SYan-Hsuan Chuang rtw_iterate_stas_atomic(rtwdev, rtw_phy_ra_info_update_iter, rtwdev); 454e3037485SYan-Hsuan Chuang } 455e3037485SYan-Hsuan Chuang 4565227c2eeSTzu-En Huang static void rtw_phy_dpk_track(struct rtw_dev *rtwdev) 4575227c2eeSTzu-En Huang { 4585227c2eeSTzu-En Huang struct rtw_chip_info *chip = rtwdev->chip; 4595227c2eeSTzu-En Huang 4605227c2eeSTzu-En Huang if (chip->ops->dpk_track) 4615227c2eeSTzu-En Huang chip->ops->dpk_track(rtwdev); 4625227c2eeSTzu-En Huang } 4635227c2eeSTzu-En Huang 464479c4ee9STzu-En Huang #define CCK_PD_FA_LV1_MIN 1000 465479c4ee9STzu-En Huang #define CCK_PD_FA_LV0_MAX 500 466479c4ee9STzu-En Huang 467479c4ee9STzu-En Huang static u8 rtw_phy_cck_pd_lv_unlink(struct rtw_dev *rtwdev) 468479c4ee9STzu-En Huang { 469479c4ee9STzu-En Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 470479c4ee9STzu-En Huang u32 cck_fa_avg = dm_info->cck_fa_avg; 471479c4ee9STzu-En Huang 472479c4ee9STzu-En Huang if (cck_fa_avg > CCK_PD_FA_LV1_MIN) 47318a0696eSTzu-En Huang return CCK_PD_LV1; 474479c4ee9STzu-En Huang 475479c4ee9STzu-En Huang if (cck_fa_avg < CCK_PD_FA_LV0_MAX) 47618a0696eSTzu-En Huang return CCK_PD_LV0; 477479c4ee9STzu-En Huang 478479c4ee9STzu-En Huang return CCK_PD_LV_MAX; 479479c4ee9STzu-En Huang } 480479c4ee9STzu-En Huang 481479c4ee9STzu-En Huang #define CCK_PD_IGI_LV4_VAL 0x38 482479c4ee9STzu-En Huang #define CCK_PD_IGI_LV3_VAL 0x2a 483479c4ee9STzu-En Huang #define CCK_PD_IGI_LV2_VAL 0x24 484479c4ee9STzu-En Huang #define CCK_PD_RSSI_LV4_VAL 32 485479c4ee9STzu-En Huang #define CCK_PD_RSSI_LV3_VAL 32 486479c4ee9STzu-En Huang #define CCK_PD_RSSI_LV2_VAL 24 487479c4ee9STzu-En Huang 488479c4ee9STzu-En Huang static u8 rtw_phy_cck_pd_lv_link(struct rtw_dev *rtwdev) 489479c4ee9STzu-En Huang { 490479c4ee9STzu-En Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 491479c4ee9STzu-En Huang u8 igi = dm_info->igi_history[0]; 492479c4ee9STzu-En Huang u8 rssi = dm_info->min_rssi; 493479c4ee9STzu-En Huang u32 cck_fa_avg = dm_info->cck_fa_avg; 494479c4ee9STzu-En Huang 495479c4ee9STzu-En Huang if (igi > CCK_PD_IGI_LV4_VAL && rssi > CCK_PD_RSSI_LV4_VAL) 49618a0696eSTzu-En Huang return CCK_PD_LV4; 497479c4ee9STzu-En Huang if (igi > CCK_PD_IGI_LV3_VAL && rssi > CCK_PD_RSSI_LV3_VAL) 49818a0696eSTzu-En Huang return CCK_PD_LV3; 499479c4ee9STzu-En Huang if (igi > CCK_PD_IGI_LV2_VAL || rssi > CCK_PD_RSSI_LV2_VAL) 50018a0696eSTzu-En Huang return CCK_PD_LV2; 501479c4ee9STzu-En Huang if (cck_fa_avg > CCK_PD_FA_LV1_MIN) 50218a0696eSTzu-En Huang return CCK_PD_LV1; 503479c4ee9STzu-En Huang if (cck_fa_avg < CCK_PD_FA_LV0_MAX) 50418a0696eSTzu-En Huang return CCK_PD_LV0; 505479c4ee9STzu-En Huang 506479c4ee9STzu-En Huang return CCK_PD_LV_MAX; 507479c4ee9STzu-En Huang } 508479c4ee9STzu-En Huang 509479c4ee9STzu-En Huang static u8 rtw_phy_cck_pd_lv(struct rtw_dev *rtwdev) 510479c4ee9STzu-En Huang { 511479c4ee9STzu-En Huang if (!rtw_is_assoc(rtwdev)) 512479c4ee9STzu-En Huang return rtw_phy_cck_pd_lv_unlink(rtwdev); 513479c4ee9STzu-En Huang else 514479c4ee9STzu-En Huang return rtw_phy_cck_pd_lv_link(rtwdev); 515479c4ee9STzu-En Huang } 516479c4ee9STzu-En Huang 517479c4ee9STzu-En Huang static void rtw_phy_cck_pd(struct rtw_dev *rtwdev) 518479c4ee9STzu-En Huang { 519479c4ee9STzu-En Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 520479c4ee9STzu-En Huang struct rtw_chip_info *chip = rtwdev->chip; 521479c4ee9STzu-En Huang u32 cck_fa = dm_info->cck_fa_cnt; 522479c4ee9STzu-En Huang u8 level; 523479c4ee9STzu-En Huang 524479c4ee9STzu-En Huang if (rtwdev->hal.current_band_type != RTW_BAND_2G) 525479c4ee9STzu-En Huang return; 526479c4ee9STzu-En Huang 527479c4ee9STzu-En Huang if (dm_info->cck_fa_avg == CCK_FA_AVG_RESET) 528479c4ee9STzu-En Huang dm_info->cck_fa_avg = cck_fa; 529479c4ee9STzu-En Huang else 530479c4ee9STzu-En Huang dm_info->cck_fa_avg = (dm_info->cck_fa_avg * 3 + cck_fa) >> 2; 531479c4ee9STzu-En Huang 532479c4ee9STzu-En Huang level = rtw_phy_cck_pd_lv(rtwdev); 533479c4ee9STzu-En Huang 534479c4ee9STzu-En Huang if (level >= CCK_PD_LV_MAX) 535479c4ee9STzu-En Huang return; 536479c4ee9STzu-En Huang 537479c4ee9STzu-En Huang if (chip->ops->cck_pd_set) 538479c4ee9STzu-En Huang chip->ops->cck_pd_set(rtwdev, level); 539479c4ee9STzu-En Huang } 540479c4ee9STzu-En Huang 541c97ee3e0STzu-En Huang static void rtw_phy_pwr_track(struct rtw_dev *rtwdev) 542c97ee3e0STzu-En Huang { 543c97ee3e0STzu-En Huang rtwdev->chip->ops->pwr_track(rtwdev); 544c97ee3e0STzu-En Huang } 545c97ee3e0STzu-En Huang 546e3037485SYan-Hsuan Chuang void rtw_phy_dynamic_mechanism(struct rtw_dev *rtwdev) 547e3037485SYan-Hsuan Chuang { 548e3037485SYan-Hsuan Chuang /* for further calculation */ 549e3037485SYan-Hsuan Chuang rtw_phy_statistics(rtwdev); 550e3037485SYan-Hsuan Chuang rtw_phy_dig(rtwdev); 551479c4ee9STzu-En Huang rtw_phy_cck_pd(rtwdev); 552e3037485SYan-Hsuan Chuang rtw_phy_ra_info_update(rtwdev); 5535227c2eeSTzu-En Huang rtw_phy_dpk_track(rtwdev); 554c97ee3e0STzu-En Huang rtw_phy_pwr_track(rtwdev); 555e3037485SYan-Hsuan Chuang } 556e3037485SYan-Hsuan Chuang 557e3037485SYan-Hsuan Chuang #define FRAC_BITS 3 558e3037485SYan-Hsuan Chuang 559e3037485SYan-Hsuan Chuang static u8 rtw_phy_power_2_db(s8 power) 560e3037485SYan-Hsuan Chuang { 561e3037485SYan-Hsuan Chuang if (power <= -100 || power >= 20) 562e3037485SYan-Hsuan Chuang return 0; 563e3037485SYan-Hsuan Chuang else if (power >= 0) 564e3037485SYan-Hsuan Chuang return 100; 565e3037485SYan-Hsuan Chuang else 566e3037485SYan-Hsuan Chuang return 100 + power; 567e3037485SYan-Hsuan Chuang } 568e3037485SYan-Hsuan Chuang 569e3037485SYan-Hsuan Chuang static u64 rtw_phy_db_2_linear(u8 power_db) 570e3037485SYan-Hsuan Chuang { 571e3037485SYan-Hsuan Chuang u8 i, j; 572e3037485SYan-Hsuan Chuang u64 linear; 573e3037485SYan-Hsuan Chuang 5748a03447dSStanislaw Gruszka if (power_db > 96) 5758a03447dSStanislaw Gruszka power_db = 96; 5768a03447dSStanislaw Gruszka else if (power_db < 1) 5778a03447dSStanislaw Gruszka return 1; 5788a03447dSStanislaw Gruszka 579e3037485SYan-Hsuan Chuang /* 1dB ~ 96dB */ 580e3037485SYan-Hsuan Chuang i = (power_db - 1) >> 3; 581e3037485SYan-Hsuan Chuang j = (power_db - 1) - (i << 3); 582e3037485SYan-Hsuan Chuang 583e3037485SYan-Hsuan Chuang linear = db_invert_table[i][j]; 584e3037485SYan-Hsuan Chuang linear = i > 2 ? linear << FRAC_BITS : linear; 585e3037485SYan-Hsuan Chuang 586e3037485SYan-Hsuan Chuang return linear; 587e3037485SYan-Hsuan Chuang } 588e3037485SYan-Hsuan Chuang 589e3037485SYan-Hsuan Chuang static u8 rtw_phy_linear_2_db(u64 linear) 590e3037485SYan-Hsuan Chuang { 591e3037485SYan-Hsuan Chuang u8 i; 592e3037485SYan-Hsuan Chuang u8 j; 593e3037485SYan-Hsuan Chuang u32 dB; 594e3037485SYan-Hsuan Chuang 595e3037485SYan-Hsuan Chuang if (linear >= db_invert_table[11][7]) 596e3037485SYan-Hsuan Chuang return 96; /* maximum 96 dB */ 597e3037485SYan-Hsuan Chuang 598e3037485SYan-Hsuan Chuang for (i = 0; i < 12; i++) { 599e3037485SYan-Hsuan Chuang if (i <= 2 && (linear << FRAC_BITS) <= db_invert_table[i][7]) 600e3037485SYan-Hsuan Chuang break; 601e3037485SYan-Hsuan Chuang else if (i > 2 && linear <= db_invert_table[i][7]) 602e3037485SYan-Hsuan Chuang break; 603e3037485SYan-Hsuan Chuang } 604e3037485SYan-Hsuan Chuang 605e3037485SYan-Hsuan Chuang for (j = 0; j < 8; j++) { 606e3037485SYan-Hsuan Chuang if (i <= 2 && (linear << FRAC_BITS) <= db_invert_table[i][j]) 607e3037485SYan-Hsuan Chuang break; 608e3037485SYan-Hsuan Chuang else if (i > 2 && linear <= db_invert_table[i][j]) 609e3037485SYan-Hsuan Chuang break; 610e3037485SYan-Hsuan Chuang } 611e3037485SYan-Hsuan Chuang 612e3037485SYan-Hsuan Chuang if (j == 0 && i == 0) 613e3037485SYan-Hsuan Chuang goto end; 614e3037485SYan-Hsuan Chuang 615e3037485SYan-Hsuan Chuang if (j == 0) { 616e3037485SYan-Hsuan Chuang if (i != 3) { 617e3037485SYan-Hsuan Chuang if (db_invert_table[i][0] - linear > 618e3037485SYan-Hsuan Chuang linear - db_invert_table[i - 1][7]) { 619e3037485SYan-Hsuan Chuang i = i - 1; 620e3037485SYan-Hsuan Chuang j = 7; 621e3037485SYan-Hsuan Chuang } 622e3037485SYan-Hsuan Chuang } else { 623e3037485SYan-Hsuan Chuang if (db_invert_table[3][0] - linear > 624e3037485SYan-Hsuan Chuang linear - db_invert_table[2][7]) { 625e3037485SYan-Hsuan Chuang i = 2; 626e3037485SYan-Hsuan Chuang j = 7; 627e3037485SYan-Hsuan Chuang } 628e3037485SYan-Hsuan Chuang } 629e3037485SYan-Hsuan Chuang } else { 630e3037485SYan-Hsuan Chuang if (db_invert_table[i][j] - linear > 631e3037485SYan-Hsuan Chuang linear - db_invert_table[i][j - 1]) { 632e3037485SYan-Hsuan Chuang j = j - 1; 633e3037485SYan-Hsuan Chuang } 634e3037485SYan-Hsuan Chuang } 635e3037485SYan-Hsuan Chuang end: 636e3037485SYan-Hsuan Chuang dB = (i << 3) + j + 1; 637e3037485SYan-Hsuan Chuang 638e3037485SYan-Hsuan Chuang return dB; 639e3037485SYan-Hsuan Chuang } 640e3037485SYan-Hsuan Chuang 641e3037485SYan-Hsuan Chuang u8 rtw_phy_rf_power_2_rssi(s8 *rf_power, u8 path_num) 642e3037485SYan-Hsuan Chuang { 643e3037485SYan-Hsuan Chuang s8 power; 644e3037485SYan-Hsuan Chuang u8 power_db; 645e3037485SYan-Hsuan Chuang u64 linear; 646e3037485SYan-Hsuan Chuang u64 sum = 0; 647e3037485SYan-Hsuan Chuang u8 path; 648e3037485SYan-Hsuan Chuang 649e3037485SYan-Hsuan Chuang for (path = 0; path < path_num; path++) { 650e3037485SYan-Hsuan Chuang power = rf_power[path]; 651e3037485SYan-Hsuan Chuang power_db = rtw_phy_power_2_db(power); 652e3037485SYan-Hsuan Chuang linear = rtw_phy_db_2_linear(power_db); 653e3037485SYan-Hsuan Chuang sum += linear; 654e3037485SYan-Hsuan Chuang } 655e3037485SYan-Hsuan Chuang 656e3037485SYan-Hsuan Chuang sum = (sum + (1 << (FRAC_BITS - 1))) >> FRAC_BITS; 657e3037485SYan-Hsuan Chuang switch (path_num) { 658e3037485SYan-Hsuan Chuang case 2: 659e3037485SYan-Hsuan Chuang sum >>= 1; 660e3037485SYan-Hsuan Chuang break; 661e3037485SYan-Hsuan Chuang case 3: 662e3037485SYan-Hsuan Chuang sum = ((sum) + ((sum) << 1) + ((sum) << 3)) >> 5; 663e3037485SYan-Hsuan Chuang break; 664e3037485SYan-Hsuan Chuang case 4: 665e3037485SYan-Hsuan Chuang sum >>= 2; 666e3037485SYan-Hsuan Chuang break; 667e3037485SYan-Hsuan Chuang default: 668e3037485SYan-Hsuan Chuang break; 669e3037485SYan-Hsuan Chuang } 670e3037485SYan-Hsuan Chuang 671e3037485SYan-Hsuan Chuang return rtw_phy_linear_2_db(sum); 672e3037485SYan-Hsuan Chuang } 673e3037485SYan-Hsuan Chuang 674e3037485SYan-Hsuan Chuang u32 rtw_phy_read_rf(struct rtw_dev *rtwdev, enum rtw_rf_path rf_path, 675e3037485SYan-Hsuan Chuang u32 addr, u32 mask) 676e3037485SYan-Hsuan Chuang { 677e3037485SYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 678e3037485SYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 679e3037485SYan-Hsuan Chuang const u32 *base_addr = chip->rf_base_addr; 680e3037485SYan-Hsuan Chuang u32 val, direct_addr; 681e3037485SYan-Hsuan Chuang 682e3037485SYan-Hsuan Chuang if (rf_path >= hal->rf_path_num) { 683e3037485SYan-Hsuan Chuang rtw_err(rtwdev, "unsupported rf path (%d)\n", rf_path); 684e3037485SYan-Hsuan Chuang return INV_RF_DATA; 685e3037485SYan-Hsuan Chuang } 686e3037485SYan-Hsuan Chuang 687e3037485SYan-Hsuan Chuang addr &= 0xff; 688e3037485SYan-Hsuan Chuang direct_addr = base_addr[rf_path] + (addr << 2); 689e3037485SYan-Hsuan Chuang mask &= RFREG_MASK; 690e3037485SYan-Hsuan Chuang 691e3037485SYan-Hsuan Chuang val = rtw_read32_mask(rtwdev, direct_addr, mask); 692e3037485SYan-Hsuan Chuang 693e3037485SYan-Hsuan Chuang return val; 694e3037485SYan-Hsuan Chuang } 695e3037485SYan-Hsuan Chuang 696e3037485SYan-Hsuan Chuang bool rtw_phy_write_rf_reg_sipi(struct rtw_dev *rtwdev, enum rtw_rf_path rf_path, 697e3037485SYan-Hsuan Chuang u32 addr, u32 mask, u32 data) 698e3037485SYan-Hsuan Chuang { 699e3037485SYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 700e3037485SYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 701e3037485SYan-Hsuan Chuang u32 *sipi_addr = chip->rf_sipi_addr; 702e3037485SYan-Hsuan Chuang u32 data_and_addr; 703e3037485SYan-Hsuan Chuang u32 old_data = 0; 704e3037485SYan-Hsuan Chuang u32 shift; 705e3037485SYan-Hsuan Chuang 706e3037485SYan-Hsuan Chuang if (rf_path >= hal->rf_path_num) { 707e3037485SYan-Hsuan Chuang rtw_err(rtwdev, "unsupported rf path (%d)\n", rf_path); 708e3037485SYan-Hsuan Chuang return false; 709e3037485SYan-Hsuan Chuang } 710e3037485SYan-Hsuan Chuang 711e3037485SYan-Hsuan Chuang addr &= 0xff; 712e3037485SYan-Hsuan Chuang mask &= RFREG_MASK; 713e3037485SYan-Hsuan Chuang 714e3037485SYan-Hsuan Chuang if (mask != RFREG_MASK) { 715e3037485SYan-Hsuan Chuang old_data = rtw_phy_read_rf(rtwdev, rf_path, addr, RFREG_MASK); 716e3037485SYan-Hsuan Chuang 717e3037485SYan-Hsuan Chuang if (old_data == INV_RF_DATA) { 718e3037485SYan-Hsuan Chuang rtw_err(rtwdev, "Write fail, rf is disabled\n"); 719e3037485SYan-Hsuan Chuang return false; 720e3037485SYan-Hsuan Chuang } 721e3037485SYan-Hsuan Chuang 722e3037485SYan-Hsuan Chuang shift = __ffs(mask); 723e3037485SYan-Hsuan Chuang data = ((old_data) & (~mask)) | (data << shift); 724e3037485SYan-Hsuan Chuang } 725e3037485SYan-Hsuan Chuang 726e3037485SYan-Hsuan Chuang data_and_addr = ((addr << 20) | (data & 0x000fffff)) & 0x0fffffff; 727e3037485SYan-Hsuan Chuang 728e3037485SYan-Hsuan Chuang rtw_write32(rtwdev, sipi_addr[rf_path], data_and_addr); 729e3037485SYan-Hsuan Chuang 730e3037485SYan-Hsuan Chuang udelay(13); 731e3037485SYan-Hsuan Chuang 732e3037485SYan-Hsuan Chuang return true; 733e3037485SYan-Hsuan Chuang } 734e3037485SYan-Hsuan Chuang 735e3037485SYan-Hsuan Chuang bool rtw_phy_write_rf_reg(struct rtw_dev *rtwdev, enum rtw_rf_path rf_path, 736e3037485SYan-Hsuan Chuang u32 addr, u32 mask, u32 data) 737e3037485SYan-Hsuan Chuang { 738e3037485SYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 739e3037485SYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 740e3037485SYan-Hsuan Chuang const u32 *base_addr = chip->rf_base_addr; 741e3037485SYan-Hsuan Chuang u32 direct_addr; 742e3037485SYan-Hsuan Chuang 743e3037485SYan-Hsuan Chuang if (rf_path >= hal->rf_path_num) { 744e3037485SYan-Hsuan Chuang rtw_err(rtwdev, "unsupported rf path (%d)\n", rf_path); 745e3037485SYan-Hsuan Chuang return false; 746e3037485SYan-Hsuan Chuang } 747e3037485SYan-Hsuan Chuang 748e3037485SYan-Hsuan Chuang addr &= 0xff; 749e3037485SYan-Hsuan Chuang direct_addr = base_addr[rf_path] + (addr << 2); 750e3037485SYan-Hsuan Chuang mask &= RFREG_MASK; 751e3037485SYan-Hsuan Chuang 752818d46e7SChien-Hsun Liao if (addr == RF_CFGCH) { 753e3037485SYan-Hsuan Chuang rtw_write32_mask(rtwdev, REG_RSV_CTRL, BITS_RFC_DIRECT, DISABLE_PI); 754e3037485SYan-Hsuan Chuang rtw_write32_mask(rtwdev, REG_WLRF1, BITS_RFC_DIRECT, DISABLE_PI); 755818d46e7SChien-Hsun Liao } 756818d46e7SChien-Hsun Liao 757e3037485SYan-Hsuan Chuang rtw_write32_mask(rtwdev, direct_addr, mask, data); 758e3037485SYan-Hsuan Chuang 759e3037485SYan-Hsuan Chuang udelay(1); 760e3037485SYan-Hsuan Chuang 761818d46e7SChien-Hsun Liao if (addr == RF_CFGCH) { 762e3037485SYan-Hsuan Chuang rtw_write32_mask(rtwdev, REG_RSV_CTRL, BITS_RFC_DIRECT, ENABLE_PI); 763e3037485SYan-Hsuan Chuang rtw_write32_mask(rtwdev, REG_WLRF1, BITS_RFC_DIRECT, ENABLE_PI); 764818d46e7SChien-Hsun Liao } 765e3037485SYan-Hsuan Chuang 766e3037485SYan-Hsuan Chuang return true; 767e3037485SYan-Hsuan Chuang } 768e3037485SYan-Hsuan Chuang 769e3037485SYan-Hsuan Chuang bool rtw_phy_write_rf_reg_mix(struct rtw_dev *rtwdev, enum rtw_rf_path rf_path, 770e3037485SYan-Hsuan Chuang u32 addr, u32 mask, u32 data) 771e3037485SYan-Hsuan Chuang { 772e3037485SYan-Hsuan Chuang if (addr != 0x00) 773e3037485SYan-Hsuan Chuang return rtw_phy_write_rf_reg(rtwdev, rf_path, addr, mask, data); 774e3037485SYan-Hsuan Chuang 775e3037485SYan-Hsuan Chuang return rtw_phy_write_rf_reg_sipi(rtwdev, rf_path, addr, mask, data); 776e3037485SYan-Hsuan Chuang } 777e3037485SYan-Hsuan Chuang 778e3037485SYan-Hsuan Chuang void rtw_phy_setup_phy_cond(struct rtw_dev *rtwdev, u32 pkg) 779e3037485SYan-Hsuan Chuang { 780e3037485SYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 781e3037485SYan-Hsuan Chuang struct rtw_efuse *efuse = &rtwdev->efuse; 782e3037485SYan-Hsuan Chuang struct rtw_phy_cond cond = {0}; 783e3037485SYan-Hsuan Chuang 784e3037485SYan-Hsuan Chuang cond.cut = hal->cut_version ? hal->cut_version : 15; 785e3037485SYan-Hsuan Chuang cond.pkg = pkg ? pkg : 15; 786e3037485SYan-Hsuan Chuang cond.plat = 0x04; 787e3037485SYan-Hsuan Chuang cond.rfe = efuse->rfe_option; 788e3037485SYan-Hsuan Chuang 789e3037485SYan-Hsuan Chuang switch (rtw_hci_type(rtwdev)) { 790e3037485SYan-Hsuan Chuang case RTW_HCI_TYPE_USB: 791e3037485SYan-Hsuan Chuang cond.intf = INTF_USB; 792e3037485SYan-Hsuan Chuang break; 793e3037485SYan-Hsuan Chuang case RTW_HCI_TYPE_SDIO: 794e3037485SYan-Hsuan Chuang cond.intf = INTF_SDIO; 795e3037485SYan-Hsuan Chuang break; 796e3037485SYan-Hsuan Chuang case RTW_HCI_TYPE_PCIE: 797e3037485SYan-Hsuan Chuang default: 798e3037485SYan-Hsuan Chuang cond.intf = INTF_PCIE; 799e3037485SYan-Hsuan Chuang break; 800e3037485SYan-Hsuan Chuang } 801e3037485SYan-Hsuan Chuang 802e3037485SYan-Hsuan Chuang hal->phy_cond = cond; 803e3037485SYan-Hsuan Chuang 804e3037485SYan-Hsuan Chuang rtw_dbg(rtwdev, RTW_DBG_PHY, "phy cond=0x%08x\n", *((u32 *)&hal->phy_cond)); 805e3037485SYan-Hsuan Chuang } 806e3037485SYan-Hsuan Chuang 807e3037485SYan-Hsuan Chuang static bool check_positive(struct rtw_dev *rtwdev, struct rtw_phy_cond cond) 808e3037485SYan-Hsuan Chuang { 809e3037485SYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 810e3037485SYan-Hsuan Chuang struct rtw_phy_cond drv_cond = hal->phy_cond; 811e3037485SYan-Hsuan Chuang 812e3037485SYan-Hsuan Chuang if (cond.cut && cond.cut != drv_cond.cut) 813e3037485SYan-Hsuan Chuang return false; 814e3037485SYan-Hsuan Chuang 815e3037485SYan-Hsuan Chuang if (cond.pkg && cond.pkg != drv_cond.pkg) 816e3037485SYan-Hsuan Chuang return false; 817e3037485SYan-Hsuan Chuang 818e3037485SYan-Hsuan Chuang if (cond.intf && cond.intf != drv_cond.intf) 819e3037485SYan-Hsuan Chuang return false; 820e3037485SYan-Hsuan Chuang 821e3037485SYan-Hsuan Chuang if (cond.rfe != drv_cond.rfe) 822e3037485SYan-Hsuan Chuang return false; 823e3037485SYan-Hsuan Chuang 824e3037485SYan-Hsuan Chuang return true; 825e3037485SYan-Hsuan Chuang } 826e3037485SYan-Hsuan Chuang 827e3037485SYan-Hsuan Chuang void rtw_parse_tbl_phy_cond(struct rtw_dev *rtwdev, const struct rtw_table *tbl) 828e3037485SYan-Hsuan Chuang { 829e3037485SYan-Hsuan Chuang const union phy_table_tile *p = tbl->data; 830e3037485SYan-Hsuan Chuang const union phy_table_tile *end = p + tbl->size / 2; 831e3037485SYan-Hsuan Chuang struct rtw_phy_cond pos_cond = {0}; 832e3037485SYan-Hsuan Chuang bool is_matched = true, is_skipped = false; 833e3037485SYan-Hsuan Chuang 834e3037485SYan-Hsuan Chuang BUILD_BUG_ON(sizeof(union phy_table_tile) != sizeof(struct phy_cfg_pair)); 835e3037485SYan-Hsuan Chuang 836e3037485SYan-Hsuan Chuang for (; p < end; p++) { 837e3037485SYan-Hsuan Chuang if (p->cond.pos) { 838e3037485SYan-Hsuan Chuang switch (p->cond.branch) { 839e3037485SYan-Hsuan Chuang case BRANCH_ENDIF: 840e3037485SYan-Hsuan Chuang is_matched = true; 841e3037485SYan-Hsuan Chuang is_skipped = false; 842e3037485SYan-Hsuan Chuang break; 843e3037485SYan-Hsuan Chuang case BRANCH_ELSE: 844e3037485SYan-Hsuan Chuang is_matched = is_skipped ? false : true; 845e3037485SYan-Hsuan Chuang break; 846e3037485SYan-Hsuan Chuang case BRANCH_IF: 847e3037485SYan-Hsuan Chuang case BRANCH_ELIF: 848e3037485SYan-Hsuan Chuang default: 849e3037485SYan-Hsuan Chuang pos_cond = p->cond; 850e3037485SYan-Hsuan Chuang break; 851e3037485SYan-Hsuan Chuang } 852e3037485SYan-Hsuan Chuang } else if (p->cond.neg) { 853e3037485SYan-Hsuan Chuang if (!is_skipped) { 854e3037485SYan-Hsuan Chuang if (check_positive(rtwdev, pos_cond)) { 855e3037485SYan-Hsuan Chuang is_matched = true; 856e3037485SYan-Hsuan Chuang is_skipped = true; 857e3037485SYan-Hsuan Chuang } else { 858e3037485SYan-Hsuan Chuang is_matched = false; 859e3037485SYan-Hsuan Chuang is_skipped = false; 860e3037485SYan-Hsuan Chuang } 861e3037485SYan-Hsuan Chuang } else { 862e3037485SYan-Hsuan Chuang is_matched = false; 863e3037485SYan-Hsuan Chuang } 864e3037485SYan-Hsuan Chuang } else if (is_matched) { 865e3037485SYan-Hsuan Chuang (*tbl->do_cfg)(rtwdev, tbl, p->cfg.addr, p->cfg.data); 866e3037485SYan-Hsuan Chuang } 867e3037485SYan-Hsuan Chuang } 868e3037485SYan-Hsuan Chuang } 869e3037485SYan-Hsuan Chuang 870e3037485SYan-Hsuan Chuang #define bcd_to_dec_pwr_by_rate(val, i) bcd2bin(val >> (i * 8)) 871e3037485SYan-Hsuan Chuang 872e3037485SYan-Hsuan Chuang static u8 tbl_to_dec_pwr_by_rate(struct rtw_dev *rtwdev, u32 hex, u8 i) 873e3037485SYan-Hsuan Chuang { 874e3037485SYan-Hsuan Chuang if (rtwdev->chip->is_pwr_by_rate_dec) 875e3037485SYan-Hsuan Chuang return bcd_to_dec_pwr_by_rate(hex, i); 876fa6dfe6bSYan-Hsuan Chuang 877e3037485SYan-Hsuan Chuang return (hex >> (i * 8)) & 0xFF; 878e3037485SYan-Hsuan Chuang } 879e3037485SYan-Hsuan Chuang 88043712199SYan-Hsuan Chuang static void 88143712199SYan-Hsuan Chuang rtw_phy_get_rate_values_of_txpwr_by_rate(struct rtw_dev *rtwdev, 88243712199SYan-Hsuan Chuang u32 addr, u32 mask, u32 val, u8 *rate, 883e3037485SYan-Hsuan Chuang u8 *pwr_by_rate, u8 *rate_num) 884e3037485SYan-Hsuan Chuang { 885e3037485SYan-Hsuan Chuang int i; 886e3037485SYan-Hsuan Chuang 887e3037485SYan-Hsuan Chuang switch (addr) { 888e3037485SYan-Hsuan Chuang case 0xE00: 889e3037485SYan-Hsuan Chuang case 0x830: 890e3037485SYan-Hsuan Chuang rate[0] = DESC_RATE6M; 891e3037485SYan-Hsuan Chuang rate[1] = DESC_RATE9M; 892e3037485SYan-Hsuan Chuang rate[2] = DESC_RATE12M; 893e3037485SYan-Hsuan Chuang rate[3] = DESC_RATE18M; 894e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 895e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 896e3037485SYan-Hsuan Chuang *rate_num = 4; 897e3037485SYan-Hsuan Chuang break; 898e3037485SYan-Hsuan Chuang case 0xE04: 899e3037485SYan-Hsuan Chuang case 0x834: 900e3037485SYan-Hsuan Chuang rate[0] = DESC_RATE24M; 901e3037485SYan-Hsuan Chuang rate[1] = DESC_RATE36M; 902e3037485SYan-Hsuan Chuang rate[2] = DESC_RATE48M; 903e3037485SYan-Hsuan Chuang rate[3] = DESC_RATE54M; 904e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 905e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 906e3037485SYan-Hsuan Chuang *rate_num = 4; 907e3037485SYan-Hsuan Chuang break; 908e3037485SYan-Hsuan Chuang case 0xE08: 909e3037485SYan-Hsuan Chuang rate[0] = DESC_RATE1M; 910e3037485SYan-Hsuan Chuang pwr_by_rate[0] = bcd_to_dec_pwr_by_rate(val, 1); 911e3037485SYan-Hsuan Chuang *rate_num = 1; 912e3037485SYan-Hsuan Chuang break; 913e3037485SYan-Hsuan Chuang case 0x86C: 914e3037485SYan-Hsuan Chuang if (mask == 0xffffff00) { 915e3037485SYan-Hsuan Chuang rate[0] = DESC_RATE2M; 916e3037485SYan-Hsuan Chuang rate[1] = DESC_RATE5_5M; 917e3037485SYan-Hsuan Chuang rate[2] = DESC_RATE11M; 918e3037485SYan-Hsuan Chuang for (i = 1; i < 4; ++i) 919e3037485SYan-Hsuan Chuang pwr_by_rate[i - 1] = 920e3037485SYan-Hsuan Chuang tbl_to_dec_pwr_by_rate(rtwdev, val, i); 921e3037485SYan-Hsuan Chuang *rate_num = 3; 922e3037485SYan-Hsuan Chuang } else if (mask == 0x000000ff) { 923e3037485SYan-Hsuan Chuang rate[0] = DESC_RATE11M; 924e3037485SYan-Hsuan Chuang pwr_by_rate[0] = bcd_to_dec_pwr_by_rate(val, 0); 925e3037485SYan-Hsuan Chuang *rate_num = 1; 926e3037485SYan-Hsuan Chuang } 927e3037485SYan-Hsuan Chuang break; 928e3037485SYan-Hsuan Chuang case 0xE10: 929e3037485SYan-Hsuan Chuang case 0x83C: 930e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS0; 931e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS1; 932e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS2; 933e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS3; 934e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 935e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 936e3037485SYan-Hsuan Chuang *rate_num = 4; 937e3037485SYan-Hsuan Chuang break; 938e3037485SYan-Hsuan Chuang case 0xE14: 939e3037485SYan-Hsuan Chuang case 0x848: 940e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS4; 941e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS5; 942e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS6; 943e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS7; 944e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 945e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 946e3037485SYan-Hsuan Chuang *rate_num = 4; 947e3037485SYan-Hsuan Chuang break; 948e3037485SYan-Hsuan Chuang case 0xE18: 949e3037485SYan-Hsuan Chuang case 0x84C: 950e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS8; 951e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS9; 952e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS10; 953e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS11; 954e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 955e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 956e3037485SYan-Hsuan Chuang *rate_num = 4; 957e3037485SYan-Hsuan Chuang break; 958e3037485SYan-Hsuan Chuang case 0xE1C: 959e3037485SYan-Hsuan Chuang case 0x868: 960e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS12; 961e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS13; 962e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS14; 963e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS15; 964e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 965e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 966e3037485SYan-Hsuan Chuang *rate_num = 4; 967e3037485SYan-Hsuan Chuang break; 968e3037485SYan-Hsuan Chuang case 0x838: 969e3037485SYan-Hsuan Chuang rate[0] = DESC_RATE1M; 970e3037485SYan-Hsuan Chuang rate[1] = DESC_RATE2M; 971e3037485SYan-Hsuan Chuang rate[2] = DESC_RATE5_5M; 972e3037485SYan-Hsuan Chuang for (i = 1; i < 4; ++i) 973e3037485SYan-Hsuan Chuang pwr_by_rate[i - 1] = tbl_to_dec_pwr_by_rate(rtwdev, 974e3037485SYan-Hsuan Chuang val, i); 975e3037485SYan-Hsuan Chuang *rate_num = 3; 976e3037485SYan-Hsuan Chuang break; 977e3037485SYan-Hsuan Chuang case 0xC20: 978e3037485SYan-Hsuan Chuang case 0xE20: 979e3037485SYan-Hsuan Chuang case 0x1820: 980e3037485SYan-Hsuan Chuang case 0x1A20: 981e3037485SYan-Hsuan Chuang rate[0] = DESC_RATE1M; 982e3037485SYan-Hsuan Chuang rate[1] = DESC_RATE2M; 983e3037485SYan-Hsuan Chuang rate[2] = DESC_RATE5_5M; 984e3037485SYan-Hsuan Chuang rate[3] = DESC_RATE11M; 985e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 986e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 987e3037485SYan-Hsuan Chuang *rate_num = 4; 988e3037485SYan-Hsuan Chuang break; 989e3037485SYan-Hsuan Chuang case 0xC24: 990e3037485SYan-Hsuan Chuang case 0xE24: 991e3037485SYan-Hsuan Chuang case 0x1824: 992e3037485SYan-Hsuan Chuang case 0x1A24: 993e3037485SYan-Hsuan Chuang rate[0] = DESC_RATE6M; 994e3037485SYan-Hsuan Chuang rate[1] = DESC_RATE9M; 995e3037485SYan-Hsuan Chuang rate[2] = DESC_RATE12M; 996e3037485SYan-Hsuan Chuang rate[3] = DESC_RATE18M; 997e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 998e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 999e3037485SYan-Hsuan Chuang *rate_num = 4; 1000e3037485SYan-Hsuan Chuang break; 1001e3037485SYan-Hsuan Chuang case 0xC28: 1002e3037485SYan-Hsuan Chuang case 0xE28: 1003e3037485SYan-Hsuan Chuang case 0x1828: 1004e3037485SYan-Hsuan Chuang case 0x1A28: 1005e3037485SYan-Hsuan Chuang rate[0] = DESC_RATE24M; 1006e3037485SYan-Hsuan Chuang rate[1] = DESC_RATE36M; 1007e3037485SYan-Hsuan Chuang rate[2] = DESC_RATE48M; 1008e3037485SYan-Hsuan Chuang rate[3] = DESC_RATE54M; 1009e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1010e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1011e3037485SYan-Hsuan Chuang *rate_num = 4; 1012e3037485SYan-Hsuan Chuang break; 1013e3037485SYan-Hsuan Chuang case 0xC2C: 1014e3037485SYan-Hsuan Chuang case 0xE2C: 1015e3037485SYan-Hsuan Chuang case 0x182C: 1016e3037485SYan-Hsuan Chuang case 0x1A2C: 1017e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS0; 1018e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS1; 1019e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS2; 1020e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS3; 1021e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1022e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1023e3037485SYan-Hsuan Chuang *rate_num = 4; 1024e3037485SYan-Hsuan Chuang break; 1025e3037485SYan-Hsuan Chuang case 0xC30: 1026e3037485SYan-Hsuan Chuang case 0xE30: 1027e3037485SYan-Hsuan Chuang case 0x1830: 1028e3037485SYan-Hsuan Chuang case 0x1A30: 1029e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS4; 1030e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS5; 1031e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS6; 1032e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS7; 1033e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1034e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1035e3037485SYan-Hsuan Chuang *rate_num = 4; 1036e3037485SYan-Hsuan Chuang break; 1037e3037485SYan-Hsuan Chuang case 0xC34: 1038e3037485SYan-Hsuan Chuang case 0xE34: 1039e3037485SYan-Hsuan Chuang case 0x1834: 1040e3037485SYan-Hsuan Chuang case 0x1A34: 1041e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS8; 1042e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS9; 1043e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS10; 1044e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS11; 1045e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1046e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1047e3037485SYan-Hsuan Chuang *rate_num = 4; 1048e3037485SYan-Hsuan Chuang break; 1049e3037485SYan-Hsuan Chuang case 0xC38: 1050e3037485SYan-Hsuan Chuang case 0xE38: 1051e3037485SYan-Hsuan Chuang case 0x1838: 1052e3037485SYan-Hsuan Chuang case 0x1A38: 1053e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS12; 1054e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS13; 1055e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS14; 1056e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS15; 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 0xC3C: 1062e3037485SYan-Hsuan Chuang case 0xE3C: 1063e3037485SYan-Hsuan Chuang case 0x183C: 1064e3037485SYan-Hsuan Chuang case 0x1A3C: 1065e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEVHT1SS_MCS0; 1066e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEVHT1SS_MCS1; 1067e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEVHT1SS_MCS2; 1068e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEVHT1SS_MCS3; 1069e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1070e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1071e3037485SYan-Hsuan Chuang *rate_num = 4; 1072e3037485SYan-Hsuan Chuang break; 1073e3037485SYan-Hsuan Chuang case 0xC40: 1074e3037485SYan-Hsuan Chuang case 0xE40: 1075e3037485SYan-Hsuan Chuang case 0x1840: 1076e3037485SYan-Hsuan Chuang case 0x1A40: 1077e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEVHT1SS_MCS4; 1078e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEVHT1SS_MCS5; 1079e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEVHT1SS_MCS6; 1080e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEVHT1SS_MCS7; 1081e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1082e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1083e3037485SYan-Hsuan Chuang *rate_num = 4; 1084e3037485SYan-Hsuan Chuang break; 1085e3037485SYan-Hsuan Chuang case 0xC44: 1086e3037485SYan-Hsuan Chuang case 0xE44: 1087e3037485SYan-Hsuan Chuang case 0x1844: 1088e3037485SYan-Hsuan Chuang case 0x1A44: 1089e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEVHT1SS_MCS8; 1090e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEVHT1SS_MCS9; 1091e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEVHT2SS_MCS0; 1092e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEVHT2SS_MCS1; 1093e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1094e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1095e3037485SYan-Hsuan Chuang *rate_num = 4; 1096e3037485SYan-Hsuan Chuang break; 1097e3037485SYan-Hsuan Chuang case 0xC48: 1098e3037485SYan-Hsuan Chuang case 0xE48: 1099e3037485SYan-Hsuan Chuang case 0x1848: 1100e3037485SYan-Hsuan Chuang case 0x1A48: 1101e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEVHT2SS_MCS2; 1102e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEVHT2SS_MCS3; 1103e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEVHT2SS_MCS4; 1104e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEVHT2SS_MCS5; 1105e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1106e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1107e3037485SYan-Hsuan Chuang *rate_num = 4; 1108e3037485SYan-Hsuan Chuang break; 1109e3037485SYan-Hsuan Chuang case 0xC4C: 1110e3037485SYan-Hsuan Chuang case 0xE4C: 1111e3037485SYan-Hsuan Chuang case 0x184C: 1112e3037485SYan-Hsuan Chuang case 0x1A4C: 1113e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEVHT2SS_MCS6; 1114e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEVHT2SS_MCS7; 1115e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEVHT2SS_MCS8; 1116e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEVHT2SS_MCS9; 1117e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1118e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1119e3037485SYan-Hsuan Chuang *rate_num = 4; 1120e3037485SYan-Hsuan Chuang break; 1121e3037485SYan-Hsuan Chuang case 0xCD8: 1122e3037485SYan-Hsuan Chuang case 0xED8: 1123e3037485SYan-Hsuan Chuang case 0x18D8: 1124e3037485SYan-Hsuan Chuang case 0x1AD8: 1125e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS16; 1126e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS17; 1127e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS18; 1128e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS19; 1129e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1130e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1131e3037485SYan-Hsuan Chuang *rate_num = 4; 1132e3037485SYan-Hsuan Chuang break; 1133e3037485SYan-Hsuan Chuang case 0xCDC: 1134e3037485SYan-Hsuan Chuang case 0xEDC: 1135e3037485SYan-Hsuan Chuang case 0x18DC: 1136e3037485SYan-Hsuan Chuang case 0x1ADC: 1137e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS20; 1138e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS21; 1139e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS22; 1140e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS23; 1141e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1142e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1143e3037485SYan-Hsuan Chuang *rate_num = 4; 1144e3037485SYan-Hsuan Chuang break; 1145e3037485SYan-Hsuan Chuang case 0xCE0: 1146e3037485SYan-Hsuan Chuang case 0xEE0: 1147e3037485SYan-Hsuan Chuang case 0x18E0: 1148e3037485SYan-Hsuan Chuang case 0x1AE0: 1149e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEVHT3SS_MCS0; 1150e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEVHT3SS_MCS1; 1151e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEVHT3SS_MCS2; 1152e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEVHT3SS_MCS3; 1153e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1154e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1155e3037485SYan-Hsuan Chuang *rate_num = 4; 1156e3037485SYan-Hsuan Chuang break; 1157e3037485SYan-Hsuan Chuang case 0xCE4: 1158e3037485SYan-Hsuan Chuang case 0xEE4: 1159e3037485SYan-Hsuan Chuang case 0x18E4: 1160e3037485SYan-Hsuan Chuang case 0x1AE4: 1161e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEVHT3SS_MCS4; 1162e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEVHT3SS_MCS5; 1163e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEVHT3SS_MCS6; 1164e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEVHT3SS_MCS7; 1165e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1166e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1167e3037485SYan-Hsuan Chuang *rate_num = 4; 1168e3037485SYan-Hsuan Chuang break; 1169e3037485SYan-Hsuan Chuang case 0xCE8: 1170e3037485SYan-Hsuan Chuang case 0xEE8: 1171e3037485SYan-Hsuan Chuang case 0x18E8: 1172e3037485SYan-Hsuan Chuang case 0x1AE8: 1173e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEVHT3SS_MCS8; 1174e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEVHT3SS_MCS9; 1175e3037485SYan-Hsuan Chuang for (i = 0; i < 2; ++i) 1176e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1177e3037485SYan-Hsuan Chuang *rate_num = 2; 1178e3037485SYan-Hsuan Chuang break; 1179e3037485SYan-Hsuan Chuang default: 1180e3037485SYan-Hsuan Chuang rtw_warn(rtwdev, "invalid tx power index addr 0x%08x\n", addr); 1181e3037485SYan-Hsuan Chuang break; 1182e3037485SYan-Hsuan Chuang } 1183e3037485SYan-Hsuan Chuang } 1184e3037485SYan-Hsuan Chuang 118543712199SYan-Hsuan Chuang static void rtw_phy_store_tx_power_by_rate(struct rtw_dev *rtwdev, 1186fa6dfe6bSYan-Hsuan Chuang u32 band, u32 rfpath, u32 txnum, 1187e3037485SYan-Hsuan Chuang u32 regaddr, u32 bitmask, u32 data) 1188e3037485SYan-Hsuan Chuang { 1189e3037485SYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 1190e3037485SYan-Hsuan Chuang u8 rate_num = 0; 1191e3037485SYan-Hsuan Chuang u8 rate; 1192e3037485SYan-Hsuan Chuang u8 rates[RTW_RF_PATH_MAX] = {0}; 1193e3037485SYan-Hsuan Chuang s8 offset; 1194e3037485SYan-Hsuan Chuang s8 pwr_by_rate[RTW_RF_PATH_MAX] = {0}; 1195e3037485SYan-Hsuan Chuang int i; 1196e3037485SYan-Hsuan Chuang 119743712199SYan-Hsuan Chuang rtw_phy_get_rate_values_of_txpwr_by_rate(rtwdev, regaddr, bitmask, data, 1198e3037485SYan-Hsuan Chuang rates, pwr_by_rate, &rate_num); 1199e3037485SYan-Hsuan Chuang 1200e3037485SYan-Hsuan Chuang if (WARN_ON(rfpath >= RTW_RF_PATH_MAX || 1201e3037485SYan-Hsuan Chuang (band != PHY_BAND_2G && band != PHY_BAND_5G) || 1202e3037485SYan-Hsuan Chuang rate_num > RTW_RF_PATH_MAX)) 1203e3037485SYan-Hsuan Chuang return; 1204e3037485SYan-Hsuan Chuang 1205e3037485SYan-Hsuan Chuang for (i = 0; i < rate_num; i++) { 1206e3037485SYan-Hsuan Chuang offset = pwr_by_rate[i]; 1207e3037485SYan-Hsuan Chuang rate = rates[i]; 1208e3037485SYan-Hsuan Chuang if (band == PHY_BAND_2G) 1209e3037485SYan-Hsuan Chuang hal->tx_pwr_by_rate_offset_2g[rfpath][rate] = offset; 1210e3037485SYan-Hsuan Chuang else if (band == PHY_BAND_5G) 1211e3037485SYan-Hsuan Chuang hal->tx_pwr_by_rate_offset_5g[rfpath][rate] = offset; 1212e3037485SYan-Hsuan Chuang else 1213e3037485SYan-Hsuan Chuang continue; 1214e3037485SYan-Hsuan Chuang } 1215e3037485SYan-Hsuan Chuang } 1216e3037485SYan-Hsuan Chuang 1217fa6dfe6bSYan-Hsuan Chuang void rtw_parse_tbl_bb_pg(struct rtw_dev *rtwdev, const struct rtw_table *tbl) 1218fa6dfe6bSYan-Hsuan Chuang { 12190b8db87dSYan-Hsuan Chuang const struct rtw_phy_pg_cfg_pair *p = tbl->data; 12200b8db87dSYan-Hsuan Chuang const struct rtw_phy_pg_cfg_pair *end = p + tbl->size; 1221fa6dfe6bSYan-Hsuan Chuang 1222fa6dfe6bSYan-Hsuan Chuang for (; p < end; p++) { 1223fa6dfe6bSYan-Hsuan Chuang if (p->addr == 0xfe || p->addr == 0xffe) { 1224fa6dfe6bSYan-Hsuan Chuang msleep(50); 1225fa6dfe6bSYan-Hsuan Chuang continue; 1226fa6dfe6bSYan-Hsuan Chuang } 122743712199SYan-Hsuan Chuang rtw_phy_store_tx_power_by_rate(rtwdev, p->band, p->rf_path, 1228fa6dfe6bSYan-Hsuan Chuang p->tx_num, p->addr, p->bitmask, 1229fa6dfe6bSYan-Hsuan Chuang p->data); 1230fa6dfe6bSYan-Hsuan Chuang } 1231fa6dfe6bSYan-Hsuan Chuang } 1232fa6dfe6bSYan-Hsuan Chuang 1233fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_channel_idx_5g[RTW_MAX_CHANNEL_NUM_5G] = { 1234fa6dfe6bSYan-Hsuan Chuang 36, 38, 40, 42, 44, 46, 48, /* Band 1 */ 1235fa6dfe6bSYan-Hsuan Chuang 52, 54, 56, 58, 60, 62, 64, /* Band 2 */ 1236fa6dfe6bSYan-Hsuan Chuang 100, 102, 104, 106, 108, 110, 112, /* Band 3 */ 1237fa6dfe6bSYan-Hsuan Chuang 116, 118, 120, 122, 124, 126, 128, /* Band 3 */ 1238fa6dfe6bSYan-Hsuan Chuang 132, 134, 136, 138, 140, 142, 144, /* Band 3 */ 1239fa6dfe6bSYan-Hsuan Chuang 149, 151, 153, 155, 157, 159, 161, /* Band 4 */ 1240fa6dfe6bSYan-Hsuan Chuang 165, 167, 169, 171, 173, 175, 177}; /* Band 4 */ 1241fa6dfe6bSYan-Hsuan Chuang 1242fa6dfe6bSYan-Hsuan Chuang static int rtw_channel_to_idx(u8 band, u8 channel) 1243fa6dfe6bSYan-Hsuan Chuang { 1244fa6dfe6bSYan-Hsuan Chuang int ch_idx; 1245fa6dfe6bSYan-Hsuan Chuang u8 n_channel; 1246fa6dfe6bSYan-Hsuan Chuang 1247fa6dfe6bSYan-Hsuan Chuang if (band == PHY_BAND_2G) { 1248fa6dfe6bSYan-Hsuan Chuang ch_idx = channel - 1; 1249fa6dfe6bSYan-Hsuan Chuang n_channel = RTW_MAX_CHANNEL_NUM_2G; 1250fa6dfe6bSYan-Hsuan Chuang } else if (band == PHY_BAND_5G) { 1251fa6dfe6bSYan-Hsuan Chuang n_channel = RTW_MAX_CHANNEL_NUM_5G; 1252fa6dfe6bSYan-Hsuan Chuang for (ch_idx = 0; ch_idx < n_channel; ch_idx++) 1253fa6dfe6bSYan-Hsuan Chuang if (rtw_channel_idx_5g[ch_idx] == channel) 1254fa6dfe6bSYan-Hsuan Chuang break; 1255fa6dfe6bSYan-Hsuan Chuang } else { 1256fa6dfe6bSYan-Hsuan Chuang return -1; 1257fa6dfe6bSYan-Hsuan Chuang } 1258fa6dfe6bSYan-Hsuan Chuang 1259fa6dfe6bSYan-Hsuan Chuang if (ch_idx >= n_channel) 1260fa6dfe6bSYan-Hsuan Chuang return -1; 1261fa6dfe6bSYan-Hsuan Chuang 1262fa6dfe6bSYan-Hsuan Chuang return ch_idx; 1263fa6dfe6bSYan-Hsuan Chuang } 1264fa6dfe6bSYan-Hsuan Chuang 126543712199SYan-Hsuan Chuang static void rtw_phy_set_tx_power_limit(struct rtw_dev *rtwdev, u8 regd, u8 band, 1266fa6dfe6bSYan-Hsuan Chuang u8 bw, u8 rs, u8 ch, s8 pwr_limit) 1267fa6dfe6bSYan-Hsuan Chuang { 1268fa6dfe6bSYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 12690d350f0aSTzu-En Huang u8 max_power_index = rtwdev->chip->max_power_index; 1270adf3c676SYan-Hsuan Chuang s8 ww; 1271fa6dfe6bSYan-Hsuan Chuang int ch_idx; 1272fa6dfe6bSYan-Hsuan Chuang 1273fa6dfe6bSYan-Hsuan Chuang pwr_limit = clamp_t(s8, pwr_limit, 12740d350f0aSTzu-En Huang -max_power_index, max_power_index); 1275fa6dfe6bSYan-Hsuan Chuang ch_idx = rtw_channel_to_idx(band, ch); 1276fa6dfe6bSYan-Hsuan Chuang 1277fa6dfe6bSYan-Hsuan Chuang if (regd >= RTW_REGD_MAX || bw >= RTW_CHANNEL_WIDTH_MAX || 1278fa6dfe6bSYan-Hsuan Chuang rs >= RTW_RATE_SECTION_MAX || ch_idx < 0) { 1279fa6dfe6bSYan-Hsuan Chuang WARN(1, 1280fa6dfe6bSYan-Hsuan Chuang "wrong txpwr_lmt regd=%u, band=%u bw=%u, rs=%u, ch_idx=%u, pwr_limit=%d\n", 1281fa6dfe6bSYan-Hsuan Chuang regd, band, bw, rs, ch_idx, pwr_limit); 1282fa6dfe6bSYan-Hsuan Chuang return; 1283fa6dfe6bSYan-Hsuan Chuang } 1284fa6dfe6bSYan-Hsuan Chuang 1285adf3c676SYan-Hsuan Chuang if (band == PHY_BAND_2G) { 1286fa6dfe6bSYan-Hsuan Chuang hal->tx_pwr_limit_2g[regd][bw][rs][ch_idx] = pwr_limit; 1287adf3c676SYan-Hsuan Chuang ww = hal->tx_pwr_limit_2g[RTW_REGD_WW][bw][rs][ch_idx]; 1288adf3c676SYan-Hsuan Chuang ww = min_t(s8, ww, pwr_limit); 1289adf3c676SYan-Hsuan Chuang hal->tx_pwr_limit_2g[RTW_REGD_WW][bw][rs][ch_idx] = ww; 1290adf3c676SYan-Hsuan Chuang } else if (band == PHY_BAND_5G) { 1291fa6dfe6bSYan-Hsuan Chuang hal->tx_pwr_limit_5g[regd][bw][rs][ch_idx] = pwr_limit; 1292adf3c676SYan-Hsuan Chuang ww = hal->tx_pwr_limit_5g[RTW_REGD_WW][bw][rs][ch_idx]; 1293adf3c676SYan-Hsuan Chuang ww = min_t(s8, ww, pwr_limit); 1294adf3c676SYan-Hsuan Chuang hal->tx_pwr_limit_5g[RTW_REGD_WW][bw][rs][ch_idx] = ww; 1295adf3c676SYan-Hsuan Chuang } 1296fa6dfe6bSYan-Hsuan Chuang } 1297fa6dfe6bSYan-Hsuan Chuang 129893f68a86SZong-Zhe Yang /* cross-reference 5G power limits if values are not assigned */ 129993f68a86SZong-Zhe Yang static void 130093f68a86SZong-Zhe Yang rtw_xref_5g_txpwr_lmt(struct rtw_dev *rtwdev, u8 regd, 130193f68a86SZong-Zhe Yang u8 bw, u8 ch_idx, u8 rs_ht, u8 rs_vht) 130293f68a86SZong-Zhe Yang { 130393f68a86SZong-Zhe Yang struct rtw_hal *hal = &rtwdev->hal; 13040d350f0aSTzu-En Huang u8 max_power_index = rtwdev->chip->max_power_index; 130593f68a86SZong-Zhe Yang s8 lmt_ht = hal->tx_pwr_limit_5g[regd][bw][rs_ht][ch_idx]; 130693f68a86SZong-Zhe Yang s8 lmt_vht = hal->tx_pwr_limit_5g[regd][bw][rs_vht][ch_idx]; 130793f68a86SZong-Zhe Yang 130893f68a86SZong-Zhe Yang if (lmt_ht == lmt_vht) 130993f68a86SZong-Zhe Yang return; 131093f68a86SZong-Zhe Yang 13110d350f0aSTzu-En Huang if (lmt_ht == max_power_index) 131293f68a86SZong-Zhe Yang hal->tx_pwr_limit_5g[regd][bw][rs_ht][ch_idx] = lmt_vht; 131393f68a86SZong-Zhe Yang 13140d350f0aSTzu-En Huang else if (lmt_vht == max_power_index) 131593f68a86SZong-Zhe Yang hal->tx_pwr_limit_5g[regd][bw][rs_vht][ch_idx] = lmt_ht; 131693f68a86SZong-Zhe Yang } 131793f68a86SZong-Zhe Yang 131893f68a86SZong-Zhe Yang /* cross-reference power limits for ht and vht */ 131993f68a86SZong-Zhe Yang static void 132093f68a86SZong-Zhe Yang rtw_xref_txpwr_lmt_by_rs(struct rtw_dev *rtwdev, u8 regd, u8 bw, u8 ch_idx) 132193f68a86SZong-Zhe Yang { 132293f68a86SZong-Zhe Yang u8 rs_idx, rs_ht, rs_vht; 132393f68a86SZong-Zhe Yang u8 rs_cmp[2][2] = {{RTW_RATE_SECTION_HT_1S, RTW_RATE_SECTION_VHT_1S}, 132493f68a86SZong-Zhe Yang {RTW_RATE_SECTION_HT_2S, RTW_RATE_SECTION_VHT_2S} }; 132593f68a86SZong-Zhe Yang 132693f68a86SZong-Zhe Yang for (rs_idx = 0; rs_idx < 2; rs_idx++) { 132793f68a86SZong-Zhe Yang rs_ht = rs_cmp[rs_idx][0]; 132893f68a86SZong-Zhe Yang rs_vht = rs_cmp[rs_idx][1]; 132993f68a86SZong-Zhe Yang 133093f68a86SZong-Zhe Yang rtw_xref_5g_txpwr_lmt(rtwdev, regd, bw, ch_idx, rs_ht, rs_vht); 133193f68a86SZong-Zhe Yang } 133293f68a86SZong-Zhe Yang } 133393f68a86SZong-Zhe Yang 133493f68a86SZong-Zhe Yang /* cross-reference power limits for 5G channels */ 133593f68a86SZong-Zhe Yang static void 133693f68a86SZong-Zhe Yang rtw_xref_5g_txpwr_lmt_by_ch(struct rtw_dev *rtwdev, u8 regd, u8 bw) 133793f68a86SZong-Zhe Yang { 133893f68a86SZong-Zhe Yang u8 ch_idx; 133993f68a86SZong-Zhe Yang 134093f68a86SZong-Zhe Yang for (ch_idx = 0; ch_idx < RTW_MAX_CHANNEL_NUM_5G; ch_idx++) 134193f68a86SZong-Zhe Yang rtw_xref_txpwr_lmt_by_rs(rtwdev, regd, bw, ch_idx); 134293f68a86SZong-Zhe Yang } 134393f68a86SZong-Zhe Yang 134493f68a86SZong-Zhe Yang /* cross-reference power limits for 20/40M bandwidth */ 134593f68a86SZong-Zhe Yang static void 134693f68a86SZong-Zhe Yang rtw_xref_txpwr_lmt_by_bw(struct rtw_dev *rtwdev, u8 regd) 134793f68a86SZong-Zhe Yang { 134893f68a86SZong-Zhe Yang u8 bw; 134993f68a86SZong-Zhe Yang 135093f68a86SZong-Zhe Yang for (bw = RTW_CHANNEL_WIDTH_20; bw <= RTW_CHANNEL_WIDTH_40; bw++) 135193f68a86SZong-Zhe Yang rtw_xref_5g_txpwr_lmt_by_ch(rtwdev, regd, bw); 135293f68a86SZong-Zhe Yang } 135393f68a86SZong-Zhe Yang 135493f68a86SZong-Zhe Yang /* cross-reference power limits */ 135593f68a86SZong-Zhe Yang static void rtw_xref_txpwr_lmt(struct rtw_dev *rtwdev) 135693f68a86SZong-Zhe Yang { 135793f68a86SZong-Zhe Yang u8 regd; 135893f68a86SZong-Zhe Yang 135993f68a86SZong-Zhe Yang for (regd = 0; regd < RTW_REGD_MAX; regd++) 136093f68a86SZong-Zhe Yang rtw_xref_txpwr_lmt_by_bw(rtwdev, regd); 136193f68a86SZong-Zhe Yang } 136293f68a86SZong-Zhe Yang 1363fa6dfe6bSYan-Hsuan Chuang void rtw_parse_tbl_txpwr_lmt(struct rtw_dev *rtwdev, 1364fa6dfe6bSYan-Hsuan Chuang const struct rtw_table *tbl) 1365fa6dfe6bSYan-Hsuan Chuang { 13663457f86dSBrian Norris const struct rtw_txpwr_lmt_cfg_pair *p = tbl->data; 13673457f86dSBrian Norris const struct rtw_txpwr_lmt_cfg_pair *end = p + tbl->size; 1368fa6dfe6bSYan-Hsuan Chuang 1369fa6dfe6bSYan-Hsuan Chuang for (; p < end; p++) { 137043712199SYan-Hsuan Chuang rtw_phy_set_tx_power_limit(rtwdev, p->regd, p->band, 137143712199SYan-Hsuan Chuang p->bw, p->rs, p->ch, p->txpwr_lmt); 1372fa6dfe6bSYan-Hsuan Chuang } 137393f68a86SZong-Zhe Yang 137493f68a86SZong-Zhe Yang rtw_xref_txpwr_lmt(rtwdev); 1375fa6dfe6bSYan-Hsuan Chuang } 1376fa6dfe6bSYan-Hsuan Chuang 1377fa6dfe6bSYan-Hsuan Chuang void rtw_phy_cfg_mac(struct rtw_dev *rtwdev, const struct rtw_table *tbl, 1378fa6dfe6bSYan-Hsuan Chuang u32 addr, u32 data) 1379fa6dfe6bSYan-Hsuan Chuang { 1380fa6dfe6bSYan-Hsuan Chuang rtw_write8(rtwdev, addr, data); 1381fa6dfe6bSYan-Hsuan Chuang } 1382fa6dfe6bSYan-Hsuan Chuang 1383fa6dfe6bSYan-Hsuan Chuang void rtw_phy_cfg_agc(struct rtw_dev *rtwdev, const struct rtw_table *tbl, 1384fa6dfe6bSYan-Hsuan Chuang u32 addr, u32 data) 1385fa6dfe6bSYan-Hsuan Chuang { 1386fa6dfe6bSYan-Hsuan Chuang rtw_write32(rtwdev, addr, data); 1387fa6dfe6bSYan-Hsuan Chuang } 1388fa6dfe6bSYan-Hsuan Chuang 1389fa6dfe6bSYan-Hsuan Chuang void rtw_phy_cfg_bb(struct rtw_dev *rtwdev, const struct rtw_table *tbl, 1390fa6dfe6bSYan-Hsuan Chuang u32 addr, u32 data) 1391fa6dfe6bSYan-Hsuan Chuang { 1392fa6dfe6bSYan-Hsuan Chuang if (addr == 0xfe) 1393fa6dfe6bSYan-Hsuan Chuang msleep(50); 1394fa6dfe6bSYan-Hsuan Chuang else if (addr == 0xfd) 1395fa6dfe6bSYan-Hsuan Chuang mdelay(5); 1396fa6dfe6bSYan-Hsuan Chuang else if (addr == 0xfc) 1397fa6dfe6bSYan-Hsuan Chuang mdelay(1); 1398fa6dfe6bSYan-Hsuan Chuang else if (addr == 0xfb) 1399fa6dfe6bSYan-Hsuan Chuang usleep_range(50, 60); 1400fa6dfe6bSYan-Hsuan Chuang else if (addr == 0xfa) 1401fa6dfe6bSYan-Hsuan Chuang udelay(5); 1402fa6dfe6bSYan-Hsuan Chuang else if (addr == 0xf9) 1403fa6dfe6bSYan-Hsuan Chuang udelay(1); 1404fa6dfe6bSYan-Hsuan Chuang else 1405fa6dfe6bSYan-Hsuan Chuang rtw_write32(rtwdev, addr, data); 1406fa6dfe6bSYan-Hsuan Chuang } 1407fa6dfe6bSYan-Hsuan Chuang 1408fa6dfe6bSYan-Hsuan Chuang void rtw_phy_cfg_rf(struct rtw_dev *rtwdev, const struct rtw_table *tbl, 1409fa6dfe6bSYan-Hsuan Chuang u32 addr, u32 data) 1410fa6dfe6bSYan-Hsuan Chuang { 1411fa6dfe6bSYan-Hsuan Chuang if (addr == 0xffe) { 1412fa6dfe6bSYan-Hsuan Chuang msleep(50); 1413fa6dfe6bSYan-Hsuan Chuang } else if (addr == 0xfe) { 1414fa6dfe6bSYan-Hsuan Chuang usleep_range(100, 110); 1415fa6dfe6bSYan-Hsuan Chuang } else { 1416fa6dfe6bSYan-Hsuan Chuang rtw_write_rf(rtwdev, tbl->rf_path, addr, RFREG_MASK, data); 1417fa6dfe6bSYan-Hsuan Chuang udelay(1); 1418fa6dfe6bSYan-Hsuan Chuang } 1419fa6dfe6bSYan-Hsuan Chuang } 1420fa6dfe6bSYan-Hsuan Chuang 1421fa6dfe6bSYan-Hsuan Chuang static void rtw_load_rfk_table(struct rtw_dev *rtwdev) 1422fa6dfe6bSYan-Hsuan Chuang { 1423fa6dfe6bSYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 14245227c2eeSTzu-En Huang struct rtw_dpk_info *dpk_info = &rtwdev->dm_info.dpk_info; 1425fa6dfe6bSYan-Hsuan Chuang 1426fa6dfe6bSYan-Hsuan Chuang if (!chip->rfk_init_tbl) 1427fa6dfe6bSYan-Hsuan Chuang return; 1428fa6dfe6bSYan-Hsuan Chuang 14295227c2eeSTzu-En Huang rtw_write32_mask(rtwdev, 0x1e24, BIT(17), 0x1); 14305227c2eeSTzu-En Huang rtw_write32_mask(rtwdev, 0x1cd0, BIT(28), 0x1); 14315227c2eeSTzu-En Huang rtw_write32_mask(rtwdev, 0x1cd0, BIT(29), 0x1); 14325227c2eeSTzu-En Huang rtw_write32_mask(rtwdev, 0x1cd0, BIT(30), 0x1); 14335227c2eeSTzu-En Huang rtw_write32_mask(rtwdev, 0x1cd0, BIT(31), 0x0); 14345227c2eeSTzu-En Huang 1435fa6dfe6bSYan-Hsuan Chuang rtw_load_table(rtwdev, chip->rfk_init_tbl); 14365227c2eeSTzu-En Huang 1437*891984bcSzhengbin dpk_info->is_dpk_pwr_on = true; 1438fa6dfe6bSYan-Hsuan Chuang } 1439fa6dfe6bSYan-Hsuan Chuang 1440fa6dfe6bSYan-Hsuan Chuang void rtw_phy_load_tables(struct rtw_dev *rtwdev) 1441fa6dfe6bSYan-Hsuan Chuang { 1442fa6dfe6bSYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 1443fa6dfe6bSYan-Hsuan Chuang u8 rf_path; 1444fa6dfe6bSYan-Hsuan Chuang 1445fa6dfe6bSYan-Hsuan Chuang rtw_load_table(rtwdev, chip->mac_tbl); 1446fa6dfe6bSYan-Hsuan Chuang rtw_load_table(rtwdev, chip->bb_tbl); 1447fa6dfe6bSYan-Hsuan Chuang rtw_load_table(rtwdev, chip->agc_tbl); 1448fa6dfe6bSYan-Hsuan Chuang rtw_load_rfk_table(rtwdev); 1449fa6dfe6bSYan-Hsuan Chuang 1450fa6dfe6bSYan-Hsuan Chuang for (rf_path = 0; rf_path < rtwdev->hal.rf_path_num; rf_path++) { 1451fa6dfe6bSYan-Hsuan Chuang const struct rtw_table *tbl; 1452fa6dfe6bSYan-Hsuan Chuang 1453fa6dfe6bSYan-Hsuan Chuang tbl = chip->rf_tbl[rf_path]; 1454fa6dfe6bSYan-Hsuan Chuang rtw_load_table(rtwdev, tbl); 1455fa6dfe6bSYan-Hsuan Chuang } 1456fa6dfe6bSYan-Hsuan Chuang } 1457fa6dfe6bSYan-Hsuan Chuang 1458fa6dfe6bSYan-Hsuan Chuang static u8 rtw_get_channel_group(u8 channel) 1459fa6dfe6bSYan-Hsuan Chuang { 1460fa6dfe6bSYan-Hsuan Chuang switch (channel) { 1461fa6dfe6bSYan-Hsuan Chuang default: 1462fa6dfe6bSYan-Hsuan Chuang WARN_ON(1); 1463fa6dfe6bSYan-Hsuan Chuang /* fall through */ 1464fa6dfe6bSYan-Hsuan Chuang case 1: 1465fa6dfe6bSYan-Hsuan Chuang case 2: 1466fa6dfe6bSYan-Hsuan Chuang case 36: 1467fa6dfe6bSYan-Hsuan Chuang case 38: 1468fa6dfe6bSYan-Hsuan Chuang case 40: 1469fa6dfe6bSYan-Hsuan Chuang case 42: 1470fa6dfe6bSYan-Hsuan Chuang return 0; 1471fa6dfe6bSYan-Hsuan Chuang case 3: 1472fa6dfe6bSYan-Hsuan Chuang case 4: 1473fa6dfe6bSYan-Hsuan Chuang case 5: 1474fa6dfe6bSYan-Hsuan Chuang case 44: 1475fa6dfe6bSYan-Hsuan Chuang case 46: 1476fa6dfe6bSYan-Hsuan Chuang case 48: 1477fa6dfe6bSYan-Hsuan Chuang case 50: 1478fa6dfe6bSYan-Hsuan Chuang return 1; 1479fa6dfe6bSYan-Hsuan Chuang case 6: 1480fa6dfe6bSYan-Hsuan Chuang case 7: 1481fa6dfe6bSYan-Hsuan Chuang case 8: 1482fa6dfe6bSYan-Hsuan Chuang case 52: 1483fa6dfe6bSYan-Hsuan Chuang case 54: 1484fa6dfe6bSYan-Hsuan Chuang case 56: 1485fa6dfe6bSYan-Hsuan Chuang case 58: 1486fa6dfe6bSYan-Hsuan Chuang return 2; 1487fa6dfe6bSYan-Hsuan Chuang case 9: 1488fa6dfe6bSYan-Hsuan Chuang case 10: 1489fa6dfe6bSYan-Hsuan Chuang case 11: 1490fa6dfe6bSYan-Hsuan Chuang case 60: 1491fa6dfe6bSYan-Hsuan Chuang case 62: 1492fa6dfe6bSYan-Hsuan Chuang case 64: 1493fa6dfe6bSYan-Hsuan Chuang return 3; 1494fa6dfe6bSYan-Hsuan Chuang case 12: 1495fa6dfe6bSYan-Hsuan Chuang case 13: 1496fa6dfe6bSYan-Hsuan Chuang case 100: 1497fa6dfe6bSYan-Hsuan Chuang case 102: 1498fa6dfe6bSYan-Hsuan Chuang case 104: 1499fa6dfe6bSYan-Hsuan Chuang case 106: 1500fa6dfe6bSYan-Hsuan Chuang return 4; 1501fa6dfe6bSYan-Hsuan Chuang case 14: 1502fa6dfe6bSYan-Hsuan Chuang case 108: 1503fa6dfe6bSYan-Hsuan Chuang case 110: 1504fa6dfe6bSYan-Hsuan Chuang case 112: 1505fa6dfe6bSYan-Hsuan Chuang case 114: 1506fa6dfe6bSYan-Hsuan Chuang return 5; 1507fa6dfe6bSYan-Hsuan Chuang case 116: 1508fa6dfe6bSYan-Hsuan Chuang case 118: 1509fa6dfe6bSYan-Hsuan Chuang case 120: 1510fa6dfe6bSYan-Hsuan Chuang case 122: 1511fa6dfe6bSYan-Hsuan Chuang return 6; 1512fa6dfe6bSYan-Hsuan Chuang case 124: 1513fa6dfe6bSYan-Hsuan Chuang case 126: 1514fa6dfe6bSYan-Hsuan Chuang case 128: 1515fa6dfe6bSYan-Hsuan Chuang case 130: 1516fa6dfe6bSYan-Hsuan Chuang return 7; 1517fa6dfe6bSYan-Hsuan Chuang case 132: 1518fa6dfe6bSYan-Hsuan Chuang case 134: 1519fa6dfe6bSYan-Hsuan Chuang case 136: 1520fa6dfe6bSYan-Hsuan Chuang case 138: 1521fa6dfe6bSYan-Hsuan Chuang return 8; 1522fa6dfe6bSYan-Hsuan Chuang case 140: 1523fa6dfe6bSYan-Hsuan Chuang case 142: 1524fa6dfe6bSYan-Hsuan Chuang case 144: 1525fa6dfe6bSYan-Hsuan Chuang return 9; 1526fa6dfe6bSYan-Hsuan Chuang case 149: 1527fa6dfe6bSYan-Hsuan Chuang case 151: 1528fa6dfe6bSYan-Hsuan Chuang case 153: 1529fa6dfe6bSYan-Hsuan Chuang case 155: 1530fa6dfe6bSYan-Hsuan Chuang return 10; 1531fa6dfe6bSYan-Hsuan Chuang case 157: 1532fa6dfe6bSYan-Hsuan Chuang case 159: 1533fa6dfe6bSYan-Hsuan Chuang case 161: 1534fa6dfe6bSYan-Hsuan Chuang return 11; 1535fa6dfe6bSYan-Hsuan Chuang case 165: 1536fa6dfe6bSYan-Hsuan Chuang case 167: 1537fa6dfe6bSYan-Hsuan Chuang case 169: 1538fa6dfe6bSYan-Hsuan Chuang case 171: 1539fa6dfe6bSYan-Hsuan Chuang return 12; 1540fa6dfe6bSYan-Hsuan Chuang case 173: 1541fa6dfe6bSYan-Hsuan Chuang case 175: 1542fa6dfe6bSYan-Hsuan Chuang case 177: 1543fa6dfe6bSYan-Hsuan Chuang return 13; 1544fa6dfe6bSYan-Hsuan Chuang } 1545fa6dfe6bSYan-Hsuan Chuang } 1546fa6dfe6bSYan-Hsuan Chuang 15475227c2eeSTzu-En Huang static s8 rtw_phy_get_dis_dpd_by_rate_diff(struct rtw_dev *rtwdev, u16 rate) 15485227c2eeSTzu-En Huang { 15495227c2eeSTzu-En Huang struct rtw_chip_info *chip = rtwdev->chip; 15505227c2eeSTzu-En Huang s8 dpd_diff = 0; 15515227c2eeSTzu-En Huang 15525227c2eeSTzu-En Huang if (!chip->en_dis_dpd) 15535227c2eeSTzu-En Huang return 0; 15545227c2eeSTzu-En Huang 15555227c2eeSTzu-En Huang #define RTW_DPD_RATE_CHECK(_rate) \ 15565227c2eeSTzu-En Huang case DESC_RATE ## _rate: \ 15575227c2eeSTzu-En Huang if (DIS_DPD_RATE ## _rate & chip->dpd_ratemask) \ 15585227c2eeSTzu-En Huang dpd_diff = -6 * chip->txgi_factor; \ 15595227c2eeSTzu-En Huang break 15605227c2eeSTzu-En Huang 15615227c2eeSTzu-En Huang switch (rate) { 15625227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(6M); 15635227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(9M); 15645227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(MCS0); 15655227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(MCS1); 15665227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(MCS8); 15675227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(MCS9); 15685227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(VHT1SS_MCS0); 15695227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(VHT1SS_MCS1); 15705227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(VHT2SS_MCS0); 15715227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(VHT2SS_MCS1); 15725227c2eeSTzu-En Huang } 15735227c2eeSTzu-En Huang #undef RTW_DPD_RATE_CHECK 15745227c2eeSTzu-En Huang 15755227c2eeSTzu-En Huang return dpd_diff; 15765227c2eeSTzu-En Huang } 15775227c2eeSTzu-En Huang 157843712199SYan-Hsuan Chuang static u8 rtw_phy_get_2g_tx_power_index(struct rtw_dev *rtwdev, 1579fa6dfe6bSYan-Hsuan Chuang struct rtw_2g_txpwr_idx *pwr_idx_2g, 1580fa6dfe6bSYan-Hsuan Chuang enum rtw_bandwidth bandwidth, 1581fa6dfe6bSYan-Hsuan Chuang u8 rate, u8 group) 1582fa6dfe6bSYan-Hsuan Chuang { 1583fa6dfe6bSYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 1584fa6dfe6bSYan-Hsuan Chuang u8 tx_power; 1585fa6dfe6bSYan-Hsuan Chuang bool mcs_rate; 1586fa6dfe6bSYan-Hsuan Chuang bool above_2ss; 1587fa6dfe6bSYan-Hsuan Chuang u8 factor = chip->txgi_factor; 1588fa6dfe6bSYan-Hsuan Chuang 1589fa6dfe6bSYan-Hsuan Chuang if (rate <= DESC_RATE11M) 1590fa6dfe6bSYan-Hsuan Chuang tx_power = pwr_idx_2g->cck_base[group]; 1591fa6dfe6bSYan-Hsuan Chuang else 1592fa6dfe6bSYan-Hsuan Chuang tx_power = pwr_idx_2g->bw40_base[group]; 1593fa6dfe6bSYan-Hsuan Chuang 1594fa6dfe6bSYan-Hsuan Chuang if (rate >= DESC_RATE6M && rate <= DESC_RATE54M) 1595fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_2g->ht_1s_diff.ofdm * factor; 1596fa6dfe6bSYan-Hsuan Chuang 1597fa6dfe6bSYan-Hsuan Chuang mcs_rate = (rate >= DESC_RATEMCS0 && rate <= DESC_RATEMCS15) || 1598fa6dfe6bSYan-Hsuan Chuang (rate >= DESC_RATEVHT1SS_MCS0 && 1599fa6dfe6bSYan-Hsuan Chuang rate <= DESC_RATEVHT2SS_MCS9); 1600fa6dfe6bSYan-Hsuan Chuang above_2ss = (rate >= DESC_RATEMCS8 && rate <= DESC_RATEMCS15) || 1601fa6dfe6bSYan-Hsuan Chuang (rate >= DESC_RATEVHT2SS_MCS0); 1602fa6dfe6bSYan-Hsuan Chuang 1603fa6dfe6bSYan-Hsuan Chuang if (!mcs_rate) 1604fa6dfe6bSYan-Hsuan Chuang return tx_power; 1605fa6dfe6bSYan-Hsuan Chuang 1606fa6dfe6bSYan-Hsuan Chuang switch (bandwidth) { 1607fa6dfe6bSYan-Hsuan Chuang default: 1608fa6dfe6bSYan-Hsuan Chuang WARN_ON(1); 1609fa6dfe6bSYan-Hsuan Chuang /* fall through */ 1610fa6dfe6bSYan-Hsuan Chuang case RTW_CHANNEL_WIDTH_20: 1611fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_2g->ht_1s_diff.bw20 * factor; 1612fa6dfe6bSYan-Hsuan Chuang if (above_2ss) 1613fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_2g->ht_2s_diff.bw20 * factor; 1614fa6dfe6bSYan-Hsuan Chuang break; 1615fa6dfe6bSYan-Hsuan Chuang case RTW_CHANNEL_WIDTH_40: 1616fa6dfe6bSYan-Hsuan Chuang /* bw40 is the base power */ 1617fa6dfe6bSYan-Hsuan Chuang if (above_2ss) 1618fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_2g->ht_2s_diff.bw40 * factor; 1619fa6dfe6bSYan-Hsuan Chuang break; 1620fa6dfe6bSYan-Hsuan Chuang } 1621fa6dfe6bSYan-Hsuan Chuang 1622fa6dfe6bSYan-Hsuan Chuang return tx_power; 1623fa6dfe6bSYan-Hsuan Chuang } 1624fa6dfe6bSYan-Hsuan Chuang 162543712199SYan-Hsuan Chuang static u8 rtw_phy_get_5g_tx_power_index(struct rtw_dev *rtwdev, 1626fa6dfe6bSYan-Hsuan Chuang struct rtw_5g_txpwr_idx *pwr_idx_5g, 1627fa6dfe6bSYan-Hsuan Chuang enum rtw_bandwidth bandwidth, 1628fa6dfe6bSYan-Hsuan Chuang u8 rate, u8 group) 1629fa6dfe6bSYan-Hsuan Chuang { 1630fa6dfe6bSYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 1631fa6dfe6bSYan-Hsuan Chuang u8 tx_power; 1632fa6dfe6bSYan-Hsuan Chuang u8 upper, lower; 1633fa6dfe6bSYan-Hsuan Chuang bool mcs_rate; 1634fa6dfe6bSYan-Hsuan Chuang bool above_2ss; 1635fa6dfe6bSYan-Hsuan Chuang u8 factor = chip->txgi_factor; 1636fa6dfe6bSYan-Hsuan Chuang 1637fa6dfe6bSYan-Hsuan Chuang tx_power = pwr_idx_5g->bw40_base[group]; 1638fa6dfe6bSYan-Hsuan Chuang 1639fa6dfe6bSYan-Hsuan Chuang mcs_rate = (rate >= DESC_RATEMCS0 && rate <= DESC_RATEMCS15) || 1640fa6dfe6bSYan-Hsuan Chuang (rate >= DESC_RATEVHT1SS_MCS0 && 1641fa6dfe6bSYan-Hsuan Chuang rate <= DESC_RATEVHT2SS_MCS9); 1642fa6dfe6bSYan-Hsuan Chuang above_2ss = (rate >= DESC_RATEMCS8 && rate <= DESC_RATEMCS15) || 1643fa6dfe6bSYan-Hsuan Chuang (rate >= DESC_RATEVHT2SS_MCS0); 1644fa6dfe6bSYan-Hsuan Chuang 1645fa6dfe6bSYan-Hsuan Chuang if (!mcs_rate) { 1646fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_5g->ht_1s_diff.ofdm * factor; 1647fa6dfe6bSYan-Hsuan Chuang return tx_power; 1648fa6dfe6bSYan-Hsuan Chuang } 1649fa6dfe6bSYan-Hsuan Chuang 1650fa6dfe6bSYan-Hsuan Chuang switch (bandwidth) { 1651fa6dfe6bSYan-Hsuan Chuang default: 1652fa6dfe6bSYan-Hsuan Chuang WARN_ON(1); 1653fa6dfe6bSYan-Hsuan Chuang /* fall through */ 1654fa6dfe6bSYan-Hsuan Chuang case RTW_CHANNEL_WIDTH_20: 1655fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_5g->ht_1s_diff.bw20 * factor; 1656fa6dfe6bSYan-Hsuan Chuang if (above_2ss) 1657fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_5g->ht_2s_diff.bw20 * factor; 1658fa6dfe6bSYan-Hsuan Chuang break; 1659fa6dfe6bSYan-Hsuan Chuang case RTW_CHANNEL_WIDTH_40: 1660fa6dfe6bSYan-Hsuan Chuang /* bw40 is the base power */ 1661fa6dfe6bSYan-Hsuan Chuang if (above_2ss) 1662fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_5g->ht_2s_diff.bw40 * factor; 1663fa6dfe6bSYan-Hsuan Chuang break; 1664fa6dfe6bSYan-Hsuan Chuang case RTW_CHANNEL_WIDTH_80: 1665fa6dfe6bSYan-Hsuan Chuang /* the base idx of bw80 is the average of bw40+/bw40- */ 1666fa6dfe6bSYan-Hsuan Chuang lower = pwr_idx_5g->bw40_base[group]; 1667fa6dfe6bSYan-Hsuan Chuang upper = pwr_idx_5g->bw40_base[group + 1]; 1668fa6dfe6bSYan-Hsuan Chuang 1669fa6dfe6bSYan-Hsuan Chuang tx_power = (lower + upper) / 2; 1670fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_5g->vht_1s_diff.bw80 * factor; 1671fa6dfe6bSYan-Hsuan Chuang if (above_2ss) 1672fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_5g->vht_2s_diff.bw80 * factor; 1673fa6dfe6bSYan-Hsuan Chuang break; 1674fa6dfe6bSYan-Hsuan Chuang } 1675fa6dfe6bSYan-Hsuan Chuang 1676fa6dfe6bSYan-Hsuan Chuang return tx_power; 1677fa6dfe6bSYan-Hsuan Chuang } 1678fa6dfe6bSYan-Hsuan Chuang 167943712199SYan-Hsuan Chuang static s8 rtw_phy_get_tx_power_limit(struct rtw_dev *rtwdev, u8 band, 1680fa6dfe6bSYan-Hsuan Chuang enum rtw_bandwidth bw, u8 rf_path, 1681fa6dfe6bSYan-Hsuan Chuang u8 rate, u8 channel, u8 regd) 1682fa6dfe6bSYan-Hsuan Chuang { 1683fa6dfe6bSYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 168493f68a86SZong-Zhe Yang u8 *cch_by_bw = hal->cch_by_bw; 16850d350f0aSTzu-En Huang s8 power_limit = (s8)rtwdev->chip->max_power_index; 1686fa6dfe6bSYan-Hsuan Chuang u8 rs; 1687fa6dfe6bSYan-Hsuan Chuang int ch_idx; 168893f68a86SZong-Zhe Yang u8 cur_bw, cur_ch; 168993f68a86SZong-Zhe Yang s8 cur_lmt; 1690fa6dfe6bSYan-Hsuan Chuang 169176403816SYan-Hsuan Chuang if (regd > RTW_REGD_WW) 16920d350f0aSTzu-En Huang return power_limit; 169376403816SYan-Hsuan Chuang 1694fa6dfe6bSYan-Hsuan Chuang if (rate >= DESC_RATE1M && rate <= DESC_RATE11M) 1695fa6dfe6bSYan-Hsuan Chuang rs = RTW_RATE_SECTION_CCK; 1696fa6dfe6bSYan-Hsuan Chuang else if (rate >= DESC_RATE6M && rate <= DESC_RATE54M) 1697fa6dfe6bSYan-Hsuan Chuang rs = RTW_RATE_SECTION_OFDM; 1698fa6dfe6bSYan-Hsuan Chuang else if (rate >= DESC_RATEMCS0 && rate <= DESC_RATEMCS7) 1699fa6dfe6bSYan-Hsuan Chuang rs = RTW_RATE_SECTION_HT_1S; 1700fa6dfe6bSYan-Hsuan Chuang else if (rate >= DESC_RATEMCS8 && rate <= DESC_RATEMCS15) 1701fa6dfe6bSYan-Hsuan Chuang rs = RTW_RATE_SECTION_HT_2S; 1702fa6dfe6bSYan-Hsuan Chuang else if (rate >= DESC_RATEVHT1SS_MCS0 && rate <= DESC_RATEVHT1SS_MCS9) 1703fa6dfe6bSYan-Hsuan Chuang rs = RTW_RATE_SECTION_VHT_1S; 1704fa6dfe6bSYan-Hsuan Chuang else if (rate >= DESC_RATEVHT2SS_MCS0 && rate <= DESC_RATEVHT2SS_MCS9) 1705fa6dfe6bSYan-Hsuan Chuang rs = RTW_RATE_SECTION_VHT_2S; 1706fa6dfe6bSYan-Hsuan Chuang else 1707fa6dfe6bSYan-Hsuan Chuang goto err; 1708fa6dfe6bSYan-Hsuan Chuang 170993f68a86SZong-Zhe Yang /* only 20M BW with cck and ofdm */ 171093f68a86SZong-Zhe Yang if (rs == RTW_RATE_SECTION_CCK || rs == RTW_RATE_SECTION_OFDM) 171193f68a86SZong-Zhe Yang bw = RTW_CHANNEL_WIDTH_20; 171293f68a86SZong-Zhe Yang 171393f68a86SZong-Zhe Yang /* only 20/40M BW with ht */ 171493f68a86SZong-Zhe Yang if (rs == RTW_RATE_SECTION_HT_1S || rs == RTW_RATE_SECTION_HT_2S) 171593f68a86SZong-Zhe Yang bw = min_t(u8, bw, RTW_CHANNEL_WIDTH_40); 171693f68a86SZong-Zhe Yang 171793f68a86SZong-Zhe Yang /* select min power limit among [20M BW ~ current BW] */ 171893f68a86SZong-Zhe Yang for (cur_bw = RTW_CHANNEL_WIDTH_20; cur_bw <= bw; cur_bw++) { 171993f68a86SZong-Zhe Yang cur_ch = cch_by_bw[cur_bw]; 172093f68a86SZong-Zhe Yang 172193f68a86SZong-Zhe Yang ch_idx = rtw_channel_to_idx(band, cur_ch); 1722fa6dfe6bSYan-Hsuan Chuang if (ch_idx < 0) 1723fa6dfe6bSYan-Hsuan Chuang goto err; 1724fa6dfe6bSYan-Hsuan Chuang 172593f68a86SZong-Zhe Yang cur_lmt = cur_ch <= RTW_MAX_CHANNEL_NUM_2G ? 172693f68a86SZong-Zhe Yang hal->tx_pwr_limit_2g[regd][cur_bw][rs][ch_idx] : 172793f68a86SZong-Zhe Yang hal->tx_pwr_limit_5g[regd][cur_bw][rs][ch_idx]; 172893f68a86SZong-Zhe Yang 172993f68a86SZong-Zhe Yang power_limit = min_t(s8, cur_lmt, power_limit); 173093f68a86SZong-Zhe Yang } 1731fa6dfe6bSYan-Hsuan Chuang 1732fa6dfe6bSYan-Hsuan Chuang return power_limit; 1733fa6dfe6bSYan-Hsuan Chuang 1734fa6dfe6bSYan-Hsuan Chuang err: 1735fa6dfe6bSYan-Hsuan Chuang WARN(1, "invalid arguments, band=%d, bw=%d, path=%d, rate=%d, ch=%d\n", 1736fa6dfe6bSYan-Hsuan Chuang band, bw, rf_path, rate, channel); 17370d350f0aSTzu-En Huang return (s8)rtwdev->chip->max_power_index; 1738fa6dfe6bSYan-Hsuan Chuang } 1739fa6dfe6bSYan-Hsuan Chuang 1740b7414222SZong-Zhe Yang void rtw_get_tx_power_params(struct rtw_dev *rtwdev, u8 path, u8 rate, u8 bw, 1741b7414222SZong-Zhe Yang u8 ch, u8 regd, struct rtw_power_params *pwr_param) 1742fa6dfe6bSYan-Hsuan Chuang { 1743fa6dfe6bSYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 1744fa6dfe6bSYan-Hsuan Chuang struct rtw_txpwr_idx *pwr_idx; 1745b7414222SZong-Zhe Yang u8 group, band; 1746b7414222SZong-Zhe Yang u8 *base = &pwr_param->pwr_base; 1747b7414222SZong-Zhe Yang s8 *offset = &pwr_param->pwr_offset; 1748b7414222SZong-Zhe Yang s8 *limit = &pwr_param->pwr_limit; 1749fa6dfe6bSYan-Hsuan Chuang 1750b7414222SZong-Zhe Yang pwr_idx = &rtwdev->efuse.txpwr_idx_table[path]; 1751b7414222SZong-Zhe Yang group = rtw_get_channel_group(ch); 1752fa6dfe6bSYan-Hsuan Chuang 1753fa6dfe6bSYan-Hsuan Chuang /* base power index for 2.4G/5G */ 17548575b534SYan-Hsuan Chuang if (IS_CH_2G_BAND(ch)) { 1755fa6dfe6bSYan-Hsuan Chuang band = PHY_BAND_2G; 1756b7414222SZong-Zhe Yang *base = rtw_phy_get_2g_tx_power_index(rtwdev, 1757fa6dfe6bSYan-Hsuan Chuang &pwr_idx->pwr_idx_2g, 1758b7414222SZong-Zhe Yang bw, rate, group); 1759b7414222SZong-Zhe Yang *offset = hal->tx_pwr_by_rate_offset_2g[path][rate]; 1760fa6dfe6bSYan-Hsuan Chuang } else { 1761fa6dfe6bSYan-Hsuan Chuang band = PHY_BAND_5G; 1762b7414222SZong-Zhe Yang *base = rtw_phy_get_5g_tx_power_index(rtwdev, 1763fa6dfe6bSYan-Hsuan Chuang &pwr_idx->pwr_idx_5g, 1764b7414222SZong-Zhe Yang bw, rate, group); 1765b7414222SZong-Zhe Yang *offset = hal->tx_pwr_by_rate_offset_5g[path][rate]; 1766fa6dfe6bSYan-Hsuan Chuang } 1767fa6dfe6bSYan-Hsuan Chuang 1768b7414222SZong-Zhe Yang *limit = rtw_phy_get_tx_power_limit(rtwdev, band, bw, path, 1769b7414222SZong-Zhe Yang rate, ch, regd); 1770b7414222SZong-Zhe Yang } 1771fa6dfe6bSYan-Hsuan Chuang 1772b7414222SZong-Zhe Yang u8 1773b7414222SZong-Zhe Yang rtw_phy_get_tx_power_index(struct rtw_dev *rtwdev, u8 rf_path, u8 rate, 1774b7414222SZong-Zhe Yang enum rtw_bandwidth bandwidth, u8 channel, u8 regd) 1775b7414222SZong-Zhe Yang { 1776b7414222SZong-Zhe Yang struct rtw_power_params pwr_param = {0}; 1777b7414222SZong-Zhe Yang u8 tx_power; 1778b7414222SZong-Zhe Yang s8 offset; 1779b7414222SZong-Zhe Yang 1780b7414222SZong-Zhe Yang rtw_get_tx_power_params(rtwdev, rf_path, rate, bandwidth, 1781b7414222SZong-Zhe Yang channel, regd, &pwr_param); 1782b7414222SZong-Zhe Yang 1783b7414222SZong-Zhe Yang tx_power = pwr_param.pwr_base; 1784b7414222SZong-Zhe Yang offset = min_t(s8, pwr_param.pwr_offset, pwr_param.pwr_limit); 1785fa6dfe6bSYan-Hsuan Chuang 17865227c2eeSTzu-En Huang if (rtwdev->chip->en_dis_dpd) 17875227c2eeSTzu-En Huang offset += rtw_phy_get_dis_dpd_by_rate_diff(rtwdev, rate); 17885227c2eeSTzu-En Huang 1789fa6dfe6bSYan-Hsuan Chuang tx_power += offset; 1790fa6dfe6bSYan-Hsuan Chuang 1791fa6dfe6bSYan-Hsuan Chuang if (tx_power > rtwdev->chip->max_power_index) 1792fa6dfe6bSYan-Hsuan Chuang tx_power = rtwdev->chip->max_power_index; 1793fa6dfe6bSYan-Hsuan Chuang 1794fa6dfe6bSYan-Hsuan Chuang return tx_power; 1795fa6dfe6bSYan-Hsuan Chuang } 1796fa6dfe6bSYan-Hsuan Chuang 179743712199SYan-Hsuan Chuang static void rtw_phy_set_tx_power_index_by_rs(struct rtw_dev *rtwdev, 1798226746fdSYan-Hsuan Chuang u8 ch, u8 path, u8 rs) 1799fa6dfe6bSYan-Hsuan Chuang { 1800fa6dfe6bSYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 1801fa6dfe6bSYan-Hsuan Chuang u8 regd = rtwdev->regd.txpwr_regd; 1802fa6dfe6bSYan-Hsuan Chuang u8 *rates; 1803fa6dfe6bSYan-Hsuan Chuang u8 size; 1804fa6dfe6bSYan-Hsuan Chuang u8 rate; 1805fa6dfe6bSYan-Hsuan Chuang u8 pwr_idx; 1806fa6dfe6bSYan-Hsuan Chuang u8 bw; 1807fa6dfe6bSYan-Hsuan Chuang int i; 1808fa6dfe6bSYan-Hsuan Chuang 1809fa6dfe6bSYan-Hsuan Chuang if (rs >= RTW_RATE_SECTION_MAX) 1810fa6dfe6bSYan-Hsuan Chuang return; 1811fa6dfe6bSYan-Hsuan Chuang 1812fa6dfe6bSYan-Hsuan Chuang rates = rtw_rate_section[rs]; 1813fa6dfe6bSYan-Hsuan Chuang size = rtw_rate_size[rs]; 1814fa6dfe6bSYan-Hsuan Chuang bw = hal->current_band_width; 1815fa6dfe6bSYan-Hsuan Chuang for (i = 0; i < size; i++) { 1816fa6dfe6bSYan-Hsuan Chuang rate = rates[i]; 181743712199SYan-Hsuan Chuang pwr_idx = rtw_phy_get_tx_power_index(rtwdev, path, rate, 181843712199SYan-Hsuan Chuang bw, ch, regd); 1819fa6dfe6bSYan-Hsuan Chuang hal->tx_pwr_tbl[path][rate] = pwr_idx; 1820fa6dfe6bSYan-Hsuan Chuang } 1821fa6dfe6bSYan-Hsuan Chuang } 1822fa6dfe6bSYan-Hsuan Chuang 1823fa6dfe6bSYan-Hsuan Chuang /* set tx power level by path for each rates, note that the order of the rates 1824fa6dfe6bSYan-Hsuan Chuang * are *very* important, bacause 8822B/8821C combines every four bytes of tx 1825fa6dfe6bSYan-Hsuan Chuang * power index into a four-byte power index register, and calls set_tx_agc to 1826fa6dfe6bSYan-Hsuan Chuang * write these values into hardware 1827fa6dfe6bSYan-Hsuan Chuang */ 182843712199SYan-Hsuan Chuang static void rtw_phy_set_tx_power_level_by_path(struct rtw_dev *rtwdev, 182943712199SYan-Hsuan Chuang u8 ch, u8 path) 1830fa6dfe6bSYan-Hsuan Chuang { 1831fa6dfe6bSYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 1832fa6dfe6bSYan-Hsuan Chuang u8 rs; 1833fa6dfe6bSYan-Hsuan Chuang 1834fa6dfe6bSYan-Hsuan Chuang /* do not need cck rates if we are not in 2.4G */ 1835fa6dfe6bSYan-Hsuan Chuang if (hal->current_band_type == RTW_BAND_2G) 1836fa6dfe6bSYan-Hsuan Chuang rs = RTW_RATE_SECTION_CCK; 1837fa6dfe6bSYan-Hsuan Chuang else 1838fa6dfe6bSYan-Hsuan Chuang rs = RTW_RATE_SECTION_OFDM; 1839fa6dfe6bSYan-Hsuan Chuang 1840fa6dfe6bSYan-Hsuan Chuang for (; rs < RTW_RATE_SECTION_MAX; rs++) 184143712199SYan-Hsuan Chuang rtw_phy_set_tx_power_index_by_rs(rtwdev, ch, path, rs); 1842fa6dfe6bSYan-Hsuan Chuang } 1843fa6dfe6bSYan-Hsuan Chuang 1844fa6dfe6bSYan-Hsuan Chuang void rtw_phy_set_tx_power_level(struct rtw_dev *rtwdev, u8 channel) 1845fa6dfe6bSYan-Hsuan Chuang { 1846fa6dfe6bSYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 1847fa6dfe6bSYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 1848fa6dfe6bSYan-Hsuan Chuang u8 path; 1849fa6dfe6bSYan-Hsuan Chuang 1850fa6dfe6bSYan-Hsuan Chuang mutex_lock(&hal->tx_power_mutex); 1851fa6dfe6bSYan-Hsuan Chuang 1852fa6dfe6bSYan-Hsuan Chuang for (path = 0; path < hal->rf_path_num; path++) 185343712199SYan-Hsuan Chuang rtw_phy_set_tx_power_level_by_path(rtwdev, channel, path); 1854fa6dfe6bSYan-Hsuan Chuang 1855fa6dfe6bSYan-Hsuan Chuang chip->ops->set_tx_power_index(rtwdev); 1856fa6dfe6bSYan-Hsuan Chuang mutex_unlock(&hal->tx_power_mutex); 1857fa6dfe6bSYan-Hsuan Chuang } 1858fa6dfe6bSYan-Hsuan Chuang 185943712199SYan-Hsuan Chuang static void 186043712199SYan-Hsuan Chuang rtw_phy_tx_power_by_rate_config_by_path(struct rtw_hal *hal, u8 path, 1861e3037485SYan-Hsuan Chuang u8 rs, u8 size, u8 *rates) 1862e3037485SYan-Hsuan Chuang { 1863e3037485SYan-Hsuan Chuang u8 rate; 1864e3037485SYan-Hsuan Chuang u8 base_idx, rate_idx; 1865e3037485SYan-Hsuan Chuang s8 base_2g, base_5g; 1866e3037485SYan-Hsuan Chuang 1867e3037485SYan-Hsuan Chuang if (rs >= RTW_RATE_SECTION_VHT_1S) 1868e3037485SYan-Hsuan Chuang base_idx = rates[size - 3]; 1869e3037485SYan-Hsuan Chuang else 1870e3037485SYan-Hsuan Chuang base_idx = rates[size - 1]; 1871e3037485SYan-Hsuan Chuang base_2g = hal->tx_pwr_by_rate_offset_2g[path][base_idx]; 1872e3037485SYan-Hsuan Chuang base_5g = hal->tx_pwr_by_rate_offset_5g[path][base_idx]; 1873e3037485SYan-Hsuan Chuang hal->tx_pwr_by_rate_base_2g[path][rs] = base_2g; 1874e3037485SYan-Hsuan Chuang hal->tx_pwr_by_rate_base_5g[path][rs] = base_5g; 1875e3037485SYan-Hsuan Chuang for (rate = 0; rate < size; rate++) { 1876e3037485SYan-Hsuan Chuang rate_idx = rates[rate]; 1877e3037485SYan-Hsuan Chuang hal->tx_pwr_by_rate_offset_2g[path][rate_idx] -= base_2g; 1878e3037485SYan-Hsuan Chuang hal->tx_pwr_by_rate_offset_5g[path][rate_idx] -= base_5g; 1879e3037485SYan-Hsuan Chuang } 1880e3037485SYan-Hsuan Chuang } 1881e3037485SYan-Hsuan Chuang 1882e3037485SYan-Hsuan Chuang void rtw_phy_tx_power_by_rate_config(struct rtw_hal *hal) 1883e3037485SYan-Hsuan Chuang { 1884e3037485SYan-Hsuan Chuang u8 path; 1885e3037485SYan-Hsuan Chuang 1886e3037485SYan-Hsuan Chuang for (path = 0; path < RTW_RF_PATH_MAX; path++) { 188743712199SYan-Hsuan Chuang rtw_phy_tx_power_by_rate_config_by_path(hal, path, 1888e3037485SYan-Hsuan Chuang RTW_RATE_SECTION_CCK, 1889e3037485SYan-Hsuan Chuang rtw_cck_size, rtw_cck_rates); 189043712199SYan-Hsuan Chuang rtw_phy_tx_power_by_rate_config_by_path(hal, path, 1891e3037485SYan-Hsuan Chuang RTW_RATE_SECTION_OFDM, 1892e3037485SYan-Hsuan Chuang rtw_ofdm_size, rtw_ofdm_rates); 189343712199SYan-Hsuan Chuang rtw_phy_tx_power_by_rate_config_by_path(hal, path, 1894e3037485SYan-Hsuan Chuang RTW_RATE_SECTION_HT_1S, 1895e3037485SYan-Hsuan Chuang rtw_ht_1s_size, rtw_ht_1s_rates); 189643712199SYan-Hsuan Chuang rtw_phy_tx_power_by_rate_config_by_path(hal, path, 1897e3037485SYan-Hsuan Chuang RTW_RATE_SECTION_HT_2S, 1898e3037485SYan-Hsuan Chuang rtw_ht_2s_size, rtw_ht_2s_rates); 189943712199SYan-Hsuan Chuang rtw_phy_tx_power_by_rate_config_by_path(hal, path, 1900e3037485SYan-Hsuan Chuang RTW_RATE_SECTION_VHT_1S, 1901e3037485SYan-Hsuan Chuang rtw_vht_1s_size, rtw_vht_1s_rates); 190243712199SYan-Hsuan Chuang rtw_phy_tx_power_by_rate_config_by_path(hal, path, 1903e3037485SYan-Hsuan Chuang RTW_RATE_SECTION_VHT_2S, 1904e3037485SYan-Hsuan Chuang rtw_vht_2s_size, rtw_vht_2s_rates); 1905e3037485SYan-Hsuan Chuang } 1906e3037485SYan-Hsuan Chuang } 1907e3037485SYan-Hsuan Chuang 1908e3037485SYan-Hsuan Chuang static void 190943712199SYan-Hsuan Chuang __rtw_phy_tx_power_limit_config(struct rtw_hal *hal, u8 regd, u8 bw, u8 rs) 1910e3037485SYan-Hsuan Chuang { 191152280149SYan-Hsuan Chuang s8 base; 1912e3037485SYan-Hsuan Chuang u8 ch; 1913e3037485SYan-Hsuan Chuang 1914e3037485SYan-Hsuan Chuang for (ch = 0; ch < RTW_MAX_CHANNEL_NUM_2G; ch++) { 1915e3037485SYan-Hsuan Chuang base = hal->tx_pwr_by_rate_base_2g[0][rs]; 1916e3037485SYan-Hsuan Chuang hal->tx_pwr_limit_2g[regd][bw][rs][ch] -= base; 1917e3037485SYan-Hsuan Chuang } 1918e3037485SYan-Hsuan Chuang 1919e3037485SYan-Hsuan Chuang for (ch = 0; ch < RTW_MAX_CHANNEL_NUM_5G; ch++) { 1920e3037485SYan-Hsuan Chuang base = hal->tx_pwr_by_rate_base_5g[0][rs]; 1921e3037485SYan-Hsuan Chuang hal->tx_pwr_limit_5g[regd][bw][rs][ch] -= base; 1922e3037485SYan-Hsuan Chuang } 1923e3037485SYan-Hsuan Chuang } 1924e3037485SYan-Hsuan Chuang 1925e3037485SYan-Hsuan Chuang void rtw_phy_tx_power_limit_config(struct rtw_hal *hal) 1926e3037485SYan-Hsuan Chuang { 1927e3037485SYan-Hsuan Chuang u8 regd, bw, rs; 1928e3037485SYan-Hsuan Chuang 192993f68a86SZong-Zhe Yang /* default at channel 1 */ 193093f68a86SZong-Zhe Yang hal->cch_by_bw[RTW_CHANNEL_WIDTH_20] = 1; 193193f68a86SZong-Zhe Yang 1932e3037485SYan-Hsuan Chuang for (regd = 0; regd < RTW_REGD_MAX; regd++) 1933e3037485SYan-Hsuan Chuang for (bw = 0; bw < RTW_CHANNEL_WIDTH_MAX; bw++) 1934e3037485SYan-Hsuan Chuang for (rs = 0; rs < RTW_RATE_SECTION_MAX; rs++) 193543712199SYan-Hsuan Chuang __rtw_phy_tx_power_limit_config(hal, regd, bw, rs); 1936e3037485SYan-Hsuan Chuang } 1937e3037485SYan-Hsuan Chuang 19380d350f0aSTzu-En Huang static void rtw_phy_init_tx_power_limit(struct rtw_dev *rtwdev, 193943712199SYan-Hsuan Chuang u8 regd, u8 bw, u8 rs) 1940e3037485SYan-Hsuan Chuang { 19410d350f0aSTzu-En Huang struct rtw_hal *hal = &rtwdev->hal; 19420d350f0aSTzu-En Huang s8 max_power_index = (s8)rtwdev->chip->max_power_index; 1943e3037485SYan-Hsuan Chuang u8 ch; 1944e3037485SYan-Hsuan Chuang 1945e3037485SYan-Hsuan Chuang /* 2.4G channels */ 1946e3037485SYan-Hsuan Chuang for (ch = 0; ch < RTW_MAX_CHANNEL_NUM_2G; ch++) 19470d350f0aSTzu-En Huang hal->tx_pwr_limit_2g[regd][bw][rs][ch] = max_power_index; 1948e3037485SYan-Hsuan Chuang 1949e3037485SYan-Hsuan Chuang /* 5G channels */ 1950e3037485SYan-Hsuan Chuang for (ch = 0; ch < RTW_MAX_CHANNEL_NUM_5G; ch++) 19510d350f0aSTzu-En Huang hal->tx_pwr_limit_5g[regd][bw][rs][ch] = max_power_index; 1952e3037485SYan-Hsuan Chuang } 1953e3037485SYan-Hsuan Chuang 19540d350f0aSTzu-En Huang void rtw_phy_init_tx_power(struct rtw_dev *rtwdev) 1955e3037485SYan-Hsuan Chuang { 19560d350f0aSTzu-En Huang struct rtw_hal *hal = &rtwdev->hal; 1957e3037485SYan-Hsuan Chuang u8 regd, path, rate, rs, bw; 1958e3037485SYan-Hsuan Chuang 1959e3037485SYan-Hsuan Chuang /* init tx power by rate offset */ 1960e3037485SYan-Hsuan Chuang for (path = 0; path < RTW_RF_PATH_MAX; path++) { 1961e3037485SYan-Hsuan Chuang for (rate = 0; rate < DESC_RATE_MAX; rate++) { 1962e3037485SYan-Hsuan Chuang hal->tx_pwr_by_rate_offset_2g[path][rate] = 0; 1963e3037485SYan-Hsuan Chuang hal->tx_pwr_by_rate_offset_5g[path][rate] = 0; 1964e3037485SYan-Hsuan Chuang } 1965e3037485SYan-Hsuan Chuang } 1966e3037485SYan-Hsuan Chuang 1967e3037485SYan-Hsuan Chuang /* init tx power limit */ 1968e3037485SYan-Hsuan Chuang for (regd = 0; regd < RTW_REGD_MAX; regd++) 1969e3037485SYan-Hsuan Chuang for (bw = 0; bw < RTW_CHANNEL_WIDTH_MAX; bw++) 1970e3037485SYan-Hsuan Chuang for (rs = 0; rs < RTW_RATE_SECTION_MAX; rs++) 19710d350f0aSTzu-En Huang rtw_phy_init_tx_power_limit(rtwdev, regd, bw, 19720d350f0aSTzu-En Huang rs); 1973e3037485SYan-Hsuan Chuang } 1974c97ee3e0STzu-En Huang 1975c97ee3e0STzu-En Huang void rtw_phy_config_swing_table(struct rtw_dev *rtwdev, 1976c97ee3e0STzu-En Huang struct rtw_swing_table *swing_table) 1977c97ee3e0STzu-En Huang { 1978c97ee3e0STzu-En Huang const struct rtw_pwr_track_tbl *tbl = rtwdev->chip->pwr_track_tbl; 1979c97ee3e0STzu-En Huang u8 channel = rtwdev->hal.current_channel; 1980c97ee3e0STzu-En Huang 1981c97ee3e0STzu-En Huang if (IS_CH_2G_BAND(channel)) { 1982c97ee3e0STzu-En Huang if (rtwdev->dm_info.tx_rate <= DESC_RATE11M) { 1983c97ee3e0STzu-En Huang swing_table->p[RF_PATH_A] = tbl->pwrtrk_2g_ccka_p; 1984c97ee3e0STzu-En Huang swing_table->n[RF_PATH_A] = tbl->pwrtrk_2g_ccka_n; 1985c97ee3e0STzu-En Huang swing_table->p[RF_PATH_B] = tbl->pwrtrk_2g_cckb_p; 1986c97ee3e0STzu-En Huang swing_table->n[RF_PATH_B] = tbl->pwrtrk_2g_cckb_n; 1987c97ee3e0STzu-En Huang } else { 1988c97ee3e0STzu-En Huang swing_table->p[RF_PATH_A] = tbl->pwrtrk_2ga_p; 1989c97ee3e0STzu-En Huang swing_table->n[RF_PATH_A] = tbl->pwrtrk_2ga_n; 1990c97ee3e0STzu-En Huang swing_table->p[RF_PATH_B] = tbl->pwrtrk_2gb_p; 1991c97ee3e0STzu-En Huang swing_table->n[RF_PATH_B] = tbl->pwrtrk_2gb_n; 1992c97ee3e0STzu-En Huang } 1993c97ee3e0STzu-En Huang } else if (IS_CH_5G_BAND_1(channel) || IS_CH_5G_BAND_2(channel)) { 1994c97ee3e0STzu-En Huang swing_table->p[RF_PATH_A] = tbl->pwrtrk_5ga_p[RTW_PWR_TRK_5G_1]; 1995c97ee3e0STzu-En Huang swing_table->n[RF_PATH_A] = tbl->pwrtrk_5ga_n[RTW_PWR_TRK_5G_1]; 1996c97ee3e0STzu-En Huang swing_table->p[RF_PATH_B] = tbl->pwrtrk_5gb_p[RTW_PWR_TRK_5G_1]; 1997c97ee3e0STzu-En Huang swing_table->n[RF_PATH_B] = tbl->pwrtrk_5gb_n[RTW_PWR_TRK_5G_1]; 1998c97ee3e0STzu-En Huang } else if (IS_CH_5G_BAND_3(channel)) { 1999c97ee3e0STzu-En Huang swing_table->p[RF_PATH_A] = tbl->pwrtrk_5ga_p[RTW_PWR_TRK_5G_2]; 2000c97ee3e0STzu-En Huang swing_table->n[RF_PATH_A] = tbl->pwrtrk_5ga_n[RTW_PWR_TRK_5G_2]; 2001c97ee3e0STzu-En Huang swing_table->p[RF_PATH_B] = tbl->pwrtrk_5gb_p[RTW_PWR_TRK_5G_2]; 2002c97ee3e0STzu-En Huang swing_table->n[RF_PATH_B] = tbl->pwrtrk_5gb_n[RTW_PWR_TRK_5G_2]; 2003c97ee3e0STzu-En Huang } else if (IS_CH_5G_BAND_4(channel)) { 2004c97ee3e0STzu-En Huang swing_table->p[RF_PATH_A] = tbl->pwrtrk_5ga_p[RTW_PWR_TRK_5G_3]; 2005c97ee3e0STzu-En Huang swing_table->n[RF_PATH_A] = tbl->pwrtrk_5ga_n[RTW_PWR_TRK_5G_3]; 2006c97ee3e0STzu-En Huang swing_table->p[RF_PATH_B] = tbl->pwrtrk_5gb_p[RTW_PWR_TRK_5G_3]; 2007c97ee3e0STzu-En Huang swing_table->n[RF_PATH_B] = tbl->pwrtrk_5gb_n[RTW_PWR_TRK_5G_3]; 2008c97ee3e0STzu-En Huang } else { 2009c97ee3e0STzu-En Huang swing_table->p[RF_PATH_A] = tbl->pwrtrk_2ga_p; 2010c97ee3e0STzu-En Huang swing_table->n[RF_PATH_A] = tbl->pwrtrk_2ga_n; 2011c97ee3e0STzu-En Huang swing_table->p[RF_PATH_B] = tbl->pwrtrk_2gb_p; 2012c97ee3e0STzu-En Huang swing_table->n[RF_PATH_B] = tbl->pwrtrk_2gb_n; 2013c97ee3e0STzu-En Huang } 2014c97ee3e0STzu-En Huang } 2015c97ee3e0STzu-En Huang 2016c97ee3e0STzu-En Huang void rtw_phy_pwrtrack_avg(struct rtw_dev *rtwdev, u8 thermal, u8 path) 2017c97ee3e0STzu-En Huang { 2018c97ee3e0STzu-En Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 2019c97ee3e0STzu-En Huang 2020c97ee3e0STzu-En Huang ewma_thermal_add(&dm_info->avg_thermal[path], thermal); 2021c97ee3e0STzu-En Huang dm_info->thermal_avg[path] = 2022c97ee3e0STzu-En Huang ewma_thermal_read(&dm_info->avg_thermal[path]); 2023c97ee3e0STzu-En Huang } 2024c97ee3e0STzu-En Huang 2025c97ee3e0STzu-En Huang bool rtw_phy_pwrtrack_thermal_changed(struct rtw_dev *rtwdev, u8 thermal, 2026c97ee3e0STzu-En Huang u8 path) 2027c97ee3e0STzu-En Huang { 2028c97ee3e0STzu-En Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 2029c97ee3e0STzu-En Huang u8 avg = ewma_thermal_read(&dm_info->avg_thermal[path]); 2030c97ee3e0STzu-En Huang 2031c97ee3e0STzu-En Huang if (avg == thermal) 2032c97ee3e0STzu-En Huang return false; 2033c97ee3e0STzu-En Huang 2034c97ee3e0STzu-En Huang return true; 2035c97ee3e0STzu-En Huang } 2036c97ee3e0STzu-En Huang 2037c97ee3e0STzu-En Huang u8 rtw_phy_pwrtrack_get_delta(struct rtw_dev *rtwdev, u8 path) 2038c97ee3e0STzu-En Huang { 2039c97ee3e0STzu-En Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 2040c97ee3e0STzu-En Huang u8 therm_avg, therm_efuse, therm_delta; 2041c97ee3e0STzu-En Huang 2042c97ee3e0STzu-En Huang therm_avg = dm_info->thermal_avg[path]; 2043c97ee3e0STzu-En Huang therm_efuse = rtwdev->efuse.thermal_meter[path]; 2044c97ee3e0STzu-En Huang therm_delta = abs(therm_avg - therm_efuse); 2045c97ee3e0STzu-En Huang 2046c97ee3e0STzu-En Huang return min_t(u8, therm_delta, RTW_PWR_TRK_TBL_SZ - 1); 2047c97ee3e0STzu-En Huang } 2048c97ee3e0STzu-En Huang 2049c97ee3e0STzu-En Huang s8 rtw_phy_pwrtrack_get_pwridx(struct rtw_dev *rtwdev, 2050c97ee3e0STzu-En Huang struct rtw_swing_table *swing_table, 2051c97ee3e0STzu-En Huang u8 tbl_path, u8 therm_path, u8 delta) 2052c97ee3e0STzu-En Huang { 2053c97ee3e0STzu-En Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 2054c97ee3e0STzu-En Huang const u8 *delta_swing_table_idx_pos; 2055c97ee3e0STzu-En Huang const u8 *delta_swing_table_idx_neg; 2056c97ee3e0STzu-En Huang 2057c97ee3e0STzu-En Huang if (delta >= RTW_PWR_TRK_TBL_SZ) { 2058c97ee3e0STzu-En Huang rtw_warn(rtwdev, "power track table overflow\n"); 2059c97ee3e0STzu-En Huang return 0; 2060c97ee3e0STzu-En Huang } 2061c97ee3e0STzu-En Huang 2062baff8da6SColin Ian King if (!swing_table) { 2063c97ee3e0STzu-En Huang rtw_warn(rtwdev, "swing table not configured\n"); 2064c97ee3e0STzu-En Huang return 0; 2065c97ee3e0STzu-En Huang } 2066c97ee3e0STzu-En Huang 2067c97ee3e0STzu-En Huang delta_swing_table_idx_pos = swing_table->p[tbl_path]; 2068c97ee3e0STzu-En Huang delta_swing_table_idx_neg = swing_table->n[tbl_path]; 2069c97ee3e0STzu-En Huang 2070c97ee3e0STzu-En Huang if (!delta_swing_table_idx_pos || !delta_swing_table_idx_neg) { 2071c97ee3e0STzu-En Huang rtw_warn(rtwdev, "invalid swing table index\n"); 2072c97ee3e0STzu-En Huang return 0; 2073c97ee3e0STzu-En Huang } 2074c97ee3e0STzu-En Huang 2075c97ee3e0STzu-En Huang if (dm_info->thermal_avg[therm_path] > 2076c97ee3e0STzu-En Huang rtwdev->efuse.thermal_meter[therm_path]) 2077c97ee3e0STzu-En Huang return delta_swing_table_idx_pos[delta]; 2078c97ee3e0STzu-En Huang else 2079c97ee3e0STzu-En Huang return -delta_swing_table_idx_neg[delta]; 2080c97ee3e0STzu-En Huang } 2081c97ee3e0STzu-En Huang 2082c97ee3e0STzu-En Huang bool rtw_phy_pwrtrack_need_iqk(struct rtw_dev *rtwdev) 2083c97ee3e0STzu-En Huang { 2084c97ee3e0STzu-En Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 2085c97ee3e0STzu-En Huang u8 delta_iqk; 2086c97ee3e0STzu-En Huang 2087c97ee3e0STzu-En Huang delta_iqk = abs(dm_info->thermal_avg[0] - dm_info->thermal_meter_k); 2088c97ee3e0STzu-En Huang if (delta_iqk >= rtwdev->chip->iqk_threshold) { 2089c97ee3e0STzu-En Huang dm_info->thermal_meter_k = dm_info->thermal_avg[0]; 2090c97ee3e0STzu-En Huang return true; 2091c97ee3e0STzu-En Huang } 2092c97ee3e0STzu-En Huang return false; 2093c97ee3e0STzu-En Huang } 2094