/* Copyright Statement:
 *
 * This software/firmware and related documentation ("MediaTek Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
 * the prior written permission of MediaTek inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of MediaTek Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * Copyright  (C) [2020]  MediaTek Inc. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
 * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
 * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
 * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
 *
 * The following software/firmware and/or related documentation ("MediaTek
 * Software") have been modified by MediaTek Inc. All revisions are subject to
 * any receiver's applicable license agreements with MediaTek Inc.
 */

#include <common.h>
#include <command.h>
#include <time.h>
#include <debug_impl.h>
#include <utility.h>
#include <mtk_panel.h>
#include <standby_mode.h>
#include <mtk-pm.h>
#include <ledinfo.h>

int do_panel_pre_init(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
    int ret = 0;

#if (CONFIG_HAPS == 1)
    char *ptr;
    ptr = env_get("two_haps");
    if(ptr == NULL)
        return 0;
    else if(strncmp(ptr,ENV_SETTING_ON,ENV_SETTING_ON_SIZE) != 0)
        return 0;
#endif

    UBOOT_TRACE("IN\n");

#ifdef CONFIG_MTK_PANEL
    mtk_panel_init_device();
#else
    if (argc < 2)
    {
        ret = mtk_panel_init();
    }
    else
    {
        if(strncmp(argv[1], "-d", 2) == 0)
        {
            ret = mtk_panel_init();
        }
        else if (strncmp(argv[1], "-s", 2) == 0)
        {
            ;
        }
        else
        {
           cmd_usage(cmdtp);
        }
    }
#endif

    UBOOT_TRACE("OK\n");
    return ret;
}

int do_panel_init(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
    int ret = 0;
#if (CONFIG_HAPS == 1)
    char *ptr;
    ptr = env_get("two_haps");
    if(ptr == NULL)
        return 0;
    else if(strncmp(ptr,ENV_SETTING_ON,ENV_SETTING_ON_SIZE) != 0)
        return 0;
#endif

    UBOOT_TRACE("IN\n");

#ifdef CONFIG_MTK_PANEL
    mtk_panel_init_device();

    if (mtk_is_panel_ready())
    {
        UBOOT_DEBUG("do_panel_output_enable : enable\n");
        mtk_panel_enable(true);
    }
#else
    if (argc < 2)
    {
        ret = mtk_panel_init();
    }
    else
    {
        if(strncmp(argv[1], "-d", 2) == 0)
        {
            ret = mtk_panel_init();
        }
        else if (strncmp(argv[1], "-s", 2) == 0)
        {
            ;
        }
        else
        {
           cmd_usage(cmdtp);
           return ret;
        }
    }
#endif

    if(mtk_is_panel_ready())
    {
        if (pm_check_back_ground_active() == 0)
        {
            mtk_panel_backlight_on();
        }
    }

    UBOOT_TRACE("OK\n");
    return ret;
}

int do_backlight_on(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
    int ret = 0;
#if (CONFIG_HAPS == 1)
    char *ptr;
    ptr = env_get("two_haps");
    if(ptr == NULL)
        return 0;
    else if(strncmp(ptr,ENV_SETTING_ON,ENV_SETTING_ON_SIZE) != 0)
        return 0;
#endif

    UBOOT_TRACE("IN\n");

    if(mtk_is_panel_ready())
    {
        if (standby_mode_get_second_standby_mode() == STANDBY_SECOND_MODE)
        {
            char *env_secondary = NULL;
            env_secondary = env_get("secondary_do_logomusic");
            if (env_secondary == NULL)
            {
                printf("second_standby_mode skip backlight !!! \n");
                return 0;
            }
        }
        if (pm_check_back_ground_active() == 0)
        {
            mtk_panel_backlight_on();
            if(is_recovery_mode() == 1)
            {
                UBOOT_DEBUG("is_recovery_mode do led_info_check !!!! \n");
                ret = run_command("led_info_check 1", 0);
                if(ret != 0){
                    UBOOT_ERROR("led_info_check failure\n");
                }
            }
        }
        else
        {
            mtk_panel_backlight_off();
        }
    }
    else
    {
        UBOOT_ERROR("do Panel init first!!\n");
    }
    UBOOT_TRACE("OK\n");

    return ret;
}

#ifdef CONFIG_MTK_PANEL
int do_panel_output_enable(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{

    int ret = 0;
#if (CONFIG_HAPS == 1)
    char *ptr;
    ptr = env_get("two_haps");
    if(ptr == NULL)
        return 0;
    else if(strncmp(ptr,ENV_SETTING_ON,ENV_SETTING_ON_SIZE) != 0)
        return 0;
#endif

	if (pm_check_back_ground_active() == 1)
    {
        UBOOT_INFO("QHB case, do_panel_output_enable return\n");
        return 0;
    }

    UBOOT_TRACE("IN\n");
    if (mtk_is_panel_ready())
    {
        if (argc < 2)
        {
            UBOOT_ERROR("panel_enable command param error\n");
        }
        else
        {
            if (strncmp(argv[1],"1",1) == 0)
            {
                UBOOT_DEBUG("do_panel_output_enable : enable\n");
                mtk_panel_enable(true);
            }
            else if (strncmp(argv[1],"0",1) == 0)
            {
                UBOOT_DEBUG("do_panel_output_enable : disable\n");
                mtk_panel_enable(false);
            }
            else
                UBOOT_ERROR("panel_enable command param error\n");

        }
    }
    else
    {
        UBOOT_ERROR("do panel init first\n");
    }
    UBOOT_TRACE("OK\n");
    return ret;
}

int do_panel_mute(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
    int ret = 0;
#if (CONFIG_HAPS == 1)
    char *ptr;
    ptr = env_get("two_haps");
    if(ptr == NULL)
        return 0;
    else if(strncmp(ptr,ENV_SETTING_ON,ENV_SETTING_ON_SIZE) != 0)
        return 0;
#endif

	if (pm_check_back_ground_active() == 1)
    {
        UBOOT_INFO("QHB case, do_panel_mute return\n");
        return 0;
    }

    UBOOT_TRACE("IN \n");

    if (mtk_is_panel_ready())
    {
        if (argc < 2)
        {
            UBOOT_ERROR("panel_mute command param error\n");
        }
        else
        {
            if (strncmp(argv[1],"1",1) == 0)
            {
                UBOOT_DEBUG("do_panel_mute : enable\n");
                mtk_panel_mute(true);
            }
            else if (strncmp(argv[1],"0",1) == 0)
            {
                UBOOT_DEBUG("do_panel_mute : disable\n");
                mtk_panel_mute(false);
            }
            else
                UBOOT_ERROR("panel_enable command param error\n");

        }
    }
    else
    {
        UBOOT_ERROR("do panel init first\n");
    }

    UBOOT_TRACE("OK\n");
    return ret;
}

#endif
#define MAXARGS_2 2

U_BOOT_CMD(
    panel_pre_init, 2, 1, do_panel_pre_init,
    "panel_pre_init  - init panel\n",
    "command: panel_pre_init [option]\n"
    " -s : static init : panel init para from uboot\n"
);

U_BOOT_CMD(
    panel_init, 2, 1, do_panel_init,
    "panel_init  - init panel\n",
    "command: panel_init [option]\n"
    " -s : static init : panel init para from uboot\n"
);

U_BOOT_CMD(
    panel_post_init, CONFIG_SYS_MAXARGS, 0, do_backlight_on,
    "backlight on  - backlight on\n",
    "command: backlight_on\n"
);


#ifdef CONFIG_MTK_PANEL
U_BOOT_CMD(
    panel_mute, MAXARGS_2, 1, do_panel_mute,
    "panel mute  - panel mute\n",
    "command: panel_mute\n"
);


U_BOOT_CMD(
    panel_enable, 2, 1, do_panel_output_enable,
    "panel_enable - enable panel output\n",
    "command: panel_enable 1 or panel_enable 0\n"
);
#endif
