/* 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.
 *
 *****************************************************************************/

/*-----------------------------------------------------------------------------
 *
 *
 *---------------------------------------------------------------------------*/

/** @file nim_drvif.h
 *  nim_drvif.c function prototype declaration.
 */

#ifndef NIM_DRVIF_H
#define NIM_DRVIF_H

//---------------------------------------------------------------------------
// Include files
//---------------------------------------------------------------------------
#include "x_lint.h"
#include "tuner_if.h"
LINT_EXT_HEADER_BEGIN
#include "x_typedef.h"
#include "u_rm.h"
#include "u_tuner.h"


LINT_EXT_HEADER_END

//---------------------------------------------------------------------------
// Macro definitions
//---------------------------------------------------------------------------
#define QAM16    0
#define QAM32    1
#define QAM64    4
#define QAM128   2
#define QAM256   3

//---------------------------------------------------------------------------
// Type definitions
//---------------------------------------------------------------------------
// nim break call back

typedef INT32 (*PF_NIM_BREAK_CB_T)(void);

#if 0
/** CABLE_CHANNEL_SIGNAL_T: cable channel parameter.
 *
 */
typedef struct _CABLE_CHANNEL_SIGNAL_T
{
    INT32 i4FrequencyK;   // in KHz
    UINT32 u4SymbolRateK; // in KSymbol
    INT32 i4QamSize;      // 16/32/64/128
//    UINT32 e_bandwidth;                /*EU mw: BW_UNKNOWN = 0, BW_6_MHz, BW_7_MHz, BW_8_MHz*/
//    UINT32 e_hierarchy_priority;       /*EU mw: HIR_PRIORITY_UNKNOWN = 0, HIR_PRIORITY_NONE, HIR_PRIORITY_HIGH, HIR_PRIORITY_LOW */

} CABLE_CHANNEL_SIGNAL_T;
#endif
//for analog range scan cli command
typedef struct{
	INT32                 s4Freq;
	UINT8               u1SubSysIdx;
}ANA_SIGNAL_INFO;

/** TUNER_ALL_INFO_T
 *
 */
typedef union
{
    TUNER_CAB_DIG_TUNE_INFO_T t_cab_tuner_info;
    TUNER_TER_DIG_TUNE_INFO_T t_ter_tuner_info;
    TUNER_CAB_ANA_TUNE_INFO_T           t_cana_tuner_info;
    TUNER_TER_ANA_TUNE_INFO_T           t_tana_tuner_info;    
    TUNER_CAB_DIG_OOB_RX_TUNE_INFO_T t_orx_tuner_info;
    TUNER_CAB_DIG_OOB_TX_TUNE_INFO_T t_otx_tuner_info;
} TUNER_ALL_INFO_T;

typedef void (*PFN_NIM_ERR_NOTIFY)(UINT32 u4ErrCnt, UINT32 u4MicroSec);

//---------------------------------------------------------------------------
// Inter-file functions
//---------------------------------------------------------------------------
extern INT32 TunerNimTest(UINT16 TunerID);

/*wenming 12-2-15:add for range search*/
extern VOID vSetAutoSearchType(UINT32 u4Type);
extern BOOL TunerAcq(UINT16 TunerID, DRV_CONN_TYPE_T e_conn_type,
            void* pv_conn_info, SIZE_T z_conn_info_len,
            x_break_fct _BreakFct, void* pvArg);
void NIM_RangeSearch(DRV_CONN_TYPE_T e_conn_type, UINT32 u4FreqStartKHz,UINT32 u4FreqEndKHz, const CHAR **szArgv);


//---------------------------------------------------------------------------
// Public functions
//---------------------------------------------------------------------------

/** Tuner Primitive API: stop monitor.
 *
 *
 *  @return INT32.\n
 *    always return 0
 */
INT32 NIM_MonitorStop(void);

/** Tuner Primitive API: start monitor.
 *
 *
 *  @return INT32.\n
 *      always return 0
 */
INT32 NIM_MonitorStart(void);

/** Tuner Primitive API: Open tuner.
 *
 *
 *  @return INT32.\n
 *          If success, return DRVAPI_TUNER_OK( = 0).
 *          If fail, return error code ( < 0).
 */
INT32 NIM_Open(void);

/** Tuner Primitive API: close tuner.
 *
 *
 *  @return INT32.\n
 *          If success, return DRVAPI_TUNER_OK( = 0).
 *          If fail, return error code ( < 0).
 */
INT32 NIM_Close(void);

/** Tuner Primitive API: tuner set cable parameter.
 *
 *  @param[in]     u4FreqK      frequency khz.
 *  @param[in]     u4SymbolK    k_symbol_sec.
 *  @param[in]     i4QamSize    QAM size.
 *  @param[in]     fgSpectrumInversion    SpectrumInversion.
 *  @param[in]     pfNimBreak    break function.
 *  @param[in]     uAcqType      acq. type.
 *  @param[in]     uLoopCounts   re-try acq. times.
 *  @param[in]     u4DelayMs     delay n ms for every re-try acq.
 *
 *  @return INT32.\n
 *          If success, return DRVAPI_TUNER_OK( = 0).
 *          If fail, return error code ( < 0).
 */
//void NIM_SetCable(UINT32 u4FreqK, UINT32 u4SymbolK, INT32 i4QamSize, BOOL fgSpectrumInversion,
//                  PF_NIM_BREAK_CB_T pfNimBreak);
//void NIM_SetCable(UINT32 u4FreqK, UINT32 u4SymbolK, INT32 i4QamSize,
//    BOOL fgSpectrumInversion, PF_NIM_BREAK_CB_T pfNimBreak, TUNER_ACQ_ACTION_T uAcqType, UINT32 uLoopCounts);
BOOL NIM_SetCable(UINT32 u4FreqK, UINT32 u4SymbolK, INT32 i4QamSize,
    BOOL fgSpectrumInversion, PF_NIM_BREAK_CB_T pfNimBreak, TUNER_ACQ_ACTION_T uAcqType, UINT32 uLoopCounts, UINT32 u4DelayMs);

/** Tuner Primitive API: Set Bandwidth.
 *
 *
 *  @param[in]     u4Bandwidth   bandwidth.
 *
 *  @return void.\n
 *
 */
void NIM_SetBW(UINT32 u4Bandwidth);
/** Tuner Primitive API: Set Bandwidth.
 *
 *
 *  @param[in]     u4Bandwidth   bandwidth.
 *
 *  @return void.\n
 *
 */
void NIM_AdvSetBW(UINT32 u4Bandwidth);


/** Tuner Primitive API: tuner get cable signal parameter.
 *
 *  @param[out]     prCableSignal     pointer to cable signal parameter structure.
 *
 *  @return void.\n
 */
void NIM_GetCableSignal(SIGNAL *prCableSignal);

/** Tuner Primitive API: tuner get cable signal level.
 *
 *
 *  @return UINT8.\n
 *          return signal level
 */
UINT8 NIM_GetCableSignalLevel(void); // AGC2 * 100 / 0x3ff

/** Tuner Primitive API: tuner get cable signal SNR.
 *
 *
 *  @return UINT16.\n
 *      return signal SNR
 */
UINT16 NIM_GetCableSignalSNR(void);

/** Tuner Primitive API: tuner get cable signal PER.
 *
 *
 *  @return UINT16.\n
 *      return signal PER
 */
UINT16 NIM_GetCableSignalPER(void);

/** Tuner Primitive API: Nim test
 *
 *
 *  @return UINT16.\n
 *      return 0    ok
 *      return !=0  fail
 */
INT32 NIM_TunerNimTest(void);

/** Tuner Primitive API: tuner get cable sync.
 *
 *
 *  @return UINT16.\n
 *      success =1, fail <=0
 */
INT16 NIM_GetCableSync(void); // INT16 CheckLock()

INT32 _nim_SetTimeout2(UINT32 u4TimeoutMs);
INT32 _nim_Breaker(void);

/** Tuner Primitive API: tuner get error count.
 *
 *
 *  @return UINT32.\n
 *      return error count
 */
  
#ifndef CC_MT5880_CODE_SIZE_CTRL
UINT32 NIM_GetErrorCnt(void);
#endif
//void NIM_RegErrNotifyFunc(PFN_NIM_ERR_NOTIFY pfFunc);
BOOL NIM_PollingToggle(void);
UINT32 NIM_SetPollPeriod(UINT32 u4Period);

/** Tuner Primitive API: tuner detach I2C.
 *
 *
 *  @return UINT32.\n
 *      always return 0
 */
UINT32 NIM_DetachI2C(UINT32 u4Param);
UINT32 NIM_SetFineTune(BOOL fgFineTune);//lei_code_dB110525 for build warning;

#ifndef CC_MT5880_CODE_SIZE_CTRL
void NIM_RegErrNotifyFunc(PFN_NIM_ERR_NOTIFY pfFunc);
#endif
// Added by rocky_huang 2006/07/14
EXTERN void TunerDetachI2C(UINT16 TunerID, BOOL bDetach);
EXTERN void TunerDetachMW(UINT16 TunerID, BOOL bDetach);
EXTERN void TunerCommand(UINT16 TunerID, INT32 i4Argc, const CHAR** aszArgv);
EXTERN void TunerDtdCommand(UINT16 TunerID, INT32 i4Argc, const CHAR** aszArgv);
EXTERN void TunerAtdCommand(UINT16 TunerID, INT32 i4Argc, const CHAR** aszArgv);
EXTERN void CLISetRemapConnType(DRV_CONN_TYPE_T e_conn_type);

/** Tuner Primitive API: Set connection type.
 *
 *
 *  @param[in]     e_conn_type   connection type.
 *
 *  @return void.\n
 *
 */
void NIM_SetConnType(DRV_CONN_TYPE_T e_conn_type);
void NIM_SetATSC3Flag(UINT32 Demod_type);

DRV_CONN_TYPE_T NIM_GetConnType(void);
/** Tuner Primitive API: Set modulation type.
 *
 *
 *  @param[in]     Mod   modulation type.
 *
 *  @return void.\n
 *
 */
void NIM_SetMod(TUNER_MODULATION_T Mod);

/** Tuner Primitive API: Set Tuner ID.
 *
 *
 *  @param[in]     TunerID   Tuner ID.
 *
 *  @return void.\n
 *
 */
void NIM_SetTunerID(UINT16 TunerID);

void NIM_SetRSetting(UCHAR u1RSv);

void NIM_SetDA(UCHAR u1RSv);


/** Tuner Primitive API: Set SLT Mode.
 *
 *
 *  @param[in]     SltMode   1:DVB/0:DTD.
 *
 *  @return void.\n
 *
 */
void NIM_SetSltMode(UINT8 SltMode);

/** Tuner Primitive API: Get version.
 *
 *
 *  @return void.\n
 *
 */
void NIM_CmdSetConnType(UINT32 e_conn_type);
void NIM_CmdVer(void);

void NIM_CmdHostCmd(INT32 i4Argc, const CHAR **szArgv);
void NIM_CmdDtdCmd(INT32 i4Argc, const CHAR **szArgv);
void NIM_CmdAtdCmd(INT32 i4Argc, const CHAR **szArgv);

/** Tuner Primitive API: Get Tuner ID.
 *
 *
 *
 *  @return UINT16.\n
 *
 */
UINT16 NIM_GetTunerID(void);

/** Tuner Primitive API: Get debug level.
 *
 *
 *  @return UCHAR.\n
 *
 */
UCHAR NIM_GetDbgLvl(void);

/** Tuner Primitive API: Set debug level.
 *
 *
 *  @return void.\n
 *
 */
void NIM_SetDbgLvl(UCHAR dbg_lvl);

/** Tuner Primitive API: Get Ts Format.
 *
 *
 *  @return void.\n
 *
 */
void NIM_GetTsFmt(UCHAR *pucSerorPal, UCHAR *pucClockPol, UINT32 *pu4TsClock, BOOL *pbGatedClock, BOOL *pbExternalDemod);

/** Tuner Primitive API: Set Ts SP.
 *
 *
 *  @return void.\n
 *
 */
void NIM_SetTsSP(UCHAR ucSerorPal);

/** Tuner Primitive API: Set Ts tristate.
 *
 *
 *  @return void.\n
 *
 */
void NIM_SetTsTristate(UCHAR ucTristate);

/** Tuner Primitive API: Set Tuner control-word.
 *
 *
 *  @return void.\n
 *
 */
void NIM_SetTuner(UCHAR ucAddr, UCHAR *pucData, UCHAR ucLen);

/** Tuner Primitive API: Get Tuner Status.
 *
 *
 *  @return void.\n
 *
 */
void NIM_GetTuner(UCHAR ucAddr, UCHAR *pucData, UCHAR ucLen);

/** Tuner Primitive API: Get Tuner register value.
 *
 *
 *  @return void.\n
 *
 */
void NIM_GetTunerEx(UCHAR ucAddr, UCHAR ucReg, UCHAR *pucData, UCHAR ucLen);

void NIM_SetAnaChList(INT32 s4Freq,UINT8 u1SubSysIdx);

ANA_SIGNAL_INFO * NIM_GetAnaChInfo(UINT32 *pu4ChannelIdx);
/** Tuner Primitive API: set dvbs blind scan continue
 *
 *
 *  @return void.\n
 *
 */
void NimCmdSetBSContinue(void);
void NimCmdSetBSCStop(void);

#endif // NIM_DRVIF_H
