From: Kuninori Morimoto <kuninori.morimoto.gx@xxxxxxxxxxx> 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@xxxxxxxxxxx> --- sound/soc/soc-core.c | 140 ++++++++++++++++++++++--------------------- 1 file changed, 72 insertions(+), 68 deletions(-) diff --git a/sound/soc/soc-core.c b/sound/soc/soc-core.c index 669e4164d167..5d5805e43240 100644 --- a/sound/soc/soc-core.c +++ b/sound/soc/soc-core.c @@ -3305,6 +3305,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 @@ -3316,12 +3371,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); @@ -3343,41 +3394,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: @@ -3399,12 +3428,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); @@ -3423,41 +3448,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: -- 2.25.1