[PATCH v4 4/4] ASoC: qcom: x1e80100: Allow userspace WSA RX chmap override

From: Abel Vesa

Date: Tue Nov 24 2026 - 05:58:08 EST


The WSA RX slot mapping is currently computed entirely from the channel
count in .prepare() and pushed unconditionally via
snd_soc_dai_set_channel_map(). Userspace has no way to express a
different channel map, so any map defined in UCM is ignored.

Add writable "WSA Playback Channel Map" and "WSA2 Playback Channel Map"
controls on the WSA RX backend PCMs, using the standard ALSA channel map
TLV. Store the userspace-selected SNDRV_CHMAP_* map per WSA backend and
translate it to Q6 PCM_CHANNEL_* slots at .prepare() time. Keep the
previous default mapping unless the selected map describes exactly the
prepared channel count, and reject maps with unset positions in the
middle, so a wider map is never truncated for a narrower stream.

Until userspace selects a map, report the default one for the last
prepared channel count, initially stereo. The map only takes effect at
.prepare() time, so refuse writes while the backend is prepared, and
snapshot the map under a lock so a concurrent write cannot tear it.

The controls are added from the card late_probe callback. The backend
PCMs are only created after all DAI links are initialized, and adding
controls from the stream startup callback would take controls_rwsem
under dapm_mutex, deadlocking against DAPM mixer writes.

Allow up to six WSA channels for Glymur.

Signed-off-by: Abel Vesa <abel.vesa@xxxxxxxxxxxxxxxx>
---
sound/soc/qcom/x1e80100.c | 246 +++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 222 insertions(+), 24 deletions(-)

diff --git a/sound/soc/qcom/x1e80100.c b/sound/soc/qcom/x1e80100.c
index c927672eaede..4fe86ec062c4 100644
--- a/sound/soc/qcom/x1e80100.c
+++ b/sound/soc/qcom/x1e80100.c
@@ -5,6 +5,7 @@
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/soundwire/sdw.h>
+#include <linux/spinlock.h>
#include <sound/pcm.h>
#include <sound/jack.h>
#include <sound/soc.h>
@@ -15,14 +16,151 @@
#include "qdsp6/q6dsp-common.h"
#include "sdw.h"

+#define X1E80100_WSA_MAX_CHANNELS 6
+#define X1E80100_WSA_PORTS 2
+
struct x1e80100_snd_data {
bool stream_prepared[AFE_PORT_MAX];
struct snd_soc_card *card;
struct snd_soc_jack jack;
struct snd_soc_jack dp_jack[8];
bool jack_setup;
+ /* Protects the WSA channel map state below */
+ spinlock_t wsa_chmap_lock;
+ /* Userspace-selected SNDRV_CHMAP_* positions, 0 means unset */
+ unsigned int wsa_chmap[X1E80100_WSA_PORTS][X1E80100_WSA_MAX_CHANNELS];
+ /* Channel count of the last prepared stream */
+ unsigned int wsa_channels[X1E80100_WSA_PORTS];
+ bool wsa_prepared[X1E80100_WSA_PORTS];
};

+/* Default WSA RX channel maps, indexed by channel count */
+static const unsigned int
+x1e80100_wsa_default_chmap[X1E80100_WSA_MAX_CHANNELS + 1][X1E80100_WSA_MAX_CHANNELS] = {
+ [1] = { SNDRV_CHMAP_MONO },
+ [2] = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR },
+ [3] = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_FC },
+ [4] = { SNDRV_CHMAP_FL, SNDRV_CHMAP_RL, SNDRV_CHMAP_FR, SNDRV_CHMAP_RR },
+ [6] = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_RL,
+ SNDRV_CHMAP_RR, SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE },
+};
+
+static int x1e80100_wsa_port_index(unsigned int dai_id)
+{
+ switch (dai_id) {
+ case WSA_CODEC_DMA_RX_0:
+ return 0;
+ case WSA_CODEC_DMA_RX_1:
+ return 1;
+ default:
+ return -EINVAL;
+ }
+}
+
+static struct x1e80100_snd_data *
+x1e80100_wsa_chmap_kcontrol_data(struct snd_kcontrol *kcontrol, int *idx)
+{
+ struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
+ struct snd_soc_pcm_runtime *rtd = info->pcm->private_data;
+ struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
+
+ *idx = x1e80100_wsa_port_index(cpu_dai->id);
+ if (*idx < 0)
+ return NULL;
+
+ return snd_soc_card_get_drvdata(rtd->card);
+}
+
+static int x1e80100_wsa_chmap_ctl_get(struct snd_kcontrol *kcontrol,
+ struct snd_ctl_elem_value *ucontrol)
+{
+ struct x1e80100_snd_data *data;
+ const unsigned int *map;
+ int i, idx;
+
+ data = x1e80100_wsa_chmap_kcontrol_data(kcontrol, &idx);
+ if (!data)
+ return -EINVAL;
+
+ guard(spinlock)(&data->wsa_chmap_lock);
+
+ /* Report the default map until userspace selects one */
+ map = data->wsa_chmap[idx];
+ if (!map[0])
+ map = x1e80100_wsa_default_chmap[data->wsa_channels[idx]];
+
+ for (i = 0; i < X1E80100_WSA_MAX_CHANNELS; i++)
+ ucontrol->value.integer.value[i] = map[i];
+
+ return 0;
+}
+
+static int x1e80100_wsa_chmap_ctl_put(struct snd_kcontrol *kcontrol,
+ struct snd_ctl_elem_value *ucontrol)
+{
+ unsigned int map[X1E80100_WSA_MAX_CHANNELS];
+ struct x1e80100_snd_data *data;
+ bool changed = false;
+ int i, idx;
+
+ data = x1e80100_wsa_chmap_kcontrol_data(kcontrol, &idx);
+ if (!data)
+ return -EINVAL;
+
+ for (i = 0; i < X1E80100_WSA_MAX_CHANNELS; i++) {
+ map[i] = ucontrol->value.integer.value[i];
+
+ if (map[i] && qcom_snd_chmap_to_q6(map[i]) < 0)
+ return -EINVAL;
+
+ /* Unset positions are allowed only as a suffix of the map */
+ if (map[i] && i && !map[i - 1])
+ return -EINVAL;
+ }
+
+ guard(spinlock)(&data->wsa_chmap_lock);
+
+ /* The map is applied on prepare; refuse updating prepared streams */
+ if (data->wsa_prepared[idx])
+ return -EBUSY;
+
+ for (i = 0; i < X1E80100_WSA_MAX_CHANNELS; i++) {
+ if (data->wsa_chmap[idx][i] != map[i]) {
+ data->wsa_chmap[idx][i] = map[i];
+ changed = true;
+ }
+ }
+
+ return changed ? 1 : 0;
+}
+
+static int x1e80100_add_wsa_chmap_ctls(struct snd_soc_pcm_runtime *rtd,
+ const char *name)
+{
+ struct snd_pcm_chmap *info;
+ unsigned int i;
+ int ret;
+
+ ret = snd_pcm_add_named_chmap_ctls(rtd->pcm, SNDRV_PCM_STREAM_PLAYBACK,
+ name, snd_pcm_std_chmaps,
+ X1E80100_WSA_MAX_CHANNELS, 0, &info);
+ if (ret)
+ return ret;
+
+ /*
+ * The core registers a query-only control. Make it writable so that
+ * userspace (UCM) can select the WSA channel map. The card is not yet
+ * registered at this point, so the control is not visible to
+ * userspace while being modified.
+ */
+ for (i = 0; i < info->kctl->count; i++)
+ info->kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
+ info->kctl->get = x1e80100_wsa_chmap_ctl_get;
+ info->kctl->put = x1e80100_wsa_chmap_ctl_put;
+
+ return 0;
+}
+
static int x1e80100_snd_init(struct snd_soc_pcm_runtime *rtd)
{
struct x1e80100_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
@@ -109,29 +247,43 @@ static int x1e80100_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
return 0;
}

-static int x1e80100_snd_hw_map_channels(unsigned int *ch_map, int num)
+static int x1e80100_snd_build_wsa_slots(struct x1e80100_snd_data *data,
+ unsigned int dai_id,
+ unsigned int channels,
+ unsigned int *rx_slot)
{
- switch (num) {
- case 1:
- ch_map[0] = PCM_CHANNEL_FC;
- break;
- case 2:
- ch_map[0] = PCM_CHANNEL_FL;
- ch_map[1] = PCM_CHANNEL_FR;
- break;
- case 3:
- ch_map[0] = PCM_CHANNEL_FL;
- ch_map[1] = PCM_CHANNEL_FR;
- ch_map[2] = PCM_CHANNEL_FC;
- break;
- case 4:
- ch_map[0] = PCM_CHANNEL_FL;
- ch_map[1] = PCM_CHANNEL_LB;
- ch_map[2] = PCM_CHANNEL_FR;
- ch_map[3] = PCM_CHANNEL_RB;
- break;
- default:
+ unsigned int map[X1E80100_WSA_MAX_CHANNELS];
+ unsigned int i, set = 0;
+ int idx;
+
+ idx = x1e80100_wsa_port_index(dai_id);
+ if (idx < 0)
+ return idx;
+
+ if (channels > X1E80100_WSA_MAX_CHANNELS ||
+ !x1e80100_wsa_default_chmap[channels][0])
return -EINVAL;
+
+ scoped_guard(spinlock, &data->wsa_chmap_lock) {
+ memcpy(map, data->wsa_chmap[idx], sizeof(map));
+ data->wsa_channels[idx] = channels;
+ data->wsa_prepared[idx] = true;
+ }
+
+ for (i = 0; i < X1E80100_WSA_MAX_CHANNELS; i++)
+ if (map[i])
+ set++;
+
+ /* Use the map only when it describes exactly this channel count */
+ if (set != channels)
+ memcpy(map, x1e80100_wsa_default_chmap[channels], sizeof(map));
+
+ for (i = 0; i < channels; i++) {
+ int q6 = qcom_snd_chmap_to_q6(map[i]);
+
+ if (q6 < 0)
+ return q6;
+ rx_slot[i] = q6;
}

return 0;
@@ -143,13 +295,14 @@ static int x1e80100_snd_prepare(struct snd_pcm_substream *substream)
struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
struct x1e80100_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
unsigned int channels = substream->runtime->channels;
- unsigned int rx_slot[4];
+ unsigned int rx_slot[X1E80100_WSA_MAX_CHANNELS];
int ret;

switch (cpu_dai->id) {
case WSA_CODEC_DMA_RX_0:
case WSA_CODEC_DMA_RX_1:
- ret = x1e80100_snd_hw_map_channels(rx_slot, channels);
+ ret = x1e80100_snd_build_wsa_slots(data, cpu_dai->id,
+ channels, rx_slot);
if (ret)
return ret;

@@ -170,6 +323,12 @@ static int x1e80100_snd_hw_free(struct snd_pcm_substream *substream)
struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
struct x1e80100_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
+ int idx = x1e80100_wsa_port_index(cpu_dai->id);
+
+ if (idx >= 0) {
+ guard(spinlock)(&data->wsa_chmap_lock);
+ data->wsa_prepared[idx] = false;
+ }

return qcom_snd_sdw_hw_free(substream, &data->stream_prepared[cpu_dai->id]);
}
@@ -181,6 +340,40 @@ static const struct snd_soc_ops x1e80100_be_ops = {
.prepare = x1e80100_snd_prepare,
};

+/*
+ * The WSA channel map controls are added here rather than in the DAI link
+ * init or startup callbacks: the backend PCMs are created only after all
+ * links are initialized, and adding controls from startup would take
+ * controls_rwsem under dapm_mutex, deadlocking against DAPM mixer writes.
+ */
+static int x1e80100_late_probe(struct snd_soc_card *card)
+{
+ struct snd_soc_pcm_runtime *rtd;
+ int ret;
+
+ for_each_card_rtds(card, rtd) {
+ struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
+ const char *name;
+
+ switch (cpu_dai->id) {
+ case WSA_CODEC_DMA_RX_0:
+ name = "WSA Playback Channel Map";
+ break;
+ case WSA_CODEC_DMA_RX_1:
+ name = "WSA2 Playback Channel Map";
+ break;
+ default:
+ continue;
+ }
+
+ ret = x1e80100_add_wsa_chmap_ctls(rtd, name);
+ if (ret)
+ return ret;
+ }
+
+ return 0;
+}
+
static void x1e80100_add_be_ops(struct snd_soc_card *card)
{
struct snd_soc_dai_link *link;
@@ -201,7 +394,7 @@ static int x1e80100_platform_probe(struct platform_device *pdev)
struct snd_soc_card *card;
struct x1e80100_snd_data *data;
struct device *dev = &pdev->dev;
- int ret;
+ int i, ret;

card = devm_kzalloc_obj(dev, *card);
if (!card)
@@ -211,6 +404,10 @@ static int x1e80100_platform_probe(struct platform_device *pdev)
if (!data)
return -ENOMEM;

+ spin_lock_init(&data->wsa_chmap_lock);
+ for (i = 0; i < X1E80100_WSA_PORTS; i++)
+ data->wsa_channels[i] = 2;
+
card->owner = THIS_MODULE;
card->dev = dev;
dev_set_drvdata(dev, card);
@@ -221,6 +418,7 @@ static int x1e80100_platform_probe(struct platform_device *pdev)
return ret;

card->driver_name = of_device_get_match_data(dev);
+ card->late_probe = x1e80100_late_probe;
x1e80100_add_be_ops(card);

return devm_snd_soc_register_card(dev, card);

--
2.54.0