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>
43 #define LOG_AUDIO_EVENTS 1
44 #define LOG_AUDIO_FAULTS 0
60 #if (LOG_AUDIO_EVENTS != 1)
61 static inline void LOG(unsigned id, unsigned arg) {}
63 static const char *pcm_log_strings[] = {
77 DECLARE_LOG(pcm_log, 64, pcm_log_strings);
79 static int __init _pcm_log_init(void)
81 return ev_log_init(&pcm_log);
83 module_init(_pcm_log_init);
85 #define LOG(id,arg) ev_log_write(&pcm_log, id, arg)
92 #define BUFSZ (960 * 5)
93 #define DMASZ (BUFSZ * 2)
95 #define AUDPP_CMD_CFG_OBJ_UPDATE 0x8000
96 #define AUDPP_CMD_EQ_FLAG_DIS 0x0000
97 #define AUDPP_CMD_EQ_FLAG_ENA -1
98 #define AUDPP_CMD_IIR_FLAG_DIS 0x0000
99 #define AUDPP_CMD_IIR_FLAG_ENA -1
101 #define AUDPP_CMD_IIR_TUNING_FILTER 1
102 #define AUDPP_CMD_EQUALIZER 2
103 #define AUDPP_CMD_ADRC 3
105 #define ADRC_ENABLE 0x0001
106 #define EQ_ENABLE 0x0002
107 #define IIR_ENABLE 0x0004
110 uint16_t compression_th;
111 uint16_t compression_slope;
113 uint16_t attack_const_lsw;
114 uint16_t attack_const_msw;
115 uint16_t release_const_lsw;
116 uint16_t release_const_msw;
117 uint16_t adrc_system_delay;
122 uint16_t eq_params[132];
125 struct rx_iir_filter {
127 uint16_t iir_params[48];
131 audpp_cmd_cfg_object_params_common common;
134 uint16_t eq_params[132];
135 } audpp_cmd_cfg_object_params_eq;
138 audpp_cmd_cfg_object_params_common common;
139 uint16_t active_flag;
141 uint16_t iir_params[48];
142 } audpp_cmd_cfg_object_params_rx_iir;
152 struct buffer out[2];
158 uint8_t out_needed; /* number of buffers the dsp is waiting for */
163 struct mutex write_lock;
164 wait_queue_head_t wait;
166 /* configuration to use on next enable */
167 uint32_t out_sample_rate;
168 uint32_t out_channel_mode;
170 uint32_t out_buffer_size;
172 struct audmgr audmgr;
174 /* data allocated for various buffers */
181 int stopped; /* set when stopped, cleared on flush */
184 struct wake_lock wakelock;
185 struct wake_lock idlelock;
188 struct adrc_filter adrc;
194 struct rx_iir_filter iir;
197 static void audio_prevent_sleep(struct audio *audio)
199 printk(KERN_INFO "++++++++++++++++++++++++++++++\n");
200 wake_lock(&audio->wakelock);
201 wake_lock(&audio->idlelock);
204 static void audio_allow_sleep(struct audio *audio)
206 wake_unlock(&audio->wakelock);
207 wake_unlock(&audio->idlelock);
208 printk(KERN_INFO "------------------------------\n");
211 static int audio_dsp_out_enable(struct audio *audio, int yes);
212 static int audio_dsp_send_buffer(struct audio *audio, unsigned id, unsigned len);
213 static int audio_dsp_set_adrc(struct audio *audio);
214 static int audio_dsp_set_eq(struct audio *audio);
215 static int audio_dsp_set_rx_iir(struct audio *audio);
217 static void audio_dsp_event(void *private, unsigned id, uint16_t *msg);
219 /* must be called with audio->lock held */
220 static int audio_enable(struct audio *audio)
222 struct audmgr_config cfg;
225 pr_info("audio_enable()\n");
230 /* refuse to start if we're not ready */
231 if (!audio->out[0].used || !audio->out[1].used)
234 /* we start buffers 0 and 1, so buffer 0 will be the
235 * next one the dsp will want
238 audio->out_needed = 0;
240 cfg.tx_rate = RPC_AUD_DEF_SAMPLE_RATE_NONE;
241 cfg.rx_rate = RPC_AUD_DEF_SAMPLE_RATE_48000;
242 cfg.def_method = RPC_AUD_DEF_METHOD_HOST_PCM;
243 cfg.codec = RPC_AUD_DEF_CODEC_PCM;
244 cfg.snd_method = RPC_SND_METHOD_MIDI;
246 audio_prevent_sleep(audio);
247 rc = audmgr_enable(&audio->audmgr, &cfg);
249 audio_allow_sleep(audio);
253 if (audpp_enable(-1, audio_dsp_event, audio)) {
254 pr_err("audio: audpp_enable() failed\n");
255 audmgr_disable(&audio->audmgr);
256 audio_allow_sleep(audio);
264 /* must be called with audio->lock held */
265 static int audio_disable(struct audio *audio)
267 pr_info("audio_disable()\n");
268 if (audio->enabled) {
270 audio_dsp_out_enable(audio, 0);
272 audpp_disable(-1, audio);
274 wake_up(&audio->wait);
275 audmgr_disable(&audio->audmgr);
276 audio->out_needed = 0;
277 audio_allow_sleep(audio);
282 /* ------------------- dsp --------------------- */
283 static void audio_dsp_event(void *private, unsigned id, uint16_t *msg)
285 struct audio *audio = private;
286 struct buffer *frame;
289 LOG(EV_DSP_EVENT, id);
291 case AUDPP_MSG_HOST_PCM_INTF_MSG: {
292 unsigned id = msg[2];
293 unsigned idx = msg[3] - 1;
295 /* pr_info("audio_dsp_event: HOST_PCM id %d idx %d\n", id, idx); */
296 if (id != AUDPP_MSG_HOSTPCM_ID_ARM_RX) {
297 pr_err("bogus id\n");
301 pr_err("bogus buffer idx\n");
305 spin_lock_irqsave(&audio->dsp_lock, flags);
306 if (audio->running) {
307 atomic_add(audio->out[idx].used, &audio->out_bytes);
308 audio->out[idx].used = 0;
310 frame = audio->out + audio->out_tail;
312 audio_dsp_send_buffer(
313 audio, audio->out_tail, frame->used);
314 audio->out_tail ^= 1;
318 wake_up(&audio->wait);
320 spin_unlock_irqrestore(&audio->dsp_lock, flags);
323 case AUDPP_MSG_PCMDMAMISSED:
324 pr_info("audio_dsp_event: PCMDMAMISSED %d\n", msg[0]);
326 case AUDPP_MSG_CFG_MSG:
327 if (msg[0] == AUDPP_MSG_ENA_ENA) {
329 pr_info("audio_dsp_event: CFG_MSG ENABLE\n");
330 audio->out_needed = 0;
332 audpp_set_volume_and_pan(5, audio->volume, 0);
333 audio_dsp_set_adrc(audio);
334 audio_dsp_set_eq(audio);
335 audio_dsp_set_rx_iir(audio);
336 audio_dsp_out_enable(audio, 1);
337 } else if (msg[0] == AUDPP_MSG_ENA_DIS) {
339 pr_info("audio_dsp_event: CFG_MSG DISABLE\n");
342 pr_err("audio_dsp_event: CFG_MSG %d?\n", msg[0]);
346 pr_err("audio_dsp_event: UNKNOWN (%d)\n", id);
350 static int audio_dsp_out_enable(struct audio *audio, int yes)
352 audpp_cmd_pcm_intf cmd;
354 memset(&cmd, 0, sizeof(cmd));
355 cmd.cmd_id = AUDPP_CMD_PCM_INTF_2;
356 cmd.object_num = AUDPP_CMD_PCM_INTF_OBJECT_NUM;
357 cmd.config = AUDPP_CMD_PCM_INTF_CONFIG_CMD_V;
358 cmd.intf_type = AUDPP_CMD_PCM_INTF_RX_ENA_ARMTODSP_V;
361 cmd.write_buf1LSW = audio->out[0].addr;
362 cmd.write_buf1MSW = audio->out[0].addr >> 16;
363 cmd.write_buf1_len = audio->out[0].size;
364 cmd.write_buf2LSW = audio->out[1].addr;
365 cmd.write_buf2MSW = audio->out[1].addr >> 16;
366 cmd.write_buf2_len = audio->out[1].size;
367 cmd.arm_to_rx_flag = AUDPP_CMD_PCM_INTF_ENA_V;
368 cmd.weight_decoder_to_rx = audio->out_weight;
369 cmd.weight_arm_to_rx = 1;
370 cmd.partition_number_arm_to_dsp = 0;
371 cmd.sample_rate = audio->out_sample_rate;
372 cmd.channel_mode = audio->out_channel_mode;
375 return audpp_send_queue2(&cmd, sizeof(cmd));
378 static int audio_dsp_send_buffer(struct audio *audio, unsigned idx, unsigned len)
380 audpp_cmd_pcm_intf_send_buffer cmd;
382 cmd.cmd_id = AUDPP_CMD_PCM_INTF_2;
383 cmd.host_pcm_object = AUDPP_CMD_PCM_INTF_OBJECT_NUM;
384 cmd.config = AUDPP_CMD_PCM_INTF_BUFFER_CMD_V;
385 cmd.intf_type = AUDPP_CMD_PCM_INTF_RX_ENA_ARMTODSP_V;
386 cmd.dsp_to_arm_buf_id = 0;
387 cmd.arm_to_dsp_buf_id = idx + 1;
388 cmd.arm_to_dsp_buf_len = len;
390 LOG(EV_SEND_BUFFER, idx);
391 return audpp_send_queue2(&cmd, sizeof(cmd));
394 static int audio_dsp_set_adrc(struct audio *audio)
396 audpp_cmd_cfg_object_params_adrc cmd;
398 memset(&cmd, 0, sizeof(cmd));
399 cmd.common.comman_cfg = AUDPP_CMD_CFG_OBJ_UPDATE;
400 cmd.common.command_type = AUDPP_CMD_ADRC;
402 if (audio->adrc_enable) {
403 cmd.adrc_flag = AUDPP_CMD_ADRC_FLAG_ENA;
404 cmd.compression_th = audio->adrc.compression_th;
405 cmd.compression_slope = audio->adrc.compression_slope;
406 cmd.rms_time = audio->adrc.rms_time;
407 cmd.attack_const_lsw = audio->adrc.attack_const_lsw;
408 cmd.attack_const_msw = audio->adrc.attack_const_msw;
409 cmd.release_const_lsw = audio->adrc.release_const_lsw;
410 cmd.release_const_msw = audio->adrc.release_const_msw;
411 cmd.adrc_system_delay = audio->adrc.adrc_system_delay;
413 cmd.adrc_flag = AUDPP_CMD_ADRC_FLAG_DIS;
415 return audpp_send_queue3(&cmd, sizeof(cmd));
418 static int audio_dsp_set_eq(struct audio *audio)
420 audpp_cmd_cfg_object_params_eq cmd;
422 memset(&cmd, 0, sizeof(cmd));
423 cmd.common.comman_cfg = AUDPP_CMD_CFG_OBJ_UPDATE;
424 cmd.common.command_type = AUDPP_CMD_EQUALIZER;
426 if (audio->eq_enable) {
427 cmd.eq_flag = AUDPP_CMD_EQ_FLAG_ENA;
428 cmd.num_bands = audio->eq.num_bands;
429 memcpy(&cmd.eq_params, audio->eq.eq_params,
430 sizeof(audio->eq.eq_params));
432 cmd.eq_flag = AUDPP_CMD_EQ_FLAG_DIS;
434 return audpp_send_queue3(&cmd, sizeof(cmd));
437 static int audio_dsp_set_rx_iir(struct audio *audio)
439 audpp_cmd_cfg_object_params_rx_iir cmd;
441 memset(&cmd, 0, sizeof(cmd));
442 cmd.common.comman_cfg = AUDPP_CMD_CFG_OBJ_UPDATE;
443 cmd.common.command_type = AUDPP_CMD_IIR_TUNING_FILTER;
445 if (audio->rx_iir_enable) {
446 cmd.active_flag = AUDPP_CMD_IIR_FLAG_ENA;
447 cmd.num_bands = audio->iir.num_bands;
448 memcpy(&cmd.iir_params, audio->iir.iir_params,
449 sizeof(audio->iir.iir_params));
451 cmd.active_flag = AUDPP_CMD_IIR_FLAG_DIS;
454 return audpp_send_queue3(&cmd, sizeof(cmd));
457 /* ------------------- device --------------------- */
459 static int audio_enable_adrc(struct audio *audio, int enable)
461 if (audio->adrc_enable != enable) {
462 audio->adrc_enable = enable;
464 audio_dsp_set_adrc(audio);
469 static int audio_enable_eq(struct audio *audio, int enable)
471 if (audio->eq_enable != enable) {
472 audio->eq_enable = enable;
474 audio_dsp_set_eq(audio);
479 static int audio_enable_rx_iir(struct audio *audio, int enable)
481 if (audio->rx_iir_enable != enable) {
482 audio->rx_iir_enable = enable;
484 audio_dsp_set_rx_iir(audio);
489 static void audio_flush(struct audio *audio)
491 audio->out[0].used = 0;
492 audio->out[1].used = 0;
498 static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
500 struct audio *audio = file->private_data;
503 if (cmd == AUDIO_GET_STATS) {
504 struct msm_audio_stats stats;
505 stats.byte_count = atomic_read(&audio->out_bytes);
506 if (copy_to_user((void*) arg, &stats, sizeof(stats)))
510 if (cmd == AUDIO_SET_VOLUME) {
512 spin_lock_irqsave(&audio->dsp_lock, flags);
515 audpp_set_volume_and_pan(6, arg, 0);
516 spin_unlock_irqrestore(&audio->dsp_lock, flags);
520 mutex_lock(&audio->lock);
523 rc = audio_enable(audio);
526 rc = audio_disable(audio);
530 if (audio->stopped) {
531 /* Make sure we're stopped and we wake any threads
532 * that might be blocked holding the write_lock.
533 * While audio->stopped write threads will always
536 wake_up(&audio->wait);
537 mutex_lock(&audio->write_lock);
539 mutex_unlock(&audio->write_lock);
541 case AUDIO_SET_CONFIG: {
542 struct msm_audio_config config;
543 if (copy_from_user(&config, (void*) arg, sizeof(config))) {
547 if (config.channel_count == 1) {
548 config.channel_count = AUDPP_CMD_PCM_INTF_MONO_V;
549 } else if (config.channel_count == 2) {
550 config.channel_count= AUDPP_CMD_PCM_INTF_STEREO_V;
555 audio->out_sample_rate = config.sample_rate;
556 audio->out_channel_mode = config.channel_count;
560 case AUDIO_GET_CONFIG: {
561 struct msm_audio_config config;
562 config.buffer_size = BUFSZ;
563 config.buffer_count = 2;
564 config.sample_rate = audio->out_sample_rate;
565 if (audio->out_channel_mode == AUDPP_CMD_PCM_INTF_MONO_V) {
566 config.channel_count = 1;
568 config.channel_count = 2;
570 config.unused[0] = 0;
571 config.unused[1] = 0;
572 config.unused[2] = 0;
573 config.unused[3] = 0;
574 if (copy_to_user((void*) arg, &config, sizeof(config))) {
584 mutex_unlock(&audio->lock);
588 static ssize_t audio_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
593 static inline int rt_policy(int policy)
595 if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR))
600 static inline int task_has_rt_policy(struct task_struct *p)
602 return rt_policy(p->policy);
605 static ssize_t audio_write(struct file *file, const char __user *buf,
606 size_t count, loff_t *pos)
608 struct sched_param s = { .sched_priority = 1 };
609 struct audio *audio = file->private_data;
611 const char __user *start = buf;
612 struct buffer *frame;
614 int old_prio = current->rt_priority;
615 int old_policy = current->policy;
616 int cap_nice = cap_raised(current_cap(), CAP_SYS_NICE);
619 LOG(EV_WRITE, count | (audio->running << 28) | (audio->stopped << 24));
621 /* just for this write, set us real-time */
622 if (!task_has_rt_policy(current)) {
623 struct cred *new = prepare_creds();
624 cap_raise(new->cap_effective, CAP_SYS_NICE);
626 sched_setscheduler(current, SCHED_RR, &s);
629 mutex_lock(&audio->write_lock);
631 frame = audio->out + audio->out_head;
633 LOG(EV_WAIT_EVENT, 0);
634 rc = wait_event_interruptible(audio->wait,
635 (frame->used == 0) || (audio->stopped));
636 LOG(EV_WAIT_EVENT, 1);
640 if (audio->stopped) {
644 xfer = count > frame->size ? frame->size : count;
645 if (copy_from_user(frame->data, buf, xfer)) {
650 audio->out_head ^= 1;
654 spin_lock_irqsave(&audio->dsp_lock, flags);
655 LOG(EV_FILL_BUFFER, audio->out_head ^ 1);
656 frame = audio->out + audio->out_tail;
657 if (frame->used && audio->out_needed) {
658 audio_dsp_send_buffer(audio, audio->out_tail, frame->used);
659 audio->out_tail ^= 1;
662 spin_unlock_irqrestore(&audio->dsp_lock, flags);
665 mutex_unlock(&audio->write_lock);
667 /* restore scheduling policy and priority */
668 if (!rt_policy(old_policy)) {
669 struct sched_param v = { .sched_priority = old_prio };
670 sched_setscheduler(current, old_policy, &v);
671 if (likely(!cap_nice)) {
672 struct cred *new = prepare_creds();
673 cap_lower(new->cap_effective, CAP_SYS_NICE);
675 sched_setscheduler(current, SCHED_RR, &s);
679 LOG(EV_RETURN,(buf > start) ? (buf - start) : rc);
685 static int audio_release(struct inode *inode, struct file *file)
687 struct audio *audio = file->private_data;
690 mutex_lock(&audio->lock);
691 audio_disable(audio);
694 mutex_unlock(&audio->lock);
698 static struct audio the_audio;
700 static int audio_open(struct inode *inode, struct file *file)
702 struct audio *audio = &the_audio;
705 mutex_lock(&audio->lock);
708 pr_err("audio: busy\n");
714 audio->data = dma_alloc_coherent(NULL, DMASZ,
715 &audio->phys, GFP_KERNEL);
717 pr_err("audio: could not allocate DMA buffers\n");
723 rc = audmgr_open(&audio->audmgr);
727 audio->out_buffer_size = BUFSZ;
728 audio->out_sample_rate = 44100;
729 audio->out_channel_mode = AUDPP_CMD_PCM_INTF_STEREO_V;
730 audio->out_weight = 100;
732 audio->out[0].data = audio->data + 0;
733 audio->out[0].addr = audio->phys + 0;
734 audio->out[0].size = BUFSZ;
736 audio->out[1].data = audio->data + BUFSZ;
737 audio->out[1].addr = audio->phys + BUFSZ;
738 audio->out[1].size = BUFSZ;
740 audio->volume = 0x2000;
744 file->private_data = audio;
749 mutex_unlock(&audio->lock);
753 static long audpp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
755 struct audio *audio = file->private_data;
757 uint16_t enable_mask;
759 mutex_lock(&audio->lock);
761 case AUDIO_ENABLE_AUDPP:
762 if (copy_from_user(&enable_mask, (void *) arg, sizeof(enable_mask)))
765 enable = (enable_mask & ADRC_ENABLE)? 1 : 0;
766 audio_enable_adrc(audio, enable);
767 enable = (enable_mask & EQ_ENABLE)? 1 : 0;
768 audio_enable_eq(audio, enable);
769 enable = (enable_mask & IIR_ENABLE)? 1 : 0;
770 audio_enable_rx_iir(audio, enable);
774 if (copy_from_user(&audio->adrc, (void*) arg, sizeof(audio->adrc)))
779 if (copy_from_user(&audio->eq, (void*) arg, sizeof(audio->eq)))
783 case AUDIO_SET_RX_IIR:
784 if (copy_from_user(&audio->iir, (void*) arg, sizeof(audio->iir)))
797 mutex_unlock(&audio->lock);
801 static int audpp_open(struct inode *inode, struct file *file)
803 struct audio *audio = &the_audio;
805 file->private_data = audio;
809 static struct file_operations audio_fops = {
810 .owner = THIS_MODULE,
812 .release = audio_release,
814 .write = audio_write,
815 .unlocked_ioctl = audio_ioctl,
818 static struct file_operations audpp_fops = {
819 .owner = THIS_MODULE,
821 .unlocked_ioctl = audpp_ioctl,
824 struct miscdevice audio_misc = {
825 .minor = MISC_DYNAMIC_MINOR,
826 .name = "msm_pcm_out",
830 struct miscdevice audpp_misc = {
831 .minor = MISC_DYNAMIC_MINOR,
832 .name = "msm_pcm_ctl",
836 static int __init audio_init(void)
838 mutex_init(&the_audio.lock);
839 mutex_init(&the_audio.write_lock);
840 spin_lock_init(&the_audio.dsp_lock);
841 init_waitqueue_head(&the_audio.wait);
842 wake_lock_init(&the_audio.wakelock, WAKE_LOCK_SUSPEND, "audio_pcm");
843 wake_lock_init(&the_audio.idlelock, WAKE_LOCK_IDLE, "audio_pcm_idle");
844 return (misc_register(&audio_misc) || misc_register(&audpp_misc));
847 device_initcall(audio_init);