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_free(struct snd_compr_stream *cstream)
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);
34 snd_soc_runtime_deactivate(rtd, stream);
36 snd_soc_dai_digital_mute(codec_dai, 1, stream);
38 if (!snd_soc_dai_active(cpu_dai))
41 if (!snd_soc_dai_active(codec_dai))
44 snd_soc_link_compr_shutdown(cstream, 0);
46 snd_soc_component_compr_free(cstream, 0);
48 snd_soc_dai_compr_shutdown(cpu_dai, cstream, 0);
50 snd_soc_dapm_stream_stop(rtd, stream);
52 mutex_unlock(&rtd->card->pcm_mutex);
54 snd_soc_pcm_component_pm_runtime_put(rtd, cstream, 0);
59 static int soc_compr_open(struct snd_compr_stream *cstream)
61 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
62 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
63 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
66 ret = snd_soc_pcm_component_pm_runtime_get(rtd, cstream);
70 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
72 ret = snd_soc_dai_compr_startup(cpu_dai, cstream);
76 ret = snd_soc_component_compr_open(cstream);
80 ret = snd_soc_link_compr_startup(cstream);
84 snd_soc_runtime_activate(rtd, stream);
86 mutex_unlock(&rtd->card->pcm_mutex);
91 snd_soc_component_compr_free(cstream, 1);
93 snd_soc_dai_compr_shutdown(cpu_dai, cstream, 1);
95 mutex_unlock(&rtd->card->pcm_mutex);
97 snd_soc_pcm_component_pm_runtime_put(rtd, cstream, 1);
102 static int soc_compr_open_fe(struct snd_compr_stream *cstream)
104 struct snd_soc_pcm_runtime *fe = cstream->private_data;
105 struct snd_pcm_substream *fe_substream =
106 fe->pcm->streams[cstream->direction].substream;
107 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
108 struct snd_soc_dpcm *dpcm;
109 struct snd_soc_dapm_widget_list *list;
110 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
113 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
114 fe->dpcm[stream].runtime = fe_substream->runtime;
116 ret = dpcm_path_get(fe, stream, &list);
120 dev_dbg(fe->dev, "Compress ASoC: %s no valid %s route\n",
121 fe->dai_link->name, stream ? "capture" : "playback");
122 /* calculate valid and active FE <-> BE dpcms */
123 dpcm_process_paths(fe, stream, &list, 1);
124 fe->dpcm[stream].runtime = fe_substream->runtime;
126 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
128 ret = dpcm_be_dai_startup(fe, stream);
130 /* clean up all links */
131 for_each_dpcm_be(fe, stream, dpcm)
132 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
134 dpcm_be_disconnect(fe, stream);
135 fe->dpcm[stream].runtime = NULL;
139 ret = snd_soc_dai_compr_startup(cpu_dai, cstream);
143 ret = snd_soc_component_compr_open(cstream);
147 ret = snd_soc_link_compr_startup(cstream);
151 dpcm_clear_pending_state(fe, stream);
152 dpcm_path_put(&list);
154 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
155 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
157 snd_soc_runtime_activate(fe, stream);
159 mutex_unlock(&fe->card->mutex);
164 snd_soc_component_compr_free(cstream, 1);
166 snd_soc_dai_compr_shutdown(cpu_dai, cstream, 1);
168 dpcm_path_put(&list);
170 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
171 mutex_unlock(&fe->card->mutex);
175 static int soc_compr_free_fe(struct snd_compr_stream *cstream)
177 struct snd_soc_pcm_runtime *fe = cstream->private_data;
178 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
179 struct snd_soc_dpcm *dpcm;
180 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
183 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
185 snd_soc_runtime_deactivate(fe, stream);
187 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
189 ret = dpcm_be_dai_hw_free(fe, stream);
191 dev_err(fe->dev, "Compressed ASoC: hw_free failed: %d\n", ret);
193 ret = dpcm_be_dai_shutdown(fe, stream);
195 /* mark FE's links ready to prune */
196 for_each_dpcm_be(fe, stream, dpcm)
197 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
199 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
201 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
202 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
204 dpcm_be_disconnect(fe, stream);
206 fe->dpcm[stream].runtime = NULL;
208 snd_soc_link_compr_shutdown(cstream, 0);
210 snd_soc_component_compr_free(cstream, 0);
212 snd_soc_dai_compr_shutdown(cpu_dai, cstream, 0);
214 mutex_unlock(&fe->card->mutex);
218 static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd)
220 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
221 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
222 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
223 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
226 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
228 ret = snd_soc_component_compr_trigger(cstream, cmd);
232 ret = snd_soc_dai_compr_trigger(cpu_dai, cstream, cmd);
237 case SNDRV_PCM_TRIGGER_START:
238 snd_soc_dai_digital_mute(codec_dai, 0, stream);
240 case SNDRV_PCM_TRIGGER_STOP:
241 snd_soc_dai_digital_mute(codec_dai, 1, stream);
246 mutex_unlock(&rtd->card->pcm_mutex);
250 static int soc_compr_trigger_fe(struct snd_compr_stream *cstream, int cmd)
252 struct snd_soc_pcm_runtime *fe = cstream->private_data;
253 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
254 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
257 if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN ||
258 cmd == SND_COMPR_TRIGGER_DRAIN)
259 return snd_soc_component_compr_trigger(cstream, cmd);
261 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
263 ret = snd_soc_dai_compr_trigger(cpu_dai, cstream, cmd);
267 ret = snd_soc_component_compr_trigger(cstream, cmd);
271 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
273 ret = dpcm_be_dai_trigger(fe, stream, cmd);
276 case SNDRV_PCM_TRIGGER_START:
277 case SNDRV_PCM_TRIGGER_RESUME:
278 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
279 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
281 case SNDRV_PCM_TRIGGER_STOP:
282 case SNDRV_PCM_TRIGGER_SUSPEND:
283 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
285 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
286 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
291 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
292 mutex_unlock(&fe->card->mutex);
296 static int soc_compr_set_params(struct snd_compr_stream *cstream,
297 struct snd_compr_params *params)
299 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
300 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
301 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
304 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
307 * First we call set_params for the CPU DAI, then the component
308 * driver this should configure the SoC side. If the machine has
309 * compressed ops then we call that as well. The expectation is
310 * that these callbacks will configure everything for this compress
311 * path, like configuring a PCM port for a CODEC.
313 ret = snd_soc_dai_compr_set_params(cpu_dai, cstream, params);
317 ret = snd_soc_component_compr_set_params(cstream, params);
321 ret = snd_soc_link_compr_set_params(cstream);
325 snd_soc_dapm_stream_event(rtd, stream, SND_SOC_DAPM_STREAM_START);
327 /* cancel any delayed stream shutdown that is pending */
329 mutex_unlock(&rtd->card->pcm_mutex);
331 cancel_delayed_work_sync(&rtd->delayed_work);
336 mutex_unlock(&rtd->card->pcm_mutex);
340 static int soc_compr_set_params_fe(struct snd_compr_stream *cstream,
341 struct snd_compr_params *params)
343 struct snd_soc_pcm_runtime *fe = cstream->private_data;
344 struct snd_pcm_substream *fe_substream =
345 fe->pcm->streams[cstream->direction].substream;
346 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
347 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
350 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
353 * Create an empty hw_params for the BE as the machine driver must
354 * fix this up to match DSP decoder and ASRC configuration.
355 * I.e. machine driver fixup for compressed BE is mandatory.
357 memset(&fe->dpcm[fe_substream->stream].hw_params, 0,
358 sizeof(struct snd_pcm_hw_params));
360 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
362 ret = dpcm_be_dai_hw_params(fe, stream);
366 ret = dpcm_be_dai_prepare(fe, stream);
370 ret = snd_soc_dai_compr_set_params(cpu_dai, cstream, params);
374 ret = snd_soc_component_compr_set_params(cstream, params);
378 ret = snd_soc_link_compr_set_params(cstream);
382 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
383 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
386 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
387 mutex_unlock(&fe->card->mutex);
391 static int soc_compr_get_params(struct snd_compr_stream *cstream,
392 struct snd_codec *params)
394 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
395 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
398 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
400 ret = snd_soc_dai_compr_get_params(cpu_dai, cstream, params);
404 ret = snd_soc_component_compr_get_params(cstream, params);
406 mutex_unlock(&rtd->card->pcm_mutex);
410 static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
412 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
413 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
416 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
418 ret = snd_soc_dai_compr_ack(cpu_dai, cstream, bytes);
422 ret = snd_soc_component_compr_ack(cstream, bytes);
424 mutex_unlock(&rtd->card->pcm_mutex);
428 static int soc_compr_pointer(struct snd_compr_stream *cstream,
429 struct snd_compr_tstamp *tstamp)
431 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
433 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
435 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
437 ret = snd_soc_dai_compr_pointer(cpu_dai, cstream, tstamp);
441 ret = snd_soc_component_compr_pointer(cstream, tstamp);
443 mutex_unlock(&rtd->card->pcm_mutex);
447 static int soc_compr_set_metadata(struct snd_compr_stream *cstream,
448 struct snd_compr_metadata *metadata)
450 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
451 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
454 ret = snd_soc_dai_compr_set_metadata(cpu_dai, cstream, metadata);
458 return snd_soc_component_compr_set_metadata(cstream, metadata);
461 static int soc_compr_get_metadata(struct snd_compr_stream *cstream,
462 struct snd_compr_metadata *metadata)
464 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
465 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
468 ret = snd_soc_dai_compr_get_metadata(cpu_dai, cstream, metadata);
472 return snd_soc_component_compr_get_metadata(cstream, metadata);
475 /* ASoC Compress operations */
476 static struct snd_compr_ops soc_compr_ops = {
477 .open = soc_compr_open,
478 .free = soc_compr_free,
479 .set_params = soc_compr_set_params,
480 .set_metadata = soc_compr_set_metadata,
481 .get_metadata = soc_compr_get_metadata,
482 .get_params = soc_compr_get_params,
483 .trigger = soc_compr_trigger,
484 .pointer = soc_compr_pointer,
485 .ack = soc_compr_ack,
486 .get_caps = snd_soc_component_compr_get_caps,
487 .get_codec_caps = snd_soc_component_compr_get_codec_caps,
490 /* ASoC Dynamic Compress operations */
491 static struct snd_compr_ops soc_compr_dyn_ops = {
492 .open = soc_compr_open_fe,
493 .free = soc_compr_free_fe,
494 .set_params = soc_compr_set_params_fe,
495 .get_params = soc_compr_get_params,
496 .set_metadata = soc_compr_set_metadata,
497 .get_metadata = soc_compr_get_metadata,
498 .trigger = soc_compr_trigger_fe,
499 .pointer = soc_compr_pointer,
500 .ack = soc_compr_ack,
501 .get_caps = snd_soc_component_compr_get_caps,
502 .get_codec_caps = snd_soc_component_compr_get_codec_caps,
506 * snd_soc_new_compress - create a new compress.
508 * @rtd: The runtime for which we will create compress
509 * @num: the device index number (zero based - shared with normal PCMs)
511 * Return: 0 for success, else error.
513 int snd_soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
515 struct snd_soc_component *component;
516 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
517 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
518 struct snd_compr *compr;
519 struct snd_pcm *be_pcm;
521 int ret = 0, direction = 0;
522 int playback = 0, capture = 0;
526 * make sure these are same value,
527 * and then use these as equally
529 BUILD_BUG_ON((int)SNDRV_PCM_STREAM_PLAYBACK != (int)SND_COMPRESS_PLAYBACK);
530 BUILD_BUG_ON((int)SNDRV_PCM_STREAM_CAPTURE != (int)SND_COMPRESS_CAPTURE);
532 if (rtd->num_cpus > 1 ||
533 rtd->num_codecs > 1) {
534 dev_err(rtd->card->dev,
535 "Compress ASoC: Multi CPU/Codec not supported\n");
539 /* check client and interface hw capabilities */
540 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
541 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
543 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
544 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
548 * Compress devices are unidirectional so only one of the directions
549 * should be set, check for that (xor)
551 if (playback + capture != 1) {
552 dev_err(rtd->card->dev,
553 "Compress ASoC: Invalid direction for P %d, C %d\n",
559 direction = SND_COMPRESS_PLAYBACK;
561 direction = SND_COMPRESS_CAPTURE;
563 compr = devm_kzalloc(rtd->card->dev, sizeof(*compr), GFP_KERNEL);
567 compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
572 if (rtd->dai_link->dynamic) {
573 snprintf(new_name, sizeof(new_name), "(%s)",
574 rtd->dai_link->stream_name);
576 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
577 rtd->dai_link->dpcm_playback,
578 rtd->dai_link->dpcm_capture, &be_pcm);
580 dev_err(rtd->card->dev,
581 "Compress ASoC: can't create compressed for %s: %d\n",
582 rtd->dai_link->name, ret);
588 if (rtd->dai_link->dpcm_playback)
589 be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
590 else if (rtd->dai_link->dpcm_capture)
591 be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
592 memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
594 snprintf(new_name, sizeof(new_name), "%s %s-%d",
595 rtd->dai_link->stream_name, codec_dai->name, num);
597 memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
600 for_each_rtd_components(rtd, i, component) {
601 if (!component->driver->compress_ops ||
602 !component->driver->compress_ops->copy)
605 compr->ops->copy = snd_soc_component_compr_copy;
609 mutex_init(&compr->lock);
610 ret = snd_compress_new(rtd->card->snd_card, num, direction,
613 component = asoc_rtd_to_codec(rtd, 0)->component;
614 dev_err(component->dev,
615 "Compress ASoC: can't create compress for codec %s: %d\n",
616 component->name, ret);
620 /* DAPM dai link stream work */
621 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
624 compr->private_data = rtd;
626 dev_dbg(rtd->card->dev, "Compress ASoC: %s <-> %s mapping ok\n",
627 codec_dai->name, cpu_dai->name);
631 EXPORT_SYMBOL_GPL(snd_soc_new_compress);