Commit 644fefe4 authored by Hugues Fruchet's avatar Hugues Fruchet Committed by Greg Kroah-Hartman

media: ov5640: fix auto gain & exposure when changing mode

commit 3cca8ef5 upstream.

Ensure that auto gain and auto exposure are well restored
when changing mode.
Signed-off-by: default avatarHugues Fruchet <hugues.fruchet@st.com>
Reviewed-by: default avatarJacopo Mondi <jacopo@jmondi.org>
Tested-by: default avatarJacopo Mondi <jacopo@jmondi.org>
Signed-off-by: default avatarSakari Ailus <sakari.ailus@linux.intel.com>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab+samsung@kernel.org>
Signed-off-by: default avatarAdam Ford <aford173@gmail.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent f8248ad5
...@@ -1022,6 +1022,18 @@ static int ov5640_get_gain(struct ov5640_dev *sensor) ...@@ -1022,6 +1022,18 @@ static int ov5640_get_gain(struct ov5640_dev *sensor)
return gain & 0x3ff; return gain & 0x3ff;
} }
static int ov5640_set_gain(struct ov5640_dev *sensor, int gain)
{
return ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN,
(u16)gain & 0x3ff);
}
static int ov5640_set_autogain(struct ov5640_dev *sensor, bool on)
{
return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
BIT(1), on ? 0 : BIT(1));
}
static int ov5640_set_stream_dvp(struct ov5640_dev *sensor, bool on) static int ov5640_set_stream_dvp(struct ov5640_dev *sensor, bool on)
{ {
int ret; int ret;
...@@ -1588,7 +1600,7 @@ static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor, ...@@ -1588,7 +1600,7 @@ static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor,
} }
/* set capture gain */ /* set capture gain */
ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.gain, cap_gain16); ret = ov5640_set_gain(sensor, cap_gain16);
if (ret) if (ret)
return ret; return ret;
...@@ -1601,7 +1613,7 @@ static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor, ...@@ -1601,7 +1613,7 @@ static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor,
} }
/* set exposure */ /* set exposure */
return __v4l2_ctrl_s_ctrl(sensor->ctrls.exposure, cap_shutter); return ov5640_set_exposure(sensor, cap_shutter);
} }
/* /*
...@@ -1609,26 +1621,13 @@ static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor, ...@@ -1609,26 +1621,13 @@ static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor,
* change mode directly * change mode directly
*/ */
static int ov5640_set_mode_direct(struct ov5640_dev *sensor, static int ov5640_set_mode_direct(struct ov5640_dev *sensor,
const struct ov5640_mode_info *mode, const struct ov5640_mode_info *mode)
bool auto_exp)
{ {
int ret;
if (!mode->reg_data) if (!mode->reg_data)
return -EINVAL; return -EINVAL;
/* Write capture setting */ /* Write capture setting */
ret = ov5640_load_regs(sensor, mode); return ov5640_load_regs(sensor, mode);
if (ret < 0)
return ret;
/* turn auto gain/exposure back on for direct mode */
ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_gain, 1);
if (ret)
return ret;
return __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_exp, auto_exp ?
V4L2_EXPOSURE_AUTO : V4L2_EXPOSURE_MANUAL);
} }
static int ov5640_set_mode(struct ov5640_dev *sensor) static int ov5640_set_mode(struct ov5640_dev *sensor)
...@@ -1636,6 +1635,7 @@ static int ov5640_set_mode(struct ov5640_dev *sensor) ...@@ -1636,6 +1635,7 @@ static int ov5640_set_mode(struct ov5640_dev *sensor)
const struct ov5640_mode_info *mode = sensor->current_mode; const struct ov5640_mode_info *mode = sensor->current_mode;
const struct ov5640_mode_info *orig_mode = sensor->last_mode; const struct ov5640_mode_info *orig_mode = sensor->last_mode;
enum ov5640_downsize_mode dn_mode, orig_dn_mode; enum ov5640_downsize_mode dn_mode, orig_dn_mode;
bool auto_gain = sensor->ctrls.auto_gain->val == 1;
bool auto_exp = sensor->ctrls.auto_exp->val == V4L2_EXPOSURE_AUTO; bool auto_exp = sensor->ctrls.auto_exp->val == V4L2_EXPOSURE_AUTO;
int ret; int ret;
...@@ -1643,19 +1643,23 @@ static int ov5640_set_mode(struct ov5640_dev *sensor) ...@@ -1643,19 +1643,23 @@ static int ov5640_set_mode(struct ov5640_dev *sensor)
orig_dn_mode = orig_mode->dn_mode; orig_dn_mode = orig_mode->dn_mode;
/* auto gain and exposure must be turned off when changing modes */ /* auto gain and exposure must be turned off when changing modes */
ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_gain, 0); if (auto_gain) {
if (ret) ret = ov5640_set_autogain(sensor, false);
return ret; if (ret)
return ret;
}
ret = ov5640_set_autoexposure(sensor, false); if (auto_exp) {
if (ret) ret = ov5640_set_autoexposure(sensor, false);
return ret; if (ret)
goto restore_auto_gain;
}
if ((dn_mode == SUBSAMPLING && orig_dn_mode == SCALING) || if ((dn_mode == SUBSAMPLING && orig_dn_mode == SCALING) ||
(dn_mode == SCALING && orig_dn_mode == SUBSAMPLING)) { (dn_mode == SCALING && orig_dn_mode == SUBSAMPLING)) {
/* /*
* change between subsampling and scaling * change between subsampling and scaling
* go through exposure calucation * go through exposure calculation
*/ */
ret = ov5640_set_mode_exposure_calc(sensor, mode); ret = ov5640_set_mode_exposure_calc(sensor, mode);
} else { } else {
...@@ -1663,11 +1667,16 @@ static int ov5640_set_mode(struct ov5640_dev *sensor) ...@@ -1663,11 +1667,16 @@ static int ov5640_set_mode(struct ov5640_dev *sensor)
* change inside subsampling or scaling * change inside subsampling or scaling
* download firmware directly * download firmware directly
*/ */
ret = ov5640_set_mode_direct(sensor, mode, auto_exp); ret = ov5640_set_mode_direct(sensor, mode);
} }
if (ret < 0) if (ret < 0)
return ret; goto restore_auto_exp_gain;
/* restore auto gain and exposure */
if (auto_gain)
ov5640_set_autogain(sensor, true);
if (auto_exp)
ov5640_set_autoexposure(sensor, true);
ret = ov5640_set_binning(sensor, dn_mode != SCALING); ret = ov5640_set_binning(sensor, dn_mode != SCALING);
if (ret < 0) if (ret < 0)
...@@ -1689,6 +1698,15 @@ static int ov5640_set_mode(struct ov5640_dev *sensor) ...@@ -1689,6 +1698,15 @@ static int ov5640_set_mode(struct ov5640_dev *sensor)
sensor->last_mode = mode; sensor->last_mode = mode;
return 0; return 0;
restore_auto_exp_gain:
if (auto_exp)
ov5640_set_autoexposure(sensor, true);
restore_auto_gain:
if (auto_gain)
ov5640_set_autogain(sensor, true);
return ret;
} }
static int ov5640_set_framefmt(struct ov5640_dev *sensor, static int ov5640_set_framefmt(struct ov5640_dev *sensor,
...@@ -2201,20 +2219,20 @@ static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb) ...@@ -2201,20 +2219,20 @@ static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb)
return ret; return ret;
} }
static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor, int exp) static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor,
enum v4l2_exposure_auto_type auto_exposure)
{ {
struct ov5640_ctrls *ctrls = &sensor->ctrls; struct ov5640_ctrls *ctrls = &sensor->ctrls;
bool auto_exposure = (exp == V4L2_EXPOSURE_AUTO); bool auto_exp = (auto_exposure == V4L2_EXPOSURE_AUTO);
int ret = 0; int ret = 0;
if (ctrls->auto_exp->is_new) { if (ctrls->auto_exp->is_new) {
ret = ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL, ret = ov5640_set_autoexposure(sensor, auto_exp);
BIT(0), auto_exposure ? 0 : BIT(0));
if (ret) if (ret)
return ret; return ret;
} }
if (!auto_exposure && ctrls->exposure->is_new) { if (!auto_exp && ctrls->exposure->is_new) {
u16 max_exp; u16 max_exp;
ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS, ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS,
...@@ -2234,25 +2252,19 @@ static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor, int exp) ...@@ -2234,25 +2252,19 @@ static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor, int exp)
return ret; return ret;
} }
static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, int auto_gain) static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, bool auto_gain)
{ {
struct ov5640_ctrls *ctrls = &sensor->ctrls; struct ov5640_ctrls *ctrls = &sensor->ctrls;
int ret = 0; int ret = 0;
if (ctrls->auto_gain->is_new) { if (ctrls->auto_gain->is_new) {
ret = ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL, ret = ov5640_set_autogain(sensor, auto_gain);
BIT(1),
ctrls->auto_gain->val ? 0 : BIT(1));
if (ret) if (ret)
return ret; return ret;
} }
if (!auto_gain && ctrls->gain->is_new) { if (!auto_gain && ctrls->gain->is_new)
u16 gain = (u16)ctrls->gain->val; ret = ov5640_set_gain(sensor, ctrls->gain->val);
ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN,
gain & 0x3ff);
}
return ret; return ret;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment