Merge tag 'udf_for_v4.18-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/jack...
[linux-2.6-block.git] / sound / soc / soc-compress.c
CommitLineData
1245b700
NK
1/*
2 * soc-compress.c -- ALSA SoC Compress
3 *
4 * Copyright (C) 2012 Intel Corp.
5 *
6 * Authors: Namarta Kohli <namartax.kohli@intel.com>
7 * Ramesh Babu K V <ramesh.babu@linux.intel.com>
8 * Vinod Koul <vinod.koul@linux.intel.com>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 *
15 */
16
17#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/delay.h>
20#include <linux/slab.h>
21#include <linux/workqueue.h>
22#include <sound/core.h>
23#include <sound/compress_params.h>
24#include <sound/compress_driver.h>
25#include <sound/soc.h>
26#include <sound/initval.h>
2a99ef0f 27#include <sound/soc-dpcm.h>
1245b700 28
1e57b828
CK
29static int soc_compr_components_open(struct snd_compr_stream *cstream,
30 struct snd_soc_component **last)
1245b700
NK
31{
32 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
9e7e3738
KM
33 struct snd_soc_component *component;
34 struct snd_soc_rtdcom_list *rtdcom;
572e6c8d 35 int ret;
1245b700 36
9e7e3738
KM
37 for_each_rtdcom(rtd, rtdcom) {
38 component = rtdcom->component;
39
9e7e3738
KM
40 if (!component->driver->compr_ops ||
41 !component->driver->compr_ops->open)
42 continue;
43
572e6c8d
CK
44 ret = component->driver->compr_ops->open(cstream);
45 if (ret < 0) {
141dfc9e
CK
46 dev_err(component->dev,
47 "Compress ASoC: can't open platform %s: %d\n",
572e6c8d 48 component->name, ret);
1e57b828
CK
49
50 *last = component;
51 return ret;
52 }
53 }
54
55 *last = NULL;
56 return 0;
57}
58
59static int soc_compr_components_free(struct snd_compr_stream *cstream,
60 struct snd_soc_component *last)
61{
62 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
63 struct snd_soc_component *component;
64 struct snd_soc_rtdcom_list *rtdcom;
65
66 for_each_rtdcom(rtd, rtdcom) {
67 component = rtdcom->component;
68
69 if (component == last)
70 break;
71
72 if (!component->driver->compr_ops ||
73 !component->driver->compr_ops->free)
74 continue;
75
76 component->driver->compr_ops->free(cstream);
77 }
78
79 return 0;
80}
81
82static int soc_compr_open(struct snd_compr_stream *cstream)
83{
84 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
85 struct snd_soc_component *component;
86 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
87 int ret;
88
89 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
90
91 if (cpu_dai->driver->cops && cpu_dai->driver->cops->startup) {
92 ret = cpu_dai->driver->cops->startup(cstream, cpu_dai);
93 if (ret < 0) {
94 dev_err(cpu_dai->dev,
95 "Compress ASoC: can't open interface %s: %d\n",
96 cpu_dai->name, ret);
97 goto out;
9e7e3738
KM
98 }
99 }
1e57b828
CK
100
101 ret = soc_compr_components_open(cstream, &component);
102 if (ret < 0)
103 goto machine_err;
9e7e3738 104
1245b700
NK
105 if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->startup) {
106 ret = rtd->dai_link->compr_ops->startup(cstream);
107 if (ret < 0) {
141dfc9e
CK
108 dev_err(rtd->dev,
109 "Compress ASoC: %s startup failed: %d\n",
110 rtd->dai_link->name, ret);
1245b700
NK
111 goto machine_err;
112 }
113 }
114
24894b76 115 snd_soc_runtime_activate(rtd, cstream->direction);
1245b700 116
15e2e619
CK
117 mutex_unlock(&rtd->pcm_mutex);
118
1245b700
NK
119 return 0;
120
121machine_err:
1e57b828 122 soc_compr_components_free(cstream, component);
9e7e3738 123
2e622ae4
VK
124 if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
125 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
1245b700 126out:
15e2e619 127 mutex_unlock(&rtd->pcm_mutex);
1245b700
NK
128 return ret;
129}
130
2a99ef0f
LG
131static int soc_compr_open_fe(struct snd_compr_stream *cstream)
132{
133 struct snd_soc_pcm_runtime *fe = cstream->private_data;
01b8cedf
SBP
134 struct snd_pcm_substream *fe_substream =
135 fe->pcm->streams[cstream->direction].substream;
9e7e3738 136 struct snd_soc_component *component;
2e622ae4 137 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
2a99ef0f
LG
138 struct snd_soc_dpcm *dpcm;
139 struct snd_soc_dapm_widget_list *list;
140 int stream;
572e6c8d 141 int ret;
2a99ef0f
LG
142
143 if (cstream->direction == SND_COMPRESS_PLAYBACK)
144 stream = SNDRV_PCM_STREAM_PLAYBACK;
145 else
146 stream = SNDRV_PCM_STREAM_CAPTURE;
147
148 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
149
2e622ae4
VK
150 if (cpu_dai->driver->cops && cpu_dai->driver->cops->startup) {
151 ret = cpu_dai->driver->cops->startup(cstream, cpu_dai);
152 if (ret < 0) {
141dfc9e
CK
153 dev_err(cpu_dai->dev,
154 "Compress ASoC: can't open interface %s: %d\n",
2e622ae4
VK
155 cpu_dai->name, ret);
156 goto out;
157 }
158 }
159
1e57b828
CK
160 ret = soc_compr_components_open(cstream, &component);
161 if (ret < 0)
162 goto machine_err;
9e7e3738 163
2a99ef0f
LG
164 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->startup) {
165 ret = fe->dai_link->compr_ops->startup(cstream);
166 if (ret < 0) {
141dfc9e
CK
167 pr_err("Compress ASoC: %s startup failed: %d\n",
168 fe->dai_link->name, ret);
2a99ef0f
LG
169 goto machine_err;
170 }
171 }
172
173 fe->dpcm[stream].runtime = fe_substream->runtime;
174
8f70e515 175 ret = dpcm_path_get(fe, stream, &list);
2e4ec1c0 176 if (ret < 0)
8f70e515 177 goto fe_err;
2e4ec1c0 178 else if (ret == 0)
141dfc9e 179 dev_dbg(fe->dev, "Compress ASoC: %s no valid %s route\n",
2a99ef0f 180 fe->dai_link->name, stream ? "capture" : "playback");
2a99ef0f
LG
181
182 /* calculate valid and active FE <-> BE dpcms */
183 dpcm_process_paths(fe, stream, &list, 1);
184
185 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
186
187 ret = dpcm_be_dai_startup(fe, stream);
188 if (ret < 0) {
189 /* clean up all links */
190 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
191 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
192
193 dpcm_be_disconnect(fe, stream);
194 fe->dpcm[stream].runtime = NULL;
b0f12c61 195 goto path_err;
2a99ef0f
LG
196 }
197
198 dpcm_clear_pending_state(fe, stream);
199 dpcm_path_put(&list);
200
201 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
202 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
203
24894b76 204 snd_soc_runtime_activate(fe, stream);
2a99ef0f
LG
205
206 mutex_unlock(&fe->card->mutex);
207
208 return 0;
209
b0f12c61
CK
210path_err:
211 dpcm_path_put(&list);
2a99ef0f
LG
212fe_err:
213 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->shutdown)
214 fe->dai_link->compr_ops->shutdown(cstream);
215machine_err:
1e57b828 216 soc_compr_components_free(cstream, component);
9e7e3738 217
2e622ae4
VK
218 if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
219 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
2a99ef0f
LG
220out:
221 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
222 mutex_unlock(&fe->card->mutex);
223 return ret;
224}
225
202c8f70
CK
226/*
227 * Power down the audio subsystem pmdown_time msecs after close is called.
228 * This is to ensure there are no pops or clicks in between any music tracks
229 * due to DAPM power cycling.
230 */
231static void close_delayed_work(struct work_struct *work)
232{
233 struct snd_soc_pcm_runtime *rtd =
234 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
235 struct snd_soc_dai *codec_dai = rtd->codec_dai;
236
237 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
238
141dfc9e
CK
239 dev_dbg(rtd->dev,
240 "Compress ASoC: pop wq checking: %s status: %s waiting: %s\n",
241 codec_dai->driver->playback.stream_name,
242 codec_dai->playback_active ? "active" : "inactive",
243 rtd->pop_wait ? "yes" : "no");
202c8f70
CK
244
245 /* are we waiting on this codec DAI stream */
246 if (rtd->pop_wait == 1) {
247 rtd->pop_wait = 0;
248 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
249 SND_SOC_DAPM_STREAM_STOP);
250 }
251
252 mutex_unlock(&rtd->pcm_mutex);
253}
254
1245b700
NK
255static int soc_compr_free(struct snd_compr_stream *cstream)
256{
257 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
1245b700
NK
258 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
259 struct snd_soc_dai *codec_dai = rtd->codec_dai;
24894b76 260 int stream;
1245b700 261
15e2e619
CK
262 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
263
24894b76
LPC
264 if (cstream->direction == SND_COMPRESS_PLAYBACK)
265 stream = SNDRV_PCM_STREAM_PLAYBACK;
266 else
267 stream = SNDRV_PCM_STREAM_CAPTURE;
1245b700 268
24894b76 269 snd_soc_runtime_deactivate(rtd, stream);
da18396f 270
24894b76 271 snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
1245b700
NK
272
273 if (!cpu_dai->active)
274 cpu_dai->rate = 0;
275
276 if (!codec_dai->active)
277 codec_dai->rate = 0;
278
1245b700
NK
279 if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->shutdown)
280 rtd->dai_link->compr_ops->shutdown(cstream);
281
1e57b828 282 soc_compr_components_free(cstream, NULL);
9e7e3738 283
2e622ae4
VK
284 if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
285 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
286
1245b700 287 if (cstream->direction == SND_COMPRESS_PLAYBACK) {
208a1589 288 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
1245b700 289 snd_soc_dapm_stream_event(rtd,
89027d9e
CK
290 SNDRV_PCM_STREAM_PLAYBACK,
291 SND_SOC_DAPM_STREAM_STOP);
8c3d2aa4 292 } else {
9bffb1fb 293 rtd->pop_wait = 1;
3d24cfe4
MB
294 queue_delayed_work(system_power_efficient_wq,
295 &rtd->delayed_work,
296 msecs_to_jiffies(rtd->pmdown_time));
8c3d2aa4 297 }
1245b700
NK
298 } else {
299 /* capture streams can be powered down now */
300 snd_soc_dapm_stream_event(rtd,
89027d9e
CK
301 SNDRV_PCM_STREAM_CAPTURE,
302 SND_SOC_DAPM_STREAM_STOP);
1245b700
NK
303 }
304
15e2e619 305 mutex_unlock(&rtd->pcm_mutex);
1245b700
NK
306 return 0;
307}
308
2a99ef0f
LG
309static int soc_compr_free_fe(struct snd_compr_stream *cstream)
310{
311 struct snd_soc_pcm_runtime *fe = cstream->private_data;
2e622ae4 312 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
2a99ef0f
LG
313 struct snd_soc_dpcm *dpcm;
314 int stream, ret;
315
316 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
317
24894b76 318 if (cstream->direction == SND_COMPRESS_PLAYBACK)
2a99ef0f 319 stream = SNDRV_PCM_STREAM_PLAYBACK;
24894b76 320 else
2a99ef0f 321 stream = SNDRV_PCM_STREAM_CAPTURE;
2a99ef0f 322
24894b76 323 snd_soc_runtime_deactivate(fe, stream);
2a99ef0f
LG
324
325 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
326
327 ret = dpcm_be_dai_hw_free(fe, stream);
328 if (ret < 0)
141dfc9e 329 dev_err(fe->dev, "Compressed ASoC: hw_free failed: %d\n", ret);
2a99ef0f
LG
330
331 ret = dpcm_be_dai_shutdown(fe, stream);
332
333 /* mark FE's links ready to prune */
334 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
335 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
336
15f6b09a 337 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
2a99ef0f
LG
338
339 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
340 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
341
342 dpcm_be_disconnect(fe, stream);
343
344 fe->dpcm[stream].runtime = NULL;
345
346 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->shutdown)
347 fe->dai_link->compr_ops->shutdown(cstream);
348
1e57b828 349 soc_compr_components_free(cstream, NULL);
9e7e3738 350
2e622ae4
VK
351 if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
352 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
353
2a99ef0f
LG
354 mutex_unlock(&fe->card->mutex);
355 return 0;
356}
357
1245b700
NK
358static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd)
359{
360
361 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
9e7e3738
KM
362 struct snd_soc_component *component;
363 struct snd_soc_rtdcom_list *rtdcom;
1245b700 364 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2e622ae4 365 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
9e7e3738 366 int ret = 0, __ret;
1245b700 367
15e2e619
CK
368 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
369
9e7e3738
KM
370 for_each_rtdcom(rtd, rtdcom) {
371 component = rtdcom->component;
372
9e7e3738
KM
373 if (!component->driver->compr_ops ||
374 !component->driver->compr_ops->trigger)
375 continue;
376
377 __ret = component->driver->compr_ops->trigger(cstream, cmd);
378 if (__ret < 0)
379 ret = __ret;
380 }
381 if (ret < 0)
382 goto out;
383
2e622ae4
VK
384 if (cpu_dai->driver->cops && cpu_dai->driver->cops->trigger)
385 cpu_dai->driver->cops->trigger(cstream, cmd, cpu_dai);
386
da18396f
MB
387 switch (cmd) {
388 case SNDRV_PCM_TRIGGER_START:
389 snd_soc_dai_digital_mute(codec_dai, 0, cstream->direction);
390 break;
391 case SNDRV_PCM_TRIGGER_STOP:
392 snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
393 break;
e38b9b74 394 }
1245b700 395
15e2e619
CK
396out:
397 mutex_unlock(&rtd->pcm_mutex);
1245b700
NK
398 return ret;
399}
400
2a99ef0f
LG
401static int soc_compr_trigger_fe(struct snd_compr_stream *cstream, int cmd)
402{
403 struct snd_soc_pcm_runtime *fe = cstream->private_data;
9e7e3738
KM
404 struct snd_soc_component *component;
405 struct snd_soc_rtdcom_list *rtdcom;
2e622ae4 406 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
9e7e3738 407 int ret = 0, __ret, stream;
2a99ef0f
LG
408
409 if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN ||
410 cmd == SND_COMPR_TRIGGER_DRAIN) {
411
9e7e3738
KM
412 for_each_rtdcom(fe, rtdcom) {
413 component = rtdcom->component;
414
9e7e3738
KM
415 if (!component->driver->compr_ops ||
416 !component->driver->compr_ops->trigger)
417 continue;
418
419 __ret = component->driver->compr_ops->trigger(cstream, cmd);
420 if (__ret < 0)
421 ret = __ret;
422 }
423 return ret;
2a99ef0f
LG
424 }
425
426 if (cstream->direction == SND_COMPRESS_PLAYBACK)
427 stream = SNDRV_PCM_STREAM_PLAYBACK;
428 else
429 stream = SNDRV_PCM_STREAM_CAPTURE;
430
2a99ef0f
LG
431 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
432
2e622ae4
VK
433 if (cpu_dai->driver->cops && cpu_dai->driver->cops->trigger) {
434 ret = cpu_dai->driver->cops->trigger(cstream, cmd, cpu_dai);
435 if (ret < 0)
436 goto out;
437 }
438
9e7e3738
KM
439 for_each_rtdcom(fe, rtdcom) {
440 component = rtdcom->component;
441
9e7e3738
KM
442 if (!component->driver->compr_ops ||
443 !component->driver->compr_ops->trigger)
444 continue;
445
446 __ret = component->driver->compr_ops->trigger(cstream, cmd);
447 if (__ret < 0)
448 ret = __ret;
449 }
450 if (ret < 0)
451 goto out;
452
2a99ef0f
LG
453 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
454
455 ret = dpcm_be_dai_trigger(fe, stream, cmd);
456
457 switch (cmd) {
458 case SNDRV_PCM_TRIGGER_START:
459 case SNDRV_PCM_TRIGGER_RESUME:
460 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
461 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
462 break;
463 case SNDRV_PCM_TRIGGER_STOP:
464 case SNDRV_PCM_TRIGGER_SUSPEND:
465 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
466 break;
467 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
468 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
469 break;
470 }
471
472out:
473 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
474 mutex_unlock(&fe->card->mutex);
475 return ret;
476}
477
1245b700
NK
478static int soc_compr_set_params(struct snd_compr_stream *cstream,
479 struct snd_compr_params *params)
480{
481 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
9e7e3738
KM
482 struct snd_soc_component *component;
483 struct snd_soc_rtdcom_list *rtdcom;
2e622ae4 484 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
9e7e3738 485 int ret = 0, __ret;
1245b700 486
15e2e619
CK
487 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
488
ef050bec
CK
489 /*
490 * First we call set_params for the CPU DAI, then the component
491 * driver this should configure the SoC side. If the machine has
492 * compressed ops then we call that as well. The expectation is
493 * that these callbacks will configure everything for this compress
494 * path, like configuring a PCM port for a CODEC.
1245b700 495 */
2e622ae4
VK
496 if (cpu_dai->driver->cops && cpu_dai->driver->cops->set_params) {
497 ret = cpu_dai->driver->cops->set_params(cstream, params, cpu_dai);
498 if (ret < 0)
499 goto err;
500 }
501
9e7e3738
KM
502 for_each_rtdcom(rtd, rtdcom) {
503 component = rtdcom->component;
504
9e7e3738
KM
505 if (!component->driver->compr_ops ||
506 !component->driver->compr_ops->set_params)
507 continue;
508
509 __ret = component->driver->compr_ops->set_params(cstream, params);
510 if (__ret < 0)
511 ret = __ret;
512 }
513 if (ret < 0)
514 goto err;
515
1245b700
NK
516 if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->set_params) {
517 ret = rtd->dai_link->compr_ops->set_params(cstream);
518 if (ret < 0)
fa40ef20 519 goto err;
1245b700
NK
520 }
521
2c071ed7
CK
522 if (cstream->direction == SND_COMPRESS_PLAYBACK)
523 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
89027d9e 524 SND_SOC_DAPM_STREAM_START);
2c071ed7
CK
525 else
526 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
89027d9e 527 SND_SOC_DAPM_STREAM_START);
1245b700 528
fa40ef20
CK
529 /* cancel any delayed stream shutdown that is pending */
530 rtd->pop_wait = 0;
531 mutex_unlock(&rtd->pcm_mutex);
532
533 cancel_delayed_work_sync(&rtd->delayed_work);
534
535 return ret;
536
537err:
15e2e619 538 mutex_unlock(&rtd->pcm_mutex);
1245b700
NK
539 return ret;
540}
541
2a99ef0f 542static int soc_compr_set_params_fe(struct snd_compr_stream *cstream,
89027d9e 543 struct snd_compr_params *params)
2a99ef0f
LG
544{
545 struct snd_soc_pcm_runtime *fe = cstream->private_data;
01b8cedf
SBP
546 struct snd_pcm_substream *fe_substream =
547 fe->pcm->streams[cstream->direction].substream;
9e7e3738
KM
548 struct snd_soc_component *component;
549 struct snd_soc_rtdcom_list *rtdcom;
2e622ae4 550 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
9e7e3738 551 int ret = 0, __ret, stream;
2a99ef0f
LG
552
553 if (cstream->direction == SND_COMPRESS_PLAYBACK)
554 stream = SNDRV_PCM_STREAM_PLAYBACK;
555 else
556 stream = SNDRV_PCM_STREAM_CAPTURE;
557
558 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
559
2e622ae4
VK
560 if (cpu_dai->driver->cops && cpu_dai->driver->cops->set_params) {
561 ret = cpu_dai->driver->cops->set_params(cstream, params, cpu_dai);
562 if (ret < 0)
563 goto out;
564 }
565
9e7e3738
KM
566 for_each_rtdcom(fe, rtdcom) {
567 component = rtdcom->component;
568
9e7e3738
KM
569 if (!component->driver->compr_ops ||
570 !component->driver->compr_ops->set_params)
571 continue;
572
573 __ret = component->driver->compr_ops->set_params(cstream, params);
574 if (__ret < 0)
575 ret = __ret;
576 }
577 if (ret < 0)
578 goto out;
579
2a99ef0f
LG
580 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->set_params) {
581 ret = fe->dai_link->compr_ops->set_params(cstream);
582 if (ret < 0)
583 goto out;
584 }
585
586 /*
587 * Create an empty hw_params for the BE as the machine driver must
588 * fix this up to match DSP decoder and ASRC configuration.
589 * I.e. machine driver fixup for compressed BE is mandatory.
590 */
591 memset(&fe->dpcm[fe_substream->stream].hw_params, 0,
592 sizeof(struct snd_pcm_hw_params));
593
594 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
595
596 ret = dpcm_be_dai_hw_params(fe, stream);
597 if (ret < 0)
598 goto out;
599
600 ret = dpcm_be_dai_prepare(fe, stream);
601 if (ret < 0)
602 goto out;
603
15f6b09a 604 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2a99ef0f
LG
605 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
606
607out:
608 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
609 mutex_unlock(&fe->card->mutex);
610 return ret;
611}
612
1245b700 613static int soc_compr_get_params(struct snd_compr_stream *cstream,
89027d9e 614 struct snd_codec *params)
1245b700
NK
615{
616 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
9e7e3738
KM
617 struct snd_soc_component *component;
618 struct snd_soc_rtdcom_list *rtdcom;
2e622ae4 619 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
9e7e3738 620 int ret = 0, __ret;
1245b700 621
15e2e619
CK
622 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
623
2e622ae4
VK
624 if (cpu_dai->driver->cops && cpu_dai->driver->cops->get_params) {
625 ret = cpu_dai->driver->cops->get_params(cstream, params, cpu_dai);
626 if (ret < 0)
627 goto err;
628 }
629
9e7e3738
KM
630 for_each_rtdcom(rtd, rtdcom) {
631 component = rtdcom->component;
632
9e7e3738
KM
633 if (!component->driver->compr_ops ||
634 !component->driver->compr_ops->get_params)
635 continue;
636
637 __ret = component->driver->compr_ops->get_params(cstream, params);
638 if (__ret < 0)
639 ret = __ret;
640 }
1245b700 641
2e622ae4 642err:
15e2e619 643 mutex_unlock(&rtd->pcm_mutex);
1245b700
NK
644 return ret;
645}
646
647static int soc_compr_get_caps(struct snd_compr_stream *cstream,
89027d9e 648 struct snd_compr_caps *caps)
1245b700
NK
649{
650 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
9e7e3738
KM
651 struct snd_soc_component *component;
652 struct snd_soc_rtdcom_list *rtdcom;
653 int ret = 0, __ret;
1245b700 654
15e2e619
CK
655 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
656
9e7e3738
KM
657 for_each_rtdcom(rtd, rtdcom) {
658 component = rtdcom->component;
659
9e7e3738
KM
660 if (!component->driver->compr_ops ||
661 !component->driver->compr_ops->get_caps)
662 continue;
663
664 __ret = component->driver->compr_ops->get_caps(cstream, caps);
665 if (__ret < 0)
666 ret = __ret;
667 }
668
15e2e619 669 mutex_unlock(&rtd->pcm_mutex);
1245b700
NK
670 return ret;
671}
672
673static int soc_compr_get_codec_caps(struct snd_compr_stream *cstream,
89027d9e 674 struct snd_compr_codec_caps *codec)
1245b700
NK
675{
676 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
9e7e3738
KM
677 struct snd_soc_component *component;
678 struct snd_soc_rtdcom_list *rtdcom;
679 int ret = 0, __ret;
1245b700 680
15e2e619
CK
681 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
682
9e7e3738
KM
683 for_each_rtdcom(rtd, rtdcom) {
684 component = rtdcom->component;
685
9e7e3738
KM
686 if (!component->driver->compr_ops ||
687 !component->driver->compr_ops->get_codec_caps)
688 continue;
689
690 __ret = component->driver->compr_ops->get_codec_caps(cstream, codec);
691 if (__ret < 0)
692 ret = __ret;
693 }
694
15e2e619 695 mutex_unlock(&rtd->pcm_mutex);
1245b700
NK
696 return ret;
697}
698
699static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
700{
701 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
9e7e3738
KM
702 struct snd_soc_component *component;
703 struct snd_soc_rtdcom_list *rtdcom;
2e622ae4 704 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
9e7e3738 705 int ret = 0, __ret;
1245b700 706
15e2e619
CK
707 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
708
2e622ae4
VK
709 if (cpu_dai->driver->cops && cpu_dai->driver->cops->ack) {
710 ret = cpu_dai->driver->cops->ack(cstream, bytes, cpu_dai);
711 if (ret < 0)
712 goto err;
713 }
714
9e7e3738
KM
715 for_each_rtdcom(rtd, rtdcom) {
716 component = rtdcom->component;
717
9e7e3738
KM
718 if (!component->driver->compr_ops ||
719 !component->driver->compr_ops->ack)
720 continue;
721
722 __ret = component->driver->compr_ops->ack(cstream, bytes);
723 if (__ret < 0)
724 ret = __ret;
725 }
1245b700 726
2e622ae4 727err:
15e2e619 728 mutex_unlock(&rtd->pcm_mutex);
1245b700
NK
729 return ret;
730}
731
732static int soc_compr_pointer(struct snd_compr_stream *cstream,
89027d9e 733 struct snd_compr_tstamp *tstamp)
1245b700
NK
734{
735 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
9e7e3738
KM
736 struct snd_soc_component *component;
737 struct snd_soc_rtdcom_list *rtdcom;
738 int ret = 0, __ret;
2e622ae4 739 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1245b700 740
15e2e619
CK
741 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
742
2e622ae4
VK
743 if (cpu_dai->driver->cops && cpu_dai->driver->cops->pointer)
744 cpu_dai->driver->cops->pointer(cstream, tstamp, cpu_dai);
745
9e7e3738
KM
746 for_each_rtdcom(rtd, rtdcom) {
747 component = rtdcom->component;
748
9e7e3738
KM
749 if (!component->driver->compr_ops ||
750 !component->driver->compr_ops->pointer)
751 continue;
1245b700 752
9e7e3738
KM
753 __ret = component->driver->compr_ops->pointer(cstream, tstamp);
754 if (__ret < 0)
755 ret = __ret;
756 }
757
15e2e619 758 mutex_unlock(&rtd->pcm_mutex);
7c9190f7 759 return ret;
1245b700
NK
760}
761
1f88eb0f 762static int soc_compr_copy(struct snd_compr_stream *cstream,
4daf891c 763 char __user *buf, size_t count)
1f88eb0f
CK
764{
765 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
9e7e3738
KM
766 struct snd_soc_component *component;
767 struct snd_soc_rtdcom_list *rtdcom;
290df4d3 768 int ret = 0;
1f88eb0f
CK
769
770 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
771
9e7e3738
KM
772 for_each_rtdcom(rtd, rtdcom) {
773 component = rtdcom->component;
774
9e7e3738
KM
775 if (!component->driver->compr_ops ||
776 !component->driver->compr_ops->copy)
777 continue;
1f88eb0f 778
290df4d3
CK
779 ret = component->driver->compr_ops->copy(cstream, buf, count);
780 break;
9e7e3738 781 }
290df4d3 782
1f88eb0f
CK
783 mutex_unlock(&rtd->pcm_mutex);
784 return ret;
785}
786
02bd90e8 787static int soc_compr_set_metadata(struct snd_compr_stream *cstream,
89027d9e 788 struct snd_compr_metadata *metadata)
36953d98
JK
789{
790 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
9e7e3738
KM
791 struct snd_soc_component *component;
792 struct snd_soc_rtdcom_list *rtdcom;
2e622ae4 793 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
9e7e3738 794 int ret = 0, __ret;
36953d98 795
2e622ae4
VK
796 if (cpu_dai->driver->cops && cpu_dai->driver->cops->set_metadata) {
797 ret = cpu_dai->driver->cops->set_metadata(cstream, metadata, cpu_dai);
798 if (ret < 0)
799 return ret;
800 }
801
9e7e3738
KM
802 for_each_rtdcom(rtd, rtdcom) {
803 component = rtdcom->component;
804
9e7e3738
KM
805 if (!component->driver->compr_ops ||
806 !component->driver->compr_ops->set_metadata)
807 continue;
808
809 __ret = component->driver->compr_ops->set_metadata(cstream, metadata);
810 if (__ret < 0)
811 ret = __ret;
812 }
36953d98
JK
813
814 return ret;
815}
816
02bd90e8 817static int soc_compr_get_metadata(struct snd_compr_stream *cstream,
89027d9e 818 struct snd_compr_metadata *metadata)
36953d98
JK
819{
820 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
9e7e3738
KM
821 struct snd_soc_component *component;
822 struct snd_soc_rtdcom_list *rtdcom;
2e622ae4 823 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
9e7e3738 824 int ret = 0, __ret;
36953d98 825
2e622ae4
VK
826 if (cpu_dai->driver->cops && cpu_dai->driver->cops->get_metadata) {
827 ret = cpu_dai->driver->cops->get_metadata(cstream, metadata, cpu_dai);
828 if (ret < 0)
829 return ret;
830 }
831
9e7e3738
KM
832 for_each_rtdcom(rtd, rtdcom) {
833 component = rtdcom->component;
834
9e7e3738
KM
835 if (!component->driver->compr_ops ||
836 !component->driver->compr_ops->get_metadata)
837 continue;
838
839 __ret = component->driver->compr_ops->get_metadata(cstream, metadata);
840 if (__ret < 0)
841 ret = __ret;
842 }
36953d98
JK
843
844 return ret;
845}
2a99ef0f 846
1245b700
NK
847/* ASoC Compress operations */
848static struct snd_compr_ops soc_compr_ops = {
849 .open = soc_compr_open,
850 .free = soc_compr_free,
851 .set_params = soc_compr_set_params,
02bd90e8
VK
852 .set_metadata = soc_compr_set_metadata,
853 .get_metadata = soc_compr_get_metadata,
1245b700
NK
854 .get_params = soc_compr_get_params,
855 .trigger = soc_compr_trigger,
856 .pointer = soc_compr_pointer,
857 .ack = soc_compr_ack,
858 .get_caps = soc_compr_get_caps,
859 .get_codec_caps = soc_compr_get_codec_caps
860};
861
2a99ef0f
LG
862/* ASoC Dynamic Compress operations */
863static struct snd_compr_ops soc_compr_dyn_ops = {
864 .open = soc_compr_open_fe,
865 .free = soc_compr_free_fe,
866 .set_params = soc_compr_set_params_fe,
867 .get_params = soc_compr_get_params,
868 .set_metadata = soc_compr_set_metadata,
869 .get_metadata = soc_compr_get_metadata,
870 .trigger = soc_compr_trigger_fe,
871 .pointer = soc_compr_pointer,
872 .ack = soc_compr_ack,
873 .get_caps = soc_compr_get_caps,
874 .get_codec_caps = soc_compr_get_codec_caps
875};
876
6f0c4226
JY
877/**
878 * snd_soc_new_compress - create a new compress.
879 *
880 * @rtd: The runtime for which we will create compress
881 * @num: the device index number (zero based - shared with normal PCMs)
882 *
883 * Return: 0 for success, else error.
884 */
885int snd_soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
1245b700 886{
9e7e3738
KM
887 struct snd_soc_component *component;
888 struct snd_soc_rtdcom_list *rtdcom;
1245b700
NK
889 struct snd_soc_dai *codec_dai = rtd->codec_dai;
890 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
891 struct snd_compr *compr;
2a99ef0f 892 struct snd_pcm *be_pcm;
1245b700
NK
893 char new_name[64];
894 int ret = 0, direction = 0;
a1068045 895 int playback = 0, capture = 0;
1245b700 896
8151d5e6 897 if (rtd->num_codecs > 1) {
141dfc9e
CK
898 dev_err(rtd->card->dev,
899 "Compress ASoC: Multicodec not supported\n");
8151d5e6
BC
900 return -EINVAL;
901 }
902
1245b700 903 /* check client and interface hw capabilities */
daa2db59 904 if (codec_dai->driver->playback.channels_min)
a1068045
VK
905 playback = 1;
906 if (codec_dai->driver->capture.channels_min)
907 capture = 1;
908
909 capture = capture && cpu_dai->driver->capture.channels_min;
910 playback = playback && cpu_dai->driver->playback.channels_min;
911
912 /*
913 * Compress devices are unidirectional so only one of the directions
914 * should be set, check for that (xor)
915 */
916 if (playback + capture != 1) {
141dfc9e
CK
917 dev_err(rtd->card->dev,
918 "Compress ASoC: Invalid direction for P %d, C %d\n",
919 playback, capture);
a1068045
VK
920 return -EINVAL;
921 }
922
aeb6fa0f 923 if (playback)
daa2db59 924 direction = SND_COMPRESS_PLAYBACK;
daa2db59 925 else
a1068045 926 direction = SND_COMPRESS_CAPTURE;
daa2db59 927
1245b700 928 compr = kzalloc(sizeof(*compr), GFP_KERNEL);
7a0cf42e 929 if (!compr)
1245b700 930 return -ENOMEM;
1245b700 931
1f88eb0f
CK
932 compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
933 GFP_KERNEL);
7a0cf42e 934 if (!compr->ops) {
1f88eb0f
CK
935 ret = -ENOMEM;
936 goto compr_err;
937 }
2a99ef0f
LG
938
939 if (rtd->dai_link->dynamic) {
940 snprintf(new_name, sizeof(new_name), "(%s)",
941 rtd->dai_link->stream_name);
942
943 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
d3268a40
QY
944 rtd->dai_link->dpcm_playback,
945 rtd->dai_link->dpcm_capture, &be_pcm);
2a99ef0f 946 if (ret < 0) {
141dfc9e
CK
947 dev_err(rtd->card->dev,
948 "Compress ASoC: can't create compressed for %s: %d\n",
949 rtd->dai_link->name, ret);
2a99ef0f
LG
950 goto compr_err;
951 }
952
953 rtd->pcm = be_pcm;
954 rtd->fe_compr = 1;
d3268a40
QY
955 if (rtd->dai_link->dpcm_playback)
956 be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
957 else if (rtd->dai_link->dpcm_capture)
958 be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2a99ef0f 959 memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
aeb6fa0f
PD
960 } else {
961 snprintf(new_name, sizeof(new_name), "%s %s-%d",
962 rtd->dai_link->stream_name, codec_dai->name, num);
963
2a99ef0f 964 memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
aeb6fa0f 965 }
1f88eb0f 966
9e7e3738
KM
967 for_each_rtdcom(rtd, rtdcom) {
968 component = rtdcom->component;
969
9e7e3738
KM
970 if (!component->driver->compr_ops ||
971 !component->driver->compr_ops->copy)
972 continue;
973
1f88eb0f 974 compr->ops->copy = soc_compr_copy;
ca76db6c 975 break;
9e7e3738
KM
976 }
977
1245b700 978 mutex_init(&compr->lock);
e5241a8c
RF
979 ret = snd_compress_new(rtd->card->snd_card, num, direction,
980 new_name, compr);
1245b700 981 if (ret < 0) {
e5acfc7d 982 component = rtd->codec_dai->component;
141dfc9e
CK
983 dev_err(component->dev,
984 "Compress ASoC: can't create compress for codec %s: %d\n",
985 component->name, ret);
1f88eb0f 986 goto compr_err;
1245b700
NK
987 }
988
202c8f70
CK
989 /* DAPM dai link stream work */
990 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
991
1245b700
NK
992 rtd->compr = compr;
993 compr->private_data = rtd;
994
141dfc9e
CK
995 dev_info(rtd->card->dev, "Compress ASoC: %s <-> %s mapping ok\n",
996 codec_dai->name, cpu_dai->name);
1245b700 997 return ret;
1f88eb0f
CK
998
999compr_err:
1000 kfree(compr);
1001 return ret;
1245b700 1002}
6f0c4226 1003EXPORT_SYMBOL_GPL(snd_soc_new_compress);