Fix gain equation on Driver

This commit is contained in:
dchvs 2021-03-04 00:36:20 -06:00
parent fef47412d5
commit 7f1843d73d
1 changed files with 52 additions and 47 deletions

View File

@ -135,8 +135,8 @@
#define MT9M021_ANALOG_GAIN_SHIFT 4 #define MT9M021_ANALOG_GAIN_SHIFT 4
#define MT9M021_ANALOG_GAIN_MASK 0x0030 #define MT9M021_ANALOG_GAIN_MASK 0x0030
#define MT9M021_GLOBAL_GAIN_MIN 4 #define MT9M021_GLOBAL_GAIN_MIN 100
#define MT9M021_GLOBAL_GAIN_MAX 6476 #define MT9M021_GLOBAL_GAIN_MAX 797
#define MT9M021_GLOBAL_GAIN_DEF 100 #define MT9M021_GLOBAL_GAIN_DEF 100
#define MT9M021_COARSE_INT_TIME_MIN 0x0001 #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 camera_common_data *s_data = tc_dev->s_data;
struct device *dev = tc_dev->dev; struct device *dev = tc_dev->dev;
int err = 0; int err = 0;
u16 gain_mul; u16 gain_mul;
u16 reg16 = 0; u16 reg16 = 0;
u8 integer, fraction; u64 gain = 0;
dev_dbg(dev, "Setting Gain to: %lld", val); dev_dbg(dev, "Setting Gain to: %lld", val);
if (val >= MT9M021_GAIN_8X_FIXED) { if (val >= MT9M021_GAIN_8X_FIXED) {
integer = val / MT9M021_GAIN_8X_FIXED; gain_mul = MT9M021_GAIN_8X;
fraction = ((val / 8) % 100) * 32 / 100; } else if (val >= MT9M021_GAIN_4X_FIXED) {
gain_mul = MT9M021_GAIN_8X; gain_mul = MT9M021_GAIN_4X;
} else if (val >= MT9M021_GAIN_4X_FIXED) { } else if (val >= MT9M021_GAIN_2X_FIXED) {
integer = val / MT9M021_GAIN_4X_FIXED; gain_mul = MT9M021_GAIN_2X;
fraction = ((val / 4) % 100) * 32 / 100; } else {
gain_mul = MT9M021_GAIN_4X; gain_mul = MT9M021_GAIN_1X;
} 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;
}
/* Update analog gain multiplier */ /*
err = mt9m021_read_reg16(s_data, MT9M021_DIGITAL_TEST, &reg16); * Digital gain equation:
if (err) *
goto exit; * RANGE: 1x, 7.97x
reg16 = * GAIN: VAL / STEPS;
(reg16 & ~MT9M021_ANALOG_GAIN_MASK) | * STEPS: 1/32
((gain_mul << MT9M021_ANALOG_GAIN_SHIFT) & *
MT9M021_ANALOG_GAIN_MASK); * SCALE FACTOR = 100.000
err = mt9m021_write_reg16(s_data, MT9M021_DIGITAL_TEST, reg16); *
msleep(10); * min_gain_val = 100.000
if (err) * max_gain_val = 797.000
goto exit; * step_gain_val = 3125
*/
gain = val / 3125;
/* Update analog gain multiplier */
err = mt9m021_read_reg16(s_data, MT9M021_DIGITAL_TEST, &reg16);
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 */ /* Update global gain */
err = err =
mt9m021_write_reg16(s_data, MT9M021_GLOBAL_GAIN, mt9m021_write_reg16(s_data, MT9M021_GLOBAL_GAIN, gain);
(integer << 5) | fraction); msleep(10);
msleep(10);
if (err) if (err)
goto exit; goto exit;
err = err =
mt9m021_write_reg16(s_data, MT9M021_GLOBAL_GAIN_CB, mt9m021_write_reg16(s_data, MT9M021_GLOBAL_GAIN_CB, gain);
(integer << 5) | fraction);
if (err) if (err)
goto exit; 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); 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) if (err)
return err; return err;
err = mt9m021_write_reg16(s_data, MT9M021_COARSE_INT_TIME_CB, val); 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 mt9m021 *priv = (struct mt9m021 *)tc_dev->priv;
struct device *dev = tc_dev->dev; struct device *dev = tc_dev->dev;
const struct sensor_mode_properties *mode = 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; u64 coarse_time;
int err = 0; 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 =
coarse_time = mode->signal_properties.pixel_clock.val * val * mode->signal_properties.pixel_clock.val /
val / mode->image_properties.line_length / mode->image_properties.line_length /
mode->control_properties.exposure_factor; mode->control_properties.exposure_factor;
err = mt9m021_set_coarse_time(priv, coarse_time); err = mt9m021_set_coarse_time(priv, coarse_time);
if (err) if (err)