#!/bin/sh
# Useage: ./load3521d [ -r|-i|-a ] [ ad ]
#         -r : rmmod all modules
#         -i : insmod all modules
#    default : rmmod all moules and then insmod them
#

####################Variables Definition##########################
AD_TYPE=6134        	# ad type

mem_total=512;		# 512M, total mem
mem_start=0x80000000;	# phy mem start

os_mem_size=200;		# 64M, os mem
mmz_start=0x8C800000;	# mmz start addr
mmz_size=312M;		# 128M, mmz size
board_mode="demo";	# demo or sck
if [ "`cat /link/config/fac`" == "SH" ]; then
mem_total=1024;		# 512M, total mem
mem_start=0x80000000;	# phy mem start

os_mem_size=256;		# 64M, os mem
mmz_start=0x90000000;	# mmz start addr
mmz_size=768M;		# 128M, mmz size
board_mode="demo";		# demo or sck
fi

##################################################################
b_arg_demb=1

report_error()
{
	echo "******* Error: There's something wrong, please check! *****"
	exit 1
}

insert_audio()
{
	insmod hi3521d_aio.ko
	insmod hi3521d_ai.ko
	insmod hi3521d_ao.ko
	insmod hi3521d_aenc.ko
	insmod hi3521d_adec.ko
#	insmod extdrv/tlv_320aic31.ko 
}

remove_audio()
{
#	rmmod tlv_320aic31.ko
	rmmod hi3521d_adec
	rmmod hi3521d_aenc
	rmmod hi3521d_ao
	rmmod hi3521d_ai
	rmmod hi3521d_aio
}

insert_ad()
{
	case $AD_TYPE in
		6134)
			insmod extdrv/nvp6134_ex.ko
			;;
		2823)
			insmod extdrv/tp2823.ko
			;;			
		6574)
			insmod extdrv/rn6574.ko
			;;
		*)
			echo "xxxx Invalid ad type $AD_TYPE xxxx"
			report_error
			;;
	esac
}

remove_ad()
{
	case $AD_TYPE in
		6134)
			rmmod nvp6134_ex.ko
			;;
		2823)
			rmmod tp2823
			;;			
		6574)
			rmmod rn6574
			;;
		*)
			echo "xxxx Invalid ad type $AD_TYPE xxxx"
			report_error
			;;
	esac
}


insert_ko()
{
	# sys_config(drv/sys_config): pinmux and ddr priority configuration
	insmod sys_config.ko mode=$board_mode 
	#echo $board_mode;
	# osal load (contains mmz.ko/hiuser.ko/media.ko)
	insmod hi_osal.ko mmz=anonymous,0,$mmz_start,$mmz_size anony=1 || report_error
	insmod hi_base.ko vb_force_exit=1
	insmod hi3521d_sys.ko mem_total=$mem_total
	if [[ $? -ne 0 ]]; then
		exit;
	fi 
	insmod hi3521d_vdec.ko
	insmod hi3521d_vfmw.ko 
	insmod hi3521d_jpegd.ko
	insmod hi3521d_tde.ko
	insmod hi3521d_region.ko
	insmod hi3521d_vgs.ko
	insmod hi3521d_viu.ko detect_err_frame=-1;	
	
	insmod hi3521d_vpss.ko
	insmod hi3521d_vou.ko
	insmod hifb.ko  video="hifb:vram0_size:32400,vram1_size:8100" softcursor="off"
	insmod hi3521d_hdmi.ko

	insmod hi3521d_rc.ko
	insmod hi3521d_venc.ko
	insmod hi3521d_chnl.ko
	insmod hi3521d_vedu.ko
	insmod hi3521d_h264e.ko H264eOneStreamBuffer=1
	insmod hi3521d_h265e.ko H265eOneStreamBuffer=1
	insmod hi3521d_jpege.ko
	insmod hi3521d_vda.ko
	insmod hi3521d_ive.ko
	insmod hi_rtc.ko
	insmod wdt.ko 
	insmod hi_ir.ko 
	insmod extdrv/gpio_com_drv.ko

if [ "`cat /link/config/fac`" == "SH" ]; then
	insmod extdrv/gs2971a.ko
	insmod extdrv/8821cu_21.ko
	insmod extdrv/8852bu_21.ko
else	
	insmod extdrv/gpioi2c010_drv.ko
	insmod extdrv/gpioi2c032_drv.ko
	insmod extdrv/8821cu.ko
	insmod extdrv/8852bu.ko
fi	
	insmod extdrv/sha204drv.ko


	#echo "==== Your input AD type is $AD_TYPE ===="
#	insert_ad

	insert_audio

}

remove_ko()
{
	remove_audio
	remove_ad

	rmmod hi3521d_ive
	rmmod hi3521d_vda

	
	rmmod hi3521d_jpege
	rmmod hi3521d_h264e
	rmmod hi3521d_h265e
	rmmod hi3521d_vedu
	rmmod hi3521d_chnl
	rmmod hi3521d_venc
	rmmod hi3521d_rc

	rmmod hi3521d_hdmi
	rmmod hifb
	rmmod hi3521d_vou
	rmmod hi3521d_vpss
	rmmod hi3521d_viu

	rmmod hi3521d_vgs
	rmmod hi3521d_region
	rmmod hi3521d_tde

	rmmod hi3521d_jpegd
	rmmod hi3521d_vfmw
	rmmod hi3521d_vdec
	rmmod hi3521d_sys
	rmmod hi_base
	rmmod hi_osal
	rmmod sys_config
}

load_usage()
{
	echo "Usage:  ./load3521d [-option] [ad_name]"
	echo "options:"
	echo "    -i                       insert modules"
	echo "    -r                       remove modules"
	echo "    -a                       remove modules first, then insert modules"
	echo "    -ad ad_name              config AD type [default: nvp6134]"
        echo "    -total mem_size          config total mem size [unit: M, default: 2048]"
	echo "    -osmem os_mem_size       config os mem size [unit: M, default: 64]"
	echo "    -mode demo               config board type [demo or sck, default: demo]"
	echo "    -h                       help information"
	echo -e "Available ad: nvp6134, tp2823"
	echo -e "notes: osmem option can't be used when mmz zone partition is enable\n\n"
	echo -e "for example: ./load3521d -a -ad 6134 -total 2048 -osmem 64\n"
}

calc_mmz_info()
{
	mmz_start=`echo "$mem_start $os_mem_size" | 
	awk 'BEGIN { temp = 0; }
	{
		temp = $1/1024/1024 + $2;
	} 
	END { printf("0x%x00000\n", temp); }'`

	mmz_size=`echo "$mem_total $os_mem_size" | 
	awk 'BEGIN { temp = 0; }
	{
		temp = $1 - $2;
	} 
	END { printf("%dM\n", temp); }'`
	echo "mmz_start: $mmz_start, mmz_size: $mmz_size"
}


######################parse arg###################################
b_arg_os_mem=0
b_arg_total_mem=0
b_arg_ad=0
b_arg_insmod=0
b_arg_remove=0
b_arg_mode=0

for arg in $@
do
	if [ $b_arg_total_mem -eq 1 ]; then
		b_arg_total_mem=0;
		mem_total=$arg;
		
		if [ -z $mem_total ]; then
			echo "[error] mem_total is null"
			exit;
		fi
	fi
	
	if [ $b_arg_mode -eq 1 ]; then
		b_arg_mode=0;
		board_mode=$arg;
		
		if [ -z $board_mode ]; then
			echo "[error] board_mode is null"
			exit;
		fi
	fi
	
	if [ $b_arg_os_mem -eq 1 ] ; then
		b_arg_os_mem=0;
		os_mem_size=$arg;

		if [ -z $os_mem_size ]; then
			echo "[error] os_mem_size is null"
			exit;
		fi
	fi

	if [ $b_arg_ad -eq 1 ] ; then
		b_arg_ad=0
		AD_TYPE=$arg;
	fi

	case $arg in
		"-i")
			b_arg_insmod=1;
			;;

		"-r")
			b_arg_remove=1;
			;;
		"-a")			
			b_arg_insmod=1;
			b_arg_remove=1;
			;;

		"-h")
			load_usage;
			;;

		"-ad")
			b_arg_ad=1;
			;;

		"-osmem")
			b_arg_os_mem=1;
			;;
		"-total")
			b_arg_total_mem=1;
			;;
		"-mode")
			b_arg_mode=1;
			;;			
	esac
	

	
done
#######################parse arg end########################

if [ $os_mem_size -ge $mem_total ] ; then
	echo "[err] os_mem[$os_mem_size], over total_mem[$mem_total]"
	exit;
fi

calc_mmz_info;

#######################Action###############################

if [ $# -lt 1 ]; then
    load_usage;
    exit 0;
fi

if [ $b_arg_remove -eq 1 ]; then
	remove_ko;
fi
if [ $b_arg_insmod -eq 1 ]; then
	insert_ko;
fi
