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