blob: b2a5351b1a11a9c0b8ac4b08da502e6db965dcc3 [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>
Cezary Rojewski4137f4b2019-12-17 10:58:50 +010022#include <linux/pm_runtime.h>
Namarta Kohli1245b702012-08-16 17:10:41 +053023
Charles Keepax1e57b822018-04-24 16:39:03 +010024static int soc_compr_components_open(struct snd_compr_stream *cstream,
25 struct snd_soc_component **last)
Namarta Kohli1245b702012-08-16 17:10:41 +053026{
27 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +000028 struct snd_soc_component *component;
29 struct snd_soc_rtdcom_list *rtdcom;
Charles Keepax1e57b822018-04-24 16:39:03 +010030 int ret;
31
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +090032 for_each_rtd_components(rtd, rtdcom, component) {
Charles Keepax1e57b822018-04-24 16:39:03 +010033 if (!component->driver->compr_ops ||
34 !component->driver->compr_ops->open)
35 continue;
36
37 ret = component->driver->compr_ops->open(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
48 *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;
57 struct snd_soc_rtdcom_list *rtdcom;
58
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +090059 for_each_rtd_components(rtd, rtdcom, component) {
Charles Keepax1e57b822018-04-24 16:39:03 +010060 if (component == last)
61 break;
62
63 if (!component->driver->compr_ops ||
64 !component->driver->compr_ops->free)
65 continue;
66
67 component->driver->compr_ops->free(cstream);
68 }
69
70 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;
Cezary Rojewski4137f4b2019-12-17 10:58:50 +010076 struct snd_soc_component *component, *save = NULL;
77 struct snd_soc_rtdcom_list *rtdcom;
Vinod Koul2e622ae2016-11-13 12:10:02 +053078 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Charles Keepax572e6c82018-04-24 16:39:01 +010079 int ret;
Namarta Kohli1245b702012-08-16 17:10:41 +053080
Cezary Rojewski4137f4b2019-12-17 10:58:50 +010081 for_each_rtd_components(rtd, rtdcom, component) {
82 ret = pm_runtime_get_sync(component->dev);
83 if (ret < 0 && ret != -EACCES) {
84 pm_runtime_put_noidle(component->dev);
85 save = component;
86 goto pm_err;
87 }
88 }
89
Peter Ujfalusi72b745e2019-08-13 13:45:32 +030090 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Charles Keepax15e2e612013-01-24 09:44:29 +000091
Vinod Koul2e622ae2016-11-13 12:10:02 +053092 if (cpu_dai->driver->cops && cpu_dai->driver->cops->startup) {
93 ret = cpu_dai->driver->cops->startup(cstream, cpu_dai);
94 if (ret < 0) {
Charles Keepax141dfc92018-01-26 13:08:45 +000095 dev_err(cpu_dai->dev,
96 "Compress ASoC: can't open interface %s: %d\n",
Vinod Koul2e622ae2016-11-13 12:10:02 +053097 cpu_dai->name, ret);
98 goto out;
99 }
100 }
101
Charles Keepax1e57b822018-04-24 16:39:03 +0100102 ret = soc_compr_components_open(cstream, &component);
103 if (ret < 0)
104 goto machine_err;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000105
Namarta Kohli1245b702012-08-16 17:10:41 +0530106 if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->startup) {
107 ret = rtd->dai_link->compr_ops->startup(cstream);
108 if (ret < 0) {
Charles Keepax141dfc92018-01-26 13:08:45 +0000109 dev_err(rtd->dev,
110 "Compress ASoC: %s startup failed: %d\n",
111 rtd->dai_link->name, ret);
Namarta Kohli1245b702012-08-16 17:10:41 +0530112 goto machine_err;
113 }
114 }
115
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100116 snd_soc_runtime_activate(rtd, cstream->direction);
Namarta Kohli1245b702012-08-16 17:10:41 +0530117
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300118 mutex_unlock(&rtd->card->pcm_mutex);
Charles Keepax15e2e612013-01-24 09:44:29 +0000119
Namarta Kohli1245b702012-08-16 17:10:41 +0530120 return 0;
121
122machine_err:
Charles Keepax1e57b822018-04-24 16:39:03 +0100123 soc_compr_components_free(cstream, component);
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000124
Vinod Koul2e622ae2016-11-13 12:10:02 +0530125 if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
126 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
Namarta Kohli1245b702012-08-16 17:10:41 +0530127out:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300128 mutex_unlock(&rtd->card->pcm_mutex);
Cezary Rojewski4137f4b2019-12-17 10:58:50 +0100129pm_err:
130 for_each_rtd_components(rtd, rtdcom, component) {
131 if (component == save)
132 break;
133 pm_runtime_mark_last_busy(component->dev);
134 pm_runtime_put_autosuspend(component->dev);
135 }
136
Namarta Kohli1245b702012-08-16 17:10:41 +0530137 return ret;
138}
139
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000140static int soc_compr_open_fe(struct snd_compr_stream *cstream)
141{
142 struct snd_soc_pcm_runtime *fe = cstream->private_data;
Satish Babu Patakokila01b8ced2017-06-16 17:33:40 -0700143 struct snd_pcm_substream *fe_substream =
144 fe->pcm->streams[cstream->direction].substream;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000145 struct snd_soc_component *component;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530146 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000147 struct snd_soc_dpcm *dpcm;
148 struct snd_soc_dapm_widget_list *list;
149 int stream;
Charles Keepax572e6c82018-04-24 16:39:01 +0100150 int ret;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000151
152 if (cstream->direction == SND_COMPRESS_PLAYBACK)
153 stream = SNDRV_PCM_STREAM_PLAYBACK;
154 else
155 stream = SNDRV_PCM_STREAM_CAPTURE;
156
157 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Srinivas Kandagatla0b0722e2018-08-03 13:30:03 +0100158 fe->dpcm[stream].runtime = fe_substream->runtime;
159
160 ret = dpcm_path_get(fe, stream, &list);
161 if (ret < 0)
162 goto be_err;
163 else if (ret == 0)
164 dev_dbg(fe->dev, "Compress ASoC: %s no valid %s route\n",
165 fe->dai_link->name, stream ? "capture" : "playback");
166 /* calculate valid and active FE <-> BE dpcms */
167 dpcm_process_paths(fe, stream, &list, 1);
168 fe->dpcm[stream].runtime = fe_substream->runtime;
169
170 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
171
172 ret = dpcm_be_dai_startup(fe, stream);
173 if (ret < 0) {
174 /* clean up all links */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +0000175 for_each_dpcm_be(fe, stream, dpcm)
Srinivas Kandagatla0b0722e2018-08-03 13:30:03 +0100176 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
177
178 dpcm_be_disconnect(fe, stream);
179 fe->dpcm[stream].runtime = NULL;
180 goto out;
181 }
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000182
Vinod Koul2e622ae2016-11-13 12:10:02 +0530183 if (cpu_dai->driver->cops && cpu_dai->driver->cops->startup) {
184 ret = cpu_dai->driver->cops->startup(cstream, cpu_dai);
185 if (ret < 0) {
Charles Keepax141dfc92018-01-26 13:08:45 +0000186 dev_err(cpu_dai->dev,
187 "Compress ASoC: can't open interface %s: %d\n",
Vinod Koul2e622ae2016-11-13 12:10:02 +0530188 cpu_dai->name, ret);
189 goto out;
190 }
191 }
192
Charles Keepax1e57b822018-04-24 16:39:03 +0100193 ret = soc_compr_components_open(cstream, &component);
194 if (ret < 0)
Srinivas Kandagatla0b0722e2018-08-03 13:30:03 +0100195 goto open_err;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000196
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000197 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->startup) {
198 ret = fe->dai_link->compr_ops->startup(cstream);
199 if (ret < 0) {
Charles Keepax141dfc92018-01-26 13:08:45 +0000200 pr_err("Compress ASoC: %s startup failed: %d\n",
201 fe->dai_link->name, ret);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000202 goto machine_err;
203 }
204 }
205
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000206 dpcm_clear_pending_state(fe, stream);
207 dpcm_path_put(&list);
208
209 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
210 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
211
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100212 snd_soc_runtime_activate(fe, stream);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000213
214 mutex_unlock(&fe->card->mutex);
215
216 return 0;
217
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000218machine_err:
Charles Keepax1e57b822018-04-24 16:39:03 +0100219 soc_compr_components_free(cstream, component);
Srinivas Kandagatla0b0722e2018-08-03 13:30:03 +0100220open_err:
Vinod Koul2e622ae2016-11-13 12:10:02 +0530221 if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
222 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000223out:
Srinivas Kandagatla0b0722e2018-08-03 13:30:03 +0100224 dpcm_path_put(&list);
225be_err:
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000226 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
227 mutex_unlock(&fe->card->mutex);
228 return ret;
229}
230
Charles Keepax202c8f72013-01-24 09:44:30 +0000231/*
232 * Power down the audio subsystem pmdown_time msecs after close is called.
233 * This is to ensure there are no pops or clicks in between any music tracks
234 * due to DAPM power cycling.
235 */
Curtis Malainey4bf2e382019-12-03 09:30:07 -0800236static void close_delayed_work(struct snd_soc_pcm_runtime *rtd)
Charles Keepax202c8f72013-01-24 09:44:30 +0000237{
Charles Keepax202c8f72013-01-24 09:44:30 +0000238 struct snd_soc_dai *codec_dai = rtd->codec_dai;
239
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300240 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Charles Keepax202c8f72013-01-24 09:44:30 +0000241
Charles Keepax141dfc92018-01-26 13:08:45 +0000242 dev_dbg(rtd->dev,
243 "Compress ASoC: pop wq checking: %s status: %s waiting: %s\n",
244 codec_dai->driver->playback.stream_name,
245 codec_dai->playback_active ? "active" : "inactive",
246 rtd->pop_wait ? "yes" : "no");
Charles Keepax202c8f72013-01-24 09:44:30 +0000247
248 /* are we waiting on this codec DAI stream */
249 if (rtd->pop_wait == 1) {
250 rtd->pop_wait = 0;
251 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
252 SND_SOC_DAPM_STREAM_STOP);
253 }
254
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300255 mutex_unlock(&rtd->card->pcm_mutex);
Charles Keepax202c8f72013-01-24 09:44:30 +0000256}
257
Namarta Kohli1245b702012-08-16 17:10:41 +0530258static int soc_compr_free(struct snd_compr_stream *cstream)
259{
260 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Cezary Rojewski4137f4b2019-12-17 10:58:50 +0100261 struct snd_soc_component *component;
262 struct snd_soc_rtdcom_list *rtdcom;
Namarta Kohli1245b702012-08-16 17:10:41 +0530263 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
264 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100265 int stream;
Namarta Kohli1245b702012-08-16 17:10:41 +0530266
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300267 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Charles Keepax15e2e612013-01-24 09:44:29 +0000268
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100269 if (cstream->direction == SND_COMPRESS_PLAYBACK)
270 stream = SNDRV_PCM_STREAM_PLAYBACK;
271 else
272 stream = SNDRV_PCM_STREAM_CAPTURE;
273
274 snd_soc_runtime_deactivate(rtd, stream);
Namarta Kohli1245b702012-08-16 17:10:41 +0530275
Mark Brownda183962013-02-06 15:44:07 +0000276 snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
277
Namarta Kohli1245b702012-08-16 17:10:41 +0530278 if (!cpu_dai->active)
279 cpu_dai->rate = 0;
280
281 if (!codec_dai->active)
282 codec_dai->rate = 0;
283
Namarta Kohli1245b702012-08-16 17:10:41 +0530284 if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->shutdown)
285 rtd->dai_link->compr_ops->shutdown(cstream);
286
Charles Keepax1e57b822018-04-24 16:39:03 +0100287 soc_compr_components_free(cstream, NULL);
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000288
Vinod Koul2e622ae2016-11-13 12:10:02 +0530289 if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
290 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
291
Namarta Kohli1245b702012-08-16 17:10:41 +0530292 if (cstream->direction == SND_COMPRESS_PLAYBACK) {
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100293 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
Namarta Kohli1245b702012-08-16 17:10:41 +0530294 snd_soc_dapm_stream_event(rtd,
Charles Keepax89027d92018-04-26 17:30:07 +0100295 SNDRV_PCM_STREAM_PLAYBACK,
296 SND_SOC_DAPM_STREAM_STOP);
Charles Keepax8c3d2aa2013-01-24 09:44:28 +0000297 } else {
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600298 rtd->pop_wait = 1;
Mark Brown3d24cfe2013-08-09 18:12:29 +0100299 queue_delayed_work(system_power_efficient_wq,
300 &rtd->delayed_work,
301 msecs_to_jiffies(rtd->pmdown_time));
Charles Keepax8c3d2aa2013-01-24 09:44:28 +0000302 }
Namarta Kohli1245b702012-08-16 17:10:41 +0530303 } else {
304 /* capture streams can be powered down now */
305 snd_soc_dapm_stream_event(rtd,
Charles Keepax89027d92018-04-26 17:30:07 +0100306 SNDRV_PCM_STREAM_CAPTURE,
307 SND_SOC_DAPM_STREAM_STOP);
Namarta Kohli1245b702012-08-16 17:10:41 +0530308 }
309
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300310 mutex_unlock(&rtd->card->pcm_mutex);
Cezary Rojewski4137f4b2019-12-17 10:58:50 +0100311
312 for_each_rtd_components(rtd, rtdcom, component) {
313 pm_runtime_mark_last_busy(component->dev);
314 pm_runtime_put_autosuspend(component->dev);
315 }
316
Namarta Kohli1245b702012-08-16 17:10:41 +0530317 return 0;
318}
319
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000320static int soc_compr_free_fe(struct snd_compr_stream *cstream)
321{
322 struct snd_soc_pcm_runtime *fe = cstream->private_data;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530323 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000324 struct snd_soc_dpcm *dpcm;
325 int stream, ret;
326
327 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
328
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100329 if (cstream->direction == SND_COMPRESS_PLAYBACK)
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000330 stream = SNDRV_PCM_STREAM_PLAYBACK;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100331 else
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000332 stream = SNDRV_PCM_STREAM_CAPTURE;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000333
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100334 snd_soc_runtime_deactivate(fe, stream);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000335
336 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
337
338 ret = dpcm_be_dai_hw_free(fe, stream);
339 if (ret < 0)
Charles Keepax141dfc92018-01-26 13:08:45 +0000340 dev_err(fe->dev, "Compressed ASoC: hw_free failed: %d\n", ret);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000341
342 ret = dpcm_be_dai_shutdown(fe, stream);
343
344 /* mark FE's links ready to prune */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +0000345 for_each_dpcm_be(fe, stream, dpcm)
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000346 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
347
Daniel Mack15f6b092014-10-19 09:07:35 +0200348 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000349
350 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
351 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
352
353 dpcm_be_disconnect(fe, stream);
354
355 fe->dpcm[stream].runtime = NULL;
356
357 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->shutdown)
358 fe->dai_link->compr_ops->shutdown(cstream);
359
Charles Keepax1e57b822018-04-24 16:39:03 +0100360 soc_compr_components_free(cstream, NULL);
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000361
Vinod Koul2e622ae2016-11-13 12:10:02 +0530362 if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
363 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
364
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000365 mutex_unlock(&fe->card->mutex);
366 return 0;
367}
368
Charles Keepax4ef0ecb2019-02-05 11:18:13 +0000369static int soc_compr_components_trigger(struct snd_compr_stream *cstream,
370 int cmd)
Namarta Kohli1245b702012-08-16 17:10:41 +0530371{
Namarta Kohli1245b702012-08-16 17:10:41 +0530372 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000373 struct snd_soc_component *component;
374 struct snd_soc_rtdcom_list *rtdcom;
Charles Keepax4ef0ecb2019-02-05 11:18:13 +0000375 int ret;
Charles Keepax15e2e612013-01-24 09:44:29 +0000376
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900377 for_each_rtd_components(rtd, rtdcom, component) {
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000378 if (!component->driver->compr_ops ||
379 !component->driver->compr_ops->trigger)
380 continue;
381
Charles Keepax52cadf12019-02-05 11:18:12 +0000382 ret = component->driver->compr_ops->trigger(cstream, cmd);
383 if (ret < 0)
Charles Keepax4ef0ecb2019-02-05 11:18:13 +0000384 return ret;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000385 }
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000386
Charles Keepax4ef0ecb2019-02-05 11:18:13 +0000387 return 0;
388}
389
390static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd)
391{
392 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
393 struct snd_soc_dai *codec_dai = rtd->codec_dai;
394 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
395 int ret;
396
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300397 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Charles Keepax4ef0ecb2019-02-05 11:18:13 +0000398
399 ret = soc_compr_components_trigger(cstream, cmd);
400 if (ret < 0)
401 goto out;
402
Vinod Koul2e622ae2016-11-13 12:10:02 +0530403 if (cpu_dai->driver->cops && cpu_dai->driver->cops->trigger)
404 cpu_dai->driver->cops->trigger(cstream, cmd, cpu_dai);
405
Mark Brownda183962013-02-06 15:44:07 +0000406 switch (cmd) {
407 case SNDRV_PCM_TRIGGER_START:
408 snd_soc_dai_digital_mute(codec_dai, 0, cstream->direction);
409 break;
410 case SNDRV_PCM_TRIGGER_STOP:
411 snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
412 break;
Mark Browne38b9b72013-02-06 13:52:42 +0000413 }
Namarta Kohli1245b702012-08-16 17:10:41 +0530414
Charles Keepax15e2e612013-01-24 09:44:29 +0000415out:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300416 mutex_unlock(&rtd->card->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530417 return ret;
418}
419
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000420static int soc_compr_trigger_fe(struct snd_compr_stream *cstream, int cmd)
421{
422 struct snd_soc_pcm_runtime *fe = cstream->private_data;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530423 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
Charles Keepax52cadf12019-02-05 11:18:12 +0000424 int ret, stream;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000425
426 if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN ||
Charles Keepax4ef0ecb2019-02-05 11:18:13 +0000427 cmd == SND_COMPR_TRIGGER_DRAIN)
428 return soc_compr_components_trigger(cstream, cmd);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000429
430 if (cstream->direction == SND_COMPRESS_PLAYBACK)
431 stream = SNDRV_PCM_STREAM_PLAYBACK;
432 else
433 stream = SNDRV_PCM_STREAM_CAPTURE;
434
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000435 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
436
Vinod Koul2e622ae2016-11-13 12:10:02 +0530437 if (cpu_dai->driver->cops && cpu_dai->driver->cops->trigger) {
438 ret = cpu_dai->driver->cops->trigger(cstream, cmd, cpu_dai);
439 if (ret < 0)
440 goto out;
441 }
442
Charles Keepax4ef0ecb2019-02-05 11:18:13 +0000443 ret = soc_compr_components_trigger(cstream, cmd);
444 if (ret < 0)
445 goto out;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000446
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000447 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
448
449 ret = dpcm_be_dai_trigger(fe, stream, cmd);
450
451 switch (cmd) {
452 case SNDRV_PCM_TRIGGER_START:
453 case SNDRV_PCM_TRIGGER_RESUME:
454 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
455 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
456 break;
457 case SNDRV_PCM_TRIGGER_STOP:
458 case SNDRV_PCM_TRIGGER_SUSPEND:
459 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
460 break;
461 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
462 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
463 break;
464 }
465
466out:
467 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
468 mutex_unlock(&fe->card->mutex);
469 return ret;
470}
471
Charles Keepax4ef0ecb2019-02-05 11:18:13 +0000472static int soc_compr_components_set_params(struct snd_compr_stream *cstream,
473 struct snd_compr_params *params)
Namarta Kohli1245b702012-08-16 17:10:41 +0530474{
475 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000476 struct snd_soc_component *component;
477 struct snd_soc_rtdcom_list *rtdcom;
Charles Keepax4ef0ecb2019-02-05 11:18:13 +0000478 int ret;
479
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900480 for_each_rtd_components(rtd, rtdcom, component) {
Charles Keepax4ef0ecb2019-02-05 11:18:13 +0000481 if (!component->driver->compr_ops ||
482 !component->driver->compr_ops->set_params)
483 continue;
484
485 ret = component->driver->compr_ops->set_params(cstream, params);
486 if (ret < 0)
487 return ret;
488 }
489
490 return 0;
491}
492
493static int soc_compr_set_params(struct snd_compr_stream *cstream,
494 struct snd_compr_params *params)
495{
496 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530497 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Charles Keepax52cadf12019-02-05 11:18:12 +0000498 int ret;
Namarta Kohli1245b702012-08-16 17:10:41 +0530499
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300500 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Charles Keepax15e2e612013-01-24 09:44:29 +0000501
Charles Keepaxef050be2018-04-24 16:39:02 +0100502 /*
503 * First we call set_params for the CPU DAI, then the component
504 * driver this should configure the SoC side. If the machine has
505 * compressed ops then we call that as well. The expectation is
506 * that these callbacks will configure everything for this compress
507 * path, like configuring a PCM port for a CODEC.
Namarta Kohli1245b702012-08-16 17:10:41 +0530508 */
Vinod Koul2e622ae2016-11-13 12:10:02 +0530509 if (cpu_dai->driver->cops && cpu_dai->driver->cops->set_params) {
510 ret = cpu_dai->driver->cops->set_params(cstream, params, cpu_dai);
511 if (ret < 0)
512 goto err;
513 }
514
Charles Keepax4ef0ecb2019-02-05 11:18:13 +0000515 ret = soc_compr_components_set_params(cstream, params);
516 if (ret < 0)
517 goto err;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000518
Namarta Kohli1245b702012-08-16 17:10:41 +0530519 if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->set_params) {
520 ret = rtd->dai_link->compr_ops->set_params(cstream);
521 if (ret < 0)
Charles Keepaxfa40ef22013-03-27 16:39:01 +0000522 goto err;
Namarta Kohli1245b702012-08-16 17:10:41 +0530523 }
524
Charles Keepax2c071ed2013-05-20 08:33:54 +0100525 if (cstream->direction == SND_COMPRESS_PLAYBACK)
526 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Charles Keepax89027d92018-04-26 17:30:07 +0100527 SND_SOC_DAPM_STREAM_START);
Charles Keepax2c071ed2013-05-20 08:33:54 +0100528 else
529 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Charles Keepax89027d92018-04-26 17:30:07 +0100530 SND_SOC_DAPM_STREAM_START);
Namarta Kohli1245b702012-08-16 17:10:41 +0530531
Charles Keepaxfa40ef22013-03-27 16:39:01 +0000532 /* cancel any delayed stream shutdown that is pending */
533 rtd->pop_wait = 0;
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300534 mutex_unlock(&rtd->card->pcm_mutex);
Charles Keepaxfa40ef22013-03-27 16:39:01 +0000535
536 cancel_delayed_work_sync(&rtd->delayed_work);
537
Charles Keepax52cadf12019-02-05 11:18:12 +0000538 return 0;
Charles Keepaxfa40ef22013-03-27 16:39:01 +0000539
540err:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300541 mutex_unlock(&rtd->card->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530542 return ret;
543}
544
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000545static int soc_compr_set_params_fe(struct snd_compr_stream *cstream,
Charles Keepax89027d92018-04-26 17:30:07 +0100546 struct snd_compr_params *params)
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000547{
548 struct snd_soc_pcm_runtime *fe = cstream->private_data;
Satish Babu Patakokila01b8ced2017-06-16 17:33:40 -0700549 struct snd_pcm_substream *fe_substream =
550 fe->pcm->streams[cstream->direction].substream;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530551 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
Charles Keepax52cadf12019-02-05 11:18:12 +0000552 int ret, stream;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000553
554 if (cstream->direction == SND_COMPRESS_PLAYBACK)
555 stream = SNDRV_PCM_STREAM_PLAYBACK;
556 else
557 stream = SNDRV_PCM_STREAM_CAPTURE;
558
559 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
560
Srinivas Kandagatla0b0722e2018-08-03 13:30:03 +0100561 /*
562 * Create an empty hw_params for the BE as the machine driver must
563 * fix this up to match DSP decoder and ASRC configuration.
564 * I.e. machine driver fixup for compressed BE is mandatory.
565 */
566 memset(&fe->dpcm[fe_substream->stream].hw_params, 0,
567 sizeof(struct snd_pcm_hw_params));
568
569 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
570
571 ret = dpcm_be_dai_hw_params(fe, stream);
572 if (ret < 0)
573 goto out;
574
575 ret = dpcm_be_dai_prepare(fe, stream);
576 if (ret < 0)
577 goto out;
578
Vinod Koul2e622ae2016-11-13 12:10:02 +0530579 if (cpu_dai->driver->cops && cpu_dai->driver->cops->set_params) {
580 ret = cpu_dai->driver->cops->set_params(cstream, params, cpu_dai);
581 if (ret < 0)
582 goto out;
583 }
584
Charles Keepax4ef0ecb2019-02-05 11:18:13 +0000585 ret = soc_compr_components_set_params(cstream, params);
586 if (ret < 0)
587 goto out;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000588
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000589 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->set_params) {
590 ret = fe->dai_link->compr_ops->set_params(cstream);
591 if (ret < 0)
592 goto out;
593 }
594
Daniel Mack15f6b092014-10-19 09:07:35 +0200595 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000596 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
597
598out:
599 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
600 mutex_unlock(&fe->card->mutex);
601 return ret;
602}
603
Namarta Kohli1245b702012-08-16 17:10:41 +0530604static int soc_compr_get_params(struct snd_compr_stream *cstream,
Charles Keepax89027d92018-04-26 17:30:07 +0100605 struct snd_codec *params)
Namarta Kohli1245b702012-08-16 17:10:41 +0530606{
607 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000608 struct snd_soc_component *component;
609 struct snd_soc_rtdcom_list *rtdcom;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530610 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Charles Keepax52cadf12019-02-05 11:18:12 +0000611 int ret = 0;
Namarta Kohli1245b702012-08-16 17:10:41 +0530612
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300613 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Charles Keepax15e2e612013-01-24 09:44:29 +0000614
Vinod Koul2e622ae2016-11-13 12:10:02 +0530615 if (cpu_dai->driver->cops && cpu_dai->driver->cops->get_params) {
616 ret = cpu_dai->driver->cops->get_params(cstream, params, cpu_dai);
617 if (ret < 0)
618 goto err;
619 }
620
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900621 for_each_rtd_components(rtd, rtdcom, component) {
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000622 if (!component->driver->compr_ops ||
623 !component->driver->compr_ops->get_params)
624 continue;
625
Charles Keepax52cadf12019-02-05 11:18:12 +0000626 ret = component->driver->compr_ops->get_params(cstream, params);
627 break;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000628 }
Namarta Kohli1245b702012-08-16 17:10:41 +0530629
Vinod Koul2e622ae2016-11-13 12:10:02 +0530630err:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300631 mutex_unlock(&rtd->card->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530632 return ret;
633}
634
635static int soc_compr_get_caps(struct snd_compr_stream *cstream,
Charles Keepax89027d92018-04-26 17:30:07 +0100636 struct snd_compr_caps *caps)
Namarta Kohli1245b702012-08-16 17:10:41 +0530637{
638 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000639 struct snd_soc_component *component;
640 struct snd_soc_rtdcom_list *rtdcom;
Charles Keepax52cadf12019-02-05 11:18:12 +0000641 int ret = 0;
Namarta Kohli1245b702012-08-16 17:10:41 +0530642
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300643 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Charles Keepax15e2e612013-01-24 09:44:29 +0000644
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900645 for_each_rtd_components(rtd, rtdcom, component) {
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000646 if (!component->driver->compr_ops ||
647 !component->driver->compr_ops->get_caps)
648 continue;
649
Charles Keepax52cadf12019-02-05 11:18:12 +0000650 ret = component->driver->compr_ops->get_caps(cstream, caps);
651 break;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000652 }
653
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300654 mutex_unlock(&rtd->card->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530655 return ret;
656}
657
658static int soc_compr_get_codec_caps(struct snd_compr_stream *cstream,
Charles Keepax89027d92018-04-26 17:30:07 +0100659 struct snd_compr_codec_caps *codec)
Namarta Kohli1245b702012-08-16 17:10:41 +0530660{
661 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000662 struct snd_soc_component *component;
663 struct snd_soc_rtdcom_list *rtdcom;
Charles Keepax52cadf12019-02-05 11:18:12 +0000664 int ret = 0;
Namarta Kohli1245b702012-08-16 17:10:41 +0530665
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300666 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Charles Keepax15e2e612013-01-24 09:44:29 +0000667
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900668 for_each_rtd_components(rtd, rtdcom, component) {
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000669 if (!component->driver->compr_ops ||
670 !component->driver->compr_ops->get_codec_caps)
671 continue;
672
Charles Keepax52cadf12019-02-05 11:18:12 +0000673 ret = component->driver->compr_ops->get_codec_caps(cstream,
674 codec);
675 break;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000676 }
677
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300678 mutex_unlock(&rtd->card->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530679 return ret;
680}
681
682static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
683{
684 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000685 struct snd_soc_component *component;
686 struct snd_soc_rtdcom_list *rtdcom;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530687 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Charles Keepax52cadf12019-02-05 11:18:12 +0000688 int ret = 0;
Namarta Kohli1245b702012-08-16 17:10:41 +0530689
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300690 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Charles Keepax15e2e612013-01-24 09:44:29 +0000691
Vinod Koul2e622ae2016-11-13 12:10:02 +0530692 if (cpu_dai->driver->cops && cpu_dai->driver->cops->ack) {
693 ret = cpu_dai->driver->cops->ack(cstream, bytes, cpu_dai);
694 if (ret < 0)
695 goto err;
696 }
697
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900698 for_each_rtd_components(rtd, rtdcom, component) {
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000699 if (!component->driver->compr_ops ||
700 !component->driver->compr_ops->ack)
701 continue;
702
Charles Keepax52cadf12019-02-05 11:18:12 +0000703 ret = component->driver->compr_ops->ack(cstream, bytes);
704 if (ret < 0)
705 goto err;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000706 }
Namarta Kohli1245b702012-08-16 17:10:41 +0530707
Vinod Koul2e622ae2016-11-13 12:10:02 +0530708err:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300709 mutex_unlock(&rtd->card->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530710 return ret;
711}
712
713static int soc_compr_pointer(struct snd_compr_stream *cstream,
Charles Keepax89027d92018-04-26 17:30:07 +0100714 struct snd_compr_tstamp *tstamp)
Namarta Kohli1245b702012-08-16 17:10:41 +0530715{
716 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000717 struct snd_soc_component *component;
718 struct snd_soc_rtdcom_list *rtdcom;
Charles Keepax52cadf12019-02-05 11:18:12 +0000719 int ret = 0;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530720 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Namarta Kohli1245b702012-08-16 17:10:41 +0530721
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300722 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Charles Keepax15e2e612013-01-24 09:44:29 +0000723
Vinod Koul2e622ae2016-11-13 12:10:02 +0530724 if (cpu_dai->driver->cops && cpu_dai->driver->cops->pointer)
725 cpu_dai->driver->cops->pointer(cstream, tstamp, cpu_dai);
726
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900727 for_each_rtd_components(rtd, rtdcom, component) {
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000728 if (!component->driver->compr_ops ||
729 !component->driver->compr_ops->pointer)
730 continue;
731
Charles Keepax52cadf12019-02-05 11:18:12 +0000732 ret = component->driver->compr_ops->pointer(cstream, tstamp);
733 break;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000734 }
735
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300736 mutex_unlock(&rtd->card->pcm_mutex);
Charles Keepax7c9190f2016-06-20 09:51:32 +0100737 return ret;
Namarta Kohli1245b702012-08-16 17:10:41 +0530738}
739
Charles Keepax1f88eb02013-02-05 10:41:47 +0000740static int soc_compr_copy(struct snd_compr_stream *cstream,
Charles Keepax4daf8912013-04-18 11:01:38 +0100741 char __user *buf, size_t count)
Charles Keepax1f88eb02013-02-05 10:41:47 +0000742{
743 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000744 struct snd_soc_component *component;
745 struct snd_soc_rtdcom_list *rtdcom;
Charles Keepax290df4d32018-01-26 13:08:43 +0000746 int ret = 0;
Charles Keepax1f88eb02013-02-05 10:41:47 +0000747
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300748 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Charles Keepax1f88eb02013-02-05 10:41:47 +0000749
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900750 for_each_rtd_components(rtd, rtdcom, component) {
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000751 if (!component->driver->compr_ops ||
752 !component->driver->compr_ops->copy)
753 continue;
754
Charles Keepax290df4d32018-01-26 13:08:43 +0000755 ret = component->driver->compr_ops->copy(cstream, buf, count);
756 break;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000757 }
Charles Keepax290df4d32018-01-26 13:08:43 +0000758
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300759 mutex_unlock(&rtd->card->pcm_mutex);
Charles Keepax1f88eb02013-02-05 10:41:47 +0000760 return ret;
761}
762
Vinod Koul02bd90e2013-07-28 20:06:15 +0530763static int soc_compr_set_metadata(struct snd_compr_stream *cstream,
Charles Keepax89027d92018-04-26 17:30:07 +0100764 struct snd_compr_metadata *metadata)
Jeeja KP36953d92013-03-26 21:22:28 +0530765{
766 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000767 struct snd_soc_component *component;
768 struct snd_soc_rtdcom_list *rtdcom;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530769 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Charles Keepax52cadf12019-02-05 11:18:12 +0000770 int ret;
Jeeja KP36953d92013-03-26 21:22:28 +0530771
Vinod Koul2e622ae2016-11-13 12:10:02 +0530772 if (cpu_dai->driver->cops && cpu_dai->driver->cops->set_metadata) {
773 ret = cpu_dai->driver->cops->set_metadata(cstream, metadata, cpu_dai);
774 if (ret < 0)
775 return ret;
776 }
777
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900778 for_each_rtd_components(rtd, rtdcom, component) {
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000779 if (!component->driver->compr_ops ||
780 !component->driver->compr_ops->set_metadata)
781 continue;
782
Charles Keepax52cadf12019-02-05 11:18:12 +0000783 ret = component->driver->compr_ops->set_metadata(cstream,
784 metadata);
785 if (ret < 0)
786 return ret;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000787 }
Jeeja KP36953d92013-03-26 21:22:28 +0530788
Charles Keepax52cadf12019-02-05 11:18:12 +0000789 return 0;
Jeeja KP36953d92013-03-26 21:22:28 +0530790}
791
Vinod Koul02bd90e2013-07-28 20:06:15 +0530792static int soc_compr_get_metadata(struct snd_compr_stream *cstream,
Charles Keepax89027d92018-04-26 17:30:07 +0100793 struct snd_compr_metadata *metadata)
Jeeja KP36953d92013-03-26 21:22:28 +0530794{
795 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000796 struct snd_soc_component *component;
797 struct snd_soc_rtdcom_list *rtdcom;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530798 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Charles Keepax52cadf12019-02-05 11:18:12 +0000799 int ret;
Jeeja KP36953d92013-03-26 21:22:28 +0530800
Vinod Koul2e622ae2016-11-13 12:10:02 +0530801 if (cpu_dai->driver->cops && cpu_dai->driver->cops->get_metadata) {
802 ret = cpu_dai->driver->cops->get_metadata(cstream, metadata, cpu_dai);
803 if (ret < 0)
804 return ret;
805 }
806
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900807 for_each_rtd_components(rtd, rtdcom, component) {
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000808 if (!component->driver->compr_ops ||
809 !component->driver->compr_ops->get_metadata)
810 continue;
811
Charles Keepax52cadf12019-02-05 11:18:12 +0000812 return component->driver->compr_ops->get_metadata(cstream,
813 metadata);
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000814 }
Jeeja KP36953d92013-03-26 21:22:28 +0530815
Charles Keepax52cadf12019-02-05 11:18:12 +0000816 return 0;
Jeeja KP36953d92013-03-26 21:22:28 +0530817}
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000818
Namarta Kohli1245b702012-08-16 17:10:41 +0530819/* ASoC Compress operations */
820static struct snd_compr_ops soc_compr_ops = {
821 .open = soc_compr_open,
822 .free = soc_compr_free,
823 .set_params = soc_compr_set_params,
Vinod Koul02bd90e2013-07-28 20:06:15 +0530824 .set_metadata = soc_compr_set_metadata,
825 .get_metadata = soc_compr_get_metadata,
Namarta Kohli1245b702012-08-16 17:10:41 +0530826 .get_params = soc_compr_get_params,
827 .trigger = soc_compr_trigger,
828 .pointer = soc_compr_pointer,
829 .ack = soc_compr_ack,
830 .get_caps = soc_compr_get_caps,
831 .get_codec_caps = soc_compr_get_codec_caps
832};
833
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000834/* ASoC Dynamic Compress operations */
835static struct snd_compr_ops soc_compr_dyn_ops = {
836 .open = soc_compr_open_fe,
837 .free = soc_compr_free_fe,
838 .set_params = soc_compr_set_params_fe,
839 .get_params = soc_compr_get_params,
840 .set_metadata = soc_compr_set_metadata,
841 .get_metadata = soc_compr_get_metadata,
842 .trigger = soc_compr_trigger_fe,
843 .pointer = soc_compr_pointer,
844 .ack = soc_compr_ack,
845 .get_caps = soc_compr_get_caps,
846 .get_codec_caps = soc_compr_get_codec_caps
847};
848
Jie Yang6f0c4222015-10-13 23:41:00 +0800849/**
850 * snd_soc_new_compress - create a new compress.
851 *
852 * @rtd: The runtime for which we will create compress
853 * @num: the device index number (zero based - shared with normal PCMs)
854 *
855 * Return: 0 for success, else error.
856 */
857int snd_soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
Namarta Kohli1245b702012-08-16 17:10:41 +0530858{
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000859 struct snd_soc_component *component;
860 struct snd_soc_rtdcom_list *rtdcom;
Namarta Kohli1245b702012-08-16 17:10:41 +0530861 struct snd_soc_dai *codec_dai = rtd->codec_dai;
862 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
863 struct snd_compr *compr;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000864 struct snd_pcm *be_pcm;
Namarta Kohli1245b702012-08-16 17:10:41 +0530865 char new_name[64];
866 int ret = 0, direction = 0;
Vinod Koula1068042016-01-07 21:48:14 +0530867 int playback = 0, capture = 0;
Namarta Kohli1245b702012-08-16 17:10:41 +0530868
Benoit Cousson8151d5e2014-07-08 23:19:37 +0200869 if (rtd->num_codecs > 1) {
Charles Keepax141dfc92018-01-26 13:08:45 +0000870 dev_err(rtd->card->dev,
871 "Compress ASoC: Multicodec not supported\n");
Benoit Cousson8151d5e2014-07-08 23:19:37 +0200872 return -EINVAL;
873 }
874
Namarta Kohli1245b702012-08-16 17:10:41 +0530875 /* check client and interface hw capabilities */
Kuninori Morimoto467fece2019-07-22 10:36:16 +0900876 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
877 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
Vinod Koula1068042016-01-07 21:48:14 +0530878 playback = 1;
Kuninori Morimoto467fece2019-07-22 10:36:16 +0900879 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
880 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
Vinod Koula1068042016-01-07 21:48:14 +0530881 capture = 1;
882
Vinod Koula1068042016-01-07 21:48:14 +0530883 /*
884 * Compress devices are unidirectional so only one of the directions
885 * should be set, check for that (xor)
886 */
887 if (playback + capture != 1) {
Charles Keepax141dfc92018-01-26 13:08:45 +0000888 dev_err(rtd->card->dev,
889 "Compress ASoC: Invalid direction for P %d, C %d\n",
890 playback, capture);
Charles Keepaxdaa2db52013-04-18 11:02:38 +0100891 return -EINVAL;
Vinod Koula1068042016-01-07 21:48:14 +0530892 }
893
Peng Donglinaeb6fa02017-08-16 22:47:53 +0800894 if (playback)
Vinod Koula1068042016-01-07 21:48:14 +0530895 direction = SND_COMPRESS_PLAYBACK;
896 else
897 direction = SND_COMPRESS_CAPTURE;
Charles Keepaxdaa2db52013-04-18 11:02:38 +0100898
Amadeusz Sławiński09f448a2019-06-17 13:36:36 +0200899 compr = devm_kzalloc(rtd->card->dev, sizeof(*compr), GFP_KERNEL);
Markus Elfring7a0cf422017-08-10 16:21:34 +0200900 if (!compr)
Namarta Kohli1245b702012-08-16 17:10:41 +0530901 return -ENOMEM;
Namarta Kohli1245b702012-08-16 17:10:41 +0530902
Charles Keepax1f88eb02013-02-05 10:41:47 +0000903 compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
904 GFP_KERNEL);
Amadeusz Sławiński09f448a2019-06-17 13:36:36 +0200905 if (!compr->ops)
906 return -ENOMEM;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000907
908 if (rtd->dai_link->dynamic) {
909 snprintf(new_name, sizeof(new_name), "(%s)",
910 rtd->dai_link->stream_name);
911
912 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
Qais Yousefd3268a42015-01-14 08:47:29 +0000913 rtd->dai_link->dpcm_playback,
914 rtd->dai_link->dpcm_capture, &be_pcm);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000915 if (ret < 0) {
Charles Keepax141dfc92018-01-26 13:08:45 +0000916 dev_err(rtd->card->dev,
917 "Compress ASoC: can't create compressed for %s: %d\n",
918 rtd->dai_link->name, ret);
Amadeusz Sławiński09f448a2019-06-17 13:36:36 +0200919 return ret;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000920 }
921
922 rtd->pcm = be_pcm;
923 rtd->fe_compr = 1;
Qais Yousefd3268a42015-01-14 08:47:29 +0000924 if (rtd->dai_link->dpcm_playback)
925 be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
926 else if (rtd->dai_link->dpcm_capture)
927 be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000928 memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
Peng Donglinaeb6fa02017-08-16 22:47:53 +0800929 } else {
930 snprintf(new_name, sizeof(new_name), "%s %s-%d",
931 rtd->dai_link->stream_name, codec_dai->name, num);
932
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000933 memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
Peng Donglinaeb6fa02017-08-16 22:47:53 +0800934 }
Charles Keepax1f88eb02013-02-05 10:41:47 +0000935
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900936 for_each_rtd_components(rtd, rtdcom, component) {
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000937 if (!component->driver->compr_ops ||
938 !component->driver->compr_ops->copy)
939 continue;
940
941 compr->ops->copy = soc_compr_copy;
Charles Keepaxca76db62018-04-26 17:30:04 +0100942 break;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000943 }
944
Namarta Kohli1245b702012-08-16 17:10:41 +0530945 mutex_init(&compr->lock);
Richard Fitzgeralde5241a82015-11-25 13:00:24 +0000946 ret = snd_compress_new(rtd->card->snd_card, num, direction,
947 new_name, compr);
Namarta Kohli1245b702012-08-16 17:10:41 +0530948 if (ret < 0) {
Kuninori Morimotoe5acfc72017-12-05 04:23:05 +0000949 component = rtd->codec_dai->component;
Charles Keepax141dfc92018-01-26 13:08:45 +0000950 dev_err(component->dev,
951 "Compress ASoC: can't create compress for codec %s: %d\n",
952 component->name, ret);
Amadeusz Sławiński09f448a2019-06-17 13:36:36 +0200953 return ret;
Namarta Kohli1245b702012-08-16 17:10:41 +0530954 }
955
Charles Keepax202c8f72013-01-24 09:44:30 +0000956 /* DAPM dai link stream work */
Curtis Malainey4bf2e382019-12-03 09:30:07 -0800957 rtd->close_delayed_work_func = close_delayed_work;
Charles Keepax202c8f72013-01-24 09:44:30 +0000958
Namarta Kohli1245b702012-08-16 17:10:41 +0530959 rtd->compr = compr;
960 compr->private_data = rtd;
961
Charles Keepax141dfc92018-01-26 13:08:45 +0000962 dev_info(rtd->card->dev, "Compress ASoC: %s <-> %s mapping ok\n",
963 codec_dai->name, cpu_dai->name);
Charles Keepax1f88eb02013-02-05 10:41:47 +0000964
Amadeusz Sławiński09f448a2019-06-17 13:36:36 +0200965 return 0;
Namarta Kohli1245b702012-08-16 17:10:41 +0530966}
Jie Yang6f0c4222015-10-13 23:41:00 +0800967EXPORT_SYMBOL_GPL(snd_soc_new_compress);