blob: 776337d07b3f4ac46513623e83eaabc141f5dd1d [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;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530195 int ret;
196
Santosh Mardie27ed782012-03-29 04:20:38 +0530197 pr_debug("compressed stream prepare\n");
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530198 prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
199 prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
200 prtd->pcm_irq_pos = 0;
201 /* rate and channels are sent to audio driver */
202 prtd->samp_rate = runtime->rate;
203 prtd->channel_mode = runtime->channels;
204 prtd->out_head = 0;
Krishnankutty Kolathappilly46dded22012-01-12 12:03:34 -0800205 atomic_set(&prtd->out_count, runtime->periods);
206
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530207 if (prtd->enabled)
208 return 0;
209
Krishnankutty Kolathappilly46dded22012-01-12 12:03:34 -0800210 switch (compr->info.codec_param.codec.id) {
211 case SND_AUDIOCODEC_MP3:
212 ret = q6asm_media_format_block(prtd->audio_client,
213 compr->codec);
214 if (ret < 0)
215 pr_info("%s: CMD Format block failed\n", __func__);
216 break;
217 case SND_AUDIOCODEC_AAC:
218 pr_debug("SND_AUDIOCODEC_AAC\n");
219 memset(&aac_cfg, 0x0, sizeof(struct asm_aac_cfg));
220 aac_cfg.aot = AAC_ENC_MODE_EAAC_P;
221 aac_cfg.format = 0x03;
222 aac_cfg.ch_cfg = runtime->channels;
Aviral Gupta66698c12012-04-28 22:09:48 +0530223 aac_cfg.sample_rate = runtime->rate;
Krishnankutty Kolathappilly46dded22012-01-12 12:03:34 -0800224 ret = q6asm_media_format_block_aac(prtd->audio_client,
225 &aac_cfg);
226 if (ret < 0)
227 pr_err("%s: CMD Format block failed\n", __func__);
228 break;
Santosh Mardie27ed782012-03-29 04:20:38 +0530229 case SND_AUDIOCODEC_AC3_PASS_THROUGH:
230 pr_debug("compressd playback, no need to send"
231 " the decoder params\n");
232 break;
Aviral Gupta66698c12012-04-28 22:09:48 +0530233 case SND_AUDIOCODEC_WMA:
234 pr_debug("SND_AUDIOCODEC_WMA\n");
235 memset(&wma_cfg, 0x0, sizeof(struct asm_wma_cfg));
236 wma_cfg.format_tag = compr->info.codec_param.codec.format;
237 wma_cfg.ch_cfg = runtime->channels;
238 wma_cfg.sample_rate = runtime->rate;
239 wma_cfg.avg_bytes_per_sec =
240 compr->info.codec_param.codec.bit_rate/8;
241 wma_cfg.block_align = compr->info.codec_param.codec.align;
242 wma_cfg.valid_bits_per_sample =
243 compr->info.codec_param.codec.options.wma.bits_per_sample;
244 wma_cfg.ch_mask =
245 compr->info.codec_param.codec.options.wma.channelmask;
246 wma_cfg.encode_opt =
247 compr->info.codec_param.codec.options.wma.encodeopt;
248 ret = q6asm_media_format_block_wma(prtd->audio_client,
249 &wma_cfg);
250 if (ret < 0)
251 pr_err("%s: CMD Format block failed\n", __func__);
252 break;
Krishnankutty Kolathappilly46dded22012-01-12 12:03:34 -0800253 default:
254 return -EINVAL;
255 }
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530256
257 prtd->enabled = 1;
258 prtd->cmd_ack = 0;
259
260 return 0;
261}
262
263static int msm_compr_trigger(struct snd_pcm_substream *substream, int cmd)
264{
265 int ret = 0;
266 struct snd_pcm_runtime *runtime = substream->runtime;
Santosh Mardie27ed782012-03-29 04:20:38 +0530267 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530268 struct compr_audio *compr = runtime->private_data;
269 struct msm_audio *prtd = &compr->prtd;
270
271 pr_debug("%s\n", __func__);
272 switch (cmd) {
273 case SNDRV_PCM_TRIGGER_START:
274 prtd->pcm_irq_pos = 0;
Santosh Mardie27ed782012-03-29 04:20:38 +0530275 if (compr->info.codec_param.codec.id ==
276 SND_AUDIOCODEC_AC3_PASS_THROUGH) {
277 msm_pcm_routing_reg_psthr_stream(
278 soc_prtd->dai_link->be_id,
279 prtd->session_id, substream->stream);
280 }
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530281 case SNDRV_PCM_TRIGGER_RESUME:
282 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
283 pr_debug("%s: Trigger start\n", __func__);
284 q6asm_run_nowait(prtd->audio_client, 0, 0, 0);
285 atomic_set(&prtd->start, 1);
286 break;
287 case SNDRV_PCM_TRIGGER_STOP:
288 pr_debug("SNDRV_PCM_TRIGGER_STOP\n");
289 atomic_set(&prtd->start, 0);
290 break;
291 case SNDRV_PCM_TRIGGER_SUSPEND:
292 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
293 pr_debug("SNDRV_PCM_TRIGGER_PAUSE\n");
294 q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
295 atomic_set(&prtd->start, 0);
296 break;
297 default:
298 ret = -EINVAL;
299 break;
300 }
301
302 return ret;
303}
304
305static void populate_codec_list(struct compr_audio *compr,
306 struct snd_pcm_runtime *runtime)
307{
308 pr_debug("%s\n", __func__);
309 /* MP3 Block */
310 compr->info.compr_cap.num_codecs = 1;
311 compr->info.compr_cap.min_fragment_size = runtime->hw.period_bytes_min;
312 compr->info.compr_cap.max_fragment_size = runtime->hw.period_bytes_max;
313 compr->info.compr_cap.min_fragments = runtime->hw.periods_min;
314 compr->info.compr_cap.max_fragments = runtime->hw.periods_max;
315 compr->info.compr_cap.codecs[0] = SND_AUDIOCODEC_MP3;
Krishnankutty Kolathappilly46dded22012-01-12 12:03:34 -0800316 compr->info.compr_cap.codecs[1] = SND_AUDIOCODEC_AAC;
Santosh Mardie27ed782012-03-29 04:20:38 +0530317 compr->info.compr_cap.codecs[2] = SND_AUDIOCODEC_AC3_PASS_THROUGH;
Aviral Gupta66698c12012-04-28 22:09:48 +0530318 compr->info.compr_cap.codecs[3] = SND_AUDIOCODEC_WMA;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530319 /* Add new codecs here */
320}
321
322static int msm_compr_open(struct snd_pcm_substream *substream)
323{
324 struct snd_pcm_runtime *runtime = substream->runtime;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530325 struct compr_audio *compr;
326 struct msm_audio *prtd;
327 int ret = 0;
Krishnankutty Kolathappilly27d7b302012-02-29 21:04:31 -0800328 struct asm_softpause_params softpause = {
329 .enable = SOFT_PAUSE_ENABLE,
330 .period = SOFT_PAUSE_PERIOD,
331 .step = SOFT_PAUSE_STEP,
332 .rampingcurve = SOFT_PAUSE_CURVE_LINEAR,
333 };
334 struct asm_softvolume_params softvol = {
335 .period = SOFT_VOLUME_PERIOD,
336 .step = SOFT_VOLUME_STEP,
337 .rampingcurve = SOFT_VOLUME_CURVE_LINEAR,
338 };
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530339
340 /* Capture path */
341 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
342 return -EINVAL;
343
344 pr_debug("%s\n", __func__);
345 compr = kzalloc(sizeof(struct compr_audio), GFP_KERNEL);
346 if (compr == NULL) {
347 pr_err("Failed to allocate memory for msm_audio\n");
348 return -ENOMEM;
349 }
350 prtd = &compr->prtd;
351 prtd->substream = substream;
352 prtd->audio_client = q6asm_audio_client_alloc(
353 (app_cb)compr_event_handler, compr);
354 if (!prtd->audio_client) {
355 pr_info("%s: Could not allocate memory\n", __func__);
356 kfree(prtd);
357 return -ENOMEM;
358 }
359 runtime->hw = msm_compr_hardware_playback;
360
361 pr_info("%s: session ID %d\n", __func__, prtd->audio_client->session);
362
363 prtd->session_id = prtd->audio_client->session;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530364
365 prtd->cmd_ack = 1;
366
367 ret = snd_pcm_hw_constraint_list(runtime, 0,
368 SNDRV_PCM_HW_PARAM_RATE,
369 &constraints_sample_rates);
370 if (ret < 0)
371 pr_info("snd_pcm_hw_constraint_list failed\n");
372 /* Ensure that buffer size is a multiple of period size */
373 ret = snd_pcm_hw_constraint_integer(runtime,
374 SNDRV_PCM_HW_PARAM_PERIODS);
375 if (ret < 0)
376 pr_info("snd_pcm_hw_constraint_integer failed\n");
377
378 prtd->dsp_cnt = 0;
Asish Bhattacharya31bd80d2012-01-04 02:30:13 +0530379 atomic_set(&prtd->pending_buffer, 1);
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530380 compr->codec = FORMAT_MP3;
381 populate_codec_list(compr, runtime);
382 runtime->private_data = compr;
Krishnankutty Kolathappilly27d7b302012-02-29 21:04:31 -0800383 compressed_audio.prtd = &compr->prtd;
384 ret = compressed_set_volume(compressed_audio.volume);
385 if (ret < 0)
386 pr_err("%s : Set Volume failed : %d", __func__, ret);
387
388 ret = q6asm_set_softpause(compressed_audio.prtd->audio_client,
389 &softpause);
390 if (ret < 0)
391 pr_err("%s: Send SoftPause Param failed ret=%d\n",
392 __func__, ret);
393 ret = q6asm_set_softvolume(compressed_audio.prtd->audio_client,
394 &softvol);
395 if (ret < 0)
396 pr_err("%s: Send SoftVolume Param failed ret=%d\n",
397 __func__, ret);
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530398
399 return 0;
400}
401
Krishnankutty Kolathappilly27d7b302012-02-29 21:04:31 -0800402int compressed_set_volume(unsigned volume)
403{
404 int rc = 0;
405 if (compressed_audio.prtd && compressed_audio.prtd->audio_client) {
406 rc = q6asm_set_volume(compressed_audio.prtd->audio_client,
407 volume);
408 if (rc < 0) {
409 pr_err("%s: Send Volume command failed"
410 " rc=%d\n", __func__, rc);
411 }
412 }
413 compressed_audio.volume = volume;
414 return rc;
415}
416
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530417static int msm_compr_playback_close(struct snd_pcm_substream *substream)
418{
419 struct snd_pcm_runtime *runtime = substream->runtime;
420 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
421 struct compr_audio *compr = runtime->private_data;
422 struct msm_audio *prtd = &compr->prtd;
423 int dir = 0;
424
425 pr_debug("%s\n", __func__);
426
427 dir = IN;
Asish Bhattacharya31bd80d2012-01-04 02:30:13 +0530428 atomic_set(&prtd->pending_buffer, 0);
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530429 q6asm_cmd(prtd->audio_client, CMD_CLOSE);
Krishnankutty Kolathappilly27d7b302012-02-29 21:04:31 -0800430 compressed_audio.prtd = NULL;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530431 q6asm_audio_client_buf_free_contiguous(dir,
432 prtd->audio_client);
433
434 msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->be_id,
435 SNDRV_PCM_STREAM_PLAYBACK);
436 q6asm_audio_client_free(prtd->audio_client);
437 kfree(prtd);
438 return 0;
439}
440
441static int msm_compr_close(struct snd_pcm_substream *substream)
442{
443 int ret = 0;
444
445 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
446 ret = msm_compr_playback_close(substream);
447 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
448 ret = EINVAL;
449 return ret;
450}
451static int msm_compr_prepare(struct snd_pcm_substream *substream)
452{
453 int ret = 0;
454
455 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
456 ret = msm_compr_playback_prepare(substream);
457 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
458 ret = EINVAL;
459 return ret;
460}
461
462static snd_pcm_uframes_t msm_compr_pointer(struct snd_pcm_substream *substream)
463{
464
465 struct snd_pcm_runtime *runtime = substream->runtime;
466 struct compr_audio *compr = runtime->private_data;
467 struct msm_audio *prtd = &compr->prtd;
468
469 if (prtd->pcm_irq_pos >= prtd->pcm_size)
470 prtd->pcm_irq_pos = 0;
471
472 pr_debug("pcm_irq_pos = %d\n", prtd->pcm_irq_pos);
473 return bytes_to_frames(runtime, (prtd->pcm_irq_pos));
474}
475
476static int msm_compr_mmap(struct snd_pcm_substream *substream,
477 struct vm_area_struct *vma)
478{
479 int result = 0;
480 struct snd_pcm_runtime *runtime = substream->runtime;
481 struct compr_audio *compr = runtime->private_data;
482 struct msm_audio *prtd = &compr->prtd;
483
484 pr_debug("%s\n", __func__);
485 prtd->mmap_flag = 1;
486 if (runtime->dma_addr && runtime->dma_bytes) {
487 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
488 result = remap_pfn_range(vma, vma->vm_start,
489 runtime->dma_addr >> PAGE_SHIFT,
490 runtime->dma_bytes,
491 vma->vm_page_prot);
492 } else {
493 pr_err("Physical address or size of buf is NULL");
494 return -EINVAL;
495 }
496 return result;
497}
498
499static int msm_compr_hw_params(struct snd_pcm_substream *substream,
500 struct snd_pcm_hw_params *params)
501{
502 struct snd_pcm_runtime *runtime = substream->runtime;
Santosh Mardie27ed782012-03-29 04:20:38 +0530503 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530504 struct compr_audio *compr = runtime->private_data;
505 struct msm_audio *prtd = &compr->prtd;
506 struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
507 struct audio_buffer *buf;
508 int dir, ret;
509
510 pr_debug("%s\n", __func__);
511 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
512 dir = IN;
513 else
514 return -EINVAL;
515
Santosh Mardie27ed782012-03-29 04:20:38 +0530516 switch (compr->info.codec_param.codec.id) {
517 case SND_AUDIOCODEC_AC3_PASS_THROUGH:
518 ret = q6asm_open_write_compressed(prtd->audio_client,
519 compr->codec);
520 if (ret < 0) {
521 pr_err("%s: compressed Session out open failed\n",
522 __func__);
523 return -ENOMEM;
524 }
525 break;
526 default:
527 ret = q6asm_open_write(prtd->audio_client, compr->codec);
528 if (ret < 0) {
529 pr_err("%s: Session out open failed\n", __func__);
530 return -ENOMEM;
531 }
532 msm_pcm_routing_reg_phy_stream(soc_prtd->dai_link->be_id,
533 prtd->session_id, substream->stream);
534
535 break;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530536 }
537 ret = q6asm_set_io_mode(prtd->audio_client, ASYNC_IO_MODE);
538 if (ret < 0) {
539 pr_err("%s: Set IO mode failed\n", __func__);
540 return -ENOMEM;
541 }
542
543 ret = q6asm_audio_client_buf_alloc_contiguous(dir,
544 prtd->audio_client,
545 runtime->hw.period_bytes_min,
546 runtime->hw.periods_max);
547 if (ret < 0) {
548 pr_err("Audio Start: Buffer Allocation failed "
549 "rc = %d\n", ret);
550 return -ENOMEM;
551 }
552 buf = prtd->audio_client->port[dir].buf;
553
554 pr_debug("%s:buf = %p\n", __func__, buf);
555 dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
556 dma_buf->dev.dev = substream->pcm->card->dev;
557 dma_buf->private_data = NULL;
558 dma_buf->area = buf[0].data;
559 dma_buf->addr = buf[0].phys;
560 dma_buf->bytes = runtime->hw.buffer_bytes_max;
561 if (!dma_buf->area)
562 return -ENOMEM;
563
564 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
565 return 0;
566}
567
568static int msm_compr_ioctl(struct snd_pcm_substream *substream,
569 unsigned int cmd, void *arg)
570{
571 int rc = 0;
572 struct snd_pcm_runtime *runtime = substream->runtime;
573 struct compr_audio *compr = runtime->private_data;
574 struct msm_audio *prtd = &compr->prtd;
Asish Bhattacharya8664b252011-12-30 05:43:23 +0530575 uint64_t timestamp;
576 uint64_t temp;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530577
578 switch (cmd) {
Asish Bhattacharya8664b252011-12-30 05:43:23 +0530579 case SNDRV_COMPRESS_TSTAMP: {
580 struct snd_compr_tstamp tstamp;
581 pr_debug("SNDRV_COMPRESS_TSTAMP\n");
582
583 memset(&tstamp, 0x0, sizeof(struct snd_compr_tstamp));
584 timestamp = q6asm_get_session_time(prtd->audio_client);
585 if (timestamp < 0) {
586 pr_err("%s: Get Session Time return value =%lld\n",
587 __func__, timestamp);
588 return -EAGAIN;
589 }
590 temp = (timestamp * 2 * runtime->channels);
591 temp = temp * (runtime->rate/1000);
592 temp = div_u64(temp, 1000);
593 tstamp.sampling_rate = runtime->rate;
594 tstamp.rendered = (size_t)(temp & 0xFFFFFFFF);
595 tstamp.decoded = (size_t)((temp >> 32) & 0xFFFFFFFF);
596 tstamp.timestamp = timestamp;
597 pr_debug("%s: bytes_consumed:lsb = %d, msb = %d,"
598 "timestamp = %lld,\n",
599 __func__, tstamp.rendered, tstamp.decoded,
600 tstamp.timestamp);
601 if (copy_to_user((void *) arg, &tstamp,
602 sizeof(struct snd_compr_tstamp)))
603 return -EFAULT;
604 return 0;
605 }
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530606 case SNDRV_COMPRESS_GET_CAPS:
607 pr_debug("SNDRV_COMPRESS_GET_CAPS\n");
608 if (copy_to_user((void *) arg, &compr->info.compr_cap,
609 sizeof(struct snd_compr_caps))) {
610 rc = -EFAULT;
611 pr_err("%s: ERROR: copy to user\n", __func__);
612 return rc;
613 }
614 return 0;
615 case SNDRV_COMPRESS_SET_PARAMS:
616 pr_debug("SNDRV_COMPRESS_SET_PARAMS: ");
617 if (copy_from_user(&compr->info.codec_param, (void *) arg,
618 sizeof(struct snd_compr_params))) {
619 rc = -EFAULT;
620 pr_err("%s: ERROR: copy from user\n", __func__);
621 return rc;
622 }
623 switch (compr->info.codec_param.codec.id) {
624 case SND_AUDIOCODEC_MP3:
625 /* For MP3 we dont need any other parameter */
626 pr_debug("SND_AUDIOCODEC_MP3\n");
627 compr->codec = FORMAT_MP3;
628 break;
Krishnankutty Kolathappilly46dded22012-01-12 12:03:34 -0800629 case SND_AUDIOCODEC_AAC:
630 pr_debug("SND_AUDIOCODEC_AAC\n");
631 compr->codec = FORMAT_MPEG4_AAC;
632 break;
Santosh Mardie27ed782012-03-29 04:20:38 +0530633 case SND_AUDIOCODEC_AC3_PASS_THROUGH:
634 pr_debug("SND_AUDIOCODEC_AC3_PASS_THROUGH\n");
635 compr->codec = FORMAT_AC3;
636 break;
Aviral Gupta66698c12012-04-28 22:09:48 +0530637 case SND_AUDIOCODEC_WMA:
638 pr_debug("SND_AUDIOCODEC_WMA\n");
639 compr->codec = FORMAT_WMA_V9;
640 break;
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530641 default:
642 pr_debug("FORMAT_LINEAR_PCM\n");
643 compr->codec = FORMAT_LINEAR_PCM;
644 break;
645 }
646 return 0;
647 case SNDRV_PCM_IOCTL1_RESET:
648 prtd->cmd_ack = 0;
649 rc = q6asm_cmd(prtd->audio_client, CMD_FLUSH);
650 if (rc < 0)
651 pr_err("%s: flush cmd failed rc=%d\n", __func__, rc);
652 rc = wait_event_timeout(the_locks.eos_wait,
653 prtd->cmd_ack, 5 * HZ);
654 if (rc < 0)
655 pr_err("Flush cmd timeout\n");
656 prtd->pcm_irq_pos = 0;
657 break;
658 default:
659 break;
660 }
661 return snd_pcm_lib_ioctl(substream, cmd, arg);
662}
663
664static struct snd_pcm_ops msm_compr_ops = {
665 .open = msm_compr_open,
666 .hw_params = msm_compr_hw_params,
667 .close = msm_compr_close,
668 .ioctl = msm_compr_ioctl,
669 .prepare = msm_compr_prepare,
670 .trigger = msm_compr_trigger,
671 .pointer = msm_compr_pointer,
672 .mmap = msm_compr_mmap,
673};
674
675static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
676{
677 struct snd_card *card = rtd->card->snd_card;
678 int ret = 0;
679
680 if (!card->dev->coherent_dma_mask)
681 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
682 return ret;
683}
684
685static struct snd_soc_platform_driver msm_soc_platform = {
686 .ops = &msm_compr_ops,
687 .pcm_new = msm_asoc_pcm_new,
688};
689
690static __devinit int msm_compr_probe(struct platform_device *pdev)
691{
692 pr_info("%s: dev name %s\n", __func__, dev_name(&pdev->dev));
693 return snd_soc_register_platform(&pdev->dev,
694 &msm_soc_platform);
695}
696
697static int msm_compr_remove(struct platform_device *pdev)
698{
699 snd_soc_unregister_platform(&pdev->dev);
700 return 0;
701}
702
703static struct platform_driver msm_compr_driver = {
704 .driver = {
705 .name = "msm-compr-dsp",
706 .owner = THIS_MODULE,
707 },
708 .probe = msm_compr_probe,
709 .remove = __devexit_p(msm_compr_remove),
710};
711
712static int __init msm_soc_platform_init(void)
713{
714 init_waitqueue_head(&the_locks.enable_wait);
715 init_waitqueue_head(&the_locks.eos_wait);
716 init_waitqueue_head(&the_locks.write_wait);
717 init_waitqueue_head(&the_locks.read_wait);
718
719 return platform_driver_register(&msm_compr_driver);
720}
721module_init(msm_soc_platform_init);
722
723static void __exit msm_soc_platform_exit(void)
724{
725 platform_driver_unregister(&msm_compr_driver);
726}
727module_exit(msm_soc_platform_exit);
728
729MODULE_DESCRIPTION("PCM module platform driver");
730MODULE_LICENSE("GPL v2");