1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 // Software and any modification/derivatives thereof.
18 // No right, ownership, or interest to MStar Software and any
19 // modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 // supplied together with third party`s software and the use of MStar
23 // Software may require additional licenses from third parties.
24 // Therefore, you hereby agree it is your sole responsibility to separately
25 // obtain any and all third party right and license necessary for your use of
26 // such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 // MStar`s confidential information and you agree to keep MStar`s
30 // confidential information in strictest confidence and not disclose to any
31 // third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 // kind. Any warranties are hereby expressly disclaimed by MStar, including
35 // without limitation, any warranties of merchantability, non-infringement of
36 // intellectual property rights, fitness for a particular purpose, error free
37 // and in conformity with any international standard. You agree to waive any
38 // claim against MStar for any loss, damage, cost or expense that you may
39 // incur related to your use of MStar Software.
40 // In no event shall MStar be liable for any direct, indirect, incidental or
41 // consequential damages, including without limitation, lost of profit or
42 // revenues, lost or damage of data, and unauthorized system use.
43 // You agree that this Section 4 shall still apply without being affected
44 // even if MStar Software has been modified by MStar in accordance with your
45 // request or instruction for your use, except otherwise agreed by both
46 // parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 // services in relation with MStar Software to you for your use of
50 // MStar Software in conjunction with your or your customer`s product
51 // ("Services").
52 // You understand and agree that, except otherwise agreed by both parties in
53 // writing, Services are provided on an "AS IS" basis and the warranty
54 // disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 // or otherwise:
58 // (a) conferring any license or right to use MStar name, trademark, service
59 // mark, symbol or any other identification;
60 // (b) obligating MStar or any of its affiliates to furnish any person,
61 // including without limitation, you and your customers, any assistance
62 // of any kind whatsoever, or any information; or
63 // (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 // of Taiwan, R.O.C., excluding its conflict of law rules.
67 // Any and all dispute arising out hereof or related hereto shall be finally
68 // settled by arbitration referred to the Chinese Arbitration Association,
69 // Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 // Rules of the Association by three (3) arbitrators appointed in accordance
71 // with the said Rules.
72 // The place of arbitration shall be in Taipei, Taiwan and the language shall
73 // be English.
74 // The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 ////////////////////////////////////////////////////////////////////////////////
79 //
80 // Copyright (c) 2008-2009 MStar Semiconductor, Inc.
81 // All rights reserved.
82 //
83 // Unless otherwise stipulated in writing, any and all information contained
84 // herein regardless in any format shall remain the sole proprietary of
85 // MStar Semiconductor Inc. and be kept in strict confidence
86 // ("MStar Confidential Information") by the recipient.
87 // Any unauthorized act including without limitation unauthorized disclosure,
88 // copying, use, reproduction, sale, distribution, modification, disassembling,
89 // reverse engineering and compiling of the contents of MStar Confidential
90 // Information is unlawful and strictly prohibited. MStar hereby reserves the
91 // rights to any and all damages, losses, costs and expenses resulting therefrom.
92 //
93 ////////////////////////////////////////////////////////////////////////////////
94 #define _DRV_WBLE_C_
95
96
97 #include <string.h>
98 #include "MsCommon.h"
99 #include "MsVersion.h"
100
101 #include "MsOS.h"
102 #include "MsTypes.h"
103 #include "wble_hwreg_utility2.h"
104
105 #include "drvMMIO.h"
106 #include "drvWBLE.h"
107 #include "mhal_wble.h"
108
109 /******************************************************************************/
110 /* Constant */
111 /******************************************************************************/
112 #define BLE_DEFLICK_BLEND_FACTOR 32UL
113 #define BLE_DEFLICK_PRECISION_SHIFT 64UL
114
115
116 #define BLE_METHOD_FIX_START 1 // Fixed starting point, variable slop
117 #define BLE_METHOD_FIX_SLOP 2 // Variable staring point, fixed slop
118 #define BLE_METHOD_DYNAMIC_START_SLOP 3 // Variable staring point, variable slop
119
120 #define BLE_SLOP_PARAM 0
121 #define BLE_START_PARAM 1
122
123
124 /******************************************************************************/
125 /* Local */
126 /******************************************************************************/
127
128 static MS_U8 g_u8BLE_OriSlop = 0x80;
129 static MS_U8 g_u8WLE_OriSlop = 0x80;
130
131 static MS_U16 u16PreHistogramTotal;
132 static MS_U16 u16PreBLESlope;
133 static MS_U16 u16PreBLEStart;
134 static MS_U8 u8SlopeDefalut;
135
136 static MSIF_Version _api_xc_wble_version = {
137 .DDI = { WBLE_API_VERSION },
138 };
139
140 //-------------------------------------------------------------------------------------------------
141 /// Get XC library version
142 /// @param ppVersion \b OUT: store the version in this member
143 /// @return @ref E_APIXC_ReturnValue
144 //-------------------------------------------------------------------------------------------------
MDrv_WBLE_GetLibVer(const MSIF_Version ** ppVersion)145 E_DRVWBLE_ReturnValue MDrv_WBLE_GetLibVer(const MSIF_Version **ppVersion)
146 {
147 if (!ppVersion)
148 {
149 return E_DRVWBLE_RET_FAIL;
150 }
151
152 *ppVersion = &_api_xc_wble_version;
153 return E_DRVWBLE_RET_OK;
154 }
155
156
MDrv_WBLE_Init(void)157 void MDrv_WBLE_Init(void)
158 {
159 u16PreHistogramTotal = 0;
160 u16PreBLESlope = Hal_WBLE_get_black_slop() * BLE_DEFLICK_PRECISION_SHIFT;
161 u16PreBLEStart = Hal_WBLE_get_black_start() * BLE_DEFLICK_PRECISION_SHIFT;
162 }
163
MDrv_WBLE_EnableBLE(MS_BOOL bEnable)164 void MDrv_WBLE_EnableBLE(MS_BOOL bEnable)
165 {
166 Hal_WBLE_set_ble(bEnable);
167 }
168
169
MDrv_WBLE_EnableWLE(MS_BOOL bEnable)170 void MDrv_WBLE_EnableWLE(MS_BOOL bEnable)
171 {
172 Hal_WBLE_set_wle(bEnable);
173 }
174
175
MDrv_WBLE_SetBLE(MS_U8 u8Start,MS_U8 u8Slop)176 void MDrv_WBLE_SetBLE(MS_U8 u8Start, MS_U8 u8Slop)
177 {
178 Hal_WBLE_set_ble_start(u8Start);
179 Hal_WBLE_set_ble_slop(u8Slop);
180 g_u8BLE_OriSlop = u8Slop;
181 }
182
MDrv_WBLE_SetWLE(MS_U8 u8Start,MS_U8 u8Slop)183 void MDrv_WBLE_SetWLE(MS_U8 u8Start, MS_U8 u8Slop)
184 {
185 Hal_WBLE_set_wle_start(u8Start);
186 Hal_WBLE_set_wle_slop(u8Slop);
187 g_u8WLE_OriSlop = u8Slop;
188 }
189
190
MDrv_WBLE_Handler_BLEAvgLuma(MS_U8 u8AvgY,MS_U16 u16YTotalSum)191 void MDrv_WBLE_Handler_BLEAvgLuma(MS_U8 u8AvgY, MS_U16 u16YTotalSum)
192 {
193 MS_U8 u8Reg;
194 MS_BOOL bEnableBLE;
195 MS_U8 u8Method, u8Strength, u8NewTarget;
196 MS_U8 u8FlickAlpha;
197 MS_U8 u8Step;
198 MS_U16 u16Blend;
199
200 MS_U8 u8Max[2], u8Min[2];
201 MS_U16 u16PreTarget[2], u16TmpTarget[2];
202 MS_U8 u8Start, u8End;
203 MS_U8 i;
204 MS_U16 u16Tmp;
205
206 // control register
207 u8Reg = Hal_WBLE_get_sw_ble_method();
208
209 bEnableBLE = u8Reg & BIT(0);
210 switch(u8Reg & (BIT(1)|BIT(2)))
211 {
212 case 0x00:
213 u8Method = BLE_METHOD_FIX_START;
214 u8Start = BLE_SLOP_PARAM;
215 u8End = BLE_SLOP_PARAM;
216 break;
217 case BIT(1):
218 u8Method = BLE_METHOD_FIX_SLOP;
219 u8Start = BLE_START_PARAM;
220 u8End = BLE_START_PARAM;
221 Hal_WBLE_set_ble_slop(u8SlopeDefalut);
222 break;
223
224 default:
225 case BIT(2):
226 u8Method = BLE_METHOD_DYNAMIC_START_SLOP;
227 u8Start = BLE_SLOP_PARAM;
228 u8End = BLE_START_PARAM;
229 break;
230 }
231
232 if(bEnableBLE)
233 {
234 u8Strength = Hal_WBLE_get_ble_stregth();
235 u8Step = Hal_WBLE_get_ble_step();
236
237 u16Tmp = Hal_WBLE_get_ble_slop_boundary();
238 u8Max[BLE_SLOP_PARAM] = (MS_U8)((u16Tmp & 0xFF00)>>8);
239 u8Min[BLE_SLOP_PARAM] = (MS_U8)(u16Tmp & 0x00FF);
240
241 u16Tmp = Hal_WBLE_get_ble_start_boundary();
242 u8Max[BLE_START_PARAM] = (MS_U8)((u16Tmp & 0xFF00)>>8);
243 u8Min[BLE_START_PARAM] = (MS_U8)(u16Tmp & 0x00FF);
244
245 if(u8Method & BLE_METHOD_FIX_SLOP)
246 { // fix slope
247 MS_U8 u8Tmp;
248 u8Tmp = (u8AvgY > 0x10) ? (u8AvgY - 0x10) : 0x00;
249 u16TmpTarget[BLE_START_PARAM]= (MS_U32)u8Tmp * (MS_U32)u8Strength / 128 + 0x00;
250 u16PreTarget[BLE_START_PARAM] = u16PreBLEStart;
251 }
252
253 if(u8Method & BLE_METHOD_FIX_START)
254 { // fix start
255 u16TmpTarget[BLE_SLOP_PARAM] = (MS_U32)u8AvgY * (MS_U32)u8Strength / 128 + 0x70;
256 u16PreTarget[BLE_SLOP_PARAM] = u16PreBLESlope;
257 }
258
259
260 // de-flicker
261 if (u16PreHistogramTotal >= u16YTotalSum)
262 {
263 if ((u16PreHistogramTotal - u16YTotalSum) < Hal_WBLE_get_ble_delta())
264 u8FlickAlpha = Hal_WBLE_get_ble_flickalpha();
265 else
266 u8FlickAlpha = BLE_DEFLICK_BLEND_FACTOR;
267 }
268 else
269 {
270 if ((u16YTotalSum - u16PreHistogramTotal) < Hal_WBLE_get_ble_delta())
271 u8FlickAlpha = Hal_WBLE_get_ble_flickalpha();
272 else
273 u8FlickAlpha = BLE_DEFLICK_BLEND_FACTOR;
274 }
275
276 u16PreHistogramTotal = u16YTotalSum;
277
278 for(i=u8Start; i<=u8End; i++)
279 {
280 if(u16TmpTarget[i] > (MS_U16)u8Max[i])
281 u8NewTarget = u8Max[i];
282 else if(u16TmpTarget[i] < (MS_U16)u8Min[i])
283 u8NewTarget = u8Min[i];
284 else
285 u8NewTarget = u16TmpTarget[i];
286
287 u16Blend =(MS_U16)( (((MS_U32)u8FlickAlpha * (MS_U32)u8NewTarget * (MS_U32)BLE_DEFLICK_PRECISION_SHIFT) +
288 (((MS_U32)BLE_DEFLICK_BLEND_FACTOR-(MS_U32)u8FlickAlpha) * (MS_U32)u16PreTarget[i])) / BLE_DEFLICK_BLEND_FACTOR);
289
290 // the difference can't be too big
291 if(u16Blend > u16PreTarget[i])
292 {
293 if((u16Blend - u16PreTarget[i])> (BLE_DEFLICK_PRECISION_SHIFT * (MS_U16)u8Step))
294 u16Blend = u16PreTarget[i] + (BLE_DEFLICK_PRECISION_SHIFT * (MS_U16)u8Step);
295 }
296 else if(u16Blend < u16PreTarget[i])
297 {
298 if((u16PreTarget[i] - u16Blend)> (BLE_DEFLICK_PRECISION_SHIFT * (MS_U16)u8Step))
299 u16Blend = u16PreTarget[i] - (BLE_DEFLICK_PRECISION_SHIFT * (MS_U16)u8Step);
300 }
301
302 if(i == BLE_SLOP_PARAM)
303 {
304 u16PreBLESlope = u16Blend;
305 }
306 else
307 {
308 u16PreBLEStart = u16Blend;
309 }
310
311
312 u8NewTarget = (MS_U8)(u16Blend / BLE_DEFLICK_PRECISION_SHIFT);
313
314 if(i == BLE_SLOP_PARAM)
315 {
316 Hal_WBLE_set_ble_slop(u8NewTarget);
317 }
318 if(i == BLE_START_PARAM)
319 {
320 Hal_WBLE_set_ble_start(u8NewTarget);
321 }
322 }
323 }
324 else
325 {
326 Hal_WBLE_set_ble_slop(0x80);
327 Hal_WBLE_set_ble_start(0x70);
328 }
329 }
330
331
MDrv_WBLE_Set_SlopeValue(MS_U8 u8Slope)332 void MDrv_WBLE_Set_SlopeValue(MS_U8 u8Slope)
333 {
334 u8SlopeDefalut = u8Slope;
335
336 }
337
338