blob: 5e9690c85d8f5c9afc248830ce6007d0890f19dc [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 Girdwood2a99ef02014-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 Girdwood2a99ef02014-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;
Mark Brown3d24cfe2013-08-09 18:12:29 +0100245 queue_delayed_work(system_power_efficient_wq,
246 &rtd->delayed_work,
247 msecs_to_jiffies(rtd->pmdown_time));
Charles Keepax8c3d2aa2013-01-24 09:44:28 +0000248 }
Namarta Kohli1245b702012-08-16 17:10:41 +0530249 } 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
Charles Keepax15e2e612013-01-24 09:44:29 +0000256 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530257 return 0;
258}
259
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000260static 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
Namarta Kohli1245b702012-08-16 17:10:41 +0530319static 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
Charles Keepax15e2e612013-01-24 09:44:29 +0000327 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
328
Namarta Kohli1245b702012-08-16 17:10:41 +0530329 if (platform->driver->compr_ops && platform->driver->compr_ops->trigger) {
330 ret = platform->driver->compr_ops->trigger(cstream, cmd);
331 if (ret < 0)
Charles Keepax15e2e612013-01-24 09:44:29 +0000332 goto out;
Namarta Kohli1245b702012-08-16 17:10:41 +0530333 }
334
Mark Brownda183962013-02-06 15:44:07 +0000335 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;
Mark Browne38b9b72013-02-06 13:52:42 +0000342 }
Namarta Kohli1245b702012-08-16 17:10:41 +0530343
Charles Keepax15e2e612013-01-24 09:44:29 +0000344out:
345 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530346 return ret;
347}
348
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000349static 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
Namarta Kohli1245b702012-08-16 17:10:41 +0530402static 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;
Namarta Kohli1245b702012-08-16 17:10:41 +0530407 int ret = 0;
408
Charles Keepax15e2e612013-01-24 09:44:29 +0000409 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
410
Namarta Kohli1245b702012-08-16 17:10:41 +0530411 /* 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)
Charles Keepaxfa40ef22013-03-27 16:39:01 +0000420 goto err;
Namarta Kohli1245b702012-08-16 17:10:41 +0530421 }
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)
Charles Keepaxfa40ef22013-03-27 16:39:01 +0000426 goto err;
Namarta Kohli1245b702012-08-16 17:10:41 +0530427 }
428
Charles Keepax2c071ed2013-05-20 08:33:54 +0100429 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);
Namarta Kohli1245b702012-08-16 17:10:41 +0530435
Charles Keepaxfa40ef22013-03-27 16:39:01 +0000436 /* 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:
Charles Keepax15e2e612013-01-24 09:44:29 +0000445 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530446 return ret;
447}
448
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000449static 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
Namarta Kohli1245b702012-08-16 17:10:41 +0530507static 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
Charles Keepax15e2e612013-01-24 09:44:29 +0000514 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
515
Namarta Kohli1245b702012-08-16 17:10:41 +0530516 if (platform->driver->compr_ops && platform->driver->compr_ops->get_params)
517 ret = platform->driver->compr_ops->get_params(cstream, params);
518
Charles Keepax15e2e612013-01-24 09:44:29 +0000519 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530520 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
Charles Keepax15e2e612013-01-24 09:44:29 +0000530 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
531
Namarta Kohli1245b702012-08-16 17:10:41 +0530532 if (platform->driver->compr_ops && platform->driver->compr_ops->get_caps)
533 ret = platform->driver->compr_ops->get_caps(cstream, caps);
534
Charles Keepax15e2e612013-01-24 09:44:29 +0000535 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530536 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
Charles Keepax15e2e612013-01-24 09:44:29 +0000546 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
547
Namarta Kohli1245b702012-08-16 17:10:41 +0530548 if (platform->driver->compr_ops && platform->driver->compr_ops->get_codec_caps)
549 ret = platform->driver->compr_ops->get_codec_caps(cstream, codec);
550
Charles Keepax15e2e612013-01-24 09:44:29 +0000551 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530552 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
Charles Keepax15e2e612013-01-24 09:44:29 +0000561 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
562
Namarta Kohli1245b702012-08-16 17:10:41 +0530563 if (platform->driver->compr_ops && platform->driver->compr_ops->ack)
564 ret = platform->driver->compr_ops->ack(cstream, bytes);
565
Charles Keepax15e2e612013-01-24 09:44:29 +0000566 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530567 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
Charles Keepax15e2e612013-01-24 09:44:29 +0000576 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
577
Namarta Kohli1245b702012-08-16 17:10:41 +0530578 if (platform->driver->compr_ops && platform->driver->compr_ops->pointer)
579 platform->driver->compr_ops->pointer(cstream, tstamp);
580
Charles Keepax15e2e612013-01-24 09:44:29 +0000581 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530582 return 0;
583}
584
Charles Keepax1f88eb02013-02-05 10:41:47 +0000585static int soc_compr_copy(struct snd_compr_stream *cstream,
Charles Keepax4daf8912013-04-18 11:01:38 +0100586 char __user *buf, size_t count)
Charles Keepax1f88eb02013-02-05 10:41:47 +0000587{
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
Vinod Koul02bd90e2013-07-28 20:06:15 +0530601static int soc_compr_set_metadata(struct snd_compr_stream *cstream,
Jeeja KP36953d92013-03-26 21:22:28 +0530602 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
Vinod Koul02bd90e2013-07-28 20:06:15 +0530614static int soc_compr_get_metadata(struct snd_compr_stream *cstream,
Jeeja KP36953d92013-03-26 21:22:28 +0530615 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}
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000626
Namarta Kohli1245b702012-08-16 17:10:41 +0530627/* 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,
Vinod Koul02bd90e2013-07-28 20:06:15 +0530632 .set_metadata = soc_compr_set_metadata,
633 .get_metadata = soc_compr_get_metadata,
Namarta Kohli1245b702012-08-16 17:10:41 +0530634 .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
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000642/* 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
Namarta Kohli1245b702012-08-16 17:10:41 +0530657/* 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;
Charles Keepax1f88eb02013-02-05 10:41:47 +0000661 struct snd_soc_platform *platform = rtd->platform;
Namarta Kohli1245b702012-08-16 17:10:41 +0530662 struct snd_soc_dai *codec_dai = rtd->codec_dai;
663 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
664 struct snd_compr *compr;
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000665 struct snd_pcm *be_pcm;
Namarta Kohli1245b702012-08-16 17:10:41 +0530666 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);
Charles Keepaxdaa2db52013-04-18 11:02:38 +0100672
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
Namarta Kohli1245b702012-08-16 17:10:41 +0530680 compr = kzalloc(sizeof(*compr), GFP_KERNEL);
681 if (compr == NULL) {
682 snd_printk(KERN_ERR "Cannot allocate compr\n");
683 return -ENOMEM;
684 }
685
Charles Keepax1f88eb02013-02-05 10:41:47 +0000686 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 }
Liam Girdwood2a99ef02014-01-17 17:03:56 +0000693
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));
Charles Keepax1f88eb02013-02-05 10:41:47 +0000713
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
Namarta Kohli1245b702012-08-16 17:10:41 +0530718 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);
Charles Keepax1f88eb02013-02-05 10:41:47 +0000723 goto compr_err;
Namarta Kohli1245b702012-08-16 17:10:41 +0530724 }
725
Charles Keepax202c8f72013-01-24 09:44:30 +0000726 /* DAPM dai link stream work */
727 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
728
Namarta Kohli1245b702012-08-16 17:10:41 +0530729 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;
Charles Keepax1f88eb02013-02-05 10:41:47 +0000735
736compr_err:
737 kfree(compr);
738 return ret;
Namarta Kohli1245b702012-08-16 17:10:41 +0530739}