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


####################Variables Definition##########################


SNS_TYPE=bt1120        # sensor type

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

##################################################################

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

insert_audio()
{
	insmod acodec.ko
	insmod hi3516a_aio.ko
	insmod hi3516a_ai.ko
	insmod hi3516a_ao.ko
	insmod hi3516a_aenc.ko
	insmod hi3516a_adec.ko
	echo "insert audio"
}

remove_audio()
{
	rmmod hi3516a_adec
	rmmod hi3516a_aenc
	rmmod hi3516a_ao
	rmmod hi3516a_ai
	rmmod hi3516a_aio
	rmmod acodec
	echo "remove audio"
}

insert_sns()
{
	case $SNS_TYPE in
		bt1120)
			
			;;
		*)
			echo "xxxx Invalid sensor type $SNS_TYPE xxxx"
			report_error
			;;
	esac
}

remove_sns()
{
	rmmod ssp &> /dev/null
	rmmod sensor_spi &> /dev/null
	rmmod ssp_pana &> /dev/null
}

sys_config()
{
	# pinmux configuration
	sh pinmux_hi3516a.sh > /dev/null

	# clock configuration
	sh clkcfg_hi3516a.sh > /dev/null

	# system configuration
	sh sysctl_hi3516a.sh $b_arg_online  > /dev/null
}

insert_ko()
{
	# sys config
	sys_config;

	# driver load
	insmod mmz.ko mmz=anonymous,0,$mmz_start,$mmz_size anony=1 || report_error
	insmod hi_media.ko
	insmod hi3516a_base.ko
	
	insmod hi3516a_sys.ko vi_vpss_online=$b_arg_online sensor=$SNS_TYPE

	insmod hi3516a_tde.ko
	insmod hi3516a_region.ko
	insmod hi3516a_vgs.ko

	insmod hi3516a_isp.ko
	insmod hi3516a_viu.ko detect_err_frame=10;
	insmod hi3516a_vpss.ko
	insmod hi3516a_vou.ko lowPowerMode=0
	#insmod hi3516a_vou.ko detectCycle=0            #close dac detect
	insmod hifb.ko video="hifb:vram0_size:1620"     # default pal

	insmod hi3516a_rc.ko
	insmod hi3516a_venc.ko
	insmod hi3516a_chnl.ko
	insmod hi3516a_h264e.ko
	insmod hi3516a_h265e.ko
	insmod hi3516a_jpege.ko
	insmod hi3516a_vda.ko
	insmod hi3516a_ive.ko

	insmod extdrv/wdt.ko default_margin=30 nowayout=1 nodeamon=1
	insmod extdrv/gpioi2cdrv.ko

	insmod extdrv/adv_7611.ko
	if [ $? -ne 0 ]; then
		insmod extdrv/it6604drv.ko
	fi

	insmod extdrv/alpu_c.ko
#	insmod extdrv/8188eu.ko
	insmod extdrv/8192du.ko

	echo "==== Your input Sensor type is $SNS_TYPE ===="
	insert_sns

	insert_audio
	echo "==== Your input Sensor type is $SNS_TYPE ===="

    insmod hi_mipi.ko
}

remove_ko()
{
	remove_audio
	remove_sns

	rmmod sil9024 &> /dev/null
	rmmod pwm

	rmmod hi3516a_ive
	rmmod hi3516a_vda
	
	rmmod hi3516a_rc
	rmmod hi3516a_jpege
	rmmod hi3516a_h264e
	rmmod hi3516a_h265e
	rmmod hi3516a_chnl
	rmmod hi3516a_venc

	rmmod hifb
	rmmod hi3516a_vou
	rmmod hi3516a_vpss
	rmmod hi3516a_viu
	rmmod hi_mipi

	rmmod ssp_ad9020
	rmmod hi3516a_vgs
	rmmod hi3516a_region
	rmmod hi3516a_tde

	rmmod sensor_i2c
	rmmod piris
	rmmod hi3516a_isp
	rmmod hi3516a_sys
	rmmod hi3516a_base
	rmmod hi_media
	rmmod mmz
}

sys_restore()
{
	####################################################
	pinmux_hi3516a.sh > /dev/null

	clkcfg_hi3516a.sh > /dev/null

	# system configuration
	sysctl_hi3516a.sh $b_arg_online > /dev/null

	insert_sns;
}

load_usage()
{
	echo "Usage:  ./load3516a [-option] [sensor_name]"
	echo "options:"
	echo "    -i                       insert modules"
	echo "    -r                       remove modules"
	echo "    -a                       remove modules first, then insert modules"
	echo "    -sensor sensor_name      config sensor type [default: ar0130]"
	echo "    -osmem os_mem_size       config os mem size [unit: M, default: 64]"
	echo "    -offline                 vi/vpss offline"
	echo "    -h                       help information"
	echo -e "Available sensors: imx178, imx185, mn34220, etc"
	echo -e "notes: osmem option can't be used when mmz zone partition is enable\n\n"
	echo -e "for example online:   ./load3516a -a -sensor imx178 -osmem 64\n"
	echo -e "            offline:  ./load3516a -a -sensor imx178 -osmem 64 -offline\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_sensor=0
b_arg_insmod=0
b_arg_remove=0
b_arg_online=1
b_arg_restore=0

for arg in $@
do
	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

		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;
	fi

	if [ $b_arg_sensor -eq 1 ] ; then
		b_arg_sensor=0
		SNS_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;
			;;
		"-sensor")
			b_arg_sensor=1;
			;;
		"-osmem")
			b_arg_os_mem=1;
			;;
		"-restore")
			b_arg_restore=1;
			;;
		"-offline")
			b_arg_online=0;
			;;
	esac
done
#######################parse arg end########################

#######################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

if [ $b_arg_restore -eq 1 ]; then	
	sys_restore;
fi

