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>
22 #include <sound/soc-link.h>
23 #include <linux/pm_runtime.h>
25 static int soc_compr_open(struct snd_compr_stream *cstream)
27 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
28 struct snd_soc_component *component = NULL;
29 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
30 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
33 ret = snd_soc_pcm_component_pm_runtime_get(rtd, cstream);
37 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
39 ret = snd_soc_dai_compr_startup(cpu_dai, cstream);
43 ret = snd_soc_component_compr_open(cstream, &component);
47 ret = snd_soc_link_compr_startup(cstream);
51 snd_soc_runtime_activate(rtd, stream);
53 mutex_unlock(&rtd->card->pcm_mutex);
58 snd_soc_component_compr_free(cstream, component);
60 snd_soc_dai_compr_shutdown(cpu_dai, cstream);
62 mutex_unlock(&rtd->card->pcm_mutex);
64 snd_soc_pcm_component_pm_runtime_put(rtd, cstream, 1);
69 static int soc_compr_open_fe(struct snd_compr_stream *cstream)
71 struct snd_soc_pcm_runtime *fe = cstream->private_data;
72 struct snd_pcm_substream *fe_substream =
73 fe->pcm->streams[cstream->direction].substream;
74 struct snd_soc_component *component;
75 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
76 struct snd_soc_dpcm *dpcm;
77 struct snd_soc_dapm_widget_list *list;
78 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
81 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
82 fe->dpcm[stream].runtime = fe_substream->runtime;
84 ret = dpcm_path_get(fe, stream, &list);
88 dev_dbg(fe->dev, "Compress ASoC: %s no valid %s route\n",
89 fe->dai_link->name, stream ? "capture" : "playback");
90 /* calculate valid and active FE <-> BE dpcms */
91 dpcm_process_paths(fe, stream, &list, 1);
92 fe->dpcm[stream].runtime = fe_substream->runtime;
94 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
96 ret = dpcm_be_dai_startup(fe, stream);
98 /* clean up all links */
99 for_each_dpcm_be(fe, stream, dpcm)
100 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
102 dpcm_be_disconnect(fe, stream);
103 fe->dpcm[stream].runtime = NULL;
107 ret = snd_soc_dai_compr_startup(cpu_dai, cstream);
111 ret = snd_soc_component_compr_open(cstream, &component);
115 ret = snd_soc_link_compr_startup(cstream);
119 dpcm_clear_pending_state(fe, stream);
120 dpcm_path_put(&list);
122 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
123 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
125 snd_soc_runtime_activate(fe, stream);
127 mutex_unlock(&fe->card->mutex);
132 snd_soc_component_compr_free(cstream, component);
134 snd_soc_dai_compr_shutdown(cpu_dai, cstream);
136 dpcm_path_put(&list);
138 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
139 mutex_unlock(&fe->card->mutex);
143 static int soc_compr_free(struct snd_compr_stream *cstream)
145 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
146 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
147 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
148 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
150 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
152 snd_soc_runtime_deactivate(rtd, stream);
154 snd_soc_dai_digital_mute(codec_dai, 1, stream);
156 if (!snd_soc_dai_active(cpu_dai))
159 if (!snd_soc_dai_active(codec_dai))
162 snd_soc_link_compr_shutdown(cstream);
164 snd_soc_component_compr_free(cstream, NULL);
166 snd_soc_dai_compr_shutdown(cpu_dai, cstream);
168 snd_soc_dapm_stream_stop(rtd, stream);
170 mutex_unlock(&rtd->card->pcm_mutex);
172 snd_soc_pcm_component_pm_runtime_put(rtd, cstream, 0);
177 static int soc_compr_free_fe(struct snd_compr_stream *cstream)
179 struct snd_soc_pcm_runtime *fe = cstream->private_data;
180 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
181 struct snd_soc_dpcm *dpcm;
182 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
185 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
187 snd_soc_runtime_deactivate(fe, stream);
189 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
191 ret = dpcm_be_dai_hw_free(fe, stream);
193 dev_err(fe->dev, "Compressed ASoC: hw_free failed: %d\n", ret);
195 ret = dpcm_be_dai_shutdown(fe, stream);
197 /* mark FE's links ready to prune */
198 for_each_dpcm_be(fe, stream, dpcm)
199 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
201 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
203 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
204 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
206 dpcm_be_disconnect(fe, stream);
208 fe->dpcm[stream].runtime = NULL;
210 snd_soc_link_compr_shutdown(cstream);
212 snd_soc_component_compr_free(cstream, NULL);
214 snd_soc_dai_compr_shutdown(cpu_dai, cstream);
216 mutex_unlock(&fe->card->mutex);
220 static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd)
222 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
223 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
224 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
225 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
228 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
230 ret = snd_soc_component_compr_trigger(cstream, cmd);
234 ret = snd_soc_dai_compr_trigger(cpu_dai, cstream, cmd);
239 case SNDRV_PCM_TRIGGER_START:
240 snd_soc_dai_digital_mute(codec_dai, 0, stream);
242 case SNDRV_PCM_TRIGGER_STOP:
243 snd_soc_dai_digital_mute(codec_dai, 1, stream);
248 mutex_unlock(&rtd->card->pcm_mutex);
252 static int soc_compr_trigger_fe(struct snd_compr_stream *cstream, int cmd)
254 struct snd_soc_pcm_runtime *fe = cstream->private_data;
255 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
256 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
259 if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN ||
260 cmd == SND_COMPR_TRIGGER_DRAIN)
261 return snd_soc_component_compr_trigger(cstream, cmd);
263 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
265 ret = snd_soc_dai_compr_trigger(cpu_dai, cstream, cmd);
269 ret = snd_soc_component_compr_trigger(cstream, cmd);
273 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
275 ret = dpcm_be_dai_trigger(fe, stream, cmd);
278 case SNDRV_PCM_TRIGGER_START:
279 case SNDRV_PCM_TRIGGER_RESUME:
280 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
281 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
283 case SNDRV_PCM_TRIGGER_STOP:
284 case SNDRV_PCM_TRIGGER_SUSPEND:
285 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
287 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
288 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
293 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
294 mutex_unlock(&fe->card->mutex);
298 static int soc_compr_set_params(struct snd_compr_stream *cstream,
299 struct snd_compr_params *params)
301 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
302 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
303 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
306 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
309 * First we call set_params for the CPU DAI, then the component
310 * driver this should configure the SoC side. If the machine has
311 * compressed ops then we call that as well. The expectation is
312 * that these callbacks will configure everything for this compress
313 * path, like configuring a PCM port for a CODEC.
315 ret = snd_soc_dai_compr_set_params(cpu_dai, cstream, params);
319 ret = snd_soc_component_compr_set_params(cstream, params);
323 ret = snd_soc_link_compr_set_params(cstream);
327 snd_soc_dapm_stream_event(rtd, stream, SND_SOC_DAPM_STREAM_START);
329 /* cancel any delayed stream shutdown that is pending */
331 mutex_unlock(&rtd->card->pcm_mutex);
333 cancel_delayed_work_sync(&rtd->delayed_work);
338 mutex_unlock(&rtd->card->pcm_mutex);
342 static int soc_compr_set_params_fe(struct snd_compr_stream *cstream,
343 struct snd_compr_params *params)
345 struct snd_soc_pcm_runtime *fe = cstream->private_data;
346 struct snd_pcm_substream *fe_substream =
347 fe->pcm->streams[cstream->direction].substream;
348 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
349 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
352 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
355 * Create an empty hw_params for the BE as the machine driver must
356 * fix this up to match DSP decoder and ASRC configuration.
357 * I.e. machine driver fixup for compressed BE is mandatory.
359 memset(&fe->dpcm[fe_substream->stream].hw_params, 0,
360 sizeof(struct snd_pcm_hw_params));
362 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
364 ret = dpcm_be_dai_hw_params(fe, stream);
368 ret = dpcm_be_dai_prepare(fe, stream);
372 ret = snd_soc_dai_compr_set_params(cpu_dai, cstream, params);
376 ret = snd_soc_component_compr_set_params(cstream, params);
380 ret = snd_soc_link_compr_set_params(cstream);
384 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
385 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
388 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
389 mutex_unlock(&fe->card->mutex);
393 static int soc_compr_get_params(struct snd_compr_stream *cstream,
394 struct snd_codec *params)
396 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
397 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
400 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
402 ret = snd_soc_dai_compr_get_params(cpu_dai, cstream, params);
406 ret = snd_soc_component_compr_get_params(cstream, params);
408 mutex_unlock(&rtd->card->pcm_mutex);
412 static int soc_compr_get_caps(struct snd_compr_stream *cstream,
413 struct snd_compr_caps *caps)
415 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
416 struct snd_soc_component *component;
419 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
421 for_each_rtd_components(rtd, i, component) {
422 if (!component->driver->compress_ops ||
423 !component->driver->compress_ops->get_caps)
426 ret = component->driver->compress_ops->get_caps(
427 component, cstream, caps);
431 mutex_unlock(&rtd->card->pcm_mutex);
435 static int soc_compr_get_codec_caps(struct snd_compr_stream *cstream,
436 struct snd_compr_codec_caps *codec)
438 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
439 struct snd_soc_component *component;
442 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
444 for_each_rtd_components(rtd, i, component) {
445 if (!component->driver->compress_ops ||
446 !component->driver->compress_ops->get_codec_caps)
449 ret = component->driver->compress_ops->get_codec_caps(
450 component, cstream, codec);
454 mutex_unlock(&rtd->card->pcm_mutex);
458 static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
460 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
461 struct snd_soc_component *component;
462 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
465 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
467 ret = snd_soc_dai_compr_ack(cpu_dai, cstream, bytes);
471 for_each_rtd_components(rtd, i, component) {
472 if (!component->driver->compress_ops ||
473 !component->driver->compress_ops->ack)
476 ret = component->driver->compress_ops->ack(
477 component, cstream, bytes);
483 mutex_unlock(&rtd->card->pcm_mutex);
487 static int soc_compr_pointer(struct snd_compr_stream *cstream,
488 struct snd_compr_tstamp *tstamp)
490 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
491 struct snd_soc_component *component;
493 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
495 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
497 ret = snd_soc_dai_compr_pointer(cpu_dai, cstream, tstamp);
501 for_each_rtd_components(rtd, i, component) {
502 if (!component->driver->compress_ops ||
503 !component->driver->compress_ops->pointer)
506 ret = component->driver->compress_ops->pointer(
507 component, cstream, tstamp);
511 mutex_unlock(&rtd->card->pcm_mutex);
515 static int soc_compr_copy(struct snd_compr_stream *cstream,
516 char __user *buf, size_t count)
518 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
519 struct snd_soc_component *component;
522 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
524 for_each_rtd_components(rtd, i, component) {
525 if (!component->driver->compress_ops ||
526 !component->driver->compress_ops->copy)
529 ret = component->driver->compress_ops->copy(
530 component, cstream, buf, count);
534 mutex_unlock(&rtd->card->pcm_mutex);
538 static int soc_compr_set_metadata(struct snd_compr_stream *cstream,
539 struct snd_compr_metadata *metadata)
541 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
542 struct snd_soc_component *component;
543 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
546 ret = snd_soc_dai_compr_set_metadata(cpu_dai, cstream, metadata);
550 for_each_rtd_components(rtd, i, component) {
551 if (!component->driver->compress_ops ||
552 !component->driver->compress_ops->set_metadata)
555 ret = component->driver->compress_ops->set_metadata(
556 component, cstream, metadata);
564 static int soc_compr_get_metadata(struct snd_compr_stream *cstream,
565 struct snd_compr_metadata *metadata)
567 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
568 struct snd_soc_component *component;
569 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
572 ret = snd_soc_dai_compr_get_metadata(cpu_dai, cstream, metadata);
576 for_each_rtd_components(rtd, i, component) {
577 if (!component->driver->compress_ops ||
578 !component->driver->compress_ops->get_metadata)
581 return component->driver->compress_ops->get_metadata(
582 component, cstream, metadata);
588 /* ASoC Compress operations */
589 static struct snd_compr_ops soc_compr_ops = {
590 .open = soc_compr_open,
591 .free = soc_compr_free,
592 .set_params = soc_compr_set_params,
593 .set_metadata = soc_compr_set_metadata,
594 .get_metadata = soc_compr_get_metadata,
595 .get_params = soc_compr_get_params,
596 .trigger = soc_compr_trigger,
597 .pointer = soc_compr_pointer,
598 .ack = soc_compr_ack,
599 .get_caps = soc_compr_get_caps,
600 .get_codec_caps = soc_compr_get_codec_caps
603 /* ASoC Dynamic Compress operations */
604 static struct snd_compr_ops soc_compr_dyn_ops = {
605 .open = soc_compr_open_fe,
606 .free = soc_compr_free_fe,
607 .set_params = soc_compr_set_params_fe,
608 .get_params = soc_compr_get_params,
609 .set_metadata = soc_compr_set_metadata,
610 .get_metadata = soc_compr_get_metadata,
611 .trigger = soc_compr_trigger_fe,
612 .pointer = soc_compr_pointer,
613 .ack = soc_compr_ack,
614 .get_caps = soc_compr_get_caps,
615 .get_codec_caps = soc_compr_get_codec_caps
619 * snd_soc_new_compress - create a new compress.
621 * @rtd: The runtime for which we will create compress
622 * @num: the device index number (zero based - shared with normal PCMs)
624 * Return: 0 for success, else error.
626 int snd_soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
628 struct snd_soc_component *component;
629 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
630 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
631 struct snd_compr *compr;
632 struct snd_pcm *be_pcm;
634 int ret = 0, direction = 0;
635 int playback = 0, capture = 0;
639 * make sure these are same value,
640 * and then use these as equally
642 BUILD_BUG_ON((int)SNDRV_PCM_STREAM_PLAYBACK != (int)SND_COMPRESS_PLAYBACK);
643 BUILD_BUG_ON((int)SNDRV_PCM_STREAM_CAPTURE != (int)SND_COMPRESS_CAPTURE);
645 if (rtd->num_cpus > 1 ||
646 rtd->num_codecs > 1) {
647 dev_err(rtd->card->dev,
648 "Compress ASoC: Multi CPU/Codec not supported\n");
652 /* check client and interface hw capabilities */
653 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
654 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
656 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
657 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
661 * Compress devices are unidirectional so only one of the directions
662 * should be set, check for that (xor)
664 if (playback + capture != 1) {
665 dev_err(rtd->card->dev,
666 "Compress ASoC: Invalid direction for P %d, C %d\n",
672 direction = SND_COMPRESS_PLAYBACK;
674 direction = SND_COMPRESS_CAPTURE;
676 compr = devm_kzalloc(rtd->card->dev, sizeof(*compr), GFP_KERNEL);
680 compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
685 if (rtd->dai_link->dynamic) {
686 snprintf(new_name, sizeof(new_name), "(%s)",
687 rtd->dai_link->stream_name);
689 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
690 rtd->dai_link->dpcm_playback,
691 rtd->dai_link->dpcm_capture, &be_pcm);
693 dev_err(rtd->card->dev,
694 "Compress ASoC: can't create compressed for %s: %d\n",
695 rtd->dai_link->name, ret);
701 if (rtd->dai_link->dpcm_playback)
702 be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
703 else if (rtd->dai_link->dpcm_capture)
704 be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
705 memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
707 snprintf(new_name, sizeof(new_name), "%s %s-%d",
708 rtd->dai_link->stream_name, codec_dai->name, num);
710 memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
713 for_each_rtd_components(rtd, i, component) {
714 if (!component->driver->compress_ops ||
715 !component->driver->compress_ops->copy)
718 compr->ops->copy = soc_compr_copy;
722 mutex_init(&compr->lock);
723 ret = snd_compress_new(rtd->card->snd_card, num, direction,
726 component = asoc_rtd_to_codec(rtd, 0)->component;
727 dev_err(component->dev,
728 "Compress ASoC: can't create compress for codec %s: %d\n",
729 component->name, ret);
733 /* DAPM dai link stream work */
734 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
737 compr->private_data = rtd;
739 dev_dbg(rtd->card->dev, "Compress ASoC: %s <-> %s mapping ok\n",
740 codec_dai->name, cpu_dai->name);
744 EXPORT_SYMBOL_GPL(snd_soc_new_compress);