[PATCH v5 13/24] ASoC: qcom: audioreach: add speaker protection calibration trigger infrastructure

From: Srinivas Kandagatla

Date: Fri Dec 04 2026 - 06:32:14 EST


From: Ravi Hothi <ravi.hothi@xxxxxxxxxxxxxxxx>

Introduce the speaker protection operation mode as an internal state
(default Normal) and a write-only edge trigger to start a calibration
run:

- sp_operation_mode holds the current mode;
audioreach_get_sp_operation_mode() exposes it as read-only status
for userspace kcontrols.
- audioreach_trigger_sp_calibration() flips Normal -> Calibration via
cmpxchg, returning -EBUSY if a run is already in flight, and arms a
60 s watchdog to bound the armed state.
- On the DSP reporting terminal state for every speaker, the mode
auto-returns to Normal and the watchdog is cancelled. The VI module
path uses the current mode on the next configure.

The calibration run is edge-triggered rather than a user-visible mode
so a stale alsactl state file cannot flip the DSP into Calibration and
nothing can leave the system stuck there.

Signed-off-by: Ravi Hothi <ravi.hothi@xxxxxxxxxxxxxxxx>
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@xxxxxxxxxxxxxxxx>
---
sound/soc/qcom/qdsp6/audioreach.c | 91 ++++++++++++++++++++++++++-----
sound/soc/qcom/qdsp6/audioreach.h | 3 +
sound/soc/qcom/qdsp6/q6apm.c | 2 +
3 files changed, 82 insertions(+), 14 deletions(-)

diff --git a/sound/soc/qcom/qdsp6/audioreach.c b/sound/soc/qcom/qdsp6/audioreach.c
index a187e0b13633..be37681f467f 100644
--- a/sound/soc/qcom/qdsp6/audioreach.c
+++ b/sound/soc/qcom/qdsp6/audioreach.c
@@ -5,6 +5,7 @@
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/soc/qcom/apr.h>
+#include <linux/workqueue.h>
#include <sound/soc.h>
#include <sound/soc-dai.h>
#include <sound/pcm.h>
@@ -13,6 +14,8 @@
#include "q6apm.h"
#include "audioreach.h"

+static u32 sp_operation_mode;
+
enum sp_vi_cali_state {
SP_VI_CALI_IDLE, /* Not calibrating */
SP_VI_CALI_RUNNING, /* Events arriving, none decisive yet */
@@ -1456,6 +1459,65 @@ static int audioreach_gain_set(struct q6apm_graph *graph,
return q6apm_send_cmd_sync(graph->apm, pkt, 0);
}

+u32 audioreach_get_sp_operation_mode(void)
+{
+ return READ_ONCE(sp_operation_mode);
+}
+
+/*
+ * Calibration is edge-triggered rather than a user-visible mode. Guard
+ * against a trigger write while a run is still in flight, and bound the
+ * armed state with a watchdog so a trigger without a matching playback
+ * does not leave the system in Calibration forever.
+ */
+#define SP_CAL_WATCHDOG_SECS 60
+
+static void audioreach_sp_cal_watchdog_fn(struct work_struct *work);
+static DECLARE_DELAYED_WORK(sp_cal_watchdog, audioreach_sp_cal_watchdog_fn);
+
+static void audioreach_sp_disarm_vi(void)
+{
+ guard(mutex)(&sp_vi_r0_lock);
+ sp_vi_r0.armed = false;
+}
+
+static void audioreach_sp_cal_watchdog_fn(struct work_struct *work)
+{
+ if (cmpxchg(&sp_operation_mode, PARAM_ID_SP_VI_OP_MODE_CALIBRATION,
+ PARAM_ID_SP_VI_OP_MODE_NORMAL) !=
+ PARAM_ID_SP_VI_OP_MODE_CALIBRATION)
+ return;
+
+ audioreach_sp_disarm_vi();
+ pr_info("audioreach: SP calibration watchdog fired, back to Normal\n");
+}
+
+/* Finish before writing the mode so a trigger racing in cannot be clobbered */
+static void audioreach_sp_finish_calibration(void)
+{
+ cancel_delayed_work_sync(&sp_cal_watchdog);
+ WRITE_ONCE(sp_operation_mode, PARAM_ID_SP_VI_OP_MODE_NORMAL);
+}
+
+/* Release the delayed work from the module on unload so the timer cannot fire */
+void audioreach_sp_shutdown(void)
+{
+ cancel_delayed_work_sync(&sp_cal_watchdog);
+}
+EXPORT_SYMBOL_GPL(audioreach_sp_shutdown);
+
+int audioreach_trigger_sp_calibration(void)
+{
+ if (cmpxchg(&sp_operation_mode, PARAM_ID_SP_VI_OP_MODE_NORMAL,
+ PARAM_ID_SP_VI_OP_MODE_CALIBRATION) !=
+ PARAM_ID_SP_VI_OP_MODE_NORMAL)
+ return -EBUSY;
+
+ schedule_delayed_work(&sp_cal_watchdog,
+ SP_CAL_WATCHDOG_SECS * HZ);
+ return 0;
+}
+
static bool sp_vi_cali_state_is_failure(u32 state)
{
return state == VI_CALIBRATION_STATE_FAILED ||
@@ -1506,10 +1568,6 @@ void audioreach_vi_calibration_event(struct device *dev,
unsigned int i;
u32 bit;

- /* A payload with no entries gives no new information */
- if (!num_ch)
- return;
-
guard(mutex)(&sp_vi_r0_lock);

if (!sp_vi_r0.armed)
@@ -1566,14 +1624,16 @@ void audioreach_vi_calibration_event(struct device *dev,
}

dev_info(dev, "VI calibration complete: %s\n", success ? "success" : "failed");
+
+ /* Edge-triggered: the mode auto-returns to Normal on terminal state. */
+ audioreach_sp_finish_calibration();
}

static int audioreach_speaker_protection(struct q6apm_graph *graph,
- const struct audioreach_module *module,
- uint32_t operation_mode)
+ const struct audioreach_module *module)
{
return audioreach_send_u32_param(graph, module, PARAM_ID_SP_OP_MODE,
- operation_mode);
+ READ_ONCE(sp_operation_mode));
}

static int audioreach_register_events(struct q6apm_graph *graph,
@@ -1615,7 +1675,8 @@ static void audioreach_arm_vi_calibration(void)

static int audioreach_speaker_protection_vi(struct q6apm_graph *graph,
const struct audioreach_module *module,
- const struct audioreach_module_config *mcfg)
+ const struct audioreach_module_config *mcfg,
+ u32 mode)
{
u32 num_channels = mcfg->num_channels;
struct apm_module_sp_vi_op_mode_cfg *op_cfg;
@@ -1640,11 +1701,13 @@ static int audioreach_speaker_protection_vi(struct q6apm_graph *graph,
return -EINVAL;
}

- audioreach_arm_vi_calibration();
+ if (mode == PARAM_ID_SP_VI_OP_MODE_CALIBRATION) {
+ audioreach_arm_vi_calibration();

- rc = audioreach_register_events(graph, module);
- if (rc)
- return rc;
+ rc = audioreach_register_events(graph, module);
+ if (rc)
+ return rc;
+ }

op_sz = APM_SP_VI_OP_MODE_CFG_PSIZE(num_speakers);
/* Channel mapping for Isense and Vsense, thus twice number of speakers. */
@@ -1668,7 +1731,7 @@ static int audioreach_speaker_protection_vi(struct q6apm_graph *graph,

/* The DSP calls this field num_speakers for the VI module */
op_cfg->cfg.num_channels = num_speakers;
- op_cfg->cfg.operation_mode = PARAM_ID_SP_VI_OP_MODE_CALIBRATION;
+ op_cfg->cfg.operation_mode = mode;
p += op_sz;

cm_cfg = p;
@@ -1760,7 +1823,7 @@ int audioreach_set_media_format(struct q6apm_graph *graph,
break;
case MODULE_ID_SPEAKER_PROTECTION:
rc = audioreach_speaker_protection(graph, module,
- PARAM_ID_SP_OP_MODE_CALIBRATION);
+ READ_ONCE(sp_operation_mode));
if (!rc)
rc = audioreach_module_enable(graph, module, true);

diff --git a/sound/soc/qcom/qdsp6/audioreach.h b/sound/soc/qcom/qdsp6/audioreach.h
index 062727d09612..9f1ef0a3e510 100644
--- a/sound/soc/qcom/qdsp6/audioreach.h
+++ b/sound/soc/qcom/qdsp6/audioreach.h
@@ -1113,9 +1113,12 @@ int audioreach_gain_set_vol_ctrl(struct q6apm *apm,
int audioreach_send_u32_param(struct q6apm_graph *graph,
const struct audioreach_module *module,
uint32_t param_id, uint32_t param_val);
+u32 audioreach_get_sp_operation_mode(void);
+int audioreach_trigger_sp_calibration(void);
void audioreach_vi_calibration_event(struct device *dev,
const struct event_id_vi_per_spkr_calibration *cali,
u32 num_ch);
+void audioreach_sp_shutdown(void);
int audioreach_compr_set_param(struct q6apm_graph *graph,
const struct audioreach_module_config *mcfg);
int audioreach_setup_push_pull(struct q6apm_graph *graph, phys_addr_t bphys,
diff --git a/sound/soc/qcom/qdsp6/q6apm.c b/sound/soc/qcom/qdsp6/q6apm.c
index c410e2f6e6a3..6494eb80f089 100644
--- a/sound/soc/qcom/qdsp6/q6apm.c
+++ b/sound/soc/qcom/qdsp6/q6apm.c
@@ -950,6 +950,8 @@ static int apm_probe(gpr_device_t *gdev)

static void apm_remove(gpr_device_t *gdev)
{
+ /* Release the SP calibration watchdog before the module goes away */
+ audioreach_sp_shutdown();
g_apm = NULL;
of_platform_depopulate(&gdev->dev);
snd_soc_unregister_component(&gdev->dev);
--
2.53.0