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 /* calculate valid and active FE <-> BE dpcms */
153 dpcm_process_paths(fe, stream, &list, 1);
154 fe->dpcm[stream].runtime = fe_substream->runtime;
156 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
158 ret = dpcm_be_dai_startup(fe, stream);
160 /* clean up all links */
161 for_each_dpcm_be(fe, stream, dpcm)
162 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
164 dpcm_be_disconnect(fe, stream);
165 fe->dpcm[stream].runtime = NULL;
169 ret = snd_soc_dai_compr_startup(cpu_dai, cstream);
173 ret = snd_soc_compr_components_open(cstream);
177 ret = snd_soc_link_compr_startup(cstream);
181 dpcm_clear_pending_state(fe, stream);
182 dpcm_path_put(&list);
184 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
185 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
187 mutex_lock_nested(&fe->card->pcm_mutex, fe->card->pcm_subclass);
188 snd_soc_runtime_activate(fe, stream);
189 mutex_unlock(&fe->card->pcm_mutex);
191 mutex_unlock(&fe->card->mutex);
196 snd_soc_compr_components_free(cstream, 1);
198 snd_soc_dai_compr_shutdown(cpu_dai, cstream, 1);
200 dpcm_path_put(&list);
202 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
203 mutex_unlock(&fe->card->mutex);
207 static int soc_compr_free_fe(struct snd_compr_stream *cstream)
209 struct snd_soc_pcm_runtime *fe = cstream->private_data;
210 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
211 struct snd_soc_dpcm *dpcm;
212 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
214 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
216 mutex_lock_nested(&fe->card->pcm_mutex, fe->card->pcm_subclass);
217 snd_soc_runtime_deactivate(fe, stream);
218 mutex_unlock(&fe->card->pcm_mutex);
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 fe->dpcm[stream].runtime = NULL;
239 snd_soc_link_compr_shutdown(cstream, 0);
241 snd_soc_compr_components_free(cstream, 0);
243 snd_soc_dai_compr_shutdown(cpu_dai, cstream, 0);
245 mutex_unlock(&fe->card->mutex);
249 static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd)
251 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
252 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
253 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
254 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
257 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
259 ret = snd_soc_component_compr_trigger(cstream, cmd);
263 ret = snd_soc_dai_compr_trigger(cpu_dai, cstream, cmd);
268 case SNDRV_PCM_TRIGGER_START:
269 snd_soc_dai_digital_mute(codec_dai, 0, stream);
271 case SNDRV_PCM_TRIGGER_STOP:
272 snd_soc_dai_digital_mute(codec_dai, 1, stream);
277 mutex_unlock(&rtd->card->pcm_mutex);
281 static int soc_compr_trigger_fe(struct snd_compr_stream *cstream, int cmd)
283 struct snd_soc_pcm_runtime *fe = cstream->private_data;
284 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
285 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
288 if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN ||
289 cmd == SND_COMPR_TRIGGER_DRAIN)
290 return snd_soc_component_compr_trigger(cstream, cmd);
292 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
294 ret = snd_soc_dai_compr_trigger(cpu_dai, cstream, cmd);
298 ret = snd_soc_component_compr_trigger(cstream, cmd);
302 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
304 ret = dpcm_be_dai_trigger(fe, stream, cmd);
307 case SNDRV_PCM_TRIGGER_START:
308 case SNDRV_PCM_TRIGGER_RESUME:
309 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
310 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
312 case SNDRV_PCM_TRIGGER_STOP:
313 case SNDRV_PCM_TRIGGER_SUSPEND:
314 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
316 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
317 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
322 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
323 mutex_unlock(&fe->card->mutex);
327 static int soc_compr_set_params(struct snd_compr_stream *cstream,
328 struct snd_compr_params *params)
330 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
331 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
332 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
335 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
338 * First we call set_params for the CPU DAI, then the component
339 * driver this should configure the SoC side. If the machine has
340 * compressed ops then we call that as well. The expectation is
341 * that these callbacks will configure everything for this compress
342 * path, like configuring a PCM port for a CODEC.
344 ret = snd_soc_dai_compr_set_params(cpu_dai, cstream, params);
348 ret = snd_soc_component_compr_set_params(cstream, params);
352 ret = snd_soc_link_compr_set_params(cstream);
356 snd_soc_dapm_stream_event(rtd, stream, SND_SOC_DAPM_STREAM_START);
358 /* cancel any delayed stream shutdown that is pending */
360 mutex_unlock(&rtd->card->pcm_mutex);
362 cancel_delayed_work_sync(&rtd->delayed_work);
367 mutex_unlock(&rtd->card->pcm_mutex);
371 static int soc_compr_set_params_fe(struct snd_compr_stream *cstream,
372 struct snd_compr_params *params)
374 struct snd_soc_pcm_runtime *fe = cstream->private_data;
375 struct snd_pcm_substream *fe_substream =
376 fe->pcm->streams[cstream->direction].substream;
377 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
378 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
381 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
384 * Create an empty hw_params for the BE as the machine driver must
385 * fix this up to match DSP decoder and ASRC configuration.
386 * I.e. machine driver fixup for compressed BE is mandatory.
388 memset(&fe->dpcm[fe_substream->stream].hw_params, 0,
389 sizeof(struct snd_pcm_hw_params));
391 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
393 ret = dpcm_be_dai_hw_params(fe, stream);
397 ret = dpcm_be_dai_prepare(fe, stream);
401 ret = snd_soc_dai_compr_set_params(cpu_dai, cstream, params);
405 ret = snd_soc_component_compr_set_params(cstream, params);
409 ret = snd_soc_link_compr_set_params(cstream);
413 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
414 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
417 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
418 mutex_unlock(&fe->card->mutex);
422 static int soc_compr_get_params(struct snd_compr_stream *cstream,
423 struct snd_codec *params)
425 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
426 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
429 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
431 ret = snd_soc_dai_compr_get_params(cpu_dai, cstream, params);
435 ret = snd_soc_component_compr_get_params(cstream, params);
437 mutex_unlock(&rtd->card->pcm_mutex);
441 static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
443 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
444 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
447 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
449 ret = snd_soc_dai_compr_ack(cpu_dai, cstream, bytes);
453 ret = snd_soc_component_compr_ack(cstream, bytes);
455 mutex_unlock(&rtd->card->pcm_mutex);
459 static int soc_compr_pointer(struct snd_compr_stream *cstream,
460 struct snd_compr_tstamp *tstamp)
462 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
464 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
466 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
468 ret = snd_soc_dai_compr_pointer(cpu_dai, cstream, tstamp);
472 ret = snd_soc_component_compr_pointer(cstream, tstamp);
474 mutex_unlock(&rtd->card->pcm_mutex);
478 static int soc_compr_set_metadata(struct snd_compr_stream *cstream,
479 struct snd_compr_metadata *metadata)
481 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
482 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
485 ret = snd_soc_dai_compr_set_metadata(cpu_dai, cstream, metadata);
489 return snd_soc_component_compr_set_metadata(cstream, metadata);
492 static int soc_compr_get_metadata(struct snd_compr_stream *cstream,
493 struct snd_compr_metadata *metadata)
495 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
496 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
499 ret = snd_soc_dai_compr_get_metadata(cpu_dai, cstream, metadata);
503 return snd_soc_component_compr_get_metadata(cstream, metadata);
506 /* ASoC Compress operations */
507 static struct snd_compr_ops soc_compr_ops = {
508 .open = soc_compr_open,
509 .free = soc_compr_free,
510 .set_params = soc_compr_set_params,
511 .set_metadata = soc_compr_set_metadata,
512 .get_metadata = soc_compr_get_metadata,
513 .get_params = soc_compr_get_params,
514 .trigger = soc_compr_trigger,
515 .pointer = soc_compr_pointer,
516 .ack = soc_compr_ack,
517 .get_caps = snd_soc_component_compr_get_caps,
518 .get_codec_caps = snd_soc_component_compr_get_codec_caps,
521 /* ASoC Dynamic Compress operations */
522 static struct snd_compr_ops soc_compr_dyn_ops = {
523 .open = soc_compr_open_fe,
524 .free = soc_compr_free_fe,
525 .set_params = soc_compr_set_params_fe,
526 .get_params = soc_compr_get_params,
527 .set_metadata = soc_compr_set_metadata,
528 .get_metadata = soc_compr_get_metadata,
529 .trigger = soc_compr_trigger_fe,
530 .pointer = soc_compr_pointer,
531 .ack = soc_compr_ack,
532 .get_caps = snd_soc_component_compr_get_caps,
533 .get_codec_caps = snd_soc_component_compr_get_codec_caps,
537 * snd_soc_new_compress - create a new compress.
539 * @rtd: The runtime for which we will create compress
540 * @num: the device index number (zero based - shared with normal PCMs)
542 * Return: 0 for success, else error.
544 int snd_soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
546 struct snd_soc_component *component;
547 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
548 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
549 struct snd_compr *compr;
550 struct snd_pcm *be_pcm;
552 int ret = 0, direction = 0;
553 int playback = 0, capture = 0;
557 * make sure these are same value,
558 * and then use these as equally
560 BUILD_BUG_ON((int)SNDRV_PCM_STREAM_PLAYBACK != (int)SND_COMPRESS_PLAYBACK);
561 BUILD_BUG_ON((int)SNDRV_PCM_STREAM_CAPTURE != (int)SND_COMPRESS_CAPTURE);
563 if (rtd->dai_link->num_cpus > 1 ||
564 rtd->dai_link->num_codecs > 1) {
565 dev_err(rtd->card->dev,
566 "Compress ASoC: Multi CPU/Codec not supported\n");
571 dev_err(rtd->card->dev, "Missing codec\n");
575 /* check client and interface hw capabilities */
576 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
577 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
579 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
580 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
584 * Compress devices are unidirectional so only one of the directions
585 * should be set, check for that (xor)
587 if (playback + capture != 1) {
588 dev_err(rtd->card->dev,
589 "Compress ASoC: Invalid direction for P %d, C %d\n",
595 direction = SND_COMPRESS_PLAYBACK;
597 direction = SND_COMPRESS_CAPTURE;
599 compr = devm_kzalloc(rtd->card->dev, sizeof(*compr), GFP_KERNEL);
603 compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
608 if (rtd->dai_link->dynamic) {
609 snprintf(new_name, sizeof(new_name), "(%s)",
610 rtd->dai_link->stream_name);
612 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
613 rtd->dai_link->dpcm_playback,
614 rtd->dai_link->dpcm_capture, &be_pcm);
616 dev_err(rtd->card->dev,
617 "Compress ASoC: can't create compressed for %s: %d\n",
618 rtd->dai_link->name, ret);
624 if (rtd->dai_link->dpcm_playback)
625 be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
626 else if (rtd->dai_link->dpcm_capture)
627 be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
628 memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
630 snprintf(new_name, sizeof(new_name), "%s %s-%d",
631 rtd->dai_link->stream_name, codec_dai->name, num);
633 memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
636 for_each_rtd_components(rtd, i, component) {
637 if (!component->driver->compress_ops ||
638 !component->driver->compress_ops->copy)
641 compr->ops->copy = snd_soc_component_compr_copy;
645 ret = snd_compress_new(rtd->card->snd_card, num, direction,
648 component = asoc_rtd_to_codec(rtd, 0)->component;
649 dev_err(component->dev,
650 "Compress ASoC: can't create compress for codec %s: %d\n",
651 component->name, ret);
655 /* DAPM dai link stream work */
656 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
659 compr->private_data = rtd;
661 dev_dbg(rtd->card->dev, "Compress ASoC: %s <-> %s mapping ok\n",
662 codec_dai->name, cpu_dai->name);
666 EXPORT_SYMBOL_GPL(snd_soc_new_compress);