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, fmt: "%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(dev: &wiphy->dev, GFP_KERNEL, fmt: "mt7925_%s" , |
54 | wiphy_name(wiphy)); |
55 | |
56 | hwmon = devm_hwmon_device_register_with_groups(dev: &wiphy->dev, name, drvdata: phy, |
57 | groups: mt7925_hwmon_groups); |
58 | return PTR_ERR_OR_ZERO(ptr: hwmon); |
59 | } |
60 | static void |
61 | mt7925_regd_notifier(struct wiphy *wiphy, |
62 | struct regulatory_request *req) |
63 | { |
64 | struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy); |
65 | struct mt792x_dev *dev = mt792x_hw_dev(hw); |
66 | struct mt76_dev *mdev = &dev->mt76; |
67 | |
68 | /* allow world regdom at the first boot only */ |
69 | if (!memcmp(p: req->alpha2, q: "00" , size: 2) && |
70 | mdev->alpha2[0] && mdev->alpha2[1]) |
71 | return; |
72 | |
73 | /* do not need to update the same country twice */ |
74 | if (!memcmp(p: req->alpha2, q: mdev->alpha2, size: 2) && |
75 | dev->country_ie_env == req->country_ie_env) |
76 | return; |
77 | |
78 | memcpy(mdev->alpha2, req->alpha2, 2); |
79 | mdev->region = req->dfs_region; |
80 | dev->country_ie_env = req->country_ie_env; |
81 | |
82 | mt792x_mutex_acquire(dev); |
83 | mt7925_mcu_set_clc(dev, alpha2: req->alpha2, env_cap: req->country_ie_env); |
84 | mt7925_mcu_set_channel_domain(phy: hw->priv); |
85 | mt7925_set_tx_sar_pwr(hw, NULL); |
86 | mt792x_mutex_release(dev); |
87 | } |
88 | |
89 | static void mt7925_mac_init_basic_rates(struct mt792x_dev *dev) |
90 | { |
91 | int i; |
92 | |
93 | for (i = 0; i < ARRAY_SIZE(mt76_rates); i++) { |
94 | u16 rate = mt76_rates[i].hw_value; |
95 | u16 idx = MT792x_BASIC_RATES_TBL + i; |
96 | |
97 | rate = FIELD_PREP(MT_TX_RATE_MODE, rate >> 8) | |
98 | FIELD_PREP(MT_TX_RATE_IDX, rate & GENMASK(7, 0)); |
99 | mt7925_mac_set_fixed_rate_table(dev, tbl_idx: idx, rate_idx: rate); |
100 | } |
101 | } |
102 | |
103 | int mt7925_mac_init(struct mt792x_dev *dev) |
104 | { |
105 | int i; |
106 | |
107 | mt76_rmw_field(dev, MT_MDP_DCR1, MT_MDP_DCR1_MAX_RX_LEN, 1536); |
108 | /* enable hardware de-agg */ |
109 | mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_DAMSDU_EN); |
110 | |
111 | for (i = 0; i < MT792x_WTBL_SIZE; i++) |
112 | mt7925_mac_wtbl_update(dev, idx: i, |
113 | MT_WTBL_UPDATE_ADM_COUNT_CLEAR); |
114 | for (i = 0; i < 2; i++) |
115 | mt792x_mac_init_band(dev, band: i); |
116 | |
117 | mt7925_mac_init_basic_rates(dev); |
118 | |
119 | memzero_explicit(s: &dev->mt76.alpha2, count: sizeof(dev->mt76.alpha2)); |
120 | |
121 | return 0; |
122 | } |
123 | EXPORT_SYMBOL_GPL(mt7925_mac_init); |
124 | |
125 | static int __mt7925_init_hardware(struct mt792x_dev *dev) |
126 | { |
127 | int ret; |
128 | |
129 | ret = mt792x_mcu_init(dev); |
130 | if (ret) |
131 | goto out; |
132 | |
133 | mt76_eeprom_override(phy: &dev->mphy); |
134 | |
135 | ret = mt7925_mcu_set_eeprom(dev); |
136 | if (ret) |
137 | goto out; |
138 | |
139 | ret = mt7925_mac_init(dev); |
140 | if (ret) |
141 | goto out; |
142 | |
143 | out: |
144 | return ret; |
145 | } |
146 | |
147 | static int mt7925_init_hardware(struct mt792x_dev *dev) |
148 | { |
149 | int ret, i; |
150 | |
151 | set_bit(nr: MT76_STATE_INITIALIZED, addr: &dev->mphy.state); |
152 | |
153 | for (i = 0; i < MT792x_MCU_INIT_RETRY_COUNT; i++) { |
154 | ret = __mt7925_init_hardware(dev); |
155 | if (!ret) |
156 | break; |
157 | |
158 | mt792x_init_reset(dev); |
159 | } |
160 | |
161 | if (i == MT792x_MCU_INIT_RETRY_COUNT) { |
162 | dev_err(dev->mt76.dev, "hardware init failed\n" ); |
163 | return ret; |
164 | } |
165 | |
166 | return 0; |
167 | } |
168 | |
169 | static void mt7925_init_work(struct work_struct *work) |
170 | { |
171 | struct mt792x_dev *dev = container_of(work, struct mt792x_dev, |
172 | init_work); |
173 | int ret; |
174 | |
175 | ret = mt7925_init_hardware(dev); |
176 | if (ret) |
177 | return; |
178 | |
179 | mt76_set_stream_caps(phy: &dev->mphy, vht: true); |
180 | mt7925_set_stream_he_eht_caps(phy: &dev->phy); |
181 | |
182 | ret = mt76_register_device(dev: &dev->mt76, vht: true, rates: mt76_rates, |
183 | ARRAY_SIZE(mt76_rates)); |
184 | if (ret) { |
185 | dev_err(dev->mt76.dev, "register device failed\n" ); |
186 | return; |
187 | } |
188 | |
189 | ret = mt7925_init_debugfs(dev); |
190 | if (ret) { |
191 | dev_err(dev->mt76.dev, "register debugfs failed\n" ); |
192 | return; |
193 | } |
194 | |
195 | ret = mt7925_thermal_init(phy: &dev->phy); |
196 | if (ret) { |
197 | dev_err(dev->mt76.dev, "thermal init failed\n" ); |
198 | return; |
199 | } |
200 | |
201 | /* we support chip reset now */ |
202 | dev->hw_init_done = true; |
203 | |
204 | mt7925_mcu_set_deep_sleep(dev, enable: dev->pm.ds_enable); |
205 | } |
206 | |
207 | int mt7925_register_device(struct mt792x_dev *dev) |
208 | { |
209 | struct ieee80211_hw *hw = mt76_hw(dev); |
210 | int ret; |
211 | |
212 | dev->phy.dev = dev; |
213 | dev->phy.mt76 = &dev->mt76.phy; |
214 | dev->mt76.phy.priv = &dev->phy; |
215 | dev->mt76.tx_worker.fn = mt792x_tx_worker; |
216 | |
217 | INIT_DELAYED_WORK(&dev->pm.ps_work, mt792x_pm_power_save_work); |
218 | INIT_WORK(&dev->pm.wake_work, mt792x_pm_wake_work); |
219 | spin_lock_init(&dev->pm.wake.lock); |
220 | mutex_init(&dev->pm.mutex); |
221 | init_waitqueue_head(&dev->pm.wait); |
222 | spin_lock_init(&dev->pm.txq_lock); |
223 | INIT_DELAYED_WORK(&dev->mphy.mac_work, mt792x_mac_work); |
224 | INIT_DELAYED_WORK(&dev->phy.scan_work, mt7925_scan_work); |
225 | INIT_DELAYED_WORK(&dev->coredump.work, mt7925_coredump_work); |
226 | #if IS_ENABLED(CONFIG_IPV6) |
227 | INIT_WORK(&dev->ipv6_ns_work, mt7925_set_ipv6_ns_work); |
228 | skb_queue_head_init(list: &dev->ipv6_ns_list); |
229 | #endif |
230 | skb_queue_head_init(list: &dev->phy.scan_event_list); |
231 | skb_queue_head_init(list: &dev->coredump.msg_list); |
232 | |
233 | INIT_WORK(&dev->reset_work, mt7925_mac_reset_work); |
234 | INIT_WORK(&dev->init_work, mt7925_init_work); |
235 | |
236 | INIT_WORK(&dev->phy.roc_work, mt7925_roc_work); |
237 | timer_setup(&dev->phy.roc_timer, mt792x_roc_timer, 0); |
238 | init_waitqueue_head(&dev->phy.roc_wait); |
239 | |
240 | dev->pm.idle_timeout = MT792x_PM_TIMEOUT; |
241 | dev->pm.stats.last_wake_event = jiffies; |
242 | dev->pm.stats.last_doze_event = jiffies; |
243 | if (!mt76_is_usb(&dev->mt76)) { |
244 | dev->pm.enable_user = true; |
245 | dev->pm.enable = true; |
246 | dev->pm.ds_enable_user = true; |
247 | dev->pm.ds_enable = true; |
248 | } |
249 | |
250 | if (!mt76_is_mmio(&dev->mt76)) |
251 | hw->extra_tx_headroom += MT_SDIO_TXD_SIZE + MT_SDIO_HDR_SIZE; |
252 | |
253 | mt792x_init_acpi_sar(dev); |
254 | |
255 | ret = mt792x_init_wcid(dev); |
256 | if (ret) |
257 | return ret; |
258 | |
259 | ret = mt792x_init_wiphy(hw); |
260 | if (ret) |
261 | return ret; |
262 | |
263 | hw->wiphy->reg_notifier = mt7925_regd_notifier; |
264 | dev->mphy.sband_2g.sband.ht_cap.cap |= |
265 | IEEE80211_HT_CAP_LDPC_CODING | |
266 | IEEE80211_HT_CAP_MAX_AMSDU; |
267 | dev->mphy.sband_2g.sband.ht_cap.ampdu_density = |
268 | IEEE80211_HT_MPDU_DENSITY_2; |
269 | dev->mphy.sband_5g.sband.ht_cap.cap |= |
270 | IEEE80211_HT_CAP_LDPC_CODING | |
271 | IEEE80211_HT_CAP_MAX_AMSDU; |
272 | dev->mphy.sband_2g.sband.ht_cap.ampdu_density = |
273 | IEEE80211_HT_MPDU_DENSITY_1; |
274 | dev->mphy.sband_5g.sband.vht_cap.cap |= |
275 | IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 | |
276 | IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK | |
277 | IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE | |
278 | IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE | |
279 | (3 << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT); |
280 | dev->mphy.sband_5g.sband.vht_cap.cap |= |
281 | IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ | |
282 | IEEE80211_VHT_CAP_SHORT_GI_160; |
283 | |
284 | dev->mphy.hw->wiphy->available_antennas_rx = dev->mphy.chainmask; |
285 | dev->mphy.hw->wiphy->available_antennas_tx = dev->mphy.chainmask; |
286 | |
287 | queue_work(wq: system_wq, work: &dev->init_work); |
288 | |
289 | return 0; |
290 | } |
291 | EXPORT_SYMBOL_GPL(mt7925_register_device); |
292 | |