xref: /utopia/UTPA2-700.0.x/modules/ch34/drv/ch34/drvCH34.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
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 
95 ///////////////////////////////////////////////////////////////////////////////////////////////////
96 ///
97 /// file    drvTEMP.c
98 /// @brief  TEMP Driver Interface
99 /// @author MStar Semiconductor Inc.
100 ///////////////////////////////////////////////////////////////////////////////////////////////////
101 
102 
103 //-------------------------------------------------------------------------------------------------
104 //  Include Files
105 //-------------------------------------------------------------------------------------------------
106 
107 // Common Definition
108 #include "MsCommon.h"
109 #include "MsVersion.h"
110 #include "drvCH34.h"
111 #include "drvCH34_v2.h"
112 #include "drvCH34_private.h"
113 #include "drvMMIO.h"
114 
115 //#include "drvCH34_Tbl.h"
116 
117 // Internal Definition
118 #include "MsOS.h"
119 #include "regCHIP.h"
120 #include "regCH34.h"
121 #include "halCH34.h"
122 #include "utopia.h"
123 
124 //#include "halCH34_Tbl.h"
125 
126 
127 //-------------------------------------------------------------------------------------------------
128 //  Driver Compiler Options
129 //-------------------------------------------------------------------------------------------------
130 
131 
132 //-------------------------------------------------------------------------------------------------
133 //  Local Defines
134 //-------------------------------------------------------------------------------------------------
135 
136 
137 //-------------------------------------------------------------------------------------------------
138 //  Local Structures
139 //-------------------------------------------------------------------------------------------------
140 
141 
142 //-------------------------------------------------------------------------------------------------
143 //  Global Variables
144 //-------------------------------------------------------------------------------------------------
145 
146 
147 //-------------------------------------------------------------------------------------------------
148 //  Local Variables
149 //-------------------------------------------------------------------------------------------------
150 
151 //-------------------------------------------------------------------------------------------------
152 //  Debug Functions
153 //-------------------------------------------------------------------------------------------------
154 
155 
156 //-------------------------------------------------------------------------------------------------
157 //  Local Functions
158 //-------------------------------------------------------------------------------------------------
159 
160 
161 //-------------------------------------------------------------------------------------------------
162 //  Global Functions
163 //-------------------------------------------------------------------------------------------------
164 #define CH34_TABLE_OFFSET                  4UL
165 #define CH34_TABLE_MAX_DEPTH               2000UL
166 
167 static MS_BOOL _bInit;
168 void* pu32CH34Inst = NULL;
169 
MDrv_Ch34_Init_U2(void)170 CH34_Drv_Result MDrv_Ch34_Init_U2(void)
171 {
172     MS_VIRT virtCH34RiuBaseAddr;
173     MS_U32 u32NonPMBankSize;
174 	// get MMIO base
175 	if(MDrv_MMIO_GetBASE( &virtCH34RiuBaseAddr, &u32NonPMBankSize, MS_MODULE_GE ) != TRUE)
176 	{
177 		printf("GetBase failed\n");
178 		return En_Drv_CH34_FAIL;
179 	}
180 
181     if(!_bInit)
182     {
183         // Driver Initialization
184         HAL_CH34_Init(virtCH34RiuBaseAddr);
185         _bInit = TRUE;
186     }
187     return En_Drv_CH34_OK;
188 }
189 
MDrv_Ch34_Init(void)190 CH34_Drv_Result MDrv_Ch34_Init(void)
191 {
192     if (pu32CH34Inst == NULL)
193     {
194         if(UtopiaOpen(MODULE_CH34, &pu32CH34Inst, 0, NULL) != UTOPIA_STATUS_SUCCESS)
195         {
196             printf("UtopiaOpen pu32CH34Inst failed\n");
197     		return En_Drv_CH34_FAIL;
198         }
199     }
200 	else
201 	{
202         printf("CH34 has been initialized\n");
203         return En_Drv_CH34_OK;
204 	}
205 
206 	CH34_INIT_INFO CH34Args;
207 	if(UtopiaIoctl(pu32CH34Inst, MAPI_CMD_CH34_INIT, (void*)&CH34Args) != UTOPIA_STATUS_SUCCESS)
208 	{
209 		printf("Obtain CH34 engine fail\n");
210 		return En_Drv_CH34_FAIL;
211 	}
212 	else
213 	{
214 		return CH34Args.bReturnValue;
215 	}
216 }
217 
MDrv_Ch34_TurnOnOff_U2(MS_BOOL bOn)218 CH34_Drv_Result MDrv_Ch34_TurnOnOff_U2(MS_BOOL bOn)
219 {
220 	HAL_CH34_EnableDAC(bOn);
221     return En_Drv_CH34_OK;
222 }
223 
MDrv_Ch34_TurnOnOff(MS_BOOL bOn)224 CH34_Drv_Result MDrv_Ch34_TurnOnOff(MS_BOOL bOn)
225 {
226     if (pu32CH34Inst == NULL)
227     {
228 		printf("CH34 not initialize!\n");
229         return En_Drv_CH34_FAIL;
230 
231     }
232 
233 	CH34_TURN_ONOFF_INFO CH34Args;
234 	CH34Args.bOn = bOn;
235 	if(UtopiaIoctl(pu32CH34Inst, MAPI_CMD_CH34_TURNONOFF, (void*)&CH34Args) != UTOPIA_STATUS_SUCCESS)
236 	{
237 		printf("Obtain CH34 engine fail\n");
238 		return En_Drv_CH34_FAIL;
239 	}
240 	else
241 	{
242 		return CH34Args.bReturnValue;
243 	}
244 }
245 
_MappingEnum(E_CH34_CHANNEL channel,E_CH34_VIDEO_SYS videosys,E_CH34_AUDIO_SYS audiosys,E_HAL_CH34_CHANNEL * halchannel,E_HAL_CH34_VIDEO_SYS * halvideosys,E_HAL_CH34_AUDIO_SYS * halaudiosys)246 static void _MappingEnum(E_CH34_CHANNEL channel, E_CH34_VIDEO_SYS videosys, E_CH34_AUDIO_SYS audiosys,
247 			E_HAL_CH34_CHANNEL *halchannel, E_HAL_CH34_VIDEO_SYS *halvideosys, E_HAL_CH34_AUDIO_SYS *halaudiosys)
248 {
249 	switch(channel)
250 	{
251 		case E_CH34_CH3:
252 			*halchannel = E_HAL_CH34_CH3;
253 			break;
254 		case E_CH34_CH4:
255 		default:
256 			*halchannel = E_HAL_CH34_CH4;
257 			break;
258 	}
259 	switch(videosys)
260 	{
261 		case E_CH34_VIDEO_SYS_NTSC:
262 			*halvideosys = E_HAL_CH34_VIDEO_SYS_NTSC;
263 			break;
264 		case E_CH34_VIDEO_SYS_PAL:
265 		default:
266 			*halvideosys = E_HAL_CH34_VIDEO_SYS_PAL;
267 			break;
268 	}
269 	switch(audiosys)
270 	{
271 		case E_CH34_AUDIO_SYS_MONO:
272 			*halaudiosys = E_HAL_CH34_AUDIO_SYS_MONO;
273 			break;
274 		case E_CH34_AUDIO_SYS_BTSC:
275 			*halaudiosys = E_HAL_CH34_AUDIO_SYS_BTSC;
276 			break;
277                   case E_CH34_AUDIO_SYS_BTSC_SAP:
278 			*halaudiosys = E_HAL_CH34_AUDIO_SYS_BTSC_SAP;
279 			break;
280 		case E_CH34_AUDIO_SYS_A2:
281 		default:
282 			*halaudiosys = E_HAL_CH34_AUDIO_SYS_A2;
283 			break;
284 	}
285 }
286 
MDrv_Ch34_SetMode_U2(E_CH34_CHANNEL channel,E_CH34_VIDEO_SYS videosys,E_CH34_AUDIO_SYS audiosys)287 CH34_Drv_Result MDrv_Ch34_SetMode_U2(E_CH34_CHANNEL channel, E_CH34_VIDEO_SYS videosys, E_CH34_AUDIO_SYS audiosys)
288 {
289 	MS_U8 i = 0;
290 	E_HAL_CH34_CHANNEL eChannel;
291 	E_HAL_CH34_VIDEO_SYS eVideoSys;
292 	E_HAL_CH34_AUDIO_SYS eAudiosys;
293 	_MappingEnum(channel,videosys,audiosys,&eChannel,&eVideoSys,&eAudiosys);
294 	for(i = 0 ; i < E_HAL_CH34_TBL_MAX ; i++)
295 	{
296 		MS_U8* pTable = NULL;
297 		if(HAL_CH34_GetTable(eChannel,eVideoSys,eAudiosys,(E_HAL_CH34_TABLETYPE)i,&pTable) == FALSE)
298 		{
299 			printf("[%s] Get table fail\n",__FUNCTION__);
300 			return En_Drv_CH34_INVALID_PARAM;
301 		}
302 		if(NULL != pTable)
303 		{
304 	    	MDrv_Ch34_WriteCH34Table_U2(pTable);
305 		}
306 	}
307 	return En_Drv_CH34_OK;
308 }
309 
MDrv_Ch34_SetMode(E_CH34_CHANNEL channel,E_CH34_VIDEO_SYS videosys,E_CH34_AUDIO_SYS audiosys)310 CH34_Drv_Result MDrv_Ch34_SetMode(E_CH34_CHANNEL channel, E_CH34_VIDEO_SYS videosys, E_CH34_AUDIO_SYS audiosys)
311 {
312     if (pu32CH34Inst == NULL)
313     {
314 		printf("CH34 not initialize!\n");
315         return En_Drv_CH34_FAIL;
316 
317     }
318 
319 	CH34_SET_MODE_INFO CH34Args;
320 	CH34Args.channel = channel;
321 	CH34Args.videosys = videosys;
322 	CH34Args.audiosys = audiosys;
323 	if(UtopiaIoctl(pu32CH34Inst, MAPI_CMD_CH34_SETMODE, (void*)&CH34Args) != UTOPIA_STATUS_SUCCESS)
324 	{
325 		printf("Obtain CH34 engine fail\n");
326 		return En_Drv_CH34_FAIL;
327 	}
328 	else
329 	{
330 		return CH34Args.bReturnValue;
331 	}
332 }
333 
MDrv_Ch34_WriteCH34Table_U2(MS_U8 * pTable)334 CH34_Drv_Result MDrv_Ch34_WriteCH34Table_U2(MS_U8* pTable)
335 {
336     MS_U32 u32Index; // register addr+offset
337     MS_U16 u16Dummy;
338     if(!_bInit)
339     {
340 		printf("CH34 not initialize!\n");
341         return En_Drv_CH34_FAIL;
342     }
343 
344     u16Dummy = CH34_TABLE_MAX_DEPTH;
345     do
346     {
347         u32Index = ((pTable[0] << 16) + (pTable[1] << 8) + pTable[2]);
348 		u32Index &= 0xFFFFFFUL;
349         if (u32Index == 0xFFFFFFUL) // check end of table
350             break;
351 
352 		CH34_WriteReg(u32Index,pTable[3]);
353 
354         pTable += CH34_TABLE_OFFSET; //next row of table
355 
356     }while (--u16Dummy > 0);
357 
358     return En_Drv_CH34_OK;
359 }
360 
MDrv_Ch34_WriteCH34Table(MS_U8 * pTable)361 CH34_Drv_Result MDrv_Ch34_WriteCH34Table(MS_U8* pTable)
362 {
363     if (pu32CH34Inst == NULL)
364     {
365 		printf("CH34 not initialize!\n");
366         return En_Drv_CH34_FAIL;
367 
368     }
369 
370 	CH34_WRITE_TABLE_INFO CH34Args;
371 	CH34Args.pTable = pTable;
372 	if(UtopiaIoctl(pu32CH34Inst, MAPI_CMD_CH34_WRITETABLE, (void*)&CH34Args) != UTOPIA_STATUS_SUCCESS)
373 	{
374 		printf("Obtain CH34 engine fail\n");
375 		return En_Drv_CH34_FAIL;
376 	}
377 	else
378 	{
379 		return CH34Args.bReturnValue;
380 	}
381 
382 }
383 
384