#!/bin/sh
# Useage: ./load3536c [ -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=1024;		# 512M, total mem
mem_start=0x80000000;	# phy mem start

os_mem_size=64;		# 64M, os mem
mmz_start=0x84000000;	# mmz start addr
mmz_size=960M;		# 128M, mmz size

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

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

insert_audio()
{
	insmod hi3536c_aio.ko
	insmod hi3536c_ai.ko
	insmod hi3536c_ao.ko
	insmod hi3536c_aenc.ko
	insmod hi3536c_adec.ko
#	insmod extdrv/tlv_320aic31.ko 
}

remove_audio()
{
#	rmmod tlv_320aic31.ko
	rmmod hi3536c_adec
	rmmod hi3536c_aenc
	rmmod hi3536c_ao
	rmmod hi3536c_ai
	rmmod hi3536c_aio
}


insert_ko()
{
	# low power control
	# source ./lowpower.sh > /dev/null

	# pinmux configuration
#	source ./pinmux_hi3536c_vga_hdmi_spi.sh > /dev/null
#	source ./pinmux_hi3536c_i2s.sh > /dev/null

	source ./padctrl_hi3536c.sh > /dev/null
	if [ $b_arg_demb -eq 1 ]; then
		source ./pinmux_demob.sh > /dev/null
		AD_TYPE=6134
	else
		source ./pinmux_sckb.sh > /dev/null	
		AD_TYPE=6134
	fi
	
	# crg configuration
	source ./crgctrl_hi3536c.sh > /dev/null

	# system configuration
	source ./sysctl_hi3536c_asic.sh > /dev/null

	# driver load
	insmod mmz.ko mmz=anonymous,0,$mmz_start,$mmz_size anony=1 || report_error
	insmod hiuser.ko
	insmod hi_media.ko
	insmod hi3536c_base.ko
	insmod hi3536c_sys.ko mem_total=$mem_total
	if [[ $? -ne 0 ]]; then
		exit;
	fi

	insmod hi3536c_vdec.ko VBSource=2
#	insmod hi3536c_vdec.ko
	insmod hi3536c_vfmw.ko 
	insmod hi3536c_jpegd.ko
	insmod hi3536c_tde.ko
	insmod hi3536c_region.ko
	insmod hi3536c_vgs.ko
	
	insmod hi3536c_vpss.ko
	insmod hi3536c_vou.ko
	insmod hifb.ko  video="hifb:vram0_size:32400,vram1_size:8100,vram2_size:2160,vram3_size:128,vram4_size:128" softcursor="off"
	insmod hi3536c_hdmi.ko

	insmod hi3536c_rc.ko
	insmod hi3536c_venc.ko
	insmod hi3536c_chnl.ko
	insmod hi3536c_h264e.ko
	insmod hi3536c_h265e.ko
	insmod hi3536c_jpege.ko
	insmod hi3536c_vda.ko
	insmod hi3536c_ive.ko
	insmod extdrv/i2c.ko
	insmod extdrv/alpu_c.ko
	insmod extdrv/wdt.ko default_margin=30 nowayout=1 nodeamon=1
#	insmod extdrv/8192du.ko
#	insmod extdrv/88x2cu.ko
	insmod extdrv/88x2bu.ko
	insmod extdrv/gpio_i2c.ko
	insmod extdrv/adv7611.ko
	insmod extdrv/oled12864drv.ko
	insert_audio
}

remove_ko()
{
	remove_audio

	rmmod hi3536c_ive
	rmmod hi3536c_vda

	
	rmmod hi3536c_jpege
	rmmod hi3536c_h264e
	rmmod hi3536c_h265e
	rmmod hi3536c_chnl
	rmmod hi3536c_venc
	rmmod hi3536c_rc

	rmmod hi3536c_hdmi
	rmmod hifb
	rmmod hi3536c_vou
	rmmod hi3536c_vpss

	rmmod hi3536c_vgs
	rmmod hi3536c_region
	rmmod hi3536c_tde

	rmmod hi3536c_jpegd
	rmmod hi3536c_vfmw
	rmmod hi3536c_vdec
	rmmod hi3536c_sys
	rmmod hi3536c_base
	rmmod hi_media
	rmmod hiuser
	rmmod mmz
}

load_usage()
{
	echo "Usage:  ./load3536c [-option] [ad_name]"
	echo "options:"
	echo "    -i                       insert modules"
	echo "    -r                       remove modules"
	echo "    -a                       remove modules first, then insert modules"
        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 "    -h                       help information"
	echo -e "notes: osmem option can't be used when mmz zone partition is enable\n\n"
	echo -e "for example: ./load3536c -a -total 1024 -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

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_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;
			;;
		"-sck")
			b_arg_demb=0;
			;;
		"-pro")
			b_arg_demb=2;
			;;
			
			
	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
