Merge branch 'sh/genirq' into sh-latest
[firefly-linux-kernel-4.4.55.git] / drivers / media / video / omap3isp / ispccdc.c
index eaabc27f0fa2936b8de8b1bf0a143e49a9fc7916..7e32331b60fb0e8362b7cd50ea38680a59638336 100644 (file)
@@ -38,6 +38,9 @@
 #include "ispreg.h"
 #include "ispccdc.h"
 
+#define CCDC_MIN_WIDTH         32
+#define CCDC_MIN_HEIGHT                32
+
 static struct v4l2_mbus_framefmt *
 __ccdc_get_format(struct isp_ccdc_device *ccdc, struct v4l2_subdev_fh *fh,
                  unsigned int pad, enum v4l2_subdev_format_whence which);
@@ -836,8 +839,8 @@ static void ccdc_config_vp(struct isp_ccdc_device *ccdc)
 
        if (pipe->input)
                div = DIV_ROUND_UP(l3_ick, pipe->max_rate);
-       else if (ccdc->vpcfg.pixelclk)
-               div = l3_ick / ccdc->vpcfg.pixelclk;
+       else if (pipe->external_rate)
+               div = l3_ick / pipe->external_rate;
 
        div = clamp(div, 2U, max_div);
        fmtcfg_vp |= (div - 2) << ISPCCDC_FMTCFG_VPIF_FRQ_SHIFT;
@@ -1118,6 +1121,7 @@ static void ccdc_configure(struct isp_ccdc_device *ccdc)
        struct isp_parallel_platform_data *pdata = NULL;
        struct v4l2_subdev *sensor;
        struct v4l2_mbus_framefmt *format;
+       const struct v4l2_rect *crop;
        const struct isp_format_info *fmt_info;
        struct v4l2_subdev_format fmt_src;
        unsigned int depth_out;
@@ -1211,14 +1215,14 @@ static void ccdc_configure(struct isp_ccdc_device *ccdc)
                       OMAP3_ISP_IOMEM_CCDC, ISPCCDC_VDINT);
 
        /* CCDC_PAD_SOURCE_OF */
-       format = &ccdc->formats[CCDC_PAD_SOURCE_OF];
+       crop = &ccdc->crop;
 
-       isp_reg_writel(isp, (0 << ISPCCDC_HORZ_INFO_SPH_SHIFT) |
-                      ((format->width - 1) << ISPCCDC_HORZ_INFO_NPH_SHIFT),
+       isp_reg_writel(isp, (crop->left << ISPCCDC_HORZ_INFO_SPH_SHIFT) |
+                      ((crop->width - 1) << ISPCCDC_HORZ_INFO_NPH_SHIFT),
                       OMAP3_ISP_IOMEM_CCDC, ISPCCDC_HORZ_INFO);
-       isp_reg_writel(isp, 0 << ISPCCDC_VERT_START_SLV0_SHIFT,
+       isp_reg_writel(isp, crop->top << ISPCCDC_VERT_START_SLV0_SHIFT,
                       OMAP3_ISP_IOMEM_CCDC, ISPCCDC_VERT_START);
-       isp_reg_writel(isp, (format->height - 1)
+       isp_reg_writel(isp, (crop->height - 1)
                        << ISPCCDC_VERT_LINES_NLV_SHIFT,
                       OMAP3_ISP_IOMEM_CCDC, ISPCCDC_VERT_LINES);
 
@@ -1410,6 +1414,9 @@ static void ccdc_hs_vs_isr(struct isp_ccdc_device *ccdc)
        struct video_device *vdev = ccdc->subdev.devnode;
        struct v4l2_event event;
 
+       /* Frame number propagation */
+       atomic_inc(&pipe->frame_number);
+
        memset(&event, 0, sizeof(event));
        event.type = V4L2_EVENT_FRAME_SYNC;
        event.u.frame_sync.frame_sequence = atomic_read(&pipe->frame_number);
@@ -1703,7 +1710,7 @@ static int ccdc_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
        if (sub->id != 0)
                return -EINVAL;
 
-       return v4l2_event_subscribe(fh, sub, OMAP3ISP_CCDC_NEVENTS);
+       return v4l2_event_subscribe(fh, sub, OMAP3ISP_CCDC_NEVENTS, NULL);
 }
 
 static int ccdc_unsubscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
@@ -1790,6 +1797,16 @@ __ccdc_get_format(struct isp_ccdc_device *ccdc, struct v4l2_subdev_fh *fh,
                return &ccdc->formats[pad];
 }
 
+static struct v4l2_rect *
+__ccdc_get_crop(struct isp_ccdc_device *ccdc, struct v4l2_subdev_fh *fh,
+               enum v4l2_subdev_format_whence which)
+{
+       if (which == V4L2_SUBDEV_FORMAT_TRY)
+               return v4l2_subdev_get_try_crop(fh, CCDC_PAD_SOURCE_OF);
+       else
+               return &ccdc->crop;
+}
+
 /*
  * ccdc_try_format - Try video format on a pad
  * @ccdc: ISP CCDC device
@@ -1806,6 +1823,7 @@ ccdc_try_format(struct isp_ccdc_device *ccdc, struct v4l2_subdev_fh *fh,
        const struct isp_format_info *info;
        unsigned int width = fmt->width;
        unsigned int height = fmt->height;
+       struct v4l2_rect *crop;
        unsigned int i;
 
        switch (pad) {
@@ -1831,14 +1849,10 @@ ccdc_try_format(struct isp_ccdc_device *ccdc, struct v4l2_subdev_fh *fh,
                format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SINK, which);
                memcpy(fmt, format, sizeof(*fmt));
 
-               /* The data formatter truncates the number of horizontal output
-                * pixels to a multiple of 16. To avoid clipping data, allow
-                * callers to request an output size bigger than the input size
-                * up to the nearest multiple of 16.
-                */
-               fmt->width = clamp_t(u32, width, 32, fmt->width + 15);
-               fmt->width &= ~15;
-               fmt->height = clamp_t(u32, height, 32, fmt->height);
+               /* Hardcode the output size to the crop rectangle size. */
+               crop = __ccdc_get_crop(ccdc, fh, which);
+               fmt->width = crop->width;
+               fmt->height = crop->height;
                break;
 
        case CCDC_PAD_SOURCE_VP:
@@ -1865,6 +1879,49 @@ ccdc_try_format(struct isp_ccdc_device *ccdc, struct v4l2_subdev_fh *fh,
        fmt->field = V4L2_FIELD_NONE;
 }
 
+/*
+ * ccdc_try_crop - Validate a crop rectangle
+ * @ccdc: ISP CCDC device
+ * @sink: format on the sink pad
+ * @crop: crop rectangle to be validated
+ */
+static void ccdc_try_crop(struct isp_ccdc_device *ccdc,
+                         const struct v4l2_mbus_framefmt *sink,
+                         struct v4l2_rect *crop)
+{
+       const struct isp_format_info *info;
+       unsigned int max_width;
+
+       /* For Bayer formats, restrict left/top and width/height to even values
+        * to keep the Bayer pattern.
+        */
+       info = omap3isp_video_format_info(sink->code);
+       if (info->flavor != V4L2_MBUS_FMT_Y8_1X8) {
+               crop->left &= ~1;
+               crop->top &= ~1;
+       }
+
+       crop->left = clamp_t(u32, crop->left, 0, sink->width - CCDC_MIN_WIDTH);
+       crop->top = clamp_t(u32, crop->top, 0, sink->height - CCDC_MIN_HEIGHT);
+
+       /* The data formatter truncates the number of horizontal output pixels
+        * to a multiple of 16. To avoid clipping data, allow callers to request
+        * an output size bigger than the input size up to the nearest multiple
+        * of 16.
+        */
+       max_width = (sink->width - crop->left + 15) & ~15;
+       crop->width = clamp_t(u32, crop->width, CCDC_MIN_WIDTH, max_width)
+                   & ~15;
+       crop->height = clamp_t(u32, crop->height, CCDC_MIN_HEIGHT,
+                              sink->height - crop->top);
+
+       /* Odd width/height values don't make sense for Bayer formats. */
+       if (info->flavor != V4L2_MBUS_FMT_Y8_1X8) {
+               crop->width &= ~1;
+               crop->height &= ~1;
+       }
+}
+
 /*
  * ccdc_enum_mbus_code - Handle pixel format enumeration
  * @sd     : pointer to v4l2 subdev structure
@@ -1936,6 +1993,93 @@ static int ccdc_enum_frame_size(struct v4l2_subdev *sd,
        return 0;
 }
 
+/*
+ * ccdc_get_selection - Retrieve a selection rectangle on a pad
+ * @sd: ISP CCDC V4L2 subdevice
+ * @fh: V4L2 subdev file handle
+ * @sel: Selection rectangle
+ *
+ * The only supported rectangles are the crop rectangles on the output formatter
+ * source pad.
+ *
+ * Return 0 on success or a negative error code otherwise.
+ */
+static int ccdc_get_selection(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
+                             struct v4l2_subdev_selection *sel)
+{
+       struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
+       struct v4l2_mbus_framefmt *format;
+
+       if (sel->pad != CCDC_PAD_SOURCE_OF)
+               return -EINVAL;
+
+       switch (sel->target) {
+       case V4L2_SUBDEV_SEL_TGT_CROP_BOUNDS:
+               sel->r.left = 0;
+               sel->r.top = 0;
+               sel->r.width = INT_MAX;
+               sel->r.height = INT_MAX;
+
+               format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SINK, sel->which);
+               ccdc_try_crop(ccdc, format, &sel->r);
+               break;
+
+       case V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL:
+               sel->r = *__ccdc_get_crop(ccdc, fh, sel->which);
+               break;
+
+       default:
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+/*
+ * ccdc_set_selection - Set a selection rectangle on a pad
+ * @sd: ISP CCDC V4L2 subdevice
+ * @fh: V4L2 subdev file handle
+ * @sel: Selection rectangle
+ *
+ * The only supported rectangle is the actual crop rectangle on the output
+ * formatter source pad.
+ *
+ * Return 0 on success or a negative error code otherwise.
+ */
+static int ccdc_set_selection(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
+                             struct v4l2_subdev_selection *sel)
+{
+       struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
+       struct v4l2_mbus_framefmt *format;
+
+       if (sel->target != V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL ||
+           sel->pad != CCDC_PAD_SOURCE_OF)
+               return -EINVAL;
+
+       /* The crop rectangle can't be changed while streaming. */
+       if (ccdc->state != ISP_PIPELINE_STREAM_STOPPED)
+               return -EBUSY;
+
+       /* Modifying the crop rectangle always changes the format on the source
+        * pad. If the KEEP_CONFIG flag is set, just return the current crop
+        * rectangle.
+        */
+       if (sel->flags & V4L2_SUBDEV_SEL_FLAG_KEEP_CONFIG) {
+               sel->r = *__ccdc_get_crop(ccdc, fh, sel->which);
+               return 0;
+       }
+
+       format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SINK, sel->which);
+       ccdc_try_crop(ccdc, format, &sel->r);
+       *__ccdc_get_crop(ccdc, fh, sel->which) = sel->r;
+
+       /* Update the source format. */
+       format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SOURCE_OF, sel->which);
+       ccdc_try_format(ccdc, fh, CCDC_PAD_SOURCE_OF, format, sel->which);
+
+       return 0;
+}
+
 /*
  * ccdc_get_format - Retrieve the video format on a pad
  * @sd : ISP CCDC V4L2 subdevice
@@ -1973,6 +2117,7 @@ static int ccdc_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
 {
        struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
        struct v4l2_mbus_framefmt *format;
+       struct v4l2_rect *crop;
 
        format = __ccdc_get_format(ccdc, fh, fmt->pad, fmt->which);
        if (format == NULL)
@@ -1983,6 +2128,16 @@ static int ccdc_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
 
        /* Propagate the format from sink to source */
        if (fmt->pad == CCDC_PAD_SINK) {
+               /* Reset the crop rectangle. */
+               crop = __ccdc_get_crop(ccdc, fh, fmt->which);
+               crop->left = 0;
+               crop->top = 0;
+               crop->width = fmt->format.width;
+               crop->height = fmt->format.height;
+
+               ccdc_try_crop(ccdc, &fmt->format, crop);
+
+               /* Update the source formats. */
                format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SOURCE_OF,
                                           fmt->which);
                *format = fmt->format;
@@ -1999,6 +2154,69 @@ static int ccdc_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
        return 0;
 }
 
+/*
+ * Decide whether desired output pixel code can be obtained with
+ * the lane shifter by shifting the input pixel code.
+ * @in: input pixelcode to shifter
+ * @out: output pixelcode from shifter
+ * @additional_shift: # of bits the sensor's LSB is offset from CAMEXT[0]
+ *
+ * return true if the combination is possible
+ * return false otherwise
+ */
+static bool ccdc_is_shiftable(enum v4l2_mbus_pixelcode in,
+                             enum v4l2_mbus_pixelcode out,
+                             unsigned int additional_shift)
+{
+       const struct isp_format_info *in_info, *out_info;
+
+       if (in == out)
+               return true;
+
+       in_info = omap3isp_video_format_info(in);
+       out_info = omap3isp_video_format_info(out);
+
+       if ((in_info->flavor == 0) || (out_info->flavor == 0))
+               return false;
+
+       if (in_info->flavor != out_info->flavor)
+               return false;
+
+       return in_info->bpp - out_info->bpp + additional_shift <= 6;
+}
+
+static int ccdc_link_validate(struct v4l2_subdev *sd,
+                             struct media_link *link,
+                             struct v4l2_subdev_format *source_fmt,
+                             struct v4l2_subdev_format *sink_fmt)
+{
+       struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
+       unsigned long parallel_shift;
+
+       /* Check if the two ends match */
+       if (source_fmt->format.width != sink_fmt->format.width ||
+           source_fmt->format.height != sink_fmt->format.height)
+               return -EPIPE;
+
+       /* We've got a parallel sensor here. */
+       if (ccdc->input == CCDC_INPUT_PARALLEL) {
+               struct isp_parallel_platform_data *pdata =
+                       &((struct isp_v4l2_subdevs_group *)
+                         media_entity_to_v4l2_subdev(link->source->entity)
+                         ->host_priv)->bus.parallel;
+               parallel_shift = pdata->data_lane_shift * 2;
+       } else {
+               parallel_shift = 0;
+       }
+
+       /* Lane shifter may be used to drop bits on CCDC sink pad */
+       if (!ccdc_is_shiftable(source_fmt->format.code,
+                              sink_fmt->format.code, parallel_shift))
+               return -EPIPE;
+
+       return 0;
+}
+
 /*
  * ccdc_init_formats - Initialize formats on all pads
  * @sd: ISP CCDC V4L2 subdevice
@@ -2041,6 +2259,9 @@ static const struct v4l2_subdev_pad_ops ccdc_v4l2_pad_ops = {
        .enum_frame_size = ccdc_enum_frame_size,
        .get_fmt = ccdc_get_format,
        .set_fmt = ccdc_set_format,
+       .get_selection = ccdc_get_selection,
+       .set_selection = ccdc_set_selection,
+       .link_validate = ccdc_link_validate,
 };
 
 /* V4L2 subdev operations */
@@ -2150,6 +2371,7 @@ static int ccdc_link_setup(struct media_entity *entity,
 /* media operations */
 static const struct media_entity_operations ccdc_media_ops = {
        .link_setup = ccdc_link_setup,
+       .link_validate = v4l2_subdev_link_validate,
 };
 
 void omap3isp_ccdc_unregister_entities(struct isp_ccdc_device *ccdc)
@@ -2276,8 +2498,6 @@ int omap3isp_ccdc_init(struct isp_device *isp)
        ccdc->clamp.oblen = 0;
        ccdc->clamp.dcsubval = 0;
 
-       ccdc->vpcfg.pixelclk = 0;
-
        ccdc->update = OMAP3ISP_CCDC_BLCLAMP;
        ccdc_apply_controls(ccdc);