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 dev_dbg(fe->dev, "Compress ASoC: %s no valid %s route\n",
120 fe->dai_link->name, stream ? "capture" : "playback");
121 /* calculate valid and active FE <-> BE dpcms */
122 dpcm_process_paths(fe, stream, &list, 1);
123 fe->dpcm[stream].runtime = fe_substream->runtime;
125 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
127 ret = dpcm_be_dai_startup(fe, stream);
129 /* clean up all links */
130 for_each_dpcm_be(fe, stream, dpcm)
131 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
133 dpcm_be_disconnect(fe, stream);
134 fe->dpcm[stream].runtime = NULL;
138 ret = snd_soc_dai_compr_startup(cpu_dai, cstream);
142 ret = snd_soc_component_compr_open(cstream);
146 ret = snd_soc_link_compr_startup(cstream);
150 dpcm_clear_pending_state(fe, stream);
151 dpcm_path_put(&list);
153 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
154 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
156 snd_soc_runtime_activate(fe, stream);
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 */
182 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
184 snd_soc_runtime_deactivate(fe, stream);
186 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
188 ret = dpcm_be_dai_hw_free(fe, stream);
190 dev_err(fe->dev, "Compressed ASoC: hw_free failed: %d\n", ret);
192 ret = dpcm_be_dai_shutdown(fe, stream);
194 /* mark FE's links ready to prune */
195 for_each_dpcm_be(fe, stream, dpcm)
196 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
198 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
200 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
201 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
203 dpcm_be_disconnect(fe, stream);
205 fe->dpcm[stream].runtime = NULL;
207 snd_soc_link_compr_shutdown(cstream, 0);
209 snd_soc_component_compr_free(cstream, 0);
211 snd_soc_dai_compr_shutdown(cpu_dai, cstream, 0);
213 mutex_unlock(&fe->card->mutex);
217 static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd)
219 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
220 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
221 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
222 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
225 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
227 ret = snd_soc_component_compr_trigger(cstream, cmd);
231 ret = snd_soc_dai_compr_trigger(cpu_dai, cstream, cmd);
236 case SNDRV_PCM_TRIGGER_START:
237 snd_soc_dai_digital_mute(codec_dai, 0, stream);
239 case SNDRV_PCM_TRIGGER_STOP:
240 snd_soc_dai_digital_mute(codec_dai, 1, stream);
245 mutex_unlock(&rtd->card->pcm_mutex);
249 static int soc_compr_trigger_fe(struct snd_compr_stream *cstream, int cmd)
251 struct snd_soc_pcm_runtime *fe = cstream->private_data;
252 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
253 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
256 if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN ||
257 cmd == SND_COMPR_TRIGGER_DRAIN)
258 return snd_soc_component_compr_trigger(cstream, cmd);
260 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
262 ret = snd_soc_dai_compr_trigger(cpu_dai, cstream, cmd);
266 ret = snd_soc_component_compr_trigger(cstream, cmd);
270 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
272 ret = dpcm_be_dai_trigger(fe, stream, cmd);
275 case SNDRV_PCM_TRIGGER_START:
276 case SNDRV_PCM_TRIGGER_RESUME:
277 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
278 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
280 case SNDRV_PCM_TRIGGER_STOP:
281 case SNDRV_PCM_TRIGGER_SUSPEND:
282 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
284 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
285 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
290 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
291 mutex_unlock(&fe->card->mutex);
295 static int soc_compr_set_params(struct snd_compr_stream *cstream,
296 struct snd_compr_params *params)
298 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
299 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
300 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
303 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
306 * First we call set_params for the CPU DAI, then the component
307 * driver this should configure the SoC side. If the machine has
308 * compressed ops then we call that as well. The expectation is
309 * that these callbacks will configure everything for this compress
310 * path, like configuring a PCM port for a CODEC.
312 ret = snd_soc_dai_compr_set_params(cpu_dai, cstream, params);
316 ret = snd_soc_component_compr_set_params(cstream, params);
320 ret = snd_soc_link_compr_set_params(cstream);
324 snd_soc_dapm_stream_event(rtd, stream, SND_SOC_DAPM_STREAM_START);
326 /* cancel any delayed stream shutdown that is pending */
328 mutex_unlock(&rtd->card->pcm_mutex);
330 cancel_delayed_work_sync(&rtd->delayed_work);
335 mutex_unlock(&rtd->card->pcm_mutex);
339 static int soc_compr_set_params_fe(struct snd_compr_stream *cstream,
340 struct snd_compr_params *params)
342 struct snd_soc_pcm_runtime *fe = cstream->private_data;
343 struct snd_pcm_substream *fe_substream =
344 fe->pcm->streams[cstream->direction].substream;
345 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
346 int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
349 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
352 * Create an empty hw_params for the BE as the machine driver must
353 * fix this up to match DSP decoder and ASRC configuration.
354 * I.e. machine driver fixup for compressed BE is mandatory.
356 memset(&fe->dpcm[fe_substream->stream].hw_params, 0,
357 sizeof(struct snd_pcm_hw_params));
359 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
361 ret = dpcm_be_dai_hw_params(fe, stream);
365 ret = dpcm_be_dai_prepare(fe, stream);
369 ret = snd_soc_dai_compr_set_params(cpu_dai, cstream, params);
373 ret = snd_soc_component_compr_set_params(cstream, params);
377 ret = snd_soc_link_compr_set_params(cstream);
381 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
382 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
385 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
386 mutex_unlock(&fe->card->mutex);
390 static int soc_compr_get_params(struct snd_compr_stream *cstream,
391 struct snd_codec *params)
393 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
394 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
397 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
399 ret = snd_soc_dai_compr_get_params(cpu_dai, cstream, params);
403 ret = snd_soc_component_compr_get_params(cstream, params);
405 mutex_unlock(&rtd->card->pcm_mutex);
409 static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
411 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
412 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
415 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
417 ret = snd_soc_dai_compr_ack(cpu_dai, cstream, bytes);
421 ret = snd_soc_component_compr_ack(cstream, bytes);
423 mutex_unlock(&rtd->card->pcm_mutex);
427 static int soc_compr_pointer(struct snd_compr_stream *cstream,
428 struct snd_compr_tstamp *tstamp)
430 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
432 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
434 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
436 ret = snd_soc_dai_compr_pointer(cpu_dai, cstream, tstamp);
440 ret = snd_soc_component_compr_pointer(cstream, tstamp);
442 mutex_unlock(&rtd->card->pcm_mutex);
446 static int soc_compr_set_metadata(struct snd_compr_stream *cstream,
447 struct snd_compr_metadata *metadata)
449 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
450 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
453 ret = snd_soc_dai_compr_set_metadata(cpu_dai, cstream, metadata);
457 return snd_soc_component_compr_set_metadata(cstream, metadata);
460 static int soc_compr_get_metadata(struct snd_compr_stream *cstream,
461 struct snd_compr_metadata *metadata)
463 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
464 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
467 ret = snd_soc_dai_compr_get_metadata(cpu_dai, cstream, metadata);
471 return snd_soc_component_compr_get_metadata(cstream, metadata);
474 /* ASoC Compress operations */
475 static struct snd_compr_ops soc_compr_ops = {
476 .open = soc_compr_open,
477 .free = soc_compr_free,
478 .set_params = soc_compr_set_params,
479 .set_metadata = soc_compr_set_metadata,
480 .get_metadata = soc_compr_get_metadata,
481 .get_params = soc_compr_get_params,
482 .trigger = soc_compr_trigger,
483 .pointer = soc_compr_pointer,
484 .ack = soc_compr_ack,
485 .get_caps = snd_soc_component_compr_get_caps,
486 .get_codec_caps = snd_soc_component_compr_get_codec_caps,
489 /* ASoC Dynamic Compress operations */
490 static struct snd_compr_ops soc_compr_dyn_ops = {
491 .open = soc_compr_open_fe,
492 .free = soc_compr_free_fe,
493 .set_params = soc_compr_set_params_fe,
494 .get_params = soc_compr_get_params,
495 .set_metadata = soc_compr_set_metadata,
496 .get_metadata = soc_compr_get_metadata,
497 .trigger = soc_compr_trigger_fe,
498 .pointer = soc_compr_pointer,
499 .ack = soc_compr_ack,
500 .get_caps = snd_soc_component_compr_get_caps,
501 .get_codec_caps = snd_soc_component_compr_get_codec_caps,
505 * snd_soc_new_compress - create a new compress.
507 * @rtd: The runtime for which we will create compress
508 * @num: the device index number (zero based - shared with normal PCMs)
510 * Return: 0 for success, else error.
512 int snd_soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
514 struct snd_soc_component *component;
515 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
516 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
517 struct snd_compr *compr;
518 struct snd_pcm *be_pcm;
520 int ret = 0, direction = 0;
521 int playback = 0, capture = 0;
525 * make sure these are same value,
526 * and then use these as equally
528 BUILD_BUG_ON((int)SNDRV_PCM_STREAM_PLAYBACK != (int)SND_COMPRESS_PLAYBACK);
529 BUILD_BUG_ON((int)SNDRV_PCM_STREAM_CAPTURE != (int)SND_COMPRESS_CAPTURE);
531 if (rtd->num_cpus > 1 ||
532 rtd->num_codecs > 1) {
533 dev_err(rtd->card->dev,
534 "Compress ASoC: Multi CPU/Codec not supported\n");
538 /* check client and interface hw capabilities */
539 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
540 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
542 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
543 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
547 * Compress devices are unidirectional so only one of the directions
548 * should be set, check for that (xor)
550 if (playback + capture != 1) {
551 dev_err(rtd->card->dev,
552 "Compress ASoC: Invalid direction for P %d, C %d\n",
558 direction = SND_COMPRESS_PLAYBACK;
560 direction = SND_COMPRESS_CAPTURE;
562 compr = devm_kzalloc(rtd->card->dev, sizeof(*compr), GFP_KERNEL);
566 compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
571 if (rtd->dai_link->dynamic) {
572 snprintf(new_name, sizeof(new_name), "(%s)",
573 rtd->dai_link->stream_name);
575 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
576 rtd->dai_link->dpcm_playback,
577 rtd->dai_link->dpcm_capture, &be_pcm);
579 dev_err(rtd->card->dev,
580 "Compress ASoC: can't create compressed for %s: %d\n",
581 rtd->dai_link->name, ret);
587 if (rtd->dai_link->dpcm_playback)
588 be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
589 else if (rtd->dai_link->dpcm_capture)
590 be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
591 memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
593 snprintf(new_name, sizeof(new_name), "%s %s-%d",
594 rtd->dai_link->stream_name, codec_dai->name, num);
596 memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
599 for_each_rtd_components(rtd, i, component) {
600 if (!component->driver->compress_ops ||
601 !component->driver->compress_ops->copy)
604 compr->ops->copy = snd_soc_component_compr_copy;
608 mutex_init(&compr->lock);
609 ret = snd_compress_new(rtd->card->snd_card, num, direction,
612 component = asoc_rtd_to_codec(rtd, 0)->component;
613 dev_err(component->dev,
614 "Compress ASoC: can't create compress for codec %s: %d\n",
615 component->name, ret);
619 /* DAPM dai link stream work */
620 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
623 compr->private_data = rtd;
625 dev_dbg(rtd->card->dev, "Compress ASoC: %s <-> %s mapping ok\n",
626 codec_dai->name, cpu_dai->name);
630 EXPORT_SYMBOL_GPL(snd_soc_new_compress);