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

/*-----------------------------------------------------------------------------
 * $RCSfile: x_tuner.h,v $
 * $Revision: #2 $
 * $Date: 2015/07/17 $
 * $Author: rui.gao $
 *
 * Description:
 *         This header file contains tuner driver specific definitions, which
 *         are exported.
 *---------------------------------------------------------------------------*/

#ifndef _X_TUNER_H_
#define _X_TUNER_H_


/*-----------------------------------------------------------------------------
                    include files
-----------------------------------------------------------------------------*/

#include "u_tuner.h"


/*-----------------------------------------------------------------------------
                    macros, defines, typedefs, enums
 ----------------------------------------------------------------------------*/

/* Connect types */
#define TUNER_CONN_TYPE(_x)             (RM_CONN_TYPE_LAST_ENTRY + (_x))
#define TUNER_CONN_TYPE_SAT_DIG         TUNER_CONN_TYPE(0)
#define TUNER_CONN_TYPE_CAB_DIG         TUNER_CONN_TYPE(1)
#define TUNER_CONN_TYPE_TER_DIG         TUNER_CONN_TYPE(2)
#define TUNER_CONN_TYPE_SAT_ANA         TUNER_CONN_TYPE(3)
#define TUNER_CONN_TYPE_CAB_ANA         TUNER_CONN_TYPE(4)
#define TUNER_CONN_TYPE_TER_ANA         TUNER_CONN_TYPE(5)
#define TUNER_CONN_TYPE_CAB_DIG_OOB_TX  TUNER_CONN_TYPE(6)
#define TUNER_CONN_TYPE_CAB_DIG_OOB_RX  TUNER_CONN_TYPE(7)
#define TUNER_CONN_TYPE_SAT_ANA_SCART_OUT  TUNER_CONN_TYPE(8)
#define TUNER_CONN_TYPE_CAB_ANA_SCART_OUT  TUNER_CONN_TYPE(9)
#define TUNER_CONN_TYPE_TER_ANA_SCART_OUT  TUNER_CONN_TYPE(10)

#define TUNER_CONN_TYPE_PVT_ANA         TUNER_CONN_TYPE(128)

/* Disconnect reasons */
#define TUNER_DISC_REASON(_x)           (RM_DISC_REASON_LAST_ENTRY + (_x))
#define TUNER_DISC_REASON_SIGNAL_LOSS   TUNER_DISC_REASON(0)
#define TUNER_DISC_REASON_UNKNOWN_LNB   TUNER_DISC_REASON(1)
#define TUNER_DISC_REASON_OUT_OF_RANGE  TUNER_DISC_REASON(2)

/* Get operations */
#define TUNER_GET_TYPE(_x)                     (RM_GET_TYPE_LAST_ENTRY + (_x))
#define TUNER_GET_TYPE_SAT_DIG                 TUNER_GET_TYPE(1)
#define TUNER_GET_TYPE_CAB_DIG                 TUNER_GET_TYPE(2)
#define TUNER_GET_TYPE_TER_DIG                 TUNER_GET_TYPE(3)
#define TUNER_GET_TYPE_SAT_ANA                 TUNER_GET_TYPE(4)
#define TUNER_GET_TYPE_CAB_ANA                 TUNER_GET_TYPE(5)
#define TUNER_GET_TYPE_TER_ANA                 TUNER_GET_TYPE(6)
#define TUNER_GET_TYPE_CAB_DIG_OOB_TX          TUNER_GET_TYPE(7)
#define TUNER_GET_TYPE_CAB_DIG_OOB_RX          TUNER_GET_TYPE(8)
#define TUNER_GET_TYPE_SIGNAL_LEVEL            TUNER_GET_TYPE(9)
#define TUNER_GET_TYPE_STATE                   TUNER_GET_TYPE(10)
#define TUNER_GET_TYPE_LNB                     TUNER_GET_TYPE(11)
#define TUNER_GET_TYPE_NO_DISC_NFY             TUNER_GET_TYPE(12)
#define TUNER_GET_TYPE_DBM_SIGNAL_LEVEL        TUNER_GET_TYPE(13)
#define TUNER_GET_TYPE_ANA_ATTRIBUTE           TUNER_GET_TYPE(14)
#define TUNER_GET_TYPE_TV_SYS_GROUP            TUNER_GET_TYPE(15)
#define TUNER_GET_TYPE_TV_SYS_MASK             TUNER_GET_TYPE(16)
#define TUNER_GET_TYPE_DBM_SNR                 TUNER_GET_TYPE(17)
#define TUNER_GET_TYPE_AGC                     TUNER_GET_TYPE(18)
#define TUNER_GET_TYPE_PACKET_ERRORS           TUNER_GET_TYPE(19)
#define TUNER_GET_TYPE_INTERLEAVING            TUNER_GET_TYPE(20)
#define TUNER_GET_TYPE_KHZ_CFO                 TUNER_GET_TYPE(21)
#define TUNER_GET_TYPE_BER                     TUNER_GET_TYPE(22)
#define TUNER_GET_TYPE_DIG_ATTRIBUTE           TUNER_GET_TYPE(23)
#define TUNER_GET_TYPE_RAW_SIGNAL_LEVEL        TUNER_GET_TYPE(24)
#define TUNER_GET_TYPE_SIGNAL_LEVEL_TOLERANCE  TUNER_GET_TYPE(25)
#define TUNER_GET_TYPE_NEXT_STEP_SIZE          TUNER_GET_TYPE(26)
#define TUNER_GET_TYPE_PACKET_STATUS           TUNER_GET_TYPE(27)
#define TUNER_GET_TYPE_BER_TOLERANCE           TUNER_GET_TYPE(28)
#define TUNER_GET_TYPE_CELL_ID                 TUNER_GET_TYPE(29)
#define TUNER_GET_TYPE_SIGNAL_STATUS		   TUNER_GET_TYPE(30)
#define TUNER_GET_TYPE_RFAGC                   TUNER_GET_TYPE(31)
#define TUNER_GET_TYPE_TS_FMT                  TUNER_GET_TYPE(32)
#define TUNER_GET_TYPE_UEC                     TUNER_GET_TYPE(33)
#define TUNER_GET_TYPE_SQI                     TUNER_GET_TYPE(34)
#define TUNER_GET_TYPE_SSI                     TUNER_GET_TYPE(35)
#define TUNER_GET_TYPE_EWS_STATUS              TUNER_GET_TYPE(36)
#define TUNER_GET_TYPE_DVBT2_PLP               TUNER_GET_TYPE(37)
#define TUNER_GET_TYPE_NL                      TUNER_GET_TYPE(38)//d120114_Haibo:Get Noise level
#define TUNER_GET_TYPE_ATD_DESCRAMBLE_PROPERTY     TUNER_GET_TYPE(39)
#define TUNER_GET_TYPE_PRE_VBER                TUNER_GET_TYPE(40) //wenming 2012-3-24: for getting pre vber info
#define TUNER_GET_TYPE_IFAGC                   TUNER_GET_TYPE(41)
#define TUNER_GET_TYPE_SCAN_PROGRESS           TUNER_GET_TYPE(42)
#define TUNER_GET_TYPE_FE_STATUS               TUNER_GET_TYPE(43) //weile 2012-12-11 for supporting fe status to mw
#define TUNER_GET_TYPE_LNB_SHORT_STATUS        TUNER_GET_TYPE(44)//zhilin 2013-01-23:sync code for dvbs cxd3829 over-load detect function
#define TUNER_GET_TYPE_BER_UPPER			   TUNER_GET_TYPE(45)//Yanyan 20130523: get BER/PER UPPER&LOWER value for ISDBT
#define TUNER_GET_TYPE_BER_LOWER			   TUNER_GET_TYPE(46)
#define TUNER_GET_TYPE_PER_UPPER			   TUNER_GET_TYPE(47)
#define TUNER_GET_TYPE_PER_LOWER			   TUNER_GET_TYPE(48)
#define TUNER_GET_TYPE_TMCC_INFO			   TUNER_GET_TYPE(49)
#define TUNER_GET_TYPE_DIG_DEMOD_STATUS	       TUNER_GET_TYPE(50)
#define TUNER_GET_TYPE_LNB_STATUS              TUNER_GET_TYPE(51)
#define TUNER_GET_TYPE_AGC_STATUS              TUNER_GET_TYPE(52) //zhishang add for maple 2014.4.22
#define TUNER_GET_TYPE_TS_LOCK_STATUS          TUNER_GET_TYPE(53)//Xiaohong Add for Sakura
#define TUNER_GET_TYPE_DEMOD_LOCK_STATUS              TUNER_GET_TYPE(54)//Xiaohong Add for Sakura
#define TUNER_GET_TYPE_TUNER_CHIP_REVISION              TUNER_GET_TYPE(55)//Xiaohong Add for Sakura
#define TUNER_GET_TYPE_DEMOD_CHIP_REVISION              TUNER_GET_TYPE(56)//Xiaohong Add for Sakura
#define TUNER_GET_TYPE_LAYER_NUM              TUNER_GET_TYPE(57)//Xiaohong Add for Sakura
#define TUNER_GET_TYPE_TUNER_STATUS              TUNER_GET_TYPE(58) //Xiaohong Add for Sakura
#define TUNER_GET_TYPE_DIG_INSTANCE_NUM        TUNER_GET_TYPE(59)
#define TUNER_GET_TYPE_ACTUAL_FREQ              TUNER_GET_TYPE(60)
#define TUNER_GET_TYPE_SAT_UI4_DATA             TUNER_GET_TYPE(61)   //Gao Rui Add for DVBS get ui4_data to MW
#define TUNER_GET_TYPE_SYMBOL_RATE              TUNER_GET_TYPE(62)
#define TUNER_GET_TYPE_QAM_MODE                 TUNER_GET_TYPE(63)
#define TUNER_GET_TYPE_LNA_SETTING              TUNER_GET_TYPE(64)
#define TUNER_GET_TYPE_ATSC3_PLP               TUNER_GET_TYPE(65)    // add for ATSC3.0 PLP 
#define TUNER_GET_TYPE_IQ_SENSE	               TUNER_GET_TYPE(66)	//add for DVBS get IQ sense info
#define TUNER_GET_TYPE_SCIF_INVERTED			TUNER_GET_TYPE(67)	//add for DVBS get SCIF Inverted


/* Set operations */
#define TUNER_SET_TYPE(_x)                  (RM_SET_TYPE_LAST_ENTRY + (_x))
#define TUNER_SET_TYPE_LNB                  TUNER_SET_TYPE(11)
#define TUNER_SET_TYPE_NO_DISC_NFY         (TUNER_SET_TYPE(12) | RM_SET_TYPE_ARG_NO_REF)
#define TUNER_SET_TYPE_TV_SYS_MASK          TUNER_SET_TYPE(13)
#define TUNER_SET_TYPE_CAB_ANA_TUNE_INFO    TUNER_SET_TYPE(14)
#define TUNER_SET_TYPE_SAT_ANA_TUNE_INFO    TUNER_SET_TYPE(15)
#define TUNER_SET_TYPE_TER_ANA_TUNE_INFO    TUNER_SET_TYPE(16)
#define TUNER_SET_TYPE_CI_CARD_TYPE         TUNER_SET_TYPE(17)
#define TUNER_SET_TYPE_DIAGNOSTICS         (TUNER_SET_TYPE(18) | RM_SET_TYPE_ARG_NO_REF)
#define TUNER_SET_TYPE_CAB_DIG_TUNE_INFO    TUNER_SET_TYPE(19)
#define TUNER_SET_TYPE_SAT_DIG_TUNE_INFO    TUNER_SET_TYPE(20)
#define TUNER_SET_TYPE_TER_DIG_TUNE_INFO    TUNER_SET_TYPE(21)
#define TUNER_SET_TYPE_TS_SP                TUNER_SET_TYPE(22)
#define TUNER_SET_TYPE_TS_TRISTATE          TUNER_SET_TYPE(23)
#define TUNER_SET_TYPE_FREQ_AUTO_NFY        TUNER_SET_TYPE(24)
#define TUNER_SET_TYPE_DISEQC_CMD           TUNER_SET_TYPE(25)
#define TUNER_SET_TYPE_SCAN_CONTINUE        TUNER_SET_TYPE(26)
#define TUNER_SET_TYPE_TS_OUTPUT            TUNER_SET_TYPE(27)
#define TUNER_SET_TYPE_NO_AUD_VID           TUNER_SET_TYPE(28)
#define TUNER_SET_TYPE_ATD_DESCRAMBLE_PROPERTY  TUNER_SET_TYPE(29)
#define TUNER_SET_TYPE_UNICABLE_BLIND_SCAN  TUNER_SET_TYPE(30)
#define TUNER_SET_TYPE_DEMOD_EARLY_BREAK_CTL TUNER_SET_TYPE(31)
#define TUNER_SET_TYPE_RESET_CMD             TUNER_SET_TYPE(32)
#define TUNER_SET_TYPE_DEMOD_CLOCK           TUNER_SET_TYPE(33)
#define TUNER_SET_TYPE_TS_GAP_LENGTH         TUNER_SET_TYPE(34)
#define TUNER_SET_TYPE_DEMOD_PARAMETER       TUNER_SET_TYPE(35)
#define TUNER_SET_TYPE_LNBC_SET_SLEEP        TUNER_SET_TYPE(36) //CC_DEMOD_NON_DVBS_LNBC_OUTPUT_0V
#define TUNER_SET_TYPE_DEMOD_SHUTDOWN        TUNER_SET_TYPE(37) //CC_DEMOD_SUPPORT_SHUTDOWN_MODE
#define TUNER_SET_TYPE_TSL_REC_ID            TUNER_SET_TYPE(38)
#define TUNER_SET_TYPE_DEMOD_FLAG            TUNER_SET_TYPE(39)  //TO CLEAN DEMOD INIT FLAG
#define TUNER_SET_TYPE_TS_PID                TUNER_SET_TYPE(40)
#define TUNER_SET_TYPE_DEL_TS_PID            TUNER_SET_TYPE(41)
#define TUNER_SET_TYPE_ATSC3_PLP             TUNER_SET_TYPE(42)  // add for ATSC3.0 PLP 


/* Flags used during registration */
#define TUNER_FLAG_PVT_CONN_DATA                    ((UINT32) 0x00010000)
#define TUNER_FLAG_SIG_LEVEL_NOT_SUPPORTED          ((UINT32) 0x00020000)
#define TUNER_FLAG_DBM_LEVEL_NOT_SUPPORTED          ((UINT32) 0x00040000)
#define TUNER_FLAG_DEC_REQ_FOR_TV_SIG_DETECTION     ((UINT32) 0x00080000)
#define TUNER_FLAG_DEC_REQ_FOR_COLOR_SYS_DETECTION  ((UINT32) 0x00100000)


/* DVB delivery system descriptor tune setup */
#define TUNER_DVB_DESCR_TUNE_LEN  13

typedef UINT8 TUNER_DVB_DESCR_TUNE_INFO_T [TUNER_DVB_DESCR_TUNE_LEN];

/* CI CAM type conditions */
typedef enum
{
    TUNER_CI_CARD_TYPE_1 = 0,
    TUNER_CI_CARD_TYPE_2,
    TUNER_CI_CARD_TYPE_3
}   TUNER_CI_TYPE_T;

/* State conditions */
typedef enum
{
    TUNER_STATE_DISCONNECTED = 0,
    TUNER_STATE_CONNECTING,
    TUNER_STATE_CONNECTED,
    TUNER_STATE_DISCONNECTING
}   TUNER_STATE_T;

/* Status conditions */
typedef enum
{
    TUNER_COND_STATUS_GOT_SIGNAL = 0,
    TUNER_COND_STATUS_LOST_SIGNAL,
    TUNER_COND_STATUS_EWS_ON,
    TUNER_COND_STATUS_EWS_OFF,
    TUNER_COND_STATUS_AUTOSCAN_END,
    TUNER_COND_STATUS_SHORT_ON,
    TUNER_COND_STATUS_SHORT_OFF,
    TUNER_COND_STATUS_UNKNOWN                /* weile 2012-12-11 add here for supporting fe status to mw */
}   TUNER_COND_STATUS_T;

/* Tuner state structure */
typedef struct
{
    TUNER_STATE_T  e_state;
    BOOL           b_dig_signal;
}   TUNER_STATE_INFO_T;

/* Analog tuner attribute */
typedef struct
{
    UINT32  ui4_lower_bound_freq;       /* Lower bound of supported range */
    UINT32  ui4_upper_bound_freq;       /* Upper bound of supported range */
    UINT32  ui4_fine_tune_delta_freq;   /* Fine-tune capability */
}   TUNER_ANA_ATTRIBUTE_T;

/*Analog tuner descramble box attribute */
typedef struct
{
     BOOL   fgDescrambleBoxOn;     /*Descramble ON(1)/OFF(0)*/
	 BOOL   fgGenSnowChChg;        /*If generate snow mode when do channel change*/
	 UINT16 u2CVBSDelay;           /*CVBS delay*/
	 UINT16 u2ChkTvdDelay;         /*Delay before check TVD lock status*/
	 UINT16 u2ScalingGain;         /*Scaling gain */
}TUNER_DESCRAMBLE_ATTRIBUTE_T;

/* Digital tuner attribute */
typedef struct
{
    UINT32  ui4_lower_bound_freq;       /* Lower bound of supported range */
    UINT32  ui4_upper_bound_freq;       /* Upper bound of supported range */
    UINT32  ui4_fine_tune_delta_freq;   /* Fine-tune capability */
    BOOL    b_auto_qam_capability;      /*Auto QAM detection capability.*/
    BOOL    b_auto_sr_capability;       /*Auto SR detection capability.*/
	BOOL    b_drv_notify_progress_capability;/*driver notify blind scan progress*/
}   TUNER_DIG_ATTRIBUTE_T;

typedef struct
{
    UINT16  ui2_antenna_num;
    UINT16  ui2_cable_num;
    UINT16  ui2_satellite_num;
    UINT16  ui2_analog_num;
}   TUNER_DIG_INSTANCE_NUM_T;


/* TV system group structure */
typedef struct
{
    UINT8   ui1_tv_sys_group_idx;       /* TV system group index */
    UINT32  ui4_tv_sys_mask;            /* Mask of TV systems */
}   TUNER_TV_SYS_GROUP_INFO_T;

/* Step size structure */
typedef struct
{
    INT32  i4_step_size;
}   TUNER_STEP_SIZE_T;

/* Packet status structure */
typedef struct
{
    UINT32  ui4_errors;
    UINT8   ui1_error_rate;
    UINT8   ui1_lost_rate;
}   TUNER_PACKET_STATUS_T;

/*********** MPEG Format structure *************************************/
typedef enum
{
    SP_UNKNOWN,
    SP_SERIAL,
    SP_PARALLEL,
} SIG_SP_T;

/* Polarities */
typedef enum
{
    POL_UNKNOWN_ACTIVE,
    POL_HIGH_ACTIVE,
    POL_LOW_ACTIVE,
} SIG_POL_T;

typedef enum
{
    BOOL_UNKNOWN,
    BOOL_FALSE,
    BOOL_TRUE,
} BOOL_T;

/* TS format structure */
typedef struct 
{
    SIG_SP_T  SerOrPar;
    SIG_POL_T ClockPolarity;
    BOOL_T    fgTristate ;
    UINT32 u4TsClockInKHz; // get TS clock in kHz
    BOOL_T fgGatedClock; // get if it is gated clock
    BOOL_T fgExternalDemod; // get if it is 3rd-party or in-house demod. ex: 3rd-party BOOL_TRUE; In-house: BOOL_FALSE.
} TS_FMT_T;

/* To use with ANALOG_BRDCST_CTRL_GET/SET_SCART_BYPASS commands */
/*------------------------------------------------------------------*/
/*! @struct ANALOG_BRDCST_CTRL_FREQ_AUTO_NFY_T
 *  @brief  Structure for indicating if frequency notify is enabled.
 *  @code
 *  typedef struct
 *  {
 *      BOOL  b_enable_freq_auto_nfy;
 *  }   ANALOG_BRDCST_CTRL_FREQ_AUTO_NFY_T;
 *  @endcode
 *  @li@c  b_enable_freq_auto_nfy   - Indicates if frequency notify is enabled or not.
 */
/*------------------------------------------------------------------*/
typedef struct
{
    BOOL  b_enable_freq_auto_nfy;
}   ANALOG_BRDCST_CTRL_FREQ_AUTO_NFY_T;


typedef TUNER_TV_SYS_GROUP_INFO_T TUNER_TV_SYS_GROUP_INFO;
typedef TUNER_ANA_ATTRIBUTE_T TUNER_ANA_ATTRIBUTE;

#endif /* _X_TUNER_H_ */

