[][MAC80211][WiFi6][mt76][Disable RegDB when enable single sku]

[Description]
Refactor init txpower flow, disable RegDB when enable single sku.
And add e2p and RegDB maximum txpower in debugfs.

[Release-log]

Change-Id: If137b701c8f17cd894d6e5ec2ef19cdb0dfc96a4
Reviewed-on: https://gerrit.mediatek.inc/c/openwrt/feeds/mtk_openwrt_feeds/+/7875956
diff --git a/autobuild_mac80211_release/package/kernel/mt76/patches/1036-wifi-mt76-mt7915-Disable-RegDB-when-enable-single-sk.patch b/autobuild_mac80211_release/package/kernel/mt76/patches/1036-wifi-mt76-mt7915-Disable-RegDB-when-enable-single-sk.patch
new file mode 100644
index 0000000..2ce9ef0
--- /dev/null
+++ b/autobuild_mac80211_release/package/kernel/mt76/patches/1036-wifi-mt76-mt7915-Disable-RegDB-when-enable-single-sk.patch
@@ -0,0 +1,167 @@
+From 4cb59dc3fc13de7cfb32800927cb586036f9ec7b Mon Sep 17 00:00:00 2001
+From: "Allen.Ye" <allen.ye@mediatek.com>
+Date: Fri, 11 Aug 2023 16:46:53 +0800
+Subject: [PATCH 1035/1035] wifi: mt76: mt7915: Disable RegDB when enable
+ single sku
+
+---
+ eeprom.c         |  3 ++-
+ mt76.h           |  3 +++
+ mt7915/debugfs.c | 43 +++++++++++++++++++++++++++++++++++++++++--
+ mt7915/init.c    |  9 ++++++++-
+ 4 files changed, 54 insertions(+), 4 deletions(-)
+
+diff --git a/eeprom.c b/eeprom.c
+index 4189525..38b0a58 100644
+--- a/eeprom.c
++++ b/eeprom.c
+@@ -216,7 +216,7 @@ static bool mt76_string_prop_find(struct property *prop, const char *str)
+ 	return false;
+ }
+ 
+-static struct device_node *
++struct device_node *
+ mt76_find_power_limits_node(struct mt76_dev *dev)
+ {
+ 	struct device_node *np = dev->dev->of_node;
+@@ -255,6 +255,7 @@ mt76_find_power_limits_node(struct mt76_dev *dev)
+ 	of_node_put(np);
+ 	return fallback;
+ }
++EXPORT_SYMBOL_GPL(mt76_find_power_limits_node);
+ 
+ static const __be32 *
+ mt76_get_of_array(struct device_node *np, char *name, size_t *len, int min)
+diff --git a/mt76.h b/mt76.h
+index 2f801de..aece4ec 100644
+--- a/mt76.h
++++ b/mt76.h
+@@ -1655,6 +1655,9 @@ mt76_mcu_skb_send_msg(struct mt76_dev *dev, struct sk_buff *skb, int cmd,
+ 
+ void mt76_set_irq_mask(struct mt76_dev *dev, u32 addr, u32 clear, u32 set);
+ 
++struct device_node *
++mt76_find_power_limits_node(struct mt76_dev *dev);
++
+ s8 mt76_get_rate_power_limits(struct mt76_phy *phy,
+ 			      struct ieee80211_channel *chan,
+ 			      struct mt76_power_limits *dest,
+diff --git a/mt7915/debugfs.c b/mt7915/debugfs.c
+index 2bf907c..bb312ee 100644
+--- a/mt7915/debugfs.c
++++ b/mt7915/debugfs.c
+@@ -1019,10 +1019,16 @@ mt7915_rate_txpower_get(struct file *file, char __user *user_buf,
+ {
+ 	struct mt7915_phy *phy = file->private_data;
+ 	struct mt7915_dev *dev = phy->dev;
++	struct ieee80211_channel *chan = phy->mt76->chandef.chan;
++	struct ieee80211_supported_band sband;
+ 	s8 txpwr[MT7915_SKU_RATE_NUM];
+-	static const size_t sz = 2048;
++	static const size_t sz = 4096;
+ 	u8 band = phy->mt76->band_idx;
+ 	int i, offs = 0, len = 0;
++	u32 target_power = 0;
++	int n_chains = hweight16(phy->mt76->chainmask);
++	int nss_delta = mt76_tx_power_nss_delta(n_chains);
++	int pwr_delta;
+ 	ssize_t ret;
+ 	char *buf;
+ 	u32 reg;
+@@ -1084,9 +1090,36 @@ mt7915_rate_txpower_get(struct file *file, char __user *user_buf,
+ 	reg = is_mt7915(&dev->mt76) ? MT_WF_PHY_TPC_CTRL_STAT(band) :
+ 	      MT_WF_PHY_TPC_CTRL_STAT_MT7916(band);
+ 
+-	len += scnprintf(buf + len, sz - len, "\nTx power (bbp)  : %6ld\n",
++	len += scnprintf(buf + len, sz - len, "\nTx power (bbp)  : %6ld [0.5 dBm]\n",
+ 			 mt76_get_field(dev, reg, MT_WF_PHY_TPC_POWER));
+ 
++	len += scnprintf(buf + len, sz - len, "RegDB maximum power:\t%d [dBm]\n",
++			 chan->max_reg_power);
++
++	if (chan->band == NL80211_BAND_2GHZ)
++		sband = phy->mt76->sband_2g.sband;
++	else if (chan->band == NL80211_BAND_5GHZ)
++		sband = phy->mt76->sband_5g.sband;
++	else if (chan->band == NL80211_BAND_6GHZ)
++		sband = phy->mt76->sband_6g.sband;
++
++	pwr_delta = mt7915_eeprom_get_power_delta(dev, sband.band);
++
++	for (i = 0; i < n_chains; i++) {
++		u32 val;
++
++		val = mt7915_eeprom_get_target_power(dev, chan, i);
++		target_power = max(target_power, val);
++	}
++
++	target_power += pwr_delta + nss_delta;
++	target_power = DIV_ROUND_UP(target_power, 2);
++	len += scnprintf(buf + len, sz - len, "eeprom maximum power:\t%d [dBm]\n",
++			 target_power);
++
++	len += scnprintf(buf + len, sz - len, "nss_delta:\t%d [0.5 dBm]\n",
++			 nss_delta);
++
+ 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
+ 
+ out:
+@@ -1262,6 +1295,8 @@ static int
+ mt7915_txpower_info_show(struct seq_file *file, void *data)
+ {
+ 	struct mt7915_phy *phy = file->private;
++	struct mt76_phy *mphy = phy->mt76;
++	struct mt76_dev *dev = mphy->dev;
+ 	struct {
+ 		u8 category;
+ 		u8 rsv1;
+@@ -1303,6 +1338,7 @@ mt7915_txpower_info_show(struct seq_file *file, void *data)
+ 		s8 mu_tx_power_manual;
+ 		u8 rsv3;
+ 	} __packed basic_info = {};
++	struct device_node *np;
+ 	int ret;
+ 
+ 	ret = mt7915_mcu_get_txpower_sku(phy, (s8 *)&basic_info, sizeof(basic_info),
+@@ -1337,6 +1373,9 @@ mt7915_txpower_info_show(struct seq_file *file, void *data)
+ 	seq_printf(file, "    Theraml Compensation Value: %d\n",
+ 		   basic_info.thermal_compensate_value);
+ 
++	np = mt76_find_power_limits_node(dev);
++	seq_printf(file, "    RegDB:  %s\n", !np ? "enable" : "disable");
++
+ out:
+ 	return ret;
+ }
+diff --git a/mt7915/init.c b/mt7915/init.c
+index f8ecaf3..623b070 100644
+--- a/mt7915/init.c
++++ b/mt7915/init.c
+@@ -286,7 +286,9 @@ void __mt7915_init_txpower(struct mt7915_phy *phy,
+ 	int nss_delta = mt76_tx_power_nss_delta(n_chains);
+ 	int pwr_delta = mt7915_eeprom_get_power_delta(dev, sband->band);
+ 	struct mt76_power_limits limits;
++	struct device_node *np;
+ 
++	np = mt76_find_power_limits_node(&dev->mt76);
+ 	for (i = 0; i < sband->n_channels; i++) {
+ 		struct ieee80211_channel *chan = &sband->channels[i];
+ 		u32 target_power = 0;
+@@ -305,8 +307,13 @@ void __mt7915_init_txpower(struct mt7915_phy *phy,
+ 							  target_power);
+ 		target_power += nss_delta;
+ 		target_power = DIV_ROUND_UP(target_power, 2);
+-		chan->max_power = min_t(int, chan->max_reg_power,
++
++		/* can NOT find country node in dts */
++		if (!np)
++			chan->max_power = min_t(int, chan->max_reg_power,
+ 					target_power);
++		else
++			chan->max_power = target_power;
+ 		chan->orig_mpwr = target_power;
+ 	}
+ }
+-- 
+2.18.0
+