u8 calc_voltage_table(struct str_smart_dim *smart, const u8 *mtp)
{
    int c,i,j;
    int offset = 0;
    int offset1 = 0;	
    s16 t1,t2; 
    s16 adjust_mtp[CI_MAX][IV_MAX] = {0, };
    //u32 adjust_volt[CI_MAX][AD_IVMAX] = {0, };
    u8 range_index; 
    u8 table_index=0;

    u32 v1, v2;
    u32 ratio;
    
    const u32(*calc_volt[IV_MAX])(s16 gamma, int rgb_index, u32 adjust_volt[CI_MAX][AD_IVMAX]) = 
    {
        calc_v1_volt,
        calc_v15_volt,
        calc_v35_volt,
        calc_v59_volt,
        calc_v87_volt,
        calc_v171_volt,
        calc_v255_volt,
    };

    u8 calc_seq[6] = {IV_1, IV_171, IV_87, IV_59, IV_35, IV_15};
    u8 ad_seq[6] = {AD_IV1, AD_IV171, AD_IV87, AD_IV59, AD_IV35, AD_IV15};


	// Gamma MTP Offset Setting, Output 1, V0, V255
    for(c=CI_RED;c<CI_MAX;c++){
		//V255R -> V171R -> V87 -> V59 -> V35 -> V15 -> V1R ->  V255G -> .. -> V1G -> .. -> V1B
		offset = IV_MAX * c + (IV_MAX - IV_255); // MTP 21 Bytes, 
		offset1 = IV_TABLE_MAX * c + (IV_MAX - IV_255); // default gamma(Cmd) = 24 bytes
        t1 = s9_to_s16(mtp[offset-1]<<8|mtp[offset]);
		t2 = s9_to_s16( (smart->default_gamma[offset1-1] << 8) | (smart->default_gamma[offset1 ]) ) + t1; 

        smart->mtp[c][IV_255] = t1;
        adjust_mtp[c][IV_255] = t2;
  //for V0 All RGB Voltage Value is Reference Voltage     		
        smart->adjust_volt[c][AD_IV0] = 4608; // 4.5 * 1024 VREG in mV
        smart->adjust_volt[c][AD_IV255] = calc_volt[IV_255](t2, c, smart->adjust_volt);
    }

	// Gamma MTP Offset Setting, Output 1, V1 to V171
    for(c=CI_RED;c<CI_MAX;c++){
        for(i=IV_1;i<IV_255;i++){
			offset = IV_MAX * c + (IV_MAX - calc_seq[i]);
			offset1 = IV_TABLE_MAX * c + (IV_MAX - calc_seq[i]);

			if( i == IV_1 ) /* MTP Offset V15 ~ V255. not use for V1 */
				t1 = 0;
			else
	            t1 = (s8)mtp[offset];
			
            t2 = smart->default_gamma[offset1] + t1;
	
            smart->mtp[c][calc_seq[i]] = t1;
            adjust_mtp[c][calc_seq[i]] = t2;
            smart->adjust_volt[c][ad_seq[i]] = calc_volt[calc_seq[i]](t2, c, smart->adjust_volt);
        }
    }

	// Gamma MTP Offset Setting, Output Gray Table (smart->ve)
	for (i = 0; i < AD_IVMAX; i++) {
        for(c=CI_RED;c<CI_MAX;c++){
			smart->ve[table_index].v[c] = smart->adjust_volt[c][i];
        }
        
        range_index = 0;
		for (j = table_index+1; j < table_index+range_table_count[i]; j++) {
			for (c = CI_RED; c < CI_MAX; c++) {
				if (smart->t_info[i].offset_table != NULL)
					ratio = smart->t_info[i].offset_table[range_index] * smart->t_info[i].rv;
				else
					ratio = (range_table_count[i]-(range_index+1)) * smart->t_info[i].rv;

				v1 = smart->adjust_volt[c][i+1] << 15;
				v2 = (smart->adjust_volt[c][i] - smart->adjust_volt[c][i+1])*ratio;
				smart->ve[j].v[c] = ((v1+v2) >> 15);
			}
			range_index++;
		}
		table_index = j;
	}


#if 1 

		printk("++++++++++++++++++++++++++++++ MTP VALUE ++++++++++++++++++++++++++++++\n");
		for(i=IV_1;i<IV_MAX;i++){
			printk("V Level : %d - ",i);
		for (c = CI_RED; c < CI_MAX; c++)
				printk("  %c : 0x%08x(%04d)",color_name[c],smart->mtp[c][i],smart->mtp[c][i]);
			printk("\n");
		}
		
		printk("\n\n++++++++++++++++++++++++++++++ ADJUST VALUE ++++++++++++++++++++++++++++++\n");
		for(i=IV_1;i<IV_MAX;i++){
			printk("V Level : %d - ",i);
		for (c = CI_RED; c < CI_MAX; c++)
				printk("  %c : 0x%08x(%04d)",color_name[c],
					adjust_mtp[c][i],adjust_mtp[c][i]);
			printk("\n");
		}
	
		printk("\n\n++++++++++++++++++++++++++++++ ADJUST VOLTAGE ++++++++++++++++++++++++++++++\n");
		for(i=AD_IV0;i<AD_IVMAX;i++){
			printk("V Level : %d - ",i);
		for (c = CI_RED; c < CI_MAX; c++)
			printk("  %c : %04dV", color_name[c], smart->adjust_volt[c][i]);
			printk("\n");
		}

		printk("\n\n++++++++++++++++++++++++++++++++++++++	VOLTAGE TABLE ++++++++++++++++++++++++++++++++++++++\n");
		for(i=0;i<256;i++){
			printk("Gray Level : %03d - ",i);
		for (c = CI_RED; c < CI_MAX; c++)
			printk("  %c : %04dV", color_name[c], smart->ve[i].v[c]);
			printk("\n");
		}
#endif    

    return 0;
}
u8 calc_voltage_table(struct str_smart_dim *smart, const u8 *mtp)
{
	int c, i, j;
#if defined(MTP_REVERSE)
	int offset1 = 0;
#endif
	int offset = 0;
	s16 t1, t2;
	s16 adjust_mtp[CI_MAX][IV_MAX];
	/* u32 adjust_volt[CI_MAX][AD_IVMAX] = {0, }; */
	u8 range_index;
	u8 table_index = 0;

	u32 v1, v2;
	u32 ratio;

	u32(*calc_volt[IV_MAX])(s16 gamma, int rgb_index, u32 adjust_volt[CI_MAX][AD_IVMAX]) = {
		calc_v1_volt,
		calc_v19_volt,
		calc_v43_volt,
		calc_v87_volt,
		calc_v171_volt,
		calc_v255_volt,
	};

	u8 calc_seq[6] = {IV_1, IV_171, IV_87, IV_43, IV_19};
	u8 ad_seq[6] = {AD_IV1, AD_IV171, AD_IV87, AD_IV43, AD_IV19};

	memset(adjust_mtp, 0, sizeof(adjust_mtp));

	for (c = CI_RED; c < CI_MAX; c++) {
		offset = ((c + 1) * V255_MTP_OFFSET) + (c * 2);
		 //printk("1 offset : %d\n", offset);

		if (mtp[offset] & 0x01)
			t1 = mtp[offset + 1] * -1;
		else
			t1 = mtp[offset + 1];
		 //printk("2 t1 : %d\n", t1);

		/* start for MTP_REVERSE */
 		offset = IV_255*CI_MAX+c*2;
#if defined(MTP_REVERSE)
		offset1 = IV_255*(c+1)+(c*2);
		t1 = s9_to_s16(mtp[offset1]<<8|mtp[offset1+1]);
#else
		t1 = s9_to_s16(mtp[offset]<<8|mtp[offset+1]);
#endif
		t2 = (smart->default_gamma[offset]<<8 | smart->default_gamma[offset+1]) + t1;

		smart->mtp[c][IV_255] = t1;
		adjust_mtp[c][IV_255] = t2;
		smart->adjust_volt[c][AD_IV255] = calc_volt[IV_255](t2, c, smart->adjust_volt);

		/* for V0 All RGB Voltage Value is Reference Voltage */
		smart->adjust_volt[c][AD_IV0] = 4200;
	}

	for (c = CI_RED; c < CI_MAX; c++) {
		for (i = IV_1; i < IV_255; i++) {
#if defined(MTP_REVERSE)
			t1 = (s8)mtp[(calc_seq[i])+(c*7)]; /* IV_1, IV_171, IV_87, IV_43, IV_19 */
#else
			t1 = (s8)mtp[CI_MAX*calc_seq[i]+c];
#endif
			t2 = smart->default_gamma[CI_MAX*calc_seq[i] +c] + t1;
		/* end MTP_REVERSE */

			smart->mtp[c][calc_seq[i]] = t1;
			adjust_mtp[c][calc_seq[i]] = t2;
			smart->adjust_volt[c][ad_seq[i]] = calc_volt[calc_seq[i]](t2, c, smart->adjust_volt);
		}
	}

	for (i = 0; i < AD_IVMAX; i++) {
		for (c = CI_RED; c < CI_MAX; c++)
			smart->ve[table_index].v[c] = smart->adjust_volt[c][i];

		range_index = 0;
		for (j = table_index + 1; j < table_index + range_table_count[i]; j++) {
			for (c = CI_RED; c < CI_MAX; c++) {
				if (smart->t_info[i].offset_table != NULL)
					ratio = smart->t_info[i].offset_table[range_index] * smart->t_info[i].rv;
				else
					ratio = (range_table_count[i]-(range_index+1)) * smart->t_info[i].rv;

				v1 = smart->adjust_volt[c][i+1] << 15;
				v2 = (smart->adjust_volt[c][i] - smart->adjust_volt[c][i+1])*ratio;
				smart->ve[j].v[c] = ((v1+v2) >> 15);
			}
			range_index++;
		}
		table_index = j;
	}

#if 0
	printk(KERN_INFO "++++++++++++++++++++++++++++++ MTP VALUE ++++++++++++++++++++++++++++++\n");
	for (i = IV_1; i < IV_MAX; i++) {
		printk("V Level : %d - ", i);
		for (c = CI_RED; c < CI_MAX; c++)
			printk("  %c : 0x%08x(%04d)", color_name[c], smart->mtp[c][i], smart->mtp[c][i]);
		printk("\n");
	}

	printk(KERN_INFO "\n\n++++++++++++++++++++++++++++++ ADJUST VALUE ++++++++++++++++++++++++++++++\n");
	for (i = IV_1; i < IV_MAX; i++) {
		printk("V Level : %d - ", i);
		for (c = CI_RED; c < CI_MAX; c++)
			printk("  %c : 0x%08x(%04d)", color_name[c],
				adjust_mtp[c][i], adjust_mtp[c][i]);
		printk("\n");
	}

	printk(KERN_INFO "\n\n++++++++++++++++++++++++++++++ ADJUST VOLTAGE ++++++++++++++++++++++++++++++\n");
	for (i = AD_IV0; i < AD_IVMAX; i++) {
		printk("V Level : %d - ", i);
		for (c = CI_RED; c < CI_MAX; c++)
			printk("  %c : %04dV", color_name[c], smart->adjust_volt[c][i]);
		printk("\n");
	}

	printk(KERN_INFO "\n\n++++++++++++++++++++++++++++++++++++++  VOLTAGE TABLE ++++++++++++++++++++++++++++++++++++++\n");
	for (i = 0; i < 256; i++) {
		printk("Gray Level : %03d - ", i);
		for (c = CI_RED; c < CI_MAX; c++)
			printk("  %c : %04dV", color_name[c], smart->ve[i].v[c]);
		printk("\n");
	}
#endif
	return 0;
}
u8 calc_voltage_table(struct str_smart_dim *smart, const u8 *mtp)
{
    int c,i,j;
#if defined(MTP_REVERSE)
    int offset1 = 0;
#endif
    int offset = 0;
    s16 t1,t2; 
    u8 range_index; 
    u8 table_index=0;

    u32 v1, v2;
    u32 ratio;
    
    const u32(*calc_volt[IV_MAX])(s16 gamma, int rgb_index, u32 adjust_volt[CI_MAX][AD_IVMAX]) = 
    {
        calc_v1_volt,
        calc_v15_volt,
        calc_v35_volt,
        calc_v50_volt,
        calc_v87_volt,
        calc_v171_volt,
        calc_v255_volt,
    };

    u8 calc_seq[6] = {IV_1, IV_171, IV_87, IV_59, IV_35, IV_15};
    u8 ad_seq[6] = {AD_IV1, AD_IV171, AD_IV87, AD_IV59, AD_IV35, AD_IV15};


    for(c=CI_RED;c<CI_MAX;c++){
        offset = IV_255*CI_MAX+c*2;
#if defined(MTP_REVERSE)        
        offset1 = IV_255*(c+1)+(c*2);
        t1 = s9_to_s16(mtp[offset1]<<8|mtp[offset1+1]);
#else
        t1 = s9_to_s16(mtp[offset]<<8|mtp[offset+1]);
#endif
#if 0 
        t2 = s9_to_s16(smart->default_gamma[offset]<<8|
            smart->default_gamma[offset+1]) + t1;
#else
        t2 = (smart->default_gamma[offset]<<8 | smart->default_gamma[offset+1]) + t1;

#endif
        smart->mtp[c][IV_255] = t1;
        smart->adjust_mtp[c][IV_255] = t2;
        smart->adjust_volt[c][AD_IV255] = calc_volt[IV_255](t2, c, smart->adjust_volt);
        
  //for V0 All RGB Voltage Value is Reference Voltage      
        smart->adjust_volt[c][AD_IV0] = 4600;
    }

    for(c=CI_RED;c<CI_MAX;c++){
        for(i=IV_1;i<IV_255;i++){
#if defined(MTP_REVERSE)              
            t1 = (s8)mtp[(calc_seq[i])+(c*8)];
#else
            t1 = (s8)mtp[CI_MAX*calc_seq[i]+c];
#endif
            t2 = smart->default_gamma[CI_MAX*calc_seq[i]+c] + t1;
            
            smart->mtp[c][calc_seq[i]] = t1;
            smart->adjust_mtp[c][calc_seq[i]] = t2;
            smart->adjust_volt[c][ad_seq[i]] = calc_volt[calc_seq[i]](t2, c, smart->adjust_volt);
        }
    }
    
    for(i=0;i<AD_IVMAX;i++){
        for(c=CI_RED;c<CI_MAX;c++){
            smart->ve[table_index].v[c] = smart->adjust_volt[c][i];
        }
        
        range_index = 0;
        for(j=table_index+1;j<table_index+range_table_count[i];j++){            
            for(c=CI_RED;c<CI_MAX;c++){
                if(smart->t_info[i].offset_table != NULL) 
                    ratio = smart->t_info[i].offset_table[range_index] * smart->t_info[i].rv;
                else
                    ratio = (range_table_count[i]-(range_index+1)) * smart->t_info[i].rv;
                
                v1 = smart->adjust_volt[c][i+1] << 15;
                v2 = (smart->adjust_volt[c][i] - smart->adjust_volt[c][i+1])*ratio;
                smart->ve[j].v[c] = ((v1+v2) >> 15);
            }
            range_index++;
        }
        table_index = j;
    }

#if 1 

    printk("++++++++++++++++++++++++++++++ MTP VALUE ++++++++++++++++++++++++++++++\n");
    for(i=IV_1;i<IV_MAX;i++){
        #ifndef CONFIG_USA_MODEL_SGH_T989
        printk("V Level : %d - ",i);
        #endif
        for(c=CI_RED;c<CI_MAX;c++){
            printk("  %c : 0x%08x(%04d)",color_name[c],smart->mtp[c][i],smart->mtp[c][i]);
        }
        printk("\n");
    }
    
    printk("\n\n++++++++++++++++++++++++++++++ ADJUST VALUE ++++++++++++++++++++++++++++++\n");
    for(i=IV_1;i<IV_MAX;i++){
        #ifndef CONFIG_USA_MODEL_SGH_T989
        printk("V Level : %d - ",i);
        #endif
        for(c=CI_RED;c<CI_MAX;c++){
            printk("  %c : 0x%08x(%04d)",color_name[c],
                smart->adjust_mtp[c][i],smart->adjust_mtp[c][i]);
        }
        printk("\n");
    }


    printk("\n\n++++++++++++++++++++++++++++++ ADJUST VOLTAGE ++++++++++++++++++++++++++++++\n");
    for(i=AD_IV0;i<AD_IVMAX;i++){
        #ifndef CONFIG_USA_MODEL_SGH_T989
        printk("V Level : %d - ",i);
        #endif
        for(c=CI_RED;c<CI_MAX;c++){
            printk("  %c : %04dV",color_name[c],
                smart->adjust_volt[c][i]);
        }
        printk("\n");
    }
#if 0
    printk("\n\n++++++++++++++++++++++++++++++++++++++  VOLTAGE TABLE ++++++++++++++++++++++++++++++++++++++\n");
    for(i=0;i<256;i++){
        #ifndef CONFIG_USA_MODEL_SGH_T989
        printk("Gray Level : %03d - ",i);
        #endif
        for(c=CI_RED;c<CI_MAX;c++){
            printk("  %c : %04dV",color_name[c],
                smart->ve[i].v[c]);   
        }
        printk("\n");
    }
#endif    
#endif    
    return 0;
}