From e1b00df124f5b8feb982559b0cd5c31d18ebebae Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ond=C5=99ej=20Jirman?= <megi@xff.cz>
Date: Sun, 7 Nov 2021 19:29:06 +0100
Subject: usb: typec: fusb302: Extend debugging interface with driver state
 dumps

This is useful for debugging.

Signed-off-by: Ondrej Jirman <megi@xff.cz>
---
 drivers/usb/typec/tcpm/fusb302.c | 78 ++++++++++++++++++++++++++++++++
 1 file changed, 78 insertions(+)

diff --git a/drivers/usb/typec/tcpm/fusb302.c b/drivers/usb/typec/tcpm/fusb302.c
index 53fa863dfa96..2ee7222e531a 100644
--- a/drivers/usb/typec/tcpm/fusb302.c
+++ b/drivers/usb/typec/tcpm/fusb302.c
@@ -208,6 +208,81 @@ static int fusb302_debug_show(struct seq_file *s, void *v)
 }
 DEFINE_SHOW_ATTRIBUTE(fusb302_debug);
 
+static const char * const typec_cc_status_name[];
+static const char * const cc_polarity_name[];
+static const char * const toggling_mode_name[] = {
+	[TOGGLING_MODE_OFF] = "Off",
+	[TOGGLING_MODE_DRP] = "DRP",
+	[TOGGLING_MODE_SNK] = "SNK",
+	[TOGGLING_MODE_SRC] = "SRC",
+};
+static const char * const src_current_status_name[] = {
+	[SRC_CURRENT_DEFAULT] = "Default",
+	[SRC_CURRENT_MEDIUM] = "Medium",
+	[SRC_CURRENT_HIGH] = "High",
+};
+
+#define FUSB_REG(n) { n, #n },
+struct fusb_reg {
+	u8 addr;
+	const char* name;
+} fusb_regs[] = {
+	FUSB_REG(FUSB_REG_DEVICE_ID)
+	FUSB_REG(FUSB_REG_SWITCHES0)
+	FUSB_REG(FUSB_REG_SWITCHES1)
+	FUSB_REG(FUSB_REG_MEASURE)
+	FUSB_REG(FUSB_REG_CONTROL0)
+	FUSB_REG(FUSB_REG_CONTROL1)
+	FUSB_REG(FUSB_REG_CONTROL2)
+	FUSB_REG(FUSB_REG_CONTROL3)
+	FUSB_REG(FUSB_REG_MASK)
+	FUSB_REG(FUSB_REG_POWER)
+	FUSB_REG(FUSB_REG_RESET)
+	FUSB_REG(FUSB_REG_MASKA)
+	FUSB_REG(FUSB_REG_MASKB)
+	FUSB_REG(FUSB_REG_STATUS0A)
+	FUSB_REG(FUSB_REG_STATUS1A)
+	FUSB_REG(FUSB_REG_INTERRUPTA)
+	FUSB_REG(FUSB_REG_INTERRUPTB)
+	FUSB_REG(FUSB_REG_STATUS0)
+	FUSB_REG(FUSB_REG_STATUS1)
+	FUSB_REG(FUSB_REG_INTERRUPT)
+};
+
+static int fusb302_i2c_read(struct fusb302_chip *chip,
+			    u8 address, u8 *data);
+
+static int fusb302_debug_regs_show(struct seq_file *s, void *v)
+{
+	struct fusb302_chip *chip = (struct fusb302_chip *)s->private;
+	int i, ret;
+
+	seq_printf(s, "chip->intr_togdone = %d\n", chip->intr_togdone);
+	seq_printf(s, "chip->intr_bc_lvl = %d\n", chip->intr_bc_lvl);
+	seq_printf(s, "chip->intr_comp_chng = %d\n", chip->intr_comp_chng);
+	seq_printf(s, "chip->vconn_on = %d\n", chip->vconn_on);
+	seq_printf(s, "chip->vbus_on = %d\n", chip->vbus_on);
+	seq_printf(s, "chip->charge_on = %d\n", chip->charge_on);
+	seq_printf(s, "chip->vbus_present = %d\n", chip->vbus_present);
+	seq_printf(s, "chip->cc_polarity = %s\n", cc_polarity_name[chip->cc_polarity]);
+	seq_printf(s, "chip->cc1 = %s\n", typec_cc_status_name[chip->cc1]);
+	seq_printf(s, "chip->cc2 = %s\n", typec_cc_status_name[chip->cc2]);
+	seq_printf(s, "chip->toggling_mode = %s\n", toggling_mode_name[chip->toggling_mode]);
+	seq_printf(s, "chip->src_current_status = %s\n", src_current_status_name[chip->src_current_status]);
+
+	seq_printf(s, "\nRegisters:\n");
+        for (i = 0; i < ARRAY_SIZE(fusb_regs); i++) {
+		u8 val = 0;
+
+		ret = fusb302_i2c_read(chip, fusb_regs[i].addr, &val);
+		if (ret >= 0)
+			seq_printf(s, "%s = %02hhx\n", fusb_regs[i].name, val);
+	}
+
+	return 0;
+}
+DEFINE_SHOW_ATTRIBUTE(fusb302_debug_regs);
+
 static void fusb302_debugfs_init(struct fusb302_chip *chip)
 {
 	char name[NAME_MAX];
@@ -217,6 +292,9 @@ static void fusb302_debugfs_init(struct fusb302_chip *chip)
 	chip->dentry = debugfs_create_dir(name, usb_debug_root);
 	debugfs_create_file("log", S_IFREG | 0444, chip->dentry, chip,
 			    &fusb302_debug_fops);
+
+	debugfs_create_file("regs", S_IFREG | 0444, chip->dentry, chip,
+			    &fusb302_debug_regs_fops);
 }
 
 static void fusb302_debugfs_exit(struct fusb302_chip *chip)
-- 
2.35.3

