 /*  keapiSensors.c - KEAPI BSP support for sensors at Kontron nanoETXsp board

  *
  *  KEAPI v1.2.0.3 implementation.
  *  
  *  Initialization.
  */

/*
 * Copyright 2011 Kontron KGSC.
 */

/*
 modification history
 --------------------
 01b,12may11,ivo update for nanoETXsp
 01a,07apr11,ivo initial version
*/

/*
DESCRIPTION:
    KEAPI BSP support for sensors at Kontron ktq45 flex board.
    KEAPI v1.2.0.3 implementation. Initialization.
*/

#include <vxWorks.h>
#include <config.h>

#include <string.h>
#include <math.h>

#include "keapi/keapiBSPCommon.h"

#define KEAPI_BSP_SENSORS
#include "keapi/keapiBSP.h"
#undef  KEAPI_BSP_SENSORS   

KEAPI_RETVAL keapiGetTempSensorCount (
        KEAPI_PINT32 pTempSensorCount
	)
    {
	if (!TEMP_SENSORS_INITED)
		{
		return KEAPI_RET_RETRIEVAL_ERROR;
		}
	
    *pTempSensorCount = TSENSORS_COUNT;
    return KEAPI_RET_SUCCESS;
    }

KEAPI_RETVAL keapiGetTempSensorList (
        PTEMP_SENSOR pTempSensors,
        KEAPI_INT32 tempSensorCount
	)
    {
        KEAPI_INT32 i;
        int retval;
        
        if ((UINT32)tempSensorCount > TSENSORS_COUNT)
            {
            return KEAPI_RET_PARAM_ERROR;
            }
        
        if (!TEMP_SENSORS_INITED)
        	{
			return KEAPI_RET_RETRIEVAL_ERROR;
        	}
        
        TEMP_SENSORS_UPDATE;
        
        for (i = 0; i < tempSensorCount; i++)
			{
        	SENSOR_TEMP_TYPE temp;
        	retval = GET_TEMP_SENSOR(i, &temp);
        	if (retval)
        		return KEAPI_RET_RETRIEVAL_ERROR;
        	
			strncpy(pTempSensors[i].name, tsensorNames[i], MAX_STR);
			/* Convert to millidegrees */
			pTempSensors[i].value = TEMP_SENSOR_TO_MILLIDEGREES(i, temp); 
            }
        
        return KEAPI_RET_SUCCESS;
    }

KEAPI_RETVAL keapiGetTempSensorValue (
        KEAPI_INT32 tempSensorNr,
        KEAPI_PINT32 pTempSensorValue
	) 
    {
	SENSOR_TEMP_TYPE temp;
	int retval;
	
    if ((UINT32)tempSensorNr >= TSENSORS_COUNT)
        {
        return KEAPI_RET_PARAM_ERROR;
        }
    
    if (!TEMP_SENSORS_INITED)
    	{
		return KEAPI_RET_RETRIEVAL_ERROR;
    	}
    
    TEMP_SENSORS_UPDATE;
    
    retval = GET_TEMP_SENSOR(tempSensorNr, &temp);
    if(retval)
    	return KEAPI_RET_RETRIEVAL_ERROR;
    
    *pTempSensorValue =  TEMP_SENSOR_TO_MILLIDEGREES(tempSensorNr, temp);
    return KEAPI_RET_SUCCESS;
    }

KEAPI_RETVAL keapiGetCpuTemp (
        KEAPI_INT32 cpuNr,
        KEAPI_PINT32 pCpuTemp
	)
    {
	SENSOR_TEMP_TYPE temp;
	int retval;
	
    if (cpuNr != 0)
        {
        return KEAPI_RET_PARAM_ERROR;
        }
    
    if (!TEMP_SENSORS_INITED)
    	{
		return KEAPI_RET_RETRIEVAL_ERROR;
    	}
    
    TEMP_SENSORS_UPDATE;
    
    retval = GET_TEMP_SENSOR(CPU_SENSOR, &temp);
    if(retval)
        	return KEAPI_RET_RETRIEVAL_ERROR;
    
    *pCpuTemp = TEMP_SENSOR_TO_MILLIDEGREES(CPU_SENSOR, temp);
    
    return KEAPI_RET_SUCCESS;
    }

KEAPI_RETVAL keapiGetChipsetTemp (
        KEAPI_PINT32 pChipsetTemp
	)
    {
	SENSOR_TEMP_TYPE temp; 
	int retval;
	
	if (!TEMP_SENSORS_INITED)
		{
		return KEAPI_RET_RETRIEVAL_ERROR;
		}
	
	
	TEMP_SENSORS_UPDATE;
	
	retval = GET_TEMP_SENSOR(CHIPSET_SENSOR, &temp);
	if(retval)
		return KEAPI_RET_RETRIEVAL_ERROR;
	
    *pChipsetTemp = TEMP_SENSOR_TO_MILLIDEGREES(CHIPSET_SENSOR, temp);
    return KEAPI_RET_SUCCESS;
    }

KEAPI_RETVAL keapiGetSystemTemp (
        KEAPI_PINT32 pSystemTemp
	)
    {
	SENSOR_TEMP_TYPE temp;
	int retval;
	
	if (!TEMP_SENSORS_INITED)
		{
		return KEAPI_RET_RETRIEVAL_ERROR;
		}
	
	TEMP_SENSORS_UPDATE;
	
    retval = GET_TEMP_SENSOR(SYSTEM_SENSOR, &temp);
    if(retval)
    	return KEAPI_RET_RETRIEVAL_ERROR;
    
    *pSystemTemp = TEMP_SENSOR_TO_MILLIDEGREES(SYSTEM_SENSOR, temp);
    return KEAPI_RET_SUCCESS;
    }

#ifndef KEAPI_VOLTAGE_SENSORS_UNSUPPORTED

KEAPI_RETVAL keapiGetVoltageSensorCount (
        KEAPI_PINT32 pVoltageSensorCount
    )
    {
    *pVoltageSensorCount = VSENSORS_COUNT;
    return KEAPI_RET_SUCCESS;
    }

KEAPI_RETVAL keapiGetVoltageSensorList (
        PVOLTAGE_SENSOR pVoltageSensors,
        KEAPI_INT32 voltageSensorCount
    )
    {
    KEAPI_INT32 i;
    int retval;
        
    if (voltageSensorCount > VSENSORS_COUNT)
        {
        return KEAPI_RET_PARAM_ERROR;
        }
        
    if (!VOLTAGE_SENSORS_INITED)
        {
        return KEAPI_RET_RETRIEVAL_ERROR;
        }
        
    VOLTAGE_SENSORS_UPDATE;
    
    for (i = 0; i < voltageSensorCount; i++)
        {
      	SENSOR_VOLTAGE_TYPE val;
      	retval = GET_VOLTAGE_SENSOR(i, &val);
      	if(retval)
      		return KEAPI_RET_RETRIEVAL_ERROR;
      	
        strncpy(pVoltageSensors[i].name, vsensorNames[i], MAX_STR);
        pVoltageSensors[i].value = VOLTAGE_SENSOR_TO_MILLIVOLTS(i, val);
        }
        
    return KEAPI_RET_SUCCESS;
    }

KEAPI_RETVAL keapiGetVoltageSensorValue (
        KEAPI_INT32 voltageSensorNr,
        KEAPI_PINT32 pVoltageSensorValue
	)
    {
	SENSOR_VOLTAGE_TYPE val;
	int retval;
	
	if (voltageSensorNr >= VSENSORS_COUNT)
	    {
	    return KEAPI_RET_PARAM_ERROR;
	    }
	        
	if (!VOLTAGE_SENSORS_INITED)
		{
	    return KEAPI_RET_RETRIEVAL_ERROR;
	    }
	        
	VOLTAGE_SENSORS_UPDATE;
	   
	retval = GET_VOLTAGE_SENSOR(voltageSensorNr, &val);
	if(retval)
		return KEAPI_RET_RETRIEVAL_ERROR;
	
    *pVoltageSensorValue = VOLTAGE_SENSOR_TO_MILLIVOLTS(voltageSensorNr, val);
    return KEAPI_RET_SUCCESS;
    }
#endif /* KEAPI_VOLTAGE_SENSORS_UNSUPPORTED */

#ifndef KEAPI_FAN_UNSUPPORTED
KEAPI_RETVAL keapiGetFanCount (
        KEAPI_PINT32 pFanCount
	)
    {
	*pFanCount = FSENSORS_COUNT;
    return KEAPI_RET_SUCCESS;
    }

KEAPI_RETVAL keapiGetFanList (
        PFAN pFanSensors,
        KEAPI_INT32 fanSensorCount
	)
    {
    KEAPI_INT32 i;
    int retval;
        
    if (fanSensorCount > FSENSORS_COUNT)
        {
        return KEAPI_RET_PARAM_ERROR;
        }
        
    if (!FAN_SENSORS_INITED)
		{
        return KEAPI_RET_RETRIEVAL_ERROR;
        }
        
    FAN_SENSORS_UPDATE;
    for (i = 0; i < fanSensorCount; i++)
		{
       	GET_FAN_SENSOR_METHOD(i, &pFanSensors[i].method, retval);
       	if(retval != KEAPI_RET_SUCCESS)
       		return KEAPI_RET_RETRIEVAL_ERROR;
       	
        strncpy(pFanSensors[i].name, fansensorNames[i], MAX_STR);
        retval = GET_FAN_SENSOR_PWM_RPM(i, &pFanSensors[i].pwm, 
        		&pFanSensors[i].rpm);
        if(retval)
        	return KEAPI_RET_RETRIEVAL_ERROR;
        
        retval = GET_FAN_SENSOR_POWER_MODE(i, &pFanSensors[i].powerMode);
        if(retval)
        	return KEAPI_RET_RETRIEVAL_ERROR;
        }
        
    return KEAPI_RET_SUCCESS;
    }

KEAPI_RETVAL keapiGetFanSpeed (
	KEAPI_INT32 fanNr,
	KEAPI_PUINT8 pwm,
	KEAPI_PINT32 rpm
    )
    {
	int retval;
	
	if (fanNr >= FSENSORS_COUNT)
	    {
	    return KEAPI_RET_PARAM_ERROR;
	    }
	
    if (!FAN_SENSORS_INITED)
        {
        return KEAPI_RET_RETRIEVAL_ERROR;
        }
    
    FAN_SENSORS_UPDATE;
    retval = GET_FAN_SENSOR_PWM_RPM(fanNr, pwm, rpm);
    if(retval)
    	return KEAPI_RET_RETRIEVAL_ERROR;
    
    return KEAPI_RET_SUCCESS;
    }

IMPORT KEAPI_RETVAL keapiGetFanMode (
	KEAPI_UINT32 fanNr,
	KEAPI_PUINT8 fanMode
    )
	{
	int retval;
	
	if (fanNr >= FSENSORS_COUNT)
	    {
	    return KEAPI_RET_PARAM_ERROR;
	    }
	
    if (!FAN_SENSORS_INITED)
        {
        return KEAPI_RET_RETRIEVAL_ERROR;
        }
    
    FAN_SENSORS_UPDATE;
    GET_FAN_SENSOR_METHOD(fanNr, fanMode, retval);
    
    return retval;
	}

IMPORT KEAPI_RETVAL keapiSetFanModeManual (
	KEAPI_INT32 fanNr,
	KEAPI_UINT8 targetPwm
	)
	{
	int retval;
	if (fanNr >= FSENSORS_COUNT)
	    {
	    return KEAPI_RET_PARAM_ERROR;
	    }
	
    if (!FAN_SENSORS_INITED)
        {
        return KEAPI_RET_ERROR;
        }
    
    retval = FAN_SET_MODE_MANUAL(fanNr, targetPwm);
    if(retval)
    	return KEAPI_RET_RETRIEVAL_ERROR;
    
    return KEAPI_RET_SUCCESS;
	}

IMPORT KEAPI_RETVAL keapiSetFanModeThermalCruise (
	KEAPI_UINT32 fanNr,
	KEAPI_UINT32 targetTemp,
	KEAPI_UINT32 toleranceTemp
    )
	{
	int retval;
	
	if (fanNr >= FSENSORS_COUNT)
		{
		return KEAPI_RET_PARAM_ERROR;
	    }
		
	if (!FAN_SENSORS_INITED)
	    {
	    return KEAPI_RET_ERROR;
	    }
	    
	FAN_SET_MODE_THERMAL_CRUISE(fanNr, targetTemp, toleranceTemp, retval);
	
	return retval;
	}

IMPORT KEAPI_RETVAL keapiGetCpuFanSpeed (
	KEAPI_PINT32 pCpuFanRpm
    )
	{
	int retval;
	
	if (!FAN_SENSORS_INITED)
		{
		return KEAPI_RET_RETRIEVAL_ERROR;
		}
	
	FAN_SENSORS_UPDATE;
	
	retval = GET_FAN_SENSOR_RPM(CPU_FAN_NUMBER, pCpuFanRpm);
	if (retval)
		return KEAPI_RET_RETRIEVAL_ERROR;

	return KEAPI_RET_SUCCESS;
	}

IMPORT KEAPI_RETVAL keapiSetCpuFanSpeed (
	KEAPI_UINT8 cpuFanSpeedPwm
    )
	{
	return keapiSetFanModeManual(CPU_FAN_NUMBER, cpuFanSpeedPwm);
	}
	
IMPORT KEAPI_RETVAL keapiGetSystemFanSpeed (
	KEAPI_PINT32 pSystemFanRpm
	)
	{
	int retval;

	if (!FAN_SENSORS_INITED)
		{
		return KEAPI_RET_RETRIEVAL_ERROR;
		}
	
	FAN_SENSORS_UPDATE;
	
	retval = GET_FAN_SENSOR_RPM(SYSTEM_FAN_NUMBER, pSystemFanRpm);
	if (retval)
		return KEAPI_RET_RETRIEVAL_ERROR;
	
	return KEAPI_RET_SUCCESS;
	}

IMPORT KEAPI_RETVAL keapiSetSystemFanSpeed (
	KEAPI_UINT8 systemFanSpeedPwm
    )
	{
	return keapiSetFanModeManual(SYSTEM_FAN_NUMBER, systemFanSpeedPwm);
	}

#endif /* KEAPI_FAN_UNSUPPORTED */
