aa843d4219878dd592ba8b8592d751dcaf950051
[openwrt/staging/blogic.git] /
1 From 821b5192c955144bd2f0aeea6cd153e1aedd16e1 Mon Sep 17 00:00:00 2001
2 From: Shiji Yang <yangshiji66@outlook.com>
3 Date: Fri, 11 Aug 2023 14:34:54 +0800
4 Subject: wifi: rt2x00: limit MT7620 TX power based on eeprom calibration
5
6 In the vendor driver, the current channel power is queried from
7 EEPROM_TXPOWER_BG1 and EEPROM_TXPOWER_BG2. And then the mixed value
8 will be written into the low half-word of the TX_ALC_CFG_0 register.
9 The high half-word of the TX_ALC_CFG_0 is a fixed value 0x2f2f.
10
11 We can't get the accurate TX power. Based on my tests and the new
12 MediaTek mt76 driver source code, the real TX power is approximately
13 equal to channel_power + (max) rate_power. Usually max rate_power is
14 the gain of the OFDM 6M rate, which can be readed from the offset
15 EEPROM_TXPOWER_BYRATE +1.
16
17 Based on these eeprom values, this patch adds basic TX power control
18 for the MT7620 and limits its maximum TX power. This can avoid the
19 link speed decrease caused by chip overheating. rt2800_config_alc()
20 function has also been renamed to rt2800_config_alc_rt6352() because
21 it's only used by RT6352 (MT7620).
22
23 Notice:
24 It's still need some work to sync the max channel power to the user
25 interface. This part is missing from the rt2x00 driver framework. If
26 we set the power exceed the calibration value, it won't take effect.
27
28 Signed-off-by: Shiji Yang <yangshiji66@outlook.com>
29 Acked-by: Stanislaw Gruszka <stf_xl@wp.pl>
30 Signed-off-by: Kalle Valo <kvalo@kernel.org>
31 Link: https://lore.kernel.org/r/TYAP286MB03159090ED14044215E59FD6BC10A@TYAP286MB0315.JPNP286.PROD.OUTLOOK.COM
32 ---
33 drivers/net/wireless/ralink/rt2x00/rt2800lib.c | 57 ++++++++++++++++++--------
34 1 file changed, 40 insertions(+), 17 deletions(-)
35
36 --- a/drivers/net/wireless/ralink/rt2x00/rt2800lib.c
37 +++ b/drivers/net/wireless/ralink/rt2x00/rt2800lib.c
38 @@ -3865,28 +3865,51 @@ static void rt2800_config_channel_rf7620
39 }
40 }
41
42 -static void rt2800_config_alc(struct rt2x00_dev *rt2x00dev,
43 - struct ieee80211_channel *chan,
44 - int power_level) {
45 - u16 eeprom, target_power, max_power;
46 +static void rt2800_config_alc_rt6352(struct rt2x00_dev *rt2x00dev,
47 + struct ieee80211_channel *chan,
48 + int power_level)
49 +{
50 + int cur_channel = rt2x00dev->rf_channel;
51 + u16 eeprom, chan_power, rate_power, target_power;
52 + u16 tx_power[2];
53 + s8 *power_group[2];
54 u32 mac_sys_ctrl;
55 - u32 reg;
56 + u32 cnt, reg;
57 u8 bbp;
58
59 - /* hardware unit is 0.5dBm, limited to 23.5dBm */
60 - power_level *= 2;
61 - if (power_level > 0x2f)
62 - power_level = 0x2f;
63 -
64 - max_power = chan->max_power * 2;
65 - if (max_power > 0x2f)
66 - max_power = 0x2f;
67 + if (WARN_ON(cur_channel < 1 || cur_channel > 14))
68 + return;
69 +
70 + /* get per chain power, 2 chains in total, unit is 0.5dBm */
71 + power_level = (power_level - 3) * 2;
72 +
73 + /* We can't get the accurate TX power. Based on some tests, the real
74 + * TX power is approximately equal to channel_power + (max)rate_power.
75 + * Usually max rate_power is the gain of the OFDM 6M rate. The antenna
76 + * gain and externel PA gain are not included as we are unable to
77 + * obtain these values.
78 + */
79 + rate_power = rt2800_eeprom_read_from_array(rt2x00dev,
80 + EEPROM_TXPOWER_BYRATE, 1);
81 + rate_power &= 0x3f;
82 + power_level -= rate_power;
83 + if (power_level < 1)
84 + power_level = 1;
85 +
86 + power_group[0] = rt2800_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG1);
87 + power_group[1] = rt2800_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG2);
88 + for (cnt = 0; cnt < 2; cnt++) {
89 + chan_power = power_group[cnt][cur_channel - 1];
90 + if (chan_power >= 0x20 || chan_power == 0)
91 + chan_power = 0x10;
92 + tx_power[cnt] = power_level < chan_power ? power_level : chan_power;
93 + }
94
95 reg = rt2800_register_read(rt2x00dev, TX_ALC_CFG_0);
96 - rt2x00_set_field32(&reg, TX_ALC_CFG_0_CH_INIT_0, power_level);
97 - rt2x00_set_field32(&reg, TX_ALC_CFG_0_CH_INIT_1, power_level);
98 - rt2x00_set_field32(&reg, TX_ALC_CFG_0_LIMIT_0, max_power);
99 - rt2x00_set_field32(&reg, TX_ALC_CFG_0_LIMIT_1, max_power);
100 + rt2x00_set_field32(&reg, TX_ALC_CFG_0_CH_INIT_0, tx_power[0]);
101 + rt2x00_set_field32(&reg, TX_ALC_CFG_0_CH_INIT_1, tx_power[1]);
102 + rt2x00_set_field32(&reg, TX_ALC_CFG_0_LIMIT_0, 0x2f);
103 + rt2x00_set_field32(&reg, TX_ALC_CFG_0_LIMIT_1, 0x2f);
104
105 eeprom = rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1);
106 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_INTERNAL_TX_ALC)) {
107 @@ -5268,7 +5291,7 @@ static void rt2800_config_txpower_rt6352
108 rt2x00_set_field32(&pwreg, TX_PWR_CFG_9B_STBC_MCS7, t);
109 rt2800_register_write(rt2x00dev, TX_PWR_CFG_9, pwreg);
110
111 - rt2800_config_alc(rt2x00dev, chan, power_level);
112 + rt2800_config_alc_rt6352(rt2x00dev, chan, power_level);
113
114 /* TODO: temperature compensation code! */
115 }