blob: 8e4e2055818cd6ab3875e97e87323a4ae3127920 [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;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530194 int ret;
195
Santosh Mardie27ed782012-03-29 04:20:38 +0530196 pr_debug("compressed stream prepare\n");
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530197 prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
198 prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
199 prtd->pcm_irq_pos = 0;
200 /* rate and channels are sent to audio driver */
201 prtd->samp_rate = runtime->rate;
202 prtd->channel_mode = runtime->channels;
203 prtd->out_head = 0;
Krishnankutty Kolathappilly46dded22012-01-12 12:03:34 -0800204 atomic_set(&prtd->out_count, runtime->periods);
205
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530206 if (prtd->enabled)
207 return 0;
208
Krishnankutty Kolathappilly46dded22012-01-12 12:03:34 -0800209 switch (compr->info.codec_param.codec.id) {
210 case SND_AUDIOCODEC_MP3:
211 ret = q6asm_media_format_block(prtd->audio_client,
212 compr->codec);
213 if (ret < 0)
214 pr_info("%s: CMD Format block failed\n", __func__);
215 break;
216 case SND_AUDIOCODEC_AAC:
217 pr_debug("SND_AUDIOCODEC_AAC\n");
218 memset(&aac_cfg, 0x0, sizeof(struct asm_aac_cfg));
219 aac_cfg.aot = AAC_ENC_MODE_EAAC_P;
220 aac_cfg.format = 0x03;
221 aac_cfg.ch_cfg = runtime->channels;
222 aac_cfg.sample_rate = runtime->rate;
223 ret = q6asm_media_format_block_aac(prtd->audio_client,
224 &aac_cfg);
225 if (ret < 0)
226 pr_err("%s: CMD Format block failed\n", __func__);
227 break;
Santosh Mardie27ed782012-03-29 04:20:38 +0530228 case SND_AUDIOCODEC_AC3_PASS_THROUGH:
229 pr_debug("compressd playback, no need to send"
230 " the decoder params\n");
231 break;
Krishnankutty Kolathappilly46dded22012-01-12 12:03:34 -0800232 default:
233 return -EINVAL;
234 }
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530235
236 prtd->enabled = 1;
237 prtd->cmd_ack = 0;
238
239 return 0;
240}
241
242static int msm_compr_trigger(struct snd_pcm_substream *substream, int cmd)
243{
244 int ret = 0;
245 struct snd_pcm_runtime *runtime = substream->runtime;
Santosh Mardie27ed782012-03-29 04:20:38 +0530246 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530247 struct compr_audio *compr = runtime->private_data;
248 struct msm_audio *prtd = &compr->prtd;
249
250 pr_debug("%s\n", __func__);
251 switch (cmd) {
252 case SNDRV_PCM_TRIGGER_START:
253 prtd->pcm_irq_pos = 0;
Santosh Mardie27ed782012-03-29 04:20:38 +0530254 if (compr->info.codec_param.codec.id ==
255 SND_AUDIOCODEC_AC3_PASS_THROUGH) {
256 msm_pcm_routing_reg_psthr_stream(
257 soc_prtd->dai_link->be_id,
258 prtd->session_id, substream->stream);
259 }
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530260 case SNDRV_PCM_TRIGGER_RESUME:
261 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
262 pr_debug("%s: Trigger start\n", __func__);
263 q6asm_run_nowait(prtd->audio_client, 0, 0, 0);
264 atomic_set(&prtd->start, 1);
265 break;
266 case SNDRV_PCM_TRIGGER_STOP:
267 pr_debug("SNDRV_PCM_TRIGGER_STOP\n");
268 atomic_set(&prtd->start, 0);
269 break;
270 case SNDRV_PCM_TRIGGER_SUSPEND:
271 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
272 pr_debug("SNDRV_PCM_TRIGGER_PAUSE\n");
273 q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
274 atomic_set(&prtd->start, 0);
275 break;
276 default:
277 ret = -EINVAL;
278 break;
279 }
280
281 return ret;
282}
283
284static void populate_codec_list(struct compr_audio *compr,
285 struct snd_pcm_runtime *runtime)
286{
287 pr_debug("%s\n", __func__);
288 /* MP3 Block */
289 compr->info.compr_cap.num_codecs = 1;
290 compr->info.compr_cap.min_fragment_size = runtime->hw.period_bytes_min;
291 compr->info.compr_cap.max_fragment_size = runtime->hw.period_bytes_max;
292 compr->info.compr_cap.min_fragments = runtime->hw.periods_min;
293 compr->info.compr_cap.max_fragments = runtime->hw.periods_max;
294 compr->info.compr_cap.codecs[0] = SND_AUDIOCODEC_MP3;
Krishnankutty Kolathappilly46dded22012-01-12 12:03:34 -0800295 compr->info.compr_cap.codecs[1] = SND_AUDIOCODEC_AAC;
Santosh Mardie27ed782012-03-29 04:20:38 +0530296 compr->info.compr_cap.codecs[2] = SND_AUDIOCODEC_AC3_PASS_THROUGH;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530297 /* Add new codecs here */
298}
299
300static int msm_compr_open(struct snd_pcm_substream *substream)
301{
302 struct snd_pcm_runtime *runtime = substream->runtime;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530303 struct compr_audio *compr;
304 struct msm_audio *prtd;
305 int ret = 0;
Krishnankutty Kolathappilly27d7b302012-02-29 21:04:31 -0800306 struct asm_softpause_params softpause = {
307 .enable = SOFT_PAUSE_ENABLE,
308 .period = SOFT_PAUSE_PERIOD,
309 .step = SOFT_PAUSE_STEP,
310 .rampingcurve = SOFT_PAUSE_CURVE_LINEAR,
311 };
312 struct asm_softvolume_params softvol = {
313 .period = SOFT_VOLUME_PERIOD,
314 .step = SOFT_VOLUME_STEP,
315 .rampingcurve = SOFT_VOLUME_CURVE_LINEAR,
316 };
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530317
318 /* Capture path */
319 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
320 return -EINVAL;
321
322 pr_debug("%s\n", __func__);
323 compr = kzalloc(sizeof(struct compr_audio), GFP_KERNEL);
324 if (compr == NULL) {
325 pr_err("Failed to allocate memory for msm_audio\n");
326 return -ENOMEM;
327 }
328 prtd = &compr->prtd;
329 prtd->substream = substream;
330 prtd->audio_client = q6asm_audio_client_alloc(
331 (app_cb)compr_event_handler, compr);
332 if (!prtd->audio_client) {
333 pr_info("%s: Could not allocate memory\n", __func__);
334 kfree(prtd);
335 return -ENOMEM;
336 }
337 runtime->hw = msm_compr_hardware_playback;
338
339 pr_info("%s: session ID %d\n", __func__, prtd->audio_client->session);
340
341 prtd->session_id = prtd->audio_client->session;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530342
343 prtd->cmd_ack = 1;
344
345 ret = snd_pcm_hw_constraint_list(runtime, 0,
346 SNDRV_PCM_HW_PARAM_RATE,
347 &constraints_sample_rates);
348 if (ret < 0)
349 pr_info("snd_pcm_hw_constraint_list failed\n");
350 /* Ensure that buffer size is a multiple of period size */
351 ret = snd_pcm_hw_constraint_integer(runtime,
352 SNDRV_PCM_HW_PARAM_PERIODS);
353 if (ret < 0)
354 pr_info("snd_pcm_hw_constraint_integer failed\n");
355
356 prtd->dsp_cnt = 0;
Asish Bhattacharya31bd80d2012-01-04 02:30:13 +0530357 atomic_set(&prtd->pending_buffer, 1);
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530358 compr->codec = FORMAT_MP3;
359 populate_codec_list(compr, runtime);
360 runtime->private_data = compr;
Krishnankutty Kolathappilly27d7b302012-02-29 21:04:31 -0800361 compressed_audio.prtd = &compr->prtd;
362 ret = compressed_set_volume(compressed_audio.volume);
363 if (ret < 0)
364 pr_err("%s : Set Volume failed : %d", __func__, ret);
365
366 ret = q6asm_set_softpause(compressed_audio.prtd->audio_client,
367 &softpause);
368 if (ret < 0)
369 pr_err("%s: Send SoftPause Param failed ret=%d\n",
370 __func__, ret);
371 ret = q6asm_set_softvolume(compressed_audio.prtd->audio_client,
372 &softvol);
373 if (ret < 0)
374 pr_err("%s: Send SoftVolume Param failed ret=%d\n",
375 __func__, ret);
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530376
377 return 0;
378}
379
Krishnankutty Kolathappilly27d7b302012-02-29 21:04:31 -0800380int compressed_set_volume(unsigned volume)
381{
382 int rc = 0;
383 if (compressed_audio.prtd && compressed_audio.prtd->audio_client) {
384 rc = q6asm_set_volume(compressed_audio.prtd->audio_client,
385 volume);
386 if (rc < 0) {
387 pr_err("%s: Send Volume command failed"
388 " rc=%d\n", __func__, rc);
389 }
390 }
391 compressed_audio.volume = volume;
392 return rc;
393}
394
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530395static int msm_compr_playback_close(struct snd_pcm_substream *substream)
396{
397 struct snd_pcm_runtime *runtime = substream->runtime;
398 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
399 struct compr_audio *compr = runtime->private_data;
400 struct msm_audio *prtd = &compr->prtd;
401 int dir = 0;
402
403 pr_debug("%s\n", __func__);
404
405 dir = IN;
Asish Bhattacharya31bd80d2012-01-04 02:30:13 +0530406 atomic_set(&prtd->pending_buffer, 0);
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530407 q6asm_cmd(prtd->audio_client, CMD_CLOSE);
Krishnankutty Kolathappilly27d7b302012-02-29 21:04:31 -0800408 compressed_audio.prtd = NULL;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530409 q6asm_audio_client_buf_free_contiguous(dir,
410 prtd->audio_client);
411
412 msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->be_id,
413 SNDRV_PCM_STREAM_PLAYBACK);
414 q6asm_audio_client_free(prtd->audio_client);
415 kfree(prtd);
416 return 0;
417}
418
419static int msm_compr_close(struct snd_pcm_substream *substream)
420{
421 int ret = 0;
422
423 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
424 ret = msm_compr_playback_close(substream);
425 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
426 ret = EINVAL;
427 return ret;
428}
429static int msm_compr_prepare(struct snd_pcm_substream *substream)
430{
431 int ret = 0;
432
433 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
434 ret = msm_compr_playback_prepare(substream);
435 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
436 ret = EINVAL;
437 return ret;
438}
439
440static snd_pcm_uframes_t msm_compr_pointer(struct snd_pcm_substream *substream)
441{
442
443 struct snd_pcm_runtime *runtime = substream->runtime;
444 struct compr_audio *compr = runtime->private_data;
445 struct msm_audio *prtd = &compr->prtd;
446
447 if (prtd->pcm_irq_pos >= prtd->pcm_size)
448 prtd->pcm_irq_pos = 0;
449
450 pr_debug("pcm_irq_pos = %d\n", prtd->pcm_irq_pos);
451 return bytes_to_frames(runtime, (prtd->pcm_irq_pos));
452}
453
454static int msm_compr_mmap(struct snd_pcm_substream *substream,
455 struct vm_area_struct *vma)
456{
457 int result = 0;
458 struct snd_pcm_runtime *runtime = substream->runtime;
459 struct compr_audio *compr = runtime->private_data;
460 struct msm_audio *prtd = &compr->prtd;
461
462 pr_debug("%s\n", __func__);
463 prtd->mmap_flag = 1;
464 if (runtime->dma_addr && runtime->dma_bytes) {
465 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
466 result = remap_pfn_range(vma, vma->vm_start,
467 runtime->dma_addr >> PAGE_SHIFT,
468 runtime->dma_bytes,
469 vma->vm_page_prot);
470 } else {
471 pr_err("Physical address or size of buf is NULL");
472 return -EINVAL;
473 }
474 return result;
475}
476
477static int msm_compr_hw_params(struct snd_pcm_substream *substream,
478 struct snd_pcm_hw_params *params)
479{
480 struct snd_pcm_runtime *runtime = substream->runtime;
Santosh Mardie27ed782012-03-29 04:20:38 +0530481 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530482 struct compr_audio *compr = runtime->private_data;
483 struct msm_audio *prtd = &compr->prtd;
484 struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
485 struct audio_buffer *buf;
486 int dir, ret;
487
488 pr_debug("%s\n", __func__);
489 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
490 dir = IN;
491 else
492 return -EINVAL;
493
Santosh Mardie27ed782012-03-29 04:20:38 +0530494 switch (compr->info.codec_param.codec.id) {
495 case SND_AUDIOCODEC_AC3_PASS_THROUGH:
496 ret = q6asm_open_write_compressed(prtd->audio_client,
497 compr->codec);
498 if (ret < 0) {
499 pr_err("%s: compressed Session out open failed\n",
500 __func__);
501 return -ENOMEM;
502 }
503 break;
504 default:
505 ret = q6asm_open_write(prtd->audio_client, compr->codec);
506 if (ret < 0) {
507 pr_err("%s: Session out open failed\n", __func__);
508 return -ENOMEM;
509 }
510 msm_pcm_routing_reg_phy_stream(soc_prtd->dai_link->be_id,
511 prtd->session_id, substream->stream);
512
513 break;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530514 }
515 ret = q6asm_set_io_mode(prtd->audio_client, ASYNC_IO_MODE);
516 if (ret < 0) {
517 pr_err("%s: Set IO mode failed\n", __func__);
518 return -ENOMEM;
519 }
520
521 ret = q6asm_audio_client_buf_alloc_contiguous(dir,
522 prtd->audio_client,
523 runtime->hw.period_bytes_min,
524 runtime->hw.periods_max);
525 if (ret < 0) {
526 pr_err("Audio Start: Buffer Allocation failed "
527 "rc = %d\n", ret);
528 return -ENOMEM;
529 }
530 buf = prtd->audio_client->port[dir].buf;
531
532 pr_debug("%s:buf = %p\n", __func__, buf);
533 dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
534 dma_buf->dev.dev = substream->pcm->card->dev;
535 dma_buf->private_data = NULL;
536 dma_buf->area = buf[0].data;
537 dma_buf->addr = buf[0].phys;
538 dma_buf->bytes = runtime->hw.buffer_bytes_max;
539 if (!dma_buf->area)
540 return -ENOMEM;
541
542 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
543 return 0;
544}
545
546static int msm_compr_ioctl(struct snd_pcm_substream *substream,
547 unsigned int cmd, void *arg)
548{
549 int rc = 0;
550 struct snd_pcm_runtime *runtime = substream->runtime;
551 struct compr_audio *compr = runtime->private_data;
552 struct msm_audio *prtd = &compr->prtd;
Asish Bhattacharya8664b252011-12-30 05:43:23 +0530553 uint64_t timestamp;
554 uint64_t temp;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530555
556 switch (cmd) {
Asish Bhattacharya8664b252011-12-30 05:43:23 +0530557 case SNDRV_COMPRESS_TSTAMP: {
558 struct snd_compr_tstamp tstamp;
559 pr_debug("SNDRV_COMPRESS_TSTAMP\n");
560
561 memset(&tstamp, 0x0, sizeof(struct snd_compr_tstamp));
562 timestamp = q6asm_get_session_time(prtd->audio_client);
563 if (timestamp < 0) {
564 pr_err("%s: Get Session Time return value =%lld\n",
565 __func__, timestamp);
566 return -EAGAIN;
567 }
568 temp = (timestamp * 2 * runtime->channels);
569 temp = temp * (runtime->rate/1000);
570 temp = div_u64(temp, 1000);
571 tstamp.sampling_rate = runtime->rate;
572 tstamp.rendered = (size_t)(temp & 0xFFFFFFFF);
573 tstamp.decoded = (size_t)((temp >> 32) & 0xFFFFFFFF);
574 tstamp.timestamp = timestamp;
575 pr_debug("%s: bytes_consumed:lsb = %d, msb = %d,"
576 "timestamp = %lld,\n",
577 __func__, tstamp.rendered, tstamp.decoded,
578 tstamp.timestamp);
579 if (copy_to_user((void *) arg, &tstamp,
580 sizeof(struct snd_compr_tstamp)))
581 return -EFAULT;
582 return 0;
583 }
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530584 case SNDRV_COMPRESS_GET_CAPS:
585 pr_debug("SNDRV_COMPRESS_GET_CAPS\n");
586 if (copy_to_user((void *) arg, &compr->info.compr_cap,
587 sizeof(struct snd_compr_caps))) {
588 rc = -EFAULT;
589 pr_err("%s: ERROR: copy to user\n", __func__);
590 return rc;
591 }
592 return 0;
593 case SNDRV_COMPRESS_SET_PARAMS:
594 pr_debug("SNDRV_COMPRESS_SET_PARAMS: ");
595 if (copy_from_user(&compr->info.codec_param, (void *) arg,
596 sizeof(struct snd_compr_params))) {
597 rc = -EFAULT;
598 pr_err("%s: ERROR: copy from user\n", __func__);
599 return rc;
600 }
601 switch (compr->info.codec_param.codec.id) {
602 case SND_AUDIOCODEC_MP3:
603 /* For MP3 we dont need any other parameter */
604 pr_debug("SND_AUDIOCODEC_MP3\n");
605 compr->codec = FORMAT_MP3;
606 break;
Krishnankutty Kolathappilly46dded22012-01-12 12:03:34 -0800607 case SND_AUDIOCODEC_AAC:
608 pr_debug("SND_AUDIOCODEC_AAC\n");
609 compr->codec = FORMAT_MPEG4_AAC;
610 break;
Santosh Mardie27ed782012-03-29 04:20:38 +0530611 case SND_AUDIOCODEC_AC3_PASS_THROUGH:
612 pr_debug("SND_AUDIOCODEC_AC3_PASS_THROUGH\n");
613 compr->codec = FORMAT_AC3;
614 break;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530615 default:
616 pr_debug("FORMAT_LINEAR_PCM\n");
617 compr->codec = FORMAT_LINEAR_PCM;
618 break;
619 }
620 return 0;
621 case SNDRV_PCM_IOCTL1_RESET:
622 prtd->cmd_ack = 0;
623 rc = q6asm_cmd(prtd->audio_client, CMD_FLUSH);
624 if (rc < 0)
625 pr_err("%s: flush cmd failed rc=%d\n", __func__, rc);
626 rc = wait_event_timeout(the_locks.eos_wait,
627 prtd->cmd_ack, 5 * HZ);
628 if (rc < 0)
629 pr_err("Flush cmd timeout\n");
630 prtd->pcm_irq_pos = 0;
631 break;
632 default:
633 break;
634 }
635 return snd_pcm_lib_ioctl(substream, cmd, arg);
636}
637
638static struct snd_pcm_ops msm_compr_ops = {
639 .open = msm_compr_open,
640 .hw_params = msm_compr_hw_params,
641 .close = msm_compr_close,
642 .ioctl = msm_compr_ioctl,
643 .prepare = msm_compr_prepare,
644 .trigger = msm_compr_trigger,
645 .pointer = msm_compr_pointer,
646 .mmap = msm_compr_mmap,
647};
648
649static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
650{
651 struct snd_card *card = rtd->card->snd_card;
652 int ret = 0;
653
654 if (!card->dev->coherent_dma_mask)
655 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
656 return ret;
657}
658
659static struct snd_soc_platform_driver msm_soc_platform = {
660 .ops = &msm_compr_ops,
661 .pcm_new = msm_asoc_pcm_new,
662};
663
664static __devinit int msm_compr_probe(struct platform_device *pdev)
665{
666 pr_info("%s: dev name %s\n", __func__, dev_name(&pdev->dev));
667 return snd_soc_register_platform(&pdev->dev,
668 &msm_soc_platform);
669}
670
671static int msm_compr_remove(struct platform_device *pdev)
672{
673 snd_soc_unregister_platform(&pdev->dev);
674 return 0;
675}
676
677static struct platform_driver msm_compr_driver = {
678 .driver = {
679 .name = "msm-compr-dsp",
680 .owner = THIS_MODULE,
681 },
682 .probe = msm_compr_probe,
683 .remove = __devexit_p(msm_compr_remove),
684};
685
686static int __init msm_soc_platform_init(void)
687{
688 init_waitqueue_head(&the_locks.enable_wait);
689 init_waitqueue_head(&the_locks.eos_wait);
690 init_waitqueue_head(&the_locks.write_wait);
691 init_waitqueue_head(&the_locks.read_wait);
692
693 return platform_driver_register(&msm_compr_driver);
694}
695module_init(msm_soc_platform_init);
696
697static void __exit msm_soc_platform_exit(void)
698{
699 platform_driver_unregister(&msm_compr_driver);
700}
701module_exit(msm_soc_platform_exit);
702
703MODULE_DESCRIPTION("PCM module platform driver");
704MODULE_LICENSE("GPL v2");