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