1 /* arch/arm/mach-msm/audio_evrc.c
3 * Copyright (c) 2008 QUALCOMM USA, INC.
5 * This code also borrows from audio_aac.c, which is
6 * Copyright (C) 2008 Google, Inc.
7 * Copyright (C) 2008 HTC Corporation
9 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 * See the GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you can find it at http://www.fsf.org.
22 #include <linux/module.h>
24 #include <linux/miscdevice.h>
25 #include <linux/uaccess.h>
26 #include <linux/kthread.h>
27 #include <linux/wait.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/delay.h>
31 #include <asm/atomic.h>
32 #include <asm/ioctls.h>
33 #include <mach/msm_adsp.h>
34 #include <linux/msm_audio.h>
37 #include <mach/qdsp5/qdsp5audppcmdi.h>
38 #include <mach/qdsp5/qdsp5audppmsg.h>
39 #include <mach/qdsp5/qdsp5audplaycmdi.h>
40 #include <mach/qdsp5/qdsp5audplaymsg.h>
45 #define dprintk(format, arg...) \
46 printk(KERN_DEBUG format, ## arg)
48 #define dprintk(format, arg...) do {} while (0)
51 /* Hold 30 packets of 24 bytes each*/
53 #define DMASZ (BUFSZ * 2)
55 #define AUDDEC_DEC_EVRC 12
57 #define PCM_BUFSZ_MIN 1600 /* 100ms worth of data */
58 #define PCM_BUF_MAX_COUNT 5
59 /* DSP only accepts 5 buffers at most
60 * but support 2 buffers currently
62 #define EVRC_DECODED_FRSZ 320 /* EVRC 20ms 8KHz mono PCM size */
64 #define ROUTING_MODE_FTRT 1
65 #define ROUTING_MODE_RT 2
66 /* Decoder status received from AUDPPTASK */
67 #define AUDPP_DEC_STATUS_SLEEP 0
68 #define AUDPP_DEC_STATUS_INIT 1
69 #define AUDPP_DEC_STATUS_CFG 2
70 #define AUDPP_DEC_STATUS_PLAY 3
75 unsigned used; /* Input usage actual DSP produced PCM size */
86 uint8_t out_needed; /* number of buffers the dsp is waiting for */
91 struct mutex write_lock;
92 wait_queue_head_t write_wait;
94 /* Host PCM section */
95 struct buffer in[PCM_BUF_MAX_COUNT];
96 struct mutex read_lock;
97 wait_queue_head_t read_wait; /* Wait queue for read */
98 char *read_data; /* pointer to reader buffer */
99 dma_addr_t read_phys; /* physical address of reader buffer */
100 uint8_t read_next; /* index to input buffers to be read next */
101 uint8_t fill_next; /* index to buffer that DSP should be filling */
102 uint8_t pcm_buf_count; /* number of pcm buffer allocated */
103 /* ---- End of Host PCM section */
105 struct msm_adsp_module *audplay;
106 struct audmgr audmgr;
108 /* data allocated for various buffers */
115 uint8_t stopped:1; /* set when stopped, cleared on flush */
116 uint8_t pcm_feedback:1;
117 uint8_t buf_refresh:1;
121 uint32_t read_ptr_offset;
123 static struct audio the_evrc_audio;
125 static int auddec_dsp_config(struct audio *audio, int enable);
126 static void audpp_cmd_cfg_adec_params(struct audio *audio);
127 static void audpp_cmd_cfg_routing_mode(struct audio *audio);
128 static void audevrc_send_data(struct audio *audio, unsigned needed);
129 static void audevrc_dsp_event(void *private, unsigned id, uint16_t *msg);
130 static void audevrc_config_hostpcm(struct audio *audio);
131 static void audevrc_buffer_refresh(struct audio *audio);
133 /* must be called with audio->lock held */
134 static int audevrc_enable(struct audio *audio)
136 struct audmgr_config cfg;
143 audio->out_needed = 0;
145 cfg.tx_rate = RPC_AUD_DEF_SAMPLE_RATE_NONE;
146 cfg.rx_rate = RPC_AUD_DEF_SAMPLE_RATE_48000;
147 cfg.def_method = RPC_AUD_DEF_METHOD_PLAYBACK;
148 cfg.codec = RPC_AUD_DEF_CODEC_EVRC;
149 cfg.snd_method = RPC_SND_METHOD_MIDI;
151 rc = audmgr_enable(&audio->audmgr, &cfg);
155 if (msm_adsp_enable(audio->audplay)) {
156 pr_err("audio: msm_adsp_enable(audplay) failed\n");
157 audmgr_disable(&audio->audmgr);
161 if (audpp_enable(audio->dec_id, audevrc_dsp_event, audio)) {
162 pr_err("audio: audpp_enable() failed\n");
163 msm_adsp_disable(audio->audplay);
164 audmgr_disable(&audio->audmgr);
171 /* must be called with audio->lock held */
172 static int audevrc_disable(struct audio *audio)
174 if (audio->enabled) {
176 auddec_dsp_config(audio, 0);
177 wake_up(&audio->write_wait);
178 wake_up(&audio->read_wait);
179 msm_adsp_disable(audio->audplay);
180 audpp_disable(audio->dec_id, audio);
181 audmgr_disable(&audio->audmgr);
182 audio->out_needed = 0;
187 /* ------------------- dsp --------------------- */
189 static void audevrc_update_pcm_buf_entry(struct audio *audio,
195 spin_lock_irqsave(&audio->dsp_lock, flags);
196 for (index = 0; index < payload[1]; index++) {
197 if (audio->in[audio->fill_next].addr
198 == payload[2 + index * 2]) {
199 dprintk("audevrc_update_pcm_buf_entry: in[%d] ready\n",
201 audio->in[audio->fill_next].used =
202 payload[3 + index * 2];
203 if ((++audio->fill_next) == audio->pcm_buf_count)
204 audio->fill_next = 0;
208 ("audevrc_update_pcm_buf_entry: expected=%x ret=%x\n",
209 audio->in[audio->fill_next].addr,
210 payload[1 + index * 2]);
214 if (audio->in[audio->fill_next].used == 0) {
215 audevrc_buffer_refresh(audio);
217 dprintk("audevrc_update_pcm_buf_entry: read cannot keep up\n");
218 audio->buf_refresh = 1;
221 spin_unlock_irqrestore(&audio->dsp_lock, flags);
222 wake_up(&audio->read_wait);
225 static void audplay_dsp_event(void *data, unsigned id, size_t len,
226 void (*getevent) (void *ptr, size_t len))
228 struct audio *audio = data;
230 getevent(msg, sizeof(msg));
232 dprintk("audplay_dsp_event: msg_id=%x\n", id);
234 case AUDPLAY_MSG_DEC_NEEDS_DATA:
235 audevrc_send_data(audio, 1);
237 case AUDPLAY_MSG_BUFFER_UPDATE:
238 dprintk("audevrc_update_pcm_buf_entry:======> \n");
239 audevrc_update_pcm_buf_entry(audio, msg);
242 pr_err("unexpected message from decoder \n");
246 static void audevrc_dsp_event(void *private, unsigned id, uint16_t *msg)
248 struct audio *audio = private;
251 case AUDPP_MSG_STATUS_MSG:{
252 unsigned status = msg[1];
255 case AUDPP_DEC_STATUS_SLEEP:
256 dprintk("decoder status: sleep \n");
259 case AUDPP_DEC_STATUS_INIT:
260 dprintk("decoder status: init \n");
261 audpp_cmd_cfg_routing_mode(audio);
264 case AUDPP_DEC_STATUS_CFG:
265 dprintk("decoder status: cfg \n");
267 case AUDPP_DEC_STATUS_PLAY:
268 dprintk("decoder status: play \n");
269 if (audio->pcm_feedback) {
270 audevrc_config_hostpcm(audio);
271 audevrc_buffer_refresh(audio);
275 pr_err("unknown decoder status \n");
279 case AUDPP_MSG_CFG_MSG:
280 if (msg[0] == AUDPP_MSG_ENA_ENA) {
281 dprintk("audevrc_dsp_event: CFG_MSG ENABLE\n");
282 auddec_dsp_config(audio, 1);
283 audio->out_needed = 0;
285 audpp_set_volume_and_pan(audio->dec_id, audio->volume,
287 audpp_avsync(audio->dec_id, 22050);
288 } else if (msg[0] == AUDPP_MSG_ENA_DIS) {
289 dprintk("audevrc_dsp_event: CFG_MSG DISABLE\n");
290 audpp_avsync(audio->dec_id, 0);
293 pr_err("audevrc_dsp_event: CFG_MSG %d?\n", msg[0]);
296 case AUDPP_MSG_ROUTING_ACK:
297 dprintk("audevrc_dsp_event: ROUTING_ACK\n");
298 audpp_cmd_cfg_adec_params(audio);
302 pr_err("audevrc_dsp_event: UNKNOWN (%d)\n", id);
307 struct msm_adsp_ops audplay_adsp_ops_evrc = {
308 .event = audplay_dsp_event,
311 #define audplay_send_queue0(audio, cmd, len) \
312 msm_adsp_write(audio->audplay, QDSP_uPAudPlay0BitStreamCtrlQueue, \
315 static int auddec_dsp_config(struct audio *audio, int enable)
317 audpp_cmd_cfg_dec_type cmd;
319 memset(&cmd, 0, sizeof(cmd));
320 cmd.cmd_id = AUDPP_CMD_CFG_DEC_TYPE;
322 cmd.dec0_cfg = AUDPP_CMD_UPDATDE_CFG_DEC |
323 AUDPP_CMD_ENA_DEC_V | AUDDEC_DEC_EVRC;
325 cmd.dec0_cfg = AUDPP_CMD_UPDATDE_CFG_DEC | AUDPP_CMD_DIS_DEC_V;
327 return audpp_send_queue1(&cmd, sizeof(cmd));
330 static void audpp_cmd_cfg_adec_params(struct audio *audio)
332 struct audpp_cmd_cfg_adec_params_evrc cmd;
334 memset(&cmd, 0, sizeof(cmd));
335 cmd.common.cmd_id = AUDPP_CMD_CFG_ADEC_PARAMS;
336 cmd.common.length = sizeof(cmd);
337 cmd.common.dec_id = audio->dec_id;
338 cmd.common.input_sampling_frequency = 8000;
339 cmd.stereo_cfg = AUDPP_CMD_PCM_INTF_MONO_V;
341 audpp_send_queue2(&cmd, sizeof(cmd));
344 static void audpp_cmd_cfg_routing_mode(struct audio *audio)
346 struct audpp_cmd_routing_mode cmd;
347 dprintk("audpp_cmd_cfg_routing_mode()\n");
348 memset(&cmd, 0, sizeof(cmd));
349 cmd.cmd_id = AUDPP_CMD_ROUTING_MODE;
350 cmd.object_number = audio->dec_id;
351 if (audio->pcm_feedback)
352 cmd.routing_mode = ROUTING_MODE_FTRT;
354 cmd.routing_mode = ROUTING_MODE_RT;
356 audpp_send_queue1(&cmd, sizeof(cmd));
359 static int audplay_dsp_send_data_avail(struct audio *audio,
360 unsigned idx, unsigned len)
362 audplay_cmd_bitstream_data_avail cmd;
364 cmd.cmd_id = AUDPLAY_CMD_BITSTREAM_DATA_AVAIL;
365 cmd.decoder_id = audio->dec_id;
366 cmd.buf_ptr = audio->out[idx].addr;
367 cmd.buf_size = len / 2;
368 cmd.partition_number = 0;
369 return audplay_send_queue0(audio, &cmd, sizeof(cmd));
372 static void audevrc_buffer_refresh(struct audio *audio)
374 struct audplay_cmd_buffer_refresh refresh_cmd;
376 refresh_cmd.cmd_id = AUDPLAY_CMD_BUFFER_REFRESH;
377 refresh_cmd.num_buffers = 1;
378 refresh_cmd.buf0_address = audio->in[audio->fill_next].addr;
379 refresh_cmd.buf0_length = audio->in[audio->fill_next].size;
381 refresh_cmd.buf_read_count = 0;
382 dprintk("audplay_buffer_fresh: buf0_addr=%x buf0_len=%d\n",
383 refresh_cmd.buf0_address, refresh_cmd.buf0_length);
384 audplay_send_queue0(audio, &refresh_cmd, sizeof(refresh_cmd));
387 static void audevrc_config_hostpcm(struct audio *audio)
389 struct audplay_cmd_hpcm_buf_cfg cfg_cmd;
391 dprintk("audevrc_config_hostpcm()\n");
392 cfg_cmd.cmd_id = AUDPLAY_CMD_HPCM_BUF_CFG;
393 cfg_cmd.max_buffers = 1;
394 cfg_cmd.byte_swap = 0;
395 cfg_cmd.hostpcm_config = (0x8000) | (0x4000);
396 cfg_cmd.feedback_frequency = 1;
397 cfg_cmd.partition_number = 0;
398 audplay_send_queue0(audio, &cfg_cmd, sizeof(cfg_cmd));
402 static void audevrc_send_data(struct audio *audio, unsigned needed)
404 struct buffer *frame;
407 spin_lock_irqsave(&audio->dsp_lock, flags);
412 /* We were called from the callback because the DSP
413 * requested more data. Note that the DSP does want
414 * more data, and if a buffer was in-flight, mark it
415 * as available (since the DSP must now be done with
418 audio->out_needed = 1;
419 frame = audio->out + audio->out_tail;
420 if (frame->used == 0xffffffff) {
421 dprintk("frame %d free\n", audio->out_tail);
423 audio->out_tail ^= 1;
424 wake_up(&audio->write_wait);
428 if (audio->out_needed) {
429 /* If the DSP currently wants data and we have a
430 * buffer available, we will send it and reset
431 * the needed flag. We'll mark the buffer as in-flight
432 * so that it won't be recycled until the next buffer
436 frame = audio->out + audio->out_tail;
438 BUG_ON(frame->used == 0xffffffff);
439 dprintk("frame %d busy\n", audio->out_tail);
440 audplay_dsp_send_data_avail(audio, audio->out_tail,
442 frame->used = 0xffffffff;
443 audio->out_needed = 0;
447 spin_unlock_irqrestore(&audio->dsp_lock, flags);
450 /* ------------------- device --------------------- */
452 static void audevrc_flush(struct audio *audio)
454 audio->out[0].used = 0;
455 audio->out[1].used = 0;
459 atomic_set(&audio->out_bytes, 0);
462 static void audevrc_flush_pcm_buf(struct audio *audio)
466 for (index = 0; index < PCM_BUF_MAX_COUNT; index++)
467 audio->in[index].used = 0;
469 audio->read_next = 0;
470 audio->fill_next = 0;
473 static long audevrc_ioctl(struct file *file, unsigned int cmd,
476 struct audio *audio = file->private_data;
479 dprintk("audevrc_ioctl() cmd = %d\n", cmd);
481 if (cmd == AUDIO_GET_STATS) {
482 struct msm_audio_stats stats;
483 stats.byte_count = audpp_avsync_byte_count(audio->dec_id);
484 stats.sample_count = audpp_avsync_sample_count(audio->dec_id);
485 if (copy_to_user((void *)arg, &stats, sizeof(stats)))
489 if (cmd == AUDIO_SET_VOLUME) {
491 spin_lock_irqsave(&audio->dsp_lock, flags);
494 audpp_set_volume_and_pan(audio->dec_id, arg, 0);
495 spin_unlock_irqrestore(&audio->dsp_lock, flags);
498 mutex_lock(&audio->lock);
501 rc = audevrc_enable(audio);
504 rc = audevrc_disable(audio);
507 case AUDIO_SET_CONFIG:{
508 dprintk("AUDIO_SET_CONFIG not applicable \n");
511 case AUDIO_GET_CONFIG:{
512 struct msm_audio_config config;
513 config.buffer_size = BUFSZ;
514 config.buffer_count = 2;
515 config.sample_rate = 8000;
516 config.channel_count = 1;
517 config.unused[0] = 0;
518 config.unused[1] = 0;
519 config.unused[2] = 0;
520 config.unused[3] = 0;
521 if (copy_to_user((void *)arg, &config, sizeof(config)))
527 case AUDIO_GET_PCM_CONFIG:{
528 struct msm_audio_pcm_config config;
529 config.pcm_feedback = 0;
530 config.buffer_count = PCM_BUF_MAX_COUNT;
531 config.buffer_size = PCM_BUFSZ_MIN;
532 if (copy_to_user((void *)arg, &config, sizeof(config)))
538 case AUDIO_SET_PCM_CONFIG:{
539 struct msm_audio_pcm_config config;
541 (&config, (void *)arg, sizeof(config))) {
545 if ((config.buffer_count > PCM_BUF_MAX_COUNT) ||
546 (config.buffer_count == 1))
547 config.buffer_count = PCM_BUF_MAX_COUNT;
549 if (config.buffer_size < PCM_BUFSZ_MIN)
550 config.buffer_size = PCM_BUFSZ_MIN;
552 /* Check if pcm feedback is required */
553 if ((config.pcm_feedback) && (!audio->read_data)) {
554 dprintk("audevrc_ioctl: allocate PCM buf %d\n",
555 config.buffer_count *
558 dma_alloc_coherent(NULL,
563 if (!audio->read_data) {
565 ("audevrc_ioctl: no mem for pcm buf\n");
570 audio->pcm_feedback = 1;
571 audio->buf_refresh = 0;
572 audio->pcm_buf_count =
574 audio->read_next = 0;
575 audio->fill_next = 0;
578 index < config.buffer_count;
580 audio->in[index].data =
581 audio->read_data + offset;
582 audio->in[index].addr =
583 audio->read_phys + offset;
584 audio->in[index].size =
586 audio->in[index].used = 0;
587 offset += config.buffer_size;
597 dprintk("%s: AUDIO_PAUSE %ld\n", __func__, arg);
598 rc = audpp_pause(audio->dec_id, (int) arg);
603 mutex_unlock(&audio->lock);
607 static ssize_t audevrc_read(struct file *file, char __user *buf, size_t count,
610 struct audio *audio = file->private_data;
611 const char __user *start = buf;
613 if (!audio->pcm_feedback) {
615 /* PCM feedback is not enabled. Nothing to read */
617 mutex_lock(&audio->read_lock);
618 dprintk("audevrc_read() \n");
620 rc = wait_event_interruptible(audio->read_wait,
621 (audio->in[audio->read_next].
622 used > 0) || (audio->stopped));
623 dprintk("audevrc_read() wait terminated \n");
626 if (audio->stopped) {
630 if (count < audio->in[audio->read_next].used) {
631 /* Read must happen in frame boundary. Since driver does
632 * not know frame size, read count must be greater or
633 * equal to size of PCM samples
635 dprintk("audevrc_read:read stop - partial frame\n");
638 dprintk("audevrc_read: read from in[%d]\n",
641 (buf, audio->in[audio->read_next].data,
642 audio->in[audio->read_next].used)) {
643 pr_err("audevrc_read: invalid addr %x \n",
648 count -= audio->in[audio->read_next].used;
649 buf += audio->in[audio->read_next].used;
650 audio->in[audio->read_next].used = 0;
651 if ((++audio->read_next) == audio->pcm_buf_count)
652 audio->read_next = 0;
653 if (audio->in[audio->read_next].used == 0)
654 break; /* No data ready at this moment
655 * Exit while loop to prevent
656 * output thread sleep too long
661 if (audio->buf_refresh) {
662 audio->buf_refresh = 0;
663 dprintk("audevrc_read: kick start pcm feedback again\n");
664 audevrc_buffer_refresh(audio);
666 mutex_unlock(&audio->read_lock);
669 dprintk("audevrc_read: read %d bytes\n", rc);
673 static ssize_t audevrc_write(struct file *file, const char __user *buf,
674 size_t count, loff_t *pos)
676 struct audio *audio = file->private_data;
677 const char __user *start = buf;
678 struct buffer *frame;
684 mutex_lock(&audio->write_lock);
685 dprintk("audevrc_write() \n");
687 frame = audio->out + audio->out_head;
688 rc = wait_event_interruptible(audio->write_wait,
690 || (audio->stopped));
693 if (audio->stopped) {
697 xfer = (count > frame->size) ? frame->size : count;
698 if (copy_from_user(frame->data, buf, xfer)) {
704 audio->out_head ^= 1;
708 audevrc_send_data(audio, 0);
711 mutex_unlock(&audio->write_lock);
717 static int audevrc_release(struct inode *inode, struct file *file)
719 struct audio *audio = file->private_data;
721 dprintk("audevrc_release()\n");
723 mutex_lock(&audio->lock);
724 audevrc_disable(audio);
725 audevrc_flush(audio);
726 audevrc_flush_pcm_buf(audio);
727 msm_adsp_put(audio->audplay);
728 audio->audplay = NULL;
730 dma_free_coherent(NULL, DMASZ, audio->data, audio->phys);
732 if (audio->read_data != NULL) {
733 dma_free_coherent(NULL,
734 audio->in[0].size * audio->pcm_buf_count,
735 audio->read_data, audio->read_phys);
736 audio->read_data = NULL;
738 audio->pcm_feedback = 0;
739 mutex_unlock(&audio->lock);
743 static struct audio the_evrc_audio;
745 static int audevrc_open(struct inode *inode, struct file *file)
747 struct audio *audio = &the_evrc_audio;
751 pr_err("audio: busy\n");
756 mutex_lock(&audio->lock);
759 audio->data = dma_alloc_coherent(NULL, DMASZ,
760 &audio->phys, GFP_KERNEL);
762 pr_err("audio: could not allocate DMA buffers\n");
768 rc = audmgr_open(&audio->audmgr);
772 rc = msm_adsp_get("AUDPLAY0TASK", &audio->audplay,
773 &audplay_adsp_ops_evrc, audio);
775 pr_err("audio: failed to get audplay0 dsp module\n");
781 audio->out[0].data = audio->data + 0;
782 audio->out[0].addr = audio->phys + 0;
783 audio->out[0].size = BUFSZ;
785 audio->out[1].data = audio->data + BUFSZ;
786 audio->out[1].addr = audio->phys + BUFSZ;
787 audio->out[1].size = BUFSZ;
789 audio->volume = 0x3FFF;
791 audevrc_flush(audio);
794 file->private_data = audio;
796 mutex_unlock(&audio->lock);
800 audmgr_close(&audio->audmgr);
802 dma_free_coherent(NULL, DMASZ, audio->data, audio->phys);
804 mutex_unlock(&audio->lock);
808 static struct file_operations audio_evrc_fops = {
809 .owner = THIS_MODULE,
810 .open = audevrc_open,
811 .release = audevrc_release,
812 .read = audevrc_read,
813 .write = audevrc_write,
814 .unlocked_ioctl = audevrc_ioctl,
817 struct miscdevice audio_evrc_misc = {
818 .minor = MISC_DYNAMIC_MINOR,
820 .fops = &audio_evrc_fops,
823 static int __init audevrc_init(void)
825 mutex_init(&the_evrc_audio.lock);
826 mutex_init(&the_evrc_audio.write_lock);
827 mutex_init(&the_evrc_audio.read_lock);
828 spin_lock_init(&the_evrc_audio.dsp_lock);
829 init_waitqueue_head(&the_evrc_audio.write_wait);
830 init_waitqueue_head(&the_evrc_audio.read_wait);
831 the_evrc_audio.read_data = NULL;
832 return misc_register(&audio_evrc_misc);
835 static void __exit audevrc_exit(void)
837 misc_deregister(&audio_evrc_misc);
840 module_init(audevrc_init);
841 module_exit(audevrc_exit);
843 MODULE_DESCRIPTION("MSM EVRC driver");
844 MODULE_LICENSE("GPL v2");
845 MODULE_AUTHOR("QUALCOMM Inc");