2 * Copyright (C) 2007 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 /*******************************************************************************
24 * Audio Driver Kernel Function
28 * Audio dl1 data1 playback
34 *------------------------------------------------------------------------------
40 *******************************************************************************/
43 /*****************************************************************************
44 * C O M P I L E R F L A G S
45 *****************************************************************************/
48 /*****************************************************************************
49 * E X T E R N A L R E F E R E N C E S
50 *****************************************************************************/
52 #include "AudDrv_Common.h"
53 #include "AudDrv_Def.h"
54 #include "AudDrv_Afe.h"
55 #include "AudDrv_Ana.h"
56 #include "AudDrv_Clk.h"
57 #include "AudDrv_Kernel.h"
58 #include "mt_soc_afe_control.h"
59 #include "mt_soc_digital_type.h"
60 #include "mt_soc_pcm_common.h"
62 #include <linux/kernel.h>
63 #include <linux/module.h>
64 #include <linux/init.h>
65 #include <linux/device.h>
66 #include <linux/slab.h>
68 #include <linux/completion.h>
70 #include <linux/delay.h>
71 #include <linux/interrupt.h>
72 #include <linux/dma-mapping.h>
73 #include <linux/vmalloc.h>
74 #include <linux/platform_device.h>
75 #include <linux/miscdevice.h>
76 #include <linux/wait.h>
77 #include <linux/spinlock.h>
78 #include <linux/sched.h>
79 #include <linux/wakelock.h>
80 #include <linux/semaphore.h>
81 #include <linux/jiffies.h>
82 #include <linux/proc_fs.h>
83 #include <linux/string.h>
84 #include <linux/mutex.h>
85 #include <linux/xlog.h>
86 #include <mach/irqs.h>
87 #include <asm/uaccess.h>
90 #include <mach/mt_reg_base.h>
91 #include <asm/div64.h>
92 #include <linux/aee.h>
93 #include <mach/upmu_common.h>
94 #include <mach/upmu_hw.h>
95 #include <mach/mt_gpio.h>
96 #include <mach/mt_typedefs.h>
97 #include <linux/clk.h>
98 #include <linux/delay.h>
99 #include <linux/platform_device.h>
100 #include <linux/slab.h>
101 #include <sound/core.h>
102 #include <sound/soc.h>
103 #include <sound/soc-dapm.h>
104 #include <sound/pcm.h>
105 //#include <asm/mach-types.h>
108 #include <linux/of.h>
109 #include <linux/of_irq.h>
110 #include <linux/of_address.h>
111 static unsigned int pin_extspkamp
, pin_vowclk
, pin_audclk
, pin_audmiso
, pin_audmosi
, pin_i2s1clk
, pin_i2s1dat
, pin_i2s1mclk
, pin_i2s1ws
;
112 static unsigned int pin_mode_audclk
, pin_mode_audmosi
, pin_mode_audmiso
, pin_mode_vowclk
, pin_mode_extspkamp
, pin_mode_i2s1clk
, pin_mode_i2s1dat
, pin_mode_i2s1mclk
, pin_mode_i2s1ws
;
113 static unsigned int if_config1
, if_config2
, if_config3
, if_config4
, if_config5
, if_config6
, if_config7
, if_config8
, if_config9
;
116 static AFE_MEM_CONTROL_T
*pMemControl
= NULL
;
117 static int mPlaybackSramState
= 0;
118 static struct snd_dma_buffer
*Dl1_Playback_dma_buf
= NULL
;
120 static DEFINE_SPINLOCK(auddrv_DLCtl_lock
);
122 static struct device
*mDev
= NULL
;
125 * function implementation
128 void StartAudioPcmHardware(void);
129 void StopAudioPcmHardware(void);
130 static int mtk_soc_dl1_probe(struct platform_device
*pdev
);
131 static int mtk_soc_pcm_dl1_close(struct snd_pcm_substream
*substream
);
132 static int mtk_asoc_pcm_dl1_new(struct snd_soc_pcm_runtime
*rtd
);
133 static int mtk_asoc_dl1_probe(struct snd_soc_platform
*platform
);
135 static bool mPrepareDone
= false;
137 #define USE_RATE SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000
138 #define USE_RATE_MIN 8000
139 #define USE_RATE_MAX 192000
140 #define USE_CHANNELS_MIN 1
141 #define USE_CHANNELS_MAX 2
142 #define USE_PERIODS_MIN 512
143 #define USE_PERIODS_MAX 8192
145 static struct snd_pcm_hardware mtk_pcm_dl1_hardware
=
147 .info
= (SNDRV_PCM_INFO_MMAP
|
148 SNDRV_PCM_INFO_INTERLEAVED
|
149 SNDRV_PCM_INFO_RESUME
|
150 SNDRV_PCM_INFO_MMAP_VALID
),
151 .formats
= SND_SOC_ADV_MT_FMTS
,
152 .rates
= SOC_HIGH_USE_RATE
,
153 .rate_min
= SOC_HIGH_USE_RATE_MIN
,
154 .rate_max
= SOC_HIGH_USE_RATE_MAX
,
155 .channels_min
= SOC_NORMAL_USE_CHANNELS_MIN
,
156 .channels_max
= SOC_NORMAL_USE_CHANNELS_MAX
,
157 .buffer_bytes_max
= Dl1_MAX_BUFFER_SIZE
,
158 .period_bytes_max
= Dl1_MAX_PERIOD_SIZE
,
159 .periods_min
= SOC_NORMAL_USE_PERIODS_MIN
,
160 .periods_max
= SOC_NORMAL_USE_PERIODS_MAX
,
164 static int mtk_pcm_dl1_stop(struct snd_pcm_substream
*substream
)
166 printk("%s \n", __func__
);
168 SetIrqEnable(Soc_Aud_IRQ_MCU_MODE_IRQ1_MCU_MODE
, false);
169 SetMemoryPathEnable(Soc_Aud_Digital_Block_MEM_DL1
, false);
171 // here start digital part
172 SetConnection(Soc_Aud_InterCon_DisConnect
, Soc_Aud_InterConnectionInput_I05
, Soc_Aud_InterConnectionOutput_O03
);
173 SetConnection(Soc_Aud_InterCon_DisConnect
, Soc_Aud_InterConnectionInput_I06
, Soc_Aud_InterConnectionOutput_O04
);
175 ClearMemBlock(Soc_Aud_Digital_Block_MEM_DL1
);
179 static snd_pcm_uframes_t
mtk_pcm_pointer(struct snd_pcm_substream
*substream
)
181 kal_int32 HW_memory_index
= 0;
182 kal_int32 HW_Cur_ReadIdx
= 0;
183 kal_uint32 Frameidx
= 0;
184 kal_int32 Afe_consumed_bytes
= 0;
185 AFE_BLOCK_T
*Afe_Block
= &pMemControl
->rBlock
;
186 //struct snd_pcm_runtime *runtime = substream->runtime;
187 PRINTK_AUD_DL1(" %s Afe_Block->u4DMAReadIdx = 0x%x\n", __func__
, Afe_Block
->u4DMAReadIdx
);
189 Auddrv_Dl1_Spinlock_lock();
191 // get total bytes to copy
192 //Frameidx = audio_bytes_to_frame(substream , Afe_Block->u4DMAReadIdx);
195 if (GetMemoryPathEnable(Soc_Aud_Digital_Block_MEM_DL1
) == true)
197 HW_Cur_ReadIdx
= Afe_Get_Reg(AFE_DL1_CUR
);
198 if (HW_Cur_ReadIdx
== 0)
200 PRINTK_AUDDRV("[Auddrv] HW_Cur_ReadIdx ==0 \n");
201 HW_Cur_ReadIdx
= Afe_Block
->pucPhysBufAddr
;
204 HW_memory_index
= (HW_Cur_ReadIdx
- Afe_Block
->pucPhysBufAddr
);
205 if (HW_memory_index
>= Afe_Block
->u4DMAReadIdx
)
207 Afe_consumed_bytes
= HW_memory_index
- Afe_Block
->u4DMAReadIdx
;
211 Afe_consumed_bytes
= Afe_Block
->u4BufferSize
+ HW_memory_index
- Afe_Block
->u4DMAReadIdx
;
214 Afe_consumed_bytes
= Align64ByteSize(Afe_consumed_bytes
);
216 Afe_Block
->u4DataRemained
-= Afe_consumed_bytes
;
217 Afe_Block
->u4DMAReadIdx
+= Afe_consumed_bytes
;
218 Afe_Block
->u4DMAReadIdx
%= Afe_Block
->u4BufferSize
;
219 PRINTK_AUD_DL1("[Auddrv] HW_Cur_ReadIdx =0x%x HW_memory_index = 0x%x Afe_consumed_bytes = 0x%x\n", HW_Cur_ReadIdx
, HW_memory_index
, Afe_consumed_bytes
);
220 Auddrv_Dl1_Spinlock_unlock();
222 return audio_bytes_to_frame(substream
, Afe_Block
->u4DMAReadIdx
);
226 Frameidx
= audio_bytes_to_frame(substream
, Afe_Block
->u4DMAReadIdx
);
227 Auddrv_Dl1_Spinlock_unlock();
232 static void SetDL1Buffer(struct snd_pcm_substream
*substream
,
233 struct snd_pcm_hw_params
*hw_params
)
235 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
236 AFE_BLOCK_T
*pblock
= &pMemControl
->rBlock
;
237 pblock
->pucPhysBufAddr
= runtime
->dma_addr
;
238 pblock
->pucVirtBufAddr
= runtime
->dma_area
;
239 pblock
->u4BufferSize
= runtime
->dma_bytes
;
240 pblock
->u4SampleNumMask
= 0x001f; // 32 byte align
241 pblock
->u4WriteIdx
= 0;
242 pblock
->u4DMAReadIdx
= 0;
243 pblock
->u4DataRemained
= 0;
244 pblock
->u4fsyncflag
= false;
245 pblock
->uResetFlag
= true;
246 printk("SetDL1Buffer u4BufferSize = %d pucVirtBufAddr = %p pucPhysBufAddr = 0x%x\n",
247 pblock
->u4BufferSize
, pblock
->pucVirtBufAddr
, pblock
->pucPhysBufAddr
);
248 // set dram address top hardware
249 Afe_Set_Reg(AFE_DL1_BASE
, pblock
->pucPhysBufAddr
, 0xffffffff);
250 Afe_Set_Reg(AFE_DL1_END
, pblock
->pucPhysBufAddr
+ (pblock
->u4BufferSize
- 1), 0xffffffff);
251 memset((void *)pblock
->pucVirtBufAddr
, 0, pblock
->u4BufferSize
);
255 static int mtk_pcm_dl1_params(struct snd_pcm_substream
*substream
,
256 struct snd_pcm_hw_params
*hw_params
)
258 //struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
260 PRINTK_AUDDRV("mtk_pcm_dl1_params \n");
262 /* runtime->dma_bytes has to be set manually to allow mmap */
263 substream
->runtime
->dma_bytes
= params_buffer_bytes(hw_params
);
265 if (mPlaybackSramState
== SRAM_STATE_PLAYBACKFULL
)
267 //substream->runtime->dma_bytes = AFE_INTERNAL_SRAM_SIZE;
268 substream
->runtime
->dma_area
= (unsigned char *)Get_Afe_SramBase_Pointer();
269 substream
->runtime
->dma_addr
= AFE_INTERNAL_SRAM_PHY_BASE
;
270 AudDrv_Allocate_DL1_Buffer(mDev
, substream
->runtime
->dma_bytes
);
274 substream
->runtime
->dma_bytes
= params_buffer_bytes(hw_params
);
275 substream
->runtime
->dma_area
= Dl1_Playback_dma_buf
->area
;
276 substream
->runtime
->dma_addr
= Dl1_Playback_dma_buf
->addr
;
277 SetDL1Buffer(substream
, hw_params
);
280 PRINTK_AUDDRV("dma_bytes = %zu dma_area = %p dma_addr = 0x%lx\n",
281 substream
->runtime
->dma_bytes
, substream
->runtime
->dma_area
, (long)substream
->runtime
->dma_addr
);
285 static int mtk_pcm_dl1_hw_free(struct snd_pcm_substream
*substream
)
287 PRINTK_AUDDRV("mtk_pcm_dl1_hw_free \n");
292 static struct snd_pcm_hw_constraint_list constraints_sample_rates
=
294 .count
= ARRAY_SIZE(soc_high_supported_sample_rates
),
295 .list
= soc_high_supported_sample_rates
,
299 static int mtk_pcm_dl1_open(struct snd_pcm_substream
*substream
)
302 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
303 PRINTK_AUDDRV("mtk_pcm_dl1_open\n");
305 AfeControlSramLock();
306 if (GetSramState() == SRAM_STATE_FREE
)
308 mtk_pcm_dl1_hardware
.buffer_bytes_max
= GetPLaybackSramFullSize();
309 mPlaybackSramState
= SRAM_STATE_PLAYBACKFULL
;
310 SetSramState(mPlaybackSramState
);
314 mtk_pcm_dl1_hardware
.buffer_bytes_max
= GetPLaybackDramSize();
315 mPlaybackSramState
= SRAM_STATE_PLAYBACKDRAM
;
317 AfeControlSramUnLock();
318 if (mPlaybackSramState
== SRAM_STATE_PLAYBACKDRAM
)
323 printk("mtk_pcm_dl1_hardware.buffer_bytes_max = %zu mPlaybackSramState = %d\n", mtk_pcm_dl1_hardware
.buffer_bytes_max
, mPlaybackSramState
);
324 runtime
->hw
= mtk_pcm_dl1_hardware
;
327 memcpy((void *)(&(runtime
->hw
)), (void *)&mtk_pcm_dl1_hardware
, sizeof(struct snd_pcm_hardware
));
328 pMemControl
= Get_Mem_ControlT(Soc_Aud_Digital_Block_MEM_DL1
);
330 ret
= snd_pcm_hw_constraint_list(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
331 &constraints_sample_rates
);
335 printk("snd_pcm_hw_constraint_integer failed\n");
338 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
340 printk("SNDRV_PCM_STREAM_PLAYBACK mtkalsa_dl1playback_constraints\n");
344 printk("SNDRV_PCM_STREAM_CAPTURE mtkalsa_dl1playback_constraints\n");
349 printk("ret < 0 mtk_soc_pcm_dl1_close\n");
350 mtk_soc_pcm_dl1_close(substream
);
354 //PRINTK_AUDDRV("mtk_pcm_dl1_open return\n");
358 static int mtk_soc_pcm_dl1_close(struct snd_pcm_substream
*substream
)
360 printk("%s \n", __func__
);
361 if (mPrepareDone
== true)
364 SetMemoryPathEnable(Soc_Aud_Digital_Block_I2S_OUT_DAC
, false);
365 if (GetI2SDacEnable() == false)
367 SetI2SDacEnable(false);
370 RemoveMemifSubStream(Soc_Aud_Digital_Block_MEM_DL1
, substream
);
373 mPrepareDone
= false;
376 if (mPlaybackSramState
== SRAM_STATE_PLAYBACKDRAM
)
378 AudDrv_Emi_Clk_Off();
380 AfeControlSramLock();
381 ClearSramState(mPlaybackSramState
);
382 mPlaybackSramState
= GetSramState();
383 AfeControlSramUnLock();
388 static int mtk_pcm_prepare(struct snd_pcm_substream
*substream
)
391 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
392 if (mPrepareDone
== false)
394 printk("%s format = %d SNDRV_PCM_FORMAT_S32_LE = %d SNDRV_PCM_FORMAT_U32_LE = %d \n", __func__
, runtime
->format
, SNDRV_PCM_FORMAT_S32_LE
, SNDRV_PCM_FORMAT_U32_LE
);
395 SetMemifSubStream(Soc_Aud_Digital_Block_MEM_DL1
, substream
);
397 if (runtime
->format
== SNDRV_PCM_FORMAT_S32_LE
|| runtime
->format
== SNDRV_PCM_FORMAT_U32_LE
)
399 SetMemIfFetchFormatPerSample(Soc_Aud_Digital_Block_MEM_DL1
, AFE_WLEN_32_BIT_ALIGN_8BIT_0_24BIT_DATA
);
400 SetMemIfFetchFormatPerSample(Soc_Aud_Digital_Block_MEM_DL2
, AFE_WLEN_32_BIT_ALIGN_8BIT_0_24BIT_DATA
);
401 SetoutputConnectionFormat(OUTPUT_DATA_FORMAT_24BIT
, Soc_Aud_InterConnectionOutput_O03
);
402 SetoutputConnectionFormat(OUTPUT_DATA_FORMAT_24BIT
, Soc_Aud_InterConnectionOutput_O04
);
403 mI2SWLen
= Soc_Aud_I2S_WLEN_WLEN_32BITS
;
407 SetMemIfFetchFormatPerSample(Soc_Aud_Digital_Block_MEM_DL1
, AFE_WLEN_16_BIT
);
408 SetMemIfFetchFormatPerSample(Soc_Aud_Digital_Block_MEM_DL2
, AFE_WLEN_16_BIT
);
409 SetoutputConnectionFormat(OUTPUT_DATA_FORMAT_16BIT
, Soc_Aud_InterConnectionOutput_O03
);
410 SetoutputConnectionFormat(OUTPUT_DATA_FORMAT_16BIT
, Soc_Aud_InterConnectionOutput_O04
);
411 mI2SWLen
= Soc_Aud_I2S_WLEN_WLEN_16BITS
;
414 SetSampleRate(Soc_Aud_Digital_Block_MEM_I2S
, runtime
->rate
);
417 if (GetMemoryPathEnable(Soc_Aud_Digital_Block_I2S_OUT_DAC
) == false)
419 SetMemoryPathEnable(Soc_Aud_Digital_Block_I2S_OUT_DAC
, true);
420 SetI2SDacOut(substream
->runtime
->rate
, false, mI2SWLen
);
421 SetI2SDacEnable(true);
425 SetMemoryPathEnable(Soc_Aud_Digital_Block_I2S_OUT_DAC
, true);
427 // here to set interrupt_distributor
428 SetIrqMcuCounter(Soc_Aud_IRQ_MCU_MODE_IRQ1_MCU_MODE
, runtime
->period_size
);
429 SetIrqMcuSampleRate(Soc_Aud_IRQ_MCU_MODE_IRQ1_MCU_MODE
, runtime
->rate
);
438 static int mtk_pcm_dl1_start(struct snd_pcm_substream
*substream
)
440 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
441 printk("%s\n", __func__
);
442 // here start digital part
444 SetConnection(Soc_Aud_InterCon_Connection
, Soc_Aud_InterConnectionInput_I05
, Soc_Aud_InterConnectionOutput_O03
);
445 SetConnection(Soc_Aud_InterCon_Connection
, Soc_Aud_InterConnectionInput_I06
, Soc_Aud_InterConnectionOutput_O04
);
447 SetIrqEnable(Soc_Aud_IRQ_MCU_MODE_IRQ1_MCU_MODE
, true);
449 SetSampleRate(Soc_Aud_Digital_Block_MEM_DL1
, runtime
->rate
);
450 SetChannels(Soc_Aud_Digital_Block_MEM_DL1
, runtime
->channels
);
451 SetMemoryPathEnable(Soc_Aud_Digital_Block_MEM_DL1
, true);
455 #ifdef DENALI_FPGA_EARLYPORTING //ccc early porting, copy from TurnOnDacPower() and ADC_LOOP_DAC_Func()
456 printk("%s pcm loopback_b \n", __func__
);
457 Ana_Set_Reg(AFE_AUDIO_TOP_CON0
, 0x0000, 0xffff); //power on clock
459 Ana_Set_Reg(AFUNC_AUD_CON2
, 0x0006, 0xffffffff);
460 Ana_Set_Reg(AFUNC_AUD_CON0
, 0xc3a1, 0xffffffff); //sdm audio fifo clock power on
461 Ana_Set_Reg(AFUNC_AUD_CON2
, 0x0003, 0xffffffff); //sdm power on
462 Ana_Set_Reg(AFUNC_AUD_CON2
, 0x000b, 0xffffffff); //sdm fifo enable
463 Ana_Set_Reg(AFE_DL_SDM_CON1
, 0x001e, 0xffffffff); //set attenuation gain
464 Ana_Set_Reg(AFE_UL_DL_CON0
, 0x0001, 0xffffffff); //[0] afe enable
466 //Ana_Set_Reg(AFE_UL_SRC0_CON0_H, 0x0000 , 0x0010); // UL1
467 // Ana_Set_Reg(AFE_UL_SRC0_CON0_L, 0x0001, 0xffff); //power on uplink
468 //Ana_Set_Reg(AFE_PMIC_NEWIF_CFG0, 0x0380, 0xffff); //MTKIF
469 //Ana_Set_Reg(AFE_DL_SRC2_CON0_H, 0x0800, 0xffff); //DL
470 //Ana_Set_Reg(AFE_DL_SRC2_CON0_L, 0x0001, 0xffff); //DL
472 // Ana_Set_Reg(AFE_PMIC_NEWIF_CFG0 , 0 << 12 | 0x330 , 0xffffffff); //8k sample rate
473 Ana_Set_Reg(AFE_PMIC_NEWIF_CFG0
, 8 << 12 | 0x330 , 0xffffffff); //48k sample rate
475 // Ana_Set_Reg(AFE_DL_SRC2_CON0_H , 0 << 12 | 0x300 , 0xffffffff);//8k sample rate
476 Ana_Set_Reg(AFE_DL_SRC2_CON0_H
, 8 << 12 | 0x300 , 0xffffffff); //48k sample rate
477 Ana_Set_Reg(AFE_DL_SRC2_CON0_L
, 0x0001 , 0xffffffff); //turn off mute function and turn on dl
478 Ana_Set_Reg(PMIC_AFE_TOP_CON0
, 0x0000 , 0xffffffff); //set DL in normal path, not from sine gen table
479 Afe_Set_Reg(FPGA_CFG1
, 0x1, 0xffff); // must set in FPGA platform for PMIC digital loopback
481 printk("%s pcm loopback_e \n", __func__
);
488 static int mtk_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
490 PRINTK_AUDDRV("mtk_pcm_trigger cmd = %d\n", cmd
);
493 case SNDRV_PCM_TRIGGER_START
:
494 case SNDRV_PCM_TRIGGER_RESUME
:
495 return mtk_pcm_dl1_start(substream
);
496 case SNDRV_PCM_TRIGGER_STOP
:
497 case SNDRV_PCM_TRIGGER_SUSPEND
:
498 return mtk_pcm_dl1_stop(substream
);
503 static int mtk_pcm_copy(struct snd_pcm_substream
*substream
,
504 int channel
, snd_pcm_uframes_t pos
,
505 void __user
*dst
, snd_pcm_uframes_t count
)
507 AFE_BLOCK_T
*Afe_Block
= NULL
;
508 int copy_size
= 0, Afe_WriteIdx_tmp
;
510 //struct snd_pcm_runtime *runtime = substream->runtime;
511 char *data_w_ptr
= (char *)dst
;
512 PRINTK_AUD_DL1("mtk_pcm_copy pos = %lu count = %lu\n ", pos
, count
);
513 // get total bytes to copy
514 count
= audio_frame_to_bytes(substream
, count
);
516 // check which memif nned to be write
517 Afe_Block
= &pMemControl
->rBlock
;
519 PRINTK_AUD_DL1("AudDrv_write WriteIdx=0x%x, ReadIdx=0x%x, DataRemained=0x%x \n",
520 Afe_Block
->u4WriteIdx
, Afe_Block
->u4DMAReadIdx
, Afe_Block
->u4DataRemained
);
522 if (Afe_Block
->u4BufferSize
== 0)
524 printk("AudDrv_write: u4BufferSize=0 Error");
528 spin_lock_irqsave(&auddrv_DLCtl_lock
, flags
);
529 copy_size
= Afe_Block
->u4BufferSize
- Afe_Block
->u4DataRemained
; // free space of the buffer
530 spin_unlock_irqrestore(&auddrv_DLCtl_lock
, flags
);
531 if (count
<= copy_size
)
543 copy_size
= Align64ByteSize(copy_size
);
544 PRINTK_AUD_DL1("copy_size=0x%x, count=0x%x \n", copy_size
,(unsigned int) count
);
548 spin_lock_irqsave(&auddrv_DLCtl_lock
, flags
);
549 Afe_WriteIdx_tmp
= Afe_Block
->u4WriteIdx
;
550 spin_unlock_irqrestore(&auddrv_DLCtl_lock
, flags
);
552 if (Afe_WriteIdx_tmp
+ copy_size
< Afe_Block
->u4BufferSize
) // copy once
554 if (!access_ok(VERIFY_READ
, data_w_ptr
, copy_size
))
556 PRINTK_AUDDRV("AudDrv_write 0ptr invalid data_w_ptr=%p, size=%d", data_w_ptr
, copy_size
);
557 PRINTK_AUDDRV("AudDrv_write u4BufferSize=%d, u4DataRemained=%d", Afe_Block
->u4BufferSize
, Afe_Block
->u4DataRemained
);
561 PRINTK_AUD_DL1("memcpy Afe_Block->pucVirtBufAddr+Afe_WriteIdx= %p data_w_ptr = %p copy_size = 0x%x\n",
562 Afe_Block
->pucVirtBufAddr
+ Afe_WriteIdx_tmp
, data_w_ptr
, copy_size
);
563 if (copy_from_user((Afe_Block
->pucVirtBufAddr
+ Afe_WriteIdx_tmp
), data_w_ptr
, copy_size
))
565 PRINTK_AUDDRV("AudDrv_write Fail copy from user \n");
570 spin_lock_irqsave(&auddrv_DLCtl_lock
, flags
);
571 Afe_Block
->u4DataRemained
+= copy_size
;
572 Afe_Block
->u4WriteIdx
= Afe_WriteIdx_tmp
+ copy_size
;
573 Afe_Block
->u4WriteIdx
%= Afe_Block
->u4BufferSize
;
574 spin_unlock_irqrestore(&auddrv_DLCtl_lock
, flags
);
575 data_w_ptr
+= copy_size
;
578 PRINTK_AUD_DL1("AudDrv_write finish1, copy_size:%x, WriteIdx:%x, ReadIdx=%x, DataRemained:%x, count=%d \r\n",
579 copy_size
, Afe_Block
->u4WriteIdx
, Afe_Block
->u4DMAReadIdx
, Afe_Block
->u4DataRemained
, (int)count
);
584 kal_uint32 size_1
= 0, size_2
= 0;
585 size_1
= Align64ByteSize((Afe_Block
->u4BufferSize
- Afe_WriteIdx_tmp
));
586 size_2
= Align64ByteSize((copy_size
- size_1
));
587 PRINTK_AUD_DL1("size_1=0x%x, size_2=0x%x \n", size_1
, size_2
);
588 if (!access_ok(VERIFY_READ
, data_w_ptr
, size_1
))
590 printk("AudDrv_write 1ptr invalid data_w_ptr=%p, size_1=%d", data_w_ptr
, size_1
);
591 printk("AudDrv_write u4BufferSize=%d, u4DataRemained=%d", Afe_Block
->u4BufferSize
, Afe_Block
->u4DataRemained
);
596 PRINTK_AUD_DL1("mcmcpy Afe_Block->pucVirtBufAddr+Afe_WriteIdx= %p data_w_ptr = %p size_1 = %x\n",
597 Afe_Block
->pucVirtBufAddr
+ Afe_WriteIdx_tmp
, data_w_ptr
, size_1
);
598 if ((copy_from_user((Afe_Block
->pucVirtBufAddr
+ Afe_WriteIdx_tmp
), data_w_ptr
, (unsigned int)size_1
)))
600 PRINTK_AUDDRV("AudDrv_write Fail 1 copy from user");
604 spin_lock_irqsave(&auddrv_DLCtl_lock
, flags
);
605 Afe_Block
->u4DataRemained
+= size_1
;
606 Afe_Block
->u4WriteIdx
= Afe_WriteIdx_tmp
+ size_1
;
607 Afe_Block
->u4WriteIdx
%= Afe_Block
->u4BufferSize
;
608 Afe_WriteIdx_tmp
= Afe_Block
->u4WriteIdx
;
609 spin_unlock_irqrestore(&auddrv_DLCtl_lock
, flags
);
611 if (!access_ok(VERIFY_READ
, data_w_ptr
+ size_1
, size_2
))
613 PRINTK_AUDDRV("AudDrv_write 2ptr invalid data_w_ptr=%p, size_1=%d, size_2=%d", data_w_ptr
, size_1
, size_2
);
614 PRINTK_AUDDRV("AudDrv_write u4BufferSize=%d, u4DataRemained=%d", Afe_Block
->u4BufferSize
, Afe_Block
->u4DataRemained
);
619 PRINTK_AUD_DL1("mcmcpy Afe_Block->pucVirtBufAddr+Afe_WriteIdx= %p data_w_ptr+size_1 = %p size_2 = %x\n",
620 Afe_Block
->pucVirtBufAddr
+ Afe_WriteIdx_tmp
, data_w_ptr
+ size_1
, (unsigned int)size_2
);
621 if ((copy_from_user((Afe_Block
->pucVirtBufAddr
+ Afe_WriteIdx_tmp
), (data_w_ptr
+ size_1
), size_2
)))
623 PRINTK_AUDDRV("AudDrv_write Fail 2 copy from user");
627 spin_lock_irqsave(&auddrv_DLCtl_lock
, flags
);
629 Afe_Block
->u4DataRemained
+= size_2
;
630 Afe_Block
->u4WriteIdx
= Afe_WriteIdx_tmp
+ size_2
;
631 Afe_Block
->u4WriteIdx
%= Afe_Block
->u4BufferSize
;
632 spin_unlock_irqrestore(&auddrv_DLCtl_lock
, flags
);
634 data_w_ptr
+= copy_size
;
636 PRINTK_AUD_DL1("AudDrv_write finish2, copy size:%x, WriteIdx:%x,ReadIdx=%x DataRemained:%x \r\n",
637 copy_size
, Afe_Block
->u4WriteIdx
, Afe_Block
->u4DMAReadIdx
, Afe_Block
->u4DataRemained
);
643 static int mtk_pcm_silence(struct snd_pcm_substream
*substream
,
644 int channel
, snd_pcm_uframes_t pos
,
645 snd_pcm_uframes_t count
)
647 PRINTK_AUDDRV("%s \n", __func__
);
648 return 0; /* do nothing */
651 static void *dummy_page
[2];
653 static struct page
*mtk_pcm_page(struct snd_pcm_substream
*substream
,
654 unsigned long offset
)
656 PRINTK_AUDDRV("%s \n", __func__
);
657 return virt_to_page(dummy_page
[substream
->stream
]); /* the same page */
660 static struct snd_pcm_ops mtk_afe_ops
=
662 .open
= mtk_pcm_dl1_open
,
663 .close
= mtk_soc_pcm_dl1_close
,
664 .ioctl
= snd_pcm_lib_ioctl
,
665 .hw_params
= mtk_pcm_dl1_params
,
666 .hw_free
= mtk_pcm_dl1_hw_free
,
667 .prepare
= mtk_pcm_prepare
,
668 .trigger
= mtk_pcm_trigger
,
669 .pointer
= mtk_pcm_pointer
,
670 .copy
= mtk_pcm_copy
,
671 .silence
= mtk_pcm_silence
,
672 .page
= mtk_pcm_page
,
675 static struct snd_soc_platform_driver mtk_soc_platform
=
678 .pcm_new
= mtk_asoc_pcm_dl1_new
,
679 .probe
= mtk_asoc_dl1_probe
,
682 static int mtk_soc_dl1_probe(struct platform_device
*pdev
)
685 PRINTK_AUDDRV("%s \n", __func__
);
687 pdev
->dev
.coherent_dma_mask
= DMA_BIT_MASK(64);
688 if (!pdev
->dev
.dma_mask
)
690 pdev
->dev
.dma_mask
= &pdev
->dev
.coherent_dma_mask
;
693 if (pdev
->dev
.of_node
)
695 dev_set_name(&pdev
->dev
, "%s", MT_SOC_DL1_PCM
);
698 PRINTK_AUDDRV("%s: dev name %s\n", __func__
, dev_name(&pdev
->dev
));
701 ret
= Register_Aud_Irq(&pdev
->dev
, MT6735_AFE_MCU_IRQ_LINE
);
706 return snd_soc_register_platform(&pdev
->dev
,
710 static int mtk_asoc_pcm_dl1_new(struct snd_soc_pcm_runtime
*rtd
)
713 PRINTK_AUDDRV("%s\n", __func__
);
718 static int mtk_asoc_dl1_probe(struct snd_soc_platform
*platform
)
720 PRINTK_AUDDRV("mtk_asoc_dl1_probe\n");
722 AudDrv_Allocate_mem_Buffer(platform
->dev
, Soc_Aud_Digital_Block_MEM_DL1
, Dl1_MAX_BUFFER_SIZE
);
723 Dl1_Playback_dma_buf
= Get_Mem_Buffer(Soc_Aud_Digital_Block_MEM_DL1
);
727 static int mtk_afe_remove(struct platform_device
*pdev
)
729 PRINTK_AUDDRV("%s \n", __func__
);
730 snd_soc_unregister_platform(&pdev
->dev
);
735 extern void *AFE_BASE_ADDRESS
;
736 u32 afe_irq_number
= 0;
739 static const struct of_device_id mt_soc_pcm_dl1_of_ids
[] =
741 { .compatible
= "mediatek,mt_soc_pcm_dl1", },
745 static int Auddrv_Reg_map_new(void)
747 struct device_node
*node
= NULL
;
749 node
= of_find_compatible_node(NULL
, NULL
, "mediatek,mt_soc_pcm_dl1");
752 /* Setup IO addresses */
753 AFE_BASE_ADDRESS
= of_iomap(node
, 0);
754 printk("[mt_soc_pcm_dl1] AFE_BASE_ADDRESS=0x%p\n", AFE_BASE_ADDRESS
);
758 printk("[mt_soc_pcm_dl1] node NULL, can't iomap AFE_BASE!!!\n");
760 of_property_read_u32(node
, "reg", &AFE_BASE_PHY
);
761 printk("[mt_soc_pcm_dl1] AFE_BASE_PHY=0x%x\n", AFE_BASE_PHY
);
764 afe_irq_number
= irq_of_parse_and_map(node
, 0);
765 printk("[mt_soc_pcm_dl1] afe_irq_number=0x%x\n", afe_irq_number
);
768 printk("[mt_soc_pcm_dl1] get afe_irq_number failed!!!\n");
773 static int Auddrv_OF_ParseGPIO(void)
775 struct device_node
*node
= NULL
;
777 node
= of_find_compatible_node(NULL
, NULL
, "mediatek,mt_soc_pcm_dl1");
790 if (of_property_read_u32_index(node
, "audclk-gpio", 0, &pin_audclk
))
793 printk("audclk-gpio get pin fail!!!\n");
795 if (of_property_read_u32_index(node
, "audclk-gpio", 1, &pin_mode_audclk
))
798 printk("audclk-gpio get pin_mode fail!!!\n");
801 if (of_property_read_u32_index(node
, "audmiso-gpio", 0, &pin_audmiso
))
804 printk("audmiso-gpio get pin fail!!!\n");
806 if (of_property_read_u32_index(node
, "audmiso-gpio", 1, &pin_mode_audmiso
))
809 printk("audmiso-gpio get pin_mode fail!!!\n");
812 if (of_property_read_u32_index(node
, "audmosi-gpio", 0, &pin_audmosi
))
815 printk("audmosi-gpio get pin fail!!!\n");
817 if (of_property_read_u32_index(node
, "audmosi-gpio", 1, &pin_mode_audmosi
))
820 printk("audmosi-gpio get pin_mode fail!!!\n");
823 if (of_property_read_u32_index(node
, "vowclk-gpio", 0, &pin_vowclk
))
826 printk("vowclk-gpio get pin fail!!!\n");
828 if (of_property_read_u32_index(node
, "vowclk-gpio", 1, &pin_mode_vowclk
))
831 printk("vowclk-gpio get pin_mode fail!!!\n");
834 if (of_property_read_u32_index(node
, "extspkamp-gpio", 0, &pin_extspkamp
))
837 printk("extspkamp-gpio get pin fail!!!\n");
839 if (of_property_read_u32_index(node
, "extspkamp-gpio", 1, &pin_mode_extspkamp
))
842 printk("extspkamp-gpio get pin_mode fail!!!\n");
845 if (of_property_read_u32_index(node
, "i2s1clk-gpio", 0, &pin_i2s1clk
))
848 printk("i2s1clk-gpio get pin fail!!!\n");
850 if (of_property_read_u32_index(node
, "i2s1clk-gpio", 1, &pin_mode_i2s1clk
))
853 printk("i2s1clk-gpio get pin_mode fail!!!\n");
856 if (of_property_read_u32_index(node
, "i2s1dat-gpio", 0, &pin_i2s1dat
))
859 printk("i2s1dat-gpio get pin fail!!!\n");
861 if (of_property_read_u32_index(node
, "i2s1dat-gpio", 1, &pin_mode_i2s1dat
))
864 printk("i2s1dat-gpio get pin_mode fail!!!\n");
867 if (of_property_read_u32_index(node
, "i2s1mclk-gpio", 0, &pin_i2s1mclk
))
870 printk("i2s1mclk-gpio get pin fail!!!\n");
872 if (of_property_read_u32_index(node
, "i2s1mclk-gpio", 1, &pin_mode_i2s1mclk
))
875 printk("i2s1mclk-gpio get pin_mode fail!!!\n");
878 if (of_property_read_u32_index(node
, "i2s1ws-gpio", 0, &pin_i2s1ws
))
881 printk("i2s1ws-gpio get pin fail!!!\n");
883 if (of_property_read_u32_index(node
, "i2s1ws-gpio", 1, &pin_mode_i2s1ws
))
886 printk("i2s1ws-gpio get pin_mode fail!!!\n");
889 printk("Auddrv_OF_ParseGPIO pin_audclk=%d, pin_audmiso=%d, pin_audmosi=%d \n", pin_audclk
, pin_audmiso
, pin_audmosi
);
890 printk("Auddrv_OF_ParseGPIO pin_vowclk=%d, pin_extspkamp=%d \n", pin_vowclk
, pin_extspkamp
);
891 printk("Auddrv_OF_ParseGPIO pin_i2s1clk=%d, pin_i2s1dat=%d, pin_i2s1mclk=%d, pin_i2s1ws=%d \n", pin_i2s1clk
, pin_i2s1dat
, pin_i2s1mclk
, pin_i2s1ws
);
895 printk("Auddrv_OF_ParseGPIO node NULL!!!\n");
901 int GetGPIO_Info(int type
, int *pin
, int *pinmode
)
908 *pin
= pin_audclk
| 0x80000000;
909 *pinmode
= pin_mode_audclk
;
913 printk("GetGPIO_Info type %d fail!!!\n", type
);
919 case 2: //pin_audmiso
922 *pin
= pin_audmiso
| 0x80000000;
923 *pinmode
= pin_mode_audmiso
;
927 printk("GetGPIO_Info type %d fail!!!\n", type
);
933 case 3: //pin_audmosi
936 *pin
= pin_audmosi
| 0x80000000;
937 *pinmode
= pin_mode_audmosi
;
941 printk("GetGPIO_Info type %d fail!!!\n", type
);
950 *pin
= pin_vowclk
| 0x80000000;
951 *pinmode
= pin_mode_vowclk
;
955 printk("GetGPIO_Info type %d fail!!!\n", type
);
961 case 5: //pin_extspkamp
964 *pin
= pin_extspkamp
| 0x80000000;
965 *pinmode
= pin_mode_extspkamp
;
969 printk("GetGPIO_Info type %d fail!!!\n", type
);
975 case 6: //pin_i2s1clk
978 *pin
= pin_i2s1clk
| 0x80000000;
979 *pinmode
= pin_mode_i2s1clk
;
983 printk("GetGPIO_Info type %d fail!!!\n", type
);
989 case 7: //pin_i2s1dat
992 *pin
= pin_i2s1dat
| 0x80000000;
993 *pinmode
= pin_mode_i2s1dat
;
997 printk("GetGPIO_Info type %d fail!!!\n", type
);
1003 case 8: //pin_i2s1mclk
1004 if (if_config8
== 1)
1006 *pin
= pin_i2s1mclk
| 0x80000000;
1007 *pinmode
= pin_mode_i2s1mclk
;
1011 printk("GetGPIO_Info type %d fail!!!\n", type
);
1017 case 9: //pin_i2s1ws
1018 if (if_config9
== 1)
1020 *pin
= pin_i2s1ws
| 0x80000000;
1021 *pinmode
= pin_mode_i2s1ws
;
1025 printk("GetGPIO_Info type %d fail!!!\n", type
);
1034 printk("Auddrv_OF_ParseGPIO invalid type=%d!!!\n", type
);
1039 EXPORT_SYMBOL(GetGPIO_Info
);
1042 static struct platform_driver mtk_afe_driver
=
1045 .name
= MT_SOC_DL1_PCM
,
1046 .owner
= THIS_MODULE
,
1048 .of_match_table
= mt_soc_pcm_dl1_of_ids
,
1051 .probe
= mtk_soc_dl1_probe
,
1052 .remove
= mtk_afe_remove
,
1056 static struct platform_device
*soc_mtkafe_dev
;
1059 static int __init
mtk_soc_platform_init(void)
1062 PRINTK_AUDDRV("%s \n", __func__
);
1064 Auddrv_Reg_map_new();
1065 ret
= Register_Aud_Irq(NULL
, afe_irq_number
);
1066 Auddrv_OF_ParseGPIO();
1068 soc_mtkafe_dev
= platform_device_alloc(MT_SOC_DL1_PCM
, -1);
1069 if (!soc_mtkafe_dev
)
1074 ret
= platform_device_add(soc_mtkafe_dev
);
1077 platform_device_put(soc_mtkafe_dev
);
1081 ret
= platform_driver_register(&mtk_afe_driver
);
1085 module_init(mtk_soc_platform_init
);
1087 static void __exit
mtk_soc_platform_exit(void)
1089 PRINTK_AUDDRV("%s \n", __func__
);
1091 platform_driver_unregister(&mtk_afe_driver
);
1093 module_exit(mtk_soc_platform_exit
);
1095 MODULE_DESCRIPTION("AFE PCM module platform driver");
1096 MODULE_LICENSE("GPL");