media: tvp5150: add FORMAT_TRY support for get/set selection handlers
authorMarco Felsch <m.felsch@pengutronix.de>
Thu, 12 Mar 2020 10:31:48 +0000 (11:31 +0100)
committerMauro Carvalho Chehab <mchehab+huawei@kernel.org>
Thu, 12 Mar 2020 15:38:21 +0000 (16:38 +0100)
Since commit 10d5509c8d50 ("[media] v4l2: remove g/s_crop from video ops")
the 'which' field for set/get_selection must be FORMAT_ACTIVE. There is
no way to try different selections. The patch adds a helper function to
select the correct selection memory space (sub-device file handle or
driver state) which will be set/returned.

The selection rectangle is updated if the format is FORMAT_ACTIVE and
the rectangle position and/or size differs from the current set
rectangle.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
drivers/media/i2c/tvp5150.c

index 38f02c23e352e2d1b5c676d5b3d309d6e3dbf035..bb3752078bc14418cc8e940f51e6a4998fa4d814 100644 (file)
@@ -19,6 +19,7 @@
 #include <media/v4l2-ctrls.h>
 #include <media/v4l2-fwnode.h>
 #include <media/v4l2-mc.h>
+#include <media/v4l2-rect.h>
 
 #include "tvp5150_reg.h"
 
@@ -995,6 +996,25 @@ static void tvp5150_set_default(v4l2_std_id std, struct v4l2_rect *crop)
                crop->height = TVP5150_V_MAX_OTHERS;
 }
 
+static struct v4l2_rect *
+tvp5150_get_pad_crop(struct tvp5150 *decoder,
+                    struct v4l2_subdev_pad_config *cfg, unsigned int pad,
+                    enum v4l2_subdev_format_whence which)
+{
+       switch (which) {
+       case V4L2_SUBDEV_FORMAT_ACTIVE:
+               return &decoder->rect;
+       case V4L2_SUBDEV_FORMAT_TRY:
+#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
+               return v4l2_subdev_get_try_crop(&decoder->sd, cfg, pad);
+#else
+               return ERR_PTR(-EINVAL);
+#endif
+       default:
+               return ERR_PTR(-EINVAL);
+       }
+}
+
 static int tvp5150_fill_fmt(struct v4l2_subdev *sd,
                            struct v4l2_subdev_pad_config *cfg,
                            struct v4l2_subdev_format *format)
@@ -1019,17 +1039,51 @@ static int tvp5150_fill_fmt(struct v4l2_subdev *sd,
        return 0;
 }
 
+static unsigned int tvp5150_get_hmax(struct v4l2_subdev *sd)
+{
+       struct tvp5150 *decoder = to_tvp5150(sd);
+       v4l2_std_id std;
+
+       /* Calculate height based on current standard */
+       if (decoder->norm == V4L2_STD_ALL)
+               std = tvp5150_read_std(sd);
+       else
+               std = decoder->norm;
+
+       return (std & V4L2_STD_525_60) ?
+               TVP5150_V_MAX_525_60 : TVP5150_V_MAX_OTHERS;
+}
+
+static void tvp5150_set_hw_selection(struct v4l2_subdev *sd,
+                                    struct v4l2_rect *rect)
+{
+       struct tvp5150 *decoder = to_tvp5150(sd);
+       unsigned int hmax = tvp5150_get_hmax(sd);
+
+       regmap_write(decoder->regmap, TVP5150_VERT_BLANKING_START, rect->top);
+       regmap_write(decoder->regmap, TVP5150_VERT_BLANKING_STOP,
+                    rect->top + rect->height - hmax);
+       regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_ST_MSB,
+                    rect->left >> TVP5150_CROP_SHIFT);
+       regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_ST_LSB,
+                    rect->left | (1 << TVP5150_CROP_SHIFT));
+       regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_STP_MSB,
+                    (rect->left + rect->width - TVP5150_MAX_CROP_LEFT) >>
+                    TVP5150_CROP_SHIFT);
+       regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_STP_LSB,
+                    rect->left + rect->width - TVP5150_MAX_CROP_LEFT);
+}
+
 static int tvp5150_set_selection(struct v4l2_subdev *sd,
                                 struct v4l2_subdev_pad_config *cfg,
                                 struct v4l2_subdev_selection *sel)
 {
        struct tvp5150 *decoder = to_tvp5150(sd);
        struct v4l2_rect *rect = &sel->r;
-       v4l2_std_id std;
-       int hmax;
+       struct v4l2_rect *crop;
+       unsigned int hmax;
 
-       if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE ||
-           sel->target != V4L2_SEL_TGT_CROP)
+       if (sel->target != V4L2_SEL_TGT_CROP)
                return -EINVAL;
 
        dev_dbg_lvl(sd->dev, 1, debug, "%s left=%d, top=%d, width=%d, height=%d\n",
@@ -1038,17 +1092,7 @@ static int tvp5150_set_selection(struct v4l2_subdev *sd,
        /* tvp5150 has some special limits */
        rect->left = clamp(rect->left, 0, TVP5150_MAX_CROP_LEFT);
        rect->top = clamp(rect->top, 0, TVP5150_MAX_CROP_TOP);
-
-       /* Calculate height based on current standard */
-       if (decoder->norm == V4L2_STD_ALL)
-               std = tvp5150_read_std(sd);
-       else
-               std = decoder->norm;
-
-       if (std & V4L2_STD_525_60)
-               hmax = TVP5150_V_MAX_525_60;
-       else
-               hmax = TVP5150_V_MAX_OTHERS;
+       hmax = tvp5150_get_hmax(sd);
 
        /*
         * alignments:
@@ -1061,20 +1105,23 @@ static int tvp5150_set_selection(struct v4l2_subdev *sd,
                              hmax - TVP5150_MAX_CROP_TOP - rect->top,
                              hmax - rect->top, 0, 0);
 
-       regmap_write(decoder->regmap, TVP5150_VERT_BLANKING_START, rect->top);
-       regmap_write(decoder->regmap, TVP5150_VERT_BLANKING_STOP,
-                    rect->top + rect->height - hmax);
-       regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_ST_MSB,
-                    rect->left >> TVP5150_CROP_SHIFT);
-       regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_ST_LSB,
-                    rect->left | (1 << TVP5150_CROP_SHIFT));
-       regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_STP_MSB,
-                    (rect->left + rect->width - TVP5150_MAX_CROP_LEFT) >>
-                    TVP5150_CROP_SHIFT);
-       regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_STP_LSB,
-                    rect->left + rect->width - TVP5150_MAX_CROP_LEFT);
+       if (!IS_ENABLED(CONFIG_VIDEO_V4L2_SUBDEV_API) &&
+           sel->which == V4L2_SUBDEV_FORMAT_TRY)
+               return 0;
+
+       crop = tvp5150_get_pad_crop(decoder, cfg, sel->pad, sel->which);
+       if (IS_ERR(crop))
+               return PTR_ERR(crop);
+
+       /*
+        * Update output image size if the selection (crop) rectangle size or
+        * position has been modified.
+        */
+       if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE &&
+           !v4l2_rect_equal(rect, crop))
+               tvp5150_set_hw_selection(sd, rect);
 
-       decoder->rect = *rect;
+       *crop = *rect;
 
        return 0;
 }
@@ -1084,11 +1131,9 @@ static int tvp5150_get_selection(struct v4l2_subdev *sd,
                                 struct v4l2_subdev_selection *sel)
 {
        struct tvp5150 *decoder = container_of(sd, struct tvp5150, sd);
+       struct v4l2_rect *crop;
        v4l2_std_id std;
 
-       if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
-               return -EINVAL;
-
        switch (sel->target) {
        case V4L2_SEL_TGT_CROP_BOUNDS:
                sel->r.left = 0;
@@ -1106,7 +1151,11 @@ static int tvp5150_get_selection(struct v4l2_subdev *sd,
                        sel->r.height = TVP5150_V_MAX_OTHERS;
                return 0;
        case V4L2_SEL_TGT_CROP:
-               sel->r = decoder->rect;
+               crop = tvp5150_get_pad_crop(decoder, cfg, sel->pad,
+                                           sel->which);
+               if (IS_ERR(crop))
+                       return PTR_ERR(crop);
+               sel->r = *crop;
                return 0;
        default:
                return -EINVAL;