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>
30 #include <linux/gfp.h>
32 #include <asm/atomic.h>
33 #include <asm/ioctls.h>
34 #include <mach/msm_adsp.h>
35 #include <linux/msm_audio.h>
38 #include <mach/qdsp5/qdsp5audppcmdi.h>
39 #include <mach/qdsp5/qdsp5audppmsg.h>
40 #include <mach/qdsp5/qdsp5audplaycmdi.h>
41 #include <mach/qdsp5/qdsp5audplaymsg.h>
46 #define dprintk(format, arg...) \
47 printk(KERN_DEBUG format, ## arg)
49 #define dprintk(format, arg...) do {} while (0)
52 /* Hold 30 packets of 24 bytes each*/
54 #define DMASZ (BUFSZ * 2)
56 #define AUDDEC_DEC_EVRC 12
58 #define PCM_BUFSZ_MIN 1600 /* 100ms worth of data */
59 #define PCM_BUF_MAX_COUNT 5
60 /* DSP only accepts 5 buffers at most
61 * but support 2 buffers currently
63 #define EVRC_DECODED_FRSZ 320 /* EVRC 20ms 8KHz mono PCM size */
65 #define ROUTING_MODE_FTRT 1
66 #define ROUTING_MODE_RT 2
67 /* Decoder status received from AUDPPTASK */
68 #define AUDPP_DEC_STATUS_SLEEP 0
69 #define AUDPP_DEC_STATUS_INIT 1
70 #define AUDPP_DEC_STATUS_CFG 2
71 #define AUDPP_DEC_STATUS_PLAY 3
76 unsigned used; /* Input usage actual DSP produced PCM size */
87 uint8_t out_needed; /* number of buffers the dsp is waiting for */
92 struct mutex write_lock;
93 wait_queue_head_t write_wait;
95 /* Host PCM section */
96 struct buffer in[PCM_BUF_MAX_COUNT];
97 struct mutex read_lock;
98 wait_queue_head_t read_wait; /* Wait queue for read */
99 char *read_data; /* pointer to reader buffer */
100 dma_addr_t read_phys; /* physical address of reader buffer */
101 uint8_t read_next; /* index to input buffers to be read next */
102 uint8_t fill_next; /* index to buffer that DSP should be filling */
103 uint8_t pcm_buf_count; /* number of pcm buffer allocated */
104 /* ---- End of Host PCM section */
106 struct msm_adsp_module *audplay;
107 struct audmgr audmgr;
109 /* data allocated for various buffers */
116 uint8_t stopped:1; /* set when stopped, cleared on flush */
117 uint8_t pcm_feedback:1;
118 uint8_t buf_refresh:1;
122 uint32_t read_ptr_offset;
124 static struct audio the_evrc_audio;
126 static int auddec_dsp_config(struct audio *audio, int enable);
127 static void audpp_cmd_cfg_adec_params(struct audio *audio);
128 static void audpp_cmd_cfg_routing_mode(struct audio *audio);
129 static void audevrc_send_data(struct audio *audio, unsigned needed);
130 static void audevrc_dsp_event(void *private, unsigned id, uint16_t *msg);
131 static void audevrc_config_hostpcm(struct audio *audio);
132 static void audevrc_buffer_refresh(struct audio *audio);
134 /* must be called with audio->lock held */
135 static int audevrc_enable(struct audio *audio)
137 struct audmgr_config cfg;
144 audio->out_needed = 0;
146 cfg.tx_rate = RPC_AUD_DEF_SAMPLE_RATE_NONE;
147 cfg.rx_rate = RPC_AUD_DEF_SAMPLE_RATE_48000;
148 cfg.def_method = RPC_AUD_DEF_METHOD_PLAYBACK;
149 cfg.codec = RPC_AUD_DEF_CODEC_EVRC;
150 cfg.snd_method = RPC_SND_METHOD_MIDI;
152 rc = audmgr_enable(&audio->audmgr, &cfg);
156 if (msm_adsp_enable(audio->audplay)) {
157 pr_err("audio: msm_adsp_enable(audplay) failed\n");
158 audmgr_disable(&audio->audmgr);
162 if (audpp_enable(audio->dec_id, audevrc_dsp_event, audio)) {
163 pr_err("audio: audpp_enable() failed\n");
164 msm_adsp_disable(audio->audplay);
165 audmgr_disable(&audio->audmgr);
172 /* must be called with audio->lock held */
173 static int audevrc_disable(struct audio *audio)
175 if (audio->enabled) {
177 auddec_dsp_config(audio, 0);
178 wake_up(&audio->write_wait);
179 wake_up(&audio->read_wait);
180 msm_adsp_disable(audio->audplay);
181 audpp_disable(audio->dec_id, audio);
182 audmgr_disable(&audio->audmgr);
183 audio->out_needed = 0;
188 /* ------------------- dsp --------------------- */
190 static void audevrc_update_pcm_buf_entry(struct audio *audio,
196 spin_lock_irqsave(&audio->dsp_lock, flags);
197 for (index = 0; index < payload[1]; index++) {
198 if (audio->in[audio->fill_next].addr
199 == payload[2 + index * 2]) {
200 dprintk("audevrc_update_pcm_buf_entry: in[%d] ready\n",
202 audio->in[audio->fill_next].used =
203 payload[3 + index * 2];
204 if ((++audio->fill_next) == audio->pcm_buf_count)
205 audio->fill_next = 0;
209 ("audevrc_update_pcm_buf_entry: expected=%x ret=%x\n",
210 audio->in[audio->fill_next].addr,
211 payload[1 + index * 2]);
215 if (audio->in[audio->fill_next].used == 0) {
216 audevrc_buffer_refresh(audio);
218 dprintk("audevrc_update_pcm_buf_entry: read cannot keep up\n");
219 audio->buf_refresh = 1;
222 spin_unlock_irqrestore(&audio->dsp_lock, flags);
223 wake_up(&audio->read_wait);
226 static void audplay_dsp_event(void *data, unsigned id, size_t len,
227 void (*getevent) (void *ptr, size_t len))
229 struct audio *audio = data;
231 getevent(msg, sizeof(msg));
233 dprintk("audplay_dsp_event: msg_id=%x\n", id);
235 case AUDPLAY_MSG_DEC_NEEDS_DATA:
236 audevrc_send_data(audio, 1);
238 case AUDPLAY_MSG_BUFFER_UPDATE:
239 dprintk("audevrc_update_pcm_buf_entry:======> \n");
240 audevrc_update_pcm_buf_entry(audio, msg);
243 pr_err("unexpected message from decoder \n");
247 static void audevrc_dsp_event(void *private, unsigned id, uint16_t *msg)
249 struct audio *audio = private;
252 case AUDPP_MSG_STATUS_MSG:{
253 unsigned status = msg[1];
256 case AUDPP_DEC_STATUS_SLEEP:
257 dprintk("decoder status: sleep \n");
260 case AUDPP_DEC_STATUS_INIT:
261 dprintk("decoder status: init \n");
262 audpp_cmd_cfg_routing_mode(audio);
265 case AUDPP_DEC_STATUS_CFG:
266 dprintk("decoder status: cfg \n");
268 case AUDPP_DEC_STATUS_PLAY:
269 dprintk("decoder status: play \n");
270 if (audio->pcm_feedback) {
271 audevrc_config_hostpcm(audio);
272 audevrc_buffer_refresh(audio);
276 pr_err("unknown decoder status \n");
280 case AUDPP_MSG_CFG_MSG:
281 if (msg[0] == AUDPP_MSG_ENA_ENA) {
282 dprintk("audevrc_dsp_event: CFG_MSG ENABLE\n");
283 auddec_dsp_config(audio, 1);
284 audio->out_needed = 0;
286 audpp_set_volume_and_pan(audio->dec_id, audio->volume,
288 audpp_avsync(audio->dec_id, 22050);
289 } else if (msg[0] == AUDPP_MSG_ENA_DIS) {
290 dprintk("audevrc_dsp_event: CFG_MSG DISABLE\n");
291 audpp_avsync(audio->dec_id, 0);
294 pr_err("audevrc_dsp_event: CFG_MSG %d?\n", msg[0]);
297 case AUDPP_MSG_ROUTING_ACK:
298 dprintk("audevrc_dsp_event: ROUTING_ACK\n");
299 audpp_cmd_cfg_adec_params(audio);
303 pr_err("audevrc_dsp_event: UNKNOWN (%d)\n", id);
308 struct msm_adsp_ops audplay_adsp_ops_evrc = {
309 .event = audplay_dsp_event,
312 #define audplay_send_queue0(audio, cmd, len) \
313 msm_adsp_write(audio->audplay, QDSP_uPAudPlay0BitStreamCtrlQueue, \
316 static int auddec_dsp_config(struct audio *audio, int enable)
318 audpp_cmd_cfg_dec_type cmd;
320 memset(&cmd, 0, sizeof(cmd));
321 cmd.cmd_id = AUDPP_CMD_CFG_DEC_TYPE;
323 cmd.dec0_cfg = AUDPP_CMD_UPDATDE_CFG_DEC |
324 AUDPP_CMD_ENA_DEC_V | AUDDEC_DEC_EVRC;
326 cmd.dec0_cfg = AUDPP_CMD_UPDATDE_CFG_DEC | AUDPP_CMD_DIS_DEC_V;
328 return audpp_send_queue1(&cmd, sizeof(cmd));
331 static void audpp_cmd_cfg_adec_params(struct audio *audio)
333 struct audpp_cmd_cfg_adec_params_evrc cmd;
335 memset(&cmd, 0, sizeof(cmd));
336 cmd.common.cmd_id = AUDPP_CMD_CFG_ADEC_PARAMS;
337 cmd.common.length = sizeof(cmd);
338 cmd.common.dec_id = audio->dec_id;
339 cmd.common.input_sampling_frequency = 8000;
340 cmd.stereo_cfg = AUDPP_CMD_PCM_INTF_MONO_V;
342 audpp_send_queue2(&cmd, sizeof(cmd));
345 static void audpp_cmd_cfg_routing_mode(struct audio *audio)
347 struct audpp_cmd_routing_mode cmd;
348 dprintk("audpp_cmd_cfg_routing_mode()\n");
349 memset(&cmd, 0, sizeof(cmd));
350 cmd.cmd_id = AUDPP_CMD_ROUTING_MODE;
351 cmd.object_number = audio->dec_id;
352 if (audio->pcm_feedback)
353 cmd.routing_mode = ROUTING_MODE_FTRT;
355 cmd.routing_mode = ROUTING_MODE_RT;
357 audpp_send_queue1(&cmd, sizeof(cmd));
360 static int audplay_dsp_send_data_avail(struct audio *audio,
361 unsigned idx, unsigned len)
363 audplay_cmd_bitstream_data_avail cmd;
365 cmd.cmd_id = AUDPLAY_CMD_BITSTREAM_DATA_AVAIL;
366 cmd.decoder_id = audio->dec_id;
367 cmd.buf_ptr = audio->out[idx].addr;
368 cmd.buf_size = len / 2;
369 cmd.partition_number = 0;
370 return audplay_send_queue0(audio, &cmd, sizeof(cmd));
373 static void audevrc_buffer_refresh(struct audio *audio)
375 struct audplay_cmd_buffer_refresh refresh_cmd;
377 refresh_cmd.cmd_id = AUDPLAY_CMD_BUFFER_REFRESH;
378 refresh_cmd.num_buffers = 1;
379 refresh_cmd.buf0_address = audio->in[audio->fill_next].addr;
380 refresh_cmd.buf0_length = audio->in[audio->fill_next].size;
382 refresh_cmd.buf_read_count = 0;
383 dprintk("audplay_buffer_fresh: buf0_addr=%x buf0_len=%d\n",
384 refresh_cmd.buf0_address, refresh_cmd.buf0_length);
385 audplay_send_queue0(audio, &refresh_cmd, sizeof(refresh_cmd));
388 static void audevrc_config_hostpcm(struct audio *audio)
390 struct audplay_cmd_hpcm_buf_cfg cfg_cmd;
392 dprintk("audevrc_config_hostpcm()\n");
393 cfg_cmd.cmd_id = AUDPLAY_CMD_HPCM_BUF_CFG;
394 cfg_cmd.max_buffers = 1;
395 cfg_cmd.byte_swap = 0;
396 cfg_cmd.hostpcm_config = (0x8000) | (0x4000);
397 cfg_cmd.feedback_frequency = 1;
398 cfg_cmd.partition_number = 0;
399 audplay_send_queue0(audio, &cfg_cmd, sizeof(cfg_cmd));
403 static void audevrc_send_data(struct audio *audio, unsigned needed)
405 struct buffer *frame;
408 spin_lock_irqsave(&audio->dsp_lock, flags);
413 /* We were called from the callback because the DSP
414 * requested more data. Note that the DSP does want
415 * more data, and if a buffer was in-flight, mark it
416 * as available (since the DSP must now be done with
419 audio->out_needed = 1;
420 frame = audio->out + audio->out_tail;
421 if (frame->used == 0xffffffff) {
422 dprintk("frame %d free\n", audio->out_tail);
424 audio->out_tail ^= 1;
425 wake_up(&audio->write_wait);
429 if (audio->out_needed) {
430 /* If the DSP currently wants data and we have a
431 * buffer available, we will send it and reset
432 * the needed flag. We'll mark the buffer as in-flight
433 * so that it won't be recycled until the next buffer
437 frame = audio->out + audio->out_tail;
439 BUG_ON(frame->used == 0xffffffff);
440 dprintk("frame %d busy\n", audio->out_tail);
441 audplay_dsp_send_data_avail(audio, audio->out_tail,
443 frame->used = 0xffffffff;
444 audio->out_needed = 0;
448 spin_unlock_irqrestore(&audio->dsp_lock, flags);
451 /* ------------------- device --------------------- */
453 static void audevrc_flush(struct audio *audio)
455 audio->out[0].used = 0;
456 audio->out[1].used = 0;
460 atomic_set(&audio->out_bytes, 0);
463 static void audevrc_flush_pcm_buf(struct audio *audio)
467 for (index = 0; index < PCM_BUF_MAX_COUNT; index++)
468 audio->in[index].used = 0;
470 audio->read_next = 0;
471 audio->fill_next = 0;
474 static long audevrc_ioctl(struct file *file, unsigned int cmd,
477 struct audio *audio = file->private_data;
480 dprintk("audevrc_ioctl() cmd = %d\n", cmd);
482 if (cmd == AUDIO_GET_STATS) {
483 struct msm_audio_stats stats;
484 stats.byte_count = audpp_avsync_byte_count(audio->dec_id);
485 stats.sample_count = audpp_avsync_sample_count(audio->dec_id);
486 if (copy_to_user((void *)arg, &stats, sizeof(stats)))
490 if (cmd == AUDIO_SET_VOLUME) {
492 spin_lock_irqsave(&audio->dsp_lock, flags);
495 audpp_set_volume_and_pan(audio->dec_id, arg, 0);
496 spin_unlock_irqrestore(&audio->dsp_lock, flags);
499 mutex_lock(&audio->lock);
502 rc = audevrc_enable(audio);
505 rc = audevrc_disable(audio);
508 case AUDIO_SET_CONFIG:{
509 dprintk("AUDIO_SET_CONFIG not applicable \n");
512 case AUDIO_GET_CONFIG:{
513 struct msm_audio_config config;
514 config.buffer_size = BUFSZ;
515 config.buffer_count = 2;
516 config.sample_rate = 8000;
517 config.channel_count = 1;
518 config.unused[0] = 0;
519 config.unused[1] = 0;
520 config.unused[2] = 0;
521 config.unused[3] = 0;
522 if (copy_to_user((void *)arg, &config, sizeof(config)))
528 case AUDIO_GET_PCM_CONFIG:{
529 struct msm_audio_pcm_config config;
530 config.pcm_feedback = 0;
531 config.buffer_count = PCM_BUF_MAX_COUNT;
532 config.buffer_size = PCM_BUFSZ_MIN;
533 if (copy_to_user((void *)arg, &config, sizeof(config)))
539 case AUDIO_SET_PCM_CONFIG:{
540 struct msm_audio_pcm_config config;
542 (&config, (void *)arg, sizeof(config))) {
546 if ((config.buffer_count > PCM_BUF_MAX_COUNT) ||
547 (config.buffer_count == 1))
548 config.buffer_count = PCM_BUF_MAX_COUNT;
550 if (config.buffer_size < PCM_BUFSZ_MIN)
551 config.buffer_size = PCM_BUFSZ_MIN;
553 /* Check if pcm feedback is required */
554 if ((config.pcm_feedback) && (!audio->read_data)) {
555 dprintk("audevrc_ioctl: allocate PCM buf %d\n",
556 config.buffer_count *
559 dma_alloc_coherent(NULL,
564 if (!audio->read_data) {
566 ("audevrc_ioctl: no mem for pcm buf\n");
571 audio->pcm_feedback = 1;
572 audio->buf_refresh = 0;
573 audio->pcm_buf_count =
575 audio->read_next = 0;
576 audio->fill_next = 0;
579 index < config.buffer_count;
581 audio->in[index].data =
582 audio->read_data + offset;
583 audio->in[index].addr =
584 audio->read_phys + offset;
585 audio->in[index].size =
587 audio->in[index].used = 0;
588 offset += config.buffer_size;
598 dprintk("%s: AUDIO_PAUSE %ld\n", __func__, arg);
599 rc = audpp_pause(audio->dec_id, (int) arg);
604 mutex_unlock(&audio->lock);
608 static ssize_t audevrc_read(struct file *file, char __user *buf, size_t count,
611 struct audio *audio = file->private_data;
612 const char __user *start = buf;
614 if (!audio->pcm_feedback) {
616 /* PCM feedback is not enabled. Nothing to read */
618 mutex_lock(&audio->read_lock);
619 dprintk("audevrc_read() \n");
621 rc = wait_event_interruptible(audio->read_wait,
622 (audio->in[audio->read_next].
623 used > 0) || (audio->stopped));
624 dprintk("audevrc_read() wait terminated \n");
627 if (audio->stopped) {
631 if (count < audio->in[audio->read_next].used) {
632 /* Read must happen in frame boundary. Since driver does
633 * not know frame size, read count must be greater or
634 * equal to size of PCM samples
636 dprintk("audevrc_read:read stop - partial frame\n");
639 dprintk("audevrc_read: read from in[%d]\n",
642 (buf, audio->in[audio->read_next].data,
643 audio->in[audio->read_next].used)) {
644 pr_err("audevrc_read: invalid addr %x \n",
649 count -= audio->in[audio->read_next].used;
650 buf += audio->in[audio->read_next].used;
651 audio->in[audio->read_next].used = 0;
652 if ((++audio->read_next) == audio->pcm_buf_count)
653 audio->read_next = 0;
654 if (audio->in[audio->read_next].used == 0)
655 break; /* No data ready at this moment
656 * Exit while loop to prevent
657 * output thread sleep too long
662 if (audio->buf_refresh) {
663 audio->buf_refresh = 0;
664 dprintk("audevrc_read: kick start pcm feedback again\n");
665 audevrc_buffer_refresh(audio);
667 mutex_unlock(&audio->read_lock);
670 dprintk("audevrc_read: read %d bytes\n", rc);
674 static ssize_t audevrc_write(struct file *file, const char __user *buf,
675 size_t count, loff_t *pos)
677 struct audio *audio = file->private_data;
678 const char __user *start = buf;
679 struct buffer *frame;
685 mutex_lock(&audio->write_lock);
686 dprintk("audevrc_write() \n");
688 frame = audio->out + audio->out_head;
689 rc = wait_event_interruptible(audio->write_wait,
691 || (audio->stopped));
694 if (audio->stopped) {
698 xfer = (count > frame->size) ? frame->size : count;
699 if (copy_from_user(frame->data, buf, xfer)) {
705 audio->out_head ^= 1;
709 audevrc_send_data(audio, 0);
712 mutex_unlock(&audio->write_lock);
718 static int audevrc_release(struct inode *inode, struct file *file)
720 struct audio *audio = file->private_data;
722 dprintk("audevrc_release()\n");
724 mutex_lock(&audio->lock);
725 audevrc_disable(audio);
726 audevrc_flush(audio);
727 audevrc_flush_pcm_buf(audio);
728 msm_adsp_put(audio->audplay);
729 audio->audplay = NULL;
731 dma_free_coherent(NULL, DMASZ, audio->data, audio->phys);
733 if (audio->read_data != NULL) {
734 dma_free_coherent(NULL,
735 audio->in[0].size * audio->pcm_buf_count,
736 audio->read_data, audio->read_phys);
737 audio->read_data = NULL;
739 audio->pcm_feedback = 0;
740 mutex_unlock(&audio->lock);
744 static struct audio the_evrc_audio;
746 static int audevrc_open(struct inode *inode, struct file *file)
748 struct audio *audio = &the_evrc_audio;
752 pr_err("audio: busy\n");
757 mutex_lock(&audio->lock);
760 audio->data = dma_alloc_coherent(NULL, DMASZ,
761 &audio->phys, GFP_KERNEL);
763 pr_err("audio: could not allocate DMA buffers\n");
769 rc = audmgr_open(&audio->audmgr);
773 rc = msm_adsp_get("AUDPLAY0TASK", &audio->audplay,
774 &audplay_adsp_ops_evrc, audio);
776 pr_err("audio: failed to get audplay0 dsp module\n");
782 audio->out[0].data = audio->data + 0;
783 audio->out[0].addr = audio->phys + 0;
784 audio->out[0].size = BUFSZ;
786 audio->out[1].data = audio->data + BUFSZ;
787 audio->out[1].addr = audio->phys + BUFSZ;
788 audio->out[1].size = BUFSZ;
790 audio->volume = 0x3FFF;
792 audevrc_flush(audio);
795 file->private_data = audio;
797 mutex_unlock(&audio->lock);
801 audmgr_close(&audio->audmgr);
803 dma_free_coherent(NULL, DMASZ, audio->data, audio->phys);
805 mutex_unlock(&audio->lock);
809 static struct file_operations audio_evrc_fops = {
810 .owner = THIS_MODULE,
811 .open = audevrc_open,
812 .release = audevrc_release,
813 .read = audevrc_read,
814 .write = audevrc_write,
815 .unlocked_ioctl = audevrc_ioctl,
818 struct miscdevice audio_evrc_misc = {
819 .minor = MISC_DYNAMIC_MINOR,
821 .fops = &audio_evrc_fops,
824 static int __init audevrc_init(void)
826 mutex_init(&the_evrc_audio.lock);
827 mutex_init(&the_evrc_audio.write_lock);
828 mutex_init(&the_evrc_audio.read_lock);
829 spin_lock_init(&the_evrc_audio.dsp_lock);
830 init_waitqueue_head(&the_evrc_audio.write_wait);
831 init_waitqueue_head(&the_evrc_audio.read_wait);
832 the_evrc_audio.read_data = NULL;
833 return misc_register(&audio_evrc_misc);
836 static void __exit audevrc_exit(void)
838 misc_deregister(&audio_evrc_misc);
841 module_init(audevrc_init);
842 module_exit(audevrc_exit);
844 MODULE_DESCRIPTION("MSM EVRC driver");
845 MODULE_LICENSE("GPL v2");
846 MODULE_AUTHOR("QUALCOMM Inc");