Current soc_pcm_open() calls snd_soc_component_open() under loop.
Thus, it needs to care about opened/not-yet-opened Component.
But, if soc-component.c is handling it, soc-pcm.c don't need to care
about it.
This patch adds opened flag to soc-component.h, and simplify soc-pcm.c.
This is one of prepare for cleanup soc-pcm-open()
Signed-off-by: Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
Reviewed-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
Link: https://lore.kernel.org/r/874kvzcey1.wl-kuninori.morimoto.gx@renesas.com
Signed-off-by: Mark Brown <broonie@kernel.org>
 
 
        unsigned int active;
 
-       unsigned int suspended:1; /* is in suspend PM state */
-
        struct list_head list;
        struct list_head card_aux_list; /* for auxiliary bound components */
        struct list_head card_list;
        struct dentry *debugfs_root;
        const char *debugfs_prefix;
 #endif
+
+       /* bit field */
+       unsigned int suspended:1; /* is in suspend PM state */
+       unsigned int opened:1;
+       unsigned int module:1;
 };
 
 #define for_each_component_dais(component, dai)\
 
 int snd_soc_component_module_get(struct snd_soc_component *component,
                                 int upon_open)
 {
+       if (component->module)
+               return 0;
+
        if (component->driver->module_get_upon_open == !!upon_open &&
            !try_module_get(component->dev->driver->owner))
                return -ENODEV;
 
+       component->module = 1;
+
        return 0;
 }
 
 void snd_soc_component_module_put(struct snd_soc_component *component,
                                  int upon_open)
 {
-       if (component->driver->module_get_upon_open == !!upon_open)
+       if (component->module &&
+           component->driver->module_get_upon_open == !!upon_open)
                module_put(component->dev->driver->owner);
+
+       component->module = 0;
 }
 
 int snd_soc_component_open(struct snd_soc_component *component,
                           struct snd_pcm_substream *substream)
 {
-       if (component->driver->open)
-               return component->driver->open(component, substream);
-       return 0;
+       int ret = 0;
+
+       if (!component->opened &&
+           component->driver->open)
+               ret = component->driver->open(component, substream);
+
+       if (ret == 0)
+               component->opened = 1;
+
+       return ret;
 }
 
 int snd_soc_component_close(struct snd_soc_component *component,
                            struct snd_pcm_substream *substream)
 {
-       if (component->driver->close)
-               return component->driver->close(component, substream);
-       return 0;
+       int ret = 0;
+
+       if (component->opened &&
+           component->driver->close)
+               ret = component->driver->close(component, substream);
+
+       component->opened = 0;
+
+       return ret;
 }
 
 int snd_soc_component_prepare(struct snd_soc_component *component,
 
        hw->rate_max = min_not_zero(hw->rate_max, rate_max);
 }
 
-static int soc_pcm_components_open(struct snd_pcm_substream *substream,
-                                  struct snd_soc_component **last)
+static int soc_pcm_components_open(struct snd_pcm_substream *substream)
 {
        struct snd_soc_pcm_runtime *rtd = substream->private_data;
        struct snd_soc_component *component;
        int i, ret = 0;
 
        for_each_rtd_components(rtd, i, component) {
-               *last = component;
-
                ret = snd_soc_component_module_get_when_open(component);
                if (ret < 0) {
                        dev_err(component->dev,
                        return ret;
                }
        }
-       *last = NULL;
+
        return 0;
 }
 
-static int soc_pcm_components_close(struct snd_pcm_substream *substream,
-                                   struct snd_soc_component *last)
+static int soc_pcm_components_close(struct snd_pcm_substream *substream)
 {
        struct snd_soc_pcm_runtime *rtd = substream->private_data;
        struct snd_soc_component *component;
        int i, r, ret = 0;
 
        for_each_rtd_components(rtd, i, component) {
-               if (component == last)
-                       break;
-
                r = snd_soc_component_close(component, substream);
                if (r < 0)
                        ret = r; /* use last ret */
                goto out;
        }
 
-       ret = soc_pcm_components_open(substream, &component);
+       ret = soc_pcm_components_open(substream);
        if (ret < 0)
                goto component_err;
 
                snd_soc_dai_shutdown(codec_dai, substream);
 
 component_err:
-       soc_pcm_components_close(substream, component);
+       soc_pcm_components_close(substream);
 
        snd_soc_dai_shutdown(cpu_dai, substream);
 out:
 
        soc_rtd_shutdown(rtd, substream);
 
-       soc_pcm_components_close(substream, NULL);
+       soc_pcm_components_close(substream);
 
        snd_soc_dapm_stream_stop(rtd, substream->stream);