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_call(fimc, set_stream,
127 static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend)
131 if (!fimc_capture_active(fimc))
134 spin_lock_irqsave(&fimc->slock, flags);
135 set_bit(ST_CAPT_SHUT, &fimc->state);
136 fimc_deactivate_capture(fimc);
137 spin_unlock_irqrestore(&fimc->slock, flags);
139 wait_event_timeout(fimc->irq_queue,
140 !test_bit(ST_CAPT_SHUT, &fimc->state),
141 (2*HZ/10)); /* 200 ms */
143 return fimc_capture_state_cleanup(fimc, suspend);
147 * fimc_capture_config_update - apply the camera interface configuration
149 * To be called from within the interrupt handler with fimc.slock
150 * spinlock held. It updates the camera pixel crop, rotation and
153 static int fimc_capture_config_update(struct fimc_ctx *ctx)
155 struct fimc_dev *fimc = ctx->fimc_dev;
158 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
160 ret = fimc_set_scaler_info(ctx);
164 fimc_hw_set_prescaler(ctx);
165 fimc_hw_set_mainscaler(ctx);
166 fimc_hw_set_target_format(ctx);
167 fimc_hw_set_rotation(ctx);
168 fimc_hw_set_effect(ctx);
169 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
170 fimc_hw_set_out_dma(ctx);
171 if (fimc->variant->has_alpha)
172 fimc_hw_set_rgb_alpha(ctx);
174 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
178 void fimc_capture_irq_handler(struct fimc_dev *fimc, int deq_buf)
180 struct v4l2_subdev *csis = fimc->pipeline.subdevs[IDX_CSIS];
181 struct fimc_vid_cap *cap = &fimc->vid_cap;
182 struct fimc_frame *f = &cap->ctx->d_frame;
183 struct fimc_vid_buffer *v_buf;
187 if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) {
188 wake_up(&fimc->irq_queue);
192 if (!list_empty(&cap->active_buf_q) &&
193 test_bit(ST_CAPT_RUN, &fimc->state) && deq_buf) {
194 ktime_get_real_ts(&ts);
196 v_buf = fimc_active_queue_pop(cap);
198 tv = &v_buf->vb.v4l2_buf.timestamp;
199 tv->tv_sec = ts.tv_sec;
200 tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
201 v_buf->vb.v4l2_buf.sequence = cap->frame_count++;
203 vb2_buffer_done(&v_buf->vb, VB2_BUF_STATE_DONE);
206 if (!list_empty(&cap->pending_buf_q)) {
208 v_buf = fimc_pending_queue_pop(cap);
209 fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index);
210 v_buf->index = cap->buf_index;
212 /* Move the buffer to the capture active queue */
213 fimc_active_queue_add(cap, v_buf);
215 dbg("next frame: %d, done frame: %d",
216 fimc_hw_get_frame_index(fimc), v_buf->index);
218 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
222 * Set up a buffer at MIPI-CSIS if current image format
223 * requires the frame embedded data capture.
225 if (f->fmt->mdataplanes && !list_empty(&cap->active_buf_q)) {
226 unsigned int plane = ffs(f->fmt->mdataplanes) - 1;
227 unsigned int size = f->payload[plane];
228 s32 index = fimc_hw_get_frame_index(fimc);
231 list_for_each_entry(v_buf, &cap->active_buf_q, list) {
232 if (v_buf->index != index)
234 vaddr = vb2_plane_vaddr(&v_buf->vb, plane);
235 v4l2_subdev_call(csis, video, s_rx_buffer,
241 if (cap->active_buf_cnt == 0) {
243 clear_bit(ST_CAPT_RUN, &fimc->state);
245 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
248 set_bit(ST_CAPT_RUN, &fimc->state);
251 if (test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
252 fimc_capture_config_update(cap->ctx);
254 if (cap->active_buf_cnt == 1) {
255 fimc_deactivate_capture(fimc);
256 clear_bit(ST_CAPT_STREAM, &fimc->state);
259 dbg("frame: %d, active_buf_cnt: %d",
260 fimc_hw_get_frame_index(fimc), cap->active_buf_cnt);
264 static int start_streaming(struct vb2_queue *q, unsigned int count)
266 struct fimc_ctx *ctx = q->drv_priv;
267 struct fimc_dev *fimc = ctx->fimc_dev;
268 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
272 vid_cap->frame_count = 0;
274 ret = fimc_capture_hw_init(fimc);
276 fimc_capture_state_cleanup(fimc, false);
280 set_bit(ST_CAPT_PEND, &fimc->state);
282 min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
284 if (vid_cap->active_buf_cnt >= min_bufs &&
285 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
286 fimc_activate_capture(ctx);
288 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
289 fimc_pipeline_call(fimc, set_stream,
296 static int stop_streaming(struct vb2_queue *q)
298 struct fimc_ctx *ctx = q->drv_priv;
299 struct fimc_dev *fimc = ctx->fimc_dev;
301 if (!fimc_capture_active(fimc))
304 return fimc_stop_capture(fimc, false);
307 int fimc_capture_suspend(struct fimc_dev *fimc)
309 bool suspend = fimc_capture_busy(fimc);
311 int ret = fimc_stop_capture(fimc, suspend);
314 return fimc_pipeline_call(fimc, close, &fimc->pipeline);
317 static void buffer_queue(struct vb2_buffer *vb);
319 int fimc_capture_resume(struct fimc_dev *fimc)
321 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
322 struct fimc_vid_buffer *buf;
325 if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state))
328 INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
329 vid_cap->buf_index = 0;
330 fimc_pipeline_call(fimc, open, &fimc->pipeline,
331 &vid_cap->vfd.entity, false);
332 fimc_capture_hw_init(fimc);
334 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
336 for (i = 0; i < vid_cap->reqbufs_count; i++) {
337 if (list_empty(&vid_cap->pending_buf_q))
339 buf = fimc_pending_queue_pop(vid_cap);
340 buffer_queue(&buf->vb);
346 static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
347 unsigned int *num_buffers, unsigned int *num_planes,
348 unsigned int sizes[], void *allocators[])
350 const struct v4l2_pix_format_mplane *pixm = NULL;
351 struct fimc_ctx *ctx = vq->drv_priv;
352 struct fimc_frame *frame = &ctx->d_frame;
353 struct fimc_fmt *fmt = frame->fmt;
358 pixm = &pfmt->fmt.pix_mp;
359 fmt = fimc_find_format(&pixm->pixelformat, NULL,
360 FMT_FLAGS_CAM | FMT_FLAGS_M2M, -1);
361 wh = pixm->width * pixm->height;
363 wh = frame->f_width * frame->f_height;
369 *num_planes = fmt->memplanes;
371 for (i = 0; i < fmt->memplanes; i++) {
372 unsigned int size = (wh * fmt->depth[i]) / 8;
374 sizes[i] = max(size, pixm->plane_fmt[i].sizeimage);
375 else if (fimc_fmt_is_user_defined(fmt->color))
376 sizes[i] = frame->payload[i];
378 sizes[i] = max_t(u32, size, frame->payload[i]);
380 allocators[i] = ctx->fimc_dev->alloc_ctx;
386 static int buffer_prepare(struct vb2_buffer *vb)
388 struct vb2_queue *vq = vb->vb2_queue;
389 struct fimc_ctx *ctx = vq->drv_priv;
392 if (ctx->d_frame.fmt == NULL)
395 for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
396 unsigned long size = ctx->d_frame.payload[i];
398 if (vb2_plane_size(vb, i) < size) {
399 v4l2_err(&ctx->fimc_dev->vid_cap.vfd,
400 "User buffer too small (%ld < %ld)\n",
401 vb2_plane_size(vb, i), size);
404 vb2_set_plane_payload(vb, i, size);
410 static void buffer_queue(struct vb2_buffer *vb)
412 struct fimc_vid_buffer *buf
413 = container_of(vb, struct fimc_vid_buffer, vb);
414 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
415 struct fimc_dev *fimc = ctx->fimc_dev;
416 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
420 spin_lock_irqsave(&fimc->slock, flags);
421 fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);
423 if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
424 !test_bit(ST_CAPT_STREAM, &fimc->state) &&
425 vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
426 /* Setup the buffer directly for processing. */
427 int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
430 fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
431 buf->index = vid_cap->buf_index;
432 fimc_active_queue_add(vid_cap, buf);
434 if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
435 vid_cap->buf_index = 0;
437 fimc_pending_queue_add(vid_cap, buf);
440 min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
443 if (vb2_is_streaming(&vid_cap->vbq) &&
444 vid_cap->active_buf_cnt >= min_bufs &&
445 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
446 fimc_activate_capture(ctx);
447 spin_unlock_irqrestore(&fimc->slock, flags);
449 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
450 fimc_pipeline_call(fimc, set_stream,
454 spin_unlock_irqrestore(&fimc->slock, flags);
457 static void fimc_lock(struct vb2_queue *vq)
459 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
460 mutex_lock(&ctx->fimc_dev->lock);
463 static void fimc_unlock(struct vb2_queue *vq)
465 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
466 mutex_unlock(&ctx->fimc_dev->lock);
469 static struct vb2_ops fimc_capture_qops = {
470 .queue_setup = queue_setup,
471 .buf_prepare = buffer_prepare,
472 .buf_queue = buffer_queue,
473 .wait_prepare = fimc_unlock,
474 .wait_finish = fimc_lock,
475 .start_streaming = start_streaming,
476 .stop_streaming = stop_streaming,
480 * fimc_capture_ctrls_create - initialize the control handler
481 * Initialize the capture video node control handler and fill it
482 * with the FIMC controls. Inherit any sensor's controls if the
483 * 'user_subdev_api' flag is false (default behaviour).
484 * This function need to be called with the graph mutex held.
486 int fimc_capture_ctrls_create(struct fimc_dev *fimc)
488 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
491 if (WARN_ON(vid_cap->ctx == NULL))
493 if (vid_cap->ctx->ctrls.ready)
496 ret = fimc_ctrls_create(vid_cap->ctx);
497 if (ret || vid_cap->user_subdev_api || !vid_cap->ctx->ctrls.ready)
500 return v4l2_ctrl_add_handler(&vid_cap->ctx->ctrls.handler,
501 fimc->pipeline.subdevs[IDX_SENSOR]->ctrl_handler, NULL);
504 static int fimc_capture_set_default_format(struct fimc_dev *fimc);
506 static int fimc_capture_open(struct file *file)
508 struct fimc_dev *fimc = video_drvdata(file);
511 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
513 if (mutex_lock_interruptible(&fimc->lock))
516 if (fimc_m2m_active(fimc))
519 set_bit(ST_CAPT_BUSY, &fimc->state);
520 ret = pm_runtime_get_sync(&fimc->pdev->dev);
524 ret = v4l2_fh_open(file);
526 pm_runtime_put(&fimc->pdev->dev);
530 if (++fimc->vid_cap.refcnt == 1) {
531 ret = fimc_pipeline_call(fimc, open, &fimc->pipeline,
532 &fimc->vid_cap.vfd.entity, true);
534 if (!ret && !fimc->vid_cap.user_subdev_api)
535 ret = fimc_capture_set_default_format(fimc);
538 ret = fimc_capture_ctrls_create(fimc);
541 clear_bit(ST_CAPT_BUSY, &fimc->state);
542 pm_runtime_put_sync(&fimc->pdev->dev);
543 fimc->vid_cap.refcnt--;
544 v4l2_fh_release(file);
548 mutex_unlock(&fimc->lock);
552 static int fimc_capture_close(struct file *file)
554 struct fimc_dev *fimc = video_drvdata(file);
557 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
559 mutex_lock(&fimc->lock);
561 if (--fimc->vid_cap.refcnt == 0) {
562 clear_bit(ST_CAPT_BUSY, &fimc->state);
563 fimc_stop_capture(fimc, false);
564 fimc_pipeline_call(fimc, close, &fimc->pipeline);
565 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
568 pm_runtime_put(&fimc->pdev->dev);
570 if (fimc->vid_cap.refcnt == 0) {
571 vb2_queue_release(&fimc->vid_cap.vbq);
572 fimc_ctrls_delete(fimc->vid_cap.ctx);
575 ret = v4l2_fh_release(file);
577 mutex_unlock(&fimc->lock);
581 static unsigned int fimc_capture_poll(struct file *file,
582 struct poll_table_struct *wait)
584 struct fimc_dev *fimc = video_drvdata(file);
587 if (mutex_lock_interruptible(&fimc->lock))
590 ret = vb2_poll(&fimc->vid_cap.vbq, file, wait);
591 mutex_unlock(&fimc->lock);
596 static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
598 struct fimc_dev *fimc = video_drvdata(file);
601 if (mutex_lock_interruptible(&fimc->lock))
604 ret = vb2_mmap(&fimc->vid_cap.vbq, vma);
605 mutex_unlock(&fimc->lock);
610 static const struct v4l2_file_operations fimc_capture_fops = {
611 .owner = THIS_MODULE,
612 .open = fimc_capture_open,
613 .release = fimc_capture_close,
614 .poll = fimc_capture_poll,
615 .unlocked_ioctl = video_ioctl2,
616 .mmap = fimc_capture_mmap,
620 * Format and crop negotiation helpers
623 static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
624 u32 *width, u32 *height,
625 u32 *code, u32 *fourcc, int pad)
627 bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
628 struct fimc_dev *fimc = ctx->fimc_dev;
629 struct fimc_variant *var = fimc->variant;
630 struct fimc_pix_limit *pl = var->pix_limit;
631 struct fimc_frame *dst = &ctx->d_frame;
632 u32 depth, min_w, max_w, min_h, align_h = 3;
633 u32 mask = FMT_FLAGS_CAM;
634 struct fimc_fmt *ffmt;
636 /* Conversion from/to JPEG or User Defined format is not supported */
637 if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
638 fimc_fmt_is_user_defined(ctx->s_frame.fmt->color))
639 *code = ctx->s_frame.fmt->mbus_code;
641 if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad != FIMC_SD_PAD_SINK)
642 mask |= FMT_FLAGS_M2M;
644 ffmt = fimc_find_format(fourcc, code, mask, 0);
648 *code = ffmt->mbus_code;
650 *fourcc = ffmt->fourcc;
652 if (pad == FIMC_SD_PAD_SINK) {
653 max_w = fimc_fmt_is_user_defined(ffmt->color) ?
654 pl->scaler_dis_w : pl->scaler_en_w;
655 /* Apply the camera input interface pixel constraints */
656 v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
657 height, max_t(u32, *height, 32),
658 FIMC_CAMIF_MAX_HEIGHT,
659 fimc_fmt_is_user_defined(ffmt->color) ?
664 /* Can't scale or crop in transparent (JPEG) transfer mode */
665 if (fimc_fmt_is_user_defined(ffmt->color)) {
666 *width = ctx->s_frame.f_width;
667 *height = ctx->s_frame.f_height;
670 /* Apply the scaler and the output DMA constraints */
671 max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
672 if (ctx->state & FIMC_COMPOSE) {
673 min_w = dst->offs_h + dst->width;
674 min_h = dst->offs_v + dst->height;
676 min_w = var->min_out_pixsize;
677 min_h = var->min_out_pixsize;
679 if (var->min_vsize_align == 1 && !rotation)
680 align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
682 depth = fimc_get_format_depth(ffmt);
683 v4l_bound_align_image(width, min_w, max_w,
684 ffs(var->min_out_pixsize) - 1,
685 height, min_h, FIMC_CAMIF_MAX_HEIGHT,
687 64/(ALIGN(depth, 8)));
689 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
690 pad, code ? *code : 0, *width, *height,
691 dst->f_width, dst->f_height);
696 static void fimc_capture_try_selection(struct fimc_ctx *ctx,
700 bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
701 struct fimc_dev *fimc = ctx->fimc_dev;
702 struct fimc_variant *var = fimc->variant;
703 struct fimc_pix_limit *pl = var->pix_limit;
704 struct fimc_frame *sink = &ctx->s_frame;
705 u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
706 u32 align_sz = 0, align_h = 4;
707 u32 max_sc_h, max_sc_v;
709 /* In JPEG transparent transfer mode cropping is not supported */
710 if (fimc_fmt_is_user_defined(ctx->d_frame.fmt->color)) {
711 r->width = sink->f_width;
712 r->height = sink->f_height;
713 r->left = r->top = 0;
716 if (target == V4L2_SEL_TGT_COMPOSE) {
717 if (ctx->rotation != 90 && ctx->rotation != 270)
719 max_sc_h = min(SCALER_MAX_HRATIO, 1 << (ffs(sink->width) - 3));
720 max_sc_v = min(SCALER_MAX_VRATIO, 1 << (ffs(sink->height) - 1));
721 min_sz = var->min_out_pixsize;
723 u32 depth = fimc_get_format_depth(sink->fmt);
724 align_sz = 64/ALIGN(depth, 8);
725 min_sz = var->min_inp_pixsize;
726 min_w = min_h = min_sz;
727 max_sc_h = max_sc_v = 1;
730 * For the compose rectangle the following constraints must be met:
731 * - it must fit in the sink pad format rectangle (f_width/f_height);
732 * - maximum downscaling ratio is 64;
733 * - maximum crop size depends if the rotator is used or not;
734 * - the sink pad format width/height must be 4 multiple of the
735 * prescaler ratios determined by sink pad size and source pad crop,
736 * the prescaler ratio is returned by fimc_get_scaler_factor().
739 rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
740 rotate ? sink->f_height : sink->f_width);
741 max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
743 if (target == V4L2_SEL_TGT_COMPOSE) {
744 min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
745 min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
747 swap(max_sc_h, max_sc_v);
751 v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
752 &r->height, min_h, max_h, align_h,
754 /* Adjust left/top if crop/compose rectangle is out of bounds */
755 r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
756 r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height);
757 r->left = round_down(r->left, var->hor_offs_align);
759 dbg("target %#x: (%d,%d)/%dx%d, sink fmt: %dx%d",
760 target, r->left, r->top, r->width, r->height,
761 sink->f_width, sink->f_height);
765 * The video node ioctl operations
767 static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
768 struct v4l2_capability *cap)
770 struct fimc_dev *fimc = video_drvdata(file);
772 strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
773 strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
774 cap->bus_info[0] = 0;
775 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE_MPLANE;
780 static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv,
781 struct v4l2_fmtdesc *f)
783 struct fimc_fmt *fmt;
785 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
789 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
790 f->pixelformat = fmt->fourcc;
791 if (fmt->fourcc == V4L2_MBUS_FMT_JPEG_1X8)
792 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
797 * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
799 * @ctx: FIMC capture context
800 * @tfmt: media bus format to try/set on subdevs
801 * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
802 * @set: true to set format on subdevs, false to try only
804 static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
805 struct v4l2_mbus_framefmt *tfmt,
806 struct fimc_fmt **fmt_id,
809 struct fimc_dev *fimc = ctx->fimc_dev;
810 struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
811 struct v4l2_subdev *csis = fimc->pipeline.subdevs[IDX_CSIS];
812 struct v4l2_subdev_format sfmt;
813 struct v4l2_mbus_framefmt *mf = &sfmt.format;
814 struct fimc_fmt *ffmt = NULL;
817 if (WARN_ON(!sd || !tfmt))
820 memset(&sfmt, 0, sizeof(sfmt));
823 sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
825 ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
829 * Notify user-space if common pixel code for
830 * host and sensor does not exist.
834 mf->code = tfmt->code = ffmt->mbus_code;
836 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
839 if (mf->code != tfmt->code) {
843 if (mf->width != tfmt->width || mf->height != tfmt->height) {
844 u32 fcc = ffmt->fourcc;
845 tfmt->width = mf->width;
846 tfmt->height = mf->height;
847 ffmt = fimc_capture_try_format(ctx,
848 &tfmt->width, &tfmt->height,
849 NULL, &fcc, FIMC_SD_PAD_SOURCE);
850 if (ffmt && ffmt->mbus_code)
851 mf->code = ffmt->mbus_code;
852 if (mf->width != tfmt->width ||
853 mf->height != tfmt->height)
855 tfmt->code = mf->code;
858 ret = v4l2_subdev_call(csis, pad, set_fmt, NULL, &sfmt);
860 if (mf->code == tfmt->code &&
861 mf->width == tfmt->width && mf->height == tfmt->height)
869 dbg("code: 0x%x, %dx%d, %p", mf->code, mf->width, mf->height, ffmt);
874 * fimc_get_sensor_frame_desc - query the sensor for media bus frame parameters
875 * @sensor: pointer to the sensor subdev
876 * @plane_fmt: provides plane sizes corresponding to the frame layout entries
877 * @try: true to set the frame parameters, false to query only
879 * This function is used by this driver only for compressed/blob data formats.
881 static int fimc_get_sensor_frame_desc(struct v4l2_subdev *sensor,
882 struct v4l2_plane_pix_format *plane_fmt,
883 unsigned int num_planes, bool try)
885 struct v4l2_mbus_frame_desc fd;
888 for (i = 0; i < num_planes; i++)
889 fd.entry[i].length = plane_fmt[i].sizeimage;
892 ret = v4l2_subdev_call(sensor, pad, set_frame_desc, 0, &fd);
894 ret = v4l2_subdev_call(sensor, pad, get_frame_desc, 0, &fd);
899 if (num_planes != fd.num_entries)
902 for (i = 0; i < num_planes; i++)
903 plane_fmt[i].sizeimage = fd.entry[i].length;
905 if (fd.entry[0].length > FIMC_MAX_JPEG_BUF_SIZE) {
906 v4l2_err(sensor->v4l2_dev, "Unsupported buffer size: %u\n",
915 static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
916 struct v4l2_format *f)
918 struct fimc_dev *fimc = video_drvdata(file);
919 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
921 return fimc_fill_format(&ctx->d_frame, f);
924 static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
925 struct v4l2_format *f)
927 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
928 struct fimc_dev *fimc = video_drvdata(file);
929 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
930 struct v4l2_mbus_framefmt mf;
931 struct fimc_fmt *ffmt = NULL;
933 if (fimc_jpeg_fourcc(pix->pixelformat)) {
934 fimc_capture_try_format(ctx, &pix->width, &pix->height,
935 NULL, &pix->pixelformat,
937 ctx->s_frame.f_width = pix->width;
938 ctx->s_frame.f_height = pix->height;
940 ffmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
941 NULL, &pix->pixelformat,
946 if (!fimc->vid_cap.user_subdev_api) {
947 mf.width = pix->width;
948 mf.height = pix->height;
949 mf.code = ffmt->mbus_code;
950 fimc_md_graph_lock(fimc);
951 fimc_pipeline_try_format(ctx, &mf, &ffmt, false);
952 fimc_md_graph_unlock(fimc);
953 pix->width = mf.width;
954 pix->height = mf.height;
956 pix->pixelformat = ffmt->fourcc;
959 fimc_adjust_mplane_format(ffmt, pix->width, pix->height, pix);
961 if (ffmt->flags & FMT_FLAGS_COMPRESSED)
962 fimc_get_sensor_frame_desc(fimc->pipeline.subdevs[IDX_SENSOR],
963 pix->plane_fmt, ffmt->memplanes, true);
968 static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx,
969 enum fimc_color_fmt color)
971 bool jpeg = fimc_fmt_is_user_defined(color);
973 ctx->scaler.enabled = !jpeg;
974 fimc_ctrls_activate(ctx, !jpeg);
977 set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
979 clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
982 static int fimc_capture_set_format(struct fimc_dev *fimc, struct v4l2_format *f)
984 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
985 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
986 struct v4l2_mbus_framefmt *mf = &fimc->vid_cap.mf;
987 struct fimc_frame *ff = &ctx->d_frame;
988 struct fimc_fmt *s_fmt = NULL;
991 if (vb2_is_busy(&fimc->vid_cap.vbq))
994 /* Pre-configure format at camera interface input, for JPEG only */
995 if (fimc_jpeg_fourcc(pix->pixelformat)) {
996 fimc_capture_try_format(ctx, &pix->width, &pix->height,
997 NULL, &pix->pixelformat,
999 ctx->s_frame.f_width = pix->width;
1000 ctx->s_frame.f_height = pix->height;
1002 /* Try the format at the scaler and the DMA output */
1003 ff->fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
1004 NULL, &pix->pixelformat,
1005 FIMC_SD_PAD_SOURCE);
1009 /* Update RGB Alpha control state and value range */
1010 fimc_alpha_ctrl_update(ctx);
1012 /* Try to match format at the host and the sensor */
1013 if (!fimc->vid_cap.user_subdev_api) {
1014 mf->code = ff->fmt->mbus_code;
1015 mf->width = pix->width;
1016 mf->height = pix->height;
1018 fimc_md_graph_lock(fimc);
1019 ret = fimc_pipeline_try_format(ctx, mf, &s_fmt, true);
1020 fimc_md_graph_unlock(fimc);
1023 pix->width = mf->width;
1024 pix->height = mf->height;
1027 fimc_adjust_mplane_format(ff->fmt, pix->width, pix->height, pix);
1029 if (ff->fmt->flags & FMT_FLAGS_COMPRESSED) {
1030 ret = fimc_get_sensor_frame_desc(fimc->pipeline.subdevs[IDX_SENSOR],
1031 pix->plane_fmt, ff->fmt->memplanes,
1037 for (i = 0; i < ff->fmt->memplanes; i++)
1038 ff->payload[i] = pix->plane_fmt[i].sizeimage;
1040 set_frame_bounds(ff, pix->width, pix->height);
1041 /* Reset the composition rectangle if not yet configured */
1042 if (!(ctx->state & FIMC_COMPOSE))
1043 set_frame_crop(ff, 0, 0, pix->width, pix->height);
1045 fimc_capture_mark_jpeg_xfer(ctx, ff->fmt->color);
1047 /* Reset cropping and set format at the camera interface input */
1048 if (!fimc->vid_cap.user_subdev_api) {
1049 ctx->s_frame.fmt = s_fmt;
1050 set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
1051 set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
1057 static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
1058 struct v4l2_format *f)
1060 struct fimc_dev *fimc = video_drvdata(file);
1062 return fimc_capture_set_format(fimc, f);
1065 static int fimc_cap_enum_input(struct file *file, void *priv,
1066 struct v4l2_input *i)
1068 struct fimc_dev *fimc = video_drvdata(file);
1069 struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
1074 i->type = V4L2_INPUT_TYPE_CAMERA;
1076 strlcpy(i->name, sd->name, sizeof(i->name));
1080 static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
1082 return i == 0 ? i : -EINVAL;
1085 static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
1092 * fimc_pipeline_validate - check for formats inconsistencies
1093 * between source and sink pad of each link
1095 * Return 0 if all formats match or -EPIPE otherwise.
1097 static int fimc_pipeline_validate(struct fimc_dev *fimc)
1099 struct v4l2_subdev_format sink_fmt, src_fmt;
1100 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
1101 struct v4l2_subdev *sd;
1102 struct media_pad *pad;
1105 /* Start with the video capture node pad */
1106 pad = media_entity_remote_source(&vid_cap->vd_pad);
1109 /* FIMC.{N} subdevice */
1110 sd = media_entity_to_v4l2_subdev(pad->entity);
1113 /* Retrieve format at the sink pad */
1114 pad = &sd->entity.pads[0];
1115 if (!(pad->flags & MEDIA_PAD_FL_SINK))
1117 /* Don't call FIMC subdev operation to avoid nested locking */
1118 if (sd == &fimc->vid_cap.subdev) {
1119 struct fimc_frame *ff = &vid_cap->ctx->s_frame;
1120 sink_fmt.format.width = ff->f_width;
1121 sink_fmt.format.height = ff->f_height;
1122 sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
1124 sink_fmt.pad = pad->index;
1125 sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1126 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
1127 if (ret < 0 && ret != -ENOIOCTLCMD)
1130 /* Retrieve format at the source pad */
1131 pad = media_entity_remote_source(pad);
1133 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1136 sd = media_entity_to_v4l2_subdev(pad->entity);
1137 src_fmt.pad = pad->index;
1138 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1139 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
1140 if (ret < 0 && ret != -ENOIOCTLCMD)
1143 if (src_fmt.format.width != sink_fmt.format.width ||
1144 src_fmt.format.height != sink_fmt.format.height ||
1145 src_fmt.format.code != sink_fmt.format.code)
1148 if (sd == fimc->pipeline.subdevs[IDX_SENSOR] &&
1149 fimc_user_defined_mbus_fmt(src_fmt.format.code)) {
1150 struct v4l2_plane_pix_format plane_fmt[FIMC_MAX_PLANES];
1151 struct fimc_frame *frame = &vid_cap->ctx->d_frame;
1154 ret = fimc_get_sensor_frame_desc(sd, plane_fmt,
1155 frame->fmt->memplanes,
1160 for (i = 0; i < frame->fmt->memplanes; i++)
1161 if (frame->payload[i] < plane_fmt[i].sizeimage)
1168 static int fimc_cap_streamon(struct file *file, void *priv,
1169 enum v4l2_buf_type type)
1171 struct fimc_dev *fimc = video_drvdata(file);
1172 struct fimc_pipeline *p = &fimc->pipeline;
1173 struct v4l2_subdev *sd = p->subdevs[IDX_SENSOR];
1176 if (fimc_capture_active(fimc))
1179 ret = media_entity_pipeline_start(&sd->entity, p->m_pipeline);
1183 if (fimc->vid_cap.user_subdev_api) {
1184 ret = fimc_pipeline_validate(fimc);
1186 media_entity_pipeline_stop(&sd->entity);
1190 return vb2_streamon(&fimc->vid_cap.vbq, type);
1193 static int fimc_cap_streamoff(struct file *file, void *priv,
1194 enum v4l2_buf_type type)
1196 struct fimc_dev *fimc = video_drvdata(file);
1197 struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
1200 ret = vb2_streamoff(&fimc->vid_cap.vbq, type);
1202 media_entity_pipeline_stop(&sd->entity);
1206 static int fimc_cap_reqbufs(struct file *file, void *priv,
1207 struct v4l2_requestbuffers *reqbufs)
1209 struct fimc_dev *fimc = video_drvdata(file);
1210 int ret = vb2_reqbufs(&fimc->vid_cap.vbq, reqbufs);
1213 fimc->vid_cap.reqbufs_count = reqbufs->count;
1217 static int fimc_cap_querybuf(struct file *file, void *priv,
1218 struct v4l2_buffer *buf)
1220 struct fimc_dev *fimc = video_drvdata(file);
1222 return vb2_querybuf(&fimc->vid_cap.vbq, buf);
1225 static int fimc_cap_qbuf(struct file *file, void *priv,
1226 struct v4l2_buffer *buf)
1228 struct fimc_dev *fimc = video_drvdata(file);
1230 return vb2_qbuf(&fimc->vid_cap.vbq, buf);
1233 static int fimc_cap_expbuf(struct file *file, void *priv,
1234 struct v4l2_exportbuffer *eb)
1236 struct fimc_dev *fimc = video_drvdata(file);
1238 return vb2_expbuf(&fimc->vid_cap.vbq, eb);
1241 static int fimc_cap_dqbuf(struct file *file, void *priv,
1242 struct v4l2_buffer *buf)
1244 struct fimc_dev *fimc = video_drvdata(file);
1246 return vb2_dqbuf(&fimc->vid_cap.vbq, buf, file->f_flags & O_NONBLOCK);
1249 static int fimc_cap_create_bufs(struct file *file, void *priv,
1250 struct v4l2_create_buffers *create)
1252 struct fimc_dev *fimc = video_drvdata(file);
1254 return vb2_create_bufs(&fimc->vid_cap.vbq, create);
1257 static int fimc_cap_prepare_buf(struct file *file, void *priv,
1258 struct v4l2_buffer *b)
1260 struct fimc_dev *fimc = video_drvdata(file);
1262 return vb2_prepare_buf(&fimc->vid_cap.vbq, b);
1265 static int fimc_cap_g_selection(struct file *file, void *fh,
1266 struct v4l2_selection *s)
1268 struct fimc_dev *fimc = video_drvdata(file);
1269 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1270 struct fimc_frame *f = &ctx->s_frame;
1272 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1275 switch (s->target) {
1276 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1277 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1279 case V4L2_SEL_TGT_CROP_BOUNDS:
1280 case V4L2_SEL_TGT_CROP_DEFAULT:
1283 s->r.width = f->o_width;
1284 s->r.height = f->o_height;
1287 case V4L2_SEL_TGT_COMPOSE:
1289 case V4L2_SEL_TGT_CROP:
1290 s->r.left = f->offs_h;
1291 s->r.top = f->offs_v;
1292 s->r.width = f->width;
1293 s->r.height = f->height;
1300 /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
1301 static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
1303 if (a->left < b->left || a->top < b->top)
1305 if (a->left + a->width > b->left + b->width)
1307 if (a->top + a->height > b->top + b->height)
1313 static int fimc_cap_s_selection(struct file *file, void *fh,
1314 struct v4l2_selection *s)
1316 struct fimc_dev *fimc = video_drvdata(file);
1317 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1318 struct v4l2_rect rect = s->r;
1319 struct fimc_frame *f;
1320 unsigned long flags;
1322 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1325 if (s->target == V4L2_SEL_TGT_COMPOSE)
1327 else if (s->target == V4L2_SEL_TGT_CROP)
1332 fimc_capture_try_selection(ctx, &rect, s->target);
1334 if (s->flags & V4L2_SEL_FLAG_LE &&
1335 !enclosed_rectangle(&rect, &s->r))
1338 if (s->flags & V4L2_SEL_FLAG_GE &&
1339 !enclosed_rectangle(&s->r, &rect))
1343 spin_lock_irqsave(&fimc->slock, flags);
1344 set_frame_crop(f, s->r.left, s->r.top, s->r.width,
1346 spin_unlock_irqrestore(&fimc->slock, flags);
1348 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1352 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
1353 .vidioc_querycap = fimc_vidioc_querycap_capture,
1355 .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane,
1356 .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane,
1357 .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
1358 .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane,
1360 .vidioc_reqbufs = fimc_cap_reqbufs,
1361 .vidioc_querybuf = fimc_cap_querybuf,
1363 .vidioc_qbuf = fimc_cap_qbuf,
1364 .vidioc_dqbuf = fimc_cap_dqbuf,
1365 .vidioc_expbuf = fimc_cap_expbuf,
1367 .vidioc_prepare_buf = fimc_cap_prepare_buf,
1368 .vidioc_create_bufs = fimc_cap_create_bufs,
1370 .vidioc_streamon = fimc_cap_streamon,
1371 .vidioc_streamoff = fimc_cap_streamoff,
1373 .vidioc_g_selection = fimc_cap_g_selection,
1374 .vidioc_s_selection = fimc_cap_s_selection,
1376 .vidioc_enum_input = fimc_cap_enum_input,
1377 .vidioc_s_input = fimc_cap_s_input,
1378 .vidioc_g_input = fimc_cap_g_input,
1381 /* Capture subdev media entity operations */
1382 static int fimc_link_setup(struct media_entity *entity,
1383 const struct media_pad *local,
1384 const struct media_pad *remote, u32 flags)
1386 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1387 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1389 if (media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1392 if (WARN_ON(fimc == NULL))
1395 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1396 local->entity->name, remote->entity->name, flags,
1397 fimc->vid_cap.input);
1399 if (flags & MEDIA_LNK_FL_ENABLED) {
1400 if (fimc->vid_cap.input != 0)
1402 fimc->vid_cap.input = sd->grp_id;
1406 fimc->vid_cap.input = 0;
1410 static const struct media_entity_operations fimc_sd_media_ops = {
1411 .link_setup = fimc_link_setup,
1415 * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1416 * @sd: pointer to a subdev generating the notification
1417 * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1418 * @arg: pointer to an u32 type integer that stores the frame payload value
1420 * The End Of Frame notification sent by sensor subdev in its still capture
1421 * mode. If there is only a single VSYNC generated by the sensor at the
1422 * beginning of a frame transmission, FIMC does not issue the LastIrq
1423 * (end of frame) interrupt. And this notification is used to complete the
1424 * frame capture and returning a buffer to user-space. Subdev drivers should
1425 * call this notification from their last 'End of frame capture' interrupt.
1427 void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
1430 struct fimc_sensor_info *sensor;
1431 struct fimc_vid_buffer *buf;
1432 struct fimc_md *fmd;
1433 struct fimc_dev *fimc;
1434 unsigned long flags;
1439 sensor = v4l2_get_subdev_hostdata(sd);
1440 fmd = entity_to_fimc_mdev(&sd->entity);
1442 spin_lock_irqsave(&fmd->slock, flags);
1443 fimc = sensor ? sensor->host : NULL;
1445 if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
1446 test_bit(ST_CAPT_PEND, &fimc->state)) {
1447 unsigned long irq_flags;
1448 spin_lock_irqsave(&fimc->slock, irq_flags);
1449 if (!list_empty(&fimc->vid_cap.active_buf_q)) {
1450 buf = list_entry(fimc->vid_cap.active_buf_q.next,
1451 struct fimc_vid_buffer, list);
1452 vb2_set_plane_payload(&buf->vb, 0, *((u32 *)arg));
1454 fimc_capture_irq_handler(fimc, 1);
1455 fimc_deactivate_capture(fimc);
1456 spin_unlock_irqrestore(&fimc->slock, irq_flags);
1458 spin_unlock_irqrestore(&fmd->slock, flags);
1461 static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1462 struct v4l2_subdev_fh *fh,
1463 struct v4l2_subdev_mbus_code_enum *code)
1465 struct fimc_fmt *fmt;
1467 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1470 code->code = fmt->mbus_code;
1474 static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
1475 struct v4l2_subdev_fh *fh,
1476 struct v4l2_subdev_format *fmt)
1478 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1479 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1480 struct v4l2_mbus_framefmt *mf;
1481 struct fimc_frame *ff;
1483 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1484 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1489 mf->colorspace = V4L2_COLORSPACE_JPEG;
1490 ff = fmt->pad == FIMC_SD_PAD_SINK ? &ctx->s_frame : &ctx->d_frame;
1492 mutex_lock(&fimc->lock);
1493 /* The pixel code is same on both input and output pad */
1494 if (!WARN_ON(ctx->s_frame.fmt == NULL))
1495 mf->code = ctx->s_frame.fmt->mbus_code;
1496 mf->width = ff->f_width;
1497 mf->height = ff->f_height;
1498 mutex_unlock(&fimc->lock);
1503 static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
1504 struct v4l2_subdev_fh *fh,
1505 struct v4l2_subdev_format *fmt)
1507 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1508 struct v4l2_mbus_framefmt *mf = &fmt->format;
1509 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1510 struct fimc_frame *ff;
1511 struct fimc_fmt *ffmt;
1513 dbg("pad%d: code: 0x%x, %dx%d",
1514 fmt->pad, mf->code, mf->width, mf->height);
1516 if (fmt->pad == FIMC_SD_PAD_SOURCE &&
1517 vb2_is_busy(&fimc->vid_cap.vbq))
1520 mutex_lock(&fimc->lock);
1521 ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1522 &mf->code, NULL, fmt->pad);
1523 mutex_unlock(&fimc->lock);
1524 mf->colorspace = V4L2_COLORSPACE_JPEG;
1526 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1527 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1531 /* Update RGB Alpha control state and value range */
1532 fimc_alpha_ctrl_update(ctx);
1534 fimc_capture_mark_jpeg_xfer(ctx, ffmt->color);
1536 ff = fmt->pad == FIMC_SD_PAD_SINK ?
1537 &ctx->s_frame : &ctx->d_frame;
1539 mutex_lock(&fimc->lock);
1540 set_frame_bounds(ff, mf->width, mf->height);
1541 fimc->vid_cap.mf = *mf;
1544 /* Reset the crop rectangle if required. */
1545 if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_COMPOSE)))
1546 set_frame_crop(ff, 0, 0, mf->width, mf->height);
1548 if (fmt->pad == FIMC_SD_PAD_SINK)
1549 ctx->state &= ~FIMC_COMPOSE;
1550 mutex_unlock(&fimc->lock);
1554 static int fimc_subdev_get_selection(struct v4l2_subdev *sd,
1555 struct v4l2_subdev_fh *fh,
1556 struct v4l2_subdev_selection *sel)
1558 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1559 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1560 struct fimc_frame *f = &ctx->s_frame;
1561 struct v4l2_rect *r = &sel->r;
1562 struct v4l2_rect *try_sel;
1564 if (sel->pad != FIMC_SD_PAD_SINK)
1567 mutex_lock(&fimc->lock);
1569 switch (sel->target) {
1570 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1572 case V4L2_SEL_TGT_CROP_BOUNDS:
1573 r->width = f->o_width;
1574 r->height = f->o_height;
1577 mutex_unlock(&fimc->lock);
1580 case V4L2_SEL_TGT_CROP:
1581 try_sel = v4l2_subdev_get_try_crop(fh, sel->pad);
1583 case V4L2_SEL_TGT_COMPOSE:
1584 try_sel = v4l2_subdev_get_try_compose(fh, sel->pad);
1588 mutex_unlock(&fimc->lock);
1592 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1595 r->left = f->offs_h;
1597 r->width = f->width;
1598 r->height = f->height;
1601 dbg("target %#x: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1602 sel->pad, r->left, r->top, r->width, r->height,
1603 f->f_width, f->f_height);
1605 mutex_unlock(&fimc->lock);
1609 static int fimc_subdev_set_selection(struct v4l2_subdev *sd,
1610 struct v4l2_subdev_fh *fh,
1611 struct v4l2_subdev_selection *sel)
1613 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1614 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1615 struct fimc_frame *f = &ctx->s_frame;
1616 struct v4l2_rect *r = &sel->r;
1617 struct v4l2_rect *try_sel;
1618 unsigned long flags;
1620 if (sel->pad != FIMC_SD_PAD_SINK)
1623 mutex_lock(&fimc->lock);
1624 fimc_capture_try_selection(ctx, r, V4L2_SEL_TGT_CROP);
1626 switch (sel->target) {
1627 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1629 case V4L2_SEL_TGT_CROP_BOUNDS:
1630 r->width = f->o_width;
1631 r->height = f->o_height;
1634 mutex_unlock(&fimc->lock);
1637 case V4L2_SEL_TGT_CROP:
1638 try_sel = v4l2_subdev_get_try_crop(fh, sel->pad);
1640 case V4L2_SEL_TGT_COMPOSE:
1641 try_sel = v4l2_subdev_get_try_compose(fh, sel->pad);
1645 mutex_unlock(&fimc->lock);
1649 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1652 spin_lock_irqsave(&fimc->slock, flags);
1653 set_frame_crop(f, r->left, r->top, r->width, r->height);
1654 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1655 spin_unlock_irqrestore(&fimc->slock, flags);
1656 if (sel->target == V4L2_SEL_TGT_COMPOSE)
1657 ctx->state |= FIMC_COMPOSE;
1660 dbg("target %#x: (%d,%d)/%dx%d", sel->target, r->left, r->top,
1661 r->width, r->height);
1663 mutex_unlock(&fimc->lock);
1667 static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
1668 .enum_mbus_code = fimc_subdev_enum_mbus_code,
1669 .get_selection = fimc_subdev_get_selection,
1670 .set_selection = fimc_subdev_set_selection,
1671 .get_fmt = fimc_subdev_get_fmt,
1672 .set_fmt = fimc_subdev_set_fmt,
1675 static struct v4l2_subdev_ops fimc_subdev_ops = {
1676 .pad = &fimc_subdev_pad_ops,
1679 /* Set default format at the sensor and host interface */
1680 static int fimc_capture_set_default_format(struct fimc_dev *fimc)
1682 struct v4l2_format fmt = {
1683 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1687 .pixelformat = V4L2_PIX_FMT_YUYV,
1688 .field = V4L2_FIELD_NONE,
1689 .colorspace = V4L2_COLORSPACE_JPEG,
1693 return fimc_capture_set_format(fimc, &fmt);
1696 /* fimc->lock must be already initialized */
1697 static int fimc_register_capture_device(struct fimc_dev *fimc,
1698 struct v4l2_device *v4l2_dev)
1700 struct video_device *vfd = &fimc->vid_cap.vfd;
1701 struct fimc_vid_cap *vid_cap;
1702 struct fimc_ctx *ctx;
1703 struct vb2_queue *q;
1706 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1710 ctx->fimc_dev = fimc;
1711 ctx->in_path = FIMC_IO_CAMERA;
1712 ctx->out_path = FIMC_IO_DMA;
1713 ctx->state = FIMC_CTX_CAP;
1714 ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1715 ctx->d_frame.fmt = ctx->s_frame.fmt;
1717 memset(vfd, 0, sizeof(*vfd));
1718 snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.capture", fimc->id);
1720 vfd->fops = &fimc_capture_fops;
1721 vfd->ioctl_ops = &fimc_capture_ioctl_ops;
1722 vfd->v4l2_dev = v4l2_dev;
1724 vfd->release = video_device_release_empty;
1725 vfd->lock = &fimc->lock;
1727 video_set_drvdata(vfd, fimc);
1729 vid_cap = &fimc->vid_cap;
1730 vid_cap->active_buf_cnt = 0;
1731 vid_cap->reqbufs_count = 0;
1732 vid_cap->refcnt = 0;
1734 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1735 INIT_LIST_HEAD(&vid_cap->active_buf_q);
1738 q = &fimc->vid_cap.vbq;
1739 memset(q, 0, sizeof(*q));
1740 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1741 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1742 q->drv_priv = fimc->vid_cap.ctx;
1743 q->ops = &fimc_capture_qops;
1744 q->mem_ops = &vb2_dma_contig_memops;
1745 q->buf_struct_size = sizeof(struct fimc_vid_buffer);
1747 ret = vb2_queue_init(q);
1751 vid_cap->vd_pad.flags = MEDIA_PAD_FL_SINK;
1752 ret = media_entity_init(&vfd->entity, 1, &vid_cap->vd_pad, 0);
1756 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
1760 v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n",
1761 vfd->name, video_device_node_name(vfd));
1763 vfd->ctrl_handler = &ctx->ctrls.handler;
1767 media_entity_cleanup(&vfd->entity);
1773 static int fimc_capture_subdev_registered(struct v4l2_subdev *sd)
1775 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1781 ret = fimc_register_m2m_device(fimc, sd->v4l2_dev);
1785 fimc->pipeline_ops = v4l2_get_subdev_hostdata(sd);
1787 ret = fimc_register_capture_device(fimc, sd->v4l2_dev);
1789 fimc_unregister_m2m_device(fimc);
1790 fimc->pipeline_ops = NULL;
1796 static void fimc_capture_subdev_unregistered(struct v4l2_subdev *sd)
1798 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1803 fimc_unregister_m2m_device(fimc);
1805 if (video_is_registered(&fimc->vid_cap.vfd)) {
1806 video_unregister_device(&fimc->vid_cap.vfd);
1807 media_entity_cleanup(&fimc->vid_cap.vfd.entity);
1808 fimc->pipeline_ops = NULL;
1810 kfree(fimc->vid_cap.ctx);
1811 fimc->vid_cap.ctx = NULL;
1814 static const struct v4l2_subdev_internal_ops fimc_capture_sd_internal_ops = {
1815 .registered = fimc_capture_subdev_registered,
1816 .unregistered = fimc_capture_subdev_unregistered,
1819 int fimc_initialize_capture_subdev(struct fimc_dev *fimc)
1821 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1824 v4l2_subdev_init(sd, &fimc_subdev_ops);
1825 sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
1826 snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->pdev->id);
1828 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1829 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1830 ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
1831 fimc->vid_cap.sd_pads, 0);
1835 sd->entity.ops = &fimc_sd_media_ops;
1836 sd->internal_ops = &fimc_capture_sd_internal_ops;
1837 v4l2_set_subdevdata(sd, fimc);
1841 void fimc_unregister_capture_subdev(struct fimc_dev *fimc)
1843 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1845 v4l2_device_unregister_subdev(sd);
1846 media_entity_cleanup(&sd->entity);
1847 v4l2_set_subdevdata(sd, NULL);