ov13858.c (a7bc5773cd166032e35e343dfb6067a93d8402d1) ov13858.c (227b183dcbcc430a6ce29518d3b24d481597e87d)
1/*
2 * Copyright (c) 2017 Intel Corporation.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License version
6 * 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,

--- 1341 unchanged lines hidden (view full) ---

1350
1351 mutex_lock(&ov13858->mutex);
1352 ret = ov13858_do_get_pad_format(ov13858, cfg, fmt);
1353 mutex_unlock(&ov13858->mutex);
1354
1355 return ret;
1356}
1357
1/*
2 * Copyright (c) 2017 Intel Corporation.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License version
6 * 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,

--- 1341 unchanged lines hidden (view full) ---

1350
1351 mutex_lock(&ov13858->mutex);
1352 ret = ov13858_do_get_pad_format(ov13858, cfg, fmt);
1353 mutex_unlock(&ov13858->mutex);
1354
1355 return ret;
1356}
1357
1358/*
1359 * Calculate resolution distance
1360 */
1361static int
1358static int
1362ov13858_get_resolution_dist(const struct ov13858_mode *mode,
1363 struct v4l2_mbus_framefmt *framefmt)
1364{
1365 return abs(mode->width - framefmt->width) +
1366 abs(mode->height - framefmt->height);
1367}
1368
1369/*
1370 * Find the closest supported resolution to the requested resolution
1371 */
1372static const struct ov13858_mode *
1373ov13858_find_best_fit(struct ov13858 *ov13858,
1374 struct v4l2_subdev_format *fmt)
1375{
1376 int i, dist, cur_best_fit = 0, cur_best_fit_dist = -1;
1377 struct v4l2_mbus_framefmt *framefmt = &fmt->format;
1378
1379 for (i = 0; i < ARRAY_SIZE(supported_modes); i++) {
1380 dist = ov13858_get_resolution_dist(&supported_modes[i],
1381 framefmt);
1382 if (cur_best_fit_dist == -1 || dist < cur_best_fit_dist) {
1383 cur_best_fit_dist = dist;
1384 cur_best_fit = i;
1385 }
1386 }
1387
1388 return &supported_modes[cur_best_fit];
1389}
1390
1391static int
1392ov13858_set_pad_format(struct v4l2_subdev *sd,
1393 struct v4l2_subdev_pad_config *cfg,
1394 struct v4l2_subdev_format *fmt)
1395{
1396 struct ov13858 *ov13858 = to_ov13858(sd);
1397 const struct ov13858_mode *mode;
1398 struct v4l2_mbus_framefmt *framefmt;
1399 s32 vblank_def;
1400 s32 vblank_min;
1401 s64 h_blank;
1402 s64 pixel_rate;
1403 s64 link_freq;
1404
1405 mutex_lock(&ov13858->mutex);
1406
1407 /* Only one raw bayer(GRBG) order is supported */
1408 if (fmt->format.code != MEDIA_BUS_FMT_SGRBG10_1X10)
1409 fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
1410
1359ov13858_set_pad_format(struct v4l2_subdev *sd,
1360 struct v4l2_subdev_pad_config *cfg,
1361 struct v4l2_subdev_format *fmt)
1362{
1363 struct ov13858 *ov13858 = to_ov13858(sd);
1364 const struct ov13858_mode *mode;
1365 struct v4l2_mbus_framefmt *framefmt;
1366 s32 vblank_def;
1367 s32 vblank_min;
1368 s64 h_blank;
1369 s64 pixel_rate;
1370 s64 link_freq;
1371
1372 mutex_lock(&ov13858->mutex);
1373
1374 /* Only one raw bayer(GRBG) order is supported */
1375 if (fmt->format.code != MEDIA_BUS_FMT_SGRBG10_1X10)
1376 fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
1377
1411 mode = ov13858_find_best_fit(ov13858, fmt);
1378 mode = v4l2_find_nearest_size(supported_modes, width, height,
1379 fmt->format.width, fmt->format.height);
1412 ov13858_update_pad_format(mode, fmt);
1413 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1414 framefmt = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
1415 *framefmt = fmt->format;
1416 } else {
1417 ov13858->cur_mode = mode;
1418 __v4l2_ctrl_s_ctrl(ov13858->link_freq, mode->link_freq_index);
1419 link_freq = link_freq_menu_items[mode->link_freq_index];

--- 436 unchanged lines hidden ---
1380 ov13858_update_pad_format(mode, fmt);
1381 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1382 framefmt = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
1383 *framefmt = fmt->format;
1384 } else {
1385 ov13858->cur_mode = mode;
1386 __v4l2_ctrl_s_ctrl(ov13858->link_freq, mode->link_freq_index);
1387 link_freq = link_freq_menu_items[mode->link_freq_index];

--- 436 unchanged lines hidden ---