[media] s5p-fimc: Use v4l2_subdev internal ops to register video nodes
[deliverable/linux.git] / drivers / media / video / s5p-fimc / fimc-capture.c
index b06efd2083287bc74b0f779fabdc159a41414437..b45da278021372af42f2cd173150aec0a83113b4 100644 (file)
@@ -83,7 +83,9 @@ static int fimc_capture_state_cleanup(struct fimc_dev *fimc, bool suspend)
 
        fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_SHUT |
                         1 << ST_CAPT_STREAM | 1 << ST_CAPT_ISP_STREAM);
-       if (!suspend)
+       if (suspend)
+               fimc->state |= (1 << ST_CAPT_SUSPENDED);
+       else
                fimc->state &= ~(1 << ST_CAPT_PEND | 1 << ST_CAPT_SUSPENDED);
 
        /* Release unused buffers */
@@ -99,7 +101,6 @@ static int fimc_capture_state_cleanup(struct fimc_dev *fimc, bool suspend)
                else
                        vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
        }
-       set_bit(ST_CAPT_SUSPENDED, &fimc->state);
 
        fimc_hw_reset(fimc);
        cap->buf_index = 0;
@@ -138,7 +139,7 @@ static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend)
  * spinlock held. It updates the camera pixel crop, rotation and
  * image flip in H/W.
  */
-int fimc_capture_config_update(struct fimc_ctx *ctx)
+static int fimc_capture_config_update(struct fimc_ctx *ctx)
 {
        struct fimc_dev *fimc = ctx->fimc_dev;
        int ret;
@@ -146,24 +147,89 @@ int fimc_capture_config_update(struct fimc_ctx *ctx)
        if (!test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
                return 0;
 
-       spin_lock(&ctx->slock);
        fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
+
        ret = fimc_set_scaler_info(ctx);
-       if (ret == 0) {
-               fimc_hw_set_prescaler(ctx);
-               fimc_hw_set_mainscaler(ctx);
-               fimc_hw_set_target_format(ctx);
-               fimc_hw_set_rotation(ctx);
-               fimc_prepare_dma_offset(ctx, &ctx->d_frame);
-               fimc_hw_set_out_dma(ctx);
-               if (fimc->variant->has_alpha)
-                       fimc_hw_set_rgb_alpha(ctx);
-               clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
-       }
-       spin_unlock(&ctx->slock);
+       if (ret)
+               return ret;
+
+       fimc_hw_set_prescaler(ctx);
+       fimc_hw_set_mainscaler(ctx);
+       fimc_hw_set_target_format(ctx);
+       fimc_hw_set_rotation(ctx);
+       fimc_prepare_dma_offset(ctx, &ctx->d_frame);
+       fimc_hw_set_out_dma(ctx);
+       if (fimc->variant->has_alpha)
+               fimc_hw_set_rgb_alpha(ctx);
+
+       clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
        return ret;
 }
 
+void fimc_capture_irq_handler(struct fimc_dev *fimc, int deq_buf)
+{
+       struct fimc_vid_cap *cap = &fimc->vid_cap;
+       struct fimc_vid_buffer *v_buf;
+       struct timeval *tv;
+       struct timespec ts;
+
+       if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) {
+               wake_up(&fimc->irq_queue);
+               goto done;
+       }
+
+       if (!list_empty(&cap->active_buf_q) &&
+           test_bit(ST_CAPT_RUN, &fimc->state) && deq_buf) {
+               ktime_get_real_ts(&ts);
+
+               v_buf = fimc_active_queue_pop(cap);
+
+               tv = &v_buf->vb.v4l2_buf.timestamp;
+               tv->tv_sec = ts.tv_sec;
+               tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
+               v_buf->vb.v4l2_buf.sequence = cap->frame_count++;
+
+               vb2_buffer_done(&v_buf->vb, VB2_BUF_STATE_DONE);
+       }
+
+       if (!list_empty(&cap->pending_buf_q)) {
+
+               v_buf = fimc_pending_queue_pop(cap);
+               fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index);
+               v_buf->index = cap->buf_index;
+
+               /* Move the buffer to the capture active queue */
+               fimc_active_queue_add(cap, v_buf);
+
+               dbg("next frame: %d, done frame: %d",
+                   fimc_hw_get_frame_index(fimc), v_buf->index);
+
+               if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
+                       cap->buf_index = 0;
+       }
+
+       if (cap->active_buf_cnt == 0) {
+               if (deq_buf)
+                       clear_bit(ST_CAPT_RUN, &fimc->state);
+
+               if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
+                       cap->buf_index = 0;
+       } else {
+               set_bit(ST_CAPT_RUN, &fimc->state);
+       }
+
+       fimc_capture_config_update(cap->ctx);
+done:
+       if (cap->active_buf_cnt == 1) {
+               fimc_deactivate_capture(fimc);
+               clear_bit(ST_CAPT_STREAM, &fimc->state);
+       }
+
+       dbg("frame: %d, active_buf_cnt: %d",
+           fimc_hw_get_frame_index(fimc), cap->active_buf_cnt);
+}
+
+
 static int start_streaming(struct vb2_queue *q, unsigned int count)
 {
        struct fimc_ctx *ctx = q->drv_priv;
@@ -246,28 +312,37 @@ int fimc_capture_resume(struct fimc_dev *fimc)
 
 }
 
-static unsigned int get_plane_size(struct fimc_frame *fr, unsigned int plane)
-{
-       if (!fr || plane >= fr->fmt->memplanes)
-               return 0;
-       return fr->f_width * fr->f_height * fr->fmt->depth[plane] / 8;
-}
-
-static int queue_setup(struct vb2_queue *vq,  const struct v4l2_format *pfmt,
+static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
                       unsigned int *num_buffers, unsigned int *num_planes,
                       unsigned int sizes[], void *allocators[])
 {
+       const struct v4l2_pix_format_mplane *pixm = NULL;
        struct fimc_ctx *ctx = vq->drv_priv;
-       struct fimc_fmt *fmt = ctx->d_frame.fmt;
+       struct fimc_frame *frame = &ctx->d_frame;
+       struct fimc_fmt *fmt = frame->fmt;
+       unsigned long wh;
        int i;
 
-       if (!fmt)
+       if (pfmt) {
+               pixm = &pfmt->fmt.pix_mp;
+               fmt = fimc_find_format(&pixm->pixelformat, NULL,
+                                      FMT_FLAGS_CAM | FMT_FLAGS_M2M, -1);
+               wh = pixm->width * pixm->height;
+       } else {
+               wh = frame->f_width * frame->f_height;
+       }
+
+       if (fmt == NULL)
                return -EINVAL;
 
        *num_planes = fmt->memplanes;
 
        for (i = 0; i < fmt->memplanes; i++) {
-               sizes[i] = get_plane_size(&ctx->d_frame, i);
+               unsigned int size = (wh * fmt->depth[i]) / 8;
+               if (pixm)
+                       sizes[i] = max(size, pixm->plane_fmt[i].sizeimage);
+               else
+                       sizes[i] = size;
                allocators[i] = ctx->fimc_dev->alloc_ctx;
        }
 
@@ -918,7 +993,7 @@ static int fimc_pipeline_validate(struct fimc_dev *fimc)
                if (!(pad->flags & MEDIA_PAD_FL_SINK))
                        break;
                /* Don't call FIMC subdev operation to avoid nested locking */
-               if (sd == fimc->vid_cap.subdev) {
+               if (sd == &fimc->vid_cap.subdev) {
                        struct fimc_frame *ff = &vid_cap->ctx->s_frame;
                        sink_fmt.format.width = ff->f_width;
                        sink_fmt.format.height = ff->f_height;
@@ -1234,7 +1309,7 @@ void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
                                         struct fimc_vid_buffer, list);
                        vb2_set_plane_payload(&buf->vb, 0, *((u32 *)arg));
                }
-               fimc_capture_irq_handler(fimc, true);
+               fimc_capture_irq_handler(fimc, 1);
                fimc_deactivate_capture(fimc);
                spin_unlock_irqrestore(&fimc->slock, irq_flags);
        }
@@ -1383,7 +1458,7 @@ static int fimc_subdev_set_crop(struct v4l2_subdev *sd,
        fimc_capture_try_crop(ctx, r, crop->pad);
 
        if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
-               mutex_lock(&fimc->lock);
+               mutex_unlock(&fimc->lock);
                *v4l2_subdev_get_try_crop(fh, crop->pad) = *r;
                return 0;
        }
@@ -1414,53 +1489,6 @@ static struct v4l2_subdev_ops fimc_subdev_ops = {
        .pad = &fimc_subdev_pad_ops,
 };
 
-static int fimc_create_capture_subdev(struct fimc_dev *fimc,
-                                     struct v4l2_device *v4l2_dev)
-{
-       struct v4l2_subdev *sd;
-       int ret;
-
-       sd = kzalloc(sizeof(*sd), GFP_KERNEL);
-       if (!sd)
-               return -ENOMEM;
-
-       v4l2_subdev_init(sd, &fimc_subdev_ops);
-       sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
-       snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->pdev->id);
-
-       fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
-       fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
-       ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
-                               fimc->vid_cap.sd_pads, 0);
-       if (ret)
-               goto me_err;
-       ret = v4l2_device_register_subdev(v4l2_dev, sd);
-       if (ret)
-               goto sd_err;
-
-       fimc->vid_cap.subdev = sd;
-       v4l2_set_subdevdata(sd, fimc);
-       sd->entity.ops = &fimc_sd_media_ops;
-       return 0;
-sd_err:
-       media_entity_cleanup(&sd->entity);
-me_err:
-       kfree(sd);
-       return ret;
-}
-
-static void fimc_destroy_capture_subdev(struct fimc_dev *fimc)
-{
-       struct v4l2_subdev *sd = fimc->vid_cap.subdev;
-
-       if (!sd)
-               return;
-       media_entity_cleanup(&sd->entity);
-       v4l2_device_unregister_subdev(sd);
-       kfree(sd);
-       fimc->vid_cap.subdev = NULL;
-}
-
 /* Set default format at the sensor and host interface */
 static int fimc_capture_set_default_format(struct fimc_dev *fimc)
 {
@@ -1479,7 +1507,7 @@ static int fimc_capture_set_default_format(struct fimc_dev *fimc)
 }
 
 /* fimc->lock must be already initialized */
-int fimc_register_capture_device(struct fimc_dev *fimc,
+static int fimc_register_capture_device(struct fimc_dev *fimc,
                                 struct v4l2_device *v4l2_dev)
 {
        struct video_device *vfd;
@@ -1497,7 +1525,7 @@ int fimc_register_capture_device(struct fimc_dev *fimc,
        ctx->out_path    = FIMC_DMA;
        ctx->state       = FIMC_CTX_CAP;
        ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
-       ctx->d_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
+       ctx->d_frame.fmt = ctx->s_frame.fmt;
 
        vfd = video_device_alloc();
        if (!vfd) {
@@ -1505,8 +1533,7 @@ int fimc_register_capture_device(struct fimc_dev *fimc,
                goto err_vd_alloc;
        }
 
-       snprintf(vfd->name, sizeof(vfd->name), "%s.capture",
-                dev_name(&fimc->pdev->dev));
+       snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.capture", fimc->id);
 
        vfd->fops       = &fimc_capture_fops;
        vfd->ioctl_ops  = &fimc_capture_ioctl_ops;
@@ -1514,6 +1541,10 @@ int fimc_register_capture_device(struct fimc_dev *fimc,
        vfd->minor      = -1;
        vfd->release    = video_device_release;
        vfd->lock       = &fimc->lock;
+       /* Locking in file operations other than ioctl should be done
+          by the driver, not the V4L2 core.
+          This driver needs auditing so that this flag can be removed. */
+       set_bit(V4L2_FL_LOCK_ALL_FOPS, &vfd->flags);
        video_set_drvdata(vfd, fimc);
 
        vid_cap = &fimc->vid_cap;
@@ -1524,7 +1555,6 @@ int fimc_register_capture_device(struct fimc_dev *fimc,
 
        INIT_LIST_HEAD(&vid_cap->pending_buf_q);
        INIT_LIST_HEAD(&vid_cap->active_buf_q);
-       spin_lock_init(&ctx->slock);
        vid_cap->ctx = ctx;
 
        q = &fimc->vid_cap.vbq;
@@ -1538,18 +1568,22 @@ int fimc_register_capture_device(struct fimc_dev *fimc,
 
        vb2_queue_init(q);
 
-       fimc->vid_cap.vd_pad.flags = MEDIA_PAD_FL_SINK;
-       ret = media_entity_init(&vfd->entity, 1, &fimc->vid_cap.vd_pad, 0);
+       vid_cap->vd_pad.flags = MEDIA_PAD_FL_SINK;
+       ret = media_entity_init(&vfd->entity, 1, &vid_cap->vd_pad, 0);
        if (ret)
                goto err_ent;
-       ret = fimc_create_capture_subdev(fimc, v4l2_dev);
+
+       ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
        if (ret)
-               goto err_sd_reg;
+               goto err_vd;
+
+       v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n",
+                 vfd->name, video_device_node_name(vfd));
 
        vfd->ctrl_handler = &ctx->ctrl_handler;
        return 0;
 
-err_sd_reg:
+err_vd:
        media_entity_cleanup(&vfd->entity);
 err_ent:
        video_device_release(vfd);
@@ -1558,17 +1592,73 @@ err_vd_alloc:
        return ret;
 }
 
-void fimc_unregister_capture_device(struct fimc_dev *fimc)
+static int fimc_capture_subdev_registered(struct v4l2_subdev *sd)
+{
+       struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
+       int ret;
+
+       ret = fimc_register_m2m_device(fimc, sd->v4l2_dev);
+       if (ret)
+               return ret;
+
+       ret = fimc_register_capture_device(fimc, sd->v4l2_dev);
+       if (ret)
+               fimc_unregister_m2m_device(fimc);
+
+       return ret;
+}
+
+static void fimc_capture_subdev_unregistered(struct v4l2_subdev *sd)
 {
-       struct video_device *vfd = fimc->vid_cap.vfd;
+       struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
+
+       if (fimc == NULL)
+               return;
+
+       fimc_unregister_m2m_device(fimc);
 
-       if (vfd) {
-               media_entity_cleanup(&vfd->entity);
-               /* Can also be called if video device was
-                  not registered */
-               video_unregister_device(vfd);
+       if (fimc->vid_cap.vfd) {
+               media_entity_cleanup(&fimc->vid_cap.vfd->entity);
+               video_unregister_device(fimc->vid_cap.vfd);
+               fimc->vid_cap.vfd = NULL;
        }
-       fimc_destroy_capture_subdev(fimc);
+
        kfree(fimc->vid_cap.ctx);
        fimc->vid_cap.ctx = NULL;
 }
+
+static const struct v4l2_subdev_internal_ops fimc_capture_sd_internal_ops = {
+       .registered = fimc_capture_subdev_registered,
+       .unregistered = fimc_capture_subdev_unregistered,
+};
+
+int fimc_initialize_capture_subdev(struct fimc_dev *fimc)
+{
+       struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
+       int ret;
+
+       v4l2_subdev_init(sd, &fimc_subdev_ops);
+       sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
+       snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->pdev->id);
+
+       fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
+       fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
+       ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
+                               fimc->vid_cap.sd_pads, 0);
+       if (ret)
+               return ret;
+
+       sd->entity.ops = &fimc_sd_media_ops;
+       sd->internal_ops = &fimc_capture_sd_internal_ops;
+       v4l2_set_subdevdata(sd, fimc);
+       return 0;
+}
+
+void fimc_unregister_capture_subdev(struct fimc_dev *fimc)
+{
+       struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
+
+       v4l2_device_unregister_subdev(sd);
+       media_entity_cleanup(&sd->entity);
+       v4l2_set_subdevdata(sd, NULL);
+}
This page took 0.035996 seconds and 5 git commands to generate.