1 // SPDX-License-Identifier: GPL-2.0+
3 // soc-compress.c -- ALSA SoC Compress
5 // Copyright (C) 2012 Intel Corp.
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>
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>
23 static int soc_compr_components_open(struct snd_compr_stream *cstream,
24 struct snd_soc_component **last)
26 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
27 struct snd_soc_component *component;
28 struct snd_soc_rtdcom_list *rtdcom;
31 for_each_rtd_components(rtd, rtdcom, component) {
32 if (!component->driver->compr_ops ||
33 !component->driver->compr_ops->open)
36 ret = component->driver->compr_ops->open(cstream);
38 dev_err(component->dev,
39 "Compress ASoC: can't open platform %s: %d\n",
40 component->name, ret);
51 static int soc_compr_components_free(struct snd_compr_stream *cstream,
52 struct snd_soc_component *last)
54 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
55 struct snd_soc_component *component;
56 struct snd_soc_rtdcom_list *rtdcom;
58 for_each_rtd_components(rtd, rtdcom, component) {
59 if (component == last)
62 if (!component->driver->compr_ops ||
63 !component->driver->compr_ops->free)
66 component->driver->compr_ops->free(cstream);
72 static int soc_compr_open(struct snd_compr_stream *cstream)
74 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
75 struct snd_soc_component *component;
76 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
79 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
81 if (cpu_dai->driver->cops && cpu_dai->driver->cops->startup) {
82 ret = cpu_dai->driver->cops->startup(cstream, cpu_dai);
85 "Compress ASoC: can't open interface %s: %d\n",
91 ret = soc_compr_components_open(cstream, &component);
95 if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->startup) {
96 ret = rtd->dai_link->compr_ops->startup(cstream);
99 "Compress ASoC: %s startup failed: %d\n",
100 rtd->dai_link->name, ret);
105 snd_soc_runtime_activate(rtd, cstream->direction);
107 mutex_unlock(&rtd->card->pcm_mutex);
112 soc_compr_components_free(cstream, component);
114 if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
115 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
117 mutex_unlock(&rtd->card->pcm_mutex);
121 static int soc_compr_open_fe(struct snd_compr_stream *cstream)
123 struct snd_soc_pcm_runtime *fe = cstream->private_data;
124 struct snd_pcm_substream *fe_substream =
125 fe->pcm->streams[cstream->direction].substream;
126 struct snd_soc_component *component;
127 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
128 struct snd_soc_dpcm *dpcm;
129 struct snd_soc_dapm_widget_list *list;
133 if (cstream->direction == SND_COMPRESS_PLAYBACK)
134 stream = SNDRV_PCM_STREAM_PLAYBACK;
136 stream = SNDRV_PCM_STREAM_CAPTURE;
138 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
139 fe->dpcm[stream].runtime = fe_substream->runtime;
141 ret = dpcm_path_get(fe, stream, &list);
145 dev_dbg(fe->dev, "Compress ASoC: %s no valid %s route\n",
146 fe->dai_link->name, stream ? "capture" : "playback");
147 /* calculate valid and active FE <-> BE dpcms */
148 dpcm_process_paths(fe, stream, &list, 1);
149 fe->dpcm[stream].runtime = fe_substream->runtime;
151 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
153 ret = dpcm_be_dai_startup(fe, stream);
155 /* clean up all links */
156 for_each_dpcm_be(fe, stream, dpcm)
157 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
159 dpcm_be_disconnect(fe, stream);
160 fe->dpcm[stream].runtime = NULL;
164 if (cpu_dai->driver->cops && cpu_dai->driver->cops->startup) {
165 ret = cpu_dai->driver->cops->startup(cstream, cpu_dai);
167 dev_err(cpu_dai->dev,
168 "Compress ASoC: can't open interface %s: %d\n",
174 ret = soc_compr_components_open(cstream, &component);
178 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->startup) {
179 ret = fe->dai_link->compr_ops->startup(cstream);
181 pr_err("Compress ASoC: %s startup failed: %d\n",
182 fe->dai_link->name, ret);
187 dpcm_clear_pending_state(fe, stream);
188 dpcm_path_put(&list);
190 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
191 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
193 snd_soc_runtime_activate(fe, stream);
195 mutex_unlock(&fe->card->mutex);
200 soc_compr_components_free(cstream, component);
202 if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
203 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
205 dpcm_path_put(&list);
207 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
208 mutex_unlock(&fe->card->mutex);
213 * Power down the audio subsystem pmdown_time msecs after close is called.
214 * This is to ensure there are no pops or clicks in between any music tracks
215 * due to DAPM power cycling.
217 static void close_delayed_work(struct snd_soc_pcm_runtime *rtd)
219 struct snd_soc_dai *codec_dai = rtd->codec_dai;
221 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
224 "Compress ASoC: pop wq checking: %s status: %s waiting: %s\n",
225 codec_dai->driver->playback.stream_name,
226 codec_dai->playback_active ? "active" : "inactive",
227 rtd->pop_wait ? "yes" : "no");
229 /* are we waiting on this codec DAI stream */
230 if (rtd->pop_wait == 1) {
232 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
233 SND_SOC_DAPM_STREAM_STOP);
236 mutex_unlock(&rtd->card->pcm_mutex);
239 static int soc_compr_free(struct snd_compr_stream *cstream)
241 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
242 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
243 struct snd_soc_dai *codec_dai = rtd->codec_dai;
246 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
248 if (cstream->direction == SND_COMPRESS_PLAYBACK)
249 stream = SNDRV_PCM_STREAM_PLAYBACK;
251 stream = SNDRV_PCM_STREAM_CAPTURE;
253 snd_soc_runtime_deactivate(rtd, stream);
255 snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
257 if (!cpu_dai->active)
260 if (!codec_dai->active)
263 if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->shutdown)
264 rtd->dai_link->compr_ops->shutdown(cstream);
266 soc_compr_components_free(cstream, NULL);
268 if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
269 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
271 if (cstream->direction == SND_COMPRESS_PLAYBACK) {
272 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
273 snd_soc_dapm_stream_event(rtd,
274 SNDRV_PCM_STREAM_PLAYBACK,
275 SND_SOC_DAPM_STREAM_STOP);
278 queue_delayed_work(system_power_efficient_wq,
280 msecs_to_jiffies(rtd->pmdown_time));
283 /* capture streams can be powered down now */
284 snd_soc_dapm_stream_event(rtd,
285 SNDRV_PCM_STREAM_CAPTURE,
286 SND_SOC_DAPM_STREAM_STOP);
289 mutex_unlock(&rtd->card->pcm_mutex);
293 static int soc_compr_free_fe(struct snd_compr_stream *cstream)
295 struct snd_soc_pcm_runtime *fe = cstream->private_data;
296 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
297 struct snd_soc_dpcm *dpcm;
300 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
302 if (cstream->direction == SND_COMPRESS_PLAYBACK)
303 stream = SNDRV_PCM_STREAM_PLAYBACK;
305 stream = SNDRV_PCM_STREAM_CAPTURE;
307 snd_soc_runtime_deactivate(fe, stream);
309 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
311 ret = dpcm_be_dai_hw_free(fe, stream);
313 dev_err(fe->dev, "Compressed ASoC: hw_free failed: %d\n", ret);
315 ret = dpcm_be_dai_shutdown(fe, stream);
317 /* mark FE's links ready to prune */
318 for_each_dpcm_be(fe, stream, dpcm)
319 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
321 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
323 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
324 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
326 dpcm_be_disconnect(fe, stream);
328 fe->dpcm[stream].runtime = NULL;
330 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->shutdown)
331 fe->dai_link->compr_ops->shutdown(cstream);
333 soc_compr_components_free(cstream, NULL);
335 if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
336 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
338 mutex_unlock(&fe->card->mutex);
342 static int soc_compr_components_trigger(struct snd_compr_stream *cstream,
345 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
346 struct snd_soc_component *component;
347 struct snd_soc_rtdcom_list *rtdcom;
350 for_each_rtd_components(rtd, rtdcom, component) {
351 if (!component->driver->compr_ops ||
352 !component->driver->compr_ops->trigger)
355 ret = component->driver->compr_ops->trigger(cstream, cmd);
363 static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd)
365 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
366 struct snd_soc_dai *codec_dai = rtd->codec_dai;
367 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
370 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
372 ret = soc_compr_components_trigger(cstream, cmd);
376 if (cpu_dai->driver->cops && cpu_dai->driver->cops->trigger)
377 cpu_dai->driver->cops->trigger(cstream, cmd, cpu_dai);
380 case SNDRV_PCM_TRIGGER_START:
381 snd_soc_dai_digital_mute(codec_dai, 0, cstream->direction);
383 case SNDRV_PCM_TRIGGER_STOP:
384 snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
389 mutex_unlock(&rtd->card->pcm_mutex);
393 static int soc_compr_trigger_fe(struct snd_compr_stream *cstream, int cmd)
395 struct snd_soc_pcm_runtime *fe = cstream->private_data;
396 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
399 if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN ||
400 cmd == SND_COMPR_TRIGGER_DRAIN)
401 return soc_compr_components_trigger(cstream, cmd);
403 if (cstream->direction == SND_COMPRESS_PLAYBACK)
404 stream = SNDRV_PCM_STREAM_PLAYBACK;
406 stream = SNDRV_PCM_STREAM_CAPTURE;
408 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
410 if (cpu_dai->driver->cops && cpu_dai->driver->cops->trigger) {
411 ret = cpu_dai->driver->cops->trigger(cstream, cmd, cpu_dai);
416 ret = soc_compr_components_trigger(cstream, cmd);
420 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
422 ret = dpcm_be_dai_trigger(fe, stream, cmd);
425 case SNDRV_PCM_TRIGGER_START:
426 case SNDRV_PCM_TRIGGER_RESUME:
427 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
428 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
430 case SNDRV_PCM_TRIGGER_STOP:
431 case SNDRV_PCM_TRIGGER_SUSPEND:
432 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
434 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
435 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
440 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
441 mutex_unlock(&fe->card->mutex);
445 static int soc_compr_components_set_params(struct snd_compr_stream *cstream,
446 struct snd_compr_params *params)
448 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
449 struct snd_soc_component *component;
450 struct snd_soc_rtdcom_list *rtdcom;
453 for_each_rtd_components(rtd, rtdcom, component) {
454 if (!component->driver->compr_ops ||
455 !component->driver->compr_ops->set_params)
458 ret = component->driver->compr_ops->set_params(cstream, params);
466 static int soc_compr_set_params(struct snd_compr_stream *cstream,
467 struct snd_compr_params *params)
469 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
470 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
473 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
476 * First we call set_params for the CPU DAI, then the component
477 * driver this should configure the SoC side. If the machine has
478 * compressed ops then we call that as well. The expectation is
479 * that these callbacks will configure everything for this compress
480 * path, like configuring a PCM port for a CODEC.
482 if (cpu_dai->driver->cops && cpu_dai->driver->cops->set_params) {
483 ret = cpu_dai->driver->cops->set_params(cstream, params, cpu_dai);
488 ret = soc_compr_components_set_params(cstream, params);
492 if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->set_params) {
493 ret = rtd->dai_link->compr_ops->set_params(cstream);
498 if (cstream->direction == SND_COMPRESS_PLAYBACK)
499 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
500 SND_SOC_DAPM_STREAM_START);
502 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
503 SND_SOC_DAPM_STREAM_START);
505 /* cancel any delayed stream shutdown that is pending */
507 mutex_unlock(&rtd->card->pcm_mutex);
509 cancel_delayed_work_sync(&rtd->delayed_work);
514 mutex_unlock(&rtd->card->pcm_mutex);
518 static int soc_compr_set_params_fe(struct snd_compr_stream *cstream,
519 struct snd_compr_params *params)
521 struct snd_soc_pcm_runtime *fe = cstream->private_data;
522 struct snd_pcm_substream *fe_substream =
523 fe->pcm->streams[cstream->direction].substream;
524 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
527 if (cstream->direction == SND_COMPRESS_PLAYBACK)
528 stream = SNDRV_PCM_STREAM_PLAYBACK;
530 stream = SNDRV_PCM_STREAM_CAPTURE;
532 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
535 * Create an empty hw_params for the BE as the machine driver must
536 * fix this up to match DSP decoder and ASRC configuration.
537 * I.e. machine driver fixup for compressed BE is mandatory.
539 memset(&fe->dpcm[fe_substream->stream].hw_params, 0,
540 sizeof(struct snd_pcm_hw_params));
542 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
544 ret = dpcm_be_dai_hw_params(fe, stream);
548 ret = dpcm_be_dai_prepare(fe, stream);
552 if (cpu_dai->driver->cops && cpu_dai->driver->cops->set_params) {
553 ret = cpu_dai->driver->cops->set_params(cstream, params, cpu_dai);
558 ret = soc_compr_components_set_params(cstream, params);
562 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->set_params) {
563 ret = fe->dai_link->compr_ops->set_params(cstream);
568 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
569 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
572 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
573 mutex_unlock(&fe->card->mutex);
577 static int soc_compr_get_params(struct snd_compr_stream *cstream,
578 struct snd_codec *params)
580 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
581 struct snd_soc_component *component;
582 struct snd_soc_rtdcom_list *rtdcom;
583 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
586 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
588 if (cpu_dai->driver->cops && cpu_dai->driver->cops->get_params) {
589 ret = cpu_dai->driver->cops->get_params(cstream, params, cpu_dai);
594 for_each_rtd_components(rtd, rtdcom, component) {
595 if (!component->driver->compr_ops ||
596 !component->driver->compr_ops->get_params)
599 ret = component->driver->compr_ops->get_params(cstream, params);
604 mutex_unlock(&rtd->card->pcm_mutex);
608 static int soc_compr_get_caps(struct snd_compr_stream *cstream,
609 struct snd_compr_caps *caps)
611 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
612 struct snd_soc_component *component;
613 struct snd_soc_rtdcom_list *rtdcom;
616 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
618 for_each_rtd_components(rtd, rtdcom, component) {
619 if (!component->driver->compr_ops ||
620 !component->driver->compr_ops->get_caps)
623 ret = component->driver->compr_ops->get_caps(cstream, caps);
627 mutex_unlock(&rtd->card->pcm_mutex);
631 static int soc_compr_get_codec_caps(struct snd_compr_stream *cstream,
632 struct snd_compr_codec_caps *codec)
634 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
635 struct snd_soc_component *component;
636 struct snd_soc_rtdcom_list *rtdcom;
639 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
641 for_each_rtd_components(rtd, rtdcom, component) {
642 if (!component->driver->compr_ops ||
643 !component->driver->compr_ops->get_codec_caps)
646 ret = component->driver->compr_ops->get_codec_caps(cstream,
651 mutex_unlock(&rtd->card->pcm_mutex);
655 static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
657 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
658 struct snd_soc_component *component;
659 struct snd_soc_rtdcom_list *rtdcom;
660 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
663 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
665 if (cpu_dai->driver->cops && cpu_dai->driver->cops->ack) {
666 ret = cpu_dai->driver->cops->ack(cstream, bytes, cpu_dai);
671 for_each_rtd_components(rtd, rtdcom, component) {
672 if (!component->driver->compr_ops ||
673 !component->driver->compr_ops->ack)
676 ret = component->driver->compr_ops->ack(cstream, bytes);
682 mutex_unlock(&rtd->card->pcm_mutex);
686 static int soc_compr_pointer(struct snd_compr_stream *cstream,
687 struct snd_compr_tstamp *tstamp)
689 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
690 struct snd_soc_component *component;
691 struct snd_soc_rtdcom_list *rtdcom;
693 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
695 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
697 if (cpu_dai->driver->cops && cpu_dai->driver->cops->pointer)
698 cpu_dai->driver->cops->pointer(cstream, tstamp, cpu_dai);
700 for_each_rtd_components(rtd, rtdcom, component) {
701 if (!component->driver->compr_ops ||
702 !component->driver->compr_ops->pointer)
705 ret = component->driver->compr_ops->pointer(cstream, tstamp);
709 mutex_unlock(&rtd->card->pcm_mutex);
713 static int soc_compr_copy(struct snd_compr_stream *cstream,
714 char __user *buf, size_t count)
716 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
717 struct snd_soc_component *component;
718 struct snd_soc_rtdcom_list *rtdcom;
721 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
723 for_each_rtd_components(rtd, rtdcom, component) {
724 if (!component->driver->compr_ops ||
725 !component->driver->compr_ops->copy)
728 ret = component->driver->compr_ops->copy(cstream, buf, count);
732 mutex_unlock(&rtd->card->pcm_mutex);
736 static int soc_compr_set_metadata(struct snd_compr_stream *cstream,
737 struct snd_compr_metadata *metadata)
739 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
740 struct snd_soc_component *component;
741 struct snd_soc_rtdcom_list *rtdcom;
742 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
745 if (cpu_dai->driver->cops && cpu_dai->driver->cops->set_metadata) {
746 ret = cpu_dai->driver->cops->set_metadata(cstream, metadata, cpu_dai);
751 for_each_rtd_components(rtd, rtdcom, component) {
752 if (!component->driver->compr_ops ||
753 !component->driver->compr_ops->set_metadata)
756 ret = component->driver->compr_ops->set_metadata(cstream,
765 static int soc_compr_get_metadata(struct snd_compr_stream *cstream,
766 struct snd_compr_metadata *metadata)
768 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
769 struct snd_soc_component *component;
770 struct snd_soc_rtdcom_list *rtdcom;
771 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
774 if (cpu_dai->driver->cops && cpu_dai->driver->cops->get_metadata) {
775 ret = cpu_dai->driver->cops->get_metadata(cstream, metadata, cpu_dai);
780 for_each_rtd_components(rtd, rtdcom, component) {
781 if (!component->driver->compr_ops ||
782 !component->driver->compr_ops->get_metadata)
785 return component->driver->compr_ops->get_metadata(cstream,
792 /* ASoC Compress operations */
793 static struct snd_compr_ops soc_compr_ops = {
794 .open = soc_compr_open,
795 .free = soc_compr_free,
796 .set_params = soc_compr_set_params,
797 .set_metadata = soc_compr_set_metadata,
798 .get_metadata = soc_compr_get_metadata,
799 .get_params = soc_compr_get_params,
800 .trigger = soc_compr_trigger,
801 .pointer = soc_compr_pointer,
802 .ack = soc_compr_ack,
803 .get_caps = soc_compr_get_caps,
804 .get_codec_caps = soc_compr_get_codec_caps
807 /* ASoC Dynamic Compress operations */
808 static struct snd_compr_ops soc_compr_dyn_ops = {
809 .open = soc_compr_open_fe,
810 .free = soc_compr_free_fe,
811 .set_params = soc_compr_set_params_fe,
812 .get_params = soc_compr_get_params,
813 .set_metadata = soc_compr_set_metadata,
814 .get_metadata = soc_compr_get_metadata,
815 .trigger = soc_compr_trigger_fe,
816 .pointer = soc_compr_pointer,
817 .ack = soc_compr_ack,
818 .get_caps = soc_compr_get_caps,
819 .get_codec_caps = soc_compr_get_codec_caps
823 * snd_soc_new_compress - create a new compress.
825 * @rtd: The runtime for which we will create compress
826 * @num: the device index number (zero based - shared with normal PCMs)
828 * Return: 0 for success, else error.
830 int snd_soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
832 struct snd_soc_component *component;
833 struct snd_soc_rtdcom_list *rtdcom;
834 struct snd_soc_dai *codec_dai = rtd->codec_dai;
835 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
836 struct snd_compr *compr;
837 struct snd_pcm *be_pcm;
839 int ret = 0, direction = 0;
840 int playback = 0, capture = 0;
842 if (rtd->num_codecs > 1) {
843 dev_err(rtd->card->dev,
844 "Compress ASoC: Multicodec not supported\n");
848 /* check client and interface hw capabilities */
849 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
850 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
852 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
853 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
857 * Compress devices are unidirectional so only one of the directions
858 * should be set, check for that (xor)
860 if (playback + capture != 1) {
861 dev_err(rtd->card->dev,
862 "Compress ASoC: Invalid direction for P %d, C %d\n",
868 direction = SND_COMPRESS_PLAYBACK;
870 direction = SND_COMPRESS_CAPTURE;
872 compr = devm_kzalloc(rtd->card->dev, sizeof(*compr), GFP_KERNEL);
876 compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
881 if (rtd->dai_link->dynamic) {
882 snprintf(new_name, sizeof(new_name), "(%s)",
883 rtd->dai_link->stream_name);
885 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
886 rtd->dai_link->dpcm_playback,
887 rtd->dai_link->dpcm_capture, &be_pcm);
889 dev_err(rtd->card->dev,
890 "Compress ASoC: can't create compressed for %s: %d\n",
891 rtd->dai_link->name, ret);
897 if (rtd->dai_link->dpcm_playback)
898 be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
899 else if (rtd->dai_link->dpcm_capture)
900 be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
901 memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
903 snprintf(new_name, sizeof(new_name), "%s %s-%d",
904 rtd->dai_link->stream_name, codec_dai->name, num);
906 memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
909 for_each_rtd_components(rtd, rtdcom, component) {
910 if (!component->driver->compr_ops ||
911 !component->driver->compr_ops->copy)
914 compr->ops->copy = soc_compr_copy;
918 mutex_init(&compr->lock);
919 ret = snd_compress_new(rtd->card->snd_card, num, direction,
922 component = rtd->codec_dai->component;
923 dev_err(component->dev,
924 "Compress ASoC: can't create compress for codec %s: %d\n",
925 component->name, ret);
929 /* DAPM dai link stream work */
930 rtd->close_delayed_work_func = close_delayed_work;
933 compr->private_data = rtd;
935 dev_info(rtd->card->dev, "Compress ASoC: %s <-> %s mapping ok\n",
936 codec_dai->name, cpu_dai->name);
940 EXPORT_SYMBOL_GPL(snd_soc_new_compress);