1 /* arch/arm/mach-msm/qdsp5/audio_in.c
3 * pcm audio input 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/gfp.h>
28 #include <linux/delay.h>
30 #include <linux/msm_audio.h>
32 #include <asm/atomic.h>
33 #include <asm/ioctls.h>
34 #include <mach/msm_adsp.h>
35 #include <mach/msm_rpcrouter.h>
39 #include <mach/qdsp5/qdsp5audpreproccmdi.h>
40 #include <mach/qdsp5/qdsp5audpreprocmsg.h>
41 #include <mach/qdsp5/qdsp5audreccmdi.h>
42 #include <mach/qdsp5/qdsp5audrecmsg.h>
44 /* for queue ids - should be relative to module number*/
47 /* FRAME_NUM must be a power of two */
49 #define FRAME_SIZE (2052 * 2)
50 #define MONO_DATA_SIZE (2048)
51 #define STEREO_DATA_SIZE (MONO_DATA_SIZE * 2)
52 #define DMASZ (FRAME_SIZE * FRAME_NUM)
54 #define AGC_PARAM_SIZE (20)
55 #define NS_PARAM_SIZE (6)
56 #define IIR_PARAM_SIZE (48)
59 #define AGC_ENABLE 0x0001
60 #define NS_ENABLE 0x0002
61 #define IIR_ENABLE 0x0004
63 struct tx_agc_config
{
64 uint16_t agc_params
[AGC_PARAM_SIZE
];
68 uint16_t ns_params
[NS_PARAM_SIZE
];
71 struct tx_iir_filter
{
73 uint16_t iir_params
[IIR_PARAM_SIZE
];
76 struct audpre_cmd_iir_config_type
{
80 uint16_t iir_params
[IIR_PARAM_SIZE
];
91 struct buffer in
[FRAME_NUM
];
98 struct mutex read_lock
;
99 wait_queue_head_t wait
;
101 struct msm_adsp_module
*audpre
;
102 struct msm_adsp_module
*audrec
;
104 /* configuration to use on next enable */
106 uint32_t channel_mode
;
107 uint32_t buffer_size
; /* 2048 for mono, 4096 for stereo */
108 uint32_t type
; /* 0 for PCM ,1 for AAC */
110 uint32_t in_head
; /* next buffer dsp will write */
111 uint32_t in_tail
; /* next buffer read() will read */
112 uint32_t in_count
; /* number of buffers available to read() */
114 unsigned short samp_rate_index
;
116 struct audmgr audmgr
;
118 /* data allocated for various buffers */
125 int stopped
; /* set when stopped, cleared on flush */
127 /* audpre settings */
129 struct tx_agc_config agc
;
135 struct tx_iir_filter iir
;
138 static int audio_in_dsp_enable(struct audio_in
*audio
, int enable
);
139 static int audio_in_encoder_config(struct audio_in
*audio
);
140 static int audio_dsp_read_buffer(struct audio_in
*audio
, uint32_t read_cnt
);
141 static void audio_flush(struct audio_in
*audio
);
142 static int audio_dsp_set_agc(struct audio_in
*audio
);
143 static int audio_dsp_set_ns(struct audio_in
*audio
);
144 static int audio_dsp_set_tx_iir(struct audio_in
*audio
);
146 static unsigned convert_dsp_samp_index(unsigned index
)
149 case 48000: return AUDREC_CMD_SAMP_RATE_INDX_48000
;
150 case 44100: return AUDREC_CMD_SAMP_RATE_INDX_44100
;
151 case 32000: return AUDREC_CMD_SAMP_RATE_INDX_32000
;
152 case 24000: return AUDREC_CMD_SAMP_RATE_INDX_24000
;
153 case 22050: return AUDREC_CMD_SAMP_RATE_INDX_22050
;
154 case 16000: return AUDREC_CMD_SAMP_RATE_INDX_16000
;
155 case 12000: return AUDREC_CMD_SAMP_RATE_INDX_12000
;
156 case 11025: return AUDREC_CMD_SAMP_RATE_INDX_11025
;
157 case 8000: return AUDREC_CMD_SAMP_RATE_INDX_8000
;
158 default: return AUDREC_CMD_SAMP_RATE_INDX_11025
;
162 static unsigned convert_samp_rate(unsigned hz
)
165 case 48000: return RPC_AUD_DEF_SAMPLE_RATE_48000
;
166 case 44100: return RPC_AUD_DEF_SAMPLE_RATE_44100
;
167 case 32000: return RPC_AUD_DEF_SAMPLE_RATE_32000
;
168 case 24000: return RPC_AUD_DEF_SAMPLE_RATE_24000
;
169 case 22050: return RPC_AUD_DEF_SAMPLE_RATE_22050
;
170 case 16000: return RPC_AUD_DEF_SAMPLE_RATE_16000
;
171 case 12000: return RPC_AUD_DEF_SAMPLE_RATE_12000
;
172 case 11025: return RPC_AUD_DEF_SAMPLE_RATE_11025
;
173 case 8000: return RPC_AUD_DEF_SAMPLE_RATE_8000
;
174 default: return RPC_AUD_DEF_SAMPLE_RATE_11025
;
178 static unsigned convert_samp_index(unsigned index
)
181 case RPC_AUD_DEF_SAMPLE_RATE_48000
: return 48000;
182 case RPC_AUD_DEF_SAMPLE_RATE_44100
: return 44100;
183 case RPC_AUD_DEF_SAMPLE_RATE_32000
: return 32000;
184 case RPC_AUD_DEF_SAMPLE_RATE_24000
: return 24000;
185 case RPC_AUD_DEF_SAMPLE_RATE_22050
: return 22050;
186 case RPC_AUD_DEF_SAMPLE_RATE_16000
: return 16000;
187 case RPC_AUD_DEF_SAMPLE_RATE_12000
: return 12000;
188 case RPC_AUD_DEF_SAMPLE_RATE_11025
: return 11025;
189 case RPC_AUD_DEF_SAMPLE_RATE_8000
: return 8000;
190 default: return 11025;
194 /* must be called with audio->lock held */
195 static int audio_in_enable(struct audio_in
*audio
)
197 struct audmgr_config cfg
;
203 cfg
.tx_rate
= audio
->samp_rate
;
204 cfg
.rx_rate
= RPC_AUD_DEF_SAMPLE_RATE_NONE
;
205 cfg
.def_method
= RPC_AUD_DEF_METHOD_RECORD
;
206 if (audio
->type
== AUDREC_CMD_TYPE_0_INDEX_WAV
)
207 cfg
.codec
= RPC_AUD_DEF_CODEC_PCM
;
209 cfg
.codec
= RPC_AUD_DEF_CODEC_AAC
;
210 cfg
.snd_method
= RPC_SND_METHOD_MIDI
;
212 rc
= audmgr_enable(&audio
->audmgr
, &cfg
);
216 if (msm_adsp_enable(audio
->audpre
)) {
217 pr_err("audrec: msm_adsp_enable(audpre) failed\n");
220 if (msm_adsp_enable(audio
->audrec
)) {
221 pr_err("audrec: msm_adsp_enable(audrec) failed\n");
226 audio_in_dsp_enable(audio
, 1);
231 /* must be called with audio->lock held */
232 static int audio_in_disable(struct audio_in
*audio
)
234 if (audio
->enabled
) {
237 audio_in_dsp_enable(audio
, 0);
239 wake_up(&audio
->wait
);
241 msm_adsp_disable(audio
->audrec
);
242 msm_adsp_disable(audio
->audpre
);
243 audmgr_disable(&audio
->audmgr
);
248 /* ------------------- dsp --------------------- */
249 static void audpre_dsp_event(void *data
, unsigned id
, size_t len
,
250 void (*getevent
)(void *ptr
, size_t len
))
253 getevent(msg
, sizeof(msg
));
256 case AUDPREPROC_MSG_CMD_CFG_DONE_MSG
:
257 pr_info("audpre: type %d, status_flag %d\n", msg
[0], msg
[1]);
259 case AUDPREPROC_MSG_ERROR_MSG_ID
:
260 pr_info("audpre: err_index %d\n", msg
[0]);
263 pr_err("audpre: unknown event %d\n", id
);
272 unsigned char samples
[];
273 } __attribute__((packed
));
275 static void audio_in_get_dsp_frames(struct audio_in
*audio
)
277 struct audio_frame
*frame
;
281 index
= audio
->in_head
;
284 frame
= (void *) (((char *)audio
->in
[index
].data
) - sizeof(*frame
));
286 spin_lock_irqsave(&audio
->dsp_lock
, flags
);
287 audio
->in
[index
].size
= frame
->bytes
;
289 audio
->in_head
= (audio
->in_head
+ 1) & (FRAME_NUM
- 1);
291 /* If overflow, move the tail index foward. */
292 if (audio
->in_head
== audio
->in_tail
)
293 audio
->in_tail
= (audio
->in_tail
+ 1) & (FRAME_NUM
- 1);
297 audio_dsp_read_buffer(audio
, audio
->dsp_cnt
++);
298 spin_unlock_irqrestore(&audio
->dsp_lock
, flags
);
300 wake_up(&audio
->wait
);
303 static void audrec_dsp_event(void *data
, unsigned id
, size_t len
,
304 void (*getevent
)(void *ptr
, size_t len
))
306 struct audio_in
*audio
= data
;
308 getevent(msg
, sizeof(msg
));
311 case AUDREC_MSG_CMD_CFG_DONE_MSG
:
312 if (msg
[0] & AUDREC_MSG_CFG_DONE_TYPE_0_UPDATE
) {
313 if (msg
[0] & AUDREC_MSG_CFG_DONE_TYPE_0_ENA
) {
314 pr_info("audpre: CFG ENABLED\n");
315 audio_dsp_set_agc(audio
);
316 audio_dsp_set_ns(audio
);
317 audio_dsp_set_tx_iir(audio
);
318 audio_in_encoder_config(audio
);
320 pr_info("audrec: CFG SLEEP\n");
324 pr_info("audrec: CMD_CFG_DONE %x\n", msg
[0]);
327 case AUDREC_MSG_CMD_AREC_PARAM_CFG_DONE_MSG
: {
328 pr_info("audrec: PARAM CFG DONE\n");
332 case AUDREC_MSG_FATAL_ERR_MSG
:
333 pr_err("audrec: ERROR %x\n", msg
[0]);
335 case AUDREC_MSG_PACKET_READY_MSG
:
336 /* REC_DBG("type %x, count %d", msg[0], (msg[1] | (msg[2] << 16))); */
337 audio_in_get_dsp_frames(audio
);
340 pr_err("audrec: unknown event %d\n", id
);
344 struct msm_adsp_ops audpre_adsp_ops
= {
345 .event
= audpre_dsp_event
,
348 struct msm_adsp_ops audrec_adsp_ops
= {
349 .event
= audrec_dsp_event
,
353 #define audio_send_queue_pre(audio, cmd, len) \
354 msm_adsp_write(audio->audpre, QDSP_uPAudPreProcCmdQueue, cmd, len)
355 #define audio_send_queue_recbs(audio, cmd, len) \
356 msm_adsp_write(audio->audrec, QDSP_uPAudRecBitStreamQueue, cmd, len)
357 #define audio_send_queue_rec(audio, cmd, len) \
358 msm_adsp_write(audio->audrec, \
359 QDSP_uPAudRecCmdQueue, cmd, len)
361 static int audio_dsp_set_agc(struct audio_in
*audio
)
363 audpreproc_cmd_cfg_agc_params cmd
;
365 memset(&cmd
, 0, sizeof(cmd
));
366 cmd
.cmd_id
= AUDPREPROC_CMD_CFG_AGC_PARAMS
;
368 if (audio
->agc_enable
) {
369 /* cmd.tx_agc_param_mask = 0xFE00 from sample code */
370 cmd
.tx_agc_param_mask
=
371 (1 << AUDPREPROC_CMD_TX_AGC_PARAM_MASK_COMP_SLOPE
) |
372 (1 << AUDPREPROC_CMD_TX_AGC_PARAM_MASK_COMP_TH
) |
373 (1 << AUDPREPROC_CMD_TX_AGC_PARAM_MASK_EXP_SLOPE
) |
374 (1 << AUDPREPROC_CMD_TX_AGC_PARAM_MASK_EXP_TH
) |
375 (1 << AUDPREPROC_CMD_TX_AGC_PARAM_MASK_COMP_AIG_FLAG
) |
376 (1 << AUDPREPROC_CMD_TX_AGC_PARAM_MASK_COMP_STATIC_GAIN
) |
377 (1 << AUDPREPROC_CMD_TX_AGC_PARAM_MASK_TX_AGC_ENA_FLAG
);
378 cmd
.tx_agc_enable_flag
=
379 AUDPREPROC_CMD_TX_AGC_ENA_FLAG_ENA
;
380 memcpy(&cmd
.static_gain
, &audio
->agc
.agc_params
[0],
381 sizeof(uint16_t) * 6);
382 /* cmd.param_mask = 0xFFF0 from sample code */
384 (1 << AUDPREPROC_CMD_PARAM_MASK_RMS_TAY
) |
385 (1 << AUDPREPROC_CMD_PARAM_MASK_RELEASEK
) |
386 (1 << AUDPREPROC_CMD_PARAM_MASK_DELAY
) |
387 (1 << AUDPREPROC_CMD_PARAM_MASK_ATTACKK
) |
388 (1 << AUDPREPROC_CMD_PARAM_MASK_LEAKRATE_SLOW
) |
389 (1 << AUDPREPROC_CMD_PARAM_MASK_LEAKRATE_FAST
) |
390 (1 << AUDPREPROC_CMD_PARAM_MASK_AIG_RELEASEK
) |
391 (1 << AUDPREPROC_CMD_PARAM_MASK_AIG_MIN
) |
392 (1 << AUDPREPROC_CMD_PARAM_MASK_AIG_MAX
) |
393 (1 << AUDPREPROC_CMD_PARAM_MASK_LEAK_UP
) |
394 (1 << AUDPREPROC_CMD_PARAM_MASK_LEAK_DOWN
) |
395 (1 << AUDPREPROC_CMD_PARAM_MASK_AIG_ATTACKK
);
396 memcpy(&cmd
.aig_attackk
, &audio
->agc
.agc_params
[6],
397 sizeof(uint16_t) * 14);
400 cmd
.tx_agc_param_mask
=
401 (1 << AUDPREPROC_CMD_TX_AGC_PARAM_MASK_TX_AGC_ENA_FLAG
);
402 cmd
.tx_agc_enable_flag
=
403 AUDPREPROC_CMD_TX_AGC_ENA_FLAG_DIS
;
406 pr_info("cmd_id = 0x%04x\n", cmd
.cmd_id
);
407 pr_info("tx_agc_param_mask = 0x%04x\n", cmd
.tx_agc_param_mask
);
408 pr_info("tx_agc_enable_flag = 0x%04x\n", cmd
.tx_agc_enable_flag
);
409 pr_info("static_gain = 0x%04x\n", cmd
.static_gain
);
410 pr_info("adaptive_gain_flag = 0x%04x\n", cmd
.adaptive_gain_flag
);
411 pr_info("expander_th = 0x%04x\n", cmd
.expander_th
);
412 pr_info("expander_slope = 0x%04x\n", cmd
.expander_slope
);
413 pr_info("compressor_th = 0x%04x\n", cmd
.compressor_th
);
414 pr_info("compressor_slope = 0x%04x\n", cmd
.compressor_slope
);
415 pr_info("param_mask = 0x%04x\n", cmd
.param_mask
);
416 pr_info("aig_attackk = 0x%04x\n", cmd
.aig_attackk
);
417 pr_info("aig_leak_down = 0x%04x\n", cmd
.aig_leak_down
);
418 pr_info("aig_leak_up = 0x%04x\n", cmd
.aig_leak_up
);
419 pr_info("aig_max = 0x%04x\n", cmd
.aig_max
);
420 pr_info("aig_min = 0x%04x\n", cmd
.aig_min
);
421 pr_info("aig_releasek = 0x%04x\n", cmd
.aig_releasek
);
422 pr_info("aig_leakrate_fast = 0x%04x\n", cmd
.aig_leakrate_fast
);
423 pr_info("aig_leakrate_slow = 0x%04x\n", cmd
.aig_leakrate_slow
);
424 pr_info("attackk_msw = 0x%04x\n", cmd
.attackk_msw
);
425 pr_info("attackk_lsw = 0x%04x\n", cmd
.attackk_lsw
);
426 pr_info("delay = 0x%04x\n", cmd
.delay
);
427 pr_info("releasek_msw = 0x%04x\n", cmd
.releasek_msw
);
428 pr_info("releasek_lsw = 0x%04x\n", cmd
.releasek_lsw
);
429 pr_info("rms_tav = 0x%04x\n", cmd
.rms_tav
);
431 return audio_send_queue_pre(audio
, &cmd
, sizeof(cmd
));
434 static int audio_dsp_set_ns(struct audio_in
*audio
)
436 audpreproc_cmd_cfg_ns_params cmd
;
438 memset(&cmd
, 0, sizeof(cmd
));
439 cmd
.cmd_id
= AUDPREPROC_CMD_CFG_NS_PARAMS
;
441 if (audio
->ns_enable
) {
442 /* cmd.ec_mode_new is fixed as 0x0064 when enable from sample code */
444 AUDPREPROC_CMD_EC_MODE_NEW_NS_ENA
|
445 AUDPREPROC_CMD_EC_MODE_NEW_HB_ENA
|
446 AUDPREPROC_CMD_EC_MODE_NEW_VA_ENA
;
447 memcpy(&cmd
.dens_gamma_n
, &audio
->ns
.ns_params
,
448 sizeof(audio
->ns
.ns_params
));
451 AUDPREPROC_CMD_EC_MODE_NEW_NLMS_DIS
|
452 AUDPREPROC_CMD_EC_MODE_NEW_DES_DIS
|
453 AUDPREPROC_CMD_EC_MODE_NEW_NS_DIS
|
454 AUDPREPROC_CMD_EC_MODE_NEW_CNI_DIS
|
455 AUDPREPROC_CMD_EC_MODE_NEW_NLES_DIS
|
456 AUDPREPROC_CMD_EC_MODE_NEW_HB_DIS
|
457 AUDPREPROC_CMD_EC_MODE_NEW_VA_DIS
|
458 AUDPREPROC_CMD_EC_MODE_NEW_PCD_DIS
|
459 AUDPREPROC_CMD_EC_MODE_NEW_FEHI_DIS
|
460 AUDPREPROC_CMD_EC_MODE_NEW_NEHI_DIS
|
461 AUDPREPROC_CMD_EC_MODE_NEW_NLPP_DIS
|
462 AUDPREPROC_CMD_EC_MODE_NEW_FNE_DIS
|
463 AUDPREPROC_CMD_EC_MODE_NEW_PRENLMS_DIS
;
466 pr_info("cmd_id = 0x%04x\n", cmd
.cmd_id
);
467 pr_info("ec_mode_new = 0x%04x\n", cmd
.ec_mode_new
);
468 pr_info("dens_gamma_n = 0x%04x\n", cmd
.dens_gamma_n
);
469 pr_info("dens_nfe_block_size = 0x%04x\n", cmd
.dens_nfe_block_size
);
470 pr_info("dens_limit_ns = 0x%04x\n", cmd
.dens_limit_ns
);
471 pr_info("dens_limit_ns_d = 0x%04x\n", cmd
.dens_limit_ns_d
);
472 pr_info("wb_gamma_e = 0x%04x\n", cmd
.wb_gamma_e
);
473 pr_info("wb_gamma_n = 0x%04x\n", cmd
.wb_gamma_n
);
475 return audio_send_queue_pre(audio
, &cmd
, sizeof(cmd
));
478 static int audio_dsp_set_tx_iir(struct audio_in
*audio
)
480 struct audpre_cmd_iir_config_type cmd
;
482 memset(&cmd
, 0, sizeof(cmd
));
483 cmd
.cmd_id
= AUDPREPROC_CMD_CFG_IIR_TUNING_FILTER_PARAMS
;
485 if (audio
->iir_enable
) {
486 cmd
.active_flag
= AUDPREPROC_CMD_IIR_ACTIVE_FLAG_ENA
;
487 cmd
.num_bands
= audio
->iir
.num_bands
;
488 memcpy(&cmd
.iir_params
, &audio
->iir
.iir_params
,
489 sizeof(audio
->iir
.iir_params
));
491 cmd
.active_flag
= AUDPREPROC_CMD_IIR_ACTIVE_FLAG_DIS
;
494 pr_info("cmd_id = 0x%04x\n", cmd
.cmd_id
);
495 pr_info("active_flag = 0x%04x\n", cmd
.active_flag
);
497 return audio_send_queue_pre(audio
, &cmd
, sizeof(cmd
));
500 static int audio_in_dsp_enable(struct audio_in
*audio
, int enable
)
504 memset(&cmd
, 0, sizeof(cmd
));
505 cmd
.cmd_id
= AUDREC_CMD_CFG
;
506 cmd
.type_0
= enable
? AUDREC_CMD_TYPE_0_ENA
: AUDREC_CMD_TYPE_0_DIS
;
507 cmd
.type_0
|= (AUDREC_CMD_TYPE_0_UPDATE
| audio
->type
);
510 return audio_send_queue_rec(audio
, &cmd
, sizeof(cmd
));
513 static int audio_in_encoder_config(struct audio_in
*audio
)
515 audrec_cmd_arec0param_cfg cmd
;
516 uint16_t *data
= (void *) audio
->data
;
519 memset(&cmd
, 0, sizeof(cmd
));
520 cmd
.cmd_id
= AUDREC_CMD_AREC0PARAM_CFG
;
521 cmd
.ptr_to_extpkt_buffer_msw
= audio
->phys
>> 16;
522 cmd
.ptr_to_extpkt_buffer_lsw
= audio
->phys
;
523 cmd
.buf_len
= FRAME_NUM
; /* Both WAV and AAC use 8 frames */
524 cmd
.samp_rate_index
= audio
->samp_rate_index
;
525 cmd
.stereo_mode
= audio
->channel_mode
; /* 0 for mono, 1 for stereo */
527 cmd
.rec_quality
= 0x1C00;
529 /* prepare buffer pointers:
530 * Mono: 1024 samples + 4 halfword header
531 * Stereo: 2048 samples + 4 halfword header
533 * Mono/Stere: 768 + 4 halfword header
535 for (n
= 0; n
< FRAME_NUM
; n
++) {
536 audio
->in
[n
].data
= data
+ 4;
537 if (audio
->type
== AUDREC_CMD_TYPE_0_INDEX_WAV
)
538 data
+= (4 + (audio
->channel_mode
? 2048 : 1024));
539 else if (audio
->type
== AUDREC_CMD_TYPE_0_INDEX_AAC
)
543 return audio_send_queue_rec(audio
, &cmd
, sizeof(cmd
));
546 static int audio_dsp_read_buffer(struct audio_in
*audio
, uint32_t read_cnt
)
548 audrec_cmd_packet_ext_ptr cmd
;
550 memset(&cmd
, 0, sizeof(cmd
));
551 cmd
.cmd_id
= AUDREC_CMD_PACKET_EXT_PTR
;
552 /* Both WAV and AAC use AUDREC_CMD_TYPE_0 */
553 cmd
.type
= AUDREC_CMD_TYPE_0
;
554 cmd
.curr_rec_count_msw
= read_cnt
>> 16;
555 cmd
.curr_rec_count_lsw
= read_cnt
;
557 return audio_send_queue_recbs(audio
, &cmd
, sizeof(cmd
));
560 /* ------------------- device --------------------- */
562 static void audio_enable_agc(struct audio_in
*audio
, int enable
)
564 if (audio
->agc_enable
!= enable
) {
565 audio
->agc_enable
= enable
;
567 audio_dsp_set_agc(audio
);
571 static void audio_enable_ns(struct audio_in
*audio
, int enable
)
573 if (audio
->ns_enable
!= enable
) {
574 audio
->ns_enable
= enable
;
576 audio_dsp_set_ns(audio
);
580 static void audio_enable_tx_iir(struct audio_in
*audio
, int enable
)
582 if (audio
->iir_enable
!= enable
) {
583 audio
->iir_enable
= enable
;
585 audio_dsp_set_tx_iir(audio
);
589 static void audio_flush(struct audio_in
*audio
)
597 for (i
= 0; i
< FRAME_NUM
; i
++) {
598 audio
->in
[i
].size
= 0;
599 audio
->in
[i
].read
= 0;
603 static long audio_in_ioctl(struct file
*file
,
604 unsigned int cmd
, unsigned long arg
)
606 struct audio_in
*audio
= file
->private_data
;
609 if (cmd
== AUDIO_GET_STATS
) {
610 struct msm_audio_stats stats
;
611 stats
.byte_count
= atomic_read(&audio
->in_bytes
);
612 if (copy_to_user((void *) arg
, &stats
, sizeof(stats
)))
617 mutex_lock(&audio
->lock
);
620 rc
= audio_in_enable(audio
);
623 rc
= audio_in_disable(audio
);
627 if (audio
->stopped
) {
628 /* Make sure we're stopped and we wake any threads
629 * that might be blocked holding the read_lock.
630 * While audio->stopped read threads will always
633 wake_up(&audio
->wait
);
634 mutex_lock(&audio
->read_lock
);
636 mutex_unlock(&audio
->read_lock
);
638 case AUDIO_SET_CONFIG
: {
639 struct msm_audio_config cfg
;
640 if (copy_from_user(&cfg
, (void *) arg
, sizeof(cfg
))) {
644 if (cfg
.channel_count
== 1) {
645 cfg
.channel_count
= AUDREC_CMD_STEREO_MODE_MONO
;
646 } else if (cfg
.channel_count
== 2) {
647 cfg
.channel_count
= AUDREC_CMD_STEREO_MODE_STEREO
;
654 cfg
.type
= AUDREC_CMD_TYPE_0_INDEX_WAV
;
655 } else if (cfg
.type
== 1) {
656 cfg
.type
= AUDREC_CMD_TYPE_0_INDEX_AAC
;
661 audio
->samp_rate
= convert_samp_rate(cfg
.sample_rate
);
662 audio
->samp_rate_index
=
663 convert_dsp_samp_index(cfg
.sample_rate
);
664 audio
->channel_mode
= cfg
.channel_count
;
666 audio
->channel_mode
? STEREO_DATA_SIZE
668 audio
->type
= cfg
.type
;
672 case AUDIO_GET_CONFIG
: {
673 struct msm_audio_config cfg
;
674 cfg
.buffer_size
= audio
->buffer_size
;
675 cfg
.buffer_count
= FRAME_NUM
;
676 cfg
.sample_rate
= convert_samp_index(audio
->samp_rate
);
677 if (audio
->channel_mode
== AUDREC_CMD_STEREO_MODE_MONO
)
678 cfg
.channel_count
= 1;
680 cfg
.channel_count
= 2;
681 if (audio
->type
== AUDREC_CMD_TYPE_0_INDEX_WAV
)
688 if (copy_to_user((void *) arg
, &cfg
, sizeof(cfg
)))
697 mutex_unlock(&audio
->lock
);
701 static ssize_t
audio_in_read(struct file
*file
,
703 size_t count
, loff_t
*pos
)
705 struct audio_in
*audio
= file
->private_data
;
707 const char __user
*start
= buf
;
713 mutex_lock(&audio
->read_lock
);
715 rc
= wait_event_interruptible(
716 audio
->wait
, (audio
->in_count
> 0) || audio
->stopped
);
720 if (audio
->stopped
) {
725 index
= audio
->in_tail
;
726 data
= (uint8_t *) audio
->in
[index
].data
;
727 size
= audio
->in
[index
].size
;
729 if (copy_to_user(buf
, data
, size
)) {
733 spin_lock_irqsave(&audio
->dsp_lock
, flags
);
734 if (index
!= audio
->in_tail
) {
735 /* overrun -- data is invalid and we need to retry */
736 spin_unlock_irqrestore(&audio
->dsp_lock
, flags
);
739 audio
->in
[index
].size
= 0;
740 audio
->in_tail
= (audio
->in_tail
+ 1) & (FRAME_NUM
- 1);
742 spin_unlock_irqrestore(&audio
->dsp_lock
, flags
);
745 if (audio
->type
== AUDREC_CMD_TYPE_0_INDEX_AAC
)
748 pr_err("audio_in: short read\n");
751 if (audio
->type
== AUDREC_CMD_TYPE_0_INDEX_AAC
)
752 break; /* AAC only read one frame */
754 mutex_unlock(&audio
->read_lock
);
762 static ssize_t
audio_in_write(struct file
*file
,
763 const char __user
*buf
,
764 size_t count
, loff_t
*pos
)
769 static int audio_in_release(struct inode
*inode
, struct file
*file
)
771 struct audio_in
*audio
= file
->private_data
;
773 mutex_lock(&audio
->lock
);
774 audio_in_disable(audio
);
776 msm_adsp_put(audio
->audrec
);
777 msm_adsp_put(audio
->audpre
);
778 audio
->audrec
= NULL
;
779 audio
->audpre
= NULL
;
781 mutex_unlock(&audio
->lock
);
785 static struct audio_in the_audio_in
;
787 static int audio_in_open(struct inode
*inode
, struct file
*file
)
789 struct audio_in
*audio
= &the_audio_in
;
792 mutex_lock(&audio
->lock
);
798 /* Settings will be re-config at AUDIO_SET_CONFIG,
799 * but at least we need to have initial config
801 audio
->samp_rate
= RPC_AUD_DEF_SAMPLE_RATE_11025
;
802 audio
->samp_rate_index
= AUDREC_CMD_SAMP_RATE_INDX_11025
;
803 audio
->channel_mode
= AUDREC_CMD_STEREO_MODE_MONO
;
804 audio
->buffer_size
= MONO_DATA_SIZE
;
805 audio
->type
= AUDREC_CMD_TYPE_0_INDEX_WAV
;
807 rc
= audmgr_open(&audio
->audmgr
);
810 rc
= msm_adsp_get("AUDPREPROCTASK", &audio
->audpre
,
811 &audpre_adsp_ops
, audio
);
814 rc
= msm_adsp_get("AUDRECTASK", &audio
->audrec
,
815 &audrec_adsp_ops
, audio
);
824 file
->private_data
= audio
;
828 mutex_unlock(&audio
->lock
);
832 static long audpre_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
834 struct audio_in
*audio
= file
->private_data
;
836 uint16_t enable_mask
;
841 mutex_lock(&audio
->lock
);
843 case AUDIO_ENABLE_AUDPRE
: {
844 if (copy_from_user(&enable_mask
, (void *) arg
,
845 sizeof(enable_mask
)))
848 enable
= (enable_mask
& AGC_ENABLE
) ? 1 : 0;
849 audio_enable_agc(audio
, enable
);
850 enable
= (enable_mask
& NS_ENABLE
) ? 1 : 0;
851 audio_enable_ns(audio
, enable
);
852 enable
= (enable_mask
& IIR_ENABLE
) ? 1 : 0;
853 audio_enable_tx_iir(audio
, enable
);
856 case AUDIO_SET_AGC
: {
857 if (copy_from_user(&audio
->agc
, (void *) arg
,
861 pr_info("set agc\n");
862 for (i
= 0; i
< AGC_PARAM_SIZE
; i
++) \
863 pr_info("agc_params[%d] = 0x%04x\n", i
,
864 audio
->agc
.agc_params
[i
]);
869 if (copy_from_user(&audio
->ns
, (void *) arg
,
874 for (i
= 0; i
< NS_PARAM_SIZE
; i
++) \
875 pr_info("ns_params[%d] = 0x%04x\n",
876 i
, audio
->ns
.ns_params
[i
]);
880 case AUDIO_SET_TX_IIR
: {
881 if (copy_from_user(&audio
->iir
, (void *) arg
,
885 pr_info("set iir\n");
886 pr_info("iir.num_bands = 0x%04x\n", audio
->iir
.num_bands
);
887 for (i
= 0; i
< IIR_PARAM_SIZE
; i
++) \
888 pr_info("iir_params[%d] = 0x%04x\n",
889 i
, audio
->iir
.iir_params
[i
]);
902 mutex_unlock(&audio
->lock
);
906 static int audpre_open(struct inode
*inode
, struct file
*file
)
908 struct audio_in
*audio
= &the_audio_in
;
909 file
->private_data
= audio
;
913 static struct file_operations audio_fops
= {
914 .owner
= THIS_MODULE
,
915 .open
= audio_in_open
,
916 .release
= audio_in_release
,
917 .read
= audio_in_read
,
918 .write
= audio_in_write
,
919 .unlocked_ioctl
= audio_in_ioctl
,
922 static struct file_operations audpre_fops
= {
923 .owner
= THIS_MODULE
,
925 .unlocked_ioctl
= audpre_ioctl
,
928 struct miscdevice audio_in_misc
= {
929 .minor
= MISC_DYNAMIC_MINOR
,
930 .name
= "msm_pcm_in",
934 struct miscdevice audpre_misc
= {
935 .minor
= MISC_DYNAMIC_MINOR
,
936 .name
= "msm_audpre",
937 .fops
= &audpre_fops
,
940 static int __init
audio_in_init(void)
943 the_audio_in
.data
= dma_alloc_coherent(NULL
, DMASZ
,
944 &the_audio_in
.phys
, GFP_KERNEL
);
945 if (!the_audio_in
.data
) {
946 printk(KERN_ERR
"%s: Unable to allocate DMA buffer\n",
951 mutex_init(&the_audio_in
.lock
);
952 mutex_init(&the_audio_in
.read_lock
);
953 spin_lock_init(&the_audio_in
.dsp_lock
);
954 init_waitqueue_head(&the_audio_in
.wait
);
955 rc
= misc_register(&audio_in_misc
);
957 rc
= misc_register(&audpre_misc
);
959 misc_deregister(&audio_in_misc
);
964 device_initcall(audio_in_init
);