18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: ISC 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name> 48c2ecf20Sopenharmony_ci * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com> 58c2ecf20Sopenharmony_ci */ 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#include <linux/kernel.h> 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include "mt76x02.h" 108c2ecf20Sopenharmony_ci#include "mt76x02_phy.h" 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_civoid mt76x02_phy_set_rxpath(struct mt76x02_dev *dev) 138c2ecf20Sopenharmony_ci{ 148c2ecf20Sopenharmony_ci u32 val; 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci val = mt76_rr(dev, MT_BBP(AGC, 0)); 178c2ecf20Sopenharmony_ci val &= ~BIT(4); 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci switch (dev->chainmask & 0xf) { 208c2ecf20Sopenharmony_ci case 2: 218c2ecf20Sopenharmony_ci val |= BIT(3); 228c2ecf20Sopenharmony_ci break; 238c2ecf20Sopenharmony_ci default: 248c2ecf20Sopenharmony_ci val &= ~BIT(3); 258c2ecf20Sopenharmony_ci break; 268c2ecf20Sopenharmony_ci } 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(AGC, 0), val); 298c2ecf20Sopenharmony_ci mb(); 308c2ecf20Sopenharmony_ci val = mt76_rr(dev, MT_BBP(AGC, 0)); 318c2ecf20Sopenharmony_ci} 328c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mt76x02_phy_set_rxpath); 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_civoid mt76x02_phy_set_txdac(struct mt76x02_dev *dev) 358c2ecf20Sopenharmony_ci{ 368c2ecf20Sopenharmony_ci int txpath; 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci txpath = (dev->chainmask >> 8) & 0xf; 398c2ecf20Sopenharmony_ci switch (txpath) { 408c2ecf20Sopenharmony_ci case 2: 418c2ecf20Sopenharmony_ci mt76_set(dev, MT_BBP(TXBE, 5), 0x3); 428c2ecf20Sopenharmony_ci break; 438c2ecf20Sopenharmony_ci default: 448c2ecf20Sopenharmony_ci mt76_clear(dev, MT_BBP(TXBE, 5), 0x3); 458c2ecf20Sopenharmony_ci break; 468c2ecf20Sopenharmony_ci } 478c2ecf20Sopenharmony_ci} 488c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mt76x02_phy_set_txdac); 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_cistatic u32 518c2ecf20Sopenharmony_cimt76x02_tx_power_mask(u8 v1, u8 v2, u8 v3, u8 v4) 528c2ecf20Sopenharmony_ci{ 538c2ecf20Sopenharmony_ci u32 val = 0; 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci val |= (v1 & (BIT(6) - 1)) << 0; 568c2ecf20Sopenharmony_ci val |= (v2 & (BIT(6) - 1)) << 8; 578c2ecf20Sopenharmony_ci val |= (v3 & (BIT(6) - 1)) << 16; 588c2ecf20Sopenharmony_ci val |= (v4 & (BIT(6) - 1)) << 24; 598c2ecf20Sopenharmony_ci return val; 608c2ecf20Sopenharmony_ci} 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ciint mt76x02_get_max_rate_power(struct mt76_rate_power *r) 638c2ecf20Sopenharmony_ci{ 648c2ecf20Sopenharmony_ci s8 ret = 0; 658c2ecf20Sopenharmony_ci int i; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci for (i = 0; i < sizeof(r->all); i++) 688c2ecf20Sopenharmony_ci ret = max(ret, r->all[i]); 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci return ret; 718c2ecf20Sopenharmony_ci} 728c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mt76x02_get_max_rate_power); 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_civoid mt76x02_limit_rate_power(struct mt76_rate_power *r, int limit) 758c2ecf20Sopenharmony_ci{ 768c2ecf20Sopenharmony_ci int i; 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci for (i = 0; i < sizeof(r->all); i++) 798c2ecf20Sopenharmony_ci if (r->all[i] > limit) 808c2ecf20Sopenharmony_ci r->all[i] = limit; 818c2ecf20Sopenharmony_ci} 828c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mt76x02_limit_rate_power); 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_civoid mt76x02_add_rate_power_offset(struct mt76_rate_power *r, int offset) 858c2ecf20Sopenharmony_ci{ 868c2ecf20Sopenharmony_ci int i; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci for (i = 0; i < sizeof(r->all); i++) 898c2ecf20Sopenharmony_ci r->all[i] += offset; 908c2ecf20Sopenharmony_ci} 918c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mt76x02_add_rate_power_offset); 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_civoid mt76x02_phy_set_txpower(struct mt76x02_dev *dev, int txp_0, int txp_1) 948c2ecf20Sopenharmony_ci{ 958c2ecf20Sopenharmony_ci struct mt76_rate_power *t = &dev->mt76.rate_power; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci mt76_rmw_field(dev, MT_TX_ALC_CFG_0, MT_TX_ALC_CFG_0_CH_INIT_0, txp_0); 988c2ecf20Sopenharmony_ci mt76_rmw_field(dev, MT_TX_ALC_CFG_0, MT_TX_ALC_CFG_0_CH_INIT_1, txp_1); 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci mt76_wr(dev, MT_TX_PWR_CFG_0, 1018c2ecf20Sopenharmony_ci mt76x02_tx_power_mask(t->cck[0], t->cck[2], t->ofdm[0], 1028c2ecf20Sopenharmony_ci t->ofdm[2])); 1038c2ecf20Sopenharmony_ci mt76_wr(dev, MT_TX_PWR_CFG_1, 1048c2ecf20Sopenharmony_ci mt76x02_tx_power_mask(t->ofdm[4], t->ofdm[6], t->ht[0], 1058c2ecf20Sopenharmony_ci t->ht[2])); 1068c2ecf20Sopenharmony_ci mt76_wr(dev, MT_TX_PWR_CFG_2, 1078c2ecf20Sopenharmony_ci mt76x02_tx_power_mask(t->ht[4], t->ht[6], t->ht[8], 1088c2ecf20Sopenharmony_ci t->ht[10])); 1098c2ecf20Sopenharmony_ci mt76_wr(dev, MT_TX_PWR_CFG_3, 1108c2ecf20Sopenharmony_ci mt76x02_tx_power_mask(t->ht[12], t->ht[14], t->stbc[0], 1118c2ecf20Sopenharmony_ci t->stbc[2])); 1128c2ecf20Sopenharmony_ci mt76_wr(dev, MT_TX_PWR_CFG_4, 1138c2ecf20Sopenharmony_ci mt76x02_tx_power_mask(t->stbc[4], t->stbc[6], 0, 0)); 1148c2ecf20Sopenharmony_ci mt76_wr(dev, MT_TX_PWR_CFG_7, 1158c2ecf20Sopenharmony_ci mt76x02_tx_power_mask(t->ofdm[7], t->vht[8], t->ht[7], 1168c2ecf20Sopenharmony_ci t->vht[9])); 1178c2ecf20Sopenharmony_ci mt76_wr(dev, MT_TX_PWR_CFG_8, 1188c2ecf20Sopenharmony_ci mt76x02_tx_power_mask(t->ht[14], 0, t->vht[8], t->vht[9])); 1198c2ecf20Sopenharmony_ci mt76_wr(dev, MT_TX_PWR_CFG_9, 1208c2ecf20Sopenharmony_ci mt76x02_tx_power_mask(t->ht[7], 0, t->stbc[8], t->stbc[9])); 1218c2ecf20Sopenharmony_ci} 1228c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mt76x02_phy_set_txpower); 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_civoid mt76x02_phy_set_bw(struct mt76x02_dev *dev, int width, u8 ctrl) 1258c2ecf20Sopenharmony_ci{ 1268c2ecf20Sopenharmony_ci int core_val, agc_val; 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci switch (width) { 1298c2ecf20Sopenharmony_ci case NL80211_CHAN_WIDTH_80: 1308c2ecf20Sopenharmony_ci core_val = 3; 1318c2ecf20Sopenharmony_ci agc_val = 7; 1328c2ecf20Sopenharmony_ci break; 1338c2ecf20Sopenharmony_ci case NL80211_CHAN_WIDTH_40: 1348c2ecf20Sopenharmony_ci core_val = 2; 1358c2ecf20Sopenharmony_ci agc_val = 3; 1368c2ecf20Sopenharmony_ci break; 1378c2ecf20Sopenharmony_ci default: 1388c2ecf20Sopenharmony_ci core_val = 0; 1398c2ecf20Sopenharmony_ci agc_val = 1; 1408c2ecf20Sopenharmony_ci break; 1418c2ecf20Sopenharmony_ci } 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci mt76_rmw_field(dev, MT_BBP(CORE, 1), MT_BBP_CORE_R1_BW, core_val); 1448c2ecf20Sopenharmony_ci mt76_rmw_field(dev, MT_BBP(AGC, 0), MT_BBP_AGC_R0_BW, agc_val); 1458c2ecf20Sopenharmony_ci mt76_rmw_field(dev, MT_BBP(AGC, 0), MT_BBP_AGC_R0_CTRL_CHAN, ctrl); 1468c2ecf20Sopenharmony_ci mt76_rmw_field(dev, MT_BBP(TXBE, 0), MT_BBP_TXBE_R0_CTRL_CHAN, ctrl); 1478c2ecf20Sopenharmony_ci} 1488c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mt76x02_phy_set_bw); 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_civoid mt76x02_phy_set_band(struct mt76x02_dev *dev, int band, 1518c2ecf20Sopenharmony_ci bool primary_upper) 1528c2ecf20Sopenharmony_ci{ 1538c2ecf20Sopenharmony_ci switch (band) { 1548c2ecf20Sopenharmony_ci case NL80211_BAND_2GHZ: 1558c2ecf20Sopenharmony_ci mt76_set(dev, MT_TX_BAND_CFG, MT_TX_BAND_CFG_2G); 1568c2ecf20Sopenharmony_ci mt76_clear(dev, MT_TX_BAND_CFG, MT_TX_BAND_CFG_5G); 1578c2ecf20Sopenharmony_ci break; 1588c2ecf20Sopenharmony_ci case NL80211_BAND_5GHZ: 1598c2ecf20Sopenharmony_ci mt76_clear(dev, MT_TX_BAND_CFG, MT_TX_BAND_CFG_2G); 1608c2ecf20Sopenharmony_ci mt76_set(dev, MT_TX_BAND_CFG, MT_TX_BAND_CFG_5G); 1618c2ecf20Sopenharmony_ci break; 1628c2ecf20Sopenharmony_ci } 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci mt76_rmw_field(dev, MT_TX_BAND_CFG, MT_TX_BAND_CFG_UPPER_40M, 1658c2ecf20Sopenharmony_ci primary_upper); 1668c2ecf20Sopenharmony_ci} 1678c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mt76x02_phy_set_band); 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_cibool mt76x02_phy_adjust_vga_gain(struct mt76x02_dev *dev) 1708c2ecf20Sopenharmony_ci{ 1718c2ecf20Sopenharmony_ci u8 limit = dev->cal.low_gain > 0 ? 16 : 4; 1728c2ecf20Sopenharmony_ci bool ret = false; 1738c2ecf20Sopenharmony_ci u32 false_cca; 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci false_cca = FIELD_GET(MT_RX_STAT_1_CCA_ERRORS, 1768c2ecf20Sopenharmony_ci mt76_rr(dev, MT_RX_STAT_1)); 1778c2ecf20Sopenharmony_ci dev->cal.false_cca = false_cca; 1788c2ecf20Sopenharmony_ci if (false_cca > 800 && dev->cal.agc_gain_adjust < limit) { 1798c2ecf20Sopenharmony_ci dev->cal.agc_gain_adjust += 2; 1808c2ecf20Sopenharmony_ci ret = true; 1818c2ecf20Sopenharmony_ci } else if ((false_cca < 10 && dev->cal.agc_gain_adjust > 0) || 1828c2ecf20Sopenharmony_ci (dev->cal.agc_gain_adjust >= limit && false_cca < 500)) { 1838c2ecf20Sopenharmony_ci dev->cal.agc_gain_adjust -= 2; 1848c2ecf20Sopenharmony_ci ret = true; 1858c2ecf20Sopenharmony_ci } 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci dev->cal.agc_lowest_gain = dev->cal.agc_gain_adjust >= limit; 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci return ret; 1908c2ecf20Sopenharmony_ci} 1918c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mt76x02_phy_adjust_vga_gain); 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_civoid mt76x02_init_agc_gain(struct mt76x02_dev *dev) 1948c2ecf20Sopenharmony_ci{ 1958c2ecf20Sopenharmony_ci dev->cal.agc_gain_init[0] = mt76_get_field(dev, MT_BBP(AGC, 8), 1968c2ecf20Sopenharmony_ci MT_BBP_AGC_GAIN); 1978c2ecf20Sopenharmony_ci dev->cal.agc_gain_init[1] = mt76_get_field(dev, MT_BBP(AGC, 9), 1988c2ecf20Sopenharmony_ci MT_BBP_AGC_GAIN); 1998c2ecf20Sopenharmony_ci memcpy(dev->cal.agc_gain_cur, dev->cal.agc_gain_init, 2008c2ecf20Sopenharmony_ci sizeof(dev->cal.agc_gain_cur)); 2018c2ecf20Sopenharmony_ci dev->cal.low_gain = -1; 2028c2ecf20Sopenharmony_ci dev->cal.gain_init_done = true; 2038c2ecf20Sopenharmony_ci} 2048c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mt76x02_init_agc_gain); 205