backlight: ldim: optimize ldim algorithm for flicker issue
authorEvoke Zhang <evoke.zhang@amlogic.com>
Thu, 7 Jun 2018 05:12:00 +0000 (13:12 +0800)
committerYixun Lan <yixun.lan@amlogic.com>
Fri, 6 Jul 2018 08:11:57 +0000 (01:11 -0700)
PD#167455: backlight: ldim: optimize ldim algorithm for flicker issue
also add driver and algorithm version recode.

Change-Id: I4b217f6d611c5689366170907db7e90b5a653a5e
Signed-off-by: Evoke Zhang <evoke.zhang@amlogic.com>
MAINTAINERS
drivers/amlogic/media/vout/backlight/aml_bl.c
drivers/amlogic/media/vout/backlight/aml_ldim/iw7027_bl.c
drivers/amlogic/media/vout/backlight/aml_ldim/iw7027_bl.h [deleted file]
drivers/amlogic/media/vout/backlight/aml_ldim/ldim_drv.c
drivers/amlogic/media/vout/backlight/aml_ldim/ldim_drv.h
drivers/amlogic/media/vout/backlight/aml_ldim/ldim_func.c
drivers/amlogic/media/vout/backlight/aml_ldim/ldim_func.h
include/linux/amlogic/media/vout/lcd/aml_bl.h
include/linux/amlogic/media/vout/lcd/aml_ldim.h

index 343397783c9eeee4bb3a273e5af324f3c69acf41..4e593e0c6de7c5eabf024916fd6062bf9e8594be 100644 (file)
@@ -14483,6 +14483,10 @@ AMLOGIC TXLX T962E R321 buildroot dts
 M:     liangzhuo.xie <liangzhuo.xie@amlogic.com>
 F:     arch/arm64/boot/dts/amlogic/txlx_t962e_r321_buildroot.dts
 
+AMLOGIC BACKLIGHT LOCAL DIMMING
+M:      evoke.zhang <evoke.zhang@amlogic.com>
+F:      drivers/amlogic/media/vout/backlight/aml_ldim/iw7027_bl.h
+
 AMLOGIC G12B
 M: Yan Wang <yan.wang@amlogic.com>
 F: arch/arm64/boot/dts/amlogic/mesong12b.dtsi
index 75367073f26c8ec8d4bb73b2619535bf01634d7d..45087ac31789ae6c04549b1483880c8471a247d7 100644 (file)
@@ -418,11 +418,11 @@ static int bl_pwm_out_level_check(struct bl_pwm_config_s *bl_pwm)
        return out_level;
 }
 
-static void bl_set_pwm_vs(struct bl_pwm_config_s *bl_pwm, int out_level)
+static void bl_set_pwm_vs(struct bl_pwm_config_s *bl_pwm,
+               unsigned int pol, unsigned int out_level)
 {
        unsigned int pwm_hi, n, sw;
        unsigned int vs[4], ve[4];
-       unsigned int pol = 0;
        int i;
 
        if (bl_debug_print_flag) {
@@ -441,8 +441,6 @@ static void bl_set_pwm_vs(struct bl_pwm_config_s *bl_pwm, int out_level)
                        ve[i] = 0x1fff;
                }
        } else {
-               if (bl_pwm->pwm_method == BL_PWM_NEGATIVE)
-                       pol = 1;
                bl_pwm->pwm_level =
                (((bl_pwm->pwm_cnt * bl_pwm->pwm_duty / 10) + 5) / 10);
                pwm_hi = bl_pwm->pwm_level;
@@ -450,8 +448,10 @@ static void bl_set_pwm_vs(struct bl_pwm_config_s *bl_pwm, int out_level)
                sw = (bl_pwm->pwm_cnt * 10 / n + 5) / 10;
                pwm_hi = (pwm_hi * 10 / n + 5) / 10;
                pwm_hi = (pwm_hi > 1) ? pwm_hi : 1;
-               if (bl_debug_print_flag)
-                       BLPR("n=%d, sw=%d, pwm_high=%d\n", n, sw, pwm_hi);
+               if (bl_debug_print_flag) {
+                       BLPR("pwm_vs: n=%d, sw=%d, pwm_high=%d\n",
+                               n, sw, pwm_hi);
+               }
                for (i = 0; i < n; i++) {
                        vs[i] = 1 + (sw * i);
                        ve[i] = vs[i] + pwm_hi - 1;
@@ -463,7 +463,7 @@ static void bl_set_pwm_vs(struct bl_pwm_config_s *bl_pwm, int out_level)
        }
        if (bl_debug_print_flag) {
                for (i = 0; i < 4; i++) {
-                       BLPR("vs[%d]=%d, ve[%d]=%d\n",
+                       BLPR("pwm_vs: vs[%d]=%d, ve[%d]=%d\n",
                                i, vs[i], i, ve[i]);
                }
        }
@@ -483,81 +483,67 @@ static void bl_set_pwm_vs(struct bl_pwm_config_s *bl_pwm, int out_level)
        }
 }
 
-static void bl_set_pwm(struct bl_pwm_config_s *bl_pwm)
+static void bl_set_pwm_normal(struct bl_pwm_config_s *bl_pwm,
+               unsigned int pol, unsigned int out_level)
 {
-       unsigned int port = bl_pwm->pwm_port;
-       unsigned int pol = 0;
-       unsigned int pwm_period, pwm_duty, out_level = 0xff;
-       struct pwm_device *pwm = bl_pwm->pwm_data.pwm;
+       unsigned int pwm_period, pwm_duty, port_index;
 
-       out_level = bl_pwm_out_level_check(bl_pwm);
-       if (bl_pwm->pwm_method == BL_PWM_NEGATIVE)
-               pol = 1;
+       if (IS_ERR_OR_NULL(bl_pwm->pwm_data.pwm)) {
+               BLERR("%s: invalid bl_pwm_ch\n", __func__);
+               return;
+       }
+
+       pwm_period = 1000000000 / bl_pwm->pwm_freq;
+       pwm_duty = (pwm_period * bl_pwm->pwm_duty) / 100;
+       port_index = bl_pwm->pwm_data.port_index;
        if (bl_debug_print_flag) {
-               BLPR("port %d: pwm_duty=%d, out_level=%d, pol=%s\n",
-                       port, bl_pwm->pwm_duty, out_level,
-                       (pol ? "negative":"positive"));
-       }
-
-       switch (port) {
-       case BL_PWM_A:
-       case BL_PWM_B:
-       case BL_PWM_C:
-       case BL_PWM_D:
-       case BL_PWM_E:
-       case BL_PWM_F:
-               pwm_period = 1000000000 / bl_pwm->pwm_freq;
-               pwm_duty = (pwm_period * bl_pwm->pwm_duty) / 100;
+               pr_info("pwm: pwm=0x%p, port_index=%d, meson_index=%d\n",
+               bl_pwm->pwm_data.pwm, port_index, bl_pwm->pwm_data.meson_index);
+       }
+       if (((port_index % 2) == bl_pwm->pwm_data.meson_index) &&
+               (port_index == bl_pwm->pwm_port)) {
+               bl_pwm->pwm_data.state.polarity = pol;
+               bl_pwm->pwm_data.state.duty_cycle = pwm_duty;
+               bl_pwm->pwm_data.state.period = pwm_period;
+               bl_pwm->pwm_data.state.enabled = true;
                if (bl_debug_print_flag) {
-                       pr_info("pwm=0x%p, port_index=%d, meson_index=%d\n",
-                       bl_pwm->pwm_data.pwm, bl_pwm->pwm_data.port_index,
-                       bl_pwm->pwm_data.meson_index);
-               }
-               if ((!IS_ERR_OR_NULL(bl_pwm->pwm_data.pwm)) &&
-                       ((bl_pwm->pwm_data.port_index % 2) ==
-                       bl_pwm->pwm_data.meson_index) &&
-                       (bl_pwm->pwm_data.port_index == bl_pwm->pwm_port)) {
-                       bl_pwm->pwm_data.state.polarity = pol;
-                       bl_pwm->pwm_data.state.duty_cycle = pwm_duty;
-                       bl_pwm->pwm_data.state.period = pwm_period;
-                       bl_pwm->pwm_data.state.enabled = true;
-                       if (bl_debug_print_flag) {
-                               BLPR("polarity=%d\n",
-                                       bl_pwm->pwm_data.state.polarity);
-                               BLPR("duty_cycle=%d\n",
-                                       bl_pwm->pwm_data.state.duty_cycle);
-                               BLPR("period=%d\n",
-                                       bl_pwm->pwm_data.state.period);
-                               BLPR("enabled=%d\n",
-                                       bl_pwm->pwm_data.state.enabled);
-                               }
-                       if (out_level == 0xff) {
-                               pwm_constant_disable(bl_pwm->pwm_data.meson,
-                                       bl_pwm->pwm_data.meson_index);
-                       } else {
-                               /* pwm duty 100% or 0% special control */
-                               pwm_constant_enable(bl_pwm->pwm_data.meson,
-                                       bl_pwm->pwm_data.meson_index);
-                       }
-                       pwm_apply_state(pwm, &(bl_pwm->pwm_data.state));
+                       BLPR(
+       "pwm state: polarity=%d, duty_cycle=%d, period=%d, enabled=%d\n",
+                               bl_pwm->pwm_data.state.polarity,
+                               bl_pwm->pwm_data.state.duty_cycle,
+                               bl_pwm->pwm_data.state.period,
+                               bl_pwm->pwm_data.state.enabled);
+               }
+               if (out_level == 0xff) {
+                       pwm_constant_disable(bl_pwm->pwm_data.meson,
+                               bl_pwm->pwm_data.meson_index);
                } else {
-                       BLERR("%s: invalid bl_pwm_ch\n", __func__);
+                       /* pwm duty 100% or 0% special control */
+                       pwm_constant_enable(bl_pwm->pwm_data.meson,
+                               bl_pwm->pwm_data.meson_index);
                }
-               break;
-       case BL_PWM_VS:
-               bl_set_pwm_vs(bl_pwm, out_level);
-               break;
-       default:
-               break;
+               pwm_apply_state(bl_pwm->pwm_data.pwm,
+                       &(bl_pwm->pwm_data.state));
        }
 }
 
 void bl_pwm_ctrl(struct bl_pwm_config_s *bl_pwm, int status)
 {
        struct pwm_state pstate;
+       unsigned int pol = 0, out_level = 0xff;
+
+       if (bl_pwm->pwm_method == BL_PWM_NEGATIVE)
+               pol = 1;
 
        if (status) {
                /* enable pwm */
+               out_level = bl_pwm_out_level_check(bl_pwm);
+               if (bl_debug_print_flag) {
+                       BLPR("port %d: pwm_duty=%d, out_level=%d, pol=%s\n",
+                               bl_pwm->pwm_port, bl_pwm->pwm_duty, out_level,
+                               (pol ? "negative":"positive"));
+               }
+
                switch (bl_pwm->pwm_port) {
                case BL_PWM_A:
                case BL_PWM_B:
@@ -565,8 +551,10 @@ void bl_pwm_ctrl(struct bl_pwm_config_s *bl_pwm, int status)
                case BL_PWM_D:
                case BL_PWM_E:
                case BL_PWM_F:
+                       bl_set_pwm_normal(bl_pwm, pol, out_level);
+                       break;
                case BL_PWM_VS:
-                       bl_set_pwm(bl_pwm);
+                       bl_set_pwm_vs(bl_pwm, pol, out_level);
                        break;
                default:
                        break;
@@ -580,42 +568,47 @@ void bl_pwm_ctrl(struct bl_pwm_config_s *bl_pwm, int status)
                case BL_PWM_D:
                case BL_PWM_E:
                case BL_PWM_F:
-                       if (!IS_ERR_OR_NULL(bl_pwm->pwm_data.pwm)) {
-                               pwm_get_state(bl_pwm->pwm_data.pwm, &pstate);
-                               pwm_constant_enable(bl_pwm->pwm_data.meson,
-                                       bl_pwm->pwm_data.meson_index);
-                               if (bl_pwm->pwm_method)
-                                       pstate.polarity = 0;
-                               else
-                                       pstate.polarity = 1;
-                               pstate.duty_cycle = 0;
-                               pstate.enabled = 1;
-                               pstate.period = bl_pwm->pwm_data.state.period;
-                               if (bl_debug_print_flag) {
-                                       BLPR("polarity=%d\n",
-                                               pstate.polarity);
-                                       BLPR("duty_cycle=%d\n",
-                                               pstate.duty_cycle);
-                                       BLPR("period=%d\n",
-                                               pstate.period);
-                                       BLPR("enabled=%d\n",
-                                               pstate.enabled);
-                                       }
-                               pwm_apply_state(bl_pwm->pwm_data.pwm,
-                                       &(pstate));
-                               }
+                       if (IS_ERR_OR_NULL(bl_pwm->pwm_data.pwm)) {
+                               BLERR("%s: invalid bl_pwm_ch\n", __func__);
+                               return;
+                       }
+
+                       pwm_get_state(bl_pwm->pwm_data.pwm, &pstate);
+                       pwm_constant_enable(bl_pwm->pwm_data.meson,
+                               bl_pwm->pwm_data.meson_index);
+                       if (bl_pwm->pwm_method)
+                               pstate.polarity = 0;
+                       else
+                               pstate.polarity = 1;
+                       pstate.duty_cycle = 0;
+                       pstate.enabled = 1;
+                       pstate.period = bl_pwm->pwm_data.state.period;
+                       if (bl_debug_print_flag) {
+                               BLPR(
+       "pwm state: polarity=%d, duty_cycle=%d, period=%d, enabled=%d\n",
+                                       pstate.polarity, pstate.duty_cycle,
+                                       pstate.period, pstate.enabled);
+                       }
+                       pwm_apply_state(bl_pwm->pwm_data.pwm, &(pstate));
                        break;
                case BL_PWM_VS:
-                       if (bl_pwm->pwm_method == BL_PWM_NEGATIVE)
-                               bl_set_pwm_vs(bl_pwm, 1);
-                       else
-                               bl_set_pwm_vs(bl_pwm, 0);
+                       bl_set_pwm_vs(bl_pwm, pol, 0);
                default:
                        break;
                }
        }
 }
 
+static void bl_set_pwm(struct bl_pwm_config_s *bl_pwm)
+{
+       if (bl_drv->state & BL_STATE_BL_ON) {
+               bl_pwm_ctrl(bl_pwm, 1);
+       } else {
+               if (bl_debug_print_flag)
+                       BLERR("%s: bl_drv state is off\n", __func__);
+       }
+}
+
 static void bl_power_en_ctrl(struct bl_config_s *bconf, int status)
 {
        if (status) {
@@ -946,22 +939,22 @@ static void bl_set_duty_pwm(struct bl_pwm_config_s *bl_pwm)
 
        if (bl_pwm_duty_free) {
                if (bl_pwm->pwm_duty > 100) {
-                       BLERR("pwm_duty %d%% is bigger than 100%%\n",
+                       BLERR(
+                       "pwm_duty %d%% is bigger than 100%%, reset to 100%%\n",
                                bl_pwm->pwm_duty);
                        bl_pwm->pwm_duty = 100;
-                       BLPR("reset to 100%%\n");
                }
        } else {
                if (bl_pwm->pwm_duty > bl_pwm->pwm_duty_max) {
-                       BLERR("pwm_duty %d%% is bigger than duty_max %d%%\n",
+                       BLERR(
+       "pwm_duty %d%% is bigger than duty_max %d%%, reset to duty_max\n",
                                bl_pwm->pwm_duty, bl_pwm->pwm_duty_max);
                        bl_pwm->pwm_duty = bl_pwm->pwm_duty_max;
-                       BLPR("reset to duty_max\n");
                } else if (bl_pwm->pwm_duty < bl_pwm->pwm_duty_min) {
-                       BLERR("pwm_duty %d%% is smaller than duty_min %d%%\n",
+                       BLERR(
+       "pwm_duty %d%% is smaller than duty_min %d%%, reset to duty_min\n",
                                bl_pwm->pwm_duty, bl_pwm->pwm_duty_min);
                        bl_pwm->pwm_duty = bl_pwm->pwm_duty_min;
-                       BLPR("reset to duty_min\n");
                }
        }
 
@@ -997,7 +990,7 @@ static void bl_set_level_pwm(struct bl_pwm_config_s *bl_pwm, unsigned int level)
                BLPR("port %d mapping: level=%d, level_max=%d, level_min=%d\n",
                        bl_pwm->pwm_port, level, max, min);
                BLPR("port %d: duty=%d%%, duty_max=%d%%, duty_min=%d%%\n",
-                       bl_pwm->pwm_port, pwm_max, pwm_min, bl_pwm->pwm_level);
+                       bl_pwm->pwm_port, bl_pwm->pwm_duty, pwm_max, pwm_min);
        }
 
        bl_set_pwm(bl_pwm);
index 14b24964578ccd566314bccf2899a8037b86c587..53e572e97ebb7f580112a0d276797b9eb6ad304b 100644 (file)
@@ -34,7 +34,6 @@
 #include <linux/amlogic/aml_gpio_consumer.h>
 #include <linux/amlogic/media/vout/lcd/aml_ldim.h>
 #include <linux/amlogic/media/vout/lcd/aml_bl.h>
-#include "iw7027_bl.h"
 #include "ldim_drv.h"
 #include "ldim_dev_drv.h"
 
@@ -342,8 +341,8 @@ static inline unsigned int iw7027_get_value(unsigned int level)
 
 static int iw7027_smr(unsigned short *buf, unsigned char len)
 {
-       int i, j;
-       unsigned int value_flag = 0;
+       int i;
+       unsigned int value_flag = 0, temp;
        unsigned char val[20];
        unsigned short *mapping;
        struct aml_ldim_driver_s *ldim_drv = aml_ldim_get_driver();
@@ -371,59 +370,26 @@ static int iw7027_smr(unsigned short *buf, unsigned char len)
        dim_max = ldim_drv->ldev_conf->dim_max;
        dim_min = ldim_drv->ldev_conf->dim_min;
 
-       for (i = 0; i < 10; i++)
-               value_flag = value_flag || buf[i];
-       if (value_flag == 0) {
-               for (j = 0; j < 20; j++)
-                       val[j] = 0;
-               goto iw7027_smr_end;
-       }
-
        if (bl_iw7027->test_mode) {
-               val[0] = (test_brightness[0] & 0xf00) >> 8;
-               val[1] = test_brightness[0] & 0xff;
-               val[2] = (test_brightness[1] & 0xf00) >> 8;
-               val[3] = test_brightness[1] & 0xff;
-               val[4] = (test_brightness[2] & 0xf00) >> 8;
-               val[5] = test_brightness[2] & 0xff;
-               val[6] = (test_brightness[3] & 0xf00) >> 8;
-               val[7] = test_brightness[3] & 0xff;
-               val[8] = (test_brightness[4] & 0xf00) >> 8;
-               val[9] = test_brightness[4] & 0xff;
-               val[10] = (test_brightness[5] & 0xf00) >> 8;
-               val[11] = test_brightness[5] & 0xff;
-               val[12] = (test_brightness[6] & 0xf00) >> 8;
-               val[13] = test_brightness[6] & 0xff;
-               val[14] = (test_brightness[7] & 0xf00) >> 8;
-               val[15] = test_brightness[7] & 0xff;
-               val[16] = (test_brightness[8] & 0xf00) >> 8;
-               val[17] = test_brightness[8] & 0xff;
-               val[18] = (test_brightness[9] & 0xf00) >> 8;
-               val[19] = test_brightness[9] & 0xff;
+               for (i = 0; i < 10; i++) {
+                       val[2*i] = (test_brightness[i] >> 8) & 0xf;
+                       val[2*i+1] = test_brightness[i] & 0xff;
+               }
        } else {
-               val[0] = ((iw7027_get_value(buf[mapping[0]])) & 0xf00) >> 8;
-               val[1] = (iw7027_get_value(buf[mapping[0]])) & 0xff;
-               val[2] = ((iw7027_get_value(buf[mapping[1]])) & 0xf00) >> 8;
-               val[3] = (iw7027_get_value(buf[mapping[1]])) & 0xff;
-               val[4] = ((iw7027_get_value(buf[mapping[2]])) & 0xf00) >> 8;
-               val[5] = (iw7027_get_value(buf[mapping[2]])) & 0xff;
-               val[6] = ((iw7027_get_value(buf[mapping[3]])) & 0xf00) >> 8;
-               val[7] = (iw7027_get_value(buf[mapping[3]])) & 0xff;
-               val[8] = ((iw7027_get_value(buf[mapping[4]])) & 0xf00) >> 8;
-               val[9] = (iw7027_get_value(buf[mapping[4]])) & 0xff;
-               val[10] = ((iw7027_get_value(buf[mapping[5]])) & 0xf00) >> 8;
-               val[11] = (iw7027_get_value(buf[mapping[5]])) & 0xff;
-               val[12] = ((iw7027_get_value(buf[mapping[6]])) & 0xf00) >> 8;
-               val[13] = (iw7027_get_value(buf[mapping[6]])) & 0xff;
-               val[14] = ((iw7027_get_value(buf[mapping[7]])) & 0xf00) >> 8;
-               val[15] = (iw7027_get_value(buf[mapping[7]])) & 0xff;
-               val[16] = ((iw7027_get_value(buf[mapping[8]])) & 0xf00) >> 8;
-               val[17] = (iw7027_get_value(buf[mapping[8]])) & 0xff;
-               val[18] = ((iw7027_get_value(buf[mapping[9]])) & 0xf00) >> 8;
-               val[19] = (iw7027_get_value(buf[mapping[9]])) & 0xff;
+               for (i = 0; i < 10; i++)
+                       value_flag += buf[i];
+               if (value_flag == 0) {
+                       for (i = 0; i < 20; i++)
+                               val[i] = 0;
+               } else {
+                       for (i = 0; i < 10; i++) {
+                               temp = iw7027_get_value(buf[mapping[i]]);
+                               val[2*i] = (temp >> 8) & 0xf;
+                               val[2*i+1] = temp & 0xff;
+                       }
+               }
        }
 
-iw7027_smr_end:
        iw7027_wregs(bl_iw7027->spi, 0x40, val, 20);
 
        iw7027_spi_op_flag = 0;
diff --git a/drivers/amlogic/media/vout/backlight/aml_ldim/iw7027_bl.h b/drivers/amlogic/media/vout/backlight/aml_ldim/iw7027_bl.h
deleted file mode 100644 (file)
index 2d1b4c7..0000000
+++ /dev/null
@@ -1,115 +0,0 @@
-/*
- * drivers/amlogic/media/vout/backlight/aml_ldim/iw7027_bl.h
- *
- * Copyright (C) 2017 Amlogic, Inc. All rights reserved.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
- * more details.
- *
- */
-
-#ifndef __IW7027_HW_H
-#define __IW7027_HW_H
-
-#define CHECK_INIT_DONE_MAX_COUNT      10
-
-#define BRIGHTNESS_2D                  0x7FF
-#define BRIGHTNESS_3D                  0x333
-
-#define BRIGHTNESS_2D_MAX              0xFFF
-#define BRIGHTNESS_3D_MAX              0x500
-
-/* skyworht 39" */
-#define ISET_VALUE_2D_SKY39            0xB43C
-#define ISET_VALUE_3D_SKY39            0xB4B4
-#define VDAC_VALUE_2D_SKY39            0xE5
-#define VDAC_VALUE_3D_SKY39            0x85
-
-#define VDAC_MIN_2D_SKY39              0xB0
-#define VDAC_MAX_2D_SKY39              0xF1
-#define VDAC_MIN_3D_SKY39              0x00
-#define VDAC_MAX_3D_SKY39              0x96
-
-/* skyworht 42" */
-#define ISET_VALUE_2D_SKY42            0xA537
-#define ISET_VALUE_3D_SKY42            0xA5A5
-#define VDAC_VALUE_2D_SKY42            0xE6
-#define VDAC_VALUE_3D_SKY42            0x84
-
-#define VDAC_MIN_2D_SKY42              0xB3
-#define VDAC_MAX_2D_SKY42              0xF4
-#define VDAC_MIN_3D_SKY42              0x3A
-#define VDAC_MAX_3D_SKY42              0x97
-
-/* skyworht 50" */
-#define ISET_VALUE_2D_SKY50            0xB43C
-#define ISET_VALUE_3D_SKY50            0xB4B4
-#define VDAC_VALUE_2D_SKY50            0xE1
-#define VDAC_VALUE_3D_SKY50            0x77
-
-#define VDAC_MIN_2D_SKY50              0xC0
-#define VDAC_MAX_2D_SKY50              0xF5
-#define VDAC_MIN_3D_SKY50              0x47
-#define VDAC_MAX_3D_SKY50              0xA3
-
-#define EEPROM_ADDR_VDAC_2D            3504    /* 0xDB0 */
-#define EEPROM_ADDR_VDAC_3D            3506    /* 0xDB2 */
-
-#define VSYNC_CNT_2D_3D                        64
-#define VSYNC_CNT_3D_2D                        64
-#define VSYNC_CNT_SET_BRI_ZERO         49
-#define VSYNC_CNT_RAMP                 45
-#define VSYNC_CNT_SET_BRI_2D           15
-#define VSYNC_CNT_SET_BRI_3D           15
-#define VSYNC_CNT_WAIT_EN_PROT         0
-
-/* scan timing parameters for 42"*/
-#define DEFAULT_TD0_2D         333 /* 0.333ms */
-#define DEFAULT_DG1_2D         720 /* 0.720ms */
-#define DEFAULT_DELTAT_2D      790 /* 0.790ms */
-
-#define DEFAULT_TD0_3D         333 /* 0.333ms */
-#define DEFAULT_DG1_3D         720 /* 0.720ms */
-#define DEFAULT_DELTAT_3D      790 /* 0.790ms */
-
-/* scan timing parameters for 39"*/
-#define DEFAULT_TD0_2D_SKY39           333  /* 0.333ms */
-#define DEFAULT_DG1_2D_SKY39           700  /* 0.700ms */
-#define DEFAULT_DELTAT_2D_SKY39                1104 /* 1.104ms */
-
-#define DEFAULT_TD0_3D_SKY39           333  /* 0.333ms */
-#define DEFAULT_DG1_3D_SKY39           700  /* 0.700ms */
-#define DEFAULT_DELTAT_3D_SKY39                1104 /* 1.104ms */
-
-/* scan timing parameters for 50"*/
-#define DEFAULT_TD0_2D_SKY50           333  /* 0.333ms */
-#define DEFAULT_DG1_2D_SKY50           720  /* 0.720ms */
-#define DEFAULT_DELTAT_2D_SKY50                790  /* 1.120ms */
-
-#define DEFAULT_TD0_3D_SKY50           333  /* 0.333ms */
-#define DEFAULT_DG1_3D_SKY50           720  /* 0.720ms */
-#define DEFAULT_DELTAT_3D_SKY50                790  /* 1.120ms */
-
-#define EEPROM_ADDR_PANEL      3463
-
-struct iwatt_reg_map {
-       u16 addr;
-       u16 val_2d;
-       u16 val_3d;
-};
-
-extern int  dirspi_write(struct spi_device *spi, u8 *buf, int len);
-extern int  dirspi_read(struct spi_device *spi, u8 *buf, int len);
-extern void dirspi_start(struct spi_device *spi);
-extern void dirspi_stop(struct spi_device *spi);
-#endif /* __IW7027_HW_H */
-
-
-
index ade43b132ab30414ce64d60c9ad2adb52033ff09..409848b4cd1ad9bafe23afa7f5d20db7aaaa7ada 100644 (file)
 #include <linux/amlogic/media/utils/vdec_reg.h>
 #include <linux/amlogic/media/vout/lcd/lcd_unifykey.h>
 
-
 #define AML_LDIM_DEV_NAME            "aml_ldim"
+
+#ifndef MAX
+#define MAX(a, b)   ((a > b) ? a:b)
+#endif
+#ifndef MIN
+#define MIN(a, b)   ((a < b) ? a:b)
+#endif
+
+#ifndef ABS
+#define ABS(a)   ((a < 0) ? (-a):a)
+#endif
+
+#define FRM_NUM_DBG 5
+
 const char ldim_dev_id[] = "ldim-dev";
+unsigned char ldim_debug_print;
 
 struct ldim_dev_s {
        struct cdev   cdev;
@@ -67,16 +81,35 @@ struct ldim_dev_s {
 };
 static struct ldim_dev_s ldim_dev;
 static struct ldim_param_s ldim_db_para;
-static unsigned int LDIM_DATA_FROM_DB;
-static unsigned int print_db_flag;
-static int ldim_on_flag;
-static unsigned int ldim_func_en;
-static unsigned int ldim_remap_en;
-static unsigned int ldim_func_bypass;
-
 struct LDReg nPRM;
 struct FW_DAT FDat;
 
+static unsigned int ldim_hist_row = 1;
+static unsigned int ldim_hist_col = 1;
+static unsigned int ldim_blk_row = 1;
+static unsigned int ldim_blk_col = 1;
+static unsigned int ldim_data_min = LD_DATA_MIN;
+static unsigned int ldim_brightness_level;
+
+static unsigned char ldim_on_flag;
+static unsigned char LDIM_DATA_FROM_DB, db_print_flag;
+static unsigned char ldim_func_en, ldim_remap_en;
+static unsigned char ldim_func_bypass; /* for lcd bist pattern */
+static unsigned char ldim_brightness_bypass;
+static unsigned char ldim_level_update;
+static unsigned char ldim_test_en;
+
+static spinlock_t  ldim_isr_lock;
+static spinlock_t  rdma_ldim_isr_lock;
+
+static struct workqueue_struct *ldim_queue;
+static struct work_struct ldim_on_vs_work;
+static struct work_struct ldim_off_vs_work;
+
+static unsigned int ldim_irq_cnt;
+static unsigned int rdma_ldim_irq_cnt;
+
+/* ************************************************* */
 static unsigned long val_1[16] = {512, 512, 512, 512, 546, 546,
        585, 630, 745, 819, 910, 1170, 512, 512, 512, 512};
 static unsigned long bin_1[16] = {4, 4, 4, 4, 4, 4,
@@ -86,97 +119,79 @@ static unsigned long val_2[16] = {3712, 3712, 3712, 3712, 3823, 3823,
 static unsigned long bin_2[16] = {29, 29, 29, 29, 29, 29,
        25, 22, 17, 15, 13, 28, 28, 27, 26, 25};
 
-unsigned int invalid_val_cnt;
-static unsigned int ldim_irq_cnt;
-static unsigned int rdma_ldim_irq_cnt;
-static unsigned int ldim_test_en;
-static unsigned int incr_con_en;
-static unsigned int ov_gain = 16;
-static unsigned int incr_dif_gain = 16;
-
-static spinlock_t  ldim_isr_lock;
-static spinlock_t  rdma_ldim_isr_lock;
 
-static struct workqueue_struct *ldim_queue;
-static struct work_struct ldim_on_vs_work;
-static struct work_struct ldim_off_vs_work;
+/* *************** alg parameters ******************* */
+#define LD_FW_ALG_FRM_V0     0 /* gxtvbb, txlx local dimming, 201806 */
+#define LD_FW_ALG_FRM_V1     1 /* txlx local contrast, 2017.10 round */
+#define LD_FW_ALG_FRM_SEL    LD_FW_ALG_FRM_V0
 
-#define FRM_NUM_DBG 5
+#if (LD_FW_ALG_FRM_SEL == LD_FW_ALG_FRM_V1)
 static unsigned long fw_LD_ThSF_l = 1600;
-static unsigned long fw_LD_ThTF_l = 32;
-static unsigned long  lpf_gain = 128;  /* [0~128~256], norm 128 as 1*/
-static unsigned long  lpf_res = 41;    /* 1024/9 = 113*/
-static unsigned long  rgb_base = 127;
-static unsigned long boost_gain = 2; /*256;*/
-static unsigned long alpha = 256; /*256;*/
-static unsigned long alpha_delta = 255;/* to fix flicker */
+static unsigned long fw_LD_ThTF_l = 32;/* note*/
+static unsigned long boost_gain = 2;
 static unsigned long boost_gain_neg = 3;
-static unsigned long Dbprint_lv;
-static unsigned int  db_cnt;
-static unsigned int bl_remap_curve[16] = {
-       436, 479, 551, 651, 780, 938, 1125, 1340,
-       1584, 1856, 2158, 2488, 2847, 3234, 3650, 4095
-};/*BL_matrix remap curve*/
-static unsigned long Sf_bypass, Boost_light_bypass;
-static unsigned long Lpf_bypass, Ld_remap_bypass;
-static unsigned long fw_LD_Whist[16] = {
-       32, 64, 96, 128, 160, 192, 224, 256,
-       288, 320, 352, 384, 416, 448, 480, 512
-};
-
-#define LD_DATA_MIN    10
-static unsigned int ldim_data_min;
-static unsigned int ldim_brightness_level;
-static unsigned long litgain = LD_DATA_DEPTH; /* 0xfff */
-static unsigned long avg_gain = LD_DATA_DEPTH; /* 0xfff */
-
-#ifndef MAX
-#define MAX(a, b)   ((a > b) ? a:b)
-#endif
-#ifndef MIN
-#define MIN(a, b)   ((a < b) ? a:b)
-#endif
+static unsigned long alpha_delta = 255;/* to fix flicker */
+#define LD_FW_ALG_FRM_VER    "V1 - 20180612"
 
-#ifndef ABS
-#define ABS(a)   ((a < 0) ? (-a):a)
-#endif
+#elif (LD_FW_ALG_FRM_SEL == LD_FW_ALG_FRM_V0)
+static unsigned long fw_LD_ThSF_l = 1600;
+static unsigned long fw_LD_ThTF_l = 256;
+static unsigned long boost_gain = 456; /*norm 256 to 1,T960 finally use*/
+static unsigned long TF_alpha = 256; /*256;*/
+static unsigned long lpf_gain = 128;  /* [0~128~256], norm 128 as 1*/
+#define LD_FW_ALG_FRM_VER    "V0 - 20180628"
+#endif /* LD_FW_ALG_FRM_V0 */
 
-unsigned int ldim_fw_hist_print;/*20180525*/
-module_param(ldim_fw_hist_print, uint, 0664);
-MODULE_PARM_DESC(ldim_fw_hist_print, "ldim_fw_hist_print");
+/*LPF tap: 0-lpf_res 41,1-lpf_res 114,...*/
+static unsigned long lpf_res = 14;    /* 1024/9*9 = 13,LPF_method=3 */
+static unsigned long rgb_base = 127;
 
-unsigned int ldim_fw_TF_sum_th = 32760;/*20180530*/
-module_param(ldim_fw_TF_sum_th, uint, 0664);
-MODULE_PARM_DESC(ldim_fw_TF_sum_th, "ldim_fw_TF_sum_th");
+static unsigned int ov_gain = 16;
+static unsigned int incr_dif_gain = 16;
 
-static unsigned int ldim_level_update;
-module_param(ldim_level_update, uint, 0664);
-MODULE_PARM_DESC(ldim_level_update, "ldim_level_update");
+static unsigned int LPF_method = 3;
+static unsigned int LD_TF_STEP_TH = 100;
+static unsigned int TF_step_method = 3;
+static unsigned int TF_FRESH_BL = 8;
+
+static unsigned int TF_BLK_FRESH_BL = 5;
+static unsigned int side_blk_diff_th = 100;
+static unsigned int bbd_th = 200;
+static unsigned char bbd_detect_en = 1;
+static unsigned char diff_blk_luma_en = 1;
+
+static unsigned int fw_rgb_diff_th = 32760;
+static unsigned int max_luma = 4060;
+static unsigned int lmh_avg_TH = 200;/*for woman flicker*/
+static unsigned int fw_TF_sum_th = 32760;/*20180530*/
+
+static unsigned long avg_gain = LD_DATA_MAX;
+static unsigned long litgain = LD_DATA_MAX;
+/* *************** alg parameters ******************* */
+
+/*BL_matrix remap curve*/
+static unsigned long bl_remap_curve[16] = {
+       612, 654, 721, 851, 1001, 1181, 1339, 1516,
+       1738, 1948, 2152, 2388, 2621, 2889, 3159, 3502
+};
+static unsigned long fw_LD_Whist[16] = {
+       32, 64, 96, 128, 160, 192, 224, 256,
+       288, 320, 352, 384, 416, 448, 480, 512
+};
+static unsigned char Sf_bypass, Boost_light_bypass;
+static unsigned char Lpf_bypass, Ld_remap_bypass;
+static unsigned char black_frm;
 
-unsigned int ldim_debug_print;
-module_param(ldim_debug_print, uint, 0664);
-MODULE_PARM_DESC(ldim_debug_print, "ldim_debug_print");
+static unsigned char fw_hist_print;/*20180525*/
+static unsigned char Debug;/*20180606*/
+static unsigned int db_cnt;
+static unsigned int fw_print_frequent = 8;/*20180606,print every 8 frame*/
+static unsigned int Dbprint_lv;
 
 static unsigned int ldim_hist_en;
 module_param(ldim_hist_en, uint, 0664);
 MODULE_PARM_DESC(ldim_hist_en, "ldim_hist_en");
 
-static unsigned int ldim_hist_row = 1;
-module_param(ldim_hist_row, uint, 0664);
-MODULE_PARM_DESC(ldim_hist_row, "ldim_hist_row");
-
-static unsigned int ldim_hist_col = 1;
-module_param(ldim_hist_col, uint, 0664);
-MODULE_PARM_DESC(ldim_hist_col, "ldim_hist_col");
-
-static unsigned int ldim_blk_row = 1;
-module_param(ldim_blk_row, uint, 0664);
-MODULE_PARM_DESC(ldim_blk_row, "ldim_blk_row");
-
-static unsigned int ldim_blk_col = 1;
-module_param(ldim_blk_col, uint, 0664);
-MODULE_PARM_DESC(ldim_blk_col, "ldim_blk_col");
-
 static unsigned int ldim_avg_update_en;
 module_param(ldim_avg_update_en, uint, 0664);
 MODULE_PARM_DESC(ldim_avg_update_en, "ldim_avg_update_en");
@@ -198,7 +213,7 @@ static void ldim_on_vs_brightness(void);
 static void ldim_off_vs_brightness(void);
 static void ldim_on_vs_arithmetic(void);
 static void ldim_update_setting(void);
-static void ldim_get_matrix_info_6(void);
+static void ldim_get_matrix_info_max_rgb(void);
 static void ldim_bl_remap_curve(int slop_gain);
 static void ldim_bl_remap_curve_print(void);
 static struct ldim_config_s ldim_config = {
@@ -373,13 +388,13 @@ static void ldim_bl_remap_curve_print(void)
        pr_info("bl_remap_curve:\n");
        len = 0;
        for (i = 0; i < 16; i++)
-               len += sprintf(buf+len, "\t%4d\n", bl_remap_curve[i]);
+               len += sprintf(buf+len, "\t%ld\n", bl_remap_curve[i]);
        pr_info("%s\n", buf);
 
        kfree(buf);
 }
 
-#if 0
+#if (LD_FW_ALG_FRM_SEL == LD_FW_ALG_FRM_V1)
 static void ld_fw_alg_frm_txlx(struct LDReg *nPRM1, struct FW_DAT *FDat1,
        unsigned int *max_matrix, unsigned int *hist_matrix)
 {
@@ -387,7 +402,7 @@ static void ld_fw_alg_frm_txlx(struct LDReg *nPRM1, struct FW_DAT *FDat1,
        int dif, blkRow, blkCol, k, m, n;
        unsigned long sum;
        int adpt_alp;
-       unsigned int avg, Bmin, Bmax, bl_value;
+       unsigned int avg, alpha = 256, Bmin, Bmax, bl_value;
        unsigned int Vnum  = (nPRM1->reg_LD_BLK_Vnum);
        unsigned int Hnum  = (nPRM1->reg_LD_BLK_Hnum);
        unsigned int *tBL_matrix;
@@ -424,7 +439,7 @@ static void ld_fw_alg_frm_txlx(struct LDReg *nPRM1, struct FW_DAT *FDat1,
                                nPRM1->reg_LD_pic_RowMax,
                                nPRM1->reg_LD_pic_ColMax,
                                ldim_hist_col);
-                       if (ldim_fw_hist_print) {
+                       if (fw_hist_print) {
                                for (blkRow = 0; blkRow < Vnum; blkRow++) {
                                        for (blkCol = 0; blkCol < Hnum;
                                                blkCol++) {
@@ -550,7 +565,7 @@ static void ld_fw_alg_frm_txlx(struct LDReg *nPRM1, struct FW_DAT *FDat1,
                                        = 4095;
                        if (FDat1->SF_BL_matrix[blkRow*Hnum + blkCol] < 0)
                                FDat1->SF_BL_matrix[blkRow*Hnum + blkCol]
-                                       = 0; //clip
+                                       = 0; /*clip*/
                }
        }
        for (blkRow = 0; blkRow < Vnum; blkRow++) {
@@ -565,7 +580,7 @@ static void ld_fw_alg_frm_txlx(struct LDReg *nPRM1, struct FW_DAT *FDat1,
                        pr_info("BL_boost_SF [%d * %d] info:\n",
                                Vnum, Hnum);
                        pr_info("boost_gain: %ld,%ld:\n"
-                               "luma_avg = %d, SF_sum= %d, TF_sum = %d\n [\n ",
+                               "luma_avg = %d, SF_sum= %d, TF_sum = %d\n\n ",
                                boost_gain, boost_gain_neg,
                                avg, SF_sum, TF_sum);
                        for (blkRow = 0; blkRow < Vnum; blkRow++) {
@@ -591,7 +606,7 @@ static void ld_fw_alg_frm_txlx(struct LDReg *nPRM1, struct FW_DAT *FDat1,
                                blkRow*Hnum + blkCol]));
 
                        dif_sum = ABS(SF_sum - TF_sum);
-                       if (dif_sum > ldim_fw_TF_sum_th)
+                       if (dif_sum > fw_TF_sum_th)
                                alpha = 256-alpha_delta;
                        else
                                alpha = MIN(256, fw_LD_ThTF);
@@ -632,7 +647,7 @@ static void ld_fw_alg_frm_txlx(struct LDReg *nPRM1, struct FW_DAT *FDat1,
 
        if (Dbprint_lv == 1) {
                if (pr_flag) {
-                       pr_info("BL_TF(final out)[%d*%d] info(alpha:%ld,): [\n",
+                       pr_info("BL_TF(final out)[%d*%d] info(alpha:%d,): [\n",
                                Vnum, Hnum, alpha);
                        for (blkRow = 0; blkRow < Vnum; blkRow++) {
                                for (blkCol = 0; blkCol < Hnum; blkCol++) {
@@ -665,40 +680,45 @@ static void ld_fw_alg_frm_txlx(struct LDReg *nPRM1, struct FW_DAT *FDat1,
                nPRM1->reg_BL_matrix_AVG = 1024;
        nPRM1->reg_BL_matrix_Compensate = nPRM1->reg_BL_matrix_AVG;
 }
-#endif
 
+#elif (LD_FW_ALG_FRM_SEL == LD_FW_ALG_FRM_V0)
+static unsigned long  black_time_cnt; /*black_display statue */
 static void ld_fw_alg_frm_gxtvbb(struct LDReg *nPRM1, struct FW_DAT *FDat1,
        unsigned int *max_matrix, unsigned int *hist_matrix)
 {
        /* Notes, nPRM will be set here in SW algorithm too */
-       int dif, blkRow, blkCol, k, m, n, i;
+       int dif, blkRow, blkCol, k, m, n, i, tmp;
        unsigned long sum;
-       unsigned int avg, alpha, Bmin, Bmax;
+       unsigned int avg, alpha = 256, Bmin, Bmax; /*local here*/
        unsigned int bl_value, bl_valuex128;
        static unsigned int  Map_bl_matrix[192];
        static unsigned int  Tf_bl_matrix_map[192];
+       static unsigned int  TF_bl_matrix_t[384]; /*max=16*24*/
        static unsigned int  Map_bl_matrix_Compensate;
        static unsigned int  Map_bl_matrix_AVG;
+       /*consistent with ldim hist_row*/
        unsigned int Vnum  = (nPRM1->reg_LD_BLK_Vnum);
+       /*consistent with ldim hist_col*/
        unsigned int Hnum  = (nPRM1->reg_LD_BLK_Hnum);
        unsigned int *tBL_matrix;
        unsigned int BLmax = 4096;
-       int stride = ldim_hist_col;
+       int stride = ldim_hist_col;/* inherit from ldim_hist_col */
        int RGBmax, Histmx, maxNB, curNB = 0;
        unsigned int fw_blk_num  = Vnum * Hnum;
        unsigned int fw_LD_Thist = ((fw_blk_num * 5) >> 2);
-       unsigned int fw_LD_Whist[16] = {32, 64, 96, 128, 160, 192, 224, 256,
-                               288, 320, 352, 384, 416, 448, 480, 512};
+       /*consistent with ldim picture size: ldim_pic_row * ldim_pic_col*/
        unsigned int fw_pic_size =
                        (nPRM1->reg_LD_pic_RowMax) * (nPRM1->reg_LD_pic_ColMax);
        int fw_LD_ThSF = 1600;
        unsigned int fw_LD_ThTF = 32;
-       unsigned long Debug = 0;
-       unsigned int  Tf_luma_avg_frm[FRM_NUM_DBG] = {0, 0, 0, 0, 0};
-       unsigned int  Tf_blkLuma_avg[FRM_NUM_DBG][16];
-       unsigned int  Tf_bl_matrix[FRM_NUM_DBG][16];
-       unsigned int  Tf_diff_frm_luma[FRM_NUM_DBG] = {0, 0, 0, 0, 0};
-       unsigned int  Tf_diff_blk_luma[FRM_NUM_DBG][16];
+       static unsigned int  Tf_luma_avg_frm[FRM_NUM_DBG] = {0, 0, 0, 0, 0};
+       /* note!  when Hnum,Vnum changed.
+        * make sure Hnum*Vnum<= 16 or change 16 to other num
+        */
+       static unsigned int  Tf_blkLuma_avg[FRM_NUM_DBG][16];
+       static unsigned int  Tf_bl_matrix[FRM_NUM_DBG][16];
+       static unsigned int  Tf_diff_frm_luma[FRM_NUM_DBG] = {0, 0, 0, 0, 0};
+       static unsigned int  Tf_diff_blk_luma[FRM_NUM_DBG][16];
        unsigned int fw_LD_BLEst_ACmode = 1;
        int num = 0, mm = 0, nn = 0;
        unsigned int fw_hist_mx;
@@ -712,33 +732,95 @@ static void ld_fw_alg_frm_gxtvbb(struct LDReg *nPRM1, struct FW_DAT *FDat1,
        int tvalue = 0, min_diff = 0;
        int Bmax_lpf = 0;
        int frm_rgbmax = 0, black_blk_num = 0;
+       int tdiff = 0, mx_diff = 0;
+
+       /* for debug */
+       unsigned int pr_flag = 0;
+       static unsigned int frm_cnt;
 
-       int SF_avg = 0;
-       unsigned int SF_dif = 0;
+       /*patch for fast luma change. */
+       int step = 0;
+       static unsigned int TF_bl_matrix_t2[16]; /*>Vnum*Hnum*/
+       static unsigned int TF_bl_matrix_t3[16]; /*>Vnum*Hnum*/
+       int diff_blk_matrix_tf_avg = 0;
+       int Tf_diff_frm_luma_tf_avg = 0;
+       int side_blk_diff = 0;
 
        fw_LD_ThSF = fw_LD_ThSF_l;
        fw_LD_ThTF = fw_LD_ThTF_l;
-       diff_frm_luma = 0;
-       diff_blk_luma = 0;
-       diff_blk_matrix = 0;
-       remap_value = 0;
+
        tBL_matrix = FDat1->TF_BL_matrix_2;
-       /* Luma_avg transmit */
+
+       /* first 8 frame & every 8(fw_print_frequent) frames print once*/
+       pr_flag = (frm_cnt < 8) || (frm_cnt % fw_print_frequent == 0);
+
+       if ((Dbprint_lv == 1) || (Dbprint_lv == 1024)) {
+               if (pr_flag) {
+                       pr_info("input: Vnum = [%4d],Hnum = [%4d],\n"
+                       "RowMax=[%4d],ColMax=[%4d],ldim_hist_col=[%4d]\n",
+                               Vnum, Hnum,
+                               nPRM1->reg_LD_pic_RowMax,
+                               nPRM1->reg_LD_pic_ColMax,
+                               ldim_hist_col);
+                       if (fw_hist_print) {
+                               for (blkRow = 0; blkRow < Vnum; blkRow++) {
+                                       for (blkCol = 0; blkCol < Hnum;
+                                               blkCol++) {
+                                               pr_info("(%d,%d,%d)\n ",
+                                               max_matrix[blkRow*3*stride +
+                                                       blkCol*3 + 0],
+                                               max_matrix[blkRow*3*stride +
+                                                       blkCol*3 + 1],
+                                               max_matrix[blkRow*3*stride +
+                                                       blkCol*3 + 2]);
+                                       }
+                                       pr_info(";\n");
+                               }
+                               pr_info("];\n");
+                       }
+               }
+       }
+
+       /* store 5 frames Luma_avg */
        for (i = 0; i < (FRM_NUM_DBG - 1); i++) {
                for (blkRow = 0; blkRow < Vnum; blkRow++) {
                        for (blkCol = 0; blkCol < Hnum; blkCol++) {
-                               Tf_blkLuma_avg[i][blkRow * Hnum + blkCol] =
-                               Tf_blkLuma_avg[i+1][blkRow * Hnum + blkCol];
-                               Tf_bl_matrix[i][blkRow * Hnum + blkCol] =
-                               Tf_bl_matrix[i+1][blkRow * Hnum + blkCol];
-                               Tf_diff_blk_luma[i][blkRow * Hnum + blkCol] =
-                               Tf_diff_blk_luma[i+1][blkRow * Hnum + blkCol];
+                               tmp = blkRow * Hnum + blkCol;
+                               Tf_blkLuma_avg[i][tmp] =
+                                       Tf_blkLuma_avg[i+1][tmp];
+                               Tf_bl_matrix[i][tmp] =
+                                       Tf_bl_matrix[i+1][tmp];
+                               Tf_diff_blk_luma[i][tmp] =
+                                       Tf_diff_blk_luma[i+1][tmp];
                        }
                }
                Tf_luma_avg_frm[i] = Tf_luma_avg_frm[i+1];
                Tf_diff_frm_luma[i] = Tf_diff_frm_luma[i+1];
        }
 
+       if ((Dbprint_lv == 2) || (Dbprint_lv == 1024)) {
+               if (pr_flag) {
+                       pr_info("step-pre:5 frames info:\n ");
+                       for (i = 0; i < FRM_NUM_DBG; i++) {
+                               for (blkRow = 0; blkRow < Vnum; blkRow++) {
+                                       for (blkCol = 0; blkCol < Hnum;
+                                               blkCol++) {
+                                               tmp = blkRow * Hnum + blkCol;
+                                               pr_info("Tf_blkLuma_avg[%d][%4d],Tf_bl_matrix[%d][%4d],Tf_diff_blk_luma[%d][%4d]\n",
+                                               i,
+                                               Tf_blkLuma_avg[i][tmp],
+                                               i,
+                                               Tf_bl_matrix[i][tmp],
+                                               i,
+                                               Tf_diff_blk_luma[i][tmp]);
+                                       }
+                                       pr_info(" ;\n ");
+                               }
+                               pr_info("];\n ");
+                       }
+               }
+       }
+
        frm_rgbmax = 0; black_blk_num = 0;
        for (blkRow = 0; blkRow < Vnum; blkRow++) {
                for (blkCol = 0; blkCol < Hnum; blkCol++) {
@@ -758,6 +840,7 @@ static void ld_fw_alg_frm_gxtvbb(struct LDReg *nPRM1, struct FW_DAT *FDat1,
        sum = 0; Luma_avg = 0;
        for (blkRow = 0; blkRow < Vnum; blkRow++) {
                for (blkCol = 0; blkCol < Hnum; blkCol++) {
+                       tmp = blkRow * Hnum + blkCol;
                        RGBmax = MAX(MAX(max_matrix[blkRow * 3 * stride +
                                blkCol * 3], max_matrix[blkRow * 3 * stride +
                                blkCol * 3 + 1]), max_matrix[blkRow * 3 *
@@ -766,7 +849,7 @@ static void ld_fw_alg_frm_gxtvbb(struct LDReg *nPRM1, struct FW_DAT *FDat1,
                                if (RGBmax < rgb_base)
                                        RGBmax = rgb_base;
                        }
-                       /* Consider the sitrogram */
+                       /* Consider the histogram */
                        Histmx = 0;
                        blkLuma_avg = 0;
                        blk_sum = 0;
@@ -780,72 +863,183 @@ static void ld_fw_alg_frm_gxtvbb(struct LDReg *nPRM1, struct FW_DAT *FDat1,
                                        LD_STA_BIN_NUM * stride + blkCol *
                                        LD_STA_BIN_NUM + k] * k * 32;
                        }
-                       Tf_blkLuma_avg[FRM_NUM_DBG - 1][blkRow * Hnum +
-                               blkCol] = ((blkLuma_avg + (blk_sum >> 1)) /
+                       Tf_blkLuma_avg[FRM_NUM_DBG - 1][tmp] =
+                               ((blkLuma_avg + (blk_sum >> 1)) /
                                (blk_sum + 1));
-                       Luma_avg += Tf_blkLuma_avg[FRM_NUM_DBG - 1][
-                               blkRow * Hnum + blkCol];
+                       Luma_avg += Tf_blkLuma_avg[FRM_NUM_DBG - 1][tmp];
                        fw_hist_mx = ((Histmx >> 8) * fw_LD_Thist * 2 /
                                (fw_pic_size >> 8));
                        /* further debug */
-                       tBL_matrix[blkRow * Hnum + blkCol] =
+                       tBL_matrix[tmp] =
                                ((BLmax * MIN(fw_hist_mx, RGBmax)) >> 10);
                }
        }
 
+       if ((Dbprint_lv == 3) || (Dbprint_lv == 1024)) {
+               if (pr_flag) {
+                       pr_info("step-0: BL_0[%d * %d]:\n ", Vnum, Hnum);
+                       pr_info(
+                       " LD_STA_BIN_NUM=%d, rgb_base=%ld, frm_rgbmax=%d [\n ",
+                               LD_STA_BIN_NUM,
+                               rgb_base,
+                               frm_rgbmax);
+                       for (blkRow = 0; blkRow < Vnum; blkRow++) {
+                               for (blkCol = 0; blkCol < Hnum; blkCol++) {
+                                       pr_info("(%4d)\n",
+                                       tBL_matrix[blkRow*Hnum + blkCol]);
+                               }
+                               pr_info(" ;\n ");
+                       }
+                       pr_info("];\n ");
+               }
+       }
+
        /*To solve black/white woman flicker*/
-       lmh_avg = (Luma_avg  / (Vnum * Hnum));
+       lmh_avg = (Luma_avg / (Vnum * Hnum));
        Tf_luma_avg_frm[FRM_NUM_DBG - 1] = lmh_avg;
 
        /* Spatial Filter the BackLits */
        sum = 0;
        for (blkRow = 0; blkRow < Vnum; blkRow++) {
                for (blkCol = 0; blkCol < Hnum; blkCol++) {
+                       tmp = blkRow * Hnum + blkCol;
                        maxNB = 0;
                        for (m =  -1; m < 2; m++) {
                                for (n =  -1; n < 2; n++) {
                                        if ((m == 0) && (n == 0)) {
-                                               curNB =
-                                       tBL_matrix[blkRow * Hnum + blkCol];
+                                               curNB = tBL_matrix[tmp];
                                        } else if (((blkRow + m) >= 0) &&
                                                ((blkRow + m) < Vnum) &&
                                                ((blkCol + n) >= 0) &&
                                                ((blkCol+n) < Hnum)) {
-                                               maxNB = MAX(maxNB, tBL_matrix[(
-                                                       blkRow + m) * Hnum
+                                               maxNB = MAX(maxNB,
+                                               tBL_matrix[(blkRow + m) * Hnum
                                                        + blkCol + n]);
                                        }
                                }
                        }
                        /* SF matrix */
                        if (Sf_bypass == 1) {
-                               FDat1->SF_BL_matrix[blkRow * Hnum + blkCol] =
-                               tBL_matrix[blkRow * Hnum + blkCol];
+                               FDat1->SF_BL_matrix[tmp] = tBL_matrix[tmp];
                        } else {
-                               FDat1->SF_BL_matrix[blkRow * Hnum + blkCol] =
-                               MAX(curNB, (maxNB - fw_LD_ThSF));
+                               FDat1->SF_BL_matrix[tmp] =
+                                       MAX(curNB, (maxNB - fw_LD_ThSF));
                        }
-                       sum += FDat1->SF_BL_matrix[blkRow * Hnum + blkCol];
+                       sum += FDat1->SF_BL_matrix[tmp];
                        /* for SF_BL_matrix average calculation */
                }
        }
 
+       if ((Dbprint_lv == 4) || (Dbprint_lv == 1024)) {
+               if (pr_flag) {
+                       pr_info("step-1: BL_SF:\n ");
+                       pr_info("Sf_bypass=%d, fw_LD_ThSF=%d, sum=%ld [\n ",
+                               Sf_bypass,
+                               fw_LD_ThSF,
+                               sum);
+                       for (blkRow = 0; blkRow < Vnum; blkRow++) {
+                               for (blkCol = 0; blkCol < Hnum; blkCol++) {
+                                       pr_info("(%4d)\n",
+                                               FDat1->SF_BL_matrix[
+                                               blkRow * Hnum + blkCol]);
+                               }
+                               pr_info(" ;\n ");
+                       }
+                       pr_info("];\n ");
+               }
+       }
+
+       /*side black bar detect*/
+       avg = sum / fw_blk_num;
+       for (blkRow = 0; blkRow < Vnum; blkRow++) {
+               for (blkCol = 0; blkCol < Hnum; blkCol++) {
+                       tmp = blkRow * Hnum + blkCol;
+                       dif = avg - (FDat1->SF_BL_matrix[tmp]);
+                       dif = ABS(dif);
+
+                       if ((blkCol == 0) &&
+                               (dif > bbd_th) &&
+                               bbd_detect_en) {
+                               /*1-0*/
+                               side_blk_diff =
+                                       FDat1->SF_BL_matrix[tmp + 1] -
+                                       FDat1->SF_BL_matrix[tmp];
+                               if (side_blk_diff > side_blk_diff_th) {
+                                       FDat1->SF_BL_matrix[tmp] =
+                                               FDat1->SF_BL_matrix[tmp + 1] -
+                                               side_blk_diff_th; /*8-9*/
+                               }
+                       }
+                       if ((blkCol == (Hnum - 1)) &&
+                               (dif > bbd_th) &&
+                               bbd_detect_en) {
+                               side_blk_diff = FDat1->SF_BL_matrix[tmp - 1] -
+                                       FDat1->SF_BL_matrix[tmp];
+                               if (side_blk_diff > side_blk_diff_th) {
+                                       FDat1->SF_BL_matrix[tmp] =
+                                               FDat1->SF_BL_matrix[tmp - 1] -
+                                               side_blk_diff_th;
+                               }
+                       }
+               }
+       }
+
+       if ((Dbprint_lv == 13) || (Dbprint_lv == 1024)) {
+               if (pr_flag) {
+                       pr_info("step-2.5: BL_SF_bbd:\n ");
+                       pr_info(
+                       " bbd_detect_en=%d, bbd_th=%d, side_blk_diff=%d,avg=%d\n ",
+                               bbd_detect_en,
+                               bbd_th,
+                               side_blk_diff,
+                               avg);
+                       for (blkRow = 0; blkRow < Vnum; blkRow++) {
+                               for (blkCol = 0; blkCol < Hnum; blkCol++) {
+                                       pr_info("(%4d)\n",
+                                       FDat1->SF_BL_matrix[
+                                               blkRow * Hnum + blkCol]);
+                               }
+                               pr_info(";\n ");
+                       }
+                       pr_info(" ];\n ");
+               }
+       }
+
        /* boost the bright region lights a little bit. */
        avg = ((sum * 7 / fw_blk_num) >> 3);
        for (blkRow = 0; blkRow < Vnum; blkRow++) {
                for (blkCol = 0; blkCol < Hnum; blkCol++) {
-                       dif = (FDat1->SF_BL_matrix[blkRow * Hnum + blkCol]
-                               - avg);
+                       tmp = blkRow * Hnum + blkCol;
+                       dif = (FDat1->SF_BL_matrix[tmp] - avg);
 
-                       FDat1->SF_BL_matrix[blkRow * Hnum + blkCol] += 0;
+                       FDat1->SF_BL_matrix[tmp] += 0;
                        if (Boost_light_bypass == 0) {
-                               FDat1->SF_BL_matrix[blkRow * Hnum + blkCol] =
-                                       (FDat1->SF_BL_matrix[blkRow * Hnum +
-                                       blkCol] * boost_gain + 64) >> 7;
+                               FDat1->SF_BL_matrix[tmp] =
+                                       (FDat1->SF_BL_matrix[tmp] *
+                                       boost_gain + 64) >> 7;
                        }
-                       if (FDat1->SF_BL_matrix[blkRow * Hnum + blkCol] > 4095)
-                               FDat1->SF_BL_matrix[blkRow * Hnum + blkCol]
-                                       = 4095;
+                       if (FDat1->SF_BL_matrix[tmp] > 4095)
+                               FDat1->SF_BL_matrix[tmp] = 4095;
+               }
+       }
+
+       if ((Dbprint_lv == 5) || (Dbprint_lv == 1024)) {
+               if (pr_flag) {
+                       pr_info("step-2: BL_SF_boost:\n ");
+                       pr_info(
+                       " Boost_light_bypass=%d, boost_gain=%ld, avg=%d [\n ",
+                               Boost_light_bypass,
+                               boost_gain,
+                               avg);
+                       for (blkRow = 0; blkRow < Vnum; blkRow++) {
+                               for (blkCol = 0; blkCol < Hnum; blkCol++) {
+                                       pr_info("(%4d)\n",
+                                       FDat1->SF_BL_matrix[
+                                               blkRow * Hnum + blkCol]);
+                               }
+                               pr_info(";\n ");
+                       }
+                       pr_info(" ];\n ");
                }
        }
 
@@ -858,101 +1052,150 @@ static void ld_fw_alg_frm_gxtvbb(struct LDReg *nPRM1, struct FW_DAT *FDat1,
                }
        }
 
-       SF_avg = SF_sum / fw_blk_num;
-       for (blkRow = 0; blkRow < Vnum; blkRow++) {
-               for (blkCol = 0; blkCol < Hnum; blkCol++) {
-                       if (Debug == 1) {
-                               SF_dif = FDat1->SF_BL_matrix[blkRow * Hnum
-                                       + blkCol] - SF_avg;
-                               if (FDat1->SF_BL_matrix[blkRow * Hnum
-                                       + blkCol] <= SF_avg) {
-                                       FDat1->SF_BL_matrix[blkRow * Hnum +
-                                               blkCol] = ((SF_avg + 64) >> 7);
-                               } else {
-                                       /* need optimize */
-                                       FDat1->SF_BL_matrix[blkRow * Hnum +
-                                               blkCol] = ((SF_avg + 64) >> 7)
-                                               + SF_dif;
-                               }
-                               if (FDat1->SF_BL_matrix[
-                                       blkRow * Hnum + blkCol] >
-                                        4095) {
-                                       FDat1->SF_BL_matrix[blkRow * Hnum +
-                                               blkCol] = 4095;
-                               }
-                       }
-               }
-       }
-
        /* LPF  Only for Xiaomi 8 Led ,here Vnum = 1;*/
        Bmin = 4096; Bmax_lpf = 0;
        for (blkRow = 0; blkRow < Vnum; blkRow++) {
                for (blkCol = 0; blkCol < Hnum; blkCol++) {
+                       tmp = blkRow * Hnum + blkCol;
                        sum = 0;
-                       for (m = -2; m < 3; m++) {
-                               for (n = -2; n < 3; n++) {
-                                       /* be careful here */
-                                       mm = MIN(MAX(blkRow + m, 0),
-                                               Vnum-1);
-                                       nn = MIN(MAX(blkCol + n, 0),
-                                               Hnum-1);
-                                       num = MIN(MAX((mm * Hnum + nn),
-                                               0), (Vnum * Hnum - 1));
-                                       sum += FDat1->SF_BL_matrix[num];
+                       if (LPF_method == 0) {/* 5*5 lpf_res=1024/25=41 */
+                               for (m = -2; m < 3; m++) {
+                                       for (n = -2; n < 3; n++) {
+                                               /* be careful here */
+                                               mm = MIN(MAX(blkRow + m, 0),
+                                                       Vnum-1);
+                                               nn = MIN(MAX(blkCol + n, 0),
+                                                       Hnum-1);
+                                               num = MIN(MAX((mm * Hnum + nn),
+                                                       0), (Vnum * Hnum - 1));
+                                               sum += FDat1->SF_BL_matrix[num];
+                                       }
+                               }
+                       } else if (LPF_method == 1) {/*3*3,lpf_res=1024/9=114*/
+                               for (m = -1; m < 2; m++) {
+                                       for (n = -1; n < 2; n++) {
+                                               /* be careful here */
+                                               mm = MIN(MAX(blkRow + m, 0),
+                                                       Vnum-1);
+                                               nn = MIN(MAX(blkCol + n, 0),
+                                                       Hnum-1);
+                                               num = MIN(MAX((mm * Hnum + nn),
+                                                       0),
+                                                       (Vnum * Hnum - 1));
+                                               sum += FDat1->SF_BL_matrix[num];
+                                       }
+                               }
+                       } else if (LPF_method == 2) {/*7*7,lpf_res=1024/49=21*/
+                               for (m = -3; m < 4; m++) {
+                                       for (n = -3; n < 4; n++) {
+                                               /* be careful here */
+                                               mm = MIN(MAX(blkRow + m, 0),
+                                                       Vnum-1);
+                                               nn = MIN(MAX(blkCol + n, 0),
+                                                       Hnum-1);
+                                               num = MIN(MAX((mm * Hnum + nn),
+                                                       0),
+                                                       (Vnum * Hnum - 1));
+                                               sum += FDat1->SF_BL_matrix[num];
+                                       }
+                               }
+                       } else if (LPF_method == 3) {/*9*9,lpf_res=1024/81=13*/
+                               for (m = -4; m < 5; m++) {
+                                       for (n = -4; n < 5; n++) {
+                                               /* be careful here */
+                                               mm = MIN(MAX(blkRow + m, 0),
+                                                       Vnum-1);
+                                               nn = MIN(MAX(blkCol + n, 0),
+                                                       Hnum-1);
+                                               num = MIN(MAX((mm * Hnum + nn),
+                                                       0),
+                                                       (Vnum * Hnum - 1));
+                                               sum += FDat1->SF_BL_matrix[num];
+                                       }
                                }
                        }
-                       if (Lpf_bypass == 1) {
-                               FDat1->SF_BL_matrix[blkRow * Hnum +
-                                       blkCol] = FDat1->SF_BL_matrix[
-                                       blkRow * Hnum + blkCol];
-                       } else {
-                               FDat1->SF_BL_matrix[blkRow * Hnum +
-                                       blkCol] = (((sum * lpf_res
-                                       >> 10) * lpf_gain) >> 7);
+
+                       if (Lpf_bypass == 0) {
+                               FDat1->SF_BL_matrix[tmp] =
+                               (((sum * lpf_res >> 10) * lpf_gain) >> 7);
                        }
 
-                       tvalue =
-                       FDat1->SF_BL_matrix[blkRow * Hnum + blkCol];
+                       /*check if sum overflow*/
+                       if (Dbprint_lv == 6)
+                               pr_info("step 3.1: sum=%ld:\n ", sum);
+                       tvalue = FDat1->SF_BL_matrix[tmp];
                        tvalue = (tvalue * ov_gain + 16) >> 4;
-                       min_diff = tvalue -
-                       FDat1->SF_BL_matrix[blkRow * Hnum + blkCol];
+                       min_diff = tvalue - FDat1->SF_BL_matrix[tmp];
                        Bmin = MIN(Bmin, min_diff);
 
                        Bmax_lpf = MAX(Bmax_lpf,
-                               Tf_bl_matrix[FRM_NUM_DBG-2]
-                               [blkRow * Hnum + blkCol]);
+                               Tf_bl_matrix[FRM_NUM_DBG-2][tmp]);
+
+                       if (FDat1->SF_BL_matrix[tmp] > 4095)
+                               FDat1->SF_BL_matrix[tmp] = 4095;
+               }
+       }
 
-                       if (FDat1->SF_BL_matrix[blkRow * Hnum + blkCol]
-                               > 4095) {
-                               FDat1->SF_BL_matrix[blkRow * Hnum +
-                                       blkCol] = 4095;
+       if ((Dbprint_lv == 6) || (Dbprint_lv == 1024)) {
+               if (pr_flag) {
+                       pr_info("step 3.1: BL_SF_lpf:\n ");
+                       pr_info(" LPF_method=%d, Lpf_bypass=%d, lpf_res=%ld, lpf_gain=%ld\n",
+                               LPF_method,
+                               Lpf_bypass,
+                               lpf_res,
+                               lpf_gain);
+                       for (blkRow = 0; blkRow < Vnum; blkRow++) {
+                               for (blkCol = 0; blkCol < Hnum; blkCol++) {
+                                       pr_info("(%4d)\n",
+                                       FDat1->SF_BL_matrix[
+                                               blkRow * Hnum + blkCol]);
+                               }
+                               pr_info(";\n ");
                        }
+                       pr_info("];\n ");
+                       pr_info("Bmax_lpf=%d, Bmin=%d\n ", Bmax_lpf, Bmin);
                }
        }
 
-       if (incr_con_en == 1) {
+
+       /*hivicast00082*/
+       if (lmh_avg > lmh_avg_TH) {
                for (blkRow = 0; blkRow < Vnum; blkRow++) {
                        for (blkCol = 0; blkCol < Hnum; blkCol++) {
-                               tvalue = FDat1->SF_BL_matrix[blkRow * Hnum +
-                                       blkCol];
-                               tvalue = (tvalue * ov_gain + 16) >> 4;
-                               tvalue = tvalue - ((Bmin * incr_dif_gain +
-                                       8) >> 4);
-                               /* incr_dif_gain [1~16]*/
-                               FDat1->SF_BL_matrix[blkRow * Hnum + blkCol]
-                                       = tvalue;
-                               if (FDat1->SF_BL_matrix[blkRow * Hnum +
-                                       blkCol] > 4095)
-                                       FDat1->SF_BL_matrix[blkRow * Hnum +
-                                       blkCol] = 4095;
+                               tmp = blkRow * Hnum + blkCol;
+                               tdiff = max_luma - FDat1->SF_BL_matrix[tmp];
+                               mx_diff = MAX(tdiff, 0);
+
+                               FDat1->SF_BL_matrix[tmp] =
+                                       FDat1->SF_BL_matrix[tmp] +
+                                       ((mx_diff + 1) >> 1);
                        }
                }
        }
+       if ((Dbprint_lv == 7) || (Dbprint_lv == 1024)) {
+               if (pr_flag) {
+                       pr_info("step-3.2:BL_SF(hivicast00082:\n ");
+                       pr_info(" max_luma=%d, mx_diff=%d,lmh_avg_TH=%d\n ",
+                               max_luma,
+                               mx_diff,
+                               lmh_avg_TH);
+                       for (blkRow = 0; blkRow < Vnum; blkRow++) {
+                               for (blkCol = 0; blkCol < Hnum; blkCol++) {
+                                       pr_info("(%4d)\n",
+                                       FDat1->SF_BL_matrix[
+                                               blkRow * Hnum + blkCol]);
+                               }
+                               pr_info(";\n ");
+                       }
+                       pr_info("];\n ");
+               }
+       }
 
        /* Temperary filter */
        sum = 0; Bmin = 4096; Bmax = 0;
        for (blkRow = 0; blkRow < Vnum; blkRow++) {
                for (blkCol = 0; blkCol < Hnum; blkCol++) {
+                       tmp = blkRow * Hnum + blkCol;
                        /* Optimization needed here */
                        dif_r = FDat1->last_STA1_MaxRGB[blkRow * 3 * stride
                                + blkCol * 3] - max_matrix[blkRow * 3 * stride
@@ -966,30 +1209,38 @@ static void ld_fw_alg_frm_gxtvbb(struct LDReg *nPRM1, struct FW_DAT *FDat1,
                                + blkCol * 3 + 2] - max_matrix[blkRow * 3
                                * stride + blkCol * 3 + 2];
                        dif_b = ABS(dif_b);
-                       adpt_alp = (FDat1->SF_BL_matrix[blkRow * Hnum
-                               + blkCol]) - (FDat1->TF_BL_matrix[blkRow
-                               * Hnum + blkCol]);
+                       adpt_alp = (FDat1->SF_BL_matrix[tmp]) -
+                               (FDat1->TF_BL_matrix[tmp]);
                        adpt_alp = ABS(adpt_alp);
 
                        dif_sum = SF_sum - TF_sum;
                        dif_sum = ABS(dif_sum);
-                       if (dif_sum > 32760)
-                               alpha = 256;
+                       /*note: T968&T960 only use current BL_matrix:
+                        *TF_alpha =256,fw_LD_ThTF=256
+                        *20180612
+                        */
+                       /*note: manual set TF_alpha no useful anymore*/
+                       TF_alpha = 1 << TF_FRESH_BL;
+                       if (dif_sum > fw_rgb_diff_th) /* 32760 */
+                               alpha = TF_alpha; /* 256 */
                        else
-                               alpha = MIN(256, fw_LD_ThTF);
+                               alpha = MIN(TF_alpha, fw_LD_ThTF);
                        bl_valuex128 = 0;
 
-                       FDat1->TF_BL_alpha[blkRow * Hnum + blkCol] = alpha;
+                       FDat1->TF_BL_alpha[tmp] = alpha;
 
-                       /* get the temporary filtered BL_value */
-                       bl_value = (((256 - alpha) * (FDat1->TF_BL_matrix[
-                               blkRow * Hnum + blkCol]) + alpha
-                               * (FDat1->SF_BL_matrix[blkRow * Hnum
-                               + blkCol]) + 128) >> 8);
+                       /*4.1: TF: get the temporary filtered BL_value */
+                       bl_value = (((TF_alpha - alpha) *
+                               (FDat1->TF_BL_matrix[tmp]) +
+                               alpha * (FDat1->SF_BL_matrix[tmp]) +
+                               (TF_alpha >> 1)) >> TF_FRESH_BL);
+                               /*2^8 = TF_alpha*/
                        if (bl_value > 4095)
                                bl_value = 4095;
+                       /* for debug */
+                       TF_bl_matrix_t[tmp] = bl_value;
 
-                       /*ld_curve map*/
+                       /*4.2: remap: ld_curve map*/
                        int_x = bl_value >> 8;
                        rmd_x = bl_value % 256;
                        norm  = 256;
@@ -1009,63 +1260,89 @@ static void ld_fw_alg_frm_gxtvbb(struct LDReg *nPRM1, struct FW_DAT *FDat1,
                        bl_value_map = (bl_value_map > 4095) ? 4095 :
                                bl_value_map;
 
-                       Tf_bl_matrix_map[blkRow * Hnum + blkCol] = bl_value_map;
+                       Tf_bl_matrix_map[tmp] = bl_value_map;
 
+                       /*4.3 tiir optimize */
                        diff_frm_luma = Tf_luma_avg_frm[FRM_NUM_DBG - 1] -
                                Tf_luma_avg_frm[FRM_NUM_DBG - 2];
                        diff_frm_luma = ABS(diff_frm_luma);
                        Tf_diff_frm_luma[FRM_NUM_DBG - 1] = diff_frm_luma;
                        if (Tf_luma_avg_frm[FRM_NUM_DBG - 1] <= 1) {
-                               Tf_bl_matrix[FRM_NUM_DBG - 2][blkRow*Hnum +
-                                                       blkCol] = Bmax_lpf>>1;
+                               /* Tf_bl_matrix[FRM_NUM_DBG - 2][tmp] =
+                                *      Bmax_lpf>>1;
+                                */
+                               Tf_bl_matrix[FRM_NUM_DBG - 2][tmp] =
+                                       Tf_bl_matrix[FRM_NUM_DBG - 2][tmp];
+                               /* do nothing. need to optimise */
                        }
                        if (diff_frm_luma < 10) {/*same video scene*/
-                               diff_blk_luma = Tf_blkLuma_avg[FRM_NUM_DBG
-                                       - 1][blkRow * Hnum + blkCol] -
-                                       Tf_blkLuma_avg[FRM_NUM_DBG - 2][blkRow
-                                       * Hnum + blkCol];
+                               diff_blk_luma =
+                                       Tf_blkLuma_avg[FRM_NUM_DBG - 1][tmp] -
+                                       Tf_blkLuma_avg[FRM_NUM_DBG - 2][tmp];
                                diff_blk_matrix = bl_value_map -
-                                       Tf_bl_matrix[FRM_NUM_DBG-2][blkRow
-                                       * Hnum + blkCol];
+                                       Tf_bl_matrix[FRM_NUM_DBG-2][tmp];
                                diff_blk_matrix = ABS(diff_blk_matrix);
-                               Tf_diff_blk_luma[FRM_NUM_DBG - 1][blkRow
-                                       * Hnum + blkCol] = diff_blk_matrix;
+                               Tf_diff_blk_luma[FRM_NUM_DBG - 1][tmp] =
+                                       diff_blk_matrix;
 
                                /*Debug print local value*/
-                               if (Dbprint_lv == 1) {
+                               if (Dbprint_lv == 200) {
                                        if ((db_cnt % 4) == 0) {
                                                /*4 frames print once*/
                                                pr_info(
-                                                       "diff_blk_matrix[%4d]\n"
-                                                       "bl_value_map[%4d]\n"
-                                                       "Tf_bl_matrix[%4d]\n"
-                                                       "frm_luma[%4d]\n\n",
-                                                       diff_blk_matrix,
-                                                       bl_value_map,
-                                                       Tf_bl_matrix[
-                                                               FRM_NUM_DBG - 2]
-                                                               [blkRow * Hnum +
-                                                               blkCol],
-                                                       Tf_luma_avg_frm[
-                                                               FRM_NUM_DBG -
-                                                               1]);
+                                               "##4.3_tiir-1: diff_frm_luma <10\n"
+                                               "diff_blk_matrix[%4d]\n"
+                                               "bl_value_map[%4d]\n"
+                                               "Tf_bl_matrix[%4d]\n",
+                                               diff_blk_matrix,
+                                               bl_value_map,
+                                               Tf_bl_matrix[FRM_NUM_DBG - 2]
+                                                       [tmp]);
                                        }
                                }
-                               if ((diff_blk_matrix > 50)) {
-                                       if (bl_value_map >= Tf_bl_matrix[
-                                               FRM_NUM_DBG-2][
-                                               blkRow*Hnum + blkCol]) {
-                                               bl_value_map = Tf_bl_matrix[
-                                                       FRM_NUM_DBG-2][
-                                                       blkRow*Hnum + blkCol] +
-                                                       ((diff_blk_matrix +
-                                                       16) >> 5);
-                                       } else {
-                                               bl_value_map = Tf_bl_matrix[
-                                                       FRM_NUM_DBG-2][
-                                                       blkRow*Hnum + blkCol] -
-                                                       ((diff_blk_matrix +
-                                                       16) >> 5);
+                               if ((diff_blk_matrix > 50) &&
+                                       (diff_blk_luma_en)) {
+                                       if (bl_value_map >=
+                                       Tf_bl_matrix[FRM_NUM_DBG-2][tmp]) {
+                                               bl_value_map =
+                                               Tf_bl_matrix[FRM_NUM_DBG-2]
+                                                       [tmp] +
+                                               ((diff_blk_matrix + 16) >> 5);
+                                       } else { /* any difference ? */
+                                               bl_value_map =
+                                               Tf_bl_matrix[FRM_NUM_DBG-2]
+                                                       [tmp] -
+                                               ((diff_blk_matrix + 16) >> 5);
+                                       }
+                                       if (Dbprint_lv == 2) {
+                                               /* just for confirm */
+                                               pr_info("##--2: diff_blk_matrix >50\n");
+                                       }
+
+                                       if ((Dbprint_lv == 200) &&
+                                               ((db_cnt % 4) == 0)) {
+                                               /*4 frames print once*/
+                                               pr_info(
+                                               "##4.3_tiir-2: diff_blk_matrix > 50\n"
+                                               "bl_value_map= [%4d]\n"
+                                               "blkRow * Hnum + blkCol = %d(cnt)\n"
+                                               "Tf_bl_matrix[FRM_NUM_DBG-1][cnt])= [%4d]\n"
+                                               "Tf_bl_matrix[FRM_NUM_DBG-2][cnt])= [%4d]\n"
+                                               "Tf_bl_matrix[FRM_NUM_DBG-3][cnt])= [%4d]\n"
+                                               "Tf_bl_matrix[FRM_NUM_DBG-4][cnt])= [%4d]\n"
+                                               "Tf_bl_matrix[FRM_NUM_DBG-5][cnt])= [%4d]\n",
+                                               bl_value_map,
+                                               tmp,
+                                               Tf_bl_matrix[FRM_NUM_DBG - 1]
+                                                       [tmp],
+                                               Tf_bl_matrix[FRM_NUM_DBG - 2]
+                                                       [tmp],
+                                               Tf_bl_matrix[FRM_NUM_DBG - 3]
+                                                       [tmp],
+                                               Tf_bl_matrix[FRM_NUM_DBG - 4]
+                                                       [tmp],
+                                               Tf_bl_matrix[FRM_NUM_DBG - 5]
+                                                       [tmp]);
                                        }
                                } else
                                        bl_value_map = bl_value_map;
@@ -1076,32 +1353,123 @@ static void ld_fw_alg_frm_gxtvbb(struct LDReg *nPRM1, struct FW_DAT *FDat1,
                                        bl_value_map = 0;
                        }
 
-                       /*Debug print local value*/
-                       if (Dbprint_lv == 1) {
-                               if ((db_cnt%4) == 0) {  /*4 frames print once*/
-                                       pr_info("Aftert bl_value_map[%4d]\n",
-                                               bl_value_map);
-                               }
+                       /* for debug */
+                       TF_bl_matrix_t3[tmp] = bl_value_map;
+
+                       /*4.4 patch for fast luma change.20180621*/
+                       diff_frm_luma = Tf_luma_avg_frm[FRM_NUM_DBG - 1] -
+                               Tf_luma_avg_frm[FRM_NUM_DBG - 2];/*0~512*/
+                       /*current*/
+                       Tf_diff_frm_luma[FRM_NUM_DBG - 1] = diff_frm_luma;
+                       Tf_diff_frm_luma_tf_avg =
+                               (Tf_diff_frm_luma[FRM_NUM_DBG - 1] +
+                               Tf_diff_frm_luma[FRM_NUM_DBG - 2] +
+                               Tf_diff_frm_luma[FRM_NUM_DBG - 3] +
+                               Tf_diff_frm_luma[FRM_NUM_DBG - 4]) * 2;
+                       /*(/4*8,12bit)4 frames avg. note: FRM_NUM_DBG >=4*/
+
+                       diff_blk_matrix = bl_value_map -
+                               Tf_bl_matrix[FRM_NUM_DBG-2][tmp];
+                       diff_blk_matrix_tf_avg =
+                               (Tf_diff_blk_luma[FRM_NUM_DBG - 2][tmp] +
+                               Tf_diff_blk_luma[FRM_NUM_DBG - 3][tmp] +
+                               Tf_diff_blk_luma[FRM_NUM_DBG - 4][tmp] +
+                               Tf_diff_blk_luma[FRM_NUM_DBG - 5][tmp]) >> 2;
+                       /*make sure FRM_NUM_DBG > 5 */
+
+                       if ((dif_sum < fw_rgb_diff_th) &&
+                               (ABS(diff_frm_luma*8) > LD_TF_STEP_TH)) {
+
+                               if (TF_step_method == 1) {
+                                       step = diff_frm_luma * 8;/*12bit*/
+                               /* dark --> bright, limit increase step,u12*/
+                                       if (step > LD_TF_STEP_TH)
+                                               step = LD_TF_STEP_TH;/*12bit*/
+                               /*bright --> dark, limit decrease step,u12*/
+                                       else if (step < (-LD_TF_STEP_TH))
+                                               step = -(LD_TF_STEP_TH);
+                                       bl_value_map =
+                                       (FDat1->TF_BL_matrix[tmp]) + step;
+                               } else if (TF_step_method == 2) {
+                                       step =
+                                       (Tf_diff_frm_luma[FRM_NUM_DBG - 1] +
+                                       Tf_diff_frm_luma[FRM_NUM_DBG - 2] +
+                                       Tf_diff_frm_luma[FRM_NUM_DBG - 3] +
+                                       Tf_diff_frm_luma[FRM_NUM_DBG - 4]) * 2;
+                       /*(/4*8,12bit)4 frames avg. note: FRM_NUM_DBG >=4*/
+
+                               /* dark --> bright, limit increase step,u12*/
+                                       if (step > LD_TF_STEP_TH)
+                                               step = LD_TF_STEP_TH ;/*12bit*/
+                               /*bright --> dark, limit decrease step,u12*/
+                                       else if (step < (-LD_TF_STEP_TH))
+                                               step = -(LD_TF_STEP_TH);
+
+                                       bl_value_map =
+                                       (FDat1->TF_BL_matrix[tmp]) + step;
+                               } else if (TF_step_method == 3) {
+                                       diff_blk_matrix = bl_value_map -
+                                       Tf_bl_matrix[FRM_NUM_DBG-2][tmp];
+
+                                       if (diff_blk_matrix > 0) {
+                                               bl_value_map =
+                                               Tf_bl_matrix[FRM_NUM_DBG-2][tmp]
+                                               + ((diff_blk_matrix +
+                                               (1 << (TF_BLK_FRESH_BL-1))) >>
+                                                       TF_BLK_FRESH_BL);
+                                       } else {
+                                               bl_value_map =
+                                               Tf_bl_matrix[FRM_NUM_DBG-2][tmp]
+                                               - ((ABS(diff_blk_matrix) +
+                                               (1 << (TF_BLK_FRESH_BL-1))) >>
+                                                       TF_BLK_FRESH_BL);
+                                       }
+                               } else
+                                       bl_value_map = bl_value_map;
                        }
 
+                       if (Dbprint_lv == 100 && pr_flag) {
+                               pr_info("new TF filter:row[%d],col[%d]:dif_sum=%d(th(%d))\n",
+                                       blkRow, blkCol, dif_sum,
+                                       fw_rgb_diff_th);
+                               pr_info("diff_frm_luma:%4d,step:%4d, LD_TF_STEP_TH:%4d (pre_bl[%4d] => bl[%4d])\n\n",
+                                       diff_frm_luma,
+                                       step, LD_TF_STEP_TH,
+                                       (FDat1->TF_BL_matrix[tmp]),
+                                       bl_value_map);
+                       }
+
+                       if (Dbprint_lv == 100 && pr_flag &&
+                               (TF_step_method == 3)) {
+                               pr_info("new TF filter:row[%d],col[%d]:dif_sum=%d(th(%d))\n",
+                                       blkRow, blkCol, dif_sum,
+                                       fw_rgb_diff_th);
+                               pr_info("diff_frm_luma:%4d,diff_blk_matrix:%4d, TF_BLK_FRESH_BL:%4d (pre_bl[%4d] => bl[%4d])\n\n",
+                                       diff_frm_luma,
+                                       diff_blk_matrix, TF_BLK_FRESH_BL,
+                                       Tf_bl_matrix[FRM_NUM_DBG-2][tmp],
+                                       bl_value_map);
+                       }
+
+                       /* debug */
+                       TF_bl_matrix_t2[tmp] = bl_value_map;
+
                        remap_value = bl_value_map;
                        if (remap_value > 4095)
                                remap_value = 4095;
                        if (nPRM1->reg_LD_BackLit_mode == 1) {
-                               nPRM1->BL_matrix[blkCol * Vnum + blkRow]  =
-                                                               bl_value_map;
-                           Map_bl_matrix[blkCol * Vnum + blkRow] = remap_value;
+                               nPRM1->BL_matrix[blkCol * Vnum + blkRow] =
+                                       bl_value_map;
+                               Map_bl_matrix[blkCol * Vnum + blkRow] =
+                                       remap_value;
                        } else {
-                               nPRM1->BL_matrix[blkRow * Hnum + blkCol]  =
-                                                               bl_value_map;
-                           Map_bl_matrix[blkRow * Hnum + blkCol] = remap_value;
+                               nPRM1->BL_matrix[tmp] = bl_value_map;
+                               Map_bl_matrix[tmp] = remap_value;
                        }
 
                        /* Get the TF_BL_matrix */
-                       FDat1->TF_BL_matrix[blkRow * Hnum + blkCol]
-                               = bl_value_map;
-                       Tf_bl_matrix[FRM_NUM_DBG-1][blkRow * Hnum + blkCol]
-                               = bl_value_map;
+                       FDat1->TF_BL_matrix[tmp] = bl_value_map;
+                       Tf_bl_matrix[FRM_NUM_DBG-1][tmp] = bl_value_map;
 
                        /* leave the Delayed version for next frame */
                        for (k = 0; k < 3; k++) {
@@ -1116,43 +1484,147 @@ static void ld_fw_alg_frm_gxtvbb(struct LDReg *nPRM1, struct FW_DAT *FDat1,
                        Bmax = MAX(Bmax, bl_value_map);
                }
        }
-       /*Debug print local value*/
-       if (Dbprint_lv == 1) {
-               if ((db_cnt%4) == 0) {  /*5 frames print once*/
-                       for (i = 0; i < 8; i++)
-                               pr_info("Tf_bl_matrix[blk_%d][%4d]\n", i,
-                                       Tf_bl_matrix[FRM_NUM_DBG-1][i]);
+
+       if ((Dbprint_lv == 8) || (Dbprint_lv == 1024)) {
+               if (pr_flag) {
+                       pr_info("step-4.1: BL_TF: [\n ");
+                       pr_info("alpha =%d, dif_sum =%d\n ", alpha, dif_sum);
+                       for (blkRow = 0; blkRow < Vnum; blkRow++) {
+                               for (blkCol = 0; blkCol < Hnum; blkCol++) {
+                                       pr_info("(%4d)\n",
+                                       TF_bl_matrix_t[blkRow * Hnum + blkCol]);
+                               }
+                               pr_info(";\n ");
+                       }
+                       pr_info("];\n");
                }
        }
-       db_cnt++;
-       if (db_cnt > 4095)
-               db_cnt = 0;
-
-       /* set the DC reduction for the BL_modeling */
-       if (fw_LD_BLEst_ACmode == 0)
-               nPRM1->reg_BL_matrix_AVG = 0;
-       else if (fw_LD_BLEst_ACmode == 1) {
-               /*nPRM->reg_BL_matrix_AVG = (sum/fw_blk_num);*/
-               nPRM1->reg_BL_matrix_AVG = ((sum / fw_blk_num) *
-                       avg_gain + 2048) >> 12;
-               Map_bl_matrix_AVG = ((sum / fw_blk_num) *
-                       avg_gain + 2048) >> 12;
-               Map_bl_matrix_Compensate = ((sum / fw_blk_num) *
-                       avg_gain + 2048) >> 12;
-       } else if (fw_LD_BLEst_ACmode == 2)
-               nPRM1->reg_BL_matrix_AVG = Bmin;
-       else if (fw_LD_BLEst_ACmode == 3)
-               nPRM1->reg_BL_matrix_AVG = Bmax;
-       else if (fw_LD_BLEst_ACmode == 4)
-               nPRM1->reg_BL_matrix_AVG = 2048;
-       else
-               nPRM1->reg_BL_matrix_AVG = 1024;
-
-       nPRM1->reg_BL_matrix_Compensate = nPRM1->reg_BL_matrix_AVG;
-}
 
-void LDIM_WR_BASE_LUT_DRT(int base, int *pData, int len)
-{
+       if ((Dbprint_lv == 9) || (Dbprint_lv == 1024)) {
+               if (pr_flag) {
+                       pr_info("step-4.2: BL_remap: [\n ");
+                       pr_info("Ld_remap_bypass =%d\n ", Ld_remap_bypass);
+                       for (blkRow = 0; blkRow < Vnum; blkRow++) {
+                               for (blkCol = 0; blkCol < Hnum; blkCol++) {
+                                       pr_info("(%4d)\n",
+                                       Tf_bl_matrix_map[
+                                               blkRow * Hnum + blkCol]);
+                               }
+                               pr_info(";\n ");
+                       }
+                       pr_info("];\n ");
+               }
+       }
+
+       if ((Dbprint_lv == 10) || (Dbprint_lv == 1024)) {
+               if (pr_flag) {
+                       pr_info("step-4.3: BL_remap_patch_old: [\n ");
+                       pr_info(" diff_frm_luma=%d,\n ", diff_frm_luma);
+                       for (blkRow = 0; blkRow < Vnum; blkRow++) {
+                               for (blkCol = 0; blkCol < Hnum; blkCol++) {
+                                       pr_info("(%4d)\n",
+                                       TF_bl_matrix_t3[
+                                               blkRow * Hnum + blkCol]);
+                               }
+                               pr_info(";\n ");
+                       }
+                       pr_info("];\n ");
+               }
+       }
+
+       if ((Dbprint_lv == 11) || (Dbprint_lv == 1024)) {
+               if (pr_flag) {
+                       pr_info("step-4.4: BL_remap_patch_new: [\n ");
+                       pr_info(" diff_frm_luma=%d, dif_sum=%d, LD_TF_STEP_TH=%d, TF_step_method=%d\n ",
+                               diff_frm_luma,
+                               dif_sum,
+                               LD_TF_STEP_TH,
+                               TF_step_method);
+                       for (blkRow = 0; blkRow < Vnum; blkRow++) {
+                               for (blkCol = 0; blkCol < Hnum; blkCol++) {
+                                       pr_info("(%4d)\n",
+                                               TF_bl_matrix_t2
+                                               [blkRow * Hnum + blkCol]);
+                               }
+                               pr_info(";\n ");
+                       }
+                       pr_info("];\n ");
+               }
+       }
+       if ((Dbprint_lv == 12) || (Dbprint_lv == 1024)) {
+               if (pr_flag) {
+                       pr_info("step 5: BL_TF_optimise(final BL_matrix):\n ");
+                       pr_info("frm_print_cnt = %d, diff_frm_luma =%d, diff_blk_matrix =%d\n ",
+                               fw_print_frequent,
+                               diff_frm_luma,
+                               diff_blk_matrix);
+                       for (blkRow = 0; blkRow < Vnum; blkRow++) {
+                               for (blkCol = 0; blkCol < Hnum; blkCol++) {
+                                       pr_info("(%4d)\n",
+                                       nPRM1->BL_matrix[
+                                               blkRow * Hnum + blkCol]);
+                               }
+                               pr_info(";\n ");
+                       }
+                       pr_info("];\n ");
+               }
+       }
+
+       db_cnt++;
+       if (db_cnt > 4095)
+               db_cnt = 0;
+
+       frm_cnt++;
+       if (frm_cnt > 4095)
+               frm_cnt = 0;
+
+       /* set the DC reduction for the BL_modeling */
+       if (fw_LD_BLEst_ACmode == 0)
+               nPRM1->reg_BL_matrix_AVG = 0;
+       else if (fw_LD_BLEst_ACmode == 1) {
+               /*nPRM->reg_BL_matrix_AVG = (sum/fw_blk_num);*/
+               nPRM1->reg_BL_matrix_AVG = ((sum / fw_blk_num) *
+                       avg_gain + 2048) >> 12;
+               Map_bl_matrix_AVG = ((sum / fw_blk_num) *
+                       avg_gain + 2048) >> 12;
+               Map_bl_matrix_Compensate = ((sum / fw_blk_num) *
+                       avg_gain + 2048) >> 12;
+       } else if (fw_LD_BLEst_ACmode == 2)
+               nPRM1->reg_BL_matrix_AVG = Bmin;
+       else if (fw_LD_BLEst_ACmode == 3)
+               nPRM1->reg_BL_matrix_AVG = Bmax;
+       else if (fw_LD_BLEst_ACmode == 4)
+               nPRM1->reg_BL_matrix_AVG = 2048;
+       else
+               nPRM1->reg_BL_matrix_AVG = 1024;
+
+       nPRM1->reg_BL_matrix_Compensate = nPRM1->reg_BL_matrix_AVG;
+
+       /*black_display statue */
+       if (Hnum == 10) {
+               if (frm_rgbmax < 10) {
+                       black_time_cnt++;
+                       if (black_time_cnt > 120)
+                               black_frm = 1;
+               } else {
+                       black_time_cnt = 0;
+                       black_frm = 0;
+                       ldim_level_update = 1;
+               }
+       } else {
+               if (frm_rgbmax < 10) {
+                       black_frm = 1;
+               } else {
+                       black_frm = 0;
+                       ldim_level_update = 1;
+               }
+       }
+       /*note: black_frm should co-work with brightness controlling(spi)*/
+}
+#endif
+
+void LDIM_WR_BASE_LUT_DRT(int base, int *pData, int len)
+{
        int i;
        int addr;
 
@@ -1175,6 +1647,8 @@ static void ldim_stts_initial(unsigned int pic_h, unsigned int pic_v,
        unsigned int row_start, col_start;
        struct aml_bl_drv_s *bl_drv = aml_bl_get_driver();
 
+       BLK_Vnum = (BLK_Vnum == 0) ? 1 : BLK_Vnum;
+       BLK_Hnum = (BLK_Hnum == 0) ? 1 : BLK_Hnum;
        LDIMPR("%s: %d %d %d %d\n", __func__, pic_h, pic_v, BLK_Vnum, BLK_Hnum);
 
        resolution = (((pic_h - 1) & 0xffff) << 16) | ((pic_v - 1) & 0xffff);
@@ -1184,7 +1658,7 @@ static void ldim_stts_initial(unsigned int pic_h, unsigned int pic_v,
        case BL_CHIP_TXLX:
                Wr(LDIM_STTS_CTRL0, 7 << 2);
                ldim_set_matrix_ycbcr2rgb();
-               ldim_stts_en(resolution, 0, 0, 3, 1, 1, 0);
+               ldim_stts_en(resolution, 0, 0, 1, 1, 1, 0);
                break;
        case BL_CHIP_GXTVBB:
                Wr(LDIM_STTS_CTRL0, 3<<3);/*4 mux to vpp_dout*/
@@ -1206,6 +1680,124 @@ static void ldim_stts_initial(unsigned int pic_h, unsigned int pic_v,
                row_start, col_start, BLK_Hnum);
 }
 
+static void ldim_db_para_print(struct LDReg *mLDReg)
+{
+       int i, len;
+       char *buf;
+
+       len = 32 * 10 + 10;
+       buf = kcalloc(len, sizeof(char), GFP_KERNEL);
+       if (buf == NULL) {
+               LDIMERR("print buf malloc error\n");
+               return;
+       }
+
+       LDIMPR("rgb_base = %ld\n"
+               "boost_gain = %ld\n"
+               "lpf_res = %ld\n"
+               "fw_LD_ThSF = %ld\n\n",
+               rgb_base,
+               boost_gain,
+               lpf_res,
+               fw_LD_ThSF_l);
+
+       LDIMPR("ld_vgain = %d\n"
+               "ld_hgain = %d\n"
+               "ld_litgain = %d\n\n",
+               mLDReg->reg_LD_Vgain,
+               mLDReg->reg_LD_Hgain,
+               mLDReg->reg_LD_Litgain);
+
+       LDIMPR("ld_lut_vdg_lext = %d\n"
+               "ld_lut_hdg_lext = %d\n"
+               "ld_lut_vhk_lext = %d\n\n",
+               mLDReg->reg_LD_LUT_Vdg_LEXT,
+               mLDReg->reg_LD_LUT_Hdg_LEXT,
+               mLDReg->reg_LD_LUT_VHk_LEXT);
+
+       len = 0;
+       LDIMPR("ld_lut_hdg:\n");
+       for (i = 0; i < 32; i++) {
+               len += sprintf(buf+len, "\t%d",
+                       mLDReg->reg_LD_LUT_Hdg[i]);
+               if (i % 8 == 7)
+                       len += sprintf(buf+len, "\n");
+       }
+       pr_info("%s\n", buf);
+
+       len = 0;
+       LDIMPR("ld_lut_vdg:\n");
+       for (i = 0; i < 32; i++) {
+               len += sprintf(buf+len, "\t%d",
+                       mLDReg->reg_LD_LUT_Vdg[i]);
+               if (i % 8 == 7)
+                       len += sprintf(buf+len, "\n");
+       }
+       pr_info("%s\n", buf);
+
+       len = 0;
+       LDIMPR("ld_lut_vhk:\n");
+       for (i = 0; i < 32; i++) {
+               len += sprintf(buf+len, "\t%d",
+                       mLDReg->reg_LD_LUT_VHk[i]);
+               if (i % 8 == 7)
+                       len += sprintf(buf+len, "\n");
+       }
+       pr_info("%s\n", buf);
+
+       len = 0;
+       LDIMPR("ld_lut_vhk_pos:\n");
+       for (i = 0; i < 32; i++) {
+               len += sprintf(buf+len, "\t%d",
+                       mLDReg->reg_LD_LUT_VHk_pos[i]);
+               if (i % 8 == 7)
+                       len += sprintf(buf+len, "\n");
+       }
+       pr_info("%s\n", buf);
+
+       len = 0;
+       LDIMPR("ld_lut_vhk_neg:\n");
+       for (i = 0; i < 32; i++) {
+               len += sprintf(buf+len, "\t%d",
+                       mLDReg->reg_LD_LUT_VHk_neg[i]);
+               if (i % 8 == 7)
+                       len += sprintf(buf+len, "\n");
+       }
+       pr_info("%s\n", buf);
+
+       len = 0;
+       LDIMPR("ld_lut_hhk:\n");
+       for (i = 0; i < 32; i++) {
+               len += sprintf(buf+len, "\t%d",
+                       mLDReg->reg_LD_LUT_HHk[i]);
+               if (i % 8 == 7)
+                       len += sprintf(buf+len, "\n");
+       }
+       pr_info("%s\n", buf);
+
+       len = 0;
+       LDIMPR("ld_lut_vho_pos:\n");
+       for (i = 0; i < 32; i++) {
+               len += sprintf(buf+len, "\t%d",
+                       mLDReg->reg_LD_LUT_VHo_pos[i]);
+               if (i % 8 == 7)
+                       len += sprintf(buf+len, "\n");
+       }
+       pr_info("%s\n", buf);
+
+       len = 0;
+       LDIMPR("ld_lut_vho_neg:\n");
+       for (i = 0; i < 32; i++) {
+               len += sprintf(buf+len, "\t%d",
+                       mLDReg->reg_LD_LUT_VHo_neg[i]);
+               if (i % 8 == 7)
+                       len += sprintf(buf+len, "\n");
+       }
+       pr_info("%s\n", buf);
+
+       kfree(buf);
+}
+
 static void ldim_db_load_update(struct LDReg *mLDReg)
 {
        int i;
@@ -1245,68 +1837,15 @@ static void ldim_db_load_update(struct LDReg *mLDReg)
                                ldim_db_para.ld_lut_vho_pos[i];
                        mLDReg->reg_LD_LUT_VHo_neg[i] =
                                ldim_db_para.ld_lut_vho_neg[i];
-                       }
+               }
 
                /* remapping */
                /*ldim_db_para.lit_idx_th;*/
                /*ldim_db_para.comp_gain;*/
        }
 
-       if (print_db_flag == 1) {
-               LDIMPR("rgb_base = %ld\n", rgb_base);
-               LDIMPR("\n");
-               LDIMPR("boost_gain = %ld\n", boost_gain);
-               LDIMPR("\n");
-               LDIMPR("lpf_res = %ld\n", lpf_res);
-               LDIMPR("\n");
-               LDIMPR("fw_LD_ThSF = %ld\n", fw_LD_ThSF_l);
-               LDIMPR("\n");
-
-               LDIMPR("ld_vgain = %d\n", mLDReg->reg_LD_Vgain);
-               LDIMPR("\n");
-               LDIMPR("ld_hgain = %d\n", mLDReg->reg_LD_Hgain);
-               LDIMPR("\n");
-               LDIMPR("ld_litgain = %d\n", mLDReg->reg_LD_Litgain);
-               LDIMPR("\n");
-
-               LDIMPR("ld_lut_vdg_lext = %d\n", mLDReg->reg_LD_LUT_Vdg_LEXT);
-               LDIMPR("\n");
-               LDIMPR("ld_lut_hdg_lext = %d\n", mLDReg->reg_LD_LUT_Hdg_LEXT);
-               LDIMPR("\n");
-               LDIMPR("ld_lut_vhk_lext = %d\n", mLDReg->reg_LD_LUT_VHk_LEXT);
-               LDIMPR("\n");
-
-               for (i = 0; i < 32; i++)
-                       LDIMPR("ld_lut_hdg = %d\n", mLDReg->reg_LD_LUT_Hdg[i]);
-               LDIMPR("\n");
-               for (i = 0; i < 32; i++)
-                       LDIMPR("ld_lut_vdg = %d\n", mLDReg->reg_LD_LUT_Vdg[i]);
-               LDIMPR("\n");
-               for (i = 0; i < 32; i++)
-                       LDIMPR("ld_lut_vhk = %d\n", mLDReg->reg_LD_LUT_VHk[i]);
-               LDIMPR("\n");
-
-               for (i = 0; i < 32; i++)
-                       LDIMPR("ld_lut_vhk_pos = %d\n",
-                               mLDReg->reg_LD_LUT_VHk_pos[i]);
-               LDIMPR("\n");
-               for (i = 0; i < 32; i++)
-                       LDIMPR("ld_lut_vhk_neg = %d\n",
-                               mLDReg->reg_LD_LUT_VHk_neg[i]);
-               LDIMPR("\n");
-               for (i = 0; i < 32; i++)
-                       LDIMPR("ld_lut_hhk = %d\n",
-                               mLDReg->reg_LD_LUT_HHk[i]);
-               LDIMPR("\n");
-               for (i = 0; i < 32; i++)
-                       LDIMPR("ld_lut_vho_pos = %d\n",
-                               mLDReg->reg_LD_LUT_VHo_pos[i]);
-               LDIMPR("\n");
-               for (i = 0; i < 32; i++)
-                       LDIMPR("ld_lut_vho_neg = %d\n",
-                               mLDReg->reg_LD_LUT_VHo_neg[i]);
-               LDIMPR("\n");
-       }
+       if (db_print_flag == 1)
+               ldim_db_para_print(mLDReg);
 }
 
 static void LDIM_Initial_GXTVBB(unsigned int ldim_bl_en,
@@ -1456,7 +1995,6 @@ static void LDIM_Initial_GXTVBB(unsigned int ldim_bl_en,
        /* ldim_param_misc_ctrl0; */
        LDIM_WR_32Bits(REG_LD_MISC_CTRL0, data);
        kfree(arrayTmp);
-
 }
 
 static int LDIM_Update_Matrix(int NewBlMatrix[], int BlMatrixNum)
@@ -1671,7 +2209,6 @@ static void LDIM_Initial_TXLX(unsigned int ldim_bl_en,
        LDIM_Update_Matrix(nPRM.BL_matrix, 16 * 24);
 
        kfree(arrayTmp);
-
 }
 
 static void LDIM_Initial(unsigned int pic_h, unsigned int pic_v,
@@ -1709,7 +2246,6 @@ static void LDIM_Initial(unsigned int pic_h, unsigned int pic_v,
        default:
                break;
        }
-
 }
 
 static int ldim_open(struct inode *inode, struct file *file)
@@ -1764,10 +2300,10 @@ static long ldim_compat_ioctl(struct file *file, unsigned int cmd,
 #endif
 
 static const struct file_operations ldim_fops = {
-       .owner   = THIS_MODULE,
-       .open    = ldim_open,
-       .release = ldim_release,
-       .unlocked_ioctl   = ldim_ioctl,
+       .owner          = THIS_MODULE,
+       .open           = ldim_open,
+       .release        = ldim_release,
+       .unlocked_ioctl = ldim_ioctl,
 #ifdef CONFIG_COMPAT
        .compat_ioctl = ldim_compat_ioctl,
 #endif
@@ -1818,6 +2354,7 @@ static void ldim_dump_histgram(void)
 static irqreturn_t rdma_ldim_intr(int irq, void *dev_id)
 {
        ulong flags;
+
        /*LDIMPR("*********rdma_ldim_intr start*********\n");*/
        spin_lock_irqsave(&rdma_ldim_isr_lock, flags);
 
@@ -2049,7 +2586,7 @@ static void ldim_on_vs_brightness(void)
                ldim_driver.device_bri_update(ldim_driver.ldim_matrix_buf,
                        size);
                if (ldim_driver.static_pic_flag == 1) {
-                       ldim_get_matrix_info_6();
+                       ldim_get_matrix_info_max_rgb();
                        ldim_dump_histgram();
                }
        } else {
@@ -2070,10 +2607,8 @@ static void ldim_off_vs_brightness(void)
 
        if (ldim_level_update) {
                ldim_level_update = 0;
-               if (ldim_debug_print) {
-                       LDIMPR("%s: level update: 0x%lx\n",
-                               __func__, litgain);
-               }
+               if (ldim_debug_print)
+                       LDIMPR("%s: level update: 0x%lx\n", __func__, litgain);
                for (i = 0; i < size; i++) {
                        ldim_driver.local_ldim_matrix[i] =
                                (unsigned short)nPRM.BL_matrix[i];
@@ -2098,7 +2633,7 @@ static void ldim_off_vs_brightness(void)
                ldim_driver.device_bri_update(ldim_driver.ldim_matrix_buf,
                        size);
                if (ldim_driver.static_pic_flag == 1) {
-                       ldim_get_matrix_info_6();
+                       ldim_get_matrix_info_max_rgb();
                        ldim_dump_histgram();
                }
        } else {
@@ -2109,20 +2644,9 @@ static void ldim_off_vs_brightness(void)
 static inline void ld_fw_alg_frm(struct LDReg *nPRM1, struct FW_DAT *FDat1,
        unsigned int *max_matrix, unsigned int *hist_matrix)
 {
-#if 0
-       struct aml_bl_drv_s *bl_drv = aml_bl_get_driver();
-
-       switch (bl_drv->data->chip_type) {
-       case BL_CHIP_TXLX:
-               ld_fw_alg_frm_txlx(nPRM1, FDat1, max_matrix, hist_matrix);
-               break;
-       case BL_CHIP_GXTVBB:
-               ld_fw_alg_frm_gxtvbb(nPRM1, FDat1, max_matrix, hist_matrix);
-               break;
-       default:
-               break;
-       }
-#else
+#if (LD_FW_ALG_FRM_SEL == LD_FW_ALG_FRM_V1)
+       ld_fw_alg_frm_txlx(nPRM1, FDat1, max_matrix, hist_matrix);
+#elif (LD_FW_ALG_FRM_SEL == LD_FW_ALG_FRM_V0)
        ld_fw_alg_frm_gxtvbb(nPRM1, FDat1, max_matrix, hist_matrix);
 #endif
 }
@@ -2177,15 +2701,15 @@ static void ldim_on_vs_arithmetic(void)
        kfree(local_ldim_max_rgb);
 }
 
-static void ldim_get_matrix_info_2(void)
+static void ldim_get_matrix_info_TF(void)
 {
        unsigned int i, j, len;
-       unsigned int *local_ldim_matrix_t = NULL;
+       unsigned int *ldim_matrix_t = NULL;
        char *buf;
 
-       local_ldim_matrix_t = kcalloc(ldim_hist_row*ldim_hist_col,
+       ldim_matrix_t = kcalloc(ldim_hist_row*ldim_hist_col,
                sizeof(unsigned int), GFP_KERNEL);
-       if (local_ldim_matrix_t == NULL) {
+       if (ldim_matrix_t == NULL) {
                LDIMERR("ldim_matrix_t malloc error\n");
                return;
        }
@@ -2194,36 +2718,36 @@ static void ldim_get_matrix_info_2(void)
        buf = kcalloc(len, sizeof(char), GFP_KERNEL);
        if (buf == NULL) {
                LDIMERR("print buf malloc error\n");
-               kfree(local_ldim_matrix_t);
+               kfree(ldim_matrix_t);
                return;
        }
 
-       memcpy(local_ldim_matrix_t, &FDat.TF_BL_matrix[0],
+       memcpy(ldim_matrix_t, &FDat.TF_BL_matrix[0],
                ldim_blk_col*ldim_blk_row*sizeof(unsigned int));
 
        pr_info("%s:\n", __func__);
        for (i = 0; i < ldim_blk_row; i++) {
                len = 0;
                for (j = 0; j < ldim_blk_col; j++) {
-                       len += sprintf(buf+len, "\t0x%x",
-                               local_ldim_matrix_t[ldim_blk_col*i+j]);
+                       len += sprintf(buf+len, "\t%4d",
+                               ldim_matrix_t[ldim_blk_col*i+j]);
                }
                pr_info("%s\n", buf);
                msleep(20);
        }
 
        kfree(buf);
-       kfree(local_ldim_matrix_t);
+       kfree(ldim_matrix_t);
 }
-static void ldim_get_matrix_info_3(void)
+static void ldim_get_matrix_info_SF(void)
 {
        unsigned int i, j, len;
-       unsigned int *local_ldim_matrix_t = NULL;
+       unsigned int *ldim_matrix_t = NULL;
        char *buf;
 
-       local_ldim_matrix_t = kcalloc(ldim_hist_row*ldim_hist_col,
+       ldim_matrix_t = kcalloc(ldim_hist_row*ldim_hist_col,
                sizeof(unsigned int), GFP_KERNEL);
-       if (local_ldim_matrix_t == NULL) {
+       if (ldim_matrix_t == NULL) {
                LDIMERR("ldim_matrix_t malloc error\n");
                return;
        }
@@ -2232,36 +2756,36 @@ static void ldim_get_matrix_info_3(void)
        buf = kcalloc(len, sizeof(char), GFP_KERNEL);
        if (buf == NULL) {
                LDIMERR("print buf malloc error\n");
-               kfree(local_ldim_matrix_t);
+               kfree(ldim_matrix_t);
                return;
        }
 
-       memcpy(local_ldim_matrix_t, &FDat.SF_BL_matrix[0],
+       memcpy(ldim_matrix_t, &FDat.SF_BL_matrix[0],
                ldim_blk_col*ldim_blk_row*sizeof(unsigned int));
 
        pr_info("%s:\n", __func__);
        for (i = 0; i < ldim_blk_row; i++) {
                len = 0;
                for (j = 0; j < ldim_blk_col; j++) {
-                       len += sprintf(buf+len, "\t0x%x",
-                               local_ldim_matrix_t[ldim_blk_col*i+j]);
+                       len += sprintf(buf+len, "\t%4d",
+                               ldim_matrix_t[ldim_blk_col*i+j]);
                }
                pr_info("%s\n", buf);
                msleep(20);
        }
 
        kfree(buf);
-       kfree(local_ldim_matrix_t);
+       kfree(ldim_matrix_t);
 }
 static void ldim_get_matrix_info_4(void)
 {
        unsigned int i, j, k, len;
-       unsigned int *local_ldim_matrix_t = NULL;
+       unsigned int *ldim_matrix_t = NULL;
        char *buf;
 
-       local_ldim_matrix_t = kcalloc(ldim_hist_row*ldim_hist_col,
+       ldim_matrix_t = kcalloc(ldim_hist_row*ldim_hist_col,
                16*sizeof(unsigned int), GFP_KERNEL);
-       if (local_ldim_matrix_t == NULL) {
+       if (ldim_matrix_t == NULL) {
                LDIMERR("ldim_matrix_t malloc error\n");
                return;
        }
@@ -2270,11 +2794,11 @@ static void ldim_get_matrix_info_4(void)
        buf = kcalloc(len, sizeof(char), GFP_KERNEL);
        if (buf == NULL) {
                LDIMERR("print buf malloc error\n");
-               kfree(local_ldim_matrix_t);
+               kfree(ldim_matrix_t);
                return;
        }
 
-       memcpy(local_ldim_matrix_t, &FDat.last_STA1_MaxRGB[0],
+       memcpy(ldim_matrix_t, &FDat.last_STA1_MaxRGB[0],
                ldim_blk_col*ldim_blk_row*3*sizeof(unsigned int));
 
        pr_info("%s:\n", __func__);
@@ -2283,8 +2807,8 @@ static void ldim_get_matrix_info_4(void)
                for (j = 0; j < ldim_blk_col; j++) {
                        len += sprintf(buf+len, "\tcol %d:", ldim_blk_col);
                        for (k = 0; k < 3; k++) {
-                               len += sprintf(buf+len, "\t0x%x",
-                               local_ldim_matrix_t[3*ldim_blk_col*i+j*3+k]);
+                               len += sprintf(buf+len, "\t%4d",
+                                       ldim_matrix_t[3*ldim_blk_col*i+j*3+k]);
                        }
                        len += sprintf(buf+len, "\n");
                }
@@ -2293,17 +2817,17 @@ static void ldim_get_matrix_info_4(void)
        }
 
        kfree(buf);
-       kfree(local_ldim_matrix_t);
+       kfree(ldim_matrix_t);
 }
-static void ldim_get_matrix_info_5(void)
+static void ldim_get_matrix_info_alpha(void)
 {
        unsigned int i, j, len;
-       unsigned int *local_ldim_matrix_t = NULL;
+       unsigned int *ldim_matrix_t = NULL;
        char *buf;
 
-       local_ldim_matrix_t = kcalloc(ldim_hist_row*ldim_hist_col,
+       ldim_matrix_t = kcalloc(ldim_hist_row*ldim_hist_col,
                sizeof(unsigned int), GFP_KERNEL);
-       if (local_ldim_matrix_t == NULL) {
+       if (ldim_matrix_t == NULL) {
                LDIMERR("ldim_matrix_t malloc error\n");
                return;
        }
@@ -2312,29 +2836,29 @@ static void ldim_get_matrix_info_5(void)
        buf = kcalloc(len, sizeof(char), GFP_KERNEL);
        if (buf == NULL) {
                LDIMERR("print buf malloc error\n");
-               kfree(local_ldim_matrix_t);
+               kfree(ldim_matrix_t);
                return;
        }
 
-       memcpy(local_ldim_matrix_t, &FDat.TF_BL_alpha[0],
+       memcpy(ldim_matrix_t, &FDat.TF_BL_alpha[0],
                ldim_blk_col*ldim_blk_row*sizeof(unsigned int));
 
        pr_info("%s:\n", __func__);
        for (i = 0; i < ldim_blk_row; i++) {
                len = 0;
                for (j = 0; j < ldim_blk_col; j++) {
-                       len += sprintf(buf+len, "\t0x%x",
-                               local_ldim_matrix_t[ldim_blk_col*i+j]);
+                       len += sprintf(buf+len, "\t%4d",
+                               ldim_matrix_t[ldim_blk_col*i+j]);
                }
                pr_info("%s\n", buf);
                msleep(20);
        }
 
        kfree(buf);
-       kfree(local_ldim_matrix_t);
+       kfree(ldim_matrix_t);
 }
 
-static void ldim_get_matrix_info_6(void)
+static void ldim_get_matrix_info_max_rgb(void)
 {
        unsigned int i, j, len;
        unsigned int *p = NULL;
@@ -2383,15 +2907,15 @@ static void ldim_get_matrix(unsigned int *data, unsigned int reg_sel)
        if (reg_sel == 0)
                LDIM_RD_BASE_LUT(REG_LD_BLK_VIDX_BASE, data, 16, 32);
        else if (reg_sel == 3)
-               ldim_get_matrix_info_2();
+               ldim_get_matrix_info_TF();
        else if (reg_sel == 4)
-               ldim_get_matrix_info_3();
+               ldim_get_matrix_info_SF();
        else if (reg_sel == 5)
                ldim_get_matrix_info_4();
        else if (reg_sel == 6)
-               ldim_get_matrix_info_5();
+               ldim_get_matrix_info_alpha();
        else if (reg_sel == 7)
-               ldim_get_matrix_info_6();
+               ldim_get_matrix_info_max_rgb();
        else if (reg_sel == REG_LD_LUT_HDG_BASE)
                LDIM_RD_BASE_LUT_2(REG_LD_LUT_HDG_BASE, data, 10, 32);
        else if (reg_sel == REG_LD_LUT_VHK_BASE)
@@ -2403,21 +2927,21 @@ static void ldim_get_matrix(unsigned int *data, unsigned int reg_sel)
 static void ldim_get_matrix_info(void)
 {
        unsigned int i, j, len;
-       unsigned short *local_ldim_matrix_t = NULL;
-       unsigned short *local_ldim_matrix_spi_t = NULL;
+       unsigned short *ldim_matrix_t = NULL;
+       unsigned short *ldim_matrix_spi_t = NULL;
        char *buf;
 
-       local_ldim_matrix_t = kcalloc(ldim_hist_row*ldim_hist_col,
+       ldim_matrix_t = kcalloc(ldim_hist_row*ldim_hist_col,
                sizeof(unsigned int), GFP_KERNEL);
-       if (local_ldim_matrix_t == NULL) {
+       if (ldim_matrix_t == NULL) {
                LDIMERR("ldim_matrix_t malloc error\n");
                return;
        }
-       local_ldim_matrix_spi_t = kcalloc(ldim_hist_row*ldim_hist_col,
+       ldim_matrix_spi_t = kcalloc(ldim_hist_row*ldim_hist_col,
                sizeof(unsigned int), GFP_KERNEL);
-       if (local_ldim_matrix_spi_t == NULL) {
+       if (ldim_matrix_spi_t == NULL) {
                LDIMERR("ldim_matrix_spi_t malloc error\n");
-               kfree(local_ldim_matrix_t);
+               kfree(ldim_matrix_t);
                return;
        }
 
@@ -2425,14 +2949,14 @@ static void ldim_get_matrix_info(void)
        buf = kcalloc(len, sizeof(char), GFP_KERNEL);
        if (buf == NULL) {
                LDIMERR("print buf malloc error\n");
-               kfree(local_ldim_matrix_t);
-               kfree(local_ldim_matrix_spi_t);
+               kfree(ldim_matrix_t);
+               kfree(ldim_matrix_spi_t);
                return;
        }
 
-       memcpy(local_ldim_matrix_t, &ldim_driver.local_ldim_matrix[0],
+       memcpy(ldim_matrix_t, &ldim_driver.local_ldim_matrix[0],
                ldim_blk_col*ldim_blk_row*sizeof(unsigned short));
-       memcpy(local_ldim_matrix_spi_t,
+       memcpy(ldim_matrix_spi_t,
                &ldim_driver.ldim_matrix_buf[0],
                ldim_blk_col*ldim_blk_row*sizeof(unsigned short));
 
@@ -2440,19 +2964,20 @@ static void ldim_get_matrix_info(void)
        for (i = 0; i < ldim_blk_row; i++) {
                len = 0;
                for (j = 0; j < ldim_blk_col; j++) {
-                       len += sprintf(buf+len, "\t0x%x",
-                               local_ldim_matrix_t[ldim_blk_col*i+j]);
+                       len += sprintf(buf+len, "\t%4d",
+                               ldim_matrix_t[ldim_blk_col*i+j]);
                }
                pr_info("%s\n", buf);
                msleep(20);
        }
 
+       pr_info("current black_frm: %d\n", black_frm);
        pr_info("spi transfer_matrix:\n");
        for (i = 0; i < ldim_blk_row; i++) {
                len = 0;
                for (j = 0; j < ldim_blk_col; j++) {
-                       len += sprintf(buf+len, "\t0x%x",
-                               local_ldim_matrix_spi_t[ldim_blk_col*i+j]);
+                       len += sprintf(buf+len, "\t%4d",
+                               ldim_matrix_spi_t[ldim_blk_col*i+j]);
                }
                pr_info("%s\n", buf);
                msleep(20);
@@ -2460,14 +2985,14 @@ static void ldim_get_matrix_info(void)
        pr_info("\n");
 
        kfree(buf);
-       kfree(local_ldim_matrix_t);
-       kfree(local_ldim_matrix_spi_t);
+       kfree(ldim_matrix_t);
+       kfree(ldim_matrix_spi_t);
 }
 
 static void ldim_nPRM_bl_matrix_info(void)
 {
        unsigned int i, j, len;
-       unsigned int local_ldim_matrix_t[LD_BLKREGNUM] = {0};
+       unsigned int ldim_matrix_t[LD_BLKREGNUM] = {0};
        char *buf;
 
        len = ldim_blk_col * 10 + 20;
@@ -2477,15 +3002,15 @@ static void ldim_nPRM_bl_matrix_info(void)
                return;
        }
 
-       memcpy(&local_ldim_matrix_t[0], &nPRM.BL_matrix[0],
+       memcpy(&ldim_matrix_t[0], &nPRM.BL_matrix[0],
                ldim_blk_col*ldim_blk_row*sizeof(unsigned short));
 
        pr_info("%s and spi info:\n", __func__);
        for (i = 0; i < ldim_blk_row; i++) {
                len = 0;
                for (j = 0; j < ldim_blk_col; j++) {
-                       len += sprintf(buf+len, "\t0x%x", local_ldim_matrix_t
-                               [ldim_blk_col*i+j]);
+                       len += sprintf(buf+len, "\t0x%x",
+                               ldim_matrix_t[ldim_blk_col*i+j]);
                }
                pr_info("%s\n", buf);
                msleep(20);
@@ -2511,7 +3036,7 @@ static void ldim_set_matrix(unsigned int *data, unsigned int reg_sel,
                LDIM_WR_BASE_LUT(REG_LD_LUT_VDG_BASE, data, 16, cnt);
 }
 
-static void ldim_remap_ctrl(int status)
+static void ldim_remap_ctrl(unsigned char status)
 {
        unsigned int temp;
 
@@ -2530,7 +3055,7 @@ static void ldim_remap_ctrl(int status)
        LDIMPR("%s: %d\n", __func__, status);
 }
 
-static void ldim_func_ctrl(int status)
+static void ldim_func_ctrl(unsigned char status)
 {
        if (status) {
                /* enable other flag */
@@ -2614,11 +3139,14 @@ static int ldim_power_off(void)
 
 static int ldim_set_level(unsigned int level)
 {
-       int ret = 0;
        struct aml_bl_drv_s *bl_drv = aml_bl_get_driver();
        unsigned int level_max, level_min;
 
        ldim_brightness_level = level;
+
+       if (ldim_brightness_bypass)
+               return 0;
+
        level_max = bl_drv->bconf->level_max;
        level_min = bl_drv->bconf->level_min;
 
@@ -2628,7 +3156,7 @@ static int ldim_set_level(unsigned int level)
        litgain = (unsigned long)level;
        ldim_level_update = 1;
 
-       return ret;
+       return 0;
 }
 
 static void ldim_test_ctrl(int flag)
@@ -2676,9 +3204,17 @@ static ssize_t ldim_attr_show(struct class *cla,
        ssize_t len = 0;
 
        len += sprintf(buf+len,
-       "\necho histgram_ldim > /sys/class/aml_ldim/attr\n");
+       "\necho hist > /sys/class/aml_ldim/attr\n");
        len += sprintf(buf+len,
-       "echo ldim_init 1920 1080 8 2 0 1 0 > /sys/class/aml_ldim/attr\n");
+       "echo maxrgb > /sys/class/aml_ldim/attr\n");
+       len += sprintf(buf+len,
+       "echo matrix > /sys/class/aml_ldim/attr\n");
+       len += sprintf(buf+len,
+       "echo histgram_ldim > /sys/class/aml_ldim/attr\n");
+       len += sprintf(buf+len,
+       "echo ldim_matrix_get 7 > /sys/class/aml_ldim/attr\n");
+       len += sprintf(buf+len,
+       "echo ldim_matrix_info > /sys/class/aml_ldim/attr\n");
        len += sprintf(buf+len,
        "echo ldim_matrix_get 0/1/2/3 > /sys/class/aml_ldim/attr\n");
        len += sprintf(buf+len,
@@ -2688,6 +3224,8 @@ static ssize_t ldim_attr_show(struct class *cla,
        len += sprintf(buf+len,
        "echo info > /sys/class/aml_ldim/attr\n");
        len += sprintf(buf+len,
+       "echo alg_para > /sys/class/aml_ldim/attr\n");
+       len += sprintf(buf+len,
        "echo test_mode 0 > /sys/class/aml_ldim/attr\n");
        len += sprintf(buf+len,
        "echo test_set 0 0xfff > /sys/class/aml_ldim/attr\n");
@@ -2700,17 +3238,57 @@ static ssize_t ldim_attr_show(struct class *cla,
        "echo fw_LD_ThSF_l 1600 > /sys/class/aml_ldim/attr\n");
        len += sprintf(buf+len,
        "echo fw_LD_ThTF_l 32 > /sys/class/aml_ldim/attr\n");
-
+       len += sprintf(buf+len,
+       "echo boost_gain 4 > /sys/class/aml_ldim/attr\n");
+#if (LD_FW_ALG_FRM_SEL == LD_FW_ALG_FRM_V1)
+       len += sprintf(buf+len,
+       "echo alpha_delta 0 > /sys/class/aml_ldim/attr\n");
+       len += sprintf(buf+len,
+       "echo boost_gain_neg 4 > /sys/class/aml_ldim/attr\n");
+#elif (LD_FW_ALG_FRM_SEL == LD_FW_ALG_FRM_V0)
+       len += sprintf(buf+len,
+       "echo TF_alpha 256 > /sys/class/aml_ldim/attr\n");
+       len += sprintf(buf+len,
+       "echo lpf_gain 0 > /sys/class/aml_ldim/attr\n");
+       len += sprintf(buf+len,
+       "echo lpf_res 0 > /sys/class/aml_ldim/attr\n");
        len += sprintf(buf+len,
        "echo rgb_base 128 > /sys/class/aml_ldim/attr\n");
+#endif
 
        len += sprintf(buf+len,
-       "echo Dbprint_lv 128 > /sys/class/aml_ldim/attr\n");
+       "echo ov_gain 16 > /sys/class/aml_ldim/attr\n");
+       len += sprintf(buf+len,
+       "echo incr_dif_gain 16 > /sys/class/aml_ldim/attr\n");
+       len += sprintf(buf+len,
+       "echo avg_gain 2048 > /sys/class/aml_ldim/attr\n");
+       len += sprintf(buf+len,
+       "echo litgain 4096 > /sys/class/aml_ldim/attr\n");
 
        len += sprintf(buf+len,
-       "echo lpf_gain 0 > /sys/class/aml_ldim/attr\n");
+       "echo LPF_method 3 > /sys/class/aml_ldim/attr\n");
        len += sprintf(buf+len,
-       "echo lpf_res 0 > /sys/class/aml_ldim/attr\n");
+       "echo LD_TF_STEP_TH 100 > /sys/class/aml_ldim/attr\n");
+       len += sprintf(buf+len,
+       "echo TF_step_method 3 > /sys/class/aml_ldim/attr\n");
+       len += sprintf(buf+len,
+       "echo TF_FRESH_BL 8 > /sys/class/aml_ldim/attr\n");
+
+       len += sprintf(buf+len,
+       "echo TF_BLK_FRESH_BL 5 > /sys/class/aml_ldim/attr\n");
+       len += sprintf(buf+len,
+       "echo side_blk_diff_th 100 > /sys/class/aml_ldim/attr\n");
+       len += sprintf(buf+len,
+       "echo bbd_th 200 > /sys/class/aml_ldim/attr\n");
+       len += sprintf(buf+len,
+       "echo bbd_detect_en 1 > /sys/class/aml_ldim/attr\n");
+       len += sprintf(buf+len,
+       "echo diff_blk_luma_en 1 > /sys/class/aml_ldim/attr\n");
+
+       len += sprintf(buf+len,
+       "echo bl_remap_curve > /sys/class/aml_ldim/attr\n");
+       len += sprintf(buf+len,
+       "echo fw_LD_Whist > /sys/class/aml_ldim/attr\n");
 
        len += sprintf(buf+len,
        "echo Sf_bypass 0 > /sys/class/aml_ldim/attr\n");
@@ -2722,17 +3300,16 @@ static ssize_t ldim_attr_show(struct class *cla,
        "echo Ld_remap_bypass 0 > /sys/class/aml_ldim/attr\n");
 
        len += sprintf(buf+len,
-       "echo litgain 4096 > /sys/class/aml_ldim/attr\n");
+       "echo fw_hist_print 1 > /sys/class/aml_ldim/attr\n");
        len += sprintf(buf+len,
-       "echo boost_gain 4 > /sys/class/aml_ldim/attr\n");
+       "echo Debug 1 > /sys/class/aml_ldim/attr\n");
        len += sprintf(buf+len,
-       "echo alpha 256 > /sys/class/aml_ldim/attr\n");
+       "echo fw_print_frequent 8 > /sys/class/aml_ldim/attr\n");
        len += sprintf(buf+len,
-       "echo alpha_delta 0 > /sys/class/aml_ldim/attr\n");
-       len += sprintf(buf+len,
-       "echo boost_gain_neg 4 > /sys/class/aml_ldim/attr\n");
+       "echo Dbprint_lv 1 > /sys/class/aml_ldim/attr\n");
+
        len += sprintf(buf+len,
-       "echo avg_gain 2048 > /sys/class/aml_ldim/attr\n");
+       "echo ldim_init 1920 1080 8 2 0 1 0 > /sys/class/aml_ldim/attr\n");
 
        len += sprintf(buf+len,
        "echo curve_0 512 4 3712 29 > /sys/class/aml_ldim/attr\n");
@@ -2788,7 +3365,6 @@ static ssize_t ldim_attr_store(struct class *cla,
        unsigned long blk_vnum = 0, blk_hnum = 0, hist_row = 0, hist_col = 0;
        unsigned long backlit_mod = 0, ldim_bl_en = 0, ldim_hvcnt_bypass = 0;
        unsigned long val1 = 0, val2 = 0;
-       unsigned long db_data;
 
        if (!buf)
                return len;
@@ -2809,16 +3385,24 @@ static ssize_t ldim_attr_store(struct class *cla,
 
        if (!strcmp(parm[0], "histgram_ldim")) {
                ldim_dump_histgram();
-       } else if (!strcmp(parm[0], "data_db_enable")) {
+       } else if (!strcmp(parm[0], "maxrgb")) {
+               ldim_get_matrix_info_max_rgb();
+       } else if (!strcmp(parm[0], "hist")) {
+               ldim_dump_histgram();
+       } else if (!strcmp(parm[0], "matrix")) {
+               ldim_get_matrix_info();
+       } else if (!strcmp(parm[0], "db_en")) {
                if (parm[1] != NULL) {
-                       if (kstrtoul(parm[1], 10, &db_data) < 0)
-                               return -EINVAL;
+                       if (kstrtoul(parm[1], 10, &val1) < 0)
+                               goto ldim_attr_store_end;
+                       LDIM_DATA_FROM_DB = (unsigned char)val1;
+                       ldim_db_load_update(&nPRM);
                }
-               LDIM_DATA_FROM_DB = (unsigned int)db_data;
-               pr_info("set LDIM_DATA_FROM_DB=%lu\n", db_data);
-       } else if (!strcmp(parm[0], "print_db_flag")) {
-               print_db_flag = 1;
-               ldim_db_load_update(&nPRM);
+               pr_info("LDIM_DATA_FROM_DB = %d\n", LDIM_DATA_FROM_DB);
+       } else if (!strcmp(parm[0], "db_print")) {
+               db_print_flag = 1;
+       } else if (!strcmp(parm[0], "dump_db")) {
+               ldim_db_para_print(&nPRM);
        } else if (!strcmp(parm[0], "ldim_init")) {
                if (parm[7] != NULL) {
                        if (kstrtoul(parm[1], 10, &pic_h) < 0)
@@ -2835,18 +3419,19 @@ static ssize_t ldim_attr_store(struct class *cla,
                                return -EINVAL;
                        if (kstrtoul(parm[7], 10, &ldim_hvcnt_bypass) < 0)
                                goto ldim_attr_store_end;
+
+                       pr_info("ldim init param:%lu,%lu,%lu,%lu,%lu,%lu,%lu\n",
+                               pic_h, pic_v, blk_vnum, blk_hnum,
+                               backlit_mod, ldim_bl_en, ldim_hvcnt_bypass);
+                       ldim_blk_row = blk_vnum;
+                       ldim_blk_col = blk_hnum;
+                       ldim_config.row = ldim_blk_row;
+                       ldim_config.col = ldim_blk_col;
+                       ldim_config.bl_mode = (unsigned char)backlit_mod;
+                       LDIM_Initial(pic_h, pic_v, blk_vnum, blk_hnum,
+                               backlit_mod, ldim_bl_en, ldim_hvcnt_bypass);
+                       pr_info("**************ldim init ok*************\n");
                }
-               pr_info("****ldim init param:%lu,%lu,%lu,%lu,%lu,%lu,%lu****\n",
-                       pic_h, pic_v, blk_vnum, blk_hnum,
-                       backlit_mod, ldim_bl_en, ldim_hvcnt_bypass);
-               ldim_blk_row = blk_vnum;
-               ldim_blk_col = blk_hnum;
-               ldim_config.row = ldim_blk_row;
-               ldim_config.col = ldim_blk_col;
-               ldim_config.bl_mode = (unsigned char)backlit_mod;
-               LDIM_Initial(pic_h, pic_v, blk_vnum, blk_hnum,
-                       backlit_mod, ldim_bl_en, ldim_hvcnt_bypass);
-               pr_info("**************ldim init ok*************\n");
        } else if (!strcmp(parm[0], "ldim_stts_init")) {
                if (parm[4] != NULL) {
                        if (kstrtoul(parm[1], 10, &pic_h) < 0)
@@ -2857,82 +3442,85 @@ static ssize_t ldim_attr_store(struct class *cla,
                                goto ldim_attr_store_end;
                        if (kstrtoul(parm[4], 10, &hist_col) < 0)
                                goto ldim_attr_store_end;
+
+                       pr_info("****ldim init param:%lu,%lu,%lu,%lu********\n",
+                               pic_h, pic_v, hist_row, hist_col);
+                       ldim_hist_row = hist_row;
+                       ldim_hist_col = hist_col;
+                       ldim_stts_initial(pic_h, pic_v, hist_row, hist_col);
+                       pr_info("************ldim stts init ok*************\n");
                }
-               pr_info("****ldim init param:%lu,%lu,%lu,%lu*********\n",
-                       pic_h, pic_v, hist_row, hist_col);
-               ldim_hist_row = hist_row;
-               ldim_hist_col = hist_col;
-               ldim_stts_initial(pic_h, pic_v, hist_row, hist_col);
-               pr_info("**************ldim stts init ok*************\n");
        } else if (!strcmp(parm[0], "remap")) {
                if (parm[1] != NULL) {
                        if (kstrtoul(parm[1], 10, &val1) < 0)
                                goto ldim_attr_store_end;
-               }
-               if (val1) {
-                       if (ldim_func_en) {
-                               ldim_remap_en = 1;
-                               ldim_remap_ctrl(1);
+                       if (val1) {
+                               if (ldim_func_en) {
+                                       ldim_remap_en = 1;
+                                       ldim_remap_ctrl(1);
+                               } else {
+                                       pr_info(
+                                       "error: ldim_func is disabled\n");
+                               }
                        } else {
-                               pr_info("error: ldim_func is disabled\n");
+                               ldim_remap_en = 0;
+                               ldim_remap_ctrl(0);
                        }
-               } else {
-                       ldim_remap_en = 0;
-                       ldim_remap_ctrl(0);
                }
                pr_info("ldim_remap_en: %d\n", ldim_remap_en);
-       } else if (!strcmp(parm[0], "remap_get")) {
-               pr_info("ldim_remap_en: %d\n", ldim_remap_en);
        } else if (!strcmp(parm[0], "ldim_matrix_get")) {
                unsigned int data[32] = {0};
-               unsigned int k, g;
-               unsigned long reg_sel = 0;
+               unsigned int k, g, reg_sel = 0;
 
                if (parm[1] != NULL) {
-                       if (kstrtoul(parm[1], 10, &reg_sel) < 0)
-                               goto ldim_attr_store_end;
-               }
-               pr_buf = kzalloc(sizeof(char) * 200, GFP_KERNEL);
-               if (!pr_buf) {
-                       LDIMERR("buf malloc error\n");
-                       goto ldim_attr_store_end;
-               }
-               ldim_get_matrix(&data[0], reg_sel);
-               if ((reg_sel == 0) || (reg_sel == 1)) {
-                       pr_info("**********ldim matrix info start**********\n");
-                       for (k = 0; k < 4; k++) {
-                               pr_len = 0;
-                               for (g = 0; g < 8; g++) {
-                                       pr_len += sprintf(pr_buf+pr_len,
-                                               "\t%d", data[8*k+g]);
+                       if (kstrtoul(parm[1], 10, &val1) < 0)
+                               goto ldim_attr_store_end;
+                       reg_sel = (unsigned int)val1;
+
+                       pr_buf = kzalloc(sizeof(char) * 200, GFP_KERNEL);
+                       if (!pr_buf) {
+                               LDIMERR("buf malloc error\n");
+                               goto ldim_attr_store_end;
+                       }
+                       ldim_get_matrix(&data[0], reg_sel);
+                       if ((reg_sel == 0) || (reg_sel == 1)) {
+                               pr_info("******ldim matrix info start******\n");
+                               for (k = 0; k < 4; k++) {
+                                       pr_len = 0;
+                                       for (g = 0; g < 8; g++) {
+                                               pr_len += sprintf(pr_buf+pr_len,
+                                                       "\t%d", data[8*k+g]);
+                                       }
+                                       pr_info("%s\n", pr_buf);
                                }
-                               pr_info("%s\n", pr_buf);
+                               pr_info("*******ldim matrix info end*******\n");
                        }
-                       pr_info("**********ldim matrix info end***********\n");
+                       kfree(pr_buf);
                }
-               kfree(pr_buf);
        } else if (!strcmp(parm[0], "ldim_matrix_set")) {
                unsigned int data_set[32] = {0};
-               unsigned long reg_sel_1 = 0, k1, cnt1 = 0;
+               unsigned int reg_sel_1 = 0, k1, cnt1 = 0;
                unsigned long temp_set[32] = {0};
 
-               if (parm[1] != NULL) {
-                       if (kstrtoul(parm[1], 10, &reg_sel_1) < 0)
-                               goto ldim_attr_store_end;
-               }
                if (parm[2] != NULL) {
-                       if (kstrtoul(parm[1], 10, &cnt1) < 0)
+                       if (kstrtoul(parm[1], 10, &val1) < 0)
                                goto ldim_attr_store_end;
-               }
-               for (k1 = 0; k1 < cnt1; k1++) {
-                       if (parm[k1+2] != NULL) {
-                               temp_set[k1] =
-                                       kstrtoul(parm[k1+2], 10, &temp_set[k1]);
-                               data_set[k1] = (unsigned int)temp_set[k1];
+                       if (kstrtoul(parm[2], 10, &val2) < 0)
+                               goto ldim_attr_store_end;
+
+                       reg_sel_1 = val1;
+                       cnt1 = val2;
+                       for (k1 = 0; k1 < cnt1; k1++) {
+                               if (parm[k1+2] != NULL) {
+                                       temp_set[k1] = kstrtoul(parm[k1+2],
+                                               10, &temp_set[k1]);
+                                       data_set[k1] =
+                                               (unsigned int)temp_set[k1];
+                               }
                        }
+                       ldim_set_matrix(&data_set[0], reg_sel_1, cnt1);
+                       pr_info("***********ldim matrix set over***********\n");
                }
-               ldim_set_matrix(&data_set[0], (unsigned int)reg_sel_1, cnt1);
-               pr_info("**************ldim matrix set over*************\n");
        } else if (!strcmp(parm[0], "ldim_matrix_info")) {
                ldim_get_matrix_info();
                pr_info("**************ldim matrix info over*************\n");
@@ -2951,16 +3539,17 @@ static ssize_t ldim_attr_store(struct class *cla,
                if (parm[1] != NULL) {
                        if (kstrtoul(parm[1], 10, &val1) < 0)
                                goto ldim_attr_store_end;
+
+                       ldim_data_min = (unsigned int)val1;
+                       ldim_set_level(ldim_brightness_level);
+                       pr_info("*****ldim brightness data_min update******\n");
                }
-               ldim_data_min = (unsigned int)val1;
-               ldim_set_level(ldim_brightness_level);
-               pr_info("**********ldim brightness data_min update*********\n");
        } else if (!strcmp(parm[0], "test_mode")) {
                if (parm[1] != NULL) {
                        if (kstrtoul(parm[1], 10, &val1) < 0)
                                goto ldim_attr_store_end;
+                       ldim_test_en = (unsigned char)val1;
                }
-               ldim_test_en = (unsigned int)val1;
                LDIMPR("test_mode: %d\n", ldim_test_en);
        } else if (!strcmp(parm[0], "test_set")) {
                if (parm[2] != NULL) {
@@ -2968,25 +3557,30 @@ static ssize_t ldim_attr_store(struct class *cla,
                                goto ldim_attr_store_end;
                        if (kstrtoul(parm[2], 16, &val2) < 0)
                                goto ldim_attr_store_end;
-               }
-               size = ldim_blk_row * ldim_blk_col;
-               if (val1 < size) {
-                       ldim_driver.ldim_test_matrix[val1] =
-                               (unsigned short)val2;
-                       LDIMPR("set test_matrix[%d] = 0x%03x\n",
-                               (unsigned int)val1, (unsigned int)val2);
-               } else {
-                       LDIMERR("invalid index for test_matrix: %d\n",
-                               (unsigned int)val1);
+
+                       size = ldim_blk_row * ldim_blk_col;
+                       if (val1 < size) {
+                               ldim_driver.ldim_test_matrix[val1] =
+                                       (unsigned short)val2;
+                               LDIMPR("set test_matrix[%d] = 0x%03x\n",
+                                       (unsigned int)val1, (unsigned int)val2);
+                       } else {
+                               LDIMERR("invalid index for test_matrix: %d\n",
+                                       (unsigned int)val1);
+                       }
                }
        } else if (!strcmp(parm[0], "test_set_all")) {
                if (parm[1] != NULL) {
                        if (kstrtoul(parm[1], 16, &val1) < 0)
                                goto ldim_attr_store_end;
+
+                       for (i = 0; i < ldim_blk_row * ldim_blk_col; i++) {
+                               ldim_driver.ldim_test_matrix[i] =
+                                       (unsigned short)val1;
+                       }
+                       LDIMPR("set all test_matrix to 0x%03x\n",
+                               (unsigned int)val1);
                }
-               for (i = 0; i < ldim_blk_row * ldim_blk_col; i++)
-                       ldim_driver.ldim_test_matrix[i] = (unsigned short)val1;
-               LDIMPR("set all test_matrix to 0x%03x\n", (unsigned int)val1);
        } else if (!strcmp(parm[0], "test_get")) {
                LDIMPR("get test_mode: %d, test_matrix:\n", ldim_test_en);
                size = ldim_blk_row * ldim_blk_col;
@@ -2994,225 +3588,431 @@ static ssize_t ldim_attr_store(struct class *cla,
                        pr_info("0x%03x\t", ldim_driver.ldim_test_matrix[i]);
                pr_info("\n");
        } else if (!strcmp(parm[0], "rs")) {
-               unsigned long reg_addr = 0, reg_val;
+               unsigned int reg_addr = 0, reg_val;
 
                if (parm[1] != NULL) {
-                       if (kstrtoul(parm[1], 16, &reg_addr) < 0)
+                       if (kstrtoul(parm[1], 16, &val1) < 0)
                                goto ldim_attr_store_end;
+                       reg_addr = (unsigned int)val1;
+
+                       reg_val = LDIM_RD_32Bits(reg_addr);
+                       pr_info("reg_addr: 0x%x=0x%x\n", reg_addr, reg_val);
                }
-               reg_val = LDIM_RD_32Bits(reg_addr);
-               pr_info("reg_addr:0x%x=0x%x\n",
-                       (unsigned int)reg_addr, (unsigned int)reg_val);
        } else if (!strcmp(parm[0], "ws")) {
-               unsigned long reg_addr = 0, reg_val = 0;
+               unsigned int reg_addr = 0, reg_val = 0;
 
-               if (parm[1] != NULL) {
-                       if (kstrtoul(parm[1], 16, &reg_addr) < 0)
-                               goto ldim_attr_store_end;
-               }
                if (parm[2] != NULL) {
-                       if (kstrtoul(parm[2], 16, &reg_val) < 0)
+                       if (kstrtoul(parm[1], 16, &val1) < 0)
+                               goto ldim_attr_store_end;
+                       if (kstrtoul(parm[2], 16, &val2) < 0)
                                goto ldim_attr_store_end;
+                       reg_addr = (unsigned int)val1;
+                       reg_val = (unsigned int)val2;
+
+                       LDIM_WR_32Bits(reg_addr, reg_val);
+                       pr_info("reg_addr: 0x%x=0x%x, readback: 0x%x\n",
+                               reg_addr, reg_val, LDIM_RD_32Bits(reg_addr));
                }
-               LDIM_WR_32Bits(reg_addr, reg_val);
-               pr_info("reg_addr:0x%x=0x%x\n",
-                       (unsigned int)reg_addr, (unsigned int)reg_val);
        } else if (!strcmp(parm[0], "update_matrix")) {
-               unsigned long mode = 0;
+               unsigned int mode = 0;
 
                if (parm[1] != NULL) {
-                       if (kstrtoul(parm[1], 10, &mode) < 0)
+                       if (kstrtoul(parm[1], 10, &val1) < 0)
                                goto ldim_attr_store_end;
+                       mode  = (unsigned int)val1;
+
+                       ldim_update_matrix(mode);
+                       pr_info("mode: %d\n", mode);
                }
-               ldim_update_matrix(mode);
-               pr_info("mode:%d\n", (unsigned int)mode);
+       } else if (!strcmp(parm[0], "bl_remap_curve")) {
+               ldim_bl_remap_curve_print();
        } else if (!strcmp(parm[0], "fw_LD_Whist")) {
                for (i = 0; i < 16; i++)
                        pr_info("(%d),", (unsigned int)fw_LD_Whist[i]);
                if (parm[16] != NULL) {
                        if (kstrtoul(parm[1], 10, &fw_LD_Whist[0]) < 0)
-                               return -EINVAL;
+                               goto ldim_attr_store_end;
                        if (kstrtoul(parm[2], 10, &fw_LD_Whist[1]) < 0)
-                               return -EINVAL;
+                               goto ldim_attr_store_end;
                        if (kstrtoul(parm[3], 10, &fw_LD_Whist[2]) < 0)
-                               return -EINVAL;
+                               goto ldim_attr_store_end;
                        if (kstrtoul(parm[4], 10, &fw_LD_Whist[3]) < 0)
-                               return -EINVAL;
+                               goto ldim_attr_store_end;
                        if (kstrtoul(parm[5], 10, &fw_LD_Whist[4]) < 0)
-                               return -EINVAL;
+                               goto ldim_attr_store_end;
                        if (kstrtoul(parm[6], 10, &fw_LD_Whist[5]) < 0)
-                               return -EINVAL;
+                               goto ldim_attr_store_end;
                        if (kstrtoul(parm[7], 10, &fw_LD_Whist[6]) < 0)
-                               return -EINVAL;
+                               goto ldim_attr_store_end;
                        if (kstrtoul(parm[8], 10, &fw_LD_Whist[7]) < 0)
-                               return -EINVAL;
+                               goto ldim_attr_store_end;
                        if (kstrtoul(parm[9], 10, &fw_LD_Whist[8]) < 0)
-                               return -EINVAL;
+                               goto ldim_attr_store_end;
                        if (kstrtoul(parm[10], 10, &fw_LD_Whist[9]) < 0)
-                               return -EINVAL;
+                               goto ldim_attr_store_end;
                        if (kstrtoul(parm[11], 10, &fw_LD_Whist[10]) < 0)
-                               return -EINVAL;
+                               goto ldim_attr_store_end;
                        if (kstrtoul(parm[12], 10, &fw_LD_Whist[11]) < 0)
-                               return -EINVAL;
+                               goto ldim_attr_store_end;
                        if (kstrtoul(parm[13], 10, &fw_LD_Whist[12]) < 0)
-                               return -EINVAL;
+                               goto ldim_attr_store_end;
                        if (kstrtoul(parm[14], 10, &fw_LD_Whist[13]) < 0)
-                               return -EINVAL;
+                               goto ldim_attr_store_end;
                        if (kstrtoul(parm[15], 10, &fw_LD_Whist[14]) < 0)
-                               return -EINVAL;
+                               goto ldim_attr_store_end;
                        if (kstrtoul(parm[16], 10, &fw_LD_Whist[15]) < 0)
-                               return -EINVAL;
+                               goto ldim_attr_store_end;
+
+                       for (i = 0; i < 16; i++)
+                               pr_info("(%d),", (unsigned int)fw_LD_Whist[i]);
+                       pr_info("\n********fw_LD_Whist ok*********\n");
                }
-               for (i = 0; i < 16; i++)
-                       pr_info("(%d),", (unsigned int)fw_LD_Whist[i]);
-               pr_info("\n********fw_LD_Whist ok*********\n");
+       } else if (!strcmp(parm[0], "Sf_bypass")) {
+               if (parm[1] != NULL) {
+                       if (kstrtoul(parm[1], 10, &val1) < 0)
+                               goto ldim_attr_store_end;
+                       Sf_bypass = (unsigned char)val1;
+               }
+               pr_info("Sf_bypass = %d\n", Sf_bypass);
+       } else if (!strcmp(parm[0], "Boost_light_bypass")) {
+               if (parm[1] != NULL) {
+                       if (kstrtoul(parm[1], 10, &val1) < 0)
+                               goto ldim_attr_store_end;
+                       Boost_light_bypass = (unsigned char)val1;
+               }
+               pr_info("Boost_light_bypass = %d\n", Boost_light_bypass);
+       } else if (!strcmp(parm[0], "Lpf_bypass")) {
+               if (parm[1] != NULL) {
+                       if (kstrtoul(parm[1], 10, &val1) < 0)
+                               goto ldim_attr_store_end;
+                       Lpf_bypass = (unsigned char)val1;
+               }
+               pr_info("Lpf_bypass = %d\n", Lpf_bypass);
+       } else if (!strcmp(parm[0], "Ld_remap_bypass")) {
+               if (parm[1] != NULL) {
+                       if (kstrtoul(parm[1], 10, &val1) < 0)
+                               goto ldim_attr_store_end;
+                       Ld_remap_bypass = (unsigned char)val1;
+               }
+               pr_info("Ld_remap_bypass = %d\n", Ld_remap_bypass);
+       } else if (!strcmp(parm[0], "slp_gain")) {
+               unsigned int slop_gain = 0;
+
+               if (parm[1] != NULL) {
+                       if (kstrtoul(parm[1], 10, &val1) < 0)
+                               goto ldim_attr_store_end;
+
+                       slop_gain = (unsigned int)val1;
+                       ldim_bl_remap_curve(slop_gain);
+                       pr_info("slp_gain = %d\n", slop_gain);
+                       ldim_bl_remap_curve_print();
+               }
+       } else if (!strcmp(parm[0], "ov_gain")) {
+               if (parm[1] != NULL) {
+                       if (kstrtoul(parm[1], 10, &val1) < 0)
+                               goto ldim_attr_store_end;
+                       ov_gain = (unsigned int)val1;
+               }
+               pr_info("ov_gain = %d\n", ov_gain);
+       } else if (!strcmp(parm[0], "incr_dif_gain")) {
+               if (parm[1] != NULL) {
+                       if (kstrtoul(parm[1], 10, &val1) < 0)
+                               goto ldim_attr_store_end;
+                       incr_dif_gain = (unsigned int)val1;
+               }
+               pr_info("incr_dif_gain = %d\n", incr_dif_gain);
+       } else if (!strcmp(parm[0], "litgain")) {
+               if (parm[1] != NULL) {
+                       if (kstrtoul(parm[1], 10, &litgain) < 0)
+                               goto ldim_attr_store_end;
+               }
+               pr_info("litgain = %ld\n", litgain);
+       } else if (!strcmp(parm[0], "brightness_bypass")) {
+               if (parm[1] != NULL) {
+                       if (kstrtoul(parm[1], 10, &val1) < 0)
+                               goto ldim_attr_store_end;
+                       ldim_brightness_bypass = (unsigned char)val1;
+                       if (ldim_brightness_bypass == 0)
+                               ldim_set_level(ldim_brightness_level);
+               }
+               pr_info("brightness_bypass = %d\n", ldim_brightness_bypass);
        } else if (!strcmp(parm[0], "fw_LD_ThSF_l")) {
-               pr_info("now fw_LD_ThSF_1 = %ld\n", fw_LD_ThSF_l);
                if (parm[1] != NULL) {
                        if (kstrtoul(parm[1], 10, &fw_LD_ThSF_l) < 0)
                                goto ldim_attr_store_end;
                }
-               pr_info("set fw_LD_ThSF_l=%ld\n", fw_LD_ThSF_l);
+               pr_info("fw_LD_ThSF_l = %ld\n", fw_LD_ThSF_l);
        } else if (!strcmp(parm[0], "fw_LD_ThTF_l")) {
-               pr_info("now fw_LD_ThTF_l = %ld\n", fw_LD_ThTF_l);
                if (parm[1] != NULL) {
                        if (kstrtoul(parm[1], 10, &fw_LD_ThTF_l) < 0)
                                goto ldim_attr_store_end;
                }
-               pr_info("set fw_LD_ThTF_l=%ld\n", fw_LD_ThTF_l);
-       } else if (!strcmp(parm[0], "rgb_base")) {
+               pr_info("fw_LD_ThTF_l = %ld\n", fw_LD_ThTF_l);
+       } else if (!strcmp(parm[0], "boost_gain")) {
                if (parm[1] != NULL) {
-                       if (kstrtoul(parm[1], 10, &rgb_base) < 0)
+                       if (kstrtoul(parm[1], 10, &boost_gain) < 0)
+                               goto ldim_attr_store_end;
+               }
+               pr_info("boost_gain = %ld\n", boost_gain);
+#if (LD_FW_ALG_FRM_SEL == LD_FW_ALG_FRM_V1)
+       } else if (!strcmp(parm[0], "boost_gain_neg")) {
+               if (parm[1] != NULL) {
+                       if (kstrtoul(parm[1], 10, &boost_gain_neg) < 0)
                                goto ldim_attr_store_end;
                }
-               pr_info("set rgb_base=%ld\n", rgb_base);
+               pr_info("boost_gain_neg = %ld\n", boost_gain_neg);
+       } else if (!strcmp(parm[0], "alpha_delta")) {
+               if (parm[1] != NULL) {
+                       if (kstrtoul(parm[1], 10, &alpha_delta) < 0)
+                               goto ldim_attr_store_end;
+               }
+               pr_info("alpha_delta = %ld\n", alpha_delta);
+#elif (LD_FW_ALG_FRM_SEL == LD_FW_ALG_FRM_V0)
+       } else if (!strcmp(parm[0], "TF_alpha")) {
+               if (parm[1] != NULL) {
+                       if (kstrtoul(parm[1], 10, &TF_alpha) < 0)
+                               goto ldim_attr_store_end;
+               }
+               pr_info("TF_alpha = %ld\n", TF_alpha);
        } else if (!strcmp(parm[0], "lpf_gain")) {
                if (parm[1] != NULL) {
                        if (kstrtoul(parm[1], 10, &lpf_gain) < 0)
                                goto ldim_attr_store_end;
                }
-               pr_info("set lpf_gain=%ld\n", lpf_gain);
+               pr_info("lpf_gain = %ld\n", lpf_gain);
        } else if (!strcmp(parm[0], "lpf_res")) {
                if (parm[1] != NULL) {
                        if (kstrtoul(parm[1], 10, &lpf_res) < 0)
                                goto ldim_attr_store_end;
                }
-               pr_info("set lpf_res=%ld\n", lpf_res);
-       } else if (!strcmp(parm[0], "Sf_bypass")) {
+               pr_info("lpf_res = %ld\n", lpf_res);
+       } else if (!strcmp(parm[0], "rgb_base")) {
                if (parm[1] != NULL) {
-                       if (kstrtoul(parm[1], 10, &Sf_bypass) < 0)
+                       if (kstrtoul(parm[1], 10, &rgb_base) < 0)
                                goto ldim_attr_store_end;
                }
-               pr_info("set Sf_bypass=%ld\n", Sf_bypass);
-       } else if (!strcmp(parm[0], "Boost_light_bypass")) {
+               pr_info("rgb_base = %ld\n", rgb_base);
+#endif
+       } else if (!strcmp(parm[0], "avg_gain")) {
                if (parm[1] != NULL) {
-                       if (kstrtoul(parm[1], 10, &Boost_light_bypass) < 0)
+                       if (kstrtoul(parm[1], 10, &avg_gain) < 0)
                                goto ldim_attr_store_end;
                }
-               pr_info("set Boost_light_bypass=%ld\n", Boost_light_bypass);
-       } else if (!strcmp(parm[0], "Lpf_bypass")) {
+               pr_info("avg_gain = %ld\n", avg_gain);
+       } else if (!strcmp(parm[0], "fw_rgb_diff_th")) {
                if (parm[1] != NULL) {
-                       if (kstrtoul(parm[1], 10, &Lpf_bypass) < 0)
+                       if (kstrtoul(parm[1], 10, &val1) < 0)
                                goto ldim_attr_store_end;
+                       fw_rgb_diff_th = (unsigned int)val1;
                }
-               pr_info("set Lpf_bypass=%ld\n", Lpf_bypass);
-       } else if (!strcmp(parm[0], "Ld_remap_bypass")) {
+               pr_info("fw_rgb_diff_th = %d\n", fw_rgb_diff_th);
+       } else if (!strcmp(parm[0], "max_luma")) {
                if (parm[1] != NULL) {
-                       if (kstrtoul(parm[1], 10, &Ld_remap_bypass) < 0)
+                       if (kstrtoul(parm[1], 10, &val1) < 0)
                                goto ldim_attr_store_end;
+                       max_luma = (unsigned int)val1;
                }
-               pr_info("set Ld_remap_bypass=%ld\n", Ld_remap_bypass);
-       } else if (!strcmp(parm[0], "slp_gain")) {
-               unsigned int slop_gain;
-
+               pr_info("max_luma = %d\n", max_luma);
+       } else if (!strcmp(parm[0], "lmh_avg_TH")) {
                if (parm[1] != NULL) {
                        if (kstrtoul(parm[1], 10, &val1) < 0)
                                goto ldim_attr_store_end;
+                       lmh_avg_TH = (unsigned int)val1;
                }
-               slop_gain = (unsigned int)val1;
-               ldim_bl_remap_curve(slop_gain);
-               ldim_bl_remap_curve_print();
-               pr_info("set slp_gain=%d\n", slop_gain);
-       } else if (!strcmp(parm[0], "incr_con_en")) {
+               pr_info("lmh_avg_TH = %d\n", lmh_avg_TH);
+       } else if (!strcmp(parm[0], "fw_TF_sum_th")) {
                if (parm[1] != NULL) {
                        if (kstrtoul(parm[1], 10, &val1) < 0)
                                goto ldim_attr_store_end;
+                       fw_TF_sum_th = (unsigned int)val1;
                }
-               incr_con_en = (unsigned int)val1;
-               pr_info("set incr_con_en=%d\n", incr_con_en);
-       } else if (!strcmp(parm[0], "ov_gain")) {
+               pr_info("fw_TF_sum_th = %d\n", fw_TF_sum_th);
+       } else if (!strcmp(parm[0], "LPF_method")) {
                if (parm[1] != NULL) {
                        if (kstrtoul(parm[1], 10, &val1) < 0)
                                goto ldim_attr_store_end;
+                       LPF_method = (unsigned int)val1;
                }
-               ov_gain = (unsigned int)val1;
-               pr_info("set ov_gain=%d\n", ov_gain);
-       } else if (!strcmp(parm[0], "incr_dif_gain")) {
+               pr_info("LPF_method = %d\n", LPF_method);
+       } else if (!strcmp(parm[0], "LD_TF_STEP_TH")) {
                if (parm[1] != NULL) {
                        if (kstrtoul(parm[1], 10, &val1) < 0)
                                goto ldim_attr_store_end;
+                       LD_TF_STEP_TH = (unsigned int)val1;
                }
-               incr_dif_gain = (unsigned int)val1;
-               pr_info("set incr_dif_gain=%d\n", incr_dif_gain);
-       } else if (!strcmp(parm[0], "litgain")) {
+               pr_info("LD_TF_STEP_TH = %d\n", LD_TF_STEP_TH);
+       } else if (!strcmp(parm[0], "TF_step_method")) {
                if (parm[1] != NULL) {
-                       if (kstrtoul(parm[1], 10, &litgain) < 0)
+                       if (kstrtoul(parm[1], 10, &val1) < 0)
                                goto ldim_attr_store_end;
+                       TF_step_method = (unsigned int)val1;
                }
-               pr_info("set litgain=%ld\n", litgain);
-       } else if (!strcmp(parm[0], "boost_gain")) {
-               pr_info("now boost_gain = %ld\n", boost_gain);
+               pr_info("TF_step_method = %d\n", TF_step_method);
+       } else if (!strcmp(parm[0], "TF_FRESH_BL")) {
                if (parm[1] != NULL) {
-                       if (kstrtoul(parm[1], 10, &boost_gain) < 0)
+                       if (kstrtoul(parm[1], 10, &val1) < 0)
                                goto ldim_attr_store_end;
+                       TF_FRESH_BL = (unsigned int)val1;
                }
-               pr_info("set boost_gain=%ld\n", boost_gain);
-       } else if (!strcmp(parm[0], "alpha")) {
-               pr_info("now alpha = %ld\n", alpha);
+               pr_info("TF_FRESH_BL = %d\n", TF_FRESH_BL);
+       } else if (!strcmp(parm[0], "TF_BLK_FRESH_BL")) {
                if (parm[1] != NULL) {
-                       if (kstrtoul(parm[1], 10, &alpha) < 0)
+                       if (kstrtoul(parm[1], 10, &val1) < 0)
                                goto ldim_attr_store_end;
+                       TF_BLK_FRESH_BL = (unsigned int)val1;
                }
-               pr_info("set alpha = %ld\n", alpha);
-       } else if (!strcmp(parm[0], "alpha_delta")) {
-               pr_info("now alpha_delta = %ld\n", alpha_delta);
+               pr_info("TF_BLK_FRESH_BL = %d\n", TF_BLK_FRESH_BL);
+       } else if (!strcmp(parm[0], "bbd_detect_en")) {
                if (parm[1] != NULL) {
-                       if (kstrtoul(parm[1], 10, &alpha_delta) < 0)
+                       if (kstrtoul(parm[1], 10, &val1) < 0)
                                goto ldim_attr_store_end;
+                       bbd_detect_en = (unsigned char)val1;
                }
-               pr_info("set alpha_delta = %ld\n", alpha_delta);
-       } else if (!strcmp(parm[0], "boost_gain_neg")) {
-               pr_info("now boost_gain_neg = %ld\n", boost_gain_neg);
+               pr_info("bbd_detect_en = %d\n", bbd_detect_en);
+       } else if (!strcmp(parm[0], "side_blk_diff_th")) {
                if (parm[1] != NULL) {
-                       if (kstrtoul(parm[1], 10, &boost_gain_neg) < 0)
+                       if (kstrtoul(parm[1], 10, &val1) < 0)
                                goto ldim_attr_store_end;
+                       side_blk_diff_th = (unsigned int)val1;
                }
-               pr_info("set boost_gain_neg = %ld\n", boost_gain_neg);
-       } else if (!strcmp(parm[0], "Dbprint_lv")) {
+               pr_info("side_blk_diff_th = %d\n", side_blk_diff_th);
+       } else if (!strcmp(parm[0], "bbd_th")) {
                if (parm[1] != NULL) {
-                       if (kstrtoul(parm[1], 10, &Dbprint_lv) < 0)
+                       if (kstrtoul(parm[1], 10, &val1) < 0)
                                goto ldim_attr_store_end;
+                       bbd_th = (unsigned int)val1;
                }
-               pr_info("set Dbprint_lv=%ld\n", Dbprint_lv);
-       } else if (!strcmp(parm[0], "avg_gain")) {
+               pr_info("bbd_th = %d\n", bbd_th);
+       } else if (!strcmp(parm[0], "diff_blk_luma_en")) {
                if (parm[1] != NULL) {
-                       if (kstrtoul(parm[1], 10, &avg_gain) < 0)
+                       if (kstrtoul(parm[1], 10, &val1) < 0)
                                goto ldim_attr_store_end;
+                       diff_blk_luma_en = (unsigned char)val1;
                }
-               pr_info("set avg_gain=%ld\n", avg_gain);
+               pr_info("diff_blk_luma_en = %d\n", diff_blk_luma_en);
+       } else if (!strcmp(parm[0], "fw_hist_print")) {
+               if (parm[1] != NULL) {
+                       if (kstrtoul(parm[1], 10, &val1) < 0)
+                               goto ldim_attr_store_end;
+                       fw_hist_print = (unsigned char)val1;
+               }
+               pr_info("fw_hist_print = %d\n", fw_hist_print);
+       } else if (!strcmp(parm[0], "Debug")) {
+               if (parm[1] != NULL) {
+                       if (kstrtoul(parm[1], 10, &val1) < 0)
+                               goto ldim_attr_store_end;
+                       Debug = (unsigned char)val1;
+               }
+               pr_info("Debug = %d\n", Debug);
+       } else if (!strcmp(parm[0], "fw_print_frequent")) {
+               if (parm[1] != NULL) {
+                       if (kstrtoul(parm[1], 10, &val1) < 0)
+                               goto ldim_attr_store_end;
+                       fw_print_frequent = (unsigned int)val1;
+               }
+               pr_info("fw_print_frequent = %d\n", fw_print_frequent);
+       } else if (!strcmp(parm[0], "Dbprint_lv")) {
+               if (parm[1] != NULL) {
+                       if (kstrtoul(parm[1], 10, &val1) < 0)
+                               goto ldim_attr_store_end;
+                       Dbprint_lv = (unsigned int)val1;
+               }
+               pr_info("Dbprint_lv = %d\n", Dbprint_lv);
+       } else if (!strcmp(parm[0], "alg_para")) {
+               pr_info("ldim_alg_ver         = %s\n",
+                       LD_FW_ALG_FRM_VER);
+#if (LD_FW_ALG_FRM_SEL == LD_FW_ALG_FRM_V1)
+               pr_info("fw_LD_ThSF_l         = %ld\n"
+                       "fw_LD_ThTF_l         = %ld\n"
+                       "boost_gain           = %ld\n"
+                       "boost_gain_neg       = %ld\n"
+                       "alpha_delta          = %ld\n\n",
+                       fw_LD_ThSF_l,
+                       fw_LD_ThTF_l,
+                       boost_gain,
+                       boost_gain_neg,
+                       alpha_delta);
+#elif (LD_FW_ALG_FRM_SEL == LD_FW_ALG_FRM_V0)
+               pr_info("fw_LD_ThSF_l         = %ld\n"
+                       "fw_LD_ThTF_l         = %ld\n"
+                       "boost_gain           = %ld\n"
+                       "TF_alpha             = %ld\n"
+                       "lpf_gain             = %ld\n"
+                       "lpf_res              = %ld\n"
+                       "rgb_base             = %ld\n\n",
+                       fw_LD_ThSF_l,
+                       fw_LD_ThTF_l,
+                       boost_gain,
+                       TF_alpha,
+                       lpf_gain,
+                       lpf_res,
+                       rgb_base);
+#endif
+               pr_info("fw_rgb_diff_th       = %d\n"
+                       "max_luma             = %d\n"
+                       "lmh_avg_TH           = %d\n"
+                       "fw_TF_sum_th         = %d\n"
+                       "LPF_method           = %d\n"
+                       "LD_TF_STEP_TH        = %d\n"
+                       "TF_step_method       = %d\n"
+                       "TF_FRESH_BL          = %d\n\n",
+                       fw_rgb_diff_th,
+                       max_luma,
+                       lmh_avg_TH,
+                       fw_TF_sum_th,
+                       LPF_method,
+                       LD_TF_STEP_TH,
+                       TF_step_method,
+                       TF_FRESH_BL);
+               pr_info("TF_BLK_FRESH_BL      = %d\n"
+                       "side_blk_diff_th     = %d\n"
+                       "bbd_th               = %d\n"
+                       "bbd_detect_en        = %d\n"
+                       "diff_blk_luma_en     = %d\n\n",
+                       TF_BLK_FRESH_BL,
+                       side_blk_diff_th,
+                       bbd_th,
+                       bbd_detect_en,
+                       diff_blk_luma_en);
+               pr_info("ov_gain              = %d\n"
+                       "incr_dif_gain        = %d\n"
+                       "avg_gain             = %ld\n"
+                       "litgain              = %ld\n\n",
+                       ov_gain,
+                       incr_dif_gain,
+                       avg_gain,
+                       litgain);
+               pr_info("Sf_bypass            = %d\n"
+                       "Boost_light_bypass   = %d\n"
+                       "Lpf_bypass           = %d\n"
+                       "Ld_remap_bypass      = %d\n\n",
+                       Sf_bypass,
+                       Boost_light_bypass,
+                       Lpf_bypass,
+                       Ld_remap_bypass);
+               pr_info("fw_hist_print        = %d\n"
+                       "Debug                = %d\n"
+                       "fw_print_frequent    = %d\n"
+                       "Dbprint_lv           = %d\n\n",
+                       fw_hist_print,
+                       Debug,
+                       fw_print_frequent,
+                       Dbprint_lv);
        } else if (!strcmp(parm[0], "info")) {
+               pr_info("ldim_drv_ver          = %s\n",
+                       LDIM_DRV_VER);
                ldim_driver.config_print();
                pr_info("\nldim_blk_row          = %d\n"
                        "ldim_blk_col          = %d\n"
                        "ldim_hist_row         = %d\n"
                        "ldim_hist_col         = %d\n"
                        "ldim_bl_mode          = %d\n"
-                       "dev_index             = %d\n",
+                       "dev_index             = %d\n\n",
                        ldim_blk_row, ldim_blk_col,
                        ldim_hist_row, ldim_hist_col,
                        ldim_config.bl_mode,
                        ldim_driver.dev_index);
-               pr_info("\nldim_on_flag          = %d\n"
+               pr_info("ldim_on_flag          = %d\n"
                        "ldim_func_en          = %d\n"
                        "ldim_remap_en         = %d\n"
                        "ldim_func_bypass      = %d\n"
@@ -3237,6 +4037,13 @@ static ssize_t ldim_attr_store(struct class *cla,
                        "nPRM.reg_LD_pic_ColMax = %d\n",
                        nPRM.reg_LD_BLK_Hnum, nPRM.reg_LD_BLK_Vnum,
                        nPRM.reg_LD_pic_RowMax, nPRM.reg_LD_pic_ColMax);
+       } else if (!strcmp(parm[0], "print")) {
+               if (parm[1] != NULL) {
+                       if (kstrtoul(parm[1], 10, &val1) < 0)
+                               goto ldim_attr_store_end;
+                       ldim_debug_print = (unsigned char)val1;
+               }
+               pr_info("ldim_debug_print = %d\n", ldim_debug_print);
        } else
                pr_info("no support cmd!!!\n");
 
@@ -3452,14 +4259,13 @@ err1:
        kfree(ldim_driver.ldim_matrix_buf);
 err0:
        return -1;
-
 }
 
 int aml_ldim_probe(struct platform_device *pdev)
 {
        int ret = 0;
        unsigned int i;
-       unsigned int ldim_irq = 0, rdma_irq = 0;
+       unsigned int ldim_vsync_irq = 0, ldim_rdma_irq = 0;
        struct ldim_dev_s *devp = &ldim_dev;
        struct aml_bl_drv_s *bl_drv = aml_bl_get_driver();
 
@@ -3471,7 +4277,6 @@ int aml_ldim_probe(struct platform_device *pdev)
        ldim_test_en = 0;
 
        ldim_brightness_level = 0;
-       ldim_data_min = LD_DATA_MIN;
        ldim_level_update = 0;
 
        /* alg flag */
@@ -3480,8 +4285,6 @@ int aml_ldim_probe(struct platform_device *pdev)
        ldim_alg_en = 0;
        ldim_top_en = 0;
        ldim_hist_en = 0;
-       avg_gain = LD_DATA_MAX;
-       invalid_val_cnt = 0;
        Dbprint_lv = 0;
        db_cnt = 0;
 
@@ -3552,40 +4355,38 @@ int aml_ldim_probe(struct platform_device *pdev)
        spin_lock_init(&ldim_isr_lock);
        spin_lock_init(&rdma_ldim_isr_lock);
 
-       bl_drv->res_ldim_irq = platform_get_resource(pdev,
-               IORESOURCE_IRQ, 0);
-       if (!bl_drv->res_ldim_irq) {
+       bl_drv->res_ldim_vsync_irq =
+               platform_get_resource(pdev, IORESOURCE_IRQ, 0);
+       if (!bl_drv->res_ldim_vsync_irq) {
                ret = -ENODEV;
+               LDIMERR("ldim_vsync_irq resource error\n");
                goto err;
-       } else {
-               ldim_irq = bl_drv->res_ldim_irq->start;
-               LDIMPR("ldim_irq: %d\n", ldim_irq);
-               if (request_irq(ldim_irq, ldim_vsync_isr, IRQF_SHARED,
-                       "ldim_vsync", (void *)"ldim_vsync"))
-                       LDIMERR("can't request ldim_irq\n");
-               else
-                       LDIMPR("request ldim_irq successful\n");
        }
+       ldim_vsync_irq = bl_drv->res_ldim_vsync_irq->start;
+       LDIMPR("ldim_vsync_irq: %d\n", ldim_vsync_irq);
+       if (request_irq(ldim_vsync_irq, ldim_vsync_isr, IRQF_SHARED,
+               "ldim_vsync", (void *)"ldim_vsync"))
+               LDIMERR("can't request ldim_vsync_irq\n");
+       else
+               LDIMPR("request ldim_vsync_irq successful\n");
 
        switch (bl_drv->data->chip_type) {
        case BL_CHIP_GXTVBB:
-               bl_drv->res_rdma_irq = platform_get_resource(pdev,
+               bl_drv->res_ldim_rdma_irq = platform_get_resource(pdev,
                        IORESOURCE_IRQ, 1);
-               if (!bl_drv->res_rdma_irq) {
+               if (!bl_drv->res_ldim_rdma_irq) {
                        ret = -ENODEV;
+                       LDIMERR("ldim_rdma_irq resource error\n");
                        goto err;
-               } else {
-                       rdma_irq = bl_drv->res_rdma_irq->start;
-                       LDIMPR("rdma_irq: %d\n", rdma_irq);
-
-                       if (request_irq(rdma_irq, rdma_ldim_intr, IRQF_SHARED,
-                               "rdma_ldim", (void *)"rdma_ldim"))
-                               LDIMERR("can't request rdma_ldim\n");
-                       else
-                               LDIMPR("request rdma_ldim successful\n");
                }
+               ldim_rdma_irq = bl_drv->res_ldim_rdma_irq->start;
+               LDIMPR("ldim_rdma_irq: %d\n", ldim_rdma_irq);
+               if (request_irq(ldim_rdma_irq, rdma_ldim_intr, IRQF_SHARED,
+                       "ldim_rdma", (void *)"ldim_rdma"))
+                       LDIMERR("can't request ldim_rdma_irq\n");
+               else
+                       LDIMPR("request ldim_rdma_irq successful\n");
                break;
-       case BL_CHIP_TXLX:
        default:
                break;
        }
@@ -3628,8 +4429,8 @@ int aml_ldim_remove(void)
        kfree(ldim_driver.ldim_test_matrix);
        kfree(ldim_driver.local_ldim_matrix);
 
-       free_irq(bl_drv->res_rdma_irq->start, (void *)"rdma_ldim");
-       free_irq(bl_drv->res_ldim_irq->start, (void *)"ldim_vsync");
+       free_irq(bl_drv->res_ldim_rdma_irq->start, (void *)"ldim_rdma");
+       free_irq(bl_drv->res_ldim_vsync_irq->start, (void *)"ldim_vsync");
 
        cdev_del(devp->aml_ldim_cdevp);
        kfree(devp->aml_ldim_cdevp);
index 396e9520f06e97387753e9b885b518fbbed8affd..56fb088285025783124f66c02c88b3da4bd84c57 100644 (file)
@@ -32,7 +32,9 @@
 #define LDIMPR(fmt, args...)     pr_info("ldim: "fmt"", ## args)
 #define LDIMERR(fmt, args...)    pr_err("ldim: error: "fmt"", ## args)
 
-extern unsigned int ldim_debug_print;
+#define LDIM_DRV_VER    "20180629"
+
+extern unsigned char ldim_debug_print;
 
 /*** GXTVBB & TXLX common use register*/
 /* each base has 16 address space */
@@ -110,6 +112,7 @@ extern unsigned int ldim_debug_print;
 
 
 #define LD_DATA_DEPTH   12
+#define LD_DATA_MIN     10
 #define LD_DATA_MAX     0xfff
 
 struct LDReg {
index d5485f27a3132de6b241cd848031abfab33718d3..7d2b42a45fbb95d1cf056f0e44b0ff633208d92c 100644 (file)
@@ -617,6 +617,7 @@ void ldim_set_region(unsigned int resolution, unsigned int blk_height,
        }
 }
 
+static unsigned int invalid_val_cnt;
 void ldim_stts_read_region(unsigned int nrow, unsigned int ncol)
 {
        unsigned int i, j, k;
index 243c8f6eba85f7d34f5d5454e45d7f062046051e..c714f268a5485a34d2720429618491ab78972c9e 100644 (file)
@@ -18,8 +18,6 @@
 #include <linux/cdev.h>
 #include <linux/amlogic/iomap.h>
 
-extern unsigned int invalid_val_cnt;
-
 #define Wr_reg_bits(adr, val, start, len)  \
                aml_vcbus_update_bits(adr, ((1<<len)-1)<<start, val<<start)
 /*     #define Rd_reg_bits(adr, start, len)  \
index 00b36c6acb1184f53b30152f4ff4201dc52f7d84..f6b0d9c06cb14662063e5765b7214f425e655d0b 100644 (file)
@@ -181,8 +181,8 @@ struct aml_bl_drv_s {
        struct backlight_device   *bldev;
        struct workqueue_struct   *workqueue;
        struct delayed_work       bl_delayed_work;
-       struct resource *res_ldim_irq;
-       struct resource *res_rdma_irq;
+       struct resource *res_ldim_vsync_irq;
+       struct resource *res_ldim_rdma_irq;
 };
 
 extern struct aml_bl_drv_s *aml_bl_get_driver(void);
index 56278141df4335d5b49db1606e160aa191b41155..6840be98d01dfeae628f397de52b256cd34810b0 100644 (file)
 #include <linux/amlogic/media/vout/lcd/aml_bl.h>
 #include <linux/spi/spi.h>
 
+extern int  dirspi_write(struct spi_device *spi, u8 *buf, int len);
+extern int  dirspi_read(struct spi_device *spi, u8 *buf, int len);
+extern void dirspi_start(struct spi_device *spi);
+extern void dirspi_stop(struct spi_device *spi);
+
 #define _VE_LDIM  'C'
 
 /* VPP.ldim IOCTL command list */