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