From 7f1843d73d6a5eec2b530bc93b19e4380fac70e5 Mon Sep 17 00:00:00 2001 From: dchvs Date: Thu, 4 Mar 2021 00:36:20 -0600 Subject: [PATCH] Fix gain equation on Driver --- .../nvidia-spiri/drivers/media/i2c/mt9m021.c | 99 ++++++++++--------- 1 file changed, 52 insertions(+), 47 deletions(-) diff --git a/kernel/nvidia-spiri/drivers/media/i2c/mt9m021.c b/kernel/nvidia-spiri/drivers/media/i2c/mt9m021.c index e34291e..33e57c8 100644 --- a/kernel/nvidia-spiri/drivers/media/i2c/mt9m021.c +++ b/kernel/nvidia-spiri/drivers/media/i2c/mt9m021.c @@ -135,8 +135,8 @@ #define MT9M021_ANALOG_GAIN_SHIFT 4 #define MT9M021_ANALOG_GAIN_MASK 0x0030 -#define MT9M021_GLOBAL_GAIN_MIN 4 -#define MT9M021_GLOBAL_GAIN_MAX 6476 +#define MT9M021_GLOBAL_GAIN_MIN 100 +#define MT9M021_GLOBAL_GAIN_MAX 797 #define MT9M021_GLOBAL_GAIN_DEF 100 #define MT9M021_COARSE_INT_TIME_MIN 0x0001 @@ -396,54 +396,59 @@ static int mt9m021_set_gain(struct tegracam_device *tc_dev, s64 val) { struct camera_common_data *s_data = tc_dev->s_data; struct device *dev = tc_dev->dev; - int err = 0; - u16 gain_mul; - u16 reg16 = 0; - u8 integer, fraction; + int err = 0; + u16 gain_mul; + u16 reg16 = 0; + u64 gain = 0; dev_dbg(dev, "Setting Gain to: %lld", val); - if (val >= MT9M021_GAIN_8X_FIXED) { - integer = val / MT9M021_GAIN_8X_FIXED; - fraction = ((val / 8) % 100) * 32 / 100; - gain_mul = MT9M021_GAIN_8X; - } else if (val >= MT9M021_GAIN_4X_FIXED) { - integer = val / MT9M021_GAIN_4X_FIXED; - fraction = ((val / 4) % 100) * 32 / 100; - gain_mul = MT9M021_GAIN_4X; - } else if (val >= MT9M021_GAIN_2X_FIXED) { - integer = val / MT9M021_GAIN_2X_FIXED; - fraction = ((val / 2) % 100) * 32 / 100; - gain_mul = MT9M021_GAIN_2X; - } else { - integer = val / MT9M021_GAIN_1X_FIXED; - fraction = (val % 100) * 32 / 100; - gain_mul = MT9M021_GAIN_1X; - } + if (val >= MT9M021_GAIN_8X_FIXED) { + gain_mul = MT9M021_GAIN_8X; + } else if (val >= MT9M021_GAIN_4X_FIXED) { + gain_mul = MT9M021_GAIN_4X; + } else if (val >= MT9M021_GAIN_2X_FIXED) { + gain_mul = MT9M021_GAIN_2X; + } else { + gain_mul = MT9M021_GAIN_1X; + } - /* Update analog gain multiplier */ - err = mt9m021_read_reg16(s_data, MT9M021_DIGITAL_TEST, ®16); - if (err) - goto exit; - reg16 = - (reg16 & ~MT9M021_ANALOG_GAIN_MASK) | - ((gain_mul << MT9M021_ANALOG_GAIN_SHIFT) & - MT9M021_ANALOG_GAIN_MASK); - err = mt9m021_write_reg16(s_data, MT9M021_DIGITAL_TEST, reg16); - msleep(10); - if (err) - goto exit; + /* + * Digital gain equation: + * + * RANGE: 1x, 7.97x + * GAIN: VAL / STEPS; + * STEPS: 1/32 + * + * SCALE FACTOR = 100.000 + * + * min_gain_val = 100.000 + * max_gain_val = 797.000 + * step_gain_val = 3125 + */ + gain = val / 3125; + + /* Update analog gain multiplier */ + err = mt9m021_read_reg16(s_data, MT9M021_DIGITAL_TEST, ®16); + if (err) + goto exit; + reg16 = + (reg16 & ~MT9M021_ANALOG_GAIN_MASK) | + ((gain_mul << MT9M021_ANALOG_GAIN_SHIFT) & + MT9M021_ANALOG_GAIN_MASK); + err = mt9m021_write_reg16(s_data, MT9M021_DIGITAL_TEST, reg16); + msleep(10); + if (err) + goto exit; /* Update global gain */ err = - mt9m021_write_reg16(s_data, MT9M021_GLOBAL_GAIN, - (integer << 5) | fraction); - msleep(10); + mt9m021_write_reg16(s_data, MT9M021_GLOBAL_GAIN, gain); + msleep(10); if (err) goto exit; err = - mt9m021_write_reg16(s_data, MT9M021_GLOBAL_GAIN_CB, - (integer << 5) | fraction); + mt9m021_write_reg16(s_data, MT9M021_GLOBAL_GAIN_CB, gain); if (err) goto exit; @@ -507,7 +512,7 @@ static int mt9m021_set_coarse_time(struct mt9m021 *priv, s64 val) dev_dbg(dev, "Setting Coarse Time to: %lld", val); - err = mt9m021_write_reg16(s_data, MT9M021_COARSE_INT_TIME, val); + err = mt9m021_write_reg16(s_data, MT9M021_COARSE_INT_TIME, val); if (err) return err; err = mt9m021_write_reg16(s_data, MT9M021_COARSE_INT_TIME_CB, val); @@ -524,16 +529,16 @@ static int mt9m021_set_exposure(struct tegracam_device *tc_dev, s64 val) struct mt9m021 *priv = (struct mt9m021 *)tc_dev->priv; struct device *dev = tc_dev->dev; const struct sensor_mode_properties *mode = - &s_data->sensor_props.sensor_modes[s_data->mode]; + &s_data->sensor_props.sensor_modes[s_data->mode]; u64 coarse_time; int err = 0; - dev_dbg(dev, "Setting Exposure Time to: %lld", val); + dev_dbg(dev, "Setting Exposure Time to : %lld", val); - /* Calculate coarse-time */ - coarse_time = mode->signal_properties.pixel_clock.val * - val / mode->image_properties.line_length / - mode->control_properties.exposure_factor; + coarse_time = + val * mode->signal_properties.pixel_clock.val / + mode->image_properties.line_length / + mode->control_properties.exposure_factor; err = mt9m021_set_coarse_time(priv, coarse_time); if (err)