[PATCH v3 8/8] ALSA: hda/cs35l41: Ensure SPI devices are always reset during system suspend

From: Stefan Binding

Date: Thu Dec 03 2026 - 09:15:13 EST


After the software reset was removed from the normal system resume for
SPI devices due to boot issues, there remains a path where one of the
amps could suspend and resume without reset. This could cause
unexpected issues.

To solve this, add a software reset for SPI devices in the suspend
callback. Whilst, in theory, the boot issue could re-occur due to the
software reset, it can only occur if reset GPIO does not get used,
such as if the suspend gets aborted before the amp which owns the
shared reset suspends. The separation between the suspend and resume
calls should also help to minimise the chance of failure.

The CS35L41 may need to be woken from hibernation before reset.

I2C devices do not need to reset on suspend, since they are safe to
reset in the system resume call.

Signed-off-by: Stefan Binding <sbinding@xxxxxxxxxxxxxxxxxxxxx>
---
sound/hda/codecs/side-codecs/cs35l41_hda.c | 138 +++++++++++++++++++--
sound/hda/codecs/side-codecs/cs35l41_hda.h | 7 ++
2 files changed, 138 insertions(+), 7 deletions(-)

diff --git a/sound/hda/codecs/side-codecs/cs35l41_hda.c b/sound/hda/codecs/side-codecs/cs35l41_hda.c
index e6ab53d5ba3a..507beea057c0 100644
--- a/sound/hda/codecs/side-codecs/cs35l41_hda.c
+++ b/sound/hda/codecs/side-codecs/cs35l41_hda.c
@@ -947,7 +947,99 @@ static int cs35l41_verify_id(struct cs35l41_hda *cs35l41, unsigned int *regid, u
return 0;
}

-static int cs35l41_ready_for_reset(struct cs35l41_hda *cs35l41)
+#if IS_ENABLED(CONFIG_SPI)
+
+static int cs35l41_spi_issue_bus_locked_reset(struct cs35l41_hda *cs35l41, struct spi_device *spi)
+{
+ struct cs35l41_spi_payload *buf = cs35l41->spi_payload_buf;
+ struct spi_transfer t = {
+ .tx_buf = cs35l41->spi_payload_buf,
+ .len = sizeof(*cs35l41->spi_payload_buf),
+ };
+ struct spi_message m;
+ int ret;
+
+ buf->addr = cpu_to_be32(CS35L41_SFT_RESET);
+ buf->value = cpu_to_be32(CS35L41_SOFTWARE_RESET);
+ spi_message_init_with_transfers(&m, &t, 1);
+ ret = spi_sync_locked(spi, &m);
+ if (ret)
+ dev_err(cs35l41->dev, "spi_sync failed: %d\n", ret);
+
+ return ret;
+}
+
+static int cs35l41_spi_system_reset(struct cs35l41_hda *cs35l41)
+{
+ struct spi_device *spi = to_spi_device(cs35l41->dev);
+ int ret;
+
+ /*
+ * There must not be any other SPI bus activity while the amp is
+ * soft-resetting.
+ */
+ ret = spi_bus_lock(spi->controller);
+ if (ret) {
+ dev_err(cs35l41->dev, "spi_bus_lock failed: %d\n", ret);
+ return ret;
+ }
+
+ dev_info(cs35l41->dev, "Issuing SPI software reset\n");
+
+ /* Force quiet interval before issuing reset */
+ fsleep(10000);
+
+ ret = cs35l41_spi_issue_bus_locked_reset(cs35l41, spi);
+
+ /* Force quiet interval after issuing reset */
+ fsleep(10000);
+
+ spi_bus_unlock(spi->controller);
+
+ return ret;
+}
+
+static int cs35l41_spi_suspend_reset(struct cs35l41_hda *cs35l41)
+{
+ int ret = 0;
+
+ if (cs35l41->control_bus != SPI)
+ return 0;
+
+ if (cs35l41->cs_dsp.running) {
+ /*
+ * If the DSP was running, after _force_suspend() we will be in hibernation.
+ * We need to exit hibernation, or the reset write could be ignored.
+ */
+ regcache_cache_only(cs35l41->regmap, false);
+ ret = cs35l41_exit_hibernate(cs35l41->dev, cs35l41->regmap);
+ regcache_cache_only(cs35l41->regmap, true);
+ if (ret)
+ return ret;
+ }
+
+ ret = cs35l41_spi_system_reset(cs35l41);
+ if (ret) {
+ /* revert hibernation if reset failed */
+ if (cs35l41->cs_dsp.running) {
+ regcache_cache_only(cs35l41->regmap, false);
+ cs35l41_enter_hibernate(cs35l41->dev, cs35l41->regmap,
+ cs35l41->hw_cfg.bst_type);
+ regcache_cache_only(cs35l41->regmap, true);
+ }
+ dev_err(cs35l41->dev, "SPI System Reset Failed: %d\n", ret);
+ }
+
+ return ret;
+}
+#else
+static int cs35l41_spi_suspend_reset(struct cs35l41_hda *cs35l41)
+{
+ return 0;
+}
+#endif /* IS_ENABLED(CONFIG_SPI) */
+
+static void cs35l41_ready_for_reset(struct cs35l41_hda *cs35l41)
{
guard(mutex)(&cs35l41->fw_mutex);
if (cs35l41->cs_dsp.running) {
@@ -955,8 +1047,6 @@ static int cs35l41_ready_for_reset(struct cs35l41_hda *cs35l41)
cs35l41->cs_dsp.booted = false;
}
regcache_mark_dirty(cs35l41->regmap);
-
- return 0;
}

static void cs35l41_set_suspending(struct cs35l41_hda *cs35l41, bool suspending)
@@ -1048,15 +1138,32 @@ static int cs35l41_system_suspend(struct device *dev)
return ret;
}

+ /*
+ * Ensure SPI Devices have been reset in case of abort.
+ * All I2C devices will be reset on resume anyway, but SPI devices would only get reset on
+ * resume by the shared reset GPIO.
+ */
+ ret = cs35l41_spi_suspend_reset(cs35l41);
+ if (ret) {
+ /* clean up force_suspend */
+ int tmp = pm_runtime_force_resume(cs35l41->dev);
+
+ if (tmp)
+ dev_err(cs35l41->dev, "Resume failed after SPI reset failure: %d\n", tmp);
+
+ if (cs35l41->irq)
+ enable_irq(cs35l41->irq);
+
+ return ret;
+ }
+
/* Shutdown DSP before system suspend */
- ret = cs35l41_ready_for_reset(cs35l41);
- if (ret)
- dev_err(dev, "System Suspend Failed, not ready for Reset: %d\n", ret);
+ cs35l41_ready_for_reset(cs35l41);

dev_dbg(cs35l41->dev, "System Suspended\n");
cs35l41_set_suspending(cs35l41, false);

- return ret;
+ return 0;
}

static int cs35l41_system_suspend_late(struct device *dev)
@@ -2055,6 +2162,17 @@ static int cs35l41_hda_read_acpi(struct cs35l41_hda *cs35l41, const char *hid, i
return 0;
}

+static int cs35l41_init_config_for_spi(struct cs35l41_hda *cs35l41, struct spi_device *spi)
+{
+ cs35l41->spi_payload_buf = devm_kzalloc_obj(&spi->dev,
+ *cs35l41->spi_payload_buf,
+ GFP_KERNEL | GFP_DMA);
+ if (!cs35l41->spi_payload_buf)
+ return -ENOMEM;
+
+ return 0;
+}
+
int cs35l41_hda_probe(struct device *dev, const char *device_name, int id, int irq,
struct regmap *regmap, enum control_bus control_bus)
{
@@ -2072,6 +2190,12 @@ int cs35l41_hda_probe(struct device *dev, const char *device_name, int id, int i
if (!cs35l41)
return -ENOMEM;

+ if (control_bus == SPI) {
+ ret = cs35l41_init_config_for_spi(cs35l41, to_spi_device(dev));
+ if (ret)
+ return ret;
+ }
+
cs35l41->dev = dev;
cs35l41->irq = irq;
cs35l41->regmap = regmap;
diff --git a/sound/hda/codecs/side-codecs/cs35l41_hda.h b/sound/hda/codecs/side-codecs/cs35l41_hda.h
index b374836e8537..9329fb6834af 100644
--- a/sound/hda/codecs/side-codecs/cs35l41_hda.h
+++ b/sound/hda/codecs/side-codecs/cs35l41_hda.h
@@ -39,6 +39,12 @@ struct cs35l41_amp_efi_data {
struct cs35l41_amp_cal_data data[];
} __packed;

+struct cs35l41_spi_payload {
+ __be32 addr;
+ __be16 pad;
+ __be32 value;
+} __packed;
+
enum cs35l41_hda_spk_pos {
CS35L41_LEFT,
CS35L41_RIGHT,
@@ -79,6 +85,7 @@ struct cs35l41_hda {
struct snd_kcontrol *fw_load_ctl;
struct snd_kcontrol *mute_override_ctl;
struct notifier_block pm_notifier;
+ struct cs35l41_spi_payload *spi_payload_buf;
bool suspending;

struct regmap_irq_chip_data *irq_data;
--
2.43.0