/**
 * @file hv_drv_Mprt.c
 * @brief mprt spi module driver
 * @details This module provide interfaces for Hiview-Madia-Ware.
 * @author HiView SoC Software Team
 * @version 0.0.1
 * @date 20230201
 * @copyright Copyright(c),2022-9, Hiview Software. All rights reserved.
 * @par History:
 * <table>
 * <tr><th>Author                         <th>Date             <th>Details
 * <tr><td>HiView SoC Software Team   <td>20230201       <td>Create and first Version.
 * </table>
 **/

#include "hv_drv_Mprt.h"
#include "hv_chip_Irq.h"
#include "hv_cal_mprt.h"
#include "hv_comm_Assert.h"
#include "hv_comm_DataBase.h"
#include "hv_drv_Gpio.h"

/*全局背光12开关，enable=1是关，=0是开（非全局PWM开关）*/
VOID Hv_Drv_MPRT_SetOutHighFreqPwmEnable(BOOL bEn)
{
    //Hv_Cal_MPRT_PwmEnable(HV_PROJECT_CONFIG_MPRT_HIGHT_ENABLE_PORT, bEn ? 0 : 1);
    Hv_Drv_Gpio_SetPinLevel(HV_BOARD_CONFIG_GPIO_BLK_EN, bEn ? GPIO_LEVEL_LOW : GPIO_LEVEL_HIGH);
    return;
}

BOOL Hv_Drv_MPRT_GetOutHighFreqPwmEnable(VOID)
{
    if (GPIO_LEVEL_LOW == Hv_Drv_Gpio_GetPinLevel(HV_BOARD_CONFIG_GPIO_BLK_EN))
    {
        return HV_PROJECT_CONFIG_BACKLIGHT_ENABLE;
    }
    else
    {
        return HV_PROJECT_CONFIG_BACKLIGHT_DISABLE;
    }
}

/*全局PWM初始化*/
VOID Hv_Drv_MPRT_SetOutHighFreqPwm(UCHAR8 ucPort)
{
    USHORT16 usStart = 0;
    UINT32 uiVal = HV_PROJECT_CONFIG_MPRT_HIGHT_FREQ_PWM_DUTY * HV_PROJECT_CONFIG_MPRT_HIGHT_FREQ_PWM_TOTAL / 100;  // 50是默认占空比的值
    usStart = HV_PROJECT_CONFIG_MPRT_HIGHT_FREQ_PWM_TOTAL - uiVal;
    HV_LOG_DEBUG(VIDEO, "init ucPort %d uiVal %u, usStart %u", ucPort, uiVal, usStart);

    /*port输出*/
    Hv_Cal_MPRT_PwmSetMux(ucPort, 1 << ucPort);
    /*设置为mode3，free run 模式*/
    Hv_Cal_MPRT_PwmSetMode(ucPort, 3);

    /* 设置frequency 需要查表计算得到total和mult的值 */
#if (HV_PROJECT_CONFIG_FULL_LOCALDIMMING == HV_CONFIG_ON)
    Hv_Cal_MPRT_PwmSetStart(ucPort, usStart);
    Hv_Cal_MPRT_PwmSetValue(ucPort, uiVal);
#endif
    Hv_Cal_MPRT_PwmSetMult(ucPort, HV_PROJECT_CONFIG_MPRT_HIGHT_FREQ_PWM_MULT);
    /* 设置输出PWM的极性 */
    Hv_Cal_MPRT_PwmSetPol(ucPort, HV_PROJECT_CONFIG_MPRT_HIGHT_POL);

    /*打开高频PWM开关，输出PWM*/
    Hv_Cal_MPRT_PwmEnable(HV_PROJECT_CONFIG_MPRT_HIGHT_PORT, 1);
    return;
}

static USHORT16 LimitPwmValue(USHORT16 usValue)
{
    USHORT16 usDutyProtectMin = HV_PROJECT_CONFIG_MPRT_HIGHT_FREQ_PWM_TOTAL / 100 * 5;
    USHORT16 usDutyProtectMax = HV_PROJECT_CONFIG_MPRT_HIGHT_FREQ_PWM_TOTAL - usDutyProtectMin;
    if (usValue < usDutyProtectMin)
    {
        return usDutyProtectMin;
    } 
    else if (usValue > usDutyProtectMax)
    {
        return usDutyProtectMax;
    } 
    else
    {
        return usValue;
    }
}
/*全局PWM占空比，uiVal越大占空比越高*/
VOID Hv_Drv_MPRT_SetOutHighFreqPwmDuty(UCHAR8 ucPort, USHORT16 usSta, USHORT16 usVal)
{
    HV_LOG_DEBUG(VIDEO, "ucPort %d usSta %u usVal %u", ucPort, usSta, usVal);
    Hv_Cal_MPRT_PwmSetStart(ucPort, (usSta));
    Hv_Cal_MPRT_PwmSetValue(ucPort, (usVal));
    return;
}

/*MPRT和VRR下的MPRT开关*/
VOID Hv_Drv_MPRT_SetOutMprtPwmEnable(MprtType enMprtType)
{
    /* mode可以是0：拉高长度可配，1：拉高比例可配，推荐0, 关：输出长高电平 */
    if (enMprtType == MPRT_PWM_VRR_ENABLE)
    {
        Hv_Cal_MPRT_SetMprtStaVrrEn(1 << HV_PROJECT_CONFIG_MPRT_PORT);
        /* start 的值需要根据实际帧率调一个固定值，mprt的亮度也可以通过start的值来调节 */
        Hv_Cal_MPRT_PwmSetStart(HV_PROJECT_CONFIG_MPRT_PORT, HV_PROJECT_CONFIG_MPRT_VRR_STA_VALUE);
        /* mprt_pwm_val的值需要根据实际帧率调一个固定值，mprt的亮度也可以通过val的值来调节,推荐设大到跨帧 */
        Hv_Cal_MPRT_PwmSetValue(HV_PROJECT_CONFIG_MPRT_PORT, HV_PROJECT_CONFIG_MPRT_VRR_VALUE);
        Hv_Cal_MPRT_PwmSetMode(HV_PROJECT_CONFIG_MPRT_PORT, HV_PROJECT_CONFIG_MPRT_MODE);
    }
    else if (enMprtType == MPRT_PWM_ENABLE)
    {
        Hv_Cal_MPRT_SetMprtStaVrrEn(0);
        Hv_Cal_MPRT_PwmSetStart(HV_PROJECT_CONFIG_MPRT_PORT, HV_PROJECT_CONFIG_MPRT_STA_VALUE);
        Hv_Cal_MPRT_PwmSetValue(HV_PROJECT_CONFIG_MPRT_PORT, HV_PROJECT_CONFIG_MPRT_VALUE);
        Hv_Cal_MPRT_PwmSetMode(HV_PROJECT_CONFIG_MPRT_PORT, HV_PROJECT_CONFIG_MPRT_MODE);
    }
    else
    {
        Hv_Cal_MPRT_SetMprtStaVrrEn(0);
        /* MPRT关时，使用MODE2默认输出长高 */
        Hv_Cal_MPRT_PwmSetMode(HV_PROJECT_CONFIG_MPRT_PORT, 2);
    }
    return;
}

/*MPRT初始化配置*/
VOID Hv_Drv_MPRT_SetOutMprtPwm(UCHAR8 ucPort)
{
    MprtType enMprtType = MPRT_PWM_CLOSE;
#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_MPRT) 
    enMprtType = HV_COMMON_DATABASE_GET(UserData, ucMprt);
#endif
    /* vsync到来时输出的pwm强制拉低*/
    if (HV_PROJECT_CONFIG_MPRT_VS_CLEAR == 1)
    {
        Hv_Cal_MPRT_SetMprtVsClear(1 << ucPort);
    }
    Hv_Drv_MPRT_SetOutMprtPwmEnable(enMprtType);
    /* 设置输出PWM的极性 */
    Hv_Cal_MPRT_PwmSetPol(ucPort, HV_PROJECT_CONFIG_MPRT_POL);
    return;
}

VOID Hv_Drv_MPRT_Init(VOID)
{
    /* 设置MPRT的VS source来源 */
    Hv_Cal_MPRT_SetMprtSource(4);
    Hv_Cal_MPRT_Init();

#if (HV_PROJECT_CONFIG_FULL_LOCALDIMMING == HV_CONFIG_ON)
    Hv_Cal_MPRT_SetLdcVsync(HV_PROJECT_CONFIG_MPRT_LDCVSYNC_PORT);
#else
    /* 初始化全局背光高频PWM */
    Hv_Drv_MPRT_SetOutHighFreqPwm(HV_PROJECT_CONFIG_MPRT_HIGHT_PORT);

    /* 初始化MPRT的PWM */
    Hv_Drv_MPRT_SetOutMprtPwm(HV_PROJECT_CONFIG_MPRT_PORT);
    Hv_Cal_MPRT_PwmEnable(HV_PROJECT_CONFIG_MPRT_PORT, 1);
#endif
#ifdef HV_BOARD_CONFIG_LED_B_PWM
    Hv_Cal_MPRT_PwmEnable(HV_BOARD_CONFIG_LED_B_PWM, 1);
    Hv_Cal_MPRT_PwmSetMode(HV_BOARD_CONFIG_LED_B_PWM, 3);
    #if (HV_BOARD_CONFIG_LED_B_POL == 0)
    Hv_Cal_MPRT_PwmSetStart(HV_BOARD_CONFIG_LED_B_PWM, 7656);
    Hv_Cal_MPRT_PwmSetValue(HV_BOARD_CONFIG_LED_B_PWM, 0);
    #else
    Hv_Cal_MPRT_PwmSetStart(HV_BOARD_CONFIG_LED_B_PWM, 0);
    Hv_Cal_MPRT_PwmSetValue(HV_BOARD_CONFIG_LED_B_PWM, 7656);
    #endif
    Hv_Cal_MPRT_PwmSetMult(HV_BOARD_CONFIG_LED_B_PWM, 7);
#endif
    return;
}

VOID Hv_Drv_MPRT_PwmEnable(UCHAR8 ucPort, BOOL bEn)
{
    Hv_Cal_MPRT_PwmEnable(ucPort, bEn);
    return;
}

VOID Hv_Drv_MPRT_PwmMode(USHORT16 usPwmPort, USHORT16 usMode)
{
    Hv_Cal_MPRT_PwmSetMode(usPwmPort, usMode);
    return;
}

