1 From 65e773354de288401a6c498d006b47b7f87e0936 Mon Sep 17 00:00:00 2001
2 From: Dave Stevenson <dave.stevenson@raspberrypi.com>
3 Date: Thu, 17 Feb 2022 17:00:27 +0000
4 Subject: [PATCH] media: i2c: ov7251: Separate modes from frame
7 The modes and frame intervals are independent, therefore
8 separate them into 2 structures.
10 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
12 drivers/media/i2c/ov7251.c | 85 ++++++++++++++++++--------------------
13 1 file changed, 41 insertions(+), 44 deletions(-)
15 --- a/drivers/media/i2c/ov7251.c
16 +++ b/drivers/media/i2c/ov7251.c
17 @@ -79,14 +79,17 @@ struct reg_value {
21 +struct ov7251_frame_ival_info {
23 + struct v4l2_fract timeperframe;
26 struct ov7251_mode_info {
29 const struct reg_value *data;
33 - struct v4l2_fract timeperframe;
36 struct ov7251_pll1_config {
37 @@ -128,6 +131,7 @@ struct ov7251 {
38 struct regulator *analog_regulator;
40 const struct ov7251_mode_info *current_mode;
41 + const struct ov7251_frame_ival_info *current_ival;
43 struct v4l2_ctrl_handler ctrls;
44 struct v4l2_ctrl *exposure;
45 @@ -343,13 +347,8 @@ static const s64 link_freq[] = {
49 -static const struct ov7251_mode_info ov7251_mode_info_data[] = {
50 +static const struct ov7251_frame_ival_info ov7251_frame_ival_info_data[] = {
54 - .data = ov7251_setting_vga,
55 - .data_size = ARRAY_SIZE(ov7251_setting_vga),
56 - .exposure_def = 504,
60 @@ -357,11 +356,6 @@ static const struct ov7251_mode_info ov7
66 - .data = ov7251_setting_vga,
67 - .data_size = ARRAY_SIZE(ov7251_setting_vga),
68 - .exposure_def = 504,
72 @@ -369,11 +363,6 @@ static const struct ov7251_mode_info ov7
78 - .data = ov7251_setting_vga,
79 - .data_size = ARRAY_SIZE(ov7251_setting_vga),
80 - .exposure_def = 504,
84 @@ -382,6 +371,16 @@ static const struct ov7251_mode_info ov7
88 +static const struct ov7251_mode_info ov7251_mode_info_data[] = {
92 + .data = ov7251_setting_vga,
93 + .data_size = ARRAY_SIZE(ov7251_setting_vga),
94 + .exposure_def = 504,
98 static int ov7251_regulators_enable(struct ov7251 *ov7251)
101 @@ -789,13 +788,13 @@ static int ov7251_enum_frame_ival(struct
102 unsigned int index = fie->index;
105 - for (i = 0; i < ARRAY_SIZE(ov7251_mode_info_data); i++) {
106 - if (fie->width != ov7251_mode_info_data[i].width ||
107 - fie->height != ov7251_mode_info_data[i].height)
108 + for (i = 0; i < ARRAY_SIZE(ov7251_frame_ival_info_data); i++) {
109 + if (fie->width != ov7251_mode_info_data[0].width ||
110 + fie->height != ov7251_mode_info_data[0].height)
114 - fie->interval = ov7251_mode_info_data[i].timeperframe;
115 + fie->interval = ov7251_frame_ival_info_data[i].timeperframe;
119 @@ -854,23 +853,18 @@ static inline u32 avg_fps(const struct v
120 return (t->denominator + (t->numerator >> 1)) / t->numerator;
123 -static const struct ov7251_mode_info *
124 -ov7251_find_mode_by_ival(struct ov7251 *ov7251, struct v4l2_fract *timeperframe)
125 +static const struct ov7251_frame_ival_info *
126 +ov7251_find_frame_ival_by_ival(struct ov7251 *ov7251, struct v4l2_fract *timeperframe)
128 - const struct ov7251_mode_info *mode = ov7251->current_mode;
129 unsigned int fps_req = avg_fps(timeperframe);
130 unsigned int max_dist_match = (unsigned int) -1;
131 unsigned int i, n = 0;
133 - for (i = 0; i < ARRAY_SIZE(ov7251_mode_info_data); i++) {
134 + for (i = 0; i < ARRAY_SIZE(ov7251_frame_ival_info_data); i++) {
136 unsigned int fps_tmp;
138 - if (mode->width != ov7251_mode_info_data[i].width ||
139 - mode->height != ov7251_mode_info_data[i].height)
142 - fps_tmp = avg_fps(&ov7251_mode_info_data[i].timeperframe);
143 + fps_tmp = avg_fps(&ov7251_frame_ival_info_data[i].timeperframe);
145 dist = abs(fps_req - fps_tmp);
147 @@ -880,7 +874,7 @@ ov7251_find_mode_by_ival(struct ov7251 *
151 - return &ov7251_mode_info_data[n];
152 + return &ov7251_frame_ival_info_data[n];
155 static int ov7251_set_format(struct v4l2_subdev *sd,
156 @@ -914,7 +908,8 @@ static int ov7251_set_format(struct v4l2
157 __v4l2_ctrl_s_ctrl(ov7251->hblank, h_blank);
159 ret = __v4l2_ctrl_modify_range(ov7251->exposure, 1,
160 - new_mode->vts - OV7251_EXPOSURE_OFFSET,
161 + ov7251->current_ival->vts -
162 + OV7251_EXPOSURE_OFFSET,
163 1, new_mode->exposure_def);
166 @@ -1036,11 +1031,11 @@ static int ov7251_s_stream(struct v4l2_s
169 ret = ov7251_write_reg(ov7251, OV7251_VTS_HIGH,
170 - ov7251->current_mode->vts >> 8);
171 + ov7251->current_ival->vts >> 8);
174 ret = ov7251_write_reg(ov7251, OV7251_VTS_LOW,
175 - ov7251->current_mode->vts & 0xff);
176 + ov7251->current_ival->vts & 0xff);
179 ret = __v4l2_ctrl_handler_setup(&ov7251->ctrls);
180 @@ -1073,7 +1068,7 @@ static int ov7251_get_frame_interval(str
181 struct ov7251 *ov7251 = to_ov7251(subdev);
183 mutex_lock(&ov7251->lock);
184 - fi->interval = ov7251->current_mode->timeperframe;
185 + fi->interval = ov7251->current_ival->timeperframe;
186 mutex_unlock(&ov7251->lock);
189 @@ -1083,28 +1078,29 @@ static int ov7251_set_frame_interval(str
190 struct v4l2_subdev_frame_interval *fi)
192 struct ov7251 *ov7251 = to_ov7251(subdev);
193 - const struct ov7251_mode_info *new_mode;
194 + const struct ov7251_frame_ival_info *new_ival;
197 mutex_lock(&ov7251->lock);
198 - new_mode = ov7251_find_mode_by_ival(ov7251, &fi->interval);
199 + new_ival = ov7251_find_frame_ival_by_ival(ov7251, &fi->interval);
201 - if (new_mode != ov7251->current_mode) {
202 + if (new_ival != ov7251->current_ival) {
203 ret = __v4l2_ctrl_modify_range(ov7251->exposure, 1,
204 - new_mode->vts - OV7251_EXPOSURE_OFFSET,
205 - 1, new_mode->exposure_def);
207 + OV7251_EXPOSURE_OFFSET,
208 + 1, ov7251->current_mode->exposure_def);
212 ret = __v4l2_ctrl_s_ctrl(ov7251->exposure,
213 - new_mode->exposure_def);
214 + ov7251->current_mode->exposure_def);
218 - ov7251->current_mode = new_mode;
219 + ov7251->current_ival = new_ival;
222 - fi->interval = ov7251->current_mode->timeperframe;
223 + fi->interval = ov7251->current_ival->timeperframe;
226 mutex_unlock(&ov7251->lock);
227 @@ -1316,6 +1312,7 @@ static int ov7251_probe(struct i2c_clien
228 mutex_init(&ov7251->lock);
230 ov7251->current_mode = &ov7251_mode_info_data[0];
231 + ov7251->current_ival = &ov7251_frame_ival_info_data[0];
233 v4l2_ctrl_handler_init(&ov7251->ctrls, 10);
234 ov7251->ctrls.lock = &ov7251->lock;