/*  
 * keapiSMBusInit.c - KEAPI SMBus support for nanoETXsp board
 */
/*
 * Copyright 2011 Kontron KGSC.
 */
/*
 modification history
 --------------------
 01b,11may11,ivo update for nanoETXsp board
 01a,22apr11,ivo initial version
 */
/*
 DESCRIPTION:
 KEAPI SMBus initialization for nanoETXsp board
 KEAPI v1.2.0.3 implementation. 
 */
#include <vxWorks.h>
#include <config.h>

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

#define NANO_ETX_SP_NUM_SMBUS (1)

struct smbusDriverControl *keapiSMBusFunc[NANO_ETX_SP_NUM_SMBUS];
int keapiSmbusCount = 0;

/**
 * keapiSMBusInitialize - Initialization access to BSP SMBus device(s).
 * 
 * RETURNS:          
 * - KEAPI_RET_SUCCESS on success
 * - KEAPI_RET_ERROR
 * 
 * ERRNO
 * 
 */
STATUS keapiSMBusInitialize(void)
    {
    VXB_DEVICE_ID pDev;
    FUNCPTR pFunc;
    struct vxbDriverControl drvCtrl;
    
    /* finding the SMBus driver instance */
    pDev = vxbInstByNameFind(VXB_SCH_SMBUS_DRV_NAME, 0);
    if (pDev == NULL)
        {
        KEAPI_DBG("Cannot find SMBus instance '%s' \n",
                (int) VXB_SCH_SMBUS_DRV_NAME, 0, 0, 0, 0, 0);
        return ERROR;
        }

    /* getting the driver methods */
    pFunc = vxbDevMethodGet(pDev, DEVMETHOD_CALL(driverControl));
    if (pFunc == NULL)
        {
        KEAPI_DBG(
                "Failed to get reference to 'driverControl' driver method\n",
                0, 0, 0, 0, 0, 0);
        return ERROR;
        }

    pFunc(pDev, &drvCtrl);

    if (drvCtrl.drvCtrlInfo == NULL)        
        {
        KEAPI_DBG("Failed to get vxbSMBus functionality\n", 0, 0, 0, 0, 0, 0);
        return ERROR;
        }
    keapiSMBusFunc[0] = drvCtrl.drvCtrlInfo;
            
    keapiSmbusCount = NANO_ETX_SP_NUM_SMBUS;

    return OK;
    }

KEAPI_RETVAL keapiGetSmbusCount(KEAPI_PUCHAR pSmbusCount)
    {
    *pSmbusCount = keapiSmbusCount;
    if (keapiSmbusCount == 0)
        {
        KEAPI_DBG("keapiSMBus was not initialized properly\n", 0, 0, 0, 0, 0, 0);
        return KEAPI_RET_ERROR;
        }
    return KEAPI_RET_SUCCESS;
    }

