xref: /openbmc/linux/sound/soc/qcom/common.c (revision b9f0bfd1)
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 	ret = snd_soc_of_parse_aux_devs(card, "aux-devs");
43 	if (ret)
44 		return ret;
45 
46 	/* Populate links */
47 	num_links = of_get_available_child_count(dev->of_node);
48 
49 	/* Allocate the DAI link array */
50 	card->dai_link = devm_kcalloc(dev, num_links, sizeof(*link), GFP_KERNEL);
51 	if (!card->dai_link)
52 		return -ENOMEM;
53 
54 	card->num_links = num_links;
55 	link = card->dai_link;
56 
57 	for_each_available_child_of_node(dev->of_node, np) {
58 		dlc = devm_kzalloc(dev, 2 * sizeof(*dlc), GFP_KERNEL);
59 		if (!dlc) {
60 			ret = -ENOMEM;
61 			goto err_put_np;
62 		}
63 
64 		link->cpus	= &dlc[0];
65 		link->platforms	= &dlc[1];
66 
67 		link->num_cpus		= 1;
68 		link->num_platforms	= 1;
69 
70 		ret = of_property_read_string(np, "link-name", &link->name);
71 		if (ret) {
72 			dev_err(card->dev, "error getting codec dai_link name\n");
73 			goto err_put_np;
74 		}
75 
76 		cpu = of_get_child_by_name(np, "cpu");
77 		platform = of_get_child_by_name(np, "platform");
78 		codec = of_get_child_by_name(np, "codec");
79 
80 		if (!cpu) {
81 			dev_err(dev, "%s: Can't find cpu DT node\n", link->name);
82 			ret = -EINVAL;
83 			goto err;
84 		}
85 
86 		ret = of_parse_phandle_with_args(cpu, "sound-dai",
87 					"#sound-dai-cells", 0, &args);
88 		if (ret) {
89 			dev_err(card->dev, "%s: error getting cpu phandle\n", link->name);
90 			goto err;
91 		}
92 		link->cpus->of_node = args.np;
93 		link->id = args.args[0];
94 
95 		ret = snd_soc_of_get_dai_name(cpu, &link->cpus->dai_name);
96 		if (ret) {
97 			dev_err_probe(card->dev, ret,
98 				      "%s: error getting cpu dai name\n", link->name);
99 			goto err;
100 		}
101 
102 		if (platform) {
103 			link->platforms->of_node = of_parse_phandle(platform,
104 					"sound-dai",
105 					0);
106 			if (!link->platforms->of_node) {
107 				dev_err(card->dev, "%s: platform dai not found\n", link->name);
108 				ret = -EINVAL;
109 				goto err;
110 			}
111 		} else {
112 			link->platforms->of_node = link->cpus->of_node;
113 		}
114 
115 		if (codec) {
116 			ret = snd_soc_of_get_dai_link_codecs(dev, codec, link);
117 			if (ret < 0) {
118 				dev_err_probe(card->dev, ret,
119 					      "%s: codec dai not found\n", link->name);
120 				goto err;
121 			}
122 
123 			if (platform) {
124 				/* DPCM backend */
125 				link->no_pcm = 1;
126 				link->ignore_pmdown_time = 1;
127 			}
128 		} else {
129 			/* DPCM frontend */
130 			dlc = devm_kzalloc(dev, sizeof(*dlc), GFP_KERNEL);
131 			if (!dlc) {
132 				ret = -ENOMEM;
133 				goto err;
134 			}
135 
136 			link->codecs	 = dlc;
137 			link->num_codecs = 1;
138 
139 			link->codecs->dai_name = "snd-soc-dummy-dai";
140 			link->codecs->name = "snd-soc-dummy";
141 			link->dynamic = 1;
142 		}
143 
144 		if (platform || !codec) {
145 			/* DPCM */
146 			snd_soc_dai_link_set_capabilities(link);
147 			link->ignore_suspend = 1;
148 			link->nonatomic = 1;
149 		}
150 
151 		link->stream_name = link->name;
152 		link++;
153 
154 		of_node_put(cpu);
155 		of_node_put(codec);
156 		of_node_put(platform);
157 	}
158 
159 	return 0;
160 err:
161 	of_node_put(cpu);
162 	of_node_put(codec);
163 	of_node_put(platform);
164 err_put_np:
165 	of_node_put(np);
166 	return ret;
167 }
168 EXPORT_SYMBOL(qcom_snd_parse_of);
169 
170 MODULE_LICENSE("GPL v2");
171