2 * soc-compress.c -- ALSA SoC Compress
4 * Copyright (C) 2012 Intel Corp.
6 * Authors: Namarta Kohli <namartax.kohli@intel.com>
7 * Ramesh Babu K V <ramesh.babu@linux.intel.com>
8 * Vinod Koul <vinod.koul@linux.intel.com>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/delay.h>
20 #include <linux/slab.h>
21 #include <linux/workqueue.h>
22 #include <sound/core.h>
23 #include <sound/compress_params.h>
24 #include <sound/compress_driver.h>
25 #include <sound/soc.h>
26 #include <sound/initval.h>
27 #include <sound/soc-dpcm.h>
29 static int soc_compr_components_open(struct snd_compr_stream *cstream,
30 struct snd_soc_component **last)
32 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
33 struct snd_soc_component *component;
34 struct snd_soc_rtdcom_list *rtdcom;
37 for_each_rtdcom(rtd, rtdcom) {
38 component = rtdcom->component;
40 if (!component->driver->compr_ops ||
41 !component->driver->compr_ops->open)
44 ret = component->driver->compr_ops->open(cstream);
46 dev_err(component->dev,
47 "Compress ASoC: can't open platform %s: %d\n",
48 component->name, ret);
59 static int soc_compr_components_free(struct snd_compr_stream *cstream,
60 struct snd_soc_component *last)
62 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
63 struct snd_soc_component *component;
64 struct snd_soc_rtdcom_list *rtdcom;
66 for_each_rtdcom(rtd, rtdcom) {
67 component = rtdcom->component;
69 if (component == last)
72 if (!component->driver->compr_ops ||
73 !component->driver->compr_ops->free)
76 component->driver->compr_ops->free(cstream);
82 static int soc_compr_open(struct snd_compr_stream *cstream)
84 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
85 struct snd_soc_component *component;
86 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
89 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
91 if (cpu_dai->driver->cops && cpu_dai->driver->cops->startup) {
92 ret = cpu_dai->driver->cops->startup(cstream, cpu_dai);
95 "Compress ASoC: can't open interface %s: %d\n",
101 ret = soc_compr_components_open(cstream, &component);
105 if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->startup) {
106 ret = rtd->dai_link->compr_ops->startup(cstream);
109 "Compress ASoC: %s startup failed: %d\n",
110 rtd->dai_link->name, ret);
115 snd_soc_runtime_activate(rtd, cstream->direction);
117 mutex_unlock(&rtd->pcm_mutex);
122 soc_compr_components_free(cstream, component);
124 if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
125 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
127 mutex_unlock(&rtd->pcm_mutex);
131 static int soc_compr_open_fe(struct snd_compr_stream *cstream)
133 struct snd_soc_pcm_runtime *fe = cstream->private_data;
134 struct snd_pcm_substream *fe_substream =
135 fe->pcm->streams[cstream->direction].substream;
136 struct snd_soc_component *component;
137 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
138 struct snd_soc_dpcm *dpcm;
139 struct snd_soc_dapm_widget_list *list;
143 if (cstream->direction == SND_COMPRESS_PLAYBACK)
144 stream = SNDRV_PCM_STREAM_PLAYBACK;
146 stream = SNDRV_PCM_STREAM_CAPTURE;
148 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
150 if (cpu_dai->driver->cops && cpu_dai->driver->cops->startup) {
151 ret = cpu_dai->driver->cops->startup(cstream, cpu_dai);
153 dev_err(cpu_dai->dev,
154 "Compress ASoC: can't open interface %s: %d\n",
160 ret = soc_compr_components_open(cstream, &component);
164 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->startup) {
165 ret = fe->dai_link->compr_ops->startup(cstream);
167 pr_err("Compress ASoC: %s startup failed: %d\n",
168 fe->dai_link->name, ret);
173 fe->dpcm[stream].runtime = fe_substream->runtime;
175 ret = dpcm_path_get(fe, stream, &list);
179 dev_dbg(fe->dev, "Compress ASoC: %s no valid %s route\n",
180 fe->dai_link->name, stream ? "capture" : "playback");
182 /* calculate valid and active FE <-> BE dpcms */
183 dpcm_process_paths(fe, stream, &list, 1);
185 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
187 ret = dpcm_be_dai_startup(fe, stream);
189 /* clean up all links */
190 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
191 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
193 dpcm_be_disconnect(fe, stream);
194 fe->dpcm[stream].runtime = NULL;
198 dpcm_clear_pending_state(fe, stream);
199 dpcm_path_put(&list);
201 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
202 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
204 snd_soc_runtime_activate(fe, stream);
206 mutex_unlock(&fe->card->mutex);
211 dpcm_path_put(&list);
213 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->shutdown)
214 fe->dai_link->compr_ops->shutdown(cstream);
216 soc_compr_components_free(cstream, component);
218 if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
219 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
221 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
222 mutex_unlock(&fe->card->mutex);
227 * Power down the audio subsystem pmdown_time msecs after close is called.
228 * This is to ensure there are no pops or clicks in between any music tracks
229 * due to DAPM power cycling.
231 static void close_delayed_work(struct work_struct *work)
233 struct snd_soc_pcm_runtime *rtd =
234 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
235 struct snd_soc_dai *codec_dai = rtd->codec_dai;
237 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
240 "Compress ASoC: pop wq checking: %s status: %s waiting: %s\n",
241 codec_dai->driver->playback.stream_name,
242 codec_dai->playback_active ? "active" : "inactive",
243 rtd->pop_wait ? "yes" : "no");
245 /* are we waiting on this codec DAI stream */
246 if (rtd->pop_wait == 1) {
248 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
249 SND_SOC_DAPM_STREAM_STOP);
252 mutex_unlock(&rtd->pcm_mutex);
255 static int soc_compr_free(struct snd_compr_stream *cstream)
257 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
258 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
259 struct snd_soc_dai *codec_dai = rtd->codec_dai;
262 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
264 if (cstream->direction == SND_COMPRESS_PLAYBACK)
265 stream = SNDRV_PCM_STREAM_PLAYBACK;
267 stream = SNDRV_PCM_STREAM_CAPTURE;
269 snd_soc_runtime_deactivate(rtd, stream);
271 snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
273 if (!cpu_dai->active)
276 if (!codec_dai->active)
279 if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->shutdown)
280 rtd->dai_link->compr_ops->shutdown(cstream);
282 soc_compr_components_free(cstream, NULL);
284 if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
285 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
287 if (cstream->direction == SND_COMPRESS_PLAYBACK) {
288 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
289 snd_soc_dapm_stream_event(rtd,
290 SNDRV_PCM_STREAM_PLAYBACK,
291 SND_SOC_DAPM_STREAM_STOP);
294 queue_delayed_work(system_power_efficient_wq,
296 msecs_to_jiffies(rtd->pmdown_time));
299 /* capture streams can be powered down now */
300 snd_soc_dapm_stream_event(rtd,
301 SNDRV_PCM_STREAM_CAPTURE,
302 SND_SOC_DAPM_STREAM_STOP);
305 mutex_unlock(&rtd->pcm_mutex);
309 static int soc_compr_free_fe(struct snd_compr_stream *cstream)
311 struct snd_soc_pcm_runtime *fe = cstream->private_data;
312 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
313 struct snd_soc_dpcm *dpcm;
316 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
318 if (cstream->direction == SND_COMPRESS_PLAYBACK)
319 stream = SNDRV_PCM_STREAM_PLAYBACK;
321 stream = SNDRV_PCM_STREAM_CAPTURE;
323 snd_soc_runtime_deactivate(fe, stream);
325 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
327 ret = dpcm_be_dai_hw_free(fe, stream);
329 dev_err(fe->dev, "Compressed ASoC: hw_free failed: %d\n", ret);
331 ret = dpcm_be_dai_shutdown(fe, stream);
333 /* mark FE's links ready to prune */
334 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
335 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
337 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
339 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
340 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
342 dpcm_be_disconnect(fe, stream);
344 fe->dpcm[stream].runtime = NULL;
346 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->shutdown)
347 fe->dai_link->compr_ops->shutdown(cstream);
349 soc_compr_components_free(cstream, NULL);
351 if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
352 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
354 mutex_unlock(&fe->card->mutex);
358 static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd)
361 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
362 struct snd_soc_component *component;
363 struct snd_soc_rtdcom_list *rtdcom;
364 struct snd_soc_dai *codec_dai = rtd->codec_dai;
365 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
368 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
370 for_each_rtdcom(rtd, rtdcom) {
371 component = rtdcom->component;
373 if (!component->driver->compr_ops ||
374 !component->driver->compr_ops->trigger)
377 __ret = component->driver->compr_ops->trigger(cstream, cmd);
384 if (cpu_dai->driver->cops && cpu_dai->driver->cops->trigger)
385 cpu_dai->driver->cops->trigger(cstream, cmd, cpu_dai);
388 case SNDRV_PCM_TRIGGER_START:
389 snd_soc_dai_digital_mute(codec_dai, 0, cstream->direction);
391 case SNDRV_PCM_TRIGGER_STOP:
392 snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
397 mutex_unlock(&rtd->pcm_mutex);
401 static int soc_compr_trigger_fe(struct snd_compr_stream *cstream, int cmd)
403 struct snd_soc_pcm_runtime *fe = cstream->private_data;
404 struct snd_soc_component *component;
405 struct snd_soc_rtdcom_list *rtdcom;
406 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
407 int ret = 0, __ret, stream;
409 if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN ||
410 cmd == SND_COMPR_TRIGGER_DRAIN) {
412 for_each_rtdcom(fe, rtdcom) {
413 component = rtdcom->component;
415 if (!component->driver->compr_ops ||
416 !component->driver->compr_ops->trigger)
419 __ret = component->driver->compr_ops->trigger(cstream, cmd);
426 if (cstream->direction == SND_COMPRESS_PLAYBACK)
427 stream = SNDRV_PCM_STREAM_PLAYBACK;
429 stream = SNDRV_PCM_STREAM_CAPTURE;
431 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
433 if (cpu_dai->driver->cops && cpu_dai->driver->cops->trigger) {
434 ret = cpu_dai->driver->cops->trigger(cstream, cmd, cpu_dai);
439 for_each_rtdcom(fe, rtdcom) {
440 component = rtdcom->component;
442 if (!component->driver->compr_ops ||
443 !component->driver->compr_ops->trigger)
446 __ret = component->driver->compr_ops->trigger(cstream, cmd);
453 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
455 ret = dpcm_be_dai_trigger(fe, stream, cmd);
458 case SNDRV_PCM_TRIGGER_START:
459 case SNDRV_PCM_TRIGGER_RESUME:
460 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
461 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
463 case SNDRV_PCM_TRIGGER_STOP:
464 case SNDRV_PCM_TRIGGER_SUSPEND:
465 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
467 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
468 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
473 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
474 mutex_unlock(&fe->card->mutex);
478 static int soc_compr_set_params(struct snd_compr_stream *cstream,
479 struct snd_compr_params *params)
481 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
482 struct snd_soc_component *component;
483 struct snd_soc_rtdcom_list *rtdcom;
484 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
487 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
490 * First we call set_params for the CPU DAI, then the component
491 * driver this should configure the SoC side. If the machine has
492 * compressed ops then we call that as well. The expectation is
493 * that these callbacks will configure everything for this compress
494 * path, like configuring a PCM port for a CODEC.
496 if (cpu_dai->driver->cops && cpu_dai->driver->cops->set_params) {
497 ret = cpu_dai->driver->cops->set_params(cstream, params, cpu_dai);
502 for_each_rtdcom(rtd, rtdcom) {
503 component = rtdcom->component;
505 if (!component->driver->compr_ops ||
506 !component->driver->compr_ops->set_params)
509 __ret = component->driver->compr_ops->set_params(cstream, params);
516 if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->set_params) {
517 ret = rtd->dai_link->compr_ops->set_params(cstream);
522 if (cstream->direction == SND_COMPRESS_PLAYBACK)
523 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
524 SND_SOC_DAPM_STREAM_START);
526 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
527 SND_SOC_DAPM_STREAM_START);
529 /* cancel any delayed stream shutdown that is pending */
531 mutex_unlock(&rtd->pcm_mutex);
533 cancel_delayed_work_sync(&rtd->delayed_work);
538 mutex_unlock(&rtd->pcm_mutex);
542 static int soc_compr_set_params_fe(struct snd_compr_stream *cstream,
543 struct snd_compr_params *params)
545 struct snd_soc_pcm_runtime *fe = cstream->private_data;
546 struct snd_pcm_substream *fe_substream =
547 fe->pcm->streams[cstream->direction].substream;
548 struct snd_soc_component *component;
549 struct snd_soc_rtdcom_list *rtdcom;
550 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
551 int ret = 0, __ret, stream;
553 if (cstream->direction == SND_COMPRESS_PLAYBACK)
554 stream = SNDRV_PCM_STREAM_PLAYBACK;
556 stream = SNDRV_PCM_STREAM_CAPTURE;
558 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
560 if (cpu_dai->driver->cops && cpu_dai->driver->cops->set_params) {
561 ret = cpu_dai->driver->cops->set_params(cstream, params, cpu_dai);
566 for_each_rtdcom(fe, rtdcom) {
567 component = rtdcom->component;
569 if (!component->driver->compr_ops ||
570 !component->driver->compr_ops->set_params)
573 __ret = component->driver->compr_ops->set_params(cstream, params);
580 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->set_params) {
581 ret = fe->dai_link->compr_ops->set_params(cstream);
587 * Create an empty hw_params for the BE as the machine driver must
588 * fix this up to match DSP decoder and ASRC configuration.
589 * I.e. machine driver fixup for compressed BE is mandatory.
591 memset(&fe->dpcm[fe_substream->stream].hw_params, 0,
592 sizeof(struct snd_pcm_hw_params));
594 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
596 ret = dpcm_be_dai_hw_params(fe, stream);
600 ret = dpcm_be_dai_prepare(fe, stream);
604 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
605 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
608 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
609 mutex_unlock(&fe->card->mutex);
613 static int soc_compr_get_params(struct snd_compr_stream *cstream,
614 struct snd_codec *params)
616 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
617 struct snd_soc_component *component;
618 struct snd_soc_rtdcom_list *rtdcom;
619 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
622 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
624 if (cpu_dai->driver->cops && cpu_dai->driver->cops->get_params) {
625 ret = cpu_dai->driver->cops->get_params(cstream, params, cpu_dai);
630 for_each_rtdcom(rtd, rtdcom) {
631 component = rtdcom->component;
633 if (!component->driver->compr_ops ||
634 !component->driver->compr_ops->get_params)
637 __ret = component->driver->compr_ops->get_params(cstream, params);
643 mutex_unlock(&rtd->pcm_mutex);
647 static int soc_compr_get_caps(struct snd_compr_stream *cstream,
648 struct snd_compr_caps *caps)
650 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
651 struct snd_soc_component *component;
652 struct snd_soc_rtdcom_list *rtdcom;
655 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
657 for_each_rtdcom(rtd, rtdcom) {
658 component = rtdcom->component;
660 if (!component->driver->compr_ops ||
661 !component->driver->compr_ops->get_caps)
664 __ret = component->driver->compr_ops->get_caps(cstream, caps);
669 mutex_unlock(&rtd->pcm_mutex);
673 static int soc_compr_get_codec_caps(struct snd_compr_stream *cstream,
674 struct snd_compr_codec_caps *codec)
676 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
677 struct snd_soc_component *component;
678 struct snd_soc_rtdcom_list *rtdcom;
681 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
683 for_each_rtdcom(rtd, rtdcom) {
684 component = rtdcom->component;
686 if (!component->driver->compr_ops ||
687 !component->driver->compr_ops->get_codec_caps)
690 __ret = component->driver->compr_ops->get_codec_caps(cstream, codec);
695 mutex_unlock(&rtd->pcm_mutex);
699 static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
701 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
702 struct snd_soc_component *component;
703 struct snd_soc_rtdcom_list *rtdcom;
704 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
707 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
709 if (cpu_dai->driver->cops && cpu_dai->driver->cops->ack) {
710 ret = cpu_dai->driver->cops->ack(cstream, bytes, cpu_dai);
715 for_each_rtdcom(rtd, rtdcom) {
716 component = rtdcom->component;
718 if (!component->driver->compr_ops ||
719 !component->driver->compr_ops->ack)
722 __ret = component->driver->compr_ops->ack(cstream, bytes);
728 mutex_unlock(&rtd->pcm_mutex);
732 static int soc_compr_pointer(struct snd_compr_stream *cstream,
733 struct snd_compr_tstamp *tstamp)
735 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
736 struct snd_soc_component *component;
737 struct snd_soc_rtdcom_list *rtdcom;
739 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
741 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
743 if (cpu_dai->driver->cops && cpu_dai->driver->cops->pointer)
744 cpu_dai->driver->cops->pointer(cstream, tstamp, cpu_dai);
746 for_each_rtdcom(rtd, rtdcom) {
747 component = rtdcom->component;
749 if (!component->driver->compr_ops ||
750 !component->driver->compr_ops->pointer)
753 __ret = component->driver->compr_ops->pointer(cstream, tstamp);
758 mutex_unlock(&rtd->pcm_mutex);
762 static int soc_compr_copy(struct snd_compr_stream *cstream,
763 char __user *buf, size_t count)
765 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
766 struct snd_soc_component *component;
767 struct snd_soc_rtdcom_list *rtdcom;
770 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
772 for_each_rtdcom(rtd, rtdcom) {
773 component = rtdcom->component;
775 if (!component->driver->compr_ops ||
776 !component->driver->compr_ops->copy)
779 ret = component->driver->compr_ops->copy(cstream, buf, count);
783 mutex_unlock(&rtd->pcm_mutex);
787 static int soc_compr_set_metadata(struct snd_compr_stream *cstream,
788 struct snd_compr_metadata *metadata)
790 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
791 struct snd_soc_component *component;
792 struct snd_soc_rtdcom_list *rtdcom;
793 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
796 if (cpu_dai->driver->cops && cpu_dai->driver->cops->set_metadata) {
797 ret = cpu_dai->driver->cops->set_metadata(cstream, metadata, cpu_dai);
802 for_each_rtdcom(rtd, rtdcom) {
803 component = rtdcom->component;
805 if (!component->driver->compr_ops ||
806 !component->driver->compr_ops->set_metadata)
809 __ret = component->driver->compr_ops->set_metadata(cstream, metadata);
817 static int soc_compr_get_metadata(struct snd_compr_stream *cstream,
818 struct snd_compr_metadata *metadata)
820 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
821 struct snd_soc_component *component;
822 struct snd_soc_rtdcom_list *rtdcom;
823 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
826 if (cpu_dai->driver->cops && cpu_dai->driver->cops->get_metadata) {
827 ret = cpu_dai->driver->cops->get_metadata(cstream, metadata, cpu_dai);
832 for_each_rtdcom(rtd, rtdcom) {
833 component = rtdcom->component;
835 if (!component->driver->compr_ops ||
836 !component->driver->compr_ops->get_metadata)
839 __ret = component->driver->compr_ops->get_metadata(cstream, metadata);
847 /* ASoC Compress operations */
848 static struct snd_compr_ops soc_compr_ops = {
849 .open = soc_compr_open,
850 .free = soc_compr_free,
851 .set_params = soc_compr_set_params,
852 .set_metadata = soc_compr_set_metadata,
853 .get_metadata = soc_compr_get_metadata,
854 .get_params = soc_compr_get_params,
855 .trigger = soc_compr_trigger,
856 .pointer = soc_compr_pointer,
857 .ack = soc_compr_ack,
858 .get_caps = soc_compr_get_caps,
859 .get_codec_caps = soc_compr_get_codec_caps
862 /* ASoC Dynamic Compress operations */
863 static struct snd_compr_ops soc_compr_dyn_ops = {
864 .open = soc_compr_open_fe,
865 .free = soc_compr_free_fe,
866 .set_params = soc_compr_set_params_fe,
867 .get_params = soc_compr_get_params,
868 .set_metadata = soc_compr_set_metadata,
869 .get_metadata = soc_compr_get_metadata,
870 .trigger = soc_compr_trigger_fe,
871 .pointer = soc_compr_pointer,
872 .ack = soc_compr_ack,
873 .get_caps = soc_compr_get_caps,
874 .get_codec_caps = soc_compr_get_codec_caps
878 * snd_soc_new_compress - create a new compress.
880 * @rtd: The runtime for which we will create compress
881 * @num: the device index number (zero based - shared with normal PCMs)
883 * Return: 0 for success, else error.
885 int snd_soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
887 struct snd_soc_component *component;
888 struct snd_soc_rtdcom_list *rtdcom;
889 struct snd_soc_dai *codec_dai = rtd->codec_dai;
890 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
891 struct snd_compr *compr;
892 struct snd_pcm *be_pcm;
894 int ret = 0, direction = 0;
895 int playback = 0, capture = 0;
897 if (rtd->num_codecs > 1) {
898 dev_err(rtd->card->dev,
899 "Compress ASoC: Multicodec not supported\n");
903 /* check client and interface hw capabilities */
904 if (codec_dai->driver->playback.channels_min)
906 if (codec_dai->driver->capture.channels_min)
909 capture = capture && cpu_dai->driver->capture.channels_min;
910 playback = playback && cpu_dai->driver->playback.channels_min;
913 * Compress devices are unidirectional so only one of the directions
914 * should be set, check for that (xor)
916 if (playback + capture != 1) {
917 dev_err(rtd->card->dev,
918 "Compress ASoC: Invalid direction for P %d, C %d\n",
924 direction = SND_COMPRESS_PLAYBACK;
926 direction = SND_COMPRESS_CAPTURE;
928 compr = kzalloc(sizeof(*compr), GFP_KERNEL);
932 compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
939 if (rtd->dai_link->dynamic) {
940 snprintf(new_name, sizeof(new_name), "(%s)",
941 rtd->dai_link->stream_name);
943 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
944 rtd->dai_link->dpcm_playback,
945 rtd->dai_link->dpcm_capture, &be_pcm);
947 dev_err(rtd->card->dev,
948 "Compress ASoC: can't create compressed for %s: %d\n",
949 rtd->dai_link->name, ret);
955 if (rtd->dai_link->dpcm_playback)
956 be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
957 else if (rtd->dai_link->dpcm_capture)
958 be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
959 memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
961 snprintf(new_name, sizeof(new_name), "%s %s-%d",
962 rtd->dai_link->stream_name, codec_dai->name, num);
964 memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
967 for_each_rtdcom(rtd, rtdcom) {
968 component = rtdcom->component;
970 if (!component->driver->compr_ops ||
971 !component->driver->compr_ops->copy)
974 compr->ops->copy = soc_compr_copy;
978 mutex_init(&compr->lock);
979 ret = snd_compress_new(rtd->card->snd_card, num, direction,
982 component = rtd->codec_dai->component;
983 dev_err(component->dev,
984 "Compress ASoC: can't create compress for codec %s: %d\n",
985 component->name, ret);
989 /* DAPM dai link stream work */
990 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
993 compr->private_data = rtd;
995 dev_info(rtd->card->dev, "Compress ASoC: %s <-> %s mapping ok\n",
996 codec_dai->name, cpu_dai->name);
1003 EXPORT_SYMBOL_GPL(snd_soc_new_compress);