/*  
 * keapiSMBus.c - KEAPI SMBus support for the nanoETXsp BSP
 */
/*
 * Copyright 2011 Kontron KGSC.
 */
/*
 modification history
 --------------------
 01c,11may11,ivo update for nanoETXsp
 01b,21mar11,edv fix KEAPI_DEBUG usage
 01a,18mar11,edv initial version
 */
/*
 DESCRIPTION:
 KEAPI SMBus functionality implementation for nanoETXsp board
 KEAPI v1.2.0.3 implementation. 
 */
#include <vxWorks.h>
#include <config.h>

#include <vxBusLib.h>
#include <hwif/vxbus/vxBus.h>
#include <hwif/util/vxbParamSys.h>
#include <../3rdparty/kontron/h/keapiVxW.h>
#include <../3rdparty/kontron/h/vxbSMBus.h>

IMPORT int keapiSmbusCount; /* number of initialized SMBus instances */
IMPORT struct smbusDriverControl *keapiSMBusFunc[]; /* SMBus functions per instance */

#if (KEAPI_DEBUG > 0)
#       define CHECK_SMBUS_NR  \
        if (smbusNr >= keapiSmbusCount) \
            { \
            KEAPI_DBG("wrong smbusNr %d\n", smbusNr, 0, 0, 0, 0, 0); \
            return KEAPI_RET_ERROR; \
            }
#else
#       define CHECK_SMBUS_NR
#endif

KEAPI_RETVAL keapiSmbusQuickCommand(KEAPI_UCHAR smbusNr,
    KEAPI_UCHAR smbusAddress, KEAPI_UCHAR operation)
    {
    struct smbusDriverControl *pFunc = keapiSMBusFunc[smbusNr];
    CHECK_SMBUS_NR;
    if (pFunc->smbusQuickCommand(pFunc->pInst, smbusAddress,
            operation) == OK)
        {
        return KEAPI_RET_SUCCESS;
        }
    else
        {
        return KEAPI_RET_ERROR;
        }
    }

KEAPI_RETVAL keapiSmbusSendByte(KEAPI_UCHAR smbusNr, KEAPI_UCHAR smbusAddress,
    KEAPI_UCHAR byte)
    {
    struct smbusDriverControl *pFunc = keapiSMBusFunc[smbusNr];
    CHECK_SMBUS_NR;
    if (pFunc == NULL)
        {
        KEAPI_DBG("keapiSMBus was not initialized properly\n", 0, 0, 0, 0, 0, 0);
        return KEAPI_RET_ERROR;
        }
    if (pFunc->smbusSendByte(pFunc->pInst, smbusAddress, byte) == OK)
        {
        return KEAPI_RET_SUCCESS;
        }
    else
        {
        return KEAPI_RET_ERROR;
        }
    }

KEAPI_RETVAL keapiSmbusReceiveByte(KEAPI_UCHAR smbusNr,
    KEAPI_UCHAR smbusAddress, KEAPI_PUCHAR pByte)
    {
    struct smbusDriverControl *pFunc = keapiSMBusFunc[smbusNr];
    CHECK_SMBUS_NR;
    if (pFunc->smbusReceiveByte(pFunc->pInst, smbusAddress, pByte)
            == OK)
        {
        return KEAPI_RET_SUCCESS;
        }
    else
        {
        return KEAPI_RET_ERROR;
        }
    }

KEAPI_RETVAL keapiSmbusWriteByte(KEAPI_UCHAR smbusNr, KEAPI_UCHAR smbusAddress,
    KEAPI_UCHAR command, KEAPI_UCHAR byte)
    {
    struct smbusDriverControl *pFunc = keapiSMBusFunc[smbusNr];
    CHECK_SMBUS_NR;
    if (pFunc->smbusWriteByte(pFunc->pInst, smbusAddress, command,
            byte) == OK)
        {
        return KEAPI_RET_SUCCESS;
        }
    else
        {
        return KEAPI_RET_ERROR;
        }
    }

KEAPI_RETVAL keapiSmbusReadByte(KEAPI_UCHAR smbusNr, KEAPI_UCHAR smbusAddress,
    KEAPI_UCHAR command, KEAPI_PUCHAR pByte)
    {
    struct smbusDriverControl *pFunc = keapiSMBusFunc[smbusNr];
    CHECK_SMBUS_NR;
    if (pFunc->smbusReadByte(pFunc->pInst, smbusAddress, command,
            pByte) == OK)
        {
        return KEAPI_RET_SUCCESS;
        }
    else
        {
        return KEAPI_RET_ERROR;
        }
    }

KEAPI_RETVAL keapiSmbusWriteWord(KEAPI_UCHAR smbusNr, KEAPI_UCHAR smbusAddress,
    KEAPI_UCHAR command, KEAPI_UINT16 word)
    {
    struct smbusDriverControl *pFunc = keapiSMBusFunc[smbusNr];
    CHECK_SMBUS_NR;
    if (pFunc->smbusWriteWord(pFunc->pInst, smbusAddress, command,
            word) == OK)
        {
        return KEAPI_RET_SUCCESS;
        }
    else
        {
        return KEAPI_RET_ERROR;
        }
    }

KEAPI_RETVAL keapiSmbusReadWord(KEAPI_UCHAR smbusNr, KEAPI_UCHAR smbusAddress,
    KEAPI_UCHAR command, KEAPI_PUINT16 pWord)
    {
    struct smbusDriverControl *pFunc = keapiSMBusFunc[smbusNr];
    CHECK_SMBUS_NR;
    if (pFunc->smbusReadWord(pFunc->pInst, smbusAddress, command,
            pWord) == OK)
        {
        return KEAPI_RET_SUCCESS;
        }
    else
        {
        return KEAPI_RET_ERROR;
        }
    }


KEAPI_RETVAL keapiSmbusWriteBlock(KEAPI_UCHAR smbusNr,
    KEAPI_UCHAR smbusAddress, KEAPI_UCHAR command, KEAPI_PUCHAR pData,
    KEAPI_UCHAR dataLength)
    {
    struct smbusDriverControl *pFunc = keapiSMBusFunc[smbusNr];
    CHECK_SMBUS_NR;
    if (pFunc->smbusWriteBlock(pFunc->pInst, smbusAddress, command,
            pData, dataLength) == OK)
        {
        return KEAPI_RET_SUCCESS;
        }
    else
        {
        return KEAPI_RET_ERROR;
        }
    }


KEAPI_RETVAL keapiSmbusReadBlock(KEAPI_UCHAR smbusNr, KEAPI_UCHAR smbusAddress,
    KEAPI_UCHAR command, KEAPI_PUCHAR pData, KEAPI_PUCHAR pDataLength)
    {
    struct smbusDriverControl *pFunc = keapiSMBusFunc[smbusNr];
    CHECK_SMBUS_NR;
    if (pFunc == NULL)
        {
        KEAPI_DBG("keapiSMBus was not initialized properly\n", 0, 0, 0, 0, 0, 0);
        return KEAPI_RET_ERROR;
        }
    if (pFunc->smbusReadBlock(pFunc->pInst, smbusAddress, command,
            pData, pDataLength) == OK)
        {
        return KEAPI_RET_SUCCESS;
        }
    else
        {
        return KEAPI_RET_ERROR;
        }
    }
