[PATCH v2 10/24] ASoC: qcom: audioreach: run VI module in calibration mode with per-speaker events

From: Srinivas Kandagatla

Date: Sun Nov 22 2026 - 11:15:43 EST


From: Ravi Hothi <ravi.hothi@xxxxxxxxxxxxxxxx>

Put the speaker protection and VI modules in calibration mode, number
the V/I channel pairs consecutively (the VI capture packs one pair per
speaker without gaps), and support up to MAX_SP_VI_SPEAKERS (4) speakers
to cover laptops with four WSAs.

Register for EVENT_ID_VI_PER_SPKR_CALIBRATION on the VI module so each
speaker's state and measured resistance are reported on their own, and
dispatch the event from q6apm's graph callback into
audioreach_vi_calibration_event(). The initial handler just logs each
event at debug level; later patches add result caching and a userspace
trigger on top.

The module event registration is scoped to the graph: the DSP releases
it automatically when the graph is closed on stream close, so no explicit
unregister is needed on the driver side.

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

diff --git a/sound/soc/qcom/qdsp6/audioreach.c b/sound/soc/qcom/qdsp6/audioreach.c
index afa8e0159570..fbb9e3c4b84a 100644
--- a/sound/soc/qcom/qdsp6/audioreach.c
+++ b/sound/soc/qcom/qdsp6/audioreach.c
@@ -1462,6 +1462,18 @@ static int audioreach_gain_set(struct q6apm_graph *graph,
return q6apm_send_cmd_sync(graph->apm, pkt, 0);
}

+void audioreach_vi_calibration_event(struct device *dev,
+ const struct event_id_vi_per_spkr_calibration *cali,
+ u32 num_ch)
+{
+ unsigned int i;
+
+ /* Raised every frame; keep the per-event log at debug level */
+ for (i = 0; i < num_ch; i++)
+ dev_dbg(dev, "VI calibration channel %u: state %u\n",
+ i, cali->cali_param[i].state);
+}
+
static int audioreach_speaker_protection(struct q6apm_graph *graph,
const struct audioreach_module *module,
uint32_t operation_mode)
@@ -1470,6 +1482,35 @@ static int audioreach_speaker_protection(struct q6apm_graph *graph,
operation_mode);
}

+static int audioreach_register_events(struct q6apm_graph *graph,
+ const struct audioreach_module *module)
+{
+ struct apm_module_register_events *payload;
+ struct gpr_pkt *pkt;
+ int rc, payload_size;
+ void *p;
+
+ /* No event config payload, the packet is zeroed on allocation */
+ payload_size = ALIGN(sizeof(struct apm_module_register_events), 8);
+ pkt = audioreach_alloc_cmd_pkt(payload_size, APM_CMD_REGISTER_MODULE_EVENTS,
+ 0, graph->port->id, module->instance_id);
+ if (IS_ERR(pkt))
+ return PTR_ERR(pkt);
+
+ p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
+
+ payload = p;
+ payload->module_instance_id = module->instance_id;
+ payload->event_id = EVENT_ID_VI_PER_SPKR_CALIBRATION;
+ payload->is_register = 1;
+
+ rc = audioreach_graph_send_cmd_sync(graph, pkt, 0);
+
+ kfree(pkt);
+
+ return rc;
+}
+
static int audioreach_speaker_protection_vi(struct q6apm_graph *graph,
const struct audioreach_module *module,
const struct audioreach_module_config *mcfg)
@@ -1480,18 +1521,30 @@ static int audioreach_speaker_protection_vi(struct q6apm_graph *graph,
struct apm_module_sp_vi_ex_mode_cfg *ex_cfg;
int op_sz, cm_sz, ex_sz;
struct apm_module_param_data *param_data;
- int rc, i, payload_size, j;
+ int rc, i, j, payload_size;
struct gpr_pkt *pkt;
+ u32 num_speakers;
void *p;

- if (num_channels > 2) {
+ if (num_channels > MAX_SP_VI_SPEAKERS) {
dev_err(graph->dev, "Error: Invalid channels (%d)!\n", num_channels);
return -EINVAL;
}

- op_sz = APM_SP_VI_OP_MODE_CFG_PSIZE(num_channels);
+ /* One 32-bit V/I word per speaker, so channels == speakers */
+ num_speakers = num_channels;
+ if (!num_speakers) {
+ dev_err(graph->dev, "Error: VI needs at least one channel\n");
+ return -EINVAL;
+ }
+
+ 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. */
- cm_sz = APM_SP_VI_CH_MAP_CFG_PSIZE(num_channels * 2);
+ cm_sz = APM_SP_VI_CH_MAP_CFG_PSIZE(num_speakers * 2);
ex_sz = APM_SP_VI_EX_MODE_CFG_PSIZE;

payload_size = op_sz + cm_sz + ex_sz;
@@ -1509,8 +1562,9 @@ static int audioreach_speaker_protection_vi(struct q6apm_graph *graph,
param_data->param_id = PARAM_ID_SP_VI_OP_MODE_CFG;
param_data->param_size = op_sz - APM_MODULE_PARAM_DATA_SIZE;

- op_cfg->cfg.num_channels = num_channels;
- op_cfg->cfg.operation_mode = PARAM_ID_SP_VI_OP_MODE_NORMAL;
+ /* 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;
p += op_sz;

cm_cfg = p;
@@ -1520,20 +1574,15 @@ static int audioreach_speaker_protection_vi(struct q6apm_graph *graph,
param_data->param_id = PARAM_ID_SP_VI_CHANNEL_MAP_CFG;
param_data->param_size = cm_sz - APM_MODULE_PARAM_DATA_SIZE;

- cm_cfg->cfg.num_channels = num_channels * 2;
- /* Convert the physical mapping to a logical mapping of the channels */
- for (i = 0, j = 0; i < AR_PCM_MAX_NUM_CHANNEL && j < num_channels; i++) {
+ cm_cfg->cfg.num_channels = num_speakers * 2;
+ /* Number the V/I pairs consecutively, the capture has no gaps */
+ for (i = 0, j = 0; i < AR_PCM_MAX_NUM_CHANNEL && j < num_speakers; i++) {
if (!mcfg->channel_map[i])
continue;
- /*
- * Map speakers into Vsense and then Isense of each channel.
- * E.g. for PCM_CHANNEL_FL and PCM_CHANNEL_FR to:
- * [1, 2, 3, 4]
- */
- cm_cfg->cfg.channel_mapping[2 * j] = (mcfg->channel_map[i] - 1) * 2 + 1;
- cm_cfg->cfg.channel_mapping[2 * j + 1] = (mcfg->channel_map[i] - 1) * 2 + 2;

- ++j;
+ cm_cfg->cfg.channel_mapping[2 * j] = 2 * j + 1;
+ cm_cfg->cfg.channel_mapping[2 * j + 1] = 2 * j + 2;
+ j++;
}

p += cm_sz;
@@ -1607,7 +1656,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_NORMAL);
+ PARAM_ID_SP_OP_MODE_CALIBRATION);
if (!rc)
rc = audioreach_module_enable(graph, module, true);

@@ -1616,6 +1665,7 @@ int audioreach_set_media_format(struct q6apm_graph *graph,
rc = audioreach_speaker_protection_vi(graph, module, cfg);
if (!rc)
rc = audioreach_module_enable(graph, module, true);
+
break;
case MODULE_ID_AUDIO_IF_SOURCE:
case MODULE_ID_AUDIO_IF_SINK:
diff --git a/sound/soc/qcom/qdsp6/audioreach.h b/sound/soc/qcom/qdsp6/audioreach.h
index 0ea035fa630c..9d9449062b83 100644
--- a/sound/soc/qcom/qdsp6/audioreach.h
+++ b/sound/soc/qcom/qdsp6/audioreach.h
@@ -696,6 +696,8 @@ struct param_id_sp_op_mode {

/* Speaker Protection VI */

+#define MAX_SP_VI_SPEAKERS 4
+
#define PARAM_ID_SP_VI_OP_MODE_CFG 0x080011f4
#define PARAM_ID_SP_VI_OP_MODE_NORMAL 0
#define PARAM_ID_SP_VI_OP_MODE_CALIBRATION 1
@@ -720,6 +722,58 @@ struct param_id_sp_vi_channel_map_cfg {
uint32_t channel_mapping[];
} __packed;

+/* VI per-speaker calibration event, raised every frame in calibration mode */
+#define EVENT_ID_VI_PER_SPKR_CALIBRATION 0x08001511
+
+/**
+ * enum vi_calibration_state - Calibration state of one speaker
+ * @VI_CALIBRATION_STATE_INCORRECT_OP_MODE: Not in calibration operation mode
+ * @VI_CALIBRATION_STATE_INACTIVE: Port not started
+ * @VI_CALIBRATION_STATE_WARMUP: Warming up
+ * @VI_CALIBRATION_STATE_INPROGRESS: Calibration running
+ * @VI_CALIBRATION_STATE_SUCCESS: R0/T0 within range
+ * @VI_CALIBRATION_STATE_FAILED: R0/T0 out of range
+ * @VI_CALIBRATION_STATE_WAIT_FOR_VI: Waiting for the V/I level to rise
+ * @VI_CALIBRATION_STATE_VI_WAIT_TIMED_OUT: Gave up waiting for the V/I level
+ * @VI_CALIBRATION_STATE_LOW_VI: V/I level fell below the threshold mid-run
+ *
+ * Reported by struct vi_per_spkr_calib_param.state. The DSP chooses these
+ * values, so they are fixed and must not be renumbered.
+ */
+enum vi_calibration_state {
+ VI_CALIBRATION_STATE_INCORRECT_OP_MODE = 0,
+ VI_CALIBRATION_STATE_INACTIVE = 1,
+ VI_CALIBRATION_STATE_WARMUP = 2,
+ VI_CALIBRATION_STATE_INPROGRESS = 3,
+ VI_CALIBRATION_STATE_SUCCESS = 4,
+ VI_CALIBRATION_STATE_FAILED = 5,
+ VI_CALIBRATION_STATE_WAIT_FOR_VI = 6,
+ VI_CALIBRATION_STATE_VI_WAIT_TIMED_OUT = 7,
+ VI_CALIBRATION_STATE_LOW_VI = 8,
+};
+
+/**
+ * struct vi_per_spkr_calib_param - Per-speaker calibration result
+ * @state: Calibration state of this speaker.
+ * @r0_cali_q24: Measured DC resistance in ohms, Q24. Filled in regardless of
+ * @state, so it is meaningful for both VI_CALIBRATION_STATE_SUCCESS
+ * and VI_CALIBRATION_STATE_FAILED.
+ */
+struct vi_per_spkr_calib_param {
+ uint32_t state;
+ int32_t r0_cali_q24;
+} __packed;
+
+/**
+ * struct event_id_vi_per_spkr_calibration - Per-speaker VI calibration result
+ * @num_ch: Number of speakers the result covers.
+ * @cali_param: Per-speaker calibration state and measured resistance.
+ */
+struct event_id_vi_per_spkr_calibration {
+ uint32_t num_ch;
+ struct vi_per_spkr_calib_param cali_param[];
+} __packed;
+
#define PARAM_ID_SAL_OUTPUT_CFG 0x08001016
struct param_id_sal_output_config {
uint32_t bits_per_sample;
@@ -1060,6 +1114,9 @@ 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);
+void audioreach_vi_calibration_event(struct device *dev,
+ const struct event_id_vi_per_spkr_calibration *cali,
+ u32 num_ch);
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 968fb7898aa4..c410e2f6e6a3 100644
--- a/sound/soc/qcom/qdsp6/q6apm.c
+++ b/sound/soc/qcom/qdsp6/q6apm.c
@@ -562,6 +562,29 @@ int q6apm_get_hw_pointer(struct q6apm_graph *graph, int dir)
}
EXPORT_SYMBOL_GPL(q6apm_get_hw_pointer);

+static void q6apm_vi_calibration_event(struct device *dev,
+ const struct apm_module_event *event,
+ int payload_size)
+{
+ const struct event_id_vi_per_spkr_calibration *cali;
+ u32 num_ch;
+
+ if (payload_size < 0 || payload_size < (int)(sizeof(*event) + sizeof(*cali))) {
+ dev_err(dev, "VI calibration event truncated (%d bytes)\n",
+ payload_size);
+ return;
+ }
+
+ cali = (const struct event_id_vi_per_spkr_calibration *)(event + 1);
+
+ /* Bound the array by the received payload, not by num_ch alone */
+ num_ch = (payload_size - sizeof(*event) - sizeof(*cali)) / sizeof(cali->cali_param[0]);
+ num_ch = min_t(u32, cali->num_ch, num_ch);
+ num_ch = min_t(u32, num_ch, MAX_SP_VI_SPEAKERS);
+
+ audioreach_vi_calibration_event(dev, cali, num_ch);
+}
+
static int graph_callback(const struct gpr_resp_pkt *data, void *priv, int op)
{
struct data_cmd_rsp_rd_sh_mem_ep_data_buffer_done_v2 *rd_done;
@@ -585,6 +608,9 @@ static int graph_callback(const struct gpr_resp_pkt *data, void *priv, int op)
client_event = APM_CLIENT_EVENT_WATERMARK_EVENT;
graph->cb(client_event, hdr->token, data->payload, graph->priv);
break;
+ case EVENT_ID_VI_PER_SPKR_CALIBRATION:
+ q6apm_vi_calibration_event(dev, event, data->payload_size);
+ break;
}

break;
--
2.53.0