diff mbox series

[v2] mt76: mt7915: add debugfs knob for RF registers read/write

Message ID 20220415061444.30720-1-shayne.chen@mediatek.com
State New
Headers show
Series [v2] mt76: mt7915: add debugfs knob for RF registers read/write | expand

Commit Message

Shayne Chen April 15, 2022, 6:14 a.m. UTC
Add RF registers read/write support for debugging RF issues, which
should be processed by mcu commands.

Reviewed-by: Ryder Lee <ryder.lee@mediatek.com>
Signed-off-by: Peter Chiu <chui-hao.chiu@mediatek.com>
Signed-off-by: Shayne Chen <shayne.chen@mediatek.com>
---
v2: add dev_kfree_skb()
---
 .../wireless/mediatek/mt76/mt7915/debugfs.c   | 82 +++++++++++++++++++
 .../net/wireless/mediatek/mt76/mt7915/mcu.c   | 30 +++++++
 .../wireless/mediatek/mt76/mt7915/mt7915.h    |  4 +
 3 files changed, 116 insertions(+)

Comments

Felix Fietkau April 15, 2022, 8:26 a.m. UTC | #1
On 15.04.22 08:14, Shayne Chen wrote:
> Add RF registers read/write support for debugging RF issues, which
> should be processed by mcu commands.
> 
> Reviewed-by: Ryder Lee <ryder.lee@mediatek.com>
> Signed-off-by: Peter Chiu <chui-hao.chiu@mediatek.com>
> Signed-off-by: Shayne Chen <shayne.chen@mediatek.com>
What's the required width of rf_sel and rf_ofs? Would both fit together 
in a single u32? If so, you could just use the generic regidx debugfs 
file for both and simply add a rf_regval debugfs file.

- Felix
Shayne Chen April 15, 2022, 11:15 a.m. UTC | #2
On Fri, 2022-04-15 at 10:26 +0200, Felix Fietkau wrote:
> On 15.04.22 08:14, Shayne Chen wrote:
> > Add RF registers read/write support for debugging RF issues, which
> > should be processed by mcu commands.
> > 
> > Reviewed-by: Ryder Lee <ryder.lee@mediatek.com>
> > Signed-off-by: Peter Chiu <chui-hao.chiu@mediatek.com>
> > Signed-off-by: Shayne Chen <shayne.chen@mediatek.com>
> 
> What's the required width of rf_sel and rf_ofs? Would both fit
> together 
> in a single u32? If so, you could just use the generic regidx
> debugfs 
> file for both and simply add a rf_regval debugfs file.
> 
> - Felix

Hi Felix,

FW use the WF sel (antenna 0-3) and offset to locate rf registers.
I'll send an update patch to merge them in the generic regidx with
wf sel [31:28] and offset [27:0].

Thanks,
Shayne
Ben Greear April 15, 2022, 1:01 p.m. UTC | #3
On 4/15/22 4:15 AM, Shayne Chen wrote:
> On Fri, 2022-04-15 at 10:26 +0200, Felix Fietkau wrote:
>> On 15.04.22 08:14, Shayne Chen wrote:
>>> Add RF registers read/write support for debugging RF issues, which
>>> should be processed by mcu commands.
>>>
>>> Reviewed-by: Ryder Lee <ryder.lee@mediatek.com>
>>> Signed-off-by: Peter Chiu <chui-hao.chiu@mediatek.com>
>>> Signed-off-by: Shayne Chen <shayne.chen@mediatek.com>
>>
>> What's the required width of rf_sel and rf_ofs? Would both fit
>> together
>> in a single u32? If so, you could just use the generic regidx
>> debugfs
>> file for both and simply add a rf_regval debugfs file.
>>
>> - Felix
> 
> Hi Felix,
> 
> FW use the WF sel (antenna 0-3) and offset to locate rf registers.
> I'll send an update patch to merge them in the generic regidx with
> wf sel [31:28] and offset [27:0].
> 
> Thanks,
> Shayne

Hello,

Can you document the registers, or is this only for internal mediatek use?

Thanks,
Ben
diff mbox series

Patch

diff --git a/drivers/net/wireless/mediatek/mt76/mt7915/debugfs.c b/drivers/net/wireless/mediatek/mt76/mt7915/debugfs.c
index dece0a6e..275ff49e 100644
--- a/drivers/net/wireless/mediatek/mt76/mt7915/debugfs.c
+++ b/drivers/net/wireless/mediatek/mt76/mt7915/debugfs.c
@@ -867,6 +867,85 @@  mt7915_twt_stats(struct seq_file *s, void *data)
 	return 0;
 }
 
+static ssize_t
+mt7915_rf_regidx_read(struct file *file, char __user *userbuf,
+		      size_t count, loff_t *ppos)
+{
+	struct mt7915_dev *dev = file->private_data;
+	int ret, len = 14;
+	char *buf;
+
+	buf = kzalloc(len, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	ret = snprintf(buf, len, "%u 0x%08x\n", dev->rf_sel, dev->rf_ofs);
+	ret = simple_read_from_buffer(userbuf, count, ppos, buf, ret);
+
+	kfree(buf);
+	return ret;
+}
+
+static ssize_t
+mt7915_rf_regidx_write(struct file *file, const char __user *userbuf,
+		       size_t count, loff_t *ppos)
+{
+	struct mt7915_dev *dev = file->private_data;
+	char buf[13];
+
+	if (count > sizeof(buf))
+		return -EINVAL;
+
+	if (copy_from_user(buf, userbuf, count))
+		return -EFAULT;
+
+	buf[sizeof(buf) - 1] = '\0';
+
+	if (sscanf(buf, "%hhu 0x%x", &dev->rf_sel, &dev->rf_ofs) != 2) {
+		dev_warn(dev->mt76.dev,
+			 "format: <rf_sel> 0x<rf_ofs>\n");
+		return -EINVAL;
+	}
+
+	return count;
+}
+
+static const struct file_operations fops_rf_regidx = {
+	.open = simple_open,
+	.llseek = generic_file_llseek,
+	.read = mt7915_rf_regidx_read,
+	.write = mt7915_rf_regidx_write,
+	.owner = THIS_MODULE,
+};
+
+static int
+mt7915_rf_regval_get(void *data, u64 *val)
+{
+	struct mt7915_dev *dev = data;
+	u32 regval;
+	int ret;
+
+	ret = mt7915_mcu_rf_regval(dev, dev->rf_sel, dev->rf_ofs, &regval, false);
+	if (ret)
+		return ret;
+
+	*val = le32_to_cpu(regval);
+
+	return 0;
+}
+
+static int
+mt7915_rf_regval_set(void *data, u64 val)
+{
+	struct mt7915_dev *dev = data;
+
+	return mt7915_mcu_rf_regval(dev, dev->rf_sel, dev->rf_ofs,
+				    (u32 *)&val, true);
+}
+
+DEFINE_DEBUGFS_ATTRIBUTE(fops_rf_regval, mt7915_rf_regval_get,
+			 mt7915_rf_regval_set, "0x%08llx\n");
+
 int mt7915_init_debugfs(struct mt7915_phy *phy)
 {
 	struct mt7915_dev *dev = phy->dev;
@@ -898,6 +977,9 @@  int mt7915_init_debugfs(struct mt7915_phy *phy)
 	debugfs_create_devm_seqfile(dev->mt76.dev, "twt_stats", dir,
 				    mt7915_twt_stats);
 	debugfs_create_file("ser_trigger", 0200, dir, dev, &fops_ser_trigger);
+	debugfs_create_file("rf_regidx", 0600, dir, dev, &fops_rf_regidx);
+	debugfs_create_file("rf_regval", 0600, dir, dev, &fops_rf_regval);
+
 	if (!dev->dbdc_support || phy->band_idx) {
 		debugfs_create_u32("dfs_hw_pattern", 0400, dir,
 				   &dev->hw_pattern);
diff --git a/drivers/net/wireless/mediatek/mt76/mt7915/mcu.c b/drivers/net/wireless/mediatek/mt76/mt7915/mcu.c
index ec93a924..183ab554 100644
--- a/drivers/net/wireless/mediatek/mt76/mt7915/mcu.c
+++ b/drivers/net/wireless/mediatek/mt76/mt7915/mcu.c
@@ -3677,3 +3677,33 @@  int mt7915_mcu_twt_agrt_update(struct mt7915_dev *dev,
 	return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD(TWT_AGRT_UPDATE),
 				 &req, sizeof(req), true);
 }
+
+int mt7915_mcu_rf_regval(struct mt7915_dev *dev, u8 rf_sel,
+			 u32 rf_offset, u32 *val, bool set)
+{
+	struct {
+		__le32 idx;
+		__le32 ofs;
+		__le32 data;
+	} __packed req = {
+		.idx = cpu_to_le32(dev->rf_sel),
+		.ofs = cpu_to_le32(dev->rf_ofs),
+		.data = set ? cpu_to_le32(*val) : 0,
+	};
+	struct sk_buff *skb;
+	int ret;
+
+	if (set)
+		return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD(RF_REG_ACCESS),
+					 &req, sizeof(req), false);
+
+	ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_EXT_QUERY(RF_REG_ACCESS),
+					&req, sizeof(req), true, &skb);
+	if (ret)
+		return ret;
+
+	*val = le32_to_cpu(*(__le32 *)(skb->data + 8));
+	dev_kfree_skb(skb);
+
+	return 0;
+}
diff --git a/drivers/net/wireless/mediatek/mt76/mt7915/mt7915.h b/drivers/net/wireless/mediatek/mt76/mt7915/mt7915.h
index ca129e5e..ccff5043 100644
--- a/drivers/net/wireless/mediatek/mt76/mt7915/mt7915.h
+++ b/drivers/net/wireless/mediatek/mt76/mt7915/mt7915.h
@@ -312,6 +312,8 @@  struct mt7915_dev {
 	u8 fw_debug_wm;
 	u8 fw_debug_wa;
 	u8 fw_debug_bin;
+	u8 rf_sel;
+	u32 rf_ofs;
 
 	struct dentry *debugfs_dir;
 	struct rchan *relay_fwlog;
@@ -507,6 +509,8 @@  int mt7915_mcu_get_rx_rate(struct mt7915_phy *phy, struct ieee80211_vif *vif,
 			   struct ieee80211_sta *sta, struct rate_info *rate);
 int mt7915_mcu_rdd_background_enable(struct mt7915_phy *phy,
 				     struct cfg80211_chan_def *chandef);
+int mt7915_mcu_rf_regval(struct mt7915_dev *dev, u8 rf_sel,
+			 u32 rf_offset, u32 *val, bool set);
 int mt7915_mcu_wa_cmd(struct mt7915_dev *dev, int cmd, u32 a1, u32 a2, u32 a3);
 int mt7915_mcu_fw_log_2_host(struct mt7915_dev *dev, u8 type, u8 ctrl);
 int mt7915_mcu_fw_dbg_ctrl(struct mt7915_dev *dev, u32 module, u8 level);