Skip to contentSkip to conversation

wifi: mt76: mt7925: LED support for USB adapters

PATCH In review Josef Schlehofer 2 participants 5 messages Nov 3 – Nov 19

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover wifi: mt76: mt7925: LED support for USB adapters

MT7925 USB adapters such as the NETGEAR A9000 have a status LED on GPIO 8 of the chip. MediaTek's Windows driver asks the firmware to switch it with UNI command 0x2a, but the firmware in linux-firmware does not act on that command [1].

This series drives the pin from the host instead. The layout of the CBTOP GPIO block comes from the coda headers in MediaTek's gen4m driver for MT6639, which uses the same CBTOP register map as MT7925. The first patch lets the mt76 core register an LED that only provides brightness_set_blocking, which a USB driver needs. The second patch adds the LED to mt7925.

Tested on a Turris Omnia with an A9000 and firmware 20231227 over several days. The LED follows the throughput trigger and manual brightness changes, and turns off when the radio goes down.

Sean, JB, is it fine to drive these GPIO registers from the host, or does the firmware rely on GPIO 8 staying in its default function?

[1] https://lore.kernel.org/linux-wireless/[email protected]/

Josef Schlehofer (2):
wifi: mt76: allow LEDs with only brightness_set_blocking
wifi: mt76: mt7925: add LED support for USB adapters

 drivers/net/wireless/mediatek/mt76/mac80211.c | 12 ++++-
 .../net/wireless/mediatek/mt76/mt7925/init.c  | 51 +++++++++++++++++++
 .../net/wireless/mediatek/mt76/mt7925/mcu.c   |  2 +
 .../net/wireless/mediatek/mt76/mt7925/regs.h  |  8 +++
 drivers/net/wireless/mediatek/mt76/mt792x.h   |  2 +
 5 files changed, 73 insertions(+), 2 deletions(-)

base-commit: 991a73f4909f1348ffb4b38765cdb522a775e4c2

Signature · 1 line
2.54.0 (Apple Git-157)
#1 lore raw

PATCH 1/2 wifi: mt76: allow LEDs with only brightness_set_blocking

mt76_led_init() and mt76_led_cleanup() skip the LED unless the driver sets brightness_set or blink_set. A USB driver that switches its LED through register writes has to sleep and can only provide brightness_set_blocking. Accept that callback as well.

---
 drivers/net/wireless/mediatek/mt76/mac80211.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/net/wireless/mediatek/mt76/mac80211.c +10 -2
index c9762082f473..e3eea578a97c 100644
@@ -193,13 +193,21 @@ static const struct cfg80211_sar_capa mt76_sar_capa = {
 	.freq_ranges = &mt76_sar_freq_ranges[0],
 };
 
+static bool mt76_led_has_ops(struct mt76_phy *phy)
+{
+	struct led_classdev *cdev = &phy->leds.cdev;
+
+	return cdev->brightness_set || cdev->brightness_set_blocking ||
+	       cdev->blink_set;
+}
+
 static int mt76_led_init(struct mt76_phy *phy)
 {
 	struct mt76_dev *dev = phy->dev;
 	struct ieee80211_hw *hw = phy->hw;
 	struct device_node *np = dev->dev->of_node;
 
-	if (!phy->leds.cdev.brightness_set && !phy->leds.cdev.blink_set)
+	if (!mt76_led_has_ops(phy))
 		return 0;
 
 	np = of_get_child_by_name(np, "led");
@@ -242,7 +250,7 @@ static int mt76_led_init(struct mt76_phy *phy)
 
 static void mt76_led_cleanup(struct mt76_phy *phy)
 {
-	if (!phy->leds.cdev.brightness_set && !phy->leds.cdev.blink_set)
+	if (!mt76_led_has_ops(phy))
 		return;
 
 	led_classdev_unregister(&phy->leds.cdev);
Signature · 1 line
2.54.0 (Apple Git-157)
#2 lore raw

PATCH 2/2 wifi: mt76: mt7925: add LED support for USB adapters

In reply to Josef Schlehofer

MT7925 USB adapters such as the NETGEAR A9000 have their status LED on GPIO 8 of the chip. MediaTek's Windows driver asks the firmware to switch it, but the firmware in linux-firmware does not act on that request.

Drive the pin from the host instead. Set its level, then switch it from its default function to a GPIO output through the CBTOP GPIO registers. The register layout follows the coda headers in MediaTek's gen4m driver. The LED of the A9000 is active low.

While the adapter is idle, the throughput trigger keeps requesting the same state every second. Only write the registers when the state changes, and forget the cached state whenever the firmware is loaded.

---
 .../net/wireless/mediatek/mt76/mt7925/init.c  | 51 +++++++++++++++++++
 .../net/wireless/mediatek/mt76/mt7925/mcu.c   |  2 +
 .../net/wireless/mediatek/mt76/mt7925/regs.h  |  8 +++
 drivers/net/wireless/mediatek/mt76/mt792x.h   |  2 +
 4 files changed, 63 insertions(+)
drivers/net/wireless/mediatek/mt76/mt7925/init.c +51 -0
index 8c3049b66a85..3dfb4b2d997c 100644
@@ -187,6 +187,56 @@ static int mt7925_init_nan_cap(struct mt76_dev *mdev)
 	return 0;
 }
 
+static int mt7925_led_set_brightness(struct led_classdev *led_cdev,
+				     enum led_brightness brightness)
+{
+	struct mt76_phy *mphy = container_of(led_cdev, struct mt76_phy,
+					     leds.cdev);
+	struct mt792x_dev *dev = container_of(mphy->dev, struct mt792x_dev,
+					      mt76);
+	bool on = brightness != LED_OFF;
+	u8 pin = mphy->leds.pin;
+	u32 mask = BIT(pin % 32);
+
+	mt792x_mutex_acquire(dev);
+	if (dev->pm.suspended || dev->led_state == on)
+		goto out;
+
+	if (on ^ mphy->leds.al)
+		mt76_wr(dev, MT_CBTOP_GPIO_DOUT_SET(pin), mask);
+	else
+		mt76_wr(dev, MT_CBTOP_GPIO_DOUT_CLR(pin), mask);
+
+	/* the pin starts in another function, make it a GPIO output */
+	if (dev->led_state < 0) {
+		mt76_wr(dev, MT_CBTOP_GPIO_DIR_SET(pin), mask);
+		mt76_wr(dev, MT_CBTOP_GPIO_MODE_MOD(pin),
+			MT_CBTOP_GPIO_MODE_MOD_EN << ((pin % 8) * 4));
+	}
+
+	dev->led_state = on;
+out:
+	mt792x_mutex_release(dev);
+
+	return 0;
+}
+
+static void mt7925_init_led(struct mt792x_dev *dev)
+{
+	struct mt76_phy *mphy = &dev->mphy;
+
+	if (!IS_ENABLED(CONFIG_MT76_LEDS) || !mt76_is_usb(&dev->mt76) ||
+	    !is_mt7925(&dev->mt76))
+		return;
+
+	/* MediaTek's Windows driver uses GPIO 8 on MT7925 USB adapters */
+	mphy->leds.pin = 8;
+	/* the LED of the NETGEAR A9000 is active low */
+	mphy->leds.al = true;
+	mphy->leds.cdev.flags |= LED_HW_PLUGGABLE;
+	mphy->leds.cdev.brightness_set_blocking = mt7925_led_set_brightness;
+}
+
 static void mt7925_init_work(struct work_struct *work)
 {
 	struct mt792x_dev *dev = container_of(work, struct mt792x_dev,
@@ -208,6 +258,7 @@ static void mt7925_init_work(struct work_struct *work)
 	}
 
 	dev->mt76.init_wiphy = mt7925_init_nan_cap;
+	mt7925_init_led(dev);
 
 	ret = mt76_register_device(&dev->mt76, true, mt76_rates,
 				   ARRAY_SIZE(mt76_rates));
drivers/net/wireless/mediatek/mt76/mt7925/mcu.c +2 -0
index 26cdb3f0a98a..91564a2c1d65 100644
@@ -1191,6 +1191,8 @@ int mt7925_run_firmware(struct mt792x_dev *dev)
 {
 	int err;
 
+	dev->led_state = -1;
+
 	err = mt792x_load_firmware(dev);
 	if (err)
 		return err;
drivers/net/wireless/mediatek/mt76/mt7925/regs.h +8 -0
index e040dff321dd..73bd8c58dd84 100644
@@ -91,6 +91,14 @@
 
 #define MT_WFSYS_SW_RST_B		0x7c000140
 
+#define MT_CBTOP_GPIO_BASE		0x70005000
+#define MT_CBTOP_GPIO(ofs)		(MT_CBTOP_GPIO_BASE + (ofs))
+#define MT_CBTOP_GPIO_DIR_SET(_n)	MT_CBTOP_GPIO(0x004 + ((_n) / 32) * 0x10)
+#define MT_CBTOP_GPIO_DOUT_SET(_n)	MT_CBTOP_GPIO(0x104 + ((_n) / 32) * 0x10)
+#define MT_CBTOP_GPIO_DOUT_CLR(_n)	MT_CBTOP_GPIO(0x108 + ((_n) / 32) * 0x10)
+#define MT_CBTOP_GPIO_MODE_MOD(_n)	MT_CBTOP_GPIO(0x30c + ((_n) / 8) * 0x10)
+#define MT_CBTOP_GPIO_MODE_MOD_EN	BIT(3)
+
 #define MT7925_WTBLON_TOP_WDUCR		MT_WTBLON_TOP(0x370)
 #define MT7928_WTBLON_TOP_WDUCR		MT_WTBLON_TOP(0x400)
 #define MT_WTBLON_TOP_WDUCR_GROUP	GENMASK(4, 0)
drivers/net/wireless/mediatek/mt76/mt792x.h +2 -0
index a7d71e007da0..73c23f509b99 100644
@@ -324,6 +324,8 @@ struct mt792x_dev {
 
 	u8 fw_debug;
 	u8 fw_features;
+	/* LED state last written to the GPIO, -1 if unknown */
+	s8 led_state;
 
 	struct mt76_connac_pm pm;
 	struct mt76_connac_coredump coredump;
Signature · 1 line
2.54.0 (Apple Git-157)
Tested-by on the v1 series
#3 lore raw
In reply to Josef Schlehofer
Quoted 3 lines · Josef Schlehofer

Tested on a Turris Omnia with an A9000 and firmware 20231227 over several days. The LED follows the throughput trigger and manual brightness changes, and turns off when the radio goes down.

I tried it on an A9000 too and watched the LED on each of these:

wireless-next b8e5b0ae8, x86, the series as posted 6.18.34, Raspberry Pi 5, arm64, the same changes 6.18 LTS, x86, the same changes

It switched off and on as asked and blinked with traffic, on firmware 20260813 and 20261016, and the LED was registered again after a USB reauthorize. No warnings in the kernel log, and on wireless-next an MT7921AU and an MT7922 join and pass traffic with the series loaded.

Quoted 2 lines

Sean, JB, is it fine to drive these GPIO registers from the host, or does the firmware rely on GPIO 8 staying in its default function?

With the pin switched to a GPIO output, the A9000 joined and passed traffic on all three.