xref: /openbmc/linux/sound/soc/qcom/common.c (revision f97cee494dc92395a668445bcd24d34c89f4ff8c)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018, Linaro Limited.
3 // Copyright (c) 2018, The Linux Foundation. All rights reserved.
4 
5 #include <linux/module.h>
6 #include "common.h"
7 
8 int qcom_snd_parse_of(struct snd_soc_card *card)
9 {
10 	struct device_node *np;
11 	struct device_node *codec = NULL;
12 	struct device_node *platform = NULL;
13 	struct device_node *cpu = NULL;
14 	struct device *dev = card->dev;
15 	struct snd_soc_dai_link *link;
16 	struct of_phandle_args args;
17 	struct snd_soc_dai_link_component *dlc;
18 	int ret, num_links;
19 
20 	ret = snd_soc_of_parse_card_name(card, "model");
21 	if (ret == 0 && !card->name)
22 		/* Deprecated, only for compatibility with old device trees */
23 		ret = snd_soc_of_parse_card_name(card, "qcom,model");
24 	if (ret) {
25 		dev_err(dev, "Error parsing card name: %d\n", ret);
26 		return ret;
27 	}
28 
29 	/* DAPM routes */
30 	if (of_property_read_bool(dev->of_node, "audio-routing")) {
31 		ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
32 		if (ret)
33 			return ret;
34 	}
35 	/* Deprecated, only for compatibility with old device trees */
36 	if (of_property_read_bool(dev->of_node, "qcom,audio-routing")) {
37 		ret = snd_soc_of_parse_audio_routing(card, "qcom,audio-routing");
38 		if (ret)
39 			return ret;
40 	}
41 
42 	/* Populate links */
43 	num_links = of_get_child_count(dev->of_node);
44 
45 	/* Allocate the DAI link array */
46 	card->dai_link = devm_kcalloc(dev, num_links, sizeof(*link), GFP_KERNEL);
47 	if (!card->dai_link)
48 		return -ENOMEM;
49 
50 	card->num_links = num_links;
51 	link = card->dai_link;
52 
53 	for_each_child_of_node(dev->of_node, np) {
54 		dlc = devm_kzalloc(dev, 2 * sizeof(*dlc), GFP_KERNEL);
55 		if (!dlc) {
56 			ret = -ENOMEM;
57 			goto err;
58 		}
59 
60 		link->cpus	= &dlc[0];
61 		link->platforms	= &dlc[1];
62 
63 		link->num_cpus		= 1;
64 		link->num_platforms	= 1;
65 
66 		ret = of_property_read_string(np, "link-name", &link->name);
67 		if (ret) {
68 			dev_err(card->dev, "error getting codec dai_link name\n");
69 			goto err;
70 		}
71 
72 		cpu = of_get_child_by_name(np, "cpu");
73 		platform = of_get_child_by_name(np, "platform");
74 		codec = of_get_child_by_name(np, "codec");
75 
76 		if (!cpu) {
77 			dev_err(dev, "%s: Can't find cpu DT node\n", link->name);
78 			ret = -EINVAL;
79 			goto err;
80 		}
81 
82 		ret = of_parse_phandle_with_args(cpu, "sound-dai",
83 					"#sound-dai-cells", 0, &args);
84 		if (ret) {
85 			dev_err(card->dev, "%s: error getting cpu phandle\n", link->name);
86 			goto err;
87 		}
88 		link->cpus->of_node = args.np;
89 		link->id = args.args[0];
90 
91 		ret = snd_soc_of_get_dai_name(cpu, &link->cpus->dai_name);
92 		if (ret) {
93 			if (ret != -EPROBE_DEFER)
94 				dev_err(card->dev, "%s: error getting cpu dai name: %d\n",
95 					link->name, ret);
96 			goto err;
97 		}
98 
99 		if (platform) {
100 			link->platforms->of_node = of_parse_phandle(platform,
101 					"sound-dai",
102 					0);
103 			if (!link->platforms->of_node) {
104 				dev_err(card->dev, "%s: platform dai not found\n", link->name);
105 				ret = -EINVAL;
106 				goto err;
107 			}
108 		} else {
109 			link->platforms->of_node = link->cpus->of_node;
110 		}
111 
112 		if (codec) {
113 			ret = snd_soc_of_get_dai_link_codecs(dev, codec, link);
114 			if (ret < 0) {
115 				if (ret != -EPROBE_DEFER)
116 					dev_err(card->dev, "%s: codec dai not found: %d\n",
117 						link->name, ret);
118 				goto err;
119 			}
120 
121 			if (platform) {
122 				/* DPCM backend */
123 				link->no_pcm = 1;
124 				link->ignore_pmdown_time = 1;
125 			}
126 		} else {
127 			/* DPCM frontend */
128 			dlc = devm_kzalloc(dev, sizeof(*dlc), GFP_KERNEL);
129 			if (!dlc)
130 				return -ENOMEM;
131 
132 			link->codecs	 = dlc;
133 			link->num_codecs = 1;
134 
135 			link->codecs->dai_name = "snd-soc-dummy-dai";
136 			link->codecs->name = "snd-soc-dummy";
137 			link->dynamic = 1;
138 		}
139 
140 		if (platform || !codec) {
141 			/* DPCM */
142 			snd_soc_dai_link_set_capabilities(link);
143 			link->ignore_suspend = 1;
144 			link->nonatomic = 1;
145 		}
146 
147 		link->stream_name = link->name;
148 		link++;
149 
150 		of_node_put(cpu);
151 		of_node_put(codec);
152 		of_node_put(platform);
153 	}
154 
155 	return 0;
156 err:
157 	of_node_put(np);
158 	of_node_put(cpu);
159 	of_node_put(codec);
160 	of_node_put(platform);
161 	return ret;
162 }
163 EXPORT_SYMBOL(qcom_snd_parse_of);
164 
165 MODULE_LICENSE("GPL v2");
166