blob: 1bf5c47a427d9393f42f96222940d00f1e159197 [file] [log] [blame]
Namarta Kohli1245b702012-08-16 17:10:41 +05301/*
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>
Liam Girdwooda3f27052014-01-17 17:03:56 +000027#include <sound/soc-dpcm.h>
Namarta Kohli1245b702012-08-16 17:10:41 +053028
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
Charles Keepax15e2e612013-01-24 09:44:29 +000037 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
38
Namarta Kohli1245b702012-08-16 17:10:41 +053039 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
Charles Keepax15e2e612013-01-24 09:44:29 +000067 mutex_unlock(&rtd->pcm_mutex);
68
Namarta Kohli1245b702012-08-16 17:10:41 +053069 return 0;
70
71machine_err:
72 if (platform->driver->compr_ops && platform->driver->compr_ops->free)
73 platform->driver->compr_ops->free(cstream);
74out:
Charles Keepax15e2e612013-01-24 09:44:29 +000075 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +053076 return ret;
77}
78
Liam Girdwooda3f27052014-01-17 17:03:56 +000079static 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
Charles Keepax202c8f72013-01-24 09:44:30 +0000171/*
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
Namarta Kohli1245b702012-08-16 17:10:41 +0530199static 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
Charles Keepax15e2e612013-01-24 09:44:29 +0000207 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
208
Namarta Kohli1245b702012-08-16 17:10:41 +0530209 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
Mark Brownda183962013-02-06 15:44:07 +0000217 snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
218
Namarta Kohli1245b702012-08-16 17:10:41 +0530219 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);
Vinod Koul9d2667a2012-08-21 12:15:02 +0530235 cpu_dai->runtime = NULL;
Namarta Kohli1245b702012-08-16 17:10:41 +0530236
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);
Charles Keepax8c3d2aa2013-01-24 09:44:28 +0000243 } else {
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600244 rtd->pop_wait = 1;
Namarta Kohli1245b702012-08-16 17:10:41 +0530245 schedule_delayed_work(&rtd->delayed_work,
246 msecs_to_jiffies(rtd->pmdown_time));
Charles Keepax8c3d2aa2013-01-24 09:44:28 +0000247 }
Namarta Kohli1245b702012-08-16 17:10:41 +0530248 } else {
249 /* capture streams can be powered down now */
250 snd_soc_dapm_stream_event(rtd,
251 SNDRV_PCM_STREAM_CAPTURE,
252 SND_SOC_DAPM_STREAM_STOP);
253 }
254
Charles Keepax15e2e612013-01-24 09:44:29 +0000255 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530256 return 0;
257}
258
Liam Girdwooda3f27052014-01-17 17:03:56 +0000259static int soc_compr_free_fe(struct snd_compr_stream *cstream)
260{
261 struct snd_soc_pcm_runtime *fe = cstream->private_data;
262 struct snd_soc_platform *platform = fe->platform;
263 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
264 struct snd_soc_dai *codec_dai = fe->codec_dai;
265 struct snd_soc_dpcm *dpcm;
266 int stream, ret;
267
268 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
269
270 if (cstream->direction == SND_COMPRESS_PLAYBACK) {
271 stream = SNDRV_PCM_STREAM_PLAYBACK;
272 cpu_dai->playback_active--;
273 codec_dai->playback_active--;
274 } else {
275 stream = SNDRV_PCM_STREAM_CAPTURE;
276 cpu_dai->capture_active--;
277 codec_dai->capture_active--;
278 }
279
280 cpu_dai->active--;
281 codec_dai->active--;
282 fe->codec->active--;
283
284 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
285
286 ret = dpcm_be_dai_hw_free(fe, stream);
287 if (ret < 0)
288 dev_err(fe->dev, "compressed hw_free failed %d\n", ret);
289
290 ret = dpcm_be_dai_shutdown(fe, stream);
291
292 /* mark FE's links ready to prune */
293 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
294 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
295
296 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
297 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
298 else
299 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
300
301 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
302 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
303
304 dpcm_be_disconnect(fe, stream);
305
306 fe->dpcm[stream].runtime = NULL;
307
308 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->shutdown)
309 fe->dai_link->compr_ops->shutdown(cstream);
310
311 if (platform->driver->compr_ops && platform->driver->compr_ops->free)
312 platform->driver->compr_ops->free(cstream);
313
314 mutex_unlock(&fe->card->mutex);
315 return 0;
316}
317
Namarta Kohli1245b702012-08-16 17:10:41 +0530318static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd)
319{
320
321 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
322 struct snd_soc_platform *platform = rtd->platform;
323 struct snd_soc_dai *codec_dai = rtd->codec_dai;
324 int ret = 0;
325
Charles Keepax15e2e612013-01-24 09:44:29 +0000326 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
327
Namarta Kohli1245b702012-08-16 17:10:41 +0530328 if (platform->driver->compr_ops && platform->driver->compr_ops->trigger) {
329 ret = platform->driver->compr_ops->trigger(cstream, cmd);
330 if (ret < 0)
Charles Keepax15e2e612013-01-24 09:44:29 +0000331 goto out;
Namarta Kohli1245b702012-08-16 17:10:41 +0530332 }
333
Mark Brownda183962013-02-06 15:44:07 +0000334 switch (cmd) {
335 case SNDRV_PCM_TRIGGER_START:
336 snd_soc_dai_digital_mute(codec_dai, 0, cstream->direction);
337 break;
338 case SNDRV_PCM_TRIGGER_STOP:
339 snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
340 break;
Mark Browne38b9b72013-02-06 13:52:42 +0000341 }
Namarta Kohli1245b702012-08-16 17:10:41 +0530342
Charles Keepax15e2e612013-01-24 09:44:29 +0000343out:
344 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530345 return ret;
346}
347
Liam Girdwooda3f27052014-01-17 17:03:56 +0000348static int soc_compr_trigger_fe(struct snd_compr_stream *cstream, int cmd)
349{
350 struct snd_soc_pcm_runtime *fe = cstream->private_data;
351 struct snd_soc_platform *platform = fe->platform;
352 int ret = 0, stream;
353
354 if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN ||
355 cmd == SND_COMPR_TRIGGER_DRAIN) {
356
357 if (platform->driver->compr_ops &&
358 platform->driver->compr_ops->trigger)
359 return platform->driver->compr_ops->trigger(cstream, cmd);
360 }
361
362 if (cstream->direction == SND_COMPRESS_PLAYBACK)
363 stream = SNDRV_PCM_STREAM_PLAYBACK;
364 else
365 stream = SNDRV_PCM_STREAM_CAPTURE;
366
367
368 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
369
370 if (platform->driver->compr_ops && platform->driver->compr_ops->trigger) {
371 ret = platform->driver->compr_ops->trigger(cstream, cmd);
372 if (ret < 0)
373 goto out;
374 }
375
376 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
377
378 ret = dpcm_be_dai_trigger(fe, stream, cmd);
379
380 switch (cmd) {
381 case SNDRV_PCM_TRIGGER_START:
382 case SNDRV_PCM_TRIGGER_RESUME:
383 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
384 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
385 break;
386 case SNDRV_PCM_TRIGGER_STOP:
387 case SNDRV_PCM_TRIGGER_SUSPEND:
388 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
389 break;
390 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
391 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
392 break;
393 }
394
395out:
396 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
397 mutex_unlock(&fe->card->mutex);
398 return ret;
399}
400
Namarta Kohli1245b702012-08-16 17:10:41 +0530401static int soc_compr_set_params(struct snd_compr_stream *cstream,
402 struct snd_compr_params *params)
403{
404 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
405 struct snd_soc_platform *platform = rtd->platform;
Namarta Kohli1245b702012-08-16 17:10:41 +0530406 int ret = 0;
407
Charles Keepax15e2e612013-01-24 09:44:29 +0000408 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
409
Namarta Kohli1245b702012-08-16 17:10:41 +0530410 /* first we call set_params for the platform driver
411 * this should configure the soc side
412 * if the machine has compressed ops then we call that as well
413 * expectation is that platform and machine will configure everything
414 * for this compress path, like configuring pcm port for codec
415 */
416 if (platform->driver->compr_ops && platform->driver->compr_ops->set_params) {
417 ret = platform->driver->compr_ops->set_params(cstream, params);
418 if (ret < 0)
Charles Keepaxfa40ef22013-03-27 16:39:01 +0000419 goto err;
Namarta Kohli1245b702012-08-16 17:10:41 +0530420 }
421
422 if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->set_params) {
423 ret = rtd->dai_link->compr_ops->set_params(cstream);
424 if (ret < 0)
Charles Keepaxfa40ef22013-03-27 16:39:01 +0000425 goto err;
Namarta Kohli1245b702012-08-16 17:10:41 +0530426 }
427
Charles Keepax2c071ed2013-05-20 08:33:54 +0100428 if (cstream->direction == SND_COMPRESS_PLAYBACK)
429 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
430 SND_SOC_DAPM_STREAM_START);
431 else
432 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
433 SND_SOC_DAPM_STREAM_START);
Namarta Kohli1245b702012-08-16 17:10:41 +0530434
Charles Keepaxfa40ef22013-03-27 16:39:01 +0000435 /* cancel any delayed stream shutdown that is pending */
436 rtd->pop_wait = 0;
437 mutex_unlock(&rtd->pcm_mutex);
438
439 cancel_delayed_work_sync(&rtd->delayed_work);
440
441 return ret;
442
443err:
Charles Keepax15e2e612013-01-24 09:44:29 +0000444 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530445 return ret;
446}
447
Liam Girdwooda3f27052014-01-17 17:03:56 +0000448static int soc_compr_set_params_fe(struct snd_compr_stream *cstream,
449 struct snd_compr_params *params)
450{
451 struct snd_soc_pcm_runtime *fe = cstream->private_data;
452 struct snd_pcm_substream *fe_substream = fe->pcm->streams[0].substream;
453 struct snd_soc_platform *platform = fe->platform;
454 int ret = 0, stream;
455
456 if (cstream->direction == SND_COMPRESS_PLAYBACK)
457 stream = SNDRV_PCM_STREAM_PLAYBACK;
458 else
459 stream = SNDRV_PCM_STREAM_CAPTURE;
460
461 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
462
463 if (platform->driver->compr_ops && platform->driver->compr_ops->set_params) {
464 ret = platform->driver->compr_ops->set_params(cstream, params);
465 if (ret < 0)
466 goto out;
467 }
468
469 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->set_params) {
470 ret = fe->dai_link->compr_ops->set_params(cstream);
471 if (ret < 0)
472 goto out;
473 }
474
475 /*
476 * Create an empty hw_params for the BE as the machine driver must
477 * fix this up to match DSP decoder and ASRC configuration.
478 * I.e. machine driver fixup for compressed BE is mandatory.
479 */
480 memset(&fe->dpcm[fe_substream->stream].hw_params, 0,
481 sizeof(struct snd_pcm_hw_params));
482
483 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
484
485 ret = dpcm_be_dai_hw_params(fe, stream);
486 if (ret < 0)
487 goto out;
488
489 ret = dpcm_be_dai_prepare(fe, stream);
490 if (ret < 0)
491 goto out;
492
493 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
494 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
495 else
496 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
497
498 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
499
500out:
501 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
502 mutex_unlock(&fe->card->mutex);
503 return ret;
504}
505
Namarta Kohli1245b702012-08-16 17:10:41 +0530506static int soc_compr_get_params(struct snd_compr_stream *cstream,
507 struct snd_codec *params)
508{
509 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
510 struct snd_soc_platform *platform = rtd->platform;
511 int ret = 0;
512
Charles Keepax15e2e612013-01-24 09:44:29 +0000513 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
514
Namarta Kohli1245b702012-08-16 17:10:41 +0530515 if (platform->driver->compr_ops && platform->driver->compr_ops->get_params)
516 ret = platform->driver->compr_ops->get_params(cstream, params);
517
Charles Keepax15e2e612013-01-24 09:44:29 +0000518 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530519 return ret;
520}
521
522static int soc_compr_get_caps(struct snd_compr_stream *cstream,
523 struct snd_compr_caps *caps)
524{
525 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
526 struct snd_soc_platform *platform = rtd->platform;
527 int ret = 0;
528
Charles Keepax15e2e612013-01-24 09:44:29 +0000529 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
530
Namarta Kohli1245b702012-08-16 17:10:41 +0530531 if (platform->driver->compr_ops && platform->driver->compr_ops->get_caps)
532 ret = platform->driver->compr_ops->get_caps(cstream, caps);
533
Charles Keepax15e2e612013-01-24 09:44:29 +0000534 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530535 return ret;
536}
537
538static int soc_compr_get_codec_caps(struct snd_compr_stream *cstream,
539 struct snd_compr_codec_caps *codec)
540{
541 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
542 struct snd_soc_platform *platform = rtd->platform;
543 int ret = 0;
544
Charles Keepax15e2e612013-01-24 09:44:29 +0000545 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
546
Namarta Kohli1245b702012-08-16 17:10:41 +0530547 if (platform->driver->compr_ops && platform->driver->compr_ops->get_codec_caps)
548 ret = platform->driver->compr_ops->get_codec_caps(cstream, codec);
549
Charles Keepax15e2e612013-01-24 09:44:29 +0000550 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530551 return ret;
552}
553
554static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
555{
556 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
557 struct snd_soc_platform *platform = rtd->platform;
558 int ret = 0;
559
Charles Keepax15e2e612013-01-24 09:44:29 +0000560 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
561
Namarta Kohli1245b702012-08-16 17:10:41 +0530562 if (platform->driver->compr_ops && platform->driver->compr_ops->ack)
563 ret = platform->driver->compr_ops->ack(cstream, bytes);
564
Charles Keepax15e2e612013-01-24 09:44:29 +0000565 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530566 return ret;
567}
568
569static int soc_compr_pointer(struct snd_compr_stream *cstream,
570 struct snd_compr_tstamp *tstamp)
571{
572 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
573 struct snd_soc_platform *platform = rtd->platform;
574
Charles Keepax15e2e612013-01-24 09:44:29 +0000575 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
576
Namarta Kohli1245b702012-08-16 17:10:41 +0530577 if (platform->driver->compr_ops && platform->driver->compr_ops->pointer)
578 platform->driver->compr_ops->pointer(cstream, tstamp);
579
Charles Keepax15e2e612013-01-24 09:44:29 +0000580 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530581 return 0;
582}
583
Charles Keepax1f88eb02013-02-05 10:41:47 +0000584static int soc_compr_copy(struct snd_compr_stream *cstream,
Charles Keepax4daf8912013-04-18 11:01:38 +0100585 char __user *buf, size_t count)
Charles Keepax1f88eb02013-02-05 10:41:47 +0000586{
587 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
588 struct snd_soc_platform *platform = rtd->platform;
589 int ret = 0;
590
591 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
592
593 if (platform->driver->compr_ops && platform->driver->compr_ops->copy)
594 ret = platform->driver->compr_ops->copy(cstream, buf, count);
595
596 mutex_unlock(&rtd->pcm_mutex);
597 return ret;
598}
599
Vinod Koule978ae42013-07-28 20:06:15 +0530600static int soc_compr_set_metadata(struct snd_compr_stream *cstream,
Jeeja KP36953d92013-03-26 21:22:28 +0530601 struct snd_compr_metadata *metadata)
602{
603 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
604 struct snd_soc_platform *platform = rtd->platform;
605 int ret = 0;
606
607 if (platform->driver->compr_ops && platform->driver->compr_ops->set_metadata)
608 ret = platform->driver->compr_ops->set_metadata(cstream, metadata);
609
610 return ret;
611}
612
Vinod Koule978ae42013-07-28 20:06:15 +0530613static int soc_compr_get_metadata(struct snd_compr_stream *cstream,
Jeeja KP36953d92013-03-26 21:22:28 +0530614 struct snd_compr_metadata *metadata)
615{
616 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
617 struct snd_soc_platform *platform = rtd->platform;
618 int ret = 0;
619
620 if (platform->driver->compr_ops && platform->driver->compr_ops->get_metadata)
621 ret = platform->driver->compr_ops->get_metadata(cstream, metadata);
622
623 return ret;
624}
Liam Girdwooda3f27052014-01-17 17:03:56 +0000625
Namarta Kohli1245b702012-08-16 17:10:41 +0530626/* ASoC Compress operations */
627static struct snd_compr_ops soc_compr_ops = {
628 .open = soc_compr_open,
629 .free = soc_compr_free,
630 .set_params = soc_compr_set_params,
Vinod Koule978ae42013-07-28 20:06:15 +0530631 .set_metadata = soc_compr_set_metadata,
632 .get_metadata = soc_compr_get_metadata,
Namarta Kohli1245b702012-08-16 17:10:41 +0530633 .get_params = soc_compr_get_params,
634 .trigger = soc_compr_trigger,
635 .pointer = soc_compr_pointer,
636 .ack = soc_compr_ack,
637 .get_caps = soc_compr_get_caps,
638 .get_codec_caps = soc_compr_get_codec_caps
639};
640
Liam Girdwooda3f27052014-01-17 17:03:56 +0000641/* ASoC Dynamic Compress operations */
642static struct snd_compr_ops soc_compr_dyn_ops = {
643 .open = soc_compr_open_fe,
644 .free = soc_compr_free_fe,
645 .set_params = soc_compr_set_params_fe,
646 .get_params = soc_compr_get_params,
647 .set_metadata = soc_compr_set_metadata,
648 .get_metadata = soc_compr_get_metadata,
649 .trigger = soc_compr_trigger_fe,
650 .pointer = soc_compr_pointer,
651 .ack = soc_compr_ack,
652 .get_caps = soc_compr_get_caps,
653 .get_codec_caps = soc_compr_get_codec_caps
654};
655
Namarta Kohli1245b702012-08-16 17:10:41 +0530656/* create a new compress */
657int soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
658{
659 struct snd_soc_codec *codec = rtd->codec;
Charles Keepax1f88eb02013-02-05 10:41:47 +0000660 struct snd_soc_platform *platform = rtd->platform;
Namarta Kohli1245b702012-08-16 17:10:41 +0530661 struct snd_soc_dai *codec_dai = rtd->codec_dai;
662 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
663 struct snd_compr *compr;
Liam Girdwooda3f27052014-01-17 17:03:56 +0000664 struct snd_pcm *be_pcm;
Namarta Kohli1245b702012-08-16 17:10:41 +0530665 char new_name[64];
666 int ret = 0, direction = 0;
667
668 /* check client and interface hw capabilities */
669 snprintf(new_name, sizeof(new_name), "%s %s-%d",
670 rtd->dai_link->stream_name, codec_dai->name, num);
Charles Keepaxdaa2db52013-04-18 11:02:38 +0100671
672 if (codec_dai->driver->playback.channels_min)
673 direction = SND_COMPRESS_PLAYBACK;
674 else if (codec_dai->driver->capture.channels_min)
675 direction = SND_COMPRESS_CAPTURE;
676 else
677 return -EINVAL;
678
Namarta Kohli1245b702012-08-16 17:10:41 +0530679 compr = kzalloc(sizeof(*compr), GFP_KERNEL);
680 if (compr == NULL) {
681 snd_printk(KERN_ERR "Cannot allocate compr\n");
682 return -ENOMEM;
683 }
684
Charles Keepax1f88eb02013-02-05 10:41:47 +0000685 compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
686 GFP_KERNEL);
687 if (compr->ops == NULL) {
688 dev_err(rtd->card->dev, "Cannot allocate compressed ops\n");
689 ret = -ENOMEM;
690 goto compr_err;
691 }
Liam Girdwooda3f27052014-01-17 17:03:56 +0000692
693 if (rtd->dai_link->dynamic) {
694 snprintf(new_name, sizeof(new_name), "(%s)",
695 rtd->dai_link->stream_name);
696
697 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
698 1, 0, &be_pcm);
699 if (ret < 0) {
700 dev_err(rtd->card->dev, "ASoC: can't create compressed for %s\n",
701 rtd->dai_link->name);
702 goto compr_err;
703 }
704
705 rtd->pcm = be_pcm;
706 rtd->fe_compr = 1;
707 be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
708 be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
709 memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
710 } else
711 memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
Charles Keepax1f88eb02013-02-05 10:41:47 +0000712
713 /* Add copy callback for not memory mapped DSPs */
714 if (platform->driver->compr_ops && platform->driver->compr_ops->copy)
715 compr->ops->copy = soc_compr_copy;
716
Namarta Kohli1245b702012-08-16 17:10:41 +0530717 mutex_init(&compr->lock);
718 ret = snd_compress_new(rtd->card->snd_card, num, direction, compr);
719 if (ret < 0) {
720 pr_err("compress asoc: can't create compress for codec %s\n",
721 codec->name);
Charles Keepax1f88eb02013-02-05 10:41:47 +0000722 goto compr_err;
Namarta Kohli1245b702012-08-16 17:10:41 +0530723 }
724
Charles Keepax202c8f72013-01-24 09:44:30 +0000725 /* DAPM dai link stream work */
726 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
727
Namarta Kohli1245b702012-08-16 17:10:41 +0530728 rtd->compr = compr;
729 compr->private_data = rtd;
730
731 printk(KERN_INFO "compress asoc: %s <-> %s mapping ok\n", codec_dai->name,
732 cpu_dai->name);
733 return ret;
Charles Keepax1f88eb02013-02-05 10:41:47 +0000734
735compr_err:
736 kfree(compr);
737 return ret;
Namarta Kohli1245b702012-08-16 17:10:41 +0530738}