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 		dev_dbg(dev, "%s no specified codec\n", dai_link->name);
26 		return 0;
27 	}
28 
29 	/* set card codec info */
30 	ret = snd_soc_of_get_dai_link_codecs(dev, codec_node, dai_link);
31 
32 	of_node_put(codec_node);
33 
34 	if (ret < 0)
35 		return dev_err_probe(dev, ret, "%s: codec dai not found\n",
36 				     dai_link->name);
37 
38 	return 0;
39 }
40 
41 static int set_dailink_daifmt(struct snd_soc_card *card,
42 			      struct device_node *sub_node,
43 			      struct snd_soc_dai_link *dai_link)
44 {
45 	unsigned int daifmt;
46 	const char *str;
47 	int ret;
48 	struct {
49 		char *name;
50 		unsigned int val;
51 	} of_clk_table[] = {
52 		{ "cpu",	SND_SOC_DAIFMT_CBC_CFC },
53 		{ "codec",	SND_SOC_DAIFMT_CBP_CFP },
54 	};
55 
56 	daifmt = snd_soc_daifmt_parse_format(sub_node, NULL);
57 	if (daifmt) {
58 		dai_link->dai_fmt &= SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK;
59 		dai_link->dai_fmt |= daifmt;
60 	}
61 
62 	/*
63 	 * check "mediatek,clk-provider = xxx"
64 	 * SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK area
65 	 */
66 	ret = of_property_read_string(sub_node, "mediatek,clk-provider", &str);
67 	if (ret == 0) {
68 		int i;
69 
70 		for (i = 0; i < ARRAY_SIZE(of_clk_table); i++) {
71 			if (strcmp(str, of_clk_table[i].name) == 0) {
72 				dai_link->dai_fmt &= ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK;
73 				dai_link->dai_fmt |= of_clk_table[i].val;
74 				break;
75 			}
76 		}
77 	}
78 
79 	return 0;
80 }
81 
82 int parse_dai_link_info(struct snd_soc_card *card)
83 {
84 	struct device *dev = card->dev;
85 	struct device_node *sub_node;
86 	struct snd_soc_dai_link *dai_link;
87 	const char *dai_link_name;
88 	int ret, i;
89 
90 	/* Loop over all the dai link sub nodes */
91 	for_each_available_child_of_node(dev->of_node, sub_node) {
92 		if (of_property_read_string(sub_node, "link-name",
93 					    &dai_link_name)) {
94 			of_node_put(sub_node);
95 			return -EINVAL;
96 		}
97 
98 		for_each_card_prelinks(card, i, dai_link) {
99 			if (!strcmp(dai_link_name, dai_link->name))
100 				break;
101 		}
102 
103 		if (i >= card->num_links) {
104 			of_node_put(sub_node);
105 			return -EINVAL;
106 		}
107 
108 		ret = set_card_codec_info(card, sub_node, dai_link);
109 		if (ret < 0) {
110 			of_node_put(sub_node);
111 			return ret;
112 		}
113 
114 		ret = set_dailink_daifmt(card, sub_node, dai_link);
115 		if (ret < 0) {
116 			of_node_put(sub_node);
117 			return ret;
118 		}
119 	}
120 
121 	return 0;
122 }
123 EXPORT_SYMBOL_GPL(parse_dai_link_info);
124 
125 void clean_card_reference(struct snd_soc_card *card)
126 {
127 	struct snd_soc_dai_link *dai_link;
128 	int i;
129 
130 	/* release codec reference gotten by set_card_codec_info */
131 	for_each_card_prelinks(card, i, dai_link)
132 		snd_soc_of_put_dai_link_codecs(dai_link);
133 }
134 EXPORT_SYMBOL_GPL(clean_card_reference);
135