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