1 // SPDX-License-Identifier: ISC 2 /* Copyright (C) 2023 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 "mt7925.h" 10 #include "mac.h" 11 #include "mcu.h" 12 13 static ssize_t mt7925_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 = mt7925_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, mt7925_thermal_temp, 0); 37 38 static struct attribute *mt7925_hwmon_attrs[] = { 39 &sensor_dev_attr_temp1_input.dev_attr.attr, 40 NULL, 41 }; 42 ATTRIBUTE_GROUPS(mt7925_hwmon); 43 44 static int mt7925_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, "mt7925_%s", 54 wiphy_name(wiphy)); 55 if (!name) 56 return -ENOMEM; 57 58 hwmon = devm_hwmon_device_register_with_groups(&wiphy->dev, name, phy, 59 mt7925_hwmon_groups); 60 return PTR_ERR_OR_ZERO(hwmon); 61 } 62 63 void mt7925_regd_be_ctrl(struct mt792x_dev *dev, u8 *alpha2) 64 { 65 struct mt792x_phy *phy = &dev->phy; 66 struct mt7925_clc_rule_v2 *rule; 67 struct mt7925_clc *clc; 68 bool old = dev->has_eht, new = true; 69 u32 mtcl_conf = mt792x_acpi_get_mtcl_conf(&dev->phy, alpha2); 70 u8 *pos; 71 72 if (mtcl_conf != MT792X_ACPI_MTCL_INVALID && 73 (((mtcl_conf >> 4) & 0x3) == 0)) { 74 new = false; 75 goto out; 76 } 77 78 if (!phy->clc[MT792x_CLC_BE_CTRL]) 79 goto out; 80 81 clc = (struct mt7925_clc *)phy->clc[MT792x_CLC_BE_CTRL]; 82 pos = clc->data; 83 84 while (1) { 85 rule = (struct mt7925_clc_rule_v2 *)pos; 86 87 if (rule->alpha2[0] == alpha2[0] && 88 rule->alpha2[1] == alpha2[1]) { 89 new = false; 90 break; 91 } 92 93 /* Check the last one */ 94 if (rule->flag & BIT(0)) 95 break; 96 97 pos += sizeof(*rule); 98 } 99 100 out: 101 if (old == new) 102 return; 103 104 dev->has_eht = new; 105 mt7925_set_stream_he_eht_caps(phy); 106 } 107 108 static void 109 mt7925_regd_channel_update(struct wiphy *wiphy, struct mt792x_dev *dev) 110 { 111 #define IS_UNII_INVALID(idx, sfreq, efreq, cfreq) \ 112 (!(dev->phy.clc_chan_conf & BIT(idx)) && (cfreq) >= (sfreq) && (cfreq) <= (efreq)) 113 #define MT7925_UNII_59G_IS_VALID 0x1 114 #define MT7925_UNII_6G_IS_VALID 0x1e 115 struct ieee80211_supported_band *sband; 116 struct mt76_dev *mdev = &dev->mt76; 117 struct ieee80211_channel *ch; 118 u32 mtcl_conf = mt792x_acpi_get_mtcl_conf(&dev->phy, mdev->alpha2); 119 int i; 120 121 if (mtcl_conf != MT792X_ACPI_MTCL_INVALID) { 122 if ((mtcl_conf & 0x3) == 0) 123 dev->phy.clc_chan_conf &= ~MT7925_UNII_59G_IS_VALID; 124 if (((mtcl_conf >> 2) & 0x3) == 0) 125 dev->phy.clc_chan_conf &= ~MT7925_UNII_6G_IS_VALID; 126 } 127 128 sband = wiphy->bands[NL80211_BAND_5GHZ]; 129 if (!sband) 130 return; 131 132 for (i = 0; i < sband->n_channels; i++) { 133 ch = &sband->channels[i]; 134 135 /* UNII-4 */ 136 if (IS_UNII_INVALID(0, 5845, 5925, ch->center_freq)) 137 ch->flags |= IEEE80211_CHAN_DISABLED; 138 } 139 140 sband = wiphy->bands[NL80211_BAND_6GHZ]; 141 if (!sband) 142 return; 143 144 for (i = 0; i < sband->n_channels; i++) { 145 ch = &sband->channels[i]; 146 147 /* UNII-5/6/7/8 */ 148 if (IS_UNII_INVALID(1, 5925, 6425, ch->center_freq) || 149 IS_UNII_INVALID(2, 6425, 6525, ch->center_freq) || 150 IS_UNII_INVALID(3, 6525, 6875, ch->center_freq) || 151 IS_UNII_INVALID(4, 6875, 7125, ch->center_freq)) 152 ch->flags |= IEEE80211_CHAN_DISABLED; 153 } 154 } 155 156 void mt7925_regd_update(struct mt792x_dev *dev) 157 { 158 struct mt76_dev *mdev = &dev->mt76; 159 struct ieee80211_hw *hw = mdev->hw; 160 struct wiphy *wiphy = hw->wiphy; 161 162 if (!dev->regd_change) 163 return; 164 165 mt7925_mcu_set_clc(dev, mdev->alpha2, dev->country_ie_env); 166 mt7925_regd_channel_update(wiphy, dev); 167 mt7925_mcu_set_channel_domain(hw->priv); 168 mt7925_set_tx_sar_pwr(hw, NULL); 169 dev->regd_change = false; 170 } 171 EXPORT_SYMBOL_GPL(mt7925_regd_update); 172 173 static void 174 mt7925_regd_notifier(struct wiphy *wiphy, 175 struct regulatory_request *req) 176 { 177 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy); 178 struct mt792x_dev *dev = mt792x_hw_dev(hw); 179 struct mt76_dev *mdev = &dev->mt76; 180 struct mt76_connac_pm *pm = &dev->pm; 181 182 /* allow world regdom at the first boot only */ 183 if (!memcmp(req->alpha2, "00", 2) && 184 mdev->alpha2[0] && mdev->alpha2[1]) 185 return; 186 187 /* do not need to update the same country twice */ 188 if (!memcmp(req->alpha2, mdev->alpha2, 2) && 189 dev->country_ie_env == req->country_ie_env) 190 return; 191 192 memcpy(mdev->alpha2, req->alpha2, 2); 193 mdev->region = req->dfs_region; 194 dev->country_ie_env = req->country_ie_env; 195 dev->regd_change = true; 196 197 if (pm->suspended) 198 return; 199 200 dev->regd_in_progress = true; 201 mt792x_mutex_acquire(dev); 202 mt7925_regd_update(dev); 203 mt792x_mutex_release(dev); 204 dev->regd_in_progress = false; 205 wake_up(&dev->wait); 206 } 207 208 static void mt7925_mac_init_basic_rates(struct mt792x_dev *dev) 209 { 210 int i; 211 212 for (i = 0; i < ARRAY_SIZE(mt76_rates); i++) { 213 u16 rate = mt76_rates[i].hw_value; 214 u16 idx = MT792x_BASIC_RATES_TBL + i; 215 216 rate = FIELD_PREP(MT_TX_RATE_MODE, rate >> 8) | 217 FIELD_PREP(MT_TX_RATE_IDX, rate & GENMASK(7, 0)); 218 mt7925_mac_set_fixed_rate_table(dev, idx, rate); 219 } 220 } 221 222 int mt7925_mac_init(struct mt792x_dev *dev) 223 { 224 int i; 225 226 mt76_rmw_field(dev, MT_MDP_DCR1, MT_MDP_DCR1_MAX_RX_LEN, 1536); 227 /* enable hardware de-agg */ 228 mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_DAMSDU_EN); 229 230 for (i = 0; i < MT792x_WTBL_SIZE; i++) 231 mt7925_mac_wtbl_update(dev, i, 232 MT_WTBL_UPDATE_ADM_COUNT_CLEAR); 233 for (i = 0; i < 2; i++) 234 mt792x_mac_init_band(dev, i); 235 236 mt7925_mac_init_basic_rates(dev); 237 238 memzero_explicit(&dev->mt76.alpha2, sizeof(dev->mt76.alpha2)); 239 240 return 0; 241 } 242 EXPORT_SYMBOL_GPL(mt7925_mac_init); 243 244 static int __mt7925_init_hardware(struct mt792x_dev *dev) 245 { 246 int ret; 247 248 ret = mt792x_mcu_init(dev); 249 if (ret) 250 goto out; 251 252 ret = mt76_eeprom_override(&dev->mphy); 253 if (ret) 254 goto out; 255 256 ret = mt7925_mcu_set_eeprom(dev); 257 if (ret) 258 goto out; 259 260 ret = mt7925_mac_init(dev); 261 if (ret) 262 goto out; 263 264 out: 265 return ret; 266 } 267 268 static int mt7925_init_hardware(struct mt792x_dev *dev) 269 { 270 int ret, i; 271 272 set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state); 273 274 for (i = 0; i < MT792x_MCU_INIT_RETRY_COUNT; i++) { 275 ret = __mt7925_init_hardware(dev); 276 if (!ret) 277 break; 278 279 mt792x_init_reset(dev); 280 } 281 282 if (i == MT792x_MCU_INIT_RETRY_COUNT) { 283 dev_err(dev->mt76.dev, "hardware init failed\n"); 284 return ret; 285 } 286 287 return 0; 288 } 289 290 static void mt7925_init_work(struct work_struct *work) 291 { 292 struct mt792x_dev *dev = container_of(work, struct mt792x_dev, 293 init_work); 294 int ret; 295 296 ret = mt7925_init_hardware(dev); 297 if (ret) 298 return; 299 300 mt76_set_stream_caps(&dev->mphy, true); 301 mt7925_set_stream_he_eht_caps(&dev->phy); 302 mt792x_config_mac_addr_list(dev); 303 304 ret = mt7925_init_mlo_caps(&dev->phy); 305 if (ret) { 306 dev_err(dev->mt76.dev, "MLO init failed\n"); 307 return; 308 } 309 310 ret = mt76_register_device(&dev->mt76, true, mt76_rates, 311 ARRAY_SIZE(mt76_rates)); 312 if (ret) { 313 dev_err(dev->mt76.dev, "register device failed\n"); 314 return; 315 } 316 317 ret = mt7925_init_debugfs(dev); 318 if (ret) { 319 dev_err(dev->mt76.dev, "register debugfs failed\n"); 320 return; 321 } 322 323 ret = mt7925_thermal_init(&dev->phy); 324 if (ret) { 325 dev_err(dev->mt76.dev, "thermal init failed\n"); 326 return; 327 } 328 329 ret = mt7925_mcu_set_thermal_protect(dev); 330 if (ret) { 331 dev_err(dev->mt76.dev, "thermal protection enable failed\n"); 332 return; 333 } 334 335 /* we support chip reset now */ 336 dev->hw_init_done = true; 337 338 mt7925_mcu_set_deep_sleep(dev, dev->pm.ds_enable); 339 } 340 341 int mt7925_register_device(struct mt792x_dev *dev) 342 { 343 struct ieee80211_hw *hw = mt76_hw(dev); 344 int ret; 345 346 dev->phy.dev = dev; 347 dev->phy.mt76 = &dev->mt76.phy; 348 dev->mt76.phy.priv = &dev->phy; 349 dev->mt76.tx_worker.fn = mt792x_tx_worker; 350 351 INIT_DELAYED_WORK(&dev->pm.ps_work, mt792x_pm_power_save_work); 352 INIT_DELAYED_WORK(&dev->mlo_pm_work, mt7925_mlo_pm_work); 353 INIT_WORK(&dev->pm.wake_work, mt792x_pm_wake_work); 354 spin_lock_init(&dev->pm.wake.lock); 355 mutex_init(&dev->pm.mutex); 356 init_waitqueue_head(&dev->pm.wait); 357 init_waitqueue_head(&dev->wait); 358 spin_lock_init(&dev->pm.txq_lock); 359 INIT_DELAYED_WORK(&dev->mphy.mac_work, mt792x_mac_work); 360 INIT_DELAYED_WORK(&dev->phy.scan_work, mt7925_scan_work); 361 INIT_DELAYED_WORK(&dev->coredump.work, mt7925_coredump_work); 362 #if IS_ENABLED(CONFIG_IPV6) 363 INIT_WORK(&dev->ipv6_ns_work, mt7925_set_ipv6_ns_work); 364 skb_queue_head_init(&dev->ipv6_ns_list); 365 #endif 366 skb_queue_head_init(&dev->phy.scan_event_list); 367 skb_queue_head_init(&dev->coredump.msg_list); 368 369 INIT_WORK(&dev->reset_work, mt7925_mac_reset_work); 370 INIT_WORK(&dev->init_work, mt7925_init_work); 371 372 INIT_WORK(&dev->phy.roc_work, mt7925_roc_work); 373 timer_setup(&dev->phy.roc_timer, mt792x_roc_timer, 0); 374 init_waitqueue_head(&dev->phy.roc_wait); 375 376 dev->pm.idle_timeout = MT792x_PM_TIMEOUT; 377 dev->pm.stats.last_wake_event = jiffies; 378 dev->pm.stats.last_doze_event = jiffies; 379 if (!mt76_is_usb(&dev->mt76)) { 380 dev->pm.enable_user = true; 381 dev->pm.enable = true; 382 dev->pm.ds_enable_user = true; 383 dev->pm.ds_enable = true; 384 } 385 386 if (!mt76_is_mmio(&dev->mt76)) 387 hw->extra_tx_headroom += MT_SDIO_TXD_SIZE + MT_SDIO_HDR_SIZE; 388 389 mt792x_init_acpi_sar(dev); 390 391 ret = mt792x_init_wcid(dev); 392 if (ret) 393 return ret; 394 395 ret = mt792x_init_wiphy(hw); 396 if (ret) 397 return ret; 398 399 hw->wiphy->reg_notifier = mt7925_regd_notifier; 400 dev->mphy.sband_2g.sband.ht_cap.cap |= 401 IEEE80211_HT_CAP_LDPC_CODING | 402 IEEE80211_HT_CAP_MAX_AMSDU; 403 dev->mphy.sband_2g.sband.ht_cap.ampdu_density = 404 IEEE80211_HT_MPDU_DENSITY_2; 405 dev->mphy.sband_5g.sband.ht_cap.cap |= 406 IEEE80211_HT_CAP_LDPC_CODING | 407 IEEE80211_HT_CAP_MAX_AMSDU; 408 dev->mphy.sband_2g.sband.ht_cap.ampdu_density = 409 IEEE80211_HT_MPDU_DENSITY_1; 410 dev->mphy.sband_5g.sband.vht_cap.cap |= 411 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 | 412 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK | 413 IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE | 414 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE | 415 (3 << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT); 416 dev->mphy.sband_5g.sband.vht_cap.cap |= 417 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ | 418 IEEE80211_VHT_CAP_SHORT_GI_160; 419 420 dev->mphy.hw->wiphy->available_antennas_rx = dev->mphy.chainmask; 421 dev->mphy.hw->wiphy->available_antennas_tx = dev->mphy.chainmask; 422 423 queue_work(system_wq, &dev->init_work); 424 425 return 0; 426 } 427 EXPORT_SYMBOL_GPL(mt7925_register_device); 428