blob: 3a6a60215e815cf1535828759d86a6667a416712 [file] [log] [blame]
Kuninori Morimotob3ed4c82018-07-02 06:24:57 +00001// SPDX-License-Identifier: GPL-2.0+
2//
3// soc-compress.c -- ALSA SoC Compress
4//
5// Copyright (C) 2012 Intel Corp.
6//
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>
Namarta Kohli1245b702012-08-16 17:10:41 +053010
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>
Liam Girdwood2a99ef02014-01-17 17:03:56 +000021#include <sound/soc-dpcm.h>
Kuninori Morimoto9ab711c2020-05-25 09:57:41 +090022#include <sound/soc-link.h>
Cezary Rojewski4137f4b2019-12-17 10:58:50 +010023#include <linux/pm_runtime.h>
Namarta Kohli1245b702012-08-16 17:10:41 +053024
Charles Keepax1e57b822018-04-24 16:39:03 +010025static int soc_compr_components_open(struct snd_compr_stream *cstream,
26 struct snd_soc_component **last)
Namarta Kohli1245b702012-08-16 17:10:41 +053027{
28 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +000029 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +090030 int i, ret;
Charles Keepax1e57b822018-04-24 16:39:03 +010031
Kuninori Morimoto613fb502020-01-10 11:35:21 +090032 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoc6cb5222020-04-20 16:07:50 +090033 if (!component->driver->compress_ops ||
34 !component->driver->compress_ops->open)
35 continue;
36
37 ret = component->driver->compress_ops->open(component, cstream);
38 if (ret < 0) {
39 dev_err(component->dev,
40 "Compress ASoC: can't open platform %s: %d\n",
41 component->name, ret);
42
43 *last = component;
44 return ret;
45 }
46 }
47
Charles Keepax1e57b822018-04-24 16:39:03 +010048 *last = NULL;
49 return 0;
50}
51
52static int soc_compr_components_free(struct snd_compr_stream *cstream,
53 struct snd_soc_component *last)
54{
55 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
56 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +090057 int i;
Charles Keepax1e57b822018-04-24 16:39:03 +010058
Kuninori Morimoto613fb502020-01-10 11:35:21 +090059 for_each_rtd_components(rtd, i, component) {
Charles Keepax1e57b822018-04-24 16:39:03 +010060 if (component == last)
61 break;
62
Kuninori Morimotoc6cb5222020-04-20 16:07:50 +090063 if (!component->driver->compress_ops ||
64 !component->driver->compress_ops->free)
65 continue;
66
67 component->driver->compress_ops->free(component, cstream);
68 }
69
Charles Keepax1e57b822018-04-24 16:39:03 +010070 return 0;
71}
72
73static int soc_compr_open(struct snd_compr_stream *cstream)
74{
75 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto939a5cf2020-09-28 09:01:17 +090076 struct snd_soc_component *component = NULL;
Kuninori Morimotoc2233a22020-03-30 10:47:37 +090077 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
Kuninori Morimoto939a5cf2020-09-28 09:01:17 +090078 int ret;
Namarta Kohli1245b702012-08-16 17:10:41 +053079
Kuninori Morimoto939a5cf2020-09-28 09:01:17 +090080 ret = snd_soc_pcm_component_pm_runtime_get(rtd, cstream);
81 if (ret < 0)
82 goto pm_err;
Cezary Rojewski4137f4b2019-12-17 10:58:50 +010083
Peter Ujfalusi72b745e2019-08-13 13:45:32 +030084 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Charles Keepax15e2e612013-01-24 09:44:29 +000085
Kuninori Morimotob5ae4cc2020-04-24 08:15:24 +090086 ret = snd_soc_dai_compr_startup(cpu_dai, cstream);
87 if (ret < 0)
88 goto out;
Vinod Koul2e622ae2016-11-13 12:10:02 +053089
Charles Keepax1e57b822018-04-24 16:39:03 +010090 ret = soc_compr_components_open(cstream, &component);
91 if (ret < 0)
92 goto machine_err;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +000093
Kuninori Morimoto9ab711c2020-05-25 09:57:41 +090094 ret = snd_soc_link_compr_startup(cstream);
95 if (ret < 0)
96 goto machine_err;
Namarta Kohli1245b702012-08-16 17:10:41 +053097
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010098 snd_soc_runtime_activate(rtd, cstream->direction);
Namarta Kohli1245b702012-08-16 17:10:41 +053099
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300100 mutex_unlock(&rtd->card->pcm_mutex);
Charles Keepax15e2e612013-01-24 09:44:29 +0000101
Namarta Kohli1245b702012-08-16 17:10:41 +0530102 return 0;
103
104machine_err:
Charles Keepax1e57b822018-04-24 16:39:03 +0100105 soc_compr_components_free(cstream, component);
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000106
Kuninori Morimoto2b25f812020-04-24 08:15:28 +0900107 snd_soc_dai_compr_shutdown(cpu_dai, cstream);
Namarta Kohli1245b702012-08-16 17:10:41 +0530108out:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300109 mutex_unlock(&rtd->card->pcm_mutex);
Cezary Rojewski4137f4b2019-12-17 10:58:50 +0100110pm_err:
Kuninori Morimoto939a5cf2020-09-28 09:01:17 +0900111 snd_soc_pcm_component_pm_runtime_put(rtd, cstream, 1);
Cezary Rojewski4137f4b2019-12-17 10:58:50 +0100112
Namarta Kohli1245b702012-08-16 17:10:41 +0530113 return ret;
114}
115
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000116static int soc_compr_open_fe(struct snd_compr_stream *cstream)
117{
118 struct snd_soc_pcm_runtime *fe = cstream->private_data;
Satish Babu Patakokila01b8ced2017-06-16 17:33:40 -0700119 struct snd_pcm_substream *fe_substream =
120 fe->pcm->streams[cstream->direction].substream;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000121 struct snd_soc_component *component;
Kuninori Morimotoc2233a22020-03-30 10:47:37 +0900122 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000123 struct snd_soc_dpcm *dpcm;
124 struct snd_soc_dapm_widget_list *list;
125 int stream;
Charles Keepax572e6c82018-04-24 16:39:01 +0100126 int ret;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000127
128 if (cstream->direction == SND_COMPRESS_PLAYBACK)
129 stream = SNDRV_PCM_STREAM_PLAYBACK;
130 else
131 stream = SNDRV_PCM_STREAM_CAPTURE;
132
133 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Srinivas Kandagatla0b0722e2018-08-03 13:30:03 +0100134 fe->dpcm[stream].runtime = fe_substream->runtime;
135
136 ret = dpcm_path_get(fe, stream, &list);
137 if (ret < 0)
138 goto be_err;
139 else if (ret == 0)
140 dev_dbg(fe->dev, "Compress ASoC: %s no valid %s route\n",
141 fe->dai_link->name, stream ? "capture" : "playback");
142 /* calculate valid and active FE <-> BE dpcms */
143 dpcm_process_paths(fe, stream, &list, 1);
144 fe->dpcm[stream].runtime = fe_substream->runtime;
145
146 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
147
148 ret = dpcm_be_dai_startup(fe, stream);
149 if (ret < 0) {
150 /* clean up all links */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +0000151 for_each_dpcm_be(fe, stream, dpcm)
Srinivas Kandagatla0b0722e2018-08-03 13:30:03 +0100152 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
153
154 dpcm_be_disconnect(fe, stream);
155 fe->dpcm[stream].runtime = NULL;
156 goto out;
157 }
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000158
Kuninori Morimotob5ae4cc2020-04-24 08:15:24 +0900159 ret = snd_soc_dai_compr_startup(cpu_dai, cstream);
160 if (ret < 0)
161 goto out;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530162
Charles Keepax1e57b822018-04-24 16:39:03 +0100163 ret = soc_compr_components_open(cstream, &component);
164 if (ret < 0)
Srinivas Kandagatla0b0722e2018-08-03 13:30:03 +0100165 goto open_err;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000166
Kuninori Morimoto9ab711c2020-05-25 09:57:41 +0900167 ret = snd_soc_link_compr_startup(cstream);
168 if (ret < 0)
169 goto machine_err;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000170
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000171 dpcm_clear_pending_state(fe, stream);
172 dpcm_path_put(&list);
173
174 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
175 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
176
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100177 snd_soc_runtime_activate(fe, stream);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000178
179 mutex_unlock(&fe->card->mutex);
180
181 return 0;
182
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000183machine_err:
Charles Keepax1e57b822018-04-24 16:39:03 +0100184 soc_compr_components_free(cstream, component);
Srinivas Kandagatla0b0722e2018-08-03 13:30:03 +0100185open_err:
Kuninori Morimoto2b25f812020-04-24 08:15:28 +0900186 snd_soc_dai_compr_shutdown(cpu_dai, cstream);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000187out:
Srinivas Kandagatla0b0722e2018-08-03 13:30:03 +0100188 dpcm_path_put(&list);
189be_err:
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000190 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
191 mutex_unlock(&fe->card->mutex);
192 return ret;
193}
194
Namarta Kohli1245b702012-08-16 17:10:41 +0530195static int soc_compr_free(struct snd_compr_stream *cstream)
196{
197 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimotoc2233a22020-03-30 10:47:37 +0900198 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
199 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
Kuninori Morimoto939a5cf2020-09-28 09:01:17 +0900200 int stream;
Namarta Kohli1245b702012-08-16 17:10:41 +0530201
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300202 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Charles Keepax15e2e612013-01-24 09:44:29 +0000203
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100204 if (cstream->direction == SND_COMPRESS_PLAYBACK)
205 stream = SNDRV_PCM_STREAM_PLAYBACK;
206 else
207 stream = SNDRV_PCM_STREAM_CAPTURE;
208
209 snd_soc_runtime_deactivate(rtd, stream);
Namarta Kohli1245b702012-08-16 17:10:41 +0530210
Mark Brownda183962013-02-06 15:44:07 +0000211 snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
212
Kuninori Morimotob3dea622020-05-15 09:46:51 +0900213 if (!snd_soc_dai_active(cpu_dai))
Namarta Kohli1245b702012-08-16 17:10:41 +0530214 cpu_dai->rate = 0;
215
Kuninori Morimotob3dea622020-05-15 09:46:51 +0900216 if (!snd_soc_dai_active(codec_dai))
Namarta Kohli1245b702012-08-16 17:10:41 +0530217 codec_dai->rate = 0;
218
Kuninori Morimoto0e532c92020-05-25 09:57:45 +0900219 snd_soc_link_compr_shutdown(cstream);
Namarta Kohli1245b702012-08-16 17:10:41 +0530220
Charles Keepax1e57b822018-04-24 16:39:03 +0100221 soc_compr_components_free(cstream, NULL);
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000222
Kuninori Morimoto2b25f812020-04-24 08:15:28 +0900223 snd_soc_dai_compr_shutdown(cpu_dai, cstream);
Vinod Koul2e622ae2016-11-13 12:10:02 +0530224
Kuninori Morimoto3f4cf792020-01-10 11:36:23 +0900225 snd_soc_dapm_stream_stop(rtd, stream);
Namarta Kohli1245b702012-08-16 17:10:41 +0530226
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300227 mutex_unlock(&rtd->card->pcm_mutex);
Cezary Rojewski4137f4b2019-12-17 10:58:50 +0100228
Kuninori Morimoto939a5cf2020-09-28 09:01:17 +0900229 snd_soc_pcm_component_pm_runtime_put(rtd, cstream, 0);
Cezary Rojewski4137f4b2019-12-17 10:58:50 +0100230
Namarta Kohli1245b702012-08-16 17:10:41 +0530231 return 0;
232}
233
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000234static int soc_compr_free_fe(struct snd_compr_stream *cstream)
235{
236 struct snd_soc_pcm_runtime *fe = cstream->private_data;
Kuninori Morimotoc2233a22020-03-30 10:47:37 +0900237 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000238 struct snd_soc_dpcm *dpcm;
239 int stream, ret;
240
241 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
242
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100243 if (cstream->direction == SND_COMPRESS_PLAYBACK)
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000244 stream = SNDRV_PCM_STREAM_PLAYBACK;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100245 else
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000246 stream = SNDRV_PCM_STREAM_CAPTURE;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000247
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100248 snd_soc_runtime_deactivate(fe, stream);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000249
250 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
251
252 ret = dpcm_be_dai_hw_free(fe, stream);
253 if (ret < 0)
Charles Keepax141dfc92018-01-26 13:08:45 +0000254 dev_err(fe->dev, "Compressed ASoC: hw_free failed: %d\n", ret);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000255
256 ret = dpcm_be_dai_shutdown(fe, stream);
257
258 /* mark FE's links ready to prune */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +0000259 for_each_dpcm_be(fe, stream, dpcm)
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000260 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
261
Kuninori Morimoto1c531232020-02-21 10:25:18 +0900262 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000263
264 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
265 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
266
267 dpcm_be_disconnect(fe, stream);
268
269 fe->dpcm[stream].runtime = NULL;
270
Kuninori Morimoto0e532c92020-05-25 09:57:45 +0900271 snd_soc_link_compr_shutdown(cstream);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000272
Charles Keepax1e57b822018-04-24 16:39:03 +0100273 soc_compr_components_free(cstream, NULL);
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000274
Kuninori Morimoto2b25f812020-04-24 08:15:28 +0900275 snd_soc_dai_compr_shutdown(cpu_dai, cstream);
Vinod Koul2e622ae2016-11-13 12:10:02 +0530276
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000277 mutex_unlock(&fe->card->mutex);
278 return 0;
279}
280
Charles Keepax4ef0ecb2019-02-05 11:18:13 +0000281static int soc_compr_components_trigger(struct snd_compr_stream *cstream,
282 int cmd)
Namarta Kohli1245b702012-08-16 17:10:41 +0530283{
Namarta Kohli1245b702012-08-16 17:10:41 +0530284 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000285 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900286 int i, ret;
Charles Keepax15e2e612013-01-24 09:44:29 +0000287
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900288 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoc6cb5222020-04-20 16:07:50 +0900289 if (!component->driver->compress_ops ||
290 !component->driver->compress_ops->trigger)
291 continue;
292
293 ret = component->driver->compress_ops->trigger(
294 component, cstream, cmd);
295 if (ret < 0)
296 return ret;
297 }
298
Charles Keepax4ef0ecb2019-02-05 11:18:13 +0000299 return 0;
300}
301
302static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd)
303{
304 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimotoc2233a22020-03-30 10:47:37 +0900305 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
306 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
Charles Keepax4ef0ecb2019-02-05 11:18:13 +0000307 int ret;
308
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300309 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Charles Keepax4ef0ecb2019-02-05 11:18:13 +0000310
311 ret = soc_compr_components_trigger(cstream, cmd);
312 if (ret < 0)
313 goto out;
314
Kuninori Morimotoeb084112020-04-24 08:15:32 +0900315 ret = snd_soc_dai_compr_trigger(cpu_dai, cstream, cmd);
316 if (ret < 0)
317 goto out;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530318
Mark Brownda183962013-02-06 15:44:07 +0000319 switch (cmd) {
320 case SNDRV_PCM_TRIGGER_START:
321 snd_soc_dai_digital_mute(codec_dai, 0, cstream->direction);
322 break;
323 case SNDRV_PCM_TRIGGER_STOP:
324 snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
325 break;
Mark Browne38b9b72013-02-06 13:52:42 +0000326 }
Namarta Kohli1245b702012-08-16 17:10:41 +0530327
Charles Keepax15e2e612013-01-24 09:44:29 +0000328out:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300329 mutex_unlock(&rtd->card->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530330 return ret;
331}
332
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000333static int soc_compr_trigger_fe(struct snd_compr_stream *cstream, int cmd)
334{
335 struct snd_soc_pcm_runtime *fe = cstream->private_data;
Kuninori Morimotoc2233a22020-03-30 10:47:37 +0900336 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
Charles Keepax52cadf12019-02-05 11:18:12 +0000337 int ret, stream;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000338
339 if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN ||
Charles Keepax4ef0ecb2019-02-05 11:18:13 +0000340 cmd == SND_COMPR_TRIGGER_DRAIN)
341 return soc_compr_components_trigger(cstream, cmd);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000342
343 if (cstream->direction == SND_COMPRESS_PLAYBACK)
344 stream = SNDRV_PCM_STREAM_PLAYBACK;
345 else
346 stream = SNDRV_PCM_STREAM_CAPTURE;
347
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000348 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
349
Kuninori Morimotoeb084112020-04-24 08:15:32 +0900350 ret = snd_soc_dai_compr_trigger(cpu_dai, cstream, cmd);
351 if (ret < 0)
352 goto out;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530353
Charles Keepax4ef0ecb2019-02-05 11:18:13 +0000354 ret = soc_compr_components_trigger(cstream, cmd);
355 if (ret < 0)
356 goto out;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000357
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000358 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
359
360 ret = dpcm_be_dai_trigger(fe, stream, cmd);
361
362 switch (cmd) {
363 case SNDRV_PCM_TRIGGER_START:
364 case SNDRV_PCM_TRIGGER_RESUME:
365 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
366 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
367 break;
368 case SNDRV_PCM_TRIGGER_STOP:
369 case SNDRV_PCM_TRIGGER_SUSPEND:
370 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
371 break;
372 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
373 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
374 break;
375 }
376
377out:
378 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
379 mutex_unlock(&fe->card->mutex);
380 return ret;
381}
382
Charles Keepax4ef0ecb2019-02-05 11:18:13 +0000383static int soc_compr_components_set_params(struct snd_compr_stream *cstream,
384 struct snd_compr_params *params)
Namarta Kohli1245b702012-08-16 17:10:41 +0530385{
386 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000387 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900388 int i, ret;
Charles Keepax4ef0ecb2019-02-05 11:18:13 +0000389
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900390 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoc6cb5222020-04-20 16:07:50 +0900391 if (!component->driver->compress_ops ||
392 !component->driver->compress_ops->set_params)
393 continue;
394
395 ret = component->driver->compress_ops->set_params(
396 component, cstream, params);
397 if (ret < 0)
398 return ret;
399 }
400
Charles Keepax4ef0ecb2019-02-05 11:18:13 +0000401 return 0;
402}
403
404static int soc_compr_set_params(struct snd_compr_stream *cstream,
405 struct snd_compr_params *params)
406{
407 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimotoc2233a22020-03-30 10:47:37 +0900408 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
Charles Keepax52cadf12019-02-05 11:18:12 +0000409 int ret;
Namarta Kohli1245b702012-08-16 17:10:41 +0530410
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300411 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Charles Keepax15e2e612013-01-24 09:44:29 +0000412
Charles Keepaxef050be2018-04-24 16:39:02 +0100413 /*
414 * First we call set_params for the CPU DAI, then the component
415 * driver this should configure the SoC side. If the machine has
416 * compressed ops then we call that as well. The expectation is
417 * that these callbacks will configure everything for this compress
418 * path, like configuring a PCM port for a CODEC.
Namarta Kohli1245b702012-08-16 17:10:41 +0530419 */
Kuninori Morimoto8dfedaf2020-04-24 08:15:36 +0900420 ret = snd_soc_dai_compr_set_params(cpu_dai, cstream, params);
421 if (ret < 0)
422 goto err;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530423
Charles Keepax4ef0ecb2019-02-05 11:18:13 +0000424 ret = soc_compr_components_set_params(cstream, params);
425 if (ret < 0)
426 goto err;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000427
Kuninori Morimotoeab810f2020-05-25 09:57:50 +0900428 ret = snd_soc_link_compr_set_params(cstream);
429 if (ret < 0)
430 goto err;
Namarta Kohli1245b702012-08-16 17:10:41 +0530431
Charles Keepax2c071ed2013-05-20 08:33:54 +0100432 if (cstream->direction == SND_COMPRESS_PLAYBACK)
433 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Charles Keepax89027d92018-04-26 17:30:07 +0100434 SND_SOC_DAPM_STREAM_START);
Charles Keepax2c071ed2013-05-20 08:33:54 +0100435 else
436 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Charles Keepax89027d92018-04-26 17:30:07 +0100437 SND_SOC_DAPM_STREAM_START);
Namarta Kohli1245b702012-08-16 17:10:41 +0530438
Charles Keepaxfa40ef22013-03-27 16:39:01 +0000439 /* cancel any delayed stream shutdown that is pending */
440 rtd->pop_wait = 0;
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300441 mutex_unlock(&rtd->card->pcm_mutex);
Charles Keepaxfa40ef22013-03-27 16:39:01 +0000442
443 cancel_delayed_work_sync(&rtd->delayed_work);
444
Charles Keepax52cadf12019-02-05 11:18:12 +0000445 return 0;
Charles Keepaxfa40ef22013-03-27 16:39:01 +0000446
447err:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300448 mutex_unlock(&rtd->card->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530449 return ret;
450}
451
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000452static int soc_compr_set_params_fe(struct snd_compr_stream *cstream,
Charles Keepax89027d92018-04-26 17:30:07 +0100453 struct snd_compr_params *params)
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000454{
455 struct snd_soc_pcm_runtime *fe = cstream->private_data;
Satish Babu Patakokila01b8ced2017-06-16 17:33:40 -0700456 struct snd_pcm_substream *fe_substream =
457 fe->pcm->streams[cstream->direction].substream;
Kuninori Morimotoc2233a22020-03-30 10:47:37 +0900458 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
Charles Keepax52cadf12019-02-05 11:18:12 +0000459 int ret, stream;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000460
461 if (cstream->direction == SND_COMPRESS_PLAYBACK)
462 stream = SNDRV_PCM_STREAM_PLAYBACK;
463 else
464 stream = SNDRV_PCM_STREAM_CAPTURE;
465
466 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
467
Srinivas Kandagatla0b0722e2018-08-03 13:30:03 +0100468 /*
469 * Create an empty hw_params for the BE as the machine driver must
470 * fix this up to match DSP decoder and ASRC configuration.
471 * I.e. machine driver fixup for compressed BE is mandatory.
472 */
473 memset(&fe->dpcm[fe_substream->stream].hw_params, 0,
474 sizeof(struct snd_pcm_hw_params));
475
476 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
477
478 ret = dpcm_be_dai_hw_params(fe, stream);
479 if (ret < 0)
480 goto out;
481
482 ret = dpcm_be_dai_prepare(fe, stream);
483 if (ret < 0)
484 goto out;
485
Kuninori Morimoto8dfedaf2020-04-24 08:15:36 +0900486 ret = snd_soc_dai_compr_set_params(cpu_dai, cstream, params);
487 if (ret < 0)
488 goto out;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530489
Charles Keepax4ef0ecb2019-02-05 11:18:13 +0000490 ret = soc_compr_components_set_params(cstream, params);
491 if (ret < 0)
492 goto out;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000493
Kuninori Morimotoeab810f2020-05-25 09:57:50 +0900494 ret = snd_soc_link_compr_set_params(cstream);
495 if (ret < 0)
496 goto out;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000497
Daniel Mack15f6b092014-10-19 09:07:35 +0200498 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000499 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
500
501out:
502 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
503 mutex_unlock(&fe->card->mutex);
504 return ret;
505}
506
Namarta Kohli1245b702012-08-16 17:10:41 +0530507static int soc_compr_get_params(struct snd_compr_stream *cstream,
Charles Keepax89027d92018-04-26 17:30:07 +0100508 struct snd_codec *params)
Namarta Kohli1245b702012-08-16 17:10:41 +0530509{
510 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000511 struct snd_soc_component *component;
Kuninori Morimotoc2233a22020-03-30 10:47:37 +0900512 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900513 int i, ret = 0;
Namarta Kohli1245b702012-08-16 17:10:41 +0530514
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300515 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Charles Keepax15e2e612013-01-24 09:44:29 +0000516
Kuninori Morimotoadbef5432020-04-24 08:15:40 +0900517 ret = snd_soc_dai_compr_get_params(cpu_dai, cstream, params);
518 if (ret < 0)
519 goto err;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530520
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900521 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoc6cb5222020-04-20 16:07:50 +0900522 if (!component->driver->compress_ops ||
523 !component->driver->compress_ops->get_params)
524 continue;
525
526 ret = component->driver->compress_ops->get_params(
527 component, cstream, params);
528 break;
529 }
530
Vinod Koul2e622ae2016-11-13 12:10:02 +0530531err:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300532 mutex_unlock(&rtd->card->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530533 return ret;
534}
535
536static int soc_compr_get_caps(struct snd_compr_stream *cstream,
Charles Keepax89027d92018-04-26 17:30:07 +0100537 struct snd_compr_caps *caps)
Namarta Kohli1245b702012-08-16 17:10:41 +0530538{
539 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000540 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900541 int i, ret = 0;
Namarta Kohli1245b702012-08-16 17:10:41 +0530542
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300543 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Charles Keepax15e2e612013-01-24 09:44:29 +0000544
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900545 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoc6cb5222020-04-20 16:07:50 +0900546 if (!component->driver->compress_ops ||
547 !component->driver->compress_ops->get_caps)
548 continue;
549
550 ret = component->driver->compress_ops->get_caps(
551 component, cstream, caps);
552 break;
553 }
554
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300555 mutex_unlock(&rtd->card->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530556 return ret;
557}
558
559static int soc_compr_get_codec_caps(struct snd_compr_stream *cstream,
Charles Keepax89027d92018-04-26 17:30:07 +0100560 struct snd_compr_codec_caps *codec)
Namarta Kohli1245b702012-08-16 17:10:41 +0530561{
562 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000563 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900564 int i, ret = 0;
Namarta Kohli1245b702012-08-16 17:10:41 +0530565
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300566 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Charles Keepax15e2e612013-01-24 09:44:29 +0000567
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900568 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoc6cb5222020-04-20 16:07:50 +0900569 if (!component->driver->compress_ops ||
570 !component->driver->compress_ops->get_codec_caps)
571 continue;
572
573 ret = component->driver->compress_ops->get_codec_caps(
574 component, cstream, codec);
575 break;
576 }
577
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300578 mutex_unlock(&rtd->card->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530579 return ret;
580}
581
582static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
583{
584 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000585 struct snd_soc_component *component;
Kuninori Morimotoc2233a22020-03-30 10:47:37 +0900586 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900587 int i, ret = 0;
Namarta Kohli1245b702012-08-16 17:10:41 +0530588
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300589 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Charles Keepax15e2e612013-01-24 09:44:29 +0000590
Kuninori Morimoto53294352020-04-24 08:15:45 +0900591 ret = snd_soc_dai_compr_ack(cpu_dai, cstream, bytes);
592 if (ret < 0)
593 goto err;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530594
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900595 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoc6cb5222020-04-20 16:07:50 +0900596 if (!component->driver->compress_ops ||
597 !component->driver->compress_ops->ack)
598 continue;
599
600 ret = component->driver->compress_ops->ack(
601 component, cstream, bytes);
602 if (ret < 0)
603 goto err;
604 }
605
Vinod Koul2e622ae2016-11-13 12:10:02 +0530606err:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300607 mutex_unlock(&rtd->card->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530608 return ret;
609}
610
611static int soc_compr_pointer(struct snd_compr_stream *cstream,
Charles Keepax89027d92018-04-26 17:30:07 +0100612 struct snd_compr_tstamp *tstamp)
Namarta Kohli1245b702012-08-16 17:10:41 +0530613{
614 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000615 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900616 int i, ret = 0;
Kuninori Morimotoc2233a22020-03-30 10:47:37 +0900617 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
Namarta Kohli1245b702012-08-16 17:10:41 +0530618
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300619 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Charles Keepax15e2e612013-01-24 09:44:29 +0000620
Kuninori Morimotoed38cc52020-04-24 08:15:49 +0900621 ret = snd_soc_dai_compr_pointer(cpu_dai, cstream, tstamp);
622 if (ret < 0)
623 goto out;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530624
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900625 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoc6cb5222020-04-20 16:07:50 +0900626 if (!component->driver->compress_ops ||
627 !component->driver->compress_ops->pointer)
628 continue;
629
630 ret = component->driver->compress_ops->pointer(
631 component, cstream, tstamp);
632 break;
633 }
Kuninori Morimotoed38cc52020-04-24 08:15:49 +0900634out:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300635 mutex_unlock(&rtd->card->pcm_mutex);
Charles Keepax7c9190f2016-06-20 09:51:32 +0100636 return ret;
Namarta Kohli1245b702012-08-16 17:10:41 +0530637}
638
Charles Keepax1f88eb02013-02-05 10:41:47 +0000639static int soc_compr_copy(struct snd_compr_stream *cstream,
Charles Keepax4daf8912013-04-18 11:01:38 +0100640 char __user *buf, size_t count)
Charles Keepax1f88eb02013-02-05 10:41:47 +0000641{
642 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000643 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900644 int i, ret = 0;
Charles Keepax1f88eb02013-02-05 10:41:47 +0000645
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300646 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Charles Keepax1f88eb02013-02-05 10:41:47 +0000647
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900648 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoc6cb5222020-04-20 16:07:50 +0900649 if (!component->driver->compress_ops ||
650 !component->driver->compress_ops->copy)
651 continue;
652
653 ret = component->driver->compress_ops->copy(
654 component, cstream, buf, count);
655 break;
656 }
657
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300658 mutex_unlock(&rtd->card->pcm_mutex);
Charles Keepax1f88eb02013-02-05 10:41:47 +0000659 return ret;
660}
661
Vinod Koul02bd90e2013-07-28 20:06:15 +0530662static int soc_compr_set_metadata(struct snd_compr_stream *cstream,
Charles Keepax89027d92018-04-26 17:30:07 +0100663 struct snd_compr_metadata *metadata)
Jeeja KP36953d92013-03-26 21:22:28 +0530664{
665 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000666 struct snd_soc_component *component;
Kuninori Morimotoc2233a22020-03-30 10:47:37 +0900667 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900668 int i, ret;
Jeeja KP36953d92013-03-26 21:22:28 +0530669
Kuninori Morimoto88b3a7d2020-04-24 08:15:54 +0900670 ret = snd_soc_dai_compr_set_metadata(cpu_dai, cstream, metadata);
671 if (ret < 0)
672 return ret;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530673
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900674 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoc6cb5222020-04-20 16:07:50 +0900675 if (!component->driver->compress_ops ||
676 !component->driver->compress_ops->set_metadata)
677 continue;
678
679 ret = component->driver->compress_ops->set_metadata(
680 component, cstream, metadata);
681 if (ret < 0)
682 return ret;
683 }
684
Charles Keepax52cadf12019-02-05 11:18:12 +0000685 return 0;
Jeeja KP36953d92013-03-26 21:22:28 +0530686}
687
Vinod Koul02bd90e2013-07-28 20:06:15 +0530688static int soc_compr_get_metadata(struct snd_compr_stream *cstream,
Charles Keepax89027d92018-04-26 17:30:07 +0100689 struct snd_compr_metadata *metadata)
Jeeja KP36953d92013-03-26 21:22:28 +0530690{
691 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000692 struct snd_soc_component *component;
Kuninori Morimotoc2233a22020-03-30 10:47:37 +0900693 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900694 int i, ret;
Jeeja KP36953d92013-03-26 21:22:28 +0530695
Kuninori Morimoto94d72812020-04-24 08:15:59 +0900696 ret = snd_soc_dai_compr_get_metadata(cpu_dai, cstream, metadata);
697 if (ret < 0)
698 return ret;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530699
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900700 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoc6cb5222020-04-20 16:07:50 +0900701 if (!component->driver->compress_ops ||
702 !component->driver->compress_ops->get_metadata)
703 continue;
704
705 return component->driver->compress_ops->get_metadata(
706 component, cstream, metadata);
707 }
708
Charles Keepax52cadf12019-02-05 11:18:12 +0000709 return 0;
Jeeja KP36953d92013-03-26 21:22:28 +0530710}
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000711
Namarta Kohli1245b702012-08-16 17:10:41 +0530712/* ASoC Compress operations */
713static struct snd_compr_ops soc_compr_ops = {
714 .open = soc_compr_open,
715 .free = soc_compr_free,
716 .set_params = soc_compr_set_params,
Vinod Koul02bd90e2013-07-28 20:06:15 +0530717 .set_metadata = soc_compr_set_metadata,
718 .get_metadata = soc_compr_get_metadata,
Namarta Kohli1245b702012-08-16 17:10:41 +0530719 .get_params = soc_compr_get_params,
720 .trigger = soc_compr_trigger,
721 .pointer = soc_compr_pointer,
722 .ack = soc_compr_ack,
723 .get_caps = soc_compr_get_caps,
724 .get_codec_caps = soc_compr_get_codec_caps
725};
726
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000727/* ASoC Dynamic Compress operations */
728static struct snd_compr_ops soc_compr_dyn_ops = {
729 .open = soc_compr_open_fe,
730 .free = soc_compr_free_fe,
731 .set_params = soc_compr_set_params_fe,
732 .get_params = soc_compr_get_params,
733 .set_metadata = soc_compr_set_metadata,
734 .get_metadata = soc_compr_get_metadata,
735 .trigger = soc_compr_trigger_fe,
736 .pointer = soc_compr_pointer,
737 .ack = soc_compr_ack,
738 .get_caps = soc_compr_get_caps,
739 .get_codec_caps = soc_compr_get_codec_caps
740};
741
Jie Yang6f0c4222015-10-13 23:41:00 +0800742/**
743 * snd_soc_new_compress - create a new compress.
744 *
745 * @rtd: The runtime for which we will create compress
746 * @num: the device index number (zero based - shared with normal PCMs)
747 *
748 * Return: 0 for success, else error.
749 */
750int snd_soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
Namarta Kohli1245b702012-08-16 17:10:41 +0530751{
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000752 struct snd_soc_component *component;
Kuninori Morimotoc2233a22020-03-30 10:47:37 +0900753 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
754 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
Namarta Kohli1245b702012-08-16 17:10:41 +0530755 struct snd_compr *compr;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000756 struct snd_pcm *be_pcm;
Namarta Kohli1245b702012-08-16 17:10:41 +0530757 char new_name[64];
758 int ret = 0, direction = 0;
Vinod Koula1068042016-01-07 21:48:14 +0530759 int playback = 0, capture = 0;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900760 int i;
Namarta Kohli1245b702012-08-16 17:10:41 +0530761
Bard Liao6e1276a2020-02-25 21:39:16 +0800762 if (rtd->num_cpus > 1 ||
763 rtd->num_codecs > 1) {
Charles Keepax141dfc92018-01-26 13:08:45 +0000764 dev_err(rtd->card->dev,
Bard Liao6e1276a2020-02-25 21:39:16 +0800765 "Compress ASoC: Multi CPU/Codec not supported\n");
Benoit Cousson8151d5e2014-07-08 23:19:37 +0200766 return -EINVAL;
767 }
768
Namarta Kohli1245b702012-08-16 17:10:41 +0530769 /* check client and interface hw capabilities */
Kuninori Morimoto467fece2019-07-22 10:36:16 +0900770 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
771 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
Vinod Koula1068042016-01-07 21:48:14 +0530772 playback = 1;
Kuninori Morimoto467fece2019-07-22 10:36:16 +0900773 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
774 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
Vinod Koula1068042016-01-07 21:48:14 +0530775 capture = 1;
776
Vinod Koula1068042016-01-07 21:48:14 +0530777 /*
778 * Compress devices are unidirectional so only one of the directions
779 * should be set, check for that (xor)
780 */
781 if (playback + capture != 1) {
Charles Keepax141dfc92018-01-26 13:08:45 +0000782 dev_err(rtd->card->dev,
783 "Compress ASoC: Invalid direction for P %d, C %d\n",
784 playback, capture);
Charles Keepaxdaa2db52013-04-18 11:02:38 +0100785 return -EINVAL;
Vinod Koula1068042016-01-07 21:48:14 +0530786 }
787
Peng Donglinaeb6fa02017-08-16 22:47:53 +0800788 if (playback)
Vinod Koula1068042016-01-07 21:48:14 +0530789 direction = SND_COMPRESS_PLAYBACK;
790 else
791 direction = SND_COMPRESS_CAPTURE;
Charles Keepaxdaa2db52013-04-18 11:02:38 +0100792
Amadeusz Sławiński09f448a2019-06-17 13:36:36 +0200793 compr = devm_kzalloc(rtd->card->dev, sizeof(*compr), GFP_KERNEL);
Markus Elfring7a0cf422017-08-10 16:21:34 +0200794 if (!compr)
Namarta Kohli1245b702012-08-16 17:10:41 +0530795 return -ENOMEM;
Namarta Kohli1245b702012-08-16 17:10:41 +0530796
Charles Keepax1f88eb02013-02-05 10:41:47 +0000797 compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
798 GFP_KERNEL);
Amadeusz Sławiński09f448a2019-06-17 13:36:36 +0200799 if (!compr->ops)
800 return -ENOMEM;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000801
802 if (rtd->dai_link->dynamic) {
803 snprintf(new_name, sizeof(new_name), "(%s)",
804 rtd->dai_link->stream_name);
805
806 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
Qais Yousefd3268a42015-01-14 08:47:29 +0000807 rtd->dai_link->dpcm_playback,
808 rtd->dai_link->dpcm_capture, &be_pcm);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000809 if (ret < 0) {
Charles Keepax141dfc92018-01-26 13:08:45 +0000810 dev_err(rtd->card->dev,
811 "Compress ASoC: can't create compressed for %s: %d\n",
812 rtd->dai_link->name, ret);
Amadeusz Sławiński09f448a2019-06-17 13:36:36 +0200813 return ret;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000814 }
815
816 rtd->pcm = be_pcm;
817 rtd->fe_compr = 1;
Qais Yousefd3268a42015-01-14 08:47:29 +0000818 if (rtd->dai_link->dpcm_playback)
819 be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
820 else if (rtd->dai_link->dpcm_capture)
821 be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000822 memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
Peng Donglinaeb6fa02017-08-16 22:47:53 +0800823 } else {
824 snprintf(new_name, sizeof(new_name), "%s %s-%d",
825 rtd->dai_link->stream_name, codec_dai->name, num);
826
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000827 memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
Peng Donglinaeb6fa02017-08-16 22:47:53 +0800828 }
Charles Keepax1f88eb02013-02-05 10:41:47 +0000829
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900830 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoc6cb5222020-04-20 16:07:50 +0900831 if (!component->driver->compress_ops ||
832 !component->driver->compress_ops->copy)
833 continue;
834
835 compr->ops->copy = soc_compr_copy;
836 break;
837 }
838
Namarta Kohli1245b702012-08-16 17:10:41 +0530839 mutex_init(&compr->lock);
Richard Fitzgeralde5241a82015-11-25 13:00:24 +0000840 ret = snd_compress_new(rtd->card->snd_card, num, direction,
841 new_name, compr);
Namarta Kohli1245b702012-08-16 17:10:41 +0530842 if (ret < 0) {
Kuninori Morimotoc2233a22020-03-30 10:47:37 +0900843 component = asoc_rtd_to_codec(rtd, 0)->component;
Charles Keepax141dfc92018-01-26 13:08:45 +0000844 dev_err(component->dev,
845 "Compress ASoC: can't create compress for codec %s: %d\n",
846 component->name, ret);
Amadeusz Sławiński09f448a2019-06-17 13:36:36 +0200847 return ret;
Namarta Kohli1245b702012-08-16 17:10:41 +0530848 }
849
Charles Keepax202c8f72013-01-24 09:44:30 +0000850 /* DAPM dai link stream work */
Kuninori Morimoto83f94a22020-01-10 11:36:17 +0900851 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
Charles Keepax202c8f72013-01-24 09:44:30 +0000852
Namarta Kohli1245b702012-08-16 17:10:41 +0530853 rtd->compr = compr;
854 compr->private_data = rtd;
855
Pierre-Louis Bossart1d5cd522020-06-12 15:40:50 -0500856 dev_dbg(rtd->card->dev, "Compress ASoC: %s <-> %s mapping ok\n",
857 codec_dai->name, cpu_dai->name);
Charles Keepax1f88eb02013-02-05 10:41:47 +0000858
Amadeusz Sławiński09f448a2019-06-17 13:36:36 +0200859 return 0;
Namarta Kohli1245b702012-08-16 17:10:41 +0530860}
Jie Yang6f0c4222015-10-13 23:41:00 +0800861EXPORT_SYMBOL_GPL(snd_soc_new_compress);