ASoC: soc-core.c: share code for snd_soc_of_get_dai_link_cpus/codecs()
authorKuninori Morimoto <kuninori.morimoto.gx@renesas.com>
Wed, 22 Jun 2022 05:54:13 +0000 (05:54 +0000)
committerMark Brown <broonie@kernel.org>
Wed, 29 Jun 2022 17:10:15 +0000 (18:10 +0100)
ASoC has snd_soc_of_get_dai_link_cpus/codecs(), and these are almost same
code. The main difference are below.

for_each_link_cpus() dai_link->cpus dai_link->num_cpus
for_each_link_codecs() dai_link->codecs dai_link->num_codecs

Because we need to use these parameters, we can't share full-code for now,
but can share some codes.
This patch adds __snd_soc_of_get/put_xxx() functions, and share the code.

Signed-off-by: Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
Link: https://lore.kernel.org/r/87y1xpp7ju.wl-kuninori.morimoto.gx@renesas.com
Signed-off-by: Mark Brown <broonie@kernel.org>
sound/soc/soc-core.c

index 89d0163..e824ff1 100644 (file)
@@ -3303,6 +3303,61 @@ int snd_soc_of_get_dai_name(struct device_node *of_node,
 }
 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
 
+static void __snd_soc_of_put_component(struct snd_soc_dai_link_component *component)
+{
+       if (component->of_node) {
+               of_node_put(component->of_node);
+               component->of_node = NULL;
+       }
+}
+
+static int __snd_soc_of_get_dai_link_component_alloc(
+       struct device *dev, struct device_node *of_node,
+       struct snd_soc_dai_link_component **ret_component,
+       int *ret_num)
+{
+       struct snd_soc_dai_link_component *component;
+       int num;
+
+       /* Count the number of CPUs/CODECs */
+       num = of_count_phandle_with_args(of_node, "sound-dai", "#sound-dai-cells");
+       if (num <= 0) {
+               if (num == -ENOENT)
+                       dev_err(dev, "No 'sound-dai' property\n");
+               else
+                       dev_err(dev, "Bad phandle in 'sound-dai'\n");
+               return num;
+       }
+       component = devm_kcalloc(dev, num, sizeof(*component), GFP_KERNEL);
+       if (!component)
+               return -ENOMEM;
+
+       *ret_component  = component;
+       *ret_num        = num;
+
+       return 0;
+}
+
+static int __snd_soc_of_get_dai_link_component_parse(
+       struct device_node *of_node,
+       struct snd_soc_dai_link_component *component, int index)
+{
+       struct of_phandle_args args;
+       int ret;
+
+       ret = of_parse_phandle_with_args(of_node, "sound-dai", "#sound-dai-cells",
+                                        index, &args);
+       if (ret)
+               return ret;
+
+       ret = snd_soc_get_dai_name(&args, &component->dai_name);
+       if (ret < 0)
+               return ret;
+
+       component->of_node = args.np;
+       return 0;
+}
+
 /*
  * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
  * @dai_link: DAI link
@@ -3314,12 +3369,8 @@ void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
        struct snd_soc_dai_link_component *component;
        int index;
 
-       for_each_link_codecs(dai_link, index, component) {
-               if (!component->of_node)
-                       break;
-               of_node_put(component->of_node);
-               component->of_node = NULL;
-       }
+       for_each_link_codecs(dai_link, index, component)
+               __snd_soc_of_put_component(component);
 }
 EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
 
@@ -3341,41 +3392,19 @@ int snd_soc_of_get_dai_link_codecs(struct device *dev,
                                   struct device_node *of_node,
                                   struct snd_soc_dai_link *dai_link)
 {
-       struct of_phandle_args args;
        struct snd_soc_dai_link_component *component;
-       char *name;
-       int index, num_codecs, ret;
-
-       /* Count the number of CODECs */
-       name = "sound-dai";
-       num_codecs = of_count_phandle_with_args(of_node, name,
-                                               "#sound-dai-cells");
-       if (num_codecs <= 0) {
-               if (num_codecs == -ENOENT)
-                       dev_err(dev, "No 'sound-dai' property\n");
-               else
-                       dev_err(dev, "Bad phandle in 'sound-dai'\n");
-               return num_codecs;
-       }
-       component = devm_kcalloc(dev,
-                                num_codecs, sizeof(*component),
-                                GFP_KERNEL);
-       if (!component)
-               return -ENOMEM;
-       dai_link->codecs = component;
-       dai_link->num_codecs = num_codecs;
+       int index, ret;
+
+       ret = __snd_soc_of_get_dai_link_component_alloc(dev, of_node,
+                                        &dai_link->codecs, &dai_link->num_codecs);
+       if (ret < 0)
+               return ret;
 
        /* Parse the list */
        for_each_link_codecs(dai_link, index, component) {
-               ret = of_parse_phandle_with_args(of_node, name,
-                                                "#sound-dai-cells",
-                                                index, &args);
+               ret = __snd_soc_of_get_dai_link_component_parse(of_node, component, index);
                if (ret)
                        goto err;
-               component->of_node = args.np;
-               ret = snd_soc_get_dai_name(&args, &component->dai_name);
-               if (ret < 0)
-                       goto err;
        }
        return 0;
 err:
@@ -3397,12 +3426,8 @@ void snd_soc_of_put_dai_link_cpus(struct snd_soc_dai_link *dai_link)
        struct snd_soc_dai_link_component *component;
        int index;
 
-       for_each_link_cpus(dai_link, index, component) {
-               if (!component->of_node)
-                       break;
-               of_node_put(component->of_node);
-               component->of_node = NULL;
-       }
+       for_each_link_cpus(dai_link, index, component)
+               __snd_soc_of_put_component(component);
 }
 EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_cpus);
 
@@ -3421,41 +3446,20 @@ int snd_soc_of_get_dai_link_cpus(struct device *dev,
                                 struct device_node *of_node,
                                 struct snd_soc_dai_link *dai_link)
 {
-       struct of_phandle_args args;
        struct snd_soc_dai_link_component *component;
-       char *name;
-       int index, num_cpus, ret;
+       int index, ret;
 
        /* Count the number of CPUs */
-       name = "sound-dai";
-       num_cpus = of_count_phandle_with_args(of_node, name,
-                                               "#sound-dai-cells");
-       if (num_cpus <= 0) {
-               if (num_cpus == -ENOENT)
-                       dev_err(dev, "No 'sound-dai' property\n");
-               else
-                       dev_err(dev, "Bad phandle in 'sound-dai'\n");
-               return num_cpus;
-       }
-       component = devm_kcalloc(dev,
-                                num_cpus, sizeof(*component),
-                                GFP_KERNEL);
-       if (!component)
-               return -ENOMEM;
-       dai_link->cpus = component;
-       dai_link->num_cpus = num_cpus;
+       ret = __snd_soc_of_get_dai_link_component_alloc(dev, of_node,
+                                        &dai_link->cpus, &dai_link->num_cpus);
+       if (ret < 0)
+               return ret;
 
        /* Parse the list */
        for_each_link_cpus(dai_link, index, component) {
-               ret = of_parse_phandle_with_args(of_node, name,
-                                                "#sound-dai-cells",
-                                                index, &args);
+               ret = __snd_soc_of_get_dai_link_component_parse(of_node, component, index);
                if (ret)
                        goto err;
-               component->of_node = args.np;
-               ret = snd_soc_get_dai_name(&args, &component->dai_name);
-               if (ret < 0)
-                       goto err;
        }
        return 0;
 err: