1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * mtk-soundcard-driver.c  --  MediaTek soundcard driver common
4  *
5  * Copyright (c) 2022 MediaTek Inc.
6  * Author: Trevor Wu <trevor.wu@mediatek.com>
7  */
8 
9 #include <linux/module.h>
10 #include <linux/of.h>
11 #include <sound/soc.h>
12 
13 #include "mtk-soundcard-driver.h"
14 
15 static int set_card_codec_info(struct snd_soc_card *card,
16 			       struct device_node *sub_node,
17 			       struct snd_soc_dai_link *dai_link)
18 {
19 	struct device *dev = card->dev;
20 	struct device_node *codec_node;
21 	int ret;
22 
23 	codec_node = of_get_child_by_name(sub_node, "codec");
24 	if (!codec_node)
25 		return -EINVAL;
26 
27 	/* set card codec info */
28 	ret = snd_soc_of_get_dai_link_codecs(dev, codec_node, dai_link);
29 
30 	of_node_put(codec_node);
31 
32 	if (ret < 0)
33 		return dev_err_probe(dev, ret, "%s: codec dai not found\n",
34 				     dai_link->name);
35 
36 	return 0;
37 }
38 
39 int parse_dai_link_info(struct snd_soc_card *card)
40 {
41 	struct device *dev = card->dev;
42 	struct device_node *sub_node;
43 	struct snd_soc_dai_link *dai_link;
44 	const char *dai_link_name;
45 	int ret, i;
46 
47 	/* Loop over all the dai link sub nodes */
48 	for_each_available_child_of_node(dev->of_node, sub_node) {
49 		if (of_property_read_string(sub_node, "link-name",
50 					    &dai_link_name))
51 			return -EINVAL;
52 
53 		for_each_card_prelinks(card, i, dai_link) {
54 			if (!strcmp(dai_link_name, dai_link->name))
55 				break;
56 		}
57 
58 		if (i >= card->num_links)
59 			return -EINVAL;
60 
61 		ret = set_card_codec_info(card, sub_node, dai_link);
62 		if (ret < 0)
63 			return ret;
64 	}
65 
66 	return 0;
67 }
68 EXPORT_SYMBOL_GPL(parse_dai_link_info);
69 
70 void clean_card_reference(struct snd_soc_card *card)
71 {
72 	struct snd_soc_dai_link *dai_link;
73 	int i;
74 
75 	/* release codec reference gotten by set_card_codec_info */
76 	for_each_card_prelinks(card, i, dai_link)
77 		snd_soc_of_put_dai_link_codecs(dai_link);
78 }
79 EXPORT_SYMBOL_GPL(clean_card_reference);
80