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 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 
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 	if (!name)
56 		return -ENOMEM;
57 
58 	hwmon = devm_hwmon_device_register_with_groups(&wiphy->dev, name, phy,
59 						       mt7921_hwmon_groups);
60 	if (IS_ERR(hwmon))
61 		return PTR_ERR(hwmon);
62 
63 	return 0;
64 }
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 	ret = mt7921_init_debugfs(dev);
170 	if (ret) {
171 		dev_err(dev->mt76.dev, "register debugfs failed\n");
172 		return;
173 	}
174 
175 	ret = mt7921_thermal_init(&dev->phy);
176 	if (ret) {
177 		dev_err(dev->mt76.dev, "thermal init failed\n");
178 		return;
179 	}
180 
181 	/* we support chip reset now */
182 	dev->hw_init_done = true;
183 
184 	mt76_connac_mcu_set_deep_sleep(&dev->mt76, dev->pm.ds_enable);
185 }
186 
187 int mt7921_register_device(struct mt792x_dev *dev)
188 {
189 	struct ieee80211_hw *hw = mt76_hw(dev);
190 	int ret;
191 
192 	dev->phy.dev = dev;
193 	dev->phy.mt76 = &dev->mt76.phy;
194 	dev->mt76.phy.priv = &dev->phy;
195 	dev->mt76.tx_worker.fn = mt792x_tx_worker;
196 
197 	INIT_DELAYED_WORK(&dev->pm.ps_work, mt792x_pm_power_save_work);
198 	INIT_WORK(&dev->pm.wake_work, mt792x_pm_wake_work);
199 	spin_lock_init(&dev->pm.wake.lock);
200 	mutex_init(&dev->pm.mutex);
201 	init_waitqueue_head(&dev->pm.wait);
202 	if (mt76_is_sdio(&dev->mt76))
203 		init_waitqueue_head(&dev->mt76.sdio.wait);
204 	spin_lock_init(&dev->pm.txq_lock);
205 	INIT_DELAYED_WORK(&dev->mphy.mac_work, mt792x_mac_work);
206 	INIT_DELAYED_WORK(&dev->phy.scan_work, mt7921_scan_work);
207 	INIT_DELAYED_WORK(&dev->coredump.work, mt7921_coredump_work);
208 #if IS_ENABLED(CONFIG_IPV6)
209 	INIT_WORK(&dev->ipv6_ns_work, mt7921_set_ipv6_ns_work);
210 	skb_queue_head_init(&dev->ipv6_ns_list);
211 #endif
212 	skb_queue_head_init(&dev->phy.scan_event_list);
213 	skb_queue_head_init(&dev->coredump.msg_list);
214 
215 	INIT_WORK(&dev->reset_work, mt7921_mac_reset_work);
216 	INIT_WORK(&dev->init_work, mt7921_init_work);
217 
218 	INIT_WORK(&dev->phy.roc_work, mt7921_roc_work);
219 	timer_setup(&dev->phy.roc_timer, mt792x_roc_timer, 0);
220 	init_waitqueue_head(&dev->phy.roc_wait);
221 
222 	dev->pm.idle_timeout = MT792x_PM_TIMEOUT;
223 	dev->pm.stats.last_wake_event = jiffies;
224 	dev->pm.stats.last_doze_event = jiffies;
225 	if (!mt76_is_usb(&dev->mt76)) {
226 		dev->pm.enable_user = true;
227 		dev->pm.enable = true;
228 		dev->pm.ds_enable_user = true;
229 		dev->pm.ds_enable = true;
230 	}
231 
232 	if (!mt76_is_mmio(&dev->mt76))
233 		hw->extra_tx_headroom += MT_SDIO_TXD_SIZE + MT_SDIO_HDR_SIZE;
234 
235 	mt792x_init_acpi_sar(dev);
236 
237 	ret = mt792x_init_wcid(dev);
238 	if (ret)
239 		return ret;
240 
241 	ret = mt792x_init_wiphy(hw);
242 	if (ret)
243 		return ret;
244 
245 	hw->wiphy->reg_notifier = mt7921_regd_notifier;
246 	dev->mphy.sband_2g.sband.ht_cap.cap |=
247 			IEEE80211_HT_CAP_LDPC_CODING |
248 			IEEE80211_HT_CAP_MAX_AMSDU;
249 	dev->mphy.sband_5g.sband.ht_cap.cap |=
250 			IEEE80211_HT_CAP_LDPC_CODING |
251 			IEEE80211_HT_CAP_MAX_AMSDU;
252 	dev->mphy.sband_5g.sband.vht_cap.cap |=
253 			IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
254 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
255 			IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
256 			IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
257 			(3 << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT);
258 	if (is_mt7922(&dev->mt76))
259 		dev->mphy.sband_5g.sband.vht_cap.cap |=
260 			IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
261 			IEEE80211_VHT_CAP_SHORT_GI_160;
262 
263 	dev->mphy.hw->wiphy->available_antennas_rx = dev->mphy.chainmask;
264 	dev->mphy.hw->wiphy->available_antennas_tx = dev->mphy.chainmask;
265 
266 	queue_work(system_wq, &dev->init_work);
267 
268 	return 0;
269 }
270 EXPORT_SYMBOL_GPL(mt7921_register_device);
271