blob: 0970a835b5d781f49c4f509a2012d8178ef4f475 [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;
478 struct snd_pcm_hw_params *hw_params;
479 int ret = 0, stream;
480
481 if (cstream->direction == SND_COMPRESS_PLAYBACK)
482 stream = SNDRV_PCM_STREAM_PLAYBACK;
483 else
484 stream = SNDRV_PCM_STREAM_CAPTURE;
485
486 hw_params = kzalloc(sizeof(*hw_params), GFP_KERNEL);
487 if (hw_params == NULL)
488 return -ENOMEM;
489
Gopikrishnaiah Anandanffad0dc2014-01-03 14:24:55 -0800490 mutex_lock(&fe->card->dpcm_mutex);
Liam Girdwood660db5a2013-05-21 10:25:51 +0100491 /* first we call set_params for the platform driver
492 * this should configure the soc side
493 * if the machine has compressed ops then we call that as well
494 * expectation is that platform and machine will configure everything
495 * for this compress path, like configuring pcm port for codec
496 */
497 if (platform->driver->compr_ops && platform->driver->compr_ops->set_params) {
498 ret = platform->driver->compr_ops->set_params(cstream, params);
499 if (ret < 0)
500 goto out;
501 }
502
503 if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->set_params) {
504 ret = fe->dai_link->compr_ops->set_params(cstream);
505 if (ret < 0)
506 goto out;
507 }
508
Liam Girdwood660db5a2013-05-21 10:25:51 +0100509 memcpy(&fe->dpcm[fe_substream->stream].hw_params, params,
510 sizeof(struct snd_pcm_hw_params));
Eric Laurentdb88f282013-07-15 10:00:45 -0700511
512 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
513
Liam Girdwood660db5a2013-05-21 10:25:51 +0100514 ret = dpcm_be_dai_hw_params(fe, stream);
515 if (ret < 0)
516 goto out;
517
518 ret = dpcm_be_dai_prepare(fe, stream);
519 if (ret < 0)
520 goto out;
521
Eric Laurentdb88f282013-07-15 10:00:45 -0700522 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
523 dpcm_dapm_stream_event(fe, stream,
524 fe->cpu_dai->driver->playback.stream_name,
525 SND_SOC_DAPM_STREAM_START);
526 else
527 dpcm_dapm_stream_event(fe, stream,
528 fe->cpu_dai->driver->capture.stream_name,
529 SND_SOC_DAPM_STREAM_START);
530
531 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
532
Liam Girdwood660db5a2013-05-21 10:25:51 +0100533out:
Eric Laurentdb88f282013-07-15 10:00:45 -0700534 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
Gopikrishnaiah Anandanffad0dc2014-01-03 14:24:55 -0800535 mutex_unlock(&fe->card->dpcm_mutex);
Liam Girdwood660db5a2013-05-21 10:25:51 +0100536 return ret;
537}
538
Namarta Kohli9f052a72012-08-16 17:10:41 +0530539static int soc_compr_get_params(struct snd_compr_stream *cstream,
540 struct snd_codec *params)
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 Keepaxed87d162013-01-24 09:44:29 +0000546 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
547
Namarta Kohli9f052a72012-08-16 17:10:41 +0530548 if (platform->driver->compr_ops && platform->driver->compr_ops->get_params)
549 ret = platform->driver->compr_ops->get_params(cstream, params);
550
Charles Keepaxed87d162013-01-24 09:44:29 +0000551 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli9f052a72012-08-16 17:10:41 +0530552 return ret;
553}
554
555static int soc_compr_get_caps(struct snd_compr_stream *cstream,
556 struct snd_compr_caps *caps)
557{
558 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
559 struct snd_soc_platform *platform = rtd->platform;
560 int ret = 0;
561
Charles Keepaxed87d162013-01-24 09:44:29 +0000562 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
563
Namarta Kohli9f052a72012-08-16 17:10:41 +0530564 if (platform->driver->compr_ops && platform->driver->compr_ops->get_caps)
565 ret = platform->driver->compr_ops->get_caps(cstream, caps);
566
Charles Keepaxed87d162013-01-24 09:44:29 +0000567 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli9f052a72012-08-16 17:10:41 +0530568 return ret;
569}
570
571static int soc_compr_get_codec_caps(struct snd_compr_stream *cstream,
572 struct snd_compr_codec_caps *codec)
573{
574 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
575 struct snd_soc_platform *platform = rtd->platform;
576 int ret = 0;
577
Charles Keepaxed87d162013-01-24 09:44:29 +0000578 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
579
Namarta Kohli9f052a72012-08-16 17:10:41 +0530580 if (platform->driver->compr_ops && platform->driver->compr_ops->get_codec_caps)
581 ret = platform->driver->compr_ops->get_codec_caps(cstream, codec);
582
Charles Keepaxed87d162013-01-24 09:44:29 +0000583 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli9f052a72012-08-16 17:10:41 +0530584 return ret;
585}
586
587static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
588{
589 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
590 struct snd_soc_platform *platform = rtd->platform;
591 int ret = 0;
592
Charles Keepaxed87d162013-01-24 09:44:29 +0000593 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
594
Namarta Kohli9f052a72012-08-16 17:10:41 +0530595 if (platform->driver->compr_ops && platform->driver->compr_ops->ack)
596 ret = platform->driver->compr_ops->ack(cstream, bytes);
597
Charles Keepaxed87d162013-01-24 09:44:29 +0000598 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli9f052a72012-08-16 17:10:41 +0530599 return ret;
600}
601
602static int soc_compr_pointer(struct snd_compr_stream *cstream,
603 struct snd_compr_tstamp *tstamp)
604{
605 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
606 struct snd_soc_platform *platform = rtd->platform;
607
Charles Keepaxed87d162013-01-24 09:44:29 +0000608 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
609
Namarta Kohli9f052a72012-08-16 17:10:41 +0530610 if (platform->driver->compr_ops && platform->driver->compr_ops->pointer)
611 platform->driver->compr_ops->pointer(cstream, tstamp);
612
Charles Keepaxed87d162013-01-24 09:44:29 +0000613 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli9f052a72012-08-16 17:10:41 +0530614 return 0;
615}
616
Charles Keepax67f29ae2013-02-05 10:41:47 +0000617static int soc_compr_copy(struct snd_compr_stream *cstream,
Charles Keepax416e1d52013-04-18 11:01:38 +0100618 char __user *buf, size_t count)
Charles Keepax67f29ae2013-02-05 10:41:47 +0000619{
620 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
621 struct snd_soc_platform *platform = rtd->platform;
622 int ret = 0;
623
624 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
625
626 if (platform->driver->compr_ops && platform->driver->compr_ops->copy)
627 ret = platform->driver->compr_ops->copy(cstream, buf, count);
628
629 mutex_unlock(&rtd->pcm_mutex);
630 return ret;
631}
632
Jeeja KP1a1f1432013-03-26 21:22:28 +0530633static int sst_compr_set_metadata(struct snd_compr_stream *cstream,
634 struct snd_compr_metadata *metadata)
635{
636 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
637 struct snd_soc_platform *platform = rtd->platform;
638 int ret = 0;
639
640 if (platform->driver->compr_ops && platform->driver->compr_ops->set_metadata)
641 ret = platform->driver->compr_ops->set_metadata(cstream, metadata);
642
643 return ret;
644}
645
646static int sst_compr_get_metadata(struct snd_compr_stream *cstream,
647 struct snd_compr_metadata *metadata)
648{
649 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
650 struct snd_soc_platform *platform = rtd->platform;
651 int ret = 0;
652
653 if (platform->driver->compr_ops && platform->driver->compr_ops->get_metadata)
654 ret = platform->driver->compr_ops->get_metadata(cstream, metadata);
655
656 return ret;
657}
Namarta Kohli9f052a72012-08-16 17:10:41 +0530658/* ASoC Compress operations */
659static struct snd_compr_ops soc_compr_ops = {
660 .open = soc_compr_open,
661 .free = soc_compr_free,
662 .set_params = soc_compr_set_params,
Jeeja KP1a1f1432013-03-26 21:22:28 +0530663 .set_metadata = sst_compr_set_metadata,
664 .get_metadata = sst_compr_get_metadata,
Namarta Kohli9f052a72012-08-16 17:10:41 +0530665 .get_params = soc_compr_get_params,
666 .trigger = soc_compr_trigger,
667 .pointer = soc_compr_pointer,
668 .ack = soc_compr_ack,
669 .get_caps = soc_compr_get_caps,
670 .get_codec_caps = soc_compr_get_codec_caps
671};
672
Liam Girdwood660db5a2013-05-21 10:25:51 +0100673/* ASoC Dynamic Compress operations */
674static struct snd_compr_ops soc_compr_dyn_ops = {
675 .open = soc_compr_open_fe,
676 .free = soc_compr_free_fe,
677 .set_params = soc_compr_set_params_fe,
678 .get_params = soc_compr_get_params,
Eric Laurentf9323072013-09-04 08:43:05 -0700679 .set_metadata = sst_compr_set_metadata,
680 .get_metadata = sst_compr_get_metadata,
Liam Girdwood660db5a2013-05-21 10:25:51 +0100681 .trigger = soc_compr_trigger_fe,
682 .pointer = soc_compr_pointer,
683 .ack = soc_compr_ack,
684 .get_caps = soc_compr_get_caps,
685 .get_codec_caps = soc_compr_get_codec_caps
686};
687
Namarta Kohli9f052a72012-08-16 17:10:41 +0530688/* create a new compress */
689int soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
690{
691 struct snd_soc_codec *codec = rtd->codec;
Charles Keepax67f29ae2013-02-05 10:41:47 +0000692 struct snd_soc_platform *platform = rtd->platform;
Namarta Kohli9f052a72012-08-16 17:10:41 +0530693 struct snd_soc_dai *codec_dai = rtd->codec_dai;
694 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
695 struct snd_compr *compr;
Liam Girdwood660db5a2013-05-21 10:25:51 +0100696 struct snd_pcm *be_pcm;
Namarta Kohli9f052a72012-08-16 17:10:41 +0530697 char new_name[64];
698 int ret = 0, direction = 0;
699
700 /* check client and interface hw capabilities */
701 snprintf(new_name, sizeof(new_name), "%s %s-%d",
702 rtd->dai_link->stream_name, codec_dai->name, num);
703 direction = SND_COMPRESS_PLAYBACK;
704 compr = kzalloc(sizeof(*compr), GFP_KERNEL);
705 if (compr == NULL) {
706 snd_printk(KERN_ERR "Cannot allocate compr\n");
707 return -ENOMEM;
708 }
709
Charles Keepax67f29ae2013-02-05 10:41:47 +0000710 compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
711 GFP_KERNEL);
712 if (compr->ops == NULL) {
713 dev_err(rtd->card->dev, "Cannot allocate compressed ops\n");
714 ret = -ENOMEM;
715 goto compr_err;
716 }
Liam Girdwood660db5a2013-05-21 10:25:51 +0100717
718 if (rtd->dai_link->dynamic) {
719 snprintf(new_name, sizeof(new_name), "(%s)",
720 rtd->dai_link->stream_name);
721
722 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
723 1, 0, &be_pcm);
724 if (ret < 0) {
725 dev_err(rtd->card->dev, "ASoC: can't create compressed for %s\n",
726 rtd->dai_link->name);
727 goto compr_err;
728 }
729
730 rtd->pcm = be_pcm;
731 rtd->fe_compr = 1;
732 be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
733 //be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
734 memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
735 } else
736 memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
Charles Keepax67f29ae2013-02-05 10:41:47 +0000737
738 /* Add copy callback for not memory mapped DSPs */
739 if (platform->driver->compr_ops && platform->driver->compr_ops->copy)
740 compr->ops->copy = soc_compr_copy;
741
Namarta Kohli9f052a72012-08-16 17:10:41 +0530742 mutex_init(&compr->lock);
743 ret = snd_compress_new(rtd->card->snd_card, num, direction, compr);
744 if (ret < 0) {
745 pr_err("compress asoc: can't create compress for codec %s\n",
746 codec->name);
Charles Keepax67f29ae2013-02-05 10:41:47 +0000747 goto compr_err;
Namarta Kohli9f052a72012-08-16 17:10:41 +0530748 }
749
Charles Keepax57f8e792013-01-24 09:44:30 +0000750 /* DAPM dai link stream work */
751 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
752
Namarta Kohli9f052a72012-08-16 17:10:41 +0530753 rtd->compr = compr;
754 compr->private_data = rtd;
755
Ravi Kumar Alamandaeed58042013-11-25 10:38:40 -0800756 if (platform->driver->pcm_new) {
757 ret = platform->driver->pcm_new(rtd);
758 if (ret < 0) {
759 pr_err("asoc: compress pcm constructor failed\n");
760 goto compr_err;
761 }
762 }
763
Namarta Kohli9f052a72012-08-16 17:10:41 +0530764 printk(KERN_INFO "compress asoc: %s <-> %s mapping ok\n", codec_dai->name,
765 cpu_dai->name);
766 return ret;
Charles Keepax67f29ae2013-02-05 10:41:47 +0000767
768compr_err:
769 kfree(compr);
770 return ret;
Namarta Kohli9f052a72012-08-16 17:10:41 +0530771}