2 * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include "mt76x2_eeprom.h"
20 void mt76x2u_phy_set_rxpath(struct mt76x2_dev *dev)
24 val = mt76_rr(dev, MT_BBP(AGC, 0));
27 switch (dev->chainmask & 0xf) {
35 mt76_wr(dev, MT_BBP(AGC, 0), val);
38 void mt76x2u_phy_set_txdac(struct mt76x2_dev *dev)
42 txpath = (dev->chainmask >> 8) & 0xf;
45 mt76_set(dev, MT_BBP(TXBE, 5), 0x3);
48 mt76_clear(dev, MT_BBP(TXBE, 5), 0x3);
53 void mt76x2u_phy_channel_calibrate(struct mt76x2_dev *dev)
55 struct ieee80211_channel *chan = dev->mt76.chandef.chan;
56 bool is_5ghz = chan->band == NL80211_BAND_5GHZ;
58 if (mt76x2_channel_silent(dev))
61 mt76x2u_mac_stop(dev);
64 mt76x2u_mcu_calibrate(dev, MCU_CAL_LC, 0);
66 mt76x2u_mcu_calibrate(dev, MCU_CAL_TX_LOFT, is_5ghz);
67 mt76x2u_mcu_calibrate(dev, MCU_CAL_TXIQ, is_5ghz);
68 mt76x2u_mcu_calibrate(dev, MCU_CAL_RXIQC_FI, is_5ghz);
69 mt76x2u_mcu_calibrate(dev, MCU_CAL_TEMP_SENSOR, 0);
71 mt76x2u_mac_resume(dev);
75 mt76x2u_phy_tssi_compensate(struct mt76x2_dev *dev)
77 struct ieee80211_channel *chan = dev->mt76.chandef.chan;
78 struct mt76x2_tx_power_info txp;
79 struct mt76x2_tssi_comp t = {};
81 if (!dev->cal.tssi_cal_done)
84 if (!dev->cal.tssi_comp_pending) {
87 mt76x2u_mcu_tssi_comp(dev, &t);
88 dev->cal.tssi_comp_pending = true;
90 if (mt76_rr(dev, MT_BBP(CORE, 34)) & BIT(4))
93 dev->cal.tssi_comp_pending = false;
94 mt76x2_get_power_info(dev, &txp, chan);
96 if (mt76x2_ext_pa_enabled(dev, chan->band))
100 t.slope0 = txp.chain[0].tssi_slope;
101 t.offset0 = txp.chain[0].tssi_offset;
102 t.slope1 = txp.chain[1].tssi_slope;
103 t.offset1 = txp.chain[1].tssi_offset;
104 mt76x2u_mcu_tssi_comp(dev, &t);
106 if (t.pa_mode || dev->cal.dpd_cal_done)
109 usleep_range(10000, 20000);
110 mt76x2u_mcu_calibrate(dev, MCU_CAL_DPD, chan->hw_value);
111 dev->cal.dpd_cal_done = true;
116 mt76x2u_phy_update_channel_gain(struct mt76x2_dev *dev)
118 u8 channel = dev->mt76.chandef.chan->hw_value;
122 freq = dev->mt76.chandef.chan->center_freq;
123 freq1 = dev->mt76.chandef.center_freq1;
125 switch (dev->mt76.chandef.width) {
126 case NL80211_CHAN_WIDTH_80: {
129 ch_group_index = (freq - freq1 + 30) / 20;
130 if (WARN_ON(ch_group_index < 0 || ch_group_index > 3))
132 channel += 6 - ch_group_index * 4;
135 case NL80211_CHAN_WIDTH_40:
145 dev->cal.avg_rssi_all = mt76x2_phy_get_min_avg_rssi(dev);
146 false_cca = FIELD_GET(MT_RX_STAT_1_CCA_ERRORS,
147 mt76_rr(dev, MT_RX_STAT_1));
149 mt76x2u_mcu_set_dynamic_vga(dev, channel, false, false,
150 dev->cal.avg_rssi_all, false_cca);
153 void mt76x2u_phy_calibrate(struct work_struct *work)
155 struct mt76x2_dev *dev;
157 dev = container_of(work, struct mt76x2_dev, cal_work.work);
158 mt76x2u_phy_tssi_compensate(dev);
159 mt76x2u_phy_update_channel_gain(dev);
161 ieee80211_queue_delayed_work(mt76_hw(dev), &dev->cal_work,
162 MT_CALIBRATE_INTERVAL);
165 int mt76x2u_phy_set_channel(struct mt76x2_dev *dev,
166 struct cfg80211_chan_def *chandef)
168 u32 ext_cca_chan[4] = {
169 [0] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0, 0) |
170 FIELD_PREP(MT_EXT_CCA_CFG_CCA1, 1) |
171 FIELD_PREP(MT_EXT_CCA_CFG_CCA2, 2) |
172 FIELD_PREP(MT_EXT_CCA_CFG_CCA3, 3) |
173 FIELD_PREP(MT_EXT_CCA_CFG_CCA_MASK, BIT(0)),
174 [1] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0, 1) |
175 FIELD_PREP(MT_EXT_CCA_CFG_CCA1, 0) |
176 FIELD_PREP(MT_EXT_CCA_CFG_CCA2, 2) |
177 FIELD_PREP(MT_EXT_CCA_CFG_CCA3, 3) |
178 FIELD_PREP(MT_EXT_CCA_CFG_CCA_MASK, BIT(1)),
179 [2] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0, 2) |
180 FIELD_PREP(MT_EXT_CCA_CFG_CCA1, 3) |
181 FIELD_PREP(MT_EXT_CCA_CFG_CCA2, 1) |
182 FIELD_PREP(MT_EXT_CCA_CFG_CCA3, 0) |
183 FIELD_PREP(MT_EXT_CCA_CFG_CCA_MASK, BIT(2)),
184 [3] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0, 3) |
185 FIELD_PREP(MT_EXT_CCA_CFG_CCA1, 2) |
186 FIELD_PREP(MT_EXT_CCA_CFG_CCA2, 1) |
187 FIELD_PREP(MT_EXT_CCA_CFG_CCA3, 0) |
188 FIELD_PREP(MT_EXT_CCA_CFG_CCA_MASK, BIT(3)),
190 bool scan = test_bit(MT76_SCANNING, &dev->mt76.state);
191 struct ieee80211_channel *chan = chandef->chan;
192 u8 channel = chan->hw_value, bw, bw_index;
193 int ch_group_index, freq, freq1, ret;
195 dev->cal.channel_cal_done = false;
196 freq = chandef->chan->center_freq;
197 freq1 = chandef->center_freq1;
199 switch (chandef->width) {
200 case NL80211_CHAN_WIDTH_40:
209 channel += 2 - ch_group_index * 4;
211 case NL80211_CHAN_WIDTH_80:
212 ch_group_index = (freq - freq1 + 30) / 20;
213 if (WARN_ON(ch_group_index < 0 || ch_group_index > 3))
216 bw_index = ch_group_index;
217 channel += 6 - ch_group_index * 4;
226 mt76x2_read_rx_gain(dev);
227 mt76x2_phy_set_txpower_regs(dev, chan->band);
228 mt76x2_configure_tx_delay(dev, chan->band, bw);
229 mt76x2_phy_set_txpower(dev);
231 mt76x2_phy_set_band(dev, chan->band, ch_group_index & 1);
232 mt76x2_phy_set_bw(dev, chandef->width, ch_group_index);
234 mt76_rmw(dev, MT_EXT_CCA_CFG,
235 (MT_EXT_CCA_CFG_CCA0 |
236 MT_EXT_CCA_CFG_CCA1 |
237 MT_EXT_CCA_CFG_CCA2 |
238 MT_EXT_CCA_CFG_CCA3 |
239 MT_EXT_CCA_CFG_CCA_MASK),
240 ext_cca_chan[ch_group_index]);
242 ret = mt76x2u_mcu_set_channel(dev, channel, bw, bw_index, scan);
246 mt76x2u_mcu_init_gain(dev, channel, dev->cal.rx.mcu_gain, true);
249 if (mt76xx_rev(dev) >= MT76XX_REV_E3)
250 mt76_set(dev, MT_BBP(RXO, 13), BIT(10));
252 if (!dev->cal.init_cal_done) {
253 u8 val = mt76x2_eeprom_get(dev, MT_EE_BT_RCAL_RESULT);
256 mt76x2u_mcu_calibrate(dev, MCU_CAL_R, 0);
259 mt76x2u_mcu_calibrate(dev, MCU_CAL_RXDCOC, channel);
261 /* Rx LPF calibration */
262 if (!dev->cal.init_cal_done)
263 mt76x2u_mcu_calibrate(dev, MCU_CAL_RC, 0);
264 dev->cal.init_cal_done = true;
266 mt76_wr(dev, MT_BBP(AGC, 61), 0xff64a4e2);
267 mt76_wr(dev, MT_BBP(AGC, 7), 0x08081010);
268 mt76_wr(dev, MT_BBP(AGC, 11), 0x00000404);
269 mt76_wr(dev, MT_BBP(AGC, 2), 0x00007070);
270 mt76_wr(dev, MT_TXOP_CTRL_CFG, 0X04101b3f);
272 mt76_set(dev, MT_BBP(TXO, 4), BIT(25));
273 mt76_set(dev, MT_BBP(RXO, 13), BIT(8));
278 if (mt76x2_tssi_enabled(dev)) {
279 /* init default values for temp compensation */
280 mt76_rmw_field(dev, MT_TX_ALC_CFG_1, MT_TX_ALC_CFG_1_TEMP_COMP,
282 mt76_rmw_field(dev, MT_TX_ALC_CFG_2, MT_TX_ALC_CFG_2_TEMP_COMP,
285 /* init tssi calibration */
286 if (!mt76x2_channel_silent(dev)) {
287 struct ieee80211_channel *chan;
290 chan = dev->mt76.chandef.chan;
291 if (chan->band == NL80211_BAND_5GHZ)
293 if (mt76x2_ext_pa_enabled(dev, chan->band))
295 mt76x2u_mcu_calibrate(dev, MCU_CAL_TSSI, flag);
296 dev->cal.tssi_cal_done = true;
300 ieee80211_queue_delayed_work(mt76_hw(dev), &dev->cal_work,
301 MT_CALIBRATE_INTERVAL);