/*
 * Copyright 2024 Sony Corporation
 */

#include <command.h>
#include <common.h>
#include <utility.h>
#include <iniparser.h>
#include <iniutility.h>
#include <debug_impl.h>
#include <fs.h>
#include <dm/device.h>
#include <dm/uclass.h>
#include <serial.h>
#include "ldbe_boot.h"

// Executes a sequence of 10 commands for LDBE booting
static int do_ldbe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
	struct udevice *udev;
	int ret = 0;

	u_char is_force_retrain;
	u_char scan_mode;
	u_char swing_level;
	u_char pre_de_emphasis;
	u_char output_signal;
	u_char dlg_mode;
	u_char mute_control;
	struct ldbe_boot_param_SetTvVby1SSCControl param1;
	struct ldbe_boot_param_SetPixelMute param2;

	UBOOT_TRACE("IN\n");
	ret = ldbe_boot_fuart_init(&udev);
	if (ret)
		return 0;

	// DISP_SetRxVby1ControlRetrain
	UBOOT_INFO("DISP_SetRxVby1ControlRetrain\n");
	is_force_retrain = 0;
	ret = ldbe_boot_send_cmd_no_reply(udev, LDBE_DISP_SetRxVby1ControlRetrain_cb, (void *)&is_force_retrain);
	if (ret) {
		UBOOT_ERROR("DISP_SetRxVby1ControlRetrain: Command transmission has failed, so the command will be aborted.\n");
		return 0;
	}

	// DISP_SetTxVby1ControlRetrain
	UBOOT_INFO("DISP_SetTxVby1ControlRetrain\n");
	is_force_retrain = 0;
	ret = ldbe_boot_send_cmd_no_reply(udev, LDBE_DISP_SetTxVby1ControlRetrain_cb, (void *)&is_force_retrain);
	if (ret) {
		UBOOT_ERROR("DISP_SetTxVby1ControlRetrain: Command transmission has failed, so the command will be aborted.\n");
		return 0;
	}

	// DISP_SetTxVby1ScanMode
	UBOOT_INFO("DISP_SetTxVby1ScanMode\n");
	scan_mode = 0;
	ret = ldbe_boot_send_cmd_no_reply(udev, LDBE_DISP_SetTxVby1ScanMode_cb, (void *)&scan_mode);
	if (ret) {
		UBOOT_ERROR("DISP_SetTxVby1ScanMode: Command transmission has failed, so the command will be aborted.\n");
		return 0;
	}

	// DISP_SetTxVby1SwingLevel
	UBOOT_INFO("DISP_SetTxVby1SwingLevel\n");
	swing_level = 0;
	ret = ldbe_boot_send_cmd_no_reply(udev, LDBE_DISP_SetTxVby1SwingLevel_cb, (void *)&swing_level);
	if (ret) {
		UBOOT_ERROR("DISP_SetTxVby1SwingLevel: Command transmission has failed, so the command will be aborted.\n");
		return 0;
	}

	// DISP_SetTxVby1PreDEEmphasis
	UBOOT_INFO("DISP_SetTxVby1PreDEEmphasis\n");
	pre_de_emphasis = 0;
	ret = ldbe_boot_send_cmd_no_reply(udev, LDBE_DISP_SetTxVby1PreDEEmphasis_cb, (void *)&pre_de_emphasis);
	if (ret) {
		UBOOT_ERROR("DISP_SetTxVby1PreDEEmphasis: Command transmission has failed, so the command will be aborted.\n");
		return 0;
	}

	// DISP_SetTxVby1OutputEnable
	UBOOT_INFO("DISP_SetTxVby1OutputEnable\n");
	output_signal = 0;
	ret = ldbe_boot_send_cmd_no_reply(udev, LDBE_DISP_SetTxVby1OutputEnable_cb, (void *)&output_signal);
	if (ret) {
		UBOOT_ERROR("DISP_SetTxVby1OutputEnable: Command transmission has failed, so the command will be aborted.\n");
		return 0;
	}

	// DISP_SetTxVby1SSCControl
	UBOOT_INFO("DISP_SetTxVby1SSCControl\n");
	param1.control_enable = 0;
	param1.modulation = 0;
	param1.deviation = 0;
	ret = ldbe_boot_send_cmd_no_reply(udev, LDBE_DISP_SetTxVby1SSCControl_cb, (void *)&param1);
	if (ret) {
		UBOOT_ERROR("DISP_SetTxVby1SSCControl: Command transmission has failed, so the command will be aborted.\n");
		return 0;
	}

	// DISP_SetTxVby1DLGControl
	UBOOT_INFO("DISP_SetTxVby1DLGControl\n");
	dlg_mode = 0;
	ret = ldbe_boot_send_cmd_no_reply(udev, LDBE_DISP_SetTxVby1DLGControl_cb, (void *)&dlg_mode);
	if (ret) {
		UBOOT_ERROR("DISP_SetTxVby1DLGControl: Command transmission has failed, so the command will be aborted.\n");
		return 0;
	}

	// DISP_SetMuteControl
	UBOOT_INFO("DISP_SetMuteControl\n");
	mute_control = 0;
	ret = ldbe_boot_send_cmd_no_reply(udev, LDBE_DISP_SetMuteControl_cb, (void *)&mute_control);
	if (ret) {
		UBOOT_ERROR("DISP_SetMuteControl: Command transmission has failed, so the command will be aborted.\n");
		return 0;
	}

	// DISP_SetPixelMute
	UBOOT_INFO("DISP_SetPixelMute\n");
	param2.mute_color = 0;
	param2.pixel_mute = 0;
	param2.rgb_r = 0;
	param2.rgb_g = 0;
	param2.rgb_b = 0;
	ret = ldbe_boot_send_cmd_no_reply(udev, LDBE_DISP_SetPixelMute_cb, (void *)&param2);
	if (ret) {
		UBOOT_ERROR("DISP_SetPixelMute: Command transmission has failed, so the command will be aborted.\n");
		return 0;
	}

	UBOOT_TRACE("OUT\n");

	return 0;
}

U_BOOT_CMD(
		ldbe_boot, CONFIG_SYS_MAXARGS, 0, do_ldbe_boot,
		"LDBE boot cmd",
		NULL
		);

