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 */
186 struct adrc_filter adrc
;
192 struct rx_iir_filter iir
;
195 static void audio_prevent_sleep(struct audio
*audio
)
197 printk(KERN_INFO
"++++++++++++++++++++++++++++++\n");
200 static void audio_allow_sleep(struct audio
*audio
)
202 printk(KERN_INFO
"------------------------------\n");
205 static int audio_dsp_out_enable(struct audio
*audio
, int yes
);
206 static int audio_dsp_send_buffer(struct audio
*audio
, unsigned id
, unsigned len
);
207 static int audio_dsp_set_adrc(struct audio
*audio
);
208 static int audio_dsp_set_eq(struct audio
*audio
);
209 static int audio_dsp_set_rx_iir(struct audio
*audio
);
211 static void audio_dsp_event(void *private, unsigned id
, uint16_t *msg
);
213 /* must be called with audio->lock held */
214 static int audio_enable(struct audio
*audio
)
216 struct audmgr_config cfg
;
219 pr_info("audio_enable()\n");
224 /* refuse to start if we're not ready */
225 if (!audio
->out
[0].used
|| !audio
->out
[1].used
)
228 /* we start buffers 0 and 1, so buffer 0 will be the
229 * next one the dsp will want
232 audio
->out_needed
= 0;
234 cfg
.tx_rate
= RPC_AUD_DEF_SAMPLE_RATE_NONE
;
235 cfg
.rx_rate
= RPC_AUD_DEF_SAMPLE_RATE_48000
;
236 cfg
.def_method
= RPC_AUD_DEF_METHOD_HOST_PCM
;
237 cfg
.codec
= RPC_AUD_DEF_CODEC_PCM
;
238 cfg
.snd_method
= RPC_SND_METHOD_MIDI
;
240 audio_prevent_sleep(audio
);
241 rc
= audmgr_enable(&audio
->audmgr
, &cfg
);
243 audio_allow_sleep(audio
);
247 if (audpp_enable(-1, audio_dsp_event
, audio
)) {
248 pr_err("audio: audpp_enable() failed\n");
249 audmgr_disable(&audio
->audmgr
);
250 audio_allow_sleep(audio
);
258 /* must be called with audio->lock held */
259 static int audio_disable(struct audio
*audio
)
261 pr_info("audio_disable()\n");
262 if (audio
->enabled
) {
264 audio_dsp_out_enable(audio
, 0);
266 audpp_disable(-1, audio
);
268 wake_up(&audio
->wait
);
269 audmgr_disable(&audio
->audmgr
);
270 audio
->out_needed
= 0;
271 audio_allow_sleep(audio
);
276 /* ------------------- dsp --------------------- */
277 static void audio_dsp_event(void *private, unsigned id
, uint16_t *msg
)
279 struct audio
*audio
= private;
280 struct buffer
*frame
;
283 LOG(EV_DSP_EVENT
, id
);
285 case AUDPP_MSG_HOST_PCM_INTF_MSG
: {
286 unsigned id
= msg
[2];
287 unsigned idx
= msg
[3] - 1;
289 /* pr_info("audio_dsp_event: HOST_PCM id %d idx %d\n", id, idx); */
290 if (id
!= AUDPP_MSG_HOSTPCM_ID_ARM_RX
) {
291 pr_err("bogus id\n");
295 pr_err("bogus buffer idx\n");
299 spin_lock_irqsave(&audio
->dsp_lock
, flags
);
300 if (audio
->running
) {
301 atomic_add(audio
->out
[idx
].used
, &audio
->out_bytes
);
302 audio
->out
[idx
].used
= 0;
304 frame
= audio
->out
+ audio
->out_tail
;
306 audio_dsp_send_buffer(
307 audio
, audio
->out_tail
, frame
->used
);
308 audio
->out_tail
^= 1;
312 wake_up(&audio
->wait
);
314 spin_unlock_irqrestore(&audio
->dsp_lock
, flags
);
317 case AUDPP_MSG_PCMDMAMISSED
:
318 pr_info("audio_dsp_event: PCMDMAMISSED %d\n", msg
[0]);
320 case AUDPP_MSG_CFG_MSG
:
321 if (msg
[0] == AUDPP_MSG_ENA_ENA
) {
323 pr_info("audio_dsp_event: CFG_MSG ENABLE\n");
324 audio
->out_needed
= 0;
326 audpp_set_volume_and_pan(5, audio
->volume
, 0);
327 audio_dsp_set_adrc(audio
);
328 audio_dsp_set_eq(audio
);
329 audio_dsp_set_rx_iir(audio
);
330 audio_dsp_out_enable(audio
, 1);
331 } else if (msg
[0] == AUDPP_MSG_ENA_DIS
) {
333 pr_info("audio_dsp_event: CFG_MSG DISABLE\n");
336 pr_err("audio_dsp_event: CFG_MSG %d?\n", msg
[0]);
340 pr_err("audio_dsp_event: UNKNOWN (%d)\n", id
);
344 static int audio_dsp_out_enable(struct audio
*audio
, int yes
)
346 audpp_cmd_pcm_intf cmd
;
348 memset(&cmd
, 0, sizeof(cmd
));
349 cmd
.cmd_id
= AUDPP_CMD_PCM_INTF_2
;
350 cmd
.object_num
= AUDPP_CMD_PCM_INTF_OBJECT_NUM
;
351 cmd
.config
= AUDPP_CMD_PCM_INTF_CONFIG_CMD_V
;
352 cmd
.intf_type
= AUDPP_CMD_PCM_INTF_RX_ENA_ARMTODSP_V
;
355 cmd
.write_buf1LSW
= audio
->out
[0].addr
;
356 cmd
.write_buf1MSW
= audio
->out
[0].addr
>> 16;
357 cmd
.write_buf1_len
= audio
->out
[0].size
;
358 cmd
.write_buf2LSW
= audio
->out
[1].addr
;
359 cmd
.write_buf2MSW
= audio
->out
[1].addr
>> 16;
360 cmd
.write_buf2_len
= audio
->out
[1].size
;
361 cmd
.arm_to_rx_flag
= AUDPP_CMD_PCM_INTF_ENA_V
;
362 cmd
.weight_decoder_to_rx
= audio
->out_weight
;
363 cmd
.weight_arm_to_rx
= 1;
364 cmd
.partition_number_arm_to_dsp
= 0;
365 cmd
.sample_rate
= audio
->out_sample_rate
;
366 cmd
.channel_mode
= audio
->out_channel_mode
;
369 return audpp_send_queue2(&cmd
, sizeof(cmd
));
372 static int audio_dsp_send_buffer(struct audio
*audio
, unsigned idx
, unsigned len
)
374 audpp_cmd_pcm_intf_send_buffer cmd
;
376 cmd
.cmd_id
= AUDPP_CMD_PCM_INTF_2
;
377 cmd
.host_pcm_object
= AUDPP_CMD_PCM_INTF_OBJECT_NUM
;
378 cmd
.config
= AUDPP_CMD_PCM_INTF_BUFFER_CMD_V
;
379 cmd
.intf_type
= AUDPP_CMD_PCM_INTF_RX_ENA_ARMTODSP_V
;
380 cmd
.dsp_to_arm_buf_id
= 0;
381 cmd
.arm_to_dsp_buf_id
= idx
+ 1;
382 cmd
.arm_to_dsp_buf_len
= len
;
384 LOG(EV_SEND_BUFFER
, idx
);
385 return audpp_send_queue2(&cmd
, sizeof(cmd
));
388 static int audio_dsp_set_adrc(struct audio
*audio
)
390 audpp_cmd_cfg_object_params_adrc cmd
;
392 memset(&cmd
, 0, sizeof(cmd
));
393 cmd
.common
.comman_cfg
= AUDPP_CMD_CFG_OBJ_UPDATE
;
394 cmd
.common
.command_type
= AUDPP_CMD_ADRC
;
396 if (audio
->adrc_enable
) {
397 cmd
.adrc_flag
= AUDPP_CMD_ADRC_FLAG_ENA
;
398 cmd
.compression_th
= audio
->adrc
.compression_th
;
399 cmd
.compression_slope
= audio
->adrc
.compression_slope
;
400 cmd
.rms_time
= audio
->adrc
.rms_time
;
401 cmd
.attack_const_lsw
= audio
->adrc
.attack_const_lsw
;
402 cmd
.attack_const_msw
= audio
->adrc
.attack_const_msw
;
403 cmd
.release_const_lsw
= audio
->adrc
.release_const_lsw
;
404 cmd
.release_const_msw
= audio
->adrc
.release_const_msw
;
405 cmd
.adrc_system_delay
= audio
->adrc
.adrc_system_delay
;
407 cmd
.adrc_flag
= AUDPP_CMD_ADRC_FLAG_DIS
;
409 return audpp_send_queue3(&cmd
, sizeof(cmd
));
412 static int audio_dsp_set_eq(struct audio
*audio
)
414 audpp_cmd_cfg_object_params_eq cmd
;
416 memset(&cmd
, 0, sizeof(cmd
));
417 cmd
.common
.comman_cfg
= AUDPP_CMD_CFG_OBJ_UPDATE
;
418 cmd
.common
.command_type
= AUDPP_CMD_EQUALIZER
;
420 if (audio
->eq_enable
) {
421 cmd
.eq_flag
= AUDPP_CMD_EQ_FLAG_ENA
;
422 cmd
.num_bands
= audio
->eq
.num_bands
;
423 memcpy(&cmd
.eq_params
, audio
->eq
.eq_params
,
424 sizeof(audio
->eq
.eq_params
));
426 cmd
.eq_flag
= AUDPP_CMD_EQ_FLAG_DIS
;
428 return audpp_send_queue3(&cmd
, sizeof(cmd
));
431 static int audio_dsp_set_rx_iir(struct audio
*audio
)
433 audpp_cmd_cfg_object_params_rx_iir cmd
;
435 memset(&cmd
, 0, sizeof(cmd
));
436 cmd
.common
.comman_cfg
= AUDPP_CMD_CFG_OBJ_UPDATE
;
437 cmd
.common
.command_type
= AUDPP_CMD_IIR_TUNING_FILTER
;
439 if (audio
->rx_iir_enable
) {
440 cmd
.active_flag
= AUDPP_CMD_IIR_FLAG_ENA
;
441 cmd
.num_bands
= audio
->iir
.num_bands
;
442 memcpy(&cmd
.iir_params
, audio
->iir
.iir_params
,
443 sizeof(audio
->iir
.iir_params
));
445 cmd
.active_flag
= AUDPP_CMD_IIR_FLAG_DIS
;
448 return audpp_send_queue3(&cmd
, sizeof(cmd
));
451 /* ------------------- device --------------------- */
453 static int audio_enable_adrc(struct audio
*audio
, int enable
)
455 if (audio
->adrc_enable
!= enable
) {
456 audio
->adrc_enable
= enable
;
458 audio_dsp_set_adrc(audio
);
463 static int audio_enable_eq(struct audio
*audio
, int enable
)
465 if (audio
->eq_enable
!= enable
) {
466 audio
->eq_enable
= enable
;
468 audio_dsp_set_eq(audio
);
473 static int audio_enable_rx_iir(struct audio
*audio
, int enable
)
475 if (audio
->rx_iir_enable
!= enable
) {
476 audio
->rx_iir_enable
= enable
;
478 audio_dsp_set_rx_iir(audio
);
483 static void audio_flush(struct audio
*audio
)
485 audio
->out
[0].used
= 0;
486 audio
->out
[1].used
= 0;
492 static long audio_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
494 struct audio
*audio
= file
->private_data
;
497 if (cmd
== AUDIO_GET_STATS
) {
498 struct msm_audio_stats stats
;
499 stats
.byte_count
= atomic_read(&audio
->out_bytes
);
500 if (copy_to_user((void*) arg
, &stats
, sizeof(stats
)))
504 if (cmd
== AUDIO_SET_VOLUME
) {
506 spin_lock_irqsave(&audio
->dsp_lock
, flags
);
509 audpp_set_volume_and_pan(6, arg
, 0);
510 spin_unlock_irqrestore(&audio
->dsp_lock
, flags
);
514 mutex_lock(&audio
->lock
);
517 rc
= audio_enable(audio
);
520 rc
= audio_disable(audio
);
524 if (audio
->stopped
) {
525 /* Make sure we're stopped and we wake any threads
526 * that might be blocked holding the write_lock.
527 * While audio->stopped write threads will always
530 wake_up(&audio
->wait
);
531 mutex_lock(&audio
->write_lock
);
533 mutex_unlock(&audio
->write_lock
);
535 case AUDIO_SET_CONFIG
: {
536 struct msm_audio_config config
;
537 if (copy_from_user(&config
, (void*) arg
, sizeof(config
))) {
541 if (config
.channel_count
== 1) {
542 config
.channel_count
= AUDPP_CMD_PCM_INTF_MONO_V
;
543 } else if (config
.channel_count
== 2) {
544 config
.channel_count
= AUDPP_CMD_PCM_INTF_STEREO_V
;
549 audio
->out_sample_rate
= config
.sample_rate
;
550 audio
->out_channel_mode
= config
.channel_count
;
554 case AUDIO_GET_CONFIG
: {
555 struct msm_audio_config config
;
556 config
.buffer_size
= BUFSZ
;
557 config
.buffer_count
= 2;
558 config
.sample_rate
= audio
->out_sample_rate
;
559 if (audio
->out_channel_mode
== AUDPP_CMD_PCM_INTF_MONO_V
) {
560 config
.channel_count
= 1;
562 config
.channel_count
= 2;
564 config
.unused
[0] = 0;
565 config
.unused
[1] = 0;
566 config
.unused
[2] = 0;
567 config
.unused
[3] = 0;
568 if (copy_to_user((void*) arg
, &config
, sizeof(config
))) {
578 mutex_unlock(&audio
->lock
);
582 static ssize_t
audio_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*pos
)
587 static inline int rt_policy(int policy
)
589 if (unlikely(policy
== SCHED_FIFO
) || unlikely(policy
== SCHED_RR
))
594 static inline int task_has_rt_policy(struct task_struct
*p
)
596 return rt_policy(p
->policy
);
599 static ssize_t
audio_write(struct file
*file
, const char __user
*buf
,
600 size_t count
, loff_t
*pos
)
602 struct sched_param s
= { .sched_priority
= 1 };
603 struct audio
*audio
= file
->private_data
;
605 const char __user
*start
= buf
;
606 struct buffer
*frame
;
608 int old_prio
= current
->rt_priority
;
609 int old_policy
= current
->policy
;
610 int cap_nice
= cap_raised(current_cap(), CAP_SYS_NICE
);
613 LOG(EV_WRITE
, count
| (audio
->running
<< 28) | (audio
->stopped
<< 24));
615 /* just for this write, set us real-time */
616 if (!task_has_rt_policy(current
)) {
617 struct cred
*new = prepare_creds();
618 cap_raise(new->cap_effective
, CAP_SYS_NICE
);
620 sched_setscheduler(current
, SCHED_RR
, &s
);
623 mutex_lock(&audio
->write_lock
);
625 frame
= audio
->out
+ audio
->out_head
;
627 LOG(EV_WAIT_EVENT
, 0);
628 rc
= wait_event_interruptible(audio
->wait
,
629 (frame
->used
== 0) || (audio
->stopped
));
630 LOG(EV_WAIT_EVENT
, 1);
634 if (audio
->stopped
) {
638 xfer
= count
> frame
->size
? frame
->size
: count
;
639 if (copy_from_user(frame
->data
, buf
, xfer
)) {
644 audio
->out_head
^= 1;
648 spin_lock_irqsave(&audio
->dsp_lock
, flags
);
649 LOG(EV_FILL_BUFFER
, audio
->out_head
^ 1);
650 frame
= audio
->out
+ audio
->out_tail
;
651 if (frame
->used
&& audio
->out_needed
) {
652 audio_dsp_send_buffer(audio
, audio
->out_tail
, frame
->used
);
653 audio
->out_tail
^= 1;
656 spin_unlock_irqrestore(&audio
->dsp_lock
, flags
);
659 mutex_unlock(&audio
->write_lock
);
661 /* restore scheduling policy and priority */
662 if (!rt_policy(old_policy
)) {
663 struct sched_param v
= { .sched_priority
= old_prio
};
664 sched_setscheduler(current
, old_policy
, &v
);
665 if (likely(!cap_nice
)) {
666 struct cred
*new = prepare_creds();
667 cap_lower(new->cap_effective
, CAP_SYS_NICE
);
669 sched_setscheduler(current
, SCHED_RR
, &s
);
673 LOG(EV_RETURN
,(buf
> start
) ? (buf
- start
) : rc
);
679 static int audio_release(struct inode
*inode
, struct file
*file
)
681 struct audio
*audio
= file
->private_data
;
684 mutex_lock(&audio
->lock
);
685 audio_disable(audio
);
688 mutex_unlock(&audio
->lock
);
692 static struct audio the_audio
;
694 static int audio_open(struct inode
*inode
, struct file
*file
)
696 struct audio
*audio
= &the_audio
;
699 mutex_lock(&audio
->lock
);
702 pr_err("audio: busy\n");
708 audio
->data
= dma_alloc_coherent(NULL
, DMASZ
,
709 &audio
->phys
, GFP_KERNEL
);
711 pr_err("audio: could not allocate DMA buffers\n");
717 rc
= audmgr_open(&audio
->audmgr
);
721 audio
->out_buffer_size
= BUFSZ
;
722 audio
->out_sample_rate
= 44100;
723 audio
->out_channel_mode
= AUDPP_CMD_PCM_INTF_STEREO_V
;
724 audio
->out_weight
= 100;
726 audio
->out
[0].data
= audio
->data
+ 0;
727 audio
->out
[0].addr
= audio
->phys
+ 0;
728 audio
->out
[0].size
= BUFSZ
;
730 audio
->out
[1].data
= audio
->data
+ BUFSZ
;
731 audio
->out
[1].addr
= audio
->phys
+ BUFSZ
;
732 audio
->out
[1].size
= BUFSZ
;
734 audio
->volume
= 0x2000;
738 file
->private_data
= audio
;
743 mutex_unlock(&audio
->lock
);
747 static long audpp_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
749 struct audio
*audio
= file
->private_data
;
751 uint16_t enable_mask
;
753 mutex_lock(&audio
->lock
);
755 case AUDIO_ENABLE_AUDPP
:
756 if (copy_from_user(&enable_mask
, (void *) arg
, sizeof(enable_mask
)))
759 enable
= (enable_mask
& ADRC_ENABLE
)? 1 : 0;
760 audio_enable_adrc(audio
, enable
);
761 enable
= (enable_mask
& EQ_ENABLE
)? 1 : 0;
762 audio_enable_eq(audio
, enable
);
763 enable
= (enable_mask
& IIR_ENABLE
)? 1 : 0;
764 audio_enable_rx_iir(audio
, enable
);
768 if (copy_from_user(&audio
->adrc
, (void*) arg
, sizeof(audio
->adrc
)))
773 if (copy_from_user(&audio
->eq
, (void*) arg
, sizeof(audio
->eq
)))
777 case AUDIO_SET_RX_IIR
:
778 if (copy_from_user(&audio
->iir
, (void*) arg
, sizeof(audio
->iir
)))
791 mutex_unlock(&audio
->lock
);
795 static int audpp_open(struct inode
*inode
, struct file
*file
)
797 struct audio
*audio
= &the_audio
;
799 file
->private_data
= audio
;
803 static struct file_operations audio_fops
= {
804 .owner
= THIS_MODULE
,
806 .release
= audio_release
,
808 .write
= audio_write
,
809 .unlocked_ioctl
= audio_ioctl
,
810 .llseek
= noop_llseek
,
813 static struct file_operations audpp_fops
= {
814 .owner
= THIS_MODULE
,
816 .unlocked_ioctl
= audpp_ioctl
,
817 .llseek
= noop_llseek
,
820 struct miscdevice audio_misc
= {
821 .minor
= MISC_DYNAMIC_MINOR
,
822 .name
= "msm_pcm_out",
826 struct miscdevice audpp_misc
= {
827 .minor
= MISC_DYNAMIC_MINOR
,
828 .name
= "msm_pcm_ctl",
832 static int __init
audio_init(void)
834 mutex_init(&the_audio
.lock
);
835 mutex_init(&the_audio
.write_lock
);
836 spin_lock_init(&the_audio
.dsp_lock
);
837 init_waitqueue_head(&the_audio
.wait
);
838 return (misc_register(&audio_misc
) || misc_register(&audpp_misc
));
841 device_initcall(audio_init
);