2023-05-23 15:16:33

by Herve Codina

[permalink] [raw]
Subject: [PATCH v2 6/9] ASoC: soc-dapm.h: Add a helper to build a DAPM widget dynamically

The SND_SOC_DAPM_* helpers family are used to build widgets array in a
static way.

Introduce SND_SOC_DAPM_WIDGET() in order to use the SND_SOC_DAPM_*
helpers family in a dynamic way. The different SND_SOC_DAPM_* parameters
can be computed by the code and the widget can be built based on this
parameter computation.
For instance:
static int create_widget(char *input_name)
{
struct snd_soc_dapm_widget widget;
char name*;
...
name = input_name;
if (!name)
name = "default";

widget = SND_SOC_DAPM_WIDGET(SND_SOC_DAPM_INPUT(name));
...
}

Signed-off-by: Herve Codina <[email protected]>
---
include/sound/soc-dapm.h | 12 +++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/include/sound/soc-dapm.h b/include/sound/soc-dapm.h
index 87f8e1793af1..6b62fe5c779c 100644
--- a/include/sound/soc-dapm.h
+++ b/include/sound/soc-dapm.h
@@ -276,7 +276,17 @@ struct soc_enum;
.reg = SND_SOC_NOPM, .event = dapm_pinctrl_event, \
.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD }

-
+/*
+ * Helper to create a widget 'dynamically'
+ * This can be used with any of the SND_SOC_DAPM_* widget helper.
+ * For instance:
+ * struct snd_soc_dapm_widget widget;
+ * ...
+ * widget = SND_SOC_DAPM_WIDGET(SND_SOC_DAPM_INPUT(input_name));
+ */
+#define SND_SOC_DAPM_WIDGET(_widget)({ \
+ struct snd_soc_dapm_widget _w[1] = { _widget }; \
+ _w[0]; })

/* dapm kcontrol types */
#define SOC_DAPM_DOUBLE(xname, reg, lshift, rshift, max, invert) \
--
2.40.1



2023-06-03 14:22:59

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 6/9] ASoC: soc-dapm.h: Add a helper to build a DAPM widget dynamically

Tue, May 23, 2023 at 05:12:20PM +0200, Herve Codina kirjoitti:
> The SND_SOC_DAPM_* helpers family are used to build widgets array in a
> static way.
>
> Introduce SND_SOC_DAPM_WIDGET() in order to use the SND_SOC_DAPM_*
> helpers family in a dynamic way. The different SND_SOC_DAPM_* parameters
> can be computed by the code and the widget can be built based on this
> parameter computation.
> For instance:
> static int create_widget(char *input_name)
> {
> struct snd_soc_dapm_widget widget;
> char name*;
> ...
> name = input_name;
> if (!name)
> name = "default";
>
> widget = SND_SOC_DAPM_WIDGET(SND_SOC_DAPM_INPUT(name));
> ...
> }

Maybe instead of adding a helper, simply convert those macros to provide
a compaund literal? (See, for example,
https://elixir.bootlin.com/linux/v6.4-rc4/source/include/linux/pinctrl/pinctrl.h#L42)

--
With Best Regards,
Andy Shevchenko



2023-06-05 08:57:32

by Herve Codina

[permalink] [raw]
Subject: Re: [PATCH v2 6/9] ASoC: soc-dapm.h: Add a helper to build a DAPM widget dynamically

On Sat, 3 Jun 2023 17:07:43 +0300
[email protected] wrote:

> Tue, May 23, 2023 at 05:12:20PM +0200, Herve Codina kirjoitti:
> > The SND_SOC_DAPM_* helpers family are used to build widgets array in a
> > static way.
> >
> > Introduce SND_SOC_DAPM_WIDGET() in order to use the SND_SOC_DAPM_*
> > helpers family in a dynamic way. The different SND_SOC_DAPM_* parameters
> > can be computed by the code and the widget can be built based on this
> > parameter computation.
> > For instance:
> > static int create_widget(char *input_name)
> > {
> > struct snd_soc_dapm_widget widget;
> > char name*;
> > ...
> > name = input_name;
> > if (!name)
> > name = "default";
> >
> > widget = SND_SOC_DAPM_WIDGET(SND_SOC_DAPM_INPUT(name));
> > ...
> > }
>
> Maybe instead of adding a helper, simply convert those macros to provide
> a compaund literal? (See, for example,
> https://elixir.bootlin.com/linux/v6.4-rc4/source/include/linux/pinctrl/pinctrl.h#L42)
>

Indeed, I will convert macros and remove the helper in the next iteration.

Thanks for the review,
Hervé

2023-06-05 12:47:11

by Herve Codina

[permalink] [raw]
Subject: Re: [PATCH v2 6/9] ASoC: soc-dapm.h: Add a helper to build a DAPM widget dynamically

Hi Andy,

On Sat, 3 Jun 2023 17:07:43 +0300
[email protected] wrote:

> Tue, May 23, 2023 at 05:12:20PM +0200, Herve Codina kirjoitti:
> > The SND_SOC_DAPM_* helpers family are used to build widgets array in a
> > static way.
> >
> > Introduce SND_SOC_DAPM_WIDGET() in order to use the SND_SOC_DAPM_*
> > helpers family in a dynamic way. The different SND_SOC_DAPM_* parameters
> > can be computed by the code and the widget can be built based on this
> > parameter computation.
> > For instance:
> > static int create_widget(char *input_name)
> > {
> > struct snd_soc_dapm_widget widget;
> > char name*;
> > ...
> > name = input_name;
> > if (!name)
> > name = "default";
> >
> > widget = SND_SOC_DAPM_WIDGET(SND_SOC_DAPM_INPUT(name));
> > ...
> > }
>
> Maybe instead of adding a helper, simply convert those macros to provide
> a compaund literal? (See, for example,
> https://elixir.bootlin.com/linux/v6.4-rc4/source/include/linux/pinctrl/pinctrl.h#L42)
>

Yes, I will convert them in the next iteration.

Thanks for the review,
Hervé