aboutsummaryrefslogtreecommitdiff
path: root/sound/soc/mediatek/common/auddrv_underflow_mach.c
blob: 7b0ab96a9edb77f78cf1c297b87b4986f1242c76 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*******************************************************************************
 *
 * Filename:
 * ---------
 *   AudDrv_devtree_parser.c
 *
 * Project:
 * --------
 *
 *
 * Description:
 * ------------
 *   AudDrv_devtree_parser
 *
 * Author:
 * -------
 *   Chipeng Chang (mtk02308)
 *
 *------------------------------------------------------------------------------
 * $Revision: #1 $
 * $Modtime:$
 * $Log:$
 *
 *
 *******************************************************************************/

#include <linux/aee.h>
#include "auddrv_underflow_mach.h"

#define UnderflowrecordNumber (20)

static bool bEnableDump = false;
// default setting for samplerate and interrupt count
static unsigned int mDlSamplerate =44100;
static const unsigned int DlSampleRateUpperBound = 192000;
static unsigned int InterruptSample =1024;
static const unsigned int InterruptSampleUpperBound = 192000;
//static bool bDumpInit = false;


static unsigned int mDL1InterruptInterval = 0;
static unsigned int mDL1_Interrupt_Interval_Limit = 0;
static unsigned int mDl1Numerator =  8;   //1.6
static unsigned int mDl1denominator = 5;
static unsigned long long Irq_time_t1 = 0, Irq_time_t2 = 0;
static bool bAudioInterruptChange = false;

static unsigned long long UnderflowTime[UnderflowrecordNumber] = {0};
static unsigned int UnderflowCounter = 0;
static unsigned int UnderflowThreshold =3;
static void ClearInterruptTiming(void);
static void DumpUnderFlowTime(void);
static void ClearUnderFlowTime(void);

void Auddrv_Aee_Dump(void);
void Auddrv_Set_Interrupt_Changed(bool bChange);


void SetUnderFlowThreshold(unsigned int Threshold)
{
    UnderflowThreshold = Threshold;
}


// base on devtree name to pares dev tree.
void Auddrv_Aee_Dump(void)
{
    printk("+%s\n", __func__);
    if(bEnableDump == true)
    {
        aee_kernel_exception_api(__FILE__, __LINE__, DB_OPT_FTRACE, "Audio is blocked", "audio blocked dump ftrace");
    }
    Auddrv_Reset_Dump_State();
    printk("-%s\n", __func__);
}


/*
/    dump underflow time in kernel
*/
static void DumpUnderFlowTime(void)
{
    int i=0;
    printk("%s\n",__func__);
    for(i=0; i < UnderflowrecordNumber ; i++)
    {
        printk("UnderflowTime[%d] = %llu \n",i,UnderflowTime[i]);
    }
}


/*
/    when pcm playback is close , need to call this function to clear record.
*/

void Auddrv_Set_UnderFlow(void)
{
    unsigned long long underflow_time =sched_clock(); // in ns (10^9)
    printk("%s UnderflowCounter = %d\n",__func__,UnderflowCounter);
    UnderflowTime[UnderflowCounter] =underflow_time;
    UnderflowCounter++;
    UnderflowCounter%= UnderflowrecordNumber;
    if(UnderflowCounter > UnderflowThreshold)
    {
        DumpUnderFlowTime();
        Auddrv_Aee_Dump();
    }
}

/*
/    dump underflow time in kernel
*/

static void ClearUnderFlowTime(void)
{
    int i=0;
    for(i=0; i < UnderflowrecordNumber ; i++)
    {
        UnderflowTime[i]= 0;
    }
    UnderflowCounter =0;
}


/*
/    when pcm playback is close , need to call this function to clear record.
*/

void Auddrv_Reset_Dump_State(void)
{
    ClearUnderFlowTime();
    Auddrv_Set_Interrupt_Changed(false);
    ClearInterruptTiming();
}

/*
/    when InterruptSample or mDlSamplerate is change , nned to refine mDL1InterruptInterval
*/

void Auddrv_Set_Interrupt_Changed(bool bChange)
{
     bAudioInterruptChange =bChange;
}

/*
/    when in interrupt , call this function to check irq timing
*/
void Auddrv_CheckInterruptTiming(void)
{
    if (Irq_time_t1 == 0)
    {
        Irq_time_t1 = sched_clock(); // in ns (10^9)
    }
    else
    {
        Irq_time_t2 = Irq_time_t1;
        Irq_time_t1 = sched_clock(); // in ns (10^9)
        if(bAudioInterruptChange == true)
        {
            // for Audio Interupt is change , so this interrupt interval may not clear.clearqueue
            ClearInterruptTiming();
            return;
        }
        if ((Irq_time_t1 > Irq_time_t2) && mDL1_Interrupt_Interval_Limit)
        {
            printk("CheckInterruptTiming  Irq_time_t2 = %llu Irq_time_t1 = %llu Irq_time_t1 - Irq_time_t2 = %llu DL1_Interrupt_Interval_Limit = %d\n",
                Irq_time_t2,Irq_time_t1, Irq_time_t1 - Irq_time_t2,mDL1_Interrupt_Interval_Limit);
            Irq_time_t2 = Irq_time_t1 - Irq_time_t2;
            if (Irq_time_t2 > mDL1_Interrupt_Interval_Limit * 1000000)
            {
                printk("CheckInterruptTiming interrupt may be blocked Irq_time_t2 = %llu mDL1_Interrupt_Interval_Limit = %d\n",
                              Irq_time_t2, mDL1_Interrupt_Interval_Limit);
            }
        }
    }
}

static void ClearInterruptTiming(void)
{
    printk("%s\n",__func__);
    Irq_time_t1 = 0;
    Irq_time_t2 = 0;
}


/*
/    when InterruptSample or mDlSamplerate is change , nned to refine mDL1InterruptInterval
*/

void RefineInterrruptInterval(void)
{
    mDL1InterruptInterval = ((InterruptSample * 1000) / mDlSamplerate) + 1;
    mDL1_Interrupt_Interval_Limit = mDL1InterruptInterval*mDl1Numerator /mDl1denominator;
    printk("%s mDL1InterruptInterval = %d mDL1_Interrupt_Interval_Limit = %d\n",__func__,mDL1InterruptInterval,mDL1_Interrupt_Interval_Limit);
}

/*
/    funtion to set DL sampleRate
*/

bool Auddrv_Set_DlSamplerate(unsigned int Samplerate)
{
    printk("%s Samplerate = %d\n",__func__,Samplerate);
    if(Samplerate < DlSampleRateUpperBound)
    {
        mDlSamplerate = Samplerate;
        RefineInterrruptInterval();
    }
    return true;
}

bool Auddrv_Set_InterruptSample(unsigned int count)
{
    printk("%s count = %d\n",__func__,count);
    if(count < InterruptSampleUpperBound)
    {
        InterruptSample = count;
        RefineInterrruptInterval();
        Auddrv_Set_Interrupt_Changed(true);
    }
    return true;
}

/*
/    funtion to enable / disable dump , only enable will arised aee
*/
bool Auddrv_Enable_dump(bool bEnable)
{
    printk("%s bEnable = %d\n",__func__,bEnable);
    bEnableDump = bEnable;
    return true;
}