#!/bin/bash

# Special case: True HW with virtual card
somefpga=$(lspci -n | grep -c "1011:f00[1245]")

if [ "$CPU" = "Geode" ] ; then
    /sbin/modprobe eeprom 2>/dev/null
    /sbin/modprobe i2c-algo-bit 2>/dev/null
    /sbin/modprobe i2c-algo-pca 2>/dev/null
    /sbin/modprobe i2c-algo-pcf 2>/dev/null
    /sbin/modprobe scx200_acb 2>/dev/null
elif [ "${somefpga}" != "0" ] ; then
    /sbin/modprobe tdm_dma 2>/dev/null
    /sbin/modprobe tdm_i2c 2>/dev/null
else
    export I2CBUS=0
    export I2CCONFIGBUS=''
    export I2CCONFIGADDRESS=''
    export ILIDBUS=0
    return
fi


if [ -z "${I2CBUS}" ]; then
    I2CBUS=$(for i in $(i2cutil -F 2>&1 | grep tdm-i2c | sed -e "s/.*i2c-\([0-9]*\).*tdm.*$/\1/");do echo -n $i " ";done)
    if [ -z "${I2CBUS}" ] ; then
	I2CBUS="0"
    fi

    ILIDBUS=$(for i in $(i2cutil -F 2>&1 | grep tdm-i2c | sed -e "s/.*i2c-[0-9]*.*tdm-i2c-\([0-9]*\).*$/\1/");do echo -n $i " ";done)
    if [ -z "${ILIDBUS}" ] ; then
	ILIDBUS="0"
    fi

    isfpga=$(lspci -n | grep -c "1011:f001")
    if [ $isfpga -ge 1 ] ; then
        #is S2Me
	I2CCONFIGADDRESS=0x40
	I2CCONFIGBUS=${I2CBUS}
    else
	isfpga=$(lspci -n | grep -c -e "1011:f00[45]")
	if lspci -n | grep -q "1011:f004" ; then
            #is 4S2M
            I2CCONFIGADDRESS=0x40
            I2CCONFIGBUS=${I2CBUS}
	elif lspci -n | grep -q "1011:f005" ; then
	    #is AtomBox
            I2CCONFIGADDRESS=0x50
            I2CCONFIGBUS=${I2CBUS}
	else
            I2CCONFIGBUS=$(for i in $(i2cutil -F 2>&1 | grep smbus | sed -e "s/.*i2c-\([0-9]*\).*smbus.*$/\1/");do echo -n $i " ";done)
            isfpga=$(lspci -n | grep -c "1011:f002")
            if [ $isfpga -ge 1 ] ; then
            #is new fpga hardware
		if [ -z "${I2CCONFIGBUS}" ]; then
		    I2CCONFIGADDRESS=0x40
		    I2CCONFIGBUS=${I2CBUS}
		else
		    I2CCONFIGADDRESS=0x6f
		fi
            else
            #is SingleUnit geode
		I2CCONFIGADDRESS=0x6f
            fi
	fi
    fi

    if [ -z "${I2CBUS}" ] ; then
	I2CCONFIGBUS="0"
    fi
    export I2CBUS="${I2CBUS}"
    export I2CCONFIGBUS="${I2CCONFIGBUS}"
    export I2CCONFIGADDRESS=${I2CCONFIGADDRESS}
    export ILIDBUS="${ILIDBUS}"
fi
