Commit c286a3a0 authored by Mauro Carvalho Chehab's avatar Mauro Carvalho Chehab

media: atomisp-gc2235: use v4l2_find_nearest_size()

Instead of reinventing the wheel, use v4l2_find_nearest_size()
in order to get the closest resolution.

This should address a bug where the wrong resolution was
selected.
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab+huawei@kernel.org>
parent e3b14bf8
...@@ -670,76 +670,6 @@ static int gc2235_s_power(struct v4l2_subdev *sd, int on) ...@@ -670,76 +670,6 @@ static int gc2235_s_power(struct v4l2_subdev *sd, int on)
return ret; return ret;
} }
/*
* distance - calculate the distance
* @res: resolution
* @w: width
* @h: height
*
* Get the gap between resolution and w/h.
* res->width/height smaller than w/h wouldn't be considered.
* Returns the value of gap or -1 if fail.
*/
#define LARGEST_ALLOWED_RATIO_MISMATCH 800
static int distance(struct gc2235_resolution *res, u32 w, u32 h)
{
unsigned int w_ratio = (res->width << 13) / w;
unsigned int h_ratio;
int match;
if (h == 0)
return -1;
h_ratio = (res->height << 13) / h;
if (h_ratio == 0)
return -1;
match = abs(((w_ratio << 13) / h_ratio) - 8192);
if ((w_ratio < 8192) || (h_ratio < 8192) ||
(match > LARGEST_ALLOWED_RATIO_MISMATCH))
return -1;
return w_ratio + h_ratio;
}
/* Return the nearest higher resolution index */
static int nearest_resolution_index(int w, int h)
{
int i;
int idx = -1;
int dist;
int min_dist = INT_MAX;
struct gc2235_resolution *tmp_res = NULL;
for (i = 0; i < N_RES; i++) {
tmp_res = &gc2235_res[i];
dist = distance(tmp_res, w, h);
if (dist == -1)
continue;
if (dist < min_dist) {
min_dist = dist;
idx = i;
}
}
return idx;
}
static int get_resolution_index(int w, int h)
{
int i;
for (i = 0; i < N_RES; i++) {
if (w != gc2235_res[i].width)
continue;
if (h != gc2235_res[i].height)
continue;
return i;
}
return -1;
}
static int startup(struct v4l2_subdev *sd) static int startup(struct v4l2_subdev *sd)
{ {
struct gc2235_device *dev = to_gc2235_sensor(sd); struct gc2235_device *dev = to_gc2235_sensor(sd);
...@@ -758,7 +688,7 @@ static int startup(struct v4l2_subdev *sd) ...@@ -758,7 +688,7 @@ static int startup(struct v4l2_subdev *sd)
gc2235_write_reg_array(client, gc2235_init_settings); gc2235_write_reg_array(client, gc2235_init_settings);
} }
ret = gc2235_write_reg_array(client, gc2235_res[dev->fmt_idx].regs); ret = gc2235_write_reg_array(client, dev->res->regs);
if (ret) { if (ret) {
dev_err(&client->dev, "gc2235 write register err.\n"); dev_err(&client->dev, "gc2235 write register err.\n");
return ret; return ret;
...@@ -776,8 +706,8 @@ static int gc2235_set_fmt(struct v4l2_subdev *sd, ...@@ -776,8 +706,8 @@ static int gc2235_set_fmt(struct v4l2_subdev *sd,
struct gc2235_device *dev = to_gc2235_sensor(sd); struct gc2235_device *dev = to_gc2235_sensor(sd);
struct i2c_client *client = v4l2_get_subdevdata(sd); struct i2c_client *client = v4l2_get_subdevdata(sd);
struct camera_mipi_info *gc2235_info = NULL; struct camera_mipi_info *gc2235_info = NULL;
struct gc2235_resolution *res;
int ret = 0; int ret = 0;
int idx;
gc2235_info = v4l2_get_subdev_hostdata(sd); gc2235_info = v4l2_get_subdev_hostdata(sd);
if (!gc2235_info) if (!gc2235_info)
...@@ -786,16 +716,18 @@ static int gc2235_set_fmt(struct v4l2_subdev *sd, ...@@ -786,16 +716,18 @@ static int gc2235_set_fmt(struct v4l2_subdev *sd,
return -EINVAL; return -EINVAL;
if (!fmt) if (!fmt)
return -EINVAL; return -EINVAL;
mutex_lock(&dev->input_lock); mutex_lock(&dev->input_lock);
idx = nearest_resolution_index(fmt->width, fmt->height); res = v4l2_find_nearest_size(gc2235_res_preview,
if (idx == -1) { ARRAY_SIZE(gc2235_res_preview), width,
/* return the largest resolution */ height, fmt->width, fmt->height);
fmt->width = gc2235_res[N_RES - 1].width; if (!res)
fmt->height = gc2235_res[N_RES - 1].height; res = &gc2235_res_preview[N_RES - 1];
} else {
fmt->width = gc2235_res[idx].width; fmt->width = res->width;
fmt->height = gc2235_res[idx].height; fmt->height = res->height;
} dev->res = res;
fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10; fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
if (format->which == V4L2_SUBDEV_FORMAT_TRY) { if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
sd_state->pads->try_fmt = *fmt; sd_state->pads->try_fmt = *fmt;
...@@ -803,13 +735,6 @@ static int gc2235_set_fmt(struct v4l2_subdev *sd, ...@@ -803,13 +735,6 @@ static int gc2235_set_fmt(struct v4l2_subdev *sd,
return 0; return 0;
} }
dev->fmt_idx = get_resolution_index(fmt->width, fmt->height);
if (dev->fmt_idx == -1) {
dev_err(&client->dev, "get resolution fail\n");
mutex_unlock(&dev->input_lock);
return -EINVAL;
}
ret = startup(sd); ret = startup(sd);
if (ret) { if (ret) {
dev_err(&client->dev, "gc2235 startup err\n"); dev_err(&client->dev, "gc2235 startup err\n");
...@@ -817,7 +742,7 @@ static int gc2235_set_fmt(struct v4l2_subdev *sd, ...@@ -817,7 +742,7 @@ static int gc2235_set_fmt(struct v4l2_subdev *sd,
} }
ret = gc2235_get_intg_factor(client, gc2235_info, ret = gc2235_get_intg_factor(client, gc2235_info,
&gc2235_res[dev->fmt_idx]); dev->res);
if (ret) if (ret)
dev_err(&client->dev, "failed to get integration_factor\n"); dev_err(&client->dev, "failed to get integration_factor\n");
...@@ -839,8 +764,8 @@ static int gc2235_get_fmt(struct v4l2_subdev *sd, ...@@ -839,8 +764,8 @@ static int gc2235_get_fmt(struct v4l2_subdev *sd,
if (!fmt) if (!fmt)
return -EINVAL; return -EINVAL;
fmt->width = gc2235_res[dev->fmt_idx].width; fmt->width = dev->res->width;
fmt->height = gc2235_res[dev->fmt_idx].height; fmt->height = dev->res->height;
fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10; fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
return 0; return 0;
...@@ -953,7 +878,7 @@ static int gc2235_g_frame_interval(struct v4l2_subdev *sd, ...@@ -953,7 +878,7 @@ static int gc2235_g_frame_interval(struct v4l2_subdev *sd,
struct gc2235_device *dev = to_gc2235_sensor(sd); struct gc2235_device *dev = to_gc2235_sensor(sd);
interval->interval.numerator = 1; interval->interval.numerator = 1;
interval->interval.denominator = gc2235_res[dev->fmt_idx].fps; interval->interval.denominator = dev->res->fps;
return 0; return 0;
} }
...@@ -991,7 +916,7 @@ static int gc2235_g_skip_frames(struct v4l2_subdev *sd, u32 *frames) ...@@ -991,7 +916,7 @@ static int gc2235_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
struct gc2235_device *dev = to_gc2235_sensor(sd); struct gc2235_device *dev = to_gc2235_sensor(sd);
mutex_lock(&dev->input_lock); mutex_lock(&dev->input_lock);
*frames = gc2235_res[dev->fmt_idx].skip_frames; *frames = dev->res->skip_frames;
mutex_unlock(&dev->input_lock); mutex_unlock(&dev->input_lock);
return 0; return 0;
...@@ -1055,7 +980,7 @@ static int gc2235_probe(struct i2c_client *client) ...@@ -1055,7 +980,7 @@ static int gc2235_probe(struct i2c_client *client)
mutex_init(&dev->input_lock); mutex_init(&dev->input_lock);
dev->fmt_idx = 0; dev->res = &gc2235_res_preview[0];
v4l2_i2c_subdev_init(&dev->sd, client, &gc2235_ops); v4l2_i2c_subdev_init(&dev->sd, client, &gc2235_ops);
gcpdev = gmin_camera_platform_data(&dev->sd, gcpdev = gmin_camera_platform_data(&dev->sd,
......
...@@ -158,11 +158,10 @@ struct gc2235_device { ...@@ -158,11 +158,10 @@ struct gc2235_device {
struct v4l2_mbus_framefmt format; struct v4l2_mbus_framefmt format;
struct mutex input_lock; struct mutex input_lock;
struct v4l2_ctrl_handler ctrl_handler; struct v4l2_ctrl_handler ctrl_handler;
struct gc2235_resolution *res;
struct camera_sensor_platform_data *platform_data; struct camera_sensor_platform_data *platform_data;
int vt_pix_clk_freq_mhz; int vt_pix_clk_freq_mhz;
int fmt_idx;
u8 res;
u8 type; u8 type;
}; };
......
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