ASoC: Remove runtime field from DAI
[linux-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
NK
28
29static int soc_compr_open(struct snd_compr_stream *cstream)
30{
31 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
32 struct snd_soc_platform *platform = rtd->platform;
1245b700
NK
33 int ret = 0;
34
15e2e619
CK
35 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
36
1245b700
NK
37 if (platform->driver->compr_ops && platform->driver->compr_ops->open) {
38 ret = platform->driver->compr_ops->open(cstream);
39 if (ret < 0) {
40 pr_err("compress asoc: can't open platform %s\n", platform->name);
41 goto out;
42 }
43 }
44
45 if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->startup) {
46 ret = rtd->dai_link->compr_ops->startup(cstream);
47 if (ret < 0) {
48 pr_err("compress asoc: %s startup failed\n", rtd->dai_link->name);
49 goto machine_err;
50 }
51 }
52
24894b76 53 snd_soc_runtime_activate(rtd, cstream->direction);
1245b700 54
15e2e619
CK
55 mutex_unlock(&rtd->pcm_mutex);
56
1245b700
NK
57 return 0;
58
59machine_err:
60 if (platform->driver->compr_ops && platform->driver->compr_ops->free)
61 platform->driver->compr_ops->free(cstream);
62out:
15e2e619 63 mutex_unlock(&rtd->pcm_mutex);
1245b700
NK
64 return ret;
65}
66
2a99ef0f
LG
67static int soc_compr_open_fe(struct snd_compr_stream *cstream)
68{
69 struct snd_soc_pcm_runtime *fe = cstream->private_data;
70 struct snd_pcm_substream *fe_substream = fe->pcm->streams[0].substream;
71 struct snd_soc_platform *platform = fe->platform;
2a99ef0f
LG
72 struct snd_soc_dpcm *dpcm;
73 struct snd_soc_dapm_widget_list *list;
74 int stream;
75 int ret = 0;
76
77 if (cstream->direction == SND_COMPRESS_PLAYBACK)
78 stream = SNDRV_PCM_STREAM_PLAYBACK;
79 else
80 stream = SNDRV_PCM_STREAM_CAPTURE;
81
82 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
83
84 if (platform->driver->compr_ops && platform->driver->compr_ops->open) {
85 ret = platform->driver->compr_ops->open(cstream);
86 if (ret < 0) {
87 pr_err("compress asoc: can't open platform %s\n", platform->name);
88 goto out;
89 }
90 }
91
92 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->startup) {
93 ret = fe->dai_link->compr_ops->startup(cstream);
94 if (ret < 0) {
95 pr_err("compress asoc: %s startup failed\n", fe->dai_link->name);
96 goto machine_err;
97 }
98 }
99
100 fe->dpcm[stream].runtime = fe_substream->runtime;
101
102 if (dpcm_path_get(fe, stream, &list) <= 0) {
103 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
104 fe->dai_link->name, stream ? "capture" : "playback");
105 }
106
107 /* calculate valid and active FE <-> BE dpcms */
108 dpcm_process_paths(fe, stream, &list, 1);
109
110 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
111
112 ret = dpcm_be_dai_startup(fe, stream);
113 if (ret < 0) {
114 /* clean up all links */
115 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
116 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
117
118 dpcm_be_disconnect(fe, stream);
119 fe->dpcm[stream].runtime = NULL;
120 goto fe_err;
121 }
122
123 dpcm_clear_pending_state(fe, stream);
124 dpcm_path_put(&list);
125
126 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
127 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
128
24894b76 129 snd_soc_runtime_activate(fe, stream);
2a99ef0f
LG
130
131 mutex_unlock(&fe->card->mutex);
132
133 return 0;
134
135fe_err:
136 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->shutdown)
137 fe->dai_link->compr_ops->shutdown(cstream);
138machine_err:
139 if (platform->driver->compr_ops && platform->driver->compr_ops->free)
140 platform->driver->compr_ops->free(cstream);
141out:
142 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
143 mutex_unlock(&fe->card->mutex);
144 return ret;
145}
146
202c8f70
CK
147/*
148 * Power down the audio subsystem pmdown_time msecs after close is called.
149 * This is to ensure there are no pops or clicks in between any music tracks
150 * due to DAPM power cycling.
151 */
152static void close_delayed_work(struct work_struct *work)
153{
154 struct snd_soc_pcm_runtime *rtd =
155 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
156 struct snd_soc_dai *codec_dai = rtd->codec_dai;
157
158 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
159
160 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
161 codec_dai->driver->playback.stream_name,
162 codec_dai->playback_active ? "active" : "inactive",
163 rtd->pop_wait ? "yes" : "no");
164
165 /* are we waiting on this codec DAI stream */
166 if (rtd->pop_wait == 1) {
167 rtd->pop_wait = 0;
168 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
169 SND_SOC_DAPM_STREAM_STOP);
170 }
171
172 mutex_unlock(&rtd->pcm_mutex);
173}
174
1245b700
NK
175static int soc_compr_free(struct snd_compr_stream *cstream)
176{
177 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
178 struct snd_soc_platform *platform = rtd->platform;
179 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
180 struct snd_soc_dai *codec_dai = rtd->codec_dai;
24894b76 181 int stream;
1245b700 182
15e2e619
CK
183 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
184
24894b76
LPC
185 if (cstream->direction == SND_COMPRESS_PLAYBACK)
186 stream = SNDRV_PCM_STREAM_PLAYBACK;
187 else
188 stream = SNDRV_PCM_STREAM_CAPTURE;
1245b700 189
24894b76 190 snd_soc_runtime_deactivate(rtd, stream);
da18396f 191
24894b76 192 snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
1245b700
NK
193
194 if (!cpu_dai->active)
195 cpu_dai->rate = 0;
196
197 if (!codec_dai->active)
198 codec_dai->rate = 0;
199
200
201 if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->shutdown)
202 rtd->dai_link->compr_ops->shutdown(cstream);
203
204 if (platform->driver->compr_ops && platform->driver->compr_ops->free)
205 platform->driver->compr_ops->free(cstream);
1245b700
NK
206
207 if (cstream->direction == SND_COMPRESS_PLAYBACK) {
208a1589 208 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
1245b700
NK
209 snd_soc_dapm_stream_event(rtd,
210 SNDRV_PCM_STREAM_PLAYBACK,
211 SND_SOC_DAPM_STREAM_STOP);
8c3d2aa4 212 } else {
9bffb1fb 213 rtd->pop_wait = 1;
3d24cfe4
MB
214 queue_delayed_work(system_power_efficient_wq,
215 &rtd->delayed_work,
216 msecs_to_jiffies(rtd->pmdown_time));
8c3d2aa4 217 }
1245b700
NK
218 } else {
219 /* capture streams can be powered down now */
220 snd_soc_dapm_stream_event(rtd,
221 SNDRV_PCM_STREAM_CAPTURE,
222 SND_SOC_DAPM_STREAM_STOP);
223 }
224
15e2e619 225 mutex_unlock(&rtd->pcm_mutex);
1245b700
NK
226 return 0;
227}
228
2a99ef0f
LG
229static int soc_compr_free_fe(struct snd_compr_stream *cstream)
230{
231 struct snd_soc_pcm_runtime *fe = cstream->private_data;
232 struct snd_soc_platform *platform = fe->platform;
2a99ef0f
LG
233 struct snd_soc_dpcm *dpcm;
234 int stream, ret;
235
236 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
237
24894b76 238 if (cstream->direction == SND_COMPRESS_PLAYBACK)
2a99ef0f 239 stream = SNDRV_PCM_STREAM_PLAYBACK;
24894b76 240 else
2a99ef0f 241 stream = SNDRV_PCM_STREAM_CAPTURE;
2a99ef0f 242
24894b76 243 snd_soc_runtime_deactivate(fe, stream);
2a99ef0f
LG
244
245 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
246
247 ret = dpcm_be_dai_hw_free(fe, stream);
248 if (ret < 0)
249 dev_err(fe->dev, "compressed hw_free failed %d\n", ret);
250
251 ret = dpcm_be_dai_shutdown(fe, stream);
252
253 /* mark FE's links ready to prune */
254 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
255 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
256
257 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
258 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
259 else
260 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
261
262 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
263 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
264
265 dpcm_be_disconnect(fe, stream);
266
267 fe->dpcm[stream].runtime = NULL;
268
269 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->shutdown)
270 fe->dai_link->compr_ops->shutdown(cstream);
271
272 if (platform->driver->compr_ops && platform->driver->compr_ops->free)
273 platform->driver->compr_ops->free(cstream);
274
275 mutex_unlock(&fe->card->mutex);
276 return 0;
277}
278
1245b700
NK
279static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd)
280{
281
282 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
283 struct snd_soc_platform *platform = rtd->platform;
284 struct snd_soc_dai *codec_dai = rtd->codec_dai;
285 int ret = 0;
286
15e2e619
CK
287 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
288
1245b700
NK
289 if (platform->driver->compr_ops && platform->driver->compr_ops->trigger) {
290 ret = platform->driver->compr_ops->trigger(cstream, cmd);
291 if (ret < 0)
15e2e619 292 goto out;
1245b700
NK
293 }
294
da18396f
MB
295 switch (cmd) {
296 case SNDRV_PCM_TRIGGER_START:
297 snd_soc_dai_digital_mute(codec_dai, 0, cstream->direction);
298 break;
299 case SNDRV_PCM_TRIGGER_STOP:
300 snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
301 break;
e38b9b74 302 }
1245b700 303
15e2e619
CK
304out:
305 mutex_unlock(&rtd->pcm_mutex);
1245b700
NK
306 return ret;
307}
308
2a99ef0f
LG
309static int soc_compr_trigger_fe(struct snd_compr_stream *cstream, int cmd)
310{
311 struct snd_soc_pcm_runtime *fe = cstream->private_data;
312 struct snd_soc_platform *platform = fe->platform;
313 int ret = 0, stream;
314
315 if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN ||
316 cmd == SND_COMPR_TRIGGER_DRAIN) {
317
318 if (platform->driver->compr_ops &&
319 platform->driver->compr_ops->trigger)
320 return platform->driver->compr_ops->trigger(cstream, cmd);
321 }
322
323 if (cstream->direction == SND_COMPRESS_PLAYBACK)
324 stream = SNDRV_PCM_STREAM_PLAYBACK;
325 else
326 stream = SNDRV_PCM_STREAM_CAPTURE;
327
328
329 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
330
331 if (platform->driver->compr_ops && platform->driver->compr_ops->trigger) {
332 ret = platform->driver->compr_ops->trigger(cstream, cmd);
333 if (ret < 0)
334 goto out;
335 }
336
337 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
338
339 ret = dpcm_be_dai_trigger(fe, stream, cmd);
340
341 switch (cmd) {
342 case SNDRV_PCM_TRIGGER_START:
343 case SNDRV_PCM_TRIGGER_RESUME:
344 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
345 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
346 break;
347 case SNDRV_PCM_TRIGGER_STOP:
348 case SNDRV_PCM_TRIGGER_SUSPEND:
349 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
350 break;
351 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
352 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
353 break;
354 }
355
356out:
357 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
358 mutex_unlock(&fe->card->mutex);
359 return ret;
360}
361
1245b700
NK
362static int soc_compr_set_params(struct snd_compr_stream *cstream,
363 struct snd_compr_params *params)
364{
365 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
366 struct snd_soc_platform *platform = rtd->platform;
1245b700
NK
367 int ret = 0;
368
15e2e619
CK
369 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
370
1245b700
NK
371 /* first we call set_params for the platform driver
372 * this should configure the soc side
373 * if the machine has compressed ops then we call that as well
374 * expectation is that platform and machine will configure everything
375 * for this compress path, like configuring pcm port for codec
376 */
377 if (platform->driver->compr_ops && platform->driver->compr_ops->set_params) {
378 ret = platform->driver->compr_ops->set_params(cstream, params);
379 if (ret < 0)
fa40ef20 380 goto err;
1245b700
NK
381 }
382
383 if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->set_params) {
384 ret = rtd->dai_link->compr_ops->set_params(cstream);
385 if (ret < 0)
fa40ef20 386 goto err;
1245b700
NK
387 }
388
2c071ed7
CK
389 if (cstream->direction == SND_COMPRESS_PLAYBACK)
390 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
391 SND_SOC_DAPM_STREAM_START);
392 else
393 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
394 SND_SOC_DAPM_STREAM_START);
1245b700 395
fa40ef20
CK
396 /* cancel any delayed stream shutdown that is pending */
397 rtd->pop_wait = 0;
398 mutex_unlock(&rtd->pcm_mutex);
399
400 cancel_delayed_work_sync(&rtd->delayed_work);
401
402 return ret;
403
404err:
15e2e619 405 mutex_unlock(&rtd->pcm_mutex);
1245b700
NK
406 return ret;
407}
408
2a99ef0f
LG
409static int soc_compr_set_params_fe(struct snd_compr_stream *cstream,
410 struct snd_compr_params *params)
411{
412 struct snd_soc_pcm_runtime *fe = cstream->private_data;
413 struct snd_pcm_substream *fe_substream = fe->pcm->streams[0].substream;
414 struct snd_soc_platform *platform = fe->platform;
415 int ret = 0, stream;
416
417 if (cstream->direction == SND_COMPRESS_PLAYBACK)
418 stream = SNDRV_PCM_STREAM_PLAYBACK;
419 else
420 stream = SNDRV_PCM_STREAM_CAPTURE;
421
422 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
423
424 if (platform->driver->compr_ops && platform->driver->compr_ops->set_params) {
425 ret = platform->driver->compr_ops->set_params(cstream, params);
426 if (ret < 0)
427 goto out;
428 }
429
430 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->set_params) {
431 ret = fe->dai_link->compr_ops->set_params(cstream);
432 if (ret < 0)
433 goto out;
434 }
435
436 /*
437 * Create an empty hw_params for the BE as the machine driver must
438 * fix this up to match DSP decoder and ASRC configuration.
439 * I.e. machine driver fixup for compressed BE is mandatory.
440 */
441 memset(&fe->dpcm[fe_substream->stream].hw_params, 0,
442 sizeof(struct snd_pcm_hw_params));
443
444 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
445
446 ret = dpcm_be_dai_hw_params(fe, stream);
447 if (ret < 0)
448 goto out;
449
450 ret = dpcm_be_dai_prepare(fe, stream);
451 if (ret < 0)
452 goto out;
453
454 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
455 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
456 else
457 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
458
459 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
460
461out:
462 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
463 mutex_unlock(&fe->card->mutex);
464 return ret;
465}
466
1245b700
NK
467static int soc_compr_get_params(struct snd_compr_stream *cstream,
468 struct snd_codec *params)
469{
470 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
471 struct snd_soc_platform *platform = rtd->platform;
472 int ret = 0;
473
15e2e619
CK
474 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
475
1245b700
NK
476 if (platform->driver->compr_ops && platform->driver->compr_ops->get_params)
477 ret = platform->driver->compr_ops->get_params(cstream, params);
478
15e2e619 479 mutex_unlock(&rtd->pcm_mutex);
1245b700
NK
480 return ret;
481}
482
483static int soc_compr_get_caps(struct snd_compr_stream *cstream,
484 struct snd_compr_caps *caps)
485{
486 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
487 struct snd_soc_platform *platform = rtd->platform;
488 int ret = 0;
489
15e2e619
CK
490 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
491
1245b700
NK
492 if (platform->driver->compr_ops && platform->driver->compr_ops->get_caps)
493 ret = platform->driver->compr_ops->get_caps(cstream, caps);
494
15e2e619 495 mutex_unlock(&rtd->pcm_mutex);
1245b700
NK
496 return ret;
497}
498
499static int soc_compr_get_codec_caps(struct snd_compr_stream *cstream,
500 struct snd_compr_codec_caps *codec)
501{
502 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
503 struct snd_soc_platform *platform = rtd->platform;
504 int ret = 0;
505
15e2e619
CK
506 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
507
1245b700
NK
508 if (platform->driver->compr_ops && platform->driver->compr_ops->get_codec_caps)
509 ret = platform->driver->compr_ops->get_codec_caps(cstream, codec);
510
15e2e619 511 mutex_unlock(&rtd->pcm_mutex);
1245b700
NK
512 return ret;
513}
514
515static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
516{
517 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
518 struct snd_soc_platform *platform = rtd->platform;
519 int ret = 0;
520
15e2e619
CK
521 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
522
1245b700
NK
523 if (platform->driver->compr_ops && platform->driver->compr_ops->ack)
524 ret = platform->driver->compr_ops->ack(cstream, bytes);
525
15e2e619 526 mutex_unlock(&rtd->pcm_mutex);
1245b700
NK
527 return ret;
528}
529
530static int soc_compr_pointer(struct snd_compr_stream *cstream,
531 struct snd_compr_tstamp *tstamp)
532{
533 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
534 struct snd_soc_platform *platform = rtd->platform;
535
15e2e619
CK
536 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
537
1245b700
NK
538 if (platform->driver->compr_ops && platform->driver->compr_ops->pointer)
539 platform->driver->compr_ops->pointer(cstream, tstamp);
540
15e2e619 541 mutex_unlock(&rtd->pcm_mutex);
1245b700
NK
542 return 0;
543}
544
1f88eb0f 545static int soc_compr_copy(struct snd_compr_stream *cstream,
4daf891c 546 char __user *buf, size_t count)
1f88eb0f
CK
547{
548 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
549 struct snd_soc_platform *platform = rtd->platform;
550 int ret = 0;
551
552 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
553
554 if (platform->driver->compr_ops && platform->driver->compr_ops->copy)
555 ret = platform->driver->compr_ops->copy(cstream, buf, count);
556
557 mutex_unlock(&rtd->pcm_mutex);
558 return ret;
559}
560
02bd90e8 561static int soc_compr_set_metadata(struct snd_compr_stream *cstream,
36953d98
JK
562 struct snd_compr_metadata *metadata)
563{
564 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
565 struct snd_soc_platform *platform = rtd->platform;
566 int ret = 0;
567
568 if (platform->driver->compr_ops && platform->driver->compr_ops->set_metadata)
569 ret = platform->driver->compr_ops->set_metadata(cstream, metadata);
570
571 return ret;
572}
573
02bd90e8 574static int soc_compr_get_metadata(struct snd_compr_stream *cstream,
36953d98
JK
575 struct snd_compr_metadata *metadata)
576{
577 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
578 struct snd_soc_platform *platform = rtd->platform;
579 int ret = 0;
580
581 if (platform->driver->compr_ops && platform->driver->compr_ops->get_metadata)
582 ret = platform->driver->compr_ops->get_metadata(cstream, metadata);
583
584 return ret;
585}
2a99ef0f 586
1245b700
NK
587/* ASoC Compress operations */
588static struct snd_compr_ops soc_compr_ops = {
589 .open = soc_compr_open,
590 .free = soc_compr_free,
591 .set_params = soc_compr_set_params,
02bd90e8
VK
592 .set_metadata = soc_compr_set_metadata,
593 .get_metadata = soc_compr_get_metadata,
1245b700
NK
594 .get_params = soc_compr_get_params,
595 .trigger = soc_compr_trigger,
596 .pointer = soc_compr_pointer,
597 .ack = soc_compr_ack,
598 .get_caps = soc_compr_get_caps,
599 .get_codec_caps = soc_compr_get_codec_caps
600};
601
2a99ef0f
LG
602/* ASoC Dynamic Compress operations */
603static struct snd_compr_ops soc_compr_dyn_ops = {
604 .open = soc_compr_open_fe,
605 .free = soc_compr_free_fe,
606 .set_params = soc_compr_set_params_fe,
607 .get_params = soc_compr_get_params,
608 .set_metadata = soc_compr_set_metadata,
609 .get_metadata = soc_compr_get_metadata,
610 .trigger = soc_compr_trigger_fe,
611 .pointer = soc_compr_pointer,
612 .ack = soc_compr_ack,
613 .get_caps = soc_compr_get_caps,
614 .get_codec_caps = soc_compr_get_codec_caps
615};
616
1245b700
NK
617/* create a new compress */
618int soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
619{
620 struct snd_soc_codec *codec = rtd->codec;
1f88eb0f 621 struct snd_soc_platform *platform = rtd->platform;
1245b700
NK
622 struct snd_soc_dai *codec_dai = rtd->codec_dai;
623 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
624 struct snd_compr *compr;
2a99ef0f 625 struct snd_pcm *be_pcm;
1245b700
NK
626 char new_name[64];
627 int ret = 0, direction = 0;
628
629 /* check client and interface hw capabilities */
630 snprintf(new_name, sizeof(new_name), "%s %s-%d",
631 rtd->dai_link->stream_name, codec_dai->name, num);
daa2db59
CK
632
633 if (codec_dai->driver->playback.channels_min)
634 direction = SND_COMPRESS_PLAYBACK;
635 else if (codec_dai->driver->capture.channels_min)
636 direction = SND_COMPRESS_CAPTURE;
637 else
638 return -EINVAL;
639
1245b700
NK
640 compr = kzalloc(sizeof(*compr), GFP_KERNEL);
641 if (compr == NULL) {
642 snd_printk(KERN_ERR "Cannot allocate compr\n");
643 return -ENOMEM;
644 }
645
1f88eb0f
CK
646 compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
647 GFP_KERNEL);
648 if (compr->ops == NULL) {
649 dev_err(rtd->card->dev, "Cannot allocate compressed ops\n");
650 ret = -ENOMEM;
651 goto compr_err;
652 }
2a99ef0f
LG
653
654 if (rtd->dai_link->dynamic) {
655 snprintf(new_name, sizeof(new_name), "(%s)",
656 rtd->dai_link->stream_name);
657
658 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
659 1, 0, &be_pcm);
660 if (ret < 0) {
661 dev_err(rtd->card->dev, "ASoC: can't create compressed for %s\n",
662 rtd->dai_link->name);
663 goto compr_err;
664 }
665
666 rtd->pcm = be_pcm;
667 rtd->fe_compr = 1;
668 be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
669 be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
670 memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
671 } else
672 memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
1f88eb0f
CK
673
674 /* Add copy callback for not memory mapped DSPs */
675 if (platform->driver->compr_ops && platform->driver->compr_ops->copy)
676 compr->ops->copy = soc_compr_copy;
677
1245b700
NK
678 mutex_init(&compr->lock);
679 ret = snd_compress_new(rtd->card->snd_card, num, direction, compr);
680 if (ret < 0) {
681 pr_err("compress asoc: can't create compress for codec %s\n",
682 codec->name);
1f88eb0f 683 goto compr_err;
1245b700
NK
684 }
685
202c8f70
CK
686 /* DAPM dai link stream work */
687 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
688
1245b700
NK
689 rtd->compr = compr;
690 compr->private_data = rtd;
691
692 printk(KERN_INFO "compress asoc: %s <-> %s mapping ok\n", codec_dai->name,
693 cpu_dai->name);
694 return ret;
1f88eb0f
CK
695
696compr_err:
697 kfree(compr);
698 return ret;
1245b700 699}