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" 12f8509c38SZong-Zhe Yang #include "regd.h" 138704d0beSZong-Zhe Yang #include "sar.h" 14e3037485SYan-Hsuan Chuang 15e3037485SYan-Hsuan Chuang struct phy_cfg_pair { 16e3037485SYan-Hsuan Chuang u32 addr; 17e3037485SYan-Hsuan Chuang u32 data; 18e3037485SYan-Hsuan Chuang }; 19e3037485SYan-Hsuan Chuang 20e3037485SYan-Hsuan Chuang union phy_table_tile { 21e3037485SYan-Hsuan Chuang struct rtw_phy_cond cond; 22e3037485SYan-Hsuan Chuang struct phy_cfg_pair cfg; 23e3037485SYan-Hsuan Chuang }; 24e3037485SYan-Hsuan Chuang 25e3037485SYan-Hsuan Chuang static const u32 db_invert_table[12][8] = { 26e3037485SYan-Hsuan Chuang {10, 13, 16, 20, 27e3037485SYan-Hsuan Chuang 25, 32, 40, 50}, 28e3037485SYan-Hsuan Chuang {64, 80, 101, 128, 29e3037485SYan-Hsuan Chuang 160, 201, 256, 318}, 30e3037485SYan-Hsuan Chuang {401, 505, 635, 800, 31e3037485SYan-Hsuan Chuang 1007, 1268, 1596, 2010}, 32e3037485SYan-Hsuan Chuang {316, 398, 501, 631, 33e3037485SYan-Hsuan Chuang 794, 1000, 1259, 1585}, 34e3037485SYan-Hsuan Chuang {1995, 2512, 3162, 3981, 35e3037485SYan-Hsuan Chuang 5012, 6310, 7943, 10000}, 36e3037485SYan-Hsuan Chuang {12589, 15849, 19953, 25119, 37e3037485SYan-Hsuan Chuang 31623, 39811, 50119, 63098}, 38e3037485SYan-Hsuan Chuang {79433, 100000, 125893, 158489, 39e3037485SYan-Hsuan Chuang 199526, 251189, 316228, 398107}, 40e3037485SYan-Hsuan Chuang {501187, 630957, 794328, 1000000, 41e3037485SYan-Hsuan Chuang 1258925, 1584893, 1995262, 2511886}, 42e3037485SYan-Hsuan Chuang {3162278, 3981072, 5011872, 6309573, 43e3037485SYan-Hsuan Chuang 7943282, 1000000, 12589254, 15848932}, 44e3037485SYan-Hsuan Chuang {19952623, 25118864, 31622777, 39810717, 45e3037485SYan-Hsuan Chuang 50118723, 63095734, 79432823, 100000000}, 46e3037485SYan-Hsuan Chuang {125892541, 158489319, 199526232, 251188643, 47e3037485SYan-Hsuan Chuang 316227766, 398107171, 501187234, 630957345}, 48e3037485SYan-Hsuan Chuang {794328235, 1000000000, 1258925412, 1584893192, 49e3037485SYan-Hsuan Chuang 1995262315, 2511886432U, 3162277660U, 3981071706U} 50e3037485SYan-Hsuan Chuang }; 51e3037485SYan-Hsuan Chuang 52fa6dfe6bSYan-Hsuan Chuang u8 rtw_cck_rates[] = { DESC_RATE1M, DESC_RATE2M, DESC_RATE5_5M, DESC_RATE11M }; 53fa6dfe6bSYan-Hsuan Chuang u8 rtw_ofdm_rates[] = { 54fa6dfe6bSYan-Hsuan Chuang DESC_RATE6M, DESC_RATE9M, DESC_RATE12M, 55fa6dfe6bSYan-Hsuan Chuang DESC_RATE18M, DESC_RATE24M, DESC_RATE36M, 56fa6dfe6bSYan-Hsuan Chuang DESC_RATE48M, DESC_RATE54M 57fa6dfe6bSYan-Hsuan Chuang }; 58fa6dfe6bSYan-Hsuan Chuang u8 rtw_ht_1s_rates[] = { 59fa6dfe6bSYan-Hsuan Chuang DESC_RATEMCS0, DESC_RATEMCS1, DESC_RATEMCS2, 60fa6dfe6bSYan-Hsuan Chuang DESC_RATEMCS3, DESC_RATEMCS4, DESC_RATEMCS5, 61fa6dfe6bSYan-Hsuan Chuang DESC_RATEMCS6, DESC_RATEMCS7 62fa6dfe6bSYan-Hsuan Chuang }; 63fa6dfe6bSYan-Hsuan Chuang u8 rtw_ht_2s_rates[] = { 64fa6dfe6bSYan-Hsuan Chuang DESC_RATEMCS8, DESC_RATEMCS9, DESC_RATEMCS10, 65fa6dfe6bSYan-Hsuan Chuang DESC_RATEMCS11, DESC_RATEMCS12, DESC_RATEMCS13, 66fa6dfe6bSYan-Hsuan Chuang DESC_RATEMCS14, DESC_RATEMCS15 67fa6dfe6bSYan-Hsuan Chuang }; 68fa6dfe6bSYan-Hsuan Chuang u8 rtw_vht_1s_rates[] = { 69fa6dfe6bSYan-Hsuan Chuang DESC_RATEVHT1SS_MCS0, DESC_RATEVHT1SS_MCS1, 70fa6dfe6bSYan-Hsuan Chuang DESC_RATEVHT1SS_MCS2, DESC_RATEVHT1SS_MCS3, 71fa6dfe6bSYan-Hsuan Chuang DESC_RATEVHT1SS_MCS4, DESC_RATEVHT1SS_MCS5, 72fa6dfe6bSYan-Hsuan Chuang DESC_RATEVHT1SS_MCS6, DESC_RATEVHT1SS_MCS7, 73fa6dfe6bSYan-Hsuan Chuang DESC_RATEVHT1SS_MCS8, DESC_RATEVHT1SS_MCS9 74fa6dfe6bSYan-Hsuan Chuang }; 75fa6dfe6bSYan-Hsuan Chuang u8 rtw_vht_2s_rates[] = { 76fa6dfe6bSYan-Hsuan Chuang DESC_RATEVHT2SS_MCS0, DESC_RATEVHT2SS_MCS1, 77fa6dfe6bSYan-Hsuan Chuang DESC_RATEVHT2SS_MCS2, DESC_RATEVHT2SS_MCS3, 78fa6dfe6bSYan-Hsuan Chuang DESC_RATEVHT2SS_MCS4, DESC_RATEVHT2SS_MCS5, 79fa6dfe6bSYan-Hsuan Chuang DESC_RATEVHT2SS_MCS6, DESC_RATEVHT2SS_MCS7, 80fa6dfe6bSYan-Hsuan Chuang DESC_RATEVHT2SS_MCS8, DESC_RATEVHT2SS_MCS9 81fa6dfe6bSYan-Hsuan Chuang }; 82fa6dfe6bSYan-Hsuan Chuang u8 *rtw_rate_section[RTW_RATE_SECTION_MAX] = { 83fa6dfe6bSYan-Hsuan Chuang rtw_cck_rates, rtw_ofdm_rates, 84fa6dfe6bSYan-Hsuan Chuang rtw_ht_1s_rates, rtw_ht_2s_rates, 85fa6dfe6bSYan-Hsuan Chuang rtw_vht_1s_rates, rtw_vht_2s_rates 86fa6dfe6bSYan-Hsuan Chuang }; 87449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_rate_section); 88449be866SZong-Zhe Yang 89fa6dfe6bSYan-Hsuan Chuang u8 rtw_rate_size[RTW_RATE_SECTION_MAX] = { 90fa6dfe6bSYan-Hsuan Chuang ARRAY_SIZE(rtw_cck_rates), 91fa6dfe6bSYan-Hsuan Chuang ARRAY_SIZE(rtw_ofdm_rates), 92fa6dfe6bSYan-Hsuan Chuang ARRAY_SIZE(rtw_ht_1s_rates), 93fa6dfe6bSYan-Hsuan Chuang ARRAY_SIZE(rtw_ht_2s_rates), 94fa6dfe6bSYan-Hsuan Chuang ARRAY_SIZE(rtw_vht_1s_rates), 95fa6dfe6bSYan-Hsuan Chuang ARRAY_SIZE(rtw_vht_2s_rates) 96fa6dfe6bSYan-Hsuan Chuang }; 97449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_rate_size); 98449be866SZong-Zhe Yang 99fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_cck_size = ARRAY_SIZE(rtw_cck_rates); 100fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_ofdm_size = ARRAY_SIZE(rtw_ofdm_rates); 101fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_ht_1s_size = ARRAY_SIZE(rtw_ht_1s_rates); 102fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_ht_2s_size = ARRAY_SIZE(rtw_ht_2s_rates); 103fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_vht_1s_size = ARRAY_SIZE(rtw_vht_1s_rates); 104fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_vht_2s_size = ARRAY_SIZE(rtw_vht_2s_rates); 105fa6dfe6bSYan-Hsuan Chuang 106e3037485SYan-Hsuan Chuang enum rtw_phy_band_type { 107e3037485SYan-Hsuan Chuang PHY_BAND_2G = 0, 108e3037485SYan-Hsuan Chuang PHY_BAND_5G = 1, 109e3037485SYan-Hsuan Chuang }; 110e3037485SYan-Hsuan Chuang 111479c4ee9STzu-En Huang static void rtw_phy_cck_pd_init(struct rtw_dev *rtwdev) 112479c4ee9STzu-En Huang { 113479c4ee9STzu-En Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 114479c4ee9STzu-En Huang u8 i, j; 115479c4ee9STzu-En Huang 116479c4ee9STzu-En Huang for (i = 0; i <= RTW_CHANNEL_WIDTH_40; i++) { 117479c4ee9STzu-En Huang for (j = 0; j < RTW_RF_PATH_MAX; j++) 11818a0696eSTzu-En Huang dm_info->cck_pd_lv[i][j] = CCK_PD_LV0; 119479c4ee9STzu-En Huang } 120479c4ee9STzu-En Huang 121479c4ee9STzu-En Huang dm_info->cck_fa_avg = CCK_FA_AVG_RESET; 122479c4ee9STzu-En Huang } 123479c4ee9STzu-En Huang 1247285eb96SZong-Zhe Yang void rtw_phy_set_edcca_th(struct rtw_dev *rtwdev, u8 l2h, u8 h2l) 1257285eb96SZong-Zhe Yang { 1267285eb96SZong-Zhe Yang struct rtw_hw_reg_offset *edcca_th = rtwdev->chip->edcca_th; 1277285eb96SZong-Zhe Yang 1287285eb96SZong-Zhe Yang rtw_write32_mask(rtwdev, 1297285eb96SZong-Zhe Yang edcca_th[EDCCA_TH_L2H_IDX].hw_reg.addr, 1307285eb96SZong-Zhe Yang edcca_th[EDCCA_TH_L2H_IDX].hw_reg.mask, 1317285eb96SZong-Zhe Yang l2h + edcca_th[EDCCA_TH_L2H_IDX].offset); 1327285eb96SZong-Zhe Yang rtw_write32_mask(rtwdev, 1337285eb96SZong-Zhe Yang edcca_th[EDCCA_TH_H2L_IDX].hw_reg.addr, 1347285eb96SZong-Zhe Yang edcca_th[EDCCA_TH_H2L_IDX].hw_reg.mask, 1357285eb96SZong-Zhe Yang h2l + edcca_th[EDCCA_TH_H2L_IDX].offset); 1367285eb96SZong-Zhe Yang } 1377285eb96SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_set_edcca_th); 1387285eb96SZong-Zhe Yang 1397285eb96SZong-Zhe Yang void rtw_phy_adaptivity_set_mode(struct rtw_dev *rtwdev) 1407285eb96SZong-Zhe Yang { 1417285eb96SZong-Zhe Yang struct rtw_chip_info *chip = rtwdev->chip; 1427285eb96SZong-Zhe Yang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 1437285eb96SZong-Zhe Yang 1447285eb96SZong-Zhe Yang /* turn off in debugfs for debug usage */ 1457285eb96SZong-Zhe Yang if (!rtw_edcca_enabled) { 1467285eb96SZong-Zhe Yang dm_info->edcca_mode = RTW_EDCCA_NORMAL; 1477285eb96SZong-Zhe Yang rtw_dbg(rtwdev, RTW_DBG_PHY, "EDCCA disabled, cannot be set\n"); 1487285eb96SZong-Zhe Yang return; 1497285eb96SZong-Zhe Yang } 1507285eb96SZong-Zhe Yang 1517285eb96SZong-Zhe Yang switch (rtwdev->regd.dfs_region) { 1527285eb96SZong-Zhe Yang case NL80211_DFS_ETSI: 1537285eb96SZong-Zhe Yang dm_info->edcca_mode = RTW_EDCCA_ADAPTIVITY; 1547285eb96SZong-Zhe Yang dm_info->l2h_th_ini = chip->l2h_th_ini_ad; 1557285eb96SZong-Zhe Yang break; 1567285eb96SZong-Zhe Yang case NL80211_DFS_JP: 1577285eb96SZong-Zhe Yang dm_info->edcca_mode = RTW_EDCCA_ADAPTIVITY; 1587285eb96SZong-Zhe Yang dm_info->l2h_th_ini = chip->l2h_th_ini_cs; 1597285eb96SZong-Zhe Yang break; 1607285eb96SZong-Zhe Yang default: 1617285eb96SZong-Zhe Yang dm_info->edcca_mode = RTW_EDCCA_NORMAL; 1627285eb96SZong-Zhe Yang break; 1637285eb96SZong-Zhe Yang } 1647285eb96SZong-Zhe Yang } 1657285eb96SZong-Zhe Yang 1667285eb96SZong-Zhe Yang static void rtw_phy_adaptivity_init(struct rtw_dev *rtwdev) 1677285eb96SZong-Zhe Yang { 1687285eb96SZong-Zhe Yang struct rtw_chip_info *chip = rtwdev->chip; 1697285eb96SZong-Zhe Yang 1707285eb96SZong-Zhe Yang rtw_phy_adaptivity_set_mode(rtwdev); 1717285eb96SZong-Zhe Yang if (chip->ops->adaptivity_init) 1727285eb96SZong-Zhe Yang chip->ops->adaptivity_init(rtwdev); 1737285eb96SZong-Zhe Yang } 1747285eb96SZong-Zhe Yang 1757285eb96SZong-Zhe Yang static void rtw_phy_adaptivity(struct rtw_dev *rtwdev) 1767285eb96SZong-Zhe Yang { 1777285eb96SZong-Zhe Yang if (rtwdev->chip->ops->adaptivity) 1787285eb96SZong-Zhe Yang rtwdev->chip->ops->adaptivity(rtwdev); 1797285eb96SZong-Zhe Yang } 1807285eb96SZong-Zhe Yang 181fb8517f4SPo-Hao Huang static void rtw_phy_cfo_init(struct rtw_dev *rtwdev) 182fb8517f4SPo-Hao Huang { 183fb8517f4SPo-Hao Huang struct rtw_chip_info *chip = rtwdev->chip; 184fb8517f4SPo-Hao Huang 185fb8517f4SPo-Hao Huang if (chip->ops->cfo_init) 186fb8517f4SPo-Hao Huang chip->ops->cfo_init(rtwdev); 187fb8517f4SPo-Hao Huang } 188fb8517f4SPo-Hao Huang 1891188301fSPo-Hao Huang static void rtw_phy_tx_path_div_init(struct rtw_dev *rtwdev) 1901188301fSPo-Hao Huang { 1911188301fSPo-Hao Huang struct rtw_path_div *path_div = &rtwdev->dm_path_div; 1921188301fSPo-Hao Huang 1931188301fSPo-Hao Huang path_div->current_tx_path = rtwdev->chip->default_1ss_tx_path; 1941188301fSPo-Hao Huang path_div->path_a_cnt = 0; 1951188301fSPo-Hao Huang path_div->path_a_sum = 0; 1961188301fSPo-Hao Huang path_div->path_b_cnt = 0; 1971188301fSPo-Hao Huang path_div->path_b_sum = 0; 1981188301fSPo-Hao Huang } 1991188301fSPo-Hao Huang 200e3037485SYan-Hsuan Chuang void rtw_phy_init(struct rtw_dev *rtwdev) 201e3037485SYan-Hsuan Chuang { 202e3037485SYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 203e3037485SYan-Hsuan Chuang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 204e3037485SYan-Hsuan Chuang u32 addr, mask; 205e3037485SYan-Hsuan Chuang 206e3037485SYan-Hsuan Chuang dm_info->fa_history[3] = 0; 207e3037485SYan-Hsuan Chuang dm_info->fa_history[2] = 0; 208e3037485SYan-Hsuan Chuang dm_info->fa_history[1] = 0; 209e3037485SYan-Hsuan Chuang dm_info->fa_history[0] = 0; 210e3037485SYan-Hsuan Chuang dm_info->igi_bitmap = 0; 211e3037485SYan-Hsuan Chuang dm_info->igi_history[3] = 0; 212e3037485SYan-Hsuan Chuang dm_info->igi_history[2] = 0; 213e3037485SYan-Hsuan Chuang dm_info->igi_history[1] = 0; 214e3037485SYan-Hsuan Chuang 215e3037485SYan-Hsuan Chuang addr = chip->dig[0].addr; 216e3037485SYan-Hsuan Chuang mask = chip->dig[0].mask; 217e3037485SYan-Hsuan Chuang dm_info->igi_history[0] = rtw_read32_mask(rtwdev, addr, mask); 218479c4ee9STzu-En Huang rtw_phy_cck_pd_init(rtwdev); 2191d229e88SPing-Ke Shih 2201d229e88SPing-Ke Shih dm_info->iqk.done = false; 2217285eb96SZong-Zhe Yang rtw_phy_adaptivity_init(rtwdev); 222fb8517f4SPo-Hao Huang rtw_phy_cfo_init(rtwdev); 2231188301fSPo-Hao Huang rtw_phy_tx_path_div_init(rtwdev); 224e3037485SYan-Hsuan Chuang } 225449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_init); 226e3037485SYan-Hsuan Chuang 227e3037485SYan-Hsuan Chuang void rtw_phy_dig_write(struct rtw_dev *rtwdev, u8 igi) 228e3037485SYan-Hsuan Chuang { 229e3037485SYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 230e3037485SYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 231e3037485SYan-Hsuan Chuang u32 addr, mask; 232e3037485SYan-Hsuan Chuang u8 path; 233e3037485SYan-Hsuan Chuang 23422b726cbSBrian Norris if (chip->dig_cck) { 23522b726cbSBrian Norris const struct rtw_hw_reg *dig_cck = &chip->dig_cck[0]; 236fc637a86SPing-Ke Shih rtw_write32_mask(rtwdev, dig_cck->addr, dig_cck->mask, igi >> 1); 23722b726cbSBrian Norris } 238fc637a86SPing-Ke Shih 239e3037485SYan-Hsuan Chuang for (path = 0; path < hal->rf_path_num; path++) { 240e3037485SYan-Hsuan Chuang addr = chip->dig[path].addr; 241e3037485SYan-Hsuan Chuang mask = chip->dig[path].mask; 242e3037485SYan-Hsuan Chuang rtw_write32_mask(rtwdev, addr, mask, igi); 243e3037485SYan-Hsuan Chuang } 244e3037485SYan-Hsuan Chuang } 245e3037485SYan-Hsuan Chuang 246e3037485SYan-Hsuan Chuang static void rtw_phy_stat_false_alarm(struct rtw_dev *rtwdev) 247e3037485SYan-Hsuan Chuang { 248e3037485SYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 249e3037485SYan-Hsuan Chuang 250e3037485SYan-Hsuan Chuang chip->ops->false_alarm_statistics(rtwdev); 251e3037485SYan-Hsuan Chuang } 252e3037485SYan-Hsuan Chuang 253e3037485SYan-Hsuan Chuang #define RA_FLOOR_TABLE_SIZE 7 254e3037485SYan-Hsuan Chuang #define RA_FLOOR_UP_GAP 3 255e3037485SYan-Hsuan Chuang 256e3037485SYan-Hsuan Chuang static u8 rtw_phy_get_rssi_level(u8 old_level, u8 rssi) 257e3037485SYan-Hsuan Chuang { 258e3037485SYan-Hsuan Chuang u8 table[RA_FLOOR_TABLE_SIZE] = {20, 34, 38, 42, 46, 50, 100}; 259e3037485SYan-Hsuan Chuang u8 new_level = 0; 260e3037485SYan-Hsuan Chuang int i; 261e3037485SYan-Hsuan Chuang 262e3037485SYan-Hsuan Chuang for (i = 0; i < RA_FLOOR_TABLE_SIZE; i++) 263e3037485SYan-Hsuan Chuang if (i >= old_level) 264e3037485SYan-Hsuan Chuang table[i] += RA_FLOOR_UP_GAP; 265e3037485SYan-Hsuan Chuang 266e3037485SYan-Hsuan Chuang for (i = 0; i < RA_FLOOR_TABLE_SIZE; i++) { 267e3037485SYan-Hsuan Chuang if (rssi < table[i]) { 268e3037485SYan-Hsuan Chuang new_level = i; 269e3037485SYan-Hsuan Chuang break; 270e3037485SYan-Hsuan Chuang } 271e3037485SYan-Hsuan Chuang } 272e3037485SYan-Hsuan Chuang 273e3037485SYan-Hsuan Chuang return new_level; 274e3037485SYan-Hsuan Chuang } 275e3037485SYan-Hsuan Chuang 276e3037485SYan-Hsuan Chuang struct rtw_phy_stat_iter_data { 277e3037485SYan-Hsuan Chuang struct rtw_dev *rtwdev; 278e3037485SYan-Hsuan Chuang u8 min_rssi; 279e3037485SYan-Hsuan Chuang }; 280e3037485SYan-Hsuan Chuang 281e3037485SYan-Hsuan Chuang static void rtw_phy_stat_rssi_iter(void *data, struct ieee80211_sta *sta) 282e3037485SYan-Hsuan Chuang { 283e3037485SYan-Hsuan Chuang struct rtw_phy_stat_iter_data *iter_data = data; 284e3037485SYan-Hsuan Chuang struct rtw_dev *rtwdev = iter_data->rtwdev; 285e3037485SYan-Hsuan Chuang struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv; 286a24bad74SYan-Hsuan Chuang u8 rssi; 287e3037485SYan-Hsuan Chuang 288e3037485SYan-Hsuan Chuang rssi = ewma_rssi_read(&si->avg_rssi); 289a24bad74SYan-Hsuan Chuang si->rssi_level = rtw_phy_get_rssi_level(si->rssi_level, rssi); 290e3037485SYan-Hsuan Chuang 291e3037485SYan-Hsuan Chuang rtw_fw_send_rssi_info(rtwdev, si); 292e3037485SYan-Hsuan Chuang 293e3037485SYan-Hsuan Chuang iter_data->min_rssi = min_t(u8, rssi, iter_data->min_rssi); 294e3037485SYan-Hsuan Chuang } 295e3037485SYan-Hsuan Chuang 296e3037485SYan-Hsuan Chuang static void rtw_phy_stat_rssi(struct rtw_dev *rtwdev) 297e3037485SYan-Hsuan Chuang { 298e3037485SYan-Hsuan Chuang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 299e3037485SYan-Hsuan Chuang struct rtw_phy_stat_iter_data data = {}; 300e3037485SYan-Hsuan Chuang 301e3037485SYan-Hsuan Chuang data.rtwdev = rtwdev; 302e3037485SYan-Hsuan Chuang data.min_rssi = U8_MAX; 303e3037485SYan-Hsuan Chuang rtw_iterate_stas_atomic(rtwdev, rtw_phy_stat_rssi_iter, &data); 304e3037485SYan-Hsuan Chuang 305e3037485SYan-Hsuan Chuang dm_info->pre_min_rssi = dm_info->min_rssi; 306e3037485SYan-Hsuan Chuang dm_info->min_rssi = data.min_rssi; 307e3037485SYan-Hsuan Chuang } 308e3037485SYan-Hsuan Chuang 309082a36dcSTsang-Shian Lin static void rtw_phy_stat_rate_cnt(struct rtw_dev *rtwdev) 310082a36dcSTsang-Shian Lin { 311082a36dcSTsang-Shian Lin struct rtw_dm_info *dm_info = &rtwdev->dm_info; 312082a36dcSTsang-Shian Lin 313082a36dcSTsang-Shian Lin dm_info->last_pkt_count = dm_info->cur_pkt_count; 314082a36dcSTsang-Shian Lin memset(&dm_info->cur_pkt_count, 0, sizeof(dm_info->cur_pkt_count)); 315082a36dcSTsang-Shian Lin } 316082a36dcSTsang-Shian Lin 317e3037485SYan-Hsuan Chuang static void rtw_phy_statistics(struct rtw_dev *rtwdev) 318e3037485SYan-Hsuan Chuang { 319e3037485SYan-Hsuan Chuang rtw_phy_stat_rssi(rtwdev); 320e3037485SYan-Hsuan Chuang rtw_phy_stat_false_alarm(rtwdev); 321082a36dcSTsang-Shian Lin rtw_phy_stat_rate_cnt(rtwdev); 322e3037485SYan-Hsuan Chuang } 323e3037485SYan-Hsuan Chuang 324e3037485SYan-Hsuan Chuang #define DIG_PERF_FA_TH_LOW 250 325e3037485SYan-Hsuan Chuang #define DIG_PERF_FA_TH_HIGH 500 326e3037485SYan-Hsuan Chuang #define DIG_PERF_FA_TH_EXTRA_HIGH 750 327e3037485SYan-Hsuan Chuang #define DIG_PERF_MAX 0x5a 328e3037485SYan-Hsuan Chuang #define DIG_PERF_MID 0x40 329e3037485SYan-Hsuan Chuang #define DIG_CVRG_FA_TH_LOW 2000 330e3037485SYan-Hsuan Chuang #define DIG_CVRG_FA_TH_HIGH 4000 331e3037485SYan-Hsuan Chuang #define DIG_CVRG_FA_TH_EXTRA_HIGH 5000 332e3037485SYan-Hsuan Chuang #define DIG_CVRG_MAX 0x2a 333e3037485SYan-Hsuan Chuang #define DIG_CVRG_MID 0x26 334e3037485SYan-Hsuan Chuang #define DIG_CVRG_MIN 0x1c 335e3037485SYan-Hsuan Chuang #define DIG_RSSI_GAIN_OFFSET 15 336e3037485SYan-Hsuan Chuang 337e3037485SYan-Hsuan Chuang static bool 338e3037485SYan-Hsuan Chuang rtw_phy_dig_check_damping(struct rtw_dm_info *dm_info) 339e3037485SYan-Hsuan Chuang { 340e3037485SYan-Hsuan Chuang u16 fa_lo = DIG_PERF_FA_TH_LOW; 341e3037485SYan-Hsuan Chuang u16 fa_hi = DIG_PERF_FA_TH_HIGH; 342e3037485SYan-Hsuan Chuang u16 *fa_history; 343e3037485SYan-Hsuan Chuang u8 *igi_history; 344e3037485SYan-Hsuan Chuang u8 damping_rssi; 345e3037485SYan-Hsuan Chuang u8 min_rssi; 346e3037485SYan-Hsuan Chuang u8 diff; 347e3037485SYan-Hsuan Chuang u8 igi_bitmap; 348e3037485SYan-Hsuan Chuang bool damping = false; 349e3037485SYan-Hsuan Chuang 350e3037485SYan-Hsuan Chuang min_rssi = dm_info->min_rssi; 351e3037485SYan-Hsuan Chuang if (dm_info->damping) { 352e3037485SYan-Hsuan Chuang damping_rssi = dm_info->damping_rssi; 353e3037485SYan-Hsuan Chuang diff = min_rssi > damping_rssi ? min_rssi - damping_rssi : 354e3037485SYan-Hsuan Chuang damping_rssi - min_rssi; 355e3037485SYan-Hsuan Chuang if (diff > 3 || dm_info->damping_cnt++ > 20) { 356e3037485SYan-Hsuan Chuang dm_info->damping = false; 357e3037485SYan-Hsuan Chuang return false; 358e3037485SYan-Hsuan Chuang } 359e3037485SYan-Hsuan Chuang 360e3037485SYan-Hsuan Chuang return true; 361e3037485SYan-Hsuan Chuang } 362e3037485SYan-Hsuan Chuang 363e3037485SYan-Hsuan Chuang igi_history = dm_info->igi_history; 364e3037485SYan-Hsuan Chuang fa_history = dm_info->fa_history; 365e3037485SYan-Hsuan Chuang igi_bitmap = dm_info->igi_bitmap & 0xf; 366e3037485SYan-Hsuan Chuang switch (igi_bitmap) { 367e3037485SYan-Hsuan Chuang case 5: 368e3037485SYan-Hsuan Chuang /* down -> up -> down -> up */ 369e3037485SYan-Hsuan Chuang if (igi_history[0] > igi_history[1] && 370e3037485SYan-Hsuan Chuang igi_history[2] > igi_history[3] && 371e3037485SYan-Hsuan Chuang igi_history[0] - igi_history[1] >= 2 && 372e3037485SYan-Hsuan Chuang igi_history[2] - igi_history[3] >= 2 && 373e3037485SYan-Hsuan Chuang fa_history[0] > fa_hi && fa_history[1] < fa_lo && 374e3037485SYan-Hsuan Chuang fa_history[2] > fa_hi && fa_history[3] < fa_lo) 375e3037485SYan-Hsuan Chuang damping = true; 376e3037485SYan-Hsuan Chuang break; 377e3037485SYan-Hsuan Chuang case 9: 378e3037485SYan-Hsuan Chuang /* up -> down -> down -> up */ 379e3037485SYan-Hsuan Chuang if (igi_history[0] > igi_history[1] && 380e3037485SYan-Hsuan Chuang igi_history[3] > igi_history[2] && 381e3037485SYan-Hsuan Chuang igi_history[0] - igi_history[1] >= 4 && 382e3037485SYan-Hsuan Chuang igi_history[3] - igi_history[2] >= 2 && 383e3037485SYan-Hsuan Chuang fa_history[0] > fa_hi && fa_history[1] < fa_lo && 384e3037485SYan-Hsuan Chuang fa_history[2] < fa_lo && fa_history[3] > fa_hi) 385e3037485SYan-Hsuan Chuang damping = true; 386e3037485SYan-Hsuan Chuang break; 387e3037485SYan-Hsuan Chuang default: 388e3037485SYan-Hsuan Chuang return false; 389e3037485SYan-Hsuan Chuang } 390e3037485SYan-Hsuan Chuang 391e3037485SYan-Hsuan Chuang if (damping) { 392e3037485SYan-Hsuan Chuang dm_info->damping = true; 393e3037485SYan-Hsuan Chuang dm_info->damping_cnt = 0; 394e3037485SYan-Hsuan Chuang dm_info->damping_rssi = min_rssi; 395e3037485SYan-Hsuan Chuang } 396e3037485SYan-Hsuan Chuang 397e3037485SYan-Hsuan Chuang return damping; 398e3037485SYan-Hsuan Chuang } 399e3037485SYan-Hsuan Chuang 40076325506SZong-Zhe Yang static void rtw_phy_dig_get_boundary(struct rtw_dev *rtwdev, 40176325506SZong-Zhe Yang struct rtw_dm_info *dm_info, 402e3037485SYan-Hsuan Chuang u8 *upper, u8 *lower, bool linked) 403e3037485SYan-Hsuan Chuang { 404e3037485SYan-Hsuan Chuang u8 dig_max, dig_min, dig_mid; 405e3037485SYan-Hsuan Chuang u8 min_rssi; 406e3037485SYan-Hsuan Chuang 407e3037485SYan-Hsuan Chuang if (linked) { 408e3037485SYan-Hsuan Chuang dig_max = DIG_PERF_MAX; 409e3037485SYan-Hsuan Chuang dig_mid = DIG_PERF_MID; 41076325506SZong-Zhe Yang dig_min = rtwdev->chip->dig_min; 411e3037485SYan-Hsuan Chuang min_rssi = max_t(u8, dm_info->min_rssi, dig_min); 412e3037485SYan-Hsuan Chuang } else { 413e3037485SYan-Hsuan Chuang dig_max = DIG_CVRG_MAX; 414e3037485SYan-Hsuan Chuang dig_mid = DIG_CVRG_MID; 415e3037485SYan-Hsuan Chuang dig_min = DIG_CVRG_MIN; 416e3037485SYan-Hsuan Chuang min_rssi = dig_min; 417e3037485SYan-Hsuan Chuang } 418e3037485SYan-Hsuan Chuang 419e3037485SYan-Hsuan Chuang /* DIG MAX should be bounded by minimum RSSI with offset +15 */ 420e3037485SYan-Hsuan Chuang dig_max = min_t(u8, dig_max, min_rssi + DIG_RSSI_GAIN_OFFSET); 421e3037485SYan-Hsuan Chuang 422e3037485SYan-Hsuan Chuang *lower = clamp_t(u8, min_rssi, dig_min, dig_mid); 423e3037485SYan-Hsuan Chuang *upper = clamp_t(u8, *lower + DIG_RSSI_GAIN_OFFSET, dig_min, dig_max); 424e3037485SYan-Hsuan Chuang } 425e3037485SYan-Hsuan Chuang 426e3037485SYan-Hsuan Chuang static void rtw_phy_dig_get_threshold(struct rtw_dm_info *dm_info, 427e3037485SYan-Hsuan Chuang u16 *fa_th, u8 *step, bool linked) 428e3037485SYan-Hsuan Chuang { 429e3037485SYan-Hsuan Chuang u8 min_rssi, pre_min_rssi; 430e3037485SYan-Hsuan Chuang 431e3037485SYan-Hsuan Chuang min_rssi = dm_info->min_rssi; 432e3037485SYan-Hsuan Chuang pre_min_rssi = dm_info->pre_min_rssi; 433e3037485SYan-Hsuan Chuang step[0] = 4; 434e3037485SYan-Hsuan Chuang step[1] = 3; 435e3037485SYan-Hsuan Chuang step[2] = 2; 436e3037485SYan-Hsuan Chuang 437e3037485SYan-Hsuan Chuang if (linked) { 438e3037485SYan-Hsuan Chuang fa_th[0] = DIG_PERF_FA_TH_EXTRA_HIGH; 439e3037485SYan-Hsuan Chuang fa_th[1] = DIG_PERF_FA_TH_HIGH; 440e3037485SYan-Hsuan Chuang fa_th[2] = DIG_PERF_FA_TH_LOW; 441e3037485SYan-Hsuan Chuang if (pre_min_rssi > min_rssi) { 442e3037485SYan-Hsuan Chuang step[0] = 6; 443e3037485SYan-Hsuan Chuang step[1] = 4; 444e3037485SYan-Hsuan Chuang step[2] = 2; 445e3037485SYan-Hsuan Chuang } 446e3037485SYan-Hsuan Chuang } else { 447e3037485SYan-Hsuan Chuang fa_th[0] = DIG_CVRG_FA_TH_EXTRA_HIGH; 448e3037485SYan-Hsuan Chuang fa_th[1] = DIG_CVRG_FA_TH_HIGH; 449e3037485SYan-Hsuan Chuang fa_th[2] = DIG_CVRG_FA_TH_LOW; 450e3037485SYan-Hsuan Chuang } 451e3037485SYan-Hsuan Chuang } 452e3037485SYan-Hsuan Chuang 453e3037485SYan-Hsuan Chuang static void rtw_phy_dig_recorder(struct rtw_dm_info *dm_info, u8 igi, u16 fa) 454e3037485SYan-Hsuan Chuang { 455e3037485SYan-Hsuan Chuang u8 *igi_history; 456e3037485SYan-Hsuan Chuang u16 *fa_history; 457e3037485SYan-Hsuan Chuang u8 igi_bitmap; 458e3037485SYan-Hsuan Chuang bool up; 459e3037485SYan-Hsuan Chuang 460e3037485SYan-Hsuan Chuang igi_bitmap = dm_info->igi_bitmap << 1 & 0xfe; 461e3037485SYan-Hsuan Chuang igi_history = dm_info->igi_history; 462e3037485SYan-Hsuan Chuang fa_history = dm_info->fa_history; 463e3037485SYan-Hsuan Chuang 464e3037485SYan-Hsuan Chuang up = igi > igi_history[0]; 465e3037485SYan-Hsuan Chuang igi_bitmap |= up; 466e3037485SYan-Hsuan Chuang 467e3037485SYan-Hsuan Chuang igi_history[3] = igi_history[2]; 468e3037485SYan-Hsuan Chuang igi_history[2] = igi_history[1]; 469e3037485SYan-Hsuan Chuang igi_history[1] = igi_history[0]; 470e3037485SYan-Hsuan Chuang igi_history[0] = igi; 471e3037485SYan-Hsuan Chuang 472e3037485SYan-Hsuan Chuang fa_history[3] = fa_history[2]; 473e3037485SYan-Hsuan Chuang fa_history[2] = fa_history[1]; 474e3037485SYan-Hsuan Chuang fa_history[1] = fa_history[0]; 475e3037485SYan-Hsuan Chuang fa_history[0] = fa; 476e3037485SYan-Hsuan Chuang 477e3037485SYan-Hsuan Chuang dm_info->igi_bitmap = igi_bitmap; 478e3037485SYan-Hsuan Chuang } 479e3037485SYan-Hsuan Chuang 480e3037485SYan-Hsuan Chuang static void rtw_phy_dig(struct rtw_dev *rtwdev) 481e3037485SYan-Hsuan Chuang { 482e3037485SYan-Hsuan Chuang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 483e3037485SYan-Hsuan Chuang u8 upper_bound, lower_bound; 484e3037485SYan-Hsuan Chuang u8 pre_igi, cur_igi; 485e3037485SYan-Hsuan Chuang u16 fa_th[3], fa_cnt; 486e3037485SYan-Hsuan Chuang u8 level; 487e3037485SYan-Hsuan Chuang u8 step[3]; 488e3037485SYan-Hsuan Chuang bool linked; 489e3037485SYan-Hsuan Chuang 4903c519605SYan-Hsuan Chuang if (test_bit(RTW_FLAG_DIG_DISABLE, rtwdev->flags)) 491e3037485SYan-Hsuan Chuang return; 492e3037485SYan-Hsuan Chuang 493e3037485SYan-Hsuan Chuang if (rtw_phy_dig_check_damping(dm_info)) 494e3037485SYan-Hsuan Chuang return; 495e3037485SYan-Hsuan Chuang 496e3037485SYan-Hsuan Chuang linked = !!rtwdev->sta_cnt; 497e3037485SYan-Hsuan Chuang 498e3037485SYan-Hsuan Chuang fa_cnt = dm_info->total_fa_cnt; 499e3037485SYan-Hsuan Chuang pre_igi = dm_info->igi_history[0]; 500e3037485SYan-Hsuan Chuang 501e3037485SYan-Hsuan Chuang rtw_phy_dig_get_threshold(dm_info, fa_th, step, linked); 502e3037485SYan-Hsuan Chuang 503e3037485SYan-Hsuan Chuang /* test the false alarm count from the highest threshold level first, 504e3037485SYan-Hsuan Chuang * and increase it by corresponding step size 505e3037485SYan-Hsuan Chuang * 506e3037485SYan-Hsuan Chuang * note that the step size is offset by -2, compensate it afterall 507e3037485SYan-Hsuan Chuang */ 508e3037485SYan-Hsuan Chuang cur_igi = pre_igi; 509e3037485SYan-Hsuan Chuang for (level = 0; level < 3; level++) { 510e3037485SYan-Hsuan Chuang if (fa_cnt > fa_th[level]) { 511e3037485SYan-Hsuan Chuang cur_igi += step[level]; 512e3037485SYan-Hsuan Chuang break; 513e3037485SYan-Hsuan Chuang } 514e3037485SYan-Hsuan Chuang } 515e3037485SYan-Hsuan Chuang cur_igi -= 2; 516e3037485SYan-Hsuan Chuang 517e3037485SYan-Hsuan Chuang /* calculate the upper/lower bound by the minimum rssi we have among 518e3037485SYan-Hsuan Chuang * the peers connected with us, meanwhile make sure the igi value does 519e3037485SYan-Hsuan Chuang * not beyond the hardware limitation 520e3037485SYan-Hsuan Chuang */ 52176325506SZong-Zhe Yang rtw_phy_dig_get_boundary(rtwdev, dm_info, &upper_bound, &lower_bound, 52276325506SZong-Zhe Yang linked); 523e3037485SYan-Hsuan Chuang cur_igi = clamp_t(u8, cur_igi, lower_bound, upper_bound); 524e3037485SYan-Hsuan Chuang 525e3037485SYan-Hsuan Chuang /* record current igi value and false alarm statistics for further 526e3037485SYan-Hsuan Chuang * damping checks, and record the trend of igi values 527e3037485SYan-Hsuan Chuang */ 528e3037485SYan-Hsuan Chuang rtw_phy_dig_recorder(dm_info, cur_igi, fa_cnt); 529e3037485SYan-Hsuan Chuang 530e3037485SYan-Hsuan Chuang if (cur_igi != pre_igi) 531e3037485SYan-Hsuan Chuang rtw_phy_dig_write(rtwdev, cur_igi); 532e3037485SYan-Hsuan Chuang } 533e3037485SYan-Hsuan Chuang 534e3037485SYan-Hsuan Chuang static void rtw_phy_ra_info_update_iter(void *data, struct ieee80211_sta *sta) 535e3037485SYan-Hsuan Chuang { 536e3037485SYan-Hsuan Chuang struct rtw_dev *rtwdev = data; 537e3037485SYan-Hsuan Chuang struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv; 538e3037485SYan-Hsuan Chuang 539c1edc864SPo-Hao Huang rtw_update_sta_info(rtwdev, si, false); 540e3037485SYan-Hsuan Chuang } 541e3037485SYan-Hsuan Chuang 542e3037485SYan-Hsuan Chuang static void rtw_phy_ra_info_update(struct rtw_dev *rtwdev) 543e3037485SYan-Hsuan Chuang { 544e3037485SYan-Hsuan Chuang if (rtwdev->watch_dog_cnt & 0x3) 545e3037485SYan-Hsuan Chuang return; 546e3037485SYan-Hsuan Chuang 547e3037485SYan-Hsuan Chuang rtw_iterate_stas_atomic(rtwdev, rtw_phy_ra_info_update_iter, rtwdev); 548e3037485SYan-Hsuan Chuang } 549e3037485SYan-Hsuan Chuang 55048308726SPo-Hao Huang static u32 rtw_phy_get_rrsr_mask(struct rtw_dev *rtwdev, u8 rate_idx) 55148308726SPo-Hao Huang { 55248308726SPo-Hao Huang u8 rate_order; 55348308726SPo-Hao Huang 55448308726SPo-Hao Huang rate_order = rate_idx; 55548308726SPo-Hao Huang 55648308726SPo-Hao Huang if (rate_idx >= DESC_RATEVHT4SS_MCS0) 55748308726SPo-Hao Huang rate_order -= DESC_RATEVHT4SS_MCS0; 55848308726SPo-Hao Huang else if (rate_idx >= DESC_RATEVHT3SS_MCS0) 55948308726SPo-Hao Huang rate_order -= DESC_RATEVHT3SS_MCS0; 56048308726SPo-Hao Huang else if (rate_idx >= DESC_RATEVHT2SS_MCS0) 56148308726SPo-Hao Huang rate_order -= DESC_RATEVHT2SS_MCS0; 56248308726SPo-Hao Huang else if (rate_idx >= DESC_RATEVHT1SS_MCS0) 56348308726SPo-Hao Huang rate_order -= DESC_RATEVHT1SS_MCS0; 56448308726SPo-Hao Huang else if (rate_idx >= DESC_RATEMCS24) 56548308726SPo-Hao Huang rate_order -= DESC_RATEMCS24; 56648308726SPo-Hao Huang else if (rate_idx >= DESC_RATEMCS16) 56748308726SPo-Hao Huang rate_order -= DESC_RATEMCS16; 56848308726SPo-Hao Huang else if (rate_idx >= DESC_RATEMCS8) 56948308726SPo-Hao Huang rate_order -= DESC_RATEMCS8; 57048308726SPo-Hao Huang else if (rate_idx >= DESC_RATEMCS0) 57148308726SPo-Hao Huang rate_order -= DESC_RATEMCS0; 57248308726SPo-Hao Huang else if (rate_idx >= DESC_RATE6M) 57348308726SPo-Hao Huang rate_order -= DESC_RATE6M; 57448308726SPo-Hao Huang else 57548308726SPo-Hao Huang rate_order -= DESC_RATE1M; 57648308726SPo-Hao Huang 57748308726SPo-Hao Huang if (rate_idx >= DESC_RATEMCS0 || rate_order == 0) 57848308726SPo-Hao Huang rate_order++; 57948308726SPo-Hao Huang 58048308726SPo-Hao Huang return GENMASK(rate_order + RRSR_RATE_ORDER_CCK_LEN - 1, 0); 58148308726SPo-Hao Huang } 58248308726SPo-Hao Huang 58348308726SPo-Hao Huang static void rtw_phy_rrsr_mask_min_iter(void *data, struct ieee80211_sta *sta) 58448308726SPo-Hao Huang { 58548308726SPo-Hao Huang struct rtw_dev *rtwdev = (struct rtw_dev *)data; 58648308726SPo-Hao Huang struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv; 58748308726SPo-Hao Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 58848308726SPo-Hao Huang u32 mask = 0; 58948308726SPo-Hao Huang 59048308726SPo-Hao Huang mask = rtw_phy_get_rrsr_mask(rtwdev, si->ra_report.desc_rate); 59148308726SPo-Hao Huang if (mask < dm_info->rrsr_mask_min) 59248308726SPo-Hao Huang dm_info->rrsr_mask_min = mask; 59348308726SPo-Hao Huang } 59448308726SPo-Hao Huang 59548308726SPo-Hao Huang static void rtw_phy_rrsr_update(struct rtw_dev *rtwdev) 59648308726SPo-Hao Huang { 59748308726SPo-Hao Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 59848308726SPo-Hao Huang 59948308726SPo-Hao Huang dm_info->rrsr_mask_min = RRSR_RATE_ORDER_MAX; 60048308726SPo-Hao Huang rtw_iterate_stas_atomic(rtwdev, rtw_phy_rrsr_mask_min_iter, rtwdev); 60148308726SPo-Hao Huang rtw_write32(rtwdev, REG_RRSR, dm_info->rrsr_val_init & dm_info->rrsr_mask_min); 60248308726SPo-Hao Huang } 60348308726SPo-Hao Huang 6045227c2eeSTzu-En Huang static void rtw_phy_dpk_track(struct rtw_dev *rtwdev) 6055227c2eeSTzu-En Huang { 6065227c2eeSTzu-En Huang struct rtw_chip_info *chip = rtwdev->chip; 6075227c2eeSTzu-En Huang 6085227c2eeSTzu-En Huang if (chip->ops->dpk_track) 6095227c2eeSTzu-En Huang chip->ops->dpk_track(rtwdev); 6105227c2eeSTzu-En Huang } 6115227c2eeSTzu-En Huang 612fb8517f4SPo-Hao Huang struct rtw_rx_addr_match_data { 613fb8517f4SPo-Hao Huang struct rtw_dev *rtwdev; 614fb8517f4SPo-Hao Huang struct ieee80211_hdr *hdr; 615fb8517f4SPo-Hao Huang struct rtw_rx_pkt_stat *pkt_stat; 616fb8517f4SPo-Hao Huang u8 *bssid; 617fb8517f4SPo-Hao Huang }; 618fb8517f4SPo-Hao Huang 619fb8517f4SPo-Hao Huang static void rtw_phy_parsing_cfo_iter(void *data, u8 *mac, 620fb8517f4SPo-Hao Huang struct ieee80211_vif *vif) 621fb8517f4SPo-Hao Huang { 622fb8517f4SPo-Hao Huang struct rtw_rx_addr_match_data *iter_data = data; 623fb8517f4SPo-Hao Huang struct rtw_dev *rtwdev = iter_data->rtwdev; 624fb8517f4SPo-Hao Huang struct rtw_rx_pkt_stat *pkt_stat = iter_data->pkt_stat; 625fb8517f4SPo-Hao Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 626fb8517f4SPo-Hao Huang struct rtw_cfo_track *cfo = &dm_info->cfo_track; 627fb8517f4SPo-Hao Huang u8 *bssid = iter_data->bssid; 628fb8517f4SPo-Hao Huang u8 i; 629fb8517f4SPo-Hao Huang 630fb8517f4SPo-Hao Huang if (!ether_addr_equal(vif->bss_conf.bssid, bssid)) 631fb8517f4SPo-Hao Huang return; 632fb8517f4SPo-Hao Huang 633fb8517f4SPo-Hao Huang for (i = 0; i < rtwdev->hal.rf_path_num; i++) { 634fb8517f4SPo-Hao Huang cfo->cfo_tail[i] += pkt_stat->cfo_tail[i]; 635fb8517f4SPo-Hao Huang cfo->cfo_cnt[i]++; 636fb8517f4SPo-Hao Huang } 637fb8517f4SPo-Hao Huang 638fb8517f4SPo-Hao Huang cfo->packet_count++; 639fb8517f4SPo-Hao Huang } 640fb8517f4SPo-Hao Huang 641fb8517f4SPo-Hao Huang void rtw_phy_parsing_cfo(struct rtw_dev *rtwdev, 642fb8517f4SPo-Hao Huang struct rtw_rx_pkt_stat *pkt_stat) 643fb8517f4SPo-Hao Huang { 644fb8517f4SPo-Hao Huang struct ieee80211_hdr *hdr = pkt_stat->hdr; 645fb8517f4SPo-Hao Huang struct rtw_rx_addr_match_data data = {}; 646fb8517f4SPo-Hao Huang 647fb8517f4SPo-Hao Huang if (pkt_stat->crc_err || pkt_stat->icv_err || !pkt_stat->phy_status || 648fb8517f4SPo-Hao Huang ieee80211_is_ctl(hdr->frame_control)) 649fb8517f4SPo-Hao Huang return; 650fb8517f4SPo-Hao Huang 651fb8517f4SPo-Hao Huang data.rtwdev = rtwdev; 652fb8517f4SPo-Hao Huang data.hdr = hdr; 653fb8517f4SPo-Hao Huang data.pkt_stat = pkt_stat; 654fb8517f4SPo-Hao Huang data.bssid = get_hdr_bssid(hdr); 655fb8517f4SPo-Hao Huang 656fb8517f4SPo-Hao Huang rtw_iterate_vifs_atomic(rtwdev, rtw_phy_parsing_cfo_iter, &data); 657fb8517f4SPo-Hao Huang } 658fb8517f4SPo-Hao Huang EXPORT_SYMBOL(rtw_phy_parsing_cfo); 659fb8517f4SPo-Hao Huang 660fb8517f4SPo-Hao Huang static void rtw_phy_cfo_track(struct rtw_dev *rtwdev) 661fb8517f4SPo-Hao Huang { 662fb8517f4SPo-Hao Huang struct rtw_chip_info *chip = rtwdev->chip; 663fb8517f4SPo-Hao Huang 664fb8517f4SPo-Hao Huang if (chip->ops->cfo_track) 665fb8517f4SPo-Hao Huang chip->ops->cfo_track(rtwdev); 666fb8517f4SPo-Hao Huang } 667fb8517f4SPo-Hao Huang 668479c4ee9STzu-En Huang #define CCK_PD_FA_LV1_MIN 1000 669479c4ee9STzu-En Huang #define CCK_PD_FA_LV0_MAX 500 670479c4ee9STzu-En Huang 671479c4ee9STzu-En Huang static u8 rtw_phy_cck_pd_lv_unlink(struct rtw_dev *rtwdev) 672479c4ee9STzu-En Huang { 673479c4ee9STzu-En Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 674479c4ee9STzu-En Huang u32 cck_fa_avg = dm_info->cck_fa_avg; 675479c4ee9STzu-En Huang 676479c4ee9STzu-En Huang if (cck_fa_avg > CCK_PD_FA_LV1_MIN) 67718a0696eSTzu-En Huang return CCK_PD_LV1; 678479c4ee9STzu-En Huang 679479c4ee9STzu-En Huang if (cck_fa_avg < CCK_PD_FA_LV0_MAX) 68018a0696eSTzu-En Huang return CCK_PD_LV0; 681479c4ee9STzu-En Huang 682479c4ee9STzu-En Huang return CCK_PD_LV_MAX; 683479c4ee9STzu-En Huang } 684479c4ee9STzu-En Huang 685479c4ee9STzu-En Huang #define CCK_PD_IGI_LV4_VAL 0x38 686479c4ee9STzu-En Huang #define CCK_PD_IGI_LV3_VAL 0x2a 687479c4ee9STzu-En Huang #define CCK_PD_IGI_LV2_VAL 0x24 688479c4ee9STzu-En Huang #define CCK_PD_RSSI_LV4_VAL 32 689479c4ee9STzu-En Huang #define CCK_PD_RSSI_LV3_VAL 32 690479c4ee9STzu-En Huang #define CCK_PD_RSSI_LV2_VAL 24 691479c4ee9STzu-En Huang 692479c4ee9STzu-En Huang static u8 rtw_phy_cck_pd_lv_link(struct rtw_dev *rtwdev) 693479c4ee9STzu-En Huang { 694479c4ee9STzu-En Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 695479c4ee9STzu-En Huang u8 igi = dm_info->igi_history[0]; 696479c4ee9STzu-En Huang u8 rssi = dm_info->min_rssi; 697479c4ee9STzu-En Huang u32 cck_fa_avg = dm_info->cck_fa_avg; 698479c4ee9STzu-En Huang 699479c4ee9STzu-En Huang if (igi > CCK_PD_IGI_LV4_VAL && rssi > CCK_PD_RSSI_LV4_VAL) 70018a0696eSTzu-En Huang return CCK_PD_LV4; 701479c4ee9STzu-En Huang if (igi > CCK_PD_IGI_LV3_VAL && rssi > CCK_PD_RSSI_LV3_VAL) 70218a0696eSTzu-En Huang return CCK_PD_LV3; 703479c4ee9STzu-En Huang if (igi > CCK_PD_IGI_LV2_VAL || rssi > CCK_PD_RSSI_LV2_VAL) 70418a0696eSTzu-En Huang return CCK_PD_LV2; 705479c4ee9STzu-En Huang if (cck_fa_avg > CCK_PD_FA_LV1_MIN) 70618a0696eSTzu-En Huang return CCK_PD_LV1; 707479c4ee9STzu-En Huang if (cck_fa_avg < CCK_PD_FA_LV0_MAX) 70818a0696eSTzu-En Huang return CCK_PD_LV0; 709479c4ee9STzu-En Huang 710479c4ee9STzu-En Huang return CCK_PD_LV_MAX; 711479c4ee9STzu-En Huang } 712479c4ee9STzu-En Huang 713479c4ee9STzu-En Huang static u8 rtw_phy_cck_pd_lv(struct rtw_dev *rtwdev) 714479c4ee9STzu-En Huang { 715479c4ee9STzu-En Huang if (!rtw_is_assoc(rtwdev)) 716479c4ee9STzu-En Huang return rtw_phy_cck_pd_lv_unlink(rtwdev); 717479c4ee9STzu-En Huang else 718479c4ee9STzu-En Huang return rtw_phy_cck_pd_lv_link(rtwdev); 719479c4ee9STzu-En Huang } 720479c4ee9STzu-En Huang 721479c4ee9STzu-En Huang static void rtw_phy_cck_pd(struct rtw_dev *rtwdev) 722479c4ee9STzu-En Huang { 723479c4ee9STzu-En Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 724479c4ee9STzu-En Huang struct rtw_chip_info *chip = rtwdev->chip; 725479c4ee9STzu-En Huang u32 cck_fa = dm_info->cck_fa_cnt; 726479c4ee9STzu-En Huang u8 level; 727479c4ee9STzu-En Huang 728479c4ee9STzu-En Huang if (rtwdev->hal.current_band_type != RTW_BAND_2G) 729479c4ee9STzu-En Huang return; 730479c4ee9STzu-En Huang 731479c4ee9STzu-En Huang if (dm_info->cck_fa_avg == CCK_FA_AVG_RESET) 732479c4ee9STzu-En Huang dm_info->cck_fa_avg = cck_fa; 733479c4ee9STzu-En Huang else 734479c4ee9STzu-En Huang dm_info->cck_fa_avg = (dm_info->cck_fa_avg * 3 + cck_fa) >> 2; 735479c4ee9STzu-En Huang 736760bb2abSPing-Ke Shih rtw_dbg(rtwdev, RTW_DBG_PHY, "IGI=0x%x, rssi_min=%d, cck_fa=%d\n", 737760bb2abSPing-Ke Shih dm_info->igi_history[0], dm_info->min_rssi, 738760bb2abSPing-Ke Shih dm_info->fa_history[0]); 739760bb2abSPing-Ke Shih rtw_dbg(rtwdev, RTW_DBG_PHY, "cck_fa_avg=%d, cck_pd_default=%d\n", 740760bb2abSPing-Ke Shih dm_info->cck_fa_avg, dm_info->cck_pd_default); 741760bb2abSPing-Ke Shih 742479c4ee9STzu-En Huang level = rtw_phy_cck_pd_lv(rtwdev); 743479c4ee9STzu-En Huang 744479c4ee9STzu-En Huang if (level >= CCK_PD_LV_MAX) 745479c4ee9STzu-En Huang return; 746479c4ee9STzu-En Huang 747479c4ee9STzu-En Huang if (chip->ops->cck_pd_set) 748479c4ee9STzu-En Huang chip->ops->cck_pd_set(rtwdev, level); 749479c4ee9STzu-En Huang } 750479c4ee9STzu-En Huang 751c97ee3e0STzu-En Huang static void rtw_phy_pwr_track(struct rtw_dev *rtwdev) 752c97ee3e0STzu-En Huang { 753c97ee3e0STzu-En Huang rtwdev->chip->ops->pwr_track(rtwdev); 754c97ee3e0STzu-En Huang } 755c97ee3e0STzu-En Huang 75648308726SPo-Hao Huang static void rtw_phy_ra_track(struct rtw_dev *rtwdev) 75748308726SPo-Hao Huang { 758ec7480edSPo-Hao Huang rtw_fw_update_wl_phy_info(rtwdev); 75948308726SPo-Hao Huang rtw_phy_ra_info_update(rtwdev); 76048308726SPo-Hao Huang rtw_phy_rrsr_update(rtwdev); 76148308726SPo-Hao Huang } 76248308726SPo-Hao Huang 763e3037485SYan-Hsuan Chuang void rtw_phy_dynamic_mechanism(struct rtw_dev *rtwdev) 764e3037485SYan-Hsuan Chuang { 765e3037485SYan-Hsuan Chuang /* for further calculation */ 766e3037485SYan-Hsuan Chuang rtw_phy_statistics(rtwdev); 767e3037485SYan-Hsuan Chuang rtw_phy_dig(rtwdev); 768479c4ee9STzu-En Huang rtw_phy_cck_pd(rtwdev); 76948308726SPo-Hao Huang rtw_phy_ra_track(rtwdev); 7701188301fSPo-Hao Huang rtw_phy_tx_path_diversity(rtwdev); 771fb8517f4SPo-Hao Huang rtw_phy_cfo_track(rtwdev); 7725227c2eeSTzu-En Huang rtw_phy_dpk_track(rtwdev); 773c97ee3e0STzu-En Huang rtw_phy_pwr_track(rtwdev); 774fe7bc23aSChin-Yen Lee 775fe7bc23aSChin-Yen Lee if (rtw_fw_feature_check(&rtwdev->fw, FW_FEATURE_ADAPTIVITY)) 776fe7bc23aSChin-Yen Lee rtw_fw_adaptivity(rtwdev); 777fe7bc23aSChin-Yen Lee else 7787285eb96SZong-Zhe Yang rtw_phy_adaptivity(rtwdev); 779e3037485SYan-Hsuan Chuang } 780e3037485SYan-Hsuan Chuang 781e3037485SYan-Hsuan Chuang #define FRAC_BITS 3 782e3037485SYan-Hsuan Chuang 783e3037485SYan-Hsuan Chuang static u8 rtw_phy_power_2_db(s8 power) 784e3037485SYan-Hsuan Chuang { 785e3037485SYan-Hsuan Chuang if (power <= -100 || power >= 20) 786e3037485SYan-Hsuan Chuang return 0; 787e3037485SYan-Hsuan Chuang else if (power >= 0) 788e3037485SYan-Hsuan Chuang return 100; 789e3037485SYan-Hsuan Chuang else 790e3037485SYan-Hsuan Chuang return 100 + power; 791e3037485SYan-Hsuan Chuang } 792e3037485SYan-Hsuan Chuang 793e3037485SYan-Hsuan Chuang static u64 rtw_phy_db_2_linear(u8 power_db) 794e3037485SYan-Hsuan Chuang { 795e3037485SYan-Hsuan Chuang u8 i, j; 796e3037485SYan-Hsuan Chuang u64 linear; 797e3037485SYan-Hsuan Chuang 7988a03447dSStanislaw Gruszka if (power_db > 96) 7998a03447dSStanislaw Gruszka power_db = 96; 8008a03447dSStanislaw Gruszka else if (power_db < 1) 8018a03447dSStanislaw Gruszka return 1; 8028a03447dSStanislaw Gruszka 803e3037485SYan-Hsuan Chuang /* 1dB ~ 96dB */ 804e3037485SYan-Hsuan Chuang i = (power_db - 1) >> 3; 805e3037485SYan-Hsuan Chuang j = (power_db - 1) - (i << 3); 806e3037485SYan-Hsuan Chuang 807e3037485SYan-Hsuan Chuang linear = db_invert_table[i][j]; 808e3037485SYan-Hsuan Chuang linear = i > 2 ? linear << FRAC_BITS : linear; 809e3037485SYan-Hsuan Chuang 810e3037485SYan-Hsuan Chuang return linear; 811e3037485SYan-Hsuan Chuang } 812e3037485SYan-Hsuan Chuang 813e3037485SYan-Hsuan Chuang static u8 rtw_phy_linear_2_db(u64 linear) 814e3037485SYan-Hsuan Chuang { 815e3037485SYan-Hsuan Chuang u8 i; 816e3037485SYan-Hsuan Chuang u8 j; 817e3037485SYan-Hsuan Chuang u32 dB; 818e3037485SYan-Hsuan Chuang 819e3037485SYan-Hsuan Chuang for (i = 0; i < 12; i++) { 820e3037485SYan-Hsuan Chuang for (j = 0; j < 8; j++) { 821e3037485SYan-Hsuan Chuang if (i <= 2 && (linear << FRAC_BITS) <= db_invert_table[i][j]) 822*86331c7eSZong-Zhe Yang goto cnt; 823e3037485SYan-Hsuan Chuang else if (i > 2 && linear <= db_invert_table[i][j]) 824*86331c7eSZong-Zhe Yang goto cnt; 825*86331c7eSZong-Zhe Yang } 826e3037485SYan-Hsuan Chuang } 827e3037485SYan-Hsuan Chuang 828*86331c7eSZong-Zhe Yang return 96; /* maximum 96 dB */ 829*86331c7eSZong-Zhe Yang 830*86331c7eSZong-Zhe Yang cnt: 831e3037485SYan-Hsuan Chuang if (j == 0 && i == 0) 832e3037485SYan-Hsuan Chuang goto end; 833e3037485SYan-Hsuan Chuang 834e3037485SYan-Hsuan Chuang if (j == 0) { 835e3037485SYan-Hsuan Chuang if (i != 3) { 836e3037485SYan-Hsuan Chuang if (db_invert_table[i][0] - linear > 837e3037485SYan-Hsuan Chuang linear - db_invert_table[i - 1][7]) { 838e3037485SYan-Hsuan Chuang i = i - 1; 839e3037485SYan-Hsuan Chuang j = 7; 840e3037485SYan-Hsuan Chuang } 841e3037485SYan-Hsuan Chuang } else { 842e3037485SYan-Hsuan Chuang if (db_invert_table[3][0] - linear > 843e3037485SYan-Hsuan Chuang linear - db_invert_table[2][7]) { 844e3037485SYan-Hsuan Chuang i = 2; 845e3037485SYan-Hsuan Chuang j = 7; 846e3037485SYan-Hsuan Chuang } 847e3037485SYan-Hsuan Chuang } 848e3037485SYan-Hsuan Chuang } else { 849e3037485SYan-Hsuan Chuang if (db_invert_table[i][j] - linear > 850e3037485SYan-Hsuan Chuang linear - db_invert_table[i][j - 1]) { 851e3037485SYan-Hsuan Chuang j = j - 1; 852e3037485SYan-Hsuan Chuang } 853e3037485SYan-Hsuan Chuang } 854e3037485SYan-Hsuan Chuang end: 855e3037485SYan-Hsuan Chuang dB = (i << 3) + j + 1; 856e3037485SYan-Hsuan Chuang 857e3037485SYan-Hsuan Chuang return dB; 858e3037485SYan-Hsuan Chuang } 859e3037485SYan-Hsuan Chuang 860e3037485SYan-Hsuan Chuang u8 rtw_phy_rf_power_2_rssi(s8 *rf_power, u8 path_num) 861e3037485SYan-Hsuan Chuang { 862e3037485SYan-Hsuan Chuang s8 power; 863e3037485SYan-Hsuan Chuang u8 power_db; 864e3037485SYan-Hsuan Chuang u64 linear; 865e3037485SYan-Hsuan Chuang u64 sum = 0; 866e3037485SYan-Hsuan Chuang u8 path; 867e3037485SYan-Hsuan Chuang 868e3037485SYan-Hsuan Chuang for (path = 0; path < path_num; path++) { 869e3037485SYan-Hsuan Chuang power = rf_power[path]; 870e3037485SYan-Hsuan Chuang power_db = rtw_phy_power_2_db(power); 871e3037485SYan-Hsuan Chuang linear = rtw_phy_db_2_linear(power_db); 872e3037485SYan-Hsuan Chuang sum += linear; 873e3037485SYan-Hsuan Chuang } 874e3037485SYan-Hsuan Chuang 875e3037485SYan-Hsuan Chuang sum = (sum + (1 << (FRAC_BITS - 1))) >> FRAC_BITS; 876e3037485SYan-Hsuan Chuang switch (path_num) { 877e3037485SYan-Hsuan Chuang case 2: 878e3037485SYan-Hsuan Chuang sum >>= 1; 879e3037485SYan-Hsuan Chuang break; 880e3037485SYan-Hsuan Chuang case 3: 881e3037485SYan-Hsuan Chuang sum = ((sum) + ((sum) << 1) + ((sum) << 3)) >> 5; 882e3037485SYan-Hsuan Chuang break; 883e3037485SYan-Hsuan Chuang case 4: 884e3037485SYan-Hsuan Chuang sum >>= 2; 885e3037485SYan-Hsuan Chuang break; 886e3037485SYan-Hsuan Chuang default: 887e3037485SYan-Hsuan Chuang break; 888e3037485SYan-Hsuan Chuang } 889e3037485SYan-Hsuan Chuang 890e3037485SYan-Hsuan Chuang return rtw_phy_linear_2_db(sum); 891e3037485SYan-Hsuan Chuang } 892449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_rf_power_2_rssi); 893e3037485SYan-Hsuan Chuang 894e3037485SYan-Hsuan Chuang u32 rtw_phy_read_rf(struct rtw_dev *rtwdev, enum rtw_rf_path rf_path, 895e3037485SYan-Hsuan Chuang u32 addr, u32 mask) 896e3037485SYan-Hsuan Chuang { 897e3037485SYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 898e3037485SYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 899e3037485SYan-Hsuan Chuang const u32 *base_addr = chip->rf_base_addr; 900e3037485SYan-Hsuan Chuang u32 val, direct_addr; 901e3037485SYan-Hsuan Chuang 902e0c27cdbSPing-Ke Shih if (rf_path >= hal->rf_phy_num) { 903e3037485SYan-Hsuan Chuang rtw_err(rtwdev, "unsupported rf path (%d)\n", rf_path); 904e3037485SYan-Hsuan Chuang return INV_RF_DATA; 905e3037485SYan-Hsuan Chuang } 906e3037485SYan-Hsuan Chuang 907e3037485SYan-Hsuan Chuang addr &= 0xff; 908e3037485SYan-Hsuan Chuang direct_addr = base_addr[rf_path] + (addr << 2); 909e3037485SYan-Hsuan Chuang mask &= RFREG_MASK; 910e3037485SYan-Hsuan Chuang 911e3037485SYan-Hsuan Chuang val = rtw_read32_mask(rtwdev, direct_addr, mask); 912e3037485SYan-Hsuan Chuang 913e3037485SYan-Hsuan Chuang return val; 914e3037485SYan-Hsuan Chuang } 915449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_read_rf); 916e3037485SYan-Hsuan Chuang 917e0c27cdbSPing-Ke Shih u32 rtw_phy_read_rf_sipi(struct rtw_dev *rtwdev, enum rtw_rf_path rf_path, 918e0c27cdbSPing-Ke Shih u32 addr, u32 mask) 919e0c27cdbSPing-Ke Shih { 920e0c27cdbSPing-Ke Shih struct rtw_hal *hal = &rtwdev->hal; 921e0c27cdbSPing-Ke Shih struct rtw_chip_info *chip = rtwdev->chip; 922e0c27cdbSPing-Ke Shih const struct rtw_rf_sipi_addr *rf_sipi_addr; 923e0c27cdbSPing-Ke Shih const struct rtw_rf_sipi_addr *rf_sipi_addr_a; 924e0c27cdbSPing-Ke Shih u32 val32; 925e0c27cdbSPing-Ke Shih u32 en_pi; 926e0c27cdbSPing-Ke Shih u32 r_addr; 927e0c27cdbSPing-Ke Shih u32 shift; 928e0c27cdbSPing-Ke Shih 929e0c27cdbSPing-Ke Shih if (rf_path >= hal->rf_phy_num) { 930e0c27cdbSPing-Ke Shih rtw_err(rtwdev, "unsupported rf path (%d)\n", rf_path); 931e0c27cdbSPing-Ke Shih return INV_RF_DATA; 932e0c27cdbSPing-Ke Shih } 933e0c27cdbSPing-Ke Shih 934e0c27cdbSPing-Ke Shih if (!chip->rf_sipi_read_addr) { 935e0c27cdbSPing-Ke Shih rtw_err(rtwdev, "rf_sipi_read_addr isn't defined\n"); 936e0c27cdbSPing-Ke Shih return INV_RF_DATA; 937e0c27cdbSPing-Ke Shih } 938e0c27cdbSPing-Ke Shih 939e0c27cdbSPing-Ke Shih rf_sipi_addr = &chip->rf_sipi_read_addr[rf_path]; 940e0c27cdbSPing-Ke Shih rf_sipi_addr_a = &chip->rf_sipi_read_addr[RF_PATH_A]; 941e0c27cdbSPing-Ke Shih 942e0c27cdbSPing-Ke Shih addr &= 0xff; 943e0c27cdbSPing-Ke Shih 944e0c27cdbSPing-Ke Shih val32 = rtw_read32(rtwdev, rf_sipi_addr->hssi_2); 945e0c27cdbSPing-Ke Shih val32 = (val32 & ~LSSI_READ_ADDR_MASK) | (addr << 23); 946e0c27cdbSPing-Ke Shih rtw_write32(rtwdev, rf_sipi_addr->hssi_2, val32); 947e0c27cdbSPing-Ke Shih 948e0c27cdbSPing-Ke Shih /* toggle read edge of path A */ 949e0c27cdbSPing-Ke Shih val32 = rtw_read32(rtwdev, rf_sipi_addr_a->hssi_2); 950e0c27cdbSPing-Ke Shih rtw_write32(rtwdev, rf_sipi_addr_a->hssi_2, val32 & ~LSSI_READ_EDGE_MASK); 951e0c27cdbSPing-Ke Shih rtw_write32(rtwdev, rf_sipi_addr_a->hssi_2, val32 | LSSI_READ_EDGE_MASK); 952e0c27cdbSPing-Ke Shih 953e0c27cdbSPing-Ke Shih udelay(120); 954e0c27cdbSPing-Ke Shih 955e0c27cdbSPing-Ke Shih en_pi = rtw_read32_mask(rtwdev, rf_sipi_addr->hssi_1, BIT(8)); 956e0c27cdbSPing-Ke Shih r_addr = en_pi ? rf_sipi_addr->lssi_read_pi : rf_sipi_addr->lssi_read; 957e0c27cdbSPing-Ke Shih 958e0c27cdbSPing-Ke Shih val32 = rtw_read32_mask(rtwdev, r_addr, LSSI_READ_DATA_MASK); 959e0c27cdbSPing-Ke Shih 960e0c27cdbSPing-Ke Shih shift = __ffs(mask); 961e0c27cdbSPing-Ke Shih 962e0c27cdbSPing-Ke Shih return (val32 & mask) >> shift; 963e0c27cdbSPing-Ke Shih } 964449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_read_rf_sipi); 965e0c27cdbSPing-Ke Shih 966e3037485SYan-Hsuan Chuang bool rtw_phy_write_rf_reg_sipi(struct rtw_dev *rtwdev, enum rtw_rf_path rf_path, 967e3037485SYan-Hsuan Chuang u32 addr, u32 mask, u32 data) 968e3037485SYan-Hsuan Chuang { 969e3037485SYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 970e3037485SYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 971e3037485SYan-Hsuan Chuang u32 *sipi_addr = chip->rf_sipi_addr; 972e3037485SYan-Hsuan Chuang u32 data_and_addr; 973e3037485SYan-Hsuan Chuang u32 old_data = 0; 974e3037485SYan-Hsuan Chuang u32 shift; 975e3037485SYan-Hsuan Chuang 976e0c27cdbSPing-Ke Shih if (rf_path >= hal->rf_phy_num) { 977e3037485SYan-Hsuan Chuang rtw_err(rtwdev, "unsupported rf path (%d)\n", rf_path); 978e3037485SYan-Hsuan Chuang return false; 979e3037485SYan-Hsuan Chuang } 980e3037485SYan-Hsuan Chuang 981e3037485SYan-Hsuan Chuang addr &= 0xff; 982e3037485SYan-Hsuan Chuang mask &= RFREG_MASK; 983e3037485SYan-Hsuan Chuang 984e3037485SYan-Hsuan Chuang if (mask != RFREG_MASK) { 985e0c27cdbSPing-Ke Shih old_data = chip->ops->read_rf(rtwdev, rf_path, addr, RFREG_MASK); 986e3037485SYan-Hsuan Chuang 987e3037485SYan-Hsuan Chuang if (old_data == INV_RF_DATA) { 988e3037485SYan-Hsuan Chuang rtw_err(rtwdev, "Write fail, rf is disabled\n"); 989e3037485SYan-Hsuan Chuang return false; 990e3037485SYan-Hsuan Chuang } 991e3037485SYan-Hsuan Chuang 992e3037485SYan-Hsuan Chuang shift = __ffs(mask); 993e3037485SYan-Hsuan Chuang data = ((old_data) & (~mask)) | (data << shift); 994e3037485SYan-Hsuan Chuang } 995e3037485SYan-Hsuan Chuang 996e3037485SYan-Hsuan Chuang data_and_addr = ((addr << 20) | (data & 0x000fffff)) & 0x0fffffff; 997e3037485SYan-Hsuan Chuang 998e3037485SYan-Hsuan Chuang rtw_write32(rtwdev, sipi_addr[rf_path], data_and_addr); 999e3037485SYan-Hsuan Chuang 1000e3037485SYan-Hsuan Chuang udelay(13); 1001e3037485SYan-Hsuan Chuang 1002e3037485SYan-Hsuan Chuang return true; 1003e3037485SYan-Hsuan Chuang } 1004449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_write_rf_reg_sipi); 1005e3037485SYan-Hsuan Chuang 1006e3037485SYan-Hsuan Chuang bool rtw_phy_write_rf_reg(struct rtw_dev *rtwdev, enum rtw_rf_path rf_path, 1007e3037485SYan-Hsuan Chuang u32 addr, u32 mask, u32 data) 1008e3037485SYan-Hsuan Chuang { 1009e3037485SYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 1010e3037485SYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 1011e3037485SYan-Hsuan Chuang const u32 *base_addr = chip->rf_base_addr; 1012e3037485SYan-Hsuan Chuang u32 direct_addr; 1013e3037485SYan-Hsuan Chuang 1014e0c27cdbSPing-Ke Shih if (rf_path >= hal->rf_phy_num) { 1015e3037485SYan-Hsuan Chuang rtw_err(rtwdev, "unsupported rf path (%d)\n", rf_path); 1016e3037485SYan-Hsuan Chuang return false; 1017e3037485SYan-Hsuan Chuang } 1018e3037485SYan-Hsuan Chuang 1019e3037485SYan-Hsuan Chuang addr &= 0xff; 1020e3037485SYan-Hsuan Chuang direct_addr = base_addr[rf_path] + (addr << 2); 1021e3037485SYan-Hsuan Chuang mask &= RFREG_MASK; 1022e3037485SYan-Hsuan Chuang 1023e3037485SYan-Hsuan Chuang rtw_write32_mask(rtwdev, direct_addr, mask, data); 1024e3037485SYan-Hsuan Chuang 1025e3037485SYan-Hsuan Chuang udelay(1); 1026e3037485SYan-Hsuan Chuang 1027e3037485SYan-Hsuan Chuang return true; 1028e3037485SYan-Hsuan Chuang } 1029e3037485SYan-Hsuan Chuang 1030e3037485SYan-Hsuan Chuang bool rtw_phy_write_rf_reg_mix(struct rtw_dev *rtwdev, enum rtw_rf_path rf_path, 1031e3037485SYan-Hsuan Chuang u32 addr, u32 mask, u32 data) 1032e3037485SYan-Hsuan Chuang { 1033e3037485SYan-Hsuan Chuang if (addr != 0x00) 1034e3037485SYan-Hsuan Chuang return rtw_phy_write_rf_reg(rtwdev, rf_path, addr, mask, data); 1035e3037485SYan-Hsuan Chuang 1036e3037485SYan-Hsuan Chuang return rtw_phy_write_rf_reg_sipi(rtwdev, rf_path, addr, mask, data); 1037e3037485SYan-Hsuan Chuang } 1038449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_write_rf_reg_mix); 1039e3037485SYan-Hsuan Chuang 1040e3037485SYan-Hsuan Chuang void rtw_phy_setup_phy_cond(struct rtw_dev *rtwdev, u32 pkg) 1041e3037485SYan-Hsuan Chuang { 1042e3037485SYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 1043e3037485SYan-Hsuan Chuang struct rtw_efuse *efuse = &rtwdev->efuse; 1044e3037485SYan-Hsuan Chuang struct rtw_phy_cond cond = {0}; 1045e3037485SYan-Hsuan Chuang 1046e3037485SYan-Hsuan Chuang cond.cut = hal->cut_version ? hal->cut_version : 15; 1047e3037485SYan-Hsuan Chuang cond.pkg = pkg ? pkg : 15; 1048e3037485SYan-Hsuan Chuang cond.plat = 0x04; 1049e3037485SYan-Hsuan Chuang cond.rfe = efuse->rfe_option; 1050e3037485SYan-Hsuan Chuang 1051e3037485SYan-Hsuan Chuang switch (rtw_hci_type(rtwdev)) { 1052e3037485SYan-Hsuan Chuang case RTW_HCI_TYPE_USB: 1053e3037485SYan-Hsuan Chuang cond.intf = INTF_USB; 1054e3037485SYan-Hsuan Chuang break; 1055e3037485SYan-Hsuan Chuang case RTW_HCI_TYPE_SDIO: 1056e3037485SYan-Hsuan Chuang cond.intf = INTF_SDIO; 1057e3037485SYan-Hsuan Chuang break; 1058e3037485SYan-Hsuan Chuang case RTW_HCI_TYPE_PCIE: 1059e3037485SYan-Hsuan Chuang default: 1060e3037485SYan-Hsuan Chuang cond.intf = INTF_PCIE; 1061e3037485SYan-Hsuan Chuang break; 1062e3037485SYan-Hsuan Chuang } 1063e3037485SYan-Hsuan Chuang 1064e3037485SYan-Hsuan Chuang hal->phy_cond = cond; 1065e3037485SYan-Hsuan Chuang 1066e3037485SYan-Hsuan Chuang rtw_dbg(rtwdev, RTW_DBG_PHY, "phy cond=0x%08x\n", *((u32 *)&hal->phy_cond)); 1067e3037485SYan-Hsuan Chuang } 1068e3037485SYan-Hsuan Chuang 1069e3037485SYan-Hsuan Chuang static bool check_positive(struct rtw_dev *rtwdev, struct rtw_phy_cond cond) 1070e3037485SYan-Hsuan Chuang { 1071e3037485SYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 1072e3037485SYan-Hsuan Chuang struct rtw_phy_cond drv_cond = hal->phy_cond; 1073e3037485SYan-Hsuan Chuang 1074e3037485SYan-Hsuan Chuang if (cond.cut && cond.cut != drv_cond.cut) 1075e3037485SYan-Hsuan Chuang return false; 1076e3037485SYan-Hsuan Chuang 1077e3037485SYan-Hsuan Chuang if (cond.pkg && cond.pkg != drv_cond.pkg) 1078e3037485SYan-Hsuan Chuang return false; 1079e3037485SYan-Hsuan Chuang 1080e3037485SYan-Hsuan Chuang if (cond.intf && cond.intf != drv_cond.intf) 1081e3037485SYan-Hsuan Chuang return false; 1082e3037485SYan-Hsuan Chuang 1083e3037485SYan-Hsuan Chuang if (cond.rfe != drv_cond.rfe) 1084e3037485SYan-Hsuan Chuang return false; 1085e3037485SYan-Hsuan Chuang 1086e3037485SYan-Hsuan Chuang return true; 1087e3037485SYan-Hsuan Chuang } 1088e3037485SYan-Hsuan Chuang 1089e3037485SYan-Hsuan Chuang void rtw_parse_tbl_phy_cond(struct rtw_dev *rtwdev, const struct rtw_table *tbl) 1090e3037485SYan-Hsuan Chuang { 1091e3037485SYan-Hsuan Chuang const union phy_table_tile *p = tbl->data; 1092e3037485SYan-Hsuan Chuang const union phy_table_tile *end = p + tbl->size / 2; 1093e3037485SYan-Hsuan Chuang struct rtw_phy_cond pos_cond = {0}; 1094e3037485SYan-Hsuan Chuang bool is_matched = true, is_skipped = false; 1095e3037485SYan-Hsuan Chuang 1096e3037485SYan-Hsuan Chuang BUILD_BUG_ON(sizeof(union phy_table_tile) != sizeof(struct phy_cfg_pair)); 1097e3037485SYan-Hsuan Chuang 1098e3037485SYan-Hsuan Chuang for (; p < end; p++) { 1099e3037485SYan-Hsuan Chuang if (p->cond.pos) { 1100e3037485SYan-Hsuan Chuang switch (p->cond.branch) { 1101e3037485SYan-Hsuan Chuang case BRANCH_ENDIF: 1102e3037485SYan-Hsuan Chuang is_matched = true; 1103e3037485SYan-Hsuan Chuang is_skipped = false; 1104e3037485SYan-Hsuan Chuang break; 1105e3037485SYan-Hsuan Chuang case BRANCH_ELSE: 1106e3037485SYan-Hsuan Chuang is_matched = is_skipped ? false : true; 1107e3037485SYan-Hsuan Chuang break; 1108e3037485SYan-Hsuan Chuang case BRANCH_IF: 1109e3037485SYan-Hsuan Chuang case BRANCH_ELIF: 1110e3037485SYan-Hsuan Chuang default: 1111e3037485SYan-Hsuan Chuang pos_cond = p->cond; 1112e3037485SYan-Hsuan Chuang break; 1113e3037485SYan-Hsuan Chuang } 1114e3037485SYan-Hsuan Chuang } else if (p->cond.neg) { 1115e3037485SYan-Hsuan Chuang if (!is_skipped) { 1116e3037485SYan-Hsuan Chuang if (check_positive(rtwdev, pos_cond)) { 1117e3037485SYan-Hsuan Chuang is_matched = true; 1118e3037485SYan-Hsuan Chuang is_skipped = true; 1119e3037485SYan-Hsuan Chuang } else { 1120e3037485SYan-Hsuan Chuang is_matched = false; 1121e3037485SYan-Hsuan Chuang is_skipped = false; 1122e3037485SYan-Hsuan Chuang } 1123e3037485SYan-Hsuan Chuang } else { 1124e3037485SYan-Hsuan Chuang is_matched = false; 1125e3037485SYan-Hsuan Chuang } 1126e3037485SYan-Hsuan Chuang } else if (is_matched) { 1127e3037485SYan-Hsuan Chuang (*tbl->do_cfg)(rtwdev, tbl, p->cfg.addr, p->cfg.data); 1128e3037485SYan-Hsuan Chuang } 1129e3037485SYan-Hsuan Chuang } 1130e3037485SYan-Hsuan Chuang } 1131449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_parse_tbl_phy_cond); 1132e3037485SYan-Hsuan Chuang 1133e3037485SYan-Hsuan Chuang #define bcd_to_dec_pwr_by_rate(val, i) bcd2bin(val >> (i * 8)) 1134e3037485SYan-Hsuan Chuang 1135e3037485SYan-Hsuan Chuang static u8 tbl_to_dec_pwr_by_rate(struct rtw_dev *rtwdev, u32 hex, u8 i) 1136e3037485SYan-Hsuan Chuang { 1137e3037485SYan-Hsuan Chuang if (rtwdev->chip->is_pwr_by_rate_dec) 1138e3037485SYan-Hsuan Chuang return bcd_to_dec_pwr_by_rate(hex, i); 1139fa6dfe6bSYan-Hsuan Chuang 1140e3037485SYan-Hsuan Chuang return (hex >> (i * 8)) & 0xFF; 1141e3037485SYan-Hsuan Chuang } 1142e3037485SYan-Hsuan Chuang 114343712199SYan-Hsuan Chuang static void 114443712199SYan-Hsuan Chuang rtw_phy_get_rate_values_of_txpwr_by_rate(struct rtw_dev *rtwdev, 114543712199SYan-Hsuan Chuang u32 addr, u32 mask, u32 val, u8 *rate, 1146e3037485SYan-Hsuan Chuang u8 *pwr_by_rate, u8 *rate_num) 1147e3037485SYan-Hsuan Chuang { 1148e3037485SYan-Hsuan Chuang int i; 1149e3037485SYan-Hsuan Chuang 1150e3037485SYan-Hsuan Chuang switch (addr) { 1151e3037485SYan-Hsuan Chuang case 0xE00: 1152e3037485SYan-Hsuan Chuang case 0x830: 1153e3037485SYan-Hsuan Chuang rate[0] = DESC_RATE6M; 1154e3037485SYan-Hsuan Chuang rate[1] = DESC_RATE9M; 1155e3037485SYan-Hsuan Chuang rate[2] = DESC_RATE12M; 1156e3037485SYan-Hsuan Chuang rate[3] = DESC_RATE18M; 1157e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1158e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1159e3037485SYan-Hsuan Chuang *rate_num = 4; 1160e3037485SYan-Hsuan Chuang break; 1161e3037485SYan-Hsuan Chuang case 0xE04: 1162e3037485SYan-Hsuan Chuang case 0x834: 1163e3037485SYan-Hsuan Chuang rate[0] = DESC_RATE24M; 1164e3037485SYan-Hsuan Chuang rate[1] = DESC_RATE36M; 1165e3037485SYan-Hsuan Chuang rate[2] = DESC_RATE48M; 1166e3037485SYan-Hsuan Chuang rate[3] = DESC_RATE54M; 1167e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1168e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1169e3037485SYan-Hsuan Chuang *rate_num = 4; 1170e3037485SYan-Hsuan Chuang break; 1171e3037485SYan-Hsuan Chuang case 0xE08: 1172e3037485SYan-Hsuan Chuang rate[0] = DESC_RATE1M; 1173e3037485SYan-Hsuan Chuang pwr_by_rate[0] = bcd_to_dec_pwr_by_rate(val, 1); 1174e3037485SYan-Hsuan Chuang *rate_num = 1; 1175e3037485SYan-Hsuan Chuang break; 1176e3037485SYan-Hsuan Chuang case 0x86C: 1177e3037485SYan-Hsuan Chuang if (mask == 0xffffff00) { 1178e3037485SYan-Hsuan Chuang rate[0] = DESC_RATE2M; 1179e3037485SYan-Hsuan Chuang rate[1] = DESC_RATE5_5M; 1180e3037485SYan-Hsuan Chuang rate[2] = DESC_RATE11M; 1181e3037485SYan-Hsuan Chuang for (i = 1; i < 4; ++i) 1182e3037485SYan-Hsuan Chuang pwr_by_rate[i - 1] = 1183e3037485SYan-Hsuan Chuang tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1184e3037485SYan-Hsuan Chuang *rate_num = 3; 1185e3037485SYan-Hsuan Chuang } else if (mask == 0x000000ff) { 1186e3037485SYan-Hsuan Chuang rate[0] = DESC_RATE11M; 1187e3037485SYan-Hsuan Chuang pwr_by_rate[0] = bcd_to_dec_pwr_by_rate(val, 0); 1188e3037485SYan-Hsuan Chuang *rate_num = 1; 1189e3037485SYan-Hsuan Chuang } 1190e3037485SYan-Hsuan Chuang break; 1191e3037485SYan-Hsuan Chuang case 0xE10: 1192e3037485SYan-Hsuan Chuang case 0x83C: 1193e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS0; 1194e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS1; 1195e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS2; 1196e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS3; 1197e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1198e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1199e3037485SYan-Hsuan Chuang *rate_num = 4; 1200e3037485SYan-Hsuan Chuang break; 1201e3037485SYan-Hsuan Chuang case 0xE14: 1202e3037485SYan-Hsuan Chuang case 0x848: 1203e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS4; 1204e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS5; 1205e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS6; 1206e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS7; 1207e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1208e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1209e3037485SYan-Hsuan Chuang *rate_num = 4; 1210e3037485SYan-Hsuan Chuang break; 1211e3037485SYan-Hsuan Chuang case 0xE18: 1212e3037485SYan-Hsuan Chuang case 0x84C: 1213e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS8; 1214e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS9; 1215e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS10; 1216e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS11; 1217e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1218e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1219e3037485SYan-Hsuan Chuang *rate_num = 4; 1220e3037485SYan-Hsuan Chuang break; 1221e3037485SYan-Hsuan Chuang case 0xE1C: 1222e3037485SYan-Hsuan Chuang case 0x868: 1223e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS12; 1224e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS13; 1225e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS14; 1226e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS15; 1227e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1228e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1229e3037485SYan-Hsuan Chuang *rate_num = 4; 1230e3037485SYan-Hsuan Chuang break; 1231e3037485SYan-Hsuan Chuang case 0x838: 1232e3037485SYan-Hsuan Chuang rate[0] = DESC_RATE1M; 1233e3037485SYan-Hsuan Chuang rate[1] = DESC_RATE2M; 1234e3037485SYan-Hsuan Chuang rate[2] = DESC_RATE5_5M; 1235e3037485SYan-Hsuan Chuang for (i = 1; i < 4; ++i) 1236e3037485SYan-Hsuan Chuang pwr_by_rate[i - 1] = tbl_to_dec_pwr_by_rate(rtwdev, 1237e3037485SYan-Hsuan Chuang val, i); 1238e3037485SYan-Hsuan Chuang *rate_num = 3; 1239e3037485SYan-Hsuan Chuang break; 1240e3037485SYan-Hsuan Chuang case 0xC20: 1241e3037485SYan-Hsuan Chuang case 0xE20: 1242e3037485SYan-Hsuan Chuang case 0x1820: 1243e3037485SYan-Hsuan Chuang case 0x1A20: 1244e3037485SYan-Hsuan Chuang rate[0] = DESC_RATE1M; 1245e3037485SYan-Hsuan Chuang rate[1] = DESC_RATE2M; 1246e3037485SYan-Hsuan Chuang rate[2] = DESC_RATE5_5M; 1247e3037485SYan-Hsuan Chuang rate[3] = DESC_RATE11M; 1248e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1249e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1250e3037485SYan-Hsuan Chuang *rate_num = 4; 1251e3037485SYan-Hsuan Chuang break; 1252e3037485SYan-Hsuan Chuang case 0xC24: 1253e3037485SYan-Hsuan Chuang case 0xE24: 1254e3037485SYan-Hsuan Chuang case 0x1824: 1255e3037485SYan-Hsuan Chuang case 0x1A24: 1256e3037485SYan-Hsuan Chuang rate[0] = DESC_RATE6M; 1257e3037485SYan-Hsuan Chuang rate[1] = DESC_RATE9M; 1258e3037485SYan-Hsuan Chuang rate[2] = DESC_RATE12M; 1259e3037485SYan-Hsuan Chuang rate[3] = DESC_RATE18M; 1260e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1261e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1262e3037485SYan-Hsuan Chuang *rate_num = 4; 1263e3037485SYan-Hsuan Chuang break; 1264e3037485SYan-Hsuan Chuang case 0xC28: 1265e3037485SYan-Hsuan Chuang case 0xE28: 1266e3037485SYan-Hsuan Chuang case 0x1828: 1267e3037485SYan-Hsuan Chuang case 0x1A28: 1268e3037485SYan-Hsuan Chuang rate[0] = DESC_RATE24M; 1269e3037485SYan-Hsuan Chuang rate[1] = DESC_RATE36M; 1270e3037485SYan-Hsuan Chuang rate[2] = DESC_RATE48M; 1271e3037485SYan-Hsuan Chuang rate[3] = DESC_RATE54M; 1272e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1273e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1274e3037485SYan-Hsuan Chuang *rate_num = 4; 1275e3037485SYan-Hsuan Chuang break; 1276e3037485SYan-Hsuan Chuang case 0xC2C: 1277e3037485SYan-Hsuan Chuang case 0xE2C: 1278e3037485SYan-Hsuan Chuang case 0x182C: 1279e3037485SYan-Hsuan Chuang case 0x1A2C: 1280e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS0; 1281e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS1; 1282e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS2; 1283e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS3; 1284e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1285e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1286e3037485SYan-Hsuan Chuang *rate_num = 4; 1287e3037485SYan-Hsuan Chuang break; 1288e3037485SYan-Hsuan Chuang case 0xC30: 1289e3037485SYan-Hsuan Chuang case 0xE30: 1290e3037485SYan-Hsuan Chuang case 0x1830: 1291e3037485SYan-Hsuan Chuang case 0x1A30: 1292e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS4; 1293e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS5; 1294e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS6; 1295e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS7; 1296e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1297e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1298e3037485SYan-Hsuan Chuang *rate_num = 4; 1299e3037485SYan-Hsuan Chuang break; 1300e3037485SYan-Hsuan Chuang case 0xC34: 1301e3037485SYan-Hsuan Chuang case 0xE34: 1302e3037485SYan-Hsuan Chuang case 0x1834: 1303e3037485SYan-Hsuan Chuang case 0x1A34: 1304e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS8; 1305e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS9; 1306e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS10; 1307e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS11; 1308e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1309e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1310e3037485SYan-Hsuan Chuang *rate_num = 4; 1311e3037485SYan-Hsuan Chuang break; 1312e3037485SYan-Hsuan Chuang case 0xC38: 1313e3037485SYan-Hsuan Chuang case 0xE38: 1314e3037485SYan-Hsuan Chuang case 0x1838: 1315e3037485SYan-Hsuan Chuang case 0x1A38: 1316e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS12; 1317e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS13; 1318e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS14; 1319e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS15; 1320e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1321e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1322e3037485SYan-Hsuan Chuang *rate_num = 4; 1323e3037485SYan-Hsuan Chuang break; 1324e3037485SYan-Hsuan Chuang case 0xC3C: 1325e3037485SYan-Hsuan Chuang case 0xE3C: 1326e3037485SYan-Hsuan Chuang case 0x183C: 1327e3037485SYan-Hsuan Chuang case 0x1A3C: 1328e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEVHT1SS_MCS0; 1329e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEVHT1SS_MCS1; 1330e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEVHT1SS_MCS2; 1331e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEVHT1SS_MCS3; 1332e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1333e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1334e3037485SYan-Hsuan Chuang *rate_num = 4; 1335e3037485SYan-Hsuan Chuang break; 1336e3037485SYan-Hsuan Chuang case 0xC40: 1337e3037485SYan-Hsuan Chuang case 0xE40: 1338e3037485SYan-Hsuan Chuang case 0x1840: 1339e3037485SYan-Hsuan Chuang case 0x1A40: 1340e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEVHT1SS_MCS4; 1341e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEVHT1SS_MCS5; 1342e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEVHT1SS_MCS6; 1343e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEVHT1SS_MCS7; 1344e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1345e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1346e3037485SYan-Hsuan Chuang *rate_num = 4; 1347e3037485SYan-Hsuan Chuang break; 1348e3037485SYan-Hsuan Chuang case 0xC44: 1349e3037485SYan-Hsuan Chuang case 0xE44: 1350e3037485SYan-Hsuan Chuang case 0x1844: 1351e3037485SYan-Hsuan Chuang case 0x1A44: 1352e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEVHT1SS_MCS8; 1353e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEVHT1SS_MCS9; 1354e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEVHT2SS_MCS0; 1355e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEVHT2SS_MCS1; 1356e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1357e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1358e3037485SYan-Hsuan Chuang *rate_num = 4; 1359e3037485SYan-Hsuan Chuang break; 1360e3037485SYan-Hsuan Chuang case 0xC48: 1361e3037485SYan-Hsuan Chuang case 0xE48: 1362e3037485SYan-Hsuan Chuang case 0x1848: 1363e3037485SYan-Hsuan Chuang case 0x1A48: 1364e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEVHT2SS_MCS2; 1365e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEVHT2SS_MCS3; 1366e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEVHT2SS_MCS4; 1367e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEVHT2SS_MCS5; 1368e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1369e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1370e3037485SYan-Hsuan Chuang *rate_num = 4; 1371e3037485SYan-Hsuan Chuang break; 1372e3037485SYan-Hsuan Chuang case 0xC4C: 1373e3037485SYan-Hsuan Chuang case 0xE4C: 1374e3037485SYan-Hsuan Chuang case 0x184C: 1375e3037485SYan-Hsuan Chuang case 0x1A4C: 1376e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEVHT2SS_MCS6; 1377e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEVHT2SS_MCS7; 1378e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEVHT2SS_MCS8; 1379e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEVHT2SS_MCS9; 1380e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1381e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1382e3037485SYan-Hsuan Chuang *rate_num = 4; 1383e3037485SYan-Hsuan Chuang break; 1384e3037485SYan-Hsuan Chuang case 0xCD8: 1385e3037485SYan-Hsuan Chuang case 0xED8: 1386e3037485SYan-Hsuan Chuang case 0x18D8: 1387e3037485SYan-Hsuan Chuang case 0x1AD8: 1388e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS16; 1389e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS17; 1390e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS18; 1391e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS19; 1392e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1393e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1394e3037485SYan-Hsuan Chuang *rate_num = 4; 1395e3037485SYan-Hsuan Chuang break; 1396e3037485SYan-Hsuan Chuang case 0xCDC: 1397e3037485SYan-Hsuan Chuang case 0xEDC: 1398e3037485SYan-Hsuan Chuang case 0x18DC: 1399e3037485SYan-Hsuan Chuang case 0x1ADC: 1400e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEMCS20; 1401e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEMCS21; 1402e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEMCS22; 1403e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEMCS23; 1404e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1405e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1406e3037485SYan-Hsuan Chuang *rate_num = 4; 1407e3037485SYan-Hsuan Chuang break; 1408e3037485SYan-Hsuan Chuang case 0xCE0: 1409e3037485SYan-Hsuan Chuang case 0xEE0: 1410e3037485SYan-Hsuan Chuang case 0x18E0: 1411e3037485SYan-Hsuan Chuang case 0x1AE0: 1412e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEVHT3SS_MCS0; 1413e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEVHT3SS_MCS1; 1414e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEVHT3SS_MCS2; 1415e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEVHT3SS_MCS3; 1416e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1417e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1418e3037485SYan-Hsuan Chuang *rate_num = 4; 1419e3037485SYan-Hsuan Chuang break; 1420e3037485SYan-Hsuan Chuang case 0xCE4: 1421e3037485SYan-Hsuan Chuang case 0xEE4: 1422e3037485SYan-Hsuan Chuang case 0x18E4: 1423e3037485SYan-Hsuan Chuang case 0x1AE4: 1424e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEVHT3SS_MCS4; 1425e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEVHT3SS_MCS5; 1426e3037485SYan-Hsuan Chuang rate[2] = DESC_RATEVHT3SS_MCS6; 1427e3037485SYan-Hsuan Chuang rate[3] = DESC_RATEVHT3SS_MCS7; 1428e3037485SYan-Hsuan Chuang for (i = 0; i < 4; ++i) 1429e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1430e3037485SYan-Hsuan Chuang *rate_num = 4; 1431e3037485SYan-Hsuan Chuang break; 1432e3037485SYan-Hsuan Chuang case 0xCE8: 1433e3037485SYan-Hsuan Chuang case 0xEE8: 1434e3037485SYan-Hsuan Chuang case 0x18E8: 1435e3037485SYan-Hsuan Chuang case 0x1AE8: 1436e3037485SYan-Hsuan Chuang rate[0] = DESC_RATEVHT3SS_MCS8; 1437e3037485SYan-Hsuan Chuang rate[1] = DESC_RATEVHT3SS_MCS9; 1438e3037485SYan-Hsuan Chuang for (i = 0; i < 2; ++i) 1439e3037485SYan-Hsuan Chuang pwr_by_rate[i] = tbl_to_dec_pwr_by_rate(rtwdev, val, i); 1440e3037485SYan-Hsuan Chuang *rate_num = 2; 1441e3037485SYan-Hsuan Chuang break; 1442e3037485SYan-Hsuan Chuang default: 1443e3037485SYan-Hsuan Chuang rtw_warn(rtwdev, "invalid tx power index addr 0x%08x\n", addr); 1444e3037485SYan-Hsuan Chuang break; 1445e3037485SYan-Hsuan Chuang } 1446e3037485SYan-Hsuan Chuang } 1447e3037485SYan-Hsuan Chuang 144843712199SYan-Hsuan Chuang static void rtw_phy_store_tx_power_by_rate(struct rtw_dev *rtwdev, 1449fa6dfe6bSYan-Hsuan Chuang u32 band, u32 rfpath, u32 txnum, 1450e3037485SYan-Hsuan Chuang u32 regaddr, u32 bitmask, u32 data) 1451e3037485SYan-Hsuan Chuang { 1452e3037485SYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 1453e3037485SYan-Hsuan Chuang u8 rate_num = 0; 1454e3037485SYan-Hsuan Chuang u8 rate; 1455e3037485SYan-Hsuan Chuang u8 rates[RTW_RF_PATH_MAX] = {0}; 1456e3037485SYan-Hsuan Chuang s8 offset; 1457e3037485SYan-Hsuan Chuang s8 pwr_by_rate[RTW_RF_PATH_MAX] = {0}; 1458e3037485SYan-Hsuan Chuang int i; 1459e3037485SYan-Hsuan Chuang 146043712199SYan-Hsuan Chuang rtw_phy_get_rate_values_of_txpwr_by_rate(rtwdev, regaddr, bitmask, data, 1461e3037485SYan-Hsuan Chuang rates, pwr_by_rate, &rate_num); 1462e3037485SYan-Hsuan Chuang 1463e3037485SYan-Hsuan Chuang if (WARN_ON(rfpath >= RTW_RF_PATH_MAX || 1464e3037485SYan-Hsuan Chuang (band != PHY_BAND_2G && band != PHY_BAND_5G) || 1465e3037485SYan-Hsuan Chuang rate_num > RTW_RF_PATH_MAX)) 1466e3037485SYan-Hsuan Chuang return; 1467e3037485SYan-Hsuan Chuang 1468e3037485SYan-Hsuan Chuang for (i = 0; i < rate_num; i++) { 1469e3037485SYan-Hsuan Chuang offset = pwr_by_rate[i]; 1470e3037485SYan-Hsuan Chuang rate = rates[i]; 1471e3037485SYan-Hsuan Chuang if (band == PHY_BAND_2G) 1472e3037485SYan-Hsuan Chuang hal->tx_pwr_by_rate_offset_2g[rfpath][rate] = offset; 1473e3037485SYan-Hsuan Chuang else if (band == PHY_BAND_5G) 1474e3037485SYan-Hsuan Chuang hal->tx_pwr_by_rate_offset_5g[rfpath][rate] = offset; 1475e3037485SYan-Hsuan Chuang else 1476e3037485SYan-Hsuan Chuang continue; 1477e3037485SYan-Hsuan Chuang } 1478e3037485SYan-Hsuan Chuang } 1479e3037485SYan-Hsuan Chuang 1480fa6dfe6bSYan-Hsuan Chuang void rtw_parse_tbl_bb_pg(struct rtw_dev *rtwdev, const struct rtw_table *tbl) 1481fa6dfe6bSYan-Hsuan Chuang { 14820b8db87dSYan-Hsuan Chuang const struct rtw_phy_pg_cfg_pair *p = tbl->data; 14830b8db87dSYan-Hsuan Chuang const struct rtw_phy_pg_cfg_pair *end = p + tbl->size; 1484fa6dfe6bSYan-Hsuan Chuang 1485fa6dfe6bSYan-Hsuan Chuang for (; p < end; p++) { 1486fa6dfe6bSYan-Hsuan Chuang if (p->addr == 0xfe || p->addr == 0xffe) { 1487fa6dfe6bSYan-Hsuan Chuang msleep(50); 1488fa6dfe6bSYan-Hsuan Chuang continue; 1489fa6dfe6bSYan-Hsuan Chuang } 149043712199SYan-Hsuan Chuang rtw_phy_store_tx_power_by_rate(rtwdev, p->band, p->rf_path, 1491fa6dfe6bSYan-Hsuan Chuang p->tx_num, p->addr, p->bitmask, 1492fa6dfe6bSYan-Hsuan Chuang p->data); 1493fa6dfe6bSYan-Hsuan Chuang } 1494fa6dfe6bSYan-Hsuan Chuang } 1495449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_parse_tbl_bb_pg); 1496fa6dfe6bSYan-Hsuan Chuang 1497fa6dfe6bSYan-Hsuan Chuang static const u8 rtw_channel_idx_5g[RTW_MAX_CHANNEL_NUM_5G] = { 1498fa6dfe6bSYan-Hsuan Chuang 36, 38, 40, 42, 44, 46, 48, /* Band 1 */ 1499fa6dfe6bSYan-Hsuan Chuang 52, 54, 56, 58, 60, 62, 64, /* Band 2 */ 1500fa6dfe6bSYan-Hsuan Chuang 100, 102, 104, 106, 108, 110, 112, /* Band 3 */ 1501fa6dfe6bSYan-Hsuan Chuang 116, 118, 120, 122, 124, 126, 128, /* Band 3 */ 1502fa6dfe6bSYan-Hsuan Chuang 132, 134, 136, 138, 140, 142, 144, /* Band 3 */ 1503fa6dfe6bSYan-Hsuan Chuang 149, 151, 153, 155, 157, 159, 161, /* Band 4 */ 1504fa6dfe6bSYan-Hsuan Chuang 165, 167, 169, 171, 173, 175, 177}; /* Band 4 */ 1505fa6dfe6bSYan-Hsuan Chuang 1506fa6dfe6bSYan-Hsuan Chuang static int rtw_channel_to_idx(u8 band, u8 channel) 1507fa6dfe6bSYan-Hsuan Chuang { 1508fa6dfe6bSYan-Hsuan Chuang int ch_idx; 1509fa6dfe6bSYan-Hsuan Chuang u8 n_channel; 1510fa6dfe6bSYan-Hsuan Chuang 1511fa6dfe6bSYan-Hsuan Chuang if (band == PHY_BAND_2G) { 1512fa6dfe6bSYan-Hsuan Chuang ch_idx = channel - 1; 1513fa6dfe6bSYan-Hsuan Chuang n_channel = RTW_MAX_CHANNEL_NUM_2G; 1514fa6dfe6bSYan-Hsuan Chuang } else if (band == PHY_BAND_5G) { 1515fa6dfe6bSYan-Hsuan Chuang n_channel = RTW_MAX_CHANNEL_NUM_5G; 1516fa6dfe6bSYan-Hsuan Chuang for (ch_idx = 0; ch_idx < n_channel; ch_idx++) 1517fa6dfe6bSYan-Hsuan Chuang if (rtw_channel_idx_5g[ch_idx] == channel) 1518fa6dfe6bSYan-Hsuan Chuang break; 1519fa6dfe6bSYan-Hsuan Chuang } else { 1520fa6dfe6bSYan-Hsuan Chuang return -1; 1521fa6dfe6bSYan-Hsuan Chuang } 1522fa6dfe6bSYan-Hsuan Chuang 1523fa6dfe6bSYan-Hsuan Chuang if (ch_idx >= n_channel) 1524fa6dfe6bSYan-Hsuan Chuang return -1; 1525fa6dfe6bSYan-Hsuan Chuang 1526fa6dfe6bSYan-Hsuan Chuang return ch_idx; 1527fa6dfe6bSYan-Hsuan Chuang } 1528fa6dfe6bSYan-Hsuan Chuang 152943712199SYan-Hsuan Chuang static void rtw_phy_set_tx_power_limit(struct rtw_dev *rtwdev, u8 regd, u8 band, 1530fa6dfe6bSYan-Hsuan Chuang u8 bw, u8 rs, u8 ch, s8 pwr_limit) 1531fa6dfe6bSYan-Hsuan Chuang { 1532fa6dfe6bSYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 15330d350f0aSTzu-En Huang u8 max_power_index = rtwdev->chip->max_power_index; 1534adf3c676SYan-Hsuan Chuang s8 ww; 1535fa6dfe6bSYan-Hsuan Chuang int ch_idx; 1536fa6dfe6bSYan-Hsuan Chuang 1537fa6dfe6bSYan-Hsuan Chuang pwr_limit = clamp_t(s8, pwr_limit, 15380d350f0aSTzu-En Huang -max_power_index, max_power_index); 1539fa6dfe6bSYan-Hsuan Chuang ch_idx = rtw_channel_to_idx(band, ch); 1540fa6dfe6bSYan-Hsuan Chuang 1541fa6dfe6bSYan-Hsuan Chuang if (regd >= RTW_REGD_MAX || bw >= RTW_CHANNEL_WIDTH_MAX || 1542fa6dfe6bSYan-Hsuan Chuang rs >= RTW_RATE_SECTION_MAX || ch_idx < 0) { 1543fa6dfe6bSYan-Hsuan Chuang WARN(1, 1544fa6dfe6bSYan-Hsuan Chuang "wrong txpwr_lmt regd=%u, band=%u bw=%u, rs=%u, ch_idx=%u, pwr_limit=%d\n", 1545fa6dfe6bSYan-Hsuan Chuang regd, band, bw, rs, ch_idx, pwr_limit); 1546fa6dfe6bSYan-Hsuan Chuang return; 1547fa6dfe6bSYan-Hsuan Chuang } 1548fa6dfe6bSYan-Hsuan Chuang 1549adf3c676SYan-Hsuan Chuang if (band == PHY_BAND_2G) { 1550fa6dfe6bSYan-Hsuan Chuang hal->tx_pwr_limit_2g[regd][bw][rs][ch_idx] = pwr_limit; 1551adf3c676SYan-Hsuan Chuang ww = hal->tx_pwr_limit_2g[RTW_REGD_WW][bw][rs][ch_idx]; 1552adf3c676SYan-Hsuan Chuang ww = min_t(s8, ww, pwr_limit); 1553adf3c676SYan-Hsuan Chuang hal->tx_pwr_limit_2g[RTW_REGD_WW][bw][rs][ch_idx] = ww; 1554adf3c676SYan-Hsuan Chuang } else if (band == PHY_BAND_5G) { 1555fa6dfe6bSYan-Hsuan Chuang hal->tx_pwr_limit_5g[regd][bw][rs][ch_idx] = pwr_limit; 1556adf3c676SYan-Hsuan Chuang ww = hal->tx_pwr_limit_5g[RTW_REGD_WW][bw][rs][ch_idx]; 1557adf3c676SYan-Hsuan Chuang ww = min_t(s8, ww, pwr_limit); 1558adf3c676SYan-Hsuan Chuang hal->tx_pwr_limit_5g[RTW_REGD_WW][bw][rs][ch_idx] = ww; 1559adf3c676SYan-Hsuan Chuang } 1560fa6dfe6bSYan-Hsuan Chuang } 1561fa6dfe6bSYan-Hsuan Chuang 156293f68a86SZong-Zhe Yang /* cross-reference 5G power limits if values are not assigned */ 156393f68a86SZong-Zhe Yang static void 156493f68a86SZong-Zhe Yang rtw_xref_5g_txpwr_lmt(struct rtw_dev *rtwdev, u8 regd, 156593f68a86SZong-Zhe Yang u8 bw, u8 ch_idx, u8 rs_ht, u8 rs_vht) 156693f68a86SZong-Zhe Yang { 156793f68a86SZong-Zhe Yang struct rtw_hal *hal = &rtwdev->hal; 15680d350f0aSTzu-En Huang u8 max_power_index = rtwdev->chip->max_power_index; 156993f68a86SZong-Zhe Yang s8 lmt_ht = hal->tx_pwr_limit_5g[regd][bw][rs_ht][ch_idx]; 157093f68a86SZong-Zhe Yang s8 lmt_vht = hal->tx_pwr_limit_5g[regd][bw][rs_vht][ch_idx]; 157193f68a86SZong-Zhe Yang 157293f68a86SZong-Zhe Yang if (lmt_ht == lmt_vht) 157393f68a86SZong-Zhe Yang return; 157493f68a86SZong-Zhe Yang 15750d350f0aSTzu-En Huang if (lmt_ht == max_power_index) 157693f68a86SZong-Zhe Yang hal->tx_pwr_limit_5g[regd][bw][rs_ht][ch_idx] = lmt_vht; 157793f68a86SZong-Zhe Yang 15780d350f0aSTzu-En Huang else if (lmt_vht == max_power_index) 157993f68a86SZong-Zhe Yang hal->tx_pwr_limit_5g[regd][bw][rs_vht][ch_idx] = lmt_ht; 158093f68a86SZong-Zhe Yang } 158193f68a86SZong-Zhe Yang 158293f68a86SZong-Zhe Yang /* cross-reference power limits for ht and vht */ 158393f68a86SZong-Zhe Yang static void 158493f68a86SZong-Zhe Yang rtw_xref_txpwr_lmt_by_rs(struct rtw_dev *rtwdev, u8 regd, u8 bw, u8 ch_idx) 158593f68a86SZong-Zhe Yang { 158693f68a86SZong-Zhe Yang u8 rs_idx, rs_ht, rs_vht; 158793f68a86SZong-Zhe Yang u8 rs_cmp[2][2] = {{RTW_RATE_SECTION_HT_1S, RTW_RATE_SECTION_VHT_1S}, 158893f68a86SZong-Zhe Yang {RTW_RATE_SECTION_HT_2S, RTW_RATE_SECTION_VHT_2S} }; 158993f68a86SZong-Zhe Yang 159093f68a86SZong-Zhe Yang for (rs_idx = 0; rs_idx < 2; rs_idx++) { 159193f68a86SZong-Zhe Yang rs_ht = rs_cmp[rs_idx][0]; 159293f68a86SZong-Zhe Yang rs_vht = rs_cmp[rs_idx][1]; 159393f68a86SZong-Zhe Yang 159493f68a86SZong-Zhe Yang rtw_xref_5g_txpwr_lmt(rtwdev, regd, bw, ch_idx, rs_ht, rs_vht); 159593f68a86SZong-Zhe Yang } 159693f68a86SZong-Zhe Yang } 159793f68a86SZong-Zhe Yang 159893f68a86SZong-Zhe Yang /* cross-reference power limits for 5G channels */ 159993f68a86SZong-Zhe Yang static void 160093f68a86SZong-Zhe Yang rtw_xref_5g_txpwr_lmt_by_ch(struct rtw_dev *rtwdev, u8 regd, u8 bw) 160193f68a86SZong-Zhe Yang { 160293f68a86SZong-Zhe Yang u8 ch_idx; 160393f68a86SZong-Zhe Yang 160493f68a86SZong-Zhe Yang for (ch_idx = 0; ch_idx < RTW_MAX_CHANNEL_NUM_5G; ch_idx++) 160593f68a86SZong-Zhe Yang rtw_xref_txpwr_lmt_by_rs(rtwdev, regd, bw, ch_idx); 160693f68a86SZong-Zhe Yang } 160793f68a86SZong-Zhe Yang 160893f68a86SZong-Zhe Yang /* cross-reference power limits for 20/40M bandwidth */ 160993f68a86SZong-Zhe Yang static void 161093f68a86SZong-Zhe Yang rtw_xref_txpwr_lmt_by_bw(struct rtw_dev *rtwdev, u8 regd) 161193f68a86SZong-Zhe Yang { 161293f68a86SZong-Zhe Yang u8 bw; 161393f68a86SZong-Zhe Yang 161493f68a86SZong-Zhe Yang for (bw = RTW_CHANNEL_WIDTH_20; bw <= RTW_CHANNEL_WIDTH_40; bw++) 161593f68a86SZong-Zhe Yang rtw_xref_5g_txpwr_lmt_by_ch(rtwdev, regd, bw); 161693f68a86SZong-Zhe Yang } 161793f68a86SZong-Zhe Yang 161893f68a86SZong-Zhe Yang /* cross-reference power limits */ 161993f68a86SZong-Zhe Yang static void rtw_xref_txpwr_lmt(struct rtw_dev *rtwdev) 162093f68a86SZong-Zhe Yang { 162193f68a86SZong-Zhe Yang u8 regd; 162293f68a86SZong-Zhe Yang 162393f68a86SZong-Zhe Yang for (regd = 0; regd < RTW_REGD_MAX; regd++) 162493f68a86SZong-Zhe Yang rtw_xref_txpwr_lmt_by_bw(rtwdev, regd); 162593f68a86SZong-Zhe Yang } 162693f68a86SZong-Zhe Yang 1627f8509c38SZong-Zhe Yang static void 1628f8509c38SZong-Zhe Yang __cfg_txpwr_lmt_by_alt(struct rtw_hal *hal, u8 regd, u8 regd_alt, u8 bw, u8 rs) 1629f8509c38SZong-Zhe Yang { 1630f8509c38SZong-Zhe Yang u8 ch; 1631f8509c38SZong-Zhe Yang 1632f8509c38SZong-Zhe Yang for (ch = 0; ch < RTW_MAX_CHANNEL_NUM_2G; ch++) 1633f8509c38SZong-Zhe Yang hal->tx_pwr_limit_2g[regd][bw][rs][ch] = 1634f8509c38SZong-Zhe Yang hal->tx_pwr_limit_2g[regd_alt][bw][rs][ch]; 1635f8509c38SZong-Zhe Yang 1636f8509c38SZong-Zhe Yang for (ch = 0; ch < RTW_MAX_CHANNEL_NUM_5G; ch++) 1637f8509c38SZong-Zhe Yang hal->tx_pwr_limit_5g[regd][bw][rs][ch] = 1638f8509c38SZong-Zhe Yang hal->tx_pwr_limit_5g[regd_alt][bw][rs][ch]; 1639f8509c38SZong-Zhe Yang } 1640f8509c38SZong-Zhe Yang 1641f8509c38SZong-Zhe Yang static void 1642f8509c38SZong-Zhe Yang rtw_cfg_txpwr_lmt_by_alt(struct rtw_dev *rtwdev, u8 regd, u8 regd_alt) 1643f8509c38SZong-Zhe Yang { 1644f8509c38SZong-Zhe Yang u8 bw, rs; 1645f8509c38SZong-Zhe Yang 1646f8509c38SZong-Zhe Yang for (bw = 0; bw < RTW_CHANNEL_WIDTH_MAX; bw++) 1647f8509c38SZong-Zhe Yang for (rs = 0; rs < RTW_RATE_SECTION_MAX; rs++) 1648f8509c38SZong-Zhe Yang __cfg_txpwr_lmt_by_alt(&rtwdev->hal, regd, regd_alt, 1649f8509c38SZong-Zhe Yang bw, rs); 1650f8509c38SZong-Zhe Yang } 1651f8509c38SZong-Zhe Yang 1652fa6dfe6bSYan-Hsuan Chuang void rtw_parse_tbl_txpwr_lmt(struct rtw_dev *rtwdev, 1653fa6dfe6bSYan-Hsuan Chuang const struct rtw_table *tbl) 1654fa6dfe6bSYan-Hsuan Chuang { 16553457f86dSBrian Norris const struct rtw_txpwr_lmt_cfg_pair *p = tbl->data; 16563457f86dSBrian Norris const struct rtw_txpwr_lmt_cfg_pair *end = p + tbl->size; 1657f8509c38SZong-Zhe Yang u32 regd_cfg_flag = 0; 1658f8509c38SZong-Zhe Yang u8 regd_alt; 1659f8509c38SZong-Zhe Yang u8 i; 1660fa6dfe6bSYan-Hsuan Chuang 1661fa6dfe6bSYan-Hsuan Chuang for (; p < end; p++) { 1662f8509c38SZong-Zhe Yang regd_cfg_flag |= BIT(p->regd); 166343712199SYan-Hsuan Chuang rtw_phy_set_tx_power_limit(rtwdev, p->regd, p->band, 166443712199SYan-Hsuan Chuang p->bw, p->rs, p->ch, p->txpwr_lmt); 1665fa6dfe6bSYan-Hsuan Chuang } 166693f68a86SZong-Zhe Yang 1667f8509c38SZong-Zhe Yang for (i = 0; i < RTW_REGD_MAX; i++) { 1668f8509c38SZong-Zhe Yang if (i == RTW_REGD_WW) 1669f8509c38SZong-Zhe Yang continue; 1670f8509c38SZong-Zhe Yang 1671f8509c38SZong-Zhe Yang if (regd_cfg_flag & BIT(i)) 1672f8509c38SZong-Zhe Yang continue; 1673f8509c38SZong-Zhe Yang 1674f8509c38SZong-Zhe Yang rtw_dbg(rtwdev, RTW_DBG_REGD, 1675f8509c38SZong-Zhe Yang "txpwr regd %d does not be configured\n", i); 1676f8509c38SZong-Zhe Yang 1677f8509c38SZong-Zhe Yang if (rtw_regd_has_alt(i, ®d_alt) && 1678f8509c38SZong-Zhe Yang regd_cfg_flag & BIT(regd_alt)) { 1679f8509c38SZong-Zhe Yang rtw_dbg(rtwdev, RTW_DBG_REGD, 1680f8509c38SZong-Zhe Yang "cfg txpwr regd %d by regd %d as alternative\n", 1681f8509c38SZong-Zhe Yang i, regd_alt); 1682f8509c38SZong-Zhe Yang 1683f8509c38SZong-Zhe Yang rtw_cfg_txpwr_lmt_by_alt(rtwdev, i, regd_alt); 1684f8509c38SZong-Zhe Yang continue; 1685f8509c38SZong-Zhe Yang } 1686f8509c38SZong-Zhe Yang 1687f8509c38SZong-Zhe Yang rtw_dbg(rtwdev, RTW_DBG_REGD, "cfg txpwr regd %d by WW\n", i); 1688f8509c38SZong-Zhe Yang rtw_cfg_txpwr_lmt_by_alt(rtwdev, i, RTW_REGD_WW); 1689f8509c38SZong-Zhe Yang } 1690f8509c38SZong-Zhe Yang 169193f68a86SZong-Zhe Yang rtw_xref_txpwr_lmt(rtwdev); 1692fa6dfe6bSYan-Hsuan Chuang } 1693449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_parse_tbl_txpwr_lmt); 1694fa6dfe6bSYan-Hsuan Chuang 1695fa6dfe6bSYan-Hsuan Chuang void rtw_phy_cfg_mac(struct rtw_dev *rtwdev, const struct rtw_table *tbl, 1696fa6dfe6bSYan-Hsuan Chuang u32 addr, u32 data) 1697fa6dfe6bSYan-Hsuan Chuang { 1698fa6dfe6bSYan-Hsuan Chuang rtw_write8(rtwdev, addr, data); 1699fa6dfe6bSYan-Hsuan Chuang } 1700449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_cfg_mac); 1701fa6dfe6bSYan-Hsuan Chuang 1702fa6dfe6bSYan-Hsuan Chuang void rtw_phy_cfg_agc(struct rtw_dev *rtwdev, const struct rtw_table *tbl, 1703fa6dfe6bSYan-Hsuan Chuang u32 addr, u32 data) 1704fa6dfe6bSYan-Hsuan Chuang { 1705fa6dfe6bSYan-Hsuan Chuang rtw_write32(rtwdev, addr, data); 1706fa6dfe6bSYan-Hsuan Chuang } 1707449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_cfg_agc); 1708fa6dfe6bSYan-Hsuan Chuang 1709fa6dfe6bSYan-Hsuan Chuang void rtw_phy_cfg_bb(struct rtw_dev *rtwdev, const struct rtw_table *tbl, 1710fa6dfe6bSYan-Hsuan Chuang u32 addr, u32 data) 1711fa6dfe6bSYan-Hsuan Chuang { 1712fa6dfe6bSYan-Hsuan Chuang if (addr == 0xfe) 1713fa6dfe6bSYan-Hsuan Chuang msleep(50); 1714fa6dfe6bSYan-Hsuan Chuang else if (addr == 0xfd) 1715fa6dfe6bSYan-Hsuan Chuang mdelay(5); 1716fa6dfe6bSYan-Hsuan Chuang else if (addr == 0xfc) 1717fa6dfe6bSYan-Hsuan Chuang mdelay(1); 1718fa6dfe6bSYan-Hsuan Chuang else if (addr == 0xfb) 1719fa6dfe6bSYan-Hsuan Chuang usleep_range(50, 60); 1720fa6dfe6bSYan-Hsuan Chuang else if (addr == 0xfa) 1721fa6dfe6bSYan-Hsuan Chuang udelay(5); 1722fa6dfe6bSYan-Hsuan Chuang else if (addr == 0xf9) 1723fa6dfe6bSYan-Hsuan Chuang udelay(1); 1724fa6dfe6bSYan-Hsuan Chuang else 1725fa6dfe6bSYan-Hsuan Chuang rtw_write32(rtwdev, addr, data); 1726fa6dfe6bSYan-Hsuan Chuang } 1727449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_cfg_bb); 1728fa6dfe6bSYan-Hsuan Chuang 1729fa6dfe6bSYan-Hsuan Chuang void rtw_phy_cfg_rf(struct rtw_dev *rtwdev, const struct rtw_table *tbl, 1730fa6dfe6bSYan-Hsuan Chuang u32 addr, u32 data) 1731fa6dfe6bSYan-Hsuan Chuang { 1732fa6dfe6bSYan-Hsuan Chuang if (addr == 0xffe) { 1733fa6dfe6bSYan-Hsuan Chuang msleep(50); 1734fa6dfe6bSYan-Hsuan Chuang } else if (addr == 0xfe) { 1735fa6dfe6bSYan-Hsuan Chuang usleep_range(100, 110); 1736fa6dfe6bSYan-Hsuan Chuang } else { 1737fa6dfe6bSYan-Hsuan Chuang rtw_write_rf(rtwdev, tbl->rf_path, addr, RFREG_MASK, data); 1738fa6dfe6bSYan-Hsuan Chuang udelay(1); 1739fa6dfe6bSYan-Hsuan Chuang } 1740fa6dfe6bSYan-Hsuan Chuang } 1741449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_cfg_rf); 1742fa6dfe6bSYan-Hsuan Chuang 1743fa6dfe6bSYan-Hsuan Chuang static void rtw_load_rfk_table(struct rtw_dev *rtwdev) 1744fa6dfe6bSYan-Hsuan Chuang { 1745fa6dfe6bSYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 17465227c2eeSTzu-En Huang struct rtw_dpk_info *dpk_info = &rtwdev->dm_info.dpk_info; 1747fa6dfe6bSYan-Hsuan Chuang 1748fa6dfe6bSYan-Hsuan Chuang if (!chip->rfk_init_tbl) 1749fa6dfe6bSYan-Hsuan Chuang return; 1750fa6dfe6bSYan-Hsuan Chuang 17515227c2eeSTzu-En Huang rtw_write32_mask(rtwdev, 0x1e24, BIT(17), 0x1); 17525227c2eeSTzu-En Huang rtw_write32_mask(rtwdev, 0x1cd0, BIT(28), 0x1); 17535227c2eeSTzu-En Huang rtw_write32_mask(rtwdev, 0x1cd0, BIT(29), 0x1); 17545227c2eeSTzu-En Huang rtw_write32_mask(rtwdev, 0x1cd0, BIT(30), 0x1); 17555227c2eeSTzu-En Huang rtw_write32_mask(rtwdev, 0x1cd0, BIT(31), 0x0); 17565227c2eeSTzu-En Huang 1757fa6dfe6bSYan-Hsuan Chuang rtw_load_table(rtwdev, chip->rfk_init_tbl); 17585227c2eeSTzu-En Huang 1759891984bcSzhengbin dpk_info->is_dpk_pwr_on = true; 1760fa6dfe6bSYan-Hsuan Chuang } 1761fa6dfe6bSYan-Hsuan Chuang 1762fa6dfe6bSYan-Hsuan Chuang void rtw_phy_load_tables(struct rtw_dev *rtwdev) 1763fa6dfe6bSYan-Hsuan Chuang { 1764fa6dfe6bSYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 1765fa6dfe6bSYan-Hsuan Chuang u8 rf_path; 1766fa6dfe6bSYan-Hsuan Chuang 1767fa6dfe6bSYan-Hsuan Chuang rtw_load_table(rtwdev, chip->mac_tbl); 1768fa6dfe6bSYan-Hsuan Chuang rtw_load_table(rtwdev, chip->bb_tbl); 1769fa6dfe6bSYan-Hsuan Chuang rtw_load_table(rtwdev, chip->agc_tbl); 1770fa6dfe6bSYan-Hsuan Chuang rtw_load_rfk_table(rtwdev); 1771fa6dfe6bSYan-Hsuan Chuang 1772fa6dfe6bSYan-Hsuan Chuang for (rf_path = 0; rf_path < rtwdev->hal.rf_path_num; rf_path++) { 1773fa6dfe6bSYan-Hsuan Chuang const struct rtw_table *tbl; 1774fa6dfe6bSYan-Hsuan Chuang 1775fa6dfe6bSYan-Hsuan Chuang tbl = chip->rf_tbl[rf_path]; 1776fa6dfe6bSYan-Hsuan Chuang rtw_load_table(rtwdev, tbl); 1777fa6dfe6bSYan-Hsuan Chuang } 1778fa6dfe6bSYan-Hsuan Chuang } 1779449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_load_tables); 1780fa6dfe6bSYan-Hsuan Chuang 17812ff25985SPing-Ke Shih static u8 rtw_get_channel_group(u8 channel, u8 rate) 1782fa6dfe6bSYan-Hsuan Chuang { 1783fa6dfe6bSYan-Hsuan Chuang switch (channel) { 1784fa6dfe6bSYan-Hsuan Chuang default: 1785fa6dfe6bSYan-Hsuan Chuang WARN_ON(1); 17865466aff8SGustavo A. R. Silva fallthrough; 1787fa6dfe6bSYan-Hsuan Chuang case 1: 1788fa6dfe6bSYan-Hsuan Chuang case 2: 1789fa6dfe6bSYan-Hsuan Chuang case 36: 1790fa6dfe6bSYan-Hsuan Chuang case 38: 1791fa6dfe6bSYan-Hsuan Chuang case 40: 1792fa6dfe6bSYan-Hsuan Chuang case 42: 1793fa6dfe6bSYan-Hsuan Chuang return 0; 1794fa6dfe6bSYan-Hsuan Chuang case 3: 1795fa6dfe6bSYan-Hsuan Chuang case 4: 1796fa6dfe6bSYan-Hsuan Chuang case 5: 1797fa6dfe6bSYan-Hsuan Chuang case 44: 1798fa6dfe6bSYan-Hsuan Chuang case 46: 1799fa6dfe6bSYan-Hsuan Chuang case 48: 1800fa6dfe6bSYan-Hsuan Chuang case 50: 1801fa6dfe6bSYan-Hsuan Chuang return 1; 1802fa6dfe6bSYan-Hsuan Chuang case 6: 1803fa6dfe6bSYan-Hsuan Chuang case 7: 1804fa6dfe6bSYan-Hsuan Chuang case 8: 1805fa6dfe6bSYan-Hsuan Chuang case 52: 1806fa6dfe6bSYan-Hsuan Chuang case 54: 1807fa6dfe6bSYan-Hsuan Chuang case 56: 1808fa6dfe6bSYan-Hsuan Chuang case 58: 1809fa6dfe6bSYan-Hsuan Chuang return 2; 1810fa6dfe6bSYan-Hsuan Chuang case 9: 1811fa6dfe6bSYan-Hsuan Chuang case 10: 1812fa6dfe6bSYan-Hsuan Chuang case 11: 1813fa6dfe6bSYan-Hsuan Chuang case 60: 1814fa6dfe6bSYan-Hsuan Chuang case 62: 1815fa6dfe6bSYan-Hsuan Chuang case 64: 1816fa6dfe6bSYan-Hsuan Chuang return 3; 1817fa6dfe6bSYan-Hsuan Chuang case 12: 1818fa6dfe6bSYan-Hsuan Chuang case 13: 1819fa6dfe6bSYan-Hsuan Chuang case 100: 1820fa6dfe6bSYan-Hsuan Chuang case 102: 1821fa6dfe6bSYan-Hsuan Chuang case 104: 1822fa6dfe6bSYan-Hsuan Chuang case 106: 1823fa6dfe6bSYan-Hsuan Chuang return 4; 1824fa6dfe6bSYan-Hsuan Chuang case 14: 18252ff25985SPing-Ke Shih return rate <= DESC_RATE11M ? 5 : 4; 1826fa6dfe6bSYan-Hsuan Chuang case 108: 1827fa6dfe6bSYan-Hsuan Chuang case 110: 1828fa6dfe6bSYan-Hsuan Chuang case 112: 1829fa6dfe6bSYan-Hsuan Chuang case 114: 1830fa6dfe6bSYan-Hsuan Chuang return 5; 1831fa6dfe6bSYan-Hsuan Chuang case 116: 1832fa6dfe6bSYan-Hsuan Chuang case 118: 1833fa6dfe6bSYan-Hsuan Chuang case 120: 1834fa6dfe6bSYan-Hsuan Chuang case 122: 1835fa6dfe6bSYan-Hsuan Chuang return 6; 1836fa6dfe6bSYan-Hsuan Chuang case 124: 1837fa6dfe6bSYan-Hsuan Chuang case 126: 1838fa6dfe6bSYan-Hsuan Chuang case 128: 1839fa6dfe6bSYan-Hsuan Chuang case 130: 1840fa6dfe6bSYan-Hsuan Chuang return 7; 1841fa6dfe6bSYan-Hsuan Chuang case 132: 1842fa6dfe6bSYan-Hsuan Chuang case 134: 1843fa6dfe6bSYan-Hsuan Chuang case 136: 1844fa6dfe6bSYan-Hsuan Chuang case 138: 1845fa6dfe6bSYan-Hsuan Chuang return 8; 1846fa6dfe6bSYan-Hsuan Chuang case 140: 1847fa6dfe6bSYan-Hsuan Chuang case 142: 1848fa6dfe6bSYan-Hsuan Chuang case 144: 1849fa6dfe6bSYan-Hsuan Chuang return 9; 1850fa6dfe6bSYan-Hsuan Chuang case 149: 1851fa6dfe6bSYan-Hsuan Chuang case 151: 1852fa6dfe6bSYan-Hsuan Chuang case 153: 1853fa6dfe6bSYan-Hsuan Chuang case 155: 1854fa6dfe6bSYan-Hsuan Chuang return 10; 1855fa6dfe6bSYan-Hsuan Chuang case 157: 1856fa6dfe6bSYan-Hsuan Chuang case 159: 1857fa6dfe6bSYan-Hsuan Chuang case 161: 1858fa6dfe6bSYan-Hsuan Chuang return 11; 1859fa6dfe6bSYan-Hsuan Chuang case 165: 1860fa6dfe6bSYan-Hsuan Chuang case 167: 1861fa6dfe6bSYan-Hsuan Chuang case 169: 1862fa6dfe6bSYan-Hsuan Chuang case 171: 1863fa6dfe6bSYan-Hsuan Chuang return 12; 1864fa6dfe6bSYan-Hsuan Chuang case 173: 1865fa6dfe6bSYan-Hsuan Chuang case 175: 1866fa6dfe6bSYan-Hsuan Chuang case 177: 1867fa6dfe6bSYan-Hsuan Chuang return 13; 1868fa6dfe6bSYan-Hsuan Chuang } 1869fa6dfe6bSYan-Hsuan Chuang } 1870fa6dfe6bSYan-Hsuan Chuang 18715227c2eeSTzu-En Huang static s8 rtw_phy_get_dis_dpd_by_rate_diff(struct rtw_dev *rtwdev, u16 rate) 18725227c2eeSTzu-En Huang { 18735227c2eeSTzu-En Huang struct rtw_chip_info *chip = rtwdev->chip; 18745227c2eeSTzu-En Huang s8 dpd_diff = 0; 18755227c2eeSTzu-En Huang 18765227c2eeSTzu-En Huang if (!chip->en_dis_dpd) 18775227c2eeSTzu-En Huang return 0; 18785227c2eeSTzu-En Huang 18795227c2eeSTzu-En Huang #define RTW_DPD_RATE_CHECK(_rate) \ 18805227c2eeSTzu-En Huang case DESC_RATE ## _rate: \ 18815227c2eeSTzu-En Huang if (DIS_DPD_RATE ## _rate & chip->dpd_ratemask) \ 18825227c2eeSTzu-En Huang dpd_diff = -6 * chip->txgi_factor; \ 18835227c2eeSTzu-En Huang break 18845227c2eeSTzu-En Huang 18855227c2eeSTzu-En Huang switch (rate) { 18865227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(6M); 18875227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(9M); 18885227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(MCS0); 18895227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(MCS1); 18905227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(MCS8); 18915227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(MCS9); 18925227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(VHT1SS_MCS0); 18935227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(VHT1SS_MCS1); 18945227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(VHT2SS_MCS0); 18955227c2eeSTzu-En Huang RTW_DPD_RATE_CHECK(VHT2SS_MCS1); 18965227c2eeSTzu-En Huang } 18975227c2eeSTzu-En Huang #undef RTW_DPD_RATE_CHECK 18985227c2eeSTzu-En Huang 18995227c2eeSTzu-En Huang return dpd_diff; 19005227c2eeSTzu-En Huang } 19015227c2eeSTzu-En Huang 190243712199SYan-Hsuan Chuang static u8 rtw_phy_get_2g_tx_power_index(struct rtw_dev *rtwdev, 1903fa6dfe6bSYan-Hsuan Chuang struct rtw_2g_txpwr_idx *pwr_idx_2g, 1904fa6dfe6bSYan-Hsuan Chuang enum rtw_bandwidth bandwidth, 1905fa6dfe6bSYan-Hsuan Chuang u8 rate, u8 group) 1906fa6dfe6bSYan-Hsuan Chuang { 1907fa6dfe6bSYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 1908fa6dfe6bSYan-Hsuan Chuang u8 tx_power; 1909fa6dfe6bSYan-Hsuan Chuang bool mcs_rate; 1910fa6dfe6bSYan-Hsuan Chuang bool above_2ss; 1911fa6dfe6bSYan-Hsuan Chuang u8 factor = chip->txgi_factor; 1912fa6dfe6bSYan-Hsuan Chuang 1913fa6dfe6bSYan-Hsuan Chuang if (rate <= DESC_RATE11M) 1914fa6dfe6bSYan-Hsuan Chuang tx_power = pwr_idx_2g->cck_base[group]; 1915fa6dfe6bSYan-Hsuan Chuang else 1916fa6dfe6bSYan-Hsuan Chuang tx_power = pwr_idx_2g->bw40_base[group]; 1917fa6dfe6bSYan-Hsuan Chuang 1918fa6dfe6bSYan-Hsuan Chuang if (rate >= DESC_RATE6M && rate <= DESC_RATE54M) 1919fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_2g->ht_1s_diff.ofdm * factor; 1920fa6dfe6bSYan-Hsuan Chuang 1921fa6dfe6bSYan-Hsuan Chuang mcs_rate = (rate >= DESC_RATEMCS0 && rate <= DESC_RATEMCS15) || 1922fa6dfe6bSYan-Hsuan Chuang (rate >= DESC_RATEVHT1SS_MCS0 && 1923fa6dfe6bSYan-Hsuan Chuang rate <= DESC_RATEVHT2SS_MCS9); 1924fa6dfe6bSYan-Hsuan Chuang above_2ss = (rate >= DESC_RATEMCS8 && rate <= DESC_RATEMCS15) || 1925fa6dfe6bSYan-Hsuan Chuang (rate >= DESC_RATEVHT2SS_MCS0); 1926fa6dfe6bSYan-Hsuan Chuang 1927fa6dfe6bSYan-Hsuan Chuang if (!mcs_rate) 1928fa6dfe6bSYan-Hsuan Chuang return tx_power; 1929fa6dfe6bSYan-Hsuan Chuang 1930fa6dfe6bSYan-Hsuan Chuang switch (bandwidth) { 1931fa6dfe6bSYan-Hsuan Chuang default: 1932fa6dfe6bSYan-Hsuan Chuang WARN_ON(1); 19335466aff8SGustavo A. R. Silva fallthrough; 1934fa6dfe6bSYan-Hsuan Chuang case RTW_CHANNEL_WIDTH_20: 1935fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_2g->ht_1s_diff.bw20 * factor; 1936fa6dfe6bSYan-Hsuan Chuang if (above_2ss) 1937fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_2g->ht_2s_diff.bw20 * factor; 1938fa6dfe6bSYan-Hsuan Chuang break; 1939fa6dfe6bSYan-Hsuan Chuang case RTW_CHANNEL_WIDTH_40: 1940fa6dfe6bSYan-Hsuan Chuang /* bw40 is the base power */ 1941fa6dfe6bSYan-Hsuan Chuang if (above_2ss) 1942fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_2g->ht_2s_diff.bw40 * factor; 1943fa6dfe6bSYan-Hsuan Chuang break; 1944fa6dfe6bSYan-Hsuan Chuang } 1945fa6dfe6bSYan-Hsuan Chuang 1946fa6dfe6bSYan-Hsuan Chuang return tx_power; 1947fa6dfe6bSYan-Hsuan Chuang } 1948fa6dfe6bSYan-Hsuan Chuang 194943712199SYan-Hsuan Chuang static u8 rtw_phy_get_5g_tx_power_index(struct rtw_dev *rtwdev, 1950fa6dfe6bSYan-Hsuan Chuang struct rtw_5g_txpwr_idx *pwr_idx_5g, 1951fa6dfe6bSYan-Hsuan Chuang enum rtw_bandwidth bandwidth, 1952fa6dfe6bSYan-Hsuan Chuang u8 rate, u8 group) 1953fa6dfe6bSYan-Hsuan Chuang { 1954fa6dfe6bSYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 1955fa6dfe6bSYan-Hsuan Chuang u8 tx_power; 1956fa6dfe6bSYan-Hsuan Chuang u8 upper, lower; 1957fa6dfe6bSYan-Hsuan Chuang bool mcs_rate; 1958fa6dfe6bSYan-Hsuan Chuang bool above_2ss; 1959fa6dfe6bSYan-Hsuan Chuang u8 factor = chip->txgi_factor; 1960fa6dfe6bSYan-Hsuan Chuang 1961fa6dfe6bSYan-Hsuan Chuang tx_power = pwr_idx_5g->bw40_base[group]; 1962fa6dfe6bSYan-Hsuan Chuang 1963fa6dfe6bSYan-Hsuan Chuang mcs_rate = (rate >= DESC_RATEMCS0 && rate <= DESC_RATEMCS15) || 1964fa6dfe6bSYan-Hsuan Chuang (rate >= DESC_RATEVHT1SS_MCS0 && 1965fa6dfe6bSYan-Hsuan Chuang rate <= DESC_RATEVHT2SS_MCS9); 1966fa6dfe6bSYan-Hsuan Chuang above_2ss = (rate >= DESC_RATEMCS8 && rate <= DESC_RATEMCS15) || 1967fa6dfe6bSYan-Hsuan Chuang (rate >= DESC_RATEVHT2SS_MCS0); 1968fa6dfe6bSYan-Hsuan Chuang 1969fa6dfe6bSYan-Hsuan Chuang if (!mcs_rate) { 1970fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_5g->ht_1s_diff.ofdm * factor; 1971fa6dfe6bSYan-Hsuan Chuang return tx_power; 1972fa6dfe6bSYan-Hsuan Chuang } 1973fa6dfe6bSYan-Hsuan Chuang 1974fa6dfe6bSYan-Hsuan Chuang switch (bandwidth) { 1975fa6dfe6bSYan-Hsuan Chuang default: 1976fa6dfe6bSYan-Hsuan Chuang WARN_ON(1); 19775466aff8SGustavo A. R. Silva fallthrough; 1978fa6dfe6bSYan-Hsuan Chuang case RTW_CHANNEL_WIDTH_20: 1979fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_5g->ht_1s_diff.bw20 * factor; 1980fa6dfe6bSYan-Hsuan Chuang if (above_2ss) 1981fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_5g->ht_2s_diff.bw20 * factor; 1982fa6dfe6bSYan-Hsuan Chuang break; 1983fa6dfe6bSYan-Hsuan Chuang case RTW_CHANNEL_WIDTH_40: 1984fa6dfe6bSYan-Hsuan Chuang /* bw40 is the base power */ 1985fa6dfe6bSYan-Hsuan Chuang if (above_2ss) 1986fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_5g->ht_2s_diff.bw40 * factor; 1987fa6dfe6bSYan-Hsuan Chuang break; 1988fa6dfe6bSYan-Hsuan Chuang case RTW_CHANNEL_WIDTH_80: 1989fa6dfe6bSYan-Hsuan Chuang /* the base idx of bw80 is the average of bw40+/bw40- */ 1990fa6dfe6bSYan-Hsuan Chuang lower = pwr_idx_5g->bw40_base[group]; 1991fa6dfe6bSYan-Hsuan Chuang upper = pwr_idx_5g->bw40_base[group + 1]; 1992fa6dfe6bSYan-Hsuan Chuang 1993fa6dfe6bSYan-Hsuan Chuang tx_power = (lower + upper) / 2; 1994fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_5g->vht_1s_diff.bw80 * factor; 1995fa6dfe6bSYan-Hsuan Chuang if (above_2ss) 1996fa6dfe6bSYan-Hsuan Chuang tx_power += pwr_idx_5g->vht_2s_diff.bw80 * factor; 1997fa6dfe6bSYan-Hsuan Chuang break; 1998fa6dfe6bSYan-Hsuan Chuang } 1999fa6dfe6bSYan-Hsuan Chuang 2000fa6dfe6bSYan-Hsuan Chuang return tx_power; 2001fa6dfe6bSYan-Hsuan Chuang } 2002fa6dfe6bSYan-Hsuan Chuang 20038704d0beSZong-Zhe Yang /* return RTW_RATE_SECTION_MAX to indicate rate is invalid */ 20048704d0beSZong-Zhe Yang static u8 rtw_phy_rate_to_rate_section(u8 rate) 20058704d0beSZong-Zhe Yang { 20068704d0beSZong-Zhe Yang if (rate >= DESC_RATE1M && rate <= DESC_RATE11M) 20078704d0beSZong-Zhe Yang return RTW_RATE_SECTION_CCK; 20088704d0beSZong-Zhe Yang else if (rate >= DESC_RATE6M && rate <= DESC_RATE54M) 20098704d0beSZong-Zhe Yang return RTW_RATE_SECTION_OFDM; 20108704d0beSZong-Zhe Yang else if (rate >= DESC_RATEMCS0 && rate <= DESC_RATEMCS7) 20118704d0beSZong-Zhe Yang return RTW_RATE_SECTION_HT_1S; 20128704d0beSZong-Zhe Yang else if (rate >= DESC_RATEMCS8 && rate <= DESC_RATEMCS15) 20138704d0beSZong-Zhe Yang return RTW_RATE_SECTION_HT_2S; 20148704d0beSZong-Zhe Yang else if (rate >= DESC_RATEVHT1SS_MCS0 && rate <= DESC_RATEVHT1SS_MCS9) 20158704d0beSZong-Zhe Yang return RTW_RATE_SECTION_VHT_1S; 20168704d0beSZong-Zhe Yang else if (rate >= DESC_RATEVHT2SS_MCS0 && rate <= DESC_RATEVHT2SS_MCS9) 20178704d0beSZong-Zhe Yang return RTW_RATE_SECTION_VHT_2S; 20188704d0beSZong-Zhe Yang else 20198704d0beSZong-Zhe Yang return RTW_RATE_SECTION_MAX; 20208704d0beSZong-Zhe Yang } 20218704d0beSZong-Zhe Yang 202243712199SYan-Hsuan Chuang static s8 rtw_phy_get_tx_power_limit(struct rtw_dev *rtwdev, u8 band, 2023fa6dfe6bSYan-Hsuan Chuang enum rtw_bandwidth bw, u8 rf_path, 2024fa6dfe6bSYan-Hsuan Chuang u8 rate, u8 channel, u8 regd) 2025fa6dfe6bSYan-Hsuan Chuang { 2026fa6dfe6bSYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 202793f68a86SZong-Zhe Yang u8 *cch_by_bw = hal->cch_by_bw; 20280d350f0aSTzu-En Huang s8 power_limit = (s8)rtwdev->chip->max_power_index; 20298704d0beSZong-Zhe Yang u8 rs = rtw_phy_rate_to_rate_section(rate); 2030fa6dfe6bSYan-Hsuan Chuang int ch_idx; 203193f68a86SZong-Zhe Yang u8 cur_bw, cur_ch; 203293f68a86SZong-Zhe Yang s8 cur_lmt; 2033fa6dfe6bSYan-Hsuan Chuang 203476403816SYan-Hsuan Chuang if (regd > RTW_REGD_WW) 20350d350f0aSTzu-En Huang return power_limit; 203676403816SYan-Hsuan Chuang 20378704d0beSZong-Zhe Yang if (rs == RTW_RATE_SECTION_MAX) 2038fa6dfe6bSYan-Hsuan Chuang goto err; 2039fa6dfe6bSYan-Hsuan Chuang 204093f68a86SZong-Zhe Yang /* only 20M BW with cck and ofdm */ 204193f68a86SZong-Zhe Yang if (rs == RTW_RATE_SECTION_CCK || rs == RTW_RATE_SECTION_OFDM) 204293f68a86SZong-Zhe Yang bw = RTW_CHANNEL_WIDTH_20; 204393f68a86SZong-Zhe Yang 204493f68a86SZong-Zhe Yang /* only 20/40M BW with ht */ 204593f68a86SZong-Zhe Yang if (rs == RTW_RATE_SECTION_HT_1S || rs == RTW_RATE_SECTION_HT_2S) 204693f68a86SZong-Zhe Yang bw = min_t(u8, bw, RTW_CHANNEL_WIDTH_40); 204793f68a86SZong-Zhe Yang 204893f68a86SZong-Zhe Yang /* select min power limit among [20M BW ~ current BW] */ 204993f68a86SZong-Zhe Yang for (cur_bw = RTW_CHANNEL_WIDTH_20; cur_bw <= bw; cur_bw++) { 205093f68a86SZong-Zhe Yang cur_ch = cch_by_bw[cur_bw]; 205193f68a86SZong-Zhe Yang 205293f68a86SZong-Zhe Yang ch_idx = rtw_channel_to_idx(band, cur_ch); 2053fa6dfe6bSYan-Hsuan Chuang if (ch_idx < 0) 2054fa6dfe6bSYan-Hsuan Chuang goto err; 2055fa6dfe6bSYan-Hsuan Chuang 205693f68a86SZong-Zhe Yang cur_lmt = cur_ch <= RTW_MAX_CHANNEL_NUM_2G ? 205793f68a86SZong-Zhe Yang hal->tx_pwr_limit_2g[regd][cur_bw][rs][ch_idx] : 205893f68a86SZong-Zhe Yang hal->tx_pwr_limit_5g[regd][cur_bw][rs][ch_idx]; 205993f68a86SZong-Zhe Yang 206093f68a86SZong-Zhe Yang power_limit = min_t(s8, cur_lmt, power_limit); 206193f68a86SZong-Zhe Yang } 2062fa6dfe6bSYan-Hsuan Chuang 2063fa6dfe6bSYan-Hsuan Chuang return power_limit; 2064fa6dfe6bSYan-Hsuan Chuang 2065fa6dfe6bSYan-Hsuan Chuang err: 2066fa6dfe6bSYan-Hsuan Chuang WARN(1, "invalid arguments, band=%d, bw=%d, path=%d, rate=%d, ch=%d\n", 2067fa6dfe6bSYan-Hsuan Chuang band, bw, rf_path, rate, channel); 20680d350f0aSTzu-En Huang return (s8)rtwdev->chip->max_power_index; 2069fa6dfe6bSYan-Hsuan Chuang } 2070fa6dfe6bSYan-Hsuan Chuang 20718704d0beSZong-Zhe Yang static s8 rtw_phy_get_tx_power_sar(struct rtw_dev *rtwdev, u8 sar_band, 20728704d0beSZong-Zhe Yang u8 rf_path, u8 rate) 20738704d0beSZong-Zhe Yang { 20748704d0beSZong-Zhe Yang u8 rs = rtw_phy_rate_to_rate_section(rate); 20758704d0beSZong-Zhe Yang struct rtw_sar_arg arg = { 20768704d0beSZong-Zhe Yang .sar_band = sar_band, 20778704d0beSZong-Zhe Yang .path = rf_path, 20788704d0beSZong-Zhe Yang .rs = rs, 20798704d0beSZong-Zhe Yang }; 20808704d0beSZong-Zhe Yang 20818704d0beSZong-Zhe Yang if (rs == RTW_RATE_SECTION_MAX) 20828704d0beSZong-Zhe Yang goto err; 20838704d0beSZong-Zhe Yang 20848704d0beSZong-Zhe Yang return rtw_query_sar(rtwdev, &arg); 20858704d0beSZong-Zhe Yang 20868704d0beSZong-Zhe Yang err: 20878704d0beSZong-Zhe Yang WARN(1, "invalid arguments, sar_band=%d, path=%d, rate=%d\n", 20888704d0beSZong-Zhe Yang sar_band, rf_path, rate); 20898704d0beSZong-Zhe Yang return (s8)rtwdev->chip->max_power_index; 20908704d0beSZong-Zhe Yang } 20918704d0beSZong-Zhe Yang 2092b7414222SZong-Zhe Yang void rtw_get_tx_power_params(struct rtw_dev *rtwdev, u8 path, u8 rate, u8 bw, 2093b7414222SZong-Zhe Yang u8 ch, u8 regd, struct rtw_power_params *pwr_param) 2094fa6dfe6bSYan-Hsuan Chuang { 2095fa6dfe6bSYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 2096608d2a08SPing-Ke Shih struct rtw_dm_info *dm_info = &rtwdev->dm_info; 2097fa6dfe6bSYan-Hsuan Chuang struct rtw_txpwr_idx *pwr_idx; 2098b7414222SZong-Zhe Yang u8 group, band; 2099b7414222SZong-Zhe Yang u8 *base = &pwr_param->pwr_base; 2100b7414222SZong-Zhe Yang s8 *offset = &pwr_param->pwr_offset; 2101b7414222SZong-Zhe Yang s8 *limit = &pwr_param->pwr_limit; 2102608d2a08SPing-Ke Shih s8 *remnant = &pwr_param->pwr_remnant; 21038704d0beSZong-Zhe Yang s8 *sar = &pwr_param->pwr_sar; 2104fa6dfe6bSYan-Hsuan Chuang 2105b7414222SZong-Zhe Yang pwr_idx = &rtwdev->efuse.txpwr_idx_table[path]; 21062ff25985SPing-Ke Shih group = rtw_get_channel_group(ch, rate); 2107fa6dfe6bSYan-Hsuan Chuang 2108fa6dfe6bSYan-Hsuan Chuang /* base power index for 2.4G/5G */ 21098575b534SYan-Hsuan Chuang if (IS_CH_2G_BAND(ch)) { 2110fa6dfe6bSYan-Hsuan Chuang band = PHY_BAND_2G; 2111b7414222SZong-Zhe Yang *base = rtw_phy_get_2g_tx_power_index(rtwdev, 2112fa6dfe6bSYan-Hsuan Chuang &pwr_idx->pwr_idx_2g, 2113b7414222SZong-Zhe Yang bw, rate, group); 2114b7414222SZong-Zhe Yang *offset = hal->tx_pwr_by_rate_offset_2g[path][rate]; 2115fa6dfe6bSYan-Hsuan Chuang } else { 2116fa6dfe6bSYan-Hsuan Chuang band = PHY_BAND_5G; 2117b7414222SZong-Zhe Yang *base = rtw_phy_get_5g_tx_power_index(rtwdev, 2118fa6dfe6bSYan-Hsuan Chuang &pwr_idx->pwr_idx_5g, 2119b7414222SZong-Zhe Yang bw, rate, group); 2120b7414222SZong-Zhe Yang *offset = hal->tx_pwr_by_rate_offset_5g[path][rate]; 2121fa6dfe6bSYan-Hsuan Chuang } 2122fa6dfe6bSYan-Hsuan Chuang 2123b7414222SZong-Zhe Yang *limit = rtw_phy_get_tx_power_limit(rtwdev, band, bw, path, 2124b7414222SZong-Zhe Yang rate, ch, regd); 2125608d2a08SPing-Ke Shih *remnant = (rate <= DESC_RATE11M ? dm_info->txagc_remnant_cck : 2126608d2a08SPing-Ke Shih dm_info->txagc_remnant_ofdm); 21278704d0beSZong-Zhe Yang *sar = rtw_phy_get_tx_power_sar(rtwdev, hal->sar_band, path, rate); 2128b7414222SZong-Zhe Yang } 2129fa6dfe6bSYan-Hsuan Chuang 2130b7414222SZong-Zhe Yang u8 2131b7414222SZong-Zhe Yang rtw_phy_get_tx_power_index(struct rtw_dev *rtwdev, u8 rf_path, u8 rate, 2132b7414222SZong-Zhe Yang enum rtw_bandwidth bandwidth, u8 channel, u8 regd) 2133b7414222SZong-Zhe Yang { 2134b7414222SZong-Zhe Yang struct rtw_power_params pwr_param = {0}; 2135b7414222SZong-Zhe Yang u8 tx_power; 2136b7414222SZong-Zhe Yang s8 offset; 2137b7414222SZong-Zhe Yang 2138b7414222SZong-Zhe Yang rtw_get_tx_power_params(rtwdev, rf_path, rate, bandwidth, 2139b7414222SZong-Zhe Yang channel, regd, &pwr_param); 2140b7414222SZong-Zhe Yang 2141b7414222SZong-Zhe Yang tx_power = pwr_param.pwr_base; 21428704d0beSZong-Zhe Yang offset = min3(pwr_param.pwr_offset, 21438704d0beSZong-Zhe Yang pwr_param.pwr_limit, 21448704d0beSZong-Zhe Yang pwr_param.pwr_sar); 2145fa6dfe6bSYan-Hsuan Chuang 21465227c2eeSTzu-En Huang if (rtwdev->chip->en_dis_dpd) 21475227c2eeSTzu-En Huang offset += rtw_phy_get_dis_dpd_by_rate_diff(rtwdev, rate); 21485227c2eeSTzu-En Huang 2149608d2a08SPing-Ke Shih tx_power += offset + pwr_param.pwr_remnant; 2150fa6dfe6bSYan-Hsuan Chuang 2151fa6dfe6bSYan-Hsuan Chuang if (tx_power > rtwdev->chip->max_power_index) 2152fa6dfe6bSYan-Hsuan Chuang tx_power = rtwdev->chip->max_power_index; 2153fa6dfe6bSYan-Hsuan Chuang 2154fa6dfe6bSYan-Hsuan Chuang return tx_power; 2155fa6dfe6bSYan-Hsuan Chuang } 2156449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_get_tx_power_index); 2157fa6dfe6bSYan-Hsuan Chuang 215843712199SYan-Hsuan Chuang static void rtw_phy_set_tx_power_index_by_rs(struct rtw_dev *rtwdev, 2159226746fdSYan-Hsuan Chuang u8 ch, u8 path, u8 rs) 2160fa6dfe6bSYan-Hsuan Chuang { 2161fa6dfe6bSYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 2162f8509c38SZong-Zhe Yang u8 regd = rtw_regd_get(rtwdev); 2163fa6dfe6bSYan-Hsuan Chuang u8 *rates; 2164fa6dfe6bSYan-Hsuan Chuang u8 size; 2165fa6dfe6bSYan-Hsuan Chuang u8 rate; 2166fa6dfe6bSYan-Hsuan Chuang u8 pwr_idx; 2167fa6dfe6bSYan-Hsuan Chuang u8 bw; 2168fa6dfe6bSYan-Hsuan Chuang int i; 2169fa6dfe6bSYan-Hsuan Chuang 2170fa6dfe6bSYan-Hsuan Chuang if (rs >= RTW_RATE_SECTION_MAX) 2171fa6dfe6bSYan-Hsuan Chuang return; 2172fa6dfe6bSYan-Hsuan Chuang 2173fa6dfe6bSYan-Hsuan Chuang rates = rtw_rate_section[rs]; 2174fa6dfe6bSYan-Hsuan Chuang size = rtw_rate_size[rs]; 2175fa6dfe6bSYan-Hsuan Chuang bw = hal->current_band_width; 2176fa6dfe6bSYan-Hsuan Chuang for (i = 0; i < size; i++) { 2177fa6dfe6bSYan-Hsuan Chuang rate = rates[i]; 217843712199SYan-Hsuan Chuang pwr_idx = rtw_phy_get_tx_power_index(rtwdev, path, rate, 217943712199SYan-Hsuan Chuang bw, ch, regd); 2180fa6dfe6bSYan-Hsuan Chuang hal->tx_pwr_tbl[path][rate] = pwr_idx; 2181fa6dfe6bSYan-Hsuan Chuang } 2182fa6dfe6bSYan-Hsuan Chuang } 2183fa6dfe6bSYan-Hsuan Chuang 2184fa6dfe6bSYan-Hsuan Chuang /* set tx power level by path for each rates, note that the order of the rates 2185fa6dfe6bSYan-Hsuan Chuang * are *very* important, bacause 8822B/8821C combines every four bytes of tx 2186fa6dfe6bSYan-Hsuan Chuang * power index into a four-byte power index register, and calls set_tx_agc to 2187fa6dfe6bSYan-Hsuan Chuang * write these values into hardware 2188fa6dfe6bSYan-Hsuan Chuang */ 218943712199SYan-Hsuan Chuang static void rtw_phy_set_tx_power_level_by_path(struct rtw_dev *rtwdev, 219043712199SYan-Hsuan Chuang u8 ch, u8 path) 2191fa6dfe6bSYan-Hsuan Chuang { 2192fa6dfe6bSYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 2193fa6dfe6bSYan-Hsuan Chuang u8 rs; 2194fa6dfe6bSYan-Hsuan Chuang 2195fa6dfe6bSYan-Hsuan Chuang /* do not need cck rates if we are not in 2.4G */ 2196fa6dfe6bSYan-Hsuan Chuang if (hal->current_band_type == RTW_BAND_2G) 2197fa6dfe6bSYan-Hsuan Chuang rs = RTW_RATE_SECTION_CCK; 2198fa6dfe6bSYan-Hsuan Chuang else 2199fa6dfe6bSYan-Hsuan Chuang rs = RTW_RATE_SECTION_OFDM; 2200fa6dfe6bSYan-Hsuan Chuang 2201fa6dfe6bSYan-Hsuan Chuang for (; rs < RTW_RATE_SECTION_MAX; rs++) 220243712199SYan-Hsuan Chuang rtw_phy_set_tx_power_index_by_rs(rtwdev, ch, path, rs); 2203fa6dfe6bSYan-Hsuan Chuang } 2204fa6dfe6bSYan-Hsuan Chuang 2205fa6dfe6bSYan-Hsuan Chuang void rtw_phy_set_tx_power_level(struct rtw_dev *rtwdev, u8 channel) 2206fa6dfe6bSYan-Hsuan Chuang { 2207fa6dfe6bSYan-Hsuan Chuang struct rtw_chip_info *chip = rtwdev->chip; 2208fa6dfe6bSYan-Hsuan Chuang struct rtw_hal *hal = &rtwdev->hal; 2209fa6dfe6bSYan-Hsuan Chuang u8 path; 2210fa6dfe6bSYan-Hsuan Chuang 2211fa6dfe6bSYan-Hsuan Chuang mutex_lock(&hal->tx_power_mutex); 2212fa6dfe6bSYan-Hsuan Chuang 2213fa6dfe6bSYan-Hsuan Chuang for (path = 0; path < hal->rf_path_num; path++) 221443712199SYan-Hsuan Chuang rtw_phy_set_tx_power_level_by_path(rtwdev, channel, path); 2215fa6dfe6bSYan-Hsuan Chuang 2216fa6dfe6bSYan-Hsuan Chuang chip->ops->set_tx_power_index(rtwdev); 2217fa6dfe6bSYan-Hsuan Chuang mutex_unlock(&hal->tx_power_mutex); 2218fa6dfe6bSYan-Hsuan Chuang } 2219449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_set_tx_power_level); 2220fa6dfe6bSYan-Hsuan Chuang 222143712199SYan-Hsuan Chuang static void 222243712199SYan-Hsuan Chuang rtw_phy_tx_power_by_rate_config_by_path(struct rtw_hal *hal, u8 path, 2223e3037485SYan-Hsuan Chuang u8 rs, u8 size, u8 *rates) 2224e3037485SYan-Hsuan Chuang { 2225e3037485SYan-Hsuan Chuang u8 rate; 2226e3037485SYan-Hsuan Chuang u8 base_idx, rate_idx; 2227e3037485SYan-Hsuan Chuang s8 base_2g, base_5g; 2228e3037485SYan-Hsuan Chuang 2229e3037485SYan-Hsuan Chuang if (rs >= RTW_RATE_SECTION_VHT_1S) 2230e3037485SYan-Hsuan Chuang base_idx = rates[size - 3]; 2231e3037485SYan-Hsuan Chuang else 2232e3037485SYan-Hsuan Chuang base_idx = rates[size - 1]; 2233e3037485SYan-Hsuan Chuang base_2g = hal->tx_pwr_by_rate_offset_2g[path][base_idx]; 2234e3037485SYan-Hsuan Chuang base_5g = hal->tx_pwr_by_rate_offset_5g[path][base_idx]; 2235e3037485SYan-Hsuan Chuang hal->tx_pwr_by_rate_base_2g[path][rs] = base_2g; 2236e3037485SYan-Hsuan Chuang hal->tx_pwr_by_rate_base_5g[path][rs] = base_5g; 2237e3037485SYan-Hsuan Chuang for (rate = 0; rate < size; rate++) { 2238e3037485SYan-Hsuan Chuang rate_idx = rates[rate]; 2239e3037485SYan-Hsuan Chuang hal->tx_pwr_by_rate_offset_2g[path][rate_idx] -= base_2g; 2240e3037485SYan-Hsuan Chuang hal->tx_pwr_by_rate_offset_5g[path][rate_idx] -= base_5g; 2241e3037485SYan-Hsuan Chuang } 2242e3037485SYan-Hsuan Chuang } 2243e3037485SYan-Hsuan Chuang 2244e3037485SYan-Hsuan Chuang void rtw_phy_tx_power_by_rate_config(struct rtw_hal *hal) 2245e3037485SYan-Hsuan Chuang { 2246e3037485SYan-Hsuan Chuang u8 path; 2247e3037485SYan-Hsuan Chuang 2248e3037485SYan-Hsuan Chuang for (path = 0; path < RTW_RF_PATH_MAX; path++) { 224943712199SYan-Hsuan Chuang rtw_phy_tx_power_by_rate_config_by_path(hal, path, 2250e3037485SYan-Hsuan Chuang RTW_RATE_SECTION_CCK, 2251e3037485SYan-Hsuan Chuang rtw_cck_size, rtw_cck_rates); 225243712199SYan-Hsuan Chuang rtw_phy_tx_power_by_rate_config_by_path(hal, path, 2253e3037485SYan-Hsuan Chuang RTW_RATE_SECTION_OFDM, 2254e3037485SYan-Hsuan Chuang rtw_ofdm_size, rtw_ofdm_rates); 225543712199SYan-Hsuan Chuang rtw_phy_tx_power_by_rate_config_by_path(hal, path, 2256e3037485SYan-Hsuan Chuang RTW_RATE_SECTION_HT_1S, 2257e3037485SYan-Hsuan Chuang rtw_ht_1s_size, rtw_ht_1s_rates); 225843712199SYan-Hsuan Chuang rtw_phy_tx_power_by_rate_config_by_path(hal, path, 2259e3037485SYan-Hsuan Chuang RTW_RATE_SECTION_HT_2S, 2260e3037485SYan-Hsuan Chuang rtw_ht_2s_size, rtw_ht_2s_rates); 226143712199SYan-Hsuan Chuang rtw_phy_tx_power_by_rate_config_by_path(hal, path, 2262e3037485SYan-Hsuan Chuang RTW_RATE_SECTION_VHT_1S, 2263e3037485SYan-Hsuan Chuang rtw_vht_1s_size, rtw_vht_1s_rates); 226443712199SYan-Hsuan Chuang rtw_phy_tx_power_by_rate_config_by_path(hal, path, 2265e3037485SYan-Hsuan Chuang RTW_RATE_SECTION_VHT_2S, 2266e3037485SYan-Hsuan Chuang rtw_vht_2s_size, rtw_vht_2s_rates); 2267e3037485SYan-Hsuan Chuang } 2268e3037485SYan-Hsuan Chuang } 2269e3037485SYan-Hsuan Chuang 2270e3037485SYan-Hsuan Chuang static void 227143712199SYan-Hsuan Chuang __rtw_phy_tx_power_limit_config(struct rtw_hal *hal, u8 regd, u8 bw, u8 rs) 2272e3037485SYan-Hsuan Chuang { 227352280149SYan-Hsuan Chuang s8 base; 2274e3037485SYan-Hsuan Chuang u8 ch; 2275e3037485SYan-Hsuan Chuang 2276e3037485SYan-Hsuan Chuang for (ch = 0; ch < RTW_MAX_CHANNEL_NUM_2G; ch++) { 2277e3037485SYan-Hsuan Chuang base = hal->tx_pwr_by_rate_base_2g[0][rs]; 2278e3037485SYan-Hsuan Chuang hal->tx_pwr_limit_2g[regd][bw][rs][ch] -= base; 2279e3037485SYan-Hsuan Chuang } 2280e3037485SYan-Hsuan Chuang 2281e3037485SYan-Hsuan Chuang for (ch = 0; ch < RTW_MAX_CHANNEL_NUM_5G; ch++) { 2282e3037485SYan-Hsuan Chuang base = hal->tx_pwr_by_rate_base_5g[0][rs]; 2283e3037485SYan-Hsuan Chuang hal->tx_pwr_limit_5g[regd][bw][rs][ch] -= base; 2284e3037485SYan-Hsuan Chuang } 2285e3037485SYan-Hsuan Chuang } 2286e3037485SYan-Hsuan Chuang 2287e3037485SYan-Hsuan Chuang void rtw_phy_tx_power_limit_config(struct rtw_hal *hal) 2288e3037485SYan-Hsuan Chuang { 2289e3037485SYan-Hsuan Chuang u8 regd, bw, rs; 2290e3037485SYan-Hsuan Chuang 229193f68a86SZong-Zhe Yang /* default at channel 1 */ 229293f68a86SZong-Zhe Yang hal->cch_by_bw[RTW_CHANNEL_WIDTH_20] = 1; 229393f68a86SZong-Zhe Yang 2294e3037485SYan-Hsuan Chuang for (regd = 0; regd < RTW_REGD_MAX; regd++) 2295e3037485SYan-Hsuan Chuang for (bw = 0; bw < RTW_CHANNEL_WIDTH_MAX; bw++) 2296e3037485SYan-Hsuan Chuang for (rs = 0; rs < RTW_RATE_SECTION_MAX; rs++) 229743712199SYan-Hsuan Chuang __rtw_phy_tx_power_limit_config(hal, regd, bw, rs); 2298e3037485SYan-Hsuan Chuang } 2299e3037485SYan-Hsuan Chuang 23000d350f0aSTzu-En Huang static void rtw_phy_init_tx_power_limit(struct rtw_dev *rtwdev, 230143712199SYan-Hsuan Chuang u8 regd, u8 bw, u8 rs) 2302e3037485SYan-Hsuan Chuang { 23030d350f0aSTzu-En Huang struct rtw_hal *hal = &rtwdev->hal; 23040d350f0aSTzu-En Huang s8 max_power_index = (s8)rtwdev->chip->max_power_index; 2305e3037485SYan-Hsuan Chuang u8 ch; 2306e3037485SYan-Hsuan Chuang 2307e3037485SYan-Hsuan Chuang /* 2.4G channels */ 2308e3037485SYan-Hsuan Chuang for (ch = 0; ch < RTW_MAX_CHANNEL_NUM_2G; ch++) 23090d350f0aSTzu-En Huang hal->tx_pwr_limit_2g[regd][bw][rs][ch] = max_power_index; 2310e3037485SYan-Hsuan Chuang 2311e3037485SYan-Hsuan Chuang /* 5G channels */ 2312e3037485SYan-Hsuan Chuang for (ch = 0; ch < RTW_MAX_CHANNEL_NUM_5G; ch++) 23130d350f0aSTzu-En Huang hal->tx_pwr_limit_5g[regd][bw][rs][ch] = max_power_index; 2314e3037485SYan-Hsuan Chuang } 2315e3037485SYan-Hsuan Chuang 23160d350f0aSTzu-En Huang void rtw_phy_init_tx_power(struct rtw_dev *rtwdev) 2317e3037485SYan-Hsuan Chuang { 23180d350f0aSTzu-En Huang struct rtw_hal *hal = &rtwdev->hal; 2319e3037485SYan-Hsuan Chuang u8 regd, path, rate, rs, bw; 2320e3037485SYan-Hsuan Chuang 2321e3037485SYan-Hsuan Chuang /* init tx power by rate offset */ 2322e3037485SYan-Hsuan Chuang for (path = 0; path < RTW_RF_PATH_MAX; path++) { 2323e3037485SYan-Hsuan Chuang for (rate = 0; rate < DESC_RATE_MAX; rate++) { 2324e3037485SYan-Hsuan Chuang hal->tx_pwr_by_rate_offset_2g[path][rate] = 0; 2325e3037485SYan-Hsuan Chuang hal->tx_pwr_by_rate_offset_5g[path][rate] = 0; 2326e3037485SYan-Hsuan Chuang } 2327e3037485SYan-Hsuan Chuang } 2328e3037485SYan-Hsuan Chuang 2329e3037485SYan-Hsuan Chuang /* init tx power limit */ 2330e3037485SYan-Hsuan Chuang for (regd = 0; regd < RTW_REGD_MAX; regd++) 2331e3037485SYan-Hsuan Chuang for (bw = 0; bw < RTW_CHANNEL_WIDTH_MAX; bw++) 2332e3037485SYan-Hsuan Chuang for (rs = 0; rs < RTW_RATE_SECTION_MAX; rs++) 23330d350f0aSTzu-En Huang rtw_phy_init_tx_power_limit(rtwdev, regd, bw, 23340d350f0aSTzu-En Huang rs); 2335e3037485SYan-Hsuan Chuang } 2336c97ee3e0STzu-En Huang 2337c97ee3e0STzu-En Huang void rtw_phy_config_swing_table(struct rtw_dev *rtwdev, 2338c97ee3e0STzu-En Huang struct rtw_swing_table *swing_table) 2339c97ee3e0STzu-En Huang { 2340c97ee3e0STzu-En Huang const struct rtw_pwr_track_tbl *tbl = rtwdev->chip->pwr_track_tbl; 2341c97ee3e0STzu-En Huang u8 channel = rtwdev->hal.current_channel; 2342c97ee3e0STzu-En Huang 2343c97ee3e0STzu-En Huang if (IS_CH_2G_BAND(channel)) { 2344c97ee3e0STzu-En Huang if (rtwdev->dm_info.tx_rate <= DESC_RATE11M) { 2345c97ee3e0STzu-En Huang swing_table->p[RF_PATH_A] = tbl->pwrtrk_2g_ccka_p; 2346c97ee3e0STzu-En Huang swing_table->n[RF_PATH_A] = tbl->pwrtrk_2g_ccka_n; 2347c97ee3e0STzu-En Huang swing_table->p[RF_PATH_B] = tbl->pwrtrk_2g_cckb_p; 2348c97ee3e0STzu-En Huang swing_table->n[RF_PATH_B] = tbl->pwrtrk_2g_cckb_n; 2349c97ee3e0STzu-En Huang } else { 2350c97ee3e0STzu-En Huang swing_table->p[RF_PATH_A] = tbl->pwrtrk_2ga_p; 2351c97ee3e0STzu-En Huang swing_table->n[RF_PATH_A] = tbl->pwrtrk_2ga_n; 2352c97ee3e0STzu-En Huang swing_table->p[RF_PATH_B] = tbl->pwrtrk_2gb_p; 2353c97ee3e0STzu-En Huang swing_table->n[RF_PATH_B] = tbl->pwrtrk_2gb_n; 2354c97ee3e0STzu-En Huang } 2355c97ee3e0STzu-En Huang } else if (IS_CH_5G_BAND_1(channel) || IS_CH_5G_BAND_2(channel)) { 2356c97ee3e0STzu-En Huang swing_table->p[RF_PATH_A] = tbl->pwrtrk_5ga_p[RTW_PWR_TRK_5G_1]; 2357c97ee3e0STzu-En Huang swing_table->n[RF_PATH_A] = tbl->pwrtrk_5ga_n[RTW_PWR_TRK_5G_1]; 2358c97ee3e0STzu-En Huang swing_table->p[RF_PATH_B] = tbl->pwrtrk_5gb_p[RTW_PWR_TRK_5G_1]; 2359c97ee3e0STzu-En Huang swing_table->n[RF_PATH_B] = tbl->pwrtrk_5gb_n[RTW_PWR_TRK_5G_1]; 2360c97ee3e0STzu-En Huang } else if (IS_CH_5G_BAND_3(channel)) { 2361c97ee3e0STzu-En Huang swing_table->p[RF_PATH_A] = tbl->pwrtrk_5ga_p[RTW_PWR_TRK_5G_2]; 2362c97ee3e0STzu-En Huang swing_table->n[RF_PATH_A] = tbl->pwrtrk_5ga_n[RTW_PWR_TRK_5G_2]; 2363c97ee3e0STzu-En Huang swing_table->p[RF_PATH_B] = tbl->pwrtrk_5gb_p[RTW_PWR_TRK_5G_2]; 2364c97ee3e0STzu-En Huang swing_table->n[RF_PATH_B] = tbl->pwrtrk_5gb_n[RTW_PWR_TRK_5G_2]; 2365c97ee3e0STzu-En Huang } else if (IS_CH_5G_BAND_4(channel)) { 2366c97ee3e0STzu-En Huang swing_table->p[RF_PATH_A] = tbl->pwrtrk_5ga_p[RTW_PWR_TRK_5G_3]; 2367c97ee3e0STzu-En Huang swing_table->n[RF_PATH_A] = tbl->pwrtrk_5ga_n[RTW_PWR_TRK_5G_3]; 2368c97ee3e0STzu-En Huang swing_table->p[RF_PATH_B] = tbl->pwrtrk_5gb_p[RTW_PWR_TRK_5G_3]; 2369c97ee3e0STzu-En Huang swing_table->n[RF_PATH_B] = tbl->pwrtrk_5gb_n[RTW_PWR_TRK_5G_3]; 2370c97ee3e0STzu-En Huang } else { 2371c97ee3e0STzu-En Huang swing_table->p[RF_PATH_A] = tbl->pwrtrk_2ga_p; 2372c97ee3e0STzu-En Huang swing_table->n[RF_PATH_A] = tbl->pwrtrk_2ga_n; 2373c97ee3e0STzu-En Huang swing_table->p[RF_PATH_B] = tbl->pwrtrk_2gb_p; 2374c97ee3e0STzu-En Huang swing_table->n[RF_PATH_B] = tbl->pwrtrk_2gb_n; 2375c97ee3e0STzu-En Huang } 2376c97ee3e0STzu-En Huang } 2377449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_config_swing_table); 2378c97ee3e0STzu-En Huang 2379c97ee3e0STzu-En Huang void rtw_phy_pwrtrack_avg(struct rtw_dev *rtwdev, u8 thermal, u8 path) 2380c97ee3e0STzu-En Huang { 2381c97ee3e0STzu-En Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 2382c97ee3e0STzu-En Huang 2383c97ee3e0STzu-En Huang ewma_thermal_add(&dm_info->avg_thermal[path], thermal); 2384c97ee3e0STzu-En Huang dm_info->thermal_avg[path] = 2385c97ee3e0STzu-En Huang ewma_thermal_read(&dm_info->avg_thermal[path]); 2386c97ee3e0STzu-En Huang } 2387449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_pwrtrack_avg); 2388c97ee3e0STzu-En Huang 2389c97ee3e0STzu-En Huang bool rtw_phy_pwrtrack_thermal_changed(struct rtw_dev *rtwdev, u8 thermal, 2390c97ee3e0STzu-En Huang u8 path) 2391c97ee3e0STzu-En Huang { 2392c97ee3e0STzu-En Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 2393c97ee3e0STzu-En Huang u8 avg = ewma_thermal_read(&dm_info->avg_thermal[path]); 2394c97ee3e0STzu-En Huang 2395c97ee3e0STzu-En Huang if (avg == thermal) 2396c97ee3e0STzu-En Huang return false; 2397c97ee3e0STzu-En Huang 2398c97ee3e0STzu-En Huang return true; 2399c97ee3e0STzu-En Huang } 2400449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_pwrtrack_thermal_changed); 2401c97ee3e0STzu-En Huang 2402c97ee3e0STzu-En Huang u8 rtw_phy_pwrtrack_get_delta(struct rtw_dev *rtwdev, u8 path) 2403c97ee3e0STzu-En Huang { 2404c97ee3e0STzu-En Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 2405c97ee3e0STzu-En Huang u8 therm_avg, therm_efuse, therm_delta; 2406c97ee3e0STzu-En Huang 2407c97ee3e0STzu-En Huang therm_avg = dm_info->thermal_avg[path]; 2408c97ee3e0STzu-En Huang therm_efuse = rtwdev->efuse.thermal_meter[path]; 2409c97ee3e0STzu-En Huang therm_delta = abs(therm_avg - therm_efuse); 2410c97ee3e0STzu-En Huang 2411c97ee3e0STzu-En Huang return min_t(u8, therm_delta, RTW_PWR_TRK_TBL_SZ - 1); 2412c97ee3e0STzu-En Huang } 2413449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_pwrtrack_get_delta); 2414c97ee3e0STzu-En Huang 2415c97ee3e0STzu-En Huang s8 rtw_phy_pwrtrack_get_pwridx(struct rtw_dev *rtwdev, 2416c97ee3e0STzu-En Huang struct rtw_swing_table *swing_table, 2417c97ee3e0STzu-En Huang u8 tbl_path, u8 therm_path, u8 delta) 2418c97ee3e0STzu-En Huang { 2419c97ee3e0STzu-En Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 2420c97ee3e0STzu-En Huang const u8 *delta_swing_table_idx_pos; 2421c97ee3e0STzu-En Huang const u8 *delta_swing_table_idx_neg; 2422c97ee3e0STzu-En Huang 2423c97ee3e0STzu-En Huang if (delta >= RTW_PWR_TRK_TBL_SZ) { 2424c97ee3e0STzu-En Huang rtw_warn(rtwdev, "power track table overflow\n"); 2425c97ee3e0STzu-En Huang return 0; 2426c97ee3e0STzu-En Huang } 2427c97ee3e0STzu-En Huang 2428baff8da6SColin Ian King if (!swing_table) { 2429c97ee3e0STzu-En Huang rtw_warn(rtwdev, "swing table not configured\n"); 2430c97ee3e0STzu-En Huang return 0; 2431c97ee3e0STzu-En Huang } 2432c97ee3e0STzu-En Huang 2433c97ee3e0STzu-En Huang delta_swing_table_idx_pos = swing_table->p[tbl_path]; 2434c97ee3e0STzu-En Huang delta_swing_table_idx_neg = swing_table->n[tbl_path]; 2435c97ee3e0STzu-En Huang 2436c97ee3e0STzu-En Huang if (!delta_swing_table_idx_pos || !delta_swing_table_idx_neg) { 2437c97ee3e0STzu-En Huang rtw_warn(rtwdev, "invalid swing table index\n"); 2438c97ee3e0STzu-En Huang return 0; 2439c97ee3e0STzu-En Huang } 2440c97ee3e0STzu-En Huang 2441c97ee3e0STzu-En Huang if (dm_info->thermal_avg[therm_path] > 2442c97ee3e0STzu-En Huang rtwdev->efuse.thermal_meter[therm_path]) 2443c97ee3e0STzu-En Huang return delta_swing_table_idx_pos[delta]; 2444c97ee3e0STzu-En Huang else 2445c97ee3e0STzu-En Huang return -delta_swing_table_idx_neg[delta]; 2446c97ee3e0STzu-En Huang } 2447449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_pwrtrack_get_pwridx); 2448c97ee3e0STzu-En Huang 24497ae7784eSPo-Hao Huang bool rtw_phy_pwrtrack_need_lck(struct rtw_dev *rtwdev) 24507ae7784eSPo-Hao Huang { 24517ae7784eSPo-Hao Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 24527ae7784eSPo-Hao Huang u8 delta_lck; 24537ae7784eSPo-Hao Huang 24547ae7784eSPo-Hao Huang delta_lck = abs(dm_info->thermal_avg[0] - dm_info->thermal_meter_lck); 24557ae7784eSPo-Hao Huang if (delta_lck >= rtwdev->chip->lck_threshold) { 24567ae7784eSPo-Hao Huang dm_info->thermal_meter_lck = dm_info->thermal_avg[0]; 24577ae7784eSPo-Hao Huang return true; 24587ae7784eSPo-Hao Huang } 24597ae7784eSPo-Hao Huang return false; 24607ae7784eSPo-Hao Huang } 24617ae7784eSPo-Hao Huang EXPORT_SYMBOL(rtw_phy_pwrtrack_need_lck); 24627ae7784eSPo-Hao Huang 2463c97ee3e0STzu-En Huang bool rtw_phy_pwrtrack_need_iqk(struct rtw_dev *rtwdev) 2464c97ee3e0STzu-En Huang { 2465c97ee3e0STzu-En Huang struct rtw_dm_info *dm_info = &rtwdev->dm_info; 2466c97ee3e0STzu-En Huang u8 delta_iqk; 2467c97ee3e0STzu-En Huang 2468c97ee3e0STzu-En Huang delta_iqk = abs(dm_info->thermal_avg[0] - dm_info->thermal_meter_k); 2469c97ee3e0STzu-En Huang if (delta_iqk >= rtwdev->chip->iqk_threshold) { 2470c97ee3e0STzu-En Huang dm_info->thermal_meter_k = dm_info->thermal_avg[0]; 2471c97ee3e0STzu-En Huang return true; 2472c97ee3e0STzu-En Huang } 2473c97ee3e0STzu-En Huang return false; 2474c97ee3e0STzu-En Huang } 2475449be866SZong-Zhe Yang EXPORT_SYMBOL(rtw_phy_pwrtrack_need_iqk); 24761188301fSPo-Hao Huang 24771188301fSPo-Hao Huang static void rtw_phy_set_tx_path_by_reg(struct rtw_dev *rtwdev, 24781188301fSPo-Hao Huang enum rtw_bb_path tx_path_sel_1ss) 24791188301fSPo-Hao Huang { 24801188301fSPo-Hao Huang struct rtw_path_div *path_div = &rtwdev->dm_path_div; 24811188301fSPo-Hao Huang enum rtw_bb_path tx_path_sel_cck = tx_path_sel_1ss; 24821188301fSPo-Hao Huang struct rtw_chip_info *chip = rtwdev->chip; 24831188301fSPo-Hao Huang 24841188301fSPo-Hao Huang if (tx_path_sel_1ss == path_div->current_tx_path) 24851188301fSPo-Hao Huang return; 24861188301fSPo-Hao Huang 24871188301fSPo-Hao Huang path_div->current_tx_path = tx_path_sel_1ss; 24881188301fSPo-Hao Huang rtw_dbg(rtwdev, RTW_DBG_PATH_DIV, "Switch TX path=%s\n", 24891188301fSPo-Hao Huang tx_path_sel_1ss == BB_PATH_A ? "A" : "B"); 24901188301fSPo-Hao Huang chip->ops->config_tx_path(rtwdev, rtwdev->hal.antenna_tx, 24911188301fSPo-Hao Huang tx_path_sel_1ss, tx_path_sel_cck, false); 24921188301fSPo-Hao Huang } 24931188301fSPo-Hao Huang 24941188301fSPo-Hao Huang static void rtw_phy_tx_path_div_select(struct rtw_dev *rtwdev) 24951188301fSPo-Hao Huang { 24961188301fSPo-Hao Huang struct rtw_path_div *path_div = &rtwdev->dm_path_div; 24971188301fSPo-Hao Huang enum rtw_bb_path path = path_div->current_tx_path; 24981188301fSPo-Hao Huang s32 rssi_a = 0, rssi_b = 0; 24991188301fSPo-Hao Huang 25001188301fSPo-Hao Huang if (path_div->path_a_cnt) 25011188301fSPo-Hao Huang rssi_a = path_div->path_a_sum / path_div->path_a_cnt; 25021188301fSPo-Hao Huang else 25031188301fSPo-Hao Huang rssi_a = 0; 25041188301fSPo-Hao Huang if (path_div->path_b_cnt) 25051188301fSPo-Hao Huang rssi_b = path_div->path_b_sum / path_div->path_b_cnt; 25061188301fSPo-Hao Huang else 25071188301fSPo-Hao Huang rssi_b = 0; 25081188301fSPo-Hao Huang 25091188301fSPo-Hao Huang if (rssi_a != rssi_b) 25101188301fSPo-Hao Huang path = (rssi_a > rssi_b) ? BB_PATH_A : BB_PATH_B; 25111188301fSPo-Hao Huang 25121188301fSPo-Hao Huang path_div->path_a_cnt = 0; 25131188301fSPo-Hao Huang path_div->path_a_sum = 0; 25141188301fSPo-Hao Huang path_div->path_b_cnt = 0; 25151188301fSPo-Hao Huang path_div->path_b_sum = 0; 25161188301fSPo-Hao Huang rtw_phy_set_tx_path_by_reg(rtwdev, path); 25171188301fSPo-Hao Huang } 25181188301fSPo-Hao Huang 25191188301fSPo-Hao Huang static void rtw_phy_tx_path_diversity_2ss(struct rtw_dev *rtwdev) 25201188301fSPo-Hao Huang { 25211188301fSPo-Hao Huang if (rtwdev->hal.antenna_rx != BB_PATH_AB) { 25221188301fSPo-Hao Huang rtw_dbg(rtwdev, RTW_DBG_PATH_DIV, 25231188301fSPo-Hao Huang "[Return] tx_Path_en=%d, rx_Path_en=%d\n", 25241188301fSPo-Hao Huang rtwdev->hal.antenna_tx, rtwdev->hal.antenna_rx); 25251188301fSPo-Hao Huang return; 25261188301fSPo-Hao Huang } 25271188301fSPo-Hao Huang if (rtwdev->sta_cnt == 0) { 25281188301fSPo-Hao Huang rtw_dbg(rtwdev, RTW_DBG_PATH_DIV, "No Link\n"); 25291188301fSPo-Hao Huang return; 25301188301fSPo-Hao Huang } 25311188301fSPo-Hao Huang 25321188301fSPo-Hao Huang rtw_phy_tx_path_div_select(rtwdev); 25331188301fSPo-Hao Huang } 25341188301fSPo-Hao Huang 25351188301fSPo-Hao Huang void rtw_phy_tx_path_diversity(struct rtw_dev *rtwdev) 25361188301fSPo-Hao Huang { 25371188301fSPo-Hao Huang struct rtw_chip_info *chip = rtwdev->chip; 25381188301fSPo-Hao Huang 25391188301fSPo-Hao Huang if (!chip->path_div_supported) 25401188301fSPo-Hao Huang return; 25411188301fSPo-Hao Huang 25421188301fSPo-Hao Huang rtw_phy_tx_path_diversity_2ss(rtwdev); 25431188301fSPo-Hao Huang } 2544