/* SPDX-License-Identifier: LGPL-2.1-only OR BSD-3-Clause */
/******************************************************************************
 *
 * This file is provided under a dual license.  When you use or
 * distribute this software, you may choose to be licensed under
 * version 2.1 of the GNU Lesser General Public License
 * ("LGPLv2.1 License") or BSD License.
 *
 * LGPLv2.1 License
 *
 * Copyright(C) 2019 MediaTek Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2.1 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html for more
 * details.
 *
 * BSD LICENSE
 *
 * Copyright(C) 2019 MediaTek Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *****************************************************************************/

/*-----------------------------------------------------------------------------
 *
 * $Author$
 * $Date$
 * $RCSfile: pi_atdna_if.h,v $ 
 * $Revision$
 *
 *---------------------------------------------------------------------------*/

/** @file pi_atdana_if.h 
 *  NA tuner analog pi primitive API 
 */

#ifndef PI_ATDNA_IF_H
#define PI_ATDNA_IF_H

//-----------------------------------------------------------------------------
// Include files
//-----------------------------------------------------------------------------
#include "x_typedef.h"
#include "fe_tuner_common_if.h"

//-----------------------------------------------------------------------------
// Configurations
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// Constant definitions
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// Type definitions
//-----------------------------------------------------------------------------
//#ifndef PATD_SPECIFIC_CTX
typedef VOID *PATD_SPECIFIC_CTX;
//#endif

#ifndef tuner_break_fct
#define tuner_break_fct
typedef INT32     (*x_break_fct)(void *pArg);
#endif

typedef struct _ATV_PI_CTX_T
{
// Hardware Configure
    UINT8       u1TunerType;
    UINT8       u1RF_I2cAddr;
    UINT8       u1IF_I2cAddr;
    UINT16      u2IF_Freq;
    UINT16      u2LO_Step;
    UINT32      u4Freq;
    UINT8       u1DEMOD_state;
    UINT8       u1AnaMode;
    UINT8       u1VIFLock;
    UINT8       u1AnaDemoTop;//for MW set to 9886 top tune
//    CHAR        aSW_Ver_HW_Ver[24];
// Access Link
    VOID        *pDigiTunerCtx;
    x_break_fct isBreak;
//    VOID        *isBreak;
    VOID        *pvBreakArg;

//jackson move this, do not need this anymore
//    struct _ANA_TUNER_FUNC_T    *psTunerFunc;

    // TV Color / Sound Sub-System
//#if defined(TUNER_SUPPORT_MULTI_SYS_TV) || 1
//    ATV_SUB_SYS_CTX_T   sAtvSubSysCtx;
    UINT8       u1SubSysIdx;
    UINT32      u4TvSubSysMask;
    UINT8       ucTvSysGroupNum;
    UINT32      *pu4TvSysGroup;
    VOID        *psTunerAnaAttr;
//#endif
// ATD PI Specific context
//#if fcNEW_ANA_PIPD_ARCH
    PATD_SPECIFIC_CTX   ptATDSpecificCtx;
//#endif
} ATV_PI_CTX_T, *PATV_PI_CTX_T;

//-----------------------------------------------------------------------------
// Macro definitions
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// Public functions
//-----------------------------------------------------------------------------
/** fgDrvTunerCheckTVDLock
 *  check whether tv decoder can  decode  IF signal or not 
 *  @param  bTryLoopCnt  Re-check TVD times if TVD unlock
 *  @retval TRUE         TVD lock signal(CVBS)
 *  @retval FALSE        TVD can't lock signal(CVBS)
 */
 //----------------------------------------------------------------------------
EXTERN BOOL fgDrvTunerCheckTVDLock( UINT16 bTryLoopCnt );
//----------------------------------------------------------------------------
/** AnalogPINA_TunerSetFreq
 *  Set Tuner LO tune to specified frequency
 *  @param  psAtvPiCtx     pointer of tuner device context
 *  @param  wFreqInKHz   RF frequency in KHz.
 *  @param  u1AnaMode   Modulation Type
 *  @retval  0            Set tuner successfully
 *  @retval  1             Set tuner set fail  
 */ 
 //----------------------------------------------------------------------------
EXTERN BOOL  AnalogPINA_TunerSetFreq(ATV_PI_CTX_T *psAtvPiCtx, UINT32 wFreqInKHz, UINT8 u1AnaMode);
//----------------------------------------------------------------------------- 
/**AnalogPINA_TunerCheckVIFLock
 *  Check if programmed frequency has strong VIF signal nearby
 *  @param  psAtvPiCtx    	a type pointer use to point to struct ATV_PI_CTX_T.
 *  @param  wFreqInKHz          The frequenccy in KHz need to check the VIF status
 *  @retval   	 2  => Current frequency is very close to the nominal VIF.
           		 1  => Current frequency is is near the nominal VIF.
           		 0  => Current frequency is not close to the nominal VIF yet.
 */
//-----------------------------------------------------------------------------
EXTERN UINT8 AnalogPINA_TunerCheckVIFLock(ATV_PI_CTX_T *psAtvPiCtx, UINT32 wFreqInKHz);

//AnalogPINA_TunerSearchNearbyFreq return value, bit 
#define cpANA_LOCK_STS_VIF      0 //bit 0
#define cpANA_LOCK_STS_TVD      1 //bit 1
#define cpANA_LOCK_STS_BREAK    2 //bit 2
//----------------------------------------------------------------------------- 
/** AnalogPINA_TunerSearchNearbyFreq
 *  Find the freq. value that near center frequency of the channel.
 *  @param  psAtvPiCtx  	a type pointer use to point to struct ATV_PI_CTX_T.
 *  @param  *wFreqInKHz  	THe point of frequenccy in KHz need to check the VIF status
 *  @param  _BreakFct  	 	The break function
 *  @param  *pvArg  		The argument of break function        
 *  @retval  bit0           1:VIF lock, 0: VIF unlock
 *  @retval  bit1           1:TVD lock, 0: TVD unlock
 *  @retval  bit2           1:break happen, 0: no break
 */
//-----------------------------------------------------------------------------
EXTERN UINT8 AnalogPINA_TunerSearchNearbyFreq(ATV_PI_CTX_T *psAtvPiCtx,  UINT32 *wFreqInKHz, x_break_fct _BreakFct, VOID *pvArg);
//------------------------------------------------------------------------------
/**  AnalogPINA_TunerMonitor
 *  Find the freq. value that near center frequency of the channel.
 *  @param  psAtvPiCtx  	a type pointer use to point to struct ATV_PI_CTX_T.
 *  @param  *wFreqInKHz  	THe point of frequenccy in KHz need to check the VIF status
 *  @param  _BreakFct  	 	The break function
 *  @param  *pvArg  		The argument of break function
 *  @retval   TRUE  =>  center freq. found
            	 FALSE =>  center freq. not found (no signal on this channel)
 */
//------------------------------------------------------------------------------
EXTERN UINT8 AnalogPINA_TunerMonitor(ATV_PI_CTX_T *psAtvPiCtx, UINT32 *wFreqInKHz, x_break_fct _BreakFct, VOID *pvArg);
#endif /* PI_ATDNA_IF_H */

