blob: b9e1673fea51d006b2e61b04961e1df2d13ba9f9 [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>
Namarta Kohli1245b702012-08-16 17:10:41 +053022
Charles Keepax1e57b822018-04-24 16:39:03 +010023static int soc_compr_components_open(struct snd_compr_stream *cstream,
24 struct snd_soc_component **last)
Namarta Kohli1245b702012-08-16 17:10:41 +053025{
26 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +000027 struct snd_soc_component *component;
28 struct snd_soc_rtdcom_list *rtdcom;
Charles Keepax1e57b822018-04-24 16:39:03 +010029 int ret;
30
31 for_each_rtdcom(rtd, rtdcom) {
32 component = rtdcom->component;
33
34 if (!component->driver->compr_ops ||
35 !component->driver->compr_ops->open)
36 continue;
37
38 ret = component->driver->compr_ops->open(cstream);
39 if (ret < 0) {
40 dev_err(component->dev,
41 "Compress ASoC: can't open platform %s: %d\n",
42 component->name, ret);
43
44 *last = component;
45 return ret;
46 }
47 }
48
49 *last = NULL;
50 return 0;
51}
52
53static int soc_compr_components_free(struct snd_compr_stream *cstream,
54 struct snd_soc_component *last)
55{
56 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
57 struct snd_soc_component *component;
58 struct snd_soc_rtdcom_list *rtdcom;
59
60 for_each_rtdcom(rtd, rtdcom) {
61 component = rtdcom->component;
62
63 if (component == last)
64 break;
65
66 if (!component->driver->compr_ops ||
67 !component->driver->compr_ops->free)
68 continue;
69
70 component->driver->compr_ops->free(cstream);
71 }
72
73 return 0;
74}
75
76static int soc_compr_open(struct snd_compr_stream *cstream)
77{
78 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
79 struct snd_soc_component *component;
Vinod Koul2e622ae2016-11-13 12:10:02 +053080 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Charles Keepax572e6c82018-04-24 16:39:01 +010081 int ret;
Namarta Kohli1245b702012-08-16 17:10:41 +053082
Charles Keepax15e2e612013-01-24 09:44:29 +000083 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
84
Vinod Koul2e622ae2016-11-13 12:10:02 +053085 if (cpu_dai->driver->cops && cpu_dai->driver->cops->startup) {
86 ret = cpu_dai->driver->cops->startup(cstream, cpu_dai);
87 if (ret < 0) {
Charles Keepax141dfc92018-01-26 13:08:45 +000088 dev_err(cpu_dai->dev,
89 "Compress ASoC: can't open interface %s: %d\n",
Vinod Koul2e622ae2016-11-13 12:10:02 +053090 cpu_dai->name, ret);
91 goto out;
92 }
93 }
94
Charles Keepax1e57b822018-04-24 16:39:03 +010095 ret = soc_compr_components_open(cstream, &component);
96 if (ret < 0)
97 goto machine_err;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +000098
Namarta Kohli1245b702012-08-16 17:10:41 +053099 if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->startup) {
100 ret = rtd->dai_link->compr_ops->startup(cstream);
101 if (ret < 0) {
Charles Keepax141dfc92018-01-26 13:08:45 +0000102 dev_err(rtd->dev,
103 "Compress ASoC: %s startup failed: %d\n",
104 rtd->dai_link->name, ret);
Namarta Kohli1245b702012-08-16 17:10:41 +0530105 goto machine_err;
106 }
107 }
108
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100109 snd_soc_runtime_activate(rtd, cstream->direction);
Namarta Kohli1245b702012-08-16 17:10:41 +0530110
Charles Keepax15e2e612013-01-24 09:44:29 +0000111 mutex_unlock(&rtd->pcm_mutex);
112
Namarta Kohli1245b702012-08-16 17:10:41 +0530113 return 0;
114
115machine_err:
Charles Keepax1e57b822018-04-24 16:39:03 +0100116 soc_compr_components_free(cstream, component);
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000117
Vinod Koul2e622ae2016-11-13 12:10:02 +0530118 if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
119 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
Namarta Kohli1245b702012-08-16 17:10:41 +0530120out:
Charles Keepax15e2e612013-01-24 09:44:29 +0000121 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530122 return ret;
123}
124
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000125static int soc_compr_open_fe(struct snd_compr_stream *cstream)
126{
127 struct snd_soc_pcm_runtime *fe = cstream->private_data;
Satish Babu Patakokila01b8ced2017-06-16 17:33:40 -0700128 struct snd_pcm_substream *fe_substream =
129 fe->pcm->streams[cstream->direction].substream;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000130 struct snd_soc_component *component;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530131 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000132 struct snd_soc_dpcm *dpcm;
133 struct snd_soc_dapm_widget_list *list;
134 int stream;
Charles Keepax572e6c82018-04-24 16:39:01 +0100135 int ret;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000136
137 if (cstream->direction == SND_COMPRESS_PLAYBACK)
138 stream = SNDRV_PCM_STREAM_PLAYBACK;
139 else
140 stream = SNDRV_PCM_STREAM_CAPTURE;
141
142 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
143
Vinod Koul2e622ae2016-11-13 12:10:02 +0530144 if (cpu_dai->driver->cops && cpu_dai->driver->cops->startup) {
145 ret = cpu_dai->driver->cops->startup(cstream, cpu_dai);
146 if (ret < 0) {
Charles Keepax141dfc92018-01-26 13:08:45 +0000147 dev_err(cpu_dai->dev,
148 "Compress ASoC: can't open interface %s: %d\n",
Vinod Koul2e622ae2016-11-13 12:10:02 +0530149 cpu_dai->name, ret);
150 goto out;
151 }
152 }
153
Charles Keepax1e57b822018-04-24 16:39:03 +0100154 ret = soc_compr_components_open(cstream, &component);
155 if (ret < 0)
156 goto machine_err;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000157
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000158 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->startup) {
159 ret = fe->dai_link->compr_ops->startup(cstream);
160 if (ret < 0) {
Charles Keepax141dfc92018-01-26 13:08:45 +0000161 pr_err("Compress ASoC: %s startup failed: %d\n",
162 fe->dai_link->name, ret);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000163 goto machine_err;
164 }
165 }
166
167 fe->dpcm[stream].runtime = fe_substream->runtime;
168
Qiao Zhou8f70e512014-09-10 17:54:07 +0800169 ret = dpcm_path_get(fe, stream, &list);
Qiao Zhou2e4ec1c2014-09-12 09:41:31 +0800170 if (ret < 0)
Qiao Zhou8f70e512014-09-10 17:54:07 +0800171 goto fe_err;
Qiao Zhou2e4ec1c2014-09-12 09:41:31 +0800172 else if (ret == 0)
Charles Keepax141dfc92018-01-26 13:08:45 +0000173 dev_dbg(fe->dev, "Compress ASoC: %s no valid %s route\n",
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000174 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000175
176 /* calculate valid and active FE <-> BE dpcms */
177 dpcm_process_paths(fe, stream, &list, 1);
178
179 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
180
181 ret = dpcm_be_dai_startup(fe, stream);
182 if (ret < 0) {
183 /* clean up all links */
184 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
185 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
186
187 dpcm_be_disconnect(fe, stream);
188 fe->dpcm[stream].runtime = NULL;
Charles Keepaxb0f12c62016-08-16 11:24:46 +0100189 goto path_err;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000190 }
191
192 dpcm_clear_pending_state(fe, stream);
193 dpcm_path_put(&list);
194
195 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
196 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
197
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100198 snd_soc_runtime_activate(fe, stream);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000199
200 mutex_unlock(&fe->card->mutex);
201
202 return 0;
203
Charles Keepaxb0f12c62016-08-16 11:24:46 +0100204path_err:
205 dpcm_path_put(&list);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000206fe_err:
207 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->shutdown)
208 fe->dai_link->compr_ops->shutdown(cstream);
209machine_err:
Charles Keepax1e57b822018-04-24 16:39:03 +0100210 soc_compr_components_free(cstream, component);
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000211
Vinod Koul2e622ae2016-11-13 12:10:02 +0530212 if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
213 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000214out:
215 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
216 mutex_unlock(&fe->card->mutex);
217 return ret;
218}
219
Charles Keepax202c8f72013-01-24 09:44:30 +0000220/*
221 * Power down the audio subsystem pmdown_time msecs after close is called.
222 * This is to ensure there are no pops or clicks in between any music tracks
223 * due to DAPM power cycling.
224 */
225static void close_delayed_work(struct work_struct *work)
226{
227 struct snd_soc_pcm_runtime *rtd =
228 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
229 struct snd_soc_dai *codec_dai = rtd->codec_dai;
230
231 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
232
Charles Keepax141dfc92018-01-26 13:08:45 +0000233 dev_dbg(rtd->dev,
234 "Compress ASoC: pop wq checking: %s status: %s waiting: %s\n",
235 codec_dai->driver->playback.stream_name,
236 codec_dai->playback_active ? "active" : "inactive",
237 rtd->pop_wait ? "yes" : "no");
Charles Keepax202c8f72013-01-24 09:44:30 +0000238
239 /* are we waiting on this codec DAI stream */
240 if (rtd->pop_wait == 1) {
241 rtd->pop_wait = 0;
242 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
243 SND_SOC_DAPM_STREAM_STOP);
244 }
245
246 mutex_unlock(&rtd->pcm_mutex);
247}
248
Namarta Kohli1245b702012-08-16 17:10:41 +0530249static int soc_compr_free(struct snd_compr_stream *cstream)
250{
251 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Namarta Kohli1245b702012-08-16 17:10:41 +0530252 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
253 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100254 int stream;
Namarta Kohli1245b702012-08-16 17:10:41 +0530255
Charles Keepax15e2e612013-01-24 09:44:29 +0000256 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
257
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100258 if (cstream->direction == SND_COMPRESS_PLAYBACK)
259 stream = SNDRV_PCM_STREAM_PLAYBACK;
260 else
261 stream = SNDRV_PCM_STREAM_CAPTURE;
262
263 snd_soc_runtime_deactivate(rtd, stream);
Namarta Kohli1245b702012-08-16 17:10:41 +0530264
Mark Brownda183962013-02-06 15:44:07 +0000265 snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
266
Namarta Kohli1245b702012-08-16 17:10:41 +0530267 if (!cpu_dai->active)
268 cpu_dai->rate = 0;
269
270 if (!codec_dai->active)
271 codec_dai->rate = 0;
272
Namarta Kohli1245b702012-08-16 17:10:41 +0530273 if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->shutdown)
274 rtd->dai_link->compr_ops->shutdown(cstream);
275
Charles Keepax1e57b822018-04-24 16:39:03 +0100276 soc_compr_components_free(cstream, NULL);
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000277
Vinod Koul2e622ae2016-11-13 12:10:02 +0530278 if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
279 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
280
Namarta Kohli1245b702012-08-16 17:10:41 +0530281 if (cstream->direction == SND_COMPRESS_PLAYBACK) {
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100282 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
Namarta Kohli1245b702012-08-16 17:10:41 +0530283 snd_soc_dapm_stream_event(rtd,
Charles Keepax89027d92018-04-26 17:30:07 +0100284 SNDRV_PCM_STREAM_PLAYBACK,
285 SND_SOC_DAPM_STREAM_STOP);
Charles Keepax8c3d2aa2013-01-24 09:44:28 +0000286 } else {
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600287 rtd->pop_wait = 1;
Mark Brown3d24cfe2013-08-09 18:12:29 +0100288 queue_delayed_work(system_power_efficient_wq,
289 &rtd->delayed_work,
290 msecs_to_jiffies(rtd->pmdown_time));
Charles Keepax8c3d2aa2013-01-24 09:44:28 +0000291 }
Namarta Kohli1245b702012-08-16 17:10:41 +0530292 } else {
293 /* capture streams can be powered down now */
294 snd_soc_dapm_stream_event(rtd,
Charles Keepax89027d92018-04-26 17:30:07 +0100295 SNDRV_PCM_STREAM_CAPTURE,
296 SND_SOC_DAPM_STREAM_STOP);
Namarta Kohli1245b702012-08-16 17:10:41 +0530297 }
298
Charles Keepax15e2e612013-01-24 09:44:29 +0000299 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530300 return 0;
301}
302
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000303static int soc_compr_free_fe(struct snd_compr_stream *cstream)
304{
305 struct snd_soc_pcm_runtime *fe = cstream->private_data;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530306 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000307 struct snd_soc_dpcm *dpcm;
308 int stream, ret;
309
310 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
311
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100312 if (cstream->direction == SND_COMPRESS_PLAYBACK)
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000313 stream = SNDRV_PCM_STREAM_PLAYBACK;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100314 else
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000315 stream = SNDRV_PCM_STREAM_CAPTURE;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000316
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100317 snd_soc_runtime_deactivate(fe, stream);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000318
319 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
320
321 ret = dpcm_be_dai_hw_free(fe, stream);
322 if (ret < 0)
Charles Keepax141dfc92018-01-26 13:08:45 +0000323 dev_err(fe->dev, "Compressed ASoC: hw_free failed: %d\n", ret);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000324
325 ret = dpcm_be_dai_shutdown(fe, stream);
326
327 /* mark FE's links ready to prune */
328 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
329 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
330
Daniel Mack15f6b092014-10-19 09:07:35 +0200331 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000332
333 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
334 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
335
336 dpcm_be_disconnect(fe, stream);
337
338 fe->dpcm[stream].runtime = NULL;
339
340 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->shutdown)
341 fe->dai_link->compr_ops->shutdown(cstream);
342
Charles Keepax1e57b822018-04-24 16:39:03 +0100343 soc_compr_components_free(cstream, NULL);
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000344
Vinod Koul2e622ae2016-11-13 12:10:02 +0530345 if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
346 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
347
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000348 mutex_unlock(&fe->card->mutex);
349 return 0;
350}
351
Namarta Kohli1245b702012-08-16 17:10:41 +0530352static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd)
353{
354
355 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000356 struct snd_soc_component *component;
357 struct snd_soc_rtdcom_list *rtdcom;
Namarta Kohli1245b702012-08-16 17:10:41 +0530358 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530359 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000360 int ret = 0, __ret;
Namarta Kohli1245b702012-08-16 17:10:41 +0530361
Charles Keepax15e2e612013-01-24 09:44:29 +0000362 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
363
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000364 for_each_rtdcom(rtd, rtdcom) {
365 component = rtdcom->component;
366
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000367 if (!component->driver->compr_ops ||
368 !component->driver->compr_ops->trigger)
369 continue;
370
371 __ret = component->driver->compr_ops->trigger(cstream, cmd);
372 if (__ret < 0)
373 ret = __ret;
374 }
375 if (ret < 0)
376 goto out;
377
Vinod Koul2e622ae2016-11-13 12:10:02 +0530378 if (cpu_dai->driver->cops && cpu_dai->driver->cops->trigger)
379 cpu_dai->driver->cops->trigger(cstream, cmd, cpu_dai);
380
Mark Brownda183962013-02-06 15:44:07 +0000381 switch (cmd) {
382 case SNDRV_PCM_TRIGGER_START:
383 snd_soc_dai_digital_mute(codec_dai, 0, cstream->direction);
384 break;
385 case SNDRV_PCM_TRIGGER_STOP:
386 snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
387 break;
Mark Browne38b9b72013-02-06 13:52:42 +0000388 }
Namarta Kohli1245b702012-08-16 17:10:41 +0530389
Charles Keepax15e2e612013-01-24 09:44:29 +0000390out:
391 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530392 return ret;
393}
394
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000395static int soc_compr_trigger_fe(struct snd_compr_stream *cstream, int cmd)
396{
397 struct snd_soc_pcm_runtime *fe = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000398 struct snd_soc_component *component;
399 struct snd_soc_rtdcom_list *rtdcom;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530400 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000401 int ret = 0, __ret, stream;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000402
403 if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN ||
404 cmd == SND_COMPR_TRIGGER_DRAIN) {
405
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000406 for_each_rtdcom(fe, rtdcom) {
407 component = rtdcom->component;
408
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000409 if (!component->driver->compr_ops ||
410 !component->driver->compr_ops->trigger)
411 continue;
412
413 __ret = component->driver->compr_ops->trigger(cstream, cmd);
414 if (__ret < 0)
415 ret = __ret;
416 }
417 return ret;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000418 }
419
420 if (cstream->direction == SND_COMPRESS_PLAYBACK)
421 stream = SNDRV_PCM_STREAM_PLAYBACK;
422 else
423 stream = SNDRV_PCM_STREAM_CAPTURE;
424
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000425 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
426
Vinod Koul2e622ae2016-11-13 12:10:02 +0530427 if (cpu_dai->driver->cops && cpu_dai->driver->cops->trigger) {
428 ret = cpu_dai->driver->cops->trigger(cstream, cmd, cpu_dai);
429 if (ret < 0)
430 goto out;
431 }
432
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000433 for_each_rtdcom(fe, rtdcom) {
434 component = rtdcom->component;
435
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000436 if (!component->driver->compr_ops ||
437 !component->driver->compr_ops->trigger)
438 continue;
439
440 __ret = component->driver->compr_ops->trigger(cstream, cmd);
441 if (__ret < 0)
442 ret = __ret;
443 }
444 if (ret < 0)
445 goto out;
446
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
Namarta Kohli1245b702012-08-16 17:10:41 +0530472static int soc_compr_set_params(struct snd_compr_stream *cstream,
473 struct snd_compr_params *params)
474{
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;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530478 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000479 int ret = 0, __ret;
Namarta Kohli1245b702012-08-16 17:10:41 +0530480
Charles Keepax15e2e612013-01-24 09:44:29 +0000481 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
482
Charles Keepaxef050be2018-04-24 16:39:02 +0100483 /*
484 * First we call set_params for the CPU DAI, then the component
485 * driver this should configure the SoC side. If the machine has
486 * compressed ops then we call that as well. The expectation is
487 * that these callbacks will configure everything for this compress
488 * path, like configuring a PCM port for a CODEC.
Namarta Kohli1245b702012-08-16 17:10:41 +0530489 */
Vinod Koul2e622ae2016-11-13 12:10:02 +0530490 if (cpu_dai->driver->cops && cpu_dai->driver->cops->set_params) {
491 ret = cpu_dai->driver->cops->set_params(cstream, params, cpu_dai);
492 if (ret < 0)
493 goto err;
494 }
495
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000496 for_each_rtdcom(rtd, rtdcom) {
497 component = rtdcom->component;
498
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000499 if (!component->driver->compr_ops ||
500 !component->driver->compr_ops->set_params)
501 continue;
502
503 __ret = component->driver->compr_ops->set_params(cstream, params);
504 if (__ret < 0)
505 ret = __ret;
506 }
507 if (ret < 0)
508 goto err;
509
Namarta Kohli1245b702012-08-16 17:10:41 +0530510 if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->set_params) {
511 ret = rtd->dai_link->compr_ops->set_params(cstream);
512 if (ret < 0)
Charles Keepaxfa40ef22013-03-27 16:39:01 +0000513 goto err;
Namarta Kohli1245b702012-08-16 17:10:41 +0530514 }
515
Charles Keepax2c071ed2013-05-20 08:33:54 +0100516 if (cstream->direction == SND_COMPRESS_PLAYBACK)
517 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Charles Keepax89027d92018-04-26 17:30:07 +0100518 SND_SOC_DAPM_STREAM_START);
Charles Keepax2c071ed2013-05-20 08:33:54 +0100519 else
520 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Charles Keepax89027d92018-04-26 17:30:07 +0100521 SND_SOC_DAPM_STREAM_START);
Namarta Kohli1245b702012-08-16 17:10:41 +0530522
Charles Keepaxfa40ef22013-03-27 16:39:01 +0000523 /* cancel any delayed stream shutdown that is pending */
524 rtd->pop_wait = 0;
525 mutex_unlock(&rtd->pcm_mutex);
526
527 cancel_delayed_work_sync(&rtd->delayed_work);
528
529 return ret;
530
531err:
Charles Keepax15e2e612013-01-24 09:44:29 +0000532 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530533 return ret;
534}
535
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000536static int soc_compr_set_params_fe(struct snd_compr_stream *cstream,
Charles Keepax89027d92018-04-26 17:30:07 +0100537 struct snd_compr_params *params)
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000538{
539 struct snd_soc_pcm_runtime *fe = cstream->private_data;
Satish Babu Patakokila01b8ced2017-06-16 17:33:40 -0700540 struct snd_pcm_substream *fe_substream =
541 fe->pcm->streams[cstream->direction].substream;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000542 struct snd_soc_component *component;
543 struct snd_soc_rtdcom_list *rtdcom;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530544 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000545 int ret = 0, __ret, stream;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000546
547 if (cstream->direction == SND_COMPRESS_PLAYBACK)
548 stream = SNDRV_PCM_STREAM_PLAYBACK;
549 else
550 stream = SNDRV_PCM_STREAM_CAPTURE;
551
552 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
553
Vinod Koul2e622ae2016-11-13 12:10:02 +0530554 if (cpu_dai->driver->cops && cpu_dai->driver->cops->set_params) {
555 ret = cpu_dai->driver->cops->set_params(cstream, params, cpu_dai);
556 if (ret < 0)
557 goto out;
558 }
559
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000560 for_each_rtdcom(fe, rtdcom) {
561 component = rtdcom->component;
562
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000563 if (!component->driver->compr_ops ||
564 !component->driver->compr_ops->set_params)
565 continue;
566
567 __ret = component->driver->compr_ops->set_params(cstream, params);
568 if (__ret < 0)
569 ret = __ret;
570 }
571 if (ret < 0)
572 goto out;
573
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000574 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->set_params) {
575 ret = fe->dai_link->compr_ops->set_params(cstream);
576 if (ret < 0)
577 goto out;
578 }
579
580 /*
581 * Create an empty hw_params for the BE as the machine driver must
582 * fix this up to match DSP decoder and ASRC configuration.
583 * I.e. machine driver fixup for compressed BE is mandatory.
584 */
585 memset(&fe->dpcm[fe_substream->stream].hw_params, 0,
586 sizeof(struct snd_pcm_hw_params));
587
588 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
589
590 ret = dpcm_be_dai_hw_params(fe, stream);
591 if (ret < 0)
592 goto out;
593
594 ret = dpcm_be_dai_prepare(fe, stream);
595 if (ret < 0)
596 goto out;
597
Daniel Mack15f6b092014-10-19 09:07:35 +0200598 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000599 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
600
601out:
602 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
603 mutex_unlock(&fe->card->mutex);
604 return ret;
605}
606
Namarta Kohli1245b702012-08-16 17:10:41 +0530607static int soc_compr_get_params(struct snd_compr_stream *cstream,
Charles Keepax89027d92018-04-26 17:30:07 +0100608 struct snd_codec *params)
Namarta Kohli1245b702012-08-16 17:10:41 +0530609{
610 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000611 struct snd_soc_component *component;
612 struct snd_soc_rtdcom_list *rtdcom;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530613 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000614 int ret = 0, __ret;
Namarta Kohli1245b702012-08-16 17:10:41 +0530615
Charles Keepax15e2e612013-01-24 09:44:29 +0000616 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
617
Vinod Koul2e622ae2016-11-13 12:10:02 +0530618 if (cpu_dai->driver->cops && cpu_dai->driver->cops->get_params) {
619 ret = cpu_dai->driver->cops->get_params(cstream, params, cpu_dai);
620 if (ret < 0)
621 goto err;
622 }
623
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000624 for_each_rtdcom(rtd, rtdcom) {
625 component = rtdcom->component;
626
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000627 if (!component->driver->compr_ops ||
628 !component->driver->compr_ops->get_params)
629 continue;
630
631 __ret = component->driver->compr_ops->get_params(cstream, params);
632 if (__ret < 0)
633 ret = __ret;
634 }
Namarta Kohli1245b702012-08-16 17:10:41 +0530635
Vinod Koul2e622ae2016-11-13 12:10:02 +0530636err:
Charles Keepax15e2e612013-01-24 09:44:29 +0000637 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530638 return ret;
639}
640
641static int soc_compr_get_caps(struct snd_compr_stream *cstream,
Charles Keepax89027d92018-04-26 17:30:07 +0100642 struct snd_compr_caps *caps)
Namarta Kohli1245b702012-08-16 17:10:41 +0530643{
644 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000645 struct snd_soc_component *component;
646 struct snd_soc_rtdcom_list *rtdcom;
647 int ret = 0, __ret;
Namarta Kohli1245b702012-08-16 17:10:41 +0530648
Charles Keepax15e2e612013-01-24 09:44:29 +0000649 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
650
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000651 for_each_rtdcom(rtd, rtdcom) {
652 component = rtdcom->component;
653
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000654 if (!component->driver->compr_ops ||
655 !component->driver->compr_ops->get_caps)
656 continue;
657
658 __ret = component->driver->compr_ops->get_caps(cstream, caps);
659 if (__ret < 0)
660 ret = __ret;
661 }
662
Charles Keepax15e2e612013-01-24 09:44:29 +0000663 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530664 return ret;
665}
666
667static int soc_compr_get_codec_caps(struct snd_compr_stream *cstream,
Charles Keepax89027d92018-04-26 17:30:07 +0100668 struct snd_compr_codec_caps *codec)
Namarta Kohli1245b702012-08-16 17:10:41 +0530669{
670 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000671 struct snd_soc_component *component;
672 struct snd_soc_rtdcom_list *rtdcom;
673 int ret = 0, __ret;
Namarta Kohli1245b702012-08-16 17:10:41 +0530674
Charles Keepax15e2e612013-01-24 09:44:29 +0000675 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
676
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000677 for_each_rtdcom(rtd, rtdcom) {
678 component = rtdcom->component;
679
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000680 if (!component->driver->compr_ops ||
681 !component->driver->compr_ops->get_codec_caps)
682 continue;
683
684 __ret = component->driver->compr_ops->get_codec_caps(cstream, codec);
685 if (__ret < 0)
686 ret = __ret;
687 }
688
Charles Keepax15e2e612013-01-24 09:44:29 +0000689 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530690 return ret;
691}
692
693static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
694{
695 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000696 struct snd_soc_component *component;
697 struct snd_soc_rtdcom_list *rtdcom;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530698 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000699 int ret = 0, __ret;
Namarta Kohli1245b702012-08-16 17:10:41 +0530700
Charles Keepax15e2e612013-01-24 09:44:29 +0000701 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
702
Vinod Koul2e622ae2016-11-13 12:10:02 +0530703 if (cpu_dai->driver->cops && cpu_dai->driver->cops->ack) {
704 ret = cpu_dai->driver->cops->ack(cstream, bytes, cpu_dai);
705 if (ret < 0)
706 goto err;
707 }
708
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000709 for_each_rtdcom(rtd, rtdcom) {
710 component = rtdcom->component;
711
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000712 if (!component->driver->compr_ops ||
713 !component->driver->compr_ops->ack)
714 continue;
715
716 __ret = component->driver->compr_ops->ack(cstream, bytes);
717 if (__ret < 0)
718 ret = __ret;
719 }
Namarta Kohli1245b702012-08-16 17:10:41 +0530720
Vinod Koul2e622ae2016-11-13 12:10:02 +0530721err:
Charles Keepax15e2e612013-01-24 09:44:29 +0000722 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530723 return ret;
724}
725
726static int soc_compr_pointer(struct snd_compr_stream *cstream,
Charles Keepax89027d92018-04-26 17:30:07 +0100727 struct snd_compr_tstamp *tstamp)
Namarta Kohli1245b702012-08-16 17:10:41 +0530728{
729 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000730 struct snd_soc_component *component;
731 struct snd_soc_rtdcom_list *rtdcom;
732 int ret = 0, __ret;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530733 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Namarta Kohli1245b702012-08-16 17:10:41 +0530734
Charles Keepax15e2e612013-01-24 09:44:29 +0000735 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
736
Vinod Koul2e622ae2016-11-13 12:10:02 +0530737 if (cpu_dai->driver->cops && cpu_dai->driver->cops->pointer)
738 cpu_dai->driver->cops->pointer(cstream, tstamp, cpu_dai);
739
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000740 for_each_rtdcom(rtd, rtdcom) {
741 component = rtdcom->component;
742
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000743 if (!component->driver->compr_ops ||
744 !component->driver->compr_ops->pointer)
745 continue;
746
747 __ret = component->driver->compr_ops->pointer(cstream, tstamp);
748 if (__ret < 0)
749 ret = __ret;
750 }
751
Charles Keepax15e2e612013-01-24 09:44:29 +0000752 mutex_unlock(&rtd->pcm_mutex);
Charles Keepax7c9190f2016-06-20 09:51:32 +0100753 return ret;
Namarta Kohli1245b702012-08-16 17:10:41 +0530754}
755
Charles Keepax1f88eb02013-02-05 10:41:47 +0000756static int soc_compr_copy(struct snd_compr_stream *cstream,
Charles Keepax4daf8912013-04-18 11:01:38 +0100757 char __user *buf, size_t count)
Charles Keepax1f88eb02013-02-05 10:41:47 +0000758{
759 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000760 struct snd_soc_component *component;
761 struct snd_soc_rtdcom_list *rtdcom;
Charles Keepax290df4d32018-01-26 13:08:43 +0000762 int ret = 0;
Charles Keepax1f88eb02013-02-05 10:41:47 +0000763
764 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
765
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000766 for_each_rtdcom(rtd, rtdcom) {
767 component = rtdcom->component;
768
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000769 if (!component->driver->compr_ops ||
770 !component->driver->compr_ops->copy)
771 continue;
772
Charles Keepax290df4d32018-01-26 13:08:43 +0000773 ret = component->driver->compr_ops->copy(cstream, buf, count);
774 break;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000775 }
Charles Keepax290df4d32018-01-26 13:08:43 +0000776
Charles Keepax1f88eb02013-02-05 10:41:47 +0000777 mutex_unlock(&rtd->pcm_mutex);
778 return ret;
779}
780
Vinod Koul02bd90e2013-07-28 20:06:15 +0530781static int soc_compr_set_metadata(struct snd_compr_stream *cstream,
Charles Keepax89027d92018-04-26 17:30:07 +0100782 struct snd_compr_metadata *metadata)
Jeeja KP36953d92013-03-26 21:22:28 +0530783{
784 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000785 struct snd_soc_component *component;
786 struct snd_soc_rtdcom_list *rtdcom;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530787 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000788 int ret = 0, __ret;
Jeeja KP36953d92013-03-26 21:22:28 +0530789
Vinod Koul2e622ae2016-11-13 12:10:02 +0530790 if (cpu_dai->driver->cops && cpu_dai->driver->cops->set_metadata) {
791 ret = cpu_dai->driver->cops->set_metadata(cstream, metadata, cpu_dai);
792 if (ret < 0)
793 return ret;
794 }
795
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000796 for_each_rtdcom(rtd, rtdcom) {
797 component = rtdcom->component;
798
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000799 if (!component->driver->compr_ops ||
800 !component->driver->compr_ops->set_metadata)
801 continue;
802
803 __ret = component->driver->compr_ops->set_metadata(cstream, metadata);
804 if (__ret < 0)
805 ret = __ret;
806 }
Jeeja KP36953d92013-03-26 21:22:28 +0530807
808 return ret;
809}
810
Vinod Koul02bd90e2013-07-28 20:06:15 +0530811static int soc_compr_get_metadata(struct snd_compr_stream *cstream,
Charles Keepax89027d92018-04-26 17:30:07 +0100812 struct snd_compr_metadata *metadata)
Jeeja KP36953d92013-03-26 21:22:28 +0530813{
814 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000815 struct snd_soc_component *component;
816 struct snd_soc_rtdcom_list *rtdcom;
Vinod Koul2e622ae2016-11-13 12:10:02 +0530817 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000818 int ret = 0, __ret;
Jeeja KP36953d92013-03-26 21:22:28 +0530819
Vinod Koul2e622ae2016-11-13 12:10:02 +0530820 if (cpu_dai->driver->cops && cpu_dai->driver->cops->get_metadata) {
821 ret = cpu_dai->driver->cops->get_metadata(cstream, metadata, cpu_dai);
822 if (ret < 0)
823 return ret;
824 }
825
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000826 for_each_rtdcom(rtd, rtdcom) {
827 component = rtdcom->component;
828
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000829 if (!component->driver->compr_ops ||
830 !component->driver->compr_ops->get_metadata)
831 continue;
832
833 __ret = component->driver->compr_ops->get_metadata(cstream, metadata);
834 if (__ret < 0)
835 ret = __ret;
836 }
Jeeja KP36953d92013-03-26 21:22:28 +0530837
838 return ret;
839}
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000840
Namarta Kohli1245b702012-08-16 17:10:41 +0530841/* ASoC Compress operations */
842static struct snd_compr_ops soc_compr_ops = {
843 .open = soc_compr_open,
844 .free = soc_compr_free,
845 .set_params = soc_compr_set_params,
Vinod Koul02bd90e2013-07-28 20:06:15 +0530846 .set_metadata = soc_compr_set_metadata,
847 .get_metadata = soc_compr_get_metadata,
Namarta Kohli1245b702012-08-16 17:10:41 +0530848 .get_params = soc_compr_get_params,
849 .trigger = soc_compr_trigger,
850 .pointer = soc_compr_pointer,
851 .ack = soc_compr_ack,
852 .get_caps = soc_compr_get_caps,
853 .get_codec_caps = soc_compr_get_codec_caps
854};
855
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000856/* ASoC Dynamic Compress operations */
857static struct snd_compr_ops soc_compr_dyn_ops = {
858 .open = soc_compr_open_fe,
859 .free = soc_compr_free_fe,
860 .set_params = soc_compr_set_params_fe,
861 .get_params = soc_compr_get_params,
862 .set_metadata = soc_compr_set_metadata,
863 .get_metadata = soc_compr_get_metadata,
864 .trigger = soc_compr_trigger_fe,
865 .pointer = soc_compr_pointer,
866 .ack = soc_compr_ack,
867 .get_caps = soc_compr_get_caps,
868 .get_codec_caps = soc_compr_get_codec_caps
869};
870
Jie Yang6f0c4222015-10-13 23:41:00 +0800871/**
872 * snd_soc_new_compress - create a new compress.
873 *
874 * @rtd: The runtime for which we will create compress
875 * @num: the device index number (zero based - shared with normal PCMs)
876 *
877 * Return: 0 for success, else error.
878 */
879int snd_soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
Namarta Kohli1245b702012-08-16 17:10:41 +0530880{
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000881 struct snd_soc_component *component;
882 struct snd_soc_rtdcom_list *rtdcom;
Namarta Kohli1245b702012-08-16 17:10:41 +0530883 struct snd_soc_dai *codec_dai = rtd->codec_dai;
884 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
885 struct snd_compr *compr;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000886 struct snd_pcm *be_pcm;
Namarta Kohli1245b702012-08-16 17:10:41 +0530887 char new_name[64];
888 int ret = 0, direction = 0;
Vinod Koula1068042016-01-07 21:48:14 +0530889 int playback = 0, capture = 0;
Namarta Kohli1245b702012-08-16 17:10:41 +0530890
Benoit Cousson8151d5e2014-07-08 23:19:37 +0200891 if (rtd->num_codecs > 1) {
Charles Keepax141dfc92018-01-26 13:08:45 +0000892 dev_err(rtd->card->dev,
893 "Compress ASoC: Multicodec not supported\n");
Benoit Cousson8151d5e2014-07-08 23:19:37 +0200894 return -EINVAL;
895 }
896
Namarta Kohli1245b702012-08-16 17:10:41 +0530897 /* check client and interface hw capabilities */
Charles Keepaxdaa2db52013-04-18 11:02:38 +0100898 if (codec_dai->driver->playback.channels_min)
Vinod Koula1068042016-01-07 21:48:14 +0530899 playback = 1;
900 if (codec_dai->driver->capture.channels_min)
901 capture = 1;
902
903 capture = capture && cpu_dai->driver->capture.channels_min;
904 playback = playback && cpu_dai->driver->playback.channels_min;
905
906 /*
907 * Compress devices are unidirectional so only one of the directions
908 * should be set, check for that (xor)
909 */
910 if (playback + capture != 1) {
Charles Keepax141dfc92018-01-26 13:08:45 +0000911 dev_err(rtd->card->dev,
912 "Compress ASoC: Invalid direction for P %d, C %d\n",
913 playback, capture);
Charles Keepaxdaa2db52013-04-18 11:02:38 +0100914 return -EINVAL;
Vinod Koula1068042016-01-07 21:48:14 +0530915 }
916
Peng Donglinaeb6fa02017-08-16 22:47:53 +0800917 if (playback)
Vinod Koula1068042016-01-07 21:48:14 +0530918 direction = SND_COMPRESS_PLAYBACK;
919 else
920 direction = SND_COMPRESS_CAPTURE;
Charles Keepaxdaa2db52013-04-18 11:02:38 +0100921
Namarta Kohli1245b702012-08-16 17:10:41 +0530922 compr = kzalloc(sizeof(*compr), GFP_KERNEL);
Markus Elfring7a0cf422017-08-10 16:21:34 +0200923 if (!compr)
Namarta Kohli1245b702012-08-16 17:10:41 +0530924 return -ENOMEM;
Namarta Kohli1245b702012-08-16 17:10:41 +0530925
Charles Keepax1f88eb02013-02-05 10:41:47 +0000926 compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
927 GFP_KERNEL);
Markus Elfring7a0cf422017-08-10 16:21:34 +0200928 if (!compr->ops) {
Charles Keepax1f88eb02013-02-05 10:41:47 +0000929 ret = -ENOMEM;
930 goto compr_err;
931 }
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000932
933 if (rtd->dai_link->dynamic) {
934 snprintf(new_name, sizeof(new_name), "(%s)",
935 rtd->dai_link->stream_name);
936
937 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
Qais Yousefd3268a42015-01-14 08:47:29 +0000938 rtd->dai_link->dpcm_playback,
939 rtd->dai_link->dpcm_capture, &be_pcm);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000940 if (ret < 0) {
Charles Keepax141dfc92018-01-26 13:08:45 +0000941 dev_err(rtd->card->dev,
942 "Compress ASoC: can't create compressed for %s: %d\n",
943 rtd->dai_link->name, ret);
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000944 goto compr_err;
945 }
946
947 rtd->pcm = be_pcm;
948 rtd->fe_compr = 1;
Qais Yousefd3268a42015-01-14 08:47:29 +0000949 if (rtd->dai_link->dpcm_playback)
950 be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
951 else if (rtd->dai_link->dpcm_capture)
952 be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000953 memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
Peng Donglinaeb6fa02017-08-16 22:47:53 +0800954 } else {
955 snprintf(new_name, sizeof(new_name), "%s %s-%d",
956 rtd->dai_link->stream_name, codec_dai->name, num);
957
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000958 memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
Peng Donglinaeb6fa02017-08-16 22:47:53 +0800959 }
Charles Keepax1f88eb02013-02-05 10:41:47 +0000960
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000961 for_each_rtdcom(rtd, rtdcom) {
962 component = rtdcom->component;
963
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000964 if (!component->driver->compr_ops ||
965 !component->driver->compr_ops->copy)
966 continue;
967
968 compr->ops->copy = soc_compr_copy;
Charles Keepaxca76db62018-04-26 17:30:04 +0100969 break;
Kuninori Morimoto9e7e3732017-10-11 01:37:45 +0000970 }
971
Namarta Kohli1245b702012-08-16 17:10:41 +0530972 mutex_init(&compr->lock);
Richard Fitzgeralde5241a82015-11-25 13:00:24 +0000973 ret = snd_compress_new(rtd->card->snd_card, num, direction,
974 new_name, compr);
Namarta Kohli1245b702012-08-16 17:10:41 +0530975 if (ret < 0) {
Kuninori Morimotoe5acfc72017-12-05 04:23:05 +0000976 component = rtd->codec_dai->component;
Charles Keepax141dfc92018-01-26 13:08:45 +0000977 dev_err(component->dev,
978 "Compress ASoC: can't create compress for codec %s: %d\n",
979 component->name, ret);
Charles Keepax1f88eb02013-02-05 10:41:47 +0000980 goto compr_err;
Namarta Kohli1245b702012-08-16 17:10:41 +0530981 }
982
Charles Keepax202c8f72013-01-24 09:44:30 +0000983 /* DAPM dai link stream work */
984 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
985
Namarta Kohli1245b702012-08-16 17:10:41 +0530986 rtd->compr = compr;
987 compr->private_data = rtd;
988
Charles Keepax141dfc92018-01-26 13:08:45 +0000989 dev_info(rtd->card->dev, "Compress ASoC: %s <-> %s mapping ok\n",
990 codec_dai->name, cpu_dai->name);
Namarta Kohli1245b702012-08-16 17:10:41 +0530991 return ret;
Charles Keepax1f88eb02013-02-05 10:41:47 +0000992
993compr_err:
994 kfree(compr);
995 return ret;
Namarta Kohli1245b702012-08-16 17:10:41 +0530996}
Jie Yang6f0c4222015-10-13 23:41:00 +0800997EXPORT_SYMBOL_GPL(snd_soc_new_compress);