1 // SPDX-License-Identifier: ISC
3 * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
8 #include "../mt76x02_phy.h"
11 mt76x2u_phy_channel_calibrate(struct mt76x02_dev
*dev
, bool mac_stopped
)
13 struct ieee80211_channel
*chan
= dev
->mt76
.chandef
.chan
;
14 bool is_5ghz
= chan
->band
== NL80211_BAND_5GHZ
;
16 if (dev
->cal
.channel_cal_done
)
19 if (mt76x2_channel_silent(dev
))
23 mt76x2u_mac_stop(dev
);
26 mt76x02_mcu_calibrate(dev
, MCU_CAL_LC
, 0);
28 mt76x02_mcu_calibrate(dev
, MCU_CAL_TX_LOFT
, is_5ghz
);
29 mt76x02_mcu_calibrate(dev
, MCU_CAL_TXIQ
, is_5ghz
);
30 mt76x02_mcu_calibrate(dev
, MCU_CAL_RXIQC_FI
, is_5ghz
);
31 mt76x02_mcu_calibrate(dev
, MCU_CAL_TEMP_SENSOR
, 0);
32 mt76x02_mcu_calibrate(dev
, MCU_CAL_TX_SHAPING
, 0);
35 mt76x2_mac_resume(dev
);
36 mt76x2_apply_gain_adj(dev
);
37 mt76x02_edcca_init(dev
);
39 dev
->cal
.channel_cal_done
= true;
42 void mt76x2u_phy_calibrate(struct work_struct
*work
)
44 struct mt76x02_dev
*dev
;
46 dev
= container_of(work
, struct mt76x02_dev
, cal_work
.work
);
48 mutex_lock(&dev
->mt76
.mutex
);
50 mt76x2u_phy_channel_calibrate(dev
, false);
51 mt76x2_phy_tssi_compensate(dev
);
52 mt76x2_phy_update_channel_gain(dev
);
54 mutex_unlock(&dev
->mt76
.mutex
);
56 ieee80211_queue_delayed_work(mt76_hw(dev
), &dev
->cal_work
,
57 MT_CALIBRATE_INTERVAL
);
60 int mt76x2u_phy_set_channel(struct mt76x02_dev
*dev
,
61 struct cfg80211_chan_def
*chandef
)
63 u32 ext_cca_chan
[4] = {
64 [0] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0
, 0) |
65 FIELD_PREP(MT_EXT_CCA_CFG_CCA1
, 1) |
66 FIELD_PREP(MT_EXT_CCA_CFG_CCA2
, 2) |
67 FIELD_PREP(MT_EXT_CCA_CFG_CCA3
, 3) |
68 FIELD_PREP(MT_EXT_CCA_CFG_CCA_MASK
, BIT(0)),
69 [1] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0
, 1) |
70 FIELD_PREP(MT_EXT_CCA_CFG_CCA1
, 0) |
71 FIELD_PREP(MT_EXT_CCA_CFG_CCA2
, 2) |
72 FIELD_PREP(MT_EXT_CCA_CFG_CCA3
, 3) |
73 FIELD_PREP(MT_EXT_CCA_CFG_CCA_MASK
, BIT(1)),
74 [2] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0
, 2) |
75 FIELD_PREP(MT_EXT_CCA_CFG_CCA1
, 3) |
76 FIELD_PREP(MT_EXT_CCA_CFG_CCA2
, 1) |
77 FIELD_PREP(MT_EXT_CCA_CFG_CCA3
, 0) |
78 FIELD_PREP(MT_EXT_CCA_CFG_CCA_MASK
, BIT(2)),
79 [3] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0
, 3) |
80 FIELD_PREP(MT_EXT_CCA_CFG_CCA1
, 2) |
81 FIELD_PREP(MT_EXT_CCA_CFG_CCA2
, 1) |
82 FIELD_PREP(MT_EXT_CCA_CFG_CCA3
, 0) |
83 FIELD_PREP(MT_EXT_CCA_CFG_CCA_MASK
, BIT(3)),
85 bool scan
= test_bit(MT76_SCANNING
, &dev
->mt76
.state
);
86 struct ieee80211_channel
*chan
= chandef
->chan
;
87 u8 channel
= chan
->hw_value
, bw
, bw_index
;
88 int ch_group_index
, freq
, freq1
, ret
;
90 dev
->cal
.channel_cal_done
= false;
91 freq
= chandef
->chan
->center_freq
;
92 freq1
= chandef
->center_freq1
;
94 switch (chandef
->width
) {
95 case NL80211_CHAN_WIDTH_40
:
104 channel
+= 2 - ch_group_index
* 4;
106 case NL80211_CHAN_WIDTH_80
:
107 ch_group_index
= (freq
- freq1
+ 30) / 20;
108 if (WARN_ON(ch_group_index
< 0 || ch_group_index
> 3))
111 bw_index
= ch_group_index
;
112 channel
+= 6 - ch_group_index
* 4;
121 mt76x2_read_rx_gain(dev
);
122 mt76x2_phy_set_txpower_regs(dev
, chan
->band
);
123 mt76x2_configure_tx_delay(dev
, chan
->band
, bw
);
124 mt76x2_phy_set_txpower(dev
);
126 mt76x02_phy_set_band(dev
, chan
->band
, ch_group_index
& 1);
127 mt76x02_phy_set_bw(dev
, chandef
->width
, ch_group_index
);
129 mt76_rmw(dev
, MT_EXT_CCA_CFG
,
130 (MT_EXT_CCA_CFG_CCA0
|
131 MT_EXT_CCA_CFG_CCA1
|
132 MT_EXT_CCA_CFG_CCA2
|
133 MT_EXT_CCA_CFG_CCA3
|
134 MT_EXT_CCA_CFG_CCA_MASK
),
135 ext_cca_chan
[ch_group_index
]);
137 ret
= mt76x2_mcu_set_channel(dev
, channel
, bw
, bw_index
, scan
);
141 mt76x2_mcu_init_gain(dev
, channel
, dev
->cal
.rx
.mcu_gain
, true);
144 if (mt76xx_rev(dev
) >= MT76XX_REV_E3
)
145 mt76_set(dev
, MT_BBP(RXO
, 13), BIT(10));
147 if (!dev
->cal
.init_cal_done
) {
148 u8 val
= mt76x02_eeprom_get(dev
, MT_EE_BT_RCAL_RESULT
);
151 mt76x02_mcu_calibrate(dev
, MCU_CAL_R
, 0);
154 mt76x02_mcu_calibrate(dev
, MCU_CAL_RXDCOC
, channel
);
156 /* Rx LPF calibration */
157 if (!dev
->cal
.init_cal_done
)
158 mt76x02_mcu_calibrate(dev
, MCU_CAL_RC
, 0);
159 dev
->cal
.init_cal_done
= true;
161 mt76_wr(dev
, MT_BBP(AGC
, 61), 0xff64a4e2);
162 mt76_wr(dev
, MT_BBP(AGC
, 7), 0x08081010);
163 mt76_wr(dev
, MT_BBP(AGC
, 11), 0x00000404);
164 mt76_wr(dev
, MT_BBP(AGC
, 2), 0x00007070);
165 mt76_wr(dev
, MT_TXOP_CTRL_CFG
, 0X04101b3f);
167 mt76_set(dev
, MT_BBP(TXO
, 4), BIT(25));
168 mt76_set(dev
, MT_BBP(RXO
, 13), BIT(8));
173 mt76x2u_phy_channel_calibrate(dev
, true);
174 mt76x02_init_agc_gain(dev
);
176 if (mt76x2_tssi_enabled(dev
)) {
177 /* init default values for temp compensation */
178 mt76_rmw_field(dev
, MT_TX_ALC_CFG_1
, MT_TX_ALC_CFG_1_TEMP_COMP
,
180 mt76_rmw_field(dev
, MT_TX_ALC_CFG_2
, MT_TX_ALC_CFG_2_TEMP_COMP
,
183 /* init tssi calibration */
184 if (!mt76x2_channel_silent(dev
)) {
185 struct ieee80211_channel
*chan
;
188 chan
= dev
->mt76
.chandef
.chan
;
189 if (chan
->band
== NL80211_BAND_5GHZ
)
191 if (mt76x02_ext_pa_enabled(dev
, chan
->band
))
193 mt76x02_mcu_calibrate(dev
, MCU_CAL_TSSI
, flag
);
194 dev
->cal
.tssi_cal_done
= true;
198 ieee80211_queue_delayed_work(mt76_hw(dev
), &dev
->cal_work
,
199 MT_CALIBRATE_INTERVAL
);