[PATCH v3 1/3] ASoC: fsl_utils: Add snd_kcontrol functions for specific cases

Shengjiu Wang posted 3 patches 1 month, 1 week ago
There is a newer version of this series
[PATCH v3 1/3] ASoC: fsl_utils: Add snd_kcontrol functions for specific cases
Posted by Shengjiu Wang 1 month, 1 week ago
There are some registers which are volatile, at pm runtime suspend state,
the regmap cache only is enabled, regmap will return -EBUSY when trying to
access these registers.

static int _regmap_read(struct regmap *map, unsigned int reg,
                        unsigned int *val)
{
        int ret;
        void *context = _regmap_map_get_context(map);

        if (!map->cache_bypass) {
                ret = regcache_read(map, reg, val);
                if (ret == 0)
                        return 0;
        }

        if (map->cache_only)
                return -EBUSY;

        if (!regmap_readable(map, reg))
                return -EIO;

When exporting these registers by amixer interface to user space, there
will be -EBUSY errors in mixer-test when the cpu dai is in idle. In order
to avoid such error, needs to define FSL own functions to check pm runtime
status before calling snd_soc_get_xr_sx(), snd_soc_get_enum_double(),
snd_soc_get_volsw(), and so on, if pm runtime is not active, no register
accessing happen.

Signed-off-by: Shengjiu Wang <shengjiu.wang@nxp.com>
---
 sound/soc/fsl/fsl_utils.c | 89 +++++++++++++++++++++++++++++++++++++++
 sound/soc/fsl/fsl_utils.h | 30 +++++++++++++
 2 files changed, 119 insertions(+)

diff --git a/sound/soc/fsl/fsl_utils.c b/sound/soc/fsl/fsl_utils.c
index d69a6b9795bf..1b0f34408c00 100644
--- a/sound/soc/fsl/fsl_utils.c
+++ b/sound/soc/fsl/fsl_utils.c
@@ -10,6 +10,7 @@
 #include <linux/clk-provider.h>
 #include <linux/module.h>
 #include <linux/of_address.h>
+#include <linux/pm_runtime.h>
 #include <sound/soc.h>
 
 #include "fsl_utils.h"
@@ -197,6 +198,94 @@ void fsl_asoc_constrain_rates(struct snd_pcm_hw_constraint_list *target_constr,
 }
 EXPORT_SYMBOL(fsl_asoc_constrain_rates);
 
+/*
+ * Below functions are used by mixer interface to avoid accessing registers
+ * which are volatile at pm runtime suspend state (cache_only is enabled).
+ */
+int fsl_asoc_get_xr_sx(struct snd_kcontrol *kcontrol,
+		       struct snd_ctl_elem_value *ucontrol)
+{
+	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
+	bool pm_active = pm_runtime_active(component->dev);
+
+	if (!pm_active) {
+		ucontrol->value.integer.value[0] = 0;
+		return 0;
+	}
+
+	return snd_soc_get_xr_sx(kcontrol, ucontrol);
+}
+EXPORT_SYMBOL(fsl_asoc_get_xr_sx);
+
+int fsl_asoc_put_xr_sx(struct snd_kcontrol *kcontrol,
+		       struct snd_ctl_elem_value *ucontrol)
+{
+	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
+	bool pm_active = pm_runtime_active(component->dev);
+
+	if (!pm_active)
+		return 0;
+
+	return snd_soc_put_xr_sx(kcontrol, ucontrol);
+}
+EXPORT_SYMBOL(fsl_asoc_put_xr_sx);
+
+int fsl_asoc_get_enum_double(struct snd_kcontrol *kcontrol,
+			     struct snd_ctl_elem_value *ucontrol)
+{
+	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
+	bool pm_active = pm_runtime_active(component->dev);
+
+	if (!pm_active) {
+		ucontrol->value.enumerated.item[0] = 0;
+		return 0;
+	}
+
+	return snd_soc_get_enum_double(kcontrol, ucontrol);
+}
+EXPORT_SYMBOL(fsl_asoc_get_enum_double);
+
+int fsl_asoc_put_enum_double(struct snd_kcontrol *kcontrol,
+			     struct snd_ctl_elem_value *ucontrol)
+{
+	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
+	bool pm_active = pm_runtime_active(component->dev);
+
+	if (!pm_active)
+		return 0;
+
+	return snd_soc_put_enum_double(kcontrol, ucontrol);
+}
+EXPORT_SYMBOL(fsl_asoc_put_enum_double);
+
+int fsl_asoc_get_volsw(struct snd_kcontrol *kcontrol,
+		       struct snd_ctl_elem_value *ucontrol)
+{
+	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
+	bool pm_active = pm_runtime_active(component->dev);
+
+	if (!pm_active) {
+		ucontrol->value.integer.value[0] = 0;
+		return 0;
+	}
+
+	return snd_soc_get_volsw(kcontrol, ucontrol);
+}
+EXPORT_SYMBOL(fsl_asoc_get_volsw);
+
+int fsl_asoc_put_volsw(struct snd_kcontrol *kcontrol,
+		       struct snd_ctl_elem_value *ucontrol)
+{
+	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
+	bool pm_active = pm_runtime_active(component->dev);
+
+	if (!pm_active)
+		return 0;
+
+	return snd_soc_put_volsw(kcontrol, ucontrol);
+}
+EXPORT_SYMBOL(fsl_asoc_put_volsw);
+
 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
 MODULE_DESCRIPTION("Freescale ASoC utility code");
 MODULE_LICENSE("GPL v2");
diff --git a/sound/soc/fsl/fsl_utils.h b/sound/soc/fsl/fsl_utils.h
index 21b25a11ecda..0cf9d1e7fb14 100644
--- a/sound/soc/fsl/fsl_utils.h
+++ b/sound/soc/fsl/fsl_utils.h
@@ -31,4 +31,34 @@ void fsl_asoc_constrain_rates(struct snd_pcm_hw_constraint_list *target_constr,
 			      const struct snd_pcm_hw_constraint_list *original_constr,
 			      struct clk *pll8k_clk, struct clk *pll11k_clk,
 			      struct clk *ext_clk, int *target_rates);
+
+/* Similar to SOC_SINGLE_XR_SX, but it is for read only registers. */
+#define FSL_ASOC_SINGLE_XR_SX_EXT_RO(xname, xregbase, xregcount, xnbits, \
+				xmin, xmax, xinvert, xhandler_get) \
+{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), \
+	.access = SNDRV_CTL_ELEM_ACCESS_READ |		\
+		SNDRV_CTL_ELEM_ACCESS_VOLATILE,		\
+	.info = snd_soc_info_xr_sx, .get = xhandler_get, \
+	.private_value = (unsigned long)&(struct soc_mreg_control) \
+		{.regbase = xregbase, .regcount = xregcount, .nbits = xnbits, \
+		.invert = xinvert, .min = xmin, .max = xmax} }
+
+int fsl_asoc_get_xr_sx(struct snd_kcontrol *kcontrol,
+		       struct snd_ctl_elem_value *ucontrol);
+
+int fsl_asoc_put_xr_sx(struct snd_kcontrol *kcontrol,
+		       struct snd_ctl_elem_value *ucontrol);
+
+int fsl_asoc_get_enum_double(struct snd_kcontrol *kcontrol,
+			     struct snd_ctl_elem_value *ucontrol);
+
+int fsl_asoc_put_enum_double(struct snd_kcontrol *kcontrol,
+			     struct snd_ctl_elem_value *ucontrol);
+
+int fsl_asoc_get_volsw(struct snd_kcontrol *kcontrol,
+		       struct snd_ctl_elem_value *ucontrol);
+
+int fsl_asoc_put_volsw(struct snd_kcontrol *kcontrol,
+		       struct snd_ctl_elem_value *ucontrol);
+
 #endif /* _FSL_UTILS_H */
-- 
2.34.1
Re: [PATCH v3 1/3] ASoC: fsl_utils: Add snd_kcontrol functions for specific cases
Posted by Mark Brown 1 month ago
On Thu, Mar 05, 2026 at 11:10:29AM +0800, Shengjiu Wang wrote:

> +int fsl_asoc_get_xr_sx(struct snd_kcontrol *kcontrol,
> +		       struct snd_ctl_elem_value *ucontrol)
> +{
> +	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
> +	bool pm_active = pm_runtime_active(component->dev);
> +
> +	if (!pm_active) {
> +		ucontrol->value.integer.value[0] = 0;
> +		return 0;
> +	}
> +
> +	return snd_soc_get_xr_sx(kcontrol, ucontrol);
> +}

This is racy, what happens if we race with runtime PM disabling the
device so it gets powered off between the check and the call to the
generic function?  I think we need to take a runtime reference here, 
this is annoying but I can't see how else to do things robustly and
probably in practice there's not much performance impact.

> +EXPORT_SYMBOL(fsl_asoc_get_xr_sx);
> +
> +int fsl_asoc_put_xr_sx(struct snd_kcontrol *kcontrol,
> +		       struct snd_ctl_elem_value *ucontrol)
> +{
> +	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
> +	bool pm_active = pm_runtime_active(component->dev);
> +
> +	if (!pm_active)
> +		return 0;
> +
> +	return snd_soc_put_xr_sx(kcontrol, ucontrol);
> +}
> +EXPORT_SYMBOL(fsl_asoc_put_xr_sx);
> +
> +int fsl_asoc_get_enum_double(struct snd_kcontrol *kcontrol,
> +			     struct snd_ctl_elem_value *ucontrol)
> +{
> +	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
> +	bool pm_active = pm_runtime_active(component->dev);
> +
> +	if (!pm_active) {
> +		ucontrol->value.enumerated.item[0] = 0;
> +		return 0;
> +	}
> +
> +	return snd_soc_get_enum_double(kcontrol, ucontrol);
> +}
> +EXPORT_SYMBOL(fsl_asoc_get_enum_double);
> +
> +int fsl_asoc_put_enum_double(struct snd_kcontrol *kcontrol,
> +			     struct snd_ctl_elem_value *ucontrol)
> +{
> +	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
> +	bool pm_active = pm_runtime_active(component->dev);
> +
> +	if (!pm_active)
> +		return 0;
> +
> +	return snd_soc_put_enum_double(kcontrol, ucontrol);
> +}
> +EXPORT_SYMBOL(fsl_asoc_put_enum_double);
> +
> +int fsl_asoc_get_volsw(struct snd_kcontrol *kcontrol,
> +		       struct snd_ctl_elem_value *ucontrol)
> +{
> +	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
> +	bool pm_active = pm_runtime_active(component->dev);
> +
> +	if (!pm_active) {
> +		ucontrol->value.integer.value[0] = 0;
> +		return 0;
> +	}
> +
> +	return snd_soc_get_volsw(kcontrol, ucontrol);
> +}
> +EXPORT_SYMBOL(fsl_asoc_get_volsw);
> +
> +int fsl_asoc_put_volsw(struct snd_kcontrol *kcontrol,
> +		       struct snd_ctl_elem_value *ucontrol)
> +{
> +	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
> +	bool pm_active = pm_runtime_active(component->dev);
> +
> +	if (!pm_active)
> +		return 0;
> +
> +	return snd_soc_put_volsw(kcontrol, ucontrol);
> +}
> +EXPORT_SYMBOL(fsl_asoc_put_volsw);
> +
>  MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
>  MODULE_DESCRIPTION("Freescale ASoC utility code");
>  MODULE_LICENSE("GPL v2");
> diff --git a/sound/soc/fsl/fsl_utils.h b/sound/soc/fsl/fsl_utils.h
> index 21b25a11ecda..0cf9d1e7fb14 100644
> --- a/sound/soc/fsl/fsl_utils.h
> +++ b/sound/soc/fsl/fsl_utils.h
> @@ -31,4 +31,34 @@ void fsl_asoc_constrain_rates(struct snd_pcm_hw_constraint_list *target_constr,
>  			      const struct snd_pcm_hw_constraint_list *original_constr,
>  			      struct clk *pll8k_clk, struct clk *pll11k_clk,
>  			      struct clk *ext_clk, int *target_rates);
> +
> +/* Similar to SOC_SINGLE_XR_SX, but it is for read only registers. */
> +#define FSL_ASOC_SINGLE_XR_SX_EXT_RO(xname, xregbase, xregcount, xnbits, \
> +				xmin, xmax, xinvert, xhandler_get) \
> +{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), \
> +	.access = SNDRV_CTL_ELEM_ACCESS_READ |		\
> +		SNDRV_CTL_ELEM_ACCESS_VOLATILE,		\
> +	.info = snd_soc_info_xr_sx, .get = xhandler_get, \
> +	.private_value = (unsigned long)&(struct soc_mreg_control) \
> +		{.regbase = xregbase, .regcount = xregcount, .nbits = xnbits, \
> +		.invert = xinvert, .min = xmin, .max = xmax} }
> +
> +int fsl_asoc_get_xr_sx(struct snd_kcontrol *kcontrol,
> +		       struct snd_ctl_elem_value *ucontrol);
> +
> +int fsl_asoc_put_xr_sx(struct snd_kcontrol *kcontrol,
> +		       struct snd_ctl_elem_value *ucontrol);
> +
> +int fsl_asoc_get_enum_double(struct snd_kcontrol *kcontrol,
> +			     struct snd_ctl_elem_value *ucontrol);
> +
> +int fsl_asoc_put_enum_double(struct snd_kcontrol *kcontrol,
> +			     struct snd_ctl_elem_value *ucontrol);
> +
> +int fsl_asoc_get_volsw(struct snd_kcontrol *kcontrol,
> +		       struct snd_ctl_elem_value *ucontrol);
> +
> +int fsl_asoc_put_volsw(struct snd_kcontrol *kcontrol,
> +		       struct snd_ctl_elem_value *ucontrol);
> +
>  #endif /* _FSL_UTILS_H */
> -- 
> 2.34.1
> 
Re: [PATCH v3 1/3] ASoC: fsl_utils: Add snd_kcontrol functions for specific cases
Posted by Shengjiu Wang 1 month ago
On Thu, Mar 5, 2026 at 11:12 PM Mark Brown <broonie@kernel.org> wrote:
>
> On Thu, Mar 05, 2026 at 11:10:29AM +0800, Shengjiu Wang wrote:
>
> > +int fsl_asoc_get_xr_sx(struct snd_kcontrol *kcontrol,
> > +                    struct snd_ctl_elem_value *ucontrol)
> > +{
> > +     struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
> > +     bool pm_active = pm_runtime_active(component->dev);
> > +
> > +     if (!pm_active) {
> > +             ucontrol->value.integer.value[0] = 0;
> > +             return 0;
> > +     }
> > +
> > +     return snd_soc_get_xr_sx(kcontrol, ucontrol);
> > +}
>
> This is racy, what happens if we race with runtime PM disabling the
> device so it gets powered off between the check and the call to the
> generic function?  I think we need to take a runtime reference here,
> this is annoying but I can't see how else to do things robustly and
> probably in practice there's not much performance impact.

Ok, let me try this.
Thanks.

Best regards
Shengjiu Wang

>
> > +EXPORT_SYMBOL(fsl_asoc_get_xr_sx);
> > +
> > +int fsl_asoc_put_xr_sx(struct snd_kcontrol *kcontrol,
> > +                    struct snd_ctl_elem_value *ucontrol)
> > +{
> > +     struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
> > +     bool pm_active = pm_runtime_active(component->dev);
> > +
> > +     if (!pm_active)
> > +             return 0;
> > +
> > +     return snd_soc_put_xr_sx(kcontrol, ucontrol);
> > +}
> > +EXPORT_SYMBOL(fsl_asoc_put_xr_sx);
> > +
> > +int fsl_asoc_get_enum_double(struct snd_kcontrol *kcontrol,
> > +                          struct snd_ctl_elem_value *ucontrol)
> > +{
> > +     struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
> > +     bool pm_active = pm_runtime_active(component->dev);
> > +
> > +     if (!pm_active) {
> > +             ucontrol->value.enumerated.item[0] = 0;
> > +             return 0;
> > +     }
> > +
> > +     return snd_soc_get_enum_double(kcontrol, ucontrol);
> > +}
> > +EXPORT_SYMBOL(fsl_asoc_get_enum_double);
> > +
> > +int fsl_asoc_put_enum_double(struct snd_kcontrol *kcontrol,
> > +                          struct snd_ctl_elem_value *ucontrol)
> > +{
> > +     struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
> > +     bool pm_active = pm_runtime_active(component->dev);
> > +
> > +     if (!pm_active)
> > +             return 0;
> > +
> > +     return snd_soc_put_enum_double(kcontrol, ucontrol);
> > +}
> > +EXPORT_SYMBOL(fsl_asoc_put_enum_double);
> > +
> > +int fsl_asoc_get_volsw(struct snd_kcontrol *kcontrol,
> > +                    struct snd_ctl_elem_value *ucontrol)
> > +{
> > +     struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
> > +     bool pm_active = pm_runtime_active(component->dev);
> > +
> > +     if (!pm_active) {
> > +             ucontrol->value.integer.value[0] = 0;
> > +             return 0;
> > +     }
> > +
> > +     return snd_soc_get_volsw(kcontrol, ucontrol);
> > +}
> > +EXPORT_SYMBOL(fsl_asoc_get_volsw);
> > +
> > +int fsl_asoc_put_volsw(struct snd_kcontrol *kcontrol,
> > +                    struct snd_ctl_elem_value *ucontrol)
> > +{
> > +     struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
> > +     bool pm_active = pm_runtime_active(component->dev);
> > +
> > +     if (!pm_active)
> > +             return 0;
> > +
> > +     return snd_soc_put_volsw(kcontrol, ucontrol);
> > +}
> > +EXPORT_SYMBOL(fsl_asoc_put_volsw);
> > +
> >  MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
> >  MODULE_DESCRIPTION("Freescale ASoC utility code");
> >  MODULE_LICENSE("GPL v2");
> > diff --git a/sound/soc/fsl/fsl_utils.h b/sound/soc/fsl/fsl_utils.h
> > index 21b25a11ecda..0cf9d1e7fb14 100644
> > --- a/sound/soc/fsl/fsl_utils.h
> > +++ b/sound/soc/fsl/fsl_utils.h
> > @@ -31,4 +31,34 @@ void fsl_asoc_constrain_rates(struct snd_pcm_hw_constraint_list *target_constr,
> >                             const struct snd_pcm_hw_constraint_list *original_constr,
> >                             struct clk *pll8k_clk, struct clk *pll11k_clk,
> >                             struct clk *ext_clk, int *target_rates);
> > +
> > +/* Similar to SOC_SINGLE_XR_SX, but it is for read only registers. */
> > +#define FSL_ASOC_SINGLE_XR_SX_EXT_RO(xname, xregbase, xregcount, xnbits, \
> > +                             xmin, xmax, xinvert, xhandler_get) \
> > +{    .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), \
> > +     .access = SNDRV_CTL_ELEM_ACCESS_READ |          \
> > +             SNDRV_CTL_ELEM_ACCESS_VOLATILE,         \
> > +     .info = snd_soc_info_xr_sx, .get = xhandler_get, \
> > +     .private_value = (unsigned long)&(struct soc_mreg_control) \
> > +             {.regbase = xregbase, .regcount = xregcount, .nbits = xnbits, \
> > +             .invert = xinvert, .min = xmin, .max = xmax} }
> > +
> > +int fsl_asoc_get_xr_sx(struct snd_kcontrol *kcontrol,
> > +                    struct snd_ctl_elem_value *ucontrol);
> > +
> > +int fsl_asoc_put_xr_sx(struct snd_kcontrol *kcontrol,
> > +                    struct snd_ctl_elem_value *ucontrol);
> > +
> > +int fsl_asoc_get_enum_double(struct snd_kcontrol *kcontrol,
> > +                          struct snd_ctl_elem_value *ucontrol);
> > +
> > +int fsl_asoc_put_enum_double(struct snd_kcontrol *kcontrol,
> > +                          struct snd_ctl_elem_value *ucontrol);
> > +
> > +int fsl_asoc_get_volsw(struct snd_kcontrol *kcontrol,
> > +                    struct snd_ctl_elem_value *ucontrol);
> > +
> > +int fsl_asoc_put_volsw(struct snd_kcontrol *kcontrol,
> > +                    struct snd_ctl_elem_value *ucontrol);
> > +
> >  #endif /* _FSL_UTILS_H */
> > --
> > 2.34.1
> >