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_ack(struct snd_compr_stream *cstream, size_t bytes)
414 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
415 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
418 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
420 ret = snd_soc_dai_compr_ack(cpu_dai, cstream, bytes);
424 ret = snd_soc_component_compr_ack(cstream, bytes);
426 mutex_unlock(&rtd->card->pcm_mutex);
430 static int soc_compr_pointer(struct snd_compr_stream *cstream,
431 struct snd_compr_tstamp *tstamp)
433 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
435 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
437 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
439 ret = snd_soc_dai_compr_pointer(cpu_dai, cstream, tstamp);
443 ret = snd_soc_component_compr_pointer(cstream, tstamp);
445 mutex_unlock(&rtd->card->pcm_mutex);
449 static int soc_compr_set_metadata(struct snd_compr_stream *cstream,
450 struct snd_compr_metadata *metadata)
452 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
453 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
456 ret = snd_soc_dai_compr_set_metadata(cpu_dai, cstream, metadata);
460 return snd_soc_component_compr_set_metadata(cstream, metadata);
463 static int soc_compr_get_metadata(struct snd_compr_stream *cstream,
464 struct snd_compr_metadata *metadata)
466 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
467 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
470 ret = snd_soc_dai_compr_get_metadata(cpu_dai, cstream, metadata);
474 return snd_soc_component_compr_get_metadata(cstream, metadata);
477 /* ASoC Compress operations */
478 static struct snd_compr_ops soc_compr_ops = {
479 .open = soc_compr_open,
480 .free = soc_compr_free,
481 .set_params = soc_compr_set_params,
482 .set_metadata = soc_compr_set_metadata,
483 .get_metadata = soc_compr_get_metadata,
484 .get_params = soc_compr_get_params,
485 .trigger = soc_compr_trigger,
486 .pointer = soc_compr_pointer,
487 .ack = soc_compr_ack,
488 .get_caps = snd_soc_component_compr_get_caps,
489 .get_codec_caps = snd_soc_component_compr_get_codec_caps,
492 /* ASoC Dynamic Compress operations */
493 static struct snd_compr_ops soc_compr_dyn_ops = {
494 .open = soc_compr_open_fe,
495 .free = soc_compr_free_fe,
496 .set_params = soc_compr_set_params_fe,
497 .get_params = soc_compr_get_params,
498 .set_metadata = soc_compr_set_metadata,
499 .get_metadata = soc_compr_get_metadata,
500 .trigger = soc_compr_trigger_fe,
501 .pointer = soc_compr_pointer,
502 .ack = soc_compr_ack,
503 .get_caps = snd_soc_component_compr_get_caps,
504 .get_codec_caps = snd_soc_component_compr_get_codec_caps,
508 * snd_soc_new_compress - create a new compress.
510 * @rtd: The runtime for which we will create compress
511 * @num: the device index number (zero based - shared with normal PCMs)
513 * Return: 0 for success, else error.
515 int snd_soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
517 struct snd_soc_component *component;
518 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
519 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
520 struct snd_compr *compr;
521 struct snd_pcm *be_pcm;
523 int ret = 0, direction = 0;
524 int playback = 0, capture = 0;
528 * make sure these are same value,
529 * and then use these as equally
531 BUILD_BUG_ON((int)SNDRV_PCM_STREAM_PLAYBACK != (int)SND_COMPRESS_PLAYBACK);
532 BUILD_BUG_ON((int)SNDRV_PCM_STREAM_CAPTURE != (int)SND_COMPRESS_CAPTURE);
534 if (rtd->num_cpus > 1 ||
535 rtd->num_codecs > 1) {
536 dev_err(rtd->card->dev,
537 "Compress ASoC: Multi CPU/Codec not supported\n");
541 /* check client and interface hw capabilities */
542 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
543 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
545 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
546 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
550 * Compress devices are unidirectional so only one of the directions
551 * should be set, check for that (xor)
553 if (playback + capture != 1) {
554 dev_err(rtd->card->dev,
555 "Compress ASoC: Invalid direction for P %d, C %d\n",
561 direction = SND_COMPRESS_PLAYBACK;
563 direction = SND_COMPRESS_CAPTURE;
565 compr = devm_kzalloc(rtd->card->dev, sizeof(*compr), GFP_KERNEL);
569 compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
574 if (rtd->dai_link->dynamic) {
575 snprintf(new_name, sizeof(new_name), "(%s)",
576 rtd->dai_link->stream_name);
578 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
579 rtd->dai_link->dpcm_playback,
580 rtd->dai_link->dpcm_capture, &be_pcm);
582 dev_err(rtd->card->dev,
583 "Compress ASoC: can't create compressed for %s: %d\n",
584 rtd->dai_link->name, ret);
590 if (rtd->dai_link->dpcm_playback)
591 be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
592 else if (rtd->dai_link->dpcm_capture)
593 be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
594 memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
596 snprintf(new_name, sizeof(new_name), "%s %s-%d",
597 rtd->dai_link->stream_name, codec_dai->name, num);
599 memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
602 for_each_rtd_components(rtd, i, component) {
603 if (!component->driver->compress_ops ||
604 !component->driver->compress_ops->copy)
607 compr->ops->copy = snd_soc_component_compr_copy;
611 mutex_init(&compr->lock);
612 ret = snd_compress_new(rtd->card->snd_card, num, direction,
615 component = asoc_rtd_to_codec(rtd, 0)->component;
616 dev_err(component->dev,
617 "Compress ASoC: can't create compress for codec %s: %d\n",
618 component->name, ret);
622 /* DAPM dai link stream work */
623 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
626 compr->private_data = rtd;
628 dev_dbg(rtd->card->dev, "Compress ASoC: %s <-> %s mapping ok\n",
629 codec_dai->name, cpu_dai->name);
633 EXPORT_SYMBOL_GPL(snd_soc_new_compress);