[PATCH v2 07/21] ASoC: ops: Introduce 'soc_set_enum_kctl'
Cezary Rojewski
cezary.rojewski at intel.com
Fri Oct 9 01:27:22 PDT 2026
On 10/4/2026 8:03 AM, James Calligeros wrote:
> From: Martin Povišer <povik+lin at cutebit.org>
>
> 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.
> +++ b/sound/soc/soc-ops.c
> @@ -505,6 +505,75 @@ int snd_soc_deactivate_kctl(struct snd_soc_card *card,
> }
> EXPORT_SYMBOL_GPL(snd_soc_deactivate_kctl);
>
> +static int soc_set_enum_kctl(struct snd_kcontrol *kctl, const char *value)
> +{
> + struct snd_ctl_elem_value evalue = { 0 };
> + struct snd_ctl_elem_info info = { 0 };
> + int sel, i, ret;
> +
> + ret = kctl->info(kctl, &info);
> + if (ret < 0)
> + return ret;
> +
> + 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 -EINVAL;
ENOENT may be a better candidate.
> +
> + for (i = 0; i < info.count; i++)
> + evalue.value.enumerated.item[i] = sel;
> +
> + if (kctl->put)
> + return kctl->put(kctl, &evalue);
> + else
> + return -EINVAL;
The if-else statement is redundant here, drop the 'else' part. If fact,
it would be better to check for ->put() upfront rather than when all of
this iterating is done.
> +}
> +
> +/**
> + * 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;
> + int ret = -EINVAL;
> +
> + /* Sanity check for name */
> + if (unlikely(!name))
> + return -EINVAL;
I'd leave the validation job to snd_soc_card_get_kcontrol().
> +
> + kctl = snd_soc_card_get_kcontrol(card, name);
> +
> + if (kctl) {
> + ret = soc_set_enum_kctl(kctl, value);
> + if (ret)
> + return ret;
The if-statement is redundant.
> + } else {
> + dev_dbg(card->dev, "Tried to set unknown control '%s' to '%s'\n",
> + name, value);
One line.
Maybe just refactor the entire block:
kctl = snd_soc_card_get_kcontrol(..)
if (!kctl) {
dev_dbg(...);
return -EINVAL;
}
return snd_set_enum_kctl(...);
With that, drop the assignment for 'ret' in the declaration block.
> + }
> +
> + return ret;
> +}
> +EXPORT_SYMBOL_GPL(snd_soc_set_enum_kctl);
> +
> int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
> struct snd_ctl_elem_info *uinfo)
> {
>
More information about the Linux-mediatek
mailing list