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