blob: b5b3db71e25303813bfe216e7f0c7020dcf97ff3 [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>
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
Charles Keepax15e2e612013-01-24 09:44:29 +000036 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
37
Namarta Kohli1245b702012-08-16 17:10:41 +053038 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
Charles Keepax15e2e612013-01-24 09:44:29 +000066 mutex_unlock(&rtd->pcm_mutex);
67
Namarta Kohli1245b702012-08-16 17:10:41 +053068 return 0;
69
70machine_err:
71 if (platform->driver->compr_ops && platform->driver->compr_ops->free)
72 platform->driver->compr_ops->free(cstream);
73out:
Charles Keepax15e2e612013-01-24 09:44:29 +000074 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +053075 return ret;
76}
77
Charles Keepax202c8f72013-01-24 09:44:30 +000078/*
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
Namarta Kohli1245b702012-08-16 17:10:41 +0530106static 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
Charles Keepax15e2e612013-01-24 09:44:29 +0000114 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
115
Namarta Kohli1245b702012-08-16 17:10:41 +0530116 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
Mark Brownda183962013-02-06 15:44:07 +0000124 snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
125
Namarta Kohli1245b702012-08-16 17:10:41 +0530126 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);
Vinod Koul9d2667a2012-08-21 12:15:02 +0530142 cpu_dai->runtime = NULL;
Namarta Kohli1245b702012-08-16 17:10:41 +0530143
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);
Charles Keepax8c3d2aa2013-01-24 09:44:28 +0000150 } else {
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600151 rtd->pop_wait = 1;
Namarta Kohli1245b702012-08-16 17:10:41 +0530152 schedule_delayed_work(&rtd->delayed_work,
153 msecs_to_jiffies(rtd->pmdown_time));
Charles Keepax8c3d2aa2013-01-24 09:44:28 +0000154 }
Namarta Kohli1245b702012-08-16 17:10:41 +0530155 } 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
Charles Keepax15e2e612013-01-24 09:44:29 +0000162 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530163 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
Charles Keepax15e2e612013-01-24 09:44:29 +0000174 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
175
Namarta Kohli1245b702012-08-16 17:10:41 +0530176 if (platform->driver->compr_ops && platform->driver->compr_ops->trigger) {
177 ret = platform->driver->compr_ops->trigger(cstream, cmd);
178 if (ret < 0)
Charles Keepax15e2e612013-01-24 09:44:29 +0000179 goto out;
Namarta Kohli1245b702012-08-16 17:10:41 +0530180 }
181
Mark Brownda183962013-02-06 15:44:07 +0000182 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;
Mark Browne38b9b72013-02-06 13:52:42 +0000189 }
Namarta Kohli1245b702012-08-16 17:10:41 +0530190
Charles Keepax15e2e612013-01-24 09:44:29 +0000191out:
192 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530193 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;
Namarta Kohli1245b702012-08-16 17:10:41 +0530201 int ret = 0;
202
Charles Keepax15e2e612013-01-24 09:44:29 +0000203 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
204
Namarta Kohli1245b702012-08-16 17:10:41 +0530205 /* 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)
Charles Keepax15e2e612013-01-24 09:44:29 +0000214 goto out;
Namarta Kohli1245b702012-08-16 17:10:41 +0530215 }
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)
Charles Keepax15e2e612013-01-24 09:44:29 +0000220 goto out;
Namarta Kohli1245b702012-08-16 17:10:41 +0530221 }
222
223 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
224 SND_SOC_DAPM_STREAM_START);
225
Charles Keepax15e2e612013-01-24 09:44:29 +0000226out:
227 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530228 return ret;
229}
230
231static int soc_compr_get_params(struct snd_compr_stream *cstream,
232 struct snd_codec *params)
233{
234 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
235 struct snd_soc_platform *platform = rtd->platform;
236 int ret = 0;
237
Charles Keepax15e2e612013-01-24 09:44:29 +0000238 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
239
Namarta Kohli1245b702012-08-16 17:10:41 +0530240 if (platform->driver->compr_ops && platform->driver->compr_ops->get_params)
241 ret = platform->driver->compr_ops->get_params(cstream, params);
242
Charles Keepax15e2e612013-01-24 09:44:29 +0000243 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530244 return ret;
245}
246
247static int soc_compr_get_caps(struct snd_compr_stream *cstream,
248 struct snd_compr_caps *caps)
249{
250 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
251 struct snd_soc_platform *platform = rtd->platform;
252 int ret = 0;
253
Charles Keepax15e2e612013-01-24 09:44:29 +0000254 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
255
Namarta Kohli1245b702012-08-16 17:10:41 +0530256 if (platform->driver->compr_ops && platform->driver->compr_ops->get_caps)
257 ret = platform->driver->compr_ops->get_caps(cstream, caps);
258
Charles Keepax15e2e612013-01-24 09:44:29 +0000259 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530260 return ret;
261}
262
263static int soc_compr_get_codec_caps(struct snd_compr_stream *cstream,
264 struct snd_compr_codec_caps *codec)
265{
266 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
267 struct snd_soc_platform *platform = rtd->platform;
268 int ret = 0;
269
Charles Keepax15e2e612013-01-24 09:44:29 +0000270 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
271
Namarta Kohli1245b702012-08-16 17:10:41 +0530272 if (platform->driver->compr_ops && platform->driver->compr_ops->get_codec_caps)
273 ret = platform->driver->compr_ops->get_codec_caps(cstream, codec);
274
Charles Keepax15e2e612013-01-24 09:44:29 +0000275 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530276 return ret;
277}
278
279static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
280{
281 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
282 struct snd_soc_platform *platform = rtd->platform;
283 int ret = 0;
284
Charles Keepax15e2e612013-01-24 09:44:29 +0000285 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
286
Namarta Kohli1245b702012-08-16 17:10:41 +0530287 if (platform->driver->compr_ops && platform->driver->compr_ops->ack)
288 ret = platform->driver->compr_ops->ack(cstream, bytes);
289
Charles Keepax15e2e612013-01-24 09:44:29 +0000290 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530291 return ret;
292}
293
294static int soc_compr_pointer(struct snd_compr_stream *cstream,
295 struct snd_compr_tstamp *tstamp)
296{
297 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
298 struct snd_soc_platform *platform = rtd->platform;
299
Charles Keepax15e2e612013-01-24 09:44:29 +0000300 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
301
Namarta Kohli1245b702012-08-16 17:10:41 +0530302 if (platform->driver->compr_ops && platform->driver->compr_ops->pointer)
303 platform->driver->compr_ops->pointer(cstream, tstamp);
304
Charles Keepax15e2e612013-01-24 09:44:29 +0000305 mutex_unlock(&rtd->pcm_mutex);
Namarta Kohli1245b702012-08-16 17:10:41 +0530306 return 0;
307}
308
Charles Keepax1f88eb02013-02-05 10:41:47 +0000309static int soc_compr_copy(struct snd_compr_stream *cstream,
310 const char __user *buf, size_t count)
311{
312 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
313 struct snd_soc_platform *platform = rtd->platform;
314 int ret = 0;
315
316 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
317
318 if (platform->driver->compr_ops && platform->driver->compr_ops->copy)
319 ret = platform->driver->compr_ops->copy(cstream, buf, count);
320
321 mutex_unlock(&rtd->pcm_mutex);
322 return ret;
323}
324
Namarta Kohli1245b702012-08-16 17:10:41 +0530325/* ASoC Compress operations */
326static struct snd_compr_ops soc_compr_ops = {
327 .open = soc_compr_open,
328 .free = soc_compr_free,
329 .set_params = soc_compr_set_params,
330 .get_params = soc_compr_get_params,
331 .trigger = soc_compr_trigger,
332 .pointer = soc_compr_pointer,
333 .ack = soc_compr_ack,
334 .get_caps = soc_compr_get_caps,
335 .get_codec_caps = soc_compr_get_codec_caps
336};
337
338/* create a new compress */
339int soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
340{
341 struct snd_soc_codec *codec = rtd->codec;
Charles Keepax1f88eb02013-02-05 10:41:47 +0000342 struct snd_soc_platform *platform = rtd->platform;
Namarta Kohli1245b702012-08-16 17:10:41 +0530343 struct snd_soc_dai *codec_dai = rtd->codec_dai;
344 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
345 struct snd_compr *compr;
346 char new_name[64];
347 int ret = 0, direction = 0;
348
349 /* check client and interface hw capabilities */
350 snprintf(new_name, sizeof(new_name), "%s %s-%d",
351 rtd->dai_link->stream_name, codec_dai->name, num);
352 direction = SND_COMPRESS_PLAYBACK;
353 compr = kzalloc(sizeof(*compr), GFP_KERNEL);
354 if (compr == NULL) {
355 snd_printk(KERN_ERR "Cannot allocate compr\n");
356 return -ENOMEM;
357 }
358
Charles Keepax1f88eb02013-02-05 10:41:47 +0000359 compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
360 GFP_KERNEL);
361 if (compr->ops == NULL) {
362 dev_err(rtd->card->dev, "Cannot allocate compressed ops\n");
363 ret = -ENOMEM;
364 goto compr_err;
365 }
366 memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
367
368 /* Add copy callback for not memory mapped DSPs */
369 if (platform->driver->compr_ops && platform->driver->compr_ops->copy)
370 compr->ops->copy = soc_compr_copy;
371
Namarta Kohli1245b702012-08-16 17:10:41 +0530372 mutex_init(&compr->lock);
373 ret = snd_compress_new(rtd->card->snd_card, num, direction, compr);
374 if (ret < 0) {
375 pr_err("compress asoc: can't create compress for codec %s\n",
376 codec->name);
Charles Keepax1f88eb02013-02-05 10:41:47 +0000377 goto compr_err;
Namarta Kohli1245b702012-08-16 17:10:41 +0530378 }
379
Charles Keepax202c8f72013-01-24 09:44:30 +0000380 /* DAPM dai link stream work */
381 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
382
Namarta Kohli1245b702012-08-16 17:10:41 +0530383 rtd->compr = compr;
384 compr->private_data = rtd;
385
386 printk(KERN_INFO "compress asoc: %s <-> %s mapping ok\n", codec_dai->name,
387 cpu_dai->name);
388 return ret;
Charles Keepax1f88eb02013-02-05 10:41:47 +0000389
390compr_err:
391 kfree(compr);
392 return ret;
Namarta Kohli1245b702012-08-16 17:10:41 +0530393}