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 #if defined(__linux__) 14 static ssize_t mt7921_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 = mt7921_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, mt7921_thermal_temp, 0); 38 39 static struct attribute *mt7921_hwmon_attrs[] = { 40 &sensor_dev_attr_temp1_input.dev_attr.attr, 41 NULL, 42 }; 43 ATTRIBUTE_GROUPS(mt7921_hwmon); 44 45 static int mt7921_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, "mt7921_%s", 55 wiphy_name(wiphy)); 56 57 hwmon = devm_hwmon_device_register_with_groups(&wiphy->dev, name, phy, 58 mt7921_hwmon_groups); 59 if (IS_ERR(hwmon)) 60 return PTR_ERR(hwmon); 61 62 return 0; 63 } 64 #endif 65 66 static void 67 mt7921_regd_notifier(struct wiphy *wiphy, 68 struct regulatory_request *request) 69 { 70 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy); 71 struct mt792x_dev *dev = mt792x_hw_dev(hw); 72 73 memcpy(dev->mt76.alpha2, request->alpha2, sizeof(dev->mt76.alpha2)); 74 dev->mt76.region = request->dfs_region; 75 dev->country_ie_env = request->country_ie_env; 76 77 mt792x_mutex_acquire(dev); 78 mt7921_mcu_set_clc(dev, request->alpha2, request->country_ie_env); 79 mt76_connac_mcu_set_channel_domain(hw->priv); 80 mt7921_set_tx_sar_pwr(hw, NULL); 81 mt792x_mutex_release(dev); 82 } 83 84 int mt7921_mac_init(struct mt792x_dev *dev) 85 { 86 int i; 87 88 mt76_rmw_field(dev, MT_MDP_DCR1, MT_MDP_DCR1_MAX_RX_LEN, 1536); 89 /* enable hardware de-agg */ 90 mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_DAMSDU_EN); 91 /* enable hardware rx header translation */ 92 mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_RX_HDR_TRANS_EN); 93 94 for (i = 0; i < MT792x_WTBL_SIZE; i++) 95 mt7921_mac_wtbl_update(dev, i, 96 MT_WTBL_UPDATE_ADM_COUNT_CLEAR); 97 for (i = 0; i < 2; i++) 98 mt792x_mac_init_band(dev, i); 99 100 return mt76_connac_mcu_set_rts_thresh(&dev->mt76, 0x92b, 0); 101 } 102 EXPORT_SYMBOL_GPL(mt7921_mac_init); 103 104 static int __mt7921_init_hardware(struct mt792x_dev *dev) 105 { 106 int ret; 107 108 /* force firmware operation mode into normal state, 109 * which should be set before firmware download stage. 110 */ 111 mt76_wr(dev, MT_SWDEF_MODE, MT_SWDEF_NORMAL_MODE); 112 ret = mt792x_mcu_init(dev); 113 if (ret) 114 goto out; 115 116 mt76_eeprom_override(&dev->mphy); 117 118 ret = mt7921_mcu_set_eeprom(dev); 119 if (ret) 120 goto out; 121 122 ret = mt7921_mac_init(dev); 123 out: 124 return ret; 125 } 126 127 static int mt7921_init_hardware(struct mt792x_dev *dev) 128 { 129 int ret, i; 130 131 set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state); 132 133 for (i = 0; i < MT792x_MCU_INIT_RETRY_COUNT; i++) { 134 ret = __mt7921_init_hardware(dev); 135 if (!ret) 136 break; 137 138 mt792x_init_reset(dev); 139 } 140 141 if (i == MT792x_MCU_INIT_RETRY_COUNT) { 142 dev_err(dev->mt76.dev, "hardware init failed\n"); 143 return ret; 144 } 145 146 return 0; 147 } 148 149 static void mt7921_init_work(struct work_struct *work) 150 { 151 struct mt792x_dev *dev = container_of(work, struct mt792x_dev, 152 init_work); 153 int ret; 154 155 ret = mt7921_init_hardware(dev); 156 if (ret) 157 return; 158 159 mt76_set_stream_caps(&dev->mphy, true); 160 mt7921_set_stream_he_caps(&dev->phy); 161 162 ret = mt76_register_device(&dev->mt76, true, mt76_rates, 163 ARRAY_SIZE(mt76_rates)); 164 if (ret) { 165 dev_err(dev->mt76.dev, "register device failed\n"); 166 return; 167 } 168 169 #if !defined(__FreeBSD__) || defined(CONFIG_MT7921_DEBUGFS) 170 ret = mt7921_init_debugfs(dev); 171 if (ret) { 172 dev_err(dev->mt76.dev, "register debugfs failed\n"); 173 return; 174 } 175 #endif 176 177 #if defined(__linux__) 178 ret = mt7921_thermal_init(&dev->phy); 179 if (ret) { 180 dev_err(dev->mt76.dev, "thermal init failed\n"); 181 return; 182 } 183 #endif 184 185 /* we support chip reset now */ 186 dev->hw_init_done = true; 187 188 mt76_connac_mcu_set_deep_sleep(&dev->mt76, dev->pm.ds_enable); 189 } 190 191 int mt7921_register_device(struct mt792x_dev *dev) 192 { 193 struct ieee80211_hw *hw = mt76_hw(dev); 194 int ret; 195 196 dev->phy.dev = dev; 197 dev->phy.mt76 = &dev->mt76.phy; 198 dev->mt76.phy.priv = &dev->phy; 199 dev->mt76.tx_worker.fn = mt792x_tx_worker; 200 201 INIT_DELAYED_WORK(&dev->pm.ps_work, mt792x_pm_power_save_work); 202 INIT_WORK(&dev->pm.wake_work, mt792x_pm_wake_work); 203 spin_lock_init(&dev->pm.wake.lock); 204 mutex_init(&dev->pm.mutex); 205 init_waitqueue_head(&dev->pm.wait); 206 if (mt76_is_sdio(&dev->mt76)) 207 init_waitqueue_head(&dev->mt76.sdio.wait); 208 spin_lock_init(&dev->pm.txq_lock); 209 INIT_DELAYED_WORK(&dev->mphy.mac_work, mt792x_mac_work); 210 INIT_DELAYED_WORK(&dev->phy.scan_work, mt7921_scan_work); 211 INIT_DELAYED_WORK(&dev->coredump.work, mt7921_coredump_work); 212 #if IS_ENABLED(CONFIG_IPV6) 213 INIT_WORK(&dev->ipv6_ns_work, mt7921_set_ipv6_ns_work); 214 skb_queue_head_init(&dev->ipv6_ns_list); 215 #endif 216 skb_queue_head_init(&dev->phy.scan_event_list); 217 skb_queue_head_init(&dev->coredump.msg_list); 218 219 INIT_WORK(&dev->reset_work, mt7921_mac_reset_work); 220 INIT_WORK(&dev->init_work, mt7921_init_work); 221 222 INIT_WORK(&dev->phy.roc_work, mt7921_roc_work); 223 timer_setup(&dev->phy.roc_timer, mt792x_roc_timer, 0); 224 init_waitqueue_head(&dev->phy.roc_wait); 225 226 dev->pm.idle_timeout = MT792x_PM_TIMEOUT; 227 dev->pm.stats.last_wake_event = jiffies; 228 dev->pm.stats.last_doze_event = jiffies; 229 if (!mt76_is_usb(&dev->mt76)) { 230 dev->pm.enable_user = true; 231 dev->pm.enable = true; 232 dev->pm.ds_enable_user = true; 233 dev->pm.ds_enable = true; 234 } 235 236 if (!mt76_is_mmio(&dev->mt76)) 237 hw->extra_tx_headroom += MT_SDIO_TXD_SIZE + MT_SDIO_HDR_SIZE; 238 239 mt792x_init_acpi_sar(dev); 240 241 ret = mt792x_init_wcid(dev); 242 if (ret) 243 return ret; 244 245 ret = mt792x_init_wiphy(hw); 246 if (ret) 247 return ret; 248 249 hw->wiphy->reg_notifier = mt7921_regd_notifier; 250 dev->mphy.sband_2g.sband.ht_cap.cap |= 251 IEEE80211_HT_CAP_LDPC_CODING | 252 IEEE80211_HT_CAP_MAX_AMSDU; 253 dev->mphy.sband_5g.sband.ht_cap.cap |= 254 IEEE80211_HT_CAP_LDPC_CODING | 255 IEEE80211_HT_CAP_MAX_AMSDU; 256 dev->mphy.sband_5g.sband.vht_cap.cap |= 257 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 | 258 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK | 259 IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE | 260 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE | 261 (3 << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT); 262 if (is_mt7922(&dev->mt76)) 263 dev->mphy.sband_5g.sband.vht_cap.cap |= 264 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ | 265 IEEE80211_VHT_CAP_SHORT_GI_160; 266 267 dev->mphy.hw->wiphy->available_antennas_rx = dev->mphy.chainmask; 268 dev->mphy.hw->wiphy->available_antennas_tx = dev->mphy.chainmask; 269 270 queue_work(system_wq, &dev->init_work); 271 272 return 0; 273 } 274 EXPORT_SYMBOL_GPL(mt7921_register_device); 275