blob: 68f218f8797a9ed9b52f26229de4c7a99cbb373a [file] [log] [blame]
Krishnankutty Kolathappilly46dded22012-01-12 12:03:34 -08001/* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
Asish Bhattacharya305d1752011-11-01 20:38:26 +05302 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13
14#include <linux/init.h>
15#include <linux/err.h>
16#include <linux/module.h>
17#include <linux/moduleparam.h>
18#include <linux/time.h>
19#include <linux/wait.h>
20#include <linux/platform_device.h>
21#include <linux/slab.h>
22#include <sound/core.h>
23#include <sound/soc.h>
24#include <sound/soc-dapm.h>
25#include <sound/pcm.h>
26#include <sound/initval.h>
27#include <sound/control.h>
Santosh Mardie27ed782012-03-29 04:20:38 +053028#include <sound/q6asm.h>
Asish Bhattacharya305d1752011-11-01 20:38:26 +053029#include <asm/dma.h>
30#include <linux/dma-mapping.h>
31#include <linux/android_pmem.h>
Krishnankutty Kolathappillyf02f9ef2012-04-19 23:40:05 -070032#include <sound/timer.h>
Asish Bhattacharya305d1752011-11-01 20:38:26 +053033
34#include "msm-compr-q6.h"
35#include "msm-pcm-routing.h"
36
Krishnankutty Kolathappilly27d7b302012-02-29 21:04:31 -080037struct snd_msm {
38 struct msm_audio *prtd;
39 unsigned volume;
40};
41static struct snd_msm compressed_audio = {NULL, 0x2000} ;
42
Asish Bhattacharya305d1752011-11-01 20:38:26 +053043static struct audio_locks the_locks;
44
45static struct snd_pcm_hardware msm_compr_hardware_playback = {
46 .info = (SNDRV_PCM_INFO_MMAP |
47 SNDRV_PCM_INFO_BLOCK_TRANSFER |
48 SNDRV_PCM_INFO_MMAP_VALID |
49 SNDRV_PCM_INFO_INTERLEAVED |
50 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
51 .formats = SNDRV_PCM_FMTBIT_S16_LE,
Krishnankutty Kolathappillyfce9dbd2012-03-15 19:52:54 -070052 .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_KNOT,
Asish Bhattacharya305d1752011-11-01 20:38:26 +053053 .rate_min = 8000,
54 .rate_max = 48000,
55 .channels_min = 1,
56 .channels_max = 2,
57 .buffer_bytes_max = 1200 * 1024 * 2,
Krishnankutty Kolathappillya966f452012-01-12 12:28:36 -080058 .period_bytes_min = 4800,
Asish Bhattacharya305d1752011-11-01 20:38:26 +053059 .period_bytes_max = 1200 * 1024,
60 .periods_min = 2,
Krishnankutty Kolathappillya966f452012-01-12 12:28:36 -080061 .periods_max = 512,
Asish Bhattacharya305d1752011-11-01 20:38:26 +053062 .fifo_size = 0,
63};
64
65/* Conventional and unconventional sample rate supported */
66static unsigned int supported_sample_rates[] = {
67 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000
68};
69
70static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
71 .count = ARRAY_SIZE(supported_sample_rates),
72 .list = supported_sample_rates,
73 .mask = 0,
74};
75
76static void compr_event_handler(uint32_t opcode,
77 uint32_t token, uint32_t *payload, void *priv)
78{
79 struct compr_audio *compr = priv;
80 struct msm_audio *prtd = &compr->prtd;
81 struct snd_pcm_substream *substream = prtd->substream;
82 struct snd_pcm_runtime *runtime = substream->runtime;
83 struct audio_aio_write_param param;
84 struct audio_buffer *buf = NULL;
85 int i = 0;
86
87 pr_debug("%s opcode =%08x\n", __func__, opcode);
88 switch (opcode) {
89 case ASM_DATA_EVENT_WRITE_DONE: {
90 uint32_t *ptrmem = (uint32_t *)&param;
91 pr_debug("ASM_DATA_EVENT_WRITE_DONE\n");
92 pr_debug("Buffer Consumed = 0x%08x\n", *ptrmem);
93 prtd->pcm_irq_pos += prtd->pcm_count;
94 if (atomic_read(&prtd->start))
95 snd_pcm_period_elapsed(substream);
Krishnankutty Kolathappillyf02f9ef2012-04-19 23:40:05 -070096 else
97 if (substream->timer_running)
98 snd_timer_interrupt(substream->timer, 1);
Asish Bhattacharya305d1752011-11-01 20:38:26 +053099 atomic_inc(&prtd->out_count);
100 wake_up(&the_locks.write_wait);
101 if (!atomic_read(&prtd->start)) {
Asish Bhattacharya31bd80d2012-01-04 02:30:13 +0530102 atomic_set(&prtd->pending_buffer, 1);
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530103 break;
104 } else
Asish Bhattacharya31bd80d2012-01-04 02:30:13 +0530105 atomic_set(&prtd->pending_buffer, 0);
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530106
107 if (runtime->status->hw_ptr >= runtime->control->appl_ptr)
108 break;
109 buf = prtd->audio_client->port[IN].buf;
110 pr_debug("%s:writing %d bytes of buffer[%d] to dsp 2\n",
111 __func__, prtd->pcm_count, prtd->out_head);
112 pr_debug("%s:writing buffer[%d] from 0x%08x\n",
113 __func__, prtd->out_head,
114 ((unsigned int)buf[0].phys
115 + (prtd->out_head * prtd->pcm_count)));
116
117 param.paddr = (unsigned long)buf[0].phys
118 + (prtd->out_head * prtd->pcm_count);
119 param.len = prtd->pcm_count;
120 param.msw_ts = 0;
121 param.lsw_ts = 0;
122 param.flags = NO_TIMESTAMP;
123 param.uid = (unsigned long)buf[0].phys
124 + (prtd->out_head * prtd->pcm_count);
125 for (i = 0; i < sizeof(struct audio_aio_write_param)/4;
126 i++, ++ptrmem)
127 pr_debug("cmd[%d]=0x%08x\n", i, *ptrmem);
128 if (q6asm_async_write(prtd->audio_client,
129 &param) < 0)
130 pr_err("%s:q6asm_async_write failed\n",
131 __func__);
132 else
133 prtd->out_head =
134 (prtd->out_head + 1) & (runtime->periods - 1);
135 break;
136 }
137 case ASM_DATA_CMDRSP_EOS:
138 pr_debug("ASM_DATA_CMDRSP_EOS\n");
139 prtd->cmd_ack = 1;
140 wake_up(&the_locks.eos_wait);
141 break;
142 case APR_BASIC_RSP_RESULT: {
143 switch (payload[0]) {
144 case ASM_SESSION_CMD_RUN: {
Asish Bhattacharya31bd80d2012-01-04 02:30:13 +0530145 if (!atomic_read(&prtd->pending_buffer))
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530146 break;
147 pr_debug("%s:writing %d bytes"
148 " of buffer[%d] to dsp\n",
149 __func__, prtd->pcm_count, prtd->out_head);
150 buf = prtd->audio_client->port[IN].buf;
151 pr_debug("%s:writing buffer[%d] from 0x%08x\n",
152 __func__, prtd->out_head,
153 ((unsigned int)buf[0].phys
154 + (prtd->out_head * prtd->pcm_count)));
155 param.paddr = (unsigned long)buf[prtd->out_head].phys;
156 param.len = prtd->pcm_count;
157 param.msw_ts = 0;
158 param.lsw_ts = 0;
159 param.flags = NO_TIMESTAMP;
160 param.uid = (unsigned long)buf[prtd->out_head].phys;
161 if (q6asm_async_write(prtd->audio_client,
162 &param) < 0)
163 pr_err("%s:q6asm_async_write failed\n",
164 __func__);
165 else
166 prtd->out_head =
167 (prtd->out_head + 1)
168 & (runtime->periods - 1);
Asish Bhattacharya31bd80d2012-01-04 02:30:13 +0530169 atomic_set(&prtd->pending_buffer, 0);
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530170 }
171 break;
172 case ASM_STREAM_CMD_FLUSH:
173 pr_debug("ASM_STREAM_CMD_FLUSH\n");
174 prtd->cmd_ack = 1;
175 wake_up(&the_locks.eos_wait);
176 break;
177 default:
178 break;
179 }
180 break;
181 }
182 default:
183 pr_debug("Not Supported Event opcode[0x%x]\n", opcode);
184 break;
185 }
186}
187
188static int msm_compr_playback_prepare(struct snd_pcm_substream *substream)
189{
190 struct snd_pcm_runtime *runtime = substream->runtime;
191 struct compr_audio *compr = runtime->private_data;
192 struct msm_audio *prtd = &compr->prtd;
Krishnankutty Kolathappilly46dded22012-01-12 12:03:34 -0800193 struct asm_aac_cfg aac_cfg;
Aviral Gupta66698c12012-04-28 22:09:48 +0530194 struct asm_wma_cfg wma_cfg;
Aviral Guptac4ee3b02012-05-09 10:53:45 +0530195 struct asm_wmapro_cfg wma_pro_cfg;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530196 int ret;
197
Santosh Mardie27ed782012-03-29 04:20:38 +0530198 pr_debug("compressed stream prepare\n");
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530199 prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
200 prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
201 prtd->pcm_irq_pos = 0;
202 /* rate and channels are sent to audio driver */
203 prtd->samp_rate = runtime->rate;
204 prtd->channel_mode = runtime->channels;
205 prtd->out_head = 0;
Krishnankutty Kolathappilly46dded22012-01-12 12:03:34 -0800206 atomic_set(&prtd->out_count, runtime->periods);
207
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530208 if (prtd->enabled)
209 return 0;
210
Krishnankutty Kolathappilly46dded22012-01-12 12:03:34 -0800211 switch (compr->info.codec_param.codec.id) {
212 case SND_AUDIOCODEC_MP3:
213 ret = q6asm_media_format_block(prtd->audio_client,
214 compr->codec);
215 if (ret < 0)
216 pr_info("%s: CMD Format block failed\n", __func__);
217 break;
218 case SND_AUDIOCODEC_AAC:
219 pr_debug("SND_AUDIOCODEC_AAC\n");
220 memset(&aac_cfg, 0x0, sizeof(struct asm_aac_cfg));
221 aac_cfg.aot = AAC_ENC_MODE_EAAC_P;
222 aac_cfg.format = 0x03;
223 aac_cfg.ch_cfg = runtime->channels;
Aviral Gupta66698c12012-04-28 22:09:48 +0530224 aac_cfg.sample_rate = runtime->rate;
Krishnankutty Kolathappilly46dded22012-01-12 12:03:34 -0800225 ret = q6asm_media_format_block_aac(prtd->audio_client,
226 &aac_cfg);
227 if (ret < 0)
228 pr_err("%s: CMD Format block failed\n", __func__);
229 break;
Santosh Mardie27ed782012-03-29 04:20:38 +0530230 case SND_AUDIOCODEC_AC3_PASS_THROUGH:
231 pr_debug("compressd playback, no need to send"
232 " the decoder params\n");
233 break;
Aviral Gupta66698c12012-04-28 22:09:48 +0530234 case SND_AUDIOCODEC_WMA:
235 pr_debug("SND_AUDIOCODEC_WMA\n");
236 memset(&wma_cfg, 0x0, sizeof(struct asm_wma_cfg));
237 wma_cfg.format_tag = compr->info.codec_param.codec.format;
238 wma_cfg.ch_cfg = runtime->channels;
239 wma_cfg.sample_rate = runtime->rate;
240 wma_cfg.avg_bytes_per_sec =
241 compr->info.codec_param.codec.bit_rate/8;
242 wma_cfg.block_align = compr->info.codec_param.codec.align;
243 wma_cfg.valid_bits_per_sample =
244 compr->info.codec_param.codec.options.wma.bits_per_sample;
245 wma_cfg.ch_mask =
246 compr->info.codec_param.codec.options.wma.channelmask;
247 wma_cfg.encode_opt =
248 compr->info.codec_param.codec.options.wma.encodeopt;
249 ret = q6asm_media_format_block_wma(prtd->audio_client,
250 &wma_cfg);
251 if (ret < 0)
252 pr_err("%s: CMD Format block failed\n", __func__);
253 break;
Aviral Guptac4ee3b02012-05-09 10:53:45 +0530254 case SND_AUDIOCODEC_WMA_PRO:
255 pr_debug("SND_AUDIOCODEC_WMA_PRO\n");
256 memset(&wma_pro_cfg, 0x0, sizeof(struct asm_wmapro_cfg));
257 wma_pro_cfg.format_tag = compr->info.codec_param.codec.format;
258 wma_pro_cfg.ch_cfg = compr->info.codec_param.codec.ch_in;
259 wma_pro_cfg.sample_rate = runtime->rate;
260 wma_pro_cfg.avg_bytes_per_sec =
261 compr->info.codec_param.codec.bit_rate/8;
262 wma_pro_cfg.block_align = compr->info.codec_param.codec.align;
263 wma_pro_cfg.valid_bits_per_sample =
264 compr->info.codec_param.codec.options.wma.bits_per_sample;
265 wma_pro_cfg.ch_mask =
266 compr->info.codec_param.codec.options.wma.channelmask;
267 wma_pro_cfg.encode_opt =
268 compr->info.codec_param.codec.options.wma.encodeopt;
269 ret = q6asm_media_format_block_wmapro(prtd->audio_client,
270 &wma_pro_cfg);
271 if (ret < 0)
272 pr_err("%s: CMD Format block failed\n", __func__);
273 break;
Krishnankutty Kolathappilly46dded22012-01-12 12:03:34 -0800274 default:
275 return -EINVAL;
276 }
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530277
278 prtd->enabled = 1;
279 prtd->cmd_ack = 0;
280
281 return 0;
282}
283
284static int msm_compr_trigger(struct snd_pcm_substream *substream, int cmd)
285{
286 int ret = 0;
287 struct snd_pcm_runtime *runtime = substream->runtime;
Santosh Mardie27ed782012-03-29 04:20:38 +0530288 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530289 struct compr_audio *compr = runtime->private_data;
290 struct msm_audio *prtd = &compr->prtd;
291
292 pr_debug("%s\n", __func__);
293 switch (cmd) {
294 case SNDRV_PCM_TRIGGER_START:
295 prtd->pcm_irq_pos = 0;
Santosh Mardie27ed782012-03-29 04:20:38 +0530296 if (compr->info.codec_param.codec.id ==
297 SND_AUDIOCODEC_AC3_PASS_THROUGH) {
298 msm_pcm_routing_reg_psthr_stream(
299 soc_prtd->dai_link->be_id,
300 prtd->session_id, substream->stream);
301 }
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530302 case SNDRV_PCM_TRIGGER_RESUME:
303 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
304 pr_debug("%s: Trigger start\n", __func__);
305 q6asm_run_nowait(prtd->audio_client, 0, 0, 0);
306 atomic_set(&prtd->start, 1);
307 break;
308 case SNDRV_PCM_TRIGGER_STOP:
309 pr_debug("SNDRV_PCM_TRIGGER_STOP\n");
310 atomic_set(&prtd->start, 0);
311 break;
312 case SNDRV_PCM_TRIGGER_SUSPEND:
313 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
314 pr_debug("SNDRV_PCM_TRIGGER_PAUSE\n");
315 q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
316 atomic_set(&prtd->start, 0);
317 break;
318 default:
319 ret = -EINVAL;
320 break;
321 }
322
323 return ret;
324}
325
326static void populate_codec_list(struct compr_audio *compr,
327 struct snd_pcm_runtime *runtime)
328{
329 pr_debug("%s\n", __func__);
330 /* MP3 Block */
331 compr->info.compr_cap.num_codecs = 1;
332 compr->info.compr_cap.min_fragment_size = runtime->hw.period_bytes_min;
333 compr->info.compr_cap.max_fragment_size = runtime->hw.period_bytes_max;
334 compr->info.compr_cap.min_fragments = runtime->hw.periods_min;
335 compr->info.compr_cap.max_fragments = runtime->hw.periods_max;
336 compr->info.compr_cap.codecs[0] = SND_AUDIOCODEC_MP3;
Krishnankutty Kolathappilly46dded22012-01-12 12:03:34 -0800337 compr->info.compr_cap.codecs[1] = SND_AUDIOCODEC_AAC;
Santosh Mardie27ed782012-03-29 04:20:38 +0530338 compr->info.compr_cap.codecs[2] = SND_AUDIOCODEC_AC3_PASS_THROUGH;
Aviral Gupta66698c12012-04-28 22:09:48 +0530339 compr->info.compr_cap.codecs[3] = SND_AUDIOCODEC_WMA;
Aviral Guptac4ee3b02012-05-09 10:53:45 +0530340 compr->info.compr_cap.codecs[4] = SND_AUDIOCODEC_WMA_PRO;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530341 /* Add new codecs here */
342}
343
344static int msm_compr_open(struct snd_pcm_substream *substream)
345{
346 struct snd_pcm_runtime *runtime = substream->runtime;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530347 struct compr_audio *compr;
348 struct msm_audio *prtd;
349 int ret = 0;
Krishnankutty Kolathappilly27d7b302012-02-29 21:04:31 -0800350 struct asm_softpause_params softpause = {
351 .enable = SOFT_PAUSE_ENABLE,
352 .period = SOFT_PAUSE_PERIOD,
353 .step = SOFT_PAUSE_STEP,
354 .rampingcurve = SOFT_PAUSE_CURVE_LINEAR,
355 };
356 struct asm_softvolume_params softvol = {
357 .period = SOFT_VOLUME_PERIOD,
358 .step = SOFT_VOLUME_STEP,
359 .rampingcurve = SOFT_VOLUME_CURVE_LINEAR,
360 };
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530361
362 /* Capture path */
363 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
364 return -EINVAL;
365
366 pr_debug("%s\n", __func__);
367 compr = kzalloc(sizeof(struct compr_audio), GFP_KERNEL);
368 if (compr == NULL) {
369 pr_err("Failed to allocate memory for msm_audio\n");
370 return -ENOMEM;
371 }
372 prtd = &compr->prtd;
373 prtd->substream = substream;
374 prtd->audio_client = q6asm_audio_client_alloc(
375 (app_cb)compr_event_handler, compr);
376 if (!prtd->audio_client) {
377 pr_info("%s: Could not allocate memory\n", __func__);
378 kfree(prtd);
379 return -ENOMEM;
380 }
381 runtime->hw = msm_compr_hardware_playback;
382
383 pr_info("%s: session ID %d\n", __func__, prtd->audio_client->session);
384
385 prtd->session_id = prtd->audio_client->session;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530386
387 prtd->cmd_ack = 1;
388
389 ret = snd_pcm_hw_constraint_list(runtime, 0,
390 SNDRV_PCM_HW_PARAM_RATE,
391 &constraints_sample_rates);
392 if (ret < 0)
393 pr_info("snd_pcm_hw_constraint_list failed\n");
394 /* Ensure that buffer size is a multiple of period size */
395 ret = snd_pcm_hw_constraint_integer(runtime,
396 SNDRV_PCM_HW_PARAM_PERIODS);
397 if (ret < 0)
398 pr_info("snd_pcm_hw_constraint_integer failed\n");
399
400 prtd->dsp_cnt = 0;
Asish Bhattacharya31bd80d2012-01-04 02:30:13 +0530401 atomic_set(&prtd->pending_buffer, 1);
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530402 compr->codec = FORMAT_MP3;
403 populate_codec_list(compr, runtime);
404 runtime->private_data = compr;
Krishnankutty Kolathappilly27d7b302012-02-29 21:04:31 -0800405 compressed_audio.prtd = &compr->prtd;
406 ret = compressed_set_volume(compressed_audio.volume);
407 if (ret < 0)
408 pr_err("%s : Set Volume failed : %d", __func__, ret);
409
410 ret = q6asm_set_softpause(compressed_audio.prtd->audio_client,
411 &softpause);
412 if (ret < 0)
413 pr_err("%s: Send SoftPause Param failed ret=%d\n",
414 __func__, ret);
415 ret = q6asm_set_softvolume(compressed_audio.prtd->audio_client,
416 &softvol);
417 if (ret < 0)
418 pr_err("%s: Send SoftVolume Param failed ret=%d\n",
419 __func__, ret);
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530420
421 return 0;
422}
423
Krishnankutty Kolathappilly27d7b302012-02-29 21:04:31 -0800424int compressed_set_volume(unsigned volume)
425{
426 int rc = 0;
427 if (compressed_audio.prtd && compressed_audio.prtd->audio_client) {
428 rc = q6asm_set_volume(compressed_audio.prtd->audio_client,
429 volume);
430 if (rc < 0) {
431 pr_err("%s: Send Volume command failed"
432 " rc=%d\n", __func__, rc);
433 }
434 }
435 compressed_audio.volume = volume;
436 return rc;
437}
438
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530439static int msm_compr_playback_close(struct snd_pcm_substream *substream)
440{
441 struct snd_pcm_runtime *runtime = substream->runtime;
442 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
443 struct compr_audio *compr = runtime->private_data;
444 struct msm_audio *prtd = &compr->prtd;
445 int dir = 0;
446
447 pr_debug("%s\n", __func__);
448
449 dir = IN;
Asish Bhattacharya31bd80d2012-01-04 02:30:13 +0530450 atomic_set(&prtd->pending_buffer, 0);
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530451 q6asm_cmd(prtd->audio_client, CMD_CLOSE);
Krishnankutty Kolathappilly27d7b302012-02-29 21:04:31 -0800452 compressed_audio.prtd = NULL;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530453 q6asm_audio_client_buf_free_contiguous(dir,
454 prtd->audio_client);
455
456 msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->be_id,
457 SNDRV_PCM_STREAM_PLAYBACK);
458 q6asm_audio_client_free(prtd->audio_client);
459 kfree(prtd);
460 return 0;
461}
462
463static int msm_compr_close(struct snd_pcm_substream *substream)
464{
465 int ret = 0;
466
467 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
468 ret = msm_compr_playback_close(substream);
469 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
470 ret = EINVAL;
471 return ret;
472}
473static int msm_compr_prepare(struct snd_pcm_substream *substream)
474{
475 int ret = 0;
476
477 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
478 ret = msm_compr_playback_prepare(substream);
479 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
480 ret = EINVAL;
481 return ret;
482}
483
484static snd_pcm_uframes_t msm_compr_pointer(struct snd_pcm_substream *substream)
485{
486
487 struct snd_pcm_runtime *runtime = substream->runtime;
488 struct compr_audio *compr = runtime->private_data;
489 struct msm_audio *prtd = &compr->prtd;
490
491 if (prtd->pcm_irq_pos >= prtd->pcm_size)
492 prtd->pcm_irq_pos = 0;
493
494 pr_debug("pcm_irq_pos = %d\n", prtd->pcm_irq_pos);
495 return bytes_to_frames(runtime, (prtd->pcm_irq_pos));
496}
497
498static int msm_compr_mmap(struct snd_pcm_substream *substream,
499 struct vm_area_struct *vma)
500{
501 int result = 0;
502 struct snd_pcm_runtime *runtime = substream->runtime;
503 struct compr_audio *compr = runtime->private_data;
504 struct msm_audio *prtd = &compr->prtd;
505
506 pr_debug("%s\n", __func__);
507 prtd->mmap_flag = 1;
508 if (runtime->dma_addr && runtime->dma_bytes) {
509 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
510 result = remap_pfn_range(vma, vma->vm_start,
511 runtime->dma_addr >> PAGE_SHIFT,
512 runtime->dma_bytes,
513 vma->vm_page_prot);
514 } else {
515 pr_err("Physical address or size of buf is NULL");
516 return -EINVAL;
517 }
518 return result;
519}
520
521static int msm_compr_hw_params(struct snd_pcm_substream *substream,
522 struct snd_pcm_hw_params *params)
523{
524 struct snd_pcm_runtime *runtime = substream->runtime;
Santosh Mardie27ed782012-03-29 04:20:38 +0530525 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530526 struct compr_audio *compr = runtime->private_data;
527 struct msm_audio *prtd = &compr->prtd;
528 struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
529 struct audio_buffer *buf;
530 int dir, ret;
531
532 pr_debug("%s\n", __func__);
533 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
534 dir = IN;
535 else
536 return -EINVAL;
537
Santosh Mardie27ed782012-03-29 04:20:38 +0530538 switch (compr->info.codec_param.codec.id) {
539 case SND_AUDIOCODEC_AC3_PASS_THROUGH:
540 ret = q6asm_open_write_compressed(prtd->audio_client,
541 compr->codec);
542 if (ret < 0) {
543 pr_err("%s: compressed Session out open failed\n",
544 __func__);
545 return -ENOMEM;
546 }
547 break;
548 default:
549 ret = q6asm_open_write(prtd->audio_client, compr->codec);
550 if (ret < 0) {
551 pr_err("%s: Session out open failed\n", __func__);
552 return -ENOMEM;
553 }
554 msm_pcm_routing_reg_phy_stream(soc_prtd->dai_link->be_id,
555 prtd->session_id, substream->stream);
556
557 break;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530558 }
559 ret = q6asm_set_io_mode(prtd->audio_client, ASYNC_IO_MODE);
560 if (ret < 0) {
561 pr_err("%s: Set IO mode failed\n", __func__);
562 return -ENOMEM;
563 }
564
565 ret = q6asm_audio_client_buf_alloc_contiguous(dir,
566 prtd->audio_client,
567 runtime->hw.period_bytes_min,
568 runtime->hw.periods_max);
569 if (ret < 0) {
570 pr_err("Audio Start: Buffer Allocation failed "
571 "rc = %d\n", ret);
572 return -ENOMEM;
573 }
574 buf = prtd->audio_client->port[dir].buf;
575
576 pr_debug("%s:buf = %p\n", __func__, buf);
577 dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
578 dma_buf->dev.dev = substream->pcm->card->dev;
579 dma_buf->private_data = NULL;
580 dma_buf->area = buf[0].data;
581 dma_buf->addr = buf[0].phys;
582 dma_buf->bytes = runtime->hw.buffer_bytes_max;
583 if (!dma_buf->area)
584 return -ENOMEM;
585
586 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
587 return 0;
588}
589
590static int msm_compr_ioctl(struct snd_pcm_substream *substream,
591 unsigned int cmd, void *arg)
592{
593 int rc = 0;
594 struct snd_pcm_runtime *runtime = substream->runtime;
595 struct compr_audio *compr = runtime->private_data;
596 struct msm_audio *prtd = &compr->prtd;
Asish Bhattacharya8664b252011-12-30 05:43:23 +0530597 uint64_t timestamp;
598 uint64_t temp;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530599
600 switch (cmd) {
Asish Bhattacharya8664b252011-12-30 05:43:23 +0530601 case SNDRV_COMPRESS_TSTAMP: {
602 struct snd_compr_tstamp tstamp;
603 pr_debug("SNDRV_COMPRESS_TSTAMP\n");
604
605 memset(&tstamp, 0x0, sizeof(struct snd_compr_tstamp));
606 timestamp = q6asm_get_session_time(prtd->audio_client);
607 if (timestamp < 0) {
608 pr_err("%s: Get Session Time return value =%lld\n",
609 __func__, timestamp);
610 return -EAGAIN;
611 }
612 temp = (timestamp * 2 * runtime->channels);
613 temp = temp * (runtime->rate/1000);
614 temp = div_u64(temp, 1000);
615 tstamp.sampling_rate = runtime->rate;
616 tstamp.rendered = (size_t)(temp & 0xFFFFFFFF);
617 tstamp.decoded = (size_t)((temp >> 32) & 0xFFFFFFFF);
618 tstamp.timestamp = timestamp;
619 pr_debug("%s: bytes_consumed:lsb = %d, msb = %d,"
620 "timestamp = %lld,\n",
621 __func__, tstamp.rendered, tstamp.decoded,
622 tstamp.timestamp);
623 if (copy_to_user((void *) arg, &tstamp,
624 sizeof(struct snd_compr_tstamp)))
625 return -EFAULT;
626 return 0;
627 }
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530628 case SNDRV_COMPRESS_GET_CAPS:
629 pr_debug("SNDRV_COMPRESS_GET_CAPS\n");
630 if (copy_to_user((void *) arg, &compr->info.compr_cap,
631 sizeof(struct snd_compr_caps))) {
632 rc = -EFAULT;
633 pr_err("%s: ERROR: copy to user\n", __func__);
634 return rc;
635 }
636 return 0;
637 case SNDRV_COMPRESS_SET_PARAMS:
638 pr_debug("SNDRV_COMPRESS_SET_PARAMS: ");
639 if (copy_from_user(&compr->info.codec_param, (void *) arg,
640 sizeof(struct snd_compr_params))) {
641 rc = -EFAULT;
642 pr_err("%s: ERROR: copy from user\n", __func__);
643 return rc;
644 }
645 switch (compr->info.codec_param.codec.id) {
646 case SND_AUDIOCODEC_MP3:
647 /* For MP3 we dont need any other parameter */
648 pr_debug("SND_AUDIOCODEC_MP3\n");
649 compr->codec = FORMAT_MP3;
650 break;
Krishnankutty Kolathappilly46dded22012-01-12 12:03:34 -0800651 case SND_AUDIOCODEC_AAC:
652 pr_debug("SND_AUDIOCODEC_AAC\n");
653 compr->codec = FORMAT_MPEG4_AAC;
654 break;
Santosh Mardie27ed782012-03-29 04:20:38 +0530655 case SND_AUDIOCODEC_AC3_PASS_THROUGH:
656 pr_debug("SND_AUDIOCODEC_AC3_PASS_THROUGH\n");
657 compr->codec = FORMAT_AC3;
658 break;
Aviral Gupta66698c12012-04-28 22:09:48 +0530659 case SND_AUDIOCODEC_WMA:
660 pr_debug("SND_AUDIOCODEC_WMA\n");
661 compr->codec = FORMAT_WMA_V9;
662 break;
Aviral Guptac4ee3b02012-05-09 10:53:45 +0530663 case SND_AUDIOCODEC_WMA_PRO:
664 pr_debug("SND_AUDIOCODEC_WMA_PRO\n");
665 compr->codec = FORMAT_WMA_V10PRO;
666 break;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530667 default:
668 pr_debug("FORMAT_LINEAR_PCM\n");
669 compr->codec = FORMAT_LINEAR_PCM;
670 break;
671 }
672 return 0;
673 case SNDRV_PCM_IOCTL1_RESET:
674 prtd->cmd_ack = 0;
675 rc = q6asm_cmd(prtd->audio_client, CMD_FLUSH);
676 if (rc < 0)
677 pr_err("%s: flush cmd failed rc=%d\n", __func__, rc);
678 rc = wait_event_timeout(the_locks.eos_wait,
679 prtd->cmd_ack, 5 * HZ);
680 if (rc < 0)
681 pr_err("Flush cmd timeout\n");
682 prtd->pcm_irq_pos = 0;
683 break;
684 default:
685 break;
686 }
687 return snd_pcm_lib_ioctl(substream, cmd, arg);
688}
689
690static struct snd_pcm_ops msm_compr_ops = {
691 .open = msm_compr_open,
692 .hw_params = msm_compr_hw_params,
693 .close = msm_compr_close,
694 .ioctl = msm_compr_ioctl,
695 .prepare = msm_compr_prepare,
696 .trigger = msm_compr_trigger,
697 .pointer = msm_compr_pointer,
698 .mmap = msm_compr_mmap,
699};
700
701static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
702{
703 struct snd_card *card = rtd->card->snd_card;
704 int ret = 0;
705
706 if (!card->dev->coherent_dma_mask)
707 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
708 return ret;
709}
710
711static struct snd_soc_platform_driver msm_soc_platform = {
712 .ops = &msm_compr_ops,
713 .pcm_new = msm_asoc_pcm_new,
714};
715
716static __devinit int msm_compr_probe(struct platform_device *pdev)
717{
718 pr_info("%s: dev name %s\n", __func__, dev_name(&pdev->dev));
719 return snd_soc_register_platform(&pdev->dev,
720 &msm_soc_platform);
721}
722
723static int msm_compr_remove(struct platform_device *pdev)
724{
725 snd_soc_unregister_platform(&pdev->dev);
726 return 0;
727}
728
729static struct platform_driver msm_compr_driver = {
730 .driver = {
731 .name = "msm-compr-dsp",
732 .owner = THIS_MODULE,
733 },
734 .probe = msm_compr_probe,
735 .remove = __devexit_p(msm_compr_remove),
736};
737
738static int __init msm_soc_platform_init(void)
739{
740 init_waitqueue_head(&the_locks.enable_wait);
741 init_waitqueue_head(&the_locks.eos_wait);
742 init_waitqueue_head(&the_locks.write_wait);
743 init_waitqueue_head(&the_locks.read_wait);
744
745 return platform_driver_register(&msm_compr_driver);
746}
747module_init(msm_soc_platform_init);
748
749static void __exit msm_soc_platform_exit(void)
750{
751 platform_driver_unregister(&msm_compr_driver);
752}
753module_exit(msm_soc_platform_exit);
754
755MODULE_DESCRIPTION("PCM module platform driver");
756MODULE_LICENSE("GPL v2");