Linux wireless drivers development
 help / color / mirror / Atom feed
From: "Sebastian Müller" <thxgiving@gmail.com>
To: linux-wireless@vger.kernel.org
Cc: nbd@nbd.name, lorenzo@kernel.org, ryder.lee@mediatek.com,
	shayne.chen@mediatek.com, sean.wang@mediatek.com,
	linux-mediatek@lists.infradead.org,
	"Sebastian Müller" <thxgiving@gmail.com>
Subject: [PATCH mt76] wifi: mt76: mt7996: register LEDs for the other bands
Date: Sat,  5 Dec 2026 21:44:04 +0100	[thread overview]
Message-ID: <20261205204404.3717178-1-thxgiving@gmail.com> (raw)

The LED controller of MT7996 has one channel per band
(MT_LED_CTRL(band), MT_LED_EN(band), MT_LED_TX_BLINK(band)), and
boards like the Banana Pi BE14 card wire one LED per band.
Since the switch to a single multi-radio wiphy only band0 gets a LED
class device (mt76-phyN). The LEDs of band1 and band2 keep the
firmware default and cannot be controlled.

Move the register programming of mt7996_led_set_config() into
__mt7996_led_set_config(dev, band, ...) and register one LED class
device for each additional band, named after its frequency band
(mt76-phyN-5g, mt76-phyN-6g). They use the throughput trigger of the
wiphy by default and can be controlled from userspace like the band0
LED.

Tested on a Banana Pi BPI-R4 with the BE14 (MT7996) card, on the
mt76 version of OpenWrt 25.12: each of the three LEDs
(2.4/5/6 GHz) can be switched individually.

Signed-off-by: Sebastian Müller <thxgiving@gmail.com>
---
 mt7996/init.c   | 124 +++++++++++++++++++++++++++++++++++++++++-------
 mt7996/mt7996.h |   9 ++++
 2 files changed, 116 insertions(+), 17 deletions(-)

diff --git a/mt7996/init.c b/mt7996/init.c
index 4638bbc..8bf38b3 100644
--- a/mt7996/init.c
+++ b/mt7996/init.c
@@ -288,26 +288,21 @@ static int mt7996_thermal_init(struct mt7996_phy *phy)
 	return 0;
 }
 
-static void mt7996_led_set_config(struct led_classdev *led_cdev,
-				  u8 delay_on, u8 delay_off)
+static void __mt7996_led_set_config(struct mt7996_dev *dev, u8 band,
+				    bool al, u8 delay_on, u8 delay_off)
 {
-	struct mt7996_dev *dev;
-	struct mt76_phy *mphy;
 	u32 val;
 
-	mphy = container_of(led_cdev, struct mt76_phy, leds.cdev);
-	dev = container_of(mphy->dev, struct mt7996_dev, mt76);
-
 	/* select TX blink mode, 2: only data frames */
-	mt76_rmw_field(dev, MT_TMAC_TCR0(mphy->band_idx), MT_TMAC_TCR0_TX_BLINK, 2);
+	mt76_rmw_field(dev, MT_TMAC_TCR0(band), MT_TMAC_TCR0_TX_BLINK, 2);
 
 	/* enable LED */
-	mt76_wr(dev, MT_LED_EN(mphy->band_idx), 1);
+	mt76_wr(dev, MT_LED_EN(band), 1);
 
 	/* set LED Tx blink on/off time */
 	val = FIELD_PREP(MT_LED_TX_BLINK_ON_MASK, delay_on) |
 	      FIELD_PREP(MT_LED_TX_BLINK_OFF_MASK, delay_off);
-	mt76_wr(dev, MT_LED_TX_BLINK(mphy->band_idx), val);
+	mt76_wr(dev, MT_LED_TX_BLINK(band), val);
 
 	/* turn LED off */
 	if (delay_off == 0xff && delay_on == 0x0) {
@@ -315,26 +310,39 @@ static void mt7996_led_set_config(struct led_classdev *led_cdev,
 	} else {
 		/* control LED */
 		val = MT_LED_CTRL_BLINK_MODE | MT_LED_CTRL_KICK;
-		if (mphy->band_idx == MT_BAND1)
+		if (band == MT_BAND1)
 			val |= MT_LED_CTRL_BLINK_BAND_SEL;
 	}
 
-	if (mphy->leds.al)
+	if (al)
 		val |= MT_LED_CTRL_POLARITY;
 
-	mt76_wr(dev, MT_LED_CTRL(mphy->band_idx), val);
-	mt76_clear(dev, MT_LED_CTRL(mphy->band_idx), MT_LED_CTRL_KICK);
+	mt76_wr(dev, MT_LED_CTRL(band), val);
+	mt76_clear(dev, MT_LED_CTRL(band), MT_LED_CTRL_KICK);
+}
+
+static void mt7996_led_set_config(struct led_classdev *led_cdev,
+				  u8 delay_on, u8 delay_off)
+{
+	struct mt7996_dev *dev;
+	struct mt76_phy *mphy;
+
+	mphy = container_of(led_cdev, struct mt76_phy, leds.cdev);
+	dev = container_of(mphy->dev, struct mt7996_dev, mt76);
+
+	__mt7996_led_set_config(dev, mphy->band_idx, mphy->leds.al,
+				delay_on, delay_off);
 }
 
+#define HW_TICK		10
+#define TO_HW_TICK(_t)	(((_t) > HW_TICK) ? ((_t) / HW_TICK) : HW_TICK)
+
 static int mt7996_led_set_blink(struct led_classdev *led_cdev,
 				unsigned long *delay_on,
 				unsigned long *delay_off)
 {
 	u16 delta_on = 0, delta_off = 0;
 
-#define HW_TICK		10
-#define TO_HW_TICK(_t)	(((_t) > HW_TICK) ? ((_t) / HW_TICK) : HW_TICK)
-
 	if (*delay_on)
 		delta_on = TO_HW_TICK(*delay_on);
 	if (*delay_off)
@@ -354,6 +362,85 @@ static void mt7996_led_set_brightness(struct led_classdev *led_cdev,
 		mt7996_led_set_config(led_cdev, 0xff, 0);
 }
 
+static int mt7996_band_led_set_blink(struct led_classdev *led_cdev,
+				     unsigned long *delay_on,
+				     unsigned long *delay_off)
+{
+	struct mt7996_band_led *led =
+		container_of(led_cdev, struct mt7996_band_led, cdev);
+	u16 delta_on = 0, delta_off = 0;
+
+	if (*delay_on)
+		delta_on = TO_HW_TICK(*delay_on);
+	if (*delay_off)
+		delta_off = TO_HW_TICK(*delay_off);
+
+	__mt7996_led_set_config(led->dev, led->band, led->dev->mphy.leds.al,
+				delta_on, delta_off);
+
+	return 0;
+}
+
+static void mt7996_band_led_set_brightness(struct led_classdev *led_cdev,
+					   enum led_brightness brightness)
+{
+	struct mt7996_band_led *led =
+		container_of(led_cdev, struct mt7996_band_led, cdev);
+	bool al = led->dev->mphy.leds.al;
+
+	if (!brightness)
+		__mt7996_led_set_config(led->dev, led->band, al, 0, 0xff);
+	else
+		__mt7996_led_set_config(led->dev, led->band, al, 0xff, 0);
+}
+
+static void mt7996_band_leds_register(struct mt7996_dev *dev)
+{
+	int i;
+
+	if (!IS_ENABLED(CONFIG_MT76_LEDS) ||
+	    !dev->mphy.leds.cdev.brightness_set)
+		return;
+
+	for (i = 0; i < ARRAY_SIZE(dev->band_leds); i++) {
+		struct mt7996_band_led *led = &dev->band_leds[i];
+		struct mt7996_phy *phy = __mt7996_phy(dev, MT_BAND1 + i);
+		const char *sfx;
+
+		if (!phy)
+			continue;
+
+		if (phy->mt76->cap.has_6ghz)
+			sfx = "6g";
+		else if (phy->mt76->cap.has_5ghz)
+			sfx = "5g";
+		else
+			sfx = "2g";
+
+		led->dev = dev;
+		led->band = MT_BAND1 + i;
+		snprintf(led->name, sizeof(led->name), "%s-%s",
+			 dev->mphy.leds.name, sfx);
+		led->cdev.name = led->name;
+		led->cdev.brightness_set = mt7996_band_led_set_brightness;
+		led->cdev.blink_set = mt7996_band_led_set_blink;
+		led->cdev.default_trigger = dev->mphy.leds.cdev.default_trigger;
+
+		dev_info(dev->mt76.dev, "registering led '%s'\n", led->name);
+		if (led_classdev_register(dev->mt76.dev, &led->cdev))
+			led->cdev.dev = NULL;
+	}
+}
+
+static void mt7996_band_leds_unregister(struct mt7996_dev *dev)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(dev->band_leds); i++)
+		if (dev->band_leds[i].cdev.dev)
+			led_classdev_unregister(&dev->band_leds[i].cdev);
+}
+
 static void __mt7996_init_txpower(struct mt7996_phy *phy,
 				  struct ieee80211_supported_band *sband)
 {
@@ -1817,6 +1904,8 @@ int mt7996_register_device(struct mt7996_dev *dev)
 	mt7996_for_each_phy(dev, phy)
 		mt7996_thermal_init(phy);
 
+	mt7996_band_leds_register(dev);
+
 	ieee80211_queue_work(mt76_hw(dev), &dev->init_work);
 
 	mt7996_set_hw_init_done(dev, true);
@@ -1850,6 +1939,7 @@ void mt7996_unregister_device(struct mt7996_dev *dev)
 	mt7996_unregister_phy(mt7996_phy2(dev));
 	mt7996_unregister_thermal(&dev->phy);
 	mt7996_coredump_unregister(dev);
+	mt7996_band_leds_unregister(dev);
 	mt76_unregister_device(&dev->mt76);
 	mt7996_wed_rro_free(dev);
 	for (i = 0; i < ARRAY_SIZE(dev->mt76.napi); i++) {
diff --git a/mt7996/mt7996.h b/mt7996/mt7996.h
index 94555a6..57f1046 100644
--- a/mt7996/mt7996.h
+++ b/mt7996/mt7996.h
@@ -484,6 +484,15 @@ struct mt7996_dev {
 
 	struct mt7996_phy *radio_phy[MT7996_MAX_RADIOS];
 	struct wiphy_radio radios[MT7996_MAX_RADIOS];
+
+	/* LEDs of band1/band2 (band0 uses mphy.leds) */
+	struct mt7996_band_led {
+		struct led_classdev cdev;
+		struct mt7996_dev *dev;
+		char name[32];
+		u8 band;
+	} band_leds[2];
+
 	struct wiphy_radio_freq_range radio_freqs[MT7996_MAX_RADIOS];
 	struct mac_address radio_addrs[MT7996_MAX_RADIOS];
 
-- 
2.55.0


                 reply	other threads:[~2026-12-05 20:44 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261205204404.3717178-1-thxgiving@gmail.com \
    --to=thxgiving@gmail.com \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=linux-wireless@vger.kernel.org \
    --cc=lorenzo@kernel.org \
    --cc=nbd@nbd.name \
    --cc=ryder.lee@mediatek.com \
    --cc=sean.wang@mediatek.com \
    --cc=shayne.chen@mediatek.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox