/**
 * \file    sys_qc_hook.c
 * \brief   QC system definitions and hooks implementation
 * 
 * \par     Copyright (c) 2019 UWE Technologies Corp., Ltd. All Rights Reserved.
 * \author  James
 * \date    2019-07-31
 * \version 
 * 
 * \history 
 *  Date        Note
 *  2019-07-31  Original Creation
 * 
 */

#include "libc.h"
#include "drivers.h"
#include "sys_pd_config.h"
#include "usbpd.h"
#include "sys_qc_hook.h"
#include "sys_pd_hook.h"
#include "sys_gpio.h"
#include "sys_defs.h"


#if SYS_QUICK_CHARGE_ENABLED

volatile DPM_QC_INFO_t g_usba1_qc_info;
volatile DPM_QC_INFO_t g_usba2_qc_info;

void sys_qc_init()
{
    memset((void *)&g_usba1_qc_info, 0, sizeof(DPM_QC_INFO_t));
    memset((void *)&g_usba2_qc_info, 0, sizeof(DPM_QC_INFO_t));

    g_usba1_qc_info.volt_get = sys_usba1_volt_get;
    g_usba1_qc_info.vbus_set = sys_usba_vbus_set;
    g_usba1_qc_info.apple_samsung_mode_enabled = FALSE;
    g_usba1_qc_info.src_dpdm_mode_set = sys_usba1_src_dpdm_mode_set;
    g_usba1_qc_info.state = DPM_QC_DISABLED;

    g_usba2_qc_info.volt_get = sys_usba2_volt_get;
    g_usba2_qc_info.vbus_set = sys_usba_vbus_set;
    g_usba2_qc_info.apple_samsung_mode_enabled = FALSE;
    g_usba2_qc_info.src_dpdm_mode_set = sys_usba2_src_dpdm_mode_set;
    g_usba2_qc_info.state = DPM_QC_DISABLED;
}

void sys_qc_disable()
{
    g_sys_pd_info.usba1_qc_en_flag = FALSE;
    g_sys_pd_info.usba2_qc_en_flag = FALSE;
    dpm_qc_enable_set(&g_usba1_qc_info, FALSE, FALSE);
    dpm_qc_enable_set(&g_usba2_qc_info, FALSE, FALSE);
}

u32_t sys_usba1_volt_get(DPM_QC_VOLT_INTF_TYPE_t type)
{
    switch (type)
    {
        case DPM_QC_VOLT_INTF_TYPE_DM:
            return drv_adc_read_multi(DRV_ADC_CHN_DM1, 3) * drv_flash_trim_ref_volt_get() / ADC_REF_DIVISOR;
        case DPM_QC_VOLT_INTF_TYPE_DP:
            return drv_adc_read_multi(DRV_ADC_CHN_DP1, 3) * drv_flash_trim_ref_volt_get() / ADC_REF_DIVISOR;
        case DPM_QC_VOLT_INTF_TYPE_VBUS:
            return sys_usba_vbus_get();
        default:
            break;
    }
    return 0;
}

u32_t sys_usba2_volt_get(DPM_QC_VOLT_INTF_TYPE_t type)
{
    switch (type)
    {
        case DPM_QC_VOLT_INTF_TYPE_DM:
            return drv_adc_read_multi(DRV_ADC_CHN_DM0, 3) * drv_flash_trim_ref_volt_get() / ADC_REF_DIVISOR;
        case DPM_QC_VOLT_INTF_TYPE_DP:
            return drv_adc_read_multi(DRV_ADC_CHN_DP0, 3) * drv_flash_trim_ref_volt_get() / ADC_REF_DIVISOR;
        case DPM_QC_VOLT_INTF_TYPE_VBUS:
            return sys_usba_vbus_get();
        default:
            break;
    }
    return 0;
}

void sys_usba1_src_dpdm_mode_set(DPM_SRC_DPDM_MODE_t mode)
{
    switch (mode)
    {
        case DPM_SRC_DPDM_MODE_QC3:
        case DPM_SRC_DPDM_MODE_QC2:
            drv_chip_dp1_dm1_short_set(FALSE);
            drv_chip_dm1_20kohm_pull_down_set(TRUE);
            drv_chip_dp1_20kohm_pull_down_set(FALSE);
            break;

        case DPM_SRC_DPDM_MODE_BC:
            drv_chip_dp1_dm1_short_set(TRUE);
            drv_chip_dm1_20kohm_pull_down_set(FALSE);
            drv_chip_dp1_20kohm_pull_down_set(FALSE);
            break;

        case DPM_SRC_DPDM_MODE_SAMSUNG:
            break;

        case DPM_SRC_DPDM_MODE_APPLE:
            break;

        case DPM_SRC_DPDM_MODE_OFF:
        default:
            drv_chip_dp1_dm1_short_set(FALSE);
            drv_chip_dm1_20kohm_pull_down_set(FALSE);
            drv_chip_dp1_20kohm_pull_down_set(FALSE);
            break;
    }
}

void sys_usba2_src_dpdm_mode_set(DPM_SRC_DPDM_MODE_t mode)
{
    switch (mode)
    {
        case DPM_SRC_DPDM_MODE_QC3:
        case DPM_SRC_DPDM_MODE_QC2:
            sys_gpio_uart0_enable_set(FALSE);
            drv_chip_dp0_dm0_short_set(FALSE);
            drv_chip_dm0_20kohm_pull_down_set(TRUE);
            drv_chip_dp0_20kohm_pull_down_set(FALSE);
            break;

        case DPM_SRC_DPDM_MODE_BC:
            sys_gpio_uart0_enable_set(FALSE);
            drv_chip_dp0_dm0_short_set(TRUE);
            drv_chip_dm0_20kohm_pull_down_set(FALSE);
            drv_chip_dp0_20kohm_pull_down_set(FALSE);
            break;

        case DPM_SRC_DPDM_MODE_SAMSUNG:
            sys_gpio_uart0_enable_set(FALSE);
            break;

        case DPM_SRC_DPDM_MODE_APPLE:
            sys_gpio_uart0_enable_set(FALSE);
            break;

        case DPM_SRC_DPDM_MODE_OFF:
        default:
            drv_chip_dp0_dm0_short_set(FALSE);
            drv_chip_dm0_20kohm_pull_down_set(FALSE);
            drv_chip_dp0_20kohm_pull_down_set(FALSE);
            sys_gpio_uart0_enable_set(TRUE);
            break;
    }
}

#endif /* SYS_QUICK_CHARGE_ENABLED */


