1 /* arch/arm/mach-msm/qdsp5/audio_out.c
3 * pcm audio output device
5 * Copyright (C) 2008 Google, Inc.
6 * Copyright (C) 2008 HTC Corporation
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
19 #include <linux/module.h>
21 #include <linux/miscdevice.h>
22 #include <linux/uaccess.h>
23 #include <linux/kthread.h>
24 #include <linux/wait.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/debugfs.h>
27 #include <linux/delay.h>
28 #include <linux/wakelock.h>
29 #include <linux/gfp.h>
31 #include <linux/msm_audio.h>
33 #include <asm/atomic.h>
34 #include <asm/ioctls.h>
35 #include <mach/msm_adsp.h>
39 #include <mach/qdsp5/qdsp5audppcmdi.h>
40 #include <mach/qdsp5/qdsp5audppmsg.h>
44 #define LOG_AUDIO_EVENTS 1
45 #define LOG_AUDIO_FAULTS 0
61 #if (LOG_AUDIO_EVENTS != 1)
62 static inline void LOG(unsigned id, unsigned arg) {}
64 static const char *pcm_log_strings[] = {
78 DECLARE_LOG(pcm_log, 64, pcm_log_strings);
80 static int __init _pcm_log_init(void)
82 return ev_log_init(&pcm_log);
84 module_init(_pcm_log_init);
86 #define LOG(id,arg) ev_log_write(&pcm_log, id, arg)
93 #define BUFSZ (960 * 5)
94 #define DMASZ (BUFSZ * 2)
96 #define AUDPP_CMD_CFG_OBJ_UPDATE 0x8000
97 #define AUDPP_CMD_EQ_FLAG_DIS 0x0000
98 #define AUDPP_CMD_EQ_FLAG_ENA -1
99 #define AUDPP_CMD_IIR_FLAG_DIS 0x0000
100 #define AUDPP_CMD_IIR_FLAG_ENA -1
102 #define AUDPP_CMD_IIR_TUNING_FILTER 1
103 #define AUDPP_CMD_EQUALIZER 2
104 #define AUDPP_CMD_ADRC 3
106 #define ADRC_ENABLE 0x0001
107 #define EQ_ENABLE 0x0002
108 #define IIR_ENABLE 0x0004
111 uint16_t compression_th;
112 uint16_t compression_slope;
114 uint16_t attack_const_lsw;
115 uint16_t attack_const_msw;
116 uint16_t release_const_lsw;
117 uint16_t release_const_msw;
118 uint16_t adrc_system_delay;
123 uint16_t eq_params[132];
126 struct rx_iir_filter {
128 uint16_t iir_params[48];
132 audpp_cmd_cfg_object_params_common common;
135 uint16_t eq_params[132];
136 } audpp_cmd_cfg_object_params_eq;
139 audpp_cmd_cfg_object_params_common common;
140 uint16_t active_flag;
142 uint16_t iir_params[48];
143 } audpp_cmd_cfg_object_params_rx_iir;
153 struct buffer out[2];
159 uint8_t out_needed; /* number of buffers the dsp is waiting for */
164 struct mutex write_lock;
165 wait_queue_head_t wait;
167 /* configuration to use on next enable */
168 uint32_t out_sample_rate;
169 uint32_t out_channel_mode;
171 uint32_t out_buffer_size;
173 struct audmgr audmgr;
175 /* data allocated for various buffers */
182 int stopped; /* set when stopped, cleared on flush */
185 struct wake_lock wakelock;
186 struct wake_lock idlelock;
189 struct adrc_filter adrc;
195 struct rx_iir_filter iir;
198 static void audio_prevent_sleep(struct audio *audio)
200 printk(KERN_INFO "++++++++++++++++++++++++++++++\n");
201 wake_lock(&audio->wakelock);
202 wake_lock(&audio->idlelock);
205 static void audio_allow_sleep(struct audio *audio)
207 wake_unlock(&audio->wakelock);
208 wake_unlock(&audio->idlelock);
209 printk(KERN_INFO "------------------------------\n");
212 static int audio_dsp_out_enable(struct audio *audio, int yes);
213 static int audio_dsp_send_buffer(struct audio *audio, unsigned id, unsigned len);
214 static int audio_dsp_set_adrc(struct audio *audio);
215 static int audio_dsp_set_eq(struct audio *audio);
216 static int audio_dsp_set_rx_iir(struct audio *audio);
218 static void audio_dsp_event(void *private, unsigned id, uint16_t *msg);
220 /* must be called with audio->lock held */
221 static int audio_enable(struct audio *audio)
223 struct audmgr_config cfg;
226 pr_info("audio_enable()\n");
231 /* refuse to start if we're not ready */
232 if (!audio->out[0].used || !audio->out[1].used)
235 /* we start buffers 0 and 1, so buffer 0 will be the
236 * next one the dsp will want
239 audio->out_needed = 0;
241 cfg.tx_rate = RPC_AUD_DEF_SAMPLE_RATE_NONE;
242 cfg.rx_rate = RPC_AUD_DEF_SAMPLE_RATE_48000;
243 cfg.def_method = RPC_AUD_DEF_METHOD_HOST_PCM;
244 cfg.codec = RPC_AUD_DEF_CODEC_PCM;
245 cfg.snd_method = RPC_SND_METHOD_MIDI;
247 audio_prevent_sleep(audio);
248 rc = audmgr_enable(&audio->audmgr, &cfg);
250 audio_allow_sleep(audio);
254 if (audpp_enable(-1, audio_dsp_event, audio)) {
255 pr_err("audio: audpp_enable() failed\n");
256 audmgr_disable(&audio->audmgr);
257 audio_allow_sleep(audio);
265 /* must be called with audio->lock held */
266 static int audio_disable(struct audio *audio)
268 pr_info("audio_disable()\n");
269 if (audio->enabled) {
271 audio_dsp_out_enable(audio, 0);
273 audpp_disable(-1, audio);
275 wake_up(&audio->wait);
276 audmgr_disable(&audio->audmgr);
277 audio->out_needed = 0;
278 audio_allow_sleep(audio);
283 /* ------------------- dsp --------------------- */
284 static void audio_dsp_event(void *private, unsigned id, uint16_t *msg)
286 struct audio *audio = private;
287 struct buffer *frame;
290 LOG(EV_DSP_EVENT, id);
292 case AUDPP_MSG_HOST_PCM_INTF_MSG: {
293 unsigned id = msg[2];
294 unsigned idx = msg[3] - 1;
296 /* pr_info("audio_dsp_event: HOST_PCM id %d idx %d\n", id, idx); */
297 if (id != AUDPP_MSG_HOSTPCM_ID_ARM_RX) {
298 pr_err("bogus id\n");
302 pr_err("bogus buffer idx\n");
306 spin_lock_irqsave(&audio->dsp_lock, flags);
307 if (audio->running) {
308 atomic_add(audio->out[idx].used, &audio->out_bytes);
309 audio->out[idx].used = 0;
311 frame = audio->out + audio->out_tail;
313 audio_dsp_send_buffer(
314 audio, audio->out_tail, frame->used);
315 audio->out_tail ^= 1;
319 wake_up(&audio->wait);
321 spin_unlock_irqrestore(&audio->dsp_lock, flags);
324 case AUDPP_MSG_PCMDMAMISSED:
325 pr_info("audio_dsp_event: PCMDMAMISSED %d\n", msg[0]);
327 case AUDPP_MSG_CFG_MSG:
328 if (msg[0] == AUDPP_MSG_ENA_ENA) {
330 pr_info("audio_dsp_event: CFG_MSG ENABLE\n");
331 audio->out_needed = 0;
333 audpp_set_volume_and_pan(5, audio->volume, 0);
334 audio_dsp_set_adrc(audio);
335 audio_dsp_set_eq(audio);
336 audio_dsp_set_rx_iir(audio);
337 audio_dsp_out_enable(audio, 1);
338 } else if (msg[0] == AUDPP_MSG_ENA_DIS) {
340 pr_info("audio_dsp_event: CFG_MSG DISABLE\n");
343 pr_err("audio_dsp_event: CFG_MSG %d?\n", msg[0]);
347 pr_err("audio_dsp_event: UNKNOWN (%d)\n", id);
351 static int audio_dsp_out_enable(struct audio *audio, int yes)
353 audpp_cmd_pcm_intf cmd;
355 memset(&cmd, 0, sizeof(cmd));
356 cmd.cmd_id = AUDPP_CMD_PCM_INTF_2;
357 cmd.object_num = AUDPP_CMD_PCM_INTF_OBJECT_NUM;
358 cmd.config = AUDPP_CMD_PCM_INTF_CONFIG_CMD_V;
359 cmd.intf_type = AUDPP_CMD_PCM_INTF_RX_ENA_ARMTODSP_V;
362 cmd.write_buf1LSW = audio->out[0].addr;
363 cmd.write_buf1MSW = audio->out[0].addr >> 16;
364 cmd.write_buf1_len = audio->out[0].size;
365 cmd.write_buf2LSW = audio->out[1].addr;
366 cmd.write_buf2MSW = audio->out[1].addr >> 16;
367 cmd.write_buf2_len = audio->out[1].size;
368 cmd.arm_to_rx_flag = AUDPP_CMD_PCM_INTF_ENA_V;
369 cmd.weight_decoder_to_rx = audio->out_weight;
370 cmd.weight_arm_to_rx = 1;
371 cmd.partition_number_arm_to_dsp = 0;
372 cmd.sample_rate = audio->out_sample_rate;
373 cmd.channel_mode = audio->out_channel_mode;
376 return audpp_send_queue2(&cmd, sizeof(cmd));
379 static int audio_dsp_send_buffer(struct audio *audio, unsigned idx, unsigned len)
381 audpp_cmd_pcm_intf_send_buffer cmd;
383 cmd.cmd_id = AUDPP_CMD_PCM_INTF_2;
384 cmd.host_pcm_object = AUDPP_CMD_PCM_INTF_OBJECT_NUM;
385 cmd.config = AUDPP_CMD_PCM_INTF_BUFFER_CMD_V;
386 cmd.intf_type = AUDPP_CMD_PCM_INTF_RX_ENA_ARMTODSP_V;
387 cmd.dsp_to_arm_buf_id = 0;
388 cmd.arm_to_dsp_buf_id = idx + 1;
389 cmd.arm_to_dsp_buf_len = len;
391 LOG(EV_SEND_BUFFER, idx);
392 return audpp_send_queue2(&cmd, sizeof(cmd));
395 static int audio_dsp_set_adrc(struct audio *audio)
397 audpp_cmd_cfg_object_params_adrc cmd;
399 memset(&cmd, 0, sizeof(cmd));
400 cmd.common.comman_cfg = AUDPP_CMD_CFG_OBJ_UPDATE;
401 cmd.common.command_type = AUDPP_CMD_ADRC;
403 if (audio->adrc_enable) {
404 cmd.adrc_flag = AUDPP_CMD_ADRC_FLAG_ENA;
405 cmd.compression_th = audio->adrc.compression_th;
406 cmd.compression_slope = audio->adrc.compression_slope;
407 cmd.rms_time = audio->adrc.rms_time;
408 cmd.attack_const_lsw = audio->adrc.attack_const_lsw;
409 cmd.attack_const_msw = audio->adrc.attack_const_msw;
410 cmd.release_const_lsw = audio->adrc.release_const_lsw;
411 cmd.release_const_msw = audio->adrc.release_const_msw;
412 cmd.adrc_system_delay = audio->adrc.adrc_system_delay;
414 cmd.adrc_flag = AUDPP_CMD_ADRC_FLAG_DIS;
416 return audpp_send_queue3(&cmd, sizeof(cmd));
419 static int audio_dsp_set_eq(struct audio *audio)
421 audpp_cmd_cfg_object_params_eq cmd;
423 memset(&cmd, 0, sizeof(cmd));
424 cmd.common.comman_cfg = AUDPP_CMD_CFG_OBJ_UPDATE;
425 cmd.common.command_type = AUDPP_CMD_EQUALIZER;
427 if (audio->eq_enable) {
428 cmd.eq_flag = AUDPP_CMD_EQ_FLAG_ENA;
429 cmd.num_bands = audio->eq.num_bands;
430 memcpy(&cmd.eq_params, audio->eq.eq_params,
431 sizeof(audio->eq.eq_params));
433 cmd.eq_flag = AUDPP_CMD_EQ_FLAG_DIS;
435 return audpp_send_queue3(&cmd, sizeof(cmd));
438 static int audio_dsp_set_rx_iir(struct audio *audio)
440 audpp_cmd_cfg_object_params_rx_iir cmd;
442 memset(&cmd, 0, sizeof(cmd));
443 cmd.common.comman_cfg = AUDPP_CMD_CFG_OBJ_UPDATE;
444 cmd.common.command_type = AUDPP_CMD_IIR_TUNING_FILTER;
446 if (audio->rx_iir_enable) {
447 cmd.active_flag = AUDPP_CMD_IIR_FLAG_ENA;
448 cmd.num_bands = audio->iir.num_bands;
449 memcpy(&cmd.iir_params, audio->iir.iir_params,
450 sizeof(audio->iir.iir_params));
452 cmd.active_flag = AUDPP_CMD_IIR_FLAG_DIS;
455 return audpp_send_queue3(&cmd, sizeof(cmd));
458 /* ------------------- device --------------------- */
460 static int audio_enable_adrc(struct audio *audio, int enable)
462 if (audio->adrc_enable != enable) {
463 audio->adrc_enable = enable;
465 audio_dsp_set_adrc(audio);
470 static int audio_enable_eq(struct audio *audio, int enable)
472 if (audio->eq_enable != enable) {
473 audio->eq_enable = enable;
475 audio_dsp_set_eq(audio);
480 static int audio_enable_rx_iir(struct audio *audio, int enable)
482 if (audio->rx_iir_enable != enable) {
483 audio->rx_iir_enable = enable;
485 audio_dsp_set_rx_iir(audio);
490 static void audio_flush(struct audio *audio)
492 audio->out[0].used = 0;
493 audio->out[1].used = 0;
499 static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
501 struct audio *audio = file->private_data;
504 if (cmd == AUDIO_GET_STATS) {
505 struct msm_audio_stats stats;
506 stats.byte_count = atomic_read(&audio->out_bytes);
507 if (copy_to_user((void*) arg, &stats, sizeof(stats)))
511 if (cmd == AUDIO_SET_VOLUME) {
513 spin_lock_irqsave(&audio->dsp_lock, flags);
516 audpp_set_volume_and_pan(6, arg, 0);
517 spin_unlock_irqrestore(&audio->dsp_lock, flags);
521 mutex_lock(&audio->lock);
524 rc = audio_enable(audio);
527 rc = audio_disable(audio);
531 if (audio->stopped) {
532 /* Make sure we're stopped and we wake any threads
533 * that might be blocked holding the write_lock.
534 * While audio->stopped write threads will always
537 wake_up(&audio->wait);
538 mutex_lock(&audio->write_lock);
540 mutex_unlock(&audio->write_lock);
542 case AUDIO_SET_CONFIG: {
543 struct msm_audio_config config;
544 if (copy_from_user(&config, (void*) arg, sizeof(config))) {
548 if (config.channel_count == 1) {
549 config.channel_count = AUDPP_CMD_PCM_INTF_MONO_V;
550 } else if (config.channel_count == 2) {
551 config.channel_count= AUDPP_CMD_PCM_INTF_STEREO_V;
556 audio->out_sample_rate = config.sample_rate;
557 audio->out_channel_mode = config.channel_count;
561 case AUDIO_GET_CONFIG: {
562 struct msm_audio_config config;
563 config.buffer_size = BUFSZ;
564 config.buffer_count = 2;
565 config.sample_rate = audio->out_sample_rate;
566 if (audio->out_channel_mode == AUDPP_CMD_PCM_INTF_MONO_V) {
567 config.channel_count = 1;
569 config.channel_count = 2;
571 config.unused[0] = 0;
572 config.unused[1] = 0;
573 config.unused[2] = 0;
574 config.unused[3] = 0;
575 if (copy_to_user((void*) arg, &config, sizeof(config))) {
585 mutex_unlock(&audio->lock);
589 static ssize_t audio_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
594 static inline int rt_policy(int policy)
596 if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR))
601 static inline int task_has_rt_policy(struct task_struct *p)
603 return rt_policy(p->policy);
606 static ssize_t audio_write(struct file *file, const char __user *buf,
607 size_t count, loff_t *pos)
609 struct sched_param s = { .sched_priority = 1 };
610 struct audio *audio = file->private_data;
612 const char __user *start = buf;
613 struct buffer *frame;
615 int old_prio = current->rt_priority;
616 int old_policy = current->policy;
617 int cap_nice = cap_raised(current_cap(), CAP_SYS_NICE);
620 LOG(EV_WRITE, count | (audio->running << 28) | (audio->stopped << 24));
622 /* just for this write, set us real-time */
623 if (!task_has_rt_policy(current)) {
624 struct cred *new = prepare_creds();
625 cap_raise(new->cap_effective, CAP_SYS_NICE);
627 sched_setscheduler(current, SCHED_RR, &s);
630 mutex_lock(&audio->write_lock);
632 frame = audio->out + audio->out_head;
634 LOG(EV_WAIT_EVENT, 0);
635 rc = wait_event_interruptible(audio->wait,
636 (frame->used == 0) || (audio->stopped));
637 LOG(EV_WAIT_EVENT, 1);
641 if (audio->stopped) {
645 xfer = count > frame->size ? frame->size : count;
646 if (copy_from_user(frame->data, buf, xfer)) {
651 audio->out_head ^= 1;
655 spin_lock_irqsave(&audio->dsp_lock, flags);
656 LOG(EV_FILL_BUFFER, audio->out_head ^ 1);
657 frame = audio->out + audio->out_tail;
658 if (frame->used && audio->out_needed) {
659 audio_dsp_send_buffer(audio, audio->out_tail, frame->used);
660 audio->out_tail ^= 1;
663 spin_unlock_irqrestore(&audio->dsp_lock, flags);
666 mutex_unlock(&audio->write_lock);
668 /* restore scheduling policy and priority */
669 if (!rt_policy(old_policy)) {
670 struct sched_param v = { .sched_priority = old_prio };
671 sched_setscheduler(current, old_policy, &v);
672 if (likely(!cap_nice)) {
673 struct cred *new = prepare_creds();
674 cap_lower(new->cap_effective, CAP_SYS_NICE);
676 sched_setscheduler(current, SCHED_RR, &s);
680 LOG(EV_RETURN,(buf > start) ? (buf - start) : rc);
686 static int audio_release(struct inode *inode, struct file *file)
688 struct audio *audio = file->private_data;
691 mutex_lock(&audio->lock);
692 audio_disable(audio);
695 mutex_unlock(&audio->lock);
699 static struct audio the_audio;
701 static int audio_open(struct inode *inode, struct file *file)
703 struct audio *audio = &the_audio;
706 mutex_lock(&audio->lock);
709 pr_err("audio: busy\n");
715 audio->data = dma_alloc_coherent(NULL, DMASZ,
716 &audio->phys, GFP_KERNEL);
718 pr_err("audio: could not allocate DMA buffers\n");
724 rc = audmgr_open(&audio->audmgr);
728 audio->out_buffer_size = BUFSZ;
729 audio->out_sample_rate = 44100;
730 audio->out_channel_mode = AUDPP_CMD_PCM_INTF_STEREO_V;
731 audio->out_weight = 100;
733 audio->out[0].data = audio->data + 0;
734 audio->out[0].addr = audio->phys + 0;
735 audio->out[0].size = BUFSZ;
737 audio->out[1].data = audio->data + BUFSZ;
738 audio->out[1].addr = audio->phys + BUFSZ;
739 audio->out[1].size = BUFSZ;
741 audio->volume = 0x2000;
745 file->private_data = audio;
750 mutex_unlock(&audio->lock);
754 static long audpp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
756 struct audio *audio = file->private_data;
758 uint16_t enable_mask;
760 mutex_lock(&audio->lock);
762 case AUDIO_ENABLE_AUDPP:
763 if (copy_from_user(&enable_mask, (void *) arg, sizeof(enable_mask)))
766 enable = (enable_mask & ADRC_ENABLE)? 1 : 0;
767 audio_enable_adrc(audio, enable);
768 enable = (enable_mask & EQ_ENABLE)? 1 : 0;
769 audio_enable_eq(audio, enable);
770 enable = (enable_mask & IIR_ENABLE)? 1 : 0;
771 audio_enable_rx_iir(audio, enable);
775 if (copy_from_user(&audio->adrc, (void*) arg, sizeof(audio->adrc)))
780 if (copy_from_user(&audio->eq, (void*) arg, sizeof(audio->eq)))
784 case AUDIO_SET_RX_IIR:
785 if (copy_from_user(&audio->iir, (void*) arg, sizeof(audio->iir)))
798 mutex_unlock(&audio->lock);
802 static int audpp_open(struct inode *inode, struct file *file)
804 struct audio *audio = &the_audio;
806 file->private_data = audio;
810 static struct file_operations audio_fops = {
811 .owner = THIS_MODULE,
813 .release = audio_release,
815 .write = audio_write,
816 .unlocked_ioctl = audio_ioctl,
819 static struct file_operations audpp_fops = {
820 .owner = THIS_MODULE,
822 .unlocked_ioctl = audpp_ioctl,
825 struct miscdevice audio_misc = {
826 .minor = MISC_DYNAMIC_MINOR,
827 .name = "msm_pcm_out",
831 struct miscdevice audpp_misc = {
832 .minor = MISC_DYNAMIC_MINOR,
833 .name = "msm_pcm_ctl",
837 static int __init audio_init(void)
839 mutex_init(&the_audio.lock);
840 mutex_init(&the_audio.write_lock);
841 spin_lock_init(&the_audio.dsp_lock);
842 init_waitqueue_head(&the_audio.wait);
843 wake_lock_init(&the_audio.wakelock, WAKE_LOCK_SUSPEND, "audio_pcm");
844 wake_lock_init(&the_audio.idlelock, WAKE_LOCK_IDLE, "audio_pcm_idle");
845 return (misc_register(&audio_misc) || misc_register(&audpp_misc));
848 device_initcall(audio_init);