mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] ALSA: hda/ca0132: Fix AE-5 strip color corruption and add on-card LED control
@ 2026-11-16 14:02 Devin Christensen
  2026-11-16 14:17 ` Takashi Iwai
  0 siblings, 1 reply; 5+ messages in thread
From: Devin Christensen @ 2026-11-16 14:02 UTC (permalink / raw)
  To: tiwai, Devin Christensen; +Cc: linux-sound, linux-kernel, rafael

This update resolves the external WS2812 RGB LED strip color scrambling
glitch during rapid animation updates, and adds ALSA card control
support for the 5 on-board APA102 RGB LEDs on Sound BlasterX AE-5 cards.

1. Fix external strip color corruption:
In ae5_strip_send_frame(), formatting repeating frame cycles across the
32 KB DMA buffer combined with stopping the HDA stream after an arbitrary
15-20 ms delay caused transmissions to be cut off mid-frame / mid-byte.
WS2812 shift registers latch on signal low (>50 us), so terminating
mid-byte leaves partial bits in the registers, shifting color channel
alignments (e.g. blue/white shifting to green/purple/red) on subsequent
frames. Fix this by populating exactly one WS2812 frame followed by the
reset gap and zero-filling the remainder of the 32 KB buffer. Reduce
usleep_range to 8-10 ms, which guarantees the frame has completed and
the stream terminates cleanly during trailing silence without truncating
active LED data.

2. Add on-card APA102 RGB LED control:
The AE-5 and AE-5 Plus feature 5 on-board APA102 (DotStar) RGB LEDs
providing logo and shroud illumination. These are driven directly by
CA0113 bridge GPIO pins 2 (data) and 3 (clock) mapped through PCIe BAR2
at MMIO offset 0x320.
Expose the on-board LEDs through a volatile card control element:
  "AE-5 On-Card LEDs" (RW, BYTES): 15-byte RGB payload representing
                      the 5 on-board LEDs (3 bytes per LED).
Writes bit-bang the 32-bit start frame, 5 APA102 LED frames (8-bit
brightness and 24-bit BGR color), and 32-bit end frame over GPIO pins 2
and 3, returning the pins to idle low. Mutex serialization and runtime
PM are shared with the external strip control.

Fixes: c3e35f355fad ("ALSA: hda/ca0132: Add Sound BlasterX AE-5 external RGB LED strip support")
Signed-off-by: Devin Christensen <dchristensen8@proton.me>
---
 sound/hda/codecs/ca0132.c | 151 +++++++++++++++++++++++++++++---------
 1 file changed, 116 insertions(+), 35 deletions(-)

diff --git a/sound/hda/codecs/ca0132.c b/sound/hda/codecs/ca0132.c
index b3e768be8..b7c97a079 100644
--- a/sound/hda/codecs/ca0132.c
+++ b/sound/hda/codecs/ca0132.c
@@ -1062,6 +1062,7 @@ enum dsp_download_state {
  */
 
 #define AE5_STRIP_MAX_LEDS 100
+#define AE5_ONCARD_LEDS 5
 
 struct ca0132_spec {
 	struct hda_gen_spec gen;
@@ -1167,10 +1168,11 @@ struct ca0132_spec {
 	bool use_alt_controls;
 
 #if IS_ENABLED(CONFIG_SND_HDA_DSP_LOADER)
-	/* AE-5 WS2812 strip control */
+	/* AE-5 WS2812 strip and on-card LED control */
 	struct mutex ae5_strip_mutex;
 	u32 ae5_strip_cur_colors[AE5_STRIP_MAX_LEDS];
 	int ae5_strip_cur_num_leds;
+	u8 ae5_oncard_cur_colors[AE5_ONCARD_LEDS * 3];
 #endif
 };
 
@@ -8421,8 +8423,6 @@ static int ae5_strip_send_frame(struct hda_codec *codec, const u32 *grb_colors,
 		u32 ser_base[4], ser_clk[4], ser_out[4];
 	} saved;
 	__le32 *dst;
-	int word_idx = 0;
-	int frame_words;
 	int led, w, i;
 	int retries = 20; /* ~100ms max */
 
@@ -8470,38 +8470,25 @@ static int ae5_strip_send_frame(struct hda_codec *codec, const u32 *grb_colors,
 		usleep_range(5000, 10000);
 	}
 
-	/* 1. Populate the 32KB DMA buffer with repeating WS2812 frames + reset gaps */
+	/* 1. Populate the 32KB DMA buffer: 1 WS2812 frame + reset gap, rest zero */
 	memset(dmab.area, 0, AE5_STRIP_DMA_SIZE);
 	dst = (__le32 *)dmab.area;
 
-	/* Words per frame: preamble + (num_leds * words_per_led) + reset gap */
-	frame_words = AE5_STRIP_PREAMBLE_WORDS + (num_leds * AE5_STRIP_WORDS_PER_LED) +
-		      AE5_STRIP_RESET_GAP_WORDS;
-
-	while (word_idx + frame_words <= AE5_STRIP_TOTAL_WORDS) {
-		/* Preamble per Windows CtxHda FUN_00042944 */
-		for (w = 0; w < AE5_STRIP_PREAMBLE_WORDS; w++) {
-			*dst++ = 0;
-			word_idx++;
-		}
-		/* LEDs */
-		for (led = 0; led < num_leds; led++) {
-			ae5_strip_encode_24(grb_colors[led], enc_words);
-			for (w = 0; w < AE5_STRIP_WORDS_PER_LED; w++) {
-				*dst++ = cpu_to_le32(enc_words[w]);
-				word_idx++;
-			}
-		}
-		/* Zero reset gap (> 1.8 ms; WS2812 requires > 50 us low) */
-		for (w = 0; w < AE5_STRIP_RESET_GAP_WORDS; w++) {
-			*dst++ = 0;
-			word_idx++;
-		}
-	}
-	while (word_idx < AE5_STRIP_TOTAL_WORDS) {
+	/* Preamble per Windows CtxHda FUN_00042944 */
+	for (w = 0; w < AE5_STRIP_PREAMBLE_WORDS; w++)
 		*dst++ = 0;
-		word_idx++;
+
+	/* LEDs */
+	for (led = 0; led < num_leds; led++) {
+		ae5_strip_encode_24(grb_colors[led], enc_words);
+		for (w = 0; w < AE5_STRIP_WORDS_PER_LED; w++)
+			*dst++ = cpu_to_le32(enc_words[w]);
 	}
+
+	/* Zero reset gap (> 1.8 ms; WS2812 requires > 50 us low) */
+	for (w = 0; w < AE5_STRIP_RESET_GAP_WORDS; w++)
+		*dst++ = 0;
+
 	/* Ensure DMA buffer writes are committed before triggering the HDA stream */
 	wmb();
 
@@ -8572,12 +8559,13 @@ static int ae5_strip_send_frame(struct hda_codec *codec, const u32 *grb_colors,
 	/* 3. Trigger stream transmission on HDA link */
 	snd_hdac_dsp_trigger(hstr, true);
 	/*
-	 * WS2812 latches on the >50us reset gap after one frame. The 32KB buffer holds ~64
-	 * repeating frame+reset cycles; at ~1.4ms/frame a 15ms run transmits ~10 complete
-	 * cycles - more than enough to latch reliably. Keep it short so rapid sysfs writes
-	 * (OpenRGB effects/theme changes) are not serialized behind a 1s blocking sleep.
+	 * WS2812 latches on the >50us reset gap after the frame. At 44.1kHz stereo
+	 * (88.2k words/s), a 100-LED frame + reset gap is 488 words (~5.5ms); a 10-LED
+	 * strip is 128 words (~1.5ms). Sleeping 8-10ms guarantees the entire frame and
+	 * latch reset have completed. The remainder of the 32KB buffer is zero-filled,
+	 * so stopping the stream during the trailing zeros never truncates an active LED.
 	 */
-	usleep_range(15000, 20000);
+	usleep_range(8000, 10000);
 	snd_hdac_dsp_trigger(hstr, false);
 	codec_dbg(codec, "AE5 strip: trigger complete\n");
 
@@ -8704,6 +8692,90 @@ static int ae5_strip_num_leds_put(struct snd_kcontrol *kcontrol,
 	return 1;
 }
 
+static void ae5_oncard_write_bit(struct ca0132_spec *spec, bool bit)
+{
+	writew(bit ? 0x102 : 0x002, spec->mem_base + 0x320);
+	writew(0x103, spec->mem_base + 0x320);
+	writew(0x003, spec->mem_base + 0x320);
+}
+
+/*
+ * Caller must hold spec->ae5_strip_mutex.
+ */
+static void ae5_oncard_send_frame(struct hda_codec *codec, const u8 *rgb_colors)
+{
+	struct ca0132_spec *spec = codec->spec;
+	int i, bit;
+
+	if (!spec || !spec->mem_base)
+		return;
+
+	lockdep_assert_held(&spec->ae5_strip_mutex);
+	CLASS(snd_hda_power_pm, pm)(codec);
+
+	/* Start frame: 32 zeroes */
+	for (i = 0; i < 32; i++)
+		ae5_oncard_write_bit(spec, false);
+
+	/* 5 APA102 LEDs: 8-bit brightness (0xFF), Blue, Green, Red */
+	for (i = 0; i < AE5_ONCARD_LEDS; i++) {
+		u8 r = rgb_colors[i * 3 + 0];
+		u8 g = rgb_colors[i * 3 + 1];
+		u8 b = rgb_colors[i * 3 + 2];
+		u32 val = 0xff000000 | ((u32)b << 16) | ((u32)g << 8) | (u32)r;
+
+		for (bit = 31; bit >= 0; bit--)
+			ae5_oncard_write_bit(spec, (val >> bit) & 1);
+	}
+
+	/* End frame: 32 ones */
+	for (i = 0; i < 32; i++)
+		ae5_oncard_write_bit(spec, true);
+
+	/* Return GPIO pins 2 (data) and 3 (clock) to idle low */
+	ca0113_mmio_gpio_set(codec, 2, false);
+	ca0113_mmio_gpio_set(codec, 3, false);
+}
+
+static int ae5_oncard_ctl_info(struct snd_kcontrol *kcontrol,
+			       struct snd_ctl_elem_info *uinfo)
+{
+	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
+	uinfo->count = AE5_ONCARD_LEDS * 3;
+	return 0;
+}
+
+static int ae5_oncard_ctl_get(struct snd_kcontrol *kcontrol,
+			      struct snd_ctl_elem_value *ucontrol)
+{
+	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
+	struct ca0132_spec *spec = codec->spec;
+
+	guard(mutex)(&spec->ae5_strip_mutex);
+	memcpy(ucontrol->value.bytes.data, spec->ae5_oncard_cur_colors,
+	       AE5_ONCARD_LEDS * 3);
+	return 0;
+}
+
+static int ae5_oncard_ctl_put(struct snd_kcontrol *kcontrol,
+			      struct snd_ctl_elem_value *ucontrol)
+{
+	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
+	struct ca0132_spec *spec = codec->spec;
+	bool change;
+
+	guard(mutex)(&spec->ae5_strip_mutex);
+	change = memcmp(spec->ae5_oncard_cur_colors, ucontrol->value.bytes.data,
+			AE5_ONCARD_LEDS * 3) != 0;
+	if (!change)
+		return 0;
+
+	memcpy(spec->ae5_oncard_cur_colors, ucontrol->value.bytes.data,
+	       AE5_ONCARD_LEDS * 3);
+	ae5_oncard_send_frame(codec, spec->ae5_oncard_cur_colors);
+	return 1;
+}
+
 static const struct snd_kcontrol_new ae5_strip_ctls[] = {
 	{
 		.iface = SNDRV_CTL_ELEM_IFACE_CARD,
@@ -8723,6 +8795,15 @@ static const struct snd_kcontrol_new ae5_strip_ctls[] = {
 		.get = ae5_strip_num_leds_get,
 		.put = ae5_strip_num_leds_put,
 	},
+	{
+		.iface = SNDRV_CTL_ELEM_IFACE_CARD,
+		.name = "AE-5 On-Card LEDs",
+		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
+			  SNDRV_CTL_ELEM_ACCESS_VOLATILE,
+		.info = ae5_oncard_ctl_info,
+		.get = ae5_oncard_ctl_get,
+		.put = ae5_oncard_ctl_put,
+	},
 };
 
 static int ae5_add_strip_controls(struct hda_codec *codec)
-- 
2.56.0



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-16 14:26 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-16 14:02 [PATCH] ALSA: hda/ca0132: Fix AE-5 strip color corruption and add on-card LED control Devin Christensen
2026-11-16 14:17 ` Takashi Iwai
2026-11-16 14:25   ` [PATCH 0/2] ALSA: hda/ca0132: Fix AE-5 strip color glitch and add on-card LEDs Devin Christensen
2026-11-16 14:25     ` [PATCH 1/2] ALSA: hda/ca0132: Fix AE-5 LED strip color corruption during animation Devin Christensen
2026-11-16 14:26     ` [PATCH 2/2] ALSA: hda/ca0132: Add Sound BlasterX AE-5 on-card RGB LED support Devin Christensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®