1 // SPDX-License-Identifier: ISC 2 /* Copyright (C) 2020 MediaTek Inc. */ 3 4 #include <linux/etherdevice.h> 5 #include <linux/hwmon.h> 6 #include <linux/hwmon-sysfs.h> 7 #include <linux/thermal.h> 8 #include <linux/firmware.h> 9 #include "mt7921.h" 10 #include "../mt76_connac2_mac.h" 11 #include "mcu.h" 12 13 static ssize_t mt7921_thermal_temp_show(struct device *dev, 14 struct device_attribute *attr, 15 char *buf) 16 { 17 switch (to_sensor_dev_attr(attr)->index) { 18 case 0: { 19 struct mt792x_phy *phy = dev_get_drvdata(dev); 20 struct mt792x_dev *mdev = phy->dev; 21 int temperature; 22 23 mt792x_mutex_acquire(mdev); 24 temperature = mt7921_mcu_get_temperature(phy); 25 mt792x_mutex_release(mdev); 26 27 if (temperature < 0) 28 return temperature; 29 /* display in millidegree Celsius */ 30 return sprintf(buf, "%u\n", temperature * 1000); 31 } 32 default: 33 return -EINVAL; 34 } 35 } 36 static SENSOR_DEVICE_ATTR_RO(temp1_input, mt7921_thermal_temp, 0); 37 38 static struct attribute *mt7921_hwmon_attrs[] = { 39 &sensor_dev_attr_temp1_input.dev_attr.attr, 40 NULL, 41 }; 42 ATTRIBUTE_GROUPS(mt7921_hwmon); 43 44 static int mt7921_thermal_init(struct mt792x_phy *phy) 45 { 46 struct wiphy *wiphy = phy->mt76->hw->wiphy; 47 struct device *hwmon; 48 const char *name; 49 50 if (!IS_REACHABLE(CONFIG_HWMON)) 51 return 0; 52 53 name = devm_kasprintf(&wiphy->dev, GFP_KERNEL, "mt7921_%s", 54 wiphy_name(wiphy)); 55 56 hwmon = devm_hwmon_device_register_with_groups(&wiphy->dev, name, phy, 57 mt7921_hwmon_groups); 58 return PTR_ERR_OR_ZERO(hwmon); 59 } 60 61 static void 62 mt7921_regd_channel_update(struct wiphy *wiphy, struct mt792x_dev *dev) 63 { 64 #define IS_UNII_INVALID(idx, sfreq, efreq) \ 65 (!(dev->phy.clc_chan_conf & BIT(idx)) && (cfreq) >= (sfreq) && (cfreq) <= (efreq)) 66 struct ieee80211_supported_band *sband; 67 struct mt76_dev *mdev = &dev->mt76; 68 struct device_node *np, *band_np; 69 struct ieee80211_channel *ch; 70 int i, cfreq; 71 72 np = mt76_find_power_limits_node(mdev); 73 74 sband = wiphy->bands[NL80211_BAND_5GHZ]; 75 band_np = np ? of_get_child_by_name(np, "txpower-5g") : NULL; 76 for (i = 0; i < sband->n_channels; i++) { 77 ch = &sband->channels[i]; 78 cfreq = ch->center_freq; 79 80 if (np && (!band_np || !mt76_find_channel_node(band_np, ch))) { 81 ch->flags |= IEEE80211_CHAN_DISABLED; 82 continue; 83 } 84 85 /* UNII-4 */ 86 if (IS_UNII_INVALID(0, 5850, 5925)) 87 ch->flags |= IEEE80211_CHAN_DISABLED; 88 } 89 90 sband = wiphy->bands[NL80211_BAND_6GHZ]; 91 if (!sband) 92 return; 93 94 band_np = np ? of_get_child_by_name(np, "txpower-6g") : NULL; 95 for (i = 0; i < sband->n_channels; i++) { 96 ch = &sband->channels[i]; 97 cfreq = ch->center_freq; 98 99 if (np && (!band_np || !mt76_find_channel_node(band_np, ch))) { 100 ch->flags |= IEEE80211_CHAN_DISABLED; 101 continue; 102 } 103 104 /* UNII-5/6/7/8 */ 105 if (IS_UNII_INVALID(1, 5925, 6425) || 106 IS_UNII_INVALID(2, 6425, 6525) || 107 IS_UNII_INVALID(3, 6525, 6875) || 108 IS_UNII_INVALID(4, 6875, 7125)) 109 ch->flags |= IEEE80211_CHAN_DISABLED; 110 } 111 } 112 113 void mt7921_regd_update(struct mt792x_dev *dev) 114 { 115 struct mt76_dev *mdev = &dev->mt76; 116 struct ieee80211_hw *hw = mdev->hw; 117 struct wiphy *wiphy = hw->wiphy; 118 119 mt7921_mcu_set_clc(dev, mdev->alpha2, dev->country_ie_env); 120 mt7921_regd_channel_update(wiphy, dev); 121 mt76_connac_mcu_set_channel_domain(hw->priv); 122 mt7921_set_tx_sar_pwr(hw, NULL); 123 } 124 EXPORT_SYMBOL_GPL(mt7921_regd_update); 125 126 static void 127 mt7921_regd_notifier(struct wiphy *wiphy, 128 struct regulatory_request *request) 129 { 130 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy); 131 struct mt792x_dev *dev = mt792x_hw_dev(hw); 132 struct mt76_connac_pm *pm = &dev->pm; 133 134 memcpy(dev->mt76.alpha2, request->alpha2, sizeof(dev->mt76.alpha2)); 135 dev->mt76.region = request->dfs_region; 136 dev->country_ie_env = request->country_ie_env; 137 138 if (pm->suspended) 139 return; 140 141 dev->regd_in_progress = true; 142 143 mt792x_mutex_acquire(dev); 144 mt7921_regd_update(dev); 145 mt792x_mutex_release(dev); 146 147 dev->regd_in_progress = false; 148 wake_up(&dev->wait); 149 } 150 151 int mt7921_mac_init(struct mt792x_dev *dev) 152 { 153 int i; 154 155 mt76_rmw_field(dev, MT_MDP_DCR1, MT_MDP_DCR1_MAX_RX_LEN, 1536); 156 /* enable hardware de-agg */ 157 mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_DAMSDU_EN); 158 /* enable hardware rx header translation */ 159 mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_RX_HDR_TRANS_EN); 160 161 for (i = 0; i < MT792x_WTBL_SIZE; i++) 162 mt7921_mac_wtbl_update(dev, i, 163 MT_WTBL_UPDATE_ADM_COUNT_CLEAR); 164 for (i = 0; i < 2; i++) 165 mt792x_mac_init_band(dev, i); 166 167 return mt76_connac_mcu_set_rts_thresh(&dev->mt76, 0x92b, 0); 168 } 169 EXPORT_SYMBOL_GPL(mt7921_mac_init); 170 171 static int __mt7921_init_hardware(struct mt792x_dev *dev) 172 { 173 int ret; 174 175 /* force firmware operation mode into normal state, 176 * which should be set before firmware download stage. 177 */ 178 mt76_wr(dev, MT_SWDEF_MODE, MT_SWDEF_NORMAL_MODE); 179 ret = mt792x_mcu_init(dev); 180 if (ret) 181 goto out; 182 183 mt76_eeprom_override(&dev->mphy); 184 185 ret = mt7921_mcu_set_eeprom(dev); 186 if (ret) 187 goto out; 188 189 ret = mt7921_mac_init(dev); 190 out: 191 return ret; 192 } 193 194 static int mt7921_init_hardware(struct mt792x_dev *dev) 195 { 196 int ret, i; 197 198 set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state); 199 200 for (i = 0; i < MT792x_MCU_INIT_RETRY_COUNT; i++) { 201 ret = __mt7921_init_hardware(dev); 202 if (!ret) 203 break; 204 205 mt792x_init_reset(dev); 206 } 207 208 if (i == MT792x_MCU_INIT_RETRY_COUNT) { 209 dev_err(dev->mt76.dev, "hardware init failed\n"); 210 return ret; 211 } 212 213 return 0; 214 } 215 216 static void mt7921_init_work(struct work_struct *work) 217 { 218 struct mt792x_dev *dev = container_of(work, struct mt792x_dev, 219 init_work); 220 int ret; 221 222 ret = mt7921_init_hardware(dev); 223 if (ret) 224 return; 225 226 mt76_set_stream_caps(&dev->mphy, true); 227 mt7921_set_stream_he_caps(&dev->phy); 228 229 ret = mt76_register_device(&dev->mt76, true, mt76_rates, 230 ARRAY_SIZE(mt76_rates)); 231 if (ret) { 232 dev_err(dev->mt76.dev, "register device failed\n"); 233 return; 234 } 235 236 ret = mt7921_init_debugfs(dev); 237 if (ret) { 238 dev_err(dev->mt76.dev, "register debugfs failed\n"); 239 return; 240 } 241 242 ret = mt7921_thermal_init(&dev->phy); 243 if (ret) { 244 dev_err(dev->mt76.dev, "thermal init failed\n"); 245 return; 246 } 247 248 /* we support chip reset now */ 249 dev->hw_init_done = true; 250 251 mt76_connac_mcu_set_deep_sleep(&dev->mt76, dev->pm.ds_enable); 252 } 253 254 int mt7921_register_device(struct mt792x_dev *dev) 255 { 256 struct ieee80211_hw *hw = mt76_hw(dev); 257 int ret; 258 259 dev->phy.dev = dev; 260 dev->phy.mt76 = &dev->mt76.phy; 261 dev->mt76.phy.priv = &dev->phy; 262 dev->mt76.tx_worker.fn = mt792x_tx_worker; 263 264 INIT_DELAYED_WORK(&dev->pm.ps_work, mt792x_pm_power_save_work); 265 INIT_WORK(&dev->pm.wake_work, mt792x_pm_wake_work); 266 spin_lock_init(&dev->pm.wake.lock); 267 mutex_init(&dev->pm.mutex); 268 init_waitqueue_head(&dev->pm.wait); 269 init_waitqueue_head(&dev->wait); 270 if (mt76_is_sdio(&dev->mt76)) 271 init_waitqueue_head(&dev->mt76.sdio.wait); 272 spin_lock_init(&dev->pm.txq_lock); 273 INIT_DELAYED_WORK(&dev->mphy.mac_work, mt792x_mac_work); 274 INIT_DELAYED_WORK(&dev->phy.scan_work, mt7921_scan_work); 275 INIT_DELAYED_WORK(&dev->coredump.work, mt7921_coredump_work); 276 #if IS_ENABLED(CONFIG_IPV6) 277 INIT_WORK(&dev->ipv6_ns_work, mt7921_set_ipv6_ns_work); 278 skb_queue_head_init(&dev->ipv6_ns_list); 279 #endif 280 skb_queue_head_init(&dev->phy.scan_event_list); 281 skb_queue_head_init(&dev->coredump.msg_list); 282 283 INIT_WORK(&dev->reset_work, mt7921_mac_reset_work); 284 INIT_WORK(&dev->init_work, mt7921_init_work); 285 286 INIT_WORK(&dev->phy.roc_work, mt7921_roc_work); 287 timer_setup(&dev->phy.roc_timer, mt792x_roc_timer, 0); 288 init_waitqueue_head(&dev->phy.roc_wait); 289 290 dev->pm.idle_timeout = MT792x_PM_TIMEOUT; 291 dev->pm.stats.last_wake_event = jiffies; 292 dev->pm.stats.last_doze_event = jiffies; 293 if (!mt76_is_usb(&dev->mt76)) { 294 dev->pm.enable_user = true; 295 dev->pm.enable = true; 296 dev->pm.ds_enable_user = true; 297 dev->pm.ds_enable = true; 298 } 299 300 if (!mt76_is_mmio(&dev->mt76)) 301 hw->extra_tx_headroom += MT_SDIO_TXD_SIZE + MT_SDIO_HDR_SIZE; 302 303 mt792x_init_acpi_sar(dev); 304 305 ret = mt792x_init_wcid(dev); 306 if (ret) 307 return ret; 308 309 ret = mt792x_init_wiphy(hw); 310 if (ret) 311 return ret; 312 313 hw->wiphy->reg_notifier = mt7921_regd_notifier; 314 dev->mphy.sband_2g.sband.ht_cap.cap |= 315 IEEE80211_HT_CAP_LDPC_CODING | 316 IEEE80211_HT_CAP_MAX_AMSDU; 317 dev->mphy.sband_5g.sband.ht_cap.cap |= 318 IEEE80211_HT_CAP_LDPC_CODING | 319 IEEE80211_HT_CAP_MAX_AMSDU; 320 dev->mphy.sband_5g.sband.vht_cap.cap |= 321 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 | 322 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK | 323 IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE | 324 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE | 325 (3 << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT); 326 if (is_mt7922(&dev->mt76)) 327 dev->mphy.sband_5g.sband.vht_cap.cap |= 328 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ | 329 IEEE80211_VHT_CAP_SHORT_GI_160; 330 331 dev->mphy.hw->wiphy->available_antennas_rx = dev->mphy.chainmask; 332 dev->mphy.hw->wiphy->available_antennas_tx = dev->mphy.chainmask; 333 334 queue_work(system_wq, &dev->init_work); 335 336 return 0; 337 } 338 EXPORT_SYMBOL_GPL(mt7921_register_device); 339