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_clean(struct snd_compr_stream *cstream, int rollback)
27 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
28 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
29 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
30 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
32 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
35 snd_soc_runtime_deactivate(rtd, stream);
37 snd_soc_dai_digital_mute(codec_dai, 1, stream);
39 if (!snd_soc_dai_active(cpu_dai))
42 if (!snd_soc_dai_active(codec_dai))
45 snd_soc_link_compr_shutdown(cstream, rollback);
47 snd_soc_component_compr_free(cstream, rollback);
49 snd_soc_dai_compr_shutdown(cpu_dai, cstream, rollback);
52 snd_soc_dapm_stream_stop(rtd, stream);
54 mutex_unlock(&rtd->card->pcm_mutex);
56 snd_soc_pcm_component_pm_runtime_put(rtd, cstream, rollback);
61 static int soc_compr_free(struct snd_compr_stream *cstream)
63 return soc_compr_clean(cstream, 0);
66 static int soc_compr_open(struct snd_compr_stream *cstream)
68 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
69 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
70 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
73 ret = snd_soc_pcm_component_pm_runtime_get(rtd, cstream);
77 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
79 ret = snd_soc_dai_compr_startup(cpu_dai, cstream);
83 ret = snd_soc_component_compr_open(cstream);
87 ret = snd_soc_link_compr_startup(cstream);
91 snd_soc_runtime_activate(rtd, stream);
93 mutex_unlock(&rtd->card->pcm_mutex);
96 soc_compr_clean(cstream, 1);
101 static int soc_compr_open_fe(struct snd_compr_stream *cstream)
103 struct snd_soc_pcm_runtime *fe = cstream->private_data;
104 struct snd_pcm_substream *fe_substream =
105 fe->pcm->streams[cstream->direction].substream;
106 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
107 struct snd_soc_dpcm *dpcm;
108 struct snd_soc_dapm_widget_list *list;
109 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
112 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
113 fe->dpcm[stream].runtime = fe_substream->runtime;
115 ret = dpcm_path_get(fe, stream, &list);
119 /* calculate valid and active FE <-> BE dpcms */
120 dpcm_process_paths(fe, stream, &list, 1);
121 fe->dpcm[stream].runtime = fe_substream->runtime;
123 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
125 ret = dpcm_be_dai_startup(fe, stream);
127 /* clean up all links */
128 for_each_dpcm_be(fe, stream, dpcm)
129 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
131 dpcm_be_disconnect(fe, stream);
132 fe->dpcm[stream].runtime = NULL;
136 ret = snd_soc_dai_compr_startup(cpu_dai, cstream);
140 ret = snd_soc_component_compr_open(cstream);
144 ret = snd_soc_link_compr_startup(cstream);
148 dpcm_clear_pending_state(fe, stream);
149 dpcm_path_put(&list);
151 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
152 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
154 mutex_lock_nested(&fe->card->pcm_mutex, fe->card->pcm_subclass);
155 snd_soc_runtime_activate(fe, stream);
156 mutex_unlock(&fe->card->pcm_mutex);
158 mutex_unlock(&fe->card->mutex);
163 snd_soc_component_compr_free(cstream, 1);
165 snd_soc_dai_compr_shutdown(cpu_dai, cstream, 1);
167 dpcm_path_put(&list);
169 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
170 mutex_unlock(&fe->card->mutex);
174 static int soc_compr_free_fe(struct snd_compr_stream *cstream)
176 struct snd_soc_pcm_runtime *fe = cstream->private_data;
177 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
178 struct snd_soc_dpcm *dpcm;
179 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
181 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
183 mutex_lock_nested(&fe->card->pcm_mutex, fe->card->pcm_subclass);
184 snd_soc_runtime_deactivate(fe, stream);
185 mutex_unlock(&fe->card->pcm_mutex);
187 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
189 dpcm_be_dai_hw_free(fe, stream);
191 dpcm_be_dai_shutdown(fe, stream);
193 /* mark FE's links ready to prune */
194 for_each_dpcm_be(fe, stream, dpcm)
195 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
197 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
199 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
200 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
202 dpcm_be_disconnect(fe, stream);
204 fe->dpcm[stream].runtime = NULL;
206 snd_soc_link_compr_shutdown(cstream, 0);
208 snd_soc_component_compr_free(cstream, 0);
210 snd_soc_dai_compr_shutdown(cpu_dai, cstream, 0);
212 mutex_unlock(&fe->card->mutex);
216 static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd)
218 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
219 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
220 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
221 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
224 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
226 ret = snd_soc_component_compr_trigger(cstream, cmd);
230 ret = snd_soc_dai_compr_trigger(cpu_dai, cstream, cmd);
235 case SNDRV_PCM_TRIGGER_START:
236 snd_soc_dai_digital_mute(codec_dai, 0, stream);
238 case SNDRV_PCM_TRIGGER_STOP:
239 snd_soc_dai_digital_mute(codec_dai, 1, stream);
244 mutex_unlock(&rtd->card->pcm_mutex);
248 static int soc_compr_trigger_fe(struct snd_compr_stream *cstream, int cmd)
250 struct snd_soc_pcm_runtime *fe = cstream->private_data;
251 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
252 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
255 if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN ||
256 cmd == SND_COMPR_TRIGGER_DRAIN)
257 return snd_soc_component_compr_trigger(cstream, cmd);
259 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
261 ret = snd_soc_dai_compr_trigger(cpu_dai, cstream, cmd);
265 ret = snd_soc_component_compr_trigger(cstream, cmd);
269 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
271 ret = dpcm_be_dai_trigger(fe, stream, cmd);
274 case SNDRV_PCM_TRIGGER_START:
275 case SNDRV_PCM_TRIGGER_RESUME:
276 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
277 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
279 case SNDRV_PCM_TRIGGER_STOP:
280 case SNDRV_PCM_TRIGGER_SUSPEND:
281 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
283 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
284 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
289 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
290 mutex_unlock(&fe->card->mutex);
294 static int soc_compr_set_params(struct snd_compr_stream *cstream,
295 struct snd_compr_params *params)
297 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
298 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
299 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
302 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
305 * First we call set_params for the CPU DAI, then the component
306 * driver this should configure the SoC side. If the machine has
307 * compressed ops then we call that as well. The expectation is
308 * that these callbacks will configure everything for this compress
309 * path, like configuring a PCM port for a CODEC.
311 ret = snd_soc_dai_compr_set_params(cpu_dai, cstream, params);
315 ret = snd_soc_component_compr_set_params(cstream, params);
319 ret = snd_soc_link_compr_set_params(cstream);
323 snd_soc_dapm_stream_event(rtd, stream, SND_SOC_DAPM_STREAM_START);
325 /* cancel any delayed stream shutdown that is pending */
327 mutex_unlock(&rtd->card->pcm_mutex);
329 cancel_delayed_work_sync(&rtd->delayed_work);
334 mutex_unlock(&rtd->card->pcm_mutex);
338 static int soc_compr_set_params_fe(struct snd_compr_stream *cstream,
339 struct snd_compr_params *params)
341 struct snd_soc_pcm_runtime *fe = cstream->private_data;
342 struct snd_pcm_substream *fe_substream =
343 fe->pcm->streams[cstream->direction].substream;
344 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
345 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
348 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
351 * Create an empty hw_params for the BE as the machine driver must
352 * fix this up to match DSP decoder and ASRC configuration.
353 * I.e. machine driver fixup for compressed BE is mandatory.
355 memset(&fe->dpcm[fe_substream->stream].hw_params, 0,
356 sizeof(struct snd_pcm_hw_params));
358 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
360 ret = dpcm_be_dai_hw_params(fe, stream);
364 ret = dpcm_be_dai_prepare(fe, stream);
368 ret = snd_soc_dai_compr_set_params(cpu_dai, cstream, params);
372 ret = snd_soc_component_compr_set_params(cstream, params);
376 ret = snd_soc_link_compr_set_params(cstream);
380 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
381 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
384 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
385 mutex_unlock(&fe->card->mutex);
389 static int soc_compr_get_params(struct snd_compr_stream *cstream,
390 struct snd_codec *params)
392 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
393 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
396 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
398 ret = snd_soc_dai_compr_get_params(cpu_dai, cstream, params);
402 ret = snd_soc_component_compr_get_params(cstream, params);
404 mutex_unlock(&rtd->card->pcm_mutex);
408 static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
410 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
411 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
414 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
416 ret = snd_soc_dai_compr_ack(cpu_dai, cstream, bytes);
420 ret = snd_soc_component_compr_ack(cstream, bytes);
422 mutex_unlock(&rtd->card->pcm_mutex);
426 static int soc_compr_pointer(struct snd_compr_stream *cstream,
427 struct snd_compr_tstamp *tstamp)
429 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
431 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
433 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
435 ret = snd_soc_dai_compr_pointer(cpu_dai, cstream, tstamp);
439 ret = snd_soc_component_compr_pointer(cstream, tstamp);
441 mutex_unlock(&rtd->card->pcm_mutex);
445 static int soc_compr_set_metadata(struct snd_compr_stream *cstream,
446 struct snd_compr_metadata *metadata)
448 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
449 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
452 ret = snd_soc_dai_compr_set_metadata(cpu_dai, cstream, metadata);
456 return snd_soc_component_compr_set_metadata(cstream, metadata);
459 static int soc_compr_get_metadata(struct snd_compr_stream *cstream,
460 struct snd_compr_metadata *metadata)
462 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
463 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
466 ret = snd_soc_dai_compr_get_metadata(cpu_dai, cstream, metadata);
470 return snd_soc_component_compr_get_metadata(cstream, metadata);
473 /* ASoC Compress operations */
474 static struct snd_compr_ops soc_compr_ops = {
475 .open = soc_compr_open,
476 .free = soc_compr_free,
477 .set_params = soc_compr_set_params,
478 .set_metadata = soc_compr_set_metadata,
479 .get_metadata = soc_compr_get_metadata,
480 .get_params = soc_compr_get_params,
481 .trigger = soc_compr_trigger,
482 .pointer = soc_compr_pointer,
483 .ack = soc_compr_ack,
484 .get_caps = snd_soc_component_compr_get_caps,
485 .get_codec_caps = snd_soc_component_compr_get_codec_caps,
488 /* ASoC Dynamic Compress operations */
489 static struct snd_compr_ops soc_compr_dyn_ops = {
490 .open = soc_compr_open_fe,
491 .free = soc_compr_free_fe,
492 .set_params = soc_compr_set_params_fe,
493 .get_params = soc_compr_get_params,
494 .set_metadata = soc_compr_set_metadata,
495 .get_metadata = soc_compr_get_metadata,
496 .trigger = soc_compr_trigger_fe,
497 .pointer = soc_compr_pointer,
498 .ack = soc_compr_ack,
499 .get_caps = snd_soc_component_compr_get_caps,
500 .get_codec_caps = snd_soc_component_compr_get_codec_caps,
504 * snd_soc_new_compress - create a new compress.
506 * @rtd: The runtime for which we will create compress
507 * @num: the device index number (zero based - shared with normal PCMs)
509 * Return: 0 for success, else error.
511 int snd_soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
513 struct snd_soc_component *component;
514 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
515 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
516 struct snd_compr *compr;
517 struct snd_pcm *be_pcm;
519 int ret = 0, direction = 0;
520 int playback = 0, capture = 0;
524 * make sure these are same value,
525 * and then use these as equally
527 BUILD_BUG_ON((int)SNDRV_PCM_STREAM_PLAYBACK != (int)SND_COMPRESS_PLAYBACK);
528 BUILD_BUG_ON((int)SNDRV_PCM_STREAM_CAPTURE != (int)SND_COMPRESS_CAPTURE);
530 if (rtd->num_cpus > 1 ||
531 rtd->num_codecs > 1) {
532 dev_err(rtd->card->dev,
533 "Compress ASoC: Multi CPU/Codec not supported\n");
537 /* check client and interface hw capabilities */
538 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
539 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
541 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
542 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
546 * Compress devices are unidirectional so only one of the directions
547 * should be set, check for that (xor)
549 if (playback + capture != 1) {
550 dev_err(rtd->card->dev,
551 "Compress ASoC: Invalid direction for P %d, C %d\n",
557 direction = SND_COMPRESS_PLAYBACK;
559 direction = SND_COMPRESS_CAPTURE;
561 compr = devm_kzalloc(rtd->card->dev, sizeof(*compr), GFP_KERNEL);
565 compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
570 if (rtd->dai_link->dynamic) {
571 snprintf(new_name, sizeof(new_name), "(%s)",
572 rtd->dai_link->stream_name);
574 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
575 rtd->dai_link->dpcm_playback,
576 rtd->dai_link->dpcm_capture, &be_pcm);
578 dev_err(rtd->card->dev,
579 "Compress ASoC: can't create compressed for %s: %d\n",
580 rtd->dai_link->name, ret);
586 if (rtd->dai_link->dpcm_playback)
587 be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
588 else if (rtd->dai_link->dpcm_capture)
589 be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
590 memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
592 snprintf(new_name, sizeof(new_name), "%s %s-%d",
593 rtd->dai_link->stream_name, codec_dai->name, num);
595 memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
598 for_each_rtd_components(rtd, i, component) {
599 if (!component->driver->compress_ops ||
600 !component->driver->compress_ops->copy)
603 compr->ops->copy = snd_soc_component_compr_copy;
607 ret = snd_compress_new(rtd->card->snd_card, num, direction,
610 component = asoc_rtd_to_codec(rtd, 0)->component;
611 dev_err(component->dev,
612 "Compress ASoC: can't create compress for codec %s: %d\n",
613 component->name, ret);
617 /* DAPM dai link stream work */
618 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
621 compr->private_data = rtd;
623 dev_dbg(rtd->card->dev, "Compress ASoC: %s <-> %s mapping ok\n",
624 codec_dai->name, cpu_dai->name);
628 EXPORT_SYMBOL_GPL(snd_soc_new_compress);