[PATCH v3 05/21] ASoC: ops: Introduce 'soc_set_enum_kctl'

From: James Calligeros

Date: Sun Oct 18 2026 - 01:55:56 EST


From: Martin Povišer <povik+lin@xxxxxxxxxxx>

The new function is to be used to set enumerated controls to desired
values. It is something a machine driver may call in fixup_controls.

Signed-off-by: Martin Povišer <povik+lin@xxxxxxxxxxx>
Signed-off-by: James Calligeros <jcalligeros99@xxxxxxxxx>
---
include/sound/soc.h | 2 +
sound/soc/soc-ops.c | 67 +++++++++++++++++++++++++
2 files changed, 69 insertions(+)

diff --git a/include/sound/soc.h b/include/sound/soc.h
index 7509824a6b2e..dcfd706a73ad 100644
--- a/include/sound/soc.h
+++ b/include/sound/soc.h
@@ -580,6 +580,8 @@ int snd_soc_limit_volume(struct snd_soc_card *card,
const char *name, int max);
int snd_soc_ctl_activate(struct snd_soc_card *card,
const char *name, int active);
+int snd_soc_set_enum_kctl(struct snd_soc_card *card,
+ const char *name, const char *strval);
int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_info *uinfo);
int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
diff --git a/sound/soc/soc-ops.c b/sound/soc/soc-ops.c
index 295056bcc081..149a094112d2 100644
--- a/sound/soc/soc-ops.c
+++ b/sound/soc/soc-ops.c
@@ -503,6 +503,73 @@ int snd_soc_ctl_activate(struct snd_soc_card *card,
}
EXPORT_SYMBOL_GPL(snd_soc_ctl_activate);

+static int soc_set_enum_kctl(struct snd_kcontrol *kctl, const char *value)
+{
+ int sel, i, ret;
+
+ struct snd_ctl_elem_value *evalue __free(kfree) = kzalloc_obj(*evalue);
+ if (!evalue)
+ return -ENOMEM;
+
+ struct snd_ctl_elem_info *info __free(kfree) = kzalloc_obj(*info);
+ if (!info)
+ return -ENOMEM;
+
+ info->id = evalue->id = kctl->id;
+
+ ret = kctl->info(kctl, info);
+ if (ret < 0)
+ return ret;
+
+ if (!kctl->put)
+ return -EINVAL;
+
+ if (info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED)
+ return -EINVAL;
+
+ for (sel = 0; sel < info->value.enumerated.items; sel++) {
+ info->value.enumerated.item = sel;
+ ret = kctl->info(kctl, info);
+ if (ret < 0)
+ return ret;
+
+ if (!strcmp(value, info->value.enumerated.name))
+ break;
+ }
+
+ if (sel == info->value.enumerated.items)
+ return -ENOENT;
+
+ for (i = 0; i < info->count; i++)
+ evalue->value.enumerated.item[i] = sel;
+
+ return kctl->put(kctl, evalue);
+}
+
+/**
+ * snd_soc_set_enum_kctl - Set enumerated control matching a name
+ *
+ * @card: where to look for the controls
+ * @name: name
+ * @value: string value to set the controls to
+ *
+ * Return 0 on success, else error.
+ */
+int snd_soc_set_enum_kctl(struct snd_soc_card *card,
+ const char *name, const char *value)
+{
+ struct snd_kcontrol *kctl;
+
+ kctl = snd_soc_card_get_kcontrol(card, name);
+ if (!kctl) {
+ dev_dbg(card->dev, "Tried to set unknown control '%s' to '%s'\n", name, value);
+ return -EINVAL;
+ }
+
+ return soc_set_enum_kctl(kctl, value);
+}
+EXPORT_SYMBOL_GPL(snd_soc_set_enum_kctl);
+
int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_info *uinfo)
{

--
2.56.0