blob: fb4eed14e115700be9e0093039dcde8d63ede378 [file] [log] [blame]
Namarta Kohli9f052a72012-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 Girdwood660db5a2013-05-21 10:25:51 +010027#include <sound/soc-dpcm.h>
Namarta Kohli9f052a72012-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 Keepaxed87d162013-01-24 09:44:29 +000037 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
38
Namarta Kohli9f052a72012-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 Keepaxed87d162013-01-24 09:44:29 +000067 mutex_unlock(&rtd->pcm_mutex);
68
Namarta Kohli9f052a72012-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 Keepaxed87d162013-01-24 09:44:29 +000075 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli9f052a72012-08-16 17:10:41 +053076 return ret;
77}
78
Liam Girdwood660db5a2013-05-21 10:25:51 +010079static 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_dpcm_params *dpcm;
85 struct snd_soc_dapm_widget_list *list;
Haynes Mathew George2b4c8212013-07-26 13:38:01 -070086
87 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
88 struct snd_soc_dai *codec_dai = fe->codec_dai;
89
Liam Girdwood660db5a2013-05-21 10:25:51 +010090 int stream;
91 int ret = 0;
92
93 if (cstream->direction == SND_COMPRESS_PLAYBACK)
94 stream = SNDRV_PCM_STREAM_PLAYBACK;
95 else
96 stream = SNDRV_PCM_STREAM_CAPTURE;
97
Gopikrishnaiah Anandanffad0dc2014-01-03 14:24:55 -080098 mutex_lock(&fe->card->dpcm_mutex);
Liam Girdwood660db5a2013-05-21 10:25:51 +010099
100 if (platform->driver->compr_ops && platform->driver->compr_ops->open) {
101 ret = platform->driver->compr_ops->open(cstream);
102 if (ret < 0) {
103 pr_err("compress asoc: can't open platform %s\n", platform->name);
104 goto out;
105 }
106 }
107
108 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->startup) {
109 ret = fe->dai_link->compr_ops->startup(cstream);
110 if (ret < 0) {
111 pr_err("compress asoc: %s startup failed\n", fe->dai_link->name);
112 goto machine_err;
113 }
114 }
115
116 fe->dpcm[stream].runtime = fe_substream->runtime;
117
118 if (dpcm_path_get(fe, stream, &list) <= 0) {
119 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
120 fe->dai_link->name, stream ? "capture" : "playback");
121 }
122
123 /* calculate valid and active FE <-> BE dpcms */
124 dpcm_process_paths(fe, stream, &list, 1);
125
Eric Laurentdb88f282013-07-15 10:00:45 -0700126 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
127
Liam Girdwood660db5a2013-05-21 10:25:51 +0100128 ret = dpcm_be_dai_startup(fe, stream);
129 if (ret < 0) {
130 /* clean up all links */
131 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
132 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
133
134 dpcm_be_disconnect(fe, stream);
135 fe->dpcm[stream].runtime = NULL;
136 goto fe_err;
137 }
138
139 dpcm_clear_pending_state(fe, stream);
140 dpcm_path_put(&list);
141
Eric Laurentdb88f282013-07-15 10:00:45 -0700142 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
143 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
Haynes Mathew George2b4c8212013-07-26 13:38:01 -0700144
145 if (cstream->direction == SND_COMPRESS_PLAYBACK) {
146 cpu_dai->playback_active++;
147 codec_dai->playback_active++;
148 } else {
149 cpu_dai->capture_active++;
150 codec_dai->capture_active++;
151 }
152
153 cpu_dai->active++;
154 codec_dai->active++;
155 fe->codec->active++;
156
Gopikrishnaiah Anandanffad0dc2014-01-03 14:24:55 -0800157 mutex_unlock(&fe->card->dpcm_mutex);
Liam Girdwood660db5a2013-05-21 10:25:51 +0100158
159 return 0;
160
161fe_err:
162 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->shutdown)
163 fe->dai_link->compr_ops->shutdown(cstream);
164machine_err:
165 if (platform->driver->compr_ops && platform->driver->compr_ops->free)
166 platform->driver->compr_ops->free(cstream);
167out:
Eric Laurentdb88f282013-07-15 10:00:45 -0700168 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
Gopikrishnaiah Anandanffad0dc2014-01-03 14:24:55 -0800169 mutex_unlock(&fe->card->dpcm_mutex);
Liam Girdwood660db5a2013-05-21 10:25:51 +0100170 return ret;
171}
172
Charles Keepax57f8e792013-01-24 09:44:30 +0000173/*
174 * Power down the audio subsystem pmdown_time msecs after close is called.
175 * This is to ensure there are no pops or clicks in between any music tracks
176 * due to DAPM power cycling.
177 */
178static void close_delayed_work(struct work_struct *work)
179{
180 struct snd_soc_pcm_runtime *rtd =
181 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
182 struct snd_soc_dai *codec_dai = rtd->codec_dai;
183
184 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
185
186 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
187 codec_dai->driver->playback.stream_name,
188 codec_dai->playback_active ? "active" : "inactive",
Eric Laurent3359ca32013-04-08 16:07:19 -0700189 codec_dai->pop_wait ? "yes" : "no");
Charles Keepax57f8e792013-01-24 09:44:30 +0000190
191 /* are we waiting on this codec DAI stream */
Eric Laurent3359ca32013-04-08 16:07:19 -0700192 if (codec_dai->pop_wait == 1) {
193 codec_dai->pop_wait = 0;
Charles Keepax57f8e792013-01-24 09:44:30 +0000194 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
195 SND_SOC_DAPM_STREAM_STOP);
196 }
197
198 mutex_unlock(&rtd->pcm_mutex);
199}
200
Namarta Kohli9f052a72012-08-16 17:10:41 +0530201static int soc_compr_free(struct snd_compr_stream *cstream)
202{
203 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
204 struct snd_soc_platform *platform = rtd->platform;
205 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
206 struct snd_soc_dai *codec_dai = rtd->codec_dai;
207 struct snd_soc_codec *codec = rtd->codec;
208
Charles Keepaxed87d162013-01-24 09:44:29 +0000209 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
210
Namarta Kohli9f052a72012-08-16 17:10:41 +0530211 if (cstream->direction == SND_COMPRESS_PLAYBACK) {
212 cpu_dai->playback_active--;
213 codec_dai->playback_active--;
Mark Brown12a6e632013-02-06 13:52:42 +0000214 snd_soc_dai_digital_mute(codec_dai, 1);
Namarta Kohli9f052a72012-08-16 17:10:41 +0530215 } else {
216 cpu_dai->capture_active--;
217 codec_dai->capture_active--;
218 }
219
Namarta Kohli9f052a72012-08-16 17:10:41 +0530220 cpu_dai->active--;
221 codec_dai->active--;
222 codec->active--;
223
224 if (!cpu_dai->active)
225 cpu_dai->rate = 0;
226
227 if (!codec_dai->active)
228 codec_dai->rate = 0;
229
230
231 if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->shutdown)
232 rtd->dai_link->compr_ops->shutdown(cstream);
233
234 if (platform->driver->compr_ops && platform->driver->compr_ops->free)
235 platform->driver->compr_ops->free(cstream);
Vinod Koule3ee8fa2012-08-21 12:15:02 +0530236 cpu_dai->runtime = NULL;
Namarta Kohli9f052a72012-08-16 17:10:41 +0530237
238 if (cstream->direction == SND_COMPRESS_PLAYBACK) {
239 if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
240 rtd->dai_link->ignore_pmdown_time) {
241 snd_soc_dapm_stream_event(rtd,
Eric Laurentf2615f72013-04-08 15:52:05 -0700242 codec_dai->driver->playback.stream_name,
Namarta Kohli9f052a72012-08-16 17:10:41 +0530243 SND_SOC_DAPM_STREAM_STOP);
Charles Keepax808dc722013-01-24 09:44:28 +0000244 } else {
Namarta Kohli9f052a72012-08-16 17:10:41 +0530245 codec_dai->pop_wait = 1;
246 schedule_delayed_work(&rtd->delayed_work,
247 msecs_to_jiffies(rtd->pmdown_time));
Charles Keepax808dc722013-01-24 09:44:28 +0000248 }
Namarta Kohli9f052a72012-08-16 17:10:41 +0530249 } else {
250 /* capture streams can be powered down now */
251 snd_soc_dapm_stream_event(rtd,
Eric Laurentf2615f72013-04-08 15:52:05 -0700252 codec_dai->driver->capture.stream_name,
Namarta Kohli9f052a72012-08-16 17:10:41 +0530253 SND_SOC_DAPM_STREAM_STOP);
254 }
255
Charles Keepaxed87d162013-01-24 09:44:29 +0000256 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli9f052a72012-08-16 17:10:41 +0530257 return 0;
258}
259
Liam Girdwood660db5a2013-05-21 10:25:51 +0100260static 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_dpcm_params *dpcm;
265 int stream, ret;
266
Haynes Mathew George2b4c8212013-07-26 13:38:01 -0700267 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
268 struct snd_soc_dai *codec_dai = fe->codec_dai;
269 struct snd_soc_codec *codec = fe->codec;
270
Liam Girdwood660db5a2013-05-21 10:25:51 +0100271 if (cstream->direction == SND_COMPRESS_PLAYBACK)
272 stream = SNDRV_PCM_STREAM_PLAYBACK;
273 else
274 stream = SNDRV_PCM_STREAM_CAPTURE;
275
Gopikrishnaiah Anandanffad0dc2014-01-03 14:24:55 -0800276 mutex_lock(&fe->card->dpcm_mutex);
Haynes Mathew George2b4c8212013-07-26 13:38:01 -0700277 if (cstream->direction == SND_COMPRESS_PLAYBACK) {
278 cpu_dai->playback_active--;
279 codec_dai->playback_active--;
280 snd_soc_dai_digital_mute(codec_dai, 1);
281 } else {
282 cpu_dai->capture_active--;
283 codec_dai->capture_active--;
284 }
285
286 cpu_dai->active--;
287 codec_dai->active--;
288 codec->active--;
289
Eric Laurentdb88f282013-07-15 10:00:45 -0700290 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
291
Liam Girdwood660db5a2013-05-21 10:25:51 +0100292 ret = dpcm_be_dai_hw_free(fe, stream);
293 if (ret < 0)
294 dev_err(fe->dev, "compressed hw_free failed %d\n", ret);
295
296 ret = dpcm_be_dai_shutdown(fe, stream);
297
298 /* mark FE's links ready to prune */
299 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
300 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
301
Eric Laurentdb88f282013-07-15 10:00:45 -0700302 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
303 dpcm_dapm_stream_event(fe, stream,
304 fe->cpu_dai->driver->playback.stream_name,
305 SND_SOC_DAPM_STREAM_STOP);
306 else
307 dpcm_dapm_stream_event(fe, stream,
308 fe->cpu_dai->driver->capture.stream_name,
309 SND_SOC_DAPM_STREAM_STOP);
310
311 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
312 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
313
Liam Girdwood660db5a2013-05-21 10:25:51 +0100314 dpcm_be_disconnect(fe, stream);
315
316 fe->dpcm[stream].runtime = NULL;
317
318 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->shutdown)
319 fe->dai_link->compr_ops->shutdown(cstream);
320
321 if (platform->driver->compr_ops && platform->driver->compr_ops->free)
322 platform->driver->compr_ops->free(cstream);
323 //cpu_dai->runtime = NULL;
324
Gopikrishnaiah Anandanffad0dc2014-01-03 14:24:55 -0800325 mutex_unlock(&fe->card->dpcm_mutex);
Liam Girdwood660db5a2013-05-21 10:25:51 +0100326 return 0;
327}
328
Namarta Kohli9f052a72012-08-16 17:10:41 +0530329static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd)
330{
331
332 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
333 struct snd_soc_platform *platform = rtd->platform;
334 struct snd_soc_dai *codec_dai = rtd->codec_dai;
335 int ret = 0;
336
Eric Laurentc0c29a02013-08-28 14:53:09 -0700337 /* for partial drain and drain cmd, don't acquire lock while invoking DSP.
338 * These calls will be blocked till these operation can complete which
339 * will be a while. And during that time, app can invoke STOP, PAUSE etc
340 */
341 if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN ||
342 cmd == SND_COMPR_TRIGGER_DRAIN) {
343 if (platform->driver->compr_ops &&
344 platform->driver->compr_ops->trigger)
345 return platform->driver->compr_ops->trigger(cstream, cmd);
346 }
347
Charles Keepaxed87d162013-01-24 09:44:29 +0000348 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
349
Namarta Kohli9f052a72012-08-16 17:10:41 +0530350 if (platform->driver->compr_ops && platform->driver->compr_ops->trigger) {
351 ret = platform->driver->compr_ops->trigger(cstream, cmd);
352 if (ret < 0)
Charles Keepaxed87d162013-01-24 09:44:29 +0000353 goto out;
Namarta Kohli9f052a72012-08-16 17:10:41 +0530354 }
355
Mark Brown12a6e632013-02-06 13:52:42 +0000356 if (cstream->direction == SND_COMPRESS_PLAYBACK) {
357 switch (cmd) {
358 case SNDRV_PCM_TRIGGER_START:
359 snd_soc_dai_digital_mute(codec_dai, 0);
360 break;
361 case SNDRV_PCM_TRIGGER_STOP:
362 snd_soc_dai_digital_mute(codec_dai, 1);
363 break;
364 }
365 }
Namarta Kohli9f052a72012-08-16 17:10:41 +0530366
Charles Keepaxed87d162013-01-24 09:44:29 +0000367out:
368 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli9f052a72012-08-16 17:10:41 +0530369 return ret;
370}
371
Liam Girdwood660db5a2013-05-21 10:25:51 +0100372static int soc_compr_trigger_fe(struct snd_compr_stream *cstream, int cmd)
373{
374 struct snd_soc_pcm_runtime *fe = cstream->private_data;
375 struct snd_soc_platform *platform = fe->platform;
376 int ret = 0, stream;
377
Eric Laurentc0c29a02013-08-28 14:53:09 -0700378 /* for partial drain and drain cmd, don't acquire lock while invoking DSP.
379 * These calls will be blocked till these operation can complete which
380 * will be a while. And during that time, app can invoke STOP, PAUSE etc
381 */
382 if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN ||
383 cmd == SND_COMPR_TRIGGER_DRAIN) {
384 if (platform->driver->compr_ops &&
385 platform->driver->compr_ops->trigger)
386 return platform->driver->compr_ops->trigger(cstream, cmd);
387 }
388
Liam Girdwood660db5a2013-05-21 10:25:51 +0100389 if (cstream->direction == SND_COMPRESS_PLAYBACK)
390 stream = SNDRV_PCM_STREAM_PLAYBACK;
391 else
392 stream = SNDRV_PCM_STREAM_CAPTURE;
393
394
Gopikrishnaiah Anandanffad0dc2014-01-03 14:24:55 -0800395 mutex_lock(&fe->card->dpcm_mutex);
Liam Girdwood660db5a2013-05-21 10:25:51 +0100396 if (platform->driver->compr_ops && platform->driver->compr_ops->trigger) {
397 ret = platform->driver->compr_ops->trigger(cstream, cmd);
398 if (ret < 0)
399 goto out;
400 }
401
Eric Laurentdb88f282013-07-15 10:00:45 -0700402 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
403
Liam Girdwood660db5a2013-05-21 10:25:51 +0100404 ret = dpcm_be_dai_trigger(fe, stream, cmd);
405
Eric Laurentdb88f282013-07-15 10:00:45 -0700406 switch (cmd) {
407 case SNDRV_PCM_TRIGGER_START:
408 case SNDRV_PCM_TRIGGER_RESUME:
409 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
410 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
411 break;
412 case SNDRV_PCM_TRIGGER_STOP:
413 case SNDRV_PCM_TRIGGER_SUSPEND:
414 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
415 break;
416 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
417 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
418 break;
419 }
420
Liam Girdwood660db5a2013-05-21 10:25:51 +0100421out:
Eric Laurentdb88f282013-07-15 10:00:45 -0700422 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
Gopikrishnaiah Anandanffad0dc2014-01-03 14:24:55 -0800423 mutex_unlock(&fe->card->dpcm_mutex);
Liam Girdwood660db5a2013-05-21 10:25:51 +0100424 return ret;
425}
426
Namarta Kohli9f052a72012-08-16 17:10:41 +0530427static int soc_compr_set_params(struct snd_compr_stream *cstream,
428 struct snd_compr_params *params)
429{
430 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
431 struct snd_soc_platform *platform = rtd->platform;
432 struct snd_soc_dai *codec_dai = rtd->codec_dai;
433 int ret = 0;
434
Charles Keepaxed87d162013-01-24 09:44:29 +0000435 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
436
Namarta Kohli9f052a72012-08-16 17:10:41 +0530437 /* first we call set_params for the platform driver
438 * this should configure the soc side
439 * if the machine has compressed ops then we call that as well
440 * expectation is that platform and machine will configure everything
441 * for this compress path, like configuring pcm port for codec
442 */
443 if (platform->driver->compr_ops && platform->driver->compr_ops->set_params) {
444 ret = platform->driver->compr_ops->set_params(cstream, params);
445 if (ret < 0)
Charles Keepax32cfb4f2013-03-27 16:39:01 +0000446 goto err;
Namarta Kohli9f052a72012-08-16 17:10:41 +0530447 }
448
449 if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->set_params) {
450 ret = rtd->dai_link->compr_ops->set_params(cstream);
451 if (ret < 0)
Charles Keepax32cfb4f2013-03-27 16:39:01 +0000452 goto err;
Namarta Kohli9f052a72012-08-16 17:10:41 +0530453 }
454
Eric Laurentf2615f72013-04-08 15:52:05 -0700455 snd_soc_dapm_stream_event(rtd,
456 codec_dai->driver->playback.stream_name,
Namarta Kohli9f052a72012-08-16 17:10:41 +0530457 SND_SOC_DAPM_STREAM_START);
458
Charles Keepax32cfb4f2013-03-27 16:39:01 +0000459 /* cancel any delayed stream shutdown that is pending */
Eric Laurent429c06f2013-05-17 14:40:59 -0700460 codec_dai->pop_wait = 0;
Charles Keepax32cfb4f2013-03-27 16:39:01 +0000461 mutex_unlock(&rtd->pcm_mutex);
462
463 cancel_delayed_work_sync(&rtd->delayed_work);
464
465 return ret;
466
467err:
Charles Keepaxed87d162013-01-24 09:44:29 +0000468 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli9f052a72012-08-16 17:10:41 +0530469 return ret;
470}
471
Liam Girdwood660db5a2013-05-21 10:25:51 +0100472static int soc_compr_set_params_fe(struct snd_compr_stream *cstream,
473 struct snd_compr_params *params)
474{
475 struct snd_soc_pcm_runtime *fe = cstream->private_data;
476 struct snd_pcm_substream *fe_substream = fe->pcm->streams[0].substream;
477 struct snd_soc_platform *platform = fe->platform;
Liam Girdwood660db5a2013-05-21 10:25:51 +0100478 int ret = 0, stream;
479
480 if (cstream->direction == SND_COMPRESS_PLAYBACK)
481 stream = SNDRV_PCM_STREAM_PLAYBACK;
482 else
483 stream = SNDRV_PCM_STREAM_CAPTURE;
484
Gopikrishnaiah Anandanffad0dc2014-01-03 14:24:55 -0800485 mutex_lock(&fe->card->dpcm_mutex);
Liam Girdwood660db5a2013-05-21 10:25:51 +0100486 /* first we call set_params for the platform driver
487 * this should configure the soc side
488 * if the machine has compressed ops then we call that as well
489 * expectation is that platform and machine will configure everything
490 * for this compress path, like configuring pcm port for codec
491 */
492 if (platform->driver->compr_ops && platform->driver->compr_ops->set_params) {
493 ret = platform->driver->compr_ops->set_params(cstream, params);
494 if (ret < 0)
495 goto out;
496 }
497
498 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->set_params) {
499 ret = fe->dai_link->compr_ops->set_params(cstream);
500 if (ret < 0)
501 goto out;
502 }
503
Liam Girdwood660db5a2013-05-21 10:25:51 +0100504 memcpy(&fe->dpcm[fe_substream->stream].hw_params, params,
505 sizeof(struct snd_pcm_hw_params));
Eric Laurentdb88f282013-07-15 10:00:45 -0700506
507 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
508
Liam Girdwood660db5a2013-05-21 10:25:51 +0100509 ret = dpcm_be_dai_hw_params(fe, stream);
510 if (ret < 0)
511 goto out;
512
513 ret = dpcm_be_dai_prepare(fe, stream);
514 if (ret < 0)
515 goto out;
516
Eric Laurentdb88f282013-07-15 10:00:45 -0700517 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
518 dpcm_dapm_stream_event(fe, stream,
519 fe->cpu_dai->driver->playback.stream_name,
520 SND_SOC_DAPM_STREAM_START);
521 else
522 dpcm_dapm_stream_event(fe, stream,
523 fe->cpu_dai->driver->capture.stream_name,
524 SND_SOC_DAPM_STREAM_START);
525
526 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
527
Liam Girdwood660db5a2013-05-21 10:25:51 +0100528out:
Eric Laurentdb88f282013-07-15 10:00:45 -0700529 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
Gopikrishnaiah Anandanffad0dc2014-01-03 14:24:55 -0800530 mutex_unlock(&fe->card->dpcm_mutex);
Liam Girdwood660db5a2013-05-21 10:25:51 +0100531 return ret;
532}
533
Namarta Kohli9f052a72012-08-16 17:10:41 +0530534static int soc_compr_get_params(struct snd_compr_stream *cstream,
535 struct snd_codec *params)
536{
537 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
538 struct snd_soc_platform *platform = rtd->platform;
539 int ret = 0;
540
Charles Keepaxed87d162013-01-24 09:44:29 +0000541 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
542
Namarta Kohli9f052a72012-08-16 17:10:41 +0530543 if (platform->driver->compr_ops && platform->driver->compr_ops->get_params)
544 ret = platform->driver->compr_ops->get_params(cstream, params);
545
Charles Keepaxed87d162013-01-24 09:44:29 +0000546 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli9f052a72012-08-16 17:10:41 +0530547 return ret;
548}
549
550static int soc_compr_get_caps(struct snd_compr_stream *cstream,
551 struct snd_compr_caps *caps)
552{
553 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
554 struct snd_soc_platform *platform = rtd->platform;
555 int ret = 0;
556
Charles Keepaxed87d162013-01-24 09:44:29 +0000557 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
558
Namarta Kohli9f052a72012-08-16 17:10:41 +0530559 if (platform->driver->compr_ops && platform->driver->compr_ops->get_caps)
560 ret = platform->driver->compr_ops->get_caps(cstream, caps);
561
Charles Keepaxed87d162013-01-24 09:44:29 +0000562 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli9f052a72012-08-16 17:10:41 +0530563 return ret;
564}
565
566static int soc_compr_get_codec_caps(struct snd_compr_stream *cstream,
567 struct snd_compr_codec_caps *codec)
568{
569 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
570 struct snd_soc_platform *platform = rtd->platform;
571 int ret = 0;
572
Charles Keepaxed87d162013-01-24 09:44:29 +0000573 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
574
Namarta Kohli9f052a72012-08-16 17:10:41 +0530575 if (platform->driver->compr_ops && platform->driver->compr_ops->get_codec_caps)
576 ret = platform->driver->compr_ops->get_codec_caps(cstream, codec);
577
Charles Keepaxed87d162013-01-24 09:44:29 +0000578 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli9f052a72012-08-16 17:10:41 +0530579 return ret;
580}
581
582static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
583{
584 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
585 struct snd_soc_platform *platform = rtd->platform;
586 int ret = 0;
587
Charles Keepaxed87d162013-01-24 09:44:29 +0000588 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
589
Namarta Kohli9f052a72012-08-16 17:10:41 +0530590 if (platform->driver->compr_ops && platform->driver->compr_ops->ack)
591 ret = platform->driver->compr_ops->ack(cstream, bytes);
592
Charles Keepaxed87d162013-01-24 09:44:29 +0000593 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli9f052a72012-08-16 17:10:41 +0530594 return ret;
595}
596
597static int soc_compr_pointer(struct snd_compr_stream *cstream,
598 struct snd_compr_tstamp *tstamp)
599{
600 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
601 struct snd_soc_platform *platform = rtd->platform;
Aviral Guptac0df4302014-06-20 10:50:21 +0530602 int ret = 0;
Namarta Kohli9f052a72012-08-16 17:10:41 +0530603
Charles Keepaxed87d162013-01-24 09:44:29 +0000604 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
605
Namarta Kohli9f052a72012-08-16 17:10:41 +0530606 if (platform->driver->compr_ops && platform->driver->compr_ops->pointer)
Aviral Guptac0df4302014-06-20 10:50:21 +0530607 ret = platform->driver->compr_ops->pointer(cstream, tstamp);
Namarta Kohli9f052a72012-08-16 17:10:41 +0530608
Charles Keepaxed87d162013-01-24 09:44:29 +0000609 mutex_unlock(&rtd->pcm_mutex);
Aviral Guptac0df4302014-06-20 10:50:21 +0530610 return ret;
Namarta Kohli9f052a72012-08-16 17:10:41 +0530611}
612
Charles Keepax67f29ae2013-02-05 10:41:47 +0000613static int soc_compr_copy(struct snd_compr_stream *cstream,
Charles Keepax416e1d52013-04-18 11:01:38 +0100614 char __user *buf, size_t count)
Charles Keepax67f29ae2013-02-05 10:41:47 +0000615{
616 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
617 struct snd_soc_platform *platform = rtd->platform;
618 int ret = 0;
619
620 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
621
622 if (platform->driver->compr_ops && platform->driver->compr_ops->copy)
623 ret = platform->driver->compr_ops->copy(cstream, buf, count);
624
625 mutex_unlock(&rtd->pcm_mutex);
626 return ret;
627}
628
Jeeja KP1a1f1432013-03-26 21:22:28 +0530629static int sst_compr_set_metadata(struct snd_compr_stream *cstream,
630 struct snd_compr_metadata *metadata)
631{
632 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
633 struct snd_soc_platform *platform = rtd->platform;
634 int ret = 0;
635
636 if (platform->driver->compr_ops && platform->driver->compr_ops->set_metadata)
637 ret = platform->driver->compr_ops->set_metadata(cstream, metadata);
638
639 return ret;
640}
641
642static int sst_compr_get_metadata(struct snd_compr_stream *cstream,
643 struct snd_compr_metadata *metadata)
644{
645 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
646 struct snd_soc_platform *platform = rtd->platform;
647 int ret = 0;
648
649 if (platform->driver->compr_ops && platform->driver->compr_ops->get_metadata)
650 ret = platform->driver->compr_ops->get_metadata(cstream, metadata);
651
652 return ret;
653}
Namarta Kohli9f052a72012-08-16 17:10:41 +0530654/* ASoC Compress operations */
655static struct snd_compr_ops soc_compr_ops = {
656 .open = soc_compr_open,
657 .free = soc_compr_free,
658 .set_params = soc_compr_set_params,
Jeeja KP1a1f1432013-03-26 21:22:28 +0530659 .set_metadata = sst_compr_set_metadata,
660 .get_metadata = sst_compr_get_metadata,
Namarta Kohli9f052a72012-08-16 17:10:41 +0530661 .get_params = soc_compr_get_params,
662 .trigger = soc_compr_trigger,
663 .pointer = soc_compr_pointer,
664 .ack = soc_compr_ack,
665 .get_caps = soc_compr_get_caps,
666 .get_codec_caps = soc_compr_get_codec_caps
667};
668
Liam Girdwood660db5a2013-05-21 10:25:51 +0100669/* ASoC Dynamic Compress operations */
670static struct snd_compr_ops soc_compr_dyn_ops = {
671 .open = soc_compr_open_fe,
672 .free = soc_compr_free_fe,
673 .set_params = soc_compr_set_params_fe,
674 .get_params = soc_compr_get_params,
Eric Laurentf9323072013-09-04 08:43:05 -0700675 .set_metadata = sst_compr_set_metadata,
676 .get_metadata = sst_compr_get_metadata,
Liam Girdwood660db5a2013-05-21 10:25:51 +0100677 .trigger = soc_compr_trigger_fe,
678 .pointer = soc_compr_pointer,
679 .ack = soc_compr_ack,
680 .get_caps = soc_compr_get_caps,
681 .get_codec_caps = soc_compr_get_codec_caps
682};
683
Namarta Kohli9f052a72012-08-16 17:10:41 +0530684/* create a new compress */
685int soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
686{
687 struct snd_soc_codec *codec = rtd->codec;
Charles Keepax67f29ae2013-02-05 10:41:47 +0000688 struct snd_soc_platform *platform = rtd->platform;
Namarta Kohli9f052a72012-08-16 17:10:41 +0530689 struct snd_soc_dai *codec_dai = rtd->codec_dai;
690 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
691 struct snd_compr *compr;
Liam Girdwood660db5a2013-05-21 10:25:51 +0100692 struct snd_pcm *be_pcm;
Namarta Kohli9f052a72012-08-16 17:10:41 +0530693 char new_name[64];
694 int ret = 0, direction = 0;
695
696 /* check client and interface hw capabilities */
697 snprintf(new_name, sizeof(new_name), "%s %s-%d",
698 rtd->dai_link->stream_name, codec_dai->name, num);
699 direction = SND_COMPRESS_PLAYBACK;
700 compr = kzalloc(sizeof(*compr), GFP_KERNEL);
701 if (compr == NULL) {
702 snd_printk(KERN_ERR "Cannot allocate compr\n");
703 return -ENOMEM;
704 }
705
Charles Keepax67f29ae2013-02-05 10:41:47 +0000706 compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
707 GFP_KERNEL);
708 if (compr->ops == NULL) {
709 dev_err(rtd->card->dev, "Cannot allocate compressed ops\n");
710 ret = -ENOMEM;
711 goto compr_err;
712 }
Liam Girdwood660db5a2013-05-21 10:25:51 +0100713
714 if (rtd->dai_link->dynamic) {
715 snprintf(new_name, sizeof(new_name), "(%s)",
716 rtd->dai_link->stream_name);
717
718 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
719 1, 0, &be_pcm);
720 if (ret < 0) {
721 dev_err(rtd->card->dev, "ASoC: can't create compressed for %s\n",
722 rtd->dai_link->name);
723 goto compr_err;
724 }
725
726 rtd->pcm = be_pcm;
727 rtd->fe_compr = 1;
728 be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
729 //be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
730 memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
731 } else
732 memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
Charles Keepax67f29ae2013-02-05 10:41:47 +0000733
734 /* Add copy callback for not memory mapped DSPs */
735 if (platform->driver->compr_ops && platform->driver->compr_ops->copy)
736 compr->ops->copy = soc_compr_copy;
737
Namarta Kohli9f052a72012-08-16 17:10:41 +0530738 mutex_init(&compr->lock);
739 ret = snd_compress_new(rtd->card->snd_card, num, direction, compr);
740 if (ret < 0) {
741 pr_err("compress asoc: can't create compress for codec %s\n",
742 codec->name);
Charles Keepax67f29ae2013-02-05 10:41:47 +0000743 goto compr_err;
Namarta Kohli9f052a72012-08-16 17:10:41 +0530744 }
745
Charles Keepax57f8e792013-01-24 09:44:30 +0000746 /* DAPM dai link stream work */
747 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
748
Namarta Kohli9f052a72012-08-16 17:10:41 +0530749 rtd->compr = compr;
750 compr->private_data = rtd;
751
Ravi Kumar Alamandaeed58042013-11-25 10:38:40 -0800752 if (platform->driver->pcm_new) {
753 ret = platform->driver->pcm_new(rtd);
754 if (ret < 0) {
755 pr_err("asoc: compress pcm constructor failed\n");
756 goto compr_err;
757 }
758 }
759
Namarta Kohli9f052a72012-08-16 17:10:41 +0530760 printk(KERN_INFO "compress asoc: %s <-> %s mapping ok\n", codec_dai->name,
761 cpu_dai->name);
762 return ret;
Charles Keepax67f29ae2013-02-05 10:41:47 +0000763
764compr_err:
765 kfree(compr);
766 return ret;
Namarta Kohli9f052a72012-08-16 17:10:41 +0530767}