1
2
3
4
5
6
7
8
9
10
11
12#include <linux/device.h>
13#include <linux/module.h>
14
15#include <media/soc_camera.h>
16#include <media/v4l2-common.h>
17
18#include "soc_scale_crop.h"
19
20#ifdef DEBUG_GEOMETRY
21#define dev_geo dev_info
22#else
23#define dev_geo dev_dbg
24#endif
25
26
27static bool is_smaller(const struct v4l2_rect *r1, const struct v4l2_rect *r2)
28{
29 return r1->width < r2->width || r1->height < r2->height;
30}
31
32
33static bool is_inside(const struct v4l2_rect *r1, const struct v4l2_rect *r2)
34{
35 return r1->left > r2->left || r1->top > r2->top ||
36 r1->left + r1->width < r2->left + r2->width ||
37 r1->top + r1->height < r2->top + r2->height;
38}
39
40
41int soc_camera_client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect)
42{
43 struct v4l2_subdev_selection sdsel = {
44 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
45 .target = V4L2_SEL_TGT_CROP,
46 };
47 int ret;
48
49 ret = v4l2_subdev_call(sd, pad, get_selection, NULL, &sdsel);
50 if (!ret) {
51 *rect = sdsel.r;
52 return ret;
53 }
54
55 sdsel.target = V4L2_SEL_TGT_CROP_DEFAULT;
56 ret = v4l2_subdev_call(sd, pad, get_selection, NULL, &sdsel);
57 if (!ret)
58 *rect = sdsel.r;
59
60 return ret;
61}
62EXPORT_SYMBOL(soc_camera_client_g_rect);
63
64
65static void move_and_crop_subrect(struct v4l2_rect *rect,
66 struct v4l2_rect *subrect)
67{
68 if (rect->width < subrect->width)
69 subrect->width = rect->width;
70
71 if (rect->height < subrect->height)
72 subrect->height = rect->height;
73
74 if (rect->left > subrect->left)
75 subrect->left = rect->left;
76 else if (rect->left + rect->width <
77 subrect->left + subrect->width)
78 subrect->left = rect->left + rect->width -
79 subrect->width;
80
81 if (rect->top > subrect->top)
82 subrect->top = rect->top;
83 else if (rect->top + rect->height <
84 subrect->top + subrect->height)
85 subrect->top = rect->top + rect->height -
86 subrect->height;
87}
88
89
90
91
92
93
94
95int soc_camera_client_s_selection(struct v4l2_subdev *sd,
96 struct v4l2_selection *sel, struct v4l2_selection *cam_sel,
97 struct v4l2_rect *target_rect, struct v4l2_rect *subrect)
98{
99 struct v4l2_subdev_selection sdsel = {
100 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
101 .target = sel->target,
102 .flags = sel->flags,
103 .r = sel->r,
104 };
105 struct v4l2_subdev_selection bounds = {
106 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
107 .target = V4L2_SEL_TGT_CROP_BOUNDS,
108 };
109 struct v4l2_rect *rect = &sel->r, *cam_rect = &cam_sel->r;
110 struct device *dev = sd->v4l2_dev->dev;
111 int ret;
112 unsigned int width, height;
113
114 v4l2_subdev_call(sd, pad, set_selection, NULL, &sdsel);
115 sel->r = sdsel.r;
116 ret = soc_camera_client_g_rect(sd, cam_rect);
117 if (ret < 0)
118 return ret;
119
120
121
122
123
124 if (!memcmp(rect, cam_rect, sizeof(*rect))) {
125
126 dev_dbg(dev, "Camera S_SELECTION successful for %dx%d@%d:%d\n",
127 rect->width, rect->height, rect->left, rect->top);
128 *target_rect = *cam_rect;
129 return 0;
130 }
131
132
133 dev_geo(dev, "Fix camera S_SELECTION for %dx%d@%d:%d to %dx%d@%d:%d\n",
134 cam_rect->width, cam_rect->height,
135 cam_rect->left, cam_rect->top,
136 rect->width, rect->height, rect->left, rect->top);
137
138
139 ret = v4l2_subdev_call(sd, pad, get_selection, NULL, &bounds);
140 if (ret < 0)
141 return ret;
142
143
144 soc_camera_limit_side(&rect->left, &rect->width, sdsel.r.left, 2,
145 bounds.r.width);
146 soc_camera_limit_side(&rect->top, &rect->height, sdsel.r.top, 4,
147 bounds.r.height);
148
149
150
151
152
153 width = max_t(unsigned int, cam_rect->width, 2);
154 height = max_t(unsigned int, cam_rect->height, 2);
155
156
157
158
159
160 while (!ret && (is_smaller(cam_rect, rect) ||
161 is_inside(cam_rect, rect)) &&
162 (bounds.r.width > width || bounds.r.height > height)) {
163
164 width *= 2;
165 height *= 2;
166
167 cam_rect->width = width;
168 cam_rect->height = height;
169
170
171
172
173
174
175
176
177
178
179 if (cam_rect->left > rect->left)
180 cam_rect->left = bounds.r.left;
181
182 if (cam_rect->left + cam_rect->width < rect->left + rect->width)
183 cam_rect->width = rect->left + rect->width -
184 cam_rect->left;
185
186 if (cam_rect->top > rect->top)
187 cam_rect->top = bounds.r.top;
188
189 if (cam_rect->top + cam_rect->height < rect->top + rect->height)
190 cam_rect->height = rect->top + rect->height -
191 cam_rect->top;
192
193 sdsel.r = *cam_rect;
194 v4l2_subdev_call(sd, pad, set_selection, NULL, &sdsel);
195 *cam_rect = sdsel.r;
196 ret = soc_camera_client_g_rect(sd, cam_rect);
197 dev_geo(dev, "Camera S_SELECTION %d for %dx%d@%d:%d\n", ret,
198 cam_rect->width, cam_rect->height,
199 cam_rect->left, cam_rect->top);
200 }
201
202
203 if (is_smaller(cam_rect, rect) || is_inside(cam_rect, rect)) {
204
205
206
207
208 sdsel.r = bounds.r;
209 v4l2_subdev_call(sd, pad, set_selection, NULL, &sdsel);
210 *cam_rect = sdsel.r;
211
212 ret = soc_camera_client_g_rect(sd, cam_rect);
213 dev_geo(dev, "Camera S_SELECTION %d for max %dx%d@%d:%d\n", ret,
214 cam_rect->width, cam_rect->height,
215 cam_rect->left, cam_rect->top);
216 }
217
218 if (!ret) {
219 *target_rect = *cam_rect;
220 move_and_crop_subrect(target_rect, subrect);
221 }
222
223 return ret;
224}
225EXPORT_SYMBOL(soc_camera_client_s_selection);
226
227
228static int client_set_fmt(struct soc_camera_device *icd,
229 struct v4l2_rect *rect, struct v4l2_rect *subrect,
230 unsigned int max_width, unsigned int max_height,
231 struct v4l2_subdev_format *format, bool host_can_scale)
232{
233 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
234 struct device *dev = icd->parent;
235 struct v4l2_mbus_framefmt *mf = &format->format;
236 unsigned int width = mf->width, height = mf->height, tmp_w, tmp_h;
237 struct v4l2_subdev_selection sdsel = {
238 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
239 .target = V4L2_SEL_TGT_CROP_BOUNDS,
240 };
241 bool host_1to1;
242 int ret;
243
244 ret = v4l2_device_call_until_err(sd->v4l2_dev,
245 soc_camera_grp_id(icd), pad,
246 set_fmt, NULL, format);
247 if (ret < 0)
248 return ret;
249
250 dev_geo(dev, "camera scaled to %ux%u\n", mf->width, mf->height);
251
252 if (width == mf->width && height == mf->height) {
253
254 host_1to1 = true;
255 goto update_cache;
256 }
257
258 host_1to1 = false;
259 if (!host_can_scale)
260 goto update_cache;
261
262 ret = v4l2_subdev_call(sd, pad, get_selection, NULL, &sdsel);
263 if (ret < 0)
264 return ret;
265
266 if (max_width > sdsel.r.width)
267 max_width = sdsel.r.width;
268 if (max_height > sdsel.r.height)
269 max_height = sdsel.r.height;
270
271
272 tmp_w = mf->width;
273 tmp_h = mf->height;
274
275
276 while ((width > tmp_w || height > tmp_h) &&
277 tmp_w < max_width && tmp_h < max_height) {
278 tmp_w = min(2 * tmp_w, max_width);
279 tmp_h = min(2 * tmp_h, max_height);
280 mf->width = tmp_w;
281 mf->height = tmp_h;
282 ret = v4l2_device_call_until_err(sd->v4l2_dev,
283 soc_camera_grp_id(icd), pad,
284 set_fmt, NULL, format);
285 dev_geo(dev, "Camera scaled to %ux%u\n",
286 mf->width, mf->height);
287 if (ret < 0) {
288
289 dev_err(dev, "Client failed to set format: %d\n", ret);
290 return ret;
291 }
292 }
293
294update_cache:
295
296 ret = soc_camera_client_g_rect(sd, rect);
297 if (ret < 0)
298 return ret;
299
300 if (host_1to1)
301 *subrect = *rect;
302 else
303 move_and_crop_subrect(rect, subrect);
304
305 return 0;
306}
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321int soc_camera_client_scale(struct soc_camera_device *icd,
322 struct v4l2_rect *rect, struct v4l2_rect *subrect,
323 struct v4l2_mbus_framefmt *mf,
324 unsigned int *width, unsigned int *height,
325 bool host_can_scale, unsigned int shift)
326{
327 struct device *dev = icd->parent;
328 struct v4l2_subdev_format fmt_tmp = {
329 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
330 .format = *mf,
331 };
332 struct v4l2_mbus_framefmt *mf_tmp = &fmt_tmp.format;
333 unsigned int scale_h, scale_v;
334 int ret;
335
336
337
338
339
340 ret = client_set_fmt(icd, rect, subrect, *width, *height,
341 &fmt_tmp, host_can_scale);
342 if (ret < 0)
343 return ret;
344
345 dev_geo(dev, "5: camera scaled to %ux%u\n",
346 mf_tmp->width, mf_tmp->height);
347
348
349
350
351
352
353 scale_h = soc_camera_calc_scale(rect->width, shift, mf_tmp->width);
354 scale_v = soc_camera_calc_scale(rect->height, shift, mf_tmp->height);
355
356 mf->width = mf_tmp->width;
357 mf->height = mf_tmp->height;
358 mf->colorspace = mf_tmp->colorspace;
359
360
361
362
363
364 *width = soc_camera_shift_scale(subrect->width, shift, scale_h);
365 *height = soc_camera_shift_scale(subrect->height, shift, scale_v);
366
367 dev_geo(dev, "8: new client sub-window %ux%u\n", *width, *height);
368
369 return 0;
370}
371EXPORT_SYMBOL(soc_camera_client_scale);
372
373
374
375
376
377
378void soc_camera_calc_client_output(struct soc_camera_device *icd,
379 struct v4l2_rect *rect, struct v4l2_rect *subrect,
380 const struct v4l2_pix_format *pix, struct v4l2_mbus_framefmt *mf,
381 unsigned int shift)
382{
383 struct device *dev = icd->parent;
384 unsigned int scale_v, scale_h;
385
386 if (subrect->width == rect->width &&
387 subrect->height == rect->height) {
388
389 mf->width = pix->width;
390 mf->height = pix->height;
391 return;
392 }
393
394
395
396 dev_geo(dev, "2: subwin %ux%u@%u:%u\n",
397 subrect->width, subrect->height,
398 subrect->left, subrect->top);
399
400
401
402
403
404
405
406
407
408
409
410 scale_h = soc_camera_calc_scale(subrect->width, shift, pix->width);
411 scale_v = soc_camera_calc_scale(subrect->height, shift, pix->height);
412
413 dev_geo(dev, "3: scales %u:%u\n", scale_h, scale_v);
414
415
416
417
418
419 mf->width = soc_camera_shift_scale(rect->width, shift, scale_h);
420 mf->height = soc_camera_shift_scale(rect->height, shift, scale_v);
421}
422EXPORT_SYMBOL(soc_camera_calc_client_output);
423
424MODULE_DESCRIPTION("soc-camera scaling-cropping functions");
425MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
426MODULE_LICENSE("GPL");
427