//------------------------------------------------------------------------------
//
//  Copyright (C) 2004, Motorola Inc. All Rights Reserved
//
//------------------------------------------------------------------------------
//
// Copyright (C) 2004-2007, Freescale Semiconductor, Inc. All Rights Reserved.
// THIS SOURCE CODE, AND ITS USE AND DISTRIBUTION, IS SUBJECT TO THE TERMS
// AND CONDITIONS OF THE APPLICABLE LICENSE AGREEMENT
//
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//
//  File:  bsptcled.cpp
//
//  Provides the implementation for BSP-specific tcled support.
//
//------------------------------------------------------------------------------

#pragma warning(push)
#pragma warning(disable: 4201)
#include <windows.h>
#pragma warning(pop)

#include "pmic_tcled.h"
#include "pmic_bklight.h"
#include "bsptcled.h"

//-----------------------------------------------------------------------------
// Defines

#define BSP_TCLED_DEBUG     FALSE

//-----------------------------------------------------------------------------
// Types


//-----------------------------------------------------------------------------
// External Functions

//-----------------------------------------------------------------------------
// External Variables


//-----------------------------------------------------------------------------
// Global Variables


//-----------------------------------------------------------------------------
// Local Variables

//-----------------------------------------------------------------------------
// Local Functions

//-----------------------------------------------------------------------------
//
// Function: BSPTcledInit
//
// This function initializes the tcled PMIC
//
// Parameters:
//      none
//
// Returns:
//      TRUE means success
//      FALSE means fail
//
//-----------------------------------------------------------------------------
BOOL BSPTcledInit(void)
{ 
    BOOL rc = TRUE;
    DEBUGMSG(BSP_TCLED_DEBUG, (TEXT("+BSPTcledInit \r\n")));
  
    DEBUGMSG(BSP_TCLED_DEBUG, (TEXT("-BSPTcledInit \r\n")));
    return rc;
}


//-----------------------------------------------------------------------------
//
// Function: BSPTcledDeinit
//
// This function deinitializes the tcled PMIC
//
// Parameters:
//      none
//
// Returns:
//      TRUE means success
//      FALSE means fail
//
//-----------------------------------------------------------------------------
BOOL BSPTcledDeinit(void)
{
    BOOL rc = TRUE;
    DEBUGMSG(BSP_TCLED_DEBUG, (TEXT("+BSPTcledDeinit \r\n")));
        
    DEBUGMSG(BSP_TCLED_DEBUG, (TEXT("-BSPTcledDeinit \r\n")));
    return rc;   
}


//-----------------------------------------------------------------------------
//
// Function: BSPTcledIOControl
//
// This function handles the IOCotrol to the tcled pmic
//
// Parameters:
//      dwContext
//          [in] Context of call
//      Ioctl
//          [in] the io control code passed from upper layer
//      pInBuf
//          [in] IN buffer
//      InBufLen
//          [in] size of IN buffer
//      pOutBuf
//          [out] OUT buffer
//      OutBufLen
//          [out] size of OUT buffer
//      pdwBytesTransferred
//          [out] returned bytes
//
// Returns:
//      TRUE means success
//      FALSE means fail
//
//-----------------------------------------------------------------------------
BOOL BSPTcledIOControl(
    DWORD  dwContext,
    DWORD  Ioctl,
    PUCHAR pInBuf,
    DWORD  InBufLen, 
    PUCHAR pOutBuf,
    DWORD  OutBufLen,
    PDWORD pdwBytesTransferred)
{
    BOOL rc = TRUE;
    PFUN_CURRENT_PKT pFunCurrentPkt = NULL;
    PFUN_CYCLETIME_PKT pFunCycleTimePkt = NULL;
    PFUN_DUTYCYCLE_PKT pFunDutyCyclePkt = NULL;
    PIND_CURRENT_PKT pIndCurrentPkt = NULL;
    PIND_BLINK_PKT pIndBlinkPkt = NULL;
    PFUN_PATTERN_PKT pFunPatternPkt = NULL;
    PFUN_STROBE_PKT pFunStrobePkt = NULL;
    PFUN_SPEED_PKT pFunSpeedPkt = NULL;

    // Remove-W4: Warning C4100 workaround    
    UNREFERENCED_PARAMETER(dwContext);
    UNREFERENCED_PARAMETER(InBufLen);
    UNREFERENCED_PARAMETER(OutBufLen);
    UNREFERENCED_PARAMETER(pdwBytesTransferred);
    
    DEBUGMSG(BSP_TCLED_DEBUG, (TEXT("+BSPTcledIOControl \r\n")));
    switch(Ioctl)
    {
       case TCLED_IOCTL_ENABLE:
            if (pInBuf == NULL)
                rc = FALSE;
            if(rc == TRUE)
            {
                if(PmicBacklightMasterEnable() != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicBacklightMasterEnable failed\r\n")));
                    rc = FALSE;
                }
                if(PmicTCLEDEnable((TCLED_MODE)*pInBuf, TCLED_FUN_BANK1) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDEnable failed\r\n")));
                    rc = FALSE;
                }
            }
            break;
        case TCLED_IOCTL_DISABLE:
            if(PmicBacklightMasterDisable() != PMIC_SUCCESS)
            {
                ERRORMSG(TRUE, (TEXT("pfnPmicBacklightMasterDisable failed\r\n")));
                rc = FALSE;
            }
            if(PmicTCLEDDisable(TCLED_FUN_BANK1) != PMIC_SUCCESS)
            {
                ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDDisable failed\r\n")));
                rc = FALSE;
            }
            break;
        
        case TCLED_IOCTL_GETMODE:
            if (pOutBuf == NULL)
                rc = FALSE;

            if (rc == TRUE)
            {
                if(PmicTCLEDGetMode((TCLED_MODE*)pOutBuf, TCLED_FUN_BANK1) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDGetMode failed\r\n")));
                    rc = FALSE;
                }
            }
            break;
        case TCLED_IOCTL_IND_SETCURRENTLEVEL:
            if (pInBuf != NULL)
                pIndCurrentPkt = (PIND_CURRENT_PKT)pInBuf;
            else
                rc = FALSE;

            if (rc == TRUE)
            {
                if(PmicTCLEDIndicatorSetCurrentLevel(pIndCurrentPkt->channel, pIndCurrentPkt->level) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDIndicatorSetCurrentLevel failed\r\n")));
                    rc = FALSE;
                }
            }
            break;
        case TCLED_IOCTL_IND_GETCURRENTLEVEL:
            if (pOutBuf != NULL)
                pIndCurrentPkt = (PIND_CURRENT_PKT)pOutBuf;
            else
                rc = FALSE;

            if (rc == TRUE)
            {
                if(PmicTCLEDIndicatorGetCurrentLevel(pIndCurrentPkt->channel, &pIndCurrentPkt->level) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDIndicatorGetCurrentLevel failed\r\n")));
                    rc = FALSE;
                }
            }
            break;
        case TCLED_IOCTL_IND_SETBLINKPATTERN:
            if (pInBuf != NULL)
                pIndBlinkPkt = (PIND_BLINK_PKT)pInBuf;
            else
                rc = FALSE;

            if (rc == TRUE)
            {
                if(PmicTCLEDIndicatorSetBlinkPattern(pIndBlinkPkt->channel, pIndBlinkPkt->mode, pIndBlinkPkt->skip) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDIndicatorSetBlinkPattern failed\r\n")));
                    rc = FALSE;
                }
            }
            break;
        case TCLED_IOCTL_IND_GETBLINKPATTERN:
            if (pOutBuf != NULL)
                pIndBlinkPkt = (PIND_BLINK_PKT)pOutBuf;
            else
                rc = FALSE;

            if (rc == TRUE)
            {
                if(PmicTCLEDIndicatorGetBlinkPattern(pIndBlinkPkt->channel, &pIndBlinkPkt->mode, &pIndBlinkPkt->skip) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDIndicatorGetBlinkPattern failed\r\n")));
                    rc = FALSE;
                }          
            }
            break;
        case TCLED_IOCTL_FUN_SETCURRENTLEVEL:
            if (pInBuf != NULL)
                pFunCurrentPkt = (PFUN_CURRENT_PKT)pInBuf;
            else
                rc = FALSE;

            if (rc == TRUE)
            {
                if(PmicTCLEDFunLightSetCurrentLevel(pFunCurrentPkt->bank, pFunCurrentPkt->channel, pFunCurrentPkt->level) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDFunLightSetCurrentLevel failed\r\n")));
                    rc = FALSE;
                }
            }
            break;
        case TCLED_IOCTL_FUN_GETCURRENTLEVEL:
            if (pOutBuf != NULL)
                pFunCurrentPkt = (PFUN_CURRENT_PKT)pOutBuf;
            else
                rc = FALSE;

            if (rc == TRUE)
            {
                if(PmicTCLEDFunLightGetCurrentLevel(pFunCurrentPkt->bank, pFunCurrentPkt->channel, &pFunCurrentPkt->level) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDFunLightGetCurrentLevel failed\r\n")));
                    rc = FALSE;
                }
            }
            break;
        case TCLED_IOCTL_FUN_SETCYCLETIME:
            if (pInBuf != NULL)
                pFunCycleTimePkt = (PFUN_CYCLETIME_PKT)pInBuf;
            else
                rc = FALSE;

            if (rc == TRUE)
            {
                if(PmicTCLEDFunLightSetCycleTime(pFunCycleTimePkt->bank, pFunCycleTimePkt->ct) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDFunLightSetCycleTime failed\r\n")));
                    rc = FALSE;
                }
            }
            break;
        case TCLED_IOCTL_FUN_GETCYCLETIME:
            if (pOutBuf != NULL)
                pFunCycleTimePkt = (PFUN_CYCLETIME_PKT)pOutBuf;
            else
                rc = FALSE;

            if (rc == TRUE)
            {
                if(PmicTCLEDFunLightGetCycleTime(pFunCycleTimePkt->bank, &pFunCycleTimePkt->ct) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDFunLightGetCycleTime failed\r\n")));
                    rc = FALSE;
                }
            }
            break;
        case TCLED_IOCTL_FUN_SETDUTYCYCLE:
            if (pInBuf != NULL)
                pFunDutyCyclePkt = (PFUN_DUTYCYCLE_PKT)pInBuf;
            else
                rc = FALSE;

            if (rc == TRUE)
            {
                if(PmicTCLEDFunLightSetDutyCycle(pFunDutyCyclePkt->bank, pFunDutyCyclePkt->channel, pFunDutyCyclePkt->dc) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDFunLightSetDutyCycle failed\r\n")));
                    rc = FALSE;
                }
            }
            break;
        case TCLED_IOCTL_FUN_GETDUTYCYCLE:
            if (pOutBuf != NULL)
                pFunDutyCyclePkt = (PFUN_DUTYCYCLE_PKT)pOutBuf;
            else
                rc = FALSE;

            if (rc == TRUE)
            {
                if(PmicTCLEDFunLightGetDutyCycle(pFunDutyCyclePkt->bank, pFunDutyCyclePkt->channel, &pFunDutyCyclePkt->dc) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDFunLightGetDutyCycle failed\r\n")));
                    rc = FALSE;
                }
            }
            break;
        case TCLED_IOCTL_FUN_SETBLENDEDRAMPS:
            if (pInBuf != NULL)
                pFunPatternPkt = (PFUN_PATTERN_PKT)pInBuf;
            else
                rc = FALSE;

            if (rc == TRUE)
            {
                if(PmicTCLEDFunLightBlendedRamps(pFunPatternPkt->bank, TC_OFF) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDFunLightBlendedRamps failed\r\n")));
                    rc = FALSE;
                }
                if(PmicTCLEDFunLightBlendedRamps(pFunPatternPkt->bank, pFunPatternPkt->speed) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDFunLightBlendedRamps failed\r\n")));
                    rc = FALSE;
                }
            }
            break;
        case TCLED_IOCTL_FUN_SETSAWRAMPS:
            if (pInBuf != NULL)
                pFunPatternPkt = (PFUN_PATTERN_PKT)pInBuf;
            else
                rc = FALSE;

            if (rc == TRUE)
            {
                if(PmicTCLEDFunLightSawRamps(pFunPatternPkt->bank, TC_OFF) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDFunLightSawRamps failed\r\n")));
                    rc = FALSE;
                }
                if(PmicTCLEDFunLightSawRamps(pFunPatternPkt->bank, pFunPatternPkt->speed) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDFunLightSawRamps failed\r\n")));
                    rc = FALSE;
                }
            }
            break;
        case TCLED_IOCTL_FUN_SETBLENDEDBOWTIE:
            if (pInBuf != NULL)
                pFunPatternPkt = (PFUN_PATTERN_PKT)pInBuf;
            else
                rc = FALSE;

            if (rc == TRUE)
            {
                if(PmicTCLEDFunLightBlendedBowtie(pFunPatternPkt->bank, TC_OFF) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDFunLightBlendedBowtie failed\r\n")));
                    rc = FALSE;
                }
                if(PmicTCLEDFunLightBlendedBowtie(pFunPatternPkt->bank, pFunPatternPkt->speed) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDFunLightBlendedBowtie failed\r\n")));
                    rc = FALSE;
                }
            }
            break;
        case TCLED_IOCTL_FUN_STROBE:
            if (pInBuf != NULL)
                pFunStrobePkt = (PFUN_STROBE_PKT)pInBuf;
            else
                rc = FALSE;

            if (rc == TRUE)
            {
                if(PmicTCLEDFunLightStrobe(pFunStrobePkt->channel, TC_STROBE_OFF) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDFunLightStrobe failed\r\n")));
                    rc = FALSE;
                }
                if(PmicTCLEDFunLightStrobe(pFunStrobePkt->channel, pFunStrobePkt->speed) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDFunLightStrobe failed\r\n")));
                    rc = FALSE;
                }
                
            }
            break;
        case TCLED_IOCTL_FUN_RAMPUP:
            if (pInBuf != NULL)
                pFunSpeedPkt = (PFUN_SPEED_PKT )pInBuf;
            else
                rc = FALSE;

            if (rc == TRUE)
            {
                if(PmicTCLEDFunLightRampOff(pFunSpeedPkt->bank, pFunSpeedPkt->channel) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDFunLightRampOff failed\r\n")));
                    rc = FALSE;
                }
                if(PmicTCLEDFunLightRampUp(pFunSpeedPkt->bank, pFunSpeedPkt->channel) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDFunLightRampUp failed\r\n")));
                    rc = FALSE;
                }
            }
            break;
        case TCLED_IOCTL_FUN_RAMPDOWN:
            if (pInBuf != NULL)
                pFunSpeedPkt = (PFUN_SPEED_PKT )pInBuf;
            else
                rc = FALSE;

            if (rc == TRUE)
            {
                if(PmicTCLEDFunLightRampOff(pFunSpeedPkt->bank, pFunSpeedPkt->channel) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDFunLightRampOff failed\r\n")));
                    rc = FALSE;
                }
                if(PmicTCLEDFunLightRampDown(pFunSpeedPkt->bank, pFunSpeedPkt->channel) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDFunLightRampDown failed\r\n")));
                    rc = FALSE;
                }
            }
            break;
        case TCLED_IOCTL_FUN_RAMPOFF:
            if (pInBuf != NULL)
                pFunSpeedPkt = (PFUN_SPEED_PKT )pInBuf;
            else
                rc = FALSE;

            if (rc == TRUE)
            {
                if(PmicTCLEDFunLightRampOff(pFunSpeedPkt->bank, pFunSpeedPkt->channel) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDFunLightRampOff failed\r\n")));
                    rc = FALSE;
                }
            }
            break;
        case TCLED_IOCTL_FUN_TRIODEON:
            if (pInBuf != NULL)
                pFunSpeedPkt = (PFUN_SPEED_PKT )pInBuf;
            else
                rc = FALSE;

            if (rc == TRUE)
            {
                if(PmicTCLEDFunLightTriodeOn(pFunSpeedPkt->bank, pFunSpeedPkt->channel) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDFunLightTriodeOn failed\r\n")));
                    rc = FALSE;
                }
            }
            break;
        case TCLED_IOCTL_FUN_TRIODEOFF:
            if (pInBuf != NULL)
                pFunSpeedPkt = (PFUN_SPEED_PKT )pInBuf;
            else
                rc = FALSE;

            if (rc == TRUE)
            {
                if(PmicTCLEDFunLightTriodeOff(pFunSpeedPkt->bank, pFunSpeedPkt->channel) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDFunLightTriodeOff failed\r\n")));
                    rc = FALSE;
                }
            }
            break;
        case TCLED_IOCTL_FUN_CHASINGLIGHTSPATTERN:
            if (pInBuf != NULL)
                pFunPatternPkt = (PFUN_PATTERN_PKT )pInBuf;
            else
                rc = FALSE;

            if (rc == TRUE)
            {
                if(PmicTCLEDFunLightChasingLightsPattern(pFunPatternPkt->bank, pFunPatternPkt->pattern, TC_OFF) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDFunLightChasingLightsPattern failed\r\n")));
                    rc = FALSE;
                }
                if(PmicTCLEDFunLightChasingLightsPattern(pFunPatternPkt->bank, pFunPatternPkt->pattern, pFunPatternPkt->speed) != PMIC_SUCCESS)
                {
                    ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDFunLightChasingLightsPattern failed\r\n")));
                    rc = FALSE;
                }
            }
            break;
        case TCLED_IOCTL_FUN_ENABLEEDGESLOW:
            if(PmicTCLEDEnableEdgeSlow() != PMIC_SUCCESS)
            {
                ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDEnableEdgeSlow failed\r\n")));
                rc = FALSE;
            }
            break;
        case TCLED_IOCTL_FUN_DISABLEEDGESLOW:
            if(PmicTCLEDDisableEdgeSlow() != PMIC_SUCCESS)
            {
                ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDDisableEdgeSlow failed\r\n")));
                rc = FALSE;
            }
            break;
        case TCLED_IOCTL_FUN_ENABLEHALFCURRENT:
            if(PmicTCLEDEnableHalfCurrent() != PMIC_SUCCESS)
            {
                ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDEnableHalfCurrent failed\r\n")));
                rc = FALSE;
            }
            break;
        case TCLED_IOCTL_FUN_DISABLEHALFCURRENT:
            if(PmicTCLEDDisableHalfCurrent() != PMIC_SUCCESS)
            {
                ERRORMSG(TRUE, (TEXT("pfnPmicTCLEDDisableHalfCurrent failed\r\n")));
                rc = FALSE;
            }
            break;

    }

    DEBUGMSG(BSP_TCLED_DEBUG, (TEXT("-BSPTcledIOControl \r\n")));

    return rc;
}

