blob: d0497b15007a95577935630f395a1da551f6b67f [file] [log] [blame]
Asish Bhattacharya305d1752011-11-01 20:38:26 +05301/* Copyright (c) 2011, Code Aurora Forum. All rights reserved.
2 *
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>
28#include <asm/dma.h>
29#include <linux/dma-mapping.h>
30#include <linux/android_pmem.h>
31
32#include "msm-compr-q6.h"
33#include "msm-pcm-routing.h"
34
35static struct audio_locks the_locks;
36
37static struct snd_pcm_hardware msm_compr_hardware_playback = {
38 .info = (SNDRV_PCM_INFO_MMAP |
39 SNDRV_PCM_INFO_BLOCK_TRANSFER |
40 SNDRV_PCM_INFO_MMAP_VALID |
41 SNDRV_PCM_INFO_INTERLEAVED |
42 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
43 .formats = SNDRV_PCM_FMTBIT_S16_LE,
44 .rates = SNDRV_PCM_RATE_8000_48000,
45 .rate_min = 8000,
46 .rate_max = 48000,
47 .channels_min = 1,
48 .channels_max = 2,
49 .buffer_bytes_max = 1200 * 1024 * 2,
Krishnankutty Kolathappillya966f452012-01-12 12:28:36 -080050 .period_bytes_min = 4800,
Asish Bhattacharya305d1752011-11-01 20:38:26 +053051 .period_bytes_max = 1200 * 1024,
52 .periods_min = 2,
Krishnankutty Kolathappillya966f452012-01-12 12:28:36 -080053 .periods_max = 512,
Asish Bhattacharya305d1752011-11-01 20:38:26 +053054 .fifo_size = 0,
55};
56
57/* Conventional and unconventional sample rate supported */
58static unsigned int supported_sample_rates[] = {
59 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000
60};
61
62static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
63 .count = ARRAY_SIZE(supported_sample_rates),
64 .list = supported_sample_rates,
65 .mask = 0,
66};
67
68static void compr_event_handler(uint32_t opcode,
69 uint32_t token, uint32_t *payload, void *priv)
70{
71 struct compr_audio *compr = priv;
72 struct msm_audio *prtd = &compr->prtd;
73 struct snd_pcm_substream *substream = prtd->substream;
74 struct snd_pcm_runtime *runtime = substream->runtime;
75 struct audio_aio_write_param param;
76 struct audio_buffer *buf = NULL;
77 int i = 0;
78
79 pr_debug("%s opcode =%08x\n", __func__, opcode);
80 switch (opcode) {
81 case ASM_DATA_EVENT_WRITE_DONE: {
82 uint32_t *ptrmem = (uint32_t *)&param;
83 pr_debug("ASM_DATA_EVENT_WRITE_DONE\n");
84 pr_debug("Buffer Consumed = 0x%08x\n", *ptrmem);
85 prtd->pcm_irq_pos += prtd->pcm_count;
86 if (atomic_read(&prtd->start))
87 snd_pcm_period_elapsed(substream);
88 atomic_inc(&prtd->out_count);
89 wake_up(&the_locks.write_wait);
90 if (!atomic_read(&prtd->start)) {
Asish Bhattacharya31bd80d2012-01-04 02:30:13 +053091 atomic_set(&prtd->pending_buffer, 1);
Asish Bhattacharya305d1752011-11-01 20:38:26 +053092 break;
93 } else
Asish Bhattacharya31bd80d2012-01-04 02:30:13 +053094 atomic_set(&prtd->pending_buffer, 0);
Asish Bhattacharya305d1752011-11-01 20:38:26 +053095
96 if (runtime->status->hw_ptr >= runtime->control->appl_ptr)
97 break;
98 buf = prtd->audio_client->port[IN].buf;
99 pr_debug("%s:writing %d bytes of buffer[%d] to dsp 2\n",
100 __func__, prtd->pcm_count, prtd->out_head);
101 pr_debug("%s:writing buffer[%d] from 0x%08x\n",
102 __func__, prtd->out_head,
103 ((unsigned int)buf[0].phys
104 + (prtd->out_head * prtd->pcm_count)));
105
106 param.paddr = (unsigned long)buf[0].phys
107 + (prtd->out_head * prtd->pcm_count);
108 param.len = prtd->pcm_count;
109 param.msw_ts = 0;
110 param.lsw_ts = 0;
111 param.flags = NO_TIMESTAMP;
112 param.uid = (unsigned long)buf[0].phys
113 + (prtd->out_head * prtd->pcm_count);
114 for (i = 0; i < sizeof(struct audio_aio_write_param)/4;
115 i++, ++ptrmem)
116 pr_debug("cmd[%d]=0x%08x\n", i, *ptrmem);
117 if (q6asm_async_write(prtd->audio_client,
118 &param) < 0)
119 pr_err("%s:q6asm_async_write failed\n",
120 __func__);
121 else
122 prtd->out_head =
123 (prtd->out_head + 1) & (runtime->periods - 1);
124 break;
125 }
126 case ASM_DATA_CMDRSP_EOS:
127 pr_debug("ASM_DATA_CMDRSP_EOS\n");
128 prtd->cmd_ack = 1;
129 wake_up(&the_locks.eos_wait);
130 break;
131 case APR_BASIC_RSP_RESULT: {
132 switch (payload[0]) {
133 case ASM_SESSION_CMD_RUN: {
Asish Bhattacharya31bd80d2012-01-04 02:30:13 +0530134 if (!atomic_read(&prtd->pending_buffer))
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530135 break;
136 pr_debug("%s:writing %d bytes"
137 " of buffer[%d] to dsp\n",
138 __func__, prtd->pcm_count, prtd->out_head);
139 buf = prtd->audio_client->port[IN].buf;
140 pr_debug("%s:writing buffer[%d] from 0x%08x\n",
141 __func__, prtd->out_head,
142 ((unsigned int)buf[0].phys
143 + (prtd->out_head * prtd->pcm_count)));
144 param.paddr = (unsigned long)buf[prtd->out_head].phys;
145 param.len = prtd->pcm_count;
146 param.msw_ts = 0;
147 param.lsw_ts = 0;
148 param.flags = NO_TIMESTAMP;
149 param.uid = (unsigned long)buf[prtd->out_head].phys;
150 if (q6asm_async_write(prtd->audio_client,
151 &param) < 0)
152 pr_err("%s:q6asm_async_write failed\n",
153 __func__);
154 else
155 prtd->out_head =
156 (prtd->out_head + 1)
157 & (runtime->periods - 1);
Asish Bhattacharya31bd80d2012-01-04 02:30:13 +0530158 atomic_set(&prtd->pending_buffer, 0);
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530159 }
160 break;
161 case ASM_STREAM_CMD_FLUSH:
162 pr_debug("ASM_STREAM_CMD_FLUSH\n");
163 prtd->cmd_ack = 1;
164 wake_up(&the_locks.eos_wait);
165 break;
166 default:
167 break;
168 }
169 break;
170 }
171 default:
172 pr_debug("Not Supported Event opcode[0x%x]\n", opcode);
173 break;
174 }
175}
176
177static int msm_compr_playback_prepare(struct snd_pcm_substream *substream)
178{
179 struct snd_pcm_runtime *runtime = substream->runtime;
180 struct compr_audio *compr = runtime->private_data;
181 struct msm_audio *prtd = &compr->prtd;
182 int ret;
183
184 pr_debug("%s\n", __func__);
185 prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
186 prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
187 prtd->pcm_irq_pos = 0;
188 /* rate and channels are sent to audio driver */
189 prtd->samp_rate = runtime->rate;
190 prtd->channel_mode = runtime->channels;
191 prtd->out_head = 0;
192 if (prtd->enabled)
193 return 0;
194
195 ret = q6asm_media_format_block(prtd->audio_client, compr->codec);
196 if (ret < 0)
197 pr_info("%s: CMD Format block failed\n", __func__);
198
199 atomic_set(&prtd->out_count, runtime->periods);
200
201 prtd->enabled = 1;
202 prtd->cmd_ack = 0;
203
204 return 0;
205}
206
207static int msm_compr_trigger(struct snd_pcm_substream *substream, int cmd)
208{
209 int ret = 0;
210 struct snd_pcm_runtime *runtime = substream->runtime;
211 struct compr_audio *compr = runtime->private_data;
212 struct msm_audio *prtd = &compr->prtd;
213
214 pr_debug("%s\n", __func__);
215 switch (cmd) {
216 case SNDRV_PCM_TRIGGER_START:
217 prtd->pcm_irq_pos = 0;
218 case SNDRV_PCM_TRIGGER_RESUME:
219 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
220 pr_debug("%s: Trigger start\n", __func__);
221 q6asm_run_nowait(prtd->audio_client, 0, 0, 0);
222 atomic_set(&prtd->start, 1);
223 break;
224 case SNDRV_PCM_TRIGGER_STOP:
225 pr_debug("SNDRV_PCM_TRIGGER_STOP\n");
226 atomic_set(&prtd->start, 0);
227 break;
228 case SNDRV_PCM_TRIGGER_SUSPEND:
229 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
230 pr_debug("SNDRV_PCM_TRIGGER_PAUSE\n");
231 q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
232 atomic_set(&prtd->start, 0);
233 break;
234 default:
235 ret = -EINVAL;
236 break;
237 }
238
239 return ret;
240}
241
242static void populate_codec_list(struct compr_audio *compr,
243 struct snd_pcm_runtime *runtime)
244{
245 pr_debug("%s\n", __func__);
246 /* MP3 Block */
247 compr->info.compr_cap.num_codecs = 1;
248 compr->info.compr_cap.min_fragment_size = runtime->hw.period_bytes_min;
249 compr->info.compr_cap.max_fragment_size = runtime->hw.period_bytes_max;
250 compr->info.compr_cap.min_fragments = runtime->hw.periods_min;
251 compr->info.compr_cap.max_fragments = runtime->hw.periods_max;
252 compr->info.compr_cap.codecs[0] = SND_AUDIOCODEC_MP3;
253 /* Add new codecs here */
254}
255
256static int msm_compr_open(struct snd_pcm_substream *substream)
257{
258 struct snd_pcm_runtime *runtime = substream->runtime;
259 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
260 struct compr_audio *compr;
261 struct msm_audio *prtd;
262 int ret = 0;
263
264 /* Capture path */
265 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
266 return -EINVAL;
267
268 pr_debug("%s\n", __func__);
269 compr = kzalloc(sizeof(struct compr_audio), GFP_KERNEL);
270 if (compr == NULL) {
271 pr_err("Failed to allocate memory for msm_audio\n");
272 return -ENOMEM;
273 }
274 prtd = &compr->prtd;
275 prtd->substream = substream;
276 prtd->audio_client = q6asm_audio_client_alloc(
277 (app_cb)compr_event_handler, compr);
278 if (!prtd->audio_client) {
279 pr_info("%s: Could not allocate memory\n", __func__);
280 kfree(prtd);
281 return -ENOMEM;
282 }
283 runtime->hw = msm_compr_hardware_playback;
284
285 pr_info("%s: session ID %d\n", __func__, prtd->audio_client->session);
286
287 prtd->session_id = prtd->audio_client->session;
288 msm_pcm_routing_reg_phy_stream(soc_prtd->dai_link->be_id,
289 prtd->session_id, substream->stream);
290
291 prtd->cmd_ack = 1;
292
293 ret = snd_pcm_hw_constraint_list(runtime, 0,
294 SNDRV_PCM_HW_PARAM_RATE,
295 &constraints_sample_rates);
296 if (ret < 0)
297 pr_info("snd_pcm_hw_constraint_list failed\n");
298 /* Ensure that buffer size is a multiple of period size */
299 ret = snd_pcm_hw_constraint_integer(runtime,
300 SNDRV_PCM_HW_PARAM_PERIODS);
301 if (ret < 0)
302 pr_info("snd_pcm_hw_constraint_integer failed\n");
303
304 prtd->dsp_cnt = 0;
Asish Bhattacharya31bd80d2012-01-04 02:30:13 +0530305 atomic_set(&prtd->pending_buffer, 1);
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530306 compr->codec = FORMAT_MP3;
307 populate_codec_list(compr, runtime);
308 runtime->private_data = compr;
309
310 return 0;
311}
312
313static int msm_compr_playback_close(struct snd_pcm_substream *substream)
314{
315 struct snd_pcm_runtime *runtime = substream->runtime;
316 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
317 struct compr_audio *compr = runtime->private_data;
318 struct msm_audio *prtd = &compr->prtd;
319 int dir = 0;
320
321 pr_debug("%s\n", __func__);
322
323 dir = IN;
Asish Bhattacharya31bd80d2012-01-04 02:30:13 +0530324 atomic_set(&prtd->pending_buffer, 0);
Asish Bhattacharya305d1752011-11-01 20:38:26 +0530325 q6asm_cmd(prtd->audio_client, CMD_CLOSE);
326 q6asm_audio_client_buf_free_contiguous(dir,
327 prtd->audio_client);
328
329 msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->be_id,
330 SNDRV_PCM_STREAM_PLAYBACK);
331 q6asm_audio_client_free(prtd->audio_client);
332 kfree(prtd);
333 return 0;
334}
335
336static int msm_compr_close(struct snd_pcm_substream *substream)
337{
338 int ret = 0;
339
340 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
341 ret = msm_compr_playback_close(substream);
342 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
343 ret = EINVAL;
344 return ret;
345}
346static int msm_compr_prepare(struct snd_pcm_substream *substream)
347{
348 int ret = 0;
349
350 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
351 ret = msm_compr_playback_prepare(substream);
352 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
353 ret = EINVAL;
354 return ret;
355}
356
357static snd_pcm_uframes_t msm_compr_pointer(struct snd_pcm_substream *substream)
358{
359
360 struct snd_pcm_runtime *runtime = substream->runtime;
361 struct compr_audio *compr = runtime->private_data;
362 struct msm_audio *prtd = &compr->prtd;
363
364 if (prtd->pcm_irq_pos >= prtd->pcm_size)
365 prtd->pcm_irq_pos = 0;
366
367 pr_debug("pcm_irq_pos = %d\n", prtd->pcm_irq_pos);
368 return bytes_to_frames(runtime, (prtd->pcm_irq_pos));
369}
370
371static int msm_compr_mmap(struct snd_pcm_substream *substream,
372 struct vm_area_struct *vma)
373{
374 int result = 0;
375 struct snd_pcm_runtime *runtime = substream->runtime;
376 struct compr_audio *compr = runtime->private_data;
377 struct msm_audio *prtd = &compr->prtd;
378
379 pr_debug("%s\n", __func__);
380 prtd->mmap_flag = 1;
381 if (runtime->dma_addr && runtime->dma_bytes) {
382 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
383 result = remap_pfn_range(vma, vma->vm_start,
384 runtime->dma_addr >> PAGE_SHIFT,
385 runtime->dma_bytes,
386 vma->vm_page_prot);
387 } else {
388 pr_err("Physical address or size of buf is NULL");
389 return -EINVAL;
390 }
391 return result;
392}
393
394static int msm_compr_hw_params(struct snd_pcm_substream *substream,
395 struct snd_pcm_hw_params *params)
396{
397 struct snd_pcm_runtime *runtime = substream->runtime;
398 struct compr_audio *compr = runtime->private_data;
399 struct msm_audio *prtd = &compr->prtd;
400 struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
401 struct audio_buffer *buf;
402 int dir, ret;
403
404 pr_debug("%s\n", __func__);
405 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
406 dir = IN;
407 else
408 return -EINVAL;
409
410 ret = q6asm_open_write(prtd->audio_client, compr->codec);
411 if (ret < 0) {
412 pr_err("%s: Session out open failed\n", __func__);
413 return -ENOMEM;
414 }
415 ret = q6asm_set_io_mode(prtd->audio_client, ASYNC_IO_MODE);
416 if (ret < 0) {
417 pr_err("%s: Set IO mode failed\n", __func__);
418 return -ENOMEM;
419 }
420
421 ret = q6asm_audio_client_buf_alloc_contiguous(dir,
422 prtd->audio_client,
423 runtime->hw.period_bytes_min,
424 runtime->hw.periods_max);
425 if (ret < 0) {
426 pr_err("Audio Start: Buffer Allocation failed "
427 "rc = %d\n", ret);
428 return -ENOMEM;
429 }
430 buf = prtd->audio_client->port[dir].buf;
431
432 pr_debug("%s:buf = %p\n", __func__, buf);
433 dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
434 dma_buf->dev.dev = substream->pcm->card->dev;
435 dma_buf->private_data = NULL;
436 dma_buf->area = buf[0].data;
437 dma_buf->addr = buf[0].phys;
438 dma_buf->bytes = runtime->hw.buffer_bytes_max;
439 if (!dma_buf->area)
440 return -ENOMEM;
441
442 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
443 return 0;
444}
445
446static int msm_compr_ioctl(struct snd_pcm_substream *substream,
447 unsigned int cmd, void *arg)
448{
449 int rc = 0;
450 struct snd_pcm_runtime *runtime = substream->runtime;
451 struct compr_audio *compr = runtime->private_data;
452 struct msm_audio *prtd = &compr->prtd;
453
454 switch (cmd) {
455 case SNDRV_COMPRESS_GET_CAPS:
456 pr_debug("SNDRV_COMPRESS_GET_CAPS\n");
457 if (copy_to_user((void *) arg, &compr->info.compr_cap,
458 sizeof(struct snd_compr_caps))) {
459 rc = -EFAULT;
460 pr_err("%s: ERROR: copy to user\n", __func__);
461 return rc;
462 }
463 return 0;
464 case SNDRV_COMPRESS_SET_PARAMS:
465 pr_debug("SNDRV_COMPRESS_SET_PARAMS: ");
466 if (copy_from_user(&compr->info.codec_param, (void *) arg,
467 sizeof(struct snd_compr_params))) {
468 rc = -EFAULT;
469 pr_err("%s: ERROR: copy from user\n", __func__);
470 return rc;
471 }
472 switch (compr->info.codec_param.codec.id) {
473 case SND_AUDIOCODEC_MP3:
474 /* For MP3 we dont need any other parameter */
475 pr_debug("SND_AUDIOCODEC_MP3\n");
476 compr->codec = FORMAT_MP3;
477 break;
478 default:
479 pr_debug("FORMAT_LINEAR_PCM\n");
480 compr->codec = FORMAT_LINEAR_PCM;
481 break;
482 }
483 return 0;
484 case SNDRV_PCM_IOCTL1_RESET:
485 prtd->cmd_ack = 0;
486 rc = q6asm_cmd(prtd->audio_client, CMD_FLUSH);
487 if (rc < 0)
488 pr_err("%s: flush cmd failed rc=%d\n", __func__, rc);
489 rc = wait_event_timeout(the_locks.eos_wait,
490 prtd->cmd_ack, 5 * HZ);
491 if (rc < 0)
492 pr_err("Flush cmd timeout\n");
493 prtd->pcm_irq_pos = 0;
494 break;
495 default:
496 break;
497 }
498 return snd_pcm_lib_ioctl(substream, cmd, arg);
499}
500
501static struct snd_pcm_ops msm_compr_ops = {
502 .open = msm_compr_open,
503 .hw_params = msm_compr_hw_params,
504 .close = msm_compr_close,
505 .ioctl = msm_compr_ioctl,
506 .prepare = msm_compr_prepare,
507 .trigger = msm_compr_trigger,
508 .pointer = msm_compr_pointer,
509 .mmap = msm_compr_mmap,
510};
511
512static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
513{
514 struct snd_card *card = rtd->card->snd_card;
515 int ret = 0;
516
517 if (!card->dev->coherent_dma_mask)
518 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
519 return ret;
520}
521
522static struct snd_soc_platform_driver msm_soc_platform = {
523 .ops = &msm_compr_ops,
524 .pcm_new = msm_asoc_pcm_new,
525};
526
527static __devinit int msm_compr_probe(struct platform_device *pdev)
528{
529 pr_info("%s: dev name %s\n", __func__, dev_name(&pdev->dev));
530 return snd_soc_register_platform(&pdev->dev,
531 &msm_soc_platform);
532}
533
534static int msm_compr_remove(struct platform_device *pdev)
535{
536 snd_soc_unregister_platform(&pdev->dev);
537 return 0;
538}
539
540static struct platform_driver msm_compr_driver = {
541 .driver = {
542 .name = "msm-compr-dsp",
543 .owner = THIS_MODULE,
544 },
545 .probe = msm_compr_probe,
546 .remove = __devexit_p(msm_compr_remove),
547};
548
549static int __init msm_soc_platform_init(void)
550{
551 init_waitqueue_head(&the_locks.enable_wait);
552 init_waitqueue_head(&the_locks.eos_wait);
553 init_waitqueue_head(&the_locks.write_wait);
554 init_waitqueue_head(&the_locks.read_wait);
555
556 return platform_driver_register(&msm_compr_driver);
557}
558module_init(msm_soc_platform_init);
559
560static void __exit msm_soc_platform_exit(void)
561{
562 platform_driver_unregister(&msm_compr_driver);
563}
564module_exit(msm_soc_platform_exit);
565
566MODULE_DESCRIPTION("PCM module platform driver");
567MODULE_LICENSE("GPL v2");