/*
 *
 * Function Name:  cslDsiAfeLdoSetState
 *
 * Description:    AFE LDO Control
 *
 */
static void cslDsiAfeLdoSetState(DSI_HANDLE dsiH, DSI_LDO_STATE_t state)
{
#define DSI_LDO_HP_EN	  0x00000001
#define DSI_LDO_LP_EN	  0x00000002
#define DSI_LDO_CNTL_ENA  0x00000004
#define DSI_LDO_ISO_OUT	  0x00800000

	unsigned long ldo_val = 0;

	switch (state) {
	case DSI_LDO_HP:
		ldo_val = DSI_LDO_CNTL_ENA | DSI_LDO_HP_EN;
		break;
	case DSI_LDO_LP:
		ldo_val = DSI_LDO_CNTL_ENA | DSI_LDO_LP_EN;
		break;
	case DSI_LDO_OFF:
		ldo_val = DSI_LDO_CNTL_ENA | DSI_LDO_ISO_OUT;
		break;
	default:
		ldo_val = DSI_LDO_CNTL_ENA | DSI_LDO_HP_EN;
		LCD_DBG(LCD_DBG_ERR_ID, "[CSL DSI] %s: "
			"ERROR Invalid LDO State[%d] !\r\n", __func__,
			state);
		break;
	}

	if (dsiH->bus == 0)
		HW_REG_WRITE(0x3C004030, ldo_val);
	else
		HW_REG_WRITE(0x3C004034, ldo_val);

	OSTASK_Sleep(TICKS_IN_MILLISECONDS(1));
}
/*
 *
 * Function Name:  cslDsi1UpdateTask
 *
 * Description:    DSI Controller 0 Update Task
 *
 */
static void cslDsi1UpdateTask(void)
{
	DSI_UPD_REQ_MSG_T updMsg;
	OSStatus_t osStat;
	CSL_LCD_RES_T res;
	DSI_HANDLE dsiH = &dsiBus[1];

	for (;;) {
		res = CSL_LCD_OK;

		/* Wait for update request */
		OSQUEUE_Pend(dsiH->updReqQ, (QMsg_t *)&updMsg, TICKS_FOREVER);

		/* Wait For signal from eof DMA */
		osStat = OSSEMAPHORE_Obtain(dsiH->semaDma,
					    TICKS_IN_MILLISECONDS(updMsg.updReq.
								  timeOut_ms));

		if (osStat != OSSTATUS_SUCCESS) {
			if (osStat == OSSTATUS_TIMEOUT) {
				LCD_DBG(LCD_DBG_ERR_ID, "[CSL DSI] %s: "
					"TIMED OUT While waiting for "
					"EOF DMA\n", __func__);
				res = CSL_LCD_OS_TOUT;
			} else {
				LCD_DBG(LCD_DBG_ERR_ID, "[CSL DSI] %s: "
					"OS ERR While waiting for EOF DMA\n",
					__func__);
				res = CSL_LCD_OS_ERR;
			}

			cslDsiDmaStop(&updMsg);
		}

		if (res == CSL_LCD_OK)
			res = cslDsiWaitForInt(dsiH, 100);
		else
			cslDsiWaitForInt(dsiH, 1);

		chal_dsi_de1_enable(dsiH->chalH, FALSE);
		chal_dsi_tx_start(dsiH->chalH, TX_PKT_ENG_1, FALSE);
		chal_dsi_tx_start(dsiH->chalH, TX_PKT_ENG_2, FALSE);

		if (!updMsg.clientH->hasLock)
			OSSEMAPHORE_Release(dsiH->semaDsi);

		if (updMsg.updReq.cslLcdCb) {
			updMsg.updReq.cslLcdCb(res, &updMsg.updReq.cslLcdCbRec);
		} else {
			LCD_DBG(LCD_DBG_ERR_ID, "[CSL DSI] %s: "
				"Callback EQ NULL, Skipping\n", __func__);
		}
	}
}
/*
 *
 * Function Name:  cslDsiBtaRecover
 *
 * Description:    Recover From Failed BTA
 *
 */
static CSL_LCD_RES_T cslDsiBtaRecover(DSI_HANDLE dsiH)
{
	CSL_LCD_RES_T res = CSL_LCD_OK;

	chal_dsi_phy_state(dsiH->chalH, PHY_TXSTOP);

	OSTASK_Sleep(TICKS_IN_MILLISECONDS(1));

	chal_dsi_clr_fifo(dsiH->chalH,
			  CHAL_DSI_CTRL_CLR_CMD_DATA_FIFO |
			  CHAL_DSI_CTRL_CLR_LANED_FIFO);

	chal_dsi_phy_state(dsiH->chalH, PHY_CORE);
	return res;
}
//
// Description:     
//                   
//*****************************************************************************
static void DISPDRV_ExecCmndList( 
    DISPDRV_HANDLE_T     drvH, 
    pDISPCTRL_REC_T      cmnd_lst,
    UInt8 packet_type  )
{

    #define TX_MSG_MAX	128
    UInt32  i = 0;

    UInt8   tx_buff[TX_MSG_MAX];
    UInt32  tx_size = 0; 

    while (cmnd_lst[i].type != DISPCTRL_LIST_END)
    {
        if (cmnd_lst[i].type == DISPCTRL_WR_CMND)
        {
			
        	tx_buff[tx_size++] = cmnd_lst[i].cmnd;

        	while( cmnd_lst[i+1].type == DISPCTRL_WR_DATA )
        	{
        		tx_buff[tx_size++] = cmnd_lst[i+1].data;	
                i++;   	    
        	}
        	DISPDRV_WrSendCmnd (drvH, tx_buff, tx_size, packet_type );			
        	tx_size = 0;
			
        }
        else if ( cmnd_lst[i].type == DISPCTRL_WR_CMND_DATA )
        {	
        	tx_buff[tx_size++] = cmnd_lst[i].cmnd;
        	tx_buff[tx_size++] = cmnd_lst[i].data;
        	DISPDRV_WrSendCmnd (drvH, tx_buff, tx_size, packet_type );
        	tx_size = 0;
        
        }
        else if (cmnd_lst[i].type == DISPCTRL_SLEEP_MS)
        {
            OSTASK_Sleep ( TICKS_IN_MILLISECONDS(cmnd_lst[i].data) );
        }
        i++;
    }
} // DISPDRV_ExecCmndList
//*****************************************************************************
//
// Function Name:   bcm92416_hvga_ExecCmndList
//
// Description:     
//                   
//*****************************************************************************
static void DISPDRV_ExecCmndList(DISPDRV_HANDLE_T drvH,
				 pNEW_DISPCTRL_REC_T cmnd_lst)
{
	UInt32 i = 0;

	while (cmnd_lst[i].type != DISPCTRL_LIST_END) {
		if (cmnd_lst[i].type == DISPCTRL_WR_CMND_DATA) {

			DISPDRV_WrCmndPn(drvH, cmnd_lst[i].number,
					 cmnd_lst[i].data);
		} else if (cmnd_lst[i].type == DISPCTRL_WR_CMND) {
			DISPDRV_WrCmndP0(drvH, cmnd_lst[i].data[0]);
		} else if (cmnd_lst[i].type == DISPCTRL_SLEEP_MS) {
			OSTASK_Sleep(TICKS_IN_MILLISECONDS
				     (cmnd_lst[i].data[0]));
		}

		i++;
	}
}				// bcm92416_hvga_ExecCmndList
void exec_cmnd_list( 
    DISPDRV_HANDLE_T     dispH,
    Boolean              useOs,  
    pDISPCTRL_REC_T      cmnd_lst 
    )
{
    UInt32  i = 0;

    while (cmnd_lst[i].type != DISPCTRL_LIST_END)
    {

		if (cmnd_lst[i].type == DISPCTRL_WR_CMND_MULTIPLE_DATA)
        {
        	dsi_wrcmndPN (dispH, cmnd_lst[i].cmnd, 
                cmnd_lst[i].datasize, cmnd_lst[i].dataptr);
        }
		else if (cmnd_lst[i].type == DISPCTRL_WR_CMND_DATA)
        {
            dsi_wrcmndP1 (dispH, cmnd_lst[i].cmnd, 
                cmnd_lst[i].data);
        }
        else if (cmnd_lst[i].type == DISPCTRL_WR_CMND)
        {
            dsi_wrcmndP0 (dispH, cmnd_lst[i].cmnd);
        }
        else if (cmnd_lst[i].type == DISPCTRL_SLEEP_MS)
        {
            if ( useOs )
            {
                OSTASK_Sleep ( TICKS_IN_MILLISECONDS(cmnd_lst[i].data) );
            }    
            else
            {
				mdelay( cmnd_lst[i].data );
            }    
        }
        i++;
    }
} // execCmndList
/*
 *
 * Function Name:  cslDsiWaitForInt
 *
 * Description:
 *
 */
static CSL_LCD_RES_T cslDsiWaitForInt(DSI_HANDLE dsiH, UInt32 tout_msec)
{
	OSStatus_t osRes;
	CSL_LCD_RES_T res = CSL_LCD_OK;

	osRes =
	    OSSEMAPHORE_Obtain(dsiH->semaInt, TICKS_IN_MILLISECONDS(tout_msec));

	if (osRes != OSSTATUS_SUCCESS) {
		cslDsiDisInt(dsiH);

		if (osRes == OSSTATUS_TIMEOUT) {
			LCD_DBG(LCD_DBG_ERR_ID, "[CSL DSI] %s: "
				"ERR Timed Out!\n", __func__);
			res = CSL_LCD_OS_TOUT;
		} else {
			LCD_DBG(LCD_DBG_ERR_ID, "[CSL DSI] %s: "
				"ERR OS Err...!\n", __func__);
			res = CSL_LCD_OS_ERR;
		}
	}
	return res;
}