• 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 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
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 	mutex_unlock(&rtd->card->pcm_mutex);
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 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
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 	mutex_unlock(&rtd->card->pcm_mutex);
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 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
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 	mutex_lock_nested(&fe->card->pcm_mutex, fe->card->pcm_subclass);
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 	mutex_unlock(&fe->card->pcm_mutex);
158 
159 	mutex_unlock(&fe->card->mutex);
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 be_err:
170 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
171 	mutex_unlock(&fe->card->mutex);
172 	return ret;
173 }
174 
soc_compr_free_fe(struct snd_compr_stream * cstream)175 static int soc_compr_free_fe(struct snd_compr_stream *cstream)
176 {
177 	struct snd_soc_pcm_runtime *fe = cstream->private_data;
178 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
179 	struct snd_soc_dpcm *dpcm;
180 	int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
181 
182 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
183 
184 	mutex_lock_nested(&fe->card->pcm_mutex, fe->card->pcm_subclass);
185 	snd_soc_runtime_deactivate(fe, stream);
186 
187 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
188 
189 	dpcm_be_dai_hw_free(fe, stream);
190 
191 	dpcm_be_dai_shutdown(fe, stream);
192 
193 	/* mark FE's links ready to prune */
194 	for_each_dpcm_be(fe, stream, dpcm)
195 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
196 
197 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
198 
199 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
200 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
201 
202 	dpcm_be_disconnect(fe, stream);
203 
204 	mutex_unlock(&fe->card->pcm_mutex);
205 
206 	fe->dpcm[stream].runtime = NULL;
207 
208 	snd_soc_link_compr_shutdown(cstream, 0);
209 
210 	snd_soc_component_compr_free(cstream, 0);
211 
212 	snd_soc_dai_compr_shutdown(cpu_dai, cstream, 0);
213 
214 	mutex_unlock(&fe->card->mutex);
215 	return 0;
216 }
217 
soc_compr_trigger(struct snd_compr_stream * cstream,int cmd)218 static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd)
219 {
220 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
221 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
222 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
223 	int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
224 	int ret;
225 
226 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
227 
228 	ret = snd_soc_component_compr_trigger(cstream, cmd);
229 	if (ret < 0)
230 		goto out;
231 
232 	ret = snd_soc_dai_compr_trigger(cpu_dai, cstream, cmd);
233 	if (ret < 0)
234 		goto out;
235 
236 	switch (cmd) {
237 	case SNDRV_PCM_TRIGGER_START:
238 		snd_soc_dai_digital_mute(codec_dai, 0, stream);
239 		break;
240 	case SNDRV_PCM_TRIGGER_STOP:
241 		snd_soc_dai_digital_mute(codec_dai, 1, stream);
242 		break;
243 	}
244 
245 out:
246 	mutex_unlock(&rtd->card->pcm_mutex);
247 	return ret;
248 }
249 
soc_compr_trigger_fe(struct snd_compr_stream * cstream,int cmd)250 static int soc_compr_trigger_fe(struct snd_compr_stream *cstream, int cmd)
251 {
252 	struct snd_soc_pcm_runtime *fe = cstream->private_data;
253 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
254 	int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
255 	int ret;
256 
257 	if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN ||
258 	    cmd == SND_COMPR_TRIGGER_DRAIN)
259 		return snd_soc_component_compr_trigger(cstream, cmd);
260 
261 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
262 
263 	ret = snd_soc_dai_compr_trigger(cpu_dai, cstream, cmd);
264 	if (ret < 0)
265 		goto out;
266 
267 	ret = snd_soc_component_compr_trigger(cstream, cmd);
268 	if (ret < 0)
269 		goto out;
270 
271 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
272 
273 	ret = dpcm_be_dai_trigger(fe, stream, cmd);
274 
275 	switch (cmd) {
276 	case SNDRV_PCM_TRIGGER_START:
277 	case SNDRV_PCM_TRIGGER_RESUME:
278 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
279 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
280 		break;
281 	case SNDRV_PCM_TRIGGER_STOP:
282 	case SNDRV_PCM_TRIGGER_SUSPEND:
283 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
284 		break;
285 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
286 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
287 		break;
288 	}
289 
290 out:
291 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
292 	mutex_unlock(&fe->card->mutex);
293 	return ret;
294 }
295 
soc_compr_set_params(struct snd_compr_stream * cstream,struct snd_compr_params * params)296 static int soc_compr_set_params(struct snd_compr_stream *cstream,
297 				struct snd_compr_params *params)
298 {
299 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
300 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
301 	int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
302 	int ret;
303 
304 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
305 
306 	/*
307 	 * First we call set_params for the CPU DAI, then the component
308 	 * driver this should configure the SoC side. If the machine has
309 	 * compressed ops then we call that as well. The expectation is
310 	 * that these callbacks will configure everything for this compress
311 	 * path, like configuring a PCM port for a CODEC.
312 	 */
313 	ret = snd_soc_dai_compr_set_params(cpu_dai, cstream, params);
314 	if (ret < 0)
315 		goto err;
316 
317 	ret = snd_soc_component_compr_set_params(cstream, params);
318 	if (ret < 0)
319 		goto err;
320 
321 	ret = snd_soc_link_compr_set_params(cstream);
322 	if (ret < 0)
323 		goto err;
324 
325 	snd_soc_dapm_stream_event(rtd, stream, SND_SOC_DAPM_STREAM_START);
326 
327 	/* cancel any delayed stream shutdown that is pending */
328 	rtd->pop_wait = 0;
329 	mutex_unlock(&rtd->card->pcm_mutex);
330 
331 	cancel_delayed_work_sync(&rtd->delayed_work);
332 
333 	return 0;
334 
335 err:
336 	mutex_unlock(&rtd->card->pcm_mutex);
337 	return ret;
338 }
339 
soc_compr_set_params_fe(struct snd_compr_stream * cstream,struct snd_compr_params * params)340 static int soc_compr_set_params_fe(struct snd_compr_stream *cstream,
341 				   struct snd_compr_params *params)
342 {
343 	struct snd_soc_pcm_runtime *fe = cstream->private_data;
344 	struct snd_pcm_substream *fe_substream =
345 		 fe->pcm->streams[cstream->direction].substream;
346 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
347 	int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
348 	int ret;
349 
350 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
351 
352 	/*
353 	 * Create an empty hw_params for the BE as the machine driver must
354 	 * fix this up to match DSP decoder and ASRC configuration.
355 	 * I.e. machine driver fixup for compressed BE is mandatory.
356 	 */
357 	memset(&fe->dpcm[fe_substream->stream].hw_params, 0,
358 		sizeof(struct snd_pcm_hw_params));
359 
360 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
361 
362 	ret = dpcm_be_dai_hw_params(fe, stream);
363 	if (ret < 0)
364 		goto out;
365 
366 	ret = dpcm_be_dai_prepare(fe, stream);
367 	if (ret < 0)
368 		goto out;
369 
370 	ret = snd_soc_dai_compr_set_params(cpu_dai, cstream, params);
371 	if (ret < 0)
372 		goto out;
373 
374 	ret = snd_soc_component_compr_set_params(cstream, params);
375 	if (ret < 0)
376 		goto out;
377 
378 	ret = snd_soc_link_compr_set_params(cstream);
379 	if (ret < 0)
380 		goto out;
381 	mutex_lock_nested(&fe->card->pcm_mutex, fe->card->pcm_subclass);
382 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
383 	mutex_unlock(&fe->card->pcm_mutex);
384 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
385 
386 out:
387 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
388 	mutex_unlock(&fe->card->mutex);
389 	return ret;
390 }
391 
soc_compr_get_params(struct snd_compr_stream * cstream,struct snd_codec * params)392 static int soc_compr_get_params(struct snd_compr_stream *cstream,
393 				struct snd_codec *params)
394 {
395 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
396 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
397 	int ret = 0;
398 
399 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
400 
401 	ret = snd_soc_dai_compr_get_params(cpu_dai, cstream, params);
402 	if (ret < 0)
403 		goto err;
404 
405 	ret = snd_soc_component_compr_get_params(cstream, params);
406 err:
407 	mutex_unlock(&rtd->card->pcm_mutex);
408 	return ret;
409 }
410 
soc_compr_ack(struct snd_compr_stream * cstream,size_t bytes)411 static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
412 {
413 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
414 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
415 	int ret;
416 
417 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
418 
419 	ret = snd_soc_dai_compr_ack(cpu_dai, cstream, bytes);
420 	if (ret < 0)
421 		goto err;
422 
423 	ret = snd_soc_component_compr_ack(cstream, bytes);
424 err:
425 	mutex_unlock(&rtd->card->pcm_mutex);
426 	return ret;
427 }
428 
soc_compr_pointer(struct snd_compr_stream * cstream,struct snd_compr_tstamp * tstamp)429 static int soc_compr_pointer(struct snd_compr_stream *cstream,
430 			     struct snd_compr_tstamp *tstamp)
431 {
432 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
433 	int ret;
434 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
435 
436 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
437 
438 	ret = snd_soc_dai_compr_pointer(cpu_dai, cstream, tstamp);
439 	if (ret < 0)
440 		goto out;
441 
442 	ret = snd_soc_component_compr_pointer(cstream, tstamp);
443 out:
444 	mutex_unlock(&rtd->card->pcm_mutex);
445 	return ret;
446 }
447 
soc_compr_set_metadata(struct snd_compr_stream * cstream,struct snd_compr_metadata * metadata)448 static int soc_compr_set_metadata(struct snd_compr_stream *cstream,
449 				  struct snd_compr_metadata *metadata)
450 {
451 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
452 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
453 	int ret;
454 
455 	ret = snd_soc_dai_compr_set_metadata(cpu_dai, cstream, metadata);
456 	if (ret < 0)
457 		return ret;
458 
459 	return snd_soc_component_compr_set_metadata(cstream, metadata);
460 }
461 
soc_compr_get_metadata(struct snd_compr_stream * cstream,struct snd_compr_metadata * metadata)462 static int soc_compr_get_metadata(struct snd_compr_stream *cstream,
463 				  struct snd_compr_metadata *metadata)
464 {
465 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
466 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
467 	int ret;
468 
469 	ret = snd_soc_dai_compr_get_metadata(cpu_dai, cstream, metadata);
470 	if (ret < 0)
471 		return ret;
472 
473 	return snd_soc_component_compr_get_metadata(cstream, metadata);
474 }
475 
476 /* ASoC Compress operations */
477 static struct snd_compr_ops soc_compr_ops = {
478 	.open		= soc_compr_open,
479 	.free		= soc_compr_free,
480 	.set_params	= soc_compr_set_params,
481 	.set_metadata   = soc_compr_set_metadata,
482 	.get_metadata	= soc_compr_get_metadata,
483 	.get_params	= soc_compr_get_params,
484 	.trigger	= soc_compr_trigger,
485 	.pointer	= soc_compr_pointer,
486 	.ack		= soc_compr_ack,
487 	.get_caps	= snd_soc_component_compr_get_caps,
488 	.get_codec_caps = snd_soc_component_compr_get_codec_caps,
489 };
490 
491 /* ASoC Dynamic Compress operations */
492 static struct snd_compr_ops soc_compr_dyn_ops = {
493 	.open		= soc_compr_open_fe,
494 	.free		= soc_compr_free_fe,
495 	.set_params	= soc_compr_set_params_fe,
496 	.get_params	= soc_compr_get_params,
497 	.set_metadata   = soc_compr_set_metadata,
498 	.get_metadata	= soc_compr_get_metadata,
499 	.trigger	= soc_compr_trigger_fe,
500 	.pointer	= soc_compr_pointer,
501 	.ack		= soc_compr_ack,
502 	.get_caps	= snd_soc_component_compr_get_caps,
503 	.get_codec_caps = snd_soc_component_compr_get_codec_caps,
504 };
505 
506 /**
507  * snd_soc_new_compress - create a new compress.
508  *
509  * @rtd: The runtime for which we will create compress
510  * @num: the device index number (zero based - shared with normal PCMs)
511  *
512  * Return: 0 for success, else error.
513  */
snd_soc_new_compress(struct snd_soc_pcm_runtime * rtd,int num)514 int snd_soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
515 {
516 	struct snd_soc_component *component;
517 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
518 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
519 	struct snd_compr *compr;
520 	struct snd_pcm *be_pcm;
521 	char new_name[64];
522 	int ret = 0, direction = 0;
523 	int playback = 0, capture = 0;
524 	int i;
525 
526 	/*
527 	 * make sure these are same value,
528 	 * and then use these as equally
529 	 */
530 	BUILD_BUG_ON((int)SNDRV_PCM_STREAM_PLAYBACK != (int)SND_COMPRESS_PLAYBACK);
531 	BUILD_BUG_ON((int)SNDRV_PCM_STREAM_CAPTURE  != (int)SND_COMPRESS_CAPTURE);
532 
533 	if (rtd->num_cpus > 1 ||
534 	    rtd->num_codecs > 1) {
535 		dev_err(rtd->card->dev,
536 			"Compress ASoC: Multi CPU/Codec not supported\n");
537 		return -EINVAL;
538 	}
539 
540 	if (!codec_dai) {
541 		dev_err(rtd->card->dev, "Missing codec\n");
542 		return -EINVAL;
543 	}
544 
545 	/* check client and interface hw capabilities */
546 	if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
547 	    snd_soc_dai_stream_valid(cpu_dai,   SNDRV_PCM_STREAM_PLAYBACK))
548 		playback = 1;
549 	if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
550 	    snd_soc_dai_stream_valid(cpu_dai,   SNDRV_PCM_STREAM_CAPTURE))
551 		capture = 1;
552 
553 	/*
554 	 * Compress devices are unidirectional so only one of the directions
555 	 * should be set, check for that (xor)
556 	 */
557 	if (playback + capture != 1) {
558 		dev_err(rtd->card->dev,
559 			"Compress ASoC: Invalid direction for P %d, C %d\n",
560 			playback, capture);
561 		return -EINVAL;
562 	}
563 
564 	if (playback)
565 		direction = SND_COMPRESS_PLAYBACK;
566 	else
567 		direction = SND_COMPRESS_CAPTURE;
568 
569 	compr = devm_kzalloc(rtd->card->dev, sizeof(*compr), GFP_KERNEL);
570 	if (!compr)
571 		return -ENOMEM;
572 
573 	compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
574 				  GFP_KERNEL);
575 	if (!compr->ops)
576 		return -ENOMEM;
577 
578 	if (rtd->dai_link->dynamic) {
579 		snprintf(new_name, sizeof(new_name), "(%s)",
580 			rtd->dai_link->stream_name);
581 
582 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
583 				rtd->dai_link->dpcm_playback,
584 				rtd->dai_link->dpcm_capture, &be_pcm);
585 		if (ret < 0) {
586 			dev_err(rtd->card->dev,
587 				"Compress ASoC: can't create compressed for %s: %d\n",
588 				rtd->dai_link->name, ret);
589 			return ret;
590 		}
591 
592 		/* inherit atomicity from DAI link */
593 		be_pcm->nonatomic = rtd->dai_link->nonatomic;
594 
595 		rtd->pcm = be_pcm;
596 		rtd->fe_compr = 1;
597 		if (rtd->dai_link->dpcm_playback)
598 			be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
599 		if (rtd->dai_link->dpcm_capture)
600 			be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
601 		memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
602 	} else {
603 		snprintf(new_name, sizeof(new_name), "%s %s-%d",
604 			rtd->dai_link->stream_name, codec_dai->name, num);
605 
606 		memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
607 	}
608 
609 	for_each_rtd_components(rtd, i, component) {
610 		if (!component->driver->compress_ops ||
611 		    !component->driver->compress_ops->copy)
612 			continue;
613 
614 		compr->ops->copy = snd_soc_component_compr_copy;
615 		break;
616 	}
617 
618 	ret = snd_compress_new(rtd->card->snd_card, num, direction,
619 				new_name, compr);
620 	if (ret < 0) {
621 		component = asoc_rtd_to_codec(rtd, 0)->component;
622 		dev_err(component->dev,
623 			"Compress ASoC: can't create compress for codec %s: %d\n",
624 			component->name, ret);
625 		return ret;
626 	}
627 
628 	/* DAPM dai link stream work */
629 	rtd->close_delayed_work_func = snd_soc_close_delayed_work;
630 
631 	rtd->compr = compr;
632 	compr->private_data = rtd;
633 
634 	dev_dbg(rtd->card->dev, "Compress ASoC: %s <-> %s mapping ok\n",
635 		codec_dai->name, cpu_dai->name);
636 
637 	return 0;
638 }
639 EXPORT_SYMBOL_GPL(snd_soc_new_compress);
640