diff mbox series

[v3] wifi: rtl8xxxu: Fix LED control code of RTL8192FU

Message ID 7a2c3158-3a45-4466-b11e-fc09802b20e2@gmail.com
State New
Headers show
Series [v3] wifi: rtl8xxxu: Fix LED control code of RTL8192FU | expand

Commit Message

Bitterblue Smith Dec. 30, 2023, 10:45 p.m. UTC
Some devices, like the Comfast CF-826F, use LED1, which already works.
Others, like Asus USB-N13 C1, use LED0, which doesn't work correctly.

Write the right values to the LED control registers to make LED0 work
as well.

This is unfortunately tested only with the Comfast CF-826F.

Signed-off-by: Bitterblue Smith <rtl8821cerfe2@gmail.com>
---
v3:
 - Use more bit definitions.
 - Use u32p_replace_bits to touch only the relevant bits.
 - New author.

v2:
 - Explain why to fix the issue in this way in the commit message.
 - Split a long statement into short ones.
 - Use some of the definitions suggested by Ping-Ke Shih.

Linking v2 and v1 because it's been a while:
https://lore.kernel.org/linux-wireless/20230912112709.22456-1-zenmchen@gmail.com/
https://lore.kernel.org/linux-wireless/20230910002038.56362-1-zenmchen@gmail.com/
---
 .../realtek/rtl8xxxu/rtl8xxxu_8192f.c         | 32 +++++++++++++------
 .../wireless/realtek/rtl8xxxu/rtl8xxxu_regs.h | 15 +++++++++
 2 files changed, 38 insertions(+), 9 deletions(-)

Comments

Zenm Chen Jan. 1, 2024, 5:13 a.m. UTC | #1
Hi,

Thanks for making this patch.
I have no ASUS USB-N13 C1 now, so only tested with MERCUSYS MW310UH.
I think this patch will make the LEDs of ASUS USB-N13 C1/EDIMAX EW-7722UTn V3 work as expected.
Ping-Ke Shih Jan. 2, 2024, 1:50 a.m. UTC | #2
> -----Original Message-----
> From: Bitterblue Smith <rtl8821cerfe2@gmail.com>
> Sent: Sunday, December 31, 2023 6:46 AM
> To: linux-wireless@vger.kernel.org
> Cc: Jes Sorensen <Jes.Sorensen@gmail.com>; Ping-Ke Shih <pkshih@realtek.com>; Zenm Chen
> <zenmchen@gmail.com>
> Subject: [PATCH v3] wifi: rtl8xxxu: Fix LED control code of RTL8192FU
> 
> Some devices, like the Comfast CF-826F, use LED1, which already works.
> Others, like Asus USB-N13 C1, use LED0, which doesn't work correctly.
> 
> Write the right values to the LED control registers to make LED0 work
> as well.
> 
> This is unfortunately tested only with the Comfast CF-826F.
> 
> Signed-off-by: Bitterblue Smith <rtl8821cerfe2@gmail.com>

Thank you and Zenm for this work! 

Reviewed-by: Ping-Ke Shih <pkshih@realtek.com>
Kalle Valo Jan. 10, 2024, 2:55 p.m. UTC | #3
Bitterblue Smith <rtl8821cerfe2@gmail.com> wrote:

> Some devices, like the Comfast CF-826F, use LED1, which already works.
> Others, like Asus USB-N13 C1, use LED0, which doesn't work correctly.
> 
> Write the right values to the LED control registers to make LED0 work
> as well.
> 
> This is unfortunately tested only with the Comfast CF-826F.
> 
> Signed-off-by: Bitterblue Smith <rtl8821cerfe2@gmail.com>
> Reviewed-by: Ping-Ke Shih <pkshih@realtek.com>

Patch applied to wireless-next.git, thanks.

9475cc7ac315 wifi: rtl8xxxu: Fix LED control code of RTL8192FU
diff mbox series

Patch

diff --git a/drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8192f.c b/drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8192f.c
index 28e93835e05a..585b1a5eed69 100644
--- a/drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8192f.c
+++ b/drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8192f.c
@@ -2014,26 +2014,40 @@  static int rtl8192fu_led_brightness_set(struct led_classdev *led_cdev,
 	struct rtl8xxxu_priv *priv = container_of(led_cdev,
 						  struct rtl8xxxu_priv,
 						  led_cdev);
-	u16 ledcfg;
+	u32 ledcfg;
 
 	/* Values obtained by observing the USB traffic from the Windows driver. */
 	rtl8xxxu_write32(priv, REG_SW_GPIO_SHARE_CTRL_0, 0x20080);
 	rtl8xxxu_write32(priv, REG_SW_GPIO_SHARE_CTRL_1, 0x1b0000);
 
-	ledcfg = rtl8xxxu_read16(priv, REG_LEDCFG0);
+	ledcfg = rtl8xxxu_read32(priv, REG_LEDCFG0);
+
+	/* Comfast CF-826F uses LED1. Asus USB-N13 C1 uses LED0. Set both. */
+
+	u32p_replace_bits(&ledcfg, LED_GPIO_ENABLE, LEDCFG0_LED2EN);
+	u32p_replace_bits(&ledcfg, LED_IO_MODE_OUTPUT, LEDCFG0_LED0_IO_MODE);
+	u32p_replace_bits(&ledcfg, LED_IO_MODE_OUTPUT, LEDCFG0_LED1_IO_MODE);
 
 	if (brightness == LED_OFF) {
-		/* Value obtained like above. */
-		ledcfg = BIT(1) | BIT(7);
+		u32p_replace_bits(&ledcfg, LED_MODE_SW_CTRL, LEDCFG0_LED0CM);
+		u32p_replace_bits(&ledcfg, LED_SW_OFF, LEDCFG0_LED0SV);
+		u32p_replace_bits(&ledcfg, LED_MODE_SW_CTRL, LEDCFG0_LED1CM);
+		u32p_replace_bits(&ledcfg, LED_SW_OFF, LEDCFG0_LED1SV);
 	} else if (brightness == LED_ON) {
-		/* Value obtained like above. */
-		ledcfg = BIT(1) | BIT(7) | BIT(11);
+		u32p_replace_bits(&ledcfg, LED_MODE_SW_CTRL, LEDCFG0_LED0CM);
+		u32p_replace_bits(&ledcfg, LED_SW_ON, LEDCFG0_LED0SV);
+		u32p_replace_bits(&ledcfg, LED_MODE_SW_CTRL, LEDCFG0_LED1CM);
+		u32p_replace_bits(&ledcfg, LED_SW_ON, LEDCFG0_LED1SV);
 	} else if (brightness == RTL8XXXU_HW_LED_CONTROL) {
-		/* Value obtained by brute force. */
-		ledcfg = BIT(8) | BIT(9);
+		u32p_replace_bits(&ledcfg, LED_MODE_TX_OR_RX_EVENTS,
+				  LEDCFG0_LED0CM);
+		u32p_replace_bits(&ledcfg, LED_SW_OFF, LEDCFG0_LED0SV);
+		u32p_replace_bits(&ledcfg, LED_MODE_TX_OR_RX_EVENTS,
+				  LEDCFG0_LED1CM);
+		u32p_replace_bits(&ledcfg, LED_SW_OFF, LEDCFG0_LED1SV);
 	}
 
-	rtl8xxxu_write16(priv, REG_LEDCFG0, ledcfg);
+	rtl8xxxu_write32(priv, REG_LEDCFG0, ledcfg);
 
 	return 0;
 }
diff --git a/drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_regs.h b/drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_regs.h
index 920ee50e2115..61c0c0ec07b3 100644
--- a/drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_regs.h
+++ b/drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_regs.h
@@ -146,6 +146,21 @@ 
 #define  GPIO_INTM_EDGE_TRIG_IRQ	BIT(9)
 
 #define REG_LEDCFG0			0x004c
+#define  LEDCFG0_LED0CM			GENMASK(2, 0)
+#define  LEDCFG0_LED1CM			GENMASK(10, 8)
+#define   LED_MODE_SW_CTRL		0x0
+#define   LED_MODE_TX_OR_RX_EVENTS	0x3
+#define  LEDCFG0_LED0SV			BIT(3)
+#define  LEDCFG0_LED1SV			BIT(11)
+#define   LED_SW_OFF			0x0
+#define   LED_SW_ON			0x1
+#define  LEDCFG0_LED0_IO_MODE		BIT(7)
+#define  LEDCFG0_LED1_IO_MODE		BIT(15)
+#define   LED_IO_MODE_OUTPUT		0x0
+#define   LED_IO_MODE_INPUT		0x1
+#define  LEDCFG0_LED2EN			BIT(21)
+#define   LED_GPIO_DISABLE		0x0
+#define   LED_GPIO_ENABLE		0x1
 #define  LEDCFG0_DPDT_SELECT		BIT(23)
 #define REG_LEDCFG1			0x004d
 #define  LEDCFG1_HW_LED_CONTROL		BIT(1)