xref: /openbmc/linux/sound/soc/qcom/common.c (revision 85250a24)
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 <sound/jack.h>
7 #include <linux/input-event-codes.h>
8 #include "qdsp6/q6afe.h"
9 #include "common.h"
10 
11 int qcom_snd_parse_of(struct snd_soc_card *card)
12 {
13 	struct device_node *np;
14 	struct device_node *codec = NULL;
15 	struct device_node *platform = NULL;
16 	struct device_node *cpu = NULL;
17 	struct device *dev = card->dev;
18 	struct snd_soc_dai_link *link;
19 	struct of_phandle_args args;
20 	struct snd_soc_dai_link_component *dlc;
21 	int ret, num_links;
22 
23 	ret = snd_soc_of_parse_card_name(card, "model");
24 	if (ret == 0 && !card->name)
25 		/* Deprecated, only for compatibility with old device trees */
26 		ret = snd_soc_of_parse_card_name(card, "qcom,model");
27 	if (ret) {
28 		dev_err(dev, "Error parsing card name: %d\n", ret);
29 		return ret;
30 	}
31 
32 	if (of_property_read_bool(dev->of_node, "widgets")) {
33 		ret = snd_soc_of_parse_audio_simple_widgets(card, "widgets");
34 		if (ret)
35 			return ret;
36 	}
37 
38 	/* DAPM routes */
39 	if (of_property_read_bool(dev->of_node, "audio-routing")) {
40 		ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
41 		if (ret)
42 			return ret;
43 	}
44 	/* Deprecated, only for compatibility with old device trees */
45 	if (of_property_read_bool(dev->of_node, "qcom,audio-routing")) {
46 		ret = snd_soc_of_parse_audio_routing(card, "qcom,audio-routing");
47 		if (ret)
48 			return ret;
49 	}
50 
51 	ret = snd_soc_of_parse_pin_switches(card, "pin-switches");
52 	if (ret)
53 		return ret;
54 
55 	ret = snd_soc_of_parse_aux_devs(card, "aux-devs");
56 	if (ret)
57 		return ret;
58 
59 	/* Populate links */
60 	num_links = of_get_available_child_count(dev->of_node);
61 
62 	/* Allocate the DAI link array */
63 	card->dai_link = devm_kcalloc(dev, num_links, sizeof(*link), GFP_KERNEL);
64 	if (!card->dai_link)
65 		return -ENOMEM;
66 
67 	card->num_links = num_links;
68 	link = card->dai_link;
69 
70 	for_each_available_child_of_node(dev->of_node, np) {
71 		dlc = devm_kzalloc(dev, 2 * sizeof(*dlc), GFP_KERNEL);
72 		if (!dlc) {
73 			ret = -ENOMEM;
74 			goto err_put_np;
75 		}
76 
77 		link->cpus	= &dlc[0];
78 		link->platforms	= &dlc[1];
79 
80 		link->num_cpus		= 1;
81 		link->num_platforms	= 1;
82 
83 		ret = of_property_read_string(np, "link-name", &link->name);
84 		if (ret) {
85 			dev_err(card->dev, "error getting codec dai_link name\n");
86 			goto err_put_np;
87 		}
88 
89 		cpu = of_get_child_by_name(np, "cpu");
90 		platform = of_get_child_by_name(np, "platform");
91 		codec = of_get_child_by_name(np, "codec");
92 
93 		if (!cpu) {
94 			dev_err(dev, "%s: Can't find cpu DT node\n", link->name);
95 			ret = -EINVAL;
96 			goto err;
97 		}
98 
99 		ret = of_parse_phandle_with_args(cpu, "sound-dai",
100 					"#sound-dai-cells", 0, &args);
101 		if (ret) {
102 			dev_err(card->dev, "%s: error getting cpu phandle\n", link->name);
103 			goto err;
104 		}
105 		link->cpus->of_node = args.np;
106 		link->id = args.args[0];
107 
108 		ret = snd_soc_of_get_dai_name(cpu, &link->cpus->dai_name);
109 		if (ret) {
110 			dev_err_probe(card->dev, ret,
111 				      "%s: error getting cpu dai name\n", link->name);
112 			goto err;
113 		}
114 
115 		if (platform) {
116 			link->platforms->of_node = of_parse_phandle(platform,
117 					"sound-dai",
118 					0);
119 			if (!link->platforms->of_node) {
120 				dev_err(card->dev, "%s: platform dai not found\n", link->name);
121 				ret = -EINVAL;
122 				goto err;
123 			}
124 		} else {
125 			link->platforms->of_node = link->cpus->of_node;
126 		}
127 
128 		if (codec) {
129 			ret = snd_soc_of_get_dai_link_codecs(dev, codec, link);
130 			if (ret < 0) {
131 				dev_err_probe(card->dev, ret,
132 					      "%s: codec dai not found\n", link->name);
133 				goto err;
134 			}
135 
136 			if (platform) {
137 				/* DPCM backend */
138 				link->no_pcm = 1;
139 				link->ignore_pmdown_time = 1;
140 			}
141 		} else {
142 			/* DPCM frontend */
143 			dlc = devm_kzalloc(dev, sizeof(*dlc), GFP_KERNEL);
144 			if (!dlc) {
145 				ret = -ENOMEM;
146 				goto err;
147 			}
148 
149 			link->codecs	 = dlc;
150 			link->num_codecs = 1;
151 
152 			link->codecs->dai_name = "snd-soc-dummy-dai";
153 			link->codecs->name = "snd-soc-dummy";
154 			link->dynamic = 1;
155 		}
156 
157 		if (platform || !codec) {
158 			/* DPCM */
159 			snd_soc_dai_link_set_capabilities(link);
160 			link->ignore_suspend = 1;
161 			link->nonatomic = 1;
162 		}
163 
164 		link->stream_name = link->name;
165 		link++;
166 
167 		of_node_put(cpu);
168 		of_node_put(codec);
169 		of_node_put(platform);
170 	}
171 
172 	return 0;
173 err:
174 	of_node_put(cpu);
175 	of_node_put(codec);
176 	of_node_put(platform);
177 err_put_np:
178 	of_node_put(np);
179 	return ret;
180 }
181 EXPORT_SYMBOL_GPL(qcom_snd_parse_of);
182 
183 #if IS_ENABLED(CONFIG_SOUNDWIRE)
184 int qcom_snd_sdw_prepare(struct snd_pcm_substream *substream,
185 			 struct sdw_stream_runtime *sruntime,
186 			 bool *stream_prepared)
187 {
188 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
189 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
190 	int ret;
191 
192 	if (!sruntime)
193 		return 0;
194 
195 	switch (cpu_dai->id) {
196 	case WSA_CODEC_DMA_RX_0:
197 	case WSA_CODEC_DMA_RX_1:
198 	case RX_CODEC_DMA_RX_0:
199 	case RX_CODEC_DMA_RX_1:
200 	case TX_CODEC_DMA_TX_0:
201 	case TX_CODEC_DMA_TX_1:
202 	case TX_CODEC_DMA_TX_2:
203 	case TX_CODEC_DMA_TX_3:
204 		break;
205 	default:
206 		return 0;
207 	}
208 
209 	if (*stream_prepared) {
210 		sdw_disable_stream(sruntime);
211 		sdw_deprepare_stream(sruntime);
212 		*stream_prepared = false;
213 	}
214 
215 	ret = sdw_prepare_stream(sruntime);
216 	if (ret)
217 		return ret;
218 
219 	/**
220 	 * NOTE: there is a strict hw requirement about the ordering of port
221 	 * enables and actual WSA881x PA enable. PA enable should only happen
222 	 * after soundwire ports are enabled if not DC on the line is
223 	 * accumulated resulting in Click/Pop Noise
224 	 * PA enable/mute are handled as part of codec DAPM and digital mute.
225 	 */
226 
227 	ret = sdw_enable_stream(sruntime);
228 	if (ret) {
229 		sdw_deprepare_stream(sruntime);
230 		return ret;
231 	}
232 	*stream_prepared  = true;
233 
234 	return ret;
235 }
236 EXPORT_SYMBOL_GPL(qcom_snd_sdw_prepare);
237 
238 int qcom_snd_sdw_hw_params(struct snd_pcm_substream *substream,
239 			   struct snd_pcm_hw_params *params,
240 			   struct sdw_stream_runtime **psruntime)
241 {
242 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
243 	struct snd_soc_dai *codec_dai;
244 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
245 	struct sdw_stream_runtime *sruntime;
246 	int i;
247 
248 	switch (cpu_dai->id) {
249 	case WSA_CODEC_DMA_RX_0:
250 	case RX_CODEC_DMA_RX_0:
251 	case RX_CODEC_DMA_RX_1:
252 	case TX_CODEC_DMA_TX_0:
253 	case TX_CODEC_DMA_TX_1:
254 	case TX_CODEC_DMA_TX_2:
255 	case TX_CODEC_DMA_TX_3:
256 		for_each_rtd_codec_dais(rtd, i, codec_dai) {
257 			sruntime = snd_soc_dai_get_stream(codec_dai, substream->stream);
258 			if (sruntime != ERR_PTR(-ENOTSUPP))
259 				*psruntime = sruntime;
260 		}
261 		break;
262 	}
263 
264 	return 0;
265 
266 }
267 EXPORT_SYMBOL_GPL(qcom_snd_sdw_hw_params);
268 
269 int qcom_snd_sdw_hw_free(struct snd_pcm_substream *substream,
270 			 struct sdw_stream_runtime *sruntime, bool *stream_prepared)
271 {
272 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
273 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
274 
275 	switch (cpu_dai->id) {
276 	case WSA_CODEC_DMA_RX_0:
277 	case WSA_CODEC_DMA_RX_1:
278 	case RX_CODEC_DMA_RX_0:
279 	case RX_CODEC_DMA_RX_1:
280 	case TX_CODEC_DMA_TX_0:
281 	case TX_CODEC_DMA_TX_1:
282 	case TX_CODEC_DMA_TX_2:
283 	case TX_CODEC_DMA_TX_3:
284 		if (sruntime && *stream_prepared) {
285 			sdw_disable_stream(sruntime);
286 			sdw_deprepare_stream(sruntime);
287 			*stream_prepared = false;
288 		}
289 		break;
290 	default:
291 		break;
292 	}
293 
294 	return 0;
295 }
296 EXPORT_SYMBOL_GPL(qcom_snd_sdw_hw_free);
297 #endif
298 
299 int qcom_snd_wcd_jack_setup(struct snd_soc_pcm_runtime *rtd,
300 			    struct snd_soc_jack *jack, bool *jack_setup)
301 {
302 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
303 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
304 	struct snd_soc_card *card = rtd->card;
305 	int rval, i;
306 
307 	if (!*jack_setup) {
308 		rval = snd_soc_card_jack_new(card, "Headset Jack",
309 					     SND_JACK_HEADSET | SND_JACK_LINEOUT |
310 					     SND_JACK_MECHANICAL |
311 					     SND_JACK_BTN_0 | SND_JACK_BTN_1 |
312 					     SND_JACK_BTN_2 | SND_JACK_BTN_3 |
313 					     SND_JACK_BTN_4 | SND_JACK_BTN_5,
314 					     jack);
315 
316 		if (rval < 0) {
317 			dev_err(card->dev, "Unable to add Headphone Jack\n");
318 			return rval;
319 		}
320 
321 		snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_MEDIA);
322 		snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
323 		snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
324 		snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
325 		*jack_setup = true;
326 	}
327 
328 	switch (cpu_dai->id) {
329 	case TX_CODEC_DMA_TX_0:
330 	case TX_CODEC_DMA_TX_1:
331 	case TX_CODEC_DMA_TX_2:
332 	case TX_CODEC_DMA_TX_3:
333 		for_each_rtd_codec_dais(rtd, i, codec_dai) {
334 			rval = snd_soc_component_set_jack(codec_dai->component,
335 							  jack, NULL);
336 			if (rval != 0 && rval != -ENOTSUPP) {
337 				dev_warn(card->dev, "Failed to set jack: %d\n", rval);
338 				return rval;
339 			}
340 		}
341 
342 		break;
343 	default:
344 		break;
345 	}
346 
347 
348 	return 0;
349 }
350 EXPORT_SYMBOL_GPL(qcom_snd_wcd_jack_setup);
351 MODULE_LICENSE("GPL v2");
352