The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH v1 0/7] Fixes and improvements in CS35L41 HDA driver for System Suspend/Resume
@ 2026-11-20 16:10 Stefan Binding
  2026-11-20 16:10 ` [PATCH v1 1/7] ALSA: hda: cs35l41: Remove redundant lock_system_sleep() calls Stefan Binding
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: Stefan Binding @ 2026-11-20 16:10 UTC (permalink / raw)
  To: Mark Brown, Jaroslav Kysela, Takashi Iwai
  Cc: linux-sound, linux-kernel, patches, Stefan Binding

A number of issues in system suspend and system resume have been reported
on the CS35L41 HDA driver. These patches fix some of the issues, as well
as improve the suspend/resume code in the driver.

The main issues resolved are:
- CS35L41 boot issues after system resume (on SPI systems using S4 hibernation)
- a deadlock during system suspend due to a race condition with the filesystem

Also included is a patch to prevent issues with IRQs during suspend/resume.

In addition, there are 3 patches which prepare the driver for these
improvements and fixes:
- ALSA: hda: cs35l41: Remove redundant lock_system_sleep() calls
- ALSA: hda: cs35l41: Unset irq if not used or configured
- ALSA: hda/cs35l41: Remove support for alternative firmware types

Stefan Binding (7):
  ALSA: hda: cs35l41: Remove redundant lock_system_sleep() calls
  ASoC: cs35l41: Prevent software reset when using SPI
  ALSA: hda: cs35l41: Unset irq if not used or configured
  ALSA: hda/cs35l41: Split System Suspend/Resume callbacks
  ALSA: hda/cs35l41: Remove support for alternative firmware types
  ALSA: hda/cs35l41: Store filenames of successfully loaded firmware and
    tunings
  ALSA: hda/cs35l41: rework how firmware requests are queued and flushed
    to prevent races

 sound/hda/codecs/side-codecs/cs35l41_hda.c | 362 +++++++++++++--------
 sound/hda/codecs/side-codecs/cs35l41_hda.h |   7 +-
 sound/soc/codecs/cs35l41-lib.c             |  51 ++-
 3 files changed, 275 insertions(+), 145 deletions(-)

-- 
2.43.0


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

* [PATCH v1 1/7] ALSA: hda: cs35l41: Remove redundant lock_system_sleep() calls
  2026-11-20 16:10 [PATCH v1 0/7] Fixes and improvements in CS35L41 HDA driver for System Suspend/Resume Stefan Binding
@ 2026-11-20 16:10 ` Stefan Binding
  2026-11-20 16:10 ` [PATCH v1 2/7] ASoC: cs35l41: Prevent software reset when using SPI Stefan Binding
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Stefan Binding @ 2026-11-20 16:10 UTC (permalink / raw)
  To: Mark Brown, Jaroslav Kysela, Takashi Iwai
  Cc: linux-sound, linux-kernel, patches, Stefan Binding

These calls are there to prevent sleep when adding or removing device
links in the bind/unbind calls. However, since these calls occur in
probe and remove calls, where sleep is blocked, this is unnecessary.

Signed-off-by: Stefan Binding <sbinding@opensource.cirrus.com>
---
 sound/hda/codecs/side-codecs/cs35l41_hda.c | 6 ------
 1 file changed, 6 deletions(-)

diff --git a/sound/hda/codecs/side-codecs/cs35l41_hda.c b/sound/hda/codecs/side-codecs/cs35l41_hda.c
index 919630c11018..f6910916ad47 100644
--- a/sound/hda/codecs/side-codecs/cs35l41_hda.c
+++ b/sound/hda/codecs/side-codecs/cs35l41_hda.c
@@ -1443,7 +1443,6 @@ static int cs35l41_hda_bind(struct device *dev, struct device *master, void *mas
 	struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
 	struct hda_component_parent *parent = master_data;
 	struct hda_component *comp;
-	unsigned int sleep_flags;
 	int ret = 0;
 
 	comp = hda_component_from_index(parent, cs35l41->index);
@@ -1492,10 +1491,8 @@ static int cs35l41_hda_bind(struct device *dev, struct device *master, void *mas
 
 	mutex_unlock(&cs35l41->fw_mutex);
 
-	sleep_flags = lock_system_sleep();
 	if (!device_link_add(&cs35l41->codec->core.dev, cs35l41->dev, DL_FLAG_STATELESS))
 		dev_warn(dev, "Unable to create device link\n");
-	unlock_system_sleep(sleep_flags);
 
 	dev_info(cs35l41->dev,
 		 "CS35L41 Bound - SSID: %s, BST: %d, VSPK: %d, CH: %c, FW EN: %d, SPKID: %d\n",
@@ -1512,16 +1509,13 @@ static void cs35l41_hda_unbind(struct device *dev, struct device *master, void *
 	struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
 	struct hda_component_parent *parent = master_data;
 	struct hda_component *comp;
-	unsigned int sleep_flags;
 
 	comp = hda_component_from_index(parent, cs35l41->index);
 	if (!comp)
 		return;
 
 	if (comp->dev == dev) {
-		sleep_flags = lock_system_sleep();
 		device_link_remove(&cs35l41->codec->core.dev, cs35l41->dev);
-		unlock_system_sleep(sleep_flags);
 		memset(comp, 0, sizeof(*comp));
 
 		cs35l41_remove_controls(cs35l41);
-- 
2.43.0


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

* [PATCH v1 2/7] ASoC: cs35l41: Prevent software reset when using SPI
  2026-11-20 16:10 [PATCH v1 0/7] Fixes and improvements in CS35L41 HDA driver for System Suspend/Resume Stefan Binding
  2026-11-20 16:10 ` [PATCH v1 1/7] ALSA: hda: cs35l41: Remove redundant lock_system_sleep() calls Stefan Binding
@ 2026-11-20 16:10 ` Stefan Binding
  2026-11-20 16:10 ` [PATCH v1 3/7] ALSA: hda: cs35l41: Unset irq if not used or configured Stefan Binding
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Stefan Binding @ 2026-11-20 16:10 UTC (permalink / raw)
  To: Mark Brown, Jaroslav Kysela, Takashi Iwai
  Cc: linux-sound, linux-kernel, patches, Stefan Binding

If there is any SPI Bus activity during a software reset, even if the
chip select is not set for that Amp, the amp may not boot.

This affects both CS35L41 ASoC and HDA drivers. For HDA this also
affects system resume.

Whilst the driver needs the amp to be reset when coming out of system
suspend, the driver will always toggle the reset GPIO off and on to
ensure this occurs. If a reset GPIO is not defined for the amplifier,
the amplifier will not probe - unless the amp is using a shared reset.
In the case of a shared reset, the GPIO will control resets for both
amps anyway.

Link: https://bugzilla.kernel.org/show_bug.cgi?id=221161

Signed-off-by: Stefan Binding <sbinding@opensource.cirrus.com>
---
 sound/soc/codecs/cs35l41-lib.c | 51 ++++++++++++++++++++++++----------
 1 file changed, 36 insertions(+), 15 deletions(-)

diff --git a/sound/soc/codecs/cs35l41-lib.c b/sound/soc/codecs/cs35l41-lib.c
index 9e6bed22fbdb..0ea6e9c87da1 100644
--- a/sound/soc/codecs/cs35l41-lib.c
+++ b/sound/soc/codecs/cs35l41-lib.c
@@ -13,6 +13,7 @@
 #include <linux/regulator/consumer.h>
 #include <linux/slab.h>
 #include <linux/firmware/cirrus/wmfw.h>
+#include <linux/spi/spi.h>
 
 #include <sound/cs35l41.h>
 
@@ -1588,6 +1589,18 @@ int cs35l41_exit_hibernate(struct device *dev, struct regmap *regmap)
 }
 EXPORT_SYMBOL_GPL(cs35l41_exit_hibernate);
 
+static bool cs35l41_is_spi_regmap(struct regmap *regmap)
+{
+	struct device *dev = regmap_get_device(regmap);
+
+	if (!dev) {
+		dev_err(dev, "Failed to get device from regmap\n");
+		return false;
+	}
+
+	return (IS_ENABLED(CONFIG_SPI) && dev->bus == &spi_bus_type);
+}
+
 int cs35l41_wait_boot_done(struct device *dev, struct regmap *regmap)
 {
 	ktime_t timeout;
@@ -1627,27 +1640,35 @@ int cs35l41_reset_and_boot(struct device *dev, struct regmap *regmap)
 {
 	int ret;
 
-	ret = regmap_write(regmap, CS35L41_SFT_RESET, CS35L41_SOFTWARE_RESET);
-	if (ret) {
-		dev_warn(dev, "Failed to write software reset: %d\n", ret);
-
+	if (cs35l41_is_spi_regmap(regmap)) {
 		/*
-		 * If the write failed, it is likely because the chip is still booting after a
-		 * reset. Wait for boot, then retry the software reset. If the initial write failed
-		 * for another reason, then either waiting for boot, or the second write will fail,
-		 * So we can return the error then.
+		 * If there is any SPI Bus activity during a software reset, even if the
+		 * chip select is not set for that Amp, the amp may not boot.
 		 */
-		ret = cs35l41_wait_boot_done(dev, regmap);
-		if (ret)
-			return ret;
-
+		dev_dbg(dev, "Preventing Software Reset on SPI\n");
+	} else {
 		ret = regmap_write(regmap, CS35L41_SFT_RESET, CS35L41_SOFTWARE_RESET);
 		if (ret) {
-			dev_err(dev, "Failed to write software reset on retry: %d\n", ret);
-			return ret;
+			dev_warn(dev, "Failed to write software reset: %d\n", ret);
+
+			/*
+			 * If the write failed, it is likely because the chip is still booting after
+			 * a reset. Wait for boot, then retry the software reset. If the initial
+			 * write failed for another reason, then either waiting for boot, or the
+			 * second write will fail, so we can return the error then.
+			 */
+			ret = cs35l41_wait_boot_done(dev, regmap);
+			if (ret)
+				return ret;
+
+			ret = regmap_write(regmap, CS35L41_SFT_RESET, CS35L41_SOFTWARE_RESET);
+			if (ret) {
+				dev_err(dev, "Failed to write software reset on retry: %d\n", ret);
+				return ret;
+			}
 		}
+		usleep_range(2000, 2100);
 	}
-	usleep_range(2000, 2100);
 
 	return cs35l41_wait_boot_done(dev, regmap);
 }
-- 
2.43.0


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

* [PATCH v1 3/7] ALSA: hda: cs35l41: Unset irq if not used or configured
  2026-11-20 16:10 [PATCH v1 0/7] Fixes and improvements in CS35L41 HDA driver for System Suspend/Resume Stefan Binding
  2026-11-20 16:10 ` [PATCH v1 1/7] ALSA: hda: cs35l41: Remove redundant lock_system_sleep() calls Stefan Binding
  2026-11-20 16:10 ` [PATCH v1 2/7] ASoC: cs35l41: Prevent software reset when using SPI Stefan Binding
@ 2026-11-20 16:10 ` Stefan Binding
  2026-11-20 16:10 ` [PATCH v1 4/7] ALSA: hda/cs35l41: Split System Suspend/Resume callbacks Stefan Binding
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Stefan Binding @ 2026-11-20 16:10 UTC (permalink / raw)
  To: Mark Brown, Jaroslav Kysela, Takashi Iwai
  Cc: linux-sound, linux-kernel, patches, Stefan Binding

If the irq is not allocated, or does not exist in ACPI, clear the saved
irq value. This prevents the driver from being able to access an
unconfigured or unused irq later.

Signed-off-by: Stefan Binding <sbinding@opensource.cirrus.com>
---
 sound/hda/codecs/side-codecs/cs35l41_hda.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/sound/hda/codecs/side-codecs/cs35l41_hda.c b/sound/hda/codecs/side-codecs/cs35l41_hda.c
index f6910916ad47..2e2bf231d3fc 100644
--- a/sound/hda/codecs/side-codecs/cs35l41_hda.c
+++ b/sound/hda/codecs/side-codecs/cs35l41_hda.c
@@ -1633,6 +1633,8 @@ static void cs35l41_configure_interrupt(struct cs35l41_hda *cs35l41, int irq_pol
 					IRQF_ONESHOT | IRQF_SHARED | irq_pol,
 					0, &cs35l41_regmap_irq_chip, &cs35l41->irq_data);
 	if (ret) {
+		/* unset irq if unable to be used */
+		cs35l41->irq = 0;
 		dev_dbg(cs35l41->dev, "Unable to add IRQ Chip: %d.", ret);
 		goto err;
 	}
@@ -1712,6 +1714,9 @@ static int cs35l41_hda_apply_properties(struct cs35l41_hda *cs35l41)
 
 	if (using_irq)
 		cs35l41_configure_interrupt(cs35l41, irq_pol);
+	else
+		/* Unset irq if not used */
+		cs35l41->irq = 0;
 
 	return cs35l41_hda_channel_map(cs35l41);
 }
-- 
2.43.0


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

* [PATCH v1 4/7] ALSA: hda/cs35l41: Split System Suspend/Resume callbacks
  2026-11-20 16:10 [PATCH v1 0/7] Fixes and improvements in CS35L41 HDA driver for System Suspend/Resume Stefan Binding
                   ` (2 preceding siblings ...)
  2026-11-20 16:10 ` [PATCH v1 3/7] ALSA: hda: cs35l41: Unset irq if not used or configured Stefan Binding
@ 2026-11-20 16:10 ` Stefan Binding
  2026-11-20 16:10 ` [PATCH v1 5/7] ALSA: hda/cs35l41: Remove support for alternative firmware types Stefan Binding
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Stefan Binding @ 2026-11-20 16:10 UTC (permalink / raw)
  To: Mark Brown, Jaroslav Kysela, Takashi Iwai
  Cc: linux-sound, linux-kernel, patches, Stefan Binding

Currently, the CS35L41 driver does most of its system suspend and
resume functions inside single suspend and resume callbacks.

Ensure IRQs are correctly disabled and enabled at the right times
during the System Suspend and Resume process, to ensure we don't get
spurious interrupts which cause issues.

Signed-off-by: Stefan Binding <sbinding@opensource.cirrus.com>
---
 sound/hda/codecs/side-codecs/cs35l41_hda.c | 91 ++++++++++++++++++----
 1 file changed, 78 insertions(+), 13 deletions(-)

diff --git a/sound/hda/codecs/side-codecs/cs35l41_hda.c b/sound/hda/codecs/side-codecs/cs35l41_hda.c
index 2e2bf231d3fc..1a192749c7b0 100644
--- a/sound/hda/codecs/side-codecs/cs35l41_hda.c
+++ b/sound/hda/codecs/side-codecs/cs35l41_hda.c
@@ -942,10 +942,8 @@ static int cs35l41_system_suspend(struct device *dev)
 
 	dev_dbg(cs35l41->dev, "System Suspend\n");
 
-	if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH) {
-		dev_err_once(cs35l41->dev, "System Suspend not supported\n");
+	if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH)
 		return 0; /* don't block the whole system suspend */
-	}
 
 	scoped_guard(mutex, &cs35l41->fw_mutex) {
 		if (cs35l41->playback_started)
@@ -963,15 +961,75 @@ static int cs35l41_system_suspend(struct device *dev)
 	if (ret)
 		dev_err(dev, "System Suspend Failed, not ready for Reset: %d\n", ret);
 
+
+	/*
+	 * The interrupt line is normally shared, but after we start suspending
+	 * we can't check if our device is the source of an interrupt, and can't
+	 * clear it. Prevent this race by temporarily disabling the parent irq
+	 * until we reach _noirq.
+	 */
+	if (cs35l41->irq)
+		disable_irq(cs35l41->irq);
+
+	dev_dbg(cs35l41->dev, "System Suspended\n");
+
+	return ret;
+}
+
+static int cs35l41_system_suspend_late(struct device *dev)
+{
+	struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
+
+	if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH)
+		return 0; /* don't block the whole system suspend */
+
+	/*
+	 * RESET is usually shared by all amps so it must not be asserted until
+	 * all driver instances have done their suspend() stage.
+	 */
 	if (cs35l41->reset_gpio) {
 		dev_info(cs35l41->dev, "Asserting Reset\n");
 		gpiod_set_value_cansleep(cs35l41->reset_gpio, 0);
 		usleep_range(2000, 2100);
 	}
 
-	dev_dbg(cs35l41->dev, "System Suspended\n");
+	return 0;
+}
 
-	return ret;
+static int cs35l41_system_suspend_noirq(struct device *dev)
+{
+	struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
+
+	if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH)
+		return 0; /* don't block the whole system suspend */
+
+	/* Handlers are now disabled so the parent IRQ can safely be re-enabled. */
+	if (cs35l41->irq)
+		enable_irq(cs35l41->irq);
+
+	return 0;
+}
+
+static int cs35l41_system_resume_noirq(struct device *dev)
+{
+	struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
+
+	if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH) {
+		dev_err_once(cs35l41->dev, "System Resume not supported\n");
+		return 0; /* don't block the whole system resume */
+	}
+
+	/*
+	 * WAKE interrupts unmask if the CS35L41 resets, which can cause
+	 * spurious interrupts, and the interrupt line is normally shared.
+	 * We can't check if our device is the source of an interrupt, and can't
+	 * clear it, until it has fully resumed. Prevent this race by temporarily
+	 * disabling the parent irq until we complete resume().
+	 */
+	if (cs35l41->irq)
+		disable_irq(cs35l41->irq);
+
+	return 0;
 }
 
 static int cs35l41_system_resume(struct device *dev)
@@ -981,28 +1039,32 @@ static int cs35l41_system_resume(struct device *dev)
 
 	dev_dbg(cs35l41->dev, "System Resume\n");
 
-	if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH) {
-		dev_err_once(cs35l41->dev, "System Resume not supported\n");
+	if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH)
 		return 0; /* don't block the whole system resume */
-	}
 
 	if (cs35l41->reset_gpio) {
+		dev_info(cs35l41->dev, "Deasserting Reset\n");
 		gpiod_set_value_cansleep(cs35l41->reset_gpio, 0);
 		usleep_range(2000, 2100);
 		gpiod_set_value_cansleep(cs35l41->reset_gpio, 1);
+		usleep_range(2000, 2100);
 	}
 
-	usleep_range(2000, 2100);
-
 	regcache_cache_only(cs35l41->regmap, false);
 
 	ret = cs35l41_reset_and_boot(cs35l41->dev, cs35l41->regmap);
-	if (ret)
-		return ret;
 
 	regcache_cache_only(cs35l41->regmap, true);
+	if (ret) {
+		dev_err(cs35l41->dev, "System Resume Failed: Boot Failed: %d\n", ret);
+		if (cs35l41->irq)
+			enable_irq(cs35l41->irq);
+		return ret;
+	}
 
 	ret = pm_runtime_force_resume(dev);
+	if (cs35l41->irq)
+		enable_irq(cs35l41->irq);
 	if (ret) {
 		dev_err(dev, "System Resume Failed: Unable to runtime resume: %d\n", ret);
 		return ret;
@@ -1015,7 +1077,7 @@ static int cs35l41_system_resume(struct device *dev)
 		schedule_work(&cs35l41->fw_load_work);
 	}
 
-	return ret;
+	return 0;
 }
 
 static int cs35l41_runtime_idle(struct device *dev)
@@ -2083,6 +2145,9 @@ const struct dev_pm_ops cs35l41_hda_pm_ops = {
 		       cs35l41_runtime_idle)
 	.prepare = cs35l41_system_suspend_prep,
 	SYSTEM_SLEEP_PM_OPS(cs35l41_system_suspend, cs35l41_system_resume)
+	LATE_SYSTEM_SLEEP_PM_OPS(cs35l41_system_suspend_late, NULL)
+	NOIRQ_SYSTEM_SLEEP_PM_OPS(cs35l41_system_suspend_noirq,
+				  cs35l41_system_resume_noirq)
 };
 EXPORT_SYMBOL_NS_GPL(cs35l41_hda_pm_ops, "SND_HDA_SCODEC_CS35L41");
 
-- 
2.43.0


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

* [PATCH v1 5/7] ALSA: hda/cs35l41: Remove support for alternative firmware types
  2026-11-20 16:10 [PATCH v1 0/7] Fixes and improvements in CS35L41 HDA driver for System Suspend/Resume Stefan Binding
                   ` (3 preceding siblings ...)
  2026-11-20 16:10 ` [PATCH v1 4/7] ALSA: hda/cs35l41: Split System Suspend/Resume callbacks Stefan Binding
@ 2026-11-20 16:10 ` Stefan Binding
  2026-11-20 16:10 ` [PATCH v1 6/7] ALSA: hda/cs35l41: Store filenames of successfully loaded firmware and tunings Stefan Binding
  2026-11-20 16:10 ` [PATCH v1 7/7] ALSA: hda/cs35l41: rework how firmware requests are queued and flushed to prevent races Stefan Binding
  6 siblings, 0 replies; 8+ messages in thread
From: Stefan Binding @ 2026-11-20 16:10 UTC (permalink / raw)
  To: Mark Brown, Jaroslav Kysela, Takashi Iwai
  Cc: linux-sound, linux-kernel, patches, Stefan Binding

Originally the driver has support for loading alternative firmware
types. However, whilst it could load alternative types, only speaker
protection was ever actually supported in the driver.

Remove this support to ensure that the firmware type is fixed.
This is preparing the driver to make proper use of firmware caching in
later commits.

Signed-off-by: Stefan Binding <sbinding@opensource.cirrus.com>
---
 sound/hda/codecs/side-codecs/cs35l41_hda.c | 86 ++--------------------
 sound/hda/codecs/side-codecs/cs35l41_hda.h |  2 -
 2 files changed, 8 insertions(+), 80 deletions(-)

diff --git a/sound/hda/codecs/side-codecs/cs35l41_hda.c b/sound/hda/codecs/side-codecs/cs35l41_hda.c
index 1a192749c7b0..a0e2e0c5addf 100644
--- a/sound/hda/codecs/side-codecs/cs35l41_hda.c
+++ b/sound/hda/codecs/side-codecs/cs35l41_hda.c
@@ -37,6 +37,7 @@
 #define CS35L41_DSM_GET_MUTE		5
 #define CS35L41_NOTIFY_EVENT		0x91
 #define CS35L41_TUNING_SIG		0x109A4A35
+#define CS35L41_HDA_FW_ID		"spk-prot"
 
 enum cs35l41_tuning_param_types {
 	TUNING_PARAM_GAIN,
@@ -73,21 +74,6 @@ static const struct cirrus_amp_cal_controls cs35l41_calibration_controls = {
 	.checksum =	CAL_CHECKSUM_DSP_CTL_NAME,
 };
 
-enum cs35l41_hda_fw_id {
-	CS35L41_HDA_FW_SPK_PROT,
-	CS35L41_HDA_FW_SPK_CALI,
-	CS35L41_HDA_FW_SPK_DIAG,
-	CS35L41_HDA_FW_MISC,
-	CS35L41_HDA_NUM_FW
-};
-
-static const char * const cs35l41_hda_fw_ids[CS35L41_HDA_NUM_FW] = {
-	[CS35L41_HDA_FW_SPK_PROT] = "spk-prot",
-	[CS35L41_HDA_FW_SPK_CALI] = "spk-cali",
-	[CS35L41_HDA_FW_SPK_DIAG] = "spk-diag",
-	[CS35L41_HDA_FW_MISC] =     "misc",
-};
-
 static bool firmware_autostart = 1;
 module_param(firmware_autostart, bool, 0444);
 MODULE_PARM_DESC(firmware_autostart, "Allow automatic firmware download on boot"
@@ -173,24 +159,19 @@ static int cs35l41_request_firmware_file(struct cs35l41_hda *cs35l41,
 	*firmware = NULL;
 	if (spkid > -1 && ssid && amp_name)
 		*filename = kasprintf(GFP_KERNEL, "cirrus/%s-%s-%s-%s-spkid%d-%s.%s", CS35L41_PART,
-				      dsp_name, cs35l41_hda_fw_ids[cs35l41->firmware_type],
-				      ssid, spkid, amp_name, filetype);
+				      dsp_name, CS35L41_HDA_FW_ID, ssid, spkid, amp_name, filetype);
 	else if (spkid > -1 && ssid)
 		*filename = kasprintf(GFP_KERNEL, "cirrus/%s-%s-%s-%s-spkid%d.%s", CS35L41_PART,
-				      dsp_name, cs35l41_hda_fw_ids[cs35l41->firmware_type],
-				      ssid, spkid, filetype);
+				      dsp_name, CS35L41_HDA_FW_ID, ssid, spkid, filetype);
 	else if (ssid && amp_name)
 		*filename = kasprintf(GFP_KERNEL, "cirrus/%s-%s-%s-%s-%s.%s", CS35L41_PART,
-				      dsp_name, cs35l41_hda_fw_ids[cs35l41->firmware_type],
-				      ssid, amp_name, filetype);
+				      dsp_name, CS35L41_HDA_FW_ID, ssid, amp_name, filetype);
 	else if (ssid)
 		*filename = kasprintf(GFP_KERNEL, "cirrus/%s-%s-%s-%s.%s", CS35L41_PART,
-				      dsp_name, cs35l41_hda_fw_ids[cs35l41->firmware_type],
-				      ssid, filetype);
+				      dsp_name, CS35L41_HDA_FW_ID, ssid, filetype);
 	else
 		*filename = kasprintf(GFP_KERNEL, "cirrus/%s-%s-%s.%s", CS35L41_PART,
-				      dsp_name, cs35l41_hda_fw_ids[cs35l41->firmware_type],
-				      filetype);
+				      dsp_name, CS35L41_HDA_FW_ID, filetype);
 
 	if (*filename == NULL)
 		return -ENOMEM;
@@ -589,7 +570,7 @@ static int cs35l41_init_dsp(struct cs35l41_hda *cs35l41)
 	}
 
 	ret = cs_dsp_power_up(dsp, wmfw_firmware, wmfw_filename, coeff_firmware, coeff_filename,
-			      cs35l41_hda_fw_ids[cs35l41->firmware_type]);
+			      CS35L41_HDA_FW_ID);
 	if (ret) {
 		cs35l41_set_default_tuning_params(cs35l41);
 		return ret;
@@ -1237,7 +1218,7 @@ static int cs35l41_smart_amp(struct cs35l41_hda *cs35l41)
 	}
 
 	dev_info(cs35l41->dev, "Firmware Loaded - Type: %s, Gain: %d\n",
-		 cs35l41_hda_fw_ids[cs35l41->firmware_type], cs35l41->tuning_gain);
+		 CS35L41_HDA_FW_ID, cs35l41->tuning_gain);
 
 	return 0;
 
@@ -1319,38 +1300,6 @@ static int cs35l41_fw_load_ctl_put(struct snd_kcontrol *kcontrol,
 	return 1;
 }
 
-static int cs35l41_fw_type_ctl_get(struct snd_kcontrol *kcontrol,
-				   struct snd_ctl_elem_value *ucontrol)
-{
-	struct cs35l41_hda *cs35l41 = snd_kcontrol_chip(kcontrol);
-
-	ucontrol->value.enumerated.item[0] = cs35l41->firmware_type;
-
-	return 0;
-}
-
-static int cs35l41_fw_type_ctl_put(struct snd_kcontrol *kcontrol,
-				   struct snd_ctl_elem_value *ucontrol)
-{
-	struct cs35l41_hda *cs35l41 = snd_kcontrol_chip(kcontrol);
-
-	if (ucontrol->value.enumerated.item[0] < CS35L41_HDA_NUM_FW) {
-		if (cs35l41->firmware_type != ucontrol->value.enumerated.item[0]) {
-			cs35l41->firmware_type = ucontrol->value.enumerated.item[0];
-			return 1;
-		} else {
-			return 0;
-		}
-	}
-
-	return -EINVAL;
-}
-
-static int cs35l41_fw_type_ctl_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
-{
-	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(cs35l41_hda_fw_ids), cs35l41_hda_fw_ids);
-}
-
 static void cs35l41_remove_controls(struct cs35l41_hda *cs35l41)
 {
 	if (!cs35l41->codec)
@@ -1361,9 +1310,6 @@ static void cs35l41_remove_controls(struct cs35l41_hda *cs35l41)
 
 	snd_ctl_remove(cs35l41->codec->card, cs35l41->fw_load_ctl);
 	cs35l41->fw_load_ctl = NULL;
-
-	snd_ctl_remove(cs35l41->codec->card, cs35l41->fw_type_ctl);
-	cs35l41->fw_type_ctl = NULL;
 }
 
 static int cs35l41_add_control(struct cs35l41_hda *cs35l41,
@@ -1390,16 +1336,8 @@ static int cs35l41_add_control(struct cs35l41_hda *cs35l41,
 
 static int cs35l41_create_controls(struct cs35l41_hda *cs35l41)
 {
-	char fw_type_ctl_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
 	char fw_load_ctl_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
 	char mute_override_ctl_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
-	struct snd_kcontrol_new fw_type_ctl = {
-		.name = fw_type_ctl_name,
-		.iface = SNDRV_CTL_ELEM_IFACE_CARD,
-		.info = cs35l41_fw_type_ctl_info,
-		.get = cs35l41_fw_type_ctl_get,
-		.put = cs35l41_fw_type_ctl_put,
-	};
 	struct snd_kcontrol_new fw_load_ctl = {
 		.name = fw_load_ctl_name,
 		.iface = SNDRV_CTL_ELEM_IFACE_CARD,
@@ -1416,17 +1354,11 @@ static int cs35l41_create_controls(struct cs35l41_hda *cs35l41)
 	};
 	int ret;
 
-	scnprintf(fw_type_ctl_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s DSP1 Firmware Type",
-		  cs35l41->amp_name);
 	scnprintf(fw_load_ctl_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s DSP1 Firmware Load",
 		  cs35l41->amp_name);
 	scnprintf(mute_override_ctl_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s Forced Mute Status",
 		  cs35l41->amp_name);
 
-	ret = cs35l41_add_control(cs35l41, &fw_type_ctl, &cs35l41->fw_type_ctl);
-	if (ret)
-		goto err;
-
 	ret = cs35l41_add_control(cs35l41, &fw_load_ctl, &cs35l41->fw_load_ctl);
 	if (ret)
 		goto err;
@@ -1526,8 +1458,6 @@ static int cs35l41_hda_bind(struct device *dev, struct device *master, void *mas
 
 	strscpy(comp->name, dev_name(dev), sizeof(comp->name));
 
-	cs35l41->firmware_type = CS35L41_HDA_FW_SPK_PROT;
-
 	if (firmware_autostart) {
 		dev_dbg(cs35l41->dev, "Firmware Autostart.\n");
 		cs35l41->request_fw_load = true;
diff --git a/sound/hda/codecs/side-codecs/cs35l41_hda.h b/sound/hda/codecs/side-codecs/cs35l41_hda.h
index 56ec07c0bb74..6fea4b2ad0ac 100644
--- a/sound/hda/codecs/side-codecs/cs35l41_hda.h
+++ b/sound/hda/codecs/side-codecs/cs35l41_hda.h
@@ -73,11 +73,9 @@ struct cs35l41_hda {
 	unsigned volatile long irq_errors;
 	const char *amp_name;
 	const char *acpi_subsystem_id;
-	int firmware_type;
 	int speaker_id;
 	struct mutex fw_mutex;
 	struct work_struct fw_load_work;
-	struct snd_kcontrol *fw_type_ctl;
 	struct snd_kcontrol *fw_load_ctl;
 	struct snd_kcontrol *mute_override_ctl;
 
-- 
2.43.0


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

* [PATCH v1 6/7] ALSA: hda/cs35l41: Store filenames of successfully loaded firmware and tunings
  2026-11-20 16:10 [PATCH v1 0/7] Fixes and improvements in CS35L41 HDA driver for System Suspend/Resume Stefan Binding
                   ` (4 preceding siblings ...)
  2026-11-20 16:10 ` [PATCH v1 5/7] ALSA: hda/cs35l41: Remove support for alternative firmware types Stefan Binding
@ 2026-11-20 16:10 ` Stefan Binding
  2026-11-20 16:10 ` [PATCH v1 7/7] ALSA: hda/cs35l41: rework how firmware requests are queued and flushed to prevent races Stefan Binding
  6 siblings, 0 replies; 8+ messages in thread
From: Stefan Binding @ 2026-11-20 16:10 UTC (permalink / raw)
  To: Mark Brown, Jaroslav Kysela, Takashi Iwai
  Cc: linux-sound, linux-kernel, patches, Stefan Binding

When the CS35L41 HDA driver loads firmware, it typically loops through
possible filenames, requesting each and stopping when it finds a valid
file.

Since firmware is sometimes reloaded - such as during system resume,
it loops through the possible filenames each time. However, since the
filename will never change, we can store the filename when we
successfully load the firmware for the first time, and reuse it later.

This ensures that the only firmware files requested by the driver in
system resume are the same as the ones cached during suspend, since
only successfully requested firmware files are cached. This prevents
unnecessary file access during resume - if a cached firmware is
requested, the cached file is used, eliminating any potential blocking
calls to the filesystem.

Signed-off-by: Stefan Binding <sbinding@opensource.cirrus.com>
---
 sound/hda/codecs/side-codecs/cs35l41_hda.c | 47 +++++++++++++++++-----
 sound/hda/codecs/side-codecs/cs35l41_hda.h |  3 ++
 2 files changed, 40 insertions(+), 10 deletions(-)

diff --git a/sound/hda/codecs/side-codecs/cs35l41_hda.c b/sound/hda/codecs/side-codecs/cs35l41_hda.c
index a0e2e0c5addf..6f4b2088c5fe 100644
--- a/sound/hda/codecs/side-codecs/cs35l41_hda.c
+++ b/sound/hda/codecs/side-codecs/cs35l41_hda.c
@@ -142,6 +142,7 @@ static int cs35l41_request_tuning_param_file(struct cs35l41_hda *cs35l41, char *
 		dev_dbg(cs35l41->dev, "Failed to request '%s'\n", *filename);
 		kfree(*filename);
 		*filename = NULL;
+		cs35l41->skip_tuning_params = true;
 	}
 
 	return ret;
@@ -308,6 +309,7 @@ static int cs35l41_fallback_firmware_file(struct cs35l41_hda *cs35l41,
 	if (ret) {
 		release_firmware(*wmfw_firmware);
 		kfree(*wmfw_filename);
+		*wmfw_filename = NULL;
 		goto err;
 	}
 	return 0;
@@ -325,6 +327,25 @@ static int cs35l41_request_firmware_files(struct cs35l41_hda *cs35l41,
 {
 	int ret;
 
+	if (*wmfw_filename) {
+		ret = firmware_request_nowarn(wmfw_firmware, *wmfw_filename, cs35l41->dev);
+		if (ret != 0) {
+			dev_err(cs35l41->dev, "Failed to request known file '%s'\n",
+				*wmfw_filename);
+			return ret;
+		}
+		if (*coeff_filename) {
+			ret = firmware_request_nowarn(coeff_firmware, *coeff_filename,
+						      cs35l41->dev);
+			if (ret != 0) {
+				dev_err(cs35l41->dev, "Failed to request known file '%s'\n",
+					*coeff_filename);
+				return ret;
+			}
+		}
+		return 0;
+	}
+
 	if (cs35l41->speaker_id > -1) {
 		ret = cs35l41_request_firmware_files_spkid(cs35l41, wmfw_firmware, wmfw_filename,
 							   coeff_firmware, coeff_filename);
@@ -374,6 +395,7 @@ static int cs35l41_request_firmware_files(struct cs35l41_hda *cs35l41,
 coeff_err:
 	release_firmware(*wmfw_firmware);
 	kfree(*wmfw_filename);
+	*wmfw_filename = NULL;
 fallback:
 	return cs35l41_fallback_firmware_file(cs35l41, wmfw_firmware, wmfw_filename,
 					      coeff_firmware, coeff_filename);
@@ -514,6 +536,9 @@ static int cs35l41_load_tuning_params(struct cs35l41_hda *cs35l41, char *tuning_
 	char *tuning_param_filename __free(kfree) = NULL;
 	int ret;
 
+	if (cs35l41->skip_tuning_params)
+		return 0;
+
 	ret = cs35l41_request_tuning_param_file(cs35l41, tuning_filename, &tuning_param_file,
 						&tuning_param_filename, cs35l41->acpi_subsystem_id);
 	if (ret) {
@@ -538,8 +563,6 @@ static int cs35l41_init_dsp(struct cs35l41_hda *cs35l41)
 	const struct firmware *coeff_firmware __free(firmware) = NULL;
 	const struct firmware *wmfw_firmware __free(firmware) = NULL;
 	struct cs_dsp *dsp = &cs35l41->cs_dsp;
-	char *coeff_filename __free(kfree) = NULL;
-	char *wmfw_filename __free(kfree) = NULL;
 	int ret;
 
 	if (!cs35l41->halo_initialized) {
@@ -554,23 +577,23 @@ static int cs35l41_init_dsp(struct cs35l41_hda *cs35l41)
 
 	cs35l41_set_default_tuning_params(cs35l41);
 
-	ret = cs35l41_request_firmware_files(cs35l41, &wmfw_firmware, &wmfw_filename,
-					     &coeff_firmware, &coeff_filename);
+	ret = cs35l41_request_firmware_files(cs35l41, &wmfw_firmware, &cs35l41->wmfw_filename,
+					     &coeff_firmware, &cs35l41->coeff_filename);
 	if (ret < 0)
 		return ret;
 
-	dev_dbg(cs35l41->dev, "Loading WMFW Firmware: %s\n", wmfw_filename);
-	if (coeff_filename) {
-		dev_dbg(cs35l41->dev, "Loading Coefficient File: %s\n", coeff_filename);
-		ret = cs35l41_load_tuning_params(cs35l41, coeff_filename);
+	dev_dbg(cs35l41->dev, "Loading WMFW Firmware: %s\n", cs35l41->wmfw_filename);
+	if (cs35l41->coeff_filename) {
+		dev_dbg(cs35l41->dev, "Loading Coefficient File: %s\n", cs35l41->coeff_filename);
+		ret = cs35l41_load_tuning_params(cs35l41, cs35l41->coeff_filename);
 		if (ret)
 			dev_warn(cs35l41->dev, "Unable to load Tuning Parameters: %d\n", ret);
 	} else {
 		dev_warn(cs35l41->dev, "No Coefficient File available.\n");
 	}
 
-	ret = cs_dsp_power_up(dsp, wmfw_firmware, wmfw_filename, coeff_firmware, coeff_filename,
-			      CS35L41_HDA_FW_ID);
+	ret = cs_dsp_power_up(dsp, wmfw_firmware, cs35l41->wmfw_filename, coeff_firmware,
+			      cs35l41->coeff_filename, CS35L41_HDA_FW_ID);
 	if (ret) {
 		cs35l41_set_default_tuning_params(cs35l41);
 		return ret;
@@ -600,6 +623,10 @@ static void cs35l41_remove_dsp(struct cs35l41_hda *cs35l41)
 	cs35l41_shutdown_dsp(cs35l41);
 	cs_dsp_remove(dsp);
 	cs35l41->halo_initialized = false;
+	kfree(cs35l41->wmfw_filename);
+	kfree(cs35l41->coeff_filename);
+	cs35l41->wmfw_filename = NULL;
+	cs35l41->coeff_filename = NULL;
 }
 
 /* Protection release cycle to get the speaker out of Safe-Mode */
diff --git a/sound/hda/codecs/side-codecs/cs35l41_hda.h b/sound/hda/codecs/side-codecs/cs35l41_hda.h
index 6fea4b2ad0ac..64ee70c31a43 100644
--- a/sound/hda/codecs/side-codecs/cs35l41_hda.h
+++ b/sound/hda/codecs/side-codecs/cs35l41_hda.h
@@ -93,7 +93,10 @@ struct cs35l41_hda {
 	unsigned int tuning_gain;
 	struct cirrus_amp_cal_data cal_data;
 	bool cal_data_valid;
+	bool skip_tuning_params;
 
+	char *coeff_filename;
+	char *wmfw_filename;
 };
 
 enum halo_state {
-- 
2.43.0


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

* [PATCH v1 7/7] ALSA: hda/cs35l41: rework how firmware requests are queued and flushed to prevent races
  2026-11-20 16:10 [PATCH v1 0/7] Fixes and improvements in CS35L41 HDA driver for System Suspend/Resume Stefan Binding
                   ` (5 preceding siblings ...)
  2026-11-20 16:10 ` [PATCH v1 6/7] ALSA: hda/cs35l41: Store filenames of successfully loaded firmware and tunings Stefan Binding
@ 2026-11-20 16:10 ` Stefan Binding
  6 siblings, 0 replies; 8+ messages in thread
From: Stefan Binding @ 2026-11-20 16:10 UTC (permalink / raw)
  To: Mark Brown, Jaroslav Kysela, Takashi Iwai
  Cc: linux-sound, linux-kernel, patches, Stefan Binding

Currently, in a small number of scenarios, the firmware on CS35L41 can
be reloaded:
- Using an ALSA control
- System Resume
- After Playback (after System Resume - if reload has not occurred)

In all these cases, the reload is done inside a work item and
scheduled on the default workqueue.

This should instead be queued on the system_freezable_wq, which ensures
that the work item is handled correctly during system suspend, where it
is not allowed to start whilst the workqueue is frozen.

There are 3 main scenarios where we need to ensure firmware load work
is flushed:
- Before System Suspend
- Before first playback after System Resume
- Error Paths

Register the driver for PM Notifications so that it can correctly flush
before suspending. Ensure any firmware work is either cancelled (if
pending) or flushed in the PM_*_PREPARE notification callbacks.
This ensures that the work item completes before any workqueues are
frozen. If the firmware load work item is still running when the
workqueue is frozen, then it would ordinarily be flushed, but may hang
on waiting for filesystem access in a call to request_firmware(), if
filesystem access requires work in a freezable workqueue itself.

Additionally, ensure that we flush the work before we start playback
after system resume. This ensures firmware is loaded before we restart
any playback after system suspend.

We also need to re-queue the firmware load in the PM_POST_* notifiers,
if the suspend was aborted before we finished suspending, but after
the PM_*_PREPARE notifier cancelled a pending firmware load.

Fixes: 88672826e2a4 ("ALSA: hda: cs35l41: Support System Suspend")

Signed-off-by: Stefan Binding <sbinding@opensource.cirrus.com>
---
 sound/hda/codecs/side-codecs/cs35l41_hda.c | 129 +++++++++++++++++----
 sound/hda/codecs/side-codecs/cs35l41_hda.h |   2 +
 2 files changed, 109 insertions(+), 22 deletions(-)

diff --git a/sound/hda/codecs/side-codecs/cs35l41_hda.c b/sound/hda/codecs/side-codecs/cs35l41_hda.c
index 6f4b2088c5fe..512b1a009476 100644
--- a/sound/hda/codecs/side-codecs/cs35l41_hda.c
+++ b/sound/hda/codecs/side-codecs/cs35l41_hda.c
@@ -12,6 +12,7 @@
 #include <sound/hda_codec.h>
 #include <sound/soc.h>
 #include <linux/pm_runtime.h>
+#include <linux/suspend.h>
 #include <linux/spi/spi.h>
 #include <linux/vmalloc.h>
 #include "hda_local.h"
@@ -603,6 +604,38 @@ static int cs35l41_init_dsp(struct cs35l41_hda *cs35l41)
 	return 0;
 }
 
+static void cs35l41_fw_load_request_unlocked(struct cs35l41_hda *cs35l41)
+{
+	/* firmware request is pending */
+	if (cs35l41->fw_request_ongoing)
+		return;
+
+	/* no need to request firmware load if DSP running state matches request */
+	if (cs35l41->cs_dsp.running == cs35l41->request_fw_load)
+		return;
+
+	/* block firmware load request if suspending */
+	if (cs35l41->suspending)
+		return;
+
+	cs35l41->fw_request_ongoing = queue_work(system_freezable_wq, &cs35l41->fw_load_work);
+}
+
+static void cs35l41_fw_load_request(struct cs35l41_hda *cs35l41)
+{
+	guard(mutex)(&cs35l41->fw_mutex);
+
+	cs35l41_fw_load_request_unlocked(cs35l41);
+}
+
+static void cs35l41_fw_load_cancel_sync(struct cs35l41_hda *cs35l41)
+{
+	cancel_work_sync(&cs35l41->fw_load_work);
+
+	guard(mutex)(&cs35l41->fw_mutex);
+	cs35l41->fw_request_ongoing = false;
+}
+
 static void cs35l41_shutdown_dsp(struct cs35l41_hda *cs35l41)
 {
 	struct cs_dsp *dsp = &cs35l41->cs_dsp;
@@ -617,7 +650,7 @@ static void cs35l41_remove_dsp(struct cs35l41_hda *cs35l41)
 {
 	struct cs_dsp *dsp = &cs35l41->cs_dsp;
 
-	cancel_work_sync(&cs35l41->fw_load_work);
+	cs35l41_fw_load_cancel_sync(cs35l41);
 
 	guard(mutex)(&cs35l41->fw_mutex);
 	cs35l41_shutdown_dsp(cs35l41);
@@ -811,6 +844,7 @@ static void cs35l41_hda_playback_hook(struct device *dev, int action)
 		pm_runtime_get_sync(dev);
 		break;
 	case HDA_GEN_PCM_ACT_PREPARE:
+		flush_work(&cs35l41->fw_load_work);
 		scoped_guard(mutex, &cs35l41->fw_mutex) {
 			cs35l41_hda_play_start(dev);
 		}
@@ -821,14 +855,7 @@ static void cs35l41_hda_playback_hook(struct device *dev, int action)
 		}
 		break;
 	case HDA_GEN_PCM_ACT_CLOSE:
-		scoped_guard(mutex, &cs35l41->fw_mutex) {
-			if (!cs35l41->cs_dsp.running && cs35l41->request_fw_load &&
-			    !cs35l41->fw_request_ongoing) {
-				dev_info(dev, "Requesting Firmware Load after HDA_GEN_PCM_ACT_CLOSE\n");
-				cs35l41->fw_request_ongoing = true;
-				schedule_work(&cs35l41->fw_load_work);
-			}
-		}
+		cs35l41_fw_load_request(cs35l41);
 
 		/*
 		 * Playback must be finished for all amps before we start runtime suspend.
@@ -925,6 +952,45 @@ static int cs35l41_ready_for_reset(struct cs35l41_hda *cs35l41)
 	return 0;
 }
 
+static void cs35l41_set_suspending(struct cs35l41_hda *cs35l41, bool suspending)
+{
+	guard(mutex)(&cs35l41->fw_mutex);
+
+	cs35l41->suspending = suspending;
+}
+
+static int cs35l41_hda_pm_notifier(struct notifier_block *nb, unsigned long action, void *data)
+{
+	struct cs35l41_hda *cs35l41 = container_of(nb, struct cs35l41_hda, pm_notifier);
+
+	dev_dbg(cs35l41->dev, "PM notifier action: %lu\n", action);
+
+	switch (action) {
+	case PM_HIBERNATION_PREPARE:
+	case PM_SUSPEND_PREPARE:
+	case PM_RESTORE_PREPARE:
+		cs35l41_set_suspending(cs35l41, true);
+		cs35l41_fw_load_cancel_sync(cs35l41);
+		break;
+	case PM_POST_HIBERNATION:
+	case PM_POST_SUSPEND:
+	case PM_POST_RESTORE:
+		/*
+		 * If suspending is still set, we are in the recovery path - reload firmware
+		 * after failed suspend
+		 */
+		if (cs35l41->suspending) {
+			cs35l41_set_suspending(cs35l41, false);
+			cs35l41_fw_load_request(cs35l41);
+		}
+		break;
+	default:
+		break;
+	}
+
+	return NOTIFY_DONE;
+}
+
 static int cs35l41_system_suspend_prep(struct device *dev)
 {
 	struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
@@ -980,6 +1046,7 @@ static int cs35l41_system_suspend(struct device *dev)
 		disable_irq(cs35l41->irq);
 
 	dev_dbg(cs35l41->dev, "System Suspended\n");
+	cs35l41_set_suspending(cs35l41, false);
 
 	return ret;
 }
@@ -1078,12 +1145,7 @@ static int cs35l41_system_resume(struct device *dev)
 		return ret;
 	}
 
-	guard(mutex)(&cs35l41->fw_mutex);
-
-	if (cs35l41->request_fw_load && !cs35l41->fw_request_ongoing) {
-		cs35l41->fw_request_ongoing = true;
-		schedule_work(&cs35l41->fw_load_work);
-	}
+	cs35l41_fw_load_request(cs35l41);
 
 	return 0;
 }
@@ -1320,9 +1382,11 @@ static int cs35l41_fw_load_ctl_put(struct snd_kcontrol *kcontrol,
 		return -EBUSY;
 	}
 
-	cs35l41->fw_request_ongoing = true;
+	guard(mutex)(&cs35l41->fw_mutex);
+
 	cs35l41->request_fw_load = ucontrol->value.integer.value[0];
-	schedule_work(&cs35l41->fw_load_work);
+
+	cs35l41_fw_load_request_unlocked(cs35l41);
 
 	return 1;
 }
@@ -1464,7 +1528,7 @@ static int cs35l41_hda_bind(struct device *dev, struct device *master, void *mas
 	struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
 	struct hda_component_parent *parent = master_data;
 	struct hda_component *comp;
-	int ret = 0;
+	int ret;
 
 	comp = hda_component_from_index(parent, cs35l41->index);
 	if (!comp)
@@ -1494,8 +1558,6 @@ static int cs35l41_hda_bind(struct device *dev, struct device *master, void *mas
 		dev_dbg(cs35l41->dev, "Firmware Autostart is disabled.\n");
 	}
 
-	ret = cs35l41_create_controls(cs35l41);
-
 	comp->playback_hook = cs35l41_hda_playback_hook;
 	comp->pre_playback_hook = cs35l41_hda_pre_playback_hook;
 	comp->post_playback_hook = cs35l41_hda_post_playback_hook;
@@ -1510,9 +1572,18 @@ static int cs35l41_hda_bind(struct device *dev, struct device *master, void *mas
 
 	mutex_unlock(&cs35l41->fw_mutex);
 
+	/* Create the necessary controls, do not create them under fw_mutex */
+	ret = cs35l41_create_controls(cs35l41);
+	if (ret) {
+		dev_err(cs35l41->dev, "Failed to create controls: %d\n", ret);
+		goto err;
+	}
+
 	if (!device_link_add(&cs35l41->codec->core.dev, cs35l41->dev, DL_FLAG_STATELESS))
 		dev_warn(dev, "Unable to create device link\n");
 
+	register_pm_notifier(&cs35l41->pm_notifier);
+
 	dev_info(cs35l41->dev,
 		 "CS35L41 Bound - SSID: %s, BST: %d, VSPK: %d, CH: %c, FW EN: %d, SPKID: %d\n",
 		 cs35l41->acpi_subsystem_id, cs35l41->hw_cfg.bst_type,
@@ -1520,6 +1591,15 @@ static int cs35l41_hda_bind(struct device *dev, struct device *master, void *mas
 		 channel_name[cs35l41->hw_cfg.spk_pos],
 		 cs35l41->cs_dsp.running, cs35l41->speaker_id);
 
+	return 0;
+err:
+	memset(comp, 0, sizeof(*comp));
+	scoped_guard(mutex, &cs35l41->fw_mutex) {
+		cs35l41->request_fw_load = false;
+	}
+	cs35l41_fw_load_cancel_sync(cs35l41);
+	cs35l41->codec = NULL;
+
 	return ret;
 }
 
@@ -1538,7 +1618,11 @@ static void cs35l41_hda_unbind(struct device *dev, struct device *master, void *
 		memset(comp, 0, sizeof(*comp));
 
 		cs35l41_remove_controls(cs35l41);
-		cancel_work_sync(&cs35l41->fw_load_work);
+		scoped_guard(mutex, &cs35l41->fw_mutex) {
+			cs35l41->request_fw_load = false;
+		}
+		unregister_pm_notifier(&cs35l41->pm_notifier);
+		cs35l41_fw_load_cancel_sync(cs35l41);
 		cs35l41->codec = NULL;
 	}
 }
@@ -1970,6 +2054,7 @@ int cs35l41_hda_probe(struct device *dev, const char *device_name, int id, int i
 	cs35l41->irq = irq;
 	cs35l41->regmap = regmap;
 	cs35l41->control_bus = control_bus;
+	cs35l41->pm_notifier.notifier_call = cs35l41_hda_pm_notifier;
 	dev_set_drvdata(dev, cs35l41);
 
 	ret = cs35l41_hda_read_acpi(cs35l41, device_name, id);
@@ -2076,7 +2161,7 @@ void cs35l41_hda_remove(struct device *dev)
 	struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
 
 	component_del(cs35l41->dev, &cs35l41_hda_comp_ops);
-	cancel_work_sync(&cs35l41->fw_load_work);
+	cs35l41_fw_load_cancel_sync(cs35l41);
 
 	pm_runtime_get_sync(cs35l41->dev);
 	pm_runtime_dont_use_autosuspend(cs35l41->dev);
diff --git a/sound/hda/codecs/side-codecs/cs35l41_hda.h b/sound/hda/codecs/side-codecs/cs35l41_hda.h
index 64ee70c31a43..b374836e8537 100644
--- a/sound/hda/codecs/side-codecs/cs35l41_hda.h
+++ b/sound/hda/codecs/side-codecs/cs35l41_hda.h
@@ -78,6 +78,8 @@ struct cs35l41_hda {
 	struct work_struct fw_load_work;
 	struct snd_kcontrol *fw_load_ctl;
 	struct snd_kcontrol *mute_override_ctl;
+	struct notifier_block pm_notifier;
+	bool suspending;
 
 	struct regmap_irq_chip_data *irq_data;
 	bool firmware_running;
-- 
2.43.0


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

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

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 16:10 [PATCH v1 0/7] Fixes and improvements in CS35L41 HDA driver for System Suspend/Resume Stefan Binding
2026-11-20 16:10 ` [PATCH v1 1/7] ALSA: hda: cs35l41: Remove redundant lock_system_sleep() calls Stefan Binding
2026-11-20 16:10 ` [PATCH v1 2/7] ASoC: cs35l41: Prevent software reset when using SPI Stefan Binding
2026-11-20 16:10 ` [PATCH v1 3/7] ALSA: hda: cs35l41: Unset irq if not used or configured Stefan Binding
2026-11-20 16:10 ` [PATCH v1 4/7] ALSA: hda/cs35l41: Split System Suspend/Resume callbacks Stefan Binding
2026-11-20 16:10 ` [PATCH v1 5/7] ALSA: hda/cs35l41: Remove support for alternative firmware types Stefan Binding
2026-11-20 16:10 ` [PATCH v1 6/7] ALSA: hda/cs35l41: Store filenames of successfully loaded firmware and tunings Stefan Binding
2026-11-20 16:10 ` [PATCH v1 7/7] ALSA: hda/cs35l41: rework how firmware requests are queued and flushed to prevent races Stefan Binding

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