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-v4l2.h>
28 #include <media/videobuf2-dma-contig.h>
31 #include "fimc-core.h"
33 #include "media-dev.h"
35 static int fimc_capture_hw_init(struct fimc_dev
*fimc
)
37 struct fimc_source_info
*si
= &fimc
->vid_cap
.source_config
;
38 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
42 if (ctx
== NULL
|| ctx
->s_frame
.fmt
== NULL
)
45 if (si
->fimc_bus_type
== FIMC_BUS_TYPE_ISP_WRITEBACK
) {
46 ret
= fimc_hw_camblk_cfg_writeback(fimc
);
51 spin_lock_irqsave(&fimc
->slock
, flags
);
52 fimc_prepare_dma_offset(ctx
, &ctx
->d_frame
);
53 fimc_set_yuv_order(ctx
);
55 fimc_hw_set_camera_polarity(fimc
, si
);
56 fimc_hw_set_camera_type(fimc
, si
);
57 fimc_hw_set_camera_source(fimc
, si
);
58 fimc_hw_set_camera_offset(fimc
, &ctx
->s_frame
);
60 ret
= fimc_set_scaler_info(ctx
);
62 fimc_hw_set_input_path(ctx
);
63 fimc_hw_set_prescaler(ctx
);
64 fimc_hw_set_mainscaler(ctx
);
65 fimc_hw_set_target_format(ctx
);
66 fimc_hw_set_rotation(ctx
);
67 fimc_hw_set_effect(ctx
);
68 fimc_hw_set_output_path(ctx
);
69 fimc_hw_set_out_dma(ctx
);
70 if (fimc
->drv_data
->alpha_color
)
71 fimc_hw_set_rgb_alpha(ctx
);
72 clear_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
74 spin_unlock_irqrestore(&fimc
->slock
, flags
);
79 * Reinitialize the driver so it is ready to start the streaming again.
80 * Set fimc->state to indicate stream off and the hardware shut down state.
81 * If not suspending (@suspend is false), return any buffers to videobuf2.
82 * Otherwise put any owned buffers onto the pending buffers queue, so they
83 * can be re-spun when the device is being resumed. Also perform FIMC
84 * software reset and disable streaming on the whole pipeline if required.
86 static int fimc_capture_state_cleanup(struct fimc_dev
*fimc
, bool suspend
)
88 struct fimc_vid_cap
*cap
= &fimc
->vid_cap
;
89 struct fimc_vid_buffer
*buf
;
93 spin_lock_irqsave(&fimc
->slock
, flags
);
94 streaming
= fimc
->state
& (1 << ST_CAPT_ISP_STREAM
);
96 fimc
->state
&= ~(1 << ST_CAPT_RUN
| 1 << ST_CAPT_SHUT
|
97 1 << ST_CAPT_STREAM
| 1 << ST_CAPT_ISP_STREAM
);
99 fimc
->state
|= (1 << ST_CAPT_SUSPENDED
);
101 fimc
->state
&= ~(1 << ST_CAPT_PEND
| 1 << ST_CAPT_SUSPENDED
);
103 /* Release unused buffers */
104 while (!suspend
&& !list_empty(&cap
->pending_buf_q
)) {
105 buf
= fimc_pending_queue_pop(cap
);
106 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
108 /* If suspending put unused buffers onto pending queue */
109 while (!list_empty(&cap
->active_buf_q
)) {
110 buf
= fimc_active_queue_pop(cap
);
112 fimc_pending_queue_add(cap
, buf
);
114 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
120 spin_unlock_irqrestore(&fimc
->slock
, flags
);
123 return fimc_pipeline_call(&cap
->ve
, set_stream
, 0);
128 static int fimc_stop_capture(struct fimc_dev
*fimc
, bool suspend
)
132 if (!fimc_capture_active(fimc
))
135 spin_lock_irqsave(&fimc
->slock
, flags
);
136 set_bit(ST_CAPT_SHUT
, &fimc
->state
);
137 fimc_deactivate_capture(fimc
);
138 spin_unlock_irqrestore(&fimc
->slock
, flags
);
140 wait_event_timeout(fimc
->irq_queue
,
141 !test_bit(ST_CAPT_SHUT
, &fimc
->state
),
142 (2*HZ
/10)); /* 200 ms */
144 return fimc_capture_state_cleanup(fimc
, suspend
);
148 * fimc_capture_config_update - apply the camera interface configuration
150 * To be called from within the interrupt handler with fimc.slock
151 * spinlock held. It updates the camera pixel crop, rotation and
154 static int fimc_capture_config_update(struct fimc_ctx
*ctx
)
156 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
159 fimc_hw_set_camera_offset(fimc
, &ctx
->s_frame
);
161 ret
= fimc_set_scaler_info(ctx
);
165 fimc_hw_set_prescaler(ctx
);
166 fimc_hw_set_mainscaler(ctx
);
167 fimc_hw_set_target_format(ctx
);
168 fimc_hw_set_rotation(ctx
);
169 fimc_hw_set_effect(ctx
);
170 fimc_prepare_dma_offset(ctx
, &ctx
->d_frame
);
171 fimc_hw_set_out_dma(ctx
);
172 if (fimc
->drv_data
->alpha_color
)
173 fimc_hw_set_rgb_alpha(ctx
);
175 clear_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
179 void fimc_capture_irq_handler(struct fimc_dev
*fimc
, int deq_buf
)
181 struct fimc_vid_cap
*cap
= &fimc
->vid_cap
;
182 struct fimc_pipeline
*p
= to_fimc_pipeline(cap
->ve
.pipe
);
183 struct v4l2_subdev
*csis
= p
->subdevs
[IDX_CSIS
];
184 struct fimc_frame
*f
= &cap
->ctx
->d_frame
;
185 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 v_buf
= fimc_active_queue_pop(cap
);
196 v_buf
->vb
.vb2_buf
.timestamp
= ktime_get_ns();
197 v_buf
->vb
.sequence
= cap
->frame_count
++;
199 vb2_buffer_done(&v_buf
->vb
.vb2_buf
, VB2_BUF_STATE_DONE
);
202 if (!list_empty(&cap
->pending_buf_q
)) {
204 v_buf
= fimc_pending_queue_pop(cap
);
205 fimc_hw_set_output_addr(fimc
, &v_buf
->paddr
, cap
->buf_index
);
206 v_buf
->index
= cap
->buf_index
;
208 /* Move the buffer to the capture active queue */
209 fimc_active_queue_add(cap
, v_buf
);
211 dbg("next frame: %d, done frame: %d",
212 fimc_hw_get_frame_index(fimc
), v_buf
->index
);
214 if (++cap
->buf_index
>= FIMC_MAX_OUT_BUFS
)
218 * Set up a buffer at MIPI-CSIS if current image format
219 * requires the frame embedded data capture.
221 if (f
->fmt
->mdataplanes
&& !list_empty(&cap
->active_buf_q
)) {
222 unsigned int plane
= ffs(f
->fmt
->mdataplanes
) - 1;
223 unsigned int size
= f
->payload
[plane
];
224 s32 index
= fimc_hw_get_frame_index(fimc
);
227 list_for_each_entry(v_buf
, &cap
->active_buf_q
, list
) {
228 if (v_buf
->index
!= index
)
230 vaddr
= vb2_plane_vaddr(&v_buf
->vb
.vb2_buf
, plane
);
231 v4l2_subdev_call(csis
, video
, s_rx_buffer
,
237 if (cap
->active_buf_cnt
== 0) {
239 clear_bit(ST_CAPT_RUN
, &fimc
->state
);
241 if (++cap
->buf_index
>= FIMC_MAX_OUT_BUFS
)
244 set_bit(ST_CAPT_RUN
, &fimc
->state
);
247 if (test_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
))
248 fimc_capture_config_update(cap
->ctx
);
250 if (cap
->active_buf_cnt
== 1) {
251 fimc_deactivate_capture(fimc
);
252 clear_bit(ST_CAPT_STREAM
, &fimc
->state
);
255 dbg("frame: %d, active_buf_cnt: %d",
256 fimc_hw_get_frame_index(fimc
), cap
->active_buf_cnt
);
260 static int start_streaming(struct vb2_queue
*q
, unsigned int count
)
262 struct fimc_ctx
*ctx
= q
->drv_priv
;
263 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
264 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
268 vid_cap
->frame_count
= 0;
270 ret
= fimc_capture_hw_init(fimc
);
272 fimc_capture_state_cleanup(fimc
, false);
276 set_bit(ST_CAPT_PEND
, &fimc
->state
);
278 min_bufs
= fimc
->vid_cap
.reqbufs_count
> 1 ? 2 : 1;
280 if (vid_cap
->active_buf_cnt
>= min_bufs
&&
281 !test_and_set_bit(ST_CAPT_STREAM
, &fimc
->state
)) {
282 fimc_activate_capture(ctx
);
284 if (!test_and_set_bit(ST_CAPT_ISP_STREAM
, &fimc
->state
))
285 return fimc_pipeline_call(&vid_cap
->ve
, set_stream
, 1);
291 static void stop_streaming(struct vb2_queue
*q
)
293 struct fimc_ctx
*ctx
= q
->drv_priv
;
294 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
296 if (!fimc_capture_active(fimc
))
299 fimc_stop_capture(fimc
, false);
302 int fimc_capture_suspend(struct fimc_dev
*fimc
)
304 bool suspend
= fimc_capture_busy(fimc
);
306 int ret
= fimc_stop_capture(fimc
, suspend
);
309 return fimc_pipeline_call(&fimc
->vid_cap
.ve
, close
);
312 static void buffer_queue(struct vb2_buffer
*vb
);
314 int fimc_capture_resume(struct fimc_dev
*fimc
)
316 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
317 struct exynos_video_entity
*ve
= &vid_cap
->ve
;
318 struct fimc_vid_buffer
*buf
;
321 if (!test_and_clear_bit(ST_CAPT_SUSPENDED
, &fimc
->state
))
324 INIT_LIST_HEAD(&fimc
->vid_cap
.active_buf_q
);
325 vid_cap
->buf_index
= 0;
326 fimc_pipeline_call(ve
, open
, &ve
->vdev
.entity
, false);
327 fimc_capture_hw_init(fimc
);
329 clear_bit(ST_CAPT_SUSPENDED
, &fimc
->state
);
331 for (i
= 0; i
< vid_cap
->reqbufs_count
; i
++) {
332 if (list_empty(&vid_cap
->pending_buf_q
))
334 buf
= fimc_pending_queue_pop(vid_cap
);
335 buffer_queue(&buf
->vb
.vb2_buf
);
341 static int queue_setup(struct vb2_queue
*vq
,
342 unsigned int *num_buffers
, unsigned int *num_planes
,
343 unsigned int sizes
[], struct device
*alloc_devs
[])
345 struct fimc_ctx
*ctx
= vq
->drv_priv
;
346 struct fimc_frame
*frame
= &ctx
->d_frame
;
347 struct fimc_fmt
*fmt
= frame
->fmt
;
348 unsigned long wh
= frame
->f_width
* frame
->f_height
;
355 if (*num_planes
!= fmt
->memplanes
)
357 for (i
= 0; i
< *num_planes
; i
++)
358 if (sizes
[i
] < (wh
* fmt
->depth
[i
]) / 8)
363 *num_planes
= fmt
->memplanes
;
365 for (i
= 0; i
< fmt
->memplanes
; i
++) {
366 unsigned int size
= (wh
* fmt
->depth
[i
]) / 8;
368 if (fimc_fmt_is_user_defined(fmt
->color
))
369 sizes
[i
] = frame
->payload
[i
];
371 sizes
[i
] = max_t(u32
, size
, frame
->payload
[i
]);
377 static int buffer_prepare(struct vb2_buffer
*vb
)
379 struct vb2_queue
*vq
= vb
->vb2_queue
;
380 struct fimc_ctx
*ctx
= vq
->drv_priv
;
383 if (ctx
->d_frame
.fmt
== NULL
)
386 for (i
= 0; i
< ctx
->d_frame
.fmt
->memplanes
; i
++) {
387 unsigned long size
= ctx
->d_frame
.payload
[i
];
389 if (vb2_plane_size(vb
, i
) < size
) {
390 v4l2_err(&ctx
->fimc_dev
->vid_cap
.ve
.vdev
,
391 "User buffer too small (%ld < %ld)\n",
392 vb2_plane_size(vb
, i
), size
);
395 vb2_set_plane_payload(vb
, i
, size
);
401 static void buffer_queue(struct vb2_buffer
*vb
)
403 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
404 struct fimc_vid_buffer
*buf
405 = container_of(vbuf
, struct fimc_vid_buffer
, vb
);
406 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
407 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
408 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
409 struct exynos_video_entity
*ve
= &vid_cap
->ve
;
413 spin_lock_irqsave(&fimc
->slock
, flags
);
414 fimc_prepare_addr(ctx
, &buf
->vb
.vb2_buf
, &ctx
->d_frame
, &buf
->paddr
);
416 if (!test_bit(ST_CAPT_SUSPENDED
, &fimc
->state
) &&
417 !test_bit(ST_CAPT_STREAM
, &fimc
->state
) &&
418 vid_cap
->active_buf_cnt
< FIMC_MAX_OUT_BUFS
) {
419 /* Setup the buffer directly for processing. */
420 int buf_id
= (vid_cap
->reqbufs_count
== 1) ? -1 :
423 fimc_hw_set_output_addr(fimc
, &buf
->paddr
, buf_id
);
424 buf
->index
= vid_cap
->buf_index
;
425 fimc_active_queue_add(vid_cap
, buf
);
427 if (++vid_cap
->buf_index
>= FIMC_MAX_OUT_BUFS
)
428 vid_cap
->buf_index
= 0;
430 fimc_pending_queue_add(vid_cap
, buf
);
433 min_bufs
= vid_cap
->reqbufs_count
> 1 ? 2 : 1;
436 if (vb2_is_streaming(&vid_cap
->vbq
) &&
437 vid_cap
->active_buf_cnt
>= min_bufs
&&
438 !test_and_set_bit(ST_CAPT_STREAM
, &fimc
->state
)) {
441 fimc_activate_capture(ctx
);
442 spin_unlock_irqrestore(&fimc
->slock
, flags
);
444 if (test_and_set_bit(ST_CAPT_ISP_STREAM
, &fimc
->state
))
447 ret
= fimc_pipeline_call(ve
, set_stream
, 1);
449 v4l2_err(&ve
->vdev
, "stream on failed: %d\n", ret
);
452 spin_unlock_irqrestore(&fimc
->slock
, flags
);
455 static const struct vb2_ops fimc_capture_qops
= {
456 .queue_setup
= queue_setup
,
457 .buf_prepare
= buffer_prepare
,
458 .buf_queue
= buffer_queue
,
459 .wait_prepare
= vb2_ops_wait_prepare
,
460 .wait_finish
= vb2_ops_wait_finish
,
461 .start_streaming
= start_streaming
,
462 .stop_streaming
= stop_streaming
,
465 static int fimc_capture_set_default_format(struct fimc_dev
*fimc
);
467 static int fimc_capture_open(struct file
*file
)
469 struct fimc_dev
*fimc
= video_drvdata(file
);
470 struct fimc_vid_cap
*vc
= &fimc
->vid_cap
;
471 struct exynos_video_entity
*ve
= &vc
->ve
;
474 dbg("pid: %d, state: 0x%lx", task_pid_nr(current
), fimc
->state
);
476 mutex_lock(&fimc
->lock
);
478 if (fimc_m2m_active(fimc
))
481 set_bit(ST_CAPT_BUSY
, &fimc
->state
);
482 ret
= pm_runtime_get_sync(&fimc
->pdev
->dev
);
486 ret
= v4l2_fh_open(file
);
488 pm_runtime_put_sync(&fimc
->pdev
->dev
);
492 if (v4l2_fh_is_singular_file(file
)) {
493 fimc_md_graph_lock(ve
);
495 ret
= fimc_pipeline_call(ve
, open
, &ve
->vdev
.entity
, true);
497 if (ret
== 0 && vc
->user_subdev_api
&& vc
->inh_sensor_ctrls
) {
499 * Recreate controls of the the video node to drop
500 * any controls inherited from the sensor subdev.
502 fimc_ctrls_delete(vc
->ctx
);
504 ret
= fimc_ctrls_create(vc
->ctx
);
506 vc
->inh_sensor_ctrls
= false;
509 ve
->vdev
.entity
.use_count
++;
511 fimc_md_graph_unlock(ve
);
514 ret
= fimc_capture_set_default_format(fimc
);
517 clear_bit(ST_CAPT_BUSY
, &fimc
->state
);
518 pm_runtime_put_sync(&fimc
->pdev
->dev
);
519 v4l2_fh_release(file
);
523 mutex_unlock(&fimc
->lock
);
527 static int fimc_capture_release(struct file
*file
)
529 struct fimc_dev
*fimc
= video_drvdata(file
);
530 struct fimc_vid_cap
*vc
= &fimc
->vid_cap
;
531 bool close
= v4l2_fh_is_singular_file(file
);
534 dbg("pid: %d, state: 0x%lx", task_pid_nr(current
), fimc
->state
);
536 mutex_lock(&fimc
->lock
);
538 if (close
&& vc
->streaming
) {
539 media_pipeline_stop(&vc
->ve
.vdev
.entity
);
540 vc
->streaming
= false;
543 ret
= _vb2_fop_release(file
, NULL
);
546 clear_bit(ST_CAPT_BUSY
, &fimc
->state
);
547 fimc_pipeline_call(&vc
->ve
, close
);
548 clear_bit(ST_CAPT_SUSPENDED
, &fimc
->state
);
550 fimc_md_graph_lock(&vc
->ve
);
551 vc
->ve
.vdev
.entity
.use_count
--;
552 fimc_md_graph_unlock(&vc
->ve
);
555 pm_runtime_put_sync(&fimc
->pdev
->dev
);
556 mutex_unlock(&fimc
->lock
);
561 static const struct v4l2_file_operations fimc_capture_fops
= {
562 .owner
= THIS_MODULE
,
563 .open
= fimc_capture_open
,
564 .release
= fimc_capture_release
,
565 .poll
= vb2_fop_poll
,
566 .unlocked_ioctl
= video_ioctl2
,
567 .mmap
= vb2_fop_mmap
,
571 * Format and crop negotiation helpers
574 static struct fimc_fmt
*fimc_capture_try_format(struct fimc_ctx
*ctx
,
575 u32
*width
, u32
*height
,
576 u32
*code
, u32
*fourcc
, int pad
)
578 bool rotation
= ctx
->rotation
== 90 || ctx
->rotation
== 270;
579 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
580 const struct fimc_variant
*var
= fimc
->variant
;
581 const struct fimc_pix_limit
*pl
= var
->pix_limit
;
582 struct fimc_frame
*dst
= &ctx
->d_frame
;
583 u32 depth
, min_w
, max_w
, min_h
, align_h
= 3;
584 u32 mask
= FMT_FLAGS_CAM
;
585 struct fimc_fmt
*ffmt
;
587 /* Conversion from/to JPEG or User Defined format is not supported */
588 if (code
&& ctx
->s_frame
.fmt
&& pad
== FIMC_SD_PAD_SOURCE
&&
589 fimc_fmt_is_user_defined(ctx
->s_frame
.fmt
->color
))
590 *code
= ctx
->s_frame
.fmt
->mbus_code
;
592 if (fourcc
&& *fourcc
!= V4L2_PIX_FMT_JPEG
&& pad
== FIMC_SD_PAD_SOURCE
)
593 mask
|= FMT_FLAGS_M2M
;
595 if (pad
== FIMC_SD_PAD_SINK_FIFO
)
596 mask
= FMT_FLAGS_WRITEBACK
;
598 ffmt
= fimc_find_format(fourcc
, code
, mask
, 0);
603 *code
= ffmt
->mbus_code
;
605 *fourcc
= ffmt
->fourcc
;
607 if (pad
!= FIMC_SD_PAD_SOURCE
) {
608 max_w
= fimc_fmt_is_user_defined(ffmt
->color
) ?
609 pl
->scaler_dis_w
: pl
->scaler_en_w
;
610 /* Apply the camera input interface pixel constraints */
611 v4l_bound_align_image(width
, max_t(u32
, *width
, 32), max_w
, 4,
612 height
, max_t(u32
, *height
, 32),
613 FIMC_CAMIF_MAX_HEIGHT
,
614 fimc_fmt_is_user_defined(ffmt
->color
) ?
619 /* Can't scale or crop in transparent (JPEG) transfer mode */
620 if (fimc_fmt_is_user_defined(ffmt
->color
)) {
621 *width
= ctx
->s_frame
.f_width
;
622 *height
= ctx
->s_frame
.f_height
;
625 /* Apply the scaler and the output DMA constraints */
626 max_w
= rotation
? pl
->out_rot_en_w
: pl
->out_rot_dis_w
;
627 if (ctx
->state
& FIMC_COMPOSE
) {
628 min_w
= dst
->offs_h
+ dst
->width
;
629 min_h
= dst
->offs_v
+ dst
->height
;
631 min_w
= var
->min_out_pixsize
;
632 min_h
= var
->min_out_pixsize
;
634 if (var
->min_vsize_align
== 1 && !rotation
)
635 align_h
= fimc_fmt_is_rgb(ffmt
->color
) ? 0 : 1;
637 depth
= fimc_get_format_depth(ffmt
);
638 v4l_bound_align_image(width
, min_w
, max_w
,
639 ffs(var
->min_out_pixsize
) - 1,
640 height
, min_h
, FIMC_CAMIF_MAX_HEIGHT
,
642 64/(ALIGN(depth
, 8)));
644 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
645 pad
, code
? *code
: 0, *width
, *height
,
646 dst
->f_width
, dst
->f_height
);
651 static void fimc_capture_try_selection(struct fimc_ctx
*ctx
,
655 bool rotate
= ctx
->rotation
== 90 || ctx
->rotation
== 270;
656 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
657 const struct fimc_variant
*var
= fimc
->variant
;
658 const struct fimc_pix_limit
*pl
= var
->pix_limit
;
659 struct fimc_frame
*sink
= &ctx
->s_frame
;
660 u32 max_w
, max_h
, min_w
= 0, min_h
= 0, min_sz
;
661 u32 align_sz
= 0, align_h
= 4;
662 u32 max_sc_h
, max_sc_v
;
664 /* In JPEG transparent transfer mode cropping is not supported */
665 if (fimc_fmt_is_user_defined(ctx
->d_frame
.fmt
->color
)) {
666 r
->width
= sink
->f_width
;
667 r
->height
= sink
->f_height
;
668 r
->left
= r
->top
= 0;
671 if (target
== V4L2_SEL_TGT_COMPOSE
) {
672 if (ctx
->rotation
!= 90 && ctx
->rotation
!= 270)
674 max_sc_h
= min(SCALER_MAX_HRATIO
, 1 << (ffs(sink
->width
) - 3));
675 max_sc_v
= min(SCALER_MAX_VRATIO
, 1 << (ffs(sink
->height
) - 1));
676 min_sz
= var
->min_out_pixsize
;
678 u32 depth
= fimc_get_format_depth(sink
->fmt
);
679 align_sz
= 64/ALIGN(depth
, 8);
680 min_sz
= var
->min_inp_pixsize
;
681 min_w
= min_h
= min_sz
;
682 max_sc_h
= max_sc_v
= 1;
685 * For the compose rectangle the following constraints must be met:
686 * - it must fit in the sink pad format rectangle (f_width/f_height);
687 * - maximum downscaling ratio is 64;
688 * - maximum crop size depends if the rotator is used or not;
689 * - the sink pad format width/height must be 4 multiple of the
690 * prescaler ratios determined by sink pad size and source pad crop,
691 * the prescaler ratio is returned by fimc_get_scaler_factor().
694 rotate
? pl
->out_rot_en_w
: pl
->out_rot_dis_w
,
695 rotate
? sink
->f_height
: sink
->f_width
);
696 max_h
= min_t(u32
, FIMC_CAMIF_MAX_HEIGHT
, sink
->f_height
);
698 if (target
== V4L2_SEL_TGT_COMPOSE
) {
699 min_w
= min_t(u32
, max_w
, sink
->f_width
/ max_sc_h
);
700 min_h
= min_t(u32
, max_h
, sink
->f_height
/ max_sc_v
);
702 swap(max_sc_h
, max_sc_v
);
706 v4l_bound_align_image(&r
->width
, min_w
, max_w
, ffs(min_sz
) - 1,
707 &r
->height
, min_h
, max_h
, align_h
,
709 /* Adjust left/top if crop/compose rectangle is out of bounds */
710 r
->left
= clamp_t(u32
, r
->left
, 0, sink
->f_width
- r
->width
);
711 r
->top
= clamp_t(u32
, r
->top
, 0, sink
->f_height
- r
->height
);
712 r
->left
= round_down(r
->left
, var
->hor_offs_align
);
714 dbg("target %#x: (%d,%d)/%dx%d, sink fmt: %dx%d",
715 target
, r
->left
, r
->top
, r
->width
, r
->height
,
716 sink
->f_width
, sink
->f_height
);
720 * The video node ioctl operations
722 static int fimc_cap_querycap(struct file
*file
, void *priv
,
723 struct v4l2_capability
*cap
)
725 struct fimc_dev
*fimc
= video_drvdata(file
);
727 __fimc_vidioc_querycap(&fimc
->pdev
->dev
, cap
, V4L2_CAP_STREAMING
|
728 V4L2_CAP_VIDEO_CAPTURE_MPLANE
);
732 static int fimc_cap_enum_fmt_mplane(struct file
*file
, void *priv
,
733 struct v4l2_fmtdesc
*f
)
735 struct fimc_fmt
*fmt
;
737 fmt
= fimc_find_format(NULL
, NULL
, FMT_FLAGS_CAM
| FMT_FLAGS_M2M
,
741 strncpy(f
->description
, fmt
->name
, sizeof(f
->description
) - 1);
742 f
->pixelformat
= fmt
->fourcc
;
743 if (fmt
->fourcc
== MEDIA_BUS_FMT_JPEG_1X8
)
744 f
->flags
|= V4L2_FMT_FLAG_COMPRESSED
;
748 static struct media_entity
*fimc_pipeline_get_head(struct media_entity
*me
)
750 struct media_pad
*pad
= &me
->pads
[0];
752 while (!(pad
->flags
& MEDIA_PAD_FL_SOURCE
)) {
753 pad
= media_entity_remote_pad(pad
);
764 * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
766 * @ctx: FIMC capture context
767 * @tfmt: media bus format to try/set on subdevs
768 * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
769 * @set: true to set format on subdevs, false to try only
771 static int fimc_pipeline_try_format(struct fimc_ctx
*ctx
,
772 struct v4l2_mbus_framefmt
*tfmt
,
773 struct fimc_fmt
**fmt_id
,
776 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
777 struct fimc_pipeline
*p
= to_fimc_pipeline(fimc
->vid_cap
.ve
.pipe
);
778 struct v4l2_subdev
*sd
= p
->subdevs
[IDX_SENSOR
];
779 struct v4l2_subdev_format sfmt
;
780 struct v4l2_mbus_framefmt
*mf
= &sfmt
.format
;
781 struct media_entity
*me
;
782 struct fimc_fmt
*ffmt
;
783 struct media_pad
*pad
;
787 if (WARN_ON(!sd
|| !tfmt
))
790 memset(&sfmt
, 0, sizeof(sfmt
));
792 sfmt
.which
= set
? V4L2_SUBDEV_FORMAT_ACTIVE
: V4L2_SUBDEV_FORMAT_TRY
;
794 me
= fimc_pipeline_get_head(&sd
->entity
);
797 ffmt
= fimc_find_format(NULL
, mf
->code
!= 0 ? &mf
->code
: NULL
,
801 * Notify user-space if common pixel code for
802 * host and sensor does not exist.
806 mf
->code
= tfmt
->code
= ffmt
->mbus_code
;
808 /* set format on all pipeline subdevs */
809 while (me
!= &fimc
->vid_cap
.subdev
.entity
) {
810 sd
= media_entity_to_v4l2_subdev(me
);
813 ret
= v4l2_subdev_call(sd
, pad
, set_fmt
, NULL
, &sfmt
);
817 if (me
->pads
[0].flags
& MEDIA_PAD_FL_SINK
) {
818 sfmt
.pad
= me
->num_pads
- 1;
819 mf
->code
= tfmt
->code
;
820 ret
= v4l2_subdev_call(sd
, pad
, set_fmt
, NULL
,
826 pad
= media_entity_remote_pad(&me
->pads
[sfmt
.pad
]);
832 if (mf
->code
!= tfmt
->code
)
836 tfmt
->width
= mf
->width
;
837 tfmt
->height
= mf
->height
;
838 ffmt
= fimc_capture_try_format(ctx
, &tfmt
->width
, &tfmt
->height
,
839 NULL
, &fcc
, FIMC_SD_PAD_SINK_CAM
);
840 ffmt
= fimc_capture_try_format(ctx
, &tfmt
->width
, &tfmt
->height
,
841 NULL
, &fcc
, FIMC_SD_PAD_SOURCE
);
842 if (ffmt
&& ffmt
->mbus_code
)
843 mf
->code
= ffmt
->mbus_code
;
844 if (mf
->width
!= tfmt
->width
|| mf
->height
!= tfmt
->height
)
846 tfmt
->code
= mf
->code
;
858 * fimc_get_sensor_frame_desc - query the sensor for media bus frame parameters
859 * @sensor: pointer to the sensor subdev
860 * @plane_fmt: provides plane sizes corresponding to the frame layout entries
861 * @try: true to set the frame parameters, false to query only
863 * This function is used by this driver only for compressed/blob data formats.
865 static int fimc_get_sensor_frame_desc(struct v4l2_subdev
*sensor
,
866 struct v4l2_plane_pix_format
*plane_fmt
,
867 unsigned int num_planes
, bool try)
869 struct v4l2_mbus_frame_desc fd
;
873 for (i
= 0; i
< num_planes
; i
++)
874 fd
.entry
[i
].length
= plane_fmt
[i
].sizeimage
;
876 pad
= sensor
->entity
.num_pads
- 1;
878 ret
= v4l2_subdev_call(sensor
, pad
, set_frame_desc
, pad
, &fd
);
880 ret
= v4l2_subdev_call(sensor
, pad
, get_frame_desc
, pad
, &fd
);
885 if (num_planes
!= fd
.num_entries
)
888 for (i
= 0; i
< num_planes
; i
++)
889 plane_fmt
[i
].sizeimage
= fd
.entry
[i
].length
;
891 if (fd
.entry
[0].length
> FIMC_MAX_JPEG_BUF_SIZE
) {
892 v4l2_err(sensor
->v4l2_dev
, "Unsupported buffer size: %u\n",
901 static int fimc_cap_g_fmt_mplane(struct file
*file
, void *fh
,
902 struct v4l2_format
*f
)
904 struct fimc_dev
*fimc
= video_drvdata(file
);
906 __fimc_get_format(&fimc
->vid_cap
.ctx
->d_frame
, f
);
911 * Try or set format on the fimc.X.capture video node and additionally
912 * on the whole pipeline if @try is false.
913 * Locking: the caller must _not_ hold the graph mutex.
915 static int __video_try_or_set_format(struct fimc_dev
*fimc
,
916 struct v4l2_format
*f
, bool try,
917 struct fimc_fmt
**inp_fmt
,
918 struct fimc_fmt
**out_fmt
)
920 struct v4l2_pix_format_mplane
*pix
= &f
->fmt
.pix_mp
;
921 struct fimc_vid_cap
*vc
= &fimc
->vid_cap
;
922 struct exynos_video_entity
*ve
= &vc
->ve
;
923 struct fimc_ctx
*ctx
= vc
->ctx
;
924 unsigned int width
= 0, height
= 0;
927 /* Pre-configure format at the camera input interface, for JPEG only */
928 if (fimc_jpeg_fourcc(pix
->pixelformat
)) {
929 fimc_capture_try_format(ctx
, &pix
->width
, &pix
->height
,
930 NULL
, &pix
->pixelformat
,
931 FIMC_SD_PAD_SINK_CAM
);
934 height
= pix
->height
;
936 ctx
->s_frame
.f_width
= pix
->width
;
937 ctx
->s_frame
.f_height
= pix
->height
;
941 /* Try the format at the scaler and the DMA output */
942 *out_fmt
= fimc_capture_try_format(ctx
, &pix
->width
, &pix
->height
,
943 NULL
, &pix
->pixelformat
,
945 if (*out_fmt
== NULL
)
948 /* Restore image width/height for JPEG (no resizing supported). */
949 if (try && fimc_jpeg_fourcc(pix
->pixelformat
)) {
951 pix
->height
= height
;
954 /* Try to match format at the host and the sensor */
955 if (!vc
->user_subdev_api
) {
956 struct v4l2_mbus_framefmt mbus_fmt
;
957 struct v4l2_mbus_framefmt
*mf
;
959 mf
= try ? &mbus_fmt
: &fimc
->vid_cap
.ci_fmt
;
961 mf
->code
= (*out_fmt
)->mbus_code
;
962 mf
->width
= pix
->width
;
963 mf
->height
= pix
->height
;
965 fimc_md_graph_lock(ve
);
966 ret
= fimc_pipeline_try_format(ctx
, mf
, inp_fmt
, try);
967 fimc_md_graph_unlock(ve
);
972 pix
->width
= mf
->width
;
973 pix
->height
= mf
->height
;
976 fimc_adjust_mplane_format(*out_fmt
, pix
->width
, pix
->height
, pix
);
978 if ((*out_fmt
)->flags
& FMT_FLAGS_COMPRESSED
) {
979 struct v4l2_subdev
*sensor
;
981 fimc_md_graph_lock(ve
);
983 sensor
= __fimc_md_get_subdev(ve
->pipe
, IDX_SENSOR
);
985 fimc_get_sensor_frame_desc(sensor
, pix
->plane_fmt
,
986 (*out_fmt
)->memplanes
, try);
990 fimc_md_graph_unlock(ve
);
996 static int fimc_cap_try_fmt_mplane(struct file
*file
, void *fh
,
997 struct v4l2_format
*f
)
999 struct fimc_dev
*fimc
= video_drvdata(file
);
1000 struct fimc_fmt
*out_fmt
= NULL
, *inp_fmt
= NULL
;
1002 return __video_try_or_set_format(fimc
, f
, true, &inp_fmt
, &out_fmt
);
1005 static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx
*ctx
,
1006 enum fimc_color_fmt color
)
1008 bool jpeg
= fimc_fmt_is_user_defined(color
);
1010 ctx
->scaler
.enabled
= !jpeg
;
1011 fimc_ctrls_activate(ctx
, !jpeg
);
1014 set_bit(ST_CAPT_JPEG
, &ctx
->fimc_dev
->state
);
1016 clear_bit(ST_CAPT_JPEG
, &ctx
->fimc_dev
->state
);
1019 static int __fimc_capture_set_format(struct fimc_dev
*fimc
,
1020 struct v4l2_format
*f
)
1022 struct fimc_vid_cap
*vc
= &fimc
->vid_cap
;
1023 struct fimc_ctx
*ctx
= vc
->ctx
;
1024 struct v4l2_pix_format_mplane
*pix
= &f
->fmt
.pix_mp
;
1025 struct fimc_frame
*ff
= &ctx
->d_frame
;
1026 struct fimc_fmt
*inp_fmt
= NULL
;
1029 if (vb2_is_busy(&fimc
->vid_cap
.vbq
))
1032 ret
= __video_try_or_set_format(fimc
, f
, false, &inp_fmt
, &ff
->fmt
);
1036 /* Update RGB Alpha control state and value range */
1037 fimc_alpha_ctrl_update(ctx
);
1039 for (i
= 0; i
< ff
->fmt
->memplanes
; i
++) {
1040 ff
->bytesperline
[i
] = pix
->plane_fmt
[i
].bytesperline
;
1041 ff
->payload
[i
] = pix
->plane_fmt
[i
].sizeimage
;
1044 set_frame_bounds(ff
, pix
->width
, pix
->height
);
1045 /* Reset the composition rectangle if not yet configured */
1046 if (!(ctx
->state
& FIMC_COMPOSE
))
1047 set_frame_crop(ff
, 0, 0, pix
->width
, pix
->height
);
1049 fimc_capture_mark_jpeg_xfer(ctx
, ff
->fmt
->color
);
1051 /* Reset cropping and set format at the camera interface input */
1052 if (!vc
->user_subdev_api
) {
1053 ctx
->s_frame
.fmt
= inp_fmt
;
1054 set_frame_bounds(&ctx
->s_frame
, pix
->width
, pix
->height
);
1055 set_frame_crop(&ctx
->s_frame
, 0, 0, pix
->width
, pix
->height
);
1061 static int fimc_cap_s_fmt_mplane(struct file
*file
, void *priv
,
1062 struct v4l2_format
*f
)
1064 struct fimc_dev
*fimc
= video_drvdata(file
);
1066 return __fimc_capture_set_format(fimc
, f
);
1069 static int fimc_cap_enum_input(struct file
*file
, void *priv
,
1070 struct v4l2_input
*i
)
1072 struct fimc_dev
*fimc
= video_drvdata(file
);
1073 struct exynos_video_entity
*ve
= &fimc
->vid_cap
.ve
;
1074 struct v4l2_subdev
*sd
;
1079 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1080 fimc_md_graph_lock(ve
);
1081 sd
= __fimc_md_get_subdev(ve
->pipe
, IDX_SENSOR
);
1082 fimc_md_graph_unlock(ve
);
1085 strlcpy(i
->name
, sd
->name
, sizeof(i
->name
));
1090 static int fimc_cap_s_input(struct file
*file
, void *priv
, unsigned int i
)
1092 return i
== 0 ? i
: -EINVAL
;
1095 static int fimc_cap_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1102 * fimc_pipeline_validate - check for formats inconsistencies
1103 * between source and sink pad of each link
1105 * Return 0 if all formats match or -EPIPE otherwise.
1107 static int fimc_pipeline_validate(struct fimc_dev
*fimc
)
1109 struct v4l2_subdev_format sink_fmt
, src_fmt
;
1110 struct fimc_vid_cap
*vc
= &fimc
->vid_cap
;
1111 struct v4l2_subdev
*sd
= &vc
->subdev
;
1112 struct fimc_pipeline
*p
= to_fimc_pipeline(vc
->ve
.pipe
);
1113 struct media_pad
*sink_pad
, *src_pad
;
1118 * Find current entity sink pad and any remote sink pad linked
1119 * to it. We stop if there is no sink pad in current entity or
1120 * it is not linked to any other remote entity.
1124 for (i
= 0; i
< sd
->entity
.num_pads
; i
++) {
1125 struct media_pad
*p
= &sd
->entity
.pads
[i
];
1127 if (p
->flags
& MEDIA_PAD_FL_SINK
) {
1129 src_pad
= media_entity_remote_pad(sink_pad
);
1135 if (!src_pad
|| !is_media_entity_v4l2_subdev(src_pad
->entity
))
1138 /* Don't call FIMC subdev operation to avoid nested locking */
1139 if (sd
== &vc
->subdev
) {
1140 struct fimc_frame
*ff
= &vc
->ctx
->s_frame
;
1141 sink_fmt
.format
.width
= ff
->f_width
;
1142 sink_fmt
.format
.height
= ff
->f_height
;
1143 sink_fmt
.format
.code
= ff
->fmt
? ff
->fmt
->mbus_code
: 0;
1145 sink_fmt
.pad
= sink_pad
->index
;
1146 sink_fmt
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
1147 ret
= v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, &sink_fmt
);
1148 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
1152 /* Retrieve format at the source pad */
1153 sd
= media_entity_to_v4l2_subdev(src_pad
->entity
);
1154 src_fmt
.pad
= src_pad
->index
;
1155 src_fmt
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
1156 ret
= v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, &src_fmt
);
1157 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
1160 if (src_fmt
.format
.width
!= sink_fmt
.format
.width
||
1161 src_fmt
.format
.height
!= sink_fmt
.format
.height
||
1162 src_fmt
.format
.code
!= sink_fmt
.format
.code
)
1165 if (sd
== p
->subdevs
[IDX_SENSOR
] &&
1166 fimc_user_defined_mbus_fmt(src_fmt
.format
.code
)) {
1167 struct v4l2_plane_pix_format plane_fmt
[FIMC_MAX_PLANES
];
1168 struct fimc_frame
*frame
= &vc
->ctx
->d_frame
;
1171 ret
= fimc_get_sensor_frame_desc(sd
, plane_fmt
,
1172 frame
->fmt
->memplanes
,
1177 for (i
= 0; i
< frame
->fmt
->memplanes
; i
++)
1178 if (frame
->payload
[i
] < plane_fmt
[i
].sizeimage
)
1185 static int fimc_cap_streamon(struct file
*file
, void *priv
,
1186 enum v4l2_buf_type type
)
1188 struct fimc_dev
*fimc
= video_drvdata(file
);
1189 struct fimc_vid_cap
*vc
= &fimc
->vid_cap
;
1190 struct media_entity
*entity
= &vc
->ve
.vdev
.entity
;
1191 struct fimc_source_info
*si
= NULL
;
1192 struct v4l2_subdev
*sd
;
1195 if (fimc_capture_active(fimc
))
1198 ret
= media_pipeline_start(entity
, &vc
->ve
.pipe
->mp
);
1202 sd
= __fimc_md_get_subdev(vc
->ve
.pipe
, IDX_SENSOR
);
1204 si
= v4l2_get_subdev_hostdata(sd
);
1211 * Save configuration data related to currently attached image
1212 * sensor or other data source, e.g. FIMC-IS.
1214 vc
->source_config
= *si
;
1216 if (vc
->input
== GRP_ID_FIMC_IS
)
1217 vc
->source_config
.fimc_bus_type
= FIMC_BUS_TYPE_ISP_WRITEBACK
;
1219 if (vc
->user_subdev_api
) {
1220 ret
= fimc_pipeline_validate(fimc
);
1225 ret
= vb2_ioctl_streamon(file
, priv
, type
);
1227 vc
->streaming
= true;
1232 media_pipeline_stop(entity
);
1236 static int fimc_cap_streamoff(struct file
*file
, void *priv
,
1237 enum v4l2_buf_type type
)
1239 struct fimc_dev
*fimc
= video_drvdata(file
);
1240 struct fimc_vid_cap
*vc
= &fimc
->vid_cap
;
1243 ret
= vb2_ioctl_streamoff(file
, priv
, type
);
1247 media_pipeline_stop(&vc
->ve
.vdev
.entity
);
1248 vc
->streaming
= false;
1252 static int fimc_cap_reqbufs(struct file
*file
, void *priv
,
1253 struct v4l2_requestbuffers
*reqbufs
)
1255 struct fimc_dev
*fimc
= video_drvdata(file
);
1258 ret
= vb2_ioctl_reqbufs(file
, priv
, reqbufs
);
1261 fimc
->vid_cap
.reqbufs_count
= reqbufs
->count
;
1266 static int fimc_cap_g_selection(struct file
*file
, void *fh
,
1267 struct v4l2_selection
*s
)
1269 struct fimc_dev
*fimc
= video_drvdata(file
);
1270 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1271 struct fimc_frame
*f
= &ctx
->s_frame
;
1273 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1276 switch (s
->target
) {
1277 case V4L2_SEL_TGT_COMPOSE_DEFAULT
:
1278 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
1281 case V4L2_SEL_TGT_CROP_BOUNDS
:
1282 case V4L2_SEL_TGT_CROP_DEFAULT
:
1285 s
->r
.width
= f
->o_width
;
1286 s
->r
.height
= f
->o_height
;
1289 case V4L2_SEL_TGT_COMPOSE
:
1292 case V4L2_SEL_TGT_CROP
:
1293 s
->r
.left
= f
->offs_h
;
1294 s
->r
.top
= f
->offs_v
;
1295 s
->r
.width
= f
->width
;
1296 s
->r
.height
= f
->height
;
1303 /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
1304 static int enclosed_rectangle(struct v4l2_rect
*a
, struct v4l2_rect
*b
)
1306 if (a
->left
< b
->left
|| a
->top
< b
->top
)
1308 if (a
->left
+ a
->width
> b
->left
+ b
->width
)
1310 if (a
->top
+ a
->height
> b
->top
+ b
->height
)
1316 static int fimc_cap_s_selection(struct file
*file
, void *fh
,
1317 struct v4l2_selection
*s
)
1319 struct fimc_dev
*fimc
= video_drvdata(file
);
1320 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1321 struct v4l2_rect rect
= s
->r
;
1322 struct fimc_frame
*f
;
1323 unsigned long flags
;
1325 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1328 if (s
->target
== V4L2_SEL_TGT_COMPOSE
)
1330 else if (s
->target
== V4L2_SEL_TGT_CROP
)
1335 fimc_capture_try_selection(ctx
, &rect
, s
->target
);
1337 if (s
->flags
& V4L2_SEL_FLAG_LE
&&
1338 !enclosed_rectangle(&rect
, &s
->r
))
1341 if (s
->flags
& V4L2_SEL_FLAG_GE
&&
1342 !enclosed_rectangle(&s
->r
, &rect
))
1346 spin_lock_irqsave(&fimc
->slock
, flags
);
1347 set_frame_crop(f
, s
->r
.left
, s
->r
.top
, s
->r
.width
,
1349 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1351 set_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
1355 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops
= {
1356 .vidioc_querycap
= fimc_cap_querycap
,
1358 .vidioc_enum_fmt_vid_cap_mplane
= fimc_cap_enum_fmt_mplane
,
1359 .vidioc_try_fmt_vid_cap_mplane
= fimc_cap_try_fmt_mplane
,
1360 .vidioc_s_fmt_vid_cap_mplane
= fimc_cap_s_fmt_mplane
,
1361 .vidioc_g_fmt_vid_cap_mplane
= fimc_cap_g_fmt_mplane
,
1363 .vidioc_reqbufs
= fimc_cap_reqbufs
,
1364 .vidioc_querybuf
= vb2_ioctl_querybuf
,
1365 .vidioc_qbuf
= vb2_ioctl_qbuf
,
1366 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
1367 .vidioc_expbuf
= vb2_ioctl_expbuf
,
1368 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
1369 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
1371 .vidioc_streamon
= fimc_cap_streamon
,
1372 .vidioc_streamoff
= fimc_cap_streamoff
,
1374 .vidioc_g_selection
= fimc_cap_g_selection
,
1375 .vidioc_s_selection
= fimc_cap_s_selection
,
1377 .vidioc_enum_input
= fimc_cap_enum_input
,
1378 .vidioc_s_input
= fimc_cap_s_input
,
1379 .vidioc_g_input
= fimc_cap_g_input
,
1382 /* Capture subdev media entity operations */
1383 static int fimc_link_setup(struct media_entity
*entity
,
1384 const struct media_pad
*local
,
1385 const struct media_pad
*remote
, u32 flags
)
1387 struct v4l2_subdev
*sd
= media_entity_to_v4l2_subdev(entity
);
1388 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1389 struct fimc_vid_cap
*vc
= &fimc
->vid_cap
;
1390 struct v4l2_subdev
*sensor
;
1392 if (!is_media_entity_v4l2_subdev(remote
->entity
))
1395 if (WARN_ON(fimc
== NULL
))
1398 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1399 local
->entity
->name
, remote
->entity
->name
, flags
,
1400 fimc
->vid_cap
.input
);
1402 if (!(flags
& MEDIA_LNK_FL_ENABLED
)) {
1403 fimc
->vid_cap
.input
= 0;
1410 vc
->input
= sd
->grp_id
;
1412 if (vc
->user_subdev_api
|| vc
->inh_sensor_ctrls
)
1415 /* Inherit V4L2 controls from the image sensor subdev. */
1416 sensor
= fimc_find_remote_sensor(&vc
->subdev
.entity
);
1420 return v4l2_ctrl_add_handler(&vc
->ctx
->ctrls
.handler
,
1421 sensor
->ctrl_handler
, NULL
);
1424 static const struct media_entity_operations fimc_sd_media_ops
= {
1425 .link_setup
= fimc_link_setup
,
1429 * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1430 * @sd: pointer to a subdev generating the notification
1431 * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1432 * @arg: pointer to an u32 type integer that stores the frame payload value
1434 * The End Of Frame notification sent by sensor subdev in its still capture
1435 * mode. If there is only a single VSYNC generated by the sensor at the
1436 * beginning of a frame transmission, FIMC does not issue the LastIrq
1437 * (end of frame) interrupt. And this notification is used to complete the
1438 * frame capture and returning a buffer to user-space. Subdev drivers should
1439 * call this notification from their last 'End of frame capture' interrupt.
1441 void fimc_sensor_notify(struct v4l2_subdev
*sd
, unsigned int notification
,
1444 struct fimc_source_info
*si
;
1445 struct fimc_vid_buffer
*buf
;
1446 struct fimc_md
*fmd
;
1447 struct fimc_dev
*fimc
;
1448 unsigned long flags
;
1453 si
= v4l2_get_subdev_hostdata(sd
);
1454 fmd
= entity_to_fimc_mdev(&sd
->entity
);
1456 spin_lock_irqsave(&fmd
->slock
, flags
);
1458 fimc
= si
? source_to_sensor_info(si
)->host
: NULL
;
1460 if (fimc
&& arg
&& notification
== S5P_FIMC_TX_END_NOTIFY
&&
1461 test_bit(ST_CAPT_PEND
, &fimc
->state
)) {
1462 unsigned long irq_flags
;
1463 spin_lock_irqsave(&fimc
->slock
, irq_flags
);
1464 if (!list_empty(&fimc
->vid_cap
.active_buf_q
)) {
1465 buf
= list_entry(fimc
->vid_cap
.active_buf_q
.next
,
1466 struct fimc_vid_buffer
, list
);
1467 vb2_set_plane_payload(&buf
->vb
.vb2_buf
, 0,
1470 fimc_capture_irq_handler(fimc
, 1);
1471 fimc_deactivate_capture(fimc
);
1472 spin_unlock_irqrestore(&fimc
->slock
, irq_flags
);
1474 spin_unlock_irqrestore(&fmd
->slock
, flags
);
1477 static int fimc_subdev_enum_mbus_code(struct v4l2_subdev
*sd
,
1478 struct v4l2_subdev_pad_config
*cfg
,
1479 struct v4l2_subdev_mbus_code_enum
*code
)
1481 struct fimc_fmt
*fmt
;
1483 fmt
= fimc_find_format(NULL
, NULL
, FMT_FLAGS_CAM
, code
->index
);
1486 code
->code
= fmt
->mbus_code
;
1490 static int fimc_subdev_get_fmt(struct v4l2_subdev
*sd
,
1491 struct v4l2_subdev_pad_config
*cfg
,
1492 struct v4l2_subdev_format
*fmt
)
1494 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1495 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1496 struct fimc_frame
*ff
= &ctx
->s_frame
;
1497 struct v4l2_mbus_framefmt
*mf
;
1499 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1500 mf
= v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
);
1506 mutex_lock(&fimc
->lock
);
1509 case FIMC_SD_PAD_SOURCE
:
1510 if (!WARN_ON(ff
->fmt
== NULL
))
1511 mf
->code
= ff
->fmt
->mbus_code
;
1512 /* Sink pads crop rectangle size */
1513 mf
->width
= ff
->width
;
1514 mf
->height
= ff
->height
;
1516 case FIMC_SD_PAD_SINK_FIFO
:
1517 *mf
= fimc
->vid_cap
.wb_fmt
;
1519 case FIMC_SD_PAD_SINK_CAM
:
1521 *mf
= fimc
->vid_cap
.ci_fmt
;
1525 mutex_unlock(&fimc
->lock
);
1526 mf
->colorspace
= V4L2_COLORSPACE_JPEG
;
1531 static int fimc_subdev_set_fmt(struct v4l2_subdev
*sd
,
1532 struct v4l2_subdev_pad_config
*cfg
,
1533 struct v4l2_subdev_format
*fmt
)
1535 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1536 struct v4l2_mbus_framefmt
*mf
= &fmt
->format
;
1537 struct fimc_vid_cap
*vc
= &fimc
->vid_cap
;
1538 struct fimc_ctx
*ctx
= vc
->ctx
;
1539 struct fimc_frame
*ff
;
1540 struct fimc_fmt
*ffmt
;
1542 dbg("pad%d: code: 0x%x, %dx%d",
1543 fmt
->pad
, mf
->code
, mf
->width
, mf
->height
);
1545 if (fmt
->pad
== FIMC_SD_PAD_SOURCE
&& vb2_is_busy(&vc
->vbq
))
1548 mutex_lock(&fimc
->lock
);
1549 ffmt
= fimc_capture_try_format(ctx
, &mf
->width
, &mf
->height
,
1550 &mf
->code
, NULL
, fmt
->pad
);
1551 mutex_unlock(&fimc
->lock
);
1552 mf
->colorspace
= V4L2_COLORSPACE_JPEG
;
1554 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1555 mf
= v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
);
1559 /* There must be a bug in the driver if this happens */
1560 if (WARN_ON(ffmt
== NULL
))
1563 /* Update RGB Alpha control state and value range */
1564 fimc_alpha_ctrl_update(ctx
);
1566 fimc_capture_mark_jpeg_xfer(ctx
, ffmt
->color
);
1567 if (fmt
->pad
== FIMC_SD_PAD_SOURCE
) {
1569 /* Sink pads crop rectangle size */
1570 mf
->width
= ctx
->s_frame
.width
;
1571 mf
->height
= ctx
->s_frame
.height
;
1576 mutex_lock(&fimc
->lock
);
1577 set_frame_bounds(ff
, mf
->width
, mf
->height
);
1579 if (fmt
->pad
== FIMC_SD_PAD_SINK_FIFO
)
1581 else if (fmt
->pad
== FIMC_SD_PAD_SINK_CAM
)
1586 /* Reset the crop rectangle if required. */
1587 if (!(fmt
->pad
== FIMC_SD_PAD_SOURCE
&& (ctx
->state
& FIMC_COMPOSE
)))
1588 set_frame_crop(ff
, 0, 0, mf
->width
, mf
->height
);
1590 if (fmt
->pad
!= FIMC_SD_PAD_SOURCE
)
1591 ctx
->state
&= ~FIMC_COMPOSE
;
1593 mutex_unlock(&fimc
->lock
);
1597 static int fimc_subdev_get_selection(struct v4l2_subdev
*sd
,
1598 struct v4l2_subdev_pad_config
*cfg
,
1599 struct v4l2_subdev_selection
*sel
)
1601 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1602 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1603 struct fimc_frame
*f
= &ctx
->s_frame
;
1604 struct v4l2_rect
*r
= &sel
->r
;
1605 struct v4l2_rect
*try_sel
;
1607 if (sel
->pad
== FIMC_SD_PAD_SOURCE
)
1610 mutex_lock(&fimc
->lock
);
1612 switch (sel
->target
) {
1613 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
1616 case V4L2_SEL_TGT_CROP_BOUNDS
:
1617 r
->width
= f
->o_width
;
1618 r
->height
= f
->o_height
;
1621 mutex_unlock(&fimc
->lock
);
1624 case V4L2_SEL_TGT_CROP
:
1625 try_sel
= v4l2_subdev_get_try_crop(sd
, cfg
, sel
->pad
);
1627 case V4L2_SEL_TGT_COMPOSE
:
1628 try_sel
= v4l2_subdev_get_try_compose(sd
, cfg
, sel
->pad
);
1632 mutex_unlock(&fimc
->lock
);
1636 if (sel
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1639 r
->left
= f
->offs_h
;
1641 r
->width
= f
->width
;
1642 r
->height
= f
->height
;
1645 dbg("target %#x: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1646 sel
->pad
, r
->left
, r
->top
, r
->width
, r
->height
,
1647 f
->f_width
, f
->f_height
);
1649 mutex_unlock(&fimc
->lock
);
1653 static int fimc_subdev_set_selection(struct v4l2_subdev
*sd
,
1654 struct v4l2_subdev_pad_config
*cfg
,
1655 struct v4l2_subdev_selection
*sel
)
1657 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1658 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1659 struct fimc_frame
*f
= &ctx
->s_frame
;
1660 struct v4l2_rect
*r
= &sel
->r
;
1661 struct v4l2_rect
*try_sel
;
1662 unsigned long flags
;
1664 if (sel
->pad
== FIMC_SD_PAD_SOURCE
)
1667 mutex_lock(&fimc
->lock
);
1668 fimc_capture_try_selection(ctx
, r
, V4L2_SEL_TGT_CROP
);
1670 switch (sel
->target
) {
1671 case V4L2_SEL_TGT_CROP
:
1672 try_sel
= v4l2_subdev_get_try_crop(sd
, cfg
, sel
->pad
);
1674 case V4L2_SEL_TGT_COMPOSE
:
1675 try_sel
= v4l2_subdev_get_try_compose(sd
, cfg
, sel
->pad
);
1679 mutex_unlock(&fimc
->lock
);
1683 if (sel
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1686 spin_lock_irqsave(&fimc
->slock
, flags
);
1687 set_frame_crop(f
, r
->left
, r
->top
, r
->width
, r
->height
);
1688 set_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
1689 if (sel
->target
== V4L2_SEL_TGT_COMPOSE
)
1690 ctx
->state
|= FIMC_COMPOSE
;
1691 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1694 dbg("target %#x: (%d,%d)/%dx%d", sel
->target
, r
->left
, r
->top
,
1695 r
->width
, r
->height
);
1697 mutex_unlock(&fimc
->lock
);
1701 static const struct v4l2_subdev_pad_ops fimc_subdev_pad_ops
= {
1702 .enum_mbus_code
= fimc_subdev_enum_mbus_code
,
1703 .get_selection
= fimc_subdev_get_selection
,
1704 .set_selection
= fimc_subdev_set_selection
,
1705 .get_fmt
= fimc_subdev_get_fmt
,
1706 .set_fmt
= fimc_subdev_set_fmt
,
1709 static const struct v4l2_subdev_ops fimc_subdev_ops
= {
1710 .pad
= &fimc_subdev_pad_ops
,
1713 /* Set default format at the sensor and host interface */
1714 static int fimc_capture_set_default_format(struct fimc_dev
*fimc
)
1716 struct v4l2_format fmt
= {
1717 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
,
1719 .width
= FIMC_DEFAULT_WIDTH
,
1720 .height
= FIMC_DEFAULT_HEIGHT
,
1721 .pixelformat
= V4L2_PIX_FMT_YUYV
,
1722 .field
= V4L2_FIELD_NONE
,
1723 .colorspace
= V4L2_COLORSPACE_JPEG
,
1727 return __fimc_capture_set_format(fimc
, &fmt
);
1730 /* fimc->lock must be already initialized */
1731 static int fimc_register_capture_device(struct fimc_dev
*fimc
,
1732 struct v4l2_device
*v4l2_dev
)
1734 struct video_device
*vfd
= &fimc
->vid_cap
.ve
.vdev
;
1735 struct vb2_queue
*q
= &fimc
->vid_cap
.vbq
;
1736 struct fimc_ctx
*ctx
;
1737 struct fimc_vid_cap
*vid_cap
;
1738 struct fimc_fmt
*fmt
;
1741 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
1745 ctx
->fimc_dev
= fimc
;
1746 ctx
->in_path
= FIMC_IO_CAMERA
;
1747 ctx
->out_path
= FIMC_IO_DMA
;
1748 ctx
->state
= FIMC_CTX_CAP
;
1749 ctx
->s_frame
.fmt
= fimc_find_format(NULL
, NULL
, FMT_FLAGS_CAM
, 0);
1750 ctx
->d_frame
.fmt
= ctx
->s_frame
.fmt
;
1752 memset(vfd
, 0, sizeof(*vfd
));
1753 snprintf(vfd
->name
, sizeof(vfd
->name
), "fimc.%d.capture", fimc
->id
);
1755 vfd
->fops
= &fimc_capture_fops
;
1756 vfd
->ioctl_ops
= &fimc_capture_ioctl_ops
;
1757 vfd
->v4l2_dev
= v4l2_dev
;
1759 vfd
->release
= video_device_release_empty
;
1761 vfd
->lock
= &fimc
->lock
;
1763 video_set_drvdata(vfd
, fimc
);
1764 vid_cap
= &fimc
->vid_cap
;
1765 vid_cap
->active_buf_cnt
= 0;
1766 vid_cap
->reqbufs_count
= 0;
1769 INIT_LIST_HEAD(&vid_cap
->pending_buf_q
);
1770 INIT_LIST_HEAD(&vid_cap
->active_buf_q
);
1772 memset(q
, 0, sizeof(*q
));
1773 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
1774 q
->io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
;
1776 q
->ops
= &fimc_capture_qops
;
1777 q
->mem_ops
= &vb2_dma_contig_memops
;
1778 q
->buf_struct_size
= sizeof(struct fimc_vid_buffer
);
1779 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1780 q
->lock
= &fimc
->lock
;
1781 q
->dev
= &fimc
->pdev
->dev
;
1783 ret
= vb2_queue_init(q
);
1787 /* Default format configuration */
1788 fmt
= fimc_find_format(NULL
, NULL
, FMT_FLAGS_CAM
, 0);
1789 vid_cap
->ci_fmt
.width
= FIMC_DEFAULT_WIDTH
;
1790 vid_cap
->ci_fmt
.height
= FIMC_DEFAULT_HEIGHT
;
1791 vid_cap
->ci_fmt
.code
= fmt
->mbus_code
;
1793 ctx
->s_frame
.width
= FIMC_DEFAULT_WIDTH
;
1794 ctx
->s_frame
.height
= FIMC_DEFAULT_HEIGHT
;
1795 ctx
->s_frame
.fmt
= fmt
;
1797 fmt
= fimc_find_format(NULL
, NULL
, FMT_FLAGS_WRITEBACK
, 0);
1798 vid_cap
->wb_fmt
= vid_cap
->ci_fmt
;
1799 vid_cap
->wb_fmt
.code
= fmt
->mbus_code
;
1801 vid_cap
->vd_pad
.flags
= MEDIA_PAD_FL_SINK
;
1802 vfd
->entity
.function
= MEDIA_ENT_F_PROC_VIDEO_SCALER
;
1803 ret
= media_entity_pads_init(&vfd
->entity
, 1, &vid_cap
->vd_pad
);
1807 ret
= fimc_ctrls_create(ctx
);
1809 goto err_me_cleanup
;
1811 ret
= video_register_device(vfd
, VFL_TYPE_GRABBER
, -1);
1815 v4l2_info(v4l2_dev
, "Registered %s as /dev/%s\n",
1816 vfd
->name
, video_device_node_name(vfd
));
1818 vfd
->ctrl_handler
= &ctx
->ctrls
.handler
;
1822 fimc_ctrls_delete(ctx
);
1824 media_entity_cleanup(&vfd
->entity
);
1830 static int fimc_capture_subdev_registered(struct v4l2_subdev
*sd
)
1832 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1838 ret
= fimc_register_m2m_device(fimc
, sd
->v4l2_dev
);
1842 fimc
->vid_cap
.ve
.pipe
= v4l2_get_subdev_hostdata(sd
);
1844 ret
= fimc_register_capture_device(fimc
, sd
->v4l2_dev
);
1846 fimc_unregister_m2m_device(fimc
);
1847 fimc
->vid_cap
.ve
.pipe
= NULL
;
1853 static void fimc_capture_subdev_unregistered(struct v4l2_subdev
*sd
)
1855 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1856 struct video_device
*vdev
;
1861 mutex_lock(&fimc
->lock
);
1863 fimc_unregister_m2m_device(fimc
);
1864 vdev
= &fimc
->vid_cap
.ve
.vdev
;
1866 if (video_is_registered(vdev
)) {
1867 video_unregister_device(vdev
);
1868 media_entity_cleanup(&vdev
->entity
);
1869 fimc_ctrls_delete(fimc
->vid_cap
.ctx
);
1870 fimc
->vid_cap
.ve
.pipe
= NULL
;
1872 kfree(fimc
->vid_cap
.ctx
);
1873 fimc
->vid_cap
.ctx
= NULL
;
1875 mutex_unlock(&fimc
->lock
);
1878 static const struct v4l2_subdev_internal_ops fimc_capture_sd_internal_ops
= {
1879 .registered
= fimc_capture_subdev_registered
,
1880 .unregistered
= fimc_capture_subdev_unregistered
,
1883 int fimc_initialize_capture_subdev(struct fimc_dev
*fimc
)
1885 struct v4l2_subdev
*sd
= &fimc
->vid_cap
.subdev
;
1888 v4l2_subdev_init(sd
, &fimc_subdev_ops
);
1889 sd
->flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1890 snprintf(sd
->name
, sizeof(sd
->name
), "FIMC.%d", fimc
->id
);
1892 fimc
->vid_cap
.sd_pads
[FIMC_SD_PAD_SINK_CAM
].flags
= MEDIA_PAD_FL_SINK
;
1893 fimc
->vid_cap
.sd_pads
[FIMC_SD_PAD_SINK_FIFO
].flags
= MEDIA_PAD_FL_SINK
;
1894 fimc
->vid_cap
.sd_pads
[FIMC_SD_PAD_SOURCE
].flags
= MEDIA_PAD_FL_SOURCE
;
1895 ret
= media_entity_pads_init(&sd
->entity
, FIMC_SD_PADS_NUM
,
1896 fimc
->vid_cap
.sd_pads
);
1900 sd
->entity
.ops
= &fimc_sd_media_ops
;
1901 sd
->internal_ops
= &fimc_capture_sd_internal_ops
;
1902 v4l2_set_subdevdata(sd
, fimc
);
1906 void fimc_unregister_capture_subdev(struct fimc_dev
*fimc
)
1908 struct v4l2_subdev
*sd
= &fimc
->vid_cap
.subdev
;
1910 v4l2_device_unregister_subdev(sd
);
1911 media_entity_cleanup(&sd
->entity
);
1912 v4l2_set_subdevdata(sd
, NULL
);