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_rtdcom(rtd, rtdcom) {
32 component = rtdcom->component;
34 if (!component->driver->compr_ops ||
35 !component->driver->compr_ops->open)
38 ret = component->driver->compr_ops->open(cstream);
40 dev_err(component->dev,
41 "Compress ASoC: can't open platform %s: %d\n",
42 component->name, ret);
53 static int soc_compr_components_free(struct snd_compr_stream *cstream,
54 struct snd_soc_component *last)
56 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
57 struct snd_soc_component *component;
58 struct snd_soc_rtdcom_list *rtdcom;
60 for_each_rtdcom(rtd, rtdcom) {
61 component = rtdcom->component;
63 if (component == last)
66 if (!component->driver->compr_ops ||
67 !component->driver->compr_ops->free)
70 component->driver->compr_ops->free(cstream);
76 static int soc_compr_open(struct snd_compr_stream *cstream)
78 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
79 struct snd_soc_component *component;
80 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
83 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
85 if (cpu_dai->driver->cops && cpu_dai->driver->cops->startup) {
86 ret = cpu_dai->driver->cops->startup(cstream, cpu_dai);
89 "Compress ASoC: can't open interface %s: %d\n",
95 ret = soc_compr_components_open(cstream, &component);
99 if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->startup) {
100 ret = rtd->dai_link->compr_ops->startup(cstream);
103 "Compress ASoC: %s startup failed: %d\n",
104 rtd->dai_link->name, ret);
109 snd_soc_runtime_activate(rtd, cstream->direction);
111 mutex_unlock(&rtd->pcm_mutex);
116 soc_compr_components_free(cstream, component);
118 if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
119 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
121 mutex_unlock(&rtd->pcm_mutex);
125 static int soc_compr_open_fe(struct snd_compr_stream *cstream)
127 struct snd_soc_pcm_runtime *fe = cstream->private_data;
128 struct snd_pcm_substream *fe_substream =
129 fe->pcm->streams[cstream->direction].substream;
130 struct snd_soc_component *component;
131 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
132 struct snd_soc_dpcm *dpcm;
133 struct snd_soc_dapm_widget_list *list;
137 if (cstream->direction == SND_COMPRESS_PLAYBACK)
138 stream = SNDRV_PCM_STREAM_PLAYBACK;
140 stream = SNDRV_PCM_STREAM_CAPTURE;
142 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
144 if (cpu_dai->driver->cops && cpu_dai->driver->cops->startup) {
145 ret = cpu_dai->driver->cops->startup(cstream, cpu_dai);
147 dev_err(cpu_dai->dev,
148 "Compress ASoC: can't open interface %s: %d\n",
154 ret = soc_compr_components_open(cstream, &component);
158 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->startup) {
159 ret = fe->dai_link->compr_ops->startup(cstream);
161 pr_err("Compress ASoC: %s startup failed: %d\n",
162 fe->dai_link->name, ret);
167 fe->dpcm[stream].runtime = fe_substream->runtime;
169 ret = dpcm_path_get(fe, stream, &list);
173 dev_dbg(fe->dev, "Compress ASoC: %s no valid %s route\n",
174 fe->dai_link->name, stream ? "capture" : "playback");
176 /* calculate valid and active FE <-> BE dpcms */
177 dpcm_process_paths(fe, stream, &list, 1);
179 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
181 ret = dpcm_be_dai_startup(fe, stream);
183 /* clean up all links */
184 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
185 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
187 dpcm_be_disconnect(fe, stream);
188 fe->dpcm[stream].runtime = NULL;
192 dpcm_clear_pending_state(fe, stream);
193 dpcm_path_put(&list);
195 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
196 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
198 snd_soc_runtime_activate(fe, stream);
200 mutex_unlock(&fe->card->mutex);
205 dpcm_path_put(&list);
207 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->shutdown)
208 fe->dai_link->compr_ops->shutdown(cstream);
210 soc_compr_components_free(cstream, component);
212 if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
213 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
215 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
216 mutex_unlock(&fe->card->mutex);
221 * Power down the audio subsystem pmdown_time msecs after close is called.
222 * This is to ensure there are no pops or clicks in between any music tracks
223 * due to DAPM power cycling.
225 static void close_delayed_work(struct work_struct *work)
227 struct snd_soc_pcm_runtime *rtd =
228 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
229 struct snd_soc_dai *codec_dai = rtd->codec_dai;
231 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
234 "Compress ASoC: pop wq checking: %s status: %s waiting: %s\n",
235 codec_dai->driver->playback.stream_name,
236 codec_dai->playback_active ? "active" : "inactive",
237 rtd->pop_wait ? "yes" : "no");
239 /* are we waiting on this codec DAI stream */
240 if (rtd->pop_wait == 1) {
242 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
243 SND_SOC_DAPM_STREAM_STOP);
246 mutex_unlock(&rtd->pcm_mutex);
249 static int soc_compr_free(struct snd_compr_stream *cstream)
251 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
252 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
253 struct snd_soc_dai *codec_dai = rtd->codec_dai;
256 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
258 if (cstream->direction == SND_COMPRESS_PLAYBACK)
259 stream = SNDRV_PCM_STREAM_PLAYBACK;
261 stream = SNDRV_PCM_STREAM_CAPTURE;
263 snd_soc_runtime_deactivate(rtd, stream);
265 snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
267 if (!cpu_dai->active)
270 if (!codec_dai->active)
273 if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->shutdown)
274 rtd->dai_link->compr_ops->shutdown(cstream);
276 soc_compr_components_free(cstream, NULL);
278 if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
279 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
281 if (cstream->direction == SND_COMPRESS_PLAYBACK) {
282 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
283 snd_soc_dapm_stream_event(rtd,
284 SNDRV_PCM_STREAM_PLAYBACK,
285 SND_SOC_DAPM_STREAM_STOP);
288 queue_delayed_work(system_power_efficient_wq,
290 msecs_to_jiffies(rtd->pmdown_time));
293 /* capture streams can be powered down now */
294 snd_soc_dapm_stream_event(rtd,
295 SNDRV_PCM_STREAM_CAPTURE,
296 SND_SOC_DAPM_STREAM_STOP);
299 mutex_unlock(&rtd->pcm_mutex);
303 static int soc_compr_free_fe(struct snd_compr_stream *cstream)
305 struct snd_soc_pcm_runtime *fe = cstream->private_data;
306 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
307 struct snd_soc_dpcm *dpcm;
310 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
312 if (cstream->direction == SND_COMPRESS_PLAYBACK)
313 stream = SNDRV_PCM_STREAM_PLAYBACK;
315 stream = SNDRV_PCM_STREAM_CAPTURE;
317 snd_soc_runtime_deactivate(fe, stream);
319 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
321 ret = dpcm_be_dai_hw_free(fe, stream);
323 dev_err(fe->dev, "Compressed ASoC: hw_free failed: %d\n", ret);
325 ret = dpcm_be_dai_shutdown(fe, stream);
327 /* mark FE's links ready to prune */
328 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
329 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
331 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
333 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
334 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
336 dpcm_be_disconnect(fe, stream);
338 fe->dpcm[stream].runtime = NULL;
340 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->shutdown)
341 fe->dai_link->compr_ops->shutdown(cstream);
343 soc_compr_components_free(cstream, NULL);
345 if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
346 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
348 mutex_unlock(&fe->card->mutex);
352 static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd)
355 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
356 struct snd_soc_component *component;
357 struct snd_soc_rtdcom_list *rtdcom;
358 struct snd_soc_dai *codec_dai = rtd->codec_dai;
359 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
362 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
364 for_each_rtdcom(rtd, rtdcom) {
365 component = rtdcom->component;
367 if (!component->driver->compr_ops ||
368 !component->driver->compr_ops->trigger)
371 __ret = component->driver->compr_ops->trigger(cstream, cmd);
378 if (cpu_dai->driver->cops && cpu_dai->driver->cops->trigger)
379 cpu_dai->driver->cops->trigger(cstream, cmd, cpu_dai);
382 case SNDRV_PCM_TRIGGER_START:
383 snd_soc_dai_digital_mute(codec_dai, 0, cstream->direction);
385 case SNDRV_PCM_TRIGGER_STOP:
386 snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
391 mutex_unlock(&rtd->pcm_mutex);
395 static int soc_compr_trigger_fe(struct snd_compr_stream *cstream, int cmd)
397 struct snd_soc_pcm_runtime *fe = cstream->private_data;
398 struct snd_soc_component *component;
399 struct snd_soc_rtdcom_list *rtdcom;
400 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
401 int ret = 0, __ret, stream;
403 if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN ||
404 cmd == SND_COMPR_TRIGGER_DRAIN) {
406 for_each_rtdcom(fe, rtdcom) {
407 component = rtdcom->component;
409 if (!component->driver->compr_ops ||
410 !component->driver->compr_ops->trigger)
413 __ret = component->driver->compr_ops->trigger(cstream, cmd);
420 if (cstream->direction == SND_COMPRESS_PLAYBACK)
421 stream = SNDRV_PCM_STREAM_PLAYBACK;
423 stream = SNDRV_PCM_STREAM_CAPTURE;
425 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
427 if (cpu_dai->driver->cops && cpu_dai->driver->cops->trigger) {
428 ret = cpu_dai->driver->cops->trigger(cstream, cmd, cpu_dai);
433 for_each_rtdcom(fe, rtdcom) {
434 component = rtdcom->component;
436 if (!component->driver->compr_ops ||
437 !component->driver->compr_ops->trigger)
440 __ret = component->driver->compr_ops->trigger(cstream, cmd);
447 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
449 ret = dpcm_be_dai_trigger(fe, stream, cmd);
452 case SNDRV_PCM_TRIGGER_START:
453 case SNDRV_PCM_TRIGGER_RESUME:
454 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
455 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
457 case SNDRV_PCM_TRIGGER_STOP:
458 case SNDRV_PCM_TRIGGER_SUSPEND:
459 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
461 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
462 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
467 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
468 mutex_unlock(&fe->card->mutex);
472 static int soc_compr_set_params(struct snd_compr_stream *cstream,
473 struct snd_compr_params *params)
475 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
476 struct snd_soc_component *component;
477 struct snd_soc_rtdcom_list *rtdcom;
478 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
481 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
484 * First we call set_params for the CPU DAI, then the component
485 * driver this should configure the SoC side. If the machine has
486 * compressed ops then we call that as well. The expectation is
487 * that these callbacks will configure everything for this compress
488 * path, like configuring a PCM port for a CODEC.
490 if (cpu_dai->driver->cops && cpu_dai->driver->cops->set_params) {
491 ret = cpu_dai->driver->cops->set_params(cstream, params, cpu_dai);
496 for_each_rtdcom(rtd, rtdcom) {
497 component = rtdcom->component;
499 if (!component->driver->compr_ops ||
500 !component->driver->compr_ops->set_params)
503 __ret = component->driver->compr_ops->set_params(cstream, params);
510 if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->set_params) {
511 ret = rtd->dai_link->compr_ops->set_params(cstream);
516 if (cstream->direction == SND_COMPRESS_PLAYBACK)
517 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
518 SND_SOC_DAPM_STREAM_START);
520 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
521 SND_SOC_DAPM_STREAM_START);
523 /* cancel any delayed stream shutdown that is pending */
525 mutex_unlock(&rtd->pcm_mutex);
527 cancel_delayed_work_sync(&rtd->delayed_work);
532 mutex_unlock(&rtd->pcm_mutex);
536 static int soc_compr_set_params_fe(struct snd_compr_stream *cstream,
537 struct snd_compr_params *params)
539 struct snd_soc_pcm_runtime *fe = cstream->private_data;
540 struct snd_pcm_substream *fe_substream =
541 fe->pcm->streams[cstream->direction].substream;
542 struct snd_soc_component *component;
543 struct snd_soc_rtdcom_list *rtdcom;
544 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
545 int ret = 0, __ret, stream;
547 if (cstream->direction == SND_COMPRESS_PLAYBACK)
548 stream = SNDRV_PCM_STREAM_PLAYBACK;
550 stream = SNDRV_PCM_STREAM_CAPTURE;
552 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
554 if (cpu_dai->driver->cops && cpu_dai->driver->cops->set_params) {
555 ret = cpu_dai->driver->cops->set_params(cstream, params, cpu_dai);
560 for_each_rtdcom(fe, rtdcom) {
561 component = rtdcom->component;
563 if (!component->driver->compr_ops ||
564 !component->driver->compr_ops->set_params)
567 __ret = component->driver->compr_ops->set_params(cstream, params);
574 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->set_params) {
575 ret = fe->dai_link->compr_ops->set_params(cstream);
581 * Create an empty hw_params for the BE as the machine driver must
582 * fix this up to match DSP decoder and ASRC configuration.
583 * I.e. machine driver fixup for compressed BE is mandatory.
585 memset(&fe->dpcm[fe_substream->stream].hw_params, 0,
586 sizeof(struct snd_pcm_hw_params));
588 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
590 ret = dpcm_be_dai_hw_params(fe, stream);
594 ret = dpcm_be_dai_prepare(fe, stream);
598 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
599 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
602 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
603 mutex_unlock(&fe->card->mutex);
607 static int soc_compr_get_params(struct snd_compr_stream *cstream,
608 struct snd_codec *params)
610 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
611 struct snd_soc_component *component;
612 struct snd_soc_rtdcom_list *rtdcom;
613 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
616 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
618 if (cpu_dai->driver->cops && cpu_dai->driver->cops->get_params) {
619 ret = cpu_dai->driver->cops->get_params(cstream, params, cpu_dai);
624 for_each_rtdcom(rtd, rtdcom) {
625 component = rtdcom->component;
627 if (!component->driver->compr_ops ||
628 !component->driver->compr_ops->get_params)
631 __ret = component->driver->compr_ops->get_params(cstream, params);
637 mutex_unlock(&rtd->pcm_mutex);
641 static int soc_compr_get_caps(struct snd_compr_stream *cstream,
642 struct snd_compr_caps *caps)
644 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
645 struct snd_soc_component *component;
646 struct snd_soc_rtdcom_list *rtdcom;
649 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
651 for_each_rtdcom(rtd, rtdcom) {
652 component = rtdcom->component;
654 if (!component->driver->compr_ops ||
655 !component->driver->compr_ops->get_caps)
658 __ret = component->driver->compr_ops->get_caps(cstream, caps);
663 mutex_unlock(&rtd->pcm_mutex);
667 static int soc_compr_get_codec_caps(struct snd_compr_stream *cstream,
668 struct snd_compr_codec_caps *codec)
670 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
671 struct snd_soc_component *component;
672 struct snd_soc_rtdcom_list *rtdcom;
675 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
677 for_each_rtdcom(rtd, rtdcom) {
678 component = rtdcom->component;
680 if (!component->driver->compr_ops ||
681 !component->driver->compr_ops->get_codec_caps)
684 __ret = component->driver->compr_ops->get_codec_caps(cstream, codec);
689 mutex_unlock(&rtd->pcm_mutex);
693 static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
695 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
696 struct snd_soc_component *component;
697 struct snd_soc_rtdcom_list *rtdcom;
698 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
701 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
703 if (cpu_dai->driver->cops && cpu_dai->driver->cops->ack) {
704 ret = cpu_dai->driver->cops->ack(cstream, bytes, cpu_dai);
709 for_each_rtdcom(rtd, rtdcom) {
710 component = rtdcom->component;
712 if (!component->driver->compr_ops ||
713 !component->driver->compr_ops->ack)
716 __ret = component->driver->compr_ops->ack(cstream, bytes);
722 mutex_unlock(&rtd->pcm_mutex);
726 static int soc_compr_pointer(struct snd_compr_stream *cstream,
727 struct snd_compr_tstamp *tstamp)
729 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
730 struct snd_soc_component *component;
731 struct snd_soc_rtdcom_list *rtdcom;
733 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
735 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
737 if (cpu_dai->driver->cops && cpu_dai->driver->cops->pointer)
738 cpu_dai->driver->cops->pointer(cstream, tstamp, cpu_dai);
740 for_each_rtdcom(rtd, rtdcom) {
741 component = rtdcom->component;
743 if (!component->driver->compr_ops ||
744 !component->driver->compr_ops->pointer)
747 __ret = component->driver->compr_ops->pointer(cstream, tstamp);
752 mutex_unlock(&rtd->pcm_mutex);
756 static int soc_compr_copy(struct snd_compr_stream *cstream,
757 char __user *buf, size_t count)
759 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
760 struct snd_soc_component *component;
761 struct snd_soc_rtdcom_list *rtdcom;
764 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
766 for_each_rtdcom(rtd, rtdcom) {
767 component = rtdcom->component;
769 if (!component->driver->compr_ops ||
770 !component->driver->compr_ops->copy)
773 ret = component->driver->compr_ops->copy(cstream, buf, count);
777 mutex_unlock(&rtd->pcm_mutex);
781 static int soc_compr_set_metadata(struct snd_compr_stream *cstream,
782 struct snd_compr_metadata *metadata)
784 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
785 struct snd_soc_component *component;
786 struct snd_soc_rtdcom_list *rtdcom;
787 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
790 if (cpu_dai->driver->cops && cpu_dai->driver->cops->set_metadata) {
791 ret = cpu_dai->driver->cops->set_metadata(cstream, metadata, cpu_dai);
796 for_each_rtdcom(rtd, rtdcom) {
797 component = rtdcom->component;
799 if (!component->driver->compr_ops ||
800 !component->driver->compr_ops->set_metadata)
803 __ret = component->driver->compr_ops->set_metadata(cstream, metadata);
811 static int soc_compr_get_metadata(struct snd_compr_stream *cstream,
812 struct snd_compr_metadata *metadata)
814 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
815 struct snd_soc_component *component;
816 struct snd_soc_rtdcom_list *rtdcom;
817 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
820 if (cpu_dai->driver->cops && cpu_dai->driver->cops->get_metadata) {
821 ret = cpu_dai->driver->cops->get_metadata(cstream, metadata, cpu_dai);
826 for_each_rtdcom(rtd, rtdcom) {
827 component = rtdcom->component;
829 if (!component->driver->compr_ops ||
830 !component->driver->compr_ops->get_metadata)
833 __ret = component->driver->compr_ops->get_metadata(cstream, metadata);
841 /* ASoC Compress operations */
842 static struct snd_compr_ops soc_compr_ops = {
843 .open = soc_compr_open,
844 .free = soc_compr_free,
845 .set_params = soc_compr_set_params,
846 .set_metadata = soc_compr_set_metadata,
847 .get_metadata = soc_compr_get_metadata,
848 .get_params = soc_compr_get_params,
849 .trigger = soc_compr_trigger,
850 .pointer = soc_compr_pointer,
851 .ack = soc_compr_ack,
852 .get_caps = soc_compr_get_caps,
853 .get_codec_caps = soc_compr_get_codec_caps
856 /* ASoC Dynamic Compress operations */
857 static struct snd_compr_ops soc_compr_dyn_ops = {
858 .open = soc_compr_open_fe,
859 .free = soc_compr_free_fe,
860 .set_params = soc_compr_set_params_fe,
861 .get_params = soc_compr_get_params,
862 .set_metadata = soc_compr_set_metadata,
863 .get_metadata = soc_compr_get_metadata,
864 .trigger = soc_compr_trigger_fe,
865 .pointer = soc_compr_pointer,
866 .ack = soc_compr_ack,
867 .get_caps = soc_compr_get_caps,
868 .get_codec_caps = soc_compr_get_codec_caps
872 * snd_soc_new_compress - create a new compress.
874 * @rtd: The runtime for which we will create compress
875 * @num: the device index number (zero based - shared with normal PCMs)
877 * Return: 0 for success, else error.
879 int snd_soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
881 struct snd_soc_component *component;
882 struct snd_soc_rtdcom_list *rtdcom;
883 struct snd_soc_dai *codec_dai = rtd->codec_dai;
884 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
885 struct snd_compr *compr;
886 struct snd_pcm *be_pcm;
888 int ret = 0, direction = 0;
889 int playback = 0, capture = 0;
891 if (rtd->num_codecs > 1) {
892 dev_err(rtd->card->dev,
893 "Compress ASoC: Multicodec not supported\n");
897 /* check client and interface hw capabilities */
898 if (codec_dai->driver->playback.channels_min)
900 if (codec_dai->driver->capture.channels_min)
903 capture = capture && cpu_dai->driver->capture.channels_min;
904 playback = playback && cpu_dai->driver->playback.channels_min;
907 * Compress devices are unidirectional so only one of the directions
908 * should be set, check for that (xor)
910 if (playback + capture != 1) {
911 dev_err(rtd->card->dev,
912 "Compress ASoC: Invalid direction for P %d, C %d\n",
918 direction = SND_COMPRESS_PLAYBACK;
920 direction = SND_COMPRESS_CAPTURE;
922 compr = kzalloc(sizeof(*compr), GFP_KERNEL);
926 compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
933 if (rtd->dai_link->dynamic) {
934 snprintf(new_name, sizeof(new_name), "(%s)",
935 rtd->dai_link->stream_name);
937 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
938 rtd->dai_link->dpcm_playback,
939 rtd->dai_link->dpcm_capture, &be_pcm);
941 dev_err(rtd->card->dev,
942 "Compress ASoC: can't create compressed for %s: %d\n",
943 rtd->dai_link->name, ret);
949 if (rtd->dai_link->dpcm_playback)
950 be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
951 else if (rtd->dai_link->dpcm_capture)
952 be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
953 memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
955 snprintf(new_name, sizeof(new_name), "%s %s-%d",
956 rtd->dai_link->stream_name, codec_dai->name, num);
958 memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
961 for_each_rtdcom(rtd, rtdcom) {
962 component = rtdcom->component;
964 if (!component->driver->compr_ops ||
965 !component->driver->compr_ops->copy)
968 compr->ops->copy = soc_compr_copy;
972 mutex_init(&compr->lock);
973 ret = snd_compress_new(rtd->card->snd_card, num, direction,
976 component = rtd->codec_dai->component;
977 dev_err(component->dev,
978 "Compress ASoC: can't create compress for codec %s: %d\n",
979 component->name, ret);
983 /* DAPM dai link stream work */
984 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
987 compr->private_data = rtd;
989 dev_info(rtd->card->dev, "Compress ASoC: %s <-> %s mapping ok\n",
990 codec_dai->name, cpu_dai->name);
997 EXPORT_SYMBOL_GPL(snd_soc_new_compress);