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 snd_soc_compr_components_open(struct snd_compr_stream *cstream)
27 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
28 struct snd_soc_component *component;
32 for_each_rtd_components(rtd, i, component) {
33 ret = snd_soc_component_module_get_when_open(component, cstream);
37 ret = snd_soc_component_compr_open(component, cstream);
45 static void snd_soc_compr_components_free(struct snd_compr_stream *cstream,
48 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
49 struct snd_soc_component *component;
52 for_each_rtd_components(rtd, i, component) {
53 snd_soc_component_compr_free(component, cstream, rollback);
54 snd_soc_component_module_put_when_close(component, cstream, rollback);
58 static int soc_compr_clean(struct snd_compr_stream *cstream, int rollback)
60 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
61 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
62 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
63 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
65 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
68 snd_soc_runtime_deactivate(rtd, stream);
70 snd_soc_dai_digital_mute(codec_dai, 1, stream);
72 if (!snd_soc_dai_active(cpu_dai))
75 if (!snd_soc_dai_active(codec_dai))
78 snd_soc_link_compr_shutdown(cstream, rollback);
80 snd_soc_compr_components_free(cstream, rollback);
82 snd_soc_dai_compr_shutdown(cpu_dai, cstream, rollback);
85 snd_soc_dapm_stream_stop(rtd, stream);
87 mutex_unlock(&rtd->card->pcm_mutex);
89 snd_soc_pcm_component_pm_runtime_put(rtd, cstream, rollback);
94 static int soc_compr_free(struct snd_compr_stream *cstream)
96 return soc_compr_clean(cstream, 0);
99 static int soc_compr_open(struct snd_compr_stream *cstream)
101 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
102 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
103 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
106 ret = snd_soc_pcm_component_pm_runtime_get(rtd, cstream);
110 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
112 ret = snd_soc_dai_compr_startup(cpu_dai, cstream);
116 ret = snd_soc_compr_components_open(cstream);
120 ret = snd_soc_link_compr_startup(cstream);
124 snd_soc_runtime_activate(rtd, stream);
126 mutex_unlock(&rtd->card->pcm_mutex);
129 soc_compr_clean(cstream, 1);
134 static int soc_compr_open_fe(struct snd_compr_stream *cstream)
136 struct snd_soc_pcm_runtime *fe = cstream->private_data;
137 struct snd_pcm_substream *fe_substream =
138 fe->pcm->streams[cstream->direction].substream;
139 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
140 struct snd_soc_dpcm *dpcm;
141 struct snd_soc_dapm_widget_list *list;
142 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
145 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
146 fe->dpcm[stream].runtime = fe_substream->runtime;
148 ret = dpcm_path_get(fe, stream, &list);
152 mutex_lock_nested(&fe->card->pcm_mutex, fe->card->pcm_subclass);
154 /* calculate valid and active FE <-> BE dpcms */
155 dpcm_process_paths(fe, stream, &list, 1);
156 fe->dpcm[stream].runtime = fe_substream->runtime;
158 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
160 ret = dpcm_be_dai_startup(fe, stream);
162 /* clean up all links */
163 for_each_dpcm_be(fe, stream, dpcm)
164 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
166 dpcm_be_disconnect(fe, stream);
167 fe->dpcm[stream].runtime = NULL;
171 ret = snd_soc_dai_compr_startup(cpu_dai, cstream);
175 ret = snd_soc_compr_components_open(cstream);
179 ret = snd_soc_link_compr_startup(cstream);
183 dpcm_clear_pending_state(fe, stream);
184 dpcm_path_put(&list);
186 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
187 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
189 snd_soc_runtime_activate(fe, stream);
190 mutex_unlock(&fe->card->pcm_mutex);
192 mutex_unlock(&fe->card->mutex);
197 snd_soc_compr_components_free(cstream, 1);
199 snd_soc_dai_compr_shutdown(cpu_dai, cstream, 1);
201 dpcm_path_put(&list);
203 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
204 mutex_unlock(&fe->card->mutex);
208 static int soc_compr_free_fe(struct snd_compr_stream *cstream)
210 struct snd_soc_pcm_runtime *fe = cstream->private_data;
211 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
212 struct snd_soc_dpcm *dpcm;
213 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
215 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
217 mutex_lock_nested(&fe->card->pcm_mutex, fe->card->pcm_subclass);
218 snd_soc_runtime_deactivate(fe, stream);
220 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
222 dpcm_be_dai_hw_free(fe, stream);
224 dpcm_be_dai_shutdown(fe, stream);
226 /* mark FE's links ready to prune */
227 for_each_dpcm_be(fe, stream, dpcm)
228 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
230 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
232 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
233 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
235 dpcm_be_disconnect(fe, stream);
237 mutex_unlock(&fe->card->pcm_mutex);
239 fe->dpcm[stream].runtime = NULL;
241 snd_soc_link_compr_shutdown(cstream, 0);
243 snd_soc_compr_components_free(cstream, 0);
245 snd_soc_dai_compr_shutdown(cpu_dai, cstream, 0);
247 mutex_unlock(&fe->card->mutex);
251 static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd)
253 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
254 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
255 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
256 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
259 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
261 ret = snd_soc_component_compr_trigger(cstream, cmd);
265 ret = snd_soc_dai_compr_trigger(cpu_dai, cstream, cmd);
270 case SNDRV_PCM_TRIGGER_START:
271 snd_soc_dai_digital_mute(codec_dai, 0, stream);
273 case SNDRV_PCM_TRIGGER_STOP:
274 snd_soc_dai_digital_mute(codec_dai, 1, stream);
279 mutex_unlock(&rtd->card->pcm_mutex);
283 static int soc_compr_trigger_fe(struct snd_compr_stream *cstream, int cmd)
285 struct snd_soc_pcm_runtime *fe = cstream->private_data;
286 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
287 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
290 if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN ||
291 cmd == SND_COMPR_TRIGGER_DRAIN)
292 return snd_soc_component_compr_trigger(cstream, cmd);
294 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
296 ret = snd_soc_dai_compr_trigger(cpu_dai, cstream, cmd);
300 ret = snd_soc_component_compr_trigger(cstream, cmd);
304 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
306 ret = dpcm_be_dai_trigger(fe, stream, cmd);
309 case SNDRV_PCM_TRIGGER_START:
310 case SNDRV_PCM_TRIGGER_RESUME:
311 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
312 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
314 case SNDRV_PCM_TRIGGER_STOP:
315 case SNDRV_PCM_TRIGGER_SUSPEND:
316 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
318 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
319 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
324 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
325 mutex_unlock(&fe->card->mutex);
329 static int soc_compr_set_params(struct snd_compr_stream *cstream,
330 struct snd_compr_params *params)
332 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
333 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
334 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
337 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
340 * First we call set_params for the CPU DAI, then the component
341 * driver this should configure the SoC side. If the machine has
342 * compressed ops then we call that as well. The expectation is
343 * that these callbacks will configure everything for this compress
344 * path, like configuring a PCM port for a CODEC.
346 ret = snd_soc_dai_compr_set_params(cpu_dai, cstream, params);
350 ret = snd_soc_component_compr_set_params(cstream, params);
354 ret = snd_soc_link_compr_set_params(cstream);
358 snd_soc_dapm_stream_event(rtd, stream, SND_SOC_DAPM_STREAM_START);
360 /* cancel any delayed stream shutdown that is pending */
362 mutex_unlock(&rtd->card->pcm_mutex);
364 cancel_delayed_work_sync(&rtd->delayed_work);
369 mutex_unlock(&rtd->card->pcm_mutex);
373 static int soc_compr_set_params_fe(struct snd_compr_stream *cstream,
374 struct snd_compr_params *params)
376 struct snd_soc_pcm_runtime *fe = cstream->private_data;
377 struct snd_pcm_substream *fe_substream =
378 fe->pcm->streams[cstream->direction].substream;
379 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
380 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
383 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
386 * Create an empty hw_params for the BE as the machine driver must
387 * fix this up to match DSP decoder and ASRC configuration.
388 * I.e. machine driver fixup for compressed BE is mandatory.
390 memset(&fe->dpcm[fe_substream->stream].hw_params, 0,
391 sizeof(struct snd_pcm_hw_params));
393 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
395 ret = dpcm_be_dai_hw_params(fe, stream);
399 ret = dpcm_be_dai_prepare(fe, stream);
403 ret = snd_soc_dai_compr_set_params(cpu_dai, cstream, params);
407 ret = snd_soc_component_compr_set_params(cstream, params);
411 ret = snd_soc_link_compr_set_params(cstream);
414 mutex_lock_nested(&fe->card->pcm_mutex, fe->card->pcm_subclass);
415 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
416 mutex_unlock(&fe->card->pcm_mutex);
417 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
420 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
421 mutex_unlock(&fe->card->mutex);
425 static int soc_compr_get_params(struct snd_compr_stream *cstream,
426 struct snd_codec *params)
428 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
429 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
432 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
434 ret = snd_soc_dai_compr_get_params(cpu_dai, cstream, params);
438 ret = snd_soc_component_compr_get_params(cstream, params);
440 mutex_unlock(&rtd->card->pcm_mutex);
444 static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
446 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
447 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
450 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
452 ret = snd_soc_dai_compr_ack(cpu_dai, cstream, bytes);
456 ret = snd_soc_component_compr_ack(cstream, bytes);
458 mutex_unlock(&rtd->card->pcm_mutex);
462 static int soc_compr_pointer(struct snd_compr_stream *cstream,
463 struct snd_compr_tstamp *tstamp)
465 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
467 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
469 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
471 ret = snd_soc_dai_compr_pointer(cpu_dai, cstream, tstamp);
475 ret = snd_soc_component_compr_pointer(cstream, tstamp);
477 mutex_unlock(&rtd->card->pcm_mutex);
481 static int soc_compr_set_metadata(struct snd_compr_stream *cstream,
482 struct snd_compr_metadata *metadata)
484 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
485 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
488 ret = snd_soc_dai_compr_set_metadata(cpu_dai, cstream, metadata);
492 return snd_soc_component_compr_set_metadata(cstream, metadata);
495 static int soc_compr_get_metadata(struct snd_compr_stream *cstream,
496 struct snd_compr_metadata *metadata)
498 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
499 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
502 ret = snd_soc_dai_compr_get_metadata(cpu_dai, cstream, metadata);
506 return snd_soc_component_compr_get_metadata(cstream, metadata);
509 /* ASoC Compress operations */
510 static struct snd_compr_ops soc_compr_ops = {
511 .open = soc_compr_open,
512 .free = soc_compr_free,
513 .set_params = soc_compr_set_params,
514 .set_metadata = soc_compr_set_metadata,
515 .get_metadata = soc_compr_get_metadata,
516 .get_params = soc_compr_get_params,
517 .trigger = soc_compr_trigger,
518 .pointer = soc_compr_pointer,
519 .ack = soc_compr_ack,
520 .get_caps = snd_soc_component_compr_get_caps,
521 .get_codec_caps = snd_soc_component_compr_get_codec_caps,
524 /* ASoC Dynamic Compress operations */
525 static struct snd_compr_ops soc_compr_dyn_ops = {
526 .open = soc_compr_open_fe,
527 .free = soc_compr_free_fe,
528 .set_params = soc_compr_set_params_fe,
529 .get_params = soc_compr_get_params,
530 .set_metadata = soc_compr_set_metadata,
531 .get_metadata = soc_compr_get_metadata,
532 .trigger = soc_compr_trigger_fe,
533 .pointer = soc_compr_pointer,
534 .ack = soc_compr_ack,
535 .get_caps = snd_soc_component_compr_get_caps,
536 .get_codec_caps = snd_soc_component_compr_get_codec_caps,
540 * snd_soc_new_compress - create a new compress.
542 * @rtd: The runtime for which we will create compress
543 * @num: the device index number (zero based - shared with normal PCMs)
545 * Return: 0 for success, else error.
547 int snd_soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
549 struct snd_soc_component *component;
550 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
551 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
552 struct snd_compr *compr;
553 struct snd_pcm *be_pcm;
555 int ret = 0, direction = 0;
556 int playback = 0, capture = 0;
560 * make sure these are same value,
561 * and then use these as equally
563 BUILD_BUG_ON((int)SNDRV_PCM_STREAM_PLAYBACK != (int)SND_COMPRESS_PLAYBACK);
564 BUILD_BUG_ON((int)SNDRV_PCM_STREAM_CAPTURE != (int)SND_COMPRESS_CAPTURE);
566 if (rtd->dai_link->num_cpus > 1 ||
567 rtd->dai_link->num_codecs > 1) {
568 dev_err(rtd->card->dev,
569 "Compress ASoC: Multi CPU/Codec not supported\n");
574 dev_err(rtd->card->dev, "Missing codec\n");
578 /* check client and interface hw capabilities */
579 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
580 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
582 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
583 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
587 * Compress devices are unidirectional so only one of the directions
588 * should be set, check for that (xor)
590 if (playback + capture != 1) {
591 dev_err(rtd->card->dev,
592 "Compress ASoC: Invalid direction for P %d, C %d\n",
598 direction = SND_COMPRESS_PLAYBACK;
600 direction = SND_COMPRESS_CAPTURE;
602 compr = devm_kzalloc(rtd->card->dev, sizeof(*compr), GFP_KERNEL);
606 compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
611 if (rtd->dai_link->dynamic) {
612 snprintf(new_name, sizeof(new_name), "(%s)",
613 rtd->dai_link->stream_name);
615 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
616 rtd->dai_link->dpcm_playback,
617 rtd->dai_link->dpcm_capture, &be_pcm);
619 dev_err(rtd->card->dev,
620 "Compress ASoC: can't create compressed for %s: %d\n",
621 rtd->dai_link->name, ret);
625 /* inherit atomicity from DAI link */
626 be_pcm->nonatomic = rtd->dai_link->nonatomic;
630 if (rtd->dai_link->dpcm_playback)
631 be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
632 if (rtd->dai_link->dpcm_capture)
633 be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
634 memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
636 snprintf(new_name, sizeof(new_name), "%s %s-%d",
637 rtd->dai_link->stream_name, codec_dai->name, num);
639 memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
642 for_each_rtd_components(rtd, i, component) {
643 if (!component->driver->compress_ops ||
644 !component->driver->compress_ops->copy)
647 compr->ops->copy = snd_soc_component_compr_copy;
651 ret = snd_compress_new(rtd->card->snd_card, num, direction,
654 component = asoc_rtd_to_codec(rtd, 0)->component;
655 dev_err(component->dev,
656 "Compress ASoC: can't create compress for codec %s: %d\n",
657 component->name, ret);
661 /* DAPM dai link stream work */
662 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
665 compr->private_data = rtd;
667 dev_dbg(rtd->card->dev, "Compress ASoC: %s <-> %s mapping ok\n",
668 codec_dai->name, cpu_dai->name);
672 EXPORT_SYMBOL_GPL(snd_soc_new_compress);