ALSA: compress_core: Add support for capture streams
[linux-2.6-block.git] / sound / soc / soc-compress.c
CommitLineData
1245b700
NK
1/*
2 * soc-compress.c -- ALSA SoC Compress
3 *
4 * Copyright (C) 2012 Intel Corp.
5 *
6 * Authors: Namarta Kohli <namartax.kohli@intel.com>
7 * Ramesh Babu K V <ramesh.babu@linux.intel.com>
8 * Vinod Koul <vinod.koul@linux.intel.com>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 *
15 */
16
17#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/delay.h>
20#include <linux/slab.h>
21#include <linux/workqueue.h>
22#include <sound/core.h>
23#include <sound/compress_params.h>
24#include <sound/compress_driver.h>
25#include <sound/soc.h>
26#include <sound/initval.h>
27
28static int soc_compr_open(struct snd_compr_stream *cstream)
29{
30 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
31 struct snd_soc_platform *platform = rtd->platform;
32 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
33 struct snd_soc_dai *codec_dai = rtd->codec_dai;
34 int ret = 0;
35
15e2e619
CK
36 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
37
1245b700
NK
38 if (platform->driver->compr_ops && platform->driver->compr_ops->open) {
39 ret = platform->driver->compr_ops->open(cstream);
40 if (ret < 0) {
41 pr_err("compress asoc: can't open platform %s\n", platform->name);
42 goto out;
43 }
44 }
45
46 if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->startup) {
47 ret = rtd->dai_link->compr_ops->startup(cstream);
48 if (ret < 0) {
49 pr_err("compress asoc: %s startup failed\n", rtd->dai_link->name);
50 goto machine_err;
51 }
52 }
53
54 if (cstream->direction == SND_COMPRESS_PLAYBACK) {
55 cpu_dai->playback_active++;
56 codec_dai->playback_active++;
57 } else {
58 cpu_dai->capture_active++;
59 codec_dai->capture_active++;
60 }
61
62 cpu_dai->active++;
63 codec_dai->active++;
64 rtd->codec->active++;
65
15e2e619
CK
66 mutex_unlock(&rtd->pcm_mutex);
67
1245b700
NK
68 return 0;
69
70machine_err:
71 if (platform->driver->compr_ops && platform->driver->compr_ops->free)
72 platform->driver->compr_ops->free(cstream);
73out:
15e2e619 74 mutex_unlock(&rtd->pcm_mutex);
1245b700
NK
75 return ret;
76}
77
202c8f70
CK
78/*
79 * Power down the audio subsystem pmdown_time msecs after close is called.
80 * This is to ensure there are no pops or clicks in between any music tracks
81 * due to DAPM power cycling.
82 */
83static void close_delayed_work(struct work_struct *work)
84{
85 struct snd_soc_pcm_runtime *rtd =
86 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
87 struct snd_soc_dai *codec_dai = rtd->codec_dai;
88
89 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
90
91 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
92 codec_dai->driver->playback.stream_name,
93 codec_dai->playback_active ? "active" : "inactive",
94 rtd->pop_wait ? "yes" : "no");
95
96 /* are we waiting on this codec DAI stream */
97 if (rtd->pop_wait == 1) {
98 rtd->pop_wait = 0;
99 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
100 SND_SOC_DAPM_STREAM_STOP);
101 }
102
103 mutex_unlock(&rtd->pcm_mutex);
104}
105
1245b700
NK
106static int soc_compr_free(struct snd_compr_stream *cstream)
107{
108 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
109 struct snd_soc_platform *platform = rtd->platform;
110 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
111 struct snd_soc_dai *codec_dai = rtd->codec_dai;
112 struct snd_soc_codec *codec = rtd->codec;
113
15e2e619
CK
114 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
115
1245b700
NK
116 if (cstream->direction == SND_COMPRESS_PLAYBACK) {
117 cpu_dai->playback_active--;
118 codec_dai->playback_active--;
119 } else {
120 cpu_dai->capture_active--;
121 codec_dai->capture_active--;
122 }
123
da18396f
MB
124 snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
125
1245b700
NK
126 cpu_dai->active--;
127 codec_dai->active--;
128 codec->active--;
129
130 if (!cpu_dai->active)
131 cpu_dai->rate = 0;
132
133 if (!codec_dai->active)
134 codec_dai->rate = 0;
135
136
137 if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->shutdown)
138 rtd->dai_link->compr_ops->shutdown(cstream);
139
140 if (platform->driver->compr_ops && platform->driver->compr_ops->free)
141 platform->driver->compr_ops->free(cstream);
9d2667a9 142 cpu_dai->runtime = NULL;
1245b700
NK
143
144 if (cstream->direction == SND_COMPRESS_PLAYBACK) {
145 if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
146 rtd->dai_link->ignore_pmdown_time) {
147 snd_soc_dapm_stream_event(rtd,
148 SNDRV_PCM_STREAM_PLAYBACK,
149 SND_SOC_DAPM_STREAM_STOP);
8c3d2aa4 150 } else {
9bffb1fb 151 rtd->pop_wait = 1;
1245b700
NK
152 schedule_delayed_work(&rtd->delayed_work,
153 msecs_to_jiffies(rtd->pmdown_time));
8c3d2aa4 154 }
1245b700
NK
155 } else {
156 /* capture streams can be powered down now */
157 snd_soc_dapm_stream_event(rtd,
158 SNDRV_PCM_STREAM_CAPTURE,
159 SND_SOC_DAPM_STREAM_STOP);
160 }
161
15e2e619 162 mutex_unlock(&rtd->pcm_mutex);
1245b700
NK
163 return 0;
164}
165
166static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd)
167{
168
169 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
170 struct snd_soc_platform *platform = rtd->platform;
171 struct snd_soc_dai *codec_dai = rtd->codec_dai;
172 int ret = 0;
173
15e2e619
CK
174 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
175
1245b700
NK
176 if (platform->driver->compr_ops && platform->driver->compr_ops->trigger) {
177 ret = platform->driver->compr_ops->trigger(cstream, cmd);
178 if (ret < 0)
15e2e619 179 goto out;
1245b700
NK
180 }
181
da18396f
MB
182 switch (cmd) {
183 case SNDRV_PCM_TRIGGER_START:
184 snd_soc_dai_digital_mute(codec_dai, 0, cstream->direction);
185 break;
186 case SNDRV_PCM_TRIGGER_STOP:
187 snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
188 break;
e38b9b74 189 }
1245b700 190
15e2e619
CK
191out:
192 mutex_unlock(&rtd->pcm_mutex);
1245b700
NK
193 return ret;
194}
195
196static int soc_compr_set_params(struct snd_compr_stream *cstream,
197 struct snd_compr_params *params)
198{
199 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
200 struct snd_soc_platform *platform = rtd->platform;
1245b700
NK
201 int ret = 0;
202
15e2e619
CK
203 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
204
1245b700
NK
205 /* first we call set_params for the platform driver
206 * this should configure the soc side
207 * if the machine has compressed ops then we call that as well
208 * expectation is that platform and machine will configure everything
209 * for this compress path, like configuring pcm port for codec
210 */
211 if (platform->driver->compr_ops && platform->driver->compr_ops->set_params) {
212 ret = platform->driver->compr_ops->set_params(cstream, params);
213 if (ret < 0)
fa40ef20 214 goto err;
1245b700
NK
215 }
216
217 if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->set_params) {
218 ret = rtd->dai_link->compr_ops->set_params(cstream);
219 if (ret < 0)
fa40ef20 220 goto err;
1245b700
NK
221 }
222
223 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
224 SND_SOC_DAPM_STREAM_START);
225
fa40ef20
CK
226 /* cancel any delayed stream shutdown that is pending */
227 rtd->pop_wait = 0;
228 mutex_unlock(&rtd->pcm_mutex);
229
230 cancel_delayed_work_sync(&rtd->delayed_work);
231
232 return ret;
233
234err:
15e2e619 235 mutex_unlock(&rtd->pcm_mutex);
1245b700
NK
236 return ret;
237}
238
239static int soc_compr_get_params(struct snd_compr_stream *cstream,
240 struct snd_codec *params)
241{
242 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
243 struct snd_soc_platform *platform = rtd->platform;
244 int ret = 0;
245
15e2e619
CK
246 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
247
1245b700
NK
248 if (platform->driver->compr_ops && platform->driver->compr_ops->get_params)
249 ret = platform->driver->compr_ops->get_params(cstream, params);
250
15e2e619 251 mutex_unlock(&rtd->pcm_mutex);
1245b700
NK
252 return ret;
253}
254
255static int soc_compr_get_caps(struct snd_compr_stream *cstream,
256 struct snd_compr_caps *caps)
257{
258 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
259 struct snd_soc_platform *platform = rtd->platform;
260 int ret = 0;
261
15e2e619
CK
262 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
263
1245b700
NK
264 if (platform->driver->compr_ops && platform->driver->compr_ops->get_caps)
265 ret = platform->driver->compr_ops->get_caps(cstream, caps);
266
15e2e619 267 mutex_unlock(&rtd->pcm_mutex);
1245b700
NK
268 return ret;
269}
270
271static int soc_compr_get_codec_caps(struct snd_compr_stream *cstream,
272 struct snd_compr_codec_caps *codec)
273{
274 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
275 struct snd_soc_platform *platform = rtd->platform;
276 int ret = 0;
277
15e2e619
CK
278 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
279
1245b700
NK
280 if (platform->driver->compr_ops && platform->driver->compr_ops->get_codec_caps)
281 ret = platform->driver->compr_ops->get_codec_caps(cstream, codec);
282
15e2e619 283 mutex_unlock(&rtd->pcm_mutex);
1245b700
NK
284 return ret;
285}
286
287static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
288{
289 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
290 struct snd_soc_platform *platform = rtd->platform;
291 int ret = 0;
292
15e2e619
CK
293 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
294
1245b700
NK
295 if (platform->driver->compr_ops && platform->driver->compr_ops->ack)
296 ret = platform->driver->compr_ops->ack(cstream, bytes);
297
15e2e619 298 mutex_unlock(&rtd->pcm_mutex);
1245b700
NK
299 return ret;
300}
301
302static int soc_compr_pointer(struct snd_compr_stream *cstream,
303 struct snd_compr_tstamp *tstamp)
304{
305 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
306 struct snd_soc_platform *platform = rtd->platform;
307
15e2e619
CK
308 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
309
1245b700
NK
310 if (platform->driver->compr_ops && platform->driver->compr_ops->pointer)
311 platform->driver->compr_ops->pointer(cstream, tstamp);
312
15e2e619 313 mutex_unlock(&rtd->pcm_mutex);
1245b700
NK
314 return 0;
315}
316
1f88eb0f 317static int soc_compr_copy(struct snd_compr_stream *cstream,
4daf891c 318 char __user *buf, size_t count)
1f88eb0f
CK
319{
320 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
321 struct snd_soc_platform *platform = rtd->platform;
322 int ret = 0;
323
324 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
325
326 if (platform->driver->compr_ops && platform->driver->compr_ops->copy)
327 ret = platform->driver->compr_ops->copy(cstream, buf, count);
328
329 mutex_unlock(&rtd->pcm_mutex);
330 return ret;
331}
332
36953d98
JK
333static int sst_compr_set_metadata(struct snd_compr_stream *cstream,
334 struct snd_compr_metadata *metadata)
335{
336 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
337 struct snd_soc_platform *platform = rtd->platform;
338 int ret = 0;
339
340 if (platform->driver->compr_ops && platform->driver->compr_ops->set_metadata)
341 ret = platform->driver->compr_ops->set_metadata(cstream, metadata);
342
343 return ret;
344}
345
346static int sst_compr_get_metadata(struct snd_compr_stream *cstream,
347 struct snd_compr_metadata *metadata)
348{
349 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
350 struct snd_soc_platform *platform = rtd->platform;
351 int ret = 0;
352
353 if (platform->driver->compr_ops && platform->driver->compr_ops->get_metadata)
354 ret = platform->driver->compr_ops->get_metadata(cstream, metadata);
355
356 return ret;
357}
1245b700
NK
358/* ASoC Compress operations */
359static struct snd_compr_ops soc_compr_ops = {
360 .open = soc_compr_open,
361 .free = soc_compr_free,
362 .set_params = soc_compr_set_params,
36953d98
JK
363 .set_metadata = sst_compr_set_metadata,
364 .get_metadata = sst_compr_get_metadata,
1245b700
NK
365 .get_params = soc_compr_get_params,
366 .trigger = soc_compr_trigger,
367 .pointer = soc_compr_pointer,
368 .ack = soc_compr_ack,
369 .get_caps = soc_compr_get_caps,
370 .get_codec_caps = soc_compr_get_codec_caps
371};
372
373/* create a new compress */
374int soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
375{
376 struct snd_soc_codec *codec = rtd->codec;
1f88eb0f 377 struct snd_soc_platform *platform = rtd->platform;
1245b700
NK
378 struct snd_soc_dai *codec_dai = rtd->codec_dai;
379 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
380 struct snd_compr *compr;
381 char new_name[64];
382 int ret = 0, direction = 0;
383
384 /* check client and interface hw capabilities */
385 snprintf(new_name, sizeof(new_name), "%s %s-%d",
386 rtd->dai_link->stream_name, codec_dai->name, num);
387 direction = SND_COMPRESS_PLAYBACK;
388 compr = kzalloc(sizeof(*compr), GFP_KERNEL);
389 if (compr == NULL) {
390 snd_printk(KERN_ERR "Cannot allocate compr\n");
391 return -ENOMEM;
392 }
393
1f88eb0f
CK
394 compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
395 GFP_KERNEL);
396 if (compr->ops == NULL) {
397 dev_err(rtd->card->dev, "Cannot allocate compressed ops\n");
398 ret = -ENOMEM;
399 goto compr_err;
400 }
401 memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
402
403 /* Add copy callback for not memory mapped DSPs */
404 if (platform->driver->compr_ops && platform->driver->compr_ops->copy)
405 compr->ops->copy = soc_compr_copy;
406
1245b700
NK
407 mutex_init(&compr->lock);
408 ret = snd_compress_new(rtd->card->snd_card, num, direction, compr);
409 if (ret < 0) {
410 pr_err("compress asoc: can't create compress for codec %s\n",
411 codec->name);
1f88eb0f 412 goto compr_err;
1245b700
NK
413 }
414
202c8f70
CK
415 /* DAPM dai link stream work */
416 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
417
1245b700
NK
418 rtd->compr = compr;
419 compr->private_data = rtd;
420
421 printk(KERN_INFO "compress asoc: %s <-> %s mapping ok\n", codec_dai->name,
422 cpu_dai->name);
423 return ret;
1f88eb0f
CK
424
425compr_err:
426 kfree(compr);
427 return ret;
1245b700 428}