// SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
/*
 * Copyright (c) 2023 MediaTek Inc.
 */

#include <common.h>
#include <system_impl.h>
#include <debug_impl.h>
#include <utility.h>
#if defined(CONFIG_SECOND_STANDBY)
#include <standby_mode.h>
#endif
#include <smp/thread_info.h>
#include <smp/thread.h>
#include <mtk-pm.h>
#include <mtk_panel.h>
#include <mtk_bootmusic.h>
#include <mtk_standby.h>
#include <pmu_thread.h>
#include <cm4_thread.h>
#include <mtk_gegop.h>

int panel_priority = THREAD_MAX_PRIORITY;
int pmu_priority = THREAD_MAX_PRIORITY;
#if defined(CONFIG_REMOTEPROC_MTK_VAD_CORTEX_M4)
thread_t *run_cm4_thread_id = NULL;
#endif
#if defined(CONFIG_MTK_PMU)
thread_t *boot_pmu_thread_id = NULL;
#endif
thread_t *panel_pre_init_thread_id = NULL;
thread_t *panel_mute_true_thread_id = NULL;
thread_t *panel_panel_enable_thread_id = NULL;
thread_t *panel_mute_false_thread_id = NULL;
thread_t *panel_post_init_thread_id = NULL;
#if defined(CONFIG_SOUND)
thread_t *music_init_thread_id = NULL;
thread_t *music_play_thread_id = NULL;
#endif
#if defined(CONFIG_SECOND_STANDBY)
thread_t *standby_mode_thread_id = NULL;
extern int standby_mode_enter_flag;
#endif
thread_t *show_droid_logo_thread_id = NULL;

int do_after_uboot_init_task_create(void)
{
#if defined(CONFIG_MTK_PMU)
        boot_pmu_thread_id = thread_create_ex("boot_pmu", boot_pmu_thread_entry, NULL, 0, MUTLI_CORE_CPU_THREE, pmu_priority, 1);
        if (boot_pmu_thread_id == NULL )
        {
            UBOOT_ERROR("boot_pmu thread create fail...\n");
        }
#endif
    return 0;
}

int do_before_console_input_task_create(void)
{
#if defined(CONFIG_SECOND_STANDBY)
    if (standby_mode_enter_flag == -1)
    {
        standby_mode_enter_flag = standby_mode_enter_standby();
    }

    if (!standby_mode_enter_flag)
#endif
    {
        panel_pre_init_thread_id = thread_create_ex("p_pre_init", panel_pre_init_thread_entry, NULL, 0, MUTLI_CORE_CPU_TWO, panel_priority, 1);
        if (panel_pre_init_thread_id == NULL )
        {
            UBOOT_ERROR("panel_pre_init thread create fail...\n");
        }

#if defined(CONFIG_SOUND)
        music_init_thread_id = thread_create_ex("music_init", boot_music_init_thread_entry, NULL, 0, MUTLI_CORE_CPU_ONE, THREAD_DEFAULT_PRIORITY, 1);
        if (music_init_thread_id == NULL )
        {
            UBOOT_ERROR("music_init thread create fail...\n");
        }
#endif

#if defined(CONFIG_REMOTEPROC_MTK_VAD_CORTEX_M4)
        run_cm4_thread_id = thread_create_ex("boot_vad", run_cm4_thread_entry, NULL, 0, MUTLI_CORE_CPU_THREE, pmu_priority--, 1);
        if (run_cm4_thread_id == NULL )
        {
            UBOOT_ERROR("boot_vad thread create fail...\n");
        }
#endif

        panel_mute_true_thread_id = thread_create_ex("p_mute_on", panel_mute_enable_thread_entry, NULL, 0, MUTLI_CORE_CPU_TWO, panel_priority--, 1);
        if (panel_mute_true_thread_id == NULL )
        {
            UBOOT_ERROR("panel_mute thread create fail...\n");
        }

        panel_panel_enable_thread_id = thread_create_ex("p_enable", panel_enable_thread_entry, NULL, 0, MUTLI_CORE_CPU_TWO, panel_priority--, 1);
        if (panel_panel_enable_thread_id == NULL )
        {
            UBOOT_ERROR("panel_enable thread create fail...\n");
        }
    }

    return 0;
}

int do_after_console_input_task_create(void)
{
#if defined(CONFIG_SECOND_STANDBY)
    if (standby_mode_enter_flag == -1)
    {
        standby_mode_enter_flag = standby_mode_enter_standby();
    }

    if (!standby_mode_enter_flag)
#endif
    {
#if defined(CONFIG_SOUND)
        music_play_thread_id = thread_create_ex("music_play", boot_music_play_thread_entry, NULL, 0, MUTLI_CORE_CPU_ONE, THREAD_DEFAULT_PRIORITY, 1);
        if (music_play_thread_id == NULL )
        {
            UBOOT_ERROR("music_play thread create fail...\n");
        }
#endif

#ifdef CONFIG_MTK_PANEL
        panel_mute_false_thread_id = thread_create_ex("p_mute_off", panel_mute_disable_thread_entry, NULL, 0, MUTLI_CORE_CPU_TWO, panel_priority--, 1);
        if (panel_mute_false_thread_id == NULL )
        {
            UBOOT_ERROR("panel_mute thread create fail...\n");
        }
#endif
        panel_post_init_thread_id = thread_create_ex("p_post_init", panel_post_init_thread_entry, NULL, 0, MUTLI_CORE_CPU_TWO, panel_priority--, 1);
        if (panel_post_init_thread_id == NULL )
        {
            UBOOT_ERROR("panel_mute thread create fail...\n");
        }
    }

    return 0;
}

int do_before_boot_kernel_task_create(void)
{
#if defined(CONFIG_SECOND_STANDBY)
    standby_mode_thread_id = thread_create_ex("standby", standby_mode_thread_entry, NULL, 0, MUTLI_CORE_CPU_THREE, pmu_priority--, 1);
    if (standby_mode_thread_id == NULL )
    {
        UBOOT_ERROR("standby thread create fail...\n");
    }
#endif

    return 0;
}

int do_fastboot_droid_logo_task_create(void)
{

    int flag = get_panel_init_in_fastboot_mode();
    if (flag == 1)
    {
        panel_pre_init_thread_id = thread_create_ex("p_pre_init", panel_pre_init_thread_entry, NULL, 0, MUTLI_CORE_CPU_TWO, panel_priority, 1);
        if (panel_pre_init_thread_id == NULL )
        {
            UBOOT_ERROR("panel_pre_init thread create fail...\n");
        }

        panel_mute_true_thread_id = thread_create_ex("p_mute_on", panel_mute_enable_thread_entry, NULL, 0, MUTLI_CORE_CPU_TWO, panel_priority--, 1);
        if (panel_mute_true_thread_id == NULL )
        {
            UBOOT_ERROR("panel_mute thread create fail...\n");
        }

        panel_panel_enable_thread_id = thread_create_ex("p_enable", panel_enable_thread_entry, NULL, 0, MUTLI_CORE_CPU_TWO, panel_priority--, 1);
        if (panel_panel_enable_thread_id == NULL )
        {
            UBOOT_ERROR("panel_enable thread create fail...\n");
        }
    }

    show_droid_logo_thread_id = thread_create_ex("droid", show_droid_logo_thread_entry, NULL, 0, MUTLI_CORE_CPU_TWO, panel_priority--, 1);
    if (show_droid_logo_thread_id == NULL )
    {
        UBOOT_ERROR("droid logo thread create fail...\n");
    }

    if (flag == 1)
    {
#ifdef CONFIG_MTK_PANEL
        panel_mute_false_thread_id = thread_create_ex("p_mute_off", panel_mute_disable_thread_entry, NULL, 0, MUTLI_CORE_CPU_TWO, panel_priority--, 1);
        if (panel_mute_false_thread_id == NULL )
        {
            UBOOT_ERROR("panel_mute thread create fail...\n");
        }
#endif
        panel_post_init_thread_id = thread_create_ex("p_post_init", panel_post_init_thread_entry, NULL, 0, MUTLI_CORE_CPU_TWO, panel_priority--, 1);
        if (panel_post_init_thread_id == NULL )
        {
            UBOOT_ERROR("panel_mute thread create fail...\n");
        }
    }
    return 0;
}
