xref: /utopia/UTPA2-700.0.x/modules/demodulator/drv/dmd/u3/Int_DVBC/drvDMD.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) 2006-2007 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    drvDMD.c
98 /// @brief  DVBC Demodulator Driver Interface
99 /// @author MStar Semiconductor,Inc.
100 ///////////////////////////////////////////////////////////////////////////////////////////////////
101 
102 
103 //-------------------------------------------------------------------------------------------------
104 //  Include Files
105 //-------------------------------------------------------------------------------------------------
106 
107 #ifdef MSOS_TYPE_LINUX
108     #include <sys/ioctl.h>
109     #include <fcntl.h>
110     #include <errno.h>
111     #include <math.h>
112 #else // !MSOS_TYPE_LINUX
113 #endif // MSOS_TYPE_LINUX
114 
115 #include "MsCommon.h"
116 #include "drvDMD.h"
117 #include "devDVBC.h"
118 
119 
120 #if   (FRONTEND_DEMOD_TYPE == DEMOD_MSDVBC)
121 
122 //-------------------------------------------------------------------------------------------------
123 //  Driver Compiler Options
124 //-------------------------------------------------------------------------------------------------
125 #define DMD_DBG_ENABLE              0
126 
127 
128 //-------------------------------------------------------------------------------------------------
129 //  Local Defines
130 //-------------------------------------------------------------------------------------------------
131 
132 #define DMD_SNR_BIAS                3.5
133 
134 #ifdef MSOS_TYPE_LINUX_KERNEL
135 #define DMD_MUTEX_TIMEOUT           MSOS_WAIT_FOREVER
136 #else
137 #define DMD_MUTEX_TIMEOUT           2000                                // The maximum timeout by X4CFE_en
138 #endif
139 
140 #ifndef MS_NOSAPI
141 #define DMD_INIT()                 { _s32DmdMutexId = MsOS_CreateMutex(E_MSOS_FIFO, "DMD_Mutex", MSOS_PROCESS_SHARED);   \
142                                      if (_s32DmdMutexId < 0)                                        \
143                                      {                                                              \
144                                          DMD_DBG("%s Failed (%d)\n", __FUNCTION__, __LINE__);       \
145                                          return FALSE;                                              \
146                                      }                                                              \
147                                    }
148 #define DMD_ENTRY()                if (!MsOS_ObtainMutex(_s32DmdMutexId, DMD_MUTEX_TIMEOUT))        \
149                                    {   return FALSE;   }
150 #define DMD_RETURN(_ret)           { MsOS_ReleaseMutex(_s32DmdMutexId);                             \
151                                      return _ret; }// Should be optimized by compiler
152 #else   // MS_NOSAPI
153 #define DMD_INIT()                 { }
154 #define DMD_ENTRY()                { }
155 #define DMD_RETURN(_ret)           { return _ret; }
156 #endif  // MS_NOSAPI
157 
158 
159 #if DMD_DBG_ENABLE
160 #define DMD_DBG(fmt, args...)       printf(fmt, ##args)
161 #else
162 #define DMD_DBG(fmt, args...)       {}
163 #endif
164 
165 
166 //-------------------------------------------------------------------------------------------------
167 //  Local Structurs
168 //-------------------------------------------------------------------------------------------------
169 
170 
171 //-------------------------------------------------------------------------------------------------
172 //  Global Variables
173 //-------------------------------------------------------------------------------------------------
174 
175 
176 //-------------------------------------------------------------------------------------------------
177 //  Local Variables
178 //-------------------------------------------------------------------------------------------------
179 
180 static MS_S32                       _s32DmdMutexId;
181 static MS_BOOL                      _bDmdOpen;
182 
183 //-------------------------------------------------------------------------------------------------
184 //  Debug Functions
185 //-------------------------------------------------------------------------------------------------
186 
187 
188 //-------------------------------------------------------------------------------------------------
189 //  Local Functions
190 //-------------------------------------------------------------------------------------------------
191 int DVBC_Init(void);
192 int DVBC_Open(void);
193 int DVBC_Release(void);
194 int DVBC_Ioctl(MS_U32 cmd, ...);
195 #ifdef MS_NOSAPI
196 MS_BOOL uDVBC_Restart(DVBC_Param *pParam, DVBC_Mode *pMode);
197 #endif
198 
199 //-------------------------------------------------------------------------------------------------
200 //  Global Functions
201 //-------------------------------------------------------------------------------------------------
202 
203 //-------------------------------------------------------------------------------------------------
204 /// Initialize demodulator driver
205 /// @return TRUE(Success), FALSE(Failure)
206 /// DVBC driver is in @ref E_TASK_PRI_HIGH level
207 //-------------------------------------------------------------------------------------------------
MDrv_Dmd_Init(void)208 MS_BOOL MDrv_Dmd_Init(void)
209 {
210     DMD_DBG("%s\n", __FUNCTION__);
211 
212     DVBC_Init();
213 
214     DMD_INIT()
215 
216     DMD_DBG("%s Successful\n", __FUNCTION__);
217 
218     return TRUE;
219 
220 }
221 
222 
223 //-------------------------------------------------------------------------------------------------
224 /// Open a demodulator instance
225 /// @return TRUE(Success), FALSE(Failure)
226 //-------------------------------------------------------------------------------------------------
MDrv_Dmd_Open(void)227 MS_BOOL MDrv_Dmd_Open(void)
228 {
229     DMD_DBG("%s\n", __FUNCTION__);
230 
231     DMD_ENTRY();
232 
233     if (_bDmdOpen)
234     {
235         DMD_DBG("%s Failed (%d)\n", __FUNCTION__, __LINE__);
236         DMD_RETURN(FALSE);
237     }
238 
239     // Open SMART device
240     if (DVBC_Open() < 0)
241     {
242         DMD_DBG("%s Failed (%d)\n", __FUNCTION__, __LINE__);
243         DMD_RETURN(FALSE);
244     }
245 
246     _bDmdOpen = TRUE;
247 
248     DMD_RETURN(TRUE);
249 }
250 
251 
252 //-------------------------------------------------------------------------------------------------
253 /// Close a demodulator instance
254 /// @return TRUE(Success), FALSE(Failure)
255 //-------------------------------------------------------------------------------------------------
MDrv_Dmd_Close(void)256 MS_BOOL MDrv_Dmd_Close(void)
257 {
258     DMD_DBG("%s\n", __FUNCTION__);
259 
260     DMD_ENTRY();
261     if (_bDmdOpen == FALSE)
262     {
263         DMD_RETURN(FALSE);
264     }
265 
266     _bDmdOpen = FALSE;
267 
268     // Close SMART device and free system resource
269     DVBC_Release();
270 
271     DMD_RETURN(TRUE);
272 }
273 
274 
275 //-------------------------------------------------------------------------------------------------
276 /// Set demodulator tuning control
277 /// @param  pMode                   \b IN:  tuning control
278 /// @return TRUE(Success), FALSE(Failure)
279 /// @note
280 /// [NONOS_SUPPORT]
281 //-------------------------------------------------------------------------------------------------
MDrv_Dmd_SetMode(DMD_Mode * pMode)282 MS_BOOL MDrv_Dmd_SetMode(DMD_Mode *pMode)
283 {
284     //[TODO] configurate DVBC_Mode if it's different than DMD_Mode
285     if (DVBC_Ioctl(DEVDVBC_IOC_SETMODE, pMode))
286     {
287         return FALSE;
288     }
289     return TRUE;
290 }
291 
292 
293 //-------------------------------------------------------------------------------------------------
294 /// Reset demodulator
295 /// @return TRUE(Success), FALSE(Failure)
296 //-------------------------------------------------------------------------------------------------
MDrv_Dmd_Reset(void)297 MS_BOOL MDrv_Dmd_Reset(void)
298 {
299     DMD_ENTRY();
300 
301     DMD_DBG("%s\n", __FUNCTION__);
302 
303     if (DVBC_Ioctl(DEVDVBC_IOC_RESET))
304     {
305         DMD_RETURN(FALSE);
306     }
307 
308     DMD_RETURN(TRUE);
309 }
310 
311 
312 //-------------------------------------------------------------------------------------------------
313 /// Restart demodulator by tuning parameter
314 /// @param  pParam                  \b IN:  tuning parameter
315 /// @return TRUE(Success), FALSE(Failure)
316 /// @note
317 /// [NONOS_SUPPORT] TRUE: Lock
318 //-------------------------------------------------------------------------------------------------
MDrv_Dmd_Restart(DMD_Param * pParam)319 MS_BOOL MDrv_Dmd_Restart(DMD_Param *pParam)
320 {
321     DMD_ENTRY();
322 
323     DMD_DBG("%s\n", __FUNCTION__);
324 
325     if (DVBC_Ioctl(DEVDVBC_IOC_RESET))
326     {
327         DMD_RETURN(FALSE);
328     }
329 
330     //[TODO] configurate DVBC_Mode if it's different than DMD_Mode
331     if (DVBC_Ioctl(DEVDVBC_IOC_RESTART, pParam))
332     {
333         DMD_RETURN(FALSE);
334     }
335 
336     DMD_RETURN(TRUE);
337 }
338 
339 
340 //-------------------------------------------------------------------------------------------------
341 /// Enable/Disable demodulator external TS output
342 /// @param  bEnable                 \b IN:  Enable/disable
343 /// @return TRUE(Success), FALSE(Failure)
344 //-------------------------------------------------------------------------------------------------
MDrv_Dmd_TsOut(MS_BOOL bEnable)345 MS_BOOL MDrv_Dmd_TsOut(MS_BOOL bEnable)
346 {
347     if (DVBC_Ioctl(DEVDVBC_IOC_SETTSOUT, bEnable ? 1 : 0))
348     {
349         return FALSE;
350     }
351     return TRUE;
352 }
353 
354 
355 //-------------------------------------------------------------------------------------------------
356 /// Demodulator power on/off
357 /// @param  bPowerOn                \b IN:  Power on/off
358 /// @return TRUE(Success), FALSE(Failure)
359 //-------------------------------------------------------------------------------------------------
MDrv_Dmd_PowerOnOff(MS_BOOL bPowerOn)360 MS_BOOL MDrv_Dmd_PowerOnOff(MS_BOOL bPowerOn)
361 {
362     if (DVBC_Ioctl(DEVDVBC_IOC_SETPOWER, bPowerOn ? 1 : 0))
363     {
364         return FALSE;
365     }
366     return TRUE;
367 }
368 
369 
370 //-------------------------------------------------------------------------------------------------
371 /// Set carrier frequency bandwidth
372 /// @param  u32BW                   \b IN:  Frequency bandwidth (KHz)
373 /// @return TRUE(Success), FALSE(Failure)
374 //-------------------------------------------------------------------------------------------------
MDrv_Dmd_SetBW(MS_U32 u32BW)375 MS_BOOL MDrv_Dmd_SetBW(MS_U32 u32BW)
376 {
377     if (DVBC_Ioctl(DEVDVBC_IOC_SETBW, u32BW))
378     {
379         return FALSE;
380     }
381     return TRUE;
382 }
383 
384 
385 //-------------------------------------------------------------------------------------------------
386 /// Get carrier frequency bandwid setting
387 /// @param  pu32BW                  \b OUT: Pointer of return bandwidth
388 /// @return TRUE(Success), FALSE(Failure)
389 //-------------------------------------------------------------------------------------------------
MDrv_Dmd_GetBW(MS_U32 * pu32BW)390 MS_BOOL MDrv_Dmd_GetBW(MS_U32 *pu32BW)
391 {
392     if (DVBC_Ioctl(DEVDVBC_IOC_GETBW, pu32BW))
393     {
394         return FALSE;
395     }
396     return TRUE;
397 }
398 
399 
400 //-------------------------------------------------------------------------------------------------
401 /// Get demodulator lock status
402 /// @param  pbLock                  \b OUT: Pointer of return lock status
403 /// @return TRUE(Success), FALSE(Failure)
404 //-------------------------------------------------------------------------------------------------
MDrv_Dmd_GetLock(MS_BOOL * pbLock)405 MS_BOOL MDrv_Dmd_GetLock(MS_BOOL *pbLock)
406 {
407     if (DVBC_Ioctl(DEVDVBC_IOC_GETLOCK, pbLock))
408     {
409         return FALSE;
410     }
411     return TRUE;
412 }
413 
414 
415 #ifndef MSOS_TYPE_LINUX_KERNEL
416 //-------------------------------------------------------------------------------------------------
417 /// Get carrier SNR indicator
418 /// @param  pu32SNR                 \b OUT: Pointer of return SNR (db)
419 /// @return TRUE(Success), FALSE(Failure)
420 //-------------------------------------------------------------------------------------------------
MDrv_Dmd_GetSNR(MS_U32 * pu32SNR)421 MS_BOOL MDrv_Dmd_GetSNR(MS_U32 *pu32SNR)
422 {
423     MS_S16              s16NoisePower;
424     float              vk;
425 
426     *pu32SNR = 0;
427 
428     MS_BOOL             bLock;
429     // Check signal lock
430     if (DVBC_Ioctl(DEVDVBC_IOC_GETLOCK, &bLock))
431     {
432         return FALSE;
433     }
434     if (bLock == FALSE)
435     {
436         return FALSE;
437     }
438 
439     if (DVBC_Ioctl(DEVDVBC_IOC_GETNOISEPOWER, &s16NoisePower))
440     {
441         return FALSE;
442     }
443     if (s16NoisePower == 0)
444     {
445         s16NoisePower = 1;
446     }
447     if (s16NoisePower < 0)// negative
448     {
449         s16NoisePower = -s16NoisePower;
450     }
451 
452     vk = 10 * log10(65536.f/((double)s16NoisePower)) - DMD_SNR_BIAS;
453     *pu32SNR = (vk > 0.0) ? (MS_U32)(vk) : 0;
454 
455     return TRUE;
456 }
457 
458 //-------------------------------------------------------------------------------------------------
459 /// Get carrier BER indicator
460 /// @param  pfBER                   \b OUT: Pointer of return BER (1/65535)
461 /// @return TRUE(Success), FALSE(Failure)
462 //-------------------------------------------------------------------------------------------------
MDrv_Dmd_GetBER(float * pfBER)463 MS_BOOL MDrv_Dmd_GetBER(float *pfBER)
464 {
465     MS_BOOL             bLock;
466     MS_U16              u16WindowLen;
467     MS_U32              u32ErrorBit;
468 
469     // Check signal lock
470     if (DVBC_Ioctl(DEVDVBC_IOC_GETLOCK, &bLock))
471     {
472         return FALSE;
473     }
474     if (bLock == FALSE)
475     {
476         return FALSE;
477     }
478 
479     if (DVBC_Ioctl(DEVDVBC_IOC_GETWINDOWLEN, &u16WindowLen))
480     {
481         return FALSE;
482     }
483     if (DVBC_Ioctl(DEVDVBC_IOC_GETERRORBIT, &u32ErrorBit))
484     {
485         return FALSE;
486     }
487     // 0 <= fBER <= 1
488     *pfBER = (double)(u32ErrorBit)/((double)(u16WindowLen) * 128 * 188 * 8);
489     if (*pfBER > 1.0)
490     {
491         *pfBER = 1.0;
492     }
493     // .32 format
494 
495     return TRUE;
496 }
497 
498 
499 //-------------------------------------------------------------------------------------------------
500 /// Get carrier PWR indicator
501 /// @param  ps32PWR                 \b OUT: Pointer of return PWR (0-255)
502 /// @return TRUE(Success), FALSE(Failure)
503 //-------------------------------------------------------------------------------------------------
MDrv_Dmd_GetPWR(MS_S32 * ps32PWR)504 MS_BOOL MDrv_Dmd_GetPWR(MS_S32 *ps32PWR)
505 {
506     MS_U32              u32RfGain, u32IfGain;
507     double              fPower, fTemp;
508 
509     *ps32PWR = 0;
510 
511 #if 0 // no need to be locked
512     MS_BOOL             bLock;
513     // Check signal lock
514     if (DVBC_Ioctl(DEVDVBC_IOC_GETLOCK, &bLock))
515     {
516         return FALSE;
517     }
518     if (bLock == FALSE)
519     {
520         return FALSE;
521     }
522 #endif
523 
524     if (DVBC_Ioctl(DEVDVBC_IOC_GETRFGAIN, &u32RfGain))
525     {
526         return FALSE;
527     }
528     if (DVBC_Ioctl(DEVDVBC_IOC_GETIFGAIN, &u32IfGain))
529     {
530         return FALSE;
531     }
532     fTemp = -20 * log10((double)u32RfGain/0x10000) - 20 * log10((double)u32IfGain/0x10000) - 63.4;
533     if (fTemp >= -45.1)
534     {
535         fPower = -30 + ( fTemp + 45.1 ) * 2.5974;
536     }
537     else if (fTemp >= -47.4)
538     {
539         fPower = -50 + ( fTemp + 47.4 ) * 8.69565;
540     }
541     else if (fTemp >= -52.3)
542     {
543         fPower = -55 + ( fTemp + 52.3 ) * 1.02;
544     }
545     else //if (fTemp < -52.3)
546     {
547         fPower = -55 + ( fTemp + 52.3 ) * 3.846154;
548     }
549 
550     *ps32PWR = (MS_S32)(fPower);
551 
552 
553     return TRUE;
554 }
555 #endif
556 
557 
558 //-------------------------------------------------------------------------------------------------
559 /// Get demodulator tuning parameter
560 /// @param  pParam                  \b OUT: Pointer of return paramter
561 /// @return TRUE(Success), FALSE(Failure)
562 //-------------------------------------------------------------------------------------------------
MDrv_Dmd_GetParam(DMD_Param * pParam)563 MS_BOOL MDrv_Dmd_GetParam(DMD_Param *pParam)
564 {
565     MS_BOOL             bLock;
566 
567     // Check signal lock
568     if (DVBC_Ioctl(DEVDVBC_IOC_GETLOCK, &bLock))
569     {
570         return FALSE;
571     }
572     if (bLock == FALSE)
573     {
574         return FALSE;
575     }
576 
577     if (DVBC_Ioctl(DEVDVBC_IOC_GETPARAM, pParam))
578     {
579         return FALSE;
580     }
581 
582     return TRUE;
583 }
584 
585 
586 //-------------------------------------------------------------------------------------------------
587 /// Config demodulator register parameter
588 /// @param  pRegParam                \b OUT: Pointer of return paramter
589 /// @return TRUE(Success), FALSE(Failure)
590 //-------------------------------------------------------------------------------------------------
591 
MDrv_Dmd_Config(MS_U8 * pRegParam)592 MS_BOOL MDrv_Dmd_Config(MS_U8 *pRegParam)
593 {
594     if (DVBC_Ioctl(DEVDVBC_IOC_CONFIG, pRegParam))
595     {
596         return FALSE;
597     }
598 
599 	return TRUE;
600 }
601 
602 #endif // DEMOD_MSDVBC
603 
604