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 			of_node_put(sub_node);
52 			return -EINVAL;
53 		}
54 
55 		for_each_card_prelinks(card, i, dai_link) {
56 			if (!strcmp(dai_link_name, dai_link->name))
57 				break;
58 		}
59 
60 		if (i >= card->num_links) {
61 			of_node_put(sub_node);
62 			return -EINVAL;
63 		}
64 
65 		ret = set_card_codec_info(card, sub_node, dai_link);
66 		if (ret < 0) {
67 			of_node_put(sub_node);
68 			return ret;
69 		}
70 	}
71 
72 	return 0;
73 }
74 EXPORT_SYMBOL_GPL(parse_dai_link_info);
75 
76 void clean_card_reference(struct snd_soc_card *card)
77 {
78 	struct snd_soc_dai_link *dai_link;
79 	int i;
80 
81 	/* release codec reference gotten by set_card_codec_info */
82 	for_each_card_prelinks(card, i, dai_link)
83 		snd_soc_of_put_dai_link_codecs(dai_link);
84 }
85 EXPORT_SYMBOL_GPL(clean_card_reference);
86