/*
 * Caller must make sure the spi is ready
 * */
static void amoled_set_gamma_val(int level)
{
	lcm_write_cmd(samsung_oled_gamma_table[level]);
	qspi_send_9bit(&gamma_update);

	last_level = level;
}
static int lcm_write_seq(struct spi_msg cmd_table[], unsigned size)
{
        int i;

        for (i = 0; i < size; i++) {
                lcm_write_cmd(cmd_table[i]);
        }
        return 0;
}
static int lcm_write_tb(struct lcm_cmd cmd_table[], unsigned size)
{
	int i;

	for (i = 0; i < size; i++) {
		lcm_write_cmd(cmd_table[i].reg, cmd_table[i].val);
		if (cmd_table[i].delay)
			hr_msleep(cmd_table[i].delay);
	}
	return 0;
}
static int amoled_panel_unblank(struct msm_lcdc_panel_ops *panel_data)
{
	LCMDBG("%s\n", __func__);

	wake_lock(&panel_idle_lock);

	table_sel_idx = 0;
	mutex_lock(&panel_lock);
	gamma_table_bank_select();
	amoled_set_gamma_val(last_val);
	qspi_send(0, 0xef);
	qspi_send(1, 0xd0);
	qspi_send(1, 0xe8);
	lcm_write_cmd(0x14, 0x03);
	mutex_unlock(&panel_lock);
	wake_unlock(&panel_idle_lock);

	LCMDBG("%s: last_val = %d\n", __func__,last_val);
	led_trigger_event(amoled_lcd_backlight, LED_FULL);
	return 0;
}
/*
 * Caller must make sure the spi is ready
 * */
static void amoled_set_gamma_val(int val)
{
	int i;
	int level;
	int frac;

	val = clamp(val, SAMSUNG_OLED_MIN_VAL, SAMSUNG_OLED_MAX_VAL);
	val = (val / 2) * 2;

	if (val < 31) {
		val = 20;
	}

	level = (val - SAMSUNG_OLED_MIN_VAL) / SAMSUNG_OLED_LEVEL_STEP;
/*
	LCMDBG("set brightness = (%d, %d)\n", level, val);
*/
	frac = (val - SAMSUNG_OLED_MIN_VAL) % SAMSUNG_OLED_LEVEL_STEP;

	for (i = 0; i < OLED_GAMMA_TABLE_SIZE; ++i) {
		unsigned int v1;
		unsigned int v2 = 0;
		u8 v;
		if (frac == 0) {
			v = samsung_oled_gamma_table[level][i].val;
		} else {

			v1 = samsung_oled_gamma_table[level][i].val;
			v2 = samsung_oled_gamma_table[level+1][i].val;
			v = (v1 * (SAMSUNG_OLED_LEVEL_STEP - frac) +
			     v2 * frac) / SAMSUNG_OLED_LEVEL_STEP;
		}
		lcm_write_cmd(samsung_oled_gamma_table[level][i].reg, v);
	}

	gamma_table_bank_select();

	last_val = val;
}
static int amoled_panel_unblank(struct platform_device *pdev)
{
    LCMDBG("%s\n", __func__);


    if (samsung_oled_panel_init())
        printk(KERN_ERR "samsung_oled_panel_init failed\n");

    wake_lock(&panel_idle_lock);
    mutex_lock(&panel_lock);
    table_sel_idx = 0;
    gamma_table_bank_select();
    amoled_set_gamma_val(last_val);
    qspi_send(0, 0xef);
    qspi_send(1, 0xd0);
    qspi_send(1, 0xe8);
    lcm_write_cmd(0x14, 0x03);
    mutex_unlock(&panel_lock);
    wake_unlock(&panel_idle_lock);

    LCMDBG("%s: last_val = %d\n", __func__,last_val);
    led_trigger_event(amoled_lcd_backlight, LED_FULL);
    return 0;
}
static void gamma_table_bank_select(void)
{
	lcm_write_cmd(0x39, table_sel_vals[table_sel_idx]);
	table_sel_idx ^= 1;
}