• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // soc-compress.c  --  ALSA SoC Compress
4 //
5 // Copyright (C) 2012 Intel Corp.
6 //
7 // Authors: Namarta Kohli <namartax.kohli@intel.com>
8 //          Ramesh Babu K V <ramesh.babu@linux.intel.com>
9 //          Vinod Koul <vinod.koul@linux.intel.com>
10 
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/delay.h>
14 #include <linux/slab.h>
15 #include <linux/workqueue.h>
16 #include <sound/core.h>
17 #include <sound/compress_params.h>
18 #include <sound/compress_driver.h>
19 #include <sound/soc.h>
20 #include <sound/initval.h>
21 #include <sound/soc-dpcm.h>
22 #include <sound/soc-link.h>
23 #include <linux/pm_runtime.h>
24 
soc_compr_clean(struct snd_compr_stream * cstream,int rollback)25 static int soc_compr_clean(struct snd_compr_stream *cstream, int rollback)
26 {
27 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
28 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
29 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
30 	int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
31 
32 	snd_soc_dpcm_mutex_lock(rtd);
33 
34 	if (!rollback)
35 		snd_soc_runtime_deactivate(rtd, stream);
36 
37 	snd_soc_dai_digital_mute(codec_dai, 1, stream);
38 
39 	if (!snd_soc_dai_active(cpu_dai))
40 		cpu_dai->rate = 0;
41 
42 	if (!snd_soc_dai_active(codec_dai))
43 		codec_dai->rate = 0;
44 
45 	snd_soc_link_compr_shutdown(cstream, rollback);
46 
47 	snd_soc_component_compr_free(cstream, rollback);
48 
49 	snd_soc_dai_compr_shutdown(cpu_dai, cstream, rollback);
50 
51 	if (!rollback)
52 		snd_soc_dapm_stream_stop(rtd, stream);
53 
54 	snd_soc_dpcm_mutex_unlock(rtd);
55 
56 	snd_soc_pcm_component_pm_runtime_put(rtd, cstream, rollback);
57 
58 	return 0;
59 }
60 
soc_compr_free(struct snd_compr_stream * cstream)61 static int soc_compr_free(struct snd_compr_stream *cstream)
62 {
63 	return soc_compr_clean(cstream, 0);
64 }
65 
soc_compr_open(struct snd_compr_stream * cstream)66 static int soc_compr_open(struct snd_compr_stream *cstream)
67 {
68 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
69 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
70 	int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
71 	int ret;
72 
73 	ret = snd_soc_pcm_component_pm_runtime_get(rtd, cstream);
74 	if (ret < 0)
75 		goto err_no_lock;
76 
77 	snd_soc_dpcm_mutex_lock(rtd);
78 
79 	ret = snd_soc_dai_compr_startup(cpu_dai, cstream);
80 	if (ret < 0)
81 		goto err;
82 
83 	ret = snd_soc_component_compr_open(cstream);
84 	if (ret < 0)
85 		goto err;
86 
87 	ret = snd_soc_link_compr_startup(cstream);
88 	if (ret < 0)
89 		goto err;
90 
91 	snd_soc_runtime_activate(rtd, stream);
92 err:
93 	snd_soc_dpcm_mutex_unlock(rtd);
94 err_no_lock:
95 	if (ret < 0)
96 		soc_compr_clean(cstream, 1);
97 
98 	return ret;
99 }
100 
soc_compr_open_fe(struct snd_compr_stream * cstream)101 static int soc_compr_open_fe(struct snd_compr_stream *cstream)
102 {
103 	struct snd_soc_pcm_runtime *fe = cstream->private_data;
104 	struct snd_pcm_substream *fe_substream =
105 		 fe->pcm->streams[cstream->direction].substream;
106 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
107 	struct snd_soc_dpcm *dpcm;
108 	struct snd_soc_dapm_widget_list *list;
109 	int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
110 	int ret;
111 
112 	snd_soc_card_mutex_lock(fe->card);
113 	fe->dpcm[stream].runtime = fe_substream->runtime;
114 
115 	ret = dpcm_path_get(fe, stream, &list);
116 	if (ret < 0)
117 		goto be_err;
118 
119 	snd_soc_dpcm_mutex_lock(fe);
120 
121 	/* calculate valid and active FE <-> BE dpcms */
122 	dpcm_process_paths(fe, stream, &list, 1);
123 	fe->dpcm[stream].runtime = fe_substream->runtime;
124 
125 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
126 
127 	ret = dpcm_be_dai_startup(fe, stream);
128 	if (ret < 0) {
129 		/* clean up all links */
130 		for_each_dpcm_be(fe, stream, dpcm)
131 			dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
132 
133 		dpcm_be_disconnect(fe, stream);
134 		fe->dpcm[stream].runtime = NULL;
135 		goto out;
136 	}
137 
138 	ret = snd_soc_dai_compr_startup(cpu_dai, cstream);
139 	if (ret < 0)
140 		goto out;
141 
142 	ret = snd_soc_component_compr_open(cstream);
143 	if (ret < 0)
144 		goto open_err;
145 
146 	ret = snd_soc_link_compr_startup(cstream);
147 	if (ret < 0)
148 		goto machine_err;
149 
150 	dpcm_clear_pending_state(fe, stream);
151 	dpcm_path_put(&list);
152 
153 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
154 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
155 
156 	snd_soc_runtime_activate(fe, stream);
157 	snd_soc_dpcm_mutex_unlock(fe);
158 
159 	snd_soc_card_mutex_unlock(fe->card);
160 
161 	return 0;
162 
163 machine_err:
164 	snd_soc_component_compr_free(cstream, 1);
165 open_err:
166 	snd_soc_dai_compr_shutdown(cpu_dai, cstream, 1);
167 out:
168 	dpcm_path_put(&list);
169 	snd_soc_dpcm_mutex_unlock(fe);
170 be_err:
171 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
172 	snd_soc_card_mutex_unlock(fe->card);
173 	return ret;
174 }
175 
soc_compr_free_fe(struct snd_compr_stream * cstream)176 static int soc_compr_free_fe(struct snd_compr_stream *cstream)
177 {
178 	struct snd_soc_pcm_runtime *fe = cstream->private_data;
179 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
180 	struct snd_soc_dpcm *dpcm;
181 	int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
182 
183 	snd_soc_card_mutex_lock(fe->card);
184 
185 	snd_soc_dpcm_mutex_lock(fe);
186 	snd_soc_runtime_deactivate(fe, stream);
187 
188 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
189 
190 	dpcm_be_dai_hw_free(fe, stream);
191 
192 	dpcm_be_dai_shutdown(fe, stream);
193 
194 	/* mark FE's links ready to prune */
195 	for_each_dpcm_be(fe, stream, dpcm)
196 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
197 
198 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
199 
200 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
201 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
202 
203 	dpcm_be_disconnect(fe, stream);
204 
205 	snd_soc_dpcm_mutex_unlock(fe);
206 
207 	fe->dpcm[stream].runtime = NULL;
208 
209 	snd_soc_link_compr_shutdown(cstream, 0);
210 
211 	snd_soc_component_compr_free(cstream, 0);
212 
213 	snd_soc_dai_compr_shutdown(cpu_dai, cstream, 0);
214 
215 	snd_soc_card_mutex_unlock(fe->card);
216 	return 0;
217 }
218 
soc_compr_trigger(struct snd_compr_stream * cstream,int cmd)219 static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd)
220 {
221 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
222 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
223 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
224 	int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
225 	int ret;
226 
227 	snd_soc_dpcm_mutex_lock(rtd);
228 
229 	ret = snd_soc_component_compr_trigger(cstream, cmd);
230 	if (ret < 0)
231 		goto out;
232 
233 	ret = snd_soc_dai_compr_trigger(cpu_dai, cstream, cmd);
234 	if (ret < 0)
235 		goto out;
236 
237 	switch (cmd) {
238 	case SNDRV_PCM_TRIGGER_START:
239 		snd_soc_dai_digital_mute(codec_dai, 0, stream);
240 		break;
241 	case SNDRV_PCM_TRIGGER_STOP:
242 		snd_soc_dai_digital_mute(codec_dai, 1, stream);
243 		break;
244 	}
245 
246 out:
247 	snd_soc_dpcm_mutex_unlock(rtd);
248 	return ret;
249 }
250 
soc_compr_trigger_fe(struct snd_compr_stream * cstream,int cmd)251 static int soc_compr_trigger_fe(struct snd_compr_stream *cstream, int cmd)
252 {
253 	struct snd_soc_pcm_runtime *fe = cstream->private_data;
254 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
255 	int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
256 	int ret;
257 
258 	if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN ||
259 	    cmd == SND_COMPR_TRIGGER_DRAIN)
260 		return snd_soc_component_compr_trigger(cstream, cmd);
261 
262 	snd_soc_card_mutex_lock(fe->card);
263 
264 	ret = snd_soc_dai_compr_trigger(cpu_dai, cstream, cmd);
265 	if (ret < 0)
266 		goto out;
267 
268 	ret = snd_soc_component_compr_trigger(cstream, cmd);
269 	if (ret < 0)
270 		goto out;
271 
272 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
273 
274 	ret = dpcm_be_dai_trigger(fe, stream, cmd);
275 
276 	switch (cmd) {
277 	case SNDRV_PCM_TRIGGER_START:
278 	case SNDRV_PCM_TRIGGER_RESUME:
279 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
280 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
281 		break;
282 	case SNDRV_PCM_TRIGGER_STOP:
283 	case SNDRV_PCM_TRIGGER_SUSPEND:
284 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
285 		break;
286 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
287 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
288 		break;
289 	}
290 
291 out:
292 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
293 	snd_soc_card_mutex_unlock(fe->card);
294 	return ret;
295 }
296 
soc_compr_set_params(struct snd_compr_stream * cstream,struct snd_compr_params * params)297 static int soc_compr_set_params(struct snd_compr_stream *cstream,
298 				struct snd_compr_params *params)
299 {
300 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
301 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
302 	int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
303 	int ret;
304 
305 	snd_soc_dpcm_mutex_lock(rtd);
306 
307 	/*
308 	 * First we call set_params for the CPU DAI, then the component
309 	 * driver this should configure the SoC side. If the machine has
310 	 * compressed ops then we call that as well. The expectation is
311 	 * that these callbacks will configure everything for this compress
312 	 * path, like configuring a PCM port for a CODEC.
313 	 */
314 	ret = snd_soc_dai_compr_set_params(cpu_dai, cstream, params);
315 	if (ret < 0)
316 		goto err;
317 
318 	ret = snd_soc_component_compr_set_params(cstream, params);
319 	if (ret < 0)
320 		goto err;
321 
322 	ret = snd_soc_link_compr_set_params(cstream);
323 	if (ret < 0)
324 		goto err;
325 
326 	snd_soc_dapm_stream_event(rtd, stream, SND_SOC_DAPM_STREAM_START);
327 
328 	/* cancel any delayed stream shutdown that is pending */
329 	rtd->pop_wait = 0;
330 	snd_soc_dpcm_mutex_unlock(rtd);
331 
332 	cancel_delayed_work_sync(&rtd->delayed_work);
333 
334 	return 0;
335 
336 err:
337 	snd_soc_dpcm_mutex_unlock(rtd);
338 	return ret;
339 }
340 
soc_compr_set_params_fe(struct snd_compr_stream * cstream,struct snd_compr_params * params)341 static int soc_compr_set_params_fe(struct snd_compr_stream *cstream,
342 				   struct snd_compr_params *params)
343 {
344 	struct snd_soc_pcm_runtime *fe = cstream->private_data;
345 	struct snd_pcm_substream *fe_substream =
346 		 fe->pcm->streams[cstream->direction].substream;
347 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
348 	int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
349 	int ret;
350 
351 	snd_soc_card_mutex_lock(fe->card);
352 
353 	/*
354 	 * Create an empty hw_params for the BE as the machine driver must
355 	 * fix this up to match DSP decoder and ASRC configuration.
356 	 * I.e. machine driver fixup for compressed BE is mandatory.
357 	 */
358 	memset(&fe->dpcm[fe_substream->stream].hw_params, 0,
359 		sizeof(struct snd_pcm_hw_params));
360 
361 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
362 
363 	ret = dpcm_be_dai_hw_params(fe, stream);
364 	if (ret < 0)
365 		goto out;
366 
367 	ret = dpcm_be_dai_prepare(fe, stream);
368 	if (ret < 0)
369 		goto out;
370 
371 	ret = snd_soc_dai_compr_set_params(cpu_dai, cstream, params);
372 	if (ret < 0)
373 		goto out;
374 
375 	ret = snd_soc_component_compr_set_params(cstream, params);
376 	if (ret < 0)
377 		goto out;
378 
379 	ret = snd_soc_link_compr_set_params(cstream);
380 	if (ret < 0)
381 		goto out;
382 	snd_soc_dpcm_mutex_lock(fe);
383 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
384 	snd_soc_dpcm_mutex_unlock(fe);
385 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
386 
387 out:
388 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
389 	snd_soc_card_mutex_unlock(fe->card);
390 	return ret;
391 }
392 
soc_compr_get_params(struct snd_compr_stream * cstream,struct snd_codec * params)393 static int soc_compr_get_params(struct snd_compr_stream *cstream,
394 				struct snd_codec *params)
395 {
396 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
397 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
398 	int ret = 0;
399 
400 	snd_soc_dpcm_mutex_lock(rtd);
401 
402 	ret = snd_soc_dai_compr_get_params(cpu_dai, cstream, params);
403 	if (ret < 0)
404 		goto err;
405 
406 	ret = snd_soc_component_compr_get_params(cstream, params);
407 err:
408 	snd_soc_dpcm_mutex_unlock(rtd);
409 	return ret;
410 }
411 
soc_compr_ack(struct snd_compr_stream * cstream,size_t bytes)412 static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
413 {
414 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
415 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
416 	int ret;
417 
418 	snd_soc_dpcm_mutex_lock(rtd);
419 
420 	ret = snd_soc_dai_compr_ack(cpu_dai, cstream, bytes);
421 	if (ret < 0)
422 		goto err;
423 
424 	ret = snd_soc_component_compr_ack(cstream, bytes);
425 err:
426 	snd_soc_dpcm_mutex_unlock(rtd);
427 	return ret;
428 }
429 
soc_compr_pointer(struct snd_compr_stream * cstream,struct snd_compr_tstamp * tstamp)430 static int soc_compr_pointer(struct snd_compr_stream *cstream,
431 			     struct snd_compr_tstamp *tstamp)
432 {
433 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
434 	int ret;
435 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
436 
437 	snd_soc_dpcm_mutex_lock(rtd);
438 
439 	ret = snd_soc_dai_compr_pointer(cpu_dai, cstream, tstamp);
440 	if (ret < 0)
441 		goto out;
442 
443 	ret = snd_soc_component_compr_pointer(cstream, tstamp);
444 out:
445 	snd_soc_dpcm_mutex_unlock(rtd);
446 	return ret;
447 }
448 
soc_compr_set_metadata(struct snd_compr_stream * cstream,struct snd_compr_metadata * metadata)449 static int soc_compr_set_metadata(struct snd_compr_stream *cstream,
450 				  struct snd_compr_metadata *metadata)
451 {
452 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
453 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
454 	int ret;
455 
456 	ret = snd_soc_dai_compr_set_metadata(cpu_dai, cstream, metadata);
457 	if (ret < 0)
458 		return ret;
459 
460 	return snd_soc_component_compr_set_metadata(cstream, metadata);
461 }
462 
soc_compr_get_metadata(struct snd_compr_stream * cstream,struct snd_compr_metadata * metadata)463 static int soc_compr_get_metadata(struct snd_compr_stream *cstream,
464 				  struct snd_compr_metadata *metadata)
465 {
466 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
467 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
468 	int ret;
469 
470 	ret = snd_soc_dai_compr_get_metadata(cpu_dai, cstream, metadata);
471 	if (ret < 0)
472 		return ret;
473 
474 	return snd_soc_component_compr_get_metadata(cstream, metadata);
475 }
476 
477 /* ASoC Compress operations */
478 static struct snd_compr_ops soc_compr_ops = {
479 	.open		= soc_compr_open,
480 	.free		= soc_compr_free,
481 	.set_params	= soc_compr_set_params,
482 	.set_metadata   = soc_compr_set_metadata,
483 	.get_metadata	= soc_compr_get_metadata,
484 	.get_params	= soc_compr_get_params,
485 	.trigger	= soc_compr_trigger,
486 	.pointer	= soc_compr_pointer,
487 	.ack		= soc_compr_ack,
488 	.get_caps	= snd_soc_component_compr_get_caps,
489 	.get_codec_caps = snd_soc_component_compr_get_codec_caps,
490 };
491 
492 /* ASoC Dynamic Compress operations */
493 static struct snd_compr_ops soc_compr_dyn_ops = {
494 	.open		= soc_compr_open_fe,
495 	.free		= soc_compr_free_fe,
496 	.set_params	= soc_compr_set_params_fe,
497 	.get_params	= soc_compr_get_params,
498 	.set_metadata   = soc_compr_set_metadata,
499 	.get_metadata	= soc_compr_get_metadata,
500 	.trigger	= soc_compr_trigger_fe,
501 	.pointer	= soc_compr_pointer,
502 	.ack		= soc_compr_ack,
503 	.get_caps	= snd_soc_component_compr_get_caps,
504 	.get_codec_caps = snd_soc_component_compr_get_codec_caps,
505 };
506 
507 /**
508  * snd_soc_new_compress - create a new compress.
509  *
510  * @rtd: The runtime for which we will create compress
511  * @num: the device index number (zero based - shared with normal PCMs)
512  *
513  * Return: 0 for success, else error.
514  */
snd_soc_new_compress(struct snd_soc_pcm_runtime * rtd,int num)515 int snd_soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
516 {
517 	struct snd_soc_component *component;
518 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
519 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
520 	struct snd_compr *compr;
521 	struct snd_pcm *be_pcm;
522 	char new_name[64];
523 	int ret = 0, direction = 0;
524 	int playback = 0, capture = 0;
525 	int i;
526 
527 	/*
528 	 * make sure these are same value,
529 	 * and then use these as equally
530 	 */
531 	BUILD_BUG_ON((int)SNDRV_PCM_STREAM_PLAYBACK != (int)SND_COMPRESS_PLAYBACK);
532 	BUILD_BUG_ON((int)SNDRV_PCM_STREAM_CAPTURE  != (int)SND_COMPRESS_CAPTURE);
533 
534 	if (rtd->num_cpus > 1 ||
535 	    rtd->num_codecs > 1) {
536 		dev_err(rtd->card->dev,
537 			"Compress ASoC: Multi CPU/Codec not supported\n");
538 		return -EINVAL;
539 	}
540 
541 	if (!codec_dai) {
542 		dev_err(rtd->card->dev, "Missing codec\n");
543 		return -EINVAL;
544 	}
545 
546 	/* check client and interface hw capabilities */
547 	if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
548 	    snd_soc_dai_stream_valid(cpu_dai,   SNDRV_PCM_STREAM_PLAYBACK))
549 		playback = 1;
550 	if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
551 	    snd_soc_dai_stream_valid(cpu_dai,   SNDRV_PCM_STREAM_CAPTURE))
552 		capture = 1;
553 
554 	/*
555 	 * Compress devices are unidirectional so only one of the directions
556 	 * should be set, check for that (xor)
557 	 */
558 	if (playback + capture != 1) {
559 		dev_err(rtd->card->dev,
560 			"Compress ASoC: Invalid direction for P %d, C %d\n",
561 			playback, capture);
562 		return -EINVAL;
563 	}
564 
565 	if (playback)
566 		direction = SND_COMPRESS_PLAYBACK;
567 	else
568 		direction = SND_COMPRESS_CAPTURE;
569 
570 	compr = devm_kzalloc(rtd->card->dev, sizeof(*compr), GFP_KERNEL);
571 	if (!compr)
572 		return -ENOMEM;
573 
574 	compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
575 				  GFP_KERNEL);
576 	if (!compr->ops)
577 		return -ENOMEM;
578 
579 	if (rtd->dai_link->dynamic) {
580 		snprintf(new_name, sizeof(new_name), "(%s)",
581 			rtd->dai_link->stream_name);
582 
583 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
584 				rtd->dai_link->dpcm_playback,
585 				rtd->dai_link->dpcm_capture, &be_pcm);
586 		if (ret < 0) {
587 			dev_err(rtd->card->dev,
588 				"Compress ASoC: can't create compressed for %s: %d\n",
589 				rtd->dai_link->name, ret);
590 			return ret;
591 		}
592 
593 		/* inherit atomicity from DAI link */
594 		be_pcm->nonatomic = rtd->dai_link->nonatomic;
595 
596 		rtd->pcm = be_pcm;
597 		rtd->fe_compr = 1;
598 		if (rtd->dai_link->dpcm_playback)
599 			be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
600 		if (rtd->dai_link->dpcm_capture)
601 			be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
602 		memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
603 	} else {
604 		snprintf(new_name, sizeof(new_name), "%s %s-%d",
605 			rtd->dai_link->stream_name, codec_dai->name, num);
606 
607 		memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
608 	}
609 
610 	for_each_rtd_components(rtd, i, component) {
611 		if (!component->driver->compress_ops ||
612 		    !component->driver->compress_ops->copy)
613 			continue;
614 
615 		compr->ops->copy = snd_soc_component_compr_copy;
616 		break;
617 	}
618 
619 	ret = snd_compress_new(rtd->card->snd_card, num, direction,
620 				new_name, compr);
621 	if (ret < 0) {
622 		component = asoc_rtd_to_codec(rtd, 0)->component;
623 		dev_err(component->dev,
624 			"Compress ASoC: can't create compress for codec %s: %d\n",
625 			component->name, ret);
626 		return ret;
627 	}
628 
629 	/* DAPM dai link stream work */
630 	rtd->close_delayed_work_func = snd_soc_close_delayed_work;
631 
632 	rtd->compr = compr;
633 	compr->private_data = rtd;
634 
635 	dev_dbg(rtd->card->dev, "Compress ASoC: %s <-> %s mapping ok\n",
636 		codec_dai->name, cpu_dai->name);
637 
638 	return 0;
639 }
640 EXPORT_SYMBOL_GPL(snd_soc_new_compress);
641