2 * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver
4 * Copyright (C) 2010 - 2012 Samsung Electronics Co., Ltd.
5 * 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"
34 static int fimc_capture_hw_init(struct fimc_dev *fimc)
36 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
37 struct fimc_pipeline *p = &fimc->pipeline;
38 struct fimc_sensor_info *sensor;
42 if (p->subdevs[IDX_SENSOR] == NULL || ctx == NULL)
44 if (ctx->s_frame.fmt == NULL)
47 sensor = v4l2_get_subdev_hostdata(p->subdevs[IDX_SENSOR]);
49 spin_lock_irqsave(&fimc->slock, flags);
50 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
51 fimc_set_yuv_order(ctx);
53 fimc_hw_set_camera_polarity(fimc, sensor->pdata);
54 fimc_hw_set_camera_type(fimc, sensor->pdata);
55 fimc_hw_set_camera_source(fimc, sensor->pdata);
56 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
58 ret = fimc_set_scaler_info(ctx);
60 fimc_hw_set_input_path(ctx);
61 fimc_hw_set_prescaler(ctx);
62 fimc_hw_set_mainscaler(ctx);
63 fimc_hw_set_target_format(ctx);
64 fimc_hw_set_rotation(ctx);
65 fimc_hw_set_effect(ctx);
66 fimc_hw_set_output_path(ctx);
67 fimc_hw_set_out_dma(ctx);
68 if (fimc->variant->has_alpha)
69 fimc_hw_set_rgb_alpha(ctx);
70 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
72 spin_unlock_irqrestore(&fimc->slock, flags);
77 * Reinitialize the driver so it is ready to start the streaming again.
78 * Set fimc->state to indicate stream off and the hardware shut down state.
79 * If not suspending (@suspend is false), return any buffers to videobuf2.
80 * Otherwise put any owned buffers onto the pending buffers queue, so they
81 * can be re-spun when the device is being resumed. Also perform FIMC
82 * software reset and disable streaming on the whole pipeline if required.
84 static int fimc_capture_state_cleanup(struct fimc_dev *fimc, bool suspend)
86 struct fimc_vid_cap *cap = &fimc->vid_cap;
87 struct fimc_vid_buffer *buf;
91 spin_lock_irqsave(&fimc->slock, flags);
92 streaming = fimc->state & (1 << ST_CAPT_ISP_STREAM);
94 fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_SHUT |
95 1 << ST_CAPT_STREAM | 1 << ST_CAPT_ISP_STREAM);
97 fimc->state |= (1 << ST_CAPT_SUSPENDED);
99 fimc->state &= ~(1 << ST_CAPT_PEND | 1 << ST_CAPT_SUSPENDED);
101 /* Release unused buffers */
102 while (!suspend && !list_empty(&cap->pending_buf_q)) {
103 buf = fimc_pending_queue_pop(cap);
104 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
106 /* If suspending put unused buffers onto pending queue */
107 while (!list_empty(&cap->active_buf_q)) {
108 buf = fimc_active_queue_pop(cap);
110 fimc_pending_queue_add(cap, buf);
112 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
118 spin_unlock_irqrestore(&fimc->slock, flags);
121 return fimc_pipeline_s_stream(&fimc->pipeline, 0);
126 static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend)
130 if (!fimc_capture_active(fimc))
133 spin_lock_irqsave(&fimc->slock, flags);
134 set_bit(ST_CAPT_SHUT, &fimc->state);
135 fimc_deactivate_capture(fimc);
136 spin_unlock_irqrestore(&fimc->slock, flags);
138 wait_event_timeout(fimc->irq_queue,
139 !test_bit(ST_CAPT_SHUT, &fimc->state),
140 (2*HZ/10)); /* 200 ms */
142 return fimc_capture_state_cleanup(fimc, suspend);
146 * fimc_capture_config_update - apply the camera interface configuration
148 * To be called from within the interrupt handler with fimc.slock
149 * spinlock held. It updates the camera pixel crop, rotation and
152 static int fimc_capture_config_update(struct fimc_ctx *ctx)
154 struct fimc_dev *fimc = ctx->fimc_dev;
157 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
159 ret = fimc_set_scaler_info(ctx);
163 fimc_hw_set_prescaler(ctx);
164 fimc_hw_set_mainscaler(ctx);
165 fimc_hw_set_target_format(ctx);
166 fimc_hw_set_rotation(ctx);
167 fimc_hw_set_effect(ctx);
168 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
169 fimc_hw_set_out_dma(ctx);
170 if (fimc->variant->has_alpha)
171 fimc_hw_set_rgb_alpha(ctx);
173 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
177 void fimc_capture_irq_handler(struct fimc_dev *fimc, int deq_buf)
179 struct fimc_vid_cap *cap = &fimc->vid_cap;
180 struct fimc_vid_buffer *v_buf;
184 if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) {
185 wake_up(&fimc->irq_queue);
189 if (!list_empty(&cap->active_buf_q) &&
190 test_bit(ST_CAPT_RUN, &fimc->state) && deq_buf) {
191 ktime_get_real_ts(&ts);
193 v_buf = fimc_active_queue_pop(cap);
195 tv = &v_buf->vb.v4l2_buf.timestamp;
196 tv->tv_sec = ts.tv_sec;
197 tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
198 v_buf->vb.v4l2_buf.sequence = cap->frame_count++;
200 vb2_buffer_done(&v_buf->vb, VB2_BUF_STATE_DONE);
203 if (!list_empty(&cap->pending_buf_q)) {
205 v_buf = fimc_pending_queue_pop(cap);
206 fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index);
207 v_buf->index = cap->buf_index;
209 /* Move the buffer to the capture active queue */
210 fimc_active_queue_add(cap, v_buf);
212 dbg("next frame: %d, done frame: %d",
213 fimc_hw_get_frame_index(fimc), v_buf->index);
215 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
219 if (cap->active_buf_cnt == 0) {
221 clear_bit(ST_CAPT_RUN, &fimc->state);
223 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
226 set_bit(ST_CAPT_RUN, &fimc->state);
229 if (test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
230 fimc_capture_config_update(cap->ctx);
232 if (cap->active_buf_cnt == 1) {
233 fimc_deactivate_capture(fimc);
234 clear_bit(ST_CAPT_STREAM, &fimc->state);
237 dbg("frame: %d, active_buf_cnt: %d",
238 fimc_hw_get_frame_index(fimc), cap->active_buf_cnt);
242 static int start_streaming(struct vb2_queue *q, unsigned int count)
244 struct fimc_ctx *ctx = q->drv_priv;
245 struct fimc_dev *fimc = ctx->fimc_dev;
246 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
250 vid_cap->frame_count = 0;
252 ret = fimc_capture_hw_init(fimc);
254 fimc_capture_state_cleanup(fimc, false);
258 set_bit(ST_CAPT_PEND, &fimc->state);
260 min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
262 if (vid_cap->active_buf_cnt >= min_bufs &&
263 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
264 fimc_activate_capture(ctx);
266 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
267 fimc_pipeline_s_stream(&fimc->pipeline, 1);
273 static int stop_streaming(struct vb2_queue *q)
275 struct fimc_ctx *ctx = q->drv_priv;
276 struct fimc_dev *fimc = ctx->fimc_dev;
278 if (!fimc_capture_active(fimc))
281 return fimc_stop_capture(fimc, false);
284 int fimc_capture_suspend(struct fimc_dev *fimc)
286 bool suspend = fimc_capture_busy(fimc);
288 int ret = fimc_stop_capture(fimc, suspend);
291 return fimc_pipeline_shutdown(&fimc->pipeline);
294 static void buffer_queue(struct vb2_buffer *vb);
296 int fimc_capture_resume(struct fimc_dev *fimc)
298 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
299 struct fimc_vid_buffer *buf;
302 if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state))
305 INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
306 vid_cap->buf_index = 0;
307 fimc_pipeline_initialize(&fimc->pipeline, &vid_cap->vfd->entity,
309 fimc_capture_hw_init(fimc);
311 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
313 for (i = 0; i < vid_cap->reqbufs_count; i++) {
314 if (list_empty(&vid_cap->pending_buf_q))
316 buf = fimc_pending_queue_pop(vid_cap);
317 buffer_queue(&buf->vb);
323 static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
324 unsigned int *num_buffers, unsigned int *num_planes,
325 unsigned int sizes[], void *allocators[])
327 const struct v4l2_pix_format_mplane *pixm = NULL;
328 struct fimc_ctx *ctx = vq->drv_priv;
329 struct fimc_frame *frame = &ctx->d_frame;
330 struct fimc_fmt *fmt = frame->fmt;
335 pixm = &pfmt->fmt.pix_mp;
336 fmt = fimc_find_format(&pixm->pixelformat, NULL,
337 FMT_FLAGS_CAM | FMT_FLAGS_M2M, -1);
338 wh = pixm->width * pixm->height;
340 wh = frame->f_width * frame->f_height;
346 *num_planes = fmt->memplanes;
348 for (i = 0; i < fmt->memplanes; i++) {
349 unsigned int size = (wh * fmt->depth[i]) / 8;
351 sizes[i] = max(size, pixm->plane_fmt[i].sizeimage);
353 sizes[i] = max_t(u32, size, frame->payload[i]);
355 allocators[i] = ctx->fimc_dev->alloc_ctx;
361 static int buffer_prepare(struct vb2_buffer *vb)
363 struct vb2_queue *vq = vb->vb2_queue;
364 struct fimc_ctx *ctx = vq->drv_priv;
367 if (ctx->d_frame.fmt == NULL)
370 for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
371 unsigned long size = ctx->d_frame.payload[i];
373 if (vb2_plane_size(vb, i) < size) {
374 v4l2_err(ctx->fimc_dev->vid_cap.vfd,
375 "User buffer too small (%ld < %ld)\n",
376 vb2_plane_size(vb, i), size);
379 vb2_set_plane_payload(vb, i, size);
385 static void buffer_queue(struct vb2_buffer *vb)
387 struct fimc_vid_buffer *buf
388 = container_of(vb, struct fimc_vid_buffer, vb);
389 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
390 struct fimc_dev *fimc = ctx->fimc_dev;
391 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
395 spin_lock_irqsave(&fimc->slock, flags);
396 fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);
398 if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
399 !test_bit(ST_CAPT_STREAM, &fimc->state) &&
400 vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
401 /* Setup the buffer directly for processing. */
402 int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
405 fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
406 buf->index = vid_cap->buf_index;
407 fimc_active_queue_add(vid_cap, buf);
409 if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
410 vid_cap->buf_index = 0;
412 fimc_pending_queue_add(vid_cap, buf);
415 min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
418 if (vb2_is_streaming(&vid_cap->vbq) &&
419 vid_cap->active_buf_cnt >= min_bufs &&
420 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
421 fimc_activate_capture(ctx);
422 spin_unlock_irqrestore(&fimc->slock, flags);
424 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
425 fimc_pipeline_s_stream(&fimc->pipeline, 1);
428 spin_unlock_irqrestore(&fimc->slock, flags);
431 static void fimc_lock(struct vb2_queue *vq)
433 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
434 mutex_lock(&ctx->fimc_dev->lock);
437 static void fimc_unlock(struct vb2_queue *vq)
439 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
440 mutex_unlock(&ctx->fimc_dev->lock);
443 static struct vb2_ops fimc_capture_qops = {
444 .queue_setup = queue_setup,
445 .buf_prepare = buffer_prepare,
446 .buf_queue = buffer_queue,
447 .wait_prepare = fimc_unlock,
448 .wait_finish = fimc_lock,
449 .start_streaming = start_streaming,
450 .stop_streaming = stop_streaming,
454 * fimc_capture_ctrls_create - initialize the control handler
455 * Initialize the capture video node control handler and fill it
456 * with the FIMC controls. Inherit any sensor's controls if the
457 * 'user_subdev_api' flag is false (default behaviour).
458 * This function need to be called with the graph mutex held.
460 int fimc_capture_ctrls_create(struct fimc_dev *fimc)
462 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
465 if (WARN_ON(vid_cap->ctx == NULL))
467 if (vid_cap->ctx->ctrls.ready)
470 ret = fimc_ctrls_create(vid_cap->ctx);
471 if (ret || vid_cap->user_subdev_api || !vid_cap->ctx->ctrls.ready)
474 return v4l2_ctrl_add_handler(&vid_cap->ctx->ctrls.handler,
475 fimc->pipeline.subdevs[IDX_SENSOR]->ctrl_handler);
478 static int fimc_capture_set_default_format(struct fimc_dev *fimc);
480 static int fimc_capture_open(struct file *file)
482 struct fimc_dev *fimc = video_drvdata(file);
483 int ret = v4l2_fh_open(file);
488 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
490 /* Return if the corresponding video mem2mem node is already opened. */
491 if (fimc_m2m_active(fimc))
494 set_bit(ST_CAPT_BUSY, &fimc->state);
495 pm_runtime_get_sync(&fimc->pdev->dev);
497 if (++fimc->vid_cap.refcnt == 1) {
498 ret = fimc_pipeline_initialize(&fimc->pipeline,
499 &fimc->vid_cap.vfd->entity, true);
501 dev_err(&fimc->pdev->dev,
502 "Video pipeline initialization failed\n");
503 clear_bit(ST_CAPT_BUSY, &fimc->state);
504 pm_runtime_put_sync(&fimc->pdev->dev);
505 fimc->vid_cap.refcnt--;
506 v4l2_fh_release(file);
509 ret = fimc_capture_ctrls_create(fimc);
511 if (!ret && !fimc->vid_cap.user_subdev_api)
512 ret = fimc_capture_set_default_format(fimc);
517 static int fimc_capture_close(struct file *file)
519 struct fimc_dev *fimc = video_drvdata(file);
521 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
523 if (--fimc->vid_cap.refcnt == 0) {
524 clear_bit(ST_CAPT_BUSY, &fimc->state);
525 fimc_stop_capture(fimc, false);
526 fimc_pipeline_shutdown(&fimc->pipeline);
527 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
530 pm_runtime_put(&fimc->pdev->dev);
532 if (fimc->vid_cap.refcnt == 0) {
533 vb2_queue_release(&fimc->vid_cap.vbq);
534 fimc_ctrls_delete(fimc->vid_cap.ctx);
536 return v4l2_fh_release(file);
539 static unsigned int fimc_capture_poll(struct file *file,
540 struct poll_table_struct *wait)
542 struct fimc_dev *fimc = video_drvdata(file);
544 return vb2_poll(&fimc->vid_cap.vbq, file, wait);
547 static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
549 struct fimc_dev *fimc = video_drvdata(file);
551 return vb2_mmap(&fimc->vid_cap.vbq, vma);
554 static const struct v4l2_file_operations fimc_capture_fops = {
555 .owner = THIS_MODULE,
556 .open = fimc_capture_open,
557 .release = fimc_capture_close,
558 .poll = fimc_capture_poll,
559 .unlocked_ioctl = video_ioctl2,
560 .mmap = fimc_capture_mmap,
564 * Format and crop negotiation helpers
567 static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
568 u32 *width, u32 *height,
569 u32 *code, u32 *fourcc, int pad)
571 bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
572 struct fimc_dev *fimc = ctx->fimc_dev;
573 struct fimc_variant *var = fimc->variant;
574 struct fimc_pix_limit *pl = var->pix_limit;
575 struct fimc_frame *dst = &ctx->d_frame;
576 u32 depth, min_w, max_w, min_h, align_h = 3;
577 u32 mask = FMT_FLAGS_CAM;
578 struct fimc_fmt *ffmt;
580 /* Color conversion from/to JPEG is not supported */
581 if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
582 fimc_fmt_is_jpeg(ctx->s_frame.fmt->color))
583 *code = V4L2_MBUS_FMT_JPEG_1X8;
585 if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad != FIMC_SD_PAD_SINK)
586 mask |= FMT_FLAGS_M2M;
588 ffmt = fimc_find_format(fourcc, code, mask, 0);
592 *code = ffmt->mbus_code;
594 *fourcc = ffmt->fourcc;
596 if (pad == FIMC_SD_PAD_SINK) {
597 max_w = fimc_fmt_is_jpeg(ffmt->color) ?
598 pl->scaler_dis_w : pl->scaler_en_w;
599 /* Apply the camera input interface pixel constraints */
600 v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
601 height, max_t(u32, *height, 32),
602 FIMC_CAMIF_MAX_HEIGHT,
603 fimc_fmt_is_jpeg(ffmt->color) ? 3 : 1,
607 /* Can't scale or crop in transparent (JPEG) transfer mode */
608 if (fimc_fmt_is_jpeg(ffmt->color)) {
609 *width = ctx->s_frame.f_width;
610 *height = ctx->s_frame.f_height;
613 /* Apply the scaler and the output DMA constraints */
614 max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
615 if (ctx->state & FIMC_COMPOSE) {
616 min_w = dst->offs_h + dst->width;
617 min_h = dst->offs_v + dst->height;
619 min_w = var->min_out_pixsize;
620 min_h = var->min_out_pixsize;
622 if (var->min_vsize_align == 1 && !rotation)
623 align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
625 depth = fimc_get_format_depth(ffmt);
626 v4l_bound_align_image(width, min_w, max_w,
627 ffs(var->min_out_pixsize) - 1,
628 height, min_h, FIMC_CAMIF_MAX_HEIGHT,
630 64/(ALIGN(depth, 8)));
632 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
633 pad, code ? *code : 0, *width, *height,
634 dst->f_width, dst->f_height);
639 static void fimc_capture_try_selection(struct fimc_ctx *ctx,
643 bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
644 struct fimc_dev *fimc = ctx->fimc_dev;
645 struct fimc_variant *var = fimc->variant;
646 struct fimc_pix_limit *pl = var->pix_limit;
647 struct fimc_frame *sink = &ctx->s_frame;
648 u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
649 u32 align_sz = 0, align_h = 4;
650 u32 max_sc_h, max_sc_v;
652 /* In JPEG transparent transfer mode cropping is not supported */
653 if (fimc_fmt_is_jpeg(ctx->d_frame.fmt->color)) {
654 r->width = sink->f_width;
655 r->height = sink->f_height;
656 r->left = r->top = 0;
659 if (target == V4L2_SEL_TGT_COMPOSE_ACTIVE) {
660 if (ctx->rotation != 90 && ctx->rotation != 270)
662 max_sc_h = min(SCALER_MAX_HRATIO, 1 << (ffs(sink->width) - 3));
663 max_sc_v = min(SCALER_MAX_VRATIO, 1 << (ffs(sink->height) - 1));
664 min_sz = var->min_out_pixsize;
666 u32 depth = fimc_get_format_depth(sink->fmt);
667 align_sz = 64/ALIGN(depth, 8);
668 min_sz = var->min_inp_pixsize;
669 min_w = min_h = min_sz;
670 max_sc_h = max_sc_v = 1;
673 * For the compose rectangle the following constraints must be met:
674 * - it must fit in the sink pad format rectangle (f_width/f_height);
675 * - maximum downscaling ratio is 64;
676 * - maximum crop size depends if the rotator is used or not;
677 * - the sink pad format width/height must be 4 multiple of the
678 * prescaler ratios determined by sink pad size and source pad crop,
679 * the prescaler ratio is returned by fimc_get_scaler_factor().
682 rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
683 rotate ? sink->f_height : sink->f_width);
684 max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
686 if (target == V4L2_SEL_TGT_COMPOSE_ACTIVE) {
687 min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
688 min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
690 swap(max_sc_h, max_sc_v);
694 v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
695 &r->height, min_h, max_h, align_h,
697 /* Adjust left/top if crop/compose rectangle is out of bounds */
698 r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
699 r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height);
700 r->left = round_down(r->left, var->hor_offs_align);
702 dbg("target %#x: (%d,%d)/%dx%d, sink fmt: %dx%d",
703 target, r->left, r->top, r->width, r->height,
704 sink->f_width, sink->f_height);
708 * The video node ioctl operations
710 static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
711 struct v4l2_capability *cap)
713 struct fimc_dev *fimc = video_drvdata(file);
715 strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
716 strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
717 cap->bus_info[0] = 0;
718 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE_MPLANE;
723 static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv,
724 struct v4l2_fmtdesc *f)
726 struct fimc_fmt *fmt;
728 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
732 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
733 f->pixelformat = fmt->fourcc;
734 if (fmt->fourcc == V4L2_MBUS_FMT_JPEG_1X8)
735 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
740 * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
742 * @ctx: FIMC capture context
743 * @tfmt: media bus format to try/set on subdevs
744 * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
745 * @set: true to set format on subdevs, false to try only
747 static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
748 struct v4l2_mbus_framefmt *tfmt,
749 struct fimc_fmt **fmt_id,
752 struct fimc_dev *fimc = ctx->fimc_dev;
753 struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
754 struct v4l2_subdev *csis = fimc->pipeline.subdevs[IDX_CSIS];
755 struct v4l2_subdev_format sfmt;
756 struct v4l2_mbus_framefmt *mf = &sfmt.format;
757 struct fimc_fmt *ffmt = NULL;
760 if (WARN_ON(!sd || !tfmt))
763 memset(&sfmt, 0, sizeof(sfmt));
766 sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
768 ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
772 * Notify user-space if common pixel code for
773 * host and sensor does not exist.
777 mf->code = tfmt->code = ffmt->mbus_code;
779 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
782 if (mf->code != tfmt->code) {
786 if (mf->width != tfmt->width || mf->height != tfmt->height) {
787 u32 fcc = ffmt->fourcc;
788 tfmt->width = mf->width;
789 tfmt->height = mf->height;
790 ffmt = fimc_capture_try_format(ctx,
791 &tfmt->width, &tfmt->height,
792 NULL, &fcc, FIMC_SD_PAD_SOURCE);
793 if (ffmt && ffmt->mbus_code)
794 mf->code = ffmt->mbus_code;
795 if (mf->width != tfmt->width ||
796 mf->height != tfmt->height)
798 tfmt->code = mf->code;
801 ret = v4l2_subdev_call(csis, pad, set_fmt, NULL, &sfmt);
803 if (mf->code == tfmt->code &&
804 mf->width == tfmt->width && mf->height == tfmt->height)
812 dbg("code: 0x%x, %dx%d, %p", mf->code, mf->width, mf->height, ffmt);
816 static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
817 struct v4l2_format *f)
819 struct fimc_dev *fimc = video_drvdata(file);
820 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
822 return fimc_fill_format(&ctx->d_frame, f);
825 static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
826 struct v4l2_format *f)
828 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
829 struct fimc_dev *fimc = video_drvdata(file);
830 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
831 struct v4l2_mbus_framefmt mf;
832 struct fimc_fmt *ffmt = NULL;
834 if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
835 fimc_capture_try_format(ctx, &pix->width, &pix->height,
836 NULL, &pix->pixelformat,
838 ctx->s_frame.f_width = pix->width;
839 ctx->s_frame.f_height = pix->height;
841 ffmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
842 NULL, &pix->pixelformat,
847 if (!fimc->vid_cap.user_subdev_api) {
848 mf.width = pix->width;
849 mf.height = pix->height;
850 mf.code = ffmt->mbus_code;
851 fimc_md_graph_lock(fimc);
852 fimc_pipeline_try_format(ctx, &mf, &ffmt, false);
853 fimc_md_graph_unlock(fimc);
855 pix->width = mf.width;
856 pix->height = mf.height;
858 pix->pixelformat = ffmt->fourcc;
861 fimc_adjust_mplane_format(ffmt, pix->width, pix->height, pix);
865 static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx, bool jpeg)
867 ctx->scaler.enabled = !jpeg;
868 fimc_ctrls_activate(ctx, !jpeg);
871 set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
873 clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
876 static int fimc_capture_set_format(struct fimc_dev *fimc, struct v4l2_format *f)
878 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
879 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
880 struct v4l2_mbus_framefmt *mf = &fimc->vid_cap.mf;
881 struct fimc_frame *ff = &ctx->d_frame;
882 struct fimc_fmt *s_fmt = NULL;
885 if (vb2_is_busy(&fimc->vid_cap.vbq))
888 /* Pre-configure format at camera interface input, for JPEG only */
889 if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
890 fimc_capture_try_format(ctx, &pix->width, &pix->height,
891 NULL, &pix->pixelformat,
893 ctx->s_frame.f_width = pix->width;
894 ctx->s_frame.f_height = pix->height;
896 /* Try the format at the scaler and the DMA output */
897 ff->fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
898 NULL, &pix->pixelformat,
903 /* Update RGB Alpha control state and value range */
904 fimc_alpha_ctrl_update(ctx);
906 /* Try to match format at the host and the sensor */
907 if (!fimc->vid_cap.user_subdev_api) {
908 mf->code = ff->fmt->mbus_code;
909 mf->width = pix->width;
910 mf->height = pix->height;
912 fimc_md_graph_lock(fimc);
913 ret = fimc_pipeline_try_format(ctx, mf, &s_fmt, true);
914 fimc_md_graph_unlock(fimc);
917 pix->width = mf->width;
918 pix->height = mf->height;
921 fimc_adjust_mplane_format(ff->fmt, pix->width, pix->height, pix);
922 for (i = 0; i < ff->fmt->colplanes; i++)
923 ff->payload[i] = pix->plane_fmt[i].sizeimage;
925 set_frame_bounds(ff, pix->width, pix->height);
926 /* Reset the composition rectangle if not yet configured */
927 if (!(ctx->state & FIMC_COMPOSE))
928 set_frame_crop(ff, 0, 0, pix->width, pix->height);
930 fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ff->fmt->color));
932 /* Reset cropping and set format at the camera interface input */
933 if (!fimc->vid_cap.user_subdev_api) {
934 ctx->s_frame.fmt = s_fmt;
935 set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
936 set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
942 static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
943 struct v4l2_format *f)
945 struct fimc_dev *fimc = video_drvdata(file);
947 return fimc_capture_set_format(fimc, f);
950 static int fimc_cap_enum_input(struct file *file, void *priv,
951 struct v4l2_input *i)
953 struct fimc_dev *fimc = video_drvdata(file);
954 struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
959 i->type = V4L2_INPUT_TYPE_CAMERA;
961 strlcpy(i->name, sd->name, sizeof(i->name));
965 static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
967 return i == 0 ? i : -EINVAL;
970 static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
977 * fimc_pipeline_validate - check for formats inconsistencies
978 * between source and sink pad of each link
980 * Return 0 if all formats match or -EPIPE otherwise.
982 static int fimc_pipeline_validate(struct fimc_dev *fimc)
984 struct v4l2_subdev_format sink_fmt, src_fmt;
985 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
986 struct v4l2_subdev *sd;
987 struct media_pad *pad;
990 /* Start with the video capture node pad */
991 pad = media_entity_remote_source(&vid_cap->vd_pad);
994 /* FIMC.{N} subdevice */
995 sd = media_entity_to_v4l2_subdev(pad->entity);
998 /* Retrieve format at the sink pad */
999 pad = &sd->entity.pads[0];
1000 if (!(pad->flags & MEDIA_PAD_FL_SINK))
1002 /* Don't call FIMC subdev operation to avoid nested locking */
1003 if (sd == &fimc->vid_cap.subdev) {
1004 struct fimc_frame *ff = &vid_cap->ctx->s_frame;
1005 sink_fmt.format.width = ff->f_width;
1006 sink_fmt.format.height = ff->f_height;
1007 sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
1009 sink_fmt.pad = pad->index;
1010 sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1011 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
1012 if (ret < 0 && ret != -ENOIOCTLCMD)
1015 /* Retrieve format at the source pad */
1016 pad = media_entity_remote_source(pad);
1018 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1021 sd = media_entity_to_v4l2_subdev(pad->entity);
1022 src_fmt.pad = pad->index;
1023 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1024 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
1025 if (ret < 0 && ret != -ENOIOCTLCMD)
1028 if (src_fmt.format.width != sink_fmt.format.width ||
1029 src_fmt.format.height != sink_fmt.format.height ||
1030 src_fmt.format.code != sink_fmt.format.code)
1036 static int fimc_cap_streamon(struct file *file, void *priv,
1037 enum v4l2_buf_type type)
1039 struct fimc_dev *fimc = video_drvdata(file);
1040 struct fimc_pipeline *p = &fimc->pipeline;
1043 if (fimc_capture_active(fimc))
1046 media_entity_pipeline_start(&p->subdevs[IDX_SENSOR]->entity,
1049 if (fimc->vid_cap.user_subdev_api) {
1050 ret = fimc_pipeline_validate(fimc);
1054 return vb2_streamon(&fimc->vid_cap.vbq, type);
1057 static int fimc_cap_streamoff(struct file *file, void *priv,
1058 enum v4l2_buf_type type)
1060 struct fimc_dev *fimc = video_drvdata(file);
1061 struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
1064 ret = vb2_streamoff(&fimc->vid_cap.vbq, type);
1066 media_entity_pipeline_stop(&sd->entity);
1070 static int fimc_cap_reqbufs(struct file *file, void *priv,
1071 struct v4l2_requestbuffers *reqbufs)
1073 struct fimc_dev *fimc = video_drvdata(file);
1074 int ret = vb2_reqbufs(&fimc->vid_cap.vbq, reqbufs);
1077 fimc->vid_cap.reqbufs_count = reqbufs->count;
1081 static int fimc_cap_querybuf(struct file *file, void *priv,
1082 struct v4l2_buffer *buf)
1084 struct fimc_dev *fimc = video_drvdata(file);
1086 return vb2_querybuf(&fimc->vid_cap.vbq, buf);
1089 static int fimc_cap_qbuf(struct file *file, void *priv,
1090 struct v4l2_buffer *buf)
1092 struct fimc_dev *fimc = video_drvdata(file);
1094 return vb2_qbuf(&fimc->vid_cap.vbq, buf);
1097 static int fimc_cap_dqbuf(struct file *file, void *priv,
1098 struct v4l2_buffer *buf)
1100 struct fimc_dev *fimc = video_drvdata(file);
1102 return vb2_dqbuf(&fimc->vid_cap.vbq, buf, file->f_flags & O_NONBLOCK);
1105 static int fimc_cap_create_bufs(struct file *file, void *priv,
1106 struct v4l2_create_buffers *create)
1108 struct fimc_dev *fimc = video_drvdata(file);
1110 return vb2_create_bufs(&fimc->vid_cap.vbq, create);
1113 static int fimc_cap_prepare_buf(struct file *file, void *priv,
1114 struct v4l2_buffer *b)
1116 struct fimc_dev *fimc = video_drvdata(file);
1118 return vb2_prepare_buf(&fimc->vid_cap.vbq, b);
1121 static int fimc_cap_g_selection(struct file *file, void *fh,
1122 struct v4l2_selection *s)
1124 struct fimc_dev *fimc = video_drvdata(file);
1125 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1126 struct fimc_frame *f = &ctx->s_frame;
1128 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1131 switch (s->target) {
1132 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1133 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1135 case V4L2_SEL_TGT_CROP_BOUNDS:
1136 case V4L2_SEL_TGT_CROP_DEFAULT:
1139 s->r.width = f->o_width;
1140 s->r.height = f->o_height;
1143 case V4L2_SEL_TGT_COMPOSE_ACTIVE:
1145 case V4L2_SEL_TGT_CROP_ACTIVE:
1146 s->r.left = f->offs_h;
1147 s->r.top = f->offs_v;
1148 s->r.width = f->width;
1149 s->r.height = f->height;
1156 /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
1157 int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
1159 if (a->left < b->left || a->top < b->top)
1161 if (a->left + a->width > b->left + b->width)
1163 if (a->top + a->height > b->top + b->height)
1169 static int fimc_cap_s_selection(struct file *file, void *fh,
1170 struct v4l2_selection *s)
1172 struct fimc_dev *fimc = video_drvdata(file);
1173 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1174 struct v4l2_rect rect = s->r;
1175 struct fimc_frame *f;
1176 unsigned long flags;
1178 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1181 if (s->target == V4L2_SEL_TGT_COMPOSE_ACTIVE)
1183 else if (s->target == V4L2_SEL_TGT_CROP_ACTIVE)
1188 fimc_capture_try_selection(ctx, &rect, s->target);
1190 if (s->flags & V4L2_SEL_FLAG_LE &&
1191 !enclosed_rectangle(&rect, &s->r))
1194 if (s->flags & V4L2_SEL_FLAG_GE &&
1195 !enclosed_rectangle(&s->r, &rect))
1199 spin_lock_irqsave(&fimc->slock, flags);
1200 set_frame_crop(f, s->r.left, s->r.top, s->r.width,
1202 spin_unlock_irqrestore(&fimc->slock, flags);
1204 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1208 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
1209 .vidioc_querycap = fimc_vidioc_querycap_capture,
1211 .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane,
1212 .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane,
1213 .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
1214 .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane,
1216 .vidioc_reqbufs = fimc_cap_reqbufs,
1217 .vidioc_querybuf = fimc_cap_querybuf,
1219 .vidioc_qbuf = fimc_cap_qbuf,
1220 .vidioc_dqbuf = fimc_cap_dqbuf,
1222 .vidioc_prepare_buf = fimc_cap_prepare_buf,
1223 .vidioc_create_bufs = fimc_cap_create_bufs,
1225 .vidioc_streamon = fimc_cap_streamon,
1226 .vidioc_streamoff = fimc_cap_streamoff,
1228 .vidioc_g_selection = fimc_cap_g_selection,
1229 .vidioc_s_selection = fimc_cap_s_selection,
1231 .vidioc_enum_input = fimc_cap_enum_input,
1232 .vidioc_s_input = fimc_cap_s_input,
1233 .vidioc_g_input = fimc_cap_g_input,
1236 /* Capture subdev media entity operations */
1237 static int fimc_link_setup(struct media_entity *entity,
1238 const struct media_pad *local,
1239 const struct media_pad *remote, u32 flags)
1241 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1242 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1244 if (media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1247 if (WARN_ON(fimc == NULL))
1250 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1251 local->entity->name, remote->entity->name, flags,
1252 fimc->vid_cap.input);
1254 if (flags & MEDIA_LNK_FL_ENABLED) {
1255 if (fimc->vid_cap.input != 0)
1257 fimc->vid_cap.input = sd->grp_id;
1261 fimc->vid_cap.input = 0;
1265 static const struct media_entity_operations fimc_sd_media_ops = {
1266 .link_setup = fimc_link_setup,
1270 * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1271 * @sd: pointer to a subdev generating the notification
1272 * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1273 * @arg: pointer to an u32 type integer that stores the frame payload value
1275 * The End Of Frame notification sent by sensor subdev in its still capture
1276 * mode. If there is only a single VSYNC generated by the sensor at the
1277 * beginning of a frame transmission, FIMC does not issue the LastIrq
1278 * (end of frame) interrupt. And this notification is used to complete the
1279 * frame capture and returning a buffer to user-space. Subdev drivers should
1280 * call this notification from their last 'End of frame capture' interrupt.
1282 void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
1285 struct fimc_sensor_info *sensor;
1286 struct fimc_vid_buffer *buf;
1287 struct fimc_md *fmd;
1288 struct fimc_dev *fimc;
1289 unsigned long flags;
1294 sensor = v4l2_get_subdev_hostdata(sd);
1295 fmd = entity_to_fimc_mdev(&sd->entity);
1297 spin_lock_irqsave(&fmd->slock, flags);
1298 fimc = sensor ? sensor->host : NULL;
1300 if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
1301 test_bit(ST_CAPT_PEND, &fimc->state)) {
1302 unsigned long irq_flags;
1303 spin_lock_irqsave(&fimc->slock, irq_flags);
1304 if (!list_empty(&fimc->vid_cap.active_buf_q)) {
1305 buf = list_entry(fimc->vid_cap.active_buf_q.next,
1306 struct fimc_vid_buffer, list);
1307 vb2_set_plane_payload(&buf->vb, 0, *((u32 *)arg));
1309 fimc_capture_irq_handler(fimc, 1);
1310 fimc_deactivate_capture(fimc);
1311 spin_unlock_irqrestore(&fimc->slock, irq_flags);
1313 spin_unlock_irqrestore(&fmd->slock, flags);
1316 static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1317 struct v4l2_subdev_fh *fh,
1318 struct v4l2_subdev_mbus_code_enum *code)
1320 struct fimc_fmt *fmt;
1322 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1325 code->code = fmt->mbus_code;
1329 static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
1330 struct v4l2_subdev_fh *fh,
1331 struct v4l2_subdev_format *fmt)
1333 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1334 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1335 struct v4l2_mbus_framefmt *mf;
1336 struct fimc_frame *ff;
1338 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1339 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1344 mf->colorspace = V4L2_COLORSPACE_JPEG;
1345 ff = fmt->pad == FIMC_SD_PAD_SINK ? &ctx->s_frame : &ctx->d_frame;
1347 mutex_lock(&fimc->lock);
1348 /* The pixel code is same on both input and output pad */
1349 if (!WARN_ON(ctx->s_frame.fmt == NULL))
1350 mf->code = ctx->s_frame.fmt->mbus_code;
1351 mf->width = ff->f_width;
1352 mf->height = ff->f_height;
1353 mutex_unlock(&fimc->lock);
1358 static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
1359 struct v4l2_subdev_fh *fh,
1360 struct v4l2_subdev_format *fmt)
1362 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1363 struct v4l2_mbus_framefmt *mf = &fmt->format;
1364 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1365 struct fimc_frame *ff;
1366 struct fimc_fmt *ffmt;
1368 dbg("pad%d: code: 0x%x, %dx%d",
1369 fmt->pad, mf->code, mf->width, mf->height);
1371 if (fmt->pad == FIMC_SD_PAD_SOURCE &&
1372 vb2_is_busy(&fimc->vid_cap.vbq))
1375 mutex_lock(&fimc->lock);
1376 ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1377 &mf->code, NULL, fmt->pad);
1378 mutex_unlock(&fimc->lock);
1379 mf->colorspace = V4L2_COLORSPACE_JPEG;
1381 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1382 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1386 /* Update RGB Alpha control state and value range */
1387 fimc_alpha_ctrl_update(ctx);
1389 fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ffmt->color));
1391 ff = fmt->pad == FIMC_SD_PAD_SINK ?
1392 &ctx->s_frame : &ctx->d_frame;
1394 mutex_lock(&fimc->lock);
1395 set_frame_bounds(ff, mf->width, mf->height);
1396 fimc->vid_cap.mf = *mf;
1399 /* Reset the crop rectangle if required. */
1400 if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_COMPOSE)))
1401 set_frame_crop(ff, 0, 0, mf->width, mf->height);
1403 if (fmt->pad == FIMC_SD_PAD_SINK)
1404 ctx->state &= ~FIMC_COMPOSE;
1405 mutex_unlock(&fimc->lock);
1409 static int fimc_subdev_get_selection(struct v4l2_subdev *sd,
1410 struct v4l2_subdev_fh *fh,
1411 struct v4l2_subdev_selection *sel)
1413 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1414 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1415 struct fimc_frame *f = &ctx->s_frame;
1416 struct v4l2_rect *r = &sel->r;
1417 struct v4l2_rect *try_sel;
1419 if (sel->pad != FIMC_SD_PAD_SINK)
1422 mutex_lock(&fimc->lock);
1424 switch (sel->target) {
1425 case V4L2_SUBDEV_SEL_TGT_COMPOSE_BOUNDS:
1427 case V4L2_SUBDEV_SEL_TGT_CROP_BOUNDS:
1428 r->width = f->o_width;
1429 r->height = f->o_height;
1432 mutex_unlock(&fimc->lock);
1435 case V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL:
1436 try_sel = v4l2_subdev_get_try_crop(fh, sel->pad);
1438 case V4L2_SUBDEV_SEL_TGT_COMPOSE_ACTUAL:
1439 try_sel = v4l2_subdev_get_try_compose(fh, sel->pad);
1443 mutex_unlock(&fimc->lock);
1447 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1450 r->left = f->offs_h;
1452 r->width = f->width;
1453 r->height = f->height;
1456 dbg("target %#x: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1457 sel->pad, r->left, r->top, r->width, r->height,
1458 f->f_width, f->f_height);
1460 mutex_unlock(&fimc->lock);
1464 static int fimc_subdev_set_selection(struct v4l2_subdev *sd,
1465 struct v4l2_subdev_fh *fh,
1466 struct v4l2_subdev_selection *sel)
1468 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1469 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1470 struct fimc_frame *f = &ctx->s_frame;
1471 struct v4l2_rect *r = &sel->r;
1472 struct v4l2_rect *try_sel;
1473 unsigned long flags;
1475 if (sel->pad != FIMC_SD_PAD_SINK)
1478 mutex_lock(&fimc->lock);
1479 fimc_capture_try_selection(ctx, r, V4L2_SEL_TGT_CROP_ACTIVE);
1481 switch (sel->target) {
1482 case V4L2_SUBDEV_SEL_TGT_COMPOSE_BOUNDS:
1484 case V4L2_SUBDEV_SEL_TGT_CROP_BOUNDS:
1485 r->width = f->o_width;
1486 r->height = f->o_height;
1489 mutex_unlock(&fimc->lock);
1492 case V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL:
1493 try_sel = v4l2_subdev_get_try_crop(fh, sel->pad);
1495 case V4L2_SUBDEV_SEL_TGT_COMPOSE_ACTUAL:
1496 try_sel = v4l2_subdev_get_try_compose(fh, sel->pad);
1500 mutex_unlock(&fimc->lock);
1504 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1507 spin_lock_irqsave(&fimc->slock, flags);
1508 set_frame_crop(f, r->left, r->top, r->width, r->height);
1509 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1510 spin_unlock_irqrestore(&fimc->slock, flags);
1511 if (sel->target == V4L2_SUBDEV_SEL_TGT_COMPOSE_ACTUAL)
1512 ctx->state |= FIMC_COMPOSE;
1515 dbg("target %#x: (%d,%d)/%dx%d", sel->target, r->left, r->top,
1516 r->width, r->height);
1518 mutex_unlock(&fimc->lock);
1522 static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
1523 .enum_mbus_code = fimc_subdev_enum_mbus_code,
1524 .get_selection = fimc_subdev_get_selection,
1525 .set_selection = fimc_subdev_set_selection,
1526 .get_fmt = fimc_subdev_get_fmt,
1527 .set_fmt = fimc_subdev_set_fmt,
1530 static struct v4l2_subdev_ops fimc_subdev_ops = {
1531 .pad = &fimc_subdev_pad_ops,
1534 /* Set default format at the sensor and host interface */
1535 static int fimc_capture_set_default_format(struct fimc_dev *fimc)
1537 struct v4l2_format fmt = {
1538 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1542 .pixelformat = V4L2_PIX_FMT_YUYV,
1543 .field = V4L2_FIELD_NONE,
1544 .colorspace = V4L2_COLORSPACE_JPEG,
1548 return fimc_capture_set_format(fimc, &fmt);
1551 /* fimc->lock must be already initialized */
1552 static int fimc_register_capture_device(struct fimc_dev *fimc,
1553 struct v4l2_device *v4l2_dev)
1555 struct video_device *vfd;
1556 struct fimc_vid_cap *vid_cap;
1557 struct fimc_ctx *ctx;
1558 struct vb2_queue *q;
1561 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1565 ctx->fimc_dev = fimc;
1566 ctx->in_path = FIMC_IO_CAMERA;
1567 ctx->out_path = FIMC_IO_DMA;
1568 ctx->state = FIMC_CTX_CAP;
1569 ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1570 ctx->d_frame.fmt = ctx->s_frame.fmt;
1572 vfd = video_device_alloc();
1574 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
1578 snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.capture", fimc->id);
1580 vfd->fops = &fimc_capture_fops;
1581 vfd->ioctl_ops = &fimc_capture_ioctl_ops;
1582 vfd->v4l2_dev = v4l2_dev;
1584 vfd->release = video_device_release;
1585 vfd->lock = &fimc->lock;
1586 /* Locking in file operations other than ioctl should be done
1587 by the driver, not the V4L2 core.
1588 This driver needs auditing so that this flag can be removed. */
1589 set_bit(V4L2_FL_LOCK_ALL_FOPS, &vfd->flags);
1590 video_set_drvdata(vfd, fimc);
1592 vid_cap = &fimc->vid_cap;
1594 vid_cap->active_buf_cnt = 0;
1595 vid_cap->reqbufs_count = 0;
1596 vid_cap->refcnt = 0;
1598 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1599 INIT_LIST_HEAD(&vid_cap->active_buf_q);
1602 q = &fimc->vid_cap.vbq;
1603 memset(q, 0, sizeof(*q));
1604 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1605 q->io_modes = VB2_MMAP | VB2_USERPTR;
1606 q->drv_priv = fimc->vid_cap.ctx;
1607 q->ops = &fimc_capture_qops;
1608 q->mem_ops = &vb2_dma_contig_memops;
1609 q->buf_struct_size = sizeof(struct fimc_vid_buffer);
1613 vid_cap->vd_pad.flags = MEDIA_PAD_FL_SINK;
1614 ret = media_entity_init(&vfd->entity, 1, &vid_cap->vd_pad, 0);
1618 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
1622 v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n",
1623 vfd->name, video_device_node_name(vfd));
1625 vfd->ctrl_handler = &ctx->ctrls.handler;
1629 media_entity_cleanup(&vfd->entity);
1631 video_device_release(vfd);
1637 static int fimc_capture_subdev_registered(struct v4l2_subdev *sd)
1639 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1642 ret = fimc_register_m2m_device(fimc, sd->v4l2_dev);
1646 ret = fimc_register_capture_device(fimc, sd->v4l2_dev);
1648 fimc_unregister_m2m_device(fimc);
1653 static void fimc_capture_subdev_unregistered(struct v4l2_subdev *sd)
1655 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1660 fimc_unregister_m2m_device(fimc);
1662 if (fimc->vid_cap.vfd) {
1663 media_entity_cleanup(&fimc->vid_cap.vfd->entity);
1664 video_unregister_device(fimc->vid_cap.vfd);
1665 fimc->vid_cap.vfd = NULL;
1668 kfree(fimc->vid_cap.ctx);
1669 fimc->vid_cap.ctx = NULL;
1672 static const struct v4l2_subdev_internal_ops fimc_capture_sd_internal_ops = {
1673 .registered = fimc_capture_subdev_registered,
1674 .unregistered = fimc_capture_subdev_unregistered,
1677 int fimc_initialize_capture_subdev(struct fimc_dev *fimc)
1679 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1682 v4l2_subdev_init(sd, &fimc_subdev_ops);
1683 sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
1684 snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->pdev->id);
1686 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1687 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1688 ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
1689 fimc->vid_cap.sd_pads, 0);
1693 sd->entity.ops = &fimc_sd_media_ops;
1694 sd->internal_ops = &fimc_capture_sd_internal_ops;
1695 v4l2_set_subdevdata(sd, fimc);
1699 void fimc_unregister_capture_subdev(struct fimc_dev *fimc)
1701 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1703 v4l2_device_unregister_subdev(sd);
1704 media_entity_cleanup(&sd->entity);
1705 v4l2_set_subdevdata(sd, NULL);