static int autok_thread_func(void *data)
{
    struct sdio_autok_thread_data *autok_thread_data;
    struct sched_param param = { .sched_priority = 99 };
    unsigned int vcore_uv = 0;
    struct msdc_host *host;
    struct mmc_host *mmc;
    char stage = 0;  
    int i,j;
    int res = 0;
    int doStg2 = 0;
    void __iomem *base;
    u32 dma;
    struct timeval t0,t1;
    int time_in_s, time_in_ms;
//    unsigned long flags;
    
    autok_thread_data = (struct sdio_autok_thread_data *)data;
    sched_setscheduler(current, SCHED_FIFO, &param);    
//    preempt_disable();
    	
  	host = autok_thread_data->host;
    mmc = host->mmc;
    stage = autok_thread_data->stage;
    base = host->base;
    dma = msdc_dma_status();
        
    // Inform msdc_set_mclk() auto-K is going to process
    sdio_autok_processed = 1;
        
    // Set clock to card max clock
    mmc_set_clock(mmc, mmc->ios.clock);
    msdc_sdio_set_long_timing_delay_by_freq(host, mmc->ios.clock);
        
    msdc_ungate_clock(host);
       
    // Set PIO mode
    msdc_dma_off();
    
    vcore_uv = autok_get_current_vcore_offset();
    // End of initialize
    do_gettimeofday(&t0); 
    if(autok_thread_data->log != NULL)
        log_info = autok_thread_data->log; 
    if(stage == 1) {
        // call stage 1 auto-K callback function
        autok_thread_data->is_autok_done[host->id] = 0;
        res = msdc_autok_stg1_cal(host, vcore_uv, autok_thread_data->p_autok_predata);
        if(res){
            printk(KERN_INFO "[%s] Auto-K stage 1 fail, res = %d, set msdc parameter settings stored in nvram to 0\n", __func__, res);
            memset(autok_thread_data->p_autok_predata->ai_data[0], 0, autok_thread_data->p_autok_predata->param_count * sizeof(unsigned int));
            autok_thread_data->is_autok_done[host->id] = 2;
        }
    } else if(stage == 2) {
        // call stage 2 auto-K callback function
        
        // check if msdc params of different volt are all 0, if so, that means auto-K stg1 failed
        for(i=0; i<autok_thread_data->p_autok_predata->vol_count; i++){
            for(j=0; j<autok_thread_data->p_autok_predata->param_count; j++){
                if(autok_thread_data->p_autok_predata->ai_data[i][j].data.sel != 0){
                    doStg2 = 1;
                    break;
                }
            }
            if(doStg2)
                break;
        }

        if(doStg2){
            res = msdc_autok_stg2_cal(host, autok_thread_data->p_autok_predata, vcore_uv);
            if(res){
                printk(KERN_INFO "[%s] Auto-K stage 2 fail, res = %d, downgrade SDIO freq to 50MHz\n", __func__, res);
                mmc->ios.clock = 50*1000*1000;
                mmc_set_clock(mmc, mmc->ios.clock);
                msdc_sdio_set_long_timing_delay_by_freq(host, mmc->ios.clock);
                sdio_autok_processed = 0;
                for (i = 0; i < HOST_MAX_NUM; i++) {
                    if (autok_thread_data->p_autok_progress[i].host_id == -1) {
                        break;    
                    } else if (autok_thread_data->p_autok_progress[i].host_id == host->id) {
                        autok_thread_data->p_autok_progress[i].fail = 1;
                    }
                }
            }
        } else {    // Auto-K stg1 failed
            printk(KERN_INFO "[%s] Auto-K stage 1 fail, downgrade SDIO freq to 50MHz\n", __func__); 
            mmc->ios.clock = 50*1000*1000;
            mmc_set_clock(mmc, mmc->ios.clock);
            msdc_sdio_set_long_timing_delay_by_freq(host, mmc->ios.clock);
            sdio_autok_processed = 0;
            for (i = 0; i < HOST_MAX_NUM; i++) {
                if (autok_thread_data->p_autok_progress[i].host_id == -1) {
                    break;    
                } else if (autok_thread_data->p_autok_progress[i].host_id == host->id) {
                    autok_thread_data->p_autok_progress[i].fail = 1;
                }
            }
        }
        
        log_info = NULL;
    } else {
        printk(KERN_INFO "[%s] stage %d doesn't support in auto-K\n", __func__, stage);
        //return -EFAULT;
    }
    do_gettimeofday(&t1);
    if(dma == DMA_ON)
        msdc_dma_on();
    
    msdc_gate_clock(host,1);
    
    // [FIXDONE] Tell native module that auto-K has finished
    if(stage == 1)
        autok_calibration_done(host->id, autok_thread_data);
    else if(stage == 2){
        for(i=0; i<HOST_MAX_NUM; i++){
            if(autok_thread_data->p_autok_progress[i].host_id == -1){
                break;    
            } else if(autok_thread_data->p_autok_progress[i].host_id == host->id){
                autok_thread_data->p_autok_progress[i].done = 1;
                if(autok_thread_data->p_autok_progress[i].done > 0)
                    complete(&autok_thread_data->autok_completion[i]);
            }
        }
    }  
    time_in_s = (t1.tv_sec - t0.tv_sec);
    time_in_ms = (t1.tv_usec - t0.tv_usec)>>10;
    printk(KERN_ERR "\n[AUTOKK][Stage%d] Timediff is %d.%d(s)\n", (int)stage, time_in_s, time_in_ms );
    
//    preempt_enable();
    return 0;
}
#endif

int send_autok_uevent(char *text, struct msdc_host *host)
{
    int err = 0;
    char *envp[3];
    char *host_buf;
    char *what_buf;
    //struct msdc_host *host = mtk_msdc_host[id];
    host_buf = kzalloc(sizeof(char)*128, GFP_KERNEL);
    what_buf = kzalloc(sizeof(char)*128, GFP_KERNEL);
    
    snprintf(host_buf, MAX_ARGS_BUF-1, "HOST=%d", host->id);
    snprintf(what_buf, MAX_ARGS_BUF-1, "WHAT=%s", text);
    envp[0] = host_buf;
    envp[1] = what_buf;
    envp[2] = NULL;
    
    if(host != NULL){
        err = kobject_uevent_env(&host->mmc->class_dev.kobj, KOBJ_CHANGE, envp);
    } 
    kfree(host_buf);
    kfree(what_buf);
    if(err < 0)
        printk(KERN_INFO "[%s] kobject_uevent_env error = %d\n", __func__, err);
    
    return err;
}   
static int autok_thread_func(void *data)
{
    struct sdio_autok_thread_data *autok_thread_data;
    //struct sched_param param = { .sched_priority = 99 };
    unsigned int vcore_uv = 0;
    struct msdc_host *host;
    struct mmc_host *mmc;
    char stage = 0;  
    int i,j;
    int res = 0;
    int doStg2 = 0;
    void __iomem *base;
    u32 dma;
    struct timeval t0,t1;
    int time_in_s, time_in_ms;
//    unsigned long flags;
    
    autok_thread_data = (struct sdio_autok_thread_data *)data;
    //sched_setscheduler(current, SCHED_FIFO, &param);    
//    preempt_disable();
    	
  	host = autok_thread_data->host;
    mmc = host->mmc;
    stage = autok_thread_data->stage;
    base = host->base;
    dma = msdc_dma_status();
        
    // Inform msdc_set_mclk() auto-K is going to process
    sdio_autok_processed = 1;
        
    // Set clock to card max clock
    mmc_set_clock(mmc, mmc->ios.clock);
    msdc_sdio_set_long_timing_delay_by_freq(host, mmc->ios.clock);
        
    msdc_ungate_clock(host);
       
    // Set PIO mode
    msdc_dma_off();
    
    vcore_uv = autok_get_current_vcore_offset();
    // End of initialize
    do_gettimeofday(&t0); 
    if(autok_thread_data->log != NULL)
        log_info = autok_thread_data->log; 
    if(stage == 1) {
        // call stage 1 auto-K callback function
        autok_thread_data->is_autok_done[host->id] = 0;
        res = msdc_autok_stg1_cal(host, vcore_uv, autok_thread_data->p_autok_predata);
        if(res){
            pr_debug("[%s] Auto-K stage 1 fail, res = %d, set msdc parameter settings stored in nvram to 0\n", __func__, res);
            memset(autok_thread_data->p_autok_predata->ai_data[0], 0, autok_thread_data->p_autok_predata->param_count * sizeof(unsigned int));
            autok_thread_data->is_autok_done[host->id] = 2;
        }
        // For Abort function
        if(atomic_read(&autok_is_abort) == 1){
            autok_thread_data->is_autok_done[host->id] = 3;
        }
    } else if(stage == 2) {
        // call stage 2 auto-K callback function
        
        // check if msdc params of different volt are all 0, if so, that means auto-K stg1 failed
        for(i=0; i<autok_thread_data->p_autok_predata->vol_count; i++){
            for(j=0; j<autok_thread_data->p_autok_predata->param_count; j++){
                if(autok_thread_data->p_autok_predata->ai_data[i][j].data.sel != 0){
                    doStg2 = 1;
                    break;
                }
            }
            if(doStg2)
                break;
        }

        if(doStg2){
            res = msdc_autok_stg2_cal(host, autok_thread_data->p_autok_predata, vcore_uv);
            if(res){
                pr_debug("[%s] Auto-K stage 2 fail, res = %d, downgrade SDIO freq to 50MHz\n", __func__, res);
                mmc->ios.clock = 50*1000*1000;
                mmc_set_clock(mmc, mmc->ios.clock);
                msdc_sdio_set_long_timing_delay_by_freq(host, mmc->ios.clock);
                sdio_autok_processed = 0;
                for (i = 0; i < HOST_MAX_NUM; i++) {
                    if (autok_thread_data->p_autok_progress[i].host_id == -1) {
                        break;    
                    } else if (autok_thread_data->p_autok_progress[i].host_id == host->id) {
                        autok_thread_data->p_autok_progress[i].fail = 1;
                    }
                }
            }
        } else {    // Auto-K stg1 failed
            pr_debug("[%s] Auto-K stage 1 fail, downgrade SDIO freq to 50MHz\n", __func__); 
            mmc->ios.clock = 50*1000*1000;
            mmc_set_clock(mmc, mmc->ios.clock);
            msdc_sdio_set_long_timing_delay_by_freq(host, mmc->ios.clock);
            sdio_autok_processed = 0;
            for (i = 0; i < HOST_MAX_NUM; i++) {
                if (autok_thread_data->p_autok_progress[i].host_id == -1) {
                    break;    
                } else if (autok_thread_data->p_autok_progress[i].host_id == host->id) {
                    autok_thread_data->p_autok_progress[i].fail = 1;
                }
            }
        }
        
        log_info = NULL;
    } else {
        pr_debug("[%s] stage %d doesn't support in auto-K\n", __func__, stage);
        //return -EFAULT;
    }
    do_gettimeofday(&t1);
    if(dma == DMA_ON)
        msdc_dma_on();
    
    msdc_gate_clock(host,1);
    
    // [FIXDONE] Tell native module that auto-K has finished
    if(stage == 1)
        autok_calibration_done(host->id, autok_thread_data);
    else if(stage == 2){
        for(i=0; i<HOST_MAX_NUM; i++){
            if(autok_thread_data->p_autok_progress[i].host_id == -1){
                break;    
            } else if(autok_thread_data->p_autok_progress[i].host_id == host->id){
                autok_thread_data->p_autok_progress[i].done = 1;
                if(autok_thread_data->p_autok_progress[i].done > 0)
                    complete(&autok_thread_data->autok_completion[i]);
            }
        }
    }  
    time_in_s = (t1.tv_sec - t0.tv_sec);
    time_in_ms = (t1.tv_usec - t0.tv_usec)>>10;
    pr_err("\n[AUTOKK][Stage%d] Timediff is %d.%d(s)\n", (int)stage, time_in_s, time_in_ms );
    
//    preempt_enable();
    return 0;
}