2 * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver
4 * Copyright (C) 2010 - 2011 Samsung Electronics Co., Ltd.
5 * Author: Sylwester Nawrocki, <s.nawrocki@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/types.h>
15 #include <linux/errno.h>
16 #include <linux/bug.h>
17 #include <linux/interrupt.h>
18 #include <linux/device.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/list.h>
21 #include <linux/slab.h>
23 #include <linux/videodev2.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-ioctl.h>
26 #include <media/v4l2-mem2mem.h>
27 #include <media/videobuf2-core.h>
28 #include <media/videobuf2-dma-contig.h>
30 #include "fimc-mdevice.h"
31 #include "fimc-core.h"
33 static int fimc_init_capture(struct fimc_dev *fimc)
35 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
36 struct fimc_sensor_info *sensor;
40 if (fimc->pipeline.sensor == NULL || ctx == NULL)
42 if (ctx->s_frame.fmt == NULL)
45 sensor = v4l2_get_subdev_hostdata(fimc->pipeline.sensor);
47 spin_lock_irqsave(&fimc->slock, flags);
48 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
49 fimc_set_yuv_order(ctx);
51 fimc_hw_set_camera_polarity(fimc, sensor->pdata);
52 fimc_hw_set_camera_type(fimc, sensor->pdata);
53 fimc_hw_set_camera_source(fimc, sensor->pdata);
54 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
56 ret = fimc_set_scaler_info(ctx);
58 fimc_hw_set_input_path(ctx);
59 fimc_hw_set_prescaler(ctx);
60 fimc_hw_set_mainscaler(ctx);
61 fimc_hw_set_target_format(ctx);
62 fimc_hw_set_rotation(ctx);
63 fimc_hw_set_effect(ctx, false);
64 fimc_hw_set_output_path(ctx);
65 fimc_hw_set_out_dma(ctx);
66 if (fimc->variant->has_alpha)
67 fimc_hw_set_rgb_alpha(ctx);
68 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
70 spin_unlock_irqrestore(&fimc->slock, flags);
74 static int fimc_capture_state_cleanup(struct fimc_dev *fimc, bool suspend)
76 struct fimc_vid_cap *cap = &fimc->vid_cap;
77 struct fimc_vid_buffer *buf;
81 spin_lock_irqsave(&fimc->slock, flags);
82 streaming = fimc->state & (1 << ST_CAPT_ISP_STREAM);
84 fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_SHUT |
85 1 << ST_CAPT_STREAM | 1 << ST_CAPT_ISP_STREAM);
87 fimc->state |= (1 << ST_CAPT_SUSPENDED);
89 fimc->state &= ~(1 << ST_CAPT_PEND | 1 << ST_CAPT_SUSPENDED);
91 /* Release unused buffers */
92 while (!suspend && !list_empty(&cap->pending_buf_q)) {
93 buf = fimc_pending_queue_pop(cap);
94 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
96 /* If suspending put unused buffers onto pending queue */
97 while (!list_empty(&cap->active_buf_q)) {
98 buf = fimc_active_queue_pop(cap);
100 fimc_pending_queue_add(cap, buf);
102 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
108 spin_unlock_irqrestore(&fimc->slock, flags);
111 return fimc_pipeline_s_stream(fimc, 0);
116 static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend)
120 if (!fimc_capture_active(fimc))
123 spin_lock_irqsave(&fimc->slock, flags);
124 set_bit(ST_CAPT_SHUT, &fimc->state);
125 fimc_deactivate_capture(fimc);
126 spin_unlock_irqrestore(&fimc->slock, flags);
128 wait_event_timeout(fimc->irq_queue,
129 !test_bit(ST_CAPT_SHUT, &fimc->state),
130 (2*HZ/10)); /* 200 ms */
132 return fimc_capture_state_cleanup(fimc, suspend);
136 * fimc_capture_config_update - apply the camera interface configuration
138 * To be called from within the interrupt handler with fimc.slock
139 * spinlock held. It updates the camera pixel crop, rotation and
142 int fimc_capture_config_update(struct fimc_ctx *ctx)
144 struct fimc_dev *fimc = ctx->fimc_dev;
147 if (!test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
150 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
152 ret = fimc_set_scaler_info(ctx);
156 fimc_hw_set_prescaler(ctx);
157 fimc_hw_set_mainscaler(ctx);
158 fimc_hw_set_target_format(ctx);
159 fimc_hw_set_rotation(ctx);
160 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
161 fimc_hw_set_out_dma(ctx);
162 if (fimc->variant->has_alpha)
163 fimc_hw_set_rgb_alpha(ctx);
165 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
169 static int start_streaming(struct vb2_queue *q, unsigned int count)
171 struct fimc_ctx *ctx = q->drv_priv;
172 struct fimc_dev *fimc = ctx->fimc_dev;
173 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
177 vid_cap->frame_count = 0;
179 ret = fimc_init_capture(fimc);
183 set_bit(ST_CAPT_PEND, &fimc->state);
185 min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
187 if (vid_cap->active_buf_cnt >= min_bufs &&
188 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
189 fimc_activate_capture(ctx);
191 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
192 fimc_pipeline_s_stream(fimc, 1);
197 fimc_capture_state_cleanup(fimc, false);
201 static int stop_streaming(struct vb2_queue *q)
203 struct fimc_ctx *ctx = q->drv_priv;
204 struct fimc_dev *fimc = ctx->fimc_dev;
206 if (!fimc_capture_active(fimc))
209 return fimc_stop_capture(fimc, false);
212 int fimc_capture_suspend(struct fimc_dev *fimc)
214 bool suspend = fimc_capture_busy(fimc);
216 int ret = fimc_stop_capture(fimc, suspend);
219 return fimc_pipeline_shutdown(fimc);
222 static void buffer_queue(struct vb2_buffer *vb);
224 int fimc_capture_resume(struct fimc_dev *fimc)
226 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
227 struct fimc_vid_buffer *buf;
230 if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state))
233 INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
234 vid_cap->buf_index = 0;
235 fimc_pipeline_initialize(fimc, &fimc->vid_cap.vfd->entity,
237 fimc_init_capture(fimc);
239 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
241 for (i = 0; i < vid_cap->reqbufs_count; i++) {
242 if (list_empty(&vid_cap->pending_buf_q))
244 buf = fimc_pending_queue_pop(vid_cap);
245 buffer_queue(&buf->vb);
251 static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
252 unsigned int *num_buffers, unsigned int *num_planes,
253 unsigned int sizes[], void *allocators[])
255 const struct v4l2_pix_format_mplane *pixm = NULL;
256 struct fimc_ctx *ctx = vq->drv_priv;
257 struct fimc_frame *frame = &ctx->d_frame;
258 struct fimc_fmt *fmt = frame->fmt;
263 pixm = &pfmt->fmt.pix_mp;
264 fmt = fimc_find_format(&pixm->pixelformat, NULL,
265 FMT_FLAGS_CAM | FMT_FLAGS_M2M, -1);
266 wh = pixm->width * pixm->height;
268 wh = frame->f_width * frame->f_height;
274 *num_planes = fmt->memplanes;
276 for (i = 0; i < fmt->memplanes; i++) {
277 unsigned int size = (wh * fmt->depth[i]) / 8;
279 sizes[i] = max(size, pixm->plane_fmt[i].sizeimage);
282 allocators[i] = ctx->fimc_dev->alloc_ctx;
288 static int buffer_prepare(struct vb2_buffer *vb)
290 struct vb2_queue *vq = vb->vb2_queue;
291 struct fimc_ctx *ctx = vq->drv_priv;
294 if (ctx->d_frame.fmt == NULL)
297 for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
298 unsigned long size = ctx->d_frame.payload[i];
300 if (vb2_plane_size(vb, i) < size) {
301 v4l2_err(ctx->fimc_dev->vid_cap.vfd,
302 "User buffer too small (%ld < %ld)\n",
303 vb2_plane_size(vb, i), size);
306 vb2_set_plane_payload(vb, i, size);
312 static void buffer_queue(struct vb2_buffer *vb)
314 struct fimc_vid_buffer *buf
315 = container_of(vb, struct fimc_vid_buffer, vb);
316 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
317 struct fimc_dev *fimc = ctx->fimc_dev;
318 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
322 spin_lock_irqsave(&fimc->slock, flags);
323 fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);
325 if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
326 !test_bit(ST_CAPT_STREAM, &fimc->state) &&
327 vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
328 /* Setup the buffer directly for processing. */
329 int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
332 fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
333 buf->index = vid_cap->buf_index;
334 fimc_active_queue_add(vid_cap, buf);
336 if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
337 vid_cap->buf_index = 0;
339 fimc_pending_queue_add(vid_cap, buf);
342 min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
345 if (vb2_is_streaming(&vid_cap->vbq) &&
346 vid_cap->active_buf_cnt >= min_bufs &&
347 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
348 fimc_activate_capture(ctx);
349 spin_unlock_irqrestore(&fimc->slock, flags);
351 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
352 fimc_pipeline_s_stream(fimc, 1);
355 spin_unlock_irqrestore(&fimc->slock, flags);
358 static void fimc_lock(struct vb2_queue *vq)
360 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
361 mutex_lock(&ctx->fimc_dev->lock);
364 static void fimc_unlock(struct vb2_queue *vq)
366 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
367 mutex_unlock(&ctx->fimc_dev->lock);
370 static struct vb2_ops fimc_capture_qops = {
371 .queue_setup = queue_setup,
372 .buf_prepare = buffer_prepare,
373 .buf_queue = buffer_queue,
374 .wait_prepare = fimc_unlock,
375 .wait_finish = fimc_lock,
376 .start_streaming = start_streaming,
377 .stop_streaming = stop_streaming,
381 * fimc_capture_ctrls_create - initialize the control handler
382 * Initialize the capture video node control handler and fill it
383 * with the FIMC controls. Inherit any sensor's controls if the
384 * 'user_subdev_api' flag is false (default behaviour).
385 * This function need to be called with the graph mutex held.
387 int fimc_capture_ctrls_create(struct fimc_dev *fimc)
389 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
392 if (WARN_ON(vid_cap->ctx == NULL))
394 if (vid_cap->ctx->ctrls_rdy)
397 ret = fimc_ctrls_create(vid_cap->ctx);
398 if (ret || vid_cap->user_subdev_api)
401 return v4l2_ctrl_add_handler(&vid_cap->ctx->ctrl_handler,
402 fimc->pipeline.sensor->ctrl_handler);
405 static int fimc_capture_set_default_format(struct fimc_dev *fimc);
407 static int fimc_capture_open(struct file *file)
409 struct fimc_dev *fimc = video_drvdata(file);
410 int ret = v4l2_fh_open(file);
415 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
417 /* Return if the corresponding video mem2mem node is already opened. */
418 if (fimc_m2m_active(fimc))
421 set_bit(ST_CAPT_BUSY, &fimc->state);
422 pm_runtime_get_sync(&fimc->pdev->dev);
424 if (++fimc->vid_cap.refcnt == 1) {
425 ret = fimc_pipeline_initialize(fimc,
426 &fimc->vid_cap.vfd->entity, true);
428 dev_err(&fimc->pdev->dev,
429 "Video pipeline initialization failed\n");
430 pm_runtime_put_sync(&fimc->pdev->dev);
431 fimc->vid_cap.refcnt--;
432 v4l2_fh_release(file);
433 clear_bit(ST_CAPT_BUSY, &fimc->state);
436 ret = fimc_capture_ctrls_create(fimc);
438 if (!ret && !fimc->vid_cap.user_subdev_api)
439 ret = fimc_capture_set_default_format(fimc);
444 static int fimc_capture_close(struct file *file)
446 struct fimc_dev *fimc = video_drvdata(file);
448 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
450 if (--fimc->vid_cap.refcnt == 0) {
451 clear_bit(ST_CAPT_BUSY, &fimc->state);
452 fimc_stop_capture(fimc, false);
453 fimc_pipeline_shutdown(fimc);
454 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
457 pm_runtime_put(&fimc->pdev->dev);
459 if (fimc->vid_cap.refcnt == 0) {
460 vb2_queue_release(&fimc->vid_cap.vbq);
461 fimc_ctrls_delete(fimc->vid_cap.ctx);
463 return v4l2_fh_release(file);
466 static unsigned int fimc_capture_poll(struct file *file,
467 struct poll_table_struct *wait)
469 struct fimc_dev *fimc = video_drvdata(file);
471 return vb2_poll(&fimc->vid_cap.vbq, file, wait);
474 static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
476 struct fimc_dev *fimc = video_drvdata(file);
478 return vb2_mmap(&fimc->vid_cap.vbq, vma);
481 static const struct v4l2_file_operations fimc_capture_fops = {
482 .owner = THIS_MODULE,
483 .open = fimc_capture_open,
484 .release = fimc_capture_close,
485 .poll = fimc_capture_poll,
486 .unlocked_ioctl = video_ioctl2,
487 .mmap = fimc_capture_mmap,
491 * Format and crop negotiation helpers
494 static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
495 u32 *width, u32 *height,
496 u32 *code, u32 *fourcc, int pad)
498 bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
499 struct fimc_dev *fimc = ctx->fimc_dev;
500 struct samsung_fimc_variant *var = fimc->variant;
501 struct fimc_pix_limit *pl = var->pix_limit;
502 struct fimc_frame *dst = &ctx->d_frame;
503 u32 depth, min_w, max_w, min_h, align_h = 3;
504 u32 mask = FMT_FLAGS_CAM;
505 struct fimc_fmt *ffmt;
507 /* Color conversion from/to JPEG is not supported */
508 if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
509 fimc_fmt_is_jpeg(ctx->s_frame.fmt->color))
510 *code = V4L2_MBUS_FMT_JPEG_1X8;
512 if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad != FIMC_SD_PAD_SINK)
513 mask |= FMT_FLAGS_M2M;
515 ffmt = fimc_find_format(fourcc, code, mask, 0);
519 *code = ffmt->mbus_code;
521 *fourcc = ffmt->fourcc;
523 if (pad == FIMC_SD_PAD_SINK) {
524 max_w = fimc_fmt_is_jpeg(ffmt->color) ?
525 pl->scaler_dis_w : pl->scaler_en_w;
526 /* Apply the camera input interface pixel constraints */
527 v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
528 height, max_t(u32, *height, 32),
529 FIMC_CAMIF_MAX_HEIGHT,
530 fimc_fmt_is_jpeg(ffmt->color) ? 3 : 1,
534 /* Can't scale or crop in transparent (JPEG) transfer mode */
535 if (fimc_fmt_is_jpeg(ffmt->color)) {
536 *width = ctx->s_frame.f_width;
537 *height = ctx->s_frame.f_height;
540 /* Apply the scaler and the output DMA constraints */
541 max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
542 min_w = ctx->state & FIMC_DST_CROP ? dst->width : var->min_out_pixsize;
543 min_h = ctx->state & FIMC_DST_CROP ? dst->height : var->min_out_pixsize;
544 if (var->min_vsize_align == 1 && !rotation)
545 align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
547 depth = fimc_get_format_depth(ffmt);
548 v4l_bound_align_image(width, min_w, max_w,
549 ffs(var->min_out_pixsize) - 1,
550 height, min_h, FIMC_CAMIF_MAX_HEIGHT,
552 64/(ALIGN(depth, 8)));
554 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
555 pad, code ? *code : 0, *width, *height,
556 dst->f_width, dst->f_height);
561 static void fimc_capture_try_crop(struct fimc_ctx *ctx, struct v4l2_rect *r,
564 bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
565 struct fimc_dev *fimc = ctx->fimc_dev;
566 struct samsung_fimc_variant *var = fimc->variant;
567 struct fimc_pix_limit *pl = var->pix_limit;
568 struct fimc_frame *sink = &ctx->s_frame;
569 u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
570 u32 align_sz = 0, align_h = 4;
571 u32 max_sc_h, max_sc_v;
573 /* In JPEG transparent transfer mode cropping is not supported */
574 if (fimc_fmt_is_jpeg(ctx->d_frame.fmt->color)) {
575 r->width = sink->f_width;
576 r->height = sink->f_height;
577 r->left = r->top = 0;
580 if (pad == FIMC_SD_PAD_SOURCE) {
581 if (ctx->rotation != 90 && ctx->rotation != 270)
583 max_sc_h = min(SCALER_MAX_HRATIO, 1 << (ffs(sink->width) - 3));
584 max_sc_v = min(SCALER_MAX_VRATIO, 1 << (ffs(sink->height) - 1));
585 min_sz = var->min_out_pixsize;
587 u32 depth = fimc_get_format_depth(sink->fmt);
588 align_sz = 64/ALIGN(depth, 8);
589 min_sz = var->min_inp_pixsize;
590 min_w = min_h = min_sz;
591 max_sc_h = max_sc_v = 1;
594 * For the crop rectangle at source pad the following constraints
596 * - it must fit in the sink pad format rectangle (f_width/f_height);
597 * - maximum downscaling ratio is 64;
598 * - maximum crop size depends if the rotator is used or not;
599 * - the sink pad format width/height must be 4 multiple of the
600 * prescaler ratios determined by sink pad size and source pad crop,
601 * the prescaler ratio is returned by fimc_get_scaler_factor().
604 rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
605 rotate ? sink->f_height : sink->f_width);
606 max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
607 if (pad == FIMC_SD_PAD_SOURCE) {
608 min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
609 min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
611 swap(max_sc_h, max_sc_v);
615 v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
616 &r->height, min_h, max_h, align_h,
618 /* Adjust left/top if cropping rectangle is out of bounds */
619 r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
620 r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height);
621 r->left = round_down(r->left, var->hor_offs_align);
623 dbg("pad%d: (%d,%d)/%dx%d, sink fmt: %dx%d",
624 pad, r->left, r->top, r->width, r->height,
625 sink->f_width, sink->f_height);
629 * The video node ioctl operations
631 static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
632 struct v4l2_capability *cap)
634 struct fimc_dev *fimc = video_drvdata(file);
636 strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
637 strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
638 cap->bus_info[0] = 0;
639 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE_MPLANE;
644 static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv,
645 struct v4l2_fmtdesc *f)
647 struct fimc_fmt *fmt;
649 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
653 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
654 f->pixelformat = fmt->fourcc;
655 if (fmt->fourcc == V4L2_MBUS_FMT_JPEG_1X8)
656 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
661 * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
663 * @ctx: FIMC capture context
664 * @tfmt: media bus format to try/set on subdevs
665 * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
666 * @set: true to set format on subdevs, false to try only
668 static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
669 struct v4l2_mbus_framefmt *tfmt,
670 struct fimc_fmt **fmt_id,
673 struct fimc_dev *fimc = ctx->fimc_dev;
674 struct v4l2_subdev *sd = fimc->pipeline.sensor;
675 struct v4l2_subdev *csis = fimc->pipeline.csis;
676 struct v4l2_subdev_format sfmt;
677 struct v4l2_mbus_framefmt *mf = &sfmt.format;
678 struct fimc_fmt *ffmt = NULL;
681 if (WARN_ON(!sd || !tfmt))
684 memset(&sfmt, 0, sizeof(sfmt));
687 sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
689 ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
693 * Notify user-space if common pixel code for
694 * host and sensor does not exist.
698 mf->code = tfmt->code = ffmt->mbus_code;
700 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
703 if (mf->code != tfmt->code) {
707 if (mf->width != tfmt->width || mf->height != tfmt->height) {
708 u32 fcc = ffmt->fourcc;
709 tfmt->width = mf->width;
710 tfmt->height = mf->height;
711 ffmt = fimc_capture_try_format(ctx,
712 &tfmt->width, &tfmt->height,
713 NULL, &fcc, FIMC_SD_PAD_SOURCE);
714 if (ffmt && ffmt->mbus_code)
715 mf->code = ffmt->mbus_code;
716 if (mf->width != tfmt->width ||
717 mf->height != tfmt->height)
719 tfmt->code = mf->code;
722 ret = v4l2_subdev_call(csis, pad, set_fmt, NULL, &sfmt);
724 if (mf->code == tfmt->code &&
725 mf->width == tfmt->width && mf->height == tfmt->height)
733 dbg("code: 0x%x, %dx%d, %p", mf->code, mf->width, mf->height, ffmt);
737 static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
738 struct v4l2_format *f)
740 struct fimc_dev *fimc = video_drvdata(file);
741 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
743 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
746 return fimc_fill_format(&ctx->d_frame, f);
749 static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
750 struct v4l2_format *f)
752 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
753 struct fimc_dev *fimc = video_drvdata(file);
754 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
755 struct v4l2_mbus_framefmt mf;
756 struct fimc_fmt *ffmt = NULL;
758 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
761 if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
762 fimc_capture_try_format(ctx, &pix->width, &pix->height,
763 NULL, &pix->pixelformat,
765 ctx->s_frame.f_width = pix->width;
766 ctx->s_frame.f_height = pix->height;
768 ffmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
769 NULL, &pix->pixelformat,
774 if (!fimc->vid_cap.user_subdev_api) {
775 mf.width = pix->width;
776 mf.height = pix->height;
777 mf.code = ffmt->mbus_code;
778 fimc_md_graph_lock(fimc);
779 fimc_pipeline_try_format(ctx, &mf, &ffmt, false);
780 fimc_md_graph_unlock(fimc);
782 pix->width = mf.width;
783 pix->height = mf.height;
785 pix->pixelformat = ffmt->fourcc;
788 fimc_adjust_mplane_format(ffmt, pix->width, pix->height, pix);
792 static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx, bool jpeg)
794 ctx->scaler.enabled = !jpeg;
795 fimc_ctrls_activate(ctx, !jpeg);
798 set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
800 clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
803 static int fimc_capture_set_format(struct fimc_dev *fimc, struct v4l2_format *f)
805 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
806 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
807 struct v4l2_mbus_framefmt *mf = &fimc->vid_cap.mf;
808 struct fimc_frame *ff = &ctx->d_frame;
809 struct fimc_fmt *s_fmt = NULL;
812 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
814 if (vb2_is_busy(&fimc->vid_cap.vbq))
817 /* Pre-configure format at camera interface input, for JPEG only */
818 if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
819 fimc_capture_try_format(ctx, &pix->width, &pix->height,
820 NULL, &pix->pixelformat,
822 ctx->s_frame.f_width = pix->width;
823 ctx->s_frame.f_height = pix->height;
825 /* Try the format at the scaler and the DMA output */
826 ff->fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
827 NULL, &pix->pixelformat,
832 /* Update RGB Alpha control state and value range */
833 fimc_alpha_ctrl_update(ctx);
835 /* Try to match format at the host and the sensor */
836 if (!fimc->vid_cap.user_subdev_api) {
837 mf->code = ff->fmt->mbus_code;
838 mf->width = pix->width;
839 mf->height = pix->height;
841 fimc_md_graph_lock(fimc);
842 ret = fimc_pipeline_try_format(ctx, mf, &s_fmt, true);
843 fimc_md_graph_unlock(fimc);
846 pix->width = mf->width;
847 pix->height = mf->height;
849 fimc_adjust_mplane_format(ff->fmt, pix->width, pix->height, pix);
850 for (i = 0; i < ff->fmt->colplanes; i++)
852 (pix->width * pix->height * ff->fmt->depth[i]) / 8;
854 set_frame_bounds(ff, pix->width, pix->height);
855 /* Reset the composition rectangle if not yet configured */
856 if (!(ctx->state & FIMC_DST_CROP))
857 set_frame_crop(ff, 0, 0, pix->width, pix->height);
859 fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ff->fmt->color));
861 /* Reset cropping and set format at the camera interface input */
862 if (!fimc->vid_cap.user_subdev_api) {
863 ctx->s_frame.fmt = s_fmt;
864 set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
865 set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
871 static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
872 struct v4l2_format *f)
874 struct fimc_dev *fimc = video_drvdata(file);
876 return fimc_capture_set_format(fimc, f);
879 static int fimc_cap_enum_input(struct file *file, void *priv,
880 struct v4l2_input *i)
882 struct fimc_dev *fimc = video_drvdata(file);
883 struct v4l2_subdev *sd = fimc->pipeline.sensor;
888 i->type = V4L2_INPUT_TYPE_CAMERA;
890 strlcpy(i->name, sd->name, sizeof(i->name));
894 static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
896 return i == 0 ? i : -EINVAL;
899 static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
906 * fimc_pipeline_validate - check for formats inconsistencies
907 * between source and sink pad of each link
909 * Return 0 if all formats match or -EPIPE otherwise.
911 static int fimc_pipeline_validate(struct fimc_dev *fimc)
913 struct v4l2_subdev_format sink_fmt, src_fmt;
914 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
915 struct v4l2_subdev *sd;
916 struct media_pad *pad;
919 /* Start with the video capture node pad */
920 pad = media_entity_remote_source(&vid_cap->vd_pad);
923 /* FIMC.{N} subdevice */
924 sd = media_entity_to_v4l2_subdev(pad->entity);
927 /* Retrieve format at the sink pad */
928 pad = &sd->entity.pads[0];
929 if (!(pad->flags & MEDIA_PAD_FL_SINK))
931 /* Don't call FIMC subdev operation to avoid nested locking */
932 if (sd == fimc->vid_cap.subdev) {
933 struct fimc_frame *ff = &vid_cap->ctx->s_frame;
934 sink_fmt.format.width = ff->f_width;
935 sink_fmt.format.height = ff->f_height;
936 sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
938 sink_fmt.pad = pad->index;
939 sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
940 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
941 if (ret < 0 && ret != -ENOIOCTLCMD)
944 /* Retrieve format at the source pad */
945 pad = media_entity_remote_source(pad);
947 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
950 sd = media_entity_to_v4l2_subdev(pad->entity);
951 src_fmt.pad = pad->index;
952 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
953 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
954 if (ret < 0 && ret != -ENOIOCTLCMD)
957 if (src_fmt.format.width != sink_fmt.format.width ||
958 src_fmt.format.height != sink_fmt.format.height ||
959 src_fmt.format.code != sink_fmt.format.code)
965 static int fimc_cap_streamon(struct file *file, void *priv,
966 enum v4l2_buf_type type)
968 struct fimc_dev *fimc = video_drvdata(file);
969 struct fimc_pipeline *p = &fimc->pipeline;
972 if (fimc_capture_active(fimc))
975 media_entity_pipeline_start(&p->sensor->entity, p->pipe);
977 if (fimc->vid_cap.user_subdev_api) {
978 ret = fimc_pipeline_validate(fimc);
982 return vb2_streamon(&fimc->vid_cap.vbq, type);
985 static int fimc_cap_streamoff(struct file *file, void *priv,
986 enum v4l2_buf_type type)
988 struct fimc_dev *fimc = video_drvdata(file);
989 struct v4l2_subdev *sd = fimc->pipeline.sensor;
992 ret = vb2_streamoff(&fimc->vid_cap.vbq, type);
994 media_entity_pipeline_stop(&sd->entity);
998 static int fimc_cap_reqbufs(struct file *file, void *priv,
999 struct v4l2_requestbuffers *reqbufs)
1001 struct fimc_dev *fimc = video_drvdata(file);
1002 int ret = vb2_reqbufs(&fimc->vid_cap.vbq, reqbufs);
1005 fimc->vid_cap.reqbufs_count = reqbufs->count;
1009 static int fimc_cap_querybuf(struct file *file, void *priv,
1010 struct v4l2_buffer *buf)
1012 struct fimc_dev *fimc = video_drvdata(file);
1014 return vb2_querybuf(&fimc->vid_cap.vbq, buf);
1017 static int fimc_cap_qbuf(struct file *file, void *priv,
1018 struct v4l2_buffer *buf)
1020 struct fimc_dev *fimc = video_drvdata(file);
1022 return vb2_qbuf(&fimc->vid_cap.vbq, buf);
1025 static int fimc_cap_dqbuf(struct file *file, void *priv,
1026 struct v4l2_buffer *buf)
1028 struct fimc_dev *fimc = video_drvdata(file);
1030 return vb2_dqbuf(&fimc->vid_cap.vbq, buf, file->f_flags & O_NONBLOCK);
1033 static int fimc_cap_create_bufs(struct file *file, void *priv,
1034 struct v4l2_create_buffers *create)
1036 struct fimc_dev *fimc = video_drvdata(file);
1038 return vb2_create_bufs(&fimc->vid_cap.vbq, create);
1041 static int fimc_cap_prepare_buf(struct file *file, void *priv,
1042 struct v4l2_buffer *b)
1044 struct fimc_dev *fimc = video_drvdata(file);
1046 return vb2_prepare_buf(&fimc->vid_cap.vbq, b);
1049 static int fimc_cap_g_selection(struct file *file, void *fh,
1050 struct v4l2_selection *s)
1052 struct fimc_dev *fimc = video_drvdata(file);
1053 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1054 struct fimc_frame *f = &ctx->s_frame;
1056 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1059 switch (s->target) {
1060 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1061 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1063 case V4L2_SEL_TGT_CROP_BOUNDS:
1064 case V4L2_SEL_TGT_CROP_DEFAULT:
1067 s->r.width = f->o_width;
1068 s->r.height = f->o_height;
1071 case V4L2_SEL_TGT_COMPOSE_ACTIVE:
1073 case V4L2_SEL_TGT_CROP_ACTIVE:
1074 s->r.left = f->offs_h;
1075 s->r.top = f->offs_v;
1076 s->r.width = f->width;
1077 s->r.height = f->height;
1084 /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
1085 int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
1087 if (a->left < b->left || a->top < b->top)
1089 if (a->left + a->width > b->left + b->width)
1091 if (a->top + a->height > b->top + b->height)
1097 static int fimc_cap_s_selection(struct file *file, void *fh,
1098 struct v4l2_selection *s)
1100 struct fimc_dev *fimc = video_drvdata(file);
1101 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1102 struct v4l2_rect rect = s->r;
1103 struct fimc_frame *f;
1104 unsigned long flags;
1107 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1110 switch (s->target) {
1111 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1112 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1113 case V4L2_SEL_TGT_COMPOSE_ACTIVE:
1115 pad = FIMC_SD_PAD_SOURCE;
1117 case V4L2_SEL_TGT_CROP_BOUNDS:
1118 case V4L2_SEL_TGT_CROP_DEFAULT:
1119 case V4L2_SEL_TGT_CROP_ACTIVE:
1121 pad = FIMC_SD_PAD_SINK;
1127 fimc_capture_try_crop(ctx, &rect, pad);
1129 if (s->flags & V4L2_SEL_FLAG_LE &&
1130 !enclosed_rectangle(&rect, &s->r))
1133 if (s->flags & V4L2_SEL_FLAG_GE &&
1134 !enclosed_rectangle(&s->r, &rect))
1138 spin_lock_irqsave(&fimc->slock, flags);
1139 set_frame_crop(f, s->r.left, s->r.top, s->r.width,
1141 spin_unlock_irqrestore(&fimc->slock, flags);
1143 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1147 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
1148 .vidioc_querycap = fimc_vidioc_querycap_capture,
1150 .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane,
1151 .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane,
1152 .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
1153 .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane,
1155 .vidioc_reqbufs = fimc_cap_reqbufs,
1156 .vidioc_querybuf = fimc_cap_querybuf,
1158 .vidioc_qbuf = fimc_cap_qbuf,
1159 .vidioc_dqbuf = fimc_cap_dqbuf,
1161 .vidioc_prepare_buf = fimc_cap_prepare_buf,
1162 .vidioc_create_bufs = fimc_cap_create_bufs,
1164 .vidioc_streamon = fimc_cap_streamon,
1165 .vidioc_streamoff = fimc_cap_streamoff,
1167 .vidioc_g_selection = fimc_cap_g_selection,
1168 .vidioc_s_selection = fimc_cap_s_selection,
1170 .vidioc_enum_input = fimc_cap_enum_input,
1171 .vidioc_s_input = fimc_cap_s_input,
1172 .vidioc_g_input = fimc_cap_g_input,
1175 /* Capture subdev media entity operations */
1176 static int fimc_link_setup(struct media_entity *entity,
1177 const struct media_pad *local,
1178 const struct media_pad *remote, u32 flags)
1180 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1181 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1183 if (media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1186 if (WARN_ON(fimc == NULL))
1189 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1190 local->entity->name, remote->entity->name, flags,
1191 fimc->vid_cap.input);
1193 if (flags & MEDIA_LNK_FL_ENABLED) {
1194 if (fimc->vid_cap.input != 0)
1196 fimc->vid_cap.input = sd->grp_id;
1200 fimc->vid_cap.input = 0;
1204 static const struct media_entity_operations fimc_sd_media_ops = {
1205 .link_setup = fimc_link_setup,
1209 * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1210 * @sd: pointer to a subdev generating the notification
1211 * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1212 * @arg: pointer to an u32 type integer that stores the frame payload value
1214 * The End Of Frame notification sent by sensor subdev in its still capture
1215 * mode. If there is only a single VSYNC generated by the sensor at the
1216 * beginning of a frame transmission, FIMC does not issue the LastIrq
1217 * (end of frame) interrupt. And this notification is used to complete the
1218 * frame capture and returning a buffer to user-space. Subdev drivers should
1219 * call this notification from their last 'End of frame capture' interrupt.
1221 void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
1224 struct fimc_sensor_info *sensor;
1225 struct fimc_vid_buffer *buf;
1226 struct fimc_md *fmd;
1227 struct fimc_dev *fimc;
1228 unsigned long flags;
1233 sensor = v4l2_get_subdev_hostdata(sd);
1234 fmd = entity_to_fimc_mdev(&sd->entity);
1236 spin_lock_irqsave(&fmd->slock, flags);
1237 fimc = sensor ? sensor->host : NULL;
1239 if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
1240 test_bit(ST_CAPT_PEND, &fimc->state)) {
1241 unsigned long irq_flags;
1242 spin_lock_irqsave(&fimc->slock, irq_flags);
1243 if (!list_empty(&fimc->vid_cap.active_buf_q)) {
1244 buf = list_entry(fimc->vid_cap.active_buf_q.next,
1245 struct fimc_vid_buffer, list);
1246 vb2_set_plane_payload(&buf->vb, 0, *((u32 *)arg));
1248 fimc_capture_irq_handler(fimc, true);
1249 fimc_deactivate_capture(fimc);
1250 spin_unlock_irqrestore(&fimc->slock, irq_flags);
1252 spin_unlock_irqrestore(&fmd->slock, flags);
1255 static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1256 struct v4l2_subdev_fh *fh,
1257 struct v4l2_subdev_mbus_code_enum *code)
1259 struct fimc_fmt *fmt;
1261 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1264 code->code = fmt->mbus_code;
1268 static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
1269 struct v4l2_subdev_fh *fh,
1270 struct v4l2_subdev_format *fmt)
1272 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1273 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1274 struct v4l2_mbus_framefmt *mf;
1275 struct fimc_frame *ff;
1277 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1278 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1283 mf->colorspace = V4L2_COLORSPACE_JPEG;
1284 ff = fmt->pad == FIMC_SD_PAD_SINK ? &ctx->s_frame : &ctx->d_frame;
1286 mutex_lock(&fimc->lock);
1287 /* The pixel code is same on both input and output pad */
1288 if (!WARN_ON(ctx->s_frame.fmt == NULL))
1289 mf->code = ctx->s_frame.fmt->mbus_code;
1290 mf->width = ff->f_width;
1291 mf->height = ff->f_height;
1292 mutex_unlock(&fimc->lock);
1297 static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
1298 struct v4l2_subdev_fh *fh,
1299 struct v4l2_subdev_format *fmt)
1301 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1302 struct v4l2_mbus_framefmt *mf = &fmt->format;
1303 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1304 struct fimc_frame *ff;
1305 struct fimc_fmt *ffmt;
1307 dbg("pad%d: code: 0x%x, %dx%d",
1308 fmt->pad, mf->code, mf->width, mf->height);
1310 if (fmt->pad == FIMC_SD_PAD_SOURCE &&
1311 vb2_is_busy(&fimc->vid_cap.vbq))
1314 mutex_lock(&fimc->lock);
1315 ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1316 &mf->code, NULL, fmt->pad);
1317 mutex_unlock(&fimc->lock);
1318 mf->colorspace = V4L2_COLORSPACE_JPEG;
1320 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1321 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1325 /* Update RGB Alpha control state and value range */
1326 fimc_alpha_ctrl_update(ctx);
1328 fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ffmt->color));
1330 ff = fmt->pad == FIMC_SD_PAD_SINK ?
1331 &ctx->s_frame : &ctx->d_frame;
1333 mutex_lock(&fimc->lock);
1334 set_frame_bounds(ff, mf->width, mf->height);
1335 fimc->vid_cap.mf = *mf;
1338 /* Reset the crop rectangle if required. */
1339 if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_DST_CROP)))
1340 set_frame_crop(ff, 0, 0, mf->width, mf->height);
1342 if (fmt->pad == FIMC_SD_PAD_SINK)
1343 ctx->state &= ~FIMC_DST_CROP;
1344 mutex_unlock(&fimc->lock);
1348 static int fimc_subdev_get_crop(struct v4l2_subdev *sd,
1349 struct v4l2_subdev_fh *fh,
1350 struct v4l2_subdev_crop *crop)
1352 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1353 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1354 struct v4l2_rect *r = &crop->rect;
1355 struct fimc_frame *ff;
1357 if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
1358 crop->rect = *v4l2_subdev_get_try_crop(fh, crop->pad);
1361 ff = crop->pad == FIMC_SD_PAD_SINK ?
1362 &ctx->s_frame : &ctx->d_frame;
1364 mutex_lock(&fimc->lock);
1365 r->left = ff->offs_h;
1366 r->top = ff->offs_v;
1367 r->width = ff->width;
1368 r->height = ff->height;
1369 mutex_unlock(&fimc->lock);
1371 dbg("ff:%p, pad%d: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1372 ff, crop->pad, r->left, r->top, r->width, r->height,
1373 ff->f_width, ff->f_height);
1378 static int fimc_subdev_set_crop(struct v4l2_subdev *sd,
1379 struct v4l2_subdev_fh *fh,
1380 struct v4l2_subdev_crop *crop)
1382 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1383 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1384 struct v4l2_rect *r = &crop->rect;
1385 struct fimc_frame *ff;
1386 unsigned long flags;
1388 dbg("(%d,%d)/%dx%d", r->left, r->top, r->width, r->height);
1390 ff = crop->pad == FIMC_SD_PAD_SOURCE ?
1391 &ctx->d_frame : &ctx->s_frame;
1393 mutex_lock(&fimc->lock);
1394 fimc_capture_try_crop(ctx, r, crop->pad);
1396 if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
1397 mutex_unlock(&fimc->lock);
1398 *v4l2_subdev_get_try_crop(fh, crop->pad) = *r;
1401 spin_lock_irqsave(&fimc->slock, flags);
1402 set_frame_crop(ff, r->left, r->top, r->width, r->height);
1403 if (crop->pad == FIMC_SD_PAD_SOURCE)
1404 ctx->state |= FIMC_DST_CROP;
1406 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1407 spin_unlock_irqrestore(&fimc->slock, flags);
1409 dbg("pad%d: (%d,%d)/%dx%d", crop->pad, r->left, r->top,
1410 r->width, r->height);
1412 mutex_unlock(&fimc->lock);
1416 static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
1417 .enum_mbus_code = fimc_subdev_enum_mbus_code,
1418 .get_fmt = fimc_subdev_get_fmt,
1419 .set_fmt = fimc_subdev_set_fmt,
1420 .get_crop = fimc_subdev_get_crop,
1421 .set_crop = fimc_subdev_set_crop,
1424 static struct v4l2_subdev_ops fimc_subdev_ops = {
1425 .pad = &fimc_subdev_pad_ops,
1428 static int fimc_create_capture_subdev(struct fimc_dev *fimc,
1429 struct v4l2_device *v4l2_dev)
1431 struct v4l2_subdev *sd;
1434 sd = kzalloc(sizeof(*sd), GFP_KERNEL);
1438 v4l2_subdev_init(sd, &fimc_subdev_ops);
1439 sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
1440 snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->pdev->id);
1442 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1443 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1444 ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
1445 fimc->vid_cap.sd_pads, 0);
1448 ret = v4l2_device_register_subdev(v4l2_dev, sd);
1452 fimc->vid_cap.subdev = sd;
1453 v4l2_set_subdevdata(sd, fimc);
1454 sd->entity.ops = &fimc_sd_media_ops;
1457 media_entity_cleanup(&sd->entity);
1463 static void fimc_destroy_capture_subdev(struct fimc_dev *fimc)
1465 struct v4l2_subdev *sd = fimc->vid_cap.subdev;
1469 media_entity_cleanup(&sd->entity);
1470 v4l2_device_unregister_subdev(sd);
1472 fimc->vid_cap.subdev = NULL;
1475 /* Set default format at the sensor and host interface */
1476 static int fimc_capture_set_default_format(struct fimc_dev *fimc)
1478 struct v4l2_format fmt = {
1479 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1483 .pixelformat = V4L2_PIX_FMT_YUYV,
1484 .field = V4L2_FIELD_NONE,
1485 .colorspace = V4L2_COLORSPACE_JPEG,
1489 return fimc_capture_set_format(fimc, &fmt);
1492 /* fimc->lock must be already initialized */
1493 int fimc_register_capture_device(struct fimc_dev *fimc,
1494 struct v4l2_device *v4l2_dev)
1496 struct video_device *vfd;
1497 struct fimc_vid_cap *vid_cap;
1498 struct fimc_ctx *ctx;
1499 struct vb2_queue *q;
1502 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1506 ctx->fimc_dev = fimc;
1507 ctx->in_path = FIMC_CAMERA;
1508 ctx->out_path = FIMC_DMA;
1509 ctx->state = FIMC_CTX_CAP;
1510 ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1511 ctx->d_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1513 vfd = video_device_alloc();
1515 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
1519 snprintf(vfd->name, sizeof(vfd->name), "%s.capture",
1520 dev_name(&fimc->pdev->dev));
1522 vfd->fops = &fimc_capture_fops;
1523 vfd->ioctl_ops = &fimc_capture_ioctl_ops;
1524 vfd->v4l2_dev = v4l2_dev;
1526 vfd->release = video_device_release;
1527 vfd->lock = &fimc->lock;
1528 /* Locking in file operations other than ioctl should be done
1529 by the driver, not the V4L2 core.
1530 This driver needs auditing so that this flag can be removed. */
1531 set_bit(V4L2_FL_LOCK_ALL_FOPS, &vfd->flags);
1532 video_set_drvdata(vfd, fimc);
1534 vid_cap = &fimc->vid_cap;
1536 vid_cap->active_buf_cnt = 0;
1537 vid_cap->reqbufs_count = 0;
1538 vid_cap->refcnt = 0;
1540 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1541 INIT_LIST_HEAD(&vid_cap->active_buf_q);
1544 q = &fimc->vid_cap.vbq;
1545 memset(q, 0, sizeof(*q));
1546 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1547 q->io_modes = VB2_MMAP | VB2_USERPTR;
1548 q->drv_priv = fimc->vid_cap.ctx;
1549 q->ops = &fimc_capture_qops;
1550 q->mem_ops = &vb2_dma_contig_memops;
1551 q->buf_struct_size = sizeof(struct fimc_vid_buffer);
1555 fimc->vid_cap.vd_pad.flags = MEDIA_PAD_FL_SINK;
1556 ret = media_entity_init(&vfd->entity, 1, &fimc->vid_cap.vd_pad, 0);
1559 ret = fimc_create_capture_subdev(fimc, v4l2_dev);
1563 vfd->ctrl_handler = &ctx->ctrl_handler;
1567 media_entity_cleanup(&vfd->entity);
1569 video_device_release(vfd);
1575 void fimc_unregister_capture_device(struct fimc_dev *fimc)
1577 struct video_device *vfd = fimc->vid_cap.vfd;
1580 media_entity_cleanup(&vfd->entity);
1581 /* Can also be called if video device was
1583 video_unregister_device(vfd);
1585 fimc_destroy_capture_subdev(fimc);
1586 kfree(fimc->vid_cap.ctx);
1587 fimc->vid_cap.ctx = NULL;