#!/bin/sh
#
# This is part of OpenIPC.org project | 2022.01.19
#

# Board
board=demo

# SoC detect
chipid=$(ipcinfo --chip-name)

# Memory config
mem_start=0x40000000 # phy mem start

mem_total=$(fw_printenv -n totalmem | tr -d 'M')
mem_total=${mem_total:=64}

os_mem_size=$(fw_printenv -n osmem | tr -d 'M')
os_mem_size=${os_mem_size:=32}

YUV_TYPE0=0 # 0 -- raw, 1 --DC, 2 --bt1120, 3 --bt656

##################################################################
#
# DDR start:0x40000000, kernel start:0x40000000,  OS(32M); MMZ start:0x42000000
#
# echo "${chipid}: ${mem_total}/${os_mem_size} | ${mem_start}/${mmz_start} | ${mmz_size}"
#
#
# Hi3516Dv200
#
# mem_total=512             # 512M, total mem
# mem_start=0x40000000      # phy mem start
# os_mem_size=32            # 32M, os mem
# mmz_start=0x42000000;     # mmz start addr
# mmz_size=480M;            # 480M, mmz size
#
#
# Hi3516Ev200
#
# mem_total=64              # 64M, total mem
# mem_start=0x40000000      # phy mem start
# os_mem_size=32            # 32M, os mem
# mmz_start=0x42000000;     # mmz start addr
# mmz_size=32M;             # 32M, mmz size
#
#
# Hi3516Ev300
#
# mem_total=128             # 128M, total mem
# mem_start=0x40000000      # phy mem start
# os_mem_size=32            # 32M, os mem
# mmz_start=0x42000000;     # mmz start addr
# mmz_size=96M;             # 96M, mmz size
#
#
# Hi3518Ev300
#
# mem_total=64              # 64M, total mem
# mem_start=0x40000000      # phy mem start
# os_mem_size=32            # 32M, os mem
# mmz_start=0x42000000;     # mmz start addr
# mmz_size=32M;             # 32M, mmz size
#
##################################################################

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

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"
}

insert_osal() {
	calc_mmz_info

	if [ $mem_total -ge 128 ]; then
    	    allocator=cma
	else
	    allocator=hisi
	fi

	#modprobe open_osal anony=1 mmz_allocator=${allocator} mmz=anonymous,0,$mmz_start,$mmz_size || report_error
        modprobe open_osal anony=1 mmz_allocator=hisi mmz=anonymous,0,$mmz_start,$mmz_size || report_error
}

insert_detect() {
	modprobe open_sys_config chip=${chipid} sensors=unknown g_cmos_yuv_flag=$YUV_TYPE0 board=${board}
	insert_osal
	modprobe open_base
	modprobe hi3516ev200_isp
	modprobe open_sensor_i2c
	modprobe open_sensor_spi
}

remove_detect() {
	rmmod -w open_sensor_spi
	rmmod -w open_sensor_i2c
	rmmod -w hi3516ev200_isp
	rmmod -w open_base
	rmmod -w open_osal
	rmmod -w open_sys_config
}

insert_audio() {
	modprobe open_aio
	modprobe open_ai
	modprobe open_ao
	modprobe open_aenc
	modprobe open_adec
	modprobe open_acodec
	#	insmod hi_tlv320aic31.ko
	echo "insert audio"
}

remove_audio() {
	rmmod -w open_acodec
	#	rmmod -w hi_tlv320aic31
	rmmod -w open_adec
	rmmod -w open_aenc
	rmmod -w open_ao
	rmmod -w open_ai
	rmmod -w open_aio

	echo "remove audio"
}

insert_isp() {
	modprobe hi3516ev200_isp
}

insert_sil9024() {
	i2c_sel=2
	if [ "${chipid}" == "hi3516ev300" ]; then
		i2c_sel=1
	fi

	if [ "${board}" == "sck" ]; then
		insmod hi_sil9024.ko norm=12 i2c_num=$i2c_sel #1080P@30fps
	fi
}

insert_adv7179() {
	i2c_sel=2
	if [ "${chipid}" == "hi3516ev300" ]; then
		i2c_sel=1
	fi

	if [ "${board}" == "sck" ]; then
		insmod hi_adv7179.ko norm_mode=0 i2c_num=$i2c_sel # norm_mode = 0:PAL, 1:NTSC
	fi
}

insert_piris() {
	if [ "{chipid}" == "hi3516ev300" -a "${board}" == "demo" ]; then
		modprobe open_piris
		# modprobe open_pm   # Maybe need for Hi3518Ev300
	fi
}

insert_ko() {
	modprobe open_sys_config chip=${chipid} sensors=$SENSOR g_cmos_yuv_flag=$YUV_TYPE0 board=${board}
	insert_osal
	modprobe open_base
	modprobe open_sys
	#	modprobe open_tde
	modprobe open_rgn
	modprobe open_vgs max_vgs_job=20 max_vgs_node=20 max_vgs_task=20
	modprobe open_vi
	insert_isp
	modprobe open_vpss
	#	modprobe open_vo                             # disabled, info by @widgetii
	#	insmod hifb video="hifb:vram0_size:1620"     # default fb0:D1  # disabled, info by @widgetii
	modprobe open_chnl
	modprobe open_vedu
	modprobe open_rc
	modprobe open_venc VencMaxChnNum=3
	modprobe open_h264e
	modprobe open_h265e
	modprobe open_jpege
	modprobe open_ive save_power=0
	modprobe open_pwm
	modprobe open_sensor_i2c
	modprobe open_sensor_spi
	insert_piris
	insert_sil9024 # BT1120
	#	insert_adv7179; # BT656
	insert_audio
	modprobe open_mipi_rx
	#	modprobe open_user
	modprobe open_wdt
}

remove_ko() {
	rmmod -w open_wdt
	#	rmmod -w hi_user
	remove_audio
	#	rmmod -w hi_piris
	rmmod -w hi_pwm
	rmmod -w hi_mipi_rx
	rmmod -w hi_sil9024 &>/dev/null
	#	rmmod -w hi_adv7179 &> /dev/null
	rmmod -w open_ive
	rmmod -w open_rc
	rmmod -w open_jpege
	rmmod -w open_h264e
	rmmod -w open_h265e
	rmmod -w open_venc
	rmmod -w open_vedu
	rmmod -w open_chnl
	#	rmmod -w hifb
	#	rmmod -w open_vo
	rmmod -w open_vpss
	rmmod -w hi3516ev200_isp
	rmmod -w open_vi
	rmmod -w open_vgs
	rmmod -w open_rgn
	#	rmmod -w open_tde
	rmmod -w open_sensor_i2c &>/dev/null
	rmmod -w open_sensor_spi &>/dev/null
	rmmod -w open_sys
	rmmod -w open_base
	rmmod -w open_osal
	rmmod -w open_sys_config
}

load_usage() {
	echo "Usage:  ./load_hisilicon [-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: imx307]"
	echo "    -h                       help information"
	echo -e "Available sensors: gc2053 imx307 imx327 imx335 os05a sc2231 sc2235 sc4236 sc3235 sc4236 etc."
	echo -e "for example: ./load_hisilicon -i -sensor imx307 -osmem 32M -board demo -yuv0 0\n"
}

######################parse arg###################################
b_arg_sensor0=0
b_arg_sensor1=0
b_arg_yuv_type0=0
b_arg_insmod=0
b_arg_remove=0
b_arg_os_mem=0
b_arg_board=0

for arg in $@; do
	if [ $b_arg_sensor0 -eq 1 ]; then
		b_arg_sensor0=0
		SENSOR_MANUAL=$arg
		export SENSOR=${SENSOR_MANUAL}
	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_yuv_type0 -eq 1 ]; then
		b_arg_yuv_type0=0
		YUV_TYPE0=$arg
	fi

	if [ $b_arg_board -eq 1 ]; then
		b_arg_board=0
		BOARD=$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
		;;
	"-sensor0")
		b_arg_sensor0=1
		;;
	"-sensor")
		b_arg_sensor0=1
		;;
	"-osmem")
		b_arg_os_mem=1
		;;
	"-yuv0")
		b_arg_yuv_type0=1
		;;
	"-board")
		b_arg_board=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

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

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

# Sensor config
# SENSOR=${SENSOR:=imx307}
#
if [ -n "$SENSOR" ]; then
	logger -s -p daemon.info -t hisilicon "Manualy set SENSOR as ${SENSOR}"
else
	if fw_printenv -n sensor >/dev/null; then
		SENSOR_ENV=$(fw_printenv -n sensor)
		export SENSOR=${SENSOR_ENV}
		logger -s -p daemon.info -t hisilicon "Get data from environment and set SENSOR as ${SENSOR}"
	else
		insert_detect
		SENSOR_DETECT=$(ipcinfo --short-sensor)
		export SENSOR=${SENSOR_DETECT:=unknown}
		remove_detect
		logger -s -p daemon.info -t hisilicon "Get data from ipcinfo and set SENSOR as ${SENSOR}"
		fw_setenv sensor $SENSOR && logger -s -p daemon.info -t hisilicon "Write detected ${SENSOR} to U-Boot ENV"
	fi
fi

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

if [ "$SENSOR" = "unknown" ]; then
	exit 1
else
	if [ $b_arg_insmod -eq 1 ]; then
		insert_ko
	fi
fi
