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>
30 #include <linux/msm_audio.h>
32 #include <asm/atomic.h>
33 #include <asm/ioctls.h>
34 #include <mach/msm_adsp.h>
38 #include <mach/qdsp5/qdsp5audppcmdi.h>
39 #include <mach/qdsp5/qdsp5audppmsg.h>
41 #include <mach/htc_pwrsink.h>
45 #define LOG_AUDIO_EVENTS 1
46 #define LOG_AUDIO_FAULTS 0
62 #if (LOG_AUDIO_EVENTS != 1)
63 static inline void LOG(unsigned id, unsigned arg) {}
65 static const char *pcm_log_strings[] = {
79 DECLARE_LOG(pcm_log, 64, pcm_log_strings);
81 static int __init _pcm_log_init(void)
83 return ev_log_init(&pcm_log);
85 module_init(_pcm_log_init);
87 #define LOG(id,arg) ev_log_write(&pcm_log, id, arg)
94 #define BUFSZ (960 * 5)
95 #define DMASZ (BUFSZ * 2)
97 #define AUDPP_CMD_CFG_OBJ_UPDATE 0x8000
98 #define AUDPP_CMD_EQ_FLAG_DIS 0x0000
99 #define AUDPP_CMD_EQ_FLAG_ENA -1
100 #define AUDPP_CMD_IIR_FLAG_DIS 0x0000
101 #define AUDPP_CMD_IIR_FLAG_ENA -1
103 #define AUDPP_CMD_IIR_TUNING_FILTER 1
104 #define AUDPP_CMD_EQUALIZER 2
105 #define AUDPP_CMD_ADRC 3
107 #define ADRC_ENABLE 0x0001
108 #define EQ_ENABLE 0x0002
109 #define IIR_ENABLE 0x0004
112 uint16_t compression_th;
113 uint16_t compression_slope;
115 uint16_t attack_const_lsw;
116 uint16_t attack_const_msw;
117 uint16_t release_const_lsw;
118 uint16_t release_const_msw;
119 uint16_t adrc_system_delay;
124 uint16_t eq_params[132];
127 struct rx_iir_filter {
129 uint16_t iir_params[48];
133 audpp_cmd_cfg_object_params_common common;
136 uint16_t eq_params[132];
137 } audpp_cmd_cfg_object_params_eq;
140 audpp_cmd_cfg_object_params_common common;
141 uint16_t active_flag;
143 uint16_t iir_params[48];
144 } audpp_cmd_cfg_object_params_rx_iir;
154 struct buffer out[2];
160 uint8_t out_needed; /* number of buffers the dsp is waiting for */
165 struct mutex write_lock;
166 wait_queue_head_t wait;
168 /* configuration to use on next enable */
169 uint32_t out_sample_rate;
170 uint32_t out_channel_mode;
172 uint32_t out_buffer_size;
174 struct audmgr audmgr;
176 /* data allocated for various buffers */
183 int stopped; /* set when stopped, cleared on flush */
186 struct wake_lock wakelock;
187 struct wake_lock idlelock;
190 struct adrc_filter adrc;
196 struct rx_iir_filter iir;
199 static void audio_prevent_sleep(struct audio *audio)
201 printk(KERN_INFO "++++++++++++++++++++++++++++++\n");
202 wake_lock(&audio->wakelock);
203 wake_lock(&audio->idlelock);
206 static void audio_allow_sleep(struct audio *audio)
208 wake_unlock(&audio->wakelock);
209 wake_unlock(&audio->idlelock);
210 printk(KERN_INFO "------------------------------\n");
213 static int audio_dsp_out_enable(struct audio *audio, int yes);
214 static int audio_dsp_send_buffer(struct audio *audio, unsigned id, unsigned len);
215 static int audio_dsp_set_adrc(struct audio *audio);
216 static int audio_dsp_set_eq(struct audio *audio);
217 static int audio_dsp_set_rx_iir(struct audio *audio);
219 static void audio_dsp_event(void *private, unsigned id, uint16_t *msg);
221 /* must be called with audio->lock held */
222 static int audio_enable(struct audio *audio)
224 struct audmgr_config cfg;
227 pr_info("audio_enable()\n");
232 /* refuse to start if we're not ready */
233 if (!audio->out[0].used || !audio->out[1].used)
236 /* we start buffers 0 and 1, so buffer 0 will be the
237 * next one the dsp will want
240 audio->out_needed = 0;
242 cfg.tx_rate = RPC_AUD_DEF_SAMPLE_RATE_NONE;
243 cfg.rx_rate = RPC_AUD_DEF_SAMPLE_RATE_48000;
244 cfg.def_method = RPC_AUD_DEF_METHOD_HOST_PCM;
245 cfg.codec = RPC_AUD_DEF_CODEC_PCM;
246 cfg.snd_method = RPC_SND_METHOD_MIDI;
248 audio_prevent_sleep(audio);
249 rc = audmgr_enable(&audio->audmgr, &cfg);
251 audio_allow_sleep(audio);
255 if (audpp_enable(-1, audio_dsp_event, audio)) {
256 pr_err("audio: audpp_enable() failed\n");
257 audmgr_disable(&audio->audmgr);
258 audio_allow_sleep(audio);
263 htc_pwrsink_set(PWRSINK_AUDIO, 100);
267 /* must be called with audio->lock held */
268 static int audio_disable(struct audio *audio)
270 pr_info("audio_disable()\n");
271 if (audio->enabled) {
273 audio_dsp_out_enable(audio, 0);
275 audpp_disable(-1, audio);
277 wake_up(&audio->wait);
278 audmgr_disable(&audio->audmgr);
279 audio->out_needed = 0;
280 audio_allow_sleep(audio);
285 /* ------------------- dsp --------------------- */
286 static void audio_dsp_event(void *private, unsigned id, uint16_t *msg)
288 struct audio *audio = private;
289 struct buffer *frame;
292 LOG(EV_DSP_EVENT, id);
294 case AUDPP_MSG_HOST_PCM_INTF_MSG: {
295 unsigned id = msg[2];
296 unsigned idx = msg[3] - 1;
298 /* pr_info("audio_dsp_event: HOST_PCM id %d idx %d\n", id, idx); */
299 if (id != AUDPP_MSG_HOSTPCM_ID_ARM_RX) {
300 pr_err("bogus id\n");
304 pr_err("bogus buffer idx\n");
308 spin_lock_irqsave(&audio->dsp_lock, flags);
309 if (audio->running) {
310 atomic_add(audio->out[idx].used, &audio->out_bytes);
311 audio->out[idx].used = 0;
313 frame = audio->out + audio->out_tail;
315 audio_dsp_send_buffer(
316 audio, audio->out_tail, frame->used);
317 audio->out_tail ^= 1;
321 wake_up(&audio->wait);
323 spin_unlock_irqrestore(&audio->dsp_lock, flags);
326 case AUDPP_MSG_PCMDMAMISSED:
327 pr_info("audio_dsp_event: PCMDMAMISSED %d\n", msg[0]);
329 case AUDPP_MSG_CFG_MSG:
330 if (msg[0] == AUDPP_MSG_ENA_ENA) {
332 pr_info("audio_dsp_event: CFG_MSG ENABLE\n");
333 audio->out_needed = 0;
335 audpp_set_volume_and_pan(5, audio->volume, 0);
336 audio_dsp_set_adrc(audio);
337 audio_dsp_set_eq(audio);
338 audio_dsp_set_rx_iir(audio);
339 audio_dsp_out_enable(audio, 1);
340 } else if (msg[0] == AUDPP_MSG_ENA_DIS) {
342 pr_info("audio_dsp_event: CFG_MSG DISABLE\n");
345 pr_err("audio_dsp_event: CFG_MSG %d?\n", msg[0]);
349 pr_err("audio_dsp_event: UNKNOWN (%d)\n", id);
353 static int audio_dsp_out_enable(struct audio *audio, int yes)
355 audpp_cmd_pcm_intf cmd;
357 memset(&cmd, 0, sizeof(cmd));
358 cmd.cmd_id = AUDPP_CMD_PCM_INTF_2;
359 cmd.object_num = AUDPP_CMD_PCM_INTF_OBJECT_NUM;
360 cmd.config = AUDPP_CMD_PCM_INTF_CONFIG_CMD_V;
361 cmd.intf_type = AUDPP_CMD_PCM_INTF_RX_ENA_ARMTODSP_V;
364 cmd.write_buf1LSW = audio->out[0].addr;
365 cmd.write_buf1MSW = audio->out[0].addr >> 16;
366 cmd.write_buf1_len = audio->out[0].size;
367 cmd.write_buf2LSW = audio->out[1].addr;
368 cmd.write_buf2MSW = audio->out[1].addr >> 16;
369 cmd.write_buf2_len = audio->out[1].size;
370 cmd.arm_to_rx_flag = AUDPP_CMD_PCM_INTF_ENA_V;
371 cmd.weight_decoder_to_rx = audio->out_weight;
372 cmd.weight_arm_to_rx = 1;
373 cmd.partition_number_arm_to_dsp = 0;
374 cmd.sample_rate = audio->out_sample_rate;
375 cmd.channel_mode = audio->out_channel_mode;
378 return audpp_send_queue2(&cmd, sizeof(cmd));
381 static int audio_dsp_send_buffer(struct audio *audio, unsigned idx, unsigned len)
383 audpp_cmd_pcm_intf_send_buffer cmd;
385 cmd.cmd_id = AUDPP_CMD_PCM_INTF_2;
386 cmd.host_pcm_object = AUDPP_CMD_PCM_INTF_OBJECT_NUM;
387 cmd.config = AUDPP_CMD_PCM_INTF_BUFFER_CMD_V;
388 cmd.intf_type = AUDPP_CMD_PCM_INTF_RX_ENA_ARMTODSP_V;
389 cmd.dsp_to_arm_buf_id = 0;
390 cmd.arm_to_dsp_buf_id = idx + 1;
391 cmd.arm_to_dsp_buf_len = len;
393 LOG(EV_SEND_BUFFER, idx);
394 return audpp_send_queue2(&cmd, sizeof(cmd));
397 static int audio_dsp_set_adrc(struct audio *audio)
399 audpp_cmd_cfg_object_params_adrc cmd;
401 memset(&cmd, 0, sizeof(cmd));
402 cmd.common.comman_cfg = AUDPP_CMD_CFG_OBJ_UPDATE;
403 cmd.common.command_type = AUDPP_CMD_ADRC;
405 if (audio->adrc_enable) {
406 cmd.adrc_flag = AUDPP_CMD_ADRC_FLAG_ENA;
407 cmd.compression_th = audio->adrc.compression_th;
408 cmd.compression_slope = audio->adrc.compression_slope;
409 cmd.rms_time = audio->adrc.rms_time;
410 cmd.attack_const_lsw = audio->adrc.attack_const_lsw;
411 cmd.attack_const_msw = audio->adrc.attack_const_msw;
412 cmd.release_const_lsw = audio->adrc.release_const_lsw;
413 cmd.release_const_msw = audio->adrc.release_const_msw;
414 cmd.adrc_system_delay = audio->adrc.adrc_system_delay;
416 cmd.adrc_flag = AUDPP_CMD_ADRC_FLAG_DIS;
418 return audpp_send_queue3(&cmd, sizeof(cmd));
421 static int audio_dsp_set_eq(struct audio *audio)
423 audpp_cmd_cfg_object_params_eq cmd;
425 memset(&cmd, 0, sizeof(cmd));
426 cmd.common.comman_cfg = AUDPP_CMD_CFG_OBJ_UPDATE;
427 cmd.common.command_type = AUDPP_CMD_EQUALIZER;
429 if (audio->eq_enable) {
430 cmd.eq_flag = AUDPP_CMD_EQ_FLAG_ENA;
431 cmd.num_bands = audio->eq.num_bands;
432 memcpy(&cmd.eq_params, audio->eq.eq_params,
433 sizeof(audio->eq.eq_params));
435 cmd.eq_flag = AUDPP_CMD_EQ_FLAG_DIS;
437 return audpp_send_queue3(&cmd, sizeof(cmd));
440 static int audio_dsp_set_rx_iir(struct audio *audio)
442 audpp_cmd_cfg_object_params_rx_iir cmd;
444 memset(&cmd, 0, sizeof(cmd));
445 cmd.common.comman_cfg = AUDPP_CMD_CFG_OBJ_UPDATE;
446 cmd.common.command_type = AUDPP_CMD_IIR_TUNING_FILTER;
448 if (audio->rx_iir_enable) {
449 cmd.active_flag = AUDPP_CMD_IIR_FLAG_ENA;
450 cmd.num_bands = audio->iir.num_bands;
451 memcpy(&cmd.iir_params, audio->iir.iir_params,
452 sizeof(audio->iir.iir_params));
454 cmd.active_flag = AUDPP_CMD_IIR_FLAG_DIS;
457 return audpp_send_queue3(&cmd, sizeof(cmd));
460 /* ------------------- device --------------------- */
462 static int audio_enable_adrc(struct audio *audio, int enable)
464 if (audio->adrc_enable != enable) {
465 audio->adrc_enable = enable;
467 audio_dsp_set_adrc(audio);
472 static int audio_enable_eq(struct audio *audio, int enable)
474 if (audio->eq_enable != enable) {
475 audio->eq_enable = enable;
477 audio_dsp_set_eq(audio);
482 static int audio_enable_rx_iir(struct audio *audio, int enable)
484 if (audio->rx_iir_enable != enable) {
485 audio->rx_iir_enable = enable;
487 audio_dsp_set_rx_iir(audio);
492 static void audio_flush(struct audio *audio)
494 audio->out[0].used = 0;
495 audio->out[1].used = 0;
501 static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
503 struct audio *audio = file->private_data;
506 if (cmd == AUDIO_GET_STATS) {
507 struct msm_audio_stats stats;
508 stats.byte_count = atomic_read(&audio->out_bytes);
509 if (copy_to_user((void*) arg, &stats, sizeof(stats)))
513 if (cmd == AUDIO_SET_VOLUME) {
515 spin_lock_irqsave(&audio->dsp_lock, flags);
518 audpp_set_volume_and_pan(6, arg, 0);
519 spin_unlock_irqrestore(&audio->dsp_lock, flags);
523 mutex_lock(&audio->lock);
526 rc = audio_enable(audio);
529 rc = audio_disable(audio);
533 if (audio->stopped) {
534 /* Make sure we're stopped and we wake any threads
535 * that might be blocked holding the write_lock.
536 * While audio->stopped write threads will always
539 wake_up(&audio->wait);
540 mutex_lock(&audio->write_lock);
542 mutex_unlock(&audio->write_lock);
544 case AUDIO_SET_CONFIG: {
545 struct msm_audio_config config;
546 if (copy_from_user(&config, (void*) arg, sizeof(config))) {
550 if (config.channel_count == 1) {
551 config.channel_count = AUDPP_CMD_PCM_INTF_MONO_V;
552 } else if (config.channel_count == 2) {
553 config.channel_count= AUDPP_CMD_PCM_INTF_STEREO_V;
558 audio->out_sample_rate = config.sample_rate;
559 audio->out_channel_mode = config.channel_count;
563 case AUDIO_GET_CONFIG: {
564 struct msm_audio_config config;
565 config.buffer_size = BUFSZ;
566 config.buffer_count = 2;
567 config.sample_rate = audio->out_sample_rate;
568 if (audio->out_channel_mode == AUDPP_CMD_PCM_INTF_MONO_V) {
569 config.channel_count = 1;
571 config.channel_count = 2;
573 config.unused[0] = 0;
574 config.unused[1] = 0;
575 config.unused[2] = 0;
576 config.unused[3] = 0;
577 if (copy_to_user((void*) arg, &config, sizeof(config))) {
587 mutex_unlock(&audio->lock);
591 static ssize_t audio_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
596 static inline int rt_policy(int policy)
598 if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR))
603 static inline int task_has_rt_policy(struct task_struct *p)
605 return rt_policy(p->policy);
608 static ssize_t audio_write(struct file *file, const char __user *buf,
609 size_t count, loff_t *pos)
611 struct sched_param s = { .sched_priority = 1 };
612 struct audio *audio = file->private_data;
614 const char __user *start = buf;
615 struct buffer *frame;
617 int old_prio = current->rt_priority;
618 int old_policy = current->policy;
619 int cap_nice = cap_raised(current_cap(), CAP_SYS_NICE);
622 LOG(EV_WRITE, count | (audio->running << 28) | (audio->stopped << 24));
624 /* just for this write, set us real-time */
625 if (!task_has_rt_policy(current)) {
626 struct cred *new = prepare_creds();
627 cap_raise(new->cap_effective, CAP_SYS_NICE);
629 sched_setscheduler(current, SCHED_RR, &s);
632 mutex_lock(&audio->write_lock);
634 frame = audio->out + audio->out_head;
636 LOG(EV_WAIT_EVENT, 0);
637 rc = wait_event_interruptible(audio->wait,
638 (frame->used == 0) || (audio->stopped));
639 LOG(EV_WAIT_EVENT, 1);
643 if (audio->stopped) {
647 xfer = count > frame->size ? frame->size : count;
648 if (copy_from_user(frame->data, buf, xfer)) {
653 audio->out_head ^= 1;
657 spin_lock_irqsave(&audio->dsp_lock, flags);
658 LOG(EV_FILL_BUFFER, audio->out_head ^ 1);
659 frame = audio->out + audio->out_tail;
660 if (frame->used && audio->out_needed) {
661 audio_dsp_send_buffer(audio, audio->out_tail, frame->used);
662 audio->out_tail ^= 1;
665 spin_unlock_irqrestore(&audio->dsp_lock, flags);
668 mutex_unlock(&audio->write_lock);
670 /* restore scheduling policy and priority */
671 if (!rt_policy(old_policy)) {
672 struct sched_param v = { .sched_priority = old_prio };
673 sched_setscheduler(current, old_policy, &v);
674 if (likely(!cap_nice)) {
675 struct cred *new = prepare_creds();
676 cap_lower(new->cap_effective, CAP_SYS_NICE);
678 sched_setscheduler(current, SCHED_RR, &s);
682 LOG(EV_RETURN,(buf > start) ? (buf - start) : rc);
688 static int audio_release(struct inode *inode, struct file *file)
690 struct audio *audio = file->private_data;
693 mutex_lock(&audio->lock);
694 audio_disable(audio);
697 mutex_unlock(&audio->lock);
698 htc_pwrsink_set(PWRSINK_AUDIO, 0);
702 static struct audio the_audio;
704 static int audio_open(struct inode *inode, struct file *file)
706 struct audio *audio = &the_audio;
709 mutex_lock(&audio->lock);
712 pr_err("audio: busy\n");
718 audio->data = dma_alloc_coherent(NULL, DMASZ,
719 &audio->phys, GFP_KERNEL);
721 pr_err("audio: could not allocate DMA buffers\n");
727 rc = audmgr_open(&audio->audmgr);
731 audio->out_buffer_size = BUFSZ;
732 audio->out_sample_rate = 44100;
733 audio->out_channel_mode = AUDPP_CMD_PCM_INTF_STEREO_V;
734 audio->out_weight = 100;
736 audio->out[0].data = audio->data + 0;
737 audio->out[0].addr = audio->phys + 0;
738 audio->out[0].size = BUFSZ;
740 audio->out[1].data = audio->data + BUFSZ;
741 audio->out[1].addr = audio->phys + BUFSZ;
742 audio->out[1].size = BUFSZ;
744 audio->volume = 0x2000;
748 file->private_data = audio;
753 mutex_unlock(&audio->lock);
757 static long audpp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
759 struct audio *audio = file->private_data;
761 uint16_t enable_mask;
763 mutex_lock(&audio->lock);
765 case AUDIO_ENABLE_AUDPP:
766 if (copy_from_user(&enable_mask, (void *) arg, sizeof(enable_mask)))
769 enable = (enable_mask & ADRC_ENABLE)? 1 : 0;
770 audio_enable_adrc(audio, enable);
771 enable = (enable_mask & EQ_ENABLE)? 1 : 0;
772 audio_enable_eq(audio, enable);
773 enable = (enable_mask & IIR_ENABLE)? 1 : 0;
774 audio_enable_rx_iir(audio, enable);
778 if (copy_from_user(&audio->adrc, (void*) arg, sizeof(audio->adrc)))
783 if (copy_from_user(&audio->eq, (void*) arg, sizeof(audio->eq)))
787 case AUDIO_SET_RX_IIR:
788 if (copy_from_user(&audio->iir, (void*) arg, sizeof(audio->iir)))
801 mutex_unlock(&audio->lock);
805 static int audpp_open(struct inode *inode, struct file *file)
807 struct audio *audio = &the_audio;
809 file->private_data = audio;
813 static struct file_operations audio_fops = {
814 .owner = THIS_MODULE,
816 .release = audio_release,
818 .write = audio_write,
819 .unlocked_ioctl = audio_ioctl,
822 static struct file_operations audpp_fops = {
823 .owner = THIS_MODULE,
825 .unlocked_ioctl = audpp_ioctl,
828 struct miscdevice audio_misc = {
829 .minor = MISC_DYNAMIC_MINOR,
830 .name = "msm_pcm_out",
834 struct miscdevice audpp_misc = {
835 .minor = MISC_DYNAMIC_MINOR,
836 .name = "msm_pcm_ctl",
840 static int __init audio_init(void)
842 mutex_init(&the_audio.lock);
843 mutex_init(&the_audio.write_lock);
844 spin_lock_init(&the_audio.dsp_lock);
845 init_waitqueue_head(&the_audio.wait);
846 wake_lock_init(&the_audio.wakelock, WAKE_LOCK_SUSPEND, "audio_pcm");
847 wake_lock_init(&the_audio.idlelock, WAKE_LOCK_IDLE, "audio_pcm_idle");
848 return (misc_register(&audio_misc) || misc_register(&audpp_misc));
851 device_initcall(audio_init);