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